cregit-Linux how code gets into the kernel

Release 4.12 tools/testing/selftests/bpf/test_maps.c

/*
 * Testsuite for eBPF maps
 *
 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
 * Copyright (c) 2016 Facebook
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 */

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

#include <sys/wait.h>
#include <sys/resource.h>

#include <linux/bpf.h>

#include <bpf/bpf.h>
#include "bpf_util.h"


static int map_flags;


static void test_hashmap(int task, void *data) { long long key, next_key, first_key, value; int fd; fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), 2, map_flags); if (fd < 0) { printf("Failed to create hashmap '%s'!\n", strerror(errno)); exit(1); } key = 1; value = 1234; /* Insert key=1 element. */ assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); value = 0; /* BPF_NOEXIST means add new element if it doesn't exist. */ assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && /* key=1 already exists. */ errno == EEXIST); /* -1 is an invalid flag. */ assert(bpf_map_update_elem(fd, &key, &value, -1) == -1 && errno == EINVAL); /* Check that key=1 can be found. */ assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234); key = 2; /* Check that key=2 is not found. */ assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT); /* BPF_EXIST means update existing element. */ assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 && /* key=2 is not there. */ errno == ENOENT); /* Insert key=2 element. */ assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0); /* key=1 and key=2 were inserted, check that key=0 cannot be * inserted due to max_entries limit. */ key = 0; assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && errno == E2BIG); /* Update existing element, though the map is full. */ key = 1; assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0); key = 2; assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); key = 3; assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && errno == E2BIG); /* Check that key = 0 doesn't exist. */ key = 0; assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT); /* Iterate over two elements. */ assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 && (first_key == 1 || first_key == 2)); assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && (next_key == first_key)); assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 && (next_key == 1 || next_key == 2) && (next_key != first_key)); assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 && errno == ENOENT); /* Delete both elements. */ key = 1; assert(bpf_map_delete_elem(fd, &key) == 0); key = 2; assert(bpf_map_delete_elem(fd, &key) == 0); assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT); key = 0; /* Check that map is empty. */ assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 && errno == ENOENT); assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 && errno == ENOENT); close(fd); }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov45579.82%541.67%
Teng Qin549.47%18.33%
Daniel Borkmann417.19%18.33%
Mickaël Salaün203.51%541.67%
Total570100.00%12100.00%


static void test_hashmap_sizes(int task, void *data) { int fd, i, j; for (i = 1; i <= 512; i <<= 1) for (j = 1; j <= 1 << 18; j <<= 1) { fd = bpf_create_map(BPF_MAP_TYPE_HASH, i, j, 2, map_flags); if (fd < 0) { printf("Failed to create hashmap key=%d value=%d '%s'\n", i, j, strerror(errno)); exit(1); } close(fd); usleep(10); /* give kernel time to destroy */ } }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov105100.00%1100.00%
Total105100.00%1100.00%


static void test_hashmap_percpu(int task, void *data) { unsigned int nr_cpus = bpf_num_possible_cpus(); BPF_DECLARE_PERCPU(long, value); long long key, next_key, first_key; int expected_key_mask = 0; int fd, i; fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key), sizeof(bpf_percpu(value, 0)), 2, map_flags); if (fd < 0) { printf("Failed to create hashmap '%s'!\n", strerror(errno)); exit(1); } for (i = 0; i < nr_cpus; i++) bpf_percpu(value, i) = i + 100; key = 1; /* Insert key=1 element. */ assert(!(expected_key_mask & key)); assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0); expected_key_mask |= key; /* BPF_NOEXIST means add new element if it doesn't exist. */ assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 && /* key=1 already exists. */ errno == EEXIST); /* -1 is an invalid flag. */ assert(bpf_map_update_elem(fd, &key, value, -1) == -1 && errno == EINVAL); /* Check that key=1 can be found. Value could be 0 if the lookup * was run from a different CPU. */ bpf_percpu(value, 0) = 1; assert(bpf_map_lookup_elem(fd, &key, value) == 0 && bpf_percpu(value, 0) == 100); key = 2; /* Check that key=2 is not found. */ assert(bpf_map_lookup_elem(fd, &key, value) == -1 && errno == ENOENT); /* BPF_EXIST means update existing element. */ assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == -1 && /* key=2 is not there. */ errno == ENOENT); /* Insert key=2 element. */ assert(!(expected_key_mask & key)); assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0); expected_key_mask |= key; /* key=1 and key=2 were inserted, check that key=0 cannot be * inserted due to max_entries limit. */ key = 0; assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 && errno == E2BIG); /* Check that key = 0 doesn't exist. */ assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT); /* Iterate over two elements. */ assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 && ((expected_key_mask & first_key) == first_key)); while (!bpf_map_get_next_key(fd, &key, &next_key)) { if (first_key) { assert(next_key == first_key); first_key = 0; } assert((expected_key_mask & next_key) == next_key); expected_key_mask &= ~next_key; assert(bpf_map_lookup_elem(fd, &next_key, value) == 0); for (i = 0; i < nr_cpus; i++) assert(bpf_percpu(value, i) == i + 100); key = next_key; } assert(errno == ENOENT); /* Update with BPF_EXIST. */ key = 1; assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0); /* Delete both elements. */ key = 1; assert(bpf_map_delete_elem(fd, &key) == 0); key = 2; assert(bpf_map_delete_elem(fd, &key) == 0); assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT); key = 0; /* Check that map is empty. */ assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 && errno == ENOENT); assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 && errno == ENOENT); close(fd); }

