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.
40 #include <rte_cycles.h>
41 #include <rte_memory.h>
42 #include <rte_memzone.h>
43 #include <rte_branch_prediction.h>
44 #include <rte_mempool.h>
45 #include <rte_malloc.h>
47 #include <rte_ether.h>
48 #include <rte_ethdev.h>
49 #include <rte_prefetch.h>
50 #include <rte_string_fns.h>
51 #include <rte_errno.h>
52 #include <rte_byteorder.h>
53 #include <rte_cpuflags.h>
59 #include "virtio_logs.h"
60 #include "virtio_ethdev.h"
61 #include "virtio_pci.h"
62 #include "virtqueue.h"
63 #include "virtio_rxtx.h"
65 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
66 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
68 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0)
72 #define VIRTIO_SIMPLE_FLAGS ((uint32_t)ETH_TXQ_FLAGS_NOMULTSEGS | \
73 ETH_TXQ_FLAGS_NOOFFLOADS)
76 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
78 struct vring_desc *dp, *dp_tail;
79 struct vq_desc_extra *dxp;
80 uint16_t desc_idx_last = desc_idx;
82 dp = &vq->vq_ring.desc[desc_idx];
83 dxp = &vq->vq_descx[desc_idx];
84 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
85 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
86 while (dp->flags & VRING_DESC_F_NEXT) {
87 desc_idx_last = dp->next;
88 dp = &vq->vq_ring.desc[dp->next];
94 * We must append the existing free chain, if any, to the end of
95 * newly freed chain. If the virtqueue was completely used, then
96 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
98 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
99 vq->vq_desc_head_idx = desc_idx;
101 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
102 dp_tail->next = desc_idx;
105 vq->vq_desc_tail_idx = desc_idx_last;
106 dp->next = VQ_RING_DESC_CHAIN_END;
110 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
111 uint32_t *len, uint16_t num)
113 struct vring_used_elem *uep;
114 struct rte_mbuf *cookie;
115 uint16_t used_idx, desc_idx;
118 /* Caller does the check */
119 for (i = 0; i < num ; i++) {
120 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
121 uep = &vq->vq_ring.used->ring[used_idx];
122 desc_idx = (uint16_t) uep->id;
124 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
126 if (unlikely(cookie == NULL)) {
127 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u\n",
128 vq->vq_used_cons_idx);
132 rte_prefetch0(cookie);
133 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
135 vq->vq_used_cons_idx++;
136 vq_ring_free_chain(vq, desc_idx);
137 vq->vq_descx[desc_idx].cookie = NULL;
143 #ifndef DEFAULT_TX_FREE_THRESH
144 #define DEFAULT_TX_FREE_THRESH 32
147 /* Cleanup from completed transmits. */
149 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
151 uint16_t i, used_idx, desc_idx;
152 for (i = 0; i < num; i++) {
153 struct vring_used_elem *uep;
154 struct vq_desc_extra *dxp;
156 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
157 uep = &vq->vq_ring.used->ring[used_idx];
159 desc_idx = (uint16_t) uep->id;
160 dxp = &vq->vq_descx[desc_idx];
161 vq->vq_used_cons_idx++;
162 vq_ring_free_chain(vq, desc_idx);
164 if (dxp->cookie != NULL) {
165 rte_pktmbuf_free(dxp->cookie);
173 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
175 struct vq_desc_extra *dxp;
176 struct virtio_hw *hw = vq->hw;
177 struct vring_desc *start_dp;
179 uint16_t head_idx, idx;
181 if (unlikely(vq->vq_free_cnt == 0))
183 if (unlikely(vq->vq_free_cnt < needed))
186 head_idx = vq->vq_desc_head_idx;
187 if (unlikely(head_idx >= vq->vq_nentries))
191 dxp = &vq->vq_descx[idx];
192 dxp->cookie = (void *)cookie;
193 dxp->ndescs = needed;
195 start_dp = vq->vq_ring.desc;
197 VIRTIO_MBUF_ADDR(cookie, vq) +
198 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
200 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
201 start_dp[idx].flags = VRING_DESC_F_WRITE;
202 idx = start_dp[idx].next;
203 vq->vq_desc_head_idx = idx;
204 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
205 vq->vq_desc_tail_idx = idx;
206 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
207 vq_update_avail_ring(vq, head_idx);
213 tx_offload_enabled(struct virtio_hw *hw)
215 return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM);
219 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
220 uint16_t needed, int use_indirect, int can_push)
222 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
223 struct vq_desc_extra *dxp;
224 struct virtqueue *vq = txvq->vq;
225 struct vring_desc *start_dp;
226 uint16_t seg_num = cookie->nb_segs;
227 uint16_t head_idx, idx;
228 uint16_t head_size = vq->hw->vtnet_hdr_size;
229 struct virtio_net_hdr *hdr;
232 offload = tx_offload_enabled(vq->hw);
233 head_idx = vq->vq_desc_head_idx;
235 dxp = &vq->vq_descx[idx];
236 dxp->cookie = (void *)cookie;
237 dxp->ndescs = needed;
239 start_dp = vq->vq_ring.desc;
242 /* prepend cannot fail, checked by caller */
243 hdr = (struct virtio_net_hdr *)
244 rte_pktmbuf_prepend(cookie, head_size);
245 /* if offload disabled, it is not zeroed below, do it now */
247 memset(hdr, 0, head_size);
248 } else if (use_indirect) {
249 /* setup tx ring slot to point to indirect
250 * descriptor list stored in reserved region.
252 * the first slot in indirect ring is already preset
253 * to point to the header in reserved region
255 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
256 RTE_PTR_DIFF(&txr[idx].tx_indir, txr);
257 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
258 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
259 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
261 /* loop below will fill in rest of the indirect elements */
262 start_dp = txr[idx].tx_indir;
265 /* setup first tx ring slot to point to header
266 * stored in reserved region.
268 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
269 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
270 start_dp[idx].len = vq->hw->vtnet_hdr_size;
271 start_dp[idx].flags = VRING_DESC_F_NEXT;
272 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
274 idx = start_dp[idx].next;
278 /* Checksum Offload */
279 switch (cookie->ol_flags & PKT_TX_L4_MASK) {
280 case PKT_TX_UDP_CKSUM:
281 hdr->csum_start = cookie->l2_len + cookie->l3_len;
282 hdr->csum_offset = offsetof(struct udp_hdr,
284 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
287 case PKT_TX_TCP_CKSUM:
288 hdr->csum_start = cookie->l2_len + cookie->l3_len;
289 hdr->csum_offset = offsetof(struct tcp_hdr, cksum);
290 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
295 hdr->csum_offset = 0;
306 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
307 start_dp[idx].len = cookie->data_len;
308 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
309 idx = start_dp[idx].next;
310 } while ((cookie = cookie->next) != NULL);
313 idx = vq->vq_ring.desc[head_idx].next;
315 vq->vq_desc_head_idx = idx;
316 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
317 vq->vq_desc_tail_idx = idx;
318 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
319 vq_update_avail_ring(vq, head_idx);
323 virtio_dev_vring_start(struct virtqueue *vq)
325 int size = vq->vq_nentries;
326 struct vring *vr = &vq->vq_ring;
327 uint8_t *ring_mem = vq->vq_ring_virt_mem;
329 PMD_INIT_FUNC_TRACE();
332 * Reinitialise since virtio port might have been stopped and restarted
334 memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
335 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
336 vq->vq_used_cons_idx = 0;
337 vq->vq_desc_head_idx = 0;
338 vq->vq_avail_idx = 0;
339 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
340 vq->vq_free_cnt = vq->vq_nentries;
341 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
343 vring_desc_init(vr->desc, size);
346 * Disable device(host) interrupting guest
348 virtqueue_disable_intr(vq);
352 virtio_dev_cq_start(struct rte_eth_dev *dev)
354 struct virtio_hw *hw = dev->data->dev_private;
356 if (hw->cvq && hw->cvq->vq) {
357 virtio_dev_vring_start(hw->cvq->vq);
358 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
363 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
366 * Start receive and transmit vrings
367 * - Setup vring structure for all queues
368 * - Initialize descriptor for the rx vring
369 * - Allocate blank mbufs for the each rx descriptor
374 struct virtio_hw *hw = dev->data->dev_private;
376 PMD_INIT_FUNC_TRACE();
378 /* Start rx vring. */
379 for (i = 0; i < dev->data->nb_rx_queues; i++) {
380 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
381 struct virtqueue *vq = rxvq->vq;
385 virtio_dev_vring_start(vq);
386 if (rxvq->mpool == NULL) {
387 rte_exit(EXIT_FAILURE,
388 "Cannot allocate mbufs for rx virtqueue");
391 /* Allocate blank mbufs for the each rx descriptor */
395 if (hw->use_simple_rxtx) {
396 for (desc_idx = 0; desc_idx < vq->vq_nentries;
398 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
399 vq->vq_ring.desc[desc_idx].flags =
404 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
405 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
407 vq->sw_ring[vq->vq_nentries + desc_idx] =
411 while (!virtqueue_full(vq)) {
412 m = rte_mbuf_raw_alloc(rxvq->mpool);
416 /******************************************
417 * Enqueue allocated buffers *
418 *******************************************/
419 if (hw->use_simple_rxtx)
420 error = virtqueue_enqueue_recv_refill_simple(vq, m);
422 error = virtqueue_enqueue_recv_refill(vq, m);
431 vq_update_avail_idx(vq);
433 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
438 /* Start tx vring. */
439 for (i = 0; i < dev->data->nb_tx_queues; i++) {
440 struct virtnet_tx *txvq = dev->data->tx_queues[i];
441 struct virtqueue *vq = txvq->vq;
443 virtio_dev_vring_start(vq);
444 if (hw->use_simple_rxtx) {
445 uint16_t mid_idx = vq->vq_nentries >> 1;
447 for (desc_idx = 0; desc_idx < mid_idx; desc_idx++) {
448 vq->vq_ring.avail->ring[desc_idx] =
450 vq->vq_ring.desc[desc_idx + mid_idx].next =
452 vq->vq_ring.desc[desc_idx + mid_idx].addr =
453 txvq->virtio_net_hdr_mem +
454 offsetof(struct virtio_tx_region, tx_hdr);
455 vq->vq_ring.desc[desc_idx + mid_idx].len =
456 vq->hw->vtnet_hdr_size;
457 vq->vq_ring.desc[desc_idx + mid_idx].flags =
459 vq->vq_ring.desc[desc_idx].flags = 0;
461 for (desc_idx = mid_idx; desc_idx < vq->vq_nentries;
463 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
471 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
474 unsigned int socket_id,
475 __rte_unused const struct rte_eth_rxconf *rx_conf,
476 struct rte_mempool *mp)
478 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
479 struct virtnet_rx *rxvq;
482 PMD_INIT_FUNC_TRACE();
483 ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
484 nb_desc, socket_id, (void **)&rxvq);
486 PMD_INIT_LOG(ERR, "rvq initialization failed");
490 /* Create mempool for rx mbuf allocation */
493 dev->data->rx_queues[queue_idx] = rxvq;
495 virtio_rxq_vec_setup(rxvq);
501 virtio_dev_rx_queue_release(void *rxq)
503 struct virtnet_rx *rxvq = rxq;
504 struct virtqueue *vq;
505 const struct rte_memzone *mz;
511 * rxvq is freed when vq is freed, and as mz should be freed after the
512 * del_queue, so we reserve the mz pointer first.
517 virtio_dev_queue_release(vq);
518 rte_memzone_free(mz);
522 virtio_update_rxtx_handler(struct rte_eth_dev *dev,
523 const struct rte_eth_txconf *tx_conf)
525 uint8_t use_simple_rxtx = 0;
526 struct virtio_hw *hw = dev->data->dev_private;
528 #if defined RTE_ARCH_X86
529 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE3))
531 #elif defined RTE_ARCH_ARM64 || defined CONFIG_RTE_ARCH_ARM
532 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON))
535 /* Use simple rx/tx func if single segment and no offloads */
536 if (use_simple_rxtx &&
537 (tx_conf->txq_flags & VIRTIO_SIMPLE_FLAGS) == VIRTIO_SIMPLE_FLAGS &&
538 !vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
539 PMD_INIT_LOG(INFO, "Using simple rx/tx path");
540 dev->tx_pkt_burst = virtio_xmit_pkts_simple;
541 dev->rx_pkt_burst = virtio_recv_pkts_vec;
542 hw->use_simple_rxtx = use_simple_rxtx;
547 * struct rte_eth_dev *dev: Used to update dev
548 * uint16_t nb_desc: Defaults to values read from config space
549 * unsigned int socket_id: Used to allocate memzone
550 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
551 * uint16_t queue_idx: Just used as an index in dev txq list
554 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
557 unsigned int socket_id,
558 const struct rte_eth_txconf *tx_conf)
560 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
561 struct virtnet_tx *txvq;
562 struct virtqueue *vq;
563 uint16_t tx_free_thresh;
566 PMD_INIT_FUNC_TRACE();
569 virtio_update_rxtx_handler(dev, tx_conf);
571 ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
572 nb_desc, socket_id, (void **)&txvq);
574 PMD_INIT_LOG(ERR, "tvq initialization failed");
579 tx_free_thresh = tx_conf->tx_free_thresh;
580 if (tx_free_thresh == 0)
582 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
584 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
585 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
586 "number of TX entries minus 3 (%u)."
587 " (tx_free_thresh=%u port=%u queue=%u)\n",
589 tx_free_thresh, dev->data->port_id, queue_idx);
593 vq->vq_free_thresh = tx_free_thresh;
595 dev->data->tx_queues[queue_idx] = txvq;
600 virtio_dev_tx_queue_release(void *txq)
602 struct virtnet_tx *txvq = txq;
603 struct virtqueue *vq;
604 const struct rte_memzone *mz;
605 const struct rte_memzone *hdr_mz;
611 * txvq is freed when vq is freed, and as mz should be freed after the
612 * del_queue, so we reserve the mz pointer first.
616 hdr_mz = txvq->virtio_net_hdr_mz;
618 virtio_dev_queue_release(vq);
619 rte_memzone_free(mz);
620 rte_memzone_free(hdr_mz);
624 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
628 * Requeue the discarded mbuf. This should always be
629 * successful since it was just dequeued.
631 error = virtqueue_enqueue_recv_refill(vq, m);
632 if (unlikely(error)) {
633 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
639 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
641 uint32_t s = mbuf->pkt_len;
642 struct ether_addr *ea;
645 stats->size_bins[1]++;
646 } else if (s > 64 && s < 1024) {
649 /* count zeros, and offset into correct bin */
650 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
651 stats->size_bins[bin]++;
654 stats->size_bins[0]++;
656 stats->size_bins[6]++;
658 stats->size_bins[7]++;
661 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
662 if (is_multicast_ether_addr(ea)) {
663 if (is_broadcast_ether_addr(ea))
670 /* Optionally fill offload information in structure */
672 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
674 struct rte_net_hdr_lens hdr_lens;
675 uint32_t hdrlen, ptype;
676 int l4_supported = 0;
679 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
682 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
684 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
685 m->packet_type = ptype;
686 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
687 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
688 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
691 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
692 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
693 if (hdr->csum_start <= hdrlen && l4_supported) {
694 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
696 /* Unknown proto or tunnel, do sw cksum. We can assume
697 * the cksum field is in the first segment since the
698 * buffers we provided to the host are large enough.
699 * In case of SCTP, this will be wrong since it's a CRC
700 * but there's nothing we can do.
704 rte_raw_cksum_mbuf(m, hdr->csum_start,
705 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
707 if (likely(csum != 0xffff))
709 off = hdr->csum_offset + hdr->csum_start;
710 if (rte_pktmbuf_data_len(m) >= off + 1)
711 *rte_pktmbuf_mtod_offset(m, uint16_t *,
714 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
715 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
722 rx_offload_enabled(struct virtio_hw *hw)
724 return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM);
727 #define VIRTIO_MBUF_BURST_SZ 64
728 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
730 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
732 struct virtnet_rx *rxvq = rx_queue;
733 struct virtqueue *vq = rxvq->vq;
734 struct virtio_hw *hw;
735 struct rte_mbuf *rxm, *new_mbuf;
736 uint16_t nb_used, num, nb_rx;
737 uint32_t len[VIRTIO_MBUF_BURST_SZ];
738 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
740 uint32_t i, nb_enqueued;
743 struct virtio_net_hdr *hdr;
745 nb_used = VIRTQUEUE_NUSED(vq);
749 num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
750 num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
751 if (likely(num > DESC_PER_CACHELINE))
752 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
754 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
755 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
760 hdr_size = hw->vtnet_hdr_size;
761 offload = rx_offload_enabled(hw);
763 for (i = 0; i < num ; i++) {
766 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
768 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
769 PMD_RX_LOG(ERR, "Packet drop");
771 virtio_discard_rxbuf(vq, rxm);
772 rxvq->stats.errors++;
776 rxm->port = rxvq->port_id;
777 rxm->data_off = RTE_PKTMBUF_HEADROOM;
783 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
784 rxm->data_len = (uint16_t)(len[i] - hdr_size);
786 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
787 RTE_PKTMBUF_HEADROOM - hdr_size);
792 if (offload && virtio_rx_offload(rxm, hdr) < 0) {
793 virtio_discard_rxbuf(vq, rxm);
794 rxvq->stats.errors++;
798 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
800 rx_pkts[nb_rx++] = rxm;
802 rxvq->stats.bytes += rx_pkts[nb_rx - 1]->pkt_len;
803 virtio_update_packet_stats(&rxvq->stats, rxm);
806 rxvq->stats.packets += nb_rx;
808 /* Allocate new mbuf for the used descriptor */
810 while (likely(!virtqueue_full(vq))) {
811 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
812 if (unlikely(new_mbuf == NULL)) {
813 struct rte_eth_dev *dev
814 = &rte_eth_devices[rxvq->port_id];
815 dev->data->rx_mbuf_alloc_failed++;
818 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
819 if (unlikely(error)) {
820 rte_pktmbuf_free(new_mbuf);
826 if (likely(nb_enqueued)) {
827 vq_update_avail_idx(vq);
829 if (unlikely(virtqueue_kick_prepare(vq))) {
830 virtqueue_notify(vq);
831 PMD_RX_LOG(DEBUG, "Notified");
839 virtio_recv_mergeable_pkts(void *rx_queue,
840 struct rte_mbuf **rx_pkts,
843 struct virtnet_rx *rxvq = rx_queue;
844 struct virtqueue *vq = rxvq->vq;
845 struct virtio_hw *hw;
846 struct rte_mbuf *rxm, *new_mbuf;
847 uint16_t nb_used, num, nb_rx;
848 uint32_t len[VIRTIO_MBUF_BURST_SZ];
849 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
850 struct rte_mbuf *prev;
852 uint32_t i, nb_enqueued;
859 nb_used = VIRTQUEUE_NUSED(vq);
863 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
872 hdr_size = hw->vtnet_hdr_size;
873 offload = rx_offload_enabled(hw);
875 while (i < nb_used) {
876 struct virtio_net_hdr_mrg_rxbuf *header;
878 if (nb_rx == nb_pkts)
881 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, 1);
887 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
888 PMD_RX_LOG(DEBUG, "packet len:%d", len[0]);
892 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
893 PMD_RX_LOG(ERR, "Packet drop");
895 virtio_discard_rxbuf(vq, rxm);
896 rxvq->stats.errors++;
900 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
901 RTE_PKTMBUF_HEADROOM - hdr_size);
902 seg_num = header->num_buffers;
907 rxm->data_off = RTE_PKTMBUF_HEADROOM;
908 rxm->nb_segs = seg_num;
912 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
913 rxm->data_len = (uint16_t)(len[0] - hdr_size);
915 rxm->port = rxvq->port_id;
916 rx_pkts[nb_rx] = rxm;
919 if (offload && virtio_rx_offload(rxm, &header->hdr) < 0) {
920 virtio_discard_rxbuf(vq, rxm);
921 rxvq->stats.errors++;
925 seg_res = seg_num - 1;
927 while (seg_res != 0) {
929 * Get extra segments for current uncompleted packet.
932 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
933 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
935 virtqueue_dequeue_burst_rx(vq,
936 rcv_pkts, len, rcv_cnt);
941 "No enough segments for packet.");
943 virtio_discard_rxbuf(vq, rxm);
944 rxvq->stats.errors++;
950 while (extra_idx < rcv_cnt) {
951 rxm = rcv_pkts[extra_idx];
953 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
955 rxm->pkt_len = (uint32_t)(len[extra_idx]);
956 rxm->data_len = (uint16_t)(len[extra_idx]);
962 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
969 rte_vlan_strip(rx_pkts[nb_rx]);
971 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
972 rx_pkts[nb_rx]->data_len);
974 rxvq->stats.bytes += rx_pkts[nb_rx]->pkt_len;
975 virtio_update_packet_stats(&rxvq->stats, rx_pkts[nb_rx]);
979 rxvq->stats.packets += nb_rx;
981 /* Allocate new mbuf for the used descriptor */
983 while (likely(!virtqueue_full(vq))) {
984 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
985 if (unlikely(new_mbuf == NULL)) {
986 struct rte_eth_dev *dev
987 = &rte_eth_devices[rxvq->port_id];
988 dev->data->rx_mbuf_alloc_failed++;
991 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
992 if (unlikely(error)) {
993 rte_pktmbuf_free(new_mbuf);
999 if (likely(nb_enqueued)) {
1000 vq_update_avail_idx(vq);
1002 if (unlikely(virtqueue_kick_prepare(vq))) {
1003 virtqueue_notify(vq);
1004 PMD_RX_LOG(DEBUG, "Notified");
1012 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1014 struct virtnet_tx *txvq = tx_queue;
1015 struct virtqueue *vq = txvq->vq;
1016 struct virtio_hw *hw = vq->hw;
1017 uint16_t hdr_size = hw->vtnet_hdr_size;
1018 uint16_t nb_used, nb_tx;
1021 if (unlikely(nb_pkts < 1))
1024 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
1025 nb_used = VIRTQUEUE_NUSED(vq);
1028 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
1029 virtio_xmit_cleanup(vq, nb_used);
1031 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1032 struct rte_mbuf *txm = tx_pkts[nb_tx];
1033 int can_push = 0, use_indirect = 0, slots, need;
1035 /* Do VLAN tag insertion */
1036 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
1037 error = rte_vlan_insert(&txm);
1038 if (unlikely(error)) {
1039 rte_pktmbuf_free(txm);
1044 /* optimize ring usage */
1045 if (vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) &&
1046 rte_mbuf_refcnt_read(txm) == 1 &&
1047 RTE_MBUF_DIRECT(txm) &&
1048 txm->nb_segs == 1 &&
1049 rte_pktmbuf_headroom(txm) >= hdr_size &&
1050 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
1051 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
1053 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
1054 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
1057 /* How many main ring entries are needed to this Tx?
1058 * any_layout => number of segments
1060 * default => number of segments + 1
1062 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
1063 need = slots - vq->vq_free_cnt;
1065 /* Positive value indicates it need free vring descriptors */
1066 if (unlikely(need > 0)) {
1067 nb_used = VIRTQUEUE_NUSED(vq);
1069 need = RTE_MIN(need, (int)nb_used);
1071 virtio_xmit_cleanup(vq, need);
1072 need = slots - vq->vq_free_cnt;
1073 if (unlikely(need > 0)) {
1075 "No free tx descriptors to transmit");
1080 /* Enqueue Packet buffers */
1081 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect, can_push);
1083 txvq->stats.bytes += txm->pkt_len;
1084 virtio_update_packet_stats(&txvq->stats, txm);
1087 txvq->stats.packets += nb_tx;
1089 if (likely(nb_tx)) {
1090 vq_update_avail_idx(vq);
1092 if (unlikely(virtqueue_kick_prepare(vq))) {
1093 virtqueue_notify(vq);
1094 PMD_TX_LOG(DEBUG, "Notified backend after xmit");