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