Skip to content

Commit e4356a2

Browse files
D. WytheKernel Patches Daemon
authored andcommitted
net/smc: bpf: register smc_ops info struct_ops
To implement injection capability for smc via struct_ops, so that user can make their own smc_ops to modify the behavior of smc stack. Currently, user can write their own implememtion to choose whether to use SMC or not before TCP 3rd handshake to be comleted. In the future, users can implement more complex functions on smc by expanding it. Signed-off-by: D. Wythe <alibuda@linux.alibaba.com>
1 parent 15c2daf commit e4356a2

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

net/smc/af_smc.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "smc_sysctl.h"
5656
#include "smc_loopback.h"
5757
#include "smc_inet.h"
58+
#include "smc_ops.h"
5859

5960
static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
6061
* creation on server
@@ -3576,8 +3577,18 @@ static int __init smc_init(void)
35763577
pr_err("%s: smc_inet_init fails with %d\n", __func__, rc);
35773578
goto out_ulp;
35783579
}
3580+
3581+
rc = smc_bpf_struct_ops_init();
3582+
if (rc) {
3583+
pr_err("%s: smc_bpf_struct_ops_init fails with %d\n", __func__,
3584+
rc);
3585+
goto out_inet;
3586+
}
3587+
35793588
static_branch_enable(&tcp_have_smc);
35803589
return 0;
3590+
out_inet:
3591+
smc_inet_exit();
35813592
out_ulp:
35823593
tcp_unregister_ulp(&smc_ulp_ops);
35833594
out_lo:

net/smc/smc_ops.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
* Author: D. Wythe <alibuda@linux.alibaba.com>
1111
*/
1212

13+
#include <linux/bpf_verifier.h>
14+
#include <linux/bpf.h>
15+
#include <linux/btf.h>
1316
#include <linux/rculist.h>
1417

1518
#include "smc_ops.h"
@@ -51,3 +54,78 @@ struct smc_ops *smc_ops_find_by_name(const char *name)
5154
}
5255
return NULL;
5356
}
57+
58+
static int __bpf_smc_stub_set_tcp_option(struct tcp_sock *tp) { return 1; }
59+
static int __bpf_smc_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_ops __bpf_smc_bpf_ops = {
66+
.set_option = __bpf_smc_stub_set_tcp_option,
67+
.set_option_cond = __bpf_smc_stub_set_tcp_option_cond,
68+
};
69+
70+
static int smc_bpf_ops_init(struct btf *btf) { return 0; }
71+
72+
static int smc_bpf_ops_reg(void *kdata, struct bpf_link *link)
73+
{
74+
return smc_ops_reg(kdata);
75+
}
76+
77+
static void smc_bpf_ops_unreg(void *kdata, struct bpf_link *link)
78+
{
79+
smc_ops_unreg(kdata);
80+
}
81+
82+
static int smc_bpf_ops_init_member(const struct btf_type *t,
83+
const struct btf_member *member,
84+
void *kdata, const void *udata)
85+
{
86+
const struct smc_ops *u_ops;
87+
struct smc_ops *k_ops;
88+
u32 moff;
89+
90+
u_ops = (const struct smc_ops *)udata;
91+
k_ops = (struct smc_ops *)kdata;
92+
93+
moff = __btf_member_bit_offset(t, member) / 8;
94+
switch (moff) {
95+
case offsetof(struct smc_ops, name):
96+
if (bpf_obj_name_cpy(k_ops->name, u_ops->name,
97+
sizeof(u_ops->name)) <= 0)
98+
return -EINVAL;
99+
return 1;
100+
case offsetof(struct smc_ops, flags):
101+
if (u_ops->flags & ~SMC_OPS_ALL_FLAGS)
102+
return -EINVAL;
103+
k_ops->flags = u_ops->flags;
104+
return 1;
105+
default:
106+
break;
107+
}
108+
109+
return 0;
110+
}
111+
112+
static const struct bpf_verifier_ops smc_bpf_verifier_ops = {
113+
.get_func_proto = bpf_base_func_proto,
114+
.is_valid_access = bpf_tracing_btf_ctx_access,
115+
};
116+
117+
static struct bpf_struct_ops bpf_smc_bpf_ops = {
118+
.name = "smc_ops",
119+
.init = smc_bpf_ops_init,
120+
.reg = smc_bpf_ops_reg,
121+
.unreg = smc_bpf_ops_unreg,
122+
.cfi_stubs = &__bpf_smc_bpf_ops,
123+
.verifier_ops = &smc_bpf_verifier_ops,
124+
.init_member = smc_bpf_ops_init_member,
125+
.owner = THIS_MODULE,
126+
};
127+
128+
int smc_bpf_struct_ops_init(void)
129+
{
130+
return register_bpf_struct_ops(&bpf_smc_bpf_ops, smc_ops);
131+
}

net/smc/smc_ops.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,10 @@ void smc_ops_unreg(struct smc_ops *ops);
2424
* Note: Caller MUST ensure it's was invoked under rcu_read_lock.
2525
*/
2626
struct smc_ops *smc_ops_find_by_name(const char *name);
27+
#if IS_ENABLED(CONFIG_SMC_OPS)
28+
int smc_bpf_struct_ops_init(void);
29+
#else
30+
static inline int smc_bpf_struct_ops_init(void) { return 0; }
31+
#endif
2732

2833
#endif /* __SMC_OPS */

0 commit comments

Comments
 (0)