^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) 2018 Facebook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/tc_act/tc_bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "bpf/nlattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "main.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "netlink_dumper.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static void xdp_dump_prog_id(struct nlattr **tb, int attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) const char *mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) bool new_json_object)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) if (!tb[attr])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) if (new_json_object)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) NET_START_OBJECT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) NET_DUMP_STR("mode", " %s", mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) NET_DUMP_UINT("id", " id %u", libbpf_nla_getattr_u32(tb[attr]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (new_json_object)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) NET_END_OBJECT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static int do_xdp_dump_one(struct nlattr *attr, unsigned int ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct nlattr *tb[IFLA_XDP_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned char mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (libbpf_nla_parse_nested(tb, IFLA_XDP_MAX, attr, NULL) < 0)
^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) if (!tb[IFLA_XDP_ATTACHED])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) mode = libbpf_nla_getattr_u8(tb[IFLA_XDP_ATTACHED]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (mode == XDP_ATTACHED_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) NET_START_OBJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) NET_DUMP_STR("devname", "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) NET_DUMP_UINT("ifindex", "(%d)", ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (mode == XDP_ATTACHED_MULTI) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) jsonw_name(json_wtr, "multi_attachments");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) jsonw_start_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) xdp_dump_prog_id(tb, IFLA_XDP_SKB_PROG_ID, "generic", true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) xdp_dump_prog_id(tb, IFLA_XDP_DRV_PROG_ID, "driver", true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) xdp_dump_prog_id(tb, IFLA_XDP_HW_PROG_ID, "offload", true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) jsonw_end_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) } else if (mode == XDP_ATTACHED_DRV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) xdp_dump_prog_id(tb, IFLA_XDP_PROG_ID, "driver", false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) } else if (mode == XDP_ATTACHED_SKB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) xdp_dump_prog_id(tb, IFLA_XDP_PROG_ID, "generic", false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) } else if (mode == XDP_ATTACHED_HW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) xdp_dump_prog_id(tb, IFLA_XDP_PROG_ID, "offload", false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) NET_END_OBJECT_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (!tb[IFLA_XDP])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return do_xdp_dump_one(tb[IFLA_XDP], ifinfo->ifi_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) libbpf_nla_getattr_str(tb[IFLA_IFNAME]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int do_bpf_dump_one_act(struct nlattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (libbpf_nla_parse_nested(tb, TCA_ACT_BPF_MAX, attr, NULL) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return -LIBBPF_ERRNO__NLPARSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (!tb[TCA_ACT_BPF_PARMS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return -LIBBPF_ERRNO__NLPARSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) NET_START_OBJECT_NESTED2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (tb[TCA_ACT_BPF_NAME])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) NET_DUMP_STR("name", "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) libbpf_nla_getattr_str(tb[TCA_ACT_BPF_NAME]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (tb[TCA_ACT_BPF_ID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) NET_DUMP_UINT("id", " id %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) libbpf_nla_getattr_u32(tb[TCA_ACT_BPF_ID]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) NET_END_OBJECT_NESTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int do_dump_one_act(struct nlattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct nlattr *tb[TCA_ACT_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (!attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (libbpf_nla_parse_nested(tb, TCA_ACT_MAX, attr, NULL) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return -LIBBPF_ERRNO__NLPARSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (tb[TCA_ACT_KIND] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) strcmp(libbpf_nla_data(tb[TCA_ACT_KIND]), "bpf") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return do_bpf_dump_one_act(tb[TCA_ACT_OPTIONS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int do_bpf_act_dump(struct nlattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int act, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (libbpf_nla_parse_nested(tb, TCA_ACT_MAX_PRIO, attr, NULL) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return -LIBBPF_ERRNO__NLPARSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) NET_START_ARRAY("act", " %s [");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) for (act = 0; act <= TCA_ACT_MAX_PRIO; act++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ret = do_dump_one_act(tb[act]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) NET_END_ARRAY("] ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int do_bpf_filter_dump(struct nlattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct nlattr *tb[TCA_BPF_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (libbpf_nla_parse_nested(tb, TCA_BPF_MAX, attr, NULL) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -LIBBPF_ERRNO__NLPARSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (tb[TCA_BPF_NAME])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) NET_DUMP_STR("name", " %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) libbpf_nla_getattr_str(tb[TCA_BPF_NAME]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (tb[TCA_BPF_ID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) NET_DUMP_UINT("id", " id %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) libbpf_nla_getattr_u32(tb[TCA_BPF_ID]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (tb[TCA_BPF_ACT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ret = do_bpf_act_dump(tb[TCA_BPF_ACT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int do_filter_dump(struct tcmsg *info, struct nlattr **tb, const char *kind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) const char *devname, int ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (tb[TCA_OPTIONS] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) strcmp(libbpf_nla_data(tb[TCA_KIND]), "bpf") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) NET_START_OBJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (devname[0] != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) NET_DUMP_STR("devname", "%s", devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) NET_DUMP_UINT("ifindex", "(%u)", ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) NET_DUMP_STR("kind", " %s", kind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ret = do_bpf_filter_dump(tb[TCA_OPTIONS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) NET_END_OBJECT_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }