Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : * Copyright(c) 2020 Arm Limited
4 : : */
5 : :
6 : : #include <string.h>
7 : : #include <stdint.h>
8 : : #include <errno.h>
9 : : #include <stdio.h>
10 : : #include <sys/queue.h>
11 : :
12 : : #include <eal_export.h>
13 : : #include <rte_log.h>
14 : : #include <rte_common.h>
15 : : #include <rte_malloc.h>
16 : : #include <rte_eal_memconfig.h>
17 : : #include <rte_string_fns.h>
18 : : #include <rte_errno.h>
19 : : #include <rte_tailq.h>
20 : :
21 : : #include "rte_lpm.h"
22 : : #include "lpm_log.h"
23 : :
24 [ - + ]: 276 : RTE_LOG_REGISTER_DEFAULT(lpm_logtype, INFO);
25 : :
26 : : TAILQ_HEAD(rte_lpm_list, rte_tailq_entry);
27 : :
28 : : static struct rte_tailq_elem rte_lpm_tailq = {
29 : : .name = "RTE_LPM",
30 : : };
31 [ - + ]: 276 : EAL_REGISTER_TAILQ(rte_lpm_tailq)
32 : :
33 : : #define MAX_DEPTH_TBL24 24
34 : :
35 : : enum valid_flag {
36 : : INVALID = 0,
37 : : VALID
38 : : };
39 : :
40 : : /** @internal Rule structure. */
41 : : struct rte_lpm_rule {
42 : : uint32_t ip; /**< Rule IP address. */
43 : : uint32_t next_hop; /**< Rule next hop. */
44 : : };
45 : :
46 : : /** @internal Contains metadata about the rules table. */
47 : : struct rte_lpm_rule_info {
48 : : uint32_t used_rules; /**< Used rules so far. */
49 : : uint32_t first_rule; /**< Indexes the first rule of a given depth. */
50 : : };
51 : :
52 : : /** @internal LPM structure. */
53 : : struct __rte_lpm {
54 : : /* Exposed LPM data. */
55 : : struct rte_lpm lpm;
56 : :
57 : : /* LPM metadata. */
58 : : char name[RTE_LPM_NAMESIZE]; /**< Name of the lpm. */
59 : : uint32_t max_rules; /**< Max. balanced rules per lpm. */
60 : : uint32_t number_tbl8s; /**< Number of tbl8s. */
61 : : /**< Rule info table. */
62 : : struct rte_lpm_rule_info rule_info[RTE_LPM_MAX_DEPTH];
63 : : struct rte_lpm_rule *rules_tbl; /**< LPM rules. */
64 : :
65 : : /* RCU config. */
66 : : struct rte_rcu_qsbr *v; /* RCU QSBR variable. */
67 : : enum rte_lpm_qsbr_mode rcu_mode;/* Blocking, defer queue. */
68 : : struct rte_rcu_qsbr_dq *dq; /* RCU QSBR defer queue. */
69 : : };
70 : :
71 : : /* Macro to enable/disable run-time checks. */
72 : : #if defined(RTE_LIBRTE_LPM_DEBUG)
73 : : #include <rte_debug.h>
74 : : #define VERIFY_DEPTH(depth) do { \
75 : : if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH)) \
76 : : rte_panic("LPM: Invalid depth (%u) at line %d", \
77 : : (unsigned)(depth), __LINE__); \
78 : : } while (0)
79 : : #else
80 : : #define VERIFY_DEPTH(depth)
81 : : #endif
82 : :
83 : : /*
84 : : * Converts a given depth value to its corresponding mask value.
85 : : *
86 : : * depth (IN) : range = 1 - 32
87 : : * mask (OUT) : 32bit mask
88 : : */
89 : : static uint32_t __rte_pure
90 : : depth_to_mask(uint8_t depth)
91 : : {
92 : : VERIFY_DEPTH(depth);
93 : :
94 : : /* To calculate a mask start with a 1 on the left hand side and right
95 : : * shift while populating the left hand side with 1's
96 : : */
97 : 59264 : return (int)0x80000000 >> (depth - 1);
98 : : }
99 : :
100 : : /*
101 : : * Converts given depth value to its corresponding range value.
102 : : */
103 : : static uint32_t __rte_pure
104 : : depth_to_range(uint8_t depth)
105 : : {
106 : : VERIFY_DEPTH(depth);
107 : :
108 : : /*
109 : : * Calculate tbl24 range. (Note: 2^depth = 1 << depth)
110 : : */
111 : 5917 : if (depth <= MAX_DEPTH_TBL24)
112 : 76 : return 1 << (MAX_DEPTH_TBL24 - depth);
113 : :
114 : : /* Else if depth is greater than 24 */
115 : 5841 : return 1 << (RTE_LPM_MAX_DEPTH - depth);
116 : : }
117 : :
118 : : /*
119 : : * Find an existing lpm table and return a pointer to it.
120 : : */
121 : : RTE_EXPORT_SYMBOL(rte_lpm_find_existing)
122 : : struct rte_lpm *
123 : 40 : rte_lpm_find_existing(const char *name)
124 : : {
125 : : struct __rte_lpm *i_lpm = NULL;
126 : : struct rte_tailq_entry *te;
127 : : struct rte_lpm_list *lpm_list;
128 : :
129 : 40 : lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
130 : :
131 : 40 : rte_mcfg_tailq_read_lock();
132 [ + + ]: 74 : TAILQ_FOREACH(te, lpm_list, next) {
133 : 48 : i_lpm = te->data;
134 [ + + ]: 48 : if (strncmp(name, i_lpm->name, RTE_LPM_NAMESIZE) == 0)
135 : : break;
136 : : }
137 : 40 : rte_mcfg_tailq_read_unlock();
138 : :
139 [ + + ]: 40 : if (te == NULL) {
140 : 26 : rte_errno = ENOENT;
141 : 26 : return NULL;
142 : : }
143 : :
144 : 14 : return &i_lpm->lpm;
145 : : }
146 : :
147 : : /*
148 : : * Allocates memory for LPM object
149 : : */
150 : : RTE_EXPORT_SYMBOL(rte_lpm_create)
151 : : struct rte_lpm *
152 : 148 : rte_lpm_create(const char *name, int socket_id,
153 : : const struct rte_lpm_config *config)
154 : : {
155 : : char mem_name[RTE_LPM_NAMESIZE + sizeof("LPM_")];
156 : : struct __rte_lpm *i_lpm;
157 : : struct rte_lpm *lpm = NULL;
158 : : struct rte_tailq_entry *te;
159 : : uint32_t mem_size, rules_size, tbl8s_size;
160 : : struct rte_lpm_list *lpm_list;
161 : :
162 : 148 : lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
163 : :
164 : : RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl_entry) != 4);
165 : :
166 : : /* Check user arguments. */
167 [ + + + + ]: 148 : if ((name == NULL) || (socket_id < -1) || (config->max_rules == 0)
168 [ - + ]: 143 : || config->number_tbl8s > RTE_LPM_MAX_TBL8_NUM_GROUPS) {
169 : 5 : rte_errno = EINVAL;
170 : 5 : return NULL;
171 : : }
172 : :
173 [ - + ]: 143 : if (strlen(name) >= RTE_LPM_NAMESIZE) {
174 : 0 : rte_errno = ENAMETOOLONG;
175 : 0 : return NULL;
176 : : }
177 : :
178 : : snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
179 : :
180 : 143 : rte_mcfg_tailq_write_lock();
181 : :
182 : : /* guarantee there's no existing */
183 [ + + ]: 157 : TAILQ_FOREACH(te, lpm_list, next) {
184 : 20 : i_lpm = te->data;
185 [ + + ]: 20 : if (strncmp(name, i_lpm->name, RTE_LPM_NAMESIZE) == 0)
186 : : break;
187 : : }
188 : :
189 [ + + ]: 144 : if (te != NULL) {
190 : 7 : rte_errno = EEXIST;
191 : 7 : goto exit;
192 : : }
193 : :
194 : : /* Determine the amount of memory to allocate. */
195 : : mem_size = sizeof(*i_lpm);
196 : 137 : rules_size = sizeof(struct rte_lpm_rule) * config->max_rules;
197 : 137 : tbl8s_size = sizeof(struct rte_lpm_tbl_entry) *
198 : 137 : RTE_LPM_TBL8_GROUP_NUM_ENTRIES * config->number_tbl8s;
199 : :
200 : : /* allocate tailq entry */
201 : 137 : te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
202 [ - + ]: 137 : if (te == NULL) {
203 : 0 : LPM_LOG(ERR, "Failed to allocate tailq entry");
204 : 0 : rte_errno = ENOMEM;
205 : 0 : goto exit;
206 : : }
207 : :
208 : : /* Allocate memory to store the LPM data structures. */
209 : 137 : i_lpm = rte_zmalloc_socket(mem_name, mem_size,
210 : : RTE_CACHE_LINE_SIZE, socket_id);
211 [ - + ]: 137 : if (i_lpm == NULL) {
212 : 0 : LPM_LOG(ERR, "LPM memory allocation failed");
213 : 0 : rte_free(te);
214 : 0 : rte_errno = ENOMEM;
215 : 0 : goto exit;
216 : : }
217 : :
218 : 137 : i_lpm->rules_tbl = rte_zmalloc_socket(NULL,
219 : : (size_t)rules_size, RTE_CACHE_LINE_SIZE, socket_id);
220 : :
221 [ - + ]: 137 : if (i_lpm->rules_tbl == NULL) {
222 : 0 : LPM_LOG(ERR, "LPM rules_tbl memory allocation failed");
223 : 0 : rte_free(i_lpm);
224 : : i_lpm = NULL;
225 : 0 : rte_free(te);
226 : 0 : rte_errno = ENOMEM;
227 : 0 : goto exit;
228 : : }
229 : :
230 : 137 : i_lpm->lpm.tbl8 = rte_zmalloc_socket(NULL,
231 : : (size_t)tbl8s_size, RTE_CACHE_LINE_SIZE, socket_id);
232 : :
233 [ - + ]: 137 : if (i_lpm->lpm.tbl8 == NULL) {
234 : 0 : LPM_LOG(ERR, "LPM tbl8 memory allocation failed");
235 : 0 : rte_free(i_lpm->rules_tbl);
236 : 0 : rte_free(i_lpm);
237 : : i_lpm = NULL;
238 : 0 : rte_free(te);
239 : 0 : rte_errno = ENOMEM;
240 : 0 : goto exit;
241 : : }
242 : :
243 : : /* Save user arguments. */
244 : 137 : i_lpm->max_rules = config->max_rules;
245 : 137 : i_lpm->number_tbl8s = config->number_tbl8s;
246 : 137 : strlcpy(i_lpm->name, name, sizeof(i_lpm->name));
247 : :
248 : 137 : te->data = i_lpm;
249 : 137 : lpm = &i_lpm->lpm;
250 : :
251 : 137 : TAILQ_INSERT_TAIL(lpm_list, te, next);
252 : :
253 : 144 : exit:
254 : 144 : rte_mcfg_tailq_write_unlock();
255 : :
256 : 144 : return lpm;
257 : : }
258 : :
259 : : /*
260 : : * Deallocates memory for given LPM table.
261 : : */
262 : : RTE_EXPORT_SYMBOL(rte_lpm_free)
263 : : void
264 : 139 : rte_lpm_free(struct rte_lpm *lpm)
265 : : {
266 : : struct rte_lpm_list *lpm_list;
267 : : struct rte_tailq_entry *te;
268 : : struct __rte_lpm *i_lpm;
269 : :
270 : : /* Check user arguments. */
271 [ + + ]: 139 : if (lpm == NULL)
272 : : return;
273 : : i_lpm = container_of(lpm, struct __rte_lpm, lpm);
274 : :
275 : 137 : lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
276 : :
277 : 137 : rte_mcfg_tailq_write_lock();
278 : :
279 : : /* find our tailq entry */
280 [ + - ]: 150 : TAILQ_FOREACH(te, lpm_list, next) {
281 [ + + ]: 150 : if (te->data == (void *)i_lpm)
282 : : break;
283 : : }
284 [ + - ]: 137 : if (te != NULL)
285 [ - + ]: 137 : TAILQ_REMOVE(lpm_list, te, next);
286 : :
287 : 137 : rte_mcfg_tailq_write_unlock();
288 : :
289 [ + + ]: 137 : if (i_lpm->dq != NULL)
290 : 2 : rte_rcu_qsbr_dq_delete(i_lpm->dq);
291 : 137 : rte_free(i_lpm->lpm.tbl8);
292 : 137 : rte_free(i_lpm->rules_tbl);
293 : 137 : rte_free(i_lpm);
294 : 137 : rte_free(te);
295 : : }
296 : :
297 : : static void
298 : 1 : __lpm_rcu_qsbr_free_resource(void *p, void *data, unsigned int n)
299 : : {
300 : 1 : struct rte_lpm_tbl_entry *tbl8 = ((struct __rte_lpm *)p)->lpm.tbl8;
301 : : struct rte_lpm_tbl_entry zero_tbl8_entry = {0};
302 : 1 : uint32_t tbl8_group_index = *(uint32_t *)data;
303 : :
304 : : RTE_SET_USED(n);
305 : : /* Set tbl8 group invalid */
306 : 1 : rte_atomic_store_explicit(&tbl8[tbl8_group_index].val,
307 : : zero_tbl8_entry.val, rte_memory_order_relaxed);
308 : 1 : }
309 : :
310 : : /* Associate QSBR variable with an LPM object.
311 : : */
312 : : RTE_EXPORT_SYMBOL(rte_lpm_rcu_qsbr_add)
313 : : int
314 : 5 : rte_lpm_rcu_qsbr_add(struct rte_lpm *lpm, struct rte_lpm_rcu_config *cfg)
315 : : {
316 : 5 : struct rte_rcu_qsbr_dq_parameters params = {0};
317 : : char rcu_dq_name[RTE_RCU_QSBR_DQ_NAMESIZE];
318 : : struct __rte_lpm *i_lpm;
319 : :
320 [ - + ]: 5 : if (lpm == NULL || cfg == NULL) {
321 : 0 : rte_errno = EINVAL;
322 : 0 : return 1;
323 : : }
324 : :
325 : : i_lpm = container_of(lpm, struct __rte_lpm, lpm);
326 [ + + ]: 5 : if (i_lpm->v != NULL) {
327 : 1 : rte_errno = EEXIST;
328 : 1 : return 1;
329 : : }
330 : :
331 [ + + ]: 4 : if (cfg->mode == RTE_LPM_QSBR_MODE_SYNC) {
332 : : /* No other things to do. */
333 [ + + ]: 3 : } else if (cfg->mode == RTE_LPM_QSBR_MODE_DQ) {
334 : : /* Init QSBR defer queue. */
335 [ - + ]: 2 : if (snprintf(rcu_dq_name, sizeof(rcu_dq_name), "LPM_RCU_%s", i_lpm->name)
336 : : >= (int)sizeof(rcu_dq_name))
337 : 0 : LPM_LOG(NOTICE, "LPM rcu defer queue name truncated to '%s'",
338 : : rcu_dq_name);
339 : :
340 : 2 : params.name = rcu_dq_name;
341 : 2 : params.size = cfg->dq_size;
342 [ + - ]: 2 : if (params.size == 0)
343 : 2 : params.size = i_lpm->number_tbl8s;
344 : 2 : params.trigger_reclaim_limit = cfg->reclaim_thd;
345 : 2 : params.max_reclaim_size = cfg->reclaim_max;
346 [ + - ]: 2 : if (params.max_reclaim_size == 0)
347 : 2 : params.max_reclaim_size = RTE_LPM_RCU_DQ_RECLAIM_MAX;
348 : 2 : params.esize = sizeof(uint32_t); /* tbl8 group index */
349 : 2 : params.free_fn = __lpm_rcu_qsbr_free_resource;
350 : 2 : params.p = i_lpm;
351 : 2 : params.v = cfg->v;
352 : 2 : i_lpm->dq = rte_rcu_qsbr_dq_create(¶ms);
353 [ - + ]: 2 : if (i_lpm->dq == NULL) {
354 : 0 : LPM_LOG(ERR, "LPM defer queue creation failed: %s",
355 : : rte_strerror(rte_errno));
356 : 0 : return 1;
357 : : }
358 : : } else {
359 : 1 : rte_errno = EINVAL;
360 : 1 : return 1;
361 : : }
362 : 3 : i_lpm->rcu_mode = cfg->mode;
363 : 3 : i_lpm->v = cfg->v;
364 : :
365 : 3 : return 0;
366 : : }
367 : :
368 : : /*
369 : : * Adds a rule to the rule table.
370 : : *
371 : : * NOTE: The rule table is split into 32 groups. Each group contains rules that
372 : : * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
373 : : * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
374 : : * to refer to depth 1 because even though the depth range is 1 - 32, depths
375 : : * are stored in the rule table from 0 - 31.
376 : : * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
377 : : */
378 : : static int32_t
379 : 3350 : rule_add(struct __rte_lpm *i_lpm, uint32_t ip_masked, uint8_t depth,
380 : : uint32_t next_hop)
381 : : {
382 : : uint32_t rule_gindex, rule_index, last_rule;
383 : : int i;
384 : :
385 : : VERIFY_DEPTH(depth);
386 : :
387 : : /* Scan through rule group to see if rule already exists. */
388 [ + + ]: 3350 : if (i_lpm->rule_info[depth - 1].used_rules > 0) {
389 : :
390 : : /* rule_gindex stands for rule group index. */
391 : 772 : rule_gindex = i_lpm->rule_info[depth - 1].first_rule;
392 : : /* Initialise rule_index to point to start of rule group. */
393 : : rule_index = rule_gindex;
394 : : /* Last rule = Last used rule in this rule group. */
395 : 772 : last_rule = rule_gindex + i_lpm->rule_info[depth - 1].used_rules;
396 : :
397 [ + + ]: 164997 : for (; rule_index < last_rule; rule_index++) {
398 : :
399 : : /* If rule already exists update next hop and return. */
400 [ + + ]: 164228 : if (i_lpm->rules_tbl[rule_index].ip == ip_masked) {
401 : :
402 [ + - ]: 3 : if (i_lpm->rules_tbl[rule_index].next_hop
403 : : == next_hop)
404 : : return -EEXIST;
405 : 3 : i_lpm->rules_tbl[rule_index].next_hop = next_hop;
406 : :
407 : 3 : return rule_index;
408 : : }
409 : : }
410 : :
411 [ + - ]: 769 : if (rule_index == i_lpm->max_rules)
412 : : return -ENOSPC;
413 : : } else {
414 : : /* Calculate the position in which the rule will be stored. */
415 : : rule_index = 0;
416 : :
417 [ + + ]: 55061 : for (i = depth - 1; i > 0; i--) {
418 [ + + ]: 53522 : if (i_lpm->rule_info[i - 1].used_rules > 0) {
419 : 1039 : rule_index = i_lpm->rule_info[i - 1].first_rule
420 : : + i_lpm->rule_info[i - 1].used_rules;
421 : 1039 : break;
422 : : }
423 : : }
424 [ + - ]: 2578 : if (rule_index == i_lpm->max_rules)
425 : : return -ENOSPC;
426 : :
427 : 2578 : i_lpm->rule_info[depth - 1].first_rule = rule_index;
428 : : }
429 : :
430 : : /* Make room for the new rule in the array. */
431 [ + + ]: 6608 : for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
432 : 3261 : if (i_lpm->rule_info[i - 1].first_rule
433 [ + - ]: 3261 : + i_lpm->rule_info[i - 1].used_rules == i_lpm->max_rules)
434 : : return -ENOSPC;
435 : :
436 [ + + ]: 3261 : if (i_lpm->rule_info[i - 1].used_rules > 0) {
437 : 4 : i_lpm->rules_tbl[i_lpm->rule_info[i - 1].first_rule
438 : 4 : + i_lpm->rule_info[i - 1].used_rules]
439 : 4 : = i_lpm->rules_tbl[i_lpm->rule_info[i - 1].first_rule];
440 : 4 : i_lpm->rule_info[i - 1].first_rule++;
441 : : }
442 : : }
443 : :
444 : : /* Add the new rule. */
445 : 3347 : i_lpm->rules_tbl[rule_index].ip = ip_masked;
446 : 3347 : i_lpm->rules_tbl[rule_index].next_hop = next_hop;
447 : :
448 : : /* Increment the used rules counter for this rule group. */
449 : 3347 : i_lpm->rule_info[depth - 1].used_rules++;
450 : :
451 : 3347 : return rule_index;
452 : : }
453 : :
454 : : /*
455 : : * Delete a rule from the rule table.
456 : : * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
457 : : */
458 : : static void
459 : 2570 : rule_delete(struct __rte_lpm *i_lpm, int32_t rule_index, uint8_t depth)
460 : : {
461 : : int i;
462 : :
463 : : VERIFY_DEPTH(depth);
464 : :
465 : 2570 : i_lpm->rules_tbl[rule_index] =
466 : 2570 : i_lpm->rules_tbl[i_lpm->rule_info[depth - 1].first_rule
467 : 2570 : + i_lpm->rule_info[depth - 1].used_rules - 1];
468 : :
469 [ + + ]: 5259 : for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
470 [ + + ]: 2689 : if (i_lpm->rule_info[i].used_rules > 0) {
471 : 2 : i_lpm->rules_tbl[i_lpm->rule_info[i].first_rule - 1] =
472 : 2 : i_lpm->rules_tbl[i_lpm->rule_info[i].first_rule
473 : 2 : + i_lpm->rule_info[i].used_rules - 1];
474 : 2 : i_lpm->rule_info[i].first_rule--;
475 : : }
476 : : }
477 : :
478 : 2570 : i_lpm->rule_info[depth - 1].used_rules--;
479 : 2570 : }
480 : :
481 : : /*
482 : : * Finds a rule in rule table.
483 : : * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
484 : : */
485 : : static int32_t
486 : : rule_find(struct __rte_lpm *i_lpm, uint32_t ip_masked, uint8_t depth)
487 : : {
488 : : uint32_t rule_gindex, last_rule, rule_index;
489 : :
490 : : VERIFY_DEPTH(depth);
491 : :
492 : 55914 : rule_gindex = i_lpm->rule_info[depth - 1].first_rule;
493 : 55914 : last_rule = rule_gindex + i_lpm->rule_info[depth - 1].used_rules;
494 : :
495 : : /* Scan used rules at given depth to find rule. */
496 [ + + + + : 55915 : for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
+ + ]
497 : : /* If rule is found return the rule index. */
498 [ + + + - : 3606 : if (i_lpm->rules_tbl[rule_index].ip == ip_masked)
+ - ]
499 : 3605 : return rule_index;
500 : : }
501 : :
502 : : /* If rule is not found return -EINVAL. */
503 : : return -EINVAL;
504 : : }
505 : :
506 : : /*
507 : : * Find, clean and allocate a tbl8.
508 : : */
509 : : static int32_t
510 : 3301 : _tbl8_alloc(struct __rte_lpm *i_lpm)
511 : : {
512 : : uint32_t group_idx; /* tbl8 group index. */
513 : : struct rte_lpm_tbl_entry *tbl8_entry;
514 : :
515 : : /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
516 [ + + ]: 167529 : for (group_idx = 0; group_idx < i_lpm->number_tbl8s; group_idx++) {
517 : 167524 : tbl8_entry = &i_lpm->lpm.tbl8[group_idx *
518 : : RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
519 : : /* If a free tbl8 group is found clean it and set as VALID. */
520 [ + + ]: 167524 : if (!tbl8_entry->valid_group) {
521 : : struct rte_lpm_tbl_entry new_tbl8_entry = {
522 : : .next_hop = 0,
523 : : .valid = INVALID,
524 : : .depth = 0,
525 : : .valid_group = VALID,
526 : : };
527 : :
528 : : memset(&tbl8_entry[0], 0,
529 : : RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
530 : : sizeof(tbl8_entry[0]));
531 : :
532 : 3296 : rte_atomic_store_explicit(&tbl8_entry->val, new_tbl8_entry.val,
533 : : rte_memory_order_relaxed);
534 : :
535 : : /* Return group index for allocated tbl8 group. */
536 : 3296 : return group_idx;
537 : : }
538 : : }
539 : :
540 : : /* If there are no tbl8 groups free then return error. */
541 : : return -ENOSPC;
542 : : }
543 : :
544 : : static int32_t
545 : 3299 : tbl8_alloc(struct __rte_lpm *i_lpm)
546 : : {
547 : : int32_t group_idx; /* tbl8 group index. */
548 : :
549 : 3299 : group_idx = _tbl8_alloc(i_lpm);
550 [ + + + + ]: 3299 : if (group_idx == -ENOSPC && i_lpm->dq != NULL) {
551 : : /* If there are no tbl8 groups try to reclaim one. */
552 [ + - ]: 2 : if (rte_rcu_qsbr_dq_reclaim(i_lpm->dq, 1,
553 : : NULL, NULL, NULL) == 0)
554 : 2 : group_idx = _tbl8_alloc(i_lpm);
555 : : }
556 : :
557 : 3299 : return group_idx;
558 : : }
559 : :
560 : : static int32_t
561 : 2523 : tbl8_free(struct __rte_lpm *i_lpm, uint32_t tbl8_group_start)
562 : : {
563 : : struct rte_lpm_tbl_entry zero_tbl8_entry = {0};
564 : : int status;
565 : :
566 [ + + ]: 2523 : if (i_lpm->v == NULL) {
567 : : /* Set tbl8 group invalid*/
568 : 2010 : struct rte_lpm_tbl_entry *tbl8_entry =
569 : 2010 : &i_lpm->lpm.tbl8[tbl8_group_start];
570 : 2010 : rte_atomic_store_explicit(&tbl8_entry->val, zero_tbl8_entry.val,
571 : : rte_memory_order_relaxed);
572 [ + + ]: 513 : } else if (i_lpm->rcu_mode == RTE_LPM_QSBR_MODE_SYNC) {
573 : : /* Wait for quiescent state change. */
574 : 512 : rte_rcu_qsbr_synchronize(i_lpm->v,
575 : : RTE_QSBR_THRID_INVALID);
576 : : /* Set tbl8 group invalid*/
577 : 512 : struct rte_lpm_tbl_entry *tbl8_entry =
578 : 512 : &i_lpm->lpm.tbl8[tbl8_group_start];
579 : 512 : rte_atomic_store_explicit(&tbl8_entry->val, zero_tbl8_entry.val,
580 : : rte_memory_order_relaxed);
581 [ + - ]: 1 : } else if (i_lpm->rcu_mode == RTE_LPM_QSBR_MODE_DQ) {
582 : : /* Push into QSBR defer queue. */
583 : 1 : status = rte_rcu_qsbr_dq_enqueue(i_lpm->dq,
584 : : (void *)&tbl8_group_start);
585 [ - + ]: 1 : if (status == 1) {
586 : 0 : LPM_LOG(ERR, "Failed to push QSBR FIFO");
587 : 0 : return -rte_errno;
588 : : }
589 : : }
590 : :
591 : : return 0;
592 : : }
593 : :
594 : : static __rte_noinline int32_t
595 : 41 : add_depth_small(struct __rte_lpm *i_lpm, uint32_t ip, uint8_t depth,
596 : : uint32_t next_hop)
597 : : {
598 : : #define group_idx next_hop
599 : : uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
600 : :
601 : : /* Calculate the index into Table24. */
602 : 41 : tbl24_index = ip >> 8;
603 [ + - ]: 41 : tbl24_range = depth_to_range(depth);
604 : :
605 [ + + ]: 16778550 : for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
606 : : /*
607 : : * For invalid OR valid and non-extended tbl 24 entries set
608 : : * entry.
609 : : */
610 [ + + + + ]: 16778509 : if (!i_lpm->lpm.tbl24[i].valid || (i_lpm->lpm.tbl24[i].valid_group == 0 &&
611 [ + - ]: 8388865 : i_lpm->lpm.tbl24[i].depth <= depth)) {
612 : :
613 : 16778506 : struct rte_lpm_tbl_entry new_tbl24_entry = {
614 : : .next_hop = next_hop,
615 : : .valid = VALID,
616 : : .valid_group = 0,
617 : : .depth = depth,
618 : : };
619 : :
620 : : /* Setting tbl24 entry in one go to avoid race
621 : : * conditions
622 : : */
623 : : struct rte_lpm_tbl_entry *tbl24_entry =
624 : : &i_lpm->lpm.tbl24[i];
625 : 16778506 : rte_atomic_store_explicit(&tbl24_entry->val, new_tbl24_entry.val,
626 : : rte_memory_order_release);
627 : :
628 : : continue;
629 : : }
630 : :
631 [ + - ]: 3 : if (i_lpm->lpm.tbl24[i].valid_group == 1) {
632 : : /* If tbl24 entry is valid and extended calculate the
633 : : * index into tbl8.
634 : : */
635 : 3 : tbl8_index = i_lpm->lpm.tbl24[i].group_idx *
636 : : RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
637 : 3 : tbl8_group_end = tbl8_index +
638 : : RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
639 : :
640 [ + + ]: 771 : for (j = tbl8_index; j < tbl8_group_end; j++) {
641 [ + + ]: 768 : if (!i_lpm->lpm.tbl8[j].valid ||
642 [ + + ]: 273 : i_lpm->lpm.tbl8[j].depth <= depth) {
643 : : struct rte_lpm_tbl_entry
644 : 735 : new_tbl8_entry = {
645 : : .valid = VALID,
646 : : .valid_group = VALID,
647 : : .depth = depth,
648 : : .next_hop = next_hop,
649 : : };
650 : :
651 : : /*
652 : : * Setting tbl8 entry in one go to avoid
653 : : * race conditions
654 : : */
655 : : struct rte_lpm_tbl_entry *tbl8_entry =
656 : : &i_lpm->lpm.tbl8[j];
657 : 735 : rte_atomic_store_explicit(&tbl8_entry->val,
658 : : new_tbl8_entry.val,
659 : : rte_memory_order_relaxed);
660 : :
661 : : continue;
662 : : }
663 : : }
664 : : }
665 : : }
666 : : #undef group_idx
667 : 41 : return 0;
668 : : }
669 : :
670 : : static __rte_noinline int32_t
671 : 3309 : add_depth_big(struct __rte_lpm *i_lpm, uint32_t ip_masked, uint8_t depth,
672 : : uint32_t next_hop)
673 : : {
674 : : #define group_idx next_hop
675 : : uint32_t tbl24_index;
676 : : int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
677 : : tbl8_range, i;
678 : :
679 : 3309 : tbl24_index = (ip_masked >> 8);
680 [ - + ]: 3309 : tbl8_range = depth_to_range(depth);
681 : :
682 [ + + ]: 3309 : if (!i_lpm->lpm.tbl24[tbl24_index].valid) {
683 : : /* Search for a free tbl8 group. */
684 : 2294 : tbl8_group_index = tbl8_alloc(i_lpm);
685 : :
686 : : /* Check tbl8 allocation was successful. */
687 [ + + ]: 2294 : if (tbl8_group_index < 0) {
688 : 3 : return tbl8_group_index;
689 : : }
690 : :
691 : : /* Find index into tbl8 and range. */
692 : 2291 : tbl8_index = (tbl8_group_index *
693 : 2291 : RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
694 : 2291 : (ip_masked & 0xFF);
695 : :
696 : : /* Set tbl8 entry. */
697 [ + + ]: 13471 : for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
698 : 11180 : struct rte_lpm_tbl_entry new_tbl8_entry = {
699 : : .valid = VALID,
700 : : .depth = depth,
701 : 11180 : .valid_group = i_lpm->lpm.tbl8[i].valid_group,
702 : : .next_hop = next_hop,
703 : : };
704 : : struct rte_lpm_tbl_entry *tbl8_entry =
705 : : &i_lpm->lpm.tbl8[i];
706 : 11180 : rte_atomic_store_explicit(&tbl8_entry->val, new_tbl8_entry.val,
707 : : rte_memory_order_relaxed);
708 : : }
709 : :
710 : : /*
711 : : * Update tbl24 entry to point to new tbl8 entry. Note: The
712 : : * ext_flag and tbl8_index need to be updated simultaneously,
713 : : * so assign whole structure in one go
714 : : */
715 : :
716 : 2291 : struct rte_lpm_tbl_entry new_tbl24_entry = {
717 : : .group_idx = tbl8_group_index,
718 : : .valid = VALID,
719 : : .valid_group = 1,
720 : : .depth = 0,
721 : : };
722 : :
723 : : /* The tbl24 entry must be written only after the
724 : : * tbl8 entries are written.
725 : : */
726 : : struct rte_lpm_tbl_entry *tbl24_entry =
727 : : &i_lpm->lpm.tbl24[tbl24_index];
728 : 2291 : rte_atomic_store_explicit(&tbl24_entry->val, new_tbl24_entry.val,
729 : : rte_memory_order_release);
730 : :
731 : : } /* If valid entry but not extended calculate the index into Table8. */
732 [ + + ]: 1015 : else if (i_lpm->lpm.tbl24[tbl24_index].valid_group == 0) {
733 : : /* Search for free tbl8 group. */
734 : 1005 : tbl8_group_index = tbl8_alloc(i_lpm);
735 : :
736 [ - + ]: 1005 : if (tbl8_group_index < 0) {
737 : 0 : return tbl8_group_index;
738 : : }
739 : :
740 : 1005 : tbl8_group_start = tbl8_group_index *
741 : : RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
742 : 1005 : tbl8_group_end = tbl8_group_start +
743 : : RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
744 : :
745 : : /* Populate new tbl8 with tbl24 value. */
746 [ + + ]: 258285 : for (i = tbl8_group_start; i < tbl8_group_end; i++) {
747 : 257280 : struct rte_lpm_tbl_entry new_tbl8_entry = {
748 : : .valid = VALID,
749 : 257280 : .depth = i_lpm->lpm.tbl24[tbl24_index].depth,
750 : 257280 : .valid_group = i_lpm->lpm.tbl8[i].valid_group,
751 : 257280 : .next_hop = i_lpm->lpm.tbl24[tbl24_index].next_hop,
752 : : };
753 : : struct rte_lpm_tbl_entry *tbl8_entry =
754 : : &i_lpm->lpm.tbl8[i];
755 : 257280 : rte_atomic_store_explicit(&tbl8_entry->val, new_tbl8_entry.val,
756 : : rte_memory_order_relaxed);
757 : : }
758 : :
759 : 1005 : tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
760 : :
761 : : /* Insert new rule into the tbl8 entry. */
762 [ + + ]: 2167 : for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
763 : 1162 : struct rte_lpm_tbl_entry new_tbl8_entry = {
764 : : .valid = VALID,
765 : : .depth = depth,
766 : 1162 : .valid_group = i_lpm->lpm.tbl8[i].valid_group,
767 : : .next_hop = next_hop,
768 : : };
769 : : struct rte_lpm_tbl_entry *tbl8_entry =
770 : : &i_lpm->lpm.tbl8[i];
771 : 1162 : rte_atomic_store_explicit(&tbl8_entry->val, new_tbl8_entry.val,
772 : : rte_memory_order_relaxed);
773 : : }
774 : :
775 : : /*
776 : : * Update tbl24 entry to point to new tbl8 entry. Note: The
777 : : * ext_flag and tbl8_index need to be updated simultaneously,
778 : : * so assign whole structure in one go.
779 : : */
780 : :
781 : 1005 : struct rte_lpm_tbl_entry new_tbl24_entry = {
782 : : .group_idx = tbl8_group_index,
783 : : .valid = VALID,
784 : : .valid_group = 1,
785 : : .depth = 0,
786 : : };
787 : :
788 : : /* The tbl24 entry must be written only after the
789 : : * tbl8 entries are written.
790 : : */
791 : : struct rte_lpm_tbl_entry *tbl24_entry =
792 : : &i_lpm->lpm.tbl24[tbl24_index];
793 : 1005 : rte_atomic_store_explicit(&tbl24_entry->val, new_tbl24_entry.val,
794 : : rte_memory_order_release);
795 : :
796 : : } else { /*
797 : : * If it is valid, extended entry calculate the index into tbl8.
798 : : */
799 : 10 : tbl8_group_index = i_lpm->lpm.tbl24[tbl24_index].group_idx;
800 : 10 : tbl8_group_start = tbl8_group_index *
801 : : RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
802 : 10 : tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
803 : :
804 [ + + ]: 140 : for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
805 : :
806 [ + + ]: 130 : if (!i_lpm->lpm.tbl8[i].valid ||
807 [ + - ]: 129 : i_lpm->lpm.tbl8[i].depth <= depth) {
808 : 130 : struct rte_lpm_tbl_entry new_tbl8_entry = {
809 : : .valid = VALID,
810 : : .depth = depth,
811 : : .next_hop = next_hop,
812 : 130 : .valid_group = i_lpm->lpm.tbl8[i].valid_group,
813 : : };
814 : :
815 : : /*
816 : : * Setting tbl8 entry in one go to avoid race
817 : : * condition
818 : : */
819 : : struct rte_lpm_tbl_entry *tbl8_entry =
820 : : &i_lpm->lpm.tbl8[i];
821 : 130 : rte_atomic_store_explicit(&tbl8_entry->val,
822 : : new_tbl8_entry.val, rte_memory_order_relaxed);
823 : :
824 : : continue;
825 : : }
826 : : }
827 : : }
828 : : #undef group_idx
829 : : return 0;
830 : : }
831 : :
832 : : /*
833 : : * Add a route
834 : : */
835 : : RTE_EXPORT_SYMBOL(rte_lpm_add)
836 : : int
837 : 3353 : rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
838 : : uint32_t next_hop)
839 : : {
840 : : int32_t rule_index, status = 0;
841 : : struct __rte_lpm *i_lpm;
842 : : uint32_t ip_masked;
843 : :
844 : : /* Check user arguments. */
845 [ + + + + ]: 3353 : if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
846 : : return -EINVAL;
847 : :
848 : : i_lpm = container_of(lpm, struct __rte_lpm, lpm);
849 : 3350 : ip_masked = ip & depth_to_mask(depth);
850 : :
851 : : /* Add the rule to the rule table. */
852 : 3350 : rule_index = rule_add(i_lpm, ip_masked, depth, next_hop);
853 : :
854 : : /* Skip table entries update if The rule is the same as
855 : : * the rule in the rules table.
856 : : */
857 [ + - ]: 3350 : if (rule_index == -EEXIST)
858 : : return 0;
859 : :
860 : : /* If the is no space available for new rule return error. */
861 [ + - ]: 3350 : if (rule_index < 0) {
862 : : return rule_index;
863 : : }
864 : :
865 [ + + ]: 3350 : if (depth <= MAX_DEPTH_TBL24) {
866 : 41 : status = add_depth_small(i_lpm, ip_masked, depth, next_hop);
867 : : } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
868 : 3309 : status = add_depth_big(i_lpm, ip_masked, depth, next_hop);
869 : :
870 : : /*
871 : : * If add fails due to exhaustion of tbl8 extensions delete
872 : : * rule that was added to rule table.
873 : : */
874 [ + + ]: 3309 : if (status < 0) {
875 : 3 : rule_delete(i_lpm, rule_index, depth);
876 : :
877 : 3 : return status;
878 : : }
879 : : }
880 : :
881 : : return 0;
882 : : }
883 : :
884 : : /*
885 : : * Look for a rule in the high-level rules table
886 : : */
887 : : RTE_EXPORT_SYMBOL(rte_lpm_is_rule_present)
888 : : int
889 : 7 : rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
890 : : uint32_t *next_hop)
891 : : {
892 : : struct __rte_lpm *i_lpm;
893 : : uint32_t ip_masked;
894 : : int32_t rule_index;
895 : :
896 : : /* Check user arguments. */
897 : 7 : if ((lpm == NULL) ||
898 [ + - ]: 7 : (next_hop == NULL) ||
899 [ + - ]: 7 : (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
900 : : return -EINVAL;
901 : :
902 : : /* Look for the rule using rule_find. */
903 : : i_lpm = container_of(lpm, struct __rte_lpm, lpm);
904 : 7 : ip_masked = ip & depth_to_mask(depth);
905 : : rule_index = rule_find(i_lpm, ip_masked, depth);
906 : :
907 [ + + ]: 7 : if (rule_index >= 0) {
908 : 3 : *next_hop = i_lpm->rules_tbl[rule_index].next_hop;
909 : 3 : return 1;
910 : : }
911 : :
912 : : /* If rule is not found return 0. */
913 : : return 0;
914 : : }
915 : :
916 : : static int32_t
917 : 2567 : find_previous_rule(struct __rte_lpm *i_lpm, uint32_t ip, uint8_t depth,
918 : : uint8_t *sub_rule_depth)
919 : : {
920 : : int32_t rule_index;
921 : : uint32_t ip_masked;
922 : : uint8_t prev_depth;
923 : :
924 [ + + ]: 54870 : for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
925 : 53338 : ip_masked = ip & depth_to_mask(prev_depth);
926 : :
927 : : rule_index = rule_find(i_lpm, ip_masked, prev_depth);
928 : :
929 [ + + ]: 53338 : if (rule_index >= 0) {
930 : 1035 : *sub_rule_depth = prev_depth;
931 : 1035 : return rule_index;
932 : : }
933 : : }
934 : :
935 : : return -1;
936 : : }
937 : :
938 : : static int32_t
939 : 35 : delete_depth_small(struct __rte_lpm *i_lpm, uint32_t ip_masked,
940 : : uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
941 : : {
942 : : #define group_idx next_hop
943 : : uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
944 : :
945 : : /* Calculate the range and index into Table24. */
946 [ + - ]: 35 : tbl24_range = depth_to_range(depth);
947 : 35 : tbl24_index = (ip_masked >> 8);
948 : : struct rte_lpm_tbl_entry zero_tbl24_entry = {0};
949 : :
950 : : /*
951 : : * Firstly check the sub_rule_index. A -1 indicates no replacement rule
952 : : * and a positive number indicates a sub_rule_index.
953 : : */
954 [ + + ]: 35 : if (sub_rule_index < 0) {
955 : : /*
956 : : * If no replacement rule exists then invalidate entries
957 : : * associated with this rule.
958 : : */
959 [ + + ]: 8389395 : for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
960 : :
961 [ + + ]: 8389384 : if (i_lpm->lpm.tbl24[i].valid_group == 0 &&
962 [ + - ]: 8389382 : i_lpm->lpm.tbl24[i].depth <= depth) {
963 : : struct rte_lpm_tbl_entry *tbl24_entry =
964 : : &i_lpm->lpm.tbl24[i];
965 : 8389382 : rte_atomic_store_explicit(&tbl24_entry->val,
966 : : zero_tbl24_entry.val, rte_memory_order_release);
967 [ + - ]: 2 : } else if (i_lpm->lpm.tbl24[i].valid_group == 1) {
968 : : /*
969 : : * If TBL24 entry is extended, then there has
970 : : * to be a rule with depth >= 25 in the
971 : : * associated TBL8 group.
972 : : */
973 : :
974 : 2 : tbl8_group_index = i_lpm->lpm.tbl24[i].group_idx;
975 : 2 : tbl8_index = tbl8_group_index *
976 : : RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
977 : :
978 [ + + ]: 514 : for (j = tbl8_index; j < (tbl8_index +
979 : 512 : RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
980 : :
981 [ + + ]: 512 : if (i_lpm->lpm.tbl8[j].depth <= depth)
982 : 510 : i_lpm->lpm.tbl8[j].valid = INVALID;
983 : : }
984 : : }
985 : : }
986 : : } else {
987 : : /*
988 : : * If a replacement rule exists then modify entries
989 : : * associated with this rule.
990 : : */
991 : :
992 : 24 : struct rte_lpm_tbl_entry new_tbl24_entry = {
993 : 24 : .next_hop = i_lpm->rules_tbl[sub_rule_index].next_hop,
994 : : .valid = VALID,
995 : : .valid_group = 0,
996 : : .depth = sub_rule_depth,
997 : : };
998 : :
999 : 24 : struct rte_lpm_tbl_entry new_tbl8_entry = {
1000 : : .valid = VALID,
1001 : : .valid_group = VALID,
1002 : : .depth = sub_rule_depth,
1003 : : .next_hop = i_lpm->rules_tbl
1004 : : [sub_rule_index].next_hop,
1005 : : };
1006 : :
1007 [ + + ]: 8388632 : for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1008 : :
1009 [ + - ]: 8388608 : if (i_lpm->lpm.tbl24[i].valid_group == 0 &&
1010 [ + - ]: 8388608 : i_lpm->lpm.tbl24[i].depth <= depth) {
1011 : : struct rte_lpm_tbl_entry *tbl24_entry =
1012 : : &i_lpm->lpm.tbl24[i];
1013 : 8388608 : rte_atomic_store_explicit(&tbl24_entry->val,
1014 : : new_tbl24_entry.val, rte_memory_order_release);
1015 [ # # ]: 0 : } else if (i_lpm->lpm.tbl24[i].valid_group == 1) {
1016 : : /*
1017 : : * If TBL24 entry is extended, then there has
1018 : : * to be a rule with depth >= 25 in the
1019 : : * associated TBL8 group.
1020 : : */
1021 : :
1022 : 0 : tbl8_group_index = i_lpm->lpm.tbl24[i].group_idx;
1023 : 0 : tbl8_index = tbl8_group_index *
1024 : : RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1025 : :
1026 [ # # ]: 0 : for (j = tbl8_index; j < (tbl8_index +
1027 : 0 : RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1028 : :
1029 [ # # ]: 0 : if (i_lpm->lpm.tbl8[j].depth <= depth) {
1030 : : struct rte_lpm_tbl_entry *tbl8_entry =
1031 : : &i_lpm->lpm.tbl8[j];
1032 : 0 : rte_atomic_store_explicit(&tbl8_entry->val,
1033 : : new_tbl8_entry.val,
1034 : : rte_memory_order_relaxed);
1035 : : }
1036 : : }
1037 : : }
1038 : : }
1039 : : }
1040 : : #undef group_idx
1041 : 35 : return 0;
1042 : : }
1043 : :
1044 : : /*
1045 : : * Checks if table 8 group can be recycled.
1046 : : *
1047 : : * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
1048 : : * Return of -EINVAL means tbl8 is empty and thus can be recycled
1049 : : * Return of value > -1 means tbl8 is in use but has all the same values and
1050 : : * thus can be recycled
1051 : : */
1052 : : static int32_t
1053 : 2532 : tbl8_recycle_check(struct rte_lpm_tbl_entry *tbl8,
1054 : : uint32_t tbl8_group_start)
1055 : : {
1056 : : uint32_t tbl8_group_end, i;
1057 : 2532 : tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1058 : :
1059 : : /*
1060 : : * Check the first entry of the given tbl8. If it is invalid we know
1061 : : * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1062 : : * (As they would affect all entries in a tbl8) and thus this table
1063 : : * can not be recycled.
1064 : : */
1065 [ + + ]: 2532 : if (tbl8[tbl8_group_start].valid) {
1066 : : /*
1067 : : * If first entry is valid check if the depth is less than 24
1068 : : * and if so check the rest of the entries to verify that they
1069 : : * are all of this depth.
1070 : : */
1071 [ + + ]: 1012 : if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
1072 [ + + ]: 256768 : for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1073 : 255765 : i++) {
1074 : :
1075 [ + - ]: 255765 : if (tbl8[i].depth !=
1076 : : tbl8[tbl8_group_start].depth) {
1077 : :
1078 : : return -EEXIST;
1079 : : }
1080 : : }
1081 : : /* If all entries are the same return the tb8 index */
1082 : 1003 : return tbl8_group_start;
1083 : : }
1084 : :
1085 : : return -EEXIST;
1086 : : }
1087 : : /*
1088 : : * If the first entry is invalid check if the rest of the entries in
1089 : : * the tbl8 are invalid.
1090 : : */
1091 [ + + ]: 389120 : for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1092 [ + - ]: 387600 : if (tbl8[i].valid)
1093 : : return -EEXIST;
1094 : : }
1095 : : /* If no valid entries are found then return -EINVAL. */
1096 : : return -EINVAL;
1097 : : }
1098 : :
1099 : : static int32_t
1100 : 2532 : delete_depth_big(struct __rte_lpm *i_lpm, uint32_t ip_masked,
1101 : : uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1102 : : {
1103 : : #define group_idx next_hop
1104 : : uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1105 : : tbl8_range, i;
1106 : : int32_t tbl8_recycle_index, status = 0;
1107 : :
1108 : : /*
1109 : : * Calculate the index into tbl24 and range. Note: All depths larger
1110 : : * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1111 : : */
1112 : 2532 : tbl24_index = ip_masked >> 8;
1113 : :
1114 : : /* Calculate the index into tbl8 and range. */
1115 : 2532 : tbl8_group_index = i_lpm->lpm.tbl24[tbl24_index].group_idx;
1116 : 2532 : tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1117 : 2532 : tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1118 [ - + ]: 2532 : tbl8_range = depth_to_range(depth);
1119 : :
1120 [ + + ]: 2532 : if (sub_rule_index < 0) {
1121 : : /*
1122 : : * Loop through the range of entries on tbl8 for which the
1123 : : * rule_to_delete must be removed or modified.
1124 : : */
1125 [ + + ]: 10991 : for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1126 [ + - ]: 9470 : if (i_lpm->lpm.tbl8[i].depth <= depth)
1127 : 9470 : i_lpm->lpm.tbl8[i].valid = INVALID;
1128 : : }
1129 : : } else {
1130 : : /* Set new tbl8 entry. */
1131 : 1011 : struct rte_lpm_tbl_entry new_tbl8_entry = {
1132 : : .valid = VALID,
1133 : : .depth = sub_rule_depth,
1134 : 1011 : .valid_group = i_lpm->lpm.tbl8[tbl8_group_start].valid_group,
1135 : 1011 : .next_hop = i_lpm->rules_tbl[sub_rule_index].next_hop,
1136 : : };
1137 : :
1138 : : /*
1139 : : * Loop through the range of entries on tbl8 for which the
1140 : : * rule_to_delete must be modified.
1141 : : */
1142 [ + + ]: 2299 : for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1143 [ + - ]: 1288 : if (i_lpm->lpm.tbl8[i].depth <= depth) {
1144 : : struct rte_lpm_tbl_entry *tbl8_entry =
1145 : : &i_lpm->lpm.tbl8[i];
1146 : 1288 : rte_atomic_store_explicit(&tbl8_entry->val,
1147 : : new_tbl8_entry.val, rte_memory_order_relaxed);
1148 : : }
1149 : : }
1150 : : }
1151 : :
1152 : : /*
1153 : : * Check if there are any valid entries in this tbl8 group. If all
1154 : : * tbl8 entries are invalid we can free the tbl8 and invalidate the
1155 : : * associated tbl24 entry.
1156 : : */
1157 : :
1158 : 2532 : tbl8_recycle_index = tbl8_recycle_check(i_lpm->lpm.tbl8, tbl8_group_start);
1159 : :
1160 [ + + ]: 2532 : if (tbl8_recycle_index == -EINVAL) {
1161 : : /* Set tbl24 before freeing tbl8 to avoid race condition.
1162 : : * Prevent the free of the tbl8 group from hoisting.
1163 : : */
1164 : 1520 : i_lpm->lpm.tbl24[tbl24_index].valid = 0;
1165 : : rte_atomic_thread_fence(rte_memory_order_release);
1166 : 1520 : status = tbl8_free(i_lpm, tbl8_group_start);
1167 [ + + ]: 1012 : } else if (tbl8_recycle_index > -1) {
1168 : : /* Update tbl24 entry. */
1169 : 1003 : struct rte_lpm_tbl_entry new_tbl24_entry = {
1170 : 1003 : .next_hop = i_lpm->lpm.tbl8[tbl8_recycle_index].next_hop,
1171 : : .valid = VALID,
1172 : : .valid_group = 0,
1173 : 1003 : .depth = i_lpm->lpm.tbl8[tbl8_recycle_index].depth,
1174 : : };
1175 : :
1176 : : /* Set tbl24 before freeing tbl8 to avoid race condition.
1177 : : * Prevent the free of the tbl8 group from hoisting.
1178 : : */
1179 : : struct rte_lpm_tbl_entry *tbl24_entry =
1180 : : &i_lpm->lpm.tbl24[tbl24_index];
1181 : 1003 : rte_atomic_store_explicit(&tbl24_entry->val, new_tbl24_entry.val,
1182 : : rte_memory_order_relaxed);
1183 : : rte_atomic_thread_fence(rte_memory_order_release);
1184 : 1003 : status = tbl8_free(i_lpm, tbl8_group_start);
1185 : : }
1186 : : #undef group_idx
1187 : 2532 : return status;
1188 : : }
1189 : :
1190 : : /*
1191 : : * Deletes a rule
1192 : : */
1193 : : RTE_EXPORT_SYMBOL(rte_lpm_delete)
1194 : : int
1195 : 2572 : rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
1196 : : {
1197 : : int32_t rule_to_delete_index, sub_rule_index;
1198 : : struct __rte_lpm *i_lpm;
1199 : : uint32_t ip_masked;
1200 : : uint8_t sub_rule_depth;
1201 : : /*
1202 : : * Check input arguments. Note: IP must be a positive integer of 32
1203 : : * bits in length therefore it need not be checked.
1204 : : */
1205 [ + + + + ]: 2572 : if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1206 : : return -EINVAL;
1207 : : }
1208 : :
1209 : : i_lpm = container_of(lpm, struct __rte_lpm, lpm);
1210 : 2569 : ip_masked = ip & depth_to_mask(depth);
1211 : :
1212 : : /*
1213 : : * Find the index of the input rule, that needs to be deleted, in the
1214 : : * rule table.
1215 : : */
1216 : : rule_to_delete_index = rule_find(i_lpm, ip_masked, depth);
1217 : :
1218 : : /*
1219 : : * Check if rule_to_delete_index was found. If no rule was found the
1220 : : * function rule_find returns -EINVAL.
1221 : : */
1222 [ + + ]: 2569 : if (rule_to_delete_index < 0)
1223 : : return -EINVAL;
1224 : :
1225 : : /* Delete the rule from the rule table. */
1226 : 2567 : rule_delete(i_lpm, rule_to_delete_index, depth);
1227 : :
1228 : : /*
1229 : : * Find rule to replace the rule_to_delete. If there is no rule to
1230 : : * replace the rule_to_delete we return -1 and invalidate the table
1231 : : * entries associated with this rule.
1232 : : */
1233 : 2567 : sub_rule_depth = 0;
1234 : 2567 : sub_rule_index = find_previous_rule(i_lpm, ip, depth, &sub_rule_depth);
1235 : :
1236 : : /*
1237 : : * If the input depth value is less than 25 use function
1238 : : * delete_depth_small otherwise use delete_depth_big.
1239 : : */
1240 [ + + ]: 2567 : if (depth <= MAX_DEPTH_TBL24) {
1241 : 35 : return delete_depth_small(i_lpm, ip_masked, depth,
1242 : : sub_rule_index, sub_rule_depth);
1243 : : } else { /* If depth > MAX_DEPTH_TBL24 */
1244 : 2532 : return delete_depth_big(i_lpm, ip_masked, depth, sub_rule_index,
1245 : : sub_rule_depth);
1246 : : }
1247 : : }
1248 : :
1249 : : /*
1250 : : * Delete all rules from the LPM table.
1251 : : */
1252 : : RTE_EXPORT_SYMBOL(rte_lpm_delete_all)
1253 : : void
1254 : 10 : rte_lpm_delete_all(struct rte_lpm *lpm)
1255 : : {
1256 : : struct __rte_lpm *i_lpm;
1257 : :
1258 : : i_lpm = container_of(lpm, struct __rte_lpm, lpm);
1259 : : /* Zero rule information. */
1260 : 10 : memset(i_lpm->rule_info, 0, sizeof(i_lpm->rule_info));
1261 : :
1262 : : /* Zero tbl24. */
1263 : 10 : memset(i_lpm->lpm.tbl24, 0, sizeof(i_lpm->lpm.tbl24));
1264 : :
1265 : : /* Zero tbl8. */
1266 : 10 : memset(i_lpm->lpm.tbl8, 0, sizeof(i_lpm->lpm.tbl8[0])
1267 : 10 : * RTE_LPM_TBL8_GROUP_NUM_ENTRIES * i_lpm->number_tbl8s);
1268 : :
1269 : : /* Delete all rules form the rules table. */
1270 : 10 : memset(i_lpm->rules_tbl, 0, sizeof(i_lpm->rules_tbl[0]) * i_lpm->max_rules);
1271 : 10 : }
|