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