README moves to markdown
[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 version 2 as
7 #    published by the Free Software Foundation;
8 #
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.
13 #
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/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18
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
23
24 import sys
25 PY2 = sys.version_info[0] == 2
26
27 if PY2:
28     import ipaddr
29 else:
30     import ipaddress
31
32 @clsinit_copy
33 class NS3BaseNetDevice(NS3Base):
34     _rtype = "abstract::ns3::NetDevice"
35
36     @classmethod
37     def _register_attributes(cls):
38         mac = Attribute("mac", "MAC address for device",
39                 flags = Flags.Design)
40
41         ip = Attribute("ip", "IP address for device",
42                 flags = Flags.Design)
43
44         prefix = Attribute("prefix", "Network prefix for device",
45                 flags = Flags.Design)
46
47         cls._register_attribute(mac)
48         cls._register_attribute(ip)
49         cls._register_attribute(prefix)
50
51     @classmethod
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")
56
57         cls._register_trace(pcap)
58         cls._register_trace(promisc_pcap)
59         cls._register_trace(ascii)
60
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
65
66     @property
67     def node(self):
68         from nepi.resources.ns3.ns3node import NS3BaseNode
69         nodes = self.get_connected(NS3BaseNode.get_rtype())
70
71         if not nodes: 
72             msg = "Device not connected to node"
73             self.error(msg)
74             raise RuntimeError(msg)
75
76         return nodes[0]
77
78     @property
79     def channel(self):
80         from nepi.resources.ns3.ns3channel import NS3BaseChannel
81         channels = self.get_connected(NS3BaseChannel.get_rtype())
82
83         if not channels: 
84             msg = "Device not connected to channel"
85             self.error(msg)
86             raise RuntimeError(msg)
87
88         return channels[0]
89
90     @property
91     def queue(self):
92         from nepi.resources.ns3.ns3queue import NS3BaseQueue
93         queue = self.get_connected(NS3BaseQueue.get_rtype())
94
95         if not queue: 
96             msg = "Device not connected to queue"
97             self.error(msg)
98             raise RuntimeError(msg)
99
100         return queue[0]
101
102     @property
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
107
108     @property
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"
126
127             self._device_helper_uuid = self.simulation.create(classname)
128
129         return self._device_helper_uuid
130
131     @property
132     def _rms_to_wait(self):
133         rms = set([self.node, self.channel])
134         return rms
135
136     def _configure_object(self):
137         # Set Mac
138         self._configure_mac_address()
139
140         # Set IP address
141         self._configure_ip_address()
142         
143         # Enable traces
144         self._configure_traces()
145
146     def _configure_mac_address(self):
147         mac = self.get("mac")
148         if mac:
149             mac_uuid = self.simulation.create("Mac48Address", mac)
150         else:
151             mac_uuid = self.simulation.invoke("singleton::Mac48Address", "Allocate")
152
153         self.simulation.invoke(self.uuid, "SetAddress", mac_uuid)
154
155     def _configure_ip_address(self):
156         ip = self.get("ip")
157         prefix = self.get("prefix")
158
159         if PY2:
160             i = ipaddr.IPAddress(ip)
161         else:
162             i = ipaddress.ip_address(ip)
163         if i.version == 4:
164             # IPv4
165             ipv4 = self.node.ipv4
166             ifindex_uuid = self.simulation.invoke(ipv4.uuid, "AddInterface", 
167                     self.uuid)
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, 
173                     inaddr_uuid)
174             self.simulation.invoke(ipv4.uuid, "SetMetric", ifindex_uuid, 1)
175             self.simulation.invoke(ipv4.uuid, "SetUp", ifindex_uuid)
176         else:
177             # IPv6
178             # TODO!
179             pass
180
181     def _configure_traces(self):
182         if self.trace_enabled("pcap"):
183             helper_uuid = self.device_helper_uuid
184
185             filename = "trace-pcap-netdev-%d.pcap" % self.guid
186             self._trace_filename["pcap"] = filename
187
188             filepath = self.simulation.trace_filepath(filename)
189
190             self.simulation.invoke(helper_uuid, "EnablePcap", filepath, 
191                     self.uuid, promiscuous = False, explicitFilename = True)
192
193         if self.trace_enabled("promiscPcap"):
194             helper_uuid = self.device_helper_uuid
195
196             filename = "trace-promisc-pcap-netdev-%d.pcap" % self.guid
197             self._trace_filename["promiscPcap"] = filename
198
199             filepath = self.simulation.trace_filepath(filename)
200
201             self.simulation.invoke(helper_uuid, "EnablePcap", filepath, 
202                     self.uuid, promiscuous = True, explicitFilename = True)
203
204         if self.trace_enabled("ascii"):
205             helper_uuid = self.device_helper_uuid
206             ascii_helper_uuid = self.ascii_helper_uuid
207
208             filename = "trace-ascii-netdev-%d.tr" % self.guid
209             self._trace_filename["ascii"] = filename
210
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,
215                     self.uuid)
216
217     def _connect_object(self):
218         node = self.node
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)
222
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)
227         
228         # Verify that the device has a queue. If no queue is added a segfault 
229         # error occurs
230         queue = self.queue
231