mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2024-12-28 11:18:45 +07:00
65c9362859
This patch adds struct_ops support to the bpftool. To recap a bit on the recent bpf_struct_ops feature on the kernel side: It currently supports "struct tcp_congestion_ops" to be implemented in bpf. At a high level, bpf_struct_ops is struct_ops map populated with a number of bpf progs. bpf_struct_ops currently supports the "struct tcp_congestion_ops". However, the bpf_struct_ops design is generic enough that other kernel struct ops can be supported in the future. Although struct_ops is map+progs at a high lever, there are differences in details. For example, 1) After registering a struct_ops, the struct_ops is held by the kernel subsystem (e.g. tcp-cc). Thus, there is no need to pin a struct_ops map or its progs in order to keep them around. 2) To iterate all struct_ops in a system, it iterates all maps in type BPF_MAP_TYPE_STRUCT_OPS. BPF_MAP_TYPE_STRUCT_OPS is the current usual filter. In the future, it may need to filter by other struct_ops specific properties. e.g. filter by tcp_congestion_ops or other kernel subsystem ops in the future. 3) struct_ops requires the running kernel having BTF info. That allows more flexibility in handling other kernel structs. e.g. it can always dump the latest bpf_map_info. 4) Also, "struct_ops" command is not intended to repeat all features already provided by "map" or "prog". For example, if there really is a need to pin the struct_ops map, the user can use the "map" cmd to do that. While the first attempt was to reuse parts from map/prog.c, it ended up not a lot to share. The only obvious item is the map_parse_fds() but that still requires modifications to accommodate struct_ops map specific filtering (for the immediate and the future needs). Together with the earlier mentioned differences, it is better to part away from map/prog.c. The initial set of subcmds are, register, unregister, show, and dump. For register, it registers all struct_ops maps that can be found in an obj file. Option can be added in the future to specify a particular struct_ops map. Also, the common bpf_tcp_cc is stateless (e.g. bpf_cubic.c and bpf_dctcp.c). The "reuse map" feature is not implemented in this patch and it can be considered later also. For other subcmds, please see the man doc for details. A sample output of dump: [root@arch-fb-vm1 bpf]# bpftool struct_ops dump name cubic [{ "bpf_map_info": { "type": 26, "id": 64, "key_size": 4, "value_size": 256, "max_entries": 1, "map_flags": 0, "name": "cubic", "ifindex": 0, "btf_vmlinux_value_type_id": 18452, "netns_dev": 0, "netns_ino": 0, "btf_id": 52, "btf_key_type_id": 0, "btf_value_type_id": 0 } },{ "bpf_struct_ops_tcp_congestion_ops": { "refcnt": { "refs": { "counter": 1 } }, "state": "BPF_STRUCT_OPS_STATE_INUSE", "data": { "list": { "next": 0, "prev": 0 }, "key": 0, "flags": 0, "init": "void (struct sock *) bictcp_init/prog_id:138", "release": "void (struct sock *) 0", "ssthresh": "u32 (struct sock *) bictcp_recalc_ssthresh/prog_id:141", "cong_avoid": "void (struct sock *, u32, u32) bictcp_cong_avoid/prog_id:140", "set_state": "void (struct sock *, u8) bictcp_state/prog_id:142", "cwnd_event": "void (struct sock *, enum tcp_ca_event) bictcp_cwnd_event/prog_id:139", "in_ack_event": "void (struct sock *, u32) 0", "undo_cwnd": "u32 (struct sock *) tcp_reno_undo_cwnd/prog_id:144", "pkts_acked": "void (struct sock *, const struct ack_sample *) bictcp_acked/prog_id:143", "min_tso_segs": "u32 (struct sock *) 0", "sndbuf_expand": "u32 (struct sock *) 0", "cong_control": "void (struct sock *, const struct rate_sample *) 0", "get_info": "size_t (struct sock *, u32, int *, union tcp_cc_info *) 0", "name": "bpf_cubic", "owner": 0 } } } ] Signed-off-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Quentin Monnet <quentin@isovalent.com> Link: https://lore.kernel.org/bpf/20200318171656.129650-1-kafai@fb.com
429 lines
8.3 KiB
C
429 lines
8.3 KiB
C
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
|
|
/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
|
|
|
|
#include <ctype.h>
|
|
#include <errno.h>
|
|
#include <getopt.h>
|
|
#include <linux/bpf.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <bpf/bpf.h>
|
|
#include <bpf/libbpf.h>
|
|
|
|
#include "main.h"
|
|
|
|
#define BATCH_LINE_LEN_MAX 65536
|
|
#define BATCH_ARG_NB_MAX 4096
|
|
|
|
const char *bin_name;
|
|
static int last_argc;
|
|
static char **last_argv;
|
|
static int (*last_do_help)(int argc, char **argv);
|
|
json_writer_t *json_wtr;
|
|
bool pretty_output;
|
|
bool json_output;
|
|
bool show_pinned;
|
|
bool block_mount;
|
|
bool verifier_logs;
|
|
bool relaxed_maps;
|
|
struct pinned_obj_table prog_table;
|
|
struct pinned_obj_table map_table;
|
|
|
|
static void __noreturn clean_and_exit(int i)
|
|
{
|
|
if (json_output)
|
|
jsonw_destroy(&json_wtr);
|
|
|
|
exit(i);
|
|
}
|
|
|
|
void usage(void)
|
|
{
|
|
last_do_help(last_argc - 1, last_argv + 1);
|
|
|
|
clean_and_exit(-1);
|
|
}
|
|
|
|
static int do_help(int argc, char **argv)
|
|
{
|
|
if (json_output) {
|
|
jsonw_null(json_wtr);
|
|
return 0;
|
|
}
|
|
|
|
fprintf(stderr,
|
|
"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
|
|
" %s batch file FILE\n"
|
|
" %s version\n"
|
|
"\n"
|
|
" OBJECT := { prog | map | cgroup | perf | net | feature | btf | gen | struct_ops }\n"
|
|
" " HELP_SPEC_OPTIONS "\n"
|
|
"",
|
|
bin_name, bin_name, bin_name);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int do_version(int argc, char **argv)
|
|
{
|
|
if (json_output) {
|
|
jsonw_start_object(json_wtr);
|
|
jsonw_name(json_wtr, "version");
|
|
jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
|
|
jsonw_end_object(json_wtr);
|
|
} else {
|
|
printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int cmd_select(const struct cmd *cmds, int argc, char **argv,
|
|
int (*help)(int argc, char **argv))
|
|
{
|
|
unsigned int i;
|
|
|
|
last_argc = argc;
|
|
last_argv = argv;
|
|
last_do_help = help;
|
|
|
|
if (argc < 1 && cmds[0].func)
|
|
return cmds[0].func(argc, argv);
|
|
|
|
for (i = 0; cmds[i].func; i++)
|
|
if (is_prefix(*argv, cmds[i].cmd))
|
|
return cmds[i].func(argc - 1, argv + 1);
|
|
|
|
help(argc - 1, argv + 1);
|
|
|
|
return -1;
|
|
}
|
|
|
|
bool is_prefix(const char *pfx, const char *str)
|
|
{
|
|
if (!pfx)
|
|
return false;
|
|
if (strlen(str) < strlen(pfx))
|
|
return false;
|
|
|
|
return !memcmp(str, pfx, strlen(pfx));
|
|
}
|
|
|
|
/* Last argument MUST be NULL pointer */
|
|
int detect_common_prefix(const char *arg, ...)
|
|
{
|
|
unsigned int count = 0;
|
|
const char *ref;
|
|
char msg[256];
|
|
va_list ap;
|
|
|
|
snprintf(msg, sizeof(msg), "ambiguous prefix: '%s' could be '", arg);
|
|
va_start(ap, arg);
|
|
while ((ref = va_arg(ap, const char *))) {
|
|
if (!is_prefix(arg, ref))
|
|
continue;
|
|
count++;
|
|
if (count > 1)
|
|
strncat(msg, "' or '", sizeof(msg) - strlen(msg) - 1);
|
|
strncat(msg, ref, sizeof(msg) - strlen(msg) - 1);
|
|
}
|
|
va_end(ap);
|
|
strncat(msg, "'", sizeof(msg) - strlen(msg) - 1);
|
|
|
|
if (count >= 2) {
|
|
p_err("%s", msg);
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
|
|
{
|
|
unsigned char *data = arg;
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
const char *pfx = "";
|
|
|
|
if (!i)
|
|
/* nothing */;
|
|
else if (!(i % 16))
|
|
fprintf(f, "\n");
|
|
else if (!(i % 8))
|
|
fprintf(f, " ");
|
|
else
|
|
pfx = sep;
|
|
|
|
fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
|
|
}
|
|
}
|
|
|
|
/* Split command line into argument vector. */
|
|
static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
|
|
{
|
|
static const char ws[] = " \t\r\n";
|
|
char *cp = line;
|
|
int n_argc = 0;
|
|
|
|
while (*cp) {
|
|
/* Skip leading whitespace. */
|
|
cp += strspn(cp, ws);
|
|
|
|
if (*cp == '\0')
|
|
break;
|
|
|
|
if (n_argc >= (maxargs - 1)) {
|
|
p_err("too many arguments to command %d", cmd_nb);
|
|
return -1;
|
|
}
|
|
|
|
/* Word begins with quote. */
|
|
if (*cp == '\'' || *cp == '"') {
|
|
char quote = *cp++;
|
|
|
|
n_argv[n_argc++] = cp;
|
|
/* Find ending quote. */
|
|
cp = strchr(cp, quote);
|
|
if (!cp) {
|
|
p_err("unterminated quoted string in command %d",
|
|
cmd_nb);
|
|
return -1;
|
|
}
|
|
} else {
|
|
n_argv[n_argc++] = cp;
|
|
|
|
/* Find end of word. */
|
|
cp += strcspn(cp, ws);
|
|
if (*cp == '\0')
|
|
break;
|
|
}
|
|
|
|
/* Separate words. */
|
|
*cp++ = 0;
|
|
}
|
|
n_argv[n_argc] = NULL;
|
|
|
|
return n_argc;
|
|
}
|
|
|
|
static int do_batch(int argc, char **argv);
|
|
|
|
static const struct cmd cmds[] = {
|
|
{ "help", do_help },
|
|
{ "batch", do_batch },
|
|
{ "prog", do_prog },
|
|
{ "map", do_map },
|
|
{ "cgroup", do_cgroup },
|
|
{ "perf", do_perf },
|
|
{ "net", do_net },
|
|
{ "feature", do_feature },
|
|
{ "btf", do_btf },
|
|
{ "gen", do_gen },
|
|
{ "struct_ops", do_struct_ops },
|
|
{ "version", do_version },
|
|
{ 0 }
|
|
};
|
|
|
|
static int do_batch(int argc, char **argv)
|
|
{
|
|
char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
|
|
char *n_argv[BATCH_ARG_NB_MAX];
|
|
unsigned int lines = 0;
|
|
int n_argc;
|
|
FILE *fp;
|
|
char *cp;
|
|
int err;
|
|
int i;
|
|
|
|
if (argc < 2) {
|
|
p_err("too few parameters for batch");
|
|
return -1;
|
|
} else if (!is_prefix(*argv, "file")) {
|
|
p_err("expected 'file', got: %s", *argv);
|
|
return -1;
|
|
} else if (argc > 2) {
|
|
p_err("too many parameters for batch");
|
|
return -1;
|
|
}
|
|
NEXT_ARG();
|
|
|
|
if (!strcmp(*argv, "-"))
|
|
fp = stdin;
|
|
else
|
|
fp = fopen(*argv, "r");
|
|
if (!fp) {
|
|
p_err("Can't open file (%s): %s", *argv, strerror(errno));
|
|
return -1;
|
|
}
|
|
|
|
if (json_output)
|
|
jsonw_start_array(json_wtr);
|
|
while (fgets(buf, sizeof(buf), fp)) {
|
|
cp = strchr(buf, '#');
|
|
if (cp)
|
|
*cp = '\0';
|
|
|
|
if (strlen(buf) == sizeof(buf) - 1) {
|
|
errno = E2BIG;
|
|
break;
|
|
}
|
|
|
|
/* Append continuation lines if any (coming after a line ending
|
|
* with '\' in the batch file).
|
|
*/
|
|
while ((cp = strstr(buf, "\\\n")) != NULL) {
|
|
if (!fgets(contline, sizeof(contline), fp) ||
|
|
strlen(contline) == 0) {
|
|
p_err("missing continuation line on command %d",
|
|
lines);
|
|
err = -1;
|
|
goto err_close;
|
|
}
|
|
|
|
cp = strchr(contline, '#');
|
|
if (cp)
|
|
*cp = '\0';
|
|
|
|
if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
|
|
p_err("command %d is too long", lines);
|
|
err = -1;
|
|
goto err_close;
|
|
}
|
|
buf[strlen(buf) - 2] = '\0';
|
|
strcat(buf, contline);
|
|
}
|
|
|
|
n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
|
|
if (!n_argc)
|
|
continue;
|
|
if (n_argc < 0)
|
|
goto err_close;
|
|
|
|
if (json_output) {
|
|
jsonw_start_object(json_wtr);
|
|
jsonw_name(json_wtr, "command");
|
|
jsonw_start_array(json_wtr);
|
|
for (i = 0; i < n_argc; i++)
|
|
jsonw_string(json_wtr, n_argv[i]);
|
|
jsonw_end_array(json_wtr);
|
|
jsonw_name(json_wtr, "output");
|
|
}
|
|
|
|
err = cmd_select(cmds, n_argc, n_argv, do_help);
|
|
|
|
if (json_output)
|
|
jsonw_end_object(json_wtr);
|
|
|
|
if (err)
|
|
goto err_close;
|
|
|
|
lines++;
|
|
}
|
|
|
|
if (errno && errno != ENOENT) {
|
|
p_err("reading batch file failed: %s", strerror(errno));
|
|
err = -1;
|
|
} else {
|
|
if (!json_output)
|
|
printf("processed %d commands\n", lines);
|
|
err = 0;
|
|
}
|
|
err_close:
|
|
if (fp != stdin)
|
|
fclose(fp);
|
|
|
|
if (json_output)
|
|
jsonw_end_array(json_wtr);
|
|
|
|
return err;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
static const struct option options[] = {
|
|
{ "json", no_argument, NULL, 'j' },
|
|
{ "help", no_argument, NULL, 'h' },
|
|
{ "pretty", no_argument, NULL, 'p' },
|
|
{ "version", no_argument, NULL, 'V' },
|
|
{ "bpffs", no_argument, NULL, 'f' },
|
|
{ "mapcompat", no_argument, NULL, 'm' },
|
|
{ "nomount", no_argument, NULL, 'n' },
|
|
{ "debug", no_argument, NULL, 'd' },
|
|
{ 0 }
|
|
};
|
|
int opt, ret;
|
|
|
|
last_do_help = do_help;
|
|
pretty_output = false;
|
|
json_output = false;
|
|
show_pinned = false;
|
|
block_mount = false;
|
|
bin_name = argv[0];
|
|
|
|
hash_init(prog_table.table);
|
|
hash_init(map_table.table);
|
|
|
|
opterr = 0;
|
|
while ((opt = getopt_long(argc, argv, "Vhpjfmnd",
|
|
options, NULL)) >= 0) {
|
|
switch (opt) {
|
|
case 'V':
|
|
return do_version(argc, argv);
|
|
case 'h':
|
|
return do_help(argc, argv);
|
|
case 'p':
|
|
pretty_output = true;
|
|
/* fall through */
|
|
case 'j':
|
|
if (!json_output) {
|
|
json_wtr = jsonw_new(stdout);
|
|
if (!json_wtr) {
|
|
p_err("failed to create JSON writer");
|
|
return -1;
|
|
}
|
|
json_output = true;
|
|
}
|
|
jsonw_pretty(json_wtr, pretty_output);
|
|
break;
|
|
case 'f':
|
|
show_pinned = true;
|
|
break;
|
|
case 'm':
|
|
relaxed_maps = true;
|
|
break;
|
|
case 'n':
|
|
block_mount = true;
|
|
break;
|
|
case 'd':
|
|
libbpf_set_print(print_all_levels);
|
|
verifier_logs = true;
|
|
break;
|
|
default:
|
|
p_err("unrecognized option '%s'", argv[optind - 1]);
|
|
if (json_output)
|
|
clean_and_exit(-1);
|
|
else
|
|
usage();
|
|
}
|
|
}
|
|
|
|
argc -= optind;
|
|
argv += optind;
|
|
if (argc < 0)
|
|
usage();
|
|
|
|
ret = cmd_select(cmds, argc, argv, do_help);
|
|
|
|
if (json_output)
|
|
jsonw_destroy(&json_wtr);
|
|
|
|
if (show_pinned) {
|
|
delete_pinned_obj_table(&prog_table);
|
|
delete_pinned_obj_table(&map_table);
|
|
}
|
|
|
|
return ret;
|
|
}
|