summaryrefslogtreecommitdiffstats
path: root/samples/bpf
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2019-05-09 12:02:46 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2019-05-09 12:02:46 -0400
commitdce45af5c2e9e85f22578f2f8065f225f5d11764 (patch)
treee01e7a294586c3074142fb485516ce718a1a82d2 /samples/bpf
parent055128ee008b00fba14e3638e7e84fc2cff8d77d (diff)
parentb79656ed44c6865e17bcd93472ec39488bcc4984 (diff)
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma
Pull rdma updates from Jason Gunthorpe: "This has been a smaller cycle than normal. One new driver was accepted, which is unusual, and at least one more driver remains in review on the list. Summary: - Driver fixes for hns, hfi1, nes, rxe, i40iw, mlx5, cxgb4, vmw_pvrdma - Many patches from MatthewW converting radix tree and IDR users to use xarray - Introduction of tracepoints to the MAD layer - Build large SGLs at the start for DMA mapping and get the driver to split them - Generally clean SGL handling code throughout the subsystem - Support for restricting RDMA devices to net namespaces for containers - Progress to remove object allocation boilerplate code from drivers - Change in how the mlx5 driver shows representor ports linked to VFs - mlx5 uapi feature to access the on chip SW ICM memory - Add a new driver for 'EFA'. This is HW that supports user space packet processing through QPs in Amazon's cloud" * tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma: (186 commits) RDMA/ipoib: Allow user space differentiate between valid dev_port IB/core, ipoib: Do not overreact to SM LID change event RDMA/device: Don't fire uevent before device is fully initialized lib/scatterlist: Remove leftover from sg_page_iter comment RDMA/efa: Add driver to Kconfig/Makefile RDMA/efa: Add the efa module RDMA/efa: Add EFA verbs implementation RDMA/efa: Add common command handlers RDMA/efa: Implement functions that submit and complete admin commands RDMA/efa: Add the ABI definitions RDMA/efa: Add the com service API definitions RDMA/efa: Add the efa_com.h file RDMA/efa: Add the efa.h header file RDMA/efa: Add EFA device definitions RDMA: Add EFA related definitions RDMA/umem: Remove hugetlb flag RDMA/bnxt_re: Use core helpers to get aligned DMA address RDMA/i40iw: Use core helpers to get aligned DMA address within a supported page size RDMA/verbs: Add a DMA iterator to return aligned contiguous memory blocks RDMA/umem: Add API to find best driver supported page size in an MR ...
Diffstat (limited to 'samples/bpf')
-rw-r--r--samples/bpf/Makefile3
-rw-r--r--samples/bpf/ibumad_kern.c144
-rw-r--r--samples/bpf/ibumad_user.c122
3 files changed, 269 insertions, 0 deletions
diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index 65e667bdf979..4f0a1cdbfe7c 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -52,6 +52,7 @@ hostprogs-y += xdpsock
52hostprogs-y += xdp_fwd 52hostprogs-y += xdp_fwd
53hostprogs-y += task_fd_query 53hostprogs-y += task_fd_query
54hostprogs-y += xdp_sample_pkts 54hostprogs-y += xdp_sample_pkts
55hostprogs-y += ibumad
55hostprogs-y += hbm 56hostprogs-y += hbm
56 57
57# Libbpf dependencies 58# Libbpf dependencies
@@ -108,6 +109,7 @@ xdpsock-objs := xdpsock_user.o
108xdp_fwd-objs := xdp_fwd_user.o 109xdp_fwd-objs := xdp_fwd_user.o
109task_fd_query-objs := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS) 110task_fd_query-objs := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS)
110xdp_sample_pkts-objs := xdp_sample_pkts_user.o $(TRACE_HELPERS) 111xdp_sample_pkts-objs := xdp_sample_pkts_user.o $(TRACE_HELPERS)
112ibumad-objs := bpf_load.o ibumad_user.o $(TRACE_HELPERS)
111hbm-objs := bpf_load.o hbm.o $(CGROUP_HELPERS) 113hbm-objs := bpf_load.o hbm.o $(CGROUP_HELPERS)
112 114
113# Tell kbuild to always build the programs 115# Tell kbuild to always build the programs
@@ -166,6 +168,7 @@ always += xdp_adjust_tail_kern.o
166always += xdp_fwd_kern.o 168always += xdp_fwd_kern.o
167always += task_fd_query_kern.o 169always += task_fd_query_kern.o
168always += xdp_sample_pkts_kern.o 170always += xdp_sample_pkts_kern.o
171always += ibumad_kern.o
169always += hbm_out_kern.o 172always += hbm_out_kern.o
170 173
171KBUILD_HOSTCFLAGS += -I$(objtree)/usr/include 174KBUILD_HOSTCFLAGS += -I$(objtree)/usr/include
diff --git a/samples/bpf/ibumad_kern.c b/samples/bpf/ibumad_kern.c
new file mode 100644
index 000000000000..38b2b3f22049
--- /dev/null
+++ b/samples/bpf/ibumad_kern.c
@@ -0,0 +1,144 @@
1// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2
3/**
4 * ibumad BPF sample kernel side
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * Copyright(c) 2018 Ira Weiny, Intel Corporation
11 */
12
13#define KBUILD_MODNAME "ibumad_count_pkts_by_class"
14#include <uapi/linux/bpf.h>
15
16#include "bpf_helpers.h"
17
18
19struct bpf_map_def SEC("maps") read_count = {
20 .type = BPF_MAP_TYPE_ARRAY,
21 .key_size = sizeof(u32), /* class; u32 required */
22 .value_size = sizeof(u64), /* count of mads read */
23 .max_entries = 256, /* Room for all Classes */
24};
25
26struct bpf_map_def SEC("maps") write_count = {
27 .type = BPF_MAP_TYPE_ARRAY,
28 .key_size = sizeof(u32), /* class; u32 required */
29 .value_size = sizeof(u64), /* count of mads written */
30 .max_entries = 256, /* Room for all Classes */
31};
32
33#undef DEBUG
34#ifdef DEBUG
35#define bpf_debug(fmt, ...) \
36({ \
37 char ____fmt[] = fmt; \
38 bpf_trace_printk(____fmt, sizeof(____fmt), \
39 ##__VA_ARGS__); \
40})
41#else
42#define bpf_debug(fmt, ...)
43#endif
44
45/* Taken from the current format defined in
46 * include/trace/events/ib_umad.h
47 * and
48 * /sys/kernel/debug/tracing/events/ib_umad/ib_umad_read/format
49 * /sys/kernel/debug/tracing/events/ib_umad/ib_umad_write/format
50 */
51struct ib_umad_rw_args {
52 u64 pad;
53 u8 port_num;
54 u8 sl;
55 u8 path_bits;
56 u8 grh_present;
57 u32 id;
58 u32 status;
59 u32 timeout_ms;
60 u32 retires;
61 u32 length;
62 u32 qpn;
63 u32 qkey;
64 u8 gid_index;
65 u8 hop_limit;
66 u16 lid;
67 u16 attr_id;
68 u16 pkey_index;
69 u8 base_version;
70 u8 mgmt_class;
71 u8 class_version;
72 u8 method;
73 u32 flow_label;
74 u16 mad_status;
75 u16 class_specific;
76 u32 attr_mod;
77 u64 tid;
78 u8 gid[16];
79 u32 dev_index;
80 u8 traffic_class;
81};
82
83SEC("tracepoint/ib_umad/ib_umad_read_recv")
84int on_ib_umad_read_recv(struct ib_umad_rw_args *ctx)
85{
86 u64 zero = 0, *val;
87 u8 class = ctx->mgmt_class;
88
89 bpf_debug("ib_umad read recv : class 0x%x\n", class);
90
91 val = bpf_map_lookup_elem(&read_count, &class);
92 if (!val) {
93 bpf_map_update_elem(&read_count, &class, &zero, BPF_NOEXIST);
94 val = bpf_map_lookup_elem(&read_count, &class);
95 if (!val)
96 return 0;
97 }
98
99 (*val) += 1;
100
101 return 0;
102}
103SEC("tracepoint/ib_umad/ib_umad_read_send")
104int on_ib_umad_read_send(struct ib_umad_rw_args *ctx)
105{
106 u64 zero = 0, *val;
107 u8 class = ctx->mgmt_class;
108
109 bpf_debug("ib_umad read send : class 0x%x\n", class);
110
111 val = bpf_map_lookup_elem(&read_count, &class);
112 if (!val) {
113 bpf_map_update_elem(&read_count, &class, &zero, BPF_NOEXIST);
114 val = bpf_map_lookup_elem(&read_count, &class);
115 if (!val)
116 return 0;
117 }
118
119 (*val) += 1;
120
121 return 0;
122}
123SEC("tracepoint/ib_umad/ib_umad_write")
124int on_ib_umad_write(struct ib_umad_rw_args *ctx)
125{
126 u64 zero = 0, *val;
127 u8 class = ctx->mgmt_class;
128
129 bpf_debug("ib_umad write : class 0x%x\n", class);
130
131 val = bpf_map_lookup_elem(&write_count, &class);
132 if (!val) {
133 bpf_map_update_elem(&write_count, &class, &zero, BPF_NOEXIST);
134 val = bpf_map_lookup_elem(&write_count, &class);
135 if (!val)
136 return 0;
137 }
138
139 (*val) += 1;
140
141 return 0;
142}
143
144char _license[] SEC("license") = "GPL";
diff --git a/samples/bpf/ibumad_user.c b/samples/bpf/ibumad_user.c
new file mode 100644
index 000000000000..097d76143363
--- /dev/null
+++ b/samples/bpf/ibumad_user.c
@@ -0,0 +1,122 @@
1// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2
3/**
4 * ibumad BPF sample user side
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * Copyright(c) 2018 Ira Weiny, Intel Corporation
11 */
12
13#include <linux/bpf.h>
14#include <signal.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19#include <sys/types.h>
20#include <limits.h>
21
22#include <sys/resource.h>
23#include <getopt.h>
24#include <net/if.h>
25
26#include "bpf_load.h"
27#include "bpf_util.h"
28#include "bpf/libbpf.h"
29
30static void dump_counts(int fd)
31{
32 __u32 key;
33 __u64 value;
34
35 for (key = 0; key < 256; key++) {
36 if (bpf_map_lookup_elem(fd, &key, &value)) {
37 printf("failed to read key %u\n", key);
38 continue;
39 }
40 if (value)
41 printf("0x%02x : %llu\n", key, value);
42 }
43}
44
45static void dump_all_counts(void)
46{
47 printf("Read 'Class : count'\n");
48 dump_counts(map_fd[0]);
49 printf("Write 'Class : count'\n");
50 dump_counts(map_fd[1]);
51}
52
53static void dump_exit(int sig)
54{
55 dump_all_counts();
56 exit(0);
57}
58
59static const struct option long_options[] = {
60 {"help", no_argument, NULL, 'h'},
61 {"delay", required_argument, NULL, 'd'},
62};
63
64static void usage(char *cmd)
65{
66 printf("eBPF test program to count packets from various IP addresses\n"
67 "Usage: %s <options>\n"
68 " --help, -h this menu\n"
69 " --delay, -d <delay> wait <delay> sec between prints [1 - 1000000]\n"
70 , cmd
71 );
72}
73
74int main(int argc, char **argv)
75{
76 unsigned long delay = 5;
77 int longindex = 0;
78 int opt;
79 char bpf_file[256];
80
81 /* Create the eBPF kernel code path name.
82 * This follows the pattern of all of the other bpf samples
83 */
84 snprintf(bpf_file, sizeof(bpf_file), "%s_kern.o", argv[0]);
85
86 /* Do one final dump when exiting */
87 signal(SIGINT, dump_exit);
88 signal(SIGTERM, dump_exit);
89
90 while ((opt = getopt_long(argc, argv, "hd:rSw",
91 long_options, &longindex)) != -1) {
92 switch (opt) {
93 case 'd':
94 delay = strtoul(optarg, NULL, 0);
95 if (delay == ULONG_MAX || delay < 0 ||
96 delay > 1000000) {
97 fprintf(stderr, "ERROR: invalid delay : %s\n",
98 optarg);
99 usage(argv[0]);
100 return 1;
101 }
102 break;
103 default:
104 case 'h':
105 usage(argv[0]);
106 return 1;
107 }
108 }
109
110 if (load_bpf_file(bpf_file)) {
111 fprintf(stderr, "ERROR: failed to load eBPF from file : %s\n",
112 bpf_file);
113 return 1;
114 }
115
116 while (1) {
117 sleep(delay);
118 dump_all_counts();
119 }
120
121 return 0;
122}