From c6a5994609deec62c8aefa1fa15c517e32575ca3 Mon Sep 17 00:00:00 2001
From: Donatas Abraitis <donatas.abraitis@gmail.com>
Date: Wed, 6 May 2020 17:45:31 +0300
Subject: [PATCH 1/4] tests: Remove bgp_show_ip_bgp_fqdn test

Not really relevant for now.

Signed-off-by: Donatas Abraitis <donatas.abraitis@gmail.com>
---
 .../bgp_show_ip_bgp_fqdn/__init__.py          |   0
 .../bgp_show_ip_bgp_fqdn/r1/bgpd.conf         |   5 -
 .../bgp_show_ip_bgp_fqdn/r1/zebra.conf        |   9 --
 .../bgp_show_ip_bgp_fqdn/r2/bgpd.conf         |   5 -
 .../bgp_show_ip_bgp_fqdn/r2/zebra.conf        |  12 --
 .../bgp_show_ip_bgp_fqdn/r3/bgpd.conf         |   3 -
 .../bgp_show_ip_bgp_fqdn/r3/zebra.conf        |   6 -
 .../test_bgp_show_ip_bgp_fqdn.py              | 133 ------------------
 8 files changed, 173 deletions(-)
 delete mode 100644 tests/topotests/bgp_show_ip_bgp_fqdn/__init__.py
 delete mode 100644 tests/topotests/bgp_show_ip_bgp_fqdn/r1/bgpd.conf
 delete mode 100644 tests/topotests/bgp_show_ip_bgp_fqdn/r1/zebra.conf
 delete mode 100644 tests/topotests/bgp_show_ip_bgp_fqdn/r2/bgpd.conf
 delete mode 100644 tests/topotests/bgp_show_ip_bgp_fqdn/r2/zebra.conf
 delete mode 100644 tests/topotests/bgp_show_ip_bgp_fqdn/r3/bgpd.conf
 delete mode 100644 tests/topotests/bgp_show_ip_bgp_fqdn/r3/zebra.conf
 delete mode 100644 tests/topotests/bgp_show_ip_bgp_fqdn/test_bgp_show_ip_bgp_fqdn.py

