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 =
70 (struct rte_port_fd_reader_params *) params;
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 = (struct rte_port_fd_reader *) port;
113 if (rte_mempool_get_bulk(p->mempool, (void **) pkts, n_pkts) != 0)
116 for (i = 0; i < n_pkts; i++) {
117 rte_mbuf_refcnt_set(pkts[i], 1);
118 rte_pktmbuf_reset(pkts[i]);
121 for (i = 0; i < n_pkts; i++) {
122 struct rte_mbuf *pkt = pkts[i];
123 void *pkt_data = rte_pktmbuf_mtod(pkt, void *);
126 n_bytes = read(p->fd, pkt_data, (size_t) p->mtu);
130 pkt->data_len = n_bytes;
131 pkt->pkt_len = n_bytes;
134 for ( ; i < n_pkts; i++)
135 rte_pktmbuf_free(pkts[i]);
137 RTE_PORT_FD_READER_STATS_PKTS_IN_ADD(p, i);
143 rte_port_fd_reader_free(void *port)
146 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
155 static int rte_port_fd_reader_stats_read(void *port,
156 struct rte_port_in_stats *stats, int clear)
158 struct rte_port_fd_reader *p =
159 (struct rte_port_fd_reader *) port;
162 memcpy(stats, &p->stats, sizeof(p->stats));
165 memset(&p->stats, 0, sizeof(p->stats));
173 #ifdef RTE_PORT_STATS_COLLECT
175 #define RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(port, val) \
176 do { port->stats.n_pkts_in += val; } while (0)
177 #define RTE_PORT_FD_WRITER_STATS_PKTS_DROP_ADD(port, val) \
178 do { port->stats.n_pkts_drop += val; } while (0)
182 #define RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(port, val)
183 #define RTE_PORT_FD_WRITER_STATS_PKTS_DROP_ADD(port, val)
187 struct rte_port_fd_writer {
188 struct rte_port_out_stats stats;
190 struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
191 uint32_t tx_burst_sz;
192 uint16_t tx_buf_count;
197 rte_port_fd_writer_create(void *params, int socket_id)
199 struct rte_port_fd_writer_params *conf =
200 (struct rte_port_fd_writer_params *) params;
201 struct rte_port_fd_writer *port;
203 /* Check input parameters */
204 if ((conf == NULL) ||
205 (conf->tx_burst_sz == 0) ||
206 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
207 (!rte_is_power_of_2(conf->tx_burst_sz))) {
208 RTE_LOG(ERR, PORT, "%s: Invalid input parameters\n", __func__);
212 /* Memory allocation */
213 port = rte_zmalloc_socket("PORT", sizeof(*port),
214 RTE_CACHE_LINE_SIZE, socket_id);
216 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
222 port->tx_burst_sz = conf->tx_burst_sz;
223 port->tx_buf_count = 0;
229 send_burst(struct rte_port_fd_writer *p)
233 for (i = 0; i < p->tx_buf_count; i++) {
234 struct rte_mbuf *pkt = p->tx_buf[i];
235 void *pkt_data = rte_pktmbuf_mtod(pkt, void*);
236 size_t n_bytes = rte_pktmbuf_data_len(pkt);
239 ret = write(p->fd, pkt_data, n_bytes);
244 RTE_PORT_FD_WRITER_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - i);
246 for (i = 0; i < p->tx_buf_count; i++)
247 rte_pktmbuf_free(p->tx_buf[i]);
253 rte_port_fd_writer_tx(void *port, struct rte_mbuf *pkt)
255 struct rte_port_fd_writer *p =
256 (struct rte_port_fd_writer *) port;
258 p->tx_buf[p->tx_buf_count++] = pkt;
259 RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(p, 1);
260 if (p->tx_buf_count >= p->tx_burst_sz)
267 rte_port_fd_writer_tx_bulk(void *port,
268 struct rte_mbuf **pkts,
271 struct rte_port_fd_writer *p =
272 (struct rte_port_fd_writer *) port;
273 uint32_t tx_buf_count = p->tx_buf_count;
275 if ((pkts_mask & (pkts_mask + 1)) == 0) {
276 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
279 for (i = 0; i < n_pkts; i++)
280 p->tx_buf[tx_buf_count++] = pkts[i];
281 RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(p, n_pkts);
283 for ( ; pkts_mask; ) {
284 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
285 uint64_t pkt_mask = 1LLU << pkt_index;
286 struct rte_mbuf *pkt = pkts[pkt_index];
288 p->tx_buf[tx_buf_count++] = pkt;
289 RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(p, 1);
290 pkts_mask &= ~pkt_mask;
293 p->tx_buf_count = tx_buf_count;
294 if (tx_buf_count >= p->tx_burst_sz)
301 rte_port_fd_writer_flush(void *port)
303 struct rte_port_fd_writer *p =
304 (struct rte_port_fd_writer *) port;
306 if (p->tx_buf_count > 0)
313 rte_port_fd_writer_free(void *port)
316 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
320 rte_port_fd_writer_flush(port);
326 static int rte_port_fd_writer_stats_read(void *port,
327 struct rte_port_out_stats *stats, int clear)
329 struct rte_port_fd_writer *p =
330 (struct rte_port_fd_writer *) port;
333 memcpy(stats, &p->stats, sizeof(p->stats));
336 memset(&p->stats, 0, sizeof(p->stats));
342 * Port FD Writer Nodrop
344 #ifdef RTE_PORT_STATS_COLLECT
346 #define RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(port, val) \
347 do { port->stats.n_pkts_in += val; } while (0)
348 #define RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_DROP_ADD(port, val) \
349 do { port->stats.n_pkts_drop += val; } while (0)
353 #define RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(port, val)
354 #define RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_DROP_ADD(port, val)
358 struct rte_port_fd_writer_nodrop {
359 struct rte_port_out_stats stats;
361 struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
362 uint32_t tx_burst_sz;
363 uint16_t tx_buf_count;
369 rte_port_fd_writer_nodrop_create(void *params, int socket_id)
371 struct rte_port_fd_writer_nodrop_params *conf =
372 (struct rte_port_fd_writer_nodrop_params *) params;
373 struct rte_port_fd_writer_nodrop *port;
375 /* Check input parameters */
376 if ((conf == NULL) ||
378 (conf->tx_burst_sz == 0) ||
379 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
380 (!rte_is_power_of_2(conf->tx_burst_sz))) {
381 RTE_LOG(ERR, PORT, "%s: Invalid input parameters\n", __func__);
385 /* Memory allocation */
386 port = rte_zmalloc_socket("PORT", sizeof(*port),
387 RTE_CACHE_LINE_SIZE, socket_id);
389 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
395 port->tx_burst_sz = conf->tx_burst_sz;
396 port->tx_buf_count = 0;
399 * When n_retries is 0 it means that we should wait for every packet to
400 * send no matter how many retries should it take. To limit number of
401 * branches in fast path, we use UINT64_MAX instead of branching.
403 port->n_retries = (conf->n_retries == 0) ? UINT64_MAX : conf->n_retries;
409 send_burst_nodrop(struct rte_port_fd_writer_nodrop *p)
415 for (i = 0; (i < p->tx_buf_count) && (n_retries < p->n_retries); i++) {
416 struct rte_mbuf *pkt = p->tx_buf[i];
417 void *pkt_data = rte_pktmbuf_mtod(pkt, void*);
418 size_t n_bytes = rte_pktmbuf_data_len(pkt);
420 for ( ; n_retries < p->n_retries; n_retries++) {
423 ret = write(p->fd, pkt_data, n_bytes);
429 RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - i);
431 for (i = 0; i < p->tx_buf_count; i++)
432 rte_pktmbuf_free(p->tx_buf[i]);
438 rte_port_fd_writer_nodrop_tx(void *port, struct rte_mbuf *pkt)
440 struct rte_port_fd_writer_nodrop *p =
441 (struct rte_port_fd_writer_nodrop *) port;
443 p->tx_buf[p->tx_buf_count++] = pkt;
444 RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(p, 1);
445 if (p->tx_buf_count >= p->tx_burst_sz)
446 send_burst_nodrop(p);
452 rte_port_fd_writer_nodrop_tx_bulk(void *port,
453 struct rte_mbuf **pkts,
456 struct rte_port_fd_writer_nodrop *p =
457 (struct rte_port_fd_writer_nodrop *) port;
458 uint32_t tx_buf_count = p->tx_buf_count;
460 if ((pkts_mask & (pkts_mask + 1)) == 0) {
461 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
464 for (i = 0; i < n_pkts; i++)
465 p->tx_buf[tx_buf_count++] = pkts[i];
466 RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(p, n_pkts);
468 for ( ; pkts_mask; ) {
469 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
470 uint64_t pkt_mask = 1LLU << pkt_index;
471 struct rte_mbuf *pkt = pkts[pkt_index];
473 p->tx_buf[tx_buf_count++] = pkt;
474 RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(p, 1);
475 pkts_mask &= ~pkt_mask;
478 p->tx_buf_count = tx_buf_count;
479 if (tx_buf_count >= p->tx_burst_sz)
480 send_burst_nodrop(p);
486 rte_port_fd_writer_nodrop_flush(void *port)
488 struct rte_port_fd_writer_nodrop *p =
489 (struct rte_port_fd_writer_nodrop *) port;
491 if (p->tx_buf_count > 0)
492 send_burst_nodrop(p);
498 rte_port_fd_writer_nodrop_free(void *port)
501 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
505 rte_port_fd_writer_nodrop_flush(port);
511 static int rte_port_fd_writer_nodrop_stats_read(void *port,
512 struct rte_port_out_stats *stats, int clear)
514 struct rte_port_fd_writer_nodrop *p =
515 (struct rte_port_fd_writer_nodrop *) port;
518 memcpy(stats, &p->stats, sizeof(p->stats));
521 memset(&p->stats, 0, sizeof(p->stats));
527 * Summary of port operations
529 struct rte_port_in_ops rte_port_fd_reader_ops = {
530 .f_create = rte_port_fd_reader_create,
531 .f_free = rte_port_fd_reader_free,
532 .f_rx = rte_port_fd_reader_rx,
533 .f_stats = rte_port_fd_reader_stats_read,
536 struct rte_port_out_ops rte_port_fd_writer_ops = {
537 .f_create = rte_port_fd_writer_create,
538 .f_free = rte_port_fd_writer_free,
539 .f_tx = rte_port_fd_writer_tx,
540 .f_tx_bulk = rte_port_fd_writer_tx_bulk,
541 .f_flush = rte_port_fd_writer_flush,
542 .f_stats = rte_port_fd_writer_stats_read,
545 struct rte_port_out_ops rte_port_fd_writer_nodrop_ops = {
546 .f_create = rte_port_fd_writer_nodrop_create,
547 .f_free = rte_port_fd_writer_nodrop_free,
548 .f_tx = rte_port_fd_writer_nodrop_tx,
549 .f_tx_bulk = rte_port_fd_writer_nodrop_tx_bulk,
550 .f_flush = rte_port_fd_writer_nodrop_flush,
551 .f_stats = rte_port_fd_writer_nodrop_stats_read,