Skip to contentSkip to conversation

Improve XDP samples usability and output

PATCH Superseded Kumar Kartikeya Dwivedi 3 participants 13 messages Jul 21, 2021 – Jul 23, 2021

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

v2 cover Improve XDP samples usability and output

This set revamps XDP samples related to redirection to show better output and implement missing features consolidating all their differences and giving them a consistent look and feel, by implementing common features and command line options. Some of the TODO items like reporting redirect error numbers (ENETDOWN, EINVAL, ENOSPC, etc.) have also been implemented.

Some of the features are:
* Received packet statistics
* xdp_redirect/xdp_redirect_map tracepoint statistics
* xdp_redirect_err/xdp_redirect_map_err tracepoint statistics (with support for showing exact errno)
* xdp_cpumap_enqueue/xdp_cpumap_kthread tracepoint statistics
* xdp_devmap_xmit tracepoint statistics
* xdp_exception tracepoint statistics
* Per ifindex pair devmap_xmit stats shown dynamically (for xdp_monitor) to decompose the total.
* Use of BPF skeleton and BPF static linking to share BPF programs.
* Use of vmlinux.h and tp_btf for raw_tracepoint support.
* Removal of redundant -N/--native-mode option (enforced by default now)
* ... and massive cleanups all over the place.

All tracepoints also use raw_tp now, and tracepoints like xdp_redirect are only enabled when requested explicitly to capture successful redirection statistics.

The set of programs converted as part of this series are:
* xdp_redirect_cpu
* xdp_redirect_map_multi
* xdp_redirect_map
* xdp_redirect
* xdp_monitor

Explanation of the output:

There is now a concise output mode by default that shows primarily four fields:

  rx/s        Number of packets received per second
  redir/s     Number of packets successfully redirected per second
  err,drop/s  Aggregated count of errors per second (including dropped packets)
  xmit/s      Number of packets transmitted on the output device per second

Some examples:
; sudo ./xdp_redirect_map veth0 veth1 -s
Redirecting from veth0 (ifindex 15; driver veth) to veth1 (ifindex 14; driver veth)
veth0->veth1 0 rx/s 0 redir/s 0 err,drop/s 0 xmit/s
veth0->veth1 9,998,660 rx/s 9,998,658 redir/s 0 err,drop/s 9,998,654 xmit/s
...

There is also a verbose mode, that can also be enabled by default using -v (--verbose). The output mode can be switched dynamically at runtime using Ctrl + \ (SIGQUIT).

To make the concise output more useful, the errors that occur are expanded inline (as if verbose mode was enabled) to let the user pin down the source of the problem without having to clutter output (or possibly miss it) or always use verbose mode.

For instance, let's consider a case where the output device link state is set to down while redirection is happening:

[...]

veth0->veth1           24,503,376 rx/s                  0 err,drop/s      24,503,372 xmit/s
veth0->veth1           25,044,775 rx/s                  0 err,drop/s      25,044,783 xmit/s
veth0->veth1           25,263,046 rx/s                  4 err,drop/s      25,263,028 xmit/s

redirect_err 4 error/s
ENETDOWN 4 error/s
[...]

The same holds for xdp_exception actions.

An example of how a complete xdp_redirect_map session would look:

; sudo ./xdp_redirect_map veth0 veth1 -s
Redirecting from veth0 (ifindex 5; driver veth) to veth1 (ifindex 4; driver veth)

veth0->veth1           10,557,813 rx/s         10,557,810 redir/s               0 err,drop/s   10,557,810 xmit/s
veth0->veth1           11,556,537 rx/s         11,556,536 redir/s               0 err,drop/s   11,556,535 xmit/s
^\
veth0->veth1           11,565,356 rx/s         11,565,358 redir/s               0 err,drop/s   11,565,367 xmit/s
  receive total        11,565,356 pkt/s                 0 drop/s                0 error/s
    cpu:0              11,565,356 pkt/s                 0 drop/s                0 error/s
  redirect total       11,565,358 redir/s
    cpu:6              11,565,358 redir/s
  redirect_err                  0 error/s
  xdp_exception                 0 hit/s
  devmap_xmit total    11,565,367 xmit/s                0 drop/s                0 drv_err/s          2.00 bulk_avg
     cpu:6             11,565,367 xmit/s                0 drop/s                0 drv_err/s          2.00 bulk_avg
veth0->veth1           11,554,701 rx/s         11,554,702 redir/s               0 err,drop/s   11,554,701 xmit/s
  receive total        11,554,701 pkt/s                 0 drop/s                0 error/s
    cpu:0              11,554,701 pkt/s                 0 drop/s                0 error/s
  redirect total       11,554,702 redir/s
    cpu:6              11,554,702 redir/s
  redirect_err                  0 error/s
  xdp_exception                 0 hit/s
  devmap_xmit total    11,554,701 xmit/s                0 drop/s                0 drv_err/s          2.00 bulk_avg
     cpu:6             11,554,701 xmit/s                0 drop/s                0 drv_err/s          2.00 bulk_avg

^C
Totals

  Packets received    : 45,234,407
  Average packets/s   : 5,654,301
  Packets redirected  : 45,234,406
  Average redir/s     : 5,654,301
  Rx dropped          : 0
  Tx dropped          : 0
  Errors recorded     : 0

Packets transmitted : 45,234,413
Average transmit/s : 5,654,302

The xdp_redirect tracepoint (for success stats) needs to be enabled explicitly using --stats/-s.

Changelog:
----------
RFC (v1) -> v2
RFC (v1): https://lore.kernel.org/bpf/[email protected]

* Address all feedback from Andrii
* Use BPF static linking
* Use vmlinux.h
* Use BPF_PROG macro
* Use global variables instead of maps
* Use of tp_btf for raw_tracepoint progs
* Switch to timerfd for polling
* Use libbpf hashmap for maintaing device sets for per ifindex pair devmap_xmit stats
* Fix Makefile to specify object dependencies properly
* Use in-tree bpftool
* ... misc fixes and cleanups all over the place

Kumar Kartikeya Dwivedi (8):
samples: bpf: fix a couple of warnings
samples: bpf: Add common infrastructure for XDP samples
samples: bpf: Add BPF support for XDP samples helper
samples: bpf: Convert xdp_monitor to use XDP samples helper
samples: bpf: Convert xdp_redirect to use XDP samples helper
samples: bpf: Convert xdp_redirect_map to use XDP samples helpers
samples: bpf: Convert xdp_redirect_map_multi to use XDP samples
helpers
samples: bpf: Convert xdp_redirect_cpu to use XDP samples helpers

 samples/bpf/Makefile                          |  113 +-
 samples/bpf/cookie_uid_helper_example.c       |   12 +-
 samples/bpf/tracex4_user.c                    |    2 +-
 samples/bpf/xdp_monitor.bpf.c                 |    8 +
 samples/bpf/xdp_monitor_kern.c                |  257 ---
 samples/bpf/xdp_monitor_user.c                |  768 +--------
 samples/bpf/xdp_redirect.bpf.c                |   52 +
 samples/bpf/xdp_redirect_cpu.bpf.c            |  561 +++++++
 samples/bpf/xdp_redirect_cpu_kern.c           |  730 ---------
 samples/bpf/xdp_redirect_cpu_user.c           |  916 ++---------
 samples/bpf/xdp_redirect_kern.c               |   90 --
 ...rect_map_kern.c => xdp_redirect_map.bpf.c} |   75 +-
 ...ti_kern.c => xdp_redirect_map_multi.bpf.c} |   40 +-
 samples/bpf/xdp_redirect_map_multi_user.c     |  316 ++--
 samples/bpf/xdp_redirect_map_user.c           |  385 ++---
 samples/bpf/xdp_redirect_user.c               |  262 ++--
 samples/bpf/xdp_sample.bpf.c                  |  215 +++
 samples/bpf/xdp_sample.bpf.h                  |   57 +
 samples/bpf/xdp_sample_shared.h               |   53 +
 samples/bpf/xdp_sample_user.c                 | 1380 +++++++++++++++++
 samples/bpf/xdp_sample_user.h                 |  202 +++
 21 files changed, 3277 insertions(+), 3217 deletions(-)
 create mode 100644 samples/bpf/xdp_monitor.bpf.c
 delete mode 100644 samples/bpf/xdp_monitor_kern.c
 create mode 100644 samples/bpf/xdp_redirect.bpf.c
 create mode 100644 samples/bpf/xdp_redirect_cpu.bpf.c
 delete mode 100644 samples/bpf/xdp_redirect_cpu_kern.c
 delete mode 100644 samples/bpf/xdp_redirect_kern.c
 rename samples/bpf/{xdp_redirect_map_kern.c => xdp_redirect_map.bpf.c} (62%)
 rename samples/bpf/{xdp_redirect_map_multi_kern.c => xdp_redirect_map_multi.bpf.c} (74%)
 create mode 100644 samples/bpf/xdp_sample.bpf.c
 create mode 100644 samples/bpf/xdp_sample.bpf.h
 create mode 100644 samples/bpf/xdp_sample_shared.h
 create mode 100644 samples/bpf/xdp_sample_user.c
 create mode 100644 samples/bpf/xdp_sample_user.h