diff --git a/tests/topotests/bgp_show_ip_bgp_fqdn/__init__.py b/tests/topotests/bgp_show_ip_bgp_fqdn/__init__.py
deleted file mode 100644
index e69de29bb2..0000000000
diff --git a/tests/topotests/bgp_show_ip_bgp_fqdn/r1/bgpd.conf b/tests/topotests/bgp_show_ip_bgp_fqdn/r1/bgpd.conf
deleted file mode 100644
index f0df56e947..0000000000
--- a/tests/topotests/bgp_show_ip_bgp_fqdn/r1/bgpd.conf
+++ /dev/null
@@ -1,5 +0,0 @@
-router bgp 65000
-  no bgp ebgp-requires-policy
-  neighbor 192.168.255.2 remote-as 65001
-  address-family ipv4 unicast
-    redistribute connected
diff --git a/tests/topotests/bgp_show_ip_bgp_fqdn/r1/zebra.conf b/tests/topotests/bgp_show_ip_bgp_fqdn/r1/zebra.conf
deleted file mode 100644
index 0a283c06d5..0000000000
--- a/tests/topotests/bgp_show_ip_bgp_fqdn/r1/zebra.conf
+++ /dev/null
@@ -1,9 +0,0 @@
-!
-interface lo
- ip address 172.16.255.254/32
-!
-interface r1-eth0
- ip address 192.168.255.1/24
-!
-ip forwarding
-!
diff --git a/tests/topotests/bgp_show_ip_bgp_fqdn/r2/bgpd.conf b/tests/topotests/bgp_show_ip_bgp_fqdn/r2/bgpd.conf
deleted file mode 100644
index 422a7345f9..0000000000
--- a/tests/topotests/bgp_show_ip_bgp_fqdn/r2/bgpd.conf
+++ /dev/null
@@ -1,5 +0,0 @@
-router bgp 65001
-  no bgp ebgp-requires-policy
-  bgp default show-hostname
-  neighbor 192.168.255.1 remote-as 65000
-  neighbor 192.168.254.1 remote-as 65001
diff --git a/tests/topotests/bgp_show_ip_bgp_fqdn/r2/zebra.conf b/tests/topotests/bgp_show_ip_bgp_fqdn/r2/zebra.conf
deleted file mode 100644
index e9e2e4391f..0000000000
--- a/tests/topotests/bgp_show_ip_bgp_fqdn/r2/zebra.conf
+++ /dev/null
@@ -1,12 +0,0 @@
-!
-interface lo
- ip address 172.16.255.253/32
-!
-interface r2-eth0
- ip address 192.168.255.2/24
-!
-interface r2-eth1
- ip address 192.168.254.2/24
-!
-ip forwarding
-!
diff --git a/tests/topotests/bgp_show_ip_bgp_fqdn/r3/bgpd.conf b/tests/topotests/bgp_show_ip_bgp_fqdn/r3/bgpd.conf
deleted file mode 100644
index 8fcf6a736d..0000000000
--- a/tests/topotests/bgp_show_ip_bgp_fqdn/r3/bgpd.conf
+++ /dev/null
@@ -1,3 +0,0 @@
-router bgp 65001
-  bgp default show-hostname
-  neighbor 192.168.254.2 remote-as 65001
diff --git a/tests/topotests/bgp_show_ip_bgp_fqdn/r3/zebra.conf b/tests/topotests/bgp_show_ip_bgp_fqdn/r3/zebra.conf
deleted file mode 100644
index a8b8bc38c5..0000000000
--- a/tests/topotests/bgp_show_ip_bgp_fqdn/r3/zebra.conf
+++ /dev/null
@@ -1,6 +0,0 @@
-!
-interface r3-eth0
- ip address 192.168.254.1/24
-!
-ip forwarding
-!
diff --git a/tests/topotests/bgp_show_ip_bgp_fqdn/test_bgp_show_ip_bgp_fqdn.py b/tests/topotests/bgp_show_ip_bgp_fqdn/test_bgp_show_ip_bgp_fqdn.py
deleted file mode 100644
index e8ad180935..0000000000
--- a/tests/topotests/bgp_show_ip_bgp_fqdn/test_bgp_show_ip_bgp_fqdn.py
+++ /dev/null
@@ -1,133 +0,0 @@
-#!/usr/bin/env python
-
-#
-# test_bgp_show_ip_bgp_fqdn.py
-# Part of NetDEF Topology Tests
-#
-# Copyright (c) 2019 by
-# Donatas Abraitis <donatas.abraitis@gmail.com>
-#
-# Permission to use, copy, modify, and/or distribute this software
-# for any purpose with or without fee is hereby granted, provided
-# that the above copyright notice and this permission notice appear
-# in all copies.
-#
-# THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
-# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
-# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
-# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
-# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
-# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
-# OF THIS SOFTWARE.
-#
-
-"""
-test_bgp_show_ip_bgp_fqdn.py:
-Test if FQND is visible in `show [ip] bgp` output if
-`bgp default show-hostname` is toggled.
-
-Topology:
-r1 <-- eBGP --> r2 <-- iBGP --> r3
-
-1. Check if both hostname and ip are added to JSON output
-for 172.16.255.254/32 on r2.
-2. Check if only ip is added to JSON output for 172.16.255.254/32 on r3.
-"""
-
-import os
-import sys
-import json
-import time
-import pytest
-import functools
-
-CWD = os.path.dirname(os.path.realpath(__file__))
-sys.path.append(os.path.join(CWD, "../"))
-
-# pylint: disable=C0413
-from lib import topotest
-from lib.topogen import Topogen, TopoRouter, get_topogen
-from lib.topolog import logger
-from mininet.topo import Topo
-
-
-class TemplateTopo(Topo):
-    def build(self, *_args, **_opts):
-        tgen = get_topogen(self)
-
-        for routern in range(1, 4):
-            tgen.add_router("r{}".format(routern))
-
-        switch = tgen.add_switch("s1")
-        switch.add_link(tgen.gears["r1"])
-        switch.add_link(tgen.gears["r2"])
-
-        switch = tgen.add_switch("s2")
-        switch.add_link(tgen.gears["r2"])
-        switch.add_link(tgen.gears["r3"])
-
-
-def setup_module(mod):
-    tgen = Topogen(TemplateTopo, mod.__name__)
-    tgen.start_topology()
-
-    router_list = tgen.routers()
-
-    for i, (rname, router) in enumerate(router_list.iteritems(), 1):
-        router.load_config(
-            TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
-        )
-        router.load_config(
-            TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname))
-        )
-
-    tgen.start_router()
-
-
-def teardown_module(mod):
-    tgen = get_topogen()
-    tgen.stop_topology()
-
-
-def test_bgp_show_ip_bgp_hostname():
-    tgen = get_topogen()
-
-    if tgen.routers_have_failure():
-        pytest.skip(tgen.errors)
-
-    def _bgp_converge(router):
-        output = json.loads(router.vtysh_cmd("show ip bgp 172.16.255.254/32 json"))
-        expected = {"prefix": "172.16.255.254/32"}
-        return topotest.json_cmp(output, expected)
-
-    def _bgp_show_nexthop_hostname_and_ip(router):
-        output = json.loads(router.vtysh_cmd("show ip bgp json"))
-        for nh in output["routes"]["172.16.255.254/32"][0]["nexthops"]:
-            if "hostname" in nh and "ip" in nh:
-                return True
-        return False
-
-    def _bgp_show_nexthop_ip_only(router):
-        output = json.loads(router.vtysh_cmd("show ip bgp json"))
-        for nh in output["routes"]["172.16.255.254/32"][0]["nexthops"]:
-            if "ip" in nh and not "hostname" in nh:
-                return True
-        return False
-
-    test_func = functools.partial(_bgp_converge, tgen.gears["r2"])
-    success, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
-
-    test_func = functools.partial(_bgp_converge, tgen.gears["r3"])
-    success, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
-
-    assert result is None, 'Failed bgp convergence in "{}"'.format(tgen.gears["r2"])
-    assert _bgp_show_nexthop_hostname_and_ip(tgen.gears["r2"]) == True
-
-    assert result is None, 'Failed bgp convergence in "{}"'.format(tgen.gears["r3"])
-    assert _bgp_show_nexthop_ip_only(tgen.gears["r3"]) == True
-
-
-if __name__ == "__main__":
-    args = ["-s"] + sys.argv[1:]
-    sys.exit(pytest.main(args))

