xref: /dpdk/lib/node/ethdev_rx.c (revision ae282b0611c33aa73a01ee6137d116155053b835)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(C) 2020 Marvell International Ltd.
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #include <rte_debug.h>
699a2dd95SBruce Richardson #include <rte_ethdev.h>
799a2dd95SBruce Richardson #include <rte_ether.h>
899a2dd95SBruce Richardson #include <rte_graph.h>
9a2bc0584SZhirun Yan #include <rte_graph_worker.h>
1099a2dd95SBruce Richardson 
1199a2dd95SBruce Richardson #include "ethdev_rx_priv.h"
1299a2dd95SBruce Richardson #include "node_private.h"
1399a2dd95SBruce Richardson 
1499a2dd95SBruce Richardson static struct ethdev_rx_node_main ethdev_rx_main;
1599a2dd95SBruce Richardson 
1699a2dd95SBruce Richardson static __rte_always_inline uint16_t
ethdev_rx_node_process_inline(struct rte_graph * graph,struct rte_node * node,ethdev_rx_node_ctx_t * ctx)1799a2dd95SBruce Richardson ethdev_rx_node_process_inline(struct rte_graph *graph, struct rte_node *node,
1899a2dd95SBruce Richardson 			      ethdev_rx_node_ctx_t *ctx)
1999a2dd95SBruce Richardson {
2099a2dd95SBruce Richardson 	uint16_t count, next_index;
2199a2dd95SBruce Richardson 	uint16_t port, queue;
2299a2dd95SBruce Richardson 
2399a2dd95SBruce Richardson 	port = ctx->port_id;
2499a2dd95SBruce Richardson 	queue = ctx->queue_id;
2599a2dd95SBruce Richardson 	next_index = ctx->cls_next;
2699a2dd95SBruce Richardson 
2799a2dd95SBruce Richardson 	/* Get pkts from port */
2899a2dd95SBruce Richardson 	count = rte_eth_rx_burst(port, queue, (struct rte_mbuf **)node->objs,
2999a2dd95SBruce Richardson 				 RTE_GRAPH_BURST_SIZE);
3099a2dd95SBruce Richardson 
3199a2dd95SBruce Richardson 	if (!count)
3299a2dd95SBruce Richardson 		return 0;
3399a2dd95SBruce Richardson 	node->idx = count;
3499a2dd95SBruce Richardson 	/* Enqueue to next node */
3599a2dd95SBruce Richardson 	rte_node_next_stream_move(graph, node, next_index);
3699a2dd95SBruce Richardson 
3799a2dd95SBruce Richardson 	return count;
3899a2dd95SBruce Richardson }
3999a2dd95SBruce Richardson 
4099a2dd95SBruce Richardson static __rte_always_inline uint16_t
ethdev_rx_node_process(struct rte_graph * graph,struct rte_node * node,void ** objs,uint16_t cnt)4199a2dd95SBruce Richardson ethdev_rx_node_process(struct rte_graph *graph, struct rte_node *node,
4299a2dd95SBruce Richardson 		       void **objs, uint16_t cnt)
4399a2dd95SBruce Richardson {
4499a2dd95SBruce Richardson 	ethdev_rx_node_ctx_t *ctx = (ethdev_rx_node_ctx_t *)node->ctx;
4599a2dd95SBruce Richardson 	uint16_t n_pkts = 0;
4699a2dd95SBruce Richardson 
4799a2dd95SBruce Richardson 	RTE_SET_USED(objs);
4899a2dd95SBruce Richardson 	RTE_SET_USED(cnt);
4999a2dd95SBruce Richardson 
5099a2dd95SBruce Richardson 	n_pkts = ethdev_rx_node_process_inline(graph, node, ctx);
5199a2dd95SBruce Richardson 	return n_pkts;
5299a2dd95SBruce Richardson }
5399a2dd95SBruce Richardson 
5499a2dd95SBruce Richardson static inline uint32_t
l3_ptype(uint16_t etype,uint32_t ptype)5599a2dd95SBruce Richardson l3_ptype(uint16_t etype, uint32_t ptype)
5699a2dd95SBruce Richardson {
5799a2dd95SBruce Richardson 	ptype = ptype & ~RTE_PTYPE_L3_MASK;
5899a2dd95SBruce Richardson 	if (etype == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
5999a2dd95SBruce Richardson 		ptype |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
6099a2dd95SBruce Richardson 	else if (etype == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
6199a2dd95SBruce Richardson 		ptype |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
6299a2dd95SBruce Richardson 	return ptype;
6399a2dd95SBruce Richardson }
6499a2dd95SBruce Richardson 
6599a2dd95SBruce Richardson /* Callback for soft ptype parsing */
6699a2dd95SBruce Richardson static uint16_t
eth_pkt_parse_cb(uint16_t port,uint16_t queue,struct rte_mbuf ** mbufs,uint16_t nb_pkts,uint16_t max_pkts,void * user_param)6799a2dd95SBruce Richardson eth_pkt_parse_cb(uint16_t port, uint16_t queue, struct rte_mbuf **mbufs,
6899a2dd95SBruce Richardson 		 uint16_t nb_pkts, uint16_t max_pkts, void *user_param)
6999a2dd95SBruce Richardson {
7099a2dd95SBruce Richardson 	struct rte_mbuf *mbuf0, *mbuf1, *mbuf2, *mbuf3;
7199a2dd95SBruce Richardson 	struct rte_ether_hdr *eth_hdr;
7299a2dd95SBruce Richardson 	uint16_t etype, n_left;
7399a2dd95SBruce Richardson 	struct rte_mbuf **pkts;
7499a2dd95SBruce Richardson 
7599a2dd95SBruce Richardson 	RTE_SET_USED(port);
7699a2dd95SBruce Richardson 	RTE_SET_USED(queue);
7799a2dd95SBruce Richardson 	RTE_SET_USED(max_pkts);
7899a2dd95SBruce Richardson 	RTE_SET_USED(user_param);
7999a2dd95SBruce Richardson 
8099a2dd95SBruce Richardson 	pkts = mbufs;
8199a2dd95SBruce Richardson 	n_left = nb_pkts;
8299a2dd95SBruce Richardson 	while (n_left >= 12) {
8399a2dd95SBruce Richardson 
8499a2dd95SBruce Richardson 		/* Prefetch next-next mbufs */
8599a2dd95SBruce Richardson 		rte_prefetch0(pkts[8]);
8699a2dd95SBruce Richardson 		rte_prefetch0(pkts[9]);
8799a2dd95SBruce Richardson 		rte_prefetch0(pkts[10]);
8899a2dd95SBruce Richardson 		rte_prefetch0(pkts[11]);
8999a2dd95SBruce Richardson 
9099a2dd95SBruce Richardson 		/* Prefetch next mbuf data */
9199a2dd95SBruce Richardson 		rte_prefetch0(
9299a2dd95SBruce Richardson 			rte_pktmbuf_mtod(pkts[4], struct rte_ether_hdr *));
9399a2dd95SBruce Richardson 		rte_prefetch0(
9499a2dd95SBruce Richardson 			rte_pktmbuf_mtod(pkts[5], struct rte_ether_hdr *));
9599a2dd95SBruce Richardson 		rte_prefetch0(
9699a2dd95SBruce Richardson 			rte_pktmbuf_mtod(pkts[6], struct rte_ether_hdr *));
9799a2dd95SBruce Richardson 		rte_prefetch0(
9899a2dd95SBruce Richardson 			rte_pktmbuf_mtod(pkts[7], struct rte_ether_hdr *));
9999a2dd95SBruce Richardson 
10099a2dd95SBruce Richardson 		mbuf0 = pkts[0];
10199a2dd95SBruce Richardson 		mbuf1 = pkts[1];
10299a2dd95SBruce Richardson 		mbuf2 = pkts[2];
10399a2dd95SBruce Richardson 		mbuf3 = pkts[3];
10499a2dd95SBruce Richardson 		pkts += 4;
10599a2dd95SBruce Richardson 		n_left -= 4;
10699a2dd95SBruce Richardson 
10799a2dd95SBruce Richardson 		/* Extract ptype of mbuf0 */
10899a2dd95SBruce Richardson 		eth_hdr = rte_pktmbuf_mtod(mbuf0, struct rte_ether_hdr *);
10999a2dd95SBruce Richardson 		etype = eth_hdr->ether_type;
11099a2dd95SBruce Richardson 		mbuf0->packet_type = l3_ptype(etype, 0);
11199a2dd95SBruce Richardson 
11299a2dd95SBruce Richardson 		/* Extract ptype of mbuf1 */
11399a2dd95SBruce Richardson 		eth_hdr = rte_pktmbuf_mtod(mbuf1, struct rte_ether_hdr *);
11499a2dd95SBruce Richardson 		etype = eth_hdr->ether_type;
11599a2dd95SBruce Richardson 		mbuf1->packet_type = l3_ptype(etype, 0);
11699a2dd95SBruce Richardson 
11799a2dd95SBruce Richardson 		/* Extract ptype of mbuf2 */
11899a2dd95SBruce Richardson 		eth_hdr = rte_pktmbuf_mtod(mbuf2, struct rte_ether_hdr *);
11999a2dd95SBruce Richardson 		etype = eth_hdr->ether_type;
12099a2dd95SBruce Richardson 		mbuf2->packet_type = l3_ptype(etype, 0);
12199a2dd95SBruce Richardson 
12299a2dd95SBruce Richardson 		/* Extract ptype of mbuf3 */
12399a2dd95SBruce Richardson 		eth_hdr = rte_pktmbuf_mtod(mbuf3, struct rte_ether_hdr *);
12499a2dd95SBruce Richardson 		etype = eth_hdr->ether_type;
12599a2dd95SBruce Richardson 		mbuf3->packet_type = l3_ptype(etype, 0);
12699a2dd95SBruce Richardson 	}
12799a2dd95SBruce Richardson 
12899a2dd95SBruce Richardson 	while (n_left > 0) {
12999a2dd95SBruce Richardson 		mbuf0 = pkts[0];
13099a2dd95SBruce Richardson 
13199a2dd95SBruce Richardson 		pkts += 1;
13299a2dd95SBruce Richardson 		n_left -= 1;
13399a2dd95SBruce Richardson 
13499a2dd95SBruce Richardson 		/* Extract ptype of mbuf0 */
13599a2dd95SBruce Richardson 		eth_hdr = rte_pktmbuf_mtod(mbuf0, struct rte_ether_hdr *);
13699a2dd95SBruce Richardson 		etype = eth_hdr->ether_type;
13799a2dd95SBruce Richardson 		mbuf0->packet_type = l3_ptype(etype, 0);
13899a2dd95SBruce Richardson 	}
13999a2dd95SBruce Richardson 
14099a2dd95SBruce Richardson 	return nb_pkts;
14199a2dd95SBruce Richardson }
14299a2dd95SBruce Richardson 
14399a2dd95SBruce Richardson #define MAX_PTYPES 16
14499a2dd95SBruce Richardson static int
ethdev_ptype_setup(uint16_t port,uint16_t queue)14599a2dd95SBruce Richardson ethdev_ptype_setup(uint16_t port, uint16_t queue)
14699a2dd95SBruce Richardson {
14799a2dd95SBruce Richardson 	uint8_t l3_ipv4 = 0, l3_ipv6 = 0;
14899a2dd95SBruce Richardson 	uint32_t ptypes[MAX_PTYPES];
14999a2dd95SBruce Richardson 	int i, rc;
15099a2dd95SBruce Richardson 
15199a2dd95SBruce Richardson 	/* Check IPv4 & IPv6 ptype support */
15299a2dd95SBruce Richardson 	rc = rte_eth_dev_get_supported_ptypes(port, RTE_PTYPE_L3_MASK, ptypes,
15399a2dd95SBruce Richardson 					      MAX_PTYPES);
15499a2dd95SBruce Richardson 	for (i = 0; i < rc; i++) {
15599a2dd95SBruce Richardson 		if (ptypes[i] & RTE_PTYPE_L3_IPV4)
15699a2dd95SBruce Richardson 			l3_ipv4 = 1;
15799a2dd95SBruce Richardson 		if (ptypes[i] & RTE_PTYPE_L3_IPV6)
15899a2dd95SBruce Richardson 			l3_ipv6 = 1;
15999a2dd95SBruce Richardson 	}
16099a2dd95SBruce Richardson 
16199a2dd95SBruce Richardson 	if (!l3_ipv4 || !l3_ipv6) {
16299a2dd95SBruce Richardson 		node_info("ethdev_rx",
163*ae282b06SDavid Marchand 			  "Enabling ptype callback for required ptypes on port %u",
16499a2dd95SBruce Richardson 			  port);
16599a2dd95SBruce Richardson 
16699a2dd95SBruce Richardson 		if (!rte_eth_add_rx_callback(port, queue, eth_pkt_parse_cb,
16799a2dd95SBruce Richardson 					     NULL)) {
16899a2dd95SBruce Richardson 			node_err("ethdev_rx",
169*ae282b06SDavid Marchand 				 "Failed to add rx ptype cb: port=%d, queue=%d",
17099a2dd95SBruce Richardson 				 port, queue);
17199a2dd95SBruce Richardson 			return -EINVAL;
17299a2dd95SBruce Richardson 		}
17399a2dd95SBruce Richardson 	}
17499a2dd95SBruce Richardson 
17599a2dd95SBruce Richardson 	return 0;
17699a2dd95SBruce Richardson }
17799a2dd95SBruce Richardson 
17899a2dd95SBruce Richardson static int
ethdev_rx_node_init(const struct rte_graph * graph,struct rte_node * node)17999a2dd95SBruce Richardson ethdev_rx_node_init(const struct rte_graph *graph, struct rte_node *node)
18099a2dd95SBruce Richardson {
18199a2dd95SBruce Richardson 	ethdev_rx_node_ctx_t *ctx = (ethdev_rx_node_ctx_t *)node->ctx;
18299a2dd95SBruce Richardson 	ethdev_rx_node_elem_t *elem = ethdev_rx_main.head;
18399a2dd95SBruce Richardson 
18499a2dd95SBruce Richardson 	RTE_SET_USED(graph);
18599a2dd95SBruce Richardson 
18699a2dd95SBruce Richardson 	while (elem) {
18799a2dd95SBruce Richardson 		if (elem->nid == node->id) {
18899a2dd95SBruce Richardson 			/* Update node specific context */
18999a2dd95SBruce Richardson 			memcpy(ctx, &elem->ctx, sizeof(ethdev_rx_node_ctx_t));
19099a2dd95SBruce Richardson 			break;
19199a2dd95SBruce Richardson 		}
19299a2dd95SBruce Richardson 		elem = elem->next;
19399a2dd95SBruce Richardson 	}
19499a2dd95SBruce Richardson 
19599a2dd95SBruce Richardson 	RTE_VERIFY(elem != NULL);
19699a2dd95SBruce Richardson 
19799a2dd95SBruce Richardson 	/* Check and setup ptype */
19899a2dd95SBruce Richardson 	return ethdev_ptype_setup(ctx->port_id, ctx->queue_id);
19999a2dd95SBruce Richardson }
20099a2dd95SBruce Richardson 
20199a2dd95SBruce Richardson struct ethdev_rx_node_main *
ethdev_rx_get_node_data_get(void)20299a2dd95SBruce Richardson ethdev_rx_get_node_data_get(void)
20399a2dd95SBruce Richardson {
20499a2dd95SBruce Richardson 	return &ethdev_rx_main;
20599a2dd95SBruce Richardson }
20699a2dd95SBruce Richardson 
20799a2dd95SBruce Richardson static struct rte_node_register ethdev_rx_node_base = {
20899a2dd95SBruce Richardson 	.process = ethdev_rx_node_process,
20999a2dd95SBruce Richardson 	.flags = RTE_NODE_SOURCE_F,
21099a2dd95SBruce Richardson 	.name = "ethdev_rx",
21199a2dd95SBruce Richardson 
21299a2dd95SBruce Richardson 	.init = ethdev_rx_node_init,
21399a2dd95SBruce Richardson 
21499a2dd95SBruce Richardson 	.nb_edges = ETHDEV_RX_NEXT_MAX,
21599a2dd95SBruce Richardson 	.next_nodes = {
21699a2dd95SBruce Richardson 		[ETHDEV_RX_NEXT_PKT_CLS] = "pkt_cls",
21799a2dd95SBruce Richardson 		[ETHDEV_RX_NEXT_IP4_LOOKUP] = "ip4_lookup",
2180124e18fSPavan Nikhilesh 		[ETHDEV_RX_NEXT_IP4_REASSEMBLY] = "ip4_reassembly",
21999a2dd95SBruce Richardson 	},
22099a2dd95SBruce Richardson };
22199a2dd95SBruce Richardson 
22299a2dd95SBruce Richardson struct rte_node_register *
ethdev_rx_node_get(void)22399a2dd95SBruce Richardson ethdev_rx_node_get(void)
22499a2dd95SBruce Richardson {
22599a2dd95SBruce Richardson 	return &ethdev_rx_node_base;
22699a2dd95SBruce Richardson }
22799a2dd95SBruce Richardson 
22899a2dd95SBruce Richardson RTE_NODE_REGISTER(ethdev_rx_node_base);
229