Skip to contentSkip to conversation

net: add support for htonb and ntohb

PATCH In review Andy Gospodarek 3 participants 3 messages Apr 1, 2010 – Apr 2, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: add support for htonb and ntohb

After my recent post to net-2.6 this week to accidentally run htons to a u8, it is clear to me we _must_ add some infrastructure to make sure single bytes are in the correct network and host order on big and little endian systems. Today seemed like the perfect day to post this.

This patch adds basic support for htonb and ntohb. Patches to add this in the entire networking tree in _every_ case where a single byte is accessed will be posted next week -- I'm almost done with them!

---

 byteorder/big_endian.h    |   30 ++++++++++++++++++++++++++
 byteorder/generic.h       |    6 +++++
 byteorder/little_endian.h |   30 ++++++++++++++++++++++++++
 swab.h                    |   53 ++++++++++++++++++++++++++++++++++++++++++++--
 types.h                   |    2 +
 5 files changed, 119 insertions(+), 2 deletions(-)
include/linux/byteorder/big_endian.h +30 -0
index 3c80fd7..87f1089 100644
@@ -15,30 +15,40 @@
 #define __constant_ntohl(x) ((__force __u32)(__be32)(x))
 #define __constant_htons(x) ((__force __be16)(__u16)(x))
 #define __constant_ntohs(x) ((__force __u16)(__be16)(x))
+#define __constant_htonb(x) ((__force __be8)(__u8)(x))
+#define __constant_ntohb(x) ((__force __u8)(__be8)(x))
 #define __constant_cpu_to_le64(x) ((__force __le64)___constant_swab64((x)))
 #define __constant_le64_to_cpu(x) ___constant_swab64((__force __u64)(__le64)(x))
 #define __constant_cpu_to_le32(x) ((__force __le32)___constant_swab32((x)))
 #define __constant_le32_to_cpu(x) ___constant_swab32((__force __u32)(__le32)(x))
 #define __constant_cpu_to_le16(x) ((__force __le16)___constant_swab16((x)))
 #define __constant_le16_to_cpu(x) ___constant_swab16((__force __u16)(__le16)(x))
+#define __constant_cpu_to_le8(x) ((__force __le8)___constant_swab8((x)))
+#define __constant_le8_to_cpu(x) ___constant_swab8((__force __u8)(__le8)(x))
 #define __constant_cpu_to_be64(x) ((__force __be64)(__u64)(x))
 #define __constant_be64_to_cpu(x) ((__force __u64)(__be64)(x))
 #define __constant_cpu_to_be32(x) ((__force __be32)(__u32)(x))
 #define __constant_be32_to_cpu(x) ((__force __u32)(__be32)(x))
 #define __constant_cpu_to_be16(x) ((__force __be16)(__u16)(x))
 #define __constant_be16_to_cpu(x) ((__force __u16)(__be16)(x))
+#define __constant_cpu_to_be8(x) ((__force __be8)(__u8)(x))
+#define __constant_be8_to_cpu(x) ((__force __u8)(__be8)(x))
 #define __cpu_to_le64(x) ((__force __le64)__swab64((x)))
 #define __le64_to_cpu(x) __swab64((__force __u64)(__le64)(x))
 #define __cpu_to_le32(x) ((__force __le32)__swab32((x)))
 #define __le32_to_cpu(x) __swab32((__force __u32)(__le32)(x))
 #define __cpu_to_le16(x) ((__force __le16)__swab16((x)))
 #define __le16_to_cpu(x) __swab16((__force __u16)(__le16)(x))
+#define __cpu_to_le8(x) ((__force __le8)__swab8((x)))
+#define __le8_to_cpu(x) __swab8((__force __u8)(__le8)(x))
 #define __cpu_to_be64(x) ((__force __be64)(__u64)(x))
 #define __be64_to_cpu(x) ((__force __u64)(__be64)(x))
 #define __cpu_to_be32(x) ((__force __be32)(__u32)(x))
 #define __be32_to_cpu(x) ((__force __u32)(__be32)(x))
 #define __cpu_to_be16(x) ((__force __be16)(__u16)(x))
 #define __be16_to_cpu(x) ((__force __u16)(__be16)(x))
