Skip to content

Commit 1095ab4

Browse files
committed
bpftool: Implement link show support for tcx
jira LE-1907 Rebuild_History Non-Buildable kernel-5.14.0-427.18.1.el9_4 commit-author Daniel Borkmann <daniel@iogearbox.net> commit e16e6c6 Add support to dump tcx link information to bpftool. This adds a common helper show_link_ifindex_{plain,json}() which can be reused also for other link types. The plain text and json device output is the same format as in bpftool net dump. Below shows an example link dump output along with a cgroup link for comparison: # bpftool link [...] 10: cgroup prog 1977 cgroup_id 1 attach_type cgroup_inet6_post_bind [...] 13: tcx prog 2053 ifindex enp5s0(3) attach_type tcx_ingress 14: tcx prog 2080 ifindex enp5s0(3) attach_type tcx_egress [...] Equivalent json output: # bpftool link --json [...] { "id": 10, "type": "cgroup", "prog_id": 1977, "cgroup_id": 1, "attach_type": "cgroup_inet6_post_bind" }, [...] { "id": 13, "type": "tcx", "prog_id": 2053, "devname": "enp5s0", "ifindex": 3, "attach_type": "tcx_ingress" }, { "id": 14, "type": "tcx", "prog_id": 2080, "devname": "enp5s0", "ifindex": 3, "attach_type": "tcx_egress" } [...] Suggested-by: Yafang Shao <laoar.shao@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: Quentin Monnet <quentin@isovalent.com> Acked-by: Yafang Shao <laoar.shao@gmail.com> Link: https://lore.kernel.org/r/20230816095651.10014-1-daniel@iogearbox.net Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org> (cherry picked from commit e16e6c6) Signed-off-by: Jonathan Maple <jmaple@ciq.com>
1 parent e9b8d48 commit 1095ab4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tools/bpf/bpftool/link.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
150150
jsonw_uint_field(wtr, "attach_type", attach_type);
151151
}
152152

153+
static void show_link_ifindex_json(__u32 ifindex, json_writer_t *wtr)
154+
{
155+
char devname[IF_NAMESIZE] = "(unknown)";
156+
157+
if (ifindex)
158+
if_indextoname(ifindex, devname);
159+
else
160+
snprintf(devname, sizeof(devname), "(detached)");
161+
jsonw_string_field(wtr, "devname", devname);
162+
jsonw_uint_field(wtr, "ifindex", ifindex);
163+
}
164+
153165
static bool is_iter_map_target(const char *target_name)
154166
{
155167
return strcmp(target_name, "bpf_map_elem") == 0 ||
@@ -433,6 +445,10 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)
433445
case BPF_LINK_TYPE_NETFILTER:
434446
netfilter_dump_json(info, json_wtr);
435447
break;
448+
case BPF_LINK_TYPE_TCX:
449+
show_link_ifindex_json(info->tcx.ifindex, json_wtr);
450+
show_link_attach_type_json(info->tcx.attach_type, json_wtr);
451+
break;
436452
case BPF_LINK_TYPE_STRUCT_OPS:
437453
jsonw_uint_field(json_wtr, "map_id",
438454
info->struct_ops.map_id);
@@ -509,6 +525,22 @@ static void show_link_attach_type_plain(__u32 attach_type)
509525
printf("attach_type %u ", attach_type);
510526
}
511527

528+
static void show_link_ifindex_plain(__u32 ifindex)
529+
{
530+
char devname[IF_NAMESIZE * 2] = "(unknown)";
531+
char tmpname[IF_NAMESIZE];
532+
char *ret = NULL;
533+
534+
if (ifindex)
535+
ret = if_indextoname(ifindex, tmpname);
536+
else
537+
snprintf(devname, sizeof(devname), "(detached)");
538+
if (ret)
539+
snprintf(devname, sizeof(devname), "%s(%d)",
540+
tmpname, ifindex);
541+
printf("ifindex %s ", devname);
542+
}
543+
512544
static void show_iter_plain(struct bpf_link_info *info)
513545
{
514546
const char *target_name = u64_to_ptr(info->iter.target_name);
@@ -745,6 +777,11 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
745777
case BPF_LINK_TYPE_NETFILTER:
746778
netfilter_dump_plain(info);
747779
break;
780+
case BPF_LINK_TYPE_TCX:
781+
printf("\n\t");
782+
show_link_ifindex_plain(info->tcx.ifindex);
783+
show_link_attach_type_plain(info->tcx.attach_type);
784+
break;
748785
case BPF_LINK_TYPE_KPROBE_MULTI:
749786
show_kprobe_multi_plain(info);
750787
break;

0 commit comments

Comments
 (0)