netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Roman Gushchin <guro@fb.com>
To: <netdev@vger.kernel.org>
Cc: <linux-kernel@vger.kernel.org>, <kernel-team@fb.com>,
	<ast@kernel.org>, <daniel@iogearbox.net>,
	<jakub.kicinski@netronome.com>, <kafai@fb.com>, <guro@fb.com>
Subject: [PATCH net-next 5/5] bpftool: implement cglist command
Date: Thu, 30 Nov 2017 13:43:02 +0000	[thread overview]
Message-ID: <20171130134302.2840-6-guro@fb.com> (raw)
In-Reply-To: <20171130134302.2840-1-guro@fb.com>

Implement cgattach command to list bpf progrrams attached
to the given cgroup:

Example:
$ ./bpftool cgattach dev_cgroup.o /sys/fs/cgroup/user.slice/ device
$ ./bpftool cglist /sys/fs/cgroup/user.slice/
ID       AttachType      Name
1        device          bpf_prog1

Signed-off-by: Roman Gushchin <guro@fb.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Jakub Kicinski <jakub.kicinski@netronome.com>
Cc: Martin KaFai Lau <kafai@fb.com>
---
 tools/bpf/bpftool/main.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 81 insertions(+), 1 deletion(-)

diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index 77fcc1a0bd5d..8a48f6a32adc 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -82,12 +82,13 @@ static int do_help(int argc, char **argv)
 		"       %s batch file FILE\n"
 		"       %s cgattach FILE CGROUP TYPE\n"
 		"       %s cgdetach CGROUP TYPE ID\n"
+		"       %s cglist CGROUP TYPE\n"
 		"       %s version\n"
 		"\n"
 		"       OBJECT := { prog | map }\n"
 		"       " HELP_SPEC_OPTIONS "\n"
 		"",
-		bin_name, bin_name, bin_name, bin_name, bin_name);
+		bin_name, bin_name, bin_name, bin_name, bin_name, bin_name);
 
 	return 0;
 }
@@ -168,6 +169,7 @@ void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
 static int do_batch(int argc, char **argv);
 static int do_cgattach(int argc, char **argv);
 static int do_cgdetach(int argc, char **argv);
+static int do_cglist(int argc, char **argv);
 
 static const struct cmd cmds[] = {
 	{ "help",	do_help },
@@ -176,6 +178,7 @@ static const struct cmd cmds[] = {
 	{ "map",	do_map },
 	{ "cgattach",	do_cgattach },
 	{ "cgdetach",	do_cgdetach },
+	{ "cglist",	do_cglist },
 	{ "version",	do_version },
 	{ 0 }
 };
@@ -386,6 +389,83 @@ static int do_cgdetach(int argc, char **argv)
 	return 0;
 }
 
+static int do_cglist(int argc, char **argv)
+{
+	enum bpf_attach_type type;
+	int prog_fd, cgroup_fd;
+	__u32 attach_flags;
+	__u32 prog_ids[1024] = {0};
+	__u32 prog_cnt, iter;
+	int ret = -1;
+	struct bpf_prog_info info = {};
+	__u32 info_len = sizeof(info);
+
+	if (argc < 1) {
+		p_err("too few parameters for cglist\n");
+		return -1;
+	} else if (argc > 1) {
+		p_err("too many parameters for cglist\n");
+		return -1;
+	}
+
+	cgroup_fd = open(argv[0], O_RDONLY);
+	if (cgroup_fd < 0) {
+		p_err("can't open cgroup %s\n", argv[1]);
+		return -1;
+	}
+
+	if (json_output)
+		jsonw_start_array(json_wtr);
+
+	for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
+		prog_cnt = ARRAY_SIZE(prog_ids);
+		if (bpf_prog_query(cgroup_fd, type, 0, &attach_flags, prog_ids,
+				   &prog_cnt))
+			continue;
+
+		ret = 0;
+
+		if (prog_cnt == 0)
+			continue;
+
+		if (!json_output)
+			printf("%-8s %-15s %-15s\n", "ID", "AttachType",
+			       "Name");
+
+		for (iter = 0; iter < prog_cnt; iter++) {
+			prog_fd = bpf_prog_get_fd_by_id(prog_ids[iter]);
+			if (prog_fd < 0)
+				continue;
+
+			if (bpf_obj_get_info_by_fd(prog_fd, &info, &info_len)) {
+				close(prog_fd);
+				continue;
+			}
+
+			if (json_output) {
+				jsonw_start_object(json_wtr);
+				jsonw_uint_field(json_wtr, "id", info.id);
+				jsonw_string_field(json_wtr, "attach_type",
+						   attach_type_strings[type]);
+				jsonw_string_field(json_wtr, "name", info.name);
+				jsonw_end_object(json_wtr);
+			} else {
+				printf("%-8u %-15s %-15s\n", info.id,
+				       attach_type_strings[type], info.name);
+			}
+
+			close(prog_fd);
+		}
+	}
+
+	if (json_output)
+		jsonw_end_array(json_wtr);
+
+	close(cgroup_fd);
+
+	return ret;
+}
+
 int main(int argc, char **argv)
 {
 	static const struct option options[] = {
-- 
2.14.3

  parent reply	other threads:[~2017-11-30 13:43 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-30 13:42 [PATCH net-next 0/5] bpftool: cgroup bpf operations Roman Gushchin
2017-11-30 13:42 ` [PATCH net-next 1/5] libbpf: add ability to guess program type based on section name Roman Gushchin
2017-12-01 10:22   ` Quentin Monnet
2017-12-01 22:46     ` Jakub Kicinski
2017-12-04 12:34       ` Roman Gushchin
2017-12-04 13:12         ` Quentin Monnet
2017-12-04 13:22           ` Roman Gushchin
2017-11-30 13:42 ` [PATCH net-next 2/5] libbpf: prefer global symbols as bpf program name source Roman Gushchin
2017-11-30 13:43 ` [PATCH net-next 3/5] bpftool: implement cgattach command Roman Gushchin
2017-11-30 16:17   ` David Ahern
2017-11-30 16:44     ` Roman Gushchin
2017-11-30 16:24   ` David Ahern
2017-12-01  3:13   ` Jakub Kicinski
2017-11-30 13:43 ` [PATCH net-next 4/5] bpftool: implement cgdetach command Roman Gushchin
2017-12-01 10:26   ` Quentin Monnet
2017-11-30 13:43 ` Roman Gushchin [this message]
2017-12-01  3:04 ` [PATCH net-next 0/5] bpftool: cgroup bpf operations Jakub Kicinski
2017-12-01 17:06   ` Roman Gushchin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171130134302.2840-6-guro@fb.com \
    --to=guro@fb.com \
    --cc=ast@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=jakub.kicinski@netronome.com \
    --cc=kafai@fb.com \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).