Skip to content

Commit

Permalink
bpf: Use bpf_map_update_elem() from the library
Browse files Browse the repository at this point in the history
Replace bpf_map_update() with bpf_map_update_elem() calls.

Signed-off-by: Mickaë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: David S. Miller <davem@davemloft.net>
  • Loading branch information
Mickaël Salaün authored and David S. Miller committed Feb 10, 2017
1 parent 2ee89fb commit 10ecc72
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 91 deletions.
2 changes: 1 addition & 1 deletion tools/lib/bpf/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
}

int bpf_map_update_elem(int fd, void *key, void *value,
int bpf_map_update_elem(int fd, const void *key, const void *value,
__u64 flags)
{
union bpf_attr attr;
Expand Down
2 changes: 1 addition & 1 deletion tools/lib/bpf/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
__u32 kern_version, char *log_buf,
size_t log_buf_sz);

int bpf_map_update_elem(int fd, void *key, void *value,
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);
Expand Down
13 changes: 0 additions & 13 deletions tools/testing/selftests/bpf/bpf_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@ static inline int bpf_map_lookup(int fd, const void *key, void *value)
return bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
}

static inline int bpf_map_update(int fd, const void *key, const void *value,
uint64_t flags)
{
union bpf_attr attr = {};

attr.map_fd = fd;
attr.key = bpf_ptr_to_u64(key);
attr.value = bpf_ptr_to_u64(value);
attr.flags = flags;

return bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
}

static inline int bpf_map_delete(int fd, const void *key)
{
union bpf_attr attr = {};
Expand Down
15 changes: 8 additions & 7 deletions tools/testing/selftests/bpf/test_lpm_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <sys/time.h>
#include <sys/resource.h>

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

Expand Down Expand Up @@ -198,7 +199,7 @@ static void test_lpm_map(int keysize)

key->prefixlen = value[keysize];
memcpy(key->data, value, keysize);
r = bpf_map_update(map, key, value, 0);
r = bpf_map_update_elem(map, key, value, 0);
assert(!r);
}

Expand Down Expand Up @@ -266,32 +267,32 @@ static void test_lpm_ipaddr(void)
value = 1;
key_ipv4->prefixlen = 16;
inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
assert(bpf_map_update(map_fd_ipv4, key_ipv4, &value, 0) == 0);
assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);

value = 2;
key_ipv4->prefixlen = 24;
inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
assert(bpf_map_update(map_fd_ipv4, key_ipv4, &value, 0) == 0);
assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);

value = 3;
key_ipv4->prefixlen = 24;
inet_pton(AF_INET, "192.168.128.0", key_ipv4->data);
assert(bpf_map_update(map_fd_ipv4, key_ipv4, &value, 0) == 0);
assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);

value = 5;
key_ipv4->prefixlen = 24;
inet_pton(AF_INET, "192.168.1.0", key_ipv4->data);
assert(bpf_map_update(map_fd_ipv4, key_ipv4, &value, 0) == 0);
assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);

value = 4;
key_ipv4->prefixlen = 23;
inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
assert(bpf_map_update(map_fd_ipv4, key_ipv4, &value, 0) == 0);
assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);

value = 0xdeadbeef;
key_ipv6->prefixlen = 64;
inet_pton(AF_INET6, "2a00:1450:4001:814::200e", key_ipv6->data);
assert(bpf_map_update(map_fd_ipv6, key_ipv6, &value, 0) == 0);
assert(bpf_map_update_elem(map_fd_ipv6, key_ipv6, &value, 0) == 0);

/* Set tprefixlen to maximum for lookups */
key_ipv4->prefixlen = 32;
Expand Down
97 changes: 56 additions & 41 deletions tools/testing/selftests/bpf/test_lru_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <sys/wait.h>
#include <sys/resource.h>

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

Expand Down Expand Up @@ -119,15 +120,16 @@ static void test_lru_sanity0(int map_type, int map_flags)
/* insert key=1 element */

