From 244e6ebd162465f386687d8afcc29c1cf51de192 Mon Sep 17 00:00:00 2001 From: Igor Ryzhov Date: Sat, 27 Jan 2024 00:16:17 +0200 Subject: [PATCH] zebra: convert router-id commands to NB Signed-off-by: Igor Ryzhov --- yang/frr-zebra.yang | 15 +++ zebra/router-id.c | 199 +++++++++++++--------------------------- zebra/router-id.h | 1 + zebra/subdir.am | 1 + zebra/zebra_nb.c | 14 +++ zebra/zebra_nb.h | 4 + zebra/zebra_nb_config.c | 77 ++++++++++++++++ 7 files changed, 174 insertions(+), 137 deletions(-) diff --git a/yang/frr-zebra.yang b/yang/frr-zebra.yang index 72193664d4..06ba547d0b 100644 --- a/yang/frr-zebra.yang +++ b/yang/frr-zebra.yang @@ -2743,6 +2743,21 @@ module frr-zebra { container zebra { description "Zebra's vrf specific configuration and operational model."; + + leaf router-id { + type yang:dotted-quad; + description + "A 32-bit number in the form of a dotted quad that is used by + some routing protocols identifying a router."; + } + + leaf ipv6-router-id { + type inet:ipv6-address-no-zone; + description + "A 128-bit number in the form of an IPv6 address that is used by + some routing protocols identifying a router."; + } + uses ribs; uses vrf-vni-mapping; diff --git a/zebra/router-id.c b/zebra/router-id.c index ef87d924fe..92b005081a 100644 --- a/zebra/router-id.c +++ b/zebra/router-id.c @@ -23,6 +23,7 @@ #include "table.h" #include "rib.h" #include "vrf.h" +#include "northbound_cli.h" #include "zebra/zebra_router.h" #include "zebra/zapi_msg.h" @@ -30,6 +31,8 @@ #include "zebra/router-id.h" #include "zebra/redistribute.h" +#include "router-id_clippy.c" + static struct connected *router_id_find_node(struct list *l, struct connected *ifc) { @@ -109,7 +112,7 @@ int router_id_get(afi_t afi, struct prefix *p, struct zebra_vrf *zvrf) assert(!"Reached end of function we should never hit"); } -static int router_id_set(afi_t afi, struct prefix *p, struct zebra_vrf *zvrf) +int router_id_set(afi_t afi, struct prefix *p, struct zebra_vrf *zvrf) { struct prefix after, before; struct listnode *node; @@ -256,194 +259,121 @@ void router_id_write(struct vty *vty, struct zebra_vrf *zvrf) } if (!router_id_v6_is_any(&zvrf->rid6_user_assigned)) { vty_out(vty, "%sipv6 router-id %pI6\n", space, - &zvrf->rid_user_assigned.u.prefix6); + &zvrf->rid6_user_assigned.u.prefix6); } } -DEFUN (ip_router_id, +DEFPY_YANG (ip_router_id, ip_router_id_cmd, - "ip router-id A.B.C.D vrf NAME", + "ip router-id A.B.C.D$id vrf NAME", IP_STR "Manually set the router-id\n" "IP address to use for router-id\n" VRF_CMD_HELP_STR) { - int idx = 0; - struct prefix rid; - vrf_id_t vrf_id; - struct zebra_vrf *zvrf; - - argv_find(argv, argc, "A.B.C.D", &idx); - - if (!inet_pton(AF_INET, argv[idx]->arg, &rid.u.prefix4)) - return CMD_WARNING_CONFIG_FAILED; - - rid.prefixlen = IPV4_MAX_BITLEN; - rid.family = AF_INET; - - argv_find(argv, argc, "NAME", &idx); - VRF_GET_ID(vrf_id, argv[idx]->arg, false); - - zvrf = zebra_vrf_lookup_by_id(vrf_id); - router_id_set(AFI_IP, &rid, zvrf); - - return CMD_SUCCESS; + nb_cli_enqueue_change(vty, "./frr-zebra:zebra/router-id", NB_OP_MODIFY, + id_str); + return nb_cli_apply_changes(vty, "/frr-vrf:lib/vrf[name='%s']", vrf); } -ALIAS (ip_router_id, +ALIAS_YANG (ip_router_id, router_id_cmd, - "router-id A.B.C.D vrf NAME", + "router-id A.B.C.D$id vrf NAME", "Manually set the router-id\n" "IP address to use for router-id\n" VRF_CMD_HELP_STR); -DEFUN (ipv6_router_id, +DEFPY_YANG (ipv6_router_id, ipv6_router_id_cmd, - "ipv6 router-id X:X::X:X vrf NAME", + "ipv6 router-id X:X::X:X$id vrf NAME", IPV6_STR "Manually set the router-id\n" "IPv6 address to use for router-id\n" VRF_CMD_HELP_STR) { - int idx = 0; - struct prefix rid; - vrf_id_t vrf_id; - struct zebra_vrf *zvrf; - - argv_find(argv, argc, "X:X::X:X", &idx); - - if (!inet_pton(AF_INET6, argv[idx]->arg, &rid.u.prefix6)) - return CMD_WARNING_CONFIG_FAILED; - - rid.prefixlen = IPV6_MAX_BITLEN; - rid.family = AF_INET6; - - argv_find(argv, argc, "NAME", &idx); - VRF_GET_ID(vrf_id, argv[idx]->arg, false); - - zvrf = zebra_vrf_lookup_by_id(vrf_id); - router_id_set(AFI_IP6, &rid, zvrf); - - return CMD_SUCCESS; + nb_cli_enqueue_change(vty, "./frr-zebra:zebra/ipv6-router-id", + NB_OP_MODIFY, id_str); + return nb_cli_apply_changes(vty, "/frr-vrf:lib/vrf[name='%s']", vrf); } -DEFUN (ip_router_id_in_vrf, +DEFPY_YANG (ip_router_id_in_vrf, ip_router_id_in_vrf_cmd, - "ip router-id A.B.C.D", + "ip router-id A.B.C.D$id", IP_STR "Manually set the router-id\n" "IP address to use for router-id\n") { - ZEBRA_DECLVAR_CONTEXT_VRF(vrf, zvrf); - int idx = 0; - struct prefix rid; + nb_cli_enqueue_change(vty, "./frr-zebra:zebra/router-id", NB_OP_MODIFY, + id_str); - argv_find(argv, argc, "A.B.C.D", &idx); + if (vty->node == CONFIG_NODE) + return nb_cli_apply_changes(vty, "/frr-vrf:lib/vrf[name='%s']", + VRF_DEFAULT_NAME); - if (!inet_pton(AF_INET, argv[idx]->arg, &rid.u.prefix4)) - return CMD_WARNING_CONFIG_FAILED; - - rid.prefixlen = IPV4_MAX_BITLEN; - rid.family = AF_INET; - - router_id_set(AFI_IP, &rid, zvrf); - - return CMD_SUCCESS; + return nb_cli_apply_changes(vty, NULL); } -ALIAS (ip_router_id_in_vrf, +ALIAS_YANG (ip_router_id_in_vrf, router_id_in_vrf_cmd, - "router-id A.B.C.D", + "router-id A.B.C.D$id", "Manually set the router-id\n" "IP address to use for router-id\n"); -DEFUN (ipv6_router_id_in_vrf, +DEFPY_YANG (ipv6_router_id_in_vrf, ipv6_router_id_in_vrf_cmd, - "ipv6 router-id X:X::X:X", + "ipv6 router-id X:X::X:X$id", IP6_STR "Manually set the IPv6 router-id\n" "IPV6 address to use for router-id\n") { - ZEBRA_DECLVAR_CONTEXT_VRF(vrf, zvrf); - int idx = 0; - struct prefix rid; - - argv_find(argv, argc, "X:X::X:X", &idx); - - if (!inet_pton(AF_INET6, argv[idx]->arg, &rid.u.prefix6)) - return CMD_WARNING_CONFIG_FAILED; + nb_cli_enqueue_change(vty, "./frr-zebra:zebra/ipv6-router-id", + NB_OP_MODIFY, id_str); - rid.prefixlen = IPV6_MAX_BITLEN; - rid.family = AF_INET6; + if (vty->node == CONFIG_NODE) + return nb_cli_apply_changes(vty, "/frr-vrf:lib/vrf[name='%s']", + VRF_DEFAULT_NAME); - router_id_set(AFI_IP6, &rid, zvrf); - - return CMD_SUCCESS; + return nb_cli_apply_changes(vty, NULL); } -DEFUN (no_ip_router_id, +DEFPY_YANG (no_ip_router_id, no_ip_router_id_cmd, - "no ip router-id [A.B.C.D vrf NAME]", + "no ip router-id A.B.C.D vrf NAME", NO_STR IP_STR "Remove the manually configured router-id\n" "IP address to use for router-id\n" VRF_CMD_HELP_STR) { - int idx = 0; - struct prefix rid; - vrf_id_t vrf_id = VRF_DEFAULT; - struct zebra_vrf *zvrf; - - rid.u.prefix4.s_addr = 0; - rid.prefixlen = 0; - rid.family = AF_INET; - - if (argv_find(argv, argc, "NAME", &idx)) - VRF_GET_ID(vrf_id, argv[idx]->arg, false); - - zvrf = zebra_vrf_lookup_by_id(vrf_id); - router_id_set(AFI_IP, &rid, zvrf); - - return CMD_SUCCESS; + nb_cli_enqueue_change(vty, "./frr-zebra:zebra/router-id", NB_OP_DESTROY, + NULL); + return nb_cli_apply_changes(vty, "/frr-vrf:lib/vrf[name='%s']", vrf); } -ALIAS (no_ip_router_id, +ALIAS_YANG (no_ip_router_id, no_router_id_cmd, - "no router-id [A.B.C.D vrf NAME]", + "no router-id A.B.C.D vrf NAME", NO_STR "Remove the manually configured router-id\n" "IP address to use for router-id\n" VRF_CMD_HELP_STR); -DEFUN (no_ipv6_router_id, +DEFPY_YANG (no_ipv6_router_id, no_ipv6_router_id_cmd, - "no ipv6 router-id [X:X::X:X vrf NAME]", + "no ipv6 router-id X:X::X:X vrf NAME", NO_STR IPV6_STR "Remove the manually configured IPv6 router-id\n" "IPv6 address to use for router-id\n" VRF_CMD_HELP_STR) { - int idx = 0; - struct prefix rid; - vrf_id_t vrf_id = VRF_DEFAULT; - struct zebra_vrf *zvrf; - - memset(&rid, 0, sizeof(rid)); - rid.family = AF_INET; - - if (argv_find(argv, argc, "NAME", &idx)) - VRF_GET_ID(vrf_id, argv[idx]->arg, false); - - zvrf = zebra_vrf_lookup_by_id(vrf_id); - router_id_set(AFI_IP6, &rid, zvrf); - - return CMD_SUCCESS; + nb_cli_enqueue_change(vty, "./frr-zebra:zebra/ipv6-router-id", + NB_OP_DESTROY, NULL); + return nb_cli_apply_changes(vty, "/frr-vrf:lib/vrf[name='%s']", vrf); } -DEFUN (no_ip_router_id_in_vrf, +DEFPY_YANG (no_ip_router_id_in_vrf, no_ip_router_id_in_vrf_cmd, "no ip router-id [A.B.C.D]", NO_STR @@ -451,27 +381,24 @@ DEFUN (no_ip_router_id_in_vrf, "Remove the manually configured router-id\n" "IP address to use for router-id\n") { - ZEBRA_DECLVAR_CONTEXT_VRF(vrf, zvrf); - - struct prefix rid; + nb_cli_enqueue_change(vty, "./frr-zebra:zebra/router-id", NB_OP_DESTROY, + NULL); - rid.u.prefix4.s_addr = 0; - rid.prefixlen = 0; - rid.family = AF_INET; + if (vty->node == CONFIG_NODE) + return nb_cli_apply_changes(vty, "/frr-vrf:lib/vrf[name='%s']", + VRF_DEFAULT_NAME); - router_id_set(AFI_IP, &rid, zvrf); - - return CMD_SUCCESS; + return nb_cli_apply_changes(vty, NULL); } -ALIAS (no_ip_router_id_in_vrf, +ALIAS_YANG (no_ip_router_id_in_vrf, no_router_id_in_vrf_cmd, "no router-id [A.B.C.D]", NO_STR "Remove the manually configured router-id\n" "IP address to use for router-id\n"); -DEFUN (no_ipv6_router_id_in_vrf, +DEFPY_YANG (no_ipv6_router_id_in_vrf, no_ipv6_router_id_in_vrf_cmd, "no ipv6 router-id [X:X::X:X]", NO_STR @@ -479,16 +406,14 @@ DEFUN (no_ipv6_router_id_in_vrf, "Remove the manually configured IPv6 router-id\n" "IPv6 address to use for router-id\n") { - ZEBRA_DECLVAR_CONTEXT_VRF(vrf, zvrf); + nb_cli_enqueue_change(vty, "./frr-zebra:zebra/ipv6-router-id", + NB_OP_DESTROY, NULL); - struct prefix rid; + if (vty->node == CONFIG_NODE) + return nb_cli_apply_changes(vty, "/frr-vrf:lib/vrf[name='%s']", + VRF_DEFAULT_NAME); - memset(&rid, 0, sizeof(rid)); - rid.family = AF_INET; - - router_id_set(AFI_IP6, &rid, zvrf); - - return CMD_SUCCESS; + return nb_cli_apply_changes(vty, NULL); } DEFUN (show_ip_router_id, diff --git a/zebra/router-id.h b/zebra/router-id.h index 45860d8b7b..4753a1840e 100644 --- a/zebra/router-id.h +++ b/zebra/router-id.h @@ -27,6 +27,7 @@ extern void router_id_init(struct zebra_vrf *zvrf); extern void router_id_cmd_init(void); extern void router_id_write(struct vty *vty, struct zebra_vrf *zvrf); extern int router_id_get(afi_t afi, struct prefix *p, struct zebra_vrf *zvrf); +extern int router_id_set(afi_t afi, struct prefix *p, struct zebra_vrf *zvrf); #ifdef __cplusplus } diff --git a/zebra/subdir.am b/zebra/subdir.am index 6d499e97e8..ad1bcee695 100644 --- a/zebra/subdir.am +++ b/zebra/subdir.am @@ -126,6 +126,7 @@ clippy_scan += \ zebra/zebra_vrf.c \ zebra/dpdk/zebra_dplane_dpdk_vty.c \ zebra/label_manager.c \ + zebra/router-id.c \ # end noinst_HEADERS += \ diff --git a/zebra/zebra_nb.c b/zebra/zebra_nb.c index b261325f19..cbf5bfb59a 100644 --- a/zebra/zebra_nb.c +++ b/zebra/zebra_nb.c @@ -833,6 +833,20 @@ const struct frr_yang_module_info frr_zebra_info = { .get_elem = lib_interface_zebra_state_mcast_group_get_elem, } }, + { + .xpath = "/frr-vrf:lib/vrf/frr-zebra:zebra/router-id", + .cbs = { + .modify = lib_vrf_zebra_router_id_modify, + .destroy = lib_vrf_zebra_router_id_destroy, + } + }, + { + .xpath = "/frr-vrf:lib/vrf/frr-zebra:zebra/ipv6-router-id", + .cbs = { + .modify = lib_vrf_zebra_ipv6_router_id_modify, + .destroy = lib_vrf_zebra_ipv6_router_id_destroy, + } + }, { .xpath = "/frr-vrf:lib/vrf/frr-zebra:zebra/ribs/rib", .cbs = { diff --git a/zebra/zebra_nb.h b/zebra/zebra_nb.h index 5343cb0e9b..9cde562060 100644 --- a/zebra/zebra_nb.h +++ b/zebra/zebra_nb.h @@ -416,6 +416,10 @@ struct yang_data *lib_interface_zebra_state_remote_vtep_get_elem( struct nb_cb_get_elem_args *args); struct yang_data *lib_interface_zebra_state_mcast_group_get_elem( struct nb_cb_get_elem_args *args); +int lib_vrf_zebra_router_id_modify(struct nb_cb_modify_args *args); +int lib_vrf_zebra_router_id_destroy(struct nb_cb_destroy_args *args); +int lib_vrf_zebra_ipv6_router_id_modify(struct nb_cb_modify_args *args); +int lib_vrf_zebra_ipv6_router_id_destroy(struct nb_cb_destroy_args *args); const void *lib_vrf_zebra_ribs_rib_get_next(struct nb_cb_get_next_args *args); int lib_vrf_zebra_ribs_rib_get_keys(struct nb_cb_get_keys_args *args); const void * diff --git a/zebra/zebra_nb_config.c b/zebra/zebra_nb_config.c index 41f82f5ebe..c828d08733 100644 --- a/zebra/zebra_nb_config.c +++ b/zebra/zebra_nb_config.c @@ -25,6 +25,7 @@ #include "zebra/zebra_vxlan.h" #include "zebra/zebra_evpn_mh.h" #include "zebra/zebra_ptm.h" +#include "zebra/router-id.h" /* * XPath: /frr-zebra:zebra/mcast-rpf-lookup @@ -3226,6 +3227,82 @@ int lib_interface_zebra_ptm_enable_modify(struct nb_cb_modify_args *args) } #endif +/* + * XPath: /frr-vrf:lib/vrf/frr-zebra:zebra/router-id + */ +int lib_vrf_zebra_router_id_modify(struct nb_cb_modify_args *args) +{ + struct vrf *vrf; + struct prefix p; + + if (args->event != NB_EV_APPLY) + return NB_OK; + + vrf = nb_running_get_entry(args->dnode, NULL, true); + + yang_dnode_get_ipv4p(&p, args->dnode, NULL); + + router_id_set(AFI_IP, &p, vrf->info); + + return NB_OK; +} + +int lib_vrf_zebra_router_id_destroy(struct nb_cb_destroy_args *args) +{ + struct vrf *vrf; + struct prefix p; + + if (args->event != NB_EV_APPLY) + return NB_OK; + + vrf = nb_running_get_entry(args->dnode, NULL, true); + + memset(&p, 0, sizeof(p)); + p.family = AF_INET; + + router_id_set(AFI_IP, &p, vrf->info); + + return NB_OK; +} + +/* + * XPath: /frr-vrf:lib/vrf/frr-zebra:zebra/ipv6-router-id + */ +int lib_vrf_zebra_ipv6_router_id_modify(struct nb_cb_modify_args *args) +{ + struct vrf *vrf; + struct prefix p; + + if (args->event != NB_EV_APPLY) + return NB_OK; + + vrf = nb_running_get_entry(args->dnode, NULL, true); + + yang_dnode_get_ipv6p(&p, args->dnode, NULL); + + router_id_set(AFI_IP6, &p, vrf->info); + + return NB_OK; +} + +int lib_vrf_zebra_ipv6_router_id_destroy(struct nb_cb_destroy_args *args) +{ + struct vrf *vrf; + struct prefix p; + + if (args->event != NB_EV_APPLY) + return NB_OK; + + vrf = nb_running_get_entry(args->dnode, NULL, true); + + memset(&p, 0, sizeof(p)); + p.family = AF_INET6; + + router_id_set(AFI_IP6, &p, vrf->info); + + return NB_OK; +} + /* * XPath: /frr-vrf:lib/vrf/frr-zebra:zebra/l3vni-id */ -- 2.39.5