From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by mail.toke.dk (Postfix) with ESMTPS id 09D93A33246 for ; Thu, 19 Oct 2023 19:50:00 +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=do/kbgXf Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9b9f56489fso7030134276.1 for ; Thu, 19 Oct 2023 10:49:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697737799; x=1698342599; 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=wrMWyFZARXAbpMZ2Cvd01jTOzKURZkdFORFzsP3Nbog=; b=do/kbgXfCqnI/w3SZhcSQql2IWJnVNLaDLwW7BjmoXOldNpo8rOHDT+34G+14uHwrU KkaPGqEAbSwAISaab1EYFfIt5q2LGPza+QvD+Ym+cn8fZ8BgZLwA4a/ZVzdj72wW8bXr pDwTZTLPWNGvYSCbycuQGWrN3vusVBx1OCNI0pDF0UULo7/+EjgwmIxKgK3VYcWlw4FA nsWVtMHaINL0MuvenKmUvO2A3+Fsf3mKrgW35GcRB/mpAKD9Rq1vAKPZ9lYR91PXzwWg vvakH+Bx4gv350l77Ky+0y2F/KDFcGeVJKyIHwQZva9Vo3Pl3eurngO5wEJDKU94GS9t 4ncA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697737799; x=1698342599; 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=wrMWyFZARXAbpMZ2Cvd01jTOzKURZkdFORFzsP3Nbog=; b=p+PdGBYf545dRsx8x6oqWIlwcg3kyLjlMHTgUTkAtdXl5nrQ9DPckGUajF1oQfwq2q Y3eJ0GeR/v4+h1aIM67pwxYMCvDXGiCaf64j/cawOGBXrWnCc2jJxCFCSFKw4UzsglsH rxoA/G0yL+jjKXZN997zSwNfelWaSD0SXHcmmlZSgj8pGHWDUjSoWyUaspHnxasMihXa df2Kex0xd0bQ2mVo/K/zSXcGy3Y+8B7UJAhtvsYAuACvIBViAGzbre8YW8t9/1ctDLG3 t1L5zjwTrQZSJ8MZiV7CtPm2bYCj+9iQE34NAL5nK8epXyyGGats1oMm/zGG+9LtIQJT AvQQ== X-Gm-Message-State: AOJu0YyPUG4TXtvPaJ9uPGVDeBKx3oveIsj86/QVnR1R65LT950iYs96 zliiqFgHsafqTkYiYLcybW1SKLo= X-Google-Smtp-Source: AGHT+IHxAHuZty9GGyTtLqkmEaq6ArCG74wlQJWAeDJOpo/lmMbANhh3Wg7LQS1zGZMT2E0QcNDyIUs= X-Received: from sdf.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5935]) (user=sdf job=sendgmr) by 2002:a25:2683:0:b0:d9a:519f:d0e6 with SMTP id m125-20020a252683000000b00d9a519fd0e6mr64878ybm.6.1697737798751; Thu, 19 Oct 2023 10:49:58 -0700 (PDT) Date: Thu, 19 Oct 2023 10:49:40 -0700 In-Reply-To: <20231019174944.3376335-1-sdf@google.com> Mime-Version: 1.0 References: <20231019174944.3376335-1-sdf@google.com> X-Mailer: git-send-email 2.42.0.655.g421f12c284-goog Message-ID: <20231019174944.3376335-8-sdf@google.com> From: Stanislav Fomichev To: bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Message-ID-Hash: UPWS2CW5EH6B44ISZEI4U622A3IUC77M X-Message-ID-Hash: UPWS2CW5EH6B44ISZEI4U622A3IUC77M X-MailFrom: 3RmwxZQMKCRUDy019916z.x97IyA-238EDIyA-AC94zxE.8zE@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 v4 07/11] 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 34f1200a781b..94b9be24e39b 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -71,4 +71,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.655.g421f12c284-goog