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=BGHq2fCG; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 1EC8B5A0785 for ; Fri, 27 Mar 2026 05:34:47 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1774586072; bh=3qCwL7lIcQnr2dR2b5DM3FKuBEU8HhEYW7UGQC0+kGA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BGHq2fCGFO0wTLfocOoAyKMF6wcQTueLrelzKwmL3+o4fJOiz4exvsw+CGHXpYzEE +SZdhCpFEbeiR1vP78/gZ0Z9nB2qlMsGqvpk2KKYxXWpO8K4IAELCfX5ChtXBrmI40 jsQXVbH+BAWRCjSVM8U37DXLofP4rCkU3eNsDkTxyURLlNAmxl+reF0SFLfivyiou2 Y4vRfpu1P0Wu4iL5zpsGqL8EnsFQOCFRsYxEBniQvMSc4BvDsTpu7KdlBZ8cMnqbRP 1I5nVgehm7Bbk7I/JFrgjq7hrauIoCBAjNarXwDhrlkHy1zFjGV5e3SZTkARnMNMbU V1e4DGZW3tHcg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4fhnqD6HxYz4wTF; Fri, 27 Mar 2026 15:34:32 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 17/18] bitmap: Split bitmap helper functions into their own module Date: Fri, 27 Mar 2026 15:34:29 +1100 Message-ID: <20260327043430.1785787-18-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260327043430.1785787-1-david@gibson.dropbear.id.au> References: <20260327043430.1785787-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: GKNGQNUKHODTGDXA3DKDCAEE7PAM6VO4 X-Message-ID-Hash: GKNGQNUKHODTGDXA3DKDCAEE7PAM6VO4 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: Currently bitmap functions are in util.[ch] along with a lot of other stuff. In preparation for sharing them with a configuration client, move these out into their own files. Signed-off-by: David Gibson --- Makefile | 17 +++++----- bitmap.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ bitmap.h | 24 ++++++++++++++ conf.c | 1 + fwd.h | 1 + util.c | 84 ----------------------------------------------- util.h | 10 ------ 7 files changed, 134 insertions(+), 102 deletions(-) create mode 100644 bitmap.c create mode 100644 bitmap.h diff --git a/Makefile b/Makefile index 5b6891d7..d6ced328 100644 --- a/Makefile +++ b/Makefile @@ -37,20 +37,21 @@ FLAGS += -DPAGE_SIZE=$(shell getconf PAGE_SIZE) FLAGS += -DVERSION=\"$(VERSION)\" FLAGS += -DDUAL_STACK_SOCKETS=$(DUAL_STACK_SOCKETS) -PASST_SRCS = arch.c arp.c checksum.c conf.c dhcp.c dhcpv6.c epoll_ctl.c \ - flow.c fwd.c icmp.c igmp.c inany.c iov.c ip.c isolation.c lineread.c \ - log.c mld.c ndp.c netlink.c migrate.c packet.c passt.c pasta.c pcap.c \ - pif.c repair.c serialise.c tap.c tcp.c tcp_buf.c tcp_splice.c tcp_vu.c \ - udp.c udp_flow.c udp_vu.c util.c vhost_user.c virtio.c vu_common.c +PASST_SRCS = arch.c arp.c bitmap.c checksum.c conf.c dhcp.c dhcpv6.c \ + epoll_ctl.c flow.c fwd.c icmp.c igmp.c inany.c iov.c ip.c isolation.c \ + lineread.c log.c mld.c ndp.c netlink.c migrate.c packet.c passt.c \ + pasta.c pcap.c pif.c repair.c serialise.c tap.c tcp.c tcp_buf.c \ + tcp_splice.c tcp_vu.c udp.c udp_flow.c udp_vu.c util.c vhost_user.c \ + virtio.c vu_common.c QRAP_SRCS = qrap.c PASST_REPAIR_SRCS = passt-repair.c SRCS = $(PASST_SRCS) $(QRAP_SRCS) $(PASST_REPAIR_SRCS) MANPAGES = passt.1 pasta.1 qrap.1 passt-repair.1 -PASST_HEADERS = arch.h arp.h checksum.h conf.h dhcp.h dhcpv6.h epoll_ctl.h \ - flow.h fwd.h flow_table.h icmp.h icmp_flow.h inany.h iov.h ip.h \ - isolation.h lineread.h log.h migrate.h ndp.h netlink.h packet.h \ +PASST_HEADERS = arch.h arp.h bitmap.h checksum.h conf.h dhcp.h dhcpv6.h \ + epoll_ctl.h flow.h fwd.h flow_table.h icmp.h icmp_flow.h inany.h iov.h \ + ip.h isolation.h lineread.h log.h migrate.h ndp.h netlink.h packet.h \ passt.h pasta.h pcap.h pif.h repair.h serialise.h siphash.h tap.h tcp.h \ tcp_buf.h tcp_conn.h tcp_internal.h tcp_splice.h tcp_vu.h udp.h \ udp_flow.h udp_internal.h udp_vu.h util.h vhost_user.h virtio.h \ diff --git a/bitmap.c b/bitmap.c new file mode 100644 index 00000000..b3b204b2 --- /dev/null +++ b/bitmap.c @@ -0,0 +1,99 @@ +// 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 + * + * bitmap.c - bitmap handling + * + * Copyright Red Hat + * Author: Stefano Brivio + */ + +#include "bitmap.h" + +/** + * bitmap_set() - Set single bit in bitmap + * @map: Pointer to bitmap + * @bit: Bit number to set + */ +void bitmap_set(uint8_t *map, unsigned bit) +{ + unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit); + + *word |= BITMAP_BIT(bit); +} + +/** + * bitmap_clear() - Clear single bit in bitmap + * @map: Pointer to bitmap + * @bit: Bit number to clear + */ +/* cppcheck-suppress unusedFunction */ +void bitmap_clear(uint8_t *map, unsigned bit) +{ + unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit); + + *word &= ~BITMAP_BIT(bit); +} + +/** + * bitmap_isset() - Check for set bit in bitmap + * @map: Pointer to bitmap + * @bit: Bit number to check + * + * Return: true if given bit is set, false if it's not + */ +bool bitmap_isset(const uint8_t *map, unsigned bit) +{ + const unsigned long *word + = (const unsigned long *)map + BITMAP_WORD(bit); + + return !!(*word & BITMAP_BIT(bit)); +} + +/** + * bitmap_or() - Logical disjunction (OR) of two bitmaps + * @dst: Pointer to result bitmap + * @size: Size of bitmaps, in bytes + * @a: First operand + * @b: Second operand + */ +/* cppcheck-suppress unusedFunction */ +void bitmap_or(uint8_t *dst, size_t size, const uint8_t *a, const uint8_t *b) +{ + unsigned long *dw = (unsigned long *)dst; + unsigned long *aw = (unsigned long *)a; + unsigned long *bw = (unsigned long *)b; + size_t i; + + for (i = 0; i < size / sizeof(long); i++, dw++, aw++, bw++) + *dw = *aw | *bw; + + for (i = size / sizeof(long) * sizeof(long); i < size; i++) + dst[i] = a[i] | b[i]; +} + +/** + * bitmap_and_not() - Logical conjunction with complement (AND NOT) of bitmap + * @dst: Pointer to result bitmap + * @size: Size of bitmaps, in bytes + * @a: First operand + * @b: Second operand + */ +void bitmap_and_not(uint8_t *dst, size_t size, + const uint8_t *a, const uint8_t *b) +{ + unsigned long *dw = (unsigned long *)dst; + unsigned long *aw = (unsigned long *)a; + unsigned long *bw = (unsigned long *)b; + size_t i; + + for (i = 0; i < size / sizeof(long); i++, dw++, aw++, bw++) + *dw = *aw & ~*bw; + + for (i = size / sizeof(long) * sizeof(long); i < size; i++) + dst[i] = a[i] & ~b[i]; +} diff --git a/bitmap.h b/bitmap.h new file mode 100644 index 00000000..b6e91c02 --- /dev/null +++ b/bitmap.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later + * Copyright Red Hat + * Author: Stefano Brivio + */ + +#ifndef BITMAP_H +#define BITMAP_H + +#include +#include +#include + +#define BIT(n) (1UL << (n)) +#define BITMAP_BIT(n) (BIT((n) % (sizeof(long) * 8))) +#define BITMAP_WORD(n) (n / (sizeof(long) * 8)) + +void bitmap_set(uint8_t *map, unsigned bit); +void bitmap_clear(uint8_t *map, unsigned bit); +bool bitmap_isset(const uint8_t *map, unsigned bit); +void bitmap_or(uint8_t *dst, size_t size, const uint8_t *a, const uint8_t *b); +void bitmap_and_not(uint8_t *dst, size_t size, + const uint8_t *a, const uint8_t *b); + +#endif /* BITMAP_H */ diff --git a/conf.c b/conf.c index 5639ef03..ae37bf96 100644 --- a/conf.c +++ b/conf.c @@ -36,6 +36,7 @@ #include #include "util.h" +#include "bitmap.h" #include "ip.h" #include "passt.h" #include "netlink.h" diff --git a/fwd.h b/fwd.h index f111e139..beba0bf5 100644 --- a/fwd.h +++ b/fwd.h @@ -14,6 +14,7 @@ #include +#include "bitmap.h" #include "inany.h" struct flowside; diff --git a/util.c b/util.c index faa2c6a4..73c9d51d 100644 --- a/util.c +++ b/util.c @@ -367,90 +367,6 @@ long timespec_diff_ms(const struct timespec *a, const struct timespec *b) return timespec_diff_us(a, b) / 1000; } -/** - * bitmap_set() - Set single bit in bitmap - * @map: Pointer to bitmap - * @bit: Bit number to set - */ -void bitmap_set(uint8_t *map, unsigned bit) -{ - unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit); - - *word |= BITMAP_BIT(bit); -} - -/** - * bitmap_clear() - Clear single bit in bitmap - * @map: Pointer to bitmap - * @bit: Bit number to clear - */ -/* cppcheck-suppress unusedFunction */ -void bitmap_clear(uint8_t *map, unsigned bit) -{ - unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit); - - *word &= ~BITMAP_BIT(bit); -} - -/** - * bitmap_isset() - Check for set bit in bitmap - * @map: Pointer to bitmap - * @bit: Bit number to check - * - * Return: true if given bit is set, false if it's not - */ -bool bitmap_isset(const uint8_t *map, unsigned bit) -{ - const unsigned long *word - = (const unsigned long *)map + BITMAP_WORD(bit); - - return !!(*word & BITMAP_BIT(bit)); -} - -/** - * bitmap_or() - Logical disjunction (OR) of two bitmaps - * @dst: Pointer to result bitmap - * @size: Size of bitmaps, in bytes - * @a: First operand - * @b: Second operand - */ -/* cppcheck-suppress unusedFunction */ -void bitmap_or(uint8_t *dst, size_t size, const uint8_t *a, const uint8_t *b) -{ - unsigned long *dw = (unsigned long *)dst; - unsigned long *aw = (unsigned long *)a; - unsigned long *bw = (unsigned long *)b; - size_t i; - - for (i = 0; i < size / sizeof(long); i++, dw++, aw++, bw++) - *dw = *aw | *bw; - - for (i = size / sizeof(long) * sizeof(long); i < size; i++) - dst[i] = a[i] | b[i]; -} - -/** - * bitmap_and_not() - Logical conjunction with complement (AND NOT) of bitmap - * @dst: Pointer to result bitmap - * @size: Size of bitmaps, in bytes - * @a: First operand - * @b: Second operand - */ -void bitmap_and_not(uint8_t *dst, size_t size, - const uint8_t *a, const uint8_t *b) -{ - unsigned long *dw = (unsigned long *)dst; - unsigned long *aw = (unsigned long *)a; - unsigned long *bw = (unsigned long *)b; - size_t i; - - for (i = 0; i < size / sizeof(long); i++, dw++, aw++, bw++) - *dw = *aw & ~*bw; - - for (i = size / sizeof(long) * sizeof(long); i < size; i++) - dst[i] = a[i] & ~b[i]; -} - /** * ns_enter() - Enter configured user (unless already joined) and network ns * @c: Execution context diff --git a/util.h b/util.h index cb669105..92aeabc8 100644 --- a/util.h +++ b/util.h @@ -50,10 +50,6 @@ #define MAX_FROM_BITS(n) (((1U << (n)) - 1)) -#define BIT(n) (1UL << (n)) -#define BITMAP_BIT(n) (BIT((n) % (sizeof(long) * 8))) -#define BITMAP_WORD(n) (n / (sizeof(long) * 8)) - #define SWAP(a, b) \ do { \ __typeof__(a) __x = (a); (a) = (b); (b) = __x; \ @@ -228,12 +224,6 @@ int sock_unix(char *sock_path); void sock_probe_features(struct ctx *c); long timespec_diff_ms(const struct timespec *a, const struct timespec *b); int64_t timespec_diff_us(const struct timespec *a, const struct timespec *b); -void bitmap_set(uint8_t *map, unsigned bit); -void bitmap_clear(uint8_t *map, unsigned bit); -bool bitmap_isset(const uint8_t *map, unsigned bit); -void bitmap_or(uint8_t *dst, size_t size, const uint8_t *a, const uint8_t *b); -void bitmap_and_not(uint8_t *dst, size_t size, - const uint8_t *a, const uint8_t *b); char *line_read(char *buf, size_t len, int fd); void ns_enter(const struct ctx *c); bool ns_is_init(void); -- 2.53.0