Merge pull request #12751 from Pdoijode/pdoijode/ospf-vrf-neighbor-detail-1

ospfd: Added missing fields and option to query specific neighbor in VRF
This commit is contained in:
Donatas Abraitis 2023-02-24 11:56:28 +02:00 committed by GitHub
commit ba995a720b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 252 additions and 175 deletions

View File

@ -794,13 +794,13 @@ Showing Information
.. clicmd:: show ip ospf neighbor [json]
.. clicmd:: show ip ospf neighbor INTERFACE [json]
.. clicmd:: show ip ospf [vrf <NAME|all>] neighbor INTERFACE [json]
.. clicmd:: show ip ospf neighbor detail [json]
.. clicmd:: show ip ospf neighbor A.B.C.D [detail] [json]
.. clicmd:: show ip ospf [vrf <NAME|all>] neighbor A.B.C.D [detail] [json]
.. clicmd:: show ip ospf neighbor INTERFACE detail [json]
.. clicmd:: show ip ospf [vrf <NAME|all>] neighbor INTERFACE detail [json]
Display lsa information of LSDB.
Json o/p of this command covers base route information

View File

@ -297,6 +297,13 @@ int vty_json_no_pretty(struct vty *vty, struct json_object *json)
return vty_json_helper(vty, json, JSON_C_TO_STRING_NOSLASHESCAPE);
}
void vty_json_empty(struct vty *vty)
{
json_object *json = json_object_new_object();
vty_json(vty, json);
}
/* Output current time to the vty. */
void vty_time_print(struct vty *vty, int cr)
{

View File

@ -344,7 +344,7 @@ extern bool vty_set_include(struct vty *vty, const char *regexp);
*/
extern int vty_json(struct vty *vty, struct json_object *json);
extern int vty_json_no_pretty(struct vty *vty, struct json_object *json);
extern void vty_json_empty(struct vty *vty);
/* post fd to be passed to the vtysh client
* fd is owned by the VTY code after this and will be closed when done
*/

View File

@ -4900,9 +4900,8 @@ DEFUN (show_ip_ospf_instance_neighbor_all,
}
static int show_ip_ospf_neighbor_int_common(struct vty *vty, struct ospf *ospf,
int arg_base,
struct cmd_token **argv,
bool use_json, uint8_t use_vrf)
const char *ifname, bool use_json,
uint8_t use_vrf)
{
struct interface *ifp;
struct route_node *rn;
@ -4921,7 +4920,7 @@ static int show_ip_ospf_neighbor_int_common(struct vty *vty, struct ospf *ospf,
ospf_show_vrf_name(ospf, vty, json, use_vrf);
ifp = if_lookup_by_name(argv[arg_base]->arg, ospf->vrf_id);
ifp = if_lookup_by_name(ifname, ospf->vrf_id);
if (!ifp) {
if (use_json)
json_object_boolean_true_add(json, "noSuchIface");
@ -4947,76 +4946,22 @@ static int show_ip_ospf_neighbor_int_common(struct vty *vty, struct ospf *ospf,
return CMD_SUCCESS;
}
DEFUN (show_ip_ospf_neighbor_int,
show_ip_ospf_neighbor_int_cmd,
"show ip ospf [vrf <NAME>] neighbor IFNAME [json]",
SHOW_STR
IP_STR
"OSPF information\n"
VRF_CMD_HELP_STR
"Neighbor list\n"
"Interface name\n"
JSON_STR)
DEFPY(show_ip_ospf_instance_neighbor_int,
show_ip_ospf_instance_neighbor_int_cmd,
"show ip ospf (1-65535)$instance neighbor IFNAME$ifname [json$json]",
SHOW_STR
IP_STR
"OSPF information\n"
"Instance ID\n"
"Neighbor list\n"
"Interface name\n"
JSON_STR)
{
struct ospf *ospf;
int idx_ifname = 0;
int idx_vrf = 0;
bool uj = use_json(argc, argv);
int ret = CMD_SUCCESS;
struct interface *ifp = NULL;
char *vrf_name = NULL;
vrf_id_t vrf_id = VRF_DEFAULT;
struct vrf *vrf = NULL;
if (argv_find(argv, argc, "vrf", &idx_vrf))
vrf_name = argv[idx_vrf + 1]->arg;
if (vrf_name && strmatch(vrf_name, VRF_DEFAULT_NAME))
vrf_name = NULL;
if (vrf_name) {
vrf = vrf_lookup_by_name(vrf_name);
if (vrf)
vrf_id = vrf->vrf_id;
}
ospf = ospf_lookup_by_vrf_id(vrf_id);
if (!ospf || !ospf->oi_running)
return ret;
if (!uj)
if (!json)
show_ip_ospf_neighbour_header(vty);
argv_find(argv, argc, "IFNAME", &idx_ifname);
ifp = if_lookup_by_name(argv[idx_ifname]->arg, vrf_id);
if (!ifp)
return ret;
ret = show_ip_ospf_neighbor_int_common(vty, ospf, idx_ifname,
argv, uj, 0);
return ret;
}
DEFUN (show_ip_ospf_instance_neighbor_int,
show_ip_ospf_instance_neighbor_int_cmd,
"show ip ospf (1-65535) neighbor IFNAME [json]",
SHOW_STR
IP_STR
"OSPF information\n"
"Instance ID\n"
"Neighbor list\n"
"Interface name\n"
JSON_STR)
{
int idx_number = 3;
int idx_ifname = 5;
struct ospf *ospf;
unsigned short instance = 0;
bool uj = use_json(argc, argv);
if (!uj)
show_ip_ospf_neighbour_header(vty);
instance = strtoul(argv[idx_number]->arg, NULL, 10);
if (instance != ospf_instance)
return CMD_NOT_MY_INSTANCE;
@ -5024,11 +4969,10 @@ DEFUN (show_ip_ospf_instance_neighbor_int,
if (!ospf || !ospf->oi_running)
return CMD_SUCCESS;
if (!uj)
if (!json)
show_ip_ospf_neighbour_header(vty);
return show_ip_ospf_neighbor_int_common(vty, ospf, idx_ifname, argv, uj,
0);
return show_ip_ospf_neighbor_int_common(vty, ospf, ifname, !!json, 0);
}
static void show_ip_ospf_nbr_nbma_detail_sub(struct vty *vty,
@ -5168,19 +5112,36 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
json_object_string_add(json_neigh, "areaId",
ospf_area_desc_string(oi->area));
json_object_string_add(json_neigh, "ifaceName", oi->ifp->name);
} else
vty_out(vty, " In the area %s via interface %s\n",
if (oi->address)
json_object_string_addf(json_neigh, "localIfaceAddress",
"%pI4",
&oi->address->u.prefix4);
} else {
vty_out(vty, " In the area %s via interface %s",
ospf_area_desc_string(oi->area), oi->ifp->name);
if (oi->address)
vty_out(vty, " local interface IP %pI4\n",
&oi->address->u.prefix4);
else
vty_out(vty, "\n");
}
/* Show neighbor priority and state. */
ospf_nbr_ism_state_message(nbr, neigh_state, sizeof(neigh_state));
if (use_json) {
json_object_int_add(json_neigh, "nbrPriority", nbr->priority);
json_object_string_add(json_neigh, "nbrState", neigh_state);
} else
vty_out(vty, " Neighbor priority is %d, State is %s,",
nbr->priority, neigh_state);
json_object_string_add(json_neigh, "role",
lookup_msg(ospf_ism_state_msg,
ospf_nbr_ism_state(nbr),
NULL));
} else {
vty_out(vty,
" Neighbor priority is %d, State is %s, Role is %s,",
nbr->priority, neigh_state,
lookup_msg(ospf_ism_state_msg, ospf_nbr_ism_state(nbr),
NULL));
}
/* Show state changes. */
if (use_json)
json_object_int_add(json_neigh, "stateChangeCounter",
@ -5414,7 +5375,8 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
static int show_ip_ospf_neighbor_id_common(struct vty *vty, struct ospf *ospf,
struct in_addr *router_id,
bool use_json, uint8_t use_vrf,
bool is_detail)
bool is_detail,
json_object *json_vrf)
{
struct listnode *node;
struct ospf_neighbor *nbr;
@ -5448,6 +5410,14 @@ static int show_ip_ospf_neighbor_id_common(struct vty *vty, struct ospf *ospf,
use_json);
}
if (json_vrf && use_json) {
json_object_object_add(
json_vrf,
(ospf->vrf_id == VRF_DEFAULT) ? "default" : ospf->name,
json);
return CMD_SUCCESS;
}
if (use_json)
vty_json(vty, json);
else
@ -5456,23 +5426,50 @@ static int show_ip_ospf_neighbor_id_common(struct vty *vty, struct ospf *ospf,
return CMD_SUCCESS;
}
DEFPY(show_ip_ospf_neighbor_id, show_ip_ospf_neighbor_id_cmd,
"show ip ospf neighbor A.B.C.D$router_id [detail$detail] [json$json]",
SHOW_STR IP_STR
DEFPY(show_ip_ospf_neighbor_id,
show_ip_ospf_neighbor_id_cmd,
"show ip ospf [vrf NAME$vrf_name] neighbor A.B.C.D$router_id [detail$detail] [json$json]",
SHOW_STR
IP_STR
"OSPF information\n"
VRF_CMD_HELP_STR
"Neighbor list\n"
"Neighbor ID\n"
"Detailed output\n" JSON_STR)
"Detailed output\n"
JSON_STR)
{
struct ospf *ospf;
struct listnode *node;
int ret = CMD_SUCCESS;
int inst = 0;
for (ALL_LIST_ELEMENTS_RO(om->ospf, node, ospf)) {
if (!ospf->oi_running)
continue;
ret = show_ip_ospf_neighbor_id_common(vty, ospf, &router_id,
!!json, 0, !!detail);
if (vrf_name && !strmatch(vrf_name, "all")) {
ospf = ospf_lookup_by_inst_name(inst, vrf_name);
if (ospf == NULL || !ospf->oi_running) {
if (!json)
vty_out(vty,
"%% OSPF is not enabled in vrf %s\n",
vrf_name);
else
vty_json_empty(vty);
return CMD_SUCCESS;
}
ret = show_ip_ospf_neighbor_id_common(
vty, ospf, &router_id, !!json, 0, !!detail, NULL);
} else {
json_object *json_vrf = NULL;
if (json)
json_vrf = json_object_new_object();
for (ALL_LIST_ELEMENTS_RO(om->ospf, node, ospf)) {
if (!ospf->oi_running)
continue;
ret = show_ip_ospf_neighbor_id_common(
vty, ospf, &router_id, !!json, 0, !!detail,
json_vrf);
}
if (json)
vty_json(vty, json_vrf);
}
return ret;
@ -5497,7 +5494,7 @@ DEFPY(show_ip_ospf_instance_neighbor_id, show_ip_ospf_instance_neighbor_id_cmd,
return CMD_SUCCESS;
return show_ip_ospf_neighbor_id_common(vty, ospf, &router_id, !!json, 0,
!!detail);
!!detail, NULL);
}
static int show_ip_ospf_neighbor_detail_common(struct vty *vty,
@ -5562,77 +5559,71 @@ static int show_ip_ospf_neighbor_detail_common(struct vty *vty,
return CMD_SUCCESS;
}
DEFUN (show_ip_ospf_neighbor_detail,
show_ip_ospf_neighbor_detail_cmd,
"show ip ospf [vrf <NAME|all>] neighbor detail [json]",
SHOW_STR
IP_STR
"OSPF information\n"
VRF_CMD_HELP_STR
"All VRFs\n"
"Neighbor list\n"
"detail of all neighbors\n"
JSON_STR)
DEFPY(show_ip_ospf_neighbor_detail,
show_ip_ospf_neighbor_detail_cmd,
"show ip ospf [vrf <NAME|all>$vrf_name] neighbor detail [json$json]",
SHOW_STR
IP_STR
"OSPF information\n"
VRF_CMD_HELP_STR
"All VRFs\n"
"Neighbor list\n"
"detail of all neighbors\n"
JSON_STR)
{
struct ospf *ospf;
bool uj = use_json(argc, argv);
struct listnode *node = NULL;
char *vrf_name = NULL;
bool all_vrf = false;
int ret = CMD_SUCCESS;
int inst = 0;
int idx_vrf = 0;
uint8_t use_vrf = 0;
json_object *json = NULL;
json_object *json_vrf = NULL;
OSPF_FIND_VRF_ARGS(argv, argc, idx_vrf, vrf_name, all_vrf);
if (uj)
json = json_object_new_object();
if (json)
json_vrf = json_object_new_object();
/* vrf input is provided could be all or specific vrf*/
if (vrf_name) {
use_vrf = 1;
if (all_vrf) {
if (strmatch(vrf_name, "all")) {
for (ALL_LIST_ELEMENTS_RO(om->ospf, node, ospf)) {
if (!ospf->oi_running)
continue;
ret = show_ip_ospf_neighbor_detail_common(
vty, ospf, json, uj, use_vrf);
vty, ospf, json_vrf, !!json, use_vrf);
}
if (uj)
vty_json(vty, json);
if (json)
vty_json(vty, json_vrf);
return ret;
}
ospf = ospf_lookup_by_inst_name(inst, vrf_name);
if (ospf == NULL || !ospf->oi_running) {
if (uj)
json_object_free(json);
if (json)
vty_json(vty, json_vrf);
else
vty_out(vty,
"%% OSPF is not enabled in vrf %s\n",
vrf_name);
return CMD_SUCCESS;
}
} else {
/* Display default ospf (instance 0) info */
ospf = ospf_lookup_by_vrf_id(VRF_DEFAULT);
if (ospf == NULL || !ospf->oi_running) {
if (uj)
json_object_free(json);
if (json)
vty_json(vty, json_vrf);
else
vty_out(vty, "%% OSPF is not enabled\n");
return CMD_SUCCESS;
}
}
if (ospf) {
ret = show_ip_ospf_neighbor_detail_common(vty, ospf, json, uj,
use_vrf);
if (uj) {
vty_out(vty, "%s\n",
json_object_to_json_string_ext(
json, JSON_C_TO_STRING_PRETTY));
}
}
if (ospf)
ret = show_ip_ospf_neighbor_detail_common(vty, ospf, json_vrf,
!!json, use_vrf);
if (uj)
json_object_free(json);
if (json)
vty_json(vty, json_vrf);
return ret;
}
@ -5861,9 +5852,9 @@ DEFUN (show_ip_ospf_instance_neighbor_detail_all,
static int show_ip_ospf_neighbor_int_detail_common(struct vty *vty,
struct ospf *ospf,
int arg_base,
struct cmd_token **argv,
bool use_json)
const char *ifname,
bool use_json,
json_object *json_vrf)
{
struct ospf_interface *oi;
struct interface *ifp;
@ -5871,8 +5862,15 @@ static int show_ip_ospf_neighbor_int_detail_common(struct vty *vty,
struct ospf_neighbor *nbr;
json_object *json = NULL;
if (use_json)
if (use_json) {
json = json_object_new_object();
if (json_vrf)
json_object_object_add(json_vrf,
(ospf->vrf_id == VRF_DEFAULT)
? "default"
: ospf->name,
json);
}
if (ospf->instance) {
if (use_json)
@ -5882,13 +5880,13 @@ static int show_ip_ospf_neighbor_int_detail_common(struct vty *vty,
vty_out(vty, "\nOSPF Instance: %d\n\n", ospf->instance);
}
ifp = if_lookup_by_name(argv[arg_base]->arg, ospf->vrf_id);
ifp = if_lookup_by_name(ifname, ospf->vrf_id);
if (!ifp) {
if (!use_json)
if (!use_json) {
vty_out(vty, "No such interface.\n");
else {
vty_out(vty, "{}\n");
json_object_free(json);
} else {
if (!json_vrf)
vty_json(vty, json);
}
return CMD_WARNING;
}
@ -5916,37 +5914,114 @@ static int show_ip_ospf_neighbor_int_detail_common(struct vty *vty,
}
}
if (use_json)
vty_json(vty, json);
else
if (use_json) {
if (!json_vrf)
vty_json(vty, json);
} else {
vty_out(vty, "\n");
}
return CMD_SUCCESS;
}
DEFUN (show_ip_ospf_neighbor_int_detail,
show_ip_ospf_neighbor_int_detail_cmd,
"show ip ospf neighbor IFNAME detail [json]",
SHOW_STR
IP_STR
"OSPF information\n"
"Neighbor list\n"
"Interface name\n"
"detail of all neighbors\n"
JSON_STR)
DEFPY(show_ip_ospf_neighbor_int,
show_ip_ospf_neighbor_int_cmd,
"show ip ospf [vrf NAME$vrf_name] neighbor IFNAME$ifname [json$json]",
SHOW_STR
IP_STR
"OSPF information\n"
VRF_CMD_HELP_STR
"Neighbor list\n"
"Interface name\n"
JSON_STR)
{
struct ospf *ospf;
int ret = CMD_SUCCESS;
struct interface *ifp = NULL;
vrf_id_t vrf_id = VRF_DEFAULT;
struct vrf *vrf = NULL;
if (vrf_name && strmatch(vrf_name, VRF_DEFAULT_NAME))
vrf_name = NULL;
if (vrf_name) {
vrf = vrf_lookup_by_name(vrf_name);
if (vrf)
vrf_id = vrf->vrf_id;
}
ospf = ospf_lookup_by_vrf_id(vrf_id);
if (!ospf || !ospf->oi_running) {
if (json)
vty_json_empty(vty);
return ret;
}
if (!json)
show_ip_ospf_neighbour_header(vty);
ifp = if_lookup_by_name(ifname, vrf_id);
if (!ifp) {
if (json)
vty_json_empty(vty);
else
vty_out(vty, "No such interface.\n");
return ret;
}
ret = show_ip_ospf_neighbor_int_common(vty, ospf, ifname, !!json, 0);
return ret;
}
DEFPY(show_ip_ospf_neighbor_int_detail,
show_ip_ospf_neighbor_int_detail_cmd,
"show ip ospf [vrf NAME$vrf_name] neighbor IFNAME$ifname detail [json$json]",
SHOW_STR
IP_STR
"OSPF information\n"
VRF_CMD_HELP_STR
"Neighbor list\n"
"Interface name\n"
"detail of all neighbors\n"
JSON_STR)
{
struct ospf *ospf;
bool uj = use_json(argc, argv);
struct listnode *node = NULL;
int ret = CMD_SUCCESS;
bool ospf_output = false;
if (vrf_name && !strmatch(vrf_name, "all")) {
int inst = 0;
ospf = ospf_lookup_by_inst_name(inst, vrf_name);
if (ospf == NULL || !ospf->oi_running) {
if (!json)
vty_out(vty,
"%% OSPF is not enabled in vrf %s\n",
vrf_name);
else
vty_json_empty(vty);
return CMD_SUCCESS;
}
return show_ip_ospf_neighbor_int_detail_common(
vty, ospf, ifname, !!json, NULL);
}
json_object *json_vrf = NULL;
if (json)
json_vrf = json_object_new_object();
for (ALL_LIST_ELEMENTS_RO(om->ospf, node, ospf)) {
if (!ospf->oi_running)
continue;
ospf_output = true;
ret = show_ip_ospf_neighbor_int_detail_common(vty, ospf, 4,
argv, uj);
ret = show_ip_ospf_neighbor_int_detail_common(vty, ospf, ifname,
!!json, json_vrf);
}
if (json) {
vty_json(vty, json_vrf);
return ret;
}
if (!ospf_output)
@ -5955,25 +6030,20 @@ DEFUN (show_ip_ospf_neighbor_int_detail,
return ret;
}
DEFUN (show_ip_ospf_instance_neighbor_int_detail,
show_ip_ospf_instance_neighbor_int_detail_cmd,
"show ip ospf (1-65535) neighbor IFNAME detail [json]",
SHOW_STR
IP_STR
"OSPF information\n"
"Instance ID\n"
"Neighbor list\n"
"Interface name\n"
"detail of all neighbors\n"
JSON_STR)
DEFPY(show_ip_ospf_instance_neighbor_int_detail,
show_ip_ospf_instance_neighbor_int_detail_cmd,
"show ip ospf (1-65535)$instance neighbor IFNAME$ifname detail [json$json]",
SHOW_STR
IP_STR
"OSPF information\n"
"Instance ID\n"
"Neighbor list\n"
"Interface name\n"
"detail of all neighbors\n"
JSON_STR)
{
int idx_number = 3;
int idx_ifname = 5;
struct ospf *ospf;
unsigned short instance = 0;
bool uj = use_json(argc, argv);
instance = strtoul(argv[idx_number]->arg, NULL, 10);
if (instance != ospf_instance)
return CMD_NOT_MY_INSTANCE;
@ -5981,8 +6051,8 @@ DEFUN (show_ip_ospf_instance_neighbor_int_detail,
if (!ospf || !ospf->oi_running)
return CMD_SUCCESS;
return show_ip_ospf_neighbor_int_detail_common(vty, ospf, idx_ifname,
argv, uj);
return show_ip_ospf_neighbor_int_detail_common(vty, ospf, ifname,
!!json, NULL);
}
/* Show functions */