From e7cc3d21452bd771a97bc46ab5a1e4853c46f944 Mon Sep 17 00:00:00 2001
From: Donatas Abraitis <donatas.abraitis@gmail.com>
Date: Wed, 6 May 2020 17:46:10 +0300
Subject: [PATCH 2/4] bgpd: Show the real next-hop address in addition to
 hostname in `show bgp`

It's hard to cope with cases when next-hop is changed/unchanged or
peers are non-direct.

It would be better to show the hostname and nexthop IP address (both)
under `show bgp` to quickly identify the source and the real next-hop
of the route.

If `bgp default show-nexthop-hostname` is toggled the output looks like:
```
spine1-debian-9# show bgp
BGP table version is 1, local router ID is 2.2.2.2, vrf id 0
Default local pref 100, local AS 65002
Status codes:  s suppressed, d damped, h history, * valid, > best, = multipath,
               i internal, r RIB-failure, S Stale, R Removed
Nexthop codes: @NNN nexthop's vrf id, < announce-nh-self
Origin codes:  i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*  2a02:4780::/64   fe80::a00:27ff:fe09:f8a3(exit1-debian-9)
                                             0             0 65001 ?

spine1-debian-9# show ip bgp
BGP table version is 5, local router ID is 2.2.2.2, vrf id 0
Default local pref 100, local AS 65002
Status codes:  s suppressed, d damped, h history, * valid, > best, = multipath,
               i internal, r RIB-failure, S Stale, R Removed
Nexthop codes: @NNN nexthop's vrf id, < announce-nh-self
Origin codes:  i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*> 10.255.255.0/24  192.168.0.1(exit1-debian-9)
                                             0             0 65001 ?
```

Signed-off-by: Donatas Abraitis <donatas.abraitis@gmail.com>
---
 bgpd/bgp_route.c | 161 ++++++++++++++++++++++++++++++-----------------
 bgpd/bgp_vty.c   |  45 +++++++++++++
 bgpd/bgpd.h      |   1 +
 3 files changed, 149 insertions(+), 58 deletions(-)

diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c
index 7bfefde482..f033f525e5 100644
--- a/bgpd/bgp_route.c
+++ b/bgpd/bgp_route.c
@@ -7559,8 +7559,7 @@ static char *bgp_nexthop_hostname(struct peer *peer,
 				  struct bgp_nexthop_cache *bnc)
 {
 	if (peer->hostname
-	    && CHECK_FLAG(peer->bgp->flags, BGP_FLAG_SHOW_HOSTNAME) && bnc
-	    && CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED))
+	    && CHECK_FLAG(peer->bgp->flags, BGP_FLAG_SHOW_NEXTHOP_HOSTNAME))
 		return peer->hostname;
 	return NULL;
 }
