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
22 from nepi.resources.linux.udptunnel import LinuxUdpTunnel
23 from nepi.util.sshfuncs import ProcStatus
24 from nepi.util.timefuncs import tnow, tdiffsec
32 class LinuxNs3FdUdpTunnel(LinuxUdpTunnel):
33 _rtype = "linux::ns3::FdUdpTunnel"
34 _help = "Constructs a tunnel between two Ns-3 FdNetdevices " \
35 "located in remote Linux nodes using a UDP connection "
36 _platform = "linux::ns3"
39 def _register_attributes(cls):
40 cipher = Attribute("cipher",
41 "Cipher to encript communication. "
42 "One of PLAIN, AES, Blowfish, DES, DES3. ",
44 allowed = ["PLAIN", "AES", "Blowfish", "DES", "DES3"],
45 type = Types.Enumerate,
48 cipher_key = Attribute("cipherKey",
49 "Specify a symmetric encryption key with which to protect "
50 "packets across the tunnel. python-crypto must be installed "
54 txqueuelen = Attribute("txQueueLen",
55 "Specifies the interface's transmission queue length. "
60 bwlimit = Attribute("bwLimit",
61 "Specifies the interface's emulated bandwidth in bytes "
66 cls._register_attribute(cipher)
67 cls._register_attribute(cipher_key)
68 cls._register_attribute(txqueuelen)
69 cls._register_attribute(bwlimit)
71 def __init__(self, ec, guid):
72 super(LinuxUdpTunnel, self).__init__(ec, guid)
73 self._home = "fd-udp-tunnel-%s" % self.guid
81 def log_message(self, msg):
83 return " guid %d - %s - %s - %s " % (self.guid,
84 self.node1.get("hostname"),
85 self.node2.get("hostname"),
88 def get_endpoints(self):
89 """ Returns the list of RM that are endpoints to the tunnel
91 if not self._fd2 or not self._fd1:
92 from nepi.resources.ns3.ns3fdnetdevice import NS3BaseFdNetDevice
93 devices = self.get_connected(NS3BaseFdNetDevice.get_rtype())
94 if not devices or len(devices) != 2:
95 msg = "linux::ns3::TunTapFdLink must be connected to exactly one FdNetDevice"
97 raise RuntimeError, msg
99 self._fd1 = devices[0]
100 self._fd2 = devices[1]
102 simu = self._fd1.simulation
103 from nepi.resources.linux.node import LinuxNode
104 nodes = simu.get_connected(LinuxNode.get_rtype())
105 self._fd1node = nodes[0]
107 simu = self._fd2.simulation
108 from nepi.resources.linux.node import LinuxNode
109 nodes = simu.get_connected(LinuxNode.get_rtype())
110 self._fd2node = nodes[0]
112 if self._fd1node.get("hostname") == \
113 self._fd2node.get("hostname"):
114 msg = "linux::ns3::FdUdpTunnel requires endpoints on different hosts"
116 raise RuntimeError, msg
118 return [self._fd1, self._fd2]
140 def endpoint_node(self, endpoint):
142 if endpoint == self.endpoint1:
149 def app_home(self, endpoint):
150 node = self.endpoint_node(endpoint)
151 return os.path.join(node.exp_home, self._home)
153 def run_home(self, endpoint):
154 return os.path.join(self.app_home(endpoint), self.ec.run_id)
156 def upload_sources(self, endpoint):
159 # vif-passfd python script
160 linux_passfd = os.path.join(os.path.dirname(__file__),
165 scripts.append(linux_passfd)
167 # tunnel creation python script
168 tunchannel = os.path.join(os.path.dirname(__file__),
173 scripts.append(tunchannel)
176 scripts = ";".join(scripts)
178 node = self.endpoint_node(endpoint)
180 os.path.join(node.src_dir),
183 def endpoint_mkdir(self, endpoint):
184 node = self.endpoint_node(endpoint)
185 run_home = self.run_home(endpoint)
188 def initiate_connection(self, endpoint, remote_endpoint):
189 cipher = self.get("cipher")
190 cipher_key = self.get("cipherKey")
191 bwlimit = self.get("bwLimit")
192 txqueuelen = self.get("txQueueLen")
194 # Upload the tunnel creating script
195 self.upload_sources(endpoint)
197 # Request an address to send the file descriptor to the ns-3 simulation
198 address = endpoint.recv_fd()
200 # execute the tunnel creation script
201 node = self.endpoint_node(remote_endpoint)
202 port = self.initiate(endpoint, remote_endpoint, address, cipher,
203 cipher_key, bwlimit, txqueuelen)
207 def establish_connection(self, endpoint, remote_endpoint, port):
208 self.establish(endpoint, remote_endpoint, port)
210 def verify_connection(self, endpoint, remote_endpoint):
211 self.verify(endpoint)
213 def terminate_connection(self, endpoint, remote_endpoint):
217 def check_state_connection(self):
218 # Make sure the process is still running in background
219 # No execution errors occurred. Make sure the background
220 # process with the recorded pid is still running.
222 node1 = self.endpoint_node(self.endpoint1)
223 node2 = self.endpoint_node(self.endpoint2)
224 run_home1 = self.run_home(self.endpoint1)
225 run_home2 = self.run_home(self.endpoint1)
226 (pid1, ppid1) = self._pids[endpoint1]
227 (pid2, ppid2) = self._pids[endpoint2]
229 status1 = node1.status(pid1, ppid1)
230 status2 = node2.status(pid2, ppid2)
232 if status1 == ProcStatus.FINISHED and \
233 status2 == ProcStatus.FINISHED:
235 # check if execution errors occurred
236 (out1, err1), proc1 = node1.check_errors(run_home1)
237 (out2, err2), proc2 = node2.check_errors(run_home2)
240 msg = "Error occurred in tunnel"
241 self.error(msg, err1, err2)
246 def wait_local_port(self, endpoint):
247 """ Waits until the local_port file for the endpoint is generated,
248 and returns the port number
251 return self.wait_file(endpoint, "local_port")
253 def wait_result(self, endpoint):
254 """ Waits until the return code file for the endpoint is generated
257 return self.wait_file(endpoint, "ret_file")
259 def wait_file(self, endpoint, filename):
260 """ Waits until file on endpoint is generated """
264 node = self.endpoint_node(endpoint)
265 run_home = self.run_home(endpoint)
268 (out, err), proc = node.check_output(run_home, filename)
277 msg = "Couldn't retrieve %s" % filename
278 self.error(msg, out, err)
279 raise RuntimeError, msg
283 def initiate(self, endpoint, remote_endpoint, address, cipher, cipher_key,
284 bwlimit, txqueuelen):
286 command = self._initiate_command(endpoint, remote_endpoint,
287 address, cipher, cipher_key, bwlimit, txqueuelen)
289 node = self.endpoint_node(endpoint)
290 run_home = self.run_home(endpoint)
291 app_home = self.app_home(endpoint)
293 # upload command to connect.sh script
294 shfile = os.path.join(app_home, "fd-udp-connect.sh")
295 node.upload_command(command,
299 # invoke connect script
300 cmd = "bash %s" % shfile
301 (out, err), proc = node.run(cmd, run_home)
303 # check if execution errors occurred
304 msg = "Failed to connect endpoints "
307 self.error(msg, out, err)
308 raise RuntimeError, msg
310 # Wait for pid file to be generated
311 pid, ppid = node.wait_pid(run_home)
313 self._pids[endpoint] = (pid, ppid)
315 # Check for error information on the remote machine
316 (out, err), proc = node.check_errors(run_home)
317 # Out is what was written in the stderr file
319 msg = " Failed to start command '%s' " % command
320 self.error(msg, out, err)
321 raise RuntimeError, msg
323 port = self.wait_local_port(endpoint)
327 def _initiate_command(self, endpoint, remote_endpoint, address,
328 cipher, cipher_key, bwlimit, txqueuelen):
329 local_node = self.endpoint_node(endpoint)
330 local_run_home = self.run_home(endpoint)
331 local_app_home = self.app_home(endpoint)
332 remote_node = self.endpoint_node(remote_endpoint)
334 local_ip = local_node.get("ip")
335 remote_ip = remote_node.get("ip")
337 local_port_file = os.path.join(local_run_home, "local_port")
338 remote_port_file = os.path.join(local_run_home, "remote_port")
339 ret_file = os.path.join(local_run_home, "ret_file")
341 address = base64.b64encode(address)
344 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
345 command.append("python ${SRC}/fd-udp-connect.py")
346 command.append("-a %s" % address)
347 command.append("-p %s" % local_port_file)
348 command.append("-P %s" % remote_port_file)
349 command.append("-o %s" % local_ip)
350 command.append("-O %s" % remote_ip)
351 command.append("-R %s" % ret_file)
352 command.append("-t %s" % "IFF_TAP")
356 command.append("-c %s" % cipher)
358 command.append("-k %s " % cipher_key)
360 command.append("-q %s " % txqueuelen)
362 command.append("-b %s " % bwlimit)
364 command = " ".join(command)
365 command = self.replace_paths(command, node=local_node,
366 app_home=local_app_home, run_home=local_run_home)
370 def establish(self, endpoint, remote_endpoint, port):
371 node = self.endpoint_node(endpoint)
372 run_home = self.run_home(endpoint)
374 # upload remote port number to file
375 remote_port = "%s\n" % port
376 node.upload(remote_port,
377 os.path.join(run_home, "remote_port"),
381 def verify(self, endpoint):
382 self.wait_result(endpoint)