New upstream version 18.02
[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 /**
33  * Information relating to memory regions including offsets to
34  * addresses in QEMUs memory file.
35  */
36 struct rte_vhost_mem_region {
37         uint64_t guest_phys_addr;
38         uint64_t guest_user_addr;
39         uint64_t host_user_addr;
40         uint64_t size;
41         void     *mmap_addr;
42         uint64_t mmap_size;
43         int fd;
44 };
45
46 /**
47  * Memory structure includes region and mapping information.
48  */
49 struct rte_vhost_memory {
50         uint32_t nregions;
51         struct rte_vhost_mem_region regions[];
52 };
53
54 struct rte_vhost_vring {
55         struct vring_desc       *desc;
56         struct vring_avail      *avail;
57         struct vring_used       *used;
58         uint64_t                log_guest_addr;
59
60         /** Deprecated, use rte_vhost_vring_call() instead. */
61         int                     callfd;
62
63         int                     kickfd;
64         uint16_t                size;
65 };
66
67 /**
68  * Device and vring operations.
69  */
70 struct vhost_device_ops {
71         int (*new_device)(int vid);             /**< Add device. */
72         void (*destroy_device)(int vid);        /**< Remove device. */
73
74         int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);     /**< triggered when a vring is enabled or disabled */
75
76         /**
77          * Features could be changed after the feature negotiation.
78          * For example, VHOST_F_LOG_ALL will be set/cleared at the
79          * start/end of live migration, respectively. This callback
80          * is used to inform the application on such change.
81          */
82         int (*features_changed)(int vid, uint64_t features);
83
84         int (*new_connection)(int vid);
85         void (*destroy_connection)(int vid);
86
87         void *reserved[2]; /**< Reserved for future extension */
88 };
89
90 /**
91  * Convert guest physical address to host virtual address
92  *
93  * @param mem
94  *  the guest memory regions
95  * @param gpa
96  *  the guest physical address for querying
97  * @return
98  *  the host virtual address on success, 0 on failure
99  */
100 static __rte_always_inline uint64_t
101 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
102 {
103         struct rte_vhost_mem_region *reg;
104         uint32_t i;
105
106         for (i = 0; i < mem->nregions; i++) {
107                 reg = &mem->regions[i];
108                 if (gpa >= reg->guest_phys_addr &&
109                     gpa <  reg->guest_phys_addr + reg->size) {
110                         return gpa - reg->guest_phys_addr +
111                                reg->host_user_addr;
112                 }
113         }
114
115         return 0;
116 }
117
118 #define RTE_VHOST_NEED_LOG(features)    ((features) & (1ULL << VHOST_F_LOG_ALL))
119
120 /**
121  * Log the memory write start with given address.
122  *
123  * This function only need be invoked when the live migration starts.
124  * Therefore, we won't need call it at all in the most of time. For
125  * making the performance impact be minimum, it's suggested to do a
126  * check before calling it:
127  *
128  *        if (unlikely(RTE_VHOST_NEED_LOG(features)))
129  *                rte_vhost_log_write(vid, addr, len);
130  *
131  * @param vid
132  *  vhost device ID
133  * @param addr
134  *  the starting address for write
135  * @param len
136  *  the length to write
137  */
138 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
139
140 /**
141  * Log the used ring update start at given offset.
142  *
143  * Same as rte_vhost_log_write, it's suggested to do a check before
144  * calling it:
145  *
146  *        if (unlikely(RTE_VHOST_NEED_LOG(features)))
147  *                rte_vhost_log_used_vring(vid, vring_idx, offset, len);
148  *
149  * @param vid
150  *  vhost device ID
151  * @param vring_idx
152  *  the vring index
153  * @param offset
154  *  the offset inside the used ring
155  * @param len
156  *  the length to write
157  */
158 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
159                               uint64_t offset, uint64_t len);
160
161 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
162
163 /**
164  * Register vhost driver. path could be different for multiple
165  * instance support.
166  */
167 int rte_vhost_driver_register(const char *path, uint64_t flags);
168
169 /* Unregister vhost driver. This is only meaningful to vhost user. */
170 int rte_vhost_driver_unregister(const char *path);
171
172 /**
173  * Set the feature bits the vhost-user driver supports.
174  *
175  * @param path
176  *  The vhost-user socket file path
177  * @param features
178  *  Supported features
179  * @return
180  *  0 on success, -1 on failure
181  */
182 int rte_vhost_driver_set_features(const char *path, uint64_t features);
183
184 /**
185  * Enable vhost-user driver features.
186  *
187  * Note that
188  * - the param features should be a subset of the feature bits provided
189  *   by rte_vhost_driver_set_features().
190  * - it must be invoked before vhost-user negotiation starts.
191  *
192  * @param path
193  *  The vhost-user socket file path
194  * @param features
195  *  Features to enable
196  * @return
197  *  0 on success, -1 on failure
198  */
199 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
200
201 /**
202  * Disable vhost-user driver features.
203  *
204  * The two notes at rte_vhost_driver_enable_features() also apply here.
205  *
206  * @param path
207  *  The vhost-user socket file path
208  * @param features
209  *  Features to disable
210  * @return
211  *  0 on success, -1 on failure
212  */
213 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
214
215 /**
216  * Get the feature bits before feature negotiation.
217  *
218  * @param path
219  *  The vhost-user socket file path
220  * @param features
221  *  A pointer to store the queried feature bits
222  * @return
223  *  0 on success, -1 on failure
224  */
225 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
226
227 /**
228  * Get the feature bits after negotiation
229  *
230  * @param vid
231  *  Vhost device ID
232  * @param features
233  *  A pointer to store the queried feature bits
234  * @return
235  *  0 on success, -1 on failure
236  */
237 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
238
239 /* Register callbacks. */
240 int rte_vhost_driver_callback_register(const char *path,
241         struct vhost_device_ops const * const ops);
242
243 /**
244  *
245  * Start the vhost-user driver.
246  *
247  * This function triggers the vhost-user negotiation.
248  *
249  * @param path
250  *  The vhost-user socket file path
251  * @return
252  *  0 on success, -1 on failure
253  */
254 int rte_vhost_driver_start(const char *path);
255
256 /**
257  * Get the MTU value of the device if set in QEMU.
258  *
259  * @param vid
260  *  virtio-net device ID
261  * @param mtu
262  *  The variable to store the MTU value
263  *
264  * @return
265  *  0: success
266  *  -EAGAIN: device not yet started
267  *  -ENOTSUP: device does not support MTU feature
268  */
269 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
270
271 /**
272  * Get the numa node from which the virtio net device's memory
273  * is allocated.
274  *
275  * @param vid
276  *  vhost device ID
277  *
278  * @return
279  *  The numa node, -1 on failure
280  */
281 int rte_vhost_get_numa_node(int vid);
282
283 /**
284  * @deprecated
285  * Get the number of queues the device supports.
286  *
287  * Note this function is deprecated, as it returns a queue pair number,
288  * which is vhost specific. Instead, rte_vhost_get_vring_num should
289  * be used.
290  *
291  * @param vid
292  *  vhost device ID
293  *
294  * @return
295  *  The number of queues, 0 on failure
296  */
297 __rte_deprecated
298 uint32_t rte_vhost_get_queue_num(int vid);
299
300 /**
301  * Get the number of vrings the device supports.
302  *
303  * @param vid
304  *  vhost device ID
305  *
306  * @return
307  *  The number of vrings, 0 on failure
308  */
309 uint16_t rte_vhost_get_vring_num(int vid);
310
311 /**
312  * Get the virtio net device's ifname, which is the vhost-user socket
313  * file path.
314  *
315  * @param vid
316  *  vhost device ID
317  * @param buf
318  *  The buffer to stored the queried ifname
319  * @param len
320  *  The length of buf
321  *
322  * @return
323  *  0 on success, -1 on failure
324  */
325 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
326
327 /**
328  * Get how many avail entries are left in the queue
329  *
330  * @param vid
331  *  vhost device ID
332  * @param queue_id
333  *  virtio queue index
334  *
335  * @return
336  *  num of avail entires left
337  */
338 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
339
340 struct rte_mbuf;
341 struct rte_mempool;
342 /**
343  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
344  * be received from the physical port or from another virtual device. A packet
345  * count is returned to indicate the number of packets that were successfully
346  * added to the RX queue.
347  * @param vid
348  *  vhost device ID
349  * @param queue_id
350  *  virtio queue index in mq case
351  * @param pkts
352  *  array to contain packets to be enqueued
353  * @param count
354  *  packets num to be enqueued
355  * @return
356  *  num of packets enqueued
357  */
358 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
359         struct rte_mbuf **pkts, uint16_t count);
360
361 /**
362  * This function gets guest buffers from the virtio device TX virtqueue,
363  * construct host mbufs, copies guest buffer content to host mbufs and
364  * store them in pkts to be processed.
365  * @param vid
366  *  vhost device ID
367  * @param queue_id
368  *  virtio queue index in mq case
369  * @param mbuf_pool
370  *  mbuf_pool where host mbuf is allocated.
371  * @param pkts
372  *  array to contain packets to be dequeued
373  * @param count
374  *  packets num to be dequeued
375  * @return
376  *  num of packets dequeued
377  */
378 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
379         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
380
381 /**
382  * Get guest mem table: a list of memory regions.
383  *
384  * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
385  * guest memory regions. Application should free it at destroy_device()
386  * callback.
387  *
388  * @param vid
389  *  vhost device ID
390  * @param mem
391  *  To store the returned mem regions
392  * @return
393  *  0 on success, -1 on failure
394  */
395 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
396
397 /**
398  * Get guest vring info, including the vring address, vring size, etc.
399  *
400  * @param vid
401  *  vhost device ID
402  * @param vring_idx
403  *  vring index
404  * @param vring
405  *  the structure to hold the requested vring info
406  * @return
407  *  0 on success, -1 on failure
408  */
409 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
410                               struct rte_vhost_vring *vring);
411
412 /**
413  * Notify the guest that used descriptors have been added to the vring.  This
414  * function acts as a memory barrier.
415  *
416  * @param vid
417  *  vhost device ID
418  * @param vring_idx
419  *  vring index
420  * @return
421  *  0 on success, -1 on failure
422  */
423 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
424
425 /**
426  * Get vhost RX queue avail count.
427  *
428  * @param vid
429  *  vhost device ID
430  * @param qid
431  *  virtio queue index in mq case
432  * @return
433  *  num of desc available
434  */
435 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
436
437 #ifdef __cplusplus
438 }
439 #endif
440
441 #endif /* _RTE_VHOST_H_ */