port: add ethdev writer nodrop
[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 #include <stdint.h>
35
36 #include <rte_mbuf.h>
37 #include <rte_ethdev.h>
38 #include <rte_malloc.h>
39
40 #include "rte_port_ethdev.h"
41
42 /*
43  * Port ETHDEV Reader
44  */
45 struct rte_port_ethdev_reader {
46         uint16_t queue_id;
47         uint8_t port_id;
48 };
49
50 static void *
51 rte_port_ethdev_reader_create(void *params, int socket_id)
52 {
53         struct rte_port_ethdev_reader_params *conf =
54                         (struct rte_port_ethdev_reader_params *) params;
55         struct rte_port_ethdev_reader *port;
56
57         /* Check input parameters */
58         if (conf == NULL) {
59                 RTE_LOG(ERR, PORT, "%s: params is NULL\n", __func__);
60                 return NULL;
61         }
62
63         /* Memory allocation */
64         port = rte_zmalloc_socket("PORT", sizeof(*port),
65                         RTE_CACHE_LINE_SIZE, socket_id);
66         if (port == NULL) {
67                 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
68                 return NULL;
69         }
70
71         /* Initialization */
72         port->port_id = conf->port_id;
73         port->queue_id = conf->queue_id;
74
75         return port;
76 }
77
78 static int
79 rte_port_ethdev_reader_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
80 {
81         struct rte_port_ethdev_reader *p =
82                 (struct rte_port_ethdev_reader *) port;
83
84         return rte_eth_rx_burst(p->port_id, p->queue_id, pkts, n_pkts);
85 }
86
87 static int
88 rte_port_ethdev_reader_free(void *port)
89 {
90         if (port == NULL) {
91                 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
92                 return -EINVAL;
93         }
94
95         rte_free(port);
96
97         return 0;
98 }
99
100 /*
101  * Port ETHDEV Writer
102  */
103 struct rte_port_ethdev_writer {
104         struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
105         uint32_t tx_burst_sz;
106         uint16_t tx_buf_count;
107         uint64_t bsz_mask;
108         uint16_t queue_id;
109         uint8_t port_id;
110 };
111
112 static void *
113 rte_port_ethdev_writer_create(void *params, int socket_id)
114 {
115         struct rte_port_ethdev_writer_params *conf =
116                         (struct rte_port_ethdev_writer_params *) params;
117         struct rte_port_ethdev_writer *port;
118
119         /* Check input parameters */
120         if ((conf == NULL) ||
121                 (conf->tx_burst_sz == 0) ||
122                 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
123                 (!rte_is_power_of_2(conf->tx_burst_sz))) {
124                 RTE_LOG(ERR, PORT, "%s: Invalid input parameters\n", __func__);
125                 return NULL;
126         }
127
128         /* Memory allocation */
129         port = rte_zmalloc_socket("PORT", sizeof(*port),
130                         RTE_CACHE_LINE_SIZE, socket_id);
131         if (port == NULL) {
132                 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
133                 return NULL;
134         }
135
136         /* Initialization */
137         port->port_id = conf->port_id;
138         port->queue_id = conf->queue_id;
139         port->tx_burst_sz = conf->tx_burst_sz;
140         port->tx_buf_count = 0;
141         port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
142
143         return port;
144 }
145
146 static inline void
147 send_burst(struct rte_port_ethdev_writer *p)
148 {
149         uint32_t nb_tx;
150
151         nb_tx = rte_eth_tx_burst(p->port_id, p->queue_id,
152                          p->tx_buf, p->tx_buf_count);
153
154         for ( ; nb_tx < p->tx_buf_count; nb_tx++)
155                 rte_pktmbuf_free(p->tx_buf[nb_tx]);
156
157         p->tx_buf_count = 0;
158 }
159
160 static int
161 rte_port_ethdev_writer_tx(void *port, struct rte_mbuf *pkt)
162 {
163         struct rte_port_ethdev_writer *p =
164                 (struct rte_port_ethdev_writer *) port;
165
166         p->tx_buf[p->tx_buf_count++] = pkt;
167         if (p->tx_buf_count >= p->tx_burst_sz)
168                 send_burst(p);
169
170         return 0;
171 }
172
173 static int
174 rte_port_ethdev_writer_tx_bulk(void *port,
175                 struct rte_mbuf **pkts,
176                 uint64_t pkts_mask)
177 {
178         struct rte_port_ethdev_writer *p =
179                 (struct rte_port_ethdev_writer *) port;
180         uint32_t bsz_mask = p->bsz_mask;
181         uint32_t tx_buf_count = p->tx_buf_count;
182         uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
183                         ((pkts_mask & bsz_mask) ^ bsz_mask);
184
185         if (expr == 0) {
186                 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
187                 uint32_t n_pkts_ok;
188
189                 if (tx_buf_count)
190                         send_burst(p);
191
192                 n_pkts_ok = rte_eth_tx_burst(p->port_id, p->queue_id, pkts,
193                         n_pkts);
194
195                 for ( ; n_pkts_ok < n_pkts; n_pkts_ok++) {
196                         struct rte_mbuf *pkt = pkts[n_pkts_ok];
197
198                         rte_pktmbuf_free(pkt);
199                 }
200         } else {
201                 for ( ; pkts_mask; ) {
202                         uint32_t pkt_index = __builtin_ctzll(pkts_mask);
203                         uint64_t pkt_mask = 1LLU << pkt_index;
204                         struct rte_mbuf *pkt = pkts[pkt_index];
205
206                         p->tx_buf[tx_buf_count++] = pkt;
207                         pkts_mask &= ~pkt_mask;
208                 }
209
210                 p->tx_buf_count = tx_buf_count;
211                 if (tx_buf_count >= p->tx_burst_sz)
212                         send_burst(p);
213         }
214
215         return 0;
216 }
217
218 static int
219 rte_port_ethdev_writer_flush(void *port)
220 {
221         struct rte_port_ethdev_writer *p =
222                 (struct rte_port_ethdev_writer *) port;
223
224         if (p->tx_buf_count > 0)
225                 send_burst(p);
226
227         return 0;
228 }
229
230 static int
231 rte_port_ethdev_writer_free(void *port)
232 {
233         if (port == NULL) {
234                 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
235                 return -EINVAL;
236         }
237
238         rte_port_ethdev_writer_flush(port);
239         rte_free(port);
240
241         return 0;
242 }
243
244 /*
245  * Port ETHDEV Writer Nodrop
246  */
247 struct rte_port_ethdev_writer_nodrop {
248         struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
249         uint32_t tx_burst_sz;
250         uint16_t tx_buf_count;
251         uint64_t bsz_mask;
252         uint64_t n_retries;
253         uint16_t queue_id;
254         uint8_t port_id;
255 };
256
257 static void *
258 rte_port_ethdev_writer_nodrop_create(void *params, int socket_id)
259 {
260         struct rte_port_ethdev_writer_nodrop_params *conf =
261                         (struct rte_port_ethdev_writer_nodrop_params *) params;
262         struct rte_port_ethdev_writer_nodrop *port;
263
264         /* Check input parameters */
265         if ((conf == NULL) ||
266                 (conf->tx_burst_sz == 0) ||
267                 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
268                 (!rte_is_power_of_2(conf->tx_burst_sz))) {
269                 RTE_LOG(ERR, PORT, "%s: Invalid input parameters\n", __func__);
270                 return NULL;
271         }
272
273         /* Memory allocation */
274         port = rte_zmalloc_socket("PORT", sizeof(*port),
275                         RTE_CACHE_LINE_SIZE, socket_id);
276         if (port == NULL) {
277                 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
278                 return NULL;
279         }
280
281         /* Initialization */
282         port->port_id = conf->port_id;
283         port->queue_id = conf->queue_id;
284         port->tx_burst_sz = conf->tx_burst_sz;
285         port->tx_buf_count = 0;
286         port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
287
288         /*
289          * When n_retries is 0 it means that we should wait for every packet to
290          * send no matter how many retries should it take. To limit number of
291          * branches in fast path, we use UINT64_MAX instead of branching.
292          */
293         port->n_retries = (conf->n_retries == 0) ? UINT64_MAX : conf->n_retries;
294
295         return port;
296 }
297
298 static inline void
299 send_burst_nodrop(struct rte_port_ethdev_writer_nodrop *p)
300 {
301         uint32_t nb_tx = 0, i;
302
303         nb_tx = rte_eth_tx_burst(p->port_id, p->queue_id, p->tx_buf,
304                         p->tx_buf_count);
305
306         /* We sent all the packets in a first try */
307         if (nb_tx >= p->tx_buf_count)
308                 return;
309
310         for (i = 0; i < p->n_retries; i++) {
311                 nb_tx += rte_eth_tx_burst(p->port_id, p->queue_id,
312                                                          p->tx_buf + nb_tx, p->tx_buf_count - nb_tx);
313
314                 /* We sent all the packets in more than one try */
315                 if (nb_tx >= p->tx_buf_count)
316                         return;
317         }
318
319         /* We didn't send the packets in maximum allowed attempts */
320         for ( ; nb_tx < p->tx_buf_count; nb_tx++)
321                 rte_pktmbuf_free(p->tx_buf[nb_tx]);
322
323         p->tx_buf_count = 0;
324 }
325
326 static int
327 rte_port_ethdev_writer_nodrop_tx(void *port, struct rte_mbuf *pkt)
328 {
329         struct rte_port_ethdev_writer_nodrop *p =
330                 (struct rte_port_ethdev_writer_nodrop *) port;
331
332         p->tx_buf[p->tx_buf_count++] = pkt;
333         if (p->tx_buf_count >= p->tx_burst_sz)
334                 send_burst_nodrop(p);
335
336         return 0;
337 }
338
339 static int
340 rte_port_ethdev_writer_nodrop_tx_bulk(void *port,
341                 struct rte_mbuf **pkts,
342                 uint64_t pkts_mask)
343 {
344         struct rte_port_ethdev_writer_nodrop *p =
345                 (struct rte_port_ethdev_writer_nodrop *) port;
346
347         uint32_t bsz_mask = p->bsz_mask;
348         uint32_t tx_buf_count = p->tx_buf_count;
349         uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
350                         ((pkts_mask & bsz_mask) ^ bsz_mask);
351
352         if (expr == 0) {
353                 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
354                 uint32_t n_pkts_ok;
355
356                 if (tx_buf_count)
357                         send_burst_nodrop(p);
358
359                 n_pkts_ok = rte_eth_tx_burst(p->port_id, p->queue_id, pkts,
360                         n_pkts);
361
362                 if (n_pkts_ok >= n_pkts)
363                         return 0;
364
365                 /*
366                  * If we didnt manage to send all packets in single burst, move
367                  * remaining packets to the buffer and call send burst.
368                  */
369                 for (; n_pkts_ok < n_pkts; n_pkts_ok++) {
370                         struct rte_mbuf *pkt = pkts[n_pkts_ok];
371                         p->tx_buf[p->tx_buf_count++] = pkt;
372                 }
373                 send_burst_nodrop(p);
374         } else {
375                 for ( ; pkts_mask; ) {
376                         uint32_t pkt_index = __builtin_ctzll(pkts_mask);
377                         uint64_t pkt_mask = 1LLU << pkt_index;
378                         struct rte_mbuf *pkt = pkts[pkt_index];
379
380                         p->tx_buf[tx_buf_count++] = pkt;
381                         pkts_mask &= ~pkt_mask;
382                 }
383
384                 p->tx_buf_count = tx_buf_count;
385                 if (tx_buf_count >= p->tx_burst_sz)
386                         send_burst_nodrop(p);
387         }
388
389         return 0;
390 }
391
392 static int
393 rte_port_ethdev_writer_nodrop_flush(void *port)
394 {
395         struct rte_port_ethdev_writer_nodrop *p =
396                 (struct rte_port_ethdev_writer_nodrop *) port;
397
398         if (p->tx_buf_count > 0)
399                 send_burst_nodrop(p);
400
401         return 0;
402 }
403
404 static int
405 rte_port_ethdev_writer_nodrop_free(void *port)
406 {
407         if (port == NULL) {
408                 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
409                 return -EINVAL;
410         }
411
412         rte_port_ethdev_writer_nodrop_flush(port);
413         rte_free(port);
414
415         return 0;
416 }
417
418 /*
419  * Summary of port operations
420  */
421 struct rte_port_in_ops rte_port_ethdev_reader_ops = {
422         .f_create = rte_port_ethdev_reader_create,
423         .f_free = rte_port_ethdev_reader_free,
424         .f_rx = rte_port_ethdev_reader_rx,
425 };
426
427 struct rte_port_out_ops rte_port_ethdev_writer_ops = {
428         .f_create = rte_port_ethdev_writer_create,
429         .f_free = rte_port_ethdev_writer_free,
430         .f_tx = rte_port_ethdev_writer_tx,
431         .f_tx_bulk = rte_port_ethdev_writer_tx_bulk,
432         .f_flush = rte_port_ethdev_writer_flush,
433 };
434
435 struct rte_port_out_ops rte_port_ethdev_writer_nodrop_ops = {
436         .f_create = rte_port_ethdev_writer_nodrop_create,
437         .f_free = rte_port_ethdev_writer_nodrop_free,
438         .f_tx = rte_port_ethdev_writer_nodrop_tx,
439         .f_tx_bulk = rte_port_ethdev_writer_nodrop_tx_bulk,
440         .f_flush = rte_port_ethdev_writer_nodrop_flush,
441 };