From: Andy Zhou Date: Fri, 28 Mar 2014 03:22:37 +0000 (-0700) Subject: lib/packet.h: add hash_mac() X-Git-Tag: sliver-openvswitch-2.2.90-1~6^2~15 X-Git-Url: http://git.onelab.eu/?p=sliver-openvswitch.git;a=commitdiff_plain;h=7e36ac42e33a89d5d2c981ea7750939a1da6db89 lib/packet.h: add hash_mac() Add hash_mac() and apply it when appropriate. Signed-off-by: Andy Zhou Acked-by: Ben Pfaff --- diff --git a/lib/hash.h b/lib/hash.h index 979214af0..5a9e28941 100644 --- a/lib/hash.h +++ b/lib/hash.h @@ -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 diff --git a/lib/mac-learning.c b/lib/mac-learning.c index 0854eb92a..7dcce4154 100644 --- a/lib/mac-learning.c +++ b/lib/mac-learning.c @@ -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 * diff --git a/lib/packets.h b/lib/packets.h index 30e4d13f3..22817af16 100644 --- a/lib/packets.h +++ b/lib/packets.h @@ -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]); diff --git a/ofproto/bond.c b/ofproto/bond.c index 21201eedd..681233060 100644 --- a/ofproto/bond.c +++ b/ofproto/bond.c @@ -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 diff --git a/ofproto/ofproto-dpif-xlate.c b/ofproto/ofproto-dpif-xlate.c index 3385cdd48..e26b7c960 100644 --- a/ofproto/ofproto-dpif-xlate.c +++ b/ofproto/ofproto-dpif-xlate.c @@ -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);