Skip to content

Commit

Permalink
pimd: Clean up pim RPF/NHT show commands
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Bahr <nbahr@atcorp.com>
  • Loading branch information
nabahr committed Oct 27, 2024
1 parent 2b23bba commit bec5b5c
Showing 1 changed file with 64 additions and 58 deletions.
122 changes: 64 additions & 58 deletions pimd/pim_cmd_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2876,31 +2876,39 @@ static int pim_print_vty_pnc_cache_walkcb(struct hash_bucket *bucket, void *arg)
struct vty *vty = cwd->vty;
struct pim_instance *pim = cwd->pim;
struct nexthop *nh_node = NULL;
ifindex_t first_ifindex;
struct interface *ifp = NULL;
struct ttable *tt = NULL;
char *table = NULL;

/* Prepare table. */
tt = ttable_new(&ttable_styles[TTSTYLE_BLANK]);
ttable_add_row(tt, "Address|Interface|Nexthop");
ttable_add_row(tt, "Address|Interface|Nexthop|Table");
tt->style.cell.rpad = 2;
tt->style.corner = '+';
ttable_restyle(tt);

for (nh_node = pnc->nexthop; nh_node; nh_node = nh_node->next) {
first_ifindex = nh_node->ifindex;

ifp = if_lookup_by_index(first_ifindex, pim->vrf->vrf_id);
for (nh_node = pnc->mrib.nexthop; nh_node; nh_node = nh_node->next) {
ifp = if_lookup_by_index(nh_node->ifindex, pim->vrf->vrf_id);
#if PIM_IPV == 4
ttable_add_row(tt, "%pPA|%s|%pI4|%s", &pnc->addr, ifp ? ifp->name : "NULL",
&nh_node->gate.ipv4, "MRIB");
#else
ttable_add_row(tt, "%pPA|%s|%pI6|%s", &pnc->addr, ifp ? ifp->name : "NULL",
&nh_node->gate.ipv6, "MRIB");
#endif
}

for (nh_node = pnc->urib.nexthop; nh_node; nh_node = nh_node->next) {
ifp = if_lookup_by_index(nh_node->ifindex, pim->vrf->vrf_id);
#if PIM_IPV == 4
ttable_add_row(tt, "%pPA|%s|%pI4", &pnc->rpf.rpf_addr,
ifp ? ifp->name : "NULL", &nh_node->gate.ipv4);
ttable_add_row(tt, "%pPA|%s|%pI4|%s", &pnc->addr, ifp ? ifp->name : "NULL",
&nh_node->gate.ipv4, "URIB");
#else
ttable_add_row(tt, "%pPA|%s|%pI6", &pnc->rpf.rpf_addr,
ifp ? ifp->name : "NULL", &nh_node->gate.ipv6);
ttable_add_row(tt, "%pPA|%s|%pI6|%s", &pnc->addr, ifp ? ifp->name : "NULL",
&nh_node->gate.ipv6, "URIB");
#endif
}

/* Dump the generated table. */
table = ttable_dump(tt, "\n");
vty_out(vty, "%s\n", table);
Expand All @@ -2910,64 +2918,65 @@ static int pim_print_vty_pnc_cache_walkcb(struct hash_bucket *bucket, void *arg)
return CMD_SUCCESS;
}

