xref: /dpdk/drivers/net/virtio/virtio_rxtx_simple.c (revision 4e30ead5e7ca886535e2b30632b2948d2aac1681)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33 
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39 
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>
46 #include <rte_mbuf.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>
53 
54 #include "virtio_rxtx_simple.h"
55 
56 #ifndef __INTEL_COMPILER
57 #pragma GCC diagnostic ignored "-Wcast-qual"
58 #endif
59 
60 int __attribute__((cold))
61 virtqueue_enqueue_recv_refill_simple(struct virtqueue *vq,
62 	struct rte_mbuf *cookie)
63 {
64 	struct vq_desc_extra *dxp;
65 	struct vring_desc *start_dp;
66 	uint16_t desc_idx;
67 
68 	desc_idx = vq->vq_avail_idx & (vq->vq_nentries - 1);
69 	dxp = &vq->vq_descx[desc_idx];
70 	dxp->cookie = (void *)cookie;
71 	vq->sw_ring[desc_idx] = cookie;
72 
73 	start_dp = vq->vq_ring.desc;
74 	start_dp[desc_idx].addr =
75 		VIRTIO_MBUF_ADDR(cookie, vq) +
76 		RTE_PKTMBUF_HEADROOM - vq->hw->vtnet_hdr_size;
77 	start_dp[desc_idx].len = cookie->buf_len -
78 		RTE_PKTMBUF_HEADROOM + vq->hw->vtnet_hdr_size;
79 
80 	vq->vq_free_cnt--;
81 	vq->vq_avail_idx++;
82 
83 	return 0;
84 }
85 
86 uint16_t
87 virtio_xmit_pkts_simple(void *tx_queue, struct rte_mbuf **tx_pkts,
88 	uint16_t nb_pkts)
89 {
90 	struct virtnet_tx *txvq = tx_queue;
91 	struct virtqueue *vq = txvq->vq;
92 	struct virtio_hw *hw = vq->hw;
93 	uint16_t nb_used;
94 	uint16_t desc_idx;
95 	struct vring_desc *start_dp;
96 	uint16_t nb_tail, nb_commit;
97 	int i;
98 	uint16_t desc_idx_max = (vq->vq_nentries >> 1) - 1;
99 	uint16_t nb_tx = 0;
100 
101 	if (unlikely(hw->started == 0))
102 		return nb_tx;
103 
104 	nb_used = VIRTQUEUE_NUSED(vq);
105 	rte_compiler_barrier();
106 
107 	if (nb_used >= VIRTIO_TX_FREE_THRESH)
108 		virtio_xmit_cleanup(vq);
109 
110 	nb_commit = nb_pkts = RTE_MIN((vq->vq_free_cnt >> 1), nb_pkts);
111 	desc_idx = (uint16_t)(vq->vq_avail_idx & desc_idx_max);
112 	start_dp = vq->vq_ring.desc;
113 	nb_tail = (uint16_t) (desc_idx_max + 1 - desc_idx);
114 
115 	if (nb_commit >= nb_tail) {
116 		for (i = 0; i < nb_tail; i++)
117 			vq->vq_descx[desc_idx + i].cookie = tx_pkts[i];
118 		for (i = 0; i < nb_tail; i++) {
119 			start_dp[desc_idx].addr =
120 				VIRTIO_MBUF_DATA_DMA_ADDR(*tx_pkts, vq);
121 			start_dp[desc_idx].len = (*tx_pkts)->pkt_len;
122 			tx_pkts++;
123 			desc_idx++;
124 		}
125 		nb_commit -= nb_tail;
126 		desc_idx = 0;
127 	}
128 	for (i = 0; i < nb_commit; i++)
129 		vq->vq_descx[desc_idx + i].cookie = tx_pkts[i];
130 	for (i = 0; i < nb_commit; i++) {
131 		start_dp[desc_idx].addr =
132 			VIRTIO_MBUF_DATA_DMA_ADDR(*tx_pkts, vq);
133 		start_dp[desc_idx].len = (*tx_pkts)->pkt_len;
134 		tx_pkts++;
135 		desc_idx++;
136 	}
137 
138 	rte_compiler_barrier();
139 
140 	vq->vq_free_cnt -= (uint16_t)(nb_pkts << 1);
141 	vq->vq_avail_idx += nb_pkts;
142 	vq->vq_ring.avail->idx = vq->vq_avail_idx;
143 	txvq->stats.packets += nb_pkts;
144 
145 	if (likely(nb_pkts)) {
146 		if (unlikely(virtqueue_kick_prepare(vq)))
147 			virtqueue_notify(vq);
148 	}
149 
150 	return nb_pkts;
151 }
152 
153 int __attribute__((cold))
154 virtio_rxq_vec_setup(struct virtnet_rx *rxq)
155 {
156 	uintptr_t p;
157 	struct rte_mbuf mb_def = { .buf_addr = 0 }; /* zeroed mbuf */
158 
159 	mb_def.nb_segs = 1;
160 	mb_def.data_off = RTE_PKTMBUF_HEADROOM;
161 	mb_def.port = rxq->port_id;
162 	rte_mbuf_refcnt_set(&mb_def, 1);
163 
164 	/* prevent compiler reordering: rearm_data covers previous fields */
165 	rte_compiler_barrier();
166 	p = (uintptr_t)&mb_def.rearm_data;
167 	rxq->mbuf_initializer = *(uint64_t *)p;
168 
169 	return 0;
170 }
171 
172 /* Stub for linkage when arch specific implementation is not available */
173 uint16_t __attribute__((weak))
174 virtio_recv_pkts_vec(void *rx_queue __rte_unused,
175 		     struct rte_mbuf **rx_pkts __rte_unused,
176 		     uint16_t nb_pkts __rte_unused)
177 {
178 	rte_panic("Wrong weak function linked by linker\n");
179 	return 0;
180 }
181