Move call to call_fib_entry_notifiers for new IPv4 routes to right
before the call to fib_insert_alias. At this point the only remaining
failure path is memory allocations in fib_insert_node. Handle that
very unlikely failure with a call to call_fib_entry_notifiers to
tell drivers about it.

At this point notifier handlers can decide the fate of the new route
with a clean path to delete the potential new entry if the notifier
returns non-0.

Signed-off-by: David Ahern <d...@cumulusnetworks.com>
---
 net/ipv4/fib_trie.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
index fac0b73e24d1..67116233e2bc 100644
--- a/net/ipv4/fib_trie.c
+++ b/net/ipv4/fib_trie.c
@@ -1065,6 +1065,9 @@ static int fib_insert_node(struct trie *t, struct 
key_vector *tp,
        return -ENOMEM;
 }
 
+/* fib notifier for ADD is sent before calling fib_insert_alias with
+ * the expectation that the only possible failure ENOMEM
+ */
 static int fib_insert_alias(struct trie *t, struct key_vector *tp,
                            struct key_vector *l, struct fib_alias *new,
                            struct fib_alias *fa, t_key key)
@@ -1263,21 +1266,32 @@ int fib_table_insert(struct net *net, struct fib_table 
*tb,
        new_fa->tb_id = tb->tb_id;
        new_fa->fa_default = -1;
 
+       err = call_fib_entry_notifiers(net, event, key, plen, new_fa, extack);
+       if (err)
+               goto out_free_new_fa;
+
        /* Insert new entry to the list. */
        err = fib_insert_alias(t, tp, l, new_fa, fa, key);
        if (err)
-               goto out_free_new_fa;
+               goto out_fib_notif;
 
        if (!plen)
                tb->tb_num_default++;
 
        rt_cache_flush(cfg->fc_nlinfo.nl_net);
-       call_fib_entry_notifiers(net, event, key, plen, new_fa, extack);
        rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, new_fa->tb_id,
                  &cfg->fc_nlinfo, nlflags);
 succeeded:
        return 0;
 
+out_fib_notif:
+       /* notifier was sent that entry would be added to trie, but
+        * the add failed and need to recover. Only failure for
+        * fib_insert_alias is ENOMEM.
+        */
+       NL_SET_ERR_MSG(extack, "Failed to insert route into trie");
+       call_fib_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, key,
+                                plen, new_fa, NULL);
 out_free_new_fa:
        kmem_cache_free(fn_alias_kmem, new_fa);
 out:
-- 
2.11.0

Reply via email to