From ae076fc276a22b2a5331b066964901e65034e73c Mon Sep 17 00:00:00 2001 From: Mark Stapp Date: Fri, 3 Apr 2020 15:00:13 -0400 Subject: [PATCH] lib: add zapi_nexthop2str() Add a handy 2str api for zapi nexthop debugging. Signed-off-by: Mark Stapp --- lib/zclient.c | 33 +++++++++++++++++++++++++++++++++ lib/zclient.h | 2 ++ 2 files changed, 35 insertions(+) diff --git a/lib/zclient.c b/lib/zclient.c index fe0cd3ce27..02532e7069 100644 --- a/lib/zclient.c +++ b/lib/zclient.c @@ -1542,6 +1542,39 @@ int zapi_backup_nexthop_from_nexthop(struct zapi_nexthop *znh, return ret; } +/* + * Format some info about a zapi nexthop, for debug or logging. + */ +const char *zapi_nexthop2str(const struct zapi_nexthop *znh, char *buf, + int bufsize) +{ + char tmp[INET6_ADDRSTRLEN]; + + switch (znh->type) { + case NEXTHOP_TYPE_IFINDEX: + snprintf(buf, bufsize, "if %u", znh->ifindex); + break; + case NEXTHOP_TYPE_IPV4: + case NEXTHOP_TYPE_IPV4_IFINDEX: + inet_ntop(AF_INET, &znh->gate.ipv4, tmp, sizeof(tmp)); + snprintf(buf, bufsize, "%s if %u", tmp, znh->ifindex); + break; + case NEXTHOP_TYPE_IPV6: + case NEXTHOP_TYPE_IPV6_IFINDEX: + inet_ntop(AF_INET6, &znh->gate.ipv6, tmp, sizeof(tmp)); + snprintf(buf, bufsize, "%s if %u", tmp, znh->ifindex); + break; + case NEXTHOP_TYPE_BLACKHOLE: + snprintf(buf, bufsize, "blackhole"); + break; + default: + snprintf(buf, bufsize, "unknown"); + break; + } + + return buf; +} + /* * Decode the nexthop-tracking update message */ diff --git a/lib/zclient.h b/lib/zclient.h index 92ac017e80..5b3b4635a4 100644 --- a/lib/zclient.h +++ b/lib/zclient.h @@ -804,6 +804,8 @@ int zapi_backup_nexthop_from_nexthop(struct zapi_nexthop *znh, const struct nexthop *nh); extern bool zapi_nexthop_update_decode(struct stream *s, struct zapi_route *nhr); +const char *zapi_nexthop2str(const struct zapi_nexthop *znh, char *buf, + int bufsize); /* Decode the zebra error message */ extern bool zapi_error_decode(struct stream *s, enum zebra_error_types *error); -- 2.39.5