Skip to content

Commit bf487e3

Browse files
committed
Refactored
1 parent 57f11ea commit bf487e3

35 files changed

+1803
-78
lines changed

netsim/augment/devices.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def get_device_features(node: Box, defaults: Box) -> Box:
6767
"""
6868
Get device loopback name (built-in loopback if ifindex == 0 else an additional loopback)
6969
"""
70-
def get_loopback_name(node: Box, defaults: Box, ifindex: int = 0) -> typing.Optional[str]:
71-
lbname = get_device_attribute(node,'loopback_interface_name',defaults)
70+
def get_loopback_name(node: Box, topology: Box, ifindex: int = 0) -> typing.Optional[str]:
71+
lbname = get_device_attribute(node,'loopback_interface_name',topology.defaults)
7272
if not lbname:
7373
return None
7474

netsim/augment/nodes.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,47 @@ def check_duplicate_mgmt_addr(topology: Box) -> None:
194194
else:
195195
used_addr[af][n_addr] = nname
196196

197+
'''
198+
Set addresses of the main loopback interface
199+
'''
200+
def augment_loopback_interface(n: Box, topology: Box) -> None:
201+
n.loopback.type = 'loopback'
202+
n.loopback.neighbors = []
203+
n.loopback.virtual_interface = True
204+
n.loopback.ifindex = 0
205+
n.loopback.ifname = devices.get_loopback_name(n,topology) or 'Loopback'
206+
207+
pool = n.get('loopback.pool','loopback')
208+
prefix_list = addressing.get(topology.pools,[ pool ],n.id)
209+
210+
for af in prefix_list:
211+
if prefix_list[af] is True:
212+
log.error(
213+
f"Address pool {pool} cannot contain unnumbered/LLA addresses",
214+
category=log.IncorrectType,
215+
module='nodes')
216+
elif not n.loopback[af] and not (prefix_list[af] is False):
217+
if af == 'ipv6':
218+
if prefix_list[af].prefixlen == 128:
219+
n.loopback[af] = str(prefix_list[af])
220+
else:
221+
n.loopback[af] = addressing.get_nth_ip_from_prefix(prefix_list[af],1)
222+
else:
223+
n.loopback[af] = str(prefix_list[af])
224+
n.af[af] = True
225+
226+
for af in log.AF_LIST:
227+
if af in n.loopback and not isinstance(n.loopback[af],str):
228+
log.error(
229+
f'{af} address on the main loopback interface of node {n.name} must be a CIDR prefix',
230+
category=log.IncorrectType,
231+
module='nodes')
232+
233+
links.check_interface_host_bits(n.loopback,n)
234+
197235
"""
198236
Add device data to nodes
199237
"""
200-
201238
def find_node_device(n: Box, topology: Box) -> bool:
202239
if 'device' not in n:
203240
n.device = topology.defaults.device
@@ -389,12 +426,6 @@ def augment_node_device_data(n: Box, defaults: Box) -> None:
389426
flag='nodes.roles',
390427
category=log.IncorrectType,
391428
module='nodes')
392-
#
393-
# Set loopback.ifname
394-
if 'loopback' in n:
395-
ifname = devices.get_loopback_name(n,defaults)
396-
if ifname:
397-
n.loopback.ifname = ifname
398429

