diff options
| author | tom.leiming@gmail.com <tom.leiming@gmail.com> | 2016-02-02 01:39:57 -0500 |
|---|---|---|
| committer | David S. Miller <davem@davemloft.net> | 2016-02-06 03:34:36 -0500 |
| commit | df570f577231407d929bdc6f59ae2f53e0028e8a (patch) | |
| tree | 8108423700e2437a977924a207c7baefa43797fa /samples | |
| parent | e15596717948c95587a0b15363030283c126c23a (diff) | |
samples/bpf: unit test for BPF_MAP_TYPE_PERCPU_ARRAY
A sanity test for BPF_MAP_TYPE_PERCPU_ARRAY
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'samples')
| -rw-r--r-- | samples/bpf/test_maps.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/samples/bpf/test_maps.c b/samples/bpf/test_maps.c index 5f5fe5332148..ad466ed33093 100644 --- a/samples/bpf/test_maps.c +++ b/samples/bpf/test_maps.c | |||
| @@ -236,6 +236,94 @@ static void test_arraymap_sanity(int i, void *data) | |||
| 236 | close(map_fd); | 236 | close(map_fd); |
| 237 | } | 237 | } |
| 238 | 238 | ||
| 239 | static void test_percpu_arraymap_many_keys(void) | ||
| 240 | { | ||
| 241 | unsigned nr_cpus = sysconf(_SC_NPROCESSORS_CONF); | ||
| 242 | unsigned nr_keys = 20000; | ||
| 243 | long values[nr_cpus]; | ||
| 244 | int key, map_fd, i; | ||
| 245 | |||
| 246 | map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key), | ||
| 247 | sizeof(values[0]), nr_keys); | ||
| 248 | if (map_fd < 0) { | ||
| 249 | printf("failed to create per-cpu arraymap '%s'\n", | ||
| 250 | strerror(errno)); | ||
| 251 | exit(1); | ||
| 252 | } | ||
| 253 | |||
| 254 | for (i = 0; i < nr_cpus; i++) | ||
| 255 | values[i] = i + 10; | ||
| 256 | |||
| 257 | for (key = 0; key < nr_keys; key++) | ||
| 258 | assert(bpf_update_elem(map_fd, &key, values, BPF_ANY) == 0); | ||
| 259 | |||
| 260 | for (key = 0; key < nr_keys; key++) { | ||
| 261 | for (i = 0; i < nr_cpus; i++) | ||
| 262 | values[i] = 0; | ||
| 263 | assert(bpf_lookup_elem(map_fd, &key, values) == 0); | ||
| 264 | for (i = 0; i < nr_cpus; i++) | ||
| 265 | assert(values[i] == i + 10); | ||
| 266 | } | ||
| 267 | |||
| 268 | close(map_fd); | ||
| 269 | } | ||
| 270 | |||
| 271 | static void test_percpu_arraymap_sanity(int i, void *data) | ||
| 272 | { | ||
| 273 | unsigned nr_cpus = sysconf(_SC_NPROCESSORS_CONF); | ||
| 274 | long values[nr_cpus]; | ||
| 275 | int key, next_key, map_fd; | ||
| 276 | |||
| 277 | map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key), | ||
| 278 | sizeof(values[0]), 2); | ||
| 279 | if (map_fd < 0) { | ||
| 280 | printf("failed to create arraymap '%s'\n", strerror(errno)); | ||
| 281 | exit(1); | ||
| 282 | } | ||
| 283 | |||
| 284 | for (i = 0; i < nr_cpus; i++) | ||
| 285 | values[i] = i + 100; | ||
| 286 | |||
| 287 | key = 1; | ||
| 288 | /* insert key=1 element */ | ||
| 289 | assert(bpf_update_elem(map_fd, &key, values, BPF_ANY) == 0); | ||
| 290 | |||
| 291 | values[0] = 0; | ||
| 292 | assert(bpf_update_elem(map_fd, &key, values, BPF_NOEXIST) == -1 && | ||
| 293 | errno == EEXIST); | ||
| 294 | |||
| 295 | /* check that key=1 can be found */ | ||
| 296 | assert(bpf_lookup_elem(map_fd, &key, values) == 0 && values[0] == 100); | ||
| 297 | |||
| 298 | key = 0; | ||
| 299 | /* check that key=0 is also found and zero initialized */ | ||
| 300 | assert(bpf_lookup_elem(map_fd, &key, values) == 0 && | ||
| 301 | values[0] == 0 && values[nr_cpus - 1] == 0); | ||
| 302 | |||
| 303 | |||
| 304 | /* check that key=2 cannot be inserted due to max_entries limit */ | ||
| 305 | key = 2; | ||
| 306 | assert(bpf_update_elem(map_fd, &key, values, BPF_EXIST) == -1 && | ||
| 307 | errno == E2BIG); | ||
| 308 | |||
| 309 | /* check that key = 2 doesn't exist */ | ||
| 310 | assert(bpf_lookup_elem(map_fd, &key, values) == -1 && errno == ENOENT); | ||
| 311 | |||
| 312 | /* iterate over two elements */ | ||
| 313 | assert(bpf_get_next_key(map_fd, &key, &next_key) == 0 && | ||
| 314 | next_key == 0); | ||
| 315 | assert(bpf_get_next_key(map_fd, &next_key, &next_key) == 0 && | ||
| 316 | next_key == 1); | ||
| 317 | assert(bpf_get_next_key(map_fd, &next_key, &next_key) == -1 && | ||
| 318 | errno == ENOENT); | ||
| 319 | |||
| 320 | /* delete shouldn't succeed */ | ||
| 321 | key = 1; | ||
| 322 | assert(bpf_delete_elem(map_fd, &key) == -1 && errno == EINVAL); | ||
| 323 | |||
| 324 | close(map_fd); | ||
| 325 | } | ||
| 326 | |||
| 239 | #define MAP_SIZE (32 * 1024) | 327 | #define MAP_SIZE (32 * 1024) |
| 240 | static void test_map_large(void) | 328 | static void test_map_large(void) |
| 241 | { | 329 | { |
| @@ -305,6 +393,7 @@ static void test_map_stress(void) | |||
| 305 | run_parallel(100, test_hashmap_sanity, NULL); | 393 | run_parallel(100, test_hashmap_sanity, NULL); |
| 306 | run_parallel(100, test_percpu_hashmap_sanity, NULL); | 394 | run_parallel(100, test_percpu_hashmap_sanity, NULL); |
| 307 | run_parallel(100, test_arraymap_sanity, NULL); | 395 | run_parallel(100, test_arraymap_sanity, NULL); |
| 396 | run_parallel(100, test_percpu_arraymap_sanity, NULL); | ||
| 308 | } | 397 | } |
| 309 | 398 | ||
| 310 | #define TASKS 1024 | 399 | #define TASKS 1024 |
| @@ -379,6 +468,9 @@ int main(void) | |||
| 379 | test_hashmap_sanity(0, NULL); | 468 | test_hashmap_sanity(0, NULL); |
| 380 | test_percpu_hashmap_sanity(0, NULL); | 469 | test_percpu_hashmap_sanity(0, NULL); |
| 381 | test_arraymap_sanity(0, NULL); | 470 | test_arraymap_sanity(0, NULL); |
| 471 | test_percpu_arraymap_sanity(0, NULL); | ||
| 472 | test_percpu_arraymap_many_keys(); | ||
| 473 | |||
| 382 | test_map_large(); | 474 | test_map_large(); |
| 383 | test_map_parallel(); | 475 | test_map_parallel(); |
| 384 | test_map_stress(); | 476 | test_map_stress(); |
