From 6576bd87387b1e5ee24aa8fbc82212dedd091138 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Thu, 2 Nov 2023 13:49:28 -0700 Subject: [PATCH] lib: convert if_zapi_callbacks into actual hooks ...so that multiple functions can be subscribed. The create/destroy hooks are renamed to real/unreal because that's what they *actually* signal. Signed-off-by: David Lamparter --- babeld/babel_main.c | 6 ++++-- bfdd/ptm_adapter.c | 3 ++- bgpd/bgp_zebra.c | 6 ++++-- eigrpd/eigrp_interface.c | 6 ++++-- isisd/isis_circuit.c | 6 ++++-- ldpd/ldp_zebra.c | 5 ++++- lib/if.c | 34 +++++++++------------------------- lib/if.h | 23 +++++++++++++++++++---- nhrpd/nhrp_main.c | 6 ++++-- ospf6d/ospf6_interface.c | 6 ++++-- ospfd/ospf_interface.c | 6 ++++-- pbrd/pbr_main.c | 6 ++++-- pimd/pim_iface.c | 6 ++++-- ripd/rip_interface.c | 6 ++++-- ripngd/ripng_interface.c | 6 ++++-- sharpd/sharp_zebra.c | 6 ++++-- staticd/static_zebra.c | 6 ++++-- vrrpd/vrrp_zebra.c | 6 ++++-- zebra/interface.c | 5 ----- 19 files changed, 90 insertions(+), 64 deletions(-) diff --git a/babeld/babel_main.c b/babeld/babel_main.c index b6126d5b7d19..4ca649e2c227 100644 --- a/babeld/babel_main.c +++ b/babeld/babel_main.c @@ -182,8 +182,10 @@ main(int argc, char **argv) change_smoothing_half_life(BABEL_DEFAULT_SMOOTHING_HALF_LIFE); /* init some quagga's dependencies, and babeld's commands */ - if_zapi_callbacks(babel_ifp_create, babel_ifp_up, - babel_ifp_down, babel_ifp_destroy); + hook_register_prio(if_real, 0, babel_ifp_create); + hook_register_prio(if_up, 0, babel_ifp_up); + hook_register_prio(if_down, 0, babel_ifp_down); + hook_register_prio(if_unreal, 0, babel_ifp_destroy); babeld_quagga_init(); /* init zebra client's structure and it's commands */ /* this replace kernel_setup && kernel_setup_socket */ diff --git a/bfdd/ptm_adapter.c b/bfdd/ptm_adapter.c index 9e7ed114ee24..e6c2fb71f0cf 100644 --- a/bfdd/ptm_adapter.c +++ b/bfdd/ptm_adapter.c @@ -826,7 +826,8 @@ static zclient_handler *const bfd_handlers[] = { void bfdd_zclient_init(struct zebra_privs_t *bfdd_priv) { - if_zapi_callbacks(bfd_ifp_create, NULL, NULL, bfd_ifp_destroy); + hook_register_prio(if_real, 0, bfd_ifp_create); + hook_register_prio(if_unreal, 0, bfd_ifp_destroy); zclient = zclient_new(master, &zclient_options_default, bfd_handlers, array_size(bfd_handlers)); assert(zclient != NULL); diff --git a/bgpd/bgp_zebra.c b/bgpd/bgp_zebra.c index ff8a87d76b3e..381acd803e04 100644 --- a/bgpd/bgp_zebra.c +++ b/bgpd/bgp_zebra.c @@ -3442,8 +3442,10 @@ void bgp_zebra_init(struct event_loop *master, unsigned short instance) options.synchronous = true; zclient_num_connects = 0; - if_zapi_callbacks(bgp_ifp_create, bgp_ifp_up, - bgp_ifp_down, bgp_ifp_destroy); + hook_register_prio(if_real, 0, bgp_ifp_create); + hook_register_prio(if_up, 0, bgp_ifp_up); + hook_register_prio(if_down, 0, bgp_ifp_down); + hook_register_prio(if_unreal, 0, bgp_ifp_destroy); /* Set default values. */ zclient = zclient_new(master, &zclient_options_default, bgp_handlers, diff --git a/eigrpd/eigrp_interface.c b/eigrpd/eigrp_interface.c index 7bb1617c1959..fb8f47e7234b 100644 --- a/eigrpd/eigrp_interface.c +++ b/eigrpd/eigrp_interface.c @@ -202,8 +202,10 @@ struct list *eigrp_iflist; void eigrp_if_init(void) { - if_zapi_callbacks(eigrp_ifp_create, eigrp_ifp_up, - eigrp_ifp_down, eigrp_ifp_destroy); + hook_register_prio(if_real, 0, eigrp_ifp_create); + hook_register_prio(if_up, 0, eigrp_ifp_up); + hook_register_prio(if_down, 0, eigrp_ifp_down); + hook_register_prio(if_unreal, 0, eigrp_ifp_destroy); /* Initialize Zebra interface data structure. */ // hook_register_prio(if_add, 0, eigrp_if_new); hook_register_prio(if_del, 0, eigrp_if_delete_hook); diff --git a/isisd/isis_circuit.c b/isisd/isis_circuit.c index ffa6ad3e402d..3fca6b23d746 100644 --- a/isisd/isis_circuit.c +++ b/isisd/isis_circuit.c @@ -1680,6 +1680,8 @@ void isis_circuit_init(void) #else if_cmd_init_default(); #endif - if_zapi_callbacks(isis_ifp_create, isis_ifp_up, - isis_ifp_down, isis_ifp_destroy); + hook_register_prio(if_real, 0, isis_ifp_create); + hook_register_prio(if_up, 0, isis_ifp_up); + hook_register_prio(if_down, 0, isis_ifp_down); + hook_register_prio(if_unreal, 0, isis_ifp_destroy); } diff --git a/ldpd/ldp_zebra.c b/ldpd/ldp_zebra.c index 0fd5d4613c08..a9814f18f8f0 100644 --- a/ldpd/ldp_zebra.c +++ b/ldpd/ldp_zebra.c @@ -682,7 +682,10 @@ static zclient_handler *const ldp_handlers[] = { void ldp_zebra_init(struct event_loop *master) { - if_zapi_callbacks(ldp_ifp_create, ldp_ifp_up, ldp_ifp_down, ldp_ifp_destroy); + hook_register_prio(if_real, 0, ldp_ifp_create); + hook_register_prio(if_up, 0, ldp_ifp_up); + hook_register_prio(if_down, 0, ldp_ifp_down); + hook_register_prio(if_unreal, 0, ldp_ifp_destroy); /* Set default values. */ zclient = zclient_new(master, &zclient_options_default, ldp_handlers, diff --git a/lib/if.c b/lib/if.c index 48c01af882a4..775b03ef7d45 100644 --- a/lib/if.c +++ b/lib/if.c @@ -45,12 +45,11 @@ DEFINE_QOBJ_TYPE(interface); DEFINE_HOOK(if_add, (struct interface * ifp), (ifp)); DEFINE_KOOH(if_del, (struct interface * ifp), (ifp)); -static struct interface_master{ - int (*create_hook)(struct interface *ifp); - int (*up_hook)(struct interface *ifp); - int (*down_hook)(struct interface *ifp); - int (*destroy_hook)(struct interface *ifp); -} ifp_master = { 0, }; +DEFINE_HOOK(if_real, (struct interface * ifp), (ifp)); +DEFINE_KOOH(if_unreal, (struct interface * ifp), (ifp)); + +DEFINE_HOOK(if_up, (struct interface * ifp), (ifp)); +DEFINE_KOOH(if_down, (struct interface * ifp), (ifp)); /* Compare interface names, returning an integer greater than, equal to, or * less than 0, (following the strcmp convention), according to the @@ -180,14 +179,12 @@ static struct interface *if_new(struct vrf *vrf) void if_new_via_zapi(struct interface *ifp) { - if (ifp_master.create_hook) - (*ifp_master.create_hook)(ifp); + hook_call(if_real, ifp); } void if_destroy_via_zapi(struct interface *ifp) { - if (ifp_master.destroy_hook) - (*ifp_master.destroy_hook)(ifp); + hook_call(if_unreal, ifp); ifp->oldifindex = ifp->ifindex; if_set_index(ifp, IFINDEX_INTERNAL); @@ -198,14 +195,12 @@ void if_destroy_via_zapi(struct interface *ifp) void if_up_via_zapi(struct interface *ifp) { - if (ifp_master.up_hook) - (*ifp_master.up_hook)(ifp); + hook_call(if_up, ifp); } void if_down_via_zapi(struct interface *ifp) { - if (ifp_master.down_hook) - (*ifp_master.down_hook)(ifp); + hook_call(if_down, ifp); } static struct interface *if_create_name(const char *name, struct vrf *vrf) @@ -1477,17 +1472,6 @@ void if_cmd_init_default(void) if_cmd_init(if_nb_config_write); } -void if_zapi_callbacks(int (*create)(struct interface *ifp), - int (*up)(struct interface *ifp), - int (*down)(struct interface *ifp), - int (*destroy)(struct interface *ifp)) -{ - ifp_master.create_hook = create; - ifp_master.up_hook = up; - ifp_master.down_hook = down; - ifp_master.destroy_hook = destroy; -} - /* ------- Northbound callbacks ------- */ /* diff --git a/lib/if.h b/lib/if.h index f93f0e70b87a..c24983a096c4 100644 --- a/lib/if.h +++ b/lib/if.h @@ -389,6 +389,25 @@ DECLARE_QOBJ_TYPE(interface); DECLARE_HOOK(if_add, (struct interface * ifp), (ifp)); DECLARE_KOOH(if_del, (struct interface * ifp), (ifp)); +/* called (in daemons) when ZAPI tells us the interface actually exists + * (ifindex != IFINDEX_INTERNAL) + * + * WARNING: these 2 hooks NEVER CALLED inside zebra! + */ +DECLARE_HOOK(if_real, (struct interface * ifp), (ifp)); +DECLARE_KOOH(if_unreal, (struct interface * ifp), (ifp)); + +/* called (in daemons) on state changes on interfaces. Whether this is admin + * state (= pure config) or carrier state (= hardware link plugged) depends on + * zebra's "link-detect" configuration. By default, it's carrier state, so + * this won't happen until the interface actually has a link. + * + * WARNING: these 2 hooks NEVER CALLED inside zebra! + */ +DECLARE_HOOK(if_up, (struct interface * ifp), (ifp)); +DECLARE_KOOH(if_down, (struct interface * ifp), (ifp)); + + #define METRIC_MAX (~0) /* Connected address structure. */ @@ -609,10 +628,6 @@ extern void if_vty_config_start(struct vty *vty, struct interface *ifp); extern void if_vty_config_end(struct vty *vty); extern void if_cmd_init(int (*config_write)(struct vty *)); extern void if_cmd_init_default(void); -extern void if_zapi_callbacks(int (*create)(struct interface *ifp), - int (*up)(struct interface *ifp), - int (*down)(struct interface *ifp), - int (*destroy)(struct interface *ifp)); extern void if_new_via_zapi(struct interface *ifp); extern void if_up_via_zapi(struct interface *ifp); diff --git a/nhrpd/nhrp_main.c b/nhrpd/nhrp_main.c index e401f21ed485..73af78cbf0f8 100644 --- a/nhrpd/nhrp_main.c +++ b/nhrpd/nhrp_main.c @@ -155,8 +155,10 @@ int main(int argc, char **argv) nhrp_vc_init(); nhrp_packet_init(); vici_init(); - if_zapi_callbacks(nhrp_ifp_create, nhrp_ifp_up, - nhrp_ifp_down, nhrp_ifp_destroy); + hook_register_prio(if_real, 0, nhrp_ifp_create); + hook_register_prio(if_up, 0, nhrp_ifp_up); + hook_register_prio(if_down, 0, nhrp_ifp_down); + hook_register_prio(if_unreal, 0, nhrp_ifp_destroy); nhrp_zebra_init(); nhrp_shortcut_init(); diff --git a/ospf6d/ospf6_interface.c b/ospf6d/ospf6_interface.c index 90185b09bcf1..a05ff1098cb3 100644 --- a/ospf6d/ospf6_interface.c +++ b/ospf6d/ospf6_interface.c @@ -2797,8 +2797,10 @@ void ospf6_interface_init(void) { /* Install interface node. */ if_cmd_init(config_write_interface); - if_zapi_callbacks(ospf6_ifp_create, ospf6_ifp_up, ospf6_ifp_down, - ospf6_ifp_destroy); + hook_register_prio(if_real, 0, ospf6_ifp_create); + hook_register_prio(if_up, 0, ospf6_ifp_up); + hook_register_prio(if_down, 0, ospf6_ifp_down); + hook_register_prio(if_unreal, 0, ospf6_ifp_destroy); install_element(VIEW_NODE, &show_ipv6_ospf6_interface_prefix_cmd); install_element(VIEW_NODE, &show_ipv6_ospf6_interface_ifname_cmd); diff --git a/ospfd/ospf_interface.c b/ospfd/ospf_interface.c index 938d373d8b1f..808eeb50fde2 100644 --- a/ospfd/ospf_interface.c +++ b/ospfd/ospf_interface.c @@ -1562,8 +1562,10 @@ void ospf_reset_hello_timer(struct interface *ifp, struct in_addr addr, void ospf_if_init(void) { - if_zapi_callbacks(ospf_ifp_create, ospf_ifp_up, - ospf_ifp_down, ospf_ifp_destroy); + hook_register_prio(if_real, 0, ospf_ifp_create); + hook_register_prio(if_up, 0, ospf_ifp_up); + hook_register_prio(if_down, 0, ospf_ifp_down); + hook_register_prio(if_unreal, 0, ospf_ifp_destroy); /* Initialize Zebra interface data structure. */ hook_register_prio(if_add, 0, ospf_if_new_hook); diff --git a/pbrd/pbr_main.c b/pbrd/pbr_main.c index c4708d3f08fe..6699079a688d 100644 --- a/pbrd/pbr_main.c +++ b/pbrd/pbr_main.c @@ -158,8 +158,10 @@ int main(int argc, char **argv, char **envp) access_list_init(); pbr_nht_init(); pbr_map_init(); - if_zapi_callbacks(pbr_ifp_create, pbr_ifp_up, - pbr_ifp_down, pbr_ifp_destroy); + hook_register_prio(if_real, 0, pbr_ifp_create); + hook_register_prio(if_up, 0, pbr_ifp_up); + hook_register_prio(if_down, 0, pbr_ifp_down); + hook_register_prio(if_unreal, 0, pbr_ifp_destroy); pbr_zebra_init(); pbr_vrf_init(); pbr_vty_init(); diff --git a/pimd/pim_iface.c b/pimd/pim_iface.c index 5fa4715b052a..e00a3c0f9865 100644 --- a/pimd/pim_iface.c +++ b/pimd/pim_iface.c @@ -1771,8 +1771,10 @@ void pim_iface_init(void) hook_register_prio(if_add, 0, pim_if_new_hook); hook_register_prio(if_del, 0, pim_if_delete_hook); - if_zapi_callbacks(pim_ifp_create, pim_ifp_up, pim_ifp_down, - pim_ifp_destroy); + hook_register_prio(if_real, 0, pim_ifp_create); + hook_register_prio(if_up, 0, pim_ifp_up); + hook_register_prio(if_down, 0, pim_ifp_down); + hook_register_prio(if_unreal, 0, pim_ifp_destroy); } static void pim_if_membership_clear(struct interface *ifp) diff --git a/ripd/rip_interface.c b/ripd/rip_interface.c index 505290ed3134..93494549ccfe 100644 --- a/ripd/rip_interface.c +++ b/ripd/rip_interface.c @@ -1118,6 +1118,8 @@ void rip_if_init(void) /* Install interface node. */ if_cmd_init_default(); - if_zapi_callbacks(rip_ifp_create, rip_ifp_up, - rip_ifp_down, rip_ifp_destroy); + hook_register_prio(if_real, 0, rip_ifp_create); + hook_register_prio(if_up, 0, rip_ifp_up); + hook_register_prio(if_down, 0, rip_ifp_down); + hook_register_prio(if_unreal, 0, rip_ifp_destroy); } diff --git a/ripngd/ripng_interface.c b/ripngd/ripng_interface.c index 4cb4bb5ef38e..aa470aa65239 100644 --- a/ripngd/ripng_interface.c +++ b/ripngd/ripng_interface.c @@ -877,6 +877,8 @@ void ripng_if_init(void) /* Install interface node. */ if_cmd_init_default(); - if_zapi_callbacks(ripng_ifp_create, ripng_ifp_up, - ripng_ifp_down, ripng_ifp_destroy); + hook_register_prio(if_real, 0, ripng_ifp_create); + hook_register_prio(if_up, 0, ripng_ifp_up); + hook_register_prio(if_down, 0, ripng_ifp_down); + hook_register_prio(if_unreal, 0, ripng_ifp_destroy); } diff --git a/sharpd/sharp_zebra.c b/sharpd/sharp_zebra.c index c095fec17b20..30bf4f30e195 100644 --- a/sharpd/sharp_zebra.c +++ b/sharpd/sharp_zebra.c @@ -1077,8 +1077,10 @@ void sharp_zebra_init(void) { struct zclient_options opt = {.receive_notify = true}; - if_zapi_callbacks(sharp_ifp_create, sharp_ifp_up, sharp_ifp_down, - sharp_ifp_destroy); + hook_register_prio(if_real, 0, sharp_ifp_create); + hook_register_prio(if_up, 0, sharp_ifp_up); + hook_register_prio(if_down, 0, sharp_ifp_down); + hook_register_prio(if_unreal, 0, sharp_ifp_destroy); zclient = zclient_new(master, &opt, sharp_handlers, array_size(sharp_handlers)); diff --git a/staticd/static_zebra.c b/staticd/static_zebra.c index 6abbdadc085d..7799cbdee33c 100644 --- a/staticd/static_zebra.c +++ b/staticd/static_zebra.c @@ -542,8 +542,10 @@ void static_zebra_init(void) { struct zclient_options opt = { .receive_notify = true }; - if_zapi_callbacks(static_ifp_create, static_ifp_up, - static_ifp_down, static_ifp_destroy); + hook_register_prio(if_real, 0, static_ifp_create); + hook_register_prio(if_up, 0, static_ifp_up); + hook_register_prio(if_down, 0, static_ifp_down); + hook_register_prio(if_unreal, 0, static_ifp_destroy); zclient = zclient_new(master, &opt, static_handlers, array_size(static_handlers)); diff --git a/vrrpd/vrrp_zebra.c b/vrrpd/vrrp_zebra.c index 6d753d2e4764..10394752e0b4 100644 --- a/vrrpd/vrrp_zebra.c +++ b/vrrpd/vrrp_zebra.c @@ -183,8 +183,10 @@ static zclient_handler *const vrrp_handlers[] = { void vrrp_zebra_init(void) { - if_zapi_callbacks(vrrp_ifp_create, vrrp_ifp_up, - vrrp_ifp_down, vrrp_ifp_destroy); + hook_register_prio(if_real, 0, vrrp_ifp_create); + hook_register_prio(if_up, 0, vrrp_ifp_up); + hook_register_prio(if_down, 0, vrrp_ifp_down); + hook_register_prio(if_unreal, 0, vrrp_ifp_destroy); /* Socket for receiving updates from Zebra daemon */ zclient = zclient_new(master, &zclient_options_default, vrrp_handlers, diff --git a/zebra/interface.c b/zebra/interface.c index 1afd9d5a7d8a..1283f15b05f8 100644 --- a/zebra/interface.c +++ b/zebra/interface.c @@ -5664,11 +5664,6 @@ void zebra_if_init(void) /* Install configuration write function. */ if_cmd_init(if_config_write); install_node(&link_params_node); - /* - * This is *intentionally* setting this to NULL, signaling - * that interface creation for zebra acts differently - */ - if_zapi_callbacks(NULL, NULL, NULL, NULL); install_element(VIEW_NODE, &show_interface_cmd); install_element(VIEW_NODE, &show_interface_vrf_all_cmd);