DPDK  make-f/home/abuild/rpmbuild/BUILD/dpdk-2.2.0/mk/rte.sdkconfig.mkshowversion
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/virtio_ring.h>
44 #include <linux/virtio_net.h>
45 #include <sys/eventfd.h>
46 #include <sys/socket.h>
47 #include <linux/if.h>
48 
49 #include <rte_memory.h>
50 #include <rte_mempool.h>
51 
52 struct rte_mbuf;
53 
54 #define VHOST_MEMORY_MAX_NREGIONS 8
55 
56 /* Used to indicate that the device is running on a data core */
57 #define VIRTIO_DEV_RUNNING 1
58 
59 /* Backend value set by guest. */
60 #define VIRTIO_DEV_STOPPED -1
61 
62 
63 /* Enum for virtqueue management. */
64 enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
65 
66 #define BUF_VECTOR_MAX 256
67 
72 struct buf_vector {
73  uint64_t buf_addr;
74  uint32_t buf_len;
75  uint32_t desc_idx;
76 };
77 
82  struct vring_desc *desc;
83  struct vring_avail *avail;
84  struct vring_used *used;
85  uint32_t size;
86  uint32_t backend;
87  uint16_t vhost_hlen;
88  volatile uint16_t last_used_idx;
89  volatile uint16_t last_used_idx_res;
90  int callfd;
91  int kickfd;
92  int enabled;
93  uint64_t reserved[16];
94  struct buf_vector buf_vec[BUF_VECTOR_MAX];
96 
97 
98 /*
99  * Make an extra wrapper for VIRTIO_NET_F_MQ and
100  * VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX as they are
101  * introduced since kernel v3.8. This makes our
102  * code buildable for older kernel.
103  */
104 #ifdef VIRTIO_NET_F_MQ
105  #define VHOST_MAX_QUEUE_PAIRS VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX
106  #define VHOST_SUPPORTS_MQ (1ULL << VIRTIO_NET_F_MQ)
107 #else
108  #define VHOST_MAX_QUEUE_PAIRS 1
109  #define VHOST_SUPPORTS_MQ 0
110 #endif
111 
112 /*
113  * Define virtio 1.0 for older kernels
114  */
115 #ifndef VIRTIO_F_VERSION_1
116  #define VIRTIO_F_VERSION_1 32
117 #endif
118 
122 struct virtio_net {
123  struct virtio_memory *mem;
124  uint64_t features;
125  uint64_t protocol_features;
126  uint64_t device_fh;
127  uint32_t flags;
128 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
129  char ifname[IF_NAME_SZ];
130  uint32_t virt_qp_nb;
131  void *priv;
132  uint64_t log_size;
133  uint64_t log_base;
134  uint64_t reserved[62];
135  struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
137 
144  uint64_t memory_size;
145  uint64_t userspace_address;
146  uint64_t address_offset;
147 };
148 
149 
154  uint64_t base_address;
155  uint64_t mapped_address;
156  uint64_t mapped_size;
157  uint32_t nregions;
159 };
160 
169  int (*new_device)(struct virtio_net *);
170  void (*destroy_device)(volatile struct virtio_net *);
172  int (*vring_state_changed)(struct virtio_net *dev, uint16_t queue_id, int enable);
173 };
174 
175 static inline uint16_t __attribute__((always_inline))
176 rte_vring_available_entries(struct virtio_net *dev, uint16_t queue_id)
177 {
178  struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
179 
180  if (!vq->enabled)
181  return 0;
182 
183  return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx_res;
184 }
185 
190 static inline uint64_t __attribute__((always_inline))
191 gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa)
192 {
193  struct virtio_memory_regions *region;
194  uint32_t regionidx;
195  uint64_t vhost_va = 0;
196 
197  for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
198  region = &dev->mem->regions[regionidx];
199  if ((guest_pa >= region->guest_phys_address) &&
200  (guest_pa <= region->guest_phys_address_end)) {
201  vhost_va = region->address_offset + guest_pa;
202  break;
203  }
204  }
205  return vhost_va;
206 }
207 
211 int rte_vhost_feature_disable(uint64_t feature_mask);
212 
216 int rte_vhost_feature_enable(uint64_t feature_mask);
217 
218 /* Returns currently supported vhost features */
219 uint64_t rte_vhost_feature_get(void);
220 
221 int rte_vhost_enable_guest_notification(struct virtio_net *dev, uint16_t queue_id, int enable);
222 
223 /* Register vhost driver. dev_name could be different for multiple instance support. */
224 int rte_vhost_driver_register(const char *dev_name);
225 
226 /* Unregister vhost driver. This is only meaningful to vhost user. */
227 int rte_vhost_driver_unregister(const char *dev_name);
228 
229 /* Register callbacks. */
230 int rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const);
231 /* Start vhost driver session blocking loop. */
232 int rte_vhost_driver_session_start(void);
233 
250 uint16_t rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
251  struct rte_mbuf **pkts, uint16_t count);
252 
270 uint16_t rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
271  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
272 
273 #endif /* _VIRTIO_NET_H_ */
uint64_t protocol_features
int(* vring_state_changed)(struct virtio_net *dev, uint16_t queue_id, int enable)
uint64_t mapped_size
uint64_t log_size
volatile uint16_t last_used_idx
uint16_t rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id, struct rte_mbuf **pkts, uint16_t count)
uint16_t rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id, struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
struct vring_used * used
uint64_t base_address
struct vring_avail * avail
void(* destroy_device)(volatile struct virtio_net *)
uint32_t nregions
struct vhost_virtqueue * virtqueue[VHOST_MAX_QUEUE_PAIRS *2]
int rte_vhost_feature_disable(uint64_t feature_mask)
uint64_t features
volatile uint16_t last_used_idx_res
uint64_t mapped_address
uint16_t vhost_hlen
struct vring_desc * desc
uint32_t virt_qp_nb
uint64_t device_fh
struct virtio_memory_regions regions[0]
struct buf_vector buf_vec[BUF_VECTOR_MAX]
uint64_t log_base
uint32_t flags
char ifname[IF_NAME_SZ]
uint64_t reserved[62]
int(* new_device)(struct virtio_net *)
static uint64_t gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa)
int rte_vhost_feature_enable(uint64_t feature_mask)
struct virtio_memory * mem
uint64_t reserved[16]
#define __rte_cache_aligned
Definition: rte_memory.h:85