ipv4: Consolidate all default route selection implementations.
authorDavid S. Miller <davem@davemloft.net>
Tue, 1 Feb 2011 00:16:50 +0000 (16:16 -0800)
committerDavid S. Miller <davem@davemloft.net>
Tue, 1 Feb 2011 00:16:50 +0000 (16:16 -0800)
Both fib_trie and fib_hash have a local implementation of
fib_table_select_default().  This is completely unnecessary
code duplication.

Since we now remember the fib_table and the head of the fib
alias list of the default route, we can implement one single
generic version of this routine.

Looking at the fib_hash implementation you may get the impression
that it's possible for there to be multiple top-level routes in
the table for the default route.  The truth is, it isn't, the
insert code will only allow one entry to exist in the zero
prefix hash table, because all keys evaluate to zero and all
keys in a hash table must be unique.

Signed-off-by: David S. Miller <davem@davemloft.net>
include/net/ip_fib.h
net/ipv4/fib_frontend.c
net/ipv4/fib_hash.c
net/ipv4/fib_semantics.c
net/ipv4/fib_trie.c
net/ipv4/route.c

index f5199b08ba532610233c578c28dd271414770e1d..819d61ca25cb98ca43f6808c2733574b026104c4 100644 (file)
@@ -158,9 +158,6 @@ extern int fib_table_delete(struct fib_table *, struct fib_config *);
 extern int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
                          struct netlink_callback *cb);
 extern int fib_table_flush(struct fib_table *table);
-extern void fib_table_select_default(struct fib_table *table,
-                                    const struct flowi *flp,
-                                    struct fib_result *res);
 extern void fib_free_table(struct fib_table *tb);
 
 
@@ -221,8 +218,7 @@ extern void         ip_fib_init(void);
 extern int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
                               struct net_device *dev, __be32 *spec_dst,
                               u32 *itag, u32 mark);
-extern void fib_select_default(struct net *net, const struct flowi *flp,
-                              struct fib_result *res);
+extern void fib_select_default(struct fib_result *res);
 
 /* Exported by fib_semantics.c */
 extern int ip_fib_check_default(__be32 gw, struct net_device *dev);
index 1d2cdd43a878b0e9d9f3b53303f00ef8b00d11b6..930768ba49afb7204cbb6d408b13bd5f3f7bc431 100644 (file)
@@ -114,21 +114,6 @@ struct fib_table *fib_get_table(struct net *net, u32 id)
 }
 #endif /* CONFIG_IP_MULTIPLE_TABLES */
 
