171bb428fSJakub Kicinski /* 271e07ddcSJakub Kicinski * Copyright (C) 2017-2018 Netronome Systems, Inc. 371bb428fSJakub Kicinski * 471bb428fSJakub Kicinski * This software is dual licensed under the GNU General License Version 2, 571bb428fSJakub Kicinski * June 1991 as shown in the file COPYING in the top-level directory of this 671bb428fSJakub Kicinski * source tree or the BSD 2-Clause License provided below. You have the 771bb428fSJakub Kicinski * option to license this software under the complete terms of either license. 871bb428fSJakub Kicinski * 971bb428fSJakub Kicinski * The BSD 2-Clause License: 1071bb428fSJakub Kicinski * 1171bb428fSJakub Kicinski * Redistribution and use in source and binary forms, with or 1271bb428fSJakub Kicinski * without modification, are permitted provided that the following 1371bb428fSJakub Kicinski * conditions are met: 1471bb428fSJakub Kicinski * 1571bb428fSJakub Kicinski * 1. Redistributions of source code must retain the above 1671bb428fSJakub Kicinski * copyright notice, this list of conditions and the following 1771bb428fSJakub Kicinski * disclaimer. 1871bb428fSJakub Kicinski * 1971bb428fSJakub Kicinski * 2. Redistributions in binary form must reproduce the above 2071bb428fSJakub Kicinski * copyright notice, this list of conditions and the following 2171bb428fSJakub Kicinski * disclaimer in the documentation and/or other materials 2271bb428fSJakub Kicinski * provided with the distribution. 2371bb428fSJakub Kicinski * 2471bb428fSJakub Kicinski * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 2571bb428fSJakub Kicinski * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2671bb428fSJakub Kicinski * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 2771bb428fSJakub Kicinski * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 2871bb428fSJakub Kicinski * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 2971bb428fSJakub Kicinski * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 3071bb428fSJakub Kicinski * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 3171bb428fSJakub Kicinski * SOFTWARE. 3271bb428fSJakub Kicinski */ 3371bb428fSJakub Kicinski 343ff5a4dcSJakub Kicinski #define _GNU_SOURCE 3571bb428fSJakub Kicinski #include <errno.h> 3671bb428fSJakub Kicinski #include <fcntl.h> 37c9c35995SJakub Kicinski #include <stdarg.h> 3871bb428fSJakub Kicinski #include <stdio.h> 3971bb428fSJakub Kicinski #include <stdlib.h> 4071bb428fSJakub Kicinski #include <string.h> 4171bb428fSJakub Kicinski #include <time.h> 4271bb428fSJakub Kicinski #include <unistd.h> 43ba6dd679SJakub Kicinski #include <net/if.h> 4471bb428fSJakub Kicinski #include <sys/types.h> 4571bb428fSJakub Kicinski #include <sys/stat.h> 4671bb428fSJakub Kicinski 47c8406848SJakub Kicinski #include <linux/err.h> 48c8406848SJakub Kicinski 4971bb428fSJakub Kicinski #include <bpf.h> 5049a086c2SRoman Gushchin #include <libbpf.h> 5171bb428fSJakub Kicinski 52b6c1cedbSJiong Wang #include "cfg.h" 5371bb428fSJakub Kicinski #include "main.h" 5473bb5b4fSJiong Wang #include "xlated_dumper.h" 5571bb428fSJakub Kicinski 5671bb428fSJakub Kicinski static const char * const prog_type_name[] = { 5771bb428fSJakub Kicinski [BPF_PROG_TYPE_UNSPEC] = "unspec", 5871bb428fSJakub Kicinski [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter", 5971bb428fSJakub Kicinski [BPF_PROG_TYPE_KPROBE] = "kprobe", 6071bb428fSJakub Kicinski [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls", 6171bb428fSJakub Kicinski [BPF_PROG_TYPE_SCHED_ACT] = "sched_act", 6271bb428fSJakub Kicinski [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint", 6371bb428fSJakub Kicinski [BPF_PROG_TYPE_XDP] = "xdp", 6471bb428fSJakub Kicinski [BPF_PROG_TYPE_PERF_EVENT] = "perf_event", 6571bb428fSJakub Kicinski [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb", 6671bb428fSJakub Kicinski [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock", 6771bb428fSJakub Kicinski [BPF_PROG_TYPE_LWT_IN] = "lwt_in", 6871bb428fSJakub Kicinski [BPF_PROG_TYPE_LWT_OUT] = "lwt_out", 6971bb428fSJakub Kicinski [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit", 7071bb428fSJakub Kicinski [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops", 7171bb428fSJakub Kicinski [BPF_PROG_TYPE_SK_SKB] = "sk_skb", 7245e5e121SRoman Gushchin [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device", 73393de512SAndrey Ignatov [BPF_PROG_TYPE_SK_MSG] = "sk_msg", 74393de512SAndrey Ignatov [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint", 75393de512SAndrey Ignatov [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr", 766bdd533cSSean Young [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2", 77c22fbae7SPetar Penkov [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector", 7871bb428fSJakub Kicinski }; 7971bb428fSJakub Kicinski 80b7d3826cSJohn Fastabend static const char * const attach_type_strings[] = { 81b7d3826cSJohn Fastabend [BPF_SK_SKB_STREAM_PARSER] = "stream_parser", 82b7d3826cSJohn Fastabend [BPF_SK_SKB_STREAM_VERDICT] = "stream_verdict", 83b7d3826cSJohn Fastabend [BPF_SK_MSG_VERDICT] = "msg_verdict", 84b7d3826cSJohn Fastabend [__MAX_BPF_ATTACH_TYPE] = NULL, 85b7d3826cSJohn Fastabend }; 86b7d3826cSJohn Fastabend 87b7d3826cSJohn Fastabend enum bpf_attach_type parse_attach_type(const char *str) 88b7d3826cSJohn Fastabend { 89b7d3826cSJohn Fastabend enum bpf_attach_type type; 90b7d3826cSJohn Fastabend 91b7d3826cSJohn Fastabend for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) { 92b7d3826cSJohn Fastabend if (attach_type_strings[type] && 93b7d3826cSJohn Fastabend is_prefix(str, attach_type_strings[type])) 94b7d3826cSJohn Fastabend return type; 95b7d3826cSJohn Fastabend } 96b7d3826cSJohn Fastabend 97b7d3826cSJohn Fastabend return __MAX_BPF_ATTACH_TYPE; 98b7d3826cSJohn Fastabend } 99b7d3826cSJohn Fastabend 10071bb428fSJakub Kicinski static void print_boot_time(__u64 nsecs, char *buf, unsigned int size) 10171bb428fSJakub Kicinski { 10271bb428fSJakub Kicinski struct timespec real_time_ts, boot_time_ts; 10371bb428fSJakub Kicinski time_t wallclock_secs; 10471bb428fSJakub Kicinski struct tm load_tm; 10571bb428fSJakub Kicinski 10671bb428fSJakub Kicinski buf[--size] = '\0'; 10771bb428fSJakub Kicinski 10871bb428fSJakub Kicinski if (clock_gettime(CLOCK_REALTIME, &real_time_ts) || 10971bb428fSJakub Kicinski clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) { 11071bb428fSJakub Kicinski perror("Can't read clocks"); 11171bb428fSJakub Kicinski snprintf(buf, size, "%llu", nsecs / 1000000000); 11271bb428fSJakub Kicinski return; 11371bb428fSJakub Kicinski } 11471bb428fSJakub Kicinski 11571bb428fSJakub Kicinski wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) + 11607480cbcSJakub Kicinski (real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) / 11707480cbcSJakub Kicinski 1000000000; 11807480cbcSJakub Kicinski 11971bb428fSJakub Kicinski 12071bb428fSJakub Kicinski if (!localtime_r(&wallclock_secs, &load_tm)) { 12171bb428fSJakub Kicinski snprintf(buf, size, "%llu", nsecs / 1000000000); 12271bb428fSJakub Kicinski return; 12371bb428fSJakub Kicinski } 12471bb428fSJakub Kicinski 125a3fe1f6fSQuentin Monnet if (json_output) 126a3fe1f6fSQuentin Monnet strftime(buf, size, "%s", &load_tm); 127a3fe1f6fSQuentin Monnet else 128a3fe1f6fSQuentin Monnet strftime(buf, size, "%FT%T%z", &load_tm); 12971bb428fSJakub Kicinski } 13071bb428fSJakub Kicinski 13171bb428fSJakub Kicinski static int prog_fd_by_tag(unsigned char *tag) 13271bb428fSJakub Kicinski { 13371bb428fSJakub Kicinski struct bpf_prog_info info = {}; 13471bb428fSJakub Kicinski __u32 len = sizeof(info); 13571bb428fSJakub Kicinski unsigned int id = 0; 13671bb428fSJakub Kicinski int err; 13771bb428fSJakub Kicinski int fd; 13871bb428fSJakub Kicinski 13971bb428fSJakub Kicinski while (true) { 14071bb428fSJakub Kicinski err = bpf_prog_get_next_id(id, &id); 14171bb428fSJakub Kicinski if (err) { 1429a5ab8bfSQuentin Monnet p_err("%s", strerror(errno)); 14371bb428fSJakub Kicinski return -1; 14471bb428fSJakub Kicinski } 14571bb428fSJakub Kicinski 14671bb428fSJakub Kicinski fd = bpf_prog_get_fd_by_id(id); 14771bb428fSJakub Kicinski if (fd < 0) { 1489a5ab8bfSQuentin Monnet p_err("can't get prog by id (%u): %s", 14971bb428fSJakub Kicinski id, strerror(errno)); 15071bb428fSJakub Kicinski return -1; 15171bb428fSJakub Kicinski } 15271bb428fSJakub Kicinski 15371bb428fSJakub Kicinski err = bpf_obj_get_info_by_fd(fd, &info, &len); 15471bb428fSJakub Kicinski if (err) { 1559a5ab8bfSQuentin Monnet p_err("can't get prog info (%u): %s", 15671bb428fSJakub Kicinski id, strerror(errno)); 15771bb428fSJakub Kicinski close(fd); 15871bb428fSJakub Kicinski return -1; 15971bb428fSJakub Kicinski } 16071bb428fSJakub Kicinski 16171bb428fSJakub Kicinski if (!memcmp(tag, info.tag, BPF_TAG_SIZE)) 16271bb428fSJakub Kicinski return fd; 16371bb428fSJakub Kicinski 16471bb428fSJakub Kicinski close(fd); 16571bb428fSJakub Kicinski } 16671bb428fSJakub Kicinski } 16771bb428fSJakub Kicinski 16871bb428fSJakub Kicinski int prog_parse_fd(int *argc, char ***argv) 16971bb428fSJakub Kicinski { 17071bb428fSJakub Kicinski int fd; 17171bb428fSJakub Kicinski 17271bb428fSJakub Kicinski if (is_prefix(**argv, "id")) { 17371bb428fSJakub Kicinski unsigned int id; 17471bb428fSJakub Kicinski char *endptr; 17571bb428fSJakub Kicinski 17671bb428fSJakub Kicinski NEXT_ARGP(); 17771bb428fSJakub Kicinski 17871bb428fSJakub Kicinski id = strtoul(**argv, &endptr, 0); 17971bb428fSJakub Kicinski if (*endptr) { 1809a5ab8bfSQuentin Monnet p_err("can't parse %s as ID", **argv); 18171bb428fSJakub Kicinski return -1; 18271bb428fSJakub Kicinski } 18371bb428fSJakub Kicinski NEXT_ARGP(); 18471bb428fSJakub Kicinski 18571bb428fSJakub Kicinski fd = bpf_prog_get_fd_by_id(id); 18671bb428fSJakub Kicinski if (fd < 0) 1879a5ab8bfSQuentin Monnet p_err("get by id (%u): %s", id, strerror(errno)); 18871bb428fSJakub Kicinski return fd; 18971bb428fSJakub Kicinski } else if (is_prefix(**argv, "tag")) { 19071bb428fSJakub Kicinski unsigned char tag[BPF_TAG_SIZE]; 19171bb428fSJakub Kicinski 19271bb428fSJakub Kicinski NEXT_ARGP(); 19371bb428fSJakub Kicinski 19471bb428fSJakub Kicinski if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2, 19571bb428fSJakub Kicinski tag + 3, tag + 4, tag + 5, tag + 6, tag + 7) 19671bb428fSJakub Kicinski != BPF_TAG_SIZE) { 1979a5ab8bfSQuentin Monnet p_err("can't parse tag"); 19871bb428fSJakub Kicinski return -1; 19971bb428fSJakub Kicinski } 20071bb428fSJakub Kicinski NEXT_ARGP(); 20171bb428fSJakub Kicinski 20271bb428fSJakub Kicinski return prog_fd_by_tag(tag); 20371bb428fSJakub Kicinski } else if (is_prefix(**argv, "pinned")) { 20471bb428fSJakub Kicinski char *path; 20571bb428fSJakub Kicinski 20671bb428fSJakub Kicinski NEXT_ARGP(); 20771bb428fSJakub Kicinski 20871bb428fSJakub Kicinski path = **argv; 20971bb428fSJakub Kicinski NEXT_ARGP(); 21071bb428fSJakub Kicinski 21171bb428fSJakub Kicinski return open_obj_pinned_any(path, BPF_OBJ_PROG); 21271bb428fSJakub Kicinski } 21371bb428fSJakub Kicinski 2149a5ab8bfSQuentin Monnet p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv); 21571bb428fSJakub Kicinski return -1; 21671bb428fSJakub Kicinski } 21771bb428fSJakub Kicinski 21871bb428fSJakub Kicinski static void show_prog_maps(int fd, u32 num_maps) 21971bb428fSJakub Kicinski { 22071bb428fSJakub Kicinski struct bpf_prog_info info = {}; 22171bb428fSJakub Kicinski __u32 len = sizeof(info); 22271bb428fSJakub Kicinski __u32 map_ids[num_maps]; 22371bb428fSJakub Kicinski unsigned int i; 22471bb428fSJakub Kicinski int err; 22571bb428fSJakub Kicinski 22671bb428fSJakub Kicinski info.nr_map_ids = num_maps; 22771bb428fSJakub Kicinski info.map_ids = ptr_to_u64(map_ids); 22871bb428fSJakub Kicinski 22971bb428fSJakub Kicinski err = bpf_obj_get_info_by_fd(fd, &info, &len); 23071bb428fSJakub Kicinski if (err || !info.nr_map_ids) 23171bb428fSJakub Kicinski return; 23271bb428fSJakub Kicinski 233743cc665SQuentin Monnet if (json_output) { 234743cc665SQuentin Monnet jsonw_name(json_wtr, "map_ids"); 235743cc665SQuentin Monnet jsonw_start_array(json_wtr); 236743cc665SQuentin Monnet for (i = 0; i < info.nr_map_ids; i++) 237743cc665SQuentin Monnet jsonw_uint(json_wtr, map_ids[i]); 238743cc665SQuentin Monnet jsonw_end_array(json_wtr); 239743cc665SQuentin Monnet } else { 24071bb428fSJakub Kicinski printf(" map_ids "); 24171bb428fSJakub Kicinski for (i = 0; i < info.nr_map_ids; i++) 24271bb428fSJakub Kicinski printf("%u%s", map_ids[i], 24371bb428fSJakub Kicinski i == info.nr_map_ids - 1 ? "" : ","); 24471bb428fSJakub Kicinski } 24571bb428fSJakub Kicinski } 24671bb428fSJakub Kicinski 247743cc665SQuentin Monnet static void print_prog_json(struct bpf_prog_info *info, int fd) 248743cc665SQuentin Monnet { 249743cc665SQuentin Monnet char *memlock; 250743cc665SQuentin Monnet 251743cc665SQuentin Monnet jsonw_start_object(json_wtr); 252743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "id", info->id); 253743cc665SQuentin Monnet if (info->type < ARRAY_SIZE(prog_type_name)) 254743cc665SQuentin Monnet jsonw_string_field(json_wtr, "type", 255743cc665SQuentin Monnet prog_type_name[info->type]); 25671bb428fSJakub Kicinski else 257743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "type", info->type); 25871bb428fSJakub Kicinski 259743cc665SQuentin Monnet if (*info->name) 260743cc665SQuentin Monnet jsonw_string_field(json_wtr, "name", info->name); 26171bb428fSJakub Kicinski 262743cc665SQuentin Monnet jsonw_name(json_wtr, "tag"); 263743cc665SQuentin Monnet jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"", 264743cc665SQuentin Monnet info->tag[0], info->tag[1], info->tag[2], info->tag[3], 265743cc665SQuentin Monnet info->tag[4], info->tag[5], info->tag[6], info->tag[7]); 26671bb428fSJakub Kicinski 2679b984a20SJiri Olsa jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible); 2689b984a20SJiri Olsa 26952262210SJakub Kicinski print_dev_json(info->ifindex, info->netns_dev, info->netns_ino); 27052262210SJakub Kicinski 271743cc665SQuentin Monnet if (info->load_time) { 27271bb428fSJakub Kicinski char buf[32]; 27371bb428fSJakub Kicinski 274743cc665SQuentin Monnet print_boot_time(info->load_time, buf, sizeof(buf)); 27571bb428fSJakub Kicinski 27671bb428fSJakub Kicinski /* Piggy back on load_time, since 0 uid is a valid one */ 277a3fe1f6fSQuentin Monnet jsonw_name(json_wtr, "loaded_at"); 278a3fe1f6fSQuentin Monnet jsonw_printf(json_wtr, "%s", buf); 279743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "uid", info->created_by_uid); 28071bb428fSJakub Kicinski } 28171bb428fSJakub Kicinski 282743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len); 28371bb428fSJakub Kicinski 284743cc665SQuentin Monnet if (info->jited_prog_len) { 285743cc665SQuentin Monnet jsonw_bool_field(json_wtr, "jited", true); 286743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len); 287743cc665SQuentin Monnet } else { 288743cc665SQuentin Monnet jsonw_bool_field(json_wtr, "jited", false); 289743cc665SQuentin Monnet } 290743cc665SQuentin Monnet 291743cc665SQuentin Monnet memlock = get_fdinfo(fd, "memlock"); 292743cc665SQuentin Monnet if (memlock) 293743cc665SQuentin Monnet jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock)); 294743cc665SQuentin Monnet free(memlock); 295743cc665SQuentin Monnet 296743cc665SQuentin Monnet if (info->nr_map_ids) 297743cc665SQuentin Monnet show_prog_maps(fd, info->nr_map_ids); 298743cc665SQuentin Monnet 2994990f1f4SPrashant Bhole if (!hash_empty(prog_table.table)) { 3004990f1f4SPrashant Bhole struct pinned_obj *obj; 3014990f1f4SPrashant Bhole 3024990f1f4SPrashant Bhole jsonw_name(json_wtr, "pinned"); 3034990f1f4SPrashant Bhole jsonw_start_array(json_wtr); 3044990f1f4SPrashant Bhole hash_for_each_possible(prog_table.table, obj, hash, info->id) { 3054990f1f4SPrashant Bhole if (obj->id == info->id) 3064990f1f4SPrashant Bhole jsonw_string(json_wtr, obj->path); 3074990f1f4SPrashant Bhole } 3084990f1f4SPrashant Bhole jsonw_end_array(json_wtr); 3094990f1f4SPrashant Bhole } 3104990f1f4SPrashant Bhole 311743cc665SQuentin Monnet jsonw_end_object(json_wtr); 312743cc665SQuentin Monnet } 313743cc665SQuentin Monnet 314743cc665SQuentin Monnet static void print_prog_plain(struct bpf_prog_info *info, int fd) 315743cc665SQuentin Monnet { 316743cc665SQuentin Monnet char *memlock; 317743cc665SQuentin Monnet 318743cc665SQuentin Monnet printf("%u: ", info->id); 319743cc665SQuentin Monnet if (info->type < ARRAY_SIZE(prog_type_name)) 320743cc665SQuentin Monnet printf("%s ", prog_type_name[info->type]); 321743cc665SQuentin Monnet else 322743cc665SQuentin Monnet printf("type %u ", info->type); 323743cc665SQuentin Monnet 324743cc665SQuentin Monnet if (*info->name) 325743cc665SQuentin Monnet printf("name %s ", info->name); 326743cc665SQuentin Monnet 327743cc665SQuentin Monnet printf("tag "); 328743cc665SQuentin Monnet fprint_hex(stdout, info->tag, BPF_TAG_SIZE, ""); 32952262210SJakub Kicinski print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino); 3309b984a20SJiri Olsa printf("%s", info->gpl_compatible ? " gpl" : ""); 331743cc665SQuentin Monnet printf("\n"); 332743cc665SQuentin Monnet 333743cc665SQuentin Monnet if (info->load_time) { 334743cc665SQuentin Monnet char buf[32]; 335743cc665SQuentin Monnet 336743cc665SQuentin Monnet print_boot_time(info->load_time, buf, sizeof(buf)); 337743cc665SQuentin Monnet 338743cc665SQuentin Monnet /* Piggy back on load_time, since 0 uid is a valid one */ 339743cc665SQuentin Monnet printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid); 340743cc665SQuentin Monnet } 341743cc665SQuentin Monnet 342743cc665SQuentin Monnet printf("\txlated %uB", info->xlated_prog_len); 343743cc665SQuentin Monnet 344743cc665SQuentin Monnet if (info->jited_prog_len) 345743cc665SQuentin Monnet printf(" jited %uB", info->jited_prog_len); 34671bb428fSJakub Kicinski else 34771bb428fSJakub Kicinski printf(" not jited"); 34871bb428fSJakub Kicinski 34971bb428fSJakub Kicinski memlock = get_fdinfo(fd, "memlock"); 35071bb428fSJakub Kicinski if (memlock) 35171bb428fSJakub Kicinski printf(" memlock %sB", memlock); 35271bb428fSJakub Kicinski free(memlock); 35371bb428fSJakub Kicinski 354743cc665SQuentin Monnet if (info->nr_map_ids) 355743cc665SQuentin Monnet show_prog_maps(fd, info->nr_map_ids); 35671bb428fSJakub Kicinski 3574990f1f4SPrashant Bhole if (!hash_empty(prog_table.table)) { 3584990f1f4SPrashant Bhole struct pinned_obj *obj; 3594990f1f4SPrashant Bhole 3604990f1f4SPrashant Bhole hash_for_each_possible(prog_table.table, obj, hash, info->id) { 3614990f1f4SPrashant Bhole if (obj->id == info->id) 362*a8bfd2bcSQuentin Monnet printf("\n\tpinned %s", obj->path); 3634990f1f4SPrashant Bhole } 3644990f1f4SPrashant Bhole } 3654990f1f4SPrashant Bhole 36671bb428fSJakub Kicinski printf("\n"); 367743cc665SQuentin Monnet } 368743cc665SQuentin Monnet 369743cc665SQuentin Monnet static int show_prog(int fd) 370743cc665SQuentin Monnet { 371743cc665SQuentin Monnet struct bpf_prog_info info = {}; 372743cc665SQuentin Monnet __u32 len = sizeof(info); 373743cc665SQuentin Monnet int err; 374743cc665SQuentin Monnet 375743cc665SQuentin Monnet err = bpf_obj_get_info_by_fd(fd, &info, &len); 376743cc665SQuentin Monnet if (err) { 3779a5ab8bfSQuentin Monnet p_err("can't get prog info: %s", strerror(errno)); 378743cc665SQuentin Monnet return -1; 379743cc665SQuentin Monnet } 380743cc665SQuentin Monnet 381743cc665SQuentin Monnet if (json_output) 382743cc665SQuentin Monnet print_prog_json(&info, fd); 383743cc665SQuentin Monnet else 384743cc665SQuentin Monnet print_prog_plain(&info, fd); 38571bb428fSJakub Kicinski 38671bb428fSJakub Kicinski return 0; 38771bb428fSJakub Kicinski } 38871bb428fSJakub Kicinski 38971bb428fSJakub Kicinski static int do_show(int argc, char **argv) 390743cc665SQuentin Monnet { 391743cc665SQuentin Monnet __u32 id = 0; 39271bb428fSJakub Kicinski int err; 39371bb428fSJakub Kicinski int fd; 39471bb428fSJakub Kicinski 395c541b734SPrashant Bhole if (show_pinned) 3964990f1f4SPrashant Bhole build_pinned_obj_table(&prog_table, BPF_OBJ_PROG); 3974990f1f4SPrashant Bhole 39871bb428fSJakub Kicinski if (argc == 2) { 39971bb428fSJakub Kicinski fd = prog_parse_fd(&argc, &argv); 40071bb428fSJakub Kicinski if (fd < 0) 40171bb428fSJakub Kicinski return -1; 40271bb428fSJakub Kicinski 40371bb428fSJakub Kicinski return show_prog(fd); 40471bb428fSJakub Kicinski } 40571bb428fSJakub Kicinski 40671bb428fSJakub Kicinski if (argc) 40771bb428fSJakub Kicinski return BAD_ARG(); 40871bb428fSJakub Kicinski 409743cc665SQuentin Monnet if (json_output) 410743cc665SQuentin Monnet jsonw_start_array(json_wtr); 41171bb428fSJakub Kicinski while (true) { 41271bb428fSJakub Kicinski err = bpf_prog_get_next_id(id, &id); 41371bb428fSJakub Kicinski if (err) { 4141739c26dSQuentin Monnet if (errno == ENOENT) { 4151739c26dSQuentin Monnet err = 0; 41671bb428fSJakub Kicinski break; 4171739c26dSQuentin Monnet } 4189a5ab8bfSQuentin Monnet p_err("can't get next program: %s%s", strerror(errno), 4199a5ab8bfSQuentin Monnet errno == EINVAL ? " -- kernel too old?" : ""); 420743cc665SQuentin Monnet err = -1; 421743cc665SQuentin Monnet break; 42271bb428fSJakub Kicinski } 42371bb428fSJakub Kicinski 42471bb428fSJakub Kicinski fd = bpf_prog_get_fd_by_id(id); 42571bb428fSJakub Kicinski if (fd < 0) { 4268207c6ddSJakub Kicinski if (errno == ENOENT) 4278207c6ddSJakub Kicinski continue; 4289a5ab8bfSQuentin Monnet p_err("can't get prog by id (%u): %s", 42971bb428fSJakub Kicinski id, strerror(errno)); 430743cc665SQuentin Monnet err = -1; 431743cc665SQuentin Monnet break; 43271bb428fSJakub Kicinski } 43371bb428fSJakub Kicinski 43471bb428fSJakub Kicinski err = show_prog(fd); 43571bb428fSJakub Kicinski close(fd); 43671bb428fSJakub Kicinski if (err) 437743cc665SQuentin Monnet break; 43871bb428fSJakub Kicinski } 43971bb428fSJakub Kicinski 440743cc665SQuentin Monnet if (json_output) 441743cc665SQuentin Monnet jsonw_end_array(json_wtr); 442743cc665SQuentin Monnet 443743cc665SQuentin Monnet return err; 44471bb428fSJakub Kicinski } 44571bb428fSJakub Kicinski 44671bb428fSJakub Kicinski static int do_dump(int argc, char **argv) 44771bb428fSJakub Kicinski { 448f84192eeSSandipan Das unsigned long *func_ksyms = NULL; 44971bb428fSJakub Kicinski struct bpf_prog_info info = {}; 450f7f62c71SSandipan Das unsigned int *func_lens = NULL; 4513ddeac67SJakub Kicinski const char *disasm_opt = NULL; 452f84192eeSSandipan Das unsigned int nr_func_ksyms; 453f7f62c71SSandipan Das unsigned int nr_func_lens; 4547105e828SDaniel Borkmann struct dump_data dd = {}; 45571bb428fSJakub Kicinski __u32 len = sizeof(info); 45671bb428fSJakub Kicinski unsigned int buf_size; 45771bb428fSJakub Kicinski char *filepath = NULL; 45871bb428fSJakub Kicinski bool opcodes = false; 459b6c1cedbSJiong Wang bool visual = false; 46071bb428fSJakub Kicinski unsigned char *buf; 46171bb428fSJakub Kicinski __u32 *member_len; 46271bb428fSJakub Kicinski __u64 *member_ptr; 46371bb428fSJakub Kicinski ssize_t n; 46471bb428fSJakub Kicinski int err; 46571bb428fSJakub Kicinski int fd; 46671bb428fSJakub Kicinski 46771bb428fSJakub Kicinski if (is_prefix(*argv, "jited")) { 46871bb428fSJakub Kicinski member_len = &info.jited_prog_len; 46971bb428fSJakub Kicinski member_ptr = &info.jited_prog_insns; 47071bb428fSJakub Kicinski } else if (is_prefix(*argv, "xlated")) { 47171bb428fSJakub Kicinski member_len = &info.xlated_prog_len; 47271bb428fSJakub Kicinski member_ptr = &info.xlated_prog_insns; 47371bb428fSJakub Kicinski } else { 4749a5ab8bfSQuentin Monnet p_err("expected 'xlated' or 'jited', got: %s", *argv); 47571bb428fSJakub Kicinski return -1; 47671bb428fSJakub Kicinski } 47771bb428fSJakub Kicinski NEXT_ARG(); 47871bb428fSJakub Kicinski 47971bb428fSJakub Kicinski if (argc < 2) 48071bb428fSJakub Kicinski usage(); 48171bb428fSJakub Kicinski 48271bb428fSJakub Kicinski fd = prog_parse_fd(&argc, &argv); 48371bb428fSJakub Kicinski if (fd < 0) 48471bb428fSJakub Kicinski return -1; 48571bb428fSJakub Kicinski 48671bb428fSJakub Kicinski if (is_prefix(*argv, "file")) { 48771bb428fSJakub Kicinski NEXT_ARG(); 48871bb428fSJakub Kicinski if (!argc) { 4899a5ab8bfSQuentin Monnet p_err("expected file path"); 49071bb428fSJakub Kicinski return -1; 49171bb428fSJakub Kicinski } 49271bb428fSJakub Kicinski 49371bb428fSJakub Kicinski filepath = *argv; 49471bb428fSJakub Kicinski NEXT_ARG(); 49571bb428fSJakub Kicinski } else if (is_prefix(*argv, "opcodes")) { 49671bb428fSJakub Kicinski opcodes = true; 49771bb428fSJakub Kicinski NEXT_ARG(); 498b6c1cedbSJiong Wang } else if (is_prefix(*argv, "visual")) { 499b6c1cedbSJiong Wang visual = true; 500b6c1cedbSJiong Wang NEXT_ARG(); 50171bb428fSJakub Kicinski } 50271bb428fSJakub Kicinski 50371bb428fSJakub Kicinski if (argc) { 50471bb428fSJakub Kicinski usage(); 50571bb428fSJakub Kicinski return -1; 50671bb428fSJakub Kicinski } 50771bb428fSJakub Kicinski 50871bb428fSJakub Kicinski err = bpf_obj_get_info_by_fd(fd, &info, &len); 50971bb428fSJakub Kicinski if (err) { 5109a5ab8bfSQuentin Monnet p_err("can't get prog info: %s", strerror(errno)); 51171bb428fSJakub Kicinski return -1; 51271bb428fSJakub Kicinski } 51371bb428fSJakub Kicinski 51471bb428fSJakub Kicinski if (!*member_len) { 5159a5ab8bfSQuentin Monnet p_info("no instructions returned"); 51671bb428fSJakub Kicinski close(fd); 51771bb428fSJakub Kicinski return 0; 51871bb428fSJakub Kicinski } 51971bb428fSJakub Kicinski 52071bb428fSJakub Kicinski buf_size = *member_len; 52171bb428fSJakub Kicinski 52271bb428fSJakub Kicinski buf = malloc(buf_size); 52371bb428fSJakub Kicinski if (!buf) { 5249a5ab8bfSQuentin Monnet p_err("mem alloc failed"); 52571bb428fSJakub Kicinski close(fd); 52671bb428fSJakub Kicinski return -1; 52771bb428fSJakub Kicinski } 52871bb428fSJakub Kicinski 529f84192eeSSandipan Das nr_func_ksyms = info.nr_jited_ksyms; 530f84192eeSSandipan Das if (nr_func_ksyms) { 531f84192eeSSandipan Das func_ksyms = malloc(nr_func_ksyms * sizeof(__u64)); 532f84192eeSSandipan Das if (!func_ksyms) { 533f84192eeSSandipan Das p_err("mem alloc failed"); 534f84192eeSSandipan Das close(fd); 535f84192eeSSandipan Das goto err_free; 536f84192eeSSandipan Das } 537f84192eeSSandipan Das } 538f84192eeSSandipan Das 539f7f62c71SSandipan Das nr_func_lens = info.nr_jited_func_lens; 540f7f62c71SSandipan Das if (nr_func_lens) { 541f7f62c71SSandipan Das func_lens = malloc(nr_func_lens * sizeof(__u32)); 542f7f62c71SSandipan Das if (!func_lens) { 543f7f62c71SSandipan Das p_err("mem alloc failed"); 544f7f62c71SSandipan Das close(fd); 545f7f62c71SSandipan Das goto err_free; 546f7f62c71SSandipan Das } 547f7f62c71SSandipan Das } 548f7f62c71SSandipan Das 54971bb428fSJakub Kicinski memset(&info, 0, sizeof(info)); 55071bb428fSJakub Kicinski 55171bb428fSJakub Kicinski *member_ptr = ptr_to_u64(buf); 55271bb428fSJakub Kicinski *member_len = buf_size; 553f84192eeSSandipan Das info.jited_ksyms = ptr_to_u64(func_ksyms); 554f84192eeSSandipan Das info.nr_jited_ksyms = nr_func_ksyms; 555f7f62c71SSandipan Das info.jited_func_lens = ptr_to_u64(func_lens); 556f7f62c71SSandipan Das info.nr_jited_func_lens = nr_func_lens; 55771bb428fSJakub Kicinski 55871bb428fSJakub Kicinski err = bpf_obj_get_info_by_fd(fd, &info, &len); 55971bb428fSJakub Kicinski close(fd); 56071bb428fSJakub Kicinski if (err) { 5619a5ab8bfSQuentin Monnet p_err("can't get prog info: %s", strerror(errno)); 56271bb428fSJakub Kicinski goto err_free; 56371bb428fSJakub Kicinski } 56471bb428fSJakub Kicinski 56571bb428fSJakub Kicinski if (*member_len > buf_size) { 5669a5ab8bfSQuentin Monnet p_err("too many instructions returned"); 56771bb428fSJakub Kicinski goto err_free; 56871bb428fSJakub Kicinski } 56971bb428fSJakub Kicinski 570f84192eeSSandipan Das if (info.nr_jited_ksyms > nr_func_ksyms) { 571f84192eeSSandipan Das p_err("too many addresses returned"); 572f84192eeSSandipan Das goto err_free; 573f84192eeSSandipan Das } 574f84192eeSSandipan Das 575f7f62c71SSandipan Das if (info.nr_jited_func_lens > nr_func_lens) { 576f7f62c71SSandipan Das p_err("too many values returned"); 577f7f62c71SSandipan Das goto err_free; 578f7f62c71SSandipan Das } 579f7f62c71SSandipan Das 5807105e828SDaniel Borkmann if ((member_len == &info.jited_prog_len && 5817105e828SDaniel Borkmann info.jited_prog_insns == 0) || 5827105e828SDaniel Borkmann (member_len == &info.xlated_prog_len && 5837105e828SDaniel Borkmann info.xlated_prog_insns == 0)) { 5847105e828SDaniel Borkmann p_err("error retrieving insn dump: kernel.kptr_restrict set?"); 5857105e828SDaniel Borkmann goto err_free; 5867105e828SDaniel Borkmann } 5877105e828SDaniel Borkmann 58871bb428fSJakub Kicinski if (filepath) { 58971bb428fSJakub Kicinski fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600); 59071bb428fSJakub Kicinski if (fd < 0) { 5919a5ab8bfSQuentin Monnet p_err("can't open file %s: %s", filepath, 59271bb428fSJakub Kicinski strerror(errno)); 59371bb428fSJakub Kicinski goto err_free; 59471bb428fSJakub Kicinski } 59571bb428fSJakub Kicinski 59671bb428fSJakub Kicinski n = write(fd, buf, *member_len); 59771bb428fSJakub Kicinski close(fd); 59871bb428fSJakub Kicinski if (n != *member_len) { 5999a5ab8bfSQuentin Monnet p_err("error writing output file: %s", 60071bb428fSJakub Kicinski n < 0 ? strerror(errno) : "short write"); 60171bb428fSJakub Kicinski goto err_free; 60271bb428fSJakub Kicinski } 60352c84d36SQuentin Monnet 60452c84d36SQuentin Monnet if (json_output) 60552c84d36SQuentin Monnet jsonw_null(json_wtr); 6063197239dSJiong Wang } else if (member_len == &info.jited_prog_len) { 607e6593596SJiong Wang const char *name = NULL; 608e6593596SJiong Wang 609e6593596SJiong Wang if (info.ifindex) { 6103ddeac67SJakub Kicinski name = ifindex_to_bfd_params(info.ifindex, 611e6593596SJiong Wang info.netns_dev, 6123ddeac67SJakub Kicinski info.netns_ino, 6133ddeac67SJakub Kicinski &disasm_opt); 614e6593596SJiong Wang if (!name) 615e6593596SJiong Wang goto err_free; 616e6593596SJiong Wang } 617e6593596SJiong Wang 618f7f62c71SSandipan Das if (info.nr_jited_func_lens && info.jited_func_lens) { 619f7f62c71SSandipan Das struct kernel_sym *sym = NULL; 620f7f62c71SSandipan Das char sym_name[SYM_MAX_NAME]; 621f7f62c71SSandipan Das unsigned char *img = buf; 622f7f62c71SSandipan Das __u64 *ksyms = NULL; 623f7f62c71SSandipan Das __u32 *lens; 624f7f62c71SSandipan Das __u32 i; 625f7f62c71SSandipan Das 626f7f62c71SSandipan Das if (info.nr_jited_ksyms) { 627f7f62c71SSandipan Das kernel_syms_load(&dd); 628f7f62c71SSandipan Das ksyms = (__u64 *) info.jited_ksyms; 629f7f62c71SSandipan Das } 630f7f62c71SSandipan Das 631f7f62c71SSandipan Das if (json_output) 632f7f62c71SSandipan Das jsonw_start_array(json_wtr); 633f7f62c71SSandipan Das 634f7f62c71SSandipan Das lens = (__u32 *) info.jited_func_lens; 635f7f62c71SSandipan Das for (i = 0; i < info.nr_jited_func_lens; i++) { 636f7f62c71SSandipan Das if (ksyms) { 637f7f62c71SSandipan Das sym = kernel_syms_search(&dd, ksyms[i]); 638f7f62c71SSandipan Das if (sym) 639f7f62c71SSandipan Das sprintf(sym_name, "%s", sym->name); 640f7f62c71SSandipan Das else 641f7f62c71SSandipan Das sprintf(sym_name, "0x%016llx", ksyms[i]); 642f7f62c71SSandipan Das } else { 643f7f62c71SSandipan Das strcpy(sym_name, "unknown"); 644f7f62c71SSandipan Das } 645f7f62c71SSandipan Das 646f7f62c71SSandipan Das if (json_output) { 647f7f62c71SSandipan Das jsonw_start_object(json_wtr); 648f7f62c71SSandipan Das jsonw_name(json_wtr, "name"); 649f7f62c71SSandipan Das jsonw_string(json_wtr, sym_name); 650f7f62c71SSandipan Das jsonw_name(json_wtr, "insns"); 651f7f62c71SSandipan Das } else { 652f7f62c71SSandipan Das printf("%s:\n", sym_name); 653f7f62c71SSandipan Das } 654f7f62c71SSandipan Das 6553ddeac67SJakub Kicinski disasm_print_insn(img, lens[i], opcodes, name, 6563ddeac67SJakub Kicinski disasm_opt); 657f7f62c71SSandipan Das img += lens[i]; 658f7f62c71SSandipan Das 659f7f62c71SSandipan Das if (json_output) 660f7f62c71SSandipan Das jsonw_end_object(json_wtr); 661f7f62c71SSandipan Das else 662f7f62c71SSandipan Das printf("\n"); 663f7f62c71SSandipan Das } 664f7f62c71SSandipan Das 665f7f62c71SSandipan Das if (json_output) 666f7f62c71SSandipan Das jsonw_end_array(json_wtr); 667f7f62c71SSandipan Das } else { 6683ddeac67SJakub Kicinski disasm_print_insn(buf, *member_len, opcodes, name, 6693ddeac67SJakub Kicinski disasm_opt); 670f7f62c71SSandipan Das } 671b6c1cedbSJiong Wang } else if (visual) { 672b6c1cedbSJiong Wang if (json_output) 673b6c1cedbSJiong Wang jsonw_null(json_wtr); 674b6c1cedbSJiong Wang else 675b6c1cedbSJiong Wang dump_xlated_cfg(buf, *member_len); 6767105e828SDaniel Borkmann } else { 6777105e828SDaniel Borkmann kernel_syms_load(&dd); 678f84192eeSSandipan Das dd.nr_jited_ksyms = info.nr_jited_ksyms; 679f84192eeSSandipan Das dd.jited_ksyms = (__u64 *) info.jited_ksyms; 680f84192eeSSandipan Das 681f05e2c32SQuentin Monnet if (json_output) 6827105e828SDaniel Borkmann dump_xlated_json(&dd, buf, *member_len, opcodes); 683f05e2c32SQuentin Monnet else 6847105e828SDaniel Borkmann dump_xlated_plain(&dd, buf, *member_len, opcodes); 6857105e828SDaniel Borkmann kernel_syms_destroy(&dd); 6867105e828SDaniel Borkmann } 68771bb428fSJakub Kicinski 68871bb428fSJakub Kicinski free(buf); 689f84192eeSSandipan Das free(func_ksyms); 690f7f62c71SSandipan Das free(func_lens); 69171bb428fSJakub Kicinski return 0; 69271bb428fSJakub Kicinski 69371bb428fSJakub Kicinski err_free: 69471bb428fSJakub Kicinski free(buf); 695f84192eeSSandipan Das free(func_ksyms); 696f7f62c71SSandipan Das free(func_lens); 69771bb428fSJakub Kicinski return -1; 69871bb428fSJakub Kicinski } 69971bb428fSJakub Kicinski 70071bb428fSJakub Kicinski static int do_pin(int argc, char **argv) 70171bb428fSJakub Kicinski { 702004b45c0SQuentin Monnet int err; 703004b45c0SQuentin Monnet 704004b45c0SQuentin Monnet err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id); 705004b45c0SQuentin Monnet if (!err && json_output) 706004b45c0SQuentin Monnet jsonw_null(json_wtr); 707004b45c0SQuentin Monnet return err; 70871bb428fSJakub Kicinski } 70971bb428fSJakub Kicinski 7103ff5a4dcSJakub Kicinski struct map_replace { 7113ff5a4dcSJakub Kicinski int idx; 7123ff5a4dcSJakub Kicinski int fd; 7133ff5a4dcSJakub Kicinski char *name; 7143ff5a4dcSJakub Kicinski }; 7153ff5a4dcSJakub Kicinski 7163ff5a4dcSJakub Kicinski int map_replace_compar(const void *p1, const void *p2) 7173ff5a4dcSJakub Kicinski { 7183ff5a4dcSJakub Kicinski const struct map_replace *a = p1, *b = p2; 7193ff5a4dcSJakub Kicinski 7203ff5a4dcSJakub Kicinski return a->idx - b->idx; 7213ff5a4dcSJakub Kicinski } 7223ff5a4dcSJakub Kicinski 723b7d3826cSJohn Fastabend static int do_attach(int argc, char **argv) 724b7d3826cSJohn Fastabend { 725b7d3826cSJohn Fastabend enum bpf_attach_type attach_type; 726b7d3826cSJohn Fastabend int err, mapfd, progfd; 727b7d3826cSJohn Fastabend 728b7d3826cSJohn Fastabend if (!REQ_ARGS(5)) { 729b7d3826cSJohn Fastabend p_err("too few parameters for map attach"); 730b7d3826cSJohn Fastabend return -EINVAL; 731b7d3826cSJohn Fastabend } 732b7d3826cSJohn Fastabend 733b7d3826cSJohn Fastabend progfd = prog_parse_fd(&argc, &argv); 734b7d3826cSJohn Fastabend if (progfd < 0) 735b7d3826cSJohn Fastabend return progfd; 736b7d3826cSJohn Fastabend 737b7d3826cSJohn Fastabend attach_type = parse_attach_type(*argv); 738b7d3826cSJohn Fastabend if (attach_type == __MAX_BPF_ATTACH_TYPE) { 739b7d3826cSJohn Fastabend p_err("invalid attach type"); 740b7d3826cSJohn Fastabend return -EINVAL; 741b7d3826cSJohn Fastabend } 742b7d3826cSJohn Fastabend NEXT_ARG(); 743b7d3826cSJohn Fastabend 744b7d3826cSJohn Fastabend mapfd = map_parse_fd(&argc, &argv); 745b7d3826cSJohn Fastabend if (mapfd < 0) 746b7d3826cSJohn Fastabend return mapfd; 747b7d3826cSJohn Fastabend 748b7d3826cSJohn Fastabend err = bpf_prog_attach(progfd, mapfd, attach_type, 0); 749b7d3826cSJohn Fastabend if (err) { 750b7d3826cSJohn Fastabend p_err("failed prog attach to map"); 751b7d3826cSJohn Fastabend return -EINVAL; 752b7d3826cSJohn Fastabend } 753b7d3826cSJohn Fastabend 754b7d3826cSJohn Fastabend if (json_output) 755b7d3826cSJohn Fastabend jsonw_null(json_wtr); 756b7d3826cSJohn Fastabend return 0; 757b7d3826cSJohn Fastabend } 758b7d3826cSJohn Fastabend 759b7d3826cSJohn Fastabend static int do_detach(int argc, char **argv) 760b7d3826cSJohn Fastabend { 761b7d3826cSJohn Fastabend enum bpf_attach_type attach_type; 762b7d3826cSJohn Fastabend int err, mapfd, progfd; 763b7d3826cSJohn Fastabend 764b7d3826cSJohn Fastabend if (!REQ_ARGS(5)) { 765b7d3826cSJohn Fastabend p_err("too few parameters for map detach"); 766b7d3826cSJohn Fastabend return -EINVAL; 767b7d3826cSJohn Fastabend } 768b7d3826cSJohn Fastabend 769b7d3826cSJohn Fastabend progfd = prog_parse_fd(&argc, &argv); 770b7d3826cSJohn Fastabend if (progfd < 0) 771b7d3826cSJohn Fastabend return progfd; 772b7d3826cSJohn Fastabend 773b7d3826cSJohn Fastabend attach_type = parse_attach_type(*argv); 774b7d3826cSJohn Fastabend if (attach_type == __MAX_BPF_ATTACH_TYPE) { 775b7d3826cSJohn Fastabend p_err("invalid attach type"); 776b7d3826cSJohn Fastabend return -EINVAL; 777b7d3826cSJohn Fastabend } 778b7d3826cSJohn Fastabend NEXT_ARG(); 779b7d3826cSJohn Fastabend 780b7d3826cSJohn Fastabend mapfd = map_parse_fd(&argc, &argv); 781b7d3826cSJohn Fastabend if (mapfd < 0) 782b7d3826cSJohn Fastabend return mapfd; 783b7d3826cSJohn Fastabend 784b7d3826cSJohn Fastabend err = bpf_prog_detach2(progfd, mapfd, attach_type); 785b7d3826cSJohn Fastabend if (err) { 786b7d3826cSJohn Fastabend p_err("failed prog detach from map"); 787b7d3826cSJohn Fastabend return -EINVAL; 788b7d3826cSJohn Fastabend } 789b7d3826cSJohn Fastabend 790b7d3826cSJohn Fastabend if (json_output) 791b7d3826cSJohn Fastabend jsonw_null(json_wtr); 792b7d3826cSJohn Fastabend return 0; 793b7d3826cSJohn Fastabend } 79449a086c2SRoman Gushchin static int do_load(int argc, char **argv) 79549a086c2SRoman Gushchin { 796c8406848SJakub Kicinski enum bpf_attach_type expected_attach_type; 797c8406848SJakub Kicinski struct bpf_object_open_attr attr = { 798ba6dd679SJakub Kicinski .prog_type = BPF_PROG_TYPE_UNSPEC, 799ba6dd679SJakub Kicinski }; 8003ff5a4dcSJakub Kicinski struct map_replace *map_replace = NULL; 8013ff5a4dcSJakub Kicinski unsigned int old_map_fds = 0; 802c8406848SJakub Kicinski struct bpf_program *prog; 80349a086c2SRoman Gushchin struct bpf_object *obj; 804c8406848SJakub Kicinski struct bpf_map *map; 805c8406848SJakub Kicinski const char *pinfile; 8063ff5a4dcSJakub Kicinski unsigned int i, j; 807c8406848SJakub Kicinski __u32 ifindex = 0; 8083ff5a4dcSJakub Kicinski int idx, err; 80949a086c2SRoman Gushchin 8108d1fc3deSJakub Kicinski if (!REQ_ARGS(2)) 8118d1fc3deSJakub Kicinski return -1; 812c8406848SJakub Kicinski attr.file = GET_ARG(); 8138d1fc3deSJakub Kicinski pinfile = GET_ARG(); 81449a086c2SRoman Gushchin 815ba6dd679SJakub Kicinski while (argc) { 81649f2cba3SJakub Kicinski if (is_prefix(*argv, "type")) { 81749f2cba3SJakub Kicinski char *type; 81849f2cba3SJakub Kicinski 81949f2cba3SJakub Kicinski NEXT_ARG(); 82049f2cba3SJakub Kicinski 82149f2cba3SJakub Kicinski if (attr.prog_type != BPF_PROG_TYPE_UNSPEC) { 82249f2cba3SJakub Kicinski p_err("program type already specified"); 8233ff5a4dcSJakub Kicinski goto err_free_reuse_maps; 82449f2cba3SJakub Kicinski } 82549f2cba3SJakub Kicinski if (!REQ_ARGS(1)) 8263ff5a4dcSJakub Kicinski goto err_free_reuse_maps; 82749f2cba3SJakub Kicinski 82849f2cba3SJakub Kicinski /* Put a '/' at the end of type to appease libbpf */ 82949f2cba3SJakub Kicinski type = malloc(strlen(*argv) + 2); 83049f2cba3SJakub Kicinski if (!type) { 83149f2cba3SJakub Kicinski p_err("mem alloc failed"); 8323ff5a4dcSJakub Kicinski goto err_free_reuse_maps; 83349f2cba3SJakub Kicinski } 83449f2cba3SJakub Kicinski *type = 0; 83549f2cba3SJakub Kicinski strcat(type, *argv); 83649f2cba3SJakub Kicinski strcat(type, "/"); 83749f2cba3SJakub Kicinski 83849f2cba3SJakub Kicinski err = libbpf_prog_type_by_name(type, &attr.prog_type, 839c8406848SJakub Kicinski &expected_attach_type); 84049f2cba3SJakub Kicinski free(type); 84149f2cba3SJakub Kicinski if (err < 0) { 84249f2cba3SJakub Kicinski p_err("unknown program type '%s'", *argv); 8433ff5a4dcSJakub Kicinski goto err_free_reuse_maps; 84449f2cba3SJakub Kicinski } 84549f2cba3SJakub Kicinski NEXT_ARG(); 8463ff5a4dcSJakub Kicinski } else if (is_prefix(*argv, "map")) { 8473ff5a4dcSJakub Kicinski char *endptr, *name; 8483ff5a4dcSJakub Kicinski int fd; 8493ff5a4dcSJakub Kicinski 8503ff5a4dcSJakub Kicinski NEXT_ARG(); 8513ff5a4dcSJakub Kicinski 8523ff5a4dcSJakub Kicinski if (!REQ_ARGS(4)) 8533ff5a4dcSJakub Kicinski goto err_free_reuse_maps; 8543ff5a4dcSJakub Kicinski 8553ff5a4dcSJakub Kicinski if (is_prefix(*argv, "idx")) { 8563ff5a4dcSJakub Kicinski NEXT_ARG(); 8573ff5a4dcSJakub Kicinski 8583ff5a4dcSJakub Kicinski idx = strtoul(*argv, &endptr, 0); 8593ff5a4dcSJakub Kicinski if (*endptr) { 8603ff5a4dcSJakub Kicinski p_err("can't parse %s as IDX", *argv); 8613ff5a4dcSJakub Kicinski goto err_free_reuse_maps; 8623ff5a4dcSJakub Kicinski } 8633ff5a4dcSJakub Kicinski name = NULL; 8643ff5a4dcSJakub Kicinski } else if (is_prefix(*argv, "name")) { 8653ff5a4dcSJakub Kicinski NEXT_ARG(); 8663ff5a4dcSJakub Kicinski 8673ff5a4dcSJakub Kicinski name = *argv; 8683ff5a4dcSJakub Kicinski idx = -1; 8693ff5a4dcSJakub Kicinski } else { 8703ff5a4dcSJakub Kicinski p_err("expected 'idx' or 'name', got: '%s'?", 8713ff5a4dcSJakub Kicinski *argv); 8723ff5a4dcSJakub Kicinski goto err_free_reuse_maps; 8733ff5a4dcSJakub Kicinski } 8743ff5a4dcSJakub Kicinski NEXT_ARG(); 8753ff5a4dcSJakub Kicinski 8763ff5a4dcSJakub Kicinski fd = map_parse_fd(&argc, &argv); 8773ff5a4dcSJakub Kicinski if (fd < 0) 8783ff5a4dcSJakub Kicinski goto err_free_reuse_maps; 8793ff5a4dcSJakub Kicinski 8803ff5a4dcSJakub Kicinski map_replace = reallocarray(map_replace, old_map_fds + 1, 8813ff5a4dcSJakub Kicinski sizeof(*map_replace)); 8823ff5a4dcSJakub Kicinski if (!map_replace) { 8833ff5a4dcSJakub Kicinski p_err("mem alloc failed"); 8843ff5a4dcSJakub Kicinski goto err_free_reuse_maps; 8853ff5a4dcSJakub Kicinski } 8863ff5a4dcSJakub Kicinski map_replace[old_map_fds].idx = idx; 8873ff5a4dcSJakub Kicinski map_replace[old_map_fds].name = name; 8883ff5a4dcSJakub Kicinski map_replace[old_map_fds].fd = fd; 8893ff5a4dcSJakub Kicinski old_map_fds++; 89049f2cba3SJakub Kicinski } else if (is_prefix(*argv, "dev")) { 891ba6dd679SJakub Kicinski NEXT_ARG(); 892ba6dd679SJakub Kicinski 893c8406848SJakub Kicinski if (ifindex) { 894ba6dd679SJakub Kicinski p_err("offload device already specified"); 8953ff5a4dcSJakub Kicinski goto err_free_reuse_maps; 896ba6dd679SJakub Kicinski } 897ba6dd679SJakub Kicinski if (!REQ_ARGS(1)) 8983ff5a4dcSJakub Kicinski goto err_free_reuse_maps; 899ba6dd679SJakub Kicinski 900c8406848SJakub Kicinski ifindex = if_nametoindex(*argv); 901c8406848SJakub Kicinski if (!ifindex) { 902ba6dd679SJakub Kicinski p_err("unrecognized netdevice '%s': %s", 903ba6dd679SJakub Kicinski *argv, strerror(errno)); 9043ff5a4dcSJakub Kicinski goto err_free_reuse_maps; 905ba6dd679SJakub Kicinski } 906ba6dd679SJakub Kicinski NEXT_ARG(); 907ba6dd679SJakub Kicinski } else { 9083ff5a4dcSJakub Kicinski p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?", 909ba6dd679SJakub Kicinski *argv); 9103ff5a4dcSJakub Kicinski goto err_free_reuse_maps; 911ba6dd679SJakub Kicinski } 912ba6dd679SJakub Kicinski } 913ba6dd679SJakub Kicinski 914c034a177SJohn Fastabend obj = __bpf_object__open_xattr(&attr, bpf_flags); 915c8406848SJakub Kicinski if (IS_ERR_OR_NULL(obj)) { 916c8406848SJakub Kicinski p_err("failed to open object file"); 9173ff5a4dcSJakub Kicinski goto err_free_reuse_maps; 91849a086c2SRoman Gushchin } 91949a086c2SRoman Gushchin 920c8406848SJakub Kicinski prog = bpf_program__next(NULL, obj); 921c8406848SJakub Kicinski if (!prog) { 922c8406848SJakub Kicinski p_err("object file doesn't contain any bpf program"); 923c8406848SJakub Kicinski goto err_close_obj; 924c8406848SJakub Kicinski } 925c8406848SJakub Kicinski 926c8406848SJakub Kicinski bpf_program__set_ifindex(prog, ifindex); 927c8406848SJakub Kicinski if (attr.prog_type == BPF_PROG_TYPE_UNSPEC) { 928c8406848SJakub Kicinski const char *sec_name = bpf_program__title(prog, false); 929c8406848SJakub Kicinski 930c8406848SJakub Kicinski err = libbpf_prog_type_by_name(sec_name, &attr.prog_type, 931c8406848SJakub Kicinski &expected_attach_type); 932c8406848SJakub Kicinski if (err < 0) { 933c8406848SJakub Kicinski p_err("failed to guess program type based on section name %s\n", 934c8406848SJakub Kicinski sec_name); 935c8406848SJakub Kicinski goto err_close_obj; 936c8406848SJakub Kicinski } 937c8406848SJakub Kicinski } 938c8406848SJakub Kicinski bpf_program__set_type(prog, attr.prog_type); 939c8406848SJakub Kicinski bpf_program__set_expected_attach_type(prog, expected_attach_type); 940c8406848SJakub Kicinski 9413ff5a4dcSJakub Kicinski qsort(map_replace, old_map_fds, sizeof(*map_replace), 9423ff5a4dcSJakub Kicinski map_replace_compar); 9433ff5a4dcSJakub Kicinski 9443ff5a4dcSJakub Kicinski /* After the sort maps by name will be first on the list, because they 9453ff5a4dcSJakub Kicinski * have idx == -1. Resolve them. 9463ff5a4dcSJakub Kicinski */ 9473ff5a4dcSJakub Kicinski j = 0; 9483ff5a4dcSJakub Kicinski while (j < old_map_fds && map_replace[j].name) { 9493ff5a4dcSJakub Kicinski i = 0; 9503ff5a4dcSJakub Kicinski bpf_map__for_each(map, obj) { 9513ff5a4dcSJakub Kicinski if (!strcmp(bpf_map__name(map), map_replace[j].name)) { 9523ff5a4dcSJakub Kicinski map_replace[j].idx = i; 9533ff5a4dcSJakub Kicinski break; 9543ff5a4dcSJakub Kicinski } 9553ff5a4dcSJakub Kicinski i++; 9563ff5a4dcSJakub Kicinski } 9573ff5a4dcSJakub Kicinski if (map_replace[j].idx == -1) { 9583ff5a4dcSJakub Kicinski p_err("unable to find map '%s'", map_replace[j].name); 9593ff5a4dcSJakub Kicinski goto err_close_obj; 9603ff5a4dcSJakub Kicinski } 9613ff5a4dcSJakub Kicinski j++; 9623ff5a4dcSJakub Kicinski } 9633ff5a4dcSJakub Kicinski /* Resort if any names were resolved */ 9643ff5a4dcSJakub Kicinski if (j) 9653ff5a4dcSJakub Kicinski qsort(map_replace, old_map_fds, sizeof(*map_replace), 9663ff5a4dcSJakub Kicinski map_replace_compar); 9673ff5a4dcSJakub Kicinski 9683ff5a4dcSJakub Kicinski /* Set ifindex and name reuse */ 9693ff5a4dcSJakub Kicinski j = 0; 9703ff5a4dcSJakub Kicinski idx = 0; 9713ff5a4dcSJakub Kicinski bpf_map__for_each(map, obj) { 972c8406848SJakub Kicinski if (!bpf_map__is_offload_neutral(map)) 973c8406848SJakub Kicinski bpf_map__set_ifindex(map, ifindex); 974c8406848SJakub Kicinski 9753ff5a4dcSJakub Kicinski if (j < old_map_fds && idx == map_replace[j].idx) { 9763ff5a4dcSJakub Kicinski err = bpf_map__reuse_fd(map, map_replace[j++].fd); 9773ff5a4dcSJakub Kicinski if (err) { 9783ff5a4dcSJakub Kicinski p_err("unable to set up map reuse: %d", err); 9793ff5a4dcSJakub Kicinski goto err_close_obj; 9803ff5a4dcSJakub Kicinski } 9813ff5a4dcSJakub Kicinski 9823ff5a4dcSJakub Kicinski /* Next reuse wants to apply to the same map */ 9833ff5a4dcSJakub Kicinski if (j < old_map_fds && map_replace[j].idx == idx) { 9843ff5a4dcSJakub Kicinski p_err("replacement for map idx %d specified more than once", 9853ff5a4dcSJakub Kicinski idx); 9863ff5a4dcSJakub Kicinski goto err_close_obj; 9873ff5a4dcSJakub Kicinski } 9883ff5a4dcSJakub Kicinski } 9893ff5a4dcSJakub Kicinski 9903ff5a4dcSJakub Kicinski idx++; 9913ff5a4dcSJakub Kicinski } 9923ff5a4dcSJakub Kicinski if (j < old_map_fds) { 9933ff5a4dcSJakub Kicinski p_err("map idx '%d' not used", map_replace[j].idx); 9943ff5a4dcSJakub Kicinski goto err_close_obj; 9953ff5a4dcSJakub Kicinski } 9963ff5a4dcSJakub Kicinski 997c8406848SJakub Kicinski err = bpf_object__load(obj); 998c8406848SJakub Kicinski if (err) { 999c8406848SJakub Kicinski p_err("failed to load object file"); 1000c8406848SJakub Kicinski goto err_close_obj; 1001c8406848SJakub Kicinski } 1002c8406848SJakub Kicinski 1003c8406848SJakub Kicinski if (do_pin_fd(bpf_program__fd(prog), pinfile)) 1004bfee71fbSJakub Kicinski goto err_close_obj; 100549a086c2SRoman Gushchin 100649a086c2SRoman Gushchin if (json_output) 100749a086c2SRoman Gushchin jsonw_null(json_wtr); 100849a086c2SRoman Gushchin 1009bfee71fbSJakub Kicinski bpf_object__close(obj); 10103ff5a4dcSJakub Kicinski for (i = 0; i < old_map_fds; i++) 10113ff5a4dcSJakub Kicinski close(map_replace[i].fd); 10123ff5a4dcSJakub Kicinski free(map_replace); 1013bfee71fbSJakub Kicinski 101449a086c2SRoman Gushchin return 0; 1015bfee71fbSJakub Kicinski 1016bfee71fbSJakub Kicinski err_close_obj: 1017bfee71fbSJakub Kicinski bpf_object__close(obj); 10183ff5a4dcSJakub Kicinski err_free_reuse_maps: 10193ff5a4dcSJakub Kicinski for (i = 0; i < old_map_fds; i++) 10203ff5a4dcSJakub Kicinski close(map_replace[i].fd); 10213ff5a4dcSJakub Kicinski free(map_replace); 1022bfee71fbSJakub Kicinski return -1; 102349a086c2SRoman Gushchin } 102449a086c2SRoman Gushchin 102571bb428fSJakub Kicinski static int do_help(int argc, char **argv) 102671bb428fSJakub Kicinski { 1027004b45c0SQuentin Monnet if (json_output) { 1028004b45c0SQuentin Monnet jsonw_null(json_wtr); 1029004b45c0SQuentin Monnet return 0; 1030004b45c0SQuentin Monnet } 1031004b45c0SQuentin Monnet 103271bb428fSJakub Kicinski fprintf(stderr, 10336ebe6dbdSJakub Kicinski "Usage: %s %s { show | list } [PROG]\n" 1034b6c1cedbSJiong Wang " %s %s dump xlated PROG [{ file FILE | opcodes | visual }]\n" 10358dfbc6d1SQuentin Monnet " %s %s dump jited PROG [{ file FILE | opcodes }]\n" 103671bb428fSJakub Kicinski " %s %s pin PROG FILE\n" 10373ff5a4dcSJakub Kicinski " %s %s load OBJ FILE [type TYPE] [dev NAME] \\\n" 10383ff5a4dcSJakub Kicinski " [map { idx IDX | name NAME } MAP]\n" 1039b7d3826cSJohn Fastabend " %s %s attach PROG ATTACH_TYPE MAP\n" 1040b7d3826cSJohn Fastabend " %s %s detach PROG ATTACH_TYPE MAP\n" 104171bb428fSJakub Kicinski " %s %s help\n" 104271bb428fSJakub Kicinski "\n" 10433ff5a4dcSJakub Kicinski " " HELP_SPEC_MAP "\n" 104471bb428fSJakub Kicinski " " HELP_SPEC_PROGRAM "\n" 104549f2cba3SJakub Kicinski " TYPE := { socket | kprobe | kretprobe | classifier | action |\n" 104649f2cba3SJakub Kicinski " tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n" 104749f2cba3SJakub Kicinski " cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n" 104849f2cba3SJakub Kicinski " lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n" 104949f2cba3SJakub Kicinski " cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n" 105049f2cba3SJakub Kicinski " cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n" 105149f2cba3SJakub Kicinski " cgroup/sendmsg4 | cgroup/sendmsg6 }\n" 1052b7d3826cSJohn Fastabend " ATTACH_TYPE := { msg_verdict | skb_verdict | skb_parse }\n" 10530641c3c8SQuentin Monnet " " HELP_SPEC_OPTIONS "\n" 105471bb428fSJakub Kicinski "", 105571bb428fSJakub Kicinski bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2], 1056b7d3826cSJohn Fastabend bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2], 1057b7d3826cSJohn Fastabend bin_name, argv[-2], bin_name, argv[-2]); 105871bb428fSJakub Kicinski 105971bb428fSJakub Kicinski return 0; 106071bb428fSJakub Kicinski } 106171bb428fSJakub Kicinski 106271bb428fSJakub Kicinski static const struct cmd cmds[] = { 106371bb428fSJakub Kicinski { "show", do_show }, 10646ebe6dbdSJakub Kicinski { "list", do_show }, 10659f606179SQuentin Monnet { "help", do_help }, 106671bb428fSJakub Kicinski { "dump", do_dump }, 106771bb428fSJakub Kicinski { "pin", do_pin }, 106849a086c2SRoman Gushchin { "load", do_load }, 1069b7d3826cSJohn Fastabend { "attach", do_attach }, 1070b7d3826cSJohn Fastabend { "detach", do_detach }, 107171bb428fSJakub Kicinski { 0 } 107271bb428fSJakub Kicinski }; 107371bb428fSJakub Kicinski 107471bb428fSJakub Kicinski int do_prog(int argc, char **argv) 107571bb428fSJakub Kicinski { 107671bb428fSJakub Kicinski return cmd_select(cmds, argc, argv, do_help); 107771bb428fSJakub Kicinski } 1078