From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stefano Brivio 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 Message-ID: <20220128183357.3407606-10-sbrivio@redhat.com> In-Reply-To: <20220128183357.3407606-1-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============4061283091863417793==" --===============4061283091863417793== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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 --- 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]; =20 extern char *ip_proto_str[]; #define IP_PROTO_STR(n) \ - (((n) <=3D IPPROTO_SCTP && ip_proto_str[(n)]) ? ip_proto_str[(n)] : "?") + (((uint8_t)(n) <=3D IPPROTO_SCTP && ip_proto_str[(n)]) ? \ + ip_proto_str[(n)] : "?") =20 #include /* For MAXNS below */ =20 diff --git a/tcp.c b/tcp.c index 839bf30..18f07b6 100644 --- a/tcp.c +++ b/tcp.c @@ -2518,7 +2518,7 @@ eintr: } =20 =20 - if (n < (seq_from_tap - conn->seq_from_tap)) { + if (n < (int)(seq_from_tap - conn->seq_from_tap)) { partial_send =3D 1; conn->seq_from_tap +=3D 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 =3D &udp6_l2_iov_tap[0]; msg_i =3D msglen =3D iov_in_msg =3D 0; =20 - for (i =3D 0; i < n; i++) { + for (i =3D 0; i < (unsigned)n; i++) { struct udp6_l2_buf_t *b =3D &udp6_l2_buf[i]; size_t ip_len, iov_len; =20 @@ -770,7 +770,7 @@ void udp_sock_handler(struct ctx *c, union epoll_ref ref,= uint32_t events, cur_mh->msg_iov =3D &udp4_l2_iov_tap[0]; msg_i =3D msglen =3D iov_in_msg =3D 0; =20 - for (i =3D 0; i < n; i++) { + for (i =3D 0; i < (unsigned)n; i++) { struct udp4_l2_buf_t *b =3D &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] |=3D 1 << (bit % 8); + unsigned long *word =3D (unsigned long *)map + BITMAP_WORD(bit); + + *word |=3D BITMAP_BIT(bit); } =20 /** @@ -352,7 +354,9 @@ void bitmap_set(uint8_t *map, int bit) */ void bitmap_clear(uint8_t *map, int bit) { - map[bit / 8] &=3D ~(1 << (bit % 8)); + unsigned long *word =3D (unsigned long *)map + BITMAP_WORD(bit); + + *word &=3D ~BITMAP_BIT(bit); } =20 /** @@ -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 =3D (unsigned long *)map + BITMAP_WORD(bit); + + return *word & BITMAP_BIT(bit); } =20 /** 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)) =20 +#define BITMAP_BIT(n) (1UL << (n) % (sizeof(long) * 8)) +#define BITMAP_WORD(n) (n / (sizeof(long) * 8)) =20 #define SWAP(a, b) \ do { \ --=20 2.33.0 --===============4061283091863417793==--