1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_ethdev.h>
9 #include <rte_malloc.h>
11 #include "rte_port_ethdev.h"
16 #ifdef RTE_PORT_STATS_COLLECT
18 #define RTE_PORT_ETHDEV_READER_STATS_PKTS_IN_ADD(port, val) \
19 port->stats.n_pkts_in += val
20 #define RTE_PORT_ETHDEV_READER_STATS_PKTS_DROP_ADD(port, val) \
21 port->stats.n_pkts_drop += val
25 #define RTE_PORT_ETHDEV_READER_STATS_PKTS_IN_ADD(port, val)
26 #define RTE_PORT_ETHDEV_READER_STATS_PKTS_DROP_ADD(port, val)
30 struct rte_port_ethdev_reader {
31 struct rte_port_in_stats stats;
38 rte_port_ethdev_reader_create(void *params, int socket_id)
40 struct rte_port_ethdev_reader_params *conf =
42 struct rte_port_ethdev_reader *port;
44 /* Check input parameters */
46 RTE_LOG(ERR, PORT, "%s: params is NULL\n", __func__);
50 /* Memory allocation */
51 port = rte_zmalloc_socket("PORT", sizeof(*port),
52 RTE_CACHE_LINE_SIZE, socket_id);
54 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
59 port->port_id = conf->port_id;
60 port->queue_id = conf->queue_id;
66 rte_port_ethdev_reader_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
68 struct rte_port_ethdev_reader *p =
72 rx_pkt_cnt = rte_eth_rx_burst(p->port_id, p->queue_id, pkts, n_pkts);
73 RTE_PORT_ETHDEV_READER_STATS_PKTS_IN_ADD(p, rx_pkt_cnt);
78 rte_port_ethdev_reader_free(void *port)
81 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
90 static int rte_port_ethdev_reader_stats_read(void *port,
91 struct rte_port_in_stats *stats, int clear)
93 struct rte_port_ethdev_reader *p =
97 memcpy(stats, &p->stats, sizeof(p->stats));
100 memset(&p->stats, 0, sizeof(p->stats));
108 #ifdef RTE_PORT_STATS_COLLECT
110 #define RTE_PORT_ETHDEV_WRITER_STATS_PKTS_IN_ADD(port, val) \
111 port->stats.n_pkts_in += val
112 #define RTE_PORT_ETHDEV_WRITER_STATS_PKTS_DROP_ADD(port, val) \
113 port->stats.n_pkts_drop += val
117 #define RTE_PORT_ETHDEV_WRITER_STATS_PKTS_IN_ADD(port, val)
118 #define RTE_PORT_ETHDEV_WRITER_STATS_PKTS_DROP_ADD(port, val)
122 struct rte_port_ethdev_writer {
123 struct rte_port_out_stats stats;
125 struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
126 uint32_t tx_burst_sz;
127 uint16_t tx_buf_count;
134 rte_port_ethdev_writer_create(void *params, int socket_id)
136 struct rte_port_ethdev_writer_params *conf =
138 struct rte_port_ethdev_writer *port;
140 /* Check input parameters */
141 if ((conf == NULL) ||
142 (conf->tx_burst_sz == 0) ||
143 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
144 (!rte_is_power_of_2(conf->tx_burst_sz))) {
145 RTE_LOG(ERR, PORT, "%s: Invalid input parameters\n", __func__);
149 /* Memory allocation */
150 port = rte_zmalloc_socket("PORT", sizeof(*port),
151 RTE_CACHE_LINE_SIZE, socket_id);
153 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
158 port->port_id = conf->port_id;
159 port->queue_id = conf->queue_id;
160 port->tx_burst_sz = conf->tx_burst_sz;
161 port->tx_buf_count = 0;
162 port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
168 send_burst(struct rte_port_ethdev_writer *p)
172 nb_tx = rte_eth_tx_burst(p->port_id, p->queue_id,
173 p->tx_buf, p->tx_buf_count);
175 RTE_PORT_ETHDEV_WRITER_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
176 for ( ; nb_tx < p->tx_buf_count; nb_tx++)
177 rte_pktmbuf_free(p->tx_buf[nb_tx]);
183 rte_port_ethdev_writer_tx(void *port, struct rte_mbuf *pkt)
185 struct rte_port_ethdev_writer *p =
188 p->tx_buf[p->tx_buf_count++] = pkt;
189 RTE_PORT_ETHDEV_WRITER_STATS_PKTS_IN_ADD(p, 1);
190 if (p->tx_buf_count >= p->tx_burst_sz)
197 rte_port_ethdev_writer_tx_bulk(void *port,
198 struct rte_mbuf **pkts,
201 struct rte_port_ethdev_writer *p =
203 uint64_t bsz_mask = p->bsz_mask;
204 uint32_t tx_buf_count = p->tx_buf_count;
205 uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
206 ((pkts_mask & bsz_mask) ^ bsz_mask);
209 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
215 RTE_PORT_ETHDEV_WRITER_STATS_PKTS_IN_ADD(p, n_pkts);
216 n_pkts_ok = rte_eth_tx_burst(p->port_id, p->queue_id, pkts,
219 RTE_PORT_ETHDEV_WRITER_STATS_PKTS_DROP_ADD(p, n_pkts - n_pkts_ok);
220 for ( ; n_pkts_ok < n_pkts; n_pkts_ok++) {
221 struct rte_mbuf *pkt = pkts[n_pkts_ok];
223 rte_pktmbuf_free(pkt);
226 for ( ; pkts_mask; ) {
227 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
228 uint64_t pkt_mask = 1LLU << pkt_index;
229 struct rte_mbuf *pkt = pkts[pkt_index];
231 p->tx_buf[tx_buf_count++] = pkt;
232 RTE_PORT_ETHDEV_WRITER_STATS_PKTS_IN_ADD(p, 1);
233 pkts_mask &= ~pkt_mask;
236 p->tx_buf_count = tx_buf_count;
237 if (tx_buf_count >= p->tx_burst_sz)
245 rte_port_ethdev_writer_flush(void *port)
247 struct rte_port_ethdev_writer *p =
250 if (p->tx_buf_count > 0)
257 rte_port_ethdev_writer_free(void *port)
260 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
264 rte_port_ethdev_writer_flush(port);
270 static int rte_port_ethdev_writer_stats_read(void *port,
271 struct rte_port_out_stats *stats, int clear)
273 struct rte_port_ethdev_writer *p =
277 memcpy(stats, &p->stats, sizeof(p->stats));
280 memset(&p->stats, 0, sizeof(p->stats));
286 * Port ETHDEV Writer Nodrop
288 #ifdef RTE_PORT_STATS_COLLECT
290 #define RTE_PORT_ETHDEV_WRITER_NODROP_STATS_PKTS_IN_ADD(port, val) \
291 port->stats.n_pkts_in += val
292 #define RTE_PORT_ETHDEV_WRITER_NODROP_STATS_PKTS_DROP_ADD(port, val) \
293 port->stats.n_pkts_drop += val
297 #define RTE_PORT_ETHDEV_WRITER_NODROP_STATS_PKTS_IN_ADD(port, val)
298 #define RTE_PORT_ETHDEV_WRITER_NODROP_STATS_PKTS_DROP_ADD(port, val)
302 struct rte_port_ethdev_writer_nodrop {
303 struct rte_port_out_stats stats;
305 struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
306 uint32_t tx_burst_sz;
307 uint16_t tx_buf_count;
315 rte_port_ethdev_writer_nodrop_create(void *params, int socket_id)
317 struct rte_port_ethdev_writer_nodrop_params *conf =
319 struct rte_port_ethdev_writer_nodrop *port;
321 /* Check input parameters */
322 if ((conf == NULL) ||
323 (conf->tx_burst_sz == 0) ||
324 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
325 (!rte_is_power_of_2(conf->tx_burst_sz))) {
326 RTE_LOG(ERR, PORT, "%s: Invalid input parameters\n", __func__);
330 /* Memory allocation */
331 port = rte_zmalloc_socket("PORT", sizeof(*port),
332 RTE_CACHE_LINE_SIZE, socket_id);
334 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
339 port->port_id = conf->port_id;
340 port->queue_id = conf->queue_id;
341 port->tx_burst_sz = conf->tx_burst_sz;
342 port->tx_buf_count = 0;
343 port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
346 * When n_retries is 0 it means that we should wait for every packet to
347 * send no matter how many retries should it take. To limit number of
348 * branches in fast path, we use UINT64_MAX instead of branching.
350 port->n_retries = (conf->n_retries == 0) ? UINT64_MAX : conf->n_retries;
356 send_burst_nodrop(struct rte_port_ethdev_writer_nodrop *p)
358 uint32_t nb_tx = 0, i;
360 nb_tx = rte_eth_tx_burst(p->port_id, p->queue_id, p->tx_buf,
363 /* We sent all the packets in a first try */
364 if (nb_tx >= p->tx_buf_count) {
369 for (i = 0; i < p->n_retries; i++) {
370 nb_tx += rte_eth_tx_burst(p->port_id, p->queue_id,
371 p->tx_buf + nb_tx, p->tx_buf_count - nb_tx);
373 /* We sent all the packets in more than one try */
374 if (nb_tx >= p->tx_buf_count) {
380 /* We didn't send the packets in maximum allowed attempts */
381 RTE_PORT_ETHDEV_WRITER_NODROP_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
382 for ( ; nb_tx < p->tx_buf_count; nb_tx++)
383 rte_pktmbuf_free(p->tx_buf[nb_tx]);
389 rte_port_ethdev_writer_nodrop_tx(void *port, struct rte_mbuf *pkt)
391 struct rte_port_ethdev_writer_nodrop *p =
394 p->tx_buf[p->tx_buf_count++] = pkt;
395 RTE_PORT_ETHDEV_WRITER_NODROP_STATS_PKTS_IN_ADD(p, 1);
396 if (p->tx_buf_count >= p->tx_burst_sz)
397 send_burst_nodrop(p);
403 rte_port_ethdev_writer_nodrop_tx_bulk(void *port,
404 struct rte_mbuf **pkts,
407 struct rte_port_ethdev_writer_nodrop *p =
410 uint64_t bsz_mask = p->bsz_mask;
411 uint32_t tx_buf_count = p->tx_buf_count;
412 uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
413 ((pkts_mask & bsz_mask) ^ bsz_mask);
416 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
420 send_burst_nodrop(p);
422 RTE_PORT_ETHDEV_WRITER_NODROP_STATS_PKTS_IN_ADD(p, n_pkts);
423 n_pkts_ok = rte_eth_tx_burst(p->port_id, p->queue_id, pkts,
426 if (n_pkts_ok >= n_pkts)
430 * If we did not manage to send all packets in single burst,
431 * move remaining packets to the buffer and call send burst.
433 for (; n_pkts_ok < n_pkts; n_pkts_ok++) {
434 struct rte_mbuf *pkt = pkts[n_pkts_ok];
435 p->tx_buf[p->tx_buf_count++] = pkt;
437 send_burst_nodrop(p);
439 for ( ; pkts_mask; ) {
440 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
441 uint64_t pkt_mask = 1LLU << pkt_index;
442 struct rte_mbuf *pkt = pkts[pkt_index];
444 p->tx_buf[tx_buf_count++] = pkt;
445 RTE_PORT_ETHDEV_WRITER_NODROP_STATS_PKTS_IN_ADD(p, 1);
446 pkts_mask &= ~pkt_mask;
449 p->tx_buf_count = tx_buf_count;
450 if (tx_buf_count >= p->tx_burst_sz)
451 send_burst_nodrop(p);
458 rte_port_ethdev_writer_nodrop_flush(void *port)
460 struct rte_port_ethdev_writer_nodrop *p =
463 if (p->tx_buf_count > 0)
464 send_burst_nodrop(p);
470 rte_port_ethdev_writer_nodrop_free(void *port)
473 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
477 rte_port_ethdev_writer_nodrop_flush(port);
483 static int rte_port_ethdev_writer_nodrop_stats_read(void *port,
484 struct rte_port_out_stats *stats, int clear)
486 struct rte_port_ethdev_writer_nodrop *p =
490 memcpy(stats, &p->stats, sizeof(p->stats));
493 memset(&p->stats, 0, sizeof(p->stats));
499 * Summary of port operations
501 struct rte_port_in_ops rte_port_ethdev_reader_ops = {
502 .f_create = rte_port_ethdev_reader_create,
503 .f_free = rte_port_ethdev_reader_free,
504 .f_rx = rte_port_ethdev_reader_rx,
505 .f_stats = rte_port_ethdev_reader_stats_read,
508 struct rte_port_out_ops rte_port_ethdev_writer_ops = {
509 .f_create = rte_port_ethdev_writer_create,
510 .f_free = rte_port_ethdev_writer_free,
511 .f_tx = rte_port_ethdev_writer_tx,
512 .f_tx_bulk = rte_port_ethdev_writer_tx_bulk,
513 .f_flush = rte_port_ethdev_writer_flush,
514 .f_stats = rte_port_ethdev_writer_stats_read,
517 struct rte_port_out_ops rte_port_ethdev_writer_nodrop_ops = {
518 .f_create = rte_port_ethdev_writer_nodrop_create,
519 .f_free = rte_port_ethdev_writer_nodrop_free,
520 .f_tx = rte_port_ethdev_writer_nodrop_tx,
521 .f_tx_bulk = rte_port_ethdev_writer_nodrop_tx_bulk,
522 .f_flush = rte_port_ethdev_writer_nodrop_flush,
523 .f_stats = rte_port_ethdev_writer_nodrop_stats_read,