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