807da8c4bcc0f18eea47896f2fcc053a5543ad93
[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 protocol feature bits before feature negotiation.
294  *
295  * @param path
296  *  The vhost-user socket file path
297  * @param protocol_features
298  *  A pointer to store the queried protocol feature bits
299  * @return
300  *  0 on success, -1 on failure
301  */
302 int __rte_experimental
303 rte_vhost_driver_get_protocol_features(const char *path,
304                 uint64_t *protocol_features);
305
306 /**
307  * Get the queue number bits before feature negotiation.
308  *
309  * @param path
310  *  The vhost-user socket file path
311  * @param queue_num
312  *  A pointer to store the queried queue number bits
313  * @return
314  *  0 on success, -1 on failure
315  */
316 int __rte_experimental
317 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
318
319 /**
320  * Get the feature bits after negotiation
321  *
322  * @param vid
323  *  Vhost device ID
324  * @param features
325  *  A pointer to store the queried feature bits
326  * @return
327  *  0 on success, -1 on failure
328  */
329 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
330
331 /* Register callbacks. */
332 int rte_vhost_driver_callback_register(const char *path,
333         struct vhost_device_ops const * const ops);
334
335 /**
336  *
337  * Start the vhost-user driver.
338  *
339  * This function triggers the vhost-user negotiation.
340  *
341  * @param path
342  *  The vhost-user socket file path
343  * @return
344  *  0 on success, -1 on failure
345  */
346 int rte_vhost_driver_start(const char *path);
347
348 /**
349  * Get the MTU value of the device if set in QEMU.
350  *
351  * @param vid
352  *  virtio-net device ID
353  * @param mtu
354  *  The variable to store the MTU value
355  *
356  * @return
357  *  0: success
358  *  -EAGAIN: device not yet started
359  *  -ENOTSUP: device does not support MTU feature
360  */
361 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
362
363 /**
364  * Get the numa node from which the virtio net device's memory
365  * is allocated.
366  *
367  * @param vid
368  *  vhost device ID
369  *
370  * @return
371  *  The numa node, -1 on failure
372  */
373 int rte_vhost_get_numa_node(int vid);
374
375 /**
376  * @deprecated
377  * Get the number of queues the device supports.
378  *
379  * Note this function is deprecated, as it returns a queue pair number,
380  * which is vhost specific. Instead, rte_vhost_get_vring_num should
381  * be used.
382  *
383  * @param vid
384  *  vhost device ID
385  *
386  * @return
387  *  The number of queues, 0 on failure
388  */
389 __rte_deprecated
390 uint32_t rte_vhost_get_queue_num(int vid);
391
392 /**
393  * Get the number of vrings the device supports.
394  *
395  * @param vid
396  *  vhost device ID
397  *
398  * @return
399  *  The number of vrings, 0 on failure
400  */
401 uint16_t rte_vhost_get_vring_num(int vid);
402
403 /**
404  * Get the virtio net device's ifname, which is the vhost-user socket
405  * file path.
406  *
407  * @param vid
408  *  vhost device ID
409  * @param buf
410  *  The buffer to stored the queried ifname
411  * @param len
412  *  The length of buf
413  *
414  * @return
415  *  0 on success, -1 on failure
416  */
417 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
418
419 /**
420  * Get how many avail entries are left in the queue
421  *
422  * @param vid
423  *  vhost device ID
424  * @param queue_id
425  *  virtio queue index
426  *
427  * @return
428  *  num of avail entires left
429  */
430 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
431
432 struct rte_mbuf;
433 struct rte_mempool;
434 /**
435  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
436  * be received from the physical port or from another virtual device. A packet
437  * count is returned to indicate the number of packets that were successfully
438  * added to the RX queue.
439  * @param vid
440  *  vhost device ID
441  * @param queue_id
442  *  virtio queue index in mq case
443  * @param pkts
444  *  array to contain packets to be enqueued
445  * @param count
446  *  packets num to be enqueued
447  * @return
448  *  num of packets enqueued
449  */
450 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
451         struct rte_mbuf **pkts, uint16_t count);
452
453 /**
454  * This function gets guest buffers from the virtio device TX virtqueue,
455  * construct host mbufs, copies guest buffer content to host mbufs and
456  * store them in pkts to be processed.
457  * @param vid
458  *  vhost device ID
459  * @param queue_id
460  *  virtio queue index in mq case
461  * @param mbuf_pool
462  *  mbuf_pool where host mbuf is allocated.
463  * @param pkts
464  *  array to contain packets to be dequeued
465  * @param count
466  *  packets num to be dequeued
467  * @return
468  *  num of packets dequeued
469  */
470 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
471         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
472
473 /**
474  * Get guest mem table: a list of memory regions.
475  *
476  * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
477  * guest memory regions. Application should free it at destroy_device()
478  * callback.
479  *
480  * @param vid
481  *  vhost device ID
482  * @param mem
483  *  To store the returned mem regions
484  * @return
485  *  0 on success, -1 on failure
486  */
487 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
488
489 /**
490  * Get guest vring info, including the vring address, vring size, etc.
491  *
492  * @param vid
493  *  vhost device ID
494  * @param vring_idx
495  *  vring index
496  * @param vring
497  *  the structure to hold the requested vring info
498  * @return
499  *  0 on success, -1 on failure
500  */
501 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
502                               struct rte_vhost_vring *vring);
503
504 /**
505  * Notify the guest that used descriptors have been added to the vring.  This
506  * function acts as a memory barrier.
507  *
508  * @param vid
509  *  vhost device ID
510  * @param vring_idx
511  *  vring index
512  * @return
513  *  0 on success, -1 on failure
514  */
515 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
516
517 /**
518  * Get vhost RX queue avail count.
519  *
520  * @param vid
521  *  vhost device ID
522  * @param qid
523  *  virtio queue index in mq case
524  * @return
525  *  num of desc available
526  */
527 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
528
529 /**
530  * Get log base and log size of the vhost device
531  *
532  * @param vid
533  *  vhost device ID
534  * @param log_base
535  *  vhost log base
536  * @param log_size
537  *  vhost log size
538  * @return
539  *  0 on success, -1 on failure
540  */
541 int __rte_experimental
542 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
543
544 /**
545  * Get last_avail/used_idx of the vhost virtqueue
546  *
547  * @param vid
548  *  vhost device ID
549  * @param queue_id
550  *  vhost queue index
551  * @param last_avail_idx
552  *  vhost last_avail_idx to get
553  * @param last_used_idx
554  *  vhost last_used_idx to get
555  * @return
556  *  0 on success, -1 on failure
557  */
558 int __rte_experimental
559 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
560                 uint16_t *last_avail_idx, uint16_t *last_used_idx);
561
562 /**
563  * Set last_avail/used_idx of the vhost virtqueue
564  *
565  * @param vid
566  *  vhost device ID
567  * @param queue_id
568  *  vhost queue index
569  * @param last_avail_idx
570  *  last_avail_idx to set
571  * @param last_used_idx
572  *  last_used_idx to set
573  * @return
574  *  0 on success, -1 on failure
575  */
576 int __rte_experimental
577 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
578                 uint16_t last_avail_idx, uint16_t last_used_idx);
579
580 /**
581  * Get vdpa device id for vhost device.
582  *
583  * @param vid
584  *  vhost device id
585  * @return
586  *  device id
587  */
588 int __rte_experimental
589 rte_vhost_get_vdpa_device_id(int vid);
590
591 #ifdef __cplusplus
592 }
593 #endif
594
595 #endif /* _RTE_VHOST_H_ */