BPF Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH bpf-next v2 0/2] Atomic flow dissector updates
@ 2019-10-10 18:17 Jakub Sitnicki
  2019-10-10 18:17 ` [PATCH bpf-next v2 1/2] flow_dissector: Allow updating the flow dissector program atomically Jakub Sitnicki
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Jakub Sitnicki @ 2019-10-10 18:17 UTC (permalink / raw)
  To: bpf; +Cc: netdev, kernel-team, Stanislav Fomichev

This patch set changes how bpf(BPF_PROG_ATTACH) operates on flow dissector
hook when there is already a program attached. After this change the user
is allowed to update the program in a single syscall. Please see the first
patch for rationale.

v1 -> v2:

- Don't use CHECK macro which expects BPF program run duration, which we
  don't track in attach/detach tests. Suggested by Stanislav Fomichev.

- Test re-attaching flow dissector in both root and non-root network
  namespace. Suggested by Stanislav Fomichev.


Jakub Sitnicki (2):
  flow_dissector: Allow updating the flow dissector program atomically
  selftests/bpf: Check that flow dissector can be re-attached

 net/core/flow_dissector.c                     |  10 +-
 .../bpf/prog_tests/flow_dissector_reattach.c  | 127 ++++++++++++++++++
 2 files changed, 134 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c

-- 
2.20.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH bpf-next v2 1/2] flow_dissector: Allow updating the flow dissector program atomically
  2019-10-10 18:17 [PATCH bpf-next v2 0/2] Atomic flow dissector updates Jakub Sitnicki
@ 2019-10-10 18:17 ` Jakub Sitnicki
  2019-10-10 19:41   ` Martin Lau
  2019-10-10 18:17 ` [PATCH bpf-next v2 2/2] selftests/bpf: Check that flow dissector can be re-attached Jakub Sitnicki
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: Jakub Sitnicki @ 2019-10-10 18:17 UTC (permalink / raw)
  To: bpf; +Cc: netdev, kernel-team, Stanislav Fomichev

It is currently not possible to detach the flow dissector program and
attach a new one in an atomic fashion, that is with a single syscall.
Attempts to do so will be met with EEXIST error.

This makes updates to flow dissector program hard. Traffic steering that
relies on BPF-powered flow dissection gets disrupted while old program has
been already detached but the new one has not been attached yet.

There is also a window of opportunity to attach a flow dissector to a
non-root namespace while updating the root flow dissector, thus blocking
the update.

Lastly, the behavior is inconsistent with cgroup BPF programs, which can be
replaced with a single bpf(BPF_PROG_ATTACH, ...) syscall without any
restrictions.

Allow attaching a new flow dissector program when another one is already
present with a restriction that it can't be the same program.

Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
---
 net/core/flow_dissector.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/net/core/flow_dissector.c b/net/core/flow_dissector.c
