blob: 6199f6290e55d0ed0fb206267b303b64bf4bd8a5 [file] [log] [blame]
Charles Chanec57b952017-04-24 17:05:06 -07001#!/usr/bin/python
2
3"""
4Libraries for Trellis hosts.
5"""
6
7import sys
Ray Milkey4ed9fd82018-04-26 09:06:29 -07008import time
9
Charles Chanec57b952017-04-24 17:05:06 -070010sys.path.append('..')
Ray Milkey1df260d2018-03-28 11:52:34 -070011from mininet.node import Host, RemoteController
Yi Tseng45ee6922017-07-17 14:49:17 -070012from routinglib import RoutedHost, RoutedHost6, Router
Ray Milkey1df260d2018-03-28 11:52:34 -070013import argparse
14from mininet.net import Mininet
Charles Chanec57b952017-04-24 17:05:06 -070015
16class TaggedRoutedHost(RoutedHost):
17 """Host that can be configured with multiple IP addresses."""
18 def __init__(self, name, ips, gateway, vlan, *args, **kwargs):
19 super(RoutedHost, self).__init__(name, *args, **kwargs)
20 self.ips = ips
21 self.gateway = gateway
22 self.vlan = vlan
23 self.vlanIntf = None
24
25 def config(self, **kwargs):
26 Host.config(self, **kwargs)
27 self.vlanIntf = "%s.%s" % (self.defaultIntf(), self.vlan)
28 self.cmd('ip -4 addr flush dev %s' % self.defaultIntf())
29 self.cmd('ip link add link %s name %s type vlan id %s' % (self.defaultIntf(), self.vlanIntf, self.vlan))
30 self.cmd('ip link set up %s' % self.vlanIntf)
31
32 for ip in self.ips:
33 self.cmd('ip addr add %s dev %s' % (ip, self.vlanIntf))
34
35 self.cmd('ip route add default via %s' % self.gateway)
36
37 def terminate(self, **kwargs):
38 self.cmd('ip link remove link %s' % self.vlanIntf)
39 super(TaggedRoutedHost, self).terminate()
40
41class DhcpClient(Host):
42 def __init__(self, name, *args, **kwargs):
43 super(DhcpClient, self).__init__(name, **kwargs)
44 self.pidFile = '/run/dhclient-%s.pid' % self.name
Yi Tseng45ee6922017-07-17 14:49:17 -070045 self.leaseFile = '/var/lib/dhcp/dhcpclient-%s.lease' % (self.name, )
Charles Chanec57b952017-04-24 17:05:06 -070046
47 def config(self, **kwargs):
48 super(DhcpClient, self).config(**kwargs)
49 self.cmd('ip addr flush dev %s' % self.defaultIntf())
Yi Tseng45ee6922017-07-17 14:49:17 -070050 self.cmd('dhclient -q -4 -nw -pf %s -lf %s %s' % (self.pidFile, self.leaseFile, self.defaultIntf()))
Charles Chanec57b952017-04-24 17:05:06 -070051
52 def terminate(self, **kwargs):
53 self.cmd('kill -9 `cat %s`' % self.pidFile)
54 self.cmd('rm -rf %s' % self.pidFile)
55 super(DhcpClient, self).terminate()
56
Yi Tseng90109fb2017-07-17 14:48:15 -070057class Dhcp6Client(Host):
58 def __init__(self, name, *args, **kwargs):
59 super(Dhcp6Client, self).__init__(name, **kwargs)
60 self.pidFile = '/run/dhclient-%s.pid' % self.name
Yi Tseng45ee6922017-07-17 14:49:17 -070061 self.leaseFile = '/var/lib/dhcp/dhcpclient6-%s.lease' % (self.name, )
Yi Tseng90109fb2017-07-17 14:48:15 -070062
63 def config(self, **kwargs):
64 super(Dhcp6Client, self).config(**kwargs)
Charles Chanfacc5062017-10-23 19:45:23 -070065 self.cmd('ip -4 addr flush dev %s' % self.defaultIntf())
Ray Milkey4ed9fd82018-04-26 09:06:29 -070066 time.sleep(3)
Yi Tseng45ee6922017-07-17 14:49:17 -070067 self.cmd('dhclient -q -6 -nw -pf %s -lf %s %s' % (self.pidFile, self.leaseFile, self.defaultIntf()))
Yi Tseng90109fb2017-07-17 14:48:15 -070068
69 def terminate(self, **kwargs):
70 self.cmd('kill -9 `cat %s`' % self.pidFile)
71 self.cmd('rm -rf %s' % self.pidFile)
72 super(Dhcp6Client, self).terminate()
73
Andrea Campanellaba96c612018-04-23 12:09:34 +020074# Client that has on the same interface (eth0) both IPv4 and IPv6 addresses
75class Dhcp4and6Client(Host):
76 def __init__(self, name, *args, **kwargs):
77 super(Dhcp4and6Client, self).__init__(name, **kwargs)
78 self.pidFile4 = '/run/dhclient-%s-4.pid' % self.name
79 self.pidFile6 = '/run/dhclient-%s-6.pid' % self.name
80 self.leaseFile4 = '/var/lib/dhcp/dhcpclient-%s.lease' % (self.name, )
81 self.leaseFile6 = '/var/lib/dhcp/dhcpclient6-%s.lease' % (self.name, )
82
83 def config(self, **kwargs):
84 super(Dhcp4and6Client, self).config(**kwargs)
85 self.cmd('ip addr flush dev %s' % self.defaultIntf())
86 self.cmd('dhclient -q -4 -nw -pf %s -lf %s %s' % (self.pidFile4, self.leaseFile4, self.defaultIntf()))
87
88 self.cmd('ip -4 addr flush dev %s' % self.defaultIntf())
Ray Milkey4ed9fd82018-04-26 09:06:29 -070089 time.sleep(3)
Andrea Campanellaba96c612018-04-23 12:09:34 +020090 self.cmd('dhclient -q -6 -nw -pf %s -lf %s %s' % (self.pidFile6, self.leaseFile6, self.defaultIntf()))
91
92 def terminate(self, **kwargs):
93 self.cmd('kill -9 `cat %s`' % self.pidFile4)
94 self.cmd('rm -rf %s' % self.pidFile4)
95 self.cmd('kill -9 `cat %s`' % self.pidFile6)
96 self.cmd('rm -rf %s' % self.pidFile6)
97 super(Dhcp4and6Client, self).terminate()
98
Charles Chanec57b952017-04-24 17:05:06 -070099class DhcpServer(RoutedHost):
100 binFile = '/usr/sbin/dhcpd'
Yi Tseng45ee6922017-07-17 14:49:17 -0700101 pidFile = '/run/dhcp-server-dhcpd.pid'
Charles Chanec57b952017-04-24 17:05:06 -0700102 configFile = './dhcpd.conf'
Yi Tseng45ee6922017-07-17 14:49:17 -0700103 leasesFile = '/var/lib/dhcp/dhcpd.leases'
Charles Chanec57b952017-04-24 17:05:06 -0700104
105 def config(self, **kwargs):
106 super(DhcpServer, self).config(**kwargs)
Yi Tseng45ee6922017-07-17 14:49:17 -0700107 self.cmd('touch %s' % self.leasesFile)
Charles Chanec57b952017-04-24 17:05:06 -0700108 self.cmd('%s -q -4 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.defaultIntf()))
109
110 def terminate(self, **kwargs):
111 self.cmd('kill -9 `cat %s`' % self.pidFile)
112 self.cmd('rm -rf %s' % self.pidFile)
113 super(DhcpServer, self).terminate()
114
Yi Tseng45ee6922017-07-17 14:49:17 -0700115class Dhcp6Server(RoutedHost6):
Yi Tseng90109fb2017-07-17 14:48:15 -0700116 binFile = '/usr/sbin/dhcpd'
Yi Tseng45ee6922017-07-17 14:49:17 -0700117 pidFile = '/run/dhcp-server-dhcpd6.pid'
Yi Tseng90109fb2017-07-17 14:48:15 -0700118 configFile = './dhcpd6.conf'
119 leasesFile = '/var/lib/dhcp/dhcpd6.leases'
120
121 def config(self, **kwargs):
122 super(Dhcp6Server, self).config(**kwargs)
Yi Tseng15375ea2017-07-26 16:34:09 -0700123 linkLocalAddr = mac_to_ipv6_linklocal(kwargs['mac'])
124 self.cmd('ip -6 addr add dev %s scope link %s' % (self.defaultIntf(), linkLocalAddr))
Yi Tseng90109fb2017-07-17 14:48:15 -0700125 self.cmd('touch %s' % self.leasesFile)
126 self.cmd('%s -q -6 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.defaultIntf()))
127
128 def terminate(self, **kwargs):
129 self.cmd('kill -9 `cat %s`' % self.pidFile)
130 self.cmd('rm -rf %s' % self.pidFile)
131 self.cmd('rm -rf %s' % self.leasesFile)
Yi Tseng15375ea2017-07-26 16:34:09 -0700132 super(Dhcp6Server, self).terminate()
Yi Tseng90109fb2017-07-17 14:48:15 -0700133
Yi Tseng9e332d82017-07-28 17:52:21 -0700134class DhcpRelay(Router):
135 binFile = '/usr/sbin/dhcrelay'
136 pidFile = '/run/dhcp-relay.pid'
137 serverIp = None
138 gateway = None
139
140 def __init__(self, name, serverIp, gateway, *args, **kwargs):
141 super(DhcpRelay, self).__init__(name, **kwargs)
142 self.serverIp = serverIp
143 self.gateway = gateway
144
145 def config(self, **kwargs):
146 super(DhcpRelay, self).config(**kwargs)
147 ifacesStr = ' '.join(["-i " + ifaceName for ifaceName in self.interfaces.keys()])
148 self.cmd('route add default gw %s' % self.gateway)
149 self.cmd('%s -4 -a -pf %s %s %s' % (self.binFile, self.pidFile, ifacesStr, self.serverIp))
150
151 def terminate(self, **kwargs):
152 self.cmd('kill -9 `cat %s`', self.pidFile)
153 self.cmd('rm -rf %s' % self.pidFile)
154 super(DhcpRelay, self).terminate()
155
Charles Chanec57b952017-04-24 17:05:06 -0700156class TaggedDhcpClient(Host):
157 def __init__(self, name, vlan, *args, **kwargs):
158 super(TaggedDhcpClient, self).__init__(name, **kwargs)
159 self.pidFile = '/run/dhclient-%s.pid' % self.name
160 self.vlan = vlan
161 self.vlanIntf = None
162
163 def config(self, **kwargs):
164 super(TaggedDhcpClient, self).config(**kwargs)
165 self.vlanIntf = "%s.%s" % (self.defaultIntf(), self.vlan)
166 self.cmd('ip addr flush dev %s' % self.defaultIntf())
167 self.cmd('ip link add link %s name %s type vlan id %s' % (self.defaultIntf(), self.vlanIntf, self.vlan))
168 self.cmd('ip link set up %s' % self.vlanIntf)
169 self.cmd('dhclient -q -4 -nw -pf %s %s' % (self.pidFile, self.vlanIntf))
170
171 def terminate(self, **kwargs):
172 self.cmd('kill -9 `cat %s`' % self.pidFile)
173 self.cmd('rm -rf %s' % self.pidFile)
174 self.cmd('ip link remove link %s' % self.vlanIntf)
175 super(TaggedDhcpClient, self).terminate()
176
177class TaggedDhcpServer(TaggedRoutedHost):
178 binFile = '/usr/sbin/dhcpd'
179 pidFile = '/run/dhcp-server/dhcpd.pid'
180 configFile = './dhcpd.conf'
181
182 def config(self, **kwargs):
183 super(TaggedDhcpServer, self).config(**kwargs)
184 self.cmd('%s -q -4 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.vlanIntf))
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(TaggedDhcpServer, self).terminate()
190
191class DualHomedDhcpClient(Host):
192 def __init__(self, name, *args, **kwargs):
193 super(DualHomedDhcpClient, self).__init__(name, **kwargs)
194 self.pidFile = '/run/dhclient-%s.pid' % self.name
195 self.bond0 = None
196
197 def config(self, **kwargs):
198 super(DualHomedDhcpClient, self).config(**kwargs)
199 intf0 = self.intfs[0].name
200 intf1 = self.intfs[1].name
201 self.bond0 = "%s-bond0" % self.name
202 self.cmd('modprobe bonding')
203 self.cmd('ip link add %s type bond' % self.bond0)
204 self.cmd('ip link set %s down' % intf0)
205 self.cmd('ip link set %s down' % intf1)
206 self.cmd('ip link set %s master %s' % (intf0, self.bond0))
207 self.cmd('ip link set %s master %s' % (intf1, self.bond0))
208 self.cmd('ip addr flush dev %s' % intf0)
209 self.cmd('ip addr flush dev %s' % intf1)
210 self.cmd('ip link set %s up' % self.bond0)
211 self.cmd('dhclient -q -4 -nw -pf %s %s' % (self.pidFile, self.bond0))
212
213 def terminate(self, **kwargs):
214 self.cmd('ip link set %s down' % self.bond0)
215 self.cmd('ip link delete %s' % self.bond0)
216 self.cmd('kill -9 `cat %s`' % self.pidFile)
217 self.cmd('rm -rf %s' % self.pidFile)
218 super(DualHomedDhcpClient, self).terminate()
Yi Tseng15375ea2017-07-26 16:34:09 -0700219
Andrea Campanellaba96c612018-04-23 12:09:34 +0200220# Dual-homed Client that has both IPv4 and IPv6 addresses
221class DualHomedDhcp4and6Client(Host):
222 def __init__(self, name, *args, **kwargs):
223 super(DualHomedDhcp4and6Client, self).__init__(name, **kwargs)
224 self.pidFile4 = '/run/dhclient-%s-4.pid' % self.name
225 self.pidFile6 = '/run/dhclient-%s-6.pid' % self.name
226 self.bond0 = None
227
228 def config(self, **kwargs):
229 super(DualHomedDhcp4and6Client, self).config(**kwargs)
230 intf0 = self.intfs[0].name
231 intf1 = self.intfs[1].name
232 self.bond0 = "%s-bond0" % self.name
233 self.cmd('modprobe bonding')
234 self.cmd('ip link add %s type bond' % self.bond0)
235 self.cmd('ip link set %s down' % intf0)
236 self.cmd('ip link set %s down' % intf1)
237 self.cmd('ip link set %s master %s' % (intf0, self.bond0))
238 self.cmd('ip link set %s master %s' % (intf1, self.bond0))
239 self.cmd('ip -4 addr flush dev %s' % intf0)
240 self.cmd('ip -4 addr flush dev %s' % intf1)
241 self.cmd('ip addr flush dev %s' % intf0)
242 self.cmd('ip addr flush dev %s' % intf1)
Ray Milkey4ed9fd82018-04-26 09:06:29 -0700243 time.sleep(3)
Andrea Campanellaba96c612018-04-23 12:09:34 +0200244 self.cmd('ip link set %s up' % self.bond0)
245 self.cmd('dhclient -q -4 -nw -pf %s %s' % (self.pidFile4, self.bond0))
246 self.cmd('dhclient -q -6 -nw -pf %s %s' % (self.pidFile6, 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.pidFile4)
252 self.cmd('kill -9 `cat %s`' % self.pidFile6)
253 self.cmd('rm -rf %s' % self.pidFile4)
254 self.cmd('rm -rf %s' % self.pidFile6)
255 super(DualHomedDhcp4and6Client, self).terminate()
256
Yi Tseng15375ea2017-07-26 16:34:09 -0700257# Utility for IPv6
258def mac_to_ipv6_linklocal(mac):
259 '''
260 Convert mac address to link-local IPv6 address
261 '''
262 # Remove the most common delimiters; dots, dashes, etc.
263 mac_value = int(mac.translate(None, ' .:-'), 16)
264
265 # Split out the bytes that slot into the IPv6 address
266 # XOR the most significant byte with 0x02, inverting the
267 # Universal / Local bit
268 high2 = mac_value >> 32 & 0xffff ^ 0x0200
269 high1 = mac_value >> 24 & 0xff
270 low1 = mac_value >> 16 & 0xff
271 low2 = mac_value & 0xffff
272
273 return 'fe80::{:04x}:{:02x}ff:fe{:02x}:{:04x}'.format(high2, high1, low1, low2)
Ray Milkey1df260d2018-03-28 11:52:34 -0700274
275# Parses Trellis parameters
276def parse_trellis_args():
277 parser = argparse.ArgumentParser(description="Trellis Arguments")
278 parser.add_argument("-c", "--controllers", help = "Comma Separated List of ONOS controllers",
279 required = True, default = "")
280 return parser.parse_args()
281
282# Gets a mininet instance
283def get_mininet(arguments, topo, switch):
284 net = Mininet(topo=topo, controller=None, switch=switch)
285
286 if arguments.controllers:
287 controllers = arguments.controllers.split(',')
288 controller_number = 0
289 for controller in controllers:
290 net.addController(RemoteController('c' + str(controller_number), ip=controller))
291 controller_number += 1
292 return net
293
294# Generates the Zebra config files
295def set_up_zebra_config(controllers_string):
296 zebra_config = "log file /var/log/quagga/zebradbgp{}.log\n" \
297 "hostname zebra-bgp{}\n" \
298 "password quagga\n" \
299 "!\n" \
300 "! Default route via virtual management switch\n" \
301 "!\n" \
302 "ip route 0.0.0.0/0 172.16.0.1\n" \
303 "!\n" \
304 "fpm connection ip {} port 2620\n"
305 controllers = controllers_string.split(',')
306
307 controller1 = controllers[0]
308 if (len(controllers) > 1):
309 controller2 = controllers[1]
310 else:
311 controller2 = controller1
312
313
314 zebra1 = zebra_config.format("1", "1", controller1)
315 zebra2 = zebra_config.format("2", "2", controller2)
316
317 with open("zebradbgp1.conf", "w") as config_file_1:
318 config_file_1.write(zebra1)
319
320 with open("zebradbgp2.conf", "w") as config_file_2:
321 config_file_2.write(zebra2)
322