From 078110ca601eb266ef2baea80b0026fa31882426 Mon Sep 17 00:00:00 2001 From: Sarita Patra Date: Fri, 30 Oct 2020 00:42:34 -0700 Subject: [PATCH] ospf6d: North-bound implementation for ospf6d rmaps This commit introduces the implementation for the north-bound callbacks for the ospf6d-specific route-map match and set clauses. Signed-off-by: NaveenThanikachalam Signed-off-by: Sarita Patra --- ospf6d/ospf6_asbr.c | 127 +++++++++++++---------------- ospf6d/ospf6_main.c | 3 + ospf6d/ospf6_routemap_nb.c | 55 +++++++++++++ ospf6d/ospf6_routemap_nb.h | 40 +++++++++ ospf6d/ospf6_routemap_nb_config.c | 129 ++++++++++++++++++++++++++++++ ospf6d/subdir.am | 8 ++ 6 files changed, 291 insertions(+), 71 deletions(-) create mode 100644 ospf6d/ospf6_routemap_nb.c create mode 100644 ospf6d/ospf6_routemap_nb.h create mode 100644 ospf6d/ospf6_routemap_nb_config.c diff --git a/ospf6d/ospf6_asbr.c b/ospf6d/ospf6_asbr.c index 9fb21d5546..ffd6dc22c3 100644 --- a/ospf6d/ospf6_asbr.c +++ b/ospf6d/ospf6_asbr.c @@ -30,6 +30,7 @@ #include "plist.h" #include "thread.h" #include "linklist.h" +#include "lib/northbound_cli.h" #include "ospf6_proto.h" #include "ospf6_lsa.h" @@ -1924,99 +1925,83 @@ ospf6_routemap_rule_set_tag(void *rule, const struct prefix *p, void *object) return RMAP_OKAY; } -static const struct route_map_rule_cmd - ospf6_routemap_rule_set_tag_cmd = { +static const struct route_map_rule_cmd ospf6_routemap_rule_set_tag_cmd = { "tag", ospf6_routemap_rule_set_tag, route_map_rule_tag_compile, route_map_rule_tag_free, }; -static int route_map_command_status(struct vty *vty, enum rmap_compile_rets ret) -{ - switch (ret) { - case RMAP_RULE_MISSING: - vty_out(vty, "OSPF6 Can't find rule.\n"); - return CMD_WARNING_CONFIG_FAILED; - case RMAP_COMPILE_ERROR: - vty_out(vty, "OSPF6 Argument is malformed.\n"); - return CMD_WARNING_CONFIG_FAILED; - case RMAP_COMPILE_SUCCESS: - break; - } - - return CMD_SUCCESS; -} - /* add "set metric-type" */ -DEFUN (ospf6_routemap_set_metric_type, - ospf6_routemap_set_metric_type_cmd, - "set metric-type ", - "Set value\n" - "Type of metric\n" - "OSPF6 external type 1 metric\n" - "OSPF6 external type 2 metric\n") +DEFUN_YANG (ospf6_routemap_set_metric_type, ospf6_routemap_set_metric_type_cmd, + "set metric-type ", + "Set value\n" + "Type of metric\n" + "OSPF6 external type 1 metric\n" + "OSPF6 external type 2 metric\n") { - VTY_DECLVAR_CONTEXT(route_map_index, route_map_index); - int idx_external = 2; - enum rmap_compile_rets ret = route_map_add_set(route_map_index, - "metric-type", - argv[idx_external]->arg); + char *ext = argv[2]->text; - return route_map_command_status(vty, ret); + const char *xpath = + "./set-action[action='frr-ospf-route-map:metric-type']"; + char xpath_value[XPATH_MAXLEN]; + + nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL); + snprintf(xpath_value, sizeof(xpath_value), + "%s/rmap-set-action/frr-ospf-route-map:metric-type", xpath); + nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, ext); + return nb_cli_apply_changes(vty, NULL); } /* delete "set metric-type" */ -DEFUN (ospf6_routemap_no_set_metric_type, - ospf6_routemap_no_set_metric_type_cmd, - "no set metric-type []", - NO_STR - "Set value\n" - "Type of metric\n" - "OSPF6 external type 1 metric\n" - "OSPF6 external type 2 metric\n") +DEFUN_YANG (ospf6_routemap_no_set_metric_type, ospf6_routemap_no_set_metric_type_cmd, + "no set metric-type []", + NO_STR + "Set value\n" + "Type of metric\n" + "OSPF6 external type 1 metric\n" + "OSPF6 external type 2 metric\n") { - VTY_DECLVAR_CONTEXT(route_map_index, route_map_index); - char *ext = (argc == 4) ? argv[3]->text : NULL; - enum rmap_compile_rets ret = route_map_delete_set(route_map_index, - "metric-type", ext); + const char *xpath = + "./set-action[action='frr-ospf-route-map:metric-type']"; - return route_map_command_status(vty, ret); + nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL); + return nb_cli_apply_changes(vty, NULL); } /* add "set forwarding-address" */ -DEFUN (ospf6_routemap_set_forwarding, - ospf6_routemap_set_forwarding_cmd, - "set forwarding-address X:X::X:X", - "Set value\n" - "Forwarding Address\n" - "IPv6 Address\n") -{ - VTY_DECLVAR_CONTEXT(route_map_index, route_map_index); +DEFUN_YANG (ospf6_routemap_set_forwarding, ospf6_routemap_set_forwarding_cmd, + "set forwarding-address X:X::X:X", + "Set value\n" + "Forwarding Address\n" + "IPv6 Address\n") +{ int idx_ipv6 = 2; - enum rmap_compile_rets ret = route_map_add_set(route_map_index, - "forwarding-address", - argv[idx_ipv6]->arg); + const char *xpath = + "./set-action[action='frr-ospf6-route-map:forwarding-address']"; + char xpath_value[XPATH_MAXLEN]; - return route_map_command_status(vty, ret); + nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL); + snprintf(xpath_value, sizeof(xpath_value), + "%s/rmap-set-action/frr-ospf6-route-map:ipv6-address", xpath); + nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, + argv[idx_ipv6]->arg); + return nb_cli_apply_changes(vty, NULL); } /* delete "set forwarding-address" */ -DEFUN (ospf6_routemap_no_set_forwarding, - ospf6_routemap_no_set_forwarding_cmd, - "no set forwarding-address X:X::X:X", - NO_STR - "Set value\n" - "Forwarding Address\n" - "IPv6 Address\n") -{ - VTY_DECLVAR_CONTEXT(route_map_index, route_map_index); - int idx_ipv6 = 3; - enum rmap_compile_rets ret = route_map_delete_set(route_map_index, - "forwarding-address", - argv[idx_ipv6]->arg); - - return route_map_command_status(vty, ret); +DEFUN_YANG (ospf6_routemap_no_set_forwarding, ospf6_routemap_no_set_forwarding_cmd, + "no set forwarding-address [X:X::X:X]", + NO_STR + "Set value\n" + "Forwarding Address\n" + "IPv6 Address\n") +{ + const char *xpath = + "./set-action[action='frr-ospf6-route-map:forwarding-address']"; + + nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL); + return nb_cli_apply_changes(vty, NULL); } static void ospf6_routemap_init(void) diff --git a/ospf6d/ospf6_main.c b/ospf6d/ospf6_main.c index c601693a7e..cf61ca7a62 100644 --- a/ospf6d/ospf6_main.c +++ b/ospf6d/ospf6_main.c @@ -47,6 +47,7 @@ #include "ospf6_lsa.h" #include "ospf6_interface.h" #include "ospf6_zebra.h" +#include "ospf6_routemap_nb.h" /* Default configuration file name for ospf6d. */ #define OSPF6_DEFAULT_CONFIG "ospf6d.conf" @@ -172,6 +173,8 @@ static const struct frr_yang_module_info *const ospf6d_yang_modules[] = { &frr_interface_info, &frr_route_map_info, &frr_vrf_info, + &frr_ospf_route_map_info, + &frr_ospf6_route_map_info, }; FRR_DAEMON_INFO(ospf6d, OSPF6, .vty_port = OSPF6_VTY_PORT, diff --git a/ospf6d/ospf6_routemap_nb.c b/ospf6d/ospf6_routemap_nb.c new file mode 100644 index 0000000000..b710fefbdf --- /dev/null +++ b/ospf6d/ospf6_routemap_nb.c @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2020 Vmware + * Sarita Patra + * + * This program 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 of the License, or (at your option) + * any later version. + * + * This program 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 "lib/northbound.h" +#include "lib/routemap.h" +#include "ospf6_routemap_nb.h" + +/* clang-format off */ +const struct frr_yang_module_info frr_ospf_route_map_info = { + .name = "frr-ospf-route-map", + .nodes = { + { + .xpath = "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/frr-ospf-route-map:metric-type", + .cbs = { + .modify = lib_route_map_entry_set_action_rmap_set_action_metric_type_modify, + .destroy = lib_route_map_entry_set_action_rmap_set_action_metric_type_destroy, + } + }, + { + .xpath = NULL, + }, + } +}; + +const struct frr_yang_module_info frr_ospf6_route_map_info = { + .name = "frr-ospf6-route-map", + .nodes = { + { + .xpath = "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/frr-ospf6-route-map:ipv6-address", + .cbs = { + .modify = lib_route_map_entry_set_action_rmap_set_action_ipv6_address_modify, + .destroy = lib_route_map_entry_set_action_rmap_set_action_ipv6_address_destroy, + } + }, + { + .xpath = NULL, + }, + } +}; diff --git a/ospf6d/ospf6_routemap_nb.h b/ospf6d/ospf6_routemap_nb.h new file mode 100644 index 0000000000..181e71a8c5 --- /dev/null +++ b/ospf6d/ospf6_routemap_nb.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2020 Vmware + * Sarita Patra + * + * This program 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 of the License, or (at your option) + * any later version. + * + * This program 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 _FRR_OSPF6_ROUTEMAP_NB_H_ +#define _FRR_OSPF6_ROUTEMAP_NB_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +extern const struct frr_yang_module_info frr_ospf_route_map_info; +extern const struct frr_yang_module_info frr_ospf6_route_map_info; + +/* prototypes */ +int lib_route_map_entry_set_action_rmap_set_action_ipv6_address_modify(struct nb_cb_modify_args *args); +int lib_route_map_entry_set_action_rmap_set_action_ipv6_address_destroy(struct nb_cb_destroy_args *args); +int lib_route_map_entry_set_action_rmap_set_action_metric_type_modify(struct nb_cb_modify_args *args); +int lib_route_map_entry_set_action_rmap_set_action_metric_type_destroy(struct nb_cb_destroy_args *args); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/ospf6d/ospf6_routemap_nb_config.c b/ospf6d/ospf6_routemap_nb_config.c new file mode 100644 index 0000000000..3c7741e473 --- /dev/null +++ b/ospf6d/ospf6_routemap_nb_config.c @@ -0,0 +1,129 @@ +/* + * Copyright (C) 2020 Vmware + * Sarita Patra + * + * This program 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 of the License, or (at your option) + * any later version. + * + * This program 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 "lib/command.h" +#include "lib/log.h" +#include "lib/northbound.h" +#include "lib/routemap.h" +#include "ospf6_routemap_nb.h" + +/* + * XPath: + * /frr-route-map:lib/route-map/entry/set-action/rmap-set-action/frr-ospf-route-map:metric-type + */ +int lib_route_map_entry_set_action_rmap_set_action_metric_type_modify( + struct nb_cb_modify_args *args) +{ + struct routemap_hook_context *rhc; + const char *type; + int rv; + + switch (args->event) { + case NB_EV_VALIDATE: + case NB_EV_PREPARE: + case NB_EV_ABORT: + break; + case NB_EV_APPLY: + /* Add configuration. */ + rhc = nb_running_get_entry(args->dnode, NULL, true); + type = yang_dnode_get_string(args->dnode, NULL); + + /* Set destroy information. */ + rhc->rhc_shook = generic_set_delete; + rhc->rhc_rule = "metric-type"; + rhc->rhc_event = RMAP_EVENT_SET_DELETED; + + rv = generic_set_add(rhc->rhc_rmi, "metric-type", type, + args->errmsg, args->errmsg_len); + if (rv != CMD_SUCCESS) { + rhc->rhc_mhook = NULL; + return NB_ERR_INCONSISTENCY; + } + } + + return NB_OK; +} + +int lib_route_map_entry_set_action_rmap_set_action_metric_type_destroy( + struct nb_cb_destroy_args *args) +{ + switch (args->event) { + case NB_EV_VALIDATE: + case NB_EV_PREPARE: + case NB_EV_ABORT: + break; + case NB_EV_APPLY: + return lib_route_map_entry_set_destroy(args); + } + + return NB_OK; +} + +/* + * XPath: + * /frr-route-map:lib/route-map/entry/set-action/rmap-set-action/frr-ospf6-route-map:ipv6-address + */ +int lib_route_map_entry_set_action_rmap_set_action_ipv6_address_modify( + struct nb_cb_modify_args *args) +{ + struct routemap_hook_context *rhc; + const char *ipv6_addr; + int rv; + + switch (args->event) { + case NB_EV_VALIDATE: + case NB_EV_PREPARE: + case NB_EV_ABORT: + break; + case NB_EV_APPLY: + /* Add configuration. */ + rhc = nb_running_get_entry(args->dnode, NULL, true); + ipv6_addr = yang_dnode_get_string(args->dnode, NULL); + + /* Set destroy information. */ + rhc->rhc_shook = generic_set_delete; + rhc->rhc_rule = "forwarding-address"; + rhc->rhc_event = RMAP_EVENT_SET_DELETED; + + rv = generic_set_add(rhc->rhc_rmi, "forwarding-address", + ipv6_addr, + args->errmsg, args->errmsg_len); + if (rv != CMD_SUCCESS) { + rhc->rhc_mhook = NULL; + return NB_ERR_INCONSISTENCY; + } + } + + return NB_OK; +} + +int lib_route_map_entry_set_action_rmap_set_action_ipv6_address_destroy( + struct nb_cb_destroy_args *args) +{ + switch (args->event) { + case NB_EV_VALIDATE: + case NB_EV_PREPARE: + case NB_EV_ABORT: + break; + case NB_EV_APPLY: + return lib_route_map_entry_set_destroy(args); + } + + return NB_OK; +} diff --git a/ospf6d/subdir.am b/ospf6d/subdir.am index dc173da9dc..788b532a90 100644 --- a/ospf6d/subdir.am +++ b/ospf6d/subdir.am @@ -34,6 +34,8 @@ ospf6d_libospf6_a_SOURCES = \ ospf6d/ospf6_abr.c \ ospf6d/ospf6_area.c \ ospf6d/ospf6_asbr.c \ + ospf6d/ospf6_routemap_nb.c \ + ospf6d/ospf6_routemap_nb_config.c \ ospf6d/ospf6_bfd.c \ ospf6d/ospf6_flood.c \ ospf6d/ospf6_interface.c \ @@ -66,6 +68,7 @@ noinst_HEADERS += \ ospf6d/ospf6_network.h \ ospf6d/ospf6_proto.h \ ospf6d/ospf6_route.h \ + ospf6d/ospf6_routemap_nb.h \ ospf6d/ospf6_spf.h \ ospf6d/ospf6_top.h \ ospf6d/ospf6_zebra.h \ @@ -85,3 +88,8 @@ ospf6d_ospf6d_snmp_la_LIBADD = lib/libfrrsnmp.la clippy_scan += \ ospf6d/ospf6_asbr.c \ # end + +nodist_ospf6d_ospf6d_SOURCES = \ + yang/frr-ospf-route-map.yang.c \ + yang/frr-ospf6-route-map.yang.c \ + # end -- 2.39.5