Skip to content

Commit

Permalink
bgpd: add json output for show bgp segment-routing srv6
Browse files Browse the repository at this point in the history
Signed-off-by: Yamato Sugawara <yamato.sugawara@linecorp.com>
and add the test bgpd output to the locator_chunks topotest
  • Loading branch information
Drumato committed Nov 3, 2021
1 parent 1f9c999 commit ae1ac7f
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 49 deletions.
139 changes: 105 additions & 34 deletions bgpd/bgp_vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -9215,11 +9215,12 @@ DEFPY (no_bgp_srv6_locator,

DEFPY (show_bgp_srv6,
show_bgp_srv6_cmd,
"show bgp segment-routing srv6",
"show bgp segment-routing srv6 [json]",
SHOW_STR
BGP_STR
"BGP Segment Routing\n"
"BGP Segment Routing SRv6\n")
"BGP Segment Routing SRv6\n"
JSON_STR)
{
struct bgp *bgp;
struct listnode *node;
Expand All @@ -9230,43 +9231,113 @@ DEFPY (show_bgp_srv6,
char buf[256];
char buf_tovpn4_sid[256];
char buf_tovpn6_sid[256];
json_object *json = NULL;
json_object *json_chunks = NULL;
json_object *json_function = NULL;
json_object *json_functions = NULL;
json_object *json_bgp = NULL;
json_object *json_bgps = NULL;
json_object *json_vpn_policy = NULL;
json_object *json_vpn_policy_ip = NULL;
json_object *json_vpn_policy_ip6 = NULL;
bool uj = use_json(argc, argv);

bgp = bgp_get_default();
if (!bgp)
return CMD_SUCCESS;

vty_out(vty, "locator_name: %s\n", bgp->srv6_locator_name);
vty_out(vty, "locator_chunks:\n");
for (ALL_LIST_ELEMENTS_RO(bgp->srv6_locator_chunks, node, chunk)) {
prefix2str(chunk, buf, sizeof(buf));
vty_out(vty, "- %s\n", buf);
}

vty_out(vty, "functions:\n");
for (ALL_LIST_ELEMENTS_RO(bgp->srv6_functions, node, func)) {
inet_ntop(AF_INET6, &func->sid, buf, sizeof(buf));
vty_out(vty, "- sid: %s\n", buf);
vty_out(vty, " locator: %s\n", func->locator_name);
}

vty_out(vty, "bgps:\n");
for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp)) {
vty_out(vty, "- name: %s\n",
bgp->name ? bgp->name : "default");

tovpn4_sid = bgp->vpn_policy[AFI_IP].tovpn_sid;
tovpn6_sid = bgp->vpn_policy[AFI_IP6].tovpn_sid;
if (tovpn4_sid)
inet_ntop(AF_INET6, tovpn4_sid, buf_tovpn4_sid,
sizeof(buf_tovpn4_sid));
if (tovpn6_sid)
inet_ntop(AF_INET6, tovpn6_sid, buf_tovpn6_sid,
sizeof(buf_tovpn6_sid));

vty_out(vty, " vpn_policy[AFI_IP].tovpn_sid: %s\n",
tovpn4_sid ? buf_tovpn4_sid : "none");
vty_out(vty, " vpn_policy[AFI_IP6].tovpn_sid: %s\n",
tovpn6_sid ? buf_tovpn6_sid : "none");
if (uj) {
json = json_object_new_object();
json_chunks = json_object_new_array();
json_functions = json_object_new_array();
json_bgps = json_object_new_array();
json_object_string_add(json, "locatorName", bgp->srv6_locator_name);
json_object_object_add(json, "locatorChunks", json_chunks);
json_object_object_add(json, "functions", json_functions);
json_object_object_add(json, "bgps", json_bgps);

/* collect all chunk to json array*/
for (ALL_LIST_ELEMENTS_RO(bgp->srv6_locator_chunks, node, chunk)) {
prefix2str(chunk, buf, sizeof(buf));
json_array_string_add(json_chunks, buf);
}

/* collect all function to json array*/
for (ALL_LIST_ELEMENTS_RO(bgp->srv6_functions, node, func)) {
json_function = json_object_new_object();
json_object_array_add(json_functions, json_function);
inet_ntop(AF_INET6, &func->sid, buf, sizeof(buf));
json_object_string_add(json_function, "Sid", buf);
json_object_string_add(json_function, "locator",
func->locator_name);
}

/* collect all bgp instance to json array*/
for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp)) {
json_bgp = json_object_new_object();
json_object_array_add(json_bgps, json_bgp);
json_object_string_add(json_bgp, "name",
bgp->name ? bgp->name : "default");

json_vpn_policy = json_object_new_object();
json_vpn_policy_ip = json_object_new_object();
json_vpn_policy_ip6 = json_object_new_object();
json_object_object_add(json_bgp, "vpnPolicy", json_vpn_policy);
json_object_object_add(json_vpn_policy, "ip", json_vpn_policy_ip);
json_object_object_add(json_vpn_policy, "ip6", json_vpn_policy_ip6);

tovpn4_sid = bgp->vpn_policy[AFI_IP].tovpn_sid;
tovpn6_sid = bgp->vpn_policy[AFI_IP6].tovpn_sid;
if (tovpn4_sid)
inet_ntop(AF_INET6, tovpn4_sid, buf_tovpn4_sid,
sizeof(buf_tovpn4_sid));
if (tovpn6_sid)
inet_ntop(AF_INET6, tovpn6_sid, buf_tovpn6_sid,
sizeof(buf_tovpn6_sid));

json_object_string_add(json_vpn_policy_ip, "toVpnSid",
tovpn4_sid ? buf_tovpn4_sid : "none");
json_object_string_add(json_vpn_policy_ip6, "toVpnSid",
tovpn6_sid ? buf_tovpn6_sid : "none");
}

