udp: Neaten and reduce size of compute_score functions
authorJoe Perches <joe@perches.com>
Tue, 2 Dec 2014 04:29:06 +0000 (20:29 -0800)
committerDavid S. Miller <davem@davemloft.net>
Tue, 9 Dec 2014 01:28:47 +0000 (20:28 -0500)
The compute_score functions are a bit difficult to read.

Neaten them a bit to reduce object sizes and make them a
bit more intelligible.

Return early to avoid indentation and avoid unnecessary
initializations.

(allyesconfig, but w/ -O2 and no profiling)

$ size net/ipv[46]/udp.o.*
   text    data     bss     dec     hex filename
  28680    1184      25   29889    74c1 net/ipv4/udp.o.new
  28756    1184      25   29965    750d net/ipv4/udp.o.old
  17600    1010       2   18612    48b4 net/ipv6/udp.o.new
  17632    1010       2   18644    48d4 net/ipv6/udp.o.old

Signed-off-by: Joe Perches <joe@perches.com>
Acked-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/ipv4/udp.c
net/ipv6/udp.c

index b2d6068..dd8e006 100644 (file)
@@ -336,38 +336,45 @@ int udp_v4_get_port(struct sock *sk, unsigned short snum)
        return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal, hash2_nulladdr);
 }
 
-static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr,
-                        unsigned short hnum,
-                        __be16 sport, __be32 daddr, __be16 dport, int dif)
+static inline int compute_score(struct sock *sk, struct net *net,
+                               __be32 saddr, unsigned short hnum, __be16 sport,
+                               __be32 daddr, __be16 dport, int dif)
 {
-       int score = -1;
+       int score;
+       struct inet_sock *inet;
 
-       if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
-                       !ipv6_only_sock(sk)) {
-               struct inet_sock *inet = inet_sk(sk);
+       if (!net_eq(sock_net(sk), net) ||
+           udp_sk(sk)->udp_port_hash != hnum ||
+           ipv6_only_sock(sk))
+               return -1;
 
-               score = (sk->sk_family == PF_INET ? 2 : 1);
-               if (inet->inet_rcv_saddr) {
-                       if (inet->inet_rcv_saddr != daddr)
-                               return -1;
-                       score += 4;
-               }
-               if (inet->inet_daddr) {
-                       if (inet->inet_daddr != saddr)
-                               return -1;
-                       score += 4;
-               }
-               if (inet->inet_dport) {
-                       if (inet->inet_dport != sport)
-                               return -1;
-                       score += 4;
-               }
-               if (sk->sk_bound_dev_if) {
-                       if (sk->sk_bound_dev_if != dif)
-                               return -1;
-                       score += 4;
-               }
+       score = (sk->sk_family == PF_INET) ? 2 : 1;
+       inet = inet_sk(sk);
+
+       if (inet->inet_rcv_saddr) {
+               if (inet->inet_rcv_saddr != daddr)
+                       return -1;
+               score += 4;
+       }
+
+       if (inet->inet_daddr) {
+               if (inet->inet_daddr != saddr)
+                       return -1;
+               score += 4;
        }
+
+       if (inet->inet_dport) {
+               if (inet->inet_dport != sport)
+                       return -1;
+               score += 4;
+       }
+
+       if (sk->sk_bound_dev_if) {
+               if (sk->sk_bound_dev_if != dif)
+                       return -1;
+               score += 4;
+       }
+
        return score;
 }
 
