unaligned: Add unaligned accessors for ovs_be<N> data.
authorBen Pfaff <blp@nicira.com>
Mon, 15 Nov 2010 23:53:00 +0000 (15:53 -0800)
committerBen Pfaff <blp@nicira.com>
Tue, 30 Nov 2010 00:29:11 +0000 (16:29 -0800)
These accessors are semantically identical to the ones for uint<N>_t data,
but the names are more informative to readers, and the types provide
annotations for sparse.

lib/flow.c
lib/nx-match.c
lib/unaligned.h

index 26d03fd430146f02005ec991586533820192a4e2..099740dcda6e2d4e4db5f5998af5d9bf7638a1a5 100644 (file)
@@ -175,8 +175,8 @@ flow_extract(struct ofpbuf *packet, ovs_be32 tun_id, uint16_t in_port,
     if (flow->dl_type == htons(ETH_TYPE_IP)) {
         const struct ip_header *nh = pull_ip(&b);
         if (nh) {
-            flow->nw_src = get_unaligned_u32(&nh->ip_src);
-            flow->nw_dst = get_unaligned_u32(&nh->ip_dst);
+            flow->nw_src = get_unaligned_be32(&nh->ip_src);
+            flow->nw_dst = get_unaligned_be32(&nh->ip_dst);
             flow->nw_tos = nh->ip_tos & IP_DSCP_MASK;
             flow->nw_proto = nh->ip_proto;
             packet->l4 = b.data;
index 6c8ee13414bdf72fd4b284cf8387c19f2f3573e7..ea31c79f24ee617d5a12cf7b001ffec39d60a656 100644 (file)
@@ -151,9 +151,9 @@ parse_nx_reg(const struct nxm_field *f,
     } else {
         flow_wildcards_set_reg_mask(wc, idx,
                                     (NXM_HASMASK(f->header)
-                                     ? ntohl(get_unaligned_u32(maskp))
+                                     ? ntohl(get_unaligned_be32(maskp))
                                      : UINT32_MAX));
-        flow->regs[idx] = ntohl(get_unaligned_u32(value));
+        flow->regs[idx] = ntohl(get_unaligned_be32(value));
         flow->regs[idx] &= wc->reg_masks[idx];
         return 0;
     }
@@ -169,7 +169,7 @@ parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
     switch (f->index) {
         /* Metadata. */
     case NFI_NXM_OF_IN_PORT:
-        flow->in_port = ntohs(get_unaligned_u16(value));
+        flow->in_port = ntohs(get_unaligned_be16(value));
         if (flow->in_port == OFPP_LOCAL) {
             flow->in_port = ODPP_LOCAL;
         }
@@ -209,7 +209,7 @@ parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
         memcpy(flow->dl_src, value, ETH_ADDR_LEN);
         return 0;
     case NFI_NXM_OF_ETH_TYPE:
-        flow->dl_type = get_unaligned_u16(value);
+        flow->dl_type = get_unaligned_be16(value);
         return 0;
 
         /* 802.1Q header. */
@@ -217,15 +217,15 @@ parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
         if (wc->vlan_tci_mask) {
             return NXM_DUP_TYPE;
         } else {
-            cls_rule_set_dl_tci(rule, get_unaligned_u16(value));
+            cls_rule_set_dl_tci(rule, get_unaligned_be16(value));
             return 0;
         }
     case NFI_NXM_OF_VLAN_TCI_W:
         if (wc->vlan_tci_mask) {
             return NXM_DUP_TYPE;
         } else {
-            cls_rule_set_dl_tci_masked(rule, get_unaligned_u16(value),
-                                       get_unaligned_u16(mask));
+            cls_rule_set_dl_tci_masked(rule, get_unaligned_be16(value),
+                                       get_unaligned_be16(mask));
             return 0;
         }
 
@@ -247,7 +247,7 @@ parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
         if (wc->nw_src_mask) {
             return NXM_DUP_TYPE;
         } else {
-            cls_rule_set_nw_src(rule, get_unaligned_u32(value));
+            cls_rule_set_nw_src(rule, get_unaligned_be32(value));
             return 0;
         }
     case NFI_NXM_OF_IP_SRC_W:
@@ -255,8 +255,8 @@ parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
         if (wc->nw_src_mask) {
             return NXM_DUP_TYPE;
         } else {
-            ovs_be32 ip = get_unaligned_u32(value);
-            ovs_be32 netmask = get_unaligned_u32(mask);
+            ovs_be32 ip = get_unaligned_be32(value);
+            ovs_be32 netmask = get_unaligned_be32(mask);
             if (!cls_rule_set_nw_src_masked(rule, ip, netmask)) {
                 return NXM_BAD_MASK;
             }
@@ -267,7 +267,7 @@ parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
         if (wc->nw_dst_mask) {
             return NXM_DUP_TYPE;
         } else {
-            cls_rule_set_nw_dst(rule, get_unaligned_u32(value));
+            cls_rule_set_nw_dst(rule, get_unaligned_be32(value));
             return 0;
         }
     case NFI_NXM_OF_IP_DST_W:
@@ -275,8 +275,8 @@ parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
         if (wc->nw_dst_mask) {
             return NXM_DUP_TYPE;
         } else {
-            ovs_be32 ip = get_unaligned_u32(value);
-            ovs_be32 netmask = get_unaligned_u32(mask);
+            ovs_be32 ip = get_unaligned_be32(value);
+            ovs_be32 netmask = get_unaligned_be32(mask);
             if (!cls_rule_set_nw_dst_masked(rule, ip, netmask)) {
                 return NXM_BAD_MASK;
             }
@@ -285,18 +285,18 @@ parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
 
         /* TCP header. */
     case NFI_NXM_OF_TCP_SRC:
-        flow->tp_src = get_unaligned_u16(value);
+        flow->tp_src = get_unaligned_be16(value);
         return 0;
     case NFI_NXM_OF_TCP_DST:
-        flow->tp_dst = get_unaligned_u16(value);
+        flow->tp_dst = get_unaligned_be16(value);
         return 0;
 
         /* UDP header. */
     case NFI_NXM_OF_UDP_SRC:
-        flow->tp_src = get_unaligned_u16(value);
+        flow->tp_src = get_unaligned_be16(value);
         return 0;
     case NFI_NXM_OF_UDP_DST:
-        flow->tp_dst = get_unaligned_u16(value);
+        flow->tp_dst = get_unaligned_be16(value);
         return 0;
 
         /* ICMP header. */
@@ -309,16 +309,16 @@ parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
 
         /* ARP header. */
     case NFI_NXM_OF_ARP_OP:
-        if (ntohs(get_unaligned_u16(value)) > 255) {
+        if (ntohs(get_unaligned_be16(value)) > 255) {
             return NXM_BAD_VALUE;
         } else {
-            flow->nw_proto = ntohs(get_unaligned_u16(value));
+            flow->nw_proto = ntohs(get_unaligned_be16(value));
             return 0;
         }
 
         /* Tunnel ID. */
     case NFI_NXM_NX_TUN_ID:
-        flow->tun_id = htonl(ntohll(get_unaligned_u64(value)));
+        flow->tun_id = htonl(ntohll(get_unaligned_be64(value)));
         return 0;
 
         /* Registers. */
index 4540c31bbb6e690003cf84ecc666a58947ee3ea3..ef08f4e22a9db3a1794668a07091c0f4c51560bb 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <stdint.h>
 #include "byte-order.h"
+#include "openvswitch/types.h"
 
 /* Public API. */
 static inline uint16_t get_unaligned_u16(const uint16_t *);
@@ -28,33 +29,44 @@ static inline void put_unaligned_u16(uint16_t *, uint16_t);
 static inline void put_unaligned_u32(uint32_t *, uint32_t);
 static inline void put_unaligned_u64(uint64_t *, uint64_t);
 
+static inline ovs_be16 get_unaligned_be16(const ovs_be16 *);
+static inline ovs_be32 get_unaligned_be32(const ovs_be32 *);
+static inline ovs_be64 get_unaligned_be64(const ovs_be64 *);
+static inline void put_unaligned_be16(ovs_be16 *, ovs_be16);
+static inline void put_unaligned_be32(ovs_be32 *, ovs_be32);
+static inline void put_unaligned_be64(ovs_be64 *, ovs_be64);
+
 #ifdef __GNUC__
 /* GCC implementations. */
-#define GCC_UNALIGNED_ACCESSORS(SIZE)                       \
-struct unaligned_u##SIZE {                                  \
-    uint##SIZE##_t x __attribute__((__packed__));           \
-};                                                          \
-static inline struct unaligned_u##SIZE *                    \
-unaligned_u##SIZE(const uint##SIZE##_t *p)                  \
-{                                                           \
-    return (struct unaligned_u##SIZE *) p;                  \
-}                                                           \
-                                                            \
-static inline uint##SIZE##_t                                \
-get_unaligned_u##SIZE(const uint##SIZE##_t *p)              \
-{                                                           \
-    return unaligned_u##SIZE(p)->x;                         \
-}                                                           \
-                                                            \
-static inline void                                          \
-put_unaligned_u##SIZE(uint##SIZE##_t *p, uint##SIZE##_t x)  \
-{                                                           \
-    unaligned_u##SIZE(p)->x = x;                            \
+#define GCC_UNALIGNED_ACCESSORS(TYPE, ABBREV)   \
+struct unaligned_##ABBREV {                     \
+    TYPE x __attribute__((__packed__));         \
+};                                              \
+static inline struct unaligned_##ABBREV *       \
+unaligned_##ABBREV(const TYPE *p)               \
+{                                               \
+    return (struct unaligned_##ABBREV *) p;     \
+}                                               \
+                                                \
+static inline TYPE                              \
+get_unaligned_##ABBREV(const TYPE *p)           \
+{                                               \
+    return unaligned_##ABBREV(p)->x;            \
+}                                               \
+                                                \
+static inline void                              \
+put_unaligned_##ABBREV(TYPE *p, TYPE x)         \
+{                                               \
+    unaligned_##ABBREV(p)->x = x;               \
 }
 
-GCC_UNALIGNED_ACCESSORS(16);
-GCC_UNALIGNED_ACCESSORS(32);
-GCC_UNALIGNED_ACCESSORS(64);
+GCC_UNALIGNED_ACCESSORS(uint16_t, u16);
+GCC_UNALIGNED_ACCESSORS(uint32_t, u32);
+GCC_UNALIGNED_ACCESSORS(uint64_t, u64);
+
+GCC_UNALIGNED_ACCESSORS(ovs_be16, be16);
+GCC_UNALIGNED_ACCESSORS(ovs_be32, be32);
+GCC_UNALIGNED_ACCESSORS(ovs_be64, be64);
 #else
 /* Generic implementations. */
 
@@ -117,6 +129,16 @@ static inline void put_unaligned_u64(uint64_t *p_, uint64_t x_)
     p[6] = x >> 8;
     p[7] = x;
 }
+
+/* Only sparse cares about the difference between uint<N>_t and ovs_be<N>, and
+ * that takes the GCC branch, so there's no point in working too hard on these
+ * accessors. */
+#define get_unaligned_be16 get_unaligned_u16
+#define get_unaligned_be32 get_unaligned_u32
+#define get_unaligned_be64 get_unaligned_u64
+#define put_unaligned_be16 put_unaligned_u16
+#define put_unaligned_be32 put_unaligned_u32
+#define put_unaligned_be64 put_unaligned_u64
 #endif
 
 #endif /* unaligned.h */