From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 08/18] util: Introduce hton*_constant() in place of #ifdefs
Date: Fri, 9 Dec 2022 16:42:18 +1100 [thread overview]
Message-ID: <20221209054228.4085990-9-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20221209054228.4085990-1-david@gibson.dropbear.id.au>
We have several places where we have fairly ugly #ifdefs on __BYTE_ORDER
where we need network order values in a constant expression (so we can't
use htons() or htonl()). We can do this more cleanly by using a single
__BYTE_ORDER ifdef to define htons_constant() and htonl_constant()
macros, then using those in all the other places.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
dhcpv6.c | 50 +++++++++++++-------------------------------------
util.h | 37 +++++++++++--------------------------
2 files changed, 24 insertions(+), 63 deletions(-)
diff --git a/dhcpv6.c b/dhcpv6.c
index e763aed..d0e6c7f 100644
--- a/dhcpv6.c
+++ b/dhcpv6.c
@@ -39,37 +39,21 @@
*/
struct opt_hdr {
uint16_t t;
-#if __BYTE_ORDER == __BIG_ENDIAN
-# define OPT_CLIENTID 1
-# define OPT_SERVERID 2
-# define OPT_IA_NA 3
-# define OPT_IA_TA 4
-# define OPT_IAAADR 5
-# define OPT_STATUS_CODE 13
-# define STATUS_NOTONLINK 4
-# define OPT_DNS_SERVERS 23
-# define OPT_DNS_SEARCH 24
-#else
-# define OPT_CLIENTID __bswap_constant_16(1)
-# define OPT_SERVERID __bswap_constant_16(2)
-# define OPT_IA_NA __bswap_constant_16(3)
-# define OPT_IA_TA __bswap_constant_16(4)
-# define OPT_IAAADR __bswap_constant_16(5)
-# define OPT_STATUS_CODE __bswap_constant_16(13)
-# define STATUS_NOTONLINK __bswap_constant_16(4)
-# define OPT_DNS_SERVERS __bswap_constant_16(23)
-# define OPT_DNS_SEARCH __bswap_constant_16(24)
-#endif
+# define OPT_CLIENTID htons_constant(1)
+# define OPT_SERVERID htons_constant(2)
+# define OPT_IA_NA htons_constant(3)
+# define OPT_IA_TA htons_constant(4)
+# define OPT_IAAADR htons_constant(5)
+# define OPT_STATUS_CODE htons_constant(13)
+# define STATUS_NOTONLINK htons_constant(4)
+# define OPT_DNS_SERVERS htons_constant(23)
+# define OPT_DNS_SEARCH htons_constant(24)
#define STR_NOTONLINK "Prefix not appropriate for link."
uint16_t l;
} __attribute__((packed));
-#if __BYTE_ORDER == __BIG_ENDIAN
-# define OPT_SIZE_CONV(x) (x)
-#else
-# define OPT_SIZE_CONV(x) (__bswap_constant_16(x))
-#endif
+# define OPT_SIZE_CONV(x) (htons_constant(x))
#define OPT_SIZE(x) OPT_SIZE_CONV(sizeof(struct opt_##x) - \
sizeof(struct opt_hdr))
#define OPT_VSIZE(x) (sizeof(struct opt_##x) - \
@@ -103,19 +87,11 @@ struct opt_server_id {
uint8_t duid_lladdr[ETH_ALEN];
} __attribute__ ((packed));
-#if __BYTE_ORDER == __BIG_ENDIAN
-#define SERVER_ID { \
+#define SERVER_ID { \
{ OPT_SERVERID, OPT_SIZE(server_id) }, \
- DUID_TYPE_LLT, ARPHRD_ETHER, 0, { 0 } \
+ htons_constant(DUID_TYPE_LLT), \
+ htons_constant(ARPHRD_ETHER), 0, { 0 } \
}
-#else
-#define SERVER_ID { \
- { OPT_SERVERID, OPT_SIZE(server_id) }, \
- __bswap_constant_16(DUID_TYPE_LLT), \
- __bswap_constant_16(ARPHRD_ETHER), \
- 0, { 0 } \
-}
-#endif
/**
* struct opt_ia_na - Identity Association for Non-temporary Addresses Option
diff --git a/util.h b/util.h
index 3baec91..b4c139d 100644
--- a/util.h
+++ b/util.h
@@ -69,6 +69,14 @@
#define MAC_ZERO ((uint8_t [ETH_ALEN]){ 0 })
#define MAC_IS_ZERO(addr) (!memcmp((addr), MAC_ZERO, ETH_ALEN))
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define htons_constant(x) (x)
+#define htonl_constant(x) (x)
+#else
+#define htons_constant(x) (__bswap_constant_16(x))
+#define htonl_constant(x) (__bswap_constant_32(x))
+#endif
+
#define IN4_IS_ADDR_UNSPECIFIED(a) \
((a)->s_addr == htonl(INADDR_ANY))
#define IN4_IS_ADDR_BROADCAST(a) \
@@ -79,13 +87,8 @@
(IN_MULTICAST(ntohl((a)->s_addr)))
#define IN4_ARE_ADDR_EQUAL(a, b) \
(((struct in_addr *)(a))->s_addr == ((struct in_addr *)b)->s_addr)
-#if __BYTE_ORDER == __BIG_ENDIAN
-#define IN4ADDR_LOOPBACK_INIT \
- { .s_addr = INADDR_LOOPBACK }
-#else
#define IN4ADDR_LOOPBACK_INIT \
- { .s_addr = __bswap_constant_32(INADDR_LOOPBACK) }
-#endif
+ { .s_addr = htonl_constant(INADDR_LOOPBACK) }
#define NS_FN_STACK_SIZE (RLIMIT_STACK_VAL * 1024 / 8)
int do_clone(int (*fn)(void *), char *stack_area, size_t stack_size, int flags,
@@ -99,37 +102,19 @@ int do_clone(int (*fn)(void *), char *stack_area, size_t stack_size, int flags,
(void *)(arg)); \
} while (0)
-#if __BYTE_ORDER == __BIG_ENDIAN
-#define L2_BUF_ETH_IP4_INIT \
- { \
- .h_dest = { 0 }, \
- .h_source = { 0 }, \
- .h_proto = ETH_P_IP, \
- }
-#else
#define L2_BUF_ETH_IP4_INIT \
{ \
.h_dest = { 0 }, \
.h_source = { 0 }, \
- .h_proto = __bswap_constant_16(ETH_P_IP), \
+ .h_proto = htons_constant(ETH_P_IP), \
}
-#endif
-#if __BYTE_ORDER == __BIG_ENDIAN
-#define L2_BUF_ETH_IP6_INIT \
- { \
- .h_dest = { 0 }, \
- .h_source = { 0 }, \
- .h_proto = ETH_P_IPV6, \
- }
-#else
#define L2_BUF_ETH_IP6_INIT \
{ \
.h_dest = { 0 }, \
.h_source = { 0 }, \
- .h_proto = __bswap_constant_16(ETH_P_IPV6), \
+ .h_proto = htons_constant(ETH_P_IPV6), \
}
-#endif
#define L2_BUF_IP4_INIT(proto) \
{ \
--
@@ -69,6 +69,14 @@
#define MAC_ZERO ((uint8_t [ETH_ALEN]){ 0 })
#define MAC_IS_ZERO(addr) (!memcmp((addr), MAC_ZERO, ETH_ALEN))
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define htons_constant(x) (x)
+#define htonl_constant(x) (x)
+#else
+#define htons_constant(x) (__bswap_constant_16(x))
+#define htonl_constant(x) (__bswap_constant_32(x))
+#endif
+
#define IN4_IS_ADDR_UNSPECIFIED(a) \
((a)->s_addr == htonl(INADDR_ANY))
#define IN4_IS_ADDR_BROADCAST(a) \
@@ -79,13 +87,8 @@
(IN_MULTICAST(ntohl((a)->s_addr)))
#define IN4_ARE_ADDR_EQUAL(a, b) \
(((struct in_addr *)(a))->s_addr == ((struct in_addr *)b)->s_addr)
-#if __BYTE_ORDER == __BIG_ENDIAN
-#define IN4ADDR_LOOPBACK_INIT \
- { .s_addr = INADDR_LOOPBACK }
-#else
#define IN4ADDR_LOOPBACK_INIT \
- { .s_addr = __bswap_constant_32(INADDR_LOOPBACK) }
-#endif
+ { .s_addr = htonl_constant(INADDR_LOOPBACK) }
#define NS_FN_STACK_SIZE (RLIMIT_STACK_VAL * 1024 / 8)
int do_clone(int (*fn)(void *), char *stack_area, size_t stack_size, int flags,
@@ -99,37 +102,19 @@ int do_clone(int (*fn)(void *), char *stack_area, size_t stack_size, int flags,
(void *)(arg)); \
} while (0)
-#if __BYTE_ORDER == __BIG_ENDIAN
-#define L2_BUF_ETH_IP4_INIT \
- { \
- .h_dest = { 0 }, \
- .h_source = { 0 }, \
- .h_proto = ETH_P_IP, \
- }
-#else
#define L2_BUF_ETH_IP4_INIT \
{ \
.h_dest = { 0 }, \
.h_source = { 0 }, \
- .h_proto = __bswap_constant_16(ETH_P_IP), \
+ .h_proto = htons_constant(ETH_P_IP), \
}
-#endif
-#if __BYTE_ORDER == __BIG_ENDIAN
-#define L2_BUF_ETH_IP6_INIT \
- { \
- .h_dest = { 0 }, \
- .h_source = { 0 }, \
- .h_proto = ETH_P_IPV6, \
- }
-#else
#define L2_BUF_ETH_IP6_INIT \
{ \
.h_dest = { 0 }, \
.h_source = { 0 }, \
- .h_proto = __bswap_constant_16(ETH_P_IPV6), \
+ .h_proto = htons_constant(ETH_P_IPV6), \
}
-#endif
#define L2_BUF_IP4_INIT(proto) \
{ \
--
2.38.1
next prev parent reply other threads:[~2022-12-09 5:42 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-09 5:42 [PATCH v2 00/18] RFC: Unify and simplify tap send path David Gibson
2022-12-09 5:42 ` [PATCH v2 01/18] pcap: Introduce pcap_frame() helper David Gibson
2023-01-04 17:45 ` Stefano Brivio
2023-01-05 4:47 ` David Gibson
2022-12-09 5:42 ` [PATCH v2 02/18] pcap: Replace pcapm() with pcap_multiple() David Gibson
2022-12-09 5:42 ` [PATCH v2 03/18] tcp: Combine two parts of passt tap send path together David Gibson
2022-12-09 5:42 ` [PATCH v2 04/18] tcp: Don't keep compute total bytes in a message until we need it David Gibson
2023-01-04 17:45 ` Stefano Brivio
2023-01-05 4:48 ` David Gibson
2022-12-09 5:42 ` [PATCH v2 05/18] tcp: Improve interface to tcp_l2_buf_flush() David Gibson
2023-01-04 17:45 ` Stefano Brivio
2023-01-05 4:53 ` David Gibson
2022-12-09 5:42 ` [PATCH v2 06/18] tcp: Combine two parts of pasta tap send path together David Gibson
2022-12-09 5:42 ` [PATCH v2 07/18] tap, tcp: Move tap send path to tap.c David Gibson
2022-12-09 5:42 ` David Gibson [this message]
2022-12-09 5:42 ` [PATCH v2 09/18] tcp, udp: Use named field initializers in iov_init functions David Gibson
2022-12-09 5:42 ` [PATCH v2 10/18] util: Parameterize ethernet header initializer macro David Gibson
2022-12-09 5:42 ` [PATCH v2 11/18] tcp: Remove redundant and incorrect initialization from *_iov_init() David Gibson
2022-12-09 5:42 ` [PATCH v2 12/18] tcp: Consolidate calculation of total frame size David Gibson
2022-12-09 5:42 ` [PATCH v2 13/18] tap: Add "tap headers" abstraction David Gibson
2022-12-09 5:42 ` [PATCH v2 14/18] tcp: Use abstracted tap header David Gibson
2022-12-09 5:42 ` [PATCH v2 15/18] tap: Use different io vector bases depending on tap type David Gibson
2022-12-09 5:42 ` [PATCH v2 16/18] udp: Use abstracted tap header David Gibson
2022-12-09 5:42 ` [PATCH v2 17/18] udp: Use tap_send_frames() David Gibson
2023-01-04 17:45 ` Stefano Brivio
2023-01-05 4:54 ` David Gibson
2022-12-09 5:42 ` [PATCH v2 18/18] tap: Improve handling of partial frame sends David Gibson
2023-01-04 17:45 ` Stefano Brivio
2023-01-05 4:57 ` David Gibson
2023-01-04 17:45 ` [PATCH v2 00/18] RFC: Unify and simplify tap send path 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=20221209054228.4085990-9-david@gibson.dropbear.id.au \
--to=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/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).