From patchwork Mon Jul 6 23:01:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1323993 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=F9WEhSlw; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4B11KB3rNwz9sRN for ; Tue, 7 Jul 2020 09:01:34 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727008AbgGFXBe (ORCPT ); Mon, 6 Jul 2020 19:01:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726903AbgGFXBd (ORCPT ); Mon, 6 Jul 2020 19:01:33 -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 0C3B5C061755 for ; Mon, 6 Jul 2020 16:01:33 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id 13so12524907qks.11 for ; Mon, 06 Jul 2020 16:01:33 -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=+vrVjn18hLwzji4h54Yc/RlbA6Fx90Vj5CfTJ215CUk=; b=F9WEhSlwE4qVop4xztWFVYGCZeSNl2iTKXQgEJKisJ1/tC3RiZNLdt9jDcQXSwNJW8 Nq5ywtfFjBKpB1n9dXX+4HzJLlgYNXflc+rqyBdfMj4AefGnIqllsDQmzAiQz/z3G8ep xixs37LzDTk36D5ogLmAFFpMXsStFRYix2hxBf1GvG0e+GVjRrNhAYn/Cvt+Wq5kOLAp clnpm8sNRJsbTqQ/+P1OXY8O9kKFMJmwPJ/9/n3Ie99jAy7gb3fzZ5P9QcWEhZ7OObVk srJ+uaPrYYTigi9fyt3cCpiElvuXRVlQGuJsbLngjkuUfG2tfhwwrQ+0dM19eABp2LSB AnMQ== 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=+vrVjn18hLwzji4h54Yc/RlbA6Fx90Vj5CfTJ215CUk=; b=KIEWUUTYkGLkCWcwCGzqHkrUETAYsMsS74wczOKRIxbHOr3fDoVAN4kylUhFpTwkx7 sQAtz7BE9UTcRsFeN5/CLs/ocBtxC5AWdCSVScF1CElnDWBf+uCPUgX+krqyFoaeSkad 7sZgkWexygmvEJjSIuwYXDeAw50SwBDvuKjSpdiuzJJSu66l3RCig99Z4A/2JHi/e/jl 16b/sKw9QLdPrS9MPWsi1WoBQgnV+4+adoEIqm9NIFPHbxVjDU0YJZRd1wOUV34YxP6S EEI2N1K1ysCssvvUgTans3uBuh8Laz8Kt9GxrBDPr4J/8X5apSh5Q/GWoZrwR1K6r8m8 ouiA== X-Gm-Message-State: AOAM530XU4sDjkK0etAQW5xECpioPO/qB35e/WHQQLGgchwaFPME5c+N VgvA+EZekxNB02r6yC2mp5y3ISI= X-Google-Smtp-Source: ABdhPJy4fj0RKrX1qfygM9t/RRkYhzN9xzR196KlXCXcagWGy7DLhXM9A/tNN+JfA+Nl+XYwW03cYyc= X-Received: by 2002:a0c:e14d:: with SMTP id c13mr16788506qvl.158.1594076492231; Mon, 06 Jul 2020 16:01:32 -0700 (PDT) Date: Mon, 6 Jul 2020 16:01:25 -0700 In-Reply-To: <20200706230128.4073544-1-sdf@google.com> Message-Id: <20200706230128.4073544-2-sdf@google.com> Mime-Version: 1.0 References: <20200706230128.4073544-1-sdf@google.com> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH bpf-next v4 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 Acked-by: Andrii Nakryiko --- 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 ea6ed6d487ed..ff141d630bdf 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 Mon Jul 6 23:01: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: 1323995 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=OI5BBKCH; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4B11KF5jRhz9sRN for ; Tue, 7 Jul 2020 09:01:37 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726763AbgGFXBf (ORCPT ); Mon, 6 Jul 2020 19:01:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726802AbgGFXBe (ORCPT ); Mon, 6 Jul 2020 19:01:34 -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 C8ED4C061755 for ; Mon, 6 Jul 2020 16:01:34 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id i203so14954081yba.9 for ; Mon, 06 Jul 2020 16:01: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=KeQTrd37BEFhJhhFg8eLib2y6OHrwfUDJnin6VhVDDE=; b=OI5BBKCHe9yMHhm+QwNaQGayQifvpsnSAOlIPrU/WOn4p8C3G+ue3VpzJrcBgquQ6H z21Vy6oBM1OPbuLOHqJa3EABRW/fb8dCSayZaq9yr79z4HpwbNX30tgVU+0UXOCwDhtA SY8o0njtzdHCI2S9JDo00pSk/oGLqVldDHI3z1icx+avTwoIR6v+mi3j8fUXzzjz4OAp zGtcrTa20WdxJ9N4ywv8UDLmeQLAhWU8UE3qCJ7Xgw81gy4epedR358A3igb8cTpTIVs 7HhhvLve4jLXvtu6BYUWoPKW/EWQp7BkIgJ2809JClMgYaCtpeqPzNEbfoh4D7gsX8QY fHYQ== 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=Tf1kauLeSIsHuQDpLbdHUv7bzNbPQ8/CRLFtLcEpVAoNG4k/0np4u1WIuq/OOzUhC3 Sl1YGmOZR/SM4CxTusBH23Z5UG5vpGR5o4ZI6pTukWBIxlsYfmB2QZSyoSom42OyxGdp DUIIJ4wrxIfMFMCZmz853kgFvdq1N4v0L6zJJlZtxI/f1UWClJwpgKLBtnxHWe/kDhwR RhzVqihn8d8B4eb1XQYWPdsDiW8GsMRGb50+rnxoOF13GPEwv4zEmFQ/KjzhAY/lnRMP 0jUvKYK/ZATRSSnhfB9PHgzgq8shU8eDKvD6C62CI5yaErSP5uUvGME/Ujoeo7eiqT19 zeDg== X-Gm-Message-State: AOAM531Zcm1pT/LZDgv6z8SKu9CzZSDMRoSCiCMBk56ZU3O3gBgli2SR ZbVNjpA4MIDYNS61H+c36xwsNPQ= X-Google-Smtp-Source: ABdhPJxS/FVlLfQoHDKrYZ5hQ33tvftY8xxsUUaGDRo3PeqNb1/bKLYYJK/E5BT9en5R0qX/DjiB83c= X-Received: by 2002:a25:5d2:: with SMTP id 201mr60561397ybf.280.1594076493984; Mon, 06 Jul 2020 16:01:33 -0700 (PDT) Date: Mon, 6 Jul 2020 16:01:26 -0700 In-Reply-To: <20200706230128.4073544-1-sdf@google.com> Message-Id: <20200706230128.4073544-3-sdf@google.com> Mime-Version: 1.0 References: <20200706230128.4073544-1-sdf@google.com> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH bpf-next v4 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 Mon Jul 6 23:01: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: 1323997 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=Eq9NO4Ly; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4B11KH3nWGz9sRk for ; Tue, 7 Jul 2020 09:01:39 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727059AbgGFXBh (ORCPT ); Mon, 6 Jul 2020 19:01:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726845AbgGFXBh (ORCPT ); Mon, 6 Jul 2020 19:01:37 -0400 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBCECC061755 for ; Mon, 6 Jul 2020 16:01:36 -0700 (PDT) Received: by mail-qt1-x849.google.com with SMTP id d45so21574011qte.12 for ; Mon, 06 Jul 2020 16:01: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=4MFbPpxWqiWyQiCt/Y8/V+3EAFWxMVX6naLO9kXQaiI=; b=Eq9NO4LyHzXl17RJbq3NY1XVlrLBiLBZOAKnJw6WmZp9W2ylG+snyE0izLxMppHOAB qHMBf1AKbai+aIV4405PHilpczpmnm5mj473g+qnve1KwJSriQ1rkeIviaExSO1eVByi 0upqdV1Q/FGBWnyVtQdY5nXRA+C7Eh0ZJEvrtghcQxoDoEk98QAx44GekZ7qAXmn8VMU ISTsZ6j6RxCJHvuukvecyhFC6cJg2roN62dtQX64x/NBynwHV4aHAdiGfd7MpWz2COjf pXLTca7ISKwpLCPQQSIJPnU0qBg+x3jWjTU75J9xGrflWIsVz3wVb2LVj2+7UhTVSTpd II2A== 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=4MFbPpxWqiWyQiCt/Y8/V+3EAFWxMVX6naLO9kXQaiI=; b=q84pjSykA2ShVKLmgP6D0Ou6WVh0NHvVVMyYEQOPzTHmiwezQcN0rIFmPSFGloVHVY 4asSLjc3zncHA8EoOvJXEgB2ESWtrsti/u3R7cVna7Gya4xciIKW38pRfOwBqbqY3rcV YzSX658+jexJNZhGZuQUbkpJYVr3AaGIgUjHaCvPkDLldprPOAzSpSadHkrTUZJK1Hln 4iuIW0/cKUBnjSbgXv7HC0M0IwlTl+3tMsIssKggfFuhF+Z9ek/xDcK6kfyf2bE7MkdC gT6K9KhcGbAUvEc1zzHp6b1wLWHnkSGOlAFvOxVqfYyH23nAygjLy5ZVi/lVy5+5IA/3 Jp7A== X-Gm-Message-State: AOAM532ldrr3w1a8nmOSRSxOIr4QrdGzrhI27LGTlNneLS5C5GrY/nJe 0wZoWk7R7BP00/+r+0rHaDMq9wM= X-Google-Smtp-Source: ABdhPJyPmSwRU4P3JJ//cB5ksinMQOKBAYC+Opzi5hOvf9AgYRcNxPR3KMIsGd0I+Fkm3ujJi309ap0= X-Received: by 2002:ad4:4a64:: with SMTP id cn4mr48875063qvb.199.1594076495935; Mon, 06 Jul 2020 16:01:35 -0700 (PDT) Date: Mon, 6 Jul 2020 16:01:27 -0700 In-Reply-To: <20200706230128.4073544-1-sdf@google.com> Message-Id: <20200706230128.4073544-4-sdf@google.com> Mime-Version: 1.0 References: <20200706230128.4073544-1-sdf@google.com> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH bpf-next v4 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 , Andrii Nakryiko 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 Mon Jul 6 23:01: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: 1323999 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=TihZ8FZl; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4B11KK5lK3z9sR4 for ; Tue, 7 Jul 2020 09:01:41 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727077AbgGFXBk (ORCPT ); Mon, 6 Jul 2020 19:01:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727074AbgGFXBi (ORCPT ); Mon, 6 Jul 2020 19:01:38 -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 A2F8AC061755 for ; Mon, 6 Jul 2020 16:01:38 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id m125so5631597yba.23 for ; Mon, 06 Jul 2020 16:01:38 -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=UOfLeSIX/vN48wokU9rUl/nDRnnmynb7K/LiRKTXjzM=; b=TihZ8FZljIektigHuHaz5Lc4zSNs42KLqY5hGphgyBeuP9kih3gMiU0tq2KR/eL1Yw pk6YrXQfjx25k6nrIxrVhEt2Sh0Zxek9q3zf6/2ki8TkpnaOshNiPrGuJHE9NCtcpIzv hMeYZ79GqWYOacs2mVdwDgj2sMqD4kxn+jtD0tfClYRPXGT7f6QYTC1UDDpr7Kd9X01O o7n/le+fj3LQA7Sb7fm0xM/TAc3duRRJwafm/NYA07ysM6X/NZ+4qvJPlvIz2e5+JAzs k3SyTdCZMW1XP0JSnv/z2qCtRZEOjP2yfRzK/OTPuNE9zTN7r0n7GI4LZKMD6TvXmRJl JFSQ== 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=UOfLeSIX/vN48wokU9rUl/nDRnnmynb7K/LiRKTXjzM=; b=h6XQZ8raeNFqsePOklsO5b2lq09fEwSio778DvifgbBEbutxilNbRnYAuknMOLBmoN gMQjspR0FSupEvMFYjyKVyf/81mZfOO+aMghjXniu5SXLXMtPK1a/ADEpV1vEh9BMeSj 1sEZhaIERQVBBFfs/JnunWW9IvGslFQyF4RyM3nKEKt3PRF+VKLw0wQs+9e3xZ9PG+sY zU30BxbnpXhaKKYNva1gdKRkYzBnnVmHvTzlGLDGT/bUCxG8wc3Dn/Ysu5+KW7iAeomz RJfVwcM/SaID6shvVEPqeDNWGDy49Wvqes3hCpRtfeLNbAC08xRy9I8TPiE7Bh/nLOv+ sosA== X-Gm-Message-State: AOAM5308x12uxK30jmcn3TtYU1Iibkhhn688ThMNGDlIPILqWalCBKXt gxs+OS8SGcmgsFasCFBY2U8nnes= X-Google-Smtp-Source: ABdhPJxU8uAHBKnpXVE70pUxqCtMUYzKf6tRJfOWQ4I91xzNcxCgITgO+fVd0v/sBCOBcrsiHUu02sk= X-Received: by 2002:a25:ae01:: with SMTP id a1mr17679132ybj.119.1594076497898; Mon, 06 Jul 2020 16:01:37 -0700 (PDT) Date: Mon, 6 Jul 2020 16:01:28 -0700 In-Reply-To: <20200706230128.4073544-1-sdf@google.com> Message-Id: <20200706230128.4073544-5-sdf@google.com> Mime-Version: 1.0 References: <20200706230128.4073544-1-sdf@google.com> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH bpf-next v4 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 Acked-by: Andrii Nakryiko --- .../selftests/bpf/prog_tests/udp_limit.c | 75 +++++++++++++++++++ tools/testing/selftests/bpf/progs/udp_limit.c | 42 +++++++++++ 2 files changed, 117 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..2aba09d4d01b --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/udp_limit.c @@ -0,0 +1,75 @@ +// 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 fd1 = -1, fd2 = -1; + int cgroup_fd; + + 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_skeleton; + + /* We can reopen again after close. */ + close(fd1); + fd1 = -1; + + 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_skeleton; + + /* 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_skeleton; + +close_skeleton: + if (fd1 >= 0) + close(fd1); + if (fd2 >= 0) + close(fd2); + 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..edbb30a27e63 --- /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 = 0, in_use = 0; + +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; +}