1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include <linux/err.h>
#include <linux/if_link.h>
#include "params.h"
#include "logging.h"
#include "util.h"
#include "xdp_sample.h"
#include "xdpsock.h"
#include "compat.h"
#define PROG_NAME "test-tool"
struct enum_val xdp_modes[] = {
{"native", XDP_MODE_NATIVE},
{"skb", XDP_MODE_SKB},
{"hw", XDP_MODE_HW},
{"unspecified", XDP_MODE_UNSPEC},
{NULL, 0}
};
static const struct loadopt {
bool help;
enum xdp_attach_mode mode;
struct iface iface;
char *filename;
} defaults_load = {
.mode = XDP_MODE_NATIVE
};
static struct bpf_object *open_bpf_obj(const char *filename,
struct bpf_object_open_opts *opts)
{
struct bpf_object *obj;
int err;
obj = bpf_object__open_file(filename, opts);
err = libbpf_get_error(obj);
if (err) {
if (err == -ENOENT)
pr_debug(
"Couldn't load the eBPF program (libbpf said 'no such file').\n"
"Maybe the program was compiled with a too old "
"version of LLVM (need v9.0+)?\n");
return ERR_PTR(err);
}
return obj;
}
static int do_xdp_attach(int ifindex, int prog_fd, int old_fd, __u32 xdp_flags)
{
#ifdef HAVE_LIBBPF_BPF_XDP_ATTACH
LIBBPF_OPTS(bpf_xdp_attach_opts, opts,
.old_prog_fd = old_fd);
return bpf_xdp_attach(ifindex, prog_fd, xdp_flags, &opts);
#else
DECLARE_LIBBPF_OPTS(bpf_xdp_set_link_opts, opts, .old_fd = old_fd);
return bpf_set_link_xdp_fd_opts(ifindex, prog_fd, xdp_flags, old_fd ? &opts : NULL);
#endif
}
int do_load(const void *cfg, __unused const char *pin_root_path)
{
const struct loadopt *opt = cfg;
struct bpf_program *bpf_prog;
char errmsg[STRERR_BUFSIZE];
struct bpf_object *obj;
int err = EXIT_SUCCESS;
int xdp_flags;
int prog_fd;
silence_libbpf_logging();
retry:
obj = open_bpf_obj(opt->filename, NULL);
if (IS_ERR(obj)) {
err = PTR_ERR(obj);
if (err == -EPERM && !double_rlimit())
goto retry;
libxdp_strerror(err, errmsg, sizeof(errmsg));
pr_warn("ERROR: Couldn't open file '%s': %s\n",
opt->filename, errmsg);
goto out;
}
err = bpf_object__load(obj);
if (err) {
if (err == -EPERM && !double_rlimit()) {
bpf_object__close(obj);
goto retry;
}
libbpf_strerror(err, errmsg, sizeof(errmsg));
pr_warn("ERROR: Can't load eBPF object: %s(%d)\n",
errmsg, err);
goto out;
}
bpf_prog = bpf_object__next_program(obj, NULL);
if (!bpf_prog) {
pr_warn("ERROR: Couldn't find xdp program in bpf object!\n");
err = -ENOENT;
goto out;
}
prog_fd = bpf_program__fd(bpf_prog);
if (prog_fd < 0) {
err = prog_fd;
libxdp_strerror(err, errmsg, sizeof(errmsg));
pr_warn("ERROR: Couldn't find xdp program's file descriptor: %s\n",
errmsg);
goto out;
}
xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
switch (opt->mode) {
case XDP_MODE_SKB:
xdp_flags |= XDP_FLAGS_SKB_MODE;
break;
case XDP_MODE_NATIVE:
xdp_flags |= XDP_FLAGS_DRV_MODE;
break;
case XDP_MODE_HW:
xdp_flags |= XDP_FLAGS_HW_MODE;
break;
case XDP_MODE_UNSPEC:
break;
}
err = do_xdp_attach(opt->iface.ifindex, prog_fd, 0, xdp_flags);
if (err < 0) {
pr_info("ERROR: Failed attaching XDP program to ifindex %d: %s\n",
opt->iface.ifindex, strerror(-err));
switch (-err) {
case EBUSY:
case EEXIST:
pr_info("XDP already loaded on device.\n");
break;
case EOPNOTSUPP:
pr_info("XDP mode not supported; try using SKB mode.\n");
break;
default:
break;
}
goto out;
}
out:
return err;
}
static struct prog_option load_options[] = {
DEFINE_OPTION("mode", OPT_ENUM, struct loadopt, mode,
.short_opt = 'm',
.typearg = xdp_modes,
.metavar = "<mode>",
.help = "Load XDP program in <mode>; default native"),
DEFINE_OPTION("dev", OPT_IFNAME, struct loadopt, iface,
.positional = true,
.metavar = "<ifname>",
.required = true,
.help = "Load on device <ifname>"),
DEFINE_OPTION("filename", OPT_STRING, struct loadopt, filename,
.positional = true,
.metavar = "<filename>",
.required = true,
.help = "Load program from <filename>"),
END_OPTIONS
};
enum probe_action {
PROBE_CPUMAP_PROGRAM,
PROBE_XDP_LOAD_BYTES,
PROBE_XSK_BUSY_POLL,
};
struct enum_val probe_actions[] = {
{"cpumap-prog", PROBE_CPUMAP_PROGRAM},
{"xdp-load-bytes", PROBE_XDP_LOAD_BYTES},
{"xsk-busy-poll", PROBE_XSK_BUSY_POLL},
{NULL, 0}
};
static const struct probeopt {
enum probe_action action;
} defaults_probe = {};
int do_probe(const void *cfg, __unused const char *pin_root_path)
{
const struct probeopt *opt = cfg;
bool res = false;
switch (opt->action) {
case PROBE_CPUMAP_PROGRAM:
#ifdef HAVE_BPFTOOL
res = sample_probe_cpumap_compat();
#endif
break;
case PROBE_XDP_LOAD_BYTES:
#ifdef HAVE_BPFTOOL
res = sample_probe_xdp_load_bytes();
#endif
break;
case PROBE_XSK_BUSY_POLL:
#ifdef HAVE_BPFTOOL
res = xsk_probe_busy_poll();
#endif
break;
default:
return EXIT_FAILURE;
}
pr_debug("Probing for %s: %s\n",
probe_actions[opt->action].name,
res ? "Supported" : "Unsupported");
return res ? EXIT_SUCCESS : EXIT_FAILURE;
}
static struct prog_option probe_options[] = {
DEFINE_OPTION("action", OPT_ENUM, struct probeopt, action,
.positional = true,
.metavar = "<action>",
.required = true,
.typearg = probe_actions,
.help = "Probe for <action>"),
END_OPTIONS
};
int do_help(__unused const void *cfg, __unused const char *pin_root_path)
{
fprintf(stderr,
"Usage: test-tool COMMAND [options]\n"
"\n"
"COMMAND can be one of:\n"
" load - load an XDP program on an interface\n"
" probe - probe for kernel features\n"
" help - show this help message\n"
"\n"
"Use 'test-tool COMMAND --help' to see options for each command\n");
return -1;
}
static const struct prog_command cmds[] = {
DEFINE_COMMAND(load, "Load an XDP program on an interface"),
DEFINE_COMMAND(probe, "Probe for kernel features"),
{ .name = "help", .func = do_help, .no_cfg = true },
END_COMMANDS
};
union all_opts {
struct loadopt load;
struct probeopt probe;
};
int main(int argc, char **argv)
{
if (argc > 1)
return dispatch_commands(argv[1], argc - 1, argv + 1, cmds,
sizeof(union all_opts), PROG_NAME, false);
return do_help(NULL, NULL);
}
|