[ovs-dev,PATCHv3,03/11] types: Add big-endian 128-bit types.
diff mbox

Message ID 1443559234-7330-4-git-send-email-joestringer@nicira.com
State Changes Requested
Headers show

Commit Message

Joe Stringer Sept. 29, 2015, 8:40 p.m. UTC
These types will be used by the following patches to ensure a consistent
wire format for 128-bit connection tracking labels.

Signed-off-by: Joe Stringer <joestringer@nicira.com>
---
 include/openvswitch/types.h |  7 +++++++
 lib/byte-order.h            | 14 ++++++++++++++
 lib/meta-flow.h             |  1 +
 lib/util.h                  | 21 +++++++++++++++++++++
 4 files changed, 43 insertions(+)

Comments

Ben Pfaff Sept. 30, 2015, 6:37 a.m. UTC | #1
On Tue, Sep 29, 2015 at 01:40:26PM -0700, Joe Stringer wrote:
> These types will be used by the following patches to ensure a consistent
> wire format for 128-bit connection tracking labels.
> 
> Signed-off-by: Joe Stringer <joestringer@nicira.com>

On a big-endian system, I don't think hton128() and ntoh128() functions
should swap hi and lo.
Joe Stringer Sept. 30, 2015, 4:43 p.m. UTC | #2
On 29 September 2015 at 23:37, Ben Pfaff <blp@nicira.com> wrote:
> On Tue, Sep 29, 2015 at 01:40:26PM -0700, Joe Stringer wrote:
>> These types will be used by the following patches to ensure a consistent
>> wire format for 128-bit connection tracking labels.
>>
>> Signed-off-by: Joe Stringer <joestringer@nicira.com>
>
> On a big-endian system, I don't think hton128() and ntoh128() functions
> should swap hi and lo.

True. I guess that the order of the elements within the u128 should
also be endian-dependent. I'll respin.

Patch
diff mbox

diff --git a/include/openvswitch/types.h b/include/openvswitch/types.h
index c138b9c..bb8d3ae 100644
--- a/include/openvswitch/types.h
+++ b/include/openvswitch/types.h
@@ -88,6 +88,13 @@  typedef union {
     } u64;
 } ovs_u128;
 
+typedef union {
+    ovs_be32 be32[4];
+    struct {
+        ovs_be64 hi, lo;
+    } be64;
+} ovs_be128;
+
 /* A 64-bit value, in network byte order, that is only aligned on a 32-bit
  * boundary. */
 typedef struct {
diff --git a/lib/byte-order.h b/lib/byte-order.h
index 544f46f..0749ff4 100644
--- a/lib/byte-order.h
+++ b/lib/byte-order.h
@@ -42,6 +42,20 @@  ovs_be64 htonll(uint64_t);
 uint64_t ntohll(ovs_be64);
 #endif
 
+static inline void
+hton128(const ovs_u128 *src, ovs_be128 *dst)
+{
+    dst->be64.hi = htonll(src->u64.lo);
+    dst->be64.lo = htonll(src->u64.hi);
+}
+
+static inline void
+ntoh128(const ovs_be128 *src, ovs_u128 *dst)
+{
+    dst->u64.hi = ntohll(src->be64.lo);
+    dst->u64.lo = ntohll(src->be64.hi);
+}
+
 static inline uint32_t
 uint32_byteswap(uint32_t crc) {
     return (((crc & 0x000000ff) << 24) |
diff --git a/lib/meta-flow.h b/lib/meta-flow.h
index 02272ef..f9379f9 100644
--- a/lib/meta-flow.h
+++ b/lib/meta-flow.h
@@ -1742,6 +1742,7 @@  union mf_value {
     uint8_t tun_metadata[128];
     struct in6_addr ipv6;
     struct eth_addr mac;
+    ovs_be128 be128;
     ovs_be64 be64;
     ovs_be32 be32;
     ovs_be16 be16;
diff --git a/lib/util.h b/lib/util.h
index 7080a0c..4c7f716 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -568,6 +568,27 @@  ovs_u128_equals(const ovs_u128 *a, const ovs_u128 *b)
     return (a->u64.hi == b->u64.hi) && (a->u64.lo == b->u64.lo);
 }
 
+/* Returns true if 'val' is 0. */
+static inline bool
+ovs_u128_is_zero(const ovs_u128 *val)
+{
+    return !(val->u64.hi || val->u64.lo);
+}
+
+/* Returns non-zero if the parameters have equal value. */
+static inline int
+ovs_be128_equals(const ovs_be128 *a, const ovs_be128 *b)
+{
+    return (a->be64.hi == b->be64.hi) && (a->be64.lo == b->be64.lo);
+}
+
+/* Returns true if 'val' is 0. */
+static inline bool
+ovs_be128_is_zero(const ovs_be128 *val)
+{
+    return !(val->be64.hi || val->be64.lo);
+}
+
 void xsleep(unsigned int seconds);
 
 #ifdef _WIN32