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 int16_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 = rte_pktmbuf_mtod_offset(cookies[i],
652 struct virtio_net_hdr *, -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 int16_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 = rte_pktmbuf_mtod_offset(cookie, struct virtio_net_hdr *,
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 int16_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 = rte_pktmbuf_mtod_offset(cookie, struct virtio_net_hdr *,
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 int16_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 = rte_pktmbuf_mtod_offset(cookie, struct virtio_net_hdr *,
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,
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;
939 uint16_t rx_free_thresh;
941 PMD_INIT_FUNC_TRACE();
943 if (rx_conf->rx_deferred_start) {
944 PMD_INIT_LOG(ERR, "Rx deferred start is not supported");
948 rx_free_thresh = rx_conf->rx_free_thresh;
949 if (rx_free_thresh == 0)
951 RTE_MIN(vq->vq_nentries / 4, DEFAULT_RX_FREE_THRESH);
953 if (rx_free_thresh & 0x3) {
954 RTE_LOG(ERR, PMD, "rx_free_thresh must be multiples of four."
955 " (rx_free_thresh=%u port=%u queue=%u)\n",
956 rx_free_thresh, dev->data->port_id, queue_idx);
960 if (rx_free_thresh >= vq->vq_nentries) {
961 RTE_LOG(ERR, PMD, "rx_free_thresh must be less than the "
962 "number of RX entries (%u)."
963 " (rx_free_thresh=%u port=%u queue=%u)\n",
965 rx_free_thresh, dev->data->port_id, queue_idx);
968 vq->vq_free_thresh = rx_free_thresh;
970 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
971 nb_desc = vq->vq_nentries;
972 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
975 rxvq->queue_id = queue_idx;
977 dev->data->rx_queues[queue_idx] = rxvq;
983 virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
985 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
986 struct virtio_hw *hw = dev->data->dev_private;
987 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
988 struct virtnet_rx *rxvq = &vq->rxq;
992 bool in_order = vtpci_with_feature(hw, VIRTIO_F_IN_ORDER);
994 PMD_INIT_FUNC_TRACE();
996 /* Allocate blank mbufs for the each rx descriptor */
999 if (hw->use_vec_rx && !vtpci_packed_queue(hw)) {
1000 for (desc_idx = 0; desc_idx < vq->vq_nentries;
1002 vq->vq_split.ring.avail->ring[desc_idx] = desc_idx;
1003 vq->vq_split.ring.desc[desc_idx].flags =
1007 virtio_rxq_vec_setup(rxvq);
1010 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
1011 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
1013 vq->sw_ring[vq->vq_nentries + desc_idx] =
1017 if (hw->use_vec_rx && !vtpci_packed_queue(hw)) {
1018 while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
1019 virtio_rxq_rearm_vec(rxvq);
1020 nbufs += RTE_VIRTIO_VPMD_RX_REARM_THRESH;
1022 } else if (!vtpci_packed_queue(vq->hw) && in_order) {
1023 if ((!virtqueue_full(vq))) {
1024 uint16_t free_cnt = vq->vq_free_cnt;
1025 struct rte_mbuf *pkts[free_cnt];
1027 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, pkts,
1029 error = virtqueue_enqueue_refill_inorder(vq,
1032 if (unlikely(error)) {
1033 for (i = 0; i < free_cnt; i++)
1034 rte_pktmbuf_free(pkts[i]);
1039 vq_update_avail_idx(vq);
1042 while (!virtqueue_full(vq)) {
1043 m = rte_mbuf_raw_alloc(rxvq->mpool);
1047 /* Enqueue allocated buffers */
1048 if (vtpci_packed_queue(vq->hw))
1049 error = virtqueue_enqueue_recv_refill_packed(vq,
1052 error = virtqueue_enqueue_recv_refill(vq,
1055 rte_pktmbuf_free(m);
1061 if (!vtpci_packed_queue(vq->hw))
1062 vq_update_avail_idx(vq);
1065 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
1073 * struct rte_eth_dev *dev: Used to update dev
1074 * uint16_t nb_desc: Defaults to values read from config space
1075 * unsigned int socket_id: Used to allocate memzone
1076 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
1077 * uint16_t queue_idx: Just used as an index in dev txq list
1080 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
1083 unsigned int socket_id __rte_unused,
1084 const struct rte_eth_txconf *tx_conf)
1086 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1087 struct virtio_hw *hw = dev->data->dev_private;
1088 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1089 struct virtnet_tx *txvq;
1090 uint16_t tx_free_thresh;
1092 PMD_INIT_FUNC_TRACE();
1094 if (tx_conf->tx_deferred_start) {
1095 PMD_INIT_LOG(ERR, "Tx deferred start is not supported");
1099 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
1100 nb_desc = vq->vq_nentries;
1101 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
1104 txvq->queue_id = queue_idx;
1106 tx_free_thresh = tx_conf->tx_free_thresh;
1107 if (tx_free_thresh == 0)
1109 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
1111 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
1112 PMD_DRV_LOG(ERR, "tx_free_thresh must be less than the "
1113 "number of TX entries minus 3 (%u)."
1114 " (tx_free_thresh=%u port=%u queue=%u)\n",
1115 vq->vq_nentries - 3,
1116 tx_free_thresh, dev->data->port_id, queue_idx);
1120 vq->vq_free_thresh = tx_free_thresh;
1122 dev->data->tx_queues[queue_idx] = txvq;
1127 virtio_dev_tx_queue_setup_finish(struct rte_eth_dev *dev,
1130 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1131 struct virtio_hw *hw = dev->data->dev_private;
1132 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1134 PMD_INIT_FUNC_TRACE();
1136 if (!vtpci_packed_queue(hw)) {
1137 if (vtpci_with_feature(hw, VIRTIO_F_IN_ORDER))
1138 vq->vq_split.ring.desc[vq->vq_nentries - 1].next = 0;
1147 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
1151 * Requeue the discarded mbuf. This should always be
1152 * successful since it was just dequeued.
1154 if (vtpci_packed_queue(vq->hw))
1155 error = virtqueue_enqueue_recv_refill_packed(vq, &m, 1);
1157 error = virtqueue_enqueue_recv_refill(vq, &m, 1);
1159 if (unlikely(error)) {
1160 PMD_DRV_LOG(ERR, "cannot requeue discarded mbuf");
1161 rte_pktmbuf_free(m);
1166 virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)
1170 error = virtqueue_enqueue_refill_inorder(vq, &m, 1);
1171 if (unlikely(error)) {
1172 PMD_DRV_LOG(ERR, "cannot requeue discarded mbuf");
1173 rte_pktmbuf_free(m);
1177 /* Optionally fill offload information in structure */
1179 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
1181 struct rte_net_hdr_lens hdr_lens;
1182 uint32_t hdrlen, ptype;
1183 int l4_supported = 0;
1186 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1189 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
1191 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
1192 m->packet_type = ptype;
1193 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
1194 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
1195 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
1198 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1199 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
1200 if (hdr->csum_start <= hdrlen && l4_supported) {
1201 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
1203 /* Unknown proto or tunnel, do sw cksum. We can assume
1204 * the cksum field is in the first segment since the
1205 * buffers we provided to the host are large enough.
1206 * In case of SCTP, this will be wrong since it's a CRC
1207 * but there's nothing we can do.
1209 uint16_t csum = 0, off;
1211 rte_raw_cksum_mbuf(m, hdr->csum_start,
1212 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
1214 if (likely(csum != 0xffff))
1216 off = hdr->csum_offset + hdr->csum_start;
1217 if (rte_pktmbuf_data_len(m) >= off + 1)
1218 *rte_pktmbuf_mtod_offset(m, uint16_t *,
1221 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
1222 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
1225 /* GSO request, save required information in mbuf */
1226 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1227 /* Check unsupported modes */
1228 if ((hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) ||
1229 (hdr->gso_size == 0)) {
1233 /* Update mss lengthes in mbuf */
1234 m->tso_segsz = hdr->gso_size;
1235 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1236 case VIRTIO_NET_HDR_GSO_TCPV4:
1237 case VIRTIO_NET_HDR_GSO_TCPV6:
1238 m->ol_flags |= PKT_RX_LRO | \
1239 PKT_RX_L4_CKSUM_NONE;
1249 #define VIRTIO_MBUF_BURST_SZ 64
1250 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
1252 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1254 struct virtnet_rx *rxvq = rx_queue;
1255 struct virtqueue *vq = rxvq->vq;
1256 struct virtio_hw *hw = vq->hw;
1257 struct rte_mbuf *rxm;
1258 uint16_t nb_used, num, nb_rx;
1259 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1260 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1262 uint32_t i, nb_enqueued;
1264 struct virtio_net_hdr *hdr;
1267 if (unlikely(hw->started == 0))
1270 nb_used = VIRTQUEUE_NUSED(vq);
1272 virtio_rmb(hw->weak_barriers);
1274 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1275 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1276 num = VIRTIO_MBUF_BURST_SZ;
1277 if (likely(num > DESC_PER_CACHELINE))
1278 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1280 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1281 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
1284 hdr_size = hw->vtnet_hdr_size;
1286 for (i = 0; i < num ; i++) {
1289 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1291 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1292 PMD_RX_LOG(ERR, "Packet drop");
1294 virtio_discard_rxbuf(vq, rxm);
1295 rxvq->stats.errors++;
1299 rxm->port = rxvq->port_id;
1300 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1304 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1305 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1307 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1308 RTE_PKTMBUF_HEADROOM - hdr_size);
1311 rte_vlan_strip(rxm);
1313 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1314 virtio_discard_rxbuf(vq, rxm);
1315 rxvq->stats.errors++;
1319 virtio_rx_stats_updated(rxvq, rxm);
1321 rx_pkts[nb_rx++] = rxm;
1324 rxvq->stats.packets += nb_rx;
1326 /* Allocate new mbuf for the used descriptor */
1327 if (likely(!virtqueue_full(vq))) {
1328 uint16_t free_cnt = vq->vq_free_cnt;
1329 struct rte_mbuf *new_pkts[free_cnt];
1331 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1333 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1335 if (unlikely(error)) {
1336 for (i = 0; i < free_cnt; i++)
1337 rte_pktmbuf_free(new_pkts[i]);
1339 nb_enqueued += free_cnt;
1341 struct rte_eth_dev *dev =
1342 &rte_eth_devices[rxvq->port_id];
1343 dev->data->rx_mbuf_alloc_failed += free_cnt;
1347 if (likely(nb_enqueued)) {
1348 vq_update_avail_idx(vq);
1350 if (unlikely(virtqueue_kick_prepare(vq))) {
1351 virtqueue_notify(vq);
1352 PMD_RX_LOG(DEBUG, "Notified");
1360 virtio_recv_pkts_packed(void *rx_queue, struct rte_mbuf **rx_pkts,
1363 struct virtnet_rx *rxvq = rx_queue;
1364 struct virtqueue *vq = rxvq->vq;
1365 struct virtio_hw *hw = vq->hw;
1366 struct rte_mbuf *rxm;
1367 uint16_t num, nb_rx;
1368 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1369 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1371 uint32_t i, nb_enqueued;
1373 struct virtio_net_hdr *hdr;
1376 if (unlikely(hw->started == 0))
1379 num = RTE_MIN(VIRTIO_MBUF_BURST_SZ, nb_pkts);
1380 if (likely(num > DESC_PER_CACHELINE))
1381 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1383 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1384 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1387 hdr_size = hw->vtnet_hdr_size;
1389 for (i = 0; i < num; i++) {
1392 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1394 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1395 PMD_RX_LOG(ERR, "Packet drop");
1397 virtio_discard_rxbuf(vq, rxm);
1398 rxvq->stats.errors++;
1402 rxm->port = rxvq->port_id;
1403 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1407 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1408 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1410 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1411 RTE_PKTMBUF_HEADROOM - hdr_size);
1414 rte_vlan_strip(rxm);
1416 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1417 virtio_discard_rxbuf(vq, rxm);
1418 rxvq->stats.errors++;
1422 virtio_rx_stats_updated(rxvq, rxm);
1424 rx_pkts[nb_rx++] = rxm;
1427 rxvq->stats.packets += nb_rx;
1429 /* Allocate new mbuf for the used descriptor */
1430 if (likely(!virtqueue_full(vq))) {
1431 uint16_t free_cnt = vq->vq_free_cnt;
1432 struct rte_mbuf *new_pkts[free_cnt];
1434 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1436 error = virtqueue_enqueue_recv_refill_packed(vq,
1437 new_pkts, free_cnt);
1438 if (unlikely(error)) {
1439 for (i = 0; i < free_cnt; i++)
1440 rte_pktmbuf_free(new_pkts[i]);
1442 nb_enqueued += free_cnt;
1444 struct rte_eth_dev *dev =
1445 &rte_eth_devices[rxvq->port_id];
1446 dev->data->rx_mbuf_alloc_failed += free_cnt;
1450 if (likely(nb_enqueued)) {
1451 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1452 virtqueue_notify(vq);
1453 PMD_RX_LOG(DEBUG, "Notified");
1462 virtio_recv_pkts_inorder(void *rx_queue,
1463 struct rte_mbuf **rx_pkts,
1466 struct virtnet_rx *rxvq = rx_queue;
1467 struct virtqueue *vq = rxvq->vq;
1468 struct virtio_hw *hw = vq->hw;
1469 struct rte_mbuf *rxm;
1470 struct rte_mbuf *prev = NULL;
1471 uint16_t nb_used, num, nb_rx;
1472 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1473 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1475 uint32_t nb_enqueued;
1482 if (unlikely(hw->started == 0))
1485 nb_used = VIRTQUEUE_NUSED(vq);
1486 nb_used = RTE_MIN(nb_used, nb_pkts);
1487 nb_used = RTE_MIN(nb_used, VIRTIO_MBUF_BURST_SZ);
1489 virtio_rmb(hw->weak_barriers);
1491 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1496 hdr_size = hw->vtnet_hdr_size;
1498 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len, nb_used);
1500 for (i = 0; i < num; i++) {
1501 struct virtio_net_hdr_mrg_rxbuf *header;
1503 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1504 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1508 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1509 PMD_RX_LOG(ERR, "Packet drop");
1511 virtio_discard_rxbuf_inorder(vq, rxm);
1512 rxvq->stats.errors++;
1516 header = (struct virtio_net_hdr_mrg_rxbuf *)
1517 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1520 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1521 seg_num = header->num_buffers;
1528 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1529 rxm->nb_segs = seg_num;
1532 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1533 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1535 rxm->port = rxvq->port_id;
1537 rx_pkts[nb_rx] = rxm;
1540 if (vq->hw->has_rx_offload &&
1541 virtio_rx_offload(rxm, &header->hdr) < 0) {
1542 virtio_discard_rxbuf_inorder(vq, rxm);
1543 rxvq->stats.errors++;
1548 rte_vlan_strip(rx_pkts[nb_rx]);
1550 seg_res = seg_num - 1;
1552 /* Merge remaining segments */
1553 while (seg_res != 0 && i < (num - 1)) {
1557 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1558 rxm->pkt_len = (uint32_t)(len[i]);
1559 rxm->data_len = (uint16_t)(len[i]);
1561 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1569 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1574 /* Last packet still need merge segments */
1575 while (seg_res != 0) {
1576 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1577 VIRTIO_MBUF_BURST_SZ);
1579 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1580 virtio_rmb(hw->weak_barriers);
1581 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len,
1583 uint16_t extra_idx = 0;
1586 while (extra_idx < rcv_cnt) {
1587 rxm = rcv_pkts[extra_idx];
1589 RTE_PKTMBUF_HEADROOM - hdr_size;
1590 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1591 rxm->data_len = (uint16_t)(len[extra_idx]);
1594 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1600 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1605 "No enough segments for packet.");
1606 rte_pktmbuf_free(rx_pkts[nb_rx]);
1607 rxvq->stats.errors++;
1612 rxvq->stats.packets += nb_rx;
1614 /* Allocate new mbuf for the used descriptor */
1616 if (likely(!virtqueue_full(vq))) {
1617 /* free_cnt may include mrg descs */
1618 uint16_t free_cnt = vq->vq_free_cnt;
1619 struct rte_mbuf *new_pkts[free_cnt];
1621 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1622 error = virtqueue_enqueue_refill_inorder(vq, new_pkts,
1624 if (unlikely(error)) {
1625 for (i = 0; i < free_cnt; i++)
1626 rte_pktmbuf_free(new_pkts[i]);
1628 nb_enqueued += free_cnt;
1630 struct rte_eth_dev *dev =
1631 &rte_eth_devices[rxvq->port_id];
1632 dev->data->rx_mbuf_alloc_failed += free_cnt;
1636 if (likely(nb_enqueued)) {
1637 vq_update_avail_idx(vq);
1639 if (unlikely(virtqueue_kick_prepare(vq))) {
1640 virtqueue_notify(vq);
1641 PMD_RX_LOG(DEBUG, "Notified");
1649 virtio_recv_mergeable_pkts(void *rx_queue,
1650 struct rte_mbuf **rx_pkts,
1653 struct virtnet_rx *rxvq = rx_queue;
1654 struct virtqueue *vq = rxvq->vq;
1655 struct virtio_hw *hw = vq->hw;
1656 struct rte_mbuf *rxm;
1657 struct rte_mbuf *prev = NULL;
1658 uint16_t nb_used, num, nb_rx = 0;
1659 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1660 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1662 uint32_t nb_enqueued = 0;
1663 uint32_t seg_num = 0;
1664 uint32_t seg_res = 0;
1665 uint32_t hdr_size = hw->vtnet_hdr_size;
1668 if (unlikely(hw->started == 0))
1671 nb_used = VIRTQUEUE_NUSED(vq);
1673 virtio_rmb(hw->weak_barriers);
1675 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1677 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1678 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1679 num = VIRTIO_MBUF_BURST_SZ;
1680 if (likely(num > DESC_PER_CACHELINE))
1681 num = num - ((vq->vq_used_cons_idx + num) %
1682 DESC_PER_CACHELINE);
1685 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1687 for (i = 0; i < num; i++) {
1688 struct virtio_net_hdr_mrg_rxbuf *header;
1690 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1691 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1695 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1696 PMD_RX_LOG(ERR, "Packet drop");
1698 virtio_discard_rxbuf(vq, rxm);
1699 rxvq->stats.errors++;
1703 header = (struct virtio_net_hdr_mrg_rxbuf *)
1704 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1706 seg_num = header->num_buffers;
1710 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1711 rxm->nb_segs = seg_num;
1714 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1715 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1717 rxm->port = rxvq->port_id;
1719 rx_pkts[nb_rx] = rxm;
1722 if (hw->has_rx_offload &&
1723 virtio_rx_offload(rxm, &header->hdr) < 0) {
1724 virtio_discard_rxbuf(vq, rxm);
1725 rxvq->stats.errors++;
1730 rte_vlan_strip(rx_pkts[nb_rx]);
1732 seg_res = seg_num - 1;
1734 /* Merge remaining segments */
1735 while (seg_res != 0 && i < (num - 1)) {
1739 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1740 rxm->pkt_len = (uint32_t)(len[i]);
1741 rxm->data_len = (uint16_t)(len[i]);
1743 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1751 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1756 /* Last packet still need merge segments */
1757 while (seg_res != 0) {
1758 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1759 VIRTIO_MBUF_BURST_SZ);
1761 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1762 virtio_rmb(hw->weak_barriers);
1763 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len,
1765 uint16_t extra_idx = 0;
1768 while (extra_idx < rcv_cnt) {
1769 rxm = rcv_pkts[extra_idx];
1771 RTE_PKTMBUF_HEADROOM - hdr_size;
1772 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1773 rxm->data_len = (uint16_t)(len[extra_idx]);
1776 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1782 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1787 "No enough segments for packet.");
1788 rte_pktmbuf_free(rx_pkts[nb_rx]);
1789 rxvq->stats.errors++;
1794 rxvq->stats.packets += nb_rx;
1796 /* Allocate new mbuf for the used descriptor */
1797 if (likely(!virtqueue_full(vq))) {
1798 /* free_cnt may include mrg descs */
1799 uint16_t free_cnt = vq->vq_free_cnt;
1800 struct rte_mbuf *new_pkts[free_cnt];
1802 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1803 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1805 if (unlikely(error)) {
1806 for (i = 0; i < free_cnt; i++)
1807 rte_pktmbuf_free(new_pkts[i]);
1809 nb_enqueued += free_cnt;
1811 struct rte_eth_dev *dev =
1812 &rte_eth_devices[rxvq->port_id];
1813 dev->data->rx_mbuf_alloc_failed += free_cnt;
1817 if (likely(nb_enqueued)) {
1818 vq_update_avail_idx(vq);
1820 if (unlikely(virtqueue_kick_prepare(vq))) {
1821 virtqueue_notify(vq);
1822 PMD_RX_LOG(DEBUG, "Notified");
1830 virtio_recv_mergeable_pkts_packed(void *rx_queue,
1831 struct rte_mbuf **rx_pkts,
1834 struct virtnet_rx *rxvq = rx_queue;
1835 struct virtqueue *vq = rxvq->vq;
1836 struct virtio_hw *hw = vq->hw;
1837 struct rte_mbuf *rxm;
1838 struct rte_mbuf *prev = NULL;
1839 uint16_t num, nb_rx = 0;
1840 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1841 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1842 uint32_t nb_enqueued = 0;
1843 uint32_t seg_num = 0;
1844 uint32_t seg_res = 0;
1845 uint32_t hdr_size = hw->vtnet_hdr_size;
1849 if (unlikely(hw->started == 0))
1854 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1855 num = VIRTIO_MBUF_BURST_SZ;
1856 if (likely(num > DESC_PER_CACHELINE))
1857 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1859 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1861 for (i = 0; i < num; i++) {
1862 struct virtio_net_hdr_mrg_rxbuf *header;
1864 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1865 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1869 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1870 PMD_RX_LOG(ERR, "Packet drop");
1872 virtio_discard_rxbuf(vq, rxm);
1873 rxvq->stats.errors++;
1877 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)
1878 rxm->buf_addr + RTE_PKTMBUF_HEADROOM - hdr_size);
1879 seg_num = header->num_buffers;
1884 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1885 rxm->nb_segs = seg_num;
1888 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1889 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1891 rxm->port = rxvq->port_id;
1892 rx_pkts[nb_rx] = rxm;
1895 if (hw->has_rx_offload &&
1896 virtio_rx_offload(rxm, &header->hdr) < 0) {
1897 virtio_discard_rxbuf(vq, rxm);
1898 rxvq->stats.errors++;
1903 rte_vlan_strip(rx_pkts[nb_rx]);
1905 seg_res = seg_num - 1;
1907 /* Merge remaining segments */
1908 while (seg_res != 0 && i < (num - 1)) {
1912 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1913 rxm->pkt_len = (uint32_t)(len[i]);
1914 rxm->data_len = (uint16_t)(len[i]);
1916 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1924 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1929 /* Last packet still need merge segments */
1930 while (seg_res != 0) {
1931 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1932 VIRTIO_MBUF_BURST_SZ);
1933 uint16_t extra_idx = 0;
1935 rcv_cnt = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts,
1937 if (unlikely(rcv_cnt == 0)) {
1938 PMD_RX_LOG(ERR, "No enough segments for packet.");
1939 rte_pktmbuf_free(rx_pkts[nb_rx]);
1940 rxvq->stats.errors++;
1944 while (extra_idx < rcv_cnt) {
1945 rxm = rcv_pkts[extra_idx];
1947 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1948 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1949 rxm->data_len = (uint16_t)(len[extra_idx]);
1953 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1958 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1963 rxvq->stats.packets += nb_rx;
1965 /* Allocate new mbuf for the used descriptor */
1966 if (likely(!virtqueue_full(vq))) {
1967 /* free_cnt may include mrg descs */
1968 uint16_t free_cnt = vq->vq_free_cnt;
1969 struct rte_mbuf *new_pkts[free_cnt];
1971 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1972 error = virtqueue_enqueue_recv_refill_packed(vq,
1973 new_pkts, free_cnt);
1974 if (unlikely(error)) {
1975 for (i = 0; i < free_cnt; i++)
1976 rte_pktmbuf_free(new_pkts[i]);
1978 nb_enqueued += free_cnt;
1980 struct rte_eth_dev *dev =
1981 &rte_eth_devices[rxvq->port_id];
1982 dev->data->rx_mbuf_alloc_failed += free_cnt;
1986 if (likely(nb_enqueued)) {
1987 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1988 virtqueue_notify(vq);
1989 PMD_RX_LOG(DEBUG, "Notified");
1997 virtio_xmit_pkts_prepare(void *tx_queue __rte_unused, struct rte_mbuf **tx_pkts,
2003 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2004 struct rte_mbuf *m = tx_pkts[nb_tx];
2006 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
2007 error = rte_validate_tx_offload(m);
2008 if (unlikely(error)) {
2014 /* Do VLAN tag insertion */
2015 if (unlikely(m->ol_flags & PKT_TX_VLAN_PKT)) {
2016 error = rte_vlan_insert(&m);
2017 /* rte_vlan_insert() may change pointer
2018 * even in the case of failure
2022 if (unlikely(error)) {
2028 error = rte_net_intel_cksum_prepare(m);
2029 if (unlikely(error)) {
2034 if (m->ol_flags & PKT_TX_TCP_SEG)
2035 virtio_tso_fix_cksum(m);
2042 virtio_xmit_pkts_packed(void *tx_queue, struct rte_mbuf **tx_pkts,
2045 struct virtnet_tx *txvq = tx_queue;
2046 struct virtqueue *vq = txvq->vq;
2047 struct virtio_hw *hw = vq->hw;
2048 uint16_t hdr_size = hw->vtnet_hdr_size;
2050 bool in_order = vtpci_with_feature(hw, VIRTIO_F_IN_ORDER);
2052 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2055 if (unlikely(nb_pkts < 1))
2058 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2060 if (nb_pkts > vq->vq_free_cnt)
2061 virtio_xmit_cleanup_packed(vq, nb_pkts - vq->vq_free_cnt,
2064 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2065 struct rte_mbuf *txm = tx_pkts[nb_tx];
2066 int can_push = 0, slots, need;
2068 /* optimize ring usage */
2069 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2070 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2071 rte_mbuf_refcnt_read(txm) == 1 &&
2072 RTE_MBUF_DIRECT(txm) &&
2073 txm->nb_segs == 1 &&
2074 rte_pktmbuf_headroom(txm) >= hdr_size &&
2075 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2076 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2079 /* How many main ring entries are needed to this Tx?
2080 * any_layout => number of segments
2081 * default => number of segments + 1
2083 slots = txm->nb_segs + !can_push;
2084 need = slots - vq->vq_free_cnt;
2086 /* Positive value indicates it need free vring descriptors */
2087 if (unlikely(need > 0)) {
2088 virtio_xmit_cleanup_packed(vq, need, in_order);
2089 need = slots - vq->vq_free_cnt;
2090 if (unlikely(need > 0)) {
2092 "No free tx descriptors to transmit");
2097 /* Enqueue Packet buffers */
2099 virtqueue_enqueue_xmit_packed_fast(txvq, txm, in_order);
2101 virtqueue_enqueue_xmit_packed(txvq, txm, slots, 0,
2104 virtio_update_packet_stats(&txvq->stats, txm);
2107 txvq->stats.packets += nb_tx;
2109 if (likely(nb_tx)) {
2110 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
2111 virtqueue_notify(vq);
2112 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2120 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
2122 struct virtnet_tx *txvq = tx_queue;
2123 struct virtqueue *vq = txvq->vq;
2124 struct virtio_hw *hw = vq->hw;
2125 uint16_t hdr_size = hw->vtnet_hdr_size;
2126 uint16_t nb_used, nb_tx = 0;
2128 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2131 if (unlikely(nb_pkts < 1))
2134 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2135 nb_used = VIRTQUEUE_NUSED(vq);
2137 virtio_rmb(hw->weak_barriers);
2138 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2139 virtio_xmit_cleanup(vq, nb_used);
2141 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2142 struct rte_mbuf *txm = tx_pkts[nb_tx];
2143 int can_push = 0, use_indirect = 0, slots, need;
2145 /* optimize ring usage */
2146 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2147 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2148 rte_mbuf_refcnt_read(txm) == 1 &&
2149 RTE_MBUF_DIRECT(txm) &&
2150 txm->nb_segs == 1 &&
2151 rte_pktmbuf_headroom(txm) >= hdr_size &&
2152 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2153 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2155 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
2156 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
2159 /* How many main ring entries are needed to this Tx?
2160 * any_layout => number of segments
2162 * default => number of segments + 1
2164 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
2165 need = slots - vq->vq_free_cnt;
2167 /* Positive value indicates it need free vring descriptors */
2168 if (unlikely(need > 0)) {
2169 nb_used = VIRTQUEUE_NUSED(vq);
2170 virtio_rmb(hw->weak_barriers);
2171 need = RTE_MIN(need, (int)nb_used);
2173 virtio_xmit_cleanup(vq, need);
2174 need = slots - vq->vq_free_cnt;
2175 if (unlikely(need > 0)) {
2177 "No free tx descriptors to transmit");
2182 /* Enqueue Packet buffers */
2183 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect,
2186 virtio_update_packet_stats(&txvq->stats, txm);
2189 txvq->stats.packets += nb_tx;
2191 if (likely(nb_tx)) {
2192 vq_update_avail_idx(vq);
2194 if (unlikely(virtqueue_kick_prepare(vq))) {
2195 virtqueue_notify(vq);
2196 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2203 static __rte_always_inline int
2204 virtio_xmit_try_cleanup_inorder(struct virtqueue *vq, uint16_t need)
2206 uint16_t nb_used, nb_clean, nb_descs;
2207 struct virtio_hw *hw = vq->hw;
2209 nb_descs = vq->vq_free_cnt + need;
2210 nb_used = VIRTQUEUE_NUSED(vq);
2211 virtio_rmb(hw->weak_barriers);
2212 nb_clean = RTE_MIN(need, (int)nb_used);
2214 virtio_xmit_cleanup_inorder(vq, nb_clean);
2216 return nb_descs - vq->vq_free_cnt;
2220 virtio_xmit_pkts_inorder(void *tx_queue,
2221 struct rte_mbuf **tx_pkts,
2224 struct virtnet_tx *txvq = tx_queue;
2225 struct virtqueue *vq = txvq->vq;
2226 struct virtio_hw *hw = vq->hw;
2227 uint16_t hdr_size = hw->vtnet_hdr_size;
2228 uint16_t nb_used, nb_tx = 0, nb_inorder_pkts = 0;
2229 struct rte_mbuf *inorder_pkts[nb_pkts];
2232 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2235 if (unlikely(nb_pkts < 1))
2239 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2240 nb_used = VIRTQUEUE_NUSED(vq);
2242 virtio_rmb(hw->weak_barriers);
2243 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2244 virtio_xmit_cleanup_inorder(vq, nb_used);
2246 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2247 struct rte_mbuf *txm = tx_pkts[nb_tx];
2250 /* optimize ring usage */
2251 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2252 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2253 rte_mbuf_refcnt_read(txm) == 1 &&
2254 RTE_MBUF_DIRECT(txm) &&
2255 txm->nb_segs == 1 &&
2256 rte_pktmbuf_headroom(txm) >= hdr_size &&
2257 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2258 __alignof__(struct virtio_net_hdr_mrg_rxbuf))) {
2259 inorder_pkts[nb_inorder_pkts] = txm;
2265 if (nb_inorder_pkts) {
2266 need = nb_inorder_pkts - vq->vq_free_cnt;
2267 if (unlikely(need > 0)) {
2268 need = virtio_xmit_try_cleanup_inorder(vq,
2270 if (unlikely(need > 0)) {
2272 "No free tx descriptors to "
2277 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2279 nb_inorder_pkts = 0;
2282 slots = txm->nb_segs + 1;
2283 need = slots - vq->vq_free_cnt;
2284 if (unlikely(need > 0)) {
2285 need = virtio_xmit_try_cleanup_inorder(vq, slots);
2287 if (unlikely(need > 0)) {
2289 "No free tx descriptors to transmit");
2293 /* Enqueue Packet buffers */
2294 virtqueue_enqueue_xmit(txvq, txm, slots, 0, 0, 1);
2296 virtio_update_packet_stats(&txvq->stats, txm);
2299 /* Transmit all inorder packets */
2300 if (nb_inorder_pkts) {
2301 need = nb_inorder_pkts - vq->vq_free_cnt;
2302 if (unlikely(need > 0)) {
2303 need = virtio_xmit_try_cleanup_inorder(vq,
2305 if (unlikely(need > 0)) {
2307 "No free tx descriptors to transmit");
2308 nb_inorder_pkts = vq->vq_free_cnt;
2313 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2317 txvq->stats.packets += nb_tx;
2319 if (likely(nb_tx)) {
2320 vq_update_avail_idx(vq);
2322 if (unlikely(virtqueue_kick_prepare(vq))) {
2323 virtqueue_notify(vq);
2324 PMD_TX_LOG(DEBUG, "Notified backend after xmit");