Skip to content

Commit 65c9362

Browse files
iamkafaiborkmann
authored andcommitted
bpftool: Add struct_ops support
This patch adds struct_ops support to the bpftool. To recap a bit on the recent bpf_struct_ops feature on the kernel side: It currently supports "struct tcp_congestion_ops" to be implemented in bpf. At a high level, bpf_struct_ops is struct_ops map populated with a number of bpf progs. bpf_struct_ops currently supports the "struct tcp_congestion_ops". However, the bpf_struct_ops design is generic enough that other kernel struct ops can be supported in the future. Although struct_ops is map+progs at a high lever, there are differences in details. For example, 1) After registering a struct_ops, the struct_ops is held by the kernel subsystem (e.g. tcp-cc). Thus, there is no need to pin a struct_ops map or its progs in order to keep them around. 2) To iterate all struct_ops in a system, it iterates all maps in type BPF_MAP_TYPE_STRUCT_OPS. BPF_MAP_TYPE_STRUCT_OPS is the current usual filter. In the future, it may need to filter by other struct_ops specific properties. e.g. filter by tcp_congestion_ops or other kernel subsystem ops in the future. 3) struct_ops requires the running kernel having BTF info. That allows more flexibility in handling other kernel structs. e.g. it can always dump the latest bpf_map_info. 4) Also, "struct_ops" command is not intended to repeat all features already provided by "map" or "prog". For example, if there really is a need to pin the struct_ops map, the user can use the "map" cmd to do that. While the first attempt was to reuse parts from map/prog.c, it ended up not a lot to share. The only obvious item is the map_parse_fds() but that still requires modifications to accommodate struct_ops map specific filtering (for the immediate and the future needs). Together with the earlier mentioned differences, it is better to part away from map/prog.c. The initial set of subcmds are, register, unregister, show, and dump. For register, it registers all struct_ops maps that can be found in an obj file. Option can be added in the future to specify a particular struct_ops map. Also, the common bpf_tcp_cc is stateless (e.g. bpf_cubic.c and bpf_dctcp.c). The "reuse map" feature is not implemented in this patch and it can be considered later also. For other subcmds, please see the man doc for details. A sample output of dump: [root@arch-fb-vm1 bpf]# bpftool struct_ops dump name cubic [{ "bpf_map_info": { "type": 26, "id": 64, "key_size": 4, "value_size": 256, "max_entries": 1, "map_flags": 0, "name": "cubic", "ifindex": 0, "btf_vmlinux_value_type_id": 18452, "netns_dev": 0, "netns_ino": 0, "btf_id": 52, "btf_key_type_id": 0, "btf_value_type_id": 0 } },{ "bpf_struct_ops_tcp_congestion_ops": { "refcnt": { "refs": { "counter": 1 } }, "state": "BPF_STRUCT_OPS_STATE_INUSE", "data": { "list": { "next": 0, "prev": 0 }, "key": 0, "flags": 0, "init": "void (struct sock *) bictcp_init/prog_id:138", "release": "void (struct sock *) 0", "ssthresh": "u32 (struct sock *) bictcp_recalc_ssthresh/prog_id:141", "cong_avoid": "void (struct sock *, u32, u32) bictcp_cong_avoid/prog_id:140", "set_state": "void (struct sock *, u8) bictcp_state/prog_id:142", "cwnd_event": "void (struct sock *, enum tcp_ca_event) bictcp_cwnd_event/prog_id:139", "in_ack_event": "void (struct sock *, u32) 0", "undo_cwnd": "u32 (struct sock *) tcp_reno_undo_cwnd/prog_id:144", "pkts_acked": "void (struct sock *, const struct ack_sample *) bictcp_acked/prog_id:143", "min_tso_segs": "u32 (struct sock *) 0", "sndbuf_expand": "u32 (struct sock *) 0", "cong_control": "void (struct sock *, const struct rate_sample *) 0", "get_info": "size_t (struct sock *, u32, int *, union tcp_cc_info *) 0", "name": "bpf_cubic", "owner": 0 } } } ] Signed-off-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Quentin Monnet <quentin@isovalent.com> Link: https://lore.kernel.org/bpf/20200318171656.129650-1-kafai@fb.com
1 parent d5ae04d commit 65c9362

5 files changed

