vhost: add inflight description
[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 #include <linux/virtio_net.h>
27
28 #define RTE_VHOST_USER_CLIENT           (1ULL << 0)
29 #define RTE_VHOST_USER_NO_RECONNECT     (1ULL << 1)
30 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY        (1ULL << 2)
31 #define RTE_VHOST_USER_IOMMU_SUPPORT    (1ULL << 3)
32 #define RTE_VHOST_USER_POSTCOPY_SUPPORT         (1ULL << 4)
33
34 /** Protocol features. */
35 #ifndef VHOST_USER_PROTOCOL_F_MQ
36 #define VHOST_USER_PROTOCOL_F_MQ        0
37 #endif
38
39 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
40 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
41 #endif
42
43 #ifndef VHOST_USER_PROTOCOL_F_RARP
44 #define VHOST_USER_PROTOCOL_F_RARP      2
45 #endif
46
47 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
48 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
49 #endif
50
51 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
52 #define VHOST_USER_PROTOCOL_F_NET_MTU   4
53 #endif
54
55 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
56 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
57 #endif
58
59 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
60 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
61 #endif
62
63 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
64 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
65 #endif
66
67 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
68 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
69 #endif
70
71 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
72 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
73 #endif
74
75 #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
76 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12
77 #endif
78
79 /** Indicate whether protocol features negotiation is supported. */
80 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
81 #define VHOST_USER_F_PROTOCOL_FEATURES  30
82 #endif
83
84 /**
85  * Information relating to memory regions including offsets to
86  * addresses in QEMUs memory file.
87  */
88 struct rte_vhost_mem_region {
89         uint64_t guest_phys_addr;
90         uint64_t guest_user_addr;
91         uint64_t host_user_addr;
92         uint64_t size;
93         void     *mmap_addr;
94         uint64_t mmap_size;
95         int fd;
96 };
97
98 /**
99  * Memory structure includes region and mapping information.
100  */
101 struct rte_vhost_memory {
102         uint32_t nregions;
103         struct rte_vhost_mem_region regions[];
104 };
105
106 struct rte_vhost_vring {
107         struct vring_desc       *desc;
108         struct vring_avail      *avail;
109         struct vring_used       *used;
110         uint64_t                log_guest_addr;
111
112         /** Deprecated, use rte_vhost_vring_call() instead. */
113         int                     callfd;
114
115         int                     kickfd;
116         uint16_t                size;
117 };
118
119 /**
120  * Possible results of the vhost user message handling callbacks
121  */
122 enum rte_vhost_msg_result {
123         /* Message handling failed */
124         RTE_VHOST_MSG_RESULT_ERR = -1,
125         /* Message handling successful */
126         RTE_VHOST_MSG_RESULT_OK =  0,
127         /* Message handling successful and reply prepared */
128         RTE_VHOST_MSG_RESULT_REPLY =  1,
129         /* Message not handled */
130         RTE_VHOST_MSG_RESULT_NOT_HANDLED,
131 };
132
133 /**
134  * Function prototype for the vhost backend to handle specific vhost user
135  * messages.
136  *
137  * @param vid
138  *  vhost device id
139  * @param msg
140  *  Message pointer.
141  * @return
142  *  RTE_VHOST_MSG_RESULT_OK on success,
143  *  RTE_VHOST_MSG_RESULT_REPLY on success with reply,
144  *  RTE_VHOST_MSG_RESULT_ERR on failure,
145  *  RTE_VHOST_MSG_RESULT_NOT_HANDLED if message was not handled.
146  */
147 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
148
149 /**
150  * Optional vhost user message handlers.
151  */
152 struct rte_vhost_user_extern_ops {
153         /* Called prior to the master message handling. */
154         rte_vhost_msg_handle pre_msg_handle;
155         /* Called after the master message handling. */
156         rte_vhost_msg_handle post_msg_handle;
157 };
158
159 /**
160  * Device and vring operations.
161  */
162 struct vhost_device_ops {
163         int (*new_device)(int vid);             /**< Add device. */
164         void (*destroy_device)(int vid);        /**< Remove device. */
165
166         int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);     /**< triggered when a vring is enabled or disabled */
167
168         /**
169          * Features could be changed after the feature negotiation.
170          * For example, VHOST_F_LOG_ALL will be set/cleared at the
171          * start/end of live migration, respectively. This callback
172          * is used to inform the application on such change.
173          */
174         int (*features_changed)(int vid, uint64_t features);
175
176         int (*new_connection)(int vid);
177         void (*destroy_connection)(int vid);
178
179         /**
180          * This callback gets called each time a guest gets notified
181          * about waiting packets. This is the interrupt handling trough
182          * the eventfd_write(callfd), which can be used for counting these
183          * "slow" syscalls.
184          */
185         void (*guest_notified)(int vid);
186
187         void *reserved[1]; /**< Reserved for future extension */
188 };
189
190 /**
191  * Convert guest physical address to host virtual address
192  *
193  * This function is deprecated because unsafe.
194  * New rte_vhost_va_from_guest_pa() should be used instead to ensure
195  * guest physical ranges are fully and contiguously mapped into
196  * process virtual address space.
197  *
198  * @param mem
199  *  the guest memory regions
200  * @param gpa
201  *  the guest physical address for querying
202  * @return
203  *  the host virtual address on success, 0 on failure
204  */
205 __rte_deprecated
206 static __rte_always_inline uint64_t
207 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
208 {
209         struct rte_vhost_mem_region *reg;
210         uint32_t i;
211
212         for (i = 0; i < mem->nregions; i++) {
213                 reg = &mem->regions[i];
214                 if (gpa >= reg->guest_phys_addr &&
215                     gpa <  reg->guest_phys_addr + reg->size) {
216                         return gpa - reg->guest_phys_addr +
217                                reg->host_user_addr;
218                 }
219         }
220
221         return 0;
222 }
223
224 /**
225  * Convert guest physical address to host virtual address safely
226  *
227  * This variant of rte_vhost_gpa_to_vva() takes care all the
228  * requested length is mapped and contiguous in process address
229  * space.
230  *
231  * @param mem
232  *  the guest memory regions
233  * @param gpa
234  *  the guest physical address for querying
235  * @param len
236  *  the size of the requested area to map, updated with actual size mapped
237  * @return
238  *  the host virtual address on success, 0 on failure
239  */
240 __rte_experimental
241 static __rte_always_inline uint64_t
242 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
243                                                    uint64_t gpa, uint64_t *len)
244 {
245         struct rte_vhost_mem_region *r;
246         uint32_t i;
247
248         for (i = 0; i < mem->nregions; i++) {
249                 r = &mem->regions[i];
250                 if (gpa >= r->guest_phys_addr &&
251                     gpa <  r->guest_phys_addr + r->size) {
252
253                         if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
254                                 *len = r->guest_phys_addr + r->size - gpa;
255
256                         return gpa - r->guest_phys_addr +
257                                r->host_user_addr;
258                 }
259         }
260         *len = 0;
261
262         return 0;
263 }
264
265 #define RTE_VHOST_NEED_LOG(features)    ((features) & (1ULL << VHOST_F_LOG_ALL))
266
267 /**
268  * Log the memory write start with given address.
269  *
270  * This function only need be invoked when the live migration starts.
271  * Therefore, we won't need call it at all in the most of time. For
272  * making the performance impact be minimum, it's suggested to do a
273  * check before calling it:
274  *
275  *        if (unlikely(RTE_VHOST_NEED_LOG(features)))
276  *                rte_vhost_log_write(vid, addr, len);
277  *
278  * @param vid
279  *  vhost device ID
280  * @param addr
281  *  the starting address for write (in guest physical address space)
282  * @param len
283  *  the length to write
284  */
285 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
286
287 /**
288  * Log the used ring update start at given offset.
289  *
290  * Same as rte_vhost_log_write, it's suggested to do a check before
291  * calling it:
292  *
293  *        if (unlikely(RTE_VHOST_NEED_LOG(features)))
294  *                rte_vhost_log_used_vring(vid, vring_idx, offset, len);
295  *
296  * @param vid
297  *  vhost device ID
298  * @param vring_idx
299  *  the vring index
300  * @param offset
301  *  the offset inside the used ring
302  * @param len
303  *  the length to write
304  */
305 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
306                               uint64_t offset, uint64_t len);
307
308 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
309
310 /**
311  * Register vhost driver. path could be different for multiple
312  * instance support.
313  */
314 int rte_vhost_driver_register(const char *path, uint64_t flags);
315
316 /* Unregister vhost driver. This is only meaningful to vhost user. */
317 int rte_vhost_driver_unregister(const char *path);
318
319 /**
320  * Set the vdpa device id, enforce single connection per socket
321  *
322  * @param path
323  *  The vhost-user socket file path
324  * @param did
325  *  Device id
326  * @return
327  *  0 on success, -1 on failure
328  */
329 __rte_experimental
330 int
331 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
332
333 /**
334  * Unset the vdpa device id
335  *
336  * @param path
337  *  The vhost-user socket file path
338  * @return
339  *  0 on success, -1 on failure
340  */
341 __rte_experimental
342 int
343 rte_vhost_driver_detach_vdpa_device(const char *path);
344
345 /**
346  * Get the device id
347  *
348  * @param path
349  *  The vhost-user socket file path
350  * @return
351  *  Device id, -1 on failure
352  */
353 __rte_experimental
354 int
355 rte_vhost_driver_get_vdpa_device_id(const char *path);
356
357 /**
358  * Set the feature bits the vhost-user driver supports.
359  *
360  * @param path
361  *  The vhost-user socket file path
362  * @param features
363  *  Supported features
364  * @return
365  *  0 on success, -1 on failure
366  */
367 int rte_vhost_driver_set_features(const char *path, uint64_t features);
368
369 /**
370  * Enable vhost-user driver features.
371  *
372  * Note that
373  * - the param features should be a subset of the feature bits provided
374  *   by rte_vhost_driver_set_features().
375  * - it must be invoked before vhost-user negotiation starts.
376  *
377  * @param path
378  *  The vhost-user socket file path
379  * @param features
380  *  Features to enable
381  * @return
382  *  0 on success, -1 on failure
383  */
384 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
385
386 /**
387  * Disable vhost-user driver features.
388  *
389  * The two notes at rte_vhost_driver_enable_features() also apply here.
390  *
391  * @param path
392  *  The vhost-user socket file path
393  * @param features
394  *  Features to disable
395  * @return
396  *  0 on success, -1 on failure
397  */
398 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
399
400 /**
401  * Get the feature bits before feature negotiation.
402  *
403  * @param path
404  *  The vhost-user socket file path
405  * @param features
406  *  A pointer to store the queried feature bits
407  * @return
408  *  0 on success, -1 on failure
409  */
410 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
411
412 /**
413  * Set the protocol feature bits before feature negotiation.
414  *
415  * @param path
416  *  The vhost-user socket file path
417  * @param protocol_features
418  *  Supported protocol features
419  * @return
420  *  0 on success, -1 on failure
421  */
422 __rte_experimental
423 int
424 rte_vhost_driver_set_protocol_features(const char *path,
425                 uint64_t protocol_features);
426
427 /**
428  * Get the protocol feature bits before feature negotiation.
429  *
430  * @param path
431  *  The vhost-user socket file path
432  * @param protocol_features
433  *  A pointer to store the queried protocol feature bits
434  * @return
435  *  0 on success, -1 on failure
436  */
437 __rte_experimental
438 int
439 rte_vhost_driver_get_protocol_features(const char *path,
440                 uint64_t *protocol_features);
441
442 /**
443  * Get the queue number bits before feature negotiation.
444  *
445  * @param path
446  *  The vhost-user socket file path
447  * @param queue_num
448  *  A pointer to store the queried queue number bits
449  * @return
450  *  0 on success, -1 on failure
451  */
452 __rte_experimental
453 int
454 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
455
456 /**
457  * Get the feature bits after negotiation
458  *
459  * @param vid
460  *  Vhost device ID
461  * @param features
462  *  A pointer to store the queried feature bits
463  * @return
464  *  0 on success, -1 on failure
465  */
466 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
467
468 /* Register callbacks. */
469 int rte_vhost_driver_callback_register(const char *path,
470         struct vhost_device_ops const * const ops);
471
472 /**
473  *
474  * Start the vhost-user driver.
475  *
476  * This function triggers the vhost-user negotiation.
477  *
478  * @param path
479  *  The vhost-user socket file path
480  * @return
481  *  0 on success, -1 on failure
482  */
483 int rte_vhost_driver_start(const char *path);
484
485 /**
486  * Get the MTU value of the device if set in QEMU.
487  *
488  * @param vid
489  *  virtio-net device ID
490  * @param mtu
491  *  The variable to store the MTU value
492  *
493  * @return
494  *  0: success
495  *  -EAGAIN: device not yet started
496  *  -ENOTSUP: device does not support MTU feature
497  */
498 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
499
500 /**
501  * Get the numa node from which the virtio net device's memory
502  * is allocated.
503  *
504  * @param vid
505  *  vhost device ID
506  *
507  * @return
508  *  The numa node, -1 on failure
509  */
510 int rte_vhost_get_numa_node(int vid);
511
512 /**
513  * @deprecated
514  * Get the number of queues the device supports.
515  *
516  * Note this function is deprecated, as it returns a queue pair number,
517  * which is vhost specific. Instead, rte_vhost_get_vring_num should
518  * be used.
519  *
520  * @param vid
521  *  vhost device ID
522  *
523  * @return
524  *  The number of queues, 0 on failure
525  */
526 __rte_deprecated
527 uint32_t rte_vhost_get_queue_num(int vid);
528
529 /**
530  * Get the number of vrings the device supports.
531  *
532  * @param vid
533  *  vhost device ID
534  *
535  * @return
536  *  The number of vrings, 0 on failure
537  */
538 uint16_t rte_vhost_get_vring_num(int vid);
539
540 /**
541  * Get the virtio net device's ifname, which is the vhost-user socket
542  * file path.
543  *
544  * @param vid
545  *  vhost device ID
546  * @param buf
547  *  The buffer to stored the queried ifname
548  * @param len
549  *  The length of buf
550  *
551  * @return
552  *  0 on success, -1 on failure
553  */
554 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
555
556 /**
557  * Get how many avail entries are left in the queue
558  *
559  * @param vid
560  *  vhost device ID
561  * @param queue_id
562  *  virtio queue index
563  *
564  * @return
565  *  num of avail entries left
566  */
567 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
568
569 struct rte_mbuf;
570 struct rte_mempool;
571 /**
572  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
573  * be received from the physical port or from another virtual device. A packet
574  * count is returned to indicate the number of packets that were successfully
575  * added to the RX queue.
576  * @param vid
577  *  vhost device ID
578  * @param queue_id
579  *  virtio queue index in mq case
580  * @param pkts
581  *  array to contain packets to be enqueued
582  * @param count
583  *  packets num to be enqueued
584  * @return
585  *  num of packets enqueued
586  */
587 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
588         struct rte_mbuf **pkts, uint16_t count);
589
590 /**
591  * This function gets guest buffers from the virtio device TX virtqueue,
592  * construct host mbufs, copies guest buffer content to host mbufs and
593  * store them in pkts to be processed.
594  * @param vid
595  *  vhost device ID
596  * @param queue_id
597  *  virtio queue index in mq case
598  * @param mbuf_pool
599  *  mbuf_pool where host mbuf is allocated.
600  * @param pkts
601  *  array to contain packets to be dequeued
602  * @param count
603  *  packets num to be dequeued
604  * @return
605  *  num of packets dequeued
606  */
607 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
608         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
609
610 /**
611  * Get guest mem table: a list of memory regions.
612  *
613  * An rte_vhost_vhost_memory object will be allocated internally, to hold the
614  * guest memory regions. Application should free it at destroy_device()
615  * callback.
616  *
617  * @param vid
618  *  vhost device ID
619  * @param mem
620  *  To store the returned mem regions
621  * @return
622  *  0 on success, -1 on failure
623  */
624 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
625
626 /**
627  * Get guest vring info, including the vring address, vring size, etc.
628  *
629  * @param vid
630  *  vhost device ID
631  * @param vring_idx
632  *  vring index
633  * @param vring
634  *  the structure to hold the requested vring info
635  * @return
636  *  0 on success, -1 on failure
637  */
638 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
639                               struct rte_vhost_vring *vring);
640
641 /**
642  * Notify the guest that used descriptors have been added to the vring.  This
643  * function acts as a memory barrier.
644  *
645  * @param vid
646  *  vhost device ID
647  * @param vring_idx
648  *  vring index
649  * @return
650  *  0 on success, -1 on failure
651  */
652 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
653
654 /**
655  * Get vhost RX queue avail count.
656  *
657  * @param vid
658  *  vhost device ID
659  * @param qid
660  *  virtio queue index in mq case
661  * @return
662  *  num of desc available
663  */
664 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
665
666 /**
667  * Get log base and log size of the vhost device
668  *
669  * @param vid
670  *  vhost device ID
671  * @param log_base
672  *  vhost log base
673  * @param log_size
674  *  vhost log size
675  * @return
676  *  0 on success, -1 on failure
677  */
678 __rte_experimental
679 int
680 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
681
682 /**
683  * Get last_avail/used_idx of the vhost virtqueue
684  *
685  * @param vid
686  *  vhost device ID
687  * @param queue_id
688  *  vhost queue index
689  * @param last_avail_idx
690  *  vhost last_avail_idx to get
691  * @param last_used_idx
692  *  vhost last_used_idx to get
693  * @return
694  *  0 on success, -1 on failure
695  */
696 __rte_experimental
697 int
698 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
699                 uint16_t *last_avail_idx, uint16_t *last_used_idx);
700
701 /**
702  * Set last_avail/used_idx of the vhost virtqueue
703  *
704  * @param vid
705  *  vhost device ID
706  * @param queue_id
707  *  vhost queue index
708  * @param last_avail_idx
709  *  last_avail_idx to set
710  * @param last_used_idx
711  *  last_used_idx to set
712  * @return
713  *  0 on success, -1 on failure
714  */
715 __rte_experimental
716 int
717 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
718                 uint16_t last_avail_idx, uint16_t last_used_idx);
719
720 /**
721  * Register external message handling callbacks
722  *
723  * @param vid
724  *  vhost device ID
725  * @param ops
726  *  virtio external callbacks to register
727  * @param ctx
728  *  additional context passed to the callbacks
729  * @return
730  *  0 on success, -1 on failure
731  */
732 __rte_experimental
733 int
734 rte_vhost_extern_callback_register(int vid,
735                 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
736
737 /**
738  * Get vdpa device id for vhost device.
739  *
740  * @param vid
741  *  vhost device id
742  * @return
743  *  device id
744  */
745 __rte_experimental
746 int
747 rte_vhost_get_vdpa_device_id(int vid);
748
749 #ifdef __cplusplus
750 }
751 #endif
752
753 #endif /* _RTE_VHOST_H_ */