Skip to contentSkip to conversation

net/smc: Introduce smc_hs_ctrl

PATCH Superseded D. Wythe 3 participants 9 messages Sep 18, 2025 – Sep 24, 2025

Subsystems (9)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

D. Wythe

author maintainer
#0 lore raw

v2 cover net/smc: Introduce smc_hs_ctrl

This patch aims to introduce BPF injection capabilities for SMC and includes a self-test to ensure code stability.

Since the SMC protocol isn't ideal for every situation, especially short-lived ones, most applications can't guarantee the absence of such scenarios. Consequently, applications may need specific strategies to decide whether to use SMC. For example, an application might limit SMC usage to certain IP addresses or ports.

To maintain the principle of transparent replacement, we want applications to remain unaffected even if they need specific SMC strategies. In other words, they should not require recompilation of their code.

Additionally, we need to ensure the scalability of strategy implementation. While using socket options or sysctl might be straightforward, it could complicate future expansions.

Fortunately, BPF addresses these concerns effectively. Users can write their own strategies in eBPF to determine whether to use SMC, and they can easily modify those strategies in the future.

This is a rework of the series from [1]. Changes since [1] are limited to the SMC parts:

1. Rename smc_ops to smc_hs_ctrl and change interface name.
2. Squash SMC patches, removing standalone non-BPF hook capability.
3. Fix typos

[1]: https://lore.kernel.org/bpf/[email protected]/#t

v2 -> v1:
- Removed the fixes patch, which have already been merged on current branch.
- Fixed compilation warning of smc_call_hsbpf() when CONFIG_SMC_HS_CTRL_BPF is not enabled.
- Changed the default value of CONFIG_SMC_HS_CTRL_BPF to Y.
- Fix typo and renamed some variables

D. Wythe (4):
bpf: export necessary symbols for modules with struct_ops net/smc: bpf: Introduce generic hook for handshake flow
libbpf: fix error when st-prefix_ops and ops from differ btf bpf/selftests: add selftest for bpf_smc_hs_ctrl

 include/net/netns/smc.h                       |   3 +
 include/net/smc.h                             |  53 +++
 kernel/bpf/bpf_struct_ops.c                   |   2 +
 kernel/bpf/syscall.c                          |   1 +
 net/ipv4/tcp_output.c                         |  36 +-
 net/smc/Kconfig                               |  10 +
 net/smc/Makefile                              |   1 +
 net/smc/af_smc.c                              |  10 +
 net/smc/smc_hs_bpf.c                          | 137 ++++++
 net/smc/smc_hs_bpf.h                          |  31 ++
 net/smc/smc_sysctl.c                          |  91 ++++
 tools/lib/bpf/libbpf.c                        |  37 +-
 tools/testing/selftests/bpf/config            |   4 +
 .../selftests/bpf/prog_tests/test_bpf_smc.c   | 396 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/bpf_smc.c   | 117 ++++++
 15 files changed, 896 insertions(+), 33 deletions(-)
 create mode 100644 net/smc/smc_hs_bpf.c
 create mode 100644 net/smc/smc_hs_bpf.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_bpf_smc.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_smc.c
Signature · 1 line
2.45.0
author maintainer
#1 lore raw

PATCH v2 1/4 bpf: export necessary symbols for modules with struct_ops

Exports three necessary symbols for implementing struct_ops with tristate subsystem.

To hold or release refcnt of struct_ops refcnt by inline funcs bpf_try_module_get and bpf_module_put which use bpf_struct_ops_get(put) conditionally.

And to copy obj name from one to the other with effective checks by bpf_obj_name_cpy.

---
 kernel/bpf/bpf_struct_ops.c | 2 ++
 kernel/bpf/syscall.c        | 1 +
 2 files changed, 3 insertions(+)
kernel/bpf/bpf_struct_ops.c +2 -0
index a41e6730edcf..278490683d28 100644
@@ -1162,6 +1162,7 @@ bool bpf_struct_ops_get(const void *kdata)
 	map = __bpf_map_inc_not_zero(&st_map->map, false);
 	return !IS_ERR(map);
 }
+EXPORT_SYMBOL_GPL(bpf_struct_ops_get);
 
 void bpf_struct_ops_put(const void *kdata)
 {
@@ -1173,6 +1174,7 @@ void bpf_struct_ops_put(const void *kdata)
 
 	bpf_map_put(&st_map->map);
 }
+EXPORT_SYMBOL_GPL(bpf_struct_ops_put);
 
 u32 bpf_struct_ops_id(const void *kdata)
 {
kernel/bpf/syscall.c +1 -0
index 3f178a0f8eb1..e8ffde2596db 100644
@@ -1204,6 +1204,7 @@ int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
 
 	return src - orig_src;
 }
