Skip to content

Commit cd660ee

Browse files
D-WytheKernel Patches Daemon
authored andcommitted
net/smc: bpf: Introduce generic hook for handshake flow
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. Signed-off-by: D. Wythe <[email protected]> Reviewed-by: Dust Li <[email protected]>
1 parent 7d75b83 commit cd660ee

File tree

9 files changed

+358
-14
lines changed

9 files changed

+358
-14
lines changed

include/net/netns/smc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ struct netns_smc {
1717
#ifdef CONFIG_SYSCTL
1818
struct ctl_table_header *smc_hdr;
1919
#endif
20+
#if IS_ENABLED(CONFIG_SMC_HS_CTRL_BPF)
21+
struct smc_hs_ctrl __rcu *hs_ctrl;
22+
#endif /* CONFIG_SMC_HS_CTRL_BPF */
2023
unsigned int sysctl_autocorking_size;
2124
unsigned int sysctl_smcr_buf_type;
2225
int sysctl_smcr_testlink_time;

include/net/smc.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "linux/ism.h"
1919

2020
struct sock;
21+
struct tcp_sock;
22+
struct inet_request_sock;
2123

2224
#define SMC_MAX_PNETID_LEN 16 /* Max. length of PNET id */
2325

@@ -97,4 +99,55 @@ struct smcd_dev {
9799
u8 going_away : 1;
98100
};
99101

102+
#define SMC_HS_CTRL_NAME_MAX 16
103+
104+
enum {
105+
/* ops can be inherit from init_net */
106+
SMC_HS_CTRL_FLAG_INHERITABLE = 0x1,
107+
108+
SMC_HS_CTRL_ALL_FLAGS = SMC_HS_CTRL_FLAG_INHERITABLE,
109+
};
110+
111+
struct smc_hs_ctrl {
112+
/* private */
113+
114+
struct list_head list;
115+
struct module *owner;
116+
117+
/* public */
118+
119+
/* unique name */
120+
char name[SMC_HS_CTRL_NAME_MAX];
121+
int flags;
122+
123+
/* Invoked before computing SMC option for SYN packets.
124+
* We can control whether to set SMC options by returning various value.
125+
* Return 0 to disable SMC, or return any other value to enable it.
126+
*/
127+
int (*syn_option)(struct tcp_sock *tp);
128+
129+
/* Invoked before Set up SMC options for SYN-ACK packets
130+
* We can control whether to respond SMC options by returning various
131+
* value. Return 0 to disable SMC, or return any other value to enable
132+
* it.
133+
*/
134+
int (*synack_option)(const struct tcp_sock *tp,
135+
struct inet_request_sock *ireq);
136+
};
137+
138+
#if IS_ENABLED(CONFIG_SMC_HS_CTRL_BPF)
139+
#define smc_call_hsbpf(init_val, sk, func, ...) ({ \
140+
typeof(init_val) __ret = (init_val); \
141+
struct smc_hs_ctrl *ctrl; \
142+
rcu_read_lock(); \
143+
ctrl = rcu_dereference(sock_net(sk)->smc.hs_ctrl); \
144+
if (ctrl && ctrl->func) \
145+
__ret = ctrl->func(__VA_ARGS__); \
146+
rcu_read_unlock(); \
147+
__ret; \
148+
})
149+
#else
150+
#define smc_call_hsbpf(init_val, sk, ...) ({ (void)(sk); (init_val); })
151+
#endif /* CONFIG_SMC_HS_CTRL_BPF */
152+
100153
#endif /* _SMC_H */

net/ipv4/tcp_output.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
#include <net/tcp.h>
4141
#include <net/mptcp.h>
42+
#include <net/smc.h>
4243
#include <net/proto_memory.h>
4344

4445
#include <linux/compiler.h>
@@ -764,34 +765,41 @@ static void tcp_options_write(struct tcphdr *th, struct tcp_sock *tp,
764765
mptcp_options_write(th, ptr, tp, opts);
765766
}
766767

