1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Ethan Zhuang <zhuangwj@gmail.com>.
3 * Copyright(c) 2016 Intel Corporation.
7 #include <rte_common.h>
8 #include <rte_malloc.h>
11 #include "rte_port_kni.h"
16 #ifdef RTE_PORT_STATS_COLLECT
18 #define RTE_PORT_KNI_READER_STATS_PKTS_IN_ADD(port, val) \
19 port->stats.n_pkts_in += val
20 #define RTE_PORT_KNI_READER_STATS_PKTS_DROP_ADD(port, val) \
21 port->stats.n_pkts_drop += val
25 #define RTE_PORT_KNI_READER_STATS_PKTS_IN_ADD(port, val)
26 #define RTE_PORT_KNI_READER_STATS_PKTS_DROP_ADD(port, val)
30 struct rte_port_kni_reader {
31 struct rte_port_in_stats stats;
37 rte_port_kni_reader_create(void *params, int socket_id)
39 struct rte_port_kni_reader_params *conf =
41 struct rte_port_kni_reader *port;
43 /* Check input parameters */
45 RTE_LOG(ERR, PORT, "%s: params is NULL\n", __func__);
49 /* Memory allocation */
50 port = rte_zmalloc_socket("PORT", sizeof(*port),
51 RTE_CACHE_LINE_SIZE, socket_id);
53 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
58 port->kni = conf->kni;
64 rte_port_kni_reader_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
66 struct rte_port_kni_reader *p =
70 rx_pkt_cnt = rte_kni_rx_burst(p->kni, pkts, n_pkts);
71 RTE_PORT_KNI_READER_STATS_PKTS_IN_ADD(p, rx_pkt_cnt);
76 rte_port_kni_reader_free(void *port)
79 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
88 static int rte_port_kni_reader_stats_read(void *port,
89 struct rte_port_in_stats *stats, int clear)
91 struct rte_port_kni_reader *p =
95 memcpy(stats, &p->stats, sizeof(p->stats));
98 memset(&p->stats, 0, sizeof(p->stats));
106 #ifdef RTE_PORT_STATS_COLLECT
108 #define RTE_PORT_KNI_WRITER_STATS_PKTS_IN_ADD(port, val) \
109 port->stats.n_pkts_in += val
110 #define RTE_PORT_KNI_WRITER_STATS_PKTS_DROP_ADD(port, val) \
111 port->stats.n_pkts_drop += val
115 #define RTE_PORT_KNI_WRITER_STATS_PKTS_IN_ADD(port, val)
116 #define RTE_PORT_KNI_WRITER_STATS_PKTS_DROP_ADD(port, val)
120 struct rte_port_kni_writer {
121 struct rte_port_out_stats stats;
123 struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
124 uint32_t tx_burst_sz;
125 uint32_t tx_buf_count;
131 rte_port_kni_writer_create(void *params, int socket_id)
133 struct rte_port_kni_writer_params *conf =
135 struct rte_port_kni_writer *port;
137 /* Check input parameters */
138 if ((conf == NULL) ||
139 (conf->tx_burst_sz == 0) ||
140 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
141 (!rte_is_power_of_2(conf->tx_burst_sz))) {
142 RTE_LOG(ERR, PORT, "%s: Invalid input parameters\n", __func__);
146 /* Memory allocation */
147 port = rte_zmalloc_socket("PORT", sizeof(*port),
148 RTE_CACHE_LINE_SIZE, socket_id);
150 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
155 port->kni = conf->kni;
156 port->tx_burst_sz = conf->tx_burst_sz;
157 port->tx_buf_count = 0;
158 port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
164 send_burst(struct rte_port_kni_writer *p)
168 nb_tx = rte_kni_tx_burst(p->kni, p->tx_buf, p->tx_buf_count);
170 RTE_PORT_KNI_WRITER_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
171 for (; nb_tx < p->tx_buf_count; nb_tx++)
172 rte_pktmbuf_free(p->tx_buf[nb_tx]);
178 rte_port_kni_writer_tx(void *port, struct rte_mbuf *pkt)
180 struct rte_port_kni_writer *p =
183 p->tx_buf[p->tx_buf_count++] = pkt;
184 RTE_PORT_KNI_WRITER_STATS_PKTS_IN_ADD(p, 1);
185 if (p->tx_buf_count >= p->tx_burst_sz)
192 rte_port_kni_writer_tx_bulk(void *port,
193 struct rte_mbuf **pkts,
196 struct rte_port_kni_writer *p =
198 uint64_t bsz_mask = p->bsz_mask;
199 uint32_t tx_buf_count = p->tx_buf_count;
200 uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
201 ((pkts_mask & bsz_mask) ^ bsz_mask);
204 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
210 RTE_PORT_KNI_WRITER_STATS_PKTS_IN_ADD(p, n_pkts);
211 n_pkts_ok = rte_kni_tx_burst(p->kni, pkts, n_pkts);
213 RTE_PORT_KNI_WRITER_STATS_PKTS_DROP_ADD(p, n_pkts - n_pkts_ok);
214 for (; n_pkts_ok < n_pkts; n_pkts_ok++) {
215 struct rte_mbuf *pkt = pkts[n_pkts_ok];
217 rte_pktmbuf_free(pkt);
221 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
222 uint64_t pkt_mask = 1LLU << pkt_index;
223 struct rte_mbuf *pkt = pkts[pkt_index];
225 p->tx_buf[tx_buf_count++] = pkt;
226 RTE_PORT_KNI_WRITER_STATS_PKTS_IN_ADD(p, 1);
227 pkts_mask &= ~pkt_mask;
230 p->tx_buf_count = tx_buf_count;
231 if (tx_buf_count >= p->tx_burst_sz)
239 rte_port_kni_writer_flush(void *port)
241 struct rte_port_kni_writer *p =
244 if (p->tx_buf_count > 0)
251 rte_port_kni_writer_free(void *port)
254 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
258 rte_port_kni_writer_flush(port);
264 static int rte_port_kni_writer_stats_read(void *port,
265 struct rte_port_out_stats *stats, int clear)
267 struct rte_port_kni_writer *p =
271 memcpy(stats, &p->stats, sizeof(p->stats));
274 memset(&p->stats, 0, sizeof(p->stats));
280 * Port KNI Writer Nodrop
282 #ifdef RTE_PORT_STATS_COLLECT
284 #define RTE_PORT_KNI_WRITER_NODROP_STATS_PKTS_IN_ADD(port, val) \
285 port->stats.n_pkts_in += val
286 #define RTE_PORT_KNI_WRITER_NODROP_STATS_PKTS_DROP_ADD(port, val) \
287 port->stats.n_pkts_drop += val
291 #define RTE_PORT_KNI_WRITER_NODROP_STATS_PKTS_IN_ADD(port, val)
292 #define RTE_PORT_KNI_WRITER_NODROP_STATS_PKTS_DROP_ADD(port, val)
296 struct rte_port_kni_writer_nodrop {
297 struct rte_port_out_stats stats;
299 struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
300 uint32_t tx_burst_sz;
301 uint32_t tx_buf_count;
308 rte_port_kni_writer_nodrop_create(void *params, int socket_id)
310 struct rte_port_kni_writer_nodrop_params *conf =
312 struct rte_port_kni_writer_nodrop *port;
314 /* Check input parameters */
315 if ((conf == NULL) ||
316 (conf->tx_burst_sz == 0) ||
317 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
318 (!rte_is_power_of_2(conf->tx_burst_sz))) {
319 RTE_LOG(ERR, PORT, "%s: Invalid input parameters\n", __func__);
323 /* Memory allocation */
324 port = rte_zmalloc_socket("PORT", sizeof(*port),
325 RTE_CACHE_LINE_SIZE, socket_id);
327 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
332 port->kni = conf->kni;
333 port->tx_burst_sz = conf->tx_burst_sz;
334 port->tx_buf_count = 0;
335 port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
338 * When n_retries is 0 it means that we should wait for every packet to
339 * send no matter how many retries should it take. To limit number of
340 * branches in fast path, we use UINT64_MAX instead of branching.
342 port->n_retries = (conf->n_retries == 0) ? UINT64_MAX : conf->n_retries;
348 send_burst_nodrop(struct rte_port_kni_writer_nodrop *p)
350 uint32_t nb_tx = 0, i;
352 nb_tx = rte_kni_tx_burst(p->kni, p->tx_buf, p->tx_buf_count);
354 /* We sent all the packets in a first try */
355 if (nb_tx >= p->tx_buf_count) {
360 for (i = 0; i < p->n_retries; i++) {
361 nb_tx += rte_kni_tx_burst(p->kni,
363 p->tx_buf_count - nb_tx);
365 /* We sent all the packets in more than one try */
366 if (nb_tx >= p->tx_buf_count) {
372 /* We didn't send the packets in maximum allowed attempts */
373 RTE_PORT_KNI_WRITER_NODROP_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
374 for ( ; nb_tx < p->tx_buf_count; nb_tx++)
375 rte_pktmbuf_free(p->tx_buf[nb_tx]);
381 rte_port_kni_writer_nodrop_tx(void *port, struct rte_mbuf *pkt)
383 struct rte_port_kni_writer_nodrop *p =
386 p->tx_buf[p->tx_buf_count++] = pkt;
387 RTE_PORT_KNI_WRITER_STATS_PKTS_IN_ADD(p, 1);
388 if (p->tx_buf_count >= p->tx_burst_sz)
389 send_burst_nodrop(p);
395 rte_port_kni_writer_nodrop_tx_bulk(void *port,
396 struct rte_mbuf **pkts,
399 struct rte_port_kni_writer_nodrop *p =
402 uint64_t bsz_mask = p->bsz_mask;
403 uint32_t tx_buf_count = p->tx_buf_count;
404 uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
405 ((pkts_mask & bsz_mask) ^ bsz_mask);
408 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
412 send_burst_nodrop(p);
414 RTE_PORT_KNI_WRITER_NODROP_STATS_PKTS_IN_ADD(p, n_pkts);
415 n_pkts_ok = rte_kni_tx_burst(p->kni, pkts, n_pkts);
417 if (n_pkts_ok >= n_pkts)
421 * If we didn't manage to send all packets in single burst, move
422 * remaining packets to the buffer and call send burst.
424 for (; n_pkts_ok < n_pkts; n_pkts_ok++) {
425 struct rte_mbuf *pkt = pkts[n_pkts_ok];
426 p->tx_buf[p->tx_buf_count++] = pkt;
428 send_burst_nodrop(p);
430 for ( ; pkts_mask; ) {
431 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
432 uint64_t pkt_mask = 1LLU << pkt_index;
433 struct rte_mbuf *pkt = pkts[pkt_index];
435 p->tx_buf[tx_buf_count++] = pkt;
436 RTE_PORT_KNI_WRITER_NODROP_STATS_PKTS_IN_ADD(p, 1);
437 pkts_mask &= ~pkt_mask;
440 p->tx_buf_count = tx_buf_count;
441 if (tx_buf_count >= p->tx_burst_sz)
442 send_burst_nodrop(p);
449 rte_port_kni_writer_nodrop_flush(void *port)
451 struct rte_port_kni_writer_nodrop *p =
454 if (p->tx_buf_count > 0)
455 send_burst_nodrop(p);
461 rte_port_kni_writer_nodrop_free(void *port)
464 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
468 rte_port_kni_writer_nodrop_flush(port);
474 static int rte_port_kni_writer_nodrop_stats_read(void *port,
475 struct rte_port_out_stats *stats, int clear)
477 struct rte_port_kni_writer_nodrop *p =
481 memcpy(stats, &p->stats, sizeof(p->stats));
484 memset(&p->stats, 0, sizeof(p->stats));
491 * Summary of port operations
493 struct rte_port_in_ops rte_port_kni_reader_ops = {
494 .f_create = rte_port_kni_reader_create,
495 .f_free = rte_port_kni_reader_free,
496 .f_rx = rte_port_kni_reader_rx,
497 .f_stats = rte_port_kni_reader_stats_read,
500 struct rte_port_out_ops rte_port_kni_writer_ops = {
501 .f_create = rte_port_kni_writer_create,
502 .f_free = rte_port_kni_writer_free,
503 .f_tx = rte_port_kni_writer_tx,
504 .f_tx_bulk = rte_port_kni_writer_tx_bulk,
505 .f_flush = rte_port_kni_writer_flush,
506 .f_stats = rte_port_kni_writer_stats_read,
509 struct rte_port_out_ops rte_port_kni_writer_nodrop_ops = {
510 .f_create = rte_port_kni_writer_nodrop_create,
511 .f_free = rte_port_kni_writer_nodrop_free,
512 .f_tx = rte_port_kni_writer_nodrop_tx,
513 .f_tx_bulk = rte_port_kni_writer_nodrop_tx_bulk,
514 .f_flush = rte_port_kni_writer_nodrop_flush,
515 .f_stats = rte_port_kni_writer_nodrop_stats_read,