Skip to contentSkip to conversation

bpffs pretty print for cgroup local storage

PATCH Applied Roman Gushchin 3 participants 7 messages Dec 10, 2018 – Dec 12, 2018

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

v2 cover bpffs pretty print for cgroup local storage

This patchset implements bpffs pretty printing for cgroup local storage maps.

v2->v1:
- fixed size checks in cgroup_storage_check_btf(), based on input from Yonghong and Martin
- implemented and used btf_type_is_reg_int(), suggested by Martin

Roman Gushchin (3):
bpf: pass struct btf pointer to the map_check_btf() callback
bpf: add bpffs pretty print for cgroup local storage maps selftests/bpf: add btf annotations for cgroup_local_storage maps

 include/linux/bpf.h                       |  3 +
 include/linux/btf.h                       |  1 +
 kernel/bpf/arraymap.c                     |  1 +
 kernel/bpf/btf.c                          | 22 ++++++
 kernel/bpf/local_storage.c                | 93 ++++++++++++++++++++++-
 kernel/bpf/lpm_trie.c                     |  1 +
 kernel/bpf/syscall.c                      |  3 +-
 tools/testing/selftests/bpf/netcnt_prog.c |  6 ++
 8 files changed, 128 insertions(+), 2 deletions(-)
Signature · 1 line
2.19.2
#1 lore raw

PATCH v2 1/3 bpf: pass struct btf pointer to the map_check_btf() callback

If key_type or value_type are of non-trivial data types (e.g. structure or typedef), it's not possible to check them without the additional information, which can't be obtained without a pointer to the btf structure.

So, let's pass btf pointer to the map_check_btf() callbacks.

---
 include/linux/bpf.h   | 3 +++
 kernel/bpf/arraymap.c | 1 +
 kernel/bpf/lpm_trie.c | 1 +
 kernel/bpf/syscall.c  | 3 ++-
 4 files changed, 7 insertions(+), 1 deletion(-)
include/linux/bpf.h +3 -0
index 0c992b86eb2c..e734f163bd0b 100644
@@ -23,6 +23,7 @@ struct bpf_prog;
 struct bpf_map;
 struct sock;
 struct seq_file;
+struct btf;
 struct btf_type;
 
 /* map is generic key/value storage optionally accesible by eBPF programs */
@@ -52,6 +53,7 @@ struct bpf_map_ops {
 	void (*map_seq_show_elem)(struct bpf_map *map, void *key,
 				  struct seq_file *m);
 	int (*map_check_btf)(const struct bpf_map *map,
+			     const struct btf *btf,
 			     const struct btf_type *key_type,
 			     const struct btf_type *value_type);
 };
@@ -126,6 +128,7 @@ static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
 }
 
 int map_check_no_btf(const struct bpf_map *map,
+		     const struct btf *btf,
 		     const struct btf_type *key_type,
 		     const struct btf_type *value_type);
 
kernel/bpf/arraymap.c +1 -0
index 24583da9ffd1..25632a75d630 100644
@@ -382,6 +382,7 @@ static void percpu_array_map_seq_show_elem(struct bpf_map *map, void *key,
 }
 
 static int array_map_check_btf(const struct bpf_map *map,
+			       const struct btf *btf,
 			       const struct btf_type *key_type,
 			       const struct btf_type *value_type)
 {
kernel/bpf/lpm_trie.c +1 -0
index bfd4882e1106..abf1002080df 100644
@@ -728,6 +728,7 @@ static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key)
 }
 
 static int trie_check_btf(const struct bpf_map *map,
+			  const struct btf *btf,
 			  const struct btf_type *key_type,
 			  const struct btf_type *value_type)
 {
kernel/bpf/syscall.c +2 -1
index a99a23bf5910..4eb7750cb5c0 100644
@@ -456,6 +456,7 @@ static int bpf_obj_name_cpy(char *dst, const char *src)
 }
 
 int map_check_no_btf(const struct bpf_map *map,
+		     const struct btf *btf,
 		     const struct btf_type *key_type,
 		     const struct btf_type *value_type)
 {
@@ -478,7 +479,7 @@ static int map_check_btf(const struct bpf_map *map, const struct btf *btf,
 		return -EINVAL;
 
 	if (map->ops->map_check_btf)
-		ret = map->ops->map_check_btf(map, key_type, value_type);
+		ret = map->ops->map_check_btf(map, btf, key_type, value_type);
 
 	return ret;
 }
Signature · 1 line
2.19.2
#2 lore raw

PATCH v2 2/3 bpf: add bpffs pretty print for cgroup local storage maps

In reply to Roman Gushchin

Implement bpffs pretty printing for cgroup local storage maps (both shared and per-cpu).
Output example (captured for tools/testing/selftests/bpf/netcnt_prog.c):

Shared:

  $ cat /sys/fs/bpf/map_2
  # WARNING!! The output is for debug purpose only
  # WARNING!! The output format will change
  {4294968594,1}: {9999,1039896}

Per-cpu:

  $ cat /sys/fs/bpf/map_1
  # WARNING!! The output is for debug purpose only
  # WARNING!! The output format will change
  {4294968594,1}: {
  	cpu0: {0,0,0,0,0}
  	cpu1: {0,0,0,0,0}
  	cpu2: {1,104,0,0,0}
  	cpu3: {0,0,0,0,0}
  }
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
---
 include/linux/btf.h        |  1 +
 kernel/bpf/btf.c           | 22 +++++++++
 kernel/bpf/local_storage.c | 93 +++++++++++++++++++++++++++++++++++++-
 3 files changed, 115 insertions(+), 1 deletion(-)
include/linux/btf.h +1 -0
index b98405a56383..a4cf075b89eb 100644
@@ -47,6 +47,7 @@ void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
 int btf_get_fd_by_id(u32 id);
 u32 btf_id(const struct btf *btf);
 bool btf_name_offset_valid(const struct btf *btf, u32 offset);
+bool btf_type_is_reg_int(const struct btf_type *t, u32 expected_size);
 
 #ifdef CONFIG_BPF_SYSCALL
 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
kernel/bpf/btf.c +22 -0
index e0a827f95e19..804ab24283f6 100644
@@ -514,6 +514,28 @@ static bool btf_type_int_is_regular(const struct btf_type *t)
 	return true;
 }
 
