From patchwork Sun Oct 15 14:16:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 734831 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ADEC8CDB47E for ; Sun, 15 Oct 2023 14:17:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229561AbjJOORX (ORCPT ); Sun, 15 Oct 2023 10:17:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230010AbjJOORW (ORCPT ); Sun, 15 Oct 2023 10:17:22 -0400 Received: from mail-oi1-x232.google.com (mail-oi1-x232.google.com [IPv6:2607:f8b0:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD819D6 for ; Sun, 15 Oct 2023 07:17:19 -0700 (PDT) Received: by mail-oi1-x232.google.com with SMTP id 5614622812f47-3b2b1af964dso1674428b6e.1 for ; Sun, 15 Oct 2023 07:17:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1697379439; x=1697984239; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vJDsjXY27F9/4dBjdxEGWiwFVrUuGxBuoNe9PuwHdgE=; b=LVT7RCZPtopGXV//N4v4QN95y36OxVqgNowFvlze0sLrLO6AY+UvV9ebmW/jbHqqz5 syaPrA/jTPvstEgSyV7MUrKLU5cc8gvdKqrpSs+FZqg0+F08lBoecmRKgjy3SGj6z4Df c1gK+WN1vot1KINHaRYaIIPfCDJG5xfoNI6KC/8BSWZJ9cekPIS0RRk4lJGIQO1hSweq fIT8uUk5BJgOpRIqBOX+2UaVvj4kc1NDz/SC6ScCnWsT/dLO2ENyI/uuVypmQTOL5XNz wIuPFy27DGo+1w5a45UyzNi3LtvB1x6w005GeF5ZEtqC02b7axpYyCnZ8DElySmOMhZy ggYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697379439; x=1697984239; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vJDsjXY27F9/4dBjdxEGWiwFVrUuGxBuoNe9PuwHdgE=; b=p0Pcy9pmbNgAcbYI7IUarKrs4O3gYXKAiuiYbaPI5CQr2kAa3daP83c3BD8NnKn5oQ df+z2UCAyadeiMR2k3T4GDWr13t5O6tPuQtjgmNMqA9FU1LnWxrc7gKGiV23fGygg8kq BtNHVFSrLL6kLnd2Phe6DKix3WPJpVqT4b2jX1nk7Afg8ntkPOtW6tL3Ny1MrymU8O1z pTSxinXkK8LdzIk0nB5CS+9kkw5hZzhrLzJQ9EM75NWClolxy/PI/Z2kmTX6yxZqmtN8 JiK1PX6RlMgt7t/wOMN6CQqZzDcQ2ADxsqdetFrfR2/WahEAmjp+3YnR35c6kz8EjZIH I1bw== X-Gm-Message-State: AOJu0YzkTy/QaXG8VPHAolJ/+TDjTxrIlktqlHF1wnNs/cVUC8Wwa7Pj pyBl0WHPH9rTAhd83nRAx+GP4A== X-Google-Smtp-Source: AGHT+IGpU5uszWRjUzIjT7iHtowuf/PL2ks/58t0JISif6Khaqfb5QST4TqfG3bPtfy8oiUe2eIlIQ== X-Received: by 2002:a05:6808:210b:b0:3a7:2456:6af6 with SMTP id r11-20020a056808210b00b003a724566af6mr39723966oiw.31.1697379439071; Sun, 15 Oct 2023 07:17:19 -0700 (PDT) Received: from localhost ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with UTF8SMTPSA id c14-20020a63724e000000b005897bfc2ed3sm6234231pgn.93.2023.10.15.07.17.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 15 Oct 2023 07:17:18 -0700 (PDT) From: Akihiko Odaki Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jonathan Corbet , Willem de Bruijn , Jason Wang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Michael S. Tsirkin" , Xuan Zhuo , Mykola Lysenko , Shuah Khan , bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kselftest@vger.kernel.org, Yuri Benditovich , Andrew Melnychenko , Akihiko Odaki Subject: [RFC PATCH v2 1/7] bpf: Introduce BPF_PROG_TYPE_VNET_HASH Date: Sun, 15 Oct 2023 23:16:29 +0900 Message-ID: <20231015141644.260646-2-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231015141644.260646-1-akihiko.odaki@daynix.com> References: <20231015141644.260646-1-akihiko.odaki@daynix.com> MIME-Version: 1.0 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This new program type will be used by tun to determine the queues to deliver packets and the hash values and types reported with virtio-net headers. Signed-off-by: Akihiko Odaki --- Documentation/bpf/bpf_prog_run.rst | 1 + Documentation/bpf/libbpf/program_types.rst | 2 ++ include/linux/bpf_types.h | 2 ++ include/uapi/linux/bpf.h | 5 +++++ kernel/bpf/verifier.c | 6 ++++++ net/core/filter.c | 11 +++++++++++ tools/include/uapi/linux/bpf.h | 1 + tools/lib/bpf/libbpf.c | 2 ++ 8 files changed, 30 insertions(+) diff --git a/Documentation/bpf/bpf_prog_run.rst b/Documentation/bpf/bpf_prog_run.rst index 4868c909df5c..0d108d867c03 100644 --- a/Documentation/bpf/bpf_prog_run.rst +++ b/Documentation/bpf/bpf_prog_run.rst @@ -39,6 +39,7 @@ following types: - ``BPF_PROG_TYPE_STRUCT_OPS`` - ``BPF_PROG_TYPE_RAW_TRACEPOINT`` - ``BPF_PROG_TYPE_SYSCALL`` +- ``BPF_PROG_TYPE_VNET_HASH`` When using the ``BPF_PROG_RUN`` command, userspace supplies an input context object and (for program types operating on network packets) a buffer containing diff --git a/Documentation/bpf/libbpf/program_types.rst b/Documentation/bpf/libbpf/program_types.rst index ad4d4d5eecb0..6be53201f91b 100644 --- a/Documentation/bpf/libbpf/program_types.rst +++ b/Documentation/bpf/libbpf/program_types.rst @@ -171,6 +171,8 @@ described in more detail in the footnotes. + +----------------------------------------+----------------------------------+-----------+ | | ``BPF_TRACE_RAW_TP`` | ``tp_btf+`` [#fentry]_ | | +-------------------------------------------+----------------------------------------+----------------------------------+-----------+ +| ``BPF_PROG_TYPE_VNET_HASH`` | | ``vnet_hash`` | | ++-------------------------------------------+----------------------------------------+----------------------------------+-----------+ | ``BPF_PROG_TYPE_XDP`` | ``BPF_XDP_CPUMAP`` | ``xdp.frags/cpumap`` | | + + +----------------------------------+-----------+ | | | ``xdp/cpumap`` | | diff --git a/include/linux/bpf_types.h b/include/linux/bpf_types.h index fc0d6f32c687..dec83d495e82 100644 --- a/include/linux/bpf_types.h +++ b/include/linux/bpf_types.h @@ -34,6 +34,8 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_SK_MSG, sk_msg, struct sk_msg_md, struct sk_msg) BPF_PROG_TYPE(BPF_PROG_TYPE_FLOW_DISSECTOR, flow_dissector, struct __sk_buff, struct bpf_flow_dissector) +BPF_PROG_TYPE(BPF_PROG_TYPE_VNET_HASH, vnet_hash, + struct __sk_buff, struct sk_buff) #endif #ifdef CONFIG_BPF_EVENTS BPF_PROG_TYPE(BPF_PROG_TYPE_KPROBE, kprobe, diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 0448700890f7..298634556fab 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -988,6 +988,7 @@ enum bpf_prog_type { BPF_PROG_TYPE_SK_LOOKUP, BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */ BPF_PROG_TYPE_NETFILTER, + BPF_PROG_TYPE_VNET_HASH, }; enum bpf_attach_type { @@ -6111,6 +6112,10 @@ struct __sk_buff { __u8 tstamp_type; __u32 :24; /* Padding, future use. */ __u64 hwtstamp; + + __u32 vnet_hash_value; + __u16 vnet_hash_report; + __u16 vnet_rss_queue; }; struct bpf_tunnel_key { diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index bb78212fa5b2..fd6d842635d2 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -14373,6 +14373,7 @@ static bool may_access_skb(enum bpf_prog_type type) case BPF_PROG_TYPE_SOCKET_FILTER: case BPF_PROG_TYPE_SCHED_CLS: case BPF_PROG_TYPE_SCHED_ACT: + case BPF_PROG_TYPE_VNET_HASH: return true; default: return false; @@ -16973,6 +16974,11 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env, return -EINVAL; } + if (prog_type == BPF_PROG_TYPE_VNET_HASH) { + verbose(env, "vnet hash progs cannot use bpf_spin_lock yet\n"); + return -EINVAL; + } + if (is_tracing_prog_type(prog_type)) { verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); return -EINVAL; diff --git a/net/core/filter.c b/net/core/filter.c index a094694899c9..867edbc628de 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -10967,6 +10967,17 @@ const struct bpf_prog_ops flow_dissector_prog_ops = { .test_run = bpf_prog_test_run_flow_dissector, }; +const struct bpf_verifier_ops vnet_hash_verifier_ops = { + .get_func_proto = sk_filter_func_proto, + .is_valid_access = sk_filter_is_valid_access, + .convert_ctx_access = bpf_convert_ctx_access, + .gen_ld_abs = bpf_gen_ld_abs, +}; + +const struct bpf_prog_ops vnet_hash_prog_ops = { + .test_run = bpf_prog_test_run_skb, +}; + int sk_detach_filter(struct sock *sk) { int ret = -ENOENT; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 0448700890f7..60976fe86247 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -988,6 +988,7 @@ enum bpf_prog_type { BPF_PROG_TYPE_SK_LOOKUP, BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */ BPF_PROG_TYPE_NETFILTER, + BPF_PROG_TYPE_VNET_HASH, }; enum bpf_attach_type { diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 96ff1aa4bf6a..e74d136eae07 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -209,6 +209,7 @@ static const char * const prog_type_name[] = { [BPF_PROG_TYPE_SK_LOOKUP] = "sk_lookup", [BPF_PROG_TYPE_SYSCALL] = "syscall", [BPF_PROG_TYPE_NETFILTER] = "netfilter", + [BPF_PROG_TYPE_VNET_HASH] = "vnet_hash", }; static int __base_pr(enum libbpf_print_level level, const char *format, @@ -8858,6 +8859,7 @@ static const struct bpf_sec_def section_defs[] = { SEC_DEF("struct_ops.s+", STRUCT_OPS, 0, SEC_SLEEPABLE), SEC_DEF("sk_lookup", SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE), SEC_DEF("netfilter", NETFILTER, BPF_NETFILTER, SEC_NONE), + SEC_DEF("vnet_hash", VNET_HASH, 0, SEC_NONE), }; int libbpf_register_prog_handler(const char *sec, From patchwork Sun Oct 15 14:16:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 733964 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91CFACDB47E for ; Sun, 15 Oct 2023 14:17:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230089AbjJOORc (ORCPT ); Sun, 15 Oct 2023 10:17:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230127AbjJOORa (ORCPT ); Sun, 15 Oct 2023 10:17:30 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0226F2 for ; Sun, 15 Oct 2023 07:17:27 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-27d1aee5aa1so2627136a91.0 for ; Sun, 15 Oct 2023 07:17:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1697379447; x=1697984247; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jQwvcEuaD8qtq8qMfxzrYYiBWg40WGS1Tav8Ug6Qb8c=; b=ORNweTq+hN8tIEGYz2Vh0NJm1ki9R7JBN3Cu7ygENFKSagrHe+sAFwymK43LlQYa4O pLfz9slfiNG+0RRNCLeHSjE+nyvEweNcGW3N8QA8RS+QCC2EJBSl89uH74wr50EZjDBQ qMH/MRuL2GKqqeqq05HhbaVjeHaIApagYTdWuktKVf8XUonk8y1u/5Fi/N7r2Paq9Tvv fWhH4IsQgBXGx2w2rhz85AFWC8auPi6xNe0j1xyobcWyhLfpAek8oLyYRD3sVYOCyMy9 DKBaKw+P5myUp+r6VCr+hSQ4skR9tM0S71bbnuzW2GeH8chz4EMyEJ1Q13HJ4LiZx35F 5HOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697379447; x=1697984247; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jQwvcEuaD8qtq8qMfxzrYYiBWg40WGS1Tav8Ug6Qb8c=; b=aZ1dWEnoh1DymlGINPsPfe+utaaSRpi9jiWeIUaeuoaUEO9EjhASW7uOCxrAkRCbA9 OZg/8UY5jiSoDRQzUGnCjHSXnJj/ARWaBa5XXVHnYdyzbjGI6ebdBH39fdJxeUcewI5Q Ox1ZyEU33FkRx+O+NQLzXOUKsQjV0KMIwsFHjrWr2nJkvix8sypX3sLGmhD8CSi+xJBI m62mmwVc4Thze1pxwMoHpwNjruz89qSGNsIaFIGSvOFMsW6Fx85Wpvdk2KPwWPgucbBl FslUUd2m1a0E6JAl8qn6wMkAKRzFmF8VTJ5qhHrLxJSF0EnV/sc0CZCIVEtxE5twEUd0 tRJA== X-Gm-Message-State: AOJu0YxtGxdhCu48YrqjiAuoqA1+E00gTBtPNqDP4vKz5bcRM6NNhokt qtN512Gk2RWvCfQWH7o7gFEMSQ== X-Google-Smtp-Source: AGHT+IHrFfjiox44OQRQFnZKdtFPkv5leUtQZcmEzH7FMdsenMLqiF2Tonpvh4fiH6sOm3mXPp3izQ== X-Received: by 2002:a17:90b:124f:b0:27d:886:e2d2 with SMTP id gx15-20020a17090b124f00b0027d0886e2d2mr7855719pjb.7.1697379447008; Sun, 15 Oct 2023 07:17:27 -0700 (PDT) Received: from localhost ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with UTF8SMTPSA id k3-20020a17090a910300b0027722832498sm2987862pjo.52.2023.10.15.07.17.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 15 Oct 2023 07:17:26 -0700 (PDT) From: Akihiko Odaki Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jonathan Corbet , Willem de Bruijn , Jason Wang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Michael S. Tsirkin" , Xuan Zhuo , Mykola Lysenko , Shuah Khan , bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kselftest@vger.kernel.org, Yuri Benditovich , Andrew Melnychenko , Akihiko Odaki Subject: [RFC PATCH v2 2/7] bpf: Add vnet_hash members to __sk_buff Date: Sun, 15 Oct 2023 23:16:30 +0900 Message-ID: <20231015141644.260646-3-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231015141644.260646-1-akihiko.odaki@daynix.com> References: <20231015141644.260646-1-akihiko.odaki@daynix.com> MIME-Version: 1.0 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org They will be used only by BPF_PROG_TYPE_VNET_HASH to tell the queues to deliver packets and the hash values and types reported with virtio-net headers. Signed-off-by: Akihiko Odaki --- include/linux/filter.h | 7 ++++ net/core/filter.c | 77 +++++++++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 4 ++ 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/include/linux/filter.h b/include/linux/filter.h index bf7ad887943c..d10afe92ee45 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -643,6 +643,13 @@ struct bpf_skb_data_end { void *data_end; }; +struct bpf_skb_vnet_hash_end { + struct qdisc_skb_cb qdisc_cb; + u32 hash_value; + u16 hash_report; + u16 rss_queue; +}; + struct bpf_nh_params { u32 nh_family; union { diff --git a/net/core/filter.c b/net/core/filter.c index 867edbc628de..35bc60b71722 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -8435,9 +8435,15 @@ static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type case bpf_ctx_range(struct __sk_buff, data): case bpf_ctx_range(struct __sk_buff, data_meta): case bpf_ctx_range(struct __sk_buff, data_end): + case bpf_ctx_range(struct __sk_buff, vnet_hash_value): if (size != size_default) return false; break; + case bpf_ctx_range(struct __sk_buff, vnet_hash_report): + case bpf_ctx_range(struct __sk_buff, vnet_rss_queue): + if (size != sizeof(__u16)) + return false; + break; case bpf_ctx_range_ptr(struct __sk_buff, flow_keys): return false; case bpf_ctx_range(struct __sk_buff, hwtstamp): @@ -8473,7 +8479,7 @@ static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type return true; } -static bool sk_filter_is_valid_access(int off, int size, +static bool vnet_hash_is_valid_access(int off, int size, enum bpf_access_type type, const struct bpf_prog *prog, struct bpf_insn_access_aux *info) @@ -8493,6 +8499,9 @@ static bool sk_filter_is_valid_access(int off, int size, if (type == BPF_WRITE) { switch (off) { case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): + case bpf_ctx_range(struct __sk_buff, vnet_hash_value): + case bpf_ctx_range(struct __sk_buff, vnet_hash_report): + case bpf_ctx_range(struct __sk_buff, vnet_rss_queue): break; default: return false; @@ -8502,6 +8511,21 @@ static bool sk_filter_is_valid_access(int off, int size, return bpf_skb_is_valid_access(off, size, type, prog, info); } +static bool sk_filter_is_valid_access(int off, int size, + enum bpf_access_type type, + const struct bpf_prog *prog, + struct bpf_insn_access_aux *info) +{ + switch (off) { + case bpf_ctx_range(struct __sk_buff, vnet_hash_value): + case bpf_ctx_range(struct __sk_buff, vnet_hash_report): + case bpf_ctx_range(struct __sk_buff, vnet_rss_queue): + return false; + } + + return vnet_hash_is_valid_access(off, size, type, prog, info); +} + static bool cg_skb_is_valid_access(int off, int size, enum bpf_access_type type, const struct bpf_prog *prog, @@ -8511,6 +8535,9 @@ static bool cg_skb_is_valid_access(int off, int size, case bpf_ctx_range(struct __sk_buff, tc_classid): case bpf_ctx_range(struct __sk_buff, data_meta): case bpf_ctx_range(struct __sk_buff, wire_len): + case bpf_ctx_range(struct __sk_buff, vnet_hash_value): + case bpf_ctx_range(struct __sk_buff, vnet_hash_report): + case bpf_ctx_range(struct __sk_buff, vnet_rss_queue): return false; case bpf_ctx_range(struct __sk_buff, data): case bpf_ctx_range(struct __sk_buff, data_end): @@ -8558,6 +8585,9 @@ static bool lwt_is_valid_access(int off, int size, case bpf_ctx_range(struct __sk_buff, tstamp): case bpf_ctx_range(struct __sk_buff, wire_len): case bpf_ctx_range(struct __sk_buff, hwtstamp): + case bpf_ctx_range(struct __sk_buff, vnet_hash_value): + case bpf_ctx_range(struct __sk_buff, vnet_hash_report): + case bpf_ctx_range(struct __sk_buff, vnet_rss_queue): return false; } @@ -8799,6 +8829,10 @@ static bool tc_cls_act_is_valid_access(int off, int size, } switch (off) { + case bpf_ctx_range(struct __sk_buff, vnet_hash_value): + case bpf_ctx_range(struct __sk_buff, vnet_hash_report): + case bpf_ctx_range(struct __sk_buff, vnet_rss_queue): + return false; case bpf_ctx_range(struct __sk_buff, data): info->reg_type = PTR_TO_PACKET; break; @@ -9117,6 +9151,9 @@ static bool sk_skb_is_valid_access(int off, int size, case bpf_ctx_range(struct __sk_buff, tstamp): case bpf_ctx_range(struct __sk_buff, wire_len): case bpf_ctx_range(struct __sk_buff, hwtstamp): + case bpf_ctx_range(struct __sk_buff, vnet_hash_value): + case bpf_ctx_range(struct __sk_buff, vnet_hash_report): + case bpf_ctx_range(struct __sk_buff, vnet_rss_queue): return false; } @@ -9727,6 +9764,42 @@ static u32 bpf_convert_ctx_access(enum bpf_access_type type, hwtstamps, 8, target_size)); break; + + case offsetof(struct __sk_buff, vnet_hash_value): + BUILD_BUG_ON(sizeof_field(struct bpf_skb_vnet_hash_end, hash_value) != 4); + + off = offsetof(struct sk_buff, cb) + + offsetof(struct bpf_skb_vnet_hash_end, hash_value); + + if (type == BPF_WRITE) + *insn++ = BPF_EMIT_STORE(BPF_W, si, off); + else + *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off); + break; + + case offsetof(struct __sk_buff, vnet_hash_report): + BUILD_BUG_ON(sizeof_field(struct bpf_skb_vnet_hash_end, hash_report) != 2); + + off = offsetof(struct sk_buff, cb) + + offsetof(struct bpf_skb_vnet_hash_end, hash_report); + + if (type == BPF_WRITE) + *insn++ = BPF_EMIT_STORE(BPF_H, si, off); + else + *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, off); + break; + + case offsetof(struct __sk_buff, vnet_rss_queue): + BUILD_BUG_ON(sizeof_field(struct bpf_skb_vnet_hash_end, rss_queue) != 2); + + off = offsetof(struct sk_buff, cb) + + offsetof(struct bpf_skb_vnet_hash_end, rss_queue); + + if (type == BPF_WRITE) + *insn++ = BPF_EMIT_STORE(BPF_H, si, off); + else + *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, off); + break; } return insn - insn_buf; @@ -10969,7 +11042,7 @@ const struct bpf_prog_ops flow_dissector_prog_ops = { const struct bpf_verifier_ops vnet_hash_verifier_ops = { .get_func_proto = sk_filter_func_proto, - .is_valid_access = sk_filter_is_valid_access, + .is_valid_access = vnet_hash_is_valid_access, .convert_ctx_access = bpf_convert_ctx_access, .gen_ld_abs = bpf_gen_ld_abs, }; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 60976fe86247..298634556fab 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6112,6 +6112,10 @@ struct __sk_buff { __u8 tstamp_type; __u32 :24; /* Padding, future use. */ __u64 hwtstamp; + + __u32 vnet_hash_value; + __u16 vnet_hash_report; + __u16 vnet_rss_queue; }; struct bpf_tunnel_key { From patchwork Sun Oct 15 14:16:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 734830 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D87C1CDB47E for ; Sun, 15 Oct 2023 14:17:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230188AbjJOORm (ORCPT ); Sun, 15 Oct 2023 10:17:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230168AbjJOORi (ORCPT ); Sun, 15 Oct 2023 10:17:38 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D475FB for ; Sun, 15 Oct 2023 07:17:35 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1c9b95943beso31283205ad.1 for ; Sun, 15 Oct 2023 07:17:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1697379454; x=1697984254; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=q3e16X3BnACnel0/w/bQT7bK9RdqePAG/1Ajmk33nB0=; b=jOjNclAylNWhZ4hB+LFckWl9nZjeAdYIUYBxlpMMjRuXSf70wdqUDw1m3PBBelChkk nLQqE4n8aa2ZoFfZ0BjparIqreNrV64Ff8ga//0p9P24HdmueiH3bYlOZh5ZRUTxmr2Q Bsj5n/KP4Bxq9SQMLWBtg4QAkrprtUZBMOMUtzk1wBV8jdslEszB6tigt/mgtHUr2vpJ JmIC6Jy+LcrAjl7OPP7dUxx0xGOGSWCJLnSZbdocit6jH4QD9KyHFU7VaAenZptOZxaF //tXuDZUaogqUgVHPqMaYTdCX1EvdVrD0nkZ8h+qUdchz4L8I16yD4/NSCJ8//WBPnw2 OPmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697379454; x=1697984254; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=q3e16X3BnACnel0/w/bQT7bK9RdqePAG/1Ajmk33nB0=; b=YxWKBcmgTQ4VLFeED4UbdhoGpM69hlnSm11W4MGZfnhyV4T8/cSJ3Eqmya2pOTd63g p+FD2Yd4Yl35wsZHEzkKW5JHKa6Zmt3QjGN8h/cFJpOLAgujqQNWYQJ1mxF3Tu0VKJSf yNeh7IYqOiSK6iL9ip+vaoWYMrSlnRVawUqesJNnZfhGKAnZgHZp/EgL9IudP4B5bzFb dZGzTuuvpMBnSMmsPqRQ/SYIxR9+XONEVR9sXZL03zHRpug/rrySZjdlFKq7ZNMCtP8V ZjFfQuLjgqWHV2f1eb4sOHPU1X8m9CDnCMA9ezfH1DrOIBmFO3ppp/coyMGh2iVokd7H 4S7A== X-Gm-Message-State: AOJu0Ywjje3J1C93VxTZnNr6t1PFp10aBJanHM5b3Pckg5CNrf88AT16 wttNxJq7D8EkUIEsb+eikGHkjg== X-Google-Smtp-Source: AGHT+IGa+4527nTRMoq3BT4pWp6MGz5aJZtlZUNhOW5hIrk1Y1oqpKECZxuWNJqZ5/MeAEwk4c/hSg== X-Received: by 2002:a17:90b:4ccb:b0:27d:51c4:1679 with SMTP id nd11-20020a17090b4ccb00b0027d51c41679mr4375745pjb.27.1697379454522; Sun, 15 Oct 2023 07:17:34 -0700 (PDT) Received: from localhost ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with UTF8SMTPSA id ij6-20020a170902ab4600b001c0c79b386esm7058350plb.95.2023.10.15.07.17.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 15 Oct 2023 07:17:34 -0700 (PDT) From: Akihiko Odaki Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jonathan Corbet , Willem de Bruijn , Jason Wang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Michael S. Tsirkin" , Xuan Zhuo , Mykola Lysenko , Shuah Khan , bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kselftest@vger.kernel.org, Yuri Benditovich , Andrew Melnychenko , Akihiko Odaki Subject: [RFC PATCH v2 3/7] skbuff: Introduce SKB_EXT_TUN_VNET_HASH Date: Sun, 15 Oct 2023 23:16:31 +0900 Message-ID: <20231015141644.260646-4-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231015141644.260646-1-akihiko.odaki@daynix.com> References: <20231015141644.260646-1-akihiko.odaki@daynix.com> MIME-Version: 1.0 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This new extension will be used by tun to carry the hash values and types to report with virtio-net headers. Signed-off-by: Akihiko Odaki --- include/linux/skbuff.h | 10 ++++++++++ net/core/skbuff.c | 3 +++ 2 files changed, 13 insertions(+) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 4174c4b82d13..1f2e5d350810 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -333,6 +333,13 @@ struct tc_skb_ext { }; #endif +#if IS_ENABLED(CONFIG_TUN) +struct tun_vnet_hash { + u32 value; + u16 report; +}; +#endif + struct sk_buff_head { /* These two members must be first to match sk_buff. */ struct_group_tagged(sk_buff_list, list, @@ -4631,6 +4638,9 @@ enum skb_ext_id { #endif #if IS_ENABLED(CONFIG_MCTP_FLOWS) SKB_EXT_MCTP, +#endif +#if IS_ENABLED(CONFIG_TUN) + SKB_EXT_TUN_VNET_HASH, #endif SKB_EXT_NUM, /* must be last */ }; diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 4eaf7ed0d1f4..774c2b26bf25 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -4793,6 +4793,9 @@ static const u8 skb_ext_type_len[] = { #if IS_ENABLED(CONFIG_MCTP_FLOWS) [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow), #endif +#if IS_ENABLED(CONFIG_TUN) + [SKB_EXT_TUN_VNET_HASH] = SKB_EXT_CHUNKSIZEOF(struct tun_vnet_hash), +#endif }; static __always_inline unsigned int skb_ext_total_length(void) From patchwork Sun Oct 15 14:16:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 733963 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C667CDB47E for ; Sun, 15 Oct 2023 14:17:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230106AbjJOOR5 (ORCPT ); Sun, 15 Oct 2023 10:17:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230168AbjJOORx (ORCPT ); Sun, 15 Oct 2023 10:17:53 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC1FC136 for ; Sun, 15 Oct 2023 07:17:43 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1c9de3f66e5so21036285ad.3 for ; Sun, 15 Oct 2023 07:17:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1697379462; x=1697984262; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kvSWVbZXn/MrVEcGpo16B6JSHdwZ2fR7rly5J6YWZNE=; b=RFS2pRpOPj0Fz3zbI6kHMAvLIv9pOZrbCw4IRXWqTM7IE4gIwhuZ7cMWL1lnx2NzjC JmP2knUtSyyY1syJLyq+qzp3nZc1bh+nyiKBD+XFfVODB9AmKmpspLEWul9jaksfPYFr gBcy3eEG55wtAaa0jTvrgZKvGgbxI3eEu1jJTw5zD9zCWhHyN3HfaBCMAEqTPM9AyP+R 1C2HXXFQqdZ7eis759QCNODeaizm5UiraLBG3eSS/s6vVAZlmCMFmyTShNOh4Yb59fRT aHjvrGrh9h5WNqVk4iyDoarINlIy4OjJwRPgwySNoqVkKpXCYwohnBXH/4xDkepnrruM T9hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697379462; x=1697984262; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kvSWVbZXn/MrVEcGpo16B6JSHdwZ2fR7rly5J6YWZNE=; b=g6TGjFqVRwkqb9NG11wmJDwHBIWFQu7pdVnPyC3k6mjR6+5vXLcD9a/1r+9hwilTEo 2V31IA0pfBAtCO0ZrebPBbAuDd32f5gpMl1RjpAkftmP5ruqifMWcAYDSASVJw/Q/T1A 6iaDUYs9wg36MhbG3/DRItllwyJhBL0nld9trwuRZqYM8JrHJ99zGAFsIDEGNflSZsQP gFkqD4Qsvi6oHGm3siHKaZm/4rx16dWQUdjwBgd2Y/xH1ncTgJ9LrZS9UHpI/6DdaWVa Qnuzr8YmXjjC5WfKQivGpP1/ms+XdjvrAfMbdGhbpcEGEvbw25BYwbYyAUiobRk3+nDP lK2A== X-Gm-Message-State: AOJu0Yzq4MDsjNtSqEpdrQULH4ctIlBxBfy5A2pPnGaPQSGvo5LuJfCR CHD4E7AkJLc97kw3QfXD6DuY7Q== X-Google-Smtp-Source: AGHT+IE81It/1ElAFh3WqzA1MDP6+KyM5FALauAhwzot5b7EduhSxG7C2ZYhheRbI8ll3TgU9FJn+A== X-Received: by 2002:a17:902:aa48:b0:1c0:bcbc:d5d with SMTP id c8-20020a170902aa4800b001c0bcbc0d5dmr25880675plr.61.1697379461931; Sun, 15 Oct 2023 07:17:41 -0700 (PDT) Received: from localhost ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with UTF8SMTPSA id d8-20020a170903230800b001c62c9d7289sm6869426plh.104.2023.10.15.07.17.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 15 Oct 2023 07:17:41 -0700 (PDT) From: Akihiko Odaki Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jonathan Corbet , Willem de Bruijn , Jason Wang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Michael S. Tsirkin" , Xuan Zhuo , Mykola Lysenko , Shuah Khan , bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kselftest@vger.kernel.org, Yuri Benditovich , Andrew Melnychenko , Akihiko Odaki Subject: [RFC PATCH v2 4/7] virtio_net: Add virtio_net_hdr_v1_hash_from_skb() Date: Sun, 15 Oct 2023 23:16:32 +0900 Message-ID: <20231015141644.260646-5-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231015141644.260646-1-akihiko.odaki@daynix.com> References: <20231015141644.260646-1-akihiko.odaki@daynix.com> MIME-Version: 1.0 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org It is identical with virtio_net_hdr_from_skb() except that it impelements hash reporting. Signed-off-by: Akihiko Odaki --- include/linux/virtio_net.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h index 7b4dd69555e4..01e594b4586b 100644 --- a/include/linux/virtio_net.h +++ b/include/linux/virtio_net.h @@ -216,4 +216,26 @@ static inline int virtio_net_hdr_from_skb(const struct sk_buff *skb, return 0; } +static inline int virtio_net_hdr_v1_hash_from_skb(const struct sk_buff *skb, + struct virtio_net_hdr_v1_hash *hdr, + bool little_endian, + bool has_data_valid, + int vlan_hlen, + u32 hash_value, + u16 hash_report) +{ + int ret; + + memset(hdr, 0, sizeof(*hdr)); + + ret = virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)hdr, + little_endian, has_data_valid, vlan_hlen); + if (!ret) { + hdr->hash_value = cpu_to_le32(hash_value); + hdr->hash_report = cpu_to_le16(hash_report); + } + + return ret; +} + #endif /* _LINUX_VIRTIO_NET_H */ From patchwork Sun Oct 15 14:16:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 734829 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58936CDB47E for ; Sun, 15 Oct 2023 14:18:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230046AbjJOOSD (ORCPT ); Sun, 15 Oct 2023 10:18:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230248AbjJOOSA (ORCPT ); Sun, 15 Oct 2023 10:18:00 -0400 Received: from mail-ot1-x32a.google.com (mail-ot1-x32a.google.com [IPv6:2607:f8b0:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 558E3129 for ; Sun, 15 Oct 2023 07:17:50 -0700 (PDT) Received: by mail-ot1-x32a.google.com with SMTP id 46e09a7af769-6c63588b554so2475397a34.0 for ; Sun, 15 Oct 2023 07:17:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1697379469; x=1697984269; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ke33nDzLmipl3SjRyYaM61RMA7lxXnZa/f+yQU5tzpk=; b=kD2bkSBghNq/T2hYpS/+oxZ4nmFIDOx2d5OGFwe4xd0xb4QQaNMZBDnOZUBio8NClX 5kkU+58w4ozuf+5ZNbxkVilJolkY2iggu8riVS5yj4k9edM5VFyteazzSKUqHRQHk/df b58toKI0pGRWV1uFJFogEATh0GOr6UovIAo7EyYPaMhN7iSicVDVtNO78297q4NDBGJD kA+8FQPAtWx/p0r4UV1PdALCc7Lzjmqqe/BhxKVM0znwuwv33wLTLexTCiVp3kXXzFwv lkOoHsQDO7IubFJkHlwZNRzsBymWYHu/vf+OFJghENy3RezNTpitcdQwIBtUJkGzf+pn 1evg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697379469; x=1697984269; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ke33nDzLmipl3SjRyYaM61RMA7lxXnZa/f+yQU5tzpk=; b=PTHYw+cRk7X2Poim3AknYAvUCONb3FgHh9FOAn7ymRM2PmqQkDNlqd/i6CBHizth+7 XG0SWeA5BCO8dFG4XX3PXFijTfp1noiqRXFUv+t3DZtK490S2D1aPwB8dIHRx5gFKIUb yUvFDNgOw+yJQ+Ej7hCC+sLtEPquJD6ppQWR5wmF1f+613IYCAioZn7rBuxJ9T9pLksZ wWY7ggyAfQivSB9+i3cuq3XyiZ/guuJbcMdIbBT75RIHc8TlepAmLyDfmzFxnqtRQdgh 0xVYVUnTwJnEA5W6KMI/I+iWpUgXn1CGXn0oSN3YT3m2QRXZRaJtlGxGyccuh5HXR/JO ldnw== X-Gm-Message-State: AOJu0YyHr+89nzcm+Kn8LC3G3C4K+BqYRcAlaOepnNuc/2nVcCnV+Qjo SMq7AuaZRdNMF346+f2152le0A== X-Google-Smtp-Source: AGHT+IH7Nqw9l9pQGBIDLc7LROP4B9WO3/Rf0e7rU1BiIDInOq3e6XPZWZv49whJ5GeUl650syvp3w== X-Received: by 2002:a05:6830:208:b0:6b8:82ed:ea2e with SMTP id em8-20020a056830020800b006b882edea2emr35127511otb.4.1697379469443; Sun, 15 Oct 2023 07:17:49 -0700 (PDT) Received: from localhost ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with UTF8SMTPSA id o14-20020aa7978e000000b006b5922221f4sm3556073pfp.8.2023.10.15.07.17.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 15 Oct 2023 07:17:49 -0700 (PDT) From: Akihiko Odaki Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jonathan Corbet , Willem de Bruijn , Jason Wang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Michael S. Tsirkin" , Xuan Zhuo , Mykola Lysenko , Shuah Khan , bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kselftest@vger.kernel.org, Yuri Benditovich , Andrew Melnychenko , Akihiko Odaki Subject: [RFC PATCH v2 5/7] tun: Support BPF_PROG_TYPE_VNET_HASH Date: Sun, 15 Oct 2023 23:16:33 +0900 Message-ID: <20231015141644.260646-6-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231015141644.260646-1-akihiko.odaki@daynix.com> References: <20231015141644.260646-1-akihiko.odaki@daynix.com> MIME-Version: 1.0 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Support BPF_PROG_TYPE_VNET_HASH with TUNSETSTEERINGEBPF ioctl to make it possible to report hash values and types when steering packets. Signed-off-by: Akihiko Odaki --- drivers/net/tun.c | 158 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 117 insertions(+), 41 deletions(-) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 89ab9efe522c..e0b453572a64 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -543,19 +543,37 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb) static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb) { + struct bpf_skb_vnet_hash_end *cb = (struct bpf_skb_vnet_hash_end *)skb->cb; + struct tun_vnet_hash *ext; struct tun_prog *prog; u32 numqueues; - u16 ret = 0; + u16 queue = 0; + + BUILD_BUG_ON(sizeof(*cb) > sizeof(skb->cb)); numqueues = READ_ONCE(tun->numqueues); if (!numqueues) return 0; prog = rcu_dereference(tun->steering_prog); - if (prog) - ret = bpf_prog_run_clear_cb(prog->prog, skb); + if (prog) { + if (prog->prog->type == BPF_PROG_TYPE_VNET_HASH) { + memset(skb->cb, 0, sizeof(*cb) - sizeof(struct qdisc_skb_cb)); + bpf_prog_run_clear_cb(prog->prog, skb); + + ext = skb_ext_add(skb, SKB_EXT_TUN_VNET_HASH); + if (ext) { + ext->value = cb->hash_value; + ext->report = cb->hash_report; + } - return ret % numqueues; + queue = cb->rss_queue; + } else { + queue = bpf_prog_run_clear_cb(prog->prog, skb); + } + } + + return queue % numqueues; } static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, @@ -2116,31 +2134,74 @@ static ssize_t tun_put_user(struct tun_struct *tun, } if (vnet_hdr_sz) { - struct virtio_net_hdr gso; + struct bpf_skb_vnet_hash_end *cb = (struct bpf_skb_vnet_hash_end *)skb->cb; + struct tun_prog *prog; + struct tun_vnet_hash *vnet_hash_p; + struct tun_vnet_hash vnet_hash; + size_t vnet_hdr_content_sz = sizeof(struct virtio_net_hdr); + union { + struct virtio_net_hdr hdr; + struct virtio_net_hdr_v1_hash hdr_v1_hash; + } vnet_hdr; + int ret; if (iov_iter_count(iter) < vnet_hdr_sz) return -EINVAL; - if (virtio_net_hdr_from_skb(skb, &gso, - tun_is_little_endian(tun), true, - vlan_hlen)) { + if (vnet_hdr_sz >= sizeof(struct virtio_net_hdr_v1_hash)) { + vnet_hash_p = skb_ext_find(skb, SKB_EXT_TUN_VNET_HASH); + if (vnet_hash_p) { + vnet_hash = *vnet_hash_p; + vnet_hdr_content_sz = sizeof(struct virtio_net_hdr_v1_hash); + } else { + rcu_read_lock(); + prog = rcu_dereference(tun->steering_prog); + if (prog && prog->prog->type == BPF_PROG_TYPE_VNET_HASH) { + memset(skb->cb, 0, + sizeof(*cb) - sizeof(struct qdisc_skb_cb)); + bpf_prog_run_clear_cb(prog->prog, skb); + vnet_hash.value = cb->hash_value; + vnet_hash.report = cb->hash_report; + vnet_hdr_content_sz = + sizeof(struct virtio_net_hdr_v1_hash); + } + rcu_read_unlock(); + } + } + + switch (vnet_hdr_content_sz) { + case sizeof(struct virtio_net_hdr): + ret = virtio_net_hdr_from_skb(skb, &vnet_hdr.hdr, + tun_is_little_endian(tun), true, + vlan_hlen); + break; + + case sizeof(struct virtio_net_hdr_v1_hash): + ret = virtio_net_hdr_v1_hash_from_skb(skb, &vnet_hdr.hdr_v1_hash, + tun_is_little_endian(tun), true, + vlan_hlen, + vnet_hash.value, vnet_hash.report); + break; + } + + if (ret) { struct skb_shared_info *sinfo = skb_shinfo(skb); pr_err("unexpected GSO type: " "0x%x, gso_size %d, hdr_len %d\n", - sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size), - tun16_to_cpu(tun, gso.hdr_len)); + sinfo->gso_type, tun16_to_cpu(tun, vnet_hdr.hdr.gso_size), + tun16_to_cpu(tun, vnet_hdr.hdr.hdr_len)); print_hex_dump(KERN_ERR, "tun: ", DUMP_PREFIX_NONE, 16, 1, skb->head, - min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true); + min((int)tun16_to_cpu(tun, vnet_hdr.hdr.hdr_len), 64), true); WARN_ON_ONCE(1); return -EINVAL; } - if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso)) + if (copy_to_iter(&vnet_hdr, vnet_hdr_content_sz, iter) != vnet_hdr_content_sz) return -EFAULT; - iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); + iov_iter_advance(iter, vnet_hdr_sz - vnet_hdr_content_sz); } if (vlan_hlen) { @@ -2276,13 +2337,13 @@ static void tun_prog_free(struct rcu_head *rcu) { struct tun_prog *prog = container_of(rcu, struct tun_prog, rcu); - bpf_prog_destroy(prog->prog); + bpf_prog_put(prog->prog); kfree(prog); } -static int __tun_set_ebpf(struct tun_struct *tun, - struct tun_prog __rcu **prog_p, - struct bpf_prog *prog) +static int tun_set_ebpf(struct tun_struct *tun, + struct tun_prog __rcu **prog_p, + struct bpf_prog *prog) { struct tun_prog *old, *new = NULL; @@ -2314,8 +2375,8 @@ static void tun_free_netdev(struct net_device *dev) free_percpu(dev->tstats); tun_flow_uninit(tun); security_tun_dev_free_security(tun->security); - __tun_set_ebpf(tun, &tun->steering_prog, NULL); - __tun_set_ebpf(tun, &tun->filter_prog, NULL); + tun_set_ebpf(tun, &tun->steering_prog, NULL); + tun_set_ebpf(tun, &tun->filter_prog, NULL); } static void tun_setup(struct net_device *dev) @@ -3007,26 +3068,6 @@ static int tun_set_queue(struct file *file, struct ifreq *ifr) return ret; } -static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p, - void __user *data) -{ - struct bpf_prog *prog; - int fd; - - if (copy_from_user(&fd, data, sizeof(fd))) - return -EFAULT; - - if (fd == -1) { - prog = NULL; - } else { - prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER); - if (IS_ERR(prog)) - return PTR_ERR(prog); - } - - return __tun_set_ebpf(tun, prog_p, prog); -} - /* Return correct value for tun->dev->addr_len based on tun->dev->type. */ static unsigned char tun_get_addr_len(unsigned short type) { @@ -3077,6 +3118,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd, struct ifreq ifr; kuid_t owner; kgid_t group; + struct bpf_prog *prog; + int fd; int sndbuf; int vnet_hdr_sz; int le; @@ -3360,11 +3403,44 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd, break; case TUNSETSTEERINGEBPF: - ret = tun_set_ebpf(tun, &tun->steering_prog, argp); + if (copy_from_user(&fd, argp, sizeof(fd))) { + ret = -EFAULT; + break; + } + + if (fd == -1) { + prog = NULL; + } else { + prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_VNET_HASH); + if (IS_ERR(prog)) { + prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER); + if (IS_ERR(prog)) { + ret = PTR_ERR(prog); + break; + } + } + } + + ret = tun_set_ebpf(tun, &tun->steering_prog, prog); break; case TUNSETFILTEREBPF: - ret = tun_set_ebpf(tun, &tun->filter_prog, argp); + if (copy_from_user(&fd, argp, sizeof(fd))) { + ret = -EFAULT; + break; + } + + if (fd == -1) { + prog = NULL; + } else { + prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER); + if (IS_ERR(prog)) { + ret = PTR_ERR(prog); + break; + } + } + + ret = tun_set_ebpf(tun, &tun->filter_prog, prog); break; case TUNSETCARRIER: From patchwork Sun Oct 15 14:16:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 733962 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B863CCDB47E for ; Sun, 15 Oct 2023 14:18:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230173AbjJOOSU (ORCPT ); Sun, 15 Oct 2023 10:18:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230372AbjJOOSO (ORCPT ); Sun, 15 Oct 2023 10:18:14 -0400 Received: from mail-ot1-x333.google.com (mail-ot1-x333.google.com [IPv6:2607:f8b0:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69CC4101 for ; Sun, 15 Oct 2023 07:17:57 -0700 (PDT) Received: by mail-ot1-x333.google.com with SMTP id 46e09a7af769-6c49f781855so2454413a34.3 for ; Sun, 15 Oct 2023 07:17:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1697379477; x=1697984277; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/1mvW2t/KaK6qhmD8VB0bMy9T0sNtWqMqFjE4LFqirU=; b=PEYPCRgKus0rda0MhcXO5x2VbSl1KXWVJNCMQOGwa6AvftUWVwhruiaWAsgzcw3QIl FYVCBe5knKjWpHSEpAumK5NvaPYkDPVl2CEWDlLHWX5QFILT+11Ln3x0iR6IskEywF4Z JV/TMWoOWecs2rO2avEt+022fbjjFvrl/MZd/qz6BkSvV06TLX3CiAF2/zThv9ALbybB CwrwkB/zOYLxsIW5rH0beUvGKg6Crm1qWlDCDdtYRbaCFjiy91kJkiG7iYQ5qSJbp4xI b7XVj/mL3WV+2/HuoWnKAWSDdm1mdj1TYq60IEhVu4dKAkZvjogDVrpRmyR0RDN79GBt +1TQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697379477; x=1697984277; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/1mvW2t/KaK6qhmD8VB0bMy9T0sNtWqMqFjE4LFqirU=; b=mjcmEqCjTAcABZ3J2vjFQMGfnwod18dkTa9CcRyd8YMuCJYn7jtZu6mszivvxelkwE aRzCafkfMq/csIdyVNEXGCxBMz7D5UbipF1NSpJxpZOZwHpN6I4QvZS22CXtZCTIG+/a AbBBaqn5fJPEcGprombdKDBVML5ZvgK/kmuYOCKXtewFrtw1zlztA4z7+WX/l3ggk3qA YY6mq2juebBawqQN4BrYJdd/RKAxXnt1DKzn3XPFLJ9s4HLAB2OcP9XAkq3PvYG2NVzg 8fvJ787jTb8pSO6AQFhJgTAoxZZ4WmFwaDafj7ldiVtHPGIZN53De+/TreJpFPFjtv23 EskA== X-Gm-Message-State: AOJu0Yy4/ojIKQ4KMzrV8bOy6v4herIZ5mOnZqWSg7Cxmm3872LL2Jlx 4kJthEZCnEu8eGv6NeVonh/PBQ== X-Google-Smtp-Source: AGHT+IE6nBGBidFux5UPalixOnmQwVzr+KLBQdRpHoTGY90spcP7YYT4kzA5jLuRVCH74DvN30YsVQ== X-Received: by 2002:a05:6830:4bc:b0:6bc:952a:1032 with SMTP id l28-20020a05683004bc00b006bc952a1032mr33799823otd.14.1697379477144; Sun, 15 Oct 2023 07:17:57 -0700 (PDT) Received: from localhost ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with UTF8SMTPSA id x6-20020a636306000000b005ab46970aaasm4098753pgb.17.2023.10.15.07.17.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 15 Oct 2023 07:17:56 -0700 (PDT) From: Akihiko Odaki Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jonathan Corbet , Willem de Bruijn , Jason Wang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Michael S. Tsirkin" , Xuan Zhuo , Mykola Lysenko , Shuah Khan , bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kselftest@vger.kernel.org, Yuri Benditovich , Andrew Melnychenko , Akihiko Odaki Subject: [RFC PATCH v2 6/7] selftests/bpf: Test BPF_PROG_TYPE_VNET_HASH Date: Sun, 15 Oct 2023 23:16:34 +0900 Message-ID: <20231015141644.260646-7-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231015141644.260646-1-akihiko.odaki@daynix.com> References: <20231015141644.260646-1-akihiko.odaki@daynix.com> MIME-Version: 1.0 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The added tests will ensure that the new relevant members of struct __sk_buff are initialized with 0, that the members are properly interpreted by tun, and tun checks the virtio-net header size before reporting hash values and types the BPF program computed. Signed-off-by: Akihiko Odaki --- tools/testing/selftests/bpf/config | 1 + tools/testing/selftests/bpf/config.aarch64 | 1 - .../selftests/bpf/prog_tests/vnet_hash.c | 385 ++++++++++++++++++ tools/testing/selftests/bpf/progs/vnet_hash.c | 16 + 4 files changed, 402 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/vnet_hash.c create mode 100644 tools/testing/selftests/bpf/progs/vnet_hash.c diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/bpf/config index e41eb33b2704..c05defa83b44 100644 --- a/tools/testing/selftests/bpf/config +++ b/tools/testing/selftests/bpf/config @@ -10,6 +10,7 @@ CONFIG_BPF_LSM=y CONFIG_BPF_STREAM_PARSER=y CONFIG_BPF_SYSCALL=y # CONFIG_BPF_UNPRIV_DEFAULT_OFF is not set +CONFIG_BRIDGE=y CONFIG_CGROUP_BPF=y CONFIG_CRYPTO_HMAC=y CONFIG_CRYPTO_SHA256=y diff --git a/tools/testing/selftests/bpf/config.aarch64 b/tools/testing/selftests/bpf/config.aarch64 index 253821494884..1bf6375ac7f3 100644 --- a/tools/testing/selftests/bpf/config.aarch64 +++ b/tools/testing/selftests/bpf/config.aarch64 @@ -17,7 +17,6 @@ CONFIG_BPF_JIT_ALWAYS_ON=y CONFIG_BPF_JIT_DEFAULT_ON=y CONFIG_BPF_PRELOAD_UMD=y CONFIG_BPF_PRELOAD=y -CONFIG_BRIDGE=m CONFIG_CGROUP_CPUACCT=y CONFIG_CGROUP_DEVICE=y CONFIG_CGROUP_FREEZER=y diff --git a/tools/testing/selftests/bpf/prog_tests/vnet_hash.c b/tools/testing/selftests/bpf/prog_tests/vnet_hash.c new file mode 100644 index 000000000000..4d71d7b5adc6 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/vnet_hash.c @@ -0,0 +1,385 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE + +#include +#include + +#include "test_progs.h" +#include "vnet_hash.skel.h" + +#include +#include +#include +#include + +#define TUN_HWADDR_SOURCE { 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 } +#define TUN_HWADDR_DEST { 0x02, 0x00, 0x00, 0x00, 0x00, 0x01 } + +#define TUN_IPADDR_SOURCE htonl((172 << 24) | (17 << 16) | 0) +#define TUN_IPADDR_DEST htonl((172 << 24) | (17 << 16) | 1) + +struct payload { + struct ethhdr ethhdr; + struct arphdr arphdr; + unsigned char sender_hwaddr[6]; + uint32_t sender_ipaddr; + unsigned char target_hwaddr[6]; + uint32_t target_ipaddr; +} __packed; + +static bool bpf_setup(struct vnet_hash **skel) +{ + *skel = vnet_hash__open(); + if (!ASSERT_OK_PTR(*skel, __func__)) + return false; + + if (!ASSERT_OK(vnet_hash__load(*skel), __func__)) { + vnet_hash__destroy(*skel); + return false; + } + + return true; +} + +static void bpf_teardown(struct vnet_hash *skel) +{ + vnet_hash__destroy(skel); +} + +static bool local_setup(int *fd) +{ + *fd = socket(AF_LOCAL, SOCK_STREAM, 0); + return ASSERT_GE(*fd, 0, __func__); +} + +static bool local_set_flags(int fd, const char *name, short flags) +{ + struct ifreq ifreq = { .ifr_flags = flags }; + + strcpy(ifreq.ifr_name, name); + + return ASSERT_OK(ioctl(fd, SIOCSIFFLAGS, &ifreq), __func__); +} + +static void local_teardown(int fd) +{ + ASSERT_OK(close(fd), __func__); +} + +static bool bridge_setup(int local_fd) +{ + if (!ASSERT_OK(ioctl(local_fd, SIOCBRADDBR, "xbridge"), __func__)) + return false; + + return local_set_flags(local_fd, "xbridge", IFF_UP); +} + +static bool bridge_add_if(int local_fd, const char *name) +{ + struct ifreq ifreq = { + .ifr_name = "xbridge", + .ifr_ifindex = if_nametoindex(name) + }; + + if (!ASSERT_NEQ(ifreq.ifr_ifindex, 0, __func__)) + return false; + + return ASSERT_OK(ioctl(local_fd, SIOCBRADDIF, &ifreq), __func__); +} + +static void bridge_teardown(int local_fd) +{ + if (!local_set_flags(local_fd, "xbridge", 0)) + return; + + ASSERT_OK(ioctl(local_fd, SIOCBRDELBR, "xbridge"), __func__); +} + +static bool tun_open(int *fd, char *ifname, short flags) +{ + struct ifreq ifr; + + *fd = open("/dev/net/tun", O_RDWR); + if (!ASSERT_GE(*fd, 0, __func__)) + return false; + + memset(&ifr, 0, sizeof(ifr)); + strcpy(ifr.ifr_name, ifname); + ifr.ifr_flags = flags | IFF_TAP | IFF_NAPI | IFF_NO_PI | + IFF_MULTI_QUEUE; + + if (!ASSERT_OK(ioctl(*fd, TUNSETIFF, (void *) &ifr), __func__)) { + ASSERT_OK(close(*fd), __func__); + return false; + } + + strcpy(ifname, ifr.ifr_name); + + return true; +} + +static bool tun_source_setup(int local_fd, int *fd) +{ + char ifname[IFNAMSIZ]; + + ifname[0] = 0; + if (!tun_open(fd, ifname, 0)) + return false; + + if (!bridge_add_if(local_fd, ifname)) { + ASSERT_OK(close(*fd), __func__); + return false; + } + + if (!local_set_flags(local_fd, ifname, IFF_UP)) { + ASSERT_OK(close(*fd), __func__); + return false; + } + + return true; +} + +static void tun_source_teardown(int fd) +{ + ASSERT_OK(close(fd), __func__); +} + +static bool tun_dest_setup(int local_fd, struct vnet_hash *bpf, + int *fd, char *ifname) +{ + struct { + struct virtio_net_hdr vnet_hdr; + struct payload payload; + } __packed packet = { + .payload = { + .ethhdr = { + .h_source = TUN_HWADDR_DEST, + .h_dest = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, + .h_proto = htons(ETH_P_ARP) + }, + .arphdr = { + .ar_hrd = htons(ARPHRD_ETHER), + .ar_pro = htons(ETH_P_IP), + .ar_hln = ETH_ALEN, + .ar_pln = 4, + .ar_op = htons(ARPOP_REQUEST) + }, + .sender_hwaddr = TUN_HWADDR_DEST, + .sender_ipaddr = TUN_IPADDR_DEST, + .target_ipaddr = TUN_IPADDR_DEST + } + }; + + int bpf_fd = bpf_program__fd(bpf->progs.prog); + + ifname[0] = 0; + if (!tun_open(fd, ifname, IFF_VNET_HDR)) + return false; + + if (!ASSERT_OK(ioctl(*fd, TUNSETSTEERINGEBPF, &bpf_fd), __func__)) + goto fail; + + if (!bridge_add_if(local_fd, ifname)) + goto fail; + + if (!local_set_flags(local_fd, ifname, IFF_UP)) + goto fail; + + if (!ASSERT_EQ(write(*fd, &packet, sizeof(packet)), sizeof(packet), __func__)) + goto fail; + + return true; + +fail: + ASSERT_OK(close(*fd), __func__); + return false; +} + +static void tun_dest_teardown(int fd) +{ + ASSERT_OK(close(fd), __func__); +} + +static bool tun_dest_queue_setup(char *ifname, int *fd) +{ + return tun_open(fd, ifname, IFF_VNET_HDR); +} + +static void tun_dest_queue_teardown(int fd) +{ + ASSERT_OK(close(fd), __func__); +} + +static void *test_vnet_hash_thread(void *arg) +{ + struct payload sent = { + .ethhdr = { + .h_source = TUN_HWADDR_SOURCE, + .h_dest = TUN_HWADDR_DEST, + .h_proto = htons(ETH_P_ARP) + }, + .arphdr = { + .ar_hrd = htons(ARPHRD_ETHER), + .ar_pro = htons(ETH_P_IP), + .ar_hln = ETH_ALEN, + .ar_pln = 4, + .ar_op = htons(ARPOP_REPLY) + }, + .sender_hwaddr = TUN_HWADDR_SOURCE, + .sender_ipaddr = TUN_IPADDR_SOURCE, + .target_hwaddr = TUN_HWADDR_DEST, + .target_ipaddr = TUN_IPADDR_DEST + }; + union { + struct virtio_net_hdr_v1_hash virtio_net_hdr; + uint8_t bytes[sizeof(struct virtio_net_hdr_v1_hash) + sizeof(struct payload)]; + } received; + struct vnet_hash *bpf; + int local_fd; + int source_fd; + int dest_fds[2]; + char dest_ifname[IFNAMSIZ]; + int vnet_hdr_sz; + + if (!ASSERT_OK(unshare(CLONE_NEWNET), "unshare")) + return NULL; + + if (!bpf_setup(&bpf)) + return NULL; + + if (!local_setup(&local_fd)) + goto fail_local; + + if (!bridge_setup(local_fd)) + goto fail_bridge; + + if (!tun_source_setup(local_fd, &source_fd)) + goto fail_tun_source; + + if (!tun_dest_setup(local_fd, bpf, dest_fds, dest_ifname)) + goto fail_tun_dest; + + if (!ASSERT_EQ(write(source_fd, &sent, sizeof(sent)), sizeof(sent), "write")) + goto fail_tests_single_queue; + + if (!ASSERT_EQ(read(dest_fds[0], &received, sizeof(received)), + sizeof(struct virtio_net_hdr) + sizeof(struct payload), + "read")) + goto fail_tests_single_queue; + + ASSERT_EQ(received.virtio_net_hdr.hdr.flags, 0, + "virtio_net_hdr.hdr.flags"); + ASSERT_EQ(received.virtio_net_hdr.hdr.gso_type, VIRTIO_NET_HDR_GSO_NONE, + "virtio_net_hdr.hdr.gso_type"); + ASSERT_EQ(received.virtio_net_hdr.hdr.hdr_len, 0, + "virtio_net_hdr.hdr.hdr_len"); + ASSERT_EQ(received.virtio_net_hdr.hdr.gso_size, 0, + "virtio_net_hdr.hdr.gso_size"); + ASSERT_EQ(received.virtio_net_hdr.hdr.csum_start, 0, + "virtio_net_hdr.hdr.csum_start"); + ASSERT_EQ(received.virtio_net_hdr.hdr.csum_offset, 0, + "virtio_net_hdr.hdr.csum_offset"); + ASSERT_EQ(memcmp(received.bytes + sizeof(struct virtio_net_hdr), &sent, sizeof(sent)), 0, + "payload"); + + vnet_hdr_sz = sizeof(struct virtio_net_hdr_v1_hash); + if (!ASSERT_OK(ioctl(dest_fds[0], TUNSETVNETHDRSZ, &vnet_hdr_sz), "TUNSETVNETHDRSZ")) + goto fail_tests_single_queue; + + if (!ASSERT_EQ(write(source_fd, &sent, sizeof(sent)), sizeof(sent), + "hash: write")) + goto fail_tests_single_queue; + + if (!ASSERT_EQ(read(dest_fds[0], &received, sizeof(received)), + sizeof(struct virtio_net_hdr_v1_hash) + sizeof(struct payload), + "hash: read")) + goto fail_tests_single_queue; + + ASSERT_EQ(received.virtio_net_hdr.hdr.flags, 0, + "hash: virtio_net_hdr.hdr.flags"); + ASSERT_EQ(received.virtio_net_hdr.hdr.gso_type, VIRTIO_NET_HDR_GSO_NONE, + "hash: virtio_net_hdr.hdr.gso_type"); + ASSERT_EQ(received.virtio_net_hdr.hdr.hdr_len, 0, + "hash: virtio_net_hdr.hdr.hdr_len"); + ASSERT_EQ(received.virtio_net_hdr.hdr.gso_size, 0, + "hash: virtio_net_hdr.hdr.gso_size"); + ASSERT_EQ(received.virtio_net_hdr.hdr.csum_start, 0, + "hash: virtio_net_hdr.hdr.csum_start"); + ASSERT_EQ(received.virtio_net_hdr.hdr.csum_offset, 0, + "hash: virtio_net_hdr.hdr.csum_offset"); + ASSERT_EQ(received.virtio_net_hdr.hdr.num_buffers, 0, + "hash: virtio_net_hdr.hdr.num_buffers"); + ASSERT_EQ(received.virtio_net_hdr.hash_value, htole32(3), + "hash: virtio_net_hdr.hash_value"); + ASSERT_EQ(received.virtio_net_hdr.hash_report, htole16(2), + "hash: virtio_net_hdr.hash_report"); + ASSERT_EQ(received.virtio_net_hdr.padding, 0, + "hash: virtio_net_hdr.padding"); + ASSERT_EQ(memcmp(received.bytes + sizeof(struct virtio_net_hdr_v1_hash), &sent, + sizeof(sent)), + 0, + "hash: payload"); + + if (!tun_dest_queue_setup(dest_ifname, dest_fds + 1)) + goto fail_tests_single_queue; + + if (!ASSERT_EQ(write(source_fd, &sent, sizeof(sent)), sizeof(sent), + "hash, multi queue: write")) + goto fail_tests_multi_queue; + + if (!ASSERT_EQ(read(dest_fds[1], &received, sizeof(received)), + sizeof(struct virtio_net_hdr_v1_hash) + sizeof(struct payload), + "hash, multi queue: read")) + goto fail_tests_multi_queue; + + ASSERT_EQ(received.virtio_net_hdr.hdr.flags, 0, + "hash, multi queue: virtio_net_hdr.hdr.flags"); + ASSERT_EQ(received.virtio_net_hdr.hdr.gso_type, VIRTIO_NET_HDR_GSO_NONE, + "hash, multi queue: virtio_net_hdr.hdr.gso_type"); + ASSERT_EQ(received.virtio_net_hdr.hdr.hdr_len, 0, + "hash, multi queue: virtio_net_hdr.hdr.hdr_len"); + ASSERT_EQ(received.virtio_net_hdr.hdr.gso_size, 0, + "hash, multi queue: virtio_net_hdr.hdr.gso_size"); + ASSERT_EQ(received.virtio_net_hdr.hdr.csum_start, 0, + "hash, multi queue: virtio_net_hdr.hdr.csum_start"); + ASSERT_EQ(received.virtio_net_hdr.hdr.csum_offset, 0, + "hash, multi queue: virtio_net_hdr.hdr.csum_offset"); + ASSERT_EQ(received.virtio_net_hdr.hdr.num_buffers, 0, + "hash, multi queue: virtio_net_hdr.hdr.num_buffers"); + ASSERT_EQ(received.virtio_net_hdr.hash_value, htole32(3), + "hash, multi queue: virtio_net_hdr.hash_value"); + ASSERT_EQ(received.virtio_net_hdr.hash_report, htole16(2), + "hash, multi queue: virtio_net_hdr.hash_report"); + ASSERT_EQ(received.virtio_net_hdr.padding, 0, + "hash, multi queue: virtio_net_hdr.padding"); + ASSERT_EQ(memcmp(received.bytes + sizeof(struct virtio_net_hdr_v1_hash), &sent, + sizeof(sent)), + 0, + "hash, multi queue: payload"); + +fail_tests_multi_queue: + tun_dest_queue_teardown(dest_fds[1]); +fail_tests_single_queue: + tun_dest_teardown(dest_fds[0]); +fail_tun_dest: + tun_source_teardown(source_fd); +fail_tun_source: + bridge_teardown(local_fd); +fail_bridge: + local_teardown(local_fd); +fail_local: + bpf_teardown(bpf); + + return NULL; +} + +void test_vnet_hash(void) +{ + pthread_t thread; + int err; + + err = pthread_create(&thread, NULL, &test_vnet_hash_thread, NULL); + if (ASSERT_OK(err, "pthread_create")) + ASSERT_OK(pthread_join(thread, NULL), "pthread_join"); +} diff --git a/tools/testing/selftests/bpf/progs/vnet_hash.c b/tools/testing/selftests/bpf/progs/vnet_hash.c new file mode 100644 index 000000000000..0451bab65647 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/vnet_hash.c @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +SEC("vnet_hash") +int prog(struct __sk_buff *skb) +{ + skb->vnet_hash_value ^= 3; + skb->vnet_hash_report ^= 2; + skb->vnet_rss_queue ^= 1; + + return BPF_OK; +} + +char _license[] SEC("license") = "GPL"; From patchwork Sun Oct 15 14:16:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 734828 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D03FCDB47E for ; Sun, 15 Oct 2023 14:18:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230451AbjJOOSZ (ORCPT ); Sun, 15 Oct 2023 10:18:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230306AbjJOOSS (ORCPT ); Sun, 15 Oct 2023 10:18:18 -0400 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9371F126 for ; Sun, 15 Oct 2023 07:18:05 -0700 (PDT) Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1c894e4573bso23378245ad.0 for ; Sun, 15 Oct 2023 07:18:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1697379485; x=1697984285; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kdDWVt4lpIJYL5Xl2mxJffHYtHkpJSxptvcjLCGS28w=; b=vOunXOCAmhMrt01hw74atFbAd6bVGAiWLvDrdCbXJjlBzkMWgXfaIB5fDVUNcEAWq8 la16JOSET2/HsegMXZJF2JrV+P/PefDWO2djWugpobLWpsLU3DfLkcss9jXpnd6n4o6S N55H+J0qnvWixOMkCAoJdbX2EphnaKK5N+2RdKLH4lR8DtI7yqAzrbqthAnclBtbYnCY 1jiEA09MyYiLYBJiTBQTE7JaxXxFm8hiY7xGXCB26uHC6u7mvecJwz2q1Q7Ke/sEO6iS /M3dD2pwIBeQDmzXKFc6462IUsJW9DxODVx3nCyGfmHYQ5f5Ct4ZQJ13OeMkUVSFdfGp dXzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697379485; x=1697984285; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kdDWVt4lpIJYL5Xl2mxJffHYtHkpJSxptvcjLCGS28w=; b=QLh/0LMNPnacGwCTtpcK6BivRttOoDb98jRqUlCEU+LpcMUZD5cVINO50+3HLOKvQt jFbTCMqkf2awIbmheP2skA2airKbwcy4Od5Ik0xNxYBhRw/rS34c8UVceQfGw+9aHGNd UeKO+Z0khE2auOLiyozgRMebEc6Q879zHrYkvmt2ke1myRxPSWUkFuTliMzMAEPJhR5a UF7aZaIP5XuO7UPKlMpOaDMrzO6wPtyQ7nLBeF4X+CNm3nkQONqnSEF57SIxnXzPcJnu jCwTrs/x+x0+PwyqxVFbNcx0TVA05ZOdWbBUEb32DicAbFvv+yvjZUcv/hy03MIADgMO JXPQ== X-Gm-Message-State: AOJu0Yx/pRYy33VbGw3TJACJOHliCRX6QsMCflB2uuBwU8A0hCFWoBrp rh9vKeFneArMXfkVtfhFFT5UrQ== X-Google-Smtp-Source: AGHT+IFOz3sSjM+TcbSSo8tYhnjasdIBbaMqi0ORUeBs87fmsaclhgIAt0gLsvqEr6XRlFwAo5iIlA== X-Received: by 2002:a05:6a20:42a2:b0:15a:290:d83d with SMTP id o34-20020a056a2042a200b0015a0290d83dmr33077200pzj.41.1697379484772; Sun, 15 Oct 2023 07:18:04 -0700 (PDT) Received: from localhost ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with UTF8SMTPSA id p4-20020a170902eac400b001b9da8b4eb7sm6859498pld.35.2023.10.15.07.17.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 15 Oct 2023 07:18:04 -0700 (PDT) From: Akihiko Odaki Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jonathan Corbet , Willem de Bruijn , Jason Wang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Michael S. Tsirkin" , Xuan Zhuo , Mykola Lysenko , Shuah Khan , bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kselftest@vger.kernel.org, Yuri Benditovich , Andrew Melnychenko , Akihiko Odaki Subject: [RFC PATCH v2 7/7] vhost_net: Support VIRTIO_NET_F_HASH_REPORT Date: Sun, 15 Oct 2023 23:16:35 +0900 Message-ID: <20231015141644.260646-8-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231015141644.260646-1-akihiko.odaki@daynix.com> References: <20231015141644.260646-1-akihiko.odaki@daynix.com> MIME-Version: 1.0 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org VIRTIO_NET_F_HASH_REPORT allows to report hash values calculated on the host. When VHOST_NET_F_VIRTIO_NET_HDR is employed, it will report no hash values (i.e., the hash_report member is always set to VIRTIO_NET_HASH_REPORT_NONE). Otherwise, the values reported by the underlying socket will be reported. VIRTIO_NET_F_HASH_REPORT requires VIRTIO_F_VERSION_1. Signed-off-by: Akihiko Odaki --- drivers/vhost/net.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c index f2ed7167c848..6a31d450fae2 100644 --- a/drivers/vhost/net.c +++ b/drivers/vhost/net.c @@ -73,6 +73,7 @@ enum { VHOST_NET_FEATURES = VHOST_FEATURES | (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) | (1ULL << VIRTIO_NET_F_MRG_RXBUF) | + (1ULL << VIRTIO_NET_F_HASH_REPORT) | (1ULL << VIRTIO_F_ACCESS_PLATFORM) | (1ULL << VIRTIO_F_RING_RESET) }; @@ -1634,10 +1635,13 @@ static int vhost_net_set_features(struct vhost_net *n, u64 features) size_t vhost_hlen, sock_hlen, hdr_len; int i; - hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | - (1ULL << VIRTIO_F_VERSION_1))) ? - sizeof(struct virtio_net_hdr_mrg_rxbuf) : - sizeof(struct virtio_net_hdr); + if (features & (1ULL << VIRTIO_NET_F_HASH_REPORT)) + hdr_len = sizeof(struct virtio_net_hdr_v1_hash); + else if (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | + (1ULL << VIRTIO_F_VERSION_1))) + hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); + else + hdr_len = sizeof(struct virtio_net_hdr); if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) { /* vhost provides vnet_hdr */ vhost_hlen = hdr_len; @@ -1718,6 +1722,10 @@ static long vhost_net_ioctl(struct file *f, unsigned int ioctl, return -EFAULT; if (features & ~VHOST_NET_FEATURES) return -EOPNOTSUPP; + if ((features & ((1ULL << VIRTIO_F_VERSION_1) | + (1ULL << VIRTIO_NET_F_HASH_REPORT))) == + (1ULL << VIRTIO_NET_F_HASH_REPORT)) + return -EINVAL; return vhost_net_set_features(n, features); case VHOST_GET_BACKEND_FEATURES: features = VHOST_NET_BACKEND_FEATURES;