Serious refactoring of TUN/TAP and tunnel code. Linux udp/gre tunnels not yet functional
[nepi.git] / src / nepi / resources / planetlab / ns3 / tuntapfdlink.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 INRIA
4 #
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.
9 #
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.
14 #
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/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.resource import ResourceState, clsinit_copy
21 from nepi.resources.linux.ns3.tuntapfdlink import LinuxTunTapFdLink
22
23 import base64
24 import fcntl
25 import os
26 import socket
27 import struct
28
29 @clsinit_copy
30 class PlanetlabTunTapFdLink(LinuxTunTapFdLink):
31     """ Interconnects a TAP or TUN PlanetLab device to a FdNetDevice
32     """
33     _rtype = "planetlab::ns3::TunTapFdLink"
34
35     def __init__(self, ec, guid):
36         super(PlanetlabTunTapFdLink, self).__init__(ec, guid)
37
38     @property
39     def fdnetdevice(self):
40         if not self._fdnetdevice:
41             from nepi.resources.ns3.ns3fdnetdevice import NS3BaseFdNetDevice
42             devices = self.get_connected(NS3BaseFdNetDevice.get_rtype())
43             if not devices or len(devices) != 1: 
44                 msg = "planetlab::ns3::TunTapFdLink must be connected to exactly one FdNetDevice"
45                 self.error(msg)
46                 raise RuntimeError, msg
47
48             self._fdnetdevice = devices[0]
49
50             # Set PI headers on
51             self._fdnetdevice.set("EncapsulationMode", "DixPi")
52         
53             simu = self._fdnetdevice.simulation
54             from nepi.resources.planetlab.node import PlanetlabNode
55             nodes = simu.get_connected(PlanetlabNode.get_rtype())
56             self._fd_node = nodes[0]
57         
58         return self._fdnetdevice
59
60     @property
61     def tap(self):
62         if not self._tap:
63             from nepi.resources.planetlab.tap import PlanetlabTap
64             devices = self.get_connected(PlanetlabTap.get_rtype())
65
66             if not devices or len(devices) != 1: 
67                 msg = "planetlab::ns3::TunTapFdLink must be connected to exactly one PlanetlabTap"
68                 self.error(msg)
69                 raise RuntimeError, msg
70
71             self._tap = devices[0]
72         
73         return self._tap
74
75     def upload_sources(self):
76         scripts = []
77
78         # vif-passfd python script
79         pl_vif_passfd = os.path.join(os.path.dirname(__file__), 
80                 "..", "..",
81                 "linux",
82                 "scripts",
83                 "linux-tap-passfd.py")
84
85         scripts.append(pl_vif_passfd)
86         
87         # Upload scripts
88         scripts = ";".join(scripts)
89
90         self.node.upload(scripts,
91                 os.path.join(self.node.src_dir),
92                 overwrite = False)
93