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>
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import ResourceManager, ResourceState, \
30 class TapFdLink(ResourceManager):
31 """ Interconnects a TAP or TUN Linux device to a FdNetDevice
33 _rtype = "linux::ns3::TapFdLink"
35 def __init__(self, ec, guid):
36 super(TapFdLink, self).__init__(ec, guid)
38 self._fdnetdevice = None
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 = "TapFdLink must be connected to exactly one FdNetDevices"
49 raise RuntimeError, msg
51 self._fdnetdevice = devices[0]
53 return self._fdnetdevice
57 return self.fdnetdevice.node
62 from nepi.resources.linux.tap import LinuxTap
63 devices = self.get_connected(LinuxTap.get_rtype())
64 if not devices or len(devices) != 1:
65 msg = "TapFdLink must be connected to exactly one LinuxTap"
67 raise RuntimeError, msg
69 self._tap = devices[0]
77 def do_provision(self):
79 fdnetdevice = self.fdnetdevice
81 vif_name = self.ec.get(tap.guid, "deviceName")
82 vif_type = tap.vif_type_flag
83 pi = self.ec.get(tap.guid, "pi")
85 self._fd = self.open_tap(vif_name, vif_type, pi)
87 fdnetdevice.send_fd(self._fd)
89 super(TapFdLink, self).do_provision()
92 if self.tap.state < ResourceState.READY or \
93 self.fdnetdevice.state < ResourceState.READY:
94 self.ec.schedule(self.reschedule_delay, self.deploy)
99 super(TapFdLink, self).do_deploy()
101 def open_tap(self, vif_name, vif_type, pi):
103 TUNSETIFF = 0x400454ca
111 fd = os.open("/dev/net/tun", os.O_RDWR)
113 err = fcntl.ioctl(fd, TUNSETIFF, struct.pack("16sH", vif_name, flags))
116 raise RuntimeError("Could not configure device %s" % vif_name)