From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 3173441CA4; Wed, 15 Feb 2023 11:54:51 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id BEE49410DD; Wed, 15 Feb 2023 11:54:50 +0100 (CET) Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by mails.dpdk.org (Postfix) with ESMTP id 665FD40EE3 for ; Wed, 15 Feb 2023 11:54:49 +0100 (CET) Received: by mail-pj1-f44.google.com with SMTP id d8-20020a17090ad98800b002344fa17c8bso1531404pjv.5 for ; Wed, 15 Feb 2023 02:54:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ttg/vteFpc/a8oIYzew9L5FM7u3YA+erWt8gy8P7Zd4=; b=OQI/W/jmw65fgHlFQPVnOljD+LR91cTI7iQMr8uBgw0pQFT0Chbi+EKXZ9kYjoHxAu EtUQL6TXlmq4STZ9D/6uI3aNpDE/8HiN+b0Y9otREMR0dSZh1X/zTF7c8wnfeEcb2XZX k9SyrIQcTnSXMi/5eYLBAQkuvgfizi/AApOr/Q9FPnrly+dW2BpB2x2rbC3RXkiqROqq bQn2lkwDh8kNCj8jC/486Kf+hijD11/1GhWIKL6gTAEYQ8lr+m4NnmYpl2YCqTArppcB GqOT/jESZBCfQjdM7KhCEOu3zQNhJol+RmifuC4wp7lbJCbjTD92mXsYwwlT9tZk7r0V hncQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ttg/vteFpc/a8oIYzew9L5FM7u3YA+erWt8gy8P7Zd4=; b=f6DD+qgFxMyDZwhv9dJ7+2b91Wo9jyWjgEmlg2x/mtfP7PH0iLGBcnpX/ZS5Wv4LWC uqLK3BoMkeBW4jNsPQ9Irj1MT5vFUbS63kSFaMXnSc/yIOVnqJFfxrgiGkhdyPnFlGjl AdrhCacun8AvTIKrtMzxGJDVmRv73M1s9uzkXYZKwSDCrxPkQH0MxQC+xqzmKd9EnHxs 6XFzdUosE8RYdYQlwaOqujEJYPbT4Z6IqxpPE2JqOci9EqDTvE1mav6UQ6rVUtsfxdAK XfW4RnbF+o8BZXP2o2/cfRAtatU2GcUxIzgNBEro9zx+yd/F4jpXDIw9U3wZ0RQGO+w+ HRLA== X-Gm-Message-State: AO0yUKUbQoK3s1OeRlr/WCevkZIZValv8PFZH0XY8mhidnj4FCf8XzNI ZeAoMoISPt8yOEER5IWWfdB6/KDx/2JlFw== X-Google-Smtp-Source: AK7set+5WEC8QnOFVycvZ3kfs9SPFhlUO2QRx+2UwJmPuJsJIHk7/802TaWwBCasj+Rv2Iy/oLipDg== X-Received: by 2002:a17:90b:4f88:b0:233:f909:366f with SMTP id qe8-20020a17090b4f8800b00233f909366fmr2426775pjb.33.1676458488390; Wed, 15 Feb 2023 02:54:48 -0800 (PST) Received: from bilid.c.googlers.com.com (56.4.82.34.bc.googleusercontent.com. [34.82.4.56]) by smtp.gmail.com with ESMTPSA id k22-20020a17090a591600b0022c0a05229fsm1153995pji.41.2023.02.15.02.54.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Feb 2023 02:54:47 -0800 (PST) From: Bili Dong To: yipeng1.wang@intel.com Cc: dev@dpdk.org, cristian.dumitrescu@intel.com, Bili Dong Subject: [PATCH v2] hash: add XOR32 hash function Date: Wed, 15 Feb 2023 10:54:42 +0000 Message-Id: <20230215105442.3878441-1-qobilidop@gmail.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog In-Reply-To: <20230215103041.3861350-1-qobilidop@gmail.com> References: <20230215103041.3861350-1-qobilidop@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org An XOR32 hash is needed in the Software Switch (SWX) Pipeline for its use case in P4. We implement it in this patch so it could be easily registered in the pipeline later. Signed-off-by: Bili Dong --- .mailmap | 1 + app/test/test_hash_functions.c | 33 ++++++++++++++++-- lib/hash/rte_hash_xor.h | 64 ++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 lib/hash/rte_hash_xor.h diff --git a/.mailmap b/.mailmap index 5015494210..176dd93b66 100644 --- a/.mailmap +++ b/.mailmap @@ -159,6 +159,7 @@ Bernard Iremonger Bert van Leeuwen Bhagyada Modali Bharat Mota +Bili Dong Bill Hong Billy McFall Billy O'Mahony diff --git a/app/test/test_hash_functions.c b/app/test/test_hash_functions.c index 76d51b6e71..14d69d90c4 100644 --- a/app/test/test_hash_functions.c +++ b/app/test/test_hash_functions.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "test.h" @@ -22,8 +23,8 @@ * Hash values calculated for key sizes from array "hashtest_key_lens" * and for initial values from array "hashtest_initvals. * Each key will be formed by increasing each byte by 1: - * e.g.: key size = 4, key = 0x03020100 - * key size = 8, key = 0x0706050403020100 + * e.g.: key size = 4, key = 0x00010203 + * key size = 8, key = 0x0001020304050607 */ static uint32_t hash_values_jhash[2][12] = {{ 0x8ba9414b, 0xdf0d39c9, @@ -51,6 +52,19 @@ static uint32_t hash_values_crc[2][12] = {{ 0x789c104f, 0x53028d3e } }; +static uint32_t hash_values_xor[2][12] = {{ + 0x00000000, 0x00010000, + 0x00010203, 0x04040404, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x0c040404, 0x000d0e0f, + 0x04212223, 0x04040404 +}, +{ + 0xdeadbeef, 0xdeacbeef, + 0xdeacbcec, 0xdaa9baeb, 0xdeadbeef, 0xdeadbeef, + 0xdeadbeef, 0xdeadbeef, 0xd2a9baeb, 0xdea0b0e0, + 0xda8c9ccc, 0xdaa9baeb +} +}; /******************************************************************************* * Hash function performance test configuration section. Each performance test @@ -61,7 +75,7 @@ static uint32_t hash_values_crc[2][12] = {{ */ #define HASHTEST_ITERATIONS 1000000 #define MAX_KEYSIZE 64 -static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc}; +static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc, rte_hash_xor}; static uint32_t hashtest_initvals[] = {0, 0xdeadbeef}; static uint32_t hashtest_key_lens[] = { 1, 2, /* Unusual key sizes */ @@ -85,6 +99,9 @@ get_hash_name(rte_hash_function f) if (f == rte_hash_crc) return "rte_hash_crc"; + if (f == rte_hash_xor) + return "rte_hash_xor"; + return "UnknownHash"; } @@ -173,6 +190,16 @@ verify_precalculated_hash_func_tests(void) hash_values_crc[j][i], hash); return -1; } + + hash = rte_hash_xor(key, hashtest_key_lens[i], + hashtest_initvals[j]); + if (hash != hash_values_xor[j][i]) { + printf("XOR for %u bytes with initial value 0x%x." + "Expected 0x%x, but got 0x%x\n", + hashtest_key_lens[i], hashtest_initvals[j], + hash_values_xor[j][i], hash); + return -1; + } } } diff --git a/lib/hash/rte_hash_xor.h b/lib/hash/rte_hash_xor.h new file mode 100644 index 0000000000..8420fe997b --- /dev/null +++ b/lib/hash/rte_hash_xor.h @@ -0,0 +1,64 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Intel Corporation + */ + +#ifndef _RTE_HASH_XOR_H_ +#define _RTE_HASH_XOR_H_ + +/** + * @file + * + * RTE XOR Hash + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#include + +#define LEFT8b_MASK rte_cpu_to_be_32(0xff000000) +#define LEFT16b_MASK rte_cpu_to_be_32(0xffff0000) + +/** + * Calculate XOR32 hash on user-supplied byte array. + * + * @param data + * Data to perform hash on. + * @param data_len + * How many bytes to use to calculate hash value. + * @param init_val + * Value to initialise hash generator. + * @return + * 32bit calculated hash value. + */ +static inline uint32_t +rte_hash_xor(const void *data, uint32_t data_len, uint32_t init_val) +{ + uintptr_t pd = (uintptr_t) data; + init_val = rte_cpu_to_be_32(init_val); + + for (uint32_t i = 0; i < data_len / 4; i++) { + init_val ^= *(const uint32_t *)pd; + pd += 4; + } + + if (data_len & 0x2) { + init_val ^= *(const uint32_t *)pd & LEFT16b_MASK; + pd += 2; + } + + if (data_len & 0x1) + init_val ^= *(const uint32_t *)pd & LEFT8b_MASK; + + init_val = rte_be_to_cpu_32(init_val); + return init_val; +} + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_HASH_XOR_H_ */ -- 2.34.1