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"
58 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
59 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
61 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0)
64 static int use_simple_rxtx;
67 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
69 struct vring_desc *dp, *dp_tail;
70 struct vq_desc_extra *dxp;
71 uint16_t desc_idx_last = desc_idx;
73 dp = &vq->vq_ring.desc[desc_idx];
74 dxp = &vq->vq_descx[desc_idx];
75 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
76 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
77 while (dp->flags & VRING_DESC_F_NEXT) {
78 desc_idx_last = dp->next;
79 dp = &vq->vq_ring.desc[dp->next];
85 * We must append the existing free chain, if any, to the end of
86 * newly freed chain. If the virtqueue was completely used, then
87 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
89 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
90 vq->vq_desc_head_idx = desc_idx;
92 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
93 dp_tail->next = desc_idx;
96 vq->vq_desc_tail_idx = desc_idx_last;
97 dp->next = VQ_RING_DESC_CHAIN_END;
101 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
102 uint32_t *len, uint16_t num)
104 struct vring_used_elem *uep;
105 struct rte_mbuf *cookie;
106 uint16_t used_idx, desc_idx;
109 /* Caller does the check */
110 for (i = 0; i < num ; i++) {
111 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
112 uep = &vq->vq_ring.used->ring[used_idx];
113 desc_idx = (uint16_t) uep->id;
115 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
117 if (unlikely(cookie == NULL)) {
118 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u\n",
119 vq->vq_used_cons_idx);
123 rte_prefetch0(cookie);
124 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
126 vq->vq_used_cons_idx++;
127 vq_ring_free_chain(vq, desc_idx);
128 vq->vq_descx[desc_idx].cookie = NULL;
134 #ifndef DEFAULT_TX_FREE_THRESH
135 #define DEFAULT_TX_FREE_THRESH 32
138 /* Cleanup from completed transmits. */
140 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
142 uint16_t i, used_idx, desc_idx;
143 for (i = 0; i < num; i++) {
144 struct vring_used_elem *uep;
145 struct vq_desc_extra *dxp;
147 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
148 uep = &vq->vq_ring.used->ring[used_idx];
150 desc_idx = (uint16_t) uep->id;
151 dxp = &vq->vq_descx[desc_idx];
152 vq->vq_used_cons_idx++;
153 vq_ring_free_chain(vq, desc_idx);
155 if (dxp->cookie != NULL) {
156 rte_pktmbuf_free(dxp->cookie);
164 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
166 struct vq_desc_extra *dxp;
167 struct virtio_hw *hw = vq->hw;
168 struct vring_desc *start_dp;
170 uint16_t head_idx, idx;
172 if (unlikely(vq->vq_free_cnt == 0))
174 if (unlikely(vq->vq_free_cnt < needed))
177 head_idx = vq->vq_desc_head_idx;
178 if (unlikely(head_idx >= vq->vq_nentries))
182 dxp = &vq->vq_descx[idx];
183 dxp->cookie = (void *)cookie;
184 dxp->ndescs = needed;
186 start_dp = vq->vq_ring.desc;
188 (uint64_t)(cookie->buf_physaddr + RTE_PKTMBUF_HEADROOM
189 - hw->vtnet_hdr_size);
191 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
192 start_dp[idx].flags = VRING_DESC_F_WRITE;
193 idx = start_dp[idx].next;
194 vq->vq_desc_head_idx = idx;
195 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
196 vq->vq_desc_tail_idx = idx;
197 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
198 vq_update_avail_ring(vq, head_idx);
204 virtqueue_enqueue_xmit(struct virtqueue *txvq, struct rte_mbuf *cookie)
206 struct vq_desc_extra *dxp;
207 struct vring_desc *start_dp;
208 uint16_t seg_num = cookie->nb_segs;
209 uint16_t needed = 1 + seg_num;
210 uint16_t head_idx, idx;
211 size_t head_size = txvq->hw->vtnet_hdr_size;
213 if (unlikely(txvq->vq_free_cnt == 0))
215 if (unlikely(txvq->vq_free_cnt < needed))
217 head_idx = txvq->vq_desc_head_idx;
218 if (unlikely(head_idx >= txvq->vq_nentries))
222 dxp = &txvq->vq_descx[idx];
223 dxp->cookie = (void *)cookie;
224 dxp->ndescs = needed;
226 start_dp = txvq->vq_ring.desc;
228 txvq->virtio_net_hdr_mem + idx * head_size;
229 start_dp[idx].len = head_size;
230 start_dp[idx].flags = VRING_DESC_F_NEXT;
232 for (; ((seg_num > 0) && (cookie != NULL)); seg_num--) {
233 idx = start_dp[idx].next;
234 start_dp[idx].addr = RTE_MBUF_DATA_DMA_ADDR(cookie);
235 start_dp[idx].len = cookie->data_len;
236 start_dp[idx].flags = VRING_DESC_F_NEXT;
237 cookie = cookie->next;
240 start_dp[idx].flags &= ~VRING_DESC_F_NEXT;
241 idx = start_dp[idx].next;
242 txvq->vq_desc_head_idx = idx;
243 if (txvq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
244 txvq->vq_desc_tail_idx = idx;
245 txvq->vq_free_cnt = (uint16_t)(txvq->vq_free_cnt - needed);
246 vq_update_avail_ring(txvq, head_idx);
251 static inline struct rte_mbuf *
252 rte_rxmbuf_alloc(struct rte_mempool *mp)
256 m = __rte_mbuf_raw_alloc(mp);
257 __rte_mbuf_sanity_check_raw(m, 0);
263 virtio_dev_vring_start(struct virtqueue *vq, int queue_type)
266 int i, nbufs, error, size = vq->vq_nentries;
267 struct vring *vr = &vq->vq_ring;
268 uint8_t *ring_mem = vq->vq_ring_virt_mem;
270 PMD_INIT_FUNC_TRACE();
273 * Reinitialise since virtio port might have been stopped and restarted
275 memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
276 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
277 vq->vq_used_cons_idx = 0;
278 vq->vq_desc_head_idx = 0;
279 vq->vq_avail_idx = 0;
280 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
281 vq->vq_free_cnt = vq->vq_nentries;
282 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
284 /* Chain all the descriptors in the ring with an END */
285 for (i = 0; i < size - 1; i++)
286 vr->desc[i].next = (uint16_t)(i + 1);
287 vr->desc[i].next = VQ_RING_DESC_CHAIN_END;
290 * Disable device(host) interrupting guest
292 virtqueue_disable_intr(vq);
294 /* Only rx virtqueue needs mbufs to be allocated at initialization */
295 if (queue_type == VTNET_RQ) {
296 if (vq->mpool == NULL)
297 rte_exit(EXIT_FAILURE,
298 "Cannot allocate initial mbufs for rx virtqueue");
300 /* Allocate blank mbufs for the each rx descriptor */
305 for (i = 0; i < vq->vq_nentries; i++) {
306 vq->vq_ring.avail->ring[i] = i;
307 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
310 while (!virtqueue_full(vq)) {
311 m = rte_rxmbuf_alloc(vq->mpool);
315 /******************************************
316 * Enqueue allocated buffers *
317 *******************************************/
318 error = virtqueue_enqueue_recv_refill(vq, m);
327 vq_update_avail_idx(vq);
329 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
331 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
333 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
334 vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
335 } else if (queue_type == VTNET_TQ) {
336 if (use_simple_rxtx) {
337 int mid_idx = vq->vq_nentries >> 1;
338 for (i = 0; i < mid_idx; i++) {
339 vq->vq_ring.avail->ring[i] = i + mid_idx;
340 vq->vq_ring.desc[i + mid_idx].next = i;
341 vq->vq_ring.desc[i + mid_idx].addr =
342 vq->virtio_net_hdr_mem +
343 mid_idx * vq->hw->vtnet_hdr_size;
344 vq->vq_ring.desc[i + mid_idx].len =
345 vq->hw->vtnet_hdr_size;
346 vq->vq_ring.desc[i + mid_idx].flags =
348 vq->vq_ring.desc[i].flags = 0;
350 for (i = mid_idx; i < vq->vq_nentries; i++)
351 vq->vq_ring.avail->ring[i] = i;
354 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
356 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
357 vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
359 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
361 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
362 vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
367 virtio_dev_cq_start(struct rte_eth_dev *dev)
369 struct virtio_hw *hw = dev->data->dev_private;
372 virtio_dev_vring_start(hw->cvq, VTNET_CQ);
373 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq);
378 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
381 * Start receive and transmit vrings
382 * - Setup vring structure for all queues
383 * - Initialize descriptor for the rx vring
384 * - Allocate blank mbufs for the each rx descriptor
389 PMD_INIT_FUNC_TRACE();
391 /* Start rx vring. */
392 for (i = 0; i < dev->data->nb_rx_queues; i++) {
393 virtio_dev_vring_start(dev->data->rx_queues[i], VTNET_RQ);
394 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
397 /* Start tx vring. */
398 for (i = 0; i < dev->data->nb_tx_queues; i++) {
399 virtio_dev_vring_start(dev->data->tx_queues[i], VTNET_TQ);
400 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
405 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
408 unsigned int socket_id,
409 __rte_unused const struct rte_eth_rxconf *rx_conf,
410 struct rte_mempool *mp)
412 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
413 struct virtqueue *vq;
416 PMD_INIT_FUNC_TRACE();
417 ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
418 nb_desc, socket_id, &vq);
420 PMD_INIT_LOG(ERR, "rvq initialization failed");
424 /* Create mempool for rx mbuf allocation */
427 dev->data->rx_queues[queue_idx] = vq;
432 virtio_dev_rx_queue_release(void *rxq)
434 virtio_dev_queue_release(rxq);
438 * struct rte_eth_dev *dev: Used to update dev
439 * uint16_t nb_desc: Defaults to values read from config space
440 * unsigned int socket_id: Used to allocate memzone
441 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
442 * uint16_t queue_idx: Just used as an index in dev txq list
445 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
448 unsigned int socket_id,
449 const struct rte_eth_txconf *tx_conf)
451 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
452 struct virtqueue *vq;
453 uint16_t tx_free_thresh;
456 PMD_INIT_FUNC_TRACE();
458 if ((tx_conf->txq_flags & ETH_TXQ_FLAGS_NOXSUMS)
459 != ETH_TXQ_FLAGS_NOXSUMS) {
460 PMD_INIT_LOG(ERR, "TX checksum offload not supported\n");
464 ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
465 nb_desc, socket_id, &vq);
467 PMD_INIT_LOG(ERR, "rvq initialization failed");
471 tx_free_thresh = tx_conf->tx_free_thresh;
472 if (tx_free_thresh == 0)
474 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
476 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
477 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
478 "number of TX entries minus 3 (%u)."
479 " (tx_free_thresh=%u port=%u queue=%u)\n",
481 tx_free_thresh, dev->data->port_id, queue_idx);
485 vq->vq_free_thresh = tx_free_thresh;
487 dev->data->tx_queues[queue_idx] = vq;
492 virtio_dev_tx_queue_release(void *txq)
494 virtio_dev_queue_release(txq);
498 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
502 * Requeue the discarded mbuf. This should always be
503 * successful since it was just dequeued.
505 error = virtqueue_enqueue_recv_refill(vq, m);
506 if (unlikely(error)) {
507 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
512 #define VIRTIO_MBUF_BURST_SZ 64
513 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
515 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
517 struct virtqueue *rxvq = rx_queue;
518 struct virtio_hw *hw;
519 struct rte_mbuf *rxm, *new_mbuf;
520 uint16_t nb_used, num, nb_rx;
521 uint32_t len[VIRTIO_MBUF_BURST_SZ];
522 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
524 uint32_t i, nb_enqueued;
525 const uint32_t hdr_size = sizeof(struct virtio_net_hdr);
527 nb_used = VIRTQUEUE_NUSED(rxvq);
531 num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
532 num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
533 if (likely(num > DESC_PER_CACHELINE))
534 num = num - ((rxvq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
539 num = virtqueue_dequeue_burst_rx(rxvq, rcv_pkts, len, num);
540 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
546 for (i = 0; i < num ; i++) {
549 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
551 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
552 PMD_RX_LOG(ERR, "Packet drop");
554 virtio_discard_rxbuf(rxvq, rxm);
559 rxm->port = rxvq->port_id;
560 rxm->data_off = RTE_PKTMBUF_HEADROOM;
564 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
565 rxm->data_len = (uint16_t)(len[i] - hdr_size);
570 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
572 rx_pkts[nb_rx++] = rxm;
573 rxvq->bytes += rx_pkts[nb_rx - 1]->pkt_len;
576 rxvq->packets += nb_rx;
578 /* Allocate new mbuf for the used descriptor */
580 while (likely(!virtqueue_full(rxvq))) {
581 new_mbuf = rte_rxmbuf_alloc(rxvq->mpool);
582 if (unlikely(new_mbuf == NULL)) {
583 struct rte_eth_dev *dev
584 = &rte_eth_devices[rxvq->port_id];
585 dev->data->rx_mbuf_alloc_failed++;
588 error = virtqueue_enqueue_recv_refill(rxvq, new_mbuf);
589 if (unlikely(error)) {
590 rte_pktmbuf_free(new_mbuf);
596 if (likely(nb_enqueued)) {
597 vq_update_avail_idx(rxvq);
599 if (unlikely(virtqueue_kick_prepare(rxvq))) {
600 virtqueue_notify(rxvq);
601 PMD_RX_LOG(DEBUG, "Notified\n");
609 virtio_recv_mergeable_pkts(void *rx_queue,
610 struct rte_mbuf **rx_pkts,
613 struct virtqueue *rxvq = rx_queue;
614 struct virtio_hw *hw;
615 struct rte_mbuf *rxm, *new_mbuf;
616 uint16_t nb_used, num, nb_rx;
617 uint32_t len[VIRTIO_MBUF_BURST_SZ];
618 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
619 struct rte_mbuf *prev;
621 uint32_t i, nb_enqueued;
625 const uint32_t hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
627 nb_used = VIRTQUEUE_NUSED(rxvq);
634 PMD_RX_LOG(DEBUG, "used:%d\n", nb_used);
644 while (i < nb_used) {
645 struct virtio_net_hdr_mrg_rxbuf *header;
647 if (nb_rx == nb_pkts)
650 num = virtqueue_dequeue_burst_rx(rxvq, rcv_pkts, len, 1);
656 PMD_RX_LOG(DEBUG, "dequeue:%d\n", num);
657 PMD_RX_LOG(DEBUG, "packet len:%d\n", len[0]);
661 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
662 PMD_RX_LOG(ERR, "Packet drop\n");
664 virtio_discard_rxbuf(rxvq, rxm);
669 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
670 RTE_PKTMBUF_HEADROOM - hdr_size);
671 seg_num = header->num_buffers;
676 rxm->data_off = RTE_PKTMBUF_HEADROOM;
677 rxm->nb_segs = seg_num;
679 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
680 rxm->data_len = (uint16_t)(len[0] - hdr_size);
682 rxm->port = rxvq->port_id;
683 rx_pkts[nb_rx] = rxm;
686 seg_res = seg_num - 1;
688 while (seg_res != 0) {
690 * Get extra segments for current uncompleted packet.
693 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
694 if (likely(VIRTQUEUE_NUSED(rxvq) >= rcv_cnt)) {
696 virtqueue_dequeue_burst_rx(rxvq,
697 rcv_pkts, len, rcv_cnt);
702 "No enough segments for packet.\n");
704 virtio_discard_rxbuf(rxvq, rxm);
711 while (extra_idx < rcv_cnt) {
712 rxm = rcv_pkts[extra_idx];
714 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
716 rxm->pkt_len = (uint32_t)(len[extra_idx]);
717 rxm->data_len = (uint16_t)(len[extra_idx]);
723 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
730 rte_vlan_strip(rx_pkts[nb_rx]);
732 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
733 rx_pkts[nb_rx]->data_len);
735 rxvq->bytes += rx_pkts[nb_rx]->pkt_len;
739 rxvq->packets += nb_rx;
741 /* Allocate new mbuf for the used descriptor */
743 while (likely(!virtqueue_full(rxvq))) {
744 new_mbuf = rte_rxmbuf_alloc(rxvq->mpool);
745 if (unlikely(new_mbuf == NULL)) {
746 struct rte_eth_dev *dev
747 = &rte_eth_devices[rxvq->port_id];
748 dev->data->rx_mbuf_alloc_failed++;
751 error = virtqueue_enqueue_recv_refill(rxvq, new_mbuf);
752 if (unlikely(error)) {
753 rte_pktmbuf_free(new_mbuf);
759 if (likely(nb_enqueued)) {
760 vq_update_avail_idx(rxvq);
762 if (unlikely(virtqueue_kick_prepare(rxvq))) {
763 virtqueue_notify(rxvq);
764 PMD_RX_LOG(DEBUG, "Notified");
772 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
774 struct virtqueue *txvq = tx_queue;
775 struct rte_mbuf *txm;
776 uint16_t nb_used, nb_tx;
779 if (unlikely(nb_pkts < 1))
782 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
783 nb_used = VIRTQUEUE_NUSED(txvq);
786 if (likely(nb_used > txvq->vq_nentries - txvq->vq_free_thresh))
787 virtio_xmit_cleanup(txvq, nb_used);
791 while (nb_tx < nb_pkts) {
792 /* Need one more descriptor for virtio header. */
793 int need = tx_pkts[nb_tx]->nb_segs - txvq->vq_free_cnt + 1;
795 /*Positive value indicates it need free vring descriptors */
796 if (unlikely(need > 0)) {
797 nb_used = VIRTQUEUE_NUSED(txvq);
799 need = RTE_MIN(need, (int)nb_used);
801 virtio_xmit_cleanup(txvq, need);
802 need = (int)tx_pkts[nb_tx]->nb_segs -
803 txvq->vq_free_cnt + 1;
807 * Zero or negative value indicates it has enough free
808 * descriptors to use for transmitting.
810 if (likely(need <= 0)) {
811 txm = tx_pkts[nb_tx];
813 /* Do VLAN tag insertion */
814 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
815 error = rte_vlan_insert(&txm);
816 if (unlikely(error)) {
817 rte_pktmbuf_free(txm);
823 /* Enqueue Packet buffers */
824 error = virtqueue_enqueue_xmit(txvq, txm);
825 if (unlikely(error)) {
827 PMD_TX_LOG(ERR, "virtqueue_enqueue Free count = 0");
828 else if (error == EMSGSIZE)
829 PMD_TX_LOG(ERR, "virtqueue_enqueue Free count < 1");
831 PMD_TX_LOG(ERR, "virtqueue_enqueue error: %d", error);
835 txvq->bytes += txm->pkt_len;
837 PMD_TX_LOG(ERR, "No free tx descriptors to transmit");
842 txvq->packets += nb_tx;
845 vq_update_avail_idx(txvq);
847 if (unlikely(virtqueue_kick_prepare(txvq))) {
848 virtqueue_notify(txvq);
849 PMD_TX_LOG(DEBUG, "Notified backend after xmit");