From ece35fdcb7803f655be804d1143e361fcc6e81a3 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Sat, 21 Jan 2017 07:07:46 -0500 Subject: [PATCH 01/17] lib: Add VRF_ALL define Allow the specification of a VRF_ALL to be used for CLI. Signed-off-by: Donald Sharp --- lib/vrf.h | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/vrf.h b/lib/vrf.h index 96c716a7b8..f8bb07ef48 100644 --- a/lib/vrf.h +++ b/lib/vrf.h @@ -34,6 +34,7 @@ /* The default VRF ID */ #define VRF_DEFAULT 0 #define VRF_UNKNOWN UINT16_MAX +#define VRF_ALL UINT16_MAX - 1 /* Pending: May need to refine this. */ #ifndef IFLA_VRF_MAX From 9dd6d53149ab54c5960eb1b5f4d4248cbbc41853 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Sun, 22 Jan 2017 11:01:09 -0500 Subject: [PATCH 02/17] bgpd: Fix function used only within KEEP_OLD_VPN_COMMANDS The show_adj_route_vpn function is only currently used in conjunction with the KEEP_OLD_VPN_COMMANDS #define. Add this function to that define for the moment. Signed-off-by: Donald Sharp --- bgpd/bgp_mplsvpn.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bgpd/bgp_mplsvpn.c b/bgpd/bgp_mplsvpn.c index bfbddb6c30..dd25fdfaf4 100644 --- a/bgpd/bgp_mplsvpn.c +++ b/bgpd/bgp_mplsvpn.c @@ -566,6 +566,7 @@ DEFUN (no_vpnv6_network, return bgp_static_unset_safi (SAFI_MPLS_VPN, vty, argv[idx_ipv6_prefix]->arg, argv[idx_ext_community]->arg, argv[idx_word]->arg); } +#if defined(KEEP_OLD_VPN_COMMANDS) static int show_adj_route_vpn (struct vty *vty, struct peer *peer, struct prefix_rd *prd, u_char use_json, afi_t afi) { @@ -732,6 +733,7 @@ show_adj_route_vpn (struct vty *vty, struct peer *peer, struct prefix_rd *prd, u } return CMD_SUCCESS; } +#endif enum bgp_show_type { From 3289141f01cda126e9896401e1f74fae2913343a Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Sun, 22 Jan 2017 11:05:04 -0500 Subject: [PATCH 03/17] bgpd: Remove unused function bgp_parse_safi The bgp_parse_safi function is never called remove it. Especially as that later commits will properly handle what this function was trying to do. Signed-off-by: Donald Sharp --- bgpd/bgp_vty.c | 12 +----------- bgpd/bgp_vty.h | 3 --- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c index 2e001b3a9d..ef400380da 100644 --- a/bgpd/bgp_vty.c +++ b/bgpd/bgp_vty.c @@ -167,17 +167,7 @@ bgp_vty_safi_from_arg(const char *safi_str) } int -bgp_parse_safi(const char *str, safi_t *safi) -{ - *safi = bgp_vty_safi_from_arg(str); - if (*safi != SAFI_MAX) - return 0; - else - return -1; -} - -int -argv_find_and_parse_safi(struct cmd_token **argv, int argc, int *index, safi_t *safi) +argv_find_and_parse_safi (struct cmd_token **argv, int argc, int *index, safi_t *safi) { int ret = 0; if (argv_find (argv, argc, "unicast", index)) diff --git a/bgpd/bgp_vty.h b/bgpd/bgp_vty.h index 40c1723218..3f2f573414 100644 --- a/bgpd/bgp_vty.h +++ b/bgpd/bgp_vty.h @@ -51,9 +51,6 @@ peer_and_group_lookup_vty (struct vty *vty, const char *peer_str); extern int bgp_parse_afi(const char *str, afi_t *afi); -extern int -bgp_parse_safi(const char *str, safi_t *safi); - extern afi_t bgp_vty_afi_from_arg(const char *afi_str); From 9fa4d336caa23066f4fa886434d2857798023945 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Sun, 22 Jan 2017 13:13:48 -0500 Subject: [PATCH 04/17] bgpd: Fix vpn commands cli The parser was incorrect for the 'set ... vpn nexthop ...' commands. Signed-off-by: Donald Sharp --- bgpd/bgp_routemap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index 50524baa01..373a8e5d97 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -3992,7 +3992,7 @@ DEFUN (no_set_vpn_nexthop, DEFUN (set_ipx_vpn_nexthop, set_ipx_vpn_nexthop_cmd, - "set vpn next-hop [A.B.C.D|X:X::X:X]", + "set vpn next-hop []", SET_STR "IPv4 information\n" "IPv6 information\n" @@ -4019,7 +4019,7 @@ DEFUN (set_ipx_vpn_nexthop, DEFUN (no_set_ipx_vpn_nexthop, no_set_ipx_vpn_nexthop_cmd, - "no set vpn next-hop [A.B.C.D|X:X::X:X]", + "no set vpn next-hop []", NO_STR SET_STR "IPv4 information\n" From af462945f1a46f80c857e30b72e53ad3610817ee Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Mon, 23 Jan 2017 19:48:24 -0500 Subject: [PATCH 05/17] bgpd: Start cleanup of 'show [ip] bgp ...' commands Create bgp_vty_find_and_parse_afi_safi_vrf that will parse the `show [ip] bgp [ WORD] [ []]' part of a command and to return the correct spot we are in the command. Cleanup 'dampening parameters' part of this command. Consolidate the creation of the bgp data structure to be a bit cleaner. Signed-off-by: Donald Sharp --- bgpd/bgp_route.c | 216 +++++++++++++---------------------------------- bgpd/bgp_vty.c | 34 ++++++++ bgpd/bgp_vty.h | 3 + 3 files changed, 94 insertions(+), 159 deletions(-) diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index 6faf474286..53c0cd822a 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -7235,30 +7235,30 @@ enum bgp_show_type }; static int -bgp_show_prefix_list (struct vty *vty, const char *name, +bgp_show_prefix_list (struct vty *vty, struct bgp *bgp, const char *prefix_list_str, afi_t afi, safi_t safi, enum bgp_show_type type); static int -bgp_show_filter_list (struct vty *vty, const char *name, +bgp_show_filter_list (struct vty *vty, struct bgp *bgp, const char *filter, afi_t afi, safi_t safi, enum bgp_show_type type); static int -bgp_show_route_map (struct vty *vty, const char *name, +bgp_show_route_map (struct vty *vty, struct bgp *bgp, const char *rmap_str, afi_t afi, safi_t safi, enum bgp_show_type type); static int -bgp_show_community_list (struct vty *vty, const char *name, +bgp_show_community_list (struct vty *vty, struct bgp *bgp, const char *com, int exact, afi_t afi, safi_t safi); static int -bgp_show_prefix_longer (struct vty *vty, const char *name, +bgp_show_prefix_longer (struct vty *vty, struct bgp *bgp, const char *prefix, afi_t afi, safi_t safi, enum bgp_show_type type); static int bgp_show_regexp (struct vty *vty, const char *regstr, afi_t afi, safi_t safi, enum bgp_show_type type); static int -bgp_show_community (struct vty *vty, const char *view_name, int argc, +bgp_show_community (struct vty *vty, struct bgp *bgp, int argc, struct cmd_token **argv, int exact, afi_t afi, safi_t safi); static int @@ -7834,8 +7834,8 @@ bgp_show_route (struct vty *vty, const char *view_name, const char *ip_str, } /* BGP route print out function. */ -DEFUN (show_ip_bgp_ipv4, - show_ip_bgp_ipv4_cmd, +DEFUN (show_ip_bgp, + show_ip_bgp_cmd, "show [ip] bgp [ WORD] [ []]\ [<\ cidr-only\ @@ -7862,7 +7862,7 @@ DEFUN (show_ip_bgp_ipv4, "Display detailed information about dampening\n" "Display flap statistics of routes\n" "Display paths suppressed due to dampening\n" - "Display dampening parameters\n" + "Display detail of configured dampening parameters\n" "Display routes matching the route-map\n" "A route-map to match on\n" "Display routes conforming to the prefix-list\n" @@ -7885,67 +7885,58 @@ DEFUN (show_ip_bgp_ipv4, "Display route and more specific routes\n" JSON_STR) { - char *vrf = NULL; + vrf_id_t vrf = VRF_DEFAULT; afi_t afi = AFI_IP6; safi_t safi = SAFI_UNICAST; int exact_match = 0; enum bgp_show_type sh_type = bgp_show_type_normal; - + struct bgp *bgp = NULL; int idx = 0; - if (argv_find (argv, argc, "ip", &idx)) - afi = AFI_IP; - if (argv_find (argv, argc, "view", &idx) || argv_find (argv, argc, "vrf", &idx)) - vrf = argv[++idx]->arg; - if (argv_find (argv, argc, "ipv4", &idx) || argv_find (argv, argc, "ipv6", &idx)) - { - afi = strmatch(argv[idx]->text, "ipv6") ? AFI_IP6 : AFI_IP; - if (argv_find (argv, argc, "unicast", &idx) || argv_find (argv, argc, "multicast", &idx)) - safi = bgp_vty_safi_from_arg (argv[idx]->text); - } - else if (argv_find (argv, argc, "encap", &idx) || argv_find (argv, argc, "vpnv4", &idx)) - { - afi = AFI_IP; - safi = strmatch (argv[idx]->text, "encap") ? SAFI_ENCAP : SAFI_MPLS_VPN; - // advance idx if necessary - argv_find (argv, argc, "unicast", &idx); - } - + idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + if (!idx) + { + vty_out (vty, "View/Vrf Specified: %s is unknown", argv[5]->arg); + return CMD_WARNING; + } int uj = use_json (argc, argv); if (uj) argc--; - struct bgp *bgp = bgp_lookup_by_name (vrf); - if (bgp == NULL) - { - vty_out (vty, "Can't find BGP instance %s%s", vrf, VTY_NEWLINE); - return CMD_WARNING; - } + if (vrf != VRF_ALL) + { + bgp = bgp_lookup_by_vrf_id (vrf); + if (bgp == NULL) + { + vty_out (vty, "Can't find BGP instance %s%s", argv[5]->arg, VTY_NEWLINE); + return CMD_WARNING; + } + } + else + bgp = NULL; - if (++idx < argc) - { - if (strmatch(argv[idx]->text, "cidr-only")) - return bgp_show (vty, bgp, afi, safi, bgp_show_type_cidr_only, NULL, uj); + if (argv_find(argv, argc, "cidr-only", &idx)) + return bgp_show (vty, bgp, afi, safi, bgp_show_type_cidr_only, NULL, uj); - else if (strmatch(argv[idx]->text, "dampening")) + if (argv_find(argv, argc, "dampening", &idx)) { if (argv_find (argv, argc, "dampened-paths", &idx)) return bgp_show (vty, bgp, afi, safi, bgp_show_type_dampend_paths, NULL, uj); else if (argv_find (argv, argc, "flap-statistics", &idx)) return bgp_show (vty, bgp, afi, safi, bgp_show_type_flap_statistics, NULL, uj); else if (argv_find (argv, argc, "parameters", &idx)) - return bgp_show_dampening_parameters (vty, AFI_IP, SAFI_UNICAST); + return bgp_show_dampening_parameters (vty, afi, safi); } - else if (strmatch(argv[idx]->text, "prefix-list")) - return bgp_show_prefix_list (vty, vrf, argv[idx + 1]->arg, afi, safi, bgp_show_type_prefix_list); + if (argv_find(argv, argc, "prefix-list", &idx)) + return bgp_show_prefix_list (vty, bgp, argv[idx + 1]->arg, afi, safi, bgp_show_type_prefix_list); - else if (strmatch(argv[idx]->text, "filter-list")) - return bgp_show_filter_list (vty, vrf, argv[idx + 1]->arg, afi, safi, bgp_show_type_filter_list); + if (argv_find(argv, argc, "filter-list", &idx)) + return bgp_show_filter_list (vty, bgp, argv[idx + 1]->arg, afi, safi, bgp_show_type_filter_list); - else if (strmatch(argv[idx]->text, "route-map")) - return bgp_show_route_map (vty, vrf, argv[idx + 1]->arg, afi, safi, bgp_show_type_route_map); + if (argv_find(argv, argc, "route-map", &idx)) + return bgp_show_route_map (vty, bgp, argv[idx + 1]->arg, afi, safi, bgp_show_type_route_map); - else if (strmatch(argv[idx]->text, "community")) + if (argv_find(argv, argc, "community", &idx)) { /* show a specific community */ if (argv[idx + 1]->type == VARIABLE_TKN || @@ -7955,23 +7946,22 @@ DEFUN (show_ip_bgp_ipv4, { if (strmatch(argv[idx + 2]->text, "exact_match")) exact_match = 1; - return bgp_show_community (vty, vrf, argc, argv, exact_match, afi, safi); + return bgp_show_community (vty, bgp, argc, argv, exact_match, afi, safi); } /* show all communities */ else return bgp_show (vty, bgp, afi, safi, bgp_show_type_community_all, NULL, uj); } - else if (strmatch(argv[idx]->text, "community-list")) - { - const char *clist_number_or_name = argv[++idx]->arg; - if (++idx < argc && strmatch (argv[idx]->arg, "exact-match")) - exact_match = 1; - return bgp_show_community_list (vty, vrf, clist_number_or_name, exact_match, afi, safi); - } - /* prefix-longer */ - else if (argv[idx]->type == IPV4_TKN || argv[idx]->type == IPV6_TKN) - return bgp_show_prefix_longer (vty, vrf, argv[idx + 1]->arg, afi, safi, bgp_show_type_prefix_longer); - } + if (argv_find(argv, argc, "community-list", &idx)) + { + const char *clist_number_or_name = argv[++idx]->arg; + if (++idx < argc && strmatch (argv[idx]->arg, "exact-match")) + exact_match = 1; + return bgp_show_community_list (vty, bgp, clist_number_or_name, exact_match, afi, safi); + } + /* prefix-longer */ + if (argv_find(argv, argc, "A.B.C.D/M", &idx) || argv_find(argv, argc, "X:X::X:X/M", &idx)) + return bgp_show_prefix_longer (vty, bgp, argv[idx + 1]->arg, afi, safi, bgp_show_type_prefix_longer); return bgp_show (vty, bgp, afi, safi, sh_type, NULL, uj); } @@ -8185,18 +8175,11 @@ bgp_show_regexp (struct vty *vty, const char *regstr, afi_t afi, } static int -bgp_show_prefix_list (struct vty *vty, const char *name, +bgp_show_prefix_list (struct vty *vty, struct bgp *bgp, const char *prefix_list_str, afi_t afi, safi_t safi, enum bgp_show_type type) { struct prefix_list *plist; - struct bgp *bgp = NULL; - - if (name && !(bgp = bgp_lookup_by_name(name))) - { - vty_out (vty, "%% No such BGP instance exists%s", VTY_NEWLINE); - return CMD_WARNING; - } plist = prefix_list_lookup (afi, prefix_list_str); if (plist == NULL) @@ -8210,18 +8193,11 @@ bgp_show_prefix_list (struct vty *vty, const char *name, } static int -bgp_show_filter_list (struct vty *vty, const char *name, +bgp_show_filter_list (struct vty *vty, struct bgp *bgp, const char *filter, afi_t afi, safi_t safi, enum bgp_show_type type) { struct as_list *as_list; - struct bgp *bgp = NULL; - - if (name && !(bgp = bgp_lookup_by_name(name))) - { - vty_out (vty, "%% No such BGP instance exists%s", VTY_NEWLINE); - return CMD_WARNING; - } as_list = as_list_lookup (filter); if (as_list == NULL) @@ -8233,53 +8209,12 @@ bgp_show_filter_list (struct vty *vty, const char *name, return bgp_show (vty, bgp, afi, safi, type, as_list, 0); } -DEFUN (show_ip_bgp_dampening_info, - show_ip_bgp_dampening_params_cmd, - "show [ip] bgp dampening parameters", - SHOW_STR - IP_STR - BGP_STR - "Display detailed information about dampening\n" - "Display detail of configured dampening parameters\n") -{ - return bgp_show_dampening_parameters (vty, AFI_IP, SAFI_UNICAST); -} - - -DEFUN (show_ip_bgp_ipv4_dampening_parameters, - show_ip_bgp_ipv4_dampening_parameters_cmd, - "show [ip] bgp ipv4 dampening parameters", - SHOW_STR - IP_STR - BGP_STR - "Address Family\n" - "Address Family modifier\n" - "Address Family modifier\n" - "Display detailed information about dampening\n" - "Display detail of configured dampening parameters\n") -{ - int idx_safi = 4; - if (strncmp(argv[idx_safi]->arg, "m", 1) == 0) - return bgp_show_dampening_parameters (vty, AFI_IP, SAFI_MULTICAST); - - return bgp_show_dampening_parameters (vty, AFI_IP, SAFI_UNICAST); -} - static int -bgp_show_route_map (struct vty *vty, const char *name, +bgp_show_route_map (struct vty *vty, struct bgp *bgp, const char *rmap_str, afi_t afi, safi_t safi, enum bgp_show_type type) { struct route_map *rmap; - struct bgp *bgp = NULL; - - if (name && !(bgp = bgp_lookup_by_name(name))) - { - - - vty_out (vty, "%% No such BGP instance exists%s", VTY_NEWLINE); - return CMD_WARNING; - } rmap = route_map_lookup_by_name (rmap_str); if (! rmap) @@ -8293,36 +8228,15 @@ bgp_show_route_map (struct vty *vty, const char *name, } static int -bgp_show_community (struct vty *vty, const char *view_name, int argc, +bgp_show_community (struct vty *vty, struct bgp *bgp, int argc, struct cmd_token **argv, int exact, afi_t afi, safi_t safi) { struct community *com; struct buffer *b; - struct bgp *bgp; int i; char *str; int first = 0; - /* BGP structure lookup */ - if (view_name) - { - bgp = bgp_lookup_by_name (view_name); - if (bgp == NULL) - { - vty_out (vty, "Can't find BGP instance %s%s", view_name, VTY_NEWLINE); - return CMD_WARNING; - } - } - else - { - bgp = bgp_get_default (); - if (bgp == NULL) - { - vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE); - return CMD_WARNING; - } - } - b = buffer_new (1024); for (i = 0; i < argc; i++) { @@ -8356,18 +8270,11 @@ bgp_show_community (struct vty *vty, const char *view_name, int argc, } static int -bgp_show_community_list (struct vty *vty, const char *name, +bgp_show_community_list (struct vty *vty, struct bgp *bgp, const char *com, int exact, afi_t afi, safi_t safi) { struct community_list *list; - struct bgp *bgp = NULL; - - if (name && !(bgp = bgp_lookup_by_name(name))) - { - vty_out (vty, "%% No such BGP instance exists%s", VTY_NEWLINE); - return CMD_WARNING; - } list = community_list_lookup (bgp_clist, com, COMMUNITY_LIST_MASTER); if (list == NULL) @@ -8383,19 +8290,12 @@ bgp_show_community_list (struct vty *vty, const char *name, } static int -bgp_show_prefix_longer (struct vty *vty, const char *name, +bgp_show_prefix_longer (struct vty *vty, struct bgp *bgp, const char *prefix, afi_t afi, safi_t safi, enum bgp_show_type type) { int ret; struct prefix *p; - struct bgp *bgp = NULL; - - if (name && !(bgp = bgp_lookup_by_name(name))) - { - vty_out (vty, "%% No such BGP instance exists%s", VTY_NEWLINE); - return CMD_WARNING; - } p = prefix_new(); @@ -10527,14 +10427,12 @@ bgp_route_init (void) install_element (BGP_IPV4M_NODE, &no_aggregate_address_mask_cmd); install_element (VIEW_NODE, &show_ip_bgp_instance_all_cmd); - install_element (VIEW_NODE, &show_ip_bgp_ipv4_cmd); + install_element (VIEW_NODE, &show_ip_bgp_cmd); install_element (VIEW_NODE, &show_ip_bgp_route_cmd); install_element (VIEW_NODE, &show_ip_bgp_regexp_cmd); install_element (VIEW_NODE, &show_ip_bgp_instance_neighbor_advertised_route_cmd); install_element (VIEW_NODE, &show_ip_bgp_neighbor_routes_cmd); install_element (VIEW_NODE, &show_ip_bgp_neighbor_received_prefix_filter_cmd); - install_element (VIEW_NODE, &show_ip_bgp_dampening_params_cmd); - install_element (VIEW_NODE, &show_ip_bgp_ipv4_dampening_parameters_cmd); #ifdef KEEP_OLD_VPN_COMMANDS install_element (VIEW_NODE, &show_ip_bgp_vpn_all_route_prefix_cmd); #endif /* KEEP_OLD_VPN_COMMANDS */ diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c index ef400380da..7f4bd49d29 100644 --- a/bgpd/bgp_vty.c +++ b/bgpd/bgp_vty.c @@ -197,6 +197,40 @@ argv_find_and_parse_safi (struct cmd_token **argv, int argc, int *index, safi_t return ret; } +int +bgp_vty_find_and_parse_afi_safi_vrf (struct vty *vty, struct cmd_token **argv, int argc, int idx, + afi_t *afi, safi_t *safi, vrf_id_t *vrf) +{ + char *vrf_name = NULL; + + if (argv_find (argv, argc, "ip", &idx)) + *afi = AFI_IP; + + if (argv_find (argv, argc, "view", &idx) || argv_find (argv, argc, "vrf", &idx)) + { + vrf_name = argv[idx + 1]->arg; + idx += 2; + } + + if (argv_find_and_parse_afi (argv, argc, &idx, afi)) + argv_find_and_parse_safi (argv, argc, &idx, safi); + + if (vrf_name) + { + if (strmatch(vrf_name, "all")) + *vrf = VRF_ALL; + else + *vrf = vrf_name_to_id (vrf_name); + } + + if (*vrf == VRF_UNKNOWN) + { + vty_out (vty, "View/Vrf specified is unknown: %s", vrf_name); + return 0; + } + return idx + 1; +} + static int peer_address_self_check (struct bgp *bgp, union sockunion *su) { diff --git a/bgpd/bgp_vty.h b/bgpd/bgp_vty.h index 3f2f573414..cfc4f6cdfb 100644 --- a/bgpd/bgp_vty.h +++ b/bgpd/bgp_vty.h @@ -63,4 +63,7 @@ argv_find_and_parse_afi(struct cmd_token **argv, int argc, int *index, afi_t *af extern int argv_find_and_parse_safi(struct cmd_token **argv, int argc, int *index, safi_t *safi); +extern int +bgp_vty_find_and_parse_afi_safi_vrf (struct vty *vty, struct cmd_token **argv, int argc, int idx, + afi_t *afi, safi_t *safi, vrf_id_t *vrf); #endif /* _QUAGGA_BGP_VTY_H */ From c41247f50e8136d8f32bcc2ec83e0b87985b4c10 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Mon, 23 Jan 2017 19:58:56 -0500 Subject: [PATCH 06/17] bgpd: Cleanup 'show .... ' Cleanup the bgp bestpath or multipath show commands. Signed-off-by: Donald Sharp --- bgpd/bgp_route.c | 68 ++++++++++++++++-------------------------------- 1 file changed, 22 insertions(+), 46 deletions(-) diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index 53c0cd822a..25fd988009 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -7801,33 +7801,11 @@ bgp_show_route_in_table (struct vty *vty, struct bgp *bgp, /* Display specified route of Main RIB */ static int -bgp_show_route (struct vty *vty, const char *view_name, const char *ip_str, +bgp_show_route (struct vty *vty, struct bgp *bgp, const char *ip_str, afi_t afi, safi_t safi, struct prefix_rd *prd, int prefix_check, enum bgp_path_type pathtype, u_char use_json) { - struct bgp *bgp; - - /* BGP structure lookup. */ - if (view_name) - { - bgp = bgp_lookup_by_name (view_name); - if (bgp == NULL) - { - vty_out (vty, "Can't find BGP instance %s%s", view_name, VTY_NEWLINE); - return CMD_WARNING; - } - } - else - { - bgp = bgp_get_default (); - if (bgp == NULL) - { - vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE); - return CMD_WARNING; - } - } - return bgp_show_route_in_table (vty, bgp, bgp->rib[afi][safi], ip_str, afi, safi, prd, prefix_check, pathtype, use_json); @@ -7992,34 +7970,32 @@ DEFUN (show_ip_bgp_route, afi_t afi = AFI_IP6; safi_t safi = SAFI_UNICAST; - char *vrf = NULL; + vrf_id_t vrf = VRF_DEFAULT;; char *prefix = NULL; - + struct bgp *bgp = NULL; enum bgp_path_type path_type; u_char uj = use_json(argc, argv); int idx = 0; - /* show [ip] bgp */ - if (argv_find (argv, argc, "ip", &idx)) - afi = AFI_IP; - /* [ WORD] */ - if (argv_find (argv, argc, "view", &idx) || argv_find (argv, argc, "vrf", &idx)) - vrf = argv[++idx]->arg; - /* []|ipv6 []|encap [unicast]|vpnv4 [unicast]>] */ - if (argv_find (argv, argc, "ipv4", &idx) || argv_find (argv, argc, "ipv6", &idx)) - { - afi = strmatch(argv[idx]->text, "ipv6") ? AFI_IP6 : AFI_IP; - if (argv_find (argv, argc, "unicast", &idx) || argv_find (argv, argc, "multicast", &idx)) - safi = strmatch (argv[idx]->text, "unicast") ? SAFI_UNICAST : SAFI_MULTICAST; - } - else if (argv_find (argv, argc, "encap", &idx) || argv_find (argv, argc, "vpnv4", &idx)) - { - afi = AFI_IP; - safi = strmatch (argv[idx]->text, "encap") ? SAFI_ENCAP : SAFI_MPLS_VPN; - // advance idx if necessary - argv_find (argv, argc, "unicast", &idx); - } + idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + if (!idx) + { + vty_out (vty, "View/Vrf Specified: %s is unknown", argv[5]->arg); + return CMD_WARNING; + } + + if (vrf != VRF_ALL) + { + bgp = bgp_lookup_by_vrf_id (vrf); + if (bgp == NULL) + { + vty_out (vty, "Can't find BGP instance %s%s", argv[5]->arg, VTY_NEWLINE); + return CMD_WARNING; + } + } + else + bgp = NULL; /* */ if (argv_find (argv, argc, "A.B.C.D", &idx) || argv_find (argv, argc, "X:X::X:X", &idx)) @@ -8048,7 +8024,7 @@ DEFUN (show_ip_bgp_route, else path_type = BGP_PATH_ALL; - return bgp_show_route (vty, vrf, prefix, afi, safi, NULL, prefix_check, path_type, uj); + return bgp_show_route (vty, bgp, prefix, afi, safi, NULL, prefix_check, path_type, uj); } DEFUN (show_ip_bgp_regexp, From b00b230aa1bf6775b3207c8d0a6b61c36b892449 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Mon, 23 Jan 2017 20:29:54 -0500 Subject: [PATCH 07/17] bgp: Fix 'show .... regexp REGEX...' Fix this command to use the correct format for a show command. Signed-off-by: Donald Sharp --- bgpd/bgp_route.c | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index 25fd988009..8adc6bd971 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -8029,46 +8029,31 @@ DEFUN (show_ip_bgp_route, DEFUN (show_ip_bgp_regexp, show_ip_bgp_regexp_cmd, - "show [ip] bgp []|ipv6 []|encap [unicast]|vpnv4 [unicast]>] regexp REGEX...", + "show [ip] bgp [ WORD] [ []] regexp REGEX...", SHOW_STR IP_STR BGP_STR + BGP_INSTANCE_HELP_STR + "Address Family\n" "Address Family\n" "Address Family modifier\n" "Address Family modifier\n" "Address Family modifier\n" "Address Family modifier\n" - "Address Family\n" - "Address Family modifier\n" - "Address Family modifier\n" - "Address Family modifier\n" - "Address Family modifier\n" - "Address Family\n" - "Address Family modifier\n" - "Address Family\n" - "Address Family modifier\n" "Display routes matching the AS path regular expression\n" "A regular-expression to match the BGP AS paths\n") { + vrf_id_t vrf = VRF_DEFAULT; afi_t afi = AFI_IP6; safi_t safi = SAFI_UNICAST; int idx = 0; - - /* []|ipv6 []|encap [unicast]|vpnv4 [unicast]>] */ - if (argv_find (argv, argc, "ipv4", &idx) || argv_find (argv, argc, "ipv6", &idx)) - { - afi = strmatch(argv[idx]->text, "ipv6") ? AFI_IP6 : AFI_IP; - if (argv_find (argv, argc, "unicast", &idx) || argv_find (argv, argc, "multicast", &idx)) - safi = strmatch (argv[idx]->text, "unicast") ? SAFI_UNICAST : SAFI_MULTICAST; - } - else if (argv_find (argv, argc, "encap", &idx) || argv_find (argv, argc, "vpnv4", &idx)) - { - afi = AFI_IP; - safi = strmatch (argv[idx]->text, "encap") ? SAFI_ENCAP : SAFI_MPLS_VPN; - // advance idx if necessary - argv_find (argv, argc, "unicast", &idx); - } + idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + if (!idx) + { + vty_out (vty, "View/Vrf Specified: %s is unknown", argv[5]->arg); + return CMD_WARNING; + } // get index of regex argv_find (argv, argc, "regexp", &idx); From f2a8972b0b746522ab4442a30f4b27da6f3620a2 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Mon, 23 Jan 2017 20:50:50 -0500 Subject: [PATCH 08/17] bgpd: Cleanup 'view all' command 1) Make [ WORD] consistent 2) Fix inconsistent help string 3) Fix the show .. vrf all command Signed-off-by: Donald Sharp --- bgpd/bgp_nexthop.c | 4 ++-- bgpd/bgp_route.c | 36 ++++++++++-------------------------- bgpd/bgp_vty.c | 6 +++--- 3 files changed, 15 insertions(+), 31 deletions(-) diff --git a/bgpd/bgp_nexthop.c b/bgpd/bgp_nexthop.c index 0a9747b526..0cf96101c2 100644 --- a/bgpd/bgp_nexthop.c +++ b/bgpd/bgp_nexthop.c @@ -492,7 +492,7 @@ bgp_show_all_instances_nexthops_vty (struct vty *vty) DEFUN (show_ip_bgp_nexthop, show_ip_bgp_nexthop_cmd, - "show [ip] bgp [ VRFNAME] nexthop [detail]", + "show [ip] bgp [ WORD] nexthop [detail]", SHOW_STR IP_STR BGP_STR @@ -501,7 +501,7 @@ DEFUN (show_ip_bgp_nexthop, "Show detailed information\n") { int idx = 0; - char *vrf = argv_find (argv, argc, "VRFNAME", &idx) ? argv[idx]->arg : NULL; + char *vrf = argv_find (argv, argc, "WORD", &idx) ? argv[idx]->arg : NULL; int detail = argv_find (argv, argc, "detail", &idx) ? 1 : 0; return show_ip_bgp_nexthop_table (vty, vrf, detail); } diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index 8adc6bd971..c9fcbf9fb4 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -8067,53 +8067,37 @@ DEFUN (show_ip_bgp_regexp, DEFUN (show_ip_bgp_instance_all, show_ip_bgp_instance_all_cmd, - "show [ip] bgp all []|ipv6 []|encap [unicast]|vpnv4 [unicast]>] [json]", + "show [ip] bgp all [ []] [json]", SHOW_STR IP_STR BGP_STR BGP_INSTANCE_ALL_HELP_STR "Address Family\n" - "Address Family modifier\n" - "Address Family modifier\n" "Address Family\n" "Address Family modifier\n" "Address Family modifier\n" - "Address Family\n" "Address Family modifier\n" - "Address Family\n" "Address Family modifier\n" JSON_STR) { + vrf_id_t vrf = VRF_DEFAULT; afi_t afi = AFI_IP; safi_t safi = SAFI_UNICAST; int idx = 0; - - /* show [ip] bgp */ - if (argv_find (argv, argc, "ip", &idx)) - afi = AFI_IP; - /* []|ipv6 []|encap [unicast]|vpnv4 [unicast]>] */ - if (argv_find (argv, argc, "ipv4", &idx) || argv_find (argv, argc, "ipv6", &idx)) - { - afi = strmatch(argv[idx]->text, "ipv6") ? AFI_IP6 : AFI_IP; - if (argv_find (argv, argc, "unicast", &idx) || argv_find (argv, argc, "multicast", &idx)) - safi = strmatch (argv[idx]->text, "unicast") ? SAFI_UNICAST : SAFI_MULTICAST; - } - else if (argv_find (argv, argc, "encap", &idx) || argv_find (argv, argc, "vpnv4", &idx)) - { - afi = AFI_IP; - safi = strmatch (argv[idx]->text, "encap") ? SAFI_ENCAP : SAFI_MPLS_VPN; - // advance idx if necessary - argv_find (argv, argc, "unicast", &idx); - } - - u_char uj = use_json(argc, argv); + idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + if (!idx) + { + vty_out (vty, "View/Vrf Specified: %s is unknown", argv[5]->arg); + return CMD_WARNING; + } + int uj = use_json (argc, argv); + if (uj) argc--; bgp_show_all_instances_routes_vty (vty, afi, safi, uj); return CMD_SUCCESS; } - static int bgp_show_regexp (struct vty *vty, const char *regstr, afi_t afi, safi_t safi, enum bgp_show_type type) diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c index 7f4bd49d29..fa19308cb1 100644 --- a/bgpd/bgp_vty.c +++ b/bgpd/bgp_vty.c @@ -8605,7 +8605,7 @@ DEFUN (show_ip_bgp_neighbors, SHOW_STR IP_STR BGP_STR - BGP_INSTANCE_ALL_HELP_STR + BGP_INSTANCE_HELP_STR "Address Family\n" "Address Family\n" "Address Family\n" @@ -9241,7 +9241,7 @@ bgp_show_peer_group_vty (struct vty *vty, const char *name, DEFUN (show_ip_bgp_peer_groups, show_ip_bgp_peer_groups_cmd, - "show [ip] bgp [ VRFNAME] peer-group [PGNAME]", + "show [ip] bgp [ WORD] peer-group [PGNAME]", SHOW_STR IP_STR BGP_STR @@ -9253,7 +9253,7 @@ DEFUN (show_ip_bgp_peer_groups, vrf = pg = NULL; int idx = 0; - vrf = argv_find (argv, argc, "VRFNAME", &idx) ? argv[idx]->arg : NULL; + vrf = argv_find (argv, argc, "WORD", &idx) ? argv[idx]->arg : NULL; pg = argv_find (argv, argc, "PGNAME", &idx) ? argv[idx]->arg : NULL; return bgp_show_peer_group_vty (vty, vrf, show_all_groups, pg); From e01ca200c6db1b7d5ae17c8d90b9ea39ca7382cb Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Mon, 23 Jan 2017 21:34:36 -0500 Subject: [PATCH 09/17] bgpd: Cleanup 'show .... statistics' command Signed-off-by: Donald Sharp --- bgpd/bgp_route.c | 86 ++++-------------------------------------------- 1 file changed, 7 insertions(+), 79 deletions(-) diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index c9fcbf9fb4..48636ef34a 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -7811,6 +7811,8 @@ bgp_show_route (struct vty *vty, struct bgp *bgp, const char *ip_str, use_json); } +static int bgp_table_stats (struct vty *vty, struct bgp *bgp, afi_t afi, safi_t safi); + /* BGP route print out function. */ DEFUN (show_ip_bgp, show_ip_bgp_cmd, @@ -7821,6 +7823,7 @@ DEFUN (show_ip_bgp, |route-map WORD\ |prefix-list WORD\ |filter-list WORD\ + |statistics\ |community [ [exact-match]]\ |community-list <(1-500)|WORD> [exact-match]\ |A.B.C.D/M longer-prefixes\ @@ -7847,6 +7850,7 @@ DEFUN (show_ip_bgp, "Prefix-list name\n" "Display routes conforming to the filter-list\n" "Regular expression access list name\n" + "BGP RIB advertisement statistics\n" "Display routes matching the communities\n" COMMUNITY_AANN_STR "Do not send outside local AS (well-known community)\n" @@ -7911,6 +7915,9 @@ DEFUN (show_ip_bgp, if (argv_find(argv, argc, "filter-list", &idx)) return bgp_show_filter_list (vty, bgp, argv[idx + 1]->arg, afi, safi, bgp_show_type_filter_list); + if (argv_find(argv, argc, "statistics", &idx)) + return bgp_table_stats (vty, bgp, afi, safi); + if (argv_find(argv, argc, "route-map", &idx)) return bgp_show_route_map (vty, bgp, argv[idx + 1]->arg, afi, safi, bgp_show_type_route_map); @@ -8598,81 +8605,6 @@ bgp_table_stats (struct vty *vty, struct bgp *bgp, afi_t afi, safi_t safi) return CMD_SUCCESS; } -static int -bgp_table_stats_vty (struct vty *vty, const char *name, - const char *afi_str, const char *safi_str) -{ - struct bgp *bgp; - afi_t afi; - safi_t safi; - - if (name) - bgp = bgp_lookup_by_name (name); - else - bgp = bgp_get_default (); - - if (!bgp) - { - vty_out (vty, "%% No such BGP instance exist%s", VTY_NEWLINE); - return CMD_WARNING; - } - afi = bgp_vty_afi_from_arg(afi_str); - if (afi == AFI_MAX) - { - vty_out (vty, "%% Invalid address family \"%s\"%s", - afi_str, VTY_NEWLINE); - return CMD_WARNING; - } - safi = bgp_vty_safi_from_arg(safi_str); - if (safi == SAFI_MAX) - { - vty_out (vty, "%% Invalid subsequent address family %s%s", - safi_str, VTY_NEWLINE); - return CMD_WARNING; - } - - return bgp_table_stats (vty, bgp, afi, safi); -} - -DEFUN (show_bgp_statistics, - show_bgp_statistics_cmd, - "show [ip] bgp statistics", - SHOW_STR - IP_STR - BGP_STR - "Address Family\n" - "Address Family\n" - "Address Family modifier\n" - "Address Family modifier\n" - "Address Family modifier\n" - "Address Family modifier\n" - "BGP RIB advertisement statistics\n") -{ - int idx_afi = 2; - int idx_safi = 3; - return bgp_table_stats_vty (vty, NULL, argv[idx_afi]->arg, argv[idx_safi]->arg); -} - -DEFUN (show_bgp_statistics_view, - show_bgp_statistics_view_cmd, - "show [ip] bgp WORD statistics", - SHOW_STR - IP_STR - BGP_STR - BGP_INSTANCE_HELP_STR - "Address Family\n" - "Address Family\n" - "Address Family modifier\n" - "Address Family modifier\n" - "Address Family modifier\n" - "Address Family modifier\n" - "BGP RIB advertisement statistics\n") -{ - int idx_word = 3; - int idx_afi = 4; - return bgp_table_stats_vty (vty, NULL, argv[idx_word]->arg, argv[idx_afi]->arg); -} - enum bgp_pcounts { PCOUNT_ADJ_IN = 0, @@ -10411,10 +10343,6 @@ bgp_route_init (void) install_element (BGP_IPV6M_NODE, &ipv6_bgp_network_cmd); install_element (BGP_IPV6M_NODE, &no_ipv6_bgp_network_cmd); - /* Statistics */ - install_element (ENABLE_NODE, &show_bgp_statistics_cmd); - install_element (ENABLE_NODE, &show_bgp_statistics_view_cmd); - install_element (BGP_NODE, &bgp_distance_cmd); install_element (BGP_NODE, &no_bgp_distance_cmd); install_element (BGP_NODE, &bgp_distance_source_cmd); From 30a6a167a191f6898e1dda754daf6cd054196eae Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Mon, 23 Jan 2017 22:30:37 -0500 Subject: [PATCH 10/17] bgpd: Clean up 'show bgp neighbor ...' commands Signed-off-by: Donald Sharp --- bgpd/bgp_route.c | 329 ++++++++++++++++------------------------------- 1 file changed, 112 insertions(+), 217 deletions(-) diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index 48636ef34a..c5b7c4e776 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -8264,52 +8264,13 @@ bgp_show_prefix_longer (struct vty *vty, struct bgp *bgp, } static struct peer * -peer_lookup_in_view (struct vty *vty, const char *view_name, +peer_lookup_in_view (struct vty *vty, struct bgp *bgp, const char *ip_str, u_char use_json) { int ret; - struct bgp *bgp; struct peer *peer; union sockunion su; - /* BGP structure lookup. */ - if (view_name) - { - bgp = bgp_lookup_by_name (view_name); - if (! bgp) - { - if (use_json) - { - json_object *json_no = NULL; - json_no = json_object_new_object(); - json_object_string_add(json_no, "warning", "Can't find BGP view"); - vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE); - json_object_free(json_no); - } - else - vty_out (vty, "Can't find BGP instance %s%s", view_name, VTY_NEWLINE); - return NULL; - } - } - else - { - bgp = bgp_get_default (); - if (! bgp) - { - if (use_json) - { - json_object *json_no = NULL; - json_no = json_object_new_object(); - json_object_string_add(json_no, "warning", "No BGP process configured"); - vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE); - json_object_free(json_no); - } - else - vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE); - return NULL; - } - } - /* Get peer sockunion. */ ret = str2sockunion (ip_str, &su); if (ret < 0) @@ -8796,117 +8757,20 @@ bgp_peer_counts (struct vty *vty, struct peer *peer, afi_t afi, safi_t safi, u_c return CMD_SUCCESS; } -DEFUN (show_ip_bgp_neighbor_prefix_counts, - show_ip_bgp_neighbor_prefix_counts_cmd, - "show [ip] bgp neighbors prefix-counts [json]", - SHOW_STR - IP_STR - BGP_STR - "Detailed information on TCP and BGP neighbor connections\n" - "Neighbor to display information about\n" - "Neighbor to display information about\n" - "Neighbor on BGP configured interface\n" - "Display detailed prefix count information\n" - JSON_STR) -{ - int idx_peer = 4; - struct peer *peer; - u_char uj = use_json(argc, argv); - - peer = peer_lookup_in_view (vty, NULL, argv[idx_peer]->arg, uj); - if (! peer) - return CMD_WARNING; - - return bgp_peer_counts (vty, peer, AFI_IP, SAFI_UNICAST, uj); -} - DEFUN (show_ip_bgp_instance_neighbor_prefix_counts, show_ip_bgp_instance_neighbor_prefix_counts_cmd, - "show [ip] bgp WORD neighbors prefix-counts [json]", - SHOW_STR - IP_STR - BGP_STR - BGP_INSTANCE_HELP_STR - "Detailed information on TCP and BGP neighbor connections\n" - "Neighbor to display information about\n" - "Neighbor to display information about\n" - "Neighbor on BGP configured interface\n" - "Display detailed prefix count information\n" - JSON_STR) -{ - int idx_word = 4; - int idx_peer = 6; - struct peer *peer; - u_char uj = use_json(argc, argv); - - peer = peer_lookup_in_view (vty, argv[idx_word]->arg, argv[idx_peer]->arg, uj); - if (! peer) - return CMD_WARNING; - - return bgp_peer_counts (vty, peer, AFI_IP, SAFI_UNICAST, uj); -} - -DEFUN (show_bgp_ipv6_neighbor_prefix_counts, - show_bgp_ipv6_neighbor_prefix_counts_cmd, - "show [ip] bgp ipv6 neighbors prefix-counts [json]", - SHOW_STR - IP_STR - BGP_STR - "Address Family\n" - "Detailed information on TCP and BGP neighbor connections\n" - "Neighbor to display information about\n" - "Neighbor to display information about\n" - "Neighbor on BGP configured interface\n" - "Display detailed prefix count information\n" - JSON_STR) -{ - int idx_peer = 4; - struct peer *peer; - u_char uj = use_json(argc, argv); - - peer = peer_lookup_in_view (vty, NULL, argv[idx_peer]->arg, uj); - if (! peer) - return CMD_WARNING; - - return bgp_peer_counts (vty, peer, AFI_IP6, SAFI_UNICAST, uj); -} - -DEFUN (show_bgp_instance_ipv6_neighbor_prefix_counts, - show_bgp_instance_ipv6_neighbor_prefix_counts_cmd, - "show [ip] bgp WORD ipv6 neighbors prefix-counts [json]", + "show [ip] bgp [ WORD] [ []] " + "neighbors prefix-counts [json]", SHOW_STR IP_STR BGP_STR BGP_INSTANCE_HELP_STR "Address Family\n" - "Detailed information on TCP and BGP neighbor connections\n" - "Neighbor to display information about\n" - "Neighbor to display information about\n" - "Neighbor on BGP configured interface\n" - "Display detailed prefix count information\n" - JSON_STR) -{ - int idx_word = 3; - int idx_peer = 6; - struct peer *peer; - u_char uj = use_json(argc, argv); - - peer = peer_lookup_in_view (vty, argv[idx_word]->arg, argv[idx_peer]->arg, uj); - if (! peer) - return CMD_WARNING; - - return bgp_peer_counts (vty, peer, AFI_IP6, SAFI_UNICAST, uj); -} - -DEFUN (show_ip_bgp_ipv4_neighbor_prefix_counts, - show_ip_bgp_ipv4_neighbor_prefix_counts_cmd, - "show [ip] bgp ipv4 neighbors prefix-counts [json]", - SHOW_STR - IP_STR - BGP_STR "Address Family\n" "Address Family modifier\n" "Address Family modifier\n" + "Address Family modifier\n" + "Address Family modifier\n" "Detailed information on TCP and BGP neighbor connections\n" "Neighbor to display information about\n" "Neighbor to display information about\n" @@ -8914,18 +8778,48 @@ DEFUN (show_ip_bgp_ipv4_neighbor_prefix_counts, "Display detailed prefix count information\n" JSON_STR) { - int idx_safi = 4; - int idx_peer = 6; + vrf_id_t vrf = VRF_DEFAULT; + afi_t afi = AFI_IP6; + safi_t safi = SAFI_UNICAST; struct peer *peer; - u_char uj = use_json(argc, argv); + int idx = 0; + struct bgp *bgp = NULL; - peer = peer_lookup_in_view (vty, NULL, argv[idx_peer]->arg, uj); + idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + if (!idx) + { + vty_out (vty, "View/Vrf Specified: %s is unknown", argv[5]->arg); + return CMD_WARNING; + } + int uj = use_json (argc, argv); + if (uj) argc--; + + if (vrf != VRF_ALL) + { + bgp = bgp_lookup_by_vrf_id (vrf); + if (bgp == NULL) + { + if (uj) + { + json_object *json_no = NULL; + json_no = json_object_new_object(); + json_object_string_add(json_no, "warning", "Can't find BGP view"); + vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE); + json_object_free(json_no); + } + else + vty_out (vty, "Can't find BGP instance %s%s", argv[5]->arg, VTY_NEWLINE); + return CMD_WARNING; + } + } + else + bgp = NULL; + + argv_find (argv, argc, "neighbors", &idx); + peer = peer_lookup_in_view (vty, bgp, argv[idx+1]->arg, uj); if (! peer) return CMD_WARNING; - if (strncmp (argv[idx_safi]->arg, "m", 1) == 0) - return bgp_peer_counts (vty, peer, AFI_IP, SAFI_MULTICAST, uj); - return bgp_peer_counts (vty, peer, AFI_IP, SAFI_UNICAST, uj); } @@ -9218,20 +9112,17 @@ peer_adj_routes (struct vty *vty, struct peer *peer, afi_t afi, safi_t safi, DEFUN (show_ip_bgp_instance_neighbor_advertised_route, show_ip_bgp_instance_neighbor_advertised_route_cmd, - "show [ip] bgp [ WORD] []|ipv6 []|encap [unicast]|vpnv4 [unicast]>] neighbors [ [route-map WORD]] [json]", + "show [ip] bgp [ WORD] [ []] " + "neighbors [ [route-map WORD]] [json]", SHOW_STR IP_STR BGP_STR BGP_INSTANCE_HELP_STR "Address Family\n" - "Address Family modifier\n" - "Address Family modifier\n" "Address Family\n" "Address Family modifier\n" "Address Family modifier\n" - "Address Family\n" "Address Family modifier\n" - "Address Family\n" "Address Family modifier\n" "Detailed information on TCP and BGP neighbor connections\n" "Neighbor to display information about\n" @@ -9243,51 +9134,54 @@ DEFUN (show_ip_bgp_instance_neighbor_advertised_route, "Name of the route map\n" JSON_STR) { + vrf_id_t vrf = VRF_DEFAULT; afi_t afi = AFI_IP6; safi_t safi = SAFI_UNICAST; - char *vrf = NULL; char *rmap_name = NULL; char *peerstr = NULL; int rcvd = 0; - + struct bgp *bgp = NULL; struct peer *peer; int idx = 0; - /* show [ip] bgp */ - if (argv_find (argv, argc, "ip", &idx)) - afi = AFI_IP; - /* [ WORD] */ - if (argv_find (argv, argc, "view", &idx) || argv_find (argv, argc, "vrf", &idx)) - vrf = argv[++idx]->arg; - /* []|ipv6 []|encap [unicast]|vpnv4 [unicast]>] */ - if (argv_find (argv, argc, "ipv4", &idx) || argv_find (argv, argc, "ipv6", &idx)) - { - afi = strmatch(argv[idx]->text, "ipv6") ? AFI_IP6 : AFI_IP; - if (argv_find (argv, argc, "unicast", &idx) || argv_find (argv, argc, "multicast", &idx)) - safi = strmatch (argv[idx]->text, "unicast") ? SAFI_UNICAST : SAFI_MULTICAST; - } - else if (argv_find (argv, argc, "encap", &idx) || argv_find (argv, argc, "vpnv4", &idx)) - { - afi = AFI_IP; - safi = strmatch (argv[idx]->text, "encap") ? SAFI_ENCAP : SAFI_MPLS_VPN; - // advance idx if necessary - argv_find (argv, argc, "unicast", &idx); - } + idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + if (!idx) + { + vty_out (vty, "View/Vrf Specified: %s is unknown", argv[5]->arg); + return CMD_WARNING; + } + int uj = use_json (argc, argv); + if (uj) argc--; + + if (vrf != VRF_ALL) + { + bgp = bgp_lookup_by_vrf_id (vrf); + if (bgp == NULL) + { + if (uj) + { + json_object *json_no = NULL; + json_no = json_object_new_object(); + json_object_string_add(json_no, "warning", "Can't find BGP view"); + vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE); + json_object_free(json_no); + } + else + vty_out (vty, "Can't find BGP instance %s%s", argv[5]->arg, VTY_NEWLINE); + return CMD_WARNING; + } + } + else + bgp = NULL; /* neighbors */ argv_find (argv, argc, "neighbors", &idx); peerstr = argv[++idx]->arg; - u_char uj = use_json(argc, argv); - - peer = peer_lookup_in_view (vty, vrf, peerstr, uj); - + peer = peer_lookup_in_view (vty, bgp, peerstr, uj); if (! peer) - { - vty_out (vty, "No such neighbor%s", VTY_NEWLINE); - return CMD_WARNING; - } + return CMD_WARNING; if (argv_find (argv, argc, "received-routes", &idx)) rcvd = 1; @@ -9410,20 +9304,17 @@ bgp_show_neighbor_route (struct vty *vty, struct peer *peer, afi_t afi, DEFUN (show_ip_bgp_neighbor_routes, show_ip_bgp_neighbor_routes_cmd, - "show [ip] bgp [ WORD] []|ipv6 []|encap [unicast]|vpnv4 [unicast]>] neighbors [json]", + "show [ip] bgp [ WORD] [ []] " + "neighbors [json]", SHOW_STR IP_STR BGP_STR BGP_INSTANCE_HELP_STR "Address Family\n" - "Address Family modifier\n" - "Address Family modifier\n" "Address Family\n" "Address Family modifier\n" "Address Family modifier\n" - "Address Family\n" "Address Family modifier\n" - "Address Family\n" "Address Family modifier\n" "Detailed information on TCP and BGP neighbor connections\n" "Neighbor to display information about\n" @@ -9434,9 +9325,9 @@ DEFUN (show_ip_bgp_neighbor_routes, "Display routes learned from neighbor\n" JSON_STR) { - char *vrf = NULL; + vrf_id_t vrf = VRF_DEFAULT; char *peerstr = NULL; - + struct bgp *bgp = NULL; afi_t afi = AFI_IP6; safi_t safi = SAFI_UNICAST; struct peer *peer; @@ -9444,33 +9335,41 @@ DEFUN (show_ip_bgp_neighbor_routes, int idx = 0; - /* show [ip] bgp */ - if (argv_find (argv, argc, "ip", &idx)) - afi = AFI_IP; - /* [ WORD] */ - if (argv_find (argv, argc, "view", &idx) || argv_find (argv, argc, "vrf", &idx)) - vrf = argv[++idx]->arg; - /* []|ipv6 []|encap [unicast]|vpnv4 [unicast]>] */ - if (argv_find (argv, argc, "ipv4", &idx) || argv_find (argv, argc, "ipv6", &idx)) - { - afi = strmatch(argv[idx]->text, "ipv6") ? AFI_IP6 : AFI_IP; - if (argv_find (argv, argc, "unicast", &idx) || argv_find (argv, argc, "multicast", &idx)) - safi = strmatch (argv[idx]->text, "unicast") ? SAFI_UNICAST : SAFI_MULTICAST; - } - else if (argv_find (argv, argc, "encap", &idx) || argv_find (argv, argc, "vpnv4", &idx)) - { - afi = AFI_IP; - safi = strmatch (argv[idx]->text, "encap") ? SAFI_ENCAP : SAFI_MPLS_VPN; - // advance idx if necessary - argv_find (argv, argc, "unicast", &idx); - } + idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + if (!idx) + { + vty_out (vty, "View/Vrf Specified: %s is unknown", argv[5]->arg); + return CMD_WARNING; + } + int uj = use_json (argc, argv); + if (uj) argc--; + + if (vrf != VRF_ALL) + { + bgp = bgp_lookup_by_vrf_id (vrf); + if (bgp == NULL) + { + if (uj) + { + json_object *json_no = NULL; + json_no = json_object_new_object(); + json_object_string_add(json_no, "warning", "Can't find BGP view"); + vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE); + json_object_free(json_no); + } + else + vty_out (vty, "Can't find BGP instance %s%s", argv[5]->arg, VTY_NEWLINE); + return CMD_WARNING; + } + } + else + bgp = NULL; + /* neighbors */ argv_find (argv, argc, "neighbors", &idx); peerstr = argv[++idx]->arg; - u_char uj = use_json(argc, argv); - - peer = peer_lookup_in_view (vty, vrf, peerstr, uj); + peer = peer_lookup_in_view (vty, bgp, peerstr, uj); if (! peer) { vty_out (vty, "No such neighbor%s", VTY_NEWLINE); @@ -10321,14 +10220,10 @@ bgp_route_init (void) install_element (ENABLE_NODE, &clear_ip_bgp_dampening_address_mask_cmd); /* prefix count */ - install_element (ENABLE_NODE, &show_ip_bgp_neighbor_prefix_counts_cmd); install_element (ENABLE_NODE, &show_ip_bgp_instance_neighbor_prefix_counts_cmd); - install_element (ENABLE_NODE, &show_ip_bgp_ipv4_neighbor_prefix_counts_cmd); #ifdef KEEP_OLD_VPN_COMMANDS install_element (ENABLE_NODE, &show_ip_bgp_vpn_neighbor_prefix_counts_cmd); #endif /* KEEP_OLD_VPN_COMMANDS */ - install_element (ENABLE_NODE, &show_bgp_ipv6_neighbor_prefix_counts_cmd); - install_element (ENABLE_NODE, &show_bgp_instance_ipv6_neighbor_prefix_counts_cmd); /* New config IPv6 BGP commands. */ install_element (BGP_IPV6_NODE, &bgp_table_map_cmd); From 08435c2b24298d1c716f58fd4fc4d1bb3ea6f03b Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Tue, 24 Jan 2017 09:47:59 -0500 Subject: [PATCH 11/17] bgpd: Fix crash with parsing the community Signed-off-by: Donald Sharp --- bgpd/bgp_route.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index c5b7c4e776..92a2843ba9 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -7924,12 +7924,11 @@ DEFUN (show_ip_bgp, if (argv_find(argv, argc, "community", &idx)) { /* show a specific community */ - if (argv[idx + 1]->type == VARIABLE_TKN || - strmatch(argv[idx + 1]->text, "local-AS") || - strmatch(argv[idx + 1]->text, "no-advertise") || - strmatch(argv[idx + 1]->text, "no-export")) + if (argv_find (argv, argc, "local-AS", &idx) || + argv_find (argv, argc, "no-advertise", &idx) || + argv_find (argv, argc, "no-export", &idx)) { - if (strmatch(argv[idx + 2]->text, "exact_match")) + if (argv_find (argv, argc, "exact_match", &idx)) exact_match = 1; return bgp_show_community (vty, bgp, argc, argv, exact_match, afi, safi); } From 7eeee51e02e815ae79d6ed78f490fb45ea2ebae7 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Wed, 25 Jan 2017 09:37:13 -0500 Subject: [PATCH 12/17] bgpd: Add some documentation for bgp_vty_find_and_parse_afi_safi_vrf The use of this function was causing some confusion. As such let's add some documentation. Signed-off-by: Donald Sharp --- bgpd/bgp_vty.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c index fa19308cb1..e98c231064 100644 --- a/bgpd/bgp_vty.c +++ b/bgpd/bgp_vty.c @@ -197,6 +197,36 @@ argv_find_and_parse_safi (struct cmd_token **argv, int argc, int *index, safi_t return ret; } +/* + * bgp_vty_find_and_parse_afi_safi_vrf + * + * For a given 'show ...' command, correctly parse the afi/safi/vrf out from it + * This function *assumes* that the calling function pre-sets the afi/safi/vrf + * to appropriate values for the calling function. This is to allow the + * calling function to make decisions appropriate for the show command + * that is being parsed. + * + * The show commands are generally of the form: + * "show [ip] bgp [ WORD] [ []] ..." + * + * Since we use argv_find if the show command in particular doesn't have: + * [ip] + * [ WORD] + * [ []] + * The command parsing should still be ok. + * + * vty -> The vty for the command so we can output some useful data in + * the event of a parse error in the vrf. + * argv -> The command tokens + * argc -> How many command tokens we have + * idx -> The current place in the command, generally should be 0 for this function + * afi -> The parsed afi if it was included in the show command, returned here + * safi -> The parsed safi if it was included in the show command, returned here + * vrf -> The parsed vrf id if it was included in the show command, returned here + * + * The function returns the correct location in the parse tree for the + * last token found. + */ int bgp_vty_find_and_parse_afi_safi_vrf (struct vty *vty, struct cmd_token **argv, int argc, int idx, afi_t *afi, safi_t *safi, vrf_id_t *vrf) From c493f2d83a990b5fec0ea927915c46c47d7ed636 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Wed, 25 Jan 2017 09:43:11 -0500 Subject: [PATCH 13/17] bgpd: Remove redundant warning message When bgp_vty_find_and_parse_afi_safi_vrf detects a invalidly named vrf, it warns the user. There is no need for the calling function to warn again. Signed-off-by: Donald Sharp --- bgpd/bgp_route.c | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index 92a2843ba9..02ba731f04 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -7877,10 +7877,8 @@ DEFUN (show_ip_bgp, idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); if (!idx) - { - vty_out (vty, "View/Vrf Specified: %s is unknown", argv[5]->arg); - return CMD_WARNING; - } + return CMD_WARNING; + int uj = use_json (argc, argv); if (uj) argc--; @@ -7986,10 +7984,7 @@ DEFUN (show_ip_bgp_route, idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); if (!idx) - { - vty_out (vty, "View/Vrf Specified: %s is unknown", argv[5]->arg); - return CMD_WARNING; - } + return CMD_WARNING; if (vrf != VRF_ALL) { @@ -8056,10 +8051,7 @@ DEFUN (show_ip_bgp_regexp, int idx = 0; idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); if (!idx) - { - vty_out (vty, "View/Vrf Specified: %s is unknown", argv[5]->arg); - return CMD_WARNING; - } + return CMD_WARNING; // get index of regex argv_find (argv, argc, "regexp", &idx); @@ -8093,10 +8085,8 @@ DEFUN (show_ip_bgp_instance_all, int idx = 0; idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); if (!idx) - { - vty_out (vty, "View/Vrf Specified: %s is unknown", argv[5]->arg); - return CMD_WARNING; - } + return CMD_WARNING; + int uj = use_json (argc, argv); if (uj) argc--; @@ -8786,10 +8776,8 @@ DEFUN (show_ip_bgp_instance_neighbor_prefix_counts, idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); if (!idx) - { - vty_out (vty, "View/Vrf Specified: %s is unknown", argv[5]->arg); - return CMD_WARNING; - } + return CMD_WARNING; + int uj = use_json (argc, argv); if (uj) argc--; @@ -9146,10 +9134,8 @@ DEFUN (show_ip_bgp_instance_neighbor_advertised_route, idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); if (!idx) - { - vty_out (vty, "View/Vrf Specified: %s is unknown", argv[5]->arg); - return CMD_WARNING; - } + return CMD_WARNING; + int uj = use_json (argc, argv); if (uj) argc--; @@ -9334,12 +9320,10 @@ DEFUN (show_ip_bgp_neighbor_routes, int idx = 0; - idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); if (!idx) - { - vty_out (vty, "View/Vrf Specified: %s is unknown", argv[5]->arg); - return CMD_WARNING; - } + return CMD_WARNING; + int uj = use_json (argc, argv); if (uj) argc--; From 0e37c258a3f238ae532ee4e0b4cd308556da8bae Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Fri, 27 Jan 2017 04:57:44 -0500 Subject: [PATCH 14/17] bgpd: Fix possible crash and some minor CR issues This fix addresses these things: 1) Clean up documentation as requested 2) Fix a wrong search for "exact-match" 3) Fix possible crash. Signed-off-by: Donald Sharp --- bgpd/bgp_route.c | 15 ++++++++++++--- bgpd/bgp_vty.c | 3 +++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index 657cd076e7..573fc76655 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -8120,7 +8120,7 @@ DEFUN (show_ip_bgp, if (argv_find(argv, argc, "community-list", &idx)) { const char *clist_number_or_name = argv[++idx]->arg; - if (++idx < argc && strmatch (argv[idx]->arg, "exact-match")) + if (++idx < argc && strmatch (argv[idx]->text, "exact-match")) exact_match = 1; return bgp_show_community_list (vty, bgp, clist_number_or_name, exact_match, afi, safi); } @@ -8179,7 +8179,10 @@ DEFUN (show_ip_bgp_route, } } else - bgp = NULL; + { + vty_out (vty, "Specified 'all' vrf's but this command currently only works per view/vrf%s", VTY_NEWLINE); + return CMD_WARNING; + } /* */ if (argv_find (argv, argc, "A.B.C.D", &idx) || argv_find (argv, argc, "X:X::X:X", &idx)) @@ -9034,9 +9037,15 @@ DEFUN (show_ip_bgp_vpn_all_route_prefix, { int idx = 0; char *network = NULL; + struct bgp *bgp = bgp_get_default(); + if (!bgp) + { + vty_out (vty, "Can't find default instance%s", VTY_NEWLINE); + return CMD_WARNING; + } network = argv_find (argv, argc, "A.B.C.D", &idx) ? argv[idx]->arg : NULL; network = argv_find (argv, argc, "A.B.C.D/M", &idx) ? argv[idx]->arg : NULL; - return bgp_show_route (vty, NULL, network, AFI_IP, SAFI_MPLS_VPN, NULL, 0, BGP_PATH_ALL, use_json(argc, argv)); + return bgp_show_route (vty, bgp, network, AFI_IP, SAFI_MPLS_VPN, NULL, 0, BGP_PATH_ALL, use_json(argc, argv)); } #endif /* KEEP_OLD_VPN_COMMANDS */ diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c index 59af567800..208dd54460 100644 --- a/bgpd/bgp_vty.c +++ b/bgpd/bgp_vty.c @@ -227,6 +227,9 @@ argv_find_and_parse_safi (struct cmd_token **argv, int argc, int *index, safi_t * * The function returns the correct location in the parse tree for the * last token found. + * + * Returns 0 for failure to parse correctly, else the idx position of where + * it found the last token. */ int bgp_vty_find_and_parse_afi_safi_vrf (struct vty *vty, struct cmd_token **argv, int argc, int idx, From 9317e17dc86bdaaa345bfedc14d453a2af3ab60f Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Fri, 27 Jan 2017 08:12:53 -0500 Subject: [PATCH 15/17] bgpd: Make usage of idx consistent Convert the idx to &idx to make our api more consistent Signed-off-by: Donald Sharp --- bgpd/bgp_route.c | 15 ++++++++------- bgpd/bgp_vty.c | 19 +++++++++++-------- bgpd/bgp_vty.h | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index 573fc76655..0225a0bbef 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -8058,7 +8058,7 @@ DEFUN (show_ip_bgp, struct bgp *bgp = NULL; int idx = 0; - idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, &idx, &afi, &safi, &vrf); if (!idx) return CMD_WARNING; @@ -8067,6 +8067,7 @@ DEFUN (show_ip_bgp, if (vrf != VRF_ALL) { + vty_out(vty, "VRF-id: %d", vrf); bgp = bgp_lookup_by_vrf_id (vrf); if (bgp == NULL) { @@ -8165,7 +8166,7 @@ DEFUN (show_ip_bgp_route, int idx = 0; - idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, &idx, &afi, &safi, &vrf); if (!idx) return CMD_WARNING; @@ -8235,7 +8236,7 @@ DEFUN (show_ip_bgp_regexp, safi_t safi = SAFI_UNICAST; int idx = 0; - idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, &idx, &afi, &safi, &vrf); if (!idx) return CMD_WARNING; @@ -8269,7 +8270,7 @@ DEFUN (show_ip_bgp_instance_all, safi_t safi = SAFI_UNICAST; int idx = 0; - idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, &idx, &afi, &safi, &vrf); if (!idx) return CMD_WARNING; @@ -8960,7 +8961,7 @@ DEFUN (show_ip_bgp_instance_neighbor_prefix_counts, int idx = 0; struct bgp *bgp = NULL; - idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, &idx, &afi, &safi, &vrf); if (!idx) return CMD_WARNING; @@ -9324,7 +9325,7 @@ DEFUN (show_ip_bgp_instance_neighbor_advertised_route, int idx = 0; - idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, &idx, &afi, &safi, &vrf); if (!idx) return CMD_WARNING; @@ -9512,7 +9513,7 @@ DEFUN (show_ip_bgp_neighbor_routes, int idx = 0; - idx = bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, idx, &afi, &safi, &vrf); + bgp_vty_find_and_parse_afi_safi_vrf (vty, argv, argc, &idx, &afi, &safi, &vrf); if (!idx) return CMD_WARNING; diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c index 208dd54460..7462f29e31 100644 --- a/bgpd/bgp_vty.c +++ b/bgpd/bgp_vty.c @@ -232,22 +232,22 @@ argv_find_and_parse_safi (struct cmd_token **argv, int argc, int *index, safi_t * it found the last token. */ int -bgp_vty_find_and_parse_afi_safi_vrf (struct vty *vty, struct cmd_token **argv, int argc, int idx, +bgp_vty_find_and_parse_afi_safi_vrf (struct vty *vty, struct cmd_token **argv, int argc, int *idx, afi_t *afi, safi_t *safi, vrf_id_t *vrf) { char *vrf_name = NULL; - if (argv_find (argv, argc, "ip", &idx)) + if (argv_find (argv, argc, "ip", idx)) *afi = AFI_IP; - if (argv_find (argv, argc, "view", &idx) || argv_find (argv, argc, "vrf", &idx)) + if (argv_find (argv, argc, "view", idx) || argv_find (argv, argc, "vrf", idx)) { - vrf_name = argv[idx + 1]->arg; - idx += 2; + vrf_name = argv[*idx + 1]->arg; + *idx += 2; } - if (argv_find_and_parse_afi (argv, argc, &idx, afi)) - argv_find_and_parse_safi (argv, argc, &idx, safi); + if (argv_find_and_parse_afi (argv, argc, idx, afi)) + argv_find_and_parse_safi (argv, argc, idx, safi); if (vrf_name) { @@ -260,9 +260,12 @@ bgp_vty_find_and_parse_afi_safi_vrf (struct vty *vty, struct cmd_token **argv, i if (*vrf == VRF_UNKNOWN) { vty_out (vty, "View/Vrf specified is unknown: %s", vrf_name); + *idx = 0; return 0; } - return idx + 1; + + *idx += 1; + return *idx; } static int diff --git a/bgpd/bgp_vty.h b/bgpd/bgp_vty.h index cfc4f6cdfb..13e67d112e 100644 --- a/bgpd/bgp_vty.h +++ b/bgpd/bgp_vty.h @@ -64,6 +64,6 @@ extern int argv_find_and_parse_safi(struct cmd_token **argv, int argc, int *index, safi_t *safi); extern int -bgp_vty_find_and_parse_afi_safi_vrf (struct vty *vty, struct cmd_token **argv, int argc, int idx, +bgp_vty_find_and_parse_afi_safi_vrf (struct vty *vty, struct cmd_token **argv, int argc, int *idx, afi_t *afi, safi_t *safi, vrf_id_t *vrf); #endif /* _QUAGGA_BGP_VTY_H */ From b4898a387bc919e80de1a98aba2215a46c7cc070 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Fri, 27 Jan 2017 09:07:44 -0500 Subject: [PATCH 16/17] bgp: Add Asserts and remove dead code Add asserts to how I expect bgp_vty_find_and_parse_afi_safi to be used. Signed-off-by: Donald Sharp --- bgpd/bgp_route.c | 43 ++++++++++++++++--------------------------- bgpd/bgp_vty.c | 4 ++++ 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index 0225a0bbef..ca96bae020 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -8065,18 +8065,12 @@ DEFUN (show_ip_bgp, int uj = use_json (argc, argv); if (uj) argc--; - if (vrf != VRF_ALL) + bgp = bgp_lookup_by_vrf_id (vrf); + if (bgp == NULL) { - vty_out(vty, "VRF-id: %d", vrf); - bgp = bgp_lookup_by_vrf_id (vrf); - if (bgp == NULL) - { - vty_out (vty, "Can't find BGP instance %s%s", argv[5]->arg, VTY_NEWLINE); - return CMD_WARNING; - } + vty_out (vty, "Can't find BGP instance %s%s", argv[5]->arg, VTY_NEWLINE); + return CMD_WARNING; } - else - bgp = NULL; if (argv_find(argv, argc, "cidr-only", &idx)) return bgp_show (vty, bgp, afi, safi, bgp_show_type_cidr_only, NULL, uj); @@ -9332,26 +9326,21 @@ DEFUN (show_ip_bgp_instance_neighbor_advertised_route, int uj = use_json (argc, argv); if (uj) argc--; - if (vrf != VRF_ALL) + bgp = bgp_lookup_by_vrf_id (vrf); + if (bgp == NULL) { - bgp = bgp_lookup_by_vrf_id (vrf); - if (bgp == NULL) - { - if (uj) - { - json_object *json_no = NULL; - json_no = json_object_new_object(); - json_object_string_add(json_no, "warning", "Can't find BGP view"); - vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE); - json_object_free(json_no); + if (uj) + { + json_object *json_no = NULL; + json_no = json_object_new_object(); + json_object_string_add(json_no, "warning", "Can't find BGP view"); + vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE); + json_object_free(json_no); } - else - vty_out (vty, "Can't find BGP instance %s%s", argv[5]->arg, VTY_NEWLINE); - return CMD_WARNING; - } + else + vty_out (vty, "Can't find BGP instance %s%s", argv[5]->arg, VTY_NEWLINE); + return CMD_WARNING; } - else - bgp = NULL; /* neighbors */ argv_find (argv, argc, "neighbors", &idx); diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c index 7462f29e31..52ba24d36b 100644 --- a/bgpd/bgp_vty.c +++ b/bgpd/bgp_vty.c @@ -237,6 +237,10 @@ bgp_vty_find_and_parse_afi_safi_vrf (struct vty *vty, struct cmd_token **argv, i { char *vrf_name = NULL; + assert (afi); + assert (safi); + assert (*vrf == VRF_UNKNOWN); + if (argv_find (argv, argc, "ip", idx)) *afi = AFI_IP; From 69d143a005c19770f727cc247739ded471e8a32a Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Fri, 27 Jan 2017 09:49:30 -0500 Subject: [PATCH 17/17] bgpd: Fix assert condition error. Signed-off-by: Donald Sharp --- bgpd/bgp_vty.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c index 52ba24d36b..d9043e520d 100644 --- a/bgpd/bgp_vty.c +++ b/bgpd/bgp_vty.c @@ -239,7 +239,7 @@ bgp_vty_find_and_parse_afi_safi_vrf (struct vty *vty, struct cmd_token **argv, i assert (afi); assert (safi); - assert (*vrf == VRF_UNKNOWN); + assert (vrf && *vrf != VRF_UNKNOWN); if (argv_find (argv, argc, "ip", idx)) *afi = AFI_IP;