From a2639383ece04c2fee3bbdda54dab66a640f6aa1 Mon Sep 17 00:00:00 2001 From: Amaury Denoyelle Date: Fri, 30 Sep 2022 17:47:04 +0200 Subject: [PATCH] CLEANUP: quic: remove duplicated varint code from xprt_quic.h There was some identical code between xprt_quic and quic_enc modules. This concerns helper on QUIC varint type. Keep only the version in quic_enc file : this should help to reduce dependency on xprt_quic module. Note that quic_max_int_by_size() has been removed and is replaced by the identical quic_max_int(). This should be backported up to 2.6. --- include/haproxy/quic_enc.h | 41 ++++++++++++++++++--- include/haproxy/xprt_quic.h | 71 +------------------------------------ 2 files changed, 37 insertions(+), 75 deletions(-) diff --git a/include/haproxy/quic_enc.h b/include/haproxy/quic_enc.h index 1fddab723..45c2b4b4b 100644 --- a/include/haproxy/quic_enc.h +++ b/include/haproxy/quic_enc.h @@ -88,10 +88,10 @@ static inline size_t quic_int_getsize(uint64_t val) } } -/* Returns the maximum integer which may be encoded with bytes */ -static inline uint64_t quic_max_int_by_size(int size) +/* Returns the maximum value of a QUIC variable-length integer with as size */ +static inline uint64_t quic_max_int(size_t sz) { - switch (size) { + switch (sz) { case 1: return QUIC_VARINT_1_BYTE_MAX; case 2: @@ -100,9 +100,9 @@ static inline uint64_t quic_max_int_by_size(int size) return QUIC_VARINT_4_BYTE_MAX; case 8: return QUIC_VARINT_8_BYTE_MAX; - default: - return 0; } + + return -1; } /* Decode a QUIC variable-length integer from buffer into . @@ -235,5 +235,36 @@ static inline int b_quic_enc_int(struct buffer *b, uint64_t val) return 1; } +static inline size_t quic_incint_size_diff(uint64_t val) +{ + switch (val) { + case QUIC_VARINT_1_BYTE_MAX: + return 1; + case QUIC_VARINT_2_BYTE_MAX: + return 2; + case QUIC_VARINT_4_BYTE_MAX: + return 4; + default: + return 0; + } +} + +/* Return the difference between the encoded length of and the encoded + * length of . + */ +static inline size_t quic_decint_size_diff(uint64_t val) +{ + switch (val) { + case QUIC_VARINT_1_BYTE_MAX + 1: + return 1; + case QUIC_VARINT_2_BYTE_MAX + 1: + return 2; + case QUIC_VARINT_4_BYTE_MAX + 1: + return 4; + default: + return 0; + } +} + #endif /* USE_QUIC */ #endif /* _HAPROXY_QUIC_ENC_H */ diff --git a/include/haproxy/xprt_quic.h b/include/haproxy/xprt_quic.h index 589923808..b38490c03 100644 --- a/include/haproxy/xprt_quic.h +++ b/include/haproxy/xprt_quic.h @@ -229,24 +229,6 @@ static inline void quic_pin_cid_to_tid(unsigned char *cid, int target_tid) cid[0] = cid[0] - (cid[0] % global.nbthread) + target_tid; } -/* The maximum size of a variable-length QUIC integer encoded with 1 byte */ -#define QUIC_VARINT_1_BYTE_MAX ((1UL << 6) - 1) -/* The maximum size of a variable-length QUIC integer encoded with 2 bytes */ -#define QUIC_VARINT_2_BYTE_MAX ((1UL << 14) - 1) -/* The maximum size of a variable-length QUIC integer encoded with 4 bytes */ -#define QUIC_VARINT_4_BYTE_MAX ((1UL << 30) - 1) -/* The maximum size of a variable-length QUIC integer encoded with 8 bytes */ -#define QUIC_VARINT_8_BYTE_MAX ((1ULL << 62) - 1) - -/* The maximum size of a variable-length QUIC integer */ -#define QUIC_VARINT_MAX_SIZE 8 - -/* The two most significant bits of byte #0 from a QUIC packet gives the 2 - * logarithm of the length of a variable length encoded integer. - */ -#define QUIC_VARINT_BYTE_0_BITMASK 0x3f -#define QUIC_VARINT_BYTE_0_SHIFT 6 - /* Return a 32-bits integer in from QUIC packet with as address. * Makes point to the data after this 32-bits value if succeeded. * Note that these 32-bits integers are network bytes ordered. @@ -282,57 +264,6 @@ static inline int quic_write_uint32(unsigned char **buf, return 1; } -/* Return the difference between the encoded length of and the encoded - * length of . - */ -static inline size_t quic_incint_size_diff(uint64_t val) -{ - switch (val) { - case QUIC_VARINT_1_BYTE_MAX: - return 1; - case QUIC_VARINT_2_BYTE_MAX: - return 2; - case QUIC_VARINT_4_BYTE_MAX: - return 4; - default: - return 0; - } -} - -/* Return the difference between the encoded length of and the encoded - * length of . - */ -static inline size_t quic_decint_size_diff(uint64_t val) -{ - switch (val) { - case QUIC_VARINT_1_BYTE_MAX + 1: - return 1; - case QUIC_VARINT_2_BYTE_MAX + 1: - return 2; - case QUIC_VARINT_4_BYTE_MAX + 1: - return 4; - default: - return 0; - } -} - - -/* Returns the maximum value of a QUIC variable-length integer with as size */ -static inline uint64_t quic_max_int(size_t sz) -{ - switch (sz) { - case 1: - return QUIC_VARINT_1_BYTE_MAX; - case 2: - return QUIC_VARINT_2_BYTE_MAX; - case 4: - return QUIC_VARINT_4_BYTE_MAX; - case 8: - return QUIC_VARINT_8_BYTE_MAX; - } - - return -1; -} /* Return the maximum number of bytes we must use to completely fill a * buffer with as size for a data field of bytes prefixed by its QUIC @@ -363,7 +294,7 @@ static inline size_t max_available_room(size_t sz, size_t *len_sz) * +---------------------------+-----------.... * <--------------------------------> */ - size_t max_int = quic_max_int_by_size(*len_sz); + size_t max_int = quic_max_int(*len_sz); if (max_int + *len_sz <= sz) ret = max_int;