public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: passt-dev@passt.top
Subject: [PATCH 09/22] tcp, udp, util: Fixes for bitmap handling on big-endian, casts
Date: Fri, 28 Jan 2022 19:33:44 +0100	[thread overview]
Message-ID: <20220128183357.3407606-10-sbrivio@redhat.com> (raw)
In-Reply-To: <20220128183357.3407606-1-sbrivio@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 3222 bytes --]

Bitmap manipulating functions would otherwise refer to inconsistent
sets of bits on big-endian architectures. While at it, fix up a
couple of casts.

Signed-off-by: Stefano Brivio <sbrivio(a)redhat.com>
---
 passt.h |  3 ++-
 tcp.c   |  2 +-
 udp.c   |  4 ++--
 util.c  | 12 +++++++++---
 util.h  |  2 ++
 5 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/passt.h b/passt.h
index ae3035f..0ef1897 100644
--- a/passt.h
+++ b/passt.h
@@ -67,7 +67,8 @@ extern char pkt_buf		[PKT_BUF_BYTES];
 
 extern char *ip_proto_str[];
 #define IP_PROTO_STR(n)							\
-	(((n) <= IPPROTO_SCTP && ip_proto_str[(n)]) ? ip_proto_str[(n)] : "?")
+	(((uint8_t)(n) <= IPPROTO_SCTP && ip_proto_str[(n)]) ?		\
+			  ip_proto_str[(n)] : "?")
 
 #include <resolv.h>	/* For MAXNS below */
 
diff --git a/tcp.c b/tcp.c
index 839bf30..18f07b6 100644
--- a/tcp.c
+++ b/tcp.c
@@ -2518,7 +2518,7 @@ eintr:
 	}
 
 
