diff --git a/lib/affinitymap.c b/lib/affinitymap.c index 62c9c0df8c..17e1b2cc01 100644 --- a/lib/affinitymap.c +++ b/lib/affinitymap.c @@ -47,7 +47,7 @@ DEFINE_MTYPE_STATIC(LIB, AFFINITY_MAP_INDEX, "Affinity map index"); DEFINE_QOBJ_TYPE(affinity_maps); DEFINE_QOBJ_TYPE(affinity_map); -struct affinity_maps affinity_map_master = {NULL}; +struct affinity_maps affinity_map_master = {NULL, NULL, NULL, NULL}; static void affinity_map_free(struct affinity_map *map) { @@ -120,3 +120,54 @@ char *affinity_map_name_get(int pos) return map->name; return NULL; } + +bool affinity_map_check_use_hook(const char *affmap_name) +{ + if (affinity_map_master.check_use_hook) + return (*affinity_map_master.check_use_hook)(affmap_name); + return false; +} + +bool affinity_map_check_update_hook(const char *affmap_name, uint16_t new_pos) +{ + if (affinity_map_master.check_update_hook) + return (*affinity_map_master.check_update_hook)(affmap_name, + new_pos); + return true; +} + +void affinity_map_update_hook(const char *affmap_name, uint16_t new_pos) +{ + struct affinity_map *map; + + if (!affinity_map_master.update_hook) + return; + + map = affinity_map_get(affmap_name); + + if (!map) + /* Affinity-map creation */ + return; + + (*affinity_map_master.update_hook)(affmap_name, map->bit_position, + new_pos); +} + + +void affinity_map_set_check_use_hook(bool (*func)(const char *affmap_name)) +{ + affinity_map_master.check_use_hook = func; +} + +void affinity_map_set_check_update_hook(bool (*func)(const char *affmap_name, + uint16_t new_pos)) +{ + affinity_map_master.check_update_hook = func; +} + +void affinity_map_set_update_hook(void (*func)(const char *affmap_name, + uint16_t old_pos, + uint16_t new_pos)) +{ + affinity_map_master.update_hook = func; +} diff --git a/lib/affinitymap.h b/lib/affinitymap.h index 323439c743..19edf5a269 100644 --- a/lib/affinitymap.h +++ b/lib/affinitymap.h @@ -50,6 +50,11 @@ DECLARE_QOBJ_TYPE(affinity_map); struct affinity_maps { struct list *maps; + bool (*check_use_hook)(const char *affmap_name); + bool (*check_update_hook)(const char *affmap_name, uint16_t new_pos); + void (*update_hook)(const char *affmap_name, uint16_t old_pos, + uint16_t new_pos); + QOBJ_FIELDS; }; DECLARE_QOBJ_TYPE(affinity_maps); @@ -61,6 +66,17 @@ void affinity_map_unset(const char *name); struct affinity_map *affinity_map_get(const char *name); char *affinity_map_name_get(const int pos); +bool affinity_map_check_use_hook(const char *affmap_name); +bool affinity_map_check_update_hook(const char *affmap_name, uint16_t new_pos); +void affinity_map_update_hook(const char *affmap_name, uint16_t new_pos); + +void affinity_map_set_check_use_hook(bool (*func)(const char *affmap_name)); +void affinity_map_set_check_update_hook(bool (*func)(const char *affmap_name, + uint16_t new_pos)); +void affinity_map_set_update_hook(void (*func)(const char *affmap_name, + uint16_t old_pos, + uint16_t new_pos)); + void cli_show_affinity_map(struct vty *vty, const struct lyd_node *dnode, bool show_defaults); diff --git a/lib/affinitymap_northbound.c b/lib/affinitymap_northbound.c index 5082a8b82d..331075f5c1 100644 --- a/lib/affinitymap_northbound.c +++ b/lib/affinitymap_northbound.c @@ -42,14 +42,20 @@ static int lib_affinity_map_destroy(struct nb_cb_destroy_args *args) { const char *name; + name = yang_dnode_get_string((const struct lyd_node *)args->dnode, + "./name"); + switch (args->event) { case NB_EV_VALIDATE: + if (!affinity_map_check_use_hook(name)) + break; + snprintf(args->errmsg, args->errmsg_len, + "affinity-map %s is used", name); + return NB_ERR_VALIDATION; case NB_EV_PREPARE: case NB_EV_ABORT: break; case NB_EV_APPLY: - name = yang_dnode_get_string( - (const struct lyd_node *)args->dnode, "./name"); affinity_map_unset(name); break; } @@ -74,17 +80,24 @@ static int lib_affinity_map_value_modify(struct nb_cb_modify_args *args) switch (args->event) { case NB_EV_VALIDATE: map_name = affinity_map_name_get(pos); - if (!map_name) - return NB_OK; - if (strncmp(map_name, name, AFFINITY_NAME_SIZE) == 0) - return NB_ERR_NO_CHANGES; - snprintf(args->errmsg, args->errmsg_len, - "bit-position is used by %s.", map_name); - return NB_ERR_VALIDATION; + if (map_name && + strncmp(map_name, name, AFFINITY_NAME_SIZE) != 0) { + snprintf(args->errmsg, args->errmsg_len, + "bit-position is used by %s.", map_name); + return NB_ERR_VALIDATION; + } + if (!affinity_map_check_update_hook(name, pos)) { + snprintf( + args->errmsg, args->errmsg_len, + "affinity-map new bit-position > 31 but is used with standard admin-groups"); + return NB_ERR_VALIDATION; + } + break; case NB_EV_PREPARE: case NB_EV_ABORT: break; case NB_EV_APPLY: + affinity_map_update_hook(name, pos); affinity_map_set(name, pos); break; } diff --git a/zebra/subdir.am b/zebra/subdir.am index 5c4a87b934..00f7d52994 100644 --- a/zebra/subdir.am +++ b/zebra/subdir.am @@ -62,6 +62,7 @@ zebra_zebra_SOURCES = \ zebra/tc_netlink.c \ zebra/tc_socket.c \ zebra/zapi_msg.c \ + zebra/zebra_affinitymap.c \ zebra/zebra_dplane.c \ zebra/zebra_errors.c \ zebra/zebra_gr.c \ @@ -145,6 +146,7 @@ noinst_HEADERS += \ zebra/table_manager.h \ zebra/tc_netlink.h \ zebra/zapi_msg.h \ + zebra/zebra_affinitymap.h \ zebra/zebra_dplane.h \ zebra/zebra_errors.h \ zebra/zebra_evpn.h \ diff --git a/zebra/zebra_affinitymap.c b/zebra/zebra_affinitymap.c new file mode 100644 index 0000000000..ae0f9a8a35 --- /dev/null +++ b/zebra/zebra_affinitymap.c @@ -0,0 +1,144 @@ +/* + * zebra affinity-map. + * + * Copyright 2022 6WIND S.A. + * + * This file is part of Free Range Routing (FRR). + * + * FRR is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * FRR is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; see the file COPYING; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include "lib/if.h" +#include "lib/vrf.h" +#include "zebra/redistribute.h" +#include "zebra/zebra_affinitymap.h" + +static bool zebra_affinity_map_check_use(const char *affmap_name) +{ + char xpath[XPATH_MAXLEN]; + struct interface *ifp; + struct vrf *vrf; + + RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { + FOR_ALL_INTERFACES (vrf, ifp) { + snprintf(xpath, sizeof(xpath), + "/frr-interface:lib/interface[name='%s']", + ifp->name); + if (!yang_dnode_exists(running_config->dnode, xpath)) + continue; + snprintf( + xpath, sizeof(xpath), + "/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinities[affinity='%s']", + ifp->name, affmap_name); + if (yang_dnode_exists(running_config->dnode, xpath)) + return true; + } + } + return false; +} + +static bool zebra_affinity_map_check_update(const char *affmap_name, + uint16_t new_pos) +{ + char xpath[XPATH_MAXLEN]; + struct interface *ifp; + struct vrf *vrf; + + /* check whether the affinity-map new bit position is upper than 31 + * but is used on an interface on which affinity-mode is standard. + * Return false if the change is not possible. + */ + if (new_pos < 32) + return true; + + RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { + FOR_ALL_INTERFACES (vrf, ifp) { + snprintf(xpath, sizeof(xpath), + "/frr-interface:lib/interface[name='%s']", + ifp->name); + if (!yang_dnode_exists(running_config->dnode, xpath)) + continue; + snprintf( + xpath, sizeof(xpath), + "/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinities[affinity='%s']", + ifp->name, affmap_name); + if (!yang_dnode_exists(running_config->dnode, xpath)) + continue; + if (yang_dnode_get_enum( + running_config->dnode, + "/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinity-mode", + ifp->name) == AFFINITY_MODE_STANDARD) + return false; + } + } + return true; +} + +static void zebra_affinity_map_update(const char *affmap_name, uint16_t old_pos, + uint16_t new_pos) +{ + struct if_link_params *iflp; + enum affinity_mode aff_mode; + char xpath[XPATH_MAXLEN]; + struct interface *ifp; + struct vrf *vrf; + + RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { + FOR_ALL_INTERFACES (vrf, ifp) { + snprintf(xpath, sizeof(xpath), + "/frr-interface:lib/interface[name='%s']", + ifp->name); + if (!yang_dnode_exists(running_config->dnode, xpath)) + continue; + snprintf( + xpath, sizeof(xpath), + "/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinities[affinity='%s']", + ifp->name, affmap_name); + if (!yang_dnode_exists(running_config->dnode, xpath)) + continue; + aff_mode = yang_dnode_get_enum( + running_config->dnode, + "/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinity-mode", + ifp->name); + iflp = if_link_params_get(ifp); + if (aff_mode == AFFINITY_MODE_EXTENDED || + aff_mode == AFFINITY_MODE_BOTH) { + admin_group_unset(&iflp->ext_admin_grp, + old_pos); + admin_group_set(&iflp->ext_admin_grp, new_pos); + } + if (aff_mode == AFFINITY_MODE_STANDARD || + aff_mode == AFFINITY_MODE_BOTH) { + iflp->admin_grp &= ~(1 << old_pos); + if (new_pos < 32) + iflp->admin_grp |= 1 << new_pos; + if (iflp->admin_grp == 0) + UNSET_PARAM(iflp, LP_ADM_GRP); + } + if (if_is_operative(ifp)) + zebra_interface_parameters_update(ifp); + } + } +} + +void zebra_affinity_map_init(void) +{ + affinity_map_init(); + + affinity_map_set_check_use_hook(zebra_affinity_map_check_use); + affinity_map_set_check_update_hook(zebra_affinity_map_check_update); + affinity_map_set_update_hook(zebra_affinity_map_update); +} diff --git a/zebra/zebra_affinitymap.h b/zebra/zebra_affinitymap.h new file mode 100644 index 0000000000..5ddc998bda --- /dev/null +++ b/zebra/zebra_affinitymap.h @@ -0,0 +1,38 @@ +/* + * Zebra affinity-map header + * + * Copyright 2022 6WIND S.A. + * + * This file is part of Free Range Routing (FRR). + * + * FRR is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * FRR is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; see the file COPYING; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __ZEBRA_AFFINITYMAP_H__ +#define __ZEBRA_AFFINITYMAP_H__ + +#include "lib/affinitymap.h" + +#ifdef __cplusplus +extern "C" { +#endif + +extern void zebra_affinity_map_init(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/zebra/zebra_vty.c b/zebra/zebra_vty.c index 15ae47dfed..b013e59015 100644 --- a/zebra/zebra_vty.c +++ b/zebra/zebra_vty.c @@ -42,6 +42,7 @@ #include "zebra/zebra_mpls.h" #include "zebra/zebra_rnh.h" #include "zebra/redistribute.h" +#include "zebra/zebra_affinitymap.h" #include "zebra/zebra_routemap.h" #include "lib/json.h" #include "lib/route_opaque.h" @@ -4497,7 +4498,7 @@ void zebra_vty_init(void) /* Route-map */ zebra_route_map_init(); - affinity_map_init(); + zebra_affinity_map_init(); install_node(&ip_node); install_node(&protocol_node);