rps: Add flag to skb to indicate rxhash is based on L4 tuple
authorTom Herbert <therbert@google.com>
Sun, 14 Aug 2011 19:45:55 +0000 (19:45 +0000)
committerDavid S. Miller <davem@davemloft.net>
Thu, 18 Aug 2011 03:06:03 +0000 (20:06 -0700)
The l4_rxhash flag was added to the skb structure to indicate
that the rxhash value was computed over the 4 tuple for the
packet which includes the port information in the encapsulated
transport packet.  This is used by the stack to preserve the
rxhash value in __skb_rx_tunnel.

Signed-off-by: Tom Herbert <therbert@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/skbuff.h
include/net/dst.h
include/net/sock.h
net/core/dev.c
net/core/skbuff.c
net/ipv4/tcp_ipv4.c
net/ipv4/udp.c
net/ipv6/tcp_ipv6.c
net/ipv6/udp.c

index 7b996ed86d5b823d1b92a0df82da033cafa4d6c3..f902c331217b644c26a31d8e1be9fcc7de23b317 100644 (file)
@@ -414,6 +414,7 @@ struct sk_buff {
        __u8                    ndisc_nodetype:2;
 #endif
        __u8                    ooo_okay:1;
+       __u8                    l4_rxhash:1;
        kmemcheck_bitfield_end(flags2);
 
        /* 0/13 bit hole */
@@ -572,11 +573,11 @@ extern unsigned int   skb_find_text(struct sk_buff *skb, unsigned int from,
                                    unsigned int to, struct ts_config *config,
                                    struct ts_state *state);
 
-extern __u32 __skb_get_rxhash(struct sk_buff *skb);
+extern void __skb_get_rxhash(struct sk_buff *skb);
 static inline __u32 skb_get_rxhash(struct sk_buff *skb)
 {
        if (!skb->rxhash)
-               skb->rxhash = __skb_get_rxhash(skb);
+               __skb_get_rxhash(skb);
 
        return skb->rxhash;
 }
index 13d507d69ddbcfb01bd717ba1fd2659cc74e53d6..4fb6c43817918992f8334c49022d5184ea45c8e4 100644 (file)
@@ -325,7 +325,14 @@ static inline void skb_dst_force(struct sk_buff *skb)
 static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev)
 {
        skb->dev = dev;
-       skb->rxhash = 0;
+
+       /*
+        * Clear rxhash so that we can recalulate the hash for the
+        * encapsulated packet, unless we have already determine the hash
+        * over the L4 4-tuple.
+        */
+       if (!skb->l4_rxhash)
+               skb->rxhash = 0;
        skb_set_queue_mapping(skb, 0);
        skb_dst_drop(skb);
        nf_reset(skb);
index 8e4062f165b8ec19bab5aeea63730577aa3c31a8..5ac682f73d6389ab4fe4977fb183ab13044929b1 100644 (file)
@@ -686,16 +686,25 @@ static inline void sock_rps_reset_flow(const struct sock *sk)
 #endif
 }
 
-static inline void sock_rps_save_rxhash(struct sock *sk, u32 rxhash)
+static inline void sock_rps_save_rxhash(struct sock *sk,
+                                       const struct sk_buff *skb)
 {
 #ifdef CONFIG_RPS
-       if (unlikely(sk->sk_rxhash != rxhash)) {
+       if (unlikely(sk->sk_rxhash != skb->rxhash)) {
                sock_rps_reset_flow(sk);
-               sk->sk_rxhash = rxhash;
+               sk->sk_rxhash = skb->rxhash;
        }
 #endif
 }
 
+static inline void sock_rps_reset_rxhash(struct sock *sk)
+{
+#ifdef CONFIG_RPS
+       sock_rps_reset_flow(sk);
+       sk->sk_rxhash = 0;
+#endif
+}
+
 #define sk_wait_event(__sk, __timeo, __condition)                      \
        ({      int __rc;                                               \
                release_sock(__sk);                                     \
index 6578d948304345e210d8ac26ca9f1bdb9e5ba3f4..e485cb37228f8b837d988dd79cb19bc382680aa0 100644 (file)
@@ -2519,10 +2519,11 @@ static inline void ____napi_schedule(struct softnet_data *sd,
 
 /*
  * __skb_get_rxhash: calculate a flow hash based on src/dst addresses
- * and src/dst port numbers. Returns a non-zero hash number on success
- * and 0 on failure.
+ * and src/dst port numbers.  Sets rxhash in skb to non-zero hash value
+ * on success, zero indicates no valid hash.  Also, sets l4_rxhash in skb
+ * if hash is a canonical 4-tuple hash over transport ports.
  */
-__u32 __skb_get_rxhash(struct sk_buff *skb)
+void __skb_get_rxhash(struct sk_buff *skb)
 {
        int nhoff, hash = 0, poff;
        const struct ipv6hdr *ip6;
@@ -2574,6 +2575,7 @@ __u32 __skb_get_rxhash(struct sk_buff *skb)
                        ports.v32 = * (__force u32 *) (skb->data + nhoff);
                        if (ports.v16[1] < ports.v16[0])
                                swap(ports.v16[0], ports.v16[1]);
+                       skb->l4_rxhash = 1;
                }
        }
 
@@ -2586,7 +2588,7 @@ __u32 __skb_get_rxhash(struct sk_buff *skb)
                hash = 1;
 
 done:
-       return hash;
+       skb->rxhash = hash;
 }
 EXPORT_SYMBOL(__skb_get_rxhash);
 
index 27002dffe7ed3ea4df4f9b8d77b4210615d668c2..edb66f3e24f15243b51e27814c61a38524927e65 100644 (file)
@@ -529,6 +529,7 @@ static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
        new->mac_header         = old->mac_header;
        skb_dst_copy(new, old);
        new->rxhash             = old->rxhash;
+       new->l4_rxhash          = old->l4_rxhash;
 #ifdef CONFIG_XFRM
        new->sp                 = secpath_get(old->sp);
 #endif
index 1c12b8ec849dcff6b5363338a42ce00e06d9246a..b3f26114b03e6f2c59f014dbbe965c05367275ab 100644 (file)
@@ -1578,7 +1578,7 @@ int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
 #endif
 
        if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
-               sock_rps_save_rxhash(sk, skb->rxhash);
+               sock_rps_save_rxhash(sk, skb);
                if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len)) {
                        rsk = sk;
                        goto reset;
@@ -1595,7 +1595,7 @@ int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
                        goto discard;
 
                if (nsk != sk) {
-                       sock_rps_save_rxhash(nsk, skb->rxhash);
+                       sock_rps_save_rxhash(nsk, skb);
                        if (tcp_child_process(sk, nsk, skb)) {
                                rsk = nsk;
                                goto reset;
@@ -1603,7 +1603,7 @@ int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
                        return 0;
                }
        } else
-               sock_rps_save_rxhash(sk, skb->rxhash);
+               sock_rps_save_rxhash(sk, skb);
 
        if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len)) {
                rsk = sk;
index c1d5facab7c95ede22283664a358824af6872dd5..ebaa96bd346475dd550baadc9e32dd7a4e5d8ef6 100644 (file)
@@ -1267,7 +1267,7 @@ int udp_disconnect(struct sock *sk, int flags)
        sk->sk_state = TCP_CLOSE;
        inet->inet_daddr = 0;
        inet->inet_dport = 0;
-       sock_rps_save_rxhash(sk, 0);
+       sock_rps_reset_rxhash(sk);
        sk->sk_bound_dev_if = 0;
        if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
                inet_reset_saddr(sk);
@@ -1355,7 +1355,7 @@ static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
        int rc;
 
        if (inet_sk(sk)->inet_daddr)
-               sock_rps_save_rxhash(sk, skb->rxhash);
+               sock_rps_save_rxhash(sk, skb);
 
        rc = ip_queue_rcv_skb(sk, skb);
        if (rc < 0) {
index d1fb63f4aeb76351bd55d8c6bfa8cf0e8e20c60f..44a5859535b5512c841959688c2e71a6298ca6b7 100644 (file)
@@ -1628,7 +1628,7 @@ static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
                opt_skb = skb_clone(skb, GFP_ATOMIC);
 
        if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
-               sock_rps_save_rxhash(sk, skb->rxhash);
+               sock_rps_save_rxhash(sk, skb);
                if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len))
                        goto reset;
                if (opt_skb)
@@ -1650,7 +1650,7 @@ static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
                 * the new socket..
                 */
                if(nsk != sk) {
-                       sock_rps_save_rxhash(nsk, skb->rxhash);
+                       sock_rps_save_rxhash(nsk, skb);
                        if (tcp_child_process(sk, nsk, skb))
                                goto reset;
                        if (opt_skb)
@@ -1658,7 +1658,7 @@ static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
                        return 0;
                }
        } else
-               sock_rps_save_rxhash(sk, skb->rxhash);
+               sock_rps_save_rxhash(sk, skb);
 
        if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len))
                goto reset;
index 97e47f06e8b77fe34907ad941948ad5cdf0226e0..35bbdc42241e5ee249be4a096eac2051f5caedcd 100644 (file)
@@ -509,7 +509,7 @@ int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
        int is_udplite = IS_UDPLITE(sk);
 
        if (!ipv6_addr_any(&inet6_sk(sk)->daddr))
-               sock_rps_save_rxhash(sk, skb->rxhash);
+               sock_rps_save_rxhash(sk, skb);
 
        if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
                goto drop;