-	if (n < (seq_from_tap - conn->seq_from_tap)) {
+	if (n < (int)(seq_from_tap - conn->seq_from_tap)) {
 		partial_send = 1;
 		conn->seq_from_tap += n;
 		tcp_send_to_tap(c, conn, 0, now);
diff --git a/udp.c b/udp.c
index 15e0c96..e1a9ecb 100644
--- a/udp.c
+++ b/udp.c
@@ -684,7 +684,7 @@ void udp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events,
 		cur_mh->msg_iov = &udp6_l2_iov_tap[0];
 		msg_i = msglen = iov_in_msg = 0;
 
-		for (i = 0; i < n; i++) {
+		for (i = 0; i < (unsigned)n; i++) {
 			struct udp6_l2_buf_t *b = &udp6_l2_buf[i];
 			size_t ip_len, iov_len;
 
@@ -770,7 +770,7 @@ void udp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events,
 		cur_mh->msg_iov = &udp4_l2_iov_tap[0];
 		msg_i = msglen = iov_in_msg = 0;
 
-		for (i = 0; i < n; i++) {
+		for (i = 0; i < (unsigned)n; i++) {
 			struct udp4_l2_buf_t *b = &udp4_l2_buf[i];
 			size_t ip_len, iov_len;
 			in_addr_t s_addr;
diff --git a/util.c b/util.c
index 7a3ea51..46a539b 100644
--- a/util.c
+++ b/util.c
@@ -342,7 +342,9 @@ int timespec_diff_ms(struct timespec *a, struct timespec *b)
  */
 void bitmap_set(uint8_t *map, int bit)
 {
-	map[bit / 8] |= 1 << (bit % 8);
+	unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit);
+
+	*word |= BITMAP_BIT(bit);
 }
 
 /**
@@ -352,7 +354,9 @@ void bitmap_set(uint8_t *map, int bit)
  */
 void bitmap_clear(uint8_t *map, int bit)
 {
-	map[bit / 8] &= ~(1 << (bit % 8));
+	unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit);
+
+	*word &= ~BITMAP_BIT(bit);
 }
 
 /**
@@ -364,7 +368,9 @@ void bitmap_clear(uint8_t *map, int bit)
  */
 int bitmap_isset(const uint8_t *map, int bit)
 {
-	return map[bit / 8] & (1 << bit % 8);
+	unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit);
+
+	return *word & BITMAP_BIT(bit);
 }
 
 /**
diff --git a/util.h b/util.h
index 63cd8f9..800a91d 100644
--- a/util.h
+++ b/util.h
@@ -43,6 +43,8 @@ void debug(const char *format, ...);
 #define ROUND_DOWN(x, y)	((x) & ~((y) - 1))
 #define ROUND_UP(x, y)		(((x) + (y) - 1) & ~((y) - 1))
 
+#define BITMAP_BIT(n)		(1UL << (n) % (sizeof(long) * 8))
+#define BITMAP_WORD(n)		(n / (sizeof(long) * 8))
 
 #define SWAP(a, b)							\
 	do {								\
-- 
@@ -43,6 +43,8 @@ void debug(const char *format, ...);
 #define ROUND_DOWN(x, y)	((x) & ~((y) - 1))
 #define ROUND_UP(x, y)		(((x) + (y) - 1) & ~((y) - 1))
 
+#define BITMAP_BIT(n)		(1UL << (n) % (sizeof(long) * 8))
+#define BITMAP_WORD(n)		(n / (sizeof(long) * 8))
 
 #define SWAP(a, b)							\
 	do {								\
-- 
2.33.0


  parent reply	other threads:[~2022-01-28 18:33 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-28 18:33 [PATCH 00/22] Fixes for non-x86_64, older kernels/glibc, and some more Stefano Brivio
2022-01-28 18:33 ` [PATCH 01/22] tcp: Cover all usages of tcpi_snd_wnd with HAS_SND_WND Stefano Brivio
2022-01-28 18:33 ` [PATCH 02/22] tap, tcp: Fix two comparisons with different signedness reported by gcc 7 Stefano Brivio
2022-01-28 18:33 ` [PATCH 03/22] passt: Drop <linux/ipv6.h> include, carry own ipv6hdr and opt_hdr definitions Stefano Brivio
2022-01-28 18:33 ` [PATCH 04/22] Makefile, seccomp: Fix build for i386, ppc64, ppc64le Stefano Brivio
2022-01-28 18:33 ` [PATCH 05/22] util: Fall-back definitions for SECCOMP_RET_KILL_PROCESS, ETH_{MAX,MIN}_MTU Stefano Brivio
2022-01-28 18:33 ` [PATCH 06/22] seccomp: Introduce mechanism to allow per-arch syscalls Stefano Brivio
2022-01-28 18:33 ` [PATCH 07/22] tcp, netlink, HAS{BYTES_ACKED,MIN_RTT,GETRANDOM} and NETLINK_GET_STRICT_CHK Stefano Brivio
2022-01-28 18:33 ` [PATCH 08/22] conf, pasta: Explicitly pass CLONE_{NEWUSER,NEWNET} to setns() Stefano Brivio
2022-01-28 18:33 ` Stefano Brivio [this message]
2022-01-28 18:33 ` [PATCH 10/22] netlink: Fix swapped v4/v6-only flags in external interface detection Stefano Brivio
2022-01-28 18:33 ` [PATCH 11/22] pasta: Check for zero d_reclen returned by getdents64() syscall Stefano Brivio
2022-01-28 18:33 ` [PATCH 12/22] tcp: Don't round down MSS to >= 64KiB page size, but clamp it in any case Stefano Brivio
2022-01-28 18:33 ` [PATCH 13/22] seccomp: Add a number of alternate and per-arch syscalls Stefano Brivio
2022-01-28 18:33 ` [PATCH 14/22] demo/pasta: Don't wait for pasta to return to a prompt Stefano Brivio
2022-01-28 18:33 ` [PATCH 15/22] test/two_guests: Drop stray spaces after sleep directives Stefano Brivio
2022-01-28 18:33 ` [PATCH 16/22] perf/passt_udp: Lower failure throughput thresholds with big MTUs Stefano Brivio
2022-01-28 18:33 ` [PATCH 17/22] test/lib/setup: Don't rely on IFS to properly separate qemu arguments Stefano Brivio
2022-01-28 18:33 ` [PATCH 18/22] test/lib/video: Drop -preset ultrafast from ffmpeg arguments Stefano Brivio
2022-01-28 18:33 ` [PATCH 19/22] hooks/pre-push: Delete old versions, add -DGLIBC_NO_STATIC_NSS, disable legacy builds Stefano Brivio
2022-01-28 18:33 ` [PATCH 20/22] conf: Fix support for --stderr as short option (-e) Stefano Brivio
2022-01-28 18:33 ` [PATCH 21/22] README: Fix anchor for Performance section Stefano Brivio
2022-01-28 18:33 ` [PATCH 22/22] README: Fix link to IGMP/MLD proxy ticket Stefano Brivio

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=20220128183357.3407606-10-sbrivio@redhat.com \
    --to=sbrivio@redhat.com \
    --cc=passt-dev@passt.top \
    /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.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

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 IMAP folder(s).