Re: [PATCH 2/3] MINOR: Add `ha_generate_uuid_v7`

2024-04-25 Thread Willy Tarreau
On Thu, Apr 25, 2024 at 08:15:30PM +0200, Tim Düsterhus wrote:
> Hi
> 
> On 4/24/24 08:39, Willy Tarreau wrote:
> > Just thinking about all the shifts above, I think you could have
> > gone through less efforts by acting on 64-bit randoms (less shifts).
> > But the difference is probably not that much anyway.
> 
> I've used the existing implementation for UUIDv4 as the basis, that's why
> the code looks like it does.

Ah OK makes sense ;-)

Willy



Re: [PATCH 2/3] MINOR: Add `ha_generate_uuid_v7`

2024-04-25 Thread Tim Düsterhus

Hi

On 4/24/24 08:39, Willy Tarreau wrote:

Just thinking about all the shifts above, I think you could have
gone through less efforts by acting on 64-bit randoms (less shifts).
But the difference is probably not that much anyway.


I've used the existing implementation for UUIDv4 as the basis, that's 
why the code looks like it does.


Best regards
Tim Düsterhus



Re: [PATCH 2/3] MINOR: Add `ha_generate_uuid_v7`

2024-04-24 Thread Willy Tarreau
Hi Tim!

On Fri, Apr 19, 2024 at 09:01:26PM +0200, Tim Duesterhus wrote:
> +/* Generates a draft-ietf-uuidrev-rfc4122bis-14 version 7 UUID into chunk
> + *  which must be at least 37 bytes large.
> + */
> +void ha_generate_uuid_v7(struct buffer *output)
> +{
> + uint32_t rnd[3];
> + uint64_t last;
> + uint64_t time;
> +
> + time = (date.tv_sec * 1000) + (date.tv_usec / 1000);
> + last = ha_random64();
> + rnd[0] = last;
> + rnd[1] = last >> 32;
> +
> + last = ha_random64();
> + rnd[2] = last;
> +
> + chunk_printf(output, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx",
> +  (uint)(time >> 16u),
> +  (uint)(time & 0x),
> +  ((rnd[0] >> 16u) & 0xFFF) | 0x7000,  // highest 4 bits 
> indicate the uuid version
> +  (rnd[1] & 0x3FFF) | 0x8000,  // the highest 2 bits 
> indicate the UUID variant (10),
> +  (long long)((rnd[1] >> 14u) | ((uint64_t) rnd[2] << 18u)) 
> & 0xull);
> +}

Just thinking about all the shifts above, I think you could have
gone through less efforts by acting on 64-bit randoms (less shifts).
But the difference is probably not that much anyway.

In any case, that looks good and I've merged it now. Many thanks!
Willy



[PATCH 2/3] MINOR: Add `ha_generate_uuid_v7`

2024-04-19 Thread Tim Duesterhus
This function generates a version 7 UUID as per
draft-ietf-uuidrev-rfc4122bis-14.
---
 include/haproxy/tools.h |  1 +
 src/tools.c | 25 +
 2 files changed, 26 insertions(+)

diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h
index cbce218dc6..4e1a6dfa3f 100644
--- a/include/haproxy/tools.h
+++ b/include/haproxy/tools.h
@@ -1057,6 +1057,7 @@ int parse_dotted_uints(const char *s, unsigned int 
**nums, size_t *sz);
 
 /* PRNG */
 void ha_generate_uuid_v4(struct buffer *output);
+void ha_generate_uuid_v7(struct buffer *output);
 void ha_random_seed(const unsigned char *seed, size_t len);
 void ha_random_jump96(uint32_t dist);
 uint64_t ha_random64(void);
diff --git a/src/tools.c b/src/tools.c
index f3b095569a..114cbf6ddf 100644
--- a/src/tools.c
+++ b/src/tools.c
@@ -5605,6 +5605,31 @@ void ha_generate_uuid_v4(struct buffer *output)
 (long long)((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) 
& 0xull);
 }
 
+/* Generates a draft-ietf-uuidrev-rfc4122bis-14 version 7 UUID into chunk
+ *  which must be at least 37 bytes large.
+ */
+void ha_generate_uuid_v7(struct buffer *output)
+{
+   uint32_t rnd[3];
+   uint64_t last;
+   uint64_t time;
+
+   time = (date.tv_sec * 1000) + (date.tv_usec / 1000);
+   last = ha_random64();
+   rnd[0] = last;
+   rnd[1] = last >> 32;
+
+   last = ha_random64();
+   rnd[2] = last;
+
+   chunk_printf(output, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx",
+(uint)(time >> 16u),
+(uint)(time & 0x),
+((rnd[0] >> 16u) & 0xFFF) | 0x7000,  // highest 4 bits 
indicate the uuid version
+(rnd[1] & 0x3FFF) | 0x8000,  // the highest 2 bits 
indicate the UUID variant (10),
+(long long)((rnd[1] >> 14u) | ((uint64_t) rnd[2] << 18u)) 
& 0xull);
+}
+
 
 /* only used by parse_line() below. It supports writing in place provided that
  *  is updated to the next location before calling it. In that case, the
-- 
2.43.2