Skip to contentSkip to conversation

tools/bpf: bpftool: improve output format for bpftool net

PATCH Applied Yonghong Song 2 participants 2 messages Sep 18, 2018

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tools/bpf: bpftool: improve output format for bpftool net

This is a followup patch for Commit f6f3bac08ff9 ("tools/bpf: bpftool: add net support").
Some improvements are made for the bpftool net output. Specially, plain output is more concise such that per attachment should nicely fit in one line. Compared to previous output, the prog tag is removed since it can be easily obtained with program id. Similar to xdp attachments, the device name is added to tc attachments.

The bpf program attached through shared block mechanism is supported as well.

  $ ip link add dev v1 type veth peer name v2
  $ tc qdisc add dev v1 ingress_block 10 egress_block 20 clsact
  $ tc qdisc add dev v2 ingress_block 10 egress_block 20 clsact
  $ tc filter add block 10 protocol ip prio 25 bpf obj bpf_shared.o sec ingress flowid 1:1
  $ tc filter add block 20 protocol ip prio 30 bpf obj bpf_cyclic.o sec classifier flowid 1:1
  $ bpftool net
  xdp:

tc:
v2(7) clsact/ingress bpf_shared.o:[ingress] id 23 v2(7) clsact/egress bpf_cyclic.o:[classifier] id 24 v1(8) clsact/ingress bpf_shared.o:[ingress] id 23 v1(8) clsact/egress bpf_cyclic.o:[classifier] id 24

The documentation and "bpftool net help" are updated to make it clear that current implementation only supports xdp and tc attachments. For programs attached to cgroups, "bpftool cgroup" can be used to dump attachments. For other programs e.g. sk_{filter,skb,msg,reuseport} and lwt/seg6, iproute2 tools should be used.

The new output:

  $ bpftool net
  xdp:
  eth0(2) driver id 198
  tc:
  eth0(2) clsact/ingress fbflow_icmp id 335 act [{icmp_action id 336}]
  eth0(2) clsact/egress fbflow_egress id 334
  $ bpftool -jp net
  [{
        "xdp": [{
                "devname": "eth0",
                "ifindex": 2,
                "mode": "driver",
                "id": 198
            }
        ],
        "tc": [{
                "devname": "eth0",
                "ifindex": 2,
                "kind": "clsact/ingress",
                "name": "fbflow_icmp",
                "id": 335,
                "act": [{
                        "name": "icmp_action",
                        "id": 336
                    }
                ]
            },{
                "devname": "eth0",
                "ifindex": 2,
                "kind": "clsact/egress",
                "name": "fbflow_egress",
                "id": 334
            }
        ]
    }
  ]
---
 .../bpf/bpftool/Documentation/bpftool-net.rst |  78 +++++++------
 tools/bpf/bpftool/main.h                      |   3 +-
 tools/bpf/bpftool/net.c                       | 103 ++++++++++++------
 tools/bpf/bpftool/netlink_dumper.c            |  85 +++++++--------
 tools/bpf/bpftool/netlink_dumper.h            |  22 ++--
 5 files changed, 161 insertions(+), 130 deletions(-)

Changelogs:
v1 -> v2:
. Addressed several output format suggestions from Daniel

tools/bpf/bpftool/Documentation/bpftool-net.rst +42 -36
index 48a61837a264..408ec30d8872 100644
@@ -26,9 +26,20 @@ NET COMMANDS
 DESCRIPTION
 ===========
 	**bpftool net { show | list } [ dev name ]**
-		  List all networking device driver and tc attachment in the system.
-
-                  Output will start with all xdp program attachment, followed by
+                  List bpf program attachments in the kernel networking subsystem.
+
+                  Currently, only device driver xdp attachments and tc filter
+                  classification/action attachments are implemented, i.e., for
+                  program types **BPF_PROG_TYPE_SCHED_CLS**,
+                  **BPF_PROG_TYPE_SCHED_ACT** and **BPF_PROG_TYPE_XDP**.
+                  For programs attached to a particular cgroup, e.g.,
+                  **BPF_PROG_TYPE_CGROUP_SKB**, **BPF_PROG_TYPE_CGROUP_SOCK**,
+                  **BPF_PROG_TYPE_SOCK_OPS** and **BPF_PROG_TYPE_CGROUP_SOCK_ADDR**,
+                  users can use **bpftool cgroup** to dump cgroup attachments.
+                  For sk_{filter, skb, msg, reuseport} and lwt/seg6
+                  bpf programs, users should consult other tools, e.g., iproute2.
+
+                  The current output will start with all xdp program attachments, followed by
                   all tc class/qdisc bpf program attachments. Both xdp programs and
                   tc programs are ordered based on ifindex number. If multiple bpf
                   programs attached to the same networking device through **tc filter**,
@@ -61,21 +72,15 @@ EXAMPLES
 
 ::
 
-      xdp [
-      ifindex 2 devname eth0 prog_id 198
-      ]
-      tc_filters [
-      ifindex 2 kind qdisc_htb name prefix_matcher.o:[cls_prefix_matcher_htb]
-                prog_id 111727 tag d08fe3b4319bc2fd act []
-      ifindex 2 kind qdisc_clsact_ingress name fbflow_icmp
-                prog_id 130246 tag 3f265c7f26db62c9 act []
-      ifindex 2 kind qdisc_clsact_egress name prefix_matcher.o:[cls_prefix_matcher_clsact]
-                prog_id 111726 tag 99a197826974c876
-      ifindex 2 kind qdisc_clsact_egress name cls_fg_dscp
-                prog_id 108619 tag dc4630674fd72dcc act []
-      ifindex 2 kind qdisc_clsact_egress name fbflow_egress
-                prog_id 130245 tag 72d2d830d6888d2c
-      ]
+      xdp:
+      eth0(2) driver id 198
+
+      tc:
+      eth0(2) htb name prefix_matcher.o:[cls_prefix_matcher_htb] id 111727 act []
+      eth0(2) clsact/ingress fbflow_icmp id 130246 act []
+      eth0(2) clsact/egress prefix_matcher.o:[cls_prefix_matcher_clsact] id 111726
+      eth0(2) clsact/egress cls_fg_dscp id 108619 act []
+      eth0(2) clsact/egress fbflow_egress id 130245
 
 |
 | **# bpftool -jp net**
@@ -84,44 +89,45 @@ EXAMPLES
 
     [{
             "xdp": [{
-                    "ifindex": 2,
                     "devname": "eth0",
-                    "prog_id": 198
+                    "ifindex": 2,
+                    "mode": "driver",
+                    "id": 198
                 }
             ],
-            "tc_filters": [{
+            "tc": [{
+                    "devname": "eth0",
                     "ifindex": 2,
-                    "kind": "qdisc_htb",
+                    "kind": "htb",
                     "name": "prefix_matcher.o:[cls_prefix_matcher_htb]",
-                    "prog_id": 111727,
-                    "tag": "d08fe3b4319bc2fd",
+                    "id": 111727,
                     "act": []
                 },{
+                    "devname": "eth0",
                     "ifindex": 2,
-                    "kind": "qdisc_clsact_ingress",
+                    "kind": "clsact/ingress",
                     "name": "fbflow_icmp",
-                    "prog_id": 130246,
-                    "tag": "3f265c7f26db62c9",
+                    "id": 130246,
                     "act": []
                 },{
+                    "devname": "eth0",
                     "ifindex": 2,
-                    "kind": "qdisc_clsact_egress",
+                    "kind": "clsact/egress",
                     "name": "prefix_matcher.o:[cls_prefix_matcher_clsact]",
-                    "prog_id": 111726,
-                    "tag": "99a197826974c876"
+                    "id": 111726,
                 },{
+                    "devname": "eth0",
                     "ifindex": 2,
-                    "kind": "qdisc_clsact_egress",
+                    "kind": "clsact/egress",
                     "name": "cls_fg_dscp",
-                    "prog_id": 108619,
-                    "tag": "dc4630674fd72dcc",
+                    "id": 108619,
                     "act": []
                 },{
+                    "devname": "eth0",
                     "ifindex": 2,
-                    "kind": "qdisc_clsact_egress",
+                    "kind": "clsact/egress",
                     "name": "fbflow_egress",
-                    "prog_id": 130245,
-                    "tag": "72d2d830d6888d2c"
+                    "id": 130245,
                 }
             ]
         }
tools/bpf/bpftool/main.h +2 -1
index 02dfbcb92a23..40492cdc4e53 100644
@@ -171,5 +171,6 @@ struct nlattr;
 struct ifinfomsg;
 struct tcmsg;
 int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb);
-int do_filter_dump(struct tcmsg *ifinfo, struct nlattr **tb, const char *kind);
+int do_filter_dump(struct tcmsg *ifinfo, struct nlattr **tb, const char *kind,
+		   const char *devname, int ifindex);
 #endif
tools/bpf/bpftool/net.c +71 -32

211 lines not on this page · raw message

tools/bpf/bpftool/netlink_dumper.c +39 -46

155 lines not on this page · raw message

tools/bpf/bpftool/netlink_dumper.h +7 -15
index 552d8851ac06..0788cfbbed0e 100644
@@ -50,13 +50,13 @@
 		fprintf(stderr, "\n");			\
 }
 
-#define NET_START_ARRAY(name, newline)			\
+#define NET_START_ARRAY(name, fmt_str)			\
 {							\
 	if (json_output) {				\
 		jsonw_name(json_wtr, name);		\
 		jsonw_start_array(json_wtr);		\
 	} else {					\
-		fprintf(stderr, "%s [%s", name, newline);\
+		fprintf(stderr, fmt_str, name);		\
 	}						\
 }
 
@@ -65,31 +65,23 @@
 	if (json_output)				\
 		jsonw_end_array(json_wtr);		\
 	else						\
-		fprintf(stderr, "]%s", endstr);		\
+		fprintf(stderr, "%s", endstr);		\
 }
 
-#define NET_DUMP_UINT(name, val)			\
+#define NET_DUMP_UINT(name, fmt_str, val)		\
 {							\
 	if (json_output)				\
 		jsonw_uint_field(json_wtr, name, val);	\
 	else						\
-		fprintf(stderr, "%s %d ", name, val);	\
+		fprintf(stderr, fmt_str, val);		\
 }
 
-#define NET_DUMP_LLUINT(name, val)			\
-{							\
-	if (json_output)				\
-		jsonw_lluint_field(json_wtr, name, val);\
-	else						\
-		fprintf(stderr, "%s %lld ", name, val);	\
-}
-
-#define NET_DUMP_STR(name, str)				\
+#define NET_DUMP_STR(name, fmt_str, str)		\
 {							\
 	if (json_output)				\
 		jsonw_string_field(json_wtr, name, str);\
 	else						\
-		fprintf(stderr, "%s %s ", name, str);	\
+		fprintf(stderr, fmt_str, str);		\
 }
 
 #define NET_DUMP_STR_ONLY(str)				\
Signature · 1 line
2.17.1
maintainer Applied
#1 lore raw
Quoted 73 lines · Yonghong Song

This is a followup patch for Commit f6f3bac08ff9 ("tools/bpf: bpftool: add net support").
Some improvements are made for the bpftool net output. Specially, plain output is more concise such that per attachment should nicely fit in one line. Compared to previous output, the prog tag is removed since it can be easily obtained with program id. Similar to xdp attachments, the device name is added to tc attachments.

The bpf program attached through shared block mechanism is supported as well.

  $ ip link add dev v1 type veth peer name v2
  $ tc qdisc add dev v1 ingress_block 10 egress_block 20 clsact
  $ tc qdisc add dev v2 ingress_block 10 egress_block 20 clsact
  $ tc filter add block 10 protocol ip prio 25 bpf obj bpf_shared.o sec ingress flowid 1:1
  $ tc filter add block 20 protocol ip prio 30 bpf obj bpf_cyclic.o sec classifier flowid 1:1
  $ bpftool net
  xdp:

tc:
v2(7) clsact/ingress bpf_shared.o:[ingress] id 23 v2(7) clsact/egress bpf_cyclic.o:[classifier] id 24 v1(8) clsact/ingress bpf_shared.o:[ingress] id 23 v1(8) clsact/egress bpf_cyclic.o:[classifier] id 24

The documentation and "bpftool net help" are updated to make it clear that current implementation only supports xdp and tc attachments. For programs attached to cgroups, "bpftool cgroup" can be used to dump attachments. For other programs e.g. sk_{filter,skb,msg,reuseport} and lwt/seg6, iproute2 tools should be used.

The new output:

  $ bpftool net
  xdp:
  eth0(2) driver id 198
  tc:
  eth0(2) clsact/ingress fbflow_icmp id 335 act [{icmp_action id 336}]
  eth0(2) clsact/egress fbflow_egress id 334
  $ bpftool -jp net
  [{
        "xdp": [{
                "devname": "eth0",
                "ifindex": 2,
                "mode": "driver",
                "id": 198
            }
        ],
        "tc": [{
                "devname": "eth0",
                "ifindex": 2,
                "kind": "clsact/ingress",
                "name": "fbflow_icmp",
                "id": 335,
                "act": [{
                        "name": "icmp_action",
                        "id": 336
                    }
                ]
            },{
                "devname": "eth0",
                "ifindex": 2,
                "kind": "clsact/egress",
                "name": "fbflow_egress",
                "id": 334
            }
        ]
    }
  ]
Quoted 1 line

Applied to bpf-next, thanks!