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_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_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_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->ring_packed.desc_packed;
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->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_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_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->ring_packed.desc_packed;
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->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->ring_packed.desc_packed;
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->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_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_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_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->ring_packed.desc_packed;
434 uint16_t flags = VRING_DESC_F_WRITE | vq->avail_used_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->avail_wrap_counter ^= 1;
464 vq->avail_used_flags =
465 VRING_DESC_F_AVAIL(vq->avail_wrap_counter) |
466 VRING_DESC_F_USED(!vq->avail_wrap_counter);
467 flags = VRING_DESC_F_WRITE | vq->avail_used_flags;
470 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
474 /* When doing TSO, the IP length is not included in the pseudo header
475 * checksum of the packet given to the PMD, but for virtio it is
479 virtio_tso_fix_cksum(struct rte_mbuf *m)
481 /* common case: header is not fragmented */
482 if (likely(rte_pktmbuf_data_len(m) >= m->l2_len + m->l3_len +
484 struct ipv4_hdr *iph;
485 struct ipv6_hdr *ip6h;
487 uint16_t prev_cksum, new_cksum, ip_len, ip_paylen;
490 iph = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *, m->l2_len);
491 th = RTE_PTR_ADD(iph, m->l3_len);
492 if ((iph->version_ihl >> 4) == 4) {
493 iph->hdr_checksum = 0;
494 iph->hdr_checksum = rte_ipv4_cksum(iph);
495 ip_len = iph->total_length;
496 ip_paylen = rte_cpu_to_be_16(rte_be_to_cpu_16(ip_len) -
499 ip6h = (struct ipv6_hdr *)iph;
500 ip_paylen = ip6h->payload_len;
503 /* calculate the new phdr checksum not including ip_paylen */
504 prev_cksum = th->cksum;
507 tmp = (tmp & 0xffff) + (tmp >> 16);
510 /* replace it in the packet */
511 th->cksum = new_cksum;
516 /* avoid write operation when necessary, to lessen cache issues */
517 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
518 if ((var) != (val)) \
523 virtqueue_xmit_offload(struct virtio_net_hdr *hdr,
524 struct rte_mbuf *cookie,
528 if (cookie->ol_flags & PKT_TX_TCP_SEG)
529 cookie->ol_flags |= PKT_TX_TCP_CKSUM;
531 switch (cookie->ol_flags & PKT_TX_L4_MASK) {
532 case PKT_TX_UDP_CKSUM:
533 hdr->csum_start = cookie->l2_len + cookie->l3_len;
534 hdr->csum_offset = offsetof(struct udp_hdr,
536 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
539 case PKT_TX_TCP_CKSUM:
540 hdr->csum_start = cookie->l2_len + cookie->l3_len;
541 hdr->csum_offset = offsetof(struct tcp_hdr, cksum);
542 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
546 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
547 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
548 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
552 /* TCP Segmentation Offload */
553 if (cookie->ol_flags & PKT_TX_TCP_SEG) {
554 virtio_tso_fix_cksum(cookie);
555 hdr->gso_type = (cookie->ol_flags & PKT_TX_IPV6) ?
556 VIRTIO_NET_HDR_GSO_TCPV6 :
557 VIRTIO_NET_HDR_GSO_TCPV4;
558 hdr->gso_size = cookie->tso_segsz;
564 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
565 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
566 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
572 virtqueue_enqueue_xmit_inorder(struct virtnet_tx *txvq,
573 struct rte_mbuf **cookies,
576 struct vq_desc_extra *dxp;
577 struct virtqueue *vq = txvq->vq;
578 struct vring_desc *start_dp;
579 struct virtio_net_hdr *hdr;
581 uint16_t head_size = vq->hw->vtnet_hdr_size;
584 idx = vq->vq_desc_head_idx;
585 start_dp = vq->vq_ring.desc;
588 idx = idx & (vq->vq_nentries - 1);
589 dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
590 dxp->cookie = (void *)cookies[i];
593 hdr = (struct virtio_net_hdr *)
594 rte_pktmbuf_prepend(cookies[i], head_size);
595 cookies[i]->pkt_len -= head_size;
597 /* if offload disabled, it is not zeroed below, do it now */
598 if (!vq->hw->has_tx_offload) {
599 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
600 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
601 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
602 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
603 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
604 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
607 virtqueue_xmit_offload(hdr, cookies[i],
608 vq->hw->has_tx_offload);
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(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
626 uint16_t needed, int can_push, int in_order)
628 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
629 struct vq_desc_extra *dxp;
630 struct virtqueue *vq = txvq->vq;
631 struct vring_packed_desc *start_dp, *head_dp;
632 uint16_t idx, id, head_idx, head_flags;
633 uint16_t head_size = vq->hw->vtnet_hdr_size;
634 struct virtio_net_hdr *hdr;
637 id = in_order ? vq->vq_avail_idx : vq->vq_desc_head_idx;
639 dxp = &vq->vq_descx[id];
640 dxp->ndescs = needed;
641 dxp->cookie = cookie;
643 head_idx = vq->vq_avail_idx;
646 start_dp = vq->ring_packed.desc_packed;
648 head_dp = &vq->ring_packed.desc_packed[idx];
649 head_flags = cookie->next ? VRING_DESC_F_NEXT : 0;
650 head_flags |= vq->avail_used_flags;
653 /* prepend cannot fail, checked by caller */
654 hdr = (struct virtio_net_hdr *)
655 rte_pktmbuf_prepend(cookie, head_size);
656 /* rte_pktmbuf_prepend() counts the hdr size to the pkt length,
657 * which is wrong. Below subtract restores correct pkt size.
659 cookie->pkt_len -= head_size;
661 /* if offload disabled, it is not zeroed below, do it now */
662 if (!vq->hw->has_tx_offload) {
663 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
664 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
665 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
666 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
667 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
668 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
671 /* setup first tx ring slot to point to header
672 * stored in reserved region.
674 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
675 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
676 start_dp[idx].len = vq->hw->vtnet_hdr_size;
677 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
679 if (idx >= vq->vq_nentries) {
680 idx -= vq->vq_nentries;
681 vq->avail_wrap_counter ^= 1;
682 vq->avail_used_flags =
683 VRING_DESC_F_AVAIL(vq->avail_wrap_counter) |
684 VRING_DESC_F_USED(!vq->avail_wrap_counter);
688 virtqueue_xmit_offload(hdr, cookie, vq->hw->has_tx_offload);
693 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
694 start_dp[idx].len = cookie->data_len;
695 if (likely(idx != head_idx)) {
696 flags = cookie->next ? VRING_DESC_F_NEXT : 0;
697 flags |= vq->avail_used_flags;
698 start_dp[idx].flags = flags;
702 if (idx >= vq->vq_nentries) {
703 idx -= vq->vq_nentries;
704 vq->avail_wrap_counter ^= 1;
705 vq->avail_used_flags =
706 VRING_DESC_F_AVAIL(vq->avail_wrap_counter) |
707 VRING_DESC_F_USED(!vq->avail_wrap_counter);
709 } while ((cookie = cookie->next) != NULL);
711 start_dp[prev].id = id;
713 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
714 vq->vq_avail_idx = idx;
717 vq->vq_desc_head_idx = dxp->next;
718 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
719 vq->vq_desc_tail_idx = VQ_RING_DESC_CHAIN_END;
722 virtio_wmb(vq->hw->weak_barriers);
723 head_dp->flags = head_flags;
727 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
728 uint16_t needed, int use_indirect, int can_push,
731 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
732 struct vq_desc_extra *dxp;
733 struct virtqueue *vq = txvq->vq;
734 struct vring_desc *start_dp;
735 uint16_t seg_num = cookie->nb_segs;
736 uint16_t head_idx, idx;
737 uint16_t head_size = vq->hw->vtnet_hdr_size;
738 struct virtio_net_hdr *hdr;
740 head_idx = vq->vq_desc_head_idx;
743 dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
745 dxp = &vq->vq_descx[idx];
746 dxp->cookie = (void *)cookie;
747 dxp->ndescs = needed;
749 start_dp = vq->vq_ring.desc;
752 /* prepend cannot fail, checked by caller */
753 hdr = (struct virtio_net_hdr *)
754 rte_pktmbuf_prepend(cookie, head_size);
755 /* rte_pktmbuf_prepend() counts the hdr size to the pkt length,
756 * which is wrong. Below subtract restores correct pkt size.
758 cookie->pkt_len -= head_size;
760 /* if offload disabled, it is not zeroed below, do it now */
761 if (!vq->hw->has_tx_offload) {
762 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
763 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
764 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
765 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
766 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
767 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
769 } else if (use_indirect) {
770 /* setup tx ring slot to point to indirect
771 * descriptor list stored in reserved region.
773 * the first slot in indirect ring is already preset
774 * to point to the header in reserved region
776 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
777 RTE_PTR_DIFF(&txr[idx].tx_indir, txr);
778 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
779 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
780 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
782 /* loop below will fill in rest of the indirect elements */
783 start_dp = txr[idx].tx_indir;
786 /* setup first tx ring slot to point to header
787 * stored in reserved region.
789 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
790 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
791 start_dp[idx].len = vq->hw->vtnet_hdr_size;
792 start_dp[idx].flags = VRING_DESC_F_NEXT;
793 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
795 idx = start_dp[idx].next;
798 virtqueue_xmit_offload(hdr, cookie, vq->hw->has_tx_offload);
801 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
802 start_dp[idx].len = cookie->data_len;
803 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
804 idx = start_dp[idx].next;
805 } while ((cookie = cookie->next) != NULL);
808 idx = vq->vq_ring.desc[head_idx].next;
810 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
812 vq->vq_desc_head_idx = idx;
813 vq_update_avail_ring(vq, head_idx);
816 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
817 vq->vq_desc_tail_idx = idx;
822 virtio_dev_cq_start(struct rte_eth_dev *dev)
824 struct virtio_hw *hw = dev->data->dev_private;
826 if (hw->cvq && hw->cvq->vq) {
827 rte_spinlock_init(&hw->cvq->lock);
828 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
833 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
836 unsigned int socket_id __rte_unused,
837 const struct rte_eth_rxconf *rx_conf __rte_unused,
838 struct rte_mempool *mp)
840 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
841 struct virtio_hw *hw = dev->data->dev_private;
842 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
843 struct virtnet_rx *rxvq;
845 PMD_INIT_FUNC_TRACE();
847 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
848 nb_desc = vq->vq_nentries;
849 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
852 rxvq->queue_id = queue_idx;
854 if (rxvq->mpool == NULL) {
855 rte_exit(EXIT_FAILURE,
856 "Cannot allocate mbufs for rx virtqueue");
859 dev->data->rx_queues[queue_idx] = rxvq;
865 virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
867 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
868 struct virtio_hw *hw = dev->data->dev_private;
869 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
870 struct virtnet_rx *rxvq = &vq->rxq;
875 PMD_INIT_FUNC_TRACE();
877 /* Allocate blank mbufs for the each rx descriptor */
880 if (hw->use_simple_rx) {
881 for (desc_idx = 0; desc_idx < vq->vq_nentries;
883 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
884 vq->vq_ring.desc[desc_idx].flags =
888 virtio_rxq_vec_setup(rxvq);
891 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
892 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
894 vq->sw_ring[vq->vq_nentries + desc_idx] =
898 if (hw->use_simple_rx) {
899 while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
900 virtio_rxq_rearm_vec(rxvq);
901 nbufs += RTE_VIRTIO_VPMD_RX_REARM_THRESH;
903 } else if (hw->use_inorder_rx) {
904 if ((!virtqueue_full(vq))) {
905 uint16_t free_cnt = vq->vq_free_cnt;
906 struct rte_mbuf *pkts[free_cnt];
908 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, pkts,
910 error = virtqueue_enqueue_refill_inorder(vq,
913 if (unlikely(error)) {
914 for (i = 0; i < free_cnt; i++)
915 rte_pktmbuf_free(pkts[i]);
920 vq_update_avail_idx(vq);
923 while (!virtqueue_full(vq)) {
924 m = rte_mbuf_raw_alloc(rxvq->mpool);
928 /* Enqueue allocated buffers */
929 if (vtpci_packed_queue(vq->hw))
930 error = virtqueue_enqueue_recv_refill_packed(vq,
933 error = virtqueue_enqueue_recv_refill(vq,
942 if (!vtpci_packed_queue(vq->hw))
943 vq_update_avail_idx(vq);
946 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
954 * struct rte_eth_dev *dev: Used to update dev
955 * uint16_t nb_desc: Defaults to values read from config space
956 * unsigned int socket_id: Used to allocate memzone
957 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
958 * uint16_t queue_idx: Just used as an index in dev txq list
961 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
964 unsigned int socket_id __rte_unused,
965 const struct rte_eth_txconf *tx_conf)
967 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
968 struct virtio_hw *hw = dev->data->dev_private;
969 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
970 struct virtnet_tx *txvq;
971 uint16_t tx_free_thresh;
973 PMD_INIT_FUNC_TRACE();
975 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
976 nb_desc = vq->vq_nentries;
977 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
980 txvq->queue_id = queue_idx;
982 tx_free_thresh = tx_conf->tx_free_thresh;
983 if (tx_free_thresh == 0)
985 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
987 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
988 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
989 "number of TX entries minus 3 (%u)."
990 " (tx_free_thresh=%u port=%u queue=%u)\n",
992 tx_free_thresh, dev->data->port_id, queue_idx);
996 vq->vq_free_thresh = tx_free_thresh;
998 dev->data->tx_queues[queue_idx] = txvq;
1003 virtio_dev_tx_queue_setup_finish(struct rte_eth_dev *dev,
1006 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
1007 struct virtio_hw *hw = dev->data->dev_private;
1008 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
1010 PMD_INIT_FUNC_TRACE();
1012 if (!vtpci_packed_queue(hw)) {
1013 if (hw->use_inorder_tx)
1014 vq->vq_ring.desc[vq->vq_nentries - 1].next = 0;
1023 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
1027 * Requeue the discarded mbuf. This should always be
1028 * successful since it was just dequeued.
1030 if (vtpci_packed_queue(vq->hw))
1031 error = virtqueue_enqueue_recv_refill_packed(vq, &m, 1);
1033 error = virtqueue_enqueue_recv_refill(vq, &m, 1);
1035 if (unlikely(error)) {
1036 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
1037 rte_pktmbuf_free(m);
1042 virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)
1046 error = virtqueue_enqueue_refill_inorder(vq, &m, 1);
1047 if (unlikely(error)) {
1048 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
1049 rte_pktmbuf_free(m);
1054 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
1056 uint32_t s = mbuf->pkt_len;
1057 struct ether_addr *ea;
1062 stats->size_bins[1]++;
1063 } else if (s > 64 && s < 1024) {
1066 /* count zeros, and offset into correct bin */
1067 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
1068 stats->size_bins[bin]++;
1071 stats->size_bins[0]++;
1073 stats->size_bins[6]++;
1075 stats->size_bins[7]++;
1078 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
1079 if (is_multicast_ether_addr(ea)) {
1080 if (is_broadcast_ether_addr(ea))
1088 virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
1090 VIRTIO_DUMP_PACKET(m, m->data_len);
1092 virtio_update_packet_stats(&rxvq->stats, m);
1095 /* Optionally fill offload information in structure */
1097 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
1099 struct rte_net_hdr_lens hdr_lens;
1100 uint32_t hdrlen, ptype;
1101 int l4_supported = 0;
1104 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1107 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
1109 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
1110 m->packet_type = ptype;
1111 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
1112 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
1113 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
1116 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1117 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
1118 if (hdr->csum_start <= hdrlen && l4_supported) {
1119 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
1121 /* Unknown proto or tunnel, do sw cksum. We can assume
1122 * the cksum field is in the first segment since the
1123 * buffers we provided to the host are large enough.
1124 * In case of SCTP, this will be wrong since it's a CRC
1125 * but there's nothing we can do.
1127 uint16_t csum = 0, off;
1129 rte_raw_cksum_mbuf(m, hdr->csum_start,
1130 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
1132 if (likely(csum != 0xffff))
1134 off = hdr->csum_offset + hdr->csum_start;
1135 if (rte_pktmbuf_data_len(m) >= off + 1)
1136 *rte_pktmbuf_mtod_offset(m, uint16_t *,
1139 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
1140 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
1143 /* GSO request, save required information in mbuf */
1144 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1145 /* Check unsupported modes */
1146 if ((hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) ||
1147 (hdr->gso_size == 0)) {
1151 /* Update mss lengthes in mbuf */
1152 m->tso_segsz = hdr->gso_size;
1153 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1154 case VIRTIO_NET_HDR_GSO_TCPV4:
1155 case VIRTIO_NET_HDR_GSO_TCPV6:
1156 m->ol_flags |= PKT_RX_LRO | \
1157 PKT_RX_L4_CKSUM_NONE;
1167 #define VIRTIO_MBUF_BURST_SZ 64
1168 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
1170 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1172 struct virtnet_rx *rxvq = rx_queue;
1173 struct virtqueue *vq = rxvq->vq;
1174 struct virtio_hw *hw = vq->hw;
1175 struct rte_mbuf *rxm, *new_mbuf;
1176 uint16_t nb_used, num, nb_rx;
1177 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1178 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1180 uint32_t i, nb_enqueued;
1182 struct virtio_net_hdr *hdr;
1185 if (unlikely(hw->started == 0))
1188 nb_used = VIRTQUEUE_NUSED(vq);
1190 virtio_rmb(hw->weak_barriers);
1192 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1193 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1194 num = VIRTIO_MBUF_BURST_SZ;
1195 if (likely(num > DESC_PER_CACHELINE))
1196 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1198 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1199 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
1202 hdr_size = hw->vtnet_hdr_size;
1204 for (i = 0; i < num ; i++) {
1207 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1209 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1210 PMD_RX_LOG(ERR, "Packet drop");
1212 virtio_discard_rxbuf(vq, rxm);
1213 rxvq->stats.errors++;
1217 rxm->port = rxvq->port_id;
1218 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1222 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1223 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1225 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1226 RTE_PKTMBUF_HEADROOM - hdr_size);
1229 rte_vlan_strip(rxm);
1231 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1232 virtio_discard_rxbuf(vq, rxm);
1233 rxvq->stats.errors++;
1237 virtio_rx_stats_updated(rxvq, rxm);
1239 rx_pkts[nb_rx++] = rxm;
1242 rxvq->stats.packets += nb_rx;
1244 /* Allocate new mbuf for the used descriptor */
1245 while (likely(!virtqueue_full(vq))) {
1246 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
1247 if (unlikely(new_mbuf == NULL)) {
1248 struct rte_eth_dev *dev
1249 = &rte_eth_devices[rxvq->port_id];
1250 dev->data->rx_mbuf_alloc_failed++;
1253 error = virtqueue_enqueue_recv_refill(vq, &new_mbuf, 1);
1254 if (unlikely(error)) {
1255 rte_pktmbuf_free(new_mbuf);
1261 if (likely(nb_enqueued)) {
1262 vq_update_avail_idx(vq);
1264 if (unlikely(virtqueue_kick_prepare(vq))) {
1265 virtqueue_notify(vq);
1266 PMD_RX_LOG(DEBUG, "Notified");
1274 virtio_recv_pkts_packed(void *rx_queue, struct rte_mbuf **rx_pkts,
1277 struct virtnet_rx *rxvq = rx_queue;
1278 struct virtqueue *vq = rxvq->vq;
1279 struct virtio_hw *hw = vq->hw;
1280 struct rte_mbuf *rxm, *new_mbuf;
1281 uint16_t num, nb_rx;
1282 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1283 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1285 uint32_t i, nb_enqueued;
1287 struct virtio_net_hdr *hdr;
1290 if (unlikely(hw->started == 0))
1293 num = RTE_MIN(VIRTIO_MBUF_BURST_SZ, nb_pkts);
1294 if (likely(num > DESC_PER_CACHELINE))
1295 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1297 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1298 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1301 hdr_size = hw->vtnet_hdr_size;
1303 for (i = 0; i < num; i++) {
1306 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1308 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1309 PMD_RX_LOG(ERR, "Packet drop");
1311 virtio_discard_rxbuf(vq, rxm);
1312 rxvq->stats.errors++;
1316 rxm->port = rxvq->port_id;
1317 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1321 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1322 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1324 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1325 RTE_PKTMBUF_HEADROOM - hdr_size);
1328 rte_vlan_strip(rxm);
1330 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1331 virtio_discard_rxbuf(vq, rxm);
1332 rxvq->stats.errors++;
1336 virtio_rx_stats_updated(rxvq, rxm);
1338 rx_pkts[nb_rx++] = rxm;
1341 rxvq->stats.packets += nb_rx;
1343 /* Allocate new mbuf for the used descriptor */
1344 while (likely(!virtqueue_full(vq))) {
1345 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
1346 if (unlikely(new_mbuf == NULL)) {
1347 struct rte_eth_dev *dev =
1348 &rte_eth_devices[rxvq->port_id];
1349 dev->data->rx_mbuf_alloc_failed++;
1352 error = virtqueue_enqueue_recv_refill_packed(vq, &new_mbuf, 1);
1353 if (unlikely(error)) {
1354 rte_pktmbuf_free(new_mbuf);
1360 if (likely(nb_enqueued)) {
1361 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1362 virtqueue_notify(vq);
1363 PMD_RX_LOG(DEBUG, "Notified");
1372 virtio_recv_pkts_inorder(void *rx_queue,
1373 struct rte_mbuf **rx_pkts,
1376 struct virtnet_rx *rxvq = rx_queue;
1377 struct virtqueue *vq = rxvq->vq;
1378 struct virtio_hw *hw = vq->hw;
1379 struct rte_mbuf *rxm;
1380 struct rte_mbuf *prev;
1381 uint16_t nb_used, num, nb_rx;
1382 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1383 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1385 uint32_t nb_enqueued;
1392 if (unlikely(hw->started == 0))
1395 nb_used = VIRTQUEUE_NUSED(vq);
1396 nb_used = RTE_MIN(nb_used, nb_pkts);
1397 nb_used = RTE_MIN(nb_used, VIRTIO_MBUF_BURST_SZ);
1399 virtio_rmb(hw->weak_barriers);
1401 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1406 hdr_size = hw->vtnet_hdr_size;
1408 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len, nb_used);
1410 for (i = 0; i < num; i++) {
1411 struct virtio_net_hdr_mrg_rxbuf *header;
1413 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1414 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1418 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1419 PMD_RX_LOG(ERR, "Packet drop");
1421 virtio_discard_rxbuf_inorder(vq, rxm);
1422 rxvq->stats.errors++;
1426 header = (struct virtio_net_hdr_mrg_rxbuf *)
1427 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1430 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1431 seg_num = header->num_buffers;
1438 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1439 rxm->nb_segs = seg_num;
1442 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1443 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1445 rxm->port = rxvq->port_id;
1447 rx_pkts[nb_rx] = rxm;
1450 if (vq->hw->has_rx_offload &&
1451 virtio_rx_offload(rxm, &header->hdr) < 0) {
1452 virtio_discard_rxbuf_inorder(vq, rxm);
1453 rxvq->stats.errors++;
1458 rte_vlan_strip(rx_pkts[nb_rx]);
1460 seg_res = seg_num - 1;
1462 /* Merge remaining segments */
1463 while (seg_res != 0 && i < (num - 1)) {
1467 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1468 rxm->pkt_len = (uint32_t)(len[i]);
1469 rxm->data_len = (uint16_t)(len[i]);
1471 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1472 rx_pkts[nb_rx]->data_len += (uint16_t)(len[i]);
1482 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1487 /* Last packet still need merge segments */
1488 while (seg_res != 0) {
1489 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1490 VIRTIO_MBUF_BURST_SZ);
1492 prev = rcv_pkts[nb_rx];
1493 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1494 virtio_rmb(hw->weak_barriers);
1495 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len,
1497 uint16_t extra_idx = 0;
1500 while (extra_idx < rcv_cnt) {
1501 rxm = rcv_pkts[extra_idx];
1503 RTE_PKTMBUF_HEADROOM - hdr_size;
1504 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1505 rxm->data_len = (uint16_t)(len[extra_idx]);
1508 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1509 rx_pkts[nb_rx]->data_len += len[extra_idx];
1515 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1520 "No enough segments for packet.");
1521 virtio_discard_rxbuf_inorder(vq, prev);
1522 rxvq->stats.errors++;
1527 rxvq->stats.packets += nb_rx;
1529 /* Allocate new mbuf for the used descriptor */
1531 if (likely(!virtqueue_full(vq))) {
1532 /* free_cnt may include mrg descs */
1533 uint16_t free_cnt = vq->vq_free_cnt;
1534 struct rte_mbuf *new_pkts[free_cnt];
1536 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1537 error = virtqueue_enqueue_refill_inorder(vq, new_pkts,
1539 if (unlikely(error)) {
1540 for (i = 0; i < free_cnt; i++)
1541 rte_pktmbuf_free(new_pkts[i]);
1543 nb_enqueued += free_cnt;
1545 struct rte_eth_dev *dev =
1546 &rte_eth_devices[rxvq->port_id];
1547 dev->data->rx_mbuf_alloc_failed += free_cnt;
1551 if (likely(nb_enqueued)) {
1552 vq_update_avail_idx(vq);
1554 if (unlikely(virtqueue_kick_prepare(vq))) {
1555 virtqueue_notify(vq);
1556 PMD_RX_LOG(DEBUG, "Notified");
1564 virtio_recv_mergeable_pkts(void *rx_queue,
1565 struct rte_mbuf **rx_pkts,
1568 struct virtnet_rx *rxvq = rx_queue;
1569 struct virtqueue *vq = rxvq->vq;
1570 struct virtio_hw *hw = vq->hw;
1571 struct rte_mbuf *rxm;
1572 struct rte_mbuf *prev;
1573 uint16_t nb_used, num, nb_rx = 0;
1574 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1575 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1577 uint32_t nb_enqueued = 0;
1578 uint32_t seg_num = 0;
1579 uint32_t seg_res = 0;
1580 uint32_t hdr_size = hw->vtnet_hdr_size;
1583 if (unlikely(hw->started == 0))
1586 nb_used = VIRTQUEUE_NUSED(vq);
1588 virtio_rmb(hw->weak_barriers);
1590 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1592 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1593 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1594 num = VIRTIO_MBUF_BURST_SZ;
1595 if (likely(num > DESC_PER_CACHELINE))
1596 num = num - ((vq->vq_used_cons_idx + num) %
1597 DESC_PER_CACHELINE);
1600 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1602 for (i = 0; i < num; i++) {
1603 struct virtio_net_hdr_mrg_rxbuf *header;
1605 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1606 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1610 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1611 PMD_RX_LOG(ERR, "Packet drop");
1613 virtio_discard_rxbuf(vq, rxm);
1614 rxvq->stats.errors++;
1618 header = (struct virtio_net_hdr_mrg_rxbuf *)
1619 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1621 seg_num = header->num_buffers;
1625 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1626 rxm->nb_segs = seg_num;
1629 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1630 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1632 rxm->port = rxvq->port_id;
1634 rx_pkts[nb_rx] = rxm;
1637 if (hw->has_rx_offload &&
1638 virtio_rx_offload(rxm, &header->hdr) < 0) {
1639 virtio_discard_rxbuf(vq, rxm);
1640 rxvq->stats.errors++;
1645 rte_vlan_strip(rx_pkts[nb_rx]);
1647 seg_res = seg_num - 1;
1649 /* Merge remaining segments */
1650 while (seg_res != 0 && i < (num - 1)) {
1654 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1655 rxm->pkt_len = (uint32_t)(len[i]);
1656 rxm->data_len = (uint16_t)(len[i]);
1658 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1659 rx_pkts[nb_rx]->data_len += (uint16_t)(len[i]);
1669 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1674 /* Last packet still need merge segments */
1675 while (seg_res != 0) {
1676 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1677 VIRTIO_MBUF_BURST_SZ);
1679 prev = rcv_pkts[nb_rx];
1680 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
1681 virtio_rmb(hw->weak_barriers);
1682 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len,
1684 uint16_t extra_idx = 0;
1687 while (extra_idx < rcv_cnt) {
1688 rxm = rcv_pkts[extra_idx];
1690 RTE_PKTMBUF_HEADROOM - hdr_size;
1691 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1692 rxm->data_len = (uint16_t)(len[extra_idx]);
1695 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1696 rx_pkts[nb_rx]->data_len += len[extra_idx];
1702 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1707 "No enough segments for packet.");
1708 virtio_discard_rxbuf(vq, prev);
1709 rxvq->stats.errors++;
1714 rxvq->stats.packets += nb_rx;
1716 /* Allocate new mbuf for the used descriptor */
1717 if (likely(!virtqueue_full(vq))) {
1718 /* free_cnt may include mrg descs */
1719 uint16_t free_cnt = vq->vq_free_cnt;
1720 struct rte_mbuf *new_pkts[free_cnt];
1722 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1723 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1725 if (unlikely(error)) {
1726 for (i = 0; i < free_cnt; i++)
1727 rte_pktmbuf_free(new_pkts[i]);
1729 nb_enqueued += free_cnt;
1731 struct rte_eth_dev *dev =
1732 &rte_eth_devices[rxvq->port_id];
1733 dev->data->rx_mbuf_alloc_failed += free_cnt;
1737 if (likely(nb_enqueued)) {
1738 vq_update_avail_idx(vq);
1740 if (unlikely(virtqueue_kick_prepare(vq))) {
1741 virtqueue_notify(vq);
1742 PMD_RX_LOG(DEBUG, "Notified");
1750 virtio_recv_mergeable_pkts_packed(void *rx_queue,
1751 struct rte_mbuf **rx_pkts,
1754 struct virtnet_rx *rxvq = rx_queue;
1755 struct virtqueue *vq = rxvq->vq;
1756 struct virtio_hw *hw = vq->hw;
1757 struct rte_mbuf *rxm;
1758 struct rte_mbuf *prev = NULL;
1759 uint16_t num, nb_rx = 0;
1760 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1761 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1762 uint32_t nb_enqueued = 0;
1763 uint32_t seg_num = 0;
1764 uint32_t seg_res = 0;
1765 uint32_t hdr_size = hw->vtnet_hdr_size;
1769 if (unlikely(hw->started == 0))
1774 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1775 num = VIRTIO_MBUF_BURST_SZ;
1776 if (likely(num > DESC_PER_CACHELINE))
1777 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1779 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1781 for (i = 0; i < num; i++) {
1782 struct virtio_net_hdr_mrg_rxbuf *header;
1784 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1785 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1789 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
1790 PMD_RX_LOG(ERR, "Packet drop");
1792 virtio_discard_rxbuf(vq, rxm);
1793 rxvq->stats.errors++;
1797 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)
1798 rxm->buf_addr + RTE_PKTMBUF_HEADROOM - hdr_size);
1799 seg_num = header->num_buffers;
1804 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1805 rxm->nb_segs = seg_num;
1808 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1809 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1811 rxm->port = rxvq->port_id;
1812 rx_pkts[nb_rx] = rxm;
1815 if (hw->has_rx_offload &&
1816 virtio_rx_offload(rxm, &header->hdr) < 0) {
1817 virtio_discard_rxbuf(vq, rxm);
1818 rxvq->stats.errors++;
1823 rte_vlan_strip(rx_pkts[nb_rx]);
1825 seg_res = seg_num - 1;
1827 /* Merge remaining segments */
1828 while (seg_res != 0 && i < (num - 1)) {
1832 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1833 rxm->pkt_len = (uint32_t)(len[i]);
1834 rxm->data_len = (uint16_t)(len[i]);
1836 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1837 rx_pkts[nb_rx]->data_len += (uint16_t)(len[i]);
1847 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1852 /* Last packet still need merge segments */
1853 while (seg_res != 0) {
1854 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1855 VIRTIO_MBUF_BURST_SZ);
1856 if (likely(vq->vq_free_cnt >= rcv_cnt)) {
1857 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts,
1859 uint16_t extra_idx = 0;
1863 while (extra_idx < rcv_cnt) {
1864 rxm = rcv_pkts[extra_idx];
1867 RTE_PKTMBUF_HEADROOM - hdr_size;
1868 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1869 rxm->data_len = (uint16_t)(len[extra_idx]);
1873 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1874 rx_pkts[nb_rx]->data_len += len[extra_idx];
1879 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1884 "No enough segments for packet.");
1886 virtio_discard_rxbuf(vq, prev);
1887 rxvq->stats.errors++;
1892 rxvq->stats.packets += nb_rx;
1894 /* Allocate new mbuf for the used descriptor */
1895 if (likely(!virtqueue_full(vq))) {
1896 /* free_cnt may include mrg descs */
1897 uint16_t free_cnt = vq->vq_free_cnt;
1898 struct rte_mbuf *new_pkts[free_cnt];
1900 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1901 error = virtqueue_enqueue_recv_refill_packed(vq,
1902 new_pkts, free_cnt);
1903 if (unlikely(error)) {
1904 for (i = 0; i < free_cnt; i++)
1905 rte_pktmbuf_free(new_pkts[i]);
1907 nb_enqueued += free_cnt;
1909 struct rte_eth_dev *dev =
1910 &rte_eth_devices[rxvq->port_id];
1911 dev->data->rx_mbuf_alloc_failed += free_cnt;
1915 if (likely(nb_enqueued)) {
1916 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1917 virtqueue_notify(vq);
1918 PMD_RX_LOG(DEBUG, "Notified");
1926 virtio_xmit_pkts_packed(void *tx_queue, struct rte_mbuf **tx_pkts,
1929 struct virtnet_tx *txvq = tx_queue;
1930 struct virtqueue *vq = txvq->vq;
1931 struct virtio_hw *hw = vq->hw;
1932 uint16_t hdr_size = hw->vtnet_hdr_size;
1934 bool in_order = hw->use_inorder_tx;
1937 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
1940 if (unlikely(nb_pkts < 1))
1943 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
1945 if (nb_pkts > vq->vq_free_cnt)
1946 virtio_xmit_cleanup_packed(vq, nb_pkts - vq->vq_free_cnt,
1949 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1950 struct rte_mbuf *txm = tx_pkts[nb_tx];
1951 int can_push = 0, slots, need;
1953 /* Do VLAN tag insertion */
1954 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
1955 error = rte_vlan_insert(&txm);
1956 if (unlikely(error)) {
1957 rte_pktmbuf_free(txm);
1962 /* optimize ring usage */
1963 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
1964 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
1965 rte_mbuf_refcnt_read(txm) == 1 &&
1966 RTE_MBUF_DIRECT(txm) &&
1967 txm->nb_segs == 1 &&
1968 rte_pktmbuf_headroom(txm) >= hdr_size &&
1969 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
1970 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
1973 /* How many main ring entries are needed to this Tx?
1974 * any_layout => number of segments
1975 * default => number of segments + 1
1977 slots = txm->nb_segs + !can_push;
1978 need = slots - vq->vq_free_cnt;
1980 /* Positive value indicates it need free vring descriptors */
1981 if (unlikely(need > 0)) {
1982 virtio_xmit_cleanup_packed(vq, need, in_order);
1983 need = slots - vq->vq_free_cnt;
1984 if (unlikely(need > 0)) {
1986 "No free tx descriptors to transmit");
1991 /* Enqueue Packet buffers */
1992 virtqueue_enqueue_xmit_packed(txvq, txm, slots, can_push,
1995 virtio_update_packet_stats(&txvq->stats, txm);
1998 txvq->stats.packets += nb_tx;
2000 if (likely(nb_tx)) {
2001 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
2002 virtqueue_notify(vq);
2003 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2011 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
2013 struct virtnet_tx *txvq = tx_queue;
2014 struct virtqueue *vq = txvq->vq;
2015 struct virtio_hw *hw = vq->hw;
2016 uint16_t hdr_size = hw->vtnet_hdr_size;
2017 uint16_t nb_used, nb_tx = 0;
2020 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2023 if (unlikely(nb_pkts < 1))
2026 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2027 nb_used = VIRTQUEUE_NUSED(vq);
2029 virtio_rmb(hw->weak_barriers);
2030 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2031 virtio_xmit_cleanup(vq, nb_used);
2033 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
2034 struct rte_mbuf *txm = tx_pkts[nb_tx];
2035 int can_push = 0, use_indirect = 0, slots, need;
2037 /* Do VLAN tag insertion */
2038 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
2039 error = rte_vlan_insert(&txm);
2040 if (unlikely(error)) {
2041 rte_pktmbuf_free(txm);
2046 /* optimize ring usage */
2047 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2048 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2049 rte_mbuf_refcnt_read(txm) == 1 &&
2050 RTE_MBUF_DIRECT(txm) &&
2051 txm->nb_segs == 1 &&
2052 rte_pktmbuf_headroom(txm) >= hdr_size &&
2053 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2054 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
2056 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
2057 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
2060 /* How many main ring entries are needed to this Tx?
2061 * any_layout => number of segments
2063 * default => number of segments + 1
2065 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
2066 need = slots - vq->vq_free_cnt;
2068 /* Positive value indicates it need free vring descriptors */
2069 if (unlikely(need > 0)) {
2070 nb_used = VIRTQUEUE_NUSED(vq);
2071 virtio_rmb(hw->weak_barriers);
2072 need = RTE_MIN(need, (int)nb_used);
2074 virtio_xmit_cleanup(vq, need);
2075 need = slots - vq->vq_free_cnt;
2076 if (unlikely(need > 0)) {
2078 "No free tx descriptors to transmit");
2083 /* Enqueue Packet buffers */
2084 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect,
2087 virtio_update_packet_stats(&txvq->stats, txm);
2090 txvq->stats.packets += nb_tx;
2092 if (likely(nb_tx)) {
2093 vq_update_avail_idx(vq);
2095 if (unlikely(virtqueue_kick_prepare(vq))) {
2096 virtqueue_notify(vq);
2097 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2105 virtio_xmit_pkts_inorder(void *tx_queue,
2106 struct rte_mbuf **tx_pkts,
2109 struct virtnet_tx *txvq = tx_queue;
2110 struct virtqueue *vq = txvq->vq;
2111 struct virtio_hw *hw = vq->hw;
2112 uint16_t hdr_size = hw->vtnet_hdr_size;
2113 uint16_t nb_used, nb_avail, nb_tx = 0, nb_inorder_pkts = 0;
2114 struct rte_mbuf *inorder_pkts[nb_pkts];
2117 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
2120 if (unlikely(nb_pkts < 1))
2124 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
2125 nb_used = VIRTQUEUE_NUSED(vq);
2127 virtio_rmb(hw->weak_barriers);
2128 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
2129 virtio_xmit_cleanup_inorder(vq, nb_used);
2131 if (unlikely(!vq->vq_free_cnt))
2132 virtio_xmit_cleanup_inorder(vq, nb_used);
2134 nb_avail = RTE_MIN(vq->vq_free_cnt, nb_pkts);
2136 for (nb_tx = 0; nb_tx < nb_avail; nb_tx++) {
2137 struct rte_mbuf *txm = tx_pkts[nb_tx];
2140 /* Do VLAN tag insertion */
2141 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
2142 error = rte_vlan_insert(&txm);
2143 if (unlikely(error)) {
2144 rte_pktmbuf_free(txm);
2149 /* optimize ring usage */
2150 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
2151 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
2152 rte_mbuf_refcnt_read(txm) == 1 &&
2153 RTE_MBUF_DIRECT(txm) &&
2154 txm->nb_segs == 1 &&
2155 rte_pktmbuf_headroom(txm) >= hdr_size &&
2156 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
2157 __alignof__(struct virtio_net_hdr_mrg_rxbuf))) {
2158 inorder_pkts[nb_inorder_pkts] = txm;
2161 virtio_update_packet_stats(&txvq->stats, txm);
2165 if (nb_inorder_pkts) {
2166 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2168 nb_inorder_pkts = 0;
2171 slots = txm->nb_segs + 1;
2172 need = slots - vq->vq_free_cnt;
2173 if (unlikely(need > 0)) {
2174 nb_used = VIRTQUEUE_NUSED(vq);
2175 virtio_rmb(hw->weak_barriers);
2176 need = RTE_MIN(need, (int)nb_used);
2178 virtio_xmit_cleanup_inorder(vq, need);
2180 need = slots - vq->vq_free_cnt;
2182 if (unlikely(need > 0)) {
2184 "No free tx descriptors to transmit");
2188 /* Enqueue Packet buffers */
2189 virtqueue_enqueue_xmit(txvq, txm, slots, 0, 0, 1);
2191 virtio_update_packet_stats(&txvq->stats, txm);
2194 /* Transmit all inorder packets */
2195 if (nb_inorder_pkts)
2196 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2199 txvq->stats.packets += nb_tx;
2201 if (likely(nb_tx)) {
2202 vq_update_avail_idx(vq);
2204 if (unlikely(virtqueue_kick_prepare(vq))) {
2205 virtqueue_notify(vq);
2206 PMD_TX_LOG(DEBUG, "Notified backend after xmit");