From patchwork Wed Apr 3 22:11:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 139091 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 9285D43DF1; Thu, 4 Apr 2024 00:13:37 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 32059402EA; Thu, 4 Apr 2024 00:13:34 +0200 (CEST) Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by mails.dpdk.org (Postfix) with ESMTP id A86404027B for ; Thu, 4 Apr 2024 00:13:31 +0200 (CEST) Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2a2d159e4ffso39854a91.1 for ; Wed, 03 Apr 2024 15:13:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1712182411; x=1712787211; darn=dpdk.org; 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=Gc9056QOjuVYfhf4bhMSmsFklkfUlX/P5ar793xxAkA=; b=bCHBX8o+9oSWOvE2B9WIz4cJ5kfg1vJXt4hYdMp/ButYBFwIVfSVIX4urGyV04vxdY UHirR3pqv3NqBy3HOQUpmPS4qni9Uklgit8m7a6I6cME6hiZPoWzcK+shWCip0hz+hmS XZh2+BQ6ENH3lPZ5kHpqXLDOtrhiSpI4tDpJLGCbvsq0RU4iqsFjHfjEMTXgVSPjEpL5 8BeRcfTwVGrwDoiVQPkkCPDvYQqLPoqujWIePYEQs2j+bl8KdM1537wFbO3IribVWb69 9UDT/V5Yinfx1ZgaHM7txIxICRXW7B5yNJoRhTdHsCjl3Ms88DJgYZHV8+fVLI1tk+S5 Zi7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712182411; x=1712787211; 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=Gc9056QOjuVYfhf4bhMSmsFklkfUlX/P5ar793xxAkA=; b=FLVi6p0ascVdhcG1ng+RcTOm2dOzdHDWQcVyKWyKIq/PBp0c0MeQyI1TRRkMitygrP EGXAgZ+2yZrjwXQRNHP8IGJZTKPO8bayAA6p6r0iq2jjIUv/6AYLMGNl+mUvjmEV5FBm mZdyWKcs9HC5Ih07fmZ/N51+4C0SgCEdGRhvRvnUnzDeKnxJNCgMX0HjY0rta4b21M3e 9LmcK+YEaIf/0AkAjGlQ1p31qU3/4Rh2P8A+a8xx4ynbqf7nBJbCxyvKqihUZOQP7hRS G8LwYjG8mtLAx9Ex3L2smGbw3vF3XKwRJybH5X1/Z5VjkHxGNdizMPZtScUICG7rSsj1 5VRg== X-Gm-Message-State: AOJu0YyTNUYID0x1mb+WXpoB7BuIA2nsJdejUCheE/RerzaueJTRgRBw cuXYv7I9BxIs6FSVt+Pry1IDBo/3gWWkUmYLXLyMcKNuw/klc+waAc8z05h6zJEucDDLmAAZKeU XJAtMbQ== X-Google-Smtp-Source: AGHT+IGoirssC7iClxVb+RifLd1NZhYoSAjDhtpsFuw56IT3JvxDTOg64PfajUqsmynbZt82kk6zjQ== X-Received: by 2002:a17:90b:4c12:b0:2a2:7c53:7d1f with SMTP id na18-20020a17090b4c1200b002a27c537d1fmr5060241pjb.17.1712182410914; Wed, 03 Apr 2024 15:13:30 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id 1-20020a17090a0f0100b002a261d1da0dsm224218pjy.24.2024.04.03.15.13.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 15:13:30 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v2 1/2] eal: add functions to generate uuid values Date: Wed, 3 Apr 2024 15:11:07 -0700 Message-ID: <20240403221319.499014-2-stephen@networkplumber.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240403221319.499014-1-stephen@networkplumber.org> References: <20240403163432.437275-1-stephen@networkplumber.org> <20240403221319.499014-1-stephen@networkplumber.org> MIME-Version: 1.0 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 Useful to be able to generate uuid values for tests or for interaction with other subsystems as magic cookie. Naming and overall algorithm come from libuuid which is used by permission of original author. Signed-off-by: Stephen Hemminger --- lib/eal/common/eal_common_uuid.c | 56 ++++++++++++++++++++++++++++++++ lib/eal/include/rte_uuid.h | 22 ++++++++++++- lib/eal/version.map | 2 ++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/lib/eal/common/eal_common_uuid.c b/lib/eal/common/eal_common_uuid.c index 0a80bfbb38..fc8f58e8a4 100644 --- a/lib/eal/common/eal_common_uuid.c +++ b/lib/eal/common/eal_common_uuid.c @@ -7,7 +7,12 @@ #include #include #include +#include +#include +#include +#include +#include #include /* UUID packed form */ @@ -165,3 +170,54 @@ void rte_uuid_unparse(const rte_uuid_t uu, char *out, size_t len) uuid.node[0], uuid.node[1], uuid.node[2], uuid.node[3], uuid.node[4], uuid.node[5]); } + +void rte_uuid_generate_random(rte_uuid_t out) +{ + union { + uint64_t words[2]; + rte_uuid_t uuid; + } buf; + struct uuid uu; + + /* UUID is 128 bit */ + buf.words[0] = rte_rand(); + buf.words[1] = rte_rand(); + + /* Mark these random bytes a version 4 random uuid */ + uuid_unpack(buf.uuid, &uu); + uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000; + uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000; + uuid_pack(&uu, out); +} + +void rte_uuid_generate_time(rte_uuid_t out) +{ + struct uuid uu; + struct timespec ts; + uint64_t ns, rnd; + static uint16_t sequence; + + /* The time value for UUID is 100ns since 15 October 1582 */ + clock_gettime(CLOCK_REALTIME, &ts); + + ns = ts.tv_nsec / 100; + ns += (uint64_t) ts.tv_sec * (NSEC_PER_SEC / 100); + ns += (((uint64_t) 0x01B21DD2) << 32) + 0x13814000; + + uu.time_low = (uint32_t) ns; + uu.time_mid = (uint16_t) (ns >> 32); + uu.time_hi_and_version = (uint16_t) (ns >> 48); + uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000; + uu.clock_seq = rte_atomic_fetch_add_explicit(&sequence, 1, + rte_memory_order_relaxed); + + rnd = rte_rand(); + memcpy(uu.node, &rnd, 6); + /* + * What libuuid does set multicast bit. + * This avoids conflicts with network cards. + */ + uu.node[0] |= 0x1; + + uuid_pack(&uu, out); +} diff --git a/lib/eal/include/rte_uuid.h b/lib/eal/include/rte_uuid.h index cfefd4308a..052b78a812 100644 --- a/lib/eal/include/rte_uuid.h +++ b/lib/eal/include/rte_uuid.h @@ -18,6 +18,8 @@ extern "C" { #include #include +#include + /** * Struct describing a Universal Unique Identifier */ @@ -94,12 +96,30 @@ int rte_uuid_parse(const char *in, rte_uuid_t uu); * @param uu * UUID to format * @param out - * Resulting string buffer + * Resulting string bufferm * @param len * Sizeof the available string buffer */ void rte_uuid_unparse(const rte_uuid_t uu, char *out, size_t len); +/** + * Generate a random uuid + * + * @param uu + * Resulting UUID + */ +__rte_experimental +void rte_uuid_generate_random(rte_uuid_t uu); + +/** + * Generate a uuid based on time stamp. + * + * @param uu + * Resulting UUID + */ +__rte_experimental +void rte_uuid_generate_time(rte_uuid_t uu); + #ifdef __cplusplus } #endif diff --git a/lib/eal/version.map b/lib/eal/version.map index 3df50c3fbb..5a8aa67244 100644 --- a/lib/eal/version.map +++ b/lib/eal/version.map @@ -396,6 +396,8 @@ EXPERIMENTAL { # added in 24.03 rte_vfio_get_device_info; # WINDOWS_NO_EXPORT + rte_uuid_generate_random; + rte_uuid_generate_time; }; INTERNAL { From patchwork Wed Apr 3 22:11:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 139092 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id A7F2043DF1; Thu, 4 Apr 2024 00:13:43 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 68C9C402F1; Thu, 4 Apr 2024 00:13:35 +0200 (CEST) Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by mails.dpdk.org (Postfix) with ESMTP id 99FD7402E3 for ; Thu, 4 Apr 2024 00:13:32 +0200 (CEST) Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-2a074187a42so234923a91.0 for ; Wed, 03 Apr 2024 15:13:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1712182412; x=1712787212; darn=dpdk.org; 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=Q+LQEw2nT/iZn580xWJEOTpT6bMAqfC/BtHS/Kzliig=; b=mQ0AJwifxaD2dNZD5IqEEu/ooTVN/UUt5Iy0uTf4HALVZ9AXrEyv6MubSII3lxe2Ed PYuZvfJPyKGJSkC2Es79bubffn3IpSFQz4dPqu1aYy2QOVC8QX0m7mUuqOzWWdySLYoC ngFyGoFjZaGvAAqQ4Nf9g1vbfkWoQ8eBsXTYSH1z+B3r70ZGcGKzdZYu7pYumW2h7rP0 TLOWksyBTTWsC1QAIwvMPXQMQGfcxAPVRQG3ruI/Mo+LH2mnq1F4MzchCZsx7Sl9LsZK EPnJQi9HKPjZqiJORFRM0tDX1PzWXT4q8Cbz8VR7vzBE6fOJraFbHyJWs4P/zFEoh9P6 oPQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712182412; x=1712787212; 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=Q+LQEw2nT/iZn580xWJEOTpT6bMAqfC/BtHS/Kzliig=; b=KDWuGme718lx2AH10H1ESQb7iEafLCfRPkD5kDum5cYv2guPHuG0BSR6zsnaK8aLtl Fdfc2FCrE+A2bKWpx9zT6l8GO4vO5tvHgoE1K+GVAc0EEo4C5dCl0DNYwIuszKy75wbK ORLey3moSkkdS3JjRAv6xudMht9OibZlMmB4E8vREUC/IPJ3rJPzbI3ATdWyEinIKNxL bJWkZihuwbh4BpYvpI/wU92fHiPxSTODEYo07J644gAcR8RPlOaK6Hf5LSQAaYbd8I+q qdXfvqOklLD6eo0EYDmhNywEKO0Ul4QDSFTYBLGdUm0F0hVsBZSdsvFMIs3Pi0ZV9hPm uKEQ== X-Gm-Message-State: AOJu0Yz1eK4YqLNwjW2T4kJvXMpgxhUyuWi7/hUaurcHgScvpeK8uWC2 /awUP5hBscdjwqM3+h9XxeYaPXmoOqjX6KyY9o3RyZYwjVw1TpYr0Ds0IckoHOUWhr4xW6366fz Bx84TOQ== X-Google-Smtp-Source: AGHT+IFTz/IXAXDhojfq7996pVlZX/8IlUkjGMRQP1iuL5IpUtcABwXYXVtDHRggxMJWIwpT3Dc0tQ== X-Received: by 2002:a17:90a:f417:b0:29b:b163:181a with SMTP id ch23-20020a17090af41700b0029bb163181amr809629pjb.42.1712182411708; Wed, 03 Apr 2024 15:13:31 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id 1-20020a17090a0f0100b002a261d1da0dsm224218pjy.24.2024.04.03.15.13.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 15:13:31 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v2 2/2] test: add functional test for uuid Date: Wed, 3 Apr 2024 15:11:08 -0700 Message-ID: <20240403221319.499014-3-stephen@networkplumber.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240403221319.499014-1-stephen@networkplumber.org> References: <20240403163432.437275-1-stephen@networkplumber.org> <20240403221319.499014-1-stephen@networkplumber.org> MIME-Version: 1.0 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 The uuid functions in EAL were not covered by existing functional tests. Signed-off-by: Stephen Hemminger Acked-by: Tyler Retzlaff --- app/test/meson.build | 1 + app/test/test_uuid.c | 135 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 app/test/test_uuid.c diff --git a/app/test/meson.build b/app/test/meson.build index 7d909039ae..11fb7dbffa 100644 --- a/app/test/meson.build +++ b/app/test/meson.build @@ -198,6 +198,7 @@ source_file_deps = { 'test_trace.c': [], 'test_trace_perf.c': [], 'test_trace_register.c': [], + 'test_uuid.c' : [], 'test_vdev.c': ['kvargs', 'bus_vdev'], 'test_version.c': [], } diff --git a/app/test/test_uuid.c b/app/test/test_uuid.c new file mode 100644 index 0000000000..669ed82243 --- /dev/null +++ b/app/test/test_uuid.c @@ -0,0 +1,135 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2024 Stephen Hemminger + */ + +#include + +#include "test.h" + +#define NROUNDS 100 + +static int +check_duplicate_uuid(rte_uuid_t uids[]) +{ + int i, j; + char out[256]; + + for (i = 0; i < NROUNDS - 1; i++) { + for (j = i + 1; j < NROUNDS; j++) { + if (rte_uuid_compare(uids[i], uids[j]) == 0) { + rte_uuid_unparse(uids[i], out, sizeof(out)); + printf("Generated duplicate random uuids %d == %d : %s\n", + i, j, out); + return TEST_FAILED; + } + } + } + return TEST_SUCCESS; +} + + +static const struct uuid_case { + const char *in; + rte_uuid_t result; +} good_cases[] = { + { "02ce8e94-5b84-47fc-8f86-72633c5b5061", RTE_UUID_INIT(0x02ce8e94, 0x5b84, 0x47fc, 0x8f86, 0x72633c5b5061) }, + { "2c72af87-d220-4931-98ec-45c8520c94e1", RTE_UUID_INIT(0x2c72af87, 0xd220, 0x4931, 0x98ec, 0x45c8520c94e1) }, + { "441edca1-0942-4ccd-9b33-233e0454fe5b", RTE_UUID_INIT(0x441edca1, 0x0942, 0x4ccd, 0x9b33, 0x233e0454fe5b) }, +}; + +static const char * const bad_cases[] = { + "", /* empty */ + "41edca1-0942-4ccd-9b33-233e0454fe5b", /* too short */ + "d5cecbac-531b-4527-b0629-2bc3011dc9c", /* hyphen in wrong place */ + "db318745-1347-4f5e-m142-d86dc41172b2", /* bad hex */ +}; + +static int +test_uuid(void) +{ + rte_uuid_t uids[NROUNDS] = { }; + rte_uuid_t clone[NROUNDS] = { }; + unsigned int i; + int ret; + + /* Test generate random uuid */ + for (i = 0; i < NROUNDS; i++) { + if (!rte_uuid_is_null(uids[i])) { + printf("Zero'd uuid %d is not null\n", i); + return TEST_FAILED; + } + + rte_uuid_generate_random(uids[i]); + + if (rte_uuid_is_null(uids[i])) { + printf("Generated random uuid %d is null\n", i); + return TEST_FAILED; + } + } + + ret = check_duplicate_uuid(uids); + if (ret != TEST_SUCCESS) + return ret; + + /* Test generate time */ + for (i = 0; i < NROUNDS; i++) { + rte_uuid_generate_time(uids[i]); + + if (rte_uuid_is_null(uids[i])) { + printf("Generated random uuid %d is null\n", i); + return TEST_FAILED; + } + } + + + /* Test that copy works */ + for (i = 0; i < NROUNDS; i++) { + if (!rte_uuid_is_null(clone[i])) { + printf("Zero'd clone %d is not null\n", i); + return TEST_FAILED; + } + + rte_uuid_copy(clone[i], uids[i]); + + if (rte_uuid_compare(uids[i], clone[i]) != 0) { + printf("Copied uuid does not match\n"); + return TEST_FAILED; + } + } + + for (i = 0; i < RTE_DIM(good_cases); i++) { + const struct uuid_case *c = &good_cases[i]; + char out[37]; + rte_uuid_t uu; + + if (rte_uuid_parse(c->in, uu) != 0) { + printf("Failed to parse '%s'\n", c->in); + return TEST_FAILED; + } + if (rte_uuid_compare(uu, c->result) != 0) { + printf("Parse mismatch for '%s'\n", c->in); + return TEST_FAILED; + } + + rte_uuid_unparse(uu, out, sizeof(out)); + if (strcmp(out, c->in) != 0) { + printf("Parse/unparse mismatch (%s != %s)\n", + out, c->in); + return TEST_FAILED; + } + } + + for (i = 0; i < RTE_DIM(bad_cases); i++) { + const char *s = bad_cases[i]; + rte_uuid_t uu; + + if (rte_uuid_parse(s, uu) == 0) { + printf("Accepted parse of '%s'\n", s); + return TEST_FAILED; + } + } + return TEST_SUCCESS; +} + + +REGISTER_FAST_TEST(uuid_autotest, true, true, test_uuid);