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 0D6DBA17E67 for ; Fri, 7 Jul 2023 21:30:31 +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=rbggUvrv Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-57704a25be9so34400957b3.1 for ; Fri, 07 Jul 2023 12:30:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688758230; x=1691350230; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=soJ/4tm34L3bmuAuBxXjw7TVIvg3GBbPbu7fpYLry1k=; b=rbggUvrv6V32VM7k+4GTWk+PVZLun0x/s/A+BXLz5+P0UqGOKMDknhYUQXU8OHUrAT MP8jlFCrER3kggY8bDVb9dYiDi5wwDvnahZ2VIpduJGaWTOsoxWc5W4/aezbJDGywpYW m00Fh6uyXj+dd998dE0hKTiGAVV4S7qGqHZDfRccpe4YVafZV2giiRLo4XMMquRWh5cj NvR+kBXOOXU9Grkj95Mf4+AolE3RZnMVDm/Yhjng1EgmCQBav6cuf0GP/ISzvaLLHnfn cUunZVDGuWzQKYVQYf0E7ewCvt/Azf9OiJiDyo9X95pFpUZrpF8rUXtVxCLK7w+UHBXJ elVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688758230; x=1691350230; 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=soJ/4tm34L3bmuAuBxXjw7TVIvg3GBbPbu7fpYLry1k=; b=Jq+5n/Vo5YRiHKIq0vflyzZY31vh4JtxoUoOUgUBalKD95MdADJGIIZlroBvvXlkOB ackz+DoOk/xoMP6CsvSL91ImwJSk9WLwnYv15SzxCNJFu66ysiBhfHlwkGpSkxDhRmMk Uh7IsfFOiJSUkTi/S6HC9ZDNwn+SXbRMU48d3Rt01HfiEDFxZduFYBUqYtUe/7roGVgY HsUxEoC9NypVBOuMlkVfo5v8sDszxHx4xjvT7NedeL4q2f+LCRIywWOpyf4loTiL4YWo 0pMLZt3rw2mYKilzsSNrUzA/sUNLAv0xDl+NdOzXo95MC2xxSBOwJGMGA04hyzvqYEDp nz4w== X-Gm-Message-State: ABy/qLaHhYfrOC7thkUID95BKtPhylXez+xzRfp1xViQ7RwNBdarvX6H /kGjPteCZWR5kUTR7JT/NFvxEJQ= X-Google-Smtp-Source: APBJJlHUj9XjkAjBTLuWn0CzrSP86fxiB9BWE+OEAKmhD8cb2VCzDN8mhoLBBbc4a8T/h8lYyG4/crg= X-Received: from sdf.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5935]) (user=sdf job=sendgmr) by 2002:a81:af11:0:b0:56d:19ce:416c with SMTP id n17-20020a81af11000000b0056d19ce416cmr111873ywh.0.1688758229857; Fri, 07 Jul 2023 12:30:29 -0700 (PDT) Date: Fri, 7 Jul 2023 12:30:04 -0700 In-Reply-To: <20230707193006.1309662-1-sdf@google.com> Mime-Version: 1.0 References: <20230707193006.1309662-1-sdf@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230707193006.1309662-13-sdf@google.com> From: Stanislav Fomichev To: bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Message-ID-Hash: G2VKQOW5LPXOOD4ECJEAX5SGK43CMQPP X-Message-ID-Hash: G2VKQOW5LPXOOD4ECJEAX5SGK43CMQPP X-MailFrom: 31WeoZAMKCXIiTVWeeWbU.SecnTf-XYdjinTf-fheZUSj.dUj@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 bpf-next v3 12/14] 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 380047161aac..47a837c0d796 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -68,4 +68,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.255.g8b1d071c50-goog