diff options
| author | Piotr Suchy <piotrsuchy6@tlen.pl> | 2024-04-15 21:33:40 +0200 | 
|---|---|---|
| committer | Piotr Suchy <piotrsuchy@proton.me> | 2024-04-21 23:40:06 +0200 | 
| commit | 0a17289a3b1dc2d92126dd38d40772d5e399845c (patch) | |
| tree | 5bdbc63431576daed6fdb832a40f035cbfed833f /vtysh | |
| parent | 99a723e5a8b3f0b874e55156b21eaebaa9bb0c82 (diff) | |
vtysh, lib: fix 'show ip[v6] prefix-list ... json' formatting by moving it to vtysh
Json output is not valid for 'show ip[v6] prefix-list ... json' commands,
as it goes through all the running daemons and for each one it calls
'vty_show_prefix_list' creating a new json object. To aggreagate the output
and create a valid json that can be parsed, the commands were moved to vtysh.
Before:
{
  "ZEBRA":{
    "DEFAULT":{
      "addressFamily":"IPv4",
      "entries":[
        {
          "sequenceNumber":10,
          "type":"permit",
          "prefix":"0.0.0.0/0"
        }
      ]
    }
  }
}
{
  "OSPF":{
    "DEFAULT":{
      "addressFamily":"IPv4",
      "entries":[
        {
          "sequenceNumber":10,
          "type":"permit",
          "prefix":"0.0.0.0/0"
        }
      ]
    }
  }
}
{
  "BGP":{
    "DEFAULT":{
      "addressFamily":"IPv4",
      "entries":[
        {
          "sequenceNumber":10,
          "type":"permit",
          "prefix":"0.0.0.0/0"
        }
      ]
    }
  }
}
After:
{"zebra":{
  "DEFAULT":{
    "addressFamily":"IPv4",
    "entries":[
      {
        "sequenceNumber":10,
        "type":"permit",
        "prefix":"0.0.0.0/0"
      }
    ]
  }
}
,"ospfd":{
  "DEFAULT":{
    "addressFamily":"IPv4",
    "entries":[
      {
        "sequenceNumber":10,
        "type":"permit",
        "prefix":"0.0.0.0/0"
      }
    ]
  }
}
,"bgpd":{
  "DEFAULT":{
    "addressFamily":"IPv4",
    "entries":[
      {
        "sequenceNumber":10,
        "type":"permit",
        "prefix":"0.0.0.0/0"
      }
    ]
  }
}
}
Signed-off-by: Piotr Suchy <piotrsuchy@proton.me>
Diffstat (limited to 'vtysh')
| -rw-r--r-- | vtysh/vtysh.c | 163 | ||||
| -rw-r--r-- | vtysh/vtysh.h | 14 | 
2 files changed, 176 insertions, 1 deletions
diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c index 03e2428b9e..e03d1464d3 100644 --- a/vtysh/vtysh.c +++ b/vtysh/vtysh.c @@ -3474,6 +3474,161 @@ DEFPY (show_route_map,  	return CMD_SUCCESS;  } +static void show_prefix_list_send(afi_t afi, const char *prefix_list, +				  const char *seq, enum display_type dtype, +				  bool json) +{ +	unsigned int i; +	bool first = true; +	char command_line[128]; + +	if (afi == AFI_IP) +		snprintf(command_line, sizeof(command_line), +			 "do show ip prefix-list "); +	else if (afi == AFI_IP6) +		snprintf(command_line, sizeof(command_line), +			 "do show ipv6 prefix-list "); +	if (dtype == detail_display) +		strlcat(command_line, "detail ", sizeof(command_line)); +	else if (dtype == summary_display) +		strlcat(command_line, "summary ", sizeof(command_line)); +	if (prefix_list) +		strlcat(command_line, prefix_list, sizeof(command_line)); +	if (dtype == sequential_display) { +		strlcat(command_line, " seq ", sizeof(command_line)); +		strlcat(command_line, seq, sizeof(command_line)); +	} +	if (json) +		strlcat(command_line, " json", sizeof(command_line)); + +	if (json) +		vty_out(vty, "{"); + +	for (i = 0; i < array_size(vtysh_client); i++) { +		const struct vtysh_client *client = &vtysh_client[i]; +		bool is_connected = true; + +		if (!CHECK_FLAG(client->flag, VTYSH_PREFIX_LIST_SHOW)) +			continue; + +		for (; client; client = client->next) +			if (client->fd < 0) +				is_connected = false; + +		if (!is_connected) +			continue; + +		if (json && !first) +			vty_out(vty, ","); +		else +			first = false; + +		if (json) +			vty_out(vty, "\"%s\":", vtysh_client[i].name); + +		vtysh_client_execute_name(vtysh_client[i].name, command_line); +	} + +	if (json) +		vty_out(vty, "}\n"); +} + +DEFPY (show_ip_prefix_list, +       show_ip_prefix_list_cmd, +       "show ip prefix-list [PREFIXLIST4_NAME$name [seq$dseq (1-4294967295)$arg]] [json$uj]", +       SHOW_STR +       IP_STR +       PREFIX_LIST_STR +       "Name of a prefix list\n" +       "sequence number of an entry\n" +       "Sequence number\n" +       JSON_STR) +{ +	enum display_type dtype = normal_display; + +	if (dseq) +		dtype = sequential_display; + +	show_prefix_list_send(AFI_IP, name, arg_str, dtype, !!uj); +	return CMD_SUCCESS; +} + +DEFPY (show_ip_prefix_list_summary, +       show_ip_prefix_list_summary_cmd, +       "show ip prefix-list summary [PREFIXLIST4_NAME$name] [json$uj]", +       SHOW_STR +       IP_STR +       PREFIX_LIST_STR +       "Summary of prefix lists\n" +       "Name of a prefix list\n" +       JSON_STR) +{ +	show_prefix_list_send(AFI_IP, name, NULL, summary_display, !!uj); +	return CMD_SUCCESS; +} + +DEFPY (show_ip_prefix_list_detail, +       show_ip_prefix_list_detail_cmd, +       "show ip prefix-list detail [PREFIXLIST4_NAME$name] [json$uj]", +       SHOW_STR +       IP_STR +       PREFIX_LIST_STR +       "Detail of prefix lists\n" +       "Name of a prefix list\n" +       JSON_STR) +{ +	show_prefix_list_send(AFI_IP, name, NULL, detail_display, !!uj); +	return CMD_SUCCESS; +} + +DEFPY (show_ipv6_prefix_list, +       show_ipv6_prefix_list_cmd, +       "show ipv6 prefix-list [PREFIXLIST6_NAME$name [seq$dseq (1-4294967295)$arg]] [json$uj]", +       SHOW_STR +       IPV6_STR +       PREFIX_LIST_STR +       "Name of a prefix list\n" +       "sequence number of an entry\n" +       "Sequence number\n" +       JSON_STR) +{ +	enum display_type dtype = normal_display; + +	if (dseq) +		dtype = sequential_display; + +	show_prefix_list_send(AFI_IP6, name, arg_str, dtype, !!uj); +	return CMD_SUCCESS; +} + +DEFPY (show_ipv6_prefix_list_summary, +       show_ipv6_prefix_list_summary_cmd, +       "show ipv6 prefix-list summary [PREFIXLIST6_NAME$name] [json$uj]", +       SHOW_STR +       IPV6_STR +       PREFIX_LIST_STR +       "Summary of prefix lists\n" +       "Name of a prefix list\n" +       JSON_STR) +{ +	show_prefix_list_send(AFI_IP6, name, NULL, summary_display, !!uj); +	return CMD_SUCCESS; +} + +DEFPY (show_ipv6_prefix_list_detail, +       show_ipv6_prefix_list_detail_cmd, +       "show ipv6 prefix-list detail [PREFIXLIST6_NAME$name] [json$uj]", +       SHOW_STR +       IPV6_STR +       PREFIX_LIST_STR +       "Detail of prefix lists\n" +       "Name of a prefix list\n" +       JSON_STR) +{ +	show_prefix_list_send(AFI_IP6, name, NULL, detail_display, !!uj); +	return CMD_SUCCESS; +} +  DEFUN (vtysh_integrated_config,         vtysh_integrated_config_cmd,         "service integrated-vtysh-config", @@ -3498,8 +3653,8 @@ DEFUN (no_vtysh_integrated_config,  static void backup_config_file(const char *fbackup)  {  	char *integrate_sav = NULL; -  	size_t integrate_sav_sz = strlen(fbackup) + strlen(CONF_BACKUP_EXT) + 1; +  	integrate_sav = malloc(integrate_sav_sz);  	strlcpy(integrate_sav, fbackup, integrate_sav_sz);  	strlcat(integrate_sav, CONF_BACKUP_EXT, integrate_sav_sz); @@ -5074,6 +5229,12 @@ void vtysh_init_vty(void)  	install_element(ENABLE_NODE, &vtysh_copy_to_running_cmd);  	install_element(ENABLE_NODE, &show_route_map_cmd); +	install_element(ENABLE_NODE, &show_ip_prefix_list_cmd); +	install_element(ENABLE_NODE, &show_ip_prefix_list_summary_cmd); +	install_element(ENABLE_NODE, &show_ip_prefix_list_detail_cmd); +	install_element(ENABLE_NODE, &show_ipv6_prefix_list_cmd); +	install_element(ENABLE_NODE, &show_ipv6_prefix_list_summary_cmd); +	install_element(ENABLE_NODE, &show_ipv6_prefix_list_detail_cmd);  	/* "write terminal" command. */  	install_element(ENABLE_NODE, &vtysh_write_terminal_cmd); diff --git a/vtysh/vtysh.h b/vtysh/vtysh.h index b35d56672a..131fbef8ba 100644 --- a/vtysh/vtysh.h +++ b/vtysh/vtysh.h @@ -68,6 +68,10 @@ extern struct event_loop *master;  	VTYSH_ZEBRA | VTYSH_RIPD | VTYSH_RIPNGD | VTYSH_OSPFD | VTYSH_OSPF6D | \  		VTYSH_BGPD | VTYSH_ISISD | VTYSH_PIMD | VTYSH_EIGRPD |         \  		VTYSH_FABRICD +#define VTYSH_PREFIX_LIST_SHOW                                                 \ +	VTYSH_ZEBRA | VTYSH_RIPD | VTYSH_RIPNGD | VTYSH_OSPFD | VTYSH_OSPF6D | \ +		VTYSH_BGPD | VTYSH_ISISD | VTYSH_PIMD | VTYSH_EIGRPD |         \ +		VTYSH_FABRICD  #define VTYSH_INTERFACE_SUBSET                                                 \  	VTYSH_OSPFD | VTYSH_OSPF6D | \  		VTYSH_ISISD | VTYSH_PIMD | VTYSH_PIM6D | VTYSH_NHRPD |         \ @@ -90,7 +94,17 @@ enum vtysh_write_integrated {  	WRITE_INTEGRATED_YES  }; +enum display_type { +	normal_display, +	summary_display, +	detail_display, +	sequential_display, +	longer_display, +	first_match_display +}; +  extern enum vtysh_write_integrated vtysh_write_integrated; +extern enum display_type display_type;  extern char frr_config[];  extern char vtydir[];  | 