Signature · 1 line
2.32.0
author maintainer
#1 lore raw

PATCH v2 1/8 samples: bpf: fix a couple of warnings

cookie_uid_helper_example.c: In function ‘main’:
cookie_uid_helper_example.c:178:69: warning: ‘ -j ACCEPT’ directive
writing 10 bytes into a region of size between 8 and 58 [-Wformat-overflow=]
178 | sprintf(rules, "iptables -A OUTPUT -m bpf --object-pinned %s -j ACCEPT",

      |								       ^~~~~~~~~~
/home/kkd/src/linux/samples/bpf/cookie_uid_helper_example.c:178:9: note:
	‘sprintf’ output between 53 and 103 bytes into a destination of size 100
  178 |  sprintf(rules, "iptables -A OUTPUT -m bpf --object-pinned %s -j ACCEPT",
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  179 |         file);
      |         ~~~~~

Fix by using snprintf and a sufficiently sized buffer.

tracex4_user.c:35:15: warning: ‘write’ reading 12 bytes from a region of
size 11 [-Wstringop-overread]
35 | key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */

      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

Use size as 11.

---
 samples/bpf/cookie_uid_helper_example.c | 12 +++++++++---
 samples/bpf/tracex4_user.c              |  2 +-
 2 files changed, 10 insertions(+), 4 deletions(-)
samples/bpf/cookie_uid_helper_example.c +9 -3
index cc3bce8d3aac..30fdcd664da2 100644
@@ -1,3 +1,4 @@
+
 /* This test is a demo of using get_socket_uid and get_socket_cookie
  * helper function to do per socket based network traffic monitoring.
  * It requires iptables version higher then 1.6.1. to load pinned eBPF
@@ -167,7 +168,7 @@ static void prog_load(void)
 static void prog_attach_iptables(char *file)
 {
 	int ret;
-	char rules[100];
+	char rules[256];
 
 	if (bpf_obj_pin(prog_fd, file))
 		error(1, errno, "bpf_obj_pin");
@@ -175,8 +176,13 @@ static void prog_attach_iptables(char *file)
 		printf("file path too long: %s\n", file);
 		exit(1);
 	}
-	sprintf(rules, "iptables -A OUTPUT -m bpf --object-pinned %s -j ACCEPT",
-		file);
+	ret = snprintf(rules, sizeof(rules),
+		       "iptables -A OUTPUT -m bpf --object-pinned %s -j ACCEPT",
+		       file);
+	if (ret < 0 || ret >= sizeof(rules)) {
+		printf("error constructing iptables command\n");
+		exit(1);
+	}
 	ret = system(rules);
 	if (ret < 0) {
 		printf("iptables rule update failed: %d/n", WEXITSTATUS(ret));
samples/bpf/tracex4_user.c +1 -1
index cea399424bca..566e6440e8c2 100644
@@ -32,7 +32,7 @@ static void print_old_objects(int fd)
 	__u64 key, next_key;
 	struct pair v;
 
-	key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
+	key = write(1, "\e[1;1H\e[2J", 11); /* clear screen */
 
 	key = -1;
 	while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
Signature · 1 line
2.32.0
author maintainer
#2 lore raw

PATCH v2 2/8 samples: bpf: Add common infrastructure for XDP samples

In reply to Kumar Kartikeya Dwivedi

This file implements some common helpers to consolidate differences in features and functionality between the various XDP samples and give them a consistent look, feel, and reporting capabilities.

Some of the key features are:
* A concise output format accompanied by helpful text explaining its fields.
* An elaborate output format building upon the concise one, and folding out details in case of errors and staying out of view otherwise.
* Extended reporting of redirect errors by capturing hits for each errno and displaying them inline (ENETDOWN, EINVAL, ENOSPC, etc.) to aid debugging.
* Reporting of each xdp_exception action for all samples that use these helpers (XDP_ABORTED, etc.) to aid debugging.
* Capturing per ifindex pair devmap_xmit counts for decomposing the total TX count per devmap redirection.
* Ability to jump to source locations invoking tracepoints.
* Faster retrieval of stats per polling interval using mmap'd eBPF array map (through .bss).
* Printing driver names for devices redirecting packets.
* Printing summarized total statistics for the entire session.
* Ability to dynamically switch between concise and verbose mode, using SIGQUIT (Ctrl + \).

The goal is sharing these helpers that most of the XDP samples implement in some form but differently for each, lacking in some respect compared to one another.

---
 samples/bpf/Makefile            |    6 +-
 samples/bpf/xdp_sample_shared.h |   53 ++
 samples/bpf/xdp_sample_user.c   | 1380 +++++++++++++++++++++++++++++++
 samples/bpf/xdp_sample_user.h   |  202 +++++
 4 files changed, 1640 insertions(+), 1 deletion(-)
 create mode 100644 samples/bpf/xdp_sample_shared.h
 create mode 100644 samples/bpf/xdp_sample_user.c
 create mode 100644 samples/bpf/xdp_sample_user.h
samples/bpf/Makefile +5 -1
index 036998d11ded..57ccff5ccac4 100644
@@ -62,6 +62,7 @@ LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a
 
 CGROUP_HELPERS := ../../tools/testing/selftests/bpf/cgroup_helpers.o
 TRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o
+XDP_SAMPLE := xdp_sample_user.o
 
 fds_example-objs := fds_example.o
 sockex1-objs := sockex1_user.o
@@ -116,6 +117,8 @@ xdp_sample_pkts-objs := xdp_sample_pkts_user.o
 ibumad-objs := ibumad_user.o
 hbm-objs := hbm.o $(CGROUP_HELPERS)
 
+xdp_sample_user-objs := xdp_sample_user.o $(LIBBPFDIR)/hashmap.o
+
 # Tell kbuild to always build the programs
 always-y := $(tprogs-y)
 always-y += sockex1_kern.o
@@ -201,6 +204,7 @@ TPROGS_CFLAGS += -Wstrict-prototypes
 TPROGS_CFLAGS += -I$(objtree)/usr/include
 TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
 TPROGS_CFLAGS += -I$(srctree)/tools/lib/
+TPROGS_CFLAGS += -I$(srctree)/tools/lib/bpf
 TPROGS_CFLAGS += -I$(srctree)/tools/include
 TPROGS_CFLAGS += -I$(srctree)/tools/perf
 TPROGS_CFLAGS += -DHAVE_ATTR_TEST=0
@@ -210,7 +214,7 @@ TPROGS_CFLAGS += --sysroot=$(SYSROOT)
 TPROGS_LDFLAGS := -L$(SYSROOT)/usr/lib
 endif
 
-TPROGS_LDLIBS			+= $(LIBBPF) -lelf -lz
+TPROGS_LDLIBS			+= $(LIBBPF) -lelf -lz -lm
 TPROGLDLIBS_tracex4		+= -lrt
 TPROGLDLIBS_trace_output	+= -lrt
 TPROGLDLIBS_map_perf_test	+= -lrt
samples/bpf/xdp_sample_shared.h new file +53 -0

53 lines not on this page · raw message

samples/bpf/xdp_sample_user.c new file +1380 -0

1,380 lines not on this page · raw message

samples/bpf/xdp_sample_user.h new file +202 -0

202 lines not on this page · raw message

Signature · 1 line
2.32.0
#3 lore raw

Re: [PATCH bpf-next v2 2/8] samples: bpf: Add common infrastructure for XDP samples

Quoted 53 lines · Kumar Kartikeya Dwivedi

This file implements some common helpers to consolidate differences in features and functionality between the various XDP samples and give them a consistent look, feel, and reporting capabilities.

Some of the key features are:
* A concise output format accompanied by helpful text explaining its fields.
* An elaborate output format building upon the concise one, and folding out details in case of errors and staying out of view otherwise.
* Extended reporting of redirect errors by capturing hits for each errno and displaying them inline (ENETDOWN, EINVAL, ENOSPC, etc.) to aid debugging.
* Reporting of each xdp_exception action for all samples that use these helpers (XDP_ABORTED, etc.) to aid debugging.
* Capturing per ifindex pair devmap_xmit counts for decomposing the total TX count per devmap redirection.
* Ability to jump to source locations invoking tracepoints.
* Faster retrieval of stats per polling interval using mmap'd eBPF array map (through .bss).
* Printing driver names for devices redirecting packets.
* Printing summarized total statistics for the entire session.
* Ability to dynamically switch between concise and verbose mode, using SIGQUIT (Ctrl + \).

The goal is sharing these helpers that most of the XDP samples implement in some form but differently for each, lacking in some respect compared to one another.

---
 samples/bpf/Makefile            |    6 +-
 samples/bpf/xdp_sample_shared.h |   53 ++
 samples/bpf/xdp_sample_user.c   | 1380 +++++++++++++++++++++++++++++++
 samples/bpf/xdp_sample_user.h   |  202 +++++
 4 files changed, 1640 insertions(+), 1 deletion(-)
 create mode 100644 samples/bpf/xdp_sample_shared.h
 create mode 100644 samples/bpf/xdp_sample_user.c
 create mode 100644 samples/bpf/xdp_sample_user.h
samples/bpf/Makefile +1 -0
index 036998d11ded..57ccff5ccac4 100644
@@ -62,6 +62,7 @@ LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a
 
 CGROUP_HELPERS := ../../tools/testing/selftests/bpf/cgroup_helpers.o
 TRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o
+XDP_SAMPLE := xdp_sample_user.o
 
 fds_example-objs := fds_example.o
 sockex1-objs := sockex1_user.o

samples/bpf/Makefile xdp_sample_pkts-objs := xdp_sample_pkts_user.o · line 121

Quoted 5 lines
samples/bpf/Makefile +2 -0
@@ -116,6 +117,8 @@ xdp_sample_pkts-objs := xdp_sample_pkts_user.o
 ibumad-objs := ibumad_user.o
 hbm-objs := hbm.o $(CGROUP_HELPERS)
 
+xdp_sample_user-objs := xdp_sample_user.o $(LIBBPFDIR)/hashmap.o
+

nope, hashmap is an internal libbpf API, no using it from samples, please

Quoted 30 lines
samples/bpf/Makefile +2 -1
 # Tell kbuild to always build the programs
 always-y := $(tprogs-y)
 always-y += sockex1_kern.o
@@ -201,6 +204,7 @@ TPROGS_CFLAGS += -Wstrict-prototypes
 TPROGS_CFLAGS += -I$(objtree)/usr/include
 TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
 TPROGS_CFLAGS += -I$(srctree)/tools/lib/
+TPROGS_CFLAGS += -I$(srctree)/tools/lib/bpf
 TPROGS_CFLAGS += -I$(srctree)/tools/include
 TPROGS_CFLAGS += -I$(srctree)/tools/perf
 TPROGS_CFLAGS += -DHAVE_ATTR_TEST=0
@@ -210,7 +214,7 @@ TPROGS_CFLAGS += --sysroot=$(SYSROOT)
 TPROGS_LDFLAGS := -L$(SYSROOT)/usr/lib
 endif
 
-TPROGS_LDLIBS                  += $(LIBBPF) -lelf -lz
+TPROGS_LDLIBS                  += $(LIBBPF) -lelf -lz -lm
 TPROGLDLIBS_tracex4            += -lrt
 TPROGLDLIBS_trace_output       += -lrt
 TPROGLDLIBS_map_perf_test      += -lrt
samples/bpf/xdp_sample_shared.h new file +4 -0
new file mode 100644
index 000000000000..b211dca233d9
@@ -0,0 +1,53 @@
+#ifndef _XDP_SAMPLE_SHARED_H
+#define _XDP_SAMPLE_SHARED_H
+
+/*

samples/bpf/xdp_sample_shared.h line 14

Quoted 10 lines
samples/bpf/xdp_sample_shared.h new file +10 -0
+ * Best-effort relaxed load/store
+ * __atomic_load_n/__atomic_store_n built-in is not supported for BPF target
+ */
+#define ATOMIC_LOAD(var) ({ (*(volatile typeof(var) *)&(var)); })
+#define ATOMIC_STORE(var, val) ({ *((volatile typeof(var) *)&(var)) = (val); })
+/* This does a load + store instead of the expensive atomic fetch add, but store
+ * is still atomic so that userspace reading the value reads the old or the new
+ * one, but not a partial store.
+ */
+#define ATOMIC_ADD_NORMW(var, val)                                             \

it's rather some sort of NO_TEAR_ADD, but definitely not an atomic add? Wouldn't this ATOMIC_ADD name be too misleading?

Quoted 6 lines
samples/bpf/xdp_sample_shared.h new file +6 -0
+       ({                                                                     \
+               typeof(val) __val = (val);                                     \
+               if (__val)                                                     \
+                       ATOMIC_STORE((var), (var) + __val);                    \
+       })
+

samples/bpf/xdp_sample_shared.h line 30

Quoted 10 lines
samples/bpf/xdp_sample_shared.h new file +10 -0
+#define ATOMIC_INC_NORMW(var) ATOMIC_ADD_NORMW((var), 1)
+
+#define MAX_CPUS 64
+
+/* Values being read/stored must be word sized */
+#if __LP64__
+typedef __u64 datarec_t;
+#else
+typedef __u32 datarec_t;
+#endif

why not use size_t instead of typedefs?

Quoted 13 lines
samples/bpf/xdp_sample_shared.h new file +13 -0
+
+struct datarec {
+       datarec_t processed;
+       datarec_t dropped;
+       datarec_t issue;
+       union {
+               datarec_t xdp_pass;
+               datarec_t info;
+       };
+       datarec_t xdp_drop;
+       datarec_t xdp_redirect;
+} __attribute__((aligned(64)));
+

[...]

samples/bpf/xdp_sample_shared.h

Quoted 6 lines
samples/bpf/xdp_sample_shared.h new file +6 -0
+#define __attach_tp(name)                                                      \
+       ({                                                                     \
+               if (!bpf_program__is_tracing(skel->progs.name))                \
+                       return -EINVAL;                                        \
+               skel->links.name = bpf_program__attach(skel->progs.name);      \
+               if (!skel->links.name)                                         \

you need to turn on libbpf 1.0 mode for errors to use NULL check, see libbpf_set_strict_mode(); otherwise need to use libbpf_get_error() to check

Quoted 3 lines
samples/bpf/xdp_sample_shared.h new file +3 -0
+                       return -errno;                                         \
+       })
+

[...]

author maintainer
#4 lore raw

PATCH v2 3/8 samples: bpf: Add BPF support for XDP samples helper

In reply to Kumar Kartikeya Dwivedi

These eBPF tracepoint programs export data that is consumed by the helpers added in the previous commit.

Also add support int the Makefile to generate a vmlinux.h header that would be used by other bpf.c files in later commits.

---
 samples/bpf/Makefile         |  25 ++++
 samples/bpf/xdp_sample.bpf.c | 215 +++++++++++++++++++++++++++++++++++
 samples/bpf/xdp_sample.bpf.h |  57 ++++++++++
 3 files changed, 297 insertions(+)
 create mode 100644 samples/bpf/xdp_sample.bpf.c
 create mode 100644 samples/bpf/xdp_sample.bpf.h
samples/bpf/Makefile +25 -0
index 57ccff5ccac4..52ac1c6b56e2 100644
@@ -280,6 +280,11 @@ $(LIBBPF): FORCE
 	$(MAKE) -C $(dir $@) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \
 		LDFLAGS=$(TPROGS_LDFLAGS) srctree=$(BPF_SAMPLES_PATH)/../../ O=
 
+BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
+BPFTOOL := $(BPFTOOLDIR)/bpftool
+$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)
+	    $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../
+
 $(obj)/syscall_nrs.h:	$(obj)/syscall_nrs.s FORCE
 	$(call filechk,offsets,__SYSCALL_NRS_H__)
 
