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 VIRTIO_MBUF_ADDR(cookie, vq) +
197 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
199 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
200 start_dp[idx].flags = VRING_DESC_F_WRITE;
201 idx = start_dp[idx].next;
202 vq->vq_desc_head_idx = idx;
203 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
204 vq->vq_desc_tail_idx = idx;
205 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
206 vq_update_avail_ring(vq, head_idx);
212 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
213 uint16_t needed, int use_indirect, int can_push)
215 struct vq_desc_extra *dxp;
216 struct virtqueue *vq = txvq->vq;
217 struct vring_desc *start_dp;
218 uint16_t seg_num = cookie->nb_segs;
219 uint16_t head_idx, idx;
220 uint16_t head_size = vq->hw->vtnet_hdr_size;
223 head_idx = vq->vq_desc_head_idx;
225 dxp = &vq->vq_descx[idx];
226 dxp->cookie = (void *)cookie;
227 dxp->ndescs = needed;
229 start_dp = vq->vq_ring.desc;
232 /* put on zero'd transmit header (no offloads) */
233 void *hdr = rte_pktmbuf_prepend(cookie, head_size);
235 memset(hdr, 0, head_size);
236 } else if (use_indirect) {
237 /* setup tx ring slot to point to indirect
238 * descriptor list stored in reserved region.
240 * the first slot in indirect ring is already preset
241 * to point to the header in reserved region
243 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
245 offs = idx * sizeof(struct virtio_tx_region)
246 + offsetof(struct virtio_tx_region, tx_indir);
248 start_dp[idx].addr = txvq->virtio_net_hdr_mem + offs;
249 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
250 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
252 /* loop below will fill in rest of the indirect elements */
253 start_dp = txr[idx].tx_indir;
256 /* setup first tx ring slot to point to header
257 * stored in reserved region.
259 offs = idx * sizeof(struct virtio_tx_region)
260 + offsetof(struct virtio_tx_region, tx_hdr);
262 start_dp[idx].addr = txvq->virtio_net_hdr_mem + offs;
263 start_dp[idx].len = vq->hw->vtnet_hdr_size;
264 start_dp[idx].flags = VRING_DESC_F_NEXT;
265 idx = start_dp[idx].next;
269 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
270 start_dp[idx].len = cookie->data_len;
271 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
272 idx = start_dp[idx].next;
273 } while ((cookie = cookie->next) != NULL);
276 idx = vq->vq_ring.desc[head_idx].next;
278 vq->vq_desc_head_idx = idx;
279 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
280 vq->vq_desc_tail_idx = idx;
281 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
282 vq_update_avail_ring(vq, head_idx);
286 virtio_dev_vring_start(struct virtqueue *vq)
288 int size = vq->vq_nentries;
289 struct vring *vr = &vq->vq_ring;
290 uint8_t *ring_mem = vq->vq_ring_virt_mem;
292 PMD_INIT_FUNC_TRACE();
295 * Reinitialise since virtio port might have been stopped and restarted
297 memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
298 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
299 vq->vq_used_cons_idx = 0;
300 vq->vq_desc_head_idx = 0;
301 vq->vq_avail_idx = 0;
302 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
303 vq->vq_free_cnt = vq->vq_nentries;
304 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
306 vring_desc_init(vr->desc, size);
309 * Disable device(host) interrupting guest
311 virtqueue_disable_intr(vq);
315 virtio_dev_cq_start(struct rte_eth_dev *dev)
317 struct virtio_hw *hw = dev->data->dev_private;
319 if (hw->cvq && hw->cvq->vq) {
320 virtio_dev_vring_start(hw->cvq->vq);
321 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
326 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
329 * Start receive and transmit vrings
330 * - Setup vring structure for all queues
331 * - Initialize descriptor for the rx vring
332 * - Allocate blank mbufs for the each rx descriptor
338 PMD_INIT_FUNC_TRACE();
340 /* Start rx vring. */
341 for (i = 0; i < dev->data->nb_rx_queues; i++) {
342 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
343 struct virtqueue *vq = rxvq->vq;
347 virtio_dev_vring_start(vq);
348 if (rxvq->mpool == NULL) {
349 rte_exit(EXIT_FAILURE,
350 "Cannot allocate mbufs for rx virtqueue");
353 /* Allocate blank mbufs for the each rx descriptor */
357 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
358 if (use_simple_rxtx) {
359 for (desc_idx = 0; desc_idx < vq->vq_nentries;
361 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
362 vq->vq_ring.desc[desc_idx].flags =
367 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
368 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
370 vq->sw_ring[vq->vq_nentries + desc_idx] =
374 while (!virtqueue_full(vq)) {
375 m = rte_mbuf_raw_alloc(rxvq->mpool);
379 /******************************************
380 * Enqueue allocated buffers *
381 *******************************************/
382 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
384 error = virtqueue_enqueue_recv_refill_simple(vq, m);
387 error = virtqueue_enqueue_recv_refill(vq, m);
395 vq_update_avail_idx(vq);
397 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
402 /* Start tx vring. */
403 for (i = 0; i < dev->data->nb_tx_queues; i++) {
404 struct virtnet_tx *txvq = dev->data->tx_queues[i];
405 struct virtqueue *vq = txvq->vq;
407 virtio_dev_vring_start(vq);
408 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
409 if (use_simple_rxtx) {
410 uint16_t mid_idx = vq->vq_nentries >> 1;
412 for (desc_idx = 0; desc_idx < mid_idx; desc_idx++) {
413 vq->vq_ring.avail->ring[desc_idx] =
415 vq->vq_ring.desc[desc_idx + mid_idx].next =
417 vq->vq_ring.desc[desc_idx + mid_idx].addr =
418 txvq->virtio_net_hdr_mem +
419 offsetof(struct virtio_tx_region, tx_hdr);
420 vq->vq_ring.desc[desc_idx + mid_idx].len =
421 vq->hw->vtnet_hdr_size;
422 vq->vq_ring.desc[desc_idx + mid_idx].flags =
424 vq->vq_ring.desc[desc_idx].flags = 0;
426 for (desc_idx = mid_idx; desc_idx < vq->vq_nentries;
428 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
436 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
439 unsigned int socket_id,
440 __rte_unused const struct rte_eth_rxconf *rx_conf,
441 struct rte_mempool *mp)
443 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
444 struct virtnet_rx *rxvq;
447 PMD_INIT_FUNC_TRACE();
448 ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
449 nb_desc, socket_id, (void **)&rxvq);
451 PMD_INIT_LOG(ERR, "rvq initialization failed");
455 /* Create mempool for rx mbuf allocation */
458 dev->data->rx_queues[queue_idx] = rxvq;
460 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
461 virtio_rxq_vec_setup(rxvq);
468 virtio_dev_rx_queue_release(void *rxq)
470 struct virtnet_rx *rxvq = rxq;
471 struct virtqueue *vq;
472 const struct rte_memzone *mz;
478 * rxvq is freed when vq is freed, and as mz should be freed after the
479 * del_queue, so we reserve the mz pointer first.
484 virtio_dev_queue_release(vq);
485 rte_memzone_free(mz);
489 * struct rte_eth_dev *dev: Used to update dev
490 * uint16_t nb_desc: Defaults to values read from config space
491 * unsigned int socket_id: Used to allocate memzone
492 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
493 * uint16_t queue_idx: Just used as an index in dev txq list
496 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
499 unsigned int socket_id,
500 const struct rte_eth_txconf *tx_conf)
502 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
504 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
505 struct virtio_hw *hw = dev->data->dev_private;
507 struct virtnet_tx *txvq;
508 struct virtqueue *vq;
509 uint16_t tx_free_thresh;
512 PMD_INIT_FUNC_TRACE();
514 if ((tx_conf->txq_flags & ETH_TXQ_FLAGS_NOXSUMS)
515 != ETH_TXQ_FLAGS_NOXSUMS) {
516 PMD_INIT_LOG(ERR, "TX checksum offload not supported\n");
520 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
521 /* Use simple rx/tx func if single segment and no offloads */
522 if ((tx_conf->txq_flags & VIRTIO_SIMPLE_FLAGS) == VIRTIO_SIMPLE_FLAGS &&
523 !vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
524 PMD_INIT_LOG(INFO, "Using simple rx/tx path");
525 dev->tx_pkt_burst = virtio_xmit_pkts_simple;
526 dev->rx_pkt_burst = virtio_recv_pkts_vec;
531 ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
532 nb_desc, socket_id, (void **)&txvq);
534 PMD_INIT_LOG(ERR, "tvq initialization failed");
539 tx_free_thresh = tx_conf->tx_free_thresh;
540 if (tx_free_thresh == 0)
542 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
544 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
545 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
546 "number of TX entries minus 3 (%u)."
547 " (tx_free_thresh=%u port=%u queue=%u)\n",
549 tx_free_thresh, dev->data->port_id, queue_idx);
553 vq->vq_free_thresh = tx_free_thresh;
555 dev->data->tx_queues[queue_idx] = txvq;
560 virtio_dev_tx_queue_release(void *txq)
562 struct virtnet_tx *txvq = txq;
563 struct virtqueue *vq;
564 const struct rte_memzone *mz;
565 const struct rte_memzone *hdr_mz;
571 * txvq is freed when vq is freed, and as mz should be freed after the
572 * del_queue, so we reserve the mz pointer first.
576 hdr_mz = txvq->virtio_net_hdr_mz;
578 virtio_dev_queue_release(vq);
579 rte_memzone_free(mz);
580 rte_memzone_free(hdr_mz);
584 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
588 * Requeue the discarded mbuf. This should always be
589 * successful since it was just dequeued.
591 error = virtqueue_enqueue_recv_refill(vq, m);
592 if (unlikely(error)) {
593 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
599 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
601 uint32_t s = mbuf->pkt_len;
602 struct ether_addr *ea;
605 stats->size_bins[1]++;
606 } else if (s > 64 && s < 1024) {
609 /* count zeros, and offset into correct bin */
610 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
611 stats->size_bins[bin]++;
614 stats->size_bins[0]++;
616 stats->size_bins[6]++;
618 stats->size_bins[7]++;
621 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
622 if (is_multicast_ether_addr(ea)) {
623 if (is_broadcast_ether_addr(ea))
630 #define VIRTIO_MBUF_BURST_SZ 64
631 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
633 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
635 struct virtnet_rx *rxvq = rx_queue;
636 struct virtqueue *vq = rxvq->vq;
637 struct virtio_hw *hw;
638 struct rte_mbuf *rxm, *new_mbuf;
639 uint16_t nb_used, num, nb_rx;
640 uint32_t len[VIRTIO_MBUF_BURST_SZ];
641 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
643 uint32_t i, nb_enqueued;
646 nb_used = VIRTQUEUE_NUSED(vq);
650 num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
651 num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
652 if (likely(num > DESC_PER_CACHELINE))
653 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
655 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
656 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
661 hdr_size = hw->vtnet_hdr_size;
663 for (i = 0; i < num ; i++) {
666 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
668 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
669 PMD_RX_LOG(ERR, "Packet drop");
671 virtio_discard_rxbuf(vq, rxm);
672 rxvq->stats.errors++;
676 rxm->port = rxvq->port_id;
677 rxm->data_off = RTE_PKTMBUF_HEADROOM;
683 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
684 rxm->data_len = (uint16_t)(len[i] - hdr_size);
689 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
691 rx_pkts[nb_rx++] = rxm;
693 rxvq->stats.bytes += rx_pkts[nb_rx - 1]->pkt_len;
694 virtio_update_packet_stats(&rxvq->stats, rxm);
697 rxvq->stats.packets += nb_rx;
699 /* Allocate new mbuf for the used descriptor */
701 while (likely(!virtqueue_full(vq))) {
702 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
703 if (unlikely(new_mbuf == NULL)) {
704 struct rte_eth_dev *dev
705 = &rte_eth_devices[rxvq->port_id];
706 dev->data->rx_mbuf_alloc_failed++;
709 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
710 if (unlikely(error)) {
711 rte_pktmbuf_free(new_mbuf);
717 if (likely(nb_enqueued)) {
718 vq_update_avail_idx(vq);
720 if (unlikely(virtqueue_kick_prepare(vq))) {
721 virtqueue_notify(vq);
722 PMD_RX_LOG(DEBUG, "Notified");
730 virtio_recv_mergeable_pkts(void *rx_queue,
731 struct rte_mbuf **rx_pkts,
734 struct virtnet_rx *rxvq = rx_queue;
735 struct virtqueue *vq = rxvq->vq;
736 struct virtio_hw *hw;
737 struct rte_mbuf *rxm, *new_mbuf;
738 uint16_t nb_used, num, nb_rx;
739 uint32_t len[VIRTIO_MBUF_BURST_SZ];
740 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
741 struct rte_mbuf *prev;
743 uint32_t i, nb_enqueued;
749 nb_used = VIRTQUEUE_NUSED(vq);
753 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
762 hdr_size = hw->vtnet_hdr_size;
764 while (i < nb_used) {
765 struct virtio_net_hdr_mrg_rxbuf *header;
767 if (nb_rx == nb_pkts)
770 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, 1);
776 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
777 PMD_RX_LOG(DEBUG, "packet len:%d", len[0]);
781 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
782 PMD_RX_LOG(ERR, "Packet drop");
784 virtio_discard_rxbuf(vq, rxm);
785 rxvq->stats.errors++;
789 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
790 RTE_PKTMBUF_HEADROOM - hdr_size);
791 seg_num = header->num_buffers;
796 rxm->data_off = RTE_PKTMBUF_HEADROOM;
797 rxm->nb_segs = seg_num;
801 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
802 rxm->data_len = (uint16_t)(len[0] - hdr_size);
804 rxm->port = rxvq->port_id;
805 rx_pkts[nb_rx] = rxm;
808 seg_res = seg_num - 1;
810 while (seg_res != 0) {
812 * Get extra segments for current uncompleted packet.
815 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
816 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
818 virtqueue_dequeue_burst_rx(vq,
819 rcv_pkts, len, rcv_cnt);
824 "No enough segments for packet.");
826 virtio_discard_rxbuf(vq, rxm);
827 rxvq->stats.errors++;
833 while (extra_idx < rcv_cnt) {
834 rxm = rcv_pkts[extra_idx];
836 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
838 rxm->pkt_len = (uint32_t)(len[extra_idx]);
839 rxm->data_len = (uint16_t)(len[extra_idx]);
845 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
852 rte_vlan_strip(rx_pkts[nb_rx]);
854 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
855 rx_pkts[nb_rx]->data_len);
857 rxvq->stats.bytes += rx_pkts[nb_rx]->pkt_len;
858 virtio_update_packet_stats(&rxvq->stats, rx_pkts[nb_rx]);
862 rxvq->stats.packets += nb_rx;
864 /* Allocate new mbuf for the used descriptor */
866 while (likely(!virtqueue_full(vq))) {
867 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
868 if (unlikely(new_mbuf == NULL)) {
869 struct rte_eth_dev *dev
870 = &rte_eth_devices[rxvq->port_id];
871 dev->data->rx_mbuf_alloc_failed++;
874 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
875 if (unlikely(error)) {
876 rte_pktmbuf_free(new_mbuf);
882 if (likely(nb_enqueued)) {
883 vq_update_avail_idx(vq);
885 if (unlikely(virtqueue_kick_prepare(vq))) {
886 virtqueue_notify(vq);
887 PMD_RX_LOG(DEBUG, "Notified");
895 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
897 struct virtnet_tx *txvq = tx_queue;
898 struct virtqueue *vq = txvq->vq;
899 struct virtio_hw *hw = vq->hw;
900 uint16_t hdr_size = hw->vtnet_hdr_size;
901 uint16_t nb_used, nb_tx;
904 if (unlikely(nb_pkts < 1))
907 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
908 nb_used = VIRTQUEUE_NUSED(vq);
911 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
912 virtio_xmit_cleanup(vq, nb_used);
914 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
915 struct rte_mbuf *txm = tx_pkts[nb_tx];
916 int can_push = 0, use_indirect = 0, slots, need;
918 /* Do VLAN tag insertion */
919 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
920 error = rte_vlan_insert(&txm);
921 if (unlikely(error)) {
922 rte_pktmbuf_free(txm);
927 /* optimize ring usage */
928 if (vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) &&
929 rte_mbuf_refcnt_read(txm) == 1 &&
930 RTE_MBUF_DIRECT(txm) &&
932 rte_pktmbuf_headroom(txm) >= hdr_size &&
933 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
934 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
936 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
937 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
940 /* How many main ring entries are needed to this Tx?
941 * any_layout => number of segments
943 * default => number of segments + 1
945 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
946 need = slots - vq->vq_free_cnt;
948 /* Positive value indicates it need free vring descriptors */
949 if (unlikely(need > 0)) {
950 nb_used = VIRTQUEUE_NUSED(vq);
952 need = RTE_MIN(need, (int)nb_used);
954 virtio_xmit_cleanup(vq, need);
955 need = slots - vq->vq_free_cnt;
956 if (unlikely(need > 0)) {
958 "No free tx descriptors to transmit");
963 /* Enqueue Packet buffers */
964 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect, can_push);
966 txvq->stats.bytes += txm->pkt_len;
967 virtio_update_packet_stats(&txvq->stats, txm);
970 txvq->stats.packets += nb_tx;
973 vq_update_avail_idx(vq);
975 if (unlikely(virtqueue_kick_prepare(vq))) {
976 virtqueue_notify(vq);
977 PMD_TX_LOG(DEBUG, "Notified backend after xmit");