Skip to contentSkip to conversation

net/smc: Introduce BPF injection capability

PATCH In review D. Wythe 2 participants 7 messages Feb 21, 2023 – Feb 22, 2023

Subsystems (8)
lore
Share & tools
lore

D. Wythe

author maintainer
#0 lore raw

cover net/smc: Introduce BPF injection capability

From: "D. Wythe" <[email protected]>

This PATCHes attempts to introduce BPF injection capability for SMC, and add selftest to ensure code stability.

As we all know that the SMC protocol is not suitable for all scenarios, especially for short-lived. However, for most applications, they cannot guarantee that there are no such scenarios at all. Therefore, apps may need some specific strategies to decide shall we need to use SMC or not, for example, apps can limit the scope of the SMC to a specific IP address or port.

Based on the consideration of transparent replacement, we hope that apps can remain transparent even if they need to formulate some specific strategies for SMC using. That is, do not need to recompile their code.

On the other hand, we need to ensure the scalability of strategies implementation. Although it is simple to use socket options or sysctl, it will bring more complexity to subsequent expansion.

Fortunately, BPF can solve these concerns very well, users can write thire own strategies in eBPF to choose whether to use SMC or not. And it's quite easy for them to modify their strategies in the future.

This PATCHes implement injection capability for SMC via struct_ops. In that way, we can add new injection scenarios in the future.

D. Wythe (2):
net/smc: Introduce BPF injection capability for SMC net/smc: add selftest for SMC bpf capability

 include/linux/btf_ids.h                          |  15 ++
 include/net/smc.h                                | 254 ++++++++++++++++++
 kernel/bpf/bpf_struct_ops_types.h                |   4 +
 net/Makefile                                     |   5 +
 net/smc/af_smc.c                                 |  10 +-
 net/smc/bpf_smc_struct_ops.c                     | 146 +++++++++++
 net/smc/smc.h                                    | 220 ----------------
 tools/testing/selftests/bpf/prog_tests/bpf_smc.c |  39 +++
 tools/testing/selftests/bpf/progs/bpf_smc.c      | 315 +++++++++++++++++++++++
 9 files changed, 787 insertions(+), 221 deletions(-)
 create mode 100644 net/smc/bpf_smc_struct_ops.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_smc.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_smc.c
Signature · 1 line
1.8.3.1
author maintainer
#1 lore raw

PATCH 1/2 net/smc: Introduce BPF injection capability for SMC

From: "D. Wythe" <[email protected]>

This PATCH attempts to introduce BPF injection capability for SMC. As we all know that the SMC protocol is not suitable for all scenarios, especially for short-lived. However, for most applications, they cannot guarantee that there are no such scenarios at all. Therefore, apps may need some specific strategies to decide shall we need to use SMC or not, for example, apps can limit the scope of the SMC to a specific IP address or port.

Based on the consideration of transparent replacement, we hope that apps can remain transparent even if they need to formulate some specific strategies for SMC using. That is, do not need to recompile their code.

On the other hand, we need to ensure the scalability of strategies implementation. Although it is simple to use socket options or sysctl, it will bring more complexity to subsequent expansion.

Fortunately, BPF can solve these concerns very well, users can write thire own strategies in eBPF to choose whether to use SMC or not. And it's quite easy for them to modify their strategies in the future.

This PATCH implement injection capability for SMC via struct_ops. In that way, we can add new injection scenarios in the future.

---
 include/linux/btf_ids.h           |  15 +++
 include/net/smc.h                 | 254 ++++++++++++++++++++++++++++++++++++++
 kernel/bpf/bpf_struct_ops_types.h |   4 +
 net/Makefile                      |   5 +
 net/smc/af_smc.c                  |  10 +-
 net/smc/bpf_smc_struct_ops.c      | 146 ++++++++++++++++++++++
 net/smc/smc.h                     | 220 ---------------------------------
 7 files changed, 433 insertions(+), 221 deletions(-)
 create mode 100644 net/smc/bpf_smc_struct_ops.c
include/linux/btf_ids.h +15 -0
index 3a4f7cd..25eab1e 100644
@@ -264,6 +264,21 @@ enum {
 MAX_BTF_TRACING_TYPE,
 };
 