+EXPORT_SYMBOL_GPL(bpf_obj_name_cpy);
 
 int map_check_no_btf(const struct bpf_map *map,
 		     const struct btf *btf,
Signature · 1 line
2.45.0
author maintainer
#2 lore raw

PATCH v2 2/4 net/smc: bpf: Introduce generic hook for handshake flow

In reply to D. Wythe

The introduction of IPPROTO_SMC enables eBPF programs to determine whether to use SMC based on the context of socket creation, such as network namespaces, PID and comm name, etc.

As a subsequent enhancement, to introduce a new generic hook that allows decisions on whether to use SMC or not at runtime, including but not limited to local/remote IP address or ports.

User can write their own implememtion via bpf_struct_ops now to choose whether to use SMC or not before TCP 3rd handshake to be comleted.

---
 include/net/netns/smc.h |   3 +
 include/net/smc.h       |  53 ++++++++++++++++
 net/ipv4/tcp_output.c   |  36 +++++++----
 net/smc/Kconfig         |  10 +++
 net/smc/Makefile        |   1 +
 net/smc/af_smc.c        |  10 +++
 net/smc/smc_hs_bpf.c    | 137 ++++++++++++++++++++++++++++++++++++++++
 net/smc/smc_hs_bpf.h    |  31 +++++++++
 net/smc/smc_sysctl.c    |  91 ++++++++++++++++++++++++++
 9 files changed, 358 insertions(+), 14 deletions(-)
 create mode 100644 net/smc/smc_hs_bpf.c
 create mode 100644 net/smc/smc_hs_bpf.h
include/net/netns/smc.h +3 -0
index fc752a50f91b..d66bfd392254 100644
@@ -17,6 +17,9 @@ struct netns_smc {
 #ifdef CONFIG_SYSCTL
 	struct ctl_table_header		*smc_hdr;
 #endif
+#if IS_ENABLED(CONFIG_SMC_HS_CTRL_BPF)
+	struct smc_hs_ctrl __rcu	*hs_ctrl;
+#endif /* CONFIG_SMC_HS_CTRL_BPF */
 	unsigned int			sysctl_autocorking_size;
 	unsigned int			sysctl_smcr_buf_type;
 	int				sysctl_smcr_testlink_time;
include/net/smc.h +53 -0

63 lines not on this page · raw message

net/ipv4/tcp_output.c +22 -14
index caf11920a878..e4ee138044d3 100644
@@ -39,6 +39,7 @@
 
 #include <net/tcp.h>
 #include <net/mptcp.h>
+#include <net/smc.h>
 #include <net/proto_memory.h>
 
 #include <linux/compiler.h>
@@ -764,34 +765,41 @@ static void tcp_options_write(struct tcphdr *th, struct tcp_sock *tp,
 	mptcp_options_write(th, ptr, tp, opts);
 }
 
-static void smc_set_option(const struct tcp_sock *tp,
+static void smc_set_option(struct tcp_sock *tp,
 			   struct tcp_out_options *opts,
 			   unsigned int *remaining)
 {
 #if IS_ENABLED(CONFIG_SMC)
-	if (static_branch_unlikely(&tcp_have_smc)) {
-		if (tp->syn_smc) {
-			if (*remaining >= TCPOLEN_EXP_SMC_BASE_ALIGNED) {
-				opts->options |= OPTION_SMC;
-				*remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED;
-			}
+	struct sock *sk = &tp->inet_conn.icsk_inet.sk;
+
+	if (static_branch_unlikely(&tcp_have_smc) && tp->syn_smc) {
+		tp->syn_smc = !!smc_call_hsbpf(1, sk, syn_option, tp);
+		/* re-check syn_smc */
+		if (tp->syn_smc &&
+		    *remaining >= TCPOLEN_EXP_SMC_BASE_ALIGNED) {
+			opts->options |= OPTION_SMC;
+			*remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED;
 		}
 	}
 #endif
 }
 
 static void smc_set_option_cond(const struct tcp_sock *tp,
-				const struct inet_request_sock *ireq,
+				struct inet_request_sock *ireq,
 				struct tcp_out_options *opts,
 				unsigned int *remaining)
 {
 #if IS_ENABLED(CONFIG_SMC)
-	if (static_branch_unlikely(&tcp_have_smc)) {
-		if (tp->syn_smc && ireq->smc_ok) {
-			if (*remaining >= TCPOLEN_EXP_SMC_BASE_ALIGNED) {
-				opts->options |= OPTION_SMC;
-				*remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED;
-			}
+	const struct sock *sk = &tp->inet_conn.icsk_inet.sk;
+
+	if (static_branch_unlikely(&tcp_have_smc) && tp->syn_smc && ireq->smc_ok) {
+		ireq->smc_ok = !!smc_call_hsbpf(1, sk, synack_option,
+						tp, ireq);
+		/* re-check smc_ok */
+		if (ireq->smc_ok &&
+		    *remaining >= TCPOLEN_EXP_SMC_BASE_ALIGNED) {
+			opts->options |= OPTION_SMC;
+			*remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED;
 		}
 	}
 #endif
net/smc/Kconfig +10 -0
index ba5e6a2dd2fd..67edeb927e19 100644
@@ -33,3 +33,13 @@ config SMC_LO
 	  of architecture or hardware.
 
 	  if unsure, say N.
+
+config SMC_HS_CTRL_BPF
+	bool "Generic eBPF hook for SMC handshake flow"
+	depends on SMC && BPF_SYSCALL
+	default y
+	help
+	  SMC_HS_CTRL_BPF enables support to register generic eBPF hook for SMC
+	  handshake flow, which offer much greater flexibility in modifying the behavior
+	  of the SMC protocol stack compared to a complete kernel-based approach. Select
+	  this option if you want filtring the handshake process via eBPF programs.
net/smc/Makefile +1 -0
index 60f1c87d5212..57b919434df4 100644
@@ -7,3 +7,4 @@ smc-y += smc_cdc.o smc_tx.o smc_rx.o smc_close.o smc_ism.o smc_netlink.o smc_sta
 smc-y += smc_tracepoint.o smc_inet.o
 smc-$(CONFIG_SYSCTL) += smc_sysctl.o
 smc-$(CONFIG_SMC_LO) += smc_loopback.o
+smc-$(CONFIG_SMC_HS_CTRL_BPF) += smc_hs_bpf.o
net/smc/af_smc.c +10 -0
index e0e48f24cd61..ecc6499b05fa 100644
@@ -59,6 +59,7 @@
 #include "smc_sysctl.h"
 #include "smc_loopback.h"
 #include "smc_inet.h"
+#include "smc_hs_bpf.h"
 
 static DEFINE_MUTEX(smc_server_lgr_pending);	/* serialize link group
 						 * creation on server
@@ -3609,8 +3610,17 @@ static int __init smc_init(void)
 		pr_err("%s: smc_inet_init fails with %d\n", __func__, rc);
 		goto out_ulp;
 	}
+	rc = bpf_smc_hs_ctrl_init();
+	if (rc) {
+		pr_err("%s: bpf_smc_hs_ctrl_init fails with %d\n", __func__,
+		       rc);
+		goto out_inet;
+	}
+
 	static_branch_enable(&tcp_have_smc);
 	return 0;
+out_inet:
+	smc_inet_exit();
 out_ulp:
 	tcp_unregister_ulp(&smc_ulp_ops);
 out_lo:
net/smc/smc_hs_bpf.c new file +137 -0

137 lines not on this page · raw message

net/smc/smc_hs_bpf.h new file +31 -0
new file mode 100644
index 000000000000..f5f1807c079e
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ *  Shared Memory Communications over RDMA (SMC-R) and RoCE
+ *
+ *  Generic hook for SMC handshake flow.
+ *
+ *  Copyright IBM Corp. 2016
+ *  Copyright (c) 2025, Alibaba Inc.
+ *
+ *  Author: D. Wythe <[email protected]>
+ */
+
+#ifndef __SMC_HS_CTRL
+#define __SMC_HS_CTRL
+
+#include <net/smc.h>
+
+/* Find hs_ctrl by the target name, which required to be a c-string.
+ * Return NULL if no such ctrl was found,otherwise, return a valid ctrl.
+ *
+ * Note: Caller MUST ensure it's was invoked under rcu_read_lock.
+ */
+struct smc_hs_ctrl *smc_hs_ctrl_find_by_name(const char *name);
+
+#if IS_ENABLED(CONFIG_SMC_HS_CTRL_BPF)
+int bpf_smc_hs_ctrl_init(void);
+#else
+static inline int bpf_smc_hs_ctrl_init(void) { return 0; }
+#endif /* CONFIG_SMC_HS_CTRL_BPF */
+
+#endif /* __SMC_HS_CTRL */
net/smc/smc_sysctl.c +91 -0

133 lines not on this page · raw message

Signature · 1 line
2.45.0
#3 lore raw

Re: [PATCH bpf-next v2 2/4] net/smc: bpf: Introduce generic hook for handshake flow

kernel test robot noticed the following build warnings:

Hi Wythe,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/D-Wythe/bpf-export-necessary-symbols-for-modules-with-struct_ops/20250918-160530
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20250918080342.25041-3-alibuda%40linux.alibaba.com
patch subject: [PATCH bpf-next v2 2/4] net/smc: bpf: Introduce generic hook for handshake flow
config: i386-randconfig-063-20250919 (https://download.01.org/0day-ci/archive/20250919/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250919/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> net/ipv4/tcp_output.c:776:33: sparse: sparse: incorrect type in assignment (different address spaces) @@     expected struct smc_hs_ctrl *ctrl @@     got struct smc_hs_ctrl [noderef] __rcu * @@

net/ipv4/tcp_output.c:776:33: sparse: expected struct smc_hs_ctrl *ctrl
net/ipv4/tcp_output.c:776:33: sparse: got struct smc_hs_ctrl [noderef] __rcu *
net/ipv4/tcp_output.c:796:34: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct smc_hs_ctrl *ctrl @@ got struct smc_hs_ctrl [noderef] __rcu * @@
net/ipv4/tcp_output.c:796:34: sparse: expected struct smc_hs_ctrl *ctrl
net/ipv4/tcp_output.c:796:34: sparse: got struct smc_hs_ctrl [noderef] __rcu *
--

>> net/smc/smc_sysctl.c:59:16: sparse: sparse: incorrect type in initializer (different address spaces) @@     expected struct smc_hs_ctrl [noderef] __rcu *__ret @@     got struct smc_hs_ctrl *[assigned] ctrl @@
   net/smc/smc_sysctl.c:59:16: sparse:     expected struct smc_hs_ctrl [noderef] __rcu *__ret
   net/smc/smc_sysctl.c:59:16: sparse:     got struct smc_hs_ctrl *[assigned] ctrl
>> net/smc/smc_sysctl.c:59:14: sparse: sparse: incorrect type in assignment (different address spaces) @@     expected struct smc_hs_ctrl *[assigned] ctrl @@     got struct smc_hs_ctrl [noderef] __rcu *[assigned] __ret @@

net/smc/smc_sysctl.c:59:14: sparse: expected struct smc_hs_ctrl *[assigned] ctrl
net/smc/smc_sysctl.c:59:14: sparse: got struct smc_hs_ctrl [noderef] __rcu *[assigned] __ret

vim +776 net/ipv4/tcp_output.c

767
768 static void smc_set_option(struct tcp_sock *tp,
769 struct tcp_out_options *opts,
770 unsigned int *remaining)
771 {
772 #if IS_ENABLED(CONFIG_SMC)
773 struct sock *sk = &tp->inet_conn.icsk_inet.sk;
774
775 if (static_branch_unlikely(&tcp_have_smc) && tp->syn_smc) {

 > 776			tp->syn_smc = !!smc_call_hsbpf(1, sk, syn_option, tp);

777 /* re-check syn_smc */
778 if (tp->syn_smc &&
779 *remaining >= TCPOLEN_EXP_SMC_BASE_ALIGNED) {
780 opts->options |= OPTION_SMC;
781 *remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED;
782 }
783 }
784 #endif
785 }
786

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#4 lore raw

PATCH v2 3/4 libbpf: fix error when st-prefix_ops and ops from differ btf

In reply to D. Wythe

When a struct_ops named xxx_ops was registered by a module, and it will be used in both built-in modules and the module itself, so that the btf_type of xxx_ops will be present in btf_vmlinux instead of in btf_mod, which means that the btf_type of bpf_struct_ops_xxx_ops and xxx_ops will not be in the same btf.

Here are four possible case:

+--------+---------------+-------------+---------------------------------+
|        | st_ops_xxx_ops| xxx_ops     |                                 |
+--------+---------------+-------------+---------------------------------+
| case 0 | btf_vmlinux   | bft_vmlinux | be used and reg only in vmlinux |
+--------+---------------+-------------+---------------------------------+
| case 1 | btf_vmlinux   | bpf_mod     | INVALID                         |
+--------+---------------+-------------+---------------------------------+
| case 2 | btf_mod       | btf_vmlinux | reg in mod but be used both in  |
|        |               |             | vmlinux and mod.                |
+--------+---------------+-------------+---------------------------------+
| case 3 | btf_mod       | btf_mod     | be used and reg only in mod     |
+--------+---------------+-------------+---------------------------------+

At present, cases 0, 1, and 3 can be correctly identified, because st_ops_xxx_ops is searched from the same btf with xxx_ops. In order to handle case 2 correctly without affecting other cases, we cannot simply change the search method for st_ops_xxx_ops from find_btf_by_prefix_kind() to find_ksym_btf_id(), because in this way, case 1 will not be recognized anymore.

To address the issue, we always look for st_ops_xxx_ops first, figure out the btf, and then look for xxx_ops with the very btf to avoid such issue.

Fixes 590a00888250 ("bpf: libbpf: Add STRUCT_OPS support")
---
 tools/lib/bpf/libbpf.c | 37 ++++++++++++++++++-------------------
 1 file changed, 18 insertions(+), 19 deletions(-)
tools/lib/bpf/libbpf.c +18 -19
index fe4fc5438678..50ca13833511 100644
@@ -1013,35 +1013,34 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw,
 	const struct btf_member *kern_data_member;
 	struct btf *btf = NULL;
 	__s32 kern_vtype_id, kern_type_id;
-	char tname[256];
+	char tname[256], stname[256];
 	__u32 i;
 
 	snprintf(tname, sizeof(tname), "%.*s",
 		 (int)bpf_core_essential_name_len(tname_raw), tname_raw);
 
-	kern_type_id = find_ksym_btf_id(obj, tname, BTF_KIND_STRUCT,
-					&btf, mod_btf);
-	if (kern_type_id < 0) {
-		pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
-			tname);
-		return kern_type_id;
-	}
-	kern_type = btf__type_by_id(btf, kern_type_id);
+	snprintf(stname, sizeof(stname), "%s%.*s", STRUCT_OPS_VALUE_PREFIX,
+		 (int)strlen(tname), tname);
 
-	/* Find the corresponding "map_value" type that will be used
-	 * in map_update(BPF_MAP_TYPE_STRUCT_OPS).  For example,
-	 * find "struct bpf_struct_ops_tcp_congestion_ops" from the
-	 * btf_vmlinux.
+	/* Look for the corresponding "map_value" type that will be used
+	 * in map_update(BPF_MAP_TYPE_STRUCT_OPS) first, figure out the btf
+	 * and the mod_btf.
+	 * For example, find "struct bpf_struct_ops_tcp_congestion_ops".
 	 */
-	kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX,
-						tname, BTF_KIND_STRUCT);
+	kern_vtype_id = find_ksym_btf_id(obj, stname, BTF_KIND_STRUCT, &btf, mod_btf);
 	if (kern_vtype_id < 0) {
-		pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n",
-			STRUCT_OPS_VALUE_PREFIX, tname);
+		pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", stname);
 		return kern_vtype_id;
 	}
 	kern_vtype = btf__type_by_id(btf, kern_vtype_id);
 
+	kern_type_id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT);
+	if (kern_type_id < 0) {
+		pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", tname);
+		return kern_type_id;
+	}
+	kern_type = btf__type_by_id(btf, kern_type_id);
+
 	/* Find "struct tcp_congestion_ops" from
 	 * struct bpf_struct_ops_tcp_congestion_ops {
 	 *	[ ... ]
@@ -1054,8 +1053,8 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw,
 			break;
 	}
 	if (i == btf_vlen(kern_vtype)) {
-		pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n",
-			tname, STRUCT_OPS_VALUE_PREFIX, tname);
+		pr_warn("struct_ops init_kern: struct %s data is not found in struct %s\n",
+			tname, stname);
 		return -EINVAL;
 	}
 
Signature · 1 line
2.45.0
#5 lore raw

Re: [PATCH bpf-next v2 3/4] libbpf: fix error when st-prefix_ops and ops from differ btf

Quoted 29 lines · D. Wythe

When a struct_ops named xxx_ops was registered by a module, and it will be used in both built-in modules and the module itself, so that the btf_type of xxx_ops will be present in btf_vmlinux instead of in btf_mod, which means that the btf_type of bpf_struct_ops_xxx_ops and xxx_ops will not be in the same btf.

Here are four possible case:

+--------+---------------+-------------+---------------------------------+
|        | st_ops_xxx_ops| xxx_ops     |                                 |
+--------+---------------+-------------+---------------------------------+
| case 0 | btf_vmlinux   | bft_vmlinux | be used and reg only in vmlinux |
+--------+---------------+-------------+---------------------------------+
| case 1 | btf_vmlinux   | bpf_mod     | INVALID                         |
+--------+---------------+-------------+---------------------------------+
| case 2 | btf_mod       | btf_vmlinux | reg in mod but be used both in  |
|        |               |             | vmlinux and mod.                |
+--------+---------------+-------------+---------------------------------+
| case 3 | btf_mod       | btf_mod     | be used and reg only in mod     |
+--------+---------------+-------------+---------------------------------+

At present, cases 0, 1, and 3 can be correctly identified, because st_ops_xxx_ops is searched from the same btf with xxx_ops. In order to handle case 2 correctly without affecting other cases, we cannot simply change the search method for st_ops_xxx_ops from find_btf_by_prefix_kind() to find_ksym_btf_id(), because in this way, case 1 will not be recognized anymore.

Quoted 2 lines

To address the issue, we always look for st_ops_xxx_ops first, figure out the btf, and then look for xxx_ops with the very btf to avoid

What's "very btf"? Commit message would benefit from a little bit of proof-reading, if you can. It's a bit hard to follow, even if it's more or less clear at the end what problem you are trying to solve.

Also, I'd suggest to send this fix as a separate patch and not block it on the overall patch set, which probably will take longer. This fix is independent, so we can land it much faster.

Quoted 79 lines

such issue.

Fixes 590a00888250 ("bpf: libbpf: Add STRUCT_OPS support")
---
 tools/lib/bpf/libbpf.c | 37 ++++++++++++++++++-------------------
 1 file changed, 18 insertions(+), 19 deletions(-)
tools/lib/bpf/libbpf.c +18 -20
index fe4fc5438678..50ca13833511 100644
@@ -1013,35 +1013,34 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw,
        const struct btf_member *kern_data_member;
        struct btf *btf = NULL;
        __s32 kern_vtype_id, kern_type_id;
-       char tname[256];
+       char tname[256], stname[256];
        __u32 i;
 
        snprintf(tname, sizeof(tname), "%.*s",
                 (int)bpf_core_essential_name_len(tname_raw), tname_raw);
 
-       kern_type_id = find_ksym_btf_id(obj, tname, BTF_KIND_STRUCT,
-                                       &btf, mod_btf);
-       if (kern_type_id < 0) {
-               pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
-                       tname);
-               return kern_type_id;
-       }
-       kern_type = btf__type_by_id(btf, kern_type_id);
+       snprintf(stname, sizeof(stname), "%s%.*s", STRUCT_OPS_VALUE_PREFIX,
+                (int)strlen(tname), tname);
 
-       /* Find the corresponding "map_value" type that will be used
-        * in map_update(BPF_MAP_TYPE_STRUCT_OPS).  For example,
-        * find "struct bpf_struct_ops_tcp_congestion_ops" from the
-        * btf_vmlinux.
+       /* Look for the corresponding "map_value" type that will be used
+        * in map_update(BPF_MAP_TYPE_STRUCT_OPS) first, figure out the btf
+        * and the mod_btf.
+        * For example, find "struct bpf_struct_ops_tcp_congestion_ops".
         */
-       kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX,
-                                               tname, BTF_KIND_STRUCT);
+       kern_vtype_id = find_ksym_btf_id(obj, stname, BTF_KIND_STRUCT, &btf, mod_btf);
        if (kern_vtype_id < 0) {
-               pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n",
-                       STRUCT_OPS_VALUE_PREFIX, tname);
+               pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", stname);
                return kern_vtype_id;
        }
        kern_vtype = btf__type_by_id(btf, kern_vtype_id);
 
+       kern_type_id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT);
+       if (kern_type_id < 0) {
+               pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", tname);
+               return kern_type_id;
+       }
+       kern_type = btf__type_by_id(btf, kern_type_id);
+
        /* Find "struct tcp_congestion_ops" from
         * struct bpf_struct_ops_tcp_congestion_ops {
         *      [ ... ]
@@ -1054,8 +1053,8 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw,
                        break;
        }
        if (i == btf_vlen(kern_vtype)) {
-               pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n",
-                       tname, STRUCT_OPS_VALUE_PREFIX, tname);
+               pr_warn("struct_ops init_kern: struct %s data is not found in struct %s\n",
+                       tname, stname);
                return -EINVAL;
        }
 
--

2.45.0

author maintainer
#6 lore raw

Re: [PATCH bpf-next v2 3/4] libbpf: fix error when st-prefix_ops and ops from differ btf

Quoted 13 lines · Andrii Nakryiko
Quoted 11 lines · D. Wythe

At present, cases 0, 1, and 3 can be correctly identified, because st_ops_xxx_ops is searched from the same btf with xxx_ops. In order to handle case 2 correctly without affecting other cases, we cannot simply change the search method for st_ops_xxx_ops from find_btf_by_prefix_kind() to find_ksym_btf_id(), because in this way, case 1 will not be recognized anymore.

To address the issue, we always look for st_ops_xxx_ops first, figure out the btf, and then look for xxx_ops with the very btf to avoid

Quoted 8 lines

What's "very btf"? Commit message would benefit from a little bit of proof-reading, if you can. It's a bit hard to follow, even if it's more or less clear at the end what problem you are trying to solve.

Also, I'd suggest to send this fix as a separate patch and not block it on the overall patch set, which probably will take longer. This fix is independent, so we can land it much faster.

Thanks a lot for your suggestion. I'll improve the commit message.

It's fine to send this patch separately, I'm concerned it won't be verifiable in isolation without the other patches. But if you feel it's okay to proceed, I'm happy to do so.

Quoted 79 lines
Quoted 79 lines

such issue.

Fixes 590a00888250 ("bpf: libbpf: Add STRUCT_OPS support")
---
 tools/lib/bpf/libbpf.c | 37 ++++++++++++++++++-------------------
 1 file changed, 18 insertions(+), 19 deletions(-)
tools/lib/bpf/libbpf.c +18 -20
index fe4fc5438678..50ca13833511 100644
@@ -1013,35 +1013,34 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw,
        const struct btf_member *kern_data_member;
        struct btf *btf = NULL;
        __s32 kern_vtype_id, kern_type_id;
-       char tname[256];
+       char tname[256], stname[256];
        __u32 i;
 
        snprintf(tname, sizeof(tname), "%.*s",
                 (int)bpf_core_essential_name_len(tname_raw), tname_raw);
 
-       kern_type_id = find_ksym_btf_id(obj, tname, BTF_KIND_STRUCT,
-                                       &btf, mod_btf);
-       if (kern_type_id < 0) {
-               pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
-                       tname);
-               return kern_type_id;
-       }
-       kern_type = btf__type_by_id(btf, kern_type_id);
+       snprintf(stname, sizeof(stname), "%s%.*s", STRUCT_OPS_VALUE_PREFIX,
+                (int)strlen(tname), tname);
 
-       /* Find the corresponding "map_value" type that will be used
-        * in map_update(BPF_MAP_TYPE_STRUCT_OPS).  For example,
-        * find "struct bpf_struct_ops_tcp_congestion_ops" from the
-        * btf_vmlinux.
+       /* Look for the corresponding "map_value" type that will be used
+        * in map_update(BPF_MAP_TYPE_STRUCT_OPS) first, figure out the btf
+        * and the mod_btf.
+        * For example, find "struct bpf_struct_ops_tcp_congestion_ops".
         */
-       kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX,
-                                               tname, BTF_KIND_STRUCT);
+       kern_vtype_id = find_ksym_btf_id(obj, stname, BTF_KIND_STRUCT, &btf, mod_btf);
        if (kern_vtype_id < 0) {
-               pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n",
-                       STRUCT_OPS_VALUE_PREFIX, tname);
+               pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", stname);
                return kern_vtype_id;
        }
        kern_vtype = btf__type_by_id(btf, kern_vtype_id);
 
