DPDK  16.04.0
rte_virtio_net.h
Go to the documentation of this file.
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of Intel Corporation nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef _VIRTIO_NET_H_
35 #define _VIRTIO_NET_H_
36 
42 #include <stdint.h>
43 #include <linux/vhost.h>
44 #include <linux/virtio_ring.h>
45 #include <linux/virtio_net.h>
46 #include <sys/eventfd.h>
47 #include <sys/socket.h>
48 #include <linux/if.h>
49 
50 #include <rte_memory.h>
51 #include <rte_mempool.h>
52 #include <rte_ether.h>
53 
54 struct rte_mbuf;
55 
56 #define VHOST_MEMORY_MAX_NREGIONS 8
57 
58 /* Used to indicate that the device is running on a data core */
59 #define VIRTIO_DEV_RUNNING 1
60 
61 /* Backend value set by guest. */
62 #define VIRTIO_DEV_STOPPED -1
63 
64 
65 /* Enum for virtqueue management. */
66 enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
67 
68 #define BUF_VECTOR_MAX 256
69 
74 struct buf_vector {
75  uint64_t buf_addr;
76  uint32_t buf_len;
77  uint32_t desc_idx;
78 };
79 
84  struct vring_desc *desc;
85  struct vring_avail *avail;
86  struct vring_used *used;
87  uint32_t size;
88  uint32_t backend;
89  uint16_t vhost_hlen;
90  volatile uint16_t last_used_idx;
91  volatile uint16_t last_used_idx_res;
92 #define VIRTIO_INVALID_EVENTFD (-1)
93 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
94  int callfd;
95  int kickfd;
96  int enabled;
97  uint64_t log_guest_addr;
98  uint64_t reserved[15];
99  struct buf_vector buf_vec[BUF_VECTOR_MAX];
101 
102 /* Old kernels have no such macro defined */
103 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
104  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
105 #endif
106 
107 
108 /*
109  * Make an extra wrapper for VIRTIO_NET_F_MQ and
110  * VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX as they are
111  * introduced since kernel v3.8. This makes our
112  * code buildable for older kernel.
113  */
114 #ifdef VIRTIO_NET_F_MQ
115  #define VHOST_MAX_QUEUE_PAIRS VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX
116  #define VHOST_SUPPORTS_MQ (1ULL << VIRTIO_NET_F_MQ)
117 #else
118  #define VHOST_MAX_QUEUE_PAIRS 1
119  #define VHOST_SUPPORTS_MQ 0
120 #endif
121 
122 /*
123  * Define virtio 1.0 for older kernels
124  */
125 #ifndef VIRTIO_F_VERSION_1
126  #define VIRTIO_F_VERSION_1 32
127 #endif
128 
132 struct virtio_net {
133  struct virtio_memory *mem;
134  uint64_t features;
135  uint64_t protocol_features;
136  uint64_t device_fh;
137  uint32_t flags;
138 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
139  char ifname[IF_NAME_SZ];
140  uint32_t virt_qp_nb;
141  void *priv;
142  uint64_t log_size;
143  uint64_t log_base;
144  struct ether_addr mac;
146  uint64_t reserved[61];
147  struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
149 
156  uint64_t memory_size;
157  uint64_t userspace_address;
158  uint64_t address_offset;
159 };
160 
161 
166  uint64_t base_address;
167  uint64_t mapped_address;
168  uint64_t mapped_size;
169  uint32_t nregions;
171 };
172 
181  int (*new_device)(struct virtio_net *);
182  void (*destroy_device)(volatile struct virtio_net *);
184  int (*vring_state_changed)(struct virtio_net *dev, uint16_t queue_id, int enable);
185 };
186 
187 static inline uint16_t __attribute__((always_inline))
188 rte_vring_available_entries(struct virtio_net *dev, uint16_t queue_id)
189 {
190  struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
191 
192  if (!vq->enabled)
193  return 0;
194 
195  return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx_res;
196 }
197 
202 static inline uint64_t __attribute__((always_inline))
203 gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa)
204 {
205  struct virtio_memory_regions *region;
206  uint32_t regionidx;
207  uint64_t vhost_va = 0;
208 
209  for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
210  region = &dev->mem->regions[regionidx];
211  if ((guest_pa >= region->guest_phys_address) &&
212  (guest_pa <= region->guest_phys_address_end)) {
213  vhost_va = region->address_offset + guest_pa;
214  break;
215  }
216  }
217  return vhost_va;
218 }
219 
220 
224 int rte_vhost_feature_disable(uint64_t feature_mask);
225 
229 int rte_vhost_feature_enable(uint64_t feature_mask);
230 
231 /* Returns currently supported vhost features */
232 uint64_t rte_vhost_feature_get(void);
233 
234 int rte_vhost_enable_guest_notification(struct virtio_net *dev, uint16_t queue_id, int enable);
235 
236 /* Register vhost driver. dev_name could be different for multiple instance support. */
237 int rte_vhost_driver_register(const char *dev_name);
238 
239 /* Unregister vhost driver. This is only meaningful to vhost user. */
240 int rte_vhost_driver_unregister(const char *dev_name);
241 
242 /* Register callbacks. */
243 int rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const);
244 /* Start vhost driver session blocking loop. */
245 int rte_vhost_driver_session_start(void);
246 
263 uint16_t rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
264  struct rte_mbuf **pkts, uint16_t count);
265 
283 uint16_t rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
284  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
285 
286 #endif /* _VIRTIO_NET_H_ */