+#if IS_ENABLED(CONFIG_SMC)
+#define BTF_SMC_TYPE_xxx		\
+	BTF_SMC_TYPE(BTF_SMC_TYPE_SOCK, smc_sock)		\
+	BTF_SMC_TYPE(BTF_SMC_TYPE_CONNECTION, smc_connection)	\
+	BTF_SMC_TYPE(BTF_SMC_TYPE_HOST_CURSOR, smc_host_cursor)
+
+enum {
+#define BTF_SMC_TYPE(name, type) name,
+BTF_SMC_TYPE_xxx
+#undef BTF_SMC_TYPE
+MAX_BTF_SMC_TYPE,
+};
+extern u32 btf_smc_ids[];
+#endif
+
 extern u32 btf_tracing_ids[];
 extern u32 bpf_cgroup_btf_id[];
 extern u32 bpf_local_storage_map_btf_id[];
include/net/smc.h +254 -0

271 lines not on this page · raw message

kernel/bpf/bpf_struct_ops_types.h +4 -0
index 5678a9d..35cdd15 100644
@@ -9,4 +9,8 @@
 #include <net/tcp.h>
 BPF_STRUCT_OPS_TYPE(tcp_congestion_ops)
 #endif
+#if IS_ENABLED(CONFIG_SMC)
+#include <net/smc.h>
+BPF_STRUCT_OPS_TYPE(smc_sock_negotiator_ops)
+#endif
 #endif
net/Makefile +5 -0
index 0914bea..47a4c00 100644
@@ -52,6 +52,11 @@ obj-$(CONFIG_TIPC)		+= tipc/
 obj-$(CONFIG_NETLABEL)		+= netlabel/
 obj-$(CONFIG_IUCV)		+= iucv/
 obj-$(CONFIG_SMC)		+= smc/
+ifneq ($(CONFIG_SMC),)
+ifeq ($(CONFIG_BPF_SYSCALL),y)
+obj-y				+= smc/bpf_smc_struct_ops.o
+endif
+endif
 obj-$(CONFIG_RFKILL)		+= rfkill/
 obj-$(CONFIG_NET_9P)		+= 9p/
 obj-$(CONFIG_CAIF)		+= caif/
net/smc/af_smc.c +9 -1
index d7a7420..98651b85 100644
@@ -166,6 +166,9 @@ static bool smc_hs_congested(const struct sock *sk)
 	if (workqueue_congested(WORK_CPU_UNBOUND, smc_hs_wq))
 		return true;
 
+	if (!smc_sock_should_select_smc(smc))
+		return true;
+
 	return false;
 }
 
@@ -320,6 +323,9 @@ static int smc_release(struct socket *sock)
 	sock_hold(sk); /* sock_put below */
 	smc = smc_sk(sk);
 
+	/* trigger info gathering if needed.*/
+	smc_sock_perform_collecting_info(smc, SMC_SOCK_CLOSED_TIMING);
+
 	old_state = sk->sk_state;
 
 	/* cleanup for a dangling non-blocking connect */
@@ -1627,7 +1633,9 @@ static int smc_connect(struct socket *sock, struct sockaddr *addr,
 	}
 
 	smc_copy_sock_settings_to_clc(smc);
