Skip to content

Commit be6e198

Browse files
jsitnickiAlexei Starovoitov
authored andcommitted
bpftool: Extract helpers for showing link attach type
Code for printing link attach_type is duplicated in a couple of places, and likely will be duplicated for future link types as well. Create helpers to prevent duplication. Suggested-by: Andrii Nakryiko <[email protected]> Signed-off-by: Jakub Sitnicki <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent d60d81a commit be6e198

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

tools/bpf/bpftool/link.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ show_link_header_json(struct bpf_link_info *info, json_writer_t *wtr)
6262
jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
6363
}
6464

65+
static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
66+
{
67+
if (attach_type < ARRAY_SIZE(attach_type_name))
68+
jsonw_string_field(wtr, "attach_type",
69+
attach_type_name[attach_type]);
70+
else
71+
jsonw_uint_field(wtr, "attach_type", attach_type);
72+
}
73+
6574
static int get_prog_info(int prog_id, struct bpf_prog_info *info)
6675
{
6776
__u32 len = sizeof(*info);
@@ -105,22 +114,13 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)
105114
jsonw_uint_field(json_wtr, "prog_type",
106115
prog_info.type);
107116

108-
if (info->tracing.attach_type < ARRAY_SIZE(attach_type_name))
109-
jsonw_string_field(json_wtr, "attach_type",
110-
attach_type_name[info->tracing.attach_type]);
111-
else
112-
jsonw_uint_field(json_wtr, "attach_type",
113-
info->tracing.attach_type);
117+
show_link_attach_type_json(info->tracing.attach_type,
118+
json_wtr);
114119
break;
115120
case BPF_LINK_TYPE_CGROUP:
116121
jsonw_lluint_field(json_wtr, "cgroup_id",
117122
info->cgroup.cgroup_id);
118-
if (info->cgroup.attach_type < ARRAY_SIZE(attach_type_name))
119-
jsonw_string_field(json_wtr, "attach_type",
120-
attach_type_name[info->cgroup.attach_type]);
121-
else
122-
jsonw_uint_field(json_wtr, "attach_type",
123-
info->cgroup.attach_type);
123+
show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
124124
break;
125125
default:
126126
break;
@@ -153,6 +153,14 @@ static void show_link_header_plain(struct bpf_link_info *info)
153153
printf("prog %u ", info->prog_id);
154154
}
155155

156+
static void show_link_attach_type_plain(__u32 attach_type)
157+
{
158+
if (attach_type < ARRAY_SIZE(attach_type_name))
159+
printf("attach_type %s ", attach_type_name[attach_type]);
160+
else
161+
printf("attach_type %u ", attach_type);
162+
}
163+
156164
static int show_link_close_plain(int fd, struct bpf_link_info *info)
157165
{
158166
struct bpf_prog_info prog_info;
@@ -176,19 +184,11 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
176184
else
177185
printf("\n\tprog_type %u ", prog_info.type);
178186

179-
if (info->tracing.attach_type < ARRAY_SIZE(attach_type_name))
180-
printf("attach_type %s ",
181-
attach_type_name[info->tracing.attach_type]);
182-
else
183-
printf("attach_type %u ", info->tracing.attach_type);
187+
show_link_attach_type_plain(info->tracing.attach_type);
184188
break;
185189
case BPF_LINK_TYPE_CGROUP:
186190
printf("\n\tcgroup_id %zu ", (size_t)info->cgroup.cgroup_id);
187-
if (info->cgroup.attach_type < ARRAY_SIZE(attach_type_name))
188-
printf("attach_type %s ",
189-
attach_type_name[info->cgroup.attach_type]);
190-
else
191-
printf("attach_type %u ", info->cgroup.attach_type);
191+
show_link_attach_type_plain(info->cgroup.attach_type);
192192
break;
193193
default:
194194
break;

0 commit comments

Comments
 (0)