Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2023 Marvell.
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_lpm6.h>
15 : :
16 : : #include "rte_node_ip6_api.h"
17 : :
18 : : #include "node_private.h"
19 : :
20 : : #define IPV6_L3FWD_LPM_MAX_RULES 1024
21 : : #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 8)
22 : :
23 : : /* IP6 Lookup global data struct */
24 : : struct ip6_lookup_node_main {
25 : : struct rte_lpm6 *lpm_tbl[RTE_MAX_NUMA_NODES];
26 : : };
27 : :
28 : : struct ip6_lookup_node_ctx {
29 : : /* Socket's LPM table */
30 : : struct rte_lpm6 *lpm6;
31 : : /* Dynamic offset to mbuf priv1 */
32 : : int mbuf_priv1_off;
33 : : };
34 : :
35 : : static struct ip6_lookup_node_main ip6_lookup_nm;
36 : :
37 : : #define IP6_LOOKUP_NODE_LPM(ctx) \
38 : : (((struct ip6_lookup_node_ctx *)ctx)->lpm6)
39 : :
40 : : #define IP6_LOOKUP_NODE_PRIV1_OFF(ctx) \
41 : : (((struct ip6_lookup_node_ctx *)ctx)->mbuf_priv1_off)
42 : :
43 : : static uint16_t
44 : 0 : ip6_lookup_node_process_scalar(struct rte_graph *graph, struct rte_node *node,
45 : : void **objs, uint16_t nb_objs)
46 : : {
47 : : struct rte_mbuf *mbuf0, *mbuf1, *mbuf2, *mbuf3, **pkts;
48 : 0 : struct rte_lpm6 *lpm6 = IP6_LOOKUP_NODE_LPM(node->ctx);
49 : 0 : const int dyn = IP6_LOOKUP_NODE_PRIV1_OFF(node->ctx);
50 : : struct rte_ipv6_hdr *ipv6_hdr;
51 : : void **to_next, **from;
52 : : uint16_t last_spec = 0;
53 : : rte_edge_t next_index;
54 : : uint16_t n_left_from;
55 : : uint16_t held = 0;
56 : : uint32_t drop_nh;
57 : : int i, rc;
58 : :
59 : : /* Speculative next */
60 : : next_index = RTE_NODE_IP6_LOOKUP_NEXT_REWRITE;
61 : : /* Drop node */
62 : : drop_nh = ((uint32_t)RTE_NODE_IP6_LOOKUP_NEXT_PKT_DROP) << 16;
63 : :
64 : : pkts = (struct rte_mbuf **)objs;
65 : : from = objs;
66 : : n_left_from = nb_objs;
67 : :
68 [ # # ]: 0 : for (i = OBJS_PER_CLINE; i < RTE_GRAPH_BURST_SIZE; i += OBJS_PER_CLINE)
69 : 0 : rte_prefetch0(&objs[i]);
70 : :
71 [ # # # # ]: 0 : for (i = 0; i < 4 && i < n_left_from; i++)
72 : 0 : rte_prefetch0(rte_pktmbuf_mtod_offset(pkts[i], void *,
73 : : sizeof(struct rte_ether_hdr)));
74 : :
75 : : /* Get stream for the speculated next node */
76 : 0 : to_next = rte_node_next_stream_get(graph, node, next_index, nb_objs);
77 [ # # ]: 0 : while (n_left_from >= 4) {
78 : : struct rte_ipv6_addr ip_batch[4];
79 : : int32_t next_hop[4];
80 : : uint16_t next[4];
81 : :
82 : : #if RTE_GRAPH_BURST_SIZE > 64
83 : : /* Prefetch next-next mbufs */
84 [ # # ]: 0 : if (likely(n_left_from > 11)) {
85 : 0 : rte_prefetch0(pkts[8]);
86 : 0 : rte_prefetch0(pkts[9]);
87 : 0 : rte_prefetch0(pkts[10]);
88 : 0 : rte_prefetch0(pkts[11]);
89 : : }
90 : : #endif
91 : : /* Prefetch next mbuf data */
92 [ # # ]: 0 : if (likely(n_left_from > 7)) {
93 : 0 : rte_prefetch0(rte_pktmbuf_mtod_offset(pkts[4], void *,
94 : : sizeof(struct rte_ether_hdr)));
95 : 0 : rte_prefetch0(rte_pktmbuf_mtod_offset(pkts[5], void *,
96 : : sizeof(struct rte_ether_hdr)));
97 : 0 : rte_prefetch0(rte_pktmbuf_mtod_offset(pkts[6], void *,
98 : : sizeof(struct rte_ether_hdr)));
99 : 0 : rte_prefetch0(rte_pktmbuf_mtod_offset(pkts[7], void *,
100 : : sizeof(struct rte_ether_hdr)));
101 : : }
102 : :
103 : 0 : mbuf0 = pkts[0];
104 : 0 : mbuf1 = pkts[1];
105 : 0 : mbuf2 = pkts[2];
106 : 0 : mbuf3 = pkts[3];
107 : :
108 : 0 : pkts += 4;
109 : 0 : n_left_from -= 4;
110 : :
111 : : /* Extract DIP of mbuf0 */
112 : 0 : ipv6_hdr = rte_pktmbuf_mtod_offset(mbuf0, struct rte_ipv6_hdr *,
113 : : sizeof(struct rte_ether_hdr));
114 : : /* Extract hop_limits as ipv6 hdr is in cache */
115 : 0 : node_mbuf_priv1(mbuf0, dyn)->ttl = ipv6_hdr->hop_limits;
116 : 0 : ip_batch[0] = ipv6_hdr->dst_addr;
117 : :
118 : : /* Extract DIP of mbuf1 */
119 : 0 : ipv6_hdr = rte_pktmbuf_mtod_offset(mbuf1, struct rte_ipv6_hdr *,
120 : : sizeof(struct rte_ether_hdr));
121 : : /* Extract hop_limits as ipv6 hdr is in cache */
122 : 0 : node_mbuf_priv1(mbuf1, dyn)->ttl = ipv6_hdr->hop_limits;
123 : 0 : ip_batch[1] = ipv6_hdr->dst_addr;
124 : :
125 : : /* Extract DIP of mbuf2 */
126 : 0 : ipv6_hdr = rte_pktmbuf_mtod_offset(mbuf2, struct rte_ipv6_hdr *,
127 : : sizeof(struct rte_ether_hdr));
128 : : /* Extract hop_limits as ipv6 hdr is in cache */
129 : 0 : node_mbuf_priv1(mbuf2, dyn)->ttl = ipv6_hdr->hop_limits;
130 : 0 : ip_batch[2] = ipv6_hdr->dst_addr;
131 : :
132 : : /* Extract DIP of mbuf3 */
133 : 0 : ipv6_hdr = rte_pktmbuf_mtod_offset(mbuf3, struct rte_ipv6_hdr *,
134 : : sizeof(struct rte_ether_hdr));
135 : : /* Extract hop_limits as ipv6 hdr is in cache */
136 : 0 : node_mbuf_priv1(mbuf3, dyn)->ttl = ipv6_hdr->hop_limits;
137 : 0 : ip_batch[3] = ipv6_hdr->dst_addr;
138 : :
139 : 0 : rte_lpm6_lookup_bulk_func(lpm6, ip_batch, next_hop, 4);
140 : :
141 [ # # ]: 0 : next_hop[0] = (next_hop[0] < 0) ? (int32_t)drop_nh : next_hop[0];
142 : 0 : node_mbuf_priv1(mbuf0, dyn)->nh = (uint16_t)next_hop[0];
143 : 0 : next[0] = (uint16_t)(next_hop[0] >> 16);
144 : :
145 [ # # ]: 0 : next_hop[1] = (next_hop[1] < 0) ? (int32_t)drop_nh : next_hop[1];
146 : 0 : node_mbuf_priv1(mbuf1, dyn)->nh = (uint16_t)next_hop[1];
147 : 0 : next[1] = (uint16_t)(next_hop[1] >> 16);
148 : :
149 [ # # ]: 0 : next_hop[2] = (next_hop[2] < 0) ? (int32_t)drop_nh : next_hop[2];
150 : 0 : node_mbuf_priv1(mbuf2, dyn)->nh = (uint16_t)next_hop[2];
151 : 0 : next[2] = (uint16_t)(next_hop[2] >> 16);
152 : :
153 [ # # ]: 0 : next_hop[3] = (next_hop[3] < 0) ? (int32_t)drop_nh : next_hop[3];
154 : 0 : node_mbuf_priv1(mbuf3, dyn)->nh = (uint16_t)next_hop[3];
155 : 0 : next[3] = (uint16_t)(next_hop[3] >> 16);
156 : :
157 [ # # ]: 0 : rte_edge_t fix_spec = ((next_index == next[0]) &&
158 [ # # ]: 0 : (next_index == next[1]) &&
159 [ # # # # ]: 0 : (next_index == next[2]) &&
160 : : (next_index == next[3]));
161 : :
162 [ # # ]: 0 : if (unlikely(fix_spec == 0)) {
163 : : /* Copy things successfully speculated till now */
164 [ # # ]: 0 : rte_memcpy(to_next, from, last_spec * sizeof(from[0]));
165 : 0 : from += last_spec;
166 : 0 : to_next += last_spec;
167 : 0 : held += last_spec;
168 : : last_spec = 0;
169 : :
170 : : /* Next0 */
171 [ # # ]: 0 : if (next_index == next[0]) {
172 : 0 : to_next[0] = from[0];
173 : 0 : to_next++;
174 : 0 : held++;
175 : : } else {
176 : 0 : rte_node_enqueue_x1(graph, node, next[0], from[0]);
177 : : }
178 : :
179 : : /* Next1 */
180 [ # # ]: 0 : if (next_index == next[1]) {
181 : 0 : to_next[0] = from[1];
182 : 0 : to_next++;
183 : 0 : held++;
184 : : } else {
185 : 0 : rte_node_enqueue_x1(graph, node, next[1], from[1]);
186 : : }
187 : :
188 : : /* Next2 */
189 [ # # ]: 0 : if (next_index == next[2]) {
190 : 0 : to_next[0] = from[2];
191 : 0 : to_next++;
192 : 0 : held++;
193 : : } else {
194 : 0 : rte_node_enqueue_x1(graph, node, next[2], from[2]);
195 : : }
196 : :
197 : : /* Next3 */
198 [ # # ]: 0 : if (next_index == next[3]) {
199 : 0 : to_next[0] = from[3];
200 : 0 : to_next++;
201 : 0 : held++;
202 : : } else {
203 : 0 : rte_node_enqueue_x1(graph, node, next[3], from[3]);
204 : : }
205 : :
206 : 0 : from += 4;
207 : : } else {
208 : 0 : last_spec += 4;
209 : : }
210 : : }
211 : :
212 [ # # ]: 0 : while (n_left_from > 0) {
213 : : uint32_t next_hop;
214 : : uint16_t next0;
215 : :
216 : 0 : mbuf0 = pkts[0];
217 : :
218 : 0 : pkts += 1;
219 : 0 : n_left_from -= 1;
220 : :
221 : : /* Extract DIP of mbuf0 */
222 : 0 : ipv6_hdr = rte_pktmbuf_mtod_offset(mbuf0, struct rte_ipv6_hdr *,
223 : : sizeof(struct rte_ether_hdr));
224 : : /* Extract TTL as IPv6 hdr is in cache */
225 : 0 : node_mbuf_priv1(mbuf0, dyn)->ttl = ipv6_hdr->hop_limits;
226 : :
227 : 0 : rc = rte_lpm6_lookup(lpm6, &ipv6_hdr->dst_addr, &next_hop);
228 [ # # ]: 0 : next_hop = (rc == 0) ? next_hop : drop_nh;
229 : :
230 : 0 : node_mbuf_priv1(mbuf0, dyn)->nh = (uint16_t)next_hop;
231 : 0 : next_hop = next_hop >> 16;
232 : 0 : next0 = (uint16_t)next_hop;
233 : :
234 [ # # ]: 0 : if (unlikely(next_index ^ next0)) {
235 : : /* Copy things successfully speculated till now */
236 [ # # ]: 0 : rte_memcpy(to_next, from, last_spec * sizeof(from[0]));
237 : 0 : from += last_spec;
238 : 0 : to_next += last_spec;
239 : 0 : held += last_spec;
240 : : last_spec = 0;
241 : :
242 : 0 : rte_node_enqueue_x1(graph, node, next0, from[0]);
243 : 0 : from += 1;
244 : : } else {
245 : 0 : last_spec += 1;
246 : : }
247 : : }
248 : :
249 : : /* !!! Home run !!! */
250 [ # # ]: 0 : if (likely(last_spec == nb_objs)) {
251 : 0 : rte_node_next_stream_move(graph, node, next_index);
252 : 0 : return nb_objs;
253 : : }
254 : 0 : held += last_spec;
255 [ # # ]: 0 : rte_memcpy(to_next, from, last_spec * sizeof(from[0]));
256 : : rte_node_next_stream_put(graph, node, next_index, held);
257 : :
258 : : return nb_objs;
259 : : }
260 : :
261 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_node_ip6_route_add, 23.07)
262 : : int
263 : 0 : rte_node_ip6_route_add(const struct rte_ipv6_addr *ip, uint8_t depth, uint16_t next_hop,
264 : : enum rte_node_ip6_lookup_next next_node)
265 : : {
266 : : char abuf[INET6_ADDRSTRLEN];
267 : : uint8_t socket;
268 : : uint32_t val;
269 : : int ret;
270 : :
271 : 0 : inet_ntop(AF_INET6, ip, abuf, sizeof(abuf));
272 : : /* Embedded next node id into 24 bit next hop */
273 : 0 : val = ((next_node << 16) | next_hop) & ((1ull << 24) - 1);
274 : 0 : node_dbg("ip6_lookup", "LPM: Adding route %s / %d nh (0x%x)", abuf,
275 : : depth, val);
276 : :
277 [ # # ]: 0 : for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
278 [ # # ]: 0 : if (!ip6_lookup_nm.lpm_tbl[socket])
279 : 0 : continue;
280 : :
281 : 0 : ret = rte_lpm6_add(ip6_lookup_nm.lpm_tbl[socket], ip, depth, val);
282 [ # # ]: 0 : if (ret < 0) {
283 : 0 : node_err("ip6_lookup",
284 : : "Unable to add entry %s / %d nh (%x) to LPM "
285 : : "table on sock %d, rc=%d",
286 : : abuf, depth, val, socket, ret);
287 : 0 : return ret;
288 : : }
289 : : }
290 : :
291 : : return 0;
292 : : }
293 : :
294 : : static int
295 : 0 : setup_lpm6(struct ip6_lookup_node_main *nm, int socket)
296 : : {
297 : : struct rte_lpm6_config config_ipv6;
298 : : char s[RTE_LPM6_NAMESIZE];
299 : :
300 : : /* One LPM table per socket */
301 [ # # ]: 0 : if (nm->lpm_tbl[socket])
302 : : return 0;
303 : :
304 : : /* create the LPM table */
305 : 0 : config_ipv6.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
306 : 0 : config_ipv6.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
307 : 0 : config_ipv6.flags = 0;
308 : : snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socket);
309 : 0 : nm->lpm_tbl[socket] = rte_lpm6_create(s, socket, &config_ipv6);
310 [ # # ]: 0 : if (nm->lpm_tbl[socket] == NULL)
311 : 0 : return -rte_errno;
312 : :
313 : : return 0;
314 : : }
315 : :
316 : : static int
317 : 0 : ip6_lookup_node_init(const struct rte_graph *graph, struct rte_node *node)
318 : : {
319 : : uint16_t socket, lcore_id;
320 : : static uint8_t init_once;
321 : : int rc;
322 : :
323 : : RTE_SET_USED(graph);
324 : : RTE_BUILD_BUG_ON(sizeof(struct ip6_lookup_node_ctx) > RTE_NODE_CTX_SZ);
325 : :
326 [ # # ]: 0 : if (!init_once) {
327 : 0 : node_mbuf_priv1_dynfield_offset =
328 : 0 : rte_mbuf_dynfield_register(
329 : : &node_mbuf_priv1_dynfield_desc);
330 [ # # ]: 0 : if (node_mbuf_priv1_dynfield_offset < 0)
331 : 0 : return -rte_errno;
332 : :
333 : : /* Setup LPM tables for all sockets */
334 [ # # ]: 0 : RTE_LCORE_FOREACH(lcore_id)
335 : : {
336 : 0 : socket = rte_lcore_to_socket_id(lcore_id);
337 : 0 : rc = setup_lpm6(&ip6_lookup_nm, socket);
338 [ # # ]: 0 : if (rc) {
339 : 0 : node_err("ip6_lookup",
340 : : "Failed to setup lpm6 tbl for "
341 : : "sock %u, rc=%d", socket, rc);
342 : 0 : return rc;
343 : : }
344 : : }
345 : 0 : init_once = 1;
346 : : }
347 : :
348 : : /* Update socket's LPM and mbuf dyn priv1 offset in node ctx */
349 : 0 : IP6_LOOKUP_NODE_LPM(node->ctx) = ip6_lookup_nm.lpm_tbl[graph->socket];
350 : 0 : IP6_LOOKUP_NODE_PRIV1_OFF(node->ctx) =
351 : : node_mbuf_priv1_dynfield_offset;
352 : :
353 : 0 : node_dbg("ip6_lookup", "Initialized ip6_lookup node");
354 : :
355 : 0 : return 0;
356 : : }
357 : :
358 : : static struct rte_node_register ip6_lookup_node = {
359 : : .process = ip6_lookup_node_process_scalar,
360 : : .name = "ip6_lookup",
361 : :
362 : : .init = ip6_lookup_node_init,
363 : :
364 : : .nb_edges = RTE_NODE_IP6_LOOKUP_NEXT_PKT_DROP + 1,
365 : : .next_nodes = {
366 : : [RTE_NODE_IP6_LOOKUP_NEXT_REWRITE] = "ip6_rewrite",
367 : : [RTE_NODE_IP6_LOOKUP_NEXT_PKT_DROP] = "pkt_drop",
368 : : },
369 : : };
370 : :
371 : 252 : RTE_NODE_REGISTER(ip6_lookup_node);
|