samples/bpf: change _kern suffix to .bpf with BPF test programs
authorDaniel T. Lee <danieltimlee@gmail.com>
Sun, 15 Jan 2023 07:16:13 +0000 (16:16 +0900)
committerAlexei Starovoitov <ast@kernel.org>
Sun, 15 Jan 2023 21:32:45 +0000 (13:32 -0800)
This commit changes the _kern suffix to .bpf with the BPF test programs.
With this modification, test programs will inherit the benefit of the
new CLANG-BPF compile target.

Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Link: https://lore.kernel.org/r/20230115071613.125791-11-danieltimlee@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
20 files changed:
samples/bpf/Makefile
samples/bpf/lwt_len_hist.bpf.c [new file with mode: 0644]
samples/bpf/lwt_len_hist.sh
samples/bpf/lwt_len_hist_kern.c [deleted file]
samples/bpf/sock_flags.bpf.c [new file with mode: 0644]
samples/bpf/sock_flags_kern.c [deleted file]
samples/bpf/test_cgrp2_sock2.sh
samples/bpf/test_cgrp2_tc.bpf.c [new file with mode: 0644]
samples/bpf/test_cgrp2_tc.sh
samples/bpf/test_cgrp2_tc_kern.c [deleted file]
samples/bpf/test_map_in_map.bpf.c [new file with mode: 0644]
samples/bpf/test_map_in_map_kern.c [deleted file]
samples/bpf/test_map_in_map_user.c
samples/bpf/test_overhead_kprobe.bpf.c [new file with mode: 0644]
samples/bpf/test_overhead_kprobe_kern.c [deleted file]
samples/bpf/test_overhead_raw_tp.bpf.c [new file with mode: 0644]
samples/bpf/test_overhead_raw_tp_kern.c [deleted file]
samples/bpf/test_overhead_tp.bpf.c [new file with mode: 0644]
samples/bpf/test_overhead_tp_kern.c [deleted file]
samples/bpf/test_overhead_user.c

index 22039a0a5b35e7bd4214fcc763e0b5248f0b7152..615f24ebc49cc0a3f91aa60d02d737a0b3da4b24 100644 (file)
@@ -131,7 +131,7 @@ always-y += tracex4_kern.o
 always-y += tracex5_kern.o
 always-y += tracex6_kern.o
 always-y += tracex7_kern.o
-always-y += sock_flags_kern.o
+always-y += sock_flags.bpf.o
 always-y += test_probe_write_user.bpf.o
 always-y += trace_output.bpf.o
 always-y += tcbpf1_kern.o
@@ -140,19 +140,19 @@ always-y += lathist_kern.o
 always-y += offwaketime_kern.o
 always-y += spintest_kern.o
 always-y += map_perf_test.bpf.o
-always-y += test_overhead_tp_kern.o
-always-y += test_overhead_raw_tp_kern.o
-always-y += test_overhead_kprobe_kern.o
+always-y += test_overhead_tp.bpf.o
+always-y += test_overhead_raw_tp.bpf.o
+always-y += test_overhead_kprobe.bpf.o
 always-y += parse_varlen.o parse_simple.o parse_ldabs.o
-always-y += test_cgrp2_tc_kern.o
+always-y += test_cgrp2_tc.bpf.o
 always-y += xdp1_kern.o
 always-y += xdp2_kern.o
 always-y += test_current_task_under_cgroup.bpf.o
 always-y += trace_event_kern.o
 always-y += sampleip_kern.o
-always-y += lwt_len_hist_kern.o
+always-y += lwt_len_hist.bpf.o
 always-y += xdp_tx_iptunnel_kern.o
-always-y += test_map_in_map_kern.o
+always-y += test_map_in_map.bpf.o
 always-y += tcp_synrto_kern.o
 always-y += tcp_rwnd_kern.o
 always-y += tcp_bufs_kern.o
