*: call if_init()/if_terminate() from vrf_init()/vrf_terminate()

Later, an interface will belong to a specific VRF, and the interface
initialization will be a part of the VRF initialization. So now call
if_init() from vrf_init(), and if_terminate() from vrf_terminate().

Daemons have the according changes:
- if if_init() was called or "iflist" was initialized, now call
  vrf_init() instead;
- if if_terminate() was called or "iflist" was destroyed, now call
  vrf_terminate() instead.

Signed-off-by: Feng Lu <lu.feng@6wind.com>
Reviewed-by: Alain Ritoux <alain.ritoux@6wind.com>
Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
Acked-by: Vincent JARDIN <vincent.jardin@6wind.com>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
diff --git a/bgpd/bgp_main.c b/bgpd/bgp_main.c
index 5026b5e..ad4de79 100644
--- a/bgpd/bgp_main.c
+++ b/bgpd/bgp_main.c
@@ -36,6 +36,7 @@
 #include "filter.h"
 #include "plist.h"
 #include "stream.h"
+#include "vrf.h"
 
 #include "bgpd/bgpd.h"
 #include "bgpd/bgp_attr.h"
@@ -248,17 +249,14 @@
   /* reverse bgp_zebra_init/if_init */
   if (retain_mode)
     if_add_hook (IF_DELETE_HOOK, NULL);
-  for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
+  for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
     {
       struct listnode *c_node, *c_nnode;
       struct connected *c;
 
       for (ALL_LIST_ELEMENTS (ifp->connected, c_node, c_nnode, c))
         bgp_connected_delete (c);
-
-      if_delete (ifp);
     }
-  list_free (iflist);
 
   /* reverse bgp_attr_init */
   bgp_attr_finish ();
@@ -293,6 +291,7 @@
   /* reverse community_list_init */
   community_list_terminate (bgp_clist);
 
+  vrf_terminate ();
   cmd_terminate ();
   vty_terminate ();
   if (zclient)
@@ -427,6 +426,7 @@
   cmd_init (1);
   vty_init (master);
   memory_init ();
+  vrf_init ();
 
   /* BGP related initialization.  */
   bgp_init ();
diff --git a/bgpd/bgp_zebra.c b/bgpd/bgp_zebra.c
index 8ae7f46..13f71de 100644
--- a/bgpd/bgp_zebra.c
+++ b/bgpd/bgp_zebra.c
@@ -1096,8 +1096,5 @@
   zclient->ipv6_route_delete = zebra_read_ipv6;
 #endif /* HAVE_IPV6 */
 
-  /* Interface related init. */
-  if_init ();
-
   bgp_nexthop_buf = stream_new(BGP_NEXTHOP_BUF_SIZE);
 }
diff --git a/isisd/isis_circuit.c b/isisd/isis_circuit.c
index fdff819..2ef43cc 100644
--- a/isisd/isis_circuit.c
+++ b/isisd/isis_circuit.c
@@ -2739,7 +2739,6 @@
 isis_circuit_init ()
 {
   /* Initialize Zebra interface data structure */
-  if_init ();
   if_add_hook (IF_NEW_HOOK, isis_if_new_hook);
   if_add_hook (IF_DELETE_HOOK, isis_if_delete_hook);
 
diff --git a/isisd/isis_main.c b/isisd/isis_main.c
index 283b7ea..60ecb75 100644
--- a/isisd/isis_main.c
+++ b/isisd/isis_main.c
@@ -35,6 +35,7 @@
 #include "sigevent.h"
 #include "filter.h"
 #include "zclient.h"
+#include "vrf.h"
 
 #include "isisd/dict.h"
 #include "include-netbsd/iso.h"
@@ -330,6 +331,7 @@
   vty_init (master);
   memory_init ();
   access_list_init();
+  vrf_init ();
   isis_init ();
   isis_circuit_init ();
   isis_spf_cmds_init ();
diff --git a/lib/vrf.c b/lib/vrf.c
index 3ccbb99..51f9e37 100644
--- a/lib/vrf.c
+++ b/lib/vrf.c
@@ -22,6 +22,7 @@
 
 #include <zebra.h>
 
+#include "if.h"
 #include "vrf.h"
 #include "prefix.h"
 #include "table.h"
@@ -249,6 +250,8 @@
 
   /* Set the default VRF name. */
   default_vrf->name = XSTRDUP (MTYPE_VRF_NAME, "Default-IP-Routing-Table");
+
+  if_init ();
 }
 
 /* Terminate VRF module. */
