4 * Copyright(c) 2010-2014 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 #ifdef RTE_PMD_PACKET_PREFETCH
54 #define rte_packet_prefetch(p) rte_prefetch1(p)
56 #define rte_packet_prefetch(p) do {} while(0)
59 #define VIRTQUEUE_MAX_NAME_SZ 32
61 #define RTE_MBUF_DATA_DMA_ADDR(mb) \
62 (uint64_t) ((mb)->buf_physaddr + (uint64_t)((char *)((mb)->pkt.data) - \
63 (char *)(mb)->buf_addr))
65 #define VTNET_SQ_RQ_QUEUE_IDX 0
66 #define VTNET_SQ_TQ_QUEUE_IDX 1
67 #define VTNET_SQ_CQ_QUEUE_IDX 2
69 enum { VTNET_RQ = 0, VTNET_TQ = 1, VTNET_CQ = 2 };
71 * The maximum virtqueue size is 2^15. Use that value as the end of
72 * descriptor chain terminator since it will never be a valid index
73 * in the descriptor table. This is used to verify we are correctly
74 * handling vq_free_cnt.
76 #define VQ_RING_DESC_CHAIN_END 32768
79 * Control the RX mode, ie. promiscuous, allmulti, etc...
80 * All commands require an "out" sg entry containing a 1 byte
81 * state value, zero = disable, non-zero = enable. Commands
82 * 0 and 1 are supported with the VIRTIO_NET_F_CTRL_RX feature.
83 * Commands 2-5 are added with VIRTIO_NET_F_CTRL_RX_EXTRA.
85 #define VIRTIO_NET_CTRL_RX 0
86 #define VIRTIO_NET_CTRL_RX_PROMISC 0
87 #define VIRTIO_NET_CTRL_RX_ALLMULTI 1
88 #define VIRTIO_NET_CTRL_RX_ALLUNI 2
89 #define VIRTIO_NET_CTRL_RX_NOMULTI 3
90 #define VIRTIO_NET_CTRL_RX_NOUNI 4
91 #define VIRTIO_NET_CTRL_RX_NOBCAST 5
94 * Control VLAN filtering
96 * The VLAN filter table is controlled via a simple ADD/DEL interface.
97 * VLAN IDs not added may be filtered by the hypervisor. Del is the
98 * opposite of add. Both commands expect an out entry containing a 2
99 * byte VLAN ID. VLAN filtering is available with the
100 * VIRTIO_NET_F_CTRL_VLAN feature bit.
102 #define VIRTIO_NET_CTRL_VLAN 2
103 #define VIRTIO_NET_CTRL_VLAN_ADD 0
104 #define VIRTIO_NET_CTRL_VLAN_DEL 1
106 struct virtio_net_ctrl_hdr {
109 } __attribute__((packed));
111 typedef uint8_t virtio_net_ctrl_ack;
113 #define VIRTIO_NET_OK 0
114 #define VIRTIO_NET_ERR 1
116 #define VIRTIO_MAX_CTRL_DATA 128
118 struct virtio_pmd_ctrl {
119 struct virtio_net_ctrl_hdr hdr;
120 virtio_net_ctrl_ack status;
121 uint8_t data[VIRTIO_MAX_CTRL_DATA];
125 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
126 struct virtio_hw *hw; /**< virtio_hw structure pointer. */
127 const struct rte_memzone *mz; /**< mem zone to populate RX ring. */
128 const struct rte_memzone *virtio_net_hdr_mz; /**< memzone to populate hdr. */
129 struct rte_mempool *mpool; /**< mempool for mbuf allocation */
130 uint16_t queue_id; /**< DPDK queue index. */
131 uint8_t port_id; /**< Device port identifier. */
133 void *vq_ring_virt_mem; /**< linear address of vring*/
136 phys_addr_t vq_ring_mem; /**< physical address of vring */
138 struct vring vq_ring; /**< vring keeping desc, used and avail */
139 uint16_t vq_free_cnt; /**< num of desc available */
140 uint16_t vq_nentries; /**< vring desc numbers */
141 uint16_t vq_queue_index; /**< PCI queue index */
143 * Head of the free chain in the descriptor table. If
144 * there are no free descriptors, this will be set to
145 * VQ_RING_DESC_CHAIN_END.
147 uint16_t vq_desc_head_idx;
148 uint16_t vq_desc_tail_idx;
150 * Last consumed descriptor in the used table,
151 * trails vq_ring.used->idx.
153 uint16_t vq_used_cons_idx;
154 uint16_t vq_avail_idx;
155 phys_addr_t virtio_net_hdr_mem; /**< hdr for each xmit packet */
162 struct vq_desc_extra {
168 /* If multiqueue is provided by host, then we suppport it. */
169 #ifndef VIRTIO_NET_F_MQ
170 /* Device supports Receive Flow Steering */
171 #define VIRTIO_NET_F_MQ 0x400000
172 #define VIRTIO_NET_CTRL_MQ 4
173 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET 0
174 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN 1
175 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX 0x8000
179 * This is the first element of the scatter-gather list. If you don't
180 * specify GSO or CSUM features, you can simply ignore the header.
182 struct virtio_net_hdr {
183 #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 /**< Use csum_start,csum_offset*/
185 #define VIRTIO_NET_HDR_GSO_NONE 0 /**< Not a GSO frame */
186 #define VIRTIO_NET_HDR_GSO_TCPV4 1 /**< GSO frame, IPv4 TCP (TSO) */
187 #define VIRTIO_NET_HDR_GSO_UDP 3 /**< GSO frame, IPv4 UDP (UFO) */
188 #define VIRTIO_NET_HDR_GSO_TCPV6 4 /**< GSO frame, IPv6 TCP */
189 #define VIRTIO_NET_HDR_GSO_ECN 0x80 /**< TCP has ECN set */
191 uint16_t hdr_len; /**< Ethernet + IP + tcp/udp hdrs */
192 uint16_t gso_size; /**< Bytes to append to hdr_len per frame */
193 uint16_t csum_start; /**< Position to start checksumming from */
194 uint16_t csum_offset; /**< Offset after that to place checksum */
198 * This is the version of the header to use when the MRG_RXBUF
199 * feature has been negotiated.
201 struct virtio_net_hdr_mrg_rxbuf {
202 struct virtio_net_hdr hdr;
203 uint16_t num_buffers; /**< Number of merged rx buffers */
207 * Tell the backend not to interrupt us.
209 void virtqueue_disable_intr(struct virtqueue *vq);
211 * Dump virtqueue internal structures, for debug purpose only.
213 void virtqueue_dump(struct virtqueue *vq);
215 * Get all mbufs to be freed.
217 struct rte_mbuf *virtqueue_detatch_unused(struct virtqueue *vq);
220 virtqueue_full(const struct virtqueue *vq)
222 return vq->vq_free_cnt == 0;
225 #define VIRTQUEUE_NUSED(vq) ((uint16_t)((vq)->vq_ring.used->idx - (vq)->vq_used_cons_idx))
227 static inline void __attribute__((always_inline))
228 vq_update_avail_idx(struct virtqueue *vq)
230 rte_compiler_barrier();
231 vq->vq_ring.avail->idx = vq->vq_avail_idx;
234 static inline void __attribute__((always_inline))
235 vq_update_avail_ring(struct virtqueue *vq, uint16_t desc_idx)
239 * Place the head of the descriptor chain into the next slot and make
240 * it usable to the host. The chain is made available now rather than
241 * deferring to virtqueue_notify() in the hopes that if the host is
242 * currently running on another CPU, we can keep it processing the new
245 avail_idx = (uint16_t)(vq->vq_avail_idx & (vq->vq_nentries - 1));
246 vq->vq_ring.avail->ring[avail_idx] = desc_idx;
250 static inline int __attribute__((always_inline))
251 virtqueue_kick_prepare(struct virtqueue *vq)
253 return !(vq->vq_ring.used->flags & VRING_USED_F_NO_NOTIFY);
256 static inline void __attribute__((always_inline))
257 virtqueue_notify(struct virtqueue *vq)
260 * Ensure updated avail->idx is visible to host. mb() necessary?
261 * For virtio on IA, the notificaiton is through io port operation
262 * which is a serialization instruction itself.
264 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_NOTIFY, vq->vq_queue_index);
267 static inline void __attribute__((always_inline))
268 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
270 struct vring_desc *dp, *dp_tail;
271 struct vq_desc_extra *dxp;
272 uint16_t desc_idx_last = desc_idx;
274 dp = &vq->vq_ring.desc[desc_idx];
275 dxp = &vq->vq_descx[desc_idx];
276 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
277 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
278 while (dp->flags & VRING_DESC_F_NEXT) {
279 desc_idx_last = dp->next;
280 dp = &vq->vq_ring.desc[dp->next];
286 * We must append the existing free chain, if any, to the end of
287 * newly freed chain. If the virtqueue was completely used, then
288 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
290 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
291 vq->vq_desc_head_idx = desc_idx;
293 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
294 dp_tail->next = desc_idx;
297 vq->vq_desc_tail_idx = desc_idx_last;
298 dp->next = VQ_RING_DESC_CHAIN_END;
301 static inline int __attribute__((always_inline))
302 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
304 struct vq_desc_extra *dxp;
305 struct vring_desc *start_dp;
307 uint16_t head_idx, idx;
309 if (unlikely(vq->vq_free_cnt == 0))
311 if (unlikely(vq->vq_free_cnt < needed))
314 head_idx = vq->vq_desc_head_idx;
315 if (unlikely(head_idx >= vq->vq_nentries))
319 dxp = &vq->vq_descx[idx];
320 dxp->cookie = (void *)cookie;
321 dxp->ndescs = needed;
323 start_dp = vq->vq_ring.desc;
325 (uint64_t) (cookie->buf_physaddr + RTE_PKTMBUF_HEADROOM - sizeof(struct virtio_net_hdr));
326 start_dp[idx].len = cookie->buf_len - RTE_PKTMBUF_HEADROOM + sizeof(struct virtio_net_hdr);
327 start_dp[idx].flags = VRING_DESC_F_WRITE;
328 idx = start_dp[idx].next;
329 vq->vq_desc_head_idx = idx;
330 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
331 vq->vq_desc_tail_idx = idx;
332 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
333 vq_update_avail_ring(vq, head_idx);
338 static inline int __attribute__((always_inline))
339 virtqueue_enqueue_xmit(struct virtqueue *txvq, struct rte_mbuf *cookie)
341 struct vq_desc_extra *dxp;
342 struct vring_desc *start_dp;
344 uint16_t head_idx, idx;
346 if (unlikely(txvq->vq_free_cnt == 0))
348 if (unlikely(txvq->vq_free_cnt < needed))
350 head_idx = txvq->vq_desc_head_idx;
351 if (unlikely(head_idx >= txvq->vq_nentries))
355 dxp = &txvq->vq_descx[idx];
356 if (dxp->cookie != NULL)
357 rte_pktmbuf_free_seg(dxp->cookie);
358 dxp->cookie = (void *)cookie;
359 dxp->ndescs = needed;
361 start_dp = txvq->vq_ring.desc;
363 txvq->virtio_net_hdr_mem + idx * sizeof(struct virtio_net_hdr);
364 start_dp[idx].len = sizeof(struct virtio_net_hdr);
365 start_dp[idx].flags = VRING_DESC_F_NEXT;
366 idx = start_dp[idx].next;
367 start_dp[idx].addr = RTE_MBUF_DATA_DMA_ADDR(cookie);
368 start_dp[idx].len = cookie->pkt.data_len;
369 start_dp[idx].flags = 0;
370 idx = start_dp[idx].next;
371 txvq->vq_desc_head_idx = idx;
372 if (txvq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
373 txvq->vq_desc_tail_idx = idx;
374 txvq->vq_free_cnt = (uint16_t)(txvq->vq_free_cnt - needed);
375 vq_update_avail_ring(txvq, head_idx);
380 static inline uint16_t __attribute__((always_inline))
381 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts, uint32_t *len, uint16_t num)
383 struct vring_used_elem *uep;
384 struct rte_mbuf *cookie;
385 uint16_t used_idx, desc_idx;
388 /* Caller does the check */
389 for (i = 0; i < num; i++) {
390 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
391 uep = &vq->vq_ring.used->ring[used_idx];
392 desc_idx = (uint16_t) uep->id;
394 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
396 if (unlikely(cookie == NULL)) {
397 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
398 vq->vq_used_cons_idx);
402 rte_prefetch0(cookie);
403 rte_packet_prefetch(cookie->pkt.data);
405 vq->vq_used_cons_idx++;
406 vq_ring_free_chain(vq, desc_idx);
407 vq->vq_descx[desc_idx].cookie = NULL;
413 static inline uint16_t __attribute__((always_inline))
414 virtqueue_dequeue_pkt_tx(struct virtqueue *vq)
416 struct vring_used_elem *uep;
417 uint16_t used_idx, desc_idx;
419 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
420 uep = &vq->vq_ring.used->ring[used_idx];
421 desc_idx = (uint16_t) uep->id;
422 vq->vq_used_cons_idx++;
423 vq_ring_free_chain(vq, desc_idx);
428 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
429 #define VIRTQUEUE_DUMP(vq) do { \
430 uint16_t used_idx, nused; \
431 used_idx = (vq)->vq_ring.used->idx; \
432 nused = (uint16_t)(used_idx - (vq)->vq_used_cons_idx); \
433 PMD_INIT_LOG(DEBUG, \
434 "VQ: %s - size=%d; free=%d; used=%d; desc_head_idx=%d;" \
435 " avail.idx=%d; used_cons_idx=%d; used.idx=%d;" \
436 " avail.flags=0x%x; used.flags=0x%x", \
437 (vq)->vq_name, (vq)->vq_nentries, (vq)->vq_free_cnt, nused, \
438 (vq)->vq_desc_head_idx, (vq)->vq_ring.avail->idx, \
439 (vq)->vq_used_cons_idx, (vq)->vq_ring.used->idx, \
440 (vq)->vq_ring.avail->flags, (vq)->vq_ring.used->flags); \
443 #define VIRTQUEUE_DUMP(vq) do { } while (0)
446 #endif /* _VIRTQUEUE_H_ */