Contributors

PersonTokensPropCommitsCommitProp
Martin KaFai Lau45974.39%19.09%
Daniel Borkmann7512.16%327.27%
Teng Qin6410.37%19.09%
Mickaël Salaün172.76%545.45%
Alexei Starovoitov20.32%19.09%
Total617100.00%11100.00%


static void test_arraymap(int task, void *data) { int key, next_key, fd; long long value; fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value), 2, 0); if (fd < 0) { printf("Failed to create arraymap '%s'!\n", strerror(errno)); exit(1); } key = 1; value = 1234; /* Insert key=1 element. */ assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); value = 0; assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && errno == EEXIST); /* Check that key=1 can be found. */ assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234); key = 0; /* Check that key=0 is also found and zero initialized. */ assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0); /* key=0 and key=1 were inserted, check that key=2 cannot be inserted * due to max_entries limit. */ key = 2; assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 && errno == E2BIG); /* Check that key = 2 doesn't exist. */ assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT); /* Iterate over two elements. */ assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 && next_key == 0); assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && next_key == 0); assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 && next_key == 1); assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 && errno == ENOENT); /* Delete shouldn't succeed. */ key = 1; assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL); close(fd); }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov27383.49%222.22%
Daniel Borkmann247.34%111.11%
Teng Qin195.81%111.11%
Mickaël Salaün113.36%555.56%
Total327100.00%9100.00%


static void test_arraymap_percpu(int task, void *data) { unsigned int nr_cpus = bpf_num_possible_cpus(); BPF_DECLARE_PERCPU(long, values); int key, next_key, fd, i; fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key), sizeof(bpf_percpu(values, 0)), 2, 0); if (fd < 0) { printf("Failed to create arraymap '%s'!\n", strerror(errno)); exit(1); } for (i = 0; i < nr_cpus; i++) bpf_percpu(values, i) = i + 100; key = 1; /* Insert key=1 element. */ assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0); bpf_percpu(values, 0) = 0; assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) == -1 && errno == EEXIST); /* Check that key=1 can be found. */ assert(bpf_map_lookup_elem(fd, &key, values) == 0 && bpf_percpu(values, 0) == 100); key = 0; /* Check that key=0 is also found and zero initialized. */ assert(bpf_map_lookup_elem(fd, &key, values) == 0 && bpf_percpu(values, 0) == 0 && bpf_percpu(values, nr_cpus - 1) == 0); /* Check that key=2 cannot be inserted due to max_entries limit. */ key = 2; assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) == -1 && errno == E2BIG); /* Check that key = 2 doesn't exist. */ assert(bpf_map_lookup_elem(fd, &key, values) == -1 && errno == ENOENT); /* Iterate over two elements. */ assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 && next_key == 0); assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && next_key == 0); assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 && next_key == 1); assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 && errno == ENOENT); /* Delete shouldn't succeed. */ key = 1; assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL); close(fd); }

Contributors

PersonTokensPropCommitsCommitProp
Lei Ming28574.22%19.09%
Daniel Borkmann6717.45%327.27%
Teng Qin194.95%19.09%
Mickaël Salaün112.86%545.45%
Alexei Starovoitov20.52%19.09%
Total384100.00%11100.00%


static void test_arraymap_percpu_many_keys(void) { unsigned int nr_cpus = bpf_num_possible_cpus(); BPF_DECLARE_PERCPU(long, values); /* nr_keys is not too large otherwise the test stresses percpu * allocator more than anything else */ unsigned int nr_keys = 2000; int key, fd, i; fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key), sizeof(bpf_percpu(values, 0)), nr_keys, 0); if (fd < 0) { printf("Failed to create per-cpu arraymap '%s'!\n", strerror(errno)); exit(1); } for (i = 0; i < nr_cpus; i++) bpf_percpu(values, i) = i + 10; for (key = 0; key < nr_keys; key++) assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0); for (key = 0; key < nr_keys; key++) { for (i = 0; i < nr_cpus; i++) bpf_percpu(values, i) = 0; assert(bpf_map_lookup_elem(fd, &key, values) == 0); for (i = 0; i < nr_cpus; i++) assert(bpf_percpu(values, i) == i + 10); } close(fd); }

Contributors

PersonTokensPropCommitsCommitProp
Daniel Borkmann21596.41%337.50%
Mickaël Salaün31.35%337.50%
Lei Ming31.35%112.50%
Alexei Starovoitov20.90%112.50%
Total223100.00%8100.00%

