From dd2c81b8c09ac8c27ca909cf16640815941df31e Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Sat, 6 Nov 2021 21:10:41 +0100 Subject: [PATCH] lib: rework vty_check_node_for_xpath_decrement ...by having a flag in struct cmd_node rather than hardcoding it in `lib/command.c`. Signed-off-by: David Lamparter --- bgpd/bgp_vty.c | 11 +++++++++++ lib/command.c | 41 +++++++++-------------------------------- lib/command.h | 3 +++ vtysh/vtysh.c | 12 ++++++++++++ zebra/interface.c | 1 + 5 files changed, 36 insertions(+), 32 deletions(-) diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c index 3725f242e1..26591a5fce 100644 --- a/bgpd/bgp_vty.c +++ b/bgpd/bgp_vty.c @@ -17593,6 +17593,7 @@ static struct cmd_node bgp_ipv4_unicast_node = { .node = BGP_IPV4_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_ipv4_multicast_node = { @@ -17600,6 +17601,7 @@ static struct cmd_node bgp_ipv4_multicast_node = { .node = BGP_IPV4M_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_ipv4_labeled_unicast_node = { @@ -17607,6 +17609,7 @@ static struct cmd_node bgp_ipv4_labeled_unicast_node = { .node = BGP_IPV4L_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_ipv6_unicast_node = { @@ -17614,6 +17617,7 @@ static struct cmd_node bgp_ipv6_unicast_node = { .node = BGP_IPV6_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_ipv6_multicast_node = { @@ -17621,6 +17625,7 @@ static struct cmd_node bgp_ipv6_multicast_node = { .node = BGP_IPV6M_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_ipv6_labeled_unicast_node = { @@ -17628,6 +17633,7 @@ static struct cmd_node bgp_ipv6_labeled_unicast_node = { .node = BGP_IPV6L_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_vpnv4_node = { @@ -17635,6 +17641,7 @@ static struct cmd_node bgp_vpnv4_node = { .node = BGP_VPNV4_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_vpnv6_node = { @@ -17642,6 +17649,7 @@ static struct cmd_node bgp_vpnv6_node = { .node = BGP_VPNV6_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af-vpnv6)# ", + .no_xpath = true, }; static struct cmd_node bgp_evpn_node = { @@ -17649,6 +17657,7 @@ static struct cmd_node bgp_evpn_node = { .node = BGP_EVPN_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-evpn)# ", + .no_xpath = true, }; static struct cmd_node bgp_evpn_vni_node = { @@ -17663,6 +17672,7 @@ static struct cmd_node bgp_flowspecv4_node = { .node = BGP_FLOWSPECV4_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_flowspecv6_node = { @@ -17670,6 +17680,7 @@ static struct cmd_node bgp_flowspecv6_node = { .node = BGP_FLOWSPECV6_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af-vpnv6)# ", + .no_xpath = true, }; static struct cmd_node bgp_srv6_node = { diff --git a/lib/command.c b/lib/command.c index ea66a17bb0..9cf93ea192 100644 --- a/lib/command.c +++ b/lib/command.c @@ -145,31 +145,6 @@ static struct cmd_node config_node = { .node_exit = vty_config_node_exit, }; -static bool vty_check_node_for_xpath_decrement(enum node_type target_node, - enum node_type node) -{ - /* bgp afi-safi (`address-family `) node - * does not increment xpath_index. - * In order to use (`router bgp`) BGP_NODE's xpath as a base, - * retain xpath_index as 1 upon exiting from - * afi-safi node. - */ - - if (target_node == BGP_NODE - && (node == BGP_IPV4_NODE || node == BGP_IPV6_NODE - || node == BGP_IPV4M_NODE || node == BGP_IPV6M_NODE - || node == BGP_VPNV4_NODE || node == BGP_VPNV6_NODE - || node == BGP_EVPN_NODE || node == BGP_IPV4L_NODE - || node == BGP_IPV6L_NODE || node == BGP_FLOWSPECV4_NODE - || node == BGP_FLOWSPECV6_NODE)) - return false; - - if (target_node == INTERFACE_NODE && node == LINK_PARAMS_NODE) - return false; - - return true; -} - /* This is called from main when a daemon is invoked with -v or --version. */ void print_version(const char *progname) { @@ -922,13 +897,15 @@ static int cmd_execute_command_real(vector vline, enum cmd_filter_type filter, * a match before calling node_exit handlers below */ for (i = 0; i < up_level; i++) { + struct cmd_node *cnode; + if (node <= CONFIG_NODE) return CMD_NO_LEVEL_UP; + cnode = vector_slot(cmdvec, node); node = node_parent(node); - if (xpath_index > 0 - && vty_check_node_for_xpath_decrement(node, vty->node)) + if (xpath_index > 0 && !cnode->no_xpath) xpath_index--; } @@ -1062,12 +1039,13 @@ int cmd_execute_command(vector vline, struct vty *vty, /* This assumes all nodes above CONFIG_NODE are childs of * CONFIG_NODE */ while (vty->node > CONFIG_NODE) { + struct cmd_node *cnode = vector_slot(cmdvec, try_node); + try_node = node_parent(try_node); vty->node = try_node; - if (vty->xpath_index > 0 - && vty_check_node_for_xpath_decrement(try_node, - onode)) + if (vty->xpath_index > 0 && !cnode->no_xpath) vty->xpath_index--; + ret = cmd_execute_command_real(vline, FILTER_RELAXED, vty, cmd, 0); if (ret == CMD_SUCCESS || ret == CMD_WARNING @@ -1386,8 +1364,7 @@ void cmd_exit(struct vty *vty) } if (cnode->parent_node) vty->node = cnode->parent_node; - if (vty->xpath_index > 0 - && vty_check_node_for_xpath_decrement(vty->node, cnode->node)) + if (vty->xpath_index > 0 && !cnode->no_xpath) vty->xpath_index--; } diff --git a/lib/command.h b/lib/command.h index e2086701ad..c888356d61 100644 --- a/lib/command.h +++ b/lib/command.h @@ -213,6 +213,9 @@ struct cmd_node { /* set as soon as any command is in cmdgraph */ bool graph_built; + + /* don't decrement vty->xpath_index on leaving this node */ + bool no_xpath; }; /* Return value of the commands. */ diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c index 2537ff4571..f6c86a321c 100644 --- a/vtysh/vtysh.c +++ b/vtysh/vtysh.c @@ -1279,6 +1279,7 @@ static struct cmd_node bgp_vpnv4_node = { .node = BGP_VPNV4_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_vpnv6_node = { @@ -1286,6 +1287,7 @@ static struct cmd_node bgp_vpnv6_node = { .node = BGP_VPNV6_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_flowspecv4_node = { @@ -1293,6 +1295,7 @@ static struct cmd_node bgp_flowspecv4_node = { .node = BGP_FLOWSPECV4_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_flowspecv6_node = { @@ -1300,6 +1303,7 @@ static struct cmd_node bgp_flowspecv6_node = { .node = BGP_FLOWSPECV6_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_ipv4_node = { @@ -1307,6 +1311,7 @@ static struct cmd_node bgp_ipv4_node = { .node = BGP_IPV4_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_ipv4m_node = { @@ -1314,6 +1319,7 @@ static struct cmd_node bgp_ipv4m_node = { .node = BGP_IPV4M_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_ipv4l_node = { @@ -1321,6 +1327,7 @@ static struct cmd_node bgp_ipv4l_node = { .node = BGP_IPV4L_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_ipv6_node = { @@ -1328,6 +1335,7 @@ static struct cmd_node bgp_ipv6_node = { .node = BGP_IPV6_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_ipv6m_node = { @@ -1335,6 +1343,7 @@ static struct cmd_node bgp_ipv6m_node = { .node = BGP_IPV6M_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_evpn_node = { @@ -1342,6 +1351,7 @@ static struct cmd_node bgp_evpn_node = { .node = BGP_EVPN_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; static struct cmd_node bgp_evpn_vni_node = { @@ -1356,6 +1366,7 @@ static struct cmd_node bgp_ipv6l_node = { .node = BGP_IPV6L_NODE, .parent_node = BGP_NODE, .prompt = "%s(config-router-af)# ", + .no_xpath = true, }; #ifdef ENABLE_BGP_VNC @@ -1516,6 +1527,7 @@ struct cmd_node link_params_node = { .node = LINK_PARAMS_NODE, .parent_node = INTERFACE_NODE, .prompt = "%s(config-link-params)# ", + .no_xpath = true, }; #ifdef HAVE_BGPD diff --git a/zebra/interface.c b/zebra/interface.c index 49a1e49175..c5fe5f3949 100644 --- a/zebra/interface.c +++ b/zebra/interface.c @@ -2905,6 +2905,7 @@ struct cmd_node link_params_node = { .node = LINK_PARAMS_NODE, .parent_node = INTERFACE_NODE, .prompt = "%s(config-link-params)# ", + .no_xpath = true, }; static void link_param_cmd_set_uint32(struct interface *ifp, uint32_t *field, -- 2.39.5