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