]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
selftests/bpf: convert legacy BPF maps to BTF-defined ones
authorAndrii Nakryiko <andriin@fb.com>
Fri, 5 Jul 2019 15:50:12 +0000 (08:50 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Fri, 5 Jul 2019 20:52:25 +0000 (22:52 +0200)
Convert selftests that were originally left out and new ones added
recently to consistently use BTF-defined maps.

Reported-by: kernel test robot <rong.a.chen@intel.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
tools/testing/selftests/bpf/progs/get_cgroup_id_kern.c
tools/testing/selftests/bpf/progs/pyperf.h
tools/testing/selftests/bpf/progs/sockmap_verdict_prog.c
tools/testing/selftests/bpf/progs/strobemeta.h
tools/testing/selftests/bpf/progs/test_map_in_map.c
tools/testing/selftests/bpf/progs/test_obj_id.c
tools/testing/selftests/bpf/progs/test_xdp_loop.c
tools/testing/selftests/bpf/progs/xdp_redirect_map.c
tools/testing/selftests/bpf/progs/xdping_kern.c
tools/testing/selftests/bpf/test_queue_stack_map.h
tools/testing/selftests/bpf/test_sockmap_kern.h

index 014dba10b8a5dc1b6bb78ef54439c84c127bc13b..16c54ade6888c78930877bc373937d0dadc141c9 100644 (file)
@@ -4,19 +4,19 @@
 #include <linux/bpf.h>
 #include "bpf_helpers.h"
 
-struct bpf_map_def SEC("maps") cg_ids = {
-       .type = BPF_MAP_TYPE_ARRAY,
-       .key_size = sizeof(__u32),
-       .value_size = sizeof(__u64),
-       .max_entries = 1,
-};
-
-struct bpf_map_def SEC("maps") pidmap = {
-       .type = BPF_MAP_TYPE_ARRAY,
-       .key_size = sizeof(__u32),
-       .value_size = sizeof(__u32),
-       .max_entries = 1,
-};
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __uint(max_entries, 1);
+       __type(key, __u32);
+       __type(value, __u64);
+} cg_ids SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __uint(max_entries, 1);
+       __type(key, __u32);
+       __type(value, __u32);
+} pidmap SEC(".maps");
 
 SEC("tracepoint/syscalls/sys_enter_nanosleep")
 int trace(void *ctx)
index abf6224649bef517738ca7f365d06aca3c395c6d..003fe106fc70e94412c2108a12fa9abd65df9104 100644 (file)
@@ -58,14 +58,6 @@ typedef struct {
 } Event;
 
 
