blob: db0fb65c9e8fa546f750cecbcefa54f0bb915853 [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
10from routinglib import RoutedHost
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 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())
60 self.cmd('dhclient -q -6 -nw -pf %s %s' % (self.pidFile, self.defaultIntf()))
61
62 def terminate(self, **kwargs):
63 self.cmd('kill -9 `cat %s`' % self.pidFile)
64 self.cmd('rm -rf %s' % self.pidFile)
65 super(Dhcp6Client, self).terminate()
66
Charles Chanec57b952017-04-24 17:05:06 -070067class DhcpServer(RoutedHost):
68 binFile = '/usr/sbin/dhcpd'
69 pidFile = '/run/dhcp-server/dhcpd.pid'
70 configFile = './dhcpd.conf'
71
72 def config(self, **kwargs):
73 super(DhcpServer, self).config(**kwargs)
74 self.cmd('%s -q -4 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.defaultIntf()))
75
76 def terminate(self, **kwargs):
77 self.cmd('kill -9 `cat %s`' % self.pidFile)
78 self.cmd('rm -rf %s' % self.pidFile)
79 super(DhcpServer, self).terminate()
80
Yi Tseng90109fb2017-07-17 14:48:15 -070081class Dhcp6Server(RoutedHost):
82 binFile = '/usr/sbin/dhcpd'
83 pidFile = '/run/dhcp-server/dhcpd6.pid'
84 configFile = './dhcpd6.conf'
85 leasesFile = '/var/lib/dhcp/dhcpd6.leases'
86
87 def config(self, **kwargs):
88 super(Dhcp6Server, self).config(**kwargs)
89 self.cmd('touch %s' % self.leasesFile)
90 self.cmd('%s -q -6 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.defaultIntf()))
91
92 def terminate(self, **kwargs):
93 self.cmd('kill -9 `cat %s`' % self.pidFile)
94 self.cmd('rm -rf %s' % self.pidFile)
95 self.cmd('rm -rf %s' % self.leasesFile)
96 super(DhcpServer, self).terminate()
97
Charles Chanec57b952017-04-24 17:05:06 -070098class TaggedDhcpClient(Host):
99 def __init__(self, name, vlan, *args, **kwargs):
100 super(TaggedDhcpClient, self).__init__(name, **kwargs)
101 self.pidFile = '/run/dhclient-%s.pid' % self.name
102 self.vlan = vlan
103 self.vlanIntf = None
104
105 def config(self, **kwargs):
106 super(TaggedDhcpClient, self).config(**kwargs)
107 self.vlanIntf = "%s.%s" % (self.defaultIntf(), self.vlan)
108 self.cmd('ip addr flush dev %s' % self.defaultIntf())
109 self.cmd('ip link add link %s name %s type vlan id %s' % (self.defaultIntf(), self.vlanIntf, self.vlan))
110 self.cmd('ip link set up %s' % self.vlanIntf)
111 self.cmd('dhclient -q -4 -nw -pf %s %s' % (self.pidFile, self.vlanIntf))
112
113 def terminate(self, **kwargs):
114 self.cmd('kill -9 `cat %s`' % self.pidFile)
115 self.cmd('rm -rf %s' % self.pidFile)
116 self.cmd('ip link remove link %s' % self.vlanIntf)
117 super(TaggedDhcpClient, self).terminate()
118
119class TaggedDhcpServer(TaggedRoutedHost):
120 binFile = '/usr/sbin/dhcpd'
121 pidFile = '/run/dhcp-server/dhcpd.pid'
122 configFile = './dhcpd.conf'
123
124 def config(self, **kwargs):
125 super(TaggedDhcpServer, self).config(**kwargs)
126 self.cmd('%s -q -4 -pf %s -cf %s %s' % (self.binFile, self.pidFile, self.configFile, self.vlanIntf))
127
128 def terminate(self, **kwargs):
129 self.cmd('kill -9 `cat %s`' % self.pidFile)
130 self.cmd('rm -rf %s' % self.pidFile)
131 super(TaggedDhcpServer, self).terminate()
132
133class DualHomedDhcpClient(Host):
134 def __init__(self, name, *args, **kwargs):
135 super(DualHomedDhcpClient, self).__init__(name, **kwargs)
136 self.pidFile = '/run/dhclient-%s.pid' % self.name
137 self.bond0 = None
138
139 def config(self, **kwargs):
140 super(DualHomedDhcpClient, self).config(**kwargs)
141 intf0 = self.intfs[0].name
142 intf1 = self.intfs[1].name
143 self.bond0 = "%s-bond0" % self.name
144 self.cmd('modprobe bonding')
145 self.cmd('ip link add %s type bond' % self.bond0)
146 self.cmd('ip link set %s down' % intf0)
147 self.cmd('ip link set %s down' % intf1)
148 self.cmd('ip link set %s master %s' % (intf0, self.bond0))
149 self.cmd('ip link set %s master %s' % (intf1, self.bond0))
150 self.cmd('ip addr flush dev %s' % intf0)
151 self.cmd('ip addr flush dev %s' % intf1)
152 self.cmd('ip link set %s up' % self.bond0)
153 self.cmd('dhclient -q -4 -nw -pf %s %s' % (self.pidFile, self.bond0))
154
155 def terminate(self, **kwargs):
156 self.cmd('ip link set %s down' % self.bond0)
157 self.cmd('ip link delete %s' % self.bond0)
158 self.cmd('kill -9 `cat %s`' % self.pidFile)
159 self.cmd('rm -rf %s' % self.pidFile)
160 super(DualHomedDhcpClient, self).terminate()