From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by mail.toke.dk (Postfix) with ESMTPS id 4E914A1FF1C for ; Wed, 9 Aug 2023 18:54:33 +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=GicCoH9o Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-563379fe16aso101310a12.3 for ; Wed, 09 Aug 2023 09:54:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691600072; x=1692204872; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tPNCL2h2Yk0iVM2TXMAyw8lteKar2ayTQ5gnI+CrlwY=; b=GicCoH9oxpAap2NbclDS+ijO8buo3MYC+WBOhdHvZWEGNWxoSjHEFY8e8/K8gbgHy4 snYETT/pzh2iJ6GygT/K3hMwG621KGphqH/njC4rtPxFsKivW4TaHksAgcsJJi02TNJD I2sguxYimENc2zRlmF264uNrtCAYNXRmMR6/nsOGBKG0KkItmyo659GTiWFoGx09ffT7 CWVGrrzxFzjhUTffXVkEncnxbXCZCjQXO1azrzK+NnQOjFHRNY1Sv5WUnzO5TNitqQbc aSMboJkb/AGjLQ8rLvYY+q5RbCcG0ZOKrGBWXEGd3MtJyIwfeNBEkRCzni7PuhaNz9vB 5ojg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691600072; x=1692204872; 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=tPNCL2h2Yk0iVM2TXMAyw8lteKar2ayTQ5gnI+CrlwY=; b=KGn/hMt4h5PpsgXy4uZ+P1u3aXKZeSspUzNHjzpCAcoqQgMwPdO6mnrKw6x/cg9eg+ anbWSt23ujBHJBmhQf5ZZApc4OpIpZO/kATHsb3TK8A8iPb5XWqbmEJWQz0r+nB5dTuR PGqD3nmNCxE513QCpvo1Qe3UKIRFvsXLsViLqWaB1/XKIl+Z5bmok4cwpvlnq0RM6i5g 23bN9ryMiVrsx47GP7qzkwcfXi+MywGq5d14mtqNUn7zS/Votq2vItxDExGJijJH/sUa TYH558e5t4G5FQ1Nahf/k+huKIfN8D/VQkrN+WZATIfJYiepWc4POzOfsreEJGyK7HNt pk1w== X-Gm-Message-State: AOJu0Yz8igCf9E+1hbcmNmqpU8R7zccoyl30JRI/LKf1Wc/r3vem9fOH cqu6IO0ZM79HB6+cOwFDqfLc3ls= X-Google-Smtp-Source: AGHT+IEQ7jDbp63pxepzuS37WQVUvH0F8J/jIFOfJDxRIhRIMXfGCPUxrcw+7XNUyKR4ZuJcrc2C/N4= X-Received: from sdf.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5935]) (user=sdf job=sendgmr) by 2002:a63:3dc3:0:b0:53f:f4ca:1b0 with SMTP id k186-20020a633dc3000000b0053ff4ca01b0mr294649pga.9.1691600071807; Wed, 09 Aug 2023 09:54:31 -0700 (PDT) Date: Wed, 9 Aug 2023 09:54:15 -0700 In-Reply-To: <20230809165418.2831456-1-sdf@google.com> Mime-Version: 1.0 References: <20230809165418.2831456-1-sdf@google.com> X-Mailer: git-send-email 2.41.0.640.ga95def55d0-goog Message-ID: <20230809165418.2831456-7-sdf@google.com> From: Stanislav Fomichev To: bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Message-ID-Hash: 5U2GJGHWLKXQQOQPVPTV6ILF7GZDF5DX X-Message-ID-Hash: 5U2GJGHWLKXQQOQPVPTV6ILF7GZDF5DX X-MailFrom: 3x8TTZAMKCcwAvxy66y3w.u64Fv7-z05BAFv7-7961wuB.5wB@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] [PATCH bpf-next 6/9] 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.41.0.640.ga95def55d0-goog