vty_out(vty, "%s\n", json_object_to_json_string_ext(
json, JSON_C_TO_STRING_PRETTY));
json_object_free(json);
} else {
vty_out(vty, "locator_name: %s\n", bgp->srv6_locator_name);
vty_out(vty, "locator_chunks:\n");
for (ALL_LIST_ELEMENTS_RO(bgp->srv6_locator_chunks, node, chunk)) {
prefix2str(chunk, buf, sizeof(buf));
vty_out(vty, "- %s\n", buf);
}

vty_out(vty, "functions:\n");
for (ALL_LIST_ELEMENTS_RO(bgp->srv6_functions, node, func)) {
inet_ntop(AF_INET6, &func->sid, buf, sizeof(buf));
vty_out(vty, "- sid: %s\n", buf);
vty_out(vty, " locator: %s\n", func->locator_name);
}

vty_out(vty, "bgps:\n");
for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp)) {
vty_out(vty, "- name: %s\n",
bgp->name ? bgp->name : "default");

tovpn4_sid = bgp->vpn_policy[AFI_IP].tovpn_sid;
tovpn6_sid = bgp->vpn_policy[AFI_IP6].tovpn_sid;
if (tovpn4_sid)
inet_ntop(AF_INET6, tovpn4_sid, buf_tovpn4_sid,
sizeof(buf_tovpn4_sid));
if (tovpn6_sid)
inet_ntop(AF_INET6, tovpn6_sid, buf_tovpn6_sid,
sizeof(buf_tovpn6_sid));

vty_out(vty, " vpn_policy[AFI_IP].tovpn_sid: %s\n",
tovpn4_sid ? buf_tovpn4_sid : "none");
vty_out(vty, " vpn_policy[AFI_IP6].tovpn_sid: %s\n",
tovpn6_sid ? buf_tovpn6_sid : "none");
}
}