key = 1;
assert(!bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update(expected_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST));

/* BPF_NOEXIST means: add new element if it doesn't exist */
assert(bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST) == -1 &&
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST) == -1
/* key=1 already exists */
errno == EEXIST);
&& errno == EEXIST);

assert(bpf_map_update(lru_map_fd, &key, value, -1) == -1 &&
assert(bpf_map_update_elem(lru_map_fd, &key, value, -1) == -1 &&
errno == EINVAL);

/* insert key=2 element */
Expand All @@ -138,11 +140,11 @@ static void test_lru_sanity0(int map_type, int map_flags)
errno == ENOENT);

/* BPF_EXIST means: update existing element */
assert(bpf_map_update(lru_map_fd, &key, value, BPF_EXIST) == -1 &&
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_EXIST) == -1 &&
/* key=2 is not there */
errno == ENOENT);

assert(!bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST));

/* insert key=3 element */

Expand All @@ -159,8 +161,9 @@ static void test_lru_sanity0(int map_type, int map_flags)
assert(value[0] == 1234);

key = 3;
assert(!bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update(expected_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST));

/* key=2 has been removed from the LRU */
key = 2;
Expand Down Expand Up @@ -217,14 +220,15 @@ static void test_lru_sanity1(int map_type, int map_flags, unsigned int tgt_free)
/* Insert 1 to tgt_free (+tgt_free keys) */
end_key = 1 + tgt_free;
for (key = 1; key < end_key; key++)
assert(!bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update_elem(lru_map_fd, &key, value,
BPF_NOEXIST));

/* 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_update(expected_map_fd, &key, value,
BPF_NOEXIST));
assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST));
}

/* Insert 1+tgt_free to 2*tgt_free
Expand All @@ -234,9 +238,10 @@ static void test_lru_sanity1(int map_type, int map_flags, unsigned int tgt_free)
key = 1 + tgt_free;
end_key = key + tgt_free;
for (; key < end_key; key++) {
assert(!bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update(expected_map_fd, &key, value,
BPF_NOEXIST));
assert(!bpf_map_update_elem(lru_map_fd, &key, value,
BPF_NOEXIST));
assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST));
}

assert(map_equal(lru_map_fd, expected_map_fd));
Expand Down Expand Up @@ -301,9 +306,10 @@ static void test_lru_sanity2(int map_type, int map_flags, unsigned int tgt_free)
/* Insert 1 to tgt_free (+tgt_free keys) */
end_key = 1 + tgt_free;
for (key = 1; key < end_key; key++)
assert(!bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update_elem(lru_map_fd, &key, value,
BPF_NOEXIST));

/* Any bpf_map_update will require to acquire a new node
/* Any bpf_map_update_elem will require to acquire a new node
* from LRU first.
*
* The local list is running out of free nodes.
Expand All @@ -316,10 +322,12 @@ static void test_lru_sanity2(int map_type, int map_flags, unsigned int tgt_free)
*/
key = 1;
if (map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
assert(!bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update_elem(lru_map_fd, &key, value,
BPF_NOEXIST));
assert(!bpf_map_delete(lru_map_fd, &key));
} else {
assert(bpf_map_update(lru_map_fd, &key, value, BPF_EXIST));
assert(bpf_map_update_elem(lru_map_fd, &key, value,
BPF_EXIST));
}

/* Re-insert 1 to tgt_free/2 again and do a lookup
Expand All @@ -329,11 +337,12 @@ static void test_lru_sanity2(int map_type, int map_flags, unsigned int tgt_free)
value[0] = 4321;
for (key = 1; key < end_key; key++) {
assert(bpf_map_lookup(lru_map_fd, &key, value));
assert(!bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update_elem(lru_map_fd, &key, value,
BPF_NOEXIST));
assert(!bpf_map_lookup(lru_map_fd, &key, value));
assert(value[0] == 4321);
assert(!bpf_map_update(expected_map_fd, &key, value,
BPF_NOEXIST));
assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST));
}

value[0] = 1234;
Expand All @@ -344,14 +353,16 @@ static void test_lru_sanity2(int map_type, int map_flags, unsigned int tgt_free)
/* These newly added but not referenced keys will be
* gone during the next LRU shrink.
*/
assert(!bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update_elem(lru_map_fd, &key, value,
BPF_NOEXIST));

