blob: 23bc5c3c4789caa4ff34d6be45f3d7dc549bbf21 [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('..')
Ray Milkey1df260d2018-03-28 11:52:34 -07009from mininet.node import Host, RemoteController
Yi Tseng45ee6922017-07-17 14:49:17 -070010from routinglib import RoutedHost, RoutedHost6, Router
Ray Milkey1df260d2018-03-28 11:52:34 -070011import argparse
12from mininet.net import Mininet
Charles Chanec57b952017-04-24 17:05:06 -070013
14class 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
39class 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 Tseng45ee6922017-07-17 14:49:17 -070043 self.leaseFile = '/var/lib/dhcp/dhcpclient-%s.lease' % (self.name, )
Charles Chanec57b952017-04-24 17:05:06 -070044
45 def config(self, **kwargs):
46 super(DhcpClient, self).config(**kwargs)
47 self.cmd('ip addr flush dev %s' % self.defaultIntf())
Yi Tseng45ee6922017-07-17 14:49:17 -070048 self.cmd('dhclient -q -4 -nw -pf %s -lf %s %s' % (self.pidFile, self.leaseFile, self.defaultIntf()))
Charles Chanec57b952017-04-24 17:05:06 -070049
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 Tseng90109fb2017-07-17 14:48:15 -070055class 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 Tseng45ee6922017-07-17 14:49:17 -070059 self.leaseFile = '/var/lib/dhcp/dhcpclient6-%s.lease' % (self.name, )
Yi Tseng90109fb2017-07-17 14:48:15 -070060
61 def config(self, **kwargs):
62 super(Dhcp6Client, self).config(**kwargs)
Charles Chanfacc5062017-10-23 19:45:23 -070063 self.cmd('ip -4 addr flush dev %s' % self.defaultIntf())
Yi Tseng45ee6922017-07-17 14:49:17 -070064 self.cmd('dhclient -q -6 -nw -pf %s -lf %s %s' % (self.pidFile, self.leaseFile, self.defaultIntf()))
Yi Tseng90109fb2017-07-17 14:48:15 -070065
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 Chanec57b952017-04-24 17:05:06 -070071class DhcpServer(RoutedHost):
72 binFile = '/usr/sbin/dhcpd'
Yi Tseng45ee6922017-07-17 14:49:17 -070073 pidFile = '/run/dhcp-server-dhcpd.pid'
Charles Chanec57b952017-04-24 17:05:06 -070074 configFile = './dhcpd.conf'
Yi Tseng45ee6922017-07-17 14:49:17 -070075 leasesFile = '/var/lib/dhcp/dhcpd.leases'
Charles Chanec57b952017-04-24 17:05:06 -070076
77 def config(self, **kwargs):
78 super(DhcpServer, self).config(**kwargs)
Yi Tseng45ee6922017-07-17 14:49:17 -070079 self.cmd('touch %s' % self.leasesFile)
Charles Chanec57b952017-04-24 17:05:06 -070080 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 Tseng45ee6922017-07-17 14:49:17 -070087class Dhcp6Server(RoutedHost6):
Yi Tseng90109fb2017-07-17 14:48:15 -070088 binFile = '/usr/sbin/dhcpd'
Yi Tseng45ee6922017-07-17 14:49:17 -070089 pidFile = '/run/dhcp-server-dhcpd6.pid'
Yi Tseng90109fb2017-07-17 14:48:15 -070090 configFile = './dhcpd6.conf'
91 leasesFile = '/var/lib/dhcp/dhcpd6.leases'
92
93 def config(self, **kwargs):
94 super(Dhcp6Server, self).config(**kwargs)
Yi Tseng15375ea2017-07-26 16:34:09 -070095 linkLocalAddr = mac_to_ipv6_linklocal(kwargs['mac'])
96 self.cmd('ip -6 addr add dev %s scope link %s' % (self.defaultIntf(), linkLocalAddr))
Yi Tseng90109fb2017-07-17 14:48:15 -070097 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 Tseng15375ea2017-07-26 16:34:09 -0700104 super(Dhcp6Server, self).terminate()
Yi Tseng90109fb2017-07-17 14:48:15 -0700105
Yi Tseng9e332d82017-07-28 17:52:21 -0700106class 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 Chanec57b952017-04-24 17:05:06 -0700128class 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
149class 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
163class 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 Tseng15375ea2017-07-26 16:34:09 -0700191
192# Utility for IPv6
193def 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 Milkey1df260d2018-03-28 11:52:34 -0700209
210# Parses Trellis parameters
211def 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
218def 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
230def 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