vhost: export vhost feature definitions
[dpdk.git] / lib / librte_vhost / rte_vhost.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #ifndef _RTE_VHOST_H_
6 #define _RTE_VHOST_H_
7
8 /**
9  * @file
10  * Interface to vhost-user
11  */
12
13 #include <stdint.h>
14 #include <sys/eventfd.h>
15
16 #include <rte_memory.h>
17 #include <rte_mempool.h>
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 /* These are not C++-aware. */
24 #include <linux/vhost.h>
25 #include <linux/virtio_ring.h>
26
27 #define RTE_VHOST_USER_CLIENT           (1ULL << 0)
28 #define RTE_VHOST_USER_NO_RECONNECT     (1ULL << 1)
29 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY        (1ULL << 2)
30 #define RTE_VHOST_USER_IOMMU_SUPPORT    (1ULL << 3)
31
32 /** Protocol features. */
33 #ifndef VHOST_USER_PROTOCOL_F_MQ
34 #define VHOST_USER_PROTOCOL_F_MQ        0
35 #endif
36
37 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
38 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
39 #endif
40
41 #ifndef VHOST_USER_PROTOCOL_F_RARP
42 #define VHOST_USER_PROTOCOL_F_RARP      2
43 #endif
44
45 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
46 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
47 #endif
48
49 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
50 #define VHOST_USER_PROTOCOL_F_NET_MTU   4
51 #endif
52
53 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
54 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
55 #endif
56
57 /** Indicate whether protocol features negotiation is supported. */
58 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
59 #define VHOST_USER_F_PROTOCOL_FEATURES  30
60 #endif
61
62 /**
63  * Information relating to memory regions including offsets to
64  * addresses in QEMUs memory file.
65  */
66 struct rte_vhost_mem_region {
67         uint64_t guest_phys_addr;
68         uint64_t guest_user_addr;
69         uint64_t host_user_addr;
70         uint64_t size;
71         void     *mmap_addr;
72         uint64_t mmap_size;
73         int fd;
74 };
75
76 /**
77  * Memory structure includes region and mapping information.
78  */
79 struct rte_vhost_memory {
80         uint32_t nregions;
81         struct rte_vhost_mem_region regions[];
82 };
83
84 struct rte_vhost_vring {
85         struct vring_desc       *desc;
86         struct vring_avail      *avail;
87         struct vring_used       *used;
88         uint64_t                log_guest_addr;
89
90         /** Deprecated, use rte_vhost_vring_call() instead. */
91         int                     callfd;
92
93         int                     kickfd;
94         uint16_t                size;
95 };
96
97 /**
98  * Device and vring operations.
99  */
100 struct vhost_device_ops {
101         int (*new_device)(int vid);             /**< Add device. */
102         void (*destroy_device)(int vid);        /**< Remove device. */
103
104         int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);     /**< triggered when a vring is enabled or disabled */
105
106         /**
107          * Features could be changed after the feature negotiation.
108          * For example, VHOST_F_LOG_ALL will be set/cleared at the
109          * start/end of live migration, respectively. This callback
110          * is used to inform the application on such change.
111          */
112         int (*features_changed)(int vid, uint64_t features);
113
114         int (*new_connection)(int vid);
115         void (*destroy_connection)(int vid);
116
117         void *reserved[2]; /**< Reserved for future extension */
118 };
119
120 /**
121  * Convert guest physical address to host virtual address
122  *
123  * @param mem
124  *  the guest memory regions
125  * @param gpa
126  *  the guest physical address for querying
127  * @return
128  *  the host virtual address on success, 0 on failure
129  */
130 static __rte_always_inline uint64_t
131 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
132 {
133         struct rte_vhost_mem_region *reg;
134         uint32_t i;
135
136         for (i = 0; i < mem->nregions; i++) {
137                 reg = &mem->regions[i];
138                 if (gpa >= reg->guest_phys_addr &&
139                     gpa <  reg->guest_phys_addr + reg->size) {
140                         return gpa - reg->guest_phys_addr +
141                                reg->host_user_addr;
142                 }
143         }
144
145         return 0;
146 }
147
148 #define RTE_VHOST_NEED_LOG(features)    ((features) & (1ULL << VHOST_F_LOG_ALL))
149
150 /**
151  * Log the memory write start with given address.
152  *
153  * This function only need be invoked when the live migration starts.
154  * Therefore, we won't need call it at all in the most of time. For
155  * making the performance impact be minimum, it's suggested to do a
156  * check before calling it:
157  *
158  *        if (unlikely(RTE_VHOST_NEED_LOG(features)))
159  *                rte_vhost_log_write(vid, addr, len);
160  *
161  * @param vid
162  *  vhost device ID
163  * @param addr
164  *  the starting address for write
165  * @param len
166  *  the length to write
167  */
168 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
169
170 /**
171  * Log the used ring update start at given offset.
172  *
173  * Same as rte_vhost_log_write, it's suggested to do a check before
174  * calling it:
175  *
176  *        if (unlikely(RTE_VHOST_NEED_LOG(features)))
177  *                rte_vhost_log_used_vring(vid, vring_idx, offset, len);
178  *
179  * @param vid
180  *  vhost device ID
181  * @param vring_idx
182  *  the vring index
183  * @param offset
184  *  the offset inside the used ring
185  * @param len
186  *  the length to write
187  */
188 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
189                               uint64_t offset, uint64_t len);
190
191 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
192
193 /**
194  * Register vhost driver. path could be different for multiple
195  * instance support.
196  */
197 int rte_vhost_driver_register(const char *path, uint64_t flags);
198
199 /* Unregister vhost driver. This is only meaningful to vhost user. */
200 int rte_vhost_driver_unregister(const char *path);
201
202 /**
203  * Set the feature bits the vhost-user driver supports.
204  *
205  * @param path
206  *  The vhost-user socket file path
207  * @param features
208  *  Supported features
209  * @return
210  *  0 on success, -1 on failure
211  */
212 int rte_vhost_driver_set_features(const char *path, uint64_t features);
213
214 /**
215  * Enable vhost-user driver features.
216  *
217  * Note that
218  * - the param features should be a subset of the feature bits provided
219  *   by rte_vhost_driver_set_features().
220  * - it must be invoked before vhost-user negotiation starts.
221  *
222  * @param path
223  *  The vhost-user socket file path
224  * @param features
225  *  Features to enable
226  * @return
227  *  0 on success, -1 on failure
228  */
229 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
230
231 /**
232  * Disable vhost-user driver features.
233  *
234  * The two notes at rte_vhost_driver_enable_features() also apply here.
235  *
236  * @param path
237  *  The vhost-user socket file path
238  * @param features
239  *  Features to disable
240  * @return
241  *  0 on success, -1 on failure
242  */
243 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
244
245 /**
246  * Get the feature bits before feature negotiation.
247  *
248  * @param path
249  *  The vhost-user socket file path
250  * @param features
251  *  A pointer to store the queried feature bits
252  * @return
253  *  0 on success, -1 on failure
254  */
255 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
256
257 /**
258  * Get the feature bits after negotiation
259  *
260  * @param vid
261  *  Vhost device ID
262  * @param features
263  *  A pointer to store the queried feature bits
264  * @return
265  *  0 on success, -1 on failure
266  */
267 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
268
269 /* Register callbacks. */
270 int rte_vhost_driver_callback_register(const char *path,
271         struct vhost_device_ops const * const ops);
272
273 /**
274  *
275  * Start the vhost-user driver.
276  *
277  * This function triggers the vhost-user negotiation.
278  *
279  * @param path
280  *  The vhost-user socket file path
281  * @return
282  *  0 on success, -1 on failure
283  */
284 int rte_vhost_driver_start(const char *path);
285
286 /**
287  * Get the MTU value of the device if set in QEMU.
288  *
289  * @param vid
290  *  virtio-net device ID
291  * @param mtu
292  *  The variable to store the MTU value
293  *
294  * @return
295  *  0: success
296  *  -EAGAIN: device not yet started
297  *  -ENOTSUP: device does not support MTU feature
298  */
299 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
300
301 /**
302  * Get the numa node from which the virtio net device's memory
303  * is allocated.
304  *
305  * @param vid
306  *  vhost device ID
307  *
308  * @return
309  *  The numa node, -1 on failure
310  */
311 int rte_vhost_get_numa_node(int vid);
312
313 /**
314  * @deprecated
315  * Get the number of queues the device supports.
316  *
317  * Note this function is deprecated, as it returns a queue pair number,
318  * which is vhost specific. Instead, rte_vhost_get_vring_num should
319  * be used.
320  *
321  * @param vid
322  *  vhost device ID
323  *
324  * @return
325  *  The number of queues, 0 on failure
326  */
327 __rte_deprecated
328 uint32_t rte_vhost_get_queue_num(int vid);
329
330 /**
331  * Get the number of vrings the device supports.
332  *
333  * @param vid
334  *  vhost device ID
335  *
336  * @return
337  *  The number of vrings, 0 on failure
338  */
339 uint16_t rte_vhost_get_vring_num(int vid);
340
341 /**
342  * Get the virtio net device's ifname, which is the vhost-user socket
343  * file path.
344  *
345  * @param vid
346  *  vhost device ID
347  * @param buf
348  *  The buffer to stored the queried ifname
349  * @param len
350  *  The length of buf
351  *
352  * @return
353  *  0 on success, -1 on failure
354  */
355 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
356
357 /**
358  * Get how many avail entries are left in the queue
359  *
360  * @param vid
361  *  vhost device ID
362  * @param queue_id
363  *  virtio queue index
364  *
365  * @return
366  *  num of avail entires left
367  */
368 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
369
370 struct rte_mbuf;
371 struct rte_mempool;
372 /**
373  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
374  * be received from the physical port or from another virtual device. A packet
375  * count is returned to indicate the number of packets that were successfully
376  * added to the RX queue.
377  * @param vid
378  *  vhost device ID
379  * @param queue_id
380  *  virtio queue index in mq case
381  * @param pkts
382  *  array to contain packets to be enqueued
383  * @param count
384  *  packets num to be enqueued
385  * @return
386  *  num of packets enqueued
387  */
388 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
389         struct rte_mbuf **pkts, uint16_t count);
390
391 /**
392  * This function gets guest buffers from the virtio device TX virtqueue,
393  * construct host mbufs, copies guest buffer content to host mbufs and
394  * store them in pkts to be processed.
395  * @param vid
396  *  vhost device ID
397  * @param queue_id
398  *  virtio queue index in mq case
399  * @param mbuf_pool
400  *  mbuf_pool where host mbuf is allocated.
401  * @param pkts
402  *  array to contain packets to be dequeued
403  * @param count
404  *  packets num to be dequeued
405  * @return
406  *  num of packets dequeued
407  */
408 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
409         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
410
411 /**
412  * Get guest mem table: a list of memory regions.
413  *
414  * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
415  * guest memory regions. Application should free it at destroy_device()
416  * callback.
417  *
418  * @param vid
419  *  vhost device ID
420  * @param mem
421  *  To store the returned mem regions
422  * @return
423  *  0 on success, -1 on failure
424  */
425 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
426
427 /**
428  * Get guest vring info, including the vring address, vring size, etc.
429  *
430  * @param vid
431  *  vhost device ID
432  * @param vring_idx
433  *  vring index
434  * @param vring
435  *  the structure to hold the requested vring info
436  * @return
437  *  0 on success, -1 on failure
438  */
439 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
440                               struct rte_vhost_vring *vring);
441
442 /**
443  * Notify the guest that used descriptors have been added to the vring.  This
444  * function acts as a memory barrier.
445  *
446  * @param vid
447  *  vhost device ID
448  * @param vring_idx
449  *  vring index
450  * @return
451  *  0 on success, -1 on failure
452  */
453 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
454
455 /**
456  * Get vhost RX queue avail count.
457  *
458  * @param vid
459  *  vhost device ID
460  * @param qid
461  *  virtio queue index in mq case
462  * @return
463  *  num of desc available
464  */
465 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
466
467 #ifdef __cplusplus
468 }
469 #endif
470
471 #endif /* _RTE_VHOST_H_ */