Skip to content

Commit

Permalink
xdp-loader: introduce features command
Browse files Browse the repository at this point in the history
Add features command to xdp-loader in order to dump XDP features
supported by the NIC.

Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
  • Loading branch information
LorenzoBianconi authored and tohojo committed Jun 8, 2023
1 parent 404fbd5 commit 870db97
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 2 deletions.
1 change: 1 addition & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ check_libbpf_functions()
check_libbpf_function "bpf_xdp_attach" "(0, 0, 0, NULL)" "" "$LIBBPF_CFLAGS" "$LIBBPF_LDLIBS"
check_libbpf_function "bpf_map__set_autocreate" "(NULL, false)" "" "$LIBBPF_CFLAGS" "$LIBBPF_LDLIBS"
check_libbpf_function "bpf_prog_test_run_opts" "(0, &opts)" "DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, .batch_size = 1)" "$LIBBPF_CFLAGS" "$LIBBPF_LDLIBS"
check_libbpf_function "bpf_xdp_query" "(0, 0, &opts)" "DECLARE_LIBBPF_OPTS(bpf_xdp_query_opts, opts, .feature_flags = 1)" "$LIBBPF_CFLAGS" "$LIBBPF_LDLIBS"
}

get_libbpf_version()
Expand Down
61 changes: 61 additions & 0 deletions headers/linux/netdev.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
/* Do not edit directly, auto-generated from: */
/* Documentation/netlink/specs/netdev.yaml */
/* YNL-GEN uapi header */

#ifndef _UAPI_LINUX_NETDEV_H
#define _UAPI_LINUX_NETDEV_H

#define NETDEV_FAMILY_NAME "netdev"
#define NETDEV_FAMILY_VERSION 1

/**
* enum netdev_xdp_act
* @NETDEV_XDP_ACT_BASIC: XDP feautues set supported by all drivers
* (XDP_ABORTED, XDP_DROP, XDP_PASS, XDP_TX)
* @NETDEV_XDP_ACT_REDIRECT: The netdev supports XDP_REDIRECT
* @NETDEV_XDP_ACT_NDO_XMIT: This feature informs if netdev implements
* ndo_xdp_xmit callback.
* @NETDEV_XDP_ACT_XSK_ZEROCOPY: This feature informs if netdev supports AF_XDP
* in zero copy mode.
* @NETDEV_XDP_ACT_HW_OFFLOAD: This feature informs if netdev supports XDP hw
* offloading.
* @NETDEV_XDP_ACT_RX_SG: This feature informs if netdev implements non-linear
* XDP buffer support in the driver napi callback.
* @NETDEV_XDP_ACT_NDO_XMIT_SG: This feature informs if netdev implements
* non-linear XDP buffer support in ndo_xdp_xmit callback.
*/
enum netdev_xdp_act {
NETDEV_XDP_ACT_BASIC = 1,
NETDEV_XDP_ACT_REDIRECT = 2,
NETDEV_XDP_ACT_NDO_XMIT = 4,
NETDEV_XDP_ACT_XSK_ZEROCOPY = 8,
NETDEV_XDP_ACT_HW_OFFLOAD = 16,
NETDEV_XDP_ACT_RX_SG = 32,
NETDEV_XDP_ACT_NDO_XMIT_SG = 64,

NETDEV_XDP_ACT_MASK = 127,
};

enum {
NETDEV_A_DEV_IFINDEX = 1,
NETDEV_A_DEV_PAD,
NETDEV_A_DEV_XDP_FEATURES,

__NETDEV_A_DEV_MAX,
NETDEV_A_DEV_MAX = (__NETDEV_A_DEV_MAX - 1)
};

enum {
NETDEV_CMD_DEV_GET = 1,
NETDEV_CMD_DEV_ADD_NTF,
NETDEV_CMD_DEV_DEL_NTF,
NETDEV_CMD_DEV_CHANGE_NTF,

__NETDEV_CMD_MAX,
NETDEV_CMD_MAX = (__NETDEV_CMD_MAX - 1)
};

#define NETDEV_MCGRP_MGMT "mgmt"

#endif /* _UAPI_LINUX_NETDEV_H */
33 changes: 33 additions & 0 deletions xdp-loader/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Where COMMAND can be one of:
load - load an XDP program on an interface
unload - unload an XDP program from an interface
status - show current XDP program status
features - show XDP features supported by the NIC
clean - clean up detached program links in XDP bpffs directory
help - show the list of available commands
#+end_src
Expand Down Expand Up @@ -136,6 +137,38 @@ Enable debug logging. Specify twice for even more verbosity.
** -h, --help
Display a summary of the available options

* The FEATURES command
The =features= command displays the XDP features supported by the NIC.

Currently supported XDP features are:

** NETDEV_XDP_ACT_BASIC
The networking device has basic support for running XDP programs, and can
handle the base set of return codes (XDP_ABORTED, XDP_DROP, XDP_PASS, XDP_TX).

** NETDEV_XDP_ACT_REDIRECT
The network device supports handling the XDP_REDIRECT return code. This means
packets can be redirected from this device by XDP.

** NETDEV_XDP_ACT_NDO_XMIT
The networking interfaces implements the ndo_xdp_xmit callback. This means
packets can be redirected to this device by XDP.

** NETDEV_XDP_ACT_XSK_ZEROCOPY
The networking device supports AF_XDP in zero copy mode.

** NETDEV_XDP_ACT_HW_OFFLOAD
The networking device supports XDP hw offloading.

** NETDEV_XDP_ACT_RX_SG
The networking device supports non-linear XDP frames on the receive side.
This means XDP can be used with big MTUs on this device (if the XDP program
is compiled with fragments support)

** NETDEV_XDP_ACT_NDO_XMIT_SG
The networking device supports non-linear XDP frames on the transmit side. This
means non-linear frames can be redirected to this device.

* The CLEAN command

The syntax for the =clean= command is:
Expand Down
28 changes: 27 additions & 1 deletion xdp-loader/tests/test-xdp-loader.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
XDP_LOADER=${XDP_LOADER:-./xdp-loader}
ALL_TESTS="test_load test_section test_prog_name test_load_multi test_load_incremental test_load_clobber"
ALL_TESTS="test_load test_section test_prog_name test_load_multi test_load_incremental test_load_clobber test_features"

test_load()
{
Expand Down Expand Up @@ -90,6 +90,32 @@ test_load_clobber()
check_run $XDP_LOADER unload $NS --all -vv
}

check_xdp_feature()
{
check_run ip link add dev v0 type veth peer name v1

$XDP_LOADER features v0 | grep "$1" | grep -q "$2"
ret=$?

ip link del dev v0

[ $ret -eq 1 ] && exit 1
}

test_features()
{
skip_if_missing_kernel_symbol xdp_set_features_flag

check_xdp_feature NETDEV_XDP_ACT_BASIC yes
check_xdp_feature NETDEV_XDP_ACT_REDIRECT yes
check_xdp_feature NETDEV_XDP_ACT_NDO_XMIT no
check_xdp_feature NETDEV_XDP_ACT_XSK_ZEROCOPY no
check_xdp_feature NETDEV_XDP_ACT_HW_OFFLOAD no
check_xdp_feature NETDEV_XDP_ACT_RX_SG yes
check_xdp_feature NETDEV_XDP_ACT_NDO_XMIT_SG no

return 0
}

cleanup_tests()
{
Expand Down
44 changes: 43 additions & 1 deletion xdp-loader/xdp-loader.8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH "xdp-loader" "8" "JANUARY 9, 2023" "V1.3.1" "XDP program loader"
.TH "xdp-loader" "8" "JUNE 7, 2023" "V1.3.1" "XDP program loader"

.SH "NAME"
xdp-loader \- an XDP program loader
Expand All @@ -21,6 +21,7 @@ Where COMMAND can be one of:
load - load an XDP program on an interface
unload - unload an XDP program from an interface
status - show current XDP program status
features - show XDP features supported by the NIC
clean - clean up detached program links in XDP bpffs directory
help - show the list of available commands
\fP
Expand Down Expand Up @@ -160,6 +161,47 @@ Enable debug logging. Specify twice for even more verbosity.
.PP
Display a summary of the available options

.SH "The FEATURES command"
.PP
The \fIfeatures\fP command displays the XDP features supported by the NIC.

.PP
Currently supported XDP features are:

.SS "NETDEV_XDP_ACT_BASIC"
.PP
The networking device has basic support for running XDP programs, and can
handle the base set of return codes (XDP_ABORTED, XDP_DROP, XDP_PASS, XDP_TX).

.SS "NETDEV_XDP_ACT_REDIRECT"
.PP
The network device supports handling the XDP_REDIRECT return code. This means
packets can be redirected from this device by XDP.

.SS "NETDEV_XDP_ACT_NDO_XMIT"
.PP
The networking interfaces implements the ndo_xdp_xmit callback. This means
packets can be redirected to this device by XDP.

.SS "NETDEV_XDP_ACT_XSK_ZEROCOPY"
.PP
The networking device supports AF_XDP in zero copy mode.

.SS "NETDEV_XDP_ACT_HW_OFFLOAD"
.PP
The networking device supports XDP hw offloading.

.SS "NETDEV_XDP_ACT_RX_SG"
.PP
The networking device supports non-linear XDP frames on the receive side.
This means XDP can be used with big MTUs on this device (if the XDP program
is compiled with fragments support)

.SS "NETDEV_XDP_ACT_NDO_XMIT_SG"
.PP
The networking device supports non-linear XDP frames on the transmit side. This
means non-linear frames can be redirected to this device.

.SH "The CLEAN command"
.PP
The syntax for the \fIclean\fP command is:
Expand Down
68 changes: 68 additions & 0 deletions xdp-loader/xdp-loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <bpf/libbpf.h>
#include <xdp/libxdp.h>
#include <linux/err.h>
#include <linux/netdev.h>

#include "params.h"
#include "logging.h"
Expand Down Expand Up @@ -377,6 +378,70 @@ int do_clean(const void *cfg, __unused const char *pin_root_path)
return libxdp_clean_references(opt->iface.ifindex);
}