+#define __cpu_to_be8(x) ((__force __be8)(__u8)(x))
+#define __be8_to_cpu(x) ((__force __u8)(__be8)(x))
 
 static inline __le64 __cpu_to_le64p(const __u64 *p)
 {
@@ -64,6 +74,14 @@ static inline __u16 __le16_to_cpup(const __le16 *p)
 {
 	return __swab16p((__u16 *)p);
 }
+static inline __le8 __cpu_to_le8p(const __u8 *p)
+{
+	return (__force __le8)__swab8p(p);
+}
+static inline __u8 __le8_to_cpup(const __le8 *p)
+{
+	return __swab8p((__u8 *)p);
+}
 static inline __be64 __cpu_to_be64p(const __u64 *p)
 {
 	return (__force __be64)*p;
@@ -88,18 +106,30 @@ static inline __u16 __be16_to_cpup(const __be16 *p)
 {
 	return (__force __u16)*p;
 }
+static inline __be8 __cpu_to_be8p(const __u8 *p)
+{
+	return (__force __be8)*p;
+}
+static inline __u8 __be8_to_cpup(const __be8 *p)
+{
+	return (__force __u8)*p;
+}
 #define __cpu_to_le64s(x) __swab64s((x))
 #define __le64_to_cpus(x) __swab64s((x))
 #define __cpu_to_le32s(x) __swab32s((x))
 #define __le32_to_cpus(x) __swab32s((x))
 #define __cpu_to_le16s(x) __swab16s((x))
 #define __le16_to_cpus(x) __swab16s((x))
+#define __cpu_to_le8s(x) __swab8s((x))
+#define __le8_to_cpus(x) __swab8s((x))
 #define __cpu_to_be64s(x) do { (void)(x); } while (0)
 #define __be64_to_cpus(x) do { (void)(x); } while (0)
 #define __cpu_to_be32s(x) do { (void)(x); } while (0)
 #define __be32_to_cpus(x) do { (void)(x); } while (0)
 #define __cpu_to_be16s(x) do { (void)(x); } while (0)
 #define __be16_to_cpus(x) do { (void)(x); } while (0)
+#define __cpu_to_be8s(x) do { (void)(x); } while (0)
+#define __be8_to_cpus(x) do { (void)(x); } while (0)
 
 #ifdef __KERNEL__
 #include <linux/byteorder/generic.h>
include/linux/byteorder/generic.h +6 -0
index 0846e6b..11c9f36 100644
@@ -127,18 +127,24 @@
 
 #undef ntohl
 #undef ntohs
+#undef ntohb
 #undef htonl
 #undef htons
+#undef htonb
 
 #define ___htonl(x) __cpu_to_be32(x)
 #define ___htons(x) __cpu_to_be16(x)
+#define ___htonb(x) __cpu_to_be8(x)
 #define ___ntohl(x) __be32_to_cpu(x)
 #define ___ntohs(x) __be16_to_cpu(x)
+#define ___ntohb(x) __be8_to_cpu(x)
 
 #define htonl(x) ___htonl(x)
 #define ntohl(x) ___ntohl(x)
 #define htons(x) ___htons(x)
 #define ntohs(x) ___ntohs(x)
+#define htonb(x) ___htonb(x)
+#define ntohb(x) ___ntohb(x)
 
 static inline void le16_add_cpu(__le16 *var, u16 val)
 {
include/linux/byteorder/little_endian.h +30 -0
index 83195fb..ef862db 100644
@@ -15,30 +15,40 @@
 #define __constant_ntohl(x) ___constant_swab32((__force __be32)(x))
 #define __constant_htons(x) ((__force __be16)___constant_swab16((x)))
 #define __constant_ntohs(x) ___constant_swab16((__force __be16)(x))
+#define __constant_htonb(x) ((__force __be8)___constant_swab8((x)))
+#define __constant_ntohb(x) ___constant_swab8((__force __be8)(x))
 #define __constant_cpu_to_le64(x) ((__force __le64)(__u64)(x))
 #define __constant_le64_to_cpu(x) ((__force __u64)(__le64)(x))
 #define __constant_cpu_to_le32(x) ((__force __le32)(__u32)(x))
 #define __constant_le32_to_cpu(x) ((__force __u32)(__le32)(x))
 #define __constant_cpu_to_le16(x) ((__force __le16)(__u16)(x))
 #define __constant_le16_to_cpu(x) ((__force __u16)(__le16)(x))
+#define __constant_cpu_to_le8(x) ((__force __le8)(__u8)(x))
+#define __constant_le8_to_cpu(x) ((__force __u8)(__le8)(x))
 #define __constant_cpu_to_be64(x) ((__force __be64)___constant_swab64((x)))
 #define __constant_be64_to_cpu(x) ___constant_swab64((__force __u64)(__be64)(x))
 #define __constant_cpu_to_be32(x) ((__force __be32)___constant_swab32((x)))
 #define __constant_be32_to_cpu(x) ___constant_swab32((__force __u32)(__be32)(x))
 #define __constant_cpu_to_be16(x) ((__force __be16)___constant_swab16((x)))
 #define __constant_be16_to_cpu(x) ___constant_swab16((__force __u16)(__be16)(x))
+#define __constant_cpu_to_be8(x) ((__force __be8)___constant_swab8((x)))
+#define __constant_be8_to_cpu(x) ___constant_swab8((__force __u8)(__be8)(x))
 #define __cpu_to_le64(x) ((__force __le64)(__u64)(x))
 #define __le64_to_cpu(x) ((__force __u64)(__le64)(x))
 #define __cpu_to_le32(x) ((__force __le32)(__u32)(x))
 #define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
 #define __cpu_to_le16(x) ((__force __le16)(__u16)(x))
 #define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
+#define __cpu_to_le8(x) ((__force __le8)(__u8)(x))
+#define __le8_to_cpu(x) ((__force __u8)(__le8)(x))
 #define __cpu_to_be64(x) ((__force __be64)__swab64((x)))
 #define __be64_to_cpu(x) __swab64((__force __u64)(__be64)(x))
 #define __cpu_to_be32(x) ((__force __be32)__swab32((x)))
 #define __be32_to_cpu(x) __swab32((__force __u32)(__be32)(x))
 #define __cpu_to_be16(x) ((__force __be16)__swab16((x)))
 #define __be16_to_cpu(x) __swab16((__force __u16)(__be16)(x))
+#define __cpu_to_be8(x) ((__force __be8)__swab8((x)))
+#define __be8_to_cpu(x) __swab8((__force __u8)(__be8)(x))
 
 static inline __le64 __cpu_to_le64p(const __u64 *p)
 {
@@ -64,6 +74,14 @@ static inline __u16 __le16_to_cpup(const __le16 *p)
 {
 	return (__force __u16)*p;
 }
+static inline __le8 __cpu_to_le8p(const __u8 *p)
+{
+	return (__force __le8)*p;
+}
+static inline __u8 __le8_to_cpup(const __le8 *p)
+{
+	return (__force __u8)*p;
+}
 static inline __be64 __cpu_to_be64p(const __u64 *p)
 {
 	return (__force __be64)__swab64p(p);
@@ -88,18 +106,30 @@ static inline __u16 __be16_to_cpup(const __be16 *p)
 {
 	return __swab16p((__u16 *)p);
 }
+static inline __be8 __cpu_to_be8p(const __u8 *p)
+{
+	return (__force __be8)__swab8p(p);
+}
+static inline __u8 __be8_to_cpup(const __be8 *p)
+{
+	return __swab8p((__u8 *)p);
+}
 #define __cpu_to_le64s(x) do { (void)(x); } while (0)
 #define __le64_to_cpus(x) do { (void)(x); } while (0)
 #define __cpu_to_le32s(x) do { (void)(x); } while (0)
 #define __le32_to_cpus(x) do { (void)(x); } while (0)
 #define __cpu_to_le16s(x) do { (void)(x); } while (0)
 #define __le16_to_cpus(x) do { (void)(x); } while (0)
+#define __cpu_to_le8s(x) do { (void)(x); } while (0)
+#define __le8_to_cpus(x) do { (void)(x); } while (0)
 #define __cpu_to_be64s(x) __swab64s((x))
 #define __be64_to_cpus(x) __swab64s((x))
 #define __cpu_to_be32s(x) __swab32s((x))
 #define __be32_to_cpus(x) __swab32s((x))
 #define __cpu_to_be16s(x) __swab16s((x))
 #define __be16_to_cpus(x) __swab16s((x))
+#define __cpu_to_be8s(x) __swab8s((x))
+#define __be8_to_cpus(x) __swab8s((x))
 
 #ifdef __KERNEL__
 #include <linux/byteorder/generic.h>
include/linux/swab.h +51 -2

102 lines not on this page · raw message

include/linux/types.h +2 -0
index c42724f..f8a0a0f 100644
@@ -165,6 +165,8 @@ typedef unsigned long blkcnt_t;
 #define __bitwise
 #endif
 
+typedef __u8 __bitwise __le8;
+typedef __u8 __bitwise __be8;
 typedef __u16 __bitwise __le16;
 typedef __u16 __bitwise __be16;
 typedef __u32 __bitwise __le32;
#1 lore raw
Quoted 11 lines · Andy Gospodarek

After my recent post to net-2.6 this week to accidentally run htons to a u8, it is clear to me we _must_ add some infrastructure to make sure single bytes are in the correct network and host order on big and little endian systems. Today seemed like the perfect day to post this.

This patch adds basic support for htonb and ntohb. Patches to add this in the entire networking tree in _every_ case where a single byte is accessed will be posted next week -- I'm almost done with them!

---

Seems fine, thanks a lot Andy.

Hmm, I notice this doesnt handle yet arches where a byte is 9 bits long.

Do you have any plan, to finally support PDP-6/10 ?

#2 lore raw
Quoted 10 lines · Eric Dumazet
Quoted 9 lines · Andy Gospodarek

After my recent post to net-2.6 this week to accidentally run htons to a u8, it is clear to me we _must_ add some infrastructure to make sure single bytes are in the correct network and host order on big and little endian systems. Today seemed like the perfect day to post this.

This patch adds basic support for htonb and ntohb. Patches to add this in the entire networking tree in _every_ case where a single byte is accessed will be posted next week -- I'm almost done with them!

Quoted 9 lines
Quoted 2 lines

---

Seems fine, thanks a lot Andy.

Hmm, I notice this doesnt handle yet arches where a byte is 9 bits long.

Do you have any plan, to finally support PDP-6/10 ?

definately, and while your at it, I could really use some conversion routines for the more complex types just to save time. htonstr and ntohstr in particular

Quoted 7 lines

--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html