@@ -317,6 +322,26 @@ $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
 
 -include $(BPF_SAMPLES_PATH)/Makefile.target
 
+VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
+		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
+		     ../../../../vmlinux				\
+		     /sys/kernel/btf/vmlinux				\
+		     /boot/vmlinux-$(shell uname -r)
+VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
+
+ifeq ($(VMLINUX_BTF),)
+$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
+endif
+
+$(obj)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL)
+ifeq ($(VMLINUX_H),)
+	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
+else
+	$(Q)cp "$(VMLINUX_H)" $@
+endif
+
+clean-files += vmlinux.h
+
 # asm/sysreg.h - inline assembly used by it is incompatible with llvm.
 # But, there is no easy way to fix it, so just exclude it since it is
 # useless for BPF samples.
samples/bpf/xdp_sample.bpf.c new file +215 -0

215 lines not on this page · raw message

samples/bpf/xdp_sample.bpf.h new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.32.0
#5 lore raw

Re: [PATCH bpf-next v2 3/8] samples: bpf: Add BPF support for XDP samples helper

Quoted 16 lines · Kumar Kartikeya Dwivedi

These eBPF tracepoint programs export data that is consumed by the helpers added in the previous commit.

Also add support int the Makefile to generate a vmlinux.h header that would be used by other bpf.c files in later commits.

