1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_cycles.h>
12 #include <rte_memory.h>
13 #include <rte_branch_prediction.h>
14 #include <rte_mempool.h>
15 #include <rte_malloc.h>
17 #include <rte_ether.h>
18 #include <rte_ethdev_driver.h>
19 #include <rte_prefetch.h>
20 #include <rte_string_fns.h>
21 #include <rte_errno.h>
22 #include <rte_byteorder.h>
28 #include "virtio_logs.h"
29 #include "virtio_ethdev.h"
30 #include "virtio_pci.h"
31 #include "virtqueue.h"
32 #include "virtio_rxtx.h"
33 #include "virtio_rxtx_simple.h"
34 #include "virtio_ring.h"
36 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
37 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
39 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0)
43 virtio_dev_rx_queue_done(void *rxq, uint16_t offset)
45 struct virtnet_rx *rxvq = rxq;
46 struct virtqueue *vq = rxvq->vq;
48 return VIRTQUEUE_NUSED(vq) >= offset;
52 vq_ring_free_inorder(struct virtqueue *vq, uint16_t desc_idx, uint16_t num)
54 vq->vq_free_cnt += num;
55 vq->vq_desc_tail_idx = desc_idx & (vq->vq_nentries - 1);
59 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
61 struct vring_desc *dp, *dp_tail;
62 struct vq_desc_extra *dxp;
63 uint16_t desc_idx_last = desc_idx;
65 dp = &vq->vq_split.ring.desc[desc_idx];
66 dxp = &vq->vq_descx[desc_idx];
67 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
68 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
69 while (dp->flags & VRING_DESC_F_NEXT) {
70 desc_idx_last = dp->next;
71 dp = &vq->vq_split.ring.desc[dp->next];
77 * We must append the existing free chain, if any, to the end of
78 * newly freed chain. If the virtqueue was completely used, then
79 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
81 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
82 vq->vq_desc_head_idx = desc_idx;
84 dp_tail = &vq->vq_split.ring.desc[vq->vq_desc_tail_idx];
85 dp_tail->next = desc_idx;
88 vq->vq_desc_tail_idx = desc_idx_last;
89 dp->next = VQ_RING_DESC_CHAIN_END;
93 vq_ring_free_id_packed(struct virtqueue *vq, uint16_t id)
95 struct vq_desc_extra *dxp;
97 dxp = &vq->vq_descx[id];
98 vq->vq_free_cnt += dxp->ndescs;
100 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END)
101 vq->vq_desc_head_idx = id;
103 vq->vq_descx[vq->vq_desc_tail_idx].next = id;
105 vq->vq_desc_tail_idx = id;
106 dxp->next = VQ_RING_DESC_CHAIN_END;
110 virtqueue_dequeue_burst_rx_packed(struct virtqueue *vq,
111 struct rte_mbuf **rx_pkts,
115 struct rte_mbuf *cookie;
118 struct vring_packed_desc *desc;
121 desc = vq->vq_packed.ring.desc;
123 for (i = 0; i < num; i++) {
124 used_idx = vq->vq_used_cons_idx;
125 if (!desc_is_used(&desc[used_idx], vq))
127 virtio_rmb(vq->hw->weak_barriers);
128 len[i] = desc[used_idx].len;
129 id = desc[used_idx].id;
130 cookie = (struct rte_mbuf *)vq->vq_descx[id].cookie;
131 if (unlikely(cookie == NULL)) {
132 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
133 vq->vq_used_cons_idx);
136 rte_prefetch0(cookie);
137 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
141 vq->vq_used_cons_idx++;
142 if (vq->vq_used_cons_idx >= vq->vq_nentries) {
143 vq->vq_used_cons_idx -= vq->vq_nentries;
144 vq->vq_packed.used_wrap_counter ^= 1;
152 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
153 uint32_t *len, uint16_t num)
155 struct vring_used_elem *uep;
156 struct rte_mbuf *cookie;
157 uint16_t used_idx, desc_idx;
160 /* Caller does the check */
161 for (i = 0; i < num ; i++) {
162 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
163 uep = &vq->vq_split.ring.used->ring[used_idx];
164 desc_idx = (uint16_t) uep->id;
166 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
168 if (unlikely(cookie == NULL)) {
169 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
170 vq->vq_used_cons_idx);
174 rte_prefetch0(cookie);
175 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
177 vq->vq_used_cons_idx++;
178 vq_ring_free_chain(vq, desc_idx);
179 vq->vq_descx[desc_idx].cookie = NULL;
186 virtqueue_dequeue_rx_inorder(struct virtqueue *vq,
187 struct rte_mbuf **rx_pkts,
191 struct vring_used_elem *uep;
192 struct rte_mbuf *cookie;
193 uint16_t used_idx = 0;
196 if (unlikely(num == 0))
199 for (i = 0; i < num; i++) {
200 used_idx = vq->vq_used_cons_idx & (vq->vq_nentries - 1);
201 /* Desc idx same as used idx */
202 uep = &vq->vq_split.ring.used->ring[used_idx];
204 cookie = (struct rte_mbuf *)vq->vq_descx[used_idx].cookie;
206 if (unlikely(cookie == NULL)) {
207 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
208 vq->vq_used_cons_idx);
212 rte_prefetch0(cookie);
213 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
215 vq->vq_used_cons_idx++;
216 vq->vq_descx[used_idx].cookie = NULL;
219 vq_ring_free_inorder(vq, used_idx, i);
223 #ifndef DEFAULT_TX_FREE_THRESH
224 #define DEFAULT_TX_FREE_THRESH 32
228 virtio_xmit_cleanup_inorder_packed(struct virtqueue *vq, int num)
230 uint16_t used_idx, id, curr_id, free_cnt = 0;
231 uint16_t size = vq->vq_nentries;
232 struct vring_packed_desc *desc = vq->vq_packed.ring.desc;
233 struct vq_desc_extra *dxp;
235 used_idx = vq->vq_used_cons_idx;
236 while (num > 0 && desc_is_used(&desc[used_idx], vq)) {
237 virtio_rmb(vq->hw->weak_barriers);
238 id = desc[used_idx].id;
241 dxp = &vq->vq_descx[used_idx];
242 used_idx += dxp->ndescs;
243 free_cnt += dxp->ndescs;
245 if (used_idx >= size) {
247 vq->vq_packed.used_wrap_counter ^= 1;
249 if (dxp->cookie != NULL) {
250 rte_pktmbuf_free(dxp->cookie);
253 } while (curr_id != id);
255 vq->vq_used_cons_idx = used_idx;
256 vq->vq_free_cnt += free_cnt;
260 virtio_xmit_cleanup_normal_packed(struct virtqueue *vq, int num)
262 uint16_t used_idx, id;
263 uint16_t size = vq->vq_nentries;
264 struct vring_packed_desc *desc = vq->vq_packed.ring.desc;
265 struct vq_desc_extra *dxp;
267 used_idx = vq->vq_used_cons_idx;
268 while (num-- && desc_is_used(&desc[used_idx], vq)) {
269 virtio_rmb(vq->hw->weak_barriers);
270 id = desc[used_idx].id;
271 dxp = &vq->vq_descx[id];
272 vq->vq_used_cons_idx += dxp->ndescs;
273 if (vq->vq_used_cons_idx >= size) {
274 vq->vq_used_cons_idx -= size;
275 vq->vq_packed.used_wrap_counter ^= 1;
277 vq_ring_free_id_packed(vq, id);
278 if (dxp->cookie != NULL) {
279 rte_pktmbuf_free(dxp->cookie);
282 used_idx = vq->vq_used_cons_idx;
286 /* Cleanup from completed transmits. */
288 virtio_xmit_cleanup_packed(struct virtqueue *vq, int num, int in_order)
291 virtio_xmit_cleanup_inorder_packed(vq, num);
293 virtio_xmit_cleanup_normal_packed(vq, num);
297 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
299 uint16_t i, used_idx, desc_idx;
300 for (i = 0; i < num; i++) {
301 struct vring_used_elem *uep;
302 struct vq_desc_extra *dxp;
304 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
305 uep = &vq->vq_split.ring.used->ring[used_idx];
307 desc_idx = (uint16_t) uep->id;
308 dxp = &vq->vq_descx[desc_idx];
309 vq->vq_used_cons_idx++;
310 vq_ring_free_chain(vq, desc_idx);
312 if (dxp->cookie != NULL) {
313 rte_pktmbuf_free(dxp->cookie);
319 /* Cleanup from completed inorder transmits. */
321 virtio_xmit_cleanup_inorder(struct virtqueue *vq, uint16_t num)
323 uint16_t i, idx = vq->vq_used_cons_idx;
324 int16_t free_cnt = 0;
325 struct vq_desc_extra *dxp = NULL;
327 if (unlikely(num == 0))
330 for (i = 0; i < num; i++) {
331 dxp = &vq->vq_descx[idx++ & (vq->vq_nentries - 1)];
332 free_cnt += dxp->ndescs;
333 if (dxp->cookie != NULL) {
334 rte_pktmbuf_free(dxp->cookie);
339 vq->vq_free_cnt += free_cnt;
340 vq->vq_used_cons_idx = idx;
344 virtqueue_enqueue_refill_inorder(struct virtqueue *vq,
345 struct rte_mbuf **cookies,
348 struct vq_desc_extra *dxp;
349 struct virtio_hw *hw = vq->hw;
350 struct vring_desc *start_dp;
351 uint16_t head_idx, idx, i = 0;
353 if (unlikely(vq->vq_free_cnt == 0))
355 if (unlikely(vq->vq_free_cnt < num))
358 head_idx = vq->vq_desc_head_idx & (vq->vq_nentries - 1);
359 start_dp = vq->vq_split.ring.desc;
362 idx = head_idx & (vq->vq_nentries - 1);
363 dxp = &vq->vq_descx[idx];
364 dxp->cookie = (void *)cookies[i];
368 VIRTIO_MBUF_ADDR(cookies[i], vq) +
369 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
371 cookies[i]->buf_len -
372 RTE_PKTMBUF_HEADROOM +
374 start_dp[idx].flags = VRING_DESC_F_WRITE;
376 vq_update_avail_ring(vq, idx);
381 vq->vq_desc_head_idx += num;
382 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
387 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf **cookie,
390 struct vq_desc_extra *dxp;
391 struct virtio_hw *hw = vq->hw;
392 struct vring_desc *start_dp = vq->vq_split.ring.desc;
395 if (unlikely(vq->vq_free_cnt == 0))
397 if (unlikely(vq->vq_free_cnt < num))
400 if (unlikely(vq->vq_desc_head_idx >= vq->vq_nentries))
403 for (i = 0; i < num; i++) {
404 idx = vq->vq_desc_head_idx;
405 dxp = &vq->vq_descx[idx];
406 dxp->cookie = (void *)cookie[i];
410 VIRTIO_MBUF_ADDR(cookie[i], vq) +
411 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
413 cookie[i]->buf_len - RTE_PKTMBUF_HEADROOM +
415 start_dp[idx].flags = VRING_DESC_F_WRITE;
416 vq->vq_desc_head_idx = start_dp[idx].next;
417 vq_update_avail_ring(vq, idx);
418 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END) {
419 vq->vq_desc_tail_idx = vq->vq_desc_head_idx;
424 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
430 virtqueue_enqueue_recv_refill_packed(struct virtqueue *vq,
431 struct rte_mbuf **cookie, uint16_t num)
433 struct vring_packed_desc *start_dp = vq->vq_packed.ring.desc;
434 uint16_t flags = vq->vq_packed.cached_flags;
435 struct virtio_hw *hw = vq->hw;
436 struct vq_desc_extra *dxp;
440 if (unlikely(vq->vq_free_cnt == 0))
442 if (unlikely(vq->vq_free_cnt < num))
445 for (i = 0; i < num; i++) {
446 idx = vq->vq_avail_idx;
447 dxp = &vq->vq_descx[idx];
448 dxp->cookie = (void *)cookie[i];
451 start_dp[idx].addr = VIRTIO_MBUF_ADDR(cookie[i], vq) +
452 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
453 start_dp[idx].len = cookie[i]->buf_len - RTE_PKTMBUF_HEADROOM
454 + hw->vtnet_hdr_size;
456 vq->vq_desc_head_idx = dxp->next;
457 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
458 vq->vq_desc_tail_idx = vq->vq_desc_head_idx;
459 virtio_wmb(hw->weak_barriers);
460 start_dp[idx].flags = flags;
461 if (++vq->vq_avail_idx >= vq->vq_nentries) {
462 vq->vq_avail_idx -= vq->vq_nentries;
463 vq->vq_packed.cached_flags ^=
464 VRING_PACKED_DESC_F_AVAIL_USED;
465 flags = vq->vq_packed.cached_flags;
468 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
472 /* When doing TSO, the IP length is not included in the pseudo header
473 * checksum of the packet given to the PMD, but for virtio it is
477 virtio_tso_fix_cksum(struct rte_mbuf *m)
479 /* common case: header is not fragmented */
480 if (likely(rte_pktmbuf_data_len(m) >= m->l2_len + m->l3_len +
482 struct ipv4_hdr *iph;
483 struct ipv6_hdr *ip6h;
485 uint16_t prev_cksum, new_cksum, ip_len, ip_paylen;
488 iph = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *, m->l2_len);
489 th = RTE_PTR_ADD(iph, m->l3_len);
490 if ((iph->version_ihl >> 4) == 4) {
491 iph->hdr_checksum = 0;
492 iph->hdr_checksum = rte_ipv4_cksum(iph);
493 ip_len = iph->total_length;
494 ip_paylen = rte_cpu_to_be_16(rte_be_to_cpu_16(ip_len) -
497 ip6h = (struct ipv6_hdr *)iph;
498 ip_paylen = ip6h->payload_len;
501 /* calculate the new phdr checksum not including ip_paylen */
502 prev_cksum = th->cksum;
505 tmp = (tmp & 0xffff) + (tmp >> 16);
508 /* replace it in the packet */
509 th->cksum = new_cksum;
514 /* avoid write operation when necessary, to lessen cache issues */
515 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
516 if ((var) != (val)) \
520 #define virtqueue_clear_net_hdr(_hdr) do { \
521 ASSIGN_UNLESS_EQUAL((_hdr)->csum_start, 0); \
522 ASSIGN_UNLESS_EQUAL((_hdr)->csum_offset, 0); \
523 ASSIGN_UNLESS_EQUAL((_hdr)->flags, 0); \
524 ASSIGN_UNLESS_EQUAL((_hdr)->gso_type, 0); \
525 ASSIGN_UNLESS_EQUAL((_hdr)->gso_size, 0); \
526 ASSIGN_UNLESS_EQUAL((_hdr)->hdr_len, 0); \
530 virtqueue_xmit_offload(struct virtio_net_hdr *hdr,
531 struct rte_mbuf *cookie,
535 if (cookie->ol_flags & PKT_TX_TCP_SEG)
536 cookie->ol_flags |= PKT_TX_TCP_CKSUM;
538 switch (cookie->ol_flags & PKT_TX_L4_MASK) {
539 case PKT_TX_UDP_CKSUM:
540 hdr->csum_start = cookie->l2_len + cookie->l3_len;
541 hdr->csum_offset = offsetof(struct udp_hdr,
543 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
546 case PKT_TX_TCP_CKSUM:
547 hdr->csum_start = cookie->l2_len + cookie->l3_len;
548 hdr->csum_offset = offsetof(struct tcp_hdr, cksum);
549 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
553 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
554 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
555 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
559 /* TCP Segmentation Offload */
560 if (cookie->ol_flags & PKT_TX_TCP_SEG) {
561 virtio_tso_fix_cksum(cookie);
562 hdr->gso_type = (cookie->ol_flags & PKT_TX_IPV6) ?
563 VIRTIO_NET_HDR_GSO_TCPV6 :
564 VIRTIO_NET_HDR_GSO_TCPV4;
565 hdr->gso_size = cookie->tso_segsz;
571 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
572 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
573 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
579 virtqueue_enqueue_xmit_inorder(struct virtnet_tx *txvq,
580 struct rte_mbuf **cookies,
583 struct vq_desc_extra *dxp;
584 struct virtqueue *vq = txvq->vq;
585 struct vring_desc *start_dp;
586 struct virtio_net_hdr *hdr;
588 uint16_t head_size = vq->hw->vtnet_hdr_size;
591 idx = vq->vq_desc_head_idx;
592 start_dp = vq->vq_split.ring.desc;
595 idx = idx & (vq->vq_nentries - 1);
596 dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
597 dxp->cookie = (void *)cookies[i];
600 hdr = (struct virtio_net_hdr *)
601 rte_pktmbuf_prepend(cookies[i], head_size);
602 cookies[i]->pkt_len -= head_size;
604 /* if offload disabled, hdr is not zeroed yet, do it now */
605 if (!vq->hw->has_tx_offload)
606 virtqueue_clear_net_hdr(hdr);
608 virtqueue_xmit_offload(hdr, cookies[i], true);
610 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookies[i], vq);
611 start_dp[idx].len = cookies[i]->data_len;
612 start_dp[idx].flags = 0;
614 vq_update_avail_ring(vq, idx);
620 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
621 vq->vq_desc_head_idx = idx & (vq->vq_nentries - 1);
625 virtqueue_enqueue_xmit_packed_fast(struct virtnet_tx *txvq,
626 struct rte_mbuf *cookie,
629 struct virtqueue *vq = txvq->vq;
630 struct vring_packed_desc *dp;
631 struct vq_desc_extra *dxp;
632 uint16_t idx, id, flags;
633 uint16_t head_size = vq->hw->vtnet_hdr_size;
634 struct virtio_net_hdr *hdr;
636 id = in_order ? vq->vq_avail_idx : vq->vq_desc_head_idx;
637 idx = vq->vq_avail_idx;
638 dp = &vq->vq_packed.ring.desc[idx];
640 dxp = &vq->vq_descx[id];
642 dxp->cookie = cookie;
644 flags = vq->vq_packed.cached_flags;
646 /* prepend cannot fail, checked by caller */
647 hdr = (struct virtio_net_hdr *)
648 rte_pktmbuf_prepend(cookie, head_size);
649 cookie->pkt_len -= head_size;
651 /* if offload disabled, hdr is not zeroed yet, do it now */
652 if (!vq->hw->has_tx_offload)
653 virtqueue_clear_net_hdr(hdr);
655 virtqueue_xmit_offload(hdr, cookie, true);
657 dp->addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
658 dp->len = cookie->data_len;
661 if (++vq->vq_avail_idx >= vq->vq_nentries) {
662 vq->vq_avail_idx -= vq->vq_nentries;
663 vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
669 vq->vq_desc_head_idx = dxp->next;
670 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
671 vq->vq_desc_tail_idx = VQ_RING_DESC_CHAIN_END;
674 virtio_wmb(vq->hw->weak_barriers);
679 virtqueue_enqueue_xmit_packed(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
680 uint16_t needed, int can_push, int in_order)
682 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
683 struct vq_desc_extra *dxp;
684 struct virtqueue *vq = txvq->vq;
685 struct vring_packed_desc *start_dp, *head_dp;
686 uint16_t idx, id, head_idx, head_flags;
687 uint16_t head_size = vq->hw->vtnet_hdr_size;
688 struct virtio_net_hdr *hdr;
691 id = in_order ? vq->vq_avail_idx : vq->vq_desc_head_idx;
693 dxp = &vq->vq_descx[id];
694 dxp->ndescs = needed;
695 dxp->cookie = cookie;
697 head_idx = vq->vq_avail_idx;
700 start_dp = vq->vq_packed.ring.desc;
702 head_dp = &vq->vq_packed.ring.desc[idx];
703 head_flags = cookie->next ? VRING_DESC_F_NEXT : 0;
704 head_flags |= vq->vq_packed.cached_flags;
707 /* prepend cannot fail, checked by caller */
708 hdr = (struct virtio_net_hdr *)
709 rte_pktmbuf_prepend(cookie, head_size);
710 /* rte_pktmbuf_prepend() counts the hdr size to the pkt length,
711 * which is wrong. Below subtract restores correct pkt size.
713 cookie->pkt_len -= head_size;
715 /* if offload disabled, it is not zeroed below, do it now */
716 if (!vq->hw->has_tx_offload)
717 virtqueue_clear_net_hdr(hdr);
719 /* setup first tx ring slot to point to header
720 * stored in reserved region.
722 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
723 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
724 start_dp[idx].len = vq->hw->vtnet_hdr_size;
725 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
727 if (idx >= vq->vq_nentries) {
728 idx -= vq->vq_nentries;
729 vq->vq_packed.cached_flags ^=
730 VRING_PACKED_DESC_F_AVAIL_USED;
734 virtqueue_xmit_offload(hdr, cookie, vq->hw->has_tx_offload);
739 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
740 start_dp[idx].len = cookie->data_len;
741 if (likely(idx != head_idx)) {
742 flags = cookie->next ? VRING_DESC_F_NEXT : 0;
743 flags |= vq->vq_packed.cached_flags;
744 start_dp[idx].flags = flags;
748 if (idx >= vq->vq_nentries) {
749 idx -= vq->vq_nentries;
750 vq->vq_packed.cached_flags ^=
751 VRING_PACKED_DESC_F_AVAIL_USED;
753 } while ((cookie = cookie->next) != NULL);
755 start_dp[prev].id = id;
757 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
758 vq->vq_avail_idx = idx;
761 vq->vq_desc_head_idx = dxp->next;
762 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
763 vq->vq_desc_tail_idx = VQ_RING_DESC_CHAIN_END;
766 virtio_wmb(vq->hw->weak_barriers);
767 head_dp->flags = head_flags;
771 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
772 uint16_t needed, int use_indirect, int can_push,
775 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
776 struct vq_desc_extra *dxp;
777 struct virtqueue *vq = txvq->vq;
778 struct vring_desc *start_dp;
779 uint16_t seg_num = cookie->nb_segs;
780 uint16_t head_idx, idx;
781 uint16_t head_size = vq->hw->vtnet_hdr_size;
782 struct virtio_net_hdr *hdr;
784 head_idx = vq->vq_desc_head_idx;
787 dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
789 dxp = &vq->vq_descx[idx];
790 dxp->cookie = (void *)cookie;
791 dxp->ndescs = needed;
793 start_dp = vq->vq_split.ring.desc;
796 /* prepend cannot fail, checked by caller */
797 hdr = (struct virtio_net_hdr *)
798 rte_pktmbuf_prepend(cookie, head_size);
799 /* rte_pktmbuf_prepend() counts the hdr size to the pkt length,
800 * which is wrong. Below subtract restores correct pkt size.
802 cookie->pkt_len -= head_size;
804 /* if offload disabled, it is not zeroed below, do it now */
805 if (!vq->hw->has_tx_offload)
806 virtqueue_clear_net_hdr(hdr);
807 } else if (use_indirect) {
808 /* setup tx ring slot to point to indirect
809 * descriptor list stored in reserved region.
811 * the first slot in indirect ring is already preset
812 * to point to the header in reserved region
814 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
815 RTE_PTR_DIFF(&txr[idx].tx_indir, txr);
816 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
817 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
818 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
820 /* loop below will fill in rest of the indirect elements */
821 start_dp = txr[idx].tx_indir;
824 /* setup first tx ring slot to point to header
825 * stored in reserved region.
827 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
828 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
829 start_dp[idx].len = vq->hw->vtnet_hdr_size;
830 start_dp[idx].flags = VRING_DESC_F_NEXT;
831 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
833 idx = start_dp[idx].next;
836 virtqueue_xmit_offload(hdr, cookie, vq->hw->has_tx_offload);
839 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
840 start_dp[idx].len = cookie->data_len;
841 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
842 idx = start_dp[idx].next;
843 } while ((cookie = cookie->next) != NULL);
846 idx = vq->vq_split.ring.desc[head_idx].next;
848 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
850 vq->vq_desc_head_idx = idx;
851 vq_update_avail_ring(vq, head_idx);
854 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
855 vq->vq_desc_tail_idx = idx;
860 virtio_dev_cq_start(struct rte_eth_dev *dev)
862 struct virtio_hw *hw = dev->data->dev_private;
864 if (hw->cvq && hw->cvq->vq) {
865 rte_spinlock_init(&hw->cvq->lock);
866 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
871 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
874 unsigned int socket_id __rte_unused,
875 const struct rte_eth_rxconf *rx_conf __rte_unused,
876 struct rte_mempool *mp)
878 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
879 struct virtio_hw *hw = dev->data->dev_private;
880 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
881 struct virtnet_rx *rxvq;
883 PMD_INIT_FUNC_TRACE();
885 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
886 nb_desc = vq->vq_nentries;
887 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
890 rxvq->queue_id = queue_idx;
892 if (rxvq->mpool == NULL) {
893 rte_exit(EXIT_FAILURE,
894 "Cannot allocate mbufs for rx virtqueue");
897 dev->data->rx_queues[queue_idx] = rxvq;
903 virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
905 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
906 struct virtio_hw *hw = dev->data->dev_private;
907 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
908 struct virtnet_rx *rxvq = &vq->rxq;
913 PMD_INIT_FUNC_TRACE();
915 /* Allocate blank mbufs for the each rx descriptor */
918 if (hw->use_simple_rx) {
919 for (desc_idx = 0; desc_idx < vq->vq_nentries;
921 vq->vq_split.ring.avail->ring[desc_idx] = desc_idx;
922 vq->vq_split.ring.desc[desc_idx].flags =
926 virtio_rxq_vec_setup(rxvq);
929 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
930 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
932 vq->sw_ring[vq->vq_nentries + desc_idx] =
936 if (hw->use_simple_rx) {
937 while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
938 virtio_rxq_rearm_vec(rxvq);
939 nbufs += RTE_VIRTIO_VPMD_RX_REARM_THRESH;
941 } else if (hw->use_inorder_rx) {
942 if ((!virtqueue_full(vq))) {
943 uint16_t free_cnt = vq->vq_free_cnt;
944 struct rte_mbuf *pkts[free_cnt];
946 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, pkts,
948 error = virtqueue_enqueue_refill_inorder(vq,
951 if (unlikely(error)) {
952 for (i = 0; i < free_cnt; i++)
953 rte_pktmbuf_free(pkts[i]);
958 vq_update_avail_idx(vq);
961 while (!virtqueue_full(vq)) {
962 m = rte_mbuf_raw_alloc(rxvq->mpool);
966 /* Enqueue allocated buffers */
967 if (vtpci_packed_queue(vq->hw))
968 error = virtqueue_enqueue_recv_refill_packed(vq,
971 error = virtqueue_enqueue_recv_refill(vq,
980 if (!vtpci_packed_queue(vq->hw))
981 vq_update_avail_idx(vq);
984 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
992 * struct rte_eth_dev *dev: Used to update dev
993 * uint16_t nb_desc: Defaults to values read from config space
994 * unsigned int socket_id: Used to allocate memzone
995 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
996 * uint16_t queue_idx: Just used as an index in dev txq list
999 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
1002 unsigned int socket_id __rte_unused,
1003 const struct rte_eth_txconf *tx_conf)
1005 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1006 struct virtio_hw *hw = dev->data->dev_private;
1007 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1008 struct virtnet_tx *txvq;
1009 uint16_t tx_free_thresh;
1011 PMD_INIT_FUNC_TRACE();
1013 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
1014 nb_desc = vq->vq_nentries;
1015 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
1018 txvq->queue_id = queue_idx;
1020 tx_free_thresh = tx_conf->tx_free_thresh;
1021 if (tx_free_thresh == 0)
1023 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
1025 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
1026 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
1027 "number of TX entries minus 3 (%u)."
1028 " (tx_free_thresh=%u port=%u queue=%u)\n",
1029 vq->vq_nentries - 3,
1030 tx_free_thresh, dev->data->port_id, queue_idx);
1034 vq->vq_free_thresh = tx_free_thresh;
1036 dev->data->tx_queues[queue_idx] = txvq;
1041 virtio_dev_tx_queue_setup_finish(struct rte_eth_dev *dev,
1044 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1045 struct virtio_hw *hw = dev->data->dev_private;
1046 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1048 PMD_INIT_FUNC_TRACE();
1050 if (!vtpci_packed_queue(hw)) {
1051 if (hw->use_inorder_tx)
1052 vq->vq_split.ring.desc[vq->vq_nentries - 1].next = 0;
1061 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
1065 * Requeue the discarded mbuf. This should always be
1066 * successful since it was just dequeued.
1068 if (vtpci_packed_queue(vq->hw))
1069 error = virtqueue_enqueue_recv_refill_packed(vq, &m, 1);
1071 error = virtqueue_enqueue_recv_refill(vq, &m, 1);
1073 if (unlikely(error)) {
1074 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
1075 rte_pktmbuf_free(m);
1080 virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)
1084 error = virtqueue_enqueue_refill_inorder(vq, &m, 1);
1085 if (unlikely(error)) {
1086 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
1087 rte_pktmbuf_free(m);
1092 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
1094 uint32_t s = mbuf->pkt_len;
1095 struct rte_ether_addr *ea;
1100 stats->size_bins[1]++;
1101 } else if (s > 64 && s < 1024) {
1104 /* count zeros, and offset into correct bin */
1105 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
1106 stats->size_bins[bin]++;
1109 stats->size_bins[0]++;
1111 stats->size_bins[6]++;
1113 stats->size_bins[7]++;
1116 ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
1117 if (is_multicast_ether_addr(ea)) {
1118 if (is_broadcast_ether_addr(ea))
1126 virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
1128 VIRTIO_DUMP_PACKET(m, m->data_len);
1130 virtio_update_packet_stats(&rxvq->stats, m);
1133 /* Optionally fill offload information in structure */
1135 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
1137 struct rte_net_hdr_lens hdr_lens;
1138 uint32_t hdrlen, ptype;
1139 int l4_supported = 0;
1142 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1145 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
1147 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
1148 m->packet_type = ptype;
1149 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
1150 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
1151 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
1154 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1155 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
1156 if (hdr->csum_start <= hdrlen && l4_supported) {
1157 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
1159 /* Unknown proto or tunnel, do sw cksum. We can assume
1160 * the cksum field is in the first segment since the
1161 * buffers we provided to the host are large enough.
1162 * In case of SCTP, this will be wrong since it's a CRC
1163 * but there's nothing we can do.
1165 uint16_t csum = 0, off;
1167 rte_raw_cksum_mbuf(m, hdr->csum_start,
1168 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
1170 if (likely(csum != 0xffff))
1172 off = hdr->csum_offset + hdr->csum_start;
1173 if (rte_pktmbuf_data_len(m) >= off + 1)
1174 *rte_pktmbuf_mtod_offset(m, uint16_t *,
1177 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
1178 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
1181 /* GSO request, save required information in mbuf */
1182 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1183 /* Check unsupported modes */
1184 if ((hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) ||
1185 (hdr->gso_size == 0)) {
1189 /* Update mss lengthes in mbuf */
1190 m->tso_segsz = hdr->gso_size;
1191 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1192 case VIRTIO_NET_HDR_GSO_TCPV4:
1193 case VIRTIO_NET_HDR_GSO_TCPV6:
1194 m->ol_flags |= PKT_RX_LRO | \
1195 PKT_RX_L4_CKSUM_NONE;
1205 #define VIRTIO_MBUF_BURST_SZ 64
1206 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
1208 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1210 struct virtnet_rx *rxvq = rx_queue;
1211 struct virtqueue *vq = rxvq->vq;
1212 struct virtio_hw *hw = vq->hw;
1213 struct rte_mbuf *rxm;
1214 uint16_t nb_used, num, nb_rx;
1215 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1216 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1218 uint32_t i, nb_enqueued;
1220 struct virtio_net_hdr *hdr;
1223 if (unlikely(hw->started == 0))
1226 nb_used = VIRTQUEUE_NUSED(vq);
1228 virtio_rmb(hw->weak_barriers);
1230 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1231 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1232 num = VIRTIO_MBUF_BURST_SZ;
1233 if (likely(num > DESC_PER_CACHELINE))
1234 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1236 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1237 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
1240 hdr_size = hw->vtnet_hdr_size;
1242 for (i = 0; i < num ; i++) {
1245 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1247 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1248 PMD_RX_LOG(ERR, "Packet drop");
1250 virtio_discard_rxbuf(vq, rxm);
1251 rxvq->stats.errors++;
1255 rxm->port = rxvq->port_id;
1256 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1260 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1261 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1263 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1264 RTE_PKTMBUF_HEADROOM - hdr_size);
1267 rte_vlan_strip(rxm);
1269 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1270 virtio_discard_rxbuf(vq, rxm);
1271 rxvq->stats.errors++;
1275 virtio_rx_stats_updated(rxvq, rxm);
1277 rx_pkts[nb_rx++] = rxm;
1280 rxvq->stats.packets += nb_rx;
1282 /* Allocate new mbuf for the used descriptor */
1283 if (likely(!virtqueue_full(vq))) {
1284 uint16_t free_cnt = vq->vq_free_cnt;
1285 struct rte_mbuf *new_pkts[free_cnt];
1287 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1289 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1291 if (unlikely(error)) {
1292 for (i = 0; i < free_cnt; i++)
1293 rte_pktmbuf_free(new_pkts[i]);
1295 nb_enqueued += free_cnt;
1297 struct rte_eth_dev *dev =
1298 &rte_eth_devices[rxvq->port_id];
1299 dev->data->rx_mbuf_alloc_failed += free_cnt;
1303 if (likely(nb_enqueued)) {
1304 vq_update_avail_idx(vq);
1306 if (unlikely(virtqueue_kick_prepare(vq))) {
1307 virtqueue_notify(vq);
1308 PMD_RX_LOG(DEBUG, "Notified");
1316 virtio_recv_pkts_packed(void *rx_queue, struct rte_mbuf **rx_pkts,
1319 struct virtnet_rx *rxvq = rx_queue;
1320 struct virtqueue *vq = rxvq->vq;
1321 struct virtio_hw *hw = vq->hw;
1322 struct rte_mbuf *rxm;
1323 uint16_t num, nb_rx;
1324 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1325 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1327 uint32_t i, nb_enqueued;
1329 struct virtio_net_hdr *hdr;
1332 if (unlikely(hw->started == 0))
1335 num = RTE_MIN(VIRTIO_MBUF_BURST_SZ, nb_pkts);
1336 if (likely(num > DESC_PER_CACHELINE))
1337 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1339 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1340 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1343 hdr_size = hw->vtnet_hdr_size;
1345 for (i = 0; i < num; i++) {
1348 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1350 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1351 PMD_RX_LOG(ERR, "Packet drop");
1353 virtio_discard_rxbuf(vq, rxm);
1354 rxvq->stats.errors++;
1358 rxm->port = rxvq->port_id;
1359 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1363 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1364 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1366 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1367 RTE_PKTMBUF_HEADROOM - hdr_size);
1370 rte_vlan_strip(rxm);
1372 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1373 virtio_discard_rxbuf(vq, rxm);
1374 rxvq->stats.errors++;
1378 virtio_rx_stats_updated(rxvq, rxm);
1380 rx_pkts[nb_rx++] = rxm;
1383 rxvq->stats.packets += nb_rx;
1385 /* Allocate new mbuf for the used descriptor */
1386 if (likely(!virtqueue_full(vq))) {
1387 uint16_t free_cnt = vq->vq_free_cnt;
1388 struct rte_mbuf *new_pkts[free_cnt];
1390 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1392 error = virtqueue_enqueue_recv_refill_packed(vq,
1393 new_pkts, free_cnt);
1394 if (unlikely(error)) {
1395 for (i = 0; i < free_cnt; i++)
1396 rte_pktmbuf_free(new_pkts[i]);
1398 nb_enqueued += free_cnt;
1400 struct rte_eth_dev *dev =
1401 &rte_eth_devices[rxvq->port_id];
1402 dev->data->rx_mbuf_alloc_failed += free_cnt;
1406 if (likely(nb_enqueued)) {
1407 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1408 virtqueue_notify(vq);
1409 PMD_RX_LOG(DEBUG, "Notified");
1418 virtio_recv_pkts_inorder(void *rx_queue,
1419 struct rte_mbuf **rx_pkts,
1422 struct virtnet_rx *rxvq = rx_queue;
1423 struct virtqueue *vq = rxvq->vq;
1424 struct virtio_hw *hw = vq->hw;
1425 struct rte_mbuf *rxm;
1426 struct rte_mbuf *prev;
1427 uint16_t nb_used, num, nb_rx;
1428 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1429 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1431 uint32_t nb_enqueued;
1438 if (unlikely(hw->started == 0))
1441 nb_used = VIRTQUEUE_NUSED(vq);
1442 nb_used = RTE_MIN(nb_used, nb_pkts);
1443 nb_used = RTE_MIN(nb_used, VIRTIO_MBUF_BURST_SZ);
1445 virtio_rmb(hw->weak_barriers);
1447 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1452 hdr_size = hw->vtnet_hdr_size;
1454 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len, nb_used);
1456 for (i = 0; i < num; i++) {
1457 struct virtio_net_hdr_mrg_rxbuf *header;
1459 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1460 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1464 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1465 PMD_RX_LOG(ERR, "Packet drop");
1467 virtio_discard_rxbuf_inorder(vq, rxm);
1468 rxvq->stats.errors++;
1472 header = (struct virtio_net_hdr_mrg_rxbuf *)
1473 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1476 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1477 seg_num = header->num_buffers;
1484 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1485 rxm->nb_segs = seg_num;
1488 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1489 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1491 rxm->port = rxvq->port_id;
1493 rx_pkts[nb_rx] = rxm;
1496 if (vq->hw->has_rx_offload &&
1497 virtio_rx_offload(rxm, &header->hdr) < 0) {
1498 virtio_discard_rxbuf_inorder(vq, rxm);
1499 rxvq->stats.errors++;
1504 rte_vlan_strip(rx_pkts[nb_rx]);
1506 seg_res = seg_num - 1;
1508 /* Merge remaining segments */
1509 while (seg_res != 0 && i < (num - 1)) {
1513 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1514 rxm->pkt_len = (uint32_t)(len[i]);
1515 rxm->data_len = (uint16_t)(len[i]);
1517 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1518 rx_pkts[nb_rx]->data_len += (uint16_t)(len[i]);
1528 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1533 /* Last packet still need merge segments */
1534 while (seg_res != 0) {
1535 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1536 VIRTIO_MBUF_BURST_SZ);
1538 prev = rcv_pkts[nb_rx];
1539 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1540 virtio_rmb(hw->weak_barriers);
1541 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len,
1543 uint16_t extra_idx = 0;
1546 while (extra_idx < rcv_cnt) {
1547 rxm = rcv_pkts[extra_idx];
1549 RTE_PKTMBUF_HEADROOM - hdr_size;
1550 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1551 rxm->data_len = (uint16_t)(len[extra_idx]);
1554 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1555 rx_pkts[nb_rx]->data_len += len[extra_idx];
1561 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1566 "No enough segments for packet.");
1567 virtio_discard_rxbuf_inorder(vq, prev);
1568 rxvq->stats.errors++;
1573 rxvq->stats.packets += nb_rx;
1575 /* Allocate new mbuf for the used descriptor */
1577 if (likely(!virtqueue_full(vq))) {
1578 /* free_cnt may include mrg descs */
1579 uint16_t free_cnt = vq->vq_free_cnt;
1580 struct rte_mbuf *new_pkts[free_cnt];
1582 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1583 error = virtqueue_enqueue_refill_inorder(vq, new_pkts,
1585 if (unlikely(error)) {
1586 for (i = 0; i < free_cnt; i++)
1587 rte_pktmbuf_free(new_pkts[i]);
1589 nb_enqueued += free_cnt;
1591 struct rte_eth_dev *dev =
1592 &rte_eth_devices[rxvq->port_id];
1593 dev->data->rx_mbuf_alloc_failed += free_cnt;
1597 if (likely(nb_enqueued)) {
1598 vq_update_avail_idx(vq);
1600 if (unlikely(virtqueue_kick_prepare(vq))) {
1601 virtqueue_notify(vq);
1602 PMD_RX_LOG(DEBUG, "Notified");
1610 virtio_recv_mergeable_pkts(void *rx_queue,
1611 struct rte_mbuf **rx_pkts,
1614 struct virtnet_rx *rxvq = rx_queue;
1615 struct virtqueue *vq = rxvq->vq;
1616 struct virtio_hw *hw = vq->hw;
1617 struct rte_mbuf *rxm;
1618 struct rte_mbuf *prev;
1619 uint16_t nb_used, num, nb_rx = 0;
1620 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1621 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1623 uint32_t nb_enqueued = 0;
1624 uint32_t seg_num = 0;
1625 uint32_t seg_res = 0;
1626 uint32_t hdr_size = hw->vtnet_hdr_size;
1629 if (unlikely(hw->started == 0))
1632 nb_used = VIRTQUEUE_NUSED(vq);
1634 virtio_rmb(hw->weak_barriers);
1636 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1638 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1639 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1640 num = VIRTIO_MBUF_BURST_SZ;
1641 if (likely(num > DESC_PER_CACHELINE))
1642 num = num - ((vq->vq_used_cons_idx + num) %
1643 DESC_PER_CACHELINE);
1646 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1648 for (i = 0; i < num; i++) {
1649 struct virtio_net_hdr_mrg_rxbuf *header;
1651 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1652 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1656 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1657 PMD_RX_LOG(ERR, "Packet drop");
1659 virtio_discard_rxbuf(vq, rxm);
1660 rxvq->stats.errors++;
1664 header = (struct virtio_net_hdr_mrg_rxbuf *)
1665 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1667 seg_num = header->num_buffers;
1671 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1672 rxm->nb_segs = seg_num;
1675 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1676 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1678 rxm->port = rxvq->port_id;
1680 rx_pkts[nb_rx] = rxm;
1683 if (hw->has_rx_offload &&
1684 virtio_rx_offload(rxm, &header->hdr) < 0) {
1685 virtio_discard_rxbuf(vq, rxm);
1686 rxvq->stats.errors++;
1691 rte_vlan_strip(rx_pkts[nb_rx]);
1693 seg_res = seg_num - 1;
1695 /* Merge remaining segments */
1696 while (seg_res != 0 && i < (num - 1)) {
1700 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1701 rxm->pkt_len = (uint32_t)(len[i]);
1702 rxm->data_len = (uint16_t)(len[i]);
1704 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1705 rx_pkts[nb_rx]->data_len += (uint16_t)(len[i]);
1715 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1720 /* Last packet still need merge segments */
1721 while (seg_res != 0) {
1722 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1723 VIRTIO_MBUF_BURST_SZ);
1725 prev = rcv_pkts[nb_rx];
1726 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1727 virtio_rmb(hw->weak_barriers);
1728 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len,
1730 uint16_t extra_idx = 0;
1733 while (extra_idx < rcv_cnt) {
1734 rxm = rcv_pkts[extra_idx];
1736 RTE_PKTMBUF_HEADROOM - hdr_size;
1737 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1738 rxm->data_len = (uint16_t)(len[extra_idx]);
1741 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1742 rx_pkts[nb_rx]->data_len += len[extra_idx];
1748 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1753 "No enough segments for packet.");
1754 virtio_discard_rxbuf(vq, prev);
1755 rxvq->stats.errors++;
1760 rxvq->stats.packets += nb_rx;
1762 /* Allocate new mbuf for the used descriptor */
1763 if (likely(!virtqueue_full(vq))) {
1764 /* free_cnt may include mrg descs */
1765 uint16_t free_cnt = vq->vq_free_cnt;
1766 struct rte_mbuf *new_pkts[free_cnt];
1768 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1769 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1771 if (unlikely(error)) {
1772 for (i = 0; i < free_cnt; i++)
1773 rte_pktmbuf_free(new_pkts[i]);
1775 nb_enqueued += free_cnt;
1777 struct rte_eth_dev *dev =
1778 &rte_eth_devices[rxvq->port_id];
1779 dev->data->rx_mbuf_alloc_failed += free_cnt;
1783 if (likely(nb_enqueued)) {
1784 vq_update_avail_idx(vq);
1786 if (unlikely(virtqueue_kick_prepare(vq))) {
1787 virtqueue_notify(vq);
1788 PMD_RX_LOG(DEBUG, "Notified");
1796 virtio_recv_mergeable_pkts_packed(void *rx_queue,
1797 struct rte_mbuf **rx_pkts,
1800 struct virtnet_rx *rxvq = rx_queue;
1801 struct virtqueue *vq = rxvq->vq;
1802 struct virtio_hw *hw = vq->hw;
1803 struct rte_mbuf *rxm;
1804 struct rte_mbuf *prev = NULL;
1805 uint16_t num, nb_rx = 0;
1806 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1807 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1808 uint32_t nb_enqueued = 0;
1809 uint32_t seg_num = 0;
1810 uint32_t seg_res = 0;
1811 uint32_t hdr_size = hw->vtnet_hdr_size;
1815 if (unlikely(hw->started == 0))
1820 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1821 num = VIRTIO_MBUF_BURST_SZ;
1822 if (likely(num > DESC_PER_CACHELINE))
1823 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1825 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1827 for (i = 0; i < num; i++) {
1828 struct virtio_net_hdr_mrg_rxbuf *header;
1830 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1831 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1835 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1836 PMD_RX_LOG(ERR, "Packet drop");
1838 virtio_discard_rxbuf(vq, rxm);
1839 rxvq->stats.errors++;
1843 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)
1844 rxm->buf_addr + RTE_PKTMBUF_HEADROOM - hdr_size);
1845 seg_num = header->num_buffers;
1850 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1851 rxm->nb_segs = seg_num;
1854 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1855 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1857 rxm->port = rxvq->port_id;
1858 rx_pkts[nb_rx] = rxm;
1861 if (hw->has_rx_offload &&
1862 virtio_rx_offload(rxm, &header->hdr) < 0) {
1863 virtio_discard_rxbuf(vq, rxm);
1864 rxvq->stats.errors++;
1869 rte_vlan_strip(rx_pkts[nb_rx]);
1871 seg_res = seg_num - 1;
1873 /* Merge remaining segments */
1874 while (seg_res != 0 && i < (num - 1)) {
1878 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1879 rxm->pkt_len = (uint32_t)(len[i]);
1880 rxm->data_len = (uint16_t)(len[i]);
1882 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1883 rx_pkts[nb_rx]->data_len += (uint16_t)(len[i]);
1893 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1898 /* Last packet still need merge segments */
1899 while (seg_res != 0) {
1900 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1901 VIRTIO_MBUF_BURST_SZ);
1902 if (likely(vq->vq_free_cnt >= rcv_cnt)) {
1903 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts,
1905 uint16_t extra_idx = 0;
1909 while (extra_idx < rcv_cnt) {
1910 rxm = rcv_pkts[extra_idx];
1913 RTE_PKTMBUF_HEADROOM - hdr_size;
1914 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1915 rxm->data_len = (uint16_t)(len[extra_idx]);
1919 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1920 rx_pkts[nb_rx]->data_len += len[extra_idx];
1925 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1930 "No enough segments for packet.");
1932 virtio_discard_rxbuf(vq, prev);
1933 rxvq->stats.errors++;
1938 rxvq->stats.packets += nb_rx;
1940 /* Allocate new mbuf for the used descriptor */
1941 if (likely(!virtqueue_full(vq))) {
1942 /* free_cnt may include mrg descs */
1943 uint16_t free_cnt = vq->vq_free_cnt;
1944 struct rte_mbuf *new_pkts[free_cnt];
1946 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1947 error = virtqueue_enqueue_recv_refill_packed(vq,
1948 new_pkts, free_cnt);
1949 if (unlikely(error)) {
1950 for (i = 0; i < free_cnt; i++)
1951 rte_pktmbuf_free(new_pkts[i]);
1953 nb_enqueued += free_cnt;
1955 struct rte_eth_dev *dev =
1956 &rte_eth_devices[rxvq->port_id];
1957 dev->data->rx_mbuf_alloc_failed += free_cnt;
1961 if (likely(nb_enqueued)) {
1962 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1963 virtqueue_notify(vq);
1964 PMD_RX_LOG(DEBUG, "Notified");
1972 virtio_xmit_pkts_packed(void *tx_queue, struct rte_mbuf **tx_pkts,
1975 struct virtnet_tx *txvq = tx_queue;
1976 struct virtqueue *vq = txvq->vq;
1977 struct virtio_hw *hw = vq->hw;
1978 uint16_t hdr_size = hw->vtnet_hdr_size;
1980 bool in_order = hw->use_inorder_tx;
1983 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
1986 if (unlikely(nb_pkts < 1))
1989 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
1991 if (nb_pkts > vq->vq_free_cnt)
1992 virtio_xmit_cleanup_packed(vq, nb_pkts - vq->vq_free_cnt,
1995 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1996 struct rte_mbuf *txm = tx_pkts[nb_tx];
1997 int can_push = 0, slots, need;
1999 /* Do VLAN tag insertion */
2000 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
2001 error = rte_vlan_insert(&txm);
2002 if (unlikely(error)) {
2003 rte_pktmbuf_free(txm);
2006 /* vlan_insert may add a header mbuf */
2007 tx_pkts[nb_tx] = txm;
2010 /* optimize ring usage */
2011 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2012 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2013 rte_mbuf_refcnt_read(txm) == 1 &&
2014 RTE_MBUF_DIRECT(txm) &&
2015 txm->nb_segs == 1 &&
2016 rte_pktmbuf_headroom(txm) >= hdr_size &&
2017 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2018 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2021 /* How many main ring entries are needed to this Tx?
2022 * any_layout => number of segments
2023 * default => number of segments + 1
2025 slots = txm->nb_segs + !can_push;
2026 need = slots - vq->vq_free_cnt;
2028 /* Positive value indicates it need free vring descriptors */
2029 if (unlikely(need > 0)) {
2030 virtio_xmit_cleanup_packed(vq, need, in_order);
2031 need = slots - vq->vq_free_cnt;
2032 if (unlikely(need > 0)) {
2034 "No free tx descriptors to transmit");
2039 /* Enqueue Packet buffers */
2041 virtqueue_enqueue_xmit_packed_fast(txvq, txm, in_order);
2043 virtqueue_enqueue_xmit_packed(txvq, txm, slots, 0,
2046 virtio_update_packet_stats(&txvq->stats, txm);
2049 txvq->stats.packets += nb_tx;
2051 if (likely(nb_tx)) {
2052 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
2053 virtqueue_notify(vq);
2054 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2062 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
2064 struct virtnet_tx *txvq = tx_queue;
2065 struct virtqueue *vq = txvq->vq;
2066 struct virtio_hw *hw = vq->hw;
2067 uint16_t hdr_size = hw->vtnet_hdr_size;
2068 uint16_t nb_used, nb_tx = 0;
2071 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2074 if (unlikely(nb_pkts < 1))
2077 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2078 nb_used = VIRTQUEUE_NUSED(vq);
2080 virtio_rmb(hw->weak_barriers);
2081 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2082 virtio_xmit_cleanup(vq, nb_used);
2084 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2085 struct rte_mbuf *txm = tx_pkts[nb_tx];
2086 int can_push = 0, use_indirect = 0, slots, need;
2088 /* Do VLAN tag insertion */
2089 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
2090 error = rte_vlan_insert(&txm);
2091 if (unlikely(error)) {
2092 rte_pktmbuf_free(txm);
2095 /* vlan_insert may add a header mbuf */
2096 tx_pkts[nb_tx] = txm;
2099 /* optimize ring usage */
2100 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2101 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2102 rte_mbuf_refcnt_read(txm) == 1 &&
2103 RTE_MBUF_DIRECT(txm) &&
2104 txm->nb_segs == 1 &&
2105 rte_pktmbuf_headroom(txm) >= hdr_size &&
2106 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2107 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2109 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
2110 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
2113 /* How many main ring entries are needed to this Tx?
2114 * any_layout => number of segments
2116 * default => number of segments + 1
2118 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
2119 need = slots - vq->vq_free_cnt;
2121 /* Positive value indicates it need free vring descriptors */
2122 if (unlikely(need > 0)) {
2123 nb_used = VIRTQUEUE_NUSED(vq);
2124 virtio_rmb(hw->weak_barriers);
2125 need = RTE_MIN(need, (int)nb_used);
2127 virtio_xmit_cleanup(vq, need);
2128 need = slots - vq->vq_free_cnt;
2129 if (unlikely(need > 0)) {
2131 "No free tx descriptors to transmit");
2136 /* Enqueue Packet buffers */
2137 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect,
2140 virtio_update_packet_stats(&txvq->stats, txm);
2143 txvq->stats.packets += nb_tx;
2145 if (likely(nb_tx)) {
2146 vq_update_avail_idx(vq);
2148 if (unlikely(virtqueue_kick_prepare(vq))) {
2149 virtqueue_notify(vq);
2150 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2158 virtio_xmit_pkts_inorder(void *tx_queue,
2159 struct rte_mbuf **tx_pkts,
2162 struct virtnet_tx *txvq = tx_queue;
2163 struct virtqueue *vq = txvq->vq;
2164 struct virtio_hw *hw = vq->hw;
2165 uint16_t hdr_size = hw->vtnet_hdr_size;
2166 uint16_t nb_used, nb_avail, nb_tx = 0, nb_inorder_pkts = 0;
2167 struct rte_mbuf *inorder_pkts[nb_pkts];
2170 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2173 if (unlikely(nb_pkts < 1))
2177 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2178 nb_used = VIRTQUEUE_NUSED(vq);
2180 virtio_rmb(hw->weak_barriers);
2181 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2182 virtio_xmit_cleanup_inorder(vq, nb_used);
2184 if (unlikely(!vq->vq_free_cnt))
2185 virtio_xmit_cleanup_inorder(vq, nb_used);
2187 nb_avail = RTE_MIN(vq->vq_free_cnt, nb_pkts);
2189 for (nb_tx = 0; nb_tx < nb_avail; nb_tx++) {
2190 struct rte_mbuf *txm = tx_pkts[nb_tx];
2193 /* Do VLAN tag insertion */
2194 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
2195 error = rte_vlan_insert(&txm);
2196 if (unlikely(error)) {
2197 rte_pktmbuf_free(txm);
2200 /* vlan_insert may add a header mbuf */
2201 tx_pkts[nb_tx] = txm;
2204 /* optimize ring usage */
2205 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2206 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2207 rte_mbuf_refcnt_read(txm) == 1 &&
2208 RTE_MBUF_DIRECT(txm) &&
2209 txm->nb_segs == 1 &&
2210 rte_pktmbuf_headroom(txm) >= hdr_size &&
2211 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2212 __alignof__(struct virtio_net_hdr_mrg_rxbuf))) {
2213 inorder_pkts[nb_inorder_pkts] = txm;
2216 virtio_update_packet_stats(&txvq->stats, txm);
2220 if (nb_inorder_pkts) {
2221 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2223 nb_inorder_pkts = 0;
2226 slots = txm->nb_segs + 1;
2227 need = slots - vq->vq_free_cnt;
2228 if (unlikely(need > 0)) {
2229 nb_used = VIRTQUEUE_NUSED(vq);
2230 virtio_rmb(hw->weak_barriers);
2231 need = RTE_MIN(need, (int)nb_used);
2233 virtio_xmit_cleanup_inorder(vq, need);
2235 need = slots - vq->vq_free_cnt;
2237 if (unlikely(need > 0)) {
2239 "No free tx descriptors to transmit");
2243 /* Enqueue Packet buffers */
2244 virtqueue_enqueue_xmit(txvq, txm, slots, 0, 0, 1);
2246 virtio_update_packet_stats(&txvq->stats, txm);
2249 /* Transmit all inorder packets */
2250 if (nb_inorder_pkts)
2251 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2254 txvq->stats.packets += nb_tx;
2256 if (likely(nb_tx)) {
2257 vq_update_avail_idx(vq);
2259 if (unlikely(virtqueue_kick_prepare(vq))) {
2260 virtqueue_notify(vq);
2261 PMD_TX_LOG(DEBUG, "Notified backend after xmit");