vhost: fix missing include
[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 int __rte_experimental
317 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
318
319 /**
320  * Unset the vdpa device id
321  *
322  * @param path
323  *  The vhost-user socket file path
324  * @return
325  *  0 on success, -1 on failure
326  */
327 int __rte_experimental
328 rte_vhost_driver_detach_vdpa_device(const char *path);
329
330 /**
331  * Get the device id
332  *
333  * @param path
334  *  The vhost-user socket file path
335  * @return
336  *  Device id, -1 on failure
337  */
338 int __rte_experimental
339 rte_vhost_driver_get_vdpa_device_id(const char *path);
340
341 /**
342  * Set the feature bits the vhost-user driver supports.
343  *
344  * @param path
345  *  The vhost-user socket file path
346  * @param features
347  *  Supported features
348  * @return
349  *  0 on success, -1 on failure
350  */
351 int rte_vhost_driver_set_features(const char *path, uint64_t features);
352
353 /**
354  * Enable vhost-user driver features.
355  *
356  * Note that
357  * - the param features should be a subset of the feature bits provided
358  *   by rte_vhost_driver_set_features().
359  * - it must be invoked before vhost-user negotiation starts.
360  *
361  * @param path
362  *  The vhost-user socket file path
363  * @param features
364  *  Features to enable
365  * @return
366  *  0 on success, -1 on failure
367  */
368 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
369
370 /**
371  * Disable vhost-user driver features.
372  *
373  * The two notes at rte_vhost_driver_enable_features() also apply here.
374  *
375  * @param path
376  *  The vhost-user socket file path
377  * @param features
378  *  Features to disable
379  * @return
380  *  0 on success, -1 on failure
381  */
382 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
383
384 /**
385  * Get the feature bits before feature negotiation.
386  *
387  * @param path
388  *  The vhost-user socket file path
389  * @param features
390  *  A pointer to store the queried feature bits
391  * @return
392  *  0 on success, -1 on failure
393  */
394 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
395
396 /**
397  * Set the protocol feature bits before feature negotiation.
398  *
399  * @param path
400  *  The vhost-user socket file path
401  * @param protocol_features
402  *  Supported protocol features
403  * @return
404  *  0 on success, -1 on failure
405  */
406 int __rte_experimental
407 rte_vhost_driver_set_protocol_features(const char *path,
408                 uint64_t protocol_features);
409
410 /**
411  * Get the protocol feature bits before feature negotiation.
412  *
413  * @param path
414  *  The vhost-user socket file path
415  * @param protocol_features
416  *  A pointer to store the queried protocol feature bits
417  * @return
418  *  0 on success, -1 on failure
419  */
420 int __rte_experimental
421 rte_vhost_driver_get_protocol_features(const char *path,
422                 uint64_t *protocol_features);
423
424 /**
425  * Get the queue number bits before feature negotiation.
426  *
427  * @param path
428  *  The vhost-user socket file path
429  * @param queue_num
430  *  A pointer to store the queried queue number bits
431  * @return
432  *  0 on success, -1 on failure
433  */
434 int __rte_experimental
435 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
436
437 /**
438  * Get the feature bits after negotiation
439  *
440  * @param vid
441  *  Vhost device ID
442  * @param features
443  *  A pointer to store the queried feature bits
444  * @return
445  *  0 on success, -1 on failure
446  */
447 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
448
449 /* Register callbacks. */
450 int rte_vhost_driver_callback_register(const char *path,
451         struct vhost_device_ops const * const ops);
452
453 /**
454  *
455  * Start the vhost-user driver.
456  *
457  * This function triggers the vhost-user negotiation.
458  *
459  * @param path
460  *  The vhost-user socket file path
461  * @return
462  *  0 on success, -1 on failure
463  */
464 int rte_vhost_driver_start(const char *path);
465
466 /**
467  * Get the MTU value of the device if set in QEMU.
468  *
469  * @param vid
470  *  virtio-net device ID
471  * @param mtu
472  *  The variable to store the MTU value
473  *
474  * @return
475  *  0: success
476  *  -EAGAIN: device not yet started
477  *  -ENOTSUP: device does not support MTU feature
478  */
479 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
480
481 /**
482  * Get the numa node from which the virtio net device's memory
483  * is allocated.
484  *
485  * @param vid
486  *  vhost device ID
487  *
488  * @return
489  *  The numa node, -1 on failure
490  */
491 int rte_vhost_get_numa_node(int vid);
492
493 /**
494  * @deprecated
495  * Get the number of queues the device supports.
496  *
497  * Note this function is deprecated, as it returns a queue pair number,
498  * which is vhost specific. Instead, rte_vhost_get_vring_num should
499  * be used.
500  *
501  * @param vid
502  *  vhost device ID
503  *
504  * @return
505  *  The number of queues, 0 on failure
506  */
507 __rte_deprecated
508 uint32_t rte_vhost_get_queue_num(int vid);
509
510 /**
511  * Get the number of vrings the device supports.
512  *
513  * @param vid
514  *  vhost device ID
515  *
516  * @return
517  *  The number of vrings, 0 on failure
518  */
519 uint16_t rte_vhost_get_vring_num(int vid);
520
521 /**
522  * Get the virtio net device's ifname, which is the vhost-user socket
523  * file path.
524  *
525  * @param vid
526  *  vhost device ID
527  * @param buf
528  *  The buffer to stored the queried ifname
529  * @param len
530  *  The length of buf
531  *
532  * @return
533  *  0 on success, -1 on failure
534  */
535 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
536
537 /**
538  * Get how many avail entries are left in the queue
539  *
540  * @param vid
541  *  vhost device ID
542  * @param queue_id
543  *  virtio queue index
544  *
545  * @return
546  *  num of avail entries left
547  */
548 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
549
550 struct rte_mbuf;
551 struct rte_mempool;
552 /**
553  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
554  * be received from the physical port or from another virtual device. A packet
555  * count is returned to indicate the number of packets that were successfully
556  * added to the RX queue.
557  * @param vid
558  *  vhost device ID
559  * @param queue_id
560  *  virtio queue index in mq case
561  * @param pkts
562  *  array to contain packets to be enqueued
563  * @param count
564  *  packets num to be enqueued
565  * @return
566  *  num of packets enqueued
567  */
568 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
569         struct rte_mbuf **pkts, uint16_t count);
570
571 /**
572  * This function gets guest buffers from the virtio device TX virtqueue,
573  * construct host mbufs, copies guest buffer content to host mbufs and
574  * store them in pkts to be processed.
575  * @param vid
576  *  vhost device ID
577  * @param queue_id
578  *  virtio queue index in mq case
579  * @param mbuf_pool
580  *  mbuf_pool where host mbuf is allocated.
581  * @param pkts
582  *  array to contain packets to be dequeued
583  * @param count
584  *  packets num to be dequeued
585  * @return
586  *  num of packets dequeued
587  */
588 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
589         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
590
591 /**
592  * Get guest mem table: a list of memory regions.
593  *
594  * An rte_vhost_vhost_memory object will be allocated internally, to hold the
595  * guest memory regions. Application should free it at destroy_device()
596  * callback.
597  *
598  * @param vid
599  *  vhost device ID
600  * @param mem
601  *  To store the returned mem regions
602  * @return
603  *  0 on success, -1 on failure
604  */
605 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
606
607 /**
608  * Get guest vring info, including the vring address, vring size, etc.
609  *
610  * @param vid
611  *  vhost device ID
612  * @param vring_idx
613  *  vring index
614  * @param vring
615  *  the structure to hold the requested vring info
616  * @return
617  *  0 on success, -1 on failure
618  */
619 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
620                               struct rte_vhost_vring *vring);
621
622 /**
623  * Notify the guest that used descriptors have been added to the vring.  This
624  * function acts as a memory barrier.
625  *
626  * @param vid
627  *  vhost device ID
628  * @param vring_idx
629  *  vring index
630  * @return
631  *  0 on success, -1 on failure
632  */
633 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
634
635 /**
636  * Get vhost RX queue avail count.
637  *
638  * @param vid
639  *  vhost device ID
640  * @param qid
641  *  virtio queue index in mq case
642  * @return
643  *  num of desc available
644  */
645 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
646
647 /**
648  * Get log base and log size of the vhost device
649  *
650  * @param vid
651  *  vhost device ID
652  * @param log_base
653  *  vhost log base
654  * @param log_size
655  *  vhost log size
656  * @return
657  *  0 on success, -1 on failure
658  */
659 int __rte_experimental
660 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
661
662 /**
663  * Get last_avail/used_idx of the vhost virtqueue
664  *
665  * @param vid
666  *  vhost device ID
667  * @param queue_id
668  *  vhost queue index
669  * @param last_avail_idx
670  *  vhost last_avail_idx to get
671  * @param last_used_idx
672  *  vhost last_used_idx to get
673  * @return
674  *  0 on success, -1 on failure
675  */
676 int __rte_experimental
677 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
678                 uint16_t *last_avail_idx, uint16_t *last_used_idx);
679
680 /**
681  * Set last_avail/used_idx of the vhost virtqueue
682  *
683  * @param vid
684  *  vhost device ID
685  * @param queue_id
686  *  vhost queue index
687  * @param last_avail_idx
688  *  last_avail_idx to set
689  * @param last_used_idx
690  *  last_used_idx to set
691  * @return
692  *  0 on success, -1 on failure
693  */
694 int __rte_experimental
695 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
696                 uint16_t last_avail_idx, uint16_t last_used_idx);
697
698 /**
699  * Register external message handling callbacks
700  *
701  * @param vid
702  *  vhost device ID
703  * @param ops
704  *  virtio external callbacks to register
705  * @param ctx
706  *  additional context passed to the callbacks
707  * @return
708  *  0 on success, -1 on failure
709  */
710 int __rte_experimental
711 rte_vhost_extern_callback_register(int vid,
712                 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
713
714 /**
715  * Get vdpa device id for vhost device.
716  *
717  * @param vid
718  *  vhost device id
719  * @return
720  *  device id
721  */
722 int __rte_experimental
723 rte_vhost_get_vdpa_device_id(int vid);
724
725 #ifdef __cplusplus
726 }
727 #endif
728
729 #endif /* _RTE_VHOST_H_ */