Charles Chan | ec57b95 | 2017-04-24 17:05:06 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """ |
| 4 | Libraries for Trellis hosts. |
| 5 | """ |
| 6 | |
| 7 | import sys |
| 8 | sys.path.append('..') |
Ray Milkey | 1df260d | 2018-03-28 11:52:34 -0700 | [diff] [blame] | 9 | from mininet.node import Host, RemoteController |
Yi Tseng | 45ee692 | 2017-07-17 14:49:17 -0700 | [diff] [blame] | 10 | from routinglib import RoutedHost, RoutedHost6, Router |
Ray Milkey | 1df260d | 2018-03-28 11:52:34 -0700 | [diff] [blame] | 11 | import argparse |
| 12 | from mininet.net import Mininet |
Charles Chan | ec57b95 | 2017-04-24 17:05:06 -0700 | [diff] [blame] | 13 | |
| 14 | class TaggedRoutedHost(RoutedHost): |
| 15 | """Host that can be configured with multiple IP addresses.""" |
| 16 | def __init__(self, name, ips, gateway, vlan, *args, **kwargs): |
| 17 | super(RoutedHost, self).__init__(name, *args, **kwargs) |
| 18 | self.ips = ips |
| 19 | self.gateway = gateway |
| 20 | self.vlan = vlan |
| 21 | self.vlanIntf = None |
| 22 | |
| 23 | def config(self, **kwargs): |
| 24 | Host.config(self, **kwargs) |
| 25 | self.vlanIntf = "%s.%s" % (self.defaultIntf(), self.vlan) |
| 26 | self.cmd('ip -4 addr flush dev %s' % self.defaultIntf()) |
| 27 | self.cmd('ip link add link %s name %s type vlan id %s' % (self.defaultIntf(), self.vlanIntf, self.vlan)) |
| 28 | self.cmd('ip link set up %s' % self.vlanIntf) |
| 29 | |
| 30 | for ip in self.ips: |
| 31 | self.cmd('ip addr add %s dev %s' % (ip, self.vlanIntf)) |
| 32 | |
| 33 | self.cmd('ip route add default via %s' % self.gateway) |
| 34 | |
| 35 | def terminate(self, **kwargs): |
| 36 | self.cmd('ip link remove link %s' % self.vlanIntf) |
| 37 | super(TaggedRoutedHost, self).terminate() |
| 38 | |
| 39 | class DhcpClient(Host): |
| 40 | def __init__(self, name, *args, **kwargs): |
| 41 | super(DhcpClient, self).__init__(name, **kwargs) |
| 42 | self.pidFile = '/run/dhclient-%s.pid' % self.name |
Yi Tseng | 45ee692 | 2017-07-17 14:49:17 -0700 | [diff] [blame] | 43 | self.leaseFile = '/var/lib/dhcp/dhcpclient-%s.lease' % (self.name, ) |
Charles Chan | ec57b95 | 2017-04-24 17:05:06 -0700 | [diff] [blame] | 44 | |
| 45 | def config(self, **kwargs): |
| 46 | super(DhcpClient, self).config(**kwargs) |
| 47 | self.cmd('ip addr flush dev %s' % self.defaultIntf()) |
Yi Tseng | 45ee692 | 2017-07-17 14:49:17 -0700 | [diff] [blame] | 48 | self.cmd('dhclient -q -4 -nw -pf %s -lf %s %s' % (self.pidFile, self.leaseFile, self.defaultIntf())) |
Charles Chan | ec57b95 | 2017-04-24 17:05:06 -0700 | [diff] [blame] | 49 | |
| 50 | def terminate(self, **kwargs): |
| 51 | self.cmd('kill -9 `cat %s`' % self.pidFile) |
| 52 | self.cmd('rm -rf %s' % self.pidFile) |
| 53 | super(DhcpClient, self).terminate() |
| 54 | |
Yi Tseng | 90109fb | 2017-07-17 14:48:15 -0700 | [diff] [blame] | 55 | class Dhcp6Client(Host): |
| 56 | def __init__(self, name, *args, **kwargs): |
| 57 | super(Dhcp6Client, self).__init__(name, **kwargs) |
| 58 | self.pidFile = '/run/dhclient-%s.pid' % self.name |
Yi Tseng | 45ee692 | 2017-07-17 14:49:17 -0700 | [diff] [blame] | 59 | self.leaseFile = '/var/lib/dhcp/dhcpclient6-%s.lease' % (self.name, ) |
Yi Tseng | 90109fb | 2017-07-17 14:48:15 -0700 | [diff] [blame] | 60 | |
| 61 | def config(self, **kwargs): |
| 62 | super(Dhcp6Client, self).config(**kwargs) |
Charles Chan | facc506 | 2017-10-23 19:45:23 -0700 | [diff] [blame] | 63 | self.cmd('ip -4 addr flush dev %s' % self.defaultIntf()) |
Yi Tseng | 45ee692 | 2017-07-17 14:49:17 -0700 | [diff] [blame] | 64 | self.cmd('dhclient -q -6 -nw -pf %s -lf %s %s' % (self.pidFile, self.leaseFile, self.defaultIntf())) |
Yi Tseng | 90109fb | 2017-07-17 14:48:15 -0700 | [diff] [blame] | 65 | |
| 66 | def terminate(self, **kwargs): |
| 67 | self.cmd('kill -9 `cat %s`' % self.pidFile) |
| 68 | self.cmd('rm -rf %s' % self.pidFile) |
| 69 | super(Dhcp6Client, self).terminate() |
| 70 | |
Charles Chan | ec57b95 | 2017-04-24 17:05:06 -0700 | [diff] [blame] | 71 | class DhcpServer(RoutedHost): |
| 72 | binFile = '/usr/sbin/dhcpd' |
Yi Tseng | 45ee692 | 2017-07-17 14:49:17 -0700 | [diff] [blame] | 73 | pidFile = '/run/dhcp-server-dhcpd.pid' |
Charles Chan | ec57b95 | 2017-04-24 17:05:06 -0700 | [diff] [blame] | 74 | configFile = './dhcpd.conf' |
Yi Tseng | 45ee692 | 2017-07-17 14:49:17 -0700 | [diff] [blame] | 75 | leasesFile = '/var/lib/dhcp/dhcpd.leases' |
Charles Chan | ec57b95 | 2017-04-24 17:05:06 -0700 | [diff] [blame] | 76 | |
| 77 | def config(self, **kwargs): |
| 78 | super(DhcpServer, self).config(**kwargs) |
Yi Tseng | 45ee692 | 2017-07-17 14:49:17 -0700 | [diff] [blame] | 79 | self.cmd('touch %s' % self.leasesFile) |
Charles Chan | ec57b95 | 2017-04-24 17:05:06 -0700 | [diff] [blame] | 80 | self.cmd('%s -q -4 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.defaultIntf())) |
| 81 | |
| 82 | def terminate(self, **kwargs): |
| 83 | self.cmd('kill -9 `cat %s`' % self.pidFile) |
| 84 | self.cmd('rm -rf %s' % self.pidFile) |
| 85 | super(DhcpServer, self).terminate() |
| 86 | |
Yi Tseng | 45ee692 | 2017-07-17 14:49:17 -0700 | [diff] [blame] | 87 | class Dhcp6Server(RoutedHost6): |
Yi Tseng | 90109fb | 2017-07-17 14:48:15 -0700 | [diff] [blame] | 88 | binFile = '/usr/sbin/dhcpd' |
Yi Tseng | 45ee692 | 2017-07-17 14:49:17 -0700 | [diff] [blame] | 89 | pidFile = '/run/dhcp-server-dhcpd6.pid' |
Yi Tseng | 90109fb | 2017-07-17 14:48:15 -0700 | [diff] [blame] | 90 | configFile = './dhcpd6.conf' |
| 91 | leasesFile = '/var/lib/dhcp/dhcpd6.leases' |
| 92 | |
| 93 | def config(self, **kwargs): |
| 94 | super(Dhcp6Server, self).config(**kwargs) |
Yi Tseng | 15375ea | 2017-07-26 16:34:09 -0700 | [diff] [blame] | 95 | linkLocalAddr = mac_to_ipv6_linklocal(kwargs['mac']) |
| 96 | self.cmd('ip -6 addr add dev %s scope link %s' % (self.defaultIntf(), linkLocalAddr)) |
Yi Tseng | 90109fb | 2017-07-17 14:48:15 -0700 | [diff] [blame] | 97 | self.cmd('touch %s' % self.leasesFile) |
| 98 | self.cmd('%s -q -6 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.defaultIntf())) |
| 99 | |
| 100 | def terminate(self, **kwargs): |
| 101 | self.cmd('kill -9 `cat %s`' % self.pidFile) |
| 102 | self.cmd('rm -rf %s' % self.pidFile) |
| 103 | self.cmd('rm -rf %s' % self.leasesFile) |
Yi Tseng | 15375ea | 2017-07-26 16:34:09 -0700 | [diff] [blame] | 104 | super(Dhcp6Server, self).terminate() |
Yi Tseng | 90109fb | 2017-07-17 14:48:15 -0700 | [diff] [blame] | 105 | |
Yi Tseng | 9e332d8 | 2017-07-28 17:52:21 -0700 | [diff] [blame] | 106 | class DhcpRelay(Router): |
| 107 | binFile = '/usr/sbin/dhcrelay' |
| 108 | pidFile = '/run/dhcp-relay.pid' |
| 109 | serverIp = None |
| 110 | gateway = None |
| 111 | |
| 112 | def __init__(self, name, serverIp, gateway, *args, **kwargs): |
| 113 | super(DhcpRelay, self).__init__(name, **kwargs) |
| 114 | self.serverIp = serverIp |
| 115 | self.gateway = gateway |
| 116 | |
| 117 | def config(self, **kwargs): |
| 118 | super(DhcpRelay, self).config(**kwargs) |
| 119 | ifacesStr = ' '.join(["-i " + ifaceName for ifaceName in self.interfaces.keys()]) |
| 120 | self.cmd('route add default gw %s' % self.gateway) |
| 121 | self.cmd('%s -4 -a -pf %s %s %s' % (self.binFile, self.pidFile, ifacesStr, self.serverIp)) |
| 122 | |
| 123 | def terminate(self, **kwargs): |
| 124 | self.cmd('kill -9 `cat %s`', self.pidFile) |
| 125 | self.cmd('rm -rf %s' % self.pidFile) |
| 126 | super(DhcpRelay, self).terminate() |
| 127 | |
Charles Chan | ec57b95 | 2017-04-24 17:05:06 -0700 | [diff] [blame] | 128 | class TaggedDhcpClient(Host): |
| 129 | def __init__(self, name, vlan, *args, **kwargs): |
| 130 | super(TaggedDhcpClient, self).__init__(name, **kwargs) |
| 131 | self.pidFile = '/run/dhclient-%s.pid' % self.name |
| 132 | self.vlan = vlan |
| 133 | self.vlanIntf = None |
| 134 | |
| 135 | def config(self, **kwargs): |
| 136 | super(TaggedDhcpClient, self).config(**kwargs) |
| 137 | self.vlanIntf = "%s.%s" % (self.defaultIntf(), self.vlan) |
| 138 | self.cmd('ip addr flush dev %s' % self.defaultIntf()) |
| 139 | self.cmd('ip link add link %s name %s type vlan id %s' % (self.defaultIntf(), self.vlanIntf, self.vlan)) |
| 140 | self.cmd('ip link set up %s' % self.vlanIntf) |
| 141 | self.cmd('dhclient -q -4 -nw -pf %s %s' % (self.pidFile, self.vlanIntf)) |
| 142 | |
| 143 | def terminate(self, **kwargs): |
| 144 | self.cmd('kill -9 `cat %s`' % self.pidFile) |
| 145 | self.cmd('rm -rf %s' % self.pidFile) |
| 146 | self.cmd('ip link remove link %s' % self.vlanIntf) |
| 147 | super(TaggedDhcpClient, self).terminate() |
| 148 | |
| 149 | class TaggedDhcpServer(TaggedRoutedHost): |
| 150 | binFile = '/usr/sbin/dhcpd' |
| 151 | pidFile = '/run/dhcp-server/dhcpd.pid' |
| 152 | configFile = './dhcpd.conf' |
| 153 | |
| 154 | def config(self, **kwargs): |
| 155 | super(TaggedDhcpServer, self).config(**kwargs) |
| 156 | self.cmd('%s -q -4 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.vlanIntf)) |
| 157 | |
| 158 | def terminate(self, **kwargs): |
| 159 | self.cmd('kill -9 `cat %s`' % self.pidFile) |
| 160 | self.cmd('rm -rf %s' % self.pidFile) |
| 161 | super(TaggedDhcpServer, self).terminate() |
| 162 | |
| 163 | class DualHomedDhcpClient(Host): |
| 164 | def __init__(self, name, *args, **kwargs): |
| 165 | super(DualHomedDhcpClient, self).__init__(name, **kwargs) |
| 166 | self.pidFile = '/run/dhclient-%s.pid' % self.name |
| 167 | self.bond0 = None |
| 168 | |
| 169 | def config(self, **kwargs): |
| 170 | super(DualHomedDhcpClient, self).config(**kwargs) |
| 171 | intf0 = self.intfs[0].name |
| 172 | intf1 = self.intfs[1].name |
| 173 | self.bond0 = "%s-bond0" % self.name |
| 174 | self.cmd('modprobe bonding') |
| 175 | self.cmd('ip link add %s type bond' % self.bond0) |
| 176 | self.cmd('ip link set %s down' % intf0) |
| 177 | self.cmd('ip link set %s down' % intf1) |
| 178 | self.cmd('ip link set %s master %s' % (intf0, self.bond0)) |
| 179 | self.cmd('ip link set %s master %s' % (intf1, self.bond0)) |
| 180 | self.cmd('ip addr flush dev %s' % intf0) |
| 181 | self.cmd('ip addr flush dev %s' % intf1) |
| 182 | self.cmd('ip link set %s up' % self.bond0) |
| 183 | self.cmd('dhclient -q -4 -nw -pf %s %s' % (self.pidFile, self.bond0)) |
| 184 | |
| 185 | def terminate(self, **kwargs): |
| 186 | self.cmd('ip link set %s down' % self.bond0) |
| 187 | self.cmd('ip link delete %s' % self.bond0) |
| 188 | self.cmd('kill -9 `cat %s`' % self.pidFile) |
| 189 | self.cmd('rm -rf %s' % self.pidFile) |
| 190 | super(DualHomedDhcpClient, self).terminate() |
Yi Tseng | 15375ea | 2017-07-26 16:34:09 -0700 | [diff] [blame] | 191 | |
| 192 | # Utility for IPv6 |
| 193 | def mac_to_ipv6_linklocal(mac): |
| 194 | ''' |
| 195 | Convert mac address to link-local IPv6 address |
| 196 | ''' |
| 197 | # Remove the most common delimiters; dots, dashes, etc. |
| 198 | mac_value = int(mac.translate(None, ' .:-'), 16) |
| 199 | |
| 200 | # Split out the bytes that slot into the IPv6 address |
| 201 | # XOR the most significant byte with 0x02, inverting the |
| 202 | # Universal / Local bit |
| 203 | high2 = mac_value >> 32 & 0xffff ^ 0x0200 |
| 204 | high1 = mac_value >> 24 & 0xff |
| 205 | low1 = mac_value >> 16 & 0xff |
| 206 | low2 = mac_value & 0xffff |
| 207 | |
| 208 | return 'fe80::{:04x}:{:02x}ff:fe{:02x}:{:04x}'.format(high2, high1, low1, low2) |
Ray Milkey | 1df260d | 2018-03-28 11:52:34 -0700 | [diff] [blame] | 209 | |
| 210 | # Parses Trellis parameters |
| 211 | def parse_trellis_args(): |
| 212 | parser = argparse.ArgumentParser(description="Trellis Arguments") |
| 213 | parser.add_argument("-c", "--controllers", help = "Comma Separated List of ONOS controllers", |
| 214 | required = True, default = "") |
| 215 | return parser.parse_args() |
| 216 | |
| 217 | # Gets a mininet instance |
| 218 | def get_mininet(arguments, topo, switch): |
| 219 | net = Mininet(topo=topo, controller=None, switch=switch) |
| 220 | |
| 221 | if arguments.controllers: |
| 222 | controllers = arguments.controllers.split(',') |
| 223 | controller_number = 0 |
| 224 | for controller in controllers: |
| 225 | net.addController(RemoteController('c' + str(controller_number), ip=controller)) |
| 226 | controller_number += 1 |
| 227 | return net |
| 228 | |
| 229 | # Generates the Zebra config files |
| 230 | def set_up_zebra_config(controllers_string): |
| 231 | zebra_config = "log file /var/log/quagga/zebradbgp{}.log\n" \ |
| 232 | "hostname zebra-bgp{}\n" \ |
| 233 | "password quagga\n" \ |
| 234 | "!\n" \ |
| 235 | "! Default route via virtual management switch\n" \ |
| 236 | "!\n" \ |
| 237 | "ip route 0.0.0.0/0 172.16.0.1\n" \ |
| 238 | "!\n" \ |
| 239 | "fpm connection ip {} port 2620\n" |
| 240 | controllers = controllers_string.split(',') |
| 241 | |
| 242 | controller1 = controllers[0] |
| 243 | if (len(controllers) > 1): |
| 244 | controller2 = controllers[1] |
| 245 | else: |
| 246 | controller2 = controller1 |
| 247 | |
| 248 | |
| 249 | zebra1 = zebra_config.format("1", "1", controller1) |
| 250 | zebra2 = zebra_config.format("2", "2", controller2) |
| 251 | |
| 252 | with open("zebradbgp1.conf", "w") as config_file_1: |
| 253 | config_file_1.write(zebra1) |
| 254 | |
| 255 | with open("zebradbgp2.conf", "w") as config_file_2: |
| 256 | config_file_2.write(zebra2) |
| 257 | |