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 /* desc_is_used has a load-acquire or rte_cio_rmb inside
168 * and wait for used desc in virtqueue.
170 if (!desc_is_used(&desc[used_idx], vq))
172 len[i] = desc[used_idx].len;
173 id = desc[used_idx].id;
174 cookie = (struct rte_mbuf *)vq->vq_descx[id].cookie;
175 if (unlikely(cookie == NULL)) {
176 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
177 vq->vq_used_cons_idx);
180 rte_prefetch0(cookie);
181 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
185 vq->vq_used_cons_idx++;
186 if (vq->vq_used_cons_idx >= vq->vq_nentries) {
187 vq->vq_used_cons_idx -= vq->vq_nentries;
188 vq->vq_packed.used_wrap_counter ^= 1;
196 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
197 uint32_t *len, uint16_t num)
199 struct vring_used_elem *uep;
200 struct rte_mbuf *cookie;
201 uint16_t used_idx, desc_idx;
204 /* Caller does the check */
205 for (i = 0; i < num ; i++) {
206 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
207 uep = &vq->vq_split.ring.used->ring[used_idx];
208 desc_idx = (uint16_t) uep->id;
210 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
212 if (unlikely(cookie == NULL)) {
213 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
214 vq->vq_used_cons_idx);
218 rte_prefetch0(cookie);
219 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
221 vq->vq_used_cons_idx++;
222 vq_ring_free_chain(vq, desc_idx);
223 vq->vq_descx[desc_idx].cookie = NULL;
230 virtqueue_dequeue_rx_inorder(struct virtqueue *vq,
231 struct rte_mbuf **rx_pkts,
235 struct vring_used_elem *uep;
236 struct rte_mbuf *cookie;
237 uint16_t used_idx = 0;
240 if (unlikely(num == 0))
243 for (i = 0; i < num; i++) {
244 used_idx = vq->vq_used_cons_idx & (vq->vq_nentries - 1);
245 /* Desc idx same as used idx */
246 uep = &vq->vq_split.ring.used->ring[used_idx];
248 cookie = (struct rte_mbuf *)vq->vq_descx[used_idx].cookie;
250 if (unlikely(cookie == NULL)) {
251 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
252 vq->vq_used_cons_idx);
256 rte_prefetch0(cookie);
257 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
259 vq->vq_used_cons_idx++;
260 vq->vq_descx[used_idx].cookie = NULL;
263 vq_ring_free_inorder(vq, used_idx, i);
267 #ifndef DEFAULT_TX_FREE_THRESH
268 #define DEFAULT_TX_FREE_THRESH 32
272 virtio_xmit_cleanup_inorder_packed(struct virtqueue *vq, int num)
274 uint16_t used_idx, id, curr_id, free_cnt = 0;
275 uint16_t size = vq->vq_nentries;
276 struct vring_packed_desc *desc = vq->vq_packed.ring.desc;
277 struct vq_desc_extra *dxp;
279 used_idx = vq->vq_used_cons_idx;
280 /* desc_is_used has a load-acquire or rte_cio_rmb inside
281 * and wait for used desc in virtqueue.
283 while (num > 0 && desc_is_used(&desc[used_idx], vq)) {
284 id = desc[used_idx].id;
287 dxp = &vq->vq_descx[used_idx];
288 used_idx += dxp->ndescs;
289 free_cnt += dxp->ndescs;
291 if (used_idx >= size) {
293 vq->vq_packed.used_wrap_counter ^= 1;
295 if (dxp->cookie != NULL) {
296 rte_pktmbuf_free(dxp->cookie);
299 } while (curr_id != id);
301 vq->vq_used_cons_idx = used_idx;
302 vq->vq_free_cnt += free_cnt;
306 virtio_xmit_cleanup_normal_packed(struct virtqueue *vq, int num)
308 uint16_t used_idx, id;
309 uint16_t size = vq->vq_nentries;
310 struct vring_packed_desc *desc = vq->vq_packed.ring.desc;
311 struct vq_desc_extra *dxp;
313 used_idx = vq->vq_used_cons_idx;
314 /* desc_is_used has a load-acquire or rte_cio_rmb inside
315 * and wait for used desc in virtqueue.
317 while (num-- && desc_is_used(&desc[used_idx], vq)) {
318 id = desc[used_idx].id;
319 dxp = &vq->vq_descx[id];
320 vq->vq_used_cons_idx += dxp->ndescs;
321 if (vq->vq_used_cons_idx >= size) {
322 vq->vq_used_cons_idx -= size;
323 vq->vq_packed.used_wrap_counter ^= 1;
325 vq_ring_free_id_packed(vq, id);
326 if (dxp->cookie != NULL) {
327 rte_pktmbuf_free(dxp->cookie);
330 used_idx = vq->vq_used_cons_idx;
334 /* Cleanup from completed transmits. */
336 virtio_xmit_cleanup_packed(struct virtqueue *vq, int num, int in_order)
339 virtio_xmit_cleanup_inorder_packed(vq, num);
341 virtio_xmit_cleanup_normal_packed(vq, num);
345 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
347 uint16_t i, used_idx, desc_idx;
348 for (i = 0; i < num; i++) {
349 struct vring_used_elem *uep;
350 struct vq_desc_extra *dxp;
352 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
353 uep = &vq->vq_split.ring.used->ring[used_idx];
355 desc_idx = (uint16_t) uep->id;
356 dxp = &vq->vq_descx[desc_idx];
357 vq->vq_used_cons_idx++;
358 vq_ring_free_chain(vq, desc_idx);
360 if (dxp->cookie != NULL) {
361 rte_pktmbuf_free(dxp->cookie);
367 /* Cleanup from completed inorder transmits. */
368 static __rte_always_inline void
369 virtio_xmit_cleanup_inorder(struct virtqueue *vq, uint16_t num)
371 uint16_t i, idx = vq->vq_used_cons_idx;
372 int16_t free_cnt = 0;
373 struct vq_desc_extra *dxp = NULL;
375 if (unlikely(num == 0))
378 for (i = 0; i < num; i++) {
379 dxp = &vq->vq_descx[idx++ & (vq->vq_nentries - 1)];
380 free_cnt += dxp->ndescs;
381 if (dxp->cookie != NULL) {
382 rte_pktmbuf_free(dxp->cookie);
387 vq->vq_free_cnt += free_cnt;
388 vq->vq_used_cons_idx = idx;
392 virtqueue_enqueue_refill_inorder(struct virtqueue *vq,
393 struct rte_mbuf **cookies,
396 struct vq_desc_extra *dxp;
397 struct virtio_hw *hw = vq->hw;
398 struct vring_desc *start_dp;
399 uint16_t head_idx, idx, i = 0;
401 if (unlikely(vq->vq_free_cnt == 0))
403 if (unlikely(vq->vq_free_cnt < num))
406 head_idx = vq->vq_desc_head_idx & (vq->vq_nentries - 1);
407 start_dp = vq->vq_split.ring.desc;
410 idx = head_idx & (vq->vq_nentries - 1);
411 dxp = &vq->vq_descx[idx];
412 dxp->cookie = (void *)cookies[i];
416 VIRTIO_MBUF_ADDR(cookies[i], vq) +
417 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
419 cookies[i]->buf_len -
420 RTE_PKTMBUF_HEADROOM +
422 start_dp[idx].flags = VRING_DESC_F_WRITE;
424 vq_update_avail_ring(vq, idx);
429 vq->vq_desc_head_idx += num;
430 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
435 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf **cookie,
438 struct vq_desc_extra *dxp;
439 struct virtio_hw *hw = vq->hw;
440 struct vring_desc *start_dp = vq->vq_split.ring.desc;
443 if (unlikely(vq->vq_free_cnt == 0))
445 if (unlikely(vq->vq_free_cnt < num))
448 if (unlikely(vq->vq_desc_head_idx >= vq->vq_nentries))
451 for (i = 0; i < num; i++) {
452 idx = vq->vq_desc_head_idx;
453 dxp = &vq->vq_descx[idx];
454 dxp->cookie = (void *)cookie[i];
458 VIRTIO_MBUF_ADDR(cookie[i], vq) +
459 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
461 cookie[i]->buf_len - RTE_PKTMBUF_HEADROOM +
463 start_dp[idx].flags = VRING_DESC_F_WRITE;
464 vq->vq_desc_head_idx = start_dp[idx].next;
465 vq_update_avail_ring(vq, idx);
466 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END) {
467 vq->vq_desc_tail_idx = vq->vq_desc_head_idx;
472 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
478 virtqueue_enqueue_recv_refill_packed(struct virtqueue *vq,
479 struct rte_mbuf **cookie, uint16_t num)
481 struct vring_packed_desc *start_dp = vq->vq_packed.ring.desc;
482 uint16_t flags = vq->vq_packed.cached_flags;
483 struct virtio_hw *hw = vq->hw;
484 struct vq_desc_extra *dxp;
488 if (unlikely(vq->vq_free_cnt == 0))
490 if (unlikely(vq->vq_free_cnt < num))
493 for (i = 0; i < num; i++) {
494 idx = vq->vq_avail_idx;
495 dxp = &vq->vq_descx[idx];
496 dxp->cookie = (void *)cookie[i];
499 start_dp[idx].addr = VIRTIO_MBUF_ADDR(cookie[i], vq) +
500 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
501 start_dp[idx].len = cookie[i]->buf_len - RTE_PKTMBUF_HEADROOM
502 + hw->vtnet_hdr_size;
504 vq->vq_desc_head_idx = dxp->next;
505 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
506 vq->vq_desc_tail_idx = vq->vq_desc_head_idx;
508 virtqueue_store_flags_packed(&start_dp[idx], flags,
511 if (++vq->vq_avail_idx >= vq->vq_nentries) {
512 vq->vq_avail_idx -= vq->vq_nentries;
513 vq->vq_packed.cached_flags ^=
514 VRING_PACKED_DESC_F_AVAIL_USED;
515 flags = vq->vq_packed.cached_flags;
518 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
522 /* When doing TSO, the IP length is not included in the pseudo header
523 * checksum of the packet given to the PMD, but for virtio it is
527 virtio_tso_fix_cksum(struct rte_mbuf *m)
529 /* common case: header is not fragmented */
530 if (likely(rte_pktmbuf_data_len(m) >= m->l2_len + m->l3_len +
532 struct rte_ipv4_hdr *iph;
533 struct rte_ipv6_hdr *ip6h;
534 struct rte_tcp_hdr *th;
535 uint16_t prev_cksum, new_cksum, ip_len, ip_paylen;
538 iph = rte_pktmbuf_mtod_offset(m,
539 struct rte_ipv4_hdr *, m->l2_len);
540 th = RTE_PTR_ADD(iph, m->l3_len);
541 if ((iph->version_ihl >> 4) == 4) {
542 iph->hdr_checksum = 0;
543 iph->hdr_checksum = rte_ipv4_cksum(iph);
544 ip_len = iph->total_length;
545 ip_paylen = rte_cpu_to_be_16(rte_be_to_cpu_16(ip_len) -
548 ip6h = (struct rte_ipv6_hdr *)iph;
549 ip_paylen = ip6h->payload_len;
552 /* calculate the new phdr checksum not including ip_paylen */
553 prev_cksum = th->cksum;
556 tmp = (tmp & 0xffff) + (tmp >> 16);
559 /* replace it in the packet */
560 th->cksum = new_cksum;
565 /* avoid write operation when necessary, to lessen cache issues */
566 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
567 if ((var) != (val)) \
571 #define virtqueue_clear_net_hdr(_hdr) do { \
572 ASSIGN_UNLESS_EQUAL((_hdr)->csum_start, 0); \
573 ASSIGN_UNLESS_EQUAL((_hdr)->csum_offset, 0); \
574 ASSIGN_UNLESS_EQUAL((_hdr)->flags, 0); \
575 ASSIGN_UNLESS_EQUAL((_hdr)->gso_type, 0); \
576 ASSIGN_UNLESS_EQUAL((_hdr)->gso_size, 0); \
577 ASSIGN_UNLESS_EQUAL((_hdr)->hdr_len, 0); \
581 virtqueue_xmit_offload(struct virtio_net_hdr *hdr,
582 struct rte_mbuf *cookie,
586 if (cookie->ol_flags & PKT_TX_TCP_SEG)
587 cookie->ol_flags |= PKT_TX_TCP_CKSUM;
589 switch (cookie->ol_flags & PKT_TX_L4_MASK) {
590 case PKT_TX_UDP_CKSUM:
591 hdr->csum_start = cookie->l2_len + cookie->l3_len;
592 hdr->csum_offset = offsetof(struct rte_udp_hdr,
594 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
597 case PKT_TX_TCP_CKSUM:
598 hdr->csum_start = cookie->l2_len + cookie->l3_len;
599 hdr->csum_offset = offsetof(struct rte_tcp_hdr, cksum);
600 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
604 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
605 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
606 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
610 /* TCP Segmentation Offload */
611 if (cookie->ol_flags & PKT_TX_TCP_SEG) {
612 hdr->gso_type = (cookie->ol_flags & PKT_TX_IPV6) ?
613 VIRTIO_NET_HDR_GSO_TCPV6 :
614 VIRTIO_NET_HDR_GSO_TCPV4;
615 hdr->gso_size = cookie->tso_segsz;
621 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
622 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
623 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
629 virtqueue_enqueue_xmit_inorder(struct virtnet_tx *txvq,
630 struct rte_mbuf **cookies,
633 struct vq_desc_extra *dxp;
634 struct virtqueue *vq = txvq->vq;
635 struct vring_desc *start_dp;
636 struct virtio_net_hdr *hdr;
638 uint16_t head_size = vq->hw->vtnet_hdr_size;
641 idx = vq->vq_desc_head_idx;
642 start_dp = vq->vq_split.ring.desc;
645 idx = idx & (vq->vq_nentries - 1);
646 dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
647 dxp->cookie = (void *)cookies[i];
649 virtio_update_packet_stats(&txvq->stats, cookies[i]);
651 hdr = (struct virtio_net_hdr *)(char *)cookies[i]->buf_addr +
652 cookies[i]->data_off - head_size;
654 /* if offload disabled, hdr is not zeroed yet, do it now */
655 if (!vq->hw->has_tx_offload)
656 virtqueue_clear_net_hdr(hdr);
658 virtqueue_xmit_offload(hdr, cookies[i], true);
661 VIRTIO_MBUF_DATA_DMA_ADDR(cookies[i], vq) - head_size;
662 start_dp[idx].len = cookies[i]->data_len + head_size;
663 start_dp[idx].flags = 0;
666 vq_update_avail_ring(vq, idx);
672 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
673 vq->vq_desc_head_idx = idx & (vq->vq_nentries - 1);
677 virtqueue_enqueue_xmit_packed_fast(struct virtnet_tx *txvq,
678 struct rte_mbuf *cookie,
681 struct virtqueue *vq = txvq->vq;
682 struct vring_packed_desc *dp;
683 struct vq_desc_extra *dxp;
684 uint16_t idx, id, flags;
685 uint16_t head_size = vq->hw->vtnet_hdr_size;
686 struct virtio_net_hdr *hdr;
688 id = in_order ? vq->vq_avail_idx : vq->vq_desc_head_idx;
689 idx = vq->vq_avail_idx;
690 dp = &vq->vq_packed.ring.desc[idx];
692 dxp = &vq->vq_descx[id];
694 dxp->cookie = cookie;
696 flags = vq->vq_packed.cached_flags;
698 /* prepend cannot fail, checked by caller */
699 hdr = (struct virtio_net_hdr *)(char *)cookie->buf_addr +
700 cookie->data_off - head_size;
702 /* if offload disabled, hdr is not zeroed yet, do it now */
703 if (!vq->hw->has_tx_offload)
704 virtqueue_clear_net_hdr(hdr);
706 virtqueue_xmit_offload(hdr, cookie, true);
708 dp->addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq) - head_size;
709 dp->len = cookie->data_len + head_size;
712 if (++vq->vq_avail_idx >= vq->vq_nentries) {
713 vq->vq_avail_idx -= vq->vq_nentries;
714 vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
720 vq->vq_desc_head_idx = dxp->next;
721 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
722 vq->vq_desc_tail_idx = VQ_RING_DESC_CHAIN_END;
725 virtqueue_store_flags_packed(dp, flags, vq->hw->weak_barriers);
729 virtqueue_enqueue_xmit_packed(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
730 uint16_t needed, int can_push, int in_order)
732 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
733 struct vq_desc_extra *dxp;
734 struct virtqueue *vq = txvq->vq;
735 struct vring_packed_desc *start_dp, *head_dp;
736 uint16_t idx, id, head_idx, head_flags;
737 uint16_t head_size = vq->hw->vtnet_hdr_size;
738 struct virtio_net_hdr *hdr;
740 bool prepend_header = false;
742 id = in_order ? vq->vq_avail_idx : vq->vq_desc_head_idx;
744 dxp = &vq->vq_descx[id];
745 dxp->ndescs = needed;
746 dxp->cookie = cookie;
748 head_idx = vq->vq_avail_idx;
751 start_dp = vq->vq_packed.ring.desc;
753 head_dp = &vq->vq_packed.ring.desc[idx];
754 head_flags = cookie->next ? VRING_DESC_F_NEXT : 0;
755 head_flags |= vq->vq_packed.cached_flags;
758 /* prepend cannot fail, checked by caller */
759 hdr = (struct virtio_net_hdr *)(char *)cookie->buf_addr +
760 cookie->data_off - head_size;
761 prepend_header = true;
763 /* if offload disabled, it is not zeroed below, do it now */
764 if (!vq->hw->has_tx_offload)
765 virtqueue_clear_net_hdr(hdr);
767 /* setup first tx ring slot to point to header
768 * stored in reserved region.
770 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
771 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
772 start_dp[idx].len = vq->hw->vtnet_hdr_size;
773 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
775 if (idx >= vq->vq_nentries) {
776 idx -= vq->vq_nentries;
777 vq->vq_packed.cached_flags ^=
778 VRING_PACKED_DESC_F_AVAIL_USED;
782 virtqueue_xmit_offload(hdr, cookie, vq->hw->has_tx_offload);
787 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
788 start_dp[idx].len = cookie->data_len;
789 if (prepend_header) {
790 start_dp[idx].addr -= head_size;
791 start_dp[idx].len += head_size;
792 prepend_header = false;
795 if (likely(idx != head_idx)) {
796 flags = cookie->next ? VRING_DESC_F_NEXT : 0;
797 flags |= vq->vq_packed.cached_flags;
798 start_dp[idx].flags = flags;
802 if (idx >= vq->vq_nentries) {
803 idx -= vq->vq_nentries;
804 vq->vq_packed.cached_flags ^=
805 VRING_PACKED_DESC_F_AVAIL_USED;
807 } while ((cookie = cookie->next) != NULL);
809 start_dp[prev].id = id;
811 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
812 vq->vq_avail_idx = idx;
815 vq->vq_desc_head_idx = dxp->next;
816 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
817 vq->vq_desc_tail_idx = VQ_RING_DESC_CHAIN_END;
820 virtqueue_store_flags_packed(head_dp, head_flags,
821 vq->hw->weak_barriers);
825 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
826 uint16_t needed, int use_indirect, int can_push,
829 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
830 struct vq_desc_extra *dxp;
831 struct virtqueue *vq = txvq->vq;
832 struct vring_desc *start_dp;
833 uint16_t seg_num = cookie->nb_segs;
834 uint16_t head_idx, idx;
835 uint16_t head_size = vq->hw->vtnet_hdr_size;
836 bool prepend_header = false;
837 struct virtio_net_hdr *hdr;
839 head_idx = vq->vq_desc_head_idx;
842 dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
844 dxp = &vq->vq_descx[idx];
845 dxp->cookie = (void *)cookie;
846 dxp->ndescs = needed;
848 start_dp = vq->vq_split.ring.desc;
851 /* prepend cannot fail, checked by caller */
852 hdr = (struct virtio_net_hdr *)(char *)cookie->buf_addr +
853 cookie->data_off - head_size;
854 prepend_header = true;
856 /* if offload disabled, it is not zeroed below, do it now */
857 if (!vq->hw->has_tx_offload)
858 virtqueue_clear_net_hdr(hdr);
859 } else if (use_indirect) {
860 /* setup tx ring slot to point to indirect
861 * descriptor list stored in reserved region.
863 * the first slot in indirect ring is already preset
864 * to point to the header in reserved region
866 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
867 RTE_PTR_DIFF(&txr[idx].tx_indir, txr);
868 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
869 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
870 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
872 /* loop below will fill in rest of the indirect elements */
873 start_dp = txr[idx].tx_indir;
876 /* setup first tx ring slot to point to header
877 * stored in reserved region.
879 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
880 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
881 start_dp[idx].len = vq->hw->vtnet_hdr_size;
882 start_dp[idx].flags = VRING_DESC_F_NEXT;
883 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
885 idx = start_dp[idx].next;
888 virtqueue_xmit_offload(hdr, cookie, vq->hw->has_tx_offload);
891 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
892 start_dp[idx].len = cookie->data_len;
893 if (prepend_header) {
894 start_dp[idx].addr -= head_size;
895 start_dp[idx].len += head_size;
896 prepend_header = false;
898 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
899 idx = start_dp[idx].next;
900 } while ((cookie = cookie->next) != NULL);
903 idx = vq->vq_split.ring.desc[head_idx].next;
905 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
907 vq->vq_desc_head_idx = idx;
908 vq_update_avail_ring(vq, head_idx);
911 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
912 vq->vq_desc_tail_idx = idx;
917 virtio_dev_cq_start(struct rte_eth_dev *dev)
919 struct virtio_hw *hw = dev->data->dev_private;
921 if (hw->cvq && hw->cvq->vq) {
922 rte_spinlock_init(&hw->cvq->lock);
923 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
928 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
931 unsigned int socket_id __rte_unused,
932 const struct rte_eth_rxconf *rx_conf __rte_unused,
933 struct rte_mempool *mp)
935 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
936 struct virtio_hw *hw = dev->data->dev_private;
937 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
938 struct virtnet_rx *rxvq;
940 PMD_INIT_FUNC_TRACE();
942 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
943 nb_desc = vq->vq_nentries;
944 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
947 rxvq->queue_id = queue_idx;
949 dev->data->rx_queues[queue_idx] = rxvq;
955 virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
957 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
958 struct virtio_hw *hw = dev->data->dev_private;
959 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
960 struct virtnet_rx *rxvq = &vq->rxq;
965 PMD_INIT_FUNC_TRACE();
967 /* Allocate blank mbufs for the each rx descriptor */
970 if (hw->use_simple_rx) {
971 for (desc_idx = 0; desc_idx < vq->vq_nentries;
973 vq->vq_split.ring.avail->ring[desc_idx] = desc_idx;
974 vq->vq_split.ring.desc[desc_idx].flags =
978 virtio_rxq_vec_setup(rxvq);
981 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
982 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
984 vq->sw_ring[vq->vq_nentries + desc_idx] =
988 if (hw->use_simple_rx) {
989 while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
990 virtio_rxq_rearm_vec(rxvq);
991 nbufs += RTE_VIRTIO_VPMD_RX_REARM_THRESH;
993 } else if (hw->use_inorder_rx) {
994 if ((!virtqueue_full(vq))) {
995 uint16_t free_cnt = vq->vq_free_cnt;
996 struct rte_mbuf *pkts[free_cnt];
998 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, pkts,
1000 error = virtqueue_enqueue_refill_inorder(vq,
1003 if (unlikely(error)) {
1004 for (i = 0; i < free_cnt; i++)
1005 rte_pktmbuf_free(pkts[i]);
1010 vq_update_avail_idx(vq);
1013 while (!virtqueue_full(vq)) {
1014 m = rte_mbuf_raw_alloc(rxvq->mpool);
1018 /* Enqueue allocated buffers */
1019 if (vtpci_packed_queue(vq->hw))
1020 error = virtqueue_enqueue_recv_refill_packed(vq,
1023 error = virtqueue_enqueue_recv_refill(vq,
1026 rte_pktmbuf_free(m);
1032 if (!vtpci_packed_queue(vq->hw))
1033 vq_update_avail_idx(vq);
1036 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
1044 * struct rte_eth_dev *dev: Used to update dev
1045 * uint16_t nb_desc: Defaults to values read from config space
1046 * unsigned int socket_id: Used to allocate memzone
1047 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
1048 * uint16_t queue_idx: Just used as an index in dev txq list
1051 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
1054 unsigned int socket_id __rte_unused,
1055 const struct rte_eth_txconf *tx_conf)
1057 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1058 struct virtio_hw *hw = dev->data->dev_private;
1059 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1060 struct virtnet_tx *txvq;
1061 uint16_t tx_free_thresh;
1063 PMD_INIT_FUNC_TRACE();
1065 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
1066 nb_desc = vq->vq_nentries;
1067 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
1070 txvq->queue_id = queue_idx;
1072 tx_free_thresh = tx_conf->tx_free_thresh;
1073 if (tx_free_thresh == 0)
1075 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
1077 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
1078 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
1079 "number of TX entries minus 3 (%u)."
1080 " (tx_free_thresh=%u port=%u queue=%u)\n",
1081 vq->vq_nentries - 3,
1082 tx_free_thresh, dev->data->port_id, queue_idx);
1086 vq->vq_free_thresh = tx_free_thresh;
1088 dev->data->tx_queues[queue_idx] = txvq;
1093 virtio_dev_tx_queue_setup_finish(struct rte_eth_dev *dev,
1096 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1097 struct virtio_hw *hw = dev->data->dev_private;
1098 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1100 PMD_INIT_FUNC_TRACE();
1102 if (!vtpci_packed_queue(hw)) {
1103 if (hw->use_inorder_tx)
1104 vq->vq_split.ring.desc[vq->vq_nentries - 1].next = 0;
1113 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
1117 * Requeue the discarded mbuf. This should always be
1118 * successful since it was just dequeued.
1120 if (vtpci_packed_queue(vq->hw))
1121 error = virtqueue_enqueue_recv_refill_packed(vq, &m, 1);
1123 error = virtqueue_enqueue_recv_refill(vq, &m, 1);
1125 if (unlikely(error)) {
1126 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
1127 rte_pktmbuf_free(m);
1132 virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)
1136 error = virtqueue_enqueue_refill_inorder(vq, &m, 1);
1137 if (unlikely(error)) {
1138 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
1139 rte_pktmbuf_free(m);
1143 /* Optionally fill offload information in structure */
1145 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
1147 struct rte_net_hdr_lens hdr_lens;
1148 uint32_t hdrlen, ptype;
1149 int l4_supported = 0;
1152 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1155 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
1157 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
1158 m->packet_type = ptype;
1159 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
1160 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
1161 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
1164 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1165 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
1166 if (hdr->csum_start <= hdrlen && l4_supported) {
1167 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
1169 /* Unknown proto or tunnel, do sw cksum. We can assume
1170 * the cksum field is in the first segment since the
1171 * buffers we provided to the host are large enough.
1172 * In case of SCTP, this will be wrong since it's a CRC
1173 * but there's nothing we can do.
1175 uint16_t csum = 0, off;
1177 rte_raw_cksum_mbuf(m, hdr->csum_start,
1178 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
1180 if (likely(csum != 0xffff))
1182 off = hdr->csum_offset + hdr->csum_start;
1183 if (rte_pktmbuf_data_len(m) >= off + 1)
1184 *rte_pktmbuf_mtod_offset(m, uint16_t *,
1187 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
1188 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
1191 /* GSO request, save required information in mbuf */
1192 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1193 /* Check unsupported modes */
1194 if ((hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) ||
1195 (hdr->gso_size == 0)) {
1199 /* Update mss lengthes in mbuf */
1200 m->tso_segsz = hdr->gso_size;
1201 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1202 case VIRTIO_NET_HDR_GSO_TCPV4:
1203 case VIRTIO_NET_HDR_GSO_TCPV6:
1204 m->ol_flags |= PKT_RX_LRO | \
1205 PKT_RX_L4_CKSUM_NONE;
1215 #define VIRTIO_MBUF_BURST_SZ 64
1216 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
1218 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1220 struct virtnet_rx *rxvq = rx_queue;
1221 struct virtqueue *vq = rxvq->vq;
1222 struct virtio_hw *hw = vq->hw;
1223 struct rte_mbuf *rxm;
1224 uint16_t nb_used, num, nb_rx;
1225 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1226 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1228 uint32_t i, nb_enqueued;
1230 struct virtio_net_hdr *hdr;
1233 if (unlikely(hw->started == 0))
1236 nb_used = VIRTQUEUE_NUSED(vq);
1238 virtio_rmb(hw->weak_barriers);
1240 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1241 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1242 num = VIRTIO_MBUF_BURST_SZ;
1243 if (likely(num > DESC_PER_CACHELINE))
1244 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1246 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1247 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
1250 hdr_size = hw->vtnet_hdr_size;
1252 for (i = 0; i < num ; i++) {
1255 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1257 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1258 PMD_RX_LOG(ERR, "Packet drop");
1260 virtio_discard_rxbuf(vq, rxm);
1261 rxvq->stats.errors++;
1265 rxm->port = rxvq->port_id;
1266 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1270 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1271 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1273 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1274 RTE_PKTMBUF_HEADROOM - hdr_size);
1277 rte_vlan_strip(rxm);
1279 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1280 virtio_discard_rxbuf(vq, rxm);
1281 rxvq->stats.errors++;
1285 virtio_rx_stats_updated(rxvq, rxm);
1287 rx_pkts[nb_rx++] = rxm;
1290 rxvq->stats.packets += nb_rx;
1292 /* Allocate new mbuf for the used descriptor */
1293 if (likely(!virtqueue_full(vq))) {
1294 uint16_t free_cnt = vq->vq_free_cnt;
1295 struct rte_mbuf *new_pkts[free_cnt];
1297 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1299 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1301 if (unlikely(error)) {
1302 for (i = 0; i < free_cnt; i++)
1303 rte_pktmbuf_free(new_pkts[i]);
1305 nb_enqueued += free_cnt;
1307 struct rte_eth_dev *dev =
1308 &rte_eth_devices[rxvq->port_id];
1309 dev->data->rx_mbuf_alloc_failed += free_cnt;
1313 if (likely(nb_enqueued)) {
1314 vq_update_avail_idx(vq);
1316 if (unlikely(virtqueue_kick_prepare(vq))) {
1317 virtqueue_notify(vq);
1318 PMD_RX_LOG(DEBUG, "Notified");
1326 virtio_recv_pkts_packed(void *rx_queue, struct rte_mbuf **rx_pkts,
1329 struct virtnet_rx *rxvq = rx_queue;
1330 struct virtqueue *vq = rxvq->vq;
1331 struct virtio_hw *hw = vq->hw;
1332 struct rte_mbuf *rxm;
1333 uint16_t num, nb_rx;
1334 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1335 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1337 uint32_t i, nb_enqueued;
1339 struct virtio_net_hdr *hdr;
1342 if (unlikely(hw->started == 0))
1345 num = RTE_MIN(VIRTIO_MBUF_BURST_SZ, nb_pkts);
1346 if (likely(num > DESC_PER_CACHELINE))
1347 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1349 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1350 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1353 hdr_size = hw->vtnet_hdr_size;
1355 for (i = 0; i < num; i++) {
1358 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1360 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1361 PMD_RX_LOG(ERR, "Packet drop");
1363 virtio_discard_rxbuf(vq, rxm);
1364 rxvq->stats.errors++;
1368 rxm->port = rxvq->port_id;
1369 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1373 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1374 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1376 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1377 RTE_PKTMBUF_HEADROOM - hdr_size);
1380 rte_vlan_strip(rxm);
1382 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1383 virtio_discard_rxbuf(vq, rxm);
1384 rxvq->stats.errors++;
1388 virtio_rx_stats_updated(rxvq, rxm);
1390 rx_pkts[nb_rx++] = rxm;
1393 rxvq->stats.packets += nb_rx;
1395 /* Allocate new mbuf for the used descriptor */
1396 if (likely(!virtqueue_full(vq))) {
1397 uint16_t free_cnt = vq->vq_free_cnt;
1398 struct rte_mbuf *new_pkts[free_cnt];
1400 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1402 error = virtqueue_enqueue_recv_refill_packed(vq,
1403 new_pkts, free_cnt);
1404 if (unlikely(error)) {
1405 for (i = 0; i < free_cnt; i++)
1406 rte_pktmbuf_free(new_pkts[i]);
1408 nb_enqueued += free_cnt;
1410 struct rte_eth_dev *dev =
1411 &rte_eth_devices[rxvq->port_id];
1412 dev->data->rx_mbuf_alloc_failed += free_cnt;
1416 if (likely(nb_enqueued)) {
1417 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1418 virtqueue_notify(vq);
1419 PMD_RX_LOG(DEBUG, "Notified");
1428 virtio_recv_pkts_inorder(void *rx_queue,
1429 struct rte_mbuf **rx_pkts,
1432 struct virtnet_rx *rxvq = rx_queue;
1433 struct virtqueue *vq = rxvq->vq;
1434 struct virtio_hw *hw = vq->hw;
1435 struct rte_mbuf *rxm;
1436 struct rte_mbuf *prev = NULL;
1437 uint16_t nb_used, num, nb_rx;
1438 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1439 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1441 uint32_t nb_enqueued;
1448 if (unlikely(hw->started == 0))
1451 nb_used = VIRTQUEUE_NUSED(vq);
1452 nb_used = RTE_MIN(nb_used, nb_pkts);
1453 nb_used = RTE_MIN(nb_used, VIRTIO_MBUF_BURST_SZ);
1455 virtio_rmb(hw->weak_barriers);
1457 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1462 hdr_size = hw->vtnet_hdr_size;
1464 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len, nb_used);
1466 for (i = 0; i < num; i++) {
1467 struct virtio_net_hdr_mrg_rxbuf *header;
1469 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1470 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1474 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1475 PMD_RX_LOG(ERR, "Packet drop");
1477 virtio_discard_rxbuf_inorder(vq, rxm);
1478 rxvq->stats.errors++;
1482 header = (struct virtio_net_hdr_mrg_rxbuf *)
1483 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1486 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1487 seg_num = header->num_buffers;
1494 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1495 rxm->nb_segs = seg_num;
1498 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1499 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1501 rxm->port = rxvq->port_id;
1503 rx_pkts[nb_rx] = rxm;
1506 if (vq->hw->has_rx_offload &&
1507 virtio_rx_offload(rxm, &header->hdr) < 0) {
1508 virtio_discard_rxbuf_inorder(vq, rxm);
1509 rxvq->stats.errors++;
1514 rte_vlan_strip(rx_pkts[nb_rx]);
1516 seg_res = seg_num - 1;
1518 /* Merge remaining segments */
1519 while (seg_res != 0 && i < (num - 1)) {
1523 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1524 rxm->pkt_len = (uint32_t)(len[i]);
1525 rxm->data_len = (uint16_t)(len[i]);
1527 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1535 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1540 /* Last packet still need merge segments */
1541 while (seg_res != 0) {
1542 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1543 VIRTIO_MBUF_BURST_SZ);
1545 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1546 virtio_rmb(hw->weak_barriers);
1547 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len,
1549 uint16_t extra_idx = 0;
1552 while (extra_idx < rcv_cnt) {
1553 rxm = rcv_pkts[extra_idx];
1555 RTE_PKTMBUF_HEADROOM - hdr_size;
1556 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1557 rxm->data_len = (uint16_t)(len[extra_idx]);
1560 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1566 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1571 "No enough segments for packet.");
1572 rte_pktmbuf_free(rx_pkts[nb_rx]);
1573 rxvq->stats.errors++;
1578 rxvq->stats.packets += nb_rx;
1580 /* Allocate new mbuf for the used descriptor */
1582 if (likely(!virtqueue_full(vq))) {
1583 /* free_cnt may include mrg descs */
1584 uint16_t free_cnt = vq->vq_free_cnt;
1585 struct rte_mbuf *new_pkts[free_cnt];
1587 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1588 error = virtqueue_enqueue_refill_inorder(vq, new_pkts,
1590 if (unlikely(error)) {
1591 for (i = 0; i < free_cnt; i++)
1592 rte_pktmbuf_free(new_pkts[i]);
1594 nb_enqueued += free_cnt;
1596 struct rte_eth_dev *dev =
1597 &rte_eth_devices[rxvq->port_id];
1598 dev->data->rx_mbuf_alloc_failed += free_cnt;
1602 if (likely(nb_enqueued)) {
1603 vq_update_avail_idx(vq);
1605 if (unlikely(virtqueue_kick_prepare(vq))) {
1606 virtqueue_notify(vq);
1607 PMD_RX_LOG(DEBUG, "Notified");
1615 virtio_recv_mergeable_pkts(void *rx_queue,
1616 struct rte_mbuf **rx_pkts,
1619 struct virtnet_rx *rxvq = rx_queue;
1620 struct virtqueue *vq = rxvq->vq;
1621 struct virtio_hw *hw = vq->hw;
1622 struct rte_mbuf *rxm;
1623 struct rte_mbuf *prev = NULL;
1624 uint16_t nb_used, num, nb_rx = 0;
1625 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1626 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1628 uint32_t nb_enqueued = 0;
1629 uint32_t seg_num = 0;
1630 uint32_t seg_res = 0;
1631 uint32_t hdr_size = hw->vtnet_hdr_size;
1634 if (unlikely(hw->started == 0))
1637 nb_used = VIRTQUEUE_NUSED(vq);
1639 virtio_rmb(hw->weak_barriers);
1641 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1643 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1644 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1645 num = VIRTIO_MBUF_BURST_SZ;
1646 if (likely(num > DESC_PER_CACHELINE))
1647 num = num - ((vq->vq_used_cons_idx + num) %
1648 DESC_PER_CACHELINE);
1651 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1653 for (i = 0; i < num; i++) {
1654 struct virtio_net_hdr_mrg_rxbuf *header;
1656 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1657 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1661 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1662 PMD_RX_LOG(ERR, "Packet drop");
1664 virtio_discard_rxbuf(vq, rxm);
1665 rxvq->stats.errors++;
1669 header = (struct virtio_net_hdr_mrg_rxbuf *)
1670 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1672 seg_num = header->num_buffers;
1676 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1677 rxm->nb_segs = seg_num;
1680 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1681 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1683 rxm->port = rxvq->port_id;
1685 rx_pkts[nb_rx] = rxm;
1688 if (hw->has_rx_offload &&
1689 virtio_rx_offload(rxm, &header->hdr) < 0) {
1690 virtio_discard_rxbuf(vq, rxm);
1691 rxvq->stats.errors++;
1696 rte_vlan_strip(rx_pkts[nb_rx]);
1698 seg_res = seg_num - 1;
1700 /* Merge remaining segments */
1701 while (seg_res != 0 && i < (num - 1)) {
1705 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1706 rxm->pkt_len = (uint32_t)(len[i]);
1707 rxm->data_len = (uint16_t)(len[i]);
1709 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1717 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1722 /* Last packet still need merge segments */
1723 while (seg_res != 0) {
1724 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1725 VIRTIO_MBUF_BURST_SZ);
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];
1748 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1753 "No enough segments for packet.");
1754 rte_pktmbuf_free(rx_pkts[nb_rx]);
1755 rxvq->stats.errors++;
1760 rxvq->stats.packets += nb_rx;
1762 /* Allocate new mbuf for the used descriptor */
1763 if (likely(!virtqueue_full(vq))) {
1764 /* free_cnt may include mrg descs */
1765 uint16_t free_cnt = vq->vq_free_cnt;
1766 struct rte_mbuf *new_pkts[free_cnt];
1768 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1769 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1771 if (unlikely(error)) {
1772 for (i = 0; i < free_cnt; i++)
1773 rte_pktmbuf_free(new_pkts[i]);
1775 nb_enqueued += free_cnt;
1777 struct rte_eth_dev *dev =
1778 &rte_eth_devices[rxvq->port_id];
1779 dev->data->rx_mbuf_alloc_failed += free_cnt;
1783 if (likely(nb_enqueued)) {
1784 vq_update_avail_idx(vq);
1786 if (unlikely(virtqueue_kick_prepare(vq))) {
1787 virtqueue_notify(vq);
1788 PMD_RX_LOG(DEBUG, "Notified");
1796 virtio_recv_mergeable_pkts_packed(void *rx_queue,
1797 struct rte_mbuf **rx_pkts,
1800 struct virtnet_rx *rxvq = rx_queue;
1801 struct virtqueue *vq = rxvq->vq;
1802 struct virtio_hw *hw = vq->hw;
1803 struct rte_mbuf *rxm;
1804 struct rte_mbuf *prev = NULL;
1805 uint16_t num, nb_rx = 0;
1806 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1807 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1808 uint32_t nb_enqueued = 0;
1809 uint32_t seg_num = 0;
1810 uint32_t seg_res = 0;
1811 uint32_t hdr_size = hw->vtnet_hdr_size;
1815 if (unlikely(hw->started == 0))
1820 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1821 num = VIRTIO_MBUF_BURST_SZ;
1822 if (likely(num > DESC_PER_CACHELINE))
1823 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1825 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1827 for (i = 0; i < num; i++) {
1828 struct virtio_net_hdr_mrg_rxbuf *header;
1830 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1831 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1835 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1836 PMD_RX_LOG(ERR, "Packet drop");
1838 virtio_discard_rxbuf(vq, rxm);
1839 rxvq->stats.errors++;
1843 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)
1844 rxm->buf_addr + RTE_PKTMBUF_HEADROOM - hdr_size);
1845 seg_num = header->num_buffers;
1850 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1851 rxm->nb_segs = seg_num;
1854 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1855 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1857 rxm->port = rxvq->port_id;
1858 rx_pkts[nb_rx] = rxm;
1861 if (hw->has_rx_offload &&
1862 virtio_rx_offload(rxm, &header->hdr) < 0) {
1863 virtio_discard_rxbuf(vq, rxm);
1864 rxvq->stats.errors++;
1869 rte_vlan_strip(rx_pkts[nb_rx]);
1871 seg_res = seg_num - 1;
1873 /* Merge remaining segments */
1874 while (seg_res != 0 && i < (num - 1)) {
1878 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1879 rxm->pkt_len = (uint32_t)(len[i]);
1880 rxm->data_len = (uint16_t)(len[i]);
1882 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1890 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1895 /* Last packet still need merge segments */
1896 while (seg_res != 0) {
1897 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1898 VIRTIO_MBUF_BURST_SZ);
1899 uint16_t extra_idx = 0;
1901 rcv_cnt = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts,
1903 if (unlikely(rcv_cnt == 0)) {
1904 PMD_RX_LOG(ERR, "No enough segments for packet.");
1905 rte_pktmbuf_free(rx_pkts[nb_rx]);
1906 rxvq->stats.errors++;
1910 while (extra_idx < rcv_cnt) {
1911 rxm = rcv_pkts[extra_idx];
1913 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1914 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1915 rxm->data_len = (uint16_t)(len[extra_idx]);
1919 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1924 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1929 rxvq->stats.packets += nb_rx;
1931 /* Allocate new mbuf for the used descriptor */
1932 if (likely(!virtqueue_full(vq))) {
1933 /* free_cnt may include mrg descs */
1934 uint16_t free_cnt = vq->vq_free_cnt;
1935 struct rte_mbuf *new_pkts[free_cnt];
1937 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1938 error = virtqueue_enqueue_recv_refill_packed(vq,
1939 new_pkts, free_cnt);
1940 if (unlikely(error)) {
1941 for (i = 0; i < free_cnt; i++)
1942 rte_pktmbuf_free(new_pkts[i]);
1944 nb_enqueued += free_cnt;
1946 struct rte_eth_dev *dev =
1947 &rte_eth_devices[rxvq->port_id];
1948 dev->data->rx_mbuf_alloc_failed += free_cnt;
1952 if (likely(nb_enqueued)) {
1953 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1954 virtqueue_notify(vq);
1955 PMD_RX_LOG(DEBUG, "Notified");
1963 virtio_xmit_pkts_prepare(void *tx_queue __rte_unused, struct rte_mbuf **tx_pkts,
1969 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1970 struct rte_mbuf *m = tx_pkts[nb_tx];
1972 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1973 error = rte_validate_tx_offload(m);
1974 if (unlikely(error)) {
1980 /* Do VLAN tag insertion */
1981 if (unlikely(m->ol_flags & PKT_TX_VLAN_PKT)) {
1982 error = rte_vlan_insert(&m);
1983 /* rte_vlan_insert() may change pointer
1984 * even in the case of failure
1988 if (unlikely(error)) {
1994 error = rte_net_intel_cksum_prepare(m);
1995 if (unlikely(error)) {
2000 if (m->ol_flags & PKT_TX_TCP_SEG)
2001 virtio_tso_fix_cksum(m);
2008 virtio_xmit_pkts_packed(void *tx_queue, struct rte_mbuf **tx_pkts,
2011 struct virtnet_tx *txvq = tx_queue;
2012 struct virtqueue *vq = txvq->vq;
2013 struct virtio_hw *hw = vq->hw;
2014 uint16_t hdr_size = hw->vtnet_hdr_size;
2016 bool in_order = hw->use_inorder_tx;
2018 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2021 if (unlikely(nb_pkts < 1))
2024 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2026 if (nb_pkts > vq->vq_free_cnt)
2027 virtio_xmit_cleanup_packed(vq, nb_pkts - vq->vq_free_cnt,
2030 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2031 struct rte_mbuf *txm = tx_pkts[nb_tx];
2032 int can_push = 0, slots, need;
2034 /* optimize ring usage */
2035 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2036 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2037 rte_mbuf_refcnt_read(txm) == 1 &&
2038 RTE_MBUF_DIRECT(txm) &&
2039 txm->nb_segs == 1 &&
2040 rte_pktmbuf_headroom(txm) >= hdr_size &&
2041 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2042 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2045 /* How many main ring entries are needed to this Tx?
2046 * any_layout => number of segments
2047 * default => number of segments + 1
2049 slots = txm->nb_segs + !can_push;
2050 need = slots - vq->vq_free_cnt;
2052 /* Positive value indicates it need free vring descriptors */
2053 if (unlikely(need > 0)) {
2054 virtio_xmit_cleanup_packed(vq, need, in_order);
2055 need = slots - vq->vq_free_cnt;
2056 if (unlikely(need > 0)) {
2058 "No free tx descriptors to transmit");
2063 /* Enqueue Packet buffers */
2065 virtqueue_enqueue_xmit_packed_fast(txvq, txm, in_order);
2067 virtqueue_enqueue_xmit_packed(txvq, txm, slots, 0,
2070 virtio_update_packet_stats(&txvq->stats, txm);
2073 txvq->stats.packets += nb_tx;
2075 if (likely(nb_tx)) {
2076 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
2077 virtqueue_notify(vq);
2078 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2086 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
2088 struct virtnet_tx *txvq = tx_queue;
2089 struct virtqueue *vq = txvq->vq;
2090 struct virtio_hw *hw = vq->hw;
2091 uint16_t hdr_size = hw->vtnet_hdr_size;
2092 uint16_t nb_used, nb_tx = 0;
2094 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2097 if (unlikely(nb_pkts < 1))
2100 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2101 nb_used = VIRTQUEUE_NUSED(vq);
2103 virtio_rmb(hw->weak_barriers);
2104 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2105 virtio_xmit_cleanup(vq, nb_used);
2107 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2108 struct rte_mbuf *txm = tx_pkts[nb_tx];
2109 int can_push = 0, use_indirect = 0, slots, need;
2111 /* optimize ring usage */
2112 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2113 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2114 rte_mbuf_refcnt_read(txm) == 1 &&
2115 RTE_MBUF_DIRECT(txm) &&
2116 txm->nb_segs == 1 &&
2117 rte_pktmbuf_headroom(txm) >= hdr_size &&
2118 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2119 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2121 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
2122 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
2125 /* How many main ring entries are needed to this Tx?
2126 * any_layout => number of segments
2128 * default => number of segments + 1
2130 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
2131 need = slots - vq->vq_free_cnt;
2133 /* Positive value indicates it need free vring descriptors */
2134 if (unlikely(need > 0)) {
2135 nb_used = VIRTQUEUE_NUSED(vq);
2136 virtio_rmb(hw->weak_barriers);
2137 need = RTE_MIN(need, (int)nb_used);
2139 virtio_xmit_cleanup(vq, need);
2140 need = slots - vq->vq_free_cnt;
2141 if (unlikely(need > 0)) {
2143 "No free tx descriptors to transmit");
2148 /* Enqueue Packet buffers */
2149 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect,
2152 virtio_update_packet_stats(&txvq->stats, txm);
2155 txvq->stats.packets += nb_tx;
2157 if (likely(nb_tx)) {
2158 vq_update_avail_idx(vq);
2160 if (unlikely(virtqueue_kick_prepare(vq))) {
2161 virtqueue_notify(vq);
2162 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2169 static __rte_always_inline int
2170 virtio_xmit_try_cleanup_inorder(struct virtqueue *vq, uint16_t need)
2172 uint16_t nb_used, nb_clean, nb_descs;
2173 struct virtio_hw *hw = vq->hw;
2175 nb_descs = vq->vq_free_cnt + need;
2176 nb_used = VIRTQUEUE_NUSED(vq);
2177 virtio_rmb(hw->weak_barriers);
2178 nb_clean = RTE_MIN(need, (int)nb_used);
2180 virtio_xmit_cleanup_inorder(vq, nb_clean);
2182 return nb_descs - vq->vq_free_cnt;
2186 virtio_xmit_pkts_inorder(void *tx_queue,
2187 struct rte_mbuf **tx_pkts,
2190 struct virtnet_tx *txvq = tx_queue;
2191 struct virtqueue *vq = txvq->vq;
2192 struct virtio_hw *hw = vq->hw;
2193 uint16_t hdr_size = hw->vtnet_hdr_size;
2194 uint16_t nb_used, nb_tx = 0, nb_inorder_pkts = 0;
2195 struct rte_mbuf *inorder_pkts[nb_pkts];
2198 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2201 if (unlikely(nb_pkts < 1))
2205 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2206 nb_used = VIRTQUEUE_NUSED(vq);
2208 virtio_rmb(hw->weak_barriers);
2209 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2210 virtio_xmit_cleanup_inorder(vq, nb_used);
2212 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2213 struct rte_mbuf *txm = tx_pkts[nb_tx];
2216 /* optimize ring usage */
2217 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2218 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2219 rte_mbuf_refcnt_read(txm) == 1 &&
2220 RTE_MBUF_DIRECT(txm) &&
2221 txm->nb_segs == 1 &&
2222 rte_pktmbuf_headroom(txm) >= hdr_size &&
2223 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2224 __alignof__(struct virtio_net_hdr_mrg_rxbuf))) {
2225 inorder_pkts[nb_inorder_pkts] = txm;
2231 if (nb_inorder_pkts) {
2232 need = nb_inorder_pkts - vq->vq_free_cnt;
2233 if (unlikely(need > 0)) {
2234 need = virtio_xmit_try_cleanup_inorder(vq,
2236 if (unlikely(need > 0)) {
2238 "No free tx descriptors to "
2243 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2245 nb_inorder_pkts = 0;
2248 slots = txm->nb_segs + 1;
2249 need = slots - vq->vq_free_cnt;
2250 if (unlikely(need > 0)) {
2251 need = virtio_xmit_try_cleanup_inorder(vq, slots);
2253 if (unlikely(need > 0)) {
2255 "No free tx descriptors to transmit");
2259 /* Enqueue Packet buffers */
2260 virtqueue_enqueue_xmit(txvq, txm, slots, 0, 0, 1);
2262 virtio_update_packet_stats(&txvq->stats, txm);
2265 /* Transmit all inorder packets */
2266 if (nb_inorder_pkts) {
2267 need = nb_inorder_pkts - vq->vq_free_cnt;
2268 if (unlikely(need > 0)) {
2269 need = virtio_xmit_try_cleanup_inorder(vq,
2271 if (unlikely(need > 0)) {
2273 "No free tx descriptors to transmit");
2274 nb_inorder_pkts = vq->vq_free_cnt;
2279 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2283 txvq->stats.packets += nb_tx;
2285 if (likely(nb_tx)) {
2286 vq_update_avail_idx(vq);
2288 if (unlikely(virtqueue_kick_prepare(vq))) {
2289 virtqueue_notify(vq);
2290 PMD_TX_LOG(DEBUG, "Notified backend after xmit");