#define MAP_SIZE (32 * 1024)
static void test_map_large(void) { struct bigkey { int a; char b[116]; long long c; } key; int fd, i, value; fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), MAP_SIZE, map_flags); if (fd < 0) { printf("Failed to create large map '%s'!\n", strerror(errno)); exit(1); } for (i = 0; i < MAP_SIZE; i++) { key = (struct bigkey) { .c = i }; value = i; assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0); } key.c = -1; assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && errno == E2BIG); /* Iterate through all elements. */ assert(bpf_map_get_next_key(fd, NULL, &key) == 0); key.c = -1; for (i = 0; i < MAP_SIZE; i++) assert(bpf_map_get_next_key(fd, &key, &key) == 0); assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT); key.c = 0; assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0); key.a = 1; assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT); close(fd); }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov24885.81%225.00%
Teng Qin227.61%112.50%
Daniel Borkmann124.15%112.50%
Mickaël Salaün72.42%450.00%
Total289100.00%8100.00%


static void run_parallel(int tasks, void (*fn)(int task, void *data), void *data) { pid_t pid[tasks]; int i; for (i = 0; i < tasks; i++) { pid[i] = fork(); if (pid[i] == 0) { fn(i, data); exit(0); } else if (pid[i] == -1) { printf("Couldn't spawn #%d process!\n", i); exit(1); } } for (i = 0; i < tasks; i++) { int status; assert(waitpid(pid[i], &status, 0) == pid[i]); assert(status == 0); } }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov15198.69%150.00%
Daniel Borkmann21.31%150.00%
Total153100.00%2100.00%


static void test_map_stress(void) { run_parallel(100, test_hashmap, NULL); run_parallel(100, test_hashmap_percpu, NULL); run_parallel(100, test_hashmap_sizes, NULL); run_parallel(100, test_arraymap, NULL); run_parallel(100, test_arraymap_percpu, NULL); }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov3362.26%240.00%
Lei Ming815.09%120.00%
Martin KaFai Lau815.09%120.00%
Daniel Borkmann47.55%120.00%
Total53100.00%5100.00%

#define TASKS 1024 #define DO_UPDATE 1 #define DO_DELETE 0
static void do_work(int fn, void *data) { int do_update = ((int *)data)[1]; int fd = ((int *)data)[0]; int i, key, value; for (i = fn; i < MAP_SIZE; i += TASKS) { key = value = i; if (do_update) { assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0); assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0); } else { assert(bpf_map_delete_elem(fd, &key) == 0); } } }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov11791.41%240.00%
Daniel Borkmann86.25%120.00%
Mickaël Salaün32.34%240.00%
Total128100.00%5100.00%


static void test_map_parallel(void) { int i, fd, key = 0, value = 0; int data[2]; fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), MAP_SIZE, map_flags); if (fd < 0) { printf("Failed to create map for parallel test '%s'!\n", strerror(errno)); exit(1); } /* Use the same fd in children to add elements to this map: * child_0 adds key=0, key=1024, key=2048, ... * child_1 adds key=1, key=1025, key=2049, ... * child_1023 adds key=1023, ... */ data[0] = fd; data[1] = DO_UPDATE; run_parallel(TASKS, do_work, data); /* Check that key=0 is already there. */ assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && errno == EEXIST); /* Check that all elements were inserted. */ assert(bpf_map_get_next_key(fd, NULL, &key) == 0); key = -1; for (i = 0; i < MAP_SIZE; i++) assert(bpf_map_get_next_key(fd, &key, &key) == 0); assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT); /* Another check for all elements */ for (i = 0; i < MAP_SIZE; i++) { key = MAP_SIZE - i - 1; assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == key); } /* Now let's delete all elemenets in parallel. */ data[1] = DO_DELETE; run_parallel(TASKS, do_work, data); /* Nothing should be left. */ key = -1; assert(bpf_map_get_next_key(fd, NULL, &key) == -1 && errno == ENOENT); assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT); }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov24180.87%225.00%
Teng Qin3511.74%112.50%
Daniel Borkmann165.37%112.50%
Mickaël Salaün62.01%450.00%
Total298100.00%8100.00%


static void run_all_tests(void) { test_hashmap(0, NULL); test_hashmap_percpu(0, NULL); test_arraymap(0, NULL); test_arraymap_percpu(0, NULL); test_arraymap_percpu_many_keys(); test_map_large(); test_map_parallel(); test_map_stress(); }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov2960.42%240.00%
Lei Ming816.67%120.00%
Martin KaFai Lau612.50%120.00%
Daniel Borkmann510.42%120.00%
Total48100.00%5100.00%


int main(void) { struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY }; setrlimit(RLIMIT_MEMLOCK, &rinf); map_flags = 0; run_all_tests(); map_flags = BPF_F_NO_PREALLOC; run_all_tests(); printf("test_maps: OK\n"); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov2961.70%266.67%
Daniel Borkmann1838.30%133.33%
Total47100.00%3100.00%


Overall Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov172952.46%635.29%
Daniel Borkmann49615.05%317.65%
Martin KaFai Lau47314.35%15.88%
Lei Ming3049.22%15.88%
Teng Qin2136.46%15.88%
Mickaël Salaün812.46%529.41%
Total3296100.00%17100.00%
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.