^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright (C) 2020 Facebook */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <net/if.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "json_writer.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "main.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static const char * const link_type_name[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) [BPF_LINK_TYPE_UNSPEC] = "unspec",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) [BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) [BPF_LINK_TYPE_TRACING] = "tracing",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) [BPF_LINK_TYPE_CGROUP] = "cgroup",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) [BPF_LINK_TYPE_ITER] = "iter",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) [BPF_LINK_TYPE_NETNS] = "netns",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int link_parse_fd(int *argc, char ***argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (is_prefix(**argv, "id")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) char *endptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) NEXT_ARGP();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) id = strtoul(**argv, &endptr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (*endptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) p_err("can't parse %s as ID", **argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) NEXT_ARGP();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) fd = bpf_link_get_fd_by_id(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) p_err("failed to get link with ID %d: %s", id, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) } else if (is_prefix(**argv, "pinned")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) char *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) NEXT_ARGP();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) path = **argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) NEXT_ARGP();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return open_obj_pinned_any(path, BPF_OBJ_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) show_link_header_json(struct bpf_link_info *info, json_writer_t *wtr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) jsonw_uint_field(wtr, "id", info->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (info->type < ARRAY_SIZE(link_type_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) jsonw_string_field(wtr, "type", link_type_name[info->type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) jsonw_uint_field(wtr, "type", info->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (attach_type < ARRAY_SIZE(attach_type_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) jsonw_string_field(wtr, "attach_type",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) attach_type_name[attach_type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) jsonw_uint_field(wtr, "attach_type", attach_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static bool is_iter_map_target(const char *target_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return strcmp(target_name, "bpf_map_elem") == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) strcmp(target_name, "bpf_sk_storage_map") == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) const char *target_name = u64_to_ptr(info->iter.target_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) jsonw_string_field(wtr, "target_name", target_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (is_iter_map_target(target_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) jsonw_uint_field(wtr, "map_id", info->iter.map.map_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static int get_prog_info(int prog_id, struct bpf_prog_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) __u32 len = sizeof(*info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int err, prog_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) prog_fd = bpf_prog_get_fd_by_id(prog_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (prog_fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return prog_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) memset(info, 0, sizeof(*info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) err = bpf_obj_get_info_by_fd(prog_fd, info, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) p_err("can't get prog info: %s", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) close(prog_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static int show_link_close_json(int fd, struct bpf_link_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct bpf_prog_info prog_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) jsonw_start_object(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) show_link_header_json(info, json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) switch (info->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) case BPF_LINK_TYPE_RAW_TRACEPOINT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) jsonw_string_field(json_wtr, "tp_name",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) u64_to_ptr(info->raw_tracepoint.tp_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) case BPF_LINK_TYPE_TRACING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) err = get_prog_info(info->prog_id, &prog_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (prog_info.type < prog_type_name_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) jsonw_string_field(json_wtr, "prog_type",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) prog_type_name[prog_info.type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) jsonw_uint_field(json_wtr, "prog_type",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) prog_info.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) show_link_attach_type_json(info->tracing.attach_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) case BPF_LINK_TYPE_CGROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) jsonw_lluint_field(json_wtr, "cgroup_id",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) info->cgroup.cgroup_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) case BPF_LINK_TYPE_ITER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) show_iter_json(info, json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) case BPF_LINK_TYPE_NETNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) jsonw_uint_field(json_wtr, "netns_ino",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) info->netns.netns_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) show_link_attach_type_json(info->netns.attach_type, json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!hash_empty(link_table.table)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct pinned_obj *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) jsonw_name(json_wtr, "pinned");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) jsonw_start_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) hash_for_each_possible(link_table.table, obj, hash, info->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (obj->id == info->id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) jsonw_string(json_wtr, obj->path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) jsonw_end_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) emit_obj_refs_json(&refs_table, info->id, json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) jsonw_end_object(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static void show_link_header_plain(struct bpf_link_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) printf("%u: ", info->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (info->type < ARRAY_SIZE(link_type_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) printf("%s ", link_type_name[info->type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) printf("type %u ", info->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) printf("prog %u ", info->prog_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static void show_link_attach_type_plain(__u32 attach_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (attach_type < ARRAY_SIZE(attach_type_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) printf("attach_type %s ", attach_type_name[attach_type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) printf("attach_type %u ", attach_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static void show_iter_plain(struct bpf_link_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) const char *target_name = u64_to_ptr(info->iter.target_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) printf("target_name %s ", target_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (is_iter_map_target(target_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) printf("map_id %u ", info->iter.map.map_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static int show_link_close_plain(int fd, struct bpf_link_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct bpf_prog_info prog_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) show_link_header_plain(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) switch (info->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) case BPF_LINK_TYPE_RAW_TRACEPOINT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) printf("\n\ttp '%s' ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) (const char *)u64_to_ptr(info->raw_tracepoint.tp_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) case BPF_LINK_TYPE_TRACING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) err = get_prog_info(info->prog_id, &prog_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (prog_info.type < prog_type_name_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) printf("\n\tprog_type %s ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) prog_type_name[prog_info.type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) printf("\n\tprog_type %u ", prog_info.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) show_link_attach_type_plain(info->tracing.attach_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) case BPF_LINK_TYPE_CGROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) printf("\n\tcgroup_id %zu ", (size_t)info->cgroup.cgroup_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) show_link_attach_type_plain(info->cgroup.attach_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) case BPF_LINK_TYPE_ITER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) show_iter_plain(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) case BPF_LINK_TYPE_NETNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) printf("\n\tnetns_ino %u ", info->netns.netns_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) show_link_attach_type_plain(info->netns.attach_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (!hash_empty(link_table.table)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct pinned_obj *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) hash_for_each_possible(link_table.table, obj, hash, info->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (obj->id == info->id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) printf("\n\tpinned %s", obj->path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) emit_obj_refs_plain(&refs_table, info->id, "\n\tpids ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static int do_show_link(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct bpf_link_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) __u32 len = sizeof(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) char buf[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) memset(&info, 0, sizeof(info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) err = bpf_obj_get_info_by_fd(fd, &info, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) p_err("can't get link info: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) !info.raw_tracepoint.tp_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) info.raw_tracepoint.tp_name = (unsigned long)&buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) info.raw_tracepoint.tp_name_len = sizeof(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (info.type == BPF_LINK_TYPE_ITER &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) !info.iter.target_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) info.iter.target_name = (unsigned long)&buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) info.iter.target_name_len = sizeof(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) show_link_close_json(fd, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) show_link_close_plain(fd, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static int do_show(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) __u32 id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) int err, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (show_pinned)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) build_pinned_obj_table(&link_table, BPF_OBJ_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (argc == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) fd = link_parse_fd(&argc, &argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return do_show_link(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (argc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return BAD_ARG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) jsonw_start_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) err = bpf_link_get_next_id(id, &id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (errno == ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) p_err("can't get next link: %s%s", strerror(errno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) errno == EINVAL ? " -- kernel too old?" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) fd = bpf_link_get_fd_by_id(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (errno == ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) p_err("can't get link by id (%u): %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) id, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) err = do_show_link(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) jsonw_end_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) delete_obj_refs_table(&refs_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return errno == ENOENT ? 0 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static int do_pin(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) err = do_pin_any(argc, argv, link_parse_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (!err && json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) jsonw_null(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static int do_detach(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) int err, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (argc != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) p_err("link specifier is invalid or missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) fd = link_parse_fd(&argc, &argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) err = bpf_link_detach(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) p_err("failed link detach: %s", strerror(-err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) jsonw_null(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static int do_help(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) jsonw_null(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) "Usage: %1$s %2$s { show | list } [LINK]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) " %1$s %2$s pin LINK FILE\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) " %1$s %2$s detach LINK\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) " %1$s %2$s help\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) " " HELP_SPEC_LINK "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) " " HELP_SPEC_OPTIONS "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) bin_name, argv[-2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static const struct cmd cmds[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) { "show", do_show },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) { "list", do_show },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) { "help", do_help },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) { "pin", do_pin },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) { "detach", do_detach },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) { 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) int do_link(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return cmd_select(cmds, argc, argv, do_help);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }