#124 - Allow the ns-3 sources to be built in debug mode
[nepi.git] / src / nepi / resources / ns3 / ns3netdevice.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.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
24
25 import ipaddr
26
27 # TODO: Validate that device must be connected to queue!! If not a segmentation fault occurs
28
29 @clsinit_copy
30 class NS3BaseNetDevice(NS3Base):
31     _rtype = "abstract::ns3::NetDevice"
32
33     @classmethod
34     def _register_attributes(cls):
35         mac = Attribute("mac", "MAC address for device",
36                 flags = Flags.Design)
37
38         ip = Attribute("ip", "IP address for device",
39                 flags = Flags.Design)
40
41         prefix = Attribute("prefix", "Network prefix for device",
42                 flags = Flags.Design)
43
44         cls._register_attribute(mac)
45         cls._register_attribute(ip)
46         cls._register_attribute(prefix)
47
48     @classmethod
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")
53
54         cls._register_trace(pcap)
55         cls._register_trace(promisc_pcap)
56         cls._register_trace(ascii)
57
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
62
63     @property
64     def node(self):
65         from nepi.resources.ns3.ns3node import NS3BaseNode
66         nodes = self.get_connected(NS3BaseNode.get_rtype())
67
68         if not nodes: 
69             msg = "Device not connected to node"
70             self.error(msg)
71             raise RuntimeError, msg
72
73         return nodes[0]
74
75     @property
76     def channel(self):
77         from nepi.resources.ns3.ns3channel import NS3BaseChannel
78         channels = self.get_connected(NS3BaseChannel.get_rtype())
79
80         if not channels: 
81             msg = "Device not connected to channel"
82             self.error(msg)
83             raise RuntimeError, msg
84
85         return channels[0]
86
87     @property
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
92
93     @property
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"
109
110             self._device_helper_uuid = self.simulation.create(classname)
111
112         return self._device_helper_uuid
113
114     @property
115     def _rms_to_wait(self):
116         others = set()
117         
118         node = self.node
119         others.add(node)
120
121         ipv4 = node.ipv4
122         if node.ipv4:
123             others.add(ipv4)
124
125         others.add(self.channel)
126         return others
127
128     def _configure_object(self):
129         # Set Mac
130         mac = self.get("mac")
131         if mac:
132             mac_uuid = self.simulation.create("Mac48Address", mac)
133         else:
134             mac_uuid = self.simulation.invoke("singleton::Mac48Address", "Allocate")
135         self.simulation.invoke(self.uuid, "SetAddress", mac_uuid)
136
137         # Set IP address
138         ip = self.get("ip")
139         prefix = self.get("prefix")
140
141         i = ipaddr.IPAddress(ip)
142         if i.version == 4:
143             # IPv4
144             ipv4 = self.node.ipv4
145             ifindex_uuid = self.simulation.invoke(ipv4.uuid, "AddInterface", 
146                     self.uuid)
147             ipv4_addr_uuid = self.simulation.create("Ipv4Address", ip)
148             ipv4_mask_uuid = self.simulation.create("Ipv4Mask", "/%s" % str(prefix))
149             inaddr_uuid = self.simulation.create("Ipv4InterfaceAddress", 
150                     ipv4_addr_uuid, ipv4_mask_uuid)
151             self.simulation.invoke(ipv4.uuid, "AddAddress", ifindex_uuid, 
152                     inaddr_uuid)
153             self.simulation.invoke(ipv4.uuid, "SetMetric", ifindex_uuid, 1)
154             self.simulation.invoke(ipv4.uuid, "SetUp", ifindex_uuid)
155         else:
156             # IPv6
157             # TODO!
158             pass
159         
160         # Enable traces
161         self._configure_traces()
162
163     def _configure_traces(self):
164         if self.trace_enabled("pcap"):
165             helper_uuid = self.device_helper_uuid
166
167             filename = "trace-pcap-netdev-%d.pcap" % self.guid
168             self._trace_filename["pcap"] = filename
169
170             filepath = self.simulation.trace_filepath(filename)
171
172             self.simulation.invoke(helper_uuid, "EnablePcap", filepath, 
173                     self.uuid, promiscuous = False, explicitFilename = True)
174
175         if self.trace_enabled("promiscPcap"):
176             helper_uuid = self.device_helper_uuid
177
178             filename = "trace-promisc-pcap-netdev-%d.pcap" % self.guid
179             self._trace_filename["promiscPcap"] = filename
180
181             filepath = self.simulation.trace_filepath(filename)
182
183             self.simulation.invoke(helper_uuid, "EnablePcap", filepath, 
184                     self.uuid, promiscuous = True, explicitFilename = True)
185
186         if self.trace_enabled("ascii"):
187             helper_uuid = self.device_helper_uuid
188             ascii_helper_uuid = self.ascii_helper_uuid
189
190             filename = "trace-ascii-netdev-%d.tr" % self.guid
191             self._trace_filename["ascii"] = filename
192
193             filepath = self.simulation.trace_filepath(filename)
194             stream_uuid = self.simulation.invoke(ascii_helper_uuid, 
195                     "CreateFileStream", filepath) 
196             self.simulation.invoke(helper_uuid, "EnableAscii", stream_uuid,
197                     self.uuid)
198
199     def _connect_object(self):
200         node = self.node
201         if node and node.uuid not in self.connected:
202             self.simulation.invoke(node.uuid, "AddDevice", self.uuid)
203             self._connected.add(node.uuid)
204
205         channel = self.channel
206         if channel and channel.uuid not in self.connected:
207             self.simulation.invoke(self.uuid, "Attach", channel.uuid)
208             self._connected.add(channel.uuid)
209