767-
static void smc_set_option(const struct tcp_sock *tp,
768+
static void smc_set_option(struct tcp_sock *tp,
768769
struct tcp_out_options *opts,
769770
unsigned int *remaining)
770771
{
771772
#if IS_ENABLED(CONFIG_SMC)
772-
if (static_branch_unlikely(&tcp_have_smc)) {
773-
if (tp->syn_smc) {
774-
if (*remaining >= TCPOLEN_EXP_SMC_BASE_ALIGNED) {
775-
opts->options |= OPTION_SMC;
776-
*remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED;
777-
}
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;
778782
}
779783
}
780784
#endif
781785
}
782786

783787
static void smc_set_option_cond(const struct tcp_sock *tp,
784-
const struct inet_request_sock *ireq,
788+
struct inet_request_sock *ireq,
785789
struct tcp_out_options *opts,
786790
unsigned int *remaining)
787791
{
788792
#if IS_ENABLED(CONFIG_SMC)
789-
if (static_branch_unlikely(&tcp_have_smc)) {
790-
if (tp->syn_smc && ireq->smc_ok) {
791-
if (*remaining >= TCPOLEN_EXP_SMC_BASE_ALIGNED) {
792-
opts->options |= OPTION_SMC;
793-
*remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED;
794-
}
793+
const struct sock *sk = &tp->inet_conn.icsk_inet.sk;
794+
795+
if (static_branch_unlikely(&tcp_have_smc) && tp->syn_smc && ireq->smc_ok) {
796+
ireq->smc_ok = !!smc_call_hsbpf(1, sk, synack_option,
797+
tp, ireq);
798+
/* re-check smc_ok */
799+
if (ireq->smc_ok &&
800+
*remaining >= TCPOLEN_EXP_SMC_BASE_ALIGNED) {
801+
opts->options |= OPTION_SMC;
802+
*remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED;
795803
}
796804
}
797805
#endif

net/smc/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,13 @@ config SMC_LO
3333
of architecture or hardware.
3434

3535
if unsure, say N.
36+
37+
config SMC_HS_CTRL_BPF
38+
bool "Generic eBPF hook for SMC handshake flow"
39+
depends on SMC && BPF_SYSCALL
40+
default y
41+
help
42+
SMC_HS_CTRL_BPF enables support to register generic eBPF hook for SMC
43+
handshake flow, which offer much greater flexibility in modifying the behavior
44+
of the SMC protocol stack compared to a complete kernel-based approach. Select
45+
this option if you want filtring the handshake process via eBPF programs.

net/smc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
77
smc-y += smc_tracepoint.o smc_inet.o
88
smc-$(CONFIG_SYSCTL) += smc_sysctl.o
99
smc-$(CONFIG_SMC_LO) += smc_loopback.o
10+
smc-$(CONFIG_SMC_HS_CTRL_BPF) += smc_hs_bpf.o

net/smc/af_smc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "smc_sysctl.h"
6060
#include "smc_loopback.h"
6161
#include "smc_inet.h"
62+
#include "smc_hs_bpf.h"
6263

6364
static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
6465
* creation on server
@@ -3609,8 +3610,17 @@ static int __init smc_init(void)
36093610
pr_err("%s: smc_inet_init fails with %d\n", __func__, rc);
36103611
goto out_ulp;
36113612
}
3613+
rc = bpf_smc_hs_ctrl_init();
3614+
if (rc) {
3615+
pr_err("%s: bpf_smc_hs_ctrl_init fails with %d\n", __func__,
3616+
rc);
3617+
goto out_inet;
3618+
}
3619+
36123620
static_branch_enable(&tcp_have_smc);
36133621
return 0;
3622+
out_inet:
3623+
smc_inet_exit();
36143624
out_ulp:
36153625
tcp_unregister_ulp(&smc_ulp_ops);
36163626
out_lo:

net/smc/smc_hs_bpf.c

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Shared Memory Communications over RDMA (SMC-R) and RoCE
4+
*
5+
* Generic hook for SMC handshake flow.
6+
*
7+
* Copyright IBM Corp. 2016
8+
* Copyright (c) 2025, Alibaba Inc.
9+
*
10+
* Author: D. Wythe <[email protected]>
11+
*/
12+
13+
#include <linux/bpf_verifier.h>
14+
#include <linux/bpf.h>
15+
#include <linux/btf.h>
16+
#include <linux/rculist.h>
17+
18+
#include "smc_hs_bpf.h"
19+
20+
static DEFINE_SPINLOCK(smc_hs_ctrl_list_lock);
21+
static LIST_HEAD(smc_hs_ctrl_list);
22+
23+
static int smc_hs_ctrl_reg(struct smc_hs_ctrl *ctrl)
24+
{
25+
int ret = 0;
26+
27+
spin_lock(&smc_hs_ctrl_list_lock);
28+
/* already exist or duplicate name */
29+
if (smc_hs_ctrl_find_by_name(ctrl->name))
30+
ret = -EEXIST;
31+
else
32+
list_add_tail_rcu(&ctrl->list, &smc_hs_ctrl_list);
33+
spin_unlock(&smc_hs_ctrl_list_lock);
34+
return ret;
35+
}
36+
37+
static void smc_hs_ctrl_unreg(struct smc_hs_ctrl *ctrl)
38+
{
39+
spin_lock(&smc_hs_ctrl_list_lock);
40+
list_del_rcu(&ctrl->list);
41+
spin_unlock(&smc_hs_ctrl_list_lock);
42+
43+
/* Ensure that all readers to complete */
44+
synchronize_rcu();
45+
}
46+
47+
struct smc_hs_ctrl *smc_hs_ctrl_find_by_name(const char *name)
48+
{
49+
struct smc_hs_ctrl *ctrl;
50+
51+
list_for_each_entry_rcu(ctrl, &smc_hs_ctrl_list, list) {
52+
if (strcmp(ctrl->name, name) == 0)
53+
return ctrl;
54+
}
55+
return NULL;
56+
}
57+
58+
static int __smc_bpf_stub_set_tcp_option(struct tcp_sock *tp) { return 1; }
59+
static int __smc_bpf_stub_set_tcp_option_cond(const struct tcp_sock *tp,
60+
struct inet_request_sock *ireq)
61+
{
62+
return 1;
63+
}
64+
65+
static struct smc_hs_ctrl __smc_bpf_hs_ctrl = {
66+
.syn_option = __smc_bpf_stub_set_tcp_option,
67+
.synack_option = __smc_bpf_stub_set_tcp_option_cond,
68+
};
69+
70+
static int smc_bpf_hs_ctrl_init(struct btf *btf) { return 0; }
71+
72+
static int smc_bpf_hs_ctrl_reg(void *kdata, struct bpf_link *link)
73+
{
74+
return smc_hs_ctrl_reg(kdata);
75+
}
76+
77+
static void smc_bpf_hs_ctrl_unreg(void *kdata, struct bpf_link *link)
78+
{
79+
smc_hs_ctrl_unreg(kdata);
80+
}
81+
82+
static int smc_bpf_hs_ctrl_init_member(const struct btf_type *t,
83+
const struct btf_member *member,
84+
void *kdata, const void *udata)
85+
{
86+
const struct smc_hs_ctrl *u_ctrl;
87+
struct smc_hs_ctrl *k_ctrl;
88+
u32 moff;
89+
90+
u_ctrl = (const struct smc_hs_ctrl *)udata;
91+
k_ctrl = (struct smc_hs_ctrl *)kdata;
92+
93+
moff = __btf_member_bit_offset(t, member) / 8;
94+
switch (moff) {
95+
case offsetof(struct smc_hs_ctrl, name):
96+
if (bpf_obj_name_cpy(k_ctrl->name, u_ctrl->name,
97+
sizeof(u_ctrl->name)) <= 0)
98+
return -EINVAL;
99+
return 1;
100+
case offsetof(struct smc_hs_ctrl, flags):
101+
if (u_ctrl->flags & ~SMC_HS_CTRL_ALL_FLAGS)
102+
return -EINVAL;
103+
k_ctrl->flags = u_ctrl->flags;
104+
return 1;
105+
default:
106+
break;
107+
}
108+
109+
return 0;
110+
}
111+
112+
static const struct bpf_func_proto *
113+
bpf_smc_hs_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
114+
{
115+
return bpf_base_func_proto(func_id, prog);
116+
}
117+
118+
static const struct bpf_verifier_ops smc_bpf_verifier_ops = {
119+
.get_func_proto = bpf_smc_hs_func_proto,
120+
.is_valid_access = bpf_tracing_btf_ctx_access,
121+
};
122+
123+
static struct bpf_struct_ops bpf_smc_hs_ctrl_ops = {
124+
.name = "smc_hs_ctrl",
125+
.init = smc_bpf_hs_ctrl_init,
126+
.reg = smc_bpf_hs_ctrl_reg,
127+
.unreg = smc_bpf_hs_ctrl_unreg,
128+
.cfi_stubs = &__smc_bpf_hs_ctrl,
129+
.verifier_ops = &smc_bpf_verifier_ops,
130+
.init_member = smc_bpf_hs_ctrl_init_member,
131+
.owner = THIS_MODULE,
132+
};
133+
134+
int bpf_smc_hs_ctrl_init(void)
135+
{
136+
return register_bpf_struct_ops(&bpf_smc_hs_ctrl_ops, smc_hs_ctrl);
137+
}

net/smc/smc_hs_bpf.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Shared Memory Communications over RDMA (SMC-R) and RoCE
4+
*
5+
* Generic hook for SMC handshake flow.
6+
*
7+
* Copyright IBM Corp. 2016
8+
* Copyright (c) 2025, Alibaba Inc.
9+
*
10+
* Author: D. Wythe <[email protected]>
11+
*/
12+
13+
#ifndef __SMC_HS_CTRL
14+
#define __SMC_HS_CTRL
15+
16+
#include <net/smc.h>
17+
18+
/* Find hs_ctrl by the target name, which required to be a c-string.
19+
* Return NULL if no such ctrl was found,otherwise, return a valid ctrl.
20+
*
21+
* Note: Caller MUST ensure it's was invoked under rcu_read_lock.
22+
*/
23+
struct smc_hs_ctrl *smc_hs_ctrl_find_by_name(const char *name);
24+
25+
#if IS_ENABLED(CONFIG_SMC_HS_CTRL_BPF)
26+
int bpf_smc_hs_ctrl_init(void);
27+
#else
28+
static inline int bpf_smc_hs_ctrl_init(void) { return 0; }
29+
#endif /* CONFIG_SMC_HS_CTRL_BPF */
30+
31+
#endif /* __SMC_HS_CTRL */

0 commit comments

Comments
 (0)