Skip to content

Commit d7115a0

Browse files
committed
Set 'rr_client' and 'next_hop_self' attributes on BGP neighbor data
The BGP neighbor 'rr_client' and 'next_hop_self' attributes will make the configuration templates much cleaner as we'll avoid all the crazy logic that is repeated in every template. Note that the existing attributes are not changed, so there's no rush to migrate the templates to the new attributes. That can be done at any time we feel like cleaning them up. Also: refactor and cleanup the IBGP session generation logic to avoid duplicate code
1 parent 05583bc commit d7115a0

26 files changed

+202
-31
lines changed

netsim/modules/bgp.py

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -178,38 +178,32 @@ def get_remote_ibgp_endpoint(n: Box) -> Box:
178178
* Other nodes need IBGP sessions with all RRs in the same AS
179179
"""
180180
def build_ibgp_sessions(node: Box, sessions: Box, topology: Box) -> None:
181-
rrlist = find_bgp_rr(node.bgp.get("as"),topology)
182-
has_ibgp = False # Assume we have no IBGP sessions (yet)
183-
184-
# If we don't have route reflectors, or if the current node is a route
185-
# reflector, we need BGP sessions to all other nodes in the same AS
186-
if not(rrlist) or node.bgp.get("rr",None):
187-
for name,n in topology.nodes.items():
188-
if "bgp" in n:
189-
if n.bgp.get("as") == node.bgp.get("as") and n.name != node.name:
190-
n_intf = get_remote_ibgp_endpoint(n)
191-
neighbor_data = bgp_neighbor(n,n_intf,'ibgp',sessions,get_neighbor_rr(n))
192-
if not neighbor_data is None:
193-
if 'loopback' in node:
194-
neighbor_data._source_intf = node.loopback
195-
node.bgp.neighbors.append(neighbor_data)
196-
has_ibgp = True
197-
198-
#
199-
# The node is not a route reflector, and we have a non-empty RR list
200-
# We need BGP sessions with the route reflectors
181+
node_as = node.bgp['as'] # Set up variables that will come handy, starting with node AS
182+
is_rr = node.bgp.get("rr",None) # Is this node an RR?
183+
bgp_nhs = node.bgp.get("next_hop_self",None) # Do we have to set next hop on IBGP sessions?
184+
rrlist = [] if is_rr else find_bgp_rr(node_as,topology)
185+
186+
if is_rr or not rrlist: # If the current node is RR or we have a full mesh
187+
ibgp_ngb_list = [ # ... we need IBGP sessions to all nodes in the AS
188+
ngb for ngb in topology.nodes.values() # ... but while building the node list
189+
if ngb.name != node.name and # ... skip current node
190+
ngb.get('bgp.as',None) == node_as] # ... and everyone not in the current AS (or not running BGP)
201191
else:
202-
for n in rrlist:
203-
if n.name != node.name:
204-
n_intf = get_remote_ibgp_endpoint(n)
205-
neighbor_data = bgp_neighbor(n,n_intf,'ibgp',sessions,get_neighbor_rr(n))
206-
if not neighbor_data is None:
207-
if 'loopback' in node:
208-
neighbor_data._source_intf = node.loopback
209-
node.bgp.neighbors.append(neighbor_data)
210-
has_ibgp = True
211-
212-
if not has_ibgp:
192+
ibgp_ngb_list = rrlist
193+
194+
for n in ibgp_ngb_list:
195+
n_intf = get_remote_ibgp_endpoint(n)
196+
neighbor_data = bgp_neighbor(n,n_intf,'ibgp',sessions,get_neighbor_rr(n))
197+
if not neighbor_data is None:
198+
if 'loopback' in node:
199+
neighbor_data._source_intf = node.loopback
200+
if bgp_nhs:
201+
neighbor_data.next_hop_self = True
202+
if is_rr and not 'rr' in neighbor_data:
203+
neighbor_data.rr_client = True
204+
node.bgp.neighbors.append(neighbor_data)
205+
206+
if not ibgp_ngb_list:
213207
return
214208

215209
# Do we have to warn the user that IBGP sessions work better with IGP?
@@ -387,6 +381,9 @@ def build_ebgp_sessions(node: Box, sessions: Box, topology: Box) -> None:
387381
ebgp_data = bgp_neighbor(neighbor,ngb_ifdata,session_type,sessions,extra_data)
388382
if not ebgp_data is None:
389383
ebgp_data['as'] = neighbor_local_as
384+
if session_type == 'localas_ibgp':
385+
ebgp_data.rr_client = True
386+
ebgp_data.next_hop_self = 'all'
390387
if intf_vrf : # VRF neighbor
391388
if node.vrfs[l.vrf].bgp is False: # Is BGP disabled for this VRF?
392389
continue # ... yeah, move on

tests/topology/expected/6pe.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ nodes:
358358
as: 65000
359359
ipv4: 10.0.0.2
360360
name: pe2
361+
next_hop_self: true
361362
type: ibgp
362363
- activate:
363364
ipv4: true
@@ -473,6 +474,7 @@ nodes:
473474
as: 65000
474475
ipv4: 10.0.0.1
475476
name: pe1
477+
next_hop_self: true
476478
type: ibgp
477479
- activate:
478480
ipv4: true

tests/topology/expected/addressing-ipv6-only.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ nodes:
136136
as: 65000
137137
ipv6: 2001:db8:0:15::1
138138
name: r2
139+
next_hop_self: true
139140
type: ibgp
140141
- _source_intf:
141142
ifindex: 0
@@ -151,6 +152,7 @@ nodes:
151152
as: 65000
152153
ipv6: 2001:db8:0:2a::1
153154
name: r3
155+
next_hop_self: true
154156
type: ibgp
155157
- _source_intf:
156158
ifindex: 0
@@ -166,6 +168,7 @@ nodes:
166168
as: 65000
167169
ipv6: 2001:db8:0:1::1
168170
name: r4
171+
next_hop_self: true
169172
type: ibgp
170173
next_hop_self: true
171174
router_id: 10.0.0.7
@@ -291,6 +294,7 @@ nodes:
291294
as: 65000
292295
ipv6: 2001:db8:0:7::1
293296
name: r1
297+
next_hop_self: true
294298
type: ibgp
295299
- _source_intf:
296300
ifindex: 0
@@ -306,6 +310,7 @@ nodes:
306310
as: 65000
307311
ipv6: 2001:db8:0:2a::1
308312
name: r3
313+
next_hop_self: true
309314
type: ibgp
310315
- _source_intf:
311316
ifindex: 0
@@ -321,6 +326,7 @@ nodes:
321326
as: 65000
322327
ipv6: 2001:db8:0:1::1
323328
name: r4
329+
next_hop_self: true
324330
type: ibgp
325331
next_hop_self: true
326332
router_id: 10.0.0.21
@@ -443,6 +449,7 @@ nodes:
443449
as: 65000
444450
ipv6: 2001:db8:0:7::1
445451
name: r1
452+
next_hop_self: true
446453
type: ibgp
447454
- _source_intf:
448455
ifindex: 0
@@ -458,6 +465,7 @@ nodes:
458465
as: 65000
459466
ipv6: 2001:db8:0:15::1
460467
name: r2
468+
next_hop_self: true
461469
type: ibgp
462470
- _source_intf:
463471
ifindex: 0
@@ -473,6 +481,7 @@ nodes:
473481
as: 65000
474482
ipv6: 2001:db8:0:1::1
475483
name: r4
484+
next_hop_self: true
476485
type: ibgp
477486
next_hop_self: true
478487
router_id: 10.0.0.42
@@ -573,6 +582,7 @@ nodes:
573582
as: 65000
574583
ipv6: 2001:db8:0:7::1
575584
name: r1
585+
next_hop_self: true
576586
type: ibgp
577587
- _source_intf:
578588
ifindex: 0
@@ -588,6 +598,7 @@ nodes:
588598
as: 65000
589599
ipv6: 2001:db8:0:15::1
590600
name: r2
601+
next_hop_self: true
591602
type: ibgp
592603
- _source_intf:
593604
ifindex: 0
@@ -603,6 +614,7 @@ nodes:
603614
as: 65000
604615
ipv6: 2001:db8:0:2a::1
605616
name: r3
617+
next_hop_self: true
606618
type: ibgp
607619
next_hop_self: true
608620
router_id: 10.0.0.1

tests/topology/expected/addressing-ipv6-prefix.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ nodes:
112112
as: 65000
113113
ipv6: 2001:db8::15
114114
name: r2
115+
next_hop_self: true
115116
type: ibgp
116117
- _source_intf:
117118
ifindex: 0
@@ -127,6 +128,7 @@ nodes:
127128
as: 65000
128129
ipv6: 2001:db8::2a
129130
name: r3
131+
next_hop_self: true
130132
type: ibgp
131133
- _source_intf:
132134
ifindex: 0
@@ -142,6 +144,7 @@ nodes:
142144
as: 65000
143145
ipv6: 2001:db8::1
144146
name: r4
147+
next_hop_self: true
145148
type: ibgp
146149
next_hop_self: true
147150
router_id: 10.0.0.7
@@ -236,6 +239,7 @@ nodes:
236239
as: 65000
237240
ipv6: 2001:db8::7
238241
name: r1
242+
next_hop_self: true
239243
type: ibgp
240244
- _source_intf:
241245
ifindex: 0
@@ -251,6 +255,7 @@ nodes:
251255
as: 65000
252256
ipv6: 2001:db8::2a
253257
name: r3
258+
next_hop_self: true
254259
type: ibgp
255260
- _source_intf:
256261
ifindex: 0
@@ -266,6 +271,7 @@ nodes:
266271
as: 65000
267272
ipv6: 2001:db8::1
268273
name: r4
274+
next_hop_self: true
269275
type: ibgp
270276
next_hop_self: true
271277
router_id: 10.0.0.21
@@ -373,6 +379,7 @@ nodes:
373379
as: 65000
374380
ipv6: 2001:db8::7
375381
name: r1
382+
next_hop_self: true
376383
type: ibgp
377384
- _source_intf:
378385
ifindex: 0
@@ -388,6 +395,7 @@ nodes:
388395
as: 65000
389396
ipv6: 2001:db8::15
390397
name: r2
398+
next_hop_self: true
391399
type: ibgp
392400
- _source_intf:
393401
ifindex: 0
@@ -403,6 +411,7 @@ nodes:
403411
as: 65000
404412
ipv6: 2001:db8::1
405413
name: r4
414+
next_hop_self: true
406415
type: ibgp
407416
next_hop_self: true
408417
router_id: 10.0.0.42
@@ -495,6 +504,7 @@ nodes:
495504
as: 65000
496505
ipv6: 2001:db8::7
497506
name: r1
507+
next_hop_self: true
498508
type: ibgp
499509
- _source_intf:
500510
ifindex: 0
@@ -508,6 +518,7 @@ nodes:
508518
as: 65000
509519
ipv6: 2001:db8::15
510520
name: r2
521+
next_hop_self: true
511522
type: ibgp
512523
- _source_intf:
513524
ifindex: 0
@@ -521,6 +532,7 @@ nodes:
521532
as: 65000
522533
ipv6: 2001:db8::2a
523534
name: r3
535+
next_hop_self: true
524536
type: ibgp
525537
next_hop_self: true
526538
router_id: 10.0.0.1

tests/topology/expected/bgp-anycast.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ nodes:
6969
as: 65000
7070
ipv4: 10.0.0.2
7171
name: l2
72+
next_hop_self: true
7273
type: ibgp
7374
next_hop_self: true
7475
router_id: 10.0.0.1
@@ -147,6 +148,7 @@ nodes:
147148
as: 65000
148149
ipv4: 10.0.0.1
149150
name: l1
151+
next_hop_self: true
150152
type: ibgp
151153
next_hop_self: true
152154
router_id: 10.0.0.2

tests/topology/expected/bgp-autogroup.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ nodes:
171171
as: 65101
172172
ipv4: 10.0.0.6
173173
name: a2
174+
next_hop_self: true
174175
type: ibgp
175176
- _source_intf:
176177
ifindex: 0
@@ -186,6 +187,7 @@ nodes:
186187
as: 65101
187188
ipv4: 10.0.0.7
188189
name: a3
190+
next_hop_self: true
189191
type: ibgp
190192
- activate:
191193
ipv4: true
@@ -259,6 +261,7 @@ nodes:
259261
as: 65101
260262
ipv4: 10.0.0.5
261263
name: a1
264+
next_hop_self: true
262265
type: ibgp
263266
- _source_intf:
264267
ifindex: 0
@@ -274,6 +277,7 @@ nodes:
274277
as: 65101
275278
ipv4: 10.0.0.7
276279
name: a3
280+
next_hop_self: true
277281
type: ibgp
278282
- activate:
279283
ipv4: true
@@ -347,6 +351,7 @@ nodes:
347351
as: 65101
348352
ipv4: 10.0.0.5
349353
name: a1
354+
next_hop_self: true
350355
type: ibgp
351356
- _source_intf:
352357
ifindex: 0
@@ -362,6 +367,7 @@ nodes:
362367
as: 65101
363368
ipv4: 10.0.0.6
364369
name: a2
370+
next_hop_self: true
365371
type: ibgp
366372
- activate:
367373
ipv4: true
@@ -436,6 +442,7 @@ nodes:
436442
as: 65000
437443
ipv4: 10.0.0.4
438444
name: s1
445+
next_hop_self: true
439446
rr: true
440447
type: ibgp
441448
next_hop_self: true
@@ -514,6 +521,7 @@ nodes:
514521
as: 65000
515522
ipv4: 10.0.0.4
516523
name: s1
524+
next_hop_self: true
517525
rr: true
518526
type: ibgp
519527
- activate:
@@ -628,6 +636,7 @@ nodes:
628636
as: 65000
629637
ipv4: 10.0.0.4
630638
name: s1
639+
next_hop_self: true
631640
rr: true
632641
type: ibgp
633642
- activate:
@@ -724,6 +733,8 @@ nodes:
724733
as: 65000
725734
ipv4: 10.0.0.1
726735
name: l1
736+
next_hop_self: true
737+
rr_client: true
727738
type: ibgp
728739
- _source_intf:
729740
ifindex: 0
@@ -740,6 +751,8 @@ nodes:
740751
as: 65000
741752
ipv4: 10.0.0.2
742753
name: l2
754+
next_hop_self: true
755+
rr_client: true
743756
type: ibgp
744757
- _source_intf:
745758
ifindex: 0
@@ -756,6 +769,8 @@ nodes:
756769
as: 65000
757770
ipv4: 10.0.0.3
758771
name: l3
772+
next_hop_self: true
773+
rr_client: true
759774
type: ibgp
760775
next_hop_self: true
761776
router_id: 10.0.0.4

0 commit comments

Comments
 (0)