vhost: convert buffer addresses to GPA for logging
[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 __rte_experimental
237 static __rte_always_inline uint64_t
238 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
239                                                    uint64_t gpa, uint64_t *len)
240 {
241         struct rte_vhost_mem_region *r;
242         uint32_t i;
243
244         for (i = 0; i < mem->nregions; i++) {
245                 r = &mem->regions[i];
246                 if (gpa >= r->guest_phys_addr &&
247                     gpa <  r->guest_phys_addr + r->size) {
248
249                         if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
250                                 *len = r->guest_phys_addr + r->size - gpa;
251
252                         return gpa - r->guest_phys_addr +
253                                r->host_user_addr;
254                 }
255         }
256         *len = 0;
257
258         return 0;
259 }
260
261 #define RTE_VHOST_NEED_LOG(features)    ((features) & (1ULL << VHOST_F_LOG_ALL))
262
263 /**
264  * Log the memory write start with given address.
265  *
266  * This function only need be invoked when the live migration starts.
267  * Therefore, we won't need call it at all in the most of time. For
268  * making the performance impact be minimum, it's suggested to do a
269  * check before calling it:
270  *
271  *        if (unlikely(RTE_VHOST_NEED_LOG(features)))
272  *                rte_vhost_log_write(vid, addr, len);
273  *
274  * @param vid
275  *  vhost device ID
276  * @param addr
277  *  the starting address for write (in guest physical address space)
278  * @param len
279  *  the length to write
280  */
281 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
282
283 /**
284  * Log the used ring update start at given offset.
285  *
286  * Same as rte_vhost_log_write, it's suggested to do a check before
287  * calling it:
288  *
289  *        if (unlikely(RTE_VHOST_NEED_LOG(features)))
290  *                rte_vhost_log_used_vring(vid, vring_idx, offset, len);
291  *
292  * @param vid
293  *  vhost device ID
294  * @param vring_idx
295  *  the vring index
296  * @param offset
297  *  the offset inside the used ring
298  * @param len
299  *  the length to write
300  */
301 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
302                               uint64_t offset, uint64_t len);
303
304 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
305
306 /**
307  * Register vhost driver. path could be different for multiple
308  * instance support.
309  */
310 int rte_vhost_driver_register(const char *path, uint64_t flags);
311
312 /* Unregister vhost driver. This is only meaningful to vhost user. */
313 int rte_vhost_driver_unregister(const char *path);
314
315 /**
316  * Set the vdpa device id, enforce single connection per socket
317  *
318  * @param path
319  *  The vhost-user socket file path
320  * @param did
321  *  Device id
322  * @return
323  *  0 on success, -1 on failure
324  */
325 __rte_experimental
326 int
327 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
328
329 /**
330  * Unset the vdpa device id
331  *
332  * @param path
333  *  The vhost-user socket file path
334  * @return
335  *  0 on success, -1 on failure
336  */
337 __rte_experimental
338 int
339 rte_vhost_driver_detach_vdpa_device(const char *path);
340
341 /**
342  * Get the device id
343  *
344  * @param path
345  *  The vhost-user socket file path
346  * @return
347  *  Device id, -1 on failure
348  */
349 __rte_experimental
350 int
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  * Set the protocol feature bits before feature negotiation.
410  *
411  * @param path
412  *  The vhost-user socket file path
413  * @param protocol_features
414  *  Supported protocol features
415  * @return
416  *  0 on success, -1 on failure
417  */
418 __rte_experimental
419 int
420 rte_vhost_driver_set_protocol_features(const char *path,
421                 uint64_t protocol_features);
422
423 /**
424  * Get the protocol feature bits before feature negotiation.
425  *
426  * @param path
427  *  The vhost-user socket file path
428  * @param protocol_features
429  *  A pointer to store the queried protocol feature bits
430  * @return
431  *  0 on success, -1 on failure
432  */
433 __rte_experimental
434 int
435 rte_vhost_driver_get_protocol_features(const char *path,
436                 uint64_t *protocol_features);
437
438 /**
439  * Get the queue number bits before feature negotiation.
440  *
441  * @param path
442  *  The vhost-user socket file path
443  * @param queue_num
444  *  A pointer to store the queried queue number bits
445  * @return
446  *  0 on success, -1 on failure
447  */
448 __rte_experimental
449 int
450 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
451
452 /**
453  * Get the feature bits after negotiation
454  *
455  * @param vid
456  *  Vhost device ID
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_get_negotiated_features(int vid, uint64_t *features);
463
464 /* Register callbacks. */
465 int rte_vhost_driver_callback_register(const char *path,
466         struct vhost_device_ops const * const ops);
467
468 /**
469  *
470  * Start the vhost-user driver.
471  *
472  * This function triggers the vhost-user negotiation.
473  *
474  * @param path
475  *  The vhost-user socket file path
476  * @return
477  *  0 on success, -1 on failure
478  */
479 int rte_vhost_driver_start(const char *path);
480
481 /**
482  * Get the MTU value of the device if set in QEMU.
483  *
484  * @param vid
485  *  virtio-net device ID
486  * @param mtu
487  *  The variable to store the MTU value
488  *
489  * @return
490  *  0: success
491  *  -EAGAIN: device not yet started
492  *  -ENOTSUP: device does not support MTU feature
493  */
494 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
495
496 /**
497  * Get the numa node from which the virtio net device's memory
498  * is allocated.
499  *
500  * @param vid
501  *  vhost device ID
502  *
503  * @return
504  *  The numa node, -1 on failure
505  */
506 int rte_vhost_get_numa_node(int vid);
507
508 /**
509  * @deprecated
510  * Get the number of queues the device supports.
511  *
512  * Note this function is deprecated, as it returns a queue pair number,
513  * which is vhost specific. Instead, rte_vhost_get_vring_num should
514  * be used.
515  *
516  * @param vid
517  *  vhost device ID
518  *
519  * @return
520  *  The number of queues, 0 on failure
521  */
522 __rte_deprecated
523 uint32_t rte_vhost_get_queue_num(int vid);
524
525 /**
526  * Get the number of vrings the device supports.
527  *
528  * @param vid
529  *  vhost device ID
530  *
531  * @return
532  *  The number of vrings, 0 on failure
533  */
534 uint16_t rte_vhost_get_vring_num(int vid);
535
536 /**
537  * Get the virtio net device's ifname, which is the vhost-user socket
538  * file path.
539  *
540  * @param vid
541  *  vhost device ID
542  * @param buf
543  *  The buffer to stored the queried ifname
544  * @param len
545  *  The length of buf
546  *
547  * @return
548  *  0 on success, -1 on failure
549  */
550 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
551
552 /**
553  * Get how many avail entries are left in the queue
554  *
555  * @param vid
556  *  vhost device ID
557  * @param queue_id
558  *  virtio queue index
559  *
560  * @return
561  *  num of avail entries left
562  */
563 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
564
565 struct rte_mbuf;
566 struct rte_mempool;
567 /**
568  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
569  * be received from the physical port or from another virtual device. A packet
570  * count is returned to indicate the number of packets that were successfully
571  * added to the RX queue.
572  * @param vid
573  *  vhost device ID
574  * @param queue_id
575  *  virtio queue index in mq case
576  * @param pkts
577  *  array to contain packets to be enqueued
578  * @param count
579  *  packets num to be enqueued
580  * @return
581  *  num of packets enqueued
582  */
583 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
584         struct rte_mbuf **pkts, uint16_t count);
585
586 /**
587  * This function gets guest buffers from the virtio device TX virtqueue,
588  * construct host mbufs, copies guest buffer content to host mbufs and
589  * store them in pkts to be processed.
590  * @param vid
591  *  vhost device ID
592  * @param queue_id
593  *  virtio queue index in mq case
594  * @param mbuf_pool
595  *  mbuf_pool where host mbuf is allocated.
596  * @param pkts
597  *  array to contain packets to be dequeued
598  * @param count
599  *  packets num to be dequeued
600  * @return
601  *  num of packets dequeued
602  */
603 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
604         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
605
606 /**
607  * Get guest mem table: a list of memory regions.
608  *
609  * An rte_vhost_vhost_memory object will be allocated internally, to hold the
610  * guest memory regions. Application should free it at destroy_device()
611  * callback.
612  *
613  * @param vid
614  *  vhost device ID
615  * @param mem
616  *  To store the returned mem regions
617  * @return
618  *  0 on success, -1 on failure
619  */
620 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
621
622 /**
623  * Get guest vring info, including the vring address, vring size, etc.
624  *
625  * @param vid
626  *  vhost device ID
627  * @param vring_idx
628  *  vring index
629  * @param vring
630  *  the structure to hold the requested vring info
631  * @return
632  *  0 on success, -1 on failure
633  */
634 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
635                               struct rte_vhost_vring *vring);
636
637 /**
638  * Notify the guest that used descriptors have been added to the vring.  This
639  * function acts as a memory barrier.
640  *
641  * @param vid
642  *  vhost device ID
643  * @param vring_idx
644  *  vring index
645  * @return
646  *  0 on success, -1 on failure
647  */
648 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
649
650 /**
651  * Get vhost RX queue avail count.
652  *
653  * @param vid
654  *  vhost device ID
655  * @param qid
656  *  virtio queue index in mq case
657  * @return
658  *  num of desc available
659  */
660 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
661
662 /**
663  * Get log base and log size of the vhost device
664  *
665  * @param vid
666  *  vhost device ID
667  * @param log_base
668  *  vhost log base
669  * @param log_size
670  *  vhost log size
671  * @return
672  *  0 on success, -1 on failure
673  */
674 __rte_experimental
675 int
676 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
677
678 /**
679  * Get 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  *  vhost last_avail_idx to get
687  * @param last_used_idx
688  *  vhost last_used_idx to get
689  * @return
690  *  0 on success, -1 on failure
691  */
692 __rte_experimental
693 int
694 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
695                 uint16_t *last_avail_idx, uint16_t *last_used_idx);
696
697 /**
698  * Set last_avail/used_idx of the vhost virtqueue
699  *
700  * @param vid
701  *  vhost device ID
702  * @param queue_id
703  *  vhost queue index
704  * @param last_avail_idx
705  *  last_avail_idx to set
706  * @param last_used_idx
707  *  last_used_idx to set
708  * @return
709  *  0 on success, -1 on failure
710  */
711 __rte_experimental
712 int
713 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
714                 uint16_t last_avail_idx, uint16_t last_used_idx);
715
716 /**
717  * Register external message handling callbacks
718  *
719  * @param vid
720  *  vhost device ID
721  * @param ops
722  *  virtio external callbacks to register
723  * @param ctx
724  *  additional context passed to the callbacks
725  * @return
726  *  0 on success, -1 on failure
727  */
728 __rte_experimental
729 int
730 rte_vhost_extern_callback_register(int vid,
731                 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
732
733 /**
734  * Get vdpa device id for vhost device.
735  *
736  * @param vid
737  *  vhost device id
738  * @return
739  *  device id
740  */
741 __rte_experimental
742 int
743 rte_vhost_get_vdpa_device_id(int vid);
744
745 #ifdef __cplusplus
746 }
747 #endif
748
749 #endif /* _RTE_VHOST_H_ */