ospfd: Decode Extended Link & Prefix TLVs for json

When dumping ospf database with json output, decode Extended Link and Extended
Prefix TLVs and sub-TLVs.

Signed-off-by: Olivier Dugeon <olivier.dugeon@orange.com>
This commit is contained in:
Olivier Dugeon 2023-12-19 16:00:25 +01:00
parent b27d9e4d3b
commit 6fcc86d37a

View file

@ -31,6 +31,7 @@
#include "network.h" #include "network.h"
#include "if.h" #include "if.h"
#include "libospf.h" /* for ospf interface types */ #include "libospf.h" /* for ospf interface types */
#include <lib/json.h>
#include "ospfd/ospfd.h" #include "ospfd/ospfd.h"
#include "ospfd/ospf_interface.h" #include "ospfd/ospf_interface.h"
@ -1715,23 +1716,29 @@ static void ospf_ext_lsa_schedule(struct ext_itf *exti, enum lsa_opcode op)
/* Cisco experimental SubTLV */ /* Cisco experimental SubTLV */
static uint16_t show_vty_ext_link_rmt_itf_addr(struct vty *vty, static uint16_t show_vty_ext_link_rmt_itf_addr(struct vty *vty,
struct tlv_header *tlvh) struct tlv_header *tlvh,
json_object *json)
{ {
struct ext_subtlv_rmt_itf_addr *top = struct ext_subtlv_rmt_itf_addr *top =
(struct ext_subtlv_rmt_itf_addr *)tlvh; (struct ext_subtlv_rmt_itf_addr *)tlvh;
check_tlv_size(EXT_SUBTLV_RMT_ITF_ADDR_SIZE, "Remote Itf. Address"); check_tlv_size(EXT_SUBTLV_RMT_ITF_ADDR_SIZE, "Remote Itf. Address");
vty_out(vty, if (!json)
" Remote Interface Address Sub-TLV: Length %u\n Address: %pI4\n", vty_out(vty,
ntohs(top->header.length), &top->value); " Remote Interface Address Sub-TLV: Length %u\n Address: %pI4\n",
ntohs(top->header.length), &top->value);
else
json_object_string_addf(json, "remoteInterfaceAddress", "%pI4",
&top->value);
return TLV_SIZE(tlvh); return TLV_SIZE(tlvh);
} }
/* Adjacency SID SubTLV */ /* Adjacency SID SubTLV */
static uint16_t show_vty_ext_link_adj_sid(struct vty *vty, static uint16_t show_vty_ext_link_adj_sid(struct vty *vty,
struct tlv_header *tlvh) struct tlv_header *tlvh,
json_object *json)
{ {
struct ext_subtlv_adj_sid *top = (struct ext_subtlv_adj_sid *)tlvh; struct ext_subtlv_adj_sid *top = (struct ext_subtlv_adj_sid *)tlvh;
uint8_t tlv_size; uint8_t tlv_size;
@ -1741,21 +1748,35 @@ static uint16_t show_vty_ext_link_adj_sid(struct vty *vty,
: SID_INDEX_SIZE(EXT_SUBTLV_ADJ_SID_SIZE); : SID_INDEX_SIZE(EXT_SUBTLV_ADJ_SID_SIZE);
check_tlv_size(tlv_size, "Adjacency SID"); check_tlv_size(tlv_size, "Adjacency SID");
vty_out(vty, if (!json)
" Adj-SID Sub-TLV: Length %u\n\tFlags: 0x%x\n\tMT-ID:0x%x\n\tWeight: 0x%x\n\t%s: %u\n", vty_out(vty,
ntohs(top->header.length), top->flags, top->mtid, top->weight, " Adj-SID Sub-TLV: Length %u\n\tFlags: 0x%x\n\tMT-ID:0x%x\n\tWeight: 0x%x\n\t%s: %u\n",
CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG) ? "Label" ntohs(top->header.length), top->flags, top->mtid,
: "Index", top->weight,
CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG) CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG)
? GET_LABEL(ntohl(top->value)) ? "Label"
: ntohl(top->value)); : "Index",
CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG)
? GET_LABEL(ntohl(top->value))
: ntohl(top->value));
else {
json_object_string_addf(json, "flags", "0x%x", top->flags);
json_object_string_addf(json, "mtID", "0x%x", top->mtid);
json_object_string_addf(json, "weight", "0x%x", top->weight);
if (CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG))
json_object_int_add(json, "label",
GET_LABEL(ntohl(top->value)));
else
json_object_int_add(json, "index", ntohl(top->value));
}
return TLV_SIZE(tlvh); return TLV_SIZE(tlvh);
} }
/* LAN Adjacency SubTLV */ /* LAN Adjacency SubTLV */
static uint16_t show_vty_ext_link_lan_adj_sid(struct vty *vty, static uint16_t show_vty_ext_link_lan_adj_sid(struct vty *vty,
struct tlv_header *tlvh) struct tlv_header *tlvh,
json_object *json)
{ {
struct ext_subtlv_lan_adj_sid *top = struct ext_subtlv_lan_adj_sid *top =
(struct ext_subtlv_lan_adj_sid *)tlvh; (struct ext_subtlv_lan_adj_sid *)tlvh;
@ -1766,42 +1787,67 @@ static uint16_t show_vty_ext_link_lan_adj_sid(struct vty *vty,
: SID_INDEX_SIZE(EXT_SUBTLV_LAN_ADJ_SID_SIZE); : SID_INDEX_SIZE(EXT_SUBTLV_LAN_ADJ_SID_SIZE);
check_tlv_size(tlv_size, "LAN-Adjacency SID"); check_tlv_size(tlv_size, "LAN-Adjacency SID");
vty_out(vty, if (!json)
" LAN-Adj-SID Sub-TLV: Length %u\n\tFlags: 0x%x\n\tMT-ID:0x%x\n\tWeight: 0x%x\n\tNeighbor ID: %pI4\n\t%s: %u\n", vty_out(vty,
ntohs(top->header.length), top->flags, top->mtid, top->weight, " LAN-Adj-SID Sub-TLV: Length %u\n\tFlags: 0x%x\n\tMT-ID:0x%x\n\tWeight: 0x%x\n\tNeighbor ID: %pI4\n\t%s: %u\n",
&top->neighbor_id, ntohs(top->header.length), top->flags, top->mtid,
CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG) ? "Label" top->weight, &top->neighbor_id,
: "Index", CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG)
CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG) ? "Label"
? GET_LABEL(ntohl(top->value)) : "Index",
: ntohl(top->value)); CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG)
? GET_LABEL(ntohl(top->value))
: ntohl(top->value));
else {
json_object_string_addf(json, "flags", "0x%x", top->flags);
json_object_string_addf(json, "mtID", "0x%x", top->mtid);
json_object_string_addf(json, "weight", "0x%x", top->weight);
json_object_string_addf(json, "neighborID", "%pI4",
&top->neighbor_id);
if (CHECK_FLAG(top->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG))
json_object_int_add(json, "label",
GET_LABEL(ntohl(top->value)));
else
json_object_int_add(json, "index", ntohl(top->value));
}
return TLV_SIZE(tlvh); return TLV_SIZE(tlvh);
} }
static uint16_t show_vty_unknown_tlv(struct vty *vty, struct tlv_header *tlvh, static uint16_t show_vty_unknown_tlv(struct vty *vty, struct tlv_header *tlvh,
size_t buf_size) size_t buf_size, json_object *json)
{ {
json_object *obj;
if (TLV_SIZE(tlvh) > buf_size) { if (TLV_SIZE(tlvh) > buf_size) {
vty_out(vty, " TLV size %d exceeds buffer size. Abort!", vty_out(vty, " TLV size %d exceeds buffer size. Abort!",
TLV_SIZE(tlvh)); TLV_SIZE(tlvh));
return buf_size; return buf_size;
} }
if (!json)
vty_out(vty, " Unknown TLV: [type(0x%x), length(0x%x)]\n", vty_out(vty, " Unknown TLV: [type(0x%x), length(0x%x)]\n",
ntohs(tlvh->type), ntohs(tlvh->length)); ntohs(tlvh->type), ntohs(tlvh->length));
else {
obj = json_object_new_object();
json_object_string_addf(obj, "type", "0x%x",
ntohs(tlvh->type));
json_object_string_addf(obj, "length", "0x%x",
ntohs(tlvh->length));
json_object_object_add(json, "unknownTLV", obj);
}
return TLV_SIZE(tlvh); return TLV_SIZE(tlvh);
} }
/* Extended Link Sub TLVs */ /* Extended Link Sub TLVs */
static uint16_t show_vty_link_info(struct vty *vty, struct tlv_header *ext, static uint16_t show_vty_link_info(struct vty *vty, struct tlv_header *ext,
size_t buf_size) size_t buf_size, json_object *json)
{ {
struct ext_tlv_link *top = (struct ext_tlv_link *)ext; struct ext_tlv_link *top = (struct ext_tlv_link *)ext;
struct tlv_header *tlvh; struct tlv_header *tlvh;
uint16_t length = ntohs(top->header.length); uint16_t length = ntohs(top->header.length);
uint16_t sum = 0; uint16_t sum = 0;
json_object *jadj = NULL, *obj = NULL;
/* Verify that TLV length is valid against remaining buffer size */ /* Verify that TLV length is valid against remaining buffer size */
if (length > buf_size) { if (length > buf_size) {
@ -1811,12 +1857,22 @@ static uint16_t show_vty_link_info(struct vty *vty, struct tlv_header *ext,
return buf_size; return buf_size;
} }
vty_out(vty, if (!json) {
" Extended Link TLV: Length %u\n Link Type: 0x%x\n" vty_out(vty,
" Link ID: %pI4\n", " Extended Link TLV: Length %u\n Link Type: 0x%x\n"
ntohs(top->header.length), top->link_type, " Link ID: %pI4\n",
&top->link_id); ntohs(top->header.length), top->link_type,
vty_out(vty, " Link data: %pI4\n", &top->link_data); &top->link_id);
vty_out(vty, " Link data: %pI4\n", &top->link_data);
} else {
json_object_string_addf(json, "linkType", "0x%x",
top->link_type);
json_object_string_addf(json, "linkID", "%pI4", &top->link_id);
json_object_string_addf(json, "linkData", "%pI4",
&top->link_data);
jadj = json_object_new_array();
json_object_object_add(json, "adjacencySID", jadj);
}
/* Skip Extended TLV and parse sub-TLVs */ /* Skip Extended TLV and parse sub-TLVs */
length -= EXT_TLV_LINK_SIZE; length -= EXT_TLV_LINK_SIZE;
@ -1825,16 +1881,27 @@ static uint16_t show_vty_link_info(struct vty *vty, struct tlv_header *ext,
for (; sum < length && tlvh; tlvh = TLV_HDR_NEXT(tlvh)) { for (; sum < length && tlvh; tlvh = TLV_HDR_NEXT(tlvh)) {
switch (ntohs(tlvh->type)) { switch (ntohs(tlvh->type)) {
case EXT_SUBTLV_ADJ_SID: case EXT_SUBTLV_ADJ_SID:
sum += show_vty_ext_link_adj_sid(vty, tlvh); if (json) {
obj = json_object_new_object();
json_object_array_add(jadj, obj);
} else
obj = NULL;
sum += show_vty_ext_link_adj_sid(vty, tlvh, obj);
break; break;
case EXT_SUBTLV_LAN_ADJ_SID: case EXT_SUBTLV_LAN_ADJ_SID:
sum += show_vty_ext_link_lan_adj_sid(vty, tlvh); if (json) {
obj = json_object_new_object();
json_object_array_add(jadj, obj);
} else
obj = NULL;
sum += show_vty_ext_link_lan_adj_sid(vty, tlvh, obj);
break; break;
case EXT_SUBTLV_RMT_ITF_ADDR: case EXT_SUBTLV_RMT_ITF_ADDR:
sum += show_vty_ext_link_rmt_itf_addr(vty, tlvh); sum += show_vty_ext_link_rmt_itf_addr(vty, tlvh, json);
break; break;
default: default:
sum += show_vty_unknown_tlv(vty, tlvh, length - sum); sum += show_vty_unknown_tlv(vty, tlvh, length - sum,
json);
break; break;
} }
} }
@ -1849,9 +1916,12 @@ static void ospf_ext_link_show_info(struct vty *vty, struct json_object *json,
struct lsa_header *lsah = lsa->data; struct lsa_header *lsah = lsa->data;
struct tlv_header *tlvh; struct tlv_header *tlvh;
uint16_t length = 0, sum = 0; uint16_t length = 0, sum = 0;
json_object *jlink = NULL;
if (json) if (json) {
return; jlink = json_object_new_object();
json_object_object_add(json, "extendedLink", jlink);
}
/* Initialize TLV browsing */ /* Initialize TLV browsing */
length = lsa->size - OSPF_LSA_HEADER_SIZE; length = lsa->size - OSPF_LSA_HEADER_SIZE;
@ -1860,10 +1930,12 @@ static void ospf_ext_link_show_info(struct vty *vty, struct json_object *json,
tlvh = TLV_HDR_NEXT(tlvh)) { tlvh = TLV_HDR_NEXT(tlvh)) {
switch (ntohs(tlvh->type)) { switch (ntohs(tlvh->type)) {
case EXT_TLV_LINK: case EXT_TLV_LINK:
sum += show_vty_link_info(vty, tlvh, length - sum); sum += show_vty_link_info(vty, tlvh, length - sum,
jlink);
break; break;
default: default:
sum += show_vty_unknown_tlv(vty, tlvh, length - sum); sum += show_vty_unknown_tlv(vty, tlvh, length - sum,
jlink);
break; break;
} }
} }
@ -1871,7 +1943,8 @@ static void ospf_ext_link_show_info(struct vty *vty, struct json_object *json,
/* Prefix SID SubTLV */ /* Prefix SID SubTLV */
static uint16_t show_vty_ext_pref_pref_sid(struct vty *vty, static uint16_t show_vty_ext_pref_pref_sid(struct vty *vty,
struct tlv_header *tlvh) struct tlv_header *tlvh,
json_object *json)
{ {
struct ext_subtlv_prefix_sid *top = struct ext_subtlv_prefix_sid *top =
(struct ext_subtlv_prefix_sid *)tlvh; (struct ext_subtlv_prefix_sid *)tlvh;
@ -1882,27 +1955,39 @@ static uint16_t show_vty_ext_pref_pref_sid(struct vty *vty,
: SID_INDEX_SIZE(EXT_SUBTLV_PREFIX_SID_SIZE); : SID_INDEX_SIZE(EXT_SUBTLV_PREFIX_SID_SIZE);
check_tlv_size(tlv_size, "Prefix SID"); check_tlv_size(tlv_size, "Prefix SID");
vty_out(vty, if (!json)
" Prefix SID Sub-TLV: Length %u\n\tAlgorithm: %u\n\tFlags: 0x%x\n\tMT-ID:0x%x\n\t%s: %u\n", vty_out(vty,
ntohs(top->header.length), top->algorithm, top->flags, " Prefix SID Sub-TLV: Length %u\n\tAlgorithm: %u\n\tFlags: 0x%x\n\tMT-ID:0x%x\n\t%s: %u\n",
top->mtid, ntohs(top->header.length), top->algorithm, top->flags,
CHECK_FLAG(top->flags, EXT_SUBTLV_PREFIX_SID_VFLG) ? "Label" top->mtid,
: "Index", CHECK_FLAG(top->flags, EXT_SUBTLV_PREFIX_SID_VFLG)
CHECK_FLAG(top->flags, EXT_SUBTLV_PREFIX_SID_VFLG) ? "Label"
? GET_LABEL(ntohl(top->value)) : "Index",
: ntohl(top->value)); CHECK_FLAG(top->flags, EXT_SUBTLV_PREFIX_SID_VFLG)
? GET_LABEL(ntohl(top->value))
: ntohl(top->value));
else {
json_object_int_add(json, "algorithm", top->algorithm);
json_object_string_addf(json, "flags", "0x%x", top->flags);
json_object_string_addf(json, "mtID", "0x%x", top->mtid);
if (CHECK_FLAG(top->flags, EXT_SUBTLV_PREFIX_SID_VFLG))
json_object_int_add(json, "label",
GET_LABEL(ntohl(top->value)));
else
json_object_int_add(json, "index", ntohl(top->value));
}
return TLV_SIZE(tlvh); return TLV_SIZE(tlvh);
} }
/* Extended Prefix SubTLVs */ /* Extended Prefix SubTLVs */
static uint16_t show_vty_pref_info(struct vty *vty, struct tlv_header *ext, static uint16_t show_vty_pref_info(struct vty *vty, struct tlv_header *ext,
size_t buf_size) size_t buf_size, json_object *json)
{ {
struct ext_tlv_prefix *top = (struct ext_tlv_prefix *)ext; struct ext_tlv_prefix *top = (struct ext_tlv_prefix *)ext;
struct tlv_header *tlvh; struct tlv_header *tlvh;
uint16_t length = ntohs(top->header.length); uint16_t length = ntohs(top->header.length);
uint16_t sum = 0; uint16_t sum = 0;
json_object *jsid = NULL;
/* Verify that TLV length is valid against remaining buffer size */ /* Verify that TLV length is valid against remaining buffer size */
if (length > buf_size) { if (length > buf_size) {
@ -1912,11 +1997,21 @@ static uint16_t show_vty_pref_info(struct vty *vty, struct tlv_header *ext,
return buf_size; return buf_size;
} }
vty_out(vty, if (!json)
" Extended Prefix TLV: Length %u\n\tRoute Type: %u\n" vty_out(vty,
"\tAddress Family: 0x%x\n\tFlags: 0x%x\n\tAddress: %pI4/%u\n", " Extended Prefix TLV: Length %u\n\tRoute Type: %u\n"
ntohs(top->header.length), top->route_type, top->af, top->flags, "\tAddress Family: 0x%x\n\tFlags: 0x%x\n\tAddress: %pI4/%u\n",
&top->address, top->pref_length); ntohs(top->header.length), top->route_type, top->af,
top->flags, &top->address, top->pref_length);
else {
json_object_int_add(json, "routeType", top->route_type);
json_object_string_addf(json, "addressFamily", "0x%x", top->af);
json_object_string_addf(json, "flags", "0x%x", top->flags);
json_object_string_addf(json, "address", "%pI4", &top->address);
json_object_int_add(json, "prefixLength", top->pref_length);
jsid = json_object_new_object();
json_object_object_add(json, "prefixSID", jsid);
}
/* Skip Extended Prefix TLV and parse sub-TLVs */ /* Skip Extended Prefix TLV and parse sub-TLVs */
length -= EXT_TLV_PREFIX_SIZE; length -= EXT_TLV_PREFIX_SIZE;
@ -1925,10 +2020,11 @@ static uint16_t show_vty_pref_info(struct vty *vty, struct tlv_header *ext,
for (; sum < length && tlvh; tlvh = TLV_HDR_NEXT(tlvh)) { for (; sum < length && tlvh; tlvh = TLV_HDR_NEXT(tlvh)) {
switch (ntohs(tlvh->type)) { switch (ntohs(tlvh->type)) {
case EXT_SUBTLV_PREFIX_SID: case EXT_SUBTLV_PREFIX_SID:
sum += show_vty_ext_pref_pref_sid(vty, tlvh); sum += show_vty_ext_pref_pref_sid(vty, tlvh, jsid);
break; break;
default: default:
sum += show_vty_unknown_tlv(vty, tlvh, length - sum); sum += show_vty_unknown_tlv(vty, tlvh, length - sum,
json);
break; break;
} }
} }
@ -1943,9 +2039,12 @@ static void ospf_ext_pref_show_info(struct vty *vty, struct json_object *json,
struct lsa_header *lsah = lsa->data; struct lsa_header *lsah = lsa->data;
struct tlv_header *tlvh; struct tlv_header *tlvh;
uint16_t length = 0, sum = 0; uint16_t length = 0, sum = 0;
json_object *jpref = NULL;
if (json) if (json) {
return; jpref = json_object_new_object();
json_object_object_add(json, "extendedPrefix", jpref);
}
/* Initialize TLV browsing */ /* Initialize TLV browsing */
length = lsa->size - OSPF_LSA_HEADER_SIZE; length = lsa->size - OSPF_LSA_HEADER_SIZE;
@ -1954,10 +2053,12 @@ static void ospf_ext_pref_show_info(struct vty *vty, struct json_object *json,
tlvh = TLV_HDR_NEXT(tlvh)) { tlvh = TLV_HDR_NEXT(tlvh)) {
switch (ntohs(tlvh->type)) { switch (ntohs(tlvh->type)) {
case EXT_TLV_PREFIX: case EXT_TLV_PREFIX:
sum += show_vty_pref_info(vty, tlvh, length - sum); sum += show_vty_pref_info(vty, tlvh, length - sum,
jpref);
break; break;
default: default:
sum += show_vty_unknown_tlv(vty, tlvh, length - sum); sum += show_vty_unknown_tlv(vty, tlvh, length - sum,
jpref);
break; break;
} }
} }