diff mbox series

[bpf-next,v2,08/12] bpftool: Support link show for netns-attached links

Message ID 20200531082846.2117903-9-jakub@cloudflare.com
State Accepted
Delegated to: BPF Maintainers
Headers show
Series Link-based program attachment to network namespaces | expand

Commit Message

Jakub Sitnicki May 31, 2020, 8:28 a.m. UTC
Make `bpf link show` aware of new link type, that is links attached to
netns. When listing netns-attached links, display netns inode number as its
identifier and link attach type.

Sample session:

  # readlink /proc/self/ns/net
  net:[4026532251]
  # bpftool prog show
  357: flow_dissector  tag a04f5eef06a7f555  gpl
          loaded_at 2020-05-30T16:53:51+0200  uid 0
          xlated 16B  jited 37B  memlock 4096B
  358: flow_dissector  tag a04f5eef06a7f555  gpl
          loaded_at 2020-05-30T16:53:51+0200  uid 0
          xlated 16B  jited 37B  memlock 4096B
  # bpftool link show
  108: netns  prog 357
          netns_ino 4026532251  attach_type flow_dissector
  # bpftool link -jp show
  [{
          "id": 108,
          "type": "netns",
          "prog_id": 357,
          "netns_ino": 4026532251,
          "attach_type": "flow_dissector"
      }
  ]

  (... after netns is gone ...)

  # bpftool link show
  108: netns  prog 357
          netns_ino 0  attach_type flow_dissector
  # bpftool link -jp show
  [{
          "id": 108,
          "type": "netns",
          "prog_id": 357,
          "netns_ino": 0,
          "attach_type": "flow_dissector"
      }
  ]

Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
---
 tools/bpf/bpftool/link.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

Comments

Andrii Nakryiko June 1, 2020, 10:38 p.m. UTC | #1
On Sun, May 31, 2020 at 1:31 AM Jakub Sitnicki <jakub@cloudflare.com> wrote:
>
> Make `bpf link show` aware of new link type, that is links attached to
> netns. When listing netns-attached links, display netns inode number as its
> identifier and link attach type.
>
> Sample session:
>
>   # readlink /proc/self/ns/net
>   net:[4026532251]
>   # bpftool prog show
>   357: flow_dissector  tag a04f5eef06a7f555  gpl
>           loaded_at 2020-05-30T16:53:51+0200  uid 0
>           xlated 16B  jited 37B  memlock 4096B
>   358: flow_dissector  tag a04f5eef06a7f555  gpl
>           loaded_at 2020-05-30T16:53:51+0200  uid 0
>           xlated 16B  jited 37B  memlock 4096B
>   # bpftool link show
>   108: netns  prog 357
>           netns_ino 4026532251  attach_type flow_dissector
>   # bpftool link -jp show
>   [{
>           "id": 108,
>           "type": "netns",
>           "prog_id": 357,
>           "netns_ino": 4026532251,
>           "attach_type": "flow_dissector"
>       }
>   ]
>
>   (... after netns is gone ...)
>
>   # bpftool link show
>   108: netns  prog 357
>           netns_ino 0  attach_type flow_dissector
>   # bpftool link -jp show
>   [{
>           "id": 108,
>           "type": "netns",
>           "prog_id": 357,
>           "netns_ino": 0,
>           "attach_type": "flow_dissector"
>       }
>   ]
>
> Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
> ---

LGTM. Not sure if bpftool allows to filter progs/maps by type, but
probably we should eventually add link type filtering ("show me only
netns links")...

Acked-by: Andrii Nakryiko <andriin@fb.com>

>  tools/bpf/bpftool/link.c | 10 ++++++++++
>  1 file changed, 10 insertions(+)
>

[...]
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c
index 1ff416eff3d7..fca57ee8fafe 100644
--- a/tools/bpf/bpftool/link.c
+++ b/tools/bpf/bpftool/link.c
@@ -17,6 +17,7 @@  static const char * const link_type_name[] = {
 	[BPF_LINK_TYPE_TRACING]			= "tracing",
 	[BPF_LINK_TYPE_CGROUP]			= "cgroup",
 	[BPF_LINK_TYPE_ITER]			= "iter",
+	[BPF_LINK_TYPE_NETNS]			= "netns",
 };
 
 static int link_parse_fd(int *argc, char ***argv)
@@ -122,6 +123,11 @@  static int show_link_close_json(int fd, struct bpf_link_info *info)
 				   info->cgroup.cgroup_id);
 		show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
 		break;
+	case BPF_LINK_TYPE_NETNS:
+		jsonw_uint_field(json_wtr, "netns_ino",
+				 info->netns.netns_ino);
+		show_link_attach_type_json(info->netns.attach_type, json_wtr);
+		break;
 	default:
 		break;
 	}
@@ -190,6 +196,10 @@  static int show_link_close_plain(int fd, struct bpf_link_info *info)
 		printf("\n\tcgroup_id %zu  ", (size_t)info->cgroup.cgroup_id);
 		show_link_attach_type_plain(info->cgroup.attach_type);
 		break;
+	case BPF_LINK_TYPE_NETNS:
+		printf("\n\tnetns_ino %u  ", info->netns.netns_ino);
+		show_link_attach_type_plain(info->netns.attach_type);
+		break;
 	default:
 		break;
 	}