From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by mail.toke.dk (Postfix) with ESMTPS id 433AAA1BE14 for ; Tue, 25 Jul 2023 02:00:12 +0200 (CEST) Authentication-Results: mail.toke.dk; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20221208 header.b=jvqN6LEF Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-583a4015791so31720057b3.1 for ; Mon, 24 Jul 2023 17:00:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690243210; x=1690848010; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=j0xCHxAGKShVCxci31z5oM/FwnvfCpkAI53IOFmhMP4=; b=jvqN6LEFCiL/2RpEj9tFUv78qoXSNVJQ8GmjzbkVkfI1nBhvq2pRPEOyK3DGZp9psG EhcLMiGs/jpeQzi9FFKzCyIbtjatViStG1YmXhdiGbAJX4uVs5giviHuycjy2DhebK4a 1hGs77DA+OCyleHZSohx66GAaNRGt4b/JN17bDb81CPM7XDPaX1CMob0FxwNr1OzkkDm P5XtlNZLqsSWYQGk9B6KbZzO1ayZ6LmvRKlPl9XViqxWV9u238UiS+9w0DCvR6+Eugws fqfXtoO6CDj/AQTRwtzMMIKSkQhBEMrptyZe9DHKjnwsNIBFrFeFsdQG6R20H3nFQbAX P3eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690243210; x=1690848010; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=j0xCHxAGKShVCxci31z5oM/FwnvfCpkAI53IOFmhMP4=; b=a9r8DcIST4AAgXqWEZfLiYZPFSeykGruq+ixtBHPm7gj9jTdrbqG6XJlZNUHE76FTP 7Bie2uOppDNGjckMz54zzEp4mosUweUYb1J7I9wl50Gj+latB5Y7EUfL+n8lsOSlePBP iB0PscFCtedR60u8b2XjWR5Oq8B4PKOyhq6J7KXkX5dNlbH97k4zW5IYjRUMGwHiBn2s MTzZlAVtrnfFj0nLLLGjBuVKQUjy1deYn2iZkpknYk1jYNJJKcRUSpPxgAjpN8lnQTUM tFX1KzRstAVXU5zd0wy4URTLbos/e0u/qVVqiCZCbktTA4EzGew+AaLBEHVrwVSc25vM 2whQ== X-Gm-Message-State: ABy/qLb9Z8Dq9TEMy6ksqq8YPQqqkDGI1B/6Ya2cOzNGWdedd7Q+Wy9l LmeIHR7352UoPHHxGUyfb0onlTE= X-Google-Smtp-Source: APBJJlEgxr/axKa6jbuffaWm0GOh8TggDPXk67nc+YfYvbSh3OyokwhpRZY+YAjRzvWJ2vov0tNpeGY= X-Received: from sdf.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5935]) (user=sdf job=sendgmr) by 2002:a81:af21:0:b0:56c:e585:8b17 with SMTP id n33-20020a81af21000000b0056ce5858b17mr75742ywh.5.1690243210706; Mon, 24 Jul 2023 17:00:10 -0700 (PDT) Date: Mon, 24 Jul 2023 16:59:55 -0700 In-Reply-To: <20230724235957.1953861-1-sdf@google.com> Mime-Version: 1.0 References: <20230724235957.1953861-1-sdf@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230724235957.1953861-7-sdf@google.com> From: Stanislav Fomichev To: bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Message-ID-Hash: 324PBZYNWUGXATG5PVTUGVWXU4UD2JZ2 X-Message-ID-Hash: 324PBZYNWUGXATG5PVTUGVWXU4UD2JZ2 X-MailFrom: 3ihC_ZAMKCdMH245DD5A3.1DBM2E-67CIHM2E-EGD831I.C3I@flex--sdf.bounces.google.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, kuba@kernel.org, toke@kernel.org, willemb@google.com, dsahern@kernel.org, magnus.karlsson@intel.com, bjorn@kernel.org, maciej.fijalkowski@intel.com, hawk@kernel.org, netdev@vger.kernel.org, xdp-hints@xdp-project.net X-Mailman-Version: 3.3.8 Precedence: list Subject: [xdp-hints] [RFC net-next v4 6/8] selftests/bpf: Add csum helpers List-Id: XDP hardware hints design discussion Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Checksum helpers will be used to calculate pseudo-header checksum in AF_XDP metadata selftests. The helpers are mirroring existing kernel ones: - csum_tcpudp_magic : IPv4 pseudo header csum - csum_ipv6_magic : IPv6 pseudo header csum - csum_fold : fold csum and do one's complement Signed-off-by: Stanislav Fomichev --- tools/testing/selftests/bpf/network_helpers.h | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 694185644da6..d749757a36a3 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -67,4 +67,47 @@ struct nstoken; */ struct nstoken *open_netns(const char *name); void close_netns(struct nstoken *token); + +static __u16 csum_fold(__u32 csum) +{ + csum = (csum & 0xffff) + (csum >> 16); + csum = (csum & 0xffff) + (csum >> 16); + + return (__u16)~csum; +} + +static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, + __u32 len, __u8 proto, + __wsum csum) +{ + __u64 s = csum; + + s += (__u32)saddr; + s += (__u32)daddr; + s += htons(proto + len); + s = (s & 0xffffffff) + (s >> 32); + s = (s & 0xffffffff) + (s >> 32); + + return csum_fold((__u32)s); +} + +static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr, + const struct in6_addr *daddr, + __u32 len, __u8 proto, + __wsum csum) +{ + __u64 s = csum; + int i; + + for (i = 0; i < 4; i++) + s += (__u32)saddr->s6_addr32[i]; + for (i = 0; i < 4; i++) + s += (__u32)daddr->s6_addr32[i]; + s += htons(proto + len); + s = (s & 0xffffffff) + (s >> 32); + s = (s & 0xffffffff) + (s >> 32); + + return csum_fold((__u32)s); +} + #endif -- 2.41.0.487.g6d72f3e995-goog