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 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
112 uint32_t s = mbuf->pkt_len;
113 struct rte_ether_addr *ea;
118 stats->size_bins[1]++;
119 } else if (s > 64 && s < 1024) {
122 /* count zeros, and offset into correct bin */
123 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
124 stats->size_bins[bin]++;
127 stats->size_bins[0]++;
129 stats->size_bins[6]++;
131 stats->size_bins[7]++;
134 ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
135 if (rte_is_multicast_ether_addr(ea)) {
136 if (rte_is_broadcast_ether_addr(ea))
144 virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
146 VIRTIO_DUMP_PACKET(m, m->data_len);
148 virtio_update_packet_stats(&rxvq->stats, m);
152 virtqueue_dequeue_burst_rx_packed(struct virtqueue *vq,
153 struct rte_mbuf **rx_pkts,
157 struct rte_mbuf *cookie;
160 struct vring_packed_desc *desc;
163 desc = vq->vq_packed.ring.desc;
165 for (i = 0; i < num; i++) {
166 used_idx = vq->vq_used_cons_idx;
167 if (!desc_is_used(&desc[used_idx], vq))
169 virtio_rmb(vq->hw->weak_barriers);
170 len[i] = desc[used_idx].len;
171 id = desc[used_idx].id;
172 cookie = (struct rte_mbuf *)vq->vq_descx[id].cookie;
173 if (unlikely(cookie == NULL)) {
174 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
175 vq->vq_used_cons_idx);
178 rte_prefetch0(cookie);
179 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
183 vq->vq_used_cons_idx++;
184 if (vq->vq_used_cons_idx >= vq->vq_nentries) {
185 vq->vq_used_cons_idx -= vq->vq_nentries;
186 vq->vq_packed.used_wrap_counter ^= 1;
194 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
195 uint32_t *len, uint16_t num)
197 struct vring_used_elem *uep;
198 struct rte_mbuf *cookie;
199 uint16_t used_idx, desc_idx;
202 /* Caller does the check */
203 for (i = 0; i < num ; i++) {
204 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
205 uep = &vq->vq_split.ring.used->ring[used_idx];
206 desc_idx = (uint16_t) uep->id;
208 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
210 if (unlikely(cookie == NULL)) {
211 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
212 vq->vq_used_cons_idx);
216 rte_prefetch0(cookie);
217 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
219 vq->vq_used_cons_idx++;
220 vq_ring_free_chain(vq, desc_idx);
221 vq->vq_descx[desc_idx].cookie = NULL;
228 virtqueue_dequeue_rx_inorder(struct virtqueue *vq,
229 struct rte_mbuf **rx_pkts,
233 struct vring_used_elem *uep;
234 struct rte_mbuf *cookie;
235 uint16_t used_idx = 0;
238 if (unlikely(num == 0))
241 for (i = 0; i < num; i++) {
242 used_idx = vq->vq_used_cons_idx & (vq->vq_nentries - 1);
243 /* Desc idx same as used idx */
244 uep = &vq->vq_split.ring.used->ring[used_idx];
246 cookie = (struct rte_mbuf *)vq->vq_descx[used_idx].cookie;
248 if (unlikely(cookie == NULL)) {
249 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
250 vq->vq_used_cons_idx);
254 rte_prefetch0(cookie);
255 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
257 vq->vq_used_cons_idx++;
258 vq->vq_descx[used_idx].cookie = NULL;
261 vq_ring_free_inorder(vq, used_idx, i);
265 #ifndef DEFAULT_TX_FREE_THRESH
266 #define DEFAULT_TX_FREE_THRESH 32
270 virtio_xmit_cleanup_inorder_packed(struct virtqueue *vq, int num)
272 uint16_t used_idx, id, curr_id, free_cnt = 0;
273 uint16_t size = vq->vq_nentries;
274 struct vring_packed_desc *desc = vq->vq_packed.ring.desc;
275 struct vq_desc_extra *dxp;
277 used_idx = vq->vq_used_cons_idx;
278 while (num > 0 && desc_is_used(&desc[used_idx], vq)) {
279 virtio_rmb(vq->hw->weak_barriers);
280 id = desc[used_idx].id;
283 dxp = &vq->vq_descx[used_idx];
284 used_idx += dxp->ndescs;
285 free_cnt += dxp->ndescs;
287 if (used_idx >= size) {
289 vq->vq_packed.used_wrap_counter ^= 1;
291 if (dxp->cookie != NULL) {
292 rte_pktmbuf_free(dxp->cookie);
295 } while (curr_id != id);
297 vq->vq_used_cons_idx = used_idx;
298 vq->vq_free_cnt += free_cnt;
302 virtio_xmit_cleanup_normal_packed(struct virtqueue *vq, int num)
304 uint16_t used_idx, id;
305 uint16_t size = vq->vq_nentries;
306 struct vring_packed_desc *desc = vq->vq_packed.ring.desc;
307 struct vq_desc_extra *dxp;
309 used_idx = vq->vq_used_cons_idx;
310 while (num-- && desc_is_used(&desc[used_idx], vq)) {
311 virtio_rmb(vq->hw->weak_barriers);
312 id = desc[used_idx].id;
313 dxp = &vq->vq_descx[id];
314 vq->vq_used_cons_idx += dxp->ndescs;
315 if (vq->vq_used_cons_idx >= size) {
316 vq->vq_used_cons_idx -= size;
317 vq->vq_packed.used_wrap_counter ^= 1;
319 vq_ring_free_id_packed(vq, id);
320 if (dxp->cookie != NULL) {
321 rte_pktmbuf_free(dxp->cookie);
324 used_idx = vq->vq_used_cons_idx;
328 /* Cleanup from completed transmits. */
330 virtio_xmit_cleanup_packed(struct virtqueue *vq, int num, int in_order)
333 virtio_xmit_cleanup_inorder_packed(vq, num);
335 virtio_xmit_cleanup_normal_packed(vq, num);
339 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
341 uint16_t i, used_idx, desc_idx;
342 for (i = 0; i < num; i++) {
343 struct vring_used_elem *uep;
344 struct vq_desc_extra *dxp;
346 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
347 uep = &vq->vq_split.ring.used->ring[used_idx];
349 desc_idx = (uint16_t) uep->id;
350 dxp = &vq->vq_descx[desc_idx];
351 vq->vq_used_cons_idx++;
352 vq_ring_free_chain(vq, desc_idx);
354 if (dxp->cookie != NULL) {
355 rte_pktmbuf_free(dxp->cookie);
361 /* Cleanup from completed inorder transmits. */
362 static __rte_always_inline void
363 virtio_xmit_cleanup_inorder(struct virtqueue *vq, uint16_t num)
365 uint16_t i, idx = vq->vq_used_cons_idx;
366 int16_t free_cnt = 0;
367 struct vq_desc_extra *dxp = NULL;
369 if (unlikely(num == 0))
372 for (i = 0; i < num; i++) {
373 dxp = &vq->vq_descx[idx++ & (vq->vq_nentries - 1)];
374 free_cnt += dxp->ndescs;
375 if (dxp->cookie != NULL) {
376 rte_pktmbuf_free(dxp->cookie);
381 vq->vq_free_cnt += free_cnt;
382 vq->vq_used_cons_idx = idx;
386 virtqueue_enqueue_refill_inorder(struct virtqueue *vq,
387 struct rte_mbuf **cookies,
390 struct vq_desc_extra *dxp;
391 struct virtio_hw *hw = vq->hw;
392 struct vring_desc *start_dp;
393 uint16_t head_idx, idx, i = 0;
395 if (unlikely(vq->vq_free_cnt == 0))
397 if (unlikely(vq->vq_free_cnt < num))
400 head_idx = vq->vq_desc_head_idx & (vq->vq_nentries - 1);
401 start_dp = vq->vq_split.ring.desc;
404 idx = head_idx & (vq->vq_nentries - 1);
405 dxp = &vq->vq_descx[idx];
406 dxp->cookie = (void *)cookies[i];
410 VIRTIO_MBUF_ADDR(cookies[i], vq) +
411 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
413 cookies[i]->buf_len -
414 RTE_PKTMBUF_HEADROOM +
416 start_dp[idx].flags = VRING_DESC_F_WRITE;
418 vq_update_avail_ring(vq, idx);
423 vq->vq_desc_head_idx += num;
424 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
429 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf **cookie,
432 struct vq_desc_extra *dxp;
433 struct virtio_hw *hw = vq->hw;
434 struct vring_desc *start_dp = vq->vq_split.ring.desc;
437 if (unlikely(vq->vq_free_cnt == 0))
439 if (unlikely(vq->vq_free_cnt < num))
442 if (unlikely(vq->vq_desc_head_idx >= vq->vq_nentries))
445 for (i = 0; i < num; i++) {
446 idx = vq->vq_desc_head_idx;
447 dxp = &vq->vq_descx[idx];
448 dxp->cookie = (void *)cookie[i];
452 VIRTIO_MBUF_ADDR(cookie[i], vq) +
453 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
455 cookie[i]->buf_len - RTE_PKTMBUF_HEADROOM +
457 start_dp[idx].flags = VRING_DESC_F_WRITE;
458 vq->vq_desc_head_idx = start_dp[idx].next;
459 vq_update_avail_ring(vq, idx);
460 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END) {
461 vq->vq_desc_tail_idx = vq->vq_desc_head_idx;
466 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
472 virtqueue_enqueue_recv_refill_packed(struct virtqueue *vq,
473 struct rte_mbuf **cookie, uint16_t num)
475 struct vring_packed_desc *start_dp = vq->vq_packed.ring.desc;
476 uint16_t flags = vq->vq_packed.cached_flags;
477 struct virtio_hw *hw = vq->hw;
478 struct vq_desc_extra *dxp;
482 if (unlikely(vq->vq_free_cnt == 0))
484 if (unlikely(vq->vq_free_cnt < num))
487 for (i = 0; i < num; i++) {
488 idx = vq->vq_avail_idx;
489 dxp = &vq->vq_descx[idx];
490 dxp->cookie = (void *)cookie[i];
493 start_dp[idx].addr = VIRTIO_MBUF_ADDR(cookie[i], vq) +
494 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
495 start_dp[idx].len = cookie[i]->buf_len - RTE_PKTMBUF_HEADROOM
496 + hw->vtnet_hdr_size;
498 vq->vq_desc_head_idx = dxp->next;
499 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
500 vq->vq_desc_tail_idx = vq->vq_desc_head_idx;
501 virtio_wmb(hw->weak_barriers);
502 start_dp[idx].flags = flags;
503 if (++vq->vq_avail_idx >= vq->vq_nentries) {
504 vq->vq_avail_idx -= vq->vq_nentries;
505 vq->vq_packed.cached_flags ^=
506 VRING_PACKED_DESC_F_AVAIL_USED;
507 flags = vq->vq_packed.cached_flags;
510 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
514 /* When doing TSO, the IP length is not included in the pseudo header
515 * checksum of the packet given to the PMD, but for virtio it is
519 virtio_tso_fix_cksum(struct rte_mbuf *m)
521 /* common case: header is not fragmented */
522 if (likely(rte_pktmbuf_data_len(m) >= m->l2_len + m->l3_len +
524 struct rte_ipv4_hdr *iph;
525 struct rte_ipv6_hdr *ip6h;
526 struct rte_tcp_hdr *th;
527 uint16_t prev_cksum, new_cksum, ip_len, ip_paylen;
530 iph = rte_pktmbuf_mtod_offset(m,
531 struct rte_ipv4_hdr *, m->l2_len);
532 th = RTE_PTR_ADD(iph, m->l3_len);
533 if ((iph->version_ihl >> 4) == 4) {
534 iph->hdr_checksum = 0;
535 iph->hdr_checksum = rte_ipv4_cksum(iph);
536 ip_len = iph->total_length;
537 ip_paylen = rte_cpu_to_be_16(rte_be_to_cpu_16(ip_len) -
540 ip6h = (struct rte_ipv6_hdr *)iph;
541 ip_paylen = ip6h->payload_len;
544 /* calculate the new phdr checksum not including ip_paylen */
545 prev_cksum = th->cksum;
548 tmp = (tmp & 0xffff) + (tmp >> 16);
551 /* replace it in the packet */
552 th->cksum = new_cksum;
557 /* avoid write operation when necessary, to lessen cache issues */
558 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
559 if ((var) != (val)) \
563 #define virtqueue_clear_net_hdr(_hdr) do { \
564 ASSIGN_UNLESS_EQUAL((_hdr)->csum_start, 0); \
565 ASSIGN_UNLESS_EQUAL((_hdr)->csum_offset, 0); \
566 ASSIGN_UNLESS_EQUAL((_hdr)->flags, 0); \
567 ASSIGN_UNLESS_EQUAL((_hdr)->gso_type, 0); \
568 ASSIGN_UNLESS_EQUAL((_hdr)->gso_size, 0); \
569 ASSIGN_UNLESS_EQUAL((_hdr)->hdr_len, 0); \
573 virtqueue_xmit_offload(struct virtio_net_hdr *hdr,
574 struct rte_mbuf *cookie,
578 if (cookie->ol_flags & PKT_TX_TCP_SEG)
579 cookie->ol_flags |= PKT_TX_TCP_CKSUM;
581 switch (cookie->ol_flags & PKT_TX_L4_MASK) {
582 case PKT_TX_UDP_CKSUM:
583 hdr->csum_start = cookie->l2_len + cookie->l3_len;
584 hdr->csum_offset = offsetof(struct rte_udp_hdr,
586 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
589 case PKT_TX_TCP_CKSUM:
590 hdr->csum_start = cookie->l2_len + cookie->l3_len;
591 hdr->csum_offset = offsetof(struct rte_tcp_hdr, cksum);
592 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
596 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
597 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
598 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
602 /* TCP Segmentation Offload */
603 if (cookie->ol_flags & PKT_TX_TCP_SEG) {
604 hdr->gso_type = (cookie->ol_flags & PKT_TX_IPV6) ?
605 VIRTIO_NET_HDR_GSO_TCPV6 :
606 VIRTIO_NET_HDR_GSO_TCPV4;
607 hdr->gso_size = cookie->tso_segsz;
613 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
614 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
615 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
621 virtqueue_enqueue_xmit_inorder(struct virtnet_tx *txvq,
622 struct rte_mbuf **cookies,
625 struct vq_desc_extra *dxp;
626 struct virtqueue *vq = txvq->vq;
627 struct vring_desc *start_dp;
628 struct virtio_net_hdr *hdr;
630 uint16_t head_size = vq->hw->vtnet_hdr_size;
633 idx = vq->vq_desc_head_idx;
634 start_dp = vq->vq_split.ring.desc;
637 idx = idx & (vq->vq_nentries - 1);
638 dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
639 dxp->cookie = (void *)cookies[i];
641 virtio_update_packet_stats(&txvq->stats, cookies[i]);
643 hdr = (struct virtio_net_hdr *)(char *)cookies[i]->buf_addr +
644 cookies[i]->data_off - head_size;
646 /* if offload disabled, hdr is not zeroed yet, do it now */
647 if (!vq->hw->has_tx_offload)
648 virtqueue_clear_net_hdr(hdr);
650 virtqueue_xmit_offload(hdr, cookies[i], true);
652 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookies[i], vq);
653 start_dp[idx].len = cookies[i]->data_len + head_size;
654 start_dp[idx].flags = 0;
657 vq_update_avail_ring(vq, idx);
663 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
664 vq->vq_desc_head_idx = idx & (vq->vq_nentries - 1);
668 virtqueue_enqueue_xmit_packed_fast(struct virtnet_tx *txvq,
669 struct rte_mbuf *cookie,
672 struct virtqueue *vq = txvq->vq;
673 struct vring_packed_desc *dp;
674 struct vq_desc_extra *dxp;
675 uint16_t idx, id, flags;
676 uint16_t head_size = vq->hw->vtnet_hdr_size;
677 struct virtio_net_hdr *hdr;
679 id = in_order ? vq->vq_avail_idx : vq->vq_desc_head_idx;
680 idx = vq->vq_avail_idx;
681 dp = &vq->vq_packed.ring.desc[idx];
683 dxp = &vq->vq_descx[id];
685 dxp->cookie = cookie;
687 flags = vq->vq_packed.cached_flags;
689 /* prepend cannot fail, checked by caller */
690 hdr = (struct virtio_net_hdr *)(char *)cookie->buf_addr +
691 cookie->data_off - head_size;
693 /* if offload disabled, hdr is not zeroed yet, do it now */
694 if (!vq->hw->has_tx_offload)
695 virtqueue_clear_net_hdr(hdr);
697 virtqueue_xmit_offload(hdr, cookie, true);
699 dp->addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
700 dp->len = cookie->data_len + head_size;
703 if (++vq->vq_avail_idx >= vq->vq_nentries) {
704 vq->vq_avail_idx -= vq->vq_nentries;
705 vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
711 vq->vq_desc_head_idx = dxp->next;
712 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
713 vq->vq_desc_tail_idx = VQ_RING_DESC_CHAIN_END;
716 virtio_wmb(vq->hw->weak_barriers);
721 virtqueue_enqueue_xmit_packed(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
722 uint16_t needed, int can_push, int in_order)
724 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
725 struct vq_desc_extra *dxp;
726 struct virtqueue *vq = txvq->vq;
727 struct vring_packed_desc *start_dp, *head_dp;
728 uint16_t idx, id, head_idx, head_flags;
729 uint16_t head_size = vq->hw->vtnet_hdr_size;
730 struct virtio_net_hdr *hdr;
732 bool prepend_header = false;
734 id = in_order ? vq->vq_avail_idx : vq->vq_desc_head_idx;
736 dxp = &vq->vq_descx[id];
737 dxp->ndescs = needed;
738 dxp->cookie = cookie;
740 head_idx = vq->vq_avail_idx;
743 start_dp = vq->vq_packed.ring.desc;
745 head_dp = &vq->vq_packed.ring.desc[idx];
746 head_flags = cookie->next ? VRING_DESC_F_NEXT : 0;
747 head_flags |= vq->vq_packed.cached_flags;
750 /* prepend cannot fail, checked by caller */
751 hdr = (struct virtio_net_hdr *)(char *)cookie->buf_addr +
752 cookie->data_off - head_size;
753 prepend_header = true;
755 /* if offload disabled, it is not zeroed below, do it now */
756 if (!vq->hw->has_tx_offload)
757 virtqueue_clear_net_hdr(hdr);
759 /* setup first tx ring slot to point to header
760 * stored in reserved region.
762 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
763 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
764 start_dp[idx].len = vq->hw->vtnet_hdr_size;
765 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
767 if (idx >= vq->vq_nentries) {
768 idx -= vq->vq_nentries;
769 vq->vq_packed.cached_flags ^=
770 VRING_PACKED_DESC_F_AVAIL_USED;
774 virtqueue_xmit_offload(hdr, cookie, vq->hw->has_tx_offload);
779 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
780 start_dp[idx].len = cookie->data_len;
781 if (prepend_header) {
782 start_dp[idx].len += head_size;
783 prepend_header = false;
786 if (likely(idx != head_idx)) {
787 flags = cookie->next ? VRING_DESC_F_NEXT : 0;
788 flags |= vq->vq_packed.cached_flags;
789 start_dp[idx].flags = flags;
793 if (idx >= vq->vq_nentries) {
794 idx -= vq->vq_nentries;
795 vq->vq_packed.cached_flags ^=
796 VRING_PACKED_DESC_F_AVAIL_USED;
798 } while ((cookie = cookie->next) != NULL);
800 start_dp[prev].id = id;
802 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
803 vq->vq_avail_idx = idx;
806 vq->vq_desc_head_idx = dxp->next;
807 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
808 vq->vq_desc_tail_idx = VQ_RING_DESC_CHAIN_END;
811 virtio_wmb(vq->hw->weak_barriers);
812 head_dp->flags = head_flags;
816 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
817 uint16_t needed, int use_indirect, int can_push,
820 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
821 struct vq_desc_extra *dxp;
822 struct virtqueue *vq = txvq->vq;
823 struct vring_desc *start_dp;
824 uint16_t seg_num = cookie->nb_segs;
825 uint16_t head_idx, idx;
826 uint16_t head_size = vq->hw->vtnet_hdr_size;
827 bool prepend_header = false;
828 struct virtio_net_hdr *hdr;
830 head_idx = vq->vq_desc_head_idx;
833 dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
835 dxp = &vq->vq_descx[idx];
836 dxp->cookie = (void *)cookie;
837 dxp->ndescs = needed;
839 start_dp = vq->vq_split.ring.desc;
842 /* prepend cannot fail, checked by caller */
843 hdr = (struct virtio_net_hdr *)(char *)cookie->buf_addr +
844 cookie->data_off - head_size;
845 prepend_header = true;
847 /* if offload disabled, it is not zeroed below, do it now */
848 if (!vq->hw->has_tx_offload)
849 virtqueue_clear_net_hdr(hdr);
850 } else if (use_indirect) {
851 /* setup tx ring slot to point to indirect
852 * descriptor list stored in reserved region.
854 * the first slot in indirect ring is already preset
855 * to point to the header in reserved region
857 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
858 RTE_PTR_DIFF(&txr[idx].tx_indir, txr);
859 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
860 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
861 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
863 /* loop below will fill in rest of the indirect elements */
864 start_dp = txr[idx].tx_indir;
867 /* setup first tx ring slot to point to header
868 * stored in reserved region.
870 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
871 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
872 start_dp[idx].len = vq->hw->vtnet_hdr_size;
873 start_dp[idx].flags = VRING_DESC_F_NEXT;
874 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
876 idx = start_dp[idx].next;
879 virtqueue_xmit_offload(hdr, cookie, vq->hw->has_tx_offload);
882 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
883 start_dp[idx].len = cookie->data_len;
884 if (prepend_header) {
885 start_dp[idx].len += head_size;
886 prepend_header = false;
888 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
889 idx = start_dp[idx].next;
890 } while ((cookie = cookie->next) != NULL);
893 idx = vq->vq_split.ring.desc[head_idx].next;
895 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
897 vq->vq_desc_head_idx = idx;
898 vq_update_avail_ring(vq, head_idx);
901 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
902 vq->vq_desc_tail_idx = idx;
907 virtio_dev_cq_start(struct rte_eth_dev *dev)
909 struct virtio_hw *hw = dev->data->dev_private;
911 if (hw->cvq && hw->cvq->vq) {
912 rte_spinlock_init(&hw->cvq->lock);
913 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
918 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
921 unsigned int socket_id __rte_unused,
922 const struct rte_eth_rxconf *rx_conf __rte_unused,
923 struct rte_mempool *mp)
925 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
926 struct virtio_hw *hw = dev->data->dev_private;
927 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
928 struct virtnet_rx *rxvq;
930 PMD_INIT_FUNC_TRACE();
932 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
933 nb_desc = vq->vq_nentries;
934 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
937 rxvq->queue_id = queue_idx;
939 dev->data->rx_queues[queue_idx] = rxvq;
945 virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
947 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
948 struct virtio_hw *hw = dev->data->dev_private;
949 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
950 struct virtnet_rx *rxvq = &vq->rxq;
955 PMD_INIT_FUNC_TRACE();
957 /* Allocate blank mbufs for the each rx descriptor */
960 if (hw->use_simple_rx) {
961 for (desc_idx = 0; desc_idx < vq->vq_nentries;
963 vq->vq_split.ring.avail->ring[desc_idx] = desc_idx;
964 vq->vq_split.ring.desc[desc_idx].flags =
968 virtio_rxq_vec_setup(rxvq);
971 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
972 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
974 vq->sw_ring[vq->vq_nentries + desc_idx] =
978 if (hw->use_simple_rx) {
979 while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
980 virtio_rxq_rearm_vec(rxvq);
981 nbufs += RTE_VIRTIO_VPMD_RX_REARM_THRESH;
983 } else if (hw->use_inorder_rx) {
984 if ((!virtqueue_full(vq))) {
985 uint16_t free_cnt = vq->vq_free_cnt;
986 struct rte_mbuf *pkts[free_cnt];
988 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, pkts,
990 error = virtqueue_enqueue_refill_inorder(vq,
993 if (unlikely(error)) {
994 for (i = 0; i < free_cnt; i++)
995 rte_pktmbuf_free(pkts[i]);
1000 vq_update_avail_idx(vq);
1003 while (!virtqueue_full(vq)) {
1004 m = rte_mbuf_raw_alloc(rxvq->mpool);
1008 /* Enqueue allocated buffers */
1009 if (vtpci_packed_queue(vq->hw))
1010 error = virtqueue_enqueue_recv_refill_packed(vq,
1013 error = virtqueue_enqueue_recv_refill(vq,
1016 rte_pktmbuf_free(m);
1022 if (!vtpci_packed_queue(vq->hw))
1023 vq_update_avail_idx(vq);
1026 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
1034 * struct rte_eth_dev *dev: Used to update dev
1035 * uint16_t nb_desc: Defaults to values read from config space
1036 * unsigned int socket_id: Used to allocate memzone
1037 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
1038 * uint16_t queue_idx: Just used as an index in dev txq list
1041 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
1044 unsigned int socket_id __rte_unused,
1045 const struct rte_eth_txconf *tx_conf)
1047 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1048 struct virtio_hw *hw = dev->data->dev_private;
1049 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1050 struct virtnet_tx *txvq;
1051 uint16_t tx_free_thresh;
1053 PMD_INIT_FUNC_TRACE();
1055 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
1056 nb_desc = vq->vq_nentries;
1057 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
1060 txvq->queue_id = queue_idx;
1062 tx_free_thresh = tx_conf->tx_free_thresh;
1063 if (tx_free_thresh == 0)
1065 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
1067 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
1068 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
1069 "number of TX entries minus 3 (%u)."
1070 " (tx_free_thresh=%u port=%u queue=%u)\n",
1071 vq->vq_nentries - 3,
1072 tx_free_thresh, dev->data->port_id, queue_idx);
1076 vq->vq_free_thresh = tx_free_thresh;
1078 dev->data->tx_queues[queue_idx] = txvq;
1083 virtio_dev_tx_queue_setup_finish(struct rte_eth_dev *dev,
1086 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1087 struct virtio_hw *hw = dev->data->dev_private;
1088 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1090 PMD_INIT_FUNC_TRACE();
1092 if (!vtpci_packed_queue(hw)) {
1093 if (hw->use_inorder_tx)
1094 vq->vq_split.ring.desc[vq->vq_nentries - 1].next = 0;
1103 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
1107 * Requeue the discarded mbuf. This should always be
1108 * successful since it was just dequeued.
1110 if (vtpci_packed_queue(vq->hw))
1111 error = virtqueue_enqueue_recv_refill_packed(vq, &m, 1);
1113 error = virtqueue_enqueue_recv_refill(vq, &m, 1);
1115 if (unlikely(error)) {
1116 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
1117 rte_pktmbuf_free(m);
1122 virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)
1126 error = virtqueue_enqueue_refill_inorder(vq, &m, 1);
1127 if (unlikely(error)) {
1128 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
1129 rte_pktmbuf_free(m);
1133 /* Optionally fill offload information in structure */
1135 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
1137 struct rte_net_hdr_lens hdr_lens;
1138 uint32_t hdrlen, ptype;
1139 int l4_supported = 0;
1142 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1145 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
1147 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
1148 m->packet_type = ptype;
1149 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
1150 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
1151 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
1154 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1155 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
1156 if (hdr->csum_start <= hdrlen && l4_supported) {
1157 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
1159 /* Unknown proto or tunnel, do sw cksum. We can assume
1160 * the cksum field is in the first segment since the
1161 * buffers we provided to the host are large enough.
1162 * In case of SCTP, this will be wrong since it's a CRC
1163 * but there's nothing we can do.
1165 uint16_t csum = 0, off;
1167 rte_raw_cksum_mbuf(m, hdr->csum_start,
1168 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
1170 if (likely(csum != 0xffff))
1172 off = hdr->csum_offset + hdr->csum_start;
1173 if (rte_pktmbuf_data_len(m) >= off + 1)
1174 *rte_pktmbuf_mtod_offset(m, uint16_t *,
1177 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
1178 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
1181 /* GSO request, save required information in mbuf */
1182 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1183 /* Check unsupported modes */
1184 if ((hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) ||
1185 (hdr->gso_size == 0)) {
1189 /* Update mss lengthes in mbuf */
1190 m->tso_segsz = hdr->gso_size;
1191 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1192 case VIRTIO_NET_HDR_GSO_TCPV4:
1193 case VIRTIO_NET_HDR_GSO_TCPV6:
1194 m->ol_flags |= PKT_RX_LRO | \
1195 PKT_RX_L4_CKSUM_NONE;
1205 #define VIRTIO_MBUF_BURST_SZ 64
1206 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
1208 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1210 struct virtnet_rx *rxvq = rx_queue;
1211 struct virtqueue *vq = rxvq->vq;
1212 struct virtio_hw *hw = vq->hw;
1213 struct rte_mbuf *rxm;
1214 uint16_t nb_used, num, nb_rx;
1215 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1216 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1218 uint32_t i, nb_enqueued;
1220 struct virtio_net_hdr *hdr;
1223 if (unlikely(hw->started == 0))
1226 nb_used = VIRTQUEUE_NUSED(vq);
1228 virtio_rmb(hw->weak_barriers);
1230 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1231 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1232 num = VIRTIO_MBUF_BURST_SZ;
1233 if (likely(num > DESC_PER_CACHELINE))
1234 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1236 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1237 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
1240 hdr_size = hw->vtnet_hdr_size;
1242 for (i = 0; i < num ; i++) {
1245 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1247 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1248 PMD_RX_LOG(ERR, "Packet drop");
1250 virtio_discard_rxbuf(vq, rxm);
1251 rxvq->stats.errors++;
1255 rxm->port = rxvq->port_id;
1256 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1260 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1261 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1263 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1264 RTE_PKTMBUF_HEADROOM - hdr_size);
1267 rte_vlan_strip(rxm);
1269 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1270 virtio_discard_rxbuf(vq, rxm);
1271 rxvq->stats.errors++;
1275 virtio_rx_stats_updated(rxvq, rxm);
1277 rx_pkts[nb_rx++] = rxm;
1280 rxvq->stats.packets += nb_rx;
1282 /* Allocate new mbuf for the used descriptor */
1283 if (likely(!virtqueue_full(vq))) {
1284 uint16_t free_cnt = vq->vq_free_cnt;
1285 struct rte_mbuf *new_pkts[free_cnt];
1287 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1289 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1291 if (unlikely(error)) {
1292 for (i = 0; i < free_cnt; i++)
1293 rte_pktmbuf_free(new_pkts[i]);
1295 nb_enqueued += free_cnt;
1297 struct rte_eth_dev *dev =
1298 &rte_eth_devices[rxvq->port_id];
1299 dev->data->rx_mbuf_alloc_failed += free_cnt;
1303 if (likely(nb_enqueued)) {
1304 vq_update_avail_idx(vq);
1306 if (unlikely(virtqueue_kick_prepare(vq))) {
1307 virtqueue_notify(vq);
1308 PMD_RX_LOG(DEBUG, "Notified");
1316 virtio_recv_pkts_packed(void *rx_queue, struct rte_mbuf **rx_pkts,
1319 struct virtnet_rx *rxvq = rx_queue;
1320 struct virtqueue *vq = rxvq->vq;
1321 struct virtio_hw *hw = vq->hw;
1322 struct rte_mbuf *rxm;
1323 uint16_t num, nb_rx;
1324 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1325 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1327 uint32_t i, nb_enqueued;
1329 struct virtio_net_hdr *hdr;
1332 if (unlikely(hw->started == 0))
1335 num = RTE_MIN(VIRTIO_MBUF_BURST_SZ, nb_pkts);
1336 if (likely(num > DESC_PER_CACHELINE))
1337 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1339 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1340 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1343 hdr_size = hw->vtnet_hdr_size;
1345 for (i = 0; i < num; i++) {
1348 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1350 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1351 PMD_RX_LOG(ERR, "Packet drop");
1353 virtio_discard_rxbuf(vq, rxm);
1354 rxvq->stats.errors++;
1358 rxm->port = rxvq->port_id;
1359 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1363 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1364 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1366 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1367 RTE_PKTMBUF_HEADROOM - hdr_size);
1370 rte_vlan_strip(rxm);
1372 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1373 virtio_discard_rxbuf(vq, rxm);
1374 rxvq->stats.errors++;
1378 virtio_rx_stats_updated(rxvq, rxm);
1380 rx_pkts[nb_rx++] = rxm;
1383 rxvq->stats.packets += nb_rx;
1385 /* Allocate new mbuf for the used descriptor */
1386 if (likely(!virtqueue_full(vq))) {
1387 uint16_t free_cnt = vq->vq_free_cnt;
1388 struct rte_mbuf *new_pkts[free_cnt];
1390 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1392 error = virtqueue_enqueue_recv_refill_packed(vq,
1393 new_pkts, free_cnt);
1394 if (unlikely(error)) {
1395 for (i = 0; i < free_cnt; i++)
1396 rte_pktmbuf_free(new_pkts[i]);
1398 nb_enqueued += free_cnt;
1400 struct rte_eth_dev *dev =
1401 &rte_eth_devices[rxvq->port_id];
1402 dev->data->rx_mbuf_alloc_failed += free_cnt;
1406 if (likely(nb_enqueued)) {
1407 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1408 virtqueue_notify(vq);
1409 PMD_RX_LOG(DEBUG, "Notified");
1418 virtio_recv_pkts_inorder(void *rx_queue,
1419 struct rte_mbuf **rx_pkts,
1422 struct virtnet_rx *rxvq = rx_queue;
1423 struct virtqueue *vq = rxvq->vq;
1424 struct virtio_hw *hw = vq->hw;
1425 struct rte_mbuf *rxm;
1426 struct rte_mbuf *prev = NULL;
1427 uint16_t nb_used, num, nb_rx;
1428 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1429 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1431 uint32_t nb_enqueued;
1438 if (unlikely(hw->started == 0))
1441 nb_used = VIRTQUEUE_NUSED(vq);
1442 nb_used = RTE_MIN(nb_used, nb_pkts);
1443 nb_used = RTE_MIN(nb_used, VIRTIO_MBUF_BURST_SZ);
1445 virtio_rmb(hw->weak_barriers);
1447 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1452 hdr_size = hw->vtnet_hdr_size;
1454 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len, nb_used);
1456 for (i = 0; i < num; i++) {
1457 struct virtio_net_hdr_mrg_rxbuf *header;
1459 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1460 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1464 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1465 PMD_RX_LOG(ERR, "Packet drop");
1467 virtio_discard_rxbuf_inorder(vq, rxm);
1468 rxvq->stats.errors++;
1472 header = (struct virtio_net_hdr_mrg_rxbuf *)
1473 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1476 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1477 seg_num = header->num_buffers;
1484 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1485 rxm->nb_segs = seg_num;
1488 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1489 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1491 rxm->port = rxvq->port_id;
1493 rx_pkts[nb_rx] = rxm;
1496 if (vq->hw->has_rx_offload &&
1497 virtio_rx_offload(rxm, &header->hdr) < 0) {
1498 virtio_discard_rxbuf_inorder(vq, rxm);
1499 rxvq->stats.errors++;
1504 rte_vlan_strip(rx_pkts[nb_rx]);
1506 seg_res = seg_num - 1;
1508 /* Merge remaining segments */
1509 while (seg_res != 0 && i < (num - 1)) {
1513 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1514 rxm->pkt_len = (uint32_t)(len[i]);
1515 rxm->data_len = (uint16_t)(len[i]);
1517 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1525 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1530 /* Last packet still need merge segments */
1531 while (seg_res != 0) {
1532 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1533 VIRTIO_MBUF_BURST_SZ);
1535 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1536 virtio_rmb(hw->weak_barriers);
1537 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len,
1539 uint16_t extra_idx = 0;
1542 while (extra_idx < rcv_cnt) {
1543 rxm = rcv_pkts[extra_idx];
1545 RTE_PKTMBUF_HEADROOM - hdr_size;
1546 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1547 rxm->data_len = (uint16_t)(len[extra_idx]);
1550 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1556 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1561 "No enough segments for packet.");
1562 rte_pktmbuf_free(rx_pkts[nb_rx]);
1563 rxvq->stats.errors++;
1568 rxvq->stats.packets += nb_rx;
1570 /* Allocate new mbuf for the used descriptor */
1572 if (likely(!virtqueue_full(vq))) {
1573 /* free_cnt may include mrg descs */
1574 uint16_t free_cnt = vq->vq_free_cnt;
1575 struct rte_mbuf *new_pkts[free_cnt];
1577 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1578 error = virtqueue_enqueue_refill_inorder(vq, new_pkts,
1580 if (unlikely(error)) {
1581 for (i = 0; i < free_cnt; i++)
1582 rte_pktmbuf_free(new_pkts[i]);
1584 nb_enqueued += free_cnt;
1586 struct rte_eth_dev *dev =
1587 &rte_eth_devices[rxvq->port_id];
1588 dev->data->rx_mbuf_alloc_failed += free_cnt;
1592 if (likely(nb_enqueued)) {
1593 vq_update_avail_idx(vq);
1595 if (unlikely(virtqueue_kick_prepare(vq))) {
1596 virtqueue_notify(vq);
1597 PMD_RX_LOG(DEBUG, "Notified");
1605 virtio_recv_mergeable_pkts(void *rx_queue,
1606 struct rte_mbuf **rx_pkts,
1609 struct virtnet_rx *rxvq = rx_queue;
1610 struct virtqueue *vq = rxvq->vq;
1611 struct virtio_hw *hw = vq->hw;
1612 struct rte_mbuf *rxm;
1613 struct rte_mbuf *prev = NULL;
1614 uint16_t nb_used, num, nb_rx = 0;
1615 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1616 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1618 uint32_t nb_enqueued = 0;
1619 uint32_t seg_num = 0;
1620 uint32_t seg_res = 0;
1621 uint32_t hdr_size = hw->vtnet_hdr_size;
1624 if (unlikely(hw->started == 0))
1627 nb_used = VIRTQUEUE_NUSED(vq);
1629 virtio_rmb(hw->weak_barriers);
1631 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1633 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1634 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1635 num = VIRTIO_MBUF_BURST_SZ;
1636 if (likely(num > DESC_PER_CACHELINE))
1637 num = num - ((vq->vq_used_cons_idx + num) %
1638 DESC_PER_CACHELINE);
1641 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1643 for (i = 0; i < num; i++) {
1644 struct virtio_net_hdr_mrg_rxbuf *header;
1646 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1647 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1651 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1652 PMD_RX_LOG(ERR, "Packet drop");
1654 virtio_discard_rxbuf(vq, rxm);
1655 rxvq->stats.errors++;
1659 header = (struct virtio_net_hdr_mrg_rxbuf *)
1660 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1662 seg_num = header->num_buffers;
1666 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1667 rxm->nb_segs = seg_num;
1670 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1671 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1673 rxm->port = rxvq->port_id;
1675 rx_pkts[nb_rx] = rxm;
1678 if (hw->has_rx_offload &&
1679 virtio_rx_offload(rxm, &header->hdr) < 0) {
1680 virtio_discard_rxbuf(vq, rxm);
1681 rxvq->stats.errors++;
1686 rte_vlan_strip(rx_pkts[nb_rx]);
1688 seg_res = seg_num - 1;
1690 /* Merge remaining segments */
1691 while (seg_res != 0 && i < (num - 1)) {
1695 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1696 rxm->pkt_len = (uint32_t)(len[i]);
1697 rxm->data_len = (uint16_t)(len[i]);
1699 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1707 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1712 /* Last packet still need merge segments */
1713 while (seg_res != 0) {
1714 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1715 VIRTIO_MBUF_BURST_SZ);
1717 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1718 virtio_rmb(hw->weak_barriers);
1719 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len,
1721 uint16_t extra_idx = 0;
1724 while (extra_idx < rcv_cnt) {
1725 rxm = rcv_pkts[extra_idx];
1727 RTE_PKTMBUF_HEADROOM - hdr_size;
1728 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1729 rxm->data_len = (uint16_t)(len[extra_idx]);
1732 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1738 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1743 "No enough segments for packet.");
1744 rte_pktmbuf_free(rx_pkts[nb_rx]);
1745 rxvq->stats.errors++;
1750 rxvq->stats.packets += nb_rx;
1752 /* Allocate new mbuf for the used descriptor */
1753 if (likely(!virtqueue_full(vq))) {
1754 /* free_cnt may include mrg descs */
1755 uint16_t free_cnt = vq->vq_free_cnt;
1756 struct rte_mbuf *new_pkts[free_cnt];
1758 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1759 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1761 if (unlikely(error)) {
1762 for (i = 0; i < free_cnt; i++)
1763 rte_pktmbuf_free(new_pkts[i]);
1765 nb_enqueued += free_cnt;
1767 struct rte_eth_dev *dev =
1768 &rte_eth_devices[rxvq->port_id];
1769 dev->data->rx_mbuf_alloc_failed += free_cnt;
1773 if (likely(nb_enqueued)) {
1774 vq_update_avail_idx(vq);
1776 if (unlikely(virtqueue_kick_prepare(vq))) {
1777 virtqueue_notify(vq);
1778 PMD_RX_LOG(DEBUG, "Notified");
1786 virtio_recv_mergeable_pkts_packed(void *rx_queue,
1787 struct rte_mbuf **rx_pkts,
1790 struct virtnet_rx *rxvq = rx_queue;
1791 struct virtqueue *vq = rxvq->vq;
1792 struct virtio_hw *hw = vq->hw;
1793 struct rte_mbuf *rxm;
1794 struct rte_mbuf *prev = NULL;
1795 uint16_t num, nb_rx = 0;
1796 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1797 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1798 uint32_t nb_enqueued = 0;
1799 uint32_t seg_num = 0;
1800 uint32_t seg_res = 0;
1801 uint32_t hdr_size = hw->vtnet_hdr_size;
1805 if (unlikely(hw->started == 0))
1810 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1811 num = VIRTIO_MBUF_BURST_SZ;
1812 if (likely(num > DESC_PER_CACHELINE))
1813 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1815 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1817 for (i = 0; i < num; i++) {
1818 struct virtio_net_hdr_mrg_rxbuf *header;
1820 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1821 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1825 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1826 PMD_RX_LOG(ERR, "Packet drop");
1828 virtio_discard_rxbuf(vq, rxm);
1829 rxvq->stats.errors++;
1833 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)
1834 rxm->buf_addr + RTE_PKTMBUF_HEADROOM - hdr_size);
1835 seg_num = header->num_buffers;
1840 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1841 rxm->nb_segs = seg_num;
1844 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1845 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1847 rxm->port = rxvq->port_id;
1848 rx_pkts[nb_rx] = rxm;
1851 if (hw->has_rx_offload &&
1852 virtio_rx_offload(rxm, &header->hdr) < 0) {
1853 virtio_discard_rxbuf(vq, rxm);
1854 rxvq->stats.errors++;
1859 rte_vlan_strip(rx_pkts[nb_rx]);
1861 seg_res = seg_num - 1;
1863 /* Merge remaining segments */
1864 while (seg_res != 0 && i < (num - 1)) {
1868 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1869 rxm->pkt_len = (uint32_t)(len[i]);
1870 rxm->data_len = (uint16_t)(len[i]);
1872 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1880 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1885 /* Last packet still need merge segments */
1886 while (seg_res != 0) {
1887 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1888 VIRTIO_MBUF_BURST_SZ);
1889 uint16_t extra_idx = 0;
1891 rcv_cnt = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts,
1893 if (unlikely(rcv_cnt == 0)) {
1894 PMD_RX_LOG(ERR, "No enough segments for packet.");
1895 rte_pktmbuf_free(rx_pkts[nb_rx]);
1896 rxvq->stats.errors++;
1900 while (extra_idx < rcv_cnt) {
1901 rxm = rcv_pkts[extra_idx];
1903 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1904 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1905 rxm->data_len = (uint16_t)(len[extra_idx]);
1909 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1914 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1919 rxvq->stats.packets += nb_rx;
1921 /* Allocate new mbuf for the used descriptor */
1922 if (likely(!virtqueue_full(vq))) {
1923 /* free_cnt may include mrg descs */
1924 uint16_t free_cnt = vq->vq_free_cnt;
1925 struct rte_mbuf *new_pkts[free_cnt];
1927 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1928 error = virtqueue_enqueue_recv_refill_packed(vq,
1929 new_pkts, free_cnt);
1930 if (unlikely(error)) {
1931 for (i = 0; i < free_cnt; i++)
1932 rte_pktmbuf_free(new_pkts[i]);
1934 nb_enqueued += free_cnt;
1936 struct rte_eth_dev *dev =
1937 &rte_eth_devices[rxvq->port_id];
1938 dev->data->rx_mbuf_alloc_failed += free_cnt;
1942 if (likely(nb_enqueued)) {
1943 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1944 virtqueue_notify(vq);
1945 PMD_RX_LOG(DEBUG, "Notified");
1953 virtio_xmit_pkts_prepare(void *tx_queue __rte_unused, struct rte_mbuf **tx_pkts,
1959 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1960 struct rte_mbuf *m = tx_pkts[nb_tx];
1962 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1963 error = rte_validate_tx_offload(m);
1964 if (unlikely(error)) {
1970 /* Do VLAN tag insertion */
1971 if (unlikely(m->ol_flags & PKT_TX_VLAN_PKT)) {
1972 error = rte_vlan_insert(&m);
1973 /* rte_vlan_insert() may change pointer
1974 * even in the case of failure
1978 if (unlikely(error)) {
1984 error = rte_net_intel_cksum_prepare(m);
1985 if (unlikely(error)) {
1990 if (m->ol_flags & PKT_TX_TCP_SEG)
1991 virtio_tso_fix_cksum(m);
1998 virtio_xmit_pkts_packed(void *tx_queue, struct rte_mbuf **tx_pkts,
2001 struct virtnet_tx *txvq = tx_queue;
2002 struct virtqueue *vq = txvq->vq;
2003 struct virtio_hw *hw = vq->hw;
2004 uint16_t hdr_size = hw->vtnet_hdr_size;
2006 bool in_order = hw->use_inorder_tx;
2008 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2011 if (unlikely(nb_pkts < 1))
2014 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2016 if (nb_pkts > vq->vq_free_cnt)
2017 virtio_xmit_cleanup_packed(vq, nb_pkts - vq->vq_free_cnt,
2020 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2021 struct rte_mbuf *txm = tx_pkts[nb_tx];
2022 int can_push = 0, slots, need;
2024 /* optimize ring usage */
2025 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2026 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2027 rte_mbuf_refcnt_read(txm) == 1 &&
2028 RTE_MBUF_DIRECT(txm) &&
2029 txm->nb_segs == 1 &&
2030 rte_pktmbuf_headroom(txm) >= hdr_size &&
2031 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2032 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2035 /* How many main ring entries are needed to this Tx?
2036 * any_layout => number of segments
2037 * default => number of segments + 1
2039 slots = txm->nb_segs + !can_push;
2040 need = slots - vq->vq_free_cnt;
2042 /* Positive value indicates it need free vring descriptors */
2043 if (unlikely(need > 0)) {
2044 virtio_xmit_cleanup_packed(vq, need, in_order);
2045 need = slots - vq->vq_free_cnt;
2046 if (unlikely(need > 0)) {
2048 "No free tx descriptors to transmit");
2053 /* Enqueue Packet buffers */
2055 virtqueue_enqueue_xmit_packed_fast(txvq, txm, in_order);
2057 virtqueue_enqueue_xmit_packed(txvq, txm, slots, 0,
2060 virtio_update_packet_stats(&txvq->stats, txm);
2063 txvq->stats.packets += nb_tx;
2065 if (likely(nb_tx)) {
2066 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
2067 virtqueue_notify(vq);
2068 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2076 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
2078 struct virtnet_tx *txvq = tx_queue;
2079 struct virtqueue *vq = txvq->vq;
2080 struct virtio_hw *hw = vq->hw;
2081 uint16_t hdr_size = hw->vtnet_hdr_size;
2082 uint16_t nb_used, nb_tx = 0;
2084 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2087 if (unlikely(nb_pkts < 1))
2090 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2091 nb_used = VIRTQUEUE_NUSED(vq);
2093 virtio_rmb(hw->weak_barriers);
2094 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2095 virtio_xmit_cleanup(vq, nb_used);
2097 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2098 struct rte_mbuf *txm = tx_pkts[nb_tx];
2099 int can_push = 0, use_indirect = 0, slots, need;
2101 /* optimize ring usage */
2102 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2103 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2104 rte_mbuf_refcnt_read(txm) == 1 &&
2105 RTE_MBUF_DIRECT(txm) &&
2106 txm->nb_segs == 1 &&
2107 rte_pktmbuf_headroom(txm) >= hdr_size &&
2108 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2109 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2111 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
2112 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
2115 /* How many main ring entries are needed to this Tx?
2116 * any_layout => number of segments
2118 * default => number of segments + 1
2120 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
2121 need = slots - vq->vq_free_cnt;
2123 /* Positive value indicates it need free vring descriptors */
2124 if (unlikely(need > 0)) {
2125 nb_used = VIRTQUEUE_NUSED(vq);
2126 virtio_rmb(hw->weak_barriers);
2127 need = RTE_MIN(need, (int)nb_used);
2129 virtio_xmit_cleanup(vq, need);
2130 need = slots - vq->vq_free_cnt;
2131 if (unlikely(need > 0)) {
2133 "No free tx descriptors to transmit");
2138 /* Enqueue Packet buffers */
2139 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect,
2142 virtio_update_packet_stats(&txvq->stats, txm);
2145 txvq->stats.packets += nb_tx;
2147 if (likely(nb_tx)) {
2148 vq_update_avail_idx(vq);
2150 if (unlikely(virtqueue_kick_prepare(vq))) {
2151 virtqueue_notify(vq);
2152 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2159 static __rte_always_inline int
2160 virtio_xmit_try_cleanup_inorder(struct virtqueue *vq, uint16_t need)
2162 uint16_t nb_used, nb_clean, nb_descs;
2163 struct virtio_hw *hw = vq->hw;
2165 nb_descs = vq->vq_free_cnt + need;
2166 nb_used = VIRTQUEUE_NUSED(vq);
2167 virtio_rmb(hw->weak_barriers);
2168 nb_clean = RTE_MIN(need, (int)nb_used);
2170 virtio_xmit_cleanup_inorder(vq, nb_clean);
2172 return nb_descs - vq->vq_free_cnt;
2176 virtio_xmit_pkts_inorder(void *tx_queue,
2177 struct rte_mbuf **tx_pkts,
2180 struct virtnet_tx *txvq = tx_queue;
2181 struct virtqueue *vq = txvq->vq;
2182 struct virtio_hw *hw = vq->hw;
2183 uint16_t hdr_size = hw->vtnet_hdr_size;
2184 uint16_t nb_used, nb_tx = 0, nb_inorder_pkts = 0;
2185 struct rte_mbuf *inorder_pkts[nb_pkts];
2188 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2191 if (unlikely(nb_pkts < 1))
2195 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2196 nb_used = VIRTQUEUE_NUSED(vq);
2198 virtio_rmb(hw->weak_barriers);
2199 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2200 virtio_xmit_cleanup_inorder(vq, nb_used);
2202 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2203 struct rte_mbuf *txm = tx_pkts[nb_tx];
2206 /* optimize ring usage */
2207 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2208 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2209 rte_mbuf_refcnt_read(txm) == 1 &&
2210 RTE_MBUF_DIRECT(txm) &&
2211 txm->nb_segs == 1 &&
2212 rte_pktmbuf_headroom(txm) >= hdr_size &&
2213 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2214 __alignof__(struct virtio_net_hdr_mrg_rxbuf))) {
2215 inorder_pkts[nb_inorder_pkts] = txm;
2221 if (nb_inorder_pkts) {
2222 need = nb_inorder_pkts - vq->vq_free_cnt;
2223 if (unlikely(need > 0)) {
2224 need = virtio_xmit_try_cleanup_inorder(vq,
2226 if (unlikely(need > 0)) {
2228 "No free tx descriptors to "
2233 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2235 nb_inorder_pkts = 0;
2238 slots = txm->nb_segs + 1;
2239 need = slots - vq->vq_free_cnt;
2240 if (unlikely(need > 0)) {
2241 need = virtio_xmit_try_cleanup_inorder(vq, slots);
2243 if (unlikely(need > 0)) {
2245 "No free tx descriptors to transmit");
2249 /* Enqueue Packet buffers */
2250 virtqueue_enqueue_xmit(txvq, txm, slots, 0, 0, 1);
2252 virtio_update_packet_stats(&txvq->stats, txm);
2255 /* Transmit all inorder packets */
2256 if (nb_inorder_pkts) {
2257 need = nb_inorder_pkts - vq->vq_free_cnt;
2258 if (unlikely(need > 0)) {
2259 need = virtio_xmit_try_cleanup_inorder(vq,
2261 if (unlikely(need > 0)) {
2263 "No free tx descriptors to transmit");
2264 nb_inorder_pkts = vq->vq_free_cnt;
2269 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2273 txvq->stats.packets += nb_tx;
2275 if (likely(nb_tx)) {
2276 vq_update_avail_idx(vq);
2278 if (unlikely(virtqueue_kick_prepare(vq))) {
2279 virtqueue_notify(vq);
2280 PMD_TX_LOG(DEBUG, "Notified backend after xmit");