Skip to content

Commit f747632

Browse files
lmbborkmann
authored andcommitted
bpf: sockmap: Move generic sockmap hooks from BPF TCP
The init, close and unhash handlers from TCP sockmap are generic, and can be reused by UDP sockmap. Move the helpers into the sockmap code base and expose them. This requires tcp_bpf_get_proto and tcp_bpf_clone to be conditional on BPF_STREAM_PARSER. The moved functions are unmodified, except that sk_psock_unlink is renamed to sock_map_unlink to better match its behaviour. Signed-off-by: Lorenz Bauer <lmb@cloudflare.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20200309111243.6982-6-lmb@cloudflare.com
1 parent 5da0040 commit f747632

5 files changed

Lines changed: 118 additions & 119 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,8 @@ static inline void bpf_map_offload_map_free(struct bpf_map *map)
14191419
#if defined(CONFIG_BPF_STREAM_PARSER)
14201420
int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, u32 which);
14211421
int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
1422+
void sock_map_unhash(struct sock *sk);
1423+
void sock_map_close(struct sock *sk, long timeout);
14221424
#else
14231425
static inline int sock_map_prog_update(struct bpf_map *map,
14241426
struct bpf_prog *prog, u32 which)
@@ -1431,7 +1433,7 @@ static inline int sock_map_get_from_fd(const union bpf_attr *attr,
14311433
{
14321434
return -EINVAL;
14331435
}
1434-
#endif
1436+
#endif /* CONFIG_BPF_STREAM_PARSER */
14351437

14361438
#if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
14371439
void bpf_sk_reuseport_detach(struct sock *sk);

‎include/linux/skmsg.h‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,6 @@ static inline void sk_psock_free_link(struct sk_psock_link *link)
323323
}
324324

325325
struct sk_psock_link *sk_psock_link_pop(struct sk_psock *psock);
326-
#if defined(CONFIG_BPF_STREAM_PARSER)
327-
void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link);
328-
#else
329-
static inline void sk_psock_unlink(struct sock *sk,
330-
struct sk_psock_link *link)
331-
{
332-
}
333-
#endif
334326

335327
void __sk_psock_purge_ingress_msg(struct sk_psock *psock);
336328

@@ -399,26 +391,6 @@ static inline bool sk_psock_test_state(const struct sk_psock *psock,
399391
return test_bit(bit, &psock->state);
400392
}
401393

