blob: 08dcf2f56b716ba1f7273c820735773e9901ba59 [file] [log] [blame]
Jonghwan Hyun3731d6a2017-10-19 11:59:31 -07001#!/usr/bin/python
2
3"""
4Libraries for Trellis hosts.
5"""
6
7import sys
8sys.path.append('..')
9from mininet.node import Host
10from routinglib import RoutedHost, RoutedHost6, Router
11
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 intf = self.defaultIntf()
24 self.vlanIntf = "%s.%s" % (intf, self.vlan)
25 self.cmd('ip -4 addr flush dev %s' % intf)
26 self.cmd('ip link add link %s name %s type vlan id %s' % (intf, self.vlanIntf, self.vlan))
27 self.cmd('ip link set up %s' % self.vlanIntf)
28
29 for ip in self.ips:
30 self.cmd('ip addr add %s dev %s' % (ip, self.vlanIntf))
31
32 self.cmd('ip route add default via %s' % self.gateway)
33 intf.name = self.vlanIntf
34 self.nameToIntf[self.vlanIntf] = intf
35
36 def terminate(self, **kwargs):
37 self.cmd('ip link remove link %s' % self.vlanIntf)
38 super(TaggedRoutedHost, self).terminate()
39
40class DualHomedRoutedHost(Host):
41 def __init__(self, name, ips, gateway, *args, **kwargs):
42 super(DualHomedRoutedHost, self).__init__(name, **kwargs)
43 self.bond0 = None
44 self.ips = ips
45 self.gateway = gateway
46
47 def config(self, **kwargs):
48 super(DualHomedRoutedHost, self).config(**kwargs)
49 intf0 = self.intfs[0].name
50 intf1 = self.intfs[1].name
51 self.bond0 = "%s-bond0" % self.name
52 self.cmd('modprobe bonding')
53 self.cmd('ip link add %s type bond' % self.bond0)
54 self.cmd('ip link set %s down' % intf0)
55 self.cmd('ip link set %s down' % intf1)
56 self.cmd('ip link set %s master %s' % (intf0, self.bond0))
57 self.cmd('ip link set %s master %s' % (intf1, self.bond0))
58 self.cmd('ip addr flush dev %s' % intf0)
59 self.cmd('ip addr flush dev %s' % intf1)
60 self.cmd('ip link set %s up' % self.bond0)
61
62 for ip in self.ips:
63 self.cmd('ip addr add %s dev %s' % (ip, self.bond0))
64
65 self.cmd('ip route add default via %s' % self.gateway)
66 default_intf = self.defaultIntf()
67 default_intf.name = self.bond0
68 self.nameToIntf[self.bond0] = default_intf
69
70 def terminate(self, **kwargs):
71 self.cmd('ip link set %s down' % self.bond0)
72 self.cmd('ip link delete %s' % self.bond0)
73 super(DualHomedRoutedHost, self).terminate()
74
75class DualHomedTaggedRoutedHost(DualHomedRoutedHost):
76 def __init__(self, name, ips, gateway, vlan, *args, **kwargs):
77 super(DualHomedTaggedRoutedHost, self).__init__(name, ips, gateway, *args, **kwargs)
78 self.ips = ips
79 self.gateway = gateway
80 self.vlan = vlan
81 self.vlanIntf = None
82
83 def config(self, **kwargs):
84 super(DualHomedTaggedRoutedHost, self).config(**kwargs)
85 default_intf = self.defaultIntf()
86 self.vlanIntf = "%s.%s" % (default_intf, self.vlan)
87 self.cmd('ip -4 addr flush dev %s' % default_intf)
88 self.cmd('ip link add link %s name %s type vlan id %s' % (default_intf, self.vlanIntf, self.vlan))
89 self.cmd('ip link set up %s' % self.vlanIntf)
90
91 for ip in self.ips:
92 self.cmd('ip addr add %s dev %s' % (ip, self.vlanIntf))
93
94 self.cmd('ip route add default via %s' % self.gateway)
95 default_intf.name = self.vlanIntf
96 self.nameToIntf[self.vlanIntf] = default_intf
97
98 def terminate(self, **kwargs):
99 self.cmd('ip link remove link %s' % self.vlanIntf)
100 super(DualHomedTaggedRoutedHost, self).terminate()
101
102class DhcpClient(Host):
103 def __init__(self, name, *args, **kwargs):
104 super(DhcpClient, self).__init__(name, **kwargs)
105 self.pidFile = '/run/dhclient-%s.pid' % self.name
106 self.leaseFile = '/var/lib/dhcp/dhcpclient-%s.lease' % (self.name, )
107
108 def config(self, **kwargs):
109 super(DhcpClient, self).config(**kwargs)
110 self.cmd('ip addr flush dev %s' % self.defaultIntf())
111 self.cmd('dhclient -q -4 -nw -pf %s -lf %s %s' % (self.pidFile, self.leaseFile, self.defaultIntf()))
112
113 def terminate(self, **kwargs):
114 self.cmd('kill -9 `cat %s`' % self.pidFile)
115 self.cmd('rm -rf %s' % self.pidFile)
116 super(DhcpClient, self).terminate()
117
118class Dhcp6Client(Host):
119 def __init__(self, name, *args, **kwargs):
120 super(Dhcp6Client, self).__init__(name, **kwargs)
121 self.pidFile = '/run/dhclient-%s.pid' % self.name
122 self.leaseFile = '/var/lib/dhcp/dhcpclient6-%s.lease' % (self.name, )
123
124 def config(self, **kwargs):
125 super(Dhcp6Client, self).config(**kwargs)
126 self.cmd('ip -4 addr flush dev %s' % self.defaultIntf())
127 self.cmd('dhclient -q -6 -nw -pf %s -lf %s %s' % (self.pidFile, self.leaseFile, self.defaultIntf()))
128
129 def terminate(self, **kwargs):
130 self.cmd('kill -9 `cat %s`' % self.pidFile)
131 self.cmd('rm -rf %s' % self.pidFile)
132 super(Dhcp6Client, self).terminate()
133
134class DhcpServer(RoutedHost):
135 binFile = '/usr/sbin/dhcpd'
136 pidFile = '/run/dhcp-server-dhcpd.pid'
137 configFile = './dhcpd.conf'
138 leasesFile = '/var/lib/dhcp/dhcpd.leases'
139
140 def config(self, **kwargs):
141 super(DhcpServer, self).config(**kwargs)
142 self.cmd('touch %s' % self.leasesFile)
143 self.cmd('%s -q -4 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.defaultIntf()))
144
145 def terminate(self, **kwargs):
146 self.cmd('kill -9 `cat %s`' % self.pidFile)
147 self.cmd('rm -rf %s' % self.pidFile)
148 super(DhcpServer, self).terminate()
149
150class Dhcp6Server(RoutedHost6):
151 binFile = '/usr/sbin/dhcpd'
152 pidFile = '/run/dhcp-server-dhcpd6.pid'
153 configFile = './dhcpd6.conf'
154 leasesFile = '/var/lib/dhcp/dhcpd6.leases'
155
156 def config(self, **kwargs):
157 super(Dhcp6Server, self).config(**kwargs)
158 linkLocalAddr = mac_to_ipv6_linklocal(kwargs['mac'])
159 self.cmd('ip -6 addr add dev %s scope link %s' % (self.defaultIntf(), linkLocalAddr))
160 self.cmd('touch %s' % self.leasesFile)
161 self.cmd('%s -q -6 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.defaultIntf()))
162
163 def terminate(self, **kwargs):
164 self.cmd('kill -9 `cat %s`' % self.pidFile)
165 self.cmd('rm -rf %s' % self.pidFile)
166 self.cmd('rm -rf %s' % self.leasesFile)
167 super(Dhcp6Server, self).terminate()
168
169class DhcpRelay(Router):
170 binFile = '/usr/sbin/dhcrelay'
171 pidFile = '/run/dhcp-relay.pid'
172 serverIp = None
173 gateway = None
174
175 def __init__(self, name, serverIp, gateway, *args, **kwargs):
176 super(DhcpRelay, self).__init__(name, **kwargs)
177 self.serverIp = serverIp
178 self.gateway = gateway
179
180 def config(self, **kwargs):
181 super(DhcpRelay, self).config(**kwargs)
182 ifacesStr = ' '.join(["-i " + ifaceName for ifaceName in self.interfaces.keys()])
183 self.cmd('route add default gw %s' % self.gateway)
184 self.cmd('%s -4 -a -pf %s %s %s' % (self.binFile, self.pidFile, ifacesStr, self.serverIp))
185
186 def terminate(self, **kwargs):
187 self.cmd('kill -9 `cat %s`', self.pidFile)
188 self.cmd('rm -rf %s' % self.pidFile)
189 super(DhcpRelay, self).terminate()
190
191class TaggedDhcpClient(Host):
192 def __init__(self, name, vlan, *args, **kwargs):
193 super(TaggedDhcpClient, self).__init__(name, **kwargs)
194 self.pidFile = '/run/dhclient-%s.pid' % self.name
195 self.vlan = vlan
196 self.vlanIntf = None
197
198 def config(self, **kwargs):
199 super(TaggedDhcpClient, self).config(**kwargs)
200 self.vlanIntf = "%s.%s" % (self.defaultIntf(), self.vlan)
201 self.cmd('ip addr flush dev %s' % self.defaultIntf())
202 self.cmd('ip link add link %s name %s type vlan id %s' % (self.defaultIntf(), self.vlanIntf, self.vlan))
203 self.cmd('ip link set up %s' % self.vlanIntf)
204 self.cmd('dhclient -q -4 -nw -pf %s %s' % (self.pidFile, self.vlanIntf))
205
206 def terminate(self, **kwargs):
207 self.cmd('kill -9 `cat %s`' % self.pidFile)
208 self.cmd('rm -rf %s' % self.pidFile)
209 self.cmd('ip link remove link %s' % self.vlanIntf)
210 super(TaggedDhcpClient, self).terminate()
211
212class TaggedDhcpServer(TaggedRoutedHost):
213 binFile = '/usr/sbin/dhcpd'
214 pidFile = '/run/dhcp-server/dhcpd.pid'
215 configFile = './dhcpd.conf'
216
217 def config(self, **kwargs):
218 super(TaggedDhcpServer, self).config(**kwargs)
219 self.cmd('%s -q -4 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.vlanIntf))
220
221 def terminate(self, **kwargs):
222 self.cmd('kill -9 `cat %s`' % self.pidFile)
223 self.cmd('rm -rf %s' % self.pidFile)
224 super(TaggedDhcpServer, self).terminate()
225
226class DualHomedDhcpClient(Host):
227 def __init__(self, name, *args, **kwargs):
228 super(DualHomedDhcpClient, self).__init__(name, **kwargs)
229 self.pidFile = '/run/dhclient-%s.pid' % self.name
230 self.bond0 = None
231
232 def config(self, **kwargs):
233 super(DualHomedDhcpClient, self).config(**kwargs)
234 intf0 = self.intfs[0].name
235 intf1 = self.intfs[1].name
236 self.bond0 = "%s-bond0" % self.name
237 self.cmd('modprobe bonding')
238 self.cmd('ip link add %s type bond' % self.bond0)
239 self.cmd('ip link set %s down' % intf0)
240 self.cmd('ip link set %s down' % intf1)
241 self.cmd('ip link set %s master %s' % (intf0, self.bond0))
242 self.cmd('ip link set %s master %s' % (intf1, self.bond0))
243 self.cmd('ip addr flush dev %s' % intf0)
244 self.cmd('ip addr flush dev %s' % intf1)
245 self.cmd('ip link set %s up' % self.bond0)
246 self.cmd('dhclient -q -4 -nw -pf %s %s' % (self.pidFile, self.bond0))
247
248 def terminate(self, **kwargs):
249 self.cmd('ip link set %s down' % self.bond0)
250 self.cmd('ip link delete %s' % self.bond0)
251 self.cmd('kill -9 `cat %s`' % self.pidFile)
252 self.cmd('rm -rf %s' % self.pidFile)
253 super(DualHomedDhcpClient, self).terminate()
254
255# Utility for IPv6
256def mac_to_ipv6_linklocal(mac):
257 '''
258 Convert mac address to link-local IPv6 address
259 '''
260 # Remove the most common delimiters; dots, dashes, etc.
261 mac_value = int(mac.translate(None, ' .:-'), 16)
262
263 # Split out the bytes that slot into the IPv6 address
264 # XOR the most significant byte with 0x02, inverting the
265 # Universal / Local bit
266 high2 = mac_value >> 32 & 0xffff ^ 0x0200
267 high1 = mac_value >> 24 & 0xff
268 low1 = mac_value >> 16 & 0xff
269 low2 = mac_value & 0xffff
270
271 return 'fe80::{:04x}:{:02x}ff:fe{:02x}:{:04x}'.format(high2, high1, low1, low2)