1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2021 Intel Corporation
9 #include <rte_malloc.h>
10 #include <rte_hexdump.h>
12 #include "rte_swx_port_fd.h"
19 #define TRACE(...) printf(__VA_ARGS__)
32 struct rte_mempool *mempool;
35 struct rte_swx_port_in_stats stats;
36 struct rte_mbuf **pkts;
42 reader_create(void *args)
44 struct rte_swx_port_fd_reader_params *conf = args;
47 /* Check input parameters. */
48 if (!conf || conf->fd < 0 || conf->mtu == 0 || !conf->mempool)
51 /* Memory allocation. */
52 p = calloc(1, sizeof(struct reader));
56 p->pkts = calloc(conf->burst_size, sizeof(struct rte_mbuf *));
63 p->params.fd = conf->fd;
64 p->params.mtu = conf->mtu;
65 p->params.burst_size = conf->burst_size;
66 p->params.mempool = conf->mempool;
72 reader_free(void *port)
74 struct reader *p = port;
80 for (i = 0; i < p->n_pkts; i++)
81 rte_pktmbuf_free(p->pkts[i]);
88 reader_pkt_rx(void *port, struct rte_swx_pkt *pkt)
90 struct reader *p = port;
96 if (p->n_pkts == p->pos) {
97 if (rte_pktmbuf_alloc_bulk(p->params.mempool, p->pkts, p->params.burst_size) != 0)
100 for (i = 0; i < p->params.burst_size; i++) {
102 pkt_data = rte_pktmbuf_mtod(m, void *);
103 n_bytes = read(p->params.fd, pkt_data, (size_t) p->params.mtu);
108 m->data_len = n_bytes;
109 m->pkt_len = n_bytes;
112 p->stats.n_bytes += n_bytes;
115 for (j = i; j < p->params.burst_size; j++)
116 rte_pktmbuf_free(p->pkts[j]);
125 m = p->pkts[p->pos++];
127 pkt->pkt = m->buf_addr;
128 pkt->offset = m->data_off;
129 pkt->length = m->pkt_len;
131 TRACE("[FD %u] Pkt %d (%u bytes at offset %u)\n",
132 (uint32_t)p->params.fd,
138 rte_hexdump(stdout, NULL,
139 &((uint8_t *)m->buf_addr)[m->data_off], m->data_len);
145 reader_stats_read(void *port, struct rte_swx_port_in_stats *stats)
147 struct reader *p = port;
149 memcpy(stats, &p->stats, sizeof(p->stats));
160 struct rte_mempool *mempool;
163 struct rte_swx_port_out_stats stats;
164 struct rte_mbuf **pkts;
169 writer_create(void *args)
171 struct rte_swx_port_fd_writer_params *conf = args;
174 /* Check input parameters. */
178 /* Memory allocation. */
179 p = calloc(1, sizeof(struct writer));
184 p->pkts = calloc(conf->burst_size, sizeof(struct rte_mbuf *));
190 /* Initialization. */
191 p->params.fd = conf->fd;
192 p->params.burst_size = conf->burst_size;
198 __writer_flush(struct writer *p)
200 struct rte_mbuf *pkt;
206 for (i = 0; i < p->n_pkts; i++) {
208 pkt_data = rte_pktmbuf_mtod(pkt, void*);
209 n_bytes = rte_pktmbuf_data_len(pkt);
211 ret = write(p->params.fd, pkt_data, n_bytes);
216 TRACE("[FD %u] %u packets out\n",
217 (uint32_t)p->params.fd,
220 for (i = 0; i < p->n_pkts; i++)
221 rte_pktmbuf_free(p->pkts[i]);
227 writer_pkt_tx(void *port, struct rte_swx_pkt *pkt)
229 struct writer *p = port;
230 struct rte_mbuf *m = pkt->handle;
232 TRACE("[FD %u] Pkt %u (%u bytes at offset %u)\n",
233 (uint32_t)p->params.fd,
239 rte_hexdump(stdout, NULL, &pkt->pkt[pkt->offset], pkt->length);
241 m->pkt_len = pkt->length;
242 m->data_len = (uint16_t)pkt->length;
243 m->data_off = (uint16_t)pkt->offset;
246 p->stats.n_bytes += pkt->length;
248 p->pkts[p->n_pkts++] = m;
249 if (p->n_pkts == p->params.burst_size)
254 writer_flush(void *port)
256 struct writer *p = port;
263 writer_free(void *port)
265 struct writer *p = port;
276 writer_stats_read(void *port, struct rte_swx_port_out_stats *stats)
278 struct writer *p = port;
280 memcpy(stats, &p->stats, sizeof(p->stats));
284 * Summary of port operations
286 struct rte_swx_port_in_ops rte_swx_port_fd_reader_ops = {
287 .create = reader_create,
289 .pkt_rx = reader_pkt_rx,
290 .stats_read = reader_stats_read,
293 struct rte_swx_port_out_ops rte_swx_port_fd_writer_ops = {
294 .create = writer_create,
296 .pkt_tx = writer_pkt_tx,
297 .flush = writer_flush,
298 .stats_read = writer_stats_read,