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 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>
19 from nepi.execution.resource import ResourceState, clsinit_copy
20 from nepi.resources.linux.application import LinuxApplication
29 class LinuxTunTapFdLink(LinuxApplication):
30 """ Interconnects a TAP or TUN Linux device to a FdNetDevice
32 _rtype = "linux::ns3::TunTapFdLink"
34 def __init__(self, ec, guid):
35 super(LinuxTunTapFdLink, self).__init__(ec, guid)
37 self._fdnetdevice = None
39 self.send_address = None
40 self._home = "tuntap-link-%s" % self.guid
43 def fdnetdevice(self):
44 if not self._fdnetdevice:
45 from nepi.resources.ns3.ns3fdnetdevice import NS3BaseFdNetDevice
46 devices = self.get_connected(NS3BaseFdNetDevice.get_rtype())
47 if not devices or len(devices) != 1:
48 msg = "TunTapFdLink must be connected to exactly one FdNetDevice"
50 raise RuntimeError, msg
52 self._fdnetdevice = devices[0]
54 simu = self._fdnetdevice.simulation
55 from nepi.resources.linux.node import LinuxNode
56 nodes = simu.get_connected(LinuxNode.get_rtype())
57 self._fd_node = nodes[0]
59 return self._fdnetdevice
68 from nepi.resources.linux.tap import LinuxTap
69 devices = self.get_connected(LinuxTap.get_rtype())
70 if not devices or len(devices) != 1:
71 msg = "TunTapLink must be connected to exactly one Tap or Tun"
73 raise RuntimeError, msg
75 self._tap = devices[0]
87 def upload_sources(self):
90 # vif-passfd python script
91 linux_passfd = os.path.join(os.path.dirname(__file__),
94 "linux-tap-passfd.py")
96 scripts.append(linux_passfd)
99 scripts = ";".join(scripts)
101 self.node.upload(scripts,
102 os.path.join(self.node.src_dir),
105 def upload_start_command(self):
106 if self.tapnode.get("hostname") != \
107 self.fdnode.get("hostname"):
108 msg = "Tap and FdNetDevice are not in the same host"
110 raise RuntimeError, msg
112 self.send_address = self.fdnetdevice.recv_fd()
113 self.set("command", self._start_command)
115 command = self.get("command")
117 shfile = os.path.join(self.app_home, "start.sh")
118 self.node.run_and_wait(command, self.run_home,
124 if self.tap.state < ResourceState.READY or \
125 self.fdnetdevice.state < ResourceState.READY:
126 self.debug("---- RESCHEDULING DEPLOY ---- ")
127 self.ec.schedule(self.reschedule_delay, self.deploy)
135 if self.state == ResourceState.READY:
136 command = self.get("command")
137 self.info("Starting command '%s'" % command)
141 msg = " Failed to execute command '%s'" % command
142 self.error(msg, out, err)
143 raise RuntimeError, msg
146 def _start_command(self):
147 address = base64.b64encode(self.send_address)
150 # Use tap-passfd.py to send fd from TAP to FdNetDevice
151 command.append("sudo -S")
152 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
153 command.append("python ${SRC}/linux-tap-passfd.py")
154 command.append("-a %s" % address)
155 command.append("-S %s " % self.tap.sock_name)
157 command = " ".join(command)
158 command = self.replace_paths(command)