@@ -264,5 +267,7 @@
 
   route_table_finish (vrf_table);
   vrf_table = NULL;
+
+  if_terminate ();
 }
 
diff --git a/ospf6d/ospf6_main.c b/ospf6d/ospf6_main.c
index 4f6d9e5..1afe84a 100644
--- a/ospf6d/ospf6_main.c
+++ b/ospf6d/ospf6_main.c
@@ -35,6 +35,7 @@
 #include "privs.h"
 #include "sigevent.h"
 #include "zclient.h"
+#include "vrf.h"
 
 #include "ospf6d.h"
 #include "ospf6_top.h"
@@ -150,7 +151,7 @@
   ospf6_asbr_terminate ();
   ospf6_lsa_terminate ();
 
-  if_terminate ();
+  vrf_terminate ();
   vty_terminate ();
   cmd_terminate ();
 
@@ -318,7 +319,7 @@
   cmd_init (1);
   vty_init (master);
   memory_init ();
-  if_init ();
+  vrf_init ();
   access_list_init ();
   prefix_list_init ();
 
diff --git a/ospfd/ospf_interface.c b/ospfd/ospf_interface.c
index 0f02cc8..07c3fe3 100644
--- a/ospfd/ospf_interface.c
+++ b/ospfd/ospf_interface.c
@@ -1254,7 +1254,6 @@
 ospf_if_init ()
 {
   /* Initialize Zebra interface data structure. */
-  if_init ();
   om->iflist = iflist;
   if_add_hook (IF_NEW_HOOK, ospf_if_new_hook);
   if_add_hook (IF_DELETE_HOOK, ospf_if_delete_hook);
diff --git a/ospfd/ospf_main.c b/ospfd/ospf_main.c
index 96dfd57..826fc98 100644
--- a/ospfd/ospf_main.c
+++ b/ospfd/ospf_main.c
@@ -39,6 +39,7 @@
 #include "privs.h"
 #include "sigevent.h"
 #include "zclient.h"
+#include "vrf.h"
 
 #include "ospfd/ospfd.h"
 #include "ospfd/ospf_interface.h"
@@ -290,6 +291,7 @@
   debug_init ();
   vty_init (master);
   memory_init ();
+  vrf_init ();
 
   access_list_init ();
   prefix_list_init ();
diff --git a/pimd/pim_iface.c b/pimd/pim_iface.c
index ecf9ef6..dc3e9a2 100644
--- a/pimd/pim_iface.c
+++ b/pimd/pim_iface.c
@@ -43,11 +43,6 @@
 
 static void pim_if_igmp_join_del_all(struct interface *ifp);
 
-void pim_if_init()
-{
-  if_init();
-}
-
 static void *if_list_clean(struct pim_interface *pim_ifp)
 {
   if (pim_ifp->igmp_join_list) {
diff --git a/pimd/pim_iface.h b/pimd/pim_iface.h
index 4b06b9f..8806fdd 100644
--- a/pimd/pim_iface.h
+++ b/pimd/pim_iface.h
@@ -108,8 +108,6 @@
   ((pim_ifp)->pim_hello_period * 7 / 2) : \
   ((pim_ifp)->pim_default_holdtime))
 
-void pim_if_init(void);
-
 struct pim_interface *pim_if_new(struct interface *ifp, int igmp, int pim);
 void                  pim_if_delete(struct interface *ifp);
 void pim_if_addr_add(struct connected *ifc);
diff --git a/pimd/pim_main.c b/pimd/pim_main.c
index b57f881..63dd636 100644
--- a/pimd/pim_main.c
+++ b/pimd/pim_main.c
@@ -31,6 +31,7 @@
 #include <signal.h>
 
 #include "memory.h"
+#include "vrf.h"
 #include "filter.h"
 #include "vty.h"
 #include "sigevent.h"
@@ -203,6 +204,7 @@
   cmd_init(1);
   vty_init(master);
   memory_init();
+  vrf_init();
   access_list_init();
   pim_init();
 
diff --git a/pimd/pimd.c b/pimd/pimd.c
index 855defc..78c3ff5 100644
--- a/pimd/pimd.c
+++ b/pimd/pimd.c
@@ -24,6 +24,7 @@
 
 #include "log.h"
 #include "memory.h"
+#include "vrf.h"
 
 #include "pimd.h"
 #include "pim_cmd.h"
@@ -130,12 +131,12 @@
   qpim_infinite_assert_metric.route_metric      = PIM_ASSERT_ROUTE_METRIC_MAX;
   qpim_infinite_assert_metric.ip_address        = qpim_inaddr_any;
 
-  pim_if_init();
   pim_cmd_init();
   pim_ssmpingd_init();
 }
 
 void pim_terminate()
 {
+  vrf_terminate();
   pim_free();
 }
diff --git a/ripd/rip_interface.c b/ripd/rip_interface.c
index 35685a7..f26ef48 100644
--- a/ripd/rip_interface.c
+++ b/ripd/rip_interface.c
@@ -2055,7 +2055,6 @@
 rip_if_init (void)
 {
   /* Default initial size of interface vector. */
-  if_init();
   if_add_hook (IF_NEW_HOOK, rip_interface_new_hook);
   if_add_hook (IF_DELETE_HOOK, rip_interface_delete_hook);
   
diff --git a/ripd/rip_main.c b/ripd/rip_main.c
index e81e61b..95b1f6d 100644
--- a/ripd/rip_main.c
+++ b/ripd/rip_main.c
@@ -33,6 +33,7 @@
 #include "privs.h"
 #include "sigevent.h"
 #include "zclient.h"
+#include "vrf.h"
 
 #include "ripd/ripd.h"
 
@@ -280,6 +281,7 @@
   vty_init (master);
   memory_init ();
   keychain_init ();
+  vrf_init ();
 
   /* RIP related initialization. */
   rip_init ();
diff --git a/ripngd/ripng_interface.c b/ripngd/ripng_interface.c
index 3588463..c7865d1 100644
--- a/ripngd/ripng_interface.c
+++ b/ripngd/ripng_interface.c
@@ -35,6 +35,7 @@
 #include "table.h"
 #include "thread.h"
 #include "privs.h"
+#include "vrf.h"
 
 #include "ripngd/ripngd.h"
 #include "ripngd/ripng_debug.h"
@@ -1177,7 +1178,6 @@
 ripng_if_init ()
 {
   /* Interface initialize. */
-  iflist = list_new ();
   if_add_hook (IF_NEW_HOOK, ripng_if_new_hook);
   if_add_hook (IF_DELETE_HOOK, ripng_if_delete_hook);
 
diff --git a/ripngd/ripng_main.c b/ripngd/ripng_main.c
index acc980d..d8f2241 100644
--- a/ripngd/ripng_main.c
+++ b/ripngd/ripng_main.c
@@ -34,6 +34,7 @@
 #include "if.h"
 #include "privs.h"
 #include "sigevent.h"
+#include "vrf.h"
 
 #include "ripngd/ripngd.h"
 
@@ -276,6 +277,7 @@
   cmd_init (1);
   vty_init (master);
   memory_init ();
+  vrf_init ();
 
   /* RIPngd inits. */
   ripng_init ();
diff --git a/zebra/interface.c b/zebra/interface.c
index 49d40ba..3063dad 100644
--- a/zebra/interface.c
+++ b/zebra/interface.c
@@ -1634,7 +1634,6 @@
 zebra_if_init (void)
 {
   /* Initialize interface and new hook. */
-  if_init ();
   if_add_hook (IF_NEW_HOOK, if_zebra_new_hook);
   if_add_hook (IF_DELETE_HOOK, if_zebra_delete_hook);
   
diff --git a/zebra/test_main.c b/zebra/test_main.c
index a92cd61..aad616f 100644
--- a/zebra/test_main.c
+++ b/zebra/test_main.c
@@ -308,7 +308,6 @@
   cmd_init (1);
   vty_init (zebrad.master);
   memory_init ();
-  if_init();
   zebra_debug_init ();
   zebra_if_init ();
   test_cmd_init ();