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);
660 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookies[i], vq);
661 start_dp[idx].len = cookies[i]->data_len + head_size;
662 start_dp[idx].flags = 0;
665 vq_update_avail_ring(vq, idx);
671 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
672 vq->vq_desc_head_idx = idx & (vq->vq_nentries - 1);
676 virtqueue_enqueue_xmit_packed_fast(struct virtnet_tx *txvq,
677 struct rte_mbuf *cookie,
680 struct virtqueue *vq = txvq->vq;
681 struct vring_packed_desc *dp;
682 struct vq_desc_extra *dxp;
683 uint16_t idx, id, flags;
684 uint16_t head_size = vq->hw->vtnet_hdr_size;
685 struct virtio_net_hdr *hdr;
687 id = in_order ? vq->vq_avail_idx : vq->vq_desc_head_idx;
688 idx = vq->vq_avail_idx;
689 dp = &vq->vq_packed.ring.desc[idx];
691 dxp = &vq->vq_descx[id];
693 dxp->cookie = cookie;
695 flags = vq->vq_packed.cached_flags;
697 /* prepend cannot fail, checked by caller */
698 hdr = (struct virtio_net_hdr *)(char *)cookie->buf_addr +
699 cookie->data_off - head_size;
701 /* if offload disabled, hdr is not zeroed yet, do it now */
702 if (!vq->hw->has_tx_offload)
703 virtqueue_clear_net_hdr(hdr);
705 virtqueue_xmit_offload(hdr, cookie, true);
707 dp->addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
708 dp->len = cookie->data_len + head_size;
711 if (++vq->vq_avail_idx >= vq->vq_nentries) {
712 vq->vq_avail_idx -= vq->vq_nentries;
713 vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
719 vq->vq_desc_head_idx = dxp->next;
720 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
721 vq->vq_desc_tail_idx = VQ_RING_DESC_CHAIN_END;
724 virtqueue_store_flags_packed(dp, flags, vq->hw->weak_barriers);
728 virtqueue_enqueue_xmit_packed(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
729 uint16_t needed, int can_push, int in_order)
731 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
732 struct vq_desc_extra *dxp;
733 struct virtqueue *vq = txvq->vq;
734 struct vring_packed_desc *start_dp, *head_dp;
735 uint16_t idx, id, head_idx, head_flags;
736 uint16_t head_size = vq->hw->vtnet_hdr_size;
737 struct virtio_net_hdr *hdr;
739 bool prepend_header = false;
741 id = in_order ? vq->vq_avail_idx : vq->vq_desc_head_idx;
743 dxp = &vq->vq_descx[id];
744 dxp->ndescs = needed;
745 dxp->cookie = cookie;
747 head_idx = vq->vq_avail_idx;
750 start_dp = vq->vq_packed.ring.desc;
752 head_dp = &vq->vq_packed.ring.desc[idx];
753 head_flags = cookie->next ? VRING_DESC_F_NEXT : 0;
754 head_flags |= vq->vq_packed.cached_flags;
757 /* prepend cannot fail, checked by caller */
758 hdr = (struct virtio_net_hdr *)(char *)cookie->buf_addr +
759 cookie->data_off - head_size;
760 prepend_header = true;
762 /* if offload disabled, it is not zeroed below, do it now */
763 if (!vq->hw->has_tx_offload)
764 virtqueue_clear_net_hdr(hdr);
766 /* setup first tx ring slot to point to header
767 * stored in reserved region.
769 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
770 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
771 start_dp[idx].len = vq->hw->vtnet_hdr_size;
772 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
774 if (idx >= vq->vq_nentries) {
775 idx -= vq->vq_nentries;
776 vq->vq_packed.cached_flags ^=
777 VRING_PACKED_DESC_F_AVAIL_USED;
781 virtqueue_xmit_offload(hdr, cookie, vq->hw->has_tx_offload);
786 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
787 start_dp[idx].len = cookie->data_len;
788 if (prepend_header) {
789 start_dp[idx].len += head_size;
790 prepend_header = false;
793 if (likely(idx != head_idx)) {
794 flags = cookie->next ? VRING_DESC_F_NEXT : 0;
795 flags |= vq->vq_packed.cached_flags;
796 start_dp[idx].flags = flags;
800 if (idx >= vq->vq_nentries) {
801 idx -= vq->vq_nentries;
802 vq->vq_packed.cached_flags ^=
803 VRING_PACKED_DESC_F_AVAIL_USED;
805 } while ((cookie = cookie->next) != NULL);
807 start_dp[prev].id = id;
809 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
810 vq->vq_avail_idx = idx;
813 vq->vq_desc_head_idx = dxp->next;
814 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
815 vq->vq_desc_tail_idx = VQ_RING_DESC_CHAIN_END;
818 virtqueue_store_flags_packed(head_dp, head_flags,
819 vq->hw->weak_barriers);
823 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
824 uint16_t needed, int use_indirect, int can_push,
827 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
828 struct vq_desc_extra *dxp;
829 struct virtqueue *vq = txvq->vq;
830 struct vring_desc *start_dp;
831 uint16_t seg_num = cookie->nb_segs;
832 uint16_t head_idx, idx;
833 uint16_t head_size = vq->hw->vtnet_hdr_size;
834 bool prepend_header = false;
835 struct virtio_net_hdr *hdr;
837 head_idx = vq->vq_desc_head_idx;
840 dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
842 dxp = &vq->vq_descx[idx];
843 dxp->cookie = (void *)cookie;
844 dxp->ndescs = needed;
846 start_dp = vq->vq_split.ring.desc;
849 /* prepend cannot fail, checked by caller */
850 hdr = (struct virtio_net_hdr *)(char *)cookie->buf_addr +
851 cookie->data_off - head_size;
852 prepend_header = true;
854 /* if offload disabled, it is not zeroed below, do it now */
855 if (!vq->hw->has_tx_offload)
856 virtqueue_clear_net_hdr(hdr);
857 } else if (use_indirect) {
858 /* setup tx ring slot to point to indirect
859 * descriptor list stored in reserved region.
861 * the first slot in indirect ring is already preset
862 * to point to the header in reserved region
864 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
865 RTE_PTR_DIFF(&txr[idx].tx_indir, txr);
866 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
867 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
868 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
870 /* loop below will fill in rest of the indirect elements */
871 start_dp = txr[idx].tx_indir;
874 /* setup first tx ring slot to point to header
875 * stored in reserved region.
877 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
878 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
879 start_dp[idx].len = vq->hw->vtnet_hdr_size;
880 start_dp[idx].flags = VRING_DESC_F_NEXT;
881 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
883 idx = start_dp[idx].next;
886 virtqueue_xmit_offload(hdr, cookie, vq->hw->has_tx_offload);
889 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
890 start_dp[idx].len = cookie->data_len;
891 if (prepend_header) {
892 start_dp[idx].len += head_size;
893 prepend_header = false;
895 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
896 idx = start_dp[idx].next;
897 } while ((cookie = cookie->next) != NULL);
900 idx = vq->vq_split.ring.desc[head_idx].next;
902 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
904 vq->vq_desc_head_idx = idx;
905 vq_update_avail_ring(vq, head_idx);
908 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
909 vq->vq_desc_tail_idx = idx;
914 virtio_dev_cq_start(struct rte_eth_dev *dev)
916 struct virtio_hw *hw = dev->data->dev_private;
918 if (hw->cvq && hw->cvq->vq) {
919 rte_spinlock_init(&hw->cvq->lock);
920 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
925 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
928 unsigned int socket_id __rte_unused,
929 const struct rte_eth_rxconf *rx_conf __rte_unused,
930 struct rte_mempool *mp)
932 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
933 struct virtio_hw *hw = dev->data->dev_private;
934 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
935 struct virtnet_rx *rxvq;
937 PMD_INIT_FUNC_TRACE();
939 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
940 nb_desc = vq->vq_nentries;
941 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
944 rxvq->queue_id = queue_idx;
946 dev->data->rx_queues[queue_idx] = rxvq;
952 virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
954 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
955 struct virtio_hw *hw = dev->data->dev_private;
956 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
957 struct virtnet_rx *rxvq = &vq->rxq;
962 PMD_INIT_FUNC_TRACE();
964 /* Allocate blank mbufs for the each rx descriptor */
967 if (hw->use_simple_rx) {
968 for (desc_idx = 0; desc_idx < vq->vq_nentries;
970 vq->vq_split.ring.avail->ring[desc_idx] = desc_idx;
971 vq->vq_split.ring.desc[desc_idx].flags =
975 virtio_rxq_vec_setup(rxvq);
978 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
979 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
981 vq->sw_ring[vq->vq_nentries + desc_idx] =
985 if (hw->use_simple_rx) {
986 while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
987 virtio_rxq_rearm_vec(rxvq);
988 nbufs += RTE_VIRTIO_VPMD_RX_REARM_THRESH;
990 } else if (hw->use_inorder_rx) {
991 if ((!virtqueue_full(vq))) {
992 uint16_t free_cnt = vq->vq_free_cnt;
993 struct rte_mbuf *pkts[free_cnt];
995 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, pkts,
997 error = virtqueue_enqueue_refill_inorder(vq,
1000 if (unlikely(error)) {
1001 for (i = 0; i < free_cnt; i++)
1002 rte_pktmbuf_free(pkts[i]);
1007 vq_update_avail_idx(vq);
1010 while (!virtqueue_full(vq)) {
1011 m = rte_mbuf_raw_alloc(rxvq->mpool);
1015 /* Enqueue allocated buffers */
1016 if (vtpci_packed_queue(vq->hw))
1017 error = virtqueue_enqueue_recv_refill_packed(vq,
1020 error = virtqueue_enqueue_recv_refill(vq,
1023 rte_pktmbuf_free(m);
1029 if (!vtpci_packed_queue(vq->hw))
1030 vq_update_avail_idx(vq);
1033 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
1041 * struct rte_eth_dev *dev: Used to update dev
1042 * uint16_t nb_desc: Defaults to values read from config space
1043 * unsigned int socket_id: Used to allocate memzone
1044 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
1045 * uint16_t queue_idx: Just used as an index in dev txq list
1048 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
1051 unsigned int socket_id __rte_unused,
1052 const struct rte_eth_txconf *tx_conf)
1054 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1055 struct virtio_hw *hw = dev->data->dev_private;
1056 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1057 struct virtnet_tx *txvq;
1058 uint16_t tx_free_thresh;
1060 PMD_INIT_FUNC_TRACE();
1062 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
1063 nb_desc = vq->vq_nentries;
1064 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
1067 txvq->queue_id = queue_idx;
1069 tx_free_thresh = tx_conf->tx_free_thresh;
1070 if (tx_free_thresh == 0)
1072 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
1074 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
1075 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
1076 "number of TX entries minus 3 (%u)."
1077 " (tx_free_thresh=%u port=%u queue=%u)\n",
1078 vq->vq_nentries - 3,
1079 tx_free_thresh, dev->data->port_id, queue_idx);
1083 vq->vq_free_thresh = tx_free_thresh;
1085 dev->data->tx_queues[queue_idx] = txvq;
1090 virtio_dev_tx_queue_setup_finish(struct rte_eth_dev *dev,
1093 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1094 struct virtio_hw *hw = dev->data->dev_private;
1095 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1097 PMD_INIT_FUNC_TRACE();
1099 if (!vtpci_packed_queue(hw)) {
1100 if (hw->use_inorder_tx)
1101 vq->vq_split.ring.desc[vq->vq_nentries - 1].next = 0;
1110 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
1114 * Requeue the discarded mbuf. This should always be
1115 * successful since it was just dequeued.
1117 if (vtpci_packed_queue(vq->hw))
1118 error = virtqueue_enqueue_recv_refill_packed(vq, &m, 1);
1120 error = virtqueue_enqueue_recv_refill(vq, &m, 1);
1122 if (unlikely(error)) {
1123 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
1124 rte_pktmbuf_free(m);
1129 virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)
1133 error = virtqueue_enqueue_refill_inorder(vq, &m, 1);
1134 if (unlikely(error)) {
1135 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
1136 rte_pktmbuf_free(m);
1140 /* Optionally fill offload information in structure */
1142 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
1144 struct rte_net_hdr_lens hdr_lens;
1145 uint32_t hdrlen, ptype;
1146 int l4_supported = 0;
1149 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1152 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
1154 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
1155 m->packet_type = ptype;
1156 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
1157 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
1158 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
1161 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1162 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
1163 if (hdr->csum_start <= hdrlen && l4_supported) {
1164 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
1166 /* Unknown proto or tunnel, do sw cksum. We can assume
1167 * the cksum field is in the first segment since the
1168 * buffers we provided to the host are large enough.
1169 * In case of SCTP, this will be wrong since it's a CRC
1170 * but there's nothing we can do.
1172 uint16_t csum = 0, off;
1174 rte_raw_cksum_mbuf(m, hdr->csum_start,
1175 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
1177 if (likely(csum != 0xffff))
1179 off = hdr->csum_offset + hdr->csum_start;
1180 if (rte_pktmbuf_data_len(m) >= off + 1)
1181 *rte_pktmbuf_mtod_offset(m, uint16_t *,
1184 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
1185 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
1188 /* GSO request, save required information in mbuf */
1189 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1190 /* Check unsupported modes */
1191 if ((hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) ||
1192 (hdr->gso_size == 0)) {
1196 /* Update mss lengthes in mbuf */
1197 m->tso_segsz = hdr->gso_size;
1198 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1199 case VIRTIO_NET_HDR_GSO_TCPV4:
1200 case VIRTIO_NET_HDR_GSO_TCPV6:
1201 m->ol_flags |= PKT_RX_LRO | \
1202 PKT_RX_L4_CKSUM_NONE;
1212 #define VIRTIO_MBUF_BURST_SZ 64
1213 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
1215 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1217 struct virtnet_rx *rxvq = rx_queue;
1218 struct virtqueue *vq = rxvq->vq;
1219 struct virtio_hw *hw = vq->hw;
1220 struct rte_mbuf *rxm;
1221 uint16_t nb_used, num, nb_rx;
1222 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1223 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1225 uint32_t i, nb_enqueued;
1227 struct virtio_net_hdr *hdr;
1230 if (unlikely(hw->started == 0))
1233 nb_used = VIRTQUEUE_NUSED(vq);
1235 virtio_rmb(hw->weak_barriers);
1237 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1238 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1239 num = VIRTIO_MBUF_BURST_SZ;
1240 if (likely(num > DESC_PER_CACHELINE))
1241 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1243 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1244 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
1247 hdr_size = hw->vtnet_hdr_size;
1249 for (i = 0; i < num ; i++) {
1252 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1254 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1255 PMD_RX_LOG(ERR, "Packet drop");
1257 virtio_discard_rxbuf(vq, rxm);
1258 rxvq->stats.errors++;
1262 rxm->port = rxvq->port_id;
1263 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1267 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1268 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1270 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1271 RTE_PKTMBUF_HEADROOM - hdr_size);
1274 rte_vlan_strip(rxm);
1276 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1277 virtio_discard_rxbuf(vq, rxm);
1278 rxvq->stats.errors++;
1282 virtio_rx_stats_updated(rxvq, rxm);
1284 rx_pkts[nb_rx++] = rxm;
1287 rxvq->stats.packets += nb_rx;
1289 /* Allocate new mbuf for the used descriptor */
1290 if (likely(!virtqueue_full(vq))) {
1291 uint16_t free_cnt = vq->vq_free_cnt;
1292 struct rte_mbuf *new_pkts[free_cnt];
1294 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1296 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1298 if (unlikely(error)) {
1299 for (i = 0; i < free_cnt; i++)
1300 rte_pktmbuf_free(new_pkts[i]);
1302 nb_enqueued += free_cnt;
1304 struct rte_eth_dev *dev =
1305 &rte_eth_devices[rxvq->port_id];
1306 dev->data->rx_mbuf_alloc_failed += free_cnt;
1310 if (likely(nb_enqueued)) {
1311 vq_update_avail_idx(vq);
1313 if (unlikely(virtqueue_kick_prepare(vq))) {
1314 virtqueue_notify(vq);
1315 PMD_RX_LOG(DEBUG, "Notified");
1323 virtio_recv_pkts_packed(void *rx_queue, struct rte_mbuf **rx_pkts,
1326 struct virtnet_rx *rxvq = rx_queue;
1327 struct virtqueue *vq = rxvq->vq;
1328 struct virtio_hw *hw = vq->hw;
1329 struct rte_mbuf *rxm;
1330 uint16_t num, nb_rx;
1331 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1332 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1334 uint32_t i, nb_enqueued;
1336 struct virtio_net_hdr *hdr;
1339 if (unlikely(hw->started == 0))
1342 num = RTE_MIN(VIRTIO_MBUF_BURST_SZ, nb_pkts);
1343 if (likely(num > DESC_PER_CACHELINE))
1344 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1346 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1347 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1350 hdr_size = hw->vtnet_hdr_size;
1352 for (i = 0; i < num; i++) {
1355 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1357 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1358 PMD_RX_LOG(ERR, "Packet drop");
1360 virtio_discard_rxbuf(vq, rxm);
1361 rxvq->stats.errors++;
1365 rxm->port = rxvq->port_id;
1366 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1370 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1371 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1373 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1374 RTE_PKTMBUF_HEADROOM - hdr_size);
1377 rte_vlan_strip(rxm);
1379 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1380 virtio_discard_rxbuf(vq, rxm);
1381 rxvq->stats.errors++;
1385 virtio_rx_stats_updated(rxvq, rxm);
1387 rx_pkts[nb_rx++] = rxm;
1390 rxvq->stats.packets += nb_rx;
1392 /* Allocate new mbuf for the used descriptor */
1393 if (likely(!virtqueue_full(vq))) {
1394 uint16_t free_cnt = vq->vq_free_cnt;
1395 struct rte_mbuf *new_pkts[free_cnt];
1397 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1399 error = virtqueue_enqueue_recv_refill_packed(vq,
1400 new_pkts, free_cnt);
1401 if (unlikely(error)) {
1402 for (i = 0; i < free_cnt; i++)
1403 rte_pktmbuf_free(new_pkts[i]);
1405 nb_enqueued += free_cnt;
1407 struct rte_eth_dev *dev =
1408 &rte_eth_devices[rxvq->port_id];
1409 dev->data->rx_mbuf_alloc_failed += free_cnt;
1413 if (likely(nb_enqueued)) {
1414 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1415 virtqueue_notify(vq);
1416 PMD_RX_LOG(DEBUG, "Notified");
1425 virtio_recv_pkts_inorder(void *rx_queue,
1426 struct rte_mbuf **rx_pkts,
1429 struct virtnet_rx *rxvq = rx_queue;
1430 struct virtqueue *vq = rxvq->vq;
1431 struct virtio_hw *hw = vq->hw;
1432 struct rte_mbuf *rxm;
1433 struct rte_mbuf *prev = NULL;
1434 uint16_t nb_used, num, nb_rx;
1435 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1436 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1438 uint32_t nb_enqueued;
1445 if (unlikely(hw->started == 0))
1448 nb_used = VIRTQUEUE_NUSED(vq);
1449 nb_used = RTE_MIN(nb_used, nb_pkts);
1450 nb_used = RTE_MIN(nb_used, VIRTIO_MBUF_BURST_SZ);
1452 virtio_rmb(hw->weak_barriers);
1454 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1459 hdr_size = hw->vtnet_hdr_size;
1461 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len, nb_used);
1463 for (i = 0; i < num; i++) {
1464 struct virtio_net_hdr_mrg_rxbuf *header;
1466 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1467 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1471 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1472 PMD_RX_LOG(ERR, "Packet drop");
1474 virtio_discard_rxbuf_inorder(vq, rxm);
1475 rxvq->stats.errors++;
1479 header = (struct virtio_net_hdr_mrg_rxbuf *)
1480 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1483 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1484 seg_num = header->num_buffers;
1491 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1492 rxm->nb_segs = seg_num;
1495 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1496 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1498 rxm->port = rxvq->port_id;
1500 rx_pkts[nb_rx] = rxm;
1503 if (vq->hw->has_rx_offload &&
1504 virtio_rx_offload(rxm, &header->hdr) < 0) {
1505 virtio_discard_rxbuf_inorder(vq, rxm);
1506 rxvq->stats.errors++;
1511 rte_vlan_strip(rx_pkts[nb_rx]);
1513 seg_res = seg_num - 1;
1515 /* Merge remaining segments */
1516 while (seg_res != 0 && i < (num - 1)) {
1520 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1521 rxm->pkt_len = (uint32_t)(len[i]);
1522 rxm->data_len = (uint16_t)(len[i]);
1524 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1532 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1537 /* Last packet still need merge segments */
1538 while (seg_res != 0) {
1539 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1540 VIRTIO_MBUF_BURST_SZ);
1542 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1543 virtio_rmb(hw->weak_barriers);
1544 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len,
1546 uint16_t extra_idx = 0;
1549 while (extra_idx < rcv_cnt) {
1550 rxm = rcv_pkts[extra_idx];
1552 RTE_PKTMBUF_HEADROOM - hdr_size;
1553 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1554 rxm->data_len = (uint16_t)(len[extra_idx]);
1557 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1563 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1568 "No enough segments for packet.");
1569 rte_pktmbuf_free(rx_pkts[nb_rx]);
1570 rxvq->stats.errors++;
1575 rxvq->stats.packets += nb_rx;
1577 /* Allocate new mbuf for the used descriptor */
1579 if (likely(!virtqueue_full(vq))) {
1580 /* free_cnt may include mrg descs */
1581 uint16_t free_cnt = vq->vq_free_cnt;
1582 struct rte_mbuf *new_pkts[free_cnt];
1584 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1585 error = virtqueue_enqueue_refill_inorder(vq, new_pkts,
1587 if (unlikely(error)) {
1588 for (i = 0; i < free_cnt; i++)
1589 rte_pktmbuf_free(new_pkts[i]);
1591 nb_enqueued += free_cnt;
1593 struct rte_eth_dev *dev =
1594 &rte_eth_devices[rxvq->port_id];
1595 dev->data->rx_mbuf_alloc_failed += free_cnt;
1599 if (likely(nb_enqueued)) {
1600 vq_update_avail_idx(vq);
1602 if (unlikely(virtqueue_kick_prepare(vq))) {
1603 virtqueue_notify(vq);
1604 PMD_RX_LOG(DEBUG, "Notified");
1612 virtio_recv_mergeable_pkts(void *rx_queue,
1613 struct rte_mbuf **rx_pkts,
1616 struct virtnet_rx *rxvq = rx_queue;
1617 struct virtqueue *vq = rxvq->vq;
1618 struct virtio_hw *hw = vq->hw;
1619 struct rte_mbuf *rxm;
1620 struct rte_mbuf *prev = NULL;
1621 uint16_t nb_used, num, nb_rx = 0;
1622 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1623 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1625 uint32_t nb_enqueued = 0;
1626 uint32_t seg_num = 0;
1627 uint32_t seg_res = 0;
1628 uint32_t hdr_size = hw->vtnet_hdr_size;
1631 if (unlikely(hw->started == 0))
1634 nb_used = VIRTQUEUE_NUSED(vq);
1636 virtio_rmb(hw->weak_barriers);
1638 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1640 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1641 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1642 num = VIRTIO_MBUF_BURST_SZ;
1643 if (likely(num > DESC_PER_CACHELINE))
1644 num = num - ((vq->vq_used_cons_idx + num) %
1645 DESC_PER_CACHELINE);
1648 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1650 for (i = 0; i < num; i++) {
1651 struct virtio_net_hdr_mrg_rxbuf *header;
1653 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1654 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1658 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1659 PMD_RX_LOG(ERR, "Packet drop");
1661 virtio_discard_rxbuf(vq, rxm);
1662 rxvq->stats.errors++;
1666 header = (struct virtio_net_hdr_mrg_rxbuf *)
1667 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1669 seg_num = header->num_buffers;
1673 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1674 rxm->nb_segs = seg_num;
1677 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1678 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1680 rxm->port = rxvq->port_id;
1682 rx_pkts[nb_rx] = rxm;
1685 if (hw->has_rx_offload &&
1686 virtio_rx_offload(rxm, &header->hdr) < 0) {
1687 virtio_discard_rxbuf(vq, rxm);
1688 rxvq->stats.errors++;
1693 rte_vlan_strip(rx_pkts[nb_rx]);
1695 seg_res = seg_num - 1;
1697 /* Merge remaining segments */
1698 while (seg_res != 0 && i < (num - 1)) {
1702 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1703 rxm->pkt_len = (uint32_t)(len[i]);
1704 rxm->data_len = (uint16_t)(len[i]);
1706 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1714 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1719 /* Last packet still need merge segments */
1720 while (seg_res != 0) {
1721 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1722 VIRTIO_MBUF_BURST_SZ);
1724 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1725 virtio_rmb(hw->weak_barriers);
1726 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len,
1728 uint16_t extra_idx = 0;
1731 while (extra_idx < rcv_cnt) {
1732 rxm = rcv_pkts[extra_idx];
1734 RTE_PKTMBUF_HEADROOM - hdr_size;
1735 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1736 rxm->data_len = (uint16_t)(len[extra_idx]);
1739 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1745 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1750 "No enough segments for packet.");
1751 rte_pktmbuf_free(rx_pkts[nb_rx]);
1752 rxvq->stats.errors++;
1757 rxvq->stats.packets += nb_rx;
1759 /* Allocate new mbuf for the used descriptor */
1760 if (likely(!virtqueue_full(vq))) {
1761 /* free_cnt may include mrg descs */
1762 uint16_t free_cnt = vq->vq_free_cnt;
1763 struct rte_mbuf *new_pkts[free_cnt];
1765 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1766 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1768 if (unlikely(error)) {
1769 for (i = 0; i < free_cnt; i++)
1770 rte_pktmbuf_free(new_pkts[i]);
1772 nb_enqueued += free_cnt;
1774 struct rte_eth_dev *dev =
1775 &rte_eth_devices[rxvq->port_id];
1776 dev->data->rx_mbuf_alloc_failed += free_cnt;
1780 if (likely(nb_enqueued)) {
1781 vq_update_avail_idx(vq);
1783 if (unlikely(virtqueue_kick_prepare(vq))) {
1784 virtqueue_notify(vq);
1785 PMD_RX_LOG(DEBUG, "Notified");
1793 virtio_recv_mergeable_pkts_packed(void *rx_queue,
1794 struct rte_mbuf **rx_pkts,
1797 struct virtnet_rx *rxvq = rx_queue;
1798 struct virtqueue *vq = rxvq->vq;
1799 struct virtio_hw *hw = vq->hw;
1800 struct rte_mbuf *rxm;
1801 struct rte_mbuf *prev = NULL;
1802 uint16_t num, nb_rx = 0;
1803 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1804 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1805 uint32_t nb_enqueued = 0;
1806 uint32_t seg_num = 0;
1807 uint32_t seg_res = 0;
1808 uint32_t hdr_size = hw->vtnet_hdr_size;
1812 if (unlikely(hw->started == 0))
1817 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1818 num = VIRTIO_MBUF_BURST_SZ;
1819 if (likely(num > DESC_PER_CACHELINE))
1820 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1822 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1824 for (i = 0; i < num; i++) {
1825 struct virtio_net_hdr_mrg_rxbuf *header;
1827 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1828 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1832 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1833 PMD_RX_LOG(ERR, "Packet drop");
1835 virtio_discard_rxbuf(vq, rxm);
1836 rxvq->stats.errors++;
1840 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)
1841 rxm->buf_addr + RTE_PKTMBUF_HEADROOM - hdr_size);
1842 seg_num = header->num_buffers;
1847 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1848 rxm->nb_segs = seg_num;
1851 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1852 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1854 rxm->port = rxvq->port_id;
1855 rx_pkts[nb_rx] = rxm;
1858 if (hw->has_rx_offload &&
1859 virtio_rx_offload(rxm, &header->hdr) < 0) {
1860 virtio_discard_rxbuf(vq, rxm);
1861 rxvq->stats.errors++;
1866 rte_vlan_strip(rx_pkts[nb_rx]);
1868 seg_res = seg_num - 1;
1870 /* Merge remaining segments */
1871 while (seg_res != 0 && i < (num - 1)) {
1875 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1876 rxm->pkt_len = (uint32_t)(len[i]);
1877 rxm->data_len = (uint16_t)(len[i]);
1879 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1887 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1892 /* Last packet still need merge segments */
1893 while (seg_res != 0) {
1894 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1895 VIRTIO_MBUF_BURST_SZ);
1896 uint16_t extra_idx = 0;
1898 rcv_cnt = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts,
1900 if (unlikely(rcv_cnt == 0)) {
1901 PMD_RX_LOG(ERR, "No enough segments for packet.");
1902 rte_pktmbuf_free(rx_pkts[nb_rx]);
1903 rxvq->stats.errors++;
1907 while (extra_idx < rcv_cnt) {
1908 rxm = rcv_pkts[extra_idx];
1910 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1911 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1912 rxm->data_len = (uint16_t)(len[extra_idx]);
1916 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1921 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1926 rxvq->stats.packets += nb_rx;
1928 /* Allocate new mbuf for the used descriptor */
1929 if (likely(!virtqueue_full(vq))) {
1930 /* free_cnt may include mrg descs */
1931 uint16_t free_cnt = vq->vq_free_cnt;
1932 struct rte_mbuf *new_pkts[free_cnt];
1934 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1935 error = virtqueue_enqueue_recv_refill_packed(vq,
1936 new_pkts, free_cnt);
1937 if (unlikely(error)) {
1938 for (i = 0; i < free_cnt; i++)
1939 rte_pktmbuf_free(new_pkts[i]);
1941 nb_enqueued += free_cnt;
1943 struct rte_eth_dev *dev =
1944 &rte_eth_devices[rxvq->port_id];
1945 dev->data->rx_mbuf_alloc_failed += free_cnt;
1949 if (likely(nb_enqueued)) {
1950 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1951 virtqueue_notify(vq);
1952 PMD_RX_LOG(DEBUG, "Notified");
1960 virtio_xmit_pkts_prepare(void *tx_queue __rte_unused, struct rte_mbuf **tx_pkts,
1966 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1967 struct rte_mbuf *m = tx_pkts[nb_tx];
1969 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1970 error = rte_validate_tx_offload(m);
1971 if (unlikely(error)) {
1977 /* Do VLAN tag insertion */
1978 if (unlikely(m->ol_flags & PKT_TX_VLAN_PKT)) {
1979 error = rte_vlan_insert(&m);
1980 /* rte_vlan_insert() may change pointer
1981 * even in the case of failure
1985 if (unlikely(error)) {
1991 error = rte_net_intel_cksum_prepare(m);
1992 if (unlikely(error)) {
1997 if (m->ol_flags & PKT_TX_TCP_SEG)
1998 virtio_tso_fix_cksum(m);
2005 virtio_xmit_pkts_packed(void *tx_queue, struct rte_mbuf **tx_pkts,
2008 struct virtnet_tx *txvq = tx_queue;
2009 struct virtqueue *vq = txvq->vq;
2010 struct virtio_hw *hw = vq->hw;
2011 uint16_t hdr_size = hw->vtnet_hdr_size;
2013 bool in_order = hw->use_inorder_tx;
2015 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2018 if (unlikely(nb_pkts < 1))
2021 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2023 if (nb_pkts > vq->vq_free_cnt)
2024 virtio_xmit_cleanup_packed(vq, nb_pkts - vq->vq_free_cnt,
2027 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2028 struct rte_mbuf *txm = tx_pkts[nb_tx];
2029 int can_push = 0, slots, need;
2031 /* optimize ring usage */
2032 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2033 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2034 rte_mbuf_refcnt_read(txm) == 1 &&
2035 RTE_MBUF_DIRECT(txm) &&
2036 txm->nb_segs == 1 &&
2037 rte_pktmbuf_headroom(txm) >= hdr_size &&
2038 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2039 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2042 /* How many main ring entries are needed to this Tx?
2043 * any_layout => number of segments
2044 * default => number of segments + 1
2046 slots = txm->nb_segs + !can_push;
2047 need = slots - vq->vq_free_cnt;
2049 /* Positive value indicates it need free vring descriptors */
2050 if (unlikely(need > 0)) {
2051 virtio_xmit_cleanup_packed(vq, need, in_order);
2052 need = slots - vq->vq_free_cnt;
2053 if (unlikely(need > 0)) {
2055 "No free tx descriptors to transmit");
2060 /* Enqueue Packet buffers */
2062 virtqueue_enqueue_xmit_packed_fast(txvq, txm, in_order);
2064 virtqueue_enqueue_xmit_packed(txvq, txm, slots, 0,
2067 virtio_update_packet_stats(&txvq->stats, txm);
2070 txvq->stats.packets += nb_tx;
2072 if (likely(nb_tx)) {
2073 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
2074 virtqueue_notify(vq);
2075 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2083 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
2085 struct virtnet_tx *txvq = tx_queue;
2086 struct virtqueue *vq = txvq->vq;
2087 struct virtio_hw *hw = vq->hw;
2088 uint16_t hdr_size = hw->vtnet_hdr_size;
2089 uint16_t nb_used, nb_tx = 0;
2091 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2094 if (unlikely(nb_pkts < 1))
2097 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2098 nb_used = VIRTQUEUE_NUSED(vq);
2100 virtio_rmb(hw->weak_barriers);
2101 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2102 virtio_xmit_cleanup(vq, nb_used);
2104 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2105 struct rte_mbuf *txm = tx_pkts[nb_tx];
2106 int can_push = 0, use_indirect = 0, slots, need;
2108 /* optimize ring usage */
2109 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2110 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2111 rte_mbuf_refcnt_read(txm) == 1 &&
2112 RTE_MBUF_DIRECT(txm) &&
2113 txm->nb_segs == 1 &&
2114 rte_pktmbuf_headroom(txm) >= hdr_size &&
2115 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2116 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2118 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
2119 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
2122 /* How many main ring entries are needed to this Tx?
2123 * any_layout => number of segments
2125 * default => number of segments + 1
2127 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
2128 need = slots - vq->vq_free_cnt;
2130 /* Positive value indicates it need free vring descriptors */
2131 if (unlikely(need > 0)) {
2132 nb_used = VIRTQUEUE_NUSED(vq);
2133 virtio_rmb(hw->weak_barriers);
2134 need = RTE_MIN(need, (int)nb_used);
2136 virtio_xmit_cleanup(vq, need);
2137 need = slots - vq->vq_free_cnt;
2138 if (unlikely(need > 0)) {
2140 "No free tx descriptors to transmit");
2145 /* Enqueue Packet buffers */
2146 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect,
2149 virtio_update_packet_stats(&txvq->stats, txm);
2152 txvq->stats.packets += nb_tx;
2154 if (likely(nb_tx)) {
2155 vq_update_avail_idx(vq);
2157 if (unlikely(virtqueue_kick_prepare(vq))) {
2158 virtqueue_notify(vq);
2159 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2166 static __rte_always_inline int
2167 virtio_xmit_try_cleanup_inorder(struct virtqueue *vq, uint16_t need)
2169 uint16_t nb_used, nb_clean, nb_descs;
2170 struct virtio_hw *hw = vq->hw;
2172 nb_descs = vq->vq_free_cnt + need;
2173 nb_used = VIRTQUEUE_NUSED(vq);
2174 virtio_rmb(hw->weak_barriers);
2175 nb_clean = RTE_MIN(need, (int)nb_used);
2177 virtio_xmit_cleanup_inorder(vq, nb_clean);
2179 return nb_descs - vq->vq_free_cnt;
2183 virtio_xmit_pkts_inorder(void *tx_queue,
2184 struct rte_mbuf **tx_pkts,
2187 struct virtnet_tx *txvq = tx_queue;
2188 struct virtqueue *vq = txvq->vq;
2189 struct virtio_hw *hw = vq->hw;
2190 uint16_t hdr_size = hw->vtnet_hdr_size;
2191 uint16_t nb_used, nb_tx = 0, nb_inorder_pkts = 0;
2192 struct rte_mbuf *inorder_pkts[nb_pkts];
2195 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2198 if (unlikely(nb_pkts < 1))
2202 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2203 nb_used = VIRTQUEUE_NUSED(vq);
2205 virtio_rmb(hw->weak_barriers);
2206 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2207 virtio_xmit_cleanup_inorder(vq, nb_used);
2209 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2210 struct rte_mbuf *txm = tx_pkts[nb_tx];
2213 /* optimize ring usage */
2214 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2215 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2216 rte_mbuf_refcnt_read(txm) == 1 &&
2217 RTE_MBUF_DIRECT(txm) &&
2218 txm->nb_segs == 1 &&
2219 rte_pktmbuf_headroom(txm) >= hdr_size &&
2220 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2221 __alignof__(struct virtio_net_hdr_mrg_rxbuf))) {
2222 inorder_pkts[nb_inorder_pkts] = txm;
2228 if (nb_inorder_pkts) {
2229 need = nb_inorder_pkts - vq->vq_free_cnt;
2230 if (unlikely(need > 0)) {
2231 need = virtio_xmit_try_cleanup_inorder(vq,
2233 if (unlikely(need > 0)) {
2235 "No free tx descriptors to "
2240 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2242 nb_inorder_pkts = 0;
2245 slots = txm->nb_segs + 1;
2246 need = slots - vq->vq_free_cnt;
2247 if (unlikely(need > 0)) {
2248 need = virtio_xmit_try_cleanup_inorder(vq, slots);
2250 if (unlikely(need > 0)) {
2252 "No free tx descriptors to transmit");
2256 /* Enqueue Packet buffers */
2257 virtqueue_enqueue_xmit(txvq, txm, slots, 0, 0, 1);
2259 virtio_update_packet_stats(&txvq->stats, txm);
2262 /* Transmit all inorder packets */
2263 if (nb_inorder_pkts) {
2264 need = nb_inorder_pkts - vq->vq_free_cnt;
2265 if (unlikely(need > 0)) {
2266 need = virtio_xmit_try_cleanup_inorder(vq,
2268 if (unlikely(need > 0)) {
2270 "No free tx descriptors to transmit");
2271 nb_inorder_pkts = vq->vq_free_cnt;
2276 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2280 txvq->stats.packets += nb_tx;
2282 if (likely(nb_tx)) {
2283 vq_update_avail_idx(vq);
2285 if (unlikely(virtqueue_kick_prepare(vq))) {
2286 virtqueue_notify(vq);
2287 PMD_TX_LOG(DEBUG, "Notified backend after xmit");