Commit 75a1e792 authored by Quentin Monnet's avatar Quentin Monnet Committed by Daniel Borkmann

tools: bpftool: Allow all prog/map handles for pinning objects

Documentation and interactive help for bpftool have always explained
that the regular handles for programs (id|name|tag|pinned) and maps
(id|name|pinned) can be passed to the utility when attempting to pin
objects (bpftool prog pin PROG / bpftool map pin MAP).

THIS IS A LIE!! The tool actually accepts only ids, as the parsing is
done in do_pin_any() in common.c instead of reusing the parsing
functions that have long been generic for program and map handles.

Instead of fixing the doc, fix the code. It is trivial to reuse the
generic parsing, and to simplify do_pin_any() in the process.

Do not accept to pin multiple objects at the same time with
prog_parse_fds() or map_parse_fds() (this would require a more complex
syntax for passing multiple sysfs paths and validating that they
correspond to the number of e.g. programs we find for a given name or
tag).
Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20200312184608.12050-2-quentin@isovalent.com
parent b35f14f4
...@@ -211,39 +211,14 @@ int do_pin_fd(int fd, const char *name) ...@@ -211,39 +211,14 @@ int do_pin_fd(int fd, const char *name)
return err; return err;
} }
int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32)) int do_pin_any(int argc, char **argv, int (*get_fd)(int *, char ***))
{ {
unsigned int id;
char *endptr;
int err; int err;
int fd; int fd;
if (argc < 3) { fd = get_fd(&argc, &argv);
p_err("too few arguments, id ID and FILE path is required"); if (fd < 0)
return -1; return fd;
} else if (argc > 3) {
p_err("too many arguments");
return -1;
}
if (!is_prefix(*argv, "id")) {
p_err("expected 'id' got %s", *argv);
return -1;
}
NEXT_ARG();
id = strtoul(*argv, &endptr, 0);
if (*endptr) {
p_err("can't parse %s as ID", *argv);
return -1;
}
NEXT_ARG();
fd = get_fd_by_id(id);
if (fd < 0) {
p_err("can't open object by id (%u): %s", id, strerror(errno));
return -1;
}
err = do_pin_fd(fd, *argv); err = do_pin_fd(fd, *argv);
......
...@@ -146,7 +146,7 @@ char *get_fdinfo(int fd, const char *key); ...@@ -146,7 +146,7 @@ char *get_fdinfo(int fd, const char *key);
int open_obj_pinned(char *path, bool quiet); int open_obj_pinned(char *path, bool quiet);
int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type); int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type);
int mount_bpffs_for_pin(const char *name); int mount_bpffs_for_pin(const char *name);
int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32)); int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(int *, char ***));
int do_pin_fd(int fd, const char *name); int do_pin_fd(int fd, const char *name);
int do_prog(int argc, char **arg); int do_prog(int argc, char **arg);
......
...@@ -1384,7 +1384,7 @@ static int do_pin(int argc, char **argv) ...@@ -1384,7 +1384,7 @@ static int do_pin(int argc, char **argv)
{ {
int err; int err;
err = do_pin_any(argc, argv, bpf_map_get_fd_by_id); err = do_pin_any(argc, argv, map_parse_fd);
if (!err && json_output) if (!err && json_output)
jsonw_null(json_wtr); jsonw_null(json_wtr);
return err; return err;
......
...@@ -813,7 +813,7 @@ static int do_pin(int argc, char **argv) ...@@ -813,7 +813,7 @@ static int do_pin(int argc, char **argv)
{ {
int err; int err;
err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id); err = do_pin_any(argc, argv, prog_parse_fd);
if (!err && json_output) if (!err && json_output)
jsonw_null(json_wtr); jsonw_null(json_wtr);
return err; return err;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment