2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import clsinit_copy, ResourceState, \
23 from nepi.resources.linux.application import LinuxApplication
24 from nepi.resources.linux.node import LinuxNode
25 from nepi.util.timefuncs import tnow, tdiffsec
31 PYTHON_VSYS_VERSION = "1.0"
34 class LinuxTap(LinuxApplication):
36 _help = "Creates a TAP device on a Linux host"
40 def _register_attributes(cls):
41 ip4 = Attribute("ip4", "IPv4 Address",
44 mac = Attribute("mac", "MAC Address",
47 prefix4 = Attribute("prefix4", "IPv4 network prefix",
51 mtu = Attribute("mtu", "Maximum transmition unit for device",
54 devname = Attribute("deviceName",
55 "Name of the network interface (e.g. eth0, wlan0, etc)",
56 flags = Flags.NoWrite)
58 up = Attribute("up", "Link up",
61 pointopoint = Attribute("pointopoint", "Peer IP address",
64 txqueuelen = Attribute("txqueuelen", "Length of transmission queue",
67 txqueuelen = Attribute("txqueuelen", "Length of transmission queue",
70 gre_key = Attribute("greKey",
71 "GRE key to be used to configure GRE tunnel",
75 gre_remote = Attribute("greRemote",
76 "Public IP of remote endpoint for GRE tunnel",
79 tear_down = Attribute("tearDown",
80 "Bash script to be executed before releasing the resource",
83 cls._register_attribute(ip4)
84 cls._register_attribute(mac)
85 cls._register_attribute(prefix4)
86 cls._register_attribute(mtu)
87 cls._register_attribute(devname)
88 cls._register_attribute(up)
89 cls._register_attribute(pointopoint)
90 cls._register_attribute(txqueuelen)
91 cls._register_attribute(gre_key)
92 cls._register_attribute(gre_remote)
93 cls._register_attribute(tear_down)
95 def __init__(self, ec, guid):
96 super(LinuxTap, self).__init__(ec, guid)
97 self._home = "tap-%s" % self.guid
98 self._gre_enabled = False
102 node = self.get_connected(LinuxNode.get_rtype())
103 if node: return node[0]
107 def gre_enabled(self):
108 if not self._gre_enabled:
109 from nepi.resources.linux.gretunnel import LinuxGRETunnel
110 gre = self.get_connected(LinuxGRETunnel.get_rtype())
111 if gre: self._gre_enabled = True
113 return self._gre_enabled
115 def upload_sources(self):
116 # upload stop.sh script
117 stop_command = self.replace_paths(self._stop_command)
119 self.node.upload(stop_command,
120 os.path.join(self.app_home, "stop.sh"),
122 # Overwrite file every time.
123 # The stop.sh has the path to the socket, which should change
124 # on every experiment run.
127 def upload_start_command(self):
128 # If GRE mode is enabled, TAP creation is delayed until the
129 # tunnel is established
130 if not self.gre_enabled:
131 # We want to make sure the device is up and running
132 # before the deploy is over, so we execute the
133 # start script now and wait until it finishes.
134 command = self.get("command")
135 command = self.replace_paths(command)
137 shfile = os.path.join(self.app_home, "start.sh")
138 self.node.run_and_wait(command, self.run_home,
143 if not self.node or self.node.state < ResourceState.PROVISIONED:
144 self.ec.schedule(reschedule_delay, self.deploy)
146 if not self.get("deviceName"):
147 self.set("deviceName", "%s%d" % (self.vif_prefix, self.guid))
149 if not self.get("command"):
150 self.set("command", self._start_command)
158 if self.state == ResourceState.READY:
159 command = self.get("command")
160 self.info("Starting command '%s'" % command)
164 msg = " Failed to execute command '%s'" % command
165 self.error(msg, out, err)
166 raise RuntimeError, msg
169 command = self.get('command') or ''
171 if self.state == ResourceState.STARTED:
172 self.info("Stopping command '%s'" % command)
174 command = "bash %s" % os.path.join(self.app_home, "stop.sh")
175 (out, err), proc = self.execute_command(command,
179 msg = " Failed to stop command '%s' " % command
180 self.error(msg, out, err)
186 state_check_delay = 0.5
187 if self._state == ResourceState.STARTED and \
188 tdiffsec(tnow(), self._last_state_check) > state_check_delay:
190 if self.get("deviceName"):
191 (out, err), proc = self.node.execute("ifconfig")
193 if out.strip().find(self.get("deviceName")) == -1:
194 # tap is not running is not running (socket not found)
197 self._last_state_check = tnow()
201 def do_release(self):
202 # Node needs to wait until all associated RMs are released
204 from nepi.resources.linux.tunnel import LinuxTunnel
205 rms = self.get_connected(LinuxTunnel.get_rtype())
208 if rm.state < ResourceState.STOPPED:
209 self.ec.schedule(reschedule_delay, self.release)
212 super(LinuxTap, self).do_release()
214 def gre_connect(self, remote_endpoint, connection_app_home,
215 connection_run_home):
216 gre_connect_command = self._gre_connect_command(
217 remote_endpoint, connection_run_home)
219 # upload command to connect.sh script
220 shfile = os.path.join(connection_app_home, "gre-connect.sh")
221 endpoint.node.upload(gre_connect_command,
226 # invoke connect script
227 cmd = "bash %s" % shfile
228 (out, err), proc = self.node.run(cmd, connection_run_home)
230 # check if execution errors occurred
231 msg = " Failed to connect endpoints "
233 if proc.poll() or err:
234 self.error(msg, out, err)
235 raise RuntimeError, msg
237 # Wait for pid file to be generated
238 pid, ppid = self.node.wait_pid(connection_run_home)
240 # If the process is not running, check for error information
241 # on the remote machine
242 if not pid or not ppid:
243 (out, err), proc = self.node.check_errors(connection_run_home)
244 # Out is what was written in the stderr file
246 msg = " Failed to start command '%s' " % command
247 self.error(msg, out, err)
248 raise RuntimeError, msg
252 def _gre_connect_command(self, remote_endpoint, connection_run_home):
253 # Set the remote endpoint
254 self.set("pointopoint", remote_endpoint.get("ip4"))
255 self.set("greRemote", socket.gethostbyname(
256 remote_endpoint.node.get("hostname")))
258 # Generate GRE connect command
260 command.append(self._stop_command)
261 command.append(") ; (")
262 command.append(self._start_gre_command)
265 command = " ".join(command)
266 command = self.replace_paths(command)
271 def _start_command(self):
273 command.append("sudo -S ip tuntap add %s mode tap" % self.get("deviceName"))
274 command.append("sudo -S ip link set %s up" % self.get("deviceName"))
275 command.append("sudo -S ip addr add %s/%d dev %s" % (
278 self.get("deviceName"),
280 return ";".join(command)
283 def _stop_command(self):
285 command.append("sudo -S ip link set %s down" % self.get("deviceName"))
286 command.append("sudo -S ip link del %s" % self.get("deviceName"))
288 return ";".join(command)
291 def _start_gre_command(self):
293 command.append("sudo -S modprobe ip_gre")
294 command.append("sudo -S ip link add %s type gre remote %s local %s ttl 64 csum key %s" % (
295 self.get("deviceName"),
296 self.get("greRemote"),
297 socket.gethostbyname(self.node.get("hostname")),
300 command.append("sudo -S addr add dev %s %s/%d peer %s/%d" % (
301 self.get("deviceName"),
304 self.get("pointopoint"),
307 command.append("sudo -S ip link set %s up " % self.get("deviceName"))
309 return ";".join(command)
316 def vif_prefix(self):
320 return os.path.join(self.run_home, "tap.sock")
322 def valid_connection(self, guid):