1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_cycles.h>
12 #include <rte_memory.h>
13 #include <rte_branch_prediction.h>
14 #include <rte_mempool.h>
15 #include <rte_malloc.h>
17 #include <rte_ether.h>
18 #include <rte_ethdev_driver.h>
19 #include <rte_prefetch.h>
20 #include <rte_string_fns.h>
21 #include <rte_errno.h>
22 #include <rte_byteorder.h>
28 #include "virtio_logs.h"
29 #include "virtio_ethdev.h"
30 #include "virtio_pci.h"
31 #include "virtqueue.h"
32 #include "virtio_rxtx.h"
33 #include "virtio_rxtx_simple.h"
35 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
36 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
38 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0)
42 virtio_dev_rx_queue_done(void *rxq, uint16_t offset)
44 struct virtnet_rx *rxvq = rxq;
45 struct virtqueue *vq = rxvq->vq;
47 return VIRTQUEUE_NUSED(vq) >= offset;
51 vq_ring_free_inorder(struct virtqueue *vq, uint16_t desc_idx, uint16_t num)
53 vq->vq_free_cnt += num;
54 vq->vq_desc_tail_idx = desc_idx & (vq->vq_nentries - 1);
58 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
60 struct vring_desc *dp, *dp_tail;
61 struct vq_desc_extra *dxp;
62 uint16_t desc_idx_last = desc_idx;
64 dp = &vq->vq_ring.desc[desc_idx];
65 dxp = &vq->vq_descx[desc_idx];
66 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
67 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
68 while (dp->flags & VRING_DESC_F_NEXT) {
69 desc_idx_last = dp->next;
70 dp = &vq->vq_ring.desc[dp->next];
76 * We must append the existing free chain, if any, to the end of
77 * newly freed chain. If the virtqueue was completely used, then
78 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
80 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
81 vq->vq_desc_head_idx = desc_idx;
83 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
84 dp_tail->next = desc_idx;
87 vq->vq_desc_tail_idx = desc_idx_last;
88 dp->next = VQ_RING_DESC_CHAIN_END;
92 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
93 uint32_t *len, uint16_t num)
95 struct vring_used_elem *uep;
96 struct rte_mbuf *cookie;
97 uint16_t used_idx, desc_idx;
100 /* Caller does the check */
101 for (i = 0; i < num ; i++) {
102 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
103 uep = &vq->vq_ring.used->ring[used_idx];
104 desc_idx = (uint16_t) uep->id;
106 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
108 if (unlikely(cookie == NULL)) {
109 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
110 vq->vq_used_cons_idx);
114 rte_prefetch0(cookie);
115 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
117 vq->vq_used_cons_idx++;
118 vq_ring_free_chain(vq, desc_idx);
119 vq->vq_descx[desc_idx].cookie = NULL;
125 #ifndef DEFAULT_TX_FREE_THRESH
126 #define DEFAULT_TX_FREE_THRESH 32
129 /* Cleanup from completed transmits. */
131 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
133 uint16_t i, used_idx, desc_idx;
134 for (i = 0; i < num; i++) {
135 struct vring_used_elem *uep;
136 struct vq_desc_extra *dxp;
138 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
139 uep = &vq->vq_ring.used->ring[used_idx];
141 desc_idx = (uint16_t) uep->id;
142 dxp = &vq->vq_descx[desc_idx];
143 vq->vq_used_cons_idx++;
144 vq_ring_free_chain(vq, desc_idx);
146 if (dxp->cookie != NULL) {
147 rte_pktmbuf_free(dxp->cookie);
155 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
157 struct vq_desc_extra *dxp;
158 struct virtio_hw *hw = vq->hw;
159 struct vring_desc *start_dp;
161 uint16_t head_idx, idx;
163 if (unlikely(vq->vq_free_cnt == 0))
165 if (unlikely(vq->vq_free_cnt < needed))
168 head_idx = vq->vq_desc_head_idx;
169 if (unlikely(head_idx >= vq->vq_nentries))
173 dxp = &vq->vq_descx[idx];
174 dxp->cookie = (void *)cookie;
175 dxp->ndescs = needed;
177 start_dp = vq->vq_ring.desc;
179 VIRTIO_MBUF_ADDR(cookie, vq) +
180 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
182 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
183 start_dp[idx].flags = VRING_DESC_F_WRITE;
184 idx = start_dp[idx].next;
185 vq->vq_desc_head_idx = idx;
186 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
187 vq->vq_desc_tail_idx = idx;
188 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
189 vq_update_avail_ring(vq, head_idx);
194 /* When doing TSO, the IP length is not included in the pseudo header
195 * checksum of the packet given to the PMD, but for virtio it is
199 virtio_tso_fix_cksum(struct rte_mbuf *m)
201 /* common case: header is not fragmented */
202 if (likely(rte_pktmbuf_data_len(m) >= m->l2_len + m->l3_len +
204 struct ipv4_hdr *iph;
205 struct ipv6_hdr *ip6h;
207 uint16_t prev_cksum, new_cksum, ip_len, ip_paylen;
210 iph = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *, m->l2_len);
211 th = RTE_PTR_ADD(iph, m->l3_len);
212 if ((iph->version_ihl >> 4) == 4) {
213 iph->hdr_checksum = 0;
214 iph->hdr_checksum = rte_ipv4_cksum(iph);
215 ip_len = iph->total_length;
216 ip_paylen = rte_cpu_to_be_16(rte_be_to_cpu_16(ip_len) -
219 ip6h = (struct ipv6_hdr *)iph;
220 ip_paylen = ip6h->payload_len;
223 /* calculate the new phdr checksum not including ip_paylen */
224 prev_cksum = th->cksum;
227 tmp = (tmp & 0xffff) + (tmp >> 16);
230 /* replace it in the packet */
231 th->cksum = new_cksum;
236 tx_offload_enabled(struct virtio_hw *hw)
238 return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
239 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
240 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
243 /* avoid write operation when necessary, to lessen cache issues */
244 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
245 if ((var) != (val)) \
250 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
251 uint16_t needed, int use_indirect, int can_push)
253 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
254 struct vq_desc_extra *dxp;
255 struct virtqueue *vq = txvq->vq;
256 struct vring_desc *start_dp;
257 uint16_t seg_num = cookie->nb_segs;
258 uint16_t head_idx, idx;
259 uint16_t head_size = vq->hw->vtnet_hdr_size;
260 struct virtio_net_hdr *hdr;
263 offload = tx_offload_enabled(vq->hw);
264 head_idx = vq->vq_desc_head_idx;
266 dxp = &vq->vq_descx[idx];
267 dxp->cookie = (void *)cookie;
268 dxp->ndescs = needed;
270 start_dp = vq->vq_ring.desc;
273 /* prepend cannot fail, checked by caller */
274 hdr = (struct virtio_net_hdr *)
275 rte_pktmbuf_prepend(cookie, head_size);
276 /* rte_pktmbuf_prepend() counts the hdr size to the pkt length,
277 * which is wrong. Below subtract restores correct pkt size.
279 cookie->pkt_len -= head_size;
280 /* if offload disabled, it is not zeroed below, do it now */
282 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
283 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
284 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
285 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
286 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
287 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
289 } else if (use_indirect) {
290 /* setup tx ring slot to point to indirect
291 * descriptor list stored in reserved region.
293 * the first slot in indirect ring is already preset
294 * to point to the header in reserved region
296 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
297 RTE_PTR_DIFF(&txr[idx].tx_indir, txr);
298 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
299 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
300 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
302 /* loop below will fill in rest of the indirect elements */
303 start_dp = txr[idx].tx_indir;
306 /* setup first tx ring slot to point to header
307 * stored in reserved region.
309 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
310 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
311 start_dp[idx].len = vq->hw->vtnet_hdr_size;
312 start_dp[idx].flags = VRING_DESC_F_NEXT;
313 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
315 idx = start_dp[idx].next;
318 /* Checksum Offload / TSO */
320 if (cookie->ol_flags & PKT_TX_TCP_SEG)
321 cookie->ol_flags |= PKT_TX_TCP_CKSUM;
323 switch (cookie->ol_flags & PKT_TX_L4_MASK) {
324 case PKT_TX_UDP_CKSUM:
325 hdr->csum_start = cookie->l2_len + cookie->l3_len;
326 hdr->csum_offset = offsetof(struct udp_hdr,
328 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
331 case PKT_TX_TCP_CKSUM:
332 hdr->csum_start = cookie->l2_len + cookie->l3_len;
333 hdr->csum_offset = offsetof(struct tcp_hdr, cksum);
334 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
338 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
339 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
340 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
344 /* TCP Segmentation Offload */
345 if (cookie->ol_flags & PKT_TX_TCP_SEG) {
346 virtio_tso_fix_cksum(cookie);
347 hdr->gso_type = (cookie->ol_flags & PKT_TX_IPV6) ?
348 VIRTIO_NET_HDR_GSO_TCPV6 :
349 VIRTIO_NET_HDR_GSO_TCPV4;
350 hdr->gso_size = cookie->tso_segsz;
356 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
357 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
358 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
363 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
364 start_dp[idx].len = cookie->data_len;
365 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
366 idx = start_dp[idx].next;
367 } while ((cookie = cookie->next) != NULL);
370 idx = vq->vq_ring.desc[head_idx].next;
372 vq->vq_desc_head_idx = idx;
373 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
374 vq->vq_desc_tail_idx = idx;
375 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
376 vq_update_avail_ring(vq, head_idx);
380 virtio_dev_cq_start(struct rte_eth_dev *dev)
382 struct virtio_hw *hw = dev->data->dev_private;
384 if (hw->cvq && hw->cvq->vq) {
385 rte_spinlock_init(&hw->cvq->lock);
386 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
391 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
394 unsigned int socket_id __rte_unused,
395 const struct rte_eth_rxconf *rx_conf __rte_unused,
396 struct rte_mempool *mp)
398 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
399 struct virtio_hw *hw = dev->data->dev_private;
400 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
401 struct virtnet_rx *rxvq;
403 PMD_INIT_FUNC_TRACE();
405 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
406 nb_desc = vq->vq_nentries;
407 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
410 rxvq->queue_id = queue_idx;
412 if (rxvq->mpool == NULL) {
413 rte_exit(EXIT_FAILURE,
414 "Cannot allocate mbufs for rx virtqueue");
417 dev->data->rx_queues[queue_idx] = rxvq;
423 virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
425 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
426 struct virtio_hw *hw = dev->data->dev_private;
427 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
428 struct virtnet_rx *rxvq = &vq->rxq;
433 PMD_INIT_FUNC_TRACE();
435 /* Allocate blank mbufs for the each rx descriptor */
438 if (hw->use_simple_rx) {
439 for (desc_idx = 0; desc_idx < vq->vq_nentries;
441 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
442 vq->vq_ring.desc[desc_idx].flags =
446 virtio_rxq_vec_setup(rxvq);
449 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
450 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
452 vq->sw_ring[vq->vq_nentries + desc_idx] =
456 if (hw->use_simple_rx) {
457 while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
458 virtio_rxq_rearm_vec(rxvq);
459 nbufs += RTE_VIRTIO_VPMD_RX_REARM_THRESH;
462 while (!virtqueue_full(vq)) {
463 m = rte_mbuf_raw_alloc(rxvq->mpool);
467 /* Enqueue allocated buffers */
468 error = virtqueue_enqueue_recv_refill(vq, m);
476 vq_update_avail_idx(vq);
479 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
487 * struct rte_eth_dev *dev: Used to update dev
488 * uint16_t nb_desc: Defaults to values read from config space
489 * unsigned int socket_id: Used to allocate memzone
490 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
491 * uint16_t queue_idx: Just used as an index in dev txq list
494 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
497 unsigned int socket_id __rte_unused,
498 const struct rte_eth_txconf *tx_conf)
500 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
501 struct virtio_hw *hw = dev->data->dev_private;
502 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
503 struct virtnet_tx *txvq;
504 uint16_t tx_free_thresh;
506 PMD_INIT_FUNC_TRACE();
508 /* cannot use simple rxtx funcs with multisegs or offloads */
509 if (dev->data->dev_conf.txmode.offloads)
510 hw->use_simple_tx = 0;
512 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
513 nb_desc = vq->vq_nentries;
514 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
517 txvq->queue_id = queue_idx;
519 tx_free_thresh = tx_conf->tx_free_thresh;
520 if (tx_free_thresh == 0)
522 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
524 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
525 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
526 "number of TX entries minus 3 (%u)."
527 " (tx_free_thresh=%u port=%u queue=%u)\n",
529 tx_free_thresh, dev->data->port_id, queue_idx);
533 vq->vq_free_thresh = tx_free_thresh;
535 dev->data->tx_queues[queue_idx] = txvq;
540 virtio_dev_tx_queue_setup_finish(struct rte_eth_dev *dev,
543 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
544 struct virtio_hw *hw = dev->data->dev_private;
545 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
546 uint16_t mid_idx = vq->vq_nentries >> 1;
547 struct virtnet_tx *txvq = &vq->txq;
550 PMD_INIT_FUNC_TRACE();
552 if (hw->use_simple_tx) {
553 for (desc_idx = 0; desc_idx < mid_idx; desc_idx++) {
554 vq->vq_ring.avail->ring[desc_idx] =
556 vq->vq_ring.desc[desc_idx + mid_idx].next =
558 vq->vq_ring.desc[desc_idx + mid_idx].addr =
559 txvq->virtio_net_hdr_mem +
560 offsetof(struct virtio_tx_region, tx_hdr);
561 vq->vq_ring.desc[desc_idx + mid_idx].len =
562 vq->hw->vtnet_hdr_size;
563 vq->vq_ring.desc[desc_idx + mid_idx].flags =
565 vq->vq_ring.desc[desc_idx].flags = 0;
567 for (desc_idx = mid_idx; desc_idx < vq->vq_nentries;
569 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
578 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
582 * Requeue the discarded mbuf. This should always be
583 * successful since it was just dequeued.
585 error = virtqueue_enqueue_recv_refill(vq, m);
586 if (unlikely(error)) {
587 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
593 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
595 uint32_t s = mbuf->pkt_len;
596 struct ether_addr *ea;
599 stats->size_bins[1]++;
600 } else if (s > 64 && s < 1024) {
603 /* count zeros, and offset into correct bin */
604 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
605 stats->size_bins[bin]++;
608 stats->size_bins[0]++;
610 stats->size_bins[6]++;
612 stats->size_bins[7]++;
615 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
616 if (is_multicast_ether_addr(ea)) {
617 if (is_broadcast_ether_addr(ea))
624 /* Optionally fill offload information in structure */
626 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
628 struct rte_net_hdr_lens hdr_lens;
629 uint32_t hdrlen, ptype;
630 int l4_supported = 0;
633 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
636 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
638 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
639 m->packet_type = ptype;
640 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
641 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
642 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
645 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
646 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
647 if (hdr->csum_start <= hdrlen && l4_supported) {
648 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
650 /* Unknown proto or tunnel, do sw cksum. We can assume
651 * the cksum field is in the first segment since the
652 * buffers we provided to the host are large enough.
653 * In case of SCTP, this will be wrong since it's a CRC
654 * but there's nothing we can do.
656 uint16_t csum = 0, off;
658 rte_raw_cksum_mbuf(m, hdr->csum_start,
659 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
661 if (likely(csum != 0xffff))
663 off = hdr->csum_offset + hdr->csum_start;
664 if (rte_pktmbuf_data_len(m) >= off + 1)
665 *rte_pktmbuf_mtod_offset(m, uint16_t *,
668 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
669 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
672 /* GSO request, save required information in mbuf */
673 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
674 /* Check unsupported modes */
675 if ((hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) ||
676 (hdr->gso_size == 0)) {
680 /* Update mss lengthes in mbuf */
681 m->tso_segsz = hdr->gso_size;
682 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
683 case VIRTIO_NET_HDR_GSO_TCPV4:
684 case VIRTIO_NET_HDR_GSO_TCPV6:
685 m->ol_flags |= PKT_RX_LRO | \
686 PKT_RX_L4_CKSUM_NONE;
697 rx_offload_enabled(struct virtio_hw *hw)
699 return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
700 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
701 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
704 #define VIRTIO_MBUF_BURST_SZ 64
705 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
707 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
709 struct virtnet_rx *rxvq = rx_queue;
710 struct virtqueue *vq = rxvq->vq;
711 struct virtio_hw *hw = vq->hw;
712 struct rte_mbuf *rxm, *new_mbuf;
713 uint16_t nb_used, num, nb_rx;
714 uint32_t len[VIRTIO_MBUF_BURST_SZ];
715 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
717 uint32_t i, nb_enqueued;
720 struct virtio_net_hdr *hdr;
723 if (unlikely(hw->started == 0))
726 nb_used = VIRTQUEUE_NUSED(vq);
730 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
731 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
732 num = VIRTIO_MBUF_BURST_SZ;
733 if (likely(num > DESC_PER_CACHELINE))
734 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
736 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
737 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
740 hdr_size = hw->vtnet_hdr_size;
741 offload = rx_offload_enabled(hw);
743 for (i = 0; i < num ; i++) {
746 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
748 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
749 PMD_RX_LOG(ERR, "Packet drop");
751 virtio_discard_rxbuf(vq, rxm);
752 rxvq->stats.errors++;
756 rxm->port = rxvq->port_id;
757 rxm->data_off = RTE_PKTMBUF_HEADROOM;
761 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
762 rxm->data_len = (uint16_t)(len[i] - hdr_size);
764 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
765 RTE_PKTMBUF_HEADROOM - hdr_size);
770 if (offload && virtio_rx_offload(rxm, hdr) < 0) {
771 virtio_discard_rxbuf(vq, rxm);
772 rxvq->stats.errors++;
776 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
778 rx_pkts[nb_rx++] = rxm;
780 rxvq->stats.bytes += rxm->pkt_len;
781 virtio_update_packet_stats(&rxvq->stats, rxm);
784 rxvq->stats.packets += nb_rx;
786 /* Allocate new mbuf for the used descriptor */
788 while (likely(!virtqueue_full(vq))) {
789 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
790 if (unlikely(new_mbuf == NULL)) {
791 struct rte_eth_dev *dev
792 = &rte_eth_devices[rxvq->port_id];
793 dev->data->rx_mbuf_alloc_failed++;
796 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
797 if (unlikely(error)) {
798 rte_pktmbuf_free(new_mbuf);
804 if (likely(nb_enqueued)) {
805 vq_update_avail_idx(vq);
807 if (unlikely(virtqueue_kick_prepare(vq))) {
808 virtqueue_notify(vq);
809 PMD_RX_LOG(DEBUG, "Notified");
817 virtio_recv_mergeable_pkts(void *rx_queue,
818 struct rte_mbuf **rx_pkts,
821 struct virtnet_rx *rxvq = rx_queue;
822 struct virtqueue *vq = rxvq->vq;
823 struct virtio_hw *hw = vq->hw;
824 struct rte_mbuf *rxm, *new_mbuf;
825 uint16_t nb_used, num, nb_rx;
826 uint32_t len[VIRTIO_MBUF_BURST_SZ];
827 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
828 struct rte_mbuf *prev;
830 uint32_t i, nb_enqueued;
838 if (unlikely(hw->started == 0))
841 nb_used = VIRTQUEUE_NUSED(vq);
845 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
852 hdr_size = hw->vtnet_hdr_size;
853 offload = rx_offload_enabled(hw);
855 while (i < nb_used) {
856 struct virtio_net_hdr_mrg_rxbuf *header;
858 if (nb_rx == nb_pkts)
861 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, 1);
867 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
868 PMD_RX_LOG(DEBUG, "packet len:%d", len[0]);
872 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
873 PMD_RX_LOG(ERR, "Packet drop");
875 virtio_discard_rxbuf(vq, rxm);
876 rxvq->stats.errors++;
880 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
881 RTE_PKTMBUF_HEADROOM - hdr_size);
882 seg_num = header->num_buffers;
887 rxm->data_off = RTE_PKTMBUF_HEADROOM;
888 rxm->nb_segs = seg_num;
891 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
892 rxm->data_len = (uint16_t)(len[0] - hdr_size);
894 rxm->port = rxvq->port_id;
895 rx_pkts[nb_rx] = rxm;
898 if (offload && virtio_rx_offload(rxm, &header->hdr) < 0) {
899 virtio_discard_rxbuf(vq, rxm);
900 rxvq->stats.errors++;
904 seg_res = seg_num - 1;
906 while (seg_res != 0) {
908 * Get extra segments for current uncompleted packet.
911 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
912 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
914 virtqueue_dequeue_burst_rx(vq,
915 rcv_pkts, len, rcv_cnt);
920 "No enough segments for packet.");
922 virtio_discard_rxbuf(vq, rxm);
923 rxvq->stats.errors++;
929 while (extra_idx < rcv_cnt) {
930 rxm = rcv_pkts[extra_idx];
932 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
933 rxm->pkt_len = (uint32_t)(len[extra_idx]);
934 rxm->data_len = (uint16_t)(len[extra_idx]);
940 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
947 rte_vlan_strip(rx_pkts[nb_rx]);
949 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
950 rx_pkts[nb_rx]->data_len);
952 rxvq->stats.bytes += rx_pkts[nb_rx]->pkt_len;
953 virtio_update_packet_stats(&rxvq->stats, rx_pkts[nb_rx]);
957 rxvq->stats.packets += nb_rx;
959 /* Allocate new mbuf for the used descriptor */
961 while (likely(!virtqueue_full(vq))) {
962 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
963 if (unlikely(new_mbuf == NULL)) {
964 struct rte_eth_dev *dev
965 = &rte_eth_devices[rxvq->port_id];
966 dev->data->rx_mbuf_alloc_failed++;
969 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
970 if (unlikely(error)) {
971 rte_pktmbuf_free(new_mbuf);
977 if (likely(nb_enqueued)) {
978 vq_update_avail_idx(vq);
980 if (unlikely(virtqueue_kick_prepare(vq))) {
981 virtqueue_notify(vq);
982 PMD_RX_LOG(DEBUG, "Notified");
990 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
992 struct virtnet_tx *txvq = tx_queue;
993 struct virtqueue *vq = txvq->vq;
994 struct virtio_hw *hw = vq->hw;
995 uint16_t hdr_size = hw->vtnet_hdr_size;
996 uint16_t nb_used, nb_tx = 0;
999 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
1002 if (unlikely(nb_pkts < 1))
1005 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
1006 nb_used = VIRTQUEUE_NUSED(vq);
1009 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
1010 virtio_xmit_cleanup(vq, nb_used);
1012 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1013 struct rte_mbuf *txm = tx_pkts[nb_tx];
1014 int can_push = 0, use_indirect = 0, slots, need;
1016 /* Do VLAN tag insertion */
1017 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
1018 error = rte_vlan_insert(&txm);
1019 if (unlikely(error)) {
1020 rte_pktmbuf_free(txm);
1025 /* optimize ring usage */
1026 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
1027 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
1028 rte_mbuf_refcnt_read(txm) == 1 &&
1029 RTE_MBUF_DIRECT(txm) &&
1030 txm->nb_segs == 1 &&
1031 rte_pktmbuf_headroom(txm) >= hdr_size &&
1032 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
1033 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
1035 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
1036 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
1039 /* How many main ring entries are needed to this Tx?
1040 * any_layout => number of segments
1042 * default => number of segments + 1
1044 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
1045 need = slots - vq->vq_free_cnt;
1047 /* Positive value indicates it need free vring descriptors */
1048 if (unlikely(need > 0)) {
1049 nb_used = VIRTQUEUE_NUSED(vq);
1051 need = RTE_MIN(need, (int)nb_used);
1053 virtio_xmit_cleanup(vq, need);
1054 need = slots - vq->vq_free_cnt;
1055 if (unlikely(need > 0)) {
1057 "No free tx descriptors to transmit");
1062 /* Enqueue Packet buffers */
1063 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect, can_push);
1065 txvq->stats.bytes += txm->pkt_len;
1066 virtio_update_packet_stats(&txvq->stats, txm);
1069 txvq->stats.packets += nb_tx;
1071 if (likely(nb_tx)) {
1072 vq_update_avail_idx(vq);
1074 if (unlikely(virtqueue_kick_prepare(vq))) {
1075 virtqueue_notify(vq);
1076 PMD_TX_LOG(DEBUG, "Notified backend after xmit");