-	tcp_sk(smc->clcsock->sk)->syn_smc = 1;
+	tcp_sk(smc->clcsock->sk)->syn_smc = (smc_sock_should_select_smc(smc) == SK_PASS) ?
+		1 : 0;
+
 	if (smc->connect_nonblock) {
 		rc = -EALREADY;
 		goto out;
net/smc/bpf_smc_struct_ops.c new file +146 -0

146 lines not on this page · raw message

net/smc/smc.h +0 -220

232 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author maintainer
#2 lore raw

PATCH 2/2 net/smc: add selftest for SMC bpf capability

In reply to D. Wythe

From: "D. Wythe" <[email protected]>

This PATCH adds a tiny selftest for SMC bpf capability, making decisions on whether to use SMC by collecting certain information from kernel smc sock.

Follow the steps below to run this test.

make -C tools/testing/selftests/bpf
cd tools/testing/selftests/bpf
sudo ./test_progs -t bpf_smc

Results shows:
18 bpf_smc:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

---
 tools/testing/selftests/bpf/prog_tests/bpf_smc.c |  39 +++
 tools/testing/selftests/bpf/progs/bpf_smc.c      | 315 +++++++++++++++++++++++
 2 files changed, 354 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_smc.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_smc.c
tools/testing/selftests/bpf/prog_tests/bpf_smc.c new file +39 -0
new file mode 100644
index 0000000..b143932
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2019 Facebook */
+
+#include <linux/err.h>
+#include <netinet/tcp.h>
+#include <test_progs.h>
+#include "bpf_smc.skel.h"
+
+void test_bpf_smc(void)
+{
+	struct bpf_smc *smc_skel;
+	struct bpf_link *link;
+	int err;
+
+	smc_skel = bpf_smc__open();
+	if (!ASSERT_OK_PTR(smc_skel, "skel_open"))
+		return;
+
+	err = bpf_map__set_type(smc_skel->maps.negotiator_map, BPF_MAP_TYPE_HASH);
+	if (!ASSERT_OK(err, "bpf_map__set_type"))
+		goto error;
+
+	err = bpf_map__set_max_entries(smc_skel->maps.negotiator_map, 1);
+	if (!ASSERT_OK(err, "bpf_map__set_type"))
+		goto error;
+
+	err =  bpf_smc__load(smc_skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto error;
+
+	link = bpf_map__attach_struct_ops(smc_skel->maps.ops);
+	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
+		goto error;
+
+	bpf_link__destroy(link);
+error:
+	bpf_smc__destroy(smc_skel);
+}
+
tools/testing/selftests/bpf/progs/bpf_smc.c new file +315 -0

315 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author maintainer
#3 lore raw
In reply to D. Wythe

Sorry for forgot to cc the maintainer of BPF,
please ignore this. I will resend a new version.

Quoted 45 lines · D. Wythe

From: "D. Wythe" <[email protected]>

This PATCHes attempts to introduce BPF injection capability for SMC, and add selftest to ensure code stability.

As we all know that the SMC protocol is not suitable for all scenarios, especially for short-lived. However, for most applications, they cannot guarantee that there are no such scenarios at all. Therefore, apps may need some specific strategies to decide shall we need to use SMC or not, for example, apps can limit the scope of the SMC to a specific IP address or port.

Based on the consideration of transparent replacement, we hope that apps can remain transparent even if they need to formulate some specific strategies for SMC using. That is, do not need to recompile their code.

On the other hand, we need to ensure the scalability of strategies implementation. Although it is simple to use socket options or sysctl, it will bring more complexity to subsequent expansion.

Fortunately, BPF can solve these concerns very well, users can write thire own strategies in eBPF to choose whether to use SMC or not. And it's quite easy for them to modify their strategies in the future.

This PATCHes implement injection capability for SMC via struct_ops. In that way, we can add new injection scenarios in the future.

D. Wythe (2):
net/smc: Introduce BPF injection capability for SMC net/smc: add selftest for SMC bpf capability

  include/linux/btf_ids.h                          |  15 ++
  include/net/smc.h                                | 254 ++++++++++++++++++
  kernel/bpf/bpf_struct_ops_types.h                |   4 +
  net/Makefile                                     |   5 +
  net/smc/af_smc.c                                 |  10 +-
  net/smc/bpf_smc_struct_ops.c                     | 146 +++++++++++
  net/smc/smc.h                                    | 220 ----------------
  tools/testing/selftests/bpf/prog_tests/bpf_smc.c |  39 +++
  tools/testing/selftests/bpf/progs/bpf_smc.c      | 315 +++++++++++++++++++++++
  9 files changed, 787 insertions(+), 221 deletions(-)

create mode 100644 net/smc/bpf_smc_struct_ops.c
create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_smc.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_smc.c

#4 lore raw
Quoted 3 lines · D. Wythe

Sorry for forgot to cc the maintainer of BPF,
please ignore this. I will resend a new version.

net-next is closed.

You'll need to repost it, either as an RFC, or wait until after v6.3-rc1 has been tagged.

author maintainer
#5 lore raw
Quoted 9 lines · Simon Horman
Quoted 3 lines · D. Wythe

Sorry for forgot to cc the maintainer of BPF,
please ignore this. I will resend a new version.

net-next is closed.

You'll need to repost it, either as an RFC, or wait until after v6.3-rc1 has been tagged.

I had repost it to bpf-next, but thank you for your reminding!

Best wishes.
D. Wythe

#6 lore raw
Quoted 8 lines · D. Wythe
Quoted 5 lines · Simon Horman
Quoted 3 lines · D. Wythe

Sorry for forgot to cc the maintainer of BPF,
please ignore this. I will resend a new version.

Quoted 6 lines
Quoted 4 lines

net-next is closed.

You'll need to repost it, either as an RFC, or wait until after v6.3-rc1 has been tagged.

I had repost it to bpf-next, but thank you for your reminding!

Sorry, my mistake.