1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
13 #include "test_xmmt_ops.h"
15 #define TEST_LPM_ASSERT(cond) do { \
17 printf("Error at line %d: \n", __LINE__); \
22 typedef int32_t (*rte_lpm_test)(void);
24 static int32_t test0(void);
25 static int32_t test1(void);
26 static int32_t test2(void);
27 static int32_t test3(void);
28 static int32_t test4(void);
29 static int32_t test5(void);
30 static int32_t test6(void);
31 static int32_t test7(void);
32 static int32_t test8(void);
33 static int32_t test9(void);
34 static int32_t test10(void);
35 static int32_t test11(void);
36 static int32_t test12(void);
37 static int32_t test13(void);
38 static int32_t test14(void);
39 static int32_t test15(void);
40 static int32_t test16(void);
41 static int32_t test17(void);
42 static int32_t test18(void);
44 rte_lpm_test tests[] = {
67 #define NUM_LPM_TESTS (sizeof(tests)/sizeof(tests[0]))
70 #define NUMBER_TBL8S 256
74 * Check that rte_lpm_create fails gracefully for incorrect user input
80 struct rte_lpm *lpm = NULL;
81 struct rte_lpm_config config;
83 config.max_rules = MAX_RULES;
84 config.number_tbl8s = NUMBER_TBL8S;
87 /* rte_lpm_create: lpm name == NULL */
88 lpm = rte_lpm_create(NULL, SOCKET_ID_ANY, &config);
89 TEST_LPM_ASSERT(lpm == NULL);
91 /* rte_lpm_create: max_rules = 0 */
92 /* Note: __func__ inserts the function name, in this case "test0". */
94 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
95 TEST_LPM_ASSERT(lpm == NULL);
97 /* socket_id < -1 is invalid */
98 config.max_rules = MAX_RULES;
99 lpm = rte_lpm_create(__func__, -2, &config);
100 TEST_LPM_ASSERT(lpm == NULL);
106 * Create lpm table then delete lpm table 100 times
107 * Use a slightly different rules size each time
112 struct rte_lpm *lpm = NULL;
113 struct rte_lpm_config config;
115 config.number_tbl8s = NUMBER_TBL8S;
119 /* rte_lpm_free: Free NULL */
120 for (i = 0; i < 100; i++) {
121 config.max_rules = MAX_RULES - i;
122 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
123 TEST_LPM_ASSERT(lpm != NULL);
128 /* Can not test free so return success */
133 * Call rte_lpm_free for NULL pointer user input. Note: free has no return and
134 * therefore it is impossible to check for failure but this test is added to
135 * increase function coverage metrics and to validate that freeing null does
141 struct rte_lpm *lpm = NULL;
142 struct rte_lpm_config config;
144 config.max_rules = MAX_RULES;
145 config.number_tbl8s = NUMBER_TBL8S;
148 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
149 TEST_LPM_ASSERT(lpm != NULL);
157 * Check that rte_lpm_add fails gracefully for incorrect user input arguments
162 struct rte_lpm *lpm = NULL;
163 struct rte_lpm_config config;
165 config.max_rules = MAX_RULES;
166 config.number_tbl8s = NUMBER_TBL8S;
168 uint32_t ip = IPv4(0, 0, 0, 0), next_hop = 100;
172 /* rte_lpm_add: lpm == NULL */
173 status = rte_lpm_add(NULL, ip, depth, next_hop);
174 TEST_LPM_ASSERT(status < 0);
176 /*Create vaild lpm to use in rest of test. */
177 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
178 TEST_LPM_ASSERT(lpm != NULL);
180 /* rte_lpm_add: depth < 1 */
181 status = rte_lpm_add(lpm, ip, 0, next_hop);
182 TEST_LPM_ASSERT(status < 0);
184 /* rte_lpm_add: depth > MAX_DEPTH */
185 status = rte_lpm_add(lpm, ip, (MAX_DEPTH + 1), next_hop);
186 TEST_LPM_ASSERT(status < 0);
194 * Check that rte_lpm_delete fails gracefully for incorrect user input
200 struct rte_lpm *lpm = NULL;
201 struct rte_lpm_config config;
203 config.max_rules = MAX_RULES;
204 config.number_tbl8s = NUMBER_TBL8S;
206 uint32_t ip = IPv4(0, 0, 0, 0);
210 /* rte_lpm_delete: lpm == NULL */
211 status = rte_lpm_delete(NULL, ip, depth);
212 TEST_LPM_ASSERT(status < 0);
214 /*Create vaild lpm to use in rest of test. */
215 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
216 TEST_LPM_ASSERT(lpm != NULL);
218 /* rte_lpm_delete: depth < 1 */
219 status = rte_lpm_delete(lpm, ip, 0);
220 TEST_LPM_ASSERT(status < 0);
222 /* rte_lpm_delete: depth > MAX_DEPTH */
223 status = rte_lpm_delete(lpm, ip, (MAX_DEPTH + 1));
224 TEST_LPM_ASSERT(status < 0);
232 * Check that rte_lpm_lookup fails gracefully for incorrect user input
238 #if defined(RTE_LIBRTE_LPM_DEBUG)
239 struct rte_lpm *lpm = NULL;
240 struct rte_lpm_config config;
242 config.max_rules = MAX_RULES;
243 config.number_tbl8s = NUMBER_TBL8S;
245 uint32_t ip = IPv4(0, 0, 0, 0), next_hop_return = 0;
248 /* rte_lpm_lookup: lpm == NULL */
249 status = rte_lpm_lookup(NULL, ip, &next_hop_return);
250 TEST_LPM_ASSERT(status < 0);
252 /*Create vaild lpm to use in rest of test. */
253 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
254 TEST_LPM_ASSERT(lpm != NULL);
256 /* rte_lpm_lookup: depth < 1 */
257 status = rte_lpm_lookup(lpm, ip, NULL);
258 TEST_LPM_ASSERT(status < 0);
268 * Call add, lookup and delete for a single rule with depth <= 24
273 struct rte_lpm *lpm = NULL;
274 struct rte_lpm_config config;
276 config.max_rules = MAX_RULES;
277 config.number_tbl8s = NUMBER_TBL8S;
279 uint32_t ip = IPv4(0, 0, 0, 0), next_hop_add = 100, next_hop_return = 0;
283 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
284 TEST_LPM_ASSERT(lpm != NULL);
286 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
287 TEST_LPM_ASSERT(status == 0);
289 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
290 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
292 status = rte_lpm_delete(lpm, ip, depth);
293 TEST_LPM_ASSERT(status == 0);
295 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
296 TEST_LPM_ASSERT(status == -ENOENT);
304 * Call add, lookup and delete for a single rule with depth > 24
312 struct rte_lpm *lpm = NULL;
313 struct rte_lpm_config config;
315 config.max_rules = MAX_RULES;
316 config.number_tbl8s = NUMBER_TBL8S;
318 uint32_t ip = IPv4(0, 0, 0, 0), next_hop_add = 100, next_hop_return = 0;
322 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
323 TEST_LPM_ASSERT(lpm != NULL);
325 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
326 TEST_LPM_ASSERT(status == 0);
328 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
329 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
331 ipx4 = vect_set_epi32(ip, ip + 0x100, ip - 0x100, ip);
332 rte_lpm_lookupx4(lpm, ipx4, hop, UINT32_MAX);
333 TEST_LPM_ASSERT(hop[0] == next_hop_add);
334 TEST_LPM_ASSERT(hop[1] == UINT32_MAX);
335 TEST_LPM_ASSERT(hop[2] == UINT32_MAX);
336 TEST_LPM_ASSERT(hop[3] == next_hop_add);
338 status = rte_lpm_delete(lpm, ip, depth);
339 TEST_LPM_ASSERT(status == 0);
341 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
342 TEST_LPM_ASSERT(status == -ENOENT);
350 * Use rte_lpm_add to add rules which effect only the second half of the lpm
351 * table. Use all possible depths ranging from 1..32. Set the next hop = to the
352 * depth. Check lookup hit for on every add and check for lookup miss on the
353 * first half of the lpm table after each add. Finally delete all rules going
354 * backwards (i.e. from depth = 32 ..1) and carry out a lookup after each
355 * delete. The lookup should return the next_hop_add value related to the
356 * previous depth value (i.e. depth -1).
363 struct rte_lpm *lpm = NULL;
364 struct rte_lpm_config config;
366 config.max_rules = MAX_RULES;
367 config.number_tbl8s = NUMBER_TBL8S;
369 uint32_t ip1 = IPv4(127, 255, 255, 255), ip2 = IPv4(128, 0, 0, 0);
370 uint32_t next_hop_add, next_hop_return;
374 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
375 TEST_LPM_ASSERT(lpm != NULL);
377 /* Loop with rte_lpm_add. */
378 for (depth = 1; depth <= 32; depth++) {
379 /* Let the next_hop_add value = depth. Just for change. */
380 next_hop_add = depth;
382 status = rte_lpm_add(lpm, ip2, depth, next_hop_add);
383 TEST_LPM_ASSERT(status == 0);
385 /* Check IP in first half of tbl24 which should be empty. */
386 status = rte_lpm_lookup(lpm, ip1, &next_hop_return);
387 TEST_LPM_ASSERT(status == -ENOENT);
389 status = rte_lpm_lookup(lpm, ip2, &next_hop_return);
390 TEST_LPM_ASSERT((status == 0) &&
391 (next_hop_return == next_hop_add));
393 ipx4 = vect_set_epi32(ip2, ip1, ip2, ip1);
394 rte_lpm_lookupx4(lpm, ipx4, hop, UINT32_MAX);
395 TEST_LPM_ASSERT(hop[0] == UINT32_MAX);
396 TEST_LPM_ASSERT(hop[1] == next_hop_add);
397 TEST_LPM_ASSERT(hop[2] == UINT32_MAX);
398 TEST_LPM_ASSERT(hop[3] == next_hop_add);
401 /* Loop with rte_lpm_delete. */
402 for (depth = 32; depth >= 1; depth--) {
403 next_hop_add = (uint8_t) (depth - 1);
405 status = rte_lpm_delete(lpm, ip2, depth);
406 TEST_LPM_ASSERT(status == 0);
408 status = rte_lpm_lookup(lpm, ip2, &next_hop_return);
411 TEST_LPM_ASSERT((status == 0) &&
412 (next_hop_return == next_hop_add));
414 TEST_LPM_ASSERT(status == -ENOENT);
417 status = rte_lpm_lookup(lpm, ip1, &next_hop_return);
418 TEST_LPM_ASSERT(status == -ENOENT);
420 ipx4 = vect_set_epi32(ip1, ip1, ip2, ip2);
421 rte_lpm_lookupx4(lpm, ipx4, hop, UINT32_MAX);
423 TEST_LPM_ASSERT(hop[0] == next_hop_add);
424 TEST_LPM_ASSERT(hop[1] == next_hop_add);
426 TEST_LPM_ASSERT(hop[0] == UINT32_MAX);
427 TEST_LPM_ASSERT(hop[1] == UINT32_MAX);
429 TEST_LPM_ASSERT(hop[2] == UINT32_MAX);
430 TEST_LPM_ASSERT(hop[3] == UINT32_MAX);
439 * - Add & lookup to hit invalid TBL24 entry
440 * - Add & lookup to hit valid TBL24 entry not extended
441 * - Add & lookup to hit valid extended TBL24 entry with invalid TBL8 entry
442 * - Add & lookup to hit valid extended TBL24 entry with valid TBL8 entry
448 struct rte_lpm *lpm = NULL;
449 struct rte_lpm_config config;
451 config.max_rules = MAX_RULES;
452 config.number_tbl8s = NUMBER_TBL8S;
454 uint32_t ip, ip_1, ip_2;
455 uint8_t depth, depth_1, depth_2;
456 uint32_t next_hop_add, next_hop_add_1, next_hop_add_2, next_hop_return;
459 /* Add & lookup to hit invalid TBL24 entry */
460 ip = IPv4(128, 0, 0, 0);
464 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
465 TEST_LPM_ASSERT(lpm != NULL);
467 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
468 TEST_LPM_ASSERT(status == 0);
470 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
471 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
473 status = rte_lpm_delete(lpm, ip, depth);
474 TEST_LPM_ASSERT(status == 0);
476 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
477 TEST_LPM_ASSERT(status == -ENOENT);
479 rte_lpm_delete_all(lpm);
481 /* Add & lookup to hit valid TBL24 entry not extended */
482 ip = IPv4(128, 0, 0, 0);
486 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
487 TEST_LPM_ASSERT(status == 0);
489 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
490 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
495 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
496 TEST_LPM_ASSERT(status == 0);
498 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
499 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
503 status = rte_lpm_delete(lpm, ip, depth);
504 TEST_LPM_ASSERT(status == 0);
508 status = rte_lpm_delete(lpm, ip, depth);
509 TEST_LPM_ASSERT(status == 0);
511 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
512 TEST_LPM_ASSERT(status == -ENOENT);
514 rte_lpm_delete_all(lpm);
516 /* Add & lookup to hit valid extended TBL24 entry with invalid TBL8
518 ip = IPv4(128, 0, 0, 0);
522 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
523 TEST_LPM_ASSERT(status == 0);
525 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
526 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
528 ip = IPv4(128, 0, 0, 5);
532 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
533 TEST_LPM_ASSERT(status == 0);
535 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
536 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
538 status = rte_lpm_delete(lpm, ip, depth);
539 TEST_LPM_ASSERT(status == 0);
541 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
542 TEST_LPM_ASSERT(status == -ENOENT);
544 ip = IPv4(128, 0, 0, 0);
548 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
549 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
551 status = rte_lpm_delete(lpm, ip, depth);
552 TEST_LPM_ASSERT(status == 0);
554 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
555 TEST_LPM_ASSERT(status == -ENOENT);
557 rte_lpm_delete_all(lpm);
559 /* Add & lookup to hit valid extended TBL24 entry with valid TBL8
561 ip_1 = IPv4(128, 0, 0, 0);
563 next_hop_add_1 = 101;
565 ip_2 = IPv4(128, 0, 0, 5);
567 next_hop_add_2 = 102;
571 status = rte_lpm_add(lpm, ip_1, depth_1, next_hop_add_1);
572 TEST_LPM_ASSERT(status == 0);
574 status = rte_lpm_lookup(lpm, ip_1, &next_hop_return);
575 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add_1));
577 status = rte_lpm_add(lpm, ip_2, depth_2, next_hop_add_2);
578 TEST_LPM_ASSERT(status == 0);
580 status = rte_lpm_lookup(lpm, ip_2, &next_hop_return);
581 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add_2));
583 status = rte_lpm_delete(lpm, ip_2, depth_2);
584 TEST_LPM_ASSERT(status == 0);
586 status = rte_lpm_lookup(lpm, ip_2, &next_hop_return);
587 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add_1));
589 status = rte_lpm_delete(lpm, ip_1, depth_1);
590 TEST_LPM_ASSERT(status == 0);
592 status = rte_lpm_lookup(lpm, ip_1, &next_hop_return);
593 TEST_LPM_ASSERT(status == -ENOENT);
602 * - Add rule that covers a TBL24 range previously invalid & lookup (& delete &
604 * - Add rule that extends a TBL24 invalid entry & lookup (& delete & lookup)
605 * - Add rule that extends a TBL24 valid entry & lookup for both rules (&
607 * - Add rule that updates the next hop in TBL24 & lookup (& delete & lookup)
608 * - Add rule that updates the next hop in TBL8 & lookup (& delete & lookup)
609 * - Delete a rule that is not present in the TBL24 & lookup
610 * - Delete a rule that is not present in the TBL8 & lookup
617 struct rte_lpm *lpm = NULL;
618 struct rte_lpm_config config;
620 config.max_rules = MAX_RULES;
621 config.number_tbl8s = NUMBER_TBL8S;
623 uint32_t ip, next_hop_add, next_hop_return;
627 /* Add rule that covers a TBL24 range previously invalid & lookup
628 * (& delete & lookup) */
629 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
630 TEST_LPM_ASSERT(lpm != NULL);
632 ip = IPv4(128, 0, 0, 0);
636 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
637 TEST_LPM_ASSERT(status == 0);
639 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
640 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
642 status = rte_lpm_delete(lpm, ip, depth);
643 TEST_LPM_ASSERT(status == 0);
645 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
646 TEST_LPM_ASSERT(status == -ENOENT);
648 rte_lpm_delete_all(lpm);
650 ip = IPv4(128, 0, 0, 0);
654 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
655 TEST_LPM_ASSERT(status == 0);
657 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
658 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
660 status = rte_lpm_delete(lpm, ip, depth);
661 TEST_LPM_ASSERT(status == 0);
663 rte_lpm_delete_all(lpm);
665 /* Add rule that extends a TBL24 valid entry & lookup for both rules
666 * (& delete & lookup) */
668 ip = IPv4(128, 0, 0, 0);
672 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
673 TEST_LPM_ASSERT(status == 0);
675 ip = IPv4(128, 0, 0, 10);
679 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
680 TEST_LPM_ASSERT(status == 0);
682 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
683 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
685 ip = IPv4(128, 0, 0, 0);
688 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
689 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
691 ip = IPv4(128, 0, 0, 0);
694 status = rte_lpm_delete(lpm, ip, depth);
695 TEST_LPM_ASSERT(status == 0);
697 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
698 TEST_LPM_ASSERT(status == -ENOENT);
700 ip = IPv4(128, 0, 0, 10);
703 status = rte_lpm_delete(lpm, ip, depth);
704 TEST_LPM_ASSERT(status == 0);
706 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
707 TEST_LPM_ASSERT(status == -ENOENT);
709 rte_lpm_delete_all(lpm);
711 /* Add rule that updates the next hop in TBL24 & lookup
712 * (& delete & lookup) */
714 ip = IPv4(128, 0, 0, 0);
718 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
719 TEST_LPM_ASSERT(status == 0);
721 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
722 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
726 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
727 TEST_LPM_ASSERT(status == 0);
729 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
730 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
732 status = rte_lpm_delete(lpm, ip, depth);
733 TEST_LPM_ASSERT(status == 0);
735 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
736 TEST_LPM_ASSERT(status == -ENOENT);
738 rte_lpm_delete_all(lpm);
740 /* Add rule that updates the next hop in TBL8 & lookup
741 * (& delete & lookup) */
743 ip = IPv4(128, 0, 0, 0);
747 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
748 TEST_LPM_ASSERT(status == 0);
750 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
751 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
755 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
756 TEST_LPM_ASSERT(status == 0);
758 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
759 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
761 status = rte_lpm_delete(lpm, ip, depth);
762 TEST_LPM_ASSERT(status == 0);
764 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
765 TEST_LPM_ASSERT(status == -ENOENT);
767 rte_lpm_delete_all(lpm);
769 /* Delete a rule that is not present in the TBL24 & lookup */
771 ip = IPv4(128, 0, 0, 0);
774 status = rte_lpm_delete(lpm, ip, depth);
775 TEST_LPM_ASSERT(status < 0);
777 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
778 TEST_LPM_ASSERT(status == -ENOENT);
780 rte_lpm_delete_all(lpm);
782 /* Delete a rule that is not present in the TBL8 & lookup */
784 ip = IPv4(128, 0, 0, 0);
787 status = rte_lpm_delete(lpm, ip, depth);
788 TEST_LPM_ASSERT(status < 0);
790 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
791 TEST_LPM_ASSERT(status == -ENOENT);
799 * Add two rules, lookup to hit the more specific one, lookup to hit the less
800 * specific one delete the less specific rule and lookup previous values again;
801 * add a more specific rule than the existing rule, lookup again
808 struct rte_lpm *lpm = NULL;
809 struct rte_lpm_config config;
811 config.max_rules = MAX_RULES;
812 config.number_tbl8s = NUMBER_TBL8S;
814 uint32_t ip, next_hop_add, next_hop_return;
818 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
819 TEST_LPM_ASSERT(lpm != NULL);
821 ip = IPv4(128, 0, 0, 0);
825 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
826 TEST_LPM_ASSERT(status == 0);
828 ip = IPv4(128, 0, 0, 10);
832 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
833 TEST_LPM_ASSERT(status == 0);
835 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
836 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
838 ip = IPv4(128, 0, 0, 0);
841 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
842 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add));
844 ip = IPv4(128, 0, 0, 0);
847 status = rte_lpm_delete(lpm, ip, depth);
848 TEST_LPM_ASSERT(status == 0);
850 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
851 TEST_LPM_ASSERT(status == -ENOENT);
853 ip = IPv4(128, 0, 0, 10);
856 status = rte_lpm_delete(lpm, ip, depth);
857 TEST_LPM_ASSERT(status == 0);
859 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
860 TEST_LPM_ASSERT(status == -ENOENT);
868 * Add an extended rule (i.e. depth greater than 24, lookup (hit), delete,
869 * lookup (miss) in a for loop of 1000 times. This will check tbl8 extension
879 struct rte_lpm *lpm = NULL;
880 struct rte_lpm_config config;
882 config.max_rules = MAX_RULES;
883 config.number_tbl8s = NUMBER_TBL8S;
885 uint32_t ip, i, next_hop_add, next_hop_return;
889 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
890 TEST_LPM_ASSERT(lpm != NULL);
892 ip = IPv4(128, 0, 0, 0);
896 for (i = 0; i < 1000; i++) {
897 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
898 TEST_LPM_ASSERT(status == 0);
900 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
901 TEST_LPM_ASSERT((status == 0) &&
902 (next_hop_return == next_hop_add));
904 ipx4 = vect_set_epi32(ip, ip + 1, ip, ip - 1);
905 rte_lpm_lookupx4(lpm, ipx4, hop, UINT32_MAX);
906 TEST_LPM_ASSERT(hop[0] == UINT32_MAX);
907 TEST_LPM_ASSERT(hop[1] == next_hop_add);
908 TEST_LPM_ASSERT(hop[2] == UINT32_MAX);
909 TEST_LPM_ASSERT(hop[3] == next_hop_add);
911 status = rte_lpm_delete(lpm, ip, depth);
912 TEST_LPM_ASSERT(status == 0);
914 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
915 TEST_LPM_ASSERT(status == -ENOENT);
924 * Add a rule to tbl24, lookup (hit), then add a rule that will extend this
925 * tbl24 entry, lookup (hit). delete the rule that caused the tbl24 extension,
926 * lookup (miss) and repeat for loop of 1000 times. This will check tbl8
927 * extension and contraction.
934 struct rte_lpm *lpm = NULL;
935 struct rte_lpm_config config;
937 config.max_rules = MAX_RULES;
938 config.number_tbl8s = NUMBER_TBL8S;
940 uint32_t ip, i, next_hop_add_1, next_hop_add_2, next_hop_return;
944 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
945 TEST_LPM_ASSERT(lpm != NULL);
947 ip = IPv4(128, 0, 0, 0);
949 next_hop_add_1 = 100;
951 status = rte_lpm_add(lpm, ip, depth, next_hop_add_1);
952 TEST_LPM_ASSERT(status == 0);
954 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
955 TEST_LPM_ASSERT((status == 0) && (next_hop_return == next_hop_add_1));
958 next_hop_add_2 = 101;
960 for (i = 0; i < 1000; i++) {
961 status = rte_lpm_add(lpm, ip, depth, next_hop_add_2);
962 TEST_LPM_ASSERT(status == 0);
964 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
965 TEST_LPM_ASSERT((status == 0) &&
966 (next_hop_return == next_hop_add_2));
968 status = rte_lpm_delete(lpm, ip, depth);
969 TEST_LPM_ASSERT(status == 0);
971 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
972 TEST_LPM_ASSERT((status == 0) &&
973 (next_hop_return == next_hop_add_1));
978 status = rte_lpm_delete(lpm, ip, depth);
979 TEST_LPM_ASSERT(status == 0);
981 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
982 TEST_LPM_ASSERT(status == -ENOENT);
990 * Fore TBL8 extension exhaustion. Add 256 rules that require a tbl8 extension.
991 * No more tbl8 extensions will be allowed. Now add one more rule that required
992 * a tbl8 extension and get fail.
998 /* We only use depth = 32 in the loop below so we must make sure
999 * that we have enough storage for all rules at that depth*/
1001 struct rte_lpm *lpm = NULL;
1002 struct rte_lpm_config config;
1004 config.max_rules = 256 * 32;
1005 config.number_tbl8s = NUMBER_TBL8S;
1007 uint32_t ip, next_hop_add, next_hop_return;
1011 /* Add enough space for 256 rules for every depth */
1012 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
1013 TEST_LPM_ASSERT(lpm != NULL);
1017 ip = IPv4(0, 0, 0, 0);
1019 /* Add 256 rules that require a tbl8 extension */
1020 for (; ip <= IPv4(0, 0, 255, 0); ip += 256) {
1021 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
1022 TEST_LPM_ASSERT(status == 0);
1024 status = rte_lpm_lookup(lpm, ip, &next_hop_return);
1025 TEST_LPM_ASSERT((status == 0) &&
1026 (next_hop_return == next_hop_add));
1029 /* All tbl8 extensions have been used above. Try to add one more and
1031 ip = IPv4(1, 0, 0, 0);
1034 status = rte_lpm_add(lpm, ip, depth, next_hop_add);
1035 TEST_LPM_ASSERT(status < 0);
1043 * Sequence of operations for find existing lpm table
1046 * - find existing table: hit
1047 * - find non-existing table: miss
1053 struct rte_lpm *lpm = NULL, *result = NULL;
1054 struct rte_lpm_config config;
1056 config.max_rules = 256 * 32;
1057 config.number_tbl8s = NUMBER_TBL8S;
1061 lpm = rte_lpm_create("lpm_find_existing", SOCKET_ID_ANY, &config);
1062 TEST_LPM_ASSERT(lpm != NULL);
1064 /* Try to find existing lpm */
1065 result = rte_lpm_find_existing("lpm_find_existing");
1066 TEST_LPM_ASSERT(result == lpm);
1068 /* Try to find non-existing lpm */
1069 result = rte_lpm_find_existing("lpm_find_non_existing");
1070 TEST_LPM_ASSERT(result == NULL);
1073 rte_lpm_delete_all(lpm);
1080 * test failure condition of overloading the tbl8 so no more will fit
1081 * Check we get an error return value in that case
1087 struct rte_lpm_config config;
1089 config.max_rules = 256 * 32;
1090 config.number_tbl8s = NUMBER_TBL8S;
1092 struct rte_lpm *lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
1094 /* ip loops through all possibilities for top 24 bits of address */
1095 for (ip = 0; ip < 0xFFFFFF; ip++) {
1096 /* add an entry within a different tbl8 each time, since
1097 * depth >24 and the top 24 bits are different */
1098 if (rte_lpm_add(lpm, (ip << 8) + 0xF0, 30, 0) < 0)
1102 if (ip != NUMBER_TBL8S) {
1103 printf("Error, unexpected failure with filling tbl8 groups\n");
1104 printf("Failed after %u additions, expected after %u\n",
1105 (unsigned)ip, (unsigned)NUMBER_TBL8S);
1113 * Test for overwriting of tbl8:
1114 * - add rule /32 and lookup
1115 * - add new rule /24 and lookup
1116 * - add third rule /25 and lookup
1117 * - lookup /32 and /24 rule to ensure the table has not been overwritten.
1122 struct rte_lpm *lpm = NULL;
1123 struct rte_lpm_config config;
1125 config.max_rules = MAX_RULES;
1126 config.number_tbl8s = NUMBER_TBL8S;
1128 const uint32_t ip_10_32 = IPv4(10, 10, 10, 2);
1129 const uint32_t ip_10_24 = IPv4(10, 10, 10, 0);
1130 const uint32_t ip_20_25 = IPv4(10, 10, 20, 2);
1131 const uint8_t d_ip_10_32 = 32,
1134 const uint32_t next_hop_ip_10_32 = 100,
1135 next_hop_ip_10_24 = 105,
1136 next_hop_ip_20_25 = 111;
1137 uint32_t next_hop_return = 0;
1140 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
1141 TEST_LPM_ASSERT(lpm != NULL);
1143 if ((status = rte_lpm_add(lpm, ip_10_32, d_ip_10_32,
1144 next_hop_ip_10_32)) < 0)
1147 status = rte_lpm_lookup(lpm, ip_10_32, &next_hop_return);
1148 uint32_t test_hop_10_32 = next_hop_return;
1149 TEST_LPM_ASSERT(status == 0);
1150 TEST_LPM_ASSERT(next_hop_return == next_hop_ip_10_32);
1152 if ((status = rte_lpm_add(lpm, ip_10_24, d_ip_10_24,
1153 next_hop_ip_10_24)) < 0)
1156 status = rte_lpm_lookup(lpm, ip_10_24, &next_hop_return);
1157 uint32_t test_hop_10_24 = next_hop_return;
1158 TEST_LPM_ASSERT(status == 0);
1159 TEST_LPM_ASSERT(next_hop_return == next_hop_ip_10_24);
1161 if ((status = rte_lpm_add(lpm, ip_20_25, d_ip_20_25,
1162 next_hop_ip_20_25)) < 0)
1165 status = rte_lpm_lookup(lpm, ip_20_25, &next_hop_return);
1166 uint32_t test_hop_20_25 = next_hop_return;
1167 TEST_LPM_ASSERT(status == 0);
1168 TEST_LPM_ASSERT(next_hop_return == next_hop_ip_20_25);
1170 if (test_hop_10_32 == test_hop_10_24) {
1171 printf("Next hop return equal\n");
1175 if (test_hop_10_24 == test_hop_20_25) {
1176 printf("Next hop return equal\n");
1180 status = rte_lpm_lookup(lpm, ip_10_32, &next_hop_return);
1181 TEST_LPM_ASSERT(status == 0);
1182 TEST_LPM_ASSERT(next_hop_return == next_hop_ip_10_32);
1184 status = rte_lpm_lookup(lpm, ip_10_24, &next_hop_return);
1185 TEST_LPM_ASSERT(status == 0);
1186 TEST_LPM_ASSERT(next_hop_return == next_hop_ip_10_24);
1194 * Test for recycle of tbl8
1195 * - step 1: add a rule with depth=28 (> 24)
1196 * - step 2: add a rule with same 24-bit prefix and depth=23 (< 24)
1197 * - step 3: delete the first rule
1198 * - step 4: check tbl8 is freed
1199 * - step 5: add a rule same as the first one (depth=28)
1200 * - step 6: check same tbl8 is allocated
1201 * - step 7: add a rule with same 24-bit prefix and depth=24
1202 * - step 8: delete the rule (depth=28) added in step 5
1203 * - step 9: check tbl8 is freed
1204 * - step 10: add a rule with same 24-bit prefix and depth = 28
1205 * - setp 11: check same tbl8 is allocated again
1210 #define group_idx next_hop
1211 struct rte_lpm *lpm = NULL;
1212 struct rte_lpm_config config;
1213 uint32_t ip, next_hop;
1215 uint32_t tbl8_group_index;
1217 config.max_rules = MAX_RULES;
1218 config.number_tbl8s = NUMBER_TBL8S;
1221 lpm = rte_lpm_create(__func__, SOCKET_ID_ANY, &config);
1222 TEST_LPM_ASSERT(lpm != NULL);
1224 ip = IPv4(192, 168, 100, 100);
1227 rte_lpm_add(lpm, ip, depth, next_hop);
1229 TEST_LPM_ASSERT(lpm->tbl24[ip>>8].valid_group);
1230 tbl8_group_index = lpm->tbl24[ip>>8].group_idx;
1234 rte_lpm_add(lpm, ip, depth, next_hop);
1235 TEST_LPM_ASSERT(lpm->tbl24[ip>>8].valid_group);
1238 rte_lpm_delete(lpm, ip, depth);
1240 TEST_LPM_ASSERT(!lpm->tbl24[ip>>8].valid_group);
1243 rte_lpm_add(lpm, ip, depth, next_hop);
1245 TEST_LPM_ASSERT(lpm->tbl24[ip>>8].valid_group);
1246 TEST_LPM_ASSERT(tbl8_group_index == lpm->tbl24[ip>>8].group_idx);
1250 rte_lpm_add(lpm, ip, depth, next_hop);
1251 TEST_LPM_ASSERT(lpm->tbl24[ip>>8].valid_group);
1254 rte_lpm_delete(lpm, ip, depth);
1256 TEST_LPM_ASSERT(!lpm->tbl24[ip>>8].valid_group);
1259 rte_lpm_add(lpm, ip, depth, next_hop);
1261 TEST_LPM_ASSERT(lpm->tbl24[ip>>8].valid_group);
1262 TEST_LPM_ASSERT(tbl8_group_index == lpm->tbl24[ip>>8].group_idx);
1270 * Do all unit tests.
1277 int status, global_status = 0;
1279 for (i = 0; i < NUM_LPM_TESTS; i++) {
1280 status = tests[i]();
1282 printf("ERROR: LPM Test %u: FAIL\n", i);
1283 global_status = status;
1287 return global_status;
1290 REGISTER_TEST_COMMAND(lpm_autotest, test_lpm);