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