@@ -7570,6 +7569,7 @@ void route_vty_out(struct vty *vty, const struct prefix *p,
 		   struct bgp_path_info *path, int display, safi_t safi,
 		   json_object *json_paths)
 {
+	int len;
 	struct attr *attr = path->attr;
 	json_object *json_path = NULL;
 	json_object *json_nexthops = NULL;
@@ -7681,10 +7681,19 @@ void route_vty_out(struct vty *vty, const struct prefix *p,
 							       : "ipv6");
 			json_object_boolean_true_add(json_nexthop_global,
 						     "used");
-		} else
-			vty_out(vty, "%s%s",
-				nexthop_hostname ? nexthop_hostname : nexthop,
-				vrf_id_str);
+		} else {
+			if (nexthop_hostname)
+				len = vty_out(vty, "%s(%s)%s", nexthop,
+					      nexthop_hostname, vrf_id_str);
+			else
+				len = vty_out(vty, "%s%s", nexthop, vrf_id_str);
+
+			len = 16 - len;
+			if (len < 1)
+				vty_out(vty, "\n%*s", 36, " ");
+			else
+				vty_out(vty, "%*s", len, " ");
+		}
 	} else if (safi == SAFI_EVPN) {
 		if (json_paths) {
 			json_nexthop_global = json_object_new_object();
@@ -7701,11 +7710,20 @@ void route_vty_out(struct vty *vty, const struct prefix *p,
 					       "ipv4");
 			json_object_boolean_true_add(json_nexthop_global,
 						     "used");
-		} else
-			vty_out(vty, "%-16s%s",
-				nexthop_hostname ? nexthop_hostname
-						 : inet_ntoa(attr->nexthop),
-				vrf_id_str);
+		} else {
+			if (nexthop_hostname)
+				len = vty_out(vty, "%pI4(%s)%s", &attr->nexthop,
+					      nexthop_hostname, vrf_id_str);
+			else
+				len = vty_out(vty, "%pI4%s", &attr->nexthop,
+					      vrf_id_str);
+
+			len = 16 - len;
+			if (len < 1)
+				vty_out(vty, "\n%*s", 36, " ");
+			else
+				vty_out(vty, "%*s", len, " ");
+		}
 	} else if (safi == SAFI_FLOWSPEC) {
 		if (attr->nexthop.s_addr != INADDR_ANY) {
 			if (json_paths) {
@@ -7726,10 +7744,21 @@ void route_vty_out(struct vty *vty, const struct prefix *p,
 							json_nexthop_global,
 							     "used");
 			} else {
-				vty_out(vty, "%-16s",
-					nexthop_hostname
-						? nexthop_hostname
-						: inet_ntoa(attr->nexthop));
+				if (nexthop_hostname)
+					len = vty_out(vty, "%pI4(%s)%s",
+						      &attr->nexthop,
+						      nexthop_hostname,
+						      vrf_id_str);
+				else
+					len = vty_out(vty, "%pI4%s",
+						      &attr->nexthop,
+						      vrf_id_str);
+
+				len = 16 - len;
+				if (len < 1)
+					vty_out(vty, "\n%*s", 36, " ");
+				else
+					vty_out(vty, "%*s", len, " ");
 			}
 		}
 	} else if (p->family == AF_INET && !BGP_ATTR_NEXTHOP_AFI_IP6(attr)) {
@@ -7749,19 +7778,23 @@ void route_vty_out(struct vty *vty, const struct prefix *p,
 			json_object_boolean_true_add(json_nexthop_global,
 						     "used");
 		} else {
-			char buf[BUFSIZ];
+			if (nexthop_hostname)
+				len = vty_out(vty, "%pI4(%s)%s", &attr->nexthop,
+					      nexthop_hostname, vrf_id_str);
+			else
+				len = vty_out(vty, "%pI4%s", &attr->nexthop,
+					      vrf_id_str);
 
-			snprintf(buf, sizeof(buf), "%s%s",
-				 nexthop_hostname ? nexthop_hostname
-						  : inet_ntoa(attr->nexthop),
-				 vrf_id_str);
-			vty_out(vty, "%-16s", buf);
+			len = 16 - len;
+			if (len < 1)
+				vty_out(vty, "\n%*s", 36, " ");
+			else
+				vty_out(vty, "%*s", len, " ");
 		}
 	}
 
 	/* IPv6 Next Hop */
 	else if (p->family == AF_INET6 || BGP_ATTR_NEXTHOP_AFI_IP6(attr)) {
-		int len;
 		char buf[BUFSIZ];
 
 		if (json_paths) {
@@ -7835,15 +7868,18 @@ void route_vty_out(struct vty *vty, const struct prefix *p,
 					else
 						vty_out(vty, "%*s", len, " ");
 				} else {
-					len = vty_out(
-						vty, "%s%s",
-						nexthop_hostname
-							? nexthop_hostname
-							: inet_ntop(
-								  AF_INET6,
-								  &attr->mp_nexthop_local,
-								  buf, BUFSIZ),
-						vrf_id_str);
+					if (nexthop_hostname)
+						len = vty_out(
+							vty, "%pI6(%s)%s",
+							&attr->mp_nexthop_local,
+							nexthop_hostname,
+							vrf_id_str);
+					else
+						len = vty_out(
+							vty, "%pI6%s",
+							&attr->mp_nexthop_local,
+							vrf_id_str);
+
 					len = 16 - len;
 
 					if (len < 1)
@@ -7852,15 +7888,16 @@ void route_vty_out(struct vty *vty, const struct prefix *p,
 						vty_out(vty, "%*s", len, " ");
 				}
 			} else {
-				len = vty_out(
-					vty, "%s%s",
-					nexthop_hostname
-						? nexthop_hostname
-						: inet_ntop(
-							  AF_INET6,
-							  &attr->mp_nexthop_global,
-							  buf, BUFSIZ),
-					vrf_id_str);
+				if (nexthop_hostname)
+					len = vty_out(vty, "%pI6(%s)%s",
+						      &attr->mp_nexthop_global,
+						      nexthop_hostname,
+						      vrf_id_str);
+				else
+					len = vty_out(vty, "%pI6%s",
+						      &attr->mp_nexthop_global,
+						      vrf_id_str);
+
 				len = 16 - len;
 
 				if (len < 1)
@@ -7986,6 +8023,7 @@ void route_vty_out_tmp(struct vty *vty, const struct prefix *p,
 {
 	json_object *json_status = NULL;
 	json_object *json_net = NULL;
+	int len;
 	char buff[BUFSIZ];
 
 	/* Route status display. */
@@ -8079,7 +8117,6 @@ void route_vty_out_tmp(struct vty *vty, const struct prefix *p,
 						inet_ntoa(attr->nexthop));
 			} else if (p->family == AF_INET6
 				   || BGP_ATTR_NEXTHOP_AFI_IP6(attr)) {
-				int len;
 				char buf[BUFSIZ];
 
 				len = vty_out(
@@ -8823,12 +8860,15 @@ void route_vty_out_detail(struct vty *vty, struct bgp *bgp,
 					json_object_string_add(
 						json_nexthop_global, "hostname",
 						nexthop_hostname);
-			} else
-				vty_out(vty, "    %s",
-					nexthop_hostname
-						? nexthop_hostname
-						: inet_ntoa(
-							  attr->mp_nexthop_global_in));
+			} else {
+				if (nexthop_hostname)
+					vty_out(vty, "    %pI4(%s)",
+						&attr->mp_nexthop_global_in,
+						nexthop_hostname);
+				else
+					vty_out(vty, "    %pI4",
+						&attr->mp_nexthop_global_in);
+			}
 		} else {
 			if (json_paths) {
 				json_object_string_add(
@@ -8839,11 +8879,15 @@ void route_vty_out_detail(struct vty *vty, struct bgp *bgp,
 					json_object_string_add(
 						json_nexthop_global, "hostname",
 						nexthop_hostname);
-			} else
-				vty_out(vty, "    %s",
-					nexthop_hostname
-						? nexthop_hostname
-						: inet_ntoa(attr->nexthop));
+			} else {
+				if (nexthop_hostname)
+					vty_out(vty, "    %pI4(%s)",
+						&attr->nexthop,
+						nexthop_hostname);
+				else
+					vty_out(vty, "    %pI4",
+						&attr->nexthop);
+			}
 		}
 
 		if (json_paths)
@@ -8866,12 +8910,13 @@ void route_vty_out_detail(struct vty *vty, struct bgp *bgp,
 			json_object_string_add(json_nexthop_global, "scope",
 					       "global");
 		} else {
-			vty_out(vty, "    %s",
-				nexthop_hostname
-					? nexthop_hostname
-					: inet_ntop(AF_INET6,
-						    &attr->mp_nexthop_global,
-						    buf, INET6_ADDRSTRLEN));
+			if (nexthop_hostname)
+				vty_out(vty, "    %pI6(%s)",
+					&attr->mp_nexthop_global,
+					nexthop_hostname);
+			else
+				vty_out(vty, "    %pI6",
+					&attr->mp_nexthop_global);
 		}
 	}
 
diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c
index 78521457fd..7f00ff3fbe 100644
--- a/bgpd/bgp_vty.c
+++ b/bgpd/bgp_vty.c
@@ -84,6 +84,10 @@ FRR_CFG_DEFAULT_BOOL(BGP_SHOW_HOSTNAME,
 	{ .val_bool = true, .match_profile = "datacenter", },
 	{ .val_bool = false },
 )
+FRR_CFG_DEFAULT_BOOL(BGP_SHOW_NEXTHOP_HOSTNAME,
+	{ .val_bool = true, .match_profile = "datacenter", },
+	{ .val_bool = false },
+)
 FRR_CFG_DEFAULT_BOOL(BGP_LOG_NEIGHBOR_CHANGES,
 	{ .val_bool = true, .match_profile = "datacenter", },
 	{ .val_bool = false },
@@ -422,6 +426,8 @@ int bgp_get_vty(struct bgp **bgp, as_t *as, const char *name,
 			SET_FLAG((*bgp)->flags, BGP_FLAG_IMPORT_CHECK);
 		if (DFLT_BGP_SHOW_HOSTNAME)
 			SET_FLAG((*bgp)->flags, BGP_FLAG_SHOW_HOSTNAME);
+		if (DFLT_BGP_SHOW_NEXTHOP_HOSTNAME)
+			SET_FLAG((*bgp)->flags, BGP_FLAG_SHOW_NEXTHOP_HOSTNAME);
 		if (DFLT_BGP_LOG_NEIGHBOR_CHANGES)
 			SET_FLAG((*bgp)->flags, BGP_FLAG_LOG_NEIGHBOR_CHANGES);
 		if (DFLT_BGP_DETERMINISTIC_MED)
@@ -3100,6 +3106,32 @@ DEFUN (no_bgp_default_show_hostname,
 	return CMD_SUCCESS;
 }
 
+/* Display hostname in certain command outputs */
+DEFUN (bgp_default_show_nexthop_hostname,
+       bgp_default_show_nexthop_hostname_cmd,
+       "bgp default show-nexthop-hostname",
+       "BGP specific commands\n"
+       "Configure BGP defaults\n"
+       "Show hostname for nexthop in certain command outputs\n")
+{
+	VTY_DECLVAR_CONTEXT(bgp, bgp);
+	SET_FLAG(bgp->flags, BGP_FLAG_SHOW_NEXTHOP_HOSTNAME);
+	return CMD_SUCCESS;
+}
+
+DEFUN (no_bgp_default_show_nexthop_hostname,
+       no_bgp_default_show_nexthop_hostname_cmd,
+       "no bgp default show-nexthop-hostname",
+       NO_STR
+       "BGP specific commands\n"
+       "Configure BGP defaults\n"
+       "Show hostname for nexthop in certain command outputs\n")
+{
+	VTY_DECLVAR_CONTEXT(bgp, bgp);
+	UNSET_FLAG(bgp->flags, BGP_FLAG_SHOW_NEXTHOP_HOSTNAME);
+	return CMD_SUCCESS;
+}
+
 /* "bgp network import-check" configuration.  */
 DEFUN (bgp_network_import_check,
        bgp_network_import_check_cmd,
@@ -15190,6 +15222,15 @@ int bgp_config_write(struct vty *vty)
 					? ""
 					: "no ");
 
+		/* BGP default show-nexthop-hostname */
+		if (!!CHECK_FLAG(bgp->flags, BGP_FLAG_SHOW_NEXTHOP_HOSTNAME)
+		    != SAVE_BGP_SHOW_HOSTNAME)
+			vty_out(vty, " %sbgp default show-nexthop-hostname\n",
+				CHECK_FLAG(bgp->flags,
+					   BGP_FLAG_SHOW_NEXTHOP_HOSTNAME)
+					? ""
+					: "no ");
+
 		/* BGP default subgroup-pkt-queue-max. */
 		if (bgp->default_subgroup_pkt_queue_max
 		    != BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX)
@@ -15815,6 +15856,10 @@ void bgp_vty_init(void)
 	install_element(BGP_NODE, &bgp_default_show_hostname_cmd);
 	install_element(BGP_NODE, &no_bgp_default_show_hostname_cmd);
 
+	/* bgp default show-nexthop-hostname */
+	install_element(BGP_NODE, &bgp_default_show_nexthop_hostname_cmd);
+	install_element(BGP_NODE, &no_bgp_default_show_nexthop_hostname_cmd);
+
 	/* "bgp default subgroup-pkt-queue-max" commands. */
 	install_element(BGP_NODE, &bgp_default_subgroup_pkt_queue_max_cmd);
 	install_element(BGP_NODE, &no_bgp_default_subgroup_pkt_queue_max_cmd);
diff --git a/bgpd/bgpd.h b/bgpd/bgpd.h
index 4a5772a53b..4efc068dea 100644
--- a/bgpd/bgpd.h
+++ b/bgpd/bgpd.h
@@ -447,6 +447,7 @@ struct bgp {
 #define BGP_FLAG_SELECT_DEFER_DISABLE     (1 << 23)
 #define BGP_FLAG_GR_DISABLE_EOR           (1 << 24)
 #define BGP_FLAG_EBGP_REQUIRES_POLICY (1 << 25)
+#define BGP_FLAG_SHOW_NEXTHOP_HOSTNAME (1 << 26)
 
 	enum global_mode GLOBAL_GR_FSM[BGP_GLOBAL_GR_MODE]
 				      [BGP_GLOBAL_GR_EVENT_CMD];

From 104dfe5258cbeb0443fa4d6577794a1e5a5dafd3 Mon Sep 17 00:00:00 2001
From: Donatas Abraitis <donatas.abraitis@gmail.com>
Date: Wed, 6 May 2020 17:50:04 +0300
Subject: [PATCH 3/4] bgpd: Add "hostname" in JSON output for `show bgp` family
 outputs

This adds hostname regardless if `bgp default show-hostname` enabled or not.

Signed-off-by: Donatas Abraitis <donatas.abraitis@gmail.com>
---
 bgpd/bgp_route.c | 40 ++++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 20 deletions(-)

diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c
index f033f525e5..5f645fa871 100644
--- a/bgpd/bgp_route.c
+++ b/bgpd/bgp_route.c
@@ -7671,10 +7671,10 @@ void route_vty_out(struct vty *vty, const struct prefix *p,
 			json_object_string_add(json_nexthop_global, "ip",
 					       nexthop);
 
-			if (nexthop_hostname)
+			if (path->peer->hostname)
 				json_object_string_add(json_nexthop_global,
 						       "hostname",
-						       nexthop_hostname);
+						       path->peer->hostname);
 
 			json_object_string_add(json_nexthop_global, "afi",
 					       (af == AF_INET) ? "ipv4"
@@ -7701,10 +7701,10 @@ void route_vty_out(struct vty *vty, const struct prefix *p,
 			json_object_string_add(json_nexthop_global, "ip",
 					       inet_ntoa(attr->nexthop));
 
-			if (nexthop_hostname)
+			if (path->peer->hostname)
 				json_object_string_add(json_nexthop_global,
 						       "hostname",
-						       nexthop_hostname);
+						       path->peer->hostname);
 
 			json_object_string_add(json_nexthop_global, "afi",
 					       "ipv4");
@@ -7735,10 +7735,10 @@ void route_vty_out(struct vty *vty, const struct prefix *p,
 					json_nexthop_global, "ip",
 					inet_ntoa(attr->nexthop));
 
-				if (nexthop_hostname)
+				if (path->peer->hostname)
 					json_object_string_add(
 						json_nexthop_global, "hostname",
-						nexthop_hostname);
+						path->peer->hostname);
 
 				json_object_boolean_true_add(
 							json_nexthop_global,
@@ -7768,10 +7768,10 @@ void route_vty_out(struct vty *vty, const struct prefix *p,
 			json_object_string_add(json_nexthop_global, "ip",
 					       inet_ntoa(attr->nexthop));
 
-			if (nexthop_hostname)
+			if (path->peer->hostname)
 				json_object_string_add(json_nexthop_global,
 						       "hostname",
-						       nexthop_hostname);
+						       path->peer->hostname);
 
 			json_object_string_add(json_nexthop_global, "afi",
 					       "ipv4");
@@ -7804,10 +7804,10 @@ void route_vty_out(struct vty *vty, const struct prefix *p,
 				inet_ntop(AF_INET6, &attr->mp_nexthop_global,
 					  buf, BUFSIZ));
 
-			if (nexthop_hostname)
+			if (path->peer->hostname)
 				json_object_string_add(json_nexthop_global,
 						       "hostname",
-						       nexthop_hostname);
+						       path->peer->hostname);
 
 			json_object_string_add(json_nexthop_global, "afi",
 					       "ipv6");
@@ -7826,10 +7826,10 @@ void route_vty_out(struct vty *vty, const struct prefix *p,
 						  &attr->mp_nexthop_local, buf,
 						  BUFSIZ));
 
-				if (nexthop_hostname)
+				if (path->peer->hostname)
 					json_object_string_add(
 						json_nexthop_ll, "hostname",
-						nexthop_hostname);
+						path->peer->hostname);
 
 				json_object_string_add(json_nexthop_ll, "afi",
 						       "ipv6");
@@ -8856,10 +8856,10 @@ void route_vty_out_detail(struct vty *vty, struct bgp *bgp,
 					json_nexthop_global, "ip",
 					inet_ntoa(attr->mp_nexthop_global_in));
 
-				if (nexthop_hostname)
+				if (path->peer->hostname)
 					json_object_string_add(
 						json_nexthop_global, "hostname",
-						nexthop_hostname);
+						path->peer->hostname);
 			} else {
 				if (nexthop_hostname)
 					vty_out(vty, "    %pI4(%s)",
@@ -8875,10 +8875,10 @@ void route_vty_out_detail(struct vty *vty, struct bgp *bgp,
 					json_nexthop_global, "ip",
 					inet_ntoa(attr->nexthop));
 
-				if (nexthop_hostname)
+				if (path->peer->hostname)
 					json_object_string_add(
 						json_nexthop_global, "hostname",
-						nexthop_hostname);
+						path->peer->hostname);
 			} else {
 				if (nexthop_hostname)
 					vty_out(vty, "    %pI4(%s)",
@@ -8900,10 +8900,10 @@ void route_vty_out_detail(struct vty *vty, struct bgp *bgp,
 				inet_ntop(AF_INET6, &attr->mp_nexthop_global,
 					  buf, INET6_ADDRSTRLEN));
 
-			if (nexthop_hostname)
+			if (path->peer->hostname)
 				json_object_string_add(json_nexthop_global,
 						       "hostname",
-						       nexthop_hostname);
+						       path->peer->hostname);
 
 			json_object_string_add(json_nexthop_global, "afi",
 					       "ipv6");
@@ -9094,10 +9094,10 @@ void route_vty_out_detail(struct vty *vty, struct bgp *bgp,
 				inet_ntop(AF_INET6, &attr->mp_nexthop_local,
 					  buf, INET6_ADDRSTRLEN));
 
-			if (nexthop_hostname)
+			if (path->peer->hostname)
 				json_object_string_add(json_nexthop_ll,
 						       "hostname",
-						       nexthop_hostname);
+						       path->peer->hostname);
 
 			json_object_string_add(json_nexthop_ll, "afi", "ipv6");
 			json_object_string_add(json_nexthop_ll, "scope",

From 8df39282ea64e2a65a7910012627f78d080833b1 Mon Sep 17 00:00:00 2001
From: Donatas Abraitis <donatas.abraitis@gmail.com>
Date: Wed, 24 Jun 2020 17:26:27 +0300
Subject: [PATCH 4/4] doc: Add some words about `bgp default
 show-[nexthop]-hostname`

Signed-off-by: Donatas Abraitis <donatas.abraitis@gmail.com>
---
 doc/user/bgp.rst | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/doc/user/bgp.rst b/doc/user/bgp.rst
index 36227db604..a6c29724b0 100644
--- a/doc/user/bgp.rst
+++ b/doc/user/bgp.rst
@@ -1366,6 +1366,19 @@ Configuring Peers
    on by default or not.  This command defaults to on and is not displayed.
    The `no bgp default ipv4-unicast` form of the command is displayed.
 
+.. index:: [no] bgp default show-hostname
+.. clicmd:: [no] bgp default show-hostname
+
+   This command shows the hostname of the peer in certain BGP commands
+   outputs. It's easier to troubleshoot if you have a number of BGP peers.
+
+.. index:: [no] bgp default show-nexthop-hostname
+.. clicmd:: [no] bgp default show-nexthop-hostname
+
+   This command shows the hostname of the next-hop in certain BGP commands
+   outputs. It's easier to troubleshoot if you have a number of BGP peers
+   and a number of routes to check.
+
 .. index:: [no] neighbor PEER advertisement-interval (0-600)
 .. clicmd:: [no] neighbor PEER advertisement-interval (0-600)
 
