1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2020 Marvell International Ltd.
6 #include <sys/socket.h>
9 #include <rte_ethdev.h>
10 #include <rte_ether.h>
11 #include <rte_graph.h>
12 #include <rte_graph_worker.h>
20 #include "rte_node_ip4_api.h"
22 #include "node_private.h"
24 #define IPV4_L3FWD_LPM_MAX_RULES 1024
25 #define IPV4_L3FWD_LPM_NUMBER_TBL8S (1 << 8)
27 /* IP4 Lookup global data struct */
28 struct ip4_lookup_node_main {
29 struct rte_lpm *lpm_tbl[RTE_MAX_NUMA_NODES];
32 struct ip4_lookup_node_ctx {
33 /* Socket's LPM table */
35 /* Dynamic offset to mbuf priv1 */
39 int node_mbuf_priv1_dynfield_offset = -1;
41 static struct ip4_lookup_node_main ip4_lookup_nm;
43 #define IP4_LOOKUP_NODE_LPM(ctx) \
44 (((struct ip4_lookup_node_ctx *)ctx)->lpm)
46 #define IP4_LOOKUP_NODE_PRIV1_OFF(ctx) \
47 (((struct ip4_lookup_node_ctx *)ctx)->mbuf_priv1_off)
49 #if defined(__ARM_NEON)
50 #include "ip4_lookup_neon.h"
51 #elif defined(RTE_ARCH_X86)
52 #include "ip4_lookup_sse.h"
56 ip4_lookup_node_process_scalar(struct rte_graph *graph, struct rte_node *node,
57 void **objs, uint16_t nb_objs)
59 struct rte_lpm *lpm = IP4_LOOKUP_NODE_LPM(node->ctx);
60 const int dyn = IP4_LOOKUP_NODE_PRIV1_OFF(node->ctx);
61 struct rte_ipv4_hdr *ipv4_hdr;
62 void **to_next, **from;
63 uint16_t last_spec = 0;
64 struct rte_mbuf *mbuf;
65 rte_edge_t next_index;
70 /* Speculative next */
71 next_index = RTE_NODE_IP4_LOOKUP_NEXT_REWRITE;
73 drop_nh = ((uint32_t)RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP) << 16;
76 /* Get stream for the speculated next node */
77 to_next = rte_node_next_stream_get(graph, node, next_index, nb_objs);
78 for (i = 0; i < nb_objs; i++) {
82 mbuf = (struct rte_mbuf *)objs[i];
84 /* Extract DIP of mbuf0 */
85 ipv4_hdr = rte_pktmbuf_mtod_offset(mbuf, struct rte_ipv4_hdr *,
86 sizeof(struct rte_ether_hdr));
87 /* Extract cksum, ttl as ipv4 hdr is in cache */
88 node_mbuf_priv1(mbuf, dyn)->cksum = ipv4_hdr->hdr_checksum;
89 node_mbuf_priv1(mbuf, dyn)->ttl = ipv4_hdr->time_to_live;
91 rc = rte_lpm_lookup(lpm, rte_be_to_cpu_32(ipv4_hdr->dst_addr),
93 next_hop = (rc == 0) ? next_hop : drop_nh;
95 node_mbuf_priv1(mbuf, dyn)->nh = (uint16_t)next_hop;
96 next_hop = next_hop >> 16;
97 next = (uint16_t)next_hop;
99 if (unlikely(next_index != next)) {
100 /* Copy things successfully speculated till now */
101 rte_memcpy(to_next, from, last_spec * sizeof(from[0]));
103 to_next += last_spec;
107 rte_node_enqueue_x1(graph, node, next, from[0]);
114 /* !!! Home run !!! */
115 if (likely(last_spec == nb_objs)) {
116 rte_node_next_stream_move(graph, node, next_index);
120 rte_memcpy(to_next, from, last_spec * sizeof(from[0]));
121 rte_node_next_stream_put(graph, node, next_index, held);
127 rte_node_ip4_route_add(uint32_t ip, uint8_t depth, uint16_t next_hop,
128 enum rte_node_ip4_lookup_next next_node)
130 char abuf[INET6_ADDRSTRLEN];
136 in.s_addr = htonl(ip);
137 inet_ntop(AF_INET, &in, abuf, sizeof(abuf));
138 /* Embedded next node id into 24 bit next hop */
139 val = ((next_node << 16) | next_hop) & ((1ull << 24) - 1);
140 node_dbg("ip4_lookup", "LPM: Adding route %s / %d nh (0x%x)", abuf,
143 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
144 if (!ip4_lookup_nm.lpm_tbl[socket])
147 ret = rte_lpm_add(ip4_lookup_nm.lpm_tbl[socket],
150 node_err("ip4_lookup",
151 "Unable to add entry %s / %d nh (%x) to LPM table on sock %d, rc=%d\n",
152 abuf, depth, val, socket, ret);
161 setup_lpm(struct ip4_lookup_node_main *nm, int socket)
163 struct rte_lpm_config config_ipv4;
164 char s[RTE_LPM_NAMESIZE];
166 /* One LPM table per socket */
167 if (nm->lpm_tbl[socket])
170 /* create the LPM table */
171 config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
172 config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
173 config_ipv4.flags = 0;
174 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socket);
175 nm->lpm_tbl[socket] = rte_lpm_create(s, socket, &config_ipv4);
176 if (nm->lpm_tbl[socket] == NULL)
183 ip4_lookup_node_init(const struct rte_graph *graph, struct rte_node *node)
185 uint16_t socket, lcore_id;
186 static uint8_t init_once;
190 RTE_BUILD_BUG_ON(sizeof(struct ip4_lookup_node_ctx) > RTE_NODE_CTX_SZ);
193 node_mbuf_priv1_dynfield_offset = rte_mbuf_dynfield_register(
194 &node_mbuf_priv1_dynfield_desc);
195 if (node_mbuf_priv1_dynfield_offset < 0)
198 /* Setup LPM tables for all sockets */
199 RTE_LCORE_FOREACH(lcore_id)
201 socket = rte_lcore_to_socket_id(lcore_id);
202 rc = setup_lpm(&ip4_lookup_nm, socket);
204 node_err("ip4_lookup",
205 "Failed to setup lpm tbl for sock %u, rc=%d",
213 /* Update socket's LPM and mbuf dyn priv1 offset in node ctx */
214 IP4_LOOKUP_NODE_LPM(node->ctx) = ip4_lookup_nm.lpm_tbl[graph->socket];
215 IP4_LOOKUP_NODE_PRIV1_OFF(node->ctx) = node_mbuf_priv1_dynfield_offset;
217 #if defined(__ARM_NEON) || defined(RTE_ARCH_X86)
218 if (rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_128)
219 node->process = ip4_lookup_node_process_vec;
222 node_dbg("ip4_lookup", "Initialized ip4_lookup node");
227 static struct rte_node_register ip4_lookup_node = {
228 .process = ip4_lookup_node_process_scalar,
229 .name = "ip4_lookup",
231 .init = ip4_lookup_node_init,
233 .nb_edges = RTE_NODE_IP4_LOOKUP_NEXT_MAX,
235 [RTE_NODE_IP4_LOOKUP_NEXT_REWRITE] = "ip4_rewrite",
236 [RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP] = "pkt_drop",
240 RTE_NODE_REGISTER(ip4_lookup_node);