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