Skip to content

Commit ca2f5f2

Browse files
jrfastabAlexei Starovoitov
authored andcommitted
bpf: Refactor sockmap redirect code so its easy to reuse
We will need this block of code called from tls context shortly lets refactor the redirect logic so its easy to use. This also cleans up the switch stmt so we have fewer fallthrough cases. No logic changes are intended. Fixes: d829e9c ("tls: convert to generic sk_msg interface") Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Reviewed-by: Jakub Sitnicki <[email protected]> Acked-by: Song Liu <[email protected]> Link: https://lore.kernel.org/bpf/159079360110.5745.7024009076049029819.stgit@john-Precision-5820-Tower Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent df8fe57 commit ca2f5f2

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

net/core/skmsg.c

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -682,13 +682,43 @@ static struct sk_psock *sk_psock_from_strp(struct strparser *strp)
682682
return container_of(parser, struct sk_psock, parser);
683683
}
684684

685-
static void sk_psock_verdict_apply(struct sk_psock *psock,
686-
struct sk_buff *skb, int verdict)
685+
static void sk_psock_skb_redirect(struct sk_psock *psock, struct sk_buff *skb)
687686
{
688687
struct sk_psock *psock_other;
689688
struct sock *sk_other;
690689
bool ingress;
691690

691+
sk_other = tcp_skb_bpf_redirect_fetch(skb);
692+
if (unlikely(!sk_other)) {
693+
kfree_skb(skb);
694+
return;
695+
}
696+
psock_other = sk_psock(sk_other);
697+
if (!psock_other || sock_flag(sk_other, SOCK_DEAD) ||
698+
!sk_psock_test_state(psock_other, SK_PSOCK_TX_ENABLED)) {
699+
kfree_skb(skb);
700+
return;
701+
}
702+
703+
ingress = tcp_skb_bpf_ingress(skb);
704+
if ((!ingress && sock_writeable(sk_other)) ||
705+
(ingress &&
706+
atomic_read(&sk_other->sk_rmem_alloc) <=
707+
sk_other->sk_rcvbuf)) {
708+
if (!ingress)
709+
skb_set_owner_w(skb, sk_other);
710+
skb_queue_tail(&psock_other->ingress_skb, skb);
711+
schedule_work(&psock_other->work);
712+
} else {
713+
kfree_skb(skb);
714+
}
715+
}
716+
717+
static void sk_psock_verdict_apply(struct sk_psock *psock,
718+
struct sk_buff *skb, int verdict)
719+
{
720+
struct sock *sk_other;
721+
692722
switch (verdict) {
693723
case __SK_PASS:
694724
sk_other = psock->sk;
@@ -707,25 +737,8 @@ static void sk_psock_verdict_apply(struct sk_psock *psock,
707737
}
708738
goto out_free;
709739
case __SK_REDIRECT:
710-
sk_other = tcp_skb_bpf_redirect_fetch(skb);
711-
if (unlikely(!sk_other))
712-
goto out_free;
713-
psock_other = sk_psock(sk_other);
714-
if (!psock_other || sock_flag(sk_other, SOCK_DEAD) ||
715-
!sk_psock_test_state(psock_other, SK_PSOCK_TX_ENABLED))
716-
goto out_free;
717-
ingress = tcp_skb_bpf_ingress(skb);
718-
if ((!ingress && sock_writeable(sk_other)) ||
719-
(ingress &&
720-
atomic_read(&sk_other->sk_rmem_alloc) <=
721-
sk_other->sk_rcvbuf)) {
722-
if (!ingress)
723-
skb_set_owner_w(skb, sk_other);
724-
skb_queue_tail(&psock_other->ingress_skb, skb);
725-
schedule_work(&psock_other->work);
726-
break;
727-
}
728-
/* fall-through */
740+
sk_psock_skb_redirect(psock, skb);
741+
break;
729742
case __SK_DROP:
730743
/* fall-through */
731744
default:

0 commit comments

Comments
 (0)