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