---
 samples/bpf/Makefile         |  25 ++++
 samples/bpf/xdp_sample.bpf.c | 215 +++++++++++++++++++++++++++++++++++
 samples/bpf/xdp_sample.bpf.h |  57 ++++++++++
 3 files changed, 297 insertions(+)
 create mode 100644 samples/bpf/xdp_sample.bpf.c
 create mode 100644 samples/bpf/xdp_sample.bpf.h

[...]

samples/bpf/xdp_sample.bpf.c line 147

Quoted 6 lines
samples/bpf/xdp_sample.bpf.c new file +6 -0
+
+SEC("tp_btf/xdp_cpumap_kthread")
+int BPF_PROG(tp_xdp_cpumap_kthread, int map_id, unsigned int processed,
+            unsigned int drops, int sched, struct xdp_cpumap_stats *xdp_stats)
+{
+       // Using xdp_stats directly fails verification

nit: C++ style comment

samples/bpf/xdp_sample.bpf.c line 151

Quoted 4 lines
samples/bpf/xdp_sample.bpf.c new file +4 -0
+       u32 cpu = bpf_get_smp_processor_id();
+       struct datarec *rec;
+
+       if (cpu < MAX_CPUS) {

tip: if you invert condition and exit early, the rest of your logic will be less nested (same in few other places)

Quoted 14 lines
samples/bpf/xdp_sample.bpf.c new file +14 -0
+               rec = &sample_data.cpumap_kthread_cnt[cpu];
+
+               ATOMIC_ADD_NORMW(rec->processed, processed);
+               ATOMIC_ADD_NORMW(rec->dropped, drops);
+               ATOMIC_ADD_NORMW(rec->xdp_pass, xdp_stats->pass);
+               ATOMIC_ADD_NORMW(rec->xdp_drop, xdp_stats->drop);
+               ATOMIC_ADD_NORMW(rec->xdp_redirect, xdp_stats->redirect);
+               /* Count times kthread yielded CPU via schedule call */
+               if (sched)
+                       ATOMIC_INC_NORMW(rec->issue);
+       }
+       return 0;
+}
+

[...]

author maintainer
#6 lore raw

PATCH v2 4/8 samples: bpf: Convert xdp_monitor to use XDP samples helper

In reply to Kumar Kartikeya Dwivedi

This change converts XDP monitor tool to use the XDP samples support introduced in previous changes.

Makefile support is added to produce the BPF skeleton and link xdp_monitor bpf object with xdp_sample bpf object. This is then used to open and load the bpf object and attach the needed tracepoints only.

---
 samples/bpf/Makefile           |  49 ++-
 samples/bpf/xdp_monitor.bpf.c  |   8 +
 samples/bpf/xdp_monitor_kern.c | 257 -----------
 samples/bpf/xdp_monitor_user.c | 768 +++------------------------------
 4 files changed, 109 insertions(+), 973 deletions(-)
 create mode 100644 samples/bpf/xdp_monitor.bpf.c
 delete mode 100644 samples/bpf/xdp_monitor_kern.c
samples/bpf/Makefile +46 -3
index 52ac1c6b56e2..1b4838b2beb0 100644
@@ -43,7 +43,6 @@ tprogs-y += xdp_redirect
 tprogs-y += xdp_redirect_map
 tprogs-y += xdp_redirect_map_multi
 tprogs-y += xdp_redirect_cpu
-tprogs-y += xdp_monitor
 tprogs-y += xdp_rxq_info
 tprogs-y += syscall_tp
 tprogs-y += cpustat
@@ -57,6 +56,8 @@ tprogs-y += xdp_sample_pkts
 tprogs-y += ibumad
 tprogs-y += hbm
 
+tprogs-y += xdp_monitor
+
 # Libbpf dependencies
 LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a
 
@@ -103,7 +104,6 @@ xdp_redirect-objs := xdp_redirect_user.o
 xdp_redirect_map-objs := xdp_redirect_map_user.o
 xdp_redirect_map_multi-objs := xdp_redirect_map_multi_user.o
 xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o
-xdp_monitor-objs := xdp_monitor_user.o
 xdp_rxq_info-objs := xdp_rxq_info_user.o
 syscall_tp-objs := syscall_tp_user.o
 cpustat-objs := cpustat_user.o
@@ -118,6 +118,7 @@ ibumad-objs := ibumad_user.o
 hbm-objs := hbm.o $(CGROUP_HELPERS)
 
 xdp_sample_user-objs := xdp_sample_user.o $(LIBBPFDIR)/hashmap.o
+xdp_monitor-objs := xdp_monitor_user.o $(XDP_SAMPLE)
 
 # Tell kbuild to always build the programs
 always-y := $(tprogs-y)
@@ -167,7 +168,6 @@ always-y += xdp_redirect_kern.o
 always-y += xdp_redirect_map_kern.o
 always-y += xdp_redirect_map_multi_kern.o
 always-y += xdp_redirect_cpu_kern.o
-always-y += xdp_monitor_kern.o
 always-y += xdp_rxq_info_kern.o
 always-y += xdp2skb_meta_kern.o
 always-y += syscall_tp_kern.o