-struct bpf_elf_map {
-       __u32 type;
-       __u32 size_key;
-       __u32 size_value;
-       __u32 max_elem;
-       __u32 flags;
-};
-
 typedef int pid_t;
 
 typedef struct {
@@ -118,47 +110,47 @@ static __always_inline bool get_frame_data(void *frame_ptr, PidData *pidData,
        return true;
 }
 
-struct bpf_elf_map SEC("maps") pidmap = {
-       .type = BPF_MAP_TYPE_HASH,
-       .size_key = sizeof(int),
-       .size_value = sizeof(PidData),
-       .max_elem = 1,
-};
-
-struct bpf_elf_map SEC("maps") eventmap = {
-       .type = BPF_MAP_TYPE_HASH,
-       .size_key = sizeof(int),
-       .size_value = sizeof(Event),
-       .max_elem = 1,
-};
-
-struct bpf_elf_map SEC("maps") symbolmap = {
-       .type = BPF_MAP_TYPE_HASH,
-       .size_key = sizeof(Symbol),
-       .size_value = sizeof(int),
-       .max_elem = 1,
-};
-
-struct bpf_elf_map SEC("maps") statsmap = {
-       .type = BPF_MAP_TYPE_ARRAY,
-       .size_key = sizeof(Stats),
-       .size_value = sizeof(int),
-       .max_elem = 1,
-};
-
-struct bpf_elf_map SEC("maps") perfmap = {
-       .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
-       .size_key = sizeof(int),
-       .size_value = sizeof(int),
-       .max_elem = 32,
-};
-
-struct bpf_elf_map SEC("maps") stackmap = {
-       .type = BPF_MAP_TYPE_STACK_TRACE,
-       .size_key = sizeof(int),
-       .size_value = sizeof(long long) * 127,
-       .max_elem = 1000,
-};
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __uint(max_entries, 1);
+       __type(key, int);
+       __type(value, PidData);
+} pidmap SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __uint(max_entries, 1);
+       __type(key, int);
+       __type(value, Event);
+} eventmap SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __uint(max_entries, 1);
+       __type(key, Symbol);
+       __type(value, int);
+} symbolmap SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __uint(max_entries, 1);
+       __type(key, int);
+       __type(value, Stats);
+} statsmap SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
+       __uint(max_entries, 32);
+       __uint(key_size, sizeof(int));
+       __uint(value_size, sizeof(int));
+} perfmap SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_STACK_TRACE);
+       __uint(max_entries, 1000);
+       __uint(key_size, sizeof(int));
+       __uint(value_size, sizeof(long long) * 127);
+} stackmap SEC(".maps");
 
 static __always_inline int __on_event(struct pt_regs *ctx)
 {
index d85c874ef25e736273e4fd4d11fc617ea149dcd7..433e23918a6217bbd96d37f3bf77c4f9ba9cd624 100644 (file)
@@ -4,33 +4,33 @@
 
 int _version SEC("version") = 1;
 
-struct bpf_map_def SEC("maps") sock_map_rx = {
-       .type = BPF_MAP_TYPE_SOCKMAP,
-       .key_size = sizeof(int),
-       .value_size = sizeof(int),
-       .max_entries = 20,
-};
+struct {
+       __uint(type, BPF_MAP_TYPE_SOCKMAP);
+       __uint(max_entries, 20);
+       __uint(key_size, sizeof(int));
+       __uint(value_size, sizeof(int));
+} sock_map_rx SEC(".maps");
 
-struct bpf_map_def SEC("maps") sock_map_tx = {
-       .type = BPF_MAP_TYPE_SOCKMAP,
-       .key_size = sizeof(int),
-       .value_size = sizeof(int),
-       .max_entries = 20,
-};
+struct {
+       __uint(type, BPF_MAP_TYPE_SOCKMAP);
+       __uint(max_entries, 20);
+       __uint(key_size, sizeof(int));
+       __uint(value_size, sizeof(int));
+} sock_map_tx SEC(".maps");
 
-struct bpf_map_def SEC("maps") sock_map_msg = {
-       .type = BPF_MAP_TYPE_SOCKMAP,
-       .key_size = sizeof(int),
-       .value_size = sizeof(int),
-       .max_entries = 20,
-};
+struct {
+       __uint(type, BPF_MAP_TYPE_SOCKMAP);
+       __uint(max_entries, 20);
+       __uint(key_size, sizeof(int));
+       __uint(value_size, sizeof(int));
+} sock_map_msg SEC(".maps");
 
-struct bpf_map_def SEC("maps") sock_map_break = {
-       .type = BPF_MAP_TYPE_ARRAY,
-       .key_size = sizeof(int),
-       .value_size = sizeof(int),
-       .max_entries = 20,
-};
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __uint(max_entries, 20);
+       __type(key, int);
+       __type(value, int);
+} sock_map_break SEC(".maps");
 
 SEC("sk_skb2")
 int bpf_prog2(struct __sk_buff *skb)
index 553bc3b62e89d2b555db14b3bb712a4f66d1fb06..8a399bdfd92037ca1f07fc7f7abd64e4fcf5de42 100644 (file)
@@ -204,40 +204,40 @@ struct strobelight_bpf_sample {
        char dummy_safeguard;
 };
 
