Skip to content

Commit 7f0c57f

Browse files
fomichevborkmann
authored andcommitted
bpftool: show flow_dissector attachment status
Right now there is no way to query whether BPF flow_dissector program is attached to a network namespace or not. In previous commit, I added support for querying that info, show it when doing `bpftool net`: $ bpftool prog loadall ./bpf_flow.o \ /sys/fs/bpf/flow type flow_dissector \ pinmaps /sys/fs/bpf/flow $ bpftool prog 3: flow_dissector name _dissect tag 8c9e917b513dd5cc gpl loaded_at 2019-04-23T16:14:48-0700 uid 0 xlated 656B jited 461B memlock 4096B map_ids 1,2 btf_id 1 ... $ bpftool net -j [{"xdp":[],"tc":[],"flow_dissector":[]}] $ bpftool prog attach pinned \ /sys/fs/bpf/flow/flow_dissector flow_dissector $ bpftool net -j [{"xdp":[],"tc":[],"flow_dissector":["id":3]}] Doesn't show up in a different net namespace: $ ip netns add test $ ip netns exec test bpftool net -j [{"xdp":[],"tc":[],"flow_dissector":[]}] Non-json output: $ bpftool net xdp: tc: flow_dissector: id 3 v2: * initialization order (Jakub Kicinski) * clear errno for batch mode (Quentin Monnet) Signed-off-by: Stanislav Fomichev <sdf@google.com> Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com> Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 118c8e9 commit 7f0c57f

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

‎tools/bpf/bpftool/net.c‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#define _GNU_SOURCE
55
#include <errno.h>
6+
#include <fcntl.h>
67
#include <stdlib.h>
78
#include <string.h>
89
#include <unistd.h>
@@ -12,6 +13,8 @@
1213
#include <linux/rtnetlink.h>
1314
#include <linux/tc_act/tc_bpf.h>
1415
#include <sys/socket.h>
16+
#include <sys/stat.h>
17+
#include <sys/types.h>
1518

1619
#include <bpf.h>
1720
#include <nlattr.h>
@@ -48,6 +51,10 @@ struct bpf_filter_t {
4851
int ifindex;
4952
};
5053

54+
struct bpf_attach_info {
55+
__u32 flow_dissector_id;
56+
};
57+
5158
static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
5259
{
5360
struct bpf_netdev_t *netinfo = cookie;
@@ -180,8 +187,45 @@ static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
180187
return 0;
181188
}
182189

190+
static int query_flow_dissector(struct bpf_attach_info *attach_info)
191+
{
192+
__u32 attach_flags;
193+
__u32 prog_ids[1];
194+
__u32 prog_cnt;
195+
int err;
196+
int fd;
197+
198+
fd = open("/proc/self/ns/net", O_RDONLY);
199+
if (fd < 0) {
200+
p_err("can't open /proc/self/ns/net: %d",
201+
strerror(errno));
202+
return -1;
203+
}
204+
prog_cnt = ARRAY_SIZE(prog_ids);
205+
err = bpf_prog_query(fd, BPF_FLOW_DISSECTOR, 0,
206+
&attach_flags, prog_ids, &prog_cnt);
207+
close(fd);
208+
if (err) {
209+
if (errno == EINVAL) {
210+
/* Older kernel's don't support querying
211+
* flow dissector programs.
212+
*/
213+
errno = 0;
214+
return 0;
215+
}
216+
p_err("can't query prog: %s", strerror(errno));
217+
return -1;
218+
}
219+
220+
if (prog_cnt == 1)
221+
attach_info->flow_dissector_id = prog_ids[0];
222+
223+
return 0;
224+
}
225+
183226
static int do_show(int argc, char **argv)
184227
{
228+
struct bpf_attach_info attach_info = {};
185229
int i, sock, ret, filter_idx = -1;
186230
struct bpf_netdev_t dev_array;
187231
unsigned int nl_pid;
@@ -199,6 +243,10 @@ static int do_show(int argc, char **argv)
199243
usage();
200244
}
201245

246+
ret = query_flow_dissector(&attach_info);
247+
if (ret)
248+
return -1;
249+
202250
sock = libbpf_netlink_open(&nl_pid);
203251
if (sock < 0) {
204252
fprintf(stderr, "failed to open netlink sock\n");
@@ -227,6 +275,12 @@ static int do_show(int argc, char **argv)
227275
}
228276
NET_END_ARRAY("\n");
229277
}
278+
279+
NET_START_ARRAY("flow_dissector", "%s:\n");
280+
if (attach_info.flow_dissector_id > 0)
281+
NET_DUMP_UINT("id", "id %u", attach_info.flow_dissector_id);
282+
NET_END_ARRAY("\n");
283+
230284
NET_END_OBJECT;
231285
if (json_output)
232286
jsonw_end_array(json_wtr);

0 commit comments

Comments
 (0)