From patchwork Thu Apr 4 16:22:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 139101 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 219C043DFB; Thu, 4 Apr 2024 18:24:46 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 6B154406B6; Thu, 4 Apr 2024 18:24:41 +0200 (CEST) Received: from mail-oa1-f45.google.com (mail-oa1-f45.google.com [209.85.160.45]) by mails.dpdk.org (Postfix) with ESMTP id 0A0D6402BC for ; Thu, 4 Apr 2024 18:24:39 +0200 (CEST) Received: by mail-oa1-f45.google.com with SMTP id 586e51a60fabf-22a1e72f683so627664fac.0 for ; Thu, 04 Apr 2024 09:24:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1712247878; x=1712852678; 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=lEyohggaCjIiTNrDHIAb6B0hEkoz2H9eJQ/85fF3I1s=; b=SYmBFDzqwU7E3brJcoHO/fUitppw47A3NPakoNo2S1FCfAWFu0CuZqFGJUH/LlZItQ CbuMproSz+uggYd6Px2fv1ilz7a9GXmw4AAYdQVSj0ubNREk6ODhr3hvvlZfTp9Mu70A 89P4fKWKWHdKxo/HAHCzsib+B5ClEh7pPKD0GAbMamLiT82y0YlNBIbG95Zt/jGgu5QD NHDG6NJnkKmYD0688XMV0bXNIccO21U9vz8fh9mBrYaSJ3sP+GrnJ+ESIB5ZhsYxlNM0 5ONidLAlpojHf1Jqn8VCfpA9MRPN5FdlXe/daHYcnrIjzsQwXjRc9qGjZbVVC9Q/YPWc 677A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712247878; x=1712852678; 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=lEyohggaCjIiTNrDHIAb6B0hEkoz2H9eJQ/85fF3I1s=; b=BsH2Qrv8ZW1bvAwEtMc1hk9VoD1YKt8bxCcAQ8wZyyLT01kMB6o9fWJYIrlu4eD/2G 6QJHz9hWJRH4abegoRvJq6OHurMgOe37VC+5ERQ2tVafag+17kLCIxnys82vXxXX4Dob YZcz/bqCQ5bmOdTWpgdEew6WB8LIM3B27S/OAhmyDNt0Za+sTLxtpt1SXKmqIJcTeyT+ g3wsIutylmq6wPyy3UC+9Haz0QvdIsZOJVFBE/TaC670N1Ha66BnTF0XfyATBzEaFlcK aSI+22FJtv/CbLrRZF0/RTTtx2Ok++Lc+Wa90I8uaT7O/2JCy8xJIg6vtIGk6hjKQ0WN lBNQ== X-Gm-Message-State: AOJu0Yy4WaprJZrBgcqfQWN1ejut5PnPfeffFKJAkI4Qbwom72DNq0iG yk4ad+DwvRnjAI2X6s0L2AOGQYvedUR8Qxw8YfosGSD2Jh70b/pARdfMihBrAelSeHYPeHCG0nD /XG0= X-Google-Smtp-Source: AGHT+IGUQNvRwkQACVXQvDRJZDfDpupAwWSZVJ/qOYuze8H6vouo2Mad2Nt+1enS1pEN+YV4qouqhQ== X-Received: by 2002:a05:6870:fb9e:b0:220:a0b0:6d4a with SMTP id kv30-20020a056870fb9e00b00220a0b06d4amr2966112oab.57.1712247878262; Thu, 04 Apr 2024 09:24:38 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id k196-20020a633dcd000000b005ceeeea1816sm13784188pga.77.2024.04.04.09.24.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 09:24:37 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v3 1/2] eal: add functions to generate uuid values Date: Thu, 4 Apr 2024 09:22:13 -0700 Message-ID: <20240404162422.61510-2-stephen@networkplumber.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240404162422.61510-1-stephen@networkplumber.org> References: <20240403163432.437275-1-stephen@networkplumber.org> <20240404162422.61510-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 | 28 +++++++++++++++- lib/eal/version.map | 2 ++ 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/lib/eal/common/eal_common_uuid.c b/lib/eal/common/eal_common_uuid.c index 0a80bfbb38..dfc7c75813 100644 --- a/lib/eal/common/eal_common_uuid.c +++ b/lib/eal/common/eal_common_uuid.c @@ -3,12 +3,18 @@ */ #include +#include #include #include #include #include +#include +#include +#include +#include #include +#include /* UUID packed form */ struct uuid { @@ -165,3 +171,53 @@ 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 RTE_ATOMIC(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); + /* + * Follow the convention so that random node part will not + * not match a valid MAC address, + */ + 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..928a8312d9 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,36 @@ 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 + * + * Uses high quality pseudo-random generator rte_rand() to generate a universal + * unique identifier. + * + * @param uu + * Resulting UUID + */ +__rte_experimental +void rte_uuid_generate_random(rte_uuid_t uu); + +/** + * Generate a uuid based on time stamp. + * + * Uses the current time and a random Ethernet MAC address to generate a + * universal unique identifier. + * + * @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 {