Skip to content

Commit

Permalink
flow_dissector: Abstract out hash computation
Browse files Browse the repository at this point in the history
Move the hash computation located in __skb_get_hash to be a separate
function which takes flow_keys as input. This will allow flow hash
computation in other contexts where we only have addresses and ports.

Signed-off-by: Tom Herbert <therbert@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Tom Herbert authored and David S. Miller committed Jul 8, 2014
1 parent 081a20f commit 5ed20a6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
1 change: 1 addition & 0 deletions include/net/flow_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ struct flow_keys {

bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow);
__be32 skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto);
u32 flow_hash_from_keys(struct flow_keys *keys);
#endif
44 changes: 28 additions & 16 deletions net/core/flow_dissector.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,33 @@ static __always_inline u32 __flow_hash_1word(u32 a)
return jhash_1word(a, hashrnd);
}

static inline u32 __flow_hash_from_keys(struct flow_keys *keys)
{
u32 hash;

/* get a consistent hash (same value on both flow directions) */
if (((__force u32)keys->dst < (__force u32)keys->src) ||
(((__force u32)keys->dst == (__force u32)keys->src) &&
((__force u16)keys->port16[1] < (__force u16)keys->port16[0]))) {
swap(keys->dst, keys->src);
swap(keys->port16[0], keys->port16[1]);
}

hash = __flow_hash_3words((__force u32)keys->dst,
(__force u32)keys->src,
(__force u32)keys->ports);
if (!hash)
hash = 1;

return hash;
}

u32 flow_hash_from_keys(struct flow_keys *keys)
{
return __flow_hash_from_keys(keys);
}
EXPORT_SYMBOL(flow_hash_from_keys);

/*
* __skb_get_hash: calculate a flow hash based on src/dst addresses
* and src/dst port numbers. Sets hash in skb to non-zero hash value
Expand All @@ -211,29 +238,14 @@ static __always_inline u32 __flow_hash_1word(u32 a)
void __skb_get_hash(struct sk_buff *skb)
{
struct flow_keys keys;
u32 hash;

if (!skb_flow_dissect(skb, &keys))
return;

if (keys.ports)
skb->l4_hash = 1;

/* get a consistent hash (same value on both flow directions) */
if (((__force u32)keys.dst < (__force u32)keys.src) ||
(((__force u32)keys.dst == (__force u32)keys.src) &&
((__force u16)keys.port16[1] < (__force u16)keys.port16[0]))) {
swap(keys.dst, keys.src);
swap(keys.port16[0], keys.port16[1]);
}

hash = __flow_hash_3words((__force u32)keys.dst,
(__force u32)keys.src,
(__force u32)keys.ports);
if (!hash)
hash = 1;

skb->hash = hash;
skb->hash = __flow_hash_from_keys(&keys);
}
EXPORT_SYMBOL(__skb_get_hash);

Expand Down

0 comments on commit 5ed20a6

Please sign in to comment.