Skip to content

Commit 1243a51

Browse files
borkmannAlexei Starovoitov
authored and
Alexei Starovoitov
committed
tcp, ulp: remove ulp bits from sockmap
In order to prepare sockmap logic to be used in combination with kTLS we need to detangle it from ULP, and further split it in later commits into a generic API. Joint work with John. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: John Fastabend <john.fastabend@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 8b9088f commit 1243a51

File tree

3 files changed

+23
-88
lines changed

3 files changed

+23
-88
lines changed

include/net/tcp.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2057,7 +2057,6 @@ struct tcp_ulp_ops {
20572057
int tcp_register_ulp(struct tcp_ulp_ops *type);
20582058
void tcp_unregister_ulp(struct tcp_ulp_ops *type);
20592059
int tcp_set_ulp(struct sock *sk, const char *name);
2060-
int tcp_set_ulp_id(struct sock *sk, const int ulp);
20612060
void tcp_get_available_ulp(char *buf, size_t len);
20622061
void tcp_cleanup_ulp(struct sock *sk);
20632062

kernel/bpf/sockmap.c

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ enum {
182182
static struct proto *saved_tcpv6_prot __read_mostly;
183183
static DEFINE_SPINLOCK(tcpv6_prot_lock);
184184
static struct proto bpf_tcp_prots[SOCKMAP_NUM_PROTS][SOCKMAP_NUM_CONFIGS];
185+
185186
static void build_protos(struct proto prot[SOCKMAP_NUM_CONFIGS],
186187
struct proto *base)
187188
{
@@ -239,6 +240,13 @@ static int bpf_tcp_init(struct sock *sk)
239240
return 0;
240241
}
241242

243+
static int __init bpf_sock_init(void)
244+
{
245+
build_protos(bpf_tcp_prots[SOCKMAP_IPV4], &tcp_prot);
246+
return 0;
247+
}
248+
core_initcall(bpf_sock_init);
249+
242250
static void smap_release_sock(struct smap_psock *psock, struct sock *sock);
243251
static int free_start_sg(struct sock *sk, struct sk_msg_buff *md, bool charge);
244252

@@ -413,15 +421,6 @@ enum __sk_action {
413421
__SK_NONE,
414422
};
415423

416-
static struct tcp_ulp_ops bpf_tcp_ulp_ops __read_mostly = {
417-
.name = "bpf_tcp",
418-
.uid = TCP_ULP_BPF,
419-
.user_visible = false,
420-
.owner = NULL,
421-
.init = bpf_tcp_init,
422-
.release = bpf_tcp_release,
423-
};
424-
425424
static int memcopy_from_iter(struct sock *sk,
426425
struct sk_msg_buff *md,
427426
struct iov_iter *from, int bytes)
@@ -1236,16 +1235,6 @@ static void bpf_tcp_msg_add(struct smap_psock *psock,
12361235
bpf_prog_put(orig_tx_msg);
12371236
}
12381237

1239-
static int bpf_tcp_ulp_register(void)
1240-
{
1241-
build_protos(bpf_tcp_prots[SOCKMAP_IPV4], &tcp_prot);
1242-
/* Once BPF TX ULP is registered it is never unregistered. It
1243-
* will be in the ULP list for the lifetime of the system. Doing
1244-
* duplicate registers is not a problem.
1245-
*/
1246-
return tcp_register_ulp(&bpf_tcp_ulp_ops);
1247-
}
1248-
12491238
static int smap_verdict_func(struct smap_psock *psock, struct sk_buff *skb)
12501239
{
12511240
struct bpf_prog *prog = READ_ONCE(psock->bpf_verdict);
@@ -1491,7 +1480,7 @@ static void smap_release_sock(struct smap_psock *psock, struct sock *sock)
14911480
{
14921481
if (refcount_dec_and_test(&psock->refcnt)) {
14931482
if (psock_is_smap_sk(sock))
1494-
tcp_cleanup_ulp(sock);
1483+
bpf_tcp_release(sock);
14951484
write_lock_bh(&sock->sk_callback_lock);
14961485
smap_stop_sock(psock, sock);
14971486
write_unlock_bh(&sock->sk_callback_lock);
@@ -1666,10 +1655,6 @@ static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
16661655
attr->value_size != 4 || attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
16671656
return ERR_PTR(-EINVAL);
16681657

1669-
err = bpf_tcp_ulp_register();
1670-
if (err && err != -EEXIST)
1671-
return ERR_PTR(err);
1672-
16731658
stab = kzalloc(sizeof(*stab), GFP_USER);
16741659
if (!stab)
16751660
return ERR_PTR(-ENOMEM);
@@ -1951,7 +1936,7 @@ static int __sock_map_ctx_update_elem(struct bpf_map *map,
19511936
if (tx_msg)
19521937
bpf_tcp_msg_add(psock, sock, tx_msg);
19531938
if (new) {
1954-
err = tcp_set_ulp_id(sock, TCP_ULP_BPF);
1939+
err = bpf_tcp_init(sock);
19551940
if (err)
19561941
goto out_free;
19571942
}
@@ -2187,10 +2172,6 @@ static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
21872172
*/
21882173
return ERR_PTR(-E2BIG);
21892174

2190-
err = bpf_tcp_ulp_register();
2191-
if (err && err != -EEXIST)
2192-
return ERR_PTR(err);
2193-
21942175
htab = kzalloc(sizeof(*htab), GFP_USER);
21952176
if (!htab)
21962177
return ERR_PTR(-ENOMEM);

net/ipv4/tcp_ulp.c

Lines changed: 13 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*/
88

9-
#include<linux/module.h>
9+
#include <linux/module.h>
1010
#include <linux/mm.h>
1111
#include <linux/types.h>
1212
#include <linux/list.h>
@@ -29,18 +29,6 @@ static struct tcp_ulp_ops *tcp_ulp_find(const char *name)
2929
return NULL;
3030
}
3131

32-
static struct tcp_ulp_ops *tcp_ulp_find_id(const int ulp)
33-
{
34-
struct tcp_ulp_ops *e;
35-
36-
list_for_each_entry_rcu(e, &tcp_ulp_list, list) {
37-
if (e->uid == ulp)
38-
return e;
39-
}
40-
41-
return NULL;
42-
}
43-
4432
static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
4533
{
4634
const struct tcp_ulp_ops *ulp = NULL;
@@ -63,18 +51,6 @@ static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
6351
return ulp;
6452
}
6553

66-
static const struct tcp_ulp_ops *__tcp_ulp_lookup(const int uid)
67-
{
68-
const struct tcp_ulp_ops *ulp;
69-
70-
rcu_read_lock();
71-
ulp = tcp_ulp_find_id(uid);
72-
if (!ulp || !try_module_get(ulp->owner))
73-
ulp = NULL;
74-
rcu_read_unlock();
75-
return ulp;
76-
}
77-
7854
/* Attach new upper layer protocol to the list
7955
* of available protocols.
8056
*/
@@ -135,56 +111,35 @@ void tcp_cleanup_ulp(struct sock *sk)
135111
icsk->icsk_ulp_ops = NULL;
136112
}
137113

138-
/* Change upper layer protocol for socket */
139-
int tcp_set_ulp(struct sock *sk, const char *name)
114+
static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
140115
{
141116
struct inet_connection_sock *icsk = inet_csk(sk);
142-
const struct tcp_ulp_ops *ulp_ops;
143-
int err = 0;
117+
int err;
144118

145-
sock_owned_by_me(sk);
119+
err = -EEXIST;
146120
if (icsk->icsk_ulp_ops)
147-
return -EEXIST;
148-
149-
ulp_ops = __tcp_ulp_find_autoload(name);
150-
if (!ulp_ops)
151-
return -ENOENT;
152-
153-
if (!ulp_ops->user_visible) {
154-
module_put(ulp_ops->owner);
155-
return -ENOENT;
156-
}
121+
goto out_err;
157122

158123
err = ulp_ops->init(sk);
159-
if (err) {
160-
module_put(ulp_ops->owner);
161-
return err;
162-
}
124+
if (err)
125+
goto out_err;
163126

164127
icsk->icsk_ulp_ops = ulp_ops;
165128
return 0;
129+
out_err:
130+
module_put(ulp_ops->owner);
131+
return err;
166132
}
167133

168-
int tcp_set_ulp_id(struct sock *sk, int ulp)
134+
int tcp_set_ulp(struct sock *sk, const char *name)
169135
{
170-
struct inet_connection_sock *icsk = inet_csk(sk);
171136
const struct tcp_ulp_ops *ulp_ops;
172-
int err;
173137

174138
sock_owned_by_me(sk);
175-
if (icsk->icsk_ulp_ops)
176-
return -EEXIST;
177139

178-
ulp_ops = __tcp_ulp_lookup(ulp);
140+
ulp_ops = __tcp_ulp_find_autoload(name);
179141
if (!ulp_ops)
180142
return -ENOENT;
181143

182-
err = ulp_ops->init(sk);
183-
if (err) {
184-
module_put(ulp_ops->owner);
185-
return err;
186-
}
187-
188-
icsk->icsk_ulp_ops = ulp_ops;
189-
return 0;
144+
return __tcp_set_ulp(sk, ulp_ops);
190145
}

0 commit comments

Comments
 (0)