Skip to contentSkip to conversation

sample: xdp1 improvements

PATCH Applied Matteo Croce 3 participants 6 messages Dec 1, 2018 – Dec 3, 2018

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

v2 cover sample: xdp1 improvements

Small improvements to improve the readability and easiness to use of the xdp1 sample.

Matteo Croce (2):
samples: bpf: improve xdp1 example
samples: bpf: get ifindex from ifname

 samples/bpf/xdp1_user.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)
Signature · 1 line
2.19.1
#1 lore raw

PATCH v2 1/2 samples: bpf: improve xdp1 example

Store only the total packet count for every protocol, instead of the whole per-cpu array.
Use bpf_map_get_next_key() to iterate the map, instead of looking up all the protocols.

---
 samples/bpf/xdp1_user.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
samples/bpf/xdp1_user.c +8 -10
index b02c531510ed..4f3d824fc044 100644
@@ -34,26 +34,24 @@ static void int_exit(int sig)
 static void poll_stats(int map_fd, int interval)
 {
 	unsigned int nr_cpus = bpf_num_possible_cpus();
-	const unsigned int nr_keys = 256;
-	__u64 values[nr_cpus], prev[nr_keys][nr_cpus];
-	__u32 key;
+	__u64 values[nr_cpus], prev[UINT8_MAX] = { 0 };
 	int i;
 
-	memset(prev, 0, sizeof(prev));
-
 	while (1) {
+		__u32 key = UINT32_MAX;
+
 		sleep(interval);
 
-		for (key = 0; key < nr_keys; key++) {
+		while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
 			__u64 sum = 0;
 
 			assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
 			for (i = 0; i < nr_cpus; i++)
-				sum += (values[i] - prev[key][i]);
-			if (sum)
+				sum += values[i];
+			if (sum > prev[key])
 				printf("proto %u: %10llu pkt/s\n",
-				       key, sum / interval);
-			memcpy(prev[key], values, sizeof(values));
+				       key, (sum - prev[key]) / interval);
+			prev[key] = sum;
 		}
 	}
 }
Signature · 1 line
2.19.1
#2 lore raw

PATCH v2 2/2 samples: bpf: get ifindex from ifname

Find the ifindex with if_nametoindex() instead of requiring the numeric ifindex.

---
v2: use if_nametoindex() instead of ioctl()

 samples/bpf/xdp1_user.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
samples/bpf/xdp1_user.c +7 -2
index 4f3d824fc044..0a197f86ac43 100644
@@ -15,6 +15,7 @@
 #include <unistd.h>
 #include <libgen.h>
 #include <sys/resource.h>
+#include <net/if.h>
 
 #include "bpf_util.h"
 #include "bpf/bpf.h"
