Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : : #include <string.h>
5 : : #include <stdalign.h>
6 : : #include <stdint.h>
7 : : #include <errno.h>
8 : : #include <stdio.h>
9 : : #include <sys/queue.h>
10 : :
11 : : #include <eal_export.h>
12 : : #include <rte_log.h>
13 : : #include <rte_common.h>
14 : : #include <rte_malloc.h>
15 : : #include <rte_memcpy.h>
16 : : #include <rte_eal_memconfig.h>
17 : : #include <rte_string_fns.h>
18 : : #include <rte_errno.h>
19 : : #include <rte_hash.h>
20 : : #include <assert.h>
21 : : #include <rte_jhash.h>
22 : : #include <rte_tailq.h>
23 : :
24 : : #include "rte_lpm6.h"
25 : : #include "lpm_log.h"
26 : :
27 : : #define RTE_LPM6_TBL24_NUM_ENTRIES (1 << 24)
28 : : #define RTE_LPM6_TBL8_GROUP_NUM_ENTRIES 256
29 : : #define RTE_LPM6_TBL8_MAX_NUM_GROUPS (1 << 21)
30 : :
31 : : #define RTE_LPM6_VALID_EXT_ENTRY_BITMASK 0xA0000000
32 : : #define RTE_LPM6_LOOKUP_SUCCESS 0x20000000
33 : : #define RTE_LPM6_TBL8_BITMASK 0x001FFFFF
34 : :
35 : : #define ADD_FIRST_BYTE 3
36 : : #define LOOKUP_FIRST_BYTE 4
37 : : #define BYTE_SIZE 8
38 : : #define BYTES2_SIZE 16
39 : :
40 : : #define RULE_HASH_TABLE_EXTRA_SPACE 64
41 : : #define TBL24_IND UINT32_MAX
42 : :
43 : : #define lpm6_tbl8_gindex next_hop
44 : :
45 : : /** Flags for setting an entry as valid/invalid. */
46 : : enum valid_flag {
47 : : INVALID = 0,
48 : : VALID
49 : : };
50 : :
51 : : TAILQ_HEAD(rte_lpm6_list, rte_tailq_entry);
52 : :
53 : : static struct rte_tailq_elem rte_lpm6_tailq = {
54 : : .name = "RTE_LPM6",
55 : : };
56 [ - + ]: 252 : EAL_REGISTER_TAILQ(rte_lpm6_tailq)
57 : :
58 : : /** Tbl entry structure. It is the same for both tbl24 and tbl8 */
59 : : struct rte_lpm6_tbl_entry {
60 : : uint32_t next_hop: 21; /**< Next hop / next table to be checked. */
61 : : uint32_t depth :8; /**< Rule depth. */
62 : :
63 : : /* Flags. */
64 : : uint32_t valid :1; /**< Validation flag. */
65 : : uint32_t valid_group :1; /**< Group validation flag. */
66 : : uint32_t ext_entry :1; /**< External entry. */
67 : : };
68 : :
69 : : /** Rules tbl entry structure. */
70 : : struct rte_lpm6_rule {
71 : : struct rte_ipv6_addr ip; /**< Rule IP address. */
72 : : uint32_t next_hop; /**< Rule next hop. */
73 : : uint8_t depth; /**< Rule depth. */
74 : : };
75 : :
76 : : /** Rules tbl entry key. */
77 : : struct rte_lpm6_rule_key {
78 : : struct rte_ipv6_addr ip; /**< Rule IP address. */
79 : : uint32_t depth; /**< Rule depth. */
80 : : };
81 : :
82 : : /* Header of tbl8 */
83 : : struct rte_lpm_tbl8_hdr {
84 : : uint32_t owner_tbl_ind; /**< owner table: TBL24_IND if owner is tbl24,
85 : : * otherwise index of tbl8
86 : : */
87 : : uint32_t owner_entry_ind; /**< index of the owner table entry where
88 : : * pointer to the tbl8 is stored
89 : : */
90 : : uint32_t ref_cnt; /**< table reference counter */
91 : : };
92 : :
93 : : /** LPM6 structure. */
94 : : struct rte_lpm6 {
95 : : /* LPM metadata. */
96 : : char name[RTE_LPM6_NAMESIZE]; /**< Name of the lpm. */
97 : : uint32_t max_rules; /**< Max number of rules. */
98 : : uint32_t used_rules; /**< Used rules so far. */
99 : : uint32_t number_tbl8s; /**< Number of tbl8s to allocate. */
100 : :
101 : : /* LPM Tables. */
102 : : struct rte_hash *rules_tbl; /**< LPM rules. */
103 : : alignas(RTE_CACHE_LINE_SIZE) struct rte_lpm6_tbl_entry tbl24[RTE_LPM6_TBL24_NUM_ENTRIES];
104 : : /**< LPM tbl24 table. */
105 : :
106 : : uint32_t *tbl8_pool; /**< pool of indexes of free tbl8s */
107 : : uint32_t tbl8_pool_pos; /**< current position in the tbl8 pool */
108 : :
109 : : struct rte_lpm_tbl8_hdr *tbl8_hdrs; /* array of tbl8 headers */
110 : :
111 : : alignas(RTE_CACHE_LINE_SIZE) struct rte_lpm6_tbl_entry tbl8[];
112 : : /**< LPM tbl8 table. */
113 : : };
114 : :
115 : : /*
116 : : * LPM6 rule hash function
117 : : *
118 : : * It's used as a hash function for the rte_hash
119 : : * containing rules
120 : : */
121 : : static inline uint32_t
122 : 7621 : rule_hash(const void *data, __rte_unused uint32_t data_len,
123 : : uint32_t init_val)
124 : : {
125 : 7621 : return rte_jhash(data, sizeof(struct rte_lpm6_rule_key), init_val);
126 : : }
127 : :
128 : : /*
129 : : * Init pool of free tbl8 indexes
130 : : */
131 : : static void
132 : : tbl8_pool_init(struct rte_lpm6 *lpm)
133 : : {
134 : : uint32_t i;
135 : :
136 : : /* put entire range of indexes to the tbl8 pool */
137 [ + + + + : 4415843 : for (i = 0; i < lpm->number_tbl8s; i++)
+ + ]
138 : 4415776 : lpm->tbl8_pool[i] = i;
139 : :
140 : 67 : lpm->tbl8_pool_pos = 0;
141 : : }
142 : :
143 : : /*
144 : : * Get an index of a free tbl8 from the pool
145 : : */
146 : : static inline uint32_t
147 : : tbl8_get(struct rte_lpm6 *lpm, uint32_t *tbl8_ind)
148 : : {
149 [ + - + - ]: 6275 : if (lpm->tbl8_pool_pos == lpm->number_tbl8s)
150 : : /* no more free tbl8 */
151 : : return -ENOSPC;
152 : :
153 : : /* next index */
154 : 6275 : *tbl8_ind = lpm->tbl8_pool[lpm->tbl8_pool_pos++];
155 : : return 0;
156 : : }
157 : :
158 : : /*
159 : : * Put an index of a free tbl8 back to the pool
160 : : */
161 : : static inline uint32_t
162 : : tbl8_put(struct rte_lpm6 *lpm, uint32_t tbl8_ind)
163 : : {
164 [ + - ]: 427 : if (lpm->tbl8_pool_pos == 0)
165 : : /* pool is full */
166 : : return -ENOSPC;
167 : :
168 : 427 : lpm->tbl8_pool[--lpm->tbl8_pool_pos] = tbl8_ind;
169 : 427 : return 0;
170 : : }
171 : :
172 : : /*
173 : : * Returns number of tbl8s available in the pool
174 : : */
175 : : static inline uint32_t
176 : : tbl8_available(struct rte_lpm6 *lpm)
177 : : {
178 : 1490 : return lpm->number_tbl8s - lpm->tbl8_pool_pos;
179 : : }
180 : :
181 : : /*
182 : : * Init a rule key.
183 : : * note that ip must be already masked
184 : : */
185 : : static inline void
186 : : rule_key_init(struct rte_lpm6_rule_key *key, const struct rte_ipv6_addr *ip, uint8_t depth)
187 : : {
188 : 1645 : key->ip = *ip;
189 : 1574 : key->depth = depth;
190 : 71 : }
191 : :
192 : : /*
193 : : * Rebuild the entire LPM tree by reinserting all rules
194 : : */
195 : : static void
196 : 4 : rebuild_lpm(struct rte_lpm6 *lpm)
197 : : {
198 : : uint64_t next_hop;
199 : : struct rte_lpm6_rule_key *rule_key;
200 : 4 : uint32_t iter = 0;
201 : :
202 : 10 : while (rte_hash_iterate(lpm->rules_tbl, (void *) &rule_key,
203 [ + + ]: 10 : (void **) &next_hop, &iter) >= 0)
204 : 6 : rte_lpm6_add(lpm, &rule_key->ip, rule_key->depth,
205 : : (uint32_t) next_hop);
206 : 4 : }
207 : :
208 : : /*
209 : : * Allocates memory for LPM object
210 : : */
211 : : RTE_EXPORT_SYMBOL(rte_lpm6_create)
212 : : struct rte_lpm6 *
213 : 59 : rte_lpm6_create(const char *name, int socket_id,
214 : : const struct rte_lpm6_config *config)
215 : : {
216 : : char mem_name[RTE_LPM6_NAMESIZE];
217 : : struct rte_lpm6 *lpm = NULL;
218 : : struct rte_tailq_entry *te;
219 : : uint64_t mem_size;
220 : : struct rte_lpm6_list *lpm_list;
221 : : struct rte_hash *rules_tbl = NULL;
222 : : uint32_t *tbl8_pool = NULL;
223 : : struct rte_lpm_tbl8_hdr *tbl8_hdrs = NULL;
224 : :
225 : 59 : lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
226 : :
227 : : RTE_BUILD_BUG_ON(sizeof(struct rte_lpm6_tbl_entry) != sizeof(uint32_t));
228 : : RTE_BUILD_BUG_ON(sizeof(struct rte_lpm6_rule_key) %
229 : : sizeof(uint32_t) != 0);
230 : :
231 : : /* Check user arguments. */
232 [ + + + + ]: 59 : if ((name == NULL) || (socket_id < -1) || (config == NULL) ||
233 [ + + ]: 56 : (config->max_rules == 0) ||
234 [ + + ]: 55 : config->number_tbl8s > RTE_LPM6_TBL8_MAX_NUM_GROUPS) {
235 : 5 : rte_errno = EINVAL;
236 : 5 : return NULL;
237 : : }
238 : :
239 : : /* create rules hash table */
240 : : snprintf(mem_name, sizeof(mem_name), "LRH_%s", name);
241 : 54 : struct rte_hash_parameters rule_hash_tbl_params = {
242 : 54 : .entries = config->max_rules * 1.2 +
243 : : RULE_HASH_TABLE_EXTRA_SPACE,
244 : : .key_len = sizeof(struct rte_lpm6_rule_key),
245 : : .hash_func = rule_hash,
246 : : .hash_func_init_val = 0,
247 : : .name = mem_name,
248 : : .reserved = 0,
249 : : .socket_id = socket_id,
250 : : .extra_flag = 0
251 : : };
252 : :
253 : 54 : rules_tbl = rte_hash_create(&rule_hash_tbl_params);
254 [ + + ]: 54 : if (rules_tbl == NULL) {
255 : 1 : LPM_LOG(ERR, "LPM rules hash table allocation failed: %s (%d)",
256 : : rte_strerror(rte_errno), rte_errno);
257 : 1 : goto fail_wo_unlock;
258 : : }
259 : :
260 : : /* allocate tbl8 indexes pool */
261 : 53 : tbl8_pool = rte_malloc(NULL,
262 : 53 : sizeof(uint32_t) * config->number_tbl8s,
263 : : RTE_CACHE_LINE_SIZE);
264 [ - + ]: 53 : if (tbl8_pool == NULL) {
265 : 0 : LPM_LOG(ERR, "LPM tbl8 pool allocation failed: %s (%d)",
266 : : rte_strerror(rte_errno), rte_errno);
267 : 0 : rte_errno = ENOMEM;
268 : 0 : goto fail_wo_unlock;
269 : : }
270 : :
271 : : /* allocate tbl8 headers */
272 : 53 : tbl8_hdrs = rte_malloc(NULL,
273 : 53 : sizeof(struct rte_lpm_tbl8_hdr) * config->number_tbl8s,
274 : : RTE_CACHE_LINE_SIZE);
275 [ - + ]: 53 : if (tbl8_hdrs == NULL) {
276 : 0 : LPM_LOG(ERR, "LPM tbl8 headers allocation failed: %s (%d)",
277 : : rte_strerror(rte_errno), rte_errno);
278 : 0 : rte_errno = ENOMEM;
279 : 0 : goto fail_wo_unlock;
280 : : }
281 : :
282 : : snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
283 : :
284 : : /* Determine the amount of memory to allocate. */
285 : 53 : mem_size = sizeof(*lpm) + (sizeof(lpm->tbl8[0]) *
286 : 53 : RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * config->number_tbl8s);
287 : :
288 : 53 : rte_mcfg_tailq_write_lock();
289 : :
290 : : /* Guarantee there's no existing */
291 [ + + ]: 54 : TAILQ_FOREACH(te, lpm_list, next) {
292 : 1 : lpm = (struct rte_lpm6 *) te->data;
293 [ + - ]: 1 : if (strncmp(name, lpm->name, RTE_LPM6_NAMESIZE) == 0)
294 : : break;
295 : : }
296 : : lpm = NULL;
297 [ - + ]: 53 : if (te != NULL) {
298 : 0 : rte_errno = EEXIST;
299 : 0 : goto fail;
300 : : }
301 : :
302 : : /* allocate tailq entry */
303 : 53 : te = rte_zmalloc("LPM6_TAILQ_ENTRY", sizeof(*te), 0);
304 [ - + ]: 53 : if (te == NULL) {
305 : 0 : LPM_LOG(ERR, "Failed to allocate tailq entry!");
306 : 0 : rte_errno = ENOMEM;
307 : 0 : goto fail;
308 : : }
309 : :
310 : : /* Allocate memory to store the LPM data structures. */
311 : 53 : lpm = rte_zmalloc_socket(mem_name, (size_t)mem_size,
312 : : RTE_CACHE_LINE_SIZE, socket_id);
313 : :
314 [ - + ]: 53 : if (lpm == NULL) {
315 : 0 : LPM_LOG(ERR, "LPM memory allocation failed");
316 : 0 : rte_free(te);
317 : 0 : rte_errno = ENOMEM;
318 : 0 : goto fail;
319 : : }
320 : :
321 : : /* Save user arguments. */
322 : 53 : lpm->max_rules = config->max_rules;
323 : 53 : lpm->number_tbl8s = config->number_tbl8s;
324 : 53 : strlcpy(lpm->name, name, sizeof(lpm->name));
325 : 53 : lpm->rules_tbl = rules_tbl;
326 : 53 : lpm->tbl8_pool = tbl8_pool;
327 : 53 : lpm->tbl8_hdrs = tbl8_hdrs;
328 : :
329 : : /* init the stack */
330 : : tbl8_pool_init(lpm);
331 : :
332 : 53 : te->data = (void *) lpm;
333 : :
334 : 53 : TAILQ_INSERT_TAIL(lpm_list, te, next);
335 : 53 : rte_mcfg_tailq_write_unlock();
336 : 53 : return lpm;
337 : :
338 : 0 : fail:
339 : 0 : rte_mcfg_tailq_write_unlock();
340 : :
341 : 1 : fail_wo_unlock:
342 : 1 : rte_free(tbl8_hdrs);
343 : 1 : rte_free(tbl8_pool);
344 : 1 : rte_hash_free(rules_tbl);
345 : :
346 : 1 : return NULL;
347 : : }
348 : :
349 : : /*
350 : : * Find an existing lpm table and return a pointer to it.
351 : : */
352 : : RTE_EXPORT_SYMBOL(rte_lpm6_find_existing)
353 : : struct rte_lpm6 *
354 : 2 : rte_lpm6_find_existing(const char *name)
355 : : {
356 : : struct rte_lpm6 *l = NULL;
357 : : struct rte_tailq_entry *te;
358 : : struct rte_lpm6_list *lpm_list;
359 : :
360 : 2 : lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
361 : :
362 : 2 : rte_mcfg_tailq_read_lock();
363 [ + + ]: 3 : TAILQ_FOREACH(te, lpm_list, next) {
364 : 2 : l = (struct rte_lpm6 *) te->data;
365 [ + + ]: 2 : if (strncmp(name, l->name, RTE_LPM6_NAMESIZE) == 0)
366 : : break;
367 : : }
368 : 2 : rte_mcfg_tailq_read_unlock();
369 : :
370 [ + + ]: 2 : if (te == NULL) {
371 : 1 : rte_errno = ENOENT;
372 : 1 : return NULL;
373 : : }
374 : :
375 : : return l;
376 : : }
377 : :
378 : : /*
379 : : * Deallocates memory for given LPM table.
380 : : */
381 : : RTE_EXPORT_SYMBOL(rte_lpm6_free)
382 : : void
383 : 54 : rte_lpm6_free(struct rte_lpm6 *lpm)
384 : : {
385 : : struct rte_lpm6_list *lpm_list;
386 : : struct rte_tailq_entry *te;
387 : :
388 : : /* Check user arguments. */
389 [ + + ]: 54 : if (lpm == NULL)
390 : : return;
391 : :
392 : 53 : lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
393 : :
394 : 53 : rte_mcfg_tailq_write_lock();
395 : :
396 : : /* find our tailq entry */
397 [ + - ]: 53 : TAILQ_FOREACH(te, lpm_list, next) {
398 [ - + ]: 53 : if (te->data == (void *) lpm)
399 : : break;
400 : : }
401 : :
402 [ + - ]: 53 : if (te != NULL)
403 [ + + ]: 53 : TAILQ_REMOVE(lpm_list, te, next);
404 : :
405 : 53 : rte_mcfg_tailq_write_unlock();
406 : :
407 : 53 : rte_free(lpm->tbl8_hdrs);
408 : 53 : rte_free(lpm->tbl8_pool);
409 : 53 : rte_hash_free(lpm->rules_tbl);
410 : 53 : rte_free(lpm);
411 : 53 : rte_free(te);
412 : : }
413 : :
414 : : /* Find a rule */
415 : : static inline int
416 : : rule_find_with_key(struct rte_lpm6 *lpm,
417 : : const struct rte_lpm6_rule_key *rule_key,
418 : : uint32_t *next_hop)
419 : : {
420 : : uint64_t hash_val;
421 : : int ret;
422 : :
423 : : /* lookup for a rule */
424 : 7 : ret = rte_hash_lookup_data(lpm->rules_tbl, (const void *) rule_key,
425 : : (void **) &hash_val);
426 [ + + + + : 6056 : if (ret >= 0) {
+ + ]
427 : 22 : *next_hop = (uint32_t) hash_val;
428 : 3 : return 1;
429 : : }
430 : :
431 : : return 0;
432 : : }
433 : :
434 : : /* Find a rule */
435 : : static int
436 : : rule_find(struct rte_lpm6 *lpm, struct rte_ipv6_addr *ip, uint8_t depth,
437 : : uint32_t *next_hop)
438 : : {
439 : : struct rte_lpm6_rule_key rule_key;
440 : :
441 : : /* init a rule key */
442 : : rule_key_init(&rule_key, ip, depth);
443 : :
444 : : return rule_find_with_key(lpm, &rule_key, next_hop);
445 : : }
446 : :
447 : : /*
448 : : * Checks if a rule already exists in the rules table and updates
449 : : * the nexthop if so. Otherwise it adds a new rule if enough space is available.
450 : : *
451 : : * Returns:
452 : : * 0 - next hop of existed rule is updated
453 : : * 1 - new rule successfully added
454 : : * <0 - error
455 : : */
456 : : static inline int
457 : 1487 : rule_add(struct rte_lpm6 *lpm, struct rte_ipv6_addr *ip, uint8_t depth, uint32_t next_hop)
458 : : {
459 : : int ret, rule_exist;
460 : : struct rte_lpm6_rule_key rule_key;
461 : : uint32_t unused;
462 : :
463 : : /* init a rule key */
464 : : rule_key_init(&rule_key, ip, depth);
465 : :
466 : : /* Scan through rule list to see if rule already exists. */
467 : : rule_exist = rule_find_with_key(lpm, &rule_key, &unused);
468 : :
469 : : /*
470 : : * If rule does not exist check if there is space to add a new rule to
471 : : * this rule group. If there is no space return error.
472 : : */
473 [ + + ]: 1460 : if (!rule_exist && lpm->used_rules == lpm->max_rules)
474 : : return -ENOSPC;
475 : :
476 : : /* add the rule or update rules next hop */
477 : 1485 : ret = rte_hash_add_key_data(lpm->rules_tbl, &rule_key,
478 : 1485 : (void *)(uintptr_t) next_hop);
479 [ + - ]: 1485 : if (ret < 0)
480 : : return ret;
481 : :
482 : : /* Increment the used rules counter for this rule group. */
483 [ + + ]: 1485 : if (!rule_exist) {
484 : 1458 : lpm->used_rules++;
485 : 1458 : return 1;
486 : : }
487 : :
488 : : return 0;
489 : : }
490 : :
491 : : /*
492 : : * Function that expands a rule across the data structure when a less-generic
493 : : * one has been added before. It assures that every possible combination of bits
494 : : * in the IP address returns a match.
495 : : */
496 : : static void
497 : 18333 : expand_rule(struct rte_lpm6 *lpm, uint32_t tbl8_gindex, uint8_t old_depth,
498 : : uint8_t new_depth, uint32_t next_hop, uint8_t valid)
499 : : {
500 : : uint32_t tbl8_group_end, tbl8_gindex_next, j;
501 : :
502 : 18333 : tbl8_group_end = tbl8_gindex + RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
503 : :
504 : 18333 : struct rte_lpm6_tbl_entry new_tbl8_entry = {
505 : : .valid = valid,
506 : : .valid_group = valid,
507 : : .depth = new_depth,
508 : : .next_hop = next_hop,
509 : : .ext_entry = 0,
510 : : };
511 : :
512 [ + + ]: 4711581 : for (j = tbl8_gindex; j < tbl8_group_end; j++) {
513 [ + + + + ]: 4693248 : if (!lpm->tbl8[j].valid || (lpm->tbl8[j].ext_entry == 0
514 [ + + ]: 4498626 : && lpm->tbl8[j].depth <= old_depth)) {
515 : :
516 : 1722425 : lpm->tbl8[j] = new_tbl8_entry;
517 : :
518 [ + + ]: 2970823 : } else if (lpm->tbl8[j].ext_entry == 1) {
519 : :
520 : 15615 : tbl8_gindex_next = lpm->tbl8[j].lpm6_tbl8_gindex
521 : 15615 : * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
522 : 15615 : expand_rule(lpm, tbl8_gindex_next, old_depth, new_depth,
523 : : next_hop, valid);
524 : : }
525 : : }
526 : 18333 : }
527 : :
528 : : /*
529 : : * Init a tbl8 header
530 : : */
531 : : static inline void
532 : : init_tbl8_header(struct rte_lpm6 *lpm, uint32_t tbl_ind,
533 : : uint32_t owner_tbl_ind, uint32_t owner_entry_ind)
534 : : {
535 : 6275 : struct rte_lpm_tbl8_hdr *tbl_hdr = &lpm->tbl8_hdrs[tbl_ind];
536 : 6275 : tbl_hdr->owner_tbl_ind = owner_tbl_ind;
537 : 6275 : tbl_hdr->owner_entry_ind = owner_entry_ind;
538 : 6275 : tbl_hdr->ref_cnt = 0;
539 : : }
540 : :
541 : : /*
542 : : * Calculate index to the table based on the number and position
543 : : * of the bytes being inspected in this step.
544 : : */
545 : : static uint32_t
546 : : get_bitshift(const struct rte_ipv6_addr *ip, uint8_t first_byte, uint8_t bytes)
547 : : {
548 : : uint32_t entry_ind, i;
549 : : int8_t bitshift;
550 : :
551 : : entry_ind = 0;
552 [ + + + + ]: 27506 : for (i = first_byte; i < (uint32_t)(first_byte + bytes); i++) {
553 : 16728 : bitshift = (int8_t)((bytes - i)*BYTE_SIZE);
554 : :
555 : : if (bitshift < 0)
556 : : bitshift = 0;
557 : 16728 : entry_ind = entry_ind | ip->a[i-1] << bitshift;
558 : : }
559 : :
560 : : return entry_ind;
561 : : }
562 : :
563 : : /*
564 : : * Simulate adding a new route to the LPM counting number
565 : : * of new tables that will be needed
566 : : *
567 : : * It returns 0 on success, or 1 if
568 : : * the process needs to be continued by calling the function again.
569 : : */
570 : : static inline int
571 : 2262 : simulate_add_step(struct rte_lpm6 *lpm, struct rte_lpm6_tbl_entry *tbl,
572 : : struct rte_lpm6_tbl_entry **next_tbl, const struct rte_ipv6_addr *ip,
573 : : uint8_t bytes, uint8_t first_byte, uint8_t depth,
574 : : uint32_t *need_tbl_nb)
575 : : {
576 : : uint32_t entry_ind;
577 : : uint8_t bits_covered;
578 : : uint32_t next_tbl_ind;
579 : :
580 : : /*
581 : : * Calculate index to the table based on the number and position
582 : : * of the bytes being inspected in this step.
583 : : */
584 : 2262 : entry_ind = get_bitshift(ip, first_byte, bytes);
585 : :
586 : : /* Number of bits covered in this step */
587 : 2262 : bits_covered = (uint8_t)((bytes+first_byte-1)*BYTE_SIZE);
588 : :
589 [ + + ]: 2262 : if (depth <= bits_covered) {
590 : 359 : *need_tbl_nb = 0;
591 : 359 : return 0;
592 : : }
593 : :
594 [ + + ]: 1903 : if (tbl[entry_ind].valid == 0 || tbl[entry_ind].ext_entry == 0) {
595 : : /* from this point on a new table is needed on each level
596 : : * that is not covered yet
597 : : */
598 : 1131 : depth -= bits_covered;
599 : 1131 : uint32_t cnt = depth >> 3; /* depth / BYTE_SIZE */
600 [ + + ]: 1131 : if (depth & 7) /* 0b00000111 */
601 : : /* if depth % 8 > 0 then one more table is needed
602 : : * for those last bits
603 : : */
604 : 978 : cnt++;
605 : :
606 : 1131 : *need_tbl_nb = cnt;
607 : 1131 : return 0;
608 : : }
609 : :
610 : 772 : next_tbl_ind = tbl[entry_ind].lpm6_tbl8_gindex;
611 : 772 : *next_tbl = &(lpm->tbl8[next_tbl_ind *
612 : : RTE_LPM6_TBL8_GROUP_NUM_ENTRIES]);
613 : 772 : *need_tbl_nb = 0;
614 : 772 : return 1;
615 : : }
616 : :
617 : : /*
618 : : * Partially adds a new route to the data structure (tbl24+tbl8s).
619 : : * It returns 0 on success, a negative number on failure, or 1 if
620 : : * the process needs to be continued by calling the function again.
621 : : */
622 : : static inline int
623 : 8516 : add_step(struct rte_lpm6 *lpm, struct rte_lpm6_tbl_entry *tbl,
624 : : uint32_t tbl_ind, struct rte_lpm6_tbl_entry **next_tbl,
625 : : uint32_t *next_tbl_ind, struct rte_ipv6_addr *ip, uint8_t bytes,
626 : : uint8_t first_byte, uint8_t depth, uint32_t next_hop,
627 : : uint8_t is_new_rule)
628 : : {
629 : : uint32_t entry_ind, tbl_range, tbl8_group_start, tbl8_group_end, i;
630 : : uint32_t tbl8_gindex;
631 : : uint8_t bits_covered;
632 : : int ret;
633 : :
634 : : /*
635 : : * Calculate index to the table based on the number and position
636 : : * of the bytes being inspected in this step.
637 : : */
638 : 8516 : entry_ind = get_bitshift(ip, first_byte, bytes);
639 : :
640 : : /* Number of bits covered in this step */
641 : 8516 : bits_covered = (uint8_t)((bytes+first_byte-1)*BYTE_SIZE);
642 : :
643 : : /*
644 : : * If depth if smaller than this number (ie this is the last step)
645 : : * expand the rule across the relevant positions in the table.
646 : : */
647 [ + + ]: 8516 : if (depth <= bits_covered) {
648 : 1485 : tbl_range = 1 << (bits_covered - depth);
649 : :
650 [ + + ]: 160450456 : for (i = entry_ind; i < (entry_ind + tbl_range); i++) {
651 [ + + + + ]: 160448971 : if (!tbl[i].valid || (tbl[i].ext_entry == 0 &&
652 [ + + ]: 118464127 : tbl[i].depth <= depth)) {
653 : :
654 : 99764208 : struct rte_lpm6_tbl_entry new_tbl_entry = {
655 : : .next_hop = next_hop,
656 : : .depth = depth,
657 : : .valid = VALID,
658 : : .valid_group = VALID,
659 : : .ext_entry = 0,
660 : : };
661 : :
662 : 99764208 : tbl[i] = new_tbl_entry;
663 : :
664 [ + + ]: 60684763 : } else if (tbl[i].ext_entry == 1) {
665 : :
666 : : /*
667 : : * If tbl entry is valid and extended calculate the index
668 : : * into next tbl8 and expand the rule across the data structure.
669 : : */
670 : 2716 : tbl8_gindex = tbl[i].lpm6_tbl8_gindex *
671 : : RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
672 : 2716 : expand_rule(lpm, tbl8_gindex, depth, depth,
673 : : next_hop, VALID);
674 : : }
675 : : }
676 : :
677 : : /* update tbl8 rule reference counter */
678 [ + + ]: 1485 : if (tbl_ind != TBL24_IND && is_new_rule)
679 : 1225 : lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
680 : :
681 : 1485 : return 0;
682 : : }
683 : : /*
684 : : * If this is not the last step just fill one position
685 : : * and calculate the index to the next table.
686 : : */
687 : : else {
688 : : /* If it's invalid a new tbl8 is needed */
689 [ + + ]: 7031 : if (!tbl[entry_ind].valid) {
690 : : /* get a new table */
691 : : ret = tbl8_get(lpm, &tbl8_gindex);
692 : : if (ret != 0)
693 : : return -ENOSPC;
694 : :
695 : : /* invalidate all new tbl8 entries */
696 : 1445 : tbl8_group_start = tbl8_gindex *
697 : : RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
698 [ + + ]: 1445 : memset(&lpm->tbl8[tbl8_group_start], 0,
699 : : RTE_LPM6_TBL8_GROUP_NUM_ENTRIES *
700 : : sizeof(struct rte_lpm6_tbl_entry));
701 : :
702 : : /* init the new table's header:
703 : : * save the reference to the owner table
704 : : */
705 : : init_tbl8_header(lpm, tbl8_gindex, tbl_ind, entry_ind);
706 : :
707 : : /* reference to a new tbl8 */
708 : 1445 : struct rte_lpm6_tbl_entry new_tbl_entry = {
709 : : .lpm6_tbl8_gindex = tbl8_gindex,
710 : : .depth = 0,
711 : : .valid = VALID,
712 : : .valid_group = VALID,
713 : : .ext_entry = 1,
714 : : };
715 : :
716 : 1445 : tbl[entry_ind] = new_tbl_entry;
717 : :
718 : : /* update the current table's reference counter */
719 [ + + ]: 1445 : if (tbl_ind != TBL24_IND)
720 : 1027 : lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
721 : : }
722 : : /*
723 : : * If it's valid but not extended the rule that was stored
724 : : * here needs to be moved to the next table.
725 : : */
726 [ + + ]: 5586 : else if (tbl[entry_ind].ext_entry == 0) {
727 : : /* get a new tbl8 index */
728 : : ret = tbl8_get(lpm, &tbl8_gindex);
729 : : if (ret != 0)
730 : : return -ENOSPC;
731 : :
732 : 4830 : tbl8_group_start = tbl8_gindex *
733 : : RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
734 : 4830 : tbl8_group_end = tbl8_group_start +
735 : : RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
736 : :
737 : : struct rte_lpm6_tbl_entry tbl_entry = {
738 : 4830 : .next_hop = tbl[entry_ind].next_hop,
739 : 4830 : .depth = tbl[entry_ind].depth,
740 : : .valid = VALID,
741 : : .valid_group = VALID,
742 : : .ext_entry = 0
743 : : };
744 : :
745 : : /* Populate new tbl8 with tbl value. */
746 [ + + ]: 1241310 : for (i = tbl8_group_start; i < tbl8_group_end; i++)
747 : 1236480 : lpm->tbl8[i] = tbl_entry;
748 : :
749 : : /* init the new table's header:
750 : : * save the reference to the owner table
751 : : */
752 : : init_tbl8_header(lpm, tbl8_gindex, tbl_ind, entry_ind);
753 : :
754 : : /*
755 : : * Update tbl entry to point to new tbl8 entry. Note: The
756 : : * ext_flag and tbl8_index need to be updated simultaneously,
757 : : * so assign whole structure in one go.
758 : : */
759 : 4830 : struct rte_lpm6_tbl_entry new_tbl_entry = {
760 : : .lpm6_tbl8_gindex = tbl8_gindex,
761 : : .depth = 0,
762 : : .valid = VALID,
763 : : .valid_group = VALID,
764 : : .ext_entry = 1,
765 : : };
766 : :
767 : 4830 : tbl[entry_ind] = new_tbl_entry;
768 : :
769 : : /* update the current table's reference counter */
770 [ + + ]: 4830 : if (tbl_ind != TBL24_IND)
771 : 4132 : lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
772 : : }
773 : :
774 : 7031 : *next_tbl_ind = tbl[entry_ind].lpm6_tbl8_gindex;
775 : 7031 : *next_tbl = &(lpm->tbl8[*next_tbl_ind *
776 : : RTE_LPM6_TBL8_GROUP_NUM_ENTRIES]);
777 : : }
778 : :
779 : 7031 : return 1;
780 : : }
781 : :
782 : : /*
783 : : * Simulate adding a route to LPM
784 : : *
785 : : * Returns:
786 : : * 0 on success
787 : : * -ENOSPC not enough tbl8 left
788 : : */
789 : : static int
790 : 1490 : simulate_add(struct rte_lpm6 *lpm, const struct rte_ipv6_addr *masked_ip, uint8_t depth)
791 : : {
792 : : struct rte_lpm6_tbl_entry *tbl;
793 : 1490 : struct rte_lpm6_tbl_entry *tbl_next = NULL;
794 : : int ret, i;
795 : :
796 : : /* number of new tables needed for a step */
797 : : uint32_t need_tbl_nb;
798 : : /* total number of new tables needed */
799 : : uint32_t total_need_tbl_nb;
800 : :
801 : : /* Inspect the first three bytes through tbl24 on the first step. */
802 : 1490 : ret = simulate_add_step(lpm, lpm->tbl24, &tbl_next, masked_ip,
803 : : ADD_FIRST_BYTE, 1, depth, &need_tbl_nb);
804 : 1490 : total_need_tbl_nb = need_tbl_nb;
805 : : /*
806 : : * Inspect one by one the rest of the bytes until
807 : : * the process is completed.
808 : : */
809 [ + + ]: 2262 : for (i = ADD_FIRST_BYTE; i < RTE_IPV6_ADDR_SIZE && ret == 1; i++) {
810 : 772 : tbl = tbl_next;
811 : 772 : ret = simulate_add_step(lpm, tbl, &tbl_next, masked_ip, 1,
812 : 772 : (uint8_t)(i + 1), depth, &need_tbl_nb);
813 : 772 : total_need_tbl_nb += need_tbl_nb;
814 : : }
815 : :
816 [ + + ]: 1490 : if (tbl8_available(lpm) < total_need_tbl_nb)
817 : : /* not enough tbl8 to add a rule */
818 : 3 : return -ENOSPC;
819 : :
820 : : return 0;
821 : : }
822 : :
823 : : /*
824 : : * Add a route
825 : : */
826 : : RTE_EXPORT_SYMBOL(rte_lpm6_add)
827 : : int
828 : 1493 : rte_lpm6_add(struct rte_lpm6 *lpm, const struct rte_ipv6_addr *ip, uint8_t depth,
829 : : uint32_t next_hop)
830 : : {
831 : : struct rte_lpm6_tbl_entry *tbl;
832 : 1493 : struct rte_lpm6_tbl_entry *tbl_next = NULL;
833 : : /* init to avoid compiler warning */
834 : 1493 : uint32_t tbl_next_num = 123456;
835 : : int status;
836 : : struct rte_ipv6_addr masked_ip;
837 : : int i;
838 : :
839 : : /* Check user arguments. */
840 [ + + + + ]: 1493 : if ((lpm == NULL) || (depth < 1) || (depth > RTE_IPV6_MAX_DEPTH))
841 : : return -EINVAL;
842 : :
843 : : /* Copy the IP and mask it to avoid modifying user's input data. */
844 : 1490 : masked_ip = *ip;
845 [ + + ]: 1490 : rte_ipv6_addr_mask(&masked_ip, depth);
846 : :
847 : : /* Simulate adding a new route */
848 : 1490 : int ret = simulate_add(lpm, &masked_ip, depth);
849 [ + + ]: 1490 : if (ret < 0)
850 : : return ret;
851 : :
852 : : /* Add the rule to the rule table. */
853 : 1487 : int is_new_rule = rule_add(lpm, &masked_ip, depth, next_hop);
854 : : /* If there is no space available for new rule return error. */
855 [ + + ]: 1487 : if (is_new_rule < 0)
856 : : return is_new_rule;
857 : :
858 : : /* Inspect the first three bytes through tbl24 on the first step. */
859 : 1485 : tbl = lpm->tbl24;
860 : 1485 : status = add_step(lpm, tbl, TBL24_IND, &tbl_next, &tbl_next_num,
861 : : &masked_ip, ADD_FIRST_BYTE, 1, depth, next_hop,
862 : : is_new_rule);
863 [ - + ]: 1485 : assert(status >= 0);
864 : :
865 : : /*
866 : : * Inspect one by one the rest of the bytes until
867 : : * the process is completed.
868 : : */
869 [ + + ]: 8516 : for (i = ADD_FIRST_BYTE; i < RTE_IPV6_ADDR_SIZE && status == 1; i++) {
870 : 7031 : tbl = tbl_next;
871 : 7031 : status = add_step(lpm, tbl, tbl_next_num, &tbl_next,
872 : 7031 : &tbl_next_num, &masked_ip, 1, (uint8_t)(i + 1),
873 : : depth, next_hop, is_new_rule);
874 [ - + ]: 7031 : assert(status >= 0);
875 : : }
876 : :
877 : : return status;
878 : : }
879 : :
880 : : /*
881 : : * Takes a pointer to a table entry and inspect one level.
882 : : * The function returns 0 on lookup success, ENOENT if no match was found
883 : : * or 1 if the process needs to be continued by calling the function again.
884 : : */
885 : : static inline int
886 : : lookup_step(const struct rte_lpm6 *lpm, const struct rte_lpm6_tbl_entry *tbl,
887 : : const struct rte_lpm6_tbl_entry **tbl_next, const struct rte_ipv6_addr *ip,
888 : : uint8_t first_byte, uint32_t *next_hop)
889 : : {
890 : : uint32_t tbl8_index, tbl_entry;
891 : :
892 : : /* Take the integer value from the pointer. */
893 : 1505327 : tbl_entry = *(const uint32_t *)tbl;
894 : :
895 : : /* If it is valid and extended we calculate the new pointer to return. */
896 : 1505327 : if ((tbl_entry & RTE_LPM6_VALID_EXT_ENTRY_BITMASK) ==
897 : : RTE_LPM6_VALID_EXT_ENTRY_BITMASK) {
898 : :
899 : 1338820 : tbl8_index = ip->a[first_byte-1] +
900 : 1338820 : ((tbl_entry & RTE_LPM6_TBL8_BITMASK) *
901 : : RTE_LPM6_TBL8_GROUP_NUM_ENTRIES);
902 : :
903 : 1338748 : *tbl_next = &lpm->tbl8[tbl8_index];
904 : :
905 : 72 : return 1;
906 : : } else {
907 : : /* If not extended then we can have a match. */
908 : 166507 : *next_hop = ((uint32_t)tbl_entry & RTE_LPM6_TBL8_BITMASK);
909 [ + + + + ]: 166507 : return (tbl_entry & RTE_LPM6_LOOKUP_SUCCESS) ? 0 : -ENOENT;
910 : : }
911 : : }
912 : :
913 : : /*
914 : : * Looks up an IP
915 : : */
916 : : RTE_EXPORT_SYMBOL(rte_lpm6_lookup)
917 : : int
918 : 166481 : rte_lpm6_lookup(const struct rte_lpm6 *lpm, const struct rte_ipv6_addr *ip,
919 : : uint32_t *next_hop)
920 : : {
921 : : const struct rte_lpm6_tbl_entry *tbl;
922 : : const struct rte_lpm6_tbl_entry *tbl_next = NULL;
923 : : int status;
924 : : uint8_t first_byte;
925 : : uint32_t tbl24_index;
926 : :
927 : : /* DEBUG: Check user input arguments. */
928 [ + + + + ]: 166481 : if ((lpm == NULL) || (ip == NULL) || (next_hop == NULL))
929 : : return -EINVAL;
930 : :
931 : : first_byte = LOOKUP_FIRST_BYTE;
932 : 166478 : tbl24_index = (ip->a[0] << BYTES2_SIZE) | (ip->a[1] << BYTE_SIZE) | ip->a[2];
933 : :
934 : : /* Calculate pointer to the first entry to be inspected */
935 : 166478 : tbl = &lpm->tbl24[tbl24_index];
936 : :
937 : : do {
938 : : /* Continue inspecting following levels until success or failure */
939 [ + + ]: 1505226 : status = lookup_step(lpm, tbl, &tbl_next, ip, first_byte++, next_hop);
940 : : tbl = tbl_next;
941 : : } while (status == 1);
942 : :
943 : : return status;
944 : : }
945 : :
946 : : /*
947 : : * Looks up a group of IP addresses
948 : : */
949 : : RTE_EXPORT_SYMBOL(rte_lpm6_lookup_bulk_func)
950 : : int
951 : 9 : rte_lpm6_lookup_bulk_func(const struct rte_lpm6 *lpm,
952 : : struct rte_ipv6_addr *ips,
953 : : int32_t *next_hops, unsigned int n)
954 : : {
955 : : unsigned int i;
956 : : const struct rte_lpm6_tbl_entry *tbl;
957 : : const struct rte_lpm6_tbl_entry *tbl_next = NULL;
958 : : uint32_t tbl24_index, next_hop;
959 : : uint8_t first_byte;
960 : : int status;
961 : :
962 : : /* DEBUG: Check user input arguments. */
963 [ + + + + ]: 9 : if ((lpm == NULL) || (ips == NULL) || (next_hops == NULL))
964 : : return -EINVAL;
965 : :
966 [ + + ]: 35 : for (i = 0; i < n; i++) {
967 : : first_byte = LOOKUP_FIRST_BYTE;
968 : 29 : tbl24_index = (ips[i].a[0] << BYTES2_SIZE) |
969 : 29 : (ips[i].a[1] << BYTE_SIZE) | ips[i].a[2];
970 : :
971 : : /* Calculate pointer to the first entry to be inspected */
972 : 29 : tbl = &lpm->tbl24[tbl24_index];
973 : :
974 : : do {
975 : : /* Continue inspecting following levels
976 : : * until success or failure
977 : : */
978 : 101 : status = lookup_step(lpm, tbl, &tbl_next, &ips[i],
979 [ + + ]: 101 : first_byte++, &next_hop);
980 : : tbl = tbl_next;
981 [ + + ]: 101 : } while (status == 1);
982 : :
983 [ + + ]: 29 : if (status < 0)
984 : 15 : next_hops[i] = -1;
985 : : else
986 : 14 : next_hops[i] = (int32_t)next_hop;
987 : : }
988 : :
989 : : return 0;
990 : : }
991 : :
992 : : /*
993 : : * Look for a rule in the high-level rules table
994 : : */
995 : : RTE_EXPORT_SYMBOL(rte_lpm6_is_rule_present)
996 : : int
997 : 7 : rte_lpm6_is_rule_present(struct rte_lpm6 *lpm, const struct rte_ipv6_addr *ip, uint8_t depth,
998 : : uint32_t *next_hop)
999 : : {
1000 : : struct rte_ipv6_addr masked_ip;
1001 : :
1002 : : /* Check user arguments. */
1003 [ + - ]: 7 : if ((lpm == NULL) || next_hop == NULL || ip == NULL ||
1004 [ + - + - ]: 7 : (depth < 1) || (depth > RTE_IPV6_MAX_DEPTH))
1005 : : return -EINVAL;
1006 : :
1007 : : /* Copy the IP and mask it to avoid modifying user's input data. */
1008 [ + - ]: 7 : masked_ip = *ip;
1009 : : rte_ipv6_addr_mask(&masked_ip, depth);
1010 : :
1011 : 7 : return rule_find(lpm, &masked_ip, depth, next_hop);
1012 : : }
1013 : :
1014 : : /*
1015 : : * Delete a rule from the rule table.
1016 : : * NOTE: Valid range for depth parameter is 1 .. 128 inclusive.
1017 : : * return
1018 : : * 0 on success
1019 : : * <0 on failure
1020 : : */
1021 : : static inline int
1022 : 6 : rule_delete(struct rte_lpm6 *lpm, struct rte_ipv6_addr *ip, uint8_t depth)
1023 : : {
1024 : : int ret;
1025 : : struct rte_lpm6_rule_key rule_key;
1026 : :
1027 : : /* init rule key */
1028 : : rule_key_init(&rule_key, ip, depth);
1029 : :
1030 : : /* delete the rule */
1031 : 80 : ret = rte_hash_del_key(lpm->rules_tbl, (void *) &rule_key);
1032 [ + + + + ]: 80 : if (ret >= 0)
1033 : 77 : lpm->used_rules--;
1034 : :
1035 : 6 : return ret;
1036 : : }
1037 : :
1038 : : /*
1039 : : * Deletes a group of rules
1040 : : *
1041 : : * Note that the function rebuilds the lpm table,
1042 : : * rather than doing incremental updates like
1043 : : * the regular delete function
1044 : : */
1045 : : RTE_EXPORT_SYMBOL(rte_lpm6_delete_bulk_func)
1046 : : int
1047 : 7 : rte_lpm6_delete_bulk_func(struct rte_lpm6 *lpm,
1048 : : struct rte_ipv6_addr *ips, uint8_t *depths,
1049 : : unsigned n)
1050 : : {
1051 : : struct rte_ipv6_addr masked_ip;
1052 : : unsigned i;
1053 : :
1054 : : /* Check input arguments. */
1055 [ + + + + ]: 7 : if ((lpm == NULL) || (ips == NULL) || (depths == NULL))
1056 : : return -EINVAL;
1057 : :
1058 [ + + ]: 10 : for (i = 0; i < n; i++) {
1059 : 6 : masked_ip = ips[i];
1060 [ + - ]: 6 : rte_ipv6_addr_mask(&masked_ip, depths[i]);
1061 : 6 : rule_delete(lpm, &masked_ip, depths[i]);
1062 : : }
1063 : :
1064 : : /*
1065 : : * Set all the table entries to 0 (ie delete every rule
1066 : : * from the data structure.
1067 : : */
1068 : 4 : memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1069 : 4 : memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
1070 : 4 : * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1071 : : tbl8_pool_init(lpm);
1072 : :
1073 : : /*
1074 : : * Add every rule again (except for the ones that were removed from
1075 : : * the rules table).
1076 : : */
1077 : 4 : rebuild_lpm(lpm);
1078 : :
1079 : 4 : return 0;
1080 : : }
1081 : :
1082 : : /*
1083 : : * Delete all rules from the LPM table.
1084 : : */
1085 : : RTE_EXPORT_SYMBOL(rte_lpm6_delete_all)
1086 : : void
1087 : 10 : rte_lpm6_delete_all(struct rte_lpm6 *lpm)
1088 : : {
1089 : : /* Zero used rules counter. */
1090 : 10 : lpm->used_rules = 0;
1091 : :
1092 : : /* Zero tbl24. */
1093 : 10 : memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1094 : :
1095 : : /* Zero tbl8. */
1096 : 10 : memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0]) *
1097 : 10 : RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1098 : :
1099 : : /* init pool of free tbl8 indexes */
1100 : : tbl8_pool_init(lpm);
1101 : :
1102 : : /* Delete all rules form the rules table. */
1103 : 10 : rte_hash_reset(lpm->rules_tbl);
1104 : 10 : }
1105 : :
1106 : : /*
1107 : : * Convert a depth to a one byte long mask
1108 : : * Example: 4 will be converted to 0xF0
1109 : : */
1110 : : static uint8_t __rte_pure
1111 : : depth_to_mask_1b(uint8_t depth)
1112 : : {
1113 : : /* To calculate a mask start with a 1 on the left hand side and right
1114 : : * shift while populating the left hand side with 1's
1115 : : */
1116 : 4077 : return (signed char)0x80 >> (depth - 1);
1117 : : }
1118 : :
1119 : : /*
1120 : : * Find a less specific rule
1121 : : */
1122 : : static int
1123 : 72 : rule_find_less_specific(struct rte_lpm6 *lpm, struct rte_ipv6_addr *ip, uint8_t depth,
1124 : : struct rte_lpm6_rule *rule)
1125 : : {
1126 : : int ret;
1127 : : uint32_t next_hop;
1128 : : uint8_t mask;
1129 : : struct rte_lpm6_rule_key rule_key;
1130 : :
1131 [ + + ]: 72 : if (depth == 1)
1132 : : return 0;
1133 : :
1134 : : rule_key_init(&rule_key, ip, depth);
1135 : :
1136 [ + + ]: 4614 : while (depth > 1) {
1137 : 4562 : depth--;
1138 : :
1139 : : /* each iteration zero one more bit of the key */
1140 : 4562 : mask = depth & 7; /* depth % BYTE_SIZE */
1141 [ + + ]: 4562 : if (mask > 0)
1142 : 4034 : mask = depth_to_mask_1b(mask);
1143 : :
1144 : 4562 : rule_key.depth = depth;
1145 : 4562 : rule_key.ip.a[depth >> 3] &= mask;
1146 : :
1147 : : ret = rule_find_with_key(lpm, &rule_key, &next_hop);
1148 : : if (ret) {
1149 : 19 : rule->depth = depth;
1150 : 19 : rule->ip = rule_key.ip;
1151 : 19 : rule->next_hop = next_hop;
1152 : 19 : return 1;
1153 : : }
1154 : : }
1155 : :
1156 : : return 0;
1157 : : }
1158 : :
1159 : : /*
1160 : : * Find range of tbl8 cells occupied by a rule
1161 : : */
1162 : : static void
1163 : 72 : rule_find_range(struct rte_lpm6 *lpm, const struct rte_ipv6_addr *ip, uint8_t depth,
1164 : : struct rte_lpm6_tbl_entry **from,
1165 : : struct rte_lpm6_tbl_entry **to,
1166 : : uint32_t *out_tbl_ind)
1167 : : {
1168 : : uint32_t ind;
1169 : 72 : uint32_t first_3bytes = (uint32_t)ip->a[0] << 16 |
1170 : 72 : ip->a[1] << 8 | ip->a[2];
1171 : :
1172 [ + + ]: 72 : if (depth <= 24) {
1173 : : /* rule is within the top level */
1174 : : ind = first_3bytes;
1175 : 29 : *from = &lpm->tbl24[ind];
1176 : 29 : ind += (1 << (24 - depth)) - 1;
1177 : 29 : *to = &lpm->tbl24[ind];
1178 : 29 : *out_tbl_ind = TBL24_IND;
1179 : : } else {
1180 : : /* top level entry */
1181 : : struct rte_lpm6_tbl_entry *tbl = &lpm->tbl24[first_3bytes];
1182 [ - + ]: 43 : assert(tbl->ext_entry == 1);
1183 : : /* first tbl8 */
1184 : 43 : uint32_t tbl_ind = tbl->lpm6_tbl8_gindex;
1185 : 43 : tbl = &lpm->tbl8[tbl_ind *
1186 : : RTE_LPM6_TBL8_GROUP_NUM_ENTRIES];
1187 : : /* current ip byte, the top level is already behind */
1188 : : uint8_t byte = 3;
1189 : : /* minus top level */
1190 : 43 : depth -= 24;
1191 : :
1192 : : /* iterate through levels (tbl8s)
1193 : : * until we reach the last one
1194 : : */
1195 [ + + ]: 442 : while (depth > 8) {
1196 : 399 : tbl += ip->a[byte];
1197 [ - + ]: 399 : assert(tbl->ext_entry == 1);
1198 : : /* go to the next level/tbl8 */
1199 : 399 : tbl_ind = tbl->lpm6_tbl8_gindex;
1200 : 399 : tbl = &lpm->tbl8[tbl_ind *
1201 : : RTE_LPM6_TBL8_GROUP_NUM_ENTRIES];
1202 : 399 : byte += 1;
1203 : 399 : depth -= 8;
1204 : : }
1205 : :
1206 : : /* last level/tbl8 */
1207 : 43 : ind = ip->a[byte] & depth_to_mask_1b(depth);
1208 : 43 : *from = &tbl[ind];
1209 : 43 : ind += (1 << (8 - depth)) - 1;
1210 : 43 : *to = &tbl[ind];
1211 : 43 : *out_tbl_ind = tbl_ind;
1212 : : }
1213 : 72 : }
1214 : :
1215 : : /*
1216 : : * Remove a table from the LPM tree
1217 : : */
1218 : : static void
1219 : 427 : remove_tbl(struct rte_lpm6 *lpm, struct rte_lpm_tbl8_hdr *tbl_hdr,
1220 : : uint32_t tbl_ind, struct rte_lpm6_rule *lsp_rule)
1221 : : {
1222 : : struct rte_lpm6_tbl_entry *owner_entry;
1223 : :
1224 [ + + ]: 427 : if (tbl_hdr->owner_tbl_ind == TBL24_IND)
1225 : 40 : owner_entry = &lpm->tbl24[tbl_hdr->owner_entry_ind];
1226 : : else {
1227 : : uint32_t owner_tbl_ind = tbl_hdr->owner_tbl_ind;
1228 : 387 : owner_entry = &lpm->tbl8[
1229 : 387 : owner_tbl_ind * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES +
1230 : 387 : tbl_hdr->owner_entry_ind];
1231 : :
1232 : 387 : struct rte_lpm_tbl8_hdr *owner_tbl_hdr =
1233 : 387 : &lpm->tbl8_hdrs[owner_tbl_ind];
1234 [ + - ]: 387 : if (--owner_tbl_hdr->ref_cnt == 0)
1235 : 387 : remove_tbl(lpm, owner_tbl_hdr, owner_tbl_ind, lsp_rule);
1236 : : }
1237 : :
1238 [ - + ]: 427 : assert(owner_entry->ext_entry == 1);
1239 : :
1240 : : /* unlink the table */
1241 [ - + ]: 427 : if (lsp_rule != NULL) {
1242 : : struct rte_lpm6_tbl_entry new_tbl_entry = {
1243 : 0 : .next_hop = lsp_rule->next_hop,
1244 : 0 : .depth = lsp_rule->depth,
1245 : : .valid = VALID,
1246 : : .valid_group = VALID,
1247 : : .ext_entry = 0
1248 : : };
1249 : :
1250 : 0 : *owner_entry = new_tbl_entry;
1251 : : } else {
1252 : : struct rte_lpm6_tbl_entry new_tbl_entry = {
1253 : : .next_hop = 0,
1254 : : .depth = 0,
1255 : : .valid = INVALID,
1256 : : .valid_group = INVALID,
1257 : : .ext_entry = 0
1258 : : };
1259 : :
1260 : 427 : *owner_entry = new_tbl_entry;
1261 : : }
1262 : :
1263 : : /* return the table to the pool */
1264 : : tbl8_put(lpm, tbl_ind);
1265 : 427 : }
1266 : :
1267 : : /*
1268 : : * Deletes a rule
1269 : : */
1270 : : RTE_EXPORT_SYMBOL(rte_lpm6_delete)
1271 : : int
1272 : 77 : rte_lpm6_delete(struct rte_lpm6 *lpm, const struct rte_ipv6_addr *ip, uint8_t depth)
1273 : : {
1274 : : struct rte_ipv6_addr masked_ip;
1275 : : struct rte_lpm6_rule lsp_rule_obj;
1276 : : struct rte_lpm6_rule *lsp_rule;
1277 : : int ret;
1278 : : uint32_t tbl_ind;
1279 : : struct rte_lpm6_tbl_entry *from, *to;
1280 : :
1281 : : /* Check input arguments. */
1282 [ + + + + ]: 77 : if ((lpm == NULL) || (depth < 1) || (depth > RTE_IPV6_MAX_DEPTH))
1283 : : return -EINVAL;
1284 : :
1285 : : /* Copy the IP and mask it to avoid modifying user's input data. */
1286 : 74 : masked_ip = *ip;
1287 [ + + ]: 74 : rte_ipv6_addr_mask(&masked_ip, depth);
1288 : :
1289 : : /* Delete the rule from the rule table. */
1290 : : ret = rule_delete(lpm, &masked_ip, depth);
1291 [ + + ]: 74 : if (ret < 0)
1292 : : return -ENOENT;
1293 : :
1294 : : /* find rule cells */
1295 : 72 : rule_find_range(lpm, &masked_ip, depth, &from, &to, &tbl_ind);
1296 : :
1297 : : /* find a less specific rule (a rule with smaller depth)
1298 : : * note: masked_ip will be modified, don't use it anymore
1299 : : */
1300 : 72 : ret = rule_find_less_specific(lpm, &masked_ip, depth,
1301 : : &lsp_rule_obj);
1302 [ + + ]: 72 : lsp_rule = ret ? &lsp_rule_obj : NULL;
1303 : :
1304 : : /* decrement the table rule counter,
1305 : : * note that tbl24 doesn't have a header
1306 : : */
1307 [ + + ]: 72 : if (tbl_ind != TBL24_IND) {
1308 : 43 : struct rte_lpm_tbl8_hdr *tbl_hdr = &lpm->tbl8_hdrs[tbl_ind];
1309 [ + + ]: 43 : if (--tbl_hdr->ref_cnt == 0) {
1310 : : /* remove the table */
1311 : 40 : remove_tbl(lpm, tbl_hdr, tbl_ind, lsp_rule);
1312 : 40 : return 0;
1313 : : }
1314 : : }
1315 : :
1316 : : /* iterate rule cells */
1317 [ + + ]: 20972588 : for (; from <= to; from++)
1318 [ + + ]: 20972556 : if (from->ext_entry == 1) {
1319 : : /* reference to a more specific space
1320 : : * of the prefix/rule. Entries in a more
1321 : : * specific space that are not used by
1322 : : * a more specific prefix must be occupied
1323 : : * by the prefix
1324 : : */
1325 [ - + ]: 2 : if (lsp_rule != NULL)
1326 : 0 : expand_rule(lpm,
1327 : 0 : from->lpm6_tbl8_gindex *
1328 : : RTE_LPM6_TBL8_GROUP_NUM_ENTRIES,
1329 : 0 : depth, lsp_rule->depth,
1330 : : lsp_rule->next_hop, VALID);
1331 : : else
1332 : : /* since the prefix has no less specific prefix,
1333 : : * its more specific space must be invalidated
1334 : : */
1335 : 2 : expand_rule(lpm,
1336 : 2 : from->lpm6_tbl8_gindex *
1337 : : RTE_LPM6_TBL8_GROUP_NUM_ENTRIES,
1338 : : depth, 0, 0, INVALID);
1339 [ + - ]: 20972554 : } else if (from->depth == depth) {
1340 : : /* entry is not a reference and belongs to the prefix */
1341 [ + + ]: 20972554 : if (lsp_rule != NULL) {
1342 : : struct rte_lpm6_tbl_entry new_tbl_entry = {
1343 : 12582660 : .next_hop = lsp_rule->next_hop,
1344 : 12582660 : .depth = lsp_rule->depth,
1345 : : .valid = VALID,
1346 : : .valid_group = VALID,
1347 : : .ext_entry = 0
1348 : : };
1349 : :
1350 : 12582660 : *from = new_tbl_entry;
1351 : : } else {
1352 : : struct rte_lpm6_tbl_entry new_tbl_entry = {
1353 : : .next_hop = 0,
1354 : : .depth = 0,
1355 : : .valid = INVALID,
1356 : : .valid_group = INVALID,
1357 : : .ext_entry = 0
1358 : : };
1359 : :
1360 : 8389894 : *from = new_tbl_entry;
1361 : : }
1362 : : }
1363 : :
1364 : : return 0;
1365 : : }
|