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