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