From 7b20b86d069b2329caaa878c67448dc03b554fde Mon Sep 17 00:00:00 2001 From: "guozhongfeng.gzf" Date: Sat, 28 Dec 2024 15:39:17 +0800 Subject: [PATCH] staticd: Add new command show static ip/ipv6 route Signed-off-by: guozhongfeng.gzf --- staticd/static_routes.c | 103 ++++++++++++++++++++++++++++++++++++++++ staticd/static_routes.h | 1 + staticd/static_vty.c | 53 +++++++++++++++++++++ 3 files changed, 157 insertions(+) diff --git a/staticd/static_routes.c b/staticd/static_routes.c index cba38183bbf0..436b2c6df02a 100644 --- a/staticd/static_routes.c +++ b/staticd/static_routes.c @@ -691,3 +691,106 @@ void static_get_nh_str(struct static_nexthop *nh, char *nexthop, size_t size) break; }; } + +void static_route_show_nexthop(struct vty *vty, + const struct static_nexthop *sn) +{ + + switch (sn->type) { + case STATIC_IFNAME: + vty_out(vty, " ifname(%d):%s", sn->ifindex, sn->ifname); + break; + case STATIC_IPV4_GATEWAY: + vty_out(vty, " ip4:%pI4", &sn->addr.ipv4); + break; + case STATIC_IPV4_GATEWAY_IFNAME: + vty_out(vty, " ip4-ifindex(%d):%pI4 :%s", sn->ifindex, + &sn->addr.ipv4, sn->ifname); + break; + case STATIC_BLACKHOLE: + vty_out(vty, " blackhole:%d", sn->bh_type); + break; + case STATIC_IPV6_GATEWAY: + vty_out(vty, " ip6:%pI6", &sn->addr.ipv6); + break; + case STATIC_IPV6_GATEWAY_IFNAME: + vty_out(vty, " ip6-ifindex(%d):%pI6 :%s", sn->ifindex, + &sn->addr.ipv6, sn->ifname); + break; + }; + + vty_out(vty, " %s, registered:%s, state:%d\n", + sn->nh_valid ? "valid" : "invalid", + sn->nh_registered ? "yes" : "no", sn->state); +} + +void static_route_show_path(struct vty *vty, struct route_table *stable) +{ + struct route_node *rn; + bool first = true; + int len_rn = 0; + + for (rn = route_top(stable); rn; rn = srcdest_route_next(rn)) { + struct static_route_info *si = static_route_info_from_rnode(rn); + struct static_path *sp; + + if (si == NULL) + continue; + + frr_each (static_path_list, &si->path_list, sp) { + struct static_nexthop *sn; + + len_rn = vty_out(vty, " %pRN", sp->rn); + frr_each (static_nexthop_list, &sp->nexthop_list, sn) { + if (first) + first = false; + else + vty_out(vty, "%*c", len_rn, ' '); + static_route_show_nexthop(vty, sn); + } + } + first = true; + } +} + +void static_route_show(struct vty *vty, afi_t afi, char *vrf_name, bool vrf_all) +{ + struct route_table *stable; + struct static_vrf *svrf; + + vty_out(vty, "Staticd routes:\n"); + if (vrf_all) { + RB_FOREACH (svrf, svrf_name_head, &svrfs) { + stable = static_vrf_static_table(afi, SAFI_UNICAST, svrf); + if (stable) { + vty_out(vty, " VRF %s %s Unicast:\n", svrf->name, afi2str(afi)); + static_route_show_path(vty, stable); + } + + stable = static_vrf_static_table(afi, SAFI_MULTICAST, svrf); + if (stable) { + vty_out(vty, " VRF %s %s Municast:\n", svrf->name, afi2str(afi)); + static_route_show_path(vty, stable); + } + } + } else { + RB_FOREACH (svrf, svrf_name_head, &svrfs) { + if (strcmp(vrf_name, svrf->name) != 0) + continue; + + stable = static_vrf_static_table(afi, SAFI_UNICAST, svrf); + if (stable) { + vty_out(vty, " VRF %s %s Unicast:\n", svrf->name, afi2str(afi)); + static_route_show_path(vty, stable); + } + + stable = static_vrf_static_table(afi, SAFI_MULTICAST, svrf); + if (stable) { + vty_out(vty, " VRF %s %s Municast:\n", svrf->name, afi2str(afi)); + static_route_show_path(vty, stable); + } + } + } + + vty_out(vty, "\n"); +} diff --git a/staticd/static_routes.h b/staticd/static_routes.h index 2e2e4986c348..b156a908d02b 100644 --- a/staticd/static_routes.h +++ b/staticd/static_routes.h @@ -264,6 +264,7 @@ extern void static_bfd_initialize(struct zclient *zc, struct event_loop *tm); extern void static_bfd_show(struct vty *vty, bool isjson); +extern void static_route_show(struct vty *vty, afi_t afi, char *vrf_name, bool vrf_all); #ifdef __cplusplus } #endif diff --git a/staticd/static_vty.c b/staticd/static_vty.c index 07b8bc3d2816..e94c9ddf1f9f 100644 --- a/staticd/static_vty.c +++ b/staticd/static_vty.c @@ -1630,6 +1630,57 @@ DEFPY(staticd_show_bfd_routes, staticd_show_bfd_routes_cmd, return CMD_SUCCESS; } +DEFPY(show_static_ip_route, show_static_route_ip_cmd, + "show static ip route [vrf ]", + "Show running system information\n" + STATICD_STR + "ip address\n" + "BGP route table\n" + VRF_FULL_CMD_HELP_STR) +{ + const char *vrf_name = VRF_DEFAULT_NAME; + bool all_vrf = false; + int idx = 0; + + if (argv_find(argv, argc, "vrf", &idx)) { + vrf_name = argv[idx + 1]->arg; + all_vrf = strmatch(vrf_name, "all"); + } + + if (all_vrf) + static_route_show(vty, AFI_IP, NULL, true); + else + static_route_show(vty, AFI_IP, vrf_name, false); + + return CMD_SUCCESS; +} + +DEFPY(show_static_ipv6_route, show_static_route_ipv6_cmd, + "show static ipv6 route [vrf ]", + "Show running system information\n" + STATICD_STR + "ipv6 address\n" + "BGP route table\n" + VRF_FULL_CMD_HELP_STR) +{ + const char *vrf_name = VRF_DEFAULT_NAME; + bool all_vrf = false; + int idx = 0; + + if (argv_find(argv, argc, "vrf", &idx)) { + vrf_name = argv[idx + 1]->arg; + all_vrf = strmatch(vrf_name, "all"); + } + + if (all_vrf) + static_route_show(vty, AFI_IP6, NULL, true); + else + + static_route_show(vty, AFI_IP6, vrf_name, false); + + return CMD_SUCCESS; +} + DEFUN_NOSH (show_debugging_static, show_debugging_static_cmd, "show debugging [static]", @@ -1653,6 +1704,8 @@ void static_vty_init(void) install_element(CONFIG_NODE, &debug_staticd_cmd); install_element(ENABLE_NODE, &show_debugging_static_cmd); install_element(ENABLE_NODE, &staticd_show_bfd_routes_cmd); + install_element(ENABLE_NODE, &show_static_route_ip_cmd); + install_element(ENABLE_NODE, &show_static_route_ipv6_cmd); #else /* else INCLUDE_MGMTD_CMDDEFS_ONLY */ install_element(CONFIG_NODE, &ip_mroute_dist_cmd);