summaryrefslogtreecommitdiff
path: root/pbrd/pbr_vty.c
diff options
context:
space:
mode:
Diffstat (limited to 'pbrd/pbr_vty.c')
-rw-r--r--pbrd/pbr_vty.c159
1 files changed, 152 insertions, 7 deletions
diff --git a/pbrd/pbr_vty.c b/pbrd/pbr_vty.c
index a52c2d1e30..54029206cc 100644
--- a/pbrd/pbr_vty.c
+++ b/pbrd/pbr_vty.c
@@ -27,6 +27,7 @@
#include "nexthop_group.h"
#include "nexthop_group_private.h"
#include "log.h"
+#include "json.h"
#include "debug.h"
#include "pbr.h"
@@ -590,6 +591,61 @@ static void vty_show_pbrms(struct vty *vty,
}
}
+static void vty_json_pbrms(json_object *j, struct vty *vty,
+ const struct pbr_map_sequence *pbrms)
+{
+ json_object *jpbrm, *nexthop_group;
+ char *nhg_name = pbrms->nhgrp_name ? pbrms->nhgrp_name
+ : pbrms->internal_nhg_name;
+ char buf[PREFIX_STRLEN];
+ char rbuf[64];
+
+ jpbrm = json_object_new_object();
+
+ json_object_int_add(jpbrm, "id", pbrms->unique);
+
+ if (pbrms->reason)
+ pbr_map_reason_string(pbrms->reason, rbuf, sizeof(rbuf));
+
+ json_object_int_add(jpbrm, "sequenceNumber", pbrms->seqno);
+ json_object_int_add(jpbrm, "ruleNumber", pbrms->ruleno);
+ json_object_boolean_add(jpbrm, "vrfUnchanged", pbrms->vrf_unchanged);
+ json_object_boolean_add(jpbrm, "installed",
+ pbr_nht_get_installed(nhg_name));
+ json_object_string_add(jpbrm, "installedReason",
+ pbrms->reason ? rbuf : "Valid");
+
+ if (nhg_name) {
+ nexthop_group = json_object_new_object();
+
+ json_object_int_add(nexthop_group, "tableId",
+ pbr_nht_get_table(nhg_name));
+ json_object_string_add(nexthop_group, "name", nhg_name);
+ json_object_boolean_add(nexthop_group, "installed",
+ pbr_nht_get_installed(nhg_name));
+ json_object_int_add(nexthop_group, "installedInternally",
+ pbrms->nhs_installed);
+
+ json_object_object_add(jpbrm, "nexthopGroup", nexthop_group);
+ }
+
+ if (pbrms->vrf_lookup)
+ json_object_string_add(jpbrm, "vrfName", pbrms->vrf_name);
+
+ if (pbrms->src)
+ json_object_string_add(
+ jpbrm, "matchSrc",
+ prefix2str(pbrms->src, buf, sizeof(buf)));
+ if (pbrms->dst)
+ json_object_string_add(
+ jpbrm, "matchDst",
+ prefix2str(pbrms->dst, buf, sizeof(buf)));
+ if (pbrms->mark)
+ json_object_int_add(jpbrm, "matchMark", pbrms->mark);
+
+ json_object_array_add(j, jpbrm);
+}
+
static void vty_show_pbr_map(struct vty *vty, const struct pbr_map *pbrm,
bool detail)
{
@@ -603,54 +659,121 @@ static void vty_show_pbr_map(struct vty *vty, const struct pbr_map *pbrm,
vty_show_pbrms(vty, pbrms, detail);
}
+static void vty_json_pbr_map(json_object *j, struct vty *vty,
+ const struct pbr_map *pbrm)
+{
+ struct pbr_map_sequence *pbrms;
+ struct listnode *node;
+ json_object *jpbrms;
+
+ json_object_string_add(j, "name", pbrm->name);
+ json_object_boolean_add(j, "valid", pbrm->valid);
+
+ jpbrms = json_object_new_array();
+
+ for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, node, pbrms))
+ vty_json_pbrms(jpbrms, vty, pbrms);
+
+ json_object_object_add(j, "policies", jpbrms);
+}
+
DEFPY (show_pbr_map,
show_pbr_map_cmd,
- "show pbr map [NAME$name] [detail$detail]",
+ "show pbr map [NAME$name] [detail$detail|json$json]",
SHOW_STR
PBR_STR
"PBR Map\n"
"PBR Map Name\n"
- "Detailed information\n")
+ "Detailed information\n"
+ JSON_STR)
{
struct pbr_map *pbrm;
+ json_object *j = NULL;
+
+ if (json)
+ j = json_object_new_array();
RB_FOREACH (pbrm, pbr_map_entry_head, &pbr_maps) {
+ json_object *this_map = NULL;
if (name && strcmp(name, pbrm->name) != 0)
continue;
+ if (j)
+ this_map = json_object_new_object();
+
+ if (this_map) {
+ vty_json_pbr_map(this_map, vty, pbrm);
+
+ json_object_array_add(j, this_map);
+ continue;
+ }
+
vty_show_pbr_map(vty, pbrm, detail);
}
+
+ if (j) {
+ vty_out(vty, "%s\n",
+ json_object_to_json_string_ext(
+ j, JSON_C_TO_STRING_PRETTY));
+ json_object_free(j);
+ }
+
return CMD_SUCCESS;
}
DEFPY(show_pbr_nexthop_group,
show_pbr_nexthop_group_cmd,
- "show pbr nexthop-groups [WORD$word]",
+ "show pbr nexthop-groups [WORD$word] [json$json]",
SHOW_STR
PBR_STR
"Nexthop Groups\n"
- "Optional Name of the nexthop group\n")
+ "Optional Name of the nexthop group\n"
+ JSON_STR)
{
- pbr_nht_show_nexthop_group(vty, word);
+ json_object *j = NULL;
+
+ if (json)
+ j = json_object_new_array();
+
+ if (j) {
+ pbr_nht_json_nexthop_group(j, word);
+
+ vty_out(vty, "%s\n",
+ json_object_to_json_string_ext(
+ j, JSON_C_TO_STRING_PRETTY));
+
+ json_object_free(j);
+ } else
+ pbr_nht_show_nexthop_group(vty, word);
+
return CMD_SUCCESS;
}
DEFPY (show_pbr_interface,
show_pbr_interface_cmd,
- "show pbr interface [NAME$name]",
+ "show pbr interface [NAME$name] [json$json]",
SHOW_STR
PBR_STR
"PBR Interface\n"
- "PBR Interface Name\n")
+ "PBR Interface Name\n"
+ JSON_STR)
{
struct interface *ifp;
struct vrf *vrf;
struct pbr_interface *pbr_ifp;
+ json_object *j = NULL;
+
+ if (json)
+ j = json_object_new_array();
RB_FOREACH(vrf, vrf_name_head, &vrfs_by_name) {
FOR_ALL_INTERFACES(vrf, ifp) {
struct pbr_map *pbrm;
+ json_object *this_iface = NULL;
+
+ if (j)
+ this_iface = json_object_new_object();
if (!ifp->info)
continue;
@@ -664,6 +787,21 @@ DEFPY (show_pbr_interface,
continue;
pbrm = pbrm_find(pbr_ifp->mapname);
+
+ if (this_iface) {
+ json_object_string_add(this_iface, "name",
+ ifp->name);
+ json_object_int_add(this_iface, "index",
+ ifp->ifindex);
+ json_object_string_add(this_iface, "policy",
+ pbr_ifp->mapname);
+ json_object_boolean_add(this_iface, "valid",
+ pbrm);
+
+ json_object_array_add(j, this_iface);
+ continue;
+ }
+
vty_out(vty, " %s(%d) with pbr-policy %s", ifp->name,
ifp->ifindex, pbr_ifp->mapname);
if (!pbrm)
@@ -672,6 +810,13 @@ DEFPY (show_pbr_interface,
}
}
+ if (j) {
+ vty_out(vty, "%s\n",
+ json_object_to_json_string_ext(
+ j, JSON_C_TO_STRING_PRETTY));
+ json_object_free(j);
+ }
+
return CMD_SUCCESS;
}