+       kern_type_id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT);
+       if (kern_type_id < 0) {
+               pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", tname);
+               return kern_type_id;
+       }
+       kern_type = btf__type_by_id(btf, kern_type_id);
+
        /* Find "struct tcp_congestion_ops" from
         * struct bpf_struct_ops_tcp_congestion_ops {
         *      [ ... ]
@@ -1054,8 +1053,8 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw,
                        break;
        }
        if (i == btf_vlen(kern_vtype)) {
-               pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n",
-                       tname, STRUCT_OPS_VALUE_PREFIX, tname);
+               pr_warn("struct_ops init_kern: struct %s data is not found in struct %s\n",
+                       tname, stname);
                return -EINVAL;
        }
 
--

2.45.0

#7 lore raw

Re: [PATCH bpf-next v2 3/4] libbpf: fix error when st-prefix_ops and ops from differ btf

Quoted 24 lines · D. Wythe
Quoted 13 lines · Andrii Nakryiko
Quoted 11 lines · D. Wythe

At present, cases 0, 1, and 3 can be correctly identified, because st_ops_xxx_ops is searched from the same btf with xxx_ops. In order to handle case 2 correctly without affecting other cases, we cannot simply change the search method for st_ops_xxx_ops from find_btf_by_prefix_kind() to find_ksym_btf_id(), because in this way, case 1 will not be recognized anymore.

To address the issue, we always look for st_ops_xxx_ops first, figure out the btf, and then look for xxx_ops with the very btf to avoid

Quoted 8 lines

What's "very btf"? Commit message would benefit from a little bit of proof-reading, if you can. It's a bit hard to follow, even if it's more or less clear at the end what problem you are trying to solve.

Also, I'd suggest to send this fix as a separate patch and not block it on the overall patch set, which probably will take longer. This fix is independent, so we can land it much faster.

Quoted 5 lines

Thanks a lot for your suggestion. I'll improve the commit message.

It's fine to send this patch separately, I'm concerned it won't be verifiable in isolation without the other patches. But if you feel it's okay to proceed, I'm happy to do so.

I think the issue is pretty clear, I don't mind fixing it outside of the patch set introducing this mixed vmlinux vs module BTF. So yes, please send a patch (unless you already did, as I'm catching up on emails)

Quoted 80 lines
Quoted 80 lines
Quoted 80 lines

such issue.

Fixes 590a00888250 ("bpf: libbpf: Add STRUCT_OPS support")
---
 tools/lib/bpf/libbpf.c | 37 ++++++++++++++++++-------------------
 1 file changed, 18 insertions(+), 19 deletions(-)
tools/lib/bpf/libbpf.c +18 -20
index fe4fc5438678..50ca13833511 100644
@@ -1013,35 +1013,34 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw,
        const struct btf_member *kern_data_member;
        struct btf *btf = NULL;
        __s32 kern_vtype_id, kern_type_id;
-       char tname[256];
+       char tname[256], stname[256];
        __u32 i;
 
        snprintf(tname, sizeof(tname), "%.*s",
                 (int)bpf_core_essential_name_len(tname_raw), tname_raw);
 
-       kern_type_id = find_ksym_btf_id(obj, tname, BTF_KIND_STRUCT,
-                                       &btf, mod_btf);
-       if (kern_type_id < 0) {
-               pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
-                       tname);
-               return kern_type_id;
-       }
-       kern_type = btf__type_by_id(btf, kern_type_id);
+       snprintf(stname, sizeof(stname), "%s%.*s", STRUCT_OPS_VALUE_PREFIX,
+                (int)strlen(tname), tname);
 
-       /* Find the corresponding "map_value" type that will be used
-        * in map_update(BPF_MAP_TYPE_STRUCT_OPS).  For example,
-        * find "struct bpf_struct_ops_tcp_congestion_ops" from the
-        * btf_vmlinux.
+       /* Look for the corresponding "map_value" type that will be used
+        * in map_update(BPF_MAP_TYPE_STRUCT_OPS) first, figure out the btf
+        * and the mod_btf.
+        * For example, find "struct bpf_struct_ops_tcp_congestion_ops".
         */
