2018-12-13 10:59:25 +07:00
|
|
|
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
|
|
|
|
/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
|
2017-10-05 10:10:04 +07:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2017-10-23 23:24:06 +07:00
|
|
|
#include <getopt.h>
|
2017-10-05 10:10:04 +07:00
|
|
|
#include <linux/bpf.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-01-20 20:06:46 +07:00
|
|
|
#include <bpf/bpf.h>
|
|
|
|
#include <bpf/libbpf.h>
|
2017-10-05 10:10:04 +07:00
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
2018-03-02 11:20:09 +07:00
|
|
|
#define BATCH_LINE_LEN_MAX 65536
|
|
|
|
#define BATCH_ARG_NB_MAX 4096
|
|
|
|
|
2017-10-05 10:10:04 +07:00
|
|
|
const char *bin_name;
|
|
|
|
static int last_argc;
|
|
|
|
static char **last_argv;
|
|
|
|
static int (*last_do_help)(int argc, char **argv);
|
2017-10-23 23:24:07 +07:00
|
|
|
json_writer_t *json_wtr;
|
|
|
|
bool pretty_output;
|
|
|
|
bool json_output;
|
2017-11-08 11:55:49 +07:00
|
|
|
bool show_pinned;
|
2018-12-18 17:13:19 +07:00
|
|
|
bool block_mount;
|
2019-05-24 17:36:48 +07:00
|
|
|
bool verifier_logs;
|
2019-10-08 05:56:04 +07:00
|
|
|
bool relaxed_maps;
|
tools: bpftool: show filenames of pinned objects
Added support to show filenames of pinned objects.
For example:
root@test# ./bpftool prog
3: tracepoint name tracepoint__irq tag f677a7dd722299a3
loaded_at Oct 26/11:39 uid 0
xlated 160B not jited memlock 4096B map_ids 4
pinned /sys/fs/bpf/softirq_prog
4: tracepoint name tracepoint__irq tag ea5dc530d00b92b6
loaded_at Oct 26/11:39 uid 0
xlated 392B not jited memlock 4096B map_ids 4,6
root@test# ./bpftool --json --pretty prog
[{
"id": 3,
"type": "tracepoint",
"name": "tracepoint__irq",
"tag": "f677a7dd722299a3",
"loaded_at": "Oct 26/11:39",
"uid": 0,
"bytes_xlated": 160,
"jited": false,
"bytes_memlock": 4096,
"map_ids": [4
],
"pinned": ["/sys/fs/bpf/softirq_prog"
]
},{
"id": 4,
"type": "tracepoint",
"name": "tracepoint__irq",
"tag": "ea5dc530d00b92b6",
"loaded_at": "Oct 26/11:39",
"uid": 0,
"bytes_xlated": 392,
"jited": false,
"bytes_memlock": 4096,
"map_ids": [4,6
],
"pinned": []
}
]
root@test# ./bpftool map
4: hash name start flags 0x0
key 4B value 16B max_entries 10240 memlock 1003520B
pinned /sys/fs/bpf/softirq_map1
5: hash name iptr flags 0x0
key 4B value 8B max_entries 10240 memlock 921600B
root@test# ./bpftool --json --pretty map
[{
"id": 4,
"type": "hash",
"name": "start",
"flags": 0,
"bytes_key": 4,
"bytes_value": 16,
"max_entries": 10240,
"bytes_memlock": 1003520,
"pinned": ["/sys/fs/bpf/softirq_map1"
]
},{
"id": 5,
"type": "hash",
"name": "iptr",
"flags": 0,
"bytes_key": 4,
"bytes_value": 8,
"max_entries": 10240,
"bytes_memlock": 921600,
"pinned": []
}
]
Signed-off-by: Prashant Bhole <bhole_prashant_q7@lab.ntt.co.jp>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-11-08 11:55:48 +07:00
|
|
|
struct pinned_obj_table prog_table;
|
|
|
|
struct pinned_obj_table map_table;
|
2017-10-05 10:10:04 +07:00
|
|
|
|
2017-11-29 08:44:29 +07:00
|
|
|
static void __noreturn clean_and_exit(int i)
|
|
|
|
{
|
|
|
|
if (json_output)
|
|
|
|
jsonw_destroy(&json_wtr);
|
|
|
|
|
|
|
|
exit(i);
|
|
|
|
}
|
|
|
|
|
2017-10-05 10:10:04 +07:00
|
|
|
void usage(void)
|
|
|
|
{
|
|
|
|
last_do_help(last_argc - 1, last_argv + 1);
|
|
|
|
|
2017-11-29 08:44:29 +07:00
|
|
|
clean_and_exit(-1);
|
2017-10-05 10:10:04 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int do_help(int argc, char **argv)
|
|
|
|
{
|
2017-10-23 23:24:14 +07:00
|
|
|
if (json_output) {
|
|
|
|
jsonw_null(json_wtr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-05 10:10:04 +07:00
|
|
|
fprintf(stderr,
|
2017-10-23 23:24:16 +07:00
|
|
|
"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
|
2017-10-05 10:10:04 +07:00
|
|
|
" %s batch file FILE\n"
|
2017-10-20 05:46:26 +07:00
|
|
|
" %s version\n"
|
2017-10-05 10:10:04 +07:00
|
|
|
"\n"
|
bpftool: Add skeleton codegen command
Add `bpftool gen skeleton` command, which takes in compiled BPF .o object file
and dumps a BPF skeleton struct and related code to work with that skeleton.
Skeleton itself is tailored to a specific structure of provided BPF object
file, containing accessors (just plain struct fields) for every map and
program, as well as dedicated space for bpf_links. If BPF program is using
global variables, corresponding structure definitions of compatible memory
layout are emitted as well, making it possible to initialize and subsequently
read/update global variables values using simple and clear C syntax for
accessing fields. This skeleton majorly improves usability of
opening/loading/attaching of BPF object, as well as interacting with it
throughout the lifetime of loaded BPF object.
Generated skeleton struct has the following structure:
struct <object-name> {
/* used by libbpf's skeleton API */
struct bpf_object_skeleton *skeleton;
/* bpf_object for libbpf APIs */
struct bpf_object *obj;
struct {
/* for every defined map in BPF object: */
struct bpf_map *<map-name>;
} maps;
struct {
/* for every program in BPF object: */
struct bpf_program *<program-name>;
} progs;
struct {
/* for every program in BPF object: */
struct bpf_link *<program-name>;
} links;
/* for every present global data section: */
struct <object-name>__<one of bss, data, or rodata> {
/* memory layout of corresponding data section,
* with every defined variable represented as a struct field
* with exactly the same type, but without const/volatile
* modifiers, e.g.:
*/
int *my_var_1;
...
} *<one of bss, data, or rodata>;
};
This provides great usability improvements:
- no need to look up maps and programs by name, instead just
my_obj->maps.my_map or my_obj->progs.my_prog would give necessary
bpf_map/bpf_program pointers, which user can pass to existing libbpf APIs;
- pre-defined places for bpf_links, which will be automatically populated for
program types that libbpf knows how to attach automatically (currently
tracepoints, kprobe/kretprobe, raw tracepoint and tracing programs). On
tearing down skeleton, all active bpf_links will be destroyed (meaning BPF
programs will be detached, if they are attached). For cases in which libbpf
doesn't know how to auto-attach BPF program, user can manually create link
after loading skeleton and they will be auto-detached on skeleton
destruction:
my_obj->links.my_fancy_prog = bpf_program__attach_cgroup_whatever(
my_obj->progs.my_fancy_prog, <whatever extra param);
- it's extremely easy and convenient to work with global data from userspace
now. Both for read-only and read/write variables, it's possible to
pre-initialize them before skeleton is loaded:
skel = my_obj__open(raw_embed_data);
my_obj->rodata->my_var = 123;
my_obj__load(skel); /* 123 will be initialization value for my_var */
After load, if kernel supports mmap() for BPF arrays, user can still read
(and write for .bss and .data) variables values, but at that point it will
be directly mmap()-ed to BPF array, backing global variables. This allows to
seamlessly exchange data with BPF side. From userspace program's POV, all
the pointers and memory contents stay the same, but mapped kernel memory
changes to point to created map.
If kernel doesn't yet support mmap() for BPF arrays, it's still possible to
use those data section structs to pre-initialize .bss, .data, and .rodata,
but after load their pointers will be reset to NULL, allowing user code to
gracefully handle this condition, if necessary.
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20191214014341.3442258-14-andriin@fb.com
2019-12-14 08:43:37 +07:00
|
|
|
" OBJECT := { prog | map | cgroup | perf | net | feature | btf | gen }\n"
|
2017-10-23 23:24:16 +07:00
|
|
|
" " HELP_SPEC_OPTIONS "\n"
|
|
|
|
"",
|
2017-10-20 05:46:26 +07:00
|
|
|
bin_name, bin_name, bin_name);
|
2017-10-05 10:10:04 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-20 05:46:26 +07:00
|
|
|
static int do_version(int argc, char **argv)
|
|
|
|
{
|
2017-10-23 23:24:14 +07:00
|
|
|
if (json_output) {
|
|
|
|
jsonw_start_object(json_wtr);
|
|
|
|
jsonw_name(json_wtr, "version");
|
2017-12-28 02:16:28 +07:00
|
|
|
jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
|
2017-10-23 23:24:14 +07:00
|
|
|
jsonw_end_object(json_wtr);
|
|
|
|
} else {
|
2017-12-28 02:16:28 +07:00
|
|
|
printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
|
2017-10-23 23:24:14 +07:00
|
|
|
}
|
2017-10-20 05:46:26 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-24 17:36:46 +07:00
|
|
|
static int __printf(2, 0)
|
|
|
|
print_all_levels(__maybe_unused enum libbpf_print_level level,
|
|
|
|
const char *format, va_list args)
|
|
|
|
{
|
|
|
|
return vfprintf(stderr, format, args);
|
|
|
|
}
|
|
|
|
|
2017-10-05 10:10:04 +07:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2019-07-06 00:54:33 +07:00
|
|
|
/* 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) {
|
2019-08-15 21:32:19 +07:00
|
|
|
p_err("%s", msg);
|
2019-07-06 00:54:33 +07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-20 05:46:19 +07:00
|
|
|
void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
|
2017-10-05 10:10:04 +07:00
|
|
|
{
|
|
|
|
unsigned char *data = arg;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
const char *pfx = "";
|
|
|
|
|
|
|
|
if (!i)
|
|
|
|
/* nothing */;
|
|
|
|
else if (!(i % 16))
|
2017-10-20 05:46:19 +07:00
|
|
|
fprintf(f, "\n");
|
2017-10-05 10:10:04 +07:00
|
|
|
else if (!(i % 8))
|
2017-10-20 05:46:19 +07:00
|
|
|
fprintf(f, " ");
|
2017-10-05 10:10:04 +07:00
|
|
|
else
|
|
|
|
pfx = sep;
|
|
|
|
|
2017-10-20 05:46:19 +07:00
|
|
|
fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
|
2017-10-05 10:10:04 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 11:20:11 +07:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2017-10-05 10:10:04 +07:00
|
|
|
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 },
|
2017-12-13 22:18:54 +07:00
|
|
|
{ "cgroup", do_cgroup },
|
tools/bpftool: add perf subcommand
The new command "bpftool perf [show | list]" will traverse
all processes under /proc, and if any fd is associated
with a perf event, it will print out related perf event
information. Documentation is also added.
Below is an example to show the results using bcc commands.
Running the following 4 bcc commands:
kprobe: trace.py '__x64_sys_nanosleep'
kretprobe: trace.py 'r::__x64_sys_nanosleep'
tracepoint: trace.py 't:syscalls:sys_enter_nanosleep'
uprobe: trace.py 'p:/home/yhs/a.out:main'
The bpftool command line and result:
$ bpftool perf
pid 21711 fd 5: prog_id 5 kprobe func __x64_sys_write offset 0
pid 21765 fd 5: prog_id 7 kretprobe func __x64_sys_nanosleep offset 0
pid 21767 fd 5: prog_id 8 tracepoint sys_enter_nanosleep
pid 21800 fd 5: prog_id 9 uprobe filename /home/yhs/a.out offset 1159
$ bpftool -j perf
[{"pid":21711,"fd":5,"prog_id":5,"fd_type":"kprobe","func":"__x64_sys_write","offset":0}, \
{"pid":21765,"fd":5,"prog_id":7,"fd_type":"kretprobe","func":"__x64_sys_nanosleep","offset":0}, \
{"pid":21767,"fd":5,"prog_id":8,"fd_type":"tracepoint","tracepoint":"sys_enter_nanosleep"}, \
{"pid":21800,"fd":5,"prog_id":9,"fd_type":"uprobe","filename":"/home/yhs/a.out","offset":1159}]
$ bpftool prog
5: kprobe name probe___x64_sys tag e495a0c82f2c7a8d gpl
loaded_at 2018-05-15T04:46:37-0700 uid 0
xlated 200B not jited memlock 4096B map_ids 4
7: kprobe name probe___x64_sys tag f2fdee479a503abf gpl
loaded_at 2018-05-15T04:48:32-0700 uid 0
xlated 200B not jited memlock 4096B map_ids 7
8: tracepoint name tracepoint__sys tag 5390badef2395fcf gpl
loaded_at 2018-05-15T04:48:48-0700 uid 0
xlated 200B not jited memlock 4096B map_ids 8
9: kprobe name probe_main_1 tag 0a87bdc2e2953b6d gpl
loaded_at 2018-05-15T04:49:52-0700 uid 0
xlated 200B not jited memlock 4096B map_ids 9
$ ps ax | grep "python ./trace.py"
21711 pts/0 T 0:03 python ./trace.py __x64_sys_write
21765 pts/0 S+ 0:00 python ./trace.py r::__x64_sys_nanosleep
21767 pts/2 S+ 0:00 python ./trace.py t:syscalls:sys_enter_nanosleep
21800 pts/3 S+ 0:00 python ./trace.py p:/home/yhs/a.out:main
22374 pts/1 S+ 0:00 grep --color=auto python ./trace.py
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-05-25 01:21:58 +07:00
|
|
|
{ "perf", do_perf },
|
tools/bpf: bpftool: add net support
Add "bpftool net" support. Networking devices are enumerated
to dump device index/name associated with xdp progs.
For each networking device, tc classes and qdiscs are enumerated
in order to check their bpf filters.
In addition, root handle and clsact ingress/egress are also checked for
bpf filters. Not all filter information is printed out. Only ifindex,
kind, filter name, prog_id and tag are printed out, which are good
enough to show attachment information. If the filter action
is a bpf action, its bpf program id, bpf name and tag will be
printed out as well.
For example,
$ ./bpftool net
xdp [
ifindex 2 devname eth0 prog_id 198
]
tc_filters [
ifindex 2 kind qdisc_htb name prefix_matcher.o:[cls_prefix_matcher_htb]
prog_id 111727 tag d08fe3b4319bc2fd act []
ifindex 2 kind qdisc_clsact_ingress name fbflow_icmp
prog_id 130246 tag 3f265c7f26db62c9 act []
ifindex 2 kind qdisc_clsact_egress name prefix_matcher.o:[cls_prefix_matcher_clsact]
prog_id 111726 tag 99a197826974c876
ifindex 2 kind qdisc_clsact_egress name cls_fg_dscp
prog_id 108619 tag dc4630674fd72dcc act []
ifindex 2 kind qdisc_clsact_egress name fbflow_egress
prog_id 130245 tag 72d2d830d6888d2c
]
$ ./bpftool -jp net
[{
"xdp": [{
"ifindex": 2,
"devname": "eth0",
"prog_id": 198
}
],
"tc_filters": [{
"ifindex": 2,
"kind": "qdisc_htb",
"name": "prefix_matcher.o:[cls_prefix_matcher_htb]",
"prog_id": 111727,
"tag": "d08fe3b4319bc2fd",
"act": []
},{
"ifindex": 2,
"kind": "qdisc_clsact_ingress",
"name": "fbflow_icmp",
"prog_id": 130246,
"tag": "3f265c7f26db62c9",
"act": []
},{
"ifindex": 2,
"kind": "qdisc_clsact_egress",
"name": "prefix_matcher.o:[cls_prefix_matcher_clsact]",
"prog_id": 111726,
"tag": "99a197826974c876"
},{
"ifindex": 2,
"kind": "qdisc_clsact_egress",
"name": "cls_fg_dscp",
"prog_id": 108619,
"tag": "dc4630674fd72dcc",
"act": []
},{
"ifindex": 2,
"kind": "qdisc_clsact_egress",
"name": "fbflow_egress",
"prog_id": 130245,
"tag": "72d2d830d6888d2c"
}
]
}
]
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-09-06 06:58:06 +07:00
|
|
|
{ "net", do_net },
|
2019-01-17 22:27:50 +07:00
|
|
|
{ "feature", do_feature },
|
2019-04-26 05:30:08 +07:00
|
|
|
{ "btf", do_btf },
|
bpftool: Add skeleton codegen command
Add `bpftool gen skeleton` command, which takes in compiled BPF .o object file
and dumps a BPF skeleton struct and related code to work with that skeleton.
Skeleton itself is tailored to a specific structure of provided BPF object
file, containing accessors (just plain struct fields) for every map and
program, as well as dedicated space for bpf_links. If BPF program is using
global variables, corresponding structure definitions of compatible memory
layout are emitted as well, making it possible to initialize and subsequently
read/update global variables values using simple and clear C syntax for
accessing fields. This skeleton majorly improves usability of
opening/loading/attaching of BPF object, as well as interacting with it
throughout the lifetime of loaded BPF object.
Generated skeleton struct has the following structure:
struct <object-name> {
/* used by libbpf's skeleton API */
struct bpf_object_skeleton *skeleton;
/* bpf_object for libbpf APIs */
struct bpf_object *obj;
struct {
/* for every defined map in BPF object: */
struct bpf_map *<map-name>;
} maps;
struct {
/* for every program in BPF object: */
struct bpf_program *<program-name>;
} progs;
struct {
/* for every program in BPF object: */
struct bpf_link *<program-name>;
} links;
/* for every present global data section: */
struct <object-name>__<one of bss, data, or rodata> {
/* memory layout of corresponding data section,
* with every defined variable represented as a struct field
* with exactly the same type, but without const/volatile
* modifiers, e.g.:
*/
int *my_var_1;
...
} *<one of bss, data, or rodata>;
};
This provides great usability improvements:
- no need to look up maps and programs by name, instead just
my_obj->maps.my_map or my_obj->progs.my_prog would give necessary
bpf_map/bpf_program pointers, which user can pass to existing libbpf APIs;
- pre-defined places for bpf_links, which will be automatically populated for
program types that libbpf knows how to attach automatically (currently
tracepoints, kprobe/kretprobe, raw tracepoint and tracing programs). On
tearing down skeleton, all active bpf_links will be destroyed (meaning BPF
programs will be detached, if they are attached). For cases in which libbpf
doesn't know how to auto-attach BPF program, user can manually create link
after loading skeleton and they will be auto-detached on skeleton
destruction:
my_obj->links.my_fancy_prog = bpf_program__attach_cgroup_whatever(
my_obj->progs.my_fancy_prog, <whatever extra param);
- it's extremely easy and convenient to work with global data from userspace
now. Both for read-only and read/write variables, it's possible to
pre-initialize them before skeleton is loaded:
skel = my_obj__open(raw_embed_data);
my_obj->rodata->my_var = 123;
my_obj__load(skel); /* 123 will be initialization value for my_var */
After load, if kernel supports mmap() for BPF arrays, user can still read
(and write for .bss and .data) variables values, but at that point it will
be directly mmap()-ed to BPF array, backing global variables. This allows to
seamlessly exchange data with BPF side. From userspace program's POV, all
the pointers and memory contents stay the same, but mapped kernel memory
changes to point to created map.
If kernel doesn't yet support mmap() for BPF arrays, it's still possible to
use those data section structs to pre-initialize .bss, .data, and .rodata,
but after load their pointers will be reset to NULL, allowing user code to
gracefully handle this condition, if necessary.
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20191214014341.3442258-14-andriin@fb.com
2019-12-14 08:43:37 +07:00
|
|
|
{ "gen", do_gen },
|
2017-10-20 05:46:26 +07:00
|
|
|
{ "version", do_version },
|
2017-10-05 10:10:04 +07:00
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static int do_batch(int argc, char **argv)
|
|
|
|
{
|
2018-03-02 11:20:09 +07:00
|
|
|
char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
|
|
|
|
char *n_argv[BATCH_ARG_NB_MAX];
|
2017-10-05 10:10:04 +07:00
|
|
|
unsigned int lines = 0;
|
|
|
|
int n_argc;
|
|
|
|
FILE *fp;
|
2018-03-02 11:20:08 +07:00
|
|
|
char *cp;
|
2017-10-05 10:10:04 +07:00
|
|
|
int err;
|
2017-10-23 23:24:12 +07:00
|
|
|
int i;
|
2017-10-05 10:10:04 +07:00
|
|
|
|
|
|
|
if (argc < 2) {
|
2017-10-23 23:24:13 +07:00
|
|
|
p_err("too few parameters for batch");
|
2017-10-05 10:10:04 +07:00
|
|
|
return -1;
|
|
|
|
} else if (!is_prefix(*argv, "file")) {
|
2017-10-23 23:24:13 +07:00
|
|
|
p_err("expected 'file', got: %s", *argv);
|
2017-10-05 10:10:04 +07:00
|
|
|
return -1;
|
|
|
|
} else if (argc > 2) {
|
2017-10-23 23:24:13 +07:00
|
|
|
p_err("too many parameters for batch");
|
2017-10-05 10:10:04 +07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
NEXT_ARG();
|
|
|
|
|
2018-03-02 11:20:10 +07:00
|
|
|
if (!strcmp(*argv, "-"))
|
|
|
|
fp = stdin;
|
|
|
|
else
|
|
|
|
fp = fopen(*argv, "r");
|
2017-10-05 10:10:04 +07:00
|
|
|
if (!fp) {
|
2017-10-23 23:24:13 +07:00
|
|
|
p_err("Can't open file (%s): %s", *argv, strerror(errno));
|
2017-10-05 10:10:04 +07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-23 23:24:12 +07:00
|
|
|
if (json_output)
|
|
|
|
jsonw_start_array(json_wtr);
|
2017-10-05 10:10:04 +07:00
|
|
|
while (fgets(buf, sizeof(buf), fp)) {
|
2018-03-02 11:20:08 +07:00
|
|
|
cp = strchr(buf, '#');
|
|
|
|
if (cp)
|
|
|
|
*cp = '\0';
|
|
|
|
|
2017-10-05 10:10:04 +07:00
|
|
|
if (strlen(buf) == sizeof(buf) - 1) {
|
|
|
|
errno = E2BIG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-03-02 11:20:09 +07:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2018-03-02 11:20:11 +07:00
|
|
|
n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
|
2017-10-05 10:10:04 +07:00
|
|
|
if (!n_argc)
|
|
|
|
continue;
|
2018-03-02 11:20:11 +07:00
|
|
|
if (n_argc < 0)
|
|
|
|
goto err_close;
|
2017-10-05 10:10:04 +07:00
|
|
|
|
2017-10-23 23:24:12 +07:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2017-10-05 10:10:04 +07:00
|
|
|
err = cmd_select(cmds, n_argc, n_argv, do_help);
|
2017-10-23 23:24:12 +07:00
|
|
|
|
|
|
|
if (json_output)
|
|
|
|
jsonw_end_object(json_wtr);
|
|
|
|
|
2017-10-05 10:10:04 +07:00
|
|
|
if (err)
|
|
|
|
goto err_close;
|
|
|
|
|
|
|
|
lines++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno && errno != ENOENT) {
|
2018-02-15 13:42:55 +07:00
|
|
|
p_err("reading batch file failed: %s", strerror(errno));
|
2017-10-05 10:10:04 +07:00
|
|
|
err = -1;
|
|
|
|
} else {
|
2018-10-21 05:01:49 +07:00
|
|
|
if (!json_output)
|
|
|
|
printf("processed %d commands\n", lines);
|
2017-10-05 10:10:04 +07:00
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
err_close:
|
2018-03-02 11:20:10 +07:00
|
|
|
if (fp != stdin)
|
|
|
|
fclose(fp);
|
2017-10-05 10:10:04 +07:00
|
|
|
|
2017-10-23 23:24:12 +07:00
|
|
|
if (json_output)
|
|
|
|
jsonw_end_array(json_wtr);
|
|
|
|
|
2017-10-05 10:10:04 +07:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2017-10-23 23:24:06 +07:00
|
|
|
static const struct option options[] = {
|
2017-10-23 23:24:07 +07:00
|
|
|
{ "json", no_argument, NULL, 'j' },
|
2017-10-23 23:24:06 +07:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2017-10-23 23:24:07 +07:00
|
|
|
{ "pretty", no_argument, NULL, 'p' },
|
2017-10-23 23:24:06 +07:00
|
|
|
{ "version", no_argument, NULL, 'V' },
|
2017-11-08 11:55:49 +07:00
|
|
|
{ "bpffs", no_argument, NULL, 'f' },
|
2018-10-16 01:19:55 +07:00
|
|
|
{ "mapcompat", no_argument, NULL, 'm' },
|
2018-12-18 17:13:19 +07:00
|
|
|
{ "nomount", no_argument, NULL, 'n' },
|
2019-05-24 17:36:46 +07:00
|
|
|
{ "debug", no_argument, NULL, 'd' },
|
2017-10-23 23:24:06 +07:00
|
|
|
{ 0 }
|
|
|
|
};
|
2017-10-23 23:24:07 +07:00
|
|
|
int opt, ret;
|
2017-10-23 23:24:06 +07:00
|
|
|
|
|
|
|
last_do_help = do_help;
|
2017-10-23 23:24:07 +07:00
|
|
|
pretty_output = false;
|
|
|
|
json_output = false;
|
2017-11-08 11:55:49 +07:00
|
|
|
show_pinned = false;
|
2018-12-18 17:13:19 +07:00
|
|
|
block_mount = false;
|
2017-10-05 10:10:04 +07:00
|
|
|
bin_name = argv[0];
|
2017-10-23 23:24:06 +07:00
|
|
|
|
tools: bpftool: show filenames of pinned objects
Added support to show filenames of pinned objects.
For example:
root@test# ./bpftool prog
3: tracepoint name tracepoint__irq tag f677a7dd722299a3
loaded_at Oct 26/11:39 uid 0
xlated 160B not jited memlock 4096B map_ids 4
pinned /sys/fs/bpf/softirq_prog
4: tracepoint name tracepoint__irq tag ea5dc530d00b92b6
loaded_at Oct 26/11:39 uid 0
xlated 392B not jited memlock 4096B map_ids 4,6
root@test# ./bpftool --json --pretty prog
[{
"id": 3,
"type": "tracepoint",
"name": "tracepoint__irq",
"tag": "f677a7dd722299a3",
"loaded_at": "Oct 26/11:39",
"uid": 0,
"bytes_xlated": 160,
"jited": false,
"bytes_memlock": 4096,
"map_ids": [4
],
"pinned": ["/sys/fs/bpf/softirq_prog"
]
},{
"id": 4,
"type": "tracepoint",
"name": "tracepoint__irq",
"tag": "ea5dc530d00b92b6",
"loaded_at": "Oct 26/11:39",
"uid": 0,
"bytes_xlated": 392,
"jited": false,
"bytes_memlock": 4096,
"map_ids": [4,6
],
"pinned": []
}
]
root@test# ./bpftool map
4: hash name start flags 0x0
key 4B value 16B max_entries 10240 memlock 1003520B
pinned /sys/fs/bpf/softirq_map1
5: hash name iptr flags 0x0
key 4B value 8B max_entries 10240 memlock 921600B
root@test# ./bpftool --json --pretty map
[{
"id": 4,
"type": "hash",
"name": "start",
"flags": 0,
"bytes_key": 4,
"bytes_value": 16,
"max_entries": 10240,
"bytes_memlock": 1003520,
"pinned": ["/sys/fs/bpf/softirq_map1"
]
},{
"id": 5,
"type": "hash",
"name": "iptr",
"flags": 0,
"bytes_key": 4,
"bytes_value": 8,
"max_entries": 10240,
"bytes_memlock": 921600,
"pinned": []
}
]
Signed-off-by: Prashant Bhole <bhole_prashant_q7@lab.ntt.co.jp>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-11-08 11:55:48 +07:00
|
|
|
hash_init(prog_table.table);
|
|
|
|
hash_init(map_table.table);
|
|
|
|
|
2017-11-29 08:44:30 +07:00
|
|
|
opterr = 0;
|
2019-05-24 17:36:46 +07:00
|
|
|
while ((opt = getopt_long(argc, argv, "Vhpjfmnd",
|
2017-10-23 23:24:06 +07:00
|
|
|
options, NULL)) >= 0) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'V':
|
|
|
|
return do_version(argc, argv);
|
|
|
|
case 'h':
|
|
|
|
return do_help(argc, argv);
|
2017-10-23 23:24:07 +07:00
|
|
|
case 'p':
|
|
|
|
pretty_output = true;
|
|
|
|
/* fall through */
|
|
|
|
case 'j':
|
2017-11-29 08:44:28 +07:00
|
|
|
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);
|
2017-10-23 23:24:07 +07:00
|
|
|
break;
|
2017-11-08 11:55:49 +07:00
|
|
|
case 'f':
|
|
|
|
show_pinned = true;
|
|
|
|
break;
|
2018-10-16 01:19:55 +07:00
|
|
|
case 'm':
|
2019-10-08 05:56:04 +07:00
|
|
|
relaxed_maps = true;
|
2018-10-16 01:19:55 +07:00
|
|
|
break;
|
2018-12-18 17:13:19 +07:00
|
|
|
case 'n':
|
|
|
|
block_mount = true;
|
|
|
|
break;
|
2019-05-24 17:36:46 +07:00
|
|
|
case 'd':
|
|
|
|
libbpf_set_print(print_all_levels);
|
2019-05-24 17:36:48 +07:00
|
|
|
verifier_logs = true;
|
2019-05-24 17:36:46 +07:00
|
|
|
break;
|
2017-10-23 23:24:06 +07:00
|
|
|
default:
|
2017-11-29 08:44:30 +07:00
|
|
|
p_err("unrecognized option '%s'", argv[optind - 1]);
|
|
|
|
if (json_output)
|
|
|
|
clean_and_exit(-1);
|
|
|
|
else
|
|
|
|
usage();
|
2017-10-23 23:24:06 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc < 0)
|
|
|
|
usage();
|
2017-10-05 10:10:04 +07:00
|
|
|
|
2017-10-23 23:24:07 +07:00
|
|
|
ret = cmd_select(cmds, argc, argv, do_help);
|
|
|
|
|
|
|
|
if (json_output)
|
|
|
|
jsonw_destroy(&json_wtr);
|
|
|
|
|
2017-11-08 11:55:49 +07:00
|
|
|
if (show_pinned) {
|
|
|
|
delete_pinned_obj_table(&prog_table);
|
|
|
|
delete_pinned_obj_table(&map_table);
|
|
|
|
}
|
tools: bpftool: show filenames of pinned objects
Added support to show filenames of pinned objects.
For example:
root@test# ./bpftool prog
3: tracepoint name tracepoint__irq tag f677a7dd722299a3
loaded_at Oct 26/11:39 uid 0
xlated 160B not jited memlock 4096B map_ids 4
pinned /sys/fs/bpf/softirq_prog
4: tracepoint name tracepoint__irq tag ea5dc530d00b92b6
loaded_at Oct 26/11:39 uid 0
xlated 392B not jited memlock 4096B map_ids 4,6
root@test# ./bpftool --json --pretty prog
[{
"id": 3,
"type": "tracepoint",
"name": "tracepoint__irq",
"tag": "f677a7dd722299a3",
"loaded_at": "Oct 26/11:39",
"uid": 0,
"bytes_xlated": 160,
"jited": false,
"bytes_memlock": 4096,
"map_ids": [4
],
"pinned": ["/sys/fs/bpf/softirq_prog"
]
},{
"id": 4,
"type": "tracepoint",
"name": "tracepoint__irq",
"tag": "ea5dc530d00b92b6",
"loaded_at": "Oct 26/11:39",
"uid": 0,
"bytes_xlated": 392,
"jited": false,
"bytes_memlock": 4096,
"map_ids": [4,6
],
"pinned": []
}
]
root@test# ./bpftool map
4: hash name start flags 0x0
key 4B value 16B max_entries 10240 memlock 1003520B
pinned /sys/fs/bpf/softirq_map1
5: hash name iptr flags 0x0
key 4B value 8B max_entries 10240 memlock 921600B
root@test# ./bpftool --json --pretty map
[{
"id": 4,
"type": "hash",
"name": "start",
"flags": 0,
"bytes_key": 4,
"bytes_value": 16,
"max_entries": 10240,
"bytes_memlock": 1003520,
"pinned": ["/sys/fs/bpf/softirq_map1"
]
},{
"id": 5,
"type": "hash",
"name": "iptr",
"flags": 0,
"bytes_key": 4,
"bytes_value": 8,
"max_entries": 10240,
"bytes_memlock": 921600,
"pinned": []
}
]
Signed-off-by: Prashant Bhole <bhole_prashant_q7@lab.ntt.co.jp>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-11-08 11:55:48 +07:00
|
|
|
|
2017-10-23 23:24:07 +07:00
|
|
|
return ret;
|
2017-10-05 10:10:04 +07:00
|
|
|
}
|