171bb428fSJakub Kicinski /* 271bb428fSJakub Kicinski * Copyright (C) 2017 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 3471bb428fSJakub Kicinski /* Author: Jakub Kicinski <kubakici@wp.pl> */ 3571bb428fSJakub Kicinski 3671bb428fSJakub Kicinski #include <errno.h> 3771bb428fSJakub Kicinski #include <fcntl.h> 38c9c35995SJakub Kicinski #include <stdarg.h> 3971bb428fSJakub Kicinski #include <stdio.h> 4071bb428fSJakub Kicinski #include <stdlib.h> 4171bb428fSJakub Kicinski #include <string.h> 4271bb428fSJakub Kicinski #include <time.h> 4371bb428fSJakub Kicinski #include <unistd.h> 4471bb428fSJakub Kicinski #include <sys/types.h> 4571bb428fSJakub Kicinski #include <sys/stat.h> 4671bb428fSJakub Kicinski 4771bb428fSJakub Kicinski #include <bpf.h> 48*49a086c2SRoman Gushchin #include <libbpf.h> 4971bb428fSJakub Kicinski 5071bb428fSJakub Kicinski #include "main.h" 51c9c35995SJakub Kicinski #include "disasm.h" 5271bb428fSJakub Kicinski 5371bb428fSJakub Kicinski static const char * const prog_type_name[] = { 5471bb428fSJakub Kicinski [BPF_PROG_TYPE_UNSPEC] = "unspec", 5571bb428fSJakub Kicinski [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter", 5671bb428fSJakub Kicinski [BPF_PROG_TYPE_KPROBE] = "kprobe", 5771bb428fSJakub Kicinski [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls", 5871bb428fSJakub Kicinski [BPF_PROG_TYPE_SCHED_ACT] = "sched_act", 5971bb428fSJakub Kicinski [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint", 6071bb428fSJakub Kicinski [BPF_PROG_TYPE_XDP] = "xdp", 6171bb428fSJakub Kicinski [BPF_PROG_TYPE_PERF_EVENT] = "perf_event", 6271bb428fSJakub Kicinski [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb", 6371bb428fSJakub Kicinski [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock", 6471bb428fSJakub Kicinski [BPF_PROG_TYPE_LWT_IN] = "lwt_in", 6571bb428fSJakub Kicinski [BPF_PROG_TYPE_LWT_OUT] = "lwt_out", 6671bb428fSJakub Kicinski [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit", 6771bb428fSJakub Kicinski [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops", 6871bb428fSJakub Kicinski [BPF_PROG_TYPE_SK_SKB] = "sk_skb", 6971bb428fSJakub Kicinski }; 7071bb428fSJakub Kicinski 7171bb428fSJakub Kicinski static void print_boot_time(__u64 nsecs, char *buf, unsigned int size) 7271bb428fSJakub Kicinski { 7371bb428fSJakub Kicinski struct timespec real_time_ts, boot_time_ts; 7471bb428fSJakub Kicinski time_t wallclock_secs; 7571bb428fSJakub Kicinski struct tm load_tm; 7671bb428fSJakub Kicinski 7771bb428fSJakub Kicinski buf[--size] = '\0'; 7871bb428fSJakub Kicinski 7971bb428fSJakub Kicinski if (clock_gettime(CLOCK_REALTIME, &real_time_ts) || 8071bb428fSJakub Kicinski clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) { 8171bb428fSJakub Kicinski perror("Can't read clocks"); 8271bb428fSJakub Kicinski snprintf(buf, size, "%llu", nsecs / 1000000000); 8371bb428fSJakub Kicinski return; 8471bb428fSJakub Kicinski } 8571bb428fSJakub Kicinski 8671bb428fSJakub Kicinski wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) + 8771bb428fSJakub Kicinski nsecs / 1000000000; 8871bb428fSJakub Kicinski 8971bb428fSJakub Kicinski if (!localtime_r(&wallclock_secs, &load_tm)) { 9071bb428fSJakub Kicinski snprintf(buf, size, "%llu", nsecs / 1000000000); 9171bb428fSJakub Kicinski return; 9271bb428fSJakub Kicinski } 9371bb428fSJakub Kicinski 9471bb428fSJakub Kicinski strftime(buf, size, "%b %d/%H:%M", &load_tm); 9571bb428fSJakub Kicinski } 9671bb428fSJakub Kicinski 9771bb428fSJakub Kicinski static int prog_fd_by_tag(unsigned char *tag) 9871bb428fSJakub Kicinski { 9971bb428fSJakub Kicinski struct bpf_prog_info info = {}; 10071bb428fSJakub Kicinski __u32 len = sizeof(info); 10171bb428fSJakub Kicinski unsigned int id = 0; 10271bb428fSJakub Kicinski int err; 10371bb428fSJakub Kicinski int fd; 10471bb428fSJakub Kicinski 10571bb428fSJakub Kicinski while (true) { 10671bb428fSJakub Kicinski err = bpf_prog_get_next_id(id, &id); 10771bb428fSJakub Kicinski if (err) { 1089a5ab8bfSQuentin Monnet p_err("%s", strerror(errno)); 10971bb428fSJakub Kicinski return -1; 11071bb428fSJakub Kicinski } 11171bb428fSJakub Kicinski 11271bb428fSJakub Kicinski fd = bpf_prog_get_fd_by_id(id); 11371bb428fSJakub Kicinski if (fd < 0) { 1149a5ab8bfSQuentin Monnet p_err("can't get prog by id (%u): %s", 11571bb428fSJakub Kicinski id, strerror(errno)); 11671bb428fSJakub Kicinski return -1; 11771bb428fSJakub Kicinski } 11871bb428fSJakub Kicinski 11971bb428fSJakub Kicinski err = bpf_obj_get_info_by_fd(fd, &info, &len); 12071bb428fSJakub Kicinski if (err) { 1219a5ab8bfSQuentin Monnet p_err("can't get prog info (%u): %s", 12271bb428fSJakub Kicinski id, strerror(errno)); 12371bb428fSJakub Kicinski close(fd); 12471bb428fSJakub Kicinski return -1; 12571bb428fSJakub Kicinski } 12671bb428fSJakub Kicinski 12771bb428fSJakub Kicinski if (!memcmp(tag, info.tag, BPF_TAG_SIZE)) 12871bb428fSJakub Kicinski return fd; 12971bb428fSJakub Kicinski 13071bb428fSJakub Kicinski close(fd); 13171bb428fSJakub Kicinski } 13271bb428fSJakub Kicinski } 13371bb428fSJakub Kicinski 13471bb428fSJakub Kicinski int prog_parse_fd(int *argc, char ***argv) 13571bb428fSJakub Kicinski { 13671bb428fSJakub Kicinski int fd; 13771bb428fSJakub Kicinski 13871bb428fSJakub Kicinski if (is_prefix(**argv, "id")) { 13971bb428fSJakub Kicinski unsigned int id; 14071bb428fSJakub Kicinski char *endptr; 14171bb428fSJakub Kicinski 14271bb428fSJakub Kicinski NEXT_ARGP(); 14371bb428fSJakub Kicinski 14471bb428fSJakub Kicinski id = strtoul(**argv, &endptr, 0); 14571bb428fSJakub Kicinski if (*endptr) { 1469a5ab8bfSQuentin Monnet p_err("can't parse %s as ID", **argv); 14771bb428fSJakub Kicinski return -1; 14871bb428fSJakub Kicinski } 14971bb428fSJakub Kicinski NEXT_ARGP(); 15071bb428fSJakub Kicinski 15171bb428fSJakub Kicinski fd = bpf_prog_get_fd_by_id(id); 15271bb428fSJakub Kicinski if (fd < 0) 1539a5ab8bfSQuentin Monnet p_err("get by id (%u): %s", id, strerror(errno)); 15471bb428fSJakub Kicinski return fd; 15571bb428fSJakub Kicinski } else if (is_prefix(**argv, "tag")) { 15671bb428fSJakub Kicinski unsigned char tag[BPF_TAG_SIZE]; 15771bb428fSJakub Kicinski 15871bb428fSJakub Kicinski NEXT_ARGP(); 15971bb428fSJakub Kicinski 16071bb428fSJakub Kicinski if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2, 16171bb428fSJakub Kicinski tag + 3, tag + 4, tag + 5, tag + 6, tag + 7) 16271bb428fSJakub Kicinski != BPF_TAG_SIZE) { 1639a5ab8bfSQuentin Monnet p_err("can't parse tag"); 16471bb428fSJakub Kicinski return -1; 16571bb428fSJakub Kicinski } 16671bb428fSJakub Kicinski NEXT_ARGP(); 16771bb428fSJakub Kicinski 16871bb428fSJakub Kicinski return prog_fd_by_tag(tag); 16971bb428fSJakub Kicinski } else if (is_prefix(**argv, "pinned")) { 17071bb428fSJakub Kicinski char *path; 17171bb428fSJakub Kicinski 17271bb428fSJakub Kicinski NEXT_ARGP(); 17371bb428fSJakub Kicinski 17471bb428fSJakub Kicinski path = **argv; 17571bb428fSJakub Kicinski NEXT_ARGP(); 17671bb428fSJakub Kicinski 17771bb428fSJakub Kicinski return open_obj_pinned_any(path, BPF_OBJ_PROG); 17871bb428fSJakub Kicinski } 17971bb428fSJakub Kicinski 1809a5ab8bfSQuentin Monnet p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv); 18171bb428fSJakub Kicinski return -1; 18271bb428fSJakub Kicinski } 18371bb428fSJakub Kicinski 18471bb428fSJakub Kicinski static void show_prog_maps(int fd, u32 num_maps) 18571bb428fSJakub Kicinski { 18671bb428fSJakub Kicinski struct bpf_prog_info info = {}; 18771bb428fSJakub Kicinski __u32 len = sizeof(info); 18871bb428fSJakub Kicinski __u32 map_ids[num_maps]; 18971bb428fSJakub Kicinski unsigned int i; 19071bb428fSJakub Kicinski int err; 19171bb428fSJakub Kicinski 19271bb428fSJakub Kicinski info.nr_map_ids = num_maps; 19371bb428fSJakub Kicinski info.map_ids = ptr_to_u64(map_ids); 19471bb428fSJakub Kicinski 19571bb428fSJakub Kicinski err = bpf_obj_get_info_by_fd(fd, &info, &len); 19671bb428fSJakub Kicinski if (err || !info.nr_map_ids) 19771bb428fSJakub Kicinski return; 19871bb428fSJakub Kicinski 199743cc665SQuentin Monnet if (json_output) { 200743cc665SQuentin Monnet jsonw_name(json_wtr, "map_ids"); 201743cc665SQuentin Monnet jsonw_start_array(json_wtr); 202743cc665SQuentin Monnet for (i = 0; i < info.nr_map_ids; i++) 203743cc665SQuentin Monnet jsonw_uint(json_wtr, map_ids[i]); 204743cc665SQuentin Monnet jsonw_end_array(json_wtr); 205743cc665SQuentin Monnet } else { 20671bb428fSJakub Kicinski printf(" map_ids "); 20771bb428fSJakub Kicinski for (i = 0; i < info.nr_map_ids; i++) 20871bb428fSJakub Kicinski printf("%u%s", map_ids[i], 20971bb428fSJakub Kicinski i == info.nr_map_ids - 1 ? "" : ","); 21071bb428fSJakub Kicinski } 21171bb428fSJakub Kicinski } 21271bb428fSJakub Kicinski 213743cc665SQuentin Monnet static void print_prog_json(struct bpf_prog_info *info, int fd) 214743cc665SQuentin Monnet { 215743cc665SQuentin Monnet char *memlock; 216743cc665SQuentin Monnet 217743cc665SQuentin Monnet jsonw_start_object(json_wtr); 218743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "id", info->id); 219743cc665SQuentin Monnet if (info->type < ARRAY_SIZE(prog_type_name)) 220743cc665SQuentin Monnet jsonw_string_field(json_wtr, "type", 221743cc665SQuentin Monnet prog_type_name[info->type]); 22271bb428fSJakub Kicinski else 223743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "type", info->type); 22471bb428fSJakub Kicinski 225743cc665SQuentin Monnet if (*info->name) 226743cc665SQuentin Monnet jsonw_string_field(json_wtr, "name", info->name); 22771bb428fSJakub Kicinski 228743cc665SQuentin Monnet jsonw_name(json_wtr, "tag"); 229743cc665SQuentin Monnet jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"", 230743cc665SQuentin Monnet info->tag[0], info->tag[1], info->tag[2], info->tag[3], 231743cc665SQuentin Monnet info->tag[4], info->tag[5], info->tag[6], info->tag[7]); 23271bb428fSJakub Kicinski 233743cc665SQuentin Monnet if (info->load_time) { 23471bb428fSJakub Kicinski char buf[32]; 23571bb428fSJakub Kicinski 236743cc665SQuentin Monnet print_boot_time(info->load_time, buf, sizeof(buf)); 23771bb428fSJakub Kicinski 23871bb428fSJakub Kicinski /* Piggy back on load_time, since 0 uid is a valid one */ 239743cc665SQuentin Monnet jsonw_string_field(json_wtr, "loaded_at", buf); 240743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "uid", info->created_by_uid); 24171bb428fSJakub Kicinski } 24271bb428fSJakub Kicinski 243743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len); 24471bb428fSJakub Kicinski 245743cc665SQuentin Monnet if (info->jited_prog_len) { 246743cc665SQuentin Monnet jsonw_bool_field(json_wtr, "jited", true); 247743cc665SQuentin Monnet jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len); 248743cc665SQuentin Monnet } else { 249743cc665SQuentin Monnet jsonw_bool_field(json_wtr, "jited", false); 250743cc665SQuentin Monnet } 251743cc665SQuentin Monnet 252743cc665SQuentin Monnet memlock = get_fdinfo(fd, "memlock"); 253743cc665SQuentin Monnet if (memlock) 254743cc665SQuentin Monnet jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock)); 255743cc665SQuentin Monnet free(memlock); 256743cc665SQuentin Monnet 257743cc665SQuentin Monnet if (info->nr_map_ids) 258743cc665SQuentin Monnet show_prog_maps(fd, info->nr_map_ids); 259743cc665SQuentin Monnet 2604990f1f4SPrashant Bhole if (!hash_empty(prog_table.table)) { 2614990f1f4SPrashant Bhole struct pinned_obj *obj; 2624990f1f4SPrashant Bhole 2634990f1f4SPrashant Bhole jsonw_name(json_wtr, "pinned"); 2644990f1f4SPrashant Bhole jsonw_start_array(json_wtr); 2654990f1f4SPrashant Bhole hash_for_each_possible(prog_table.table, obj, hash, info->id) { 2664990f1f4SPrashant Bhole if (obj->id == info->id) 2674990f1f4SPrashant Bhole jsonw_string(json_wtr, obj->path); 2684990f1f4SPrashant Bhole } 2694990f1f4SPrashant Bhole jsonw_end_array(json_wtr); 2704990f1f4SPrashant Bhole } 2714990f1f4SPrashant Bhole 272743cc665SQuentin Monnet jsonw_end_object(json_wtr); 273743cc665SQuentin Monnet } 274743cc665SQuentin Monnet 275743cc665SQuentin Monnet static void print_prog_plain(struct bpf_prog_info *info, int fd) 276743cc665SQuentin Monnet { 277743cc665SQuentin Monnet char *memlock; 278743cc665SQuentin Monnet 279743cc665SQuentin Monnet printf("%u: ", info->id); 280743cc665SQuentin Monnet if (info->type < ARRAY_SIZE(prog_type_name)) 281743cc665SQuentin Monnet printf("%s ", prog_type_name[info->type]); 282743cc665SQuentin Monnet else 283743cc665SQuentin Monnet printf("type %u ", info->type); 284743cc665SQuentin Monnet 285743cc665SQuentin Monnet if (*info->name) 286743cc665SQuentin Monnet printf("name %s ", info->name); 287743cc665SQuentin Monnet 288743cc665SQuentin Monnet printf("tag "); 289743cc665SQuentin Monnet fprint_hex(stdout, info->tag, BPF_TAG_SIZE, ""); 290743cc665SQuentin Monnet printf("\n"); 291743cc665SQuentin Monnet 292743cc665SQuentin Monnet if (info->load_time) { 293743cc665SQuentin Monnet char buf[32]; 294743cc665SQuentin Monnet 295743cc665SQuentin Monnet print_boot_time(info->load_time, buf, sizeof(buf)); 296743cc665SQuentin Monnet 297743cc665SQuentin Monnet /* Piggy back on load_time, since 0 uid is a valid one */ 298743cc665SQuentin Monnet printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid); 299743cc665SQuentin Monnet } 300743cc665SQuentin Monnet 301743cc665SQuentin Monnet printf("\txlated %uB", info->xlated_prog_len); 302743cc665SQuentin Monnet 303743cc665SQuentin Monnet if (info->jited_prog_len) 304743cc665SQuentin Monnet printf(" jited %uB", info->jited_prog_len); 30571bb428fSJakub Kicinski else 30671bb428fSJakub Kicinski printf(" not jited"); 30771bb428fSJakub Kicinski 30871bb428fSJakub Kicinski memlock = get_fdinfo(fd, "memlock"); 30971bb428fSJakub Kicinski if (memlock) 31071bb428fSJakub Kicinski printf(" memlock %sB", memlock); 31171bb428fSJakub Kicinski free(memlock); 31271bb428fSJakub Kicinski 313743cc665SQuentin Monnet if (info->nr_map_ids) 314743cc665SQuentin Monnet show_prog_maps(fd, info->nr_map_ids); 31571bb428fSJakub Kicinski 3164990f1f4SPrashant Bhole if (!hash_empty(prog_table.table)) { 3174990f1f4SPrashant Bhole struct pinned_obj *obj; 3184990f1f4SPrashant Bhole 3194990f1f4SPrashant Bhole printf("\n"); 3204990f1f4SPrashant Bhole hash_for_each_possible(prog_table.table, obj, hash, info->id) { 3214990f1f4SPrashant Bhole if (obj->id == info->id) 3224990f1f4SPrashant Bhole printf("\tpinned %s\n", obj->path); 3234990f1f4SPrashant Bhole } 3244990f1f4SPrashant Bhole } 3254990f1f4SPrashant Bhole 32671bb428fSJakub Kicinski printf("\n"); 327743cc665SQuentin Monnet } 328743cc665SQuentin Monnet 329743cc665SQuentin Monnet static int show_prog(int fd) 330743cc665SQuentin Monnet { 331743cc665SQuentin Monnet struct bpf_prog_info info = {}; 332743cc665SQuentin Monnet __u32 len = sizeof(info); 333743cc665SQuentin Monnet int err; 334743cc665SQuentin Monnet 335743cc665SQuentin Monnet err = bpf_obj_get_info_by_fd(fd, &info, &len); 336743cc665SQuentin Monnet if (err) { 3379a5ab8bfSQuentin Monnet p_err("can't get prog info: %s", strerror(errno)); 338743cc665SQuentin Monnet return -1; 339743cc665SQuentin Monnet } 340743cc665SQuentin Monnet 341743cc665SQuentin Monnet if (json_output) 342743cc665SQuentin Monnet print_prog_json(&info, fd); 343743cc665SQuentin Monnet else 344743cc665SQuentin Monnet print_prog_plain(&info, fd); 34571bb428fSJakub Kicinski 34671bb428fSJakub Kicinski return 0; 34771bb428fSJakub Kicinski } 34871bb428fSJakub Kicinski 34971bb428fSJakub Kicinski static int do_show(int argc, char **argv) 350743cc665SQuentin Monnet { 351743cc665SQuentin Monnet __u32 id = 0; 35271bb428fSJakub Kicinski int err; 35371bb428fSJakub Kicinski int fd; 35471bb428fSJakub Kicinski 355c541b734SPrashant Bhole if (show_pinned) 3564990f1f4SPrashant Bhole build_pinned_obj_table(&prog_table, BPF_OBJ_PROG); 3574990f1f4SPrashant Bhole 35871bb428fSJakub Kicinski if (argc == 2) { 35971bb428fSJakub Kicinski fd = prog_parse_fd(&argc, &argv); 36071bb428fSJakub Kicinski if (fd < 0) 36171bb428fSJakub Kicinski return -1; 36271bb428fSJakub Kicinski 36371bb428fSJakub Kicinski return show_prog(fd); 36471bb428fSJakub Kicinski } 36571bb428fSJakub Kicinski 36671bb428fSJakub Kicinski if (argc) 36771bb428fSJakub Kicinski return BAD_ARG(); 36871bb428fSJakub Kicinski 369743cc665SQuentin Monnet if (json_output) 370743cc665SQuentin Monnet jsonw_start_array(json_wtr); 37171bb428fSJakub Kicinski while (true) { 37271bb428fSJakub Kicinski err = bpf_prog_get_next_id(id, &id); 37371bb428fSJakub Kicinski if (err) { 3741739c26dSQuentin Monnet if (errno == ENOENT) { 3751739c26dSQuentin Monnet err = 0; 37671bb428fSJakub Kicinski break; 3771739c26dSQuentin Monnet } 3789a5ab8bfSQuentin Monnet p_err("can't get next program: %s%s", strerror(errno), 3799a5ab8bfSQuentin Monnet errno == EINVAL ? " -- kernel too old?" : ""); 380743cc665SQuentin Monnet err = -1; 381743cc665SQuentin Monnet break; 38271bb428fSJakub Kicinski } 38371bb428fSJakub Kicinski 38471bb428fSJakub Kicinski fd = bpf_prog_get_fd_by_id(id); 38571bb428fSJakub Kicinski if (fd < 0) { 3869a5ab8bfSQuentin Monnet p_err("can't get prog by id (%u): %s", 38771bb428fSJakub Kicinski id, strerror(errno)); 388743cc665SQuentin Monnet err = -1; 389743cc665SQuentin Monnet break; 39071bb428fSJakub Kicinski } 39171bb428fSJakub Kicinski 39271bb428fSJakub Kicinski err = show_prog(fd); 39371bb428fSJakub Kicinski close(fd); 39471bb428fSJakub Kicinski if (err) 395743cc665SQuentin Monnet break; 39671bb428fSJakub Kicinski } 39771bb428fSJakub Kicinski 398743cc665SQuentin Monnet if (json_output) 399743cc665SQuentin Monnet jsonw_end_array(json_wtr); 400743cc665SQuentin Monnet 401743cc665SQuentin Monnet return err; 40271bb428fSJakub Kicinski } 40371bb428fSJakub Kicinski 404c9c35995SJakub Kicinski static void print_insn(struct bpf_verifier_env *env, const char *fmt, ...) 405c9c35995SJakub Kicinski { 406c9c35995SJakub Kicinski va_list args; 407c9c35995SJakub Kicinski 408c9c35995SJakub Kicinski va_start(args, fmt); 409c9c35995SJakub Kicinski vprintf(fmt, args); 410c9c35995SJakub Kicinski va_end(args); 411c9c35995SJakub Kicinski } 412c9c35995SJakub Kicinski 413f05e2c32SQuentin Monnet static void dump_xlated_plain(void *buf, unsigned int len, bool opcodes) 414c9c35995SJakub Kicinski { 415c9c35995SJakub Kicinski struct bpf_insn *insn = buf; 4169e2308c1SQuentin Monnet bool double_insn = false; 417c9c35995SJakub Kicinski unsigned int i; 418c9c35995SJakub Kicinski 419c9c35995SJakub Kicinski for (i = 0; i < len / sizeof(*insn); i++) { 4209e2308c1SQuentin Monnet if (double_insn) { 4219e2308c1SQuentin Monnet double_insn = false; 4229e2308c1SQuentin Monnet continue; 4239e2308c1SQuentin Monnet } 4249e2308c1SQuentin Monnet 4259e2308c1SQuentin Monnet double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW); 4269e2308c1SQuentin Monnet 427c9c35995SJakub Kicinski printf("% 4d: ", i); 428c9c35995SJakub Kicinski print_bpf_insn(print_insn, NULL, insn + i, true); 429c9c35995SJakub Kicinski 430c9c35995SJakub Kicinski if (opcodes) { 431c9c35995SJakub Kicinski printf(" "); 4329cbe1f58SQuentin Monnet fprint_hex(stdout, insn + i, 8, " "); 4339e2308c1SQuentin Monnet if (double_insn && i < len - 1) { 4349e2308c1SQuentin Monnet printf(" "); 4359e2308c1SQuentin Monnet fprint_hex(stdout, insn + i + 1, 8, " "); 4369e2308c1SQuentin Monnet } 437c9c35995SJakub Kicinski printf("\n"); 438c9c35995SJakub Kicinski } 439c9c35995SJakub Kicinski } 440c9c35995SJakub Kicinski } 441c9c35995SJakub Kicinski 442f05e2c32SQuentin Monnet static void print_insn_json(struct bpf_verifier_env *env, const char *fmt, ...) 443f05e2c32SQuentin Monnet { 444f05e2c32SQuentin Monnet unsigned int l = strlen(fmt); 445f05e2c32SQuentin Monnet char chomped_fmt[l]; 446f05e2c32SQuentin Monnet va_list args; 447f05e2c32SQuentin Monnet 448f05e2c32SQuentin Monnet va_start(args, fmt); 449f05e2c32SQuentin Monnet if (l > 0) { 450f05e2c32SQuentin Monnet strncpy(chomped_fmt, fmt, l - 1); 451f05e2c32SQuentin Monnet chomped_fmt[l - 1] = '\0'; 452f05e2c32SQuentin Monnet } 453f05e2c32SQuentin Monnet jsonw_vprintf_enquote(json_wtr, chomped_fmt, args); 454f05e2c32SQuentin Monnet va_end(args); 455f05e2c32SQuentin Monnet } 456f05e2c32SQuentin Monnet 457f05e2c32SQuentin Monnet static void dump_xlated_json(void *buf, unsigned int len, bool opcodes) 458f05e2c32SQuentin Monnet { 459f05e2c32SQuentin Monnet struct bpf_insn *insn = buf; 460f05e2c32SQuentin Monnet bool double_insn = false; 461f05e2c32SQuentin Monnet unsigned int i; 462f05e2c32SQuentin Monnet 463f05e2c32SQuentin Monnet jsonw_start_array(json_wtr); 464f05e2c32SQuentin Monnet for (i = 0; i < len / sizeof(*insn); i++) { 465f05e2c32SQuentin Monnet if (double_insn) { 466f05e2c32SQuentin Monnet double_insn = false; 467f05e2c32SQuentin Monnet continue; 468f05e2c32SQuentin Monnet } 469f05e2c32SQuentin Monnet double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW); 470f05e2c32SQuentin Monnet 471f05e2c32SQuentin Monnet jsonw_start_object(json_wtr); 472f05e2c32SQuentin Monnet jsonw_name(json_wtr, "disasm"); 473f05e2c32SQuentin Monnet print_bpf_insn(print_insn_json, NULL, insn + i, true); 474f05e2c32SQuentin Monnet 475f05e2c32SQuentin Monnet if (opcodes) { 476f05e2c32SQuentin Monnet jsonw_name(json_wtr, "opcodes"); 477f05e2c32SQuentin Monnet jsonw_start_object(json_wtr); 478f05e2c32SQuentin Monnet 479f05e2c32SQuentin Monnet jsonw_name(json_wtr, "code"); 480f05e2c32SQuentin Monnet jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code); 481f05e2c32SQuentin Monnet 482f05e2c32SQuentin Monnet jsonw_name(json_wtr, "src_reg"); 483f05e2c32SQuentin Monnet jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg); 484f05e2c32SQuentin Monnet 485f05e2c32SQuentin Monnet jsonw_name(json_wtr, "dst_reg"); 486f05e2c32SQuentin Monnet jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg); 487f05e2c32SQuentin Monnet 488f05e2c32SQuentin Monnet jsonw_name(json_wtr, "off"); 489f05e2c32SQuentin Monnet print_hex_data_json((uint8_t *)(&insn[i].off), 2); 490f05e2c32SQuentin Monnet 491f05e2c32SQuentin Monnet jsonw_name(json_wtr, "imm"); 492f05e2c32SQuentin Monnet if (double_insn && i < len - 1) 493f05e2c32SQuentin Monnet print_hex_data_json((uint8_t *)(&insn[i].imm), 494f05e2c32SQuentin Monnet 12); 495f05e2c32SQuentin Monnet else 496f05e2c32SQuentin Monnet print_hex_data_json((uint8_t *)(&insn[i].imm), 497f05e2c32SQuentin Monnet 4); 498f05e2c32SQuentin Monnet jsonw_end_object(json_wtr); 499f05e2c32SQuentin Monnet } 500f05e2c32SQuentin Monnet jsonw_end_object(json_wtr); 501f05e2c32SQuentin Monnet } 502f05e2c32SQuentin Monnet jsonw_end_array(json_wtr); 503f05e2c32SQuentin Monnet } 504f05e2c32SQuentin Monnet 50571bb428fSJakub Kicinski static int do_dump(int argc, char **argv) 50671bb428fSJakub Kicinski { 50771bb428fSJakub Kicinski struct bpf_prog_info info = {}; 50871bb428fSJakub Kicinski __u32 len = sizeof(info); 50971bb428fSJakub Kicinski unsigned int buf_size; 51071bb428fSJakub Kicinski char *filepath = NULL; 51171bb428fSJakub Kicinski bool opcodes = false; 51271bb428fSJakub Kicinski unsigned char *buf; 51371bb428fSJakub Kicinski __u32 *member_len; 51471bb428fSJakub Kicinski __u64 *member_ptr; 51571bb428fSJakub Kicinski ssize_t n; 51671bb428fSJakub Kicinski int err; 51771bb428fSJakub Kicinski int fd; 51871bb428fSJakub Kicinski 51971bb428fSJakub Kicinski if (is_prefix(*argv, "jited")) { 52071bb428fSJakub Kicinski member_len = &info.jited_prog_len; 52171bb428fSJakub Kicinski member_ptr = &info.jited_prog_insns; 52271bb428fSJakub Kicinski } else if (is_prefix(*argv, "xlated")) { 52371bb428fSJakub Kicinski member_len = &info.xlated_prog_len; 52471bb428fSJakub Kicinski member_ptr = &info.xlated_prog_insns; 52571bb428fSJakub Kicinski } else { 5269a5ab8bfSQuentin Monnet p_err("expected 'xlated' or 'jited', got: %s", *argv); 52771bb428fSJakub Kicinski return -1; 52871bb428fSJakub Kicinski } 52971bb428fSJakub Kicinski NEXT_ARG(); 53071bb428fSJakub Kicinski 53171bb428fSJakub Kicinski if (argc < 2) 53271bb428fSJakub Kicinski usage(); 53371bb428fSJakub Kicinski 53471bb428fSJakub Kicinski fd = prog_parse_fd(&argc, &argv); 53571bb428fSJakub Kicinski if (fd < 0) 53671bb428fSJakub Kicinski return -1; 53771bb428fSJakub Kicinski 53871bb428fSJakub Kicinski if (is_prefix(*argv, "file")) { 53971bb428fSJakub Kicinski NEXT_ARG(); 54071bb428fSJakub Kicinski if (!argc) { 5419a5ab8bfSQuentin Monnet p_err("expected file path"); 54271bb428fSJakub Kicinski return -1; 54371bb428fSJakub Kicinski } 54471bb428fSJakub Kicinski 54571bb428fSJakub Kicinski filepath = *argv; 54671bb428fSJakub Kicinski NEXT_ARG(); 54771bb428fSJakub Kicinski } else if (is_prefix(*argv, "opcodes")) { 54871bb428fSJakub Kicinski opcodes = true; 54971bb428fSJakub Kicinski NEXT_ARG(); 55071bb428fSJakub Kicinski } 55171bb428fSJakub Kicinski 55271bb428fSJakub Kicinski if (argc) { 55371bb428fSJakub Kicinski usage(); 55471bb428fSJakub Kicinski return -1; 55571bb428fSJakub Kicinski } 55671bb428fSJakub Kicinski 55771bb428fSJakub Kicinski err = bpf_obj_get_info_by_fd(fd, &info, &len); 55871bb428fSJakub Kicinski if (err) { 5599a5ab8bfSQuentin Monnet p_err("can't get prog info: %s", strerror(errno)); 56071bb428fSJakub Kicinski return -1; 56171bb428fSJakub Kicinski } 56271bb428fSJakub Kicinski 56371bb428fSJakub Kicinski if (!*member_len) { 5649a5ab8bfSQuentin Monnet p_info("no instructions returned"); 56571bb428fSJakub Kicinski close(fd); 56671bb428fSJakub Kicinski return 0; 56771bb428fSJakub Kicinski } 56871bb428fSJakub Kicinski 56971bb428fSJakub Kicinski buf_size = *member_len; 57071bb428fSJakub Kicinski 57171bb428fSJakub Kicinski buf = malloc(buf_size); 57271bb428fSJakub Kicinski if (!buf) { 5739a5ab8bfSQuentin Monnet p_err("mem alloc failed"); 57471bb428fSJakub Kicinski close(fd); 57571bb428fSJakub Kicinski return -1; 57671bb428fSJakub Kicinski } 57771bb428fSJakub Kicinski 57871bb428fSJakub Kicinski memset(&info, 0, sizeof(info)); 57971bb428fSJakub Kicinski 58071bb428fSJakub Kicinski *member_ptr = ptr_to_u64(buf); 58171bb428fSJakub Kicinski *member_len = buf_size; 58271bb428fSJakub Kicinski 58371bb428fSJakub Kicinski err = bpf_obj_get_info_by_fd(fd, &info, &len); 58471bb428fSJakub Kicinski close(fd); 58571bb428fSJakub Kicinski if (err) { 5869a5ab8bfSQuentin Monnet p_err("can't get prog info: %s", strerror(errno)); 58771bb428fSJakub Kicinski goto err_free; 58871bb428fSJakub Kicinski } 58971bb428fSJakub Kicinski 59071bb428fSJakub Kicinski if (*member_len > buf_size) { 5919a5ab8bfSQuentin Monnet p_err("too many instructions returned"); 59271bb428fSJakub Kicinski goto err_free; 59371bb428fSJakub Kicinski } 59471bb428fSJakub Kicinski 59571bb428fSJakub Kicinski if (filepath) { 59671bb428fSJakub Kicinski fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600); 59771bb428fSJakub Kicinski if (fd < 0) { 5989a5ab8bfSQuentin Monnet p_err("can't open file %s: %s", filepath, 59971bb428fSJakub Kicinski strerror(errno)); 60071bb428fSJakub Kicinski goto err_free; 60171bb428fSJakub Kicinski } 60271bb428fSJakub Kicinski 60371bb428fSJakub Kicinski n = write(fd, buf, *member_len); 60471bb428fSJakub Kicinski close(fd); 60571bb428fSJakub Kicinski if (n != *member_len) { 6069a5ab8bfSQuentin Monnet p_err("error writing output file: %s", 60771bb428fSJakub Kicinski n < 0 ? strerror(errno) : "short write"); 60871bb428fSJakub Kicinski goto err_free; 60971bb428fSJakub Kicinski } 61071bb428fSJakub Kicinski } else { 611c9c35995SJakub Kicinski if (member_len == &info.jited_prog_len) 61271bb428fSJakub Kicinski disasm_print_insn(buf, *member_len, opcodes); 613c9c35995SJakub Kicinski else 614f05e2c32SQuentin Monnet if (json_output) 615f05e2c32SQuentin Monnet dump_xlated_json(buf, *member_len, opcodes); 616f05e2c32SQuentin Monnet else 617f05e2c32SQuentin Monnet dump_xlated_plain(buf, *member_len, opcodes); 61871bb428fSJakub Kicinski } 61971bb428fSJakub Kicinski 62071bb428fSJakub Kicinski free(buf); 62171bb428fSJakub Kicinski 62271bb428fSJakub Kicinski return 0; 62371bb428fSJakub Kicinski 62471bb428fSJakub Kicinski err_free: 62571bb428fSJakub Kicinski free(buf); 62671bb428fSJakub Kicinski return -1; 62771bb428fSJakub Kicinski } 62871bb428fSJakub Kicinski 62971bb428fSJakub Kicinski static int do_pin(int argc, char **argv) 63071bb428fSJakub Kicinski { 631004b45c0SQuentin Monnet int err; 632004b45c0SQuentin Monnet 633004b45c0SQuentin Monnet err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id); 634004b45c0SQuentin Monnet if (!err && json_output) 635004b45c0SQuentin Monnet jsonw_null(json_wtr); 636004b45c0SQuentin Monnet return err; 63771bb428fSJakub Kicinski } 63871bb428fSJakub Kicinski 639*49a086c2SRoman Gushchin static int do_load(int argc, char **argv) 640*49a086c2SRoman Gushchin { 641*49a086c2SRoman Gushchin struct bpf_object *obj; 642*49a086c2SRoman Gushchin int prog_fd; 643*49a086c2SRoman Gushchin 644*49a086c2SRoman Gushchin if (argc != 2) 645*49a086c2SRoman Gushchin usage(); 646*49a086c2SRoman Gushchin 647*49a086c2SRoman Gushchin if (bpf_prog_load(argv[0], BPF_PROG_TYPE_UNSPEC, &obj, &prog_fd)) { 648*49a086c2SRoman Gushchin p_err("failed to load program\n"); 649*49a086c2SRoman Gushchin return -1; 650*49a086c2SRoman Gushchin } 651*49a086c2SRoman Gushchin 652*49a086c2SRoman Gushchin if (do_pin_fd(prog_fd, argv[1])) { 653*49a086c2SRoman Gushchin p_err("failed to pin program\n"); 654*49a086c2SRoman Gushchin return -1; 655*49a086c2SRoman Gushchin } 656*49a086c2SRoman Gushchin 657*49a086c2SRoman Gushchin if (json_output) 658*49a086c2SRoman Gushchin jsonw_null(json_wtr); 659*49a086c2SRoman Gushchin 660*49a086c2SRoman Gushchin return 0; 661*49a086c2SRoman Gushchin } 662*49a086c2SRoman Gushchin 66371bb428fSJakub Kicinski static int do_help(int argc, char **argv) 66471bb428fSJakub Kicinski { 665004b45c0SQuentin Monnet if (json_output) { 666004b45c0SQuentin Monnet jsonw_null(json_wtr); 667004b45c0SQuentin Monnet return 0; 668004b45c0SQuentin Monnet } 669004b45c0SQuentin Monnet 67071bb428fSJakub Kicinski fprintf(stderr, 67171bb428fSJakub Kicinski "Usage: %s %s show [PROG]\n" 6728dfbc6d1SQuentin Monnet " %s %s dump xlated PROG [{ file FILE | opcodes }]\n" 6738dfbc6d1SQuentin Monnet " %s %s dump jited PROG [{ file FILE | opcodes }]\n" 67471bb428fSJakub Kicinski " %s %s pin PROG FILE\n" 675*49a086c2SRoman Gushchin " %s %s load OBJ FILE\n" 67671bb428fSJakub Kicinski " %s %s help\n" 67771bb428fSJakub Kicinski "\n" 67871bb428fSJakub Kicinski " " HELP_SPEC_PROGRAM "\n" 6790641c3c8SQuentin Monnet " " HELP_SPEC_OPTIONS "\n" 68071bb428fSJakub Kicinski "", 68171bb428fSJakub Kicinski bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2], 682*49a086c2SRoman Gushchin bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]); 68371bb428fSJakub Kicinski 68471bb428fSJakub Kicinski return 0; 68571bb428fSJakub Kicinski } 68671bb428fSJakub Kicinski 68771bb428fSJakub Kicinski static const struct cmd cmds[] = { 68871bb428fSJakub Kicinski { "show", do_show }, 6899f606179SQuentin Monnet { "help", do_help }, 69071bb428fSJakub Kicinski { "dump", do_dump }, 69171bb428fSJakub Kicinski { "pin", do_pin }, 692*49a086c2SRoman Gushchin { "load", do_load }, 69371bb428fSJakub Kicinski { 0 } 69471bb428fSJakub Kicinski }; 69571bb428fSJakub Kicinski 69671bb428fSJakub Kicinski int do_prog(int argc, char **argv) 69771bb428fSJakub Kicinski { 69871bb428fSJakub Kicinski return cmd_select(cmds, argc, argv, do_help); 69971bb428fSJakub Kicinski } 700