399430
'''
400431
Main node transformation code
@@ -426,9 +457,11 @@ def transform(topology: Box, defaults: Box, pools: Box) -> None:
426457

427458
augment_node_device_data(n,defaults)
428459

429-
n.af = {} # Nodes must have AF attribute
460+
n.af = {} # Nodes must have AF attribute
430461

431462
augment_mgmt_if(n,defaults,topology.addressing.mgmt)
463+
if 'loopback' in n or n.get('role',None) not in ['host','bridge']: # Augment loopback interface if needed
464+
augment_loopback_interface(n,topology)
432465
providers.execute_node("augment_node_data",n,topology)
433466

434467
check_duplicate_mgmt_addr(topology)

netsim/modules/vxlan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def assign_vni(toponode: Box, obj_path: str, topology: Box) -> None:
9595
def node_set_vtep(node: Box, topology: Box) -> bool:
9696
# default vtep interface & interface name
9797
vtep_interface = node.loopback
98-
loopback_name = devices.get_loopback_name(node,topology.defaults)
98+
loopback_name = devices.get_loopback_name(node,topology)
9999
if not loopback_name:
100100
log.fatal("Can't find the loopback name of VXLAN-capable device {node.device}",module="vxlan",header=True)
101101

netsim/roles/router.py

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,5 @@
11
'''
22
Router-specific data transformation
33
4-
* Add loopback interface data
4+
Nothing left
55
'''
6-
import typing
7-
8-
from box import Box, BoxList
9-
10-
from ..utils import log
11-
from ..augment import devices,addressing,links
12-
13-
from . import select_nodes_by_role
14-
15-
'''
16-
Set addresses of the main loopback interface
17-
'''
18-
def loopback_interface(n: Box, pools: Box, topology: Box) -> None:
19-
n.loopback.type = 'loopback'
20-
n.loopback.neighbors = []
21-
n.loopback.virtual_interface = True
22-
n.loopback.ifindex = 0
23-
n.loopback.ifname = devices.get_loopback_name(n,topology.defaults) or 'Loopback'
24-
25-
pool = n.get('loopback.pool','loopback')
26-
prefix_list = addressing.get(pools,[ pool ],n.id)
27-
28-
for af in prefix_list:
29-
if prefix_list[af] is True:
30-
log.error(
31-
f"Address pool {pool} cannot contain unnumbered/LLA addresses",
32-
category=log.IncorrectType,
33-
module='nodes')
34-
elif not n.loopback[af] and not (prefix_list[af] is False):
35-
if af == 'ipv6':
36-
if prefix_list[af].prefixlen == 128:
37-
n.loopback[af] = str(prefix_list[af])
38-
else:
39-
n.loopback[af] = addressing.get_nth_ip_from_prefix(prefix_list[af],1)
40-
else:
41-
n.loopback[af] = str(prefix_list[af])
42-
n.af[af] = True
43-
44-
for af in log.AF_LIST:
45-
if af in n.loopback and not isinstance(n.loopback[af],str):
46-
log.error(
47-
f'{af} address on the main loopback interface of node {n.name} must be a CIDR prefix',
48-
category=log.IncorrectType,
49-
module='nodes')
50-
51-
links.check_interface_host_bits(n.loopback,n)
52-
53-
def post_node_transform(topology: Box) -> None:
54-
for ndata in select_nodes_by_role(topology,'router'):
55-
loopback_interface(ndata,topology.pools,topology)

tests/topology/expected/anycast-gateway.yml

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,9 @@ links:
315315
type: p2p
316316
module:
317317
- vlan
318-
- ospf
319318
- gateway
319+
- routing
320+
- ospf
320321
name: input
321322
nodes:
322323
h1:
@@ -374,8 +375,32 @@ nodes:
374375
ifname: eth0
375376
ipv4: 192.168.121.105
376377
mac: 08:4f:a9:05:00:00
378+
module:
379+
- routing
377380
name: h1
378381
role: host
382+
routing:
383+
static:
384+
- ipv4: 172.16.0.0/16
385+
nexthop:
386+
idx: 0
387+
intf: eth1
388+
ipv4: 172.16.1.254
389+
- ipv4: 10.0.0.0/24
390+
nexthop:
391+
idx: 0
392+
intf: eth1
393+
ipv4: 172.16.1.254
394+
- ipv4: 10.1.0.0/16
395+
nexthop:
396+
idx: 0
397+
intf: eth1
398+
ipv4: 172.16.1.254
399+
- ipv4: 10.2.0.0/24
400+
nexthop:
401+
idx: 0
402+
intf: eth1
403+
ipv4: 172.16.1.254
379404
h2:
380405
af:
381406
ipv4: true
@@ -431,8 +456,32 @@ nodes:
431456
ifname: eth0
432457
ipv4: 192.168.121.106
433458
mac: 08:4f:a9:06:00:00
459+
module:
460+
- routing
434461
name: h2
435462
role: host
463+
routing:
464+
static:
465+
- ipv4: 172.16.0.0/16
466+
nexthop:
467+
idx: 0
468+
intf: eth1
469+
ipv4: 172.16.1.254
470+
- ipv4: 10.0.0.0/24
471+
nexthop:
472+
idx: 0
473+
intf: eth1
474+
ipv4: 172.16.1.254
475+
- ipv4: 10.1.0.0/16
476+
nexthop:
477+
idx: 0
478+
intf: eth1
479+
ipv4: 172.16.1.254
480+
- ipv4: 10.2.0.0/24
481+
nexthop:
482+
idx: 0
483+
intf: eth1
484+
ipv4: 172.16.1.254
436485
h3:
437486
af:
438487
ipv4: true
@@ -470,8 +519,32 @@ nodes:
470519
ifname: eth0
471520
ipv4: 192.168.121.107
472521
mac: 08:4f:a9:07:00:00
522+
module:
523+
- routing
473524
name: h3
474525
role: host
526+
routing:
527+
static:
528+
- ipv4: 172.16.0.0/16
529+
nexthop:
530+
idx: 0
531+
intf: eth1
532+
ipv4: 172.16.0.1
533+
- ipv4: 10.0.0.0/24
534+
nexthop:
535+
idx: 0
536+
intf: eth1
537+
ipv4: 172.16.0.1
538+
- ipv4: 10.1.0.0/16
539+
nexthop:
540+
idx: 0
541+
intf: eth1
542+
ipv4: 172.16.0.1
543+
- ipv4: 10.2.0.0/24
544+
nexthop:
545+
idx: 0
546+
intf: eth1
547+
ipv4: 172.16.0.1
475548
h4:
476549
af:
477550
ipv4: true
@@ -604,8 +677,96 @@ nodes:
604677
ifname: eth0
605678
ipv4: 192.168.121.113
606679
mac: 08:4f:a9:0d:00:00
680+
module:
681+
- routing
607682
name: h4
608683
role: host
684+
routing:
685+
static:
686+
- ipv4: 172.16.0.0/16
687+
nexthop:
688+
idx: 0
689+
intf: eth1
690+
ipv4: 172.16.0.1
691+
- ipv4: 172.16.0.0/16
692+
nexthop:
693+
idx: 1
694+
intf: eth2
695+
ipv4: 10.42.42.13
696+
- ipv4: 172.16.0.0/16
697+
nexthop:
698+
idx: 2
699+
intf: eth3
700+
ipv4: 10.42.43.1
701+
- ipv4: 172.16.0.0/16
702+
nexthop:
703+
idx: 3
704+
intf: eth4
705+
ipv4: 172.31.31.1
706+
ipv6: 2001:db8:cafe:1::1
707+
- ipv4: 10.0.0.0/24
708+
nexthop:
709+
idx: 0
710+
intf: eth1
711+
ipv4: 172.16.0.1
712+
- ipv4: 10.0.0.0/24
713+
nexthop:
714+
idx: 1
715+
intf: eth2
716+
ipv4: 10.42.42.13
717+
- ipv4: 10.0.0.0/24
718+
nexthop:
719+
idx: 2
720+
intf: eth3
721+
ipv4: 10.42.43.1
722+
- ipv4: 10.0.0.0/24
723+
nexthop:
724+
idx: 3
725+
intf: eth4
726+
ipv4: 172.31.31.1
727+
ipv6: 2001:db8:cafe:1::1
728+
- ipv4: 10.1.0.0/16
729+
nexthop:
730+
idx: 0
731+
intf: eth1
732+
ipv4: 172.16.0.1
733+
- ipv4: 10.1.0.0/16
734+
nexthop:
735+
idx: 1
736+
intf: eth2
737+
ipv4: 10.42.42.13
738+
- ipv4: 10.1.0.0/16
739+
nexthop:
740+
idx: 2
741+
intf: eth3
742+
ipv4: 10.42.43.1
743+
- ipv4: 10.1.0.0/16
744+
nexthop:
745+
idx: 3
746+
intf: eth4
747+
ipv4: 172.31.31.1
748+
ipv6: 2001:db8:cafe:1::1
749+
- ipv4: 10.2.0.0/24
750+
nexthop:
751+
idx: 0
752+
intf: eth1
753+
ipv4: 172.16.0.1
754+
- ipv4: 10.2.0.0/24
755+
nexthop:
756+
idx: 1
757+
intf: eth2
758+
ipv4: 10.42.42.13
759+
- ipv4: 10.2.0.0/24
760+
nexthop:
761+
idx: 2
762+
intf: eth3
763+
ipv4: 10.42.43.1
764+
- ipv4: 10.2.0.0/24
765+
nexthop:
766+
idx: 3
767+
intf: eth4
768+
ipv4: 172.31.31.1
769+
ipv6: 2001:db8:cafe:1::1
609770
r1:
610771
af:
611772
ipv4: true

0 commit comments

Comments
 (0)