From patchwork Wed Jul 1 20:13:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1320785 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=ZzlPIM00; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 49xsqG62Gcz9sQt for ; Thu, 2 Jul 2020 06:13:14 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727022AbgGAUNO (ORCPT ); Wed, 1 Jul 2020 16:13:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726144AbgGAUNM (ORCPT ); Wed, 1 Jul 2020 16:13:12 -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 EFA2BC08C5C1 for ; Wed, 1 Jul 2020 13:13:11 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id k127so22821815ybk.11 for ; Wed, 01 Jul 2020 13:13:11 -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=3D4qcP5foOD863wt3lhC/zFsHrwt1YT0IDFr2PFa5Q4=; b=ZzlPIM00qQzsqKLDIFL6VzATy1g4zWsRu2KVuvCzOZWfB8OR4sHAl2CRM5sxligwBk om1XkD2aiPmpW0+Ns8+03nFvEvsOCF6sYuct5oBoB3f/ai12WRAr19ixoiNqrfzFIw+H nbZzGFqpOHNsQ8plQK8ZB+3Usp0e1LtkUK27LMPQED9h/VlFUnUoOKbHcxddd30XiK28 1nqZWZiAiMIM2rEgiZA7SglCGQe72lHGGsskOBH+KhIbcc8idOjdDsMaCbhZdNfKjX9v qGKjEp9IcM1s+VXp2j8bSrhDyMFhkVZZCMuRXu+yVZOijvU+SvzM/gaSZ46H+68H5Vpt JMyQ== 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=3D4qcP5foOD863wt3lhC/zFsHrwt1YT0IDFr2PFa5Q4=; b=Ym60fkFVkbmvYlJmeENqR5vwBzG4nfSihUKNeyb7ga16PCSPX35wISvZ7d2gcGYK3Q qJyhZvXR/svQcyFxKm2NuBqTaznx1tDFooqDhvDGseEt2LkLYjMDhmYzhjga5Z+bxIYJ 5NmGzjaKlwoujxMWMgIb6sjU53Yx3MdOOnWFHnk4YDp/3wYEc+vvo67JVxg326dxWcZm m1NY6kLoiKGkpVnPOYCCWJVvGdxsjLgpEybzCj9c8G8ZUc6klSDIItTgh7vRrP6va5Yk GUvSu+Awoi+Se915L2dniFaurUvZO3kFIfU+fiEmm6i0czchFI7arvB7tIarujy8LxxV g2hQ== X-Gm-Message-State: AOAM533cRDZ8nJmXFGvyh1jADmucpqOOITlxEa2CH7HWGFVji0iElDUp tiJ53oCwRBfbA1UA9jFlGOfmjgI= X-Google-Smtp-Source: ABdhPJyRkm4KzkhJA/Xv1/2jeJHtCMr7myQiY3kTpIlZJoZita9JYCo5dKhsUMlPtl5N/mib7yZCzEY= X-Received: by 2002:a5b:548:: with SMTP id r8mr45060274ybp.322.1593634391203; Wed, 01 Jul 2020 13:13:11 -0700 (PDT) Date: Wed, 1 Jul 2020 13:13:04 -0700 In-Reply-To: <20200701201307.855717-1-sdf@google.com> Message-Id: <20200701201307.855717-2-sdf@google.com> Mime-Version: 1.0 References: <20200701201307.855717-1-sdf@google.com> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH bpf-next v3 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: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Implement BPF_CGROUP_INET_SOCK_RELEASE hook that triggers on inet socket release. It triggers only for userspace sockets, the same semantics as existing BPF_CGROUP_INET_SOCK_CREATE. 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 --- 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 da9bf35a26f8..548a749aebb3 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 8da159936bab..156f51ffada2 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -1981,6 +1981,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; @@ -2779,6 +2780,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; @@ -2929,6 +2931,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 c5e696e6c315..ddcc0d6209e1 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -6890,6 +6890,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 Wed Jul 1 20:13:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1320787 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=XPFcwwz+; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 49xsqH4J0Bz9sRf for ; Thu, 2 Jul 2020 06:13:15 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727018AbgGAUNP (ORCPT ); Wed, 1 Jul 2020 16:13:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727024AbgGAUNO (ORCPT ); Wed, 1 Jul 2020 16:13:14 -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 C6952C08C5C1 for ; Wed, 1 Jul 2020 13:13:13 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id z7so27444257ybz.1 for ; Wed, 01 Jul 2020 13:13:13 -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=KeQTrd37BEFhJhhFg8eLib2y6OHrwfUDJnin6VhVDDE=; b=XPFcwwz+IN0GhlhFtLkS54T/AZox3vukZ/EUvPT/i0rbNqsZm+7ptZp6276Y0MIU7+ zC7FNTBq8N7L4wY4fDKZ60+fAhTJGJQyvfPCVmzo1IKbh2DmWhuzvzsvft0La448sT6H yZWPG3KepMC7HMIor0MvX7mDyp8mIwsUSuwe1FvaSQP15JJ9SZMYAER72sVUUF+4X7AJ N0vEvGbIyoI3ZsstYS6khx6KqC+dInvaT4ppZws44O2ZjIYseM8SVQThZv4BV/mmqxdH qNuZ8VYr1we2UkHAO2V6xvapYhgcm49pQMDJnRMNjRBDYnk4Cl2rZWMxup4LwwUEyAG6 M4aw== 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=KeQTrd37BEFhJhhFg8eLib2y6OHrwfUDJnin6VhVDDE=; b=dCDYth5UBCEp0CCgEYYmRMRCXzxnEwdlKZ6mgkEaO35LBvhgRI/5QOtA61x5qXjpDV ALmz2QVcoHXp/G0WpIyX4SnPBuhNG+KNTvUX6XRqTTlPVfK8wu32iZcEl73V/WMeSdz1 FCM3KOPAy9exMm2/837zYuy6TNZwKi4+XrUAd7I7VrUdeNIOk9vweLoPU0WYwvbr/Eu+ cBAiwXbWuYetzKXUC3q833Pr2d20rue31T/l8z923PYRN/WnRtYdpDV9hCzDGeGVo8kh hVj8OVFvwG8JuWnAYzHLbo7OB4F1tsD/PM5x72VUGkvkOe2p1BGEpKkLP26XxutGV+ox Rzfg== X-Gm-Message-State: AOAM533Vl/K170xzEEqeIkj1bVcKCfUG/n/iWtmrUslkA9jqk8mA8Lrc CI3OqzmrMQq0q1dSWN06PNcpHis= X-Google-Smtp-Source: ABdhPJxz3YBEHyQoZOW9dyvNhF7Y+L81FC/JAfShrbT3eSsWT6KVByCnW9l5xQYbw0o6jDJ6VV/EmKk= X-Received: by 2002:a25:3f87:: with SMTP id m129mr47200320yba.371.1593634392985; Wed, 01 Jul 2020 13:13:12 -0700 (PDT) Date: Wed, 1 Jul 2020 13:13:05 -0700 In-Reply-To: <20200701201307.855717-1-sdf@google.com> Message-Id: <20200701201307.855717-3-sdf@google.com> Mime-Version: 1.0 References: <20200701201307.855717-1-sdf@google.com> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH bpf-next v3 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 , Andrii Nakryiko 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 | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index da9bf35a26f8..548a749aebb3 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 4ea7f4f1a691..88a483627a2b 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -6917,6 +6917,10 @@ 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_create", BPF_PROG_TYPE_CGROUP_SOCK, + BPF_CGROUP_INET_SOCK_CREATE), + 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 Wed Jul 1 20:13:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1320789 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=oMaPlUn9; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 49xsqM3GpDz9sDX for ; Thu, 2 Jul 2020 06:13:19 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727093AbgGAUNR (ORCPT ); Wed, 1 Jul 2020 16:13:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727080AbgGAUNQ (ORCPT ); Wed, 1 Jul 2020 16:13:16 -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 B9B8CC08C5C1 for ; Wed, 1 Jul 2020 13:13:15 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id k127so22821964ybk.11 for ; Wed, 01 Jul 2020 13:13:15 -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=i6gJyiIxYw3Mhy0FXom58Ey1CcFrWZFXEjpF7LY96KI=; b=oMaPlUn9w3Drti1hkWqPDJfHfosD8TdQP1j5/C7ZDcZxHCFiFIeNb5mNwXjzlNw9mZ L8EXTFXn2OUW0VLsKkjOjRj8qYryygMfyNXAxJCmGupKStqkSWjaBvRwru020dOB3JFJ Bflp4gfjelZbVhClCxKE6K3VaZr8oysMU7/iljso+gc8VzTJctjVuaY2g/lLe1wwBRtX 4qGFXl8FNu0Ne+trUwv4eWMS+F3mu1UrUMh/ltLtmOHf7ZnG5Nezni5ipgvdLd0EeXrk 7fiXfdpz09NYFprMf46BzmnZpoMcerhXBdGrtgRNTbFhwl0FsnLpuEYA/Etgrbu5DW/k 5s7Q== 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=i6gJyiIxYw3Mhy0FXom58Ey1CcFrWZFXEjpF7LY96KI=; b=o9jgEQ0B1Zvj1tPNlwyBY9+AFBAZ0FF6HwHspm7ZEfvK5u1R8LMPdYDC5ZAIpiznbQ iTeoifes0xOuKtESBwD10IoRFpYWK7u0w/+UfAET2XAYeCFq7t2ldJYe2ls3KD5zUA2s neKQEEPBiXF2DVaarleChq1kP86VeCk87/sbgVSYyQA0wn9w1qgoV5vMBaXgKgiuSYr5 acjtE6p9PSH0emZt7u5PHKFt+kQPVjNJSVx4B/8C6A0ur455HV+OKHAX/uUTBz4vB4yN Bp4xi5rqg1Kj12LCxc8VSvN2m4CoAIpGLGvY1vN9MqZzeTm6Qj951MYmSUjrOGF/fUpy LPcQ== X-Gm-Message-State: AOAM530MVx96bpPbG5qmnLVf2GpzRQFtvlOvYEpBvRGQALIZmPSKct6N uGyVn/nQ+bTXt8c4iHsr3BcPQec= X-Google-Smtp-Source: ABdhPJyzLFeUucBVyixZXQtS+WX38VhzapIV0jvyJ5BCvGLslz1afjYtPuf7rTszC3nmDXsBql0QdKo= X-Received: by 2002:a05:6902:4a2:: with SMTP id r2mr46048265ybs.176.1593634395023; Wed, 01 Jul 2020 13:13:15 -0700 (PDT) Date: Wed, 1 Jul 2020 13:13:06 -0700 In-Reply-To: <20200701201307.855717-1-sdf@google.com> Message-Id: <20200701201307.855717-4-sdf@google.com> Mime-Version: 1.0 References: <20200701201307.855717-1-sdf@google.com> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH bpf-next v3 3/4] bpftool: 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 Support attaching to BPF_CGROUP_INET_SOCK_RELEASE and properly display attach type upon prog dump. Signed-off-by: Stanislav Fomichev Acked-by: Andrii Nakryiko --- tools/bpf/bpftool/common.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c index 18e5604fe260..29f4e7611ae8 100644 --- a/tools/bpf/bpftool/common.c +++ b/tools/bpf/bpftool/common.c @@ -33,6 +33,7 @@ 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 Wed Jul 1 20:13:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1320790 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=k5De39mp; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 49xsqN1W9Nz9sQt for ; Thu, 2 Jul 2020 06:13:20 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727080AbgGAUNT (ORCPT ); Wed, 1 Jul 2020 16:13:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47732 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727047AbgGAUNR (ORCPT ); Wed, 1 Jul 2020 16:13:17 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84CCCC08C5C1 for ; Wed, 1 Jul 2020 13:13:17 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id v24so18798056pgl.5 for ; Wed, 01 Jul 2020 13:13:17 -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=Ad9C5uLahkSIPyWJgO1D3o2iIwB9TsBxO1NANkErTuM=; b=k5De39mpv5M9E4DyG0KPOpg7dz+geldPUSWUvDcQeQ022JKmZlJalx1fLPOyYKrHma LDcUqAks+wCM+6bTRbk22M/7sFOfoc/TmpFXRXhd0Nq03TDjoOPHKGpl2ztNadOkc7F5 1xE01ANFE98ww2sPnBOkQjGqksBi58GgYKf3WB8APKrpGo6YGVWeI6fHBozNwtH680a/ 06KZRBHkz1rRH7Cs3BubONYEoMlNQ4/Ivc32mwqQ0uhXVTNSlL2Yz2HEDPsooC5qvvLa HsdkQpsvhRbyC19LfWt7S6JLfAM3kxf/n8OFuRj3Q4HflFErdNOJM6gEdFURLsl/m1cH D8SQ== 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=Ad9C5uLahkSIPyWJgO1D3o2iIwB9TsBxO1NANkErTuM=; b=L/cIsSYDUBKyUIQSPF5dybp8sKDA3oi8MXla+EDTIO1zv+ljsRSbDcxOLC1Rl5B3p2 mXY3c7DixXfPUqKXxcmNECOc773tQFBSIisN7k8YJUGngUNT8tREGE8QGkJWzBOpdJeW hqeKbYRvVoR1yCpse/H3H/z/IiV1qnX7c7/HNJWkaY39yvNjq9UjQxrEfsHNDcZRk+Cd AYUr/z2JnCIHsYep9FeNVl4q8ZPGnNVY4x4ewMPWL5hwYIeh8/AeVmPWVR5+qX5+19rE 5FApVFp6QnU7SfTUTraI6TLY8e0ZzMzH2aP82Kz+PwYxYdSOPHzKpnP7u7a2NHa3YgiN g1kQ== X-Gm-Message-State: AOAM531MYvWc/xySmB7dYEMg79SMVYTiXxFlLGpH534JlW8vNnsnihuI f4mPn+0NcxdP6iToyz/oN/gBRg0= X-Google-Smtp-Source: ABdhPJyHNKxAD7DnQh+lXn42+VFi5PlLvtt3s8loSTq+5XmYVRV4R7c13S6K53SH/Ri889B0suWgMJs= X-Received: by 2002:a17:90a:1c16:: with SMTP id s22mr29307947pjs.108.1593634396942; Wed, 01 Jul 2020 13:13:16 -0700 (PDT) Date: Wed, 1 Jul 2020 13:13:07 -0700 In-Reply-To: <20200701201307.855717-1-sdf@google.com> Message-Id: <20200701201307.855717-5-sdf@google.com> Mime-Version: 1.0 References: <20200701201307.855717-1-sdf@google.com> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH bpf-next v3 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 | 72 +++++++++++++++++++ tools/testing/selftests/bpf/progs/udp_limit.c | 42 +++++++++++ 2 files changed, 114 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..510c928c1496 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/udp_limit.c @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include "udp_limit.skel.h" + +#include +#include + +static int duration; + +void test_udp_limit(void) +{ + struct udp_limit *skel; + int cgroup_fd; + int fd1, fd2; + + cgroup_fd = test__join_cgroup("/udp_limit"); + if (CHECK(cgroup_fd < 0, "cg-join", "errno %d", errno)) + return; + + skel = udp_limit__open_and_load(); + if (CHECK(!skel, "skel-load", "errno %d", errno)) + goto close_cgroup_fd; + + skel->links.sock = bpf_program__attach_cgroup(skel->progs.sock, cgroup_fd); + skel->links.sock_release = bpf_program__attach_cgroup(skel->progs.sock_release, cgroup_fd); + if (CHECK(IS_ERR(skel->links.sock) || IS_ERR(skel->links.sock_release), + "cg-attach", "sock %ld sock_release %ld", + PTR_ERR(skel->links.sock), + PTR_ERR(skel->links.sock_release))) + goto close_skeleton; + + /* BPF program enforces a single UDP socket per cgroup, + * verify that. + */ + fd1 = socket(AF_INET, SOCK_DGRAM, 0); + if (CHECK(fd1 < 0, "fd1", "errno %d", errno)) + goto close_skeleton; + + fd2 = socket(AF_INET, SOCK_DGRAM, 0); + if (CHECK(fd2 >= 0, "fd2", "errno %d", errno)) + goto close_fd1; + + /* We can reopen again after close. */ + close(fd1); + + fd1 = socket(AF_INET, SOCK_DGRAM, 0); + if (CHECK(fd1 < 0, "fd1-again", "errno %d", errno)) + 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(skel->bss->invocations != 4, "bss-invocations", + "invocations=%d", skel->bss->invocations)) + goto close_fd1; + + /* We should still have a single socket in use */ + if (CHECK(skel->bss->in_use != 1, "bss-in_use", + "in_use=%d", skel->bss->in_use)) + 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..af1154bfb946 --- /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; +}