From patchwork Fri Jun 26 16:52:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1317837 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20161025 header.b=KDKt6xqp; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 49tjc52LW5z9sQx for ; Sat, 27 Jun 2020 02:52:37 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727041AbgFZQwf (ORCPT ); Fri, 26 Jun 2020 12:52:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726139AbgFZQwe (ORCPT ); Fri, 26 Jun 2020 12:52:34 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B6E9C03E979 for ; Fri, 26 Jun 2020 09:52:34 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id x22so7026977qkj.6 for ; Fri, 26 Jun 2020 09:52:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=8T9UcUdWSX0ttb1WtDDxPNgqHJdOlrPTkZTwK0rEYOo=; b=KDKt6xqpFlaiAPsD8ziYgL2Iwvm2a4gE0np2UKG2Xw8mylFBwkNljntqMDZNXdPU40 OtzHlUvTlrRUXVrDas38vj2QrTtC48Z6MYWudViRV04bbNhNSqgpaCqqM1wAITffDnRM J7D4hD9OxkuRpWBUB1JmStjAMtEkm21DsYCuh7u8uw0pb/LwudNY65osUrZfAJT3c83S Xa46ogiQozxqMYpwjijooO5oLGKe8v3xXWJCzuqxFPFoEgqe2Cu0vHxWBQTyTEZN3Of8 hvwmNu/IP4ZcIQuNeIRZ61p4x5P/oCTz0RlpmfgbZjVvYrKrw13oef9irYZ7ZqESGORe Gy6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=8T9UcUdWSX0ttb1WtDDxPNgqHJdOlrPTkZTwK0rEYOo=; b=U95i4JkAymD/NykYmK5r74n4mVhKPGIhrJMsUsOmpQD1EbHXS5t49oKpMeGwQSPX2W 5fw7IdWzGa1iwRzPkPtxXm2ZvxQiYE3rlCCoBg6dqFSFvZ/9OQg03I6Zj4g9BsCRHfWS jxUGUArWRXSPi8Rt9Or2esEZZtQSeIX3QWgwI2UCFsCENBAIc8/FLWnJXmrlgorrTgZN XD7sZ4qU/CPnNoKj+i8yB/Nu6L2eZ5LHHUDobQWK/CU8l7lqd8Q1UbP6xJo73tSF9+tQ 0EJH+tPVJssAMkyNNeyw8d3x1TnEdPsFbHsfk83iQLceOLPnVzeYmFVsv9Mjxhm9/aJv EqqA== X-Gm-Message-State: AOAM530D7HVngZInCqVIQYJbHCqsmesWj/+zaL+YxsYW2c59+Y2YEyPH bgBvYNKOGF5rys80wHHQsdM8HUx2BQl1lv54/QCbXcb2eQWAUfl717rAw6YG1XMqqvjxCQ6Pq6Z 4GOysm2tz6HKRkf0z311asK+lpbcyt5xBgpBqhzsJVu14xLW8zqph1A== X-Google-Smtp-Source: ABdhPJw1i76iXkgQ4//c+MK0T1tQu/HCzNwM6V7w51BDkW/gJLfqnTfSgAqgaqjK5mh4eGLmmIaDSDo= X-Received: by 2002:ad4:4b34:: with SMTP id s20mr4154042qvw.177.1593190353520; Fri, 26 Jun 2020 09:52:33 -0700 (PDT) Date: Fri, 26 Jun 2020 09:52:28 -0700 Message-Id: <20200626165231.672001-1-sdf@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH bpf-next v2 1/4] bpf: add BPF_CGROUP_INET_SOCK_RELEASE hook From: Stanislav Fomichev To: netdev@vger.kernel.org, bpf@vger.kernel.org Cc: davem@davemloft.net, ast@kernel.org, daniel@iogearbox.net, Stanislav Fomichev Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Sometimes it's handy to know when the socket gets freed. In particular, we'd like to try to use a smarter allocation of ports for bpf_bind and explore the possibility of limiting the number of SOCK_DGRAM sockets the process can have. Adding a release pair to existing BPF_CGROUP_INET_SOCK_CREATE can unlock both of the mentioned features. The only questionable part here is the sock->sk check in the inet_release. Looking at the places where we do 'sock->sk = NULL', I don't understand how it can race with inet_release and why the check is there (it's been there since the initial git import). Otherwise, the change itself is pretty simple, we add a BPF hook to the inet_release and avoid calling it for kernel sockets. v2: * fix compile issue with CONFIG_CGROUP_BPF=n (kernel test robot) Signed-off-by: Stanislav Fomichev --- include/linux/bpf-cgroup.h | 4 ++++ include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 3 +++ net/core/filter.c | 1 + net/ipv4/af_inet.c | 3 +++ 5 files changed, 12 insertions(+) diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h index c66c545e161a..2c6f26670acc 100644 --- a/include/linux/bpf-cgroup.h +++ b/include/linux/bpf-cgroup.h @@ -210,6 +210,9 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key, #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) \ BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_CREATE) +#define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk) \ + BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_RELEASE) + #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) \ BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET4_POST_BIND) @@ -401,6 +404,7 @@ static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map, #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; }) #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; }) #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) ({ 0; }) +#define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk) ({ 0; }) #define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) ({ 0; }) #define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) ({ 0; }) #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) ({ 0; }) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index c65b374a5090..d7aea1d0167a 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -226,6 +226,7 @@ enum bpf_attach_type { BPF_CGROUP_INET4_GETSOCKNAME, BPF_CGROUP_INET6_GETSOCKNAME, BPF_XDP_DEVMAP, + BPF_CGROUP_INET_SOCK_RELEASE, __MAX_BPF_ATTACH_TYPE }; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 4d530b1d5683..2a3d4b8f95c7 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -1994,6 +1994,7 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type, case BPF_PROG_TYPE_CGROUP_SOCK: switch (expected_attach_type) { case BPF_CGROUP_INET_SOCK_CREATE: + case BPF_CGROUP_INET_SOCK_RELEASE: case BPF_CGROUP_INET4_POST_BIND: case BPF_CGROUP_INET6_POST_BIND: return 0; @@ -2792,6 +2793,7 @@ attach_type_to_prog_type(enum bpf_attach_type attach_type) return BPF_PROG_TYPE_CGROUP_SKB; break; case BPF_CGROUP_INET_SOCK_CREATE: + case BPF_CGROUP_INET_SOCK_RELEASE: case BPF_CGROUP_INET4_POST_BIND: case BPF_CGROUP_INET6_POST_BIND: return BPF_PROG_TYPE_CGROUP_SOCK; @@ -2942,6 +2944,7 @@ static int bpf_prog_query(const union bpf_attr *attr, case BPF_CGROUP_INET_INGRESS: case BPF_CGROUP_INET_EGRESS: case BPF_CGROUP_INET_SOCK_CREATE: + case BPF_CGROUP_INET_SOCK_RELEASE: case BPF_CGROUP_INET4_BIND: case BPF_CGROUP_INET6_BIND: case BPF_CGROUP_INET4_POST_BIND: diff --git a/net/core/filter.c b/net/core/filter.c index 209482a4eaa2..7bcac182383c 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -6855,6 +6855,7 @@ static bool __sock_filter_check_attach_type(int off, case offsetof(struct bpf_sock, priority): switch (attach_type) { case BPF_CGROUP_INET_SOCK_CREATE: + case BPF_CGROUP_INET_SOCK_RELEASE: goto full_access; default: return false; diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c index 02aa5cb3a4fd..965a96ea1168 100644 --- a/net/ipv4/af_inet.c +++ b/net/ipv4/af_inet.c @@ -411,6 +411,9 @@ int inet_release(struct socket *sock) if (sk) { long timeout; + if (!sk->sk_kern_sock) + BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk); + /* Applications forget to leave groups before exiting */ ip_mc_drop_socket(sk); From patchwork Fri Jun 26 16:52:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1317839 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20161025 header.b=GpbBei1c; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 49tjc73tQfz9sQx for ; Sat, 27 Jun 2020 02:52:39 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728001AbgFZQwi (ORCPT ); Fri, 26 Jun 2020 12:52:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727052AbgFZQwg (ORCPT ); Fri, 26 Jun 2020 12:52:36 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 16557C03E97A for ; Fri, 26 Jun 2020 09:52:36 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id a205so7009358qkc.16 for ; Fri, 26 Jun 2020 09:52:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=eUwmZKxCGJq9Xs8nLFS4B15rV5sLgo86/VMV9gkjb/4=; b=GpbBei1cwX7kTf8cUy0QTU7xkWGkbB+/alxZvCAx+Y1ytygViG7wgzj8Ui3ECpUGu4 UuD7qs0PtYpzSn7pLq31oLP1psu7OkOw5qh1lbVgerCxptFzE4ju+DfWkJDp/IH7fJOA MnxWxztIRd4bHEIpzJ+6PFXU6tcETTA8niKcIYmfeu9eu2lN7bRqEWA5QgWZaCci5xGA WmIeBkzNVMsHKcxjZaz0trQnTfOm8+YWuwHnAc5UcZW0cZVP2S27Wan6AzQNpnPotbV5 6uf1s/Swi4Bt6SQRPJ21RPHlHyO7O4rPKUUlBnwEuGv3ioOdiINu4Y3oZQ9CSMonT1Vm VqBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=eUwmZKxCGJq9Xs8nLFS4B15rV5sLgo86/VMV9gkjb/4=; b=Lb6W3O/FrydZFvrUZHmZ5a8BezK/ksbz2FOwy5nM26nbM2xB8RXLxgBbjg96dflmIj LYLgGeHvIIR8xU1cHDeZ1xzZFJV+5Tup9Ln/rzaGkpK4IGr4JHXk+nHeZIBdwQq1pP4u ixQz4DvrS52Ujb+ljD8U/afzgA6LpjKZ7dIJQ59EKA1YhlvvfUwySBtZ1aS0p0FFhykX blYoq88zP7QGlWjiL0lc/6T1wSZQ6S2yDutxSFkikyd3/AIhpnSK19n8npmWjSA3dSV2 P7zckvrHE+rVORZy/INzXL+FKwpyuNspDPxTuSHoZOK5Fm5caaY1ZETzmmOx29vXX6cE cXmQ== X-Gm-Message-State: AOAM532mg1L8B9ZeMvQAoxvXh+XAAftNWTRpG8UdVo97hGdHxgIl9rPN 1jyCSF7L31YOQ5p9r+8POFss1E8FrjBqpHEWSr1q6RTz3gs9EYJFlsfuqxiGyDFf0B1PoXCq4Xk kUsOZkISg8dcqZiX5KMKU/tK18xKKq04Qjy7oLhbUC0PJG/hDD65erg== X-Google-Smtp-Source: ABdhPJwE6V/rqLyi3zRXFMUkuntbGD8cd9sZnn5khxLup5Lt7jLTREOlbOMwRihlbXlcf6IMTqupD/U= X-Received: by 2002:a0c:f78b:: with SMTP id s11mr4132483qvn.33.1593190355221; Fri, 26 Jun 2020 09:52:35 -0700 (PDT) Date: Fri, 26 Jun 2020 09:52:29 -0700 In-Reply-To: <20200626165231.672001-1-sdf@google.com> Message-Id: <20200626165231.672001-2-sdf@google.com> Mime-Version: 1.0 References: <20200626165231.672001-1-sdf@google.com> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH bpf-next v2 2/4] libbpf: add support for BPF_CGROUP_INET_SOCK_RELEASE From: Stanislav Fomichev To: netdev@vger.kernel.org, bpf@vger.kernel.org Cc: davem@davemloft.net, ast@kernel.org, daniel@iogearbox.net, Stanislav Fomichev Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Add auto-detection for the cgroup/sock_release programs. Signed-off-by: Stanislav Fomichev --- tools/include/uapi/linux/bpf.h | 1 + tools/lib/bpf/libbpf.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index c65b374a5090..d7aea1d0167a 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -226,6 +226,7 @@ enum bpf_attach_type { BPF_CGROUP_INET4_GETSOCKNAME, BPF_CGROUP_INET6_GETSOCKNAME, BPF_XDP_DEVMAP, + BPF_CGROUP_INET_SOCK_RELEASE, __MAX_BPF_ATTACH_TYPE }; diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 7f01be2b88b8..acbab6d0672d 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -6670,6 +6670,8 @@ static const struct bpf_sec_def section_defs[] = { BPF_APROG_SEC("cgroup_skb/egress", BPF_PROG_TYPE_CGROUP_SKB, BPF_CGROUP_INET_EGRESS), BPF_APROG_COMPAT("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB), + BPF_EAPROG_SEC("cgroup/sock_release", BPF_PROG_TYPE_CGROUP_SOCK, + BPF_CGROUP_INET_SOCK_RELEASE), BPF_APROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE), BPF_EAPROG_SEC("cgroup/post_bind4", BPF_PROG_TYPE_CGROUP_SOCK, From patchwork Fri Jun 26 16:52:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1317840 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20161025 header.b=L9e0MAvJ; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 49tjc808t5z9sRf for ; Sat, 27 Jun 2020 02:52:40 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726139AbgFZQwi (ORCPT ); Fri, 26 Jun 2020 12:52:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727972AbgFZQwi (ORCPT ); Fri, 26 Jun 2020 12:52:38 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BCBD0C03E979 for ; Fri, 26 Jun 2020 09:52:37 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id z7so10407460ybz.1 for ; Fri, 26 Jun 2020 09:52:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=f6Da7523rX1uWkEPLzRoAH+Igij3aUvySfasC0+tw+I=; b=L9e0MAvJShBETk20ArQF02ez232dqV2o84ZToF1CZhodWJoLD33GIkiZCRFZoo7eBM wXjS/hQ9l/NxubTSKh/++WdG6ruUosLVlGaNPmLWxdxvQ1SQfgB5QiFSfcMhnNSTfLCa 6rCdR14ZT1jndlE3E9n3Nu1j8HrUDj8pwO4goYwg22hM0CCtHdy8YhXtCLlbPRRTiwqO stQ8RbxX90raorfMqF7G0uS8+lCwBJuqNqxWaFBbk4fLN+W0fprq9XYyW8y99LZE0Ipr /f97Jqi6np6qXT42dKH6HazqNHXRqm4HglfbUPGGso2X/PJhvmCVQ6IJfoR7qt/X+6++ 8xzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=f6Da7523rX1uWkEPLzRoAH+Igij3aUvySfasC0+tw+I=; b=a9tpy0lMtaEeElml+DvknaL28n9G/4pgFtIE0KSRq96skvT32v/HSiID4rh4m6zsEF NibrpTQzS+NetiQ5FB3ygdBUJjRs0TsDNCMNPFS8vAxvARZO3lhNGYYKBYtymX3nbz7I OdI7VNNX5e+KNPpfzCAoDZUvdUisughDfhx6Icy/TGCxwBc59fpoEtHpKmedlpnhvSGB pQjrW4+3cC3oy5BpY3c1XJWN0e73TGWADBJVE6eFl1K26oJiiM63bNC1aDcSUsvUwfqu XipCKlQ0lxyxofDNNlper72l8woj1oa+CF8owza4oK8kKku88ruVxjv098Fb0/X6ZYbL bFUw== X-Gm-Message-State: AOAM531S1R3e0bif2ph4ZVvlqP97MzhrTSLsorpZLyiVV4oq/Yw6JSZw O+k5PAqU5sHf8GoIsXlac3Pap3c= X-Google-Smtp-Source: ABdhPJwu+wvSSUYejBhERT+998mB3wPdnawJIIxMRc/fJgg73eeQFByoBybA95AIp2ZnHJW0U2FLshA= X-Received: by 2002:a25:230a:: with SMTP id j10mr6699807ybj.148.1593190357016; Fri, 26 Jun 2020 09:52:37 -0700 (PDT) Date: Fri, 26 Jun 2020 09:52:30 -0700 In-Reply-To: <20200626165231.672001-1-sdf@google.com> Message-Id: <20200626165231.672001-3-sdf@google.com> Mime-Version: 1.0 References: <20200626165231.672001-1-sdf@google.com> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH bpf-next v2 3/4] bpftool: support BPF_CGROUP_INET_SOCK_RELEASE From: Stanislav Fomichev To: netdev@vger.kernel.org, bpf@vger.kernel.org Cc: davem@davemloft.net, ast@kernel.org, daniel@iogearbox.net, Stanislav Fomichev Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Support attaching to sock_release from the bpftool. Signed-off-by: Stanislav Fomichev --- tools/bpf/bpftool/main.h | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index 5cdf0bc049bd..0a281d3cceb8 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -92,6 +92,7 @@ static const char * const attach_type_name[__MAX_BPF_ATTACH_TYPE] = { [BPF_CGROUP_INET_INGRESS] = "ingress", [BPF_CGROUP_INET_EGRESS] = "egress", [BPF_CGROUP_INET_SOCK_CREATE] = "sock_create", + [BPF_CGROUP_INET_SOCK_RELEASE] = "sock_release", [BPF_CGROUP_SOCK_OPS] = "sock_ops", [BPF_CGROUP_DEVICE] = "device", [BPF_CGROUP_INET4_BIND] = "bind4", From patchwork Fri Jun 26 16:52:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1317842 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20161025 header.b=lVZlNdrH; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 49tjcD4H1qz9sQx for ; Sat, 27 Jun 2020 02:52:44 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728064AbgFZQwn (ORCPT ); Fri, 26 Jun 2020 12:52:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728057AbgFZQwk (ORCPT ); Fri, 26 Jun 2020 12:52:40 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C31EDC03E97A for ; Fri, 26 Jun 2020 09:52:39 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id m63so10224128ybc.13 for ; Fri, 26 Jun 2020 09:52:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=ZvkbolhDYPZ077HIxq1Xu+VvR7JsvXtNZjKWk9szn7I=; b=lVZlNdrHjJmnHZp1C/Mg4IVHrfILElZYzw0HVZGylOCwI80f3ZVmy78tN1n/g/txMm 5EXo102ng9XtkGkuDDSMFZdnJVBYW0Ck2BlCaU6lj5aWqMKsQ67afYYBPu0HBEurBOUx 5vAmqQD8qiKvaxjt0bivJUkg6hx3WhzjVC8C/XeYNfa3Ft2TaKiV6NY+e7RzHKOFOfsA 5tbvCBmV/obWNPm/5qnixlRs+CJGKulSsQgM3KZjr+lKfIdFbQDSgNfzzAee3/YSidOa 1b/DFHo+z3dvsZo8x1LxNvTfPCotb89C8SpcadvsdNsOrqQpRA0TkWHgMwBOczMgIlHf oFcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ZvkbolhDYPZ077HIxq1Xu+VvR7JsvXtNZjKWk9szn7I=; b=EBXj1fTEYGoZukTNLuKobB7BNP1Supja+xjOprU7Cxj5FaVS+lbkuJFfdgZHbQyNCj lmQwo1TlI09GHR7yvY20V3LxqEYyhSc86wh3tNPO3PSpxuTUfx39DjudSzrxvagsGtog EeP1VWatTW9GZg+FWqjWoYXqMkaZYdZFQU2y37VvTdDNxaHJxxfBsdbcZIYYz+gioSiq 4/7o/x8MLIUuPW1OMt8PRQUm5pdngd83G0j3KMtdF6jFV7csn4y+GsEWC2VXOj4IVlGF 1mtsowB0uArlmFET7lB6jeOfcPmoCP77tsNMsMFS+0bT/Sc6N4yJUign5sFrUdfN/sZp CM2A== X-Gm-Message-State: AOAM5336JwbTELbwh3eSa5ZGk6OurBwwmfXx9bJTzZDm+LIosKYzcZII l/wl+WWwyUUINmYTeACg7KYG8dMJSyRNkLqkJpMJDvxnwF9DiSpcelQGzTDfwKkC3+Iyej09uU8 IwFF6ZHwXJi2JcBdyJt/Zv6arsbH513S7L+1f9yylIrvZyD701J4l9w== X-Google-Smtp-Source: ABdhPJzf7JX+OydvL4GIEdUgbNsNk/nEcox1xCQ5ruP6iYvqKcxyq+EsHIOXFOvWCaXuAkuwGNQJhe0= X-Received: by 2002:a25:abd2:: with SMTP id v76mr6280518ybi.111.1593190358970; Fri, 26 Jun 2020 09:52:38 -0700 (PDT) Date: Fri, 26 Jun 2020 09:52:31 -0700 In-Reply-To: <20200626165231.672001-1-sdf@google.com> Message-Id: <20200626165231.672001-4-sdf@google.com> Mime-Version: 1.0 References: <20200626165231.672001-1-sdf@google.com> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH bpf-next v2 4/4] selftests/bpf: test BPF_CGROUP_INET_SOCK_RELEASE From: Stanislav Fomichev To: netdev@vger.kernel.org, bpf@vger.kernel.org Cc: davem@davemloft.net, ast@kernel.org, daniel@iogearbox.net, Stanislav Fomichev Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Simple test that enforces a single SOCK_DGRAM socker per cgroup. Signed-off-by: Stanislav Fomichev --- .../selftests/bpf/prog_tests/udp_limit.c | 71 +++++++++++++++++++ tools/testing/selftests/bpf/progs/udp_limit.c | 42 +++++++++++ 2 files changed, 113 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/udp_limit.c create mode 100644 tools/testing/selftests/bpf/progs/udp_limit.c diff --git a/tools/testing/selftests/bpf/prog_tests/udp_limit.c b/tools/testing/selftests/bpf/prog_tests/udp_limit.c new file mode 100644 index 000000000000..fe359a927d92 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/udp_limit.c @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include "udp_limit.skel.h" + +#include +#include + +void test_udp_limit(void) +{ + struct udp_limit *skel; + int cgroup_fd; + int fd1, fd2; + int err; + + cgroup_fd = test__join_cgroup("/udp_limit"); + if (CHECK_FAIL(cgroup_fd < 0)) + return; + + skel = udp_limit__open_and_load(); + if (CHECK_FAIL(!skel)) + goto close_cgroup_fd; + + err = bpf_prog_attach(bpf_program__fd(skel->progs.sock), + cgroup_fd, BPF_CGROUP_INET_SOCK_CREATE, 0); + if (CHECK_FAIL(err)) + goto close_skeleton; + + err = bpf_prog_attach(bpf_program__fd(skel->progs.sock_release), + cgroup_fd, BPF_CGROUP_INET_SOCK_RELEASE, 0); + if (CHECK_FAIL(err)) + goto close_skeleton; + + /* BPF program enforces a single UDP socket per cgroup, + * verify that. + */ + fd1 = socket(AF_INET, SOCK_DGRAM, 0); + if (CHECK_FAIL(fd1 < 0)) + goto close_skeleton; + + fd2 = socket(AF_INET, SOCK_DGRAM, 0); + if (CHECK_FAIL(fd2 != -1)) + goto close_fd1; + + /* We can reopen again after close. */ + close(fd1); + + fd1 = socket(AF_INET, SOCK_DGRAM, 0); + if (CHECK_FAIL(fd1 < 0)) + goto close_skeleton; + + /* Make sure the program was invoked the expected + * number of times: + * - open fd1 - BPF_CGROUP_INET_SOCK_CREATE + * - attempt to openfd2 - BPF_CGROUP_INET_SOCK_CREATE + * - close fd1 - BPF_CGROUP_INET_SOCK_RELEASE + * - open fd1 again - BPF_CGROUP_INET_SOCK_CREATE + */ + if (CHECK_FAIL(skel->bss->invocations != 4)) + goto close_fd1; + + /* We should still have a single socket in use */ + if (CHECK_FAIL(skel->bss->in_use != 1)) + goto close_fd1; + +close_fd1: + close(fd1); +close_skeleton: + udp_limit__destroy(skel); +close_cgroup_fd: + close(cgroup_fd); +} diff --git a/tools/testing/selftests/bpf/progs/udp_limit.c b/tools/testing/selftests/bpf/progs/udp_limit.c new file mode 100644 index 000000000000..98fe294d9c21 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/udp_limit.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include + +int invocations, in_use; + +SEC("cgroup/sock") +int sock(struct bpf_sock *ctx) +{ + __u32 key; + + if (ctx->type != SOCK_DGRAM) + return 1; + + __sync_fetch_and_add(&invocations, 1); + + if (&in_use > 0) { + /* BPF_CGROUP_INET_SOCK_RELEASE is _not_ called + * when we return an error from the BPF + * program! + */ + return 0; + } + + __sync_fetch_and_add(&in_use, 1); + return 1; +} + +SEC("cgroup/sock_release") +int sock_release(struct bpf_sock *ctx) +{ + __u32 key; + + if (ctx->type != SOCK_DGRAM) + return 1; + + __sync_fetch_and_add(&invocations, 1); + __sync_fetch_and_add(&in_use, -1); + return 1; +}