402-
static inline struct sk_psock *sk_psock_get_checked(struct sock *sk)
403-
{
404-
struct sk_psock *psock;
405-
406-
rcu_read_lock();
407-
psock = sk_psock(sk);
408-
if (psock) {
409-
if (sk->sk_prot->recvmsg != tcp_bpf_recvmsg) {
410-
psock = ERR_PTR(-EBUSY);
411-
goto out;
412-
}
413-
414-
if (!refcount_inc_not_zero(&psock->refcnt))
415-
psock = ERR_PTR(-EBUSY);
416-
}
417-
out:
418-
rcu_read_unlock();
419-
return psock;
420-
}
421-
422394
static inline struct sk_psock *sk_psock_get(struct sock *sk)
423395
{
424396
struct sk_psock *psock;

‎include/net/tcp.h‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,19 +2195,22 @@ void tcp_update_ulp(struct sock *sk, struct proto *p,
21952195
struct sk_msg;
21962196
struct sk_psock;
21972197

2198+
#ifdef CONFIG_BPF_STREAM_PARSER
2199+
struct proto *tcp_bpf_get_proto(struct sock *sk, struct sk_psock *psock);
2200+
void tcp_bpf_clone(const struct sock *sk, struct sock *newsk);
2201+
#else
2202+
static inline void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
2203+
{
2204+
}
2205+
#endif /* CONFIG_BPF_STREAM_PARSER */
2206+
21982207
#ifdef CONFIG_NET_SOCK_MSG
2199-
int tcp_bpf_init(struct sock *sk);
22002208
int tcp_bpf_sendmsg_redir(struct sock *sk, struct sk_msg *msg, u32 bytes,
22012209
int flags);
22022210
int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
22032211
int nonblock, int flags, int *addr_len);
22042212
int __tcp_bpf_recvmsg(struct sock *sk, struct sk_psock *psock,
22052213
struct msghdr *msg, int len, int flags);
2206-
void tcp_bpf_clone(const struct sock *sk, struct sock *newsk);
2207-
#else
2208-
static inline void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
2209-
{
2210-
}
22112214
#endif /* CONFIG_NET_SOCK_MSG */
22122215

22132216
/* Call BPF_SOCK_OPS program that returns an int. If the return value

‎net/core/sock_map.c‎

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,51 @@ static void sock_map_unref(struct sock *sk, void *link_raw)
141141
}
142142
}
143143

144+
static int sock_map_init_proto(struct sock *sk)
145+
{
146+
struct sk_psock *psock;
147+
struct proto *prot;
148+
149+
sock_owned_by_me(sk);
150+
151+
rcu_read_lock();
152+
psock = sk_psock(sk);
153+
if (unlikely(!psock)) {
154+
rcu_read_unlock();
155+
return -EINVAL;
156+
}
157+
158+
prot = tcp_bpf_get_proto(sk, psock);
159+
if (IS_ERR(prot)) {
160+
rcu_read_unlock();
161+
return PTR_ERR(prot);
162+
}
163+
164+
sk_psock_update_proto(sk, psock, prot);
165+
rcu_read_unlock();
166+
return 0;
167+
}
168+
169+
static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)
170+
{
171+
struct sk_psock *psock;
172+
173+
rcu_read_lock();
174+
psock = sk_psock(sk);
175+
if (psock) {
176+
if (sk->sk_prot->recvmsg != tcp_bpf_recvmsg) {
177+
psock = ERR_PTR(-EBUSY);
178+
goto out;
179+
}
180+
181+
if (!refcount_inc_not_zero(&psock->refcnt))
182+
psock = ERR_PTR(-EBUSY);
183+
}
184+
out:
185+
rcu_read_unlock();
186+
return psock;
187+
}
188+
144189
static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs,
145190
struct sock *sk)
146191
{
@@ -172,7 +217,7 @@ static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs,
172217
}
173218
}
174219

175-
psock = sk_psock_get_checked(sk);
220+
psock = sock_map_psock_get_checked(sk);
176221
if (IS_ERR(psock)) {
177222
ret = PTR_ERR(psock);
178223
goto out_progs;
@@ -196,7 +241,7 @@ static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs,
196241
if (msg_parser)
197242
psock_set_prog(&psock->progs.msg_parser, msg_parser);
198243

199-
ret = tcp_bpf_init(sk);
244+
ret = sock_map_init_proto(sk);
200245
if (ret < 0)
201246
goto out_drop;
202247

@@ -231,7 +276,7 @@ static int sock_map_link_no_progs(struct bpf_map *map, struct sock *sk)
231276
struct sk_psock *psock;
232277
int ret;
233278

234-
psock = sk_psock_get_checked(sk);
279+
psock = sock_map_psock_get_checked(sk);
235280
if (IS_ERR(psock))
236281
return PTR_ERR(psock);
237282

@@ -241,7 +286,7 @@ static int sock_map_link_no_progs(struct bpf_map *map, struct sock *sk)
241286
return -ENOMEM;
242287
}
243288

244-
ret = tcp_bpf_init(sk);
289+
ret = sock_map_init_proto(sk);
245290
if (ret < 0)
246291
sk_psock_put(sk, psock);
247292
return ret;
@@ -1120,7 +1165,7 @@ int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
11201165
return 0;
11211166
}
11221167

1123-
void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link)
1168+
static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
11241169
{
11251170
switch (link->map->map_type) {
11261171
case BPF_MAP_TYPE_SOCKMAP:
@@ -1133,3 +1178,54 @@ void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link)
11331178
break;
11341179
}
11351180
}
1181+
1182+
static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
1183+
{
1184+
struct sk_psock_link *link;
1185+
1186+
while ((link = sk_psock_link_pop(psock))) {
1187+
sock_map_unlink(sk, link);
1188+
sk_psock_free_link(link);
1189+
}
1190+
}
1191+
1192+
void sock_map_unhash(struct sock *sk)
1193+
{
1194+
void (*saved_unhash)(struct sock *sk);
1195+
struct sk_psock *psock;
1196+
1197+
rcu_read_lock();
1198+
psock = sk_psock(sk);
1199+
if (unlikely(!psock)) {
1200+
rcu_read_unlock();
1201+
if (sk->sk_prot->unhash)
1202+
sk->sk_prot->unhash(sk);
1203+
return;
1204+
}
1205+
1206+
saved_unhash = psock->saved_unhash;
1207+
sock_map_remove_links(sk, psock);
1208+
rcu_read_unlock();
1209+
saved_unhash(sk);
1210+
}
1211+
1212+
void sock_map_close(struct sock *sk, long timeout)
1213+
{
1214+
void (*saved_close)(struct sock *sk, long timeout);
1215+
struct sk_psock *psock;
1216+
1217+
lock_sock(sk);
1218+
rcu_read_lock();
1219+
psock = sk_psock(sk);
1220+
if (unlikely(!psock)) {
1221+
rcu_read_unlock();
1222+
release_sock(sk);
1223+
return sk->sk_prot->close(sk, timeout);
1224+
}
1225+
1226+
saved_close = psock->saved_close;
1227+
sock_map_remove_links(sk, psock);
1228+
rcu_read_unlock();
1229+
release_sock(sk);
1230+
saved_close(sk, timeout);
1231+
}

‎net/ipv4/tcp_bpf.c‎

Lines changed: 5 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -528,57 +528,7 @@ static int tcp_bpf_sendpage(struct sock *sk, struct page *page, int offset,
528528
return copied ? copied : err;
529529
}
530530

531-
static void tcp_bpf_remove(struct sock *sk, struct sk_psock *psock)
532-
{
533-
struct sk_psock_link *link;
534-
535-
while ((link = sk_psock_link_pop(psock))) {
536-
sk_psock_unlink(sk, link);
537-
sk_psock_free_link(link);
538-
}
539-
}
540-
541-
static void tcp_bpf_unhash(struct sock *sk)
542-
{
543-
void (*saved_unhash)(struct sock *sk);
544-
struct sk_psock *psock;
545-
546-
rcu_read_lock();
547-
psock = sk_psock(sk);
548-
if (unlikely(!psock)) {
549-
rcu_read_unlock();
550-
if (sk->sk_prot->unhash)
551-
sk->sk_prot->unhash(sk);
552-
return;
553-
}
554-
555-
saved_unhash = psock->saved_unhash;
556-
tcp_bpf_remove(sk, psock);
557-
rcu_read_unlock();
558-
saved_unhash(sk);
559-
}
560-
561-
static void tcp_bpf_close(struct sock *sk, long timeout)
562-
{
563-
void (*saved_close)(struct sock *sk, long timeout);
564-
struct sk_psock *psock;
565-
566-
lock_sock(sk);
567-
rcu_read_lock();
568-
psock = sk_psock(sk);
569-
if (unlikely(!psock)) {
570-
rcu_read_unlock();
571-
release_sock(sk);
572-
return sk->sk_prot->close(sk, timeout);
573-
}
574-
575-
saved_close = psock->saved_close;
576-
tcp_bpf_remove(sk, psock);
577-
rcu_read_unlock();
578-
release_sock(sk);
579-
saved_close(sk, timeout);
580-
}
581-
531+
#ifdef CONFIG_BPF_STREAM_PARSER
582532
enum {
583533
TCP_BPF_IPV4,
584534
TCP_BPF_IPV6,
@@ -599,8 +549,8 @@ static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
599549
struct proto *base)
600550
{
601551
prot[TCP_BPF_BASE] = *base;
602-
prot[TCP_BPF_BASE].unhash = tcp_bpf_unhash;
603-
prot[TCP_BPF_BASE].close = tcp_bpf_close;
552+
prot[TCP_BPF_BASE].unhash = sock_map_unhash;
553+
prot[TCP_BPF_BASE].close = sock_map_close;
604554
prot[TCP_BPF_BASE].recvmsg = tcp_bpf_recvmsg;
605555
prot[TCP_BPF_BASE].stream_memory_read = tcp_bpf_stream_read;
606556

@@ -640,7 +590,7 @@ static int tcp_bpf_assert_proto_ops(struct proto *ops)
640590
ops->sendpage == tcp_sendpage ? 0 : -ENOTSUPP;
641591
}
642592

643-
static struct proto *tcp_bpf_get_proto(struct sock *sk, struct sk_psock *psock)
593+
struct proto *tcp_bpf_get_proto(struct sock *sk, struct sk_psock *psock)
644594
{
645595
int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
646596
int config = psock->progs.msg_parser ? TCP_BPF_TX : TCP_BPF_BASE;
@@ -657,31 +607,6 @@ static struct proto *tcp_bpf_get_proto(struct sock *sk, struct sk_psock *psock)
657607
return &tcp_bpf_prots[family][config];
658608
}
659609

660-
int tcp_bpf_init(struct sock *sk)
661-
{
662-
struct sk_psock *psock;
663-
struct proto *prot;
664-
665-
sock_owned_by_me(sk);
666-
667-
rcu_read_lock();
668-
psock = sk_psock(sk);
669-
if (unlikely(!psock)) {
670-
rcu_read_unlock();
671-
return -EINVAL;
672-
}
673-
674-
prot = tcp_bpf_get_proto(sk, psock);
675-
if (IS_ERR(prot)) {
676-
rcu_read_unlock();
677-
return PTR_ERR(prot);
678-
}
679-
680-
sk_psock_update_proto(sk, psock, prot);
681-
rcu_read_unlock();
682-
return 0;
683-
}
684-
685610
/* If a child got cloned from a listening socket that had tcp_bpf
686611
* protocol callbacks installed, we need to restore the callbacks to
687612
* the default ones because the child does not inherit the psock state
@@ -695,3 +620,4 @@ void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
695620
if (prot == &tcp_bpf_prots[family][TCP_BPF_BASE])
696621
newsk->sk_prot = sk->sk_prot_creator;
697622
}
623+
#endif /* CONFIG_BPF_STREAM_PARSER */

0 commit comments

Comments
 (0)