Adding trace support for ns3 RMs
[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 @clsinit_copy
28 class NS3BaseNetDevice(NS3Base):
29     _rtype = "abstract::ns3::NetDevice"
30
31     @classmethod
32     def _register_attributes(cls):
33         mac = Attribute("mac", "MAC address for device",
34                 flags = Flags.Design)
35
36         ip = Attribute("ip", "IP address for device",
37                 flags = Flags.Design)
38
39         prefix = Attribute("prefix", "Network prefix for device",
40                 flags = Flags.Design)
41
42         cls._register_attribute(mac)
43         cls._register_attribute(ip)
44         cls._register_attribute(prefix)
45
46     @classmethod
47     def _register_traces(cls):
48         pcap = Trace("pcap", "Dump traffic sniffed on the network device in Pcap format")
49         promisc_pcap = Trace("promiscPcap", "Dump traffic sniffed in promiscuous mode on the network device in Pcap format")
50         ascii = Trace("ascii", "Dump traffic sniffed on the network device in Ascii format")
51
52         cls._register_trace(pcap)
53         cls._register_trace(promisc_pcap)
54         cls._register_trace(ascii)
55
56     def __init__(self, ec, guid):
57         super(NS3BaseNetDevice, self).__init__(ec, guid)
58         self._ascii_helper_uuid = None
59         self._device_helper_uuid = None
60
61     @property
62     def node(self):
63         from nepi.resources.ns3.ns3node import NS3BaseNode
64         nodes = self.get_connected(NS3BaseNode.get_rtype())
65
66         if not nodes: 
67             msg = "Device not connected to node"
68             self.error(msg)
69             raise RuntimeError, msg
70
71         return nodes[0]
72
73     @property
74     def channel(self):
75         from nepi.resources.ns3.ns3channel import NS3BaseChannel
76         channels = self.get_connected(NS3BaseChannel.get_rtype())
77
78         if not channels: 
79             msg = "Device not connected to channel"
80             self.error(msg)
81             raise RuntimeError, msg
82
83         return channels[0]
84
85     @property
86     def ascii_helper_uuid(self):
87         if not self._ascii_helper_uuid:
88             self._ascii_helper_uuid = self.simulation.create("AsciiTraceHelper")
89         return self._ascii_helper_uuid
90
91     @property
92     def device_helper_uuid(self):
93         if not self._device_helper_uuid:
94             rtype = self.get_rtype()
95             if rtype == "ns3::PointToPointNetDevice":
96                 classname = "PointToPointHelper"
97             elif rtype == "ns3::CsmaNetDevice":
98                 classname = "CsmaHelper"
99             elif rtype == "ns3::EmuNetDevice":
100                 classname = "EmuHelper"
101             elif rtype == "ns3::FdNetDevice":
102                 classname = "FdNetDeviceHelper"
103             elif rtype in [ "ns3::BaseStationNetDevice", "SubscriberStationNetDevice" ]:
104                 classname = "WimaxHelper"
105             elif rtype == "ns3::WifiNetDevice":
106                 classname = "YansWifiPhyHelper"
107
108             self._device_helper_uuid = self.simulation.create(classname)
109
110         return self._device_helper_uuid
111
112     @property
113     def _rms_to_wait(self):
114         others = set()
115         
116         node = self.node
117         others.add(node)
118
119         ipv4 = node.ipv4
120         if node.ipv4:
121             others.add(ipv4)
122
123         others.add(self.channel)
124         return others
125
126     def _configure_object(self):
127         # Set Mac
128         mac = self.get("mac")
129         if mac:
130             mac_uuid = self.simulation.create("Mac48Address", mac)
131         else:
132             mac_uuid = self.simulation.invoke("singleton::Mac48Address", "Allocate")
133         self.simulation.invoke(self.uuid, "SetAddress", mac_uuid)
134
135         # Set IP address
136         ip = self.get("ip")
137         prefix = self.get("prefix")
138
139         i = ipaddr.IPAddress(ip)
140         if i.version == 4:
141             # IPv4
142             ipv4 = self.node.ipv4
143             ifindex_uuid = self.simulation.invoke(ipv4.uuid, "AddInterface", 
144                     self.uuid)
145             ipv4_addr_uuid = self.simulation.create("Ipv4Address", ip)
146             ipv4_mask_uuid = self.simulation.create("Ipv4Mask", "/%s" % str(prefix))
147             inaddr_uuid = self.simulation.create("Ipv4InterfaceAddress", 
148                     ipv4_addr_uuid, ipv4_mask_uuid)
149             self.simulation.invoke(ipv4.uuid, "AddAddress", ifindex_uuid, 
150                     inaddr_uuid)
151             self.simulation.invoke(ipv4.uuid, "SetMetric", ifindex_uuid, 1)
152             self.simulation.invoke(ipv4.uuid, "SetUp", ifindex_uuid)
153         else:
154             # IPv6
155             # TODO!
156             pass
157         
158         # Enable traces
159         self._configure_traces()
160
161     def _configure_traces(self):
162         if self.trace_enabled("pcap"):
163             helper_uuid = self.device_helper_uuid
164
165             filename = "trace-pcap-netdev-%d.pcap" % self.guid
166             self._trace_filename["pcap"] = filename
167
168             filepath = self.simulation.trace_filepath(filename)
169
170             self.simulation.invoke(helper_uuid, "EnablePcap", filepath, 
171                     self.uuid, promiscuous = False, explicitFilename = True)
172
173         if self.trace_enabled("promiscPcap"):
174             helper_uuid = self.device_helper_uuid
175
176             filename = "trace-promisc-pcap-netdev-%d.pcap" % self.guid
177             self._trace_filename["promiscPcap"] = filename
178
179             filepath = self.simulation.trace_filepath(filename)
180
181             self.simulation.invoke(helper_uuid, "EnablePcap", filepath, 
182                     self.uuid, promiscuous = True, explicitFilename = True)
183
184         if self.trace_enabled("ascii"):
185             helper_uuid = self.device_helper_uuid
186             ascii_helper_uuid = self.ascii_helper_uuid
187
188             filename = "trace-ascii-netdev-%d.tr" % self.guid
189             self._trace_filename["ascii"] = filename
190
191             filepath = self.simulation.trace_filepath(filename)
192             stream_uuid = self.simulation.invoke(ascii_helper_uuid, 
193                     "CreateFileStream", filepath) 
194             self.simulation.invoke(helper_uuid, "EnableAscii", stream_uuid,
195                     self.uuid)
196
197     def _connect_object(self):
198         node = self.node
199         if node and node.uuid not in self.connected:
200             self.simulation.invoke(node.uuid, "AddDevice", self.uuid)
201             self._connected.add(node.uuid)
202
203         channel = self.channel
204         if channel and channel.uuid not in self.connected:
205             self.simulation.invoke(self.uuid, "Attach", channel.uuid)
206             self._connected.add(channel.uuid)
207