1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3 * Copyright(c) 2019 Intel Corporation
11 #include <rte_cycles.h>
12 #include <rte_random.h>
13 #include <rte_branch_prediction.h>
18 #include "test_xmmt_ops.h"
20 #define TEST_FIB_ASSERT(cond) do { \
22 printf("Error at line %d:\n", __LINE__); \
27 #define ITERATIONS (1 << 10)
28 #define BATCH_SIZE (1 << 12)
31 #define MAX_RULE_NUM (1200000)
38 static struct route_rule large_route_table[MAX_RULE_NUM];
40 static uint32_t num_route_entries;
41 #define NUM_ROUTE_ENTRIES num_route_entries
48 #define RTE_FIB_MAX_DEPTH 32
49 /* struct route_rule_count defines the total number of rules in following a/b/c
50 * each item in a[]/b[]/c[] is the number of common IP address class A/B/C, not
51 * including the ones for private local network.
53 struct route_rule_count {
54 uint32_t a[RTE_FIB_MAX_DEPTH];
55 uint32_t b[RTE_FIB_MAX_DEPTH];
56 uint32_t c[RTE_FIB_MAX_DEPTH];
59 /* All following numbers of each depth of each common IP class are just
60 * got from previous large constant table in app/test/test_lpm_routes.h .
61 * In order to match similar performance, they keep same depth and IP
62 * address coverage as previous constant table. These numbers don't
63 * include any private local IP address. As previous large const rule
64 * table was just dumped from a real router, there are no any IP address
67 static struct route_rule_count rule_count = {
68 .a = { /* IP class A in which the most significant bit is 0 */
84 3856, /* depth = 16 */
85 3268, /* depth = 17 */
86 5662, /* depth = 18 */
87 17301, /* depth = 19 */
88 22226, /* depth = 20 */
89 11147, /* depth = 21 */
90 16746, /* depth = 22 */
91 17120, /* depth = 23 */
92 77578, /* depth = 24 */
95 1107, /* depth = 27 */
96 1121, /* depth = 28 */
97 2316, /* depth = 29 */
102 .b = { /* IP class A in which the most 2 significant bits are 10 */
114 168, /* depth = 12 */
115 305, /* depth = 13 */
116 569, /* depth = 14 */
117 1129, /* depth = 15 */
118 50800, /* depth = 16 */
119 1645, /* depth = 17 */
120 1820, /* depth = 18 */
121 3506, /* depth = 19 */
122 3258, /* depth = 20 */
123 3424, /* depth = 21 */
124 4971, /* depth = 22 */
125 6885, /* depth = 23 */
126 39771, /* depth = 24 */
127 424, /* depth = 25 */
128 170, /* depth = 26 */
129 433, /* depth = 27 */
131 366, /* depth = 29 */
132 377, /* depth = 30 */
136 .c = { /* IP class A in which the most 3 significant bits are 110 */
149 237, /* depth = 13 */
150 1007, /* depth = 14 */
151 1717, /* depth = 15 */
152 14663, /* depth = 16 */
153 8070, /* depth = 17 */
154 16185, /* depth = 18 */
155 48261, /* depth = 19 */
156 36870, /* depth = 20 */
157 33960, /* depth = 21 */
158 50638, /* depth = 22 */
159 61422, /* depth = 23 */
160 466549, /* depth = 24 */
161 1829, /* depth = 25 */
162 4824, /* depth = 26 */
163 4927, /* depth = 27 */
164 5914, /* depth = 28 */
165 10254, /* depth = 29 */
166 4905, /* depth = 30 */
172 static void generate_random_rule_prefix(uint32_t ip_class, uint8_t depth)
174 /* IP address class A, the most significant bit is 0 */
175 #define IP_HEAD_MASK_A 0x00000000
176 #define IP_HEAD_BIT_NUM_A 1
178 /* IP address class B, the most significant 2 bits are 10 */
179 #define IP_HEAD_MASK_B 0x80000000
180 #define IP_HEAD_BIT_NUM_B 2
182 /* IP address class C, the most significant 3 bits are 110 */
183 #define IP_HEAD_MASK_C 0xC0000000
184 #define IP_HEAD_BIT_NUM_C 3
186 uint32_t class_depth;
191 uint32_t fixed_bit_num;
192 uint32_t ip_head_mask;
195 struct route_rule *ptr_rule;
197 if (ip_class == IP_CLASS_A) { /* IP Address class A */
198 fixed_bit_num = IP_HEAD_BIT_NUM_A;
199 ip_head_mask = IP_HEAD_MASK_A;
200 rule_num = rule_count.a[depth - 1];
201 } else if (ip_class == IP_CLASS_B) { /* IP Address class B */
202 fixed_bit_num = IP_HEAD_BIT_NUM_B;
203 ip_head_mask = IP_HEAD_MASK_B;
204 rule_num = rule_count.b[depth - 1];
205 } else { /* IP Address class C */
206 fixed_bit_num = IP_HEAD_BIT_NUM_C;
207 ip_head_mask = IP_HEAD_MASK_C;
208 rule_num = rule_count.c[depth - 1];
214 /* the number of rest bits which don't include the most significant
215 * fixed bits for this IP address class
217 class_depth = depth - fixed_bit_num;
219 /* range is the maximum number of rules for this depth and
220 * this IP address class
222 range = 1 << class_depth;
224 /* only mask the most depth significant generated bits
225 * except fixed bits for IP address class
229 /* Widen coverage of IP address in generated rules */
230 if (range <= rule_num)
233 step = round((double)range / rule_num);
235 /* Only generate rest bits except the most significant
236 * fixed bits for IP address class
238 start = lrand48() & mask;
239 ptr_rule = &large_route_table[num_route_entries];
240 for (k = 0; k < rule_num; k++) {
241 ptr_rule->ip = (start << (RTE_FIB_MAX_DEPTH - depth))
243 ptr_rule->depth = depth;
245 start = (start + step) & mask;
247 num_route_entries += rule_num;
250 static void insert_rule_in_random_pos(uint32_t ip, uint8_t depth)
254 struct route_rule tmp;
259 } while ((try_count < 10) && (pos > num_route_entries));
261 if ((pos > num_route_entries) || (pos >= MAX_RULE_NUM))
262 pos = num_route_entries >> 1;
264 tmp = large_route_table[pos];
265 large_route_table[pos].ip = ip;
266 large_route_table[pos].depth = depth;
267 if (num_route_entries < MAX_RULE_NUM)
268 large_route_table[num_route_entries++] = tmp;
271 static void generate_large_route_rule_table(void)
276 num_route_entries = 0;
277 memset(large_route_table, 0, sizeof(large_route_table));
279 for (ip_class = IP_CLASS_A; ip_class <= IP_CLASS_C; ip_class++) {
280 for (depth = 1; depth <= RTE_FIB_MAX_DEPTH; depth++)
281 generate_random_rule_prefix(ip_class, depth);
284 /* Add following rules to keep same as previous large constant table,
285 * they are 4 rules with private local IP address and 1 all-zeros prefix
288 insert_rule_in_random_pos(RTE_IPV4(0, 0, 0, 0), 8);
289 insert_rule_in_random_pos(RTE_IPV4(10, 2, 23, 147), 32);
290 insert_rule_in_random_pos(RTE_IPV4(192, 168, 100, 10), 24);
291 insert_rule_in_random_pos(RTE_IPV4(192, 168, 25, 100), 24);
292 insert_rule_in_random_pos(RTE_IPV4(192, 168, 129, 124), 32);
296 print_route_distribution(const struct route_rule *table, uint32_t n)
300 printf("Route distribution per prefix width:\n");
301 printf("DEPTH QUANTITY (PERCENT)\n");
302 printf("---------------------------\n");
305 for (i = 1; i <= 32; i++) {
306 unsigned int depth_counter = 0;
309 for (j = 0; j < n; j++)
310 if (table[j].depth == (uint8_t) i)
313 percent_hits = ((double)depth_counter)/((double)n) * 100;
314 printf("%.2u%15u (%.2f)\n", i, depth_counter, percent_hits);
322 struct rte_fib *fib = NULL;
323 struct rte_fib_conf config;
325 config.max_routes = 2000000;
326 config.type = RTE_FIB_DIR24_8;
327 config.default_nh = 0;
328 config.dir24_8.nh_sz = RTE_FIB_DIR24_8_4B;
329 config.dir24_8.num_tbl8 = 65535;
330 uint64_t begin, total_time;
332 uint32_t next_hop_add = 0xAA;
336 rte_srand(rte_rdtsc());
338 generate_large_route_rule_table();
340 printf("No. routes = %u\n", (unsigned int) NUM_ROUTE_ENTRIES);
342 print_route_distribution(large_route_table,
343 (uint32_t) NUM_ROUTE_ENTRIES);
345 fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
346 TEST_FIB_ASSERT(fib != NULL);
351 for (i = 0; i < NUM_ROUTE_ENTRIES; i++) {
352 if (rte_fib_add(fib, large_route_table[i].ip,
353 large_route_table[i].depth, next_hop_add) == 0)
357 total_time = rte_rdtsc() - begin;
359 printf("Unique added entries = %d\n", status);
361 printf("Average FIB Add: %g cycles\n",
362 (double)total_time / NUM_ROUTE_ENTRIES);
364 /* Measure bulk Lookup */
367 for (i = 0; i < ITERATIONS; i++) {
368 static uint32_t ip_batch[BATCH_SIZE];
369 uint64_t next_hops[BULK_SIZE];
371 /* Create array of random IP addresses */
372 for (j = 0; j < BATCH_SIZE; j++)
373 ip_batch[j] = rte_rand();
375 /* Lookup per batch */
377 for (j = 0; j < BATCH_SIZE; j += BULK_SIZE) {
379 rte_fib_lookup_bulk(fib, &ip_batch[j], next_hops,
381 for (k = 0; k < BULK_SIZE; k++)
382 if (unlikely(!(next_hops[k] != 0)))
386 total_time += rte_rdtsc() - begin;
388 printf("BULK FIB Lookup: %.1f cycles (fails = %.1f%%)\n",
389 (double)total_time / ((double)ITERATIONS * BATCH_SIZE),
390 (count * 100.0) / (double)(ITERATIONS * BATCH_SIZE));
395 for (i = 0; i < NUM_ROUTE_ENTRIES; i++) {
396 /* rte_lpm_delete(lpm, ip, depth) */
397 status += rte_fib_delete(fib, large_route_table[i].ip,
398 large_route_table[i].depth);
401 total_time += rte_rdtsc() - begin;
403 printf("Average FIB Delete: %g cycles\n",
404 (double)total_time / NUM_ROUTE_ENTRIES);
411 REGISTER_TEST_COMMAND(fib_perf_autotest, test_fib_perf);