4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
38 #include <rte_malloc.h>
40 #include "rte_port_fd.h"
45 #ifdef RTE_PORT_STATS_COLLECT
47 #define RTE_PORT_FD_READER_STATS_PKTS_IN_ADD(port, val) \
48 do { port->stats.n_pkts_in += val; } while (0)
49 #define RTE_PORT_FD_READER_STATS_PKTS_DROP_ADD(port, val) \
50 do { port->stats.n_pkts_drop += val; } while (0)
54 #define RTE_PORT_FD_READER_STATS_PKTS_IN_ADD(port, val)
55 #define RTE_PORT_FD_READER_STATS_PKTS_DROP_ADD(port, val)
59 struct rte_port_fd_reader {
60 struct rte_port_in_stats stats;
63 struct rte_mempool *mempool;
67 rte_port_fd_reader_create(void *params, int socket_id)
69 struct rte_port_fd_reader_params *conf =
71 struct rte_port_fd_reader *port;
73 /* Check input parameters */
75 RTE_LOG(ERR, PORT, "%s: params is NULL\n", __func__);
79 RTE_LOG(ERR, PORT, "%s: Invalid file descriptor\n", __func__);
83 RTE_LOG(ERR, PORT, "%s: Invalid MTU\n", __func__);
86 if (conf->mempool == NULL) {
87 RTE_LOG(ERR, PORT, "%s: Invalid mempool\n", __func__);
91 /* Memory allocation */
92 port = rte_zmalloc_socket("PORT", sizeof(*port),
93 RTE_CACHE_LINE_SIZE, socket_id);
95 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
101 port->mtu = conf->mtu;
102 port->mempool = conf->mempool;
108 rte_port_fd_reader_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
110 struct rte_port_fd_reader *p = port;
113 if (rte_pktmbuf_alloc_bulk(p->mempool, pkts, n_pkts) != 0)
116 for (i = 0; i < n_pkts; i++) {
117 struct rte_mbuf *pkt = pkts[i];
118 void *pkt_data = rte_pktmbuf_mtod(pkt, void *);
121 n_bytes = read(p->fd, pkt_data, (size_t) p->mtu);
125 pkt->data_len = n_bytes;
126 pkt->pkt_len = n_bytes;
129 for (j = i; j < n_pkts; j++)
130 rte_pktmbuf_free(pkts[j]);
132 RTE_PORT_FD_READER_STATS_PKTS_IN_ADD(p, i);
138 rte_port_fd_reader_free(void *port)
141 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
150 static int rte_port_fd_reader_stats_read(void *port,
151 struct rte_port_in_stats *stats, int clear)
153 struct rte_port_fd_reader *p =
157 memcpy(stats, &p->stats, sizeof(p->stats));
160 memset(&p->stats, 0, sizeof(p->stats));
168 #ifdef RTE_PORT_STATS_COLLECT
170 #define RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(port, val) \
171 do { port->stats.n_pkts_in += val; } while (0)
172 #define RTE_PORT_FD_WRITER_STATS_PKTS_DROP_ADD(port, val) \
173 do { port->stats.n_pkts_drop += val; } while (0)
177 #define RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(port, val)
178 #define RTE_PORT_FD_WRITER_STATS_PKTS_DROP_ADD(port, val)
182 struct rte_port_fd_writer {
183 struct rte_port_out_stats stats;
185 struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
186 uint32_t tx_burst_sz;
187 uint16_t tx_buf_count;
192 rte_port_fd_writer_create(void *params, int socket_id)
194 struct rte_port_fd_writer_params *conf =
196 struct rte_port_fd_writer *port;
198 /* Check input parameters */
199 if ((conf == NULL) ||
200 (conf->tx_burst_sz == 0) ||
201 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
202 (!rte_is_power_of_2(conf->tx_burst_sz))) {
203 RTE_LOG(ERR, PORT, "%s: Invalid input parameters\n", __func__);
207 /* Memory allocation */
208 port = rte_zmalloc_socket("PORT", sizeof(*port),
209 RTE_CACHE_LINE_SIZE, socket_id);
211 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
217 port->tx_burst_sz = conf->tx_burst_sz;
218 port->tx_buf_count = 0;
224 send_burst(struct rte_port_fd_writer *p)
228 for (i = 0; i < p->tx_buf_count; i++) {
229 struct rte_mbuf *pkt = p->tx_buf[i];
230 void *pkt_data = rte_pktmbuf_mtod(pkt, void*);
231 size_t n_bytes = rte_pktmbuf_data_len(pkt);
234 ret = write(p->fd, pkt_data, n_bytes);
239 RTE_PORT_FD_WRITER_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - i);
241 for (i = 0; i < p->tx_buf_count; i++)
242 rte_pktmbuf_free(p->tx_buf[i]);
248 rte_port_fd_writer_tx(void *port, struct rte_mbuf *pkt)
250 struct rte_port_fd_writer *p =
253 p->tx_buf[p->tx_buf_count++] = pkt;
254 RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(p, 1);
255 if (p->tx_buf_count >= p->tx_burst_sz)
262 rte_port_fd_writer_tx_bulk(void *port,
263 struct rte_mbuf **pkts,
266 struct rte_port_fd_writer *p =
268 uint32_t tx_buf_count = p->tx_buf_count;
270 if ((pkts_mask & (pkts_mask + 1)) == 0) {
271 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
274 for (i = 0; i < n_pkts; i++)
275 p->tx_buf[tx_buf_count++] = pkts[i];
276 RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(p, n_pkts);
278 for ( ; pkts_mask; ) {
279 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
280 uint64_t pkt_mask = 1LLU << pkt_index;
281 struct rte_mbuf *pkt = pkts[pkt_index];
283 p->tx_buf[tx_buf_count++] = pkt;
284 RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(p, 1);
285 pkts_mask &= ~pkt_mask;
288 p->tx_buf_count = tx_buf_count;
289 if (tx_buf_count >= p->tx_burst_sz)
296 rte_port_fd_writer_flush(void *port)
298 struct rte_port_fd_writer *p =
301 if (p->tx_buf_count > 0)
308 rte_port_fd_writer_free(void *port)
311 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
315 rte_port_fd_writer_flush(port);
321 static int rte_port_fd_writer_stats_read(void *port,
322 struct rte_port_out_stats *stats, int clear)
324 struct rte_port_fd_writer *p =
328 memcpy(stats, &p->stats, sizeof(p->stats));
331 memset(&p->stats, 0, sizeof(p->stats));
337 * Port FD Writer Nodrop
339 #ifdef RTE_PORT_STATS_COLLECT
341 #define RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(port, val) \
342 do { port->stats.n_pkts_in += val; } while (0)
343 #define RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_DROP_ADD(port, val) \
344 do { port->stats.n_pkts_drop += val; } while (0)
348 #define RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(port, val)
349 #define RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_DROP_ADD(port, val)
353 struct rte_port_fd_writer_nodrop {
354 struct rte_port_out_stats stats;
356 struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
357 uint32_t tx_burst_sz;
358 uint16_t tx_buf_count;
364 rte_port_fd_writer_nodrop_create(void *params, int socket_id)
366 struct rte_port_fd_writer_nodrop_params *conf =
368 struct rte_port_fd_writer_nodrop *port;
370 /* Check input parameters */
371 if ((conf == NULL) ||
373 (conf->tx_burst_sz == 0) ||
374 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
375 (!rte_is_power_of_2(conf->tx_burst_sz))) {
376 RTE_LOG(ERR, PORT, "%s: Invalid input parameters\n", __func__);
380 /* Memory allocation */
381 port = rte_zmalloc_socket("PORT", sizeof(*port),
382 RTE_CACHE_LINE_SIZE, socket_id);
384 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
390 port->tx_burst_sz = conf->tx_burst_sz;
391 port->tx_buf_count = 0;
394 * When n_retries is 0 it means that we should wait for every packet to
395 * send no matter how many retries should it take. To limit number of
396 * branches in fast path, we use UINT64_MAX instead of branching.
398 port->n_retries = (conf->n_retries == 0) ? UINT64_MAX : conf->n_retries;
404 send_burst_nodrop(struct rte_port_fd_writer_nodrop *p)
410 for (i = 0; (i < p->tx_buf_count) && (n_retries < p->n_retries); i++) {
411 struct rte_mbuf *pkt = p->tx_buf[i];
412 void *pkt_data = rte_pktmbuf_mtod(pkt, void*);
413 size_t n_bytes = rte_pktmbuf_data_len(pkt);
415 for ( ; n_retries < p->n_retries; n_retries++) {
418 ret = write(p->fd, pkt_data, n_bytes);
424 RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - i);
426 for (i = 0; i < p->tx_buf_count; i++)
427 rte_pktmbuf_free(p->tx_buf[i]);
433 rte_port_fd_writer_nodrop_tx(void *port, struct rte_mbuf *pkt)
435 struct rte_port_fd_writer_nodrop *p =
438 p->tx_buf[p->tx_buf_count++] = pkt;
439 RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(p, 1);
440 if (p->tx_buf_count >= p->tx_burst_sz)
441 send_burst_nodrop(p);
447 rte_port_fd_writer_nodrop_tx_bulk(void *port,
448 struct rte_mbuf **pkts,
451 struct rte_port_fd_writer_nodrop *p =
453 uint32_t tx_buf_count = p->tx_buf_count;
455 if ((pkts_mask & (pkts_mask + 1)) == 0) {
456 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
459 for (i = 0; i < n_pkts; i++)
460 p->tx_buf[tx_buf_count++] = pkts[i];
461 RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(p, n_pkts);
463 for ( ; pkts_mask; ) {
464 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
465 uint64_t pkt_mask = 1LLU << pkt_index;
466 struct rte_mbuf *pkt = pkts[pkt_index];
468 p->tx_buf[tx_buf_count++] = pkt;
469 RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(p, 1);
470 pkts_mask &= ~pkt_mask;
473 p->tx_buf_count = tx_buf_count;
474 if (tx_buf_count >= p->tx_burst_sz)
475 send_burst_nodrop(p);
481 rte_port_fd_writer_nodrop_flush(void *port)
483 struct rte_port_fd_writer_nodrop *p =
486 if (p->tx_buf_count > 0)
487 send_burst_nodrop(p);
493 rte_port_fd_writer_nodrop_free(void *port)
496 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
500 rte_port_fd_writer_nodrop_flush(port);
506 static int rte_port_fd_writer_nodrop_stats_read(void *port,
507 struct rte_port_out_stats *stats, int clear)
509 struct rte_port_fd_writer_nodrop *p =
513 memcpy(stats, &p->stats, sizeof(p->stats));
516 memset(&p->stats, 0, sizeof(p->stats));
522 * Summary of port operations
524 struct rte_port_in_ops rte_port_fd_reader_ops = {
525 .f_create = rte_port_fd_reader_create,
526 .f_free = rte_port_fd_reader_free,
527 .f_rx = rte_port_fd_reader_rx,
528 .f_stats = rte_port_fd_reader_stats_read,
531 struct rte_port_out_ops rte_port_fd_writer_ops = {
532 .f_create = rte_port_fd_writer_create,
533 .f_free = rte_port_fd_writer_free,
534 .f_tx = rte_port_fd_writer_tx,
535 .f_tx_bulk = rte_port_fd_writer_tx_bulk,
536 .f_flush = rte_port_fd_writer_flush,
537 .f_stats = rte_port_fd_writer_stats_read,
540 struct rte_port_out_ops rte_port_fd_writer_nodrop_ops = {
541 .f_create = rte_port_fd_writer_nodrop_create,
542 .f_free = rte_port_fd_writer_nodrop_free,
543 .f_tx = rte_port_fd_writer_nodrop_tx,
544 .f_tx_bulk = rte_port_fd_writer_nodrop_tx_bulk,
545 .f_flush = rte_port_fd_writer_nodrop_flush,
546 .f_stats = rte_port_fd_writer_nodrop_stats_read,