Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

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

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
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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));
}

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;

+1 −1
Original line number Diff line number Diff line
@@ -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,
			__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_get_next_key(int fd, void *key, void *next_key);
int bpf_obj_pin(int fd, const char *pathname);
+0 −11
Original line number Diff line number Diff line
@@ -24,17 +24,6 @@ static inline int bpf(int cmd, union bpf_attr *attr, unsigned int size)
#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)
{
	union bpf_attr attr = {};
+8 −8
Original line number Diff line number Diff line
@@ -211,7 +211,7 @@ static void test_lpm_map(int keysize)

		key->prefixlen = 8 * 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(!t == !!r);

@@ -300,32 +300,32 @@ static void test_lpm_ipaddr(void)

	/* Test some lookups that should come back with a value */
	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);

	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);

	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);

	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);

	/* Test some lookups that should not match any entry */
	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);

	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);

	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);

	close(map_fd_ipv4);
+14 −14
Original line number Diff line number Diff line
@@ -47,8 +47,8 @@ static int map_subset(int map0, int map1)
	int ret;

	while (!bpf_map_next_key(map1, &next_key, &next_key)) {
		assert(!bpf_map_lookup(map1, &next_key, value1));
		ret = bpf_map_lookup(map0, &next_key, value0);
		assert(!bpf_map_lookup_elem(map1, &next_key, value1));
		ret = bpf_map_lookup_elem(map0, &next_key, value0);
		if (ret) {
			printf("key:%llu not found from map. %s(%d)\n",
			       next_key, strerror(errno), errno);
@@ -136,7 +136,7 @@ static void test_lru_sanity0(int map_type, int map_flags)

	/* check that key=2 is not found */
	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);

	/* BPF_EXIST means: update existing element */
@@ -150,14 +150,14 @@ static void test_lru_sanity0(int map_type, int map_flags)

	/* check that key=3 is not found */
	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);

	/* check that key=1 can be found and mark the ref bit to
	 * stop LRU from removing 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);

	key = 3;
@@ -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;
	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));

@@ -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 */
	end_key = 1 + batch_size;
	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,
				            BPF_NOEXIST));
	}
@@ -336,10 +336,10 @@ static void test_lru_sanity2(int map_type, int map_flags, unsigned int tgt_free)
	end_key = 1 + batch_size;
	value[0] = 4321;
	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,
					    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(!bpf_map_update_elem(expected_map_fd, &key, value,
				            BPF_NOEXIST));
@@ -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 */
	end_key = tgt_free + batch_size;
	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,
				            BPF_NOEXIST));
	}
@@ -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));

	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,
					    BPF_NOEXIST));
	}
@@ -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];

	/* 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;

	key = last_key + 1;
	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 */
	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 */
Loading