102ff58dcSJakub Kicinski // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
202ff58dcSJakub Kicinski /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
371bb428fSJakub Kicinski
4b3d84af7SQuentin Monnet #ifndef _GNU_SOURCE
53ff5a4dcSJakub Kicinski #define _GNU_SOURCE
6b3d84af7SQuentin Monnet #endif
771bb428fSJakub Kicinski #include <errno.h>
871bb428fSJakub Kicinski #include <fcntl.h>
947c09d6aSSong Liu #include <signal.h>
10c9c35995SJakub Kicinski #include <stdarg.h>
1171bb428fSJakub Kicinski #include <stdio.h>
1271bb428fSJakub Kicinski #include <stdlib.h>
1371bb428fSJakub Kicinski #include <string.h>
1471bb428fSJakub Kicinski #include <time.h>
1571bb428fSJakub Kicinski #include <unistd.h>
16ba6dd679SJakub Kicinski #include <net/if.h>
1747c09d6aSSong Liu #include <sys/ioctl.h>
1871bb428fSJakub Kicinski #include <sys/types.h>
1971bb428fSJakub Kicinski #include <sys/stat.h>
2047c09d6aSSong Liu #include <sys/syscall.h>
21d510296dSAlexei Starovoitov #include <dirent.h>
2271bb428fSJakub Kicinski
23c8406848SJakub Kicinski #include <linux/err.h>
2447c09d6aSSong Liu #include <linux/perf_event.h>
25ba95c745SQuentin Monnet #include <linux/sizes.h>
26c8406848SJakub Kicinski
27229c3b47SToke Høiland-Jørgensen #include <bpf/bpf.h>
28229c3b47SToke Høiland-Jørgensen #include <bpf/btf.h>
298f184732SQuentin Monnet #include <bpf/hashmap.h>
30229c3b47SToke Høiland-Jørgensen #include <bpf/libbpf.h>
31a19df713SMauricio Vásquez #include <bpf/libbpf_internal.h>
32d510296dSAlexei Starovoitov #include <bpf/skel_internal.h>
3371bb428fSJakub Kicinski
34b6c1cedbSJiong Wang #include "cfg.h"
3571bb428fSJakub Kicinski #include "main.h"
3673bb5b4fSJiong Wang #include "xlated_dumper.h"
3771bb428fSJakub Kicinski
38aff52e68SYiFei Zhu #define BPF_METADATA_PREFIX "bpf_metadata_"
39aff52e68SYiFei Zhu #define BPF_METADATA_PREFIX_LEN (sizeof(BPF_METADATA_PREFIX) - 1)
40aff52e68SYiFei Zhu
41ec202509SPaul Chaignon enum dump_mode {
42ec202509SPaul Chaignon DUMP_JITED,
43ec202509SPaul Chaignon DUMP_XLATED,
44ec202509SPaul Chaignon };
45ec202509SPaul Chaignon
461ba5ad36SDaniel Müller static const bool attach_types[] = {
471ba5ad36SDaniel Müller [BPF_SK_SKB_STREAM_PARSER] = true,
481ba5ad36SDaniel Müller [BPF_SK_SKB_STREAM_VERDICT] = true,
491ba5ad36SDaniel Müller [BPF_SK_SKB_VERDICT] = true,
501ba5ad36SDaniel Müller [BPF_SK_MSG_VERDICT] = true,
511ba5ad36SDaniel Müller [BPF_FLOW_DISSECTOR] = true,
521ba5ad36SDaniel Müller [__MAX_BPF_ATTACH_TYPE] = false,
531ba5ad36SDaniel Müller };
541ba5ad36SDaniel Müller
551ba5ad36SDaniel Müller /* Textual representations traditionally used by the program and kept around
561ba5ad36SDaniel Müller * for the sake of backwards compatibility.
571ba5ad36SDaniel Müller */
58b7d3826cSJohn Fastabend static const char * const attach_type_strings[] = {
59b7d3826cSJohn Fastabend [BPF_SK_SKB_STREAM_PARSER] = "stream_parser",
60b7d3826cSJohn Fastabend [BPF_SK_SKB_STREAM_VERDICT] = "stream_verdict",
61a7ba4558SCong Wang [BPF_SK_SKB_VERDICT] = "skb_verdict",
62b7d3826cSJohn Fastabend [BPF_SK_MSG_VERDICT] = "msg_verdict",
63b7d3826cSJohn Fastabend [__MAX_BPF_ATTACH_TYPE] = NULL,
64b7d3826cSJohn Fastabend };
65b7d3826cSJohn Fastabend
668f184732SQuentin Monnet static struct hashmap *prog_table;
6746241271SQuentin Monnet
parse_attach_type(const char * str)68c101189bSQuentin Monnet static enum bpf_attach_type parse_attach_type(const char *str)
69b7d3826cSJohn Fastabend {
70b7d3826cSJohn Fastabend enum bpf_attach_type type;
71b7d3826cSJohn Fastabend
72b7d3826cSJohn Fastabend for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
731ba5ad36SDaniel Müller if (attach_types[type]) {
741ba5ad36SDaniel Müller const char *attach_type_str;
751ba5ad36SDaniel Müller
761ba5ad36SDaniel Müller attach_type_str = libbpf_bpf_attach_type_str(type);
771ba5ad36SDaniel Müller if (!strcmp(str, attach_type_str))
781ba5ad36SDaniel Müller return type;
791ba5ad36SDaniel Müller }
801ba5ad36SDaniel Müller
81b7d3826cSJohn Fastabend if (attach_type_strings[type] &&
82b7d3826cSJohn Fastabend is_prefix(str, attach_type_strings[type]))
83b7d3826cSJohn Fastabend return type;
84b7d3826cSJohn Fastabend }
85b7d3826cSJohn Fastabend
86b7d3826cSJohn Fastabend return __MAX_BPF_ATTACH_TYPE;
87b7d3826cSJohn Fastabend }
88b7d3826cSJohn Fastabend
prep_prog_info(struct bpf_prog_info * const info,enum dump_mode mode,void ** info_data,size_t * const info_data_sz)89c59765cfSDave Marchevsky static int prep_prog_info(struct bpf_prog_info *const info, enum dump_mode mode,
90c59765cfSDave Marchevsky void **info_data, size_t *const info_data_sz)
91c59765cfSDave Marchevsky {
92c59765cfSDave Marchevsky struct bpf_prog_info holder = {};
93c59765cfSDave Marchevsky size_t needed = 0;
94c59765cfSDave Marchevsky void *ptr;
95c59765cfSDave Marchevsky
96c59765cfSDave Marchevsky if (mode == DUMP_JITED) {
97c59765cfSDave Marchevsky holder.jited_prog_len = info->jited_prog_len;
98c59765cfSDave Marchevsky needed += info->jited_prog_len;
99c59765cfSDave Marchevsky } else {
100c59765cfSDave Marchevsky holder.xlated_prog_len = info->xlated_prog_len;
101c59765cfSDave Marchevsky needed += info->xlated_prog_len;
102c59765cfSDave Marchevsky }
103c59765cfSDave Marchevsky
104c59765cfSDave Marchevsky holder.nr_jited_ksyms = info->nr_jited_ksyms;
105c59765cfSDave Marchevsky needed += info->nr_jited_ksyms * sizeof(__u64);
106c59765cfSDave Marchevsky
107c59765cfSDave Marchevsky holder.nr_jited_func_lens = info->nr_jited_func_lens;
108c59765cfSDave Marchevsky needed += info->nr_jited_func_lens * sizeof(__u32);
109c59765cfSDave Marchevsky
110c59765cfSDave Marchevsky holder.nr_func_info = info->nr_func_info;
111c59765cfSDave Marchevsky holder.func_info_rec_size = info->func_info_rec_size;
112c59765cfSDave Marchevsky needed += info->nr_func_info * info->func_info_rec_size;
113c59765cfSDave Marchevsky
114c59765cfSDave Marchevsky holder.nr_line_info = info->nr_line_info;
115c59765cfSDave Marchevsky holder.line_info_rec_size = info->line_info_rec_size;
116c59765cfSDave Marchevsky needed += info->nr_line_info * info->line_info_rec_size;
117c59765cfSDave Marchevsky
118c59765cfSDave Marchevsky holder.nr_jited_line_info = info->nr_jited_line_info;
119c59765cfSDave Marchevsky holder.jited_line_info_rec_size = info->jited_line_info_rec_size;
120c59765cfSDave Marchevsky needed += info->nr_jited_line_info * info->jited_line_info_rec_size;
121c59765cfSDave Marchevsky
122c59765cfSDave Marchevsky if (needed > *info_data_sz) {
123c59765cfSDave Marchevsky ptr = realloc(*info_data, needed);
124c59765cfSDave Marchevsky if (!ptr)
125c59765cfSDave Marchevsky return -1;
126c59765cfSDave Marchevsky
127c59765cfSDave Marchevsky *info_data = ptr;
128c59765cfSDave Marchevsky *info_data_sz = needed;
129c59765cfSDave Marchevsky }
130c59765cfSDave Marchevsky ptr = *info_data;
131c59765cfSDave Marchevsky
132c59765cfSDave Marchevsky if (mode == DUMP_JITED) {
133c59765cfSDave Marchevsky holder.jited_prog_insns = ptr_to_u64(ptr);
134c59765cfSDave Marchevsky ptr += holder.jited_prog_len;
135c59765cfSDave Marchevsky } else {
136c59765cfSDave Marchevsky holder.xlated_prog_insns = ptr_to_u64(ptr);
137c59765cfSDave Marchevsky ptr += holder.xlated_prog_len;
138c59765cfSDave Marchevsky }
139c59765cfSDave Marchevsky
140c59765cfSDave Marchevsky holder.jited_ksyms = ptr_to_u64(ptr);
141c59765cfSDave Marchevsky ptr += holder.nr_jited_ksyms * sizeof(__u64);
142c59765cfSDave Marchevsky
143c59765cfSDave Marchevsky holder.jited_func_lens = ptr_to_u64(ptr);
144c59765cfSDave Marchevsky ptr += holder.nr_jited_func_lens * sizeof(__u32);
145c59765cfSDave Marchevsky
146c59765cfSDave Marchevsky holder.func_info = ptr_to_u64(ptr);
147c59765cfSDave Marchevsky ptr += holder.nr_func_info * holder.func_info_rec_size;
148c59765cfSDave Marchevsky
149c59765cfSDave Marchevsky holder.line_info = ptr_to_u64(ptr);
150c59765cfSDave Marchevsky ptr += holder.nr_line_info * holder.line_info_rec_size;
151c59765cfSDave Marchevsky
152c59765cfSDave Marchevsky holder.jited_line_info = ptr_to_u64(ptr);
153c59765cfSDave Marchevsky ptr += holder.nr_jited_line_info * holder.jited_line_info_rec_size;
154c59765cfSDave Marchevsky
155c59765cfSDave Marchevsky *info = holder;
156c59765cfSDave Marchevsky return 0;
157c59765cfSDave Marchevsky }
158c59765cfSDave Marchevsky
print_boot_time(__u64 nsecs,char * buf,unsigned int size)15971bb428fSJakub Kicinski static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
16071bb428fSJakub Kicinski {
16171bb428fSJakub Kicinski struct timespec real_time_ts, boot_time_ts;
16271bb428fSJakub Kicinski time_t wallclock_secs;
16371bb428fSJakub Kicinski struct tm load_tm;
16471bb428fSJakub Kicinski
16571bb428fSJakub Kicinski buf[--size] = '\0';
16671bb428fSJakub Kicinski
16771bb428fSJakub Kicinski if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
16871bb428fSJakub Kicinski clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
16971bb428fSJakub Kicinski perror("Can't read clocks");
17071bb428fSJakub Kicinski snprintf(buf, size, "%llu", nsecs / 1000000000);
17171bb428fSJakub Kicinski return;
17271bb428fSJakub Kicinski }
17371bb428fSJakub Kicinski
17471bb428fSJakub Kicinski wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
17507480cbcSJakub Kicinski (real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) /
17607480cbcSJakub Kicinski 1000000000;
17707480cbcSJakub Kicinski
17871bb428fSJakub Kicinski
17971bb428fSJakub Kicinski if (!localtime_r(&wallclock_secs, &load_tm)) {
18071bb428fSJakub Kicinski snprintf(buf, size, "%llu", nsecs / 1000000000);
18171bb428fSJakub Kicinski return;
18271bb428fSJakub Kicinski }
18371bb428fSJakub Kicinski
184a3fe1f6fSQuentin Monnet if (json_output)
185a3fe1f6fSQuentin Monnet strftime(buf, size, "%s", &load_tm);
186a3fe1f6fSQuentin Monnet else
187a3fe1f6fSQuentin Monnet strftime(buf, size, "%FT%T%z", &load_tm);
18871bb428fSJakub Kicinski }
18971bb428fSJakub Kicinski
show_prog_maps(int fd,__u32 num_maps)1906e7e034eSQuentin Monnet static void show_prog_maps(int fd, __u32 num_maps)
19171bb428fSJakub Kicinski {
19271bb428fSJakub Kicinski struct bpf_prog_info info = {};
19371bb428fSJakub Kicinski __u32 len = sizeof(info);
19471bb428fSJakub Kicinski __u32 map_ids[num_maps];
19571bb428fSJakub Kicinski unsigned int i;
19671bb428fSJakub Kicinski int err;
19771bb428fSJakub Kicinski
19871bb428fSJakub Kicinski info.nr_map_ids = num_maps;
19971bb428fSJakub Kicinski info.map_ids = ptr_to_u64(map_ids);
20071bb428fSJakub Kicinski
20138f0408eSIlya Leoshkevich err = bpf_prog_get_info_by_fd(fd, &info, &len);
20271bb428fSJakub Kicinski if (err || !info.nr_map_ids)
20371bb428fSJakub Kicinski return;
20471bb428fSJakub Kicinski
205743cc665SQuentin Monnet if (json_output) {
206743cc665SQuentin Monnet jsonw_name(json_wtr, "map_ids");
207743cc665SQuentin Monnet jsonw_start_array(json_wtr);
208743cc665SQuentin Monnet for (i = 0; i < info.nr_map_ids; i++)
209743cc665SQuentin Monnet jsonw_uint(json_wtr, map_ids[i]);
210743cc665SQuentin Monnet jsonw_end_array(json_wtr);
211743cc665SQuentin Monnet } else {
21271bb428fSJakub Kicinski printf(" map_ids ");
21371bb428fSJakub Kicinski for (i = 0; i < info.nr_map_ids; i++)
21471bb428fSJakub Kicinski printf("%u%s", map_ids[i],
21571bb428fSJakub Kicinski i == info.nr_map_ids - 1 ? "" : ",");
21671bb428fSJakub Kicinski }
21771bb428fSJakub Kicinski }
21871bb428fSJakub Kicinski
find_metadata(int prog_fd,struct bpf_map_info * map_info)219aff52e68SYiFei Zhu static void *find_metadata(int prog_fd, struct bpf_map_info *map_info)
220aff52e68SYiFei Zhu {
221aff52e68SYiFei Zhu struct bpf_prog_info prog_info;
222aff52e68SYiFei Zhu __u32 prog_info_len;
223aff52e68SYiFei Zhu __u32 map_info_len;
224aff52e68SYiFei Zhu void *value = NULL;
225aff52e68SYiFei Zhu __u32 *map_ids;
226aff52e68SYiFei Zhu int nr_maps;
227aff52e68SYiFei Zhu int key = 0;
228aff52e68SYiFei Zhu int map_fd;
229aff52e68SYiFei Zhu int ret;
230aff52e68SYiFei Zhu __u32 i;
231aff52e68SYiFei Zhu
232aff52e68SYiFei Zhu memset(&prog_info, 0, sizeof(prog_info));
233aff52e68SYiFei Zhu prog_info_len = sizeof(prog_info);
23438f0408eSIlya Leoshkevich ret = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
235aff52e68SYiFei Zhu if (ret)
236aff52e68SYiFei Zhu return NULL;
237aff52e68SYiFei Zhu
238aff52e68SYiFei Zhu if (!prog_info.nr_map_ids)
239aff52e68SYiFei Zhu return NULL;
240aff52e68SYiFei Zhu
241aff52e68SYiFei Zhu map_ids = calloc(prog_info.nr_map_ids, sizeof(__u32));
242aff52e68SYiFei Zhu if (!map_ids)
243aff52e68SYiFei Zhu return NULL;
244aff52e68SYiFei Zhu
245aff52e68SYiFei Zhu nr_maps = prog_info.nr_map_ids;
246aff52e68SYiFei Zhu memset(&prog_info, 0, sizeof(prog_info));
247aff52e68SYiFei Zhu prog_info.nr_map_ids = nr_maps;
248aff52e68SYiFei Zhu prog_info.map_ids = ptr_to_u64(map_ids);
249aff52e68SYiFei Zhu prog_info_len = sizeof(prog_info);
250aff52e68SYiFei Zhu
25138f0408eSIlya Leoshkevich ret = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
252aff52e68SYiFei Zhu if (ret)
253aff52e68SYiFei Zhu goto free_map_ids;
254aff52e68SYiFei Zhu
255aff52e68SYiFei Zhu for (i = 0; i < prog_info.nr_map_ids; i++) {
256aff52e68SYiFei Zhu map_fd = bpf_map_get_fd_by_id(map_ids[i]);
257aff52e68SYiFei Zhu if (map_fd < 0)
258aff52e68SYiFei Zhu goto free_map_ids;
259aff52e68SYiFei Zhu
260aff52e68SYiFei Zhu memset(map_info, 0, sizeof(*map_info));
261aff52e68SYiFei Zhu map_info_len = sizeof(*map_info);
26238f0408eSIlya Leoshkevich ret = bpf_map_get_info_by_fd(map_fd, map_info, &map_info_len);
263aff52e68SYiFei Zhu if (ret < 0) {
264aff52e68SYiFei Zhu close(map_fd);
265aff52e68SYiFei Zhu goto free_map_ids;
266aff52e68SYiFei Zhu }
267aff52e68SYiFei Zhu
268aff52e68SYiFei Zhu if (map_info->type != BPF_MAP_TYPE_ARRAY ||
269aff52e68SYiFei Zhu map_info->key_size != sizeof(int) ||
270aff52e68SYiFei Zhu map_info->max_entries != 1 ||
271aff52e68SYiFei Zhu !map_info->btf_value_type_id ||
272aff52e68SYiFei Zhu !strstr(map_info->name, ".rodata")) {
273aff52e68SYiFei Zhu close(map_fd);
274aff52e68SYiFei Zhu continue;
275aff52e68SYiFei Zhu }
276aff52e68SYiFei Zhu
277aff52e68SYiFei Zhu value = malloc(map_info->value_size);
278aff52e68SYiFei Zhu if (!value) {
279aff52e68SYiFei Zhu close(map_fd);
280aff52e68SYiFei Zhu goto free_map_ids;
281aff52e68SYiFei Zhu }
282aff52e68SYiFei Zhu
283aff52e68SYiFei Zhu if (bpf_map_lookup_elem(map_fd, &key, value)) {
284aff52e68SYiFei Zhu close(map_fd);
285aff52e68SYiFei Zhu free(value);
286aff52e68SYiFei Zhu value = NULL;
287aff52e68SYiFei Zhu goto free_map_ids;
288aff52e68SYiFei Zhu }
289aff52e68SYiFei Zhu
290aff52e68SYiFei Zhu close(map_fd);
291aff52e68SYiFei Zhu break;
292aff52e68SYiFei Zhu }
293aff52e68SYiFei Zhu
294aff52e68SYiFei Zhu free_map_ids:
295aff52e68SYiFei Zhu free(map_ids);
296aff52e68SYiFei Zhu return value;
297aff52e68SYiFei Zhu }
298aff52e68SYiFei Zhu
has_metadata_prefix(const char * s)299aff52e68SYiFei Zhu static bool has_metadata_prefix(const char *s)
300aff52e68SYiFei Zhu {
301aff52e68SYiFei Zhu return strncmp(s, BPF_METADATA_PREFIX, BPF_METADATA_PREFIX_LEN) == 0;
302aff52e68SYiFei Zhu }
303aff52e68SYiFei Zhu
show_prog_metadata(int fd,__u32 num_maps)304aff52e68SYiFei Zhu static void show_prog_metadata(int fd, __u32 num_maps)
305aff52e68SYiFei Zhu {
306aff52e68SYiFei Zhu const struct btf_type *t_datasec, *t_var;
307aff52e68SYiFei Zhu struct bpf_map_info map_info;
308aff52e68SYiFei Zhu struct btf_var_secinfo *vsi;
309aff52e68SYiFei Zhu bool printed_header = false;
310aff52e68SYiFei Zhu unsigned int i, vlen;
311aff52e68SYiFei Zhu void *value = NULL;
312aff52e68SYiFei Zhu const char *name;
31386f4b7f2SQuentin Monnet struct btf *btf;
314aff52e68SYiFei Zhu int err;
315aff52e68SYiFei Zhu
316aff52e68SYiFei Zhu if (!num_maps)
317aff52e68SYiFei Zhu return;
318aff52e68SYiFei Zhu
319aff52e68SYiFei Zhu memset(&map_info, 0, sizeof(map_info));
320aff52e68SYiFei Zhu value = find_metadata(fd, &map_info);
321aff52e68SYiFei Zhu if (!value)
322aff52e68SYiFei Zhu return;
323aff52e68SYiFei Zhu
32486f4b7f2SQuentin Monnet btf = btf__load_from_kernel_by_id(map_info.btf_id);
325d1313e01SSahid Orentino Ferdjaoui if (!btf)
326aff52e68SYiFei Zhu goto out_free;
327aff52e68SYiFei Zhu
328aff52e68SYiFei Zhu t_datasec = btf__type_by_id(btf, map_info.btf_value_type_id);
329aff52e68SYiFei Zhu if (!btf_is_datasec(t_datasec))
330aff52e68SYiFei Zhu goto out_free;
331aff52e68SYiFei Zhu
332aff52e68SYiFei Zhu vlen = btf_vlen(t_datasec);
333aff52e68SYiFei Zhu vsi = btf_var_secinfos(t_datasec);
334aff52e68SYiFei Zhu
335aff52e68SYiFei Zhu /* We don't proceed to check the kinds of the elements of the DATASEC.
336aff52e68SYiFei Zhu * The verifier enforces them to be BTF_KIND_VAR.
337aff52e68SYiFei Zhu */
338aff52e68SYiFei Zhu
339aff52e68SYiFei Zhu if (json_output) {
340aff52e68SYiFei Zhu struct btf_dumper d = {
341aff52e68SYiFei Zhu .btf = btf,
342aff52e68SYiFei Zhu .jw = json_wtr,
343aff52e68SYiFei Zhu .is_plain_text = false,
344aff52e68SYiFei Zhu };
345aff52e68SYiFei Zhu
346aff52e68SYiFei Zhu for (i = 0; i < vlen; i++, vsi++) {
347aff52e68SYiFei Zhu t_var = btf__type_by_id(btf, vsi->type);
348aff52e68SYiFei Zhu name = btf__name_by_offset(btf, t_var->name_off);
349aff52e68SYiFei Zhu
350aff52e68SYiFei Zhu if (!has_metadata_prefix(name))
351aff52e68SYiFei Zhu continue;
352aff52e68SYiFei Zhu
353aff52e68SYiFei Zhu if (!printed_header) {
354aff52e68SYiFei Zhu jsonw_name(json_wtr, "metadata");
355aff52e68SYiFei Zhu jsonw_start_object(json_wtr);
356aff52e68SYiFei Zhu printed_header = true;
357aff52e68SYiFei Zhu }
358aff52e68SYiFei Zhu
359aff52e68SYiFei Zhu jsonw_name(json_wtr, name + BPF_METADATA_PREFIX_LEN);
360aff52e68SYiFei Zhu err = btf_dumper_type(&d, t_var->type, value + vsi->offset);
361aff52e68SYiFei Zhu if (err) {
362aff52e68SYiFei Zhu p_err("btf dump failed: %d", err);
363aff52e68SYiFei Zhu break;
364aff52e68SYiFei Zhu }
365aff52e68SYiFei Zhu }
366aff52e68SYiFei Zhu if (printed_header)
367aff52e68SYiFei Zhu jsonw_end_object(json_wtr);
368aff52e68SYiFei Zhu } else {
369e89ef634SQuentin Monnet json_writer_t *btf_wtr;
370aff52e68SYiFei Zhu struct btf_dumper d = {
371aff52e68SYiFei Zhu .btf = btf,
372aff52e68SYiFei Zhu .is_plain_text = true,
373aff52e68SYiFei Zhu };
374aff52e68SYiFei Zhu
375aff52e68SYiFei Zhu for (i = 0; i < vlen; i++, vsi++) {
376aff52e68SYiFei Zhu t_var = btf__type_by_id(btf, vsi->type);
377aff52e68SYiFei Zhu name = btf__name_by_offset(btf, t_var->name_off);
378aff52e68SYiFei Zhu
379aff52e68SYiFei Zhu if (!has_metadata_prefix(name))
380aff52e68SYiFei Zhu continue;
381aff52e68SYiFei Zhu
382aff52e68SYiFei Zhu if (!printed_header) {
383aff52e68SYiFei Zhu printf("\tmetadata:");
384e89ef634SQuentin Monnet
385e89ef634SQuentin Monnet btf_wtr = jsonw_new(stdout);
386e89ef634SQuentin Monnet if (!btf_wtr) {
387e89ef634SQuentin Monnet p_err("jsonw alloc failed");
388e89ef634SQuentin Monnet goto out_free;
389e89ef634SQuentin Monnet }
390e89ef634SQuentin Monnet d.jw = btf_wtr,
391e89ef634SQuentin Monnet
392aff52e68SYiFei Zhu printed_header = true;
393aff52e68SYiFei Zhu }
394aff52e68SYiFei Zhu
395aff52e68SYiFei Zhu printf("\n\t\t%s = ", name + BPF_METADATA_PREFIX_LEN);
396aff52e68SYiFei Zhu
397aff52e68SYiFei Zhu jsonw_reset(btf_wtr);
398aff52e68SYiFei Zhu err = btf_dumper_type(&d, t_var->type, value + vsi->offset);
399aff52e68SYiFei Zhu if (err) {
400aff52e68SYiFei Zhu p_err("btf dump failed: %d", err);
401aff52e68SYiFei Zhu break;
402aff52e68SYiFei Zhu }
403aff52e68SYiFei Zhu }
404aff52e68SYiFei Zhu if (printed_header)
405aff52e68SYiFei Zhu jsonw_destroy(&btf_wtr);
406aff52e68SYiFei Zhu }
407aff52e68SYiFei Zhu
408aff52e68SYiFei Zhu out_free:
409aff52e68SYiFei Zhu btf__free(btf);
410aff52e68SYiFei Zhu free(value);
411aff52e68SYiFei Zhu }
412aff52e68SYiFei Zhu
print_prog_header_json(struct bpf_prog_info * info,int fd)413b662000aSRaman Shukhau static void print_prog_header_json(struct bpf_prog_info *info, int fd)
414743cc665SQuentin Monnet {
415b700eeb4SDaniel Müller const char *prog_type_str;
416b662000aSRaman Shukhau char prog_name[MAX_PROG_FULL_NAME];
417b662000aSRaman Shukhau
418743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "id", info->id);
419b700eeb4SDaniel Müller prog_type_str = libbpf_bpf_prog_type_str(info->type);
420b700eeb4SDaniel Müller
421b700eeb4SDaniel Müller if (prog_type_str)
422b700eeb4SDaniel Müller jsonw_string_field(json_wtr, "type", prog_type_str);
42371bb428fSJakub Kicinski else
424743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "type", info->type);
42571bb428fSJakub Kicinski
426b662000aSRaman Shukhau if (*info->name) {
427b662000aSRaman Shukhau get_prog_full_name(info, fd, prog_name, sizeof(prog_name));
428b662000aSRaman Shukhau jsonw_string_field(json_wtr, "name", prog_name);
429b662000aSRaman Shukhau }
43071bb428fSJakub Kicinski
431743cc665SQuentin Monnet jsonw_name(json_wtr, "tag");
432743cc665SQuentin Monnet jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
433743cc665SQuentin Monnet info->tag[0], info->tag[1], info->tag[2], info->tag[3],
434743cc665SQuentin Monnet info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
43571bb428fSJakub Kicinski
4369b984a20SJiri Olsa jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
43788ad472bSAlexei Starovoitov if (info->run_time_ns) {
43888ad472bSAlexei Starovoitov jsonw_uint_field(json_wtr, "run_time_ns", info->run_time_ns);
43988ad472bSAlexei Starovoitov jsonw_uint_field(json_wtr, "run_cnt", info->run_cnt);
44088ad472bSAlexei Starovoitov }
4419ed9e9baSAlexei Starovoitov if (info->recursion_misses)
4429ed9e9baSAlexei Starovoitov jsonw_uint_field(json_wtr, "recursion_misses", info->recursion_misses);
443ec202509SPaul Chaignon }
4449b984a20SJiri Olsa
print_prog_json(struct bpf_prog_info * info,int fd)445ec202509SPaul Chaignon static void print_prog_json(struct bpf_prog_info *info, int fd)
446ec202509SPaul Chaignon {
447ec202509SPaul Chaignon char *memlock;
448ec202509SPaul Chaignon
449ec202509SPaul Chaignon jsonw_start_object(json_wtr);
450b662000aSRaman Shukhau print_prog_header_json(info, fd);
45152262210SJakub Kicinski print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
45252262210SJakub Kicinski
453743cc665SQuentin Monnet if (info->load_time) {
45471bb428fSJakub Kicinski char buf[32];
45571bb428fSJakub Kicinski
456743cc665SQuentin Monnet print_boot_time(info->load_time, buf, sizeof(buf));
45771bb428fSJakub Kicinski
45871bb428fSJakub Kicinski /* Piggy back on load_time, since 0 uid is a valid one */
459a3fe1f6fSQuentin Monnet jsonw_name(json_wtr, "loaded_at");
460a3fe1f6fSQuentin Monnet jsonw_printf(json_wtr, "%s", buf);
461743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
46271bb428fSJakub Kicinski }
46371bb428fSJakub Kicinski
464743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
46571bb428fSJakub Kicinski
466743cc665SQuentin Monnet if (info->jited_prog_len) {
467743cc665SQuentin Monnet jsonw_bool_field(json_wtr, "jited", true);
468743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
469743cc665SQuentin Monnet } else {
470743cc665SQuentin Monnet jsonw_bool_field(json_wtr, "jited", false);
471743cc665SQuentin Monnet }
472743cc665SQuentin Monnet
473743cc665SQuentin Monnet memlock = get_fdinfo(fd, "memlock");
474743cc665SQuentin Monnet if (memlock)
475357b3cc3SChris J Arges jsonw_int_field(json_wtr, "bytes_memlock", atoll(memlock));
476743cc665SQuentin Monnet free(memlock);
477743cc665SQuentin Monnet
478743cc665SQuentin Monnet if (info->nr_map_ids)
479743cc665SQuentin Monnet show_prog_maps(fd, info->nr_map_ids);
480743cc665SQuentin Monnet
481569b0c77SPrashant Bhole if (info->btf_id)
482569b0c77SPrashant Bhole jsonw_int_field(json_wtr, "btf_id", info->btf_id);
483569b0c77SPrashant Bhole
4848f184732SQuentin Monnet if (!hashmap__empty(prog_table)) {
4858f184732SQuentin Monnet struct hashmap_entry *entry;
4864990f1f4SPrashant Bhole
4874990f1f4SPrashant Bhole jsonw_name(json_wtr, "pinned");
4884990f1f4SPrashant Bhole jsonw_start_array(json_wtr);
489c302378bSEduard Zingerman hashmap__for_each_key_entry(prog_table, entry, info->id)
490c302378bSEduard Zingerman jsonw_string(json_wtr, entry->pvalue);
4914990f1f4SPrashant Bhole jsonw_end_array(json_wtr);
4924990f1f4SPrashant Bhole }
4934990f1f4SPrashant Bhole
494d6699f8eSQuentin Monnet emit_obj_refs_json(refs_table, info->id, json_wtr);
495d53dee3fSAndrii Nakryiko
496aff52e68SYiFei Zhu show_prog_metadata(fd, info->nr_map_ids);
497aff52e68SYiFei Zhu
498743cc665SQuentin Monnet jsonw_end_object(json_wtr);
499743cc665SQuentin Monnet }
500743cc665SQuentin Monnet
print_prog_header_plain(struct bpf_prog_info * info,int fd)501b662000aSRaman Shukhau static void print_prog_header_plain(struct bpf_prog_info *info, int fd)
502743cc665SQuentin Monnet {
503b700eeb4SDaniel Müller const char *prog_type_str;
504b662000aSRaman Shukhau char prog_name[MAX_PROG_FULL_NAME];
505b662000aSRaman Shukhau
506743cc665SQuentin Monnet printf("%u: ", info->id);
507b700eeb4SDaniel Müller prog_type_str = libbpf_bpf_prog_type_str(info->type);
508b700eeb4SDaniel Müller if (prog_type_str)
509b700eeb4SDaniel Müller printf("%s ", prog_type_str);
510743cc665SQuentin Monnet else
511743cc665SQuentin Monnet printf("type %u ", info->type);
512743cc665SQuentin Monnet
513b662000aSRaman Shukhau if (*info->name) {
514b662000aSRaman Shukhau get_prog_full_name(info, fd, prog_name, sizeof(prog_name));
515b662000aSRaman Shukhau printf("name %s ", prog_name);
516b662000aSRaman Shukhau }
517743cc665SQuentin Monnet
518743cc665SQuentin Monnet printf("tag ");
519743cc665SQuentin Monnet fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
52052262210SJakub Kicinski print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
5219b984a20SJiri Olsa printf("%s", info->gpl_compatible ? " gpl" : "");
52288ad472bSAlexei Starovoitov if (info->run_time_ns)
52388ad472bSAlexei Starovoitov printf(" run_time_ns %lld run_cnt %lld",
52488ad472bSAlexei Starovoitov info->run_time_ns, info->run_cnt);
5259ed9e9baSAlexei Starovoitov if (info->recursion_misses)
5269ed9e9baSAlexei Starovoitov printf(" recursion_misses %lld", info->recursion_misses);
527743cc665SQuentin Monnet printf("\n");
528ec202509SPaul Chaignon }
529ec202509SPaul Chaignon
print_prog_plain(struct bpf_prog_info * info,int fd)530ec202509SPaul Chaignon static void print_prog_plain(struct bpf_prog_info *info, int fd)
531ec202509SPaul Chaignon {
532ec202509SPaul Chaignon char *memlock;
533ec202509SPaul Chaignon
534b662000aSRaman Shukhau print_prog_header_plain(info, fd);
535743cc665SQuentin Monnet
536743cc665SQuentin Monnet if (info->load_time) {
537743cc665SQuentin Monnet char buf[32];
538743cc665SQuentin Monnet
539743cc665SQuentin Monnet print_boot_time(info->load_time, buf, sizeof(buf));
540743cc665SQuentin Monnet
541743cc665SQuentin Monnet /* Piggy back on load_time, since 0 uid is a valid one */
542743cc665SQuentin Monnet printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);
543743cc665SQuentin Monnet }
544743cc665SQuentin Monnet
545743cc665SQuentin Monnet printf("\txlated %uB", info->xlated_prog_len);
546743cc665SQuentin Monnet
547743cc665SQuentin Monnet if (info->jited_prog_len)
548743cc665SQuentin Monnet printf(" jited %uB", info->jited_prog_len);
54971bb428fSJakub Kicinski else
55071bb428fSJakub Kicinski printf(" not jited");
55171bb428fSJakub Kicinski
55271bb428fSJakub Kicinski memlock = get_fdinfo(fd, "memlock");
55371bb428fSJakub Kicinski if (memlock)
55471bb428fSJakub Kicinski printf(" memlock %sB", memlock);
55571bb428fSJakub Kicinski free(memlock);
55671bb428fSJakub Kicinski
557743cc665SQuentin Monnet if (info->nr_map_ids)
558743cc665SQuentin Monnet show_prog_maps(fd, info->nr_map_ids);
55971bb428fSJakub Kicinski
5608f184732SQuentin Monnet if (!hashmap__empty(prog_table)) {
5618f184732SQuentin Monnet struct hashmap_entry *entry;
5624990f1f4SPrashant Bhole
563c302378bSEduard Zingerman hashmap__for_each_key_entry(prog_table, entry, info->id)
564c302378bSEduard Zingerman printf("\n\tpinned %s", (char *)entry->pvalue);
5654990f1f4SPrashant Bhole }
5664990f1f4SPrashant Bhole
567569b0c77SPrashant Bhole if (info->btf_id)
568031ebc1aSQuentin Monnet printf("\n\tbtf_id %d", info->btf_id);
569569b0c77SPrashant Bhole
570d6699f8eSQuentin Monnet emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
571d53dee3fSAndrii Nakryiko
57271bb428fSJakub Kicinski printf("\n");
573aff52e68SYiFei Zhu
574aff52e68SYiFei Zhu show_prog_metadata(fd, info->nr_map_ids);
575743cc665SQuentin Monnet }
576743cc665SQuentin Monnet
show_prog(int fd)577743cc665SQuentin Monnet static int show_prog(int fd)
578743cc665SQuentin Monnet {
579743cc665SQuentin Monnet struct bpf_prog_info info = {};
580743cc665SQuentin Monnet __u32 len = sizeof(info);
581743cc665SQuentin Monnet int err;
582743cc665SQuentin Monnet
58338f0408eSIlya Leoshkevich err = bpf_prog_get_info_by_fd(fd, &info, &len);
584743cc665SQuentin Monnet if (err) {
5859a5ab8bfSQuentin Monnet p_err("can't get prog info: %s", strerror(errno));
586743cc665SQuentin Monnet return -1;
587743cc665SQuentin Monnet }
588743cc665SQuentin Monnet
589743cc665SQuentin Monnet if (json_output)
590743cc665SQuentin Monnet print_prog_json(&info, fd);
591743cc665SQuentin Monnet else
592743cc665SQuentin Monnet print_prog_plain(&info, fd);
59371bb428fSJakub Kicinski
59471bb428fSJakub Kicinski return 0;
59571bb428fSJakub Kicinski }
59671bb428fSJakub Kicinski
do_show_subset(int argc,char ** argv)597ec202509SPaul Chaignon static int do_show_subset(int argc, char **argv)
598ec202509SPaul Chaignon {
599ec202509SPaul Chaignon int *fds = NULL;
600ec202509SPaul Chaignon int nb_fds, i;
601ec202509SPaul Chaignon int err = -1;
602ec202509SPaul Chaignon
603ec202509SPaul Chaignon fds = malloc(sizeof(int));
604ec202509SPaul Chaignon if (!fds) {
605ec202509SPaul Chaignon p_err("mem alloc failed");
606ec202509SPaul Chaignon return -1;
607ec202509SPaul Chaignon }
608ec202509SPaul Chaignon nb_fds = prog_parse_fds(&argc, &argv, &fds);
609ec202509SPaul Chaignon if (nb_fds < 1)
610ec202509SPaul Chaignon goto exit_free;
611ec202509SPaul Chaignon
612ec202509SPaul Chaignon if (json_output && nb_fds > 1)
613ec202509SPaul Chaignon jsonw_start_array(json_wtr); /* root array */
614ec202509SPaul Chaignon for (i = 0; i < nb_fds; i++) {
615ec202509SPaul Chaignon err = show_prog(fds[i]);
616ec202509SPaul Chaignon if (err) {
617ec202509SPaul Chaignon for (; i < nb_fds; i++)
618ec202509SPaul Chaignon close(fds[i]);
619ec202509SPaul Chaignon break;
620ec202509SPaul Chaignon }
621ec202509SPaul Chaignon close(fds[i]);
622ec202509SPaul Chaignon }
623ec202509SPaul Chaignon if (json_output && nb_fds > 1)
624ec202509SPaul Chaignon jsonw_end_array(json_wtr); /* root array */
625ec202509SPaul Chaignon
626ec202509SPaul Chaignon exit_free:
627ec202509SPaul Chaignon free(fds);
628ec202509SPaul Chaignon return err;
629ec202509SPaul Chaignon }
630ec202509SPaul Chaignon
do_show(int argc,char ** argv)63171bb428fSJakub Kicinski static int do_show(int argc, char **argv)
632743cc665SQuentin Monnet {
633743cc665SQuentin Monnet __u32 id = 0;
63471bb428fSJakub Kicinski int err;
63571bb428fSJakub Kicinski int fd;
63671bb428fSJakub Kicinski
63746241271SQuentin Monnet if (show_pinned) {
6388f184732SQuentin Monnet prog_table = hashmap__new(hash_fn_for_key_as_id,
6398f184732SQuentin Monnet equal_fn_for_key_as_id, NULL);
640622a5b58SMauricio Vásquez if (IS_ERR(prog_table)) {
6418f184732SQuentin Monnet p_err("failed to create hashmap for pinned paths");
6428f184732SQuentin Monnet return -1;
6438f184732SQuentin Monnet }
6448f184732SQuentin Monnet build_pinned_obj_table(prog_table, BPF_OBJ_PROG);
64546241271SQuentin Monnet }
646d53dee3fSAndrii Nakryiko build_obj_refs_table(&refs_table, BPF_OBJ_PROG);
6474990f1f4SPrashant Bhole
648ec202509SPaul Chaignon if (argc == 2)
649ec202509SPaul Chaignon return do_show_subset(argc, argv);
65071bb428fSJakub Kicinski
65171bb428fSJakub Kicinski if (argc)
65271bb428fSJakub Kicinski return BAD_ARG();
65371bb428fSJakub Kicinski
654743cc665SQuentin Monnet if (json_output)
655743cc665SQuentin Monnet jsonw_start_array(json_wtr);
65671bb428fSJakub Kicinski while (true) {
65771bb428fSJakub Kicinski err = bpf_prog_get_next_id(id, &id);
65871bb428fSJakub Kicinski if (err) {
6591739c26dSQuentin Monnet if (errno == ENOENT) {
6601739c26dSQuentin Monnet err = 0;
66171bb428fSJakub Kicinski break;
6621739c26dSQuentin Monnet }
6639a5ab8bfSQuentin Monnet p_err("can't get next program: %s%s", strerror(errno),
6649a5ab8bfSQuentin Monnet errno == EINVAL ? " -- kernel too old?" : "");
665743cc665SQuentin Monnet err = -1;
666743cc665SQuentin Monnet break;
66771bb428fSJakub Kicinski }
66871bb428fSJakub Kicinski
66971bb428fSJakub Kicinski fd = bpf_prog_get_fd_by_id(id);
67071bb428fSJakub Kicinski if (fd < 0) {
6718207c6ddSJakub Kicinski if (errno == ENOENT)
6728207c6ddSJakub Kicinski continue;
6739a5ab8bfSQuentin Monnet p_err("can't get prog by id (%u): %s",
67471bb428fSJakub Kicinski id, strerror(errno));
675743cc665SQuentin Monnet err = -1;
676743cc665SQuentin Monnet break;
67771bb428fSJakub Kicinski }
67871bb428fSJakub Kicinski
67971bb428fSJakub Kicinski err = show_prog(fd);
68071bb428fSJakub Kicinski close(fd);
68171bb428fSJakub Kicinski if (err)
682743cc665SQuentin Monnet break;
68371bb428fSJakub Kicinski }
68471bb428fSJakub Kicinski
685743cc665SQuentin Monnet if (json_output)
686743cc665SQuentin Monnet jsonw_end_array(json_wtr);
687743cc665SQuentin Monnet
688d6699f8eSQuentin Monnet delete_obj_refs_table(refs_table);
689d53dee3fSAndrii Nakryiko
69046241271SQuentin Monnet if (show_pinned)
6918f184732SQuentin Monnet delete_pinned_obj_table(prog_table);
69246241271SQuentin Monnet
693743cc665SQuentin Monnet return err;
69471bb428fSJakub Kicinski }
69571bb428fSJakub Kicinski
696ec202509SPaul Chaignon static int
prog_dump(struct bpf_prog_info * info,enum dump_mode mode,char * filepath,bool opcodes,bool visual,bool linum)697ec202509SPaul Chaignon prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
698ec202509SPaul Chaignon char *filepath, bool opcodes, bool visual, bool linum)
69971bb428fSJakub Kicinski {
700b053b439SMartin KaFai Lau struct bpf_prog_linfo *prog_linfo = NULL;
7013ddeac67SJakub Kicinski const char *disasm_opt = NULL;
7027105e828SDaniel Borkmann struct dump_data dd = {};
703cae73f23SSong Liu void *func_info = NULL;
704254471e5SYonghong Song struct btf *btf = NULL;
705254471e5SYonghong Song char func_sig[1024];
70671bb428fSJakub Kicinski unsigned char *buf;
707cae73f23SSong Liu __u32 member_len;
708ebbd7f64SQuentin Monnet int fd, err = -1;
70971bb428fSJakub Kicinski ssize_t n;
71071bb428fSJakub Kicinski
711cae73f23SSong Liu if (mode == DUMP_JITED) {
7125b79bcdfSToke Høiland-Jørgensen if (info->jited_prog_len == 0 || !info->jited_prog_insns) {
7139a5ab8bfSQuentin Monnet p_info("no instructions returned");
714ec202509SPaul Chaignon return -1;
715f84192eeSSandipan Das }
71609f44b75SAndrii Nakryiko buf = u64_to_ptr(info->jited_prog_insns);
717cae73f23SSong Liu member_len = info->jited_prog_len;
718cae73f23SSong Liu } else { /* DUMP_XLATED */
719d95f1e8bSToke Høiland-Jørgensen if (info->xlated_prog_len == 0 || !info->xlated_prog_insns) {
7207105e828SDaniel Borkmann p_err("error retrieving insn dump: kernel.kptr_restrict set?");
721ec202509SPaul Chaignon return -1;
7227105e828SDaniel Borkmann }
72309f44b75SAndrii Nakryiko buf = u64_to_ptr(info->xlated_prog_insns);
724cae73f23SSong Liu member_len = info->xlated_prog_len;
725cae73f23SSong Liu }
7267105e828SDaniel Borkmann
72786f4b7f2SQuentin Monnet if (info->btf_id) {
72886f4b7f2SQuentin Monnet btf = btf__load_from_kernel_by_id(info->btf_id);
729d1313e01SSahid Orentino Ferdjaoui if (!btf) {
730254471e5SYonghong Song p_err("failed to get btf");
731ec202509SPaul Chaignon return -1;
732254471e5SYonghong Song }
73386f4b7f2SQuentin Monnet }
734254471e5SYonghong Song
73509f44b75SAndrii Nakryiko func_info = u64_to_ptr(info->func_info);
736cae73f23SSong Liu
737cae73f23SSong Liu if (info->nr_line_info) {
738cae73f23SSong Liu prog_linfo = bpf_prog_linfo__new(info);
739b053b439SMartin KaFai Lau if (!prog_linfo)
74010a5ce98SMartin KaFai Lau p_info("error in processing bpf_line_info. continue without it.");
741b053b439SMartin KaFai Lau }
742b053b439SMartin KaFai Lau
74371bb428fSJakub Kicinski if (filepath) {
74471bb428fSJakub Kicinski fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
74571bb428fSJakub Kicinski if (fd < 0) {
7469a5ab8bfSQuentin Monnet p_err("can't open file %s: %s", filepath,
74771bb428fSJakub Kicinski strerror(errno));
748ebbd7f64SQuentin Monnet goto exit_free;
74971bb428fSJakub Kicinski }
75071bb428fSJakub Kicinski
751cae73f23SSong Liu n = write(fd, buf, member_len);
75271bb428fSJakub Kicinski close(fd);
75309f44b75SAndrii Nakryiko if (n != (ssize_t)member_len) {
7549a5ab8bfSQuentin Monnet p_err("error writing output file: %s",
75571bb428fSJakub Kicinski n < 0 ? strerror(errno) : "short write");
756ebbd7f64SQuentin Monnet goto exit_free;
75771bb428fSJakub Kicinski }
75852c84d36SQuentin Monnet
75952c84d36SQuentin Monnet if (json_output)
76052c84d36SQuentin Monnet jsonw_null(json_wtr);
761cae73f23SSong Liu } else if (mode == DUMP_JITED) {
762e6593596SJiong Wang const char *name = NULL;
763e6593596SJiong Wang
764cae73f23SSong Liu if (info->ifindex) {
765ce4f6608SQuentin Monnet name = ifindex_to_arch(info->ifindex, info->netns_dev,
766ce4f6608SQuentin Monnet info->netns_ino, &disasm_opt);
767e6593596SJiong Wang if (!name)
768ebbd7f64SQuentin Monnet goto exit_free;
769e6593596SJiong Wang }
770e6593596SJiong Wang
771cae73f23SSong Liu if (info->nr_jited_func_lens && info->jited_func_lens) {
772f7f62c71SSandipan Das struct kernel_sym *sym = NULL;
773254471e5SYonghong Song struct bpf_func_info *record;
774f7f62c71SSandipan Das char sym_name[SYM_MAX_NAME];
775f7f62c71SSandipan Das unsigned char *img = buf;
776f7f62c71SSandipan Das __u64 *ksyms = NULL;
777f7f62c71SSandipan Das __u32 *lens;
778f7f62c71SSandipan Das __u32 i;
779cae73f23SSong Liu if (info->nr_jited_ksyms) {
780f7f62c71SSandipan Das kernel_syms_load(&dd);
78109f44b75SAndrii Nakryiko ksyms = u64_to_ptr(info->jited_ksyms);
782f7f62c71SSandipan Das }
783f7f62c71SSandipan Das
784f7f62c71SSandipan Das if (json_output)
785f7f62c71SSandipan Das jsonw_start_array(json_wtr);
786f7f62c71SSandipan Das
78709f44b75SAndrii Nakryiko lens = u64_to_ptr(info->jited_func_lens);
788cae73f23SSong Liu for (i = 0; i < info->nr_jited_func_lens; i++) {
789f7f62c71SSandipan Das if (ksyms) {
790f7f62c71SSandipan Das sym = kernel_syms_search(&dd, ksyms[i]);
791f7f62c71SSandipan Das if (sym)
792f7f62c71SSandipan Das sprintf(sym_name, "%s", sym->name);
793f7f62c71SSandipan Das else
794f7f62c71SSandipan Das sprintf(sym_name, "0x%016llx", ksyms[i]);
795f7f62c71SSandipan Das } else {
796f7f62c71SSandipan Das strcpy(sym_name, "unknown");
797f7f62c71SSandipan Das }
798f7f62c71SSandipan Das
799254471e5SYonghong Song if (func_info) {
800cae73f23SSong Liu record = func_info + i * info->func_info_rec_size;
801254471e5SYonghong Song btf_dumper_type_only(btf, record->type_id,
802254471e5SYonghong Song func_sig,
803254471e5SYonghong Song sizeof(func_sig));
804254471e5SYonghong Song }
805254471e5SYonghong Song
806f7f62c71SSandipan Das if (json_output) {
807f7f62c71SSandipan Das jsonw_start_object(json_wtr);
808254471e5SYonghong Song if (func_info && func_sig[0] != '\0') {
809254471e5SYonghong Song jsonw_name(json_wtr, "proto");
810254471e5SYonghong Song jsonw_string(json_wtr, func_sig);
811254471e5SYonghong Song }
812f7f62c71SSandipan Das jsonw_name(json_wtr, "name");
813f7f62c71SSandipan Das jsonw_string(json_wtr, sym_name);
814f7f62c71SSandipan Das jsonw_name(json_wtr, "insns");
815f7f62c71SSandipan Das } else {
816254471e5SYonghong Song if (func_info && func_sig[0] != '\0')
817254471e5SYonghong Song printf("%s:\n", func_sig);
818f7f62c71SSandipan Das printf("%s:\n", sym_name);
819f7f62c71SSandipan Das }
820f7f62c71SSandipan Das
821*6bc209dbSAmir Mohammadi if (ksyms) {
82255b4de58SQuentin Monnet if (disasm_print_insn(img, lens[i], opcodes,
823b053b439SMartin KaFai Lau name, disasm_opt, btf,
824b053b439SMartin KaFai Lau prog_linfo, ksyms[i], i,
82555b4de58SQuentin Monnet linum))
82655b4de58SQuentin Monnet goto exit_free;
827*6bc209dbSAmir Mohammadi } else {
828*6bc209dbSAmir Mohammadi if (disasm_print_insn(img, lens[i], opcodes,
829*6bc209dbSAmir Mohammadi name, disasm_opt, btf,
830*6bc209dbSAmir Mohammadi NULL, 0, 0, false))
831*6bc209dbSAmir Mohammadi goto exit_free;
832*6bc209dbSAmir Mohammadi }
833b053b439SMartin KaFai Lau
834f7f62c71SSandipan Das img += lens[i];
835f7f62c71SSandipan Das
836f7f62c71SSandipan Das if (json_output)
837f7f62c71SSandipan Das jsonw_end_object(json_wtr);
838f7f62c71SSandipan Das else
839f7f62c71SSandipan Das printf("\n");
840f7f62c71SSandipan Das }
841f7f62c71SSandipan Das
842f7f62c71SSandipan Das if (json_output)
843f7f62c71SSandipan Das jsonw_end_array(json_wtr);
844f7f62c71SSandipan Das } else {
84555b4de58SQuentin Monnet if (disasm_print_insn(buf, member_len, opcodes, name,
84655b4de58SQuentin Monnet disasm_opt, btf, NULL, 0, 0,
84755b4de58SQuentin Monnet false))
84855b4de58SQuentin Monnet goto exit_free;
849f7f62c71SSandipan Das }
8507105e828SDaniel Borkmann } else {
8517105e828SDaniel Borkmann kernel_syms_load(&dd);
852cae73f23SSong Liu dd.nr_jited_ksyms = info->nr_jited_ksyms;
85309f44b75SAndrii Nakryiko dd.jited_ksyms = u64_to_ptr(info->jited_ksyms);
854254471e5SYonghong Song dd.btf = btf;
855254471e5SYonghong Song dd.func_info = func_info;
856cae73f23SSong Liu dd.finfo_rec_size = info->func_info_rec_size;
857b053b439SMartin KaFai Lau dd.prog_linfo = prog_linfo;
858f84192eeSSandipan Das
85905a06be7SQuentin Monnet if (json_output)
8609fd49684SQuentin Monnet dump_xlated_json(&dd, buf, member_len, opcodes, linum);
8619fd49684SQuentin Monnet else if (visual)
8627483a7a7SQuentin Monnet dump_xlated_cfg(&dd, buf, member_len, opcodes, linum);
863f05e2c32SQuentin Monnet else
8649fd49684SQuentin Monnet dump_xlated_plain(&dd, buf, member_len, opcodes, linum);
8657105e828SDaniel Borkmann kernel_syms_destroy(&dd);
8667105e828SDaniel Borkmann }
86771bb428fSJakub Kicinski
868ebbd7f64SQuentin Monnet err = 0;
869369e955bSQuentin Monnet
870ebbd7f64SQuentin Monnet exit_free:
871ebbd7f64SQuentin Monnet btf__free(btf);
872ebbd7f64SQuentin Monnet bpf_prog_linfo__free(prog_linfo);
873ebbd7f64SQuentin Monnet return err;
874ec202509SPaul Chaignon }
87571bb428fSJakub Kicinski
do_dump(int argc,char ** argv)876ec202509SPaul Chaignon static int do_dump(int argc, char **argv)
877ec202509SPaul Chaignon {
878c59765cfSDave Marchevsky struct bpf_prog_info info;
879c59765cfSDave Marchevsky __u32 info_len = sizeof(info);
880c59765cfSDave Marchevsky size_t info_data_sz = 0;
881c59765cfSDave Marchevsky void *info_data = NULL;
882ec202509SPaul Chaignon char *filepath = NULL;
883ec202509SPaul Chaignon bool opcodes = false;
884ec202509SPaul Chaignon bool visual = false;
885ec202509SPaul Chaignon enum dump_mode mode;
886ec202509SPaul Chaignon bool linum = false;
887ec202509SPaul Chaignon int nb_fds, i = 0;
888c59765cfSDave Marchevsky int *fds = NULL;
889ec202509SPaul Chaignon int err = -1;
890ec202509SPaul Chaignon
891ec202509SPaul Chaignon if (is_prefix(*argv, "jited")) {
892ec202509SPaul Chaignon if (disasm_init())
89371bb428fSJakub Kicinski return -1;
894ec202509SPaul Chaignon mode = DUMP_JITED;
895ec202509SPaul Chaignon } else if (is_prefix(*argv, "xlated")) {
896ec202509SPaul Chaignon mode = DUMP_XLATED;
897ec202509SPaul Chaignon } else {
898ec202509SPaul Chaignon p_err("expected 'xlated' or 'jited', got: %s", *argv);
899ec202509SPaul Chaignon return -1;
900ec202509SPaul Chaignon }
901ec202509SPaul Chaignon NEXT_ARG();
902ec202509SPaul Chaignon
903ec202509SPaul Chaignon if (argc < 2)
904ec202509SPaul Chaignon usage();
905ec202509SPaul Chaignon
906ec202509SPaul Chaignon fds = malloc(sizeof(int));
907ec202509SPaul Chaignon if (!fds) {
908ec202509SPaul Chaignon p_err("mem alloc failed");
909ec202509SPaul Chaignon return -1;
910ec202509SPaul Chaignon }
911ec202509SPaul Chaignon nb_fds = prog_parse_fds(&argc, &argv, &fds);
912ec202509SPaul Chaignon if (nb_fds < 1)
913ec202509SPaul Chaignon goto exit_free;
914ec202509SPaul Chaignon
9159b79f027SQuentin Monnet while (argc) {
916ec202509SPaul Chaignon if (is_prefix(*argv, "file")) {
917ec202509SPaul Chaignon NEXT_ARG();
918ec202509SPaul Chaignon if (!argc) {
919ec202509SPaul Chaignon p_err("expected file path");
920ec202509SPaul Chaignon goto exit_close;
921ec202509SPaul Chaignon }
922ec202509SPaul Chaignon if (nb_fds > 1) {
923ec202509SPaul Chaignon p_err("several programs matched");
924ec202509SPaul Chaignon goto exit_close;
925ec202509SPaul Chaignon }
926ec202509SPaul Chaignon
927ec202509SPaul Chaignon filepath = *argv;
928ec202509SPaul Chaignon NEXT_ARG();
929ec202509SPaul Chaignon } else if (is_prefix(*argv, "opcodes")) {
930ec202509SPaul Chaignon opcodes = true;
931ec202509SPaul Chaignon NEXT_ARG();
932ec202509SPaul Chaignon } else if (is_prefix(*argv, "visual")) {
933ec202509SPaul Chaignon if (nb_fds > 1) {
934ec202509SPaul Chaignon p_err("several programs matched");
935ec202509SPaul Chaignon goto exit_close;
936ec202509SPaul Chaignon }
937ec202509SPaul Chaignon
938ec202509SPaul Chaignon visual = true;
939ec202509SPaul Chaignon NEXT_ARG();
940ec202509SPaul Chaignon } else if (is_prefix(*argv, "linum")) {
941ec202509SPaul Chaignon linum = true;
942ec202509SPaul Chaignon NEXT_ARG();
9439b79f027SQuentin Monnet } else {
9449b79f027SQuentin Monnet usage();
9459b79f027SQuentin Monnet goto exit_close;
9469b79f027SQuentin Monnet }
947ec202509SPaul Chaignon }
948ec202509SPaul Chaignon
9499b79f027SQuentin Monnet if (filepath && (opcodes || visual || linum)) {
9509b79f027SQuentin Monnet p_err("'file' is not compatible with 'opcodes', 'visual', or 'linum'");
951ec202509SPaul Chaignon goto exit_close;
952ec202509SPaul Chaignon }
95305a06be7SQuentin Monnet if (json_output && visual) {
95405a06be7SQuentin Monnet p_err("'visual' is not compatible with JSON output");
95505a06be7SQuentin Monnet goto exit_close;
95605a06be7SQuentin Monnet }
957ec202509SPaul Chaignon
958ec202509SPaul Chaignon if (json_output && nb_fds > 1)
959ec202509SPaul Chaignon jsonw_start_array(json_wtr); /* root array */
960ec202509SPaul Chaignon for (i = 0; i < nb_fds; i++) {
961c59765cfSDave Marchevsky memset(&info, 0, sizeof(info));
962c59765cfSDave Marchevsky
96338f0408eSIlya Leoshkevich err = bpf_prog_get_info_by_fd(fds[i], &info, &info_len);
964c59765cfSDave Marchevsky if (err) {
965c59765cfSDave Marchevsky p_err("can't get prog info: %s", strerror(errno));
966c59765cfSDave Marchevsky break;
967c59765cfSDave Marchevsky }
968c59765cfSDave Marchevsky
969c59765cfSDave Marchevsky err = prep_prog_info(&info, mode, &info_data, &info_data_sz);
970c59765cfSDave Marchevsky if (err) {
971c59765cfSDave Marchevsky p_err("can't grow prog info_data");
972c59765cfSDave Marchevsky break;
973c59765cfSDave Marchevsky }
974c59765cfSDave Marchevsky
97538f0408eSIlya Leoshkevich err = bpf_prog_get_info_by_fd(fds[i], &info, &info_len);
976c59765cfSDave Marchevsky if (err) {
977ec202509SPaul Chaignon p_err("can't get prog info: %s", strerror(errno));
978ec202509SPaul Chaignon break;
979ec202509SPaul Chaignon }
980ec202509SPaul Chaignon
981ec202509SPaul Chaignon if (json_output && nb_fds > 1) {
982ec202509SPaul Chaignon jsonw_start_object(json_wtr); /* prog object */
983b662000aSRaman Shukhau print_prog_header_json(&info, fds[i]);
984ec202509SPaul Chaignon jsonw_name(json_wtr, "insns");
985ec202509SPaul Chaignon } else if (nb_fds > 1) {
986b662000aSRaman Shukhau print_prog_header_plain(&info, fds[i]);
987ec202509SPaul Chaignon }
988ec202509SPaul Chaignon
989c59765cfSDave Marchevsky err = prog_dump(&info, mode, filepath, opcodes, visual, linum);
990ec202509SPaul Chaignon
991ec202509SPaul Chaignon if (json_output && nb_fds > 1)
992ec202509SPaul Chaignon jsonw_end_object(json_wtr); /* prog object */
993ec202509SPaul Chaignon else if (i != nb_fds - 1 && nb_fds > 1)
994ec202509SPaul Chaignon printf("\n");
995ec202509SPaul Chaignon
996ec202509SPaul Chaignon if (err)
997ec202509SPaul Chaignon break;
998ec202509SPaul Chaignon close(fds[i]);
999ec202509SPaul Chaignon }
1000ec202509SPaul Chaignon if (json_output && nb_fds > 1)
1001ec202509SPaul Chaignon jsonw_end_array(json_wtr); /* root array */
1002ec202509SPaul Chaignon
1003ec202509SPaul Chaignon exit_close:
1004ec202509SPaul Chaignon for (; i < nb_fds; i++)
1005ec202509SPaul Chaignon close(fds[i]);
1006ec202509SPaul Chaignon exit_free:
1007c59765cfSDave Marchevsky free(info_data);
1008ec202509SPaul Chaignon free(fds);
1009ec202509SPaul Chaignon return err;
101071bb428fSJakub Kicinski }
101171bb428fSJakub Kicinski
do_pin(int argc,char ** argv)101271bb428fSJakub Kicinski static int do_pin(int argc, char **argv)
101371bb428fSJakub Kicinski {
1014004b45c0SQuentin Monnet int err;
1015004b45c0SQuentin Monnet
101675a1e792SQuentin Monnet err = do_pin_any(argc, argv, prog_parse_fd);
1017004b45c0SQuentin Monnet if (!err && json_output)
1018004b45c0SQuentin Monnet jsonw_null(json_wtr);
1019004b45c0SQuentin Monnet return err;
102071bb428fSJakub Kicinski }
102171bb428fSJakub Kicinski
10223ff5a4dcSJakub Kicinski struct map_replace {
10233ff5a4dcSJakub Kicinski int idx;
10243ff5a4dcSJakub Kicinski int fd;
10253ff5a4dcSJakub Kicinski char *name;
10263ff5a4dcSJakub Kicinski };
10273ff5a4dcSJakub Kicinski
map_replace_compar(const void * p1,const void * p2)1028c101189bSQuentin Monnet static int map_replace_compar(const void *p1, const void *p2)
10293ff5a4dcSJakub Kicinski {
10303ff5a4dcSJakub Kicinski const struct map_replace *a = p1, *b = p2;
10313ff5a4dcSJakub Kicinski
10323ff5a4dcSJakub Kicinski return a->idx - b->idx;
10333ff5a4dcSJakub Kicinski }
10343ff5a4dcSJakub Kicinski
parse_attach_detach_args(int argc,char ** argv,int * progfd,enum bpf_attach_type * attach_type,int * mapfd)1035092f0892SStanislav Fomichev static int parse_attach_detach_args(int argc, char **argv, int *progfd,
1036092f0892SStanislav Fomichev enum bpf_attach_type *attach_type,
1037092f0892SStanislav Fomichev int *mapfd)
1038092f0892SStanislav Fomichev {
1039092f0892SStanislav Fomichev if (!REQ_ARGS(3))
1040092f0892SStanislav Fomichev return -EINVAL;
1041092f0892SStanislav Fomichev
1042092f0892SStanislav Fomichev *progfd = prog_parse_fd(&argc, &argv);
1043092f0892SStanislav Fomichev if (*progfd < 0)
1044092f0892SStanislav Fomichev return *progfd;
1045092f0892SStanislav Fomichev
1046092f0892SStanislav Fomichev *attach_type = parse_attach_type(*argv);
1047092f0892SStanislav Fomichev if (*attach_type == __MAX_BPF_ATTACH_TYPE) {
1048092f0892SStanislav Fomichev p_err("invalid attach/detach type");
1049092f0892SStanislav Fomichev return -EINVAL;
1050092f0892SStanislav Fomichev }
1051092f0892SStanislav Fomichev
1052092f0892SStanislav Fomichev if (*attach_type == BPF_FLOW_DISSECTOR) {
1053f9b7ff0dSLorenz Bauer *mapfd = 0;
1054092f0892SStanislav Fomichev return 0;
1055092f0892SStanislav Fomichev }
1056092f0892SStanislav Fomichev
1057092f0892SStanislav Fomichev NEXT_ARG();
1058092f0892SStanislav Fomichev if (!REQ_ARGS(2))
1059092f0892SStanislav Fomichev return -EINVAL;
1060092f0892SStanislav Fomichev
1061092f0892SStanislav Fomichev *mapfd = map_parse_fd(&argc, &argv);
1062092f0892SStanislav Fomichev if (*mapfd < 0)
1063092f0892SStanislav Fomichev return *mapfd;
1064092f0892SStanislav Fomichev
1065092f0892SStanislav Fomichev return 0;
1066092f0892SStanislav Fomichev }
1067092f0892SStanislav Fomichev
do_attach(int argc,char ** argv)1068b7d3826cSJohn Fastabend static int do_attach(int argc, char **argv)
1069b7d3826cSJohn Fastabend {
1070b7d3826cSJohn Fastabend enum bpf_attach_type attach_type;
1071092f0892SStanislav Fomichev int err, progfd;
1072092f0892SStanislav Fomichev int mapfd;
1073b7d3826cSJohn Fastabend
1074092f0892SStanislav Fomichev err = parse_attach_detach_args(argc, argv,
1075092f0892SStanislav Fomichev &progfd, &attach_type, &mapfd);
1076092f0892SStanislav Fomichev if (err)
1077092f0892SStanislav Fomichev return err;
1078b7d3826cSJohn Fastabend
1079b7d3826cSJohn Fastabend err = bpf_prog_attach(progfd, mapfd, attach_type, 0);
1080b7d3826cSJohn Fastabend if (err) {
1081b7d3826cSJohn Fastabend p_err("failed prog attach to map");
1082b7d3826cSJohn Fastabend return -EINVAL;
1083b7d3826cSJohn Fastabend }
1084b7d3826cSJohn Fastabend
1085b7d3826cSJohn Fastabend if (json_output)
1086b7d3826cSJohn Fastabend jsonw_null(json_wtr);
1087b7d3826cSJohn Fastabend return 0;
1088b7d3826cSJohn Fastabend }
1089b7d3826cSJohn Fastabend
do_detach(int argc,char ** argv)1090b7d3826cSJohn Fastabend static int do_detach(int argc, char **argv)
1091b7d3826cSJohn Fastabend {
1092b7d3826cSJohn Fastabend enum bpf_attach_type attach_type;
1093092f0892SStanislav Fomichev int err, progfd;
1094092f0892SStanislav Fomichev int mapfd;
1095b7d3826cSJohn Fastabend
1096092f0892SStanislav Fomichev err = parse_attach_detach_args(argc, argv,
1097092f0892SStanislav Fomichev &progfd, &attach_type, &mapfd);
1098092f0892SStanislav Fomichev if (err)
1099092f0892SStanislav Fomichev return err;
1100b7d3826cSJohn Fastabend
1101b7d3826cSJohn Fastabend err = bpf_prog_detach2(progfd, mapfd, attach_type);
1102b7d3826cSJohn Fastabend if (err) {
1103b7d3826cSJohn Fastabend p_err("failed prog detach from map");
1104b7d3826cSJohn Fastabend return -EINVAL;
1105b7d3826cSJohn Fastabend }
1106b7d3826cSJohn Fastabend
1107b7d3826cSJohn Fastabend if (json_output)
1108b7d3826cSJohn Fastabend jsonw_null(json_wtr);
1109b7d3826cSJohn Fastabend return 0;
1110b7d3826cSJohn Fastabend }
111177380998SStanislav Fomichev
check_single_stdin(char * file_data_in,char * file_ctx_in)1112ba95c745SQuentin Monnet static int check_single_stdin(char *file_data_in, char *file_ctx_in)
1113ba95c745SQuentin Monnet {
1114ba95c745SQuentin Monnet if (file_data_in && file_ctx_in &&
1115ba95c745SQuentin Monnet !strcmp(file_data_in, "-") && !strcmp(file_ctx_in, "-")) {
1116ba95c745SQuentin Monnet p_err("cannot use standard input for both data_in and ctx_in");
1117ba95c745SQuentin Monnet return -1;
1118ba95c745SQuentin Monnet }
1119ba95c745SQuentin Monnet
1120ba95c745SQuentin Monnet return 0;
1121ba95c745SQuentin Monnet }
1122ba95c745SQuentin Monnet
get_run_data(const char * fname,void ** data_ptr,unsigned int * size)1123ba95c745SQuentin Monnet static int get_run_data(const char *fname, void **data_ptr, unsigned int *size)
1124ba95c745SQuentin Monnet {
1125ba95c745SQuentin Monnet size_t block_size = 256;
1126ba95c745SQuentin Monnet size_t buf_size = block_size;
1127ba95c745SQuentin Monnet size_t nb_read = 0;
1128ba95c745SQuentin Monnet void *tmp;
1129ba95c745SQuentin Monnet FILE *f;
1130ba95c745SQuentin Monnet
1131ba95c745SQuentin Monnet if (!fname) {
1132ba95c745SQuentin Monnet *data_ptr = NULL;
1133ba95c745SQuentin Monnet *size = 0;
1134ba95c745SQuentin Monnet return 0;
1135ba95c745SQuentin Monnet }
1136ba95c745SQuentin Monnet
1137ba95c745SQuentin Monnet if (!strcmp(fname, "-"))
1138ba95c745SQuentin Monnet f = stdin;
1139ba95c745SQuentin Monnet else
1140ba95c745SQuentin Monnet f = fopen(fname, "r");
1141ba95c745SQuentin Monnet if (!f) {
1142ba95c745SQuentin Monnet p_err("failed to open %s: %s", fname, strerror(errno));
1143ba95c745SQuentin Monnet return -1;
1144ba95c745SQuentin Monnet }
1145ba95c745SQuentin Monnet
1146ba95c745SQuentin Monnet *data_ptr = malloc(block_size);
1147ba95c745SQuentin Monnet if (!*data_ptr) {
1148ba95c745SQuentin Monnet p_err("failed to allocate memory for data_in/ctx_in: %s",
1149ba95c745SQuentin Monnet strerror(errno));
1150ba95c745SQuentin Monnet goto err_fclose;
1151ba95c745SQuentin Monnet }
1152ba95c745SQuentin Monnet
1153ba95c745SQuentin Monnet while ((nb_read += fread(*data_ptr + nb_read, 1, block_size, f))) {
1154ba95c745SQuentin Monnet if (feof(f))
1155ba95c745SQuentin Monnet break;
1156ba95c745SQuentin Monnet if (ferror(f)) {
1157ba95c745SQuentin Monnet p_err("failed to read data_in/ctx_in from %s: %s",
1158ba95c745SQuentin Monnet fname, strerror(errno));
1159ba95c745SQuentin Monnet goto err_free;
1160ba95c745SQuentin Monnet }
1161ba95c745SQuentin Monnet if (nb_read > buf_size - block_size) {
1162ba95c745SQuentin Monnet if (buf_size == UINT32_MAX) {
1163ba95c745SQuentin Monnet p_err("data_in/ctx_in is too long (max: %d)",
1164ba95c745SQuentin Monnet UINT32_MAX);
1165ba95c745SQuentin Monnet goto err_free;
1166ba95c745SQuentin Monnet }
1167ba95c745SQuentin Monnet /* No space for fread()-ing next chunk; realloc() */
1168ba95c745SQuentin Monnet buf_size *= 2;
1169ba95c745SQuentin Monnet tmp = realloc(*data_ptr, buf_size);
1170ba95c745SQuentin Monnet if (!tmp) {
1171ba95c745SQuentin Monnet p_err("failed to reallocate data_in/ctx_in: %s",
1172ba95c745SQuentin Monnet strerror(errno));
1173ba95c745SQuentin Monnet goto err_free;
1174ba95c745SQuentin Monnet }
1175ba95c745SQuentin Monnet *data_ptr = tmp;
1176ba95c745SQuentin Monnet }
1177ba95c745SQuentin Monnet }
1178ba95c745SQuentin Monnet if (f != stdin)
1179ba95c745SQuentin Monnet fclose(f);
1180ba95c745SQuentin Monnet
1181ba95c745SQuentin Monnet *size = nb_read;
1182ba95c745SQuentin Monnet return 0;
1183ba95c745SQuentin Monnet
1184ba95c745SQuentin Monnet err_free:
1185ba95c745SQuentin Monnet free(*data_ptr);
1186ba95c745SQuentin Monnet *data_ptr = NULL;
1187ba95c745SQuentin Monnet err_fclose:
1188ba95c745SQuentin Monnet if (f != stdin)
1189ba95c745SQuentin Monnet fclose(f);
1190ba95c745SQuentin Monnet return -1;
1191ba95c745SQuentin Monnet }
1192ba95c745SQuentin Monnet
hex_print(void * data,unsigned int size,FILE * f)1193ba95c745SQuentin Monnet static void hex_print(void *data, unsigned int size, FILE *f)
1194ba95c745SQuentin Monnet {
1195ba95c745SQuentin Monnet size_t i, j;
1196ba95c745SQuentin Monnet char c;
1197ba95c745SQuentin Monnet
1198ba95c745SQuentin Monnet for (i = 0; i < size; i += 16) {
1199ba95c745SQuentin Monnet /* Row offset */
1200ba95c745SQuentin Monnet fprintf(f, "%07zx\t", i);
1201ba95c745SQuentin Monnet
1202ba95c745SQuentin Monnet /* Hexadecimal values */
1203ba95c745SQuentin Monnet for (j = i; j < i + 16 && j < size; j++)
1204ba95c745SQuentin Monnet fprintf(f, "%02x%s", *(uint8_t *)(data + j),
1205ba95c745SQuentin Monnet j % 2 ? " " : "");
1206ba95c745SQuentin Monnet for (; j < i + 16; j++)
1207ba95c745SQuentin Monnet fprintf(f, " %s", j % 2 ? " " : "");
1208ba95c745SQuentin Monnet
1209ba95c745SQuentin Monnet /* ASCII values (if relevant), '.' otherwise */
1210ba95c745SQuentin Monnet fprintf(f, "| ");
1211ba95c745SQuentin Monnet for (j = i; j < i + 16 && j < size; j++) {
1212ba95c745SQuentin Monnet c = *(char *)(data + j);
1213ba95c745SQuentin Monnet if (c < ' ' || c > '~')
1214ba95c745SQuentin Monnet c = '.';
1215ba95c745SQuentin Monnet fprintf(f, "%c%s", c, j == i + 7 ? " " : "");
1216ba95c745SQuentin Monnet }
1217ba95c745SQuentin Monnet
1218ba95c745SQuentin Monnet fprintf(f, "\n");
1219ba95c745SQuentin Monnet }
1220ba95c745SQuentin Monnet }
1221ba95c745SQuentin Monnet
1222ba95c745SQuentin Monnet static int
print_run_output(void * data,unsigned int size,const char * fname,const char * json_key)1223ba95c745SQuentin Monnet print_run_output(void *data, unsigned int size, const char *fname,
1224ba95c745SQuentin Monnet const char *json_key)
1225ba95c745SQuentin Monnet {
1226ba95c745SQuentin Monnet size_t nb_written;
1227ba95c745SQuentin Monnet FILE *f;
1228ba95c745SQuentin Monnet
1229ba95c745SQuentin Monnet if (!fname)
1230ba95c745SQuentin Monnet return 0;
1231ba95c745SQuentin Monnet
1232ba95c745SQuentin Monnet if (!strcmp(fname, "-")) {
1233ba95c745SQuentin Monnet f = stdout;
1234ba95c745SQuentin Monnet if (json_output) {
1235ba95c745SQuentin Monnet jsonw_name(json_wtr, json_key);
1236ba95c745SQuentin Monnet print_data_json(data, size);
1237ba95c745SQuentin Monnet } else {
1238ba95c745SQuentin Monnet hex_print(data, size, f);
1239ba95c745SQuentin Monnet }
1240ba95c745SQuentin Monnet return 0;
1241ba95c745SQuentin Monnet }
1242ba95c745SQuentin Monnet
1243ba95c745SQuentin Monnet f = fopen(fname, "w");
1244ba95c745SQuentin Monnet if (!f) {
1245ba95c745SQuentin Monnet p_err("failed to open %s: %s", fname, strerror(errno));
1246ba95c745SQuentin Monnet return -1;
1247ba95c745SQuentin Monnet }
1248ba95c745SQuentin Monnet
1249ba95c745SQuentin Monnet nb_written = fwrite(data, 1, size, f);
1250ba95c745SQuentin Monnet fclose(f);
1251ba95c745SQuentin Monnet if (nb_written != size) {
1252ba95c745SQuentin Monnet p_err("failed to write output data/ctx: %s", strerror(errno));
1253ba95c745SQuentin Monnet return -1;
1254ba95c745SQuentin Monnet }
1255ba95c745SQuentin Monnet
1256ba95c745SQuentin Monnet return 0;
1257ba95c745SQuentin Monnet }
1258ba95c745SQuentin Monnet
alloc_run_data(void ** data_ptr,unsigned int size_out)1259ba95c745SQuentin Monnet static int alloc_run_data(void **data_ptr, unsigned int size_out)
1260ba95c745SQuentin Monnet {
1261ba95c745SQuentin Monnet *data_ptr = calloc(size_out, 1);
1262ba95c745SQuentin Monnet if (!*data_ptr) {
1263ba95c745SQuentin Monnet p_err("failed to allocate memory for output data/ctx: %s",
1264ba95c745SQuentin Monnet strerror(errno));
1265ba95c745SQuentin Monnet return -1;
1266ba95c745SQuentin Monnet }
1267ba95c745SQuentin Monnet
1268ba95c745SQuentin Monnet return 0;
1269ba95c745SQuentin Monnet }
1270ba95c745SQuentin Monnet
do_run(int argc,char ** argv)1271ba95c745SQuentin Monnet static int do_run(int argc, char **argv)
1272ba95c745SQuentin Monnet {
1273ba95c745SQuentin Monnet char *data_fname_in = NULL, *data_fname_out = NULL;
1274ba95c745SQuentin Monnet char *ctx_fname_in = NULL, *ctx_fname_out = NULL;
1275ba95c745SQuentin Monnet const unsigned int default_size = SZ_32K;
1276ba95c745SQuentin Monnet void *data_in = NULL, *data_out = NULL;
1277ba95c745SQuentin Monnet void *ctx_in = NULL, *ctx_out = NULL;
1278ba95c745SQuentin Monnet unsigned int repeat = 1;
1279ba95c745SQuentin Monnet int fd, err;
12809cce5313SDelyan Kratunov LIBBPF_OPTS(bpf_test_run_opts, test_attr);
1281ba95c745SQuentin Monnet
1282ba95c745SQuentin Monnet if (!REQ_ARGS(4))
1283ba95c745SQuentin Monnet return -1;
1284ba95c745SQuentin Monnet
1285ba95c745SQuentin Monnet fd = prog_parse_fd(&argc, &argv);
1286ba95c745SQuentin Monnet if (fd < 0)
1287ba95c745SQuentin Monnet return -1;
1288ba95c745SQuentin Monnet
1289ba95c745SQuentin Monnet while (argc) {
1290ba95c745SQuentin Monnet if (detect_common_prefix(*argv, "data_in", "data_out",
1291ba95c745SQuentin Monnet "data_size_out", NULL))
1292ba95c745SQuentin Monnet return -1;
1293ba95c745SQuentin Monnet if (detect_common_prefix(*argv, "ctx_in", "ctx_out",
1294ba95c745SQuentin Monnet "ctx_size_out", NULL))
1295ba95c745SQuentin Monnet return -1;
1296ba95c745SQuentin Monnet
1297ba95c745SQuentin Monnet if (is_prefix(*argv, "data_in")) {
1298ba95c745SQuentin Monnet NEXT_ARG();
1299ba95c745SQuentin Monnet if (!REQ_ARGS(1))
1300ba95c745SQuentin Monnet return -1;
1301ba95c745SQuentin Monnet
1302ba95c745SQuentin Monnet data_fname_in = GET_ARG();
1303ba95c745SQuentin Monnet if (check_single_stdin(data_fname_in, ctx_fname_in))
1304ba95c745SQuentin Monnet return -1;
1305ba95c745SQuentin Monnet } else if (is_prefix(*argv, "data_out")) {
1306ba95c745SQuentin Monnet NEXT_ARG();
1307ba95c745SQuentin Monnet if (!REQ_ARGS(1))
1308ba95c745SQuentin Monnet return -1;
1309ba95c745SQuentin Monnet
1310ba95c745SQuentin Monnet data_fname_out = GET_ARG();
1311ba95c745SQuentin Monnet } else if (is_prefix(*argv, "data_size_out")) {
1312ba95c745SQuentin Monnet char *endptr;
1313ba95c745SQuentin Monnet
1314ba95c745SQuentin Monnet NEXT_ARG();
1315ba95c745SQuentin Monnet if (!REQ_ARGS(1))
1316ba95c745SQuentin Monnet return -1;
1317ba95c745SQuentin Monnet
1318ba95c745SQuentin Monnet test_attr.data_size_out = strtoul(*argv, &endptr, 0);
1319ba95c745SQuentin Monnet if (*endptr) {
1320ba95c745SQuentin Monnet p_err("can't parse %s as output data size",
1321ba95c745SQuentin Monnet *argv);
1322ba95c745SQuentin Monnet return -1;
1323ba95c745SQuentin Monnet }
1324ba95c745SQuentin Monnet NEXT_ARG();
1325ba95c745SQuentin Monnet } else if (is_prefix(*argv, "ctx_in")) {
1326ba95c745SQuentin Monnet NEXT_ARG();
1327ba95c745SQuentin Monnet if (!REQ_ARGS(1))
1328ba95c745SQuentin Monnet return -1;
1329ba95c745SQuentin Monnet
1330ba95c745SQuentin Monnet ctx_fname_in = GET_ARG();
1331ba95c745SQuentin Monnet if (check_single_stdin(data_fname_in, ctx_fname_in))
1332ba95c745SQuentin Monnet return -1;
1333ba95c745SQuentin Monnet } else if (is_prefix(*argv, "ctx_out")) {
1334ba95c745SQuentin Monnet NEXT_ARG();
1335ba95c745SQuentin Monnet if (!REQ_ARGS(1))
1336ba95c745SQuentin Monnet return -1;
1337ba95c745SQuentin Monnet
1338ba95c745SQuentin Monnet ctx_fname_out = GET_ARG();
1339ba95c745SQuentin Monnet } else if (is_prefix(*argv, "ctx_size_out")) {
1340ba95c745SQuentin Monnet char *endptr;
1341ba95c745SQuentin Monnet
1342ba95c745SQuentin Monnet NEXT_ARG();
1343ba95c745SQuentin Monnet if (!REQ_ARGS(1))
1344ba95c745SQuentin Monnet return -1;
1345ba95c745SQuentin Monnet
1346ba95c745SQuentin Monnet test_attr.ctx_size_out = strtoul(*argv, &endptr, 0);
1347ba95c745SQuentin Monnet if (*endptr) {
1348ba95c745SQuentin Monnet p_err("can't parse %s as output context size",
1349ba95c745SQuentin Monnet *argv);
1350ba95c745SQuentin Monnet return -1;
1351ba95c745SQuentin Monnet }
1352ba95c745SQuentin Monnet NEXT_ARG();
1353ba95c745SQuentin Monnet } else if (is_prefix(*argv, "repeat")) {
1354ba95c745SQuentin Monnet char *endptr;
1355ba95c745SQuentin Monnet
1356ba95c745SQuentin Monnet NEXT_ARG();
1357ba95c745SQuentin Monnet if (!REQ_ARGS(1))
1358ba95c745SQuentin Monnet return -1;
1359ba95c745SQuentin Monnet
1360ba95c745SQuentin Monnet repeat = strtoul(*argv, &endptr, 0);
1361ba95c745SQuentin Monnet if (*endptr) {
1362ba95c745SQuentin Monnet p_err("can't parse %s as repeat number",
1363ba95c745SQuentin Monnet *argv);
1364ba95c745SQuentin Monnet return -1;
1365ba95c745SQuentin Monnet }
1366ba95c745SQuentin Monnet NEXT_ARG();
1367ba95c745SQuentin Monnet } else {
1368ba95c745SQuentin Monnet p_err("expected no more arguments, 'data_in', 'data_out', 'data_size_out', 'ctx_in', 'ctx_out', 'ctx_size_out' or 'repeat', got: '%s'?",
1369ba95c745SQuentin Monnet *argv);
1370ba95c745SQuentin Monnet return -1;
1371ba95c745SQuentin Monnet }
1372ba95c745SQuentin Monnet }
1373ba95c745SQuentin Monnet
1374ba95c745SQuentin Monnet err = get_run_data(data_fname_in, &data_in, &test_attr.data_size_in);
1375ba95c745SQuentin Monnet if (err)
1376ba95c745SQuentin Monnet return -1;
1377ba95c745SQuentin Monnet
1378ba95c745SQuentin Monnet if (data_in) {
1379ba95c745SQuentin Monnet if (!test_attr.data_size_out)
1380ba95c745SQuentin Monnet test_attr.data_size_out = default_size;
1381ba95c745SQuentin Monnet err = alloc_run_data(&data_out, test_attr.data_size_out);
1382ba95c745SQuentin Monnet if (err)
1383ba95c745SQuentin Monnet goto free_data_in;
1384ba95c745SQuentin Monnet }
1385ba95c745SQuentin Monnet
1386ba95c745SQuentin Monnet err = get_run_data(ctx_fname_in, &ctx_in, &test_attr.ctx_size_in);
1387ba95c745SQuentin Monnet if (err)
1388ba95c745SQuentin Monnet goto free_data_out;
1389ba95c745SQuentin Monnet
1390ba95c745SQuentin Monnet if (ctx_in) {
1391ba95c745SQuentin Monnet if (!test_attr.ctx_size_out)
1392ba95c745SQuentin Monnet test_attr.ctx_size_out = default_size;
1393ba95c745SQuentin Monnet err = alloc_run_data(&ctx_out, test_attr.ctx_size_out);
1394ba95c745SQuentin Monnet if (err)
1395ba95c745SQuentin Monnet goto free_ctx_in;
1396ba95c745SQuentin Monnet }
1397ba95c745SQuentin Monnet
1398ba95c745SQuentin Monnet test_attr.repeat = repeat;
1399ba95c745SQuentin Monnet test_attr.data_in = data_in;
1400ba95c745SQuentin Monnet test_attr.data_out = data_out;
1401ba95c745SQuentin Monnet test_attr.ctx_in = ctx_in;
1402ba95c745SQuentin Monnet test_attr.ctx_out = ctx_out;
1403ba95c745SQuentin Monnet
14049cce5313SDelyan Kratunov err = bpf_prog_test_run_opts(fd, &test_attr);
1405ba95c745SQuentin Monnet if (err) {
1406ba95c745SQuentin Monnet p_err("failed to run program: %s", strerror(errno));
1407ba95c745SQuentin Monnet goto free_ctx_out;
1408ba95c745SQuentin Monnet }
1409ba95c745SQuentin Monnet
1410ba95c745SQuentin Monnet err = 0;
1411ba95c745SQuentin Monnet
1412ba95c745SQuentin Monnet if (json_output)
1413ba95c745SQuentin Monnet jsonw_start_object(json_wtr); /* root */
1414ba95c745SQuentin Monnet
1415ba95c745SQuentin Monnet /* Do not exit on errors occurring when printing output data/context,
1416ba95c745SQuentin Monnet * we still want to print return value and duration for program run.
1417ba95c745SQuentin Monnet */
1418ba95c745SQuentin Monnet if (test_attr.data_size_out)
1419ba95c745SQuentin Monnet err += print_run_output(test_attr.data_out,
1420ba95c745SQuentin Monnet test_attr.data_size_out,
1421ba95c745SQuentin Monnet data_fname_out, "data_out");
1422ba95c745SQuentin Monnet if (test_attr.ctx_size_out)
1423ba95c745SQuentin Monnet err += print_run_output(test_attr.ctx_out,
1424ba95c745SQuentin Monnet test_attr.ctx_size_out,
1425ba95c745SQuentin Monnet ctx_fname_out, "ctx_out");
1426ba95c745SQuentin Monnet
1427ba95c745SQuentin Monnet if (json_output) {
1428ba95c745SQuentin Monnet jsonw_uint_field(json_wtr, "retval", test_attr.retval);
1429ba95c745SQuentin Monnet jsonw_uint_field(json_wtr, "duration", test_attr.duration);
1430ba95c745SQuentin Monnet jsonw_end_object(json_wtr); /* root */
1431ba95c745SQuentin Monnet } else {
1432ba95c745SQuentin Monnet fprintf(stdout, "Return value: %u, duration%s: %uns\n",
1433ba95c745SQuentin Monnet test_attr.retval,
1434ba95c745SQuentin Monnet repeat > 1 ? " (average)" : "", test_attr.duration);
1435ba95c745SQuentin Monnet }
1436ba95c745SQuentin Monnet
1437ba95c745SQuentin Monnet free_ctx_out:
1438ba95c745SQuentin Monnet free(ctx_out);
1439ba95c745SQuentin Monnet free_ctx_in:
1440ba95c745SQuentin Monnet free(ctx_in);
1441ba95c745SQuentin Monnet free_data_out:
1442ba95c745SQuentin Monnet free(data_out);
1443ba95c745SQuentin Monnet free_data_in:
1444ba95c745SQuentin Monnet free(data_in);
1445ba95c745SQuentin Monnet
1446ba95c745SQuentin Monnet return err;
1447ba95c745SQuentin Monnet }
1448ba95c745SQuentin Monnet
14496ae32b29SQuentin Monnet static int
get_prog_type_by_name(const char * name,enum bpf_prog_type * prog_type,enum bpf_attach_type * expected_attach_type)14506ae32b29SQuentin Monnet get_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
14516ae32b29SQuentin Monnet enum bpf_attach_type *expected_attach_type)
14526ae32b29SQuentin Monnet {
14536ae32b29SQuentin Monnet libbpf_print_fn_t print_backup;
14546ae32b29SQuentin Monnet int ret;
14556ae32b29SQuentin Monnet
14566ae32b29SQuentin Monnet ret = libbpf_prog_type_by_name(name, prog_type, expected_attach_type);
14576ae32b29SQuentin Monnet if (!ret)
14586ae32b29SQuentin Monnet return ret;
14596ae32b29SQuentin Monnet
14606ae32b29SQuentin Monnet /* libbpf_prog_type_by_name() failed, let's re-run with debug level */
14616ae32b29SQuentin Monnet print_backup = libbpf_set_print(print_all_levels);
14626ae32b29SQuentin Monnet ret = libbpf_prog_type_by_name(name, prog_type, expected_attach_type);
14636ae32b29SQuentin Monnet libbpf_set_print(print_backup);
14646ae32b29SQuentin Monnet
14656ae32b29SQuentin Monnet return ret;
14666ae32b29SQuentin Monnet }
14676ae32b29SQuentin Monnet
146819526e70SWang Yufen static int
auto_attach_program(struct bpf_program * prog,const char * path)146919526e70SWang Yufen auto_attach_program(struct bpf_program *prog, const char *path)
147019526e70SWang Yufen {
147119526e70SWang Yufen struct bpf_link *link;
147219526e70SWang Yufen int err;
147319526e70SWang Yufen
147419526e70SWang Yufen link = bpf_program__attach(prog);
147519526e70SWang Yufen if (!link) {
147619526e70SWang Yufen p_info("Program %s does not support autoattach, falling back to pinning",
147719526e70SWang Yufen bpf_program__name(prog));
147819526e70SWang Yufen return bpf_obj_pin(bpf_program__fd(prog), path);
147919526e70SWang Yufen }
148019526e70SWang Yufen
148119526e70SWang Yufen err = bpf_link__pin(link, path);
148219526e70SWang Yufen bpf_link__destroy(link);
148319526e70SWang Yufen return err;
148419526e70SWang Yufen }
148519526e70SWang Yufen
148619526e70SWang Yufen static int
auto_attach_programs(struct bpf_object * obj,const char * path)148719526e70SWang Yufen auto_attach_programs(struct bpf_object *obj, const char *path)
148819526e70SWang Yufen {
148919526e70SWang Yufen struct bpf_program *prog;
149019526e70SWang Yufen char buf[PATH_MAX];
149119526e70SWang Yufen int err;
149219526e70SWang Yufen
149319526e70SWang Yufen bpf_object__for_each_program(prog, obj) {
149419526e70SWang Yufen err = pathname_concat(buf, sizeof(buf), path, bpf_program__name(prog));
149519526e70SWang Yufen if (err)
149619526e70SWang Yufen goto err_unpin_programs;
149719526e70SWang Yufen
149819526e70SWang Yufen err = auto_attach_program(prog, buf);
149919526e70SWang Yufen if (err)
150019526e70SWang Yufen goto err_unpin_programs;
150119526e70SWang Yufen }
150219526e70SWang Yufen
150319526e70SWang Yufen return 0;
150419526e70SWang Yufen
150519526e70SWang Yufen err_unpin_programs:
150619526e70SWang Yufen while ((prog = bpf_object__prev_program(obj, prog))) {
150719526e70SWang Yufen if (pathname_concat(buf, sizeof(buf), path, bpf_program__name(prog)))
150819526e70SWang Yufen continue;
150919526e70SWang Yufen
151019526e70SWang Yufen bpf_program__unpin(prog, buf);
151119526e70SWang Yufen }
151219526e70SWang Yufen
151319526e70SWang Yufen return err;
151419526e70SWang Yufen }
151519526e70SWang Yufen
load_with_options(int argc,char ** argv,bool first_prog_only)151677380998SStanislav Fomichev static int load_with_options(int argc, char **argv, bool first_prog_only)
151749a086c2SRoman Gushchin {
151832e3e58eSAndrii Nakryiko enum bpf_prog_type common_prog_type = BPF_PROG_TYPE_UNSPEC;
1519e00aca65SAndrii Nakryiko DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts,
1520e00aca65SAndrii Nakryiko .relaxed_maps = relaxed_maps,
1521e00aca65SAndrii Nakryiko );
152255d77807SQuentin Monnet enum bpf_attach_type expected_attach_type;
15233ff5a4dcSJakub Kicinski struct map_replace *map_replace = NULL;
152477380998SStanislav Fomichev struct bpf_program *prog = NULL, *pos;
15253ff5a4dcSJakub Kicinski unsigned int old_map_fds = 0;
15263767a94bSStanislav Fomichev const char *pinmaps = NULL;
1527f46392eeSLarysa Zaremba __u32 xdpmeta_ifindex = 0;
1528f46392eeSLarysa Zaremba __u32 offload_ifindex = 0;
152919526e70SWang Yufen bool auto_attach = false;
153049a086c2SRoman Gushchin struct bpf_object *obj;
1531c8406848SJakub Kicinski struct bpf_map *map;
1532c8406848SJakub Kicinski const char *pinfile;
15333ff5a4dcSJakub Kicinski unsigned int i, j;
153432e3e58eSAndrii Nakryiko const char *file;
15353ff5a4dcSJakub Kicinski int idx, err;
153649a086c2SRoman Gushchin
153732e3e58eSAndrii Nakryiko
15388d1fc3deSJakub Kicinski if (!REQ_ARGS(2))
15398d1fc3deSJakub Kicinski return -1;
154032e3e58eSAndrii Nakryiko file = GET_ARG();
15418d1fc3deSJakub Kicinski pinfile = GET_ARG();
154249a086c2SRoman Gushchin
1543ba6dd679SJakub Kicinski while (argc) {
154449f2cba3SJakub Kicinski if (is_prefix(*argv, "type")) {
154549f2cba3SJakub Kicinski NEXT_ARG();
154649f2cba3SJakub Kicinski
154732e3e58eSAndrii Nakryiko if (common_prog_type != BPF_PROG_TYPE_UNSPEC) {
154849f2cba3SJakub Kicinski p_err("program type already specified");
15493ff5a4dcSJakub Kicinski goto err_free_reuse_maps;
155049f2cba3SJakub Kicinski }
155149f2cba3SJakub Kicinski if (!REQ_ARGS(1))
15523ff5a4dcSJakub Kicinski goto err_free_reuse_maps;
155349f2cba3SJakub Kicinski
1554314f14abSStanislav Fomichev err = libbpf_prog_type_by_name(*argv, &common_prog_type,
1555314f14abSStanislav Fomichev &expected_attach_type);
1556314f14abSStanislav Fomichev if (err < 0) {
155749f2cba3SJakub Kicinski /* Put a '/' at the end of type to appease libbpf */
1558314f14abSStanislav Fomichev char *type = malloc(strlen(*argv) + 2);
1559314f14abSStanislav Fomichev
156049f2cba3SJakub Kicinski if (!type) {
156149f2cba3SJakub Kicinski p_err("mem alloc failed");
15623ff5a4dcSJakub Kicinski goto err_free_reuse_maps;
156349f2cba3SJakub Kicinski }
156449f2cba3SJakub Kicinski *type = 0;
156549f2cba3SJakub Kicinski strcat(type, *argv);
156649f2cba3SJakub Kicinski strcat(type, "/");
156749f2cba3SJakub Kicinski
15686ae32b29SQuentin Monnet err = get_prog_type_by_name(type, &common_prog_type,
1569c8406848SJakub Kicinski &expected_attach_type);
157049f2cba3SJakub Kicinski free(type);
1571c76e4c22STaeung Song if (err < 0)
15723ff5a4dcSJakub Kicinski goto err_free_reuse_maps;
1573314f14abSStanislav Fomichev }
1574c76e4c22STaeung Song
157549f2cba3SJakub Kicinski NEXT_ARG();
15763ff5a4dcSJakub Kicinski } else if (is_prefix(*argv, "map")) {
1577dde7011aSJakub Kicinski void *new_map_replace;
15783ff5a4dcSJakub Kicinski char *endptr, *name;
15793ff5a4dcSJakub Kicinski int fd;
15803ff5a4dcSJakub Kicinski
15813ff5a4dcSJakub Kicinski NEXT_ARG();
15823ff5a4dcSJakub Kicinski
15833ff5a4dcSJakub Kicinski if (!REQ_ARGS(4))
15843ff5a4dcSJakub Kicinski goto err_free_reuse_maps;
15853ff5a4dcSJakub Kicinski
15863ff5a4dcSJakub Kicinski if (is_prefix(*argv, "idx")) {
15873ff5a4dcSJakub Kicinski NEXT_ARG();
15883ff5a4dcSJakub Kicinski
15893ff5a4dcSJakub Kicinski idx = strtoul(*argv, &endptr, 0);
15903ff5a4dcSJakub Kicinski if (*endptr) {
15913ff5a4dcSJakub Kicinski p_err("can't parse %s as IDX", *argv);
15923ff5a4dcSJakub Kicinski goto err_free_reuse_maps;
15933ff5a4dcSJakub Kicinski }
15943ff5a4dcSJakub Kicinski name = NULL;
15953ff5a4dcSJakub Kicinski } else if (is_prefix(*argv, "name")) {
15963ff5a4dcSJakub Kicinski NEXT_ARG();
15973ff5a4dcSJakub Kicinski
15983ff5a4dcSJakub Kicinski name = *argv;
15993ff5a4dcSJakub Kicinski idx = -1;
16003ff5a4dcSJakub Kicinski } else {
16013ff5a4dcSJakub Kicinski p_err("expected 'idx' or 'name', got: '%s'?",
16023ff5a4dcSJakub Kicinski *argv);
16033ff5a4dcSJakub Kicinski goto err_free_reuse_maps;
16043ff5a4dcSJakub Kicinski }
16053ff5a4dcSJakub Kicinski NEXT_ARG();
16063ff5a4dcSJakub Kicinski
16073ff5a4dcSJakub Kicinski fd = map_parse_fd(&argc, &argv);
16083ff5a4dcSJakub Kicinski if (fd < 0)
16093ff5a4dcSJakub Kicinski goto err_free_reuse_maps;
16103ff5a4dcSJakub Kicinski
1611a19df713SMauricio Vásquez new_map_replace = libbpf_reallocarray(map_replace,
1612dde7011aSJakub Kicinski old_map_fds + 1,
16133ff5a4dcSJakub Kicinski sizeof(*map_replace));
1614dde7011aSJakub Kicinski if (!new_map_replace) {
16153ff5a4dcSJakub Kicinski p_err("mem alloc failed");
16163ff5a4dcSJakub Kicinski goto err_free_reuse_maps;
16173ff5a4dcSJakub Kicinski }
1618dde7011aSJakub Kicinski map_replace = new_map_replace;
1619dde7011aSJakub Kicinski
16203ff5a4dcSJakub Kicinski map_replace[old_map_fds].idx = idx;
16213ff5a4dcSJakub Kicinski map_replace[old_map_fds].name = name;
16223ff5a4dcSJakub Kicinski map_replace[old_map_fds].fd = fd;
16233ff5a4dcSJakub Kicinski old_map_fds++;
162449f2cba3SJakub Kicinski } else if (is_prefix(*argv, "dev")) {
1625f46392eeSLarysa Zaremba p_info("Warning: 'bpftool prog load [...] dev <ifname>' syntax is deprecated.\n"
1626f46392eeSLarysa Zaremba "Going further, please use 'offload_dev <ifname>' to offload program to device.\n"
1627f46392eeSLarysa Zaremba "For applications using XDP hints only, use 'xdpmeta_dev <ifname>'.");
1628f46392eeSLarysa Zaremba goto offload_dev;
1629f46392eeSLarysa Zaremba } else if (is_prefix(*argv, "offload_dev")) {
1630f46392eeSLarysa Zaremba offload_dev:
1631ba6dd679SJakub Kicinski NEXT_ARG();
1632ba6dd679SJakub Kicinski
1633f46392eeSLarysa Zaremba if (offload_ifindex) {
1634f46392eeSLarysa Zaremba p_err("offload_dev already specified");
1635f46392eeSLarysa Zaremba goto err_free_reuse_maps;
1636f46392eeSLarysa Zaremba } else if (xdpmeta_ifindex) {
1637f46392eeSLarysa Zaremba p_err("xdpmeta_dev and offload_dev are mutually exclusive");
16383ff5a4dcSJakub Kicinski goto err_free_reuse_maps;
1639ba6dd679SJakub Kicinski }
1640ba6dd679SJakub Kicinski if (!REQ_ARGS(1))
16413ff5a4dcSJakub Kicinski goto err_free_reuse_maps;
1642ba6dd679SJakub Kicinski
1643f46392eeSLarysa Zaremba offload_ifindex = if_nametoindex(*argv);
1644f46392eeSLarysa Zaremba if (!offload_ifindex) {
1645f46392eeSLarysa Zaremba p_err("unrecognized netdevice '%s': %s",
1646f46392eeSLarysa Zaremba *argv, strerror(errno));
1647f46392eeSLarysa Zaremba goto err_free_reuse_maps;
1648f46392eeSLarysa Zaremba }
1649f46392eeSLarysa Zaremba NEXT_ARG();
1650f46392eeSLarysa Zaremba } else if (is_prefix(*argv, "xdpmeta_dev")) {
1651f46392eeSLarysa Zaremba NEXT_ARG();
1652f46392eeSLarysa Zaremba
1653f46392eeSLarysa Zaremba if (xdpmeta_ifindex) {
1654f46392eeSLarysa Zaremba p_err("xdpmeta_dev already specified");
1655f46392eeSLarysa Zaremba goto err_free_reuse_maps;
1656f46392eeSLarysa Zaremba } else if (offload_ifindex) {
1657f46392eeSLarysa Zaremba p_err("xdpmeta_dev and offload_dev are mutually exclusive");
1658f46392eeSLarysa Zaremba goto err_free_reuse_maps;
1659f46392eeSLarysa Zaremba }
1660f46392eeSLarysa Zaremba if (!REQ_ARGS(1))
1661f46392eeSLarysa Zaremba goto err_free_reuse_maps;
1662f46392eeSLarysa Zaremba
1663f46392eeSLarysa Zaremba xdpmeta_ifindex = if_nametoindex(*argv);
1664f46392eeSLarysa Zaremba if (!xdpmeta_ifindex) {
1665ba6dd679SJakub Kicinski p_err("unrecognized netdevice '%s': %s",
1666ba6dd679SJakub Kicinski *argv, strerror(errno));
16673ff5a4dcSJakub Kicinski goto err_free_reuse_maps;
1668ba6dd679SJakub Kicinski }
1669ba6dd679SJakub Kicinski NEXT_ARG();
16703767a94bSStanislav Fomichev } else if (is_prefix(*argv, "pinmaps")) {
16713767a94bSStanislav Fomichev NEXT_ARG();
16723767a94bSStanislav Fomichev
16733767a94bSStanislav Fomichev if (!REQ_ARGS(1))
16743767a94bSStanislav Fomichev goto err_free_reuse_maps;
16753767a94bSStanislav Fomichev
16763767a94bSStanislav Fomichev pinmaps = GET_ARG();
167719526e70SWang Yufen } else if (is_prefix(*argv, "autoattach")) {
167819526e70SWang Yufen auto_attach = true;
167919526e70SWang Yufen NEXT_ARG();
1680ba6dd679SJakub Kicinski } else {
16813ff5a4dcSJakub Kicinski p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
1682ba6dd679SJakub Kicinski *argv);
16833ff5a4dcSJakub Kicinski goto err_free_reuse_maps;
1684ba6dd679SJakub Kicinski }
1685ba6dd679SJakub Kicinski }
1686ba6dd679SJakub Kicinski
16876b4384ffSQuentin Monnet set_max_rlimit();
16886b4384ffSQuentin Monnet
1689b59e4ce8SAndrii Nakryiko if (verifier_logs)
1690b59e4ce8SAndrii Nakryiko /* log_level1 + log_level2 + stats, but not stable UAPI */
1691b59e4ce8SAndrii Nakryiko open_opts.kernel_log_level = 1 + 2 + 4;
1692b59e4ce8SAndrii Nakryiko
169332e3e58eSAndrii Nakryiko obj = bpf_object__open_file(file, &open_opts);
1694d1313e01SSahid Orentino Ferdjaoui if (!obj) {
1695c8406848SJakub Kicinski p_err("failed to open object file");
16963ff5a4dcSJakub Kicinski goto err_free_reuse_maps;
169749a086c2SRoman Gushchin }
169849a086c2SRoman Gushchin
169977380998SStanislav Fomichev bpf_object__for_each_program(pos, obj) {
170032e3e58eSAndrii Nakryiko enum bpf_prog_type prog_type = common_prog_type;
1701c8406848SJakub Kicinski
170232e3e58eSAndrii Nakryiko if (prog_type == BPF_PROG_TYPE_UNSPEC) {
1703fd17e272SAndrii Nakryiko const char *sec_name = bpf_program__section_name(pos);
1704c8406848SJakub Kicinski
17056ae32b29SQuentin Monnet err = get_prog_type_by_name(sec_name, &prog_type,
1706c8406848SJakub Kicinski &expected_attach_type);
1707c76e4c22STaeung Song if (err < 0)
1708c8406848SJakub Kicinski goto err_close_obj;
1709c8406848SJakub Kicinski }
171077380998SStanislav Fomichev
1711f46392eeSLarysa Zaremba if (prog_type == BPF_PROG_TYPE_XDP && xdpmeta_ifindex) {
1712f46392eeSLarysa Zaremba bpf_program__set_flags(pos, BPF_F_XDP_DEV_BOUND_ONLY);
1713f46392eeSLarysa Zaremba bpf_program__set_ifindex(pos, xdpmeta_ifindex);
1714f46392eeSLarysa Zaremba } else {
1715f46392eeSLarysa Zaremba bpf_program__set_ifindex(pos, offload_ifindex);
1716f46392eeSLarysa Zaremba }
1717b24f0b04SWei Yongjun if (bpf_program__type(pos) != prog_type)
171877380998SStanislav Fomichev bpf_program__set_type(pos, prog_type);
171977380998SStanislav Fomichev bpf_program__set_expected_attach_type(pos, expected_attach_type);
172077380998SStanislav Fomichev }
1721c8406848SJakub Kicinski
17223ff5a4dcSJakub Kicinski qsort(map_replace, old_map_fds, sizeof(*map_replace),
17233ff5a4dcSJakub Kicinski map_replace_compar);
17243ff5a4dcSJakub Kicinski
17253ff5a4dcSJakub Kicinski /* After the sort maps by name will be first on the list, because they
17263ff5a4dcSJakub Kicinski * have idx == -1. Resolve them.
17273ff5a4dcSJakub Kicinski */
17283ff5a4dcSJakub Kicinski j = 0;
17293ff5a4dcSJakub Kicinski while (j < old_map_fds && map_replace[j].name) {
17303ff5a4dcSJakub Kicinski i = 0;
1731f74a53d9SJakub Kicinski bpf_object__for_each_map(map, obj) {
17323ff5a4dcSJakub Kicinski if (!strcmp(bpf_map__name(map), map_replace[j].name)) {
17333ff5a4dcSJakub Kicinski map_replace[j].idx = i;
17343ff5a4dcSJakub Kicinski break;
17353ff5a4dcSJakub Kicinski }
17363ff5a4dcSJakub Kicinski i++;
17373ff5a4dcSJakub Kicinski }
17383ff5a4dcSJakub Kicinski if (map_replace[j].idx == -1) {
17393ff5a4dcSJakub Kicinski p_err("unable to find map '%s'", map_replace[j].name);
17403ff5a4dcSJakub Kicinski goto err_close_obj;
17413ff5a4dcSJakub Kicinski }
17423ff5a4dcSJakub Kicinski j++;
17433ff5a4dcSJakub Kicinski }
17443ff5a4dcSJakub Kicinski /* Resort if any names were resolved */
17453ff5a4dcSJakub Kicinski if (j)
17463ff5a4dcSJakub Kicinski qsort(map_replace, old_map_fds, sizeof(*map_replace),
17473ff5a4dcSJakub Kicinski map_replace_compar);
17483ff5a4dcSJakub Kicinski
17493ff5a4dcSJakub Kicinski /* Set ifindex and name reuse */
17503ff5a4dcSJakub Kicinski j = 0;
17513ff5a4dcSJakub Kicinski idx = 0;
1752f74a53d9SJakub Kicinski bpf_object__for_each_map(map, obj) {
17539855c131SChristy Lee if (bpf_map__type(map) != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
1754f46392eeSLarysa Zaremba bpf_map__set_ifindex(map, offload_ifindex);
1755c8406848SJakub Kicinski
17563ff5a4dcSJakub Kicinski if (j < old_map_fds && idx == map_replace[j].idx) {
17573ff5a4dcSJakub Kicinski err = bpf_map__reuse_fd(map, map_replace[j++].fd);
17583ff5a4dcSJakub Kicinski if (err) {
17593ff5a4dcSJakub Kicinski p_err("unable to set up map reuse: %d", err);
17603ff5a4dcSJakub Kicinski goto err_close_obj;
17613ff5a4dcSJakub Kicinski }
17623ff5a4dcSJakub Kicinski
17633ff5a4dcSJakub Kicinski /* Next reuse wants to apply to the same map */
17643ff5a4dcSJakub Kicinski if (j < old_map_fds && map_replace[j].idx == idx) {
17653ff5a4dcSJakub Kicinski p_err("replacement for map idx %d specified more than once",
17663ff5a4dcSJakub Kicinski idx);
17673ff5a4dcSJakub Kicinski goto err_close_obj;
17683ff5a4dcSJakub Kicinski }
17693ff5a4dcSJakub Kicinski }
17703ff5a4dcSJakub Kicinski
17713ff5a4dcSJakub Kicinski idx++;
17723ff5a4dcSJakub Kicinski }
17733ff5a4dcSJakub Kicinski if (j < old_map_fds) {
17743ff5a4dcSJakub Kicinski p_err("map idx '%d' not used", map_replace[j].idx);
17753ff5a4dcSJakub Kicinski goto err_close_obj;
17763ff5a4dcSJakub Kicinski }
17773ff5a4dcSJakub Kicinski
1778b59e4ce8SAndrii Nakryiko err = bpf_object__load(obj);
1779c8406848SJakub Kicinski if (err) {
1780c8406848SJakub Kicinski p_err("failed to load object file");
1781c8406848SJakub Kicinski goto err_close_obj;
1782c8406848SJakub Kicinski }
1783c8406848SJakub Kicinski
1784f92aebf1SSahil Siddiq if (first_prog_only)
1785f92aebf1SSahil Siddiq err = mount_bpffs_for_file(pinfile);
1786f92aebf1SSahil Siddiq else
1787f92aebf1SSahil Siddiq err = create_and_mount_bpffs_dir(pinfile);
178877380998SStanislav Fomichev if (err)
1789bfee71fbSJakub Kicinski goto err_close_obj;
179049a086c2SRoman Gushchin
179177380998SStanislav Fomichev if (first_prog_only) {
17926f2b219bSHengqi Chen prog = bpf_object__next_program(obj, NULL);
179377380998SStanislav Fomichev if (!prog) {
179477380998SStanislav Fomichev p_err("object file doesn't contain any bpf program");
179577380998SStanislav Fomichev goto err_close_obj;
179677380998SStanislav Fomichev }
179777380998SStanislav Fomichev
179819526e70SWang Yufen if (auto_attach)
179919526e70SWang Yufen err = auto_attach_program(prog, pinfile);
180019526e70SWang Yufen else
180177380998SStanislav Fomichev err = bpf_obj_pin(bpf_program__fd(prog), pinfile);
180277380998SStanislav Fomichev if (err) {
180377380998SStanislav Fomichev p_err("failed to pin program %s",
1804fd17e272SAndrii Nakryiko bpf_program__section_name(prog));
180577380998SStanislav Fomichev goto err_close_obj;
180677380998SStanislav Fomichev }
180777380998SStanislav Fomichev } else {
180819526e70SWang Yufen if (auto_attach)
180919526e70SWang Yufen err = auto_attach_programs(obj, pinfile);
181019526e70SWang Yufen else
181177380998SStanislav Fomichev err = bpf_object__pin_programs(obj, pinfile);
181277380998SStanislav Fomichev if (err) {
181377380998SStanislav Fomichev p_err("failed to pin all programs");
181477380998SStanislav Fomichev goto err_close_obj;
181577380998SStanislav Fomichev }
181677380998SStanislav Fomichev }
181777380998SStanislav Fomichev
18183767a94bSStanislav Fomichev if (pinmaps) {
181977bf25daSTao Chen err = create_and_mount_bpffs_dir(pinmaps);
182077bf25daSTao Chen if (err)
182177bf25daSTao Chen goto err_unpin;
182277bf25daSTao Chen
18233767a94bSStanislav Fomichev err = bpf_object__pin_maps(obj, pinmaps);
18243767a94bSStanislav Fomichev if (err) {
18253767a94bSStanislav Fomichev p_err("failed to pin all maps");
18263767a94bSStanislav Fomichev goto err_unpin;
18273767a94bSStanislav Fomichev }
18283767a94bSStanislav Fomichev }
18293767a94bSStanislav Fomichev
183049a086c2SRoman Gushchin if (json_output)
183149a086c2SRoman Gushchin jsonw_null(json_wtr);
183249a086c2SRoman Gushchin
1833bfee71fbSJakub Kicinski bpf_object__close(obj);
18343ff5a4dcSJakub Kicinski for (i = 0; i < old_map_fds; i++)
18353ff5a4dcSJakub Kicinski close(map_replace[i].fd);
18363ff5a4dcSJakub Kicinski free(map_replace);
1837bfee71fbSJakub Kicinski
183849a086c2SRoman Gushchin return 0;
1839bfee71fbSJakub Kicinski
18403767a94bSStanislav Fomichev err_unpin:
18413767a94bSStanislav Fomichev if (first_prog_only)
18423767a94bSStanislav Fomichev unlink(pinfile);
18433767a94bSStanislav Fomichev else
18443767a94bSStanislav Fomichev bpf_object__unpin_programs(obj, pinfile);
1845bfee71fbSJakub Kicinski err_close_obj:
1846bfee71fbSJakub Kicinski bpf_object__close(obj);
18473ff5a4dcSJakub Kicinski err_free_reuse_maps:
18483ff5a4dcSJakub Kicinski for (i = 0; i < old_map_fds; i++)
18493ff5a4dcSJakub Kicinski close(map_replace[i].fd);
18503ff5a4dcSJakub Kicinski free(map_replace);
1851bfee71fbSJakub Kicinski return -1;
185249a086c2SRoman Gushchin }
185349a086c2SRoman Gushchin
count_open_fds(void)1854d510296dSAlexei Starovoitov static int count_open_fds(void)
1855d510296dSAlexei Starovoitov {
1856d510296dSAlexei Starovoitov DIR *dp = opendir("/proc/self/fd");
1857d510296dSAlexei Starovoitov struct dirent *de;
1858d510296dSAlexei Starovoitov int cnt = -3;
1859d510296dSAlexei Starovoitov
1860d510296dSAlexei Starovoitov if (!dp)
1861d510296dSAlexei Starovoitov return -1;
1862d510296dSAlexei Starovoitov
1863d510296dSAlexei Starovoitov while ((de = readdir(dp)))
1864d510296dSAlexei Starovoitov cnt++;
1865d510296dSAlexei Starovoitov
1866d510296dSAlexei Starovoitov closedir(dp);
1867d510296dSAlexei Starovoitov return cnt;
1868d510296dSAlexei Starovoitov }
1869d510296dSAlexei Starovoitov
try_loader(struct gen_loader_opts * gen)1870d510296dSAlexei Starovoitov static int try_loader(struct gen_loader_opts *gen)
1871d510296dSAlexei Starovoitov {
1872d510296dSAlexei Starovoitov struct bpf_load_and_run_opts opts = {};
1873d510296dSAlexei Starovoitov struct bpf_loader_ctx *ctx;
1874d510296dSAlexei Starovoitov int ctx_sz = sizeof(*ctx) + 64 * max(sizeof(struct bpf_map_desc),
1875d510296dSAlexei Starovoitov sizeof(struct bpf_prog_desc));
1876d510296dSAlexei Starovoitov int log_buf_sz = (1u << 24) - 1;
1877d510296dSAlexei Starovoitov int err, fds_before, fd_delta;
1878942df4dcSAlexei Starovoitov char *log_buf = NULL;
1879d510296dSAlexei Starovoitov
1880d510296dSAlexei Starovoitov ctx = alloca(ctx_sz);
1881d510296dSAlexei Starovoitov memset(ctx, 0, ctx_sz);
1882d510296dSAlexei Starovoitov ctx->sz = ctx_sz;
1883942df4dcSAlexei Starovoitov if (verifier_logs) {
1884942df4dcSAlexei Starovoitov ctx->log_level = 1 + 2 + 4;
1885d510296dSAlexei Starovoitov ctx->log_size = log_buf_sz;
1886d510296dSAlexei Starovoitov log_buf = malloc(log_buf_sz);
1887d510296dSAlexei Starovoitov if (!log_buf)
1888d510296dSAlexei Starovoitov return -ENOMEM;
1889d510296dSAlexei Starovoitov ctx->log_buf = (long) log_buf;
1890942df4dcSAlexei Starovoitov }
1891d510296dSAlexei Starovoitov opts.ctx = ctx;
1892d510296dSAlexei Starovoitov opts.data = gen->data;
1893d510296dSAlexei Starovoitov opts.data_sz = gen->data_sz;
1894d510296dSAlexei Starovoitov opts.insns = gen->insns;
1895d510296dSAlexei Starovoitov opts.insns_sz = gen->insns_sz;
1896d510296dSAlexei Starovoitov fds_before = count_open_fds();
1897d510296dSAlexei Starovoitov err = bpf_load_and_run(&opts);
1898d510296dSAlexei Starovoitov fd_delta = count_open_fds() - fds_before;
1899942df4dcSAlexei Starovoitov if (err < 0 || verifier_logs) {
1900d510296dSAlexei Starovoitov fprintf(stderr, "err %d\n%s\n%s", err, opts.errstr, log_buf);
1901942df4dcSAlexei Starovoitov if (fd_delta && err < 0)
1902d510296dSAlexei Starovoitov fprintf(stderr, "loader prog leaked %d FDs\n",
1903d510296dSAlexei Starovoitov fd_delta);
1904d510296dSAlexei Starovoitov }
1905d510296dSAlexei Starovoitov free(log_buf);
1906d510296dSAlexei Starovoitov return err;
1907d510296dSAlexei Starovoitov }
1908d510296dSAlexei Starovoitov
do_loader(int argc,char ** argv)1909d510296dSAlexei Starovoitov static int do_loader(int argc, char **argv)
1910d510296dSAlexei Starovoitov {
1911d510296dSAlexei Starovoitov DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts);
1912d510296dSAlexei Starovoitov DECLARE_LIBBPF_OPTS(gen_loader_opts, gen);
1913d510296dSAlexei Starovoitov struct bpf_object *obj;
1914d510296dSAlexei Starovoitov const char *file;
1915d510296dSAlexei Starovoitov int err = 0;
1916d510296dSAlexei Starovoitov
1917d510296dSAlexei Starovoitov if (!REQ_ARGS(1))
1918d510296dSAlexei Starovoitov return -1;
1919d510296dSAlexei Starovoitov file = GET_ARG();
1920d510296dSAlexei Starovoitov
1921b59e4ce8SAndrii Nakryiko if (verifier_logs)
1922b59e4ce8SAndrii Nakryiko /* log_level1 + log_level2 + stats, but not stable UAPI */
1923b59e4ce8SAndrii Nakryiko open_opts.kernel_log_level = 1 + 2 + 4;
1924b59e4ce8SAndrii Nakryiko
1925d510296dSAlexei Starovoitov obj = bpf_object__open_file(file, &open_opts);
1926d1313e01SSahid Orentino Ferdjaoui if (!obj) {
1927d510296dSAlexei Starovoitov p_err("failed to open object file");
1928d510296dSAlexei Starovoitov goto err_close_obj;
1929d510296dSAlexei Starovoitov }
1930d510296dSAlexei Starovoitov
1931d510296dSAlexei Starovoitov err = bpf_object__gen_loader(obj, &gen);
1932d510296dSAlexei Starovoitov if (err)
1933d510296dSAlexei Starovoitov goto err_close_obj;
1934d510296dSAlexei Starovoitov
1935b59e4ce8SAndrii Nakryiko err = bpf_object__load(obj);
1936d510296dSAlexei Starovoitov if (err) {
1937d510296dSAlexei Starovoitov p_err("failed to load object file");
1938d510296dSAlexei Starovoitov goto err_close_obj;
1939d510296dSAlexei Starovoitov }
1940d510296dSAlexei Starovoitov
1941d510296dSAlexei Starovoitov if (verifier_logs) {
1942d510296dSAlexei Starovoitov struct dump_data dd = {};
1943d510296dSAlexei Starovoitov
1944d510296dSAlexei Starovoitov kernel_syms_load(&dd);
1945d510296dSAlexei Starovoitov dump_xlated_plain(&dd, (void *)gen.insns, gen.insns_sz, false, false);
1946d510296dSAlexei Starovoitov kernel_syms_destroy(&dd);
1947d510296dSAlexei Starovoitov }
1948d510296dSAlexei Starovoitov err = try_loader(&gen);
1949d510296dSAlexei Starovoitov err_close_obj:
1950d510296dSAlexei Starovoitov bpf_object__close(obj);
1951d510296dSAlexei Starovoitov return err;
1952d510296dSAlexei Starovoitov }
1953d510296dSAlexei Starovoitov
do_load(int argc,char ** argv)195477380998SStanislav Fomichev static int do_load(int argc, char **argv)
195577380998SStanislav Fomichev {
1956d510296dSAlexei Starovoitov if (use_loader)
1957d510296dSAlexei Starovoitov return do_loader(argc, argv);
195877380998SStanislav Fomichev return load_with_options(argc, argv, true);
195977380998SStanislav Fomichev }
196077380998SStanislav Fomichev
do_loadall(int argc,char ** argv)196177380998SStanislav Fomichev static int do_loadall(int argc, char **argv)
196277380998SStanislav Fomichev {
196377380998SStanislav Fomichev return load_with_options(argc, argv, false);
196477380998SStanislav Fomichev }
196577380998SStanislav Fomichev
196647c09d6aSSong Liu #ifdef BPFTOOL_WITHOUT_SKELETONS
196747c09d6aSSong Liu
do_profile(int argc,char ** argv)196847c09d6aSSong Liu static int do_profile(int argc, char **argv)
196947c09d6aSSong Liu {
197014e5728fSSong Liu p_err("bpftool prog profile command is not supported. Please build bpftool with clang >= 10.0.0");
197147c09d6aSSong Liu return 0;
197247c09d6aSSong Liu }
197347c09d6aSSong Liu
197447c09d6aSSong Liu #else /* BPFTOOL_WITHOUT_SKELETONS */
197547c09d6aSSong Liu
197647c09d6aSSong Liu #include "profiler.skel.h"
197747c09d6aSSong Liu
197847c09d6aSSong Liu struct profile_metric {
197947c09d6aSSong Liu const char *name;
198047c09d6aSSong Liu struct bpf_perf_event_value val;
198147c09d6aSSong Liu struct perf_event_attr attr;
198247c09d6aSSong Liu bool selected;
198347c09d6aSSong Liu
198447c09d6aSSong Liu /* calculate ratios like instructions per cycle */
198547c09d6aSSong Liu const int ratio_metric; /* 0 for N/A, 1 for index 0 (cycles) */
198647c09d6aSSong Liu const char *ratio_desc;
198747c09d6aSSong Liu const float ratio_mul;
198847c09d6aSSong Liu } metrics[] = {
198947c09d6aSSong Liu {
199047c09d6aSSong Liu .name = "cycles",
199147c09d6aSSong Liu .attr = {
199247c09d6aSSong Liu .type = PERF_TYPE_HARDWARE,
199347c09d6aSSong Liu .config = PERF_COUNT_HW_CPU_CYCLES,
199447c09d6aSSong Liu .exclude_user = 1,
199547c09d6aSSong Liu },
199647c09d6aSSong Liu },
199747c09d6aSSong Liu {
199847c09d6aSSong Liu .name = "instructions",
199947c09d6aSSong Liu .attr = {
200047c09d6aSSong Liu .type = PERF_TYPE_HARDWARE,
200147c09d6aSSong Liu .config = PERF_COUNT_HW_INSTRUCTIONS,
200247c09d6aSSong Liu .exclude_user = 1,
200347c09d6aSSong Liu },
200447c09d6aSSong Liu .ratio_metric = 1,
200547c09d6aSSong Liu .ratio_desc = "insns per cycle",
200647c09d6aSSong Liu .ratio_mul = 1.0,
200747c09d6aSSong Liu },
200847c09d6aSSong Liu {
200947c09d6aSSong Liu .name = "l1d_loads",
201047c09d6aSSong Liu .attr = {
201147c09d6aSSong Liu .type = PERF_TYPE_HW_CACHE,
201247c09d6aSSong Liu .config =
201347c09d6aSSong Liu PERF_COUNT_HW_CACHE_L1D |
201447c09d6aSSong Liu (PERF_COUNT_HW_CACHE_OP_READ << 8) |
201547c09d6aSSong Liu (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16),
201647c09d6aSSong Liu .exclude_user = 1,
201747c09d6aSSong Liu },
201847c09d6aSSong Liu },
201947c09d6aSSong Liu {
202047c09d6aSSong Liu .name = "llc_misses",
202147c09d6aSSong Liu .attr = {
202247c09d6aSSong Liu .type = PERF_TYPE_HW_CACHE,
202347c09d6aSSong Liu .config =
202447c09d6aSSong Liu PERF_COUNT_HW_CACHE_LL |
202547c09d6aSSong Liu (PERF_COUNT_HW_CACHE_OP_READ << 8) |
202647c09d6aSSong Liu (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
202747c09d6aSSong Liu .exclude_user = 1
202847c09d6aSSong Liu },
202947c09d6aSSong Liu .ratio_metric = 2,
203047c09d6aSSong Liu .ratio_desc = "LLC misses per million insns",
203147c09d6aSSong Liu .ratio_mul = 1e6,
203247c09d6aSSong Liu },
2033450d060eSYonghong Song {
2034450d060eSYonghong Song .name = "itlb_misses",
2035450d060eSYonghong Song .attr = {
2036450d060eSYonghong Song .type = PERF_TYPE_HW_CACHE,
2037450d060eSYonghong Song .config =
2038450d060eSYonghong Song PERF_COUNT_HW_CACHE_ITLB |
2039450d060eSYonghong Song (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2040450d060eSYonghong Song (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
2041450d060eSYonghong Song .exclude_user = 1
2042450d060eSYonghong Song },
2043450d060eSYonghong Song .ratio_metric = 2,
2044450d060eSYonghong Song .ratio_desc = "itlb misses per million insns",
2045450d060eSYonghong Song .ratio_mul = 1e6,
2046450d060eSYonghong Song },
2047450d060eSYonghong Song {
2048450d060eSYonghong Song .name = "dtlb_misses",
2049450d060eSYonghong Song .attr = {
2050450d060eSYonghong Song .type = PERF_TYPE_HW_CACHE,
2051450d060eSYonghong Song .config =
2052450d060eSYonghong Song PERF_COUNT_HW_CACHE_DTLB |
2053450d060eSYonghong Song (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2054450d060eSYonghong Song (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
2055450d060eSYonghong Song .exclude_user = 1
2056450d060eSYonghong Song },
2057450d060eSYonghong Song .ratio_metric = 2,
2058450d060eSYonghong Song .ratio_desc = "dtlb misses per million insns",
2059450d060eSYonghong Song .ratio_mul = 1e6,
2060450d060eSYonghong Song },
206147c09d6aSSong Liu };
206247c09d6aSSong Liu
206347c09d6aSSong Liu static __u64 profile_total_count;
206447c09d6aSSong Liu
206547c09d6aSSong Liu #define MAX_NUM_PROFILE_METRICS 4
206647c09d6aSSong Liu
profile_parse_metrics(int argc,char ** argv)206747c09d6aSSong Liu static int profile_parse_metrics(int argc, char **argv)
206847c09d6aSSong Liu {
206947c09d6aSSong Liu unsigned int metric_cnt;
207047c09d6aSSong Liu int selected_cnt = 0;
207147c09d6aSSong Liu unsigned int i;
207247c09d6aSSong Liu
20735eff8c18SRongguang Wei metric_cnt = ARRAY_SIZE(metrics);
207447c09d6aSSong Liu
207547c09d6aSSong Liu while (argc > 0) {
207647c09d6aSSong Liu for (i = 0; i < metric_cnt; i++) {
207747c09d6aSSong Liu if (is_prefix(argv[0], metrics[i].name)) {
207847c09d6aSSong Liu if (!metrics[i].selected)
207947c09d6aSSong Liu selected_cnt++;
208047c09d6aSSong Liu metrics[i].selected = true;
208147c09d6aSSong Liu break;
208247c09d6aSSong Liu }
208347c09d6aSSong Liu }
208447c09d6aSSong Liu if (i == metric_cnt) {
208547c09d6aSSong Liu p_err("unknown metric %s", argv[0]);
208647c09d6aSSong Liu return -1;
208747c09d6aSSong Liu }
208847c09d6aSSong Liu NEXT_ARG();
208947c09d6aSSong Liu }
209047c09d6aSSong Liu if (selected_cnt > MAX_NUM_PROFILE_METRICS) {
209147c09d6aSSong Liu p_err("too many (%d) metrics, please specify no more than %d metrics at at time",
209247c09d6aSSong Liu selected_cnt, MAX_NUM_PROFILE_METRICS);
209347c09d6aSSong Liu return -1;
209447c09d6aSSong Liu }
209547c09d6aSSong Liu return selected_cnt;
209647c09d6aSSong Liu }
209747c09d6aSSong Liu
profile_read_values(struct profiler_bpf * obj)209847c09d6aSSong Liu static void profile_read_values(struct profiler_bpf *obj)
209947c09d6aSSong Liu {
210047c09d6aSSong Liu __u32 m, cpu, num_cpu = obj->rodata->num_cpu;
210147c09d6aSSong Liu int reading_map_fd, count_map_fd;
210247c09d6aSSong Liu __u64 counts[num_cpu];
210347c09d6aSSong Liu __u32 key = 0;
210447c09d6aSSong Liu int err;
210547c09d6aSSong Liu
210647c09d6aSSong Liu reading_map_fd = bpf_map__fd(obj->maps.accum_readings);
210747c09d6aSSong Liu count_map_fd = bpf_map__fd(obj->maps.counts);
210847c09d6aSSong Liu if (reading_map_fd < 0 || count_map_fd < 0) {
210947c09d6aSSong Liu p_err("failed to get fd for map");
211047c09d6aSSong Liu return;
211147c09d6aSSong Liu }
211247c09d6aSSong Liu
211347c09d6aSSong Liu err = bpf_map_lookup_elem(count_map_fd, &key, counts);
211447c09d6aSSong Liu if (err) {
211547c09d6aSSong Liu p_err("failed to read count_map: %s", strerror(errno));
211647c09d6aSSong Liu return;
211747c09d6aSSong Liu }
211847c09d6aSSong Liu
211947c09d6aSSong Liu profile_total_count = 0;
212047c09d6aSSong Liu for (cpu = 0; cpu < num_cpu; cpu++)
212147c09d6aSSong Liu profile_total_count += counts[cpu];
212247c09d6aSSong Liu
212347c09d6aSSong Liu for (m = 0; m < ARRAY_SIZE(metrics); m++) {
212447c09d6aSSong Liu struct bpf_perf_event_value values[num_cpu];
212547c09d6aSSong Liu
212647c09d6aSSong Liu if (!metrics[m].selected)
212747c09d6aSSong Liu continue;
212847c09d6aSSong Liu
212947c09d6aSSong Liu err = bpf_map_lookup_elem(reading_map_fd, &key, values);
213047c09d6aSSong Liu if (err) {
213147c09d6aSSong Liu p_err("failed to read reading_map: %s",
213247c09d6aSSong Liu strerror(errno));
213347c09d6aSSong Liu return;
213447c09d6aSSong Liu }
213547c09d6aSSong Liu for (cpu = 0; cpu < num_cpu; cpu++) {
213647c09d6aSSong Liu metrics[m].val.counter += values[cpu].counter;
213747c09d6aSSong Liu metrics[m].val.enabled += values[cpu].enabled;
213847c09d6aSSong Liu metrics[m].val.running += values[cpu].running;
213947c09d6aSSong Liu }
214047c09d6aSSong Liu key++;
214147c09d6aSSong Liu }
214247c09d6aSSong Liu }
214347c09d6aSSong Liu
profile_print_readings_json(void)214447c09d6aSSong Liu static void profile_print_readings_json(void)
214547c09d6aSSong Liu {
214647c09d6aSSong Liu __u32 m;
214747c09d6aSSong Liu
214847c09d6aSSong Liu jsonw_start_array(json_wtr);
214947c09d6aSSong Liu for (m = 0; m < ARRAY_SIZE(metrics); m++) {
215047c09d6aSSong Liu if (!metrics[m].selected)
215147c09d6aSSong Liu continue;
215247c09d6aSSong Liu jsonw_start_object(json_wtr);
215347c09d6aSSong Liu jsonw_string_field(json_wtr, "metric", metrics[m].name);
215447c09d6aSSong Liu jsonw_lluint_field(json_wtr, "run_cnt", profile_total_count);
215547c09d6aSSong Liu jsonw_lluint_field(json_wtr, "value", metrics[m].val.counter);
215647c09d6aSSong Liu jsonw_lluint_field(json_wtr, "enabled", metrics[m].val.enabled);
215747c09d6aSSong Liu jsonw_lluint_field(json_wtr, "running", metrics[m].val.running);
215847c09d6aSSong Liu
215947c09d6aSSong Liu jsonw_end_object(json_wtr);
216047c09d6aSSong Liu }
216147c09d6aSSong Liu jsonw_end_array(json_wtr);
216247c09d6aSSong Liu }
216347c09d6aSSong Liu
profile_print_readings_plain(void)216447c09d6aSSong Liu static void profile_print_readings_plain(void)
216547c09d6aSSong Liu {
216647c09d6aSSong Liu __u32 m;
216747c09d6aSSong Liu
216847c09d6aSSong Liu printf("\n%18llu %-20s\n", profile_total_count, "run_cnt");
216947c09d6aSSong Liu for (m = 0; m < ARRAY_SIZE(metrics); m++) {
217047c09d6aSSong Liu struct bpf_perf_event_value *val = &metrics[m].val;
217147c09d6aSSong Liu int r;
217247c09d6aSSong Liu
217347c09d6aSSong Liu if (!metrics[m].selected)
217447c09d6aSSong Liu continue;
217547c09d6aSSong Liu printf("%18llu %-20s", val->counter, metrics[m].name);
217647c09d6aSSong Liu
217747c09d6aSSong Liu r = metrics[m].ratio_metric - 1;
217847c09d6aSSong Liu if (r >= 0 && metrics[r].selected &&
217947c09d6aSSong Liu metrics[r].val.counter > 0) {
218047c09d6aSSong Liu printf("# %8.2f %-30s",
218147c09d6aSSong Liu val->counter * metrics[m].ratio_mul /
218247c09d6aSSong Liu metrics[r].val.counter,
218347c09d6aSSong Liu metrics[m].ratio_desc);
218447c09d6aSSong Liu } else {
218547c09d6aSSong Liu printf("%-41s", "");
218647c09d6aSSong Liu }
218747c09d6aSSong Liu
218847c09d6aSSong Liu if (val->enabled > val->running)
218947c09d6aSSong Liu printf("(%4.2f%%)",
219047c09d6aSSong Liu val->running * 100.0 / val->enabled);
219147c09d6aSSong Liu printf("\n");
219247c09d6aSSong Liu }
219347c09d6aSSong Liu }
219447c09d6aSSong Liu
profile_print_readings(void)219547c09d6aSSong Liu static void profile_print_readings(void)
219647c09d6aSSong Liu {
219747c09d6aSSong Liu if (json_output)
219847c09d6aSSong Liu profile_print_readings_json();
219947c09d6aSSong Liu else
220047c09d6aSSong Liu profile_print_readings_plain();
220147c09d6aSSong Liu }
220247c09d6aSSong Liu
profile_target_name(int tgt_fd)220347c09d6aSSong Liu static char *profile_target_name(int tgt_fd)
220447c09d6aSSong Liu {
2205c59765cfSDave Marchevsky struct bpf_func_info func_info;
2206c59765cfSDave Marchevsky struct bpf_prog_info info = {};
2207c59765cfSDave Marchevsky __u32 info_len = sizeof(info);
220847c09d6aSSong Liu const struct btf_type *t;
2209c59765cfSDave Marchevsky __u32 func_info_rec_size;
2210369e955bSQuentin Monnet struct btf *btf = NULL;
221147c09d6aSSong Liu char *name = NULL;
2212c59765cfSDave Marchevsky int err;
221347c09d6aSSong Liu
221438f0408eSIlya Leoshkevich err = bpf_prog_get_info_by_fd(tgt_fd, &info, &info_len);
2215c59765cfSDave Marchevsky if (err) {
221638f0408eSIlya Leoshkevich p_err("failed to get info for prog FD %d", tgt_fd);
2217c59765cfSDave Marchevsky goto out;
221847c09d6aSSong Liu }
221947c09d6aSSong Liu
2220c59765cfSDave Marchevsky if (info.btf_id == 0) {
222147c09d6aSSong Liu p_err("prog FD %d doesn't have valid btf", tgt_fd);
222247c09d6aSSong Liu goto out;
222347c09d6aSSong Liu }
222447c09d6aSSong Liu
2225c59765cfSDave Marchevsky func_info_rec_size = info.func_info_rec_size;
2226c59765cfSDave Marchevsky if (info.nr_func_info == 0) {
222738f0408eSIlya Leoshkevich p_err("found 0 func_info for prog FD %d", tgt_fd);
2228c59765cfSDave Marchevsky goto out;
2229c59765cfSDave Marchevsky }
2230c59765cfSDave Marchevsky
2231c59765cfSDave Marchevsky memset(&info, 0, sizeof(info));
2232c59765cfSDave Marchevsky info.nr_func_info = 1;
2233c59765cfSDave Marchevsky info.func_info_rec_size = func_info_rec_size;
2234c59765cfSDave Marchevsky info.func_info = ptr_to_u64(&func_info);
2235c59765cfSDave Marchevsky
223638f0408eSIlya Leoshkevich err = bpf_prog_get_info_by_fd(tgt_fd, &info, &info_len);
2237c59765cfSDave Marchevsky if (err) {
2238c59765cfSDave Marchevsky p_err("failed to get func_info for prog FD %d", tgt_fd);
2239c59765cfSDave Marchevsky goto out;
2240c59765cfSDave Marchevsky }
2241c59765cfSDave Marchevsky
2242c59765cfSDave Marchevsky btf = btf__load_from_kernel_by_id(info.btf_id);
2243d1313e01SSahid Orentino Ferdjaoui if (!btf) {
224486f4b7f2SQuentin Monnet p_err("failed to load btf for prog FD %d", tgt_fd);
224586f4b7f2SQuentin Monnet goto out;
224686f4b7f2SQuentin Monnet }
224786f4b7f2SQuentin Monnet
2248c59765cfSDave Marchevsky t = btf__type_by_id(btf, func_info.type_id);
224947c09d6aSSong Liu if (!t) {
225047c09d6aSSong Liu p_err("btf %d doesn't have type %d",
2251c59765cfSDave Marchevsky info.btf_id, func_info.type_id);
225247c09d6aSSong Liu goto out;
225347c09d6aSSong Liu }
225447c09d6aSSong Liu name = strdup(btf__name_by_offset(btf, t->name_off));
225547c09d6aSSong Liu out:
2256369e955bSQuentin Monnet btf__free(btf);
225747c09d6aSSong Liu return name;
225847c09d6aSSong Liu }
225947c09d6aSSong Liu
226047c09d6aSSong Liu static struct profiler_bpf *profile_obj;
226147c09d6aSSong Liu static int profile_tgt_fd = -1;
226247c09d6aSSong Liu static char *profile_tgt_name;
226347c09d6aSSong Liu static int *profile_perf_events;
226447c09d6aSSong Liu static int profile_perf_event_cnt;
226547c09d6aSSong Liu
profile_close_perf_events(struct profiler_bpf * obj)226647c09d6aSSong Liu static void profile_close_perf_events(struct profiler_bpf *obj)
226747c09d6aSSong Liu {
226847c09d6aSSong Liu int i;
226947c09d6aSSong Liu
227047c09d6aSSong Liu for (i = profile_perf_event_cnt - 1; i >= 0; i--)
227147c09d6aSSong Liu close(profile_perf_events[i]);
227247c09d6aSSong Liu
227347c09d6aSSong Liu free(profile_perf_events);
227447c09d6aSSong Liu profile_perf_event_cnt = 0;
227547c09d6aSSong Liu }
227647c09d6aSSong Liu
profile_open_perf_event(int mid,int cpu,int map_fd)2277377c16faSTonghao Zhang static int profile_open_perf_event(int mid, int cpu, int map_fd)
2278377c16faSTonghao Zhang {
2279377c16faSTonghao Zhang int pmu_fd;
2280377c16faSTonghao Zhang
2281377c16faSTonghao Zhang pmu_fd = syscall(__NR_perf_event_open, &metrics[mid].attr,
2282377c16faSTonghao Zhang -1 /*pid*/, cpu, -1 /*group_fd*/, 0);
2283377c16faSTonghao Zhang if (pmu_fd < 0) {
2284377c16faSTonghao Zhang if (errno == ENODEV) {
2285377c16faSTonghao Zhang p_info("cpu %d may be offline, skip %s profiling.",
2286377c16faSTonghao Zhang cpu, metrics[mid].name);
2287377c16faSTonghao Zhang profile_perf_event_cnt++;
2288377c16faSTonghao Zhang return 0;
2289377c16faSTonghao Zhang }
2290377c16faSTonghao Zhang return -1;
2291377c16faSTonghao Zhang }
2292377c16faSTonghao Zhang
2293377c16faSTonghao Zhang if (bpf_map_update_elem(map_fd,
2294377c16faSTonghao Zhang &profile_perf_event_cnt,
2295377c16faSTonghao Zhang &pmu_fd, BPF_ANY) ||
2296377c16faSTonghao Zhang ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0)) {
2297377c16faSTonghao Zhang close(pmu_fd);
2298377c16faSTonghao Zhang return -1;
2299377c16faSTonghao Zhang }
2300377c16faSTonghao Zhang
2301377c16faSTonghao Zhang profile_perf_events[profile_perf_event_cnt++] = pmu_fd;
2302377c16faSTonghao Zhang return 0;
2303377c16faSTonghao Zhang }
2304377c16faSTonghao Zhang
profile_open_perf_events(struct profiler_bpf * obj)230547c09d6aSSong Liu static int profile_open_perf_events(struct profiler_bpf *obj)
230647c09d6aSSong Liu {
230747c09d6aSSong Liu unsigned int cpu, m;
2308377c16faSTonghao Zhang int map_fd;
230947c09d6aSSong Liu
231047c09d6aSSong Liu profile_perf_events = calloc(
2311e9a89238STiezhu Yang obj->rodata->num_cpu * obj->rodata->num_metric, sizeof(int));
231247c09d6aSSong Liu if (!profile_perf_events) {
231347c09d6aSSong Liu p_err("failed to allocate memory for perf_event array: %s",
231447c09d6aSSong Liu strerror(errno));
231547c09d6aSSong Liu return -1;
231647c09d6aSSong Liu }
231747c09d6aSSong Liu map_fd = bpf_map__fd(obj->maps.events);
231847c09d6aSSong Liu if (map_fd < 0) {
231947c09d6aSSong Liu p_err("failed to get fd for events map");
232047c09d6aSSong Liu return -1;
232147c09d6aSSong Liu }
232247c09d6aSSong Liu
232347c09d6aSSong Liu for (m = 0; m < ARRAY_SIZE(metrics); m++) {
232447c09d6aSSong Liu if (!metrics[m].selected)
232547c09d6aSSong Liu continue;
232647c09d6aSSong Liu for (cpu = 0; cpu < obj->rodata->num_cpu; cpu++) {
2327377c16faSTonghao Zhang if (profile_open_perf_event(m, cpu, map_fd)) {
232847c09d6aSSong Liu p_err("failed to create event %s on cpu %d",
232947c09d6aSSong Liu metrics[m].name, cpu);
233047c09d6aSSong Liu return -1;
233147c09d6aSSong Liu }
233247c09d6aSSong Liu }
233347c09d6aSSong Liu }
233447c09d6aSSong Liu return 0;
233547c09d6aSSong Liu }
233647c09d6aSSong Liu
profile_print_and_cleanup(void)233747c09d6aSSong Liu static void profile_print_and_cleanup(void)
233847c09d6aSSong Liu {
233947c09d6aSSong Liu profile_close_perf_events(profile_obj);
234047c09d6aSSong Liu profile_read_values(profile_obj);
234147c09d6aSSong Liu profile_print_readings();
234247c09d6aSSong Liu profiler_bpf__destroy(profile_obj);
234347c09d6aSSong Liu
234447c09d6aSSong Liu close(profile_tgt_fd);
234547c09d6aSSong Liu free(profile_tgt_name);
234647c09d6aSSong Liu }
234747c09d6aSSong Liu
int_exit(int signo)234847c09d6aSSong Liu static void int_exit(int signo)
234947c09d6aSSong Liu {
235047c09d6aSSong Liu profile_print_and_cleanup();
235147c09d6aSSong Liu exit(0);
235247c09d6aSSong Liu }
235347c09d6aSSong Liu
do_profile(int argc,char ** argv)235447c09d6aSSong Liu static int do_profile(int argc, char **argv)
235547c09d6aSSong Liu {
235647c09d6aSSong Liu int num_metric, num_cpu, err = -1;
235747c09d6aSSong Liu struct bpf_program *prog;
235847c09d6aSSong Liu unsigned long duration;
235947c09d6aSSong Liu char *endptr;
236047c09d6aSSong Liu
236147c09d6aSSong Liu /* we at least need two args for the prog and one metric */
236247c09d6aSSong Liu if (!REQ_ARGS(3))
236347c09d6aSSong Liu return -EINVAL;
236447c09d6aSSong Liu
236547c09d6aSSong Liu /* parse target fd */
236647c09d6aSSong Liu profile_tgt_fd = prog_parse_fd(&argc, &argv);
236747c09d6aSSong Liu if (profile_tgt_fd < 0) {
236847c09d6aSSong Liu p_err("failed to parse fd");
236947c09d6aSSong Liu return -1;
237047c09d6aSSong Liu }
237147c09d6aSSong Liu
237247c09d6aSSong Liu /* parse profiling optional duration */
237347c09d6aSSong Liu if (argc > 2 && is_prefix(argv[0], "duration")) {
237447c09d6aSSong Liu NEXT_ARG();
237547c09d6aSSong Liu duration = strtoul(*argv, &endptr, 0);
237647c09d6aSSong Liu if (*endptr)
237747c09d6aSSong Liu usage();
237847c09d6aSSong Liu NEXT_ARG();
237947c09d6aSSong Liu } else {
238047c09d6aSSong Liu duration = UINT_MAX;
238147c09d6aSSong Liu }
238247c09d6aSSong Liu
238347c09d6aSSong Liu num_metric = profile_parse_metrics(argc, argv);
238447c09d6aSSong Liu if (num_metric <= 0)
238547c09d6aSSong Liu goto out;
238647c09d6aSSong Liu
238747c09d6aSSong Liu num_cpu = libbpf_num_possible_cpus();
238847c09d6aSSong Liu if (num_cpu <= 0) {
238947c09d6aSSong Liu p_err("failed to identify number of CPUs");
239047c09d6aSSong Liu goto out;
239147c09d6aSSong Liu }
239247c09d6aSSong Liu
239347c09d6aSSong Liu profile_obj = profiler_bpf__open();
239447c09d6aSSong Liu if (!profile_obj) {
239547c09d6aSSong Liu p_err("failed to open and/or load BPF object");
239647c09d6aSSong Liu goto out;
239747c09d6aSSong Liu }
239847c09d6aSSong Liu
239947c09d6aSSong Liu profile_obj->rodata->num_cpu = num_cpu;
240047c09d6aSSong Liu profile_obj->rodata->num_metric = num_metric;
240147c09d6aSSong Liu
240247c09d6aSSong Liu /* adjust map sizes */
240339748db1SAndrii Nakryiko bpf_map__set_max_entries(profile_obj->maps.events, num_metric * num_cpu);
240439748db1SAndrii Nakryiko bpf_map__set_max_entries(profile_obj->maps.fentry_readings, num_metric);
240539748db1SAndrii Nakryiko bpf_map__set_max_entries(profile_obj->maps.accum_readings, num_metric);
240639748db1SAndrii Nakryiko bpf_map__set_max_entries(profile_obj->maps.counts, 1);
240747c09d6aSSong Liu
240847c09d6aSSong Liu /* change target name */
240947c09d6aSSong Liu profile_tgt_name = profile_target_name(profile_tgt_fd);
241047c09d6aSSong Liu if (!profile_tgt_name)
241147c09d6aSSong Liu goto out;
241247c09d6aSSong Liu
241347c09d6aSSong Liu bpf_object__for_each_program(prog, profile_obj->obj) {
241447c09d6aSSong Liu err = bpf_program__set_attach_target(prog, profile_tgt_fd,
241547c09d6aSSong Liu profile_tgt_name);
241647c09d6aSSong Liu if (err) {
241747c09d6aSSong Liu p_err("failed to set attach target\n");
241847c09d6aSSong Liu goto out;
241947c09d6aSSong Liu }
242047c09d6aSSong Liu }
242147c09d6aSSong Liu
24226b4384ffSQuentin Monnet set_max_rlimit();
242347c09d6aSSong Liu err = profiler_bpf__load(profile_obj);
242447c09d6aSSong Liu if (err) {
242547c09d6aSSong Liu p_err("failed to load profile_obj");
242647c09d6aSSong Liu goto out;
242747c09d6aSSong Liu }
242847c09d6aSSong Liu
242947c09d6aSSong Liu err = profile_open_perf_events(profile_obj);
243047c09d6aSSong Liu if (err)
243147c09d6aSSong Liu goto out;
243247c09d6aSSong Liu
243347c09d6aSSong Liu err = profiler_bpf__attach(profile_obj);
243447c09d6aSSong Liu if (err) {
243547c09d6aSSong Liu p_err("failed to attach profile_obj");
243647c09d6aSSong Liu goto out;
243747c09d6aSSong Liu }
243847c09d6aSSong Liu signal(SIGINT, int_exit);
243947c09d6aSSong Liu
244047c09d6aSSong Liu sleep(duration);
244147c09d6aSSong Liu profile_print_and_cleanup();
244247c09d6aSSong Liu return 0;
244347c09d6aSSong Liu
244447c09d6aSSong Liu out:
244547c09d6aSSong Liu profile_close_perf_events(profile_obj);
244647c09d6aSSong Liu if (profile_obj)
244747c09d6aSSong Liu profiler_bpf__destroy(profile_obj);
244847c09d6aSSong Liu close(profile_tgt_fd);
244947c09d6aSSong Liu free(profile_tgt_name);
245047c09d6aSSong Liu return err;
245147c09d6aSSong Liu }
245247c09d6aSSong Liu
245347c09d6aSSong Liu #endif /* BPFTOOL_WITHOUT_SKELETONS */
245447c09d6aSSong Liu
do_help(int argc,char ** argv)245571bb428fSJakub Kicinski static int do_help(int argc, char **argv)
245671bb428fSJakub Kicinski {
2457004b45c0SQuentin Monnet if (json_output) {
2458004b45c0SQuentin Monnet jsonw_null(json_wtr);
2459004b45c0SQuentin Monnet return 0;
2460004b45c0SQuentin Monnet }
2461004b45c0SQuentin Monnet
246271bb428fSJakub Kicinski fprintf(stderr,
246390040351SQuentin Monnet "Usage: %1$s %2$s { show | list } [PROG]\n"
24649b79f027SQuentin Monnet " %1$s %2$s dump xlated PROG [{ file FILE | [opcodes] [linum] [visual] }]\n"
24659b79f027SQuentin Monnet " %1$s %2$s dump jited PROG [{ file FILE | [opcodes] [linum] }]\n"
246690040351SQuentin Monnet " %1$s %2$s pin PROG FILE\n"
246790040351SQuentin Monnet " %1$s %2$s { load | loadall } OBJ PATH \\\n"
2468f46392eeSLarysa Zaremba " [type TYPE] [{ offload_dev | xdpmeta_dev } NAME] \\\n"
24693767a94bSStanislav Fomichev " [map { idx IDX | name NAME } MAP]\\\n"
24703767a94bSStanislav Fomichev " [pinmaps MAP_DIR]\n"
247119526e70SWang Yufen " [autoattach]\n"
247290040351SQuentin Monnet " %1$s %2$s attach PROG ATTACH_TYPE [MAP]\n"
247390040351SQuentin Monnet " %1$s %2$s detach PROG ATTACH_TYPE [MAP]\n"
247490040351SQuentin Monnet " %1$s %2$s run PROG \\\n"
2475ba95c745SQuentin Monnet " data_in FILE \\\n"
2476ba95c745SQuentin Monnet " [data_out FILE [data_size_out L]] \\\n"
2477ba95c745SQuentin Monnet " [ctx_in FILE [ctx_out FILE [ctx_size_out M]]] \\\n"
2478ba95c745SQuentin Monnet " [repeat N]\n"
247990040351SQuentin Monnet " %1$s %2$s profile PROG [duration DURATION] METRICs\n"
248090040351SQuentin Monnet " %1$s %2$s tracelog\n"
248190040351SQuentin Monnet " %1$s %2$s help\n"
248271bb428fSJakub Kicinski "\n"
24833ff5a4dcSJakub Kicinski " " HELP_SPEC_MAP "\n"
248471bb428fSJakub Kicinski " " HELP_SPEC_PROGRAM "\n"
248549f2cba3SJakub Kicinski " TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
248649f2cba3SJakub Kicinski " tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
248749f2cba3SJakub Kicinski " cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
248849f2cba3SJakub Kicinski " lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
2489f25377eeSAndrey Ignatov " sk_reuseport | flow_dissector | cgroup/sysctl |\n"
249049f2cba3SJakub Kicinski " cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
249149f2cba3SJakub Kicinski " cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
249205ee19c1SDaniel Borkmann " cgroup/getpeername4 | cgroup/getpeername6 |\n"
249305ee19c1SDaniel Borkmann " cgroup/getsockname4 | cgroup/getsockname6 | cgroup/sendmsg4 |\n"
249405ee19c1SDaniel Borkmann " cgroup/sendmsg6 | cgroup/recvmsg4 | cgroup/recvmsg6 |\n"
2495a8deba85SLiu Jian " cgroup/getsockopt | cgroup/setsockopt | cgroup/sock_release |\n"
249693a3545dSJakub Sitnicki " struct_ops | fentry | fexit | freplace | sk_lookup }\n"
24971ba5ad36SDaniel Müller " ATTACH_TYPE := { sk_msg_verdict | sk_skb_verdict | sk_skb_stream_verdict |\n"
24981ba5ad36SDaniel Müller " sk_skb_stream_parser | flow_dissector }\n"
2499450d060eSYonghong Song " METRIC := { cycles | instructions | l1d_loads | llc_misses | itlb_misses | dtlb_misses }\n"
2500c07ba629SQuentin Monnet " " HELP_SPEC_OPTIONS " |\n"
25018cc8c635SQuentin Monnet " {-f|--bpffs} | {-m|--mapcompat} | {-n|--nomount} |\n"
25028cc8c635SQuentin Monnet " {-L|--use-loader} }\n"
250371bb428fSJakub Kicinski "",
250490040351SQuentin Monnet bin_name, argv[-2]);
250571bb428fSJakub Kicinski
250671bb428fSJakub Kicinski return 0;
250771bb428fSJakub Kicinski }
250871bb428fSJakub Kicinski
250971bb428fSJakub Kicinski static const struct cmd cmds[] = {
251071bb428fSJakub Kicinski { "show", do_show },
25116ebe6dbdSJakub Kicinski { "list", do_show },
25129f606179SQuentin Monnet { "help", do_help },
251371bb428fSJakub Kicinski { "dump", do_dump },
251471bb428fSJakub Kicinski { "pin", do_pin },
251549a086c2SRoman Gushchin { "load", do_load },
251677380998SStanislav Fomichev { "loadall", do_loadall },
2517b7d3826cSJohn Fastabend { "attach", do_attach },
2518b7d3826cSJohn Fastabend { "detach", do_detach },
251930da46b5SQuentin Monnet { "tracelog", do_tracelog },
2520ba95c745SQuentin Monnet { "run", do_run },
252147c09d6aSSong Liu { "profile", do_profile },
252271bb428fSJakub Kicinski { 0 }
252371bb428fSJakub Kicinski };
252471bb428fSJakub Kicinski
do_prog(int argc,char ** argv)252571bb428fSJakub Kicinski int do_prog(int argc, char **argv)
252671bb428fSJakub Kicinski {
252771bb428fSJakub Kicinski return cmd_select(cmds, argc, argv, do_help);
252871bb428fSJakub Kicinski }
2529