-void fib_select_default(struct net *net,
-                       const struct flowi *flp, struct fib_result *res)
-{
-       struct fib_table *tb;
-       int table = RT_TABLE_MAIN;
-#ifdef CONFIG_IP_MULTIPLE_TABLES
-       if (res->r == NULL || res->r->action != FR_ACT_TO_TBL)
-               return;
-       table = res->r->table;
-#endif
-       tb = fib_get_table(net, table);
-       if (FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
-               fib_table_select_default(tb, flp, res);
-}
-
 static void fib_flush(struct net *net)
 {
        int flushed = 0;
index 0a88866ad1e5fd8a6a196e2d77468c066788491f..fadb6024de272b8778a4ba1ff364643f4c981596 100644 (file)
@@ -302,78 +302,6 @@ out:
        return err;
 }
 
-void fib_table_select_default(struct fib_table *tb,
-                             const struct flowi *flp, struct fib_result *res)
-{
-       int order, last_idx;
-       struct hlist_node *node;
-       struct fib_node *f;
-       struct fib_info *fi = NULL;
-       struct fib_info *last_resort;
-       struct fn_hash *t = (struct fn_hash *)tb->tb_data;
-       struct fn_zone *fz = t->fn_zones[0];
-       struct hlist_head *head;
-
-       if (fz == NULL)
-               return;
-
-       last_idx = -1;
-       last_resort = NULL;
-       order = -1;
-
-       rcu_read_lock();
-       head = rcu_dereference(fz->fz_hash);
-       hlist_for_each_entry_rcu(f, node, head, fn_hash) {
-               struct fib_alias *fa;
-
-               list_for_each_entry_rcu(fa, &f->fn_alias, fa_list) {
-                       struct fib_info *next_fi = fa->fa_info;
-
-                       if (fa->fa_scope != res->scope ||
-                           fa->fa_type != RTN_UNICAST)
-                               continue;
-
-                       if (next_fi->fib_priority > res->fi->fib_priority)
-                               break;
-                       if (!next_fi->fib_nh[0].nh_gw ||
-                           next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
-                               continue;
-
-                       fib_alias_accessed(fa);
-
-                       if (fi == NULL) {
-                               if (next_fi != res->fi)
-                                       break;
-                       } else if (!fib_detect_death(fi, order, &last_resort,
-                                               &last_idx, tb->tb_default)) {
-                               fib_result_assign(res, fi);
-                               tb->tb_default = order;
-                               goto out;
-                       }
-                       fi = next_fi;
-                       order++;
-               }
-       }
-
-       if (order <= 0 || fi == NULL) {
-               tb->tb_default = -1;
-               goto out;
-       }
-
-       if (!fib_detect_death(fi, order, &last_resort, &last_idx,
-                               tb->tb_default)) {
-               fib_result_assign(res, fi);
-               tb->tb_default = order;
-               goto out;
-       }
-
-       if (last_idx >= 0)
-               fib_result_assign(res, last_resort);
-       tb->tb_default = last_idx;
-out:
-       rcu_read_unlock();
-}
-
 /* Insert node F to FZ. */
 static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
 {
index 1bf6fb906cfc249c8c08924043c6e1adeeec85de..b15857d2b77ef8b1ce7b471bce37622f5f81a938 100644 (file)
@@ -1136,6 +1136,62 @@ int fib_sync_down_dev(struct net_device *dev, int force)
        return ret;
 }
 
+/* Must be invoked inside of an RCU protected region.  */
+void fib_select_default(struct fib_result *res)
+{
+       struct fib_info *fi = NULL, *last_resort = NULL;
+       struct list_head *fa_head = res->fa_head;
+       struct fib_table *tb = res->table;
+       int order = -1, last_idx = -1;
+       struct fib_alias *fa;
+
+       list_for_each_entry_rcu(fa, fa_head, fa_list) {
+               struct fib_info *next_fi = fa->fa_info;
+
+               if (fa->fa_scope != res->scope ||
+                   fa->fa_type != RTN_UNICAST)
+                       continue;
+
+               if (next_fi->fib_priority > res->fi->fib_priority)
+                       break;
+               if (!next_fi->fib_nh[0].nh_gw ||
+                   next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
+                       continue;
+
+               fib_alias_accessed(fa);
+
+               if (fi == NULL) {
+                       if (next_fi != res->fi)
+                               break;
+               } else if (!fib_detect_death(fi, order, &last_resort,
+                                            &last_idx, tb->tb_default)) {
+                       fib_result_assign(res, fi);
+                       tb->tb_default = order;
+                       goto out;
+               }
+               fi = next_fi;
+               order++;
+       }
+
+       if (order <= 0 || fi == NULL) {
+               tb->tb_default = -1;
+               goto out;
+       }
+
+       if (!fib_detect_death(fi, order, &last_resort, &last_idx,
+                               tb->tb_default)) {
+               fib_result_assign(res, fi);
+               tb->tb_default = order;
+               goto out;
+       }
+
+       if (last_idx >= 0)
+               fib_result_assign(res, last_resort);
+       tb->tb_default = last_idx;
+out:
+       rcu_read_unlock();
+}
+
 #ifdef CONFIG_IP_ROUTE_MULTIPATH
 
 /*
index 8cee5c8848ed5021ce497006eb0e0fa7e90c7122..16d589c70a92160762a13acb572351e76566d7e2 100644 (file)
@@ -1802,80 +1802,6 @@ void fib_free_table(struct fib_table *tb)
        kfree(tb);
 }
 
-void fib_table_select_default(struct fib_table *tb,
-                             const struct flowi *flp,
-                             struct fib_result *res)
-{
-       struct trie *t = (struct trie *) tb->tb_data;
-       int order, last_idx;
-       struct fib_info *fi = NULL;
-       struct fib_info *last_resort;
-       struct fib_alias *fa = NULL;
-       struct list_head *fa_head;
-       struct leaf *l;
-
-       last_idx = -1;
-       last_resort = NULL;
-       order = -1;
-
-       rcu_read_lock();
-
-       l = fib_find_node(t, 0);
-       if (!l)
-               goto out;
-
-       fa_head = get_fa_head(l, 0);
-       if (!fa_head)
-               goto out;
-
-       if (list_empty(fa_head))
-               goto out;
-
-       list_for_each_entry_rcu(fa, fa_head, fa_list) {
-               struct fib_info *next_fi = fa->fa_info;
-
-               if (fa->fa_scope != res->scope ||
-                   fa->fa_type != RTN_UNICAST)
-                       continue;
-
-               if (next_fi->fib_priority > res->fi->fib_priority)
-                       break;
-               if (!next_fi->fib_nh[0].nh_gw ||
-                   next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
-                       continue;
-
-               fib_alias_accessed(fa);
-
-               if (fi == NULL) {
-                       if (next_fi != res->fi)
-                               break;
-               } else if (!fib_detect_death(fi, order, &last_resort,
-                                            &last_idx, tb->tb_default)) {
-                       fib_result_assign(res, fi);
-                       tb->tb_default = order;
-                       goto out;
-               }
-               fi = next_fi;
-               order++;
-       }
-       if (order <= 0 || fi == NULL) {
-               tb->tb_default = -1;
-               goto out;
-       }
-
-       if (!fib_detect_death(fi, order, &last_resort, &last_idx,
-                               tb->tb_default)) {
-               fib_result_assign(res, fi);
-               tb->tb_default = order;
-               goto out;
-       }
-       if (last_idx >= 0)
-               fib_result_assign(res, last_resort);
-       tb->tb_default = last_idx;
-out:
-       rcu_read_unlock();
-}
-
 static int fn_trie_dump_fa(t_key key, int plen, struct list_head *fah,
                           struct fib_table *tb,
                           struct sk_buff *skb, struct netlink_callback *cb)
index b1e5d3ac34603b7acc5090fcc370aeb2b48b8b37..242a3de83fbb8fccb3eb1e6582297da2928d30d0 100644 (file)
@@ -2711,7 +2711,7 @@ static int ip_route_output_slow(struct net *net, struct rtable **rp,
        else
 #endif
        if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif)
-               fib_select_default(net, &fl, &res);
+               fib_select_default(&res);
 
        if (!fl.fl4_src)
                fl.fl4_src = FIB_RES_PREFSRC(res);