4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <rte_cycles.h>
41 #include <rte_memory.h>
42 #include <rte_memzone.h>
43 #include <rte_branch_prediction.h>
44 #include <rte_mempool.h>
45 #include <rte_malloc.h>
47 #include <rte_ether.h>
48 #include <rte_ethdev.h>
49 #include <rte_prefetch.h>
50 #include <rte_string_fns.h>
51 #include <rte_errno.h>
52 #include <rte_byteorder.h>
53 #include <rte_cpuflags.h>
59 #include "virtio_logs.h"
60 #include "virtio_ethdev.h"
61 #include "virtio_pci.h"
62 #include "virtqueue.h"
63 #include "virtio_rxtx.h"
64 #include "virtio_rxtx_simple.h"
66 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
67 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
69 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0)
73 #define VIRTIO_SIMPLE_FLAGS ((uint32_t)ETH_TXQ_FLAGS_NOMULTSEGS | \
74 ETH_TXQ_FLAGS_NOOFFLOADS)
77 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
79 struct vring_desc *dp, *dp_tail;
80 struct vq_desc_extra *dxp;
81 uint16_t desc_idx_last = desc_idx;
83 dp = &vq->vq_ring.desc[desc_idx];
84 dxp = &vq->vq_descx[desc_idx];
85 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
86 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
87 while (dp->flags & VRING_DESC_F_NEXT) {
88 desc_idx_last = dp->next;
89 dp = &vq->vq_ring.desc[dp->next];
95 * We must append the existing free chain, if any, to the end of
96 * newly freed chain. If the virtqueue was completely used, then
97 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
99 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
100 vq->vq_desc_head_idx = desc_idx;
102 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
103 dp_tail->next = desc_idx;
106 vq->vq_desc_tail_idx = desc_idx_last;
107 dp->next = VQ_RING_DESC_CHAIN_END;
111 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
112 uint32_t *len, uint16_t num)
114 struct vring_used_elem *uep;
115 struct rte_mbuf *cookie;
116 uint16_t used_idx, desc_idx;
119 /* Caller does the check */
120 for (i = 0; i < num ; i++) {
121 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
122 uep = &vq->vq_ring.used->ring[used_idx];
123 desc_idx = (uint16_t) uep->id;
125 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
127 if (unlikely(cookie == NULL)) {
128 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u\n",
129 vq->vq_used_cons_idx);
133 rte_prefetch0(cookie);
134 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
136 vq->vq_used_cons_idx++;
137 vq_ring_free_chain(vq, desc_idx);
138 vq->vq_descx[desc_idx].cookie = NULL;
144 #ifndef DEFAULT_TX_FREE_THRESH
145 #define DEFAULT_TX_FREE_THRESH 32
148 /* Cleanup from completed transmits. */
150 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
152 uint16_t i, used_idx, desc_idx;
153 for (i = 0; i < num; i++) {
154 struct vring_used_elem *uep;
155 struct vq_desc_extra *dxp;
157 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
158 uep = &vq->vq_ring.used->ring[used_idx];
160 desc_idx = (uint16_t) uep->id;
161 dxp = &vq->vq_descx[desc_idx];
162 vq->vq_used_cons_idx++;
163 vq_ring_free_chain(vq, desc_idx);
165 if (dxp->cookie != NULL) {
166 rte_pktmbuf_free(dxp->cookie);
174 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
176 struct vq_desc_extra *dxp;
177 struct virtio_hw *hw = vq->hw;
178 struct vring_desc *start_dp;
180 uint16_t head_idx, idx;
182 if (unlikely(vq->vq_free_cnt == 0))
184 if (unlikely(vq->vq_free_cnt < needed))
187 head_idx = vq->vq_desc_head_idx;
188 if (unlikely(head_idx >= vq->vq_nentries))
192 dxp = &vq->vq_descx[idx];
193 dxp->cookie = (void *)cookie;
194 dxp->ndescs = needed;
196 start_dp = vq->vq_ring.desc;
198 VIRTIO_MBUF_ADDR(cookie, vq) +
199 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
201 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
202 start_dp[idx].flags = VRING_DESC_F_WRITE;
203 idx = start_dp[idx].next;
204 vq->vq_desc_head_idx = idx;
205 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
206 vq->vq_desc_tail_idx = idx;
207 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
208 vq_update_avail_ring(vq, head_idx);
213 /* When doing TSO, the IP length is not included in the pseudo header
214 * checksum of the packet given to the PMD, but for virtio it is
218 virtio_tso_fix_cksum(struct rte_mbuf *m)
220 /* common case: header is not fragmented */
221 if (likely(rte_pktmbuf_data_len(m) >= m->l2_len + m->l3_len +
223 struct ipv4_hdr *iph;
224 struct ipv6_hdr *ip6h;
226 uint16_t prev_cksum, new_cksum, ip_len, ip_paylen;
229 iph = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *, m->l2_len);
230 th = RTE_PTR_ADD(iph, m->l3_len);
231 if ((iph->version_ihl >> 4) == 4) {
232 iph->hdr_checksum = 0;
233 iph->hdr_checksum = rte_ipv4_cksum(iph);
234 ip_len = iph->total_length;
235 ip_paylen = rte_cpu_to_be_16(rte_be_to_cpu_16(ip_len) -
238 ip6h = (struct ipv6_hdr *)iph;
239 ip_paylen = ip6h->payload_len;
242 /* calculate the new phdr checksum not including ip_paylen */
243 prev_cksum = th->cksum;
246 tmp = (tmp & 0xffff) + (tmp >> 16);
249 /* replace it in the packet */
250 th->cksum = new_cksum;
255 tx_offload_enabled(struct virtio_hw *hw)
257 return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
258 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
259 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
262 /* avoid write operation when necessary, to lessen cache issues */
263 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
264 if ((var) != (val)) \
269 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
270 uint16_t needed, int use_indirect, int can_push)
272 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
273 struct vq_desc_extra *dxp;
274 struct virtqueue *vq = txvq->vq;
275 struct vring_desc *start_dp;
276 uint16_t seg_num = cookie->nb_segs;
277 uint16_t head_idx, idx;
278 uint16_t head_size = vq->hw->vtnet_hdr_size;
279 struct virtio_net_hdr *hdr;
282 offload = tx_offload_enabled(vq->hw);
283 head_idx = vq->vq_desc_head_idx;
285 dxp = &vq->vq_descx[idx];
286 dxp->cookie = (void *)cookie;
287 dxp->ndescs = needed;
289 start_dp = vq->vq_ring.desc;
292 /* prepend cannot fail, checked by caller */
293 hdr = (struct virtio_net_hdr *)
294 rte_pktmbuf_prepend(cookie, head_size);
295 /* rte_pktmbuf_prepend() counts the hdr size to the pkt length,
296 * which is wrong. Below subtract restores correct pkt size.
298 cookie->pkt_len -= head_size;
299 /* if offload disabled, it is not zeroed below, do it now */
301 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
302 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
303 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
304 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
305 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
306 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
308 } else if (use_indirect) {
309 /* setup tx ring slot to point to indirect
310 * descriptor list stored in reserved region.
312 * the first slot in indirect ring is already preset
313 * to point to the header in reserved region
315 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
316 RTE_PTR_DIFF(&txr[idx].tx_indir, txr);
317 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
318 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
319 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
321 /* loop below will fill in rest of the indirect elements */
322 start_dp = txr[idx].tx_indir;
325 /* setup first tx ring slot to point to header
326 * stored in reserved region.
328 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
329 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
330 start_dp[idx].len = vq->hw->vtnet_hdr_size;
331 start_dp[idx].flags = VRING_DESC_F_NEXT;
332 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
334 idx = start_dp[idx].next;
337 /* Checksum Offload / TSO */
339 if (cookie->ol_flags & PKT_TX_TCP_SEG)
340 cookie->ol_flags |= PKT_TX_TCP_CKSUM;
342 switch (cookie->ol_flags & PKT_TX_L4_MASK) {
343 case PKT_TX_UDP_CKSUM:
344 hdr->csum_start = cookie->l2_len + cookie->l3_len;
345 hdr->csum_offset = offsetof(struct udp_hdr,
347 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
350 case PKT_TX_TCP_CKSUM:
351 hdr->csum_start = cookie->l2_len + cookie->l3_len;
352 hdr->csum_offset = offsetof(struct tcp_hdr, cksum);
353 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
357 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
358 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
359 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
363 /* TCP Segmentation Offload */
364 if (cookie->ol_flags & PKT_TX_TCP_SEG) {
365 virtio_tso_fix_cksum(cookie);
366 hdr->gso_type = (cookie->ol_flags & PKT_TX_IPV6) ?
367 VIRTIO_NET_HDR_GSO_TCPV6 :
368 VIRTIO_NET_HDR_GSO_TCPV4;
369 hdr->gso_size = cookie->tso_segsz;
375 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
376 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
377 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
382 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
383 start_dp[idx].len = cookie->data_len;
384 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
385 idx = start_dp[idx].next;
386 } while ((cookie = cookie->next) != NULL);
389 idx = vq->vq_ring.desc[head_idx].next;
391 vq->vq_desc_head_idx = idx;
392 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
393 vq->vq_desc_tail_idx = idx;
394 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
395 vq_update_avail_ring(vq, head_idx);
399 virtio_dev_cq_start(struct rte_eth_dev *dev)
401 struct virtio_hw *hw = dev->data->dev_private;
403 if (hw->cvq && hw->cvq->vq) {
404 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
409 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
412 unsigned int socket_id __rte_unused,
413 __rte_unused const struct rte_eth_rxconf *rx_conf,
414 struct rte_mempool *mp)
416 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
417 struct virtio_hw *hw = dev->data->dev_private;
418 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
419 struct virtnet_rx *rxvq;
421 PMD_INIT_FUNC_TRACE();
423 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
424 nb_desc = vq->vq_nentries;
425 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
428 rxvq->queue_id = queue_idx;
430 if (rxvq->mpool == NULL) {
431 rte_exit(EXIT_FAILURE,
432 "Cannot allocate mbufs for rx virtqueue");
434 dev->data->rx_queues[queue_idx] = rxvq;
440 virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
442 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
443 struct virtio_hw *hw = dev->data->dev_private;
444 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
445 struct virtnet_rx *rxvq = &vq->rxq;
450 PMD_INIT_FUNC_TRACE();
452 /* Allocate blank mbufs for the each rx descriptor */
455 if (hw->use_simple_rxtx) {
456 for (desc_idx = 0; desc_idx < vq->vq_nentries;
458 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
459 vq->vq_ring.desc[desc_idx].flags =
463 virtio_rxq_vec_setup(rxvq);
466 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
467 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
469 vq->sw_ring[vq->vq_nentries + desc_idx] =
473 if (hw->use_simple_rxtx) {
474 while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
475 virtio_rxq_rearm_vec(rxvq);
476 nbufs += RTE_VIRTIO_VPMD_RX_REARM_THRESH;
479 while (!virtqueue_full(vq)) {
480 m = rte_mbuf_raw_alloc(rxvq->mpool);
484 /* Enqueue allocated buffers */
485 error = virtqueue_enqueue_recv_refill(vq, m);
493 vq_update_avail_idx(vq);
496 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
504 virtio_update_rxtx_handler(struct rte_eth_dev *dev,
505 const struct rte_eth_txconf *tx_conf)
507 uint8_t use_simple_rxtx = 0;
508 struct virtio_hw *hw = dev->data->dev_private;
510 #if defined RTE_ARCH_X86
511 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE3))
513 #elif defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
514 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON))
517 /* Use simple rx/tx func if single segment and no offloads */
518 if (use_simple_rxtx &&
519 (tx_conf->txq_flags & VIRTIO_SIMPLE_FLAGS) == VIRTIO_SIMPLE_FLAGS &&
520 !vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
521 PMD_INIT_LOG(INFO, "Using simple rx/tx path");
522 dev->tx_pkt_burst = virtio_xmit_pkts_simple;
523 dev->rx_pkt_burst = virtio_recv_pkts_vec;
524 hw->use_simple_rxtx = use_simple_rxtx;
529 * struct rte_eth_dev *dev: Used to update dev
530 * uint16_t nb_desc: Defaults to values read from config space
531 * unsigned int socket_id: Used to allocate memzone
532 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
533 * uint16_t queue_idx: Just used as an index in dev txq list
536 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
539 unsigned int socket_id __rte_unused,
540 const struct rte_eth_txconf *tx_conf)
542 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
543 struct virtio_hw *hw = dev->data->dev_private;
544 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
545 struct virtnet_tx *txvq;
546 uint16_t tx_free_thresh;
548 PMD_INIT_FUNC_TRACE();
550 virtio_update_rxtx_handler(dev, tx_conf);
552 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
553 nb_desc = vq->vq_nentries;
554 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
557 txvq->queue_id = queue_idx;
559 tx_free_thresh = tx_conf->tx_free_thresh;
560 if (tx_free_thresh == 0)
562 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
564 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
565 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
566 "number of TX entries minus 3 (%u)."
567 " (tx_free_thresh=%u port=%u queue=%u)\n",
569 tx_free_thresh, dev->data->port_id, queue_idx);
573 vq->vq_free_thresh = tx_free_thresh;
575 dev->data->tx_queues[queue_idx] = txvq;
580 virtio_dev_tx_queue_setup_finish(struct rte_eth_dev *dev,
583 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
584 struct virtio_hw *hw = dev->data->dev_private;
585 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
586 uint16_t mid_idx = vq->vq_nentries >> 1;
587 struct virtnet_tx *txvq = &vq->txq;
590 PMD_INIT_FUNC_TRACE();
592 if (hw->use_simple_rxtx) {
593 for (desc_idx = 0; desc_idx < mid_idx; desc_idx++) {
594 vq->vq_ring.avail->ring[desc_idx] =
596 vq->vq_ring.desc[desc_idx + mid_idx].next =
598 vq->vq_ring.desc[desc_idx + mid_idx].addr =
599 txvq->virtio_net_hdr_mem +
600 offsetof(struct virtio_tx_region, tx_hdr);
601 vq->vq_ring.desc[desc_idx + mid_idx].len =
602 vq->hw->vtnet_hdr_size;
603 vq->vq_ring.desc[desc_idx + mid_idx].flags =
605 vq->vq_ring.desc[desc_idx].flags = 0;
607 for (desc_idx = mid_idx; desc_idx < vq->vq_nentries;
609 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
618 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
622 * Requeue the discarded mbuf. This should always be
623 * successful since it was just dequeued.
625 error = virtqueue_enqueue_recv_refill(vq, m);
626 if (unlikely(error)) {
627 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
633 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
635 uint32_t s = mbuf->pkt_len;
636 struct ether_addr *ea;
639 stats->size_bins[1]++;
640 } else if (s > 64 && s < 1024) {
643 /* count zeros, and offset into correct bin */
644 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
645 stats->size_bins[bin]++;
648 stats->size_bins[0]++;
650 stats->size_bins[6]++;
652 stats->size_bins[7]++;
655 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
656 if (is_multicast_ether_addr(ea)) {
657 if (is_broadcast_ether_addr(ea))
664 /* Optionally fill offload information in structure */
666 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
668 struct rte_net_hdr_lens hdr_lens;
669 uint32_t hdrlen, ptype;
670 int l4_supported = 0;
673 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
676 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
678 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
679 m->packet_type = ptype;
680 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
681 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
682 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
685 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
686 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
687 if (hdr->csum_start <= hdrlen && l4_supported) {
688 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
690 /* Unknown proto or tunnel, do sw cksum. We can assume
691 * the cksum field is in the first segment since the
692 * buffers we provided to the host are large enough.
693 * In case of SCTP, this will be wrong since it's a CRC
694 * but there's nothing we can do.
696 uint16_t csum = 0, off;
698 rte_raw_cksum_mbuf(m, hdr->csum_start,
699 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
701 if (likely(csum != 0xffff))
703 off = hdr->csum_offset + hdr->csum_start;
704 if (rte_pktmbuf_data_len(m) >= off + 1)
705 *rte_pktmbuf_mtod_offset(m, uint16_t *,
708 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
709 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
712 /* GSO request, save required information in mbuf */
713 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
714 /* Check unsupported modes */
715 if ((hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) ||
716 (hdr->gso_size == 0)) {
720 /* Update mss lengthes in mbuf */
721 m->tso_segsz = hdr->gso_size;
722 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
723 case VIRTIO_NET_HDR_GSO_TCPV4:
724 case VIRTIO_NET_HDR_GSO_TCPV6:
725 m->ol_flags |= PKT_RX_LRO | \
726 PKT_RX_L4_CKSUM_NONE;
737 rx_offload_enabled(struct virtio_hw *hw)
739 return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
740 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
741 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
744 #define VIRTIO_MBUF_BURST_SZ 64
745 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
747 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
749 struct virtnet_rx *rxvq = rx_queue;
750 struct virtqueue *vq = rxvq->vq;
751 struct virtio_hw *hw;
752 struct rte_mbuf *rxm, *new_mbuf;
753 uint16_t nb_used, num, nb_rx;
754 uint32_t len[VIRTIO_MBUF_BURST_SZ];
755 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
757 uint32_t i, nb_enqueued;
760 struct virtio_net_hdr *hdr;
762 nb_used = VIRTQUEUE_NUSED(vq);
766 num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
767 num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
768 if (likely(num > DESC_PER_CACHELINE))
769 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
771 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
772 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
777 hdr_size = hw->vtnet_hdr_size;
778 offload = rx_offload_enabled(hw);
780 for (i = 0; i < num ; i++) {
783 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
785 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
786 PMD_RX_LOG(ERR, "Packet drop");
788 virtio_discard_rxbuf(vq, rxm);
789 rxvq->stats.errors++;
793 rxm->port = rxvq->port_id;
794 rxm->data_off = RTE_PKTMBUF_HEADROOM;
800 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
801 rxm->data_len = (uint16_t)(len[i] - hdr_size);
803 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
804 RTE_PKTMBUF_HEADROOM - hdr_size);
809 if (offload && virtio_rx_offload(rxm, hdr) < 0) {
810 virtio_discard_rxbuf(vq, rxm);
811 rxvq->stats.errors++;
815 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
817 rx_pkts[nb_rx++] = rxm;
819 rxvq->stats.bytes += rx_pkts[nb_rx - 1]->pkt_len;
820 virtio_update_packet_stats(&rxvq->stats, rxm);
823 rxvq->stats.packets += nb_rx;
825 /* Allocate new mbuf for the used descriptor */
827 while (likely(!virtqueue_full(vq))) {
828 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
829 if (unlikely(new_mbuf == NULL)) {
830 struct rte_eth_dev *dev
831 = &rte_eth_devices[rxvq->port_id];
832 dev->data->rx_mbuf_alloc_failed++;
835 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
836 if (unlikely(error)) {
837 rte_pktmbuf_free(new_mbuf);
843 if (likely(nb_enqueued)) {
844 vq_update_avail_idx(vq);
846 if (unlikely(virtqueue_kick_prepare(vq))) {
847 virtqueue_notify(vq);
848 PMD_RX_LOG(DEBUG, "Notified");
856 virtio_recv_mergeable_pkts(void *rx_queue,
857 struct rte_mbuf **rx_pkts,
860 struct virtnet_rx *rxvq = rx_queue;
861 struct virtqueue *vq = rxvq->vq;
862 struct virtio_hw *hw;
863 struct rte_mbuf *rxm, *new_mbuf;
864 uint16_t nb_used, num, nb_rx;
865 uint32_t len[VIRTIO_MBUF_BURST_SZ];
866 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
867 struct rte_mbuf *prev;
869 uint32_t i, nb_enqueued;
876 nb_used = VIRTQUEUE_NUSED(vq);
880 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
889 hdr_size = hw->vtnet_hdr_size;
890 offload = rx_offload_enabled(hw);
892 while (i < nb_used) {
893 struct virtio_net_hdr_mrg_rxbuf *header;
895 if (nb_rx == nb_pkts)
898 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, 1);
904 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
905 PMD_RX_LOG(DEBUG, "packet len:%d", len[0]);
909 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
910 PMD_RX_LOG(ERR, "Packet drop");
912 virtio_discard_rxbuf(vq, rxm);
913 rxvq->stats.errors++;
917 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
918 RTE_PKTMBUF_HEADROOM - hdr_size);
919 seg_num = header->num_buffers;
924 rxm->data_off = RTE_PKTMBUF_HEADROOM;
925 rxm->nb_segs = seg_num;
929 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
930 rxm->data_len = (uint16_t)(len[0] - hdr_size);
932 rxm->port = rxvq->port_id;
933 rx_pkts[nb_rx] = rxm;
936 if (offload && virtio_rx_offload(rxm, &header->hdr) < 0) {
937 virtio_discard_rxbuf(vq, rxm);
938 rxvq->stats.errors++;
942 seg_res = seg_num - 1;
944 while (seg_res != 0) {
946 * Get extra segments for current uncompleted packet.
949 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
950 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
952 virtqueue_dequeue_burst_rx(vq,
953 rcv_pkts, len, rcv_cnt);
958 "No enough segments for packet.");
960 virtio_discard_rxbuf(vq, rxm);
961 rxvq->stats.errors++;
967 while (extra_idx < rcv_cnt) {
968 rxm = rcv_pkts[extra_idx];
970 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
972 rxm->pkt_len = (uint32_t)(len[extra_idx]);
973 rxm->data_len = (uint16_t)(len[extra_idx]);
979 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
986 rte_vlan_strip(rx_pkts[nb_rx]);
988 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
989 rx_pkts[nb_rx]->data_len);
991 rxvq->stats.bytes += rx_pkts[nb_rx]->pkt_len;
992 virtio_update_packet_stats(&rxvq->stats, rx_pkts[nb_rx]);
996 rxvq->stats.packets += nb_rx;
998 /* Allocate new mbuf for the used descriptor */
1000 while (likely(!virtqueue_full(vq))) {
1001 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
1002 if (unlikely(new_mbuf == NULL)) {
1003 struct rte_eth_dev *dev
1004 = &rte_eth_devices[rxvq->port_id];
1005 dev->data->rx_mbuf_alloc_failed++;
1008 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
1009 if (unlikely(error)) {
1010 rte_pktmbuf_free(new_mbuf);
1016 if (likely(nb_enqueued)) {
1017 vq_update_avail_idx(vq);
1019 if (unlikely(virtqueue_kick_prepare(vq))) {
1020 virtqueue_notify(vq);
1021 PMD_RX_LOG(DEBUG, "Notified");
1029 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1031 struct virtnet_tx *txvq = tx_queue;
1032 struct virtqueue *vq = txvq->vq;
1033 struct virtio_hw *hw = vq->hw;
1034 uint16_t hdr_size = hw->vtnet_hdr_size;
1035 uint16_t nb_used, nb_tx;
1038 if (unlikely(nb_pkts < 1))
1041 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
1042 nb_used = VIRTQUEUE_NUSED(vq);
1045 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
1046 virtio_xmit_cleanup(vq, nb_used);
1048 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1049 struct rte_mbuf *txm = tx_pkts[nb_tx];
1050 int can_push = 0, use_indirect = 0, slots, need;
1052 /* Do VLAN tag insertion */
1053 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
1054 error = rte_vlan_insert(&txm);
1055 if (unlikely(error)) {
1056 rte_pktmbuf_free(txm);
1061 /* optimize ring usage */
1062 if (vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) &&
1063 rte_mbuf_refcnt_read(txm) == 1 &&
1064 RTE_MBUF_DIRECT(txm) &&
1065 txm->nb_segs == 1 &&
1066 rte_pktmbuf_headroom(txm) >= hdr_size &&
1067 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
1068 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
1070 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
1071 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
1074 /* How many main ring entries are needed to this Tx?
1075 * any_layout => number of segments
1077 * default => number of segments + 1
1079 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
1080 need = slots - vq->vq_free_cnt;
1082 /* Positive value indicates it need free vring descriptors */
1083 if (unlikely(need > 0)) {
1084 nb_used = VIRTQUEUE_NUSED(vq);
1086 need = RTE_MIN(need, (int)nb_used);
1088 virtio_xmit_cleanup(vq, need);
1089 need = slots - vq->vq_free_cnt;
1090 if (unlikely(need > 0)) {
1092 "No free tx descriptors to transmit");
1097 /* Enqueue Packet buffers */
1098 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect, can_push);
1100 txvq->stats.bytes += txm->pkt_len;
1101 virtio_update_packet_stats(&txvq->stats, txm);
1104 txvq->stats.packets += nb_tx;
1106 if (likely(nb_tx)) {
1107 vq_update_avail_idx(vq);
1109 if (unlikely(virtqueue_kick_prepare(vq))) {
1110 virtqueue_notify(vq);
1111 PMD_TX_LOG(DEBUG, "Notified backend after xmit");