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 <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"
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 = virtnet_rxq_to_vq(rxvq);
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 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
95 uint32_t s = mbuf->pkt_len;
96 struct rte_ether_addr *ea;
101 stats->size_bins[1]++;
102 } else if (s > 64 && s < 1024) {
105 /* count zeros, and offset into correct bin */
106 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
107 stats->size_bins[bin]++;
110 stats->size_bins[0]++;
112 stats->size_bins[6]++;
114 stats->size_bins[7]++;
117 ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
118 if (rte_is_multicast_ether_addr(ea)) {
119 if (rte_is_broadcast_ether_addr(ea))
127 virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
129 VIRTIO_DUMP_PACKET(m, m->data_len);
131 virtio_update_packet_stats(&rxvq->stats, m);
135 virtqueue_dequeue_burst_rx_packed(struct virtqueue *vq,
136 struct rte_mbuf **rx_pkts,
140 struct rte_mbuf *cookie;
143 struct vring_packed_desc *desc;
146 desc = vq->vq_packed.ring.desc;
148 for (i = 0; i < num; i++) {
149 used_idx = vq->vq_used_cons_idx;
150 /* desc_is_used has a load-acquire or rte_io_rmb inside
151 * and wait for used desc in virtqueue.
153 if (!desc_is_used(&desc[used_idx], vq))
155 len[i] = desc[used_idx].len;
156 id = desc[used_idx].id;
157 cookie = (struct rte_mbuf *)vq->vq_descx[id].cookie;
158 if (unlikely(cookie == NULL)) {
159 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
160 vq->vq_used_cons_idx);
163 rte_prefetch0(cookie);
164 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
168 vq->vq_used_cons_idx++;
169 if (vq->vq_used_cons_idx >= vq->vq_nentries) {
170 vq->vq_used_cons_idx -= vq->vq_nentries;
171 vq->vq_packed.used_wrap_counter ^= 1;
179 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
180 uint32_t *len, uint16_t num)
182 struct vring_used_elem *uep;
183 struct rte_mbuf *cookie;
184 uint16_t used_idx, desc_idx;
187 /* Caller does the check */
188 for (i = 0; i < num ; i++) {
189 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
190 uep = &vq->vq_split.ring.used->ring[used_idx];
191 desc_idx = (uint16_t) uep->id;
193 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
195 if (unlikely(cookie == NULL)) {
196 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
197 vq->vq_used_cons_idx);
201 rte_prefetch0(cookie);
202 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
204 vq->vq_used_cons_idx++;
205 vq_ring_free_chain(vq, desc_idx);
206 vq->vq_descx[desc_idx].cookie = NULL;
213 virtqueue_dequeue_rx_inorder(struct virtqueue *vq,
214 struct rte_mbuf **rx_pkts,
218 struct vring_used_elem *uep;
219 struct rte_mbuf *cookie;
220 uint16_t used_idx = 0;
223 if (unlikely(num == 0))
226 for (i = 0; i < num; i++) {
227 used_idx = vq->vq_used_cons_idx & (vq->vq_nentries - 1);
228 /* Desc idx same as used idx */
229 uep = &vq->vq_split.ring.used->ring[used_idx];
231 cookie = (struct rte_mbuf *)vq->vq_descx[used_idx].cookie;
233 if (unlikely(cookie == NULL)) {
234 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
235 vq->vq_used_cons_idx);
239 rte_prefetch0(cookie);
240 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
242 vq->vq_used_cons_idx++;
243 vq->vq_descx[used_idx].cookie = NULL;
246 vq_ring_free_inorder(vq, used_idx, i);
251 virtqueue_enqueue_refill_inorder(struct virtqueue *vq,
252 struct rte_mbuf **cookies,
255 struct vq_desc_extra *dxp;
256 struct virtio_hw *hw = vq->hw;
257 struct vring_desc *start_dp;
258 uint16_t head_idx, idx, i = 0;
260 if (unlikely(vq->vq_free_cnt == 0))
262 if (unlikely(vq->vq_free_cnt < num))
265 head_idx = vq->vq_desc_head_idx & (vq->vq_nentries - 1);
266 start_dp = vq->vq_split.ring.desc;
269 idx = head_idx & (vq->vq_nentries - 1);
270 dxp = &vq->vq_descx[idx];
271 dxp->cookie = (void *)cookies[i];
274 start_dp[idx].addr = cookies[i]->buf_iova +
275 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
276 start_dp[idx].len = cookies[i]->buf_len -
277 RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
278 start_dp[idx].flags = VRING_DESC_F_WRITE;
280 vq_update_avail_ring(vq, idx);
285 vq->vq_desc_head_idx += num;
286 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
291 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf **cookie,
294 struct vq_desc_extra *dxp;
295 struct virtio_hw *hw = vq->hw;
296 struct vring_desc *start_dp = vq->vq_split.ring.desc;
299 if (unlikely(vq->vq_free_cnt == 0))
301 if (unlikely(vq->vq_free_cnt < num))
304 if (unlikely(vq->vq_desc_head_idx >= vq->vq_nentries))
307 for (i = 0; i < num; i++) {
308 idx = vq->vq_desc_head_idx;
309 dxp = &vq->vq_descx[idx];
310 dxp->cookie = (void *)cookie[i];
313 start_dp[idx].addr = cookie[i]->buf_iova +
314 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
315 start_dp[idx].len = cookie[i]->buf_len -
316 RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
317 start_dp[idx].flags = VRING_DESC_F_WRITE;
318 vq->vq_desc_head_idx = start_dp[idx].next;
319 vq_update_avail_ring(vq, idx);
320 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END) {
321 vq->vq_desc_tail_idx = vq->vq_desc_head_idx;
326 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
332 virtqueue_refill_single_packed(struct virtqueue *vq,
333 struct vring_packed_desc *dp,
334 struct rte_mbuf *cookie)
336 uint16_t flags = vq->vq_packed.cached_flags;
337 struct virtio_hw *hw = vq->hw;
339 dp->addr = cookie->buf_iova +
340 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
341 dp->len = cookie->buf_len -
342 RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
344 virtqueue_store_flags_packed(dp, flags,
347 if (++vq->vq_avail_idx >= vq->vq_nentries) {
348 vq->vq_avail_idx -= vq->vq_nentries;
349 vq->vq_packed.cached_flags ^=
350 VRING_PACKED_DESC_F_AVAIL_USED;
351 flags = vq->vq_packed.cached_flags;
356 virtqueue_enqueue_recv_refill_packed_init(struct virtqueue *vq,
357 struct rte_mbuf **cookie, uint16_t num)
359 struct vring_packed_desc *start_dp = vq->vq_packed.ring.desc;
360 struct vq_desc_extra *dxp;
364 if (unlikely(vq->vq_free_cnt == 0))
366 if (unlikely(vq->vq_free_cnt < num))
369 for (i = 0; i < num; i++) {
370 idx = vq->vq_avail_idx;
371 dxp = &vq->vq_descx[idx];
372 dxp->cookie = (void *)cookie[i];
375 virtqueue_refill_single_packed(vq, &start_dp[idx], cookie[i]);
377 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
382 virtqueue_enqueue_recv_refill_packed(struct virtqueue *vq,
383 struct rte_mbuf **cookie, uint16_t num)
385 struct vring_packed_desc *start_dp = vq->vq_packed.ring.desc;
386 struct vq_desc_extra *dxp;
390 if (unlikely(vq->vq_free_cnt == 0))
392 if (unlikely(vq->vq_free_cnt < num))
395 for (i = 0; i < num; i++) {
396 idx = vq->vq_avail_idx;
397 did = start_dp[idx].id;
398 dxp = &vq->vq_descx[did];
399 dxp->cookie = (void *)cookie[i];
402 virtqueue_refill_single_packed(vq, &start_dp[idx], cookie[i]);
404 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
408 /* When doing TSO, the IP length is not included in the pseudo header
409 * checksum of the packet given to the PMD, but for virtio it is
413 virtio_tso_fix_cksum(struct rte_mbuf *m)
415 /* common case: header is not fragmented */
416 if (likely(rte_pktmbuf_data_len(m) >= m->l2_len + m->l3_len +
418 struct rte_ipv4_hdr *iph;
419 struct rte_ipv6_hdr *ip6h;
420 struct rte_tcp_hdr *th;
421 uint16_t prev_cksum, new_cksum, ip_len, ip_paylen;
424 iph = rte_pktmbuf_mtod_offset(m,
425 struct rte_ipv4_hdr *, m->l2_len);
426 th = RTE_PTR_ADD(iph, m->l3_len);
427 if ((iph->version_ihl >> 4) == 4) {
428 iph->hdr_checksum = 0;
429 iph->hdr_checksum = rte_ipv4_cksum(iph);
430 ip_len = iph->total_length;
431 ip_paylen = rte_cpu_to_be_16(rte_be_to_cpu_16(ip_len) -
434 ip6h = (struct rte_ipv6_hdr *)iph;
435 ip_paylen = ip6h->payload_len;
438 /* calculate the new phdr checksum not including ip_paylen */
439 prev_cksum = th->cksum;
442 tmp = (tmp & 0xffff) + (tmp >> 16);
445 /* replace it in the packet */
446 th->cksum = new_cksum;
454 virtqueue_enqueue_xmit_inorder(struct virtnet_tx *txvq,
455 struct rte_mbuf **cookies,
458 struct vq_desc_extra *dxp;
459 struct virtqueue *vq = virtnet_txq_to_vq(txvq);
460 struct vring_desc *start_dp;
461 struct virtio_net_hdr *hdr;
463 int16_t head_size = vq->hw->vtnet_hdr_size;
466 idx = vq->vq_desc_head_idx;
467 start_dp = vq->vq_split.ring.desc;
470 idx = idx & (vq->vq_nentries - 1);
471 dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
472 dxp->cookie = (void *)cookies[i];
474 virtio_update_packet_stats(&txvq->stats, cookies[i]);
476 hdr = rte_pktmbuf_mtod_offset(cookies[i],
477 struct virtio_net_hdr *, -head_size);
479 /* if offload disabled, hdr is not zeroed yet, do it now */
480 if (!vq->hw->has_tx_offload)
481 virtqueue_clear_net_hdr(hdr);
483 virtqueue_xmit_offload(hdr, cookies[i]);
485 start_dp[idx].addr = rte_mbuf_data_iova(cookies[i]) - head_size;
486 start_dp[idx].len = cookies[i]->data_len + head_size;
487 start_dp[idx].flags = 0;
490 vq_update_avail_ring(vq, idx);
496 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - num);
497 vq->vq_desc_head_idx = idx & (vq->vq_nentries - 1);
501 virtqueue_enqueue_xmit_packed_fast(struct virtnet_tx *txvq,
502 struct rte_mbuf *cookie,
505 struct virtqueue *vq = virtnet_txq_to_vq(txvq);
506 struct vring_packed_desc *dp;
507 struct vq_desc_extra *dxp;
508 uint16_t idx, id, flags;
509 int16_t head_size = vq->hw->vtnet_hdr_size;
510 struct virtio_net_hdr *hdr;
512 id = in_order ? vq->vq_avail_idx : vq->vq_desc_head_idx;
513 idx = vq->vq_avail_idx;
514 dp = &vq->vq_packed.ring.desc[idx];
516 dxp = &vq->vq_descx[id];
518 dxp->cookie = cookie;
520 flags = vq->vq_packed.cached_flags;
522 /* prepend cannot fail, checked by caller */
523 hdr = rte_pktmbuf_mtod_offset(cookie, struct virtio_net_hdr *,
526 /* if offload disabled, hdr is not zeroed yet, do it now */
527 if (!vq->hw->has_tx_offload)
528 virtqueue_clear_net_hdr(hdr);
530 virtqueue_xmit_offload(hdr, cookie);
532 dp->addr = rte_mbuf_data_iova(cookie) - head_size;
533 dp->len = cookie->data_len + head_size;
536 if (++vq->vq_avail_idx >= vq->vq_nentries) {
537 vq->vq_avail_idx -= vq->vq_nentries;
538 vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
544 vq->vq_desc_head_idx = dxp->next;
545 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
546 vq->vq_desc_tail_idx = VQ_RING_DESC_CHAIN_END;
549 virtqueue_store_flags_packed(dp, flags, vq->hw->weak_barriers);
553 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
554 uint16_t needed, int use_indirect, int can_push,
557 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
558 struct vq_desc_extra *dxp;
559 struct virtqueue *vq = virtnet_txq_to_vq(txvq);
560 struct vring_desc *start_dp;
561 uint16_t seg_num = cookie->nb_segs;
562 uint16_t head_idx, idx;
563 int16_t head_size = vq->hw->vtnet_hdr_size;
564 bool prepend_header = false;
565 struct virtio_net_hdr *hdr;
567 head_idx = vq->vq_desc_head_idx;
570 dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
572 dxp = &vq->vq_descx[idx];
573 dxp->cookie = (void *)cookie;
574 dxp->ndescs = needed;
576 start_dp = vq->vq_split.ring.desc;
579 /* prepend cannot fail, checked by caller */
580 hdr = rte_pktmbuf_mtod_offset(cookie, struct virtio_net_hdr *,
582 prepend_header = true;
584 /* if offload disabled, it is not zeroed below, do it now */
585 if (!vq->hw->has_tx_offload)
586 virtqueue_clear_net_hdr(hdr);
587 } else if (use_indirect) {
588 /* setup tx ring slot to point to indirect
589 * descriptor list stored in reserved region.
591 * the first slot in indirect ring is already preset
592 * to point to the header in reserved region
594 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
595 RTE_PTR_DIFF(&txr[idx].tx_indir, txr);
596 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
597 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
598 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
600 /* loop below will fill in rest of the indirect elements */
601 start_dp = txr[idx].tx_indir;
604 /* setup first tx ring slot to point to header
605 * stored in reserved region.
607 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
608 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
609 start_dp[idx].len = vq->hw->vtnet_hdr_size;
610 start_dp[idx].flags = VRING_DESC_F_NEXT;
611 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
613 idx = start_dp[idx].next;
616 if (vq->hw->has_tx_offload)
617 virtqueue_xmit_offload(hdr, cookie);
620 start_dp[idx].addr = rte_mbuf_data_iova(cookie);
621 start_dp[idx].len = cookie->data_len;
622 if (prepend_header) {
623 start_dp[idx].addr -= head_size;
624 start_dp[idx].len += head_size;
625 prepend_header = false;
627 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
628 idx = start_dp[idx].next;
629 } while ((cookie = cookie->next) != NULL);
632 idx = vq->vq_split.ring.desc[head_idx].next;
634 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
636 vq->vq_desc_head_idx = idx;
637 vq_update_avail_ring(vq, head_idx);
640 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
641 vq->vq_desc_tail_idx = idx;
646 virtio_dev_cq_start(struct rte_eth_dev *dev)
648 struct virtio_hw *hw = dev->data->dev_private;
651 rte_spinlock_init(&hw->cvq->lock);
652 VIRTQUEUE_DUMP(virtnet_cq_to_vq(hw->cvq));
657 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
660 unsigned int socket_id __rte_unused,
661 const struct rte_eth_rxconf *rx_conf,
662 struct rte_mempool *mp)
664 uint16_t vq_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
665 struct virtio_hw *hw = dev->data->dev_private;
666 struct virtqueue *vq = hw->vqs[vq_idx];
667 struct virtnet_rx *rxvq;
668 uint16_t rx_free_thresh;
672 PMD_INIT_FUNC_TRACE();
674 if (rx_conf->rx_deferred_start) {
675 PMD_INIT_LOG(ERR, "Rx deferred start is not supported");
679 buf_size = virtio_rx_mem_pool_buf_size(mp);
680 if (!virtio_rx_check_scatter(hw->max_rx_pkt_len, buf_size,
681 hw->rx_ol_scatter, &error)) {
682 PMD_INIT_LOG(ERR, "RxQ %u Rx scatter check failed: %s",
687 rx_free_thresh = rx_conf->rx_free_thresh;
688 if (rx_free_thresh == 0)
690 RTE_MIN(vq->vq_nentries / 4, DEFAULT_RX_FREE_THRESH);
692 if (rx_free_thresh & 0x3) {
693 RTE_LOG(ERR, PMD, "rx_free_thresh must be multiples of four."
694 " (rx_free_thresh=%u port=%u queue=%u)\n",
695 rx_free_thresh, dev->data->port_id, queue_idx);
699 if (rx_free_thresh >= vq->vq_nentries) {
700 RTE_LOG(ERR, PMD, "rx_free_thresh must be less than the "
701 "number of RX entries (%u)."
702 " (rx_free_thresh=%u port=%u queue=%u)\n",
704 rx_free_thresh, dev->data->port_id, queue_idx);
707 vq->vq_free_thresh = rx_free_thresh;
709 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
710 nb_desc = vq->vq_nentries;
711 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
714 rxvq->queue_id = queue_idx;
716 dev->data->rx_queues[queue_idx] = rxvq;
722 virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
724 uint16_t vq_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
725 struct virtio_hw *hw = dev->data->dev_private;
726 struct virtqueue *vq = hw->vqs[vq_idx];
727 struct virtnet_rx *rxvq = &vq->rxq;
731 bool in_order = virtio_with_feature(hw, VIRTIO_F_IN_ORDER);
733 PMD_INIT_FUNC_TRACE();
735 /* Allocate blank mbufs for the each rx descriptor */
738 if (hw->use_vec_rx && !virtio_with_packed_queue(hw)) {
739 for (desc_idx = 0; desc_idx < vq->vq_nentries;
741 vq->vq_split.ring.avail->ring[desc_idx] = desc_idx;
742 vq->vq_split.ring.desc[desc_idx].flags =
746 virtio_rxq_vec_setup(rxvq);
749 memset(rxvq->fake_mbuf, 0, sizeof(*rxvq->fake_mbuf));
750 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST; desc_idx++)
751 vq->sw_ring[vq->vq_nentries + desc_idx] = rxvq->fake_mbuf;
753 if (hw->use_vec_rx && !virtio_with_packed_queue(hw)) {
754 while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
755 virtio_rxq_rearm_vec(rxvq);
756 nbufs += RTE_VIRTIO_VPMD_RX_REARM_THRESH;
758 } else if (!virtio_with_packed_queue(vq->hw) && in_order) {
759 if ((!virtqueue_full(vq))) {
760 uint16_t free_cnt = vq->vq_free_cnt;
761 struct rte_mbuf *pkts[free_cnt];
763 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, pkts,
765 error = virtqueue_enqueue_refill_inorder(vq,
768 if (unlikely(error)) {
769 for (i = 0; i < free_cnt; i++)
770 rte_pktmbuf_free(pkts[i]);
775 vq_update_avail_idx(vq);
778 while (!virtqueue_full(vq)) {
779 m = rte_mbuf_raw_alloc(rxvq->mpool);
783 /* Enqueue allocated buffers */
784 if (virtio_with_packed_queue(vq->hw))
785 error = virtqueue_enqueue_recv_refill_packed_init(vq,
788 error = virtqueue_enqueue_recv_refill(vq,
797 if (!virtio_with_packed_queue(vq->hw))
798 vq_update_avail_idx(vq);
801 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
809 * struct rte_eth_dev *dev: Used to update dev
810 * uint16_t nb_desc: Defaults to values read from config space
811 * unsigned int socket_id: Used to allocate memzone
812 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
813 * uint16_t queue_idx: Just used as an index in dev txq list
816 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
819 unsigned int socket_id __rte_unused,
820 const struct rte_eth_txconf *tx_conf)
822 uint8_t vq_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
823 struct virtio_hw *hw = dev->data->dev_private;
824 struct virtqueue *vq = hw->vqs[vq_idx];
825 struct virtnet_tx *txvq;
826 uint16_t tx_free_thresh;
828 PMD_INIT_FUNC_TRACE();
830 if (tx_conf->tx_deferred_start) {
831 PMD_INIT_LOG(ERR, "Tx deferred start is not supported");
835 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
836 nb_desc = vq->vq_nentries;
837 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
840 txvq->queue_id = queue_idx;
842 tx_free_thresh = tx_conf->tx_free_thresh;
843 if (tx_free_thresh == 0)
845 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
847 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
848 PMD_DRV_LOG(ERR, "tx_free_thresh must be less than the "
849 "number of TX entries minus 3 (%u)."
850 " (tx_free_thresh=%u port=%u queue=%u)\n",
852 tx_free_thresh, dev->data->port_id, queue_idx);
856 vq->vq_free_thresh = tx_free_thresh;
858 dev->data->tx_queues[queue_idx] = txvq;
863 virtio_dev_tx_queue_setup_finish(struct rte_eth_dev *dev,
866 uint8_t vq_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
867 struct virtio_hw *hw = dev->data->dev_private;
868 struct virtqueue *vq = hw->vqs[vq_idx];
870 PMD_INIT_FUNC_TRACE();
872 if (!virtio_with_packed_queue(hw)) {
873 if (virtio_with_feature(hw, VIRTIO_F_IN_ORDER))
874 vq->vq_split.ring.desc[vq->vq_nentries - 1].next = 0;
883 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
887 * Requeue the discarded mbuf. This should always be
888 * successful since it was just dequeued.
890 if (virtio_with_packed_queue(vq->hw))
891 error = virtqueue_enqueue_recv_refill_packed(vq, &m, 1);
893 error = virtqueue_enqueue_recv_refill(vq, &m, 1);
895 if (unlikely(error)) {
896 PMD_DRV_LOG(ERR, "cannot requeue discarded mbuf");
902 virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)
906 error = virtqueue_enqueue_refill_inorder(vq, &m, 1);
907 if (unlikely(error)) {
908 PMD_DRV_LOG(ERR, "cannot requeue discarded mbuf");
913 /* Optionally fill offload information in structure */
915 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
917 struct rte_net_hdr_lens hdr_lens;
918 uint32_t hdrlen, ptype;
919 int l4_supported = 0;
922 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
925 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
927 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
928 m->packet_type = ptype;
929 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
930 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
931 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
934 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
935 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
936 if (hdr->csum_start <= hdrlen && l4_supported) {
937 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
939 /* Unknown proto or tunnel, do sw cksum. We can assume
940 * the cksum field is in the first segment since the
941 * buffers we provided to the host are large enough.
942 * In case of SCTP, this will be wrong since it's a CRC
943 * but there's nothing we can do.
945 uint16_t csum = 0, off;
947 if (rte_raw_cksum_mbuf(m, hdr->csum_start,
948 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
951 if (likely(csum != 0xffff))
953 off = hdr->csum_offset + hdr->csum_start;
954 if (rte_pktmbuf_data_len(m) >= off + 1)
955 *rte_pktmbuf_mtod_offset(m, uint16_t *,
958 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
959 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
962 /* GSO request, save required information in mbuf */
963 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
964 /* Check unsupported modes */
965 if ((hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) ||
966 (hdr->gso_size == 0)) {
970 /* Update mss lengthes in mbuf */
971 m->tso_segsz = hdr->gso_size;
972 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
973 case VIRTIO_NET_HDR_GSO_TCPV4:
974 case VIRTIO_NET_HDR_GSO_TCPV6:
975 m->ol_flags |= PKT_RX_LRO | \
976 PKT_RX_L4_CKSUM_NONE;
986 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
988 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
990 struct virtnet_rx *rxvq = rx_queue;
991 struct virtqueue *vq = virtnet_rxq_to_vq(rxvq);
992 struct virtio_hw *hw = vq->hw;
993 struct rte_mbuf *rxm;
994 uint16_t nb_used, num, nb_rx;
995 uint32_t len[VIRTIO_MBUF_BURST_SZ];
996 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
998 uint32_t i, nb_enqueued;
1000 struct virtio_net_hdr *hdr;
1003 if (unlikely(hw->started == 0))
1006 nb_used = virtqueue_nused(vq);
1008 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1009 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1010 num = VIRTIO_MBUF_BURST_SZ;
1011 if (likely(num > DESC_PER_CACHELINE))
1012 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1014 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1015 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
1018 hdr_size = hw->vtnet_hdr_size;
1020 for (i = 0; i < num ; i++) {
1023 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1025 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1026 PMD_RX_LOG(ERR, "Packet drop");
1028 virtio_discard_rxbuf(vq, rxm);
1029 rxvq->stats.errors++;
1033 rxm->port = rxvq->port_id;
1034 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1038 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1039 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1041 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1042 RTE_PKTMBUF_HEADROOM - hdr_size);
1045 rte_vlan_strip(rxm);
1047 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1048 virtio_discard_rxbuf(vq, rxm);
1049 rxvq->stats.errors++;
1053 virtio_rx_stats_updated(rxvq, rxm);
1055 rx_pkts[nb_rx++] = rxm;
1058 rxvq->stats.packets += nb_rx;
1060 /* Allocate new mbuf for the used descriptor */
1061 if (likely(!virtqueue_full(vq))) {
1062 uint16_t free_cnt = vq->vq_free_cnt;
1063 struct rte_mbuf *new_pkts[free_cnt];
1065 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1067 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1069 if (unlikely(error)) {
1070 for (i = 0; i < free_cnt; i++)
1071 rte_pktmbuf_free(new_pkts[i]);
1073 nb_enqueued += free_cnt;
1075 struct rte_eth_dev *dev =
1076 &rte_eth_devices[rxvq->port_id];
1077 dev->data->rx_mbuf_alloc_failed += free_cnt;
1081 if (likely(nb_enqueued)) {
1082 vq_update_avail_idx(vq);
1084 if (unlikely(virtqueue_kick_prepare(vq))) {
1085 virtqueue_notify(vq);
1086 PMD_RX_LOG(DEBUG, "Notified");
1094 virtio_recv_pkts_packed(void *rx_queue, struct rte_mbuf **rx_pkts,
1097 struct virtnet_rx *rxvq = rx_queue;
1098 struct virtqueue *vq = virtnet_rxq_to_vq(rxvq);
1099 struct virtio_hw *hw = vq->hw;
1100 struct rte_mbuf *rxm;
1101 uint16_t num, nb_rx;
1102 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1103 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1105 uint32_t i, nb_enqueued;
1107 struct virtio_net_hdr *hdr;
1110 if (unlikely(hw->started == 0))
1113 num = RTE_MIN(VIRTIO_MBUF_BURST_SZ, nb_pkts);
1114 if (likely(num > DESC_PER_CACHELINE))
1115 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1117 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1118 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1121 hdr_size = hw->vtnet_hdr_size;
1123 for (i = 0; i < num; i++) {
1126 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1128 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1129 PMD_RX_LOG(ERR, "Packet drop");
1131 virtio_discard_rxbuf(vq, rxm);
1132 rxvq->stats.errors++;
1136 rxm->port = rxvq->port_id;
1137 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1141 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1142 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1144 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
1145 RTE_PKTMBUF_HEADROOM - hdr_size);
1148 rte_vlan_strip(rxm);
1150 if (hw->has_rx_offload && virtio_rx_offload(rxm, hdr) < 0) {
1151 virtio_discard_rxbuf(vq, rxm);
1152 rxvq->stats.errors++;
1156 virtio_rx_stats_updated(rxvq, rxm);
1158 rx_pkts[nb_rx++] = rxm;
1161 rxvq->stats.packets += nb_rx;
1163 /* Allocate new mbuf for the used descriptor */
1164 if (likely(!virtqueue_full(vq))) {
1165 uint16_t free_cnt = vq->vq_free_cnt;
1166 struct rte_mbuf *new_pkts[free_cnt];
1168 if (likely(rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts,
1170 error = virtqueue_enqueue_recv_refill_packed(vq,
1171 new_pkts, free_cnt);
1172 if (unlikely(error)) {
1173 for (i = 0; i < free_cnt; i++)
1174 rte_pktmbuf_free(new_pkts[i]);
1176 nb_enqueued += free_cnt;
1178 struct rte_eth_dev *dev =
1179 &rte_eth_devices[rxvq->port_id];
1180 dev->data->rx_mbuf_alloc_failed += free_cnt;
1184 if (likely(nb_enqueued)) {
1185 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1186 virtqueue_notify(vq);
1187 PMD_RX_LOG(DEBUG, "Notified");
1196 virtio_recv_pkts_inorder(void *rx_queue,
1197 struct rte_mbuf **rx_pkts,
1200 struct virtnet_rx *rxvq = rx_queue;
1201 struct virtqueue *vq = virtnet_rxq_to_vq(rxvq);
1202 struct virtio_hw *hw = vq->hw;
1203 struct rte_mbuf *rxm;
1204 struct rte_mbuf *prev = NULL;
1205 uint16_t nb_used, num, nb_rx;
1206 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1207 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1209 uint32_t nb_enqueued;
1216 if (unlikely(hw->started == 0))
1219 nb_used = virtqueue_nused(vq);
1220 nb_used = RTE_MIN(nb_used, nb_pkts);
1221 nb_used = RTE_MIN(nb_used, VIRTIO_MBUF_BURST_SZ);
1223 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1228 hdr_size = hw->vtnet_hdr_size;
1230 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len, nb_used);
1232 for (i = 0; i < num; i++) {
1233 struct virtio_net_hdr_mrg_rxbuf *header;
1235 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1236 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1240 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1241 PMD_RX_LOG(ERR, "Packet drop");
1243 virtio_discard_rxbuf_inorder(vq, rxm);
1244 rxvq->stats.errors++;
1248 header = (struct virtio_net_hdr_mrg_rxbuf *)
1249 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1252 if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1253 seg_num = header->num_buffers;
1260 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1261 rxm->nb_segs = seg_num;
1264 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1265 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1267 rxm->port = rxvq->port_id;
1269 rx_pkts[nb_rx] = rxm;
1272 if (vq->hw->has_rx_offload &&
1273 virtio_rx_offload(rxm, &header->hdr) < 0) {
1274 virtio_discard_rxbuf_inorder(vq, rxm);
1275 rxvq->stats.errors++;
1280 rte_vlan_strip(rx_pkts[nb_rx]);
1282 seg_res = seg_num - 1;
1284 /* Merge remaining segments */
1285 while (seg_res != 0 && i < (num - 1)) {
1289 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1290 rxm->pkt_len = (uint32_t)(len[i]);
1291 rxm->data_len = (uint16_t)(len[i]);
1293 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1301 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1306 /* Last packet still need merge segments */
1307 while (seg_res != 0) {
1308 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1309 VIRTIO_MBUF_BURST_SZ);
1311 if (likely(virtqueue_nused(vq) >= rcv_cnt)) {
1312 num = virtqueue_dequeue_rx_inorder(vq, rcv_pkts, len,
1314 uint16_t extra_idx = 0;
1317 while (extra_idx < rcv_cnt) {
1318 rxm = rcv_pkts[extra_idx];
1320 RTE_PKTMBUF_HEADROOM - hdr_size;
1321 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1322 rxm->data_len = (uint16_t)(len[extra_idx]);
1325 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1331 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1336 "No enough segments for packet.");
1337 rte_pktmbuf_free(rx_pkts[nb_rx]);
1338 rxvq->stats.errors++;
1343 rxvq->stats.packets += nb_rx;
1345 /* Allocate new mbuf for the used descriptor */
1347 if (likely(!virtqueue_full(vq))) {
1348 /* free_cnt may include mrg descs */
1349 uint16_t free_cnt = vq->vq_free_cnt;
1350 struct rte_mbuf *new_pkts[free_cnt];
1352 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1353 error = virtqueue_enqueue_refill_inorder(vq, new_pkts,
1355 if (unlikely(error)) {
1356 for (i = 0; i < free_cnt; i++)
1357 rte_pktmbuf_free(new_pkts[i]);
1359 nb_enqueued += free_cnt;
1361 struct rte_eth_dev *dev =
1362 &rte_eth_devices[rxvq->port_id];
1363 dev->data->rx_mbuf_alloc_failed += free_cnt;
1367 if (likely(nb_enqueued)) {
1368 vq_update_avail_idx(vq);
1370 if (unlikely(virtqueue_kick_prepare(vq))) {
1371 virtqueue_notify(vq);
1372 PMD_RX_LOG(DEBUG, "Notified");
1380 virtio_recv_mergeable_pkts(void *rx_queue,
1381 struct rte_mbuf **rx_pkts,
1384 struct virtnet_rx *rxvq = rx_queue;
1385 struct virtqueue *vq = virtnet_rxq_to_vq(rxvq);
1386 struct virtio_hw *hw = vq->hw;
1387 struct rte_mbuf *rxm;
1388 struct rte_mbuf *prev = NULL;
1389 uint16_t nb_used, num, nb_rx = 0;
1390 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1391 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1393 uint32_t nb_enqueued = 0;
1394 uint32_t seg_num = 0;
1395 uint32_t seg_res = 0;
1396 uint32_t hdr_size = hw->vtnet_hdr_size;
1399 if (unlikely(hw->started == 0))
1402 nb_used = virtqueue_nused(vq);
1404 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
1406 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
1407 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1408 num = VIRTIO_MBUF_BURST_SZ;
1409 if (likely(num > DESC_PER_CACHELINE))
1410 num = num - ((vq->vq_used_cons_idx + num) %
1411 DESC_PER_CACHELINE);
1414 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
1416 for (i = 0; i < num; i++) {
1417 struct virtio_net_hdr_mrg_rxbuf *header;
1419 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1420 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1424 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1425 PMD_RX_LOG(ERR, "Packet drop");
1427 virtio_discard_rxbuf(vq, rxm);
1428 rxvq->stats.errors++;
1432 header = (struct virtio_net_hdr_mrg_rxbuf *)
1433 ((char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM
1435 seg_num = header->num_buffers;
1439 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1440 rxm->nb_segs = seg_num;
1443 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1444 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1446 rxm->port = rxvq->port_id;
1448 rx_pkts[nb_rx] = rxm;
1451 if (hw->has_rx_offload &&
1452 virtio_rx_offload(rxm, &header->hdr) < 0) {
1453 virtio_discard_rxbuf(vq, rxm);
1454 rxvq->stats.errors++;
1459 rte_vlan_strip(rx_pkts[nb_rx]);
1461 seg_res = seg_num - 1;
1463 /* Merge remaining segments */
1464 while (seg_res != 0 && i < (num - 1)) {
1468 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1469 rxm->pkt_len = (uint32_t)(len[i]);
1470 rxm->data_len = (uint16_t)(len[i]);
1472 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1480 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1485 /* Last packet still need merge segments */
1486 while (seg_res != 0) {
1487 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1488 VIRTIO_MBUF_BURST_SZ);
1490 if (likely(virtqueue_nused(vq) >= rcv_cnt)) {
1491 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len,
1493 uint16_t extra_idx = 0;
1496 while (extra_idx < rcv_cnt) {
1497 rxm = rcv_pkts[extra_idx];
1499 RTE_PKTMBUF_HEADROOM - hdr_size;
1500 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1501 rxm->data_len = (uint16_t)(len[extra_idx]);
1504 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1510 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1515 "No enough segments for packet.");
1516 rte_pktmbuf_free(rx_pkts[nb_rx]);
1517 rxvq->stats.errors++;
1522 rxvq->stats.packets += nb_rx;
1524 /* Allocate new mbuf for the used descriptor */
1525 if (likely(!virtqueue_full(vq))) {
1526 /* free_cnt may include mrg descs */
1527 uint16_t free_cnt = vq->vq_free_cnt;
1528 struct rte_mbuf *new_pkts[free_cnt];
1530 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1531 error = virtqueue_enqueue_recv_refill(vq, new_pkts,
1533 if (unlikely(error)) {
1534 for (i = 0; i < free_cnt; i++)
1535 rte_pktmbuf_free(new_pkts[i]);
1537 nb_enqueued += free_cnt;
1539 struct rte_eth_dev *dev =
1540 &rte_eth_devices[rxvq->port_id];
1541 dev->data->rx_mbuf_alloc_failed += free_cnt;
1545 if (likely(nb_enqueued)) {
1546 vq_update_avail_idx(vq);
1548 if (unlikely(virtqueue_kick_prepare(vq))) {
1549 virtqueue_notify(vq);
1550 PMD_RX_LOG(DEBUG, "Notified");
1558 virtio_recv_mergeable_pkts_packed(void *rx_queue,
1559 struct rte_mbuf **rx_pkts,
1562 struct virtnet_rx *rxvq = rx_queue;
1563 struct virtqueue *vq = virtnet_rxq_to_vq(rxvq);
1564 struct virtio_hw *hw = vq->hw;
1565 struct rte_mbuf *rxm;
1566 struct rte_mbuf *prev = NULL;
1567 uint16_t num, nb_rx = 0;
1568 uint32_t len[VIRTIO_MBUF_BURST_SZ];
1569 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
1570 uint32_t nb_enqueued = 0;
1571 uint32_t seg_num = 0;
1572 uint32_t seg_res = 0;
1573 uint32_t hdr_size = hw->vtnet_hdr_size;
1577 if (unlikely(hw->started == 0))
1582 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
1583 num = VIRTIO_MBUF_BURST_SZ;
1584 if (likely(num > DESC_PER_CACHELINE))
1585 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
1587 num = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts, len, num);
1589 for (i = 0; i < num; i++) {
1590 struct virtio_net_hdr_mrg_rxbuf *header;
1592 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
1593 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
1597 if (unlikely(len[i] < hdr_size + RTE_ETHER_HDR_LEN)) {
1598 PMD_RX_LOG(ERR, "Packet drop");
1600 virtio_discard_rxbuf(vq, rxm);
1601 rxvq->stats.errors++;
1605 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)
1606 rxm->buf_addr + RTE_PKTMBUF_HEADROOM - hdr_size);
1607 seg_num = header->num_buffers;
1612 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1613 rxm->nb_segs = seg_num;
1616 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
1617 rxm->data_len = (uint16_t)(len[i] - hdr_size);
1619 rxm->port = rxvq->port_id;
1620 rx_pkts[nb_rx] = rxm;
1623 if (hw->has_rx_offload &&
1624 virtio_rx_offload(rxm, &header->hdr) < 0) {
1625 virtio_discard_rxbuf(vq, rxm);
1626 rxvq->stats.errors++;
1631 rte_vlan_strip(rx_pkts[nb_rx]);
1633 seg_res = seg_num - 1;
1635 /* Merge remaining segments */
1636 while (seg_res != 0 && i < (num - 1)) {
1640 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1641 rxm->pkt_len = (uint32_t)(len[i]);
1642 rxm->data_len = (uint16_t)(len[i]);
1644 rx_pkts[nb_rx]->pkt_len += (uint32_t)(len[i]);
1652 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1657 /* Last packet still need merge segments */
1658 while (seg_res != 0) {
1659 uint16_t rcv_cnt = RTE_MIN((uint16_t)seg_res,
1660 VIRTIO_MBUF_BURST_SZ);
1661 uint16_t extra_idx = 0;
1663 rcv_cnt = virtqueue_dequeue_burst_rx_packed(vq, rcv_pkts,
1665 if (unlikely(rcv_cnt == 0)) {
1666 PMD_RX_LOG(ERR, "No enough segments for packet.");
1667 rte_pktmbuf_free(rx_pkts[nb_rx]);
1668 rxvq->stats.errors++;
1672 while (extra_idx < rcv_cnt) {
1673 rxm = rcv_pkts[extra_idx];
1675 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
1676 rxm->pkt_len = (uint32_t)(len[extra_idx]);
1677 rxm->data_len = (uint16_t)(len[extra_idx]);
1681 rx_pkts[nb_rx]->pkt_len += len[extra_idx];
1686 virtio_rx_stats_updated(rxvq, rx_pkts[nb_rx]);
1691 rxvq->stats.packets += nb_rx;
1693 /* Allocate new mbuf for the used descriptor */
1694 if (likely(!virtqueue_full(vq))) {
1695 /* free_cnt may include mrg descs */
1696 uint16_t free_cnt = vq->vq_free_cnt;
1697 struct rte_mbuf *new_pkts[free_cnt];
1699 if (!rte_pktmbuf_alloc_bulk(rxvq->mpool, new_pkts, free_cnt)) {
1700 error = virtqueue_enqueue_recv_refill_packed(vq,
1701 new_pkts, free_cnt);
1702 if (unlikely(error)) {
1703 for (i = 0; i < free_cnt; i++)
1704 rte_pktmbuf_free(new_pkts[i]);
1706 nb_enqueued += free_cnt;
1708 struct rte_eth_dev *dev =
1709 &rte_eth_devices[rxvq->port_id];
1710 dev->data->rx_mbuf_alloc_failed += free_cnt;
1714 if (likely(nb_enqueued)) {
1715 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1716 virtqueue_notify(vq);
1717 PMD_RX_LOG(DEBUG, "Notified");
1725 virtio_xmit_pkts_prepare(void *tx_queue __rte_unused, struct rte_mbuf **tx_pkts,
1731 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1732 struct rte_mbuf *m = tx_pkts[nb_tx];
1734 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1735 error = rte_validate_tx_offload(m);
1736 if (unlikely(error)) {
1742 /* Do VLAN tag insertion */
1743 if (unlikely(m->ol_flags & PKT_TX_VLAN_PKT)) {
1744 error = rte_vlan_insert(&m);
1745 /* rte_vlan_insert() may change pointer
1746 * even in the case of failure
1750 if (unlikely(error)) {
1756 error = rte_net_intel_cksum_prepare(m);
1757 if (unlikely(error)) {
1762 if (m->ol_flags & PKT_TX_TCP_SEG)
1763 virtio_tso_fix_cksum(m);
1770 virtio_xmit_pkts_packed(void *tx_queue, struct rte_mbuf **tx_pkts,
1773 struct virtnet_tx *txvq = tx_queue;
1774 struct virtqueue *vq = virtnet_txq_to_vq(txvq);
1775 struct virtio_hw *hw = vq->hw;
1776 uint16_t hdr_size = hw->vtnet_hdr_size;
1778 bool in_order = virtio_with_feature(hw, VIRTIO_F_IN_ORDER);
1780 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
1783 if (unlikely(nb_pkts < 1))
1786 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
1788 if (nb_pkts > vq->vq_free_cnt)
1789 virtio_xmit_cleanup_packed(vq, nb_pkts - vq->vq_free_cnt,
1792 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1793 struct rte_mbuf *txm = tx_pkts[nb_tx];
1794 int can_push = 0, use_indirect = 0, slots, need;
1796 /* optimize ring usage */
1797 if ((virtio_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
1798 virtio_with_feature(hw, VIRTIO_F_VERSION_1)) &&
1799 rte_mbuf_refcnt_read(txm) == 1 &&
1800 RTE_MBUF_DIRECT(txm) &&
1801 txm->nb_segs == 1 &&
1802 rte_pktmbuf_headroom(txm) >= hdr_size &&
1803 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
1804 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
1806 else if (virtio_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
1807 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
1809 /* How many main ring entries are needed to this Tx?
1811 * any_layout => number of segments
1812 * default => number of segments + 1
1814 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
1815 need = slots - vq->vq_free_cnt;
1817 /* Positive value indicates it need free vring descriptors */
1818 if (unlikely(need > 0)) {
1819 virtio_xmit_cleanup_packed(vq, need, in_order);
1820 need = slots - vq->vq_free_cnt;
1821 if (unlikely(need > 0)) {
1823 "No free tx descriptors to transmit");
1828 /* Enqueue Packet buffers */
1830 virtqueue_enqueue_xmit_packed_fast(txvq, txm, in_order);
1832 virtqueue_enqueue_xmit_packed(txvq, txm, slots,
1836 virtio_update_packet_stats(&txvq->stats, txm);
1839 txvq->stats.packets += nb_tx;
1841 if (likely(nb_tx)) {
1842 if (unlikely(virtqueue_kick_prepare_packed(vq))) {
1843 virtqueue_notify(vq);
1844 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
1852 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1854 struct virtnet_tx *txvq = tx_queue;
1855 struct virtqueue *vq = virtnet_txq_to_vq(txvq);
1856 struct virtio_hw *hw = vq->hw;
1857 uint16_t hdr_size = hw->vtnet_hdr_size;
1858 uint16_t nb_used, nb_tx = 0;
1860 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
1863 if (unlikely(nb_pkts < 1))
1866 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
1868 nb_used = virtqueue_nused(vq);
1870 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
1871 virtio_xmit_cleanup(vq, nb_used);
1873 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1874 struct rte_mbuf *txm = tx_pkts[nb_tx];
1875 int can_push = 0, use_indirect = 0, slots, need;
1877 /* optimize ring usage */
1878 if ((virtio_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
1879 virtio_with_feature(hw, VIRTIO_F_VERSION_1)) &&
1880 rte_mbuf_refcnt_read(txm) == 1 &&
1881 RTE_MBUF_DIRECT(txm) &&
1882 txm->nb_segs == 1 &&
1883 rte_pktmbuf_headroom(txm) >= hdr_size &&
1884 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
1885 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
1887 else if (virtio_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
1888 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
1891 /* How many main ring entries are needed to this Tx?
1892 * any_layout => number of segments
1894 * default => number of segments + 1
1896 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
1897 need = slots - vq->vq_free_cnt;
1899 /* Positive value indicates it need free vring descriptors */
1900 if (unlikely(need > 0)) {
1901 nb_used = virtqueue_nused(vq);
1903 need = RTE_MIN(need, (int)nb_used);
1905 virtio_xmit_cleanup(vq, need);
1906 need = slots - vq->vq_free_cnt;
1907 if (unlikely(need > 0)) {
1909 "No free tx descriptors to transmit");
1914 /* Enqueue Packet buffers */
1915 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect,
1918 virtio_update_packet_stats(&txvq->stats, txm);
1921 txvq->stats.packets += nb_tx;
1923 if (likely(nb_tx)) {
1924 vq_update_avail_idx(vq);
1926 if (unlikely(virtqueue_kick_prepare(vq))) {
1927 virtqueue_notify(vq);
1928 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
1935 static __rte_always_inline int
1936 virtio_xmit_try_cleanup_inorder(struct virtqueue *vq, uint16_t need)
1938 uint16_t nb_used, nb_clean, nb_descs;
1940 nb_descs = vq->vq_free_cnt + need;
1941 nb_used = virtqueue_nused(vq);
1942 nb_clean = RTE_MIN(need, (int)nb_used);
1944 virtio_xmit_cleanup_inorder(vq, nb_clean);
1946 return nb_descs - vq->vq_free_cnt;
1950 virtio_xmit_pkts_inorder(void *tx_queue,
1951 struct rte_mbuf **tx_pkts,
1954 struct virtnet_tx *txvq = tx_queue;
1955 struct virtqueue *vq = virtnet_txq_to_vq(txvq);
1956 struct virtio_hw *hw = vq->hw;
1957 uint16_t hdr_size = hw->vtnet_hdr_size;
1958 uint16_t nb_used, nb_tx = 0, nb_inorder_pkts = 0;
1959 struct rte_mbuf *inorder_pkts[nb_pkts];
1962 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
1965 if (unlikely(nb_pkts < 1))
1969 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
1970 nb_used = virtqueue_nused(vq);
1972 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
1973 virtio_xmit_cleanup_inorder(vq, nb_used);
1975 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1976 struct rte_mbuf *txm = tx_pkts[nb_tx];
1979 /* optimize ring usage */
1980 if ((virtio_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
1981 virtio_with_feature(hw, VIRTIO_F_VERSION_1)) &&
1982 rte_mbuf_refcnt_read(txm) == 1 &&
1983 RTE_MBUF_DIRECT(txm) &&
1984 txm->nb_segs == 1 &&
1985 rte_pktmbuf_headroom(txm) >= hdr_size &&
1986 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
1987 __alignof__(struct virtio_net_hdr_mrg_rxbuf))) {
1988 inorder_pkts[nb_inorder_pkts] = txm;
1994 if (nb_inorder_pkts) {
1995 need = nb_inorder_pkts - vq->vq_free_cnt;
1996 if (unlikely(need > 0)) {
1997 need = virtio_xmit_try_cleanup_inorder(vq,
1999 if (unlikely(need > 0)) {
2001 "No free tx descriptors to "
2006 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2008 nb_inorder_pkts = 0;
2011 slots = txm->nb_segs + 1;
2012 need = slots - vq->vq_free_cnt;
2013 if (unlikely(need > 0)) {
2014 need = virtio_xmit_try_cleanup_inorder(vq, slots);
2016 if (unlikely(need > 0)) {
2018 "No free tx descriptors to transmit");
2022 /* Enqueue Packet buffers */
2023 virtqueue_enqueue_xmit(txvq, txm, slots, 0, 0, 1);
2025 virtio_update_packet_stats(&txvq->stats, txm);
2028 /* Transmit all inorder packets */
2029 if (nb_inorder_pkts) {
2030 need = nb_inorder_pkts - vq->vq_free_cnt;
2031 if (unlikely(need > 0)) {
2032 need = virtio_xmit_try_cleanup_inorder(vq,
2034 if (unlikely(need > 0)) {
2036 "No free tx descriptors to transmit");
2037 nb_inorder_pkts = vq->vq_free_cnt;
2042 virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
2046 txvq->stats.packets += nb_tx;
2048 if (likely(nb_tx)) {
2049 vq_update_avail_idx(vq);
2051 if (unlikely(virtqueue_kick_prepare(vq))) {
2052 virtqueue_notify(vq);
2053 PMD_TX_LOG(DEBUG, "Notified backend after xmit");
2063 virtio_recv_pkts_packed_vec(void *rx_queue __rte_unused,
2064 struct rte_mbuf **rx_pkts __rte_unused,
2065 uint16_t nb_pkts __rte_unused)
2071 virtio_xmit_pkts_packed_vec(void *tx_queue __rte_unused,
2072 struct rte_mbuf **tx_pkts __rte_unused,
2073 uint16_t nb_pkts __rte_unused)