From patchwork Wed Jun 12 17:30:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1114723 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=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; 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.b="CpQCaylW"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45PDRV11Djz9s6w for ; Thu, 13 Jun 2019 03:30:45 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2409541AbfFLRao (ORCPT ); Wed, 12 Jun 2019 13:30:44 -0400 Received: from mail-yw1-f73.google.com ([209.85.161.73]:39674 "EHLO mail-yw1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2406395AbfFLRan (ORCPT ); Wed, 12 Jun 2019 13:30:43 -0400 Received: by mail-yw1-f73.google.com with SMTP id w127so18000007ywe.6 for ; Wed, 12 Jun 2019 10:30:43 -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=aYBwDRPMLFch8pErCdIVdR1XezeWG6lP+OS+4p/QF+c=; b=CpQCaylW6r4KuQiFK2ncRi0OFhoAF4GV8L7k1BLZzapxVl+Frhylr8y2Y3R2qRTIo4 BbquX9A6+i3MWQ/FW2no1uDA4zc4cUprLSu5f2XAiEma7k4Wrb1qqDkWT3kCeD08tAIY EpLbDFfcgRluPaUt2hwGk5Lr1pQENs+1+J/hQL5W50c3OOdGZ/blvI7+jdhipHCXP7Vd t5j4Y5TGVmyoFoSY3OtXw/CSIOkkDFHnCgzSoiJofYlKTY/fnnrhRGnt1hI3WCxlhEQ4 nq75c0i/yISy3tY6rsM0P+lO2bwNd1CVXnazov+L8hYJlgEd4f4BHAzBerePOK2fcLvm RumQ== 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=aYBwDRPMLFch8pErCdIVdR1XezeWG6lP+OS+4p/QF+c=; b=fT4nI9wSn7RLK0H4xwdWBhJ99ywplIzNbjHOqj8eBUhBTYXTARlpThxQiJEf3i9NpD djGnSkQhilnDWOXxxr/DMgG+RXVP4Bx7aJIgObx/wEHE2ZH6nWZECtsEwn4U8yZvzxCw rVieCHXZapJc0RC7GQXrFla1ziDbYcFsCYcFp9L+IV1eU/F97gXG4BuTB18qQ4lajD7q 2DZ0CCctThNt/7cH7NcvgKz+LG+vXBASYrGwvpO9Lg3D3Gm+XdKiTKhMt65CNzx3tO6s DGCo7kUchUotC/Rx8Au2ZQN4Lb5L0WbqNf1B3EPsPNSwzqrznsdlpR2K5Uu//8AiB8Fz Ys5g== X-Gm-Message-State: APjAAAWQmZ9MXUnQA2y0SamHJEUsd84YNG5TT9gCHUS+4zu/2MDL8ktV gBXSZDVt9YZyIU08Valk89Zimaqy7Oi0DOXFJgQOqYRU0FNvk6XGCIVEgEGz+V7ypJ5HRkzA8R+ tS9vX7f/+9dhE3w6Qp3uBYuQpoZMjMkZ4rKrlZio2DXTq1DvlYhkv8Q== X-Google-Smtp-Source: APXvYqwsn4/amuGEz22WG8rYKmA2pTovjkohArEshMqJ0nEKfzISTDi8W5ZDi2i3k6uBkVUHB+OSj+g= X-Received: by 2002:a81:f90:: with SMTP id 138mr36999084ywp.425.1560360642931; Wed, 12 Jun 2019 10:30:42 -0700 (PDT) Date: Wed, 12 Jun 2019 10:30:37 -0700 Message-Id: <20190612173040.61944-1-sdf@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.rc2.383.gf4fbbf30c2-goog Subject: [PATCH bpf-next 1/4] bpf: export bpf_sock for BPF_PROG_TYPE_CGROUP_SOCK_ADDR prog type From: Stanislav Fomichev To: netdev@vger.kernel.org, bpf@vger.kernel.org Cc: davem@davemloft.net, ast@kernel.org, daniel@iogearbox.net, Stanislav Fomichev , Martin Lau Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org And let it use bpf_sk_storage_{get,delete} helpers to access socket storage. Kernel context (struct bpf_sock_addr_kern) already has sk member, so I just expose it to the BPF hooks. Using PTR_TO_SOCKET instead of PTR_TO_SOCK_COMMON should be safe because the hook is called on bind/connect. Cc: Martin Lau Signed-off-by: Stanislav Fomichev --- include/uapi/linux/bpf.h | 1 + net/core/filter.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index ae0907d8c03a..8815fc418cde 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -3247,6 +3247,7 @@ struct bpf_sock_addr { __u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write. * Stored in network byte order. */ + __bpf_md_ptr(struct bpf_sock *, sk); }; /* User bpf_sock_ops struct to access socket values and specify request ops diff --git a/net/core/filter.c b/net/core/filter.c index a5e4ac7fcbe5..37c4a2fd559b 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5922,6 +5922,10 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_skc_lookup_tcp: return &bpf_sock_addr_skc_lookup_tcp_proto; #endif /* CONFIG_INET */ + case BPF_FUNC_sk_storage_get: + return &bpf_sk_storage_get_proto; + case BPF_FUNC_sk_storage_delete: + return &bpf_sk_storage_delete_proto; default: return bpf_base_func_proto(func_id); } @@ -6828,6 +6832,13 @@ static bool sock_addr_is_valid_access(int off, int size, if (size != size_default) return false; break; + case offsetof(struct bpf_sock_addr, sk): + if (type != BPF_READ) + return false; + if (size != sizeof(__u64)) + return false; + info->reg_type = PTR_TO_SOCKET; + break; default: if (type == BPF_READ) { if (size != size_default) @@ -7778,6 +7789,11 @@ static u32 sock_addr_convert_ctx_access(enum bpf_access_type type, struct bpf_sock_addr_kern, struct in6_addr, t_ctx, s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg); break; + case offsetof(struct bpf_sock_addr, sk): + *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk), + si->dst_reg, si->src_reg, + offsetof(struct bpf_sock_addr_kern, sk)); + break; } return insn - insn_buf; From patchwork Wed Jun 12 17:30:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1114725 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=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; 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.b="G34nbtNB"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45PDRW4HP5z9sDB for ; Thu, 13 Jun 2019 03:30:47 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2409547AbfFLRaq (ORCPT ); Wed, 12 Jun 2019 13:30:46 -0400 Received: from mail-qk1-f201.google.com ([209.85.222.201]:42734 "EHLO mail-qk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2409542AbfFLRaq (ORCPT ); Wed, 12 Jun 2019 13:30:46 -0400 Received: by mail-qk1-f201.google.com with SMTP id l16so14316705qkk.9 for ; Wed, 12 Jun 2019 10:30:45 -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=uD0S0Fr977XbAf3v96x/HVlLPiyEpqZkm6CH6Dt+DGw=; b=G34nbtNBuyjHlA8kXx04tv5bCmuFC1FutZl6xaMcCQkcRMrwTJz4hWQdD9zLwvxR4c tvp7rqzLJj85Pkcrw7Fth45UK2p0iznaMYtoEePBVD/Eg3tz5kmc257vB3+p/U7tj1nI f6Db9Q5a+gMBkPifracEuq72k+7uWGbm3unciwFBao61B/8+/4wLJsrlM0iM5om4GW87 1HQFC1+GseGtlpAb4PtmSJYrkjMBVnuAtu2Ln47rDUKMbv12lXZIJuCDvgT7WZbx+DrA FK93rnnakjAbR5ro1ZW6DTvvduf/f12AYHV+l1yMSmmyIE5GU1DIkbOSQJG2R74Rc2s3 FhVA== 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=uD0S0Fr977XbAf3v96x/HVlLPiyEpqZkm6CH6Dt+DGw=; b=MZ4ZQNU4Zt+koiDUF2cqYxLHQZgYmncGy/EvRMInFqiM/ZprEwikGDDItFHHsZKZ7E qQLxLAoJKEeYFmmZppSr/RXU8k/OEWVprNcvdB44GI2Et5LIX2/IsttxSNySk1qR87DJ T2guraL4Yimu5qvBb83wDgTzhPLPIIpuaXRtv8kZelDInPc7Gs2Ay94CsS6soXZ1pUwq gh/CddvchsbvmenKq9BdRWyFresmnQQII9rULFjChuZy/HgwimCbDSRHNXCCwaXSqF3C sa3U2y0FdnRQOuKrfz5Ei5kTY9O3FDwHq2zOHu7YDKEyZG6NWWC22bprLaatvjlDSlE+ rW0g== X-Gm-Message-State: APjAAAUb5MSAnLAshaQjcPJxxUaJ59wpSNoIb1RKsj9fChPe9qHYbBWZ aHAEBJipJFCPrPLXr1k9xsCYjIfm4U4+jvEoP7FgOp4RzHBpNN79EVwgteMML0QVbTJnxXE35ah FprS6S3qQblv1Hsa7Eao65JiDx8SVPjSLzn6UF0XR4JHUMhbGybDj1A== X-Google-Smtp-Source: APXvYqxYen89Upp42c9iF0On/2yRREqj0vSMPc3cG7rMveKNbtUIg4iaBr/NMMrrM7zerR2q7GNZqa8= X-Received: by 2002:ac8:28bc:: with SMTP id i57mr40203937qti.288.1560360645423; Wed, 12 Jun 2019 10:30:45 -0700 (PDT) Date: Wed, 12 Jun 2019 10:30:38 -0700 In-Reply-To: <20190612173040.61944-1-sdf@google.com> Message-Id: <20190612173040.61944-2-sdf@google.com> Mime-Version: 1.0 References: <20190612173040.61944-1-sdf@google.com> X-Mailer: git-send-email 2.22.0.rc2.383.gf4fbbf30c2-goog Subject: [PATCH bpf-next 2/4] bpf: export bpf_sock for BPF_PROG_TYPE_SOCK_OPS prog type From: Stanislav Fomichev To: netdev@vger.kernel.org, bpf@vger.kernel.org Cc: davem@davemloft.net, ast@kernel.org, daniel@iogearbox.net, Stanislav Fomichev , Martin Lau Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org And let it use bpf_sk_storage_{get,delete} helpers to access socket storage. Kernel context (struct bpf_sock_ops_kern) already has sk member, so I just expose it to the BPF hooks. I use PTR_TO_SOCKET_OR_NULL and return NULL in !is_fullsock case. I also export bpf_tcp_sock to make it possible to access tcp socket stats. Cc: Martin Lau Signed-off-by: Stanislav Fomichev --- include/uapi/linux/bpf.h | 1 + net/core/filter.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 8815fc418cde..d0a23476f887 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -3299,6 +3299,7 @@ struct bpf_sock_ops { __u32 sk_txhash; __u64 bytes_received; __u64 bytes_acked; + __bpf_md_ptr(struct bpf_sock *, sk); }; /* Definitions for bpf_sock_ops_cb_flags */ diff --git a/net/core/filter.c b/net/core/filter.c index 37c4a2fd559b..8c18f2781afa 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -6147,6 +6147,14 @@ sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_get_local_storage_proto; case BPF_FUNC_perf_event_output: return &bpf_sockopt_event_output_proto; + case BPF_FUNC_sk_storage_get: + return &bpf_sk_storage_get_proto; + case BPF_FUNC_sk_storage_delete: + return &bpf_sk_storage_delete_proto; +#ifdef CONFIG_INET + case BPF_FUNC_tcp_sock: + return &bpf_tcp_sock_proto; +#endif /* CONFIG_INET */ default: return bpf_base_func_proto(func_id); } @@ -6882,6 +6890,11 @@ static bool sock_ops_is_valid_access(int off, int size, if (size != sizeof(__u64)) return false; break; + case offsetof(struct bpf_sock_ops, sk): + if (size != sizeof(__u64)) + return false; + info->reg_type = PTR_TO_SOCKET_OR_NULL; + break; default: if (size != size_default) return false; @@ -8053,6 +8066,19 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type, SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash, struct sock, type); break; + case offsetof(struct bpf_sock_ops, sk): + *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( + struct bpf_sock_ops_kern, + is_fullsock), + si->dst_reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern, + is_fullsock)); + *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); + *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( + struct bpf_sock_ops_kern, sk), + si->dst_reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern, sk)); + break; } return insn - insn_buf; } From patchwork Wed Jun 12 17:30:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1114727 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=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; 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.b="h5ro5SJS"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45PDRY1CXYz9s6w for ; Thu, 13 Jun 2019 03:30:49 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2409549AbfFLRas (ORCPT ); Wed, 12 Jun 2019 13:30:48 -0400 Received: from mail-pg1-f201.google.com ([209.85.215.201]:54703 "EHLO mail-pg1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2409542AbfFLRas (ORCPT ); Wed, 12 Jun 2019 13:30:48 -0400 Received: by mail-pg1-f201.google.com with SMTP id c4so11782942pgm.21 for ; Wed, 12 Jun 2019 10:30:48 -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=4JPge5G2892WNESlZ4eKnrVnH6/giL39WrMPoTXaExc=; b=h5ro5SJSnYzly27OdyE30z6LzTma1LfkgfmjQgh0ex07SM0R4CD8A54tdyKC+Xi+ej qM6TgeX24RNNPtcXMnryV6SNqePdy6DqDJCAWv1jlKhlZq0tCHQmlIqDKDyO5l2HX8zl rTMoFZtpxRzF2alb51rReatA6TefokA51mk2dEPkRY+BfUu9PedZ9oXJSTkn/hIhTJAa vnYaEuGd/hyVdI5z0hsLPb+nV/sUX0c3rZ7emIA++iDd62Y94+lgZRlYDTq/O3zAhJLY 6HXs4wQfFiOCLBkLLSWOBzCpo52eawcLd8I9H1VmcipR4XTOUkFrevWSewhdyCiK7VEk uvtA== 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=4JPge5G2892WNESlZ4eKnrVnH6/giL39WrMPoTXaExc=; b=CxkVFanzQvciYsazz7LTA+jNRBXzOHckY1ZJMdA3A4YLialRw3ewfIHx6N3xDRY0w3 kv9dnlT5tT7dqYfBAoTDUDlSBfTf13C2uCmVq/6v2klcr8Mwn6MqNU6p3fygG0kYdxbD PM7ss2Xxa+vsBgegq3v+T+zBzNJX1NI/0UvxDTH5eQy/5o0D/ECPEYLu27FWesIZYdQu KizXeH+E/WGVbSRqzOAcn/Jx+Y6f0jr70pho3OoxtSxacgRtpc66pQVUEh/1MtmbLzFv s9AOUuYU3cwmRB3Q9C4qOw355KuxpRjGmezAx64qgSy8JH+UsQQikpFA9+DjC1ggXMhs MOfg== X-Gm-Message-State: APjAAAUAOkS3dsXeOdv5alRKqKX7hmidZe9tGyD2sDJtL4SX8nhuVv9y ZanaDPwsUgFzAGgfu9FPG0sbv3A= X-Google-Smtp-Source: APXvYqyANjMixR1tbGgJel7+CTGj1C+KeoiYGS1TEl5Ypy6xE9mZe2jqZyRjazQiFsS4LGrx0W3wktU= X-Received: by 2002:a63:2c50:: with SMTP id s77mr25809188pgs.175.1560360647932; Wed, 12 Jun 2019 10:30:47 -0700 (PDT) Date: Wed, 12 Jun 2019 10:30:39 -0700 In-Reply-To: <20190612173040.61944-1-sdf@google.com> Message-Id: <20190612173040.61944-3-sdf@google.com> Mime-Version: 1.0 References: <20190612173040.61944-1-sdf@google.com> X-Mailer: git-send-email 2.22.0.rc2.383.gf4fbbf30c2-goog Subject: [PATCH bpf-next 3/4] bpf/tools: sync bpf.h From: Stanislav Fomichev To: netdev@vger.kernel.org, bpf@vger.kernel.org Cc: davem@davemloft.net, ast@kernel.org, daniel@iogearbox.net, Stanislav Fomichev , Martin Lau Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Add sk to struct bpf_sock_addr and struct bpf_sock_ops. Cc: Martin Lau Signed-off-by: Stanislav Fomichev --- tools/include/uapi/linux/bpf.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index ae0907d8c03a..d0a23476f887 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -3247,6 +3247,7 @@ struct bpf_sock_addr { __u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write. * Stored in network byte order. */ + __bpf_md_ptr(struct bpf_sock *, sk); }; /* User bpf_sock_ops struct to access socket values and specify request ops @@ -3298,6 +3299,7 @@ struct bpf_sock_ops { __u32 sk_txhash; __u64 bytes_received; __u64 bytes_acked; + __bpf_md_ptr(struct bpf_sock *, sk); }; /* Definitions for bpf_sock_ops_cb_flags */ From patchwork Wed Jun 12 17:30:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1114729 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=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; 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.b="JL37K2LY"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45PDRd0twxz9s6w for ; Thu, 13 Jun 2019 03:30:53 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2409548AbfFLRaw (ORCPT ); Wed, 12 Jun 2019 13:30:52 -0400 Received: from mail-vs1-f73.google.com ([209.85.217.73]:54868 "EHLO mail-vs1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2409554AbfFLRaw (ORCPT ); Wed, 12 Jun 2019 13:30:52 -0400 Received: by mail-vs1-f73.google.com with SMTP id 184so5614613vsm.21 for ; Wed, 12 Jun 2019 10:30:51 -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=lnSzeYdpJQCfSwR2gxAs8Ypau7xTKghIzw3qzvylwKU=; b=JL37K2LYpYVAau+IhV93rXXut1llW7IBWsF2sKDKVL3Knv0SH0aXSEThpMnqJ8A6G2 Gk6ggks3Te8uW27ppY++FLtHCBO5dL+ujgVZVY5asHltHtqLzi+8khdmFLD2wVBpJyc3 MOJXSgpAnNGgu1MifJYGxqjYrSK59CcN8UuvRzTsWpXSeuSfyi4bR7qY3qXuZ7FVco3Q 80W6u5ki0cgbuUuSx9v4WoBbH2cPCHFlGbwws2H0LBuU2EUp3rUKhgOCmkSi+Y7RLF8u +IAUbIQxkh//xNjl9EjPsLbXSj6VkKhdR9uDjwMMp6iBfcZz2oztBAF0Zkd5bpSRJjUp /zWg== 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=lnSzeYdpJQCfSwR2gxAs8Ypau7xTKghIzw3qzvylwKU=; b=nNWgyfylJeTvEVjwf/BsPV1+ljG3zbpbkaZvAyiArXQv5IeBnX6XoBvWc/aC8/uIbN 4ydo5SXUEXSCNkBvb9ddPcSlxyqPy3J+JxglYejXwNWZNmSYceXZTWteUHG3DSEs3tDT tpxddh6u2YijBBSf0VujkZOYGd1dSirsXc4g+XAT66+1NwxSzDZSVQr1q9pryLRzBZcH vyuhBRp6Gfdg6Rfz8wvAHLJNdqrFQVC37ILnLJZ6SdRIz9FA5TsFKMizZZx0/loOc071 bqEfZJZfS7QKGyEsMri0N8fMm76ROhmiIzGu5LjAYuOc2W7qYmQvLQvG8LO4HLHZfprg tAJw== X-Gm-Message-State: APjAAAUu6u/yRbYNfOrg5I13FCK/mwLZsojIdm/UhHdOqUhsLiMuIlgC 8OF/8awni8oL6rroAfjZoKDwj4Y= X-Google-Smtp-Source: APXvYqwwmiWbcPNvQIYtoHc53dcQgUx+dxnQYMKtt0dIs+NqjdAIV/ON7uciV+HNbT4iUwxlPAVg+7c= X-Received: by 2002:a1f:8744:: with SMTP id j65mr32844970vkd.17.1560360650604; Wed, 12 Jun 2019 10:30:50 -0700 (PDT) Date: Wed, 12 Jun 2019 10:30:40 -0700 In-Reply-To: <20190612173040.61944-1-sdf@google.com> Message-Id: <20190612173040.61944-4-sdf@google.com> Mime-Version: 1.0 References: <20190612173040.61944-1-sdf@google.com> X-Mailer: git-send-email 2.22.0.rc2.383.gf4fbbf30c2-goog Subject: [PATCH bpf-next 4/4] selftests/bpf: convert socket_cookie test to sk storage From: Stanislav Fomichev To: netdev@vger.kernel.org, bpf@vger.kernel.org Cc: davem@davemloft.net, ast@kernel.org, daniel@iogearbox.net, Stanislav Fomichev , Martin Lau Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org This lets us test that both BPF_PROG_TYPE_CGROUP_SOCK_ADDR and BPF_PROG_TYPE_SOCK_OPS can access underlying bpf_sock. Cc: Martin Lau Signed-off-by: Stanislav Fomichev --- .../selftests/bpf/progs/socket_cookie_prog.c | 46 ++++++++++++------- .../selftests/bpf/test_socket_cookie.c | 24 ++++------ 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/tools/testing/selftests/bpf/progs/socket_cookie_prog.c b/tools/testing/selftests/bpf/progs/socket_cookie_prog.c index 9ff8ac4b0bf6..0db15c3210ad 100644 --- a/tools/testing/selftests/bpf/progs/socket_cookie_prog.c +++ b/tools/testing/selftests/bpf/progs/socket_cookie_prog.c @@ -7,25 +7,35 @@ #include "bpf_helpers.h" #include "bpf_endian.h" +struct socket_cookie { + __u64 cookie_key; + __u32 cookie_value; +}; + struct bpf_map_def SEC("maps") socket_cookies = { - .type = BPF_MAP_TYPE_HASH, - .key_size = sizeof(__u64), - .value_size = sizeof(__u32), - .max_entries = 1 << 8, + .type = BPF_MAP_TYPE_SK_STORAGE, + .key_size = sizeof(int), + .value_size = sizeof(struct socket_cookie), + .map_flags = BPF_F_NO_PREALLOC, }; +BPF_ANNOTATE_KV_PAIR(socket_cookies, int, struct socket_cookie); + SEC("cgroup/connect6") int set_cookie(struct bpf_sock_addr *ctx) { - __u32 cookie_value = 0xFF; - __u64 cookie_key; + struct socket_cookie *p; if (ctx->family != AF_INET6 || ctx->user_family != AF_INET6) return 1; - cookie_key = bpf_get_socket_cookie(ctx); - if (bpf_map_update_elem(&socket_cookies, &cookie_key, &cookie_value, 0)) - return 0; + p = bpf_sk_storage_get(&socket_cookies, ctx->sk, 0, + BPF_SK_STORAGE_GET_F_CREATE); + if (!p) + return 1; + + p->cookie_value = 0xFF; + p->cookie_key = bpf_get_socket_cookie(ctx); return 1; } @@ -33,9 +43,8 @@ int set_cookie(struct bpf_sock_addr *ctx) SEC("sockops") int update_cookie(struct bpf_sock_ops *ctx) { - __u32 new_cookie_value; - __u32 *cookie_value; - __u64 cookie_key; + struct bpf_sock *sk; + struct socket_cookie *p; if (ctx->family != AF_INET6) return 1; @@ -43,14 +52,17 @@ int update_cookie(struct bpf_sock_ops *ctx) if (ctx->op != BPF_SOCK_OPS_TCP_CONNECT_CB) return 1; - cookie_key = bpf_get_socket_cookie(ctx); + if (!ctx->sk) + return 1; + + p = bpf_sk_storage_get(&socket_cookies, ctx->sk, 0, 0); + if (!p) + return 1; - cookie_value = bpf_map_lookup_elem(&socket_cookies, &cookie_key); - if (!cookie_value) + if (p->cookie_key != bpf_get_socket_cookie(ctx)) return 1; - new_cookie_value = (ctx->local_port << 8) | *cookie_value; - bpf_map_update_elem(&socket_cookies, &cookie_key, &new_cookie_value, 0); + p->cookie_value = (ctx->local_port << 8) | p->cookie_value; return 1; } diff --git a/tools/testing/selftests/bpf/test_socket_cookie.c b/tools/testing/selftests/bpf/test_socket_cookie.c index cac8ee57a013..15653b0e26eb 100644 --- a/tools/testing/selftests/bpf/test_socket_cookie.c +++ b/tools/testing/selftests/bpf/test_socket_cookie.c @@ -18,6 +18,11 @@ #define CG_PATH "/foo" #define SOCKET_COOKIE_PROG "./socket_cookie_prog.o" +struct socket_cookie { + __u64 cookie_key; + __u32 cookie_value; +}; + static int start_server(void) { struct sockaddr_in6 addr; @@ -89,8 +94,7 @@ static int validate_map(struct bpf_map *map, int client_fd) __u32 cookie_expected_value; struct sockaddr_in6 addr; socklen_t len = sizeof(addr); - __u32 cookie_value; - __u64 cookie_key; + struct socket_cookie val; int err = 0; int map_fd; @@ -101,17 +105,7 @@ static int validate_map(struct bpf_map *map, int client_fd) map_fd = bpf_map__fd(map); - err = bpf_map_get_next_key(map_fd, NULL, &cookie_key); - if (err) { - log_err("Can't get cookie key from map"); - goto out; - } - - err = bpf_map_lookup_elem(map_fd, &cookie_key, &cookie_value); - if (err) { - log_err("Can't get cookie value from map"); - goto out; - } + err = bpf_map_lookup_elem(map_fd, &client_fd, &val); err = getsockname(client_fd, (struct sockaddr *)&addr, &len); if (err) { @@ -120,8 +114,8 @@ static int validate_map(struct bpf_map *map, int client_fd) } cookie_expected_value = (ntohs(addr.sin6_port) << 8) | 0xFF; - if (cookie_value != cookie_expected_value) { - log_err("Unexpected value in map: %x != %x", cookie_value, + if (val.cookie_value != cookie_expected_value) { + log_err("Unexpected value in map: %x != %x", val.cookie_value, cookie_expected_value); goto err; }