blob: 7a66e6beec8128cf65927ecea9edc35cce406747 [file] [log] [blame]
Carmelo Casconeb34d8e12020-09-28 16:16:59 -07001#!/usr/bin/python3
Jonathan Hart65048cd2014-12-01 14:12:57 -08002from mininet.topo import Topo
3from mininet.net import Mininet
4from mininet.cli import CLI
5from mininet.log import setLogLevel, info, debug
6from mininet.node import Host, RemoteController
7
8QUAGGA_DIR = '/usr/lib/quagga'
9# Must exist and be owned by quagga user (quagga:quagga by default on Ubuntu)
10QUAGGA_RUN_DIR = '/var/run/quagga'
11CONFIG_DIR = 'configs'
12
13class SdnIpHost(Host):
14 def __init__(self, name, ip, route, *args, **kwargs):
15 Host.__init__(self, name, ip=ip, *args, **kwargs)
16
17 self.route = route
18
19 def config(self, **kwargs):
20 Host.config(self, **kwargs)
21
22 debug("configuring route %s" % self.route)
23
24 self.cmd('ip route add default via %s' % self.route)
25
26class Router(Host):
27 def __init__(self, name, quaggaConfFile, zebraConfFile, intfDict, *args, **kwargs):
28 Host.__init__(self, name, *args, **kwargs)
29
30 self.quaggaConfFile = quaggaConfFile
31 self.zebraConfFile = zebraConfFile
32 self.intfDict = intfDict
33
34 def config(self, **kwargs):
35 Host.config(self, **kwargs)
36 self.cmd('sysctl net.ipv4.ip_forward=1')
37
Carmelo Casconeb34d8e12020-09-28 16:16:59 -070038 for intf, attrs in list(self.intfDict.items()):
Jonathan Hart65048cd2014-12-01 14:12:57 -080039 self.cmd('ip addr flush dev %s' % intf)
40 if 'mac' in attrs:
41 self.cmd('ip link set %s down' % intf)
42 self.cmd('ip link set %s address %s' % (intf, attrs['mac']))
43 self.cmd('ip link set %s up ' % intf)
44 for addr in attrs['ipAddrs']:
45 self.cmd('ip addr add %s dev %s' % (addr, intf))
46
47 self.cmd('/usr/lib/quagga/zebra -d -f %s -z %s/zebra%s.api -i %s/zebra%s.pid' % (self.zebraConfFile, QUAGGA_RUN_DIR, self.name, QUAGGA_RUN_DIR, self.name))
48 self.cmd('/usr/lib/quagga/bgpd -d -f %s -z %s/zebra%s.api -i %s/bgpd%s.pid' % (self.quaggaConfFile, QUAGGA_RUN_DIR, self.name, QUAGGA_RUN_DIR, self.name))
49
50
51 def terminate(self):
52 self.cmd("ps ax | egrep 'bgpd%s.pid|zebra%s.pid' | awk '{print $1}' | xargs kill" % (self.name, self.name))
53
54 Host.terminate(self)
55
56
57class SdnIpTopo( Topo ):
58 "SDN-IP tutorial topology"
Phil Huangf5d6c912015-12-21 15:43:06 +080059
Jonathan Hart65048cd2014-12-01 14:12:57 -080060 def build( self ):
61 s1 = self.addSwitch('s1', dpid='00000000000000a1')
62 s2 = self.addSwitch('s2', dpid='00000000000000a2')
63 s3 = self.addSwitch('s3', dpid='00000000000000a3')
64 s4 = self.addSwitch('s4', dpid='00000000000000a4')
65 s5 = self.addSwitch('s5', dpid='00000000000000a5')
66 s6 = self.addSwitch('s6', dpid='00000000000000a6')
67
68 zebraConf = '%s/zebra.conf' % CONFIG_DIR
69
70 # Switches we want to attach our routers to, in the correct order
71 attachmentSwitches = [s1, s2, s5, s6]
72
73 for i in range(1, 4+1):
74 name = 'r%s' % i
75
76 eth0 = { 'mac' : '00:00:00:00:0%s:01' % i,
77 'ipAddrs' : ['10.0.%s.1/24' % i] }
78 eth1 = { 'ipAddrs' : ['192.168.%s.254/24' % i] }
79 intfs = { '%s-eth0' % name : eth0,
80 '%s-eth1' % name : eth1 }
81
82 quaggaConf = '%s/quagga%s.conf' % (CONFIG_DIR, i)
83
84 router = self.addHost(name, cls=Router, quaggaConfFile=quaggaConf,
85 zebraConfFile=zebraConf, intfDict=intfs)
Phil Huangf5d6c912015-12-21 15:43:06 +080086
87 host = self.addHost('h%s' % i, cls=SdnIpHost,
Jonathan Hart65048cd2014-12-01 14:12:57 -080088 ip='192.168.%s.1/24' % i,
89 route='192.168.%s.254' % i)
Phil Huangf5d6c912015-12-21 15:43:06 +080090
Jonathan Hart65048cd2014-12-01 14:12:57 -080091 self.addLink(router, attachmentSwitches[i-1])
92 self.addLink(router, host)
93
94 # Set up the internal BGP speaker
Phil Huangf5d6c912015-12-21 15:43:06 +080095 bgpEth0 = { 'mac':'00:00:00:00:00:01',
Jonathan Hart65048cd2014-12-01 14:12:57 -080096 'ipAddrs' : ['10.0.1.101/24',
97 '10.0.2.101/24',
98 '10.0.3.101/24',
99 '10.0.4.101/24',] }
100 bgpEth1 = { 'ipAddrs' : ['10.10.10.1/24'] }
101 bgpIntfs = { 'bgp-eth0' : bgpEth0,
102 'bgp-eth1' : bgpEth1 }
Phil Huangf5d6c912015-12-21 15:43:06 +0800103
104 bgp = self.addHost( "bgp", cls=Router,
105 quaggaConfFile = '%s/quagga-sdn.conf' % CONFIG_DIR,
106 zebraConfFile = zebraConf,
Jonathan Hart65048cd2014-12-01 14:12:57 -0800107 intfDict=bgpIntfs )
Phil Huangf5d6c912015-12-21 15:43:06 +0800108
Jonathan Hart65048cd2014-12-01 14:12:57 -0800109 self.addLink( bgp, s3 )
110
111 # Connect BGP speaker to the root namespace so it can peer with ONOS
112 root = self.addHost( 'root', inNamespace=False, ip='10.10.10.2/24' )
113 self.addLink( root, bgp )
114
115
116 # Wire up the switches in the topology
117 self.addLink( s1, s2 )
118 self.addLink( s1, s3 )
119 self.addLink( s2, s4 )
120 self.addLink( s3, s4 )
121 self.addLink( s3, s5 )
122 self.addLink( s4, s6 )
123 self.addLink( s5, s6 )
124
125topos = { 'sdnip' : SdnIpTopo }
126
127if __name__ == '__main__':
128 setLogLevel('debug')
Phil Huangf5d6c912015-12-21 15:43:06 +0800129 topo = SdnIpTopo()
Jonathan Hart65048cd2014-12-01 14:12:57 -0800130
131 net = Mininet(topo=topo, controller=RemoteController)
132
133 net.start()
134
135 CLI(net)
136
137 net.stop()
138
139 info("done\n")