diff options
| author | David S. Miller <davem@davemloft.net> | 2018-05-24 22:20:51 -0400 |
|---|---|---|
| committer | David S. Miller <davem@davemloft.net> | 2018-05-24 22:20:51 -0400 |
| commit | 90fed9c94625718a3a10db7d1e8e4efe093bbf5f (patch) | |
| tree | 09b3bc9ea679316372b139338179a230105306dc /samples | |
| parent | 49a473f5b5f54f33e0bd8618158d33f83153c921 (diff) | |
| parent | 10f678683e4026e43524b0492068a371d00fdeed (diff) | |
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Alexei Starovoitov says:
====================
pull-request: bpf-next 2018-05-24
The following pull-request contains BPF updates for your *net-next* tree.
The main changes are:
1) Björn Töpel cleans up AF_XDP (removes rebind, explicit cache alignment from uapi, etc).
2) David Ahern adds mtu checks to bpf_ipv{4,6}_fib_lookup() helpers.
3) Jesper Dangaard Brouer adds bulking support to ndo_xdp_xmit.
4) Jiong Wang adds support for indirect and arithmetic shifts to NFP
5) Martin KaFai Lau cleans up BTF uapi and makes the btf_header extensible.
6) Mathieu Xhonneux adds an End.BPF action to seg6local with BPF helpers allowing
to edit/grow/shrink a SRH and apply on a packet generic SRv6 actions.
7) Sandipan Das adds support for bpf2bpf function calls in ppc64 JIT.
8) Yonghong Song adds BPF_TASK_FD_QUERY command for introspection of tracing events.
9) other misc fixes from Gustavo A. R. Silva, Sirio Balmelli, John Fastabend, and Magnus Karlsson
====================
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'samples')
| -rw-r--r-- | samples/bpf/Makefile | 4 | ||||
| -rw-r--r-- | samples/bpf/task_fd_query_kern.c | 19 | ||||
| -rw-r--r-- | samples/bpf/task_fd_query_user.c | 382 | ||||
| -rw-r--r-- | samples/bpf/xdp_monitor_kern.c | 49 | ||||
| -rw-r--r-- | samples/bpf/xdp_monitor_user.c | 69 | ||||
| -rw-r--r-- | samples/bpf/xdpsock_user.c | 135 |
6 files changed, 599 insertions, 59 deletions
diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 62a99ab680e3..1303af10e54d 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile | |||
| @@ -51,6 +51,7 @@ hostprogs-y += cpustat | |||
| 51 | hostprogs-y += xdp_adjust_tail | 51 | hostprogs-y += xdp_adjust_tail |
| 52 | hostprogs-y += xdpsock | 52 | hostprogs-y += xdpsock |
| 53 | hostprogs-y += xdp_fwd | 53 | hostprogs-y += xdp_fwd |
| 54 | hostprogs-y += task_fd_query | ||
| 54 | 55 | ||
| 55 | # Libbpf dependencies | 56 | # Libbpf dependencies |
| 56 | LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a | 57 | LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a |
| @@ -105,6 +106,7 @@ cpustat-objs := bpf_load.o cpustat_user.o | |||
| 105 | xdp_adjust_tail-objs := xdp_adjust_tail_user.o | 106 | xdp_adjust_tail-objs := xdp_adjust_tail_user.o |
| 106 | xdpsock-objs := bpf_load.o xdpsock_user.o | 107 | xdpsock-objs := bpf_load.o xdpsock_user.o |
| 107 | xdp_fwd-objs := bpf_load.o xdp_fwd_user.o | 108 | xdp_fwd-objs := bpf_load.o xdp_fwd_user.o |
| 109 | task_fd_query-objs := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS) | ||
| 108 | 110 | ||
| 109 | # Tell kbuild to always build the programs | 111 | # Tell kbuild to always build the programs |
| 110 | always := $(hostprogs-y) | 112 | always := $(hostprogs-y) |
| @@ -160,6 +162,7 @@ always += cpustat_kern.o | |||
| 160 | always += xdp_adjust_tail_kern.o | 162 | always += xdp_adjust_tail_kern.o |
| 161 | always += xdpsock_kern.o | 163 | always += xdpsock_kern.o |
| 162 | always += xdp_fwd_kern.o | 164 | always += xdp_fwd_kern.o |
| 165 | always += task_fd_query_kern.o | ||
| 163 | 166 | ||
| 164 | HOSTCFLAGS += -I$(objtree)/usr/include | 167 | HOSTCFLAGS += -I$(objtree)/usr/include |
| 165 | HOSTCFLAGS += -I$(srctree)/tools/lib/ | 168 | HOSTCFLAGS += -I$(srctree)/tools/lib/ |
| @@ -175,6 +178,7 @@ HOSTCFLAGS_offwaketime_user.o += -I$(srctree)/tools/lib/bpf/ | |||
| 175 | HOSTCFLAGS_spintest_user.o += -I$(srctree)/tools/lib/bpf/ | 178 | HOSTCFLAGS_spintest_user.o += -I$(srctree)/tools/lib/bpf/ |
| 176 | HOSTCFLAGS_trace_event_user.o += -I$(srctree)/tools/lib/bpf/ | 179 | HOSTCFLAGS_trace_event_user.o += -I$(srctree)/tools/lib/bpf/ |
| 177 | HOSTCFLAGS_sampleip_user.o += -I$(srctree)/tools/lib/bpf/ | 180 | HOSTCFLAGS_sampleip_user.o += -I$(srctree)/tools/lib/bpf/ |
| 181 | HOSTCFLAGS_task_fd_query_user.o += -I$(srctree)/tools/lib/bpf/ | ||
| 178 | 182 | ||
| 179 | HOST_LOADLIBES += $(LIBBPF) -lelf | 183 | HOST_LOADLIBES += $(LIBBPF) -lelf |
| 180 | HOSTLOADLIBES_tracex4 += -lrt | 184 | HOSTLOADLIBES_tracex4 += -lrt |
diff --git a/samples/bpf/task_fd_query_kern.c b/samples/bpf/task_fd_query_kern.c new file mode 100644 index 000000000000..f4b0a9ea674d --- /dev/null +++ b/samples/bpf/task_fd_query_kern.c | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0 | ||
| 2 | #include <linux/version.h> | ||
| 3 | #include <linux/ptrace.h> | ||
| 4 | #include <uapi/linux/bpf.h> | ||
| 5 | #include "bpf_helpers.h" | ||
| 6 | |||
| 7 | SEC("kprobe/blk_start_request") | ||
| 8 | int bpf_prog1(struct pt_regs *ctx) | ||
| 9 | { | ||
| 10 | return 0; | ||
| 11 | } | ||
| 12 | |||
| 13 | SEC("kretprobe/blk_account_io_completion") | ||
| 14 | int bpf_prog2(struct pt_regs *ctx) | ||
| 15 | { | ||
| 16 | return 0; | ||
| 17 | } | ||
| 18 | char _license[] SEC("license") = "GPL"; | ||
| 19 | u32 _version SEC("version") = LINUX_VERSION_CODE; | ||
diff --git a/samples/bpf/task_fd_query_user.c b/samples/bpf/task_fd_query_user.c new file mode 100644 index 000000000000..8381d792f138 --- /dev/null +++ b/samples/bpf/task_fd_query_user.c | |||
| @@ -0,0 +1,382 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0 | ||
| 2 | |||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <signal.h> | ||
| 6 | #include <unistd.h> | ||
| 7 | #include <stdbool.h> | ||
| 8 | #include <string.h> | ||
| 9 | #include <stdint.h> | ||
| 10 | #include <fcntl.h> | ||
| 11 | #include <linux/bpf.h> | ||
| 12 | #include <sys/ioctl.h> | ||
| 13 | #include <sys/resource.h> | ||
| 14 | #include <sys/types.h> | ||
| 15 | #include <sys/stat.h> | ||
| 16 | |||
| 17 | #include "libbpf.h" | ||
| 18 | #include "bpf_load.h" | ||
| 19 | #include "bpf_util.h" | ||
| 20 | #include "perf-sys.h" | ||
| 21 | #include "trace_helpers.h" | ||
| 22 | |||
| 23 | #define CHECK_PERROR_RET(condition) ({ \ | ||
| 24 | int __ret = !!(condition); \ | ||
| 25 | if (__ret) { \ | ||
| 26 | printf("FAIL: %s:\n", __func__); \ | ||
| 27 | perror(" "); \ | ||
| 28 | return -1; \ | ||
| 29 | } \ | ||
| 30 | }) | ||
| 31 | |||
| 32 | #define CHECK_AND_RET(condition) ({ \ | ||
| 33 | int __ret = !!(condition); \ | ||
| 34 | if (__ret) \ | ||
| 35 | return -1; \ | ||
| 36 | }) | ||
| 37 | |||
| 38 | static __u64 ptr_to_u64(void *ptr) | ||
| 39 | { | ||
| 40 | return (__u64) (unsigned long) ptr; | ||
| 41 | } | ||
| 42 | |||
| 43 | #define PMU_TYPE_FILE "/sys/bus/event_source/devices/%s/type" | ||
| 44 | static int bpf_find_probe_type(const char *event_type) | ||
| 45 | { | ||
| 46 | char buf[256]; | ||
| 47 | int fd, ret; | ||
| 48 | |||
| 49 | ret = snprintf(buf, sizeof(buf), PMU_TYPE_FILE, event_type); | ||
| 50 | CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf)); | ||
| 51 | |||
| 52 | fd = open(buf, O_RDONLY); | ||
| 53 | CHECK_PERROR_RET(fd < 0); | ||
| 54 | |||
| 55 | ret = read(fd, buf, sizeof(buf)); | ||
| 56 | close(fd); | ||
| 57 | CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf)); | ||
| 58 | |||
| 59 | errno = 0; | ||
| 60 | ret = (int)strtol(buf, NULL, 10); | ||
| 61 | CHECK_PERROR_RET(errno); | ||
| 62 | return ret; | ||
| 63 | } | ||
| 64 | |||
| 65 | #define PMU_RETPROBE_FILE "/sys/bus/event_source/devices/%s/format/retprobe" | ||
| 66 | static int bpf_get_retprobe_bit(const char *event_type) | ||
| 67 | { | ||
| 68 | char buf[256]; | ||
| 69 | int fd, ret; | ||
| 70 | |||
| 71 | ret = snprintf(buf, sizeof(buf), PMU_RETPROBE_FILE, event_type); | ||
| 72 | CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf)); | ||
| 73 | |||
| 74 | fd = open(buf, O_RDONLY); | ||
| 75 | CHECK_PERROR_RET(fd < 0); | ||
| 76 | |||
| 77 | ret = read(fd, buf, sizeof(buf)); | ||
| 78 | close(fd); | ||
| 79 | CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf)); | ||
| 80 | CHECK_PERROR_RET(strlen(buf) < strlen("config:")); | ||
| 81 | |||
| 82 | errno = 0; | ||
| 83 | ret = (int)strtol(buf + strlen("config:"), NULL, 10); | ||
| 84 | CHECK_PERROR_RET(errno); | ||
| 85 | return ret; | ||
| 86 | } | ||
| 87 | |||
| 88 | static int test_debug_fs_kprobe(int prog_fd_idx, const char *fn_name, | ||
| 89 | __u32 expected_fd_type) | ||
| 90 | { | ||
| 91 | __u64 probe_offset, probe_addr; | ||
| 92 | __u32 len, prog_id, fd_type; | ||
| 93 | char buf[256]; | ||
| 94 | int err; | ||
| 95 | |||
| 96 | len = sizeof(buf); | ||
| 97 | err = bpf_task_fd_query(getpid(), event_fd[prog_fd_idx], 0, buf, &len, | ||
| 98 | &prog_id, &fd_type, &probe_offset, | ||
| 99 | &probe_addr); | ||
| 100 | if (err < 0) { | ||
| 101 | printf("FAIL: %s, for event_fd idx %d, fn_name %s\n", | ||
| 102 | __func__, prog_fd_idx, fn_name); | ||
| 103 | perror(" :"); | ||
| 104 | return -1; | ||
| 105 | } | ||
| 106 | if (strcmp(buf, fn_name) != 0 || | ||
| 107 | fd_type != expected_fd_type || | ||
| 108 | probe_offset != 0x0 || probe_addr != 0x0) { | ||
| 109 | printf("FAIL: bpf_trace_event_query(event_fd[%d]):\n", | ||
| 110 | prog_fd_idx); | ||
| 111 | printf("buf: %s, fd_type: %u, probe_offset: 0x%llx," | ||
| 112 | " probe_addr: 0x%llx\n", | ||
| 113 | buf, fd_type, probe_offset, probe_addr); | ||
| 114 | return -1; | ||
| 115 | } | ||
| 116 | return 0; | ||
| 117 | } | ||
| 118 | |||
| 119 | static int test_nondebug_fs_kuprobe_common(const char *event_type, | ||
| 120 | const char *name, __u64 offset, __u64 addr, bool is_return, | ||
| 121 | char *buf, __u32 *buf_len, __u32 *prog_id, __u32 *fd_type, | ||
| 122 | __u64 *probe_offset, __u64 *probe_addr) | ||
| 123 | { | ||
| 124 | int is_return_bit = bpf_get_retprobe_bit(event_type); | ||
| 125 | int type = bpf_find_probe_type(event_type); | ||
| 126 | struct perf_event_attr attr = {}; | ||
| 127 | int fd; | ||
| 128 | |||
| 129 | if (type < 0 || is_return_bit < 0) { | ||
| 130 | printf("FAIL: %s incorrect type (%d) or is_return_bit (%d)\n", | ||
| 131 | __func__, type, is_return_bi | ||
