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 <arpa/inet.h>
6*99a2dd95SBruce Richardson #include <sys/socket.h>
7*99a2dd95SBruce Richardson
8*99a2dd95SBruce Richardson #include <rte_ethdev.h>
9*99a2dd95SBruce Richardson #include <rte_ether.h>
10*99a2dd95SBruce Richardson #include <rte_graph.h>
11*99a2dd95SBruce Richardson #include <rte_graph_worker.h>
12*99a2dd95SBruce Richardson #include <rte_ip.h>
13*99a2dd95SBruce Richardson #include <rte_lpm.h>
14*99a2dd95SBruce Richardson
15*99a2dd95SBruce Richardson #include "rte_node_ip4_api.h"
16*99a2dd95SBruce Richardson
17*99a2dd95SBruce Richardson #include "node_private.h"
18*99a2dd95SBruce Richardson
19*99a2dd95SBruce Richardson #define IPV4_L3FWD_LPM_MAX_RULES 1024
20*99a2dd95SBruce Richardson #define IPV4_L3FWD_LPM_NUMBER_TBL8S (1 << 8)
21*99a2dd95SBruce Richardson
22*99a2dd95SBruce Richardson /* IP4 Lookup global data struct */
23*99a2dd95SBruce Richardson struct ip4_lookup_node_main {
24*99a2dd95SBruce Richardson struct rte_lpm *lpm_tbl[RTE_MAX_NUMA_NODES];
25*99a2dd95SBruce Richardson };
26*99a2dd95SBruce Richardson
27*99a2dd95SBruce Richardson struct ip4_lookup_node_ctx {
28*99a2dd95SBruce Richardson /* Socket's LPM table */
29*99a2dd95SBruce Richardson struct rte_lpm *lpm;
30*99a2dd95SBruce Richardson /* Dynamic offset to mbuf priv1 */
31*99a2dd95SBruce Richardson int mbuf_priv1_off;
32*99a2dd95SBruce Richardson };
33*99a2dd95SBruce Richardson
34*99a2dd95SBruce Richardson int node_mbuf_priv1_dynfield_offset = -1;
35*99a2dd95SBruce Richardson
36*99a2dd95SBruce Richardson static struct ip4_lookup_node_main ip4_lookup_nm;
37*99a2dd95SBruce Richardson
38*99a2dd95SBruce Richardson #define IP4_LOOKUP_NODE_LPM(ctx) \
39*99a2dd95SBruce Richardson (((struct ip4_lookup_node_ctx *)ctx)->lpm)
40*99a2dd95SBruce Richardson
41*99a2dd95SBruce Richardson #define IP4_LOOKUP_NODE_PRIV1_OFF(ctx) \
42*99a2dd95SBruce Richardson (((struct ip4_lookup_node_ctx *)ctx)->mbuf_priv1_off)
43*99a2dd95SBruce Richardson
44*99a2dd95SBruce Richardson #if defined(__ARM_NEON)
45*99a2dd95SBruce Richardson #include "ip4_lookup_neon.h"
46*99a2dd95SBruce Richardson #elif defined(RTE_ARCH_X86)
47*99a2dd95SBruce Richardson #include "ip4_lookup_sse.h"
48*99a2dd95SBruce Richardson #endif
49*99a2dd95SBruce Richardson
50*99a2dd95SBruce Richardson static uint16_t
ip4_lookup_node_process_scalar(struct rte_graph * graph,struct rte_node * node,void ** objs,uint16_t nb_objs)51*99a2dd95SBruce Richardson ip4_lookup_node_process_scalar(struct rte_graph *graph, struct rte_node *node,
52*99a2dd95SBruce Richardson void **objs, uint16_t nb_objs)
53*99a2dd95SBruce Richardson {
54*99a2dd95SBruce Richardson struct rte_lpm *lpm = IP4_LOOKUP_NODE_LPM(node->ctx);
55*99a2dd95SBruce Richardson const int dyn = IP4_LOOKUP_NODE_PRIV1_OFF(node->ctx);
56*99a2dd95SBruce Richardson struct rte_ipv4_hdr *ipv4_hdr;
57*99a2dd95SBruce Richardson void **to_next, **from;
58*99a2dd95SBruce Richardson uint16_t last_spec = 0;
59*99a2dd95SBruce Richardson struct rte_mbuf *mbuf;
60*99a2dd95SBruce Richardson rte_edge_t next_index;
61*99a2dd95SBruce Richardson uint16_t held = 0;
62*99a2dd95SBruce Richardson uint32_t drop_nh;
63*99a2dd95SBruce Richardson int i, rc;
64*99a2dd95SBruce Richardson
65*99a2dd95SBruce Richardson /* Speculative next */
66*99a2dd95SBruce Richardson next_index = RTE_NODE_IP4_LOOKUP_NEXT_REWRITE;
67*99a2dd95SBruce Richardson /* Drop node */
68*99a2dd95SBruce Richardson drop_nh = ((uint32_t)RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP) << 16;
69*99a2dd95SBruce Richardson from = objs;
70*99a2dd95SBruce Richardson
71*99a2dd95SBruce Richardson /* Get stream for the speculated next node */
72*99a2dd95SBruce Richardson to_next = rte_node_next_stream_get(graph, node, next_index, nb_objs);
73*99a2dd95SBruce Richardson for (i = 0; i < nb_objs; i++) {
74*99a2dd95SBruce Richardson uint32_t next_hop;
75*99a2dd95SBruce Richardson uint16_t next;
76*99a2dd95SBruce Richardson
77*99a2dd95SBruce Richardson mbuf = (struct rte_mbuf *)objs[i];
78*99a2dd95SBruce Richardson
79*99a2dd95SBruce Richardson /* Extract DIP of mbuf0 */
80*99a2dd95SBruce Richardson ipv4_hdr = rte_pktmbuf_mtod_offset(mbuf, struct rte_ipv4_hdr *,
81*99a2dd95SBruce Richardson sizeof(struct rte_ether_hdr));
82*99a2dd95SBruce Richardson /* Extract cksum, ttl as ipv4 hdr is in cache */
83*99a2dd95SBruce Richardson node_mbuf_priv1(mbuf, dyn)->cksum = ipv4_hdr->hdr_checksum;
84*99a2dd95SBruce Richardson node_mbuf_priv1(mbuf, dyn)->ttl = ipv4_hdr->time_to_live;
85*99a2dd95SBruce Richardson
86*99a2dd95SBruce Richardson rc = rte_lpm_lookup(lpm, rte_be_to_cpu_32(ipv4_hdr->dst_addr),
87*99a2dd95SBruce Richardson &next_hop);
88*99a2dd95SBruce Richardson next_hop = (rc == 0) ? next_hop : drop_nh;
89*99a2dd95SBruce Richardson
90*99a2dd95SBruce Richardson node_mbuf_priv1(mbuf, dyn)->nh = (uint16_t)next_hop;
91*99a2dd95SBruce Richardson next_hop = next_hop >> 16;
92*99a2dd95SBruce Richardson next = (uint16_t)next_hop;
93*99a2dd95SBruce Richardson
94*99a2dd95SBruce Richardson if (unlikely(next_index != next)) {
95*99a2dd95SBruce Richardson /* Copy things successfully speculated till now */
96*99a2dd95SBruce Richardson rte_memcpy(to_next, from, last_spec * sizeof(from[0]));
97*99a2dd95SBruce Richardson from += last_spec;
98*99a2dd95SBruce Richardson to_next += last_spec;
99*99a2dd95SBruce Richardson held += last_spec;
100*99a2dd95SBruce Richardson last_spec = 0;
101*99a2dd95SBruce Richardson
102*99a2dd95SBruce Richardson rte_node_enqueue_x1(graph, node, next, from[0]);
103*99a2dd95SBruce Richardson from += 1;
104*99a2dd95SBruce Richardson } else {
105*99a2dd95SBruce Richardson last_spec += 1;
106*99a2dd95SBruce Richardson }
107*99a2dd95SBruce Richardson }
108*99a2dd95SBruce Richardson
109*99a2dd95SBruce Richardson /* !!! Home run !!! */
110*99a2dd95SBruce Richardson if (likely(last_spec == nb_objs)) {
111*99a2dd95SBruce Richardson rte_node_next_stream_move(graph, node, next_index);
112*99a2dd95SBruce Richardson return nb_objs;
113*99a2dd95SBruce Richardson }
114*99a2dd95SBruce Richardson held += last_spec;
115*99a2dd95SBruce Richardson rte_memcpy(to_next, from, last_spec * sizeof(from[0]));
116*99a2dd95SBruce Richardson rte_node_next_stream_put(graph, node, next_index, held);
117*99a2dd95SBruce Richardson
118*99a2dd95SBruce Richardson return nb_objs;
119*99a2dd95SBruce Richardson }
120*99a2dd95SBruce Richardson
121*99a2dd95SBruce Richardson int
rte_node_ip4_route_add(uint32_t ip,uint8_t depth,uint16_t next_hop,enum rte_node_ip4_lookup_next next_node)122*99a2dd95SBruce Richardson rte_node_ip4_route_add(uint32_t ip, uint8_t depth, uint16_t next_hop,
123*99a2dd95SBruce Richardson enum rte_node_ip4_lookup_next next_node)
124*99a2dd95SBruce Richardson {
125*99a2dd95SBruce Richardson char abuf[INET6_ADDRSTRLEN];
126*99a2dd95SBruce Richardson struct in_addr in;
127*99a2dd95SBruce Richardson uint8_t socket;
128*99a2dd95SBruce Richardson uint32_t val;
129*99a2dd95SBruce Richardson int ret;
130*99a2dd95SBruce Richardson
131*99a2dd95SBruce Richardson in.s_addr = htonl(ip);
132*99a2dd95SBruce Richardson inet_ntop(AF_INET, &in, abuf, sizeof(abuf));
133*99a2dd95SBruce Richardson /* Embedded next node id into 24 bit next hop */
134*99a2dd95SBruce Richardson val = ((next_node << 16) | next_hop) & ((1ull << 24) - 1);
135*99a2dd95SBruce Richardson node_dbg("ip4_lookup", "LPM: Adding route %s / %d nh (0x%x)", abuf,
136*99a2dd95SBruce Richardson depth, val);
137*99a2dd95SBruce Richardson
138*99a2dd95SBruce Richardson for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
139*99a2dd95SBruce Richardson if (!ip4_lookup_nm.lpm_tbl[socket])
140*99a2dd95SBruce Richardson continue;
141*99a2dd95SBruce Richardson
142*99a2dd95SBruce Richardson ret = rte_lpm_add(ip4_lookup_nm.lpm_tbl[socket],
143*99a2dd95SBruce Richardson ip, depth, val);
144*99a2dd95SBruce Richardson if (ret < 0) {
145*99a2dd95SBruce Richardson node_err("ip4_lookup",
146*99a2dd95SBruce Richardson "Unable to add entry %s / %d nh (%x) to LPM table on sock %d, rc=%d\n",
147*99a2dd95SBruce Richardson abuf, depth, val, socket, ret);
148*99a2dd95SBruce Richardson return ret;
149*99a2dd95SBruce Richardson }
150*99a2dd95SBruce Richardson }
151*99a2dd95SBruce Richardson
152*99a2dd95SBruce Richardson return 0;
153*99a2dd95SBruce Richardson }
154*99a2dd95SBruce Richardson
155*99a2dd95SBruce Richardson static int
setup_lpm(struct ip4_lookup_node_main * nm,int socket)156*99a2dd95SBruce Richardson setup_lpm(struct ip4_lookup_node_main *nm, int socket)
157*99a2dd95SBruce Richardson {
158*99a2dd95SBruce Richardson struct rte_lpm_config config_ipv4;
159*99a2dd95SBruce Richardson char s[RTE_LPM_NAMESIZE];
160*99a2dd95SBruce Richardson
161*99a2dd95SBruce Richardson /* One LPM table per socket */
162*99a2dd95SBruce Richardson if (nm->lpm_tbl[socket])
163*99a2dd95SBruce Richardson return 0;
164*99a2dd95SBruce Richardson
165*99a2dd95SBruce Richardson /* create the LPM table */
166*99a2dd95SBruce Richardson config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
167*99a2dd95SBruce Richardson config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
168*99a2dd95SBruce Richardson config_ipv4.flags = 0;
169*99a2dd95SBruce Richardson snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socket);
170*99a2dd95SBruce Richardson nm->lpm_tbl[socket] = rte_lpm_create(s, socket, &config_ipv4);
171*99a2dd95SBruce Richardson if (nm->lpm_tbl[socket] == NULL)
172*99a2dd95SBruce Richardson return -rte_errno;
173*99a2dd95SBruce Richardson
174*99a2dd95SBruce Richardson return 0;
175*99a2dd95SBruce Richardson }
176*99a2dd95SBruce Richardson
177*99a2dd95SBruce Richardson static int
ip4_lookup_node_init(const struct rte_graph * graph,struct rte_node * node)178*99a2dd95SBruce Richardson ip4_lookup_node_init(const struct rte_graph *graph, struct rte_node *node)
179*99a2dd95SBruce Richardson {
180*99a2dd95SBruce Richardson uint16_t socket, lcore_id;
181*99a2dd95SBruce Richardson static uint8_t init_once;
182*99a2dd95SBruce Richardson int rc;
183*99a2dd95SBruce Richardson
184*99a2dd95SBruce Richardson RTE_SET_USED(graph);
185*99a2dd95SBruce Richardson RTE_BUILD_BUG_ON(sizeof(struct ip4_lookup_node_ctx) > RTE_NODE_CTX_SZ);
186*99a2dd95SBruce Richardson
187*99a2dd95SBruce Richardson if (!init_once) {
188*99a2dd95SBruce Richardson node_mbuf_priv1_dynfield_offset = rte_mbuf_dynfield_register(
189*99a2dd95SBruce Richardson &node_mbuf_priv1_dynfield_desc);
190*99a2dd95SBruce Richardson if (node_mbuf_priv1_dynfield_offset < 0)
191*99a2dd95SBruce Richardson return -rte_errno;
192*99a2dd95SBruce Richardson
193*99a2dd95SBruce Richardson /* Setup LPM tables for all sockets */
194*99a2dd95SBruce Richardson RTE_LCORE_FOREACH(lcore_id)
195*99a2dd95SBruce Richardson {
196*99a2dd95SBruce Richardson socket = rte_lcore_to_socket_id(lcore_id);
197*99a2dd95SBruce Richardson rc = setup_lpm(&ip4_lookup_nm, socket);
198*99a2dd95SBruce Richardson if (rc) {
199*99a2dd95SBruce Richardson node_err("ip4_lookup",
200*99a2dd95SBruce Richardson "Failed to setup lpm tbl for sock %u, rc=%d",
201*99a2dd95SBruce Richardson socket, rc);
202*99a2dd95SBruce Richardson return rc;
203*99a2dd95SBruce Richardson }
204*99a2dd95SBruce Richardson }
205*99a2dd95SBruce Richardson init_once = 1;
206*99a2dd95SBruce Richardson }
207*99a2dd95SBruce Richardson
208*99a2dd95SBruce Richardson /* Update socket's LPM and mbuf dyn priv1 offset in node ctx */
209*99a2dd95SBruce Richardson IP4_LOOKUP_NODE_LPM(node->ctx) = ip4_lookup_nm.lpm_tbl[graph->socket];
210*99a2dd95SBruce Richardson IP4_LOOKUP_NODE_PRIV1_OFF(node->ctx) = node_mbuf_priv1_dynfield_offset;
211*99a2dd95SBruce Richardson
212*99a2dd95SBruce Richardson #if defined(__ARM_NEON) || defined(RTE_ARCH_X86)
213*99a2dd95SBruce Richardson if (rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_128)
214*99a2dd95SBruce Richardson node->process = ip4_lookup_node_process_vec;
215*99a2dd95SBruce Richardson #endif
216*99a2dd95SBruce Richardson
217*99a2dd95SBruce Richardson node_dbg("ip4_lookup", "Initialized ip4_lookup node");
218*99a2dd95SBruce Richardson
219*99a2dd95SBruce Richardson return 0;
220*99a2dd95SBruce Richardson }
221*99a2dd95SBruce Richardson
222*99a2dd95SBruce Richardson static struct rte_node_register ip4_lookup_node = {
223*99a2dd95SBruce Richardson .process = ip4_lookup_node_process_scalar,
224*99a2dd95SBruce Richardson .name = "ip4_lookup",
225*99a2dd95SBruce Richardson
226*99a2dd95SBruce Richardson .init = ip4_lookup_node_init,
227*99a2dd95SBruce Richardson
228*99a2dd95SBruce Richardson .nb_edges = RTE_NODE_IP4_LOOKUP_NEXT_MAX,
229*99a2dd95SBruce Richardson .next_nodes = {
230*99a2dd95SBruce Richardson [RTE_NODE_IP4_LOOKUP_NEXT_REWRITE] = "ip4_rewrite",
231*99a2dd95SBruce Richardson [RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP] = "pkt_drop",
232*99a2dd95SBruce Richardson },
233*99a2dd95SBruce Richardson };
234*99a2dd95SBruce Richardson
235*99a2dd95SBruce Richardson RTE_NODE_REGISTER(ip4_lookup_node);
236