@@ -315,6 +315,8 @@ verify_target_bpf: verify_cmds
 $(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
 $(src)/*.c: verify_target_bpf $(LIBBPF)
 
+$(obj)/xdp_monitor_user.o: $(obj)/xdp_monitor.skel.h
+
 $(obj)/tracex5_kern.o: $(obj)/syscall_nrs.h
 $(obj)/hbm_out_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
 $(obj)/hbm.o: $(src)/hbm.h
@@ -342,6 +344,47 @@ endif
 
 clean-files += vmlinux.h
 
+# Get Clang's default includes on this system, as opposed to those seen by
+# '-target bpf'. This fixes "missing" files on some architectures/distros,
+# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
+#
+# Use '-idirafter': Don't interfere with include mechanics except where the
+# build would have failed anyways.
+define get_sys_includes
+$(shell $(1) -v -E - </dev/null 2>&1 \
+        | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
+$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
+endef
+
+CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
+
+$(obj)/xdp_monitor.bpf.o: $(obj)/xdp_sample.bpf.o
+
+$(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/xdp_sample_shared.h
+	@echo "  CLANG-BPF " $@
+	$(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(SRCARCH) \
+		-Wno-compare-distinct-pointer-types \
+		-I$(srctree)/samples/bpf -I$(srctree)/tools/include \
+		-I$(srctree)/tools/lib $(CLANG_SYS_INCLUDES) \
+		-c $(filter %.bpf.c,$^) -o $@
+
+LINKED_SKELS := xdp_monitor.skel.h
+clean-files += $(LINKED_SKELS)
+
+xdp_monitor.skel.h-deps := xdp_monitor.bpf.o xdp_sample.bpf.o
+
+LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
+
+BPF_SRCS_LINKED := $(notdir $(wildcard $(src)/*.bpf.c))
+BPF_OBJS_LINKED := $(patsubst %.bpf.c,$(obj)/%.bpf.o, $(BPF_SRCS_LINKED))
+BPF_SKELS_LINKED := $(addprefix $(obj)/,$(LINKED_SKELS))
+
+$(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL)
+	@echo "  BPF GEN-OBJ " $(@:.skel.h=)
+	$(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps))
+	@echo "  BPF GEN-SKEL" $(@:.skel.h=)
+	$(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=)) > $@
+
 # asm/sysreg.h - inline assembly used by it is incompatible with llvm.
 # But, there is no easy way to fix it, so just exclude it since it is
 # useless for BPF samples.
samples/bpf/xdp_monitor.bpf.c new file +8 -0
new file mode 100644
index 000000000000..cfb41e2205f4
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+/*  Copyright(c) 2017-2018 Jesper Dangaard Brouer, Red Hat Inc.
+ *
+ * XDP monitor tool, based on tracepoints
+ */
+#include "xdp_sample.bpf.h"
+
+char _license[] SEC("license") = "GPL";
samples/bpf/xdp_monitor_kern.c deleted +0 -257

257 lines not on this page · raw message

samples/bpf/xdp_monitor_user.c +55 -713

823 lines not on this page · raw message

Signature · 1 line
2.32.0
author maintainer
#7 lore raw

PATCH v2 5/8 samples: bpf: Convert xdp_redirect to use XDP samples helper

In reply to Kumar Kartikeya Dwivedi

This change converts XDP redirect tool to use the XDP samples support introduced in previous changes.

---
 samples/bpf/Makefile            |  10 +-
 samples/bpf/xdp_redirect.bpf.c  |  52 +++++++
 samples/bpf/xdp_redirect_kern.c |  90 -----------
 samples/bpf/xdp_redirect_user.c | 262 +++++++++++++-------------------
 4 files changed, 160 insertions(+), 254 deletions(-)
 create mode 100644 samples/bpf/xdp_redirect.bpf.c
 delete mode 100644 samples/bpf/xdp_redirect_kern.c
samples/bpf/Makefile +6 -4
index 1b4838b2beb0..b94b6dac09ff 100644
@@ -39,7 +39,6 @@ tprogs-y += lwt_len_hist
 tprogs-y += xdp_tx_iptunnel
 tprogs-y += test_map_in_map
 tprogs-y += per_socket_stats_example
-tprogs-y += xdp_redirect
 tprogs-y += xdp_redirect_map
 tprogs-y += xdp_redirect_map_multi
 tprogs-y += xdp_redirect_cpu
@@ -56,6 +55,7 @@ tprogs-y += xdp_sample_pkts
 tprogs-y += ibumad
 tprogs-y += hbm
 
+tprogs-y += xdp_redirect
 tprogs-y += xdp_monitor
 
 # Libbpf dependencies
@@ -100,7 +100,6 @@ lwt_len_hist-objs := lwt_len_hist_user.o
 xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
 test_map_in_map-objs := test_map_in_map_user.o
 per_socket_stats_example-objs := cookie_uid_helper_example.o
-xdp_redirect-objs := xdp_redirect_user.o
 xdp_redirect_map-objs := xdp_redirect_map_user.o
 xdp_redirect_map_multi-objs := xdp_redirect_map_multi_user.o
 xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o
@@ -118,6 +117,7 @@ ibumad-objs := ibumad_user.o
 hbm-objs := hbm.o $(CGROUP_HELPERS)
 
 xdp_sample_user-objs := xdp_sample_user.o $(LIBBPFDIR)/hashmap.o
+xdp_redirect-objs := xdp_redirect_user.o $(XDP_SAMPLE)
 xdp_monitor-objs := xdp_monitor_user.o $(XDP_SAMPLE)
 
 # Tell kbuild to always build the programs
@@ -164,7 +164,6 @@ always-y += tcp_clamp_kern.o
 always-y += tcp_basertt_kern.o
 always-y += tcp_tos_reflect_kern.o
 always-y += tcp_dumpstats_kern.o
-always-y += xdp_redirect_kern.o
 always-y += xdp_redirect_map_kern.o
 always-y += xdp_redirect_map_multi_kern.o
 always-y += xdp_redirect_cpu_kern.o
@@ -315,6 +314,7 @@ verify_target_bpf: verify_cmds
 $(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
 $(src)/*.c: verify_target_bpf $(LIBBPF)
 
+$(obj)/xdp_redirect_user.o: $(obj)/xdp_redirect.skel.h
 $(obj)/xdp_monitor_user.o: $(obj)/xdp_monitor.skel.h
 
 $(obj)/tracex5_kern.o: $(obj)/syscall_nrs.h
@@ -358,6 +358,7 @@ endef
 
 CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
 
+$(obj)/xdp_redirect.bpf.o: $(obj)/xdp_sample.bpf.o
 $(obj)/xdp_monitor.bpf.o: $(obj)/xdp_sample.bpf.o
 
 $(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/xdp_sample_shared.h
@@ -368,9 +369,10 @@ $(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/x
 		-I$(srctree)/tools/lib $(CLANG_SYS_INCLUDES) \
 		-c $(filter %.bpf.c,$^) -o $@
 
-LINKED_SKELS := xdp_monitor.skel.h
+LINKED_SKELS := xdp_redirect.skel.h xdp_monitor.skel.h
 clean-files += $(LINKED_SKELS)
 
+xdp_redirect.skel.h-deps := xdp_redirect.bpf.o xdp_sample.bpf.o
 xdp_monitor.skel.h-deps := xdp_monitor.bpf.o xdp_sample.bpf.o
 
 LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
samples/bpf/xdp_redirect.bpf.c new file +52 -0

52 lines not on this page · raw message

samples/bpf/xdp_redirect_kern.c deleted +0 -90

90 lines not on this page · raw message

samples/bpf/xdp_redirect_user.c +102 -160

303 lines not on this page · raw message

Signature · 1 line
2.32.0
#8 lore raw

Re: [PATCH bpf-next v2 5/8] samples: bpf: Convert xdp_redirect to use XDP samples helper

Quoted 102 lines · Kumar Kartikeya Dwivedi

This change converts XDP redirect tool to use the XDP samples support introduced in previous changes.

---
 samples/bpf/Makefile            |  10 +-
 samples/bpf/xdp_redirect.bpf.c  |  52 +++++++
 samples/bpf/xdp_redirect_kern.c |  90 -----------
 samples/bpf/xdp_redirect_user.c | 262 +++++++++++++-------------------
 4 files changed, 160 insertions(+), 254 deletions(-)
 create mode 100644 samples/bpf/xdp_redirect.bpf.c
 delete mode 100644 samples/bpf/xdp_redirect_kern.c
samples/bpf/Makefile +6 -4
index 1b4838b2beb0..b94b6dac09ff 100644
@@ -39,7 +39,6 @@ tprogs-y += lwt_len_hist
 tprogs-y += xdp_tx_iptunnel
 tprogs-y += test_map_in_map
 tprogs-y += per_socket_stats_example
-tprogs-y += xdp_redirect
 tprogs-y += xdp_redirect_map
 tprogs-y += xdp_redirect_map_multi
 tprogs-y += xdp_redirect_cpu
@@ -56,6 +55,7 @@ tprogs-y += xdp_sample_pkts
 tprogs-y += ibumad
 tprogs-y += hbm
 
+tprogs-y += xdp_redirect
 tprogs-y += xdp_monitor
 
 # Libbpf dependencies
@@ -100,7 +100,6 @@ lwt_len_hist-objs := lwt_len_hist_user.o
 xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
 test_map_in_map-objs := test_map_in_map_user.o
 per_socket_stats_example-objs := cookie_uid_helper_example.o
-xdp_redirect-objs := xdp_redirect_user.o
 xdp_redirect_map-objs := xdp_redirect_map_user.o
 xdp_redirect_map_multi-objs := xdp_redirect_map_multi_user.o
 xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o
@@ -118,6 +117,7 @@ ibumad-objs := ibumad_user.o
 hbm-objs := hbm.o $(CGROUP_HELPERS)
 
 xdp_sample_user-objs := xdp_sample_user.o $(LIBBPFDIR)/hashmap.o
+xdp_redirect-objs := xdp_redirect_user.o $(XDP_SAMPLE)
 xdp_monitor-objs := xdp_monitor_user.o $(XDP_SAMPLE)
 
 # Tell kbuild to always build the programs
@@ -164,7 +164,6 @@ always-y += tcp_clamp_kern.o
 always-y += tcp_basertt_kern.o
 always-y += tcp_tos_reflect_kern.o
 always-y += tcp_dumpstats_kern.o
-always-y += xdp_redirect_kern.o
 always-y += xdp_redirect_map_kern.o
 always-y += xdp_redirect_map_multi_kern.o
 always-y += xdp_redirect_cpu_kern.o
@@ -315,6 +314,7 @@ verify_target_bpf: verify_cmds
 $(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
 $(src)/*.c: verify_target_bpf $(LIBBPF)
 
+$(obj)/xdp_redirect_user.o: $(obj)/xdp_redirect.skel.h
 $(obj)/xdp_monitor_user.o: $(obj)/xdp_monitor.skel.h
 
 $(obj)/tracex5_kern.o: $(obj)/syscall_nrs.h
@@ -358,6 +358,7 @@ endef
 
 CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
 
+$(obj)/xdp_redirect.bpf.o: $(obj)/xdp_sample.bpf.o
 $(obj)/xdp_monitor.bpf.o: $(obj)/xdp_sample.bpf.o
 
 $(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/xdp_sample_shared.h
@@ -368,9 +369,10 @@ $(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/x
                -I$(srctree)/tools/lib $(CLANG_SYS_INCLUDES) \
                -c $(filter %.bpf.c,$^) -o $@
 
-LINKED_SKELS := xdp_monitor.skel.h
+LINKED_SKELS := xdp_redirect.skel.h xdp_monitor.skel.h
 clean-files += $(LINKED_SKELS)
 
+xdp_redirect.skel.h-deps := xdp_redirect.bpf.o xdp_sample.bpf.o
 xdp_monitor.skel.h-deps := xdp_monitor.bpf.o xdp_sample.bpf.o
 
 LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
samples/bpf/xdp_redirect.bpf.c new file +10 -0
new file mode 100644
index 000000000000..f5098812db36
@@ -0,0 +1,52 @@
+/* Copyright (c) 2016 John Fastabend <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.

samples/bpf/xdp_redirect.bpf.c line 20

Quoted 10 lines
samples/bpf/xdp_redirect.bpf.c new file +10 -0
+ */
+#define KBUILD_MODNAME "foo"
+
+#include "vmlinux.h"
+#include "xdp_sample.bpf.h"
+#include "xdp_sample_shared.h"
+
+const volatile int ifindex_out;
+
+SEC("xdp_redirect")

can you please use "canonical" SEC("xdp") everywhere and use function names as unique BPF program identifiers. Section name is the program type specification (plus extra type-specific argument where applicable, like tracepoint name), not a unique identifier.

Quoted 9 lines
samples/bpf/xdp_redirect.bpf.c new file +9 -0
+int xdp_redirect_prog(struct xdp_md *ctx)
+{
+       void *data_end = (void *)(long)ctx->data_end;
+       void *data = (void *)(long)ctx->data;
+       u32 key = bpf_get_smp_processor_id();
+       struct ethhdr *eth = data;
+       struct datarec *rec;
+       u64 nh_off;
+

[...]

author maintainer
#9 lore raw

PATCH v2 6/8 samples: bpf: Convert xdp_redirect_map to use XDP samples helpers

In reply to Kumar Kartikeya Dwivedi

This change converts XDP redirect_map tool to use the XDP samples support introduced in previous changes.

---
 samples/bpf/Makefile                          |  10 +-
 ...rect_map_kern.c => xdp_redirect_map.bpf.c} |  75 +---
 samples/bpf/xdp_redirect_map_user.c           | 385 +++++++-----------
 3 files changed, 181 insertions(+), 289 deletions(-)
 rename samples/bpf/{xdp_redirect_map_kern.c => xdp_redirect_map.bpf.c} (62%)
samples/bpf/Makefile +6 -4
index b94b6dac09ff..2cccb2aa8f2b 100644
@@ -39,7 +39,6 @@ tprogs-y += lwt_len_hist
 tprogs-y += xdp_tx_iptunnel
 tprogs-y += test_map_in_map
 tprogs-y += per_socket_stats_example
-tprogs-y += xdp_redirect_map
 tprogs-y += xdp_redirect_map_multi
 tprogs-y += xdp_redirect_cpu
 tprogs-y += xdp_rxq_info
@@ -55,6 +54,7 @@ tprogs-y += xdp_sample_pkts
 tprogs-y += ibumad
 tprogs-y += hbm
 
+tprogs-y += xdp_redirect_map
 tprogs-y += xdp_redirect
 tprogs-y += xdp_monitor
 
@@ -100,7 +100,6 @@ lwt_len_hist-objs := lwt_len_hist_user.o
 xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
 test_map_in_map-objs := test_map_in_map_user.o
 per_socket_stats_example-objs := cookie_uid_helper_example.o
-xdp_redirect_map-objs := xdp_redirect_map_user.o
 xdp_redirect_map_multi-objs := xdp_redirect_map_multi_user.o
 xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o
 xdp_rxq_info-objs := xdp_rxq_info_user.o
@@ -117,6 +116,7 @@ ibumad-objs := ibumad_user.o
 hbm-objs := hbm.o $(CGROUP_HELPERS)
 
 xdp_sample_user-objs := xdp_sample_user.o $(LIBBPFDIR)/hashmap.o
+xdp_redirect_map-objs := xdp_redirect_map_user.o $(XDP_SAMPLE)
 xdp_redirect-objs := xdp_redirect_user.o $(XDP_SAMPLE)
 xdp_monitor-objs := xdp_monitor_user.o $(XDP_SAMPLE)
 
@@ -164,7 +164,6 @@ always-y += tcp_clamp_kern.o
 always-y += tcp_basertt_kern.o
 always-y += tcp_tos_reflect_kern.o
 always-y += tcp_dumpstats_kern.o
-always-y += xdp_redirect_map_kern.o
 always-y += xdp_redirect_map_multi_kern.o
 always-y += xdp_redirect_cpu_kern.o
 always-y += xdp_rxq_info_kern.o
@@ -314,6 +313,7 @@ verify_target_bpf: verify_cmds
 $(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
 $(src)/*.c: verify_target_bpf $(LIBBPF)
 
+$(obj)/xdp_redirect_map_user.o: $(obj)/xdp_redirect_map.skel.h
 $(obj)/xdp_redirect_user.o: $(obj)/xdp_redirect.skel.h
 $(obj)/xdp_monitor_user.o: $(obj)/xdp_monitor.skel.h
 
@@ -358,6 +358,7 @@ endef
 
 CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
 
+$(obj)/xdp_redirect_map.bpf.o: $(obj)/xdp_sample.bpf.o
 $(obj)/xdp_redirect.bpf.o: $(obj)/xdp_sample.bpf.o
 $(obj)/xdp_monitor.bpf.o: $(obj)/xdp_sample.bpf.o
 
@@ -369,9 +370,10 @@ $(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/x
 		-I$(srctree)/tools/lib $(CLANG_SYS_INCLUDES) \
 		-c $(filter %.bpf.c,$^) -o $@
 
-LINKED_SKELS := xdp_redirect.skel.h xdp_monitor.skel.h
+LINKED_SKELS := xdp_redirect_map.skel.h xdp_redirect.skel.h xdp_monitor.skel.h
 clean-files += $(LINKED_SKELS)
 
+xdp_redirect_map.skel.h-deps := xdp_redirect_map.bpf.o xdp_sample.bpf.o
 xdp_redirect.skel.h-deps := xdp_redirect.bpf.o xdp_sample.bpf.o
 xdp_monitor.skel.h-deps := xdp_monitor.bpf.o xdp_sample.bpf.o
 
samples/bpf/xdp_redirect_map.bpf.c renamed from samples/bpf/xdp_redirect_map_kern.c +19 -56

117 lines not on this page · raw message

samples/bpf/xdp_redirect_map_user.c +156 -229

428 lines not on this page · raw message

Signature · 1 line
2.32.0
author maintainer
#10 lore raw

PATCH v2 7/8 samples: bpf: Convert xdp_redirect_map_multi to use XDP samples helpers

In reply to Kumar Kartikeya Dwivedi

This change converts XDP redirect_map_multi tool to use the XDP samples support introduced in previous changes.

---
 samples/bpf/Makefile                          |  11 +-
 ...ti_kern.c => xdp_redirect_map_multi.bpf.c} |  40 ++-
 samples/bpf/xdp_redirect_map_multi_user.c     | 316 +++++++-----------
 3 files changed, 153 insertions(+), 214 deletions(-)
 rename samples/bpf/{xdp_redirect_map_multi_kern.c => xdp_redirect_map_multi.bpf.c} (74%)
samples/bpf/Makefile +7 -4
index 2cccb2aa8f2b..064f16c12ccc 100644
@@ -39,7 +39,6 @@ tprogs-y += lwt_len_hist
 tprogs-y += xdp_tx_iptunnel
 tprogs-y += test_map_in_map
 tprogs-y += per_socket_stats_example
-tprogs-y += xdp_redirect_map_multi
 tprogs-y += xdp_redirect_cpu
 tprogs-y += xdp_rxq_info
 tprogs-y += syscall_tp
@@ -54,6 +53,7 @@ tprogs-y += xdp_sample_pkts
 tprogs-y += ibumad
 tprogs-y += hbm
 
+tprogs-y += xdp_redirect_map_multi
 tprogs-y += xdp_redirect_map
 tprogs-y += xdp_redirect
 tprogs-y += xdp_monitor
@@ -100,7 +100,6 @@ lwt_len_hist-objs := lwt_len_hist_user.o
 xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
 test_map_in_map-objs := test_map_in_map_user.o
 per_socket_stats_example-objs := cookie_uid_helper_example.o
-xdp_redirect_map_multi-objs := xdp_redirect_map_multi_user.o
 xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o
 xdp_rxq_info-objs := xdp_rxq_info_user.o
 syscall_tp-objs := syscall_tp_user.o
@@ -116,6 +115,7 @@ ibumad-objs := ibumad_user.o
 hbm-objs := hbm.o $(CGROUP_HELPERS)
 
 xdp_sample_user-objs := xdp_sample_user.o $(LIBBPFDIR)/hashmap.o
+xdp_redirect_map_multi-objs := xdp_redirect_map_multi_user.o $(XDP_SAMPLE)
 xdp_redirect_map-objs := xdp_redirect_map_user.o $(XDP_SAMPLE)
 xdp_redirect-objs := xdp_redirect_user.o $(XDP_SAMPLE)
 xdp_monitor-objs := xdp_monitor_user.o $(XDP_SAMPLE)
@@ -164,7 +164,6 @@ always-y += tcp_clamp_kern.o
 always-y += tcp_basertt_kern.o
 always-y += tcp_tos_reflect_kern.o
 always-y += tcp_dumpstats_kern.o
-always-y += xdp_redirect_map_multi_kern.o
 always-y += xdp_redirect_cpu_kern.o
 always-y += xdp_rxq_info_kern.o
 always-y += xdp2skb_meta_kern.o
@@ -313,6 +312,7 @@ verify_target_bpf: verify_cmds
 $(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
 $(src)/*.c: verify_target_bpf $(LIBBPF)
 
+$(obj)/xdp_redirect_map_multi_user.o: $(obj)/xdp_redirect_map_multi.skel.h
 $(obj)/xdp_redirect_map_user.o: $(obj)/xdp_redirect_map.skel.h
 $(obj)/xdp_redirect_user.o: $(obj)/xdp_redirect.skel.h
 $(obj)/xdp_monitor_user.o: $(obj)/xdp_monitor.skel.h
@@ -358,6 +358,7 @@ endef
 
 CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
 
+$(obj)/xdp_redirect_map_multi.bpf.o: $(obj)/xdp_sample.bpf.o
 $(obj)/xdp_redirect_map.bpf.o: $(obj)/xdp_sample.bpf.o
 $(obj)/xdp_redirect.bpf.o: $(obj)/xdp_sample.bpf.o
 $(obj)/xdp_monitor.bpf.o: $(obj)/xdp_sample.bpf.o
@@ -370,9 +371,11 @@ $(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/x
 		-I$(srctree)/tools/lib $(CLANG_SYS_INCLUDES) \
 		-c $(filter %.bpf.c,$^) -o $@
 
-LINKED_SKELS := xdp_redirect_map.skel.h xdp_redirect.skel.h xdp_monitor.skel.h
+LINKED_SKELS := xdp_redirect_map_multi.skel.h xdp_redirect_map.skel.h \
+		xdp_redirect.skel.h xdp_monitor.skel.h
 clean-files += $(LINKED_SKELS)
 
+xdp_redirect_map_multi.skel.h-deps := xdp_redirect_map_multi.bpf.o xdp_sample.bpf.o
 xdp_redirect_map.skel.h-deps := xdp_redirect_map.bpf.o xdp_sample.bpf.o
 xdp_redirect.skel.h-deps := xdp_redirect.bpf.o xdp_sample.bpf.o
 xdp_monitor.skel.h-deps := xdp_monitor.bpf.o xdp_sample.bpf.o
samples/bpf/xdp_redirect_map_multi.bpf.c renamed from samples/bpf/xdp_redirect_map_multi_kern.c +19 -21
similarity index 74%
rename from samples/bpf/xdp_redirect_map_multi_kern.c
rename to samples/bpf/xdp_redirect_map_multi.bpf.c
index 71aa23d1cb2b..66144e3a69f5 100644
@@ -1,11 +1,14 @@
 // SPDX-License-Identifier: GPL-2.0
 #define KBUILD_MODNAME "foo"
-#include <uapi/linux/bpf.h>
-#include <linux/in.h>
-#include <linux/if_ether.h>
-#include <linux/ip.h>
-#include <linux/ipv6.h>
-#include <bpf/bpf_helpers.h>
+
+#include "vmlinux.h"
+#include "xdp_sample.bpf.h"
+#include "xdp_sample_shared.h"
+
+enum {
+	BPF_F_BROADCAST		= (1ULL << 3),
+	BPF_F_EXCLUDE_INGRESS	= (1ULL << 4),
+};
 
 struct {
 	__uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
@@ -21,13 +24,6 @@ struct {
 	__uint(max_entries, 32);
 } forward_map_native SEC(".maps");
 
-struct {
-	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
-	__type(key, u32);
-	__type(value, long);
-	__uint(max_entries, 1);
-} rxcnt SEC(".maps");
-
 /* map to store egress interfaces mac addresses, set the
  * max_entries to 1 and extend it in user sapce prog.
  */
@@ -40,16 +36,18 @@ struct {
 
 static int xdp_redirect_map(struct xdp_md *ctx, void *forward_map)
 {
-	long *value;
-	u32 key = 0;
+	u32 key = bpf_get_smp_processor_id();
+	struct datarec *rec;
 
-	/* count packet in global counter */
-	value = bpf_map_lookup_elem(&rxcnt, &key);
-	if (value)
-		*value += 1;
+	if (key < MAX_CPUS) {
+		rec = &sample_data.rx_cnt[key];
+		ATOMIC_INC_NORMW(rec->processed);
 
-	return bpf_redirect_map(forward_map, key,
-				BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
+		return bpf_redirect_map(forward_map, 0,
+					BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
+	}
+
+	return XDP_PASS;
 }
 
 SEC("xdp_redirect_general")
samples/bpf/xdp_redirect_map_multi_user.c +127 -189

407 lines not on this page · raw message

Signature · 1 line
2.32.0
author maintainer
#11 lore raw

PATCH v2 8/8 samples: bpf: Convert xdp_redirect_cpu to use XDP samples helpers

In reply to Kumar Kartikeya Dwivedi

This change converts XDP redirect_cpu tool to use the XDP samples support introduced in previous changes.

One of the notable changes is removal of options to specify a separate eBPF program that supplies a redirection program that is attached to the CPUMAP, so that the packet can be redirected to a device after redirecting it to a CPU. This is replaced by a program built into the BPF object file and now the user only needs to specify the out interface to do the same.

The program also uses devmap to redirect XDP frames, instead of directly redirecting with bpf_redirect, for performance reasons.

---
 samples/bpf/Makefile                |  12 +-
 samples/bpf/xdp_redirect_cpu.bpf.c  | 561 +++++++++++++++++
 samples/bpf/xdp_redirect_cpu_kern.c | 730 ----------------------
 samples/bpf/xdp_redirect_cpu_user.c | 916 +++++-----------------------
 4 files changed, 732 insertions(+), 1487 deletions(-)
 create mode 100644 samples/bpf/xdp_redirect_cpu.bpf.c
 delete mode 100644 samples/bpf/xdp_redirect_cpu_kern.c
samples/bpf/Makefile +7 -5
index 064f16c12ccc..60d3a5ae6630 100644
@@ -39,7 +39,6 @@ tprogs-y += lwt_len_hist
 tprogs-y += xdp_tx_iptunnel
 tprogs-y += test_map_in_map
 tprogs-y += per_socket_stats_example
-tprogs-y += xdp_redirect_cpu
 tprogs-y += xdp_rxq_info
 tprogs-y += syscall_tp
 tprogs-y += cpustat
@@ -53,6 +52,7 @@ tprogs-y += xdp_sample_pkts
 tprogs-y += ibumad
 tprogs-y += hbm
 
+tprogs-y += xdp_redirect_cpu
 tprogs-y += xdp_redirect_map_multi
 tprogs-y += xdp_redirect_map
 tprogs-y += xdp_redirect
@@ -100,7 +100,6 @@ lwt_len_hist-objs := lwt_len_hist_user.o
 xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
 test_map_in_map-objs := test_map_in_map_user.o
 per_socket_stats_example-objs := cookie_uid_helper_example.o
-xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o
 xdp_rxq_info-objs := xdp_rxq_info_user.o
 syscall_tp-objs := syscall_tp_user.o
 cpustat-objs := cpustat_user.o
@@ -115,6 +114,7 @@ ibumad-objs := ibumad_user.o
 hbm-objs := hbm.o $(CGROUP_HELPERS)
 
 xdp_sample_user-objs := xdp_sample_user.o $(LIBBPFDIR)/hashmap.o
+xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o $(XDP_SAMPLE)
 xdp_redirect_map_multi-objs := xdp_redirect_map_multi_user.o $(XDP_SAMPLE)
 xdp_redirect_map-objs := xdp_redirect_map_user.o $(XDP_SAMPLE)
 xdp_redirect-objs := xdp_redirect_user.o $(XDP_SAMPLE)
@@ -164,7 +164,6 @@ always-y += tcp_clamp_kern.o
 always-y += tcp_basertt_kern.o
 always-y += tcp_tos_reflect_kern.o
 always-y += tcp_dumpstats_kern.o
-always-y += xdp_redirect_cpu_kern.o
 always-y += xdp_rxq_info_kern.o
 always-y += xdp2skb_meta_kern.o
 always-y += syscall_tp_kern.o
@@ -312,6 +311,7 @@ verify_target_bpf: verify_cmds
 $(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
 $(src)/*.c: verify_target_bpf $(LIBBPF)
 
+$(obj)/xdp_redirect_cpu_user.o: $(obj)/xdp_redirect_cpu.skel.h
 $(obj)/xdp_redirect_map_multi_user.o: $(obj)/xdp_redirect_map_multi.skel.h
 $(obj)/xdp_redirect_map_user.o: $(obj)/xdp_redirect_map.skel.h
 $(obj)/xdp_redirect_user.o: $(obj)/xdp_redirect.skel.h
@@ -358,6 +358,7 @@ endef
 
 CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
 
+$(obj)/xdp_redirect_cpu.bpf.o: $(obj)/xdp_sample.bpf.o
 $(obj)/xdp_redirect_map_multi.bpf.o: $(obj)/xdp_sample.bpf.o
 $(obj)/xdp_redirect_map.bpf.o: $(obj)/xdp_sample.bpf.o
 $(obj)/xdp_redirect.bpf.o: $(obj)/xdp_sample.bpf.o
@@ -371,10 +372,11 @@ $(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/x
 		-I$(srctree)/tools/lib $(CLANG_SYS_INCLUDES) \
 		-c $(filter %.bpf.c,$^) -o $@
 
-LINKED_SKELS := xdp_redirect_map_multi.skel.h xdp_redirect_map.skel.h \
-		xdp_redirect.skel.h xdp_monitor.skel.h
+LINKED_SKELS := xdp_redirect_cpu.skel.h xdp_redirect_map_multi.skel.h \
+		xdp_redirect_map.skel.h xdp_redirect.skel.h xdp_monitor.skel.h
 clean-files += $(LINKED_SKELS)
 
+xdp_redirect_cpu.skel.h-deps := xdp_redirect_cpu.bpf.o xdp_sample.bpf.o
 xdp_redirect_map_multi.skel.h-deps := xdp_redirect_map_multi.bpf.o xdp_sample.bpf.o
 xdp_redirect_map.skel.h-deps := xdp_redirect_map.bpf.o xdp_sample.bpf.o
 xdp_redirect.skel.h-deps := xdp_redirect.bpf.o xdp_sample.bpf.o
samples/bpf/xdp_redirect_cpu.bpf.c new file +561 -0

561 lines not on this page · raw message

samples/bpf/xdp_redirect_cpu_kern.c deleted +0 -730

730 lines not on this page · raw message

samples/bpf/xdp_redirect_cpu_user.c +164 -752

1,088 lines not on this page · raw message

Signature · 1 line
2.32.0
#12 lore raw
In reply to Kumar Kartikeya Dwivedi
Quoted 6 lines · Kumar Kartikeya Dwivedi

This set revamps XDP samples related to redirection to show better output and implement missing features consolidating all their differences and giving them a consistent look and feel, by implementing common features and command line options. Some of the TODO items like reporting redirect error numbers (ENETDOWN, EINVAL, ENOSPC, etc.) have also been implemented.

Quoted 14 lines

Some of the features are:
* Received packet statistics
* xdp_redirect/xdp_redirect_map tracepoint statistics
* xdp_redirect_err/xdp_redirect_map_err tracepoint statistics (with support for showing exact errno)
* xdp_cpumap_enqueue/xdp_cpumap_kthread tracepoint statistics
* xdp_devmap_xmit tracepoint statistics
* xdp_exception tracepoint statistics
* Per ifindex pair devmap_xmit stats shown dynamically (for xdp_monitor) to decompose the total.
* Use of BPF skeleton and BPF static linking to share BPF programs.
* Use of vmlinux.h and tp_btf for raw_tracepoint support.
* Removal of redundant -N/--native-mode option (enforced by default now)
* ... and massive cleanups all over the place.

I took this for a quick spin, and it's a great improvement over the status quo; it's not immediately obvious when using xdp_redirect and it doesn't work, and the stats output is awesome!

Today is my last day before going on vacation for three weeks, and unfortunately I won't have time to review the code in detail before then; but wanted to express some encouragement before signing off - please keep at this! :)

-Toke