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
27 class NS3BaseNetDevice(NS3Base):
28 _rtype = "abstract::ns3::NetDevice"
31 def _register_attributes(cls):
32 mac = Attribute("mac", "MAC address for device",
35 ip = Attribute("ip", "IP address for device",
38 prefix = Attribute("prefix", "Network prefix for device",
41 cls._register_attribute(mac)
42 cls._register_attribute(ip)
43 cls._register_attribute(prefix)
46 def _register_traces(cls):
47 pcap = Trace("pcap", "Dump traffic sniffed on the network device in Pcap format")
48 promisc_pcap = Trace("promiscPcap", "Dump traffic sniffed in promiscuous mode on the network device in Pcap format")
49 ascii = Trace("ascii", "Dump traffic sniffed on the network device in Ascii format")
51 cls._register_trace(pcap)
52 cls._register_trace(promisc_pcap)
53 cls._register_trace(ascii)
55 def __init__(self, ec, guid):
56 super(NS3BaseNetDevice, self).__init__(ec, guid)
57 self._ascii_helper_uuid = None
58 self._device_helper_uuid = None
62 from nepi.resources.ns3.ns3node import NS3BaseNode
63 nodes = self.get_connected(NS3BaseNode.get_rtype())
66 msg = "Device not connected to node"
68 raise RuntimeError(msg)
74 from nepi.resources.ns3.ns3channel import NS3BaseChannel
75 channels = self.get_connected(NS3BaseChannel.get_rtype())
78 msg = "Device not connected to channel"
80 raise RuntimeError(msg)
86 from nepi.resources.ns3.ns3queue import NS3BaseQueue
87 queue = self.get_connected(NS3BaseQueue.get_rtype())
90 msg = "Device not connected to queue"
92 raise RuntimeError(msg)
97 def ascii_helper_uuid(self):
98 if not self._ascii_helper_uuid:
99 self._ascii_helper_uuid = self.simulation.create("AsciiTraceHelper")
100 return self._ascii_helper_uuid
103 def device_helper_uuid(self):
104 if not self._device_helper_uuid:
105 rtype = self.get_rtype()
106 if rtype == "ns3::PointToPointNetDevice":
107 classname = "PointToPointHelper"
108 elif rtype == "ns3::CsmaNetDevice":
109 classname = "CsmaHelper"
110 elif rtype == "ns3::EmuNetDevice":
111 classname = "EmuHelper"
112 elif rtype == "ns3::FdNetDevice":
113 classname = "FdNetDeviceHelper"
114 elif rtype in [ "ns3::BaseStationNetDevice", "SubscriberStationNetDevice" ]:
115 classname = "WimaxHelper"
116 elif rtype == "ns3::WifiNetDevice":
117 classname = "YansWifiPhyHelper"
118 elif rtype == "ns3::FdNetDevice":
119 classname = "FdNetDeviceHelper"
121 self._device_helper_uuid = self.simulation.create(classname)
123 return self._device_helper_uuid
126 def _rms_to_wait(self):
127 rms = set([self.node, self.channel])
130 def _configure_object(self):
132 self._configure_mac_address()
135 self._configure_ip_address()
138 self._configure_traces()
140 def _configure_mac_address(self):
141 mac = self.get("mac")
143 mac_uuid = self.simulation.create("Mac48Address", mac)
145 mac_uuid = self.simulation.invoke("singleton::Mac48Address", "Allocate")
147 self.simulation.invoke(self.uuid, "SetAddress", mac_uuid)
149 def _configure_ip_address(self):
151 prefix = self.get("prefix")
153 i = ipaddress.ip_address(ip)
156 ipv4 = self.node.ipv4
157 ifindex_uuid = self.simulation.invoke(ipv4.uuid, "AddInterface",
159 ipv4_addr_uuid = self.simulation.create("Ipv4Address", ip)
160 ipv4_mask_uuid = self.simulation.create("Ipv4Mask", "/%s" % str(prefix))
161 inaddr_uuid = self.simulation.create("Ipv4InterfaceAddress",
162 ipv4_addr_uuid, ipv4_mask_uuid)
163 self.simulation.invoke(ipv4.uuid, "AddAddress", ifindex_uuid,
165 self.simulation.invoke(ipv4.uuid, "SetMetric", ifindex_uuid, 1)
166 self.simulation.invoke(ipv4.uuid, "SetUp", ifindex_uuid)
172 def _configure_traces(self):
173 if self.trace_enabled("pcap"):
174 helper_uuid = self.device_helper_uuid
176 filename = "trace-pcap-netdev-%d.pcap" % self.guid
177 self._trace_filename["pcap"] = filename
179 filepath = self.simulation.trace_filepath(filename)
181 self.simulation.invoke(helper_uuid, "EnablePcap", filepath,
182 self.uuid, promiscuous = False, explicitFilename = True)
184 if self.trace_enabled("promiscPcap"):
185 helper_uuid = self.device_helper_uuid
187 filename = "trace-promisc-pcap-netdev-%d.pcap" % self.guid
188 self._trace_filename["promiscPcap"] = filename
190 filepath = self.simulation.trace_filepath(filename)
192 self.simulation.invoke(helper_uuid, "EnablePcap", filepath,
193 self.uuid, promiscuous = True, explicitFilename = True)
195 if self.trace_enabled("ascii"):
196 helper_uuid = self.device_helper_uuid
197 ascii_helper_uuid = self.ascii_helper_uuid
199 filename = "trace-ascii-netdev-%d.tr" % self.guid
200 self._trace_filename["ascii"] = filename
202 filepath = self.simulation.trace_filepath(filename)
203 stream_uuid = self.simulation.invoke(ascii_helper_uuid,
204 "CreateFileStream", filepath)
205 self.simulation.invoke(helper_uuid, "EnableAscii", stream_uuid,
208 def _connect_object(self):
210 if node and node.uuid not in self.connected:
211 self.simulation.invoke(node.uuid, "AddDevice", self.uuid)
212 self._connected.add(node.uuid)
214 channel = self.channel
215 if channel and channel.uuid not in self.connected:
216 self.simulation.invoke(self.uuid, "Attach", channel.uuid)
217 self._connected.add(channel.uuid)
219 # Verify that the device has a queue. If no queue is added a segfault