+/*
+ * Check that given type is a regular int and has the expected size.
+ */
+bool btf_type_is_reg_int(const struct btf_type *t, u32 expected_size)
+{
+	u8 nr_bits, nr_bytes;
+	u32 int_data;
+
+	if (!btf_type_is_int(t))
+		return false;
+
+	int_data = btf_type_int(t);
+	nr_bits = BTF_INT_BITS(int_data);
+	nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
+	if (BITS_PER_BYTE_MASKED(nr_bits) ||
+	    BTF_INT_OFFSET(int_data) ||
+	    nr_bytes != expected_size)
+		return false;
+
+	return true;
+}
+
 __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log,
 					      const char *fmt, ...)
 {
kernel/bpf/local_storage.c +92 -1

116 lines not on this page · raw message

Signature · 1 line
2.19.2
Acked-by on v2 2/3
#3 lore raw

Re: [PATCH v2 bpf-next 2/3] bpf: add bpffs pretty print for cgroup local storage maps

Quoted 11 lines · Roman Gushchin

Implement bpffs pretty printing for cgroup local storage maps (both shared and per-cpu).
Output example (captured for tools/testing/selftests/bpf/netcnt_prog.c):

Shared:

  $ cat /sys/fs/bpf/map_2
  # WARNING!! The output is for debug purpose only
  # WARNING!! The output format will change
  {4294968594,1}: {9999,1039896}

Per-cpu:

Quoted 9 lines
  $ cat /sys/fs/bpf/map_1
  # WARNING!! The output is for debug purpose only
  # WARNING!! The output format will change
  {4294968594,1}: {
  	cpu0: {0,0,0,0,0}
  	cpu1: {0,0,0,0,0}
  	cpu2: {1,104,0,0,0}
  	cpu3: {0,0,0,0,0}
  }
#4 lore raw

PATCH v2 3/3 selftests/bpf: add btf annotations for cgroup_local_storage maps

In reply to Roman Gushchin

Add btf annotations to cgroup local storage maps (per-cpu and shared) in the network packet counting example.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
---
 tools/testing/selftests/bpf/netcnt_prog.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/testing/selftests/bpf/netcnt_prog.c +6 -0
index 1198abca1360..9f741e69cebe 100644
@@ -16,12 +16,18 @@ struct bpf_map_def SEC("maps") percpu_netcnt = {
 	.value_size = sizeof(struct percpu_net_cnt),
 };
 
+BPF_ANNOTATE_KV_PAIR(percpu_netcnt, struct bpf_cgroup_storage_key,
+		     struct percpu_net_cnt);
+
 struct bpf_map_def SEC("maps") netcnt = {
 	.type = BPF_MAP_TYPE_CGROUP_STORAGE,
 	.key_size = sizeof(struct bpf_cgroup_storage_key),
 	.value_size = sizeof(struct net_cnt),
 };
 
+BPF_ANNOTATE_KV_PAIR(netcnt, struct bpf_cgroup_storage_key,
+		     struct net_cnt);
+
 SEC("cgroup/skb")
 int bpf_nextcnt(struct __sk_buff *skb)
 {
Signature · 1 line
2.19.2
Acked-by on v2 3/3
#5 lore raw

Re: [PATCH v2 bpf-next 3/3] selftests/bpf: add btf annotations for cgroup_local_storage maps

Quoted 2 lines · Roman Gushchin

Add btf annotations to cgroup local storage maps (per-cpu and shared) in the network packet counting example.

maintainer Applied
#6 lore raw
In reply to Roman Gushchin
Quoted 12 lines · Roman Gushchin

This patchset implements bpffs pretty printing for cgroup local storage maps.

v2->v1:
- fixed size checks in cgroup_storage_check_btf(), based on input from Yonghong and Martin
- implemented and used btf_type_is_reg_int(), suggested by Martin

Roman Gushchin (3):
bpf: pass struct btf pointer to the map_check_btf() callback
bpf: add bpffs pretty print for cgroup local storage maps selftests/bpf: add btf annotations for cgroup_local_storage maps

Quoted 10 lines
 include/linux/bpf.h                       |  3 +
 include/linux/btf.h                       |  1 +
 kernel/bpf/arraymap.c                     |  1 +
 kernel/bpf/btf.c                          | 22 ++++++
 kernel/bpf/local_storage.c                | 93 ++++++++++++++++++++++-
 kernel/bpf/lpm_trie.c                     |  1 +
 kernel/bpf/syscall.c                      |  3 +-
 tools/testing/selftests/bpf/netcnt_prog.c |  6 ++
 8 files changed, 128 insertions(+), 2 deletions(-)

Applied, thanks!