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