4 * Copyright(c) 2010-2015 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_rxtx_simple.h"
56 #ifndef __INTEL_COMPILER
57 #pragma GCC diagnostic ignored "-Wcast-qual"
61 virtio_xmit_pkts_simple(void *tx_queue, struct rte_mbuf **tx_pkts,
64 struct virtnet_tx *txvq = tx_queue;
65 struct virtqueue *vq = txvq->vq;
68 struct vring_desc *start_dp;
69 uint16_t nb_tail, nb_commit;
71 uint16_t desc_idx_max = (vq->vq_nentries >> 1) - 1;
73 nb_used = VIRTQUEUE_NUSED(vq);
74 rte_compiler_barrier();
76 if (nb_used >= VIRTIO_TX_FREE_THRESH)
77 virtio_xmit_cleanup_simple(vq);
79 nb_commit = nb_pkts = RTE_MIN((vq->vq_free_cnt >> 1), nb_pkts);
80 desc_idx = (uint16_t)(vq->vq_avail_idx & desc_idx_max);
81 start_dp = vq->vq_ring.desc;
82 nb_tail = (uint16_t) (desc_idx_max + 1 - desc_idx);
84 if (nb_commit >= nb_tail) {
85 for (i = 0; i < nb_tail; i++)
86 vq->vq_descx[desc_idx + i].cookie = tx_pkts[i];
87 for (i = 0; i < nb_tail; i++) {
88 start_dp[desc_idx].addr =
89 VIRTIO_MBUF_DATA_DMA_ADDR(*tx_pkts, vq);
90 start_dp[desc_idx].len = (*tx_pkts)->pkt_len;
97 for (i = 0; i < nb_commit; i++)
98 vq->vq_descx[desc_idx + i].cookie = tx_pkts[i];
99 for (i = 0; i < nb_commit; i++) {
100 start_dp[desc_idx].addr =
101 VIRTIO_MBUF_DATA_DMA_ADDR(*tx_pkts, vq);
102 start_dp[desc_idx].len = (*tx_pkts)->pkt_len;
107 rte_compiler_barrier();
109 vq->vq_free_cnt -= (uint16_t)(nb_pkts << 1);
110 vq->vq_avail_idx += nb_pkts;
111 vq->vq_ring.avail->idx = vq->vq_avail_idx;
112 txvq->stats.packets += nb_pkts;
114 if (likely(nb_pkts)) {
115 if (unlikely(virtqueue_kick_prepare(vq)))
116 virtqueue_notify(vq);
122 int __attribute__((cold))
123 virtio_rxq_vec_setup(struct virtnet_rx *rxq)
126 struct rte_mbuf mb_def = { .buf_addr = 0 }; /* zeroed mbuf */
129 mb_def.data_off = RTE_PKTMBUF_HEADROOM;
130 mb_def.port = rxq->port_id;
131 rte_mbuf_refcnt_set(&mb_def, 1);
133 /* prevent compiler reordering: rearm_data covers previous fields */
134 rte_compiler_barrier();
135 p = (uintptr_t)&mb_def.rearm_data;
136 rxq->mbuf_initializer = *(uint64_t *)p;
141 /* Stub for linkage when arch specific implementation is not available */
142 uint16_t __attribute__((weak))
143 virtio_recv_pkts_vec(void *rx_queue __rte_unused,
144 struct rte_mbuf **rx_pkts __rte_unused,
145 uint16_t nb_pkts __rte_unused)
147 rte_panic("Wrong weak function linked by linker\n");