1*99a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2*99a2dd95SBruce Richardson * Copyright(C) 2020 Marvell International Ltd.
3*99a2dd95SBruce Richardson */
4*99a2dd95SBruce Richardson
5*99a2dd95SBruce Richardson #include <rte_debug.h>
6*99a2dd95SBruce Richardson #include <rte_ethdev.h>
7*99a2dd95SBruce Richardson #include <rte_ether.h>
8*99a2dd95SBruce Richardson #include <rte_graph.h>
9*99a2dd95SBruce Richardson #include <rte_graph_worker.h>
10*99a2dd95SBruce Richardson
11*99a2dd95SBruce Richardson #include "ethdev_rx_priv.h"
12*99a2dd95SBruce Richardson #include "node_private.h"
13*99a2dd95SBruce Richardson
14*99a2dd95SBruce Richardson static struct ethdev_rx_node_main ethdev_rx_main;
15*99a2dd95SBruce Richardson
16*99a2dd95SBruce 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)17*99a2dd95SBruce Richardson ethdev_rx_node_process_inline(struct rte_graph *graph, struct rte_node *node,
18*99a2dd95SBruce Richardson ethdev_rx_node_ctx_t *ctx)
19*99a2dd95SBruce Richardson {
20*99a2dd95SBruce Richardson uint16_t count, next_index;
21*99a2dd95SBruce Richardson uint16_t port, queue;
22*99a2dd95SBruce Richardson
23*99a2dd95SBruce Richardson port = ctx->port_id;
24*99a2dd95SBruce Richardson queue = ctx->queue_id;
25*99a2dd95SBruce Richardson next_index = ctx->cls_next;
26*99a2dd95SBruce Richardson
27*99a2dd95SBruce Richardson /* Get pkts from port */
28*99a2dd95SBruce Richardson count = rte_eth_rx_burst(port, queue, (struct rte_mbuf **)node->objs,
29*99a2dd95SBruce Richardson RTE_GRAPH_BURST_SIZE);
30*99a2dd95SBruce Richardson
31*99a2dd95SBruce Richardson if (!count)
32*99a2dd95SBruce Richardson return 0;
33*99a2dd95SBruce Richardson node->idx = count;
34*99a2dd95SBruce Richardson /* Enqueue to next node */
35*99a2dd95SBruce Richardson rte_node_next_stream_move(graph, node, next_index);
36*99a2dd95SBruce Richardson
37*99a2dd95SBruce Richardson return count;
38*99a2dd95SBruce Richardson }
39*99a2dd95SBruce Richardson
40*99a2dd95SBruce Richardson static __rte_always_inline uint16_t
ethdev_rx_node_process(struct rte_graph * graph,struct rte_node * node,void ** objs,uint16_t cnt)41*99a2dd95SBruce Richardson ethdev_rx_node_process(struct rte_graph *graph, struct rte_node *node,
42*99a2dd95SBruce Richardson void **objs, uint16_t cnt)
43*99a2dd95SBruce Richardson {
44*99a2dd95SBruce Richardson ethdev_rx_node_ctx_t *ctx = (ethdev_rx_node_ctx_t *)node->ctx;
45*99a2dd95SBruce Richardson uint16_t n_pkts = 0;
46*99a2dd95SBruce Richardson
47*99a2dd95SBruce Richardson RTE_SET_USED(objs);
48*99a2dd95SBruce Richardson RTE_SET_USED(cnt);
49*99a2dd95SBruce Richardson
50*99a2dd95SBruce Richardson n_pkts = ethdev_rx_node_process_inline(graph, node, ctx);
51*99a2dd95SBruce Richardson return n_pkts;
52*99a2dd95SBruce Richardson }
53*99a2dd95SBruce Richardson
54*99a2dd95SBruce Richardson static inline uint32_t
l3_ptype(uint16_t etype,uint32_t ptype)55*99a2dd95SBruce Richardson l3_ptype(uint16_t etype, uint32_t ptype)
56*99a2dd95SBruce Richardson {
57*99a2dd95SBruce Richardson ptype = ptype & ~RTE_PTYPE_L3_MASK;
58*99a2dd95SBruce Richardson if (etype == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
59*99a2dd95SBruce Richardson ptype |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
60*99a2dd95SBruce Richardson else if (etype == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
61*99a2dd95SBruce Richardson ptype |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
62*99a2dd95SBruce Richardson return ptype;
63*99a2dd95SBruce Richardson }
64*99a2dd95SBruce Richardson
65*99a2dd95SBruce Richardson /* Callback for soft ptype parsing */
66*99a2dd95SBruce 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)67*99a2dd95SBruce Richardson eth_pkt_parse_cb(uint16_t port, uint16_t queue, struct rte_mbuf **mbufs,
68*99a2dd95SBruce Richardson uint16_t nb_pkts, uint16_t max_pkts, void *user_param)
69*99a2dd95SBruce Richardson {
70*99a2dd95SBruce Richardson struct rte_mbuf *mbuf0, *mbuf1, *mbuf2, *mbuf3;
71*99a2dd95SBruce Richardson struct rte_ether_hdr *eth_hdr;
72*99a2dd95SBruce Richardson uint16_t etype, n_left;
73*99a2dd95SBruce Richardson struct rte_mbuf **pkts;
74*99a2dd95SBruce Richardson
75*99a2dd95SBruce Richardson RTE_SET_USED(port);
76*99a2dd95SBruce Richardson RTE_SET_USED(queue);
77*99a2dd95SBruce Richardson RTE_SET_USED(max_pkts);
78*99a2dd95SBruce Richardson RTE_SET_USED(user_param);
79*99a2dd95SBruce Richardson
80*99a2dd95SBruce Richardson pkts = mbufs;
81*99a2dd95SBruce Richardson n_left = nb_pkts;
82*99a2dd95SBruce Richardson while (n_left >= 12) {
83*99a2dd95SBruce Richardson
84*99a2dd95SBruce Richardson /* Prefetch next-next mbufs */
85*99a2dd95SBruce Richardson rte_prefetch0(pkts[8]);
86*99a2dd95SBruce Richardson rte_prefetch0(pkts[9]);
87*99a2dd95SBruce Richardson rte_prefetch0(pkts[10]);
88*99a2dd95SBruce Richardson rte_prefetch0(pkts[11]);
89*99a2dd95SBruce Richardson
90*99a2dd95SBruce Richardson /* Prefetch next mbuf data */
91*99a2dd95SBruce Richardson rte_prefetch0(
92*99a2dd95SBruce Richardson rte_pktmbuf_mtod(pkts[4], struct rte_ether_hdr *));
93*99a2dd95SBruce Richardson rte_prefetch0(
94*99a2dd95SBruce Richardson rte_pktmbuf_mtod(pkts[5], struct rte_ether_hdr *));
95*99a2dd95SBruce Richardson rte_prefetch0(
96*99a2dd95SBruce Richardson rte_pktmbuf_mtod(pkts[6], struct rte_ether_hdr *));
97*99a2dd95SBruce Richardson rte_prefetch0(
98*99a2dd95SBruce Richardson rte_pktmbuf_mtod(pkts[7], struct rte_ether_hdr *));
99*99a2dd95SBruce Richardson
100*99a2dd95SBruce Richardson mbuf0 = pkts[0];
101*99a2dd95SBruce Richardson mbuf1 = pkts[1];
102*99a2dd95SBruce Richardson mbuf2 = pkts[2];
103*99a2dd95SBruce Richardson mbuf3 = pkts[3];
104*99a2dd95SBruce Richardson pkts += 4;
105*99a2dd95SBruce Richardson n_left -= 4;
106*99a2dd95SBruce Richardson
107*99a2dd95SBruce Richardson /* Extract ptype of mbuf0 */
108*99a2dd95SBruce Richardson eth_hdr = rte_pktmbuf_mtod(mbuf0, struct rte_ether_hdr *);
109*99a2dd95SBruce Richardson etype = eth_hdr->ether_type;
110*99a2dd95SBruce Richardson mbuf0->packet_type = l3_ptype(etype, 0);
111*99a2dd95SBruce Richardson
112*99a2dd95SBruce Richardson /* Extract ptype of mbuf1 */
113*99a2dd95SBruce Richardson eth_hdr = rte_pktmbuf_mtod(mbuf1, struct rte_ether_hdr *);
114*99a2dd95SBruce Richardson etype = eth_hdr->ether_type;
115*99a2dd95SBruce Richardson mbuf1->packet_type = l3_ptype(etype, 0);
116*99a2dd95SBruce Richardson
117*99a2dd95SBruce Richardson /* Extract ptype of mbuf2 */
118*99a2dd95SBruce Richardson eth_hdr = rte_pktmbuf_mtod(mbuf2, struct rte_ether_hdr *);
119*99a2dd95SBruce Richardson etype = eth_hdr->ether_type;
120*99a2dd95SBruce Richardson mbuf2->packet_type = l3_ptype(etype, 0);
121*99a2dd95SBruce Richardson
122*99a2dd95SBruce Richardson /* Extract ptype of mbuf3 */
123*99a2dd95SBruce Richardson eth_hdr = rte_pktmbuf_mtod(mbuf3, struct rte_ether_hdr *);
124*99a2dd95SBruce Richardson etype = eth_hdr->ether_type;
125*99a2dd95SBruce Richardson mbuf3->packet_type = l3_ptype(etype, 0);
126*99a2dd95SBruce Richardson }
127*99a2dd95SBruce Richardson
128*99a2dd95SBruce Richardson while (n_left > 0) {
129*99a2dd95SBruce Richardson mbuf0 = pkts[0];
130*99a2dd95SBruce Richardson
131*99a2dd95SBruce Richardson pkts += 1;
132*99a2dd95SBruce Richardson n_left -= 1;
133*99a2dd95SBruce Richardson
134*99a2dd95SBruce Richardson /* Extract ptype of mbuf0 */
135*99a2dd95SBruce Richardson eth_hdr = rte_pktmbuf_mtod(mbuf0, struct rte_ether_hdr *);
136*99a2dd95SBruce Richardson etype = eth_hdr->ether_type;
137*99a2dd95SBruce Richardson mbuf0->packet_type = l3_ptype(etype, 0);
138*99a2dd95SBruce Richardson }
139*99a2dd95SBruce Richardson
140*99a2dd95SBruce Richardson return nb_pkts;
141*99a2dd95SBruce Richardson }
142*99a2dd95SBruce Richardson
143*99a2dd95SBruce Richardson #define MAX_PTYPES 16
144*99a2dd95SBruce Richardson static int
ethdev_ptype_setup(uint16_t port,uint16_t queue)145*99a2dd95SBruce Richardson ethdev_ptype_setup(uint16_t port, uint16_t queue)
146*99a2dd95SBruce Richardson {
147*99a2dd95SBruce Richardson uint8_t l3_ipv4 = 0, l3_ipv6 = 0;
148*99a2dd95SBruce Richardson uint32_t ptypes[MAX_PTYPES];
149*99a2dd95SBruce Richardson int i, rc;
150*99a2dd95SBruce Richardson
151*99a2dd95SBruce Richardson /* Check IPv4 & IPv6 ptype support */
152*99a2dd95SBruce Richardson rc = rte_eth_dev_get_supported_ptypes(port, RTE_PTYPE_L3_MASK, ptypes,
153*99a2dd95SBruce Richardson MAX_PTYPES);
154*99a2dd95SBruce Richardson for (i = 0; i < rc; i++) {
155*99a2dd95SBruce Richardson if (ptypes[i] & RTE_PTYPE_L3_IPV4)
156*99a2dd95SBruce Richardson l3_ipv4 = 1;
157*99a2dd95SBruce Richardson if (ptypes[i] & RTE_PTYPE_L3_IPV6)
158*99a2dd95SBruce Richardson l3_ipv6 = 1;
159*99a2dd95SBruce Richardson }
160*99a2dd95SBruce Richardson
161*99a2dd95SBruce Richardson if (!l3_ipv4 || !l3_ipv6) {
162*99a2dd95SBruce Richardson node_info("ethdev_rx",
163*99a2dd95SBruce Richardson "Enabling ptype callback for required ptypes on port %u\n",
164*99a2dd95SBruce Richardson port);
165*99a2dd95SBruce Richardson
166*99a2dd95SBruce Richardson if (!rte_eth_add_rx_callback(port, queue, eth_pkt_parse_cb,
167*99a2dd95SBruce Richardson NULL)) {
168*99a2dd95SBruce Richardson node_err("ethdev_rx",
169*99a2dd95SBruce Richardson "Failed to add rx ptype cb: port=%d, queue=%d\n",
170*99a2dd95SBruce Richardson port, queue);
171*99a2dd95SBruce Richardson return -EINVAL;
172*99a2dd95SBruce Richardson }
173*99a2dd95SBruce Richardson }
174*99a2dd95SBruce Richardson
175*99a2dd95SBruce Richardson return 0;
176*99a2dd95SBruce Richardson }
177*99a2dd95SBruce Richardson
178*99a2dd95SBruce Richardson static int
ethdev_rx_node_init(const struct rte_graph * graph,struct rte_node * node)179*99a2dd95SBruce Richardson ethdev_rx_node_init(const struct rte_graph *graph, struct rte_node *node)
180*99a2dd95SBruce Richardson {
181*99a2dd95SBruce Richardson ethdev_rx_node_ctx_t *ctx = (ethdev_rx_node_ctx_t *)node->ctx;
182*99a2dd95SBruce Richardson ethdev_rx_node_elem_t *elem = ethdev_rx_main.head;
183*99a2dd95SBruce Richardson
184*99a2dd95SBruce Richardson RTE_SET_USED(graph);
185*99a2dd95SBruce Richardson
186*99a2dd95SBruce Richardson while (elem) {
187*99a2dd95SBruce Richardson if (elem->nid == node->id) {
188*99a2dd95SBruce Richardson /* Update node specific context */
189*99a2dd95SBruce Richardson memcpy(ctx, &elem->ctx, sizeof(ethdev_rx_node_ctx_t));
190*99a2dd95SBruce Richardson break;
191*99a2dd95SBruce Richardson }
192*99a2dd95SBruce Richardson elem = elem->next;
193*99a2dd95SBruce Richardson }
194*99a2dd95SBruce Richardson
195*99a2dd95SBruce Richardson RTE_VERIFY(elem != NULL);
196*99a2dd95SBruce Richardson
197*99a2dd95SBruce Richardson ctx->cls_next = ETHDEV_RX_NEXT_PKT_CLS;
198*99a2dd95SBruce Richardson
199*99a2dd95SBruce Richardson /* Check and setup ptype */
200*99a2dd95SBruce Richardson return ethdev_ptype_setup(ctx->port_id, ctx->queue_id);
201*99a2dd95SBruce Richardson }
202*99a2dd95SBruce Richardson
203*99a2dd95SBruce Richardson struct ethdev_rx_node_main *
ethdev_rx_get_node_data_get(void)204*99a2dd95SBruce Richardson ethdev_rx_get_node_data_get(void)
205*99a2dd95SBruce Richardson {
206*99a2dd95SBruce Richardson return ðdev_rx_main;
207*99a2dd95SBruce Richardson }
208*99a2dd95SBruce Richardson
209*99a2dd95SBruce Richardson static struct rte_node_register ethdev_rx_node_base = {
210*99a2dd95SBruce Richardson .process = ethdev_rx_node_process,
211*99a2dd95SBruce Richardson .flags = RTE_NODE_SOURCE_F,
212*99a2dd95SBruce Richardson .name = "ethdev_rx",
213*99a2dd95SBruce Richardson
214*99a2dd95SBruce Richardson .init = ethdev_rx_node_init,
215*99a2dd95SBruce Richardson
216*99a2dd95SBruce Richardson .nb_edges = ETHDEV_RX_NEXT_MAX,
217*99a2dd95SBruce Richardson .next_nodes = {
218*99a2dd95SBruce Richardson /* Default pkt classification node */
219*99a2dd95SBruce Richardson [ETHDEV_RX_NEXT_PKT_CLS] = "pkt_cls",
220*99a2dd95SBruce Richardson [ETHDEV_RX_NEXT_IP4_LOOKUP] = "ip4_lookup",
221*99a2dd95SBruce Richardson },
222*99a2dd95SBruce Richardson };
223*99a2dd95SBruce Richardson
224*99a2dd95SBruce Richardson struct rte_node_register *
ethdev_rx_node_get(void)225*99a2dd95SBruce Richardson ethdev_rx_node_get(void)
226*99a2dd95SBruce Richardson {
227*99a2dd95SBruce Richardson return ðdev_rx_node_base;
228*99a2dd95SBruce Richardson }
229*99a2dd95SBruce Richardson
230*99a2dd95SBruce Richardson RTE_NODE_REGISTER(ethdev_rx_node_base);
231