4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
39 #include <rte_atomic.h>
41 #include <rte_memory.h>
42 #include <rte_memzone.h>
43 #include <rte_mempool.h>
45 #include "virtio_pci.h"
46 #include "virtio_ring.h"
47 #include "virtio_logs.h"
50 #define wmb() rte_wmb()
51 #define rmb() rte_rmb()
53 #define VIRTQUEUE_MAX_NAME_SZ 32
55 #define RTE_MBUF_DATA_DMA_ADDR(mb) \
56 (uint64_t) ((mb)->buf_physaddr + (uint64_t)((char *)((mb)->pkt.data) - \
57 (char *)(mb)->buf_addr))
59 #define VTNET_SQ_RQ_QUEUE_IDX 0
60 #define VTNET_SQ_TQ_QUEUE_IDX 1
61 #define VTNET_SQ_CQ_QUEUE_IDX 2
63 enum { VTNET_RQ = 0, VTNET_TQ = 1, VTNET_CQ = 2 };
65 * The maximum virtqueue size is 2^15. Use that value as the end of
66 * descriptor chain terminator since it will never be a valid index
67 * in the descriptor table. This is used to verify we are correctly
68 * handling vq_free_cnt.
70 #define VQ_RING_DESC_CHAIN_END 32768
73 * Control the RX mode, ie. promiscuous, allmulti, etc...
74 * All commands require an "out" sg entry containing a 1 byte
75 * state value, zero = disable, non-zero = enable. Commands
76 * 0 and 1 are supported with the VIRTIO_NET_F_CTRL_RX feature.
77 * Commands 2-5 are added with VIRTIO_NET_F_CTRL_RX_EXTRA.
79 #define VIRTIO_NET_CTRL_RX 0
80 #define VIRTIO_NET_CTRL_RX_PROMISC 0
81 #define VIRTIO_NET_CTRL_RX_ALLMULTI 1
82 #define VIRTIO_NET_CTRL_RX_ALLUNI 2
83 #define VIRTIO_NET_CTRL_RX_NOMULTI 3
84 #define VIRTIO_NET_CTRL_RX_NOUNI 4
85 #define VIRTIO_NET_CTRL_RX_NOBCAST 5
88 * Control VLAN filtering
90 * The VLAN filter table is controlled via a simple ADD/DEL interface.
91 * VLAN IDs not added may be filtered by the hypervisor. Del is the
92 * opposite of add. Both commands expect an out entry containing a 2
93 * byte VLAN ID. VLAN filtering is available with the
94 * VIRTIO_NET_F_CTRL_VLAN feature bit.
96 #define VIRTIO_NET_CTRL_VLAN 2
97 #define VIRTIO_NET_CTRL_VLAN_ADD 0
98 #define VIRTIO_NET_CTRL_VLAN_DEL 1
101 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
102 struct virtio_hw *hw; /**< virtio_hw structure pointer. */
103 const struct rte_memzone *mz; /**< mem zone to populate RX ring. */
104 const struct rte_memzone *virtio_net_hdr_mz; /**< memzone to populate hdr. */
105 struct rte_mempool *mpool; /**< mempool for mbuf allocation */
106 uint16_t queue_id; /**< DPDK queue index. */
107 uint8_t port_id; /**< Device port identifier. */
109 void *vq_ring_virt_mem; /**< linear address of vring*/
112 phys_addr_t vq_ring_mem; /**< physical address of vring */
114 struct vring vq_ring; /**< vring keeping desc, used and avail */
115 uint16_t vq_free_cnt; /**< num of desc available */
116 uint16_t vq_nentries; /**< vring desc numbers */
117 uint16_t vq_queue_index; /**< PCI queue index */
119 * Head of the free chain in the descriptor table. If
120 * there are no free descriptors, this will be set to
121 * VQ_RING_DESC_CHAIN_END.
123 uint16_t vq_desc_head_idx;
125 * Last consumed descriptor in the used table,
126 * trails vq_ring.used->idx.
128 uint16_t vq_used_cons_idx;
129 void *virtio_net_hdr_mem; /**< hdr for each xmit packet */
131 struct vq_desc_extra {
138 * This is the first element of the scatter-gather list. If you don't
139 * specify GSO or CSUM features, you can simply ignore the header.
141 struct virtio_net_hdr {
142 #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 /**< Use csum_start,csum_offset*/
144 #define VIRTIO_NET_HDR_GSO_NONE 0 /**< Not a GSO frame */
145 #define VIRTIO_NET_HDR_GSO_TCPV4 1 /**< GSO frame, IPv4 TCP (TSO) */
146 #define VIRTIO_NET_HDR_GSO_UDP 3 /**< GSO frame, IPv4 UDP (UFO) */
147 #define VIRTIO_NET_HDR_GSO_TCPV6 4 /**< GSO frame, IPv6 TCP */
148 #define VIRTIO_NET_HDR_GSO_ECN 0x80 /**< TCP has ECN set */
150 uint16_t hdr_len; /**< Ethernet + IP + tcp/udp hdrs */
151 uint16_t gso_size; /**< Bytes to append to hdr_len per frame */
152 uint16_t csum_start; /**< Position to start checksumming from */
153 uint16_t csum_offset; /**< Offset after that to place checksum */
157 * This is the version of the header to use when the MRG_RXBUF
158 * feature has been negotiated.
160 struct virtio_net_hdr_mrg_rxbuf {
161 struct virtio_net_hdr hdr;
162 uint16_t num_buffers; /**< Number of merged rx buffers */
166 * Tell the backend not to interrupt us.
168 void virtqueue_disable_intr(struct virtqueue *vq);
170 * Dump virtqueue internal structures, for debug purpose only.
172 void virtqueue_dump(struct virtqueue *vq);
174 * Get all mbufs to be freed.
176 struct rte_mbuf * virtqueue_detatch_unused(struct virtqueue *vq);
179 virtqueue_full(const struct virtqueue *vq)
181 return (vq->vq_free_cnt == 0);
184 #define VIRTQUEUE_NUSED(vq) ((uint16_t)((vq)->vq_ring.used->idx - (vq)->vq_used_cons_idx))
187 vq_ring_update_avail(struct virtqueue *vq, uint16_t desc_idx)
191 * Place the head of the descriptor chain into the next slot and make
192 * it usable to the host. The chain is made available now rather than
193 * deferring to virtqueue_notify() in the hopes that if the host is
194 * currently running on another CPU, we can keep it processing the new
197 avail_idx = (uint16_t)(vq->vq_ring.avail->idx & (vq->vq_nentries - 1));
198 vq->vq_ring.avail->ring[avail_idx] = desc_idx;
200 vq->vq_ring.avail->idx++;
203 static inline int virtqueue_kick_prepare(struct virtqueue * vq)
205 return !(vq->vq_ring.used->flags & VRING_USED_F_NO_NOTIFY);
209 virtqueue_notify(struct virtqueue *vq)
212 * Ensure updated avail->idx is visible to host. mb() necessary?
213 * For virtio on IA, the notificaiton is through io port operation
214 * which is a serialization instruction itself.
216 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_NOTIFY, vq->vq_queue_index);
220 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
222 struct vring_desc *dp;
223 struct vq_desc_extra *dxp;
225 dp = &vq->vq_ring.desc[desc_idx];
226 dxp = &vq->vq_descx[desc_idx];
227 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
228 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
229 while (dp->flags & VRING_DESC_F_NEXT) {
230 dp = &vq->vq_ring.desc[dp->next];
236 * We must append the existing free chain, if any, to the end of
237 * newly freed chain. If the virtqueue was completely used, then
238 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
240 dp->next = vq->vq_desc_head_idx;
241 vq->vq_desc_head_idx = desc_idx;
245 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
247 struct vq_desc_extra *dxp;
248 struct vring_desc *start_dp;
250 uint16_t head_idx, idx;
253 if (unlikely(vq->vq_free_cnt == 0))
255 if (unlikely(vq->vq_free_cnt < needed))
258 head_idx = vq->vq_desc_head_idx;
259 if (unlikely(head_idx >= vq->vq_nentries))
263 dxp = &vq->vq_descx[idx];
264 dxp->cookie = (void *)cookie;
265 dxp->ndescs = needed;
267 start_dp = vq->vq_ring.desc;
269 (uint64_t) (cookie->buf_physaddr + RTE_PKTMBUF_HEADROOM - sizeof(struct virtio_net_hdr));
270 start_dp[idx].len = cookie->buf_len - RTE_PKTMBUF_HEADROOM + sizeof(struct virtio_net_hdr);
271 start_dp[idx].flags = VRING_DESC_F_WRITE;
272 idx = start_dp[idx].next;
273 vq->vq_desc_head_idx = idx;
274 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
275 vq_ring_update_avail(vq, head_idx);
281 virtqueue_enqueue_xmit(struct virtqueue *txvq, struct rte_mbuf *cookie)
283 struct vq_desc_extra *dxp;
284 struct vring_desc *start_dp;
286 uint16_t head_idx, idx;
288 if (unlikely(txvq->vq_free_cnt == 0))
290 if (unlikely(txvq->vq_free_cnt < needed))
292 head_idx = txvq->vq_desc_head_idx;
293 if (unlikely(head_idx >= txvq->vq_nentries))
297 dxp = &txvq->vq_descx[idx];
298 dxp->cookie = (void *)cookie;
299 dxp->ndescs = needed;
301 start_dp = txvq->vq_ring.desc;
302 start_dp[idx].addr = (uint64_t)(uintptr_t)txvq->virtio_net_hdr_mem + idx * sizeof(struct virtio_net_hdr);
303 start_dp[idx].len = sizeof(struct virtio_net_hdr);
304 start_dp[idx].flags = VRING_DESC_F_NEXT;
305 idx = start_dp[idx].next;
306 start_dp[idx].addr = RTE_MBUF_DATA_DMA_ADDR(cookie);
307 start_dp[idx].len = cookie->pkt.data_len;
308 start_dp[idx].flags = 0;
309 idx = start_dp[idx].next;
310 txvq->vq_desc_head_idx = idx;
311 txvq->vq_free_cnt = (uint16_t)(txvq->vq_free_cnt - needed);
312 vq_ring_update_avail(txvq, head_idx);
317 static inline uint16_t
318 virtqueue_dequeue_burst(struct virtqueue *vq, struct rte_mbuf **rx_pkts, uint32_t *len, uint16_t num)
320 struct vring_used_elem *uep;
321 struct rte_mbuf *cookie;
322 uint16_t used_idx, desc_idx;
324 /* Caller does the check */
325 for (i = 0; i < num ; i ++) {
326 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
327 uep = &vq->vq_ring.used->ring[used_idx];
328 desc_idx = (uint16_t) uep->id;
329 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
330 if (unlikely(cookie == NULL)) {
331 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u\n",
332 vq->vq_used_cons_idx);
337 vq->vq_used_cons_idx++;
338 vq_ring_free_chain(vq, desc_idx);
339 vq->vq_descx[desc_idx].cookie = NULL;
344 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
345 #define VIRTQUEUE_DUMP(vq) do { \
346 uint16_t used_idx, nused; \
347 used_idx = (vq)->vq_ring.used->idx; \
348 nused = (uint16_t)(used_idx - (vq)->vq_used_cons_idx); \
349 PMD_INIT_LOG(DEBUG, \
350 "VQ: %s - size=%d; free=%d; used=%d; desc_head_idx=%d;" \
351 " avail.idx=%d; used_cons_idx=%d; used.idx=%d;" \
352 " avail.flags=0x%x; used.flags=0x%x\n", \
353 (vq)->vq_name, (vq)->vq_nentries, (vq)->vq_free_cnt, nused, \
354 (vq)->vq_desc_head_idx, (vq)->vq_ring.avail->idx, \
355 (vq)->vq_used_cons_idx, (vq)->vq_ring.used->idx, \
356 (vq)->vq_ring.avail->flags, (vq)->vq_ring.used->flags); \
359 #define VIRTQUEUE_DUMP(vq) do { } while (0)
362 #endif /* _VIRTQUEUE_H_ */