@@ -59,7 +60,7 @@ static void poll_stats(int map_fd, int interval)
 static void usage(const char *prog)
 {
 	fprintf(stderr,
-		"usage: %s [OPTS] IFINDEX\n\n"
+		"usage: %s [OPTS] IFACE\n\n"
 		"OPTS:\n"
 		"    -S    use skb-mode\n"
 		"    -N    enforce native mode\n",
@@ -102,7 +103,11 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	ifindex = strtoul(argv[optind], NULL, 0);
+	ifindex = if_nametoindex(argv[1]);
+	if (!ifindex) {
+		perror("if_nametoindex");
+		return 1;
+	}
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 	prog_load_attr.file = filename;
Signature · 1 line
2.19.1
#3 lore raw
In reply to Matteo Croce
Quoted 2 lines · Matteo Croce

Small improvements to improve the readability and easiness to use of the xdp1 sample.

Applied to bpf-next.

I think that sample code could be more useful if it's wrapped with bash script like selftests/test_xdp* tests do.
At that point it can move to selftests to get 0bot coverage. Would you be interested in doing that?

#4 lore raw
Quoted 5 lines · Alexei Starovoitov
Quoted 2 lines · Matteo Croce

Small improvements to improve the readability and easiness to use of the xdp1 sample.

Quoted 7 lines

Applied to bpf-next.

I think that sample code could be more useful if it's wrapped with bash script like selftests/test_xdp* tests do.
At that point it can move to selftests to get 0bot coverage. Would you be interested in doing that?

It would be nice, but I think that the samples have more urgent issues right now.
Many examples doesn't compile on my system (Fedora 29, GCC 8.2.1, Clang 7.0.0), these are the errors that I encounter:

HOSTCC /home/matteo/src/kernel/linux/samples/bpf/test_lru_dist
/home/matteo/src/kernel/linux/samples/bpf/test_lru_dist.c:39:8: error:
redefinition of ‘struct list_head’

 struct list_head {
        ^~~~~~~~~

HOSTCC /home/matteo/src/kernel/linux/samples/bpf/sock_example
In file included from /home/matteo/src/kernel/linux/samples/bpf/sock_example.c:27:
/usr/include/linux/ip.h:102:2: error: unknown type name ‘__sum16’
__sum16 check;
^~~~~~~

HOSTCC /home/matteo/src/kernel/linux/samples/bpf/tracex5_user.o
/home/matteo/src/kernel/linux/samples/bpf/tracex5_user.c: In function ‘install_accept_all_seccomp’:
/home/matteo/src/kernel/linux/samples/bpf/tracex5_user.c:17:21: error:
array type has incomplete element type ‘struct sock_filter’

  struct sock_filter filter[] = {
                     ^~~~~~

HOSTCC /home/matteo/src/kernel/linux/samples/bpf/test_cgrp2_attach2.o
/home/matteo/src/kernel/linux/samples/bpf/test_cgrp2_attach2.c: In function ‘prog_load_cnt’:
/home/matteo/src/kernel/linux/samples/bpf/test_cgrp2_attach2.c:229:3:
error: ‘BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE’ undeclared (first use in this function); did you mean ‘BPF_MAP_TYPE_CGROUP_STORAGE’?
BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE, ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

HOSTCC /home/matteo/src/kernel/linux/samples/bpf/xdpsock_user.o
/home/matteo/src/kernel/linux/samples/bpf/xdpsock_user.c:59:15: error:
conflicting types for ‘u64’

 typedef __u64 u64;
               ^~~

To be able to compile the samples, I temporarily removed them from the compilation this way:

samples/bpf/Makefile +0 -5
index be0a961450bc..33d7161f2231 100644
@@ -4,8 +4,6 @@ BPF_SAMPLES_PATH ?= $(abspath $(srctree)/$(src))
 TOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools
 
 # List of programs to build
-hostprogs-y := test_lru_dist
-hostprogs-y += sock_example
 hostprogs-y += fds_example
 hostprogs-y += sockex1
 hostprogs-y += sockex2
@@ -14,7 +12,6 @@ hostprogs-y += tracex1
 hostprogs-y += tracex2
 hostprogs-y += tracex3
 hostprogs-y += tracex4
-hostprogs-y += tracex5
 hostprogs-y += tracex6
 hostprogs-y += tracex7
 hostprogs-y += test_probe_write_user
@@ -26,7 +23,6 @@ hostprogs-y += map_perf_test
 hostprogs-y += test_overhead
 hostprogs-y += test_cgrp2_array_pin
 hostprogs-y += test_cgrp2_attach
-hostprogs-y += test_cgrp2_attach2
 hostprogs-y += test_cgrp2_sock
 hostprogs-y += test_cgrp2_sock2
 hostprogs-y += xdp1
@@ -49,7 +45,6 @@ hostprogs-y += xdp_rxq_info
 hostprogs-y += syscall_tp
 hostprogs-y += cpustat
 hostprogs-y += xdp_adjust_tail
-hostprogs-y += xdpsock
 hostprogs-y += xdp_fwd
 hostprogs-y += task_fd_query
 hostprogs-y += xdp_sample_pkts

Regards,

Signature · 2 lines
Matteo Croce
per aspera ad upstream
#5 lore raw
Quoted 19 lines · Matteo Croce
Quoted 5 lines · Alexei Starovoitov
Quoted 2 lines · Matteo Croce

Small improvements to improve the readability and easiness to use of the xdp1 sample.

Quoted 7 lines

Applied to bpf-next.

I think that sample code could be more useful if it's wrapped with bash script like selftests/test_xdp* tests do.
At that point it can move to selftests to get 0bot coverage. Would you be interested in doing that?

It would be nice, but I think that the samples have more urgent issues right now.
Many examples doesn't compile on my system (Fedora 29, GCC 8.2.1, Clang 7.0.0), these are the errors that I encounter:

Quoted 5 lines

HOSTCC /home/matteo/src/kernel/linux/samples/bpf/test_lru_dist
/home/matteo/src/kernel/linux/samples/bpf/test_lru_dist.c:39:8: error:
redefinition of ‘struct list_head’

 struct list_head {
        ^~~~~~~~~

I wonder if there is a way to catch that, and print "run make headers_install, please" rather than confuse newcomers. Hm..

Like this?

samples/bpf/Makefile +9 -0
index 35444f4a846b..b1fdea806d4d 100644
@@ -205,6 +205,15 @@ HOSTCC = $(CROSS_COMPILE)gcc
 CLANG_ARCH_ARGS = -target $(ARCH)
 endif
 
+HDR_PROBE := $(shell echo "\#include <linux/types.h>\n struct list_head { int a; }; int main() { return 0; }" | \
+       gcc $(KBUILD_HOSTCFLAGS) -x c - -o /dev/null 2>/dev/null && \
+       echo okay)
+
+ifeq ($(HDR_PROBE),)
+$(warning Detected possible issues with include path.)
+$(warning Please install kernel headers locally (make headers_install))
+endif
+
 BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris)
 BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF)
 BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm')