Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 28 additions & 31 deletions netsim/modules/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,38 +178,32 @@ def get_remote_ibgp_endpoint(n: Box) -> Box:
* Other nodes need IBGP sessions with all RRs in the same AS
"""
def build_ibgp_sessions(node: Box, sessions: Box, topology: Box) -> None:
rrlist = find_bgp_rr(node.bgp.get("as"),topology)
has_ibgp = False # Assume we have no IBGP sessions (yet)

# If we don't have route reflectors, or if the current node is a route
# reflector, we need BGP sessions to all other nodes in the same AS
if not(rrlist) or node.bgp.get("rr",None):
for name,n in topology.nodes.items():
if "bgp" in n:
if n.bgp.get("as") == node.bgp.get("as") and n.name != node.name:
n_intf = get_remote_ibgp_endpoint(n)
neighbor_data = bgp_neighbor(n,n_intf,'ibgp',sessions,get_neighbor_rr(n))
if not neighbor_data is None:
if 'loopback' in node:
neighbor_data._source_intf = node.loopback
node.bgp.neighbors.append(neighbor_data)
has_ibgp = True

#
# The node is not a route reflector, and we have a non-empty RR list
# We need BGP sessions with the route reflectors
node_as = node.bgp['as'] # Set up variables that will come handy, starting with node AS
is_rr = node.bgp.get("rr",None) # Is this node an RR?
bgp_nhs = node.bgp.get("next_hop_self",None) # Do we have to set next hop on IBGP sessions?
rrlist = [] if is_rr else find_bgp_rr(node_as,topology)

if is_rr or not rrlist: # If the current node is RR or we have a full mesh
ibgp_ngb_list = [ # ... we need IBGP sessions to all nodes in the AS
ngb for ngb in topology.nodes.values() # ... but while building the node list
if ngb.name != node.name and # ... skip current node
ngb.get('bgp.as',None) == node_as] # ... and everyone not in the current AS (or not running BGP)
else:
for n in rrlist:
if n.name != node.name:
n_intf = get_remote_ibgp_endpoint(n)
neighbor_data = bgp_neighbor(n,n_intf,'ibgp',sessions,get_neighbor_rr(n))
if not neighbor_data is None:
if 'loopback' in node:
neighbor_data._source_intf = node.loopback
node.bgp.neighbors.append(neighbor_data)
has_ibgp = True

if not has_ibgp:
ibgp_ngb_list = rrlist

for n in ibgp_ngb_list:
n_intf = get_remote_ibgp_endpoint(n)
neighbor_data = bgp_neighbor(n,n_intf,'ibgp',sessions,get_neighbor_rr(n))
if not neighbor_data is None:
if 'loopback' in node:
neighbor_data._source_intf = node.loopback
if bgp_nhs:
neighbor_data.next_hop_self = True
if is_rr and not 'rr' in neighbor_data:
neighbor_data.rr_client = True
node.bgp.neighbors.append(neighbor_data)

if not ibgp_ngb_list:
Copy link
Collaborator

@jbemmel jbemmel Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change leads to a warning about missing IGP even though the topology specifies bgp.sessions.ipv4: [ ebgp ] to exclude any IBGP sessions

The difference seems to be that the has_ibgp flag wouldn't get set in that case, but the new ibgp_ngb_list contains nodes even though the bgp.sessions parameter says those sessions shouldn't get built

return

# Do we have to warn the user that IBGP sessions work better with IGP?
Expand Down Expand Up @@ -387,6 +381,9 @@ def build_ebgp_sessions(node: Box, sessions: Box, topology: Box) -> None:
ebgp_data = bgp_neighbor(neighbor,ngb_ifdata,session_type,sessions,extra_data)
if not ebgp_data is None:
ebgp_data['as'] = neighbor_local_as
if session_type == 'localas_ibgp':
ebgp_data.rr_client = True
ebgp_data.next_hop_self = 'all'
if intf_vrf : # VRF neighbor
if node.vrfs[l.vrf].bgp is False: # Is BGP disabled for this VRF?
continue # ... yeah, move on
Expand Down
2 changes: 2 additions & 0 deletions tests/topology/expected/6pe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ nodes:
as: 65000
ipv4: 10.0.0.2
name: pe2
next_hop_self: true
type: ibgp
- activate:
ipv4: true
Expand Down Expand Up @@ -473,6 +474,7 @@ nodes:
as: 65000
ipv4: 10.0.0.1
name: pe1
next_hop_self: true
type: ibgp
- activate:
ipv4: true
Expand Down
12 changes: 12 additions & 0 deletions tests/topology/expected/addressing-ipv6-only.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ nodes:
as: 65000
ipv6: 2001:db8:0:15::1
name: r2
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -151,6 +152,7 @@ nodes:
as: 65000
ipv6: 2001:db8:0:2a::1
name: r3
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -166,6 +168,7 @@ nodes:
as: 65000
ipv6: 2001:db8:0:1::1
name: r4
next_hop_self: true
type: ibgp
next_hop_self: true
router_id: 10.0.0.7
Expand Down Expand Up @@ -291,6 +294,7 @@ nodes:
as: 65000
ipv6: 2001:db8:0:7::1
name: r1
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -306,6 +310,7 @@ nodes:
as: 65000
ipv6: 2001:db8:0:2a::1
name: r3
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -321,6 +326,7 @@ nodes:
as: 65000
ipv6: 2001:db8:0:1::1
name: r4
next_hop_self: true
type: ibgp
next_hop_self: true
router_id: 10.0.0.21
Expand Down Expand Up @@ -443,6 +449,7 @@ nodes:
as: 65000
ipv6: 2001:db8:0:7::1
name: r1
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -458,6 +465,7 @@ nodes:
as: 65000
ipv6: 2001:db8:0:15::1
name: r2
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -473,6 +481,7 @@ nodes:
as: 65000
ipv6: 2001:db8:0:1::1
name: r4
next_hop_self: true
type: ibgp
next_hop_self: true
router_id: 10.0.0.42
Expand Down Expand Up @@ -573,6 +582,7 @@ nodes:
as: 65000
ipv6: 2001:db8:0:7::1
name: r1
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -588,6 +598,7 @@ nodes:
as: 65000
ipv6: 2001:db8:0:15::1
name: r2
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -603,6 +614,7 @@ nodes:
as: 65000
ipv6: 2001:db8:0:2a::1
name: r3
next_hop_self: true
type: ibgp
next_hop_self: true
router_id: 10.0.0.1
Expand Down
12 changes: 12 additions & 0 deletions tests/topology/expected/addressing-ipv6-prefix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ nodes:
as: 65000
ipv6: 2001:db8::15
name: r2
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -127,6 +128,7 @@ nodes:
as: 65000
ipv6: 2001:db8::2a
name: r3
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -142,6 +144,7 @@ nodes:
as: 65000
ipv6: 2001:db8::1
name: r4
next_hop_self: true
type: ibgp
next_hop_self: true
router_id: 10.0.0.7
Expand Down Expand Up @@ -236,6 +239,7 @@ nodes:
as: 65000
ipv6: 2001:db8::7
name: r1
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -251,6 +255,7 @@ nodes:
as: 65000
ipv6: 2001:db8::2a
name: r3
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -266,6 +271,7 @@ nodes:
as: 65000
ipv6: 2001:db8::1
name: r4
next_hop_self: true
type: ibgp
next_hop_self: true
router_id: 10.0.0.21
Expand Down Expand Up @@ -373,6 +379,7 @@ nodes:
as: 65000
ipv6: 2001:db8::7
name: r1
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -388,6 +395,7 @@ nodes:
as: 65000
ipv6: 2001:db8::15
name: r2
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -403,6 +411,7 @@ nodes:
as: 65000
ipv6: 2001:db8::1
name: r4
next_hop_self: true
type: ibgp
next_hop_self: true
router_id: 10.0.0.42
Expand Down Expand Up @@ -495,6 +504,7 @@ nodes:
as: 65000
ipv6: 2001:db8::7
name: r1
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -508,6 +518,7 @@ nodes:
as: 65000
ipv6: 2001:db8::15
name: r2
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -521,6 +532,7 @@ nodes:
as: 65000
ipv6: 2001:db8::2a
name: r3
next_hop_self: true
type: ibgp
next_hop_self: true
router_id: 10.0.0.1
Expand Down
2 changes: 2 additions & 0 deletions tests/topology/expected/bgp-anycast.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ nodes:
as: 65000
ipv4: 10.0.0.2
name: l2
next_hop_self: true
type: ibgp
next_hop_self: true
router_id: 10.0.0.1
Expand Down Expand Up @@ -147,6 +148,7 @@ nodes:
as: 65000
ipv4: 10.0.0.1
name: l1
next_hop_self: true
type: ibgp
next_hop_self: true
router_id: 10.0.0.2
Expand Down
15 changes: 15 additions & 0 deletions tests/topology/expected/bgp-autogroup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ nodes:
as: 65101
ipv4: 10.0.0.6
name: a2
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -186,6 +187,7 @@ nodes:
as: 65101
ipv4: 10.0.0.7
name: a3
next_hop_self: true
type: ibgp
- activate:
ipv4: true
Expand Down Expand Up @@ -259,6 +261,7 @@ nodes:
as: 65101
ipv4: 10.0.0.5
name: a1
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -274,6 +277,7 @@ nodes:
as: 65101
ipv4: 10.0.0.7
name: a3
next_hop_self: true
type: ibgp
- activate:
ipv4: true
Expand Down Expand Up @@ -347,6 +351,7 @@ nodes:
as: 65101
ipv4: 10.0.0.5
name: a1
next_hop_self: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -362,6 +367,7 @@ nodes:
as: 65101
ipv4: 10.0.0.6
name: a2
next_hop_self: true
type: ibgp
- activate:
ipv4: true
Expand Down Expand Up @@ -436,6 +442,7 @@ nodes:
as: 65000
ipv4: 10.0.0.4
name: s1
next_hop_self: true
rr: true
type: ibgp
next_hop_self: true
Expand Down Expand Up @@ -514,6 +521,7 @@ nodes:
as: 65000
ipv4: 10.0.0.4
name: s1
next_hop_self: true
rr: true
type: ibgp
- activate:
Expand Down Expand Up @@ -628,6 +636,7 @@ nodes:
as: 65000
ipv4: 10.0.0.4
name: s1
next_hop_self: true
rr: true
type: ibgp
- activate:
Expand Down Expand Up @@ -724,6 +733,8 @@ nodes:
as: 65000
ipv4: 10.0.0.1
name: l1
next_hop_self: true
rr_client: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -740,6 +751,8 @@ nodes:
as: 65000
ipv4: 10.0.0.2
name: l2
next_hop_self: true
rr_client: true
type: ibgp
- _source_intf:
ifindex: 0
Expand All @@ -756,6 +769,8 @@ nodes:
as: 65000
ipv4: 10.0.0.3
name: l3
next_hop_self: true
rr_client: true
type: ibgp
next_hop_self: true
router_id: 10.0.0.4
Expand Down
Loading