lib/packet.h: add hash_mac()
authorAndy Zhou <azhou@nicira.com>
Fri, 28 Mar 2014 03:22:37 +0000 (20:22 -0700)
committerAndy Zhou <azhou@nicira.com>
Fri, 28 Mar 2014 19:52:12 +0000 (12:52 -0700)
Add hash_mac() and apply it when appropriate.

Signed-off-by: Andy Zhou <azhou@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
lib/hash.h
lib/mac-learning.c
lib/packets.h
ofproto/bond.c
ofproto/ofproto-dpif-xlate.c

index 979214a..5a9e289 100644 (file)
@@ -38,6 +38,7 @@ uint32_t hash_bytes(const void *, size_t n_bytes, uint32_t basis);
 static inline uint32_t hash_int(uint32_t x, uint32_t basis);
 static inline uint32_t hash_2words(uint32_t, uint32_t);
 static inline uint32_t hash_uint64(uint64_t);
+static inline uint32_t hash_uint64_basis(uint64_t x, uint32_t basis);
 uint32_t hash_3words(uint32_t, uint32_t, uint32_t);
 
 static inline uint32_t hash_boolean(bool x, uint32_t basis);
@@ -124,6 +125,11 @@ static inline uint32_t hash_uint64(const uint64_t x)
     return hash_2words((uint32_t)(x >> 32), (uint32_t)x);
 }
 
+static inline uint32_t hash_uint64_basis(const uint64_t x,
+                                         const uint32_t basis)
+{
+    return hash_3words((uint32_t)(x >> 32), (uint32_t)x, basis);
+}
 #ifdef __cplusplus
 }
 #endif
index 0854eb9..7dcce41 100644 (file)
@@ -45,9 +45,7 @@ static uint32_t
 mac_table_hash(const struct mac_learning *ml, const uint8_t mac[ETH_ADDR_LEN],
                uint16_t vlan)
 {
-    unsigned int mac1 = get_unaligned_u32(ALIGNED_CAST(uint32_t *, mac));
-    unsigned int mac2 = get_unaligned_u16(ALIGNED_CAST(uint16_t *, mac + 4));
-    return hash_3words(mac1, mac2 | (vlan << 16), ml->secret);
+    return hash_mac(mac, vlan, ml->secret);
 }
 
 static struct mac_entry *
index 30e4d13..22817af 100644 (file)
@@ -26,6 +26,7 @@
 #include "flow.h"
 #include "openvswitch/types.h"
 #include "random.h"
+#include "hash.h"
 #include "util.h"
 
 struct ofpbuf;
@@ -134,6 +135,11 @@ static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
             | ((uint64_t) ea[4] << 8)
             | ea[5]);
 }
+static inline uint64_t eth_addr_vlan_to_uint64(const uint8_t ea[ETH_ADDR_LEN],
+                                               uint16_t vlan)
+{
+    return (((uint64_t)vlan << 48) | eth_addr_to_uint64(ea));
+}
 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
 {
     ea[0] = x >> 40;
@@ -165,6 +171,11 @@ static inline void eth_addr_nicira_random(uint8_t ea[ETH_ADDR_LEN])
     /* Set the top bit to indicate random Nicira address. */
     ea[3] |= 0x80;
 }
+static inline uint32_t hash_mac(const uint8_t ea[ETH_ADDR_LEN],
+                                const uint16_t vlan, const uint32_t basis)
+{
+    return hash_uint64_basis(eth_addr_vlan_to_uint64(ea, vlan), basis);
+}
 
 bool eth_addr_is_reserved(const uint8_t ea[ETH_ADDR_LEN]);
 bool eth_addr_from_string(const char *, uint8_t ea[ETH_ADDR_LEN]);
index 21201ee..6812330 100644 (file)
@@ -1404,7 +1404,7 @@ bond_link_status_update(struct bond_slave *slave)
 static unsigned int
 bond_hash_src(const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan, uint32_t basis)
 {
-    return hash_3words(hash_bytes(mac, ETH_ADDR_LEN, 0), vlan, basis);
+    return hash_mac(mac, vlan, basis);
 }
 
 static unsigned int
index 3385cdd..e26b7c9 100644 (file)
@@ -2001,7 +2001,7 @@ xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
     const struct ofputil_bucket *bucket;
     uint32_t basis;
 
-    basis = hash_bytes(ctx->xin->flow.dl_dst, sizeof ctx->xin->flow.dl_dst, 0);
+    basis = hash_mac(ctx->xin->flow.dl_dst, 0, 0);
     bucket = group_best_live_bucket(ctx, group, basis);
     if (bucket) {
         memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);