blob: 155108d69c96527a73dc6f54b1407850bdf5dfb1 [file] [log] [blame]
Carmelo Casconeb34d8e12020-09-28 16:16:59 -07001#!/usr/bin/python3
slowrdb071b22017-07-07 11:10:25 -07002from 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, OVSSwitch
7import os
8
9QUAGGA_DIR = '/usr/lib/quagga'
10# Must exist and be owned by quagga user (quagga:quagga by default on Ubuntu)
11QUAGGA_RUN_DIR = '/var/run/quagga'
12EXABGP_RUN_EXE = '~/exabgp/sbin/exabgp'
13CONFIG_DIR = 'configs/'
14
15onos = RemoteController('onos', ip='192.168.0.1', port=6633)
16
17
18class Onos(Host):
19
20 def __init__(self, name, intfDict, *args, **kwargs):
21 Host.__init__(self, name, *args, **kwargs)
22
23 self.intfDict = intfDict
24
25 def config(self, **kwargs):
26 Host.config(self, **kwargs)
27
Carmelo Casconeb34d8e12020-09-28 16:16:59 -070028 for intf, attrs in list(self.intfDict.items()):
slowrdb071b22017-07-07 11:10:25 -070029 self.cmd('ip addr flush dev %s' % intf)
30 if 'mac' in attrs:
31 self.cmd('ip link set %s down' % intf)
32 self.cmd('ip link set %s address %s' % (intf, attrs['mac']))
33 self.cmd('ip link set %s up ' % intf)
34 for addr in attrs['ipAddrs']:
35 self.cmd('ip addr add %s dev %s' % (addr, intf))
36
37
38class QuaggaRouter(Host):
39
40 def __init__(self, name, quaggaConfFile, zebraConfFile, intfDict, *args, **kwargs):
41 Host.__init__(self, name, *args, **kwargs)
42
43 self.quaggaConfFile = quaggaConfFile
44 self.zebraConfFile = zebraConfFile
45 self.intfDict = intfDict
46
47 def config(self, **kwargs):
48 Host.config(self, **kwargs)
49 self.cmd('sysctl net.ipv4.ip_forward=1')
50
Carmelo Casconeb34d8e12020-09-28 16:16:59 -070051 for intf, attrs in list(self.intfDict.items()):
slowrdb071b22017-07-07 11:10:25 -070052 self.cmd('ip addr flush dev %s' % intf)
53 if 'mac' in attrs:
54 self.cmd('ip link set %s down' % intf)
55 self.cmd('ip link set %s address %s' % (intf, attrs['mac']))
56 self.cmd('ip link set %s up ' % intf)
57 for addr in attrs['ipAddrs']:
58 self.cmd('ip addr add %s dev %s' % (addr, intf))
59
60 self.cmd('/usr/lib/quagga/zebra -d -f %s -z %s/zebra%s.api -i %s/zebra%s.pid' %
61 (self.zebraConfFile, QUAGGA_RUN_DIR, self.name, QUAGGA_RUN_DIR, self.name))
62 self.cmd('/usr/lib/quagga/bgpd -d -f %s -z %s/zebra%s.api -i %s/bgpd%s.pid' %
63 (self.quaggaConfFile, QUAGGA_RUN_DIR, self.name, QUAGGA_RUN_DIR, self.name))
64
65 def terminate(self):
66 self.cmd("ps ax | egrep 'bgpd%s.pid|zebra%s.pid' | awk '{print $1}' | xargs kill" % (
67 self.name, self.name))
68
69 Host.terminate(self)
70
71
72class ExaBGPRouter(Host):
73
74 def __init__(self, name, exaBGPconf, intfDict, *args, **kwargs):
75 Host.__init__(self, name, *args, **kwargs)
76
77 self.exaBGPconf = exaBGPconf
78 self.intfDict = intfDict
79
80 def config(self, **kwargs):
81 Host.config(self, **kwargs)
82 self.cmd('sysctl net.ipv4.ip_forward=1')
83
Carmelo Casconeb34d8e12020-09-28 16:16:59 -070084 for intf, attrs in list(self.intfDict.items()):
slowrdb071b22017-07-07 11:10:25 -070085 self.cmd('ip addr flush dev %s' % intf)
86 if 'mac' in attrs:
87 self.cmd('ip link set %s down' % intf)
88 self.cmd('ip link set %s address %s' % (intf, attrs['mac']))
89 self.cmd('ip link set %s up ' % intf)
90 for addr in attrs['ipAddrs']:
91 self.cmd('ip addr add %s dev %s' % (addr, intf))
92
93 self.cmd('%s %s > /dev/null 2> exabgp.log &' % (EXABGP_RUN_EXE, self.exaBGPconf))
94
95 def terminate(self):
96 self.cmd(
97 "ps ax | egrep 'lib/exabgp/application/bgp.py' | awk '{print $1}' | xargs kill")
98 self.cmd(
99 "ps ax | egrep 'server.py' | awk '{print $1}' | xargs kill")
100 Host.terminate(self)
101
102
103class ONOSSwitch(OVSSwitch):
104
105 def start(self, controllers):
106 return OVSSwitch.start(self, [onos])
107
108
109class L2Switch(OVSSwitch):
110
111 def start(self, controllers):
112 return OVSSwitch.start(self, [])
113
114
115class ArtemisTopo(Topo):
116 "Artemis tutorial topology"
117
118 def build(self):
119 zebraConf = '%szebra.conf' % CONFIG_DIR
120
121 quaggaConf = '%sR1-quagga.conf' % CONFIG_DIR
122 name = 'R1'
123 eth0 = {
124 'ipAddrs': ['150.1.1.2/30']
125 }
126 eth1 = {
127 'ipAddrs': ['10.0.0.1/8']
128 }
129 eth2 = {
130 'ipAddrs': ['150.1.2.1/30']
131 }
132 intfs = {
133 '%s-eth0' % name: eth0,
134 '%s-eth1' % name: eth1,
135 '%s-eth2' % name: eth2
136 }
137 r1 = self.addHost(name, cls=QuaggaRouter, quaggaConfFile=quaggaConf,
138 zebraConfFile=zebraConf, intfDict=intfs)
139
140 quaggaConf = '%sR2-quagga.conf' % CONFIG_DIR
141 name = 'R2'
142 eth0 = {
143 'ipAddrs': ['150.1.3.1/30']
144 }
145 eth1 = {
146 'ipAddrs': ['150.1.2.2/30']
147 }
148 intfs = {
149 '%s-eth0' % name: eth0,
150 '%s-eth1' % name: eth1
151 }
152 r2 = self.addHost(name, cls=QuaggaRouter, quaggaConfFile=quaggaConf,
153 zebraConfFile=zebraConf, intfDict=intfs)
154
155 quaggaConf = '%sR3-quagga.conf' % CONFIG_DIR
156 name = 'R3'
157 eth0 = {
158 'ipAddrs': ['40.0.0.1/8']
159 }
160 eth1 = {
161 'ipAddrs': ['150.1.1.1/30']
162 }
163 intfs = {
164 '%s-eth0' % name: eth0,
165 '%s-eth1' % name: eth1
166 }
167 r3 = self.addHost(name, cls=QuaggaRouter, quaggaConfFile=quaggaConf,
168 zebraConfFile=zebraConf, intfDict=intfs)
169
170 quaggaConf = '%sR4-quagga.conf' % CONFIG_DIR
171 name = 'R4'
172 eth0 = {
173 'ipAddrs': ['150.1.3.2/30'],
174 'mac': 'e2:f5:32:16:9a:46'
175 }
176 eth1 = {
177 'ipAddrs': ['10.10.10.1/24']
178 }
179 intfs = {
180 '%s-eth0' % name: eth0,
181 '%s-eth1' % name: eth1
182 }
183 r4 = self.addHost(name, cls=QuaggaRouter, quaggaConfFile=quaggaConf,
184 zebraConfFile=zebraConf, intfDict=intfs)
185
186 ovs = self.addSwitch('ovs', dpid='00002a45d713e141', cls=ONOSSwitch)
187
188 l2_switch = self.addSwitch(
189 'l2_switch', dpid='0000000000000001', failMode='standalone', cls=L2Switch)
190
191 h1 = self.addHost('h1', ip='10.0.0.100/8', defaultRoute='via 10.0.0.1')
192 h4 = self.addHost('h4', ip='40.0.0.100/8', defaultRoute='via 40.0.0.1')
193
194 # Set up the internal BGP speaker
195
196 name = 'exabgp'
197 eth0 = {
198 'ipAddrs': ['10.0.0.3/8']
199 }
200 eth1 = {
201 'ipAddrs': ['192.168.1.2/24']
202 }
203 intfs = {
204 '%s-eth0' % name: eth0,
205 '%s-eth1' % name: eth1
206 }
207 exabgp = self.addHost(name, cls=ExaBGPRouter,
208 exaBGPconf='%sexabgp.conf' % CONFIG_DIR,
209 intfDict=intfs)
210
211 self.addLink(r1, r3, port1=0, port2=1)
212 self.addLink(r1, l2_switch, port1=1, port2=2)
213 self.addLink(r1, r2, port1=2, port2=1)
214
215 self.addLink(ovs, r2, port1=2, port2=0)
216 self.addLink(ovs, h4, port1=3, port2=0)
217 self.addLink(ovs, r4, port1=4, port2=0)
218
219 self.addLink(l2_switch, h1, port1=1, port2=0)
220 self.addLink(l2_switch, exabgp, port1=3, port2=0)
221
222 name = 'onos'
223 eth0 = {
224 'ipAddrs': ['192.168.0.1/24']
225 }
226 eth1 = {
227 'ipAddrs': ['10.10.10.2/24']
228 }
229 eth2 = {
230 'ipAddrs': ['192.168.1.1/24']
231 }
232 intfs = {
233 '%s-eth0' % name: eth0,
234 '%s-eth1' % name: eth1,
235 '%s-eth2' % name: eth2
236 }
237 onos = self.addHost(name, inNamespace=False, cls=Onos, intfDict=intfs)
238
239 self.addLink(onos, ovs, port1=0, port2=1)
240 self.addLink(onos, r4, port1=1, port2=1)
241 self.addLink(onos, exabgp, port1=2, port2=1)
242
243topos = {'artemis': ArtemisTopo}
244
245if __name__ == '__main__':
246 setLogLevel('debug')
247 topo = ArtemisTopo()
248
249 net = Mininet(topo=topo, build=False)
250 net.addController(onos)
251 net.build()
252 net.start()
253
254 CLI(net)
255
256 net.stop()
257
258 info("done\n")