diff --git a/samples/bpf/lwt_len_hist.bpf.c b/samples/bpf/lwt_len_hist.bpf.c
new file mode 100644 (file)
index 0000000..dbab80e
--- /dev/null
@@ -0,0 +1,62 @@
+/* Copyright (c) 2016 Thomas Graf <tgraf@tgraf.ch>
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+struct {
+       __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
+       __type(key, u64);
+       __type(value, u64);
+       __uint(pinning, LIBBPF_PIN_BY_NAME);
+       __uint(max_entries, 1024);
+} lwt_len_hist_map SEC(".maps");
+
+static unsigned int log2(unsigned int v)
+{
+       unsigned int r;
+       unsigned int shift;
+
+       r = (v > 0xFFFF) << 4; v >>= r;
+       shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
+       shift = (v > 0xF) << 2; v >>= shift; r |= shift;
+       shift = (v > 0x3) << 1; v >>= shift; r |= shift;
+       r |= (v >> 1);
+       return r;
+}
+
+static unsigned int log2l(unsigned long v)
+{
+       unsigned int hi = v >> 32;
+       if (hi)
+               return log2(hi) + 32;
+       else
+               return log2(v);
+}
+
+SEC("len_hist")
+int do_len_hist(struct __sk_buff *skb)
+{
+       __u64 *value, key, init_val = 1;
+
+       key = log2l(skb->len);
+
+       value = bpf_map_lookup_elem(&lwt_len_hist_map, &key);
+       if (value)
+               __sync_fetch_and_add(value, 1);
+       else
+               bpf_map_update_elem(&lwt_len_hist_map, &key, &init_val, BPF_ANY);
+
+       return BPF_OK;
+}
+
+char _license[] SEC("license") = "GPL";
index ff7d1ba0f7ed01d18688155524969a43723d170e..7078bfcc4f4d74680141fa7d36e4c4322ce601cc 100755 (executable)
@@ -4,7 +4,7 @@
 NS1=lwt_ns1
 VETH0=tst_lwt1a
 VETH1=tst_lwt1b
-BPF_PROG=lwt_len_hist_kern.o
+BPF_PROG=lwt_len_hist.bpf.o
 TRACE_ROOT=/sys/kernel/debug/tracing
 
 function cleanup {
diff --git a/samples/bpf/lwt_len_hist_kern.c b/samples/bpf/lwt_len_hist_kern.c
deleted file mode 100644 (file)
index dbab80e..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-/* Copyright (c) 2016 Thomas Graf <tgraf@tgraf.ch>
- *
- * 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.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- */
-
-#include "vmlinux.h"
-#include <bpf/bpf_helpers.h>
-
-struct {
-       __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
-       __type(key, u64);
-       __type(value, u64);
-       __uint(pinning, LIBBPF_PIN_BY_NAME);
-       __uint(max_entries, 1024);
-} lwt_len_hist_map SEC(".maps");
-
-static unsigned int log2(unsigned int v)
-{
-       unsigned int r;
-       unsigned int shift;
-
-       r = (v > 0xFFFF) << 4; v >>= r;
-       shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
-       shift = (v > 0xF) << 2; v >>= shift; r |= shift;
-       shift = (v > 0x3) << 1; v >>= shift; r |= shift;
-       r |= (v >> 1);
-       return r;
-}
-
-static unsigned int log2l(unsigned long v)
-{
-       unsigned int hi = v >> 32;
-       if (hi)
-               return log2(hi) + 32;
-       else
-               return log2(v);
-}
-
-SEC("len_hist")
-int do_len_hist(struct __sk_buff *skb)
-{
-       __u64 *value, key, init_val = 1;
-
-       key = log2l(skb->len);
-
-       value = bpf_map_lookup_elem(&lwt_len_hist_map, &key);
-       if (value)
-               __sync_fetch_and_add(value, 1);
-       else
-               bpf_map_update_elem(&lwt_len_hist_map, &key, &init_val, BPF_ANY);
-
-       return BPF_OK;
-}
-
-char _license[] SEC("license") = "GPL";
diff --git a/samples/bpf/sock_flags.bpf.c b/samples/bpf/sock_flags.bpf.c
new file mode 100644 (file)
index 0000000..0da749f
--- /dev/null
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include "net_shared.h"
+#include <bpf/bpf_helpers.h>
+
+SEC("cgroup/sock")
+int bpf_prog1(struct bpf_sock *sk)
+{
+       char fmt[] = "socket: family %d type %d protocol %d\n";
+       char fmt2[] = "socket: uid %u gid %u\n";
+       __u64 gid_uid = bpf_get_current_uid_gid();
+       __u32 uid = gid_uid & 0xffffffff;
+       __u32 gid = gid_uid >> 32;
+
+       bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
+       bpf_trace_printk(fmt2, sizeof(fmt2), uid, gid);
+
+       /* block AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6 sockets
+        * ie., make ping6 fail
+        */
+       if (sk->family == AF_INET6 &&
+           sk->type == SOCK_DGRAM   &&
+           sk->protocol == IPPROTO_ICMPV6)
+               return 0;
+
+       return 1;
+}
+
+SEC("cgroup/sock")
+int bpf_prog2(struct bpf_sock *sk)
+{
+       char fmt[] = "socket: family %d type %d protocol %d\n";
+
+       bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
+
+       /* block AF_INET, SOCK_DGRAM, IPPROTO_ICMP sockets
+        * ie., make ping fail
+        */
+       if (sk->family == AF_INET &&
+           sk->type == SOCK_DGRAM  &&
+           sk->protocol == IPPROTO_ICMP)
+               return 0;
+
+       return 1;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/samples/bpf/sock_flags_kern.c b/samples/bpf/sock_flags_kern.c
deleted file mode 100644 (file)
index 0da749f..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-#include "vmlinux.h"
-#include "net_shared.h"
-#include <bpf/bpf_helpers.h>
-
-SEC("cgroup/sock")
-int bpf_prog1(struct bpf_sock *sk)
-{
-       char fmt[] = "socket: family %d type %d protocol %d\n";
-       char fmt2[] = "socket: uid %u gid %u\n";
-       __u64 gid_uid = bpf_get_current_uid_gid();
-       __u32 uid = gid_uid & 0xffffffff;
-       __u32 gid = gid_uid >> 32;
-
-       bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
-       bpf_trace_printk(fmt2, sizeof(fmt2), uid, gid);
-
-       /* block AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6 sockets
-        * ie., make ping6 fail
-        */
-       if (sk->family == AF_INET6 &&
-           sk->type == SOCK_DGRAM   &&
-           sk->protocol == IPPROTO_ICMPV6)
-               return 0;
-
-       return 1;
-}
-
-SEC("cgroup/sock")
-int bpf_prog2(struct bpf_sock *sk)
-{
-       char fmt[] = "socket: family %d type %d protocol %d\n";
-
-       bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
-
-       /* block AF_INET, SOCK_DGRAM, IPPROTO_ICMP sockets
-        * ie., make ping fail
-        */
-       if (sk->family == AF_INET &&
-           sk->type == SOCK_DGRAM  &&
-           sk->protocol == IPPROTO_ICMP)
-               return 0;
-
-       return 1;
-}
-
-char _license[] SEC("license") = "GPL";
index 00cc8d15373cabf5bc6b95f899d7089731b52dcd..82acff93d73975ceac33ea953425677d2d825580 100755 (executable)
@@ -5,7 +5,7 @@ BPFFS=/sys/fs/bpf
 MY_DIR=$(dirname $0)
 TEST=$MY_DIR/test_cgrp2_sock2
 LINK_PIN=$BPFFS/test_cgrp2_sock2
-BPF_PROG=$MY_DIR/sock_flags_kern.o
+BPF_PROG=$MY_DIR/sock_flags.bpf.o
 
 function config_device {
        ip netns add at_ns0
diff --git a/samples/bpf/test_cgrp2_tc.bpf.c b/samples/bpf/test_cgrp2_tc.bpf.c
new file mode 100644 (file)
index 0000000..c7d2291
--- /dev/null
@@ -0,0 +1,56 @@
+/* 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.
+ */
+#define KBUILD_MODNAME "foo"
+#include "vmlinux.h"
+#include "net_shared.h"
+#include <bpf/bpf_helpers.h>
+
+/* copy of 'struct ethhdr' without __packed */
+struct eth_hdr {
+       unsigned char   h_dest[ETH_ALEN];
+       unsigned char   h_source[ETH_ALEN];
+       unsigned short  h_proto;
+};
+
+struct {
+       __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
+       __type(key, u32);
+       __type(value, u32);
+       __uint(pinning, LIBBPF_PIN_BY_NAME);
+       __uint(max_entries, 1);
+} test_cgrp2_array_pin SEC(".maps");
+
+SEC("filter")
+int handle_egress(struct __sk_buff *skb)
+{
+       void *data = (void *)(long)skb->data;
+       struct eth_hdr *eth = data;
+       struct ipv6hdr *ip6h = data + sizeof(*eth);
+       void *data_end = (void *)(long)skb->data_end;
+       char dont_care_msg[] = "dont care %04x %d\n";
+       char pass_msg[] = "pass\n";
+       char reject_msg[] = "reject\n";
+
+       /* single length check */
+       if (data + sizeof(*eth) + sizeof(*ip6h) > data_end)
+               return TC_ACT_OK;
+
+       if (eth->h_proto != bpf_htons(ETH_P_IPV6) ||
+           ip6h->nexthdr != IPPROTO_ICMPV6) {
+               bpf_trace_printk(dont_care_msg, sizeof(dont_care_msg),
+                                eth->h_proto, ip6h->nexthdr);
+               return TC_ACT_OK;
+       } else if (bpf_skb_under_cgroup(skb, &test_cgrp2_array_pin, 0) != 1) {
+               bpf_trace_printk(pass_msg, sizeof(pass_msg));
+               return TC_ACT_OK;
+       } else {
+               bpf_trace_printk(reject_msg, sizeof(reject_msg));
+               return TC_ACT_SHOT;
+       }
+}
+
+char _license[] SEC("license") = "GPL";
index 37a2c9cba6d00e07f764ac772857f33f6857494e..38e8dbc9d16eeb1aa7f4867a016eb3502acd6e8b 100755 (executable)
@@ -4,7 +4,7 @@
 MY_DIR=$(dirname $0)
 # Details on the bpf prog
 BPF_CGRP2_ARRAY_NAME='test_cgrp2_array_pin'
-BPF_PROG="$MY_DIR/test_cgrp2_tc_kern.o"
+BPF_PROG="$MY_DIR/test_cgrp2_tc.bpf.o"
 BPF_SECTION='filter'
 
 [ -z "$TC" ] && TC='tc'
diff --git a/samples/bpf/test_cgrp2_tc_kern.c b/samples/bpf/test_cgrp2_tc_kern.c
deleted file mode 100644 (file)
index c7d2291..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-/* 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.
- */
-#define KBUILD_MODNAME "foo"
-#include "vmlinux.h"
-#include "net_shared.h"
-#include <bpf/bpf_helpers.h>
-
-/* copy of 'struct ethhdr' without __packed */
-struct eth_hdr {
-       unsigned char   h_dest[ETH_ALEN];
-       unsigned char   h_source[ETH_ALEN];
-       unsigned short  h_proto;
-};
-
-struct {
-       __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
-       __type(key, u32);
-       __type(value, u32);
-       __uint(pinning, LIBBPF_PIN_BY_NAME);
-       __uint(max_entries, 1);
-} test_cgrp2_array_pin SEC(".maps");
-
-SEC("filter")
-int handle_egress(struct __sk_buff *skb)
-{
-       void *data = (void *)(long)skb->data;
-       struct eth_hdr *eth = data;
-       struct ipv6hdr *ip6h = data + sizeof(*eth);
-       void *data_end = (void *)(long)skb->data_end;
-       char dont_care_msg[] = "dont care %04x %d\n";
-       char pass_msg[] = "pass\n";
-       char reject_msg[] = "reject\n";
-
-       /* single length check */
-       if (data + sizeof(*eth) + sizeof(*ip6h) > data_end)
-               return TC_ACT_OK;
-
-       if (eth->h_proto != bpf_htons(ETH_P_IPV6) ||
-           ip6h->nexthdr != IPPROTO_ICMPV6) {
-               bpf_trace_printk(dont_care_msg, sizeof(dont_care_msg),
-                                eth->h_proto, ip6h->nexthdr);
-               return TC_ACT_OK;
-       } else if (bpf_skb_under_cgroup(skb, &test_cgrp2_array_pin, 0) != 1) {
-               bpf_trace_printk(pass_msg, sizeof(pass_msg));
-               return TC_ACT_OK;
-       } else {
-               bpf_trace_printk(reject_msg, sizeof(reject_msg));
-               return TC_ACT_SHOT;
-       }
-}
-
-char _license[] SEC("license") = "GPL";
diff --git a/samples/bpf/test_map_in_map.bpf.c b/samples/bpf/test_map_in_map.bpf.c
new file mode 100644 (file)
index 0000000..1883559
--- /dev/null
@@ -0,0 +1,176 @@
+/*
+ * Copyright (c) 2017 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.
+ */
+#define KBUILD_MODNAME "foo"
+#include "vmlinux.h"
+#include <linux/version.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+
+#define MAX_NR_PORTS 65536
+
+#define EINVAL 22
+#define ENOENT 2
+
+/* map #0 */
+struct inner_a {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __type(key, u32);
+       __type(value, int);
+       __uint(max_entries, MAX_NR_PORTS);
+} port_a SEC(".maps");
+
+/* map #1 */
+struct inner_h {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __type(key, u32);
+       __type(value, int);
+       __uint(max_entries, 1);
+} port_h SEC(".maps");
+
+/* map #2 */
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __type(key, u32);
+       __type(value, int);
+       __uint(max_entries, 1);
+} reg_result_h SEC(".maps");
+
+/* map #3 */
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __type(key, u32);
+       __type(value, int);
+       __uint(max_entries, 1);
+} inline_result_h SEC(".maps");
+
+/* map #4 */ /* Test case #0 */
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
+       __uint(max_entries, MAX_NR_PORTS);
+       __uint(key_size, sizeof(u32));
+       __array(values, struct inner_a); /* use inner_a as inner map */
+} a_of_port_a SEC(".maps");
+
+/* map #5 */ /* Test case #1 */
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
+       __uint(max_entries, 1);
+       __uint(key_size, sizeof(u32));
+       __array(values, struct inner_a); /* use inner_a as inner map */
+} h_of_port_a SEC(".maps");
+
+/* map #6 */ /* Test case #2 */
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
+       __uint(max_entries, 1);
+       __uint(key_size, sizeof(u32));
+       __array(values, struct inner_h); /* use inner_h as inner map */
+} h_of_port_h SEC(".maps");
+
+static __always_inline int do_reg_lookup(void *inner_map, u32 port)
+{
+       int *result;
+
+       result = bpf_map_lookup_elem(inner_map, &port);
+       return result ? *result : -ENOENT;
+}
+
+static __always_inline int do_inline_array_lookup(void *inner_map, u32 port)
+{
+       int *result;
+
+       if (inner_map != &port_a)
+               return -EINVAL;
+
+       result = bpf_map_lookup_elem(&port_a, &port);
+       return result ? *result : -ENOENT;
+}
+
+static __always_inline int do_inline_hash_lookup(void *inner_map, u32 port)
+{
+       int *result;
+
+       if (inner_map != &port_h)
+               return -EINVAL;
+
+       result = bpf_map_lookup_elem(&port_h, &port);
+       return result ? *result : -ENOENT;
+}
+
+SEC("kprobe/__sys_connect")
+int trace_sys_connect(struct pt_regs *ctx)
+{
+       struct sockaddr_in6 *in6;
+       u16 test_case, port, dst6[8];
+       int addrlen, ret, inline_ret, ret_key = 0;
+       u32 port_key;
+       void *outer_map, *inner_map;
+       bool inline_hash = false;
+
+       in6 = (struct sockaddr_in6 *)PT_REGS_PARM2_CORE(ctx);
+       addrlen = (int)PT_REGS_PARM3_CORE(ctx);
+
+       if (addrlen != sizeof(*in6))
+               return 0;
+
+       ret = bpf_probe_read_user(dst6, sizeof(dst6), &in6->sin6_addr);
+       if (ret) {
+               inline_ret = ret;
+               goto done;
+       }
+
+       if (dst6[0] != 0xdead || dst6[1] != 0xbeef)
+               return 0;
+
+       test_case = dst6[7];
+
+       ret = bpf_probe_read_user(&port, sizeof(port), &in6->sin6_port);
+       if (ret) {
+               inline_ret = ret;
+               goto done;
+       }
+
+       port_key = port;
+
+       ret = -ENOENT;
+       if (test_case == 0) {
+               outer_map = &a_of_port_a;
+       } else if (test_case == 1) {
+               outer_map = &h_of_port_a;
+       } else if (test_case == 2) {
+               outer_map = &h_of_port_h;
+       } else {
+               ret = __LINE__;
+               inline_ret = ret;
+               goto done;
+       }
+
+       inner_map = bpf_map_lookup_elem(outer_map, &port_key);
+       if (!inner_map) {
+               ret = __LINE__;
+               inline_ret = ret;
+               goto done;
+       }
+
+       ret = do_reg_lookup(inner_map, port_key);
+
+       if (test_case == 0 || test_case == 1)
+               inline_ret = do_inline_array_lookup(inner_map, port_key);
+       else
+               inline_ret = do_inline_hash_lookup(inner_map, port_key);
+
+done:
+       bpf_map_update_elem(&reg_result_h, &ret_key, &ret, BPF_ANY);
+       bpf_map_update_elem(&inline_result_h, &ret_key, &inline_ret, BPF_ANY);
+
+       return 0;
+}
+
+char _license[] SEC("license") = "GPL";
+u32 _version SEC("version") = LINUX_VERSION_CODE;
diff --git a/samples/bpf/test_map_in_map_kern.c b/samples/bpf/test_map_in_map_kern.c
deleted file mode 100644 (file)
index 1883559..0000000
+++ /dev/null
@@ -1,176 +0,0 @@
-/*
- * Copyright (c) 2017 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.
- */
-#define KBUILD_MODNAME "foo"
-#include "vmlinux.h"
-#include <linux/version.h>
-#include <bpf/bpf_helpers.h>
-#include <bpf/bpf_tracing.h>
-#include <bpf/bpf_core_read.h>
-
-#define MAX_NR_PORTS 65536
-
-#define EINVAL 22
-#define ENOENT 2
-
-/* map #0 */
-struct inner_a {
-       __uint(type, BPF_MAP_TYPE_ARRAY);
-       __type(key, u32);
-       __type(value, int);
-       __uint(max_entries, MAX_NR_PORTS);
-} port_a SEC(".maps");
-
-/* map #1 */
-struct inner_h {
-       __uint(type, BPF_MAP_TYPE_HASH);
-       __type(key, u32);
-       __type(value, int);
-       __uint(max_entries, 1);
-} port_h SEC(".maps");
-
-/* map #2 */
-struct {
-       __uint(type, BPF_MAP_TYPE_HASH);
-       __type(key, u32);
-       __type(value, int);
-       __uint(max_entries, 1);
-} reg_result_h SEC(".maps");
-
-/* map #3 */
-struct {
-       __uint(type, BPF_MAP_TYPE_HASH);
-       __type(key, u32);
-       __type(value, int);
-       __uint(max_entries, 1);
-} inline_result_h SEC(".maps");
-
-/* map #4 */ /* Test case #0 */
-struct {
-       __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
-       __uint(max_entries, MAX_NR_PORTS);
-       __uint(key_size, sizeof(u32));
-       __array(values, struct inner_a); /* use inner_a as inner map */
-} a_of_port_a SEC(".maps");
-
-/* map #5 */ /* Test case #1 */
-struct {
-       __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
-       __uint(max_entries, 1);
-       __uint(key_size, sizeof(u32));
-       __array(values, struct inner_a); /* use inner_a as inner map */
-} h_of_port_a SEC(".maps");
-
-/* map #6 */ /* Test case #2 */
-struct {
-       __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
-       __uint(max_entries, 1);
-       __uint(key_size, sizeof(u32));
-       __array(values, struct inner_h); /* use inner_h as inner map */
-} h_of_port_h SEC(".maps");
-
-static __always_inline int do_reg_lookup(void *inner_map, u32 port)
-{
-       int *result;
-
-       result = bpf_map_lookup_elem(inner_map, &port);
-       return result ? *result : -ENOENT;
-}
-
-static __always_inline int do_inline_array_lookup(void *inner_map, u32 port)
-{
-       int *result;
-
-       if (inner_map != &port_a)
-               return -EINVAL;
-
-       result = bpf_map_lookup_elem(&port_a, &port);
-       return result ? *result : -ENOENT;
-}
-
-static __always_inline int do_inline_hash_lookup(void *inner_map, u32 port)
-{
-       int *result;
-
-       if (inner_map != &port_h)
-               return -EINVAL;
-
-       result = bpf_map_lookup_elem(&port_h, &port);
-       return result ? *result : -ENOENT;
-}
-
-SEC("kprobe/__sys_connect")
-int trace_sys_connect(struct pt_regs *ctx)
-{
-       struct sockaddr_in6 *in6;
-       u16 test_case, port, dst6[8];
-       int addrlen, ret, inline_ret, ret_key = 0;
-       u32 port_key;
-       void *outer_map, *inner_map;
-       bool inline_hash = false;
-
-       in6 = (struct sockaddr_in6 *)PT_REGS_PARM2_CORE(ctx);
-       addrlen = (int)PT_REGS_PARM3_CORE(ctx);
-
-       if (addrlen != sizeof(*in6))
-               return 0;
-
-       ret = bpf_probe_read_user(dst6, sizeof(dst6), &in6->sin6_addr);
-       if (ret) {
-               inline_ret = ret;
-               goto done;
-       }
-
-       if (dst6[0] != 0xdead || dst6[1] != 0xbeef)
-               return 0;
-
-       test_case = dst6[7];
-
-       ret = bpf_probe_read_user(&port, sizeof(port), &in6->sin6_port);
-       if (ret) {
-               inline_ret = ret;
-               goto done;
-       }
-
-       port_key = port;
-
-       ret = -ENOENT;
-       if (test_case == 0) {
-               outer_map = &a_of_port_a;
-       } else if (test_case == 1) {
-               outer_map = &h_of_port_a;
-       } else if (test_case == 2) {
-               outer_map = &h_of_port_h;
-       } else {
-               ret = __LINE__;
-               inline_ret = ret;
-               goto done;
-       }
-
-       inner_map = bpf_map_lookup_elem(outer_map, &port_key);
-       if (!inner_map) {
-               ret = __LINE__;
-               inline_ret = ret;
-               goto done;
-       }
-
-       ret = do_reg_lookup(inner_map, port_key);
-
-       if (test_case == 0 || test_case == 1)
-               inline_ret = do_inline_array_lookup(inner_map, port_key);
-       else
-               inline_ret = do_inline_hash_lookup(inner_map, port_key);
-
-done:
-       bpf_map_update_elem(&reg_result_h, &ret_key, &ret, BPF_ANY);
-       bpf_map_update_elem(&inline_result_h, &ret_key, &inline_ret, BPF_ANY);
-
-       return 0;
-}
-
-char _license[] SEC("license") = "GPL";
-u32 _version SEC("version") = LINUX_VERSION_CODE;
index 652ec720533de487fa4407db122a25fe83b95ad7..9e79df4071f58b873335e4480bce53381ffb26e6 100644 (file)
@@ -120,7 +120,7 @@ int main(int argc, char **argv)
        struct bpf_object *obj;
        char filename[256];
 
