1 /* 2 * Copyright (C) 2019 - 2020 Intel Corporation 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 #ifndef _USFSTL_VHOST_H_ 7 #define _USFSTL_VHOST_H_ 8 9 #include "list.h" 10 #include "sched.h" 11 #include "schedctrl.h" 12 #include "vhostproto.h" 13 14 struct usfstl_vhost_user_buf { 15 unsigned int n_in_sg, n_out_sg; 16 struct iovec *in_sg, *out_sg; 17 size_t written; 18 unsigned int idx; 19 bool allocated; 20 }; 21 22 struct usfstl_vhost_user_dev { 23 uint64_t features, protocol_features; 24 struct usfstl_vhost_user_server *server; 25 void *data; 26 }; 27 28 struct usfstl_vhost_user_ops { 29 void (*connected)(struct usfstl_vhost_user_dev *dev); 30 void (*handle)(struct usfstl_vhost_user_dev *dev, 31 struct usfstl_vhost_user_buf *buf, 32 unsigned int vring); 33 void (*disconnected)(struct usfstl_vhost_user_dev *dev); 34 }; 35 36 /** 37 * struct usfstl_vhost_user_server: vhost-user device server 38 */ 39 struct usfstl_vhost_user_server { 40 /** 41 * @ops: various ops for the server/devices 42 */ 43 const struct usfstl_vhost_user_ops *ops; 44 45 /** 46 * @name: socket name to use 47 */ 48 char *socket; 49 50 /** 51 * @interrupt_latency: interrupt latency to model, in 52 * scheduler ticks (actual time then depends on 53 * the scheduler unit) 54 */ 55 unsigned int interrupt_latency; 56 57 /** 58 * @max_queues: max number of virt queues supported 59 */ 60 unsigned int max_queues; 61 62 /** 63 * @input_queues: bitmap of input queues (where to handle interrupts) 64 */ 65 uint64_t input_queues; 66 67 /** 68 * @scheduler: the scheduler to integrate with, 69 * may be %NULL 70 */ 71 struct usfstl_scheduler *scheduler; 72 73 /** 74 * @ctrl: external scheduler control to integrate with, 75 * may be %NULL 76 */ 77 struct usfstl_sched_ctrl *ctrl; 78 79 /** 80 * @features: user features 81 */ 82 uint64_t features; 83 84 /** 85 * @protocol_features: protocol features 86 */ 87 uint64_t protocol_features; 88 89 /** 90 * @config: config data, if supported 91 */ 92 const void *config; 93 94 /** 95 * @config_len: length of config data 96 */ 97 size_t config_len; 98 99 /** 100 * @data: arbitrary user data 101 */ 102 void *data; 103 }; 104 105 /** 106 * usfstl_vhost_user_server_start - start the server 107 */ 108 void usfstl_vhost_user_server_start(struct usfstl_vhost_user_server *server); 109 110 /** 111 * usfstl_vhost_user_server_stop - stop the server 112 * 113 * Note that this doesn't stop the existing devices, and it thus 114 * may be used from the connected callback, if only one connection 115 * is allowed. 116 */ 117 void usfstl_vhost_user_server_stop(struct usfstl_vhost_user_server *server); 118 119 /** 120 * usfstl_vhost_user_dev_notify - send a message on a vring 121 * @dev: device to send to 122 * @vring: vring index to send on 123 * @buf: buffer to send 124 * @buflen: length of the buffer 125 */ 126 void usfstl_vhost_user_dev_notify(struct usfstl_vhost_user_dev *dev, 127 unsigned int vring, 128 const uint8_t *buf, size_t buflen); 129 130 /** 131 * usfstl_vhost_user_config_changed - notify host of a config change event 132 * @dev: device to send to 133 */ 134 void usfstl_vhost_user_config_changed(struct usfstl_vhost_user_dev *dev); 135 136 /** 137 * usfstl_vhost_user_to_va - translate address 138 * @dev: device to translate address for 139 * @addr: guest-side virtual addr 140 */ 141 void *usfstl_vhost_user_to_va(struct usfstl_vhost_user_dev *dev, uint64_t addr); 142 143 /** 144 * usfstl_vhost_phys_to_va - translate address 145 * @dev: device to translate address for 146 * @addr: guest-side physical addr 147 */ 148 void *usfstl_vhost_phys_to_va(struct usfstl_vhost_user_dev *dev, uint64_t addr); 149 150 /* also some IOV helpers */ 151 size_t iov_len(struct iovec *sg, unsigned int nsg); 152 size_t iov_fill(struct iovec *sg, unsigned int nsg, 153 const void *buf, size_t buflen); 154 size_t iov_read(void *buf, size_t buflen, 155 struct iovec *sg, unsigned int nsg); 156 157 #endif // _USFSTL_VHOST_H_ 158