summaryrefslogtreecommitdiffstats
path: root/samples
diff options
context:
space:
mode:
authorJakub Kicinski <jakub.kicinski@netronome.com>2019-08-13 19:24:57 -0400
committerJakub Kicinski <jakub.kicinski@netronome.com>2019-08-13 19:24:57 -0400
commit708852dcac84d2b923f2e8c1327f6006f612416a (patch)
treed140423180b83750ad1eb0095cb80ec5342716d4 /samples
parenta9a96760165d8781570f2dadeed8e38484b13499 (diff)
parent72ef80b5ee131e96172f19e74b4f98fa3404efe8 (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Daniel Borkmann says: ==================== The following pull-request contains BPF updates for your *net-next* tree. There is a small merge conflict in libbpf (Cc Andrii so he's in the loop as well): for (i = 1; i <= btf__get_nr_types(btf); i++) { t = (struct btf_type *)btf__type_by_id(btf, i); if (!has_datasec && btf_is_var(t)) { /* replace VAR with INT */ t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0); <<<<<<< HEAD /* * using size = 1 is the safest choice, 4 will be too * big and cause kernel BTF validation failure if * original variable took less than 4 bytes */ t->size = 1; *(int *)(t+1) = BTF_INT_ENC(0, 0, 8); } else if (!has_datasec && kind == BTF_KIND_DATASEC) { ======= t->size = sizeof(int); *(int *)(t + 1) = BTF_INT_ENC(0, 0, 32); } else if (!has_datasec && btf_is_datasec(t)) { >>>>>>> 72ef80b5ee131e96172f19e74b4f98fa3404efe8 /* replace DATASEC with STRUCT */ Conflict is between the two commits 1d4126c4e119 ("libbpf: sanitize VAR to conservative 1-byte INT") and b03bc6853c0e ("libbpf: convert libbpf code to use new btf helpers"), so we need to pick the sanitation fixup as well as use the new btf_is_datasec() helper and the whitespace cleanup. Looks like the following: [...] if (!has_datasec && btf_is_var(t)) { /* replace VAR with INT */ t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0); /* * using size = 1 is the safest choice, 4 will be too * big and cause kernel BTF validation failure if * original variable took less than 4 bytes */ t->size = 1; *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8); } else if (!has_datasec && btf_is_datasec(t)) { /* replace DATASEC with STRUCT */ [...] The main changes are: 1) Addition of core parts of compile once - run everywhere (co-re) effort, that is, relocation of fields offsets in libbpf as well as exposure of kernel's own BTF via sysfs and loading through libbpf, from Andrii. More info on co-re: http://vger.kernel.org/bpfconf2019.html#session-2 and http://vger.kernel.org/lpc-bpf2018.html#session-2 2) Enable passing input flags to the BPF flow dissector to customize parsing and allowing it to stop early similar to the C based one, from Stanislav. 3) Add a BPF helper function that allows generating SYN cookies from XDP and tc BPF, from Petar. 4) Add devmap hash-based map type for more flexibility in device lookup for redirects, from Toke. 5) Improvements to XDP forwarding sample code now utilizing recently enabled devmap lookups, from Jesper. 6) Add support for reporting the effective cgroup progs in bpftool, from Jakub and Takshak. 7) Fix reading kernel config from bpftool via /proc/config.gz, from Peter. 8) Fix AF_XDP umem pages mapping for 32 bit architectures, from Ivan. 9) Follow-up to add two more BPF loop tests for the selftest suite, from Alexei. 10) Add perf event output helper also for other skb-based program types, from Allan. 11) Fix a co-re related compilation error in selftests, from Yonghong. ==================== Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Diffstat (limited to 'samples')
-rw-r--r--samples/bpf/trace_output_user.c43
-rw-r--r--samples/bpf/xdp_fwd_kern.c39
-rw-r--r--samples/bpf/xdp_fwd_user.c35
-rw-r--r--samples/bpf/xdp_sample_pkts_user.c61
4 files changed, 84 insertions, 94 deletions
diff --git a/samples/bpf/trace_output_user.c b/samples/bpf/trace_output_user.c
index 2dd1d39b152a..8ee47699a870 100644
--- a/samples/bpf/trace_output_user.c
+++ b/samples/bpf/trace_output_user.c
@@ -18,9 +18,6 @@
18#include <libbpf.h> 18#include <libbpf.h>
19#include "bpf_load.h" 19#include "bpf_load.h"
20#include "perf-sys.h" 20#include "perf-sys.h"
21#include "trace_helpers.h"
22
23static int pmu_fd;
24 21
25static __u64 time_get_ns(void) 22static __u64 time_get_ns(void)
26{ 23{
@@ -31,12 +28,12 @@ static __u64 time_get_ns(void)
31} 28}
32 29
33static __u64 start_time; 30static __u64 start_time;
31static __u64 cnt;
34 32
35#define MAX_CNT 100000ll 33#define MAX_CNT 100000ll
36 34
37static int print_bpf_output(void *data, int size) 35static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size)
38{ 36{
39 static __u64 cnt;
40 struct { 37 struct {
41 __u64 pid; 38 __u64 pid;
42 __u64 cookie; 39 __u64 cookie;
@@ -45,7 +42,7 @@ static int print_bpf_output(void *data, int size)
45 if (e->cookie != 0x12345678) { 42 if (e->cookie != 0x12345678) {
46 printf("BUG pid %llx cookie %llx sized %d\n", 43 printf("BUG pid %llx cookie %llx sized %d\n",
47 e->pid, e->cookie, size); 44 e->pid, e->cookie, size);
48 return LIBBPF_PERF_EVENT_ERROR; 45 return;
49 } 46 }
50 47
51 cnt++; 48 cnt++;
@@ -53,30 +50,14 @@ static int print_bpf_output(void *data, int size)
53 if (cnt == MAX_CNT) { 50 if (cnt == MAX_CNT) {
54 printf("recv %lld events per sec\n", 51 printf("recv %lld events per sec\n",
55 MAX_CNT * 1000000000ll / (time_get_ns() - start_time)); 52 MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
56 return LIBBPF_PERF_EVENT_DONE; 53 return;
57 } 54 }
58
59 return LIBBPF_PERF_EVENT_CONT;
60}
61
62static void test_bpf_perf_event(void)
63{
64 struct perf_event_attr attr = {
65 .sample_type = PERF_SAMPLE_RAW,
66 .type = PERF_TYPE_SOFTWARE,
67 .config = PERF_COUNT_SW_BPF_OUTPUT,
68 };
69 int key = 0;
70
71 pmu_fd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
72
73 assert(pmu_fd >= 0);
74 assert(bpf_map_update_elem(map_fd[0], &key, &pmu_fd, BPF_ANY) == 0);
75 ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0);
76} 55}
77 56
78int main(int argc, char **argv) 57int main(int argc, char **argv)
79{ 58{
59 struct perf_buffer_opts pb_opts = {};
60 struct perf_buffer *pb;
80 char filename[256]; 61 char filename[256];
81 FILE *f; 62 FILE *f;
82 int ret; 63 int ret;
@@ -88,16 +69,20 @@ int main(int argc, char **argv)
88 return 1; 69 return 1;
89 } 70 }
90 71
91 test_bpf_perf_event(); 72 pb_opts.sample_cb = print_bpf_output;
92 73 pb = perf_buffer__new(map_fd[0], 8, &pb_opts);
93 if (perf_event_mmap(pmu_fd) < 0) 74 ret = libbpf_get_error(pb);
75 if (ret) {
76 printf("failed to setup perf_buffer: %d\n", ret);
94 return 1; 77 return 1;
78 }
95 79
96 f = popen("taskset 1 dd if=/dev/zero of=/dev/null", "r"); 80 f = popen("taskset 1 dd if=/dev/zero of=/dev/null", "r");
97 (void) f; 81 (void) f;
98 82
99 start_time = time_get_ns(); 83 start_time = time_get_ns();
100 ret = perf_event_poller(pmu_fd, print_bpf_output); 84 while ((ret = perf_buffer__poll(pb, 1000)) >= 0 && cnt < MAX_CNT) {
85 }
101 kill(0, SIGINT); 86 kill(0, SIGINT);
102 return ret; 87 return ret;
103} 88}
diff --git a/samples/bpf/xdp_fwd_kern.c b/samples/bpf/xdp_fwd_kern.c
index a7e94e7ff87d..701a30f258b1 100644
--- a/samples/bpf/xdp_fwd_kern.c
+++ b/samples/bpf/xdp_fwd_kern.c
@@ -23,7 +23,8 @@
23 23
24#define IPV6_FLOWINFO_MASK cpu_to_be32(0x0FFFFFFF) 24#define IPV6_FLOWINFO_MASK cpu_to_be32(0x0FFFFFFF)
25 25
26struct bpf_map_def SEC("maps") tx_port = { 26/* For TX-traffic redirect requires net_device ifindex to be in this devmap */
27struct bpf_map_def SEC("maps") xdp_tx_ports = {
27 .type = BPF_MAP_TYPE_DEVMAP, 28 .type = BPF_MAP_TYPE_DEVMAP,
28 .key_size = sizeof(int), 29 .key_size = sizeof(int),
29 .value_size = sizeof(int), 30 .value_size = sizeof(int),
@@ -102,14 +103,34 @@ static __always_inline int xdp_fwd_flags(struct xdp_md *ctx, u32 flags)
102 fib_params.ifindex = ctx->ingress_ifindex; 103 fib_params.ifindex = ctx->ingress_ifindex;
103 104
104 rc = bpf_fib_lookup(ctx, &fib_params, sizeof(fib_params), flags); 105 rc = bpf_fib_lookup(ctx, &fib_params, sizeof(fib_params), flags);
105 106 /*
106 /* verify egress index has xdp support 107 * Some rc (return codes) from bpf_fib_lookup() are important,
107 * TO-DO bpf_map_lookup_elem(&tx_port, &key) fails with 108 * to understand how this XDP-prog interacts with network stack.
108 * cannot pass map_type 14 into func bpf_map_lookup_elem#1: 109 *
109 * NOTE: without verification that egress index supports XDP 110 * BPF_FIB_LKUP_RET_NO_NEIGH:
110 * forwarding packets are dropped. 111 * Even if route lookup was a success, then the MAC-addresses are also
112 * needed. This is obtained from arp/neighbour table, but if table is
113 * (still) empty then BPF_FIB_LKUP_RET_NO_NEIGH is returned. To avoid
114 * doing ARP lookup directly from XDP, then send packet to normal
115 * network stack via XDP_PASS and expect it will do ARP resolution.
116 *
117 * BPF_FIB_LKUP_RET_FWD_DISABLED:
118 * The bpf_fib_lookup respect sysctl net.ipv{4,6}.conf.all.forwarding
119 * setting, and will return BPF_FIB_LKUP_RET_FWD_DISABLED if not
120 * enabled this on ingress device.
111 */ 121 */
112 if (rc == 0) { 122 if (rc == BPF_FIB_LKUP_RET_SUCCESS) {
123 /* Verify egress index has been configured as TX-port.
124 * (Note: User can still have inserted an egress ifindex that
125 * doesn't support XDP xmit, which will result in packet drops).
126 *
127 * Note: lookup in devmap supported since 0cdbb4b09a0.
128 * If not supported will fail with:
129 * cannot pass map_type 14 into func bpf_map_lookup_elem#1:
130 */
131 if (!bpf_map_lookup_elem(&xdp_tx_ports, &fib_params.ifindex))
132 return XDP_PASS;
133
113 if (h_proto == htons(ETH_P_IP)) 134 if (h_proto == htons(ETH_P_IP))
114 ip_decrease_ttl(iph); 135 ip_decrease_ttl(iph);
115 else if (h_proto == htons(ETH_P_IPV6)) 136 else if (h_proto == htons(ETH_P_IPV6))
@@ -117,7 +138,7 @@ static __always_inline int xdp_fwd_flags(struct xdp_md *ctx, u32 flags)
117 138
118 memcpy(eth->h_dest, fib_params.dmac, ETH_ALEN); 139 memcpy(eth->h_dest, fib_params.dmac, ETH_ALEN);
119 memcpy(eth->h_source, fib_params.smac, ETH_ALEN); 140 memcpy(eth->h_source, fib_params.smac, ETH_ALEN);
120 return bpf_redirect_map(&tx_port, fib_params.ifindex, 0); 141 return bpf_redirect_map(&xdp_tx_ports, fib_params.ifindex, 0);
121 } 142 }
122 143
123 return XDP_PASS; 144 return XDP_PASS;
diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c
index 5b46ee12c696..97ff1dad7669 100644
--- a/samples/bpf/xdp_fwd_user.c
+++ b/samples/bpf/xdp_fwd_user.c
@@ -27,14 +27,20 @@
27#include "libbpf.h" 27#include "libbpf.h"
28#include <bpf/bpf.h> 28#include <bpf/bpf.h>
29 29
30 30static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
31static int do_attach(int idx, int fd, const char *name)
32{ 31{
33 int err; 32 int err;
34 33
35 err = bpf_set_link_xdp_fd(idx, fd, 0); 34 err = bpf_set_link_xdp_fd(idx, prog_fd, 0);
36 if (err < 0) 35 if (err < 0) {
37 printf("ERROR: failed to attach program to %s\n", name); 36 printf("ERROR: failed to attach program to %s\n", name);
37 return err;
38 }
39
40 /* Adding ifindex as a possible egress TX port */
41 err = bpf_map_update_elem(map_fd, &idx, &idx, 0);
42 if (err)
43 printf("ERROR: failed using device %s as TX-port\n", name);
38 44
39 return err; 45 return err;
40} 46}
@@ -47,6 +53,9 @@ static int do_detach(int idx, const char *name)
47 if (err < 0) 53 if (err < 0)
48 printf("ERROR: failed to detach program from %s\n", name); 54 printf("ERROR: failed to detach program from %s\n", name);
49 55
56 /* TODO: Remember to cleanup map, when adding use of shared map
57 * bpf_map_delete_elem((map_fd, &idx);
58 */
50 return err; 59 return err;
51} 60}
52 61
@@ -67,10 +76,10 @@ int main(int argc, char **argv)
67 }; 76 };
68 const char *prog_name = "xdp_fwd"; 77 const char *prog_name = "xdp_fwd";
69 struct bpf_program *prog; 78 struct bpf_program *prog;
79 int prog_fd, map_fd = -1;
70 char filename[PATH_MAX]; 80 char filename[PATH_MAX];
71 struct bpf_object *obj; 81 struct bpf_object *obj;
72 int opt, i, idx, err; 82 int opt, i, idx, err;
73 int prog_fd, map_fd;
74 int attach = 1; 83 int attach = 1;
75 int ret = 0; 84 int ret = 0;
76 85
@@ -103,8 +112,14 @@ int main(int argc, char **argv)
103 return 1; 112 return 1;
104 } 113 }
105 114
106 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd)) 115 err = bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd);
116 if (err) {
117 printf("Does kernel support devmap lookup?\n");
118 /* If not, the error message will be:
119 * "cannot pass map_type 14 into func bpf_map_lookup_elem#1"
120 */
107 return 1; 121 return 1;
122 }
108 123
109 prog = bpf_object__find_program_by_title(obj, prog_name); 124 prog = bpf_object__find_program_by_title(obj, prog_name);
110 prog_fd = bpf_program__fd(prog); 125 prog_fd = bpf_program__fd(prog);
@@ -113,16 +128,12 @@ int main(int argc, char **argv)
113 return 1; 128 return 1;
114 } 129 }
115 map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj, 130 map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
116 "tx_port")); 131 "xdp_tx_ports"));
117 if (map_fd < 0) { 132 if (map_fd < 0) {
118 printf("map not found: %s\n", strerror(map_fd)); 133 printf("map not found: %s\n", strerror(map_fd));
119 return 1; 134 return 1;
120 } 135 }
121 } 136 }
122 if (attach) {
123 for (i = 1; i < 64; ++i)
124 bpf_map_update_elem(map_fd, &i, &i, 0);
125 }
126 137
127 for (i = optind; i < argc; ++i) { 138 for (i = optind; i < argc; ++i) {
128 idx = if_nametoindex(argv[i]); 139 idx = if_nametoindex(argv[i]);
@@ -138,7 +149,7 @@ int main(int argc, char **argv)
138 if (err) 149 if (err)
139 ret = err; 150 ret = err;
140 } else { 151 } else {
141 err = do_attach(idx, prog_fd, argv[i]); 152 err = do_attach(idx, prog_fd, map_fd, argv[i]);
142 if (err) 153 if (err)
143 ret = err; 154 ret = err;
144 } 155 }
diff --git a/samples/bpf/xdp_sample_pkts_user.c b/samples/bpf/xdp_sample_pkts_user.c
index dc66345a929a..3002714e3cd5 100644
--- a/samples/bpf/xdp_sample_pkts_user.c
+++ b/samples/bpf/xdp_sample_pkts_user.c
@@ -17,14 +17,13 @@
17#include <linux/if_link.h> 17#include <linux/if_link.h>
18 18
19#include "perf-sys.h" 19#include "perf-sys.h"
20#include "trace_helpers.h"
21 20
22#define MAX_CPUS 128 21#define MAX_CPUS 128
23static int pmu_fds[MAX_CPUS], if_idx; 22static int if_idx;
24static struct perf_event_mmap_page *headers[MAX_CPUS];
25static char *if_name; 23static char *if_name;
26static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; 24static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
27static __u32 prog_id; 25static __u32 prog_id;
26static struct perf_buffer *pb = NULL;
28 27
29static int do_attach(int idx, int fd, const char *name) 28static int do_attach(int idx, int fd, const char *name)
30{ 29{
@@ -73,7 +72,7 @@ static int do_detach(int idx, const char *name)
73 72
74#define SAMPLE_SIZE 64 73#define SAMPLE_SIZE 64
75 74
76static int print_bpf_output(void *data, int size) 75static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size)
77{ 76{
78 struct { 77 struct {
79 __u16 cookie; 78 __u16 cookie;
@@ -83,45 +82,20 @@ static int print_bpf_output(void *data, int size)
83 int i; 82 int i;
84 83
85 if (e->cookie != 0xdead) { 84 if (e->cookie != 0xdead) {
86 printf("BUG cookie %x sized %d\n", 85 printf("BUG cookie %x sized %d\n", e->cookie, size);
87 e->cookie, size); 86 return;
88 return LIBBPF_PERF_EVENT_ERROR;
89 } 87 }
90 88
91 printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len); 89 printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len);
92 for (i = 0; i < 14 && i < e->pkt_len; i++) 90 for (i = 0; i < 14 && i < e->pkt_len; i++)
93 printf("%02x ", e->pkt_data[i]); 91 printf("%02x ", e->pkt_data[i]);
94 printf("\n"); 92 printf("\n");
95
96 return LIBBPF_PERF_EVENT_CONT;
97}
98
99static void test_bpf_perf_event(int map_fd, int num)
100{
101 struct perf_event_attr attr = {
102 .sample_type = PERF_SAMPLE_RAW,
103 .type = PERF_TYPE_SOFTWARE,
104 .config = PERF_COUNT_SW_BPF_OUTPUT,
105 .wakeup_events = 1, /* get an fd notification for every event */
106 };
107 int i;
108
109 for (i = 0; i < num; i++) {
110 int key = i;
111
112 pmu_fds[i] = sys_perf_event_open(&attr, -1/*pid*/, i/*cpu*/,
113 -1/*group_fd*/, 0);
114
115 assert(pmu_fds[i] >= 0);
116 assert(bpf_map_update_elem(map_fd, &key,
117 &pmu_fds[i], BPF_ANY) == 0);
118 ioctl(pmu_fds[i], PERF_EVENT_IOC_ENABLE, 0);
119 }
120} 93}
121 94
122static void sig_handler(int signo) 95static void sig_handler(int signo)
123{ 96{
124 do_detach(if_idx, if_name); 97 do_detach(if_idx, if_name);
98 perf_buffer__free(pb);
125 exit(0); 99 exit(0);
126} 100}
127 101
@@ -140,13 +114,13 @@ int main(int argc, char **argv)
140 struct bpf_prog_load_attr prog_load_attr = { 114 struct bpf_prog_load_attr prog_load_attr = {
141 .prog_type = BPF_PROG_TYPE_XDP, 115 .prog_type = BPF_PROG_TYPE_XDP,
142 }; 116 };
117 struct perf_buffer_opts pb_opts = {};
143 const char *optstr = "F"; 118 const char *optstr = "F";
144 int prog_fd, map_fd, opt; 119 int prog_fd, map_fd, opt;
145 struct bpf_object *obj; 120 struct bpf_object *obj;
146 struct bpf_map *map; 121 struct bpf_map *map;
147 char filename[256]; 122 char filename[256];
148 int ret, err, i; 123 int ret, err;
149 int numcpus;
150 124
151 while ((opt = getopt(argc, argv, optstr)) != -1) { 125 while ((opt = getopt(argc, argv, optstr)) != -1) {
152 switch (opt) { 126 switch (opt) {
@@ -169,10 +143,6 @@ int main(int argc, char **argv)
169 return 1; 143 return 1;
170 } 144 }
171 145
172 numcpus = get_nprocs();
173 if (numcpus > MAX_CPUS)
174 numcpus = MAX_CPUS;
175
176 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); 146 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
177 prog_load_attr.file = filename; 147 prog_load_attr.file = filename;
178 148
@@ -211,14 +181,17 @@ int main(int argc, char **argv)
211 return 1; 181 return 1;
212 } 182 }
213 183
214 test_bpf_perf_event(map_fd, numcpus); 184 pb_opts.sample_cb = print_bpf_output;
185 pb = perf_buffer__new(map_fd, 8, &pb_opts);
186 err = libbpf_get_error(pb);
187 if (err) {
188 perror("perf_buffer setup failed");
189 return 1;
190 }
215 191
216 for (i = 0; i < numcpus; i++) 192 while ((ret = perf_buffer__poll(pb, 1000)) >= 0) {
217 if (perf_event_mmap_header(pmu_fds[i], &headers[i]) < 0) 193 }
218 return 1;
219 194
220 ret = perf_event_poller_multi(pmu_fds, headers, numcpus,
221 print_bpf_output);
222 kill(0, SIGINT); 195 kill(0, SIGINT);
223 return ret; 196 return ret;
224} 197}