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 rte_ipv4_hdr *iph;
483 struct rte_ipv6_hdr *ip6h;
484 struct rte_tcp_hdr *th;
485 uint16_t prev_cksum, new_cksum, ip_len, ip_paylen;
488 iph = rte_pktmbuf_mtod_offset(m,
489 struct rte_ipv4_hdr *, m->l2_len);
490 th = RTE_PTR_ADD(iph, m->l3_len);
491 if ((iph->version_ihl >> 4) == 4) {
492 iph->hdr_checksum = 0;
493 iph->hdr_checksum = rte_ipv4_cksum(iph);
494 ip_len = iph->total_length;
495 ip_paylen = rte_cpu_to_be_16(rte_be_to_cpu_16(ip_len) -
498 ip6h = (struct rte_ipv6_hdr *)iph;
499 ip_paylen = ip6h->payload_len;
502 /* calculate the new phdr checksum not including ip_paylen */
503 prev_cksum = th->cksum;
506 tmp = (tmp & 0xffff) + (tmp >> 16);
509 /* replace it in the packet */
510 th->cksum = new_cksum;
515 /* avoid write operation when necessary, to lessen cache issues */
516 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
517 if ((var) != (val)) \
521 #define virtqueue_clear_net_hdr(_hdr) do { \
522 ASSIGN_UNLESS_EQUAL((_hdr)->csum_start, 0); \
523 ASSIGN_UNLESS_EQUAL((_hdr)->csum_offset, 0); \
524 ASSIGN_UNLESS_EQUAL((_hdr)->flags, 0); \
525 ASSIGN_UNLESS_EQUAL((_hdr)->gso_type, 0); \
526 ASSIGN_UNLESS_EQUAL((_hdr)->gso_size, 0); \
527 ASSIGN_UNLESS_EQUAL((_hdr)->hdr_len, 0); \
531 virtqueue_xmit_offload(struct virtio_net_hdr *hdr,
532 struct rte_mbuf *cookie,
536 if (cookie->ol_flags & PKT_TX_TCP_SEG)
537 cookie->ol_flags |= PKT_TX_TCP_CKSUM;
539 switch (cookie->ol_flags & PKT_TX_L4_MASK) {
540 case PKT_TX_UDP_CKSUM:
541 hdr->csum_start = cookie->l2_len + cookie->l3_len;
542 hdr->csum_offset = offsetof(struct rte_udp_hdr,
544 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
547 case PKT_TX_TCP_CKSUM:
548 hdr->csum_start = cookie->l2_len + cookie->l3_len;
549 hdr->csum_offset = offsetof(struct rte_tcp_hdr, cksum);
550 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
554 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
555 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
556 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
560 /* TCP Segmentation Offload */
561 if (cookie->ol_flags & PKT_TX_TCP_SEG) {
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 dev->data->rx_queues[queue_idx] = rxvq;
898 virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
900 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
901 struct virtio_hw *hw = dev->data->dev_private;
902 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
903 struct virtnet_rx *rxvq = &vq->rxq;
908 PMD_INIT_FUNC_TRACE();
910 /* Allocate blank mbufs for the each rx descriptor */
913 if (hw->use_simple_rx) {
914 for (desc_idx = 0; desc_idx < vq->vq_nentries;
916 vq->vq_split.ring.avail->ring[desc_idx] = desc_idx;
917 vq->vq_split.ring.desc[desc_idx].flags =
921 virtio_rxq_vec_setup(rxvq);
924 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
925 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
927 vq->sw_ring[vq->vq_nentries + desc_idx] =
931 if (hw->use_simple_rx) {
932 while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
933 virtio_rxq_rearm_vec(rxvq);
934 nbufs += RTE_VIRTIO_VPMD_RX_REARM_THRESH;
936 } else if (hw->use_inorder_rx) {
937 if ((!virtqueue_full(vq))) {
938 uint16_t free_cnt = vq->vq_free_cnt;
939 struct rte_mbuf *pkts[free_cnt];
941 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, pkts,
943 error = virtqueue_enqueue_refill_inorder(vq,
946 if (unlikely(error)) {
947 for (i = 0; i < free_cnt; i++)
948 rte_pktmbuf_free(pkts[i]);
953 vq_update_avail_idx(vq);
956 while (!virtqueue_full(vq)) {
957 m = rte_mbuf_raw_alloc(rxvq->mpool);
961 /* Enqueue allocated buffers */
962 if (vtpci_packed_queue(vq->hw))
963 error = virtqueue_enqueue_recv_refill_packed(vq,
966 error = virtqueue_enqueue_recv_refill(vq,
975 if (!vtpci_packed_queue(vq->hw))
976 vq_update_avail_idx(vq);
979 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
987 * struct rte_eth_dev *dev: Used to update dev
988 * uint16_t nb_desc: Defaults to values read from config space
989 * unsigned int socket_id: Used to allocate memzone
990 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
991 * uint16_t queue_idx: Just used as an index in dev txq list
994 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
997 unsigned int socket_id __rte_unused,
998 const struct rte_eth_txconf *tx_conf)
1000 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1001 struct virtio_hw *hw = dev->data->dev_private;
1002 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1003 struct virtnet_tx *txvq;
1004 uint16_t tx_free_thresh;
1006 PMD_INIT_FUNC_TRACE();
1008 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
1009 nb_desc = vq->vq_nentries;
1010 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
1013 txvq->queue_id = queue_idx;
1015 tx_free_thresh = tx_conf->tx_free_thresh;
1016 if (tx_free_thresh == 0)
1018 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
1020 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
1021 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
1022 "number of TX entries minus 3 (%u)."
1023 " (tx_free_thresh=%u port=%u queue=%u)\n",
1024 vq->vq_nentries - 3,
1025 tx_free_thresh, dev->data->port_id, queue_idx);
1029 vq->vq_free_thresh = tx_free_thresh;
1031 dev->data->tx_queues[queue_idx] = txvq;
1036 virtio_dev_tx_queue_setup_finish(struct rte_eth_dev *dev,
1039 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1040 struct virtio_hw *hw = dev->data->dev_private;
1041 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1043 PMD_INIT_FUNC_TRACE();
1045 if (!vtpci_packed_queue(hw)) {
1046 if (hw->use_inorder_tx)
1047 vq->vq_split.ring.desc[vq->vq_nentries - 1].next = 0;
1056 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
1060 * Requeue the discarded mbuf. This should always be
1061 * successful since it was just dequeued.
1063 if (vtpci_packed_queue(vq->hw))
1064 error = virtqueue_enqueue_recv_refill_packed(vq, &m, 1);
1066 error = virtqueue_enqueue_recv_refill(vq, &m, 1);
1068 if (unlikely(error)) {
1069 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
1070 rte_pktmbuf_free(m);
1075 virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)
1079 error = virtqueue_enqueue_refill_inorder(vq, &m, 1);
1080 if (unlikely(error)) {
1081 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
1082 rte_pktmbuf_free(m);
1087 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
1089 uint32_t s = mbuf->pkt_len;
1090 struct rte_ether_addr *ea;
1095 stats->size_bins[1]++;
1096 } else if (s > 64 && s < 1024) {
1099 /* count zeros, and offset into correct bin */
1100 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
1101 stats->size_bins[bin]++;
1104 stats->size_bins[0]++;
1106 stats->size_bins[6]++;
1108 stats->size_bins[7]++;
1111 ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
1112 if (rte_is_multicast_ether_addr(ea)) {
1113 if (rte_is_broadcast_ether_addr(ea))
1121 virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
1123 VIRTIO_DUMP_PACKET(m, m->data_len);
1125 virtio_update_packet_stats(&rxvq->stats, m);
1128 /* Optionally fill offload information in structure */
1130 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
1132 struct rte_net_hdr_lens hdr_lens;
1133 uint32_t hdrlen, ptype;
1134 int l4_supported = 0;
1137 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1140 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
1142 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
1143 m->packet_type = ptype;
1144 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
1145 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
1146 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
1149 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1150 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
1151 if (hdr->csum_start <= hdrlen && l4_supported) {
1152 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
1154 /* Unknown proto or tunnel, do sw cksum. We can assume
1155 * the cksum field is in the first segment since the
1156 * buffers we provided to the host are large enough.
1157 * In case of SCTP, this will be wrong since it's a CRC
1158 * but there's nothing we can do.
1160 uint16_t csum = 0, off;
1162 rte_raw_cksum_mbuf(m, hdr->csum_start,
1163 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
1165 if (likely(csum != 0xffff))
1167 off = hdr->csum_offset + hdr->csum_start;
1168 if (rte_pktmbuf_data_len(m) >= off + 1)
1169 *rte_pktmbuf_mtod_offset(m, uint16_t *,
1172 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
1173 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
1176 /* GSO request, save required information in mbuf */
1177 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1178 /* Check unsupported modes */
1179 if ((hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) ||
1180 (hdr->gso_size == 0)) {
1184 /* Update mss lengthes in mbuf */
1185 m->tso_segsz = hdr->gso_size;
1186 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1187 case VIRTIO_NET_HDR_GSO_TCPV4:
1188 case VIRTIO_NET_HDR_GSO_TCPV6:
1189 m->ol_flags |= PKT_RX_LRO | \
1190 PKT_RX_L4_CKSUM_NONE;
1200 #define VIRTIO_MBUF_BURST_SZ 64
1201 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
1203 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1205 struct virtnet_rx *rxvq = rx_queue;
1206 struct virtqueue *vq = rxvq->vq;
1207 struct virtio_hw *hw = vq->hw;
1208 struct rte_mbuf *rxm;
1209 uint16_t nb_used, num, nb_rx;
1210 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1211 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1213 uint32_t i, nb_enqueued;
1215 struct virtio_net_hdr *hdr;
1218 if (unlikely(hw->started == 0))
1221 nb_used = VIRTQUEUE_NUSED(vq);
1223 virtio_rmb(hw->weak_barriers);
1225 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1226 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1227 num = VIRTIO_MBUF_BURST_SZ;
1228 if (likely(num > DESC_PER_CACHELINE))
1229 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1231 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1232 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
1235 hdr_size = hw->vtnet_hdr_size;
1237 for (i = 0; i < num ; i++) {
1240 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1242 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1243 PMD_RX_LOG(ERR, "Packet drop");
1245 virtio_discard_rxbuf(vq, rxm);
1246 rxvq->stats.errors++;
1250 rxm->port = rxvq->port_id;
1251 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1255 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1256 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1258 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1259 RTE_PKTMBUF_HEADROOM - hdr_size);
1262 rte_vlan_strip(rxm);
1264 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1265 virtio_discard_rxbuf(vq, rxm);
1266 rxvq->stats.errors++;
1270 virtio_rx_stats_updated(rxvq, rxm);
1272 rx_pkts[nb_rx++] = rxm;
1275 rxvq->stats.packets += nb_rx;
1277 /* Allocate new mbuf for the used descriptor */
1278 if (likely(!virtqueue_full(vq))) {
1279 uint16_t free_cnt = vq->vq_free_cnt;
1280 struct rte_mbuf *new_pkts[free_cnt];
1282 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1284 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1286 if (unlikely(error)) {
1287 for (i = 0; i < free_cnt; i++)
1288 rte_pktmbuf_free(new_pkts[i]);
1290 nb_enqueued += free_cnt;
1292 struct rte_eth_dev *dev =
1293 &rte_eth_devices[rxvq->port_id];
1294 dev->data->rx_mbuf_alloc_failed += free_cnt;
1298 if (likely(nb_enqueued)) {
1299 vq_update_avail_idx(vq);
1301 if (unlikely(virtqueue_kick_prepare(vq))) {
1302 virtqueue_notify(vq);
1303 PMD_RX_LOG(DEBUG, "Notified");
1311 virtio_recv_pkts_packed(void *rx_queue, struct rte_mbuf **rx_pkts,
1314 struct virtnet_rx *rxvq = rx_queue;
1315 struct virtqueue *vq = rxvq->vq;
1316 struct virtio_hw *hw = vq->hw;
1317 struct rte_mbuf *rxm;
1318 uint16_t num, nb_rx;
1319 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1320 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1322 uint32_t i, nb_enqueued;
1324 struct virtio_net_hdr *hdr;
1327 if (unlikely(hw->started == 0))
1330 num = RTE_MIN(VIRTIO_MBUF_BURST_SZ, nb_pkts);
1331 if (likely(num > DESC_PER_CACHELINE))
1332 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1334 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1335 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1338 hdr_size = hw->vtnet_hdr_size;
1340 for (i = 0; i < num; i++) {
1343 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1345 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1346 PMD_RX_LOG(ERR, "Packet drop");
1348 virtio_discard_rxbuf(vq, rxm);
1349 rxvq->stats.errors++;
1353 rxm->port = rxvq->port_id;
1354 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1358 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1359 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1361 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1362 RTE_PKTMBUF_HEADROOM - hdr_size);
1365 rte_vlan_strip(rxm);
1367 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1368 virtio_discard_rxbuf(vq, rxm);
1369 rxvq->stats.errors++;
1373 virtio_rx_stats_updated(rxvq, rxm);
1375 rx_pkts[nb_rx++] = rxm;
1378 rxvq->stats.packets += nb_rx;
1380 /* Allocate new mbuf for the used descriptor */
1381 if (likely(!virtqueue_full(vq))) {
1382 uint16_t free_cnt = vq->vq_free_cnt;
1383 struct rte_mbuf *new_pkts[free_cnt];
1385 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1387 error = virtqueue_enqueue_recv_refill_packed(vq,
1388 new_pkts, free_cnt);
1389 if (unlikely(error)) {
1390 for (i = 0; i < free_cnt; i++)
1391 rte_pktmbuf_free(new_pkts[i]);
1393 nb_enqueued += free_cnt;
1395 struct rte_eth_dev *dev =
1396 &rte_eth_devices[rxvq->port_id];
1397 dev->data->rx_mbuf_alloc_failed += free_cnt;
1401 if (likely(nb_enqueued)) {
1402 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1403 virtqueue_notify(vq);
1404 PMD_RX_LOG(DEBUG, "Notified");
1413 virtio_recv_pkts_inorder(void *rx_queue,
1414 struct rte_mbuf **rx_pkts,
1417 struct virtnet_rx *rxvq = rx_queue;
1418 struct virtqueue *vq = rxvq->vq;
1419 struct virtio_hw *hw = vq->hw;
1420 struct rte_mbuf *rxm;
1421 struct rte_mbuf *prev = NULL;
1422 uint16_t nb_used, num, nb_rx;
1423 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1424 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1426 uint32_t nb_enqueued;
1433 if (unlikely(hw->started == 0))
1436 nb_used = VIRTQUEUE_NUSED(vq);
1437 nb_used = RTE_MIN(nb_used, nb_pkts);
1438 nb_used = RTE_MIN(nb_used, VIRTIO_MBUF_BURST_SZ);
1440 virtio_rmb(hw->weak_barriers);
1442 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1447 hdr_size = hw->vtnet_hdr_size;
1449 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len, nb_used);
1451 for (i = 0; i < num; i++) {
1452 struct virtio_net_hdr_mrg_rxbuf *header;
1454 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1455 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1459 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1460 PMD_RX_LOG(ERR, "Packet drop");
1462 virtio_discard_rxbuf_inorder(vq, rxm);
1463 rxvq->stats.errors++;
1467 header = (struct virtio_net_hdr_mrg_rxbuf *)
1468 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1471 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1472 seg_num = header->num_buffers;
1479 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1480 rxm->nb_segs = seg_num;
1483 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1484 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1486 rxm->port = rxvq->port_id;
1488 rx_pkts[nb_rx] = rxm;
1491 if (vq->hw->has_rx_offload &&
1492 virtio_rx_offload(rxm, &header->hdr) < 0) {
1493 virtio_discard_rxbuf_inorder(vq, rxm);
1494 rxvq->stats.errors++;
1499 rte_vlan_strip(rx_pkts[nb_rx]);
1501 seg_res = seg_num - 1;
1503 /* Merge remaining segments */
1504 while (seg_res != 0 && i < (num - 1)) {
1508 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1509 rxm->pkt_len = (uint32_t)(len[i]);
1510 rxm->data_len = (uint16_t)(len[i]);
1512 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1520 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1525 /* Last packet still need merge segments */
1526 while (seg_res != 0) {
1527 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1528 VIRTIO_MBUF_BURST_SZ);
1530 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1531 virtio_rmb(hw->weak_barriers);
1532 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len,
1534 uint16_t extra_idx = 0;
1537 while (extra_idx < rcv_cnt) {
1538 rxm = rcv_pkts[extra_idx];
1540 RTE_PKTMBUF_HEADROOM - hdr_size;
1541 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1542 rxm->data_len = (uint16_t)(len[extra_idx]);
1545 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1551 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1556 "No enough segments for packet.");
1557 rte_pktmbuf_free(rx_pkts[nb_rx]);
1558 rxvq->stats.errors++;
1563 rxvq->stats.packets += nb_rx;
1565 /* Allocate new mbuf for the used descriptor */
1567 if (likely(!virtqueue_full(vq))) {
1568 /* free_cnt may include mrg descs */
1569 uint16_t free_cnt = vq->vq_free_cnt;
1570 struct rte_mbuf *new_pkts[free_cnt];
1572 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1573 error = virtqueue_enqueue_refill_inorder(vq, new_pkts,
1575 if (unlikely(error)) {
1576 for (i = 0; i < free_cnt; i++)
1577 rte_pktmbuf_free(new_pkts[i]);
1579 nb_enqueued += free_cnt;
1581 struct rte_eth_dev *dev =
1582 &rte_eth_devices[rxvq->port_id];
1583 dev->data->rx_mbuf_alloc_failed += free_cnt;
1587 if (likely(nb_enqueued)) {
1588 vq_update_avail_idx(vq);
1590 if (unlikely(virtqueue_kick_prepare(vq))) {
1591 virtqueue_notify(vq);
1592 PMD_RX_LOG(DEBUG, "Notified");
1600 virtio_recv_mergeable_pkts(void *rx_queue,
1601 struct rte_mbuf **rx_pkts,
1604 struct virtnet_rx *rxvq = rx_queue;
1605 struct virtqueue *vq = rxvq->vq;
1606 struct virtio_hw *hw = vq->hw;
1607 struct rte_mbuf *rxm;
1608 struct rte_mbuf *prev = NULL;
1609 uint16_t nb_used, num, nb_rx = 0;
1610 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1611 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1613 uint32_t nb_enqueued = 0;
1614 uint32_t seg_num = 0;
1615 uint32_t seg_res = 0;
1616 uint32_t hdr_size = hw->vtnet_hdr_size;
1619 if (unlikely(hw->started == 0))
1622 nb_used = VIRTQUEUE_NUSED(vq);
1624 virtio_rmb(hw->weak_barriers);
1626 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1628 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1629 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1630 num = VIRTIO_MBUF_BURST_SZ;
1631 if (likely(num > DESC_PER_CACHELINE))
1632 num = num - ((vq->vq_used_cons_idx + num) %
1633 DESC_PER_CACHELINE);
1636 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1638 for (i = 0; i < num; i++) {
1639 struct virtio_net_hdr_mrg_rxbuf *header;
1641 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1642 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1646 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1647 PMD_RX_LOG(ERR, "Packet drop");
1649 virtio_discard_rxbuf(vq, rxm);
1650 rxvq->stats.errors++;
1654 header = (struct virtio_net_hdr_mrg_rxbuf *)
1655 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1657 seg_num = header->num_buffers;
1661 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1662 rxm->nb_segs = seg_num;
1665 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1666 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1668 rxm->port = rxvq->port_id;
1670 rx_pkts[nb_rx] = rxm;
1673 if (hw->has_rx_offload &&
1674 virtio_rx_offload(rxm, &header->hdr) < 0) {
1675 virtio_discard_rxbuf(vq, rxm);
1676 rxvq->stats.errors++;
1681 rte_vlan_strip(rx_pkts[nb_rx]);
1683 seg_res = seg_num - 1;
1685 /* Merge remaining segments */
1686 while (seg_res != 0 && i < (num - 1)) {
1690 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1691 rxm->pkt_len = (uint32_t)(len[i]);
1692 rxm->data_len = (uint16_t)(len[i]);
1694 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1702 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1707 /* Last packet still need merge segments */
1708 while (seg_res != 0) {
1709 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1710 VIRTIO_MBUF_BURST_SZ);
1712 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1713 virtio_rmb(hw->weak_barriers);
1714 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len,
1716 uint16_t extra_idx = 0;
1719 while (extra_idx < rcv_cnt) {
1720 rxm = rcv_pkts[extra_idx];
1722 RTE_PKTMBUF_HEADROOM - hdr_size;
1723 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1724 rxm->data_len = (uint16_t)(len[extra_idx]);
1727 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1733 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1738 "No enough segments for packet.");
1739 rte_pktmbuf_free(rx_pkts[nb_rx]);
1740 rxvq->stats.errors++;
1745 rxvq->stats.packets += nb_rx;
1747 /* Allocate new mbuf for the used descriptor */
1748 if (likely(!virtqueue_full(vq))) {
1749 /* free_cnt may include mrg descs */
1750 uint16_t free_cnt = vq->vq_free_cnt;
1751 struct rte_mbuf *new_pkts[free_cnt];
1753 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1754 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1756 if (unlikely(error)) {
1757 for (i = 0; i < free_cnt; i++)
1758 rte_pktmbuf_free(new_pkts[i]);
1760 nb_enqueued += free_cnt;
1762 struct rte_eth_dev *dev =
1763 &rte_eth_devices[rxvq->port_id];
1764 dev->data->rx_mbuf_alloc_failed += free_cnt;
1768 if (likely(nb_enqueued)) {
1769 vq_update_avail_idx(vq);
1771 if (unlikely(virtqueue_kick_prepare(vq))) {
1772 virtqueue_notify(vq);
1773 PMD_RX_LOG(DEBUG, "Notified");
1781 virtio_recv_mergeable_pkts_packed(void *rx_queue,
1782 struct rte_mbuf **rx_pkts,
1785 struct virtnet_rx *rxvq = rx_queue;
1786 struct virtqueue *vq = rxvq->vq;
1787 struct virtio_hw *hw = vq->hw;
1788 struct rte_mbuf *rxm;
1789 struct rte_mbuf *prev = NULL;
1790 uint16_t num, nb_rx = 0;
1791 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1792 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1793 uint32_t nb_enqueued = 0;
1794 uint32_t seg_num = 0;
1795 uint32_t seg_res = 0;
1796 uint32_t hdr_size = hw->vtnet_hdr_size;
1800 if (unlikely(hw->started == 0))
1805 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1806 num = VIRTIO_MBUF_BURST_SZ;
1807 if (likely(num > DESC_PER_CACHELINE))
1808 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1810 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1812 for (i = 0; i < num; i++) {
1813 struct virtio_net_hdr_mrg_rxbuf *header;
1815 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1816 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1820 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1821 PMD_RX_LOG(ERR, "Packet drop");
1823 virtio_discard_rxbuf(vq, rxm);
1824 rxvq->stats.errors++;
1828 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)
1829 rxm->buf_addr + RTE_PKTMBUF_HEADROOM - hdr_size);
1830 seg_num = header->num_buffers;
1835 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1836 rxm->nb_segs = seg_num;
1839 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1840 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1842 rxm->port = rxvq->port_id;
1843 rx_pkts[nb_rx] = rxm;
1846 if (hw->has_rx_offload &&
1847 virtio_rx_offload(rxm, &header->hdr) < 0) {
1848 virtio_discard_rxbuf(vq, rxm);
1849 rxvq->stats.errors++;
1854 rte_vlan_strip(rx_pkts[nb_rx]);
1856 seg_res = seg_num - 1;
1858 /* Merge remaining segments */
1859 while (seg_res != 0 && i < (num - 1)) {
1863 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1864 rxm->pkt_len = (uint32_t)(len[i]);
1865 rxm->data_len = (uint16_t)(len[i]);
1867 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1875 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1880 /* Last packet still need merge segments */
1881 while (seg_res != 0) {
1882 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1883 VIRTIO_MBUF_BURST_SZ);
1884 uint16_t extra_idx = 0;
1886 rcv_cnt = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts,
1888 if (unlikely(rcv_cnt == 0)) {
1889 PMD_RX_LOG(ERR, "No enough segments for packet.");
1890 rte_pktmbuf_free(rx_pkts[nb_rx]);
1891 rxvq->stats.errors++;
1895 while (extra_idx < rcv_cnt) {
1896 rxm = rcv_pkts[extra_idx];
1898 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1899 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1900 rxm->data_len = (uint16_t)(len[extra_idx]);
1904 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1909 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1914 rxvq->stats.packets += nb_rx;
1916 /* Allocate new mbuf for the used descriptor */
1917 if (likely(!virtqueue_full(vq))) {
1918 /* free_cnt may include mrg descs */
1919 uint16_t free_cnt = vq->vq_free_cnt;
1920 struct rte_mbuf *new_pkts[free_cnt];
1922 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1923 error = virtqueue_enqueue_recv_refill_packed(vq,
1924 new_pkts, free_cnt);
1925 if (unlikely(error)) {
1926 for (i = 0; i < free_cnt; i++)
1927 rte_pktmbuf_free(new_pkts[i]);
1929 nb_enqueued += free_cnt;
1931 struct rte_eth_dev *dev =
1932 &rte_eth_devices[rxvq->port_id];
1933 dev->data->rx_mbuf_alloc_failed += free_cnt;
1937 if (likely(nb_enqueued)) {
1938 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1939 virtqueue_notify(vq);
1940 PMD_RX_LOG(DEBUG, "Notified");
1948 virtio_xmit_pkts_prepare(void *tx_queue __rte_unused, struct rte_mbuf **tx_pkts,
1954 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1955 struct rte_mbuf *m = tx_pkts[nb_tx];
1957 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1958 error = rte_validate_tx_offload(m);
1959 if (unlikely(error)) {
1965 /* Do VLAN tag insertion */
1966 if (unlikely(m->ol_flags & PKT_TX_VLAN_PKT)) {
1967 error = rte_vlan_insert(&m);
1968 /* rte_vlan_insert() may change pointer
1969 * even in the case of failure
1973 if (unlikely(error)) {
1979 error = rte_net_intel_cksum_prepare(m);
1980 if (unlikely(error)) {
1985 if (m->ol_flags & PKT_TX_TCP_SEG)
1986 virtio_tso_fix_cksum(m);
1993 virtio_xmit_pkts_packed(void *tx_queue, struct rte_mbuf **tx_pkts,
1996 struct virtnet_tx *txvq = tx_queue;
1997 struct virtqueue *vq = txvq->vq;
1998 struct virtio_hw *hw = vq->hw;
1999 uint16_t hdr_size = hw->vtnet_hdr_size;
2001 bool in_order = hw->use_inorder_tx;
2003 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2006 if (unlikely(nb_pkts < 1))
2009 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2011 if (nb_pkts > vq->vq_free_cnt)
2012 virtio_xmit_cleanup_packed(vq, nb_pkts - vq->vq_free_cnt,
2015 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2016 struct rte_mbuf *txm = tx_pkts[nb_tx];
2017 int can_push = 0, slots, need;
2019 /* optimize ring usage */
2020 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2021 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2022 rte_mbuf_refcnt_read(txm) == 1 &&
2023 RTE_MBUF_DIRECT(txm) &&
2024 txm->nb_segs == 1 &&
2025 rte_pktmbuf_headroom(txm) >= hdr_size &&
2026 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2027 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2030 /* How many main ring entries are needed to this Tx?
2031 * any_layout => number of segments
2032 * default => number of segments + 1
2034 slots = txm->nb_segs + !can_push;
2035 need = slots - vq->vq_free_cnt;
2037 /* Positive value indicates it need free vring descriptors */
2038 if (unlikely(need > 0)) {
2039 virtio_xmit_cleanup_packed(vq, need, in_order);
2040 need = slots - vq->vq_free_cnt;
2041 if (unlikely(need > 0)) {
2043 "No free tx descriptors to transmit");
2048 /* Enqueue Packet buffers */
2050 virtqueue_enqueue_xmit_packed_fast(txvq, txm, in_order);
2052 virtqueue_enqueue_xmit_packed(txvq, txm, slots, 0,
2055 virtio_update_packet_stats(&txvq->stats, txm);
2058 txvq->stats.packets += nb_tx;
2060 if (likely(nb_tx)) {
2061 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
2062 virtqueue_notify(vq);
2063 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2071 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
2073 struct virtnet_tx *txvq = tx_queue;
2074 struct virtqueue *vq = txvq->vq;
2075 struct virtio_hw *hw = vq->hw;
2076 uint16_t hdr_size = hw->vtnet_hdr_size;
2077 uint16_t nb_used, nb_tx = 0;
2079 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2082 if (unlikely(nb_pkts < 1))
2085 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2086 nb_used = VIRTQUEUE_NUSED(vq);
2088 virtio_rmb(hw->weak_barriers);
2089 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2090 virtio_xmit_cleanup(vq, nb_used);
2092 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2093 struct rte_mbuf *txm = tx_pkts[nb_tx];
2094 int can_push = 0, use_indirect = 0, slots, need;
2096 /* optimize ring usage */
2097 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2098 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2099 rte_mbuf_refcnt_read(txm) == 1 &&
2100 RTE_MBUF_DIRECT(txm) &&
2101 txm->nb_segs == 1 &&
2102 rte_pktmbuf_headroom(txm) >= hdr_size &&
2103 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2104 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2106 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
2107 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
2110 /* How many main ring entries are needed to this Tx?
2111 * any_layout => number of segments
2113 * default => number of segments + 1
2115 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
2116 need = slots - vq->vq_free_cnt;
2118 /* Positive value indicates it need free vring descriptors */
2119 if (unlikely(need > 0)) {
2120 nb_used = VIRTQUEUE_NUSED(vq);
2121 virtio_rmb(hw->weak_barriers);
2122 need = RTE_MIN(need, (int)nb_used);
2124 virtio_xmit_cleanup(vq, need);
2125 need = slots - vq->vq_free_cnt;
2126 if (unlikely(need > 0)) {
2128 "No free tx descriptors to transmit");
2133 /* Enqueue Packet buffers */
2134 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect,
2137 virtio_update_packet_stats(&txvq->stats, txm);
2140 txvq->stats.packets += nb_tx;
2142 if (likely(nb_tx)) {
2143 vq_update_avail_idx(vq);
2145 if (unlikely(virtqueue_kick_prepare(vq))) {
2146 virtqueue_notify(vq);
2147 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2155 virtio_xmit_pkts_inorder(void *tx_queue,
2156 struct rte_mbuf **tx_pkts,
2159 struct virtnet_tx *txvq = tx_queue;
2160 struct virtqueue *vq = txvq->vq;
2161 struct virtio_hw *hw = vq->hw;
2162 uint16_t hdr_size = hw->vtnet_hdr_size;
2163 uint16_t nb_used, nb_avail, nb_tx = 0, nb_inorder_pkts = 0;
2164 struct rte_mbuf *inorder_pkts[nb_pkts];
2166 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2169 if (unlikely(nb_pkts < 1))
2173 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2174 nb_used = VIRTQUEUE_NUSED(vq);
2176 virtio_rmb(hw->weak_barriers);
2177 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2178 virtio_xmit_cleanup_inorder(vq, nb_used);
2180 if (unlikely(!vq->vq_free_cnt))
2181 virtio_xmit_cleanup_inorder(vq, nb_used);
2183 nb_avail = RTE_MIN(vq->vq_free_cnt, nb_pkts);
2185 for (nb_tx = 0; nb_tx < nb_avail; nb_tx++) {
2186 struct rte_mbuf *txm = tx_pkts[nb_tx];
2189 /* optimize ring usage */
2190 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2191 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2192 rte_mbuf_refcnt_read(txm) == 1 &&
2193 RTE_MBUF_DIRECT(txm) &&
2194 txm->nb_segs == 1 &&
2195 rte_pktmbuf_headroom(txm) >= hdr_size &&
2196 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2197 __alignof__(struct virtio_net_hdr_mrg_rxbuf))) {
2198 inorder_pkts[nb_inorder_pkts] = txm;
2201 virtio_update_packet_stats(&txvq->stats, txm);
2205 if (nb_inorder_pkts) {
2206 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2208 nb_inorder_pkts = 0;
2211 slots = txm->nb_segs + 1;
2212 need = slots - vq->vq_free_cnt;
2213 if (unlikely(need > 0)) {
2214 nb_used = VIRTQUEUE_NUSED(vq);
2215 virtio_rmb(hw->weak_barriers);
2216 need = RTE_MIN(need, (int)nb_used);
2218 virtio_xmit_cleanup_inorder(vq, need);
2220 need = slots - vq->vq_free_cnt;
2222 if (unlikely(need > 0)) {
2224 "No free tx descriptors to transmit");
2228 /* Enqueue Packet buffers */
2229 virtqueue_enqueue_xmit(txvq, txm, slots, 0, 0, 1);
2231 virtio_update_packet_stats(&txvq->stats, txm);
2234 /* Transmit all inorder packets */
2235 if (nb_inorder_pkts)
2236 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2239 txvq->stats.packets += nb_tx;
2241 if (likely(nb_tx)) {
2242 vq_update_avail_idx(vq);
2244 if (unlikely(virtqueue_kick_prepare(vq))) {
2245 virtqueue_notify(vq);
2246 PMD_TX_LOG(DEBUG, "Notified backend after xmit");