diff mbox series

[net-next,3/5] bpftool: implement cgattach command

Message ID 20171130134302.2840-4-guro@fb.com
State Changes Requested, archived
Delegated to: BPF Maintainers
Headers show
Series bpftool: cgroup bpf operations | expand

Commit Message

Roman Gushchin Nov. 30, 2017, 1:43 p.m. UTC
This patch add the cgattach command to bpftool.
It allows to load a bpf program from a binary file and attach
it to a given cgroup.

Example:
$ bpftool cgattach ./mybpfprog.o /sys/fs/cgroup/user.slice/ ingress

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 | 81 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 80 insertions(+), 1 deletion(-)

Comments

David Ahern Nov. 30, 2017, 4:17 p.m. UTC | #1
On 11/30/17 6:43 AM, Roman Gushchin wrote:
> @@ -75,12 +80,13 @@ static int do_help(int argc, char **argv)
>  	fprintf(stderr,
>  		"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
>  		"       %s batch file FILE\n"
> +		"       %s cgattach FILE CGROUP TYPE\n"

Can you change the order to:
+		"       %s cgattach CGROUP TYPE FILE\n"

Makes for better consistency with the detach command in the next patch:
+		"       %s cgdetach CGROUP TYPE ID\n"
David Ahern Nov. 30, 2017, 4:24 p.m. UTC | #2
On 11/30/17 6:43 AM, Roman Gushchin wrote:
> +	if (bpf_prog_attach(prog_fd, cgroup_fd, attach_type, 0)) {
> +		bpf_object__close(obj);
> +		close(prog_fd);
> +		close(cgroup_fd);
> +		p_err("Failed to attach program");
> +		return -1;
> +	}


Also, what about support for BPF_F_ALLOW_OVERRIDE and BPF_F_ALLOW_MULTI
flags?
Roman Gushchin Nov. 30, 2017, 4:44 p.m. UTC | #3
On Thu, Nov 30, 2017 at 09:17:17AM -0700, David Ahern wrote:
> On 11/30/17 6:43 AM, Roman Gushchin wrote:
> > @@ -75,12 +80,13 @@ static int do_help(int argc, char **argv)
> >  	fprintf(stderr,
> >  		"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
> >  		"       %s batch file FILE\n"
> > +		"       %s cgattach FILE CGROUP TYPE\n"
> 
> Can you change the order to:
> +		"       %s cgattach CGROUP TYPE FILE\n"
> 
> Makes for better consistency with the detach command in the next patch:
> +		"       %s cgdetach CGROUP TYPE ID\n"
> 
> 

Good point.

I'll fix this and will add support for attach_flags in v2.

Thanks!
Jakub Kicinski Dec. 1, 2017, 3:13 a.m. UTC | #4
On Thu, 30 Nov 2017 13:43:00 +0000, Roman Gushchin wrote:
> +	attach_type = parse_attach_type(argv[2]);
> +	if (attach_type == __MAX_BPF_ATTACH_TYPE) {
> +		bpf_object__close(obj);
> +		close(prog_fd);
> +		close(cgroup_fd);
> +		p_err("Invalid attach type\n");
> +		return -1;
> +	}
> +
> +	if (bpf_prog_attach(prog_fd, cgroup_fd, attach_type, 0)) {
> +		bpf_object__close(obj);
> +		close(prog_fd);
> +		close(cgroup_fd);
> +		p_err("Failed to attach program");
> +		return -1;
> +	}
> +
> +	bpf_object__close(obj);
> +	close(prog_fd);
> +	close(cgroup_fd);
> +
> +	return 0;
> +}

Could you try to consolidate the error paths into a one larger handler
and use gotos to jump to it?  You can see it done in number of places,
grep for e.g. exit_free.
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index d6e4762170a4..8eb3b9bf5bb2 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -41,9 +41,14 @@ 
 #include <linux/version.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 #include <bpf.h>
+#include <libbpf.h>
 
 #include "main.h"
 
@@ -75,12 +80,13 @@  static int do_help(int argc, char **argv)
 	fprintf(stderr,
 		"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
 		"       %s batch file FILE\n"
+		"       %s cgattach FILE 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);
 
 	return 0;
 }
@@ -159,12 +165,14 @@  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 const struct cmd cmds[] = {
 	{ "help",	do_help },
 	{ "batch",	do_batch },
 	{ "prog",	do_prog },
 	{ "map",	do_map },
+	{ "cgattach",	do_cgattach },
 	{ "version",	do_version },
 	{ 0 }
 };
@@ -259,6 +267,77 @@  static int do_batch(int argc, char **argv)
 	return err;
 }
 
+static const char * const attach_type_strings[] = {
+	[BPF_CGROUP_INET_INGRESS] = "ingress",
+	[BPF_CGROUP_INET_EGRESS] = "egress",
+	[BPF_CGROUP_INET_SOCK_CREATE] = "sock_create",
+	[BPF_CGROUP_SOCK_OPS] = "sock_ops",
+	[BPF_CGROUP_DEVICE] = "device",
+	[__MAX_BPF_ATTACH_TYPE] = NULL,
+};
+
+enum bpf_attach_type parse_attach_type(const char *str)
+{
+	enum bpf_attach_type type;
+
+	for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
+		if (attach_type_strings[type] &&
+		    strcmp(str, attach_type_strings[type]) == 0)
+			return type;
+	}
+
+	return __MAX_BPF_ATTACH_TYPE;
+}
+
+static int do_cgattach(int argc, char **argv)
+{
+	struct bpf_object *obj;
+	int prog_fd, cgroup_fd;
+	enum bpf_attach_type attach_type;
+
+	if (argc < 3) {
+		p_err("too few parameters for cgattach\n");
+		return -1;
+	} else if (argc > 3) {
+		p_err("too many parameters for cgattach\n");
+		return -1;
+	}
+
+	if (bpf_prog_load(argv[0], BPF_PROG_TYPE_UNSPEC, &obj, &prog_fd))
+		return -1;
+
+	cgroup_fd = open(argv[1], O_RDONLY);
+	if (cgroup_fd < 0) {
+		bpf_object__close(obj);
+		close(prog_fd);
+		p_err("can't open cgroup %s\n", argv[1]);
+		return -1;
+	}
+
+	attach_type = parse_attach_type(argv[2]);
+	if (attach_type == __MAX_BPF_ATTACH_TYPE) {
+		bpf_object__close(obj);
+		close(prog_fd);
+		close(cgroup_fd);
+		p_err("Invalid attach type\n");
+		return -1;
+	}
+
+	if (bpf_prog_attach(prog_fd, cgroup_fd, attach_type, 0)) {
+		bpf_object__close(obj);
+		close(prog_fd);
+		close(cgroup_fd);
+		p_err("Failed to attach program");
+		return -1;
+	}
+
+	bpf_object__close(obj);
+	close(prog_fd);
+	close(cgroup_fd);
+
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	static const struct option options[] = {