-struct bpf_map_def SEC("maps") samples = {
-       .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
-       .key_size = sizeof(int),
-       .value_size = sizeof(int),
-       .max_entries = 32,
-};
-
-struct bpf_map_def SEC("maps") stacks_0 = {
-       .type = BPF_MAP_TYPE_STACK_TRACE,
-       .key_size = sizeof(uint32_t),
-       .value_size = sizeof(uint64_t) * PERF_MAX_STACK_DEPTH,
-       .max_entries = 16,
-};
-
-struct bpf_map_def SEC("maps") stacks_1 = {
-       .type = BPF_MAP_TYPE_STACK_TRACE,
-       .key_size = sizeof(uint32_t),
-       .value_size = sizeof(uint64_t) * PERF_MAX_STACK_DEPTH,
-       .max_entries = 16,
-};
-
-struct bpf_map_def SEC("maps") sample_heap = {
-       .type = BPF_MAP_TYPE_PERCPU_ARRAY,
-       .key_size = sizeof(uint32_t),
-       .value_size = sizeof(struct strobelight_bpf_sample),
-       .max_entries = 1,
-};
-
-struct bpf_map_def SEC("maps") strobemeta_cfgs = {
-       .type = BPF_MAP_TYPE_PERCPU_ARRAY,
-       .key_size = sizeof(pid_t),
-       .value_size = sizeof(struct strobemeta_cfg),
-       .max_entries = STROBE_MAX_CFGS,
-};
+struct {
+       __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
+       __uint(max_entries, 32);
+       __uint(key_size, sizeof(int));
+       __uint(value_size, sizeof(int));
+} samples SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_STACK_TRACE);
+       __uint(max_entries, 16);
+       __uint(key_size, sizeof(uint32_t));
+       __uint(value_size, sizeof(uint64_t) * PERF_MAX_STACK_DEPTH);
+} stacks_0 SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_STACK_TRACE);
+       __uint(max_entries, 16);
+       __uint(key_size, sizeof(uint32_t));
+       __uint(value_size, sizeof(uint64_t) * PERF_MAX_STACK_DEPTH);
+} stacks_1 SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+       __uint(max_entries, 1);
+       __type(key, uint32_t);
+       __type(value, struct strobelight_bpf_sample);
+} sample_heap SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+       __uint(max_entries, STROBE_MAX_CFGS);
+       __type(key, pid_t);
+       __type(value, struct strobemeta_cfg);
+} strobemeta_cfgs SEC(".maps");
 
 /* Type for the dtv.  */
 /* https://github.com/lattera/glibc/blob/master/nptl/sysdeps/x86_64/tls.h#L34 */
index 2985f262846e0b77bd90e083f535e95f4e880637..113226115365a5c5a6a00c43816370ee21c0d972 100644 (file)
@@ -5,23 +5,23 @@
 #include <linux/types.h>
 #include "bpf_helpers.h"
 
-struct bpf_map_def SEC("maps") mim_array = {
-       .type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
-       .key_size = sizeof(int),
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
+       __uint(max_entries, 1);
+       __uint(map_flags, 0);
+       __uint(key_size, sizeof(__u32));
        /* must be sizeof(__u32) for map in map */
-       .value_size = sizeof(__u32),
-       .max_entries = 1,
-       .map_flags = 0,
-};
-
-struct bpf_map_def SEC("maps") mim_hash = {
-       .type = BPF_MAP_TYPE_HASH_OF_MAPS,
-       .key_size = sizeof(int),
+       __uint(value_size, sizeof(__u32));
+} mim_array SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
+       __uint(max_entries, 1);
+       __uint(map_flags, 0);
+       __uint(key_size, sizeof(int));
        /* must be sizeof(__u32) for map in map */
-       .value_size = sizeof(__u32),
-       .max_entries = 1,
-       .map_flags = 0,
-};
+       __uint(value_size, sizeof(__u32));
+} mim_hash SEC(".maps");
 
 SEC("xdp_mimtest")
 int xdp_mimtest0(struct xdp_md *ctx)
index 726340fa6fe09a7486e9ce6a91e61ab0a690c6d6..3d30c02bdae9fd765671c92f564935ff60b32804 100644 (file)
 
 int _version SEC("version") = 1;
 
