From: David Ahern <dsah...@gmail.com>

Be optimistic about re-using a fib_info when nexthop id is given and
the route does not use metrics. Avoids a memory allocation which in
most cases is expected to be freed anyways.

Signed-off-by: David Ahern <dsah...@gmail.com>
---
 net/ipv4/fib_semantics.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c
index 0ddf14512bb3..e4411cd5514b 100644
--- a/net/ipv4/fib_semantics.c
+++ b/net/ipv4/fib_semantics.c
@@ -316,6 +316,19 @@ static inline unsigned int fib_devindex_hashfn(unsigned 
int val)
                (val >> (DEVINDEX_HASHBITS * 2))) & mask;
 }
 
+static inline unsigned int fib_info_hashfn_cfg(const struct fib_config *cfg)
+{
+       unsigned int mask = (fib_info_hash_size - 1);
+       unsigned int val = 0;
+
+       val ^= (cfg->fc_protocol << 8) | cfg->fc_scope;
+       val ^= (__force u32)cfg->fc_prefsrc;
+       val ^= cfg->fc_priority;
+       val ^= fib_devindex_hashfn(cfg->fc_nh_id);
+
+       return (val ^ (val >> 7) ^ (val >> 12)) & mask;
+}
+
 static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
 {
        unsigned int mask = (fib_info_hash_size - 1);
@@ -334,6 +347,35 @@ static inline unsigned int fib_info_hashfn(const struct 
fib_info *fi)
        return (val ^ (val >> 7) ^ (val >> 12)) & mask;
 }
 
+/* no metrics, only nexthop id */
+static struct fib_info *fib_find_info_nh(struct net *net,
+                                        const struct fib_config *cfg)
+{
+       struct hlist_head *head;
+       struct fib_info *fi;
+       unsigned int hash;
+
+       hash = fib_info_hashfn_cfg(cfg);
+       head = &fib_info_hash[hash];
+
+       hlist_for_each_entry(fi, head, fib_hash) {
+               if (!net_eq(fi->fib_net, net))
+                       continue;
+               if (!fi->nh || fi->nh->id != cfg->fc_nh_id)
+                       continue;
+               if (cfg->fc_protocol == fi->fib_protocol &&
+                   cfg->fc_scope == fi->fib_scope &&
+                   cfg->fc_prefsrc == fi->fib_prefsrc &&
+                   cfg->fc_priority == fi->fib_priority &&
+                   cfg->fc_type == fi->fib_type &&
+                   cfg->fc_table == fi->fib_tb_id &&
+                   !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK))
+                       return fi;
+       }
+
+       return NULL;
+}
+
 static struct fib_info *fib_find_info(const struct fib_info *nfi)
 {
        struct hlist_head *head;
@@ -1154,6 +1196,12 @@ struct fib_info *fib_create_info(struct fib_config *cfg,
                        goto err_inval;
                }
 
+               if (!cfg->fc_mx) {
+                       fi = fib_find_info_nh(net, cfg);
+                       if (fi)
+                               return fi;
+               }
+
                nh = nexthop_find_by_id(net, cfg->fc_nh_id);
                if (!nh) {
                        NL_SET_ERR_MSG(extack,
-- 
2.11.0

Reply via email to