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>
19 from nepi.execution.attribute import Attribute, Flags
20 from nepi.execution.resource import clsinit_copy
21 from nepi.execution.trace import Trace
22 from nepi.resources.ns3.ns3base import NS3Base
25 PY2 = sys.version_info[0] == 2
33 class NS3BaseNetDevice(NS3Base):
34 _rtype = "abstract::ns3::NetDevice"
37 def _register_attributes(cls):
38 mac = Attribute("mac", "MAC address for device",
41 ip = Attribute("ip", "IP address for device",
44 prefix = Attribute("prefix", "Network prefix for device",
47 cls._register_attribute(mac)
48 cls._register_attribute(ip)
49 cls._register_attribute(prefix)
52 def _register_traces(cls):
53 pcap = Trace("pcap", "Dump traffic sniffed on the network device in Pcap format")
54 promisc_pcap = Trace("promiscPcap", "Dump traffic sniffed in promiscuous mode on the network device in Pcap format")
55 ascii = Trace("ascii", "Dump traffic sniffed on the network device in Ascii format")
57 cls._register_trace(pcap)
58 cls._register_trace(promisc_pcap)
59 cls._register_trace(ascii)
61 def __init__(self, ec, guid):
62 super(NS3BaseNetDevice, self).__init__(ec, guid)
63 self._ascii_helper_uuid = None
64 self._device_helper_uuid = None
68 from nepi.resources.ns3.ns3node import NS3BaseNode
69 nodes = self.get_connected(NS3BaseNode.get_rtype())
72 msg = "Device not connected to node"
74 raise RuntimeError(msg)
80 from nepi.resources.ns3.ns3channel import NS3BaseChannel
81 channels = self.get_connected(NS3BaseChannel.get_rtype())
84 msg = "Device not connected to channel"
86 raise RuntimeError(msg)
92 from nepi.resources.ns3.ns3queue import NS3BaseQueue
93 queue = self.get_connected(NS3BaseQueue.get_rtype())
96 msg = "Device not connected to queue"
98 raise RuntimeError(msg)
103 def ascii_helper_uuid(self):
104 if not self._ascii_helper_uuid:
105 self._ascii_helper_uuid = self.simulation.create("AsciiTraceHelper")
106 return self._ascii_helper_uuid
109 def device_helper_uuid(self):
110 if not self._device_helper_uuid:
111 rtype = self.get_rtype()
112 if rtype == "ns3::PointToPointNetDevice":
113 classname = "PointToPointHelper"
114 elif rtype == "ns3::CsmaNetDevice":
115 classname = "CsmaHelper"
116 elif rtype == "ns3::EmuNetDevice":
117 classname = "EmuHelper"
118 elif rtype == "ns3::FdNetDevice":
119 classname = "FdNetDeviceHelper"
120 elif rtype in [ "ns3::BaseStationNetDevice", "SubscriberStationNetDevice" ]:
121 classname = "WimaxHelper"
122 elif rtype == "ns3::WifiNetDevice":
123 classname = "YansWifiPhyHelper"
124 elif rtype == "ns3::FdNetDevice":
125 classname = "FdNetDeviceHelper"
127 self._device_helper_uuid = self.simulation.create(classname)
129 return self._device_helper_uuid
132 def _rms_to_wait(self):
133 rms = set([self.node, self.channel])
136 def _configure_object(self):
138 self._configure_mac_address()
141 self._configure_ip_address()
144 self._configure_traces()
146 def _configure_mac_address(self):
147 mac = self.get("mac")
149 mac_uuid = self.simulation.create("Mac48Address", mac)
151 mac_uuid = self.simulation.invoke("singleton::Mac48Address", "Allocate")
153 self.simulation.invoke(self.uuid, "SetAddress", mac_uuid)
155 def _configure_ip_address(self):
157 prefix = self.get("prefix")
160 i = ipaddr.IPAddress(ip)
162 i = ipaddress.ip_address(ip)
165 ipv4 = self.node.ipv4
166 ifindex_uuid = self.simulation.invoke(ipv4.uuid, "AddInterface",
168 ipv4_addr_uuid = self.simulation.create("Ipv4Address", ip)
169 ipv4_mask_uuid = self.simulation.create("Ipv4Mask", "/%s" % str(prefix))
170 inaddr_uuid = self.simulation.create("Ipv4InterfaceAddress",
171 ipv4_addr_uuid, ipv4_mask_uuid)
172 self.simulation.invoke(ipv4.uuid, "AddAddress", ifindex_uuid,
174 self.simulation.invoke(ipv4.uuid, "SetMetric", ifindex_uuid, 1)
175 self.simulation.invoke(ipv4.uuid, "SetUp", ifindex_uuid)
181 def _configure_traces(self):
182 if self.trace_enabled("pcap"):
183 helper_uuid = self.device_helper_uuid
185 filename = "trace-pcap-netdev-%d.pcap" % self.guid
186 self._trace_filename["pcap"] = filename
188 filepath = self.simulation.trace_filepath(filename)
190 self.simulation.invoke(helper_uuid, "EnablePcap", filepath,
191 self.uuid, promiscuous = False, explicitFilename = True)
193 if self.trace_enabled("promiscPcap"):
194 helper_uuid = self.device_helper_uuid
196 filename = "trace-promisc-pcap-netdev-%d.pcap" % self.guid
197 self._trace_filename["promiscPcap"] = filename
199 filepath = self.simulation.trace_filepath(filename)
201 self.simulation.invoke(helper_uuid, "EnablePcap", filepath,
202 self.uuid, promiscuous = True, explicitFilename = True)
204 if self.trace_enabled("ascii"):
205 helper_uuid = self.device_helper_uuid
206 ascii_helper_uuid = self.ascii_helper_uuid
208 filename = "trace-ascii-netdev-%d.tr" % self.guid
209 self._trace_filename["ascii"] = filename
211 filepath = self.simulation.trace_filepath(filename)
212 stream_uuid = self.simulation.invoke(ascii_helper_uuid,
213 "CreateFileStream", filepath)
214 self.simulation.invoke(helper_uuid, "EnableAscii", stream_uuid,
217 def _connect_object(self):
219 if node and node.uuid not in self.connected:
220 self.simulation.invoke(node.uuid, "AddDevice", self.uuid)
221 self._connected.add(node.uuid)
223 channel = self.channel
224 if channel and channel.uuid not in self.connected:
225 self.simulation.invoke(self.uuid, "Attach", channel.uuid)
226 self._connected.add(channel.uuid)
228 # Verify that the device has a queue. If no queue is added a segfault