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