Skip to contentSkip to conversation

samples: bpf: refactor xdp_sample_pkts_kern with BTF-defined map

PATCH In review Daniel T. Lee 2 participants 4 messages Sep 5, 2020 – Sep 9, 2020 1 message not in the archive

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

PATCH 1/1 samples: bpf: refactor xdp_sample_pkts_kern with BTF-defined map

Most of the samples were converted to use the new BTF-defined MAP as they moved to libbbpf, but some of the samples were missing.

Instead of using the previous BPF MAP definition, this commit refactors xdp_sample_pkts_kern MAP definition with the new BTF-defined MAP format.

---
 samples/bpf/xdp_sample_pkts_kern.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
samples/bpf/xdp_sample_pkts_kern.c +6 -6
index 33377289e2a8..b15172b7d455 100644
@@ -7,12 +7,12 @@
 #define SAMPLE_SIZE 64ul
 #define MAX_CPUS 128
 
-struct bpf_map_def SEC("maps") my_map = {
-	.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
-	.key_size = sizeof(int),
-	.value_size = sizeof(u32),
-	.max_entries = MAX_CPUS,
-};
+struct {
+	__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
+	__uint(key_size, sizeof(int));
+	__uint(value_size, sizeof(u32));
+	__uint(max_entries, MAX_CPUS);
+} my_map SEC(".maps");
 
 SEC("xdp_sample")
 int xdp_sample_prog(struct xdp_md *ctx)
Signature · 1 line
2.25.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 8 lines · Michal Rostecki
Quoted 6 lines · Daniel T. Lee

Most of the samples were converted to use the new BTF-defined MAP as they moved to libbbpf, but some of the samples were missing.

Instead of using the previous BPF MAP definition, this commit refactors xdp_sample_pkts_kern MAP definition with the new BTF-defined MAP format.

Quoted 8 lines
Quoted 4 lines
---
 samples/bpf/xdp_sample_pkts_kern.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

I see that samples/bpf/ibumad_kern.c and samples/bpf/xdp_monitor_kern.c still have old style BPF map definitions. Maybe you could change them as well?

Thanks for the review!

Actually, I'm well aware that there are some samples left with old style map definitions, But those examples should be transferred from bpf_load to libbbpf, not just bpf map changes.

I'm also planning to refactor those patches in the future. For now I've just refactored this file, but if you think this patch size is small,
I'll send it with other changes.

#3 lore raw
In reply to Daniel T. Lee
Quoted 21 lines · Daniel T. Lee

Most of the samples were converted to use the new BTF-defined MAP as they moved to libbbpf, but some of the samples were missing.

Instead of using the previous BPF MAP definition, this commit refactors xdp_sample_pkts_kern MAP definition with the new BTF-defined MAP format.

---
 samples/bpf/xdp_sample_pkts_kern.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
samples/bpf/xdp_sample_pkts_kern.c +0 -1
index 33377289e2a8..b15172b7d455 100644
@@ -7,12 +7,12 @@
 #define SAMPLE_SIZE 64ul
 #define MAX_CPUS 128
 
-struct bpf_map_def SEC("maps") my_map = {

samples/bpf/xdp_sample_pkts_kern.c line 14

Quoted 10 lines
samples/bpf/xdp_sample_pkts_kern.c +5 -5
-       .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
-       .key_size = sizeof(int),
-       .value_size = sizeof(u32),
-       .max_entries = MAX_CPUS,
-};
+struct {
+       __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
+       __uint(key_size, sizeof(int));
+       __uint(value_size, sizeof(u32));
+       __uint(max_entries, MAX_CPUS);

if you drop max_entries property, libbpf will set it to the maximum configured number of CPUs on the host, which is what you probably want. Do you might sending v2 without MAX_CPUS (check if macro is still used anywhere else). Thanks!

Quoted 7 lines
samples/bpf/xdp_sample_pkts_kern.c +1 -1
+} my_map SEC(".maps");
 
 SEC("xdp_sample")
 int xdp_sample_prog(struct xdp_md *ctx)
--

2.25.1

#4 lore raw
Quoted 22 lines · Andrii Nakryiko
Quoted 20 lines · Daniel T. Lee

Most of the samples were converted to use the new BTF-defined MAP as they moved to libbbpf, but some of the samples were missing.

Instead of using the previous BPF MAP definition, this commit refactors xdp_sample_pkts_kern MAP definition with the new BTF-defined MAP format.

---
 samples/bpf/xdp_sample_pkts_kern.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
samples/bpf/xdp_sample_pkts_kern.c +0 -0
index 33377289e2a8..b15172b7d455 100644
@@ -7,12 +7,12 @@
 #define SAMPLE_SIZE 64ul
 #define MAX_CPUS 128
 
Quoted 17 lines

samples/bpf/xdp_sample_pkts_kern.c line 14

Quoted 11 lines
samples/bpf/xdp_sample_pkts_kern.c +5 -6
-struct bpf_map_def SEC("maps") my_map = {
-       .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
-       .key_size = sizeof(int),
-       .value_size = sizeof(u32),
-       .max_entries = MAX_CPUS,
-};
+struct {
+       __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
+       __uint(key_size, sizeof(int));
+       __uint(value_size, sizeof(u32));
+       __uint(max_entries, MAX_CPUS);

if you drop max_entries property, libbpf will set it to the maximum configured number of CPUs on the host, which is what you probably want. Do you might sending v2 without MAX_CPUS (check if macro is still used anywhere else). Thanks!

Thanks for your time and effort for the review.

I'll check and send the next version of patch.

Quoted 7 lines
Quoted 7 lines
samples/bpf/xdp_sample_pkts_kern.c +1 -1
+} my_map SEC(".maps");
 
 SEC("xdp_sample")
 int xdp_sample_prog(struct xdp_md *ctx)
--

2.25.1

Signature · 2 lines
Best,
Daniel T. Lee