xref: /dpdk/lib/port/rte_port_fd.c (revision ae67895b507bb6af22263c79ba0d5c374b396485)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2016 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson #include <string.h>
599a2dd95SBruce Richardson #include <stdint.h>
699a2dd95SBruce Richardson #include <unistd.h>
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson #include <rte_mbuf.h>
999a2dd95SBruce Richardson #include <rte_malloc.h>
1099a2dd95SBruce Richardson 
1199a2dd95SBruce Richardson #include "rte_port_fd.h"
1299a2dd95SBruce Richardson 
13*ae67895bSDavid Marchand #include "port_log.h"
14*ae67895bSDavid Marchand 
1599a2dd95SBruce Richardson /*
1699a2dd95SBruce Richardson  * Port FD Reader
1799a2dd95SBruce Richardson  */
1899a2dd95SBruce Richardson #ifdef RTE_PORT_STATS_COLLECT
1999a2dd95SBruce Richardson 
2099a2dd95SBruce Richardson #define RTE_PORT_FD_READER_STATS_PKTS_IN_ADD(port, val) \
2199a2dd95SBruce Richardson 	do { port->stats.n_pkts_in += val; } while (0)
2299a2dd95SBruce Richardson #define RTE_PORT_FD_READER_STATS_PKTS_DROP_ADD(port, val) \
2399a2dd95SBruce Richardson 	do { port->stats.n_pkts_drop += val; } while (0)
2499a2dd95SBruce Richardson 
2599a2dd95SBruce Richardson #else
2699a2dd95SBruce Richardson 
2799a2dd95SBruce Richardson #define RTE_PORT_FD_READER_STATS_PKTS_IN_ADD(port, val)
2899a2dd95SBruce Richardson #define RTE_PORT_FD_READER_STATS_PKTS_DROP_ADD(port, val)
2999a2dd95SBruce Richardson 
3099a2dd95SBruce Richardson #endif
3199a2dd95SBruce Richardson 
3299a2dd95SBruce Richardson struct rte_port_fd_reader {
3399a2dd95SBruce Richardson 	struct rte_port_in_stats stats;
3499a2dd95SBruce Richardson 	int fd;
3599a2dd95SBruce Richardson 	uint32_t mtu;
3699a2dd95SBruce Richardson 	struct rte_mempool *mempool;
3799a2dd95SBruce Richardson };
3899a2dd95SBruce Richardson 
3999a2dd95SBruce Richardson static void *
rte_port_fd_reader_create(void * params,int socket_id)4099a2dd95SBruce Richardson rte_port_fd_reader_create(void *params, int socket_id)
4199a2dd95SBruce Richardson {
4299a2dd95SBruce Richardson 	struct rte_port_fd_reader_params *conf =
4399a2dd95SBruce Richardson 			params;
4499a2dd95SBruce Richardson 	struct rte_port_fd_reader *port;
4599a2dd95SBruce Richardson 
4699a2dd95SBruce Richardson 	/* Check input parameters */
4799a2dd95SBruce Richardson 	if (conf == NULL) {
48*ae67895bSDavid Marchand 		PORT_LOG(ERR, "%s: params is NULL", __func__);
4999a2dd95SBruce Richardson 		return NULL;
5099a2dd95SBruce Richardson 	}
5199a2dd95SBruce Richardson 	if (conf->fd < 0) {
52*ae67895bSDavid Marchand 		PORT_LOG(ERR, "%s: Invalid file descriptor", __func__);
5399a2dd95SBruce Richardson 		return NULL;
5499a2dd95SBruce Richardson 	}
5599a2dd95SBruce Richardson 	if (conf->mtu == 0) {
56*ae67895bSDavid Marchand 		PORT_LOG(ERR, "%s: Invalid MTU", __func__);
5799a2dd95SBruce Richardson 		return NULL;
5899a2dd95SBruce Richardson 	}
5999a2dd95SBruce Richardson 	if (conf->mempool == NULL) {
60*ae67895bSDavid Marchand 		PORT_LOG(ERR, "%s: Invalid mempool", __func__);
6199a2dd95SBruce Richardson 		return NULL;
6299a2dd95SBruce Richardson 	}
6399a2dd95SBruce Richardson 
6499a2dd95SBruce Richardson 	/* Memory allocation */
6599a2dd95SBruce Richardson 	port = rte_zmalloc_socket("PORT", sizeof(*port),
6699a2dd95SBruce Richardson 			RTE_CACHE_LINE_SIZE, socket_id);
6799a2dd95SBruce Richardson 	if (port == NULL) {
68*ae67895bSDavid Marchand 		PORT_LOG(ERR, "%s: Failed to allocate port", __func__);
6999a2dd95SBruce Richardson 		return NULL;
7099a2dd95SBruce Richardson 	}
7199a2dd95SBruce Richardson 
7299a2dd95SBruce Richardson 	/* Initialization */
7399a2dd95SBruce Richardson 	port->fd = conf->fd;
7499a2dd95SBruce Richardson 	port->mtu = conf->mtu;
7599a2dd95SBruce Richardson 	port->mempool = conf->mempool;
7699a2dd95SBruce Richardson 
7799a2dd95SBruce Richardson 	return port;
7899a2dd95SBruce Richardson }
7999a2dd95SBruce Richardson 
8099a2dd95SBruce Richardson static int
rte_port_fd_reader_rx(void * port,struct rte_mbuf ** pkts,uint32_t n_pkts)8199a2dd95SBruce Richardson rte_port_fd_reader_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
8299a2dd95SBruce Richardson {
8399a2dd95SBruce Richardson 	struct rte_port_fd_reader *p = port;
8499a2dd95SBruce Richardson 	uint32_t i, j;
8599a2dd95SBruce Richardson 
8699a2dd95SBruce Richardson 	if (rte_pktmbuf_alloc_bulk(p->mempool, pkts, n_pkts) != 0)
8799a2dd95SBruce Richardson 		return 0;
8899a2dd95SBruce Richardson 
8999a2dd95SBruce Richardson 	for (i = 0; i < n_pkts; i++) {
9099a2dd95SBruce Richardson 		struct rte_mbuf *pkt = pkts[i];
9199a2dd95SBruce Richardson 		void *pkt_data = rte_pktmbuf_mtod(pkt, void *);
9299a2dd95SBruce Richardson 		ssize_t n_bytes;
9399a2dd95SBruce Richardson 
9499a2dd95SBruce Richardson 		n_bytes = read(p->fd, pkt_data, (size_t) p->mtu);
9599a2dd95SBruce Richardson 		if (n_bytes <= 0)
9699a2dd95SBruce Richardson 			break;
9799a2dd95SBruce Richardson 
9899a2dd95SBruce Richardson 		pkt->data_len = n_bytes;
9999a2dd95SBruce Richardson 		pkt->pkt_len = n_bytes;
10099a2dd95SBruce Richardson 	}
10199a2dd95SBruce Richardson 
10299a2dd95SBruce Richardson 	for (j = i; j < n_pkts; j++)
10399a2dd95SBruce Richardson 		rte_pktmbuf_free(pkts[j]);
10499a2dd95SBruce Richardson 
10599a2dd95SBruce Richardson 	RTE_PORT_FD_READER_STATS_PKTS_IN_ADD(p, i);
10699a2dd95SBruce Richardson 
10799a2dd95SBruce Richardson 	return i;
10899a2dd95SBruce Richardson }
10999a2dd95SBruce Richardson 
11099a2dd95SBruce Richardson static int
rte_port_fd_reader_free(void * port)11199a2dd95SBruce Richardson rte_port_fd_reader_free(void *port)
11299a2dd95SBruce Richardson {
11399a2dd95SBruce Richardson 	if (port == NULL) {
114*ae67895bSDavid Marchand 		PORT_LOG(ERR, "%s: port is NULL", __func__);
11599a2dd95SBruce Richardson 		return -EINVAL;
11699a2dd95SBruce Richardson 	}
11799a2dd95SBruce Richardson 
11899a2dd95SBruce Richardson 	rte_free(port);
11999a2dd95SBruce Richardson 
12099a2dd95SBruce Richardson 	return 0;
12199a2dd95SBruce Richardson }
12299a2dd95SBruce Richardson 
rte_port_fd_reader_stats_read(void * port,struct rte_port_in_stats * stats,int clear)12399a2dd95SBruce Richardson static int rte_port_fd_reader_stats_read(void *port,
12499a2dd95SBruce Richardson 		struct rte_port_in_stats *stats, int clear)
12599a2dd95SBruce Richardson {
12699a2dd95SBruce Richardson 	struct rte_port_fd_reader *p =
12799a2dd95SBruce Richardson 			port;
12899a2dd95SBruce Richardson 
12999a2dd95SBruce Richardson 	if (stats != NULL)
13099a2dd95SBruce Richardson 		memcpy(stats, &p->stats, sizeof(p->stats));
13199a2dd95SBruce Richardson 
13299a2dd95SBruce Richardson 	if (clear)
13399a2dd95SBruce Richardson 		memset(&p->stats, 0, sizeof(p->stats));
13499a2dd95SBruce Richardson 
13599a2dd95SBruce Richardson 	return 0;
13699a2dd95SBruce Richardson }
13799a2dd95SBruce Richardson 
13899a2dd95SBruce Richardson /*
13999a2dd95SBruce Richardson  * Port FD Writer
14099a2dd95SBruce Richardson  */
14199a2dd95SBruce Richardson #ifdef RTE_PORT_STATS_COLLECT
14299a2dd95SBruce Richardson 
14399a2dd95SBruce Richardson #define RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(port, val) \
14499a2dd95SBruce Richardson 	do { port->stats.n_pkts_in += val; } while (0)
14599a2dd95SBruce Richardson #define RTE_PORT_FD_WRITER_STATS_PKTS_DROP_ADD(port, val) \
14699a2dd95SBruce Richardson 	do { port->stats.n_pkts_drop += val; } while (0)
14799a2dd95SBruce Richardson 
14899a2dd95SBruce Richardson #else
14999a2dd95SBruce Richardson 
15099a2dd95SBruce Richardson #define RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(port, val)
15199a2dd95SBruce Richardson #define RTE_PORT_FD_WRITER_STATS_PKTS_DROP_ADD(port, val)
15299a2dd95SBruce Richardson 
15399a2dd95SBruce Richardson #endif
15499a2dd95SBruce Richardson 
15599a2dd95SBruce Richardson struct rte_port_fd_writer {
15699a2dd95SBruce Richardson 	struct rte_port_out_stats stats;
15799a2dd95SBruce Richardson 
15899a2dd95SBruce Richardson 	struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
15999a2dd95SBruce Richardson 	uint32_t tx_burst_sz;
16099a2dd95SBruce Richardson 	uint16_t tx_buf_count;
16199a2dd95SBruce Richardson 	uint32_t fd;
16299a2dd95SBruce Richardson };
16399a2dd95SBruce Richardson 
16499a2dd95SBruce Richardson static void *
rte_port_fd_writer_create(void * params,int socket_id)16599a2dd95SBruce Richardson rte_port_fd_writer_create(void *params, int socket_id)
16699a2dd95SBruce Richardson {
16799a2dd95SBruce Richardson 	struct rte_port_fd_writer_params *conf =
16899a2dd95SBruce Richardson 		params;
16999a2dd95SBruce Richardson 	struct rte_port_fd_writer *port;
17099a2dd95SBruce Richardson 
17199a2dd95SBruce Richardson 	/* Check input parameters */
17299a2dd95SBruce Richardson 	if ((conf == NULL) ||
17399a2dd95SBruce Richardson 		(conf->tx_burst_sz == 0) ||
17499a2dd95SBruce Richardson 		(conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
17599a2dd95SBruce Richardson 		(!rte_is_power_of_2(conf->tx_burst_sz))) {
176*ae67895bSDavid Marchand 		PORT_LOG(ERR, "%s: Invalid input parameters", __func__);
17799a2dd95SBruce Richardson 		return NULL;
17899a2dd95SBruce Richardson 	}
17999a2dd95SBruce Richardson 
18099a2dd95SBruce Richardson 	/* Memory allocation */
18199a2dd95SBruce Richardson 	port = rte_zmalloc_socket("PORT", sizeof(*port),
18299a2dd95SBruce Richardson 		RTE_CACHE_LINE_SIZE, socket_id);
18399a2dd95SBruce Richardson 	if (port == NULL) {
184*ae67895bSDavid Marchand 		PORT_LOG(ERR, "%s: Failed to allocate port", __func__);
18599a2dd95SBruce Richardson 		return NULL;
18699a2dd95SBruce Richardson 	}
18799a2dd95SBruce Richardson 
18899a2dd95SBruce Richardson 	/* Initialization */
18999a2dd95SBruce Richardson 	port->fd = conf->fd;
19099a2dd95SBruce Richardson 	port->tx_burst_sz = conf->tx_burst_sz;
19199a2dd95SBruce Richardson 	port->tx_buf_count = 0;
19299a2dd95SBruce Richardson 
19399a2dd95SBruce Richardson 	return port;
19499a2dd95SBruce Richardson }
19599a2dd95SBruce Richardson 
19699a2dd95SBruce Richardson static inline void
send_burst(struct rte_port_fd_writer * p)19799a2dd95SBruce Richardson send_burst(struct rte_port_fd_writer *p)
19899a2dd95SBruce Richardson {
19999a2dd95SBruce Richardson 	uint32_t i;
20099a2dd95SBruce Richardson 
20199a2dd95SBruce Richardson 	for (i = 0; i < p->tx_buf_count; i++) {
20299a2dd95SBruce Richardson 		struct rte_mbuf *pkt = p->tx_buf[i];
20399a2dd95SBruce Richardson 		void *pkt_data = rte_pktmbuf_mtod(pkt, void*);
20499a2dd95SBruce Richardson 		size_t n_bytes = rte_pktmbuf_data_len(pkt);
20599a2dd95SBruce Richardson 		ssize_t ret;
20699a2dd95SBruce Richardson 
20799a2dd95SBruce Richardson 		ret = write(p->fd, pkt_data, n_bytes);
20899a2dd95SBruce Richardson 		if (ret < 0)
20999a2dd95SBruce Richardson 			break;
21099a2dd95SBruce Richardson 	}
21199a2dd95SBruce Richardson 
21299a2dd95SBruce Richardson 	RTE_PORT_FD_WRITER_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - i);
21399a2dd95SBruce Richardson 
21499a2dd95SBruce Richardson 	for (i = 0; i < p->tx_buf_count; i++)
21599a2dd95SBruce Richardson 		rte_pktmbuf_free(p->tx_buf[i]);
21699a2dd95SBruce Richardson 
21799a2dd95SBruce Richardson 	p->tx_buf_count = 0;
21899a2dd95SBruce Richardson }
21999a2dd95SBruce Richardson 
22099a2dd95SBruce Richardson static int
rte_port_fd_writer_tx(void * port,struct rte_mbuf * pkt)22199a2dd95SBruce Richardson rte_port_fd_writer_tx(void *port, struct rte_mbuf *pkt)
22299a2dd95SBruce Richardson {
22399a2dd95SBruce Richardson 	struct rte_port_fd_writer *p =
22499a2dd95SBruce Richardson 		port;
22599a2dd95SBruce Richardson 
22699a2dd95SBruce Richardson 	p->tx_buf[p->tx_buf_count++] = pkt;
22799a2dd95SBruce Richardson 	RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(p, 1);
22899a2dd95SBruce Richardson 	if (p->tx_buf_count >= p->tx_burst_sz)
22999a2dd95SBruce Richardson 		send_burst(p);
23099a2dd95SBruce Richardson 
23199a2dd95SBruce Richardson 	return 0;
23299a2dd95SBruce Richardson }
23399a2dd95SBruce Richardson 
23499a2dd95SBruce Richardson static int
rte_port_fd_writer_tx_bulk(void * port,struct rte_mbuf ** pkts,uint64_t pkts_mask)23599a2dd95SBruce Richardson rte_port_fd_writer_tx_bulk(void *port,
23699a2dd95SBruce Richardson 	struct rte_mbuf **pkts,
23799a2dd95SBruce Richardson 	uint64_t pkts_mask)
23899a2dd95SBruce Richardson {
23999a2dd95SBruce Richardson 	struct rte_port_fd_writer *p =
24099a2dd95SBruce Richardson 		port;
24199a2dd95SBruce Richardson 	uint32_t tx_buf_count = p->tx_buf_count;
24299a2dd95SBruce Richardson 
24399a2dd95SBruce Richardson 	if ((pkts_mask & (pkts_mask + 1)) == 0) {
2443d4e27fdSDavid Marchand 		uint64_t n_pkts = rte_popcount64(pkts_mask);
24599a2dd95SBruce Richardson 		uint32_t i;
24699a2dd95SBruce Richardson 
24799a2dd95SBruce Richardson 		for (i = 0; i < n_pkts; i++)
24899a2dd95SBruce Richardson 			p->tx_buf[tx_buf_count++] = pkts[i];
24999a2dd95SBruce Richardson 		RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(p, n_pkts);
25099a2dd95SBruce Richardson 	} else
25199a2dd95SBruce Richardson 		for ( ; pkts_mask; ) {
2523d4e27fdSDavid Marchand 			uint32_t pkt_index = rte_ctz64(pkts_mask);
25399a2dd95SBruce Richardson 			uint64_t pkt_mask = 1LLU << pkt_index;
25499a2dd95SBruce Richardson 			struct rte_mbuf *pkt = pkts[pkt_index];
25599a2dd95SBruce Richardson 
25699a2dd95SBruce Richardson 			p->tx_buf[tx_buf_count++] = pkt;
25799a2dd95SBruce Richardson 			RTE_PORT_FD_WRITER_STATS_PKTS_IN_ADD(p, 1);
25899a2dd95SBruce Richardson 			pkts_mask &= ~pkt_mask;
25999a2dd95SBruce Richardson 		}
26099a2dd95SBruce Richardson 
26199a2dd95SBruce Richardson 	p->tx_buf_count = tx_buf_count;
26299a2dd95SBruce Richardson 	if (tx_buf_count >= p->tx_burst_sz)
26399a2dd95SBruce Richardson 		send_burst(p);
26499a2dd95SBruce Richardson 
26599a2dd95SBruce Richardson 	return 0;
26699a2dd95SBruce Richardson }
26799a2dd95SBruce Richardson 
26899a2dd95SBruce Richardson static int
rte_port_fd_writer_flush(void * port)26999a2dd95SBruce Richardson rte_port_fd_writer_flush(void *port)
27099a2dd95SBruce Richardson {
27199a2dd95SBruce Richardson 	struct rte_port_fd_writer *p =
27299a2dd95SBruce Richardson 		port;
27399a2dd95SBruce Richardson 
27499a2dd95SBruce Richardson 	if (p->tx_buf_count > 0)
27599a2dd95SBruce Richardson 		send_burst(p);
27699a2dd95SBruce Richardson 
27799a2dd95SBruce Richardson 	return 0;
27899a2dd95SBruce Richardson }
27999a2dd95SBruce Richardson 
28099a2dd95SBruce Richardson static int
rte_port_fd_writer_free(void * port)28199a2dd95SBruce Richardson rte_port_fd_writer_free(void *port)
28299a2dd95SBruce Richardson {
28399a2dd95SBruce Richardson 	if (port == NULL) {
284*ae67895bSDavid Marchand 		PORT_LOG(ERR, "%s: Port is NULL", __func__);
28599a2dd95SBruce Richardson 		return -EINVAL;
28699a2dd95SBruce Richardson 	}
28799a2dd95SBruce Richardson 
28899a2dd95SBruce Richardson 	rte_port_fd_writer_flush(port);
28999a2dd95SBruce Richardson 	rte_free(port);
29099a2dd95SBruce Richardson 
29199a2dd95SBruce Richardson 	return 0;
29299a2dd95SBruce Richardson }
29399a2dd95SBruce Richardson 
rte_port_fd_writer_stats_read(void * port,struct rte_port_out_stats * stats,int clear)29499a2dd95SBruce Richardson static int rte_port_fd_writer_stats_read(void *port,
29599a2dd95SBruce Richardson 		struct rte_port_out_stats *stats, int clear)
29699a2dd95SBruce Richardson {
29799a2dd95SBruce Richardson 	struct rte_port_fd_writer *p =
29899a2dd95SBruce Richardson 		port;
29999a2dd95SBruce Richardson 
30099a2dd95SBruce Richardson 	if (stats != NULL)
30199a2dd95SBruce Richardson 		memcpy(stats, &p->stats, sizeof(p->stats));
30299a2dd95SBruce Richardson 
30399a2dd95SBruce Richardson 	if (clear)
30499a2dd95SBruce Richardson 		memset(&p->stats, 0, sizeof(p->stats));
30599a2dd95SBruce Richardson 
30699a2dd95SBruce Richardson 	return 0;
30799a2dd95SBruce Richardson }
30899a2dd95SBruce Richardson 
30999a2dd95SBruce Richardson /*
31099a2dd95SBruce Richardson  * Port FD Writer Nodrop
31199a2dd95SBruce Richardson  */
31299a2dd95SBruce Richardson #ifdef RTE_PORT_STATS_COLLECT
31399a2dd95SBruce Richardson 
31499a2dd95SBruce Richardson #define RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(port, val) \
31599a2dd95SBruce Richardson 	do { port->stats.n_pkts_in += val; } while (0)
31699a2dd95SBruce Richardson #define RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_DROP_ADD(port, val) \
31799a2dd95SBruce Richardson 	do { port->stats.n_pkts_drop += val; } while (0)
31899a2dd95SBruce Richardson 
31999a2dd95SBruce Richardson #else
32099a2dd95SBruce Richardson 
32199a2dd95SBruce Richardson #define RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(port, val)
32299a2dd95SBruce Richardson #define RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_DROP_ADD(port, val)
32399a2dd95SBruce Richardson 
32499a2dd95SBruce Richardson #endif
32599a2dd95SBruce Richardson 
32699a2dd95SBruce Richardson struct rte_port_fd_writer_nodrop {
32799a2dd95SBruce Richardson 	struct rte_port_out_stats stats;
32899a2dd95SBruce Richardson 
32999a2dd95SBruce Richardson 	struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
33099a2dd95SBruce Richardson 	uint32_t tx_burst_sz;
33199a2dd95SBruce Richardson 	uint16_t tx_buf_count;
33299a2dd95SBruce Richardson 	uint64_t n_retries;
33399a2dd95SBruce Richardson 	uint32_t fd;
33499a2dd95SBruce Richardson };
33599a2dd95SBruce Richardson 
33699a2dd95SBruce Richardson static void *
rte_port_fd_writer_nodrop_create(void * params,int socket_id)33799a2dd95SBruce Richardson rte_port_fd_writer_nodrop_create(void *params, int socket_id)
33899a2dd95SBruce Richardson {
33999a2dd95SBruce Richardson 	struct rte_port_fd_writer_nodrop_params *conf =
34099a2dd95SBruce Richardson 			params;
34199a2dd95SBruce Richardson 	struct rte_port_fd_writer_nodrop *port;
34299a2dd95SBruce Richardson 
34399a2dd95SBruce Richardson 	/* Check input parameters */
34499a2dd95SBruce Richardson 	if ((conf == NULL) ||
34599a2dd95SBruce Richardson 		(conf->fd < 0) ||
34699a2dd95SBruce Richardson 		(conf->tx_burst_sz == 0) ||
34799a2dd95SBruce Richardson 		(conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
34899a2dd95SBruce Richardson 		(!rte_is_power_of_2(conf->tx_burst_sz))) {
349*ae67895bSDavid Marchand 		PORT_LOG(ERR, "%s: Invalid input parameters", __func__);
35099a2dd95SBruce Richardson 		return NULL;
35199a2dd95SBruce Richardson 	}
35299a2dd95SBruce Richardson 
35399a2dd95SBruce Richardson 	/* Memory allocation */
35499a2dd95SBruce Richardson 	port = rte_zmalloc_socket("PORT", sizeof(*port),
35599a2dd95SBruce Richardson 		RTE_CACHE_LINE_SIZE, socket_id);
35699a2dd95SBruce Richardson 	if (port == NULL) {
357*ae67895bSDavid Marchand 		PORT_LOG(ERR, "%s: Failed to allocate port", __func__);
35899a2dd95SBruce Richardson 		return NULL;
35999a2dd95SBruce Richardson 	}
36099a2dd95SBruce Richardson 
36199a2dd95SBruce Richardson 	/* Initialization */
36299a2dd95SBruce Richardson 	port->fd = conf->fd;
36399a2dd95SBruce Richardson 	port->tx_burst_sz = conf->tx_burst_sz;
36499a2dd95SBruce Richardson 	port->tx_buf_count = 0;
36599a2dd95SBruce Richardson 
36699a2dd95SBruce Richardson 	/*
36799a2dd95SBruce Richardson 	 * When n_retries is 0 it means that we should wait for every packet to
36899a2dd95SBruce Richardson 	 * send no matter how many retries should it take. To limit number of
36999a2dd95SBruce Richardson 	 * branches in fast path, we use UINT64_MAX instead of branching.
37099a2dd95SBruce Richardson 	 */
37199a2dd95SBruce Richardson 	port->n_retries = (conf->n_retries == 0) ? UINT64_MAX : conf->n_retries;
37299a2dd95SBruce Richardson 
37399a2dd95SBruce Richardson 	return port;
37499a2dd95SBruce Richardson }
37599a2dd95SBruce Richardson 
37699a2dd95SBruce Richardson static inline void
send_burst_nodrop(struct rte_port_fd_writer_nodrop * p)37799a2dd95SBruce Richardson send_burst_nodrop(struct rte_port_fd_writer_nodrop *p)
37899a2dd95SBruce Richardson {
37999a2dd95SBruce Richardson 	uint64_t n_retries;
38099a2dd95SBruce Richardson 	uint32_t i;
38199a2dd95SBruce Richardson 
38299a2dd95SBruce Richardson 	n_retries = 0;
38399a2dd95SBruce Richardson 	for (i = 0; (i < p->tx_buf_count) && (n_retries < p->n_retries); i++) {
38499a2dd95SBruce Richardson 		struct rte_mbuf *pkt = p->tx_buf[i];
38599a2dd95SBruce Richardson 		void *pkt_data = rte_pktmbuf_mtod(pkt, void*);
38699a2dd95SBruce Richardson 		size_t n_bytes = rte_pktmbuf_data_len(pkt);
38799a2dd95SBruce Richardson 
38899a2dd95SBruce Richardson 		for ( ; n_retries < p->n_retries; n_retries++) {
38999a2dd95SBruce Richardson 			ssize_t ret;
39099a2dd95SBruce Richardson 
39199a2dd95SBruce Richardson 			ret = write(p->fd, pkt_data, n_bytes);
39299a2dd95SBruce Richardson 			if (ret)
39399a2dd95SBruce Richardson 				break;
39499a2dd95SBruce Richardson 		}
39599a2dd95SBruce Richardson 	}
39699a2dd95SBruce Richardson 
39799a2dd95SBruce Richardson 	RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - i);
39899a2dd95SBruce Richardson 
39999a2dd95SBruce Richardson 	for (i = 0; i < p->tx_buf_count; i++)
40099a2dd95SBruce Richardson 		rte_pktmbuf_free(p->tx_buf[i]);
40199a2dd95SBruce Richardson 
40299a2dd95SBruce Richardson 	p->tx_buf_count = 0;
40399a2dd95SBruce Richardson }
40499a2dd95SBruce Richardson 
40599a2dd95SBruce Richardson static int
rte_port_fd_writer_nodrop_tx(void * port,struct rte_mbuf * pkt)40699a2dd95SBruce Richardson rte_port_fd_writer_nodrop_tx(void *port, struct rte_mbuf *pkt)
40799a2dd95SBruce Richardson {
40899a2dd95SBruce Richardson 	struct rte_port_fd_writer_nodrop *p =
40999a2dd95SBruce Richardson 		port;
41099a2dd95SBruce Richardson 
41199a2dd95SBruce Richardson 	p->tx_buf[p->tx_buf_count++] = pkt;
41299a2dd95SBruce Richardson 	RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(p, 1);
41399a2dd95SBruce Richardson 	if (p->tx_buf_count >= p->tx_burst_sz)
41499a2dd95SBruce Richardson 		send_burst_nodrop(p);
41599a2dd95SBruce Richardson 
41699a2dd95SBruce Richardson 	return 0;
41799a2dd95SBruce Richardson }
41899a2dd95SBruce Richardson 
41999a2dd95SBruce Richardson static int
rte_port_fd_writer_nodrop_tx_bulk(void * port,struct rte_mbuf ** pkts,uint64_t pkts_mask)42099a2dd95SBruce Richardson rte_port_fd_writer_nodrop_tx_bulk(void *port,
42199a2dd95SBruce Richardson 	struct rte_mbuf **pkts,
42299a2dd95SBruce Richardson 	uint64_t pkts_mask)
42399a2dd95SBruce Richardson {
42499a2dd95SBruce Richardson 	struct rte_port_fd_writer_nodrop *p =
42599a2dd95SBruce Richardson 		port;
42699a2dd95SBruce Richardson 	uint32_t tx_buf_count = p->tx_buf_count;
42799a2dd95SBruce Richardson 
42899a2dd95SBruce Richardson 	if ((pkts_mask & (pkts_mask + 1)) == 0) {
4293d4e27fdSDavid Marchand 		uint64_t n_pkts = rte_popcount64(pkts_mask);
43099a2dd95SBruce Richardson 		uint32_t i;
43199a2dd95SBruce Richardson 
43299a2dd95SBruce Richardson 		for (i = 0; i < n_pkts; i++)
43399a2dd95SBruce Richardson 			p->tx_buf[tx_buf_count++] = pkts[i];
43499a2dd95SBruce Richardson 		RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(p, n_pkts);
43599a2dd95SBruce Richardson 	} else
43699a2dd95SBruce Richardson 		for ( ; pkts_mask; ) {
4373d4e27fdSDavid Marchand 			uint32_t pkt_index = rte_ctz64(pkts_mask);
43899a2dd95SBruce Richardson 			uint64_t pkt_mask = 1LLU << pkt_index;
43999a2dd95SBruce Richardson 			struct rte_mbuf *pkt = pkts[pkt_index];
44099a2dd95SBruce Richardson 
44199a2dd95SBruce Richardson 			p->tx_buf[tx_buf_count++] = pkt;
44299a2dd95SBruce Richardson 			RTE_PORT_FD_WRITER_NODROP_STATS_PKTS_IN_ADD(p, 1);
44399a2dd95SBruce Richardson 			pkts_mask &= ~pkt_mask;
44499a2dd95SBruce Richardson 		}
44599a2dd95SBruce Richardson 
44699a2dd95SBruce Richardson 	p->tx_buf_count = tx_buf_count;
44799a2dd95SBruce Richardson 	if (tx_buf_count >= p->tx_burst_sz)
44899a2dd95SBruce Richardson 		send_burst_nodrop(p);
44999a2dd95SBruce Richardson 
45099a2dd95SBruce Richardson 	return 0;
45199a2dd95SBruce Richardson }
45299a2dd95SBruce Richardson 
45399a2dd95SBruce Richardson static int
rte_port_fd_writer_nodrop_flush(void * port)45499a2dd95SBruce Richardson rte_port_fd_writer_nodrop_flush(void *port)
45599a2dd95SBruce Richardson {
45699a2dd95SBruce Richardson 	struct rte_port_fd_writer_nodrop *p =
45799a2dd95SBruce Richardson 		port;
45899a2dd95SBruce Richardson 
45999a2dd95SBruce Richardson 	if (p->tx_buf_count > 0)
46099a2dd95SBruce Richardson 		send_burst_nodrop(p);
46199a2dd95SBruce Richardson 
46299a2dd95SBruce Richardson 	return 0;
46399a2dd95SBruce Richardson }
46499a2dd95SBruce Richardson 
46599a2dd95SBruce Richardson static int
rte_port_fd_writer_nodrop_free(void * port)46699a2dd95SBruce Richardson rte_port_fd_writer_nodrop_free(void *port)
46799a2dd95SBruce Richardson {
46899a2dd95SBruce Richardson 	if (port == NULL) {
469*ae67895bSDavid Marchand 		PORT_LOG(ERR, "%s: Port is NULL", __func__);
47099a2dd95SBruce Richardson 		return -EINVAL;
47199a2dd95SBruce Richardson 	}
47299a2dd95SBruce Richardson 
47399a2dd95SBruce Richardson 	rte_port_fd_writer_nodrop_flush(port);
47499a2dd95SBruce Richardson 	rte_free(port);
47599a2dd95SBruce Richardson 
47699a2dd95SBruce Richardson return 0;
47799a2dd95SBruce Richardson }
47899a2dd95SBruce Richardson 
rte_port_fd_writer_nodrop_stats_read(void * port,struct rte_port_out_stats * stats,int clear)47999a2dd95SBruce Richardson static int rte_port_fd_writer_nodrop_stats_read(void *port,
48099a2dd95SBruce Richardson 		struct rte_port_out_stats *stats, int clear)
48199a2dd95SBruce Richardson {
48299a2dd95SBruce Richardson 	struct rte_port_fd_writer_nodrop *p =
48399a2dd95SBruce Richardson 		port;
48499a2dd95SBruce Richardson 
48599a2dd95SBruce Richardson 	if (stats != NULL)
48699a2dd95SBruce Richardson 		memcpy(stats, &p->stats, sizeof(p->stats));
48799a2dd95SBruce Richardson 
48899a2dd95SBruce Richardson 	if (clear)
48999a2dd95SBruce Richardson 		memset(&p->stats, 0, sizeof(p->stats));
49099a2dd95SBruce Richardson 
49199a2dd95SBruce Richardson 	return 0;
49299a2dd95SBruce Richardson }
49399a2dd95SBruce Richardson 
49499a2dd95SBruce Richardson /*
49599a2dd95SBruce Richardson  * Summary of port operations
49699a2dd95SBruce Richardson  */
49799a2dd95SBruce Richardson struct rte_port_in_ops rte_port_fd_reader_ops = {
49899a2dd95SBruce Richardson 	.f_create = rte_port_fd_reader_create,
49999a2dd95SBruce Richardson 	.f_free = rte_port_fd_reader_free,
50099a2dd95SBruce Richardson 	.f_rx = rte_port_fd_reader_rx,
50199a2dd95SBruce Richardson 	.f_stats = rte_port_fd_reader_stats_read,
50299a2dd95SBruce Richardson };
50399a2dd95SBruce Richardson 
50499a2dd95SBruce Richardson struct rte_port_out_ops rte_port_fd_writer_ops = {
50599a2dd95SBruce Richardson 	.f_create = rte_port_fd_writer_create,
50699a2dd95SBruce Richardson 	.f_free = rte_port_fd_writer_free,
50799a2dd95SBruce Richardson 	.f_tx = rte_port_fd_writer_tx,
50899a2dd95SBruce Richardson 	.f_tx_bulk = rte_port_fd_writer_tx_bulk,
50999a2dd95SBruce Richardson 	.f_flush = rte_port_fd_writer_flush,
51099a2dd95SBruce Richardson 	.f_stats = rte_port_fd_writer_stats_read,
51199a2dd95SBruce Richardson };
51299a2dd95SBruce Richardson 
51399a2dd95SBruce Richardson struct rte_port_out_ops rte_port_fd_writer_nodrop_ops = {
51499a2dd95SBruce Richardson 	.f_create = rte_port_fd_writer_nodrop_create,
51599a2dd95SBruce Richardson 	.f_free = rte_port_fd_writer_nodrop_free,
51699a2dd95SBruce Richardson 	.f_tx = rte_port_fd_writer_nodrop_tx,
51799a2dd95SBruce Richardson 	.f_tx_bulk = rte_port_fd_writer_nodrop_tx_bulk,
51899a2dd95SBruce Richardson 	.f_flush = rte_port_fd_writer_nodrop_flush,
51999a2dd95SBruce Richardson 	.f_stats = rte_port_fd_writer_nodrop_stats_read,
52099a2dd95SBruce Richardson };
521