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('..') |
| 9 | from mininet.node import Host |
| 10 | from routinglib import RoutedHost |
| 11 | |
| 12 | class TaggedRoutedHost(RoutedHost): |
| 13 | """Host that can be configured with multiple IP addresses.""" |
| 14 | def __init__(self, name, ips, gateway, vlan, *args, **kwargs): |
| 15 | super(RoutedHost, self).__init__(name, *args, **kwargs) |
| 16 | self.ips = ips |
| 17 | self.gateway = gateway |
| 18 | self.vlan = vlan |
| 19 | self.vlanIntf = None |
| 20 | |
| 21 | def config(self, **kwargs): |
| 22 | Host.config(self, **kwargs) |
| 23 | self.vlanIntf = "%s.%s" % (self.defaultIntf(), self.vlan) |
| 24 | self.cmd('ip -4 addr flush dev %s' % self.defaultIntf()) |
| 25 | self.cmd('ip link add link %s name %s type vlan id %s' % (self.defaultIntf(), self.vlanIntf, self.vlan)) |
| 26 | self.cmd('ip link set up %s' % self.vlanIntf) |
| 27 | |
| 28 | for ip in self.ips: |
| 29 | self.cmd('ip addr add %s dev %s' % (ip, self.vlanIntf)) |
| 30 | |
| 31 | self.cmd('ip route add default via %s' % self.gateway) |
| 32 | |
| 33 | def terminate(self, **kwargs): |
| 34 | self.cmd('ip link remove link %s' % self.vlanIntf) |
| 35 | super(TaggedRoutedHost, self).terminate() |
| 36 | |
| 37 | class DhcpClient(Host): |
| 38 | def __init__(self, name, *args, **kwargs): |
| 39 | super(DhcpClient, self).__init__(name, **kwargs) |
| 40 | self.pidFile = '/run/dhclient-%s.pid' % self.name |
| 41 | |
| 42 | def config(self, **kwargs): |
| 43 | super(DhcpClient, self).config(**kwargs) |
| 44 | self.cmd('ip addr flush dev %s' % self.defaultIntf()) |
| 45 | self.cmd('dhclient -q -4 -nw -pf %s %s' % (self.pidFile, self.defaultIntf())) |
| 46 | |
| 47 | def terminate(self, **kwargs): |
| 48 | self.cmd('kill -9 `cat %s`' % self.pidFile) |
| 49 | self.cmd('rm -rf %s' % self.pidFile) |
| 50 | super(DhcpClient, self).terminate() |
| 51 | |
| 52 | class DhcpServer(RoutedHost): |
| 53 | binFile = '/usr/sbin/dhcpd' |
| 54 | pidFile = '/run/dhcp-server/dhcpd.pid' |
| 55 | configFile = './dhcpd.conf' |
| 56 | |
| 57 | def config(self, **kwargs): |
| 58 | super(DhcpServer, self).config(**kwargs) |
| 59 | self.cmd('%s -q -4 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.defaultIntf())) |
| 60 | |
| 61 | def terminate(self, **kwargs): |
| 62 | self.cmd('kill -9 `cat %s`' % self.pidFile) |
| 63 | self.cmd('rm -rf %s' % self.pidFile) |
| 64 | super(DhcpServer, self).terminate() |
| 65 | |
| 66 | class TaggedDhcpClient(Host): |
| 67 | def __init__(self, name, vlan, *args, **kwargs): |
| 68 | super(TaggedDhcpClient, self).__init__(name, **kwargs) |
| 69 | self.pidFile = '/run/dhclient-%s.pid' % self.name |
| 70 | self.vlan = vlan |
| 71 | self.vlanIntf = None |
| 72 | |
| 73 | def config(self, **kwargs): |
| 74 | super(TaggedDhcpClient, self).config(**kwargs) |
| 75 | self.vlanIntf = "%s.%s" % (self.defaultIntf(), self.vlan) |
| 76 | self.cmd('ip addr flush dev %s' % self.defaultIntf()) |
| 77 | self.cmd('ip link add link %s name %s type vlan id %s' % (self.defaultIntf(), self.vlanIntf, self.vlan)) |
| 78 | self.cmd('ip link set up %s' % self.vlanIntf) |
| 79 | self.cmd('dhclient -q -4 -nw -pf %s %s' % (self.pidFile, self.vlanIntf)) |
| 80 | |
| 81 | def terminate(self, **kwargs): |
| 82 | self.cmd('kill -9 `cat %s`' % self.pidFile) |
| 83 | self.cmd('rm -rf %s' % self.pidFile) |
| 84 | self.cmd('ip link remove link %s' % self.vlanIntf) |
| 85 | super(TaggedDhcpClient, self).terminate() |
| 86 | |
| 87 | class TaggedDhcpServer(TaggedRoutedHost): |
| 88 | binFile = '/usr/sbin/dhcpd' |
| 89 | pidFile = '/run/dhcp-server/dhcpd.pid' |
| 90 | configFile = './dhcpd.conf' |
| 91 | |
| 92 | def config(self, **kwargs): |
| 93 | super(TaggedDhcpServer, self).config(**kwargs) |
| 94 | self.cmd('%s -q -4 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.vlanIntf)) |
| 95 | |
| 96 | def terminate(self, **kwargs): |
| 97 | self.cmd('kill -9 `cat %s`' % self.pidFile) |
| 98 | self.cmd('rm -rf %s' % self.pidFile) |
| 99 | super(TaggedDhcpServer, self).terminate() |
| 100 | |
| 101 | class DualHomedDhcpClient(Host): |
| 102 | def __init__(self, name, *args, **kwargs): |
| 103 | super(DualHomedDhcpClient, self).__init__(name, **kwargs) |
| 104 | self.pidFile = '/run/dhclient-%s.pid' % self.name |
| 105 | self.bond0 = None |
| 106 | |
| 107 | def config(self, **kwargs): |
| 108 | super(DualHomedDhcpClient, self).config(**kwargs) |
| 109 | intf0 = self.intfs[0].name |
| 110 | intf1 = self.intfs[1].name |
| 111 | self.bond0 = "%s-bond0" % self.name |
| 112 | self.cmd('modprobe bonding') |
| 113 | self.cmd('ip link add %s type bond' % self.bond0) |
| 114 | self.cmd('ip link set %s down' % intf0) |
| 115 | self.cmd('ip link set %s down' % intf1) |
| 116 | self.cmd('ip link set %s master %s' % (intf0, self.bond0)) |
| 117 | self.cmd('ip link set %s master %s' % (intf1, self.bond0)) |
| 118 | self.cmd('ip addr flush dev %s' % intf0) |
| 119 | self.cmd('ip addr flush dev %s' % intf1) |
| 120 | self.cmd('ip link set %s up' % self.bond0) |
| 121 | self.cmd('dhclient -q -4 -nw -pf %s %s' % (self.pidFile, self.bond0)) |
| 122 | |
| 123 | def terminate(self, **kwargs): |
| 124 | self.cmd('ip link set %s down' % self.bond0) |
| 125 | self.cmd('ip link delete %s' % self.bond0) |
| 126 | self.cmd('kill -9 `cat %s`' % self.pidFile) |
| 127 | self.cmd('rm -rf %s' % self.pidFile) |
| 128 | super(DualHomedDhcpClient, self).terminate() |