static int pim_print_json_pnc_cache_walkcb(struct hash_bucket *backet,
void *arg)
static void pim_print_json_nexthop(json_object *json_obj, struct nexthop *nh_node,
struct interface *ifp, char *addr_str, const char *type)
{
struct pim_nexthop_cache *pnc = backet->data;
struct json_pnc_cache_walk_data *cwd = arg;
struct pim_instance *pim = cwd->pim;
struct nexthop *nh_node = NULL;
ifindex_t first_ifindex;
struct interface *ifp = NULL;
char addr_str[PIM_ADDRSTRLEN];
json_object *json_row = NULL;
json_object *json_ifp = NULL;
json_object *json_arr = NULL;
struct pim_interface *pim_ifp = NULL;
bool pim_enable = false;

for (nh_node = pnc->nexthop; nh_node; nh_node = nh_node->next) {
first_ifindex = nh_node->ifindex;
ifp = if_lookup_by_index(first_ifindex, pim->vrf->vrf_id);
snprintfrr(addr_str, sizeof(addr_str), "%pPA",
&pnc->rpf.rpf_addr);
json_object_object_get_ex(cwd->json_obj, addr_str, &json_row);
if (!json_row) {
json_row = json_object_new_object();
json_object_string_addf(json_row, "address", "%pPA",
&pnc->rpf.rpf_addr);
json_object_object_addf(cwd->json_obj, json_row, "%pPA",
&pnc->rpf.rpf_addr);
json_arr = json_object_new_array();
json_object_object_add(json_row, "nexthops", json_arr);
}
json_ifp = json_object_new_object();
json_object_string_add(json_ifp, "interface",
ifp ? ifp->name : "NULL");

if (ifp)
pim_ifp = ifp->info;
if (ifp)
pim_ifp = ifp->info;

json_object_object_get_ex(json_obj, addr_str, &json_row);

if (pim_ifp && pim_ifp->pim_enable)
pim_enable = true;
if (!json_row) {
json_row = json_object_new_object();
json_object_string_addf(json_row, "address", "%s", addr_str);
json_object_object_addf(json_obj, json_row, "%s", addr_str);
json_arr = json_object_new_array();
json_object_object_add(json_row, "nexthops", json_arr);
}

json_object_boolean_add(json_ifp, "pimEnabled", pim_enable);
json_ifp = json_object_new_object();
json_object_string_add(json_ifp, "interface", ifp ? ifp->name : "NULL");
json_object_boolean_add(json_ifp, "pimEnabled", (pim_ifp && pim_ifp->pim_enable));
#if PIM_IPV == 4
json_object_string_addf(json_ifp, "nexthop", "%pI4",
&nh_node->gate.ipv4);
json_object_string_addf(json_ifp, "nexthop", "%pI4", &nh_node->gate.ipv4);
#else
json_object_string_addf(json_ifp, "nexthop", "%pI6",
&nh_node->gate.ipv6);
json_object_string_addf(json_ifp, "nexthop", "%pI6", &nh_node->gate.ipv6);
#endif
json_object_array_add(json_arr, json_ifp);
json_object_string_add(json_ifp, "table", type);
json_object_array_add(json_arr, json_ifp);
}

static int pim_print_json_pnc_cache_walkcb(struct hash_bucket *backet, void *arg)
{
struct pim_nexthop_cache *pnc = backet->data;
struct json_pnc_cache_walk_data *cwd = arg;
json_object *json_obj = cwd->json_obj;
struct pim_instance *pim = cwd->pim;
char addr_str[PIM_ADDRSTRLEN];
struct nexthop *nh_node = NULL;
struct interface *ifp = NULL;

snprintfrr(addr_str, sizeof(addr_str), "%pPA", &pnc->addr);
for (nh_node = pnc->mrib.nexthop; nh_node; nh_node = nh_node->next) {
ifp = if_lookup_by_index(nh_node->ifindex, pim->vrf->vrf_id);
pim_print_json_nexthop(json_obj, nh_node, ifp, addr_str, "MRIB");
}

for (nh_node = pnc->urib.nexthop; nh_node; nh_node = nh_node->next) {
ifp = if_lookup_by_index(nh_node->ifindex, pim->vrf->vrf_id);
pim_print_json_nexthop(json_obj, nh_node, ifp, addr_str, "URIB");
}
return CMD_SUCCESS;
}

int pim_show_nexthop_lookup_cmd_helper(const char *vrf, struct vty *vty,
pim_addr source, pim_addr group)
{
int result = 0;
pim_addr vif_source;
struct prefix grp;
struct pim_nexthop nexthop;
Expand Down Expand Up @@ -3033,19 +3042,16 @@ void pim_show_nexthop(struct pim_instance *pim, struct vty *vty, bool uj)
cwd.pim = pim;
jcwd.pim = pim;

if (uj) {
if (uj)
jcwd.json_obj = json_object_new_object();
} else {
vty_out(vty, "Number of registered addresses: %lu\n",
pim->rpf_hash->count);
}
else
vty_out(vty, "Number of registered addresses: %lu\n", pim->nht_hash->count);

if (uj) {
hash_walk(pim->rpf_hash, pim_print_json_pnc_cache_walkcb,
&jcwd);
hash_walk(pim->nht_hash, pim_print_json_pnc_cache_walkcb, &jcwd);
vty_json(vty, jcwd.json_obj);
} else
hash_walk(pim->rpf_hash, pim_print_vty_pnc_cache_walkcb, &cwd);
hash_walk(pim->nht_hash, pim_print_vty_pnc_cache_walkcb, &cwd);
}

int pim_show_neighbors_cmd_helper(const char *vrf, struct vty *vty,
Expand Down

0 comments on commit bec5b5c

Please sign in to comment.