From patchwork Tue Apr 9 17:05:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 139215 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 9FCC343E2C; Tue, 9 Apr 2024 19:06:53 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id E4DBC4067B; Tue, 9 Apr 2024 19:06:47 +0200 (CEST) Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by mails.dpdk.org (Postfix) with ESMTP id E83154025D for ; Tue, 9 Apr 2024 19:06:06 +0200 (CEST) Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-6ed0938cd1dso3722683b3a.2 for ; Tue, 09 Apr 2024 10:06:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1712682366; x=1713287166; 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=E9YPCoYF/LY/FI4ixXLjxwdDHr40FFq/HZiODrp8OAg=; b=Ur8p4OBGVsWFexk5yfsfj4yf+CzDFk2BuH8SJylQMtQgXBx00n2ikRaR+NVdcMzVOh U3ifDHar58ljvq6ovQXK0GHLUPbVAG9FI76j3OHfLbq3mw/MHTeqCK8LqhxrVoKG9dqL 0dZW0/a/vEIMTRDvuG1dIAGycw3TkOm4qYWsXYs1ZyJ+orLIU4G0nvSjVuqzX0BCZ+Fl enhQC2BZvYWKy2q5HqEB1/Yu49NKbqswY+ClOG7nKnjgB2FvFPjfPVJB+AlMr+ZbZA0J ULf/WYVOdOdpaQdmeykfEfqx1mWjl2QSZQt9QGoh8lBhditNbvl/qS6522Cfm+c1Q5x+ EUjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712682366; x=1713287166; 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=E9YPCoYF/LY/FI4ixXLjxwdDHr40FFq/HZiODrp8OAg=; b=VJyQeHCm/LAG9k1StngS1Byame2/c8g4fbTCL53hJDnoH1nIavw6FBvP8O8QBOeQ0z 0i8OwnfyZY2UmFL09JeCfE7k/JBymZ6Y0NP8ZO3g0v0DnFSKmhaLB4HxRxjSBmBYamR2 r2Nn+SqFkAl6bTphw2fJfaNHMIvTA7qKdMo2CbPiYAl2mkI3fy+Jnj3SKx1yWiQtWQsP bBNzpzqi4DiwYJFmjPcxNA1QcCHRVC2CTHQj4qDKn1KrbiTk4bmEa8oxVu6ldfSRmLPT G5RZeK7KMPqO9X+RSXI8tQPqqGyM1eUrhAX0eHXXnQuCrQC6+m36vTyYClVnIuQcYqA+ Qgjw== X-Gm-Message-State: AOJu0YwxcQKNV1iCZU/oG1bSStO8xn6roeR9q+a5VXCvuTdRNpsgLc7K LZhu20aR8j4cXm5UPvK+ioxQgA6FGMsQhHi4cGp0Nv9FQnrygoheg/qHDhrz0cTeVvkWPb5AJdQ E X-Google-Smtp-Source: AGHT+IHnBpRxUJPqi6+ZtzajcKM99MD86c9q/3zXMvkohnsg3KT1EW4rjlcUMZIUSesOmbG+Av+0Rw== X-Received: by 2002:a05:6a00:218f:b0:6ed:e1c:102e with SMTP id h15-20020a056a00218f00b006ed0e1c102emr139082pfi.24.1712682366038; Tue, 09 Apr 2024 10:06:06 -0700 (PDT) Received: from hermes.lan (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id x25-20020aa784d9000000b006e6b52eb59asm8545673pfn.126.2024.04.09.10.06.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 10:06:05 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v4 1/2] eal: add functions to generate uuid values Date: Tue, 9 Apr 2024 10:05:02 -0700 Message-ID: <20240409170602.541658-2-stephen@networkplumber.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240409170602.541658-1-stephen@networkplumber.org> References: <20240403163432.437275-1-stephen@networkplumber.org> <20240409170602.541658-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 | 26 +++++++++++++++ lib/eal/version.map | 4 +++ 3 files changed, 86 insertions(+) diff --git a/lib/eal/common/eal_common_uuid.c b/lib/eal/common/eal_common_uuid.c index 0a80bfbb38..a185ccb881 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 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..5fe7d32d39 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 */ @@ -100,6 +102,30 @@ int rte_uuid_parse(const char *in, rte_uuid_t uu); */ 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..aca0d97c63 100644 --- a/lib/eal/version.map +++ b/lib/eal/version.map @@ -396,6 +396,10 @@ EXPERIMENTAL { # added in 24.03 rte_vfio_get_device_info; # WINDOWS_NO_EXPORT + + # added in 24.07 + rte_uuid_generate_random; + rte_uuid_generate_time; }; INTERNAL {