4 * Copyright(c) 2016 Ethan Zhuang <zhuangwj@gmail.com>.
5 * Copyright(c) 2016 Intel Corporation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <rte_common.h>
37 #include <rte_malloc.h>
40 #include "rte_port_kni.h"
45 #ifdef RTE_PORT_STATS_COLLECT
47 #define RTE_PORT_KNI_READER_STATS_PKTS_IN_ADD(port, val) \
48 port->stats.n_pkts_in += val
49 #define RTE_PORT_KNI_READER_STATS_PKTS_DROP_ADD(port, val) \
50 port->stats.n_pkts_drop += val
54 #define RTE_PORT_KNI_READER_STATS_PKTS_IN_ADD(port, val)
55 #define RTE_PORT_KNI_READER_STATS_PKTS_DROP_ADD(port, val)
59 struct rte_port_kni_reader {
60 struct rte_port_in_stats stats;
66 rte_port_kni_reader_create(void *params, int socket_id)
68 struct rte_port_kni_reader_params *conf =
69 (struct rte_port_kni_reader_params *) params;
70 struct rte_port_kni_reader *port;
72 /* Check input parameters */
74 RTE_LOG(ERR, PORT, "%s: params is NULL\n", __func__);
78 /* Memory allocation */
79 port = rte_zmalloc_socket("PORT", sizeof(*port),
80 RTE_CACHE_LINE_SIZE, socket_id);
82 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
87 port->kni = conf->kni;
93 rte_port_kni_reader_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
95 struct rte_port_kni_reader *p =
96 (struct rte_port_kni_reader *) port;
99 rx_pkt_cnt = rte_kni_rx_burst(p->kni, pkts, n_pkts);
100 RTE_PORT_KNI_READER_STATS_PKTS_IN_ADD(p, rx_pkt_cnt);
105 rte_port_kni_reader_free(void *port)
108 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
117 static int rte_port_kni_reader_stats_read(void *port,
118 struct rte_port_in_stats *stats, int clear)
120 struct rte_port_kni_reader *p =
121 (struct rte_port_kni_reader *) port;
124 memcpy(stats, &p->stats, sizeof(p->stats));
127 memset(&p->stats, 0, sizeof(p->stats));
135 #ifdef RTE_PORT_STATS_COLLECT
137 #define RTE_PORT_KNI_WRITER_STATS_PKTS_IN_ADD(port, val) \
138 port->stats.n_pkts_in += val
139 #define RTE_PORT_KNI_WRITER_STATS_PKTS_DROP_ADD(port, val) \
140 port->stats.n_pkts_drop += val
144 #define RTE_PORT_KNI_WRITER_STATS_PKTS_IN_ADD(port, val)
145 #define RTE_PORT_KNI_WRITER_STATS_PKTS_DROP_ADD(port, val)
149 struct rte_port_kni_writer {
150 struct rte_port_out_stats stats;
152 struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
153 uint32_t tx_burst_sz;
154 uint32_t tx_buf_count;
160 rte_port_kni_writer_create(void *params, int socket_id)
162 struct rte_port_kni_writer_params *conf =
163 (struct rte_port_kni_writer_params *) params;
164 struct rte_port_kni_writer *port;
166 /* Check input parameters */
167 if ((conf == NULL) ||
168 (conf->tx_burst_sz == 0) ||
169 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
170 (!rte_is_power_of_2(conf->tx_burst_sz))) {
171 RTE_LOG(ERR, PORT, "%s: Invalid input parameters\n", __func__);
175 /* Memory allocation */
176 port = rte_zmalloc_socket("PORT", sizeof(*port),
177 RTE_CACHE_LINE_SIZE, socket_id);
179 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
184 port->kni = conf->kni;
185 port->tx_burst_sz = conf->tx_burst_sz;
186 port->tx_buf_count = 0;
187 port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
193 send_burst(struct rte_port_kni_writer *p)
197 nb_tx = rte_kni_tx_burst(p->kni, p->tx_buf, p->tx_buf_count);
199 RTE_PORT_KNI_WRITER_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
200 for (; nb_tx < p->tx_buf_count; nb_tx++)
201 rte_pktmbuf_free(p->tx_buf[nb_tx]);
207 rte_port_kni_writer_tx(void *port, struct rte_mbuf *pkt)
209 struct rte_port_kni_writer *p =
210 (struct rte_port_kni_writer *) port;
212 p->tx_buf[p->tx_buf_count++] = pkt;
213 RTE_PORT_KNI_WRITER_STATS_PKTS_IN_ADD(p, 1);
214 if (p->tx_buf_count >= p->tx_burst_sz)
221 rte_port_kni_writer_tx_bulk(void *port,
222 struct rte_mbuf **pkts,
225 struct rte_port_kni_writer *p =
226 (struct rte_port_kni_writer *) port;
227 uint64_t bsz_mask = p->bsz_mask;
228 uint32_t tx_buf_count = p->tx_buf_count;
229 uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
230 ((pkts_mask & bsz_mask) ^ bsz_mask);
233 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
239 RTE_PORT_KNI_WRITER_STATS_PKTS_IN_ADD(p, n_pkts);
240 n_pkts_ok = rte_kni_tx_burst(p->kni, pkts, n_pkts);
242 RTE_PORT_KNI_WRITER_STATS_PKTS_DROP_ADD(p, n_pkts - n_pkts_ok);
243 for (; n_pkts_ok < n_pkts; n_pkts_ok++) {
244 struct rte_mbuf *pkt = pkts[n_pkts_ok];
246 rte_pktmbuf_free(pkt);
250 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
251 uint64_t pkt_mask = 1LLU << pkt_index;
252 struct rte_mbuf *pkt = pkts[pkt_index];
254 p->tx_buf[tx_buf_count++] = pkt;
255 RTE_PORT_KNI_WRITER_STATS_PKTS_IN_ADD(p, 1);
256 pkts_mask &= ~pkt_mask;
259 p->tx_buf_count = tx_buf_count;
260 if (tx_buf_count >= p->tx_burst_sz)
268 rte_port_kni_writer_flush(void *port)
270 struct rte_port_kni_writer *p =
271 (struct rte_port_kni_writer *) port;
273 if (p->tx_buf_count > 0)
280 rte_port_kni_writer_free(void *port)
283 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
287 rte_port_kni_writer_flush(port);
293 static int rte_port_kni_writer_stats_read(void *port,
294 struct rte_port_out_stats *stats, int clear)
296 struct rte_port_kni_writer *p =
297 (struct rte_port_kni_writer *) port;
300 memcpy(stats, &p->stats, sizeof(p->stats));
303 memset(&p->stats, 0, sizeof(p->stats));
309 * Port KNI Writer Nodrop
311 #ifdef RTE_PORT_STATS_COLLECT
313 #define RTE_PORT_KNI_WRITER_NODROP_STATS_PKTS_IN_ADD(port, val) \
314 port->stats.n_pkts_in += val
315 #define RTE_PORT_KNI_WRITER_NODROP_STATS_PKTS_DROP_ADD(port, val) \
316 port->stats.n_pkts_drop += val
320 #define RTE_PORT_KNI_WRITER_NODROP_STATS_PKTS_IN_ADD(port, val)
321 #define RTE_PORT_KNI_WRITER_NODROP_STATS_PKTS_DROP_ADD(port, val)
325 struct rte_port_kni_writer_nodrop {
326 struct rte_port_out_stats stats;
328 struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
329 uint32_t tx_burst_sz;
330 uint32_t tx_buf_count;
337 rte_port_kni_writer_nodrop_create(void *params, int socket_id)
339 struct rte_port_kni_writer_nodrop_params *conf =
340 (struct rte_port_kni_writer_nodrop_params *) params;
341 struct rte_port_kni_writer_nodrop *port;
343 /* Check input parameters */
344 if ((conf == NULL) ||
345 (conf->tx_burst_sz == 0) ||
346 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
347 (!rte_is_power_of_2(conf->tx_burst_sz))) {
348 RTE_LOG(ERR, PORT, "%s: Invalid input parameters\n", __func__);
352 /* Memory allocation */
353 port = rte_zmalloc_socket("PORT", sizeof(*port),
354 RTE_CACHE_LINE_SIZE, socket_id);
356 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
361 port->kni = conf->kni;
362 port->tx_burst_sz = conf->tx_burst_sz;
363 port->tx_buf_count = 0;
364 port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
367 * When n_retries is 0 it means that we should wait for every packet to
368 * send no matter how many retries should it take. To limit number of
369 * branches in fast path, we use UINT64_MAX instead of branching.
371 port->n_retries = (conf->n_retries == 0) ? UINT64_MAX : conf->n_retries;
377 send_burst_nodrop(struct rte_port_kni_writer_nodrop *p)
379 uint32_t nb_tx = 0, i;
381 nb_tx = rte_kni_tx_burst(p->kni, p->tx_buf, p->tx_buf_count);
383 /* We sent all the packets in a first try */
384 if (nb_tx >= p->tx_buf_count) {
389 for (i = 0; i < p->n_retries; i++) {
390 nb_tx += rte_kni_tx_burst(p->kni,
392 p->tx_buf_count - nb_tx);
394 /* We sent all the packets in more than one try */
395 if (nb_tx >= p->tx_buf_count) {
401 /* We didn't send the packets in maximum allowed attempts */
402 RTE_PORT_KNI_WRITER_NODROP_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
403 for ( ; nb_tx < p->tx_buf_count; nb_tx++)
404 rte_pktmbuf_free(p->tx_buf[nb_tx]);
410 rte_port_kni_writer_nodrop_tx(void *port, struct rte_mbuf *pkt)
412 struct rte_port_kni_writer_nodrop *p =
413 (struct rte_port_kni_writer_nodrop *) port;
415 p->tx_buf[p->tx_buf_count++] = pkt;
416 RTE_PORT_KNI_WRITER_STATS_PKTS_IN_ADD(p, 1);
417 if (p->tx_buf_count >= p->tx_burst_sz)
418 send_burst_nodrop(p);
424 rte_port_kni_writer_nodrop_tx_bulk(void *port,
425 struct rte_mbuf **pkts,
428 struct rte_port_kni_writer_nodrop *p =
429 (struct rte_port_kni_writer_nodrop *) port;
431 uint64_t bsz_mask = p->bsz_mask;
432 uint32_t tx_buf_count = p->tx_buf_count;
433 uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
434 ((pkts_mask & bsz_mask) ^ bsz_mask);
437 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
441 send_burst_nodrop(p);
443 RTE_PORT_KNI_WRITER_NODROP_STATS_PKTS_IN_ADD(p, n_pkts);
444 n_pkts_ok = rte_kni_tx_burst(p->kni, pkts, n_pkts);
446 if (n_pkts_ok >= n_pkts)
450 * If we didn't manage to send all packets in single burst, move
451 * remaining packets to the buffer and call send burst.
453 for (; n_pkts_ok < n_pkts; n_pkts_ok++) {
454 struct rte_mbuf *pkt = pkts[n_pkts_ok];
455 p->tx_buf[p->tx_buf_count++] = pkt;
457 send_burst_nodrop(p);
459 for ( ; pkts_mask; ) {
460 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
461 uint64_t pkt_mask = 1LLU << pkt_index;
462 struct rte_mbuf *pkt = pkts[pkt_index];
464 p->tx_buf[tx_buf_count++] = pkt;
465 RTE_PORT_KNI_WRITER_NODROP_STATS_PKTS_IN_ADD(p, 1);
466 pkts_mask &= ~pkt_mask;
469 p->tx_buf_count = tx_buf_count;
470 if (tx_buf_count >= p->tx_burst_sz)
471 send_burst_nodrop(p);
478 rte_port_kni_writer_nodrop_flush(void *port)
480 struct rte_port_kni_writer_nodrop *p =
481 (struct rte_port_kni_writer_nodrop *) port;
483 if (p->tx_buf_count > 0)
484 send_burst_nodrop(p);
490 rte_port_kni_writer_nodrop_free(void *port)
493 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
497 rte_port_kni_writer_nodrop_flush(port);
503 static int rte_port_kni_writer_nodrop_stats_read(void *port,
504 struct rte_port_out_stats *stats, int clear)
506 struct rte_port_kni_writer_nodrop *p =
507 (struct rte_port_kni_writer_nodrop *) port;
510 memcpy(stats, &p->stats, sizeof(p->stats));
513 memset(&p->stats, 0, sizeof(p->stats));
520 * Summary of port operations
522 struct rte_port_in_ops rte_port_kni_reader_ops = {
523 .f_create = rte_port_kni_reader_create,
524 .f_free = rte_port_kni_reader_free,
525 .f_rx = rte_port_kni_reader_rx,
526 .f_stats = rte_port_kni_reader_stats_read,
529 struct rte_port_out_ops rte_port_kni_writer_ops = {
530 .f_create = rte_port_kni_writer_create,
531 .f_free = rte_port_kni_writer_free,
532 .f_tx = rte_port_kni_writer_tx,
533 .f_tx_bulk = rte_port_kni_writer_tx_bulk,
534 .f_flush = rte_port_kni_writer_flush,
535 .f_stats = rte_port_kni_writer_stats_read,
538 struct rte_port_out_ops rte_port_kni_writer_nodrop_ops = {
539 .f_create = rte_port_kni_writer_nodrop_create,
540 .f_free = rte_port_kni_writer_nodrop_free,
541 .f_tx = rte_port_kni_writer_nodrop_tx,
542 .f_tx_bulk = rte_port_kni_writer_nodrop_tx_bulk,
543 .f_flush = rte_port_kni_writer_nodrop_flush,
544 .f_stats = rte_port_kni_writer_nodrop_stats_read,