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