Improve XDP samples usability and output
Subsystems (4)
Kumar Kartikeya Dwivedi
maintainerv3 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/sveth0->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
Redirecting from veth0 (ifindex 5; driver veth) to veth1 (ifindex 4; driver veth)
veth0->veth1 7,411,506 rx/s 0 err,drop/s 7,411,470 xmit/s
veth0->veth1 8,931,770 rx/s 0 err,drop/s 8,931,771 xmit/s
^\
veth0->veth1 8,787,295 rx/s 0 err,drop/s 8,787,325 xmit/s
receive total 8,787,295 pkt/s 0 drop/s 0 error/s
cpu:7 8,787,295 pkt/s 0 drop/s 0 error/s
redirect_err 0 error/s
xdp_exception 0 hit/s
xmit veth0->veth1 8,787,325 xmit/s 0 drop/s 0 drv_err/s 2.00 bulk-avg
cpu:7 8,787,325 xmit/s 0 drop/s 0 drv_err/s 2.00 bulk-avgveth0->veth1 8,842,610 rx/s 0 err,drop/s 8,842,606 xmit/s
receive total 8,842,610 pkt/s 0 drop/s 0 error/s
cpu:7 8,842,610 pkt/s 0 drop/s 0 error/s
redirect_err 0 error/s
xdp_exception 0 hit/s
xmit veth0->veth1 8,842,606 xmit/s 0 drop/s 0 drv_err/s 2.00 bulk-avg
cpu:7 8,842,606 xmit/s 0 drop/s 0 drv_err/s 2.00 bulk-avg^C
Packets received : 33,973,181
Average packets/s : 4,246,648
Packets transmitted : 33,973,172
Average transmit/s : 4,246,647
The xdp_redirect tracepoint (for success stats) needs to be enabled explicitly using --stats/-s. Documentation for entire output and options is provided when user specifies --help/-h with a sample.
Changelog:
----------
v2 -> v3
v2: https://lore.kernel.org/bpf/[email protected]
* Adress all feedback from Andrii
* Replace usage of libbpf hashmap (internal API) with custom one
* Rename ATOMIC_* macros to NO_TEAR_* to better reflect their use
* Use size_t as a portable word sized data type
* Set libbpf_set_strict_mode
* Invert conditions in BPF programs to exit early and reduce nesting
* Use canonical SEC("xdp") naming for all XDP BPF progams
* Add missing help description for cpumap enqueue and kthread tracepoints
* Move private struct declarations from xdp_sample_user.h to .c file
* Improve help output for cpumap enqueue and cpumap kthread tracepoints
* Fix a bug where keys array for BPF_MAP_LOOKUP_BATCH is overallocated
* Fix some conditions for printing stats (earlier only checked pps, now pps, drop, err and print if any is greater than zero)
* Fix alloc_stats_record to properly return and cleanup allocated memory on allocation failure instead of calling exit(3)
* Bump bpf_map_lookup_batch count to 32 to reduce lookup time with multiple devices in map
* Fix a bug where devmap_xmit_multi stats are not printed when previous record is missing (i.e. when the first time stats are printed), by simply using a dummy record that is zeroed out
* Also print per-CPU counts for devmap_xmit_multi which we collect already
* Change mac_map to be BPF_MAP_TYPE_HASH instead of array to prevent resizing to a large size when max_ifindex is high, in xdp_redirect_map_multi
* Fix instance of strerror(errno) in sample_install_xdp to use saved errno
* Provide a usage function from samples helper
* Provide a fix where incorrect stats are shown for parallel sessions of xdp_redirect_* samples by introducing matching support for input device(s), output device(s) and cpumap map id for enqueue and kthread stats. Only xdp_monitor doesn't filter stats, all others do.
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 | 111 +-
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 | 792 +-------
samples/bpf/xdp_redirect.bpf.c | 51 +
...rect_cpu_kern.c => xdp_redirect_cpu.bpf.c} | 407 +---
samples/bpf/xdp_redirect_cpu_user.c | 992 ++--------
samples/bpf/xdp_redirect_kern.c | 90 -
...rect_map_kern.c => xdp_redirect_map.bpf.c} | 87 +-
...ti_kern.c => xdp_redirect_map_multi.bpf.c} | 51 +-
samples/bpf/xdp_redirect_map_multi_user.c | 338 ++--
samples/bpf/xdp_redirect_map_user.c | 380 ++--
samples/bpf/xdp_redirect_user.c | 265 +--
samples/bpf/xdp_sample.bpf.c | 267 +++
samples/bpf/xdp_sample.bpf.h | 62 +
samples/bpf/xdp_sample_shared.h | 47 +
samples/bpf/xdp_sample_user.c | 1732 +++++++++++++++++
samples/bpf/xdp_sample_user.h | 94 +
20 files changed, 3170 insertions(+), 2875 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
rename samples/bpf/{xdp_redirect_cpu_kern.c => xdp_redirect_cpu.bpf.c} (52%)
delete mode 100644 samples/bpf/xdp_redirect_kern.c
rename samples/bpf/{xdp_redirect_map_kern.c => xdp_redirect_map.bpf.c} (58%)
rename samples/bpf/{xdp_redirect_map_multi_kern.c => xdp_redirect_map_multi.bpf.c} (64%)
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.hSignature · 1 line
2.32.0
Kumar Kartikeya Dwivedi
maintainerPATCH v3 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
@@ -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
@@ -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
Kumar Kartikeya Dwivedi
maintainerPATCH v3 2/8 samples: bpf: Add common infrastructure for XDP samples
In reply to Kumar Kartikeya DwivediThis 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 | 3 +- samples/bpf/xdp_sample_shared.h | 47 + samples/bpf/xdp_sample_user.c | 1732 +++++++++++++++++++++++++++++++ samples/bpf/xdp_sample_user.h | 94 ++ 4 files changed, 1875 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 +2 -1
@@ -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
@@ -210,7 +211,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 +47 -0
47 lines not on this page · raw message
samples/bpf/xdp_sample_user.c new file +1732 -0
1,732 lines not on this page · raw message
samples/bpf/xdp_sample_user.h new file +94 -0
94 lines not on this page · raw message
Signature · 1 line
2.32.0
Daniel Borkmann
maintainerRe: [PATCH bpf-next v3 2/8] samples: bpf: Add common infrastructure for XDP samples
Quoted 22 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 eachxdp_exceptionaction for all samples that use these helpers (XDP_ABORTED, etc.) to aid debugging.
* Capturing per ifindex pairdevmap_xmitcounts 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
Quoted 7 lines
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.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>
Overall I think it's okay to try to streamline the individual XDP tools, but I also tend to wonder whether we keep going beyond the original purpose of kernel samples where the main goal is to provide small, easy to hack & stand-alone code snippets (like in samples/seccomp ... no doubt we have it more complex in BPF land, but still); things people can take away and extend for their purpose. A big portion of the samples are still better off in selftests so they can be run in CI, and those that are not should generally be simplified for developers to rip out, modify, experiment, and build actual applications on top.
Quoted 5 lines
--- samples/bpf/Makefile | 3 +- samples/bpf/xdp_sample_shared.h | 47 + samples/bpf/xdp_sample_user.c | 1732 +++++++++++++++++++++++++++++++ samples/bpf/xdp_sample_user.h | 94 ++
I would have wished that rather than a single patch to get all the boilerplate code in at once to have incremental improvements that refactor the individual sample tools along the way.
Quoted 32 lines
4 files changed, 1875 insertions(+), 1 deletion(-)create mode 100644
samples/bpf/xdp_sample_shared.hcreate mode 100644samples/bpf/xdp_sample_user.ccreate mode 100644samples/bpf/xdp_sample_user.hsamples/bpf/Makefile +2
-1@@ -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@@ -210,7 +211,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 += -lrtsamples/bpf/xdp_sample_shared.h new file +0
-0
samples/bpf/xdp_sample_shared.h line 9
Quoted 9 lines
samples/bpf/xdp_sample_shared.h new file +9
-0@@ -0,0 +1,47 @@ +#ifndef _XDP_SAMPLE_SHARED_H +#define _XDP_SAMPLE_SHARED_H + +/* +* Relaxed load/store +* __atomic_load_n/__atomic_store_n built-in is not supported for BPF target +*/ +#define NO_TEAR_LOAD (var) ({ (*(volatile typeof(var) *)&(var)); }) +#define NO_TEAR_STORE (var, val) ({ *((volatile typeof(var) *)&(var)) = (val); })
Can't we reuse READ_ONCE() / WRITE_ONCE() from tools/include/linux/compiler.h instead of readding the same here again? We already rely on using the tooling include infra anyway for samples.
samples/bpf/xdp_sample_shared.h line 21
Quoted 12 lines
samples/bpf/xdp_sample_shared.h new file +12
-0+/* 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 NO_TEAR_ADD (var, val) \ + ({ \ + typeof(val) __val = (val); \ + if (__val) \ + NO_TEAR_STORE((var), (var) + __val); \ + }) + +#define NO_TEAR_INC (var) NO_TEAR_ADD((var), 1)
This could likely also go there.
samples/bpf/xdp_sample_shared.h line 23
Quoted 1 line
samples/bpf/xdp_sample_shared.h new file +1
-0+#define MAX_CPUS 64
Given this is a heavy rework of samples, can't we also fix up this assumption?
samples/bpf/xdp_sample_shared.h line 24
Quoted 1 line
samples/bpf/xdp_sample_shared.h new file +1
-0+#define ELEMENTS_OF (x) (sizeof(x) / sizeof(*(x)))
See ARRAY_SIZE() under tools/include/linux/kernel.h .
samples/bpf/xdp_sample_shared.h line 35
Quoted 10 lines
samples/bpf/xdp_sample_shared.h new file +10
-0+struct datarec { +size_t processed; +size_t dropped; +size_t issue; +union { +size_t xdp_pass; +size_t info; + }; +size_t xdp_drop; +size_t xdp_redirect;
Why all size_t, can't we just use __u64 for the stats?
Quoted 44 lines
samples/bpf/xdp_sample_shared.h new file +12
-0+} __attribute__((aligned(64))); + +struct sample_data { +struct datarec rx_cnt[MAX_CPUS ]; +struct datarec redirect_err_cnt[7 *MAX_CPUS ]; +struct datarec cpumap_enqueue_cnt[MAX_CPUS *MAX_CPUS ]; +struct datarec cpumap_kthread_cnt[MAX_CPUS ]; +struct datarec exception_cnt[6 *MAX_CPUS ]; +struct datarec devmap_xmit_cnt[MAX_CPUS ]; +}; + +#endif samples/bpf/xdp_sample_user.c new file +26
-0@@ -0,0 +1,1732 @@ +// SPDX-License-Identifier: GPL-2.0-only +#define _GNU_SOURCE + +#include <errno.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <unistd.h> +#include <math.h> +#include <locale.h> +#include <sys/signalfd.h> +#include <sys/resource.h> +#include <sys/sysinfo.h> +#include <sys/timerfd.h> +#include <getopt.h> +#include <net/if.h> +#include <time.h> +#include <linux/limits.h> +#include <sys/ioctl.h> +#include <net/if.h> +#include <poll.h> +#include <linux/ethtool.h> +#include <linux/sockios.h> +#ifndef SIOCETHTOOL
samples/bpf/xdp_sample_user.c line 36
Quoted 10 lines
samples/bpf/xdp_sample_user.c new file +10
-0+#define SIOCETHTOOL 0x8946 +#endif +#include <fcntl.h> +#include <arpa/inet.h> +#include <linux/if_link.h> +#include <sys/utsname.h> +#include <bpf/bpf.h> +#include <bpf/libbpf.h> +#include "bpf_util.h" +#include "xdp_sample_user.h"
nit: Pls order the above a bit better.
Quoted 9 lines
samples/bpf/xdp_sample_user.c new file +9
-0+#define __sample_print (fmt, cond, printer, ...) \ + ({ \ + if (cond) \ + printer(fmt, ##__VA_ARGS__); \ + }) + +#define print_always (fmt, ...) __sample_print(fmt, 1, printf, ##__VA_ARGS__) +#define print_default (fmt, ...) \ + __sample_print(fmt, sample.log_level & LL_DEFAULT, printf, ##__VA_ARGS__)
samples/bpf/xdp_sample_user.c line 56
Quoted 10 lines
samples/bpf/xdp_sample_user.c new file +10
-0+#define __print_err (err, fmt, printer, ...) \ + ({ \ + __sample_print(fmt, err > 0 || sample.log_level & LL_DEFAULT, \ + printer, ##__VA_ARGS__); \ + sample.err_exp = sample.err_exp ? true : err > 0; \ + }) +#define print_err (err, fmt, ...) __print_err(err, fmt, printf, ##__VA_ARGS__) + +#define print_link_err (err, str, width, type) \ + __print_err(err, str, print_link, width, type)
Couldn't this all be more generalized so other non-XDP samples can use it as well potentially?
Quoted 129 lines
samples/bpf/xdp_sample_user.c new file +129
-0+#define __COLUMN (x) "%'10" x " %-13s" +#define FMT_COLUMNf __COLUMN(".0f") +#define FMT_COLUMNd __COLUMN("d") +#define FMT_COLUMNl __COLUMN("llu") +#define RX (rx) rx, "rx/s" +#define PPS (pps) pps, "pkt/s" +#define DROP (drop) drop, "drop/s" +#define ERR (err) err, "error/s" +#define HITS (hits) hits, "hit/s" +#define XMIT (xmit) xmit, "xmit/s" +#define PASS (pass) pass, "pass/s" +#define REDIR (redir) redir, "redir/s" +#define NANOSEC_PER_SEC 1000000000/* 10^9 */ + +#define XDP_UNKNOWN (XDP_REDIRECT + 1) +#define XDP_ACTION_MAX (XDP_UNKNOWN + 1) +#define XDP_REDIRECT_ERR_MAX 7 + +enum tp_type { +TP_REDIRECT_CNT , +TP_REDIRECT_MAP_CNT , +TP_REDIRECT_ERR_CNT , +TP_REDIRECT_MAP_ERR_CNT , +TP_CPUMAP_ENQUEUE_CNT , +TP_CPUMAP_KTHREAD_CNT , +TP_EXCEPTION_CNT , +TP_DEVMAP_XMIT_CNT , +NUM_TP , +}; + +enum map_type { +MAP_DEVMAP_XMIT_MULTI , +NUM_MAP , +}; + +enum log_level { +LL_DEFAULT =1U <<0 , +LL_SIMPLE =1U <<1 , +LL_DEBUG =1U <<2 , +}; + +struct record { +__u64 timestamp; +struct datarec total; +struct datarec *cpu; +}; + +struct stats_record { +struct record rx_cnt; +struct record redir_err[XDP_REDIRECT_ERR_MAX ]; +struct record kthread; +struct record exception[XDP_ACTION_MAX ]; +struct record devmap_xmit; +struct hash_map *devmap_xmit_multi; +struct record enq[]; +}; + +struct sample_output { +struct { +__u64 rx; +__u64 redir; +__u64 drop; +__u64 drop_xmit; +__u64 err; +__u64 xmit; + } totals; +struct { +__u64 pps; +__u64 drop; +__u64 err; + } rx_cnt; +struct { +__u64 suc; +__u64 err; + } redir_cnt; +struct { +__u64 hits; + } except_cnt; +struct { +__u64 pps; +__u64 drop; +__u64 err; +double bavg; + } xmit_cnt; +}; + +static struct { +enum log_level log_level; +struct sample_output out; +struct sample_data *data; +unsigned long interval; +int map_fd[NUM_MAP ]; +struct xdp_desc { +int ifindex; +__u32 prog_id; +int flags; + } xdp_progs[32 ]; +bool err_exp; +int xdp_cnt; +int n_cpus; +int sig_fd; +int mask; +} sample; + +static const char *xdp_redirect_err_names[XDP_REDIRECT_ERR_MAX ] = { +/* Key=1 keeps unknown errors */ +"Success" ,"Unknown" ,"EINVAL" ,"ENETDOWN" ,"EMSGSIZE" , +"EOPNOTSUPP" ,"ENOSPC" , +}; + +/* Keyed from Unknown */ +static const char *xdp_redirect_err_help[XDP_REDIRECT_ERR_MAX -1 ] = { +"Unknown error" , +"Invalid redirection" , +"Device being redirected to is down" , +"Packet length too large for device" , +"Operation not supported" , +"No space in ptr_ring of cpumap kthread" , +}; + +static const char *xdp_action_names[XDP_ACTION_MAX ] = { + [XDP_ABORTED ] ="XDP_ABORTED" , + [XDP_DROP ] ="XDP_DROP" , + [XDP_PASS ] ="XDP_PASS" , + [XDP_TX ] ="XDP_TX" , + [XDP_REDIRECT ] ="XDP_REDIRECT" , + [XDP_UNKNOWN ] ="XDP_UNKNOWN" , +}; +
samples/bpf/xdp_sample_user.c line 196
Quoted 10 lines
samples/bpf/xdp_sample_user.c new file +10
-0+static const char *elixir_search[NUM_TP ] = { + [TP_REDIRECT_CNT ] ="_trace_xdp_redirect" , + [TP_REDIRECT_MAP_CNT ] ="_trace_xdp_redirect_map" , + [TP_REDIRECT_ERR_CNT ] ="_trace_xdp_redirect_err" , + [TP_REDIRECT_MAP_ERR_CNT ] ="_trace_xdp_redirect_map_err" , + [TP_CPUMAP_ENQUEUE_CNT ] ="trace_xdp_cpumap_enqueue" , + [TP_CPUMAP_KTHREAD_CNT ] ="trace_xdp_cpumap_kthread" , + [TP_EXCEPTION_CNT ] ="trace_xdp_exception" , + [TP_DEVMAP_XMIT_CNT ] ="trace_xdp_devmap_xmit" , +};
nit: inconsistent style wrt tabs on elixir_search vs xdp_action_names, generally like the latter is preferred.
Quoted 9 lines
samples/bpf/xdp_sample_user.c new file +9
-0+/* Dumb hashmap to store ifindex pairs datarec for devmap_xmit_cnt_multi */ +#define HASH_BUCKETS (1 << 5) +#define HASH_MASK (HASH_BUCKETS - 1) + +#define hash_map_for_each (entry, map) \ + for (int __i = 0; __i < HASH_BUCKETS; __i++) \ + for (entry = map->buckets[__i]; entry; entry = entry->next) + +struct map_entry {
samples/bpf/xdp_sample_user.c line 216
Quoted 10 lines
samples/bpf/xdp_sample_user.c new file +10
-0+struct map_entry *next; +__u64 pair; +struct record *val; +}; + +struct hash_map { +struct map_entry *buckets[HASH_BUCKETS ]; +}; + +/* From tools/lib/bpf/hashmap.h */
Should this also have same license then?
Quoted 77 lines
samples/bpf/xdp_sample_user.c new file +77
-0+static size_t hash_bits (size_t h,int bits) +{ +/* shuffle bits and return requested number of upper bits */ +if (bits ==0 ) +return 0 ; + +#if (__SIZEOF_SIZE_T__ == __SIZEOF_LONG_LONG__) +/* LP64 case */ +return (h *11400714819323198485llu ) >> (__SIZEOF_LONG_LONG__ *8 - bits); +#elif (__SIZEOF_SIZE_T__ <= __SIZEOF_LONG__) +return (h *2654435769lu ) >> (__SIZEOF_LONG__ *8 - bits); +#else +# error "Unsupported size_t size" +#endif +} + +static __u64 gettime (void ) +{ +struct timespec t; +int res; + + res =clock_gettime (CLOCK_MONOTONIC , &t); +if (res <0 ) { +fprintf (stderr,"Error with gettimeofday! (%i)\n" , res); +exit (EXIT_FAIL ); + } +return (__u64 ) t.tv_sec *NANOSEC_PER_SEC + t.tv_nsec; +} + +int sample_get_cpus (void ) +{ +int cpus =get_nprocs_conf (); +return cpus <MAX_CPUS ? cpus :MAX_CPUS ; +} + +static int hash_map_add (struct hash_map *map,__u64 pair,struct record *val) +{ +struct map_entry *e, **b; + + e =calloc (1 ,sizeof (*e)); +if (!e) +return -ENOMEM ; + + val->timestamp =gettime (); + + e->pair = pair; + e->val = val; + + b = &map->buckets[hash_bits ((size_t )pair,5 ) &HASH_MASK ]; +if (*b) + e->next = *b; + *b = e; + +return 0 ; +} + +static struct map_entry *hash_map_find (struct hash_map *map,__u64 pair) +{ +struct map_entry *e; + + e = map->buckets[hash_bits ((size_t )pair,5 ) &HASH_MASK ]; +for (; e; e = e->next) { +if (e->pair == pair) +break ; + } + +return e; +} + +static void hash_map_destroy (struct hash_map *map) +{ +if (!map) +return ; + +for (int i =0 ; i <HASH_BUCKETS ; i++) { +struct map_entry *e, *f; +
samples/bpf/xdp_sample_user.c line 303
Quoted 10 lines
samples/bpf/xdp_sample_user.c new file +10
-0+ e = map->buckets[i]; +while (e) { + f = e; + e = e->next; +free (f->val->cpu); +free (f->val); +free (f); + } + } +}
There's also an existing hashtable implementation under tools/include/linux/hashtable.h, can't we reuse that one?
Quoted 88 lines
samples/bpf/xdp_sample_user.c new file +88
-0+static const char *action2str (int action) +{ +if (action <XDP_ACTION_MAX ) +return xdp_action_names[action]; +return NULL ; +} + +static void sample_print_help (int mask) +{ +printf ("Output format description\n\n" +"By default, redirect success statistics are disabled, use -s to enable.\n" +"The terse output mode is default, verbose mode can be activated using -v\n" +"Use SIGQUIT (Ctrl + \\) to switch the mode dynamically at runtime\n\n" +"Terse mode displays at most the following fields:\n" +" rx/s Number of packets received per second\n" +" redir/s Number of packets successfully redirected per second\n" +" err,drop/s Aggregated count of errors per second (including dropped packets)\n" +" xmit/s Number of packets transmitted on the output device per second\n\n" +"Output description for verbose mode:\n" +" FIELD DESCRIPTION\n" ); + +if (mask &SAMPLE_RX_CNT ) { +printf (" receive\t\tDisplays the number of packets received & errors encountered\n" +" \t\t\tWhenever an error or packet drop occurs, details of per CPU error\n" +" \t\t\tand drop statistics will be expanded inline in terse mode.\n" +" \t\t\t\tpkt/s - Packets received per second\n" +" \t\t\t\tdrop/s - Packets dropped per second\n" +" \t\t\t\terror/s - Errors encountered per second\n\n" ); + } +if (mask & (SAMPLE_REDIRECT_CNT |SAMPLE_REDIRECT_ERR_CNT )) { +printf (" redirect\t\tDisplays the number of packets successfully redirected\n" +" \t\t\tErrors encountered are expanded under redirect_err field\n" +" \t\t\tNote that passing -s to enable it has a per packet overhead\n" +" \t\t\t\tredir/s - Packets redirected successfully per second\n\n" +" redirect_err\t\tDisplays the number of packets that failed redirection\n" +" \t\t\tThe errno is expanded under this field with per CPU count\n" +" \t\t\tThe recognized errors are:\n" ); + +for (int i =2 ; i <XDP_REDIRECT_ERR_MAX ; i++) +printf ("\t\t\t %s: %s\n" , xdp_redirect_err_names[i], + xdp_redirect_err_help[i -1 ]); + +printf (" \n\t\t\t\terror/s - Packets that failed redirection per second\n\n" ); + } + +if (mask &SAMPLE_CPUMAP_ENQUEUE_CNT ) { +printf (" enqueue to cpu N\tDisplays the number of packets enqueued to bulk queue of CPU N\n" +" \t\t\tExpands to cpu:FROM->N to display enqueue stats for each CPU enqueuing to CPU N\n" +" \t\t\tReceived packets can be associated with the CPU redirect program is enqueuing \n" +" \t\t\tpackets to.\n" +" \t\t\t\tpkt/s - Packets enqueued per second from other CPU to CPU N\n" +" \t\t\t\tdrop/s - Packets dropped when trying to enqueue to CPU N\n" +" \t\t\t\tbulk-avg - Average number of packets processed for each event\n\n" ); + } + +if (mask &SAMPLE_CPUMAP_KTHREAD_CNT ) { +printf (" kthread\t\tDisplays the number of packets processed in CPUMAP kthread for each CPU\n" +" \t\t\tPackets consumed from ptr_ring in kthread, and its xdp_stats (after calling \n" +" \t\t\tCPUMAP bpf prog) are expanded below this. xdp_stats are expanded as a total and\n" +" \t\t\tthen per-CPU to associate it to each CPU's pinned CPUMAP kthread.\n" +" \t\t\t\tpkt/s - Packets consumed per second from ptr_ring\n" +" \t\t\t\tdrop/s - Packets dropped per second in kthread\n" +" \t\t\t\tsched - Number of times kthread called schedule()\n\n" +" \t\t\txdp_stats (also expands to per-CPU counts)\n" +" \t\t\t\tpass/s - XDP_PASS count for CPUMAP program execution\n" +" \t\t\t\tdrop/s - XDP_DROP count for CPUMAP program execution\n" +" \t\t\t\tredir/s - XDP_REDIRECT count for CPUMAP program execution\n\n" ); + } + +if (mask &SAMPLE_EXCEPTION_CNT ) { +printf (" xdp_exception\t\tDisplays xdp_exception tracepoint events\n" +" \t\t\tThis can occur due to internal driver errors, unrecognized\n" +" \t\t\tXDP actions and due to explicit user trigger by use of XDP_ABORTED\n" +" \t\t\tEach action is expanded below this field with its count\n" +" \t\t\t\thit/s - Number of times the tracepoint was hit per second\n\n" ); + } + +if (mask &SAMPLE_DEVMAP_XMIT_CNT ) { +printf (" devmap_xmit\t\tDisplays devmap_xmit tracepoint events\n" +" \t\t\tThis tracepoint is invoked for successful transmissions on output\n" +" \t\t\tdevice but these statistics are not available for generic XDP mode,\n" +" \t\t\thence they will be omitted from the output when using SKB mode\n" +" \t\t\t\txmit/s - Number of packets that were transmitted per second\n" +" \t\t\t\tdrop/s - Number of packets that failed transmissions per second\n" +" \t\t\t\tdrv_err/s - Number of internal driver errors per second\n" +" \t\t\t\tbulk-avg - Average number of packets processed for each event\n\n" ); + } +}
[...]
Quoted 13 lines
samples/bpf/xdp_sample_user.c new file +13
-0+static const char *make_url (enum tp_type i) +{ +const char *key = elixir_search[i]; +static struct utsname uts = {}; +static char url[128 ]; +static bool uts_init; +int maj, min; +char c[2 ]; + +if (!uts_init) { +if (uname (&uts) <0 ) +return NULL ; + uts_init = true;
samples/bpf/xdp_sample_user.c line 438
Quoted 10 lines
samples/bpf/xdp_sample_user.c new file +10
-0+ } + +if (!key ||sscanf (uts.release,"%d.%d%1s" , &maj, &min, c) !=3 ) +return NULL ; + +snprintf (url,sizeof (url),"https://elixir.bootlin.com/linux/v%d.%d/C/ident/%s" , + maj, min, key); + +return url; +}
I don't think this whole make_url() belongs in here with the link construction to elixir.bootlin.com, it's just confusing when you have downstream trees with custom backports.
Quoted 14 lines
samples/bpf/xdp_sample_user.c new file +14
-0+static void print_link (const char *str,int width,enum tp_type i) +{ +static int t =-1 ; +const char *s; +int fd, l; + +if (t <0 ) { + fd =open ("/proc/self/fd/1" ,O_RDONLY ); +if (fd <0 ) +return ; + t =isatty (fd); +close (fd); + } +
samples/bpf/xdp_sample_user.c line 463
Quoted 10 lines
samples/bpf/xdp_sample_user.c new file +10
-0+ s =make_url (i); +if (!s || !t) { +printf (" %-*s" , width, str); +return ; + } + + l =strlen (str); + width = width - l >0 ? width - l :0 ; +printf (" \x1B]8;;%s\a%s\x1B]8;;\a%*c" , s, str, width,' ' ); +}
Ditto..
Quoted 12 lines
samples/bpf/xdp_sample_user.c new file +12
-0+static struct datarec *alloc_record_per_cpu (void ) +{ +unsigned int nr_cpus =bpf_num_possible_cpus (); +struct datarec *array; + + array =calloc (nr_cpus,sizeof (struct datarec )); +if (!array) { +fprintf (stderr,"Failed to allocate memory (nr_cpus: %u)\n" , nr_cpus); +return NULL ; + } +return array; +}
[...]
Quoted 4 lines
samples/bpf/xdp_sample_user.c new file +4
-0+static int map_collect_percpu_devmap (int map_fd,struct hash_map *map) +{ +unsigned int nr_cpus =bpf_num_possible_cpus (); +__u32 batch, count =32 ;
samples/bpf/xdp_sample_user.c line 531
Quoted 10 lines
samples/bpf/xdp_sample_user.c new file +10
-0+struct datarec *values; +bool init = false; +__u64 *keys; +int i, ret; + + keys =calloc (count,sizeof (__u64)); +if (!keys) +return -ENOMEM ; + values =calloc (count * nr_cpus,sizeof (struct datarec )); +if (!values)
Leaks keys ...
Quoted 2 lines
samples/bpf/xdp_sample_user.c new file +2
-0+return -ENOMEM ; +
[...]
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v3 2/8] samples: bpf: Add common infrastructure for XDP samples
Quoted 46 lines · Daniel Borkmann
Quoted 22 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 eachxdp_exceptionaction for all samples that use these helpers (XDP_ABORTED, etc.) to aid debugging.
* Capturing per ifindex pairdevmap_xmitcounts 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, usingQuoted 7 lines
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.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>Overall I think it's okay to try to streamline the individual XDP tools, but I also tend to wonder whether we keep going beyond the original purpose of kernel samples where the main goal is to provide small, easy to hack & stand-alone code snippets (like in
samples/seccomp... no doubt we have it more complex in BPF land, but still); things people can take away and extend for their purpose. A big portion of the samples are still better off in selftests so they can be run in CI, and those that are not should generally be simplified for developers to rip out, modify, experiment, and build actual applications on top.Quoted 5 lines
--- samples/bpf/Makefile | 3 +- samples/bpf/xdp_sample_shared.h | 47 + samples/bpf/xdp_sample_user.c | 1732 +++++++++++++++++++++++++++++++ samples/bpf/xdp_sample_user.h | 94 ++
Quoted 4 lines
I would have wished that rather than a single patch to get all the boilerplate code in at once to have incremental improvements that refactor the individual sample tools along the way.
I see. I'll try to split these into smaller changes.
Quoted 34 lines
Quoted 34 lines
4 files changed, 1875 insertions(+), 1 deletion(-)create mode 100644
samples/bpf/xdp_sample_shared.hcreate mode 100644samples/bpf/xdp_sample_user.ccreate mode 100644samples/bpf/xdp_sample_user.hsamples/bpf/Makefile +2
-1@@ -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@@ -210,7 +211,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 += -lrtsamples/bpf/xdp_sample_shared.h new file +5
-0@@ -0,0 +1,47 @@ +#ifndef _XDP_SAMPLE_SHARED_H +#define _XDP_SAMPLE_SHARED_H + +/* +* Relaxed load/store
Quoted 9 lines
samples/bpf/xdp_sample_shared.h line 9
Quoted 4 lines
samples/bpf/xdp_sample_shared.h new file +4
-0+* __atomic_load_n/__atomic_store_n built-in is not supported for BPF target +*/ +#define NO_TEAR_LOAD (var) ({ (*(volatile typeof(var) *)&(var)); }) +#define NO_TEAR_STORE (var, val) ({ *((volatile typeof(var) *)&(var)) = (val); })Can't we reuse
READ_ONCE()/WRITE_ONCE()fromtools/include/linux/compiler.hinstead of readding the same here again? We already rely on using the tooling include infra anyway for samples.
Ok, I'll use those.
Quoted 6 lines
Quoted 6 lines
samples/bpf/xdp_sample_shared.h new file +6
-0+/* 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 NO_TEAR_ADD (var, val) \ + ({ \
Quoted 9 lines
samples/bpf/xdp_sample_shared.h line 21
Quoted 6 lines
samples/bpf/xdp_sample_shared.h new file +6
-0+typeof (val) __val = (val); \ +if (__val) \ +NO_TEAR_STORE ((var), (var) + __val); \ + }) + +#define NO_TEAR_INC (var) NO_TEAR_ADD((var), 1)This could likely also go there.
Ok.
Quoted 4 lines
samples/bpf/xdp_sample_shared.h line 23
Quoted 1 line
samples/bpf/xdp_sample_shared.h new file +1
-0+#define MAX_CPUS 64Given this is a heavy rework of samples, can't we also fix up this assumption?
Ok, I'll use CONFIG_NR_CPUS.
Quoted 4 lines
samples/bpf/xdp_sample_shared.h line 24
Quoted 1 line
samples/bpf/xdp_sample_shared.h new file +1
-0+#define ELEMENTS_OF (x) (sizeof(x) / sizeof(*(x)))See
ARRAY_SIZE()undertools/include/linux/kernel.h.
Ok.
Quoted 4 lines
Quoted 4 lines
samples/bpf/xdp_sample_shared.h new file +4
-0+struct datarec { +size_t processed; +size_t dropped; +size_t issue;
Quoted 9 lines
samples/bpf/xdp_sample_shared.h line 35
Quoted 6 lines
samples/bpf/xdp_sample_shared.h new file +6
-0+union { +size_t xdp_pass; +size_t info; + }; +size_t xdp_drop; +size_t xdp_redirect;Why all
size_t, can't we just use__u64for the stats?
READ_ONCE/WRITE_ONCE can tear for __u64 on 32-bit systems, which will lead to printing potentially incorrect output. size_t/unsigned long shouldn't.
Quoted 48 lines
Quoted 48 lines
samples/bpf/xdp_sample_shared.h new file +12
-0+} __attribute__((aligned(64))); + +struct sample_data { +struct datarec rx_cnt[MAX_CPUS ]; +struct datarec redirect_err_cnt[7 *MAX_CPUS ]; +struct datarec cpumap_enqueue_cnt[MAX_CPUS *MAX_CPUS ]; +struct datarec cpumap_kthread_cnt[MAX_CPUS ]; +struct datarec exception_cnt[6 *MAX_CPUS ]; +struct datarec devmap_xmit_cnt[MAX_CPUS ]; +}; + +#endif samples/bpf/xdp_sample_user.c new file +30
-0@@ -0,0 +1,1732 @@ +// SPDX-License-Identifier: GPL-2.0-only +#define _GNU_SOURCE + +#include <errno.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <unistd.h> +#include <math.h> +#include <locale.h> +#include <sys/signalfd.h> +#include <sys/resource.h> +#include <sys/sysinfo.h> +#include <sys/timerfd.h> +#include <getopt.h> +#include <net/if.h> +#include <time.h> +#include <linux/limits.h> +#include <sys/ioctl.h> +#include <net/if.h> +#include <poll.h> +#include <linux/ethtool.h> +#include <linux/sockios.h> +#ifndef SIOCETHTOOL +#define SIOCETHTOOL 0x8946 +#endif +#include <fcntl.h> +#include <arpa/inet.h>
Quoted 9 lines
samples/bpf/xdp_sample_user.c line 36
Quoted 6 lines
samples/bpf/xdp_sample_user.c new file +6
-0+#include <linux/if_link.h> +#include <sys/utsname.h> +#include <bpf/bpf.h> +#include <bpf/libbpf.h> +#include "bpf_util.h" +#include "xdp_sample_user.h" nit: Pls order the above a bit better.
Ok, will do.
Quoted 14 lines
Quoted 14 lines
samples/bpf/xdp_sample_user.c new file +14
-0+#define __sample_print (fmt, cond, printer, ...) \ + ({ \ + if (cond) \ + printer(fmt, ##__VA_ARGS__); \ + }) + +#define print_always (fmt, ...) __sample_print(fmt, 1, printf, ##__VA_ARGS__) +#define print_default (fmt, ...) \ + __sample_print(fmt, sample.log_level & LL_DEFAULT, printf, ##__VA_ARGS__) +#define __print_err (err, fmt, printer, ...) \ + ({ \ + __sample_print(fmt, err > 0 || sample.log_level & LL_DEFAULT, \ + printer, ##__VA_ARGS__); \ + sample.err_exp = sample.err_exp ? true : err > 0; \
Quoted 9 lines
samples/bpf/xdp_sample_user.c line 56
Quoted 5 lines
samples/bpf/xdp_sample_user.c new file +5
-0+ }) +#define print_err (err, fmt, ...) __print_err(err, fmt, printf, ##__VA_ARGS__) + +#define print_link_err (err, str, width, type) \ + __print_err(err, str, print_link, width, type)Couldn't this all be more generalized so other non-XDP samples can use it as well potentially?
It's a bit tied into what the log level means, but I'll try.
Quoted 134 lines
Quoted 134 lines
samples/bpf/xdp_sample_user.c new file +134
-0+#define __COLUMN (x) "%'10" x " %-13s" +#define FMT_COLUMNf __COLUMN(".0f") +#define FMT_COLUMNd __COLUMN("d") +#define FMT_COLUMNl __COLUMN("llu") +#define RX (rx) rx, "rx/s" +#define PPS (pps) pps, "pkt/s" +#define DROP (drop) drop, "drop/s" +#define ERR (err) err, "error/s" +#define HITS (hits) hits, "hit/s" +#define XMIT (xmit) xmit, "xmit/s" +#define PASS (pass) pass, "pass/s" +#define REDIR (redir) redir, "redir/s" +#define NANOSEC_PER_SEC 1000000000/* 10^9 */ + +#define XDP_UNKNOWN (XDP_REDIRECT + 1) +#define XDP_ACTION_MAX (XDP_UNKNOWN + 1) +#define XDP_REDIRECT_ERR_MAX 7 + +enum tp_type { +TP_REDIRECT_CNT , +TP_REDIRECT_MAP_CNT , +TP_REDIRECT_ERR_CNT , +TP_REDIRECT_MAP_ERR_CNT , +TP_CPUMAP_ENQUEUE_CNT , +TP_CPUMAP_KTHREAD_CNT , +TP_EXCEPTION_CNT , +TP_DEVMAP_XMIT_CNT , +NUM_TP , +}; + +enum map_type { +MAP_DEVMAP_XMIT_MULTI , +NUM_MAP , +}; + +enum log_level { +LL_DEFAULT =1U <<0 , +LL_SIMPLE =1U <<1 , +LL_DEBUG =1U <<2 , +}; + +struct record { +__u64 timestamp; +struct datarec total; +struct datarec *cpu; +}; + +struct stats_record { +struct record rx_cnt; +struct record redir_err[XDP_REDIRECT_ERR_MAX ]; +struct record kthread; +struct record exception[XDP_ACTION_MAX ]; +struct record devmap_xmit; +struct hash_map *devmap_xmit_multi; +struct record enq[]; +}; + +struct sample_output { +struct { +__u64 rx; +__u64 redir; +__u64 drop; +__u64 drop_xmit; +__u64 err; +__u64 xmit; + } totals; +struct { +__u64 pps; +__u64 drop; +__u64 err; + } rx_cnt; +struct { +__u64 suc; +__u64 err; + } redir_cnt; +struct { +__u64 hits; + } except_cnt; +struct { +__u64 pps; +__u64 drop; +__u64 err; +double bavg; + } xmit_cnt; +}; + +static struct { +enum log_level log_level; +struct sample_output out; +struct sample_data *data; +unsigned long interval; +int map_fd[NUM_MAP ]; +struct xdp_desc { +int ifindex; +__u32 prog_id; +int flags; + } xdp_progs[32 ]; +bool err_exp; +int xdp_cnt; +int n_cpus; +int sig_fd; +int mask; +} sample; + +static const char *xdp_redirect_err_names[XDP_REDIRECT_ERR_MAX ] = { +/* Key=1 keeps unknown errors */ +"Success" ,"Unknown" ,"EINVAL" ,"ENETDOWN" ,"EMSGSIZE" , +"EOPNOTSUPP" ,"ENOSPC" , +}; + +/* Keyed from Unknown */ +static const char *xdp_redirect_err_help[XDP_REDIRECT_ERR_MAX -1 ] = { +"Unknown error" , +"Invalid redirection" , +"Device being redirected to is down" , +"Packet length too large for device" , +"Operation not supported" , +"No space in ptr_ring of cpumap kthread" , +}; + +static const char *xdp_action_names[XDP_ACTION_MAX ] = { + [XDP_ABORTED ] ="XDP_ABORTED" , + [XDP_DROP ] ="XDP_DROP" , + [XDP_PASS ] ="XDP_PASS" , + [XDP_TX ] ="XDP_TX" , + [XDP_REDIRECT ] ="XDP_REDIRECT" , + [XDP_UNKNOWN ] ="XDP_UNKNOWN" , +}; + +static const char *elixir_search[NUM_TP ] = { + [TP_REDIRECT_CNT ] ="_trace_xdp_redirect" , + [TP_REDIRECT_MAP_CNT ] ="_trace_xdp_redirect_map" , + [TP_REDIRECT_ERR_CNT ] ="_trace_xdp_redirect_err" , + [TP_REDIRECT_MAP_ERR_CNT ] ="_trace_xdp_redirect_map_err" ,
Quoted 8 lines
samples/bpf/xdp_sample_user.c line 196
Quoted 5 lines
samples/bpf/xdp_sample_user.c new file +5
-0+ [TP_CPUMAP_ENQUEUE_CNT] = "trace_xdp_cpumap_enqueue", + [TP_CPUMAP_KTHREAD_CNT] = "trace_xdp_cpumap_kthread", + [TP_EXCEPTION_CNT] = "trace_xdp_exception", + [TP_DEVMAP_XMIT_CNT] = "trace_xdp_devmap_xmit", +};nit: inconsistent style wrt tabs on
elixir_searchvsxdp_action_names, generally like the latter is preferred.
Ok, I'll fix all instances.
Quoted 104 lines
Quoted 9 lines
samples/bpf/xdp_sample_user.c new file +9
-0+/* Dumb hashmap to store ifindex pairs datarec for devmap_xmit_cnt_multi */ +#define HASH_BUCKETS (1 << 5) +#define HASH_MASK (HASH_BUCKETS - 1) + +#define hash_map_for_each (entry, map) \ + for (int __i = 0; __i < HASH_BUCKETS; __i++) \ + for (entry = map->buckets[__i]; entry; entry = entry->next) + +struct map_entry {samples/bpf/xdp_sample_user.c line 216
Quoted 10 lines
samples/bpf/xdp_sample_user.c new file +10
-0+struct map_entry *next; +__u64 pair; +struct record *val; +}; + +struct hash_map { +struct map_entry *buckets[HASH_BUCKETS ]; +}; + +/* From tools/lib/bpf/hashmap.h */ Should this also have same license then?
Quoted 82 lines
samples/bpf/xdp_sample_user.c new file +82
-0+static size_t hash_bits (size_t h,int bits) +{ +/* shuffle bits and return requested number of upper bits */ +if (bits ==0 ) +return 0 ; + +#if (__SIZEOF_SIZE_T__ == __SIZEOF_LONG_LONG__) +/* LP64 case */ +return (h *11400714819323198485llu ) >> (__SIZEOF_LONG_LONG__ *8 - bits); +#elif (__SIZEOF_SIZE_T__ <= __SIZEOF_LONG__) +return (h *2654435769lu ) >> (__SIZEOF_LONG__ *8 - bits); +#else +# error "Unsupported size_t size" +#endif +} + +static __u64 gettime (void ) +{ +struct timespec t; +int res; + + res =clock_gettime (CLOCK_MONOTONIC , &t); +if (res <0 ) { +fprintf (stderr,"Error with gettimeofday! (%i)\n" , res); +exit (EXIT_FAIL ); + } +return (__u64 ) t.tv_sec *NANOSEC_PER_SEC + t.tv_nsec; +} + +int sample_get_cpus (void ) +{ +int cpus =get_nprocs_conf (); +return cpus <MAX_CPUS ? cpus :MAX_CPUS ; +} + +static int hash_map_add (struct hash_map *map,__u64 pair,struct record *val) +{ +struct map_entry *e, **b; + + e =calloc (1 ,sizeof (*e)); +if (!e) +return -ENOMEM ; + + val->timestamp =gettime (); + + e->pair = pair; + e->val = val; + + b = &map->buckets[hash_bits ((size_t )pair,5 ) &HASH_MASK ]; +if (*b) + e->next = *b; + *b = e; + +return 0 ; +} + +static struct map_entry *hash_map_find (struct hash_map *map,__u64 pair) +{ +struct map_entry *e; + + e = map->buckets[hash_bits ((size_t )pair,5 ) &HASH_MASK ]; +for (; e; e = e->next) { +if (e->pair == pair) +break ; + } + +return e; +} + +static void hash_map_destroy (struct hash_map *map) +{ +if (!map) +return ; + +for (int i =0 ; i <HASH_BUCKETS ; i++) { +struct map_entry *e, *f; + + e = map->buckets[i]; +while (e) { + f = e; + e = e->next; +free (f->val->cpu);
Quoted 9 lines
samples/bpf/xdp_sample_user.c line 303
Quoted 5 lines
samples/bpf/xdp_sample_user.c new file +5
-0+free (f->val); +free (f); + } + } +}There's also an existing hashtable implementation under
tools/include/linux/hashtable.h, can't we reuse that one?
I didn't notice this. I'll switch to using it.
Quoted 20 lines
[...]
Quoted 19 lines
samples/bpf/xdp_sample_user.c new file +19
-0+static const char *make_url (enum tp_type i) +{ +const char *key = elixir_search[i]; +static struct utsname uts = {}; +static char url[128 ]; +static bool uts_init; +int maj, min; +char c[2 ]; + +if (!uts_init) { +if (uname (&uts) <0 ) +return NULL ; + uts_init = true; + } + +if (!key ||sscanf (uts.release,"%d.%d%1s" , &maj, &min, c) !=3 ) +return NULL ; + + snprintf(url, sizeof(url), "https://elixir.bootlin.com/linux/v%d.%d/C/ident/%s",
Quoted 9 lines
samples/bpf/xdp_sample_user.c line 438
Quoted 4 lines
samples/bpf/xdp_sample_user.c new file +4
-0+ maj, min, key); + +return url; +}I don't think this whole
make_url()belongs in here with the link construction to elixir.bootlin.com, it's just confusing when you have downstream trees with custom backports.
Right, it only considers the upstream kernel. I guess I'll just drop it.
[...]
I'll also address everything else, thanks!
--
Kartikeya
Re: [PATCH bpf-next v3 2/8] samples: bpf: Add common infrastructure for XDP samples
In reply to Daniel BorkmannQuoted 31 lines · Daniel Borkmann
Quoted 22 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 eachxdp_exceptionaction for all samples that use these helpers (XDP_ABORTED, etc.) to aid debugging.
* Capturing per ifindex pairdevmap_xmitcounts 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, usingQuoted 7 lines
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.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>
Quoted 8 lines
Overall I think it's okay to try to streamline the individual XDP tools, but I also tend to wonder whether we keep going beyond the original purpose of kernel samples where the main goal is to provide small, easy to hack & stand-alone code snippets (like in
samples/seccomp... no doubt we have it more complex in BPF land, but still); things people can take away and extend for their purpose. A big portion of the samples are still better off in selftests so they can be run in CI, and those that are not should generally be simplified for developers to rip out, modify, experiment, and build actual applications on top.
FWIW the idea of improving the samples came from Jesper and myself; we've come to rely on them quite a bit for benchmarking, and our QE folks run them for testing as well. And I've lost count of the number of times I had to redo tests because something wasn't working correctly and I didn't notice that the numbers were off. Kumar took the "improve the XDP samples" idea and ran with it, and I think the result is much improved; having it be immediately obvious when something is off is a huge benefit!
So while I do share your concern about expanding the samples code too much, in this instance I think it's an improvement. I've toyed with the idea of also distributing some of the XDP samples with xdp-tools so they are easier to install as standalone utilities, but I think that is a secondary concern for later.
-Toke
Kumar Kartikeya Dwivedi
maintainerPATCH v3 3/8 samples: bpf: Add BPF support for XDP samples helper
In reply to Kumar Kartikeya DwivediThese 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 | 267 +++++++++++++++++++++++++++++++++++ samples/bpf/xdp_sample.bpf.h | 62 ++++++++ 3 files changed, 354 insertions(+) create mode 100644 samples/bpf/xdp_sample.bpf.c create mode 100644 samples/bpf/xdp_sample.bpf.h
samples/bpf/Makefile +25 -0
@@ -277,6 +277,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__)
@@ -314,6 +319,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 +267 -0
267 lines not on this page · raw message
samples/bpf/xdp_sample.bpf.h new file +62 -0
62 lines not on this page · raw message
Signature · 1 line
2.32.0
Kumar Kartikeya Dwivedi
maintainerPATCH v3 4/8 samples: bpf: Convert xdp_monitor to use XDP samples helper
In reply to Kumar Kartikeya DwivediThis 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 | 50 ++- samples/bpf/xdp_monitor.bpf.c | 8 + samples/bpf/xdp_monitor_kern.c | 257 ----------- samples/bpf/xdp_monitor_user.c | 792 +++------------------------------ 4 files changed, 110 insertions(+), 997 deletions(-) create mode 100644 samples/bpf/xdp_monitor.bpf.c delete mode 100644 samples/bpf/xdp_monitor_kern.c
samples/bpf/Makefile +47 -3
@@ -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
@@ -117,6 +117,8 @@ xdp_sample_pkts-objs := xdp_sample_pkts_user.o ibumad-objs := ibumad_user.o hbm-objs := hbm.o $(CGROUP_HELPERS) +xdp_monitor-objs := xdp_monitor_user.o $(XDP_SAMPLE) + # Tell kbuild to always build the programs always-y := $(tprogs-y) always-y += sockex1_kern.o
@@ -165,7 +167,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
@@ -312,6 +313,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
@@ -339,6 +342,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
@@ -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 -737
838 lines not on this page · raw message
Signature · 1 line
2.32.0
Kumar Kartikeya Dwivedi
maintainerPATCH v3 5/8 samples: bpf: Convert xdp_redirect to use XDP samples helper
In reply to Kumar Kartikeya DwivediThis 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 | 51 ++++++ samples/bpf/xdp_redirect_kern.c | 90 ----------- samples/bpf/xdp_redirect_user.c | 265 +++++++++++++------------------- 4 files changed, 164 insertions(+), 252 deletions(-) create mode 100644 samples/bpf/xdp_redirect.bpf.c delete mode 100644 samples/bpf/xdp_redirect_kern.c
samples/bpf/Makefile +6 -4
@@ -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
@@ -117,6 +116,7 @@ xdp_sample_pkts-objs := xdp_sample_pkts_user.o ibumad-objs := ibumad_user.o hbm-objs := hbm.o $(CGROUP_HELPERS) +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
@@ -163,7 +163,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
@@ -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_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
@@ -356,6 +356,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
@@ -366,9 +367,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 +51 -0
51 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 +107 -158
314 lines not on this page · raw message
Signature · 1 line
2.32.0
Kumar Kartikeya Dwivedi
maintainerPATCH v3 6/8 samples: bpf: Convert xdp_redirect_map to use XDP samples helpers
In reply to Kumar Kartikeya DwivediThis 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} | 87 +---
samples/bpf/xdp_redirect_map_user.c | 380 +++++++-----------
3 files changed, 179 insertions(+), 298 deletions(-)
rename samples/bpf/{xdp_redirect_map_kern.c => xdp_redirect_map.bpf.c} (58%)samples/bpf/Makefile +6 -4
@@ -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
@@ -116,6 +115,7 @@ xdp_sample_pkts-objs := xdp_sample_pkts_user.o ibumad-objs := ibumad_user.o hbm-objs := hbm.o $(CGROUP_HELPERS) +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)
@@ -163,7 +163,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
@@ -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_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
@@ -356,6 +356,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
@@ -367,9 +368,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 +21 -66
149 lines not on this page · raw message
samples/bpf/xdp_redirect_map_user.c +152 -228
430 lines not on this page · raw message
Signature · 1 line
2.32.0
Kumar Kartikeya Dwivedi
maintainerPATCH v3 7/8 samples: bpf: Convert xdp_redirect_map_multi to use XDP samples helpers
In reply to Kumar Kartikeya DwivediThis 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} | 51 ++-
samples/bpf/xdp_redirect_map_multi_user.c | 338 +++++++-----------
3 files changed, 161 insertions(+), 239 deletions(-)
rename samples/bpf/{xdp_redirect_map_multi_kern.c => xdp_redirect_map_multi.bpf.c} (64%)samples/bpf/Makefile +7 -4
@@ -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
@@ -115,6 +114,7 @@ xdp_sample_pkts-objs := xdp_sample_pkts_user.o ibumad-objs := ibumad_user.o hbm-objs := hbm.o $(CGROUP_HELPERS) +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)
@@ -163,7 +163,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
@@ -311,6 +310,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
@@ -356,6 +356,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
@@ -368,9 +369,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 +23 -28
84 lines not on this page · raw message
samples/bpf/xdp_redirect_map_multi_user.c +131 -207
425 lines not on this page · raw message
Signature · 1 line
2.32.0
Kumar Kartikeya Dwivedi
maintainerPATCH v3 8/8 samples: bpf: Convert xdp_redirect_cpu to use XDP samples helpers
In reply to Kumar Kartikeya DwivediThis 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 +-
...rect_cpu_kern.c => xdp_redirect_cpu.bpf.c} | 407 ++-----
samples/bpf/xdp_redirect_cpu_user.c | 992 ++++--------------
3 files changed, 322 insertions(+), 1089 deletions(-)
rename samples/bpf/{xdp_redirect_cpu_kern.c => xdp_redirect_cpu.bpf.c} (52%)samples/bpf/Makefile +7 -5
@@ -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
@@ -114,6 +113,7 @@ xdp_sample_pkts-objs := xdp_sample_pkts_user.o ibumad-objs := ibumad_user.o hbm-objs := hbm.o $(CGROUP_HELPERS) +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)
@@ -163,7 +163,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
@@ -310,6 +309,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
@@ -356,6 +356,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
@@ -369,10 +370,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 renamed from samples/bpf/xdp_redirect_cpu_kern.c +107 -300
635 lines not on this page · raw message
samples/bpf/xdp_redirect_cpu_user.c +208 -784
1,156 lines not on this page · raw message
Signature · 1 line
2.32.0