index 6b4b88d1599d..dbf502c18656 100644
--- a/net/core/flow_dissector.c
+++ b/net/core/flow_dissector.c
@@ -128,6 +128,8 @@ int skb_flow_dissector_bpf_prog_attach(const union bpf_attr *attr,
 		struct net *ns;
 
 		for_each_net(ns) {
+			if (ns == &init_net)
+				continue;
 			if (rcu_access_pointer(ns->flow_dissector_prog)) {
 				ret = -EEXIST;
 				goto out;
@@ -145,12 +147,14 @@ int skb_flow_dissector_bpf_prog_attach(const union bpf_attr *attr,
 
 	attached = rcu_dereference_protected(net->flow_dissector_prog,
 					     lockdep_is_held(&flow_dissector_mutex));
-	if (attached) {
-		/* Only one BPF program can be attached at a time */
-		ret = -EEXIST;
+	if (attached == prog) {
+		/* The same program cannot be attached twice */
+		ret = -EINVAL;
 		goto out;
 	}
 	rcu_assign_pointer(net->flow_dissector_prog, prog);
+	if (attached)
+		bpf_prog_put(attached);
 out:
 	mutex_unlock(&flow_dissector_mutex);
 	return ret;
-- 
2.20.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH bpf-next v2 2/2] selftests/bpf: Check that flow dissector can be re-attached
  2019-10-10 18:17 [PATCH bpf-next v2 0/2] Atomic flow dissector updates Jakub Sitnicki
  2019-10-10 18:17 ` [PATCH bpf-next v2 1/2] flow_dissector: Allow updating the flow dissector program atomically Jakub Sitnicki
@ 2019-10-10 18:17 ` Jakub Sitnicki
  2019-10-10 19:42   ` Martin Lau
  2019-10-10 19:57 ` [PATCH bpf-next v2 0/2] Atomic flow dissector updates Stanislav Fomichev
  2019-10-10 23:38 ` Daniel Borkmann
  3 siblings, 1 reply; 8+ messages in thread
From: Jakub Sitnicki @ 2019-10-10 18:17 UTC (permalink / raw)
  To: bpf; +Cc: netdev, kernel-team, Stanislav Fomichev

Make sure a new flow dissector program can be attached to replace the old
one with a single syscall. Also check that attaching the same program twice
is prohibited.

Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
---
 .../bpf/prog_tests/flow_dissector_reattach.c  | 127 ++++++++++++++++++
 1 file changed, 127 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c

diff --git a/tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c b/tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c
new file mode 100644
index 000000000000..fa502720825c
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c
@@ -0,1 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test that the flow_dissector program can be updated with a single
+ * syscall by attaching a new program that replaces the existing one.
+ *
+ * Corner case - the same program cannot be attached twice.
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <stdbool.h>
+#include <unistd.h>
+
+#include <linux/bpf.h>
+#include <bpf/bpf.h>
+
+#include "test_progs.h"
+
+static bool is_attached(int netns)
+{
+	__u32 cnt;
+	int err;
+
+	err = bpf_prog_query(netns, BPF_FLOW_DISSECTOR, 0, NULL, NULL, &cnt);
+	if (CHECK_FAIL(err)) {
+		perror("bpf_prog_query");
+		return true; /* fail-safe */
+	}
+
+	return cnt > 0;
+}
+
+static int load_prog(void)
+{
+	struct bpf_insn prog[] = {
+		BPF_MOV64_IMM(BPF_REG_0, BPF_OK),
+		BPF_EXIT_INSN(),
+	};
+	int fd;
+
+	fd = bpf_load_program(BPF_PROG_TYPE_FLOW_DISSECTOR, prog,
+			      ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
+	if (CHECK_FAIL(fd < 0))
+		perror("bpf_load_program");
+
+	return fd;
+}
+
+static void do_flow_dissector_reattach(void)
+{
+	int prog_fd[2] = { -1, -1 };
+	int err;
+
+	prog_fd[0] = load_prog();
+	if (prog_fd[0] < 0)
+		return;
+
+	prog_fd[1] = load_prog();
+	if (prog_fd[1] < 0)
+		goto out_close;
+
+	err = bpf_prog_attach(prog_fd[0], 0, BPF_FLOW_DISSECTOR, 0);
+	if (CHECK_FAIL(err)) {
+		perror("bpf_prog_attach-0");
+		goto out_close;
+	}
+
+	/* Expect success when attaching a different program */
+	err = bpf_prog_attach(prog_fd[1], 0, BPF_FLOW_DISSECTOR, 0);
+	if (CHECK_FAIL(err)) {
+		perror("bpf_prog_attach-1");
+		goto out_detach;
+	}
+
+	/* Expect failure when attaching the same program twice */
+	err = bpf_prog_attach(prog_fd[1], 0, BPF_FLOW_DISSECTOR, 0);
+	if (CHECK_FAIL(!err || errno != EINVAL))
+		perror("bpf_prog_attach-2");
+
+out_detach:
+	err = bpf_prog_detach(0, BPF_FLOW_DISSECTOR);
+	if (CHECK_FAIL(err))
+		perror("bpf_prog_detach");
+
+out_close:
+	close(prog_fd[1]);
+	close(prog_fd[0]);
+}
+
+void test_flow_dissector_reattach(void)
+{
+	int init_net, err;
+
+	init_net = open("/proc/1/ns/net", O_RDONLY);
+	if (CHECK_FAIL(init_net < 0)) {
+		perror("open(/proc/1/ns/net)");
+		return;
+	}
+
+	err = setns(init_net, CLONE_NEWNET);
+	if (CHECK_FAIL(err)) {
+		perror("setns(/proc/1/ns/net)");
+		goto out_close;
+	}
+
+	if (is_attached(init_net)) {
+		test__skip();
+		printf("Can't test with flow dissector attached to init_net\n");
+		return;
+	}
+
+	/* First run tests in root network namespace */
+	do_flow_dissector_reattach();
+
+	/* Then repeat tests in a non-root namespace */
+	err = unshare(CLONE_NEWNET);
+	if (CHECK_FAIL(err)) {
+		perror("unshare(CLONE_NEWNET)");
+		goto out_close;
+	}
+	do_flow_dissector_reattach();
+
+out_close:
+	close(init_net);
+}
-- 
2.20.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH bpf-next v2 1/2] flow_dissector: Allow updating the flow dissector program atomically
  2019-10-10 18:17 ` [PATCH bpf-next v2 1/2] flow_dissector: Allow updating the flow dissector program atomically Jakub Sitnicki
@ 2019-10-10 19:41   ` Martin Lau
  0 siblings, 0 replies; 8+ messages in thread
From: Martin Lau @ 2019-10-10 19:41 UTC (permalink / raw)
  To: Jakub Sitnicki; +Cc: bpf, netdev, kernel-team, Stanislav Fomichev

On Thu, Oct 10, 2019 at 08:17:49PM +0200, Jakub Sitnicki wrote:
> It is currently not possible to detach the flow dissector program and
> attach a new one in an atomic fashion, that is with a single syscall.
> Attempts to do so will be met with EEXIST error.
> 
> This makes updates to flow dissector program hard. Traffic steering that
> relies on BPF-powered flow dissection gets disrupted while old program has
> been already detached but the new one has not been attached yet.
> 
> There is also a window of opportunity to attach a flow dissector to a
> non-root namespace while updating the root flow dissector, thus blocking
> the update.
> 
> Lastly, the behavior is inconsistent with cgroup BPF programs, which can be
> replaced with a single bpf(BPF_PROG_ATTACH, ...) syscall without any
> restrictions.
> 
> Allow attaching a new flow dissector program when another one is already
> present with a restriction that it can't be the same program.
Acked-by: Martin KaFai Lau <kafai@fb.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH bpf-next v2 2/2] selftests/bpf: Check that flow dissector can be re-attached
  2019-10-10 18:17 ` [PATCH bpf-next v2 2/2] selftests/bpf: Check that flow dissector can be re-attached Jakub Sitnicki
@ 2019-10-10 19:42   ` Martin Lau
  0 siblings, 0 replies; 8+ messages in thread
From: Martin Lau @ 2019-10-10 19:42 UTC (permalink / raw)
  To: Jakub Sitnicki; +Cc: bpf, netdev, kernel-team, Stanislav Fomichev

On Thu, Oct 10, 2019 at 08:17:50PM +0200, Jakub Sitnicki wrote:
> Make sure a new flow dissector program can be attached to replace the old
> one with a single syscall. Also check that attaching the same program twice
> is prohibited.
> 
> Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH bpf-next v2 0/2] Atomic flow dissector updates
  2019-10-10 18:17 [PATCH bpf-next v2 0/2] Atomic flow dissector updates Jakub Sitnicki
  2019-10-10 18:17 ` [PATCH bpf-next v2 1/2] flow_dissector: Allow updating the flow dissector program atomically Jakub Sitnicki
  2019-10-10 18:17 ` [PATCH bpf-next v2 2/2] selftests/bpf: Check that flow dissector can be re-attached Jakub Sitnicki
@ 2019-10-10 19:57 ` Stanislav Fomichev
  2019-10-10 23:38 ` Daniel Borkmann
  3 siblings, 0 replies; 8+ messages in thread
From: Stanislav Fomichev @ 2019-10-10 19:57 UTC (permalink / raw)
  To: Jakub Sitnicki; +Cc: bpf, netdev, kernel-team

On 10/10, Jakub Sitnicki wrote:
> This patch set changes how bpf(BPF_PROG_ATTACH) operates on flow dissector
> hook when there is already a program attached. After this change the user
> is allowed to update the program in a single syscall. Please see the first
> patch for rationale.
> 
> v1 -> v2:
> 
> - Don't use CHECK macro which expects BPF program run duration, which we
>   don't track in attach/detach tests. Suggested by Stanislav Fomichev.
> 
> - Test re-attaching flow dissector in both root and non-root network
>   namespace. Suggested by Stanislav Fomichev.
For the series:
Reviewed-by: Stanislav Fomichev <sdf@google.com>

Thanks!

> Jakub Sitnicki (2):
>   flow_dissector: Allow updating the flow dissector program atomically
>   selftests/bpf: Check that flow dissector can be re-attached
> 
>  net/core/flow_dissector.c                     |  10 +-
>  .../bpf/prog_tests/flow_dissector_reattach.c  | 127 ++++++++++++++++++
>  2 files changed, 134 insertions(+), 3 deletions(-)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c
> 
> -- 
> 2.20.1
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH bpf-next v2 0/2] Atomic flow dissector updates
  2019-10-10 18:17 [PATCH bpf-next v2 0/2] Atomic flow dissector updates Jakub Sitnicki
                   ` (2 preceding siblings ...)
  2019-10-10 19:57 ` [PATCH bpf-next v2 0/2] Atomic flow dissector updates Stanislav Fomichev
@ 2019-10-10 23:38 ` Daniel Borkmann
  2019-10-11  8:31   ` Jakub Sitnicki
  3 siblings, 1 reply; 8+ messages in thread
From: Daniel Borkmann @ 2019-10-10 23:38 UTC (permalink / raw)
  To: Jakub Sitnicki; +Cc: bpf, netdev, kernel-team, Stanislav Fomichev

On Thu, Oct 10, 2019 at 08:17:48PM +0200, Jakub Sitnicki wrote:
> This patch set changes how bpf(BPF_PROG_ATTACH) operates on flow dissector
> hook when there is already a program attached. After this change the user
> is allowed to update the program in a single syscall. Please see the first
> patch for rationale.
> 
> v1 -> v2:
> 
> - Don't use CHECK macro which expects BPF program run duration, which we
>   don't track in attach/detach tests. Suggested by Stanislav Fomichev.
> 
> - Test re-attaching flow dissector in both root and non-root network
>   namespace. Suggested by Stanislav Fomichev.
> 
> 
> Jakub Sitnicki (2):
>   flow_dissector: Allow updating the flow dissector program atomically
>   selftests/bpf: Check that flow dissector can be re-attached
> 
>  net/core/flow_dissector.c                     |  10 +-
>  .../bpf/prog_tests/flow_dissector_reattach.c  | 127 ++++++++++++++++++
>  2 files changed, 134 insertions(+), 3 deletions(-)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c

This needs a new rebase, doesn't apply cleanly. Please carry on Martin
and Stanislav's tags. Thanks!

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH bpf-next v2 0/2] Atomic flow dissector updates
  2019-10-10 23:38 ` Daniel Borkmann
@ 2019-10-11  8:31   ` Jakub Sitnicki
  0 siblings, 0 replies; 8+ messages in thread
From: Jakub Sitnicki @ 2019-10-11  8:31 UTC (permalink / raw)
  To: Daniel Borkmann; +Cc: bpf, netdev, kernel-team, Stanislav Fomichev

On Fri, Oct 11, 2019 at 01:38 AM CEST, Daniel Borkmann wrote:
> On Thu, Oct 10, 2019 at 08:17:48PM +0200, Jakub Sitnicki wrote:
>> This patch set changes how bpf(BPF_PROG_ATTACH) operates on flow dissector
>> hook when there is already a program attached. After this change the user
>> is allowed to update the program in a single syscall. Please see the first
>> patch for rationale.
>> 
>> v1 -> v2:
>> 
>> - Don't use CHECK macro which expects BPF program run duration, which we
>>   don't track in attach/detach tests. Suggested by Stanislav Fomichev.
>> 
>> - Test re-attaching flow dissector in both root and non-root network
>>   namespace. Suggested by Stanislav Fomichev.
>> 
>> 
>> Jakub Sitnicki (2):
>>   flow_dissector: Allow updating the flow dissector program atomically
>>   selftests/bpf: Check that flow dissector can be re-attached
>> 
>>  net/core/flow_dissector.c                     |  10 +-
>>  .../bpf/prog_tests/flow_dissector_reattach.c  | 127 ++++++++++++++++++
>>  2 files changed, 134 insertions(+), 3 deletions(-)
>>  create mode 100644 tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c
>
> This needs a new rebase, doesn't apply cleanly. Please carry on Martin
> and Stanislav's tags. Thanks!

Rebased and posted v3. Thanks to Martin and Stanislav for reviewing it.

-Jakub

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, back to index

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-10 18:17 [PATCH bpf-next v2 0/2] Atomic flow dissector updates Jakub Sitnicki
2019-10-10 18:17 ` [PATCH bpf-next v2 1/2] flow_dissector: Allow updating the flow dissector program atomically Jakub Sitnicki
2019-10-10 19:41   ` Martin Lau
2019-10-10 18:17 ` [PATCH bpf-next v2 2/2] selftests/bpf: Check that flow dissector can be re-attached Jakub Sitnicki
2019-10-10 19:42   ` Martin Lau
2019-10-10 19:57 ` [PATCH bpf-next v2 0/2] Atomic flow dissector updates Stanislav Fomichev
2019-10-10 23:38 ` Daniel Borkmann
2019-10-11  8:31   ` Jakub Sitnicki

BPF Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/bpf/0 bpf/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 bpf bpf/ https://lore.kernel.org/bpf \
		bpf@vger.kernel.org
	public-inbox-index bpf

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.bpf


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git