* [PATCH net-next 2/2] net: initialize hashrnd in flow_dissector with net_get_random_once
@ 2013-10-23 11:12 Hannes Frederic Sowa
2013-10-23 13:58 ` Eric Dumazet
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Hannes Frederic Sowa @ 2013-10-23 11:12 UTC (permalink / raw)
To: netdev; +Cc: davem, edumazet
We also can defer the initialization of hashrnd in flow_dissector
to its first use. Since net_get_random_once is irqsave now we don't
have to audit the call paths if one of this functions get called by an
interrupt handler.
Cc: David S. Miller <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@google.com>
Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
---
net/core/flow_dissector.c | 34 +++++++++++++++++++++-------------
1 file changed, 21 insertions(+), 13 deletions(-)
diff --git a/net/core/flow_dissector.c b/net/core/flow_dissector.c
index f8e25ac..5cac36e 100644
--- a/net/core/flow_dissector.c
+++ b/net/core/flow_dissector.c
@@ -184,6 +184,22 @@ ipv6:
EXPORT_SYMBOL(skb_flow_dissect);
static u32 hashrnd __read_mostly;
+static __always_inline void __flow_hash_secret_init(void)
+{
+ net_get_random_once(&hashrnd, sizeof(hashrnd));
+}
+
+static __always_inline u32 __flow_hash_3words(u32 a, u32 b, u32 c)
+{
+ __flow_hash_secret_init();
+ return jhash_3words(a, b, c, hashrnd);
+}
+
+static __always_inline u32 __flow_hash_1word(u32 a)
+{
+ __flow_hash_secret_init();
+ return jhash_1word(a, hashrnd);
+}
/*
* __skb_get_rxhash: calculate a flow hash based on src/dst addresses
@@ -210,9 +226,9 @@ void __skb_get_rxhash(struct sk_buff *skb)
swap(keys.port16[0], keys.port16[1]);
}
- hash = jhash_3words((__force u32)keys.dst,
- (__force u32)keys.src,
- (__force u32)keys.ports, hashrnd);
+ hash = __flow_hash_3words((__force u32)keys.dst,
+ (__force u32)keys.src,
+ (__force u32)keys.ports);
if (!hash)
hash = 1;
@@ -248,7 +264,7 @@ u16 __skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb,
hash = skb->sk->sk_hash;
else
hash = (__force u16) skb->protocol;
- hash = jhash_1word(hash, hashrnd);
+ hash = __flow_hash_1word(hash);
return (u16) (((u64) hash * qcount) >> 32) + qoffset;
}
@@ -340,7 +356,7 @@ static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
else
hash = (__force u16) skb->protocol ^
skb->rxhash;
- hash = jhash_1word(hash, hashrnd);
+ hash = __flow_hash_1word(hash);
queue_index = map->queues[
((u64)hash * map->len) >> 32];
}
@@ -395,11 +411,3 @@ struct netdev_queue *netdev_pick_tx(struct net_device *dev,
skb_set_queue_mapping(skb, queue_index);
return netdev_get_tx_queue(dev, queue_index);
}
-
-static int __init initialize_hashrnd(void)
-{
- get_random_bytes(&hashrnd, sizeof(hashrnd));
- return 0;
-}
-
-late_initcall_sync(initialize_hashrnd);
--
1.8.3.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH net-next 2/2] net: initialize hashrnd in flow_dissector with net_get_random_once
2013-10-23 11:12 [PATCH net-next 2/2] net: initialize hashrnd in flow_dissector with net_get_random_once Hannes Frederic Sowa
@ 2013-10-23 13:58 ` Eric Dumazet
2013-10-23 14:13 ` Hannes Frederic Sowa
2013-10-23 18:06 ` [PATCH net-next v2 " Hannes Frederic Sowa
2013-10-25 23:04 ` [PATCH net-next " David Miller
2 siblings, 1 reply; 5+ messages in thread
From: Eric Dumazet @ 2013-10-23 13:58 UTC (permalink / raw)
To: Hannes Frederic Sowa; +Cc: netdev, davem, edumazet
On Wed, 2013-10-23 at 13:12 +0200, Hannes Frederic Sowa wrote:
> We also can defer the initialization of hashrnd in flow_dissector
> to its first use. Since net_get_random_once is irqsave now we don't
> have to audit the call paths if one of this functions get called by an
> interrupt handler.
>
> Cc: David S. Miller <davem@davemloft.net>
> Cc: Eric Dumazet <edumazet@google.com>
> Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
> ---
This really works well if CONFIG_JUMP_LABEL=y
I am afraid some arches do not really have this.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH net-next 2/2] net: initialize hashrnd in flow_dissector with net_get_random_once
2013-10-23 13:58 ` Eric Dumazet
@ 2013-10-23 14:13 ` Hannes Frederic Sowa
0 siblings, 0 replies; 5+ messages in thread
From: Hannes Frederic Sowa @ 2013-10-23 14:13 UTC (permalink / raw)
To: Eric Dumazet; +Cc: netdev, davem, edumazet
On Wed, Oct 23, 2013 at 06:58:34AM -0700, Eric Dumazet wrote:
> On Wed, 2013-10-23 at 13:12 +0200, Hannes Frederic Sowa wrote:
> > We also can defer the initialization of hashrnd in flow_dissector
> > to its first use. Since net_get_random_once is irqsave now we don't
> > have to audit the call paths if one of this functions get called by an
> > interrupt handler.
> >
> > Cc: David S. Miller <davem@davemloft.net>
> > Cc: Eric Dumazet <edumazet@google.com>
> > Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
> > ---
>
> This really works well if CONFIG_JUMP_LABEL=y
>
> I am afraid some arches do not really have this.
This is the problem with all users of net_get_random_once.
If an architecture does not have JUMP_LABEL or gcc does not provide
CC_HAVE_ASM_GOTO we have an unlikely branch in the code path and branch
to the epilogue of the function to generate the hash for the first
time. After that it's "just" a likely atomic boolean test.
I guess it is not that important to have a good secret hash key here as
this is solely used to dispatch the packets to cpus and does not store
anything in tables.
Greetings,
Hannes
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net-next v2 2/2] net: initialize hashrnd in flow_dissector with net_get_random_once
2013-10-23 11:12 [PATCH net-next 2/2] net: initialize hashrnd in flow_dissector with net_get_random_once Hannes Frederic Sowa
2013-10-23 13:58 ` Eric Dumazet
@ 2013-10-23 18:06 ` Hannes Frederic Sowa
2013-10-25 23:04 ` [PATCH net-next " David Miller
2 siblings, 0 replies; 5+ messages in thread
From: Hannes Frederic Sowa @ 2013-10-23 18:06 UTC (permalink / raw)
To: netdev, davem, edumazet
We also can defer the initialization of hashrnd in flow_dissector
to its first use. Since net_get_random_once is irq safe now we don't
have to audit the call paths if one of this functions get called by an
interrupt handler.
Cc: David S. Miller <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@google.com>
Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
---
v2: Reword the commit message only. It looked horribly.
net/core/flow_dissector.c | 34 +++++++++++++++++++++-------------
1 file changed, 21 insertions(+), 13 deletions(-)
diff --git a/net/core/flow_dissector.c b/net/core/flow_dissector.c
index f8e25ac..5cac36e 100644
--- a/net/core/flow_dissector.c
+++ b/net/core/flow_dissector.c
@@ -184,6 +184,22 @@ ipv6:
EXPORT_SYMBOL(skb_flow_dissect);
static u32 hashrnd __read_mostly;
+static __always_inline void __flow_hash_secret_init(void)
+{
+ net_get_random_once(&hashrnd, sizeof(hashrnd));
+}
+
+static __always_inline u32 __flow_hash_3words(u32 a, u32 b, u32 c)
+{
+ __flow_hash_secret_init();
+ return jhash_3words(a, b, c, hashrnd);
+}
+
+static __always_inline u32 __flow_hash_1word(u32 a)
+{
+ __flow_hash_secret_init();
+ return jhash_1word(a, hashrnd);
+}
/*
* __skb_get_rxhash: calculate a flow hash based on src/dst addresses
@@ -210,9 +226,9 @@ void __skb_get_rxhash(struct sk_buff *skb)
swap(keys.port16[0], keys.port16[1]);
}
- hash = jhash_3words((__force u32)keys.dst,
- (__force u32)keys.src,
- (__force u32)keys.ports, hashrnd);
+ hash = __flow_hash_3words((__force u32)keys.dst,
+ (__force u32)keys.src,
+ (__force u32)keys.ports);
if (!hash)
hash = 1;
@@ -248,7 +264,7 @@ u16 __skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb,
hash = skb->sk->sk_hash;
else
hash = (__force u16) skb->protocol;
- hash = jhash_1word(hash, hashrnd);
+ hash = __flow_hash_1word(hash);
return (u16) (((u64) hash * qcount) >> 32) + qoffset;
}
@@ -340,7 +356,7 @@ static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
else
hash = (__force u16) skb->protocol ^
skb->rxhash;
- hash = jhash_1word(hash, hashrnd);
+ hash = __flow_hash_1word(hash);
queue_index = map->queues[
((u64)hash * map->len) >> 32];
}
@@ -395,11 +411,3 @@ struct netdev_queue *netdev_pick_tx(struct net_device *dev,
skb_set_queue_mapping(skb, queue_index);
return netdev_get_tx_queue(dev, queue_index);
}
-
-static int __init initialize_hashrnd(void)
-{
- get_random_bytes(&hashrnd, sizeof(hashrnd));
- return 0;
-}
-
-late_initcall_sync(initialize_hashrnd);
--
1.8.3.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH net-next 2/2] net: initialize hashrnd in flow_dissector with net_get_random_once
2013-10-23 11:12 [PATCH net-next 2/2] net: initialize hashrnd in flow_dissector with net_get_random_once Hannes Frederic Sowa
2013-10-23 13:58 ` Eric Dumazet
2013-10-23 18:06 ` [PATCH net-next v2 " Hannes Frederic Sowa
@ 2013-10-25 23:04 ` David Miller
2 siblings, 0 replies; 5+ messages in thread
From: David Miller @ 2013-10-25 23:04 UTC (permalink / raw)
To: hannes; +Cc: netdev, edumazet
From: Hannes Frederic Sowa <hannes@stressinduktion.org>
Date: Wed, 23 Oct 2013 13:12:19 +0200
> We also can defer the initialization of hashrnd in flow_dissector
> to its first use. Since net_get_random_once is irqsave now we don't
> have to audit the call paths if one of this functions get called by an
> interrupt handler.
>
> Cc: David S. Miller <davem@davemloft.net>
> Cc: Eric Dumazet <edumazet@google.com>
> Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
Applied.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2013-10-25 23:04 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-10-23 11:12 [PATCH net-next 2/2] net: initialize hashrnd in flow_dissector with net_get_random_once Hannes Frederic Sowa
2013-10-23 13:58 ` Eric Dumazet
2013-10-23 14:13 ` Hannes Frederic Sowa
2013-10-23 18:06 ` [PATCH net-next v2 " Hannes Frederic Sowa
2013-10-25 23:04 ` [PATCH net-next " David Miller
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox