bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jakub Sitnicki <jakub@cloudflare.com>
To: bpf@vger.kernel.org
Cc: netdev@vger.kernel.org, kernel-team@cloudflare.com
Subject: [PATCH bpf-next v2 06/12] libbpf: Add support for bpf_link-based netns attachment
Date: Sun, 31 May 2020 10:28:40 +0200	[thread overview]
Message-ID: <20200531082846.2117903-7-jakub@cloudflare.com> (raw)
In-Reply-To: <20200531082846.2117903-1-jakub@cloudflare.com>

Add bpf_program__attach_nets(), which uses LINK_CREATE subcommand to create
an FD-based kernel bpf_link, for attach types tied to network namespace,
that is BPF_FLOW_DISSECTOR for the moment.

Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
---
 tools/lib/bpf/libbpf.c   | 23 ++++++++++++++++++-----
 tools/lib/bpf/libbpf.h   |  2 ++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 74d967619dcf..b8af2f91b7c3 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -7894,8 +7894,9 @@ static struct bpf_link *attach_iter(const struct bpf_sec_def *sec,
 	return bpf_program__attach_iter(prog, NULL);
 }
 
-struct bpf_link *
-bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
+static struct bpf_link *
+bpf_program__attach_fd(struct bpf_program *prog, int target_fd,
+		       const char *target_name)
 {
 	enum bpf_attach_type attach_type;
 	char errmsg[STRERR_BUFSIZE];
@@ -7915,12 +7916,12 @@ bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
 	link->detach = &bpf_link__detach_fd;
 
 	attach_type = bpf_program__get_expected_attach_type(prog);
-	link_fd = bpf_link_create(prog_fd, cgroup_fd, attach_type, NULL);
+	link_fd = bpf_link_create(prog_fd, target_fd, attach_type, NULL);
 	if (link_fd < 0) {
 		link_fd = -errno;
 		free(link);
-		pr_warn("program '%s': failed to attach to cgroup: %s\n",
-			bpf_program__title(prog, false),
+		pr_warn("program '%s': failed to attach to %s: %s\n",
+			bpf_program__title(prog, false), target_name,
 			libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
 		return ERR_PTR(link_fd);
 	}
@@ -7928,6 +7929,18 @@ bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
 	return link;
 }
 
+struct bpf_link *
+bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
+{
+	return bpf_program__attach_fd(prog, cgroup_fd, "cgroup");
+}
+
+struct bpf_link *
+bpf_program__attach_netns(struct bpf_program *prog, int netns_fd)
+{
+	return bpf_program__attach_fd(prog, netns_fd, "netns");
+}
+
 struct bpf_link *
 bpf_program__attach_iter(struct bpf_program *prog,
 			 const struct bpf_iter_attach_opts *opts)
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 8528a02d5af8..334437af3014 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -253,6 +253,8 @@ LIBBPF_API struct bpf_link *
 bpf_program__attach_lsm(struct bpf_program *prog);
 LIBBPF_API struct bpf_link *
 bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd);
+LIBBPF_API struct bpf_link *
+bpf_program__attach_netns(struct bpf_program *prog, int netns_fd);
 
 struct bpf_map;
 
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index c18860200abb..f732c77b7ed0 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -262,6 +262,7 @@ LIBBPF_0.0.9 {
 		bpf_link_get_fd_by_id;
 		bpf_link_get_next_id;
 		bpf_program__attach_iter;
+		bpf_program__attach_netns;
 		perf_buffer__consume;
 		ring_buffer__add;
 		ring_buffer__consume;
-- 
2.25.4


  parent reply	other threads:[~2020-05-31  8:29 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-31  8:28 [PATCH bpf-next v2 00/12] Link-based program attachment to network namespaces Jakub Sitnicki
2020-05-31  8:28 ` [PATCH bpf-next v2 01/12] flow_dissector: Pull locking up from prog attach callback Jakub Sitnicki
2020-05-31  8:28 ` [PATCH bpf-next v2 02/12] net: Introduce netns_bpf for BPF programs attached to netns Jakub Sitnicki
2020-05-31  8:28 ` [PATCH bpf-next v2 03/12] flow_dissector: Move out netns_bpf prog callbacks Jakub Sitnicki
2020-05-31  8:28 ` [PATCH bpf-next v2 04/12] bpf: Add link-based BPF program attachment to network namespace Jakub Sitnicki
2020-06-01 22:30   ` Andrii Nakryiko
2020-06-02  9:30     ` Jakub Sitnicki
2020-06-02 17:38       ` Andrii Nakryiko
2020-05-31  8:28 ` [PATCH bpf-next v2 05/12] bpf, cgroup: Return ENOLINK for auto-detached links on update Jakub Sitnicki
2020-06-01 22:31   ` Andrii Nakryiko
2020-05-31  8:28 ` Jakub Sitnicki [this message]
2020-06-01 22:32   ` [PATCH bpf-next v2 06/12] libbpf: Add support for bpf_link-based netns attachment Andrii Nakryiko
2020-05-31  8:28 ` [PATCH bpf-next v2 07/12] bpftool: Extract helpers for showing link attach type Jakub Sitnicki
2020-06-01 22:35   ` Andrii Nakryiko
2020-06-02  9:37     ` Jakub Sitnicki
2020-06-02 17:39       ` Andrii Nakryiko
2020-05-31  8:28 ` [PATCH bpf-next v2 08/12] bpftool: Support link show for netns-attached links Jakub Sitnicki
2020-06-01 22:38   ` Andrii Nakryiko
2020-05-31  8:28 ` [PATCH bpf-next v2 09/12] selftests/bpf: Add tests for attaching bpf_link to netns Jakub Sitnicki
2020-05-31  8:28 ` [PATCH bpf-next v2 10/12] selftests/bpf, flow_dissector: Close TAP device FD after the test Jakub Sitnicki
2020-05-31  8:28 ` [PATCH bpf-next v2 11/12] selftests/bpf: Convert test_flow_dissector to use BPF skeleton Jakub Sitnicki
2020-06-01 22:42   ` Andrii Nakryiko
2020-06-02  9:46     ` Jakub Sitnicki
2020-05-31  8:28 ` [PATCH bpf-next v2 12/12] selftests/bpf: Extend test_flow_dissector to cover link creation Jakub Sitnicki
2020-06-01 22:26 ` [PATCH bpf-next v2 00/12] Link-based program attachment to network namespaces Alexei Starovoitov

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=20200531082846.2117903-7-jakub@cloudflare.com \
    --to=jakub@cloudflare.com \
    --cc=bpf@vger.kernel.org \
    --cc=kernel-team@cloudflare.com \
    --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).