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"
35 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
36 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
38 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0)
42 virtio_dev_rx_queue_done(void *rxq, uint16_t offset)
44 struct virtnet_rx *rxvq = rxq;
45 struct virtqueue *vq = rxvq->vq;
47 return VIRTQUEUE_NUSED(vq) >= offset;
51 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
53 struct vring_desc *dp, *dp_tail;
54 struct vq_desc_extra *dxp;
55 uint16_t desc_idx_last = desc_idx;
57 dp = &vq->vq_ring.desc[desc_idx];
58 dxp = &vq->vq_descx[desc_idx];
59 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
60 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
61 while (dp->flags & VRING_DESC_F_NEXT) {
62 desc_idx_last = dp->next;
63 dp = &vq->vq_ring.desc[dp->next];
69 * We must append the existing free chain, if any, to the end of
70 * newly freed chain. If the virtqueue was completely used, then
71 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
73 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
74 vq->vq_desc_head_idx = desc_idx;
76 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
77 dp_tail->next = desc_idx;
80 vq->vq_desc_tail_idx = desc_idx_last;
81 dp->next = VQ_RING_DESC_CHAIN_END;
85 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
86 uint32_t *len, uint16_t num)
88 struct vring_used_elem *uep;
89 struct rte_mbuf *cookie;
90 uint16_t used_idx, desc_idx;
93 /* Caller does the check */
94 for (i = 0; i < num ; i++) {
95 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
96 uep = &vq->vq_ring.used->ring[used_idx];
97 desc_idx = (uint16_t) uep->id;
99 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
101 if (unlikely(cookie == NULL)) {
102 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
103 vq->vq_used_cons_idx);
107 rte_prefetch0(cookie);
108 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
110 vq->vq_used_cons_idx++;
111 vq_ring_free_chain(vq, desc_idx);
112 vq->vq_descx[desc_idx].cookie = NULL;
118 #ifndef DEFAULT_TX_FREE_THRESH
119 #define DEFAULT_TX_FREE_THRESH 32
122 /* Cleanup from completed transmits. */
124 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
126 uint16_t i, used_idx, desc_idx;
127 for (i = 0; i < num; i++) {
128 struct vring_used_elem *uep;
129 struct vq_desc_extra *dxp;
131 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
132 uep = &vq->vq_ring.used->ring[used_idx];
134 desc_idx = (uint16_t) uep->id;
135 dxp = &vq->vq_descx[desc_idx];
136 vq->vq_used_cons_idx++;
137 vq_ring_free_chain(vq, desc_idx);
139 if (dxp->cookie != NULL) {
140 rte_pktmbuf_free(dxp->cookie);
148 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
150 struct vq_desc_extra *dxp;
151 struct virtio_hw *hw = vq->hw;
152 struct vring_desc *start_dp;
154 uint16_t head_idx, idx;
156 if (unlikely(vq->vq_free_cnt == 0))
158 if (unlikely(vq->vq_free_cnt < needed))
161 head_idx = vq->vq_desc_head_idx;
162 if (unlikely(head_idx >= vq->vq_nentries))
166 dxp = &vq->vq_descx[idx];
167 dxp->cookie = (void *)cookie;
168 dxp->ndescs = needed;
170 start_dp = vq->vq_ring.desc;
172 VIRTIO_MBUF_ADDR(cookie, vq) +
173 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
175 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
176 start_dp[idx].flags = VRING_DESC_F_WRITE;
177 idx = start_dp[idx].next;
178 vq->vq_desc_head_idx = idx;
179 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
180 vq->vq_desc_tail_idx = idx;
181 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
182 vq_update_avail_ring(vq, head_idx);
187 /* When doing TSO, the IP length is not included in the pseudo header
188 * checksum of the packet given to the PMD, but for virtio it is
192 virtio_tso_fix_cksum(struct rte_mbuf *m)
194 /* common case: header is not fragmented */
195 if (likely(rte_pktmbuf_data_len(m) >= m->l2_len + m->l3_len +
197 struct ipv4_hdr *iph;
198 struct ipv6_hdr *ip6h;
200 uint16_t prev_cksum, new_cksum, ip_len, ip_paylen;
203 iph = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *, m->l2_len);
204 th = RTE_PTR_ADD(iph, m->l3_len);
205 if ((iph->version_ihl >> 4) == 4) {
206 iph->hdr_checksum = 0;
207 iph->hdr_checksum = rte_ipv4_cksum(iph);
208 ip_len = iph->total_length;
209 ip_paylen = rte_cpu_to_be_16(rte_be_to_cpu_16(ip_len) -
212 ip6h = (struct ipv6_hdr *)iph;
213 ip_paylen = ip6h->payload_len;
216 /* calculate the new phdr checksum not including ip_paylen */
217 prev_cksum = th->cksum;
220 tmp = (tmp & 0xffff) + (tmp >> 16);
223 /* replace it in the packet */
224 th->cksum = new_cksum;
229 tx_offload_enabled(struct virtio_hw *hw)
231 return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
232 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
233 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
236 /* avoid write operation when necessary, to lessen cache issues */
237 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
238 if ((var) != (val)) \
243 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
244 uint16_t needed, int use_indirect, int can_push)
246 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
247 struct vq_desc_extra *dxp;
248 struct virtqueue *vq = txvq->vq;
249 struct vring_desc *start_dp;
250 uint16_t seg_num = cookie->nb_segs;
251 uint16_t head_idx, idx;
252 uint16_t head_size = vq->hw->vtnet_hdr_size;
253 struct virtio_net_hdr *hdr;
256 offload = tx_offload_enabled(vq->hw);
257 head_idx = vq->vq_desc_head_idx;
259 dxp = &vq->vq_descx[idx];
260 dxp->cookie = (void *)cookie;
261 dxp->ndescs = needed;
263 start_dp = vq->vq_ring.desc;
266 /* prepend cannot fail, checked by caller */
267 hdr = (struct virtio_net_hdr *)
268 rte_pktmbuf_prepend(cookie, head_size);
269 /* rte_pktmbuf_prepend() counts the hdr size to the pkt length,
270 * which is wrong. Below subtract restores correct pkt size.
272 cookie->pkt_len -= head_size;
273 /* if offload disabled, it is not zeroed below, do it now */
275 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
276 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
277 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
278 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
279 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
280 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
282 } else if (use_indirect) {
283 /* setup tx ring slot to point to indirect
284 * descriptor list stored in reserved region.
286 * the first slot in indirect ring is already preset
287 * to point to the header in reserved region
289 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
290 RTE_PTR_DIFF(&txr[idx].tx_indir, txr);
291 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
292 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
293 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
295 /* loop below will fill in rest of the indirect elements */
296 start_dp = txr[idx].tx_indir;
299 /* setup first tx ring slot to point to header
300 * stored in reserved region.
302 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
303 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
304 start_dp[idx].len = vq->hw->vtnet_hdr_size;
305 start_dp[idx].flags = VRING_DESC_F_NEXT;
306 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
308 idx = start_dp[idx].next;
311 /* Checksum Offload / TSO */
313 if (cookie->ol_flags & PKT_TX_TCP_SEG)
314 cookie->ol_flags |= PKT_TX_TCP_CKSUM;
316 switch (cookie->ol_flags & PKT_TX_L4_MASK) {
317 case PKT_TX_UDP_CKSUM:
318 hdr->csum_start = cookie->l2_len + cookie->l3_len;
319 hdr->csum_offset = offsetof(struct udp_hdr,
321 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
324 case PKT_TX_TCP_CKSUM:
325 hdr->csum_start = cookie->l2_len + cookie->l3_len;
326 hdr->csum_offset = offsetof(struct tcp_hdr, cksum);
327 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
331 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
332 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
333 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
337 /* TCP Segmentation Offload */
338 if (cookie->ol_flags & PKT_TX_TCP_SEG) {
339 virtio_tso_fix_cksum(cookie);
340 hdr->gso_type = (cookie->ol_flags & PKT_TX_IPV6) ?
341 VIRTIO_NET_HDR_GSO_TCPV6 :
342 VIRTIO_NET_HDR_GSO_TCPV4;
343 hdr->gso_size = cookie->tso_segsz;
349 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
350 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
351 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
356 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
357 start_dp[idx].len = cookie->data_len;
358 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
359 idx = start_dp[idx].next;
360 } while ((cookie = cookie->next) != NULL);
363 idx = vq->vq_ring.desc[head_idx].next;
365 vq->vq_desc_head_idx = idx;
366 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
367 vq->vq_desc_tail_idx = idx;
368 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
369 vq_update_avail_ring(vq, head_idx);
373 virtio_dev_cq_start(struct rte_eth_dev *dev)
375 struct virtio_hw *hw = dev->data->dev_private;
377 if (hw->cvq && hw->cvq->vq) {
378 rte_spinlock_init(&hw->cvq->lock);
379 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
384 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
387 unsigned int socket_id __rte_unused,
388 const struct rte_eth_rxconf *rx_conf,
389 struct rte_mempool *mp)
391 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
392 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
393 struct virtio_hw *hw = dev->data->dev_private;
394 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
395 struct virtnet_rx *rxvq;
397 PMD_INIT_FUNC_TRACE();
399 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
400 nb_desc = vq->vq_nentries;
401 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
404 rxvq->queue_id = queue_idx;
406 if (rxvq->mpool == NULL) {
407 rte_exit(EXIT_FAILURE,
408 "Cannot allocate mbufs for rx virtqueue");
411 if ((rx_conf->offloads ^ rxmode->offloads) &
412 VIRTIO_PMD_PER_DEVICE_RX_OFFLOADS)
415 dev->data->rx_queues[queue_idx] = rxvq;
421 virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
423 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
424 struct virtio_hw *hw = dev->data->dev_private;
425 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
426 struct virtnet_rx *rxvq = &vq->rxq;
431 PMD_INIT_FUNC_TRACE();
433 /* Allocate blank mbufs for the each rx descriptor */
436 if (hw->use_simple_rx) {
437 for (desc_idx = 0; desc_idx < vq->vq_nentries;
439 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
440 vq->vq_ring.desc[desc_idx].flags =
444 virtio_rxq_vec_setup(rxvq);
447 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
448 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
450 vq->sw_ring[vq->vq_nentries + desc_idx] =
454 if (hw->use_simple_rx) {
455 while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
456 virtio_rxq_rearm_vec(rxvq);
457 nbufs += RTE_VIRTIO_VPMD_RX_REARM_THRESH;
460 while (!virtqueue_full(vq)) {
461 m = rte_mbuf_raw_alloc(rxvq->mpool);
465 /* Enqueue allocated buffers */
466 error = virtqueue_enqueue_recv_refill(vq, m);
474 vq_update_avail_idx(vq);
477 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
485 * struct rte_eth_dev *dev: Used to update dev
486 * uint16_t nb_desc: Defaults to values read from config space
487 * unsigned int socket_id: Used to allocate memzone
488 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
489 * uint16_t queue_idx: Just used as an index in dev txq list
492 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
495 unsigned int socket_id __rte_unused,
496 const struct rte_eth_txconf *tx_conf)
498 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
499 struct virtio_hw *hw = dev->data->dev_private;
500 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
501 struct virtnet_tx *txvq;
502 uint16_t tx_free_thresh;
504 PMD_INIT_FUNC_TRACE();
506 /* cannot use simple rxtx funcs with multisegs or offloads */
507 if (tx_conf->offloads)
508 hw->use_simple_tx = 0;
510 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
511 nb_desc = vq->vq_nentries;
512 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
515 txvq->queue_id = queue_idx;
517 tx_free_thresh = tx_conf->tx_free_thresh;
518 if (tx_free_thresh == 0)
520 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
522 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
523 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
524 "number of TX entries minus 3 (%u)."
525 " (tx_free_thresh=%u port=%u queue=%u)\n",
527 tx_free_thresh, dev->data->port_id, queue_idx);
531 vq->vq_free_thresh = tx_free_thresh;
533 dev->data->tx_queues[queue_idx] = txvq;
538 virtio_dev_tx_queue_setup_finish(struct rte_eth_dev *dev,
541 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
542 struct virtio_hw *hw = dev->data->dev_private;
543 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
544 uint16_t mid_idx = vq->vq_nentries >> 1;
545 struct virtnet_tx *txvq = &vq->txq;
548 PMD_INIT_FUNC_TRACE();
550 if (hw->use_simple_tx) {
551 for (desc_idx = 0; desc_idx < mid_idx; desc_idx++) {
552 vq->vq_ring.avail->ring[desc_idx] =
554 vq->vq_ring.desc[desc_idx + mid_idx].next =
556 vq->vq_ring.desc[desc_idx + mid_idx].addr =
557 txvq->virtio_net_hdr_mem +
558 offsetof(struct virtio_tx_region, tx_hdr);
559 vq->vq_ring.desc[desc_idx + mid_idx].len =
560 vq->hw->vtnet_hdr_size;
561 vq->vq_ring.desc[desc_idx + mid_idx].flags =
563 vq->vq_ring.desc[desc_idx].flags = 0;
565 for (desc_idx = mid_idx; desc_idx < vq->vq_nentries;
567 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
576 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
580 * Requeue the discarded mbuf. This should always be
581 * successful since it was just dequeued.
583 error = virtqueue_enqueue_recv_refill(vq, m);
584 if (unlikely(error)) {
585 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
591 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
593 uint32_t s = mbuf->pkt_len;
594 struct ether_addr *ea;
597 stats->size_bins[1]++;
598 } else if (s > 64 && s < 1024) {
601 /* count zeros, and offset into correct bin */
602 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
603 stats->size_bins[bin]++;
606 stats->size_bins[0]++;
608 stats->size_bins[6]++;
610 stats->size_bins[7]++;
613 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
614 if (is_multicast_ether_addr(ea)) {
615 if (is_broadcast_ether_addr(ea))
622 /* Optionally fill offload information in structure */
624 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
626 struct rte_net_hdr_lens hdr_lens;
627 uint32_t hdrlen, ptype;
628 int l4_supported = 0;
631 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
634 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
636 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
637 m->packet_type = ptype;
638 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
639 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
640 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
643 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
644 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
645 if (hdr->csum_start <= hdrlen && l4_supported) {
646 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
648 /* Unknown proto or tunnel, do sw cksum. We can assume
649 * the cksum field is in the first segment since the
650 * buffers we provided to the host are large enough.
651 * In case of SCTP, this will be wrong since it's a CRC
652 * but there's nothing we can do.
654 uint16_t csum = 0, off;
656 rte_raw_cksum_mbuf(m, hdr->csum_start,
657 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
659 if (likely(csum != 0xffff))
661 off = hdr->csum_offset + hdr->csum_start;
662 if (rte_pktmbuf_data_len(m) >= off + 1)
663 *rte_pktmbuf_mtod_offset(m, uint16_t *,
666 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
667 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
670 /* GSO request, save required information in mbuf */
671 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
672 /* Check unsupported modes */
673 if ((hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) ||
674 (hdr->gso_size == 0)) {
678 /* Update mss lengthes in mbuf */
679 m->tso_segsz = hdr->gso_size;
680 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
681 case VIRTIO_NET_HDR_GSO_TCPV4:
682 case VIRTIO_NET_HDR_GSO_TCPV6:
683 m->ol_flags |= PKT_RX_LRO | \
684 PKT_RX_L4_CKSUM_NONE;
695 rx_offload_enabled(struct virtio_hw *hw)
697 return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
698 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
699 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
702 #define VIRTIO_MBUF_BURST_SZ 64
703 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
705 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
707 struct virtnet_rx *rxvq = rx_queue;
708 struct virtqueue *vq = rxvq->vq;
709 struct virtio_hw *hw = vq->hw;
710 struct rte_mbuf *rxm, *new_mbuf;
711 uint16_t nb_used, num, nb_rx;
712 uint32_t len[VIRTIO_MBUF_BURST_SZ];
713 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
715 uint32_t i, nb_enqueued;
718 struct virtio_net_hdr *hdr;
721 if (unlikely(hw->started == 0))
724 nb_used = VIRTQUEUE_NUSED(vq);
728 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
729 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
730 num = VIRTIO_MBUF_BURST_SZ;
731 if (likely(num > DESC_PER_CACHELINE))
732 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
734 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
735 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
738 hdr_size = hw->vtnet_hdr_size;
739 offload = rx_offload_enabled(hw);
741 for (i = 0; i < num ; i++) {
744 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
746 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
747 PMD_RX_LOG(ERR, "Packet drop");
749 virtio_discard_rxbuf(vq, rxm);
750 rxvq->stats.errors++;
754 rxm->port = rxvq->port_id;
755 rxm->data_off = RTE_PKTMBUF_HEADROOM;
759 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
760 rxm->data_len = (uint16_t)(len[i] - hdr_size);
762 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
763 RTE_PKTMBUF_HEADROOM - hdr_size);
768 if (offload && virtio_rx_offload(rxm, hdr) < 0) {
769 virtio_discard_rxbuf(vq, rxm);
770 rxvq->stats.errors++;
774 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
776 rx_pkts[nb_rx++] = rxm;
778 rxvq->stats.bytes += rxm->pkt_len;
779 virtio_update_packet_stats(&rxvq->stats, rxm);
782 rxvq->stats.packets += nb_rx;
784 /* Allocate new mbuf for the used descriptor */
786 while (likely(!virtqueue_full(vq))) {
787 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
788 if (unlikely(new_mbuf == NULL)) {
789 struct rte_eth_dev *dev
790 = &rte_eth_devices[rxvq->port_id];
791 dev->data->rx_mbuf_alloc_failed++;
794 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
795 if (unlikely(error)) {
796 rte_pktmbuf_free(new_mbuf);
802 if (likely(nb_enqueued)) {
803 vq_update_avail_idx(vq);
805 if (unlikely(virtqueue_kick_prepare(vq))) {
806 virtqueue_notify(vq);
807 PMD_RX_LOG(DEBUG, "Notified");
815 virtio_recv_mergeable_pkts(void *rx_queue,
816 struct rte_mbuf **rx_pkts,
819 struct virtnet_rx *rxvq = rx_queue;
820 struct virtqueue *vq = rxvq->vq;
821 struct virtio_hw *hw = vq->hw;
822 struct rte_mbuf *rxm, *new_mbuf;
823 uint16_t nb_used, num, nb_rx;
824 uint32_t len[VIRTIO_MBUF_BURST_SZ];
825 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
826 struct rte_mbuf *prev;
828 uint32_t i, nb_enqueued;
836 if (unlikely(hw->started == 0))
839 nb_used = VIRTQUEUE_NUSED(vq);
843 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
850 hdr_size = hw->vtnet_hdr_size;
851 offload = rx_offload_enabled(hw);
853 while (i < nb_used) {
854 struct virtio_net_hdr_mrg_rxbuf *header;
856 if (nb_rx == nb_pkts)
859 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, 1);
865 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
866 PMD_RX_LOG(DEBUG, "packet len:%d", len[0]);
870 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
871 PMD_RX_LOG(ERR, "Packet drop");
873 virtio_discard_rxbuf(vq, rxm);
874 rxvq->stats.errors++;
878 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
879 RTE_PKTMBUF_HEADROOM - hdr_size);
880 seg_num = header->num_buffers;
885 rxm->data_off = RTE_PKTMBUF_HEADROOM;
886 rxm->nb_segs = seg_num;
889 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
890 rxm->data_len = (uint16_t)(len[0] - hdr_size);
892 rxm->port = rxvq->port_id;
893 rx_pkts[nb_rx] = rxm;
896 if (offload && virtio_rx_offload(rxm, &header->hdr) < 0) {
897 virtio_discard_rxbuf(vq, rxm);
898 rxvq->stats.errors++;
902 seg_res = seg_num - 1;
904 while (seg_res != 0) {
906 * Get extra segments for current uncompleted packet.
909 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
910 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
912 virtqueue_dequeue_burst_rx(vq,
913 rcv_pkts, len, rcv_cnt);
918 "No enough segments for packet.");
920 virtio_discard_rxbuf(vq, rxm);
921 rxvq->stats.errors++;
927 while (extra_idx < rcv_cnt) {
928 rxm = rcv_pkts[extra_idx];
930 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
931 rxm->pkt_len = (uint32_t)(len[extra_idx]);
932 rxm->data_len = (uint16_t)(len[extra_idx]);
938 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
945 rte_vlan_strip(rx_pkts[nb_rx]);
947 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
948 rx_pkts[nb_rx]->data_len);
950 rxvq->stats.bytes += rx_pkts[nb_rx]->pkt_len;
951 virtio_update_packet_stats(&rxvq->stats, rx_pkts[nb_rx]);
955 rxvq->stats.packets += nb_rx;
957 /* Allocate new mbuf for the used descriptor */
959 while (likely(!virtqueue_full(vq))) {
960 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
961 if (unlikely(new_mbuf == NULL)) {
962 struct rte_eth_dev *dev
963 = &rte_eth_devices[rxvq->port_id];
964 dev->data->rx_mbuf_alloc_failed++;
967 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
968 if (unlikely(error)) {
969 rte_pktmbuf_free(new_mbuf);
975 if (likely(nb_enqueued)) {
976 vq_update_avail_idx(vq);
978 if (unlikely(virtqueue_kick_prepare(vq))) {
979 virtqueue_notify(vq);
980 PMD_RX_LOG(DEBUG, "Notified");
988 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
990 struct virtnet_tx *txvq = tx_queue;
991 struct virtqueue *vq = txvq->vq;
992 struct virtio_hw *hw = vq->hw;
993 uint16_t hdr_size = hw->vtnet_hdr_size;
994 uint16_t nb_used, nb_tx = 0;
997 if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
1000 if (unlikely(nb_pkts < 1))
1003 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
1004 nb_used = VIRTQUEUE_NUSED(vq);
1007 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
1008 virtio_xmit_cleanup(vq, nb_used);
1010 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1011 struct rte_mbuf *txm = tx_pkts[nb_tx];
1012 int can_push = 0, use_indirect = 0, slots, need;
1014 /* Do VLAN tag insertion */
1015 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
1016 error = rte_vlan_insert(&txm);
1017 if (unlikely(error)) {
1018 rte_pktmbuf_free(txm);
1023 /* optimize ring usage */
1024 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
1025 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
1026 rte_mbuf_refcnt_read(txm) == 1 &&
1027 RTE_MBUF_DIRECT(txm) &&
1028 txm->nb_segs == 1 &&
1029 rte_pktmbuf_headroom(txm) >= hdr_size &&
1030 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
1031 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
1033 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
1034 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
1037 /* How many main ring entries are needed to this Tx?
1038 * any_layout => number of segments
1040 * default => number of segments + 1
1042 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
1043 need = slots - vq->vq_free_cnt;
1045 /* Positive value indicates it need free vring descriptors */
1046 if (unlikely(need > 0)) {
1047 nb_used = VIRTQUEUE_NUSED(vq);
1049 need = RTE_MIN(need, (int)nb_used);
1051 virtio_xmit_cleanup(vq, need);
1052 need = slots - vq->vq_free_cnt;
1053 if (unlikely(need > 0)) {
1055 "No free tx descriptors to transmit");
1060 /* Enqueue Packet buffers */
1061 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect, can_push);
1063 txvq->stats.bytes += txm->pkt_len;
1064 virtio_update_packet_stats(&txvq->stats, txm);
1067 txvq->stats.packets += nb_tx;
1069 if (likely(nb_tx)) {
1070 vq_update_avail_idx(vq);
1072 if (unlikely(virtqueue_kick_prepare(vq))) {
1073 virtqueue_notify(vq);
1074 PMD_TX_LOG(DEBUG, "Notified backend after xmit");