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 version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.resource import clsinit_copy, ResourceState
21 from nepi.resources.linux.udptunnel import LinuxUdpTunnel
22 from nepi.util.sshfuncs import ProcStatus
23 from nepi.util.timefuncs import tnow, tdiffsec
31 class LinuxNs3FdUdpTunnel(LinuxUdpTunnel):
32 _rtype = "linux::ns3::FdUdpTunnel"
33 _help = "Constructs a tunnel between two Ns-3 FdNetdevices " \
34 "located in remote Linux nodes using a UDP connection "
35 _platform = "linux::ns3"
38 def _register_attributes(cls):
39 cipher = Attribute("cipher",
40 "Cipher to encript communication. "
41 "One of PLAIN, AES, Blowfish, DES, DES3. ",
43 allowed = ["PLAIN", "AES", "Blowfish", "DES", "DES3"],
44 type = Types.Enumerate,
47 cipher_key = Attribute("cipherKey",
48 "Specify a symmetric encryption key with which to protect "
49 "packets across the tunnel. python-crypto must be installed "
53 txqueuelen = Attribute("txQueueLen",
54 "Specifies the interface's transmission queue length. "
59 bwlimit = Attribute("bwLimit",
60 "Specifies the interface's emulated bandwidth in bytes "
65 cls._register_attribute(cipher)
66 cls._register_attribute(cipher_key)
67 cls._register_attribute(txqueuelen)
68 cls._register_attribute(bwlimit)
70 def __init__(self, ec, guid):
71 super(LinuxUdpTunnel, self).__init__(ec, guid)
72 self._home = "fd-udp-tunnel-%s" % self.guid
80 def log_message(self, msg):
82 return " guid %d - %s - %s - %s " % (self.guid,
83 self.node1.get("hostname"),
84 self.node2.get("hostname"),
87 def get_endpoints(self):
88 """ Returns the list of RM that are endpoints to the tunnel
90 if not self._fd2 or not self._fd1:
91 from nepi.resources.ns3.ns3fdnetdevice import NS3BaseFdNetDevice
92 devices = self.get_connected(NS3BaseFdNetDevice.get_rtype())
93 if not devices or len(devices) != 2:
94 msg = "linux::ns3::TunTapFdLink must be connected to exactly one FdNetDevice"
96 raise RuntimeError(msg)
98 self._fd1 = devices[0]
99 self._fd2 = devices[1]
101 simu = self._fd1.simulation
102 from nepi.resources.linux.node import LinuxNode
103 nodes = simu.get_connected(LinuxNode.get_rtype())
104 self._fd1node = nodes[0]
106 simu = self._fd2.simulation
107 from nepi.resources.linux.node import LinuxNode
108 nodes = simu.get_connected(LinuxNode.get_rtype())
109 self._fd2node = nodes[0]
111 if self._fd1node.get("hostname") == \
112 self._fd2node.get("hostname"):
113 msg = "linux::ns3::FdUdpTunnel requires endpoints on different hosts"
115 raise RuntimeError(msg)
117 return [self._fd1, self._fd2]
139 def endpoint_node(self, endpoint):
141 if endpoint == self.endpoint1:
148 def app_home(self, endpoint):
149 node = self.endpoint_node(endpoint)
150 return os.path.join(node.exp_home, self._home)
152 def run_home(self, endpoint):
153 return os.path.join(self.app_home(endpoint), self.ec.run_id)
155 def upload_sources(self, endpoint):
158 # vif-passfd python script
159 linux_passfd = os.path.join(os.path.dirname(__file__),
164 scripts.append(linux_passfd)
166 # tunnel creation python script
167 tunchannel = os.path.join(os.path.dirname(__file__),
172 scripts.append(tunchannel)
175 scripts = ";".join(scripts)
177 node = self.endpoint_node(endpoint)
179 os.path.join(node.src_dir),
182 def endpoint_mkdir(self, endpoint):
183 node = self.endpoint_node(endpoint)
184 run_home = self.run_home(endpoint)
187 def initiate_connection(self, endpoint, remote_endpoint):
188 cipher = self.get("cipher")
189 cipher_key = self.get("cipherKey")
190 bwlimit = self.get("bwLimit")
191 txqueuelen = self.get("txQueueLen")
193 # Upload the tunnel creating script
194 self.upload_sources(endpoint)
196 # Request an address to send the file descriptor to the ns-3 simulation
197 address = endpoint.recv_fd()
199 # execute the tunnel creation script
200 node = self.endpoint_node(remote_endpoint)
201 port = self.initiate(endpoint, remote_endpoint, address, cipher,
202 cipher_key, bwlimit, txqueuelen)
206 def establish_connection(self, endpoint, remote_endpoint, port):
207 self.establish(endpoint, remote_endpoint, port)
209 def verify_connection(self, endpoint, remote_endpoint):
210 self.verify(endpoint)
212 def terminate_connection(self, endpoint, remote_endpoint):
216 def check_state_connection(self):
217 # Make sure the process is still running in background
218 # No execution errors occurred. Make sure the background
219 # process with the recorded pid is still running.
221 node1 = self.endpoint_node(self.endpoint1)
222 node2 = self.endpoint_node(self.endpoint2)
223 run_home1 = self.run_home(self.endpoint1)
224 run_home2 = self.run_home(self.endpoint1)
225 (pid1, ppid1) = self._pids[endpoint1]
226 (pid2, ppid2) = self._pids[endpoint2]
228 status1 = node1.status(pid1, ppid1)
229 status2 = node2.status(pid2, ppid2)
231 if status1 == ProcStatus.FINISHED and \
232 status2 == ProcStatus.FINISHED:
234 # check if execution errors occurred
235 (out1, err1), proc1 = node1.check_errors(run_home1)
236 (out2, err2), proc2 = node2.check_errors(run_home2)
239 msg = "Error occurred in tunnel"
240 self.error(msg, err1, err2)
245 def wait_local_port(self, endpoint):
246 """ Waits until the local_port file for the endpoint is generated,
247 and returns the port number
250 return self.wait_file(endpoint, "local_port")
252 def wait_result(self, endpoint):
253 """ Waits until the return code file for the endpoint is generated
256 return self.wait_file(endpoint, "ret_file")
258 def wait_file(self, endpoint, filename):
259 """ Waits until file on endpoint is generated """
263 node = self.endpoint_node(endpoint)
264 run_home = self.run_home(endpoint)
267 (out, err), proc = node.check_output(run_home, filename)
276 msg = "Couldn't retrieve %s" % filename
277 self.error(msg, out, err)
278 raise RuntimeError(msg)
282 def initiate(self, endpoint, remote_endpoint, address, cipher, cipher_key,
283 bwlimit, txqueuelen):
285 command = self._initiate_command(endpoint, remote_endpoint,
286 address, cipher, cipher_key, bwlimit, txqueuelen)
288 node = self.endpoint_node(endpoint)
289 run_home = self.run_home(endpoint)
290 app_home = self.app_home(endpoint)
292 # upload command to connect.sh script
293 shfile = os.path.join(app_home, "fd-udp-connect.sh")
294 node.upload_command(command,
298 # invoke connect script
299 cmd = "bash %s" % shfile
300 (out, err), proc = node.run(cmd, run_home)
302 # check if execution errors occurred
303 msg = "Failed to connect endpoints "
306 self.error(msg, out, err)
307 raise RuntimeError(msg)
309 # Wait for pid file to be generated
310 pid, ppid = node.wait_pid(run_home)
312 self._pids[endpoint] = (pid, ppid)
314 # Check for error information on the remote machine
315 (out, err), proc = node.check_errors(run_home)
316 # Out is what was written in the stderr file
318 msg = " Failed to start command '%s' " % command
319 self.error(msg, out, err)
320 raise RuntimeError(msg)
322 port = self.wait_local_port(endpoint)
326 def _initiate_command(self, endpoint, remote_endpoint, address,
327 cipher, cipher_key, bwlimit, txqueuelen):
328 local_node = self.endpoint_node(endpoint)
329 local_run_home = self.run_home(endpoint)
330 local_app_home = self.app_home(endpoint)
331 remote_node = self.endpoint_node(remote_endpoint)
333 local_ip = local_node.get("ip")
334 remote_ip = remote_node.get("ip")
336 local_port_file = os.path.join(local_run_home, "local_port")
337 remote_port_file = os.path.join(local_run_home, "remote_port")
338 ret_file = os.path.join(local_run_home, "ret_file")
340 address = base64.b64encode(address)
343 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
344 command.append("python ${SRC}/fd-udp-connect.py")
345 command.append("-a %s" % address)
346 command.append("-p %s" % local_port_file)
347 command.append("-P %s" % remote_port_file)
348 command.append("-o %s" % local_ip)
349 command.append("-O %s" % remote_ip)
350 command.append("-R %s" % ret_file)
351 command.append("-t %s" % "IFF_TAP")
355 command.append("-c %s" % cipher)
357 command.append("-k %s " % cipher_key)
359 command.append("-q %s " % txqueuelen)
361 command.append("-b %s " % bwlimit)
363 command = " ".join(command)
364 command = self.replace_paths(command, node=local_node,
365 app_home=local_app_home, run_home=local_run_home)
369 def establish(self, endpoint, remote_endpoint, port):
370 node = self.endpoint_node(endpoint)
371 run_home = self.run_home(endpoint)
373 # upload remote port number to file
374 remote_port = "%s\n" % port
375 node.upload(remote_port,
376 os.path.join(run_home, "remote_port"),
380 def verify(self, endpoint):
381 self.wait_result(endpoint)