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
21 from nepi.execution.resource import clsinit_copy
22 from nepi.execution.trace import Trace
23 from nepi.resources.ns3.ns3base import NS3Base
27 # TODO: Validate that device must be connected to queue!! If not a segmentation fault occurs
30 class NS3BaseNetDevice(NS3Base):
31 _rtype = "abstract::ns3::NetDevice"
34 def _register_attributes(cls):
35 mac = Attribute("mac", "MAC address for device",
38 ip = Attribute("ip", "IP address for device",
41 prefix = Attribute("prefix", "Network prefix for device",
44 cls._register_attribute(mac)
45 cls._register_attribute(ip)
46 cls._register_attribute(prefix)
49 def _register_traces(cls):
50 pcap = Trace("pcap", "Dump traffic sniffed on the network device in Pcap format")
51 promisc_pcap = Trace("promiscPcap", "Dump traffic sniffed in promiscuous mode on the network device in Pcap format")
52 ascii = Trace("ascii", "Dump traffic sniffed on the network device in Ascii format")
54 cls._register_trace(pcap)
55 cls._register_trace(promisc_pcap)
56 cls._register_trace(ascii)
58 def __init__(self, ec, guid):
59 super(NS3BaseNetDevice, self).__init__(ec, guid)
60 self._ascii_helper_uuid = None
61 self._device_helper_uuid = None
65 from nepi.resources.ns3.ns3node import NS3BaseNode
66 nodes = self.get_connected(NS3BaseNode.get_rtype())
69 msg = "Device not connected to node"
71 raise RuntimeError, msg
77 from nepi.resources.ns3.ns3channel import NS3BaseChannel
78 channels = self.get_connected(NS3BaseChannel.get_rtype())
81 msg = "Device not connected to channel"
83 raise RuntimeError, msg
88 def ascii_helper_uuid(self):
89 if not self._ascii_helper_uuid:
90 self._ascii_helper_uuid = self.simulation.create("AsciiTraceHelper")
91 return self._ascii_helper_uuid
94 def device_helper_uuid(self):
95 if not self._device_helper_uuid:
96 rtype = self.get_rtype()
97 if rtype == "ns3::PointToPointNetDevice":
98 classname = "PointToPointHelper"
99 elif rtype == "ns3::CsmaNetDevice":
100 classname = "CsmaHelper"
101 elif rtype == "ns3::EmuNetDevice":
102 classname = "EmuHelper"
103 elif rtype == "ns3::FdNetDevice":
104 classname = "FdNetDeviceHelper"
105 elif rtype in [ "ns3::BaseStationNetDevice", "SubscriberStationNetDevice" ]:
106 classname = "WimaxHelper"
107 elif rtype == "ns3::WifiNetDevice":
108 classname = "YansWifiPhyHelper"
110 self._device_helper_uuid = self.simulation.create(classname)
112 return self._device_helper_uuid
115 def _rms_to_wait(self):
125 rms.add(self.channel)
128 def _configure_object(self):
130 self._configure_mac_address()
133 self._configure_ip_address()
136 self._configure_traces()
138 def _configure_mac_address(self):
139 mac = self.get("mac")
141 mac_uuid = self.simulation.create("Mac48Address", mac)
143 mac_uuid = self.simulation.invoke("singleton::Mac48Address", "Allocate")
145 self.simulation.invoke(self.uuid, "SetAddress", mac_uuid)
147 def _configure_ip_address(self):
149 prefix = self.get("prefix")
151 i = ipaddr.IPAddress(ip)
154 ipv4 = self.node.ipv4
155 ifindex_uuid = self.simulation.invoke(ipv4.uuid, "AddInterface",
157 ipv4_addr_uuid = self.simulation.create("Ipv4Address", ip)
158 ipv4_mask_uuid = self.simulation.create("Ipv4Mask", "/%s" % str(prefix))
159 inaddr_uuid = self.simulation.create("Ipv4InterfaceAddress",
160 ipv4_addr_uuid, ipv4_mask_uuid)
161 self.simulation.invoke(ipv4.uuid, "AddAddress", ifindex_uuid,
163 self.simulation.invoke(ipv4.uuid, "SetMetric", ifindex_uuid, 1)
164 self.simulation.invoke(ipv4.uuid, "SetUp", ifindex_uuid)
170 def _configure_traces(self):
171 if self.trace_enabled("pcap"):
172 helper_uuid = self.device_helper_uuid
174 filename = "trace-pcap-netdev-%d.pcap" % self.guid
175 self._trace_filename["pcap"] = filename
177 filepath = self.simulation.trace_filepath(filename)
179 self.simulation.invoke(helper_uuid, "EnablePcap", filepath,
180 self.uuid, promiscuous = False, explicitFilename = True)
182 if self.trace_enabled("promiscPcap"):
183 helper_uuid = self.device_helper_uuid
185 filename = "trace-promisc-pcap-netdev-%d.pcap" % self.guid
186 self._trace_filename["promiscPcap"] = filename
188 filepath = self.simulation.trace_filepath(filename)
190 self.simulation.invoke(helper_uuid, "EnablePcap", filepath,
191 self.uuid, promiscuous = True, explicitFilename = True)
193 if self.trace_enabled("ascii"):
194 helper_uuid = self.device_helper_uuid
195 ascii_helper_uuid = self.ascii_helper_uuid
197 filename = "trace-ascii-netdev-%d.tr" % self.guid
198 self._trace_filename["ascii"] = filename
200 filepath = self.simulation.trace_filepath(filename)
201 stream_uuid = self.simulation.invoke(ascii_helper_uuid,
202 "CreateFileStream", filepath)
203 self.simulation.invoke(helper_uuid, "EnableAscii", stream_uuid,
206 def _connect_object(self):
208 if node and node.uuid not in self.connected:
209 self.simulation.invoke(node.uuid, "AddDevice", self.uuid)
210 self._connected.add(node.uuid)
212 channel = self.channel
213 if channel and channel.uuid not in self.connected:
214 self.simulation.invoke(self.uuid, "Attach", channel.uuid)
215 self._connected.add(channel.uuid)