8f35167f20a0ce832a29a39fef462e9925fe5305
[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 vdpa device id, enforce single connection per socket
204  *
205  * @param path
206  *  The vhost-user socket file path
207  * @param did
208  *  Device id
209  * @return
210  *  0 on success, -1 on failure
211  */
212 int __rte_experimental
213 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
214
215 /**
216  * Unset the vdpa device id
217  *
218  * @param path
219  *  The vhost-user socket file path
220  * @return
221  *  0 on success, -1 on failure
222  */
223 int __rte_experimental
224 rte_vhost_driver_detach_vdpa_device(const char *path);
225
226 /**
227  * Get the device id
228  *
229  * @param path
230  *  The vhost-user socket file path
231  * @return
232  *  Device id, -1 on failure
233  */
234 int __rte_experimental
235 rte_vhost_driver_get_vdpa_device_id(const char *path);
236
237 /**
238  * Set the feature bits the vhost-user driver supports.
239  *
240  * @param path
241  *  The vhost-user socket file path
242  * @param features
243  *  Supported features
244  * @return
245  *  0 on success, -1 on failure
246  */
247 int rte_vhost_driver_set_features(const char *path, uint64_t features);
248
249 /**
250  * Enable vhost-user driver features.
251  *
252  * Note that
253  * - the param features should be a subset of the feature bits provided
254  *   by rte_vhost_driver_set_features().
255  * - it must be invoked before vhost-user negotiation starts.
256  *
257  * @param path
258  *  The vhost-user socket file path
259  * @param features
260  *  Features to enable
261  * @return
262  *  0 on success, -1 on failure
263  */
264 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
265
266 /**
267  * Disable vhost-user driver features.
268  *
269  * The two notes at rte_vhost_driver_enable_features() also apply here.
270  *
271  * @param path
272  *  The vhost-user socket file path
273  * @param features
274  *  Features to disable
275  * @return
276  *  0 on success, -1 on failure
277  */
278 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
279
280 /**
281  * Get the feature bits before feature negotiation.
282  *
283  * @param path
284  *  The vhost-user socket file path
285  * @param features
286  *  A pointer to store the queried feature bits
287  * @return
288  *  0 on success, -1 on failure
289  */
290 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
291
292 /**
293  * Get the feature bits after negotiation
294  *
295  * @param vid
296  *  Vhost device ID
297  * @param features
298  *  A pointer to store the queried feature bits
299  * @return
300  *  0 on success, -1 on failure
301  */
302 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
303
304 /* Register callbacks. */
305 int rte_vhost_driver_callback_register(const char *path,
306         struct vhost_device_ops const * const ops);
307
308 /**
309  *
310  * Start the vhost-user driver.
311  *
312  * This function triggers the vhost-user negotiation.
313  *
314  * @param path
315  *  The vhost-user socket file path
316  * @return
317  *  0 on success, -1 on failure
318  */
319 int rte_vhost_driver_start(const char *path);
320
321 /**
322  * Get the MTU value of the device if set in QEMU.
323  *
324  * @param vid
325  *  virtio-net device ID
326  * @param mtu
327  *  The variable to store the MTU value
328  *
329  * @return
330  *  0: success
331  *  -EAGAIN: device not yet started
332  *  -ENOTSUP: device does not support MTU feature
333  */
334 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
335
336 /**
337  * Get the numa node from which the virtio net device's memory
338  * is allocated.
339  *
340  * @param vid
341  *  vhost device ID
342  *
343  * @return
344  *  The numa node, -1 on failure
345  */
346 int rte_vhost_get_numa_node(int vid);
347
348 /**
349  * @deprecated
350  * Get the number of queues the device supports.
351  *
352  * Note this function is deprecated, as it returns a queue pair number,
353  * which is vhost specific. Instead, rte_vhost_get_vring_num should
354  * be used.
355  *
356  * @param vid
357  *  vhost device ID
358  *
359  * @return
360  *  The number of queues, 0 on failure
361  */
362 __rte_deprecated
363 uint32_t rte_vhost_get_queue_num(int vid);
364
365 /**
366  * Get the number of vrings the device supports.
367  *
368  * @param vid
369  *  vhost device ID
370  *
371  * @return
372  *  The number of vrings, 0 on failure
373  */
374 uint16_t rte_vhost_get_vring_num(int vid);
375
376 /**
377  * Get the virtio net device's ifname, which is the vhost-user socket
378  * file path.
379  *
380  * @param vid
381  *  vhost device ID
382  * @param buf
383  *  The buffer to stored the queried ifname
384  * @param len
385  *  The length of buf
386  *
387  * @return
388  *  0 on success, -1 on failure
389  */
390 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
391
392 /**
393  * Get how many avail entries are left in the queue
394  *
395  * @param vid
396  *  vhost device ID
397  * @param queue_id
398  *  virtio queue index
399  *
400  * @return
401  *  num of avail entires left
402  */
403 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
404
405 struct rte_mbuf;
406 struct rte_mempool;
407 /**
408  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
409  * be received from the physical port or from another virtual device. A packet
410  * count is returned to indicate the number of packets that were successfully
411  * added to the RX queue.
412  * @param vid
413  *  vhost device ID
414  * @param queue_id
415  *  virtio queue index in mq case
416  * @param pkts
417  *  array to contain packets to be enqueued
418  * @param count
419  *  packets num to be enqueued
420  * @return
421  *  num of packets enqueued
422  */
423 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
424         struct rte_mbuf **pkts, uint16_t count);
425
426 /**
427  * This function gets guest buffers from the virtio device TX virtqueue,
428  * construct host mbufs, copies guest buffer content to host mbufs and
429  * store them in pkts to be processed.
430  * @param vid
431  *  vhost device ID
432  * @param queue_id
433  *  virtio queue index in mq case
434  * @param mbuf_pool
435  *  mbuf_pool where host mbuf is allocated.
436  * @param pkts
437  *  array to contain packets to be dequeued
438  * @param count
439  *  packets num to be dequeued
440  * @return
441  *  num of packets dequeued
442  */
443 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
444         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
445
446 /**
447  * Get guest mem table: a list of memory regions.
448  *
449  * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
450  * guest memory regions. Application should free it at destroy_device()
451  * callback.
452  *
453  * @param vid
454  *  vhost device ID
455  * @param mem
456  *  To store the returned mem regions
457  * @return
458  *  0 on success, -1 on failure
459  */
460 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
461
462 /**
463  * Get guest vring info, including the vring address, vring size, etc.
464  *
465  * @param vid
466  *  vhost device ID
467  * @param vring_idx
468  *  vring index
469  * @param vring
470  *  the structure to hold the requested vring info
471  * @return
472  *  0 on success, -1 on failure
473  */
474 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
475                               struct rte_vhost_vring *vring);
476
477 /**
478  * Notify the guest that used descriptors have been added to the vring.  This
479  * function acts as a memory barrier.
480  *
481  * @param vid
482  *  vhost device ID
483  * @param vring_idx
484  *  vring index
485  * @return
486  *  0 on success, -1 on failure
487  */
488 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
489
490 /**
491  * Get vhost RX queue avail count.
492  *
493  * @param vid
494  *  vhost device ID
495  * @param qid
496  *  virtio queue index in mq case
497  * @return
498  *  num of desc available
499  */
500 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
501
502 /**
503  * Get vdpa device id for vhost device.
504  *
505  * @param vid
506  *  vhost device id
507  * @return
508  *  device id
509  */
510 int __rte_experimental
511 rte_vhost_get_vdpa_device_id(int vid);
512
513 #ifdef __cplusplus
514 }
515 #endif
516
517 #endif /* _RTE_VHOST_H_ */