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