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)
71 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
73 struct vring_desc *dp, *dp_tail;
74 struct vq_desc_extra *dxp;
75 uint16_t desc_idx_last = desc_idx;
77 dp = &vq->vq_ring.desc[desc_idx];
78 dxp = &vq->vq_descx[desc_idx];
79 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
80 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
81 while (dp->flags & VRING_DESC_F_NEXT) {
82 desc_idx_last = dp->next;
83 dp = &vq->vq_ring.desc[dp->next];
89 * We must append the existing free chain, if any, to the end of
90 * newly freed chain. If the virtqueue was completely used, then
91 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
93 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
94 vq->vq_desc_head_idx = desc_idx;
96 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
97 dp_tail->next = desc_idx;
100 vq->vq_desc_tail_idx = desc_idx_last;
101 dp->next = VQ_RING_DESC_CHAIN_END;
105 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
106 uint32_t *len, uint16_t num)
108 struct vring_used_elem *uep;
109 struct rte_mbuf *cookie;
110 uint16_t used_idx, desc_idx;
113 /* Caller does the check */
114 for (i = 0; i < num ; i++) {
115 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
116 uep = &vq->vq_ring.used->ring[used_idx];
117 desc_idx = (uint16_t) uep->id;
119 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
121 if (unlikely(cookie == NULL)) {
122 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u\n",
123 vq->vq_used_cons_idx);
127 rte_prefetch0(cookie);
128 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
130 vq->vq_used_cons_idx++;
131 vq_ring_free_chain(vq, desc_idx);
132 vq->vq_descx[desc_idx].cookie = NULL;
138 #ifndef DEFAULT_TX_FREE_THRESH
139 #define DEFAULT_TX_FREE_THRESH 32
142 /* Cleanup from completed transmits. */
144 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
146 uint16_t i, used_idx, desc_idx;
147 for (i = 0; i < num; i++) {
148 struct vring_used_elem *uep;
149 struct vq_desc_extra *dxp;
151 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
152 uep = &vq->vq_ring.used->ring[used_idx];
154 desc_idx = (uint16_t) uep->id;
155 dxp = &vq->vq_descx[desc_idx];
156 vq->vq_used_cons_idx++;
157 vq_ring_free_chain(vq, desc_idx);
159 if (dxp->cookie != NULL) {
160 rte_pktmbuf_free(dxp->cookie);
168 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
170 struct vq_desc_extra *dxp;
171 struct virtio_hw *hw = vq->hw;
172 struct vring_desc *start_dp;
174 uint16_t head_idx, idx;
176 if (unlikely(vq->vq_free_cnt == 0))
178 if (unlikely(vq->vq_free_cnt < needed))
181 head_idx = vq->vq_desc_head_idx;
182 if (unlikely(head_idx >= vq->vq_nentries))
186 dxp = &vq->vq_descx[idx];
187 dxp->cookie = (void *)cookie;
188 dxp->ndescs = needed;
190 start_dp = vq->vq_ring.desc;
192 VIRTIO_MBUF_ADDR(cookie, vq) +
193 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
195 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
196 start_dp[idx].flags = VRING_DESC_F_WRITE;
197 idx = start_dp[idx].next;
198 vq->vq_desc_head_idx = idx;
199 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
200 vq->vq_desc_tail_idx = idx;
201 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
202 vq_update_avail_ring(vq, head_idx);
208 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
209 uint16_t needed, int use_indirect, int can_push)
211 struct vq_desc_extra *dxp;
212 struct virtqueue *vq = txvq->vq;
213 struct vring_desc *start_dp;
214 uint16_t seg_num = cookie->nb_segs;
215 uint16_t head_idx, idx;
216 uint16_t head_size = vq->hw->vtnet_hdr_size;
219 head_idx = vq->vq_desc_head_idx;
221 dxp = &vq->vq_descx[idx];
222 dxp->cookie = (void *)cookie;
223 dxp->ndescs = needed;
225 start_dp = vq->vq_ring.desc;
228 /* put on zero'd transmit header (no offloads) */
229 void *hdr = rte_pktmbuf_prepend(cookie, head_size);
231 memset(hdr, 0, head_size);
232 } else if (use_indirect) {
233 /* setup tx ring slot to point to indirect
234 * descriptor list stored in reserved region.
236 * the first slot in indirect ring is already preset
237 * to point to the header in reserved region
239 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
241 offs = idx * sizeof(struct virtio_tx_region)
242 + offsetof(struct virtio_tx_region, tx_indir);
244 start_dp[idx].addr = txvq->virtio_net_hdr_mem + offs;
245 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
246 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
248 /* loop below will fill in rest of the indirect elements */
249 start_dp = txr[idx].tx_indir;
252 /* setup first tx ring slot to point to header
253 * stored in reserved region.
255 offs = idx * sizeof(struct virtio_tx_region)
256 + offsetof(struct virtio_tx_region, tx_hdr);
258 start_dp[idx].addr = txvq->virtio_net_hdr_mem + offs;
259 start_dp[idx].len = vq->hw->vtnet_hdr_size;
260 start_dp[idx].flags = VRING_DESC_F_NEXT;
261 idx = start_dp[idx].next;
265 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
266 start_dp[idx].len = cookie->data_len;
267 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
268 idx = start_dp[idx].next;
269 } while ((cookie = cookie->next) != NULL);
272 idx = vq->vq_ring.desc[head_idx].next;
274 vq->vq_desc_head_idx = idx;
275 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
276 vq->vq_desc_tail_idx = idx;
277 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
278 vq_update_avail_ring(vq, head_idx);
282 virtio_dev_vring_start(struct virtqueue *vq)
284 int size = vq->vq_nentries;
285 struct vring *vr = &vq->vq_ring;
286 uint8_t *ring_mem = vq->vq_ring_virt_mem;
288 PMD_INIT_FUNC_TRACE();
291 * Reinitialise since virtio port might have been stopped and restarted
293 memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
294 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
295 vq->vq_used_cons_idx = 0;
296 vq->vq_desc_head_idx = 0;
297 vq->vq_avail_idx = 0;
298 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
299 vq->vq_free_cnt = vq->vq_nentries;
300 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
302 vring_desc_init(vr->desc, size);
305 * Disable device(host) interrupting guest
307 virtqueue_disable_intr(vq);
311 virtio_dev_cq_start(struct rte_eth_dev *dev)
313 struct virtio_hw *hw = dev->data->dev_private;
315 if (hw->cvq && hw->cvq->vq) {
316 virtio_dev_vring_start(hw->cvq->vq);
317 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
322 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
325 * Start receive and transmit vrings
326 * - Setup vring structure for all queues
327 * - Initialize descriptor for the rx vring
328 * - Allocate blank mbufs for the each rx descriptor
333 struct virtio_hw *hw = dev->data->dev_private;
335 PMD_INIT_FUNC_TRACE();
337 /* Start rx vring. */
338 for (i = 0; i < dev->data->nb_rx_queues; i++) {
339 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
340 struct virtqueue *vq = rxvq->vq;
344 virtio_dev_vring_start(vq);
345 if (rxvq->mpool == NULL) {
346 rte_exit(EXIT_FAILURE,
347 "Cannot allocate mbufs for rx virtqueue");
350 /* Allocate blank mbufs for the each rx descriptor */
354 if (hw->use_simple_rxtx) {
355 for (desc_idx = 0; desc_idx < vq->vq_nentries;
357 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
358 vq->vq_ring.desc[desc_idx].flags =
363 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
364 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
366 vq->sw_ring[vq->vq_nentries + desc_idx] =
370 while (!virtqueue_full(vq)) {
371 m = rte_mbuf_raw_alloc(rxvq->mpool);
375 /******************************************
376 * Enqueue allocated buffers *
377 *******************************************/
378 if (hw->use_simple_rxtx)
379 error = virtqueue_enqueue_recv_refill_simple(vq, m);
381 error = virtqueue_enqueue_recv_refill(vq, m);
390 vq_update_avail_idx(vq);
392 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
397 /* Start tx vring. */
398 for (i = 0; i < dev->data->nb_tx_queues; i++) {
399 struct virtnet_tx *txvq = dev->data->tx_queues[i];
400 struct virtqueue *vq = txvq->vq;
402 virtio_dev_vring_start(vq);
403 if (hw->use_simple_rxtx) {
404 uint16_t mid_idx = vq->vq_nentries >> 1;
406 for (desc_idx = 0; desc_idx < mid_idx; desc_idx++) {
407 vq->vq_ring.avail->ring[desc_idx] =
409 vq->vq_ring.desc[desc_idx + mid_idx].next =
411 vq->vq_ring.desc[desc_idx + mid_idx].addr =
412 txvq->virtio_net_hdr_mem +
413 offsetof(struct virtio_tx_region, tx_hdr);
414 vq->vq_ring.desc[desc_idx + mid_idx].len =
415 vq->hw->vtnet_hdr_size;
416 vq->vq_ring.desc[desc_idx + mid_idx].flags =
418 vq->vq_ring.desc[desc_idx].flags = 0;
420 for (desc_idx = mid_idx; desc_idx < vq->vq_nentries;
422 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
430 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
433 unsigned int socket_id,
434 __rte_unused const struct rte_eth_rxconf *rx_conf,
435 struct rte_mempool *mp)
437 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
438 struct virtnet_rx *rxvq;
441 PMD_INIT_FUNC_TRACE();
442 ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
443 nb_desc, socket_id, (void **)&rxvq);
445 PMD_INIT_LOG(ERR, "rvq initialization failed");
449 /* Create mempool for rx mbuf allocation */
452 dev->data->rx_queues[queue_idx] = rxvq;
454 virtio_rxq_vec_setup(rxvq);
460 virtio_dev_rx_queue_release(void *rxq)
462 struct virtnet_rx *rxvq = rxq;
463 struct virtqueue *vq;
464 const struct rte_memzone *mz;
470 * rxvq is freed when vq is freed, and as mz should be freed after the
471 * del_queue, so we reserve the mz pointer first.
476 virtio_dev_queue_release(vq);
477 rte_memzone_free(mz);
481 * struct rte_eth_dev *dev: Used to update dev
482 * uint16_t nb_desc: Defaults to values read from config space
483 * unsigned int socket_id: Used to allocate memzone
484 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
485 * uint16_t queue_idx: Just used as an index in dev txq list
488 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
491 unsigned int socket_id,
492 const struct rte_eth_txconf *tx_conf)
494 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
496 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
497 struct virtio_hw *hw = dev->data->dev_private;
499 struct virtnet_tx *txvq;
500 struct virtqueue *vq;
501 uint16_t tx_free_thresh;
504 PMD_INIT_FUNC_TRACE();
506 if ((tx_conf->txq_flags & ETH_TXQ_FLAGS_NOXSUMS)
507 != ETH_TXQ_FLAGS_NOXSUMS) {
508 PMD_INIT_LOG(ERR, "TX checksum offload not supported\n");
512 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
513 /* Use simple rx/tx func if single segment and no offloads */
514 if ((tx_conf->txq_flags & VIRTIO_SIMPLE_FLAGS) == VIRTIO_SIMPLE_FLAGS &&
515 !vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
516 PMD_INIT_LOG(INFO, "Using simple rx/tx path");
517 dev->tx_pkt_burst = virtio_xmit_pkts_simple;
518 dev->rx_pkt_burst = virtio_recv_pkts_vec;
519 hw->use_simple_rxtx = 1;
523 ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
524 nb_desc, socket_id, (void **)&txvq);
526 PMD_INIT_LOG(ERR, "tvq initialization failed");
531 tx_free_thresh = tx_conf->tx_free_thresh;
532 if (tx_free_thresh == 0)
534 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
536 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
537 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
538 "number of TX entries minus 3 (%u)."
539 " (tx_free_thresh=%u port=%u queue=%u)\n",
541 tx_free_thresh, dev->data->port_id, queue_idx);
545 vq->vq_free_thresh = tx_free_thresh;
547 dev->data->tx_queues[queue_idx] = txvq;
552 virtio_dev_tx_queue_release(void *txq)
554 struct virtnet_tx *txvq = txq;
555 struct virtqueue *vq;
556 const struct rte_memzone *mz;
557 const struct rte_memzone *hdr_mz;
563 * txvq is freed when vq is freed, and as mz should be freed after the
564 * del_queue, so we reserve the mz pointer first.
568 hdr_mz = txvq->virtio_net_hdr_mz;
570 virtio_dev_queue_release(vq);
571 rte_memzone_free(mz);
572 rte_memzone_free(hdr_mz);
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 #define VIRTIO_MBUF_BURST_SZ 64
623 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
625 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
627 struct virtnet_rx *rxvq = rx_queue;
628 struct virtqueue *vq = rxvq->vq;
629 struct virtio_hw *hw;
630 struct rte_mbuf *rxm, *new_mbuf;
631 uint16_t nb_used, num, nb_rx;
632 uint32_t len[VIRTIO_MBUF_BURST_SZ];
633 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
635 uint32_t i, nb_enqueued;
638 nb_used = VIRTQUEUE_NUSED(vq);
642 num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
643 num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
644 if (likely(num > DESC_PER_CACHELINE))
645 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
647 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
648 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
653 hdr_size = hw->vtnet_hdr_size;
655 for (i = 0; i < num ; i++) {
658 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
660 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
661 PMD_RX_LOG(ERR, "Packet drop");
663 virtio_discard_rxbuf(vq, rxm);
664 rxvq->stats.errors++;
668 rxm->port = rxvq->port_id;
669 rxm->data_off = RTE_PKTMBUF_HEADROOM;
675 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
676 rxm->data_len = (uint16_t)(len[i] - hdr_size);
681 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
683 rx_pkts[nb_rx++] = rxm;
685 rxvq->stats.bytes += rx_pkts[nb_rx - 1]->pkt_len;
686 virtio_update_packet_stats(&rxvq->stats, rxm);
689 rxvq->stats.packets += nb_rx;
691 /* Allocate new mbuf for the used descriptor */
693 while (likely(!virtqueue_full(vq))) {
694 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
695 if (unlikely(new_mbuf == NULL)) {
696 struct rte_eth_dev *dev
697 = &rte_eth_devices[rxvq->port_id];
698 dev->data->rx_mbuf_alloc_failed++;
701 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
702 if (unlikely(error)) {
703 rte_pktmbuf_free(new_mbuf);
709 if (likely(nb_enqueued)) {
710 vq_update_avail_idx(vq);
712 if (unlikely(virtqueue_kick_prepare(vq))) {
713 virtqueue_notify(vq);
714 PMD_RX_LOG(DEBUG, "Notified");
722 virtio_recv_mergeable_pkts(void *rx_queue,
723 struct rte_mbuf **rx_pkts,
726 struct virtnet_rx *rxvq = rx_queue;
727 struct virtqueue *vq = rxvq->vq;
728 struct virtio_hw *hw;
729 struct rte_mbuf *rxm, *new_mbuf;
730 uint16_t nb_used, num, nb_rx;
731 uint32_t len[VIRTIO_MBUF_BURST_SZ];
732 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
733 struct rte_mbuf *prev;
735 uint32_t i, nb_enqueued;
741 nb_used = VIRTQUEUE_NUSED(vq);
745 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
754 hdr_size = hw->vtnet_hdr_size;
756 while (i < nb_used) {
757 struct virtio_net_hdr_mrg_rxbuf *header;
759 if (nb_rx == nb_pkts)
762 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, 1);
768 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
769 PMD_RX_LOG(DEBUG, "packet len:%d", len[0]);
773 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
774 PMD_RX_LOG(ERR, "Packet drop");
776 virtio_discard_rxbuf(vq, rxm);
777 rxvq->stats.errors++;
781 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
782 RTE_PKTMBUF_HEADROOM - hdr_size);
783 seg_num = header->num_buffers;
788 rxm->data_off = RTE_PKTMBUF_HEADROOM;
789 rxm->nb_segs = seg_num;
793 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
794 rxm->data_len = (uint16_t)(len[0] - hdr_size);
796 rxm->port = rxvq->port_id;
797 rx_pkts[nb_rx] = rxm;
800 seg_res = seg_num - 1;
802 while (seg_res != 0) {
804 * Get extra segments for current uncompleted packet.
807 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
808 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
810 virtqueue_dequeue_burst_rx(vq,
811 rcv_pkts, len, rcv_cnt);
816 "No enough segments for packet.");
818 virtio_discard_rxbuf(vq, rxm);
819 rxvq->stats.errors++;
825 while (extra_idx < rcv_cnt) {
826 rxm = rcv_pkts[extra_idx];
828 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
830 rxm->pkt_len = (uint32_t)(len[extra_idx]);
831 rxm->data_len = (uint16_t)(len[extra_idx]);
837 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
844 rte_vlan_strip(rx_pkts[nb_rx]);
846 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
847 rx_pkts[nb_rx]->data_len);
849 rxvq->stats.bytes += rx_pkts[nb_rx]->pkt_len;
850 virtio_update_packet_stats(&rxvq->stats, rx_pkts[nb_rx]);
854 rxvq->stats.packets += nb_rx;
856 /* Allocate new mbuf for the used descriptor */
858 while (likely(!virtqueue_full(vq))) {
859 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
860 if (unlikely(new_mbuf == NULL)) {
861 struct rte_eth_dev *dev
862 = &rte_eth_devices[rxvq->port_id];
863 dev->data->rx_mbuf_alloc_failed++;
866 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
867 if (unlikely(error)) {
868 rte_pktmbuf_free(new_mbuf);
874 if (likely(nb_enqueued)) {
875 vq_update_avail_idx(vq);
877 if (unlikely(virtqueue_kick_prepare(vq))) {
878 virtqueue_notify(vq);
879 PMD_RX_LOG(DEBUG, "Notified");
887 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
889 struct virtnet_tx *txvq = tx_queue;
890 struct virtqueue *vq = txvq->vq;
891 struct virtio_hw *hw = vq->hw;
892 uint16_t hdr_size = hw->vtnet_hdr_size;
893 uint16_t nb_used, nb_tx;
896 if (unlikely(nb_pkts < 1))
899 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
900 nb_used = VIRTQUEUE_NUSED(vq);
903 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
904 virtio_xmit_cleanup(vq, nb_used);
906 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
907 struct rte_mbuf *txm = tx_pkts[nb_tx];
908 int can_push = 0, use_indirect = 0, slots, need;
910 /* Do VLAN tag insertion */
911 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
912 error = rte_vlan_insert(&txm);
913 if (unlikely(error)) {
914 rte_pktmbuf_free(txm);
919 /* optimize ring usage */
920 if (vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) &&
921 rte_mbuf_refcnt_read(txm) == 1 &&
922 RTE_MBUF_DIRECT(txm) &&
924 rte_pktmbuf_headroom(txm) >= hdr_size &&
925 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
926 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
928 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
929 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
932 /* How many main ring entries are needed to this Tx?
933 * any_layout => number of segments
935 * default => number of segments + 1
937 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
938 need = slots - vq->vq_free_cnt;
940 /* Positive value indicates it need free vring descriptors */
941 if (unlikely(need > 0)) {
942 nb_used = VIRTQUEUE_NUSED(vq);
944 need = RTE_MIN(need, (int)nb_used);
946 virtio_xmit_cleanup(vq, need);
947 need = slots - vq->vq_free_cnt;
948 if (unlikely(need > 0)) {
950 "No free tx descriptors to transmit");
955 /* Enqueue Packet buffers */
956 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect, can_push);
958 txvq->stats.bytes += txm->pkt_len;
959 virtio_update_packet_stats(&txvq->stats, txm);
962 txvq->stats.packets += nb_tx;
965 vq_update_avail_idx(vq);
967 if (unlikely(virtqueue_kick_prepare(vq))) {
968 virtqueue_notify(vq);
969 PMD_TX_LOG(DEBUG, "Notified backend after xmit");