-       kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX,
-                                               tname, BTF_KIND_STRUCT);
+       kern_vtype_id = find_ksym_btf_id(obj, stname, BTF_KIND_STRUCT, &btf, mod_btf);
        if (kern_vtype_id < 0) {
-               pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n",
-                       STRUCT_OPS_VALUE_PREFIX, tname);
+               pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", stname);
                return kern_vtype_id;
        }
        kern_vtype = btf__type_by_id(btf, kern_vtype_id);
 
+       kern_type_id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT);
+       if (kern_type_id < 0) {
+               pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", tname);
+               return kern_type_id;
+       }
+       kern_type = btf__type_by_id(btf, kern_type_id);
+
        /* Find "struct tcp_congestion_ops" from
         * struct bpf_struct_ops_tcp_congestion_ops {
         *      [ ... ]
@@ -1054,8 +1053,8 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw,
                        break;
        }
        if (i == btf_vlen(kern_vtype)) {
-               pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n",
-                       tname, STRUCT_OPS_VALUE_PREFIX, tname);
+               pr_warn("struct_ops init_kern: struct %s data is not found in struct %s\n",
+                       tname, stname);
                return -EINVAL;
        }
 
--

2.45.0

author maintainer
#8 lore raw

PATCH v2 4/4 bpf/selftests: add selftest for bpf_smc_hs_ctrl

In reply to D. Wythe

This tests introduces a tiny smc_hs_ctrl for filtering SMC connections based on IP pairs, and also adds a realistic topology model to verify it.

Also, we can only use SMC loopback under CI test, so an additional configuration needs to be enabled.

Follow the steps below to run this test.

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

Results shows:
Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED

---
 tools/testing/selftests/bpf/config            |   4 +
 .../selftests/bpf/prog_tests/test_bpf_smc.c   | 396 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/bpf_smc.c   | 117 ++++++
 3 files changed, 517 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_bpf_smc.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_smc.c
tools/testing/selftests/bpf/config +4 -0
index 70b28c1e653e..9a0cee79fc23 100644
@@ -123,3 +123,7 @@ CONFIG_XDP_SOCKETS=y
 CONFIG_XFRM_INTERFACE=y
 CONFIG_TCP_CONG_DCTCP=y
 CONFIG_TCP_CONG_BBR=y
+CONFIG_INFINIBAND=y
+CONFIG_SMC=y
+CONFIG_SMC_HS_CTRL_BPF=y
+CONFIG_SMC_LO=y
\ No newline at end of file
tools/testing/selftests/bpf/prog_tests/test_bpf_smc.c new file +396 -0

396 lines not on this page · raw message

tools/testing/selftests/bpf/progs/bpf_smc.c new file +117 -0

117 lines not on this page · raw message

Signature · 1 line
2.45.0