2 * Testsuite for eBPF maps
4 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
12 #include <linux/bpf.h>
20 /* sanity tests for map API */
21 static void test_hashmap_sanity(int i
, void *data
)
23 long long key
, next_key
, value
;
26 map_fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(key
), sizeof(value
), 2);
28 printf("failed to create hashmap '%s'\n", strerror(errno
));
34 /* insert key=1 element */
35 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_ANY
) == 0);
38 /* BPF_NOEXIST means: add new element if it doesn't exist */
39 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
40 /* key=1 already exists */
43 assert(bpf_update_elem(map_fd
, &key
, &value
, -1) == -1 && errno
== EINVAL
);
45 /* check that key=1 can be found */
46 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == 0 && value
== 1234);
49 /* check that key=2 is not found */
50 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == -1 && errno
== ENOENT
);
52 /* BPF_EXIST means: update existing element */
53 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_EXIST
) == -1 &&
54 /* key=2 is not there */
57 /* insert key=2 element */
58 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == 0);
60 /* key=1 and key=2 were inserted, check that key=0 cannot be inserted
61 * due to max_entries limit
64 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
67 /* check that key = 0 doesn't exist */
68 assert(bpf_delete_elem(map_fd
, &key
) == -1 && errno
== ENOENT
);
70 /* iterate over two elements */
71 assert(bpf_get_next_key(map_fd
, &key
, &next_key
) == 0 &&
72 (next_key
== 1 || next_key
== 2));
73 assert(bpf_get_next_key(map_fd
, &next_key
, &next_key
) == 0 &&
74 (next_key
== 1 || next_key
== 2));
75 assert(bpf_get_next_key(map_fd
, &next_key
, &next_key
) == -1 &&
78 /* delete both elements */
80 assert(bpf_delete_elem(map_fd
, &key
) == 0);
82 assert(bpf_delete_elem(map_fd
, &key
) == 0);
83 assert(bpf_delete_elem(map_fd
, &key
) == -1 && errno
== ENOENT
);
86 /* check that map is empty */
87 assert(bpf_get_next_key(map_fd
, &key
, &next_key
) == -1 &&
92 static void test_arraymap_sanity(int i
, void *data
)
94 int key
, next_key
, map_fd
;
97 map_fd
= bpf_create_map(BPF_MAP_TYPE_ARRAY
, sizeof(key
), sizeof(value
), 2);
99 printf("failed to create arraymap '%s'\n", strerror(errno
));
105 /* insert key=1 element */
106 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_ANY
) == 0);
109 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
112 /* check that key=1 can be found */
113 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == 0 && value
== 1234);
116 /* check that key=0 is also found and zero initialized */
117 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == 0 && value
== 0);
120 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
121 * due to max_entries limit
124 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_EXIST
) == -1 &&
127 /* check that key = 2 doesn't exist */
128 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == -1 && errno
== ENOENT
);
130 /* iterate over two elements */
131 assert(bpf_get_next_key(map_fd
, &key
, &next_key
) == 0 &&
133 assert(bpf_get_next_key(map_fd
, &next_key
, &next_key
) == 0 &&
135 assert(bpf_get_next_key(map_fd
, &next_key
, &next_key
) == -1 &&
138 /* delete shouldn't succeed */
140 assert(bpf_delete_elem(map_fd
, &key
) == -1 && errno
== EINVAL
);
145 #define MAP_SIZE (32 * 1024)
146 static void test_map_large(void)
153 int map_fd
, i
, value
;
155 /* allocate 4Mbyte of memory */
156 map_fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(key
), sizeof(value
),
159 printf("failed to create large map '%s'\n", strerror(errno
));
163 for (i
= 0; i
< MAP_SIZE
; i
++) {
164 key
= (struct bigkey
) {.c
= i
};
166 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == 0);
169 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
172 /* iterate through all elements */
173 for (i
= 0; i
< MAP_SIZE
; i
++)
174 assert(bpf_get_next_key(map_fd
, &key
, &key
) == 0);
175 assert(bpf_get_next_key(map_fd
, &key
, &key
) == -1 && errno
== ENOENT
);
178 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == 0 && value
== 0);
180 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == -1 && errno
== ENOENT
);
185 /* fork N children and wait for them to complete */
186 static void run_parallel(int tasks
, void (*fn
)(int i
, void *data
), void *data
)
191 for (i
= 0; i
< tasks
; i
++) {
196 } else if (pid
[i
] == -1) {
197 printf("couldn't spawn #%d process\n", i
);
201 for (i
= 0; i
< tasks
; i
++) {
204 assert(waitpid(pid
[i
], &status
, 0) == pid
[i
]);
209 static void test_map_stress(void)
211 run_parallel(100, test_hashmap_sanity
, NULL
);
212 run_parallel(100, test_arraymap_sanity
, NULL
);
218 static void do_work(int fn
, void *data
)
220 int map_fd
= ((int *)data
)[0];
221 int do_update
= ((int *)data
)[1];
225 for (i
= fn
; i
< MAP_SIZE
; i
+= TASKS
) {
228 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == 0);
230 assert(bpf_delete_elem(map_fd
, &key
) == 0);
234 static void test_map_parallel(void)
236 int i
, map_fd
, key
= 0, value
= 0;
239 map_fd
= bpf_create_map(BPF_MAP_TYPE_HASH
, sizeof(key
), sizeof(value
),
242 printf("failed to create map for parallel test '%s'\n",
249 /* use the same map_fd in children to add elements to this map
250 * child_0 adds key=0, key=1024, key=2048, ...
251 * child_1 adds key=1, key=1025, key=2049, ...
252 * child_1023 adds key=1023, ...
254 run_parallel(TASKS
, do_work
, data
);
256 /* check that key=0 is already there */
257 assert(bpf_update_elem(map_fd
, &key
, &value
, BPF_NOEXIST
) == -1 &&
260 /* check that all elements were inserted */
262 for (i
= 0; i
< MAP_SIZE
; i
++)
263 assert(bpf_get_next_key(map_fd
, &key
, &key
) == 0);
264 assert(bpf_get_next_key(map_fd
, &key
, &key
) == -1 && errno
== ENOENT
);
266 /* another check for all elements */
267 for (i
= 0; i
< MAP_SIZE
; i
++) {
268 key
= MAP_SIZE
- i
- 1;
269 assert(bpf_lookup_elem(map_fd
, &key
, &value
) == 0 &&
273 /* now let's delete all elemenets in parallel */
275 run_parallel(TASKS
, do_work
, data
);
277 /* nothing should be left */
279 assert(bpf_get_next_key(map_fd
, &key
, &key
) == -1 && errno
== ENOENT
);
284 test_hashmap_sanity(0, NULL
);
285 test_arraymap_sanity(0, NULL
);
289 printf("test_maps: OK\n");