@@ -378,33 +385,39 @@ static inline int compute_score2(struct sock *sk, struct net *net,
                                 __be32 saddr, __be16 sport,
                                 __be32 daddr, unsigned int hnum, int dif)
 {
-       int score = -1;
+       int score;
+       struct inet_sock *inet;
+
+       if (!net_eq(sock_net(sk), net) ||
+           ipv6_only_sock(sk))
+               return -1;
 
-       if (net_eq(sock_net(sk), net) && !ipv6_only_sock(sk)) {
-               struct inet_sock *inet = inet_sk(sk);
+       inet = inet_sk(sk);
 
-               if (inet->inet_rcv_saddr != daddr)
+       if (inet->inet_rcv_saddr != daddr ||
+           inet->inet_num != hnum)
+               return -1;
+
+       score = (sk->sk_family == PF_INET) ? 2 : 1;
+
+       if (inet->inet_daddr) {
+               if (inet->inet_daddr != saddr)
                        return -1;
-               if (inet->inet_num != hnum)
+               score += 4;
+       }
+
+       if (inet->inet_dport) {
+               if (inet->inet_dport != sport)
                        return -1;
+               score += 4;
+       }
 
-               score = (sk->sk_family == PF_INET ? 2 : 1);
-               if (inet->inet_daddr) {
-                       if (inet->inet_daddr != saddr)
-                               return -1;
-                       score += 4;
-               }
-               if (inet->inet_dport) {
-                       if (inet->inet_dport != sport)
-                               return -1;
-                       score += 4;
-               }
-               if (sk->sk_bound_dev_if) {
-                       if (sk->sk_bound_dev_if != dif)
-                               return -1;
-                       score += 4;
-               }
+       if (sk->sk_bound_dev_if) {
+               if (sk->sk_bound_dev_if != dif)
+                       return -1;
+               score += 4;
        }
+
        return score;
 }
 
index 7cfb5d7..7f96432 100644 (file)
@@ -148,72 +148,85 @@ static inline int compute_score(struct sock *sk, struct net *net,
                                const struct in6_addr *daddr, __be16 dport,
                                int dif)
 {
-       int score = -1;
+       int score;
+       struct inet_sock *inet;
 
-       if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
-                       sk->sk_family == PF_INET6) {
-               struct inet_sock *inet = inet_sk(sk);
+       if (!net_eq(sock_net(sk), net) ||
+           udp_sk(sk)->udp_port_hash != hnum ||
+           sk->sk_family != PF_INET6)
+               return -1;
 
-               score = 0;
-               if (inet->inet_dport) {
-                       if (inet->inet_dport != sport)
-                               return -1;
-                       score++;
-               }
-               if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
-                       if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
-                               return -1;
-                       score++;
-               }
-               if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
-                       if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
-                               return -1;
-                       score++;
-               }
-               if (sk->sk_bound_dev_if) {
-                       if (sk->sk_bound_dev_if != dif)
-                               return -1;
-                       score++;
-               }
+       score = 0;
+       inet = inet_sk(sk);
+
+       if (inet->inet_dport) {
+               if (inet->inet_dport != sport)
+                       return -1;
+               score++;
        }
+
+       if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
+               if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
+                       return -1;
+               score++;
+       }
+
+       if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
+               if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
+                       return -1;
+               score++;
+       }
+
+       if (sk->sk_bound_dev_if) {
+               if (sk->sk_bound_dev_if != dif)
+                       return -1;
+               score++;
+       }
+
        return score;
 }
 
 #define SCORE2_MAX (1 + 1 + 1)
 static inline int compute_score2(struct sock *sk, struct net *net,
-                               const struct in6_addr *saddr, __be16 sport,
-                               const struct in6_addr *daddr, unsigned short hnum,
-                               int dif)
+                                const struct in6_addr *saddr, __be16 sport,
+                                const struct in6_addr *daddr,
+                                unsigned short hnum, int dif)
 {
-       int score = -1;
+       int score;
+       struct inet_sock *inet;
 
-       if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
-                       sk->sk_family == PF_INET6) {
-               struct inet_sock *inet = inet_sk(sk);
+       if (!net_eq(sock_net(sk), net) ||
+           udp_sk(sk)->udp_port_hash != hnum ||
+           sk->sk_family != PF_INET6)
+               return -1;
 
-               if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
+       if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
+               return -1;
+
+       score = 0;
+       inet = inet_sk(sk);
+
+       if (inet->inet_dport) {
+               if (inet->inet_dport != sport)
                        return -1;
-               score = 0;
-               if (inet->inet_dport) {
-                       if (inet->inet_dport != sport)
-                               return -1;
-                       score++;
-               }
-               if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
-                       if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
-                               return -1;
-                       score++;
-               }
-               if (sk->sk_bound_dev_if) {
-                       if (sk->sk_bound_dev_if != dif)
-                               return -1;
-                       score++;
-               }
+               score++;
        }
+
+       if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
+               if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
+                       return -1;
+               score++;
+       }
+
+       if (sk->sk_bound_dev_if) {
+               if (sk->sk_bound_dev_if != dif)
+                       return -1;
+               score++;
+       }
+
        return score;
 }
 
-
 /* called with read_rcu_lock() */
 static struct sock *udp6_lib_lookup2(struct net *net,
                const struct in6_addr *saddr, __be16 sport,