From patchwork Fri Jun 26 00:09:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1317273 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=cvv3oOX5; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 49tHLl141Cz9sRN for ; Fri, 26 Jun 2020 10:09:35 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725957AbgFZAJe (ORCPT ); Thu, 25 Jun 2020 20:09:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725884AbgFZAJd (ORCPT ); Thu, 25 Jun 2020 20:09:33 -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 C5FB3C08C5DB for ; Thu, 25 Jun 2020 17:09:32 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id w9so7942208ybt.2 for ; Thu, 25 Jun 2020 17:09:32 -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=vkDmfyOJtnFtYzWvfEgCd9QelFOIV4Uf9bIpqIS45QQ=; b=cvv3oOX5TSfIrmzMS63S0JIxCqqxGEr+RjFTxaMoPlfiWlmI3IDQkzBchu3Zl1VuEn C6DRg0J+qkG1UfuIqfnb9fl9AiKmgXiCyXVu3pbKZQ9fxh/pM0GWm1nIE5HKFb8bgF6g 0gDlZjdZ7hrSWq45PDw4d44rbVl7YC98dT/dPMrPEMOGxKA6Y8Ntx+cTd7afFM8M8f3o IBsWhd0M2gJLF7s9mVF6Ak3EVIta5OlePGkbr93/vAmoRGgATMqvYdTtqspb+CzGi/9y JApVMpyI0BSF/IpRHpcJht52iC58S6u4HMxOp0fvxeLp52cUHHYSB+V6ooWEI/cRNbPh Y05Q== 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=vkDmfyOJtnFtYzWvfEgCd9QelFOIV4Uf9bIpqIS45QQ=; b=Q6menkYBdq7/BmIBmxK1HT/giBFtEIwU91EUChHMHuK7L7rN4YXUP2ppMt/4Ozqcx5 Tx+bXUT9sZOVP00P5cwrNiPF4Rj0SUUdc3kphq/ZZiil9ECb/xR8qBFOoNI6QxAhFrmT LYSggAEMX/LoCA4VR63BoA1jemDbm0QSKokA+WNLPaInytcn3H/dxES9+4TGskeKPk0N aNLQhhgusAiZ+IrJHMT0sJWo564FkA4DL7V1wxczdAkm0AQeh4moN8zmJl58nbczvgN/ j262qQulTjC4RRjRmA97QFkNf9xkEzmx+QPyxF7IbS1HpJSx8H8ZsS6mV2GYrqrXW40s tNUw== X-Gm-Message-State: AOAM530xSWHifm2f+t/PNzaOMoMqJzotj5jsJEerOzjSG22QuO+gXjZ8 HAo3O7kJNgR2E0zqj1Xh8abDMq0tnIr+oFEYyFQ5hNQ//aM64yN1x+bfAGNoldCwoG6vMWdS2OI dzeWD6/iAGif5yeips+olObQQEI1XSfZIiLKh/kSjWtbrG04F+0Gb0Q== X-Google-Smtp-Source: ABdhPJwn7cFeIjhdrEPyiYpK8kPTuvvISD5PrZu5UQ6OipDyT8ufD9F7LzQjg562e0i7O8m5xsQBLeE= X-Received: by 2002:a25:18d5:: with SMTP id 204mr608318yby.209.1593130171514; Thu, 25 Jun 2020 17:09:31 -0700 (PDT) Date: Thu, 25 Jun 2020 17:09:26 -0700 Message-Id: <20200626000929.217930-1-sdf@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.27.0.111.gc72c7da667-goog Subject: [PATCH bpf-next 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. Signed-off-by: Stanislav Fomichev Reported-by: kernel test robot Reported-by: kernel test robot --- include/linux/bpf-cgroup.h | 3 +++ 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, 11 insertions(+) diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h index c66c545e161a..b4fd09fe67bd 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) 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 00:09:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1317275 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=KZqWNIfN; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 49tHLm4qDYz9sQt for ; Fri, 26 Jun 2020 10:09:36 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726026AbgFZAJg (ORCPT ); Thu, 25 Jun 2020 20:09:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725884AbgFZAJe (ORCPT ); Thu, 25 Jun 2020 20:09:34 -0400 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09D12C08C5C1 for ; Thu, 25 Jun 2020 17:09:34 -0700 (PDT) Received: by mail-qv1-xf4a.google.com with SMTP id y36so5211070qvf.21 for ; Thu, 25 Jun 2020 17:09:34 -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=ckDyJyIISt4tei12gN9FsxVUG4gEaoCFToLks0PqBtA=; b=KZqWNIfN2CICU1FUSqxVxLHkeSZ+p1D1kdbC+XW0/ARcYNOEa1lUIgfT4lwjMKKJVZ 6ij9J4P4DBf/DgrSiG8EajJfReF0Rwqcsxedy5WMq2KlEx/QcLc7hnYZX54EyqZza4m6 7LU8eDk0/VPHAyvgWQ/fzOiPE9JzmlRE9Rxva8P5R8qTGWc3XlGXQafmfAZur2zdEjE1 zXnehX15silq71pKXHt6LP45DOBCy6pqVRB+lt0Bx/Bxs6B9VWF4SVnKGmxFbohKsWop yV9Bnl2UTRh6JNXvniNEU3NroMcXGMiKP5zFK2Gbpcn1Uxphlp0RFryMq/3vYUVxOji6 Cpcg== 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=ckDyJyIISt4tei12gN9FsxVUG4gEaoCFToLks0PqBtA=; b=jjcKAGdLVYeKmlz4c3rsscZGoTRP/k8OEHiXfyi7cZQFcQOYCwAzJmj92RrEcaQPzS yeb7bRxk8dFI85QQ+Di5t0dIf8ynaNLMPxvILn+q4MDkBcj3WyGJB6BLI0jbFcPCE8yz 8F9imqAQk/atzECrxMRTKPuzq4SLy4RermS5+KT7mxGm3gn5jZAJtpleyqFwXdDg+FfD zaoHuk8w08CnYIF48tPea68X3S80PT13LcVALXKMCEx/jTulkIcnbmz3EqTkZq3eCV3i i5gB96TPBqlNayGZtGJEtO90hpTVl5J4wYsebp35ekj0qiIpg3sThWJlmozD2whlmWCZ btxQ== X-Gm-Message-State: AOAM533fq9+CavPRS5F8QlqbiVv1cZ+X5/S1TbVn+81w7HfXn3F6gvUh l9VWDHoQ2xn3Bm+QG1fyNe0bPGA= X-Google-Smtp-Source: ABdhPJyGLx07XsUcwKpyQcHALJqHFiP2afMYDRlMnkBWFntySvS1QYN/6UwfBLTrYP+s1IP2rYRoLqg= X-Received: by 2002:a05:6214:10cb:: with SMTP id r11mr692728qvs.203.1593130173206; Thu, 25 Jun 2020 17:09:33 -0700 (PDT) Date: Thu, 25 Jun 2020 17:09:27 -0700 In-Reply-To: <20200626000929.217930-1-sdf@google.com> Message-Id: <20200626000929.217930-2-sdf@google.com> Mime-Version: 1.0 References: <20200626000929.217930-1-sdf@google.com> X-Mailer: git-send-email 2.27.0.111.gc72c7da667-goog Subject: [PATCH bpf-next 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: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Add auto-detection for the cgroup/sock_release programs. Signed-off-by: Stanislav Fomichev Acked-by: Andrii Nakryiko --- 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 00:09: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: 1317278 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=OruMzUJG; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 49tHLs355Mz9sQt for ; Fri, 26 Jun 2020 10:09:41 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726067AbgFZAJk (ORCPT ); Thu, 25 Jun 2020 20:09:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726118AbgFZAJg (ORCPT ); Thu, 25 Jun 2020 20:09:36 -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 21BD8C08C5DB for ; Thu, 25 Jun 2020 17:09:36 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id r17so7788347ybj.22 for ; Thu, 25 Jun 2020 17:09: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=Po39cTrb4umUGd/JnKFlg9lYtuzyyRnUnXZQpa3U0lU=; b=OruMzUJGvxCCYiWGiEkVCinWPSdqr1wMBdbZV0GzX37FKfkT849ItKzQF4DwCmkbRD Xa39dWBv2pzYUvw6xuUKvQtx3h3ybZxA5tWZJmB/vkQQpdPL59WK3TZXQZ/mZqMWXD5J IscQp7tvMAunXFqJqypEj3K5cFB5wigaCq2zENnZoFQAHBJow9BpjC2AzIiPKR0DdtQ0 b8/HoDKKfr8Fm+j4T00hbUWlxpEgixT+H5jUCG70+p0fEF1yOfkNCcnfU8AmCgfDTr0g yyMdOH8Py5TN8Si31ppSHBP8yuSRxh4TAk1/dyleuqXul9XEyh4dqxj07ilj8RgUJ0S9 zniQ== 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=Po39cTrb4umUGd/JnKFlg9lYtuzyyRnUnXZQpa3U0lU=; b=nnKQJLla/YTyTf9QmWHWSX4J+rP1eoT1Lg0aID3EpMdLBCZZ/TGx/RI7cyvZJd/P0d mgczQ4NvTggxt44FOSbYu4O1GjaP2d7C/Aw+KDASBb9dXNHyAYKJSKkGrJ+AyeSM/MnV 8hL3Ncw0VeEZ83L7W88PUwa92RYSu0BdP5yedNcxh3BWvrOBYOWpwqO7amcbD9+cNoqK pIJBKq3Yd2czjvieyUH/y/fu3hVKKJ1nLfriQQ5tHMFFZYlGmCMAeOc6CraVXjS4AsYq Jr+VjvmNpZOQo/6ag8dEoQE2D0Vvrzh6BVcT/+7tQp+jf6L3V74n5BwAPP9pRiwS4c6g dTvw== X-Gm-Message-State: AOAM530CX3UuYoFFEyWvi8qvSG+T7W6T+VcEesdsU9sPaoLHcbogYkSe mL1p4dePwXXDaXwKhQIMBuXATtxcZ1u+ZLGln4Lm/rIeo8U4n7JsGYfjiHQWOIgVscb1l3dKSP2 UFdRUnD91IcDWB2GEEJHJ6kpyOFBFHURdTAJNo9QNAuqgiQzEzPPihg== X-Google-Smtp-Source: ABdhPJxCU7Cn8UcnnQpFemEz22ZR+/j2WiU/1JW7YkYSdNLk6xgmRCVfOPQAZJVIbp1qu2tuliJU78g= X-Received: by 2002:a05:6902:4ef:: with SMTP id w15mr740042ybs.44.1593130175290; Thu, 25 Jun 2020 17:09:35 -0700 (PDT) Date: Thu, 25 Jun 2020 17:09:28 -0700 In-Reply-To: <20200626000929.217930-1-sdf@google.com> Message-Id: <20200626000929.217930-3-sdf@google.com> Mime-Version: 1.0 References: <20200626000929.217930-1-sdf@google.com> X-Mailer: git-send-email 2.27.0.111.gc72c7da667-goog Subject: [PATCH bpf-next 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: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@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 00:09: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: 1317277 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=ULYvbk8j; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 49tHLr426pz9sRN for ; Fri, 26 Jun 2020 10:09:40 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725880AbgFZAJj (ORCPT ); Thu, 25 Jun 2020 20:09:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725994AbgFZAJi (ORCPT ); Thu, 25 Jun 2020 20:09:38 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC92CC08C5DC for ; Thu, 25 Jun 2020 17:09:37 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id v16so4935210pfm.15 for ; Thu, 25 Jun 2020 17:09: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=JSRT5WaSjm+Hfa8Hv/bWI3Jcjpw/nnAwcs/ZyVl3qZ0=; b=ULYvbk8jHkvhogTgP0Bewdx+UlEZwl4wsx48kzZQgc0H+SslgTXOQGeKydMUcmieMh QiqxR4DWVeJPevgJyFIWtzhOEO1BEY8HZ+kIJXkddxtINpiulD5F5XPU3lpFKlHuNhn8 5VSaM1kAnpzvfsHSPhCMryNrbTZ4J0NOOhDVoOhoWKc1dZtiCcsgkucjruB4XBySW0/d HaL0bKGVl4LiGeoHjTpebhIQr9CIdBFMfxrM/WXsJFmeUl+FThxOO0PYbmxJcw434vl4 TpE3tmCYvoW1F/MAdh4Uiy9dwLw0zWqQfOKAPz5d6QORjYbRnu6QMTi4dxnWpFkUBw0z D+KA== 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=JSRT5WaSjm+Hfa8Hv/bWI3Jcjpw/nnAwcs/ZyVl3qZ0=; b=m/tOxMd/NeEq1J0wYfApku5fE2z3hsjY3zY7OVZFEeaGKY9h3mOMAJhutGUQ8gKpQP xcQEyw24x3uBDeVmp6e+AnUcKWCm7pme7+28bYKpcHPTg2lz3XKT1izQYMaLaP4Q4FyG m5LrJVB+ARU0fZQhFS7FHdMXEr/O+bcTma6gwkNh7jjwngMmZvMqqsqATS7c5IM2UfAm OwB6ZN6pO3abEZtmSmLTXSdqnvE+u16f208BwsgHoOVaNsWOhdGMngpHUnKjae6hrgSp pPa0G8HBenVKukzfWhDxNhyN+ZkcnPvEgkWgJLb+y+3Q5dTImszJp0v/IE79jvAYJDwb 3xAA== X-Gm-Message-State: AOAM531Ioy5ufS7tmJ3YRRyEK3JWLO5CZwWMrsB8H43cCj5/7zEpNmZe dPW02B5DQYcl1JBjPrEAsHaVEMI= X-Google-Smtp-Source: ABdhPJz+s3w2X1rq3a3/wUf8BzyCPL2H7IsK1g+gGI3WBEsyt/NWsqIzFmMm/Ea18lSYJY8HuA/NFoQ= X-Received: by 2002:a17:90a:6a03:: with SMTP id t3mr550917pjj.174.1593130177216; Thu, 25 Jun 2020 17:09:37 -0700 (PDT) Date: Thu, 25 Jun 2020 17:09:29 -0700 In-Reply-To: <20200626000929.217930-1-sdf@google.com> Message-Id: <20200626000929.217930-4-sdf@google.com> Mime-Version: 1.0 References: <20200626000929.217930-1-sdf@google.com> X-Mailer: git-send-email 2.27.0.111.gc72c7da667-goog Subject: [PATCH bpf-next 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: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@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; +}