Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """ |
| 4 | Libraries for creating L3 topologies with routing protocols. |
| 5 | """ |
| 6 | |
| 7 | from mininet.node import Host, OVSBridge |
| 8 | from mininet.nodelib import NAT |
| 9 | from mininet.log import info, debug, error |
| 10 | from mininet.cli import CLI |
| 11 | from ipaddress import ip_network, ip_address, ip_interface |
Jonathan Hart | d5d8087 | 2017-01-16 13:55:41 -0800 | [diff] [blame] | 12 | import os |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 13 | |
| 14 | class RoutedHost(Host): |
| 15 | """Host that can be configured with multiple IP addresses.""" |
| 16 | def __init__(self, name, ips, gateway, *args, **kwargs): |
| 17 | super(RoutedHost, self).__init__(name, *args, **kwargs) |
| 18 | |
| 19 | self.ips = ips |
| 20 | self.gateway = gateway |
| 21 | |
| 22 | def config(self, **kwargs): |
| 23 | Host.config(self, **kwargs) |
| 24 | |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 25 | self.cmd('ip -4 addr flush dev %s' % self.defaultIntf()) |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 26 | for ip in self.ips: |
| 27 | self.cmd('ip addr add %s dev %s' % (ip, self.defaultIntf())) |
| 28 | |
| 29 | self.cmd('ip route add default via %s' % self.gateway) |
| 30 | |
| 31 | class Router(Host): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 32 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 33 | """An L3 router. |
| 34 | Configures the Linux kernel for L3 forwarding and supports rich interface |
| 35 | configuration of IP addresses, MAC addresses and VLANs.""" |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 36 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 37 | def __init__(self, name, interfaces, *args, **kwargs): |
| 38 | super(Router, self).__init__(name, **kwargs) |
| 39 | |
| 40 | self.interfaces = interfaces |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 41 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 42 | def config(self, **kwargs): |
| 43 | super(Host, self).config(**kwargs) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 44 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 45 | self.cmd('sysctl net.ipv4.ip_forward=1') |
| 46 | self.cmd('sysctl net.ipv4.conf.all.rp_filter=0') |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 47 | self.cmd('sysctl net.ipv6.conf.all.forwarding=1') |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 48 | |
| 49 | for intf, configs in self.interfaces.items(): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 50 | self.cmd('ip -4 addr flush dev %s' % intf) |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 51 | self.cmd( 'sysctl net.ipv4.conf.%s.rp_filter=0' % intf ) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 52 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 53 | if not isinstance(configs, list): |
| 54 | configs = [configs] |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 55 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 56 | for attrs in configs: |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 57 | # Configure the vlan if there is one |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 58 | if 'vlan' in attrs: |
| 59 | vlanName = '%s.%s' % (intf, attrs['vlan']) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 60 | self.cmd('ip link add link %s name %s type vlan id %s' % |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 61 | (intf, vlanName, attrs['vlan'])) |
| 62 | self.cmd('ip link set %s up' % vlanName) |
| 63 | addrIntf = vlanName |
| 64 | else: |
| 65 | addrIntf = intf |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 66 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 67 | # Now configure the addresses on the vlan/native interface |
| 68 | if 'mac' in attrs: |
| 69 | self.cmd('ip link set %s down' % addrIntf) |
| 70 | self.cmd('ip link set %s address %s' % (addrIntf, attrs['mac'])) |
| 71 | self.cmd('ip link set %s up' % addrIntf) |
| 72 | for addr in attrs['ipAddrs']: |
| 73 | self.cmd('ip addr add %s dev %s' % (addr, addrIntf)) |
| 74 | |
| 75 | class QuaggaRouter(Router): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 76 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 77 | """Runs Quagga to create a router that can speak routing protocols.""" |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 78 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 79 | binDir = '/usr/lib/quagga' |
Jonathan Hart | d5d8087 | 2017-01-16 13:55:41 -0800 | [diff] [blame] | 80 | logDir = '/var/log/quagga' |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 81 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 82 | def __init__(self, name, interfaces, |
| 83 | defaultRoute=None, |
| 84 | zebraConfFile=None, |
| 85 | protocols=[], |
| 86 | fpm=None, |
| 87 | runDir='/var/run/quagga', *args, **kwargs): |
| 88 | super(QuaggaRouter, self).__init__(name, interfaces, **kwargs) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 89 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 90 | self.protocols = protocols |
| 91 | self.fpm = fpm |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 92 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 93 | for p in self.protocols: |
| 94 | p.setQuaggaRouter(self) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 95 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 96 | self.runDir = runDir |
| 97 | self.defaultRoute = defaultRoute |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 98 | |
Jonathan Hart | d5d8087 | 2017-01-16 13:55:41 -0800 | [diff] [blame] | 99 | # Ensure required directories exist |
| 100 | try: |
| 101 | original_umask = os.umask(0) |
| 102 | if (not os.path.isdir(QuaggaRouter.logDir)): |
| 103 | os.makedirs(QuaggaRouter.logDir, 0777) |
| 104 | if (not os.path.isdir(self.runDir)): |
| 105 | os.makedirs(self.runDir, 0777) |
| 106 | finally: |
| 107 | os.umask(original_umask) |
| 108 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 109 | self.zebraConfFile = zebraConfFile |
| 110 | if (self.zebraConfFile is None): |
| 111 | self.zebraConfFile = '%s/zebrad%s.conf' % (self.runDir, self.name) |
| 112 | self.generateZebra() |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 113 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 114 | self.socket = '%s/zebra%s.api' % (self.runDir, self.name) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 115 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 116 | self.zebraPidFile = '%s/zebra%s.pid' % (self.runDir, self.name) |
| 117 | |
| 118 | def generateZebra(self): |
| 119 | configFile = open(self.zebraConfFile, 'w+') |
Jonathan Hart | d5d8087 | 2017-01-16 13:55:41 -0800 | [diff] [blame] | 120 | configFile.write('log file %s/zebrad%s.log\n' % (QuaggaRouter.logDir, self.name)) |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 121 | configFile.write('hostname zebra-%s\n' % self.name) |
Jonathan Hart | e6b897f | 2017-01-24 17:09:58 -0800 | [diff] [blame] | 122 | configFile.write('password %s\n' % 'quagga') |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 123 | if (self.fpm is not None): |
| 124 | configFile.write('fpm connection ip %s port 2620' % self.fpm) |
| 125 | configFile.close() |
| 126 | |
| 127 | def config(self, **kwargs): |
| 128 | super(QuaggaRouter, self).config(**kwargs) |
| 129 | |
| 130 | self.cmd('%s/zebra -d -f %s -z %s -i %s' |
| 131 | % (QuaggaRouter.binDir, self.zebraConfFile, self.socket, self.zebraPidFile)) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 132 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 133 | for p in self.protocols: |
| 134 | p.config(**kwargs) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 135 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 136 | if self.defaultRoute: |
| 137 | self.cmd('ip route add default via %s' % self.defaultRoute) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 138 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 139 | def terminate(self, **kwargs): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 140 | self.cmd("ps ax | grep '%s' | awk '{print $1}' | xargs kill" |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 141 | % (self.socket)) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 142 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 143 | for p in self.protocols: |
| 144 | p.terminate(**kwargs) |
| 145 | |
| 146 | super(QuaggaRouter, self).terminate() |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 147 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 148 | class Protocol(object): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 149 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 150 | """Base abstraction of a protocol that the QuaggaRouter can run.""" |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 151 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 152 | def setQuaggaRouter(self, qr): |
| 153 | self.qr = qr |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 154 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 155 | def config(self, **kwargs): |
| 156 | pass |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 157 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 158 | def terminate(self, **kwargs): |
| 159 | pass |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 160 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 161 | class BgpProtocol(Protocol): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 162 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 163 | """Configures and runs the BGP protocol in Quagga.""" |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 164 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 165 | def __init__(self, configFile=None, asNum=None, neighbors=[], routes=[], *args, **kwargs): |
| 166 | self.configFile = configFile |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 167 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 168 | self.asNum = asNum |
| 169 | self.neighbors = neighbors |
| 170 | self.routes = routes |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 171 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 172 | def config(self, **kwargs): |
| 173 | if self.configFile is None: |
| 174 | self.configFile = '%s/bgpd%s.conf' % (self.qr.runDir, self.qr.name) |
| 175 | self.generateConfig() |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 176 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 177 | bgpdPidFile = '%s/bgpd%s.pid' % (self.qr.runDir, self.qr.name) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 178 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 179 | self.qr.cmd('%s/bgpd -d -f %s -z %s -i %s' |
| 180 | % (QuaggaRouter.binDir, self.configFile, self.qr.socket, bgpdPidFile)) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 181 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 182 | def generateConfig(self): |
| 183 | conf = ConfigurationWriter(self.configFile) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 184 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 185 | def getRouterId(interfaces): |
| 186 | intfAttributes = interfaces.itervalues().next() |
| 187 | print intfAttributes |
| 188 | if isinstance(intfAttributes, list): |
| 189 | # Try use the first set of attributes, but if using vlans they might not have addresses |
| 190 | intfAttributes = intfAttributes[1] if not intfAttributes[0]['ipAddrs'] else intfAttributes[0] |
| 191 | return intfAttributes['ipAddrs'][0].split('/')[0] |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 192 | |
Jonathan Hart | d5d8087 | 2017-01-16 13:55:41 -0800 | [diff] [blame] | 193 | conf.writeLine('log file %s/bgpd%s.log' % (QuaggaRouter.logDir, self.qr.name)) |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 194 | conf.writeLine('hostname bgp-%s' % self.qr.name); |
Jonathan Hart | e6b897f | 2017-01-24 17:09:58 -0800 | [diff] [blame] | 195 | conf.writeLine('password %s' % 'quagga') |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 196 | conf.writeLine('!') |
| 197 | conf.writeLine('router bgp %s' % self.asNum) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 198 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 199 | conf.indent() |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 200 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 201 | conf.writeLine('bgp router-id %s' % getRouterId(self.qr.interfaces)) |
| 202 | conf.writeLine('timers bgp %s' % '3 9') |
| 203 | conf.writeLine('!') |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 204 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 205 | for neighbor in self.neighbors: |
| 206 | conf.writeLine('neighbor %s remote-as %s' % (neighbor['address'], neighbor['as'])) |
| 207 | conf.writeLine('neighbor %s ebgp-multihop' % neighbor['address']) |
| 208 | conf.writeLine('neighbor %s timers connect %s' % (neighbor['address'], '5')) |
| 209 | conf.writeLine('neighbor %s advertisement-interval %s' % (neighbor['address'], '5')) |
| 210 | if 'port' in neighbor: |
| 211 | conf.writeLine('neighbor %s port %s' % (neighbor['address'], neighbor['port'])) |
| 212 | conf.writeLine('!') |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 213 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 214 | for route in self.routes: |
| 215 | conf.writeLine('network %s' % route) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 216 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 217 | conf.close() |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 218 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 219 | class OspfProtocol(Protocol): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 220 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 221 | """Configures and runs the OSPF protocol in Quagga.""" |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 222 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 223 | def __init__(self, configFile=None, *args, **kwargs): |
| 224 | self.configFile = configFile |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 225 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 226 | def config(self, **kwargs): |
| 227 | if self.configFile is None: |
| 228 | self.configFile = '%s/ospfd%s.conf' % (self.qr.runDir, self.qr.name) |
| 229 | self.generateConfig() |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 230 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 231 | ospfPidFile = '%s/ospf%s.pid' % (self.qr.runDir, self.qr.name) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 232 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 233 | self.qr.cmd('%s/ospfd -d -f %s -z %s -i %s' |
| 234 | % (QuaggaRouter.binDir, self.configFile, self.qr.socket, ospfPidFile)) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 235 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 236 | def generateConfig(self): |
| 237 | conf = ConfigurationWriter(self.configFile) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 238 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 239 | def getRouterId(interfaces): |
| 240 | intfAttributes = interfaces.itervalues().next() |
| 241 | print intfAttributes |
| 242 | if isinstance(intfAttributes, list): |
| 243 | # Try use the first set of attributes, but if using vlans they might not have addresses |
| 244 | intfAttributes = intfAttributes[1] if not intfAttributes[0]['ipAddrs'] else intfAttributes[0] |
| 245 | return intfAttributes['ipAddrs'][0].split('/')[0] |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 246 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 247 | conf.writeLine('hostname ospf-%s' % self.qr.name); |
| 248 | conf.writeLine('password %s' % 'hello') |
| 249 | conf.writeLine('!') |
| 250 | conf.writeLine('router ospf') |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 251 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 252 | conf.indent() |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 253 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 254 | conf.writeLine('ospf router-id %s' % getRouterId(self.qr.interfaces)) |
| 255 | conf.writeLine('!') |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 256 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 257 | for name, intf in self.qr.interfaces.items(): |
| 258 | for ip in intf['ipAddrs']: |
| 259 | conf.writeLine('network %s area 0' % ip) |
| 260 | #if intf['ipAddrs'][0].startswith('192.168'): |
| 261 | # writeLine(1, 'passive-interface %s' % name) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 262 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 263 | conf.close() |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 264 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 265 | class PimProtocol(Protocol): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 266 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 267 | """Configures and runs the PIM protcol in Quagga.""" |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 268 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 269 | def __init__(self, configFile=None, *args, **kwargs): |
| 270 | self.configFile = configFile |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 271 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 272 | def config(self, **kwargs): |
| 273 | pimPidFile = '%s/pim%s.pid' % (self.qr.runDir, self.qr.name) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 274 | |
Jonathan Hart | 0960859 | 2016-05-19 09:39:22 -0700 | [diff] [blame] | 275 | self.qr.cmd('%s/pimd -Z -d -f %s -z %s -i %s' |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 276 | % (QuaggaRouter.binDir, self.configFile, self.qr.socket, pimPidFile)) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 277 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 278 | class ConfigurationWriter(object): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 279 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 280 | """Utility class for writing a configuration file.""" |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 281 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 282 | def __init__(self, filename): |
| 283 | self.filename = filename |
| 284 | self.indentValue = 0; |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 285 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 286 | self.configFile = open(self.filename, 'w+') |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 287 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 288 | def indent(self): |
| 289 | self.indentValue += 1 |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 290 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 291 | def unindent(self): |
| 292 | if (self.indentValue > 0): |
| 293 | self.indentValue -= 1 |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 294 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 295 | def write(self, string): |
| 296 | self.configFile.write(string) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 297 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 298 | def writeLine(self, string): |
| 299 | intentStr = '' |
| 300 | for _ in range(0, self.indentValue): |
| 301 | intentStr += ' ' |
| 302 | self.write('%s%s\n' % (intentStr, string)) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 303 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 304 | def close(self): |
| 305 | self.configFile.close() |
| 306 | |
| 307 | #Backward compatibility for BGP-only use case |
| 308 | class BgpRouter(QuaggaRouter): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 309 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 310 | """Quagga router running the BGP protocol.""" |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 311 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 312 | def __init__(self, name, interfaces, |
| 313 | asNum, neighbors, routes=[], |
| 314 | defaultRoute=None, |
| 315 | quaggaConfFile=None, |
| 316 | zebraConfFile=None, |
| 317 | *args, **kwargs): |
| 318 | bgp = BgpProtocol(configFile=quaggaConfFile, asNum=asNum, neighbors=neighbors, routes=routes) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 319 | |
| 320 | super(BgpRouter, self).__init__(name, interfaces, |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 321 | zebraConfFile=zebraConfFile, |
| 322 | defaultRoute=defaultRoute, |
| 323 | protocols=[bgp], |
| 324 | *args, **kwargs) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 325 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 326 | class RouterData(object): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 327 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 328 | """Internal data structure storing information about a router.""" |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 329 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 330 | def __init__(self, index): |
| 331 | self.index = index; |
| 332 | self.neighbors = [] |
| 333 | self.interfaces = {} |
| 334 | self.switches = [] |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 335 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 336 | def addNeighbor(self, theirAddress, theirAsNum): |
| 337 | self.neighbors.append({'address':theirAddress.ip, 'as':theirAsNum}) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 338 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 339 | def addInterface(self, intf, vlan, address): |
| 340 | if not intf in self.interfaces: |
| 341 | self.interfaces[intf] = InterfaceData(intf) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 342 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 343 | self.interfaces[intf].addAddress(vlan, address) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 344 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 345 | def setSwitch(self, switch): |
| 346 | self.switches.append(switch) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 347 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 348 | class InterfaceData(object): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 349 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 350 | """Internal data structure storing information about an interface.""" |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 351 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 352 | def __init__(self, number): |
| 353 | self.number = number |
| 354 | self.addressesByVlan = {} |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 355 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 356 | def addAddress(self, vlan, address): |
| 357 | if not vlan in self.addressesByVlan: |
| 358 | self.addressesByVlan[vlan] = [] |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 359 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 360 | self.addressesByVlan[vlan].append(address.with_prefixlen) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 361 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 362 | class RoutedNetwork(object): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 363 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 364 | """Creates a host behind a router. This is common boilerplate topology |
| 365 | segment in routed networks.""" |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 366 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 367 | @staticmethod |
| 368 | def build(topology, router, hostName, networks): |
| 369 | # There's a convention that the router's addresses are already set up, |
| 370 | # and it has the last address in the network. |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 371 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 372 | def getFirstAddress(network): |
| 373 | return '%s/%s' % (network[1], network.prefixlen) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 374 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 375 | defaultRoute = AutonomousSystem.getLastAddress(networks[0]).ip |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 376 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 377 | host = topology.addHost(hostName, cls=RoutedHost, |
| 378 | ips=[getFirstAddress(network) for network in networks], |
| 379 | gateway=defaultRoute) |
| 380 | |
| 381 | topology.addLink(router, host) |
| 382 | |
| 383 | class AutonomousSystem(object): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 384 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 385 | """Base abstraction of an autonomous system, which implies some internal |
| 386 | topology and connections to other topology elements (switches/other ASes).""" |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 387 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 388 | psIdx = 1 |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 389 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 390 | def __init__(self, asNum, numRouters): |
| 391 | self.asNum = asNum |
| 392 | self.numRouters = numRouters |
| 393 | self.routers = {} |
| 394 | for i in range(1, numRouters + 1): |
| 395 | self.routers[i] = RouterData(i) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 396 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 397 | self.routerNodes={} |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 398 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 399 | self.neighbors=[] |
| 400 | self.vlanAddresses={} |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 401 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 402 | def peerWith(self, myRouter, myAddress, theirAddress, theirAsNum, intf=1, vlan=None): |
| 403 | router = self.routers[myRouter] |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 404 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 405 | router.addInterface(intf, vlan, myAddress) |
| 406 | router.addNeighbor(theirAddress, theirAsNum) |
| 407 | |
| 408 | def getRouter(self, i): |
| 409 | return self.routerNodes[i] |
| 410 | |
| 411 | @staticmethod |
| 412 | def generatePeeringAddresses(): |
| 413 | network = ip_network(u'10.0.%s.0/24' % AutonomousSystem.psIdx) |
| 414 | AutonomousSystem.psIdx += 1 |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 415 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 416 | return ip_interface('%s/%s' % (network[1], network.prefixlen)), \ |
| 417 | ip_interface('%s/%s' % (network[2], network.prefixlen)) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 418 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 419 | @staticmethod |
| 420 | def addPeering(as1, as2, router1=1, router2=1, intf1=1, intf2=1, address1=None, address2=None, useVlans=False): |
| 421 | vlan = AutonomousSystem.psIdx if useVlans else None |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 422 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 423 | if address1 is None or address2 is None: |
| 424 | (address1, address2) = AutonomousSystem.generatePeeringAddresses() |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 425 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 426 | as1.peerWith(router1, address1, address2, as2.asNum, intf=intf1, vlan=vlan) |
| 427 | as2.peerWith(router2, address2, address1, as1.asNum, intf=intf2, vlan=vlan) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 428 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 429 | @staticmethod |
| 430 | def getLastAddress(network): |
| 431 | return ip_interface(network.network_address + network.num_addresses - 2) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 432 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 433 | @staticmethod |
| 434 | def getIthAddress(network, i): |
| 435 | return ip_interface('%s/%s' % (network[i], network.prefixlen)) |
| 436 | |
| 437 | class BasicAutonomousSystem(AutonomousSystem): |
| 438 | |
| 439 | """Basic autonomous system containing one host and one or more routers |
| 440 | which peer with other ASes.""" |
| 441 | |
| 442 | def __init__(self, num, routes, numRouters=1): |
| 443 | super(BasicAutonomousSystem, self).__init__(65000+num, numRouters) |
| 444 | self.num = num |
| 445 | self.routes = routes |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 446 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 447 | def addLink(self, switch, router=1): |
| 448 | self.routers[router].setSwitch(switch) |
| 449 | |
| 450 | def build(self, topology): |
| 451 | self.addRouterAndHost(topology) |
| 452 | |
| 453 | def addRouterAndHost(self, topology): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 454 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 455 | # TODO implementation is messy and needs to be cleaned up |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 456 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 457 | intfs = {} |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 458 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 459 | router = self.routers[1] |
| 460 | for i, router in self.routers.items(): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 461 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 462 | #routerName = 'r%i%i' % (self.num, i) |
| 463 | routerName = 'r%i' % self.num |
| 464 | if not i==1: |
| 465 | routerName += ('%i' % i) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 466 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 467 | hostName = 'h%i' % self.num |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 468 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 469 | for j, interface in router.interfaces.items(): |
| 470 | nativeAddresses = interface.addressesByVlan.pop(None, []) |
| 471 | peeringIntf = [{'mac' : '00:00:%02x:00:%02x:%02x' % (self.num, i, j), |
| 472 | 'ipAddrs' : nativeAddresses}] |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 473 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 474 | for vlan, addresses in interface.addressesByVlan.items(): |
| 475 | peeringIntf.append({'vlan':vlan, |
| 476 | 'mac':'00:00:%02x:%02x:%02x:%02x' % (self.num, vlan, i, j), |
| 477 | 'ipAddrs':addresses}) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 478 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 479 | intfs.update({'%s-eth%s' % (routerName, j-1) : peeringIntf}) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 480 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 481 | # Only add the host to the first router for now |
| 482 | if i==1: |
| 483 | internalAddresses=[] |
| 484 | for route in self.routes: |
| 485 | internalAddresses.append('%s/%s' % (AutonomousSystem.getLastAddress(route).ip, route.prefixlen)) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 486 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 487 | internalIntf = {'ipAddrs' : internalAddresses} |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 488 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 489 | # This is the configuration of the next interface after all the peering interfaces |
| 490 | intfs.update({'%s-eth%s' % (routerName, len(router.interfaces.keys())) : internalIntf}) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 491 | |
| 492 | routerNode = topology.addHost(routerName, |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 493 | asNum=self.asNum, neighbors=router.neighbors, |
| 494 | routes=self.routes, |
| 495 | cls=BgpRouter, interfaces=intfs) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 496 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 497 | self.routerNodes[i] = routerNode |
| 498 | |
| 499 | for switch in router.switches: |
| 500 | topology.addLink(switch, routerNode) |
| 501 | |
| 502 | # Only add the host to the first router for now |
| 503 | if i==1: |
| 504 | defaultRoute = internalAddresses[0].split('/')[0] |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 505 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 506 | host = topology.addHost(hostName, cls=RoutedHost, |
| 507 | ips=[self.getFirstAddress(route) for route in self.routes], |
| 508 | gateway=defaultRoute) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 509 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 510 | topology.addLink(routerNode, host) |
| 511 | |
| 512 | #def getLastAddress(self, network): |
| 513 | # return ip_address(network.network_address + network.num_addresses - 2) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 514 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 515 | def getFirstAddress(self, network): |
| 516 | return '%s/%s' % (network[1], network.prefixlen) |
| 517 | |
| 518 | # TODO fix this AS - doesn't currently work |
| 519 | class RouteServerAutonomousSystem(BasicAutonomousSystem): |
| 520 | |
| 521 | def __init__(self, routerAddress, *args, **kwargs): |
| 522 | BasicAutonomousSystem.__init__(self, *args, **kwargs) |
| 523 | |
| 524 | self.routerAddress = routerAddress |
| 525 | |
| 526 | def build(self, topology, connectAtSwitch): |
| 527 | |
| 528 | switch = topology.addSwitch('as%isw' % self.num, cls=OVSBridge) |
| 529 | |
| 530 | self.addRouterAndHost(topology, self.routerAddress, switch) |
| 531 | |
| 532 | rsName = 'rs%i' % self.num |
| 533 | routeServer = topology.addHost(rsName, |
| 534 | self.asnum, self.neighbors, |
| 535 | cls=BgpRouter, |
| 536 | interfaces={'%s-eth0' % rsName : {'ipAddrs':[self.peeringAddress]}}) |
| 537 | |
| 538 | topology.addLink(routeServer, switch) |
| 539 | topology.addLink(switch, connectAtSwitch) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 540 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 541 | class SdnAutonomousSystem(AutonomousSystem): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 542 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 543 | """Runs the internal BGP speakers needed for ONOS routing apps like |
| 544 | SDN-IP.""" |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 545 | |
Jonathan Hart | e6b897f | 2017-01-24 17:09:58 -0800 | [diff] [blame] | 546 | routerIdx = 1 |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 547 | |
Jonathan Hart | e6b897f | 2017-01-24 17:09:58 -0800 | [diff] [blame] | 548 | def __init__(self, onosIps, num=1, numBgpSpeakers=1, asNum=65000, externalOnos=True, |
Jonathan Hart | fc0af77 | 2017-01-16 13:15:08 -0800 | [diff] [blame] | 549 | peerIntfConfig=None, withFpm=False): |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 550 | super(SdnAutonomousSystem, self).__init__(asNum, numBgpSpeakers) |
| 551 | self.onosIps = onosIps |
Jonathan Hart | e6b897f | 2017-01-24 17:09:58 -0800 | [diff] [blame] | 552 | self.num = num |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 553 | self.numBgpSpeakers = numBgpSpeakers |
| 554 | self.peerIntfConfig = peerIntfConfig |
Jonathan Hart | fc0af77 | 2017-01-16 13:15:08 -0800 | [diff] [blame] | 555 | self.withFpm = withFpm |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 556 | self.externalOnos= externalOnos |
| 557 | self.internalPeeringSubnet = ip_network(u'1.1.1.0/24') |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 558 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 559 | for router in self.routers.values(): |
| 560 | # Add iBGP sessions to ONOS nodes |
| 561 | for onosIp in onosIps: |
| 562 | router.neighbors.append({'address':onosIp, 'as':asNum, 'port':2000}) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 563 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 564 | # Add iBGP sessions to other BGP speakers |
| 565 | for i, router2 in self.routers.items(): |
| 566 | if router == router2: |
| 567 | continue |
Jonathan Hart | e6b897f | 2017-01-24 17:09:58 -0800 | [diff] [blame] | 568 | cpIpBase = self.num*10 |
| 569 | ip = AutonomousSystem.getIthAddress(self.internalPeeringSubnet, cpIpBase+i) |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 570 | router.neighbors.append({'address':ip.ip, 'as':asNum}) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 571 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 572 | def build(self, topology, connectAtSwitch, controlSwitch): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 573 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 574 | natIp = AutonomousSystem.getLastAddress(self.internalPeeringSubnet) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 575 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 576 | for i, router in self.routers.items(): |
Jonathan Hart | e6b897f | 2017-01-24 17:09:58 -0800 | [diff] [blame] | 577 | num = SdnAutonomousSystem.routerIdx |
| 578 | SdnAutonomousSystem.routerIdx += 1 |
| 579 | name = 'bgp%s' % num |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 580 | |
Jonathan Hart | e6b897f | 2017-01-24 17:09:58 -0800 | [diff] [blame] | 581 | cpIpBase = self.num*10 |
| 582 | ip = AutonomousSystem.getIthAddress(self.internalPeeringSubnet, cpIpBase+i) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 583 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 584 | eth0 = { 'ipAddrs' : [ str(ip) ] } |
| 585 | if self.peerIntfConfig is not None: |
| 586 | eth1 = self.peerIntfConfig |
| 587 | else: |
| 588 | nativeAddresses = router.interfaces[1].addressesByVlan.pop(None, []) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 589 | eth1 = [{ 'mac':'00:00:00:00:00:%02x' % num, |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 590 | 'ipAddrs' : nativeAddresses }] |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 591 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 592 | for vlan, addresses in router.interfaces[1].addressesByVlan.items(): |
| 593 | eth1.append({'vlan':vlan, |
Jonathan Hart | e6b897f | 2017-01-24 17:09:58 -0800 | [diff] [blame] | 594 | 'mac':'00:00:00:%02x:%02x:00' % (num, vlan), |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 595 | 'ipAddrs':addresses}) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 596 | |
| 597 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 598 | intfs = { '%s-eth0' % name : eth0, |
| 599 | '%s-eth1' % name : eth1 } |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 600 | |
| 601 | bgp = topology.addHost( name, cls=BgpRouter, asNum=self.asNum, |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 602 | neighbors=router.neighbors, |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 603 | interfaces=intfs, |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 604 | defaultRoute=str(natIp.ip), |
Jonathan Hart | fc0af77 | 2017-01-16 13:15:08 -0800 | [diff] [blame] | 605 | fpm=self.onosIps[0] if self.withFpm else None ) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 606 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 607 | topology.addLink( bgp, controlSwitch ) |
| 608 | topology.addLink( bgp, connectAtSwitch ) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 609 | |
| 610 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 611 | if self.externalOnos: |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 612 | nat = topology.addHost('nat', cls=NAT, |
| 613 | ip='%s/%s' % (natIp.ip, self.internalPeeringSubnet.prefixlen), |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 614 | subnet=str(self.internalPeeringSubnet), inNamespace=False); |
| 615 | topology.addLink(controlSwitch, nat) |
| 616 | |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 617 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 618 | def generateRoutes(baseRange, numRoutes, subnetSize=None): |
| 619 | baseNetwork = ip_network(baseRange) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 620 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 621 | # We need to get at least 2 addresses out of each subnet, so the biggest |
| 622 | # prefix length we can have is /30 |
| 623 | maxPrefixLength = baseNetwork.max_prefixlen - 2 |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 624 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 625 | if subnetSize is not None: |
| 626 | return list(baseNetwork.subnets(new_prefix=subnetSize)) |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 627 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 628 | trySubnetSize = baseNetwork.prefixlen + 1 |
| 629 | while trySubnetSize <= maxPrefixLength and \ |
| 630 | len(list(baseNetwork.subnets(new_prefix=trySubnetSize))) < numRoutes: |
| 631 | trySubnetSize += 1 |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 632 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 633 | if trySubnetSize > maxPrefixLength: |
| 634 | raise Exception("Can't get enough routes from input parameters") |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 635 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 636 | return list(baseNetwork.subnets(new_prefix=trySubnetSize))[:numRoutes] |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 637 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 638 | class RoutingCli( CLI ): |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 639 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 640 | """CLI command that can bring a host up or down. Useful for simulating router failure.""" |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 641 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 642 | def do_host( self, line ): |
| 643 | args = line.split() |
| 644 | if len(args) != 2: |
| 645 | error( 'invalid number of args: host <host name> {up, down}\n' ) |
| 646 | return |
Charles Chan | 76128b6 | 2017-03-27 20:28:14 -0700 | [diff] [blame^] | 647 | |
Jonathan Hart | ce97e5b | 2016-04-19 01:41:31 -0700 | [diff] [blame] | 648 | host = args[ 0 ] |
| 649 | command = args[ 1 ] |
| 650 | if host not in self.mn or self.mn.get( host ) not in self.mn.hosts: |
| 651 | error( 'invalid host: %s\n' % args[ 1 ] ) |
| 652 | else: |
| 653 | if command == 'up': |
| 654 | op = 'up' |
| 655 | elif command == 'down': |
| 656 | op = 'down' |
| 657 | else: |
| 658 | error( 'invalid command: host <host name> {up, down}\n' ) |
| 659 | return |
| 660 | |
| 661 | for intf in self.mn.get( host ).intfList( ): |
| 662 | intf.link.intf1.ifconfig( op ) |
| 663 | intf.link.intf2.ifconfig( op ) |