Lines changed: 743 additions & 1 deletion

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
==================
2+
bpftool-struct_ops
3+
==================
4+
-------------------------------------------------------------------------------
5+
tool to register/unregister/introspect BPF struct_ops
6+
-------------------------------------------------------------------------------
7+
8+
:Manual section: 8
9+
10+
SYNOPSIS
11+
========
12+
13+
**bpftool** [*OPTIONS*] **struct_ops** *COMMAND*
14+
15+
*OPTIONS* := { { **-j** | **--json** } [{ **-p** | **--pretty** }] }
16+
17+
*COMMANDS* :=
18+
{ **show** | **list** | **dump** | **register** | **unregister** | **help** }
19+
20+
STRUCT_OPS COMMANDS
21+
===================
22+
23+
| **bpftool** **struct_ops { show | list }** [*STRUCT_OPS_MAP*]
24+
| **bpftool** **struct_ops dump** [*STRUCT_OPS_MAP*]
25+
| **bpftool** **struct_ops register** *OBJ*
26+
| **bpftool** **struct_ops unregister** *STRUCT_OPS_MAP*
27+
| **bpftool** **struct_ops help**
28+
|
29+
| *STRUCT_OPS_MAP* := { **id** *STRUCT_OPS_MAP_ID* | **name** *STRUCT_OPS_MAP_NAME* }
30+
| *OBJ* := /a/file/of/bpf_struct_ops.o
31+
32+
33+
DESCRIPTION
34+
===========
35+
**bpftool struct_ops { show | list }** [*STRUCT_OPS_MAP*]
36+
Show brief information about the struct_ops in the system.
37+
If *STRUCT_OPS_MAP* is specified, it shows information only
38+
for the given struct_ops. Otherwise, it lists all struct_ops
39+
currently existing in the system.
40+
41+
Output will start with struct_ops map ID, followed by its map
42+
name and its struct_ops's kernel type.
43+
44+
**bpftool struct_ops dump** [*STRUCT_OPS_MAP*]
45+
Dump details information about the struct_ops in the system.
46+
If *STRUCT_OPS_MAP* is specified, it dumps information only
47+
for the given struct_ops. Otherwise, it dumps all struct_ops
48+
currently existing in the system.
49+
50+
**bpftool struct_ops register** *OBJ*
51+
Register bpf struct_ops from *OBJ*. All struct_ops under
52+
the ELF section ".struct_ops" will be registered to
53+
its kernel subsystem.
54+
55+
**bpftool struct_ops unregister** *STRUCT_OPS_MAP*
56+
Unregister the *STRUCT_OPS_MAP* from the kernel subsystem.
57+
58+
**bpftool struct_ops help**
59+
Print short help message.
60+
61+
OPTIONS
62+
=======
63+
-h, --help
64+
Print short generic help message (similar to **bpftool help**).
65+
66+
-V, --version
67+
Print version number (similar to **bpftool version**).
68+
69+
-j, --json
70+
Generate JSON output. For commands that cannot produce JSON, this
71+
option has no effect.
72+
73+
-p, --pretty
74+
Generate human-readable JSON output. Implies **-j**.
75+
76+
-d, --debug
77+
Print all logs available, even debug-level information. This
78+
includes logs from libbpf as well as from the verifier, when
79+
attempting to load programs.
80+
81+
EXAMPLES
82+
========
83+
**# bpftool struct_ops show**
84+
85+
::
86+
87+
100: dctcp tcp_congestion_ops
88+
105: cubic tcp_congestion_ops
89+
90+
**# bpftool struct_ops unregister id 105**
91+
92+
::
93+
94+
Unregistered tcp_congestion_ops cubic id 105
95+
96+
**# bpftool struct_ops register bpf_cubic.o**
97+
98+
::
99+
100+
Registered tcp_congestion_ops cubic id 110
101+
102+
103+
SEE ALSO
104+
========
105+
**bpf**\ (2),
106+
**bpf-helpers**\ (7),
107+
**bpftool**\ (8),
108+
**bpftool-prog**\ (8),
109+
**bpftool-map**\ (8),
110+
**bpftool-cgroup**\ (8),
111+
**bpftool-feature**\ (8),
112+
**bpftool-net**\ (8),
113+
**bpftool-perf**\ (8),
114+
**bpftool-btf**\ (8)
115+
**bpftool-gen**\ (8)
116+

‎tools/bpf/bpftool/bash-completion/bpftool‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,34 @@ _bpftool()
576576
;;
577577
esac
578578
;;
579+
struct_ops)
580+
local STRUCT_OPS_TYPE='id name'
581+
case $command in
582+
show|list|dump|unregister)
583+
case $prev in
584+
$command)
585+
COMPREPLY=( $( compgen -W "$STRUCT_OPS_TYPE" -- "$cur" ) )
586+
;;
587+
id)
588+
_bpftool_get_map_ids_for_type struct_ops
589+
;;
590+
name)
591+
_bpftool_get_map_names_for_type struct_ops
592+
;;
593+
esac
594+
return 0
595+
;;
596+
register)
597+
_filedir
598+
return 0
599+
;;
600+
*)
601+
[[ $prev == $object ]] && \
602+
COMPREPLY=( $( compgen -W 'register unregister show list dump help' \
603+
-- "$cur" ) )
604+
;;
605+
esac
606+
;;
579607
map)
580608
local MAP_TYPE='id pinned name'
581609
case $command in

‎tools/bpf/bpftool/main.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static int do_help(int argc, char **argv)
5858
" %s batch file FILE\n"
5959
" %s version\n"
6060
"\n"
61-
" OBJECT := { prog | map | cgroup | perf | net | feature | btf | gen }\n"
61+
" OBJECT := { prog | map | cgroup | perf | net | feature | btf | gen | struct_ops }\n"
6262
" " HELP_SPEC_OPTIONS "\n"
6363
"",
6464
bin_name, bin_name, bin_name);
@@ -221,6 +221,7 @@ static const struct cmd cmds[] = {
221221
{ "feature", do_feature },
222222
{ "btf", do_btf },
223223
{ "gen", do_gen },
224+
{ "struct_ops", do_struct_ops },
224225
{ "version", do_version },
225226
{ 0 }
226227
};

‎tools/bpf/bpftool/main.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ int do_tracelog(int argc, char **arg);
161161
int do_feature(int argc, char **argv);
162162
int do_btf(int argc, char **argv);
163163
int do_gen(int argc, char **argv);
164+
int do_struct_ops(int argc, char **argv);
164165

165166
int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
166167
int prog_parse_fd(int *argc, char ***argv);

0 commit comments

Comments
 (0)