2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2014 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>
18 from nepi.execution.resource import ResourceState, clsinit_copy
19 from nepi.resources.linux.application import LinuxApplication
28 class LinuxTunTapFdLink(LinuxApplication):
29 """ Interconnects a TAP or TUN Linux device to a FdNetDevice
31 _rtype = "linux::ns3::TunTapFdLink"
33 def __init__(self, ec, guid):
34 super(LinuxTunTapFdLink, self).__init__(ec, guid)
36 self._fdnetdevice = None
38 self.send_address = None
39 self._home = "tuntap-link-%s" % self.guid
42 def fdnetdevice(self):
43 if not self._fdnetdevice:
44 from nepi.resources.ns3.ns3fdnetdevice import NS3BaseFdNetDevice
45 devices = self.get_connected(NS3BaseFdNetDevice.get_rtype())
46 if not devices or len(devices) != 1:
47 msg = "TunTapFdLink must be connected to exactly one FdNetDevice"
49 raise RuntimeError(msg)
51 self._fdnetdevice = devices[0]
53 simu = self._fdnetdevice.simulation
54 from nepi.resources.linux.node import LinuxNode
55 nodes = simu.get_connected(LinuxNode.get_rtype())
56 self._fd_node = nodes[0]
58 return self._fdnetdevice
67 from nepi.resources.linux.tap import LinuxTap
68 devices = self.get_connected(LinuxTap.get_rtype())
69 if not devices or len(devices) != 1:
70 msg = "TunTapLink must be connected to exactly one Tap or Tun"
72 raise RuntimeError(msg)
74 self._tap = devices[0]
86 def upload_sources(self):
89 # vif-passfd python script
90 linux_passfd = os.path.join(os.path.dirname(__file__),
93 "linux-tap-passfd.py")
95 scripts.append(linux_passfd)
98 scripts = ";".join(scripts)
100 self.node.upload(scripts,
101 os.path.join(self.node.src_dir),
104 def upload_start_command(self):
105 if self.tapnode.get("hostname") != \
106 self.fdnode.get("hostname"):
107 msg = "Tap and FdNetDevice are not in the same host"
109 raise RuntimeError(msg)
111 self.send_address = self.fdnetdevice.recv_fd()
112 self.set("command", self._start_command)
114 command = self.get("command")
116 shfile = os.path.join(self.app_home, "start.sh")
117 self.node.run_and_wait(command, self.run_home,
123 if self.tap.state < ResourceState.READY or \
124 self.fdnetdevice.state < ResourceState.READY:
125 self.debug("---- RESCHEDULING DEPLOY ---- ")
126 self.ec.schedule(self.reschedule_delay, self.deploy)
134 if self.state == ResourceState.READY:
135 command = self.get("command")
136 self.info("Starting command '%s'" % command)
140 msg = " Failed to execute command '%s'" % command
141 self.error(msg, out, err)
142 raise RuntimeError(msg)
145 def _start_command(self):
146 address = base64.b64encode(self.send_address)
149 # Use tap-passfd.py to send fd from TAP to FdNetDevice
150 command.append("sudo -S")
151 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
152 command.append("python ${SRC}/linux-tap-passfd.py")
153 command.append("-a %s" % address)
154 command.append("-S %s " % self.tap.sock_name)
156 command = " ".join(command)
157 command = self.replace_paths(command)