/* Insert 1+tgt_free*3/2 to tgt_free*5/2 */
end_key = key + tgt_free;
for (; key < end_key; key++) {
assert(!bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update(expected_map_fd, &key, value,
BPF_NOEXIST));
assert(!bpf_map_update_elem(lru_map_fd, &key, value,
BPF_NOEXIST));
assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST));
}

assert(map_equal(lru_map_fd, expected_map_fd));
Expand Down Expand Up @@ -401,14 +412,15 @@ static void test_lru_sanity3(int map_type, int map_flags, unsigned int tgt_free)
/* Insert 1 to 2*tgt_free (+2*tgt_free keys) */
end_key = 1 + (2 * tgt_free);
for (key = 1; key < end_key; key++)
assert(!bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update_elem(lru_map_fd, &key, value,
BPF_NOEXIST));

/* 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_update(expected_map_fd, &key, value,
BPF_NOEXIST));
assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST));
}

/* Add 1+2*tgt_free to tgt_free*5/2
Expand All @@ -417,9 +429,10 @@ static void test_lru_sanity3(int map_type, int map_flags, unsigned int tgt_free)
key = 2 * tgt_free + 1;
end_key = key + batch_size;
for (; key < end_key; key++) {
assert(!bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update(expected_map_fd, &key, value,
BPF_NOEXIST));
assert(!bpf_map_update_elem(lru_map_fd, &key, value,
BPF_NOEXIST));
assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST));
}

assert(map_equal(lru_map_fd, expected_map_fd));
Expand Down Expand Up @@ -457,15 +470,16 @@ static void test_lru_sanity4(int map_type, int map_flags, unsigned int tgt_free)
value[0] = 1234;

for (key = 1; key <= 2 * tgt_free; key++)
assert(!bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update_elem(lru_map_fd, &key, value,
BPF_NOEXIST));

key = 1;
assert(bpf_map_update(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++) {
assert(!bpf_map_lookup(lru_map_fd, &key, value));
assert(!bpf_map_update(expected_map_fd, &key, value,
BPF_NOEXIST));
assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST));
}

for (; key <= 2 * tgt_free; key++) {
Expand All @@ -475,9 +489,10 @@ static void test_lru_sanity4(int map_type, int map_flags, unsigned int tgt_free)

end_key = key + 2 * tgt_free;
for (; key < end_key; key++) {
assert(!bpf_map_update(lru_map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update(expected_map_fd, &key, value,
BPF_NOEXIST));
assert(!bpf_map_update_elem(lru_map_fd, &key, value,
BPF_NOEXIST));
assert(!bpf_map_update_elem(expected_map_fd, &key, value,
BPF_NOEXIST));
}

assert(map_equal(lru_map_fd, expected_map_fd));
Expand All @@ -498,7 +513,7 @@ static void do_test_lru_sanity5(unsigned long long last_key, int map_fd)
value[0] = 1234;

key = last_key + 1;
assert(!bpf_map_update(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));

/* Cannot find the last key because it was removed by LRU */
Expand All @@ -523,7 +538,7 @@ static void test_lru_sanity5(int map_type, int map_flags)

value[0] = 1234;
key = 0;
assert(!bpf_map_update(map_fd, &key, value, BPF_NOEXIST));
assert(!bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST));

while (sched_next_online(0, &next_cpu) != -1) {
pid_t pid;
Expand Down
Loading

0 comments on commit 10ecc72

Please sign in to comment.