From a2719d0e88ac2d459238f90281cbafe09d088d79 Mon Sep 17 00:00:00 2001 From: Igor Ryzhov Date: Sun, 21 Jun 2020 19:21:51 +0300 Subject: [PATCH] zebra: show interfaces from all VRFs in "show interface ..." commands Signed-off-by: Igor Ryzhov --- lib/if.c | 11 +++++++++ lib/if.h | 3 ++- zebra/interface.c | 62 +++++++++++++++++++++-------------------------- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/lib/if.c b/lib/if.c index f03574e787..e12b4f8c8f 100644 --- a/lib/if.c +++ b/lib/if.c @@ -383,6 +383,17 @@ struct interface *if_lookup_by_name(const char *name, vrf_id_t vrf_id) return RB_FIND(if_name_head, &vrf->ifaces_by_name, &if_tmp); } +struct interface *if_lookup_by_name_vrf(const char *name, struct vrf *vrf) +{ + struct interface if_tmp; + + if (!name || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ) + return NULL; + + strlcpy(if_tmp.name, name, sizeof(if_tmp.name)); + return RB_FIND(if_name_head, &vrf->ifaces_by_name, &if_tmp); +} + struct interface *if_lookup_by_name_all_vrf(const char *name) { struct vrf *vrf; diff --git a/lib/if.h b/lib/if.h index e74e366e74..40e87c1e31 100644 --- a/lib/if.h +++ b/lib/if.h @@ -522,7 +522,9 @@ extern struct interface *if_lookup_prefix(const struct prefix *prefix, size_t if_lookup_by_hwaddr(const uint8_t *hw_addr, size_t addrsz, struct interface ***result, vrf_id_t vrf_id); +struct vrf; extern struct interface *if_lookup_by_name_all_vrf(const char *ifname); +extern struct interface *if_lookup_by_name_vrf(const char *name, struct vrf *vrf); extern struct interface *if_lookup_by_name(const char *ifname, vrf_id_t vrf_id); extern struct interface *if_get_by_name(const char *ifname, vrf_id_t vrf_id); extern struct interface *if_get_by_ifindex(ifindex_t ifindex, vrf_id_t vrf_id, @@ -551,7 +553,6 @@ extern bool if_is_loopback_or_vrf(const struct interface *ifp); extern int if_is_broadcast(const struct interface *ifp); extern int if_is_pointopoint(const struct interface *ifp); extern int if_is_multicast(const struct interface *ifp); -struct vrf; extern void if_terminate(struct vrf *vrf); extern void if_dump_all(void); extern const char *if_flag_dump(unsigned long); diff --git a/zebra/interface.c b/zebra/interface.c index 9d1f70609b..982a63a022 100644 --- a/zebra/interface.c +++ b/zebra/interface.c @@ -1709,7 +1709,7 @@ struct cmd_node interface_node = { #endif /* Show all interfaces to vty. */ DEFPY(show_interface, show_interface_cmd, - "show interface [vrf NAME$vrf_name] [brief$brief]", + "show interface vrf NAME$vrf_name [brief$brief]", SHOW_STR "Interface status and configuration\n" VRF_CMD_HELP_STR @@ -1717,15 +1717,15 @@ DEFPY(show_interface, show_interface_cmd, { struct vrf *vrf; struct interface *ifp; - vrf_id_t vrf_id = VRF_DEFAULT; interface_update_stats(); - if (vrf_name) - VRF_GET_ID(vrf_id, vrf_name, false); + vrf = vrf_lookup_by_name(vrf_name); + if (!vrf) { + vty_out(vty, "%% VRF %s not found\n", vrf_name); + return CMD_WARNING; + } - /* All interface print. */ - vrf = vrf_lookup_by_id(vrf_id); if (brief) { ifs_dump_brief_vty(vty, vrf); } else { @@ -1741,7 +1741,7 @@ DEFPY(show_interface, show_interface_cmd, /* Show all interfaces to vty. */ DEFPY (show_interface_vrf_all, show_interface_vrf_all_cmd, - "show interface vrf all [brief$brief]", + "show interface [vrf all] [brief$brief]", SHOW_STR "Interface status and configuration\n" VRF_ALL_CMD_HELP_STR @@ -1778,14 +1778,17 @@ DEFUN (show_interface_name_vrf, int idx_ifname = 2; int idx_name = 4; struct interface *ifp; - vrf_id_t vrf_id; + struct vrf *vrf; interface_update_stats(); - VRF_GET_ID(vrf_id, argv[idx_name]->arg, false); + vrf = vrf_lookup_by_name(argv[idx_name]->arg); + if (!vrf) { + vty_out(vty, "%% VRF %s not found\n", argv[idx_name]->arg); + return CMD_WARNING; + } - /* Specified interface print. */ - ifp = if_lookup_by_name(argv[idx_ifname]->arg, vrf_id); + ifp = if_lookup_by_name_vrf(argv[idx_ifname]->arg, vrf); if (ifp == NULL) { vty_out(vty, "%% Can't find interface %s\n", argv[idx_ifname]->arg); @@ -1806,35 +1809,23 @@ DEFUN (show_interface_name_vrf_all, VRF_ALL_CMD_HELP_STR) { int idx_ifname = 2; - struct vrf *vrf; struct interface *ifp; - int found = 0; interface_update_stats(); - /* All interface print. */ - RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) { - /* Specified interface print. */ - ifp = if_lookup_by_name(argv[idx_ifname]->arg, vrf->vrf_id); - if (ifp) { - if_dump_vty(vty, ifp); - found++; - } - } - - if (!found) { + ifp = if_lookup_by_name_all_vrf(argv[idx_ifname]->arg); + if (ifp == NULL) { vty_out(vty, "%% Can't find interface %s\n", argv[idx_ifname]->arg); return CMD_WARNING; } + if_dump_vty(vty, ifp); return CMD_SUCCESS; } - -static void if_show_description(struct vty *vty, vrf_id_t vrf_id) +static void if_show_description(struct vty *vty, struct vrf *vrf) { - struct vrf *vrf = vrf_lookup_by_id(vrf_id); struct interface *ifp; vty_out(vty, "Interface Status Protocol Description\n"); @@ -1882,18 +1873,21 @@ static void if_show_description(struct vty *vty, vrf_id_t vrf_id) DEFUN (show_interface_desc, show_interface_desc_cmd, - "show interface description [vrf NAME]", + "show interface description vrf NAME", SHOW_STR "Interface status and configuration\n" "Interface description\n" VRF_CMD_HELP_STR) { - vrf_id_t vrf_id = VRF_DEFAULT; + struct vrf *vrf; - if (argc > 3) - VRF_GET_ID(vrf_id, argv[4]->arg, false); + vrf = vrf_lookup_by_name(argv[4]->arg); + if (!vrf) { + vty_out(vty, "%% VRF %s not found\n", argv[4]->arg); + return CMD_WARNING; + } - if_show_description(vty, vrf_id); + if_show_description(vty, vrf); return CMD_SUCCESS; } @@ -1901,7 +1895,7 @@ DEFUN (show_interface_desc, DEFUN (show_interface_desc_vrf_all, show_interface_desc_vrf_all_cmd, - "show interface description vrf all", + "show interface description [vrf all]", SHOW_STR "Interface status and configuration\n" "Interface description\n" @@ -1913,7 +1907,7 @@ DEFUN (show_interface_desc_vrf_all, if (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) { vty_out(vty, "\n\tVRF %s(%u)\n\n", VRF_LOGNAME(vrf), vrf->vrf_id); - if_show_description(vty, vrf->vrf_id); + if_show_description(vty, vrf); } return CMD_SUCCESS;