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 "virtqueue.h"
57 #include "virtio_rxtx.h"
59 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
60 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
62 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0)
65 static int use_simple_rxtx;
68 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
70 struct vring_desc *dp, *dp_tail;
71 struct vq_desc_extra *dxp;
72 uint16_t desc_idx_last = desc_idx;
74 dp = &vq->vq_ring.desc[desc_idx];
75 dxp = &vq->vq_descx[desc_idx];
76 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
77 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
78 while (dp->flags & VRING_DESC_F_NEXT) {
79 desc_idx_last = dp->next;
80 dp = &vq->vq_ring.desc[dp->next];
86 * We must append the existing free chain, if any, to the end of
87 * newly freed chain. If the virtqueue was completely used, then
88 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
90 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
91 vq->vq_desc_head_idx = desc_idx;
93 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
94 dp_tail->next = desc_idx;
97 vq->vq_desc_tail_idx = desc_idx_last;
98 dp->next = VQ_RING_DESC_CHAIN_END;
102 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
103 uint32_t *len, uint16_t num)
105 struct vring_used_elem *uep;
106 struct rte_mbuf *cookie;
107 uint16_t used_idx, desc_idx;
110 /* Caller does the check */
111 for (i = 0; i < num ; i++) {
112 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
113 uep = &vq->vq_ring.used->ring[used_idx];
114 desc_idx = (uint16_t) uep->id;
116 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
118 if (unlikely(cookie == NULL)) {
119 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u\n",
120 vq->vq_used_cons_idx);
124 rte_prefetch0(cookie);
125 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
127 vq->vq_used_cons_idx++;
128 vq_ring_free_chain(vq, desc_idx);
129 vq->vq_descx[desc_idx].cookie = NULL;
135 #ifndef DEFAULT_TX_FREE_THRESH
136 #define DEFAULT_TX_FREE_THRESH 32
139 /* Cleanup from completed transmits. */
141 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
143 uint16_t i, used_idx, desc_idx;
144 for (i = 0; i < num; i++) {
145 struct vring_used_elem *uep;
146 struct vq_desc_extra *dxp;
148 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
149 uep = &vq->vq_ring.used->ring[used_idx];
151 desc_idx = (uint16_t) uep->id;
152 dxp = &vq->vq_descx[desc_idx];
153 vq->vq_used_cons_idx++;
154 vq_ring_free_chain(vq, desc_idx);
156 if (dxp->cookie != NULL) {
157 rte_pktmbuf_free(dxp->cookie);
165 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
167 struct vq_desc_extra *dxp;
168 struct virtio_hw *hw = vq->hw;
169 struct vring_desc *start_dp;
171 uint16_t head_idx, idx;
173 if (unlikely(vq->vq_free_cnt == 0))
175 if (unlikely(vq->vq_free_cnt < needed))
178 head_idx = vq->vq_desc_head_idx;
179 if (unlikely(head_idx >= vq->vq_nentries))
183 dxp = &vq->vq_descx[idx];
184 dxp->cookie = (void *)cookie;
185 dxp->ndescs = needed;
187 start_dp = vq->vq_ring.desc;
189 (uint64_t)(cookie->buf_physaddr + RTE_PKTMBUF_HEADROOM
190 - hw->vtnet_hdr_size);
192 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
193 start_dp[idx].flags = VRING_DESC_F_WRITE;
194 idx = start_dp[idx].next;
195 vq->vq_desc_head_idx = idx;
196 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
197 vq->vq_desc_tail_idx = idx;
198 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
199 vq_update_avail_ring(vq, head_idx);
205 virtqueue_enqueue_xmit(struct virtqueue *txvq, struct rte_mbuf *cookie)
207 struct vq_desc_extra *dxp;
208 struct vring_desc *start_dp;
209 uint16_t seg_num = cookie->nb_segs;
210 uint16_t needed = 1 + seg_num;
211 uint16_t head_idx, idx;
212 size_t head_size = txvq->hw->vtnet_hdr_size;
214 if (unlikely(txvq->vq_free_cnt == 0))
216 if (unlikely(txvq->vq_free_cnt < needed))
218 head_idx = txvq->vq_desc_head_idx;
219 if (unlikely(head_idx >= txvq->vq_nentries))
223 dxp = &txvq->vq_descx[idx];
224 dxp->cookie = (void *)cookie;
225 dxp->ndescs = needed;
227 start_dp = txvq->vq_ring.desc;
229 txvq->virtio_net_hdr_mem + idx * head_size;
230 start_dp[idx].len = head_size;
231 start_dp[idx].flags = VRING_DESC_F_NEXT;
233 for (; ((seg_num > 0) && (cookie != NULL)); seg_num--) {
234 idx = start_dp[idx].next;
235 start_dp[idx].addr = RTE_MBUF_DATA_DMA_ADDR(cookie);
236 start_dp[idx].len = cookie->data_len;
237 start_dp[idx].flags = VRING_DESC_F_NEXT;
238 cookie = cookie->next;
241 start_dp[idx].flags &= ~VRING_DESC_F_NEXT;
242 idx = start_dp[idx].next;
243 txvq->vq_desc_head_idx = idx;
244 if (txvq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
245 txvq->vq_desc_tail_idx = idx;
246 txvq->vq_free_cnt = (uint16_t)(txvq->vq_free_cnt - needed);
247 vq_update_avail_ring(txvq, head_idx);
252 static inline struct rte_mbuf *
253 rte_rxmbuf_alloc(struct rte_mempool *mp)
257 m = __rte_mbuf_raw_alloc(mp);
258 __rte_mbuf_sanity_check_raw(m, 0);
264 virtio_dev_vring_start(struct virtqueue *vq, int queue_type)
267 int i, nbufs, error, size = vq->vq_nentries;
268 struct vring *vr = &vq->vq_ring;
269 uint8_t *ring_mem = vq->vq_ring_virt_mem;
271 PMD_INIT_FUNC_TRACE();
274 * Reinitialise since virtio port might have been stopped and restarted
276 memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
277 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
278 vq->vq_used_cons_idx = 0;
279 vq->vq_desc_head_idx = 0;
280 vq->vq_avail_idx = 0;
281 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
282 vq->vq_free_cnt = vq->vq_nentries;
283 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
285 /* Chain all the descriptors in the ring with an END */
286 for (i = 0; i < size - 1; i++)
287 vr->desc[i].next = (uint16_t)(i + 1);
288 vr->desc[i].next = VQ_RING_DESC_CHAIN_END;
291 * Disable device(host) interrupting guest
293 virtqueue_disable_intr(vq);
295 /* Only rx virtqueue needs mbufs to be allocated at initialization */
296 if (queue_type == VTNET_RQ) {
297 if (vq->mpool == NULL)
298 rte_exit(EXIT_FAILURE,
299 "Cannot allocate initial mbufs for rx virtqueue");
301 /* Allocate blank mbufs for the each rx descriptor */
306 for (i = 0; i < vq->vq_nentries; i++) {
307 vq->vq_ring.avail->ring[i] = i;
308 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
311 memset(&vq->fake_mbuf, 0, sizeof(vq->fake_mbuf));
312 for (i = 0; i < RTE_PMD_VIRTIO_RX_MAX_BURST; i++)
313 vq->sw_ring[vq->vq_nentries + i] = &vq->fake_mbuf;
315 while (!virtqueue_full(vq)) {
316 m = rte_rxmbuf_alloc(vq->mpool);
320 /******************************************
321 * Enqueue allocated buffers *
322 *******************************************/
324 error = virtqueue_enqueue_recv_refill_simple(vq, m);
326 error = virtqueue_enqueue_recv_refill(vq, m);
334 vq_update_avail_idx(vq);
336 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
338 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
340 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
341 vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
342 } else if (queue_type == VTNET_TQ) {
343 if (use_simple_rxtx) {
344 int mid_idx = vq->vq_nentries >> 1;
345 for (i = 0; i < mid_idx; i++) {
346 vq->vq_ring.avail->ring[i] = i + mid_idx;
347 vq->vq_ring.desc[i + mid_idx].next = i;
348 vq->vq_ring.desc[i + mid_idx].addr =
349 vq->virtio_net_hdr_mem +
350 mid_idx * vq->hw->vtnet_hdr_size;
351 vq->vq_ring.desc[i + mid_idx].len =
352 vq->hw->vtnet_hdr_size;
353 vq->vq_ring.desc[i + mid_idx].flags =
355 vq->vq_ring.desc[i].flags = 0;
357 for (i = mid_idx; i < vq->vq_nentries; i++)
358 vq->vq_ring.avail->ring[i] = i;
361 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
363 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
364 vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
366 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
368 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
369 vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
374 virtio_dev_cq_start(struct rte_eth_dev *dev)
376 struct virtio_hw *hw = dev->data->dev_private;
379 virtio_dev_vring_start(hw->cvq, VTNET_CQ);
380 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq);
385 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
388 * Start receive and transmit vrings
389 * - Setup vring structure for all queues
390 * - Initialize descriptor for the rx vring
391 * - Allocate blank mbufs for the each rx descriptor
396 PMD_INIT_FUNC_TRACE();
398 /* Start rx vring. */
399 for (i = 0; i < dev->data->nb_rx_queues; i++) {
400 virtio_dev_vring_start(dev->data->rx_queues[i], VTNET_RQ);
401 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
404 /* Start tx vring. */
405 for (i = 0; i < dev->data->nb_tx_queues; i++) {
406 virtio_dev_vring_start(dev->data->tx_queues[i], VTNET_TQ);
407 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
412 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
415 unsigned int socket_id,
416 __rte_unused const struct rte_eth_rxconf *rx_conf,
417 struct rte_mempool *mp)
419 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
420 struct virtqueue *vq;
423 PMD_INIT_FUNC_TRACE();
424 ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
425 nb_desc, socket_id, &vq);
427 PMD_INIT_LOG(ERR, "rvq initialization failed");
431 /* Create mempool for rx mbuf allocation */
434 dev->data->rx_queues[queue_idx] = vq;
439 virtio_dev_rx_queue_release(void *rxq)
441 virtio_dev_queue_release(rxq);
445 * struct rte_eth_dev *dev: Used to update dev
446 * uint16_t nb_desc: Defaults to values read from config space
447 * unsigned int socket_id: Used to allocate memzone
448 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
449 * uint16_t queue_idx: Just used as an index in dev txq list
452 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
455 unsigned int socket_id,
456 const struct rte_eth_txconf *tx_conf)
458 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
459 struct virtqueue *vq;
460 uint16_t tx_free_thresh;
463 PMD_INIT_FUNC_TRACE();
465 if ((tx_conf->txq_flags & ETH_TXQ_FLAGS_NOXSUMS)
466 != ETH_TXQ_FLAGS_NOXSUMS) {
467 PMD_INIT_LOG(ERR, "TX checksum offload not supported\n");
471 ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
472 nb_desc, socket_id, &vq);
474 PMD_INIT_LOG(ERR, "rvq initialization failed");
478 tx_free_thresh = tx_conf->tx_free_thresh;
479 if (tx_free_thresh == 0)
481 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
483 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
484 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
485 "number of TX entries minus 3 (%u)."
486 " (tx_free_thresh=%u port=%u queue=%u)\n",
488 tx_free_thresh, dev->data->port_id, queue_idx);
492 vq->vq_free_thresh = tx_free_thresh;
494 dev->data->tx_queues[queue_idx] = vq;
499 virtio_dev_tx_queue_release(void *txq)
501 virtio_dev_queue_release(txq);
505 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
509 * Requeue the discarded mbuf. This should always be
510 * successful since it was just dequeued.
512 error = virtqueue_enqueue_recv_refill(vq, m);
513 if (unlikely(error)) {
514 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
519 #define VIRTIO_MBUF_BURST_SZ 64
520 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
522 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
524 struct virtqueue *rxvq = rx_queue;
525 struct virtio_hw *hw;
526 struct rte_mbuf *rxm, *new_mbuf;
527 uint16_t nb_used, num, nb_rx;
528 uint32_t len[VIRTIO_MBUF_BURST_SZ];
529 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
531 uint32_t i, nb_enqueued;
532 const uint32_t hdr_size = sizeof(struct virtio_net_hdr);
534 nb_used = VIRTQUEUE_NUSED(rxvq);
538 num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
539 num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
540 if (likely(num > DESC_PER_CACHELINE))
541 num = num - ((rxvq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
546 num = virtqueue_dequeue_burst_rx(rxvq, rcv_pkts, len, num);
547 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
553 for (i = 0; i < num ; i++) {
556 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
558 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
559 PMD_RX_LOG(ERR, "Packet drop");
561 virtio_discard_rxbuf(rxvq, rxm);
566 rxm->port = rxvq->port_id;
567 rxm->data_off = RTE_PKTMBUF_HEADROOM;
571 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
572 rxm->data_len = (uint16_t)(len[i] - hdr_size);
577 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
579 rx_pkts[nb_rx++] = rxm;
580 rxvq->bytes += rx_pkts[nb_rx - 1]->pkt_len;
583 rxvq->packets += nb_rx;
585 /* Allocate new mbuf for the used descriptor */
587 while (likely(!virtqueue_full(rxvq))) {
588 new_mbuf = rte_rxmbuf_alloc(rxvq->mpool);
589 if (unlikely(new_mbuf == NULL)) {
590 struct rte_eth_dev *dev
591 = &rte_eth_devices[rxvq->port_id];
592 dev->data->rx_mbuf_alloc_failed++;
595 error = virtqueue_enqueue_recv_refill(rxvq, new_mbuf);
596 if (unlikely(error)) {
597 rte_pktmbuf_free(new_mbuf);
603 if (likely(nb_enqueued)) {
604 vq_update_avail_idx(rxvq);
606 if (unlikely(virtqueue_kick_prepare(rxvq))) {
607 virtqueue_notify(rxvq);
608 PMD_RX_LOG(DEBUG, "Notified\n");
616 virtio_recv_mergeable_pkts(void *rx_queue,
617 struct rte_mbuf **rx_pkts,
620 struct virtqueue *rxvq = rx_queue;
621 struct virtio_hw *hw;
622 struct rte_mbuf *rxm, *new_mbuf;
623 uint16_t nb_used, num, nb_rx;
624 uint32_t len[VIRTIO_MBUF_BURST_SZ];
625 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
626 struct rte_mbuf *prev;
628 uint32_t i, nb_enqueued;
632 const uint32_t hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
634 nb_used = VIRTQUEUE_NUSED(rxvq);
641 PMD_RX_LOG(DEBUG, "used:%d\n", nb_used);
651 while (i < nb_used) {
652 struct virtio_net_hdr_mrg_rxbuf *header;
654 if (nb_rx == nb_pkts)
657 num = virtqueue_dequeue_burst_rx(rxvq, rcv_pkts, len, 1);
663 PMD_RX_LOG(DEBUG, "dequeue:%d\n", num);
664 PMD_RX_LOG(DEBUG, "packet len:%d\n", len[0]);
668 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
669 PMD_RX_LOG(ERR, "Packet drop\n");
671 virtio_discard_rxbuf(rxvq, rxm);
676 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
677 RTE_PKTMBUF_HEADROOM - hdr_size);
678 seg_num = header->num_buffers;
683 rxm->data_off = RTE_PKTMBUF_HEADROOM;
684 rxm->nb_segs = seg_num;
686 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
687 rxm->data_len = (uint16_t)(len[0] - hdr_size);
689 rxm->port = rxvq->port_id;
690 rx_pkts[nb_rx] = rxm;
693 seg_res = seg_num - 1;
695 while (seg_res != 0) {
697 * Get extra segments for current uncompleted packet.
700 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
701 if (likely(VIRTQUEUE_NUSED(rxvq) >= rcv_cnt)) {
703 virtqueue_dequeue_burst_rx(rxvq,
704 rcv_pkts, len, rcv_cnt);
709 "No enough segments for packet.\n");
711 virtio_discard_rxbuf(rxvq, rxm);
718 while (extra_idx < rcv_cnt) {
719 rxm = rcv_pkts[extra_idx];
721 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
723 rxm->pkt_len = (uint32_t)(len[extra_idx]);
724 rxm->data_len = (uint16_t)(len[extra_idx]);
730 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
737 rte_vlan_strip(rx_pkts[nb_rx]);
739 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
740 rx_pkts[nb_rx]->data_len);
742 rxvq->bytes += rx_pkts[nb_rx]->pkt_len;
746 rxvq->packets += nb_rx;
748 /* Allocate new mbuf for the used descriptor */
750 while (likely(!virtqueue_full(rxvq))) {
751 new_mbuf = rte_rxmbuf_alloc(rxvq->mpool);
752 if (unlikely(new_mbuf == NULL)) {
753 struct rte_eth_dev *dev
754 = &rte_eth_devices[rxvq->port_id];
755 dev->data->rx_mbuf_alloc_failed++;
758 error = virtqueue_enqueue_recv_refill(rxvq, new_mbuf);
759 if (unlikely(error)) {
760 rte_pktmbuf_free(new_mbuf);
766 if (likely(nb_enqueued)) {
767 vq_update_avail_idx(rxvq);
769 if (unlikely(virtqueue_kick_prepare(rxvq))) {
770 virtqueue_notify(rxvq);
771 PMD_RX_LOG(DEBUG, "Notified");
779 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
781 struct virtqueue *txvq = tx_queue;
782 struct rte_mbuf *txm;
783 uint16_t nb_used, nb_tx;
786 if (unlikely(nb_pkts < 1))
789 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
790 nb_used = VIRTQUEUE_NUSED(txvq);
793 if (likely(nb_used > txvq->vq_nentries - txvq->vq_free_thresh))
794 virtio_xmit_cleanup(txvq, nb_used);
798 while (nb_tx < nb_pkts) {
799 /* Need one more descriptor for virtio header. */
800 int need = tx_pkts[nb_tx]->nb_segs - txvq->vq_free_cnt + 1;
802 /*Positive value indicates it need free vring descriptors */
803 if (unlikely(need > 0)) {
804 nb_used = VIRTQUEUE_NUSED(txvq);
806 need = RTE_MIN(need, (int)nb_used);
808 virtio_xmit_cleanup(txvq, need);
809 need = (int)tx_pkts[nb_tx]->nb_segs -
810 txvq->vq_free_cnt + 1;
814 * Zero or negative value indicates it has enough free
815 * descriptors to use for transmitting.
817 if (likely(need <= 0)) {
818 txm = tx_pkts[nb_tx];
820 /* Do VLAN tag insertion */
821 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
822 error = rte_vlan_insert(&txm);
823 if (unlikely(error)) {
824 rte_pktmbuf_free(txm);
830 /* Enqueue Packet buffers */
831 error = virtqueue_enqueue_xmit(txvq, txm);
832 if (unlikely(error)) {
834 PMD_TX_LOG(ERR, "virtqueue_enqueue Free count = 0");
835 else if (error == EMSGSIZE)
836 PMD_TX_LOG(ERR, "virtqueue_enqueue Free count < 1");
838 PMD_TX_LOG(ERR, "virtqueue_enqueue error: %d", error);
842 txvq->bytes += txm->pkt_len;
844 PMD_TX_LOG(ERR, "No free tx descriptors to transmit");
849 txvq->packets += nb_tx;
852 vq_update_avail_idx(txvq);
854 if (unlikely(virtqueue_kick_prepare(txvq))) {
855 virtqueue_notify(txvq);
856 PMD_TX_LOG(DEBUG, "Notified backend after xmit");