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>
54 #include "virtio_logs.h"
55 #include "virtio_ethdev.h"
56 #include "virtio_pci.h"
57 #include "virtqueue.h"
58 #include "virtio_rxtx.h"
60 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
61 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
63 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0)
67 #define VIRTIO_SIMPLE_FLAGS ((uint32_t)ETH_TXQ_FLAGS_NOMULTSEGS | \
68 ETH_TXQ_FLAGS_NOOFFLOADS)
70 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
71 static int use_simple_rxtx;
75 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
77 struct vring_desc *dp, *dp_tail;
78 struct vq_desc_extra *dxp;
79 uint16_t desc_idx_last = desc_idx;
81 dp = &vq->vq_ring.desc[desc_idx];
82 dxp = &vq->vq_descx[desc_idx];
83 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
84 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
85 while (dp->flags & VRING_DESC_F_NEXT) {
86 desc_idx_last = dp->next;
87 dp = &vq->vq_ring.desc[dp->next];
93 * We must append the existing free chain, if any, to the end of
94 * newly freed chain. If the virtqueue was completely used, then
95 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
97 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
98 vq->vq_desc_head_idx = desc_idx;
100 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
101 dp_tail->next = desc_idx;
104 vq->vq_desc_tail_idx = desc_idx_last;
105 dp->next = VQ_RING_DESC_CHAIN_END;
109 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
110 uint32_t *len, uint16_t num)
112 struct vring_used_elem *uep;
113 struct rte_mbuf *cookie;
114 uint16_t used_idx, desc_idx;
117 /* Caller does the check */
118 for (i = 0; i < num ; i++) {
119 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
120 uep = &vq->vq_ring.used->ring[used_idx];
121 desc_idx = (uint16_t) uep->id;
123 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
125 if (unlikely(cookie == NULL)) {
126 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u\n",
127 vq->vq_used_cons_idx);
131 rte_prefetch0(cookie);
132 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
134 vq->vq_used_cons_idx++;
135 vq_ring_free_chain(vq, desc_idx);
136 vq->vq_descx[desc_idx].cookie = NULL;
142 #ifndef DEFAULT_TX_FREE_THRESH
143 #define DEFAULT_TX_FREE_THRESH 32
146 /* Cleanup from completed transmits. */
148 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
150 uint16_t i, used_idx, desc_idx;
151 for (i = 0; i < num; i++) {
152 struct vring_used_elem *uep;
153 struct vq_desc_extra *dxp;
155 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
156 uep = &vq->vq_ring.used->ring[used_idx];
158 desc_idx = (uint16_t) uep->id;
159 dxp = &vq->vq_descx[desc_idx];
160 vq->vq_used_cons_idx++;
161 vq_ring_free_chain(vq, desc_idx);
163 if (dxp->cookie != NULL) {
164 rte_pktmbuf_free(dxp->cookie);
172 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
174 struct vq_desc_extra *dxp;
175 struct virtio_hw *hw = vq->hw;
176 struct vring_desc *start_dp;
178 uint16_t head_idx, idx;
180 if (unlikely(vq->vq_free_cnt == 0))
182 if (unlikely(vq->vq_free_cnt < needed))
185 head_idx = vq->vq_desc_head_idx;
186 if (unlikely(head_idx >= vq->vq_nentries))
190 dxp = &vq->vq_descx[idx];
191 dxp->cookie = (void *)cookie;
192 dxp->ndescs = needed;
194 start_dp = vq->vq_ring.desc;
196 MBUF_DATA_DMA_ADDR(cookie, vq->offset) - hw->vtnet_hdr_size;
198 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
199 start_dp[idx].flags = VRING_DESC_F_WRITE;
200 idx = start_dp[idx].next;
201 vq->vq_desc_head_idx = idx;
202 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
203 vq->vq_desc_tail_idx = idx;
204 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
205 vq_update_avail_ring(vq, head_idx);
211 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
212 uint16_t needed, int use_indirect, int can_push)
214 struct vq_desc_extra *dxp;
215 struct virtqueue *vq = txvq->vq;
216 struct vring_desc *start_dp;
217 uint16_t seg_num = cookie->nb_segs;
218 uint16_t head_idx, idx;
219 uint16_t head_size = vq->hw->vtnet_hdr_size;
222 head_idx = vq->vq_desc_head_idx;
224 dxp = &vq->vq_descx[idx];
225 dxp->cookie = (void *)cookie;
226 dxp->ndescs = needed;
228 start_dp = vq->vq_ring.desc;
231 /* put on zero'd transmit header (no offloads) */
232 void *hdr = rte_pktmbuf_prepend(cookie, head_size);
234 memset(hdr, 0, head_size);
235 } else if (use_indirect) {
236 /* setup tx ring slot to point to indirect
237 * descriptor list stored in reserved region.
239 * the first slot in indirect ring is already preset
240 * to point to the header in reserved region
242 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
244 offs = idx * sizeof(struct virtio_tx_region)
245 + offsetof(struct virtio_tx_region, tx_indir);
247 start_dp[idx].addr = txvq->virtio_net_hdr_mem + offs;
248 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
249 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
251 /* loop below will fill in rest of the indirect elements */
252 start_dp = txr[idx].tx_indir;
255 /* setup first tx ring slot to point to header
256 * stored in reserved region.
258 offs = idx * sizeof(struct virtio_tx_region)
259 + offsetof(struct virtio_tx_region, tx_hdr);
261 start_dp[idx].addr = txvq->virtio_net_hdr_mem + offs;
262 start_dp[idx].len = vq->hw->vtnet_hdr_size;
263 start_dp[idx].flags = VRING_DESC_F_NEXT;
264 idx = start_dp[idx].next;
268 start_dp[idx].addr = MBUF_DATA_DMA_ADDR(cookie, vq->offset);
269 start_dp[idx].len = cookie->data_len;
270 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
271 idx = start_dp[idx].next;
272 } while ((cookie = cookie->next) != NULL);
275 idx = vq->vq_ring.desc[head_idx].next;
277 vq->vq_desc_head_idx = idx;
278 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
279 vq->vq_desc_tail_idx = idx;
280 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
281 vq_update_avail_ring(vq, head_idx);
285 virtio_dev_vring_start(struct virtqueue *vq)
287 int size = vq->vq_nentries;
288 struct vring *vr = &vq->vq_ring;
289 uint8_t *ring_mem = vq->vq_ring_virt_mem;
291 PMD_INIT_FUNC_TRACE();
294 * Reinitialise since virtio port might have been stopped and restarted
296 memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
297 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
298 vq->vq_used_cons_idx = 0;
299 vq->vq_desc_head_idx = 0;
300 vq->vq_avail_idx = 0;
301 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
302 vq->vq_free_cnt = vq->vq_nentries;
303 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
305 vring_desc_init(vr->desc, size);
308 * Disable device(host) interrupting guest
310 virtqueue_disable_intr(vq);
314 virtio_dev_cq_start(struct rte_eth_dev *dev)
316 struct virtio_hw *hw = dev->data->dev_private;
318 if (hw->cvq && hw->cvq->vq) {
319 virtio_dev_vring_start(hw->cvq->vq);
320 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
325 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
328 * Start receive and transmit vrings
329 * - Setup vring structure for all queues
330 * - Initialize descriptor for the rx vring
331 * - Allocate blank mbufs for the each rx descriptor
337 PMD_INIT_FUNC_TRACE();
339 /* Start rx vring. */
340 for (i = 0; i < dev->data->nb_rx_queues; i++) {
341 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
342 struct virtqueue *vq = rxvq->vq;
346 virtio_dev_vring_start(vq);
347 if (rxvq->mpool == NULL) {
348 rte_exit(EXIT_FAILURE,
349 "Cannot allocate mbufs for rx virtqueue");
352 /* Allocate blank mbufs for the each rx descriptor */
356 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
357 if (use_simple_rxtx) {
358 for (desc_idx = 0; desc_idx < vq->vq_nentries;
360 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
361 vq->vq_ring.desc[desc_idx].flags =
366 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
367 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
369 vq->sw_ring[vq->vq_nentries + desc_idx] =
373 while (!virtqueue_full(vq)) {
374 m = rte_mbuf_raw_alloc(rxvq->mpool);
378 /******************************************
379 * Enqueue allocated buffers *
380 *******************************************/
381 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
383 error = virtqueue_enqueue_recv_refill_simple(vq, m);
386 error = virtqueue_enqueue_recv_refill(vq, m);
394 vq_update_avail_idx(vq);
396 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
401 /* Start tx vring. */
402 for (i = 0; i < dev->data->nb_tx_queues; i++) {
403 struct virtnet_tx *txvq = dev->data->tx_queues[i];
404 struct virtqueue *vq = txvq->vq;
406 virtio_dev_vring_start(vq);
407 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
408 if (use_simple_rxtx) {
409 uint16_t mid_idx = vq->vq_nentries >> 1;
411 for (desc_idx = 0; desc_idx < mid_idx; desc_idx++) {
412 vq->vq_ring.avail->ring[desc_idx] =
414 vq->vq_ring.desc[desc_idx + mid_idx].next =
416 vq->vq_ring.desc[desc_idx + mid_idx].addr =
417 txvq->virtio_net_hdr_mem +
418 offsetof(struct virtio_tx_region, tx_hdr);
419 vq->vq_ring.desc[desc_idx + mid_idx].len =
420 vq->hw->vtnet_hdr_size;
421 vq->vq_ring.desc[desc_idx + mid_idx].flags =
423 vq->vq_ring.desc[desc_idx].flags = 0;
425 for (desc_idx = mid_idx; desc_idx < vq->vq_nentries;
427 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
435 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
438 unsigned int socket_id,
439 __rte_unused const struct rte_eth_rxconf *rx_conf,
440 struct rte_mempool *mp)
442 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
443 struct virtnet_rx *rxvq;
446 PMD_INIT_FUNC_TRACE();
447 ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
448 nb_desc, socket_id, (void **)&rxvq);
450 PMD_INIT_LOG(ERR, "rvq initialization failed");
454 /* Create mempool for rx mbuf allocation */
457 dev->data->rx_queues[queue_idx] = rxvq;
459 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
460 virtio_rxq_vec_setup(rxvq);
467 virtio_dev_rx_queue_release(void *rxq)
469 struct virtnet_rx *rxvq = rxq;
470 struct virtqueue *vq = rxvq->vq;
471 /* rxvq is freed when vq is freed, and as mz should be freed after the
472 * del_queue, so we reserve the mz pointer first.
474 const struct rte_memzone *mz = rxvq->mz;
476 /* no need to free rxq as vq and rxq are allocated together */
477 virtio_dev_queue_release(vq);
478 rte_memzone_free(mz);
482 * struct rte_eth_dev *dev: Used to update dev
483 * uint16_t nb_desc: Defaults to values read from config space
484 * unsigned int socket_id: Used to allocate memzone
485 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
486 * uint16_t queue_idx: Just used as an index in dev txq list
489 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
492 unsigned int socket_id,
493 const struct rte_eth_txconf *tx_conf)
495 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
497 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
498 struct virtio_hw *hw = dev->data->dev_private;
500 struct virtnet_tx *txvq;
501 struct virtqueue *vq;
502 uint16_t tx_free_thresh;
505 PMD_INIT_FUNC_TRACE();
507 if ((tx_conf->txq_flags & ETH_TXQ_FLAGS_NOXSUMS)
508 != ETH_TXQ_FLAGS_NOXSUMS) {
509 PMD_INIT_LOG(ERR, "TX checksum offload not supported\n");
513 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
514 /* Use simple rx/tx func if single segment and no offloads */
515 if ((tx_conf->txq_flags & VIRTIO_SIMPLE_FLAGS) == VIRTIO_SIMPLE_FLAGS &&
516 !vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
517 PMD_INIT_LOG(INFO, "Using simple rx/tx path");
518 dev->tx_pkt_burst = virtio_xmit_pkts_simple;
519 dev->rx_pkt_burst = virtio_recv_pkts_vec;
524 ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
525 nb_desc, socket_id, (void **)&txvq);
527 PMD_INIT_LOG(ERR, "tvq initialization failed");
532 tx_free_thresh = tx_conf->tx_free_thresh;
533 if (tx_free_thresh == 0)
535 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
537 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
538 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
539 "number of TX entries minus 3 (%u)."
540 " (tx_free_thresh=%u port=%u queue=%u)\n",
542 tx_free_thresh, dev->data->port_id, queue_idx);
546 vq->vq_free_thresh = tx_free_thresh;
548 dev->data->tx_queues[queue_idx] = txvq;
553 virtio_dev_tx_queue_release(void *txq)
555 struct virtnet_tx *txvq = txq;
556 struct virtqueue *vq = txvq->vq;
557 /* txvq is freed when vq is freed, and as mz should be freed after the
558 * del_queue, so we reserve the mz pointer first.
560 const struct rte_memzone *hdr_mz = txvq->virtio_net_hdr_mz;
561 const struct rte_memzone *mz = txvq->mz;
563 virtio_dev_queue_release(vq);
564 rte_memzone_free(mz);
565 rte_memzone_free(hdr_mz);
569 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
573 * Requeue the discarded mbuf. This should always be
574 * successful since it was just dequeued.
576 error = virtqueue_enqueue_recv_refill(vq, m);
577 if (unlikely(error)) {
578 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
584 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
586 uint32_t s = mbuf->pkt_len;
587 struct ether_addr *ea;
590 stats->size_bins[1]++;
591 } else if (s > 64 && s < 1024) {
594 /* count zeros, and offset into correct bin */
595 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
596 stats->size_bins[bin]++;
599 stats->size_bins[0]++;
601 stats->size_bins[6]++;
603 stats->size_bins[7]++;
606 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
607 if (is_multicast_ether_addr(ea)) {
608 if (is_broadcast_ether_addr(ea))
615 #define VIRTIO_MBUF_BURST_SZ 64
616 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
618 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
620 struct virtnet_rx *rxvq = rx_queue;
621 struct virtqueue *vq = rxvq->vq;
622 struct virtio_hw *hw;
623 struct rte_mbuf *rxm, *new_mbuf;
624 uint16_t nb_used, num, nb_rx;
625 uint32_t len[VIRTIO_MBUF_BURST_SZ];
626 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
628 uint32_t i, nb_enqueued;
631 nb_used = VIRTQUEUE_NUSED(vq);
635 num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
636 num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
637 if (likely(num > DESC_PER_CACHELINE))
638 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
640 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
641 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
646 hdr_size = hw->vtnet_hdr_size;
648 for (i = 0; i < num ; i++) {
651 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
653 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
654 PMD_RX_LOG(ERR, "Packet drop");
656 virtio_discard_rxbuf(vq, rxm);
657 rxvq->stats.errors++;
661 rxm->port = rxvq->port_id;
662 rxm->data_off = RTE_PKTMBUF_HEADROOM;
668 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
669 rxm->data_len = (uint16_t)(len[i] - hdr_size);
674 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
676 rx_pkts[nb_rx++] = rxm;
678 rxvq->stats.bytes += rx_pkts[nb_rx - 1]->pkt_len;
679 virtio_update_packet_stats(&rxvq->stats, rxm);
682 rxvq->stats.packets += nb_rx;
684 /* Allocate new mbuf for the used descriptor */
686 while (likely(!virtqueue_full(vq))) {
687 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
688 if (unlikely(new_mbuf == NULL)) {
689 struct rte_eth_dev *dev
690 = &rte_eth_devices[rxvq->port_id];
691 dev->data->rx_mbuf_alloc_failed++;
694 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
695 if (unlikely(error)) {
696 rte_pktmbuf_free(new_mbuf);
702 if (likely(nb_enqueued)) {
703 vq_update_avail_idx(vq);
705 if (unlikely(virtqueue_kick_prepare(vq))) {
706 virtqueue_notify(vq);
707 PMD_RX_LOG(DEBUG, "Notified");
715 virtio_recv_mergeable_pkts(void *rx_queue,
716 struct rte_mbuf **rx_pkts,
719 struct virtnet_rx *rxvq = rx_queue;
720 struct virtqueue *vq = rxvq->vq;
721 struct virtio_hw *hw;
722 struct rte_mbuf *rxm, *new_mbuf;
723 uint16_t nb_used, num, nb_rx;
724 uint32_t len[VIRTIO_MBUF_BURST_SZ];
725 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
726 struct rte_mbuf *prev;
728 uint32_t i, nb_enqueued;
734 nb_used = VIRTQUEUE_NUSED(vq);
738 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
747 hdr_size = hw->vtnet_hdr_size;
749 while (i < nb_used) {
750 struct virtio_net_hdr_mrg_rxbuf *header;
752 if (nb_rx == nb_pkts)
755 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, 1);
761 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
762 PMD_RX_LOG(DEBUG, "packet len:%d", len[0]);
766 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
767 PMD_RX_LOG(ERR, "Packet drop");
769 virtio_discard_rxbuf(vq, rxm);
770 rxvq->stats.errors++;
774 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
775 RTE_PKTMBUF_HEADROOM - hdr_size);
776 seg_num = header->num_buffers;
781 rxm->data_off = RTE_PKTMBUF_HEADROOM;
782 rxm->nb_segs = seg_num;
786 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
787 rxm->data_len = (uint16_t)(len[0] - hdr_size);
789 rxm->port = rxvq->port_id;
790 rx_pkts[nb_rx] = rxm;
793 seg_res = seg_num - 1;
795 while (seg_res != 0) {
797 * Get extra segments for current uncompleted packet.
800 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
801 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
803 virtqueue_dequeue_burst_rx(vq,
804 rcv_pkts, len, rcv_cnt);
809 "No enough segments for packet.");
811 virtio_discard_rxbuf(vq, rxm);
812 rxvq->stats.errors++;
818 while (extra_idx < rcv_cnt) {
819 rxm = rcv_pkts[extra_idx];
821 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
823 rxm->pkt_len = (uint32_t)(len[extra_idx]);
824 rxm->data_len = (uint16_t)(len[extra_idx]);
830 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
837 rte_vlan_strip(rx_pkts[nb_rx]);
839 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
840 rx_pkts[nb_rx]->data_len);
842 rxvq->stats.bytes += rx_pkts[nb_rx]->pkt_len;
843 virtio_update_packet_stats(&rxvq->stats, rx_pkts[nb_rx]);
847 rxvq->stats.packets += nb_rx;
849 /* Allocate new mbuf for the used descriptor */
851 while (likely(!virtqueue_full(vq))) {
852 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
853 if (unlikely(new_mbuf == NULL)) {
854 struct rte_eth_dev *dev
855 = &rte_eth_devices[rxvq->port_id];
856 dev->data->rx_mbuf_alloc_failed++;
859 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
860 if (unlikely(error)) {
861 rte_pktmbuf_free(new_mbuf);
867 if (likely(nb_enqueued)) {
868 vq_update_avail_idx(vq);
870 if (unlikely(virtqueue_kick_prepare(vq))) {
871 virtqueue_notify(vq);
872 PMD_RX_LOG(DEBUG, "Notified");
880 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
882 struct virtnet_tx *txvq = tx_queue;
883 struct virtqueue *vq = txvq->vq;
884 struct virtio_hw *hw = vq->hw;
885 uint16_t hdr_size = hw->vtnet_hdr_size;
886 uint16_t nb_used, nb_tx;
889 if (unlikely(nb_pkts < 1))
892 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
893 nb_used = VIRTQUEUE_NUSED(vq);
896 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
897 virtio_xmit_cleanup(vq, nb_used);
899 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
900 struct rte_mbuf *txm = tx_pkts[nb_tx];
901 int can_push = 0, use_indirect = 0, slots, need;
903 /* Do VLAN tag insertion */
904 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
905 error = rte_vlan_insert(&txm);
906 if (unlikely(error)) {
907 rte_pktmbuf_free(txm);
912 /* optimize ring usage */
913 if (vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) &&
914 rte_mbuf_refcnt_read(txm) == 1 &&
915 RTE_MBUF_DIRECT(txm) &&
917 rte_pktmbuf_headroom(txm) >= hdr_size &&
918 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
919 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
921 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
922 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
925 /* How many main ring entries are needed to this Tx?
926 * any_layout => number of segments
928 * default => number of segments + 1
930 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
931 need = slots - vq->vq_free_cnt;
933 /* Positive value indicates it need free vring descriptors */
934 if (unlikely(need > 0)) {
935 nb_used = VIRTQUEUE_NUSED(vq);
937 need = RTE_MIN(need, (int)nb_used);
939 virtio_xmit_cleanup(vq, need);
940 need = slots - vq->vq_free_cnt;
941 if (unlikely(need > 0)) {
943 "No free tx descriptors to transmit");
948 /* Enqueue Packet buffers */
949 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect, can_push);
951 txvq->stats.bytes += txm->pkt_len;
952 virtio_update_packet_stats(&txvq->stats, txm);
955 txvq->stats.packets += nb_tx;
958 vq_update_avail_idx(vq);
960 if (unlikely(virtqueue_kick_prepare(vq))) {
961 virtqueue_notify(vq);
962 PMD_TX_LOG(DEBUG, "Notified backend after xmit");