From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202602 header.b=MpVmuYNw; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id D280F5A0657 for ; Tue, 21 Apr 2026 08:25:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1776752718; bh=vBvSAEosH9OteyIrEMfYKs0RaZyr9T3qW0tWJxS6xq4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MpVmuYNwCKKjkbsEAEFBgo6VD1tWhyGRQMIo9wrpiXbKQrIYup5dDzvCCW5XWJ2YS fCA87Gu/HYRxWwfaqeTuF1b2l+oyppOTmQxTZ8v37gtE9akfVu4QbWjK5nfUwMg6MA nw3pQDLGSwbFSfb3YZ3QMdobW4F1mazwbfVMv5+vIUGwbKYvi9+20sDh6HTPSlgbBR /n8K0FwBuhB0iPJWB4VSt/Er7wIJZeyqZ21M6c+IXz5zyUQiD0dJX2/WT6oPX2Uaem kHdIAwMN0sSbhaAIPcD9B5Geol449lMoBXAdyPdrCgNpeuyQI5RtVJkgrjaVn0DiOw peBlo0j/+nDcg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4g0C5V3H0pz4wJs; Tue, 21 Apr 2026 16:25:18 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v5 14/18] inany: Prepare inany.[ch] for sharing with pesto tool Date: Tue, 21 Apr 2026 16:25:12 +1000 Message-ID: <20260421062516.2601204-15-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260421062516.2601204-1-david@gibson.dropbear.id.au> References: <20260421062516.2601204-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 7UV3F22ULLWIELLKFDSLLDXCISRWU4K6 X-Message-ID-Hash: 7UV3F22ULLWIELLKFDSLLDXCISRWU4K6 X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: inany contains a number of helpful functions for dealing with addresses which might be IPv4 or IPv6. We're going to want to use that in pesto. For the most part inany doesn't depend on other passt/pasta internals, however it does depend on siphash.h, which pesto doesn't need. Move the single dependent function, inany_siphash_feed() to siphash.h, renaming to match. Use that include inany.[ch] into pesto as well as passt/pasta. While we're there reformat pesto.c's header comment to match the convention used in most other files. Signed-off-by: David Gibson --- common.h | 20 ++++++++++++++++++-- flow.c | 4 ++-- fwd.c | 2 +- fwd.h | 1 + fwd_rule.h | 1 + inany.c | 19 ++++++++++++++----- inany.h | 17 ++--------------- pif.h | 1 + siphash.h | 13 +++++++++++++ util.h | 16 ---------------- 10 files changed, 53 insertions(+), 41 deletions(-) diff --git a/common.h b/common.h index 4a167ae0..370cfa16 100644 --- a/common.h +++ b/common.h @@ -18,9 +18,27 @@ "This is free software: you are free to change and redistribute it.\n" \ "There is NO WARRANTY, to the extent permitted by law.\n\n" +#ifndef MIN +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) +#endif +#ifndef MAX +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) +#endif + +#define MAX_FROM_BITS(n) (((1U << (n)) - 1)) + /* FPRINTF() intentionally silences cert-err33-c clang-tidy warnings */ #define FPRINTF(f, ...) (void)fprintf(f, __VA_ARGS__) +#define ARRAY_SIZE(a) ((int)(sizeof(a) / sizeof((a)[0]))) + +#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) +#define DIV_ROUND_CLOSEST(n, d) (((n) + (d) / 2) / (d)) +#define ROUND_DOWN(x, y) ((x) & ~((y) - 1)) +#define ROUND_UP(x, y) (((x) + (y) - 1) & ~((y) - 1)) + +#define UINT16_STRLEN (sizeof("65535")) + /* * Starting from glibc 2.40.9000 and commit 25a5eb4010df ("string: strerror, * strsignal cannot use buffer after dlmopen (bug 32026)"), strerror() needs @@ -53,8 +71,6 @@ static inline const char *strerror_(int errnum) #define strerror(x) @ "Don't call strerror() directly, use strerror_() instead" -#define ARRAY_SIZE(a) ((int)(sizeof(a) / sizeof((a)[0]))) - #ifndef __bswap_constant_16 #define __bswap_constant_16(x) \ ((uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))) diff --git a/flow.c b/flow.c index 56a6c6d3..91f2b81f 100644 --- a/flow.c +++ b/flow.c @@ -680,8 +680,8 @@ static uint64_t flow_hash(const struct ctx *c, uint8_t proto, uint8_t pif, { struct siphash_state state = SIPHASH_INIT(c->hash_secret); - inany_siphash_feed(&state, &side->oaddr); - inany_siphash_feed(&state, &side->eaddr); + siphash_feed_inany(&state, &side->oaddr); + siphash_feed_inany(&state, &side->eaddr); return siphash_final(&state, 38, (uint64_t)proto << 40 | (uint64_t)pif << 32 | diff --git a/fwd.c b/fwd.c index 728a783c..8849cfcd 100644 --- a/fwd.c +++ b/fwd.c @@ -80,7 +80,7 @@ static size_t neigh_table_slot(const struct ctx *c, struct siphash_state st = SIPHASH_INIT(c->hash_secret); uint32_t i; - inany_siphash_feed(&st, key); + siphash_feed_inany(&st, key); i = siphash_final(&st, sizeof(*key), 0); return ((size_t)i) & (NEIGH_TABLE_SIZE - 1); diff --git a/fwd.h b/fwd.h index 8f845d09..ac247826 100644 --- a/fwd.h +++ b/fwd.h @@ -19,6 +19,7 @@ #include "fwd_rule.h" struct flowside; +struct ctx; #define FWD_NO_HINT (-1) diff --git a/fwd_rule.h b/fwd_rule.h index 58551382..f51f1b4b 100644 --- a/fwd_rule.h +++ b/fwd_rule.h @@ -13,6 +13,7 @@ #include #include +#include "common.h" #include "ip.h" #include "inany.h" #include "bitmap.h" diff --git a/inany.c b/inany.c index 2a586ed1..23faf3ff 100644 --- a/inany.c +++ b/inany.c @@ -1,9 +1,19 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later - * Copyright Red Hat - * Author: David Gibson +// SPDX-License-Identifier: GPL-2.0-or-later + +/* PASST - Plug A Simple Socket Transport + * for qemu/UNIX domain socket mode + * + * PASTA - Pack A Subtle Tap Abstraction + * for network namespace/tap device mode + * + * PESTO - Programmable Extensible Socket Translation Orchestrator + * front-end for passt(1) and pasta(1) forwarding configuration * * inany.c - Types and helpers for handling addresses which could be * IPv6 or IPv4 (encoded as IPv4-mapped IPv6 addresses) + * + * Copyright Red Hat + * Author: David Gibson */ #include @@ -13,9 +23,8 @@ #include #include -#include "util.h" +#include "common.h" #include "ip.h" -#include "siphash.h" #include "inany.h" #include "fwd.h" diff --git a/inany.h b/inany.h index 1f7741d1..73385b91 100644 --- a/inany.h +++ b/inany.h @@ -11,13 +11,11 @@ #include #include +#include #include -#include "util.h" +#include "common.h" #include "ip.h" -#include "siphash.h" - -struct siphash_state; /** union inany_addr - Represents either an IPv4 or IPv6 address * @a6: Address as an IPv6 address, may be IPv4-mapped @@ -301,17 +299,6 @@ static inline int inany_from_sockaddr(union inany_addr *dst, in_port_t *port, return -1; } -/** inany_siphash_feed- Fold IPv[46] address into an in-progress siphash - * @state: siphash state - * @aa: inany to hash - */ -static inline void inany_siphash_feed(struct siphash_state *state, - const union inany_addr *aa) -{ - siphash_feed(state, (uint64_t)aa->u32[0] << 32 | aa->u32[1]); - siphash_feed(state, (uint64_t)aa->u32[2] << 32 | aa->u32[3]); -} - #define INANY_ADDRSTRLEN MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) bool inany_matches(const union inany_addr *a, const union inany_addr *b); diff --git a/pif.h b/pif.h index d7708603..37409d57 100644 --- a/pif.h +++ b/pif.h @@ -16,6 +16,7 @@ union inany_addr; union sockaddr_inany; +struct ctx; /** * enum pif_type - Type of passt/pasta interface ("pif") diff --git a/siphash.h b/siphash.h index bbddcac0..313b8947 100644 --- a/siphash.h +++ b/siphash.h @@ -47,6 +47,8 @@ #include #include +#include "inany.h" + /** * struct siphash_state - Internal state of siphash calculation */ @@ -101,6 +103,17 @@ static inline void siphash_feed(struct siphash_state *state, uint64_t in) state->v[0] ^= in; } +/** siphash_feed_inany() - Fold IPv[46] address into an in-progress siphash + * @state: siphash state + * @aa: inany to hash + */ +static inline void siphash_feed_inany(struct siphash_state *state, + const union inany_addr *aa) +{ + siphash_feed(state, (uint64_t)aa->u32[0] << 32 | aa->u32[1]); + siphash_feed(state, (uint64_t)aa->u32[2] << 32 | aa->u32[3]); +} + /** * siphash_final() - Finalize SipHash calculations * @v: siphash state (4 x 64-bit integers) diff --git a/util.h b/util.h index dc14c78c..70aadeba 100644 --- a/util.h +++ b/util.h @@ -29,20 +29,6 @@ #define IP_MAX_MTU USHRT_MAX #endif -#ifndef MIN -#define MIN(x, y) (((x) < (y)) ? (x) : (y)) -#endif -#ifndef MAX -#define MAX(x, y) (((x) > (y)) ? (x) : (y)) -#endif - -#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) -#define DIV_ROUND_CLOSEST(n, d) (((n) + (d) / 2) / (d)) -#define ROUND_DOWN(x, y) ((x) & ~((y) - 1)) -#define ROUND_UP(x, y) (((x) + (y) - 1) & ~((y) - 1)) - -#define MAX_FROM_BITS(n) (((1U << (n)) - 1)) - #define SWAP(a, b) \ do { \ __typeof__(a) __x = (a); (a) = (b); (b) = __x; \ @@ -202,8 +188,6 @@ static inline const char *af_name(sa_family_t af) } } -#define UINT16_STRLEN (sizeof("65535")) - /* inet address (- '\0') + port (u16) (- '\0') + ':' + '\0' */ #define SOCKADDR_INET_STRLEN \ (INET_ADDRSTRLEN-1 + UINT16_STRLEN-1 + sizeof(":")) -- 2.53.0