static const struct featuresopt {
struct iface iface;
} defaults_features = {};

static struct prog_option features_options[] = {
DEFINE_OPTION("dev", OPT_IFNAME, struct featuresopt, iface,
.positional = true,
.metavar = "<ifname>",
.required = true,
.help = "Show XDP features for device <ifname>"),
END_OPTIONS
};

#define CHECK_XDP_FEATURE(f) (opts.feature_flags & (f) ? "yes" : "no")
static int iface_print_xdp_features(const struct iface *iface)
{
#ifdef HAVE_LIBBPF_BPF_XDP_QUERY
LIBBPF_OPTS(bpf_xdp_query_opts, opts);
int err;

err = bpf_xdp_query(iface->ifindex, 0, &opts);
if (err) {
pr_warn("The running kernel doesn't support querying XDP features (%d).\n", err);
return err;
}

/* NETDEV_XDP features are defined in <linux/netdev.h> kernel header */
printf("NETDEV_XDP_ACT_BASIC:\t\t%s\n",
CHECK_XDP_FEATURE(NETDEV_XDP_ACT_BASIC));
printf("NETDEV_XDP_ACT_REDIRECT:\t%s\n",
CHECK_XDP_FEATURE(NETDEV_XDP_ACT_REDIRECT));
printf("NETDEV_XDP_ACT_NDO_XMIT:\t%s\n",
CHECK_XDP_FEATURE(NETDEV_XDP_ACT_NDO_XMIT));
printf("NETDEV_XDP_ACT_XSK_ZEROCOPY:\t%s\n",
CHECK_XDP_FEATURE(NETDEV_XDP_ACT_XSK_ZEROCOPY));
printf("NETDEV_XDP_ACT_HW_OFFLOAD:\t%s\n",
CHECK_XDP_FEATURE(NETDEV_XDP_ACT_HW_OFFLOAD));
printf("NETDEV_XDP_ACT_RX_SG:\t\t%s\n",
CHECK_XDP_FEATURE(NETDEV_XDP_ACT_RX_SG));
printf("NETDEV_XDP_ACT_NDO_XMIT_SG:\t%s\n",
CHECK_XDP_FEATURE(NETDEV_XDP_ACT_NDO_XMIT_SG));

if (opts.feature_flags & ~NETDEV_XDP_ACT_MASK)
pr_debug("unknown reported xdp features: 0x%llx\n",
opts.feature_flags & ~NETDEV_XDP_ACT_MASK);

return 0;
#else
__unused const void *i = iface;

pr_warn("Cannot display features, because xdp-loader was compiled against an "
"old version of libbpf without support for querying features.\n");

return -EOPNOTSUPP;
#endif
}

int do_features(const void *cfg, __unused const char *pin_root_path)
{
const struct featuresopt *opt = cfg;

return iface_print_xdp_features(&opt->iface);
}

int do_help(__unused const void *cfg, __unused const char *pin_root_path)
{
fprintf(stderr,
Expand All @@ -387,6 +452,7 @@ int do_help(__unused const void *cfg, __unused const char *pin_root_path)
" unload - unload an XDP program from an interface\n"
" status - show current XDP program status\n"
" clean - clean up detached program links in XDP bpffs directory\n"
" features - show XDP features supported by the NIC\n"
" help - show this help message\n"
"\n"
"Use 'xdp-loader COMMAND --help' to see options for each command\n");
Expand All @@ -398,6 +464,7 @@ static const struct prog_command cmds[] = {
DEFINE_COMMAND(unload, "Unload an XDP program from an interface"),
DEFINE_COMMAND(clean, "Clean up detached program links in XDP bpffs directory"),
DEFINE_COMMAND(status, "Show XDP program status"),
DEFINE_COMMAND(features, "Show NIC XDP features"),
{ .name = "help", .func = do_help, .no_cfg = true },
END_COMMANDS
};
Expand All @@ -406,6 +473,7 @@ union all_opts {
struct loadopt load;
struct unloadopt unload;
struct statusopt status;
struct featuresopt features;
};

int main(int argc, char **argv)
Expand Down

0 comments on commit 870db97

Please sign in to comment.