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 D021BA2A339 for ; Thu, 14 Sep 2023 23:05:07 +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=zgwfPBvZ Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-577af72294bso1102428a12.2 for ; Thu, 14 Sep 2023 14:05:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694725506; x=1695330306; 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=14VbHOqDafwAEmRkcsvovpu96sXlLVwtTgqqhNnYgGk=; b=zgwfPBvZue2k2LIgClCApqEezdaHEZeCpPJ1F780waXxW69ffMIESKe+GUPD9wRyuz l6alnjk+faK+GB2otJGBBhDC1ZUTVPjvdyGxLVFNqsUO29t0CQERM7NN4Y3hzuJf2EYy 6heapo3izSJjkvd6QvcOpkhd6JJc1KJoMxeOvtAGbhx+2d+kil5m93uTBXOePOAg8zgz 3cRm+wH0Ufws8fs5m/f7+c363aDnWWklKdronvysUQOaeBE2EqYudfKlNFMeJuPeVu5N xefFNNvQ6nKE597nGt8Je51rfpz2NhK0jGnaYjD9ZyfpuF0qqR+T89wIse+IADKyk6lZ Lzlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694725506; x=1695330306; 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=14VbHOqDafwAEmRkcsvovpu96sXlLVwtTgqqhNnYgGk=; b=VmApL7ByseCHd8L/uZ6NLO6I5JSB6nIzUkYUUUMnf53AuEX614W9DXJHm3WQkOohr2 agoV/VooPwgmWXKvYbRPtvlKazgz/+vzfznUw6IAl7kdhqpvm30jT2zOoMiRgCfHFO8b 25BqI4AZ7C92kcEec1wEA/TU+5YN4hLQ3Zx3ObLo2b4JujrJ1X9xUMlkKZdIntbXs3fw keAA1Lbn3+rdKqXtdjbuGCRsxD/ZqW5DaE8T33bcGSm17p19gncsglgazXqpc7QDlaLX 65U6bE41hMwzWt+0UqtmAuefN8uDoOX98uKmX6VJAeqZX28shqDylA0+Aax4YHO0lCIj ka6w== X-Gm-Message-State: AOJu0YyT3mr3eEtvtfMQ+DTelmOXs0I/HN2YmqR3BOJmkkBG0o28qtPq S0HldTMCklzv9CmDQDhdsY28kww= X-Google-Smtp-Source: AGHT+IHwSi48qvJdhvmPfcZWPgqPC20HubxSk5DHX5wOCnxgL7h677co1BhFyfAM+WdU60ZWR+hFZKA= X-Received: from sdf.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5935]) (user=sdf job=sendgmr) by 2002:a63:be41:0:b0:563:84fc:f4dd with SMTP id g1-20020a63be41000000b0056384fcf4ddmr146499pgo.6.1694725506209; Thu, 14 Sep 2023 14:05:06 -0700 (PDT) Date: Thu, 14 Sep 2023 14:04:49 -0700 In-Reply-To: <20230914210452.2588884-1-sdf@google.com> Mime-Version: 1.0 References: <20230914210452.2588884-1-sdf@google.com> X-Mailer: git-send-email 2.42.0.459.ge4e396fd5e-goog Message-ID: <20230914210452.2588884-7-sdf@google.com> From: Stanislav Fomichev To: bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Message-ID-Hash: RT4DP4ORUDMQ4A5OT6JOLQN6TI2MEVLC X-Message-ID-Hash: RT4DP4ORUDMQ4A5OT6JOLQN6TI2MEVLC X-MailFrom: 3gnUDZQMKCakbMOPXXPUN.LXVgMY-QRWcbgMY-YaXSNLc.WNc@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 v2 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.42.0.459.ge4e396fd5e-goog