]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib,zebra: add affinity-map configuration hooks
authorLouis Scalbert <louis.scalbert@6wind.com>
Wed, 9 Nov 2022 14:17:19 +0000 (15:17 +0100)
committerLouis Scalbert <louis.scalbert@6wind.com>
Fri, 10 Feb 2023 12:52:01 +0000 (13:52 +0100)
Add affinity-map hooks to check the utilization of affinity-map in
link-params before its deletion and to update link-params when the
affinity-map bit-position is updated.

Signed-off-by: Louis Scalbert <louis.scalbert@6wind.com>
lib/affinitymap.c
lib/affinitymap.h
lib/affinitymap_northbound.c
zebra/subdir.am
zebra/zebra_affinitymap.c [new file with mode: 0644]
zebra/zebra_affinitymap.h [new file with mode: 0644]
zebra/zebra_vty.c

index 62c9c0df8c7d5ef3c2d167e76c469c4a4519a9e6..17e1b2cc01692fdf9df79a5c30004023a9fe1456 100644 (file)
@@ -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;
+}
index 323439c7435c51f8f009de70fa64b34ecfc38566..19edf5a269ea72cc4e8ede9ff43159180ac92245 100644 (file)
@@ -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);
 
index 5082a8b82d125637547c9f42d732041d5ee54849..331075f5c111fa08bb075c30578c29c2c64197d8 100644 (file)
@@ -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;
        }
index 5c4a87b93427a6cb6f5731c699d43720b244cc47..00f7d529943ec94229111ff153f39817785f0748 100644 (file)
@@ -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 (file)
index 0000000..ae0f9a8
--- /dev/null
@@ -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 <zebra.h>
+#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 (file)
index 0000000..5ddc998
--- /dev/null
@@ -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
index 15ae47dfed55c7cd30f94ca6f1649d00e3bdffec..b013e590158962b422b54aafe658b74a87a4f1ed 100644 (file)
@@ -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);