blob: 72429087dced2f628ca7e92823589a6b6bd2156a [file] [log] [blame]
Charles Chanec57b952017-04-24 17:05:06 -07001#!/usr/bin/python
2
3"""
4Libraries for Trellis hosts.
5"""
6
7import sys
8sys.path.append('..')
9from mininet.node import Host
Yi Tseng9e332d82017-07-28 17:52:21 -070010from routinglib import RoutedHost, Router
Charles Chanec57b952017-04-24 17:05:06 -070011
12class 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
37class 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
Yi Tseng90109fb2017-07-17 14:48:15 -070052class Dhcp6Client(Host):
53 def __init__(self, name, *args, **kwargs):
54 super(Dhcp6Client, self).__init__(name, **kwargs)
55 self.pidFile = '/run/dhclient-%s.pid' % self.name
56
57 def config(self, **kwargs):
58 super(Dhcp6Client, self).config(**kwargs)
59 self.cmd('ip addr flush dev %s' % self.defaultIntf())
Yi Tseng15375ea2017-07-26 16:34:09 -070060 linkLocalAddr = mac_to_ipv6_linklocal(kwargs['mac'])
61 self.cmd('ip -6 addr add dev %s scope link %s' % (self.defaultIntf(), linkLocalAddr))
Yi Tseng90109fb2017-07-17 14:48:15 -070062 self.cmd('dhclient -q -6 -nw -pf %s %s' % (self.pidFile, self.defaultIntf()))
63
64 def terminate(self, **kwargs):
65 self.cmd('kill -9 `cat %s`' % self.pidFile)
66 self.cmd('rm -rf %s' % self.pidFile)
67 super(Dhcp6Client, self).terminate()
68
Charles Chanec57b952017-04-24 17:05:06 -070069class DhcpServer(RoutedHost):
70 binFile = '/usr/sbin/dhcpd'
71 pidFile = '/run/dhcp-server/dhcpd.pid'
72 configFile = './dhcpd.conf'
73
74 def config(self, **kwargs):
75 super(DhcpServer, self).config(**kwargs)
76 self.cmd('%s -q -4 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.defaultIntf()))
77
78 def terminate(self, **kwargs):
79 self.cmd('kill -9 `cat %s`' % self.pidFile)
80 self.cmd('rm -rf %s' % self.pidFile)
81 super(DhcpServer, self).terminate()
82
Yi Tseng90109fb2017-07-17 14:48:15 -070083class Dhcp6Server(RoutedHost):
84 binFile = '/usr/sbin/dhcpd'
85 pidFile = '/run/dhcp-server/dhcpd6.pid'
86 configFile = './dhcpd6.conf'
87 leasesFile = '/var/lib/dhcp/dhcpd6.leases'
88
89 def config(self, **kwargs):
90 super(Dhcp6Server, self).config(**kwargs)
Yi Tseng15375ea2017-07-26 16:34:09 -070091 linkLocalAddr = mac_to_ipv6_linklocal(kwargs['mac'])
92 self.cmd('ip -6 addr add dev %s scope link %s' % (self.defaultIntf(), linkLocalAddr))
Yi Tseng90109fb2017-07-17 14:48:15 -070093 self.cmd('touch %s' % self.leasesFile)
94 self.cmd('%s -q -6 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.defaultIntf()))
95
96 def terminate(self, **kwargs):
97 self.cmd('kill -9 `cat %s`' % self.pidFile)
98 self.cmd('rm -rf %s' % self.pidFile)
99 self.cmd('rm -rf %s' % self.leasesFile)
Yi Tseng15375ea2017-07-26 16:34:09 -0700100 super(Dhcp6Server, self).terminate()
Yi Tseng90109fb2017-07-17 14:48:15 -0700101
Yi Tseng9e332d82017-07-28 17:52:21 -0700102class DhcpRelay(Router):
103 binFile = '/usr/sbin/dhcrelay'
104 pidFile = '/run/dhcp-relay.pid'
105 serverIp = None
106 gateway = None
107
108 def __init__(self, name, serverIp, gateway, *args, **kwargs):
109 super(DhcpRelay, self).__init__(name, **kwargs)
110 self.serverIp = serverIp
111 self.gateway = gateway
112
113 def config(self, **kwargs):
114 super(DhcpRelay, self).config(**kwargs)
115 ifacesStr = ' '.join(["-i " + ifaceName for ifaceName in self.interfaces.keys()])
116 self.cmd('route add default gw %s' % self.gateway)
117 self.cmd('%s -4 -a -pf %s %s %s' % (self.binFile, self.pidFile, ifacesStr, self.serverIp))
118
119 def terminate(self, **kwargs):
120 self.cmd('kill -9 `cat %s`', self.pidFile)
121 self.cmd('rm -rf %s' % self.pidFile)
122 super(DhcpRelay, self).terminate()
123
Charles Chanec57b952017-04-24 17:05:06 -0700124class TaggedDhcpClient(Host):
125 def __init__(self, name, vlan, *args, **kwargs):
126 super(TaggedDhcpClient, self).__init__(name, **kwargs)
127 self.pidFile = '/run/dhclient-%s.pid' % self.name
128 self.vlan = vlan
129 self.vlanIntf = None
130
131 def config(self, **kwargs):
132 super(TaggedDhcpClient, self).config(**kwargs)
133 self.vlanIntf = "%s.%s" % (self.defaultIntf(), self.vlan)
134 self.cmd('ip addr flush dev %s' % self.defaultIntf())
135 self.cmd('ip link add link %s name %s type vlan id %s' % (self.defaultIntf(), self.vlanIntf, self.vlan))
136 self.cmd('ip link set up %s' % self.vlanIntf)
137 self.cmd('dhclient -q -4 -nw -pf %s %s' % (self.pidFile, self.vlanIntf))
138
139 def terminate(self, **kwargs):
140 self.cmd('kill -9 `cat %s`' % self.pidFile)
141 self.cmd('rm -rf %s' % self.pidFile)
142 self.cmd('ip link remove link %s' % self.vlanIntf)
143 super(TaggedDhcpClient, self).terminate()
144
145class TaggedDhcpServer(TaggedRoutedHost):
146 binFile = '/usr/sbin/dhcpd'
147 pidFile = '/run/dhcp-server/dhcpd.pid'
148 configFile = './dhcpd.conf'
149
150 def config(self, **kwargs):
151 super(TaggedDhcpServer, self).config(**kwargs)
152 self.cmd('%s -q -4 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.vlanIntf))
153
154 def terminate(self, **kwargs):
155 self.cmd('kill -9 `cat %s`' % self.pidFile)
156 self.cmd('rm -rf %s' % self.pidFile)
157 super(TaggedDhcpServer, self).terminate()
158
159class DualHomedDhcpClient(Host):
160 def __init__(self, name, *args, **kwargs):
161 super(DualHomedDhcpClient, self).__init__(name, **kwargs)
162 self.pidFile = '/run/dhclient-%s.pid' % self.name
163 self.bond0 = None
164
165 def config(self, **kwargs):
166 super(DualHomedDhcpClient, self).config(**kwargs)
167 intf0 = self.intfs[0].name
168 intf1 = self.intfs[1].name
169 self.bond0 = "%s-bond0" % self.name
170 self.cmd('modprobe bonding')
171 self.cmd('ip link add %s type bond' % self.bond0)
172 self.cmd('ip link set %s down' % intf0)
173 self.cmd('ip link set %s down' % intf1)
174 self.cmd('ip link set %s master %s' % (intf0, self.bond0))
175 self.cmd('ip link set %s master %s' % (intf1, self.bond0))
176 self.cmd('ip addr flush dev %s' % intf0)
177 self.cmd('ip addr flush dev %s' % intf1)
178 self.cmd('ip link set %s up' % self.bond0)
179 self.cmd('dhclient -q -4 -nw -pf %s %s' % (self.pidFile, self.bond0))
180
181 def terminate(self, **kwargs):
182 self.cmd('ip link set %s down' % self.bond0)
183 self.cmd('ip link delete %s' % self.bond0)
184 self.cmd('kill -9 `cat %s`' % self.pidFile)
185 self.cmd('rm -rf %s' % self.pidFile)
186 super(DualHomedDhcpClient, self).terminate()
Yi Tseng15375ea2017-07-26 16:34:09 -0700187
188# Utility for IPv6
189def mac_to_ipv6_linklocal(mac):
190 '''
191 Convert mac address to link-local IPv6 address
192 '''
193 # Remove the most common delimiters; dots, dashes, etc.
194 mac_value = int(mac.translate(None, ' .:-'), 16)
195
196 # Split out the bytes that slot into the IPv6 address
197 # XOR the most significant byte with 0x02, inverting the
198 # Universal / Local bit
199 high2 = mac_value >> 32 & 0xffff ^ 0x0200
200 high1 = mac_value >> 24 & 0xff
201 low1 = mac_value >> 16 & 0xff
202 low2 = mac_value & 0xffff
203
204 return 'fe80::{:04x}:{:02x}ff:fe{:02x}:{:04x}'.format(high2, high1, low1, low2)