Commit e5ff7c40 authored by Mickaël Salaün's avatar Mickaël Salaün Committed by David S. Miller

bpf: Use bpf_map_lookup_elem() from the library

Replace bpf_map_lookup() with bpf_map_lookup_elem() calls.
Signed-off-by: default avatarMickaël Salaün <mic@digikod.net>
Cc: Alexei Starovoitov <ast@fb.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 10ecc728
...@@ -112,7 +112,7 @@ int bpf_map_update_elem(int fd, const void *key, const void *value, ...@@ -112,7 +112,7 @@ int bpf_map_update_elem(int fd, const void *key, const void *value,
return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)); return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
} }
int bpf_map_lookup_elem(int fd, void *key, void *value) int bpf_map_lookup_elem(int fd, const void *key, void *value)
{ {
union bpf_attr attr; union bpf_attr attr;
......
...@@ -36,7 +36,7 @@ int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns, ...@@ -36,7 +36,7 @@ int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
int bpf_map_update_elem(int fd, const void *key, const void *value, int bpf_map_update_elem(int fd, const void *key, const void *value,
__u64 flags); __u64 flags);
int bpf_map_lookup_elem(int fd, void *key, void *value); int bpf_map_lookup_elem(int fd, const void *key, void *value);
int bpf_map_delete_elem(int fd, void *key); int bpf_map_delete_elem(int fd, void *key);
int bpf_map_get_next_key(int fd, void *key, void *next_key); int bpf_map_get_next_key(int fd, void *key, void *next_key);
int bpf_obj_pin(int fd, const char *pathname); int bpf_obj_pin(int fd, const char *pathname);
......
...@@ -24,17 +24,6 @@ static inline int bpf(int cmd, union bpf_attr *attr, unsigned int size) ...@@ -24,17 +24,6 @@ static inline int bpf(int cmd, union bpf_attr *attr, unsigned int size)
#endif #endif
} }
static inline int bpf_map_lookup(int fd, const void *key, void *value)
{
union bpf_attr attr = {};
attr.map_fd = fd;
attr.key = bpf_ptr_to_u64(key);
attr.value = bpf_ptr_to_u64(value);
return bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
}
static inline int bpf_map_delete(int fd, const void *key) static inline int bpf_map_delete(int fd, const void *key)
{ {
union bpf_attr attr = {}; union bpf_attr attr = {};
......
...@@ -211,7 +211,7 @@ static void test_lpm_map(int keysize) ...@@ -211,7 +211,7 @@ static void test_lpm_map(int keysize)
key->prefixlen = 8 * keysize; key->prefixlen = 8 * keysize;
memcpy(key->data, data, keysize); memcpy(key->data, data, keysize);
r = bpf_map_lookup(map, key, value); r = bpf_map_lookup_elem(map, key, value);
assert(!r || errno == ENOENT); assert(!r || errno == ENOENT);
assert(!t == !!r); assert(!t == !!r);
...@@ -300,32 +300,32 @@ static void test_lpm_ipaddr(void) ...@@ -300,32 +300,32 @@ static void test_lpm_ipaddr(void)
/* Test some lookups that should come back with a value */ /* Test some lookups that should come back with a value */
inet_pton(AF_INET, "192.168.128.23", key_ipv4->data); inet_pton(AF_INET, "192.168.128.23", key_ipv4->data);
assert(bpf_map_lookup(map_fd_ipv4, key_ipv4, &value) == 0); assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == 0);
assert(value == 3); assert(value == 3);
inet_pton(AF_INET, "192.168.0.1", key_ipv4->data); inet_pton(AF_INET, "192.168.0.1", key_ipv4->data);
assert(bpf_map_lookup(map_fd_ipv4, key_ipv4, &value) == 0); assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == 0);
assert(value == 2); assert(value == 2);
inet_pton(AF_INET6, "2a00:1450:4001:814::", key_ipv6->data); inet_pton(AF_INET6, "2a00:1450:4001:814::", key_ipv6->data);
assert(bpf_map_lookup(map_fd_ipv6, key_ipv6, &value) == 0); assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == 0);
assert(value == 0xdeadbeef); assert(value == 0xdeadbeef);
inet_pton(AF_INET6, "2a00:1450:4001:814::1", key_ipv6->data); inet_pton(AF_INET6, "2a00:1450:4001:814::1", key_ipv6->data);
assert(bpf_map_lookup(map_fd_ipv6, key_ipv6, &value) == 0); assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == 0);
assert(value == 0xdeadbeef); assert(value == 0xdeadbeef);
/* Test some lookups that should not match any entry */ /* Test some lookups that should not match any entry */
inet_pton(AF_INET, "10.0.0.1", key_ipv4->data); inet_pton(AF_INET, "10.0.0.1", key_ipv4->data);
assert(bpf_map_lookup(map_fd_ipv4, key_ipv4, &value) == -1 && assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -1 &&
errno == ENOENT); errno == ENOENT);
inet_pton(AF_INET, "11.11.11.11", key_ipv4->data); inet_pton(AF_INET, "11.11.11.11", key_ipv4->data);
assert(bpf_map_lookup(map_fd_ipv4, key_ipv4, &value) == -1 && assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -1 &&
errno == ENOENT); errno == ENOENT);
inet_pton(AF_INET6, "2a00:ffff::", key_ipv6->data); inet_pton(AF_INET6, "2a00:ffff::", key_ipv6->data);
assert(bpf_map_lookup(map_fd_ipv6, key_ipv6, &value) == -1 && assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == -1 &&
errno == ENOENT); errno == ENOENT);
close(map_fd_ipv4); close(map_fd_ipv4);
......
...@@ -47,8 +47,8 @@ static int map_subset(int map0, int map1) ...@@ -47,8 +47,8 @@ static int map_subset(int map0, int map1)
int ret; int ret;
while (!bpf_map_next_key(map1, &next_key, &next_key)) { while (!bpf_map_next_key(map1, &next_key, &next_key)) {
assert(!bpf_map_lookup(map1, &next_key, value1)); assert(!bpf_map_lookup_elem(map1, &next_key, value1));
ret = bpf_map_lookup(map0, &next_key, value0); ret = bpf_map_lookup_elem(map0, &next_key, value0);
if (ret) { if (ret) {
printf("key:%llu not found from map. %s(%d)\n", printf("key:%llu not found from map. %s(%d)\n",
next_key, strerror(errno), errno); next_key, strerror(errno), errno);
...@@ -136,7 +136,7 @@ static void test_lru_sanity0(int map_type, int map_flags) ...@@ -136,7 +136,7 @@ static void test_lru_sanity0(int map_type, int map_flags)
/* check that key=2 is not found */ /* check that key=2 is not found */
key = 2; key = 2;
assert(bpf_map_lookup(lru_map_fd, &key, value) == -1 && assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1 &&
errno == ENOENT); errno == ENOENT);
/* BPF_EXIST means: update existing element */ /* BPF_EXIST means: update existing element */
...@@ -150,14 +150,14 @@ static void test_lru_sanity0(int map_type, int map_flags) ...@@ -150,14 +150,14 @@ static void test_lru_sanity0(int map_type, int map_flags)
/* check that key=3 is not found */ /* check that key=3 is not found */
key = 3; key = 3;
assert(bpf_map_lookup(lru_map_fd, &key, value) == -1 && assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1 &&
errno == ENOENT); errno == ENOENT);
/* check that key=1 can be found and mark the ref bit to /* check that key=1 can be found and mark the ref bit to
* stop LRU from removing key=1 * stop LRU from removing key=1
*/ */
key = 1; key = 1;
assert(!bpf_map_lookup(lru_map_fd, &key, value)); assert(!bpf_map_lookup_elem(lru_map_fd, &key, value));
assert(value[0] == 1234); assert(value[0] == 1234);
key = 3; key = 3;
...@@ -167,7 +167,7 @@ static void test_lru_sanity0(int map_type, int map_flags) ...@@ -167,7 +167,7 @@ static void test_lru_sanity0(int map_type, int map_flags)
/* key=2 has been removed from the LRU */ /* key=2 has been removed from the LRU */
key = 2; key = 2;
assert(bpf_map_lookup(lru_map_fd, &key, value) == -1); assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1);
assert(map_equal(lru_map_fd, expected_map_fd)); assert(map_equal(lru_map_fd, expected_map_fd));
...@@ -226,7 +226,7 @@ static void test_lru_sanity1(int map_type, int map_flags, unsigned int tgt_free) ...@@ -226,7 +226,7 @@ static void test_lru_sanity1(int map_type, int map_flags, unsigned int tgt_free)
/* Lookup 1 to tgt_free/2 */ /* Lookup 1 to tgt_free/2 */
end_key = 1 + batch_size; end_key = 1 + batch_size;
for (key = 1; key < end_key; key++) { for (key = 1; key < end_key; key++) {
assert(!bpf_map_lookup(lru_map_fd, &key, value)); assert(!bpf_map_lookup_elem(lru_map_fd, &key, value));
assert(!bpf_map_update_elem(expected_map_fd, &key, value, assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST)); BPF_NOEXIST));
} }
...@@ -336,10 +336,10 @@ static void test_lru_sanity2(int map_type, int map_flags, unsigned int tgt_free) ...@@ -336,10 +336,10 @@ static void test_lru_sanity2(int map_type, int map_flags, unsigned int tgt_free)
end_key = 1 + batch_size; end_key = 1 + batch_size;
value[0] = 4321; value[0] = 4321;
for (key = 1; key < end_key; key++) { for (key = 1; key < end_key; key++) {
assert(bpf_map_lookup(lru_map_fd, &key, value)); assert(bpf_map_lookup_elem(lru_map_fd, &key, value));
assert(!bpf_map_update_elem(lru_map_fd, &key, value, assert(!bpf_map_update_elem(lru_map_fd, &key, value,
BPF_NOEXIST)); BPF_NOEXIST));
assert(!bpf_map_lookup(lru_map_fd, &key, value)); assert(!bpf_map_lookup_elem(lru_map_fd, &key, value));
assert(value[0] == 4321); assert(value[0] == 4321);
assert(!bpf_map_update_elem(expected_map_fd, &key, value, assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST)); BPF_NOEXIST));
...@@ -418,7 +418,7 @@ static void test_lru_sanity3(int map_type, int map_flags, unsigned int tgt_free) ...@@ -418,7 +418,7 @@ static void test_lru_sanity3(int map_type, int map_flags, unsigned int tgt_free)
/* Lookup key 1 to tgt_free*3/2 */ /* Lookup key 1 to tgt_free*3/2 */
end_key = tgt_free + batch_size; end_key = tgt_free + batch_size;
for (key = 1; key < end_key; key++) { for (key = 1; key < end_key; key++) {
assert(!bpf_map_lookup(lru_map_fd, &key, value)); assert(!bpf_map_lookup_elem(lru_map_fd, &key, value));
assert(!bpf_map_update_elem(expected_map_fd, &key, value, assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST)); BPF_NOEXIST));
} }
...@@ -477,7 +477,7 @@ static void test_lru_sanity4(int map_type, int map_flags, unsigned int tgt_free) ...@@ -477,7 +477,7 @@ static void test_lru_sanity4(int map_type, int map_flags, unsigned int tgt_free)
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST)); assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST));
for (key = 1; key <= tgt_free; key++) { for (key = 1; key <= tgt_free; key++) {
assert(!bpf_map_lookup(lru_map_fd, &key, value)); assert(!bpf_map_lookup_elem(lru_map_fd, &key, value));
assert(!bpf_map_update_elem(expected_map_fd, &key, value, assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST)); BPF_NOEXIST));
} }
...@@ -508,16 +508,16 @@ static void do_test_lru_sanity5(unsigned long long last_key, int map_fd) ...@@ -508,16 +508,16 @@ static void do_test_lru_sanity5(unsigned long long last_key, int map_fd)
unsigned long long key, value[nr_cpus]; unsigned long long key, value[nr_cpus];
/* Ensure the last key inserted by previous CPU can be found */ /* Ensure the last key inserted by previous CPU can be found */
assert(!bpf_map_lookup(map_fd, &last_key, value)); assert(!bpf_map_lookup_elem(map_fd, &last_key, value));
value[0] = 1234; value[0] = 1234;
key = last_key + 1; key = last_key + 1;
assert(!bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST)); assert(!bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_lookup(map_fd, &key, value)); assert(!bpf_map_lookup_elem(map_fd, &key, value));
/* Cannot find the last key because it was removed by LRU */ /* Cannot find the last key because it was removed by LRU */
assert(bpf_map_lookup(map_fd, &last_key, value)); assert(bpf_map_lookup_elem(map_fd, &last_key, value));
} }
/* Test map with only one element */ /* Test map with only one element */
......
...@@ -55,11 +55,11 @@ static void test_hashmap(int task, void *data) ...@@ -55,11 +55,11 @@ static void test_hashmap(int task, void *data)
errno == EINVAL); errno == EINVAL);
/* Check that key=1 can be found. */ /* Check that key=1 can be found. */
assert(bpf_map_lookup(fd, &key, &value) == 0 && value == 1234); assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
key = 2; key = 2;
/* Check that key=2 is not found. */ /* Check that key=2 is not found. */
assert(bpf_map_lookup(fd, &key, &value) == -1 && errno == ENOENT); assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
/* BPF_EXIST means update existing element. */ /* BPF_EXIST means update existing element. */
assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 && assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
...@@ -148,11 +148,11 @@ static void test_hashmap_percpu(int task, void *data) ...@@ -148,11 +148,11 @@ static void test_hashmap_percpu(int task, void *data)
* was run from a different CPU. * was run from a different CPU.
*/ */
value[0] = 1; value[0] = 1;
assert(bpf_map_lookup(fd, &key, value) == 0 && value[0] == 100); assert(bpf_map_lookup_elem(fd, &key, value) == 0 && value[0] == 100);
key = 2; key = 2;
/* Check that key=2 is not found. */ /* Check that key=2 is not found. */
assert(bpf_map_lookup(fd, &key, value) == -1 && errno == ENOENT); assert(bpf_map_lookup_elem(fd, &key, value) == -1 && errno == ENOENT);
/* BPF_EXIST means update existing element. */ /* BPF_EXIST means update existing element. */
assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == -1 && assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == -1 &&
...@@ -179,7 +179,7 @@ static void test_hashmap_percpu(int task, void *data) ...@@ -179,7 +179,7 @@ static void test_hashmap_percpu(int task, void *data)
assert((expected_key_mask & next_key) == next_key); assert((expected_key_mask & next_key) == next_key);
expected_key_mask &= ~next_key; expected_key_mask &= ~next_key;
assert(bpf_map_lookup(fd, &next_key, value) == 0); assert(bpf_map_lookup_elem(fd, &next_key, value) == 0);
for (i = 0; i < nr_cpus; i++) for (i = 0; i < nr_cpus; i++)
assert(value[i] == i + 100); assert(value[i] == i + 100);
...@@ -229,11 +229,11 @@ static void test_arraymap(int task, void *data) ...@@ -229,11 +229,11 @@ static void test_arraymap(int task, void *data)
errno == EEXIST); errno == EEXIST);
/* Check that key=1 can be found. */ /* Check that key=1 can be found. */
assert(bpf_map_lookup(fd, &key, &value) == 0 && value == 1234); assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
key = 0; key = 0;
/* Check that key=0 is also found and zero initialized. */ /* Check that key=0 is also found and zero initialized. */
assert(bpf_map_lookup(fd, &key, &value) == 0 && value == 0); 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 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
* due to max_entries limit. * due to max_entries limit.
...@@ -243,7 +243,7 @@ static void test_arraymap(int task, void *data) ...@@ -243,7 +243,7 @@ static void test_arraymap(int task, void *data)
errno == E2BIG); errno == E2BIG);
/* Check that key = 2 doesn't exist. */ /* Check that key = 2 doesn't exist. */
assert(bpf_map_lookup(fd, &key, &value) == -1 && errno == ENOENT); assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
/* Iterate over two elements. */ /* Iterate over two elements. */
assert(bpf_map_next_key(fd, &key, &next_key) == 0 && assert(bpf_map_next_key(fd, &key, &next_key) == 0 &&
...@@ -285,11 +285,11 @@ static void test_arraymap_percpu(int task, void *data) ...@@ -285,11 +285,11 @@ static void test_arraymap_percpu(int task, void *data)
errno == EEXIST); errno == EEXIST);
/* Check that key=1 can be found. */ /* Check that key=1 can be found. */
assert(bpf_map_lookup(fd, &key, values) == 0 && values[0] == 100); assert(bpf_map_lookup_elem(fd, &key, values) == 0 && values[0] == 100);
key = 0; key = 0;
/* Check that key=0 is also found and zero initialized. */ /* Check that key=0 is also found and zero initialized. */
assert(bpf_map_lookup(fd, &key, values) == 0 && assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
values[0] == 0 && values[nr_cpus - 1] == 0); values[0] == 0 && values[nr_cpus - 1] == 0);
/* Check that key=2 cannot be inserted due to max_entries limit. */ /* Check that key=2 cannot be inserted due to max_entries limit. */
...@@ -298,7 +298,7 @@ static void test_arraymap_percpu(int task, void *data) ...@@ -298,7 +298,7 @@ static void test_arraymap_percpu(int task, void *data)
errno == E2BIG); errno == E2BIG);
/* Check that key = 2 doesn't exist. */ /* Check that key = 2 doesn't exist. */
assert(bpf_map_lookup(fd, &key, values) == -1 && errno == ENOENT); assert(bpf_map_lookup_elem(fd, &key, values) == -1 && errno == ENOENT);
/* Iterate over two elements. */ /* Iterate over two elements. */
assert(bpf_map_next_key(fd, &key, &next_key) == 0 && assert(bpf_map_next_key(fd, &key, &next_key) == 0 &&
...@@ -340,7 +340,7 @@ static void test_arraymap_percpu_many_keys(void) ...@@ -340,7 +340,7 @@ static void test_arraymap_percpu_many_keys(void)
for (i = 0; i < nr_cpus; i++) for (i = 0; i < nr_cpus; i++)
values[i] = 0; values[i] = 0;
assert(bpf_map_lookup(fd, &key, values) == 0); assert(bpf_map_lookup_elem(fd, &key, values) == 0);
for (i = 0; i < nr_cpus; i++) for (i = 0; i < nr_cpus; i++)
assert(values[i] == i + 10); assert(values[i] == i + 10);
...@@ -384,9 +384,9 @@ static void test_map_large(void) ...@@ -384,9 +384,9 @@ static void test_map_large(void)
assert(bpf_map_next_key(fd, &key, &key) == -1 && errno == ENOENT); assert(bpf_map_next_key(fd, &key, &key) == -1 && errno == ENOENT);
key.c = 0; key.c = 0;
assert(bpf_map_lookup(fd, &key, &value) == 0 && value == 0); assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
key.a = 1; key.a = 1;
assert(bpf_map_lookup(fd, &key, &value) == -1 && errno == ENOENT); assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
close(fd); close(fd);
} }
...@@ -486,7 +486,7 @@ static void test_map_parallel(void) ...@@ -486,7 +486,7 @@ static void test_map_parallel(void)
for (i = 0; i < MAP_SIZE; i++) { for (i = 0; i < MAP_SIZE; i++) {
key = MAP_SIZE - i - 1; key = MAP_SIZE - i - 1;
assert(bpf_map_lookup(fd, &key, &value) == 0 && assert(bpf_map_lookup_elem(fd, &key, &value) == 0 &&
value == key); value == key);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment