blob: 8b13230b4c468f1e4f26d87899f8081acf980158 [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Alexei Starovoitov26e90932016-03-08 15:07:54 -08002/* Copyright (c) 2016 Facebook
Alexei Starovoitov26e90932016-03-08 15:07:54 -08003 */
4#define _GNU_SOURCE
5#include <sched.h>
6#include <stdio.h>
7#include <sys/types.h>
8#include <asm/unistd.h>
9#include <unistd.h>
10#include <assert.h>
11#include <sys/wait.h>
12#include <stdlib.h>
13#include <signal.h>
Alexei Starovoitov26e90932016-03-08 15:07:54 -080014#include <string.h>
15#include <time.h>
Naveen N. Rao77e63532016-04-04 22:31:32 +053016#include <sys/resource.h>
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -070017#include <arpa/inet.h>
18#include <errno.h>
19
Jakub Kicinski2bf3e2e2018-05-14 22:35:02 -070020#include <bpf/bpf.h>
Daniel T. Leecc7f6412020-07-08 03:48:54 +090021#include <bpf/libbpf.h>
Alexei Starovoitov26e90932016-03-08 15:07:54 -080022
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070023#define TEST_BIT(t) (1U << (t))
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070024#define MAX_NR_CPUS 1024
Alexei Starovoitov26e90932016-03-08 15:07:54 -080025
26static __u64 time_get_ns(void)
27{
28 struct timespec ts;
29
30 clock_gettime(CLOCK_MONOTONIC, &ts);
31 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
32}
33
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070034enum test_type {
35 HASH_PREALLOC,
36 PERCPU_HASH_PREALLOC,
37 HASH_KMALLOC,
38 PERCPU_HASH_KMALLOC,
39 LRU_HASH_PREALLOC,
40 NOCOMMON_LRU_HASH_PREALLOC,
41 LPM_KMALLOC,
42 HASH_LOOKUP,
43 ARRAY_LOOKUP,
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070044 INNER_LRU_HASH_PREALLOC,
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -070045 LRU_HASH_LOOKUP,
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070046 NR_TESTS,
47};
48
49const char *test_map_names[NR_TESTS] = {
50 [HASH_PREALLOC] = "hash_map",
51 [PERCPU_HASH_PREALLOC] = "percpu_hash_map",
52 [HASH_KMALLOC] = "hash_map_alloc",
53 [PERCPU_HASH_KMALLOC] = "percpu_hash_map_alloc",
54 [LRU_HASH_PREALLOC] = "lru_hash_map",
55 [NOCOMMON_LRU_HASH_PREALLOC] = "nocommon_lru_hash_map",
56 [LPM_KMALLOC] = "lpm_trie_map_alloc",
57 [HASH_LOOKUP] = "hash_map",
58 [ARRAY_LOOKUP] = "array_map",
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070059 [INNER_LRU_HASH_PREALLOC] = "inner_lru_hash_map",
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -070060 [LRU_HASH_LOOKUP] = "lru_hash_lookup_map",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070061};
Alexei Starovoitov26e90932016-03-08 15:07:54 -080062
Daniel T. Leecc7f6412020-07-08 03:48:54 +090063enum map_idx {
64 array_of_lru_hashs_idx,
65 hash_map_alloc_idx,
66 lru_hash_lookup_idx,
67 NR_IDXES,
68};
69
70static int map_fd[NR_IDXES];
71
Alexei Starovoitov26e90932016-03-08 15:07:54 -080072static int test_flags = ~0;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070073static uint32_t num_map_entries;
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070074static uint32_t inner_lru_hash_size;
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -070075static int lru_hash_lookup_test_entries = 32;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070076static uint32_t max_cnt = 1000000;
77
78static int check_test_flags(enum test_type t)
79{
80 return test_flags & TEST_BIT(t);
81}
Alexei Starovoitov26e90932016-03-08 15:07:54 -080082
83static void test_hash_prealloc(int cpu)
84{
85 __u64 start_time;
86 int i;
87
88 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070089 for (i = 0; i < max_cnt; i++)
Alexei Starovoitov26e90932016-03-08 15:07:54 -080090 syscall(__NR_getuid);
91 printf("%d:hash_map_perf pre-alloc %lld events per sec\n",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070092 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
Alexei Starovoitov26e90932016-03-08 15:07:54 -080093}
94
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -070095static int pre_test_lru_hash_lookup(int tasks)
96{
97 int fd = map_fd[lru_hash_lookup_idx];
98 uint32_t key;
99 long val = 1;
100 int ret;
101
102 if (num_map_entries > lru_hash_lookup_test_entries)
103 lru_hash_lookup_test_entries = num_map_entries;
104
105 /* Populate the lru_hash_map for LRU_HASH_LOOKUP perf test.
106 *
107 * It is fine that the user requests for a map with
108 * num_map_entries < 32 and some of the later lru hash lookup
109 * may return not found. For LRU map, we are not interested
110 * in such small map performance.
111 */
112 for (key = 0; key < lru_hash_lookup_test_entries; key++) {
113 ret = bpf_map_update_elem(fd, &key, &val, BPF_NOEXIST);
114 if (ret)
115 return ret;
116 }
117
118 return 0;
119}
120
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700121static void do_test_lru(enum test_type test, int cpu)
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800122{
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700123 static int inner_lru_map_fds[MAX_NR_CPUS];
124
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700125 struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
126 const char *test_name;
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800127 __u64 start_time;
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700128 int i, ret;
129
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900130 if (test == INNER_LRU_HASH_PREALLOC && cpu) {
131 /* If CPU is not 0, create inner_lru hash map and insert the fd
132 * value into the array_of_lru_hash map. In case of CPU 0,
133 * 'inner_lru_hash_map' was statically inserted on the map init
134 */
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700135 int outer_fd = map_fd[array_of_lru_hashs_idx];
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700136 unsigned int mycpu, mynode;
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700137
138 assert(cpu < MAX_NR_CPUS);
139
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900140 ret = syscall(__NR_getcpu, &mycpu, &mynode, NULL);
141 assert(!ret);
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700142
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900143 inner_lru_map_fds[cpu] =
144 bpf_create_map_node(BPF_MAP_TYPE_LRU_HASH,
145 test_map_names[INNER_LRU_HASH_PREALLOC],
146 sizeof(uint32_t),
147 sizeof(long),
148 inner_lru_hash_size, 0,
149 mynode);
150 if (inner_lru_map_fds[cpu] == -1) {
151 printf("cannot create BPF_MAP_TYPE_LRU_HASH %s(%d)\n",
152 strerror(errno), errno);
153 exit(1);
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700154 }
155
156 ret = bpf_map_update_elem(outer_fd, &cpu,
157 &inner_lru_map_fds[cpu],
158 BPF_ANY);
159 if (ret) {
160 printf("cannot update ARRAY_OF_LRU_HASHS with key:%u. %s(%d)\n",
161 cpu, strerror(errno), errno);
162 exit(1);
163 }
164 }
165
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700166 in6.sin6_addr.s6_addr16[0] = 0xdead;
167 in6.sin6_addr.s6_addr16[1] = 0xbeef;
168
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700169 if (test == LRU_HASH_PREALLOC) {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700170 test_name = "lru_hash_map_perf";
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700171 in6.sin6_addr.s6_addr16[2] = 0;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700172 } else if (test == NOCOMMON_LRU_HASH_PREALLOC) {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700173 test_name = "nocommon_lru_hash_map_perf";
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700174 in6.sin6_addr.s6_addr16[2] = 1;
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700175 } else if (test == INNER_LRU_HASH_PREALLOC) {
176 test_name = "inner_lru_hash_map_perf";
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700177 in6.sin6_addr.s6_addr16[2] = 2;
178 } else if (test == LRU_HASH_LOOKUP) {
179 test_name = "lru_hash_lookup_perf";
180 in6.sin6_addr.s6_addr16[2] = 3;
181 in6.sin6_addr.s6_addr32[3] = 0;
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700182 } else {
183 assert(0);
184 }
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800185
186 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700187 for (i = 0; i < max_cnt; i++) {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700188 ret = connect(-1, (const struct sockaddr *)&in6, sizeof(in6));
189 assert(ret == -1 && errno == EBADF);
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700190 if (in6.sin6_addr.s6_addr32[3] <
191 lru_hash_lookup_test_entries - 32)
192 in6.sin6_addr.s6_addr32[3] += 32;
193 else
194 in6.sin6_addr.s6_addr32[3] = 0;
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700195 }
196 printf("%d:%s pre-alloc %lld events per sec\n",
197 cpu, test_name,
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700198 max_cnt * 1000000000ll / (time_get_ns() - start_time));
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800199}
200
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700201static void test_lru_hash_prealloc(int cpu)
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800202{
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700203 do_test_lru(LRU_HASH_PREALLOC, cpu);
204}
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800205
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700206static void test_nocommon_lru_hash_prealloc(int cpu)
207{
208 do_test_lru(NOCOMMON_LRU_HASH_PREALLOC, cpu);
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800209}
210
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700211static void test_inner_lru_hash_prealloc(int cpu)
212{
213 do_test_lru(INNER_LRU_HASH_PREALLOC, cpu);
214}
215
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700216static void test_lru_hash_lookup(int cpu)
217{
218 do_test_lru(LRU_HASH_LOOKUP, cpu);
219}
220
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800221static void test_percpu_hash_prealloc(int cpu)
222{
223 __u64 start_time;
224 int i;
225
226 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700227 for (i = 0; i < max_cnt; i++)
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800228 syscall(__NR_geteuid);
229 printf("%d:percpu_hash_map_perf pre-alloc %lld events per sec\n",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700230 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800231}
232
233static void test_hash_kmalloc(int cpu)
234{
235 __u64 start_time;
236 int i;
237
238 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700239 for (i = 0; i < max_cnt; i++)
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800240 syscall(__NR_getgid);
241 printf("%d:hash_map_perf kmalloc %lld events per sec\n",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700242 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800243}
244
245static void test_percpu_hash_kmalloc(int cpu)
246{
247 __u64 start_time;
248 int i;
249
250 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700251 for (i = 0; i < max_cnt; i++)
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800252 syscall(__NR_getegid);
253 printf("%d:percpu_hash_map_perf kmalloc %lld events per sec\n",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700254 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800255}
256
David Herrmannb8a943e2017-01-21 17:26:13 +0100257static void test_lpm_kmalloc(int cpu)
258{
259 __u64 start_time;
260 int i;
261
262 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700263 for (i = 0; i < max_cnt; i++)
David Herrmannb8a943e2017-01-21 17:26:13 +0100264 syscall(__NR_gettid);
265 printf("%d:lpm_perf kmalloc %lld events per sec\n",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700266 cpu, max_cnt * 1000000000ll / (time_get_ns() - start_time));
David Herrmannb8a943e2017-01-21 17:26:13 +0100267}
268
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700269static void test_hash_lookup(int cpu)
270{
271 __u64 start_time;
272 int i;
273
274 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700275 for (i = 0; i < max_cnt; i++)
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700276 syscall(__NR_getpgid, 0);
277 printf("%d:hash_lookup %lld lookups per sec\n",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700278 cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time));
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700279}
280
281static void test_array_lookup(int cpu)
282{
283 __u64 start_time;
284 int i;
285
286 start_time = time_get_ns();
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700287 for (i = 0; i < max_cnt; i++)
Joel Fernandes95ec66962017-09-20 09:11:56 -0700288 syscall(__NR_getppid, 0);
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700289 printf("%d:array_lookup %lld lookups per sec\n",
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700290 cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time));
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700291}
292
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700293typedef int (*pre_test_func)(int tasks);
294const pre_test_func pre_test_funcs[] = {
295 [LRU_HASH_LOOKUP] = pre_test_lru_hash_lookup,
296};
297
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700298typedef void (*test_func)(int cpu);
299const test_func test_funcs[] = {
300 [HASH_PREALLOC] = test_hash_prealloc,
301 [PERCPU_HASH_PREALLOC] = test_percpu_hash_prealloc,
302 [HASH_KMALLOC] = test_hash_kmalloc,
303 [PERCPU_HASH_KMALLOC] = test_percpu_hash_kmalloc,
304 [LRU_HASH_PREALLOC] = test_lru_hash_prealloc,
305 [NOCOMMON_LRU_HASH_PREALLOC] = test_nocommon_lru_hash_prealloc,
306 [LPM_KMALLOC] = test_lpm_kmalloc,
307 [HASH_LOOKUP] = test_hash_lookup,
308 [ARRAY_LOOKUP] = test_array_lookup,
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700309 [INNER_LRU_HASH_PREALLOC] = test_inner_lru_hash_prealloc,
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700310 [LRU_HASH_LOOKUP] = test_lru_hash_lookup,
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700311};
312
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700313static int pre_test(int tasks)
314{
315 int i;
316
317 for (i = 0; i < NR_TESTS; i++) {
318 if (pre_test_funcs[i] && check_test_flags(i)) {
319 int ret = pre_test_funcs[i](tasks);
320
321 if (ret)
322 return ret;
323 }
324 }
325
326 return 0;
327}
328
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800329static void loop(int cpu)
330{
331 cpu_set_t cpuset;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700332 int i;
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800333
334 CPU_ZERO(&cpuset);
335 CPU_SET(cpu, &cpuset);
336 sched_setaffinity(0, sizeof(cpuset), &cpuset);
337
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700338 for (i = 0; i < NR_TESTS; i++) {
339 if (check_test_flags(i))
340 test_funcs[i](cpu);
341 }
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800342}
343
344static void run_perf_test(int tasks)
345{
346 pid_t pid[tasks];
347 int i;
348
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700349 assert(!pre_test(tasks));
350
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800351 for (i = 0; i < tasks; i++) {
352 pid[i] = fork();
353 if (pid[i] == 0) {
354 loop(i);
355 exit(0);
356 } else if (pid[i] == -1) {
357 printf("couldn't spawn #%d process\n", i);
358 exit(1);
359 }
360 }
361 for (i = 0; i < tasks; i++) {
362 int status;
363
364 assert(waitpid(pid[i], &status, 0) == pid[i]);
365 assert(status == 0);
366 }
367}
368
David Herrmannb8a943e2017-01-21 17:26:13 +0100369static void fill_lpm_trie(void)
370{
371 struct bpf_lpm_trie_key *key;
372 unsigned long value = 0;
373 unsigned int i;
374 int r;
375
376 key = alloca(sizeof(*key) + 4);
377 key->prefixlen = 32;
378
379 for (i = 0; i < 512; ++i) {
380 key->prefixlen = rand() % 33;
381 key->data[0] = rand() & 0xff;
382 key->data[1] = rand() & 0xff;
383 key->data[2] = rand() & 0xff;
384 key->data[3] = rand() & 0xff;
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900385 r = bpf_map_update_elem(map_fd[hash_map_alloc_idx],
386 key, &value, 0);
David Herrmannb8a943e2017-01-21 17:26:13 +0100387 assert(!r);
388 }
389
390 key->prefixlen = 32;
391 key->data[0] = 192;
392 key->data[1] = 168;
393 key->data[2] = 0;
394 key->data[3] = 1;
395 value = 128;
396
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900397 r = bpf_map_update_elem(map_fd[hash_map_alloc_idx], key, &value, 0);
David Herrmannb8a943e2017-01-21 17:26:13 +0100398 assert(!r);
399}
400
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900401static void fixup_map(struct bpf_object *obj)
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700402{
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900403 struct bpf_map *map;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700404 int i;
405
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900406 bpf_object__for_each_map(map, obj) {
407 const char *name = bpf_map__name(map);
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700408
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900409 /* Only change the max_entries for the enabled test(s) */
410 for (i = 0; i < NR_TESTS; i++) {
411 if (!strcmp(test_map_names[i], name) &&
412 (check_test_flags(i))) {
413 bpf_map__resize(map, num_map_entries);
414 continue;
415 }
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700416 }
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700417 }
418
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700419 inner_lru_hash_size = num_map_entries;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700420}
421
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800422int main(int argc, char **argv)
423{
424 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900425 int nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
426 struct bpf_link *links[8];
427 struct bpf_program *prog;
428 struct bpf_object *obj;
429 struct bpf_map *map;
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800430 char filename[256];
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900431 int i = 0;
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800432
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900433 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
434 perror("setrlimit(RLIMIT_MEMLOCK)");
435 return 1;
436 }
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800437
438 if (argc > 1)
439 test_flags = atoi(argv[1]) ? : test_flags;
440
441 if (argc > 2)
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900442 nr_cpus = atoi(argv[2]) ? : nr_cpus;
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800443
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700444 if (argc > 3)
445 num_map_entries = atoi(argv[3]);
446
447 if (argc > 4)
448 max_cnt = atoi(argv[4]);
449
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900450 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
451 obj = bpf_object__open_file(filename, NULL);
452 if (libbpf_get_error(obj)) {
453 fprintf(stderr, "ERROR: opening BPF object file failed\n");
454 return 0;
455 }
456
457 map = bpf_object__find_map_by_name(obj, "inner_lru_hash_map");
458 if (libbpf_get_error(map)) {
459 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
460 goto cleanup;
461 }
462
463 inner_lru_hash_size = bpf_map__max_entries(map);
464 if (!inner_lru_hash_size) {
465 fprintf(stderr, "ERROR: failed to get map attribute\n");
466 goto cleanup;
467 }
468
469 /* resize BPF map prior to loading */
470 if (num_map_entries > 0)
471 fixup_map(obj);
472
473 /* load BPF program */
474 if (bpf_object__load(obj)) {
475 fprintf(stderr, "ERROR: loading BPF object file failed\n");
476 goto cleanup;
477 }
478
479 map_fd[0] = bpf_object__find_map_fd_by_name(obj, "array_of_lru_hashs");
480 map_fd[1] = bpf_object__find_map_fd_by_name(obj, "hash_map_alloc");
481 map_fd[2] = bpf_object__find_map_fd_by_name(obj, "lru_hash_lookup_map");
482 if (map_fd[0] < 0 || map_fd[1] < 0 || map_fd[2] < 0) {
483 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
484 goto cleanup;
485 }
486
487 bpf_object__for_each_program(prog, obj) {
488 links[i] = bpf_program__attach(prog);
489 if (libbpf_get_error(links[i])) {
490 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
491 links[i] = NULL;
492 goto cleanup;
493 }
494 i++;
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800495 }
496
David Herrmannb8a943e2017-01-21 17:26:13 +0100497 fill_lpm_trie();
498
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900499 run_perf_test(nr_cpus);
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800500
Daniel T. Leecc7f6412020-07-08 03:48:54 +0900501cleanup:
502 for (i--; i >= 0; i--)
503 bpf_link__destroy(links[i]);
504
505 bpf_object__close(obj);
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800506 return 0;
507}