Skip to content

Commit

Permalink
lib: mgmtd: convert affinitymap to mgmtd
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hopps <[email protected]>
  • Loading branch information
choppsv1 committed Jan 16, 2024
1 parent 9a042e0 commit 161c693
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 94 deletions.
5 changes: 2 additions & 3 deletions isisd/isis_affinitymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ static bool isis_affinity_map_check_use(const char *affmap_name)
return false;
}

static void isis_affinity_map_update(const char *affmap_name, uint16_t old_pos,
static void isis_affinity_map_update(const struct lyd_node *dnode,
const char *affmap_name, uint16_t old_pos,
uint16_t new_pos)
{
struct isis *isis = isis_lookup_by_vrfid(VRF_DEFAULT);
Expand Down Expand Up @@ -88,8 +89,6 @@ static void isis_affinity_map_update(const char *affmap_name, uint16_t old_pos,

void isis_affinity_map_init(void)
{
affinity_map_init();

affinity_map_set_check_use_hook(isis_affinity_map_check_use);
affinity_map_set_update_hook(isis_affinity_map_update);
}
Expand Down
19 changes: 12 additions & 7 deletions lib/affinitymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,18 @@ bool affinity_map_check_use_hook(const char *affmap_name)
return false;
}

bool affinity_map_check_update_hook(const char *affmap_name, uint16_t new_pos)
bool affinity_map_check_update_hook(const struct lyd_node *dnode,
const char *affmap_name, uint16_t new_pos)
{
if (affinity_map_master.check_update_hook)
return (*affinity_map_master.check_update_hook)(affmap_name,
return (*affinity_map_master.check_update_hook)(dnode,
affmap_name,
new_pos);
return true;
}

void affinity_map_update_hook(const char *affmap_name, uint16_t new_pos)
void affinity_map_update_hook(const struct lyd_node *dnode,
const char *affmap_name, uint16_t new_pos)
{
struct affinity_map *map;

Expand All @@ -149,8 +152,8 @@ void affinity_map_update_hook(const char *affmap_name, uint16_t new_pos)
/* Affinity-map creation */
return;

(*affinity_map_master.update_hook)(affmap_name, map->bit_position,
new_pos);
(*affinity_map_master.update_hook)(dnode, affmap_name,
map->bit_position, new_pos);
}


Expand All @@ -159,13 +162,15 @@ 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,
void affinity_map_set_check_update_hook(bool (*func)(const struct lyd_node *dnode,
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,
void affinity_map_set_update_hook(void (*func)(const struct lyd_node *dnode,
const char *affmap_name,
uint16_t old_pos,
uint16_t new_pos))
{
Expand Down
18 changes: 12 additions & 6 deletions lib/affinitymap.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ 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,
bool (*check_update_hook)(const struct lyd_node *dnode,
const char *affmap_name, uint16_t new_pos);
void (*update_hook)(const struct lyd_node *dnode,
const char *affmap_name, uint16_t old_pos,
uint16_t new_pos);

QOBJ_FIELDS;
Expand All @@ -67,13 +69,17 @@ 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);
bool affinity_map_check_update_hook(const struct lyd_node *dnode,
const char *affmap_name, uint16_t new_pos);
void affinity_map_update_hook(const struct lyd_node *dnode,
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,
void affinity_map_set_check_update_hook(bool (*func)(const struct lyd_node *dnode,
const char *affmap_name,
uint16_t new_pos));
void affinity_map_set_update_hook(void (*func)(const char *affmap_name,
void affinity_map_set_update_hook(void (*func)(const struct lyd_node *dnode,
const char *affmap_name,
uint16_t old_pos,
uint16_t new_pos));

Expand Down
36 changes: 14 additions & 22 deletions lib/affinitymap_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@
#include "lib/affinitymap.h"
#include "lib/affinitymap_cli_clippy.c"

/* Route map node structure. */
static int affinity_map_config_write(struct vty *vty);
static struct cmd_node affinitymap_node = {
.name = "affinity-map",
.node = AFFMAP_NODE,
.prompt = "",
.config_write = affinity_map_config_write,
/* clang-format off */
const struct frr_yang_module_info frr_affinity_map_cli_info = {
.name = "frr-affinity-map",
.ignore_cfg_cbs = true,
.nodes = {
{
.xpath = "/frr-affinity-map:lib/affinity-maps/affinity-map",
.cbs.cli_show = cli_show_affinity_map,
},
{
.xpath = NULL,
},
}
};

/* max value is EXT_ADMIN_GROUP_MAX_POSITIONS - 1 */
Expand Down Expand Up @@ -75,20 +81,6 @@ DEFPY_YANG_NOSH(no_affinity_map, no_affinity_map_cmd,
return nb_cli_apply_changes(vty, NULL);
}

static int affinity_map_config_write(struct vty *vty)
{
const struct lyd_node *dnode;
int written = 0;

dnode = yang_dnode_get(running_config->dnode, "/frr-affinity-map:lib");
if (dnode) {
nb_cli_show_dnode_cmds(vty, dnode, false);
written = 1;
}

return written;
}

void cli_show_affinity_map(struct vty *vty, const struct lyd_node *dnode,
bool show_defaults __attribute__((__unused__)))
{
Expand All @@ -101,7 +93,7 @@ void cli_show_affinity_map(struct vty *vty, const struct lyd_node *dnode,
void affinity_map_init(void)
{
/* CLI commands. */
install_node(&affinitymap_node);
// install_node(&affinitymap_node);
install_element(CONFIG_NODE, &affinity_map_cmd);
install_element(CONFIG_NODE, &no_affinity_map_cmd);
}
22 changes: 7 additions & 15 deletions lib/affinitymap_northbound.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,19 @@ static int lib_affinity_map_destroy(struct nb_cb_destroy_args *args)
*/
static int lib_affinity_map_value_modify(struct nb_cb_modify_args *args)
{
const struct lyd_node *dnode = args->dnode;
const char *name;
char *map_name;
uint16_t pos;

name = yang_dnode_get_string(
(const struct lyd_node *)args->dnode->parent, "./name");
name = yang_dnode_get_string((const struct lyd_node *)dnode->parent,
"./name");

pos = yang_dnode_get_uint16(
(const struct lyd_node *)args->dnode->parent, "./value");
pos = yang_dnode_get_uint16((const struct lyd_node *)dnode->parent,
"./value");

switch (args->event) {
case NB_EV_VALIDATE:
map_name = affinity_map_name_get(pos);
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)) {
if (!affinity_map_check_update_hook(dnode, name, pos)) {
snprintf(
args->errmsg, args->errmsg_len,
"affinity-map new bit-position > 31 but is used with standard admin-groups");
Expand All @@ -97,7 +90,7 @@ static int lib_affinity_map_value_modify(struct nb_cb_modify_args *args)
case NB_EV_ABORT:
break;
case NB_EV_APPLY:
affinity_map_update_hook(name, pos);
affinity_map_update_hook(dnode, name, pos);
affinity_map_set(name, pos);
break;
}
Expand All @@ -119,7 +112,6 @@ const struct frr_yang_module_info frr_affinity_map_info = {
.cbs = {
.create = lib_affinity_map_create,
.destroy = lib_affinity_map_destroy,
.cli_show = cli_show_affinity_map,
}
},
{
Expand Down
1 change: 0 additions & 1 deletion lib/subdir.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ lib_libfrr_la_LIBADD = $(LIBCAP) $(UNWIND_LIBS) $(LIBYANG_LIBS) $(LUA_LIB) $(UST
lib_libfrr_la_SOURCES = \
lib/admin_group.c \
lib/affinitymap.c \
lib/affinitymap_cli.c \
lib/affinitymap_northbound.c \
lib/agg_table.c \
lib/atomlist.c \
Expand Down
18 changes: 14 additions & 4 deletions mgmtd/mgmt_be_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ struct mgmt_be_xpath_map {
* Each client gets their own map, but also union all the strings into the
* above map as well.
*/


static const char *const zebra_config_xpaths[] = {
"/frr-affinity-map:lib/affinity-maps/affinity-map",
"/frr-interface:lib",
"/frr-vrf:lib",
NULL,
};

#if HAVE_STATICD
static const char *const staticd_xpaths[] = {
"/frr-vrf:lib",
Expand All @@ -67,8 +76,8 @@ static const char *const staticd_xpaths[] = {
};
#endif

static const char *const *be_client_xpaths[MGMTD_BE_CLIENT_ID_MAX] = {

static const char *const *be_client_config_xpaths[MGMTD_BE_CLIENT_ID_MAX] = {
[MGMTD_BE_CLIENT_ID_ZEBRA] = zebra_config_xpaths,
#ifdef HAVE_STATICD
[MGMTD_BE_CLIENT_ID_STATICD] = staticd_xpaths,
#endif
Expand Down Expand Up @@ -190,7 +199,7 @@ static void mgmt_be_xpath_map_init(void)

FOREACH_MGMTD_BE_CLIENT_ID (id) {
/* Initialize the common config init map */
for (init = be_client_xpaths[id]; init && *init; init++) {
for (init = be_client_config_xpaths[id]; init && *init; init++) {
MGMTD_BE_ADAPTER_DBG(" - CFG XPATH: '%s'", *init);
mgmt_register_client_xpath(id, *init, true);
}
Expand Down Expand Up @@ -843,7 +852,8 @@ static bool be_is_client_interested(const char *xpath,
MGMTD_BE_ADAPTER_DBG("Checking client: %s for xpath: '%s'",
mgmt_be_client_id2name(id), xpath);

xpaths = config ? be_client_xpaths[id] : be_client_oper_xpaths[id];
xpaths = config ? be_client_config_xpaths[id]
: be_client_oper_xpaths[id];
if (xpaths) {
for (; *xpaths; xpaths++) {
if (mgmt_be_xpath_prefix(*xpaths, xpath)) {
Expand Down
14 changes: 6 additions & 8 deletions mgmtd/mgmt_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,28 +185,26 @@ static void mgmt_vrf_terminate(void)
vrf_terminate();
}

extern const struct frr_yang_module_info frr_affinity_map_cli_info;
#ifdef HAVE_STATICD
extern const struct frr_yang_module_info frr_staticd_info;
#endif


/*
* These are stub info structs that are used to load the modules used by backend
* clients into mgmtd. The modules are used by libyang in order to support
* parsing binary data returns from the backend.
*
* These are only needed if `cli_show` commands are not defined for the module.
* When `cli_show` callbacks are defined then the non-stub frr_yang_module_info
* that contains pointers to them should be included here instead.
*/
const struct frr_yang_module_info zebra_info = {
.name = "frr-zebra",
.ignore_cfg_cbs = true,
.nodes = { { .xpath = NULL } },
};

const struct frr_yang_module_info affinity_map_info = {
.name = "frr-affinity-map",
.ignore_cfg_cbs = true,
.nodes = { { .xpath = NULL } },
};

const struct frr_yang_module_info zebra_route_map_info = {
.name = "frr-zebra-route-map",
.ignore_cfg_cbs = true,
Expand All @@ -218,6 +216,7 @@ const struct frr_yang_module_info zebra_route_map_info = {
* MGMTd.
*/
static const struct frr_yang_module_info *const mgmt_yang_modules[] = {
&frr_affinity_map_cli_info,
&frr_filter_info,
&frr_interface_info,
&frr_route_map_info,
Expand All @@ -229,7 +228,6 @@ static const struct frr_yang_module_info *const mgmt_yang_modules[] = {
*/

&zebra_info,
&affinity_map_info,
&zebra_route_map_info,

#ifdef HAVE_STATICD
Expand Down
4 changes: 4 additions & 0 deletions mgmtd/mgmt_vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <zebra.h>

#include "affinitymap.h"
#include "command.h"
#include "json.h"
#include "network.h"
Expand Down Expand Up @@ -556,6 +557,9 @@ void mgmt_vty_init(void)
* backend components that are moved to new MGMTD infra
* here one by one.
*/
affinity_map_init();
if_cmd_init(NULL);

#if HAVE_STATICD
extern void static_vty_init(void);
static_vty_init();
Expand Down
4 changes: 4 additions & 0 deletions mgmtd/subdir.am
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ nodist_mgmtd_mgmtd_SOURCES += \
# end
nodist_mgmtd_libmgmt_be_nb_la_SOURCES += staticd/static_vty.c
endif

# Unconditionally YANG support
nodist_mgmtd_mgmtd_SOURCES += yang/frr-affinity-map.yang.c
nodist_mgmtd_libmgmt_be_nb_la_SOURCES += lib/affinitymap_cli.c
20 changes: 19 additions & 1 deletion tests/topotests/example_munet/test_munet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
#
# Copyright (c) 2023, LabN Consulting, L.L.C.
#
import pytest
from munet.base import get_event_loop

pytestmark = [pytest.mark.asyncio]


@pytest.fixture(scope="function")
def event_loop():
"""Create an instance of the default event loop for the session."""
loop = get_event_loop()
try:
# logging.info("event_loop_fixture: yielding with new event loop watcher")
yield loop
finally:
loop.close()


async def test_native_test(unet):
o = unet.hosts["r1"].cmd_nostatus("ip addr")
# o = unet.hosts["r1"].cmd_nostatus("ip addr")
o = "Hello World"
print(o)
4 changes: 2 additions & 2 deletions vtysh/vtysh.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ extern struct event_loop *master;
VTYSH_SHARPD | VTYSH_PBRD | VTYSH_STATICD | VTYSH_BFDD | \
VTYSH_FABRICD | VTYSH_VRRPD | VTYSH_PATHD | VTYSH_MGMTD
#define VTYSH_ACL VTYSH_BFDD|VTYSH_BABELD|VTYSH_BGPD|VTYSH_EIGRPD|VTYSH_ISISD|VTYSH_FABRICD|VTYSH_LDPD|VTYSH_NHRPD|VTYSH_OSPF6D|VTYSH_OSPFD|VTYSH_PBRD|VTYSH_PIMD|VTYSH_PIM6D|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_VRRPD|VTYSH_ZEBRA
#define VTYSH_AFFMAP VTYSH_ZEBRA | VTYSH_ISISD
#define VTYSH_AFFMAP VTYSH_MGMTD
#define VTYSH_RMAP VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ISISD|VTYSH_PIMD|VTYSH_EIGRPD|VTYSH_FABRICD
#define VTYSH_INTERFACE_SUBSET \
VTYSH_ZEBRA | VTYSH_RIPD | VTYSH_RIPNGD | VTYSH_OSPFD | VTYSH_OSPF6D | \
VTYSH_ISISD | VTYSH_PIMD | VTYSH_PIM6D | VTYSH_NHRPD | \
VTYSH_EIGRPD | VTYSH_BABELD | VTYSH_PBRD | VTYSH_FABRICD | \
VTYSH_VRRPD
VTYSH_VRRPD | VTYSH_MGMTD
#define VTYSH_INTERFACE VTYSH_INTERFACE_SUBSET | VTYSH_BGPD
#define VTYSH_VRF VTYSH_INTERFACE_SUBSET | VTYSH_MGMTD
#define VTYSH_KEYS VTYSH_RIPD | VTYSH_EIGRPD | VTYSH_OSPF6D | VTYSH_OSPFD
Expand Down
Loading

0 comments on commit 161c693

Please sign in to comment.