return CMD_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"locatorName":"",
"locatorChunks":[
],
"functions":[
],
"bgps":[
{
"name":"default",
"vpnPolicy":{
"ip":{
"toVpnSid":"none"
},
"ip6":{
"toVpnSid":"none"
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"locatorName":"",
"locatorChunks":[
],
"functions":[
],
"bgps":[
{
"name":"default",
"vpnPolicy":{
"ip":{
"toVpnSid":"none"
},
"ip6":{
"toVpnSid":"none"
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"locatorName":"loc1",
"locatorChunks":[
"2001:db8:1:1:1::/68"
],
"functions":[
],
"bgps":[
{
"name":"default",
"vpnPolicy":{
"ip":{
"toVpnSid":"none"
},
"ip6":{
"toVpnSid":"none"
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"locatorName":"loc1",
"locatorChunks":[
"2001:db8:1:1:1::/68"
],
"functions":[
],
"bgps":[
{
"name":"default",
"vpnPolicy":{
"ip":{
"toVpnSid":"none"
},
"ip6":{
"toVpnSid":"none"
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"locatorName":"",
"locatorChunks":[
],
"functions":[
],
"bgps":[
{
"name":"default",
"vpnPolicy":{
"ip":{
"toVpnSid":"none"
},
"ip6":{
"toVpnSid":"none"
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"locatorName":"",
"locatorChunks":[
],
"functions":[
],
"bgps":[
{
"name":"default",
"vpnPolicy":{
"ip":{
"toVpnSid":"none"
},
"ip6":{
"toVpnSid":"none"
}
}
}
]
}
22 changes: 7 additions & 15 deletions tests/topotests/srv6_locator_chunks/test_srv6_locator_chunks.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,11 @@ def _check_sharpd_chunk(router, expected_chunk_file):
expected = open_json_file("{}/{}".format(CWD, expected_chunk_file))
return topotest.json_cmp(output, expected)

"""
TODO(drumato):
Currently bgpd doesn't support the json output
with `show bgp segment-routing srv6`.
def _check_bgpd_chunk(router, expected_chunk_file):
logger.info("checking bgpd locator chunk status")
output = json.loads(router.vtysh_cmd("show bgp segment-routing srv6"))
output = json.loads(router.vtysh_cmd("show bgp segment-routing srv6 json"))
expected = open_json_file("{}/{}".format(CWD, expected_chunk_file))
return topotest.json_cmp(output, expected)
"""

def check_srv6_locator(router, expected_file):
func = functools.partial(_check_srv6_locator, router, expected_file)
Expand All @@ -143,12 +137,10 @@ def check_sharpd_chunk(router, expected_file):
success, result = topotest.run_and_expect(func, None, count=5, wait=0.5)
assert result is None, "Failed"

"""
def check_bgpd_chunk(router, expected_file):
func = functools.partial(_check_bgpd_chunk, router, expected_file)
success, result = topotest.run_and_expect(func, None, count=5, wait=0.5)
assert result is None, "Failed"
"""

# FOR DEVELOPER:
# If you want to stop some specific line and start interactive shell,
Expand All @@ -157,38 +149,38 @@ def check_bgpd_chunk(router, expected_file):
logger.info("Test STEP1: locator configuration")
check_srv6_locator(router, "step1/expected_locators.json")
check_sharpd_chunk(router, "step1/expected_sharpd_chunks.json")
# check_bgpd_chunk(router, "step1/expected_bgpd_chunks.json")
check_bgpd_chunk(router, "step1/expected_bgpd_chunks.json")

logger.info("Test STEP2: get locator chunk for locator loc1 from sharpd")
get_locator_chunk_from_sharpd(router, "loc1")
check_srv6_locator(router, "step2/expected_locators.json")
check_sharpd_chunk(router, "step2/expected_sharpd_chunks.json")
# check_bgpd_chunk(router, "step2/expected_bgpd_chunks.json")
check_bgpd_chunk(router, "step2/expected_bgpd_chunks.json")

logger.info("Test STEP3: get locator chunk for locator loc1 from bgpd")
get_locator_chunk_from_bgpd(router, "loc1")
check_srv6_locator(router, "step3/expected_locators.json")
check_sharpd_chunk(router, "step3/expected_sharpd_chunks.json")
# check_bgpd_chunk(router, "step3/expected_bgpd_chunks.json")
check_bgpd_chunk(router, "step3/expected_bgpd_chunks.json")

logger.info("Test STEP4: release locator chunk loc1 by sharpd")
release_locator_chunk_from_sharpd(router, "loc1")
check_srv6_locator(router, "step4/expected_locators.json")
check_sharpd_chunk(router, "step4/expected_sharpd_chunks.json")
# check_bgpd_chunk(router, "step4/expected_bgpd_chunks.json")
check_bgpd_chunk(router, "step4/expected_bgpd_chunks.json")

logger.info("Test STEP5: release locator chunk loc1 by bgpd")
get_locator_chunk_from_sharpd(router, "loc1")
release_locator_chunk_from_bgpd(router, "loc1")
check_srv6_locator(router, "step5/expected_locators.json")
check_sharpd_chunk(router, "step5/expected_sharpd_chunks.json")
# check_bgpd_chunk(router, "step5/expected_bgpd_chunks.json")
check_bgpd_chunk(router, "step5/expected_bgpd_chunks.json")

logger.info("Test STEP6: release all chunk")
release_locator_chunk_from_sharpd(router, "loc1")
check_srv6_locator(router, "step6/expected_locators.json")
check_sharpd_chunk(router, "step6/expected_sharpd_chunks.json")
# check_bgpd_chunk(router, "step6/expected_bgpd_chunks.json")
check_bgpd_chunk(router, "step6/expected_bgpd_chunks.json")

if __name__ == "__main__":
args = ["-s"] + sys.argv[1:]
Expand Down

0 comments on commit ae1ac7f

Please sign in to comment.