-       snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
+       snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]);
        obj = bpf_object__open_file(filename, NULL);
        if (libbpf_get_error(obj)) {
                fprintf(stderr, "ERROR: opening BPF object file failed\n");
diff --git a/samples/bpf/test_overhead_kprobe.bpf.c b/samples/bpf/test_overhead_kprobe.bpf.c
new file mode 100644 (file)
index 0000000..c352873
--- /dev/null
@@ -0,0 +1,47 @@
+/* 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 "vmlinux.h"
+#include <linux/version.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#define _(P)                                                                   \
+       ({                                                                     \
+               typeof(P) val = 0;                                             \
+               bpf_probe_read_kernel(&val, sizeof(val), &(P));                \
+               val;                                                           \
+       })
+
+SEC("kprobe/__set_task_comm")
+int prog(struct pt_regs *ctx)
+{
+       struct signal_struct *signal;
+       struct task_struct *tsk;
+       char oldcomm[TASK_COMM_LEN] = {};
+       char newcomm[TASK_COMM_LEN] = {};
+       u16 oom_score_adj;
+       u32 pid;
+
+       tsk = (void *)PT_REGS_PARM1(ctx);
+
+       pid = _(tsk->pid);
+       bpf_probe_read_kernel_str(oldcomm, sizeof(oldcomm), &tsk->comm);
+       bpf_probe_read_kernel_str(newcomm, sizeof(newcomm),
+                                 (void *)PT_REGS_PARM2(ctx));
+       signal = _(tsk->signal);
+       oom_score_adj = _(signal->oom_score_adj);
+       return 0;
+}
+
+SEC("kprobe/fib_table_lookup")
+int prog2(struct pt_regs *ctx)
+{
+       return 0;
+}
+
+char _license[] SEC("license") = "GPL";
+u32 _version SEC("version") = LINUX_VERSION_CODE;
diff --git a/samples/bpf/test_overhead_kprobe_kern.c b/samples/bpf/test_overhead_kprobe_kern.c
deleted file mode 100644 (file)
index c352873..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-/* 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 "vmlinux.h"
-#include <linux/version.h>
-#include <bpf/bpf_helpers.h>
-#include <bpf/bpf_tracing.h>
-
-#define _(P)                                                                   \
-       ({                                                                     \
-               typeof(P) val = 0;                                             \
-               bpf_probe_read_kernel(&val, sizeof(val), &(P));                \
-               val;                                                           \
-       })
-
-SEC("kprobe/__set_task_comm")
-int prog(struct pt_regs *ctx)
-{
-       struct signal_struct *signal;
-       struct task_struct *tsk;
-       char oldcomm[TASK_COMM_LEN] = {};
-       char newcomm[TASK_COMM_LEN] = {};
-       u16 oom_score_adj;
-       u32 pid;
-
-       tsk = (void *)PT_REGS_PARM1(ctx);
-
-       pid = _(tsk->pid);
-       bpf_probe_read_kernel_str(oldcomm, sizeof(oldcomm), &tsk->comm);
-       bpf_probe_read_kernel_str(newcomm, sizeof(newcomm),
-                                 (void *)PT_REGS_PARM2(ctx));
-       signal = _(tsk->signal);
-       oom_score_adj = _(signal->oom_score_adj);
-       return 0;
-}
-
-SEC("kprobe/fib_table_lookup")
-int prog2(struct pt_regs *ctx)
-{
-       return 0;
-}
-
-char _license[] SEC("license") = "GPL";
-u32 _version SEC("version") = LINUX_VERSION_CODE;
diff --git a/samples/bpf/test_overhead_raw_tp.bpf.c b/samples/bpf/test_overhead_raw_tp.bpf.c
new file mode 100644 (file)
index 0000000..6af39fe
--- /dev/null
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2018 Facebook */
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+SEC("raw_tracepoint/task_rename")
+int prog(struct bpf_raw_tracepoint_args *ctx)
+{
+       return 0;
+}
+
+SEC("raw_tracepoint/fib_table_lookup")
+int prog2(struct bpf_raw_tracepoint_args *ctx)
+{
+       return 0;
+}
+char _license[] SEC("license") = "GPL";
diff --git a/samples/bpf/test_overhead_raw_tp_kern.c b/samples/bpf/test_overhead_raw_tp_kern.c
deleted file mode 100644 (file)
index 6af39fe..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-/* Copyright (c) 2018 Facebook */
-#include "vmlinux.h"
-#include <bpf/bpf_helpers.h>
-
-SEC("raw_tracepoint/task_rename")
-int prog(struct bpf_raw_tracepoint_args *ctx)
-{
-       return 0;
-}
-
-SEC("raw_tracepoint/fib_table_lookup")
-int prog2(struct bpf_raw_tracepoint_args *ctx)
-{
-       return 0;
-}
-char _license[] SEC("license") = "GPL";
diff --git a/samples/bpf/test_overhead_tp.bpf.c b/samples/bpf/test_overhead_tp.bpf.c
new file mode 100644 (file)
index 0000000..67cab38
--- /dev/null
@@ -0,0 +1,48 @@
+/* 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 "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+/* from /sys/kernel/debug/tracing/events/task/task_rename/format */
+struct task_rename {
+       __u64 pad;
+       __u32 pid;
+       char oldcomm[TASK_COMM_LEN];
+       char newcomm[TASK_COMM_LEN];
+       __u16 oom_score_adj;
+};
+SEC("tracepoint/task/task_rename")
+int prog(struct task_rename *ctx)
+{
+       return 0;
+}
+
+/* from /sys/kernel/debug/tracing/events/fib/fib_table_lookup/format */
+struct fib_table_lookup {
+       __u64 pad;
+       __u32 tb_id;
+       int err;
+       int oif;
+       int iif;
+       __u8 proto;
+       __u8 tos;
+       __u8 scope;
+       __u8 flags;
+       __u8 src[4];
+       __u8 dst[4];
+       __u8 gw4[4];
+       __u8 gw6[16];
+       __u16 sport;
+       __u16 dport;
+       char name[16];
+};
+SEC("tracepoint/fib/fib_table_lookup")
+int prog2(struct fib_table_lookup *ctx)
+{
+       return 0;
+}
+char _license[] SEC("license") = "GPL";
diff --git a/samples/bpf/test_overhead_tp_kern.c b/samples/bpf/test_overhead_tp_kern.c
deleted file mode 100644 (file)
index 67cab38..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/* 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 "vmlinux.h"
-#include <bpf/bpf_helpers.h>
-
-/* from /sys/kernel/debug/tracing/events/task/task_rename/format */
-struct task_rename {
-       __u64 pad;
-       __u32 pid;
-       char oldcomm[TASK_COMM_LEN];
-       char newcomm[TASK_COMM_LEN];
-       __u16 oom_score_adj;
-};
-SEC("tracepoint/task/task_rename")
-int prog(struct task_rename *ctx)
-{
-       return 0;
-}
-
-/* from /sys/kernel/debug/tracing/events/fib/fib_table_lookup/format */
-struct fib_table_lookup {
-       __u64 pad;
-       __u32 tb_id;
-       int err;
-       int oif;
-       int iif;
-       __u8 proto;
-       __u8 tos;
-       __u8 scope;
-       __u8 flags;
-       __u8 src[4];
-       __u8 dst[4];
-       __u8 gw4[4];
-       __u8 gw6[16];
-       __u16 sport;
-       __u16 dport;
-       char name[16];
-};
-SEC("tracepoint/fib/fib_table_lookup")
-int prog2(struct fib_table_lookup *ctx)
-{
-       return 0;
-}
-char _license[] SEC("license") = "GPL";
index ce28d30f852ebb8f9e615afbe3b1e5648256cfc1..dbd86f7b1473ac97d28dd8a74ffb36ec4622fdaa 100644 (file)
@@ -189,7 +189,7 @@ int main(int argc, char **argv)
 
        if (test_flags & 0xC) {
                snprintf(filename, sizeof(filename),
-                        "%s_kprobe_kern.o", argv[0]);
+                        "%s_kprobe.bpf.o", argv[0]);
 
                printf("w/KPROBE\n");
                err = load_progs(filename);
@@ -201,7 +201,7 @@ int main(int argc, char **argv)
 
        if (test_flags & 0x30) {
                snprintf(filename, sizeof(filename),
-                        "%s_tp_kern.o", argv[0]);
+                        "%s_tp.bpf.o", argv[0]);
                printf("w/TRACEPOINT\n");
                err = load_progs(filename);
                if (!err)
@@ -212,7 +212,7 @@ int main(int argc, char **argv)
 
        if (test_flags & 0xC0) {
                snprintf(filename, sizeof(filename),
-                        "%s_raw_tp_kern.o", argv[0]);
+                        "%s_raw_tp.bpf.o", argv[0]);
                printf("w/RAW_TRACEPOINT\n");
                err = load_progs(filename);
                if (!err)