Centos-kernel-stream-9/tools/bpf/bpftool/iter.c

124 lines
2.3 KiB
C
Raw Normal View History

2020-05-09 17:59:20 +00:00
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
// Copyright (C) 2020 Facebook
#ifndef _GNU_SOURCE
2020-05-09 17:59:20 +00:00
#define _GNU_SOURCE
#endif
#include <errno.h>
#include <unistd.h>
2020-05-09 17:59:20 +00:00
#include <linux/err.h>
#include <bpf/libbpf.h>
#include "main.h"
static int do_pin(int argc, char **argv)
{
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, iter_opts);
union bpf_iter_link_info linfo;
2020-05-09 17:59:20 +00:00
const char *objfile, *path;
struct bpf_program *prog;
struct bpf_object *obj;
struct bpf_link *link;
int err = -1, map_fd = -1;
2020-05-09 17:59:20 +00:00
if (!REQ_ARGS(2))
usage();
objfile = GET_ARG();
path = GET_ARG();
/* optional arguments */
if (argc) {
if (is_prefix(*argv, "map")) {
NEXT_ARG();
if (!REQ_ARGS(2)) {
p_err("incorrect map spec");
return -1;
}
map_fd = map_parse_fd(&argc, &argv);
if (map_fd < 0)
return -1;
memset(&linfo, 0, sizeof(linfo));
linfo.map.map_fd = map_fd;
iter_opts.link_info = &linfo;
iter_opts.link_info_len = sizeof(linfo);
}
}
2020-05-09 17:59:20 +00:00
obj = bpf_object__open(objfile);
if (!obj) {
err = -errno;
2020-05-09 17:59:20 +00:00
p_err("can't open objfile %s", objfile);
goto close_map_fd;
2020-05-09 17:59:20 +00:00
}
err = bpf_object__load(obj);
if (err) {
p_err("can't load objfile %s", objfile);
goto close_obj;
}
prog = bpf_object__next_program(obj, NULL);
2020-05-09 17:59:20 +00:00
if (!prog) {
err = -errno;
2020-05-09 17:59:20 +00:00
p_err("can't find bpf program in objfile %s", objfile);
goto close_obj;
}
link = bpf_program__attach_iter(prog, &iter_opts);
if (!link) {
err = -errno;
2020-05-09 17:59:20 +00:00
p_err("attach_iter failed for program %s",
bpf_program__name(prog));
goto close_obj;
}
bpftool: Mount bpffs on provided dir instead of parent dir JIRA: https://issues.redhat.com/browse/RHEL-30773 commit 478a535ae54ad3831371904d93b5dfc403222e17 Author: Sahil Siddiq <icegambit91@gmail.com> Date: Fri Apr 5 00:52:19 2024 +0530 bpftool: Mount bpffs on provided dir instead of parent dir When pinning programs/objects under PATH (eg: during "bpftool prog loadall") the bpffs is mounted on the parent dir of PATH in the following situations: - the given dir exists but it is not bpffs. - the given dir doesn't exist and the parent dir is not bpffs. Mounting on the parent dir can also have the unintentional side- effect of hiding other files located under the parent dir. If the given dir exists but is not bpffs, then the bpffs should be mounted on the given dir and not its parent dir. Similarly, if the given dir doesn't exist and its parent dir is not bpffs, then the given dir should be created and the bpffs should be mounted on this new dir. Fixes: 2a36c26fe3b8 ("bpftool: Support bpffs mountpoint as pin path for prog loadall") Signed-off-by: Sahil Siddiq <icegambit91@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/2da44d24-74ae-a564-1764-afccf395eeec@isovalent.com/T/#t Link: https://lore.kernel.org/bpf/20240404192219.52373-1-icegambit91@gmail.com Closes: https://github.com/libbpf/bpftool/issues/100 Changes since v1: - Split "mount_bpffs_for_pin" into two functions. This is done to improve maintainability and readability. Changes since v2: - mount_bpffs_for_pin: rename to "create_and_mount_bpffs_dir". - mount_bpffs_given_file: rename to "mount_bpffs_given_file". - create_and_mount_bpffs_dir: - introduce "dir_exists" boolean. - remove new dir if "mnt_fs" fails. - improve error handling and error messages. Changes since v3: - Rectify function name. - Improve error messages and formatting. - mount_bpffs_for_file: - Check if dir exists before block_mount check. Changes since v4: - Use strdup instead of strcpy. - create_and_mount_bpffs_dir: - Use S_IRWXU instead of 0700. - Improve error handling and formatting. Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-10-23 10:51:24 +00:00
err = mount_bpffs_for_file(path);
2020-05-09 17:59:20 +00:00
if (err)
goto close_link;
err = bpf_link__pin(link, path);
if (err) {
p_err("pin_iter failed for program %s to path %s",
bpf_program__name(prog), path);
goto close_link;
}
close_link:
bpf_link__destroy(link);
close_obj:
bpf_object__close(obj);
close_map_fd:
if (map_fd >= 0)
close(map_fd);
2020-05-09 17:59:20 +00:00
return err;
}
static int do_help(int argc, char **argv)
{
fprintf(stderr,
"Usage: %1$s %2$s pin OBJ PATH [map MAP]\n"
" %1$s %2$s help\n"
tools: bpftool: Update and synchronise option list in doc and help msg Bugzilla: http://bugzilla.redhat.com/2041365 commit c07ba629df97b796ca7bbdfbf4748266ead27745 Author: Quentin Monnet <quentin@isovalent.com> Date: Fri Jul 30 22:54:32 2021 +0100 tools: bpftool: Update and synchronise option list in doc and help msg All bpftool commands support the options for JSON output and debug from libbpf. In addition, some commands support additional options corresponding to specific use cases. The list of options described in the man pages for the different commands are not always accurate. The messages for interactive help are mostly limited to HELP_SPEC_OPTIONS, and are even less representative of the actual set of options supported for the commands. Let's update the lists: - HELP_SPEC_OPTIONS is modified to contain the "default" options (JSON and debug), and to be extensible (no ending curly bracket). - All commands use HELP_SPEC_OPTIONS in their help message, and then complete the list with their specific options. - The lists of options in the man pages are updated. - The formatting of the list for bpftool.rst is adjusted to match formatting for the other man pages. This is for consistency, and also because it will be helpful in a future patch to automatically check that the files are synchronised. Signed-off-by: Quentin Monnet <quentin@isovalent.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20210730215435.7095-5-quentin@isovalent.com Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-01-17 14:25:38 +00:00
"\n"
" " HELP_SPEC_MAP "\n"
tools: bpftool: Update and synchronise option list in doc and help msg Bugzilla: http://bugzilla.redhat.com/2041365 commit c07ba629df97b796ca7bbdfbf4748266ead27745 Author: Quentin Monnet <quentin@isovalent.com> Date: Fri Jul 30 22:54:32 2021 +0100 tools: bpftool: Update and synchronise option list in doc and help msg All bpftool commands support the options for JSON output and debug from libbpf. In addition, some commands support additional options corresponding to specific use cases. The list of options described in the man pages for the different commands are not always accurate. The messages for interactive help are mostly limited to HELP_SPEC_OPTIONS, and are even less representative of the actual set of options supported for the commands. Let's update the lists: - HELP_SPEC_OPTIONS is modified to contain the "default" options (JSON and debug), and to be extensible (no ending curly bracket). - All commands use HELP_SPEC_OPTIONS in their help message, and then complete the list with their specific options. - The lists of options in the man pages are updated. - The formatting of the list for bpftool.rst is adjusted to match formatting for the other man pages. This is for consistency, and also because it will be helpful in a future patch to automatically check that the files are synchronised. Signed-off-by: Quentin Monnet <quentin@isovalent.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20210730215435.7095-5-quentin@isovalent.com Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-01-17 14:25:38 +00:00
" " HELP_SPEC_OPTIONS " }\n"
"",
bin_name, "iter");
2020-05-09 17:59:20 +00:00
return 0;
}
static const struct cmd cmds[] = {
{ "help", do_help },
{ "pin", do_pin },
{ 0 }
};
int do_iter(int argc, char **argv)
{
return cmd_select(cmds, argc, argv, do_help);
}