add prefix to cache line macros
[dpdk.git] / lib / librte_port / rte_port_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <string.h>
34
35 #include <rte_mbuf.h>
36 #include <rte_ethdev.h>
37 #include <rte_malloc.h>
38
39 #include "rte_port_ethdev.h"
40
41 /*
42  * Port ETHDEV Reader
43  */
44 struct rte_port_ethdev_reader {
45         uint16_t queue_id;
46         uint8_t port_id;
47 };
48
49 static void *
50 rte_port_ethdev_reader_create(void *params, int socket_id)
51 {
52         struct rte_port_ethdev_reader_params *conf =
53                         (struct rte_port_ethdev_reader_params *) params;
54         struct rte_port_ethdev_reader *port;
55
56         /* Check input parameters */
57         if (conf == NULL) {
58                 RTE_LOG(ERR, PORT, "%s: params is NULL\n", __func__);
59                 return NULL;
60         }
61
62         /* Memory allocation */
63         port = rte_zmalloc_socket("PORT", sizeof(*port),
64                         RTE_CACHE_LINE_SIZE, socket_id);
65         if (port == NULL) {
66                 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
67                 return NULL;
68         }
69
70         /* Initialization */
71         port->port_id = conf->port_id;
72         port->queue_id = conf->queue_id;
73
74         return port;
75 }
76
77 static int
78 rte_port_ethdev_reader_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
79 {
80         struct rte_port_ethdev_reader *p =
81                 (struct rte_port_ethdev_reader *) port;
82
83         return rte_eth_rx_burst(p->port_id, p->queue_id, pkts, n_pkts);
84 }
85
86 static int
87 rte_port_ethdev_reader_free(void *port)
88 {
89         if (port == NULL) {
90                 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
91                 return -EINVAL;
92         }
93
94         rte_free(port);
95
96         return 0;
97 }
98
99 /*
100  * Port ETHDEV Writer
101  */
102 #define RTE_PORT_ETHDEV_WRITER_APPROACH                  1
103
104 struct rte_port_ethdev_writer {
105         struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
106         uint32_t tx_burst_sz;
107         uint16_t tx_buf_count;
108         uint64_t bsz_mask;
109         uint16_t queue_id;
110         uint8_t port_id;
111 };
112
113 static void *
114 rte_port_ethdev_writer_create(void *params, int socket_id)
115 {
116         struct rte_port_ethdev_writer_params *conf =
117                         (struct rte_port_ethdev_writer_params *) params;
118         struct rte_port_ethdev_writer *port;
119
120         /* Check input parameters */
121         if ((conf == NULL) ||
122                 (conf->tx_burst_sz == 0) ||
123                 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
124                 (!rte_is_power_of_2(conf->tx_burst_sz))) {
125                 RTE_LOG(ERR, PORT, "%s: Invalid input parameters\n", __func__);
126                 return NULL;
127         }
128
129         /* Memory allocation */
130         port = rte_zmalloc_socket("PORT", sizeof(*port),
131                         RTE_CACHE_LINE_SIZE, socket_id);
132         if (port == NULL) {
133                 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
134                 return NULL;
135         }
136
137         /* Initialization */
138         port->port_id = conf->port_id;
139         port->queue_id = conf->queue_id;
140         port->tx_burst_sz = conf->tx_burst_sz;
141         port->tx_buf_count = 0;
142         port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
143
144         return port;
145 }
146
147 static inline void
148 send_burst(struct rte_port_ethdev_writer *p)
149 {
150         uint32_t nb_tx;
151
152         nb_tx = rte_eth_tx_burst(p->port_id, p->queue_id,
153                          p->tx_buf, p->tx_buf_count);
154
155         for ( ; nb_tx < p->tx_buf_count; nb_tx++)
156                 rte_pktmbuf_free(p->tx_buf[nb_tx]);
157
158         p->tx_buf_count = 0;
159 }
160
161 static int
162 rte_port_ethdev_writer_tx(void *port, struct rte_mbuf *pkt)
163 {
164         struct rte_port_ethdev_writer *p =
165                 (struct rte_port_ethdev_writer *) port;
166
167         p->tx_buf[p->tx_buf_count++] = pkt;
168         if (p->tx_buf_count >= p->tx_burst_sz)
169                 send_burst(p);
170
171         return 0;
172 }
173
174 #if RTE_PORT_ETHDEV_WRITER_APPROACH == 0
175
176 static int
177 rte_port_ethdev_writer_tx_bulk(void *port,
178                 struct rte_mbuf **pkts,
179                 uint64_t pkts_mask)
180 {
181         struct rte_port_ethdev_writer *p =
182                 (struct rte_port_ethdev_writer *) port;
183
184         if ((pkts_mask & (pkts_mask + 1)) == 0) {
185                 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
186                 uint32_t i;
187
188                 for (i = 0; i < n_pkts; i++) {
189                         struct rte_mbuf *pkt = pkts[i];
190
191                         p->tx_buf[p->tx_buf_count++] = pkt;
192                         if (p->tx_buf_count >= p->tx_burst_sz)
193                                 send_burst(p);
194                 }
195         } else {
196                 for ( ; pkts_mask; ) {
197                         uint32_t pkt_index = __builtin_ctzll(pkts_mask);
198                         uint64_t pkt_mask = 1LLU << pkt_index;
199                         struct rte_mbuf *pkt = pkts[pkt_index];
200
201                         p->tx_buf[p->tx_buf_count++] = pkt;
202                         if (p->tx_buf_count >= p->tx_burst_sz)
203                                 send_burst(p);
204                         pkts_mask &= ~pkt_mask;
205                 }
206         }
207
208         return 0;
209 }
210
211 #elif RTE_PORT_ETHDEV_WRITER_APPROACH == 1
212
213 static int
214 rte_port_ethdev_writer_tx_bulk(void *port,
215                 struct rte_mbuf **pkts,
216                 uint64_t pkts_mask)
217 {
218         struct rte_port_ethdev_writer *p =
219                 (struct rte_port_ethdev_writer *) port;
220         uint32_t bsz_mask = p->bsz_mask;
221         uint32_t tx_buf_count = p->tx_buf_count;
222         uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
223                         ((pkts_mask & bsz_mask) ^ bsz_mask);
224
225         if (expr == 0) {
226                 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
227                 uint32_t n_pkts_ok;
228
229                 if (tx_buf_count)
230                         send_burst(p);
231
232                 n_pkts_ok = rte_eth_tx_burst(p->port_id, p->queue_id, pkts,
233                         n_pkts);
234
235                 for ( ; n_pkts_ok < n_pkts; n_pkts_ok++) {
236                         struct rte_mbuf *pkt = pkts[n_pkts_ok];
237
238                         rte_pktmbuf_free(pkt);
239                 }
240         } else {
241                 for ( ; pkts_mask; ) {
242                         uint32_t pkt_index = __builtin_ctzll(pkts_mask);
243                         uint64_t pkt_mask = 1LLU << pkt_index;
244                         struct rte_mbuf *pkt = pkts[pkt_index];
245
246                         p->tx_buf[tx_buf_count++] = pkt;
247                         pkts_mask &= ~pkt_mask;
248                 }
249
250                 p->tx_buf_count = tx_buf_count;
251                 if (tx_buf_count >= p->tx_burst_sz)
252                         send_burst(p);
253         }
254
255         return 0;
256 }
257
258 #else
259
260 #error Invalid value for RTE_PORT_ETHDEV_WRITER_APPROACH
261
262 #endif
263
264 static int
265 rte_port_ethdev_writer_flush(void *port)
266 {
267         struct rte_port_ethdev_writer *p =
268                 (struct rte_port_ethdev_writer *) port;
269
270         if (p->tx_buf_count > 0)
271                 send_burst(p);
272
273         return 0;
274 }
275
276 static int
277 rte_port_ethdev_writer_free(void *port)
278 {
279         if (port == NULL) {
280                 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
281                 return -EINVAL;
282         }
283
284         rte_port_ethdev_writer_flush(port);
285         rte_free(port);
286
287         return 0;
288 }
289
290 /*
291  * Summary of port operations
292  */
293 struct rte_port_in_ops rte_port_ethdev_reader_ops = {
294         .f_create = rte_port_ethdev_reader_create,
295         .f_free = rte_port_ethdev_reader_free,
296         .f_rx = rte_port_ethdev_reader_rx,
297 };
298
299 struct rte_port_out_ops rte_port_ethdev_writer_ops = {
300         .f_create = rte_port_ethdev_writer_create,
301         .f_free = rte_port_ethdev_writer_free,
302         .f_tx = rte_port_ethdev_writer_tx,
303         .f_tx_bulk = rte_port_ethdev_writer_tx_bulk,
304         .f_flush = rte_port_ethdev_writer_flush,
305 };