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"
34 #include "virtio_ring.h"
36 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
37 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
39 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0)
43 virtio_dev_rx_queue_done(void *rxq, uint16_t offset)
45 struct virtnet_rx *rxvq = rxq;
46 struct virtqueue *vq = rxvq->vq;
48 return VIRTQUEUE_NUSED(vq) >= offset;
52 vq_ring_free_inorder(struct virtqueue *vq, uint16_t desc_idx, uint16_t num)
54 vq->vq_free_cnt += num;
55 vq->vq_desc_tail_idx = desc_idx & (vq->vq_nentries - 1);
59 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
61 struct vring_desc *dp, *dp_tail;
62 struct vq_desc_extra *dxp;
63 uint16_t desc_idx_last = desc_idx;
65 dp = &vq->vq_split.ring.desc[desc_idx];
66 dxp = &vq->vq_descx[desc_idx];
67 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
68 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
69 while (dp->flags & VRING_DESC_F_NEXT) {
70 desc_idx_last = dp->next;
71 dp = &vq->vq_split.ring.desc[dp->next];
77 * We must append the existing free chain, if any, to the end of
78 * newly freed chain. If the virtqueue was completely used, then
79 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
81 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
82 vq->vq_desc_head_idx = desc_idx;
84 dp_tail = &vq->vq_split.ring.desc[vq->vq_desc_tail_idx];
85 dp_tail->next = desc_idx;
88 vq->vq_desc_tail_idx = desc_idx_last;
89 dp->next = VQ_RING_DESC_CHAIN_END;
93 vq_ring_free_id_packed(struct virtqueue *vq, uint16_t id)
95 struct vq_desc_extra *dxp;
97 dxp = &vq->vq_descx[id];
98 vq->vq_free_cnt += dxp->ndescs;
100 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END)
101 vq->vq_desc_head_idx = id;
103 vq->vq_descx[vq->vq_desc_tail_idx].next = id;
105 vq->vq_desc_tail_idx = id;
106 dxp->next = VQ_RING_DESC_CHAIN_END;
110 virtqueue_dequeue_burst_rx_packed(struct virtqueue *vq,
111 struct rte_mbuf **rx_pkts,
115 struct rte_mbuf *cookie;
118 struct vring_packed_desc *desc;
121 desc = vq->vq_packed.ring.desc;
123 for (i = 0; i < num; i++) {
124 used_idx = vq->vq_used_cons_idx;
125 if (!desc_is_used(&desc[used_idx], vq))
127 virtio_rmb(vq->hw->weak_barriers);
128 len[i] = desc[used_idx].len;
129 id = desc[used_idx].id;
130 cookie = (struct rte_mbuf *)vq->vq_descx[id].cookie;
131 if (unlikely(cookie == NULL)) {
132 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
133 vq->vq_used_cons_idx);
136 rte_prefetch0(cookie);
137 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
141 vq->vq_used_cons_idx++;
142 if (vq->vq_used_cons_idx >= vq->vq_nentries) {
143 vq->vq_used_cons_idx -= vq->vq_nentries;
144 vq->vq_packed.used_wrap_counter ^= 1;
152 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
153 uint32_t *len, uint16_t num)
155 struct vring_used_elem *uep;
156 struct rte_mbuf *cookie;
157 uint16_t used_idx, desc_idx;
160 /* Caller does the check */
161 for (i = 0; i < num ; i++) {
162 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
163 uep = &vq->vq_split.ring.used->ring[used_idx];
164 desc_idx = (uint16_t) uep->id;
166 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
168 if (unlikely(cookie == NULL)) {
169 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
170 vq->vq_used_cons_idx);
174 rte_prefetch0(cookie);
175 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
177 vq->vq_used_cons_idx++;
178 vq_ring_free_chain(vq, desc_idx);
179 vq->vq_descx[desc_idx].cookie = NULL;
186 virtqueue_dequeue_rx_inorder(struct virtqueue *vq,
187 struct rte_mbuf **rx_pkts,
191 struct vring_used_elem *uep;
192 struct rte_mbuf *cookie;
193 uint16_t used_idx = 0;
196 if (unlikely(num == 0))
199 for (i = 0; i < num; i++) {
200 used_idx = vq->vq_used_cons_idx & (vq->vq_nentries - 1);
201 /* Desc idx same as used idx */
202 uep = &vq->vq_split.ring.used->ring[used_idx];
204 cookie = (struct rte_mbuf *)vq->vq_descx[used_idx].cookie;
206 if (unlikely(cookie == NULL)) {
207 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
208 vq->vq_used_cons_idx);
212 rte_prefetch0(cookie);
213 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
215 vq->vq_used_cons_idx++;
216 vq->vq_descx[used_idx].cookie = NULL;
219 vq_ring_free_inorder(vq, used_idx, i);
223 #ifndef DEFAULT_TX_FREE_THRESH
224 #define DEFAULT_TX_FREE_THRESH 32
228 virtio_xmit_cleanup_inorder_packed(struct virtqueue *vq, int num)
230 uint16_t used_idx, id, curr_id, free_cnt = 0;
231 uint16_t size = vq->vq_nentries;
232 struct vring_packed_desc *desc = vq->vq_packed.ring.desc;
233 struct vq_desc_extra *dxp;
235 used_idx = vq->vq_used_cons_idx;
236 while (num > 0 && desc_is_used(&desc[used_idx], vq)) {
237 virtio_rmb(vq->hw->weak_barriers);
238 id = desc[used_idx].id;
241 dxp = &vq->vq_descx[used_idx];
242 used_idx += dxp->ndescs;
243 free_cnt += dxp->ndescs;
245 if (used_idx >= size) {
247 vq->vq_packed.used_wrap_counter ^= 1;
249 if (dxp->cookie != NULL) {
250 rte_pktmbuf_free(dxp->cookie);
253 } while (curr_id != id);
255 vq->vq_used_cons_idx = used_idx;
256 vq->vq_free_cnt += free_cnt;
260 virtio_xmit_cleanup_normal_packed(struct virtqueue *vq, int num)
262 uint16_t used_idx, id;
263 uint16_t size = vq->vq_nentries;
264 struct vring_packed_desc *desc = vq->vq_packed.ring.desc;
265 struct vq_desc_extra *dxp;
267 used_idx = vq->vq_used_cons_idx;
268 while (num-- && desc_is_used(&desc[used_idx], vq)) {
269 virtio_rmb(vq->hw->weak_barriers);
270 id = desc[used_idx].id;
271 dxp = &vq->vq_descx[id];
272 vq->vq_used_cons_idx += dxp->ndescs;
273 if (vq->vq_used_cons_idx >= size) {
274 vq->vq_used_cons_idx -= size;
275 vq->vq_packed.used_wrap_counter ^= 1;
277 vq_ring_free_id_packed(vq, id);
278 if (dxp->cookie != NULL) {
279 rte_pktmbuf_free(dxp->cookie);
282 used_idx = vq->vq_used_cons_idx;
286 /* Cleanup from completed transmits. */
288 virtio_xmit_cleanup_packed(struct virtqueue *vq, int num, int in_order)
291 virtio_xmit_cleanup_inorder_packed(vq, num);
293 virtio_xmit_cleanup_normal_packed(vq, num);
297 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
299 uint16_t i, used_idx, desc_idx;
300 for (i = 0; i < num; i++) {
301 struct vring_used_elem *uep;
302 struct vq_desc_extra *dxp;
304 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
305 uep = &vq->vq_split.ring.used->ring[used_idx];
307 desc_idx = (uint16_t) uep->id;
308 dxp = &vq->vq_descx[desc_idx];
309 vq->vq_used_cons_idx++;
310 vq_ring_free_chain(vq, desc_idx);
312 if (dxp->cookie != NULL) {
313 rte_pktmbuf_free(dxp->cookie);
319 /* Cleanup from completed inorder transmits. */
321 virtio_xmit_cleanup_inorder(struct virtqueue *vq, uint16_t num)
323 uint16_t i, idx = vq->vq_used_cons_idx;
324 int16_t free_cnt = 0;
325 struct vq_desc_extra *dxp = NULL;
327 if (unlikely(num == 0))
330 for (i = 0; i < num; i++) {
331 dxp = &vq->vq_descx[idx++ & (vq->vq_nentries - 1)];
332 free_cnt += dxp->ndescs;
333 if (dxp->cookie != NULL) {
334 rte_pktmbuf_free(dxp->cookie);
339 vq->vq_free_cnt += free_cnt;
340 vq->vq_used_cons_idx = idx;
344 virtqueue_enqueue_refill_inorder(struct virtqueue *vq,
345 struct rte_mbuf **cookies,
348 struct vq_desc_extra *dxp;
349 struct virtio_hw *hw = vq->hw;
350 struct vring_desc *start_dp;
351 uint16_t head_idx, idx, i = 0;
353 if (unlikely(vq->vq_free_cnt == 0))
355 if (unlikely(vq->vq_free_cnt < num))
358 head_idx = vq->vq_desc_head_idx & (vq->vq_nentries - 1);
359 start_dp = vq->vq_split.ring.desc;
362 idx = head_idx & (vq->vq_nentries - 1);
363 dxp = &vq->vq_descx[idx];
364 dxp->cookie = (void *)cookies[i];
368 VIRTIO_MBUF_ADDR(cookies[i], vq) +
369 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
371 cookies[i]->buf_len -
372 RTE_PKTMBUF_HEADROOM +
374 start_dp[idx].flags = VRING_DESC_F_WRITE;
376 vq_update_avail_ring(vq, idx);
381 vq->vq_desc_head_idx += num;
382 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
387 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf **cookie,
390 struct vq_desc_extra *dxp;
391 struct virtio_hw *hw = vq->hw;
392 struct vring_desc *start_dp = vq->vq_split.ring.desc;
395 if (unlikely(vq->vq_free_cnt == 0))
397 if (unlikely(vq->vq_free_cnt < num))
400 if (unlikely(vq->vq_desc_head_idx >= vq->vq_nentries))
403 for (i = 0; i < num; i++) {
404 idx = vq->vq_desc_head_idx;
405 dxp = &vq->vq_descx[idx];
406 dxp->cookie = (void *)cookie[i];
410 VIRTIO_MBUF_ADDR(cookie[i], vq) +
411 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
413 cookie[i]->buf_len - RTE_PKTMBUF_HEADROOM +
415 start_dp[idx].flags = VRING_DESC_F_WRITE;
416 vq->vq_desc_head_idx = start_dp[idx].next;
417 vq_update_avail_ring(vq, idx);
418 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END) {
419 vq->vq_desc_tail_idx = vq->vq_desc_head_idx;
424 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
430 virtqueue_enqueue_recv_refill_packed(struct virtqueue *vq,
431 struct rte_mbuf **cookie, uint16_t num)
433 struct vring_packed_desc *start_dp = vq->vq_packed.ring.desc;
434 uint16_t flags = vq->vq_packed.cached_flags;
435 struct virtio_hw *hw = vq->hw;
436 struct vq_desc_extra *dxp;
440 if (unlikely(vq->vq_free_cnt == 0))
442 if (unlikely(vq->vq_free_cnt < num))
445 for (i = 0; i < num; i++) {
446 idx = vq->vq_avail_idx;
447 dxp = &vq->vq_descx[idx];
448 dxp->cookie = (void *)cookie[i];
451 start_dp[idx].addr = VIRTIO_MBUF_ADDR(cookie[i], vq) +
452 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
453 start_dp[idx].len = cookie[i]->buf_len - RTE_PKTMBUF_HEADROOM
454 + hw->vtnet_hdr_size;
456 vq->vq_desc_head_idx = dxp->next;
457 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
458 vq->vq_desc_tail_idx = vq->vq_desc_head_idx;
459 virtio_wmb(hw->weak_barriers);
460 start_dp[idx].flags = flags;
461 if (++vq->vq_avail_idx >= vq->vq_nentries) {
462 vq->vq_avail_idx -= vq->vq_nentries;
463 vq->vq_packed.cached_flags ^=
464 VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
465 flags = vq->vq_packed.cached_flags;
468 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
472 /* When doing TSO, the IP length is not included in the pseudo header
473 * checksum of the packet given to the PMD, but for virtio it is
477 virtio_tso_fix_cksum(struct rte_mbuf *m)
479 /* common case: header is not fragmented */
480 if (likely(rte_pktmbuf_data_len(m) >= m->l2_len + m->l3_len +
482 struct ipv4_hdr *iph;
483 struct ipv6_hdr *ip6h;
485 uint16_t prev_cksum, new_cksum, ip_len, ip_paylen;
488 iph = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *, m->l2_len);
489 th = RTE_PTR_ADD(iph, m->l3_len);
490 if ((iph->version_ihl >> 4) == 4) {
491 iph->hdr_checksum = 0;
492 iph->hdr_checksum = rte_ipv4_cksum(iph);
493 ip_len = iph->total_length;
494 ip_paylen = rte_cpu_to_be_16(rte_be_to_cpu_16(ip_len) -
497 ip6h = (struct ipv6_hdr *)iph;
498 ip_paylen = ip6h->payload_len;
501 /* calculate the new phdr checksum not including ip_paylen */
502 prev_cksum = th->cksum;
505 tmp = (tmp & 0xffff) + (tmp >> 16);
508 /* replace it in the packet */
509 th->cksum = new_cksum;
514 /* avoid write operation when necessary, to lessen cache issues */
515 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
516 if ((var) != (val)) \
520 #define virtqueue_clear_net_hdr(_hdr) do { \
521 ASSIGN_UNLESS_EQUAL((_hdr)->csum_start, 0); \
522 ASSIGN_UNLESS_EQUAL((_hdr)->csum_offset, 0); \
523 ASSIGN_UNLESS_EQUAL((_hdr)->flags, 0); \
524 ASSIGN_UNLESS_EQUAL((_hdr)->gso_type, 0); \
525 ASSIGN_UNLESS_EQUAL((_hdr)->gso_size, 0); \
526 ASSIGN_UNLESS_EQUAL((_hdr)->hdr_len, 0); \
530 virtqueue_xmit_offload(struct virtio_net_hdr *hdr,
531 struct rte_mbuf *cookie,
535 if (cookie->ol_flags & PKT_TX_TCP_SEG)
536 cookie->ol_flags |= PKT_TX_TCP_CKSUM;
538 switch (cookie->ol_flags & PKT_TX_L4_MASK) {
539 case PKT_TX_UDP_CKSUM:
540 hdr->csum_start = cookie->l2_len + cookie->l3_len;
541 hdr->csum_offset = offsetof(struct udp_hdr,
543 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
546 case PKT_TX_TCP_CKSUM:
547 hdr->csum_start = cookie->l2_len + cookie->l3_len;
548 hdr->csum_offset = offsetof(struct tcp_hdr, cksum);
549 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
553 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
554 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
555 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
559 /* TCP Segmentation Offload */
560 if (cookie->ol_flags & PKT_TX_TCP_SEG) {
561 virtio_tso_fix_cksum(cookie);
562 hdr->gso_type = (cookie->ol_flags & PKT_TX_IPV6) ?
563 VIRTIO_NET_HDR_GSO_TCPV6 :
564 VIRTIO_NET_HDR_GSO_TCPV4;
565 hdr->gso_size = cookie->tso_segsz;
571 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
572 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
573 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
579 virtqueue_enqueue_xmit_inorder(struct virtnet_tx *txvq,
580 struct rte_mbuf **cookies,
583 struct vq_desc_extra *dxp;
584 struct virtqueue *vq = txvq->vq;
585 struct vring_desc *start_dp;
586 struct virtio_net_hdr *hdr;
588 uint16_t head_size = vq->hw->vtnet_hdr_size;
591 idx = vq->vq_desc_head_idx;
592 start_dp = vq->vq_split.ring.desc;
595 idx = idx & (vq->vq_nentries - 1);
596 dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
597 dxp->cookie = (void *)cookies[i];
600 hdr = (struct virtio_net_hdr *)
601 rte_pktmbuf_prepend(cookies[i], head_size);
602 cookies[i]->pkt_len -= head_size;
604 /* if offload disabled, hdr is not zeroed yet, do it now */
605 if (!vq->hw->has_tx_offload)
606 virtqueue_clear_net_hdr(hdr);
608 virtqueue_xmit_offload(hdr, cookies[i], true);
610 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookies[i], vq);
611 start_dp[idx].len = cookies[i]->data_len;
612 start_dp[idx].flags = 0;
614 vq_update_avail_ring(vq, idx);
620 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
621 vq->vq_desc_head_idx = idx & (vq->vq_nentries - 1);
625 virtqueue_enqueue_xmit_packed_fast(struct virtnet_tx *txvq,
626 struct rte_mbuf *cookie,
629 struct virtqueue *vq = txvq->vq;
630 struct vring_packed_desc *dp;
631 struct vq_desc_extra *dxp;
632 uint16_t idx, id, flags;
633 uint16_t head_size = vq->hw->vtnet_hdr_size;
634 struct virtio_net_hdr *hdr;
636 id = in_order ? vq->vq_avail_idx : vq->vq_desc_head_idx;
637 idx = vq->vq_avail_idx;
638 dp = &vq->vq_packed.ring.desc[idx];
640 dxp = &vq->vq_descx[id];
642 dxp->cookie = cookie;
644 flags = vq->vq_packed.cached_flags;
646 /* prepend cannot fail, checked by caller */
647 hdr = (struct virtio_net_hdr *)
648 rte_pktmbuf_prepend(cookie, head_size);
649 cookie->pkt_len -= head_size;
651 /* if offload disabled, hdr is not zeroed yet, do it now */
652 if (!vq->hw->has_tx_offload)
653 virtqueue_clear_net_hdr(hdr);
655 virtqueue_xmit_offload(hdr, cookie, true);
657 dp->addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
658 dp->len = cookie->data_len;
661 if (++vq->vq_avail_idx >= vq->vq_nentries) {
662 vq->vq_avail_idx -= vq->vq_nentries;
663 vq->vq_packed.cached_flags ^=
664 VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
670 vq->vq_desc_head_idx = dxp->next;
671 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
672 vq->vq_desc_tail_idx = VQ_RING_DESC_CHAIN_END;
675 virtio_wmb(vq->hw->weak_barriers);
680 virtqueue_enqueue_xmit_packed(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
681 uint16_t needed, int can_push, int in_order)
683 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
684 struct vq_desc_extra *dxp;
685 struct virtqueue *vq = txvq->vq;
686 struct vring_packed_desc *start_dp, *head_dp;
687 uint16_t idx, id, head_idx, head_flags;
688 uint16_t head_size = vq->hw->vtnet_hdr_size;
689 struct virtio_net_hdr *hdr;
692 id = in_order ? vq->vq_avail_idx : vq->vq_desc_head_idx;
694 dxp = &vq->vq_descx[id];
695 dxp->ndescs = needed;
696 dxp->cookie = cookie;
698 head_idx = vq->vq_avail_idx;
701 start_dp = vq->vq_packed.ring.desc;
703 head_dp = &vq->vq_packed.ring.desc[idx];
704 head_flags = cookie->next ? VRING_DESC_F_NEXT : 0;
705 head_flags |= vq->vq_packed.cached_flags;
708 /* prepend cannot fail, checked by caller */
709 hdr = (struct virtio_net_hdr *)
710 rte_pktmbuf_prepend(cookie, head_size);
711 /* rte_pktmbuf_prepend() counts the hdr size to the pkt length,
712 * which is wrong. Below subtract restores correct pkt size.
714 cookie->pkt_len -= head_size;
716 /* if offload disabled, it is not zeroed below, do it now */
717 if (!vq->hw->has_tx_offload)
718 virtqueue_clear_net_hdr(hdr);
720 /* setup first tx ring slot to point to header
721 * stored in reserved region.
723 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
724 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
725 start_dp[idx].len = vq->hw->vtnet_hdr_size;
726 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
728 if (idx >= vq->vq_nentries) {
729 idx -= vq->vq_nentries;
730 vq->vq_packed.cached_flags ^=
731 VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
735 virtqueue_xmit_offload(hdr, cookie, vq->hw->has_tx_offload);
740 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
741 start_dp[idx].len = cookie->data_len;
742 if (likely(idx != head_idx)) {
743 flags = cookie->next ? VRING_DESC_F_NEXT : 0;
744 flags |= vq->vq_packed.cached_flags;
745 start_dp[idx].flags = flags;
749 if (idx >= vq->vq_nentries) {
750 idx -= vq->vq_nentries;
751 vq->vq_packed.cached_flags ^=
752 VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
754 } while ((cookie = cookie->next) != NULL);
756 start_dp[prev].id = id;
758 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
759 vq->vq_avail_idx = idx;
762 vq->vq_desc_head_idx = dxp->next;
763 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
764 vq->vq_desc_tail_idx = VQ_RING_DESC_CHAIN_END;
767 virtio_wmb(vq->hw->weak_barriers);
768 head_dp->flags = head_flags;
772 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
773 uint16_t needed, int use_indirect, int can_push,
776 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
777 struct vq_desc_extra *dxp;
778 struct virtqueue *vq = txvq->vq;
779 struct vring_desc *start_dp;
780 uint16_t seg_num = cookie->nb_segs;
781 uint16_t head_idx, idx;
782 uint16_t head_size = vq->hw->vtnet_hdr_size;
783 struct virtio_net_hdr *hdr;
785 head_idx = vq->vq_desc_head_idx;
788 dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
790 dxp = &vq->vq_descx[idx];
791 dxp->cookie = (void *)cookie;
792 dxp->ndescs = needed;
794 start_dp = vq->vq_split.ring.desc;
797 /* prepend cannot fail, checked by caller */
798 hdr = (struct virtio_net_hdr *)
799 rte_pktmbuf_prepend(cookie, head_size);
800 /* rte_pktmbuf_prepend() counts the hdr size to the pkt length,
801 * which is wrong. Below subtract restores correct pkt size.
803 cookie->pkt_len -= head_size;
805 /* if offload disabled, it is not zeroed below, do it now */
806 if (!vq->hw->has_tx_offload)
807 virtqueue_clear_net_hdr(hdr);
808 } else if (use_indirect) {
809 /* setup tx ring slot to point to indirect
810 * descriptor list stored in reserved region.
812 * the first slot in indirect ring is already preset
813 * to point to the header in reserved region
815 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
816 RTE_PTR_DIFF(&txr[idx].tx_indir, txr);
817 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
818 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
819 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
821 /* loop below will fill in rest of the indirect elements */
822 start_dp = txr[idx].tx_indir;
825 /* setup first tx ring slot to point to header
826 * stored in reserved region.
828 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
829 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
830 start_dp[idx].len = vq->hw->vtnet_hdr_size;
831 start_dp[idx].flags = VRING_DESC_F_NEXT;
832 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
834 idx = start_dp[idx].next;
837 virtqueue_xmit_offload(hdr, cookie, vq->hw->has_tx_offload);
840 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
841 start_dp[idx].len = cookie->data_len;
842 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
843 idx = start_dp[idx].next;
844 } while ((cookie = cookie->next) != NULL);
847 idx = vq->vq_split.ring.desc[head_idx].next;
849 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
851 vq->vq_desc_head_idx = idx;
852 vq_update_avail_ring(vq, head_idx);
855 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
856 vq->vq_desc_tail_idx = idx;
861 virtio_dev_cq_start(struct rte_eth_dev *dev)
863 struct virtio_hw *hw = dev->data->dev_private;
865 if (hw->cvq && hw->cvq->vq) {
866 rte_spinlock_init(&hw->cvq->lock);
867 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
872 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
875 unsigned int socket_id __rte_unused,
876 const struct rte_eth_rxconf *rx_conf __rte_unused,
877 struct rte_mempool *mp)
879 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
880 struct virtio_hw *hw = dev->data->dev_private;
881 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
882 struct virtnet_rx *rxvq;
884 PMD_INIT_FUNC_TRACE();
886 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
887 nb_desc = vq->vq_nentries;
888 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
891 rxvq->queue_id = queue_idx;
893 if (rxvq->mpool == NULL) {
894 rte_exit(EXIT_FAILURE,
895 "Cannot allocate mbufs for rx virtqueue");
898 dev->data->rx_queues[queue_idx] = rxvq;
904 virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
906 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
907 struct virtio_hw *hw = dev->data->dev_private;
908 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
909 struct virtnet_rx *rxvq = &vq->rxq;
914 PMD_INIT_FUNC_TRACE();
916 /* Allocate blank mbufs for the each rx descriptor */
919 if (hw->use_simple_rx) {
920 for (desc_idx = 0; desc_idx < vq->vq_nentries;
922 vq->vq_split.ring.avail->ring[desc_idx] = desc_idx;
923 vq->vq_split.ring.desc[desc_idx].flags =
927 virtio_rxq_vec_setup(rxvq);
930 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
931 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
933 vq->sw_ring[vq->vq_nentries + desc_idx] =
937 if (hw->use_simple_rx) {
938 while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
939 virtio_rxq_rearm_vec(rxvq);
940 nbufs += RTE_VIRTIO_VPMD_RX_REARM_THRESH;
942 } else if (hw->use_inorder_rx) {
943 if ((!virtqueue_full(vq))) {
944 uint16_t free_cnt = vq->vq_free_cnt;
945 struct rte_mbuf *pkts[free_cnt];
947 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, pkts,
949 error = virtqueue_enqueue_refill_inorder(vq,
952 if (unlikely(error)) {
953 for (i = 0; i < free_cnt; i++)
954 rte_pktmbuf_free(pkts[i]);
959 vq_update_avail_idx(vq);
962 while (!virtqueue_full(vq)) {
963 m = rte_mbuf_raw_alloc(rxvq->mpool);
967 /* Enqueue allocated buffers */
968 if (vtpci_packed_queue(vq->hw))
969 error = virtqueue_enqueue_recv_refill_packed(vq,
972 error = virtqueue_enqueue_recv_refill(vq,
981 if (!vtpci_packed_queue(vq->hw))
982 vq_update_avail_idx(vq);
985 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
993 * struct rte_eth_dev *dev: Used to update dev
994 * uint16_t nb_desc: Defaults to values read from config space
995 * unsigned int socket_id: Used to allocate memzone
996 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
997 * uint16_t queue_idx: Just used as an index in dev txq list
1000 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
1003 unsigned int socket_id __rte_unused,
1004 const struct rte_eth_txconf *tx_conf)
1006 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1007 struct virtio_hw *hw = dev->data->dev_private;
1008 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1009 struct virtnet_tx *txvq;
1010 uint16_t tx_free_thresh;
1012 PMD_INIT_FUNC_TRACE();
1014 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
1015 nb_desc = vq->vq_nentries;
1016 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
1019 txvq->queue_id = queue_idx;
1021 tx_free_thresh = tx_conf->tx_free_thresh;
1022 if (tx_free_thresh == 0)
1024 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
1026 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
1027 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
1028 "number of TX entries minus 3 (%u)."
1029 " (tx_free_thresh=%u port=%u queue=%u)\n",
1030 vq->vq_nentries - 3,
1031 tx_free_thresh, dev->data->port_id, queue_idx);
1035 vq->vq_free_thresh = tx_free_thresh;
1037 dev->data->tx_queues[queue_idx] = txvq;
1042 virtio_dev_tx_queue_setup_finish(struct rte_eth_dev *dev,
1045 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1046 struct virtio_hw *hw = dev->data->dev_private;
1047 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1049 PMD_INIT_FUNC_TRACE();
1051 if (!vtpci_packed_queue(hw)) {
1052 if (hw->use_inorder_tx)
1053 vq->vq_split.ring.desc[vq->vq_nentries - 1].next = 0;
1062 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
1066 * Requeue the discarded mbuf. This should always be
1067 * successful since it was just dequeued.
1069 if (vtpci_packed_queue(vq->hw))
1070 error = virtqueue_enqueue_recv_refill_packed(vq, &m, 1);
1072 error = virtqueue_enqueue_recv_refill(vq, &m, 1);
1074 if (unlikely(error)) {
1075 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
1076 rte_pktmbuf_free(m);
1081 virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)
1085 error = virtqueue_enqueue_refill_inorder(vq, &m, 1);
1086 if (unlikely(error)) {
1087 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
1088 rte_pktmbuf_free(m);
1093 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
1095 uint32_t s = mbuf->pkt_len;
1096 struct ether_addr *ea;
1101 stats->size_bins[1]++;
1102 } else if (s > 64 && s < 1024) {
1105 /* count zeros, and offset into correct bin */
1106 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
1107 stats->size_bins[bin]++;
1110 stats->size_bins[0]++;
1112 stats->size_bins[6]++;
1114 stats->size_bins[7]++;
1117 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
1118 if (is_multicast_ether_addr(ea)) {
1119 if (is_broadcast_ether_addr(ea))
1127 virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
1129 VIRTIO_DUMP_PACKET(m, m->data_len);
1131 virtio_update_packet_stats(&rxvq->stats, m);
1134 /* Optionally fill offload information in structure */
1136 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
1138 struct rte_net_hdr_lens hdr_lens;
1139 uint32_t hdrlen, ptype;
1140 int l4_supported = 0;
1143 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1146 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
1148 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
1149 m->packet_type = ptype;
1150 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
1151 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
1152 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
1155 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1156 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
1157 if (hdr->csum_start <= hdrlen && l4_supported) {
1158 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
1160 /* Unknown proto or tunnel, do sw cksum. We can assume
1161 * the cksum field is in the first segment since the
1162 * buffers we provided to the host are large enough.
1163 * In case of SCTP, this will be wrong since it's a CRC
1164 * but there's nothing we can do.
1166 uint16_t csum = 0, off;
1168 rte_raw_cksum_mbuf(m, hdr->csum_start,
1169 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
1171 if (likely(csum != 0xffff))
1173 off = hdr->csum_offset + hdr->csum_start;
1174 if (rte_pktmbuf_data_len(m) >= off + 1)
1175 *rte_pktmbuf_mtod_offset(m, uint16_t *,
1178 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
1179 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
1182 /* GSO request, save required information in mbuf */
1183 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1184 /* Check unsupported modes */
1185 if ((hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) ||
1186 (hdr->gso_size == 0)) {
1190 /* Update mss lengthes in mbuf */
1191 m->tso_segsz = hdr->gso_size;
1192 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1193 case VIRTIO_NET_HDR_GSO_TCPV4:
1194 case VIRTIO_NET_HDR_GSO_TCPV6:
1195 m->ol_flags |= PKT_RX_LRO | \
1196 PKT_RX_L4_CKSUM_NONE;
1206 #define VIRTIO_MBUF_BURST_SZ 64
1207 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
1209 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1211 struct virtnet_rx *rxvq = rx_queue;
1212 struct virtqueue *vq = rxvq->vq;
1213 struct virtio_hw *hw = vq->hw;
1214 struct rte_mbuf *rxm;
1215 uint16_t nb_used, num, nb_rx;
1216 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1217 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1219 uint32_t i, nb_enqueued;
1221 struct virtio_net_hdr *hdr;
1224 if (unlikely(hw->started == 0))
1227 nb_used = VIRTQUEUE_NUSED(vq);
1229 virtio_rmb(hw->weak_barriers);
1231 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1232 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1233 num = VIRTIO_MBUF_BURST_SZ;
1234 if (likely(num > DESC_PER_CACHELINE))
1235 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1237 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1238 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
1241 hdr_size = hw->vtnet_hdr_size;
1243 for (i = 0; i < num ; i++) {
1246 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1248 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1249 PMD_RX_LOG(ERR, "Packet drop");
1251 virtio_discard_rxbuf(vq, rxm);
1252 rxvq->stats.errors++;
1256 rxm->port = rxvq->port_id;
1257 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1261 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1262 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1264 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1265 RTE_PKTMBUF_HEADROOM - hdr_size);
1268 rte_vlan_strip(rxm);
1270 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1271 virtio_discard_rxbuf(vq, rxm);
1272 rxvq->stats.errors++;
1276 virtio_rx_stats_updated(rxvq, rxm);
1278 rx_pkts[nb_rx++] = rxm;
1281 rxvq->stats.packets += nb_rx;
1283 /* Allocate new mbuf for the used descriptor */
1284 if (likely(!virtqueue_full(vq))) {
1285 uint16_t free_cnt = vq->vq_free_cnt;
1286 struct rte_mbuf *new_pkts[free_cnt];
1288 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1290 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1292 if (unlikely(error)) {
1293 for (i = 0; i < free_cnt; i++)
1294 rte_pktmbuf_free(new_pkts[i]);
1296 nb_enqueued += free_cnt;
1298 struct rte_eth_dev *dev =
1299 &rte_eth_devices[rxvq->port_id];
1300 dev->data->rx_mbuf_alloc_failed += free_cnt;
1304 if (likely(nb_enqueued)) {
1305 vq_update_avail_idx(vq);
1307 if (unlikely(virtqueue_kick_prepare(vq))) {
1308 virtqueue_notify(vq);
1309 PMD_RX_LOG(DEBUG, "Notified");
1317 virtio_recv_pkts_packed(void *rx_queue, struct rte_mbuf **rx_pkts,
1320 struct virtnet_rx *rxvq = rx_queue;
1321 struct virtqueue *vq = rxvq->vq;
1322 struct virtio_hw *hw = vq->hw;
1323 struct rte_mbuf *rxm;
1324 uint16_t num, nb_rx;
1325 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1326 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1328 uint32_t i, nb_enqueued;
1330 struct virtio_net_hdr *hdr;
1333 if (unlikely(hw->started == 0))
1336 num = RTE_MIN(VIRTIO_MBUF_BURST_SZ, nb_pkts);
1337 if (likely(num > DESC_PER_CACHELINE))
1338 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1340 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1341 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1344 hdr_size = hw->vtnet_hdr_size;
1346 for (i = 0; i < num; i++) {
1349 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1351 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1352 PMD_RX_LOG(ERR, "Packet drop");
1354 virtio_discard_rxbuf(vq, rxm);
1355 rxvq->stats.errors++;
1359 rxm->port = rxvq->port_id;
1360 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1364 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1365 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1367 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1368 RTE_PKTMBUF_HEADROOM - hdr_size);
1371 rte_vlan_strip(rxm);
1373 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1374 virtio_discard_rxbuf(vq, rxm);
1375 rxvq->stats.errors++;
1379 virtio_rx_stats_updated(rxvq, rxm);
1381 rx_pkts[nb_rx++] = rxm;
1384 rxvq->stats.packets += nb_rx;
1386 /* Allocate new mbuf for the used descriptor */
1387 if (likely(!virtqueue_full(vq))) {
1388 uint16_t free_cnt = vq->vq_free_cnt;
1389 struct rte_mbuf *new_pkts[free_cnt];
1391 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1393 error = virtqueue_enqueue_recv_refill_packed(vq,
1394 new_pkts, free_cnt);
1395 if (unlikely(error)) {
1396 for (i = 0; i < free_cnt; i++)
1397 rte_pktmbuf_free(new_pkts[i]);
1399 nb_enqueued += free_cnt;
1401 struct rte_eth_dev *dev =
1402 &rte_eth_devices[rxvq->port_id];
1403 dev->data->rx_mbuf_alloc_failed += free_cnt;
1407 if (likely(nb_enqueued)) {
1408 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1409 virtqueue_notify(vq);
1410 PMD_RX_LOG(DEBUG, "Notified");
1419 virtio_recv_pkts_inorder(void *rx_queue,
1420 struct rte_mbuf **rx_pkts,
1423 struct virtnet_rx *rxvq = rx_queue;
1424 struct virtqueue *vq = rxvq->vq;
1425 struct virtio_hw *hw = vq->hw;
1426 struct rte_mbuf *rxm;
1427 struct rte_mbuf *prev;
1428 uint16_t nb_used, num, nb_rx;
1429 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1430 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1432 uint32_t nb_enqueued;
1439 if (unlikely(hw->started == 0))
1442 nb_used = VIRTQUEUE_NUSED(vq);
1443 nb_used = RTE_MIN(nb_used, nb_pkts);
1444 nb_used = RTE_MIN(nb_used, VIRTIO_MBUF_BURST_SZ);
1446 virtio_rmb(hw->weak_barriers);
1448 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1453 hdr_size = hw->vtnet_hdr_size;
1455 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len, nb_used);
1457 for (i = 0; i < num; i++) {
1458 struct virtio_net_hdr_mrg_rxbuf *header;
1460 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1461 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1465 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1466 PMD_RX_LOG(ERR, "Packet drop");
1468 virtio_discard_rxbuf_inorder(vq, rxm);
1469 rxvq->stats.errors++;
1473 header = (struct virtio_net_hdr_mrg_rxbuf *)
1474 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1477 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1478 seg_num = header->num_buffers;
1485 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1486 rxm->nb_segs = seg_num;
1489 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1490 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1492 rxm->port = rxvq->port_id;
1494 rx_pkts[nb_rx] = rxm;
1497 if (vq->hw->has_rx_offload &&
1498 virtio_rx_offload(rxm, &header->hdr) < 0) {
1499 virtio_discard_rxbuf_inorder(vq, rxm);
1500 rxvq->stats.errors++;
1505 rte_vlan_strip(rx_pkts[nb_rx]);
1507 seg_res = seg_num - 1;
1509 /* Merge remaining segments */
1510 while (seg_res != 0 && i < (num - 1)) {
1514 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1515 rxm->pkt_len = (uint32_t)(len[i]);
1516 rxm->data_len = (uint16_t)(len[i]);
1518 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1519 rx_pkts[nb_rx]->data_len += (uint16_t)(len[i]);
1529 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1534 /* Last packet still need merge segments */
1535 while (seg_res != 0) {
1536 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1537 VIRTIO_MBUF_BURST_SZ);
1539 prev = rcv_pkts[nb_rx];
1540 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1541 virtio_rmb(hw->weak_barriers);
1542 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len,
1544 uint16_t extra_idx = 0;
1547 while (extra_idx < rcv_cnt) {
1548 rxm = rcv_pkts[extra_idx];
1550 RTE_PKTMBUF_HEADROOM - hdr_size;
1551 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1552 rxm->data_len = (uint16_t)(len[extra_idx]);
1555 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1556 rx_pkts[nb_rx]->data_len += len[extra_idx];
1562 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1567 "No enough segments for packet.");
1568 virtio_discard_rxbuf_inorder(vq, prev);
1569 rxvq->stats.errors++;
1574 rxvq->stats.packets += nb_rx;
1576 /* Allocate new mbuf for the used descriptor */
1578 if (likely(!virtqueue_full(vq))) {
1579 /* free_cnt may include mrg descs */
1580 uint16_t free_cnt = vq->vq_free_cnt;
1581 struct rte_mbuf *new_pkts[free_cnt];
1583 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1584 error = virtqueue_enqueue_refill_inorder(vq, new_pkts,
1586 if (unlikely(error)) {
1587 for (i = 0; i < free_cnt; i++)
1588 rte_pktmbuf_free(new_pkts[i]);
1590 nb_enqueued += free_cnt;
1592 struct rte_eth_dev *dev =
1593 &rte_eth_devices[rxvq->port_id];
1594 dev->data->rx_mbuf_alloc_failed += free_cnt;
1598 if (likely(nb_enqueued)) {
1599 vq_update_avail_idx(vq);
1601 if (unlikely(virtqueue_kick_prepare(vq))) {
1602 virtqueue_notify(vq);
1603 PMD_RX_LOG(DEBUG, "Notified");
1611 virtio_recv_mergeable_pkts(void *rx_queue,
1612 struct rte_mbuf **rx_pkts,
1615 struct virtnet_rx *rxvq = rx_queue;
1616 struct virtqueue *vq = rxvq->vq;
1617 struct virtio_hw *hw = vq->hw;
1618 struct rte_mbuf *rxm;
1619 struct rte_mbuf *prev;
1620 uint16_t nb_used, num, nb_rx = 0;
1621 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1622 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1624 uint32_t nb_enqueued = 0;
1625 uint32_t seg_num = 0;
1626 uint32_t seg_res = 0;
1627 uint32_t hdr_size = hw->vtnet_hdr_size;
1630 if (unlikely(hw->started == 0))
1633 nb_used = VIRTQUEUE_NUSED(vq);
1635 virtio_rmb(hw->weak_barriers);
1637 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1639 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1640 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1641 num = VIRTIO_MBUF_BURST_SZ;
1642 if (likely(num > DESC_PER_CACHELINE))
1643 num = num - ((vq->vq_used_cons_idx + num) %
1644 DESC_PER_CACHELINE);
1647 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1649 for (i = 0; i < num; i++) {
1650 struct virtio_net_hdr_mrg_rxbuf *header;
1652 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1653 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1657 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1658 PMD_RX_LOG(ERR, "Packet drop");
1660 virtio_discard_rxbuf(vq, rxm);
1661 rxvq->stats.errors++;
1665 header = (struct virtio_net_hdr_mrg_rxbuf *)
1666 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1668 seg_num = header->num_buffers;
1672 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1673 rxm->nb_segs = seg_num;
1676 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1677 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1679 rxm->port = rxvq->port_id;
1681 rx_pkts[nb_rx] = rxm;
1684 if (hw->has_rx_offload &&
1685 virtio_rx_offload(rxm, &header->hdr) < 0) {
1686 virtio_discard_rxbuf(vq, rxm);
1687 rxvq->stats.errors++;
1692 rte_vlan_strip(rx_pkts[nb_rx]);
1694 seg_res = seg_num - 1;
1696 /* Merge remaining segments */
1697 while (seg_res != 0 && i < (num - 1)) {
1701 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1702 rxm->pkt_len = (uint32_t)(len[i]);
1703 rxm->data_len = (uint16_t)(len[i]);
1705 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1706 rx_pkts[nb_rx]->data_len += (uint16_t)(len[i]);
1716 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1721 /* Last packet still need merge segments */
1722 while (seg_res != 0) {
1723 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1724 VIRTIO_MBUF_BURST_SZ);
1726 prev = rcv_pkts[nb_rx];
1727 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1728 virtio_rmb(hw->weak_barriers);
1729 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len,
1731 uint16_t extra_idx = 0;
1734 while (extra_idx < rcv_cnt) {
1735 rxm = rcv_pkts[extra_idx];
1737 RTE_PKTMBUF_HEADROOM - hdr_size;
1738 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1739 rxm->data_len = (uint16_t)(len[extra_idx]);
1742 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1743 rx_pkts[nb_rx]->data_len += len[extra_idx];
1749 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1754 "No enough segments for packet.");
1755 virtio_discard_rxbuf(vq, prev);
1756 rxvq->stats.errors++;
1761 rxvq->stats.packets += nb_rx;
1763 /* Allocate new mbuf for the used descriptor */
1764 if (likely(!virtqueue_full(vq))) {
1765 /* free_cnt may include mrg descs */
1766 uint16_t free_cnt = vq->vq_free_cnt;
1767 struct rte_mbuf *new_pkts[free_cnt];
1769 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1770 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1772 if (unlikely(error)) {
1773 for (i = 0; i < free_cnt; i++)
1774 rte_pktmbuf_free(new_pkts[i]);
1776 nb_enqueued += free_cnt;
1778 struct rte_eth_dev *dev =
1779 &rte_eth_devices[rxvq->port_id];
1780 dev->data->rx_mbuf_alloc_failed += free_cnt;
1784 if (likely(nb_enqueued)) {
1785 vq_update_avail_idx(vq);
1787 if (unlikely(virtqueue_kick_prepare(vq))) {
1788 virtqueue_notify(vq);
1789 PMD_RX_LOG(DEBUG, "Notified");
1797 virtio_recv_mergeable_pkts_packed(void *rx_queue,
1798 struct rte_mbuf **rx_pkts,
1801 struct virtnet_rx *rxvq = rx_queue;
1802 struct virtqueue *vq = rxvq->vq;
1803 struct virtio_hw *hw = vq->hw;
1804 struct rte_mbuf *rxm;
1805 struct rte_mbuf *prev = NULL;
1806 uint16_t num, nb_rx = 0;
1807 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1808 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1809 uint32_t nb_enqueued = 0;
1810 uint32_t seg_num = 0;
1811 uint32_t seg_res = 0;
1812 uint32_t hdr_size = hw->vtnet_hdr_size;
1816 if (unlikely(hw->started == 0))
1821 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1822 num = VIRTIO_MBUF_BURST_SZ;
1823 if (likely(num > DESC_PER_CACHELINE))
1824 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1826 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1828 for (i = 0; i < num; i++) {
1829 struct virtio_net_hdr_mrg_rxbuf *header;
1831 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1832 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1836 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1837 PMD_RX_LOG(ERR, "Packet drop");
1839 virtio_discard_rxbuf(vq, rxm);
1840 rxvq->stats.errors++;
1844 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)
1845 rxm->buf_addr + RTE_PKTMBUF_HEADROOM - hdr_size);
1846 seg_num = header->num_buffers;
1851 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1852 rxm->nb_segs = seg_num;
1855 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1856 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1858 rxm->port = rxvq->port_id;
1859 rx_pkts[nb_rx] = rxm;
1862 if (hw->has_rx_offload &&
1863 virtio_rx_offload(rxm, &header->hdr) < 0) {
1864 virtio_discard_rxbuf(vq, rxm);
1865 rxvq->stats.errors++;
1870 rte_vlan_strip(rx_pkts[nb_rx]);
1872 seg_res = seg_num - 1;
1874 /* Merge remaining segments */
1875 while (seg_res != 0 && i < (num - 1)) {
1879 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1880 rxm->pkt_len = (uint32_t)(len[i]);
1881 rxm->data_len = (uint16_t)(len[i]);
1883 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1884 rx_pkts[nb_rx]->data_len += (uint16_t)(len[i]);
1894 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1899 /* Last packet still need merge segments */
1900 while (seg_res != 0) {
1901 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1902 VIRTIO_MBUF_BURST_SZ);
1903 if (likely(vq->vq_free_cnt >= rcv_cnt)) {
1904 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts,
1906 uint16_t extra_idx = 0;
1910 while (extra_idx < rcv_cnt) {
1911 rxm = rcv_pkts[extra_idx];
1914 RTE_PKTMBUF_HEADROOM - hdr_size;
1915 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1916 rxm->data_len = (uint16_t)(len[extra_idx]);
1920 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1921 rx_pkts[nb_rx]->data_len += len[extra_idx];
1926 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1931 "No enough segments for packet.");
1933 virtio_discard_rxbuf(vq, prev);
1934 rxvq->stats.errors++;
1939 rxvq->stats.packets += nb_rx;
1941 /* Allocate new mbuf for the used descriptor */
1942 if (likely(!virtqueue_full(vq))) {
1943 /* free_cnt may include mrg descs */
1944 uint16_t free_cnt = vq->vq_free_cnt;
1945 struct rte_mbuf *new_pkts[free_cnt];
1947 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1948 error = virtqueue_enqueue_recv_refill_packed(vq,
1949 new_pkts, free_cnt);
1950 if (unlikely(error)) {
1951 for (i = 0; i < free_cnt; i++)
1952 rte_pktmbuf_free(new_pkts[i]);
1954 nb_enqueued += free_cnt;
1956 struct rte_eth_dev *dev =
1957 &rte_eth_devices[rxvq->port_id];
1958 dev->data->rx_mbuf_alloc_failed += free_cnt;
1962 if (likely(nb_enqueued)) {
1963 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1964 virtqueue_notify(vq);
1965 PMD_RX_LOG(DEBUG, "Notified");
1973 virtio_xmit_pkts_packed(void *tx_queue, struct rte_mbuf **tx_pkts,
1976 struct virtnet_tx *txvq = tx_queue;
1977 struct virtqueue *vq = txvq->vq;
1978 struct virtio_hw *hw = vq->hw;
1979 uint16_t hdr_size = hw->vtnet_hdr_size;
1981 bool in_order = hw->use_inorder_tx;
1984 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
1987 if (unlikely(nb_pkts < 1))
1990 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
1992 if (nb_pkts > vq->vq_free_cnt)
1993 virtio_xmit_cleanup_packed(vq, nb_pkts - vq->vq_free_cnt,
1996 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1997 struct rte_mbuf *txm = tx_pkts[nb_tx];
1998 int can_push = 0, slots, need;
2000 /* Do VLAN tag insertion */
2001 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
2002 error = rte_vlan_insert(&txm);
2003 if (unlikely(error)) {
2004 rte_pktmbuf_free(txm);
2009 /* optimize ring usage */
2010 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2011 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2012 rte_mbuf_refcnt_read(txm) == 1 &&
2013 RTE_MBUF_DIRECT(txm) &&
2014 txm->nb_segs == 1 &&
2015 rte_pktmbuf_headroom(txm) >= hdr_size &&
2016 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2017 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2020 /* How many main ring entries are needed to this Tx?
2021 * any_layout => number of segments
2022 * default => number of segments + 1
2024 slots = txm->nb_segs + !can_push;
2025 need = slots - vq->vq_free_cnt;
2027 /* Positive value indicates it need free vring descriptors */
2028 if (unlikely(need > 0)) {
2029 virtio_xmit_cleanup_packed(vq, need, in_order);
2030 need = slots - vq->vq_free_cnt;
2031 if (unlikely(need > 0)) {
2033 "No free tx descriptors to transmit");
2038 /* Enqueue Packet buffers */
2040 virtqueue_enqueue_xmit_packed_fast(txvq, txm, in_order);
2042 virtqueue_enqueue_xmit_packed(txvq, txm, slots, 0,
2045 virtio_update_packet_stats(&txvq->stats, txm);
2048 txvq->stats.packets += nb_tx;
2050 if (likely(nb_tx)) {
2051 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
2052 virtqueue_notify(vq);
2053 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2061 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
2063 struct virtnet_tx *txvq = tx_queue;
2064 struct virtqueue *vq = txvq->vq;
2065 struct virtio_hw *hw = vq->hw;
2066 uint16_t hdr_size = hw->vtnet_hdr_size;
2067 uint16_t nb_used, nb_tx = 0;
2070 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2073 if (unlikely(nb_pkts < 1))
2076 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2077 nb_used = VIRTQUEUE_NUSED(vq);
2079 virtio_rmb(hw->weak_barriers);
2080 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2081 virtio_xmit_cleanup(vq, nb_used);
2083 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2084 struct rte_mbuf *txm = tx_pkts[nb_tx];
2085 int can_push = 0, use_indirect = 0, slots, need;
2087 /* Do VLAN tag insertion */
2088 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
2089 error = rte_vlan_insert(&txm);
2090 if (unlikely(error)) {
2091 rte_pktmbuf_free(txm);
2096 /* optimize ring usage */
2097 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2098 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2099 rte_mbuf_refcnt_read(txm) == 1 &&
2100 RTE_MBUF_DIRECT(txm) &&
2101 txm->nb_segs == 1 &&
2102 rte_pktmbuf_headroom(txm) >= hdr_size &&
2103 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2104 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2106 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
2107 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
2110 /* How many main ring entries are needed to this Tx?
2111 * any_layout => number of segments
2113 * default => number of segments + 1
2115 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
2116 need = slots - vq->vq_free_cnt;
2118 /* Positive value indicates it need free vring descriptors */
2119 if (unlikely(need > 0)) {
2120 nb_used = VIRTQUEUE_NUSED(vq);
2121 virtio_rmb(hw->weak_barriers);
2122 need = RTE_MIN(need, (int)nb_used);
2124 virtio_xmit_cleanup(vq, need);
2125 need = slots - vq->vq_free_cnt;
2126 if (unlikely(need > 0)) {
2128 "No free tx descriptors to transmit");
2133 /* Enqueue Packet buffers */
2134 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect,
2137 virtio_update_packet_stats(&txvq->stats, txm);
2140 txvq->stats.packets += nb_tx;
2142 if (likely(nb_tx)) {
2143 vq_update_avail_idx(vq);
2145 if (unlikely(virtqueue_kick_prepare(vq))) {
2146 virtqueue_notify(vq);
2147 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2155 virtio_xmit_pkts_inorder(void *tx_queue,
2156 struct rte_mbuf **tx_pkts,
2159 struct virtnet_tx *txvq = tx_queue;
2160 struct virtqueue *vq = txvq->vq;
2161 struct virtio_hw *hw = vq->hw;
2162 uint16_t hdr_size = hw->vtnet_hdr_size;
2163 uint16_t nb_used, nb_avail, nb_tx = 0, nb_inorder_pkts = 0;
2164 struct rte_mbuf *inorder_pkts[nb_pkts];
2167 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2170 if (unlikely(nb_pkts < 1))
2174 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2175 nb_used = VIRTQUEUE_NUSED(vq);
2177 virtio_rmb(hw->weak_barriers);
2178 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2179 virtio_xmit_cleanup_inorder(vq, nb_used);
2181 if (unlikely(!vq->vq_free_cnt))
2182 virtio_xmit_cleanup_inorder(vq, nb_used);
2184 nb_avail = RTE_MIN(vq->vq_free_cnt, nb_pkts);
2186 for (nb_tx = 0; nb_tx < nb_avail; nb_tx++) {
2187 struct rte_mbuf *txm = tx_pkts[nb_tx];
2190 /* Do VLAN tag insertion */
2191 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
2192 error = rte_vlan_insert(&txm);
2193 if (unlikely(error)) {
2194 rte_pktmbuf_free(txm);
2199 /* optimize ring usage */
2200 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2201 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2202 rte_mbuf_refcnt_read(txm) == 1 &&
2203 RTE_MBUF_DIRECT(txm) &&
2204 txm->nb_segs == 1 &&
2205 rte_pktmbuf_headroom(txm) >= hdr_size &&
2206 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2207 __alignof__(struct virtio_net_hdr_mrg_rxbuf))) {
2208 inorder_pkts[nb_inorder_pkts] = txm;
2211 virtio_update_packet_stats(&txvq->stats, txm);
2215 if (nb_inorder_pkts) {
2216 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2218 nb_inorder_pkts = 0;
2221 slots = txm->nb_segs + 1;
2222 need = slots - vq->vq_free_cnt;
2223 if (unlikely(need > 0)) {
2224 nb_used = VIRTQUEUE_NUSED(vq);
2225 virtio_rmb(hw->weak_barriers);
2226 need = RTE_MIN(need, (int)nb_used);
2228 virtio_xmit_cleanup_inorder(vq, need);
2230 need = slots - vq->vq_free_cnt;
2232 if (unlikely(need > 0)) {
2234 "No free tx descriptors to transmit");
2238 /* Enqueue Packet buffers */
2239 virtqueue_enqueue_xmit(txvq, txm, slots, 0, 0, 1);
2241 virtio_update_packet_stats(&txvq->stats, txm);
2244 /* Transmit all inorder packets */
2245 if (nb_inorder_pkts)
2246 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2249 txvq->stats.packets += nb_tx;
2251 if (likely(nb_tx)) {
2252 vq_update_avail_idx(vq);
2254 if (unlikely(virtqueue_kick_prepare(vq))) {
2255 virtqueue_notify(vq);
2256 PMD_TX_LOG(DEBUG, "Notified backend after xmit");