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 7BBEDA2E2B1 for ; Tue, 3 Oct 2023 22:05:39 +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=20230601 header.b=iCN78HO1 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a234ffeb90so18584767b3.3 for ; Tue, 03 Oct 2023 13:05:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696363538; x=1696968338; darn=xdp-project.net; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=i46mDPbkS444ecqdpl7wlqYw/jPyqLr/xzNOaA+jOp0=; b=iCN78HO1K/DKl5JB8cEFWDhoulSsJrs6FFu0Pd8yWvTu8FOE7MbdYr/urmb7BtM8bZ aW5JFVj7VW3YwxiGEd1nb40jKkDUOEwgizTIPKBqfebM1Efbt1X+B+9bXBIjuW1qyosZ bNoNOVvuDto7dT/iZFQF6zoNlSQ9ZDytKp+mm0ityO9f8CE3i7s3ofs/95bfO5SlqGf6 bRdp0xnKZHxRsNDygzOJBYkQi2SEn3338X0VJzQglWWi2ZDc5J/XhOGOPJDv1peek7ix lZnyRbTB4li+oaxAlbpO1AhMhsCk8EIBJB5M5xa+T9XyhhbQBweoV1wwsa7jNqdJdVlX IFhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696363538; x=1696968338; 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=i46mDPbkS444ecqdpl7wlqYw/jPyqLr/xzNOaA+jOp0=; b=HfSlk5Smn8AkEZz9pSf0GW+k29Hr6nVhkGXsr6wm2uz4BrpbmkVTQv0QjbM/NLXzKl 1bVR2Jy9QUds3FIzDWlzdkHtSbpTBvHd9RGIBtBjsrgI2sQOHKmPbGvYkwGP0s/s9CAB Eejl1w6Bpn/ZGvSJZRmUx4Am0cO1e2zQ6KqPNznarSt6Eg6vrESuwqmXNqVH26jzid1X avtqq+RA43E4lVi2TMcvjyVEbomzmgUVznB0Ut99E5uFirZc/WfFheTEFG39V5fGTlfi bT3YgpGb0vwVIwkQ2pQ6vuowujyuY1DcG4nnydPPranngWpu8Eleoo7yM9fV5rW8lhaQ ZakQ== X-Gm-Message-State: AOJu0Yxq70qBqyk6TsK9J/vt+7lRoNNRS9v4jUSQCwUbpG/uLiM5fth/ HZc62Lo2xHCU5/n4DRBI9kQ8mNQ= X-Google-Smtp-Source: AGHT+IGvz5XH8tMQAIcRbgTpu83PYPgI1FfIBafasTRbUK4Y+pHcK8ImXgPrHYlzONMcQsDRhxaRCvQ= X-Received: from sdf.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5935]) (user=sdf job=sendgmr) by 2002:a81:441c:0:b0:59b:ec33:ec6c with SMTP id r28-20020a81441c000000b0059bec33ec6cmr9223ywa.9.1696363538214; Tue, 03 Oct 2023 13:05:38 -0700 (PDT) Date: Tue, 3 Oct 2023 13:05:19 -0700 In-Reply-To: <20231003200522.1914523-1-sdf@google.com> Mime-Version: 1.0 References: <20231003200522.1914523-1-sdf@google.com> X-Mailer: git-send-email 2.42.0.582.g8ccd20d70d-goog Message-ID: <20231003200522.1914523-8-sdf@google.com> From: Stanislav Fomichev To: bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Message-ID-Hash: IQNI3YLGC3BLTUF2VWDIOUNPIQTS5BR2 X-Message-ID-Hash: IQNI3YLGC3BLTUF2VWDIOUNPIQTS5BR2 X-MailFrom: 3EnQcZQMKCZsN8ABJJBG9.7JHS8K-CDIONS8K-KMJE97O.I9O@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, yoong.siang.song@intel.com, netdev@vger.kernel.org, xdp-hints@xdp-project.net X-Mailman-Version: 3.3.8 Precedence: list Subject: [xdp-hints] [PATCH bpf-next v3 07/10] 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 5eccc67d1a99..654a854c9fb2 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -70,4 +70,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.42.0.582.g8ccd20d70d-goog