1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 // Copyright (C) 2020 Facebook
10 #include <bpf/libbpf.h>
14 static int do_pin(int argc
, char **argv
)
16 DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts
, iter_opts
);
17 union bpf_iter_link_info linfo
;
18 const char *objfile
, *path
;
19 struct bpf_program
*prog
;
20 struct bpf_object
*obj
;
21 struct bpf_link
*link
;
22 int err
= -1, map_fd
= -1;
30 /* optional arguments */
32 if (is_prefix(*argv
, "map")) {
36 p_err("incorrect map spec");
40 map_fd
= map_parse_fd(&argc
, &argv
);
44 memset(&linfo
, 0, sizeof(linfo
));
45 linfo
.map
.map_fd
= map_fd
;
46 iter_opts
.link_info
= &linfo
;
47 iter_opts
.link_info_len
= sizeof(linfo
);
51 obj
= bpf_object__open(objfile
);
54 p_err("can't open objfile %s", objfile
);
58 err
= bpf_object__load(obj
);
60 p_err("can't load objfile %s", objfile
);
64 prog
= bpf_object__next_program(obj
, NULL
);
67 p_err("can't find bpf program in objfile %s", objfile
);
71 link
= bpf_program__attach_iter(prog
, &iter_opts
);
74 p_err("attach_iter failed for program %s",
75 bpf_program__name(prog
));
79 err
= mount_bpffs_for_file(path
);
83 err
= bpf_link__pin(link
, path
);
85 p_err("pin_iter failed for program %s to path %s",
86 bpf_program__name(prog
), path
);
91 bpf_link__destroy(link
);
93 bpf_object__close(obj
);
100 static int do_help(int argc
, char **argv
)
103 "Usage: %1$s %2$s pin OBJ PATH [map MAP]\n"
106 " " HELP_SPEC_MAP
"\n"
107 " " HELP_SPEC_OPTIONS
" }\n"
114 static const struct cmd cmds
[] = {
120 int do_iter(int argc
, char **argv
)
122 return cmd_select(cmds
, argc
, argv
, do_help
);