DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH v4 1/2] eal: add functions to generate uuid values
Date: Tue,  9 Apr 2024 10:05:02 -0700	[thread overview]
Message-ID: <20240409170602.541658-2-stephen@networkplumber.org> (raw)
In-Reply-To: <20240409170602.541658-1-stephen@networkplumber.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 <stephen@networkplumber.org>
---
 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 <stdio.h>
+#include <stdbool.h>
 #include <string.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <ctype.h>
+#include <time.h>
 
+#include <rte_stdatomic.h>
+#include <rte_random.h>
+#include <rte_time.h>
 #include <rte_uuid.h>
+#include <rte_os_shim.h>
 
 /* 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 <stddef.h>
 #include <string.h>
 
+#include <rte_compat.h>
+
 /**
  * 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 {
-- 
2.43.0


  reply	other threads:[~2024-04-09 17:06 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-03 16:32 [PATCH 0/2] uuid: enhancements and tests Stephen Hemminger
2024-04-03 16:32 ` [PATCH 1/2] eal: add functions to generate uuid values Stephen Hemminger
2024-04-04 16:11   ` Tyler Retzlaff
2024-04-03 16:32 ` [PATCH 2/2] test: add functional test for uuid Stephen Hemminger
2024-04-03 22:11 ` [PATCH v2 0/2] uuid: add generate functions and tests Stephen Hemminger
2024-04-03 22:11   ` [PATCH v2 1/2] eal: add functions to generate uuid values Stephen Hemminger
2024-04-04 16:16     ` Tyler Retzlaff
2024-04-03 22:11   ` [PATCH v2 2/2] test: add functional test for uuid Stephen Hemminger
2024-04-04 16:18     ` Tyler Retzlaff
2024-04-04 16:22 ` [PATCH v3 0/2] uuid: add generate functions and tests Stephen Hemminger
2024-04-04 16:22   ` [PATCH v3 1/2] eal: add functions to generate uuid values Stephen Hemminger
2024-04-04 16:22   ` [PATCH v3 2/2] test: add functional test for uuid Stephen Hemminger
2024-04-05 16:53 ` [PATCH v4 00/30] replace use of rte_memcpy with fixed sizes Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 01/30] cocci/rte_memcpy: add script to eliminate fixed size rte_memcpy Stephen Hemminger
2024-04-06  9:01     ` Morten Brørup
2024-04-05 16:53   ` [PATCH v4 02/30] eal: replace use of " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 03/30] ethdev: replace uses of rte_memcpy Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 04/30] eventdev: replace use of fixed size rte_memcpy Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 05/30] cryptodev: " Stephen Hemminger
2024-04-10 15:40     ` [EXTERNAL] " Akhil Goyal
2024-04-05 16:53   ` [PATCH v4 06/30] ip_frag: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 07/30] net: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 08/30] lpm: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 09/30] node: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 10/30] pdcp: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 11/30] pipeline: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 12/30] rib: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 13/30] security: " Stephen Hemminger
2024-04-10 15:40     ` [EXTERNAL] " Akhil Goyal
2024-04-05 16:53   ` [PATCH v4 14/30] bus: remove unneeded rte_memcpy.h include Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 15/30] net: replace use of fixed size rte_memcpy Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 16/30] raw: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 17/30] baseband: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 18/30] common: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 19/30] crypto: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 20/30] " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 21/30] event: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 22/30] mempool: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 23/30] ml/cnxk: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 24/30] app/test-pmd: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 25/30] app/graph: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 26/30] app/test-eventdev: " Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 27/30] app/test: " Stephen Hemminger
2024-04-10 18:28     ` [EXTERNAL] " Akhil Goyal
2024-04-05 16:53   ` [PATCH v4 28/30] app/test-pipeline: remove unused rte_memcpy.h include Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 29/30] app/test-bbdev: remove unnecessary include of rte_memcpy.h Stephen Hemminger
2024-04-05 16:53   ` [PATCH v4 30/30] examples: replace use of fixed size rte_memcpy Stephen Hemminger
2024-04-09 17:05 ` [PATCH v4 0/2] uuid: generator functions and unit test Stephen Hemminger
2024-04-09 17:05   ` Stephen Hemminger [this message]
2024-04-09 17:05   ` [PATCH v4 2/2] test: add functional test for uuid Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240409170602.541658-2-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).