]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: replace if_add_hook with hook_* logic
authorDavid Lamparter <equinox@opensourcerouting.org>
Tue, 8 Aug 2017 08:50:43 +0000 (10:50 +0200)
committerDavid Lamparter <equinox@opensourcerouting.org>
Tue, 15 Aug 2017 11:25:44 +0000 (13:25 +0200)
This allows modules to register their own additional hooks on interface
creation/deletion.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
babeld/babel_interface.c
bgpd/bgp_main.c
eigrpd/eigrp_interface.c
isisd/isis_circuit.c
lib/if.c
lib/if.h
nhrpd/nhrp_interface.c
ospfd/ospf_interface.c
ripd/rip_interface.c
ripngd/ripng_interface.c
zebra/interface.c

index 1ae33b3a27af7a6b49f077ca8f913e29de7ca5d5..9fa32ee6fa6f8acf2b1e03d24e744e9f0b391f03 100644 (file)
@@ -1256,8 +1256,8 @@ void
 babel_if_init ()
 {
     /* initialize interface list */
-    if_add_hook (IF_NEW_HOOK,    babel_if_new_hook);
-    if_add_hook (IF_DELETE_HOOK, babel_if_delete_hook);
+    hook_register_prio(if_add, 0, babel_if_new_hook);
+    hook_register_prio(if_del, 0, babel_if_delete_hook);
 
     babel_enable_if = vector_init (1);
 
index a80dff271d609e5eec5411091ca6b9873238d067..d1359402dfbf81e6a6ef2d8f2fd6e0fa4725b0b7 100644 (file)
@@ -178,9 +178,6 @@ static __attribute__((__noreturn__)) void bgp_exit(int status)
 
        bgp_close();
 
-       if (retain_mode)
-               if_add_hook(IF_DELETE_HOOK, NULL);
-
        /* reverse bgp_master_init */
        for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
                bgp_delete(bgp);
index 7f05e147038c17778d1dd79b32a51e70e2aa6a2a..22b6fa394f8a43070fad0b0115dbb93b69c895cb 100644 (file)
@@ -150,8 +150,8 @@ struct list *eigrp_iflist;
 void eigrp_if_init()
 {
        /* Initialize Zebra interface data structure. */
-       if_add_hook(IF_NEW_HOOK, eigrp_if_new_hook);
-       if_add_hook(IF_DELETE_HOOK, eigrp_if_delete_hook);
+       hook_register_prio(if_add, 0, eigrp_if_new_hook);
+       hook_register_prio(if_del, 0, eigrp_if_delete_hook);
 }
 
 int eigrp_if_new_hook(struct interface *ifp)
index 9622dcdbc4e480c424a7f20fb231920385cfe35d..a1aa87e396d15ecbfc7333a90eca2c6c12304a4a 100644 (file)
@@ -1336,8 +1336,8 @@ int isis_if_delete_hook(struct interface *ifp)
 void isis_circuit_init()
 {
        /* Initialize Zebra interface data structure */
-       if_add_hook(IF_NEW_HOOK, isis_if_new_hook);
-       if_add_hook(IF_DELETE_HOOK, isis_if_delete_hook);
+       hook_register_prio(if_add, 0, isis_if_new_hook);
+       hook_register_prio(if_del, 0, isis_if_delete_hook);
 
        /* Install interface node */
        install_node(&interface_node, isis_interface_config_write);
index 4e4534851cccd28d31c5c54d5c52c2f6d7de325f..43c382beaaa4cb7107720553d5a19b3acc066463 100644 (file)
--- a/lib/if.c
+++ b/lib/if.c
@@ -42,17 +42,12 @@ DEFINE_MTYPE_STATIC(LIB, IF_LINK_PARAMS, "Informational Link Parameters")
 
 DEFINE_QOBJ_TYPE(interface)
 
+DEFINE_HOOK(if_add, (struct interface *ifp), (ifp))
+DEFINE_KOOH(if_del, (struct interface *ifp), (ifp))
+
 /* List of interfaces in only the default VRF */
 int ptm_enable = 0;
 
-/* One for each program.  This structure is needed to store hooks. */
-struct if_master {
-       int (*if_new_hook)(struct interface *);
-       int (*if_delete_hook)(struct interface *);
-} if_master = {
-       0,
-};
-
 /* Compare interface names, returning an integer greater than, equal to, or
  * less than 0, (following the strcmp convention), according to the
  * relationship between ifp1 and ifp2.  Interface names consist of an
@@ -150,10 +145,7 @@ struct interface *if_create(const char *name, int namelen, vrf_id_t vrf_id)
        SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
 
        QOBJ_REG(ifp, interface);
-
-       if (if_master.if_new_hook)
-               (*if_master.if_new_hook)(ifp);
-
+        hook_call(if_add, ifp);
        return ifp;
 }
 
@@ -182,9 +174,7 @@ void if_update_to_new_vrf(struct interface *ifp, vrf_id_t vrf_id)
 /* Delete interface structure. */
 void if_delete_retain(struct interface *ifp)
 {
-       if (if_master.if_delete_hook)
-               (*if_master.if_delete_hook)(ifp);
-
+        hook_call(if_del, ifp);
        QOBJ_UNREG(ifp);
 
        /* Free connected address list */
@@ -209,21 +199,6 @@ void if_delete(struct interface *ifp)
        XFREE(MTYPE_IF, ifp);
 }
 
-/* Add hook to interface master. */
-void if_add_hook(int type, int (*func)(struct interface *ifp))
-{
-       switch (type) {
-       case IF_NEW_HOOK:
-               if_master.if_new_hook = func;
-               break;
-       case IF_DELETE_HOOK:
-               if_master.if_delete_hook = func;
-               break;
-       default:
-               break;
-       }
-}
-
 /* Interface existance check by index. */
 struct interface *if_lookup_by_index(ifindex_t ifindex, vrf_id_t vrf_id)
 {
index f80ac19179527e2bf57da2d165cd6c1d90f2a06d..a592e0ff857d25ebfd34b6f8c655ecf4fe7919b0 100644 (file)
--- a/lib/if.h
+++ b/lib/if.h
@@ -25,6 +25,7 @@
 #include "linklist.h"
 #include "memory.h"
 #include "qobj.h"
+#include "hook.h"
 
 DECLARE_MTYPE(IF)
 DECLARE_MTYPE(CONNECTED_LABEL)
@@ -283,6 +284,17 @@ struct interface {
 };
 DECLARE_QOBJ_TYPE(interface)
 
+/* called from the library code whenever interfaces are created/deleted
+ * note: interfaces may not be fully realized at that point; also they
+ * may not exist in the system (ifindex = IFINDEX_INTERNAL)
+ *
+ * priority values are important here, daemons should be at 0 while modules
+ * can use 1000+ so they run after the daemon has initialised daemon-specific
+ * interface data
+ */
+DECLARE_HOOK(if_add, (struct interface *ifp), (ifp))
+DECLARE_KOOH(if_del, (struct interface *ifp), (ifp))
+
 /* Connected address structure. */
 struct connected {
        /* Attached interface. */
@@ -355,10 +367,6 @@ struct nbr_connected {
                 ? (C)->destination                                            \
                 : (C)->address)
 
-/* Interface hook sort. */
-#define IF_NEW_HOOK   0
-#define IF_DELETE_HOOK 1
-
 /* There are some interface flags which are only supported by some
    operating system. */
 
@@ -442,7 +450,6 @@ extern int if_is_loopback(struct interface *);
 extern int if_is_broadcast(struct interface *);
 extern int if_is_pointopoint(struct interface *);
 extern int if_is_multicast(struct interface *);
-extern void if_add_hook(int, int (*)(struct interface *));
 extern void if_init(struct list **);
 extern void if_cmd_init(void);
 extern void if_terminate(struct list **);
index 58ad1675498da2c1af91be0fb8d0b584ea17eb3b..a46962c91af37a41e073e3e91d2eab1fe344ad49 100644 (file)
@@ -48,8 +48,8 @@ static int nhrp_if_delete_hook(struct interface *ifp)
 
 void nhrp_interface_init(void)
 {
-       if_add_hook(IF_NEW_HOOK,    nhrp_if_new_hook);
-       if_add_hook(IF_DELETE_HOOK, nhrp_if_delete_hook);
+       hook_register_prio(if_add, 0, nhrp_if_new_hook);
+       hook_register_prio(if_del, 0, nhrp_if_delete_hook);
 }
 
 void nhrp_interface_update_mtu(struct interface *ifp, afi_t afi)
index 4ea8ec26f2acf892d923a212e60f361484b6007a..54639afd6c852cf4cf2bb52f8ddc6f14eec7d3d0 100644 (file)
@@ -1163,6 +1163,6 @@ void ospf_if_init()
 {
        /* Initialize Zebra interface data structure. */
        om->iflist = vrf_iflist(VRF_DEFAULT);
-       if_add_hook(IF_NEW_HOOK, ospf_if_new_hook);
-       if_add_hook(IF_DELETE_HOOK, ospf_if_delete_hook);
+       hook_register_prio(if_add, 0, ospf_if_new_hook);
+       hook_register_prio(if_del, 0, ospf_if_delete_hook);
 }
index a1704711230dc01b31b5b7ed1e9fa7f7cece36a7..00b6d1cadd2aaafc3ed15d9e7b523fc2583eaf3c 100644 (file)
@@ -1877,8 +1877,8 @@ static int rip_interface_delete_hook(struct interface *ifp)
 void rip_if_init(void)
 {
        /* Default initial size of interface vector. */
-       if_add_hook(IF_NEW_HOOK, rip_interface_new_hook);
-       if_add_hook(IF_DELETE_HOOK, rip_interface_delete_hook);
+       hook_register_prio(if_add, 0, rip_interface_new_hook);
+       hook_register_prio(if_del, 0, rip_interface_delete_hook);
 
        /* RIP network init. */
        rip_enable_interface = vector_init(1);
index c762d8ace729cadee9c7eb2c3105101352bea045..02fab6825483b48a77449e905063ec0e7c9ee78a 100644 (file)
@@ -1121,8 +1121,8 @@ static struct cmd_node interface_node = {
 void ripng_if_init()
 {
        /* Interface initialize. */
-       if_add_hook(IF_NEW_HOOK, ripng_if_new_hook);
-       if_add_hook(IF_DELETE_HOOK, ripng_if_delete_hook);
+       hook_register_prio(if_add, 0, ripng_if_new_hook);
+       hook_register_prio(if_del, 0, ripng_if_delete_hook);
 
        /* RIPng enable network init. */
        ripng_enable_network = route_table_init();
index c4d0363994f51c5169f06bd45fe789259edd3c67..48158c82c48e650db669c45b9fe6eb7fa48e0e18 100644 (file)
@@ -2930,8 +2930,8 @@ static int if_config_write(struct vty *vty)
 void zebra_if_init(void)
 {
        /* Initialize interface and new hook. */
-       if_add_hook(IF_NEW_HOOK, if_zebra_new_hook);
-       if_add_hook(IF_DELETE_HOOK, if_zebra_delete_hook);
+       hook_register_prio(if_add, 0, if_zebra_new_hook);
+       hook_register_prio(if_del, 0, if_zebra_delete_hook);
 
        /* Install configuration write function. */
        install_node(&interface_node, if_config_write);