Skip to content

Commit

Permalink
pbr: format change in nexthop-group json output
Browse files Browse the repository at this point in the history
 - JSON format is changed.
	currently json output returns as array format,
        in which we cannot add extra attributes under it.
   	so, we modified the JSON format into dict.
 - Added nhgCount into vtysh "show pbr nexthop-groups json".
 - Addressed interface-name & vrf-id issues.
	currently, "vrfId" is mapped with interface name.

supported commands:

```
show pbr nexthop-groups json
show pbr nexthop-groups <nh-grp> json
```

Ticket:#3963583

Issue: 3963583

Signed-off-by: Sindhu Parvathi Gopinathan's <[email protected]>
  • Loading branch information
Sindhu Parvathi Gopinathan authored and krishna-samy committed Jan 3, 2025
1 parent 83aa6b3 commit 42578fa
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
9 changes: 6 additions & 3 deletions lib/nexthop_group.c
Original file line number Diff line number Diff line change
Expand Up @@ -1080,24 +1080,27 @@ void nexthop_group_json_nexthop(json_object *j, const struct nexthop *nh)

switch (nh->type) {
case NEXTHOP_TYPE_IFINDEX:
json_object_string_add(j, "nexthop",
json_object_string_add(j, "interfaceName",
ifindex2ifname(nh->ifindex, nh->vrf_id));
json_object_int_add(j, "vrfId", nh->vrf_id);
break;
case NEXTHOP_TYPE_IPV4:
json_object_string_addf(j, "nexthop", "%pI4", &nh->gate.ipv4);
break;
case NEXTHOP_TYPE_IPV4_IFINDEX:
json_object_string_addf(j, "nexthop", "%pI4", &nh->gate.ipv4);
json_object_string_add(j, "vrfId",
json_object_string_add(j, "interfaceName",
ifindex2ifname(nh->ifindex, nh->vrf_id));
json_object_int_add(j, "vrfId", nh->vrf_id);
break;
case NEXTHOP_TYPE_IPV6:
json_object_string_addf(j, "nexthop", "%pI6", &nh->gate.ipv6);
break;
case NEXTHOP_TYPE_IPV6_IFINDEX:
json_object_string_addf(j, "nexthop", "%pI6", &nh->gate.ipv6);
json_object_string_add(j, "vrfId",
json_object_string_add(j, "interfaceName",
ifindex2ifname(nh->ifindex, nh->vrf_id));
json_object_int_add(j, "vrfId", nh->vrf_id);
break;
case NEXTHOP_TYPE_BLACKHOLE:
break;
Expand Down
9 changes: 7 additions & 2 deletions pbrd/pbr_nht.c
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,7 @@ struct pbr_nht_show {
struct vty *vty;
json_object *json;
const char *name;
uint16_t nhg_count;
};

static void pbr_nht_show_nhg(struct hash_bucket *b, void *data)
Expand Down Expand Up @@ -1447,7 +1448,8 @@ static void pbr_nht_json_nhg(struct hash_bucket *b, void *data)
json_object_object_add(this_group, "nexthops", group_hops);
}

json_object_array_add(j, this_group);
json_object_object_addf(j, this_group, "%u", pnhgc->table_id);
pns->nhg_count++;
}

void pbr_nht_show_nexthop_group(struct vty *vty, const char *name)
Expand All @@ -1460,14 +1462,17 @@ void pbr_nht_show_nexthop_group(struct vty *vty, const char *name)
hash_iterate(pbr_nhg_hash, pbr_nht_show_nhg, &pns);
}

void pbr_nht_json_nexthop_group(json_object *j, const char *name)
void pbr_nht_json_nexthop_group(json_object *j, const char *name,
uint16_t *pbr_nhg_count)
{
struct pbr_nht_show pns;

pns.name = name;
pns.json = j;
pns.nhg_count = 0;

hash_iterate(pbr_nhg_hash, pbr_nht_json_nhg, &pns);
*pbr_nhg_count = pns.nhg_count;
}

void pbr_nht_init(void)
Expand Down
3 changes: 2 additions & 1 deletion pbrd/pbr_nht.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ extern char *pbr_nht_nexthop_make_name(char *name, size_t l, uint32_t seqno,
char *buffer);

extern void pbr_nht_show_nexthop_group(struct vty *vty, const char *name);
extern void pbr_nht_json_nexthop_group(json_object *j, const char *name);
extern void pbr_nht_json_nexthop_group(json_object *j, const char *name,
uint16_t *pbr_nhg_count);

/*
* When we get a callback from zebra about a nexthop changing
Expand Down
7 changes: 4 additions & 3 deletions pbrd/pbr_vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -1897,13 +1897,14 @@ DEFPY(show_pbr_nexthop_group,
JSON_STR)
{
json_object *j = NULL;
uint16_t pbr_nhg_count = 0;

if (json)
j = json_object_new_array();
j = json_object_new_object();

if (j) {
pbr_nht_json_nexthop_group(j, word);

pbr_nht_json_nexthop_group(j, word, &pbr_nhg_count);
json_object_int_add(j, "nhgCount", pbr_nhg_count);
vty_json(vty, j);
} else
pbr_nht_show_nexthop_group(vty, word);
Expand Down

0 comments on commit 42578fa

Please sign in to comment.