Simple cross ping experiment between linux and ns3
[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 queue(self):
87         from nepi.resources.ns3.ns3queue import NS3BaseQueue
88         queue = self.get_connected(NS3BaseQueue.get_rtype())
89
90         if not queue: 
91             msg = "Device not connected to queue"
92             self.error(msg)
93             raise RuntimeError, msg
94
95         return queue[0]
96
97     @property
98     def ascii_helper_uuid(self):
99         if not self._ascii_helper_uuid:
100             self._ascii_helper_uuid = self.simulation.create("AsciiTraceHelper")
101         return self._ascii_helper_uuid
102
103     @property
104     def device_helper_uuid(self):
105         if not self._device_helper_uuid:
106             rtype = self.get_rtype()
107             if rtype == "ns3::PointToPointNetDevice":
108                 classname = "PointToPointHelper"
109             elif rtype == "ns3::CsmaNetDevice":
110                 classname = "CsmaHelper"
111             elif rtype == "ns3::EmuNetDevice":
112                 classname = "EmuHelper"
113             elif rtype == "ns3::FdNetDevice":
114                 classname = "FdNetDeviceHelper"
115             elif rtype in [ "ns3::BaseStationNetDevice", "SubscriberStationNetDevice" ]:
116                 classname = "WimaxHelper"
117             elif rtype == "ns3::WifiNetDevice":
118                 classname = "YansWifiPhyHelper"
119             elif rtype == "ns3::FdNetDevice":
120                 classname = "FdNetDeviceHelper"
121
122             self._device_helper_uuid = self.simulation.create(classname)
123
124         return self._device_helper_uuid
125
126     @property
127     def _rms_to_wait(self):
128         rms = set()
129         
130         node = self.node
131         rms.add(node)
132
133         rms.add(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         i = ipaddr.IPAddress(ip)
160         if i.version == 4:
161             # IPv4
162             ipv4 = self.node.ipv4
163             ifindex_uuid = self.simulation.invoke(ipv4.uuid, "AddInterface", 
164                     self.uuid)
165             ipv4_addr_uuid = self.simulation.create("Ipv4Address", ip)
166             ipv4_mask_uuid = self.simulation.create("Ipv4Mask", "/%s" % str(prefix))
167             inaddr_uuid = self.simulation.create("Ipv4InterfaceAddress", 
168                     ipv4_addr_uuid, ipv4_mask_uuid)
169             self.simulation.invoke(ipv4.uuid, "AddAddress", ifindex_uuid, 
170                     inaddr_uuid)
171             self.simulation.invoke(ipv4.uuid, "SetMetric", ifindex_uuid, 1)
172             self.simulation.invoke(ipv4.uuid, "SetUp", ifindex_uuid)
173         else:
174             # IPv6
175             # TODO!
176             pass
177
178     def _configure_traces(self):
179         if self.trace_enabled("pcap"):
180             helper_uuid = self.device_helper_uuid
181
182             filename = "trace-pcap-netdev-%d.pcap" % self.guid
183             self._trace_filename["pcap"] = filename
184
185             filepath = self.simulation.trace_filepath(filename)
186
187             self.simulation.invoke(helper_uuid, "EnablePcap", filepath, 
188                     self.uuid, promiscuous = False, explicitFilename = True)
189
190         if self.trace_enabled("promiscPcap"):
191             helper_uuid = self.device_helper_uuid
192
193             filename = "trace-promisc-pcap-netdev-%d.pcap" % self.guid
194             self._trace_filename["promiscPcap"] = filename
195
196             filepath = self.simulation.trace_filepath(filename)
197
198             self.simulation.invoke(helper_uuid, "EnablePcap", filepath, 
199                     self.uuid, promiscuous = True, explicitFilename = True)
200
201         if self.trace_enabled("ascii"):
202             helper_uuid = self.device_helper_uuid
203             ascii_helper_uuid = self.ascii_helper_uuid
204
205             filename = "trace-ascii-netdev-%d.tr" % self.guid
206             self._trace_filename["ascii"] = filename
207
208             filepath = self.simulation.trace_filepath(filename)
209             stream_uuid = self.simulation.invoke(ascii_helper_uuid, 
210                     "CreateFileStream", filepath) 
211             self.simulation.invoke(helper_uuid, "EnableAscii", stream_uuid,
212                     self.uuid)
213
214     def _connect_object(self):
215         node = self.node
216         if node and node.uuid not in self.connected:
217             self.simulation.invoke(node.uuid, "AddDevice", self.uuid)
218             self._connected.add(node.uuid)
219
220         channel = self.channel
221         if channel and channel.uuid not in self.connected:
222             self.simulation.invoke(self.uuid, "Attach", channel.uuid)
223             self._connected.add(channel.uuid)
224         
225         # Verify that the device has a queue. If no queue is added a segfault 
226         # error occurs
227         queue = self.queue
228