-struct bpf_map_def SEC("maps") test_map_id = {
-       .type = BPF_MAP_TYPE_ARRAY,
-       .key_size = sizeof(__u32),
-       .value_size = sizeof(__u64),
-       .max_entries = 1,
-};
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __uint(max_entries, 1);
+       __type(key, __u32);
+       __type(value, __u64);
+} test_map_id SEC(".maps");
 
 SEC("test_obj_id_dummy")
 int test_obj_id(struct __sk_buff *skb)
index 7fa4677df22e17fff0f99eae030a67f2584cca78..97175f73c3fea4b55cf11a11b757d463cfced321 100644 (file)
 
 int _version SEC("version") = 1;
 
-struct bpf_map_def SEC("maps") rxcnt = {
-       .type = BPF_MAP_TYPE_PERCPU_ARRAY,
-       .key_size = sizeof(__u32),
-       .value_size = sizeof(__u64),
-       .max_entries = 256,
-};
-
-struct bpf_map_def SEC("maps") vip2tnl = {
-       .type = BPF_MAP_TYPE_HASH,
-       .key_size = sizeof(struct vip),
-       .value_size = sizeof(struct iptnl_info),
-       .max_entries = MAX_IPTNL_ENTRIES,
-};
+struct {
+       __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+       __uint(max_entries, 256);
+       __type(key, __u32);
+       __type(value, __u64);
+} rxcnt SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __uint(max_entries, MAX_IPTNL_ENTRIES);
+       __type(key, struct vip);
+       __type(value, struct iptnl_info);
+} vip2tnl SEC(".maps");
 
 static __always_inline void count_tx(__u32 protocol)
 {
index e87a985b9df9c3589cb41679b7155b850aff1a1d..1c5f298d7196185c0a270cd2212bb865f45cebdf 100644 (file)
@@ -3,12 +3,12 @@
 #include <linux/bpf.h>
 #include "bpf_helpers.h"
 
-struct bpf_map_def SEC("maps") tx_port = {
-       .type = BPF_MAP_TYPE_DEVMAP,
-       .key_size = sizeof(int),
-       .value_size = sizeof(int),
-       .max_entries = 8,
-};
+struct {
+       __uint(type, BPF_MAP_TYPE_DEVMAP);
+       __uint(max_entries, 8);
+       __uint(key_size, sizeof(int));
+       __uint(value_size, sizeof(int));
+} tx_port SEC(".maps");
 
 SEC("redirect_map_0")
 int xdp_redirect_map_0(struct xdp_md *xdp)
index 87393e7c667c819ee5ca3426811ef58f9a1ee95a..112a2857f4e274d7a9878303698ac90c3046d980 100644 (file)
 
 #include "xdping.h"
 
-struct bpf_map_def SEC("maps") ping_map = {
-       .type = BPF_MAP_TYPE_HASH,
-       .key_size = sizeof(__u32),
-       .value_size = sizeof(struct pinginfo),
-       .max_entries = 256,
-};
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __uint(max_entries, 256);
+       __type(key, __u32);
+       __type(value, struct pinginfo);
+} ping_map SEC(".maps");
 
 static __always_inline void swap_src_dst_mac(void *data)
 {
index 295b9b3bc5c72d93c922b447ebac2d8d6318e9fd..0e014d3b2b36ccc56d94a87f9bf175bdde9b2fff 100644 (file)
 
 int _version SEC("version") = 1;
 
-struct bpf_map_def __attribute__ ((section("maps"), used)) map_in = {
-       .type = MAP_TYPE,
-       .key_size = 0,
-       .value_size = sizeof(__u32),
-       .max_entries = 32,
-       .map_flags = 0,
-};
-
-struct bpf_map_def __attribute__ ((section("maps"), used)) map_out = {
-       .type = MAP_TYPE,
-       .key_size = 0,
-       .value_size = sizeof(__u32),
-       .max_entries = 32,
-       .map_flags = 0,
-};
+struct {
+       __uint(type, MAP_TYPE);
+       __uint(max_entries, 32);
+       __uint(map_flags, 0);
+       __uint(key_size, 0);
+       __uint(value_size, sizeof(__u32));
+} map_in SEC(".maps");
+
+struct {
+       __uint(type, MAP_TYPE);
+       __uint(max_entries, 32);
+       __uint(map_flags, 0);
+       __uint(key_size, 0);
+       __uint(value_size, sizeof(__u32));
+} map_out SEC(".maps");
 
 SEC("test")
 int _test(struct __sk_buff *skb)
index 4e7d3da21357ea62421d5fce69e19e4e64fbda09..d008b41b7d8d98daea38b4a579e1f38bc5e800f8 100644 (file)
  * are established and verdicts are decided.
  */
 
-struct bpf_map_def SEC("maps") sock_map = {
-       .type = TEST_MAP_TYPE,
-       .key_size = sizeof(int),
-       .value_size = sizeof(int),
-       .max_entries = 20,
-};
-
-struct bpf_map_def SEC("maps") sock_map_txmsg = {
-       .type = TEST_MAP_TYPE,
-       .key_size = sizeof(int),
-       .value_size = sizeof(int),
-       .max_entries = 20,
-};
-
-struct bpf_map_def SEC("maps") sock_map_redir = {
-       .type = TEST_MAP_TYPE,
-       .key_size = sizeof(int),
-       .value_size = sizeof(int),
-       .max_entries = 20,
-};
-
-struct bpf_map_def SEC("maps") sock_apply_bytes = {
-       .type = BPF_MAP_TYPE_ARRAY,
-       .key_size = sizeof(int),
-       .value_size = sizeof(int),
-       .max_entries = 1
-};
-
-struct bpf_map_def SEC("maps") sock_cork_bytes = {
-       .type = BPF_MAP_TYPE_ARRAY,
-       .key_size = sizeof(int),
-       .value_size = sizeof(int),
-       .max_entries = 1
-};
-
-struct bpf_map_def SEC("maps") sock_bytes = {
-       .type = BPF_MAP_TYPE_ARRAY,
-       .key_size = sizeof(int),
-       .value_size = sizeof(int),
-       .max_entries = 6
-};
-
-struct bpf_map_def SEC("maps") sock_redir_flags = {
-       .type = BPF_MAP_TYPE_ARRAY,
-       .key_size = sizeof(int),
-       .value_size = sizeof(int),
-       .max_entries = 1
-};
-
-struct bpf_map_def SEC("maps") sock_skb_opts = {
-       .type = BPF_MAP_TYPE_ARRAY,
-       .key_size = sizeof(int),
-       .value_size = sizeof(int),
-       .max_entries = 1
-};
+struct {
+       __uint(type, TEST_MAP_TYPE);
+       __uint(max_entries, 20);
+       __uint(key_size, sizeof(int));
+       __uint(value_size, sizeof(int));
+} sock_map SEC(".maps");
+
+struct {
+       __uint(type, TEST_MAP_TYPE);
+       __uint(max_entries, 20);
+       __uint(key_size, sizeof(int));
+       __uint(value_size, sizeof(int));
+} sock_map_txmsg SEC(".maps");
+
+struct {
+       __uint(type, TEST_MAP_TYPE);
+       __uint(max_entries, 20);
+       __uint(key_size, sizeof(int));
+       __uint(value_size, sizeof(int));
+} sock_map_redir SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __uint(max_entries, 1);
+       __type(key, int);
+       __type(value, int);
+} sock_apply_bytes SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __uint(max_entries, 1);
+       __type(key, int);
+       __type(value, int);
+} sock_cork_bytes SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __uint(max_entries, 6);
+       __type(key, int);
+       __type(value, int);
+} sock_bytes SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __uint(max_entries, 1);
+       __type(key, int);
+       __type(value, int);
+} sock_redir_flags SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __uint(max_entries, 1);
+       __type(key, int);
+       __type(value, int);
+} sock_skb_opts SEC(".maps");
 
 SEC("sk_skb1")
 int bpf_prog1(struct __sk_buff *skb)