]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/commitdiff
bpf: Refactor sockmap redirect code so its easy to reuse
authorJohn Fastabend <john.fastabend@gmail.com>
Fri, 29 May 2020 23:06:41 +0000 (16:06 -0700)
committerKhalid Elmously <khalid.elmously@canonical.com>
Sat, 8 Aug 2020 05:53:12 +0000 (01:53 -0400)
BugLink: https://bugs.launchpad.net/bugs/1885023
[ Upstream commit ca2f5f21dbbd5e3a00cd3e97f728aa2ca0b2e011 ]

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: d829e9c4112b5 ("tls: convert to generic sk_msg interface")
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/159079360110.5745.7024009076049029819.stgit@john-Precision-5820-Tower
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Kamal Mostafa <kamal@canonical.com>
Signed-off-by: Kelsey Skunberg <kelsey.skunberg@canonical.com>
net/core/skmsg.c

index ded2d52276786d2845d0b82d6d0f95ccf76992cc..8e538a28fe0d38bf8b1e47ea0a0f7d071706f0e1 100644 (file)
@@ -686,13 +686,43 @@ static struct sk_psock *sk_psock_from_strp(struct strparser *strp)
        return container_of(parser, struct sk_psock, parser);
 }
 
-static void sk_psock_verdict_apply(struct sk_psock *psock,
-                                  struct sk_buff *skb, int verdict)
+static void sk_psock_skb_redirect(struct sk_psock *psock, struct sk_buff *skb)
 {
        struct sk_psock *psock_other;
        struct sock *sk_other;
        bool ingress;
 
+       sk_other = tcp_skb_bpf_redirect_fetch(skb);
+       if (unlikely(!sk_other)) {
+               kfree_skb(skb);
+               return;
+       }
+       psock_other = sk_psock(sk_other);
+       if (!psock_other || sock_flag(sk_other, SOCK_DEAD) ||
+           !sk_psock_test_state(psock_other, SK_PSOCK_TX_ENABLED)) {
+               kfree_skb(skb);
+               return;
+       }
+
+       ingress = tcp_skb_bpf_ingress(skb);
+       if ((!ingress && sock_writeable(sk_other)) ||
+           (ingress &&
+            atomic_read(&sk_other->sk_rmem_alloc) <=
+            sk_other->sk_rcvbuf)) {
+               if (!ingress)
+                       skb_set_owner_w(skb, sk_other);
+               skb_queue_tail(&psock_other->ingress_skb, skb);
+               schedule_work(&psock_other->work);
+       } else {
+               kfree_skb(skb);
+       }
+}
+
+static void sk_psock_verdict_apply(struct sk_psock *psock,
+                                  struct sk_buff *skb, int verdict)
+{
+       struct sock *sk_other;
+
        switch (verdict) {
        case __SK_PASS:
                sk_other = psock->sk;
@@ -711,25 +741,8 @@ static void sk_psock_verdict_apply(struct sk_psock *psock,
                }
                goto out_free;
        case __SK_REDIRECT:
-               sk_other = tcp_skb_bpf_redirect_fetch(skb);
-               if (unlikely(!sk_other))
-                       goto out_free;
-               psock_other = sk_psock(sk_other);
-               if (!psock_other || sock_flag(sk_other, SOCK_DEAD) ||
-                   !sk_psock_test_state(psock_other, SK_PSOCK_TX_ENABLED))
-                       goto out_free;
-               ingress = tcp_skb_bpf_ingress(skb);
-               if ((!ingress && sock_writeable(sk_other)) ||
-                   (ingress &&
-                    atomic_read(&sk_other->sk_rmem_alloc) <=
-                    sk_other->sk_rcvbuf)) {
-                       if (!ingress)
-                               skb_set_owner_w(skb, sk_other);
-                       skb_queue_tail(&psock_other->ingress_skb, skb);
-                       schedule_work(&psock_other->work);
-                       break;
-               }
-               /* fall-through */
+               sk_psock_skb_redirect(psock, skb);
+               break;
        case __SK_DROP:
                /* fall-through */
        default: