Skip to content

Commit c6cc1ca

Browse files
tomratbertdavem330
authored andcommitted
flowi: Abstract out functions to get flow hash based on flowi
Create __get_hash_from_flowi6 and __get_hash_from_flowi4 to get the flow keys and hash based on flowi structures. These are called by __skb_get_hash_flowi6 and __skb_get_hash_flowi4. Also, created get_hash_from_flowi6 and get_hash_from_flowi4 which can be called when just the hash value for a flowi is needed. Signed-off-by: Tom Herbert <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent bcc8383 commit c6cc1ca

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

include/linux/skbuff.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,8 +1030,12 @@ __u32 __skb_get_hash_flowi6(struct sk_buff *skb, struct flowi6 *fl6);
10301030

10311031
static inline __u32 skb_get_hash_flowi6(struct sk_buff *skb, struct flowi6 *fl6)
10321032
{
1033-
if (!skb->l4_hash && !skb->sw_hash)
1034-
__skb_get_hash_flowi6(skb, fl6);
1033+
if (!skb->l4_hash && !skb->sw_hash) {
1034+
struct flow_keys keys;
1035+
1036+
__skb_set_sw_hash(skb, __get_hash_from_flowi6(fl6, &keys),
1037+
flow_keys_have_l4(&keys));
1038+
}
10351039

10361040
return skb->hash;
10371041
}
@@ -1040,8 +1044,12 @@ __u32 __skb_get_hash_flowi4(struct sk_buff *skb, struct flowi4 *fl);
10401044

10411045
static inline __u32 skb_get_hash_flowi4(struct sk_buff *skb, struct flowi4 *fl4)
10421046
{
1043-
if (!skb->l4_hash && !skb->sw_hash)
1044-
__skb_get_hash_flowi4(skb, fl4);
1047+
if (!skb->l4_hash && !skb->sw_hash) {
1048+
struct flow_keys keys;
1049+
1050+
__skb_set_sw_hash(skb, __get_hash_from_flowi4(fl4, &keys),
1051+
flow_keys_have_l4(&keys));
1052+
}
10451053

10461054
return skb->hash;
10471055
}

include/net/flow.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/socket.h>
1111
#include <linux/in6.h>
1212
#include <linux/atomic.h>
13+
#include <net/flow_dissector.h>
1314

1415
/*
1516
* ifindex generation is per-net namespace, and loopback is
@@ -243,4 +244,22 @@ void flow_cache_flush(struct net *net);
243244
void flow_cache_flush_deferred(struct net *net);
244245
extern atomic_t flow_cache_genid;
245246

247+
__u32 __get_hash_from_flowi6(struct flowi6 *fl6, struct flow_keys *keys);
248+
249+
static inline __u32 get_hash_from_flowi6(struct flowi6 *fl6)
250+
{
251+
struct flow_keys keys;
252+
253+
return __get_hash_from_flowi6(fl6, &keys);
254+
}
255+
256+
__u32 __get_hash_from_flowi4(struct flowi4 *fl4, struct flow_keys *keys);
257+
258+
static inline __u32 get_hash_from_flowi4(struct flowi4 *fl4)
259+
{
260+
struct flow_keys keys;
261+
262+
return __get_hash_from_flowi4(fl4, &keys);
263+
}
264+
246265
#endif

include/net/flow_dissector.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,6 @@ static inline bool flow_keys_have_l4(struct flow_keys *keys)
172172
return (keys->ports.ports || keys->tags.flow_label);
173173
}
174174

175+
u32 flow_hash_from_keys(struct flow_keys *keys);
176+
175177
#endif

net/core/flow.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/cpumask.h>
2323
#include <linux/mutex.h>
2424
#include <net/flow.h>
25+
#include <net/flow_dissector.h>
2526
#include <linux/atomic.h>
2627
#include <linux/security.h>
2728
#include <net/net_namespace.h>
@@ -509,3 +510,38 @@ void flow_cache_fini(struct net *net)
509510
fc->percpu = NULL;
510511
}
511512
EXPORT_SYMBOL(flow_cache_fini);
513+
514+
__u32 __get_hash_from_flowi6(struct flowi6 *fl6, struct flow_keys *keys)
515+
{
516+
memset(keys, 0, sizeof(*keys));
517+
518+
memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
519+
sizeof(keys->addrs.v6addrs.src));
520+
memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
521+
sizeof(keys->addrs.v6addrs.dst));
522+
keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
523+
keys->ports.src = fl6->fl6_sport;
524+
keys->ports.dst = fl6->fl6_dport;
525+
keys->keyid.keyid = fl6->fl6_gre_key;
526+
keys->tags.flow_label = (__force u32)fl6->flowlabel;
527+
keys->basic.ip_proto = fl6->flowi6_proto;
528+
529+
return flow_hash_from_keys(keys);
530+
}
531+
EXPORT_SYMBOL(__get_hash_from_flowi6);
532+
533+
__u32 __get_hash_from_flowi4(struct flowi4 *fl4, struct flow_keys *keys)
534+
{
535+
memset(keys, 0, sizeof(*keys));
536+
537+
keys->addrs.v4addrs.src = fl4->saddr;
538+
keys->addrs.v4addrs.dst = fl4->daddr;
539+
keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
540+
keys->ports.src = fl4->fl4_sport;
541+
keys->ports.dst = fl4->fl4_dport;
542+
keys->keyid.keyid = fl4->fl4_gre_key;
543+
keys->basic.ip_proto = fl4->flowi4_proto;
544+
545+
return flow_hash_from_keys(keys);
546+
}
547+
EXPORT_SYMBOL(__get_hash_from_flowi4);

0 commit comments

Comments
 (0)