|
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /* |
|
3 * This program is free software; you can redistribute it and/or modify |
|
4 * it under the terms of the GNU General Public License version 2 as |
|
5 * published by the Free Software Foundation; |
|
6 * |
|
7 * This program is distributed in the hope that it will be useful, |
|
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
10 * GNU General Public License for more details. |
|
11 * |
|
12 * You should have received a copy of the GNU General Public License |
|
13 * along with this program; if not, write to the Free Software |
|
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
15 * |
|
16 */ |
|
17 |
|
18 // |
|
19 // This ns-3 example demonstrates the use of helper functions to ease |
|
20 // the construction of simulation scenarios. |
|
21 // |
|
22 // The simulation topology consists of a mixed wired and wireless |
|
23 // scenario in which a hierarchical mobility model is used. |
|
24 // |
|
25 // The simulation layout consists of N backbone routers interconnected |
|
26 // by an ad hoc wifi network. |
|
27 // Each backbone router also has a local 802.11 network and is connected |
|
28 // to a local LAN. An additional set of (K-1) nodes are connected to |
|
29 // this backbone. Finally, a local LAN is connected to each router |
|
30 // on the backbone, with L-1 additional hosts. |
|
31 // |
|
32 // The nodes are populated with TCP/IP stacks, and OLSR unicast routing |
|
33 // on the backbone. An example UDP transfer is shown. The simulator |
|
34 // be configured to output tcpdumps or traces from different nodes. |
|
35 // |
|
36 // |
|
37 // +--------------------------------------------------------+ |
|
38 // | | |
|
39 // | 802.11 ad hoc, ns-2 mobility | |
|
40 // | | |
|
41 // +--------------------------------------------------------+ |
|
42 // | o o o (N backbone routers) | |
|
43 // +--------+ +--------+ |
|
44 // wired LAN | mobile | wired LAN | mobile | |
|
45 // -----------| router | -----------| router | |
|
46 // --------- --------- |
|
47 // | | |
|
48 // +----------------+ +----------------+ |
|
49 // | 802.11 | | 802.11 | |
|
50 // | net | | net | |
|
51 // | K-1 hosts | | K-1 hosts | |
|
52 // +----------------+ +----------------+ |
|
53 // |
|
54 |
|
55 #include <fstream> |
|
56 #include "ns3/core-module.h" |
|
57 #include "ns3/common-module.h" |
|
58 #include "ns3/node-module.h" |
|
59 #include "ns3/helper-module.h" |
|
60 #include "ns3/mobility-module.h" |
|
61 #include "ns3/contrib-module.h" |
|
62 #include "ns3/wifi-module.h" |
|
63 |
|
64 using namespace ns3; |
|
65 |
|
66 // |
|
67 // Define logging keyword for this file |
|
68 // |
|
69 NS_LOG_COMPONENT_DEFINE ("MixedWireless"); |
|
70 |
|
71 // |
|
72 // This function will be used below as a trace sink |
|
73 // |
|
74 #ifdef ENABLE_FOR_TRACING_EXAMPLE |
|
75 static void |
|
76 CourseChangeCallback (std::string path, Ptr<const MobilityModel> model) |
|
77 { |
|
78 Vector position = model->GetPosition (); |
|
79 std::cout << "CourseChange " << path << " x=" << position.x << ", y=" << position.y << ", z=" << position.z << std::endl; |
|
80 } |
|
81 #endif |
|
82 |
|
83 int |
|
84 main (int argc, char *argv[]) |
|
85 { |
|
86 // |
|
87 // First, we declare and initialize a few local variables that control some |
|
88 // simulation parameters. |
|
89 // |
|
90 uint32_t backboneNodes = 10; |
|
91 uint32_t infraNodes = 5; |
|
92 uint32_t lanNodes = 5; |
|
93 uint32_t stopTime = 10; |
|
94 |
|
95 // |
|
96 // Simulation defaults are typically set next, before command line |
|
97 // arguments are parsed. |
|
98 // |
|
99 Config::SetDefault ("ns3::OnOffApplication::PacketSize", String ("210")); |
|
100 Config::SetDefault ("ns3::OnOffApplication::DataRate", String ("448kb/s")); |
|
101 |
|
102 // |
|
103 // For convenience, we add the local variables to the command line argument |
|
104 // system so that they can be overridden with flags such as |
|
105 // "--backboneNodes=20" |
|
106 // |
|
107 CommandLine cmd; |
|
108 cmd.AddValue("backboneNodes", "number of backbone nodes", backboneNodes); |
|
109 cmd.AddValue ("infraNodes", "number of leaf nodes", infraNodes); |
|
110 cmd.AddValue("lanNodes", "number of LAN nodes", lanNodes); |
|
111 cmd.AddValue("stopTime", "simulation stop time (seconds)", stopTime); |
|
112 |
|
113 // |
|
114 // The system global variables and the local values added to the argument |
|
115 // system can be overridden by command line arguments by using this call. |
|
116 // |
|
117 cmd.Parse (argc, argv); |
|
118 |
|
119 // The metadata system (off by default) is used by ascii tracing below |
|
120 Packet::EnableMetadata (); |
|
121 |
|
122 /////////////////////////////////////////////////////////////////////////// |
|
123 // // |
|
124 // Construct the backbone // |
|
125 // // |
|
126 /////////////////////////////////////////////////////////////////////////// |
|
127 |
|
128 // |
|
129 // Create a container to manage the nodes of the adhoc (backbone) network. |
|
130 // Later we'll create the rest of the nodes we'll need. |
|
131 // |
|
132 NodeContainer backbone; |
|
133 backbone.Create (backboneNodes); |
|
134 // |
|
135 // Create the backbone wifi net devices and install them into the nodes in |
|
136 // our container |
|
137 // |
|
138 WifiHelper wifi; |
|
139 wifi.SetMac ("ns3::AdhocWifiMac"); |
|
140 wifi.SetPhy ("ns3::WifiPhy"); |
|
141 NetDeviceContainer backboneDevices = wifi.Install (backbone); |
|
142 // |
|
143 // Add the IPv4 protocol stack to the nodes in our container |
|
144 // |
|
145 InternetStackHelper internet; |
|
146 internet.Install (backbone); |
|
147 // |
|
148 // Assign IPv4 addresses to the device drivers (actually to the associated |
|
149 // IPv4 interfaces) we just created. |
|
150 // |
|
151 Ipv4AddressHelper ipAddrs; |
|
152 ipAddrs.SetBase ("192.168.0.0", "255.255.255.0"); |
|
153 ipAddrs.Assign (backboneDevices); |
|
154 |
|
155 // |
|
156 // The ad-hoc network nodes need a mobility model so we aggregate one to |
|
157 // each of the nodes we just finished building. |
|
158 // |
|
159 MobilityHelper mobility; |
|
160 Ptr<ListPositionAllocator> positionAlloc = |
|
161 CreateObject<ListPositionAllocator> (); |
|
162 positionAlloc->Add (Vector (0.0, 0.0, 0.0)); |
|
163 positionAlloc->Add (Vector (5.0, 0.0, 0.0)); |
|
164 mobility.SetPositionAllocator (positionAlloc); |
|
165 mobility.SetMobilityModel ("ns3::RandomDirection2dMobilityModel", |
|
166 "Bounds", Rectangle (0, 1000, 0, 1000), |
|
167 "Speed", ConstantVariable (2000), |
|
168 "Pause", ConstantVariable (0.2)); |
|
169 mobility.Layout (backbone); |
|
170 |
|
171 /////////////////////////////////////////////////////////////////////////// |
|
172 // // |
|
173 // Construct the LANs // |
|
174 // // |
|
175 /////////////////////////////////////////////////////////////////////////// |
|
176 |
|
177 // Reset the address base-- all of the CSMA networks will be in |
|
178 // the "172.16 address space |
|
179 ipAddrs.SetBase ("172.16.0.0", "255.255.255.0"); |
|
180 |
|
181 for (uint32_t i = 0; i < backboneNodes; ++i) |
|
182 { |
|
183 NS_LOG_INFO ("Configuring local area network for backbone node " << i); |
|
184 // |
|
185 // Create a container to manage the nodes of the LAN. Pick one of |
|
186 // the backbone nodes to be part of the LAN and first add it to |
|
187 // the container. Then create the rest of the nodes we'll need. |
|
188 // |
|
189 NodeContainer lan; |
|
190 lan.Add (backbone.Get (i)); |
|
191 lan.Create (lanNodes - 1); |
|
192 // |
|
193 // Create the CSMA net devices and install them into the nodes in our |
|
194 // collection. |
|
195 // |
|
196 CsmaHelper csma; |
|
197 csma.SetChannelParameter ("BitRate", DataRate (5000000)); |
|
198 csma.SetChannelParameter ("Delay", MilliSeconds (2)); |
|
199 NetDeviceContainer lanDevices = csma.Install (lan); |
|
200 // |
|
201 // Add the IPv4 protocol stack to the nodes in our container |
|
202 // |
|
203 internet.Install (lan); |
|
204 // |
|
205 // Assign IPv4 addresses to the device drivers (actually to the |
|
206 // associated IPv4 interfaces) we just created. |
|
207 // |
|
208 ipAddrs.Assign (lanDevices); |
|
209 // |
|
210 // Assign a new network prefix for the next LAN, according to the |
|
211 // network mask initialized above |
|
212 // |
|
213 ipAddrs.NewNetwork (); |
|
214 } |
|
215 |
|
216 /////////////////////////////////////////////////////////////////////////// |
|
217 // // |
|
218 // Construct the mobile networks // |
|
219 // // |
|
220 /////////////////////////////////////////////////////////////////////////// |
|
221 |
|
222 // Reset the address base-- all of the 802.11 networks will be in |
|
223 // the "10.0" address space |
|
224 ipAddrs.SetBase ("10.0.0.0", "255.255.255.0"); |
|
225 |
|
226 for (uint32_t i = 0; i < backboneNodes; ++i) |
|
227 { |
|
228 NS_LOG_INFO ("Configuring wireless network for backbone node " << i); |
|
229 // |
|
230 // Create a container to manage the nodes of the network. Pick one of |
|
231 // the backbone nodes to be part of the network and first add it to |
|
232 // the container. Then create the rest of the nodes we'll need. |
|
233 // |
|
234 NodeContainer infra; |
|
235 infra.Add (backbone.Get (i)); |
|
236 infra.Create (infraNodes - 1); |
|
237 // |
|
238 // Create another ad hoc network and devices |
|
239 // |
|
240 WifiHelper wifiInfra; |
|
241 wifiInfra.SetMac ("ns3::AdhocWifiMac"); |
|
242 wifiInfra.SetPhy ("ns3::WifiPhy"); |
|
243 NetDeviceContainer infraDevices = wifiInfra.Install (infra); |
|
244 |
|
245 // Add the IPv4 protocol stack to the nodes in our container |
|
246 // |
|
247 internet.Install (infra); |
|
248 // |
|
249 // Assign IPv4 addresses to the device drivers (actually to the associated |
|
250 // IPv4 interfaces) we just created. |
|
251 // |
|
252 ipAddrs.Assign (infraDevices); |
|
253 // |
|
254 // Assign a new network prefix for each mobile network, according to |
|
255 // the network mask initialized above |
|
256 // |
|
257 ipAddrs.NewNetwork (); |
|
258 // |
|
259 // The new wireless nodes need a mobility model so we aggregate one |
|
260 // to each of the nodes we just finished building. |
|
261 // |
|
262 Ptr<ListPositionAllocator> subnetAlloc = |
|
263 CreateObject<ListPositionAllocator> (); |
|
264 for (uint32_t j = 0; j < infra.GetN (); ++j) |
|
265 { |
|
266 subnetAlloc->Add (Vector (0.0, j, 0.0)); |
|
267 } |
|
268 mobility.EnableNotifier (); |
|
269 mobility.PushReferenceMobilityModel (backbone.Get (i)); |
|
270 mobility.SetPositionAllocator (subnetAlloc); |
|
271 mobility.SetMobilityModel ("ns3::RandomDirection2dMobilityModel", |
|
272 "Bounds", Rectangle (-25, 25, -25, 25), |
|
273 "Speed", ConstantVariable (30), |
|
274 "Pause", ConstantVariable (0.4)); |
|
275 mobility.Layout (infra); |
|
276 } |
|
277 /////////////////////////////////////////////////////////////////////////// |
|
278 // // |
|
279 // Routing configuration // |
|
280 // // |
|
281 /////////////////////////////////////////////////////////////////////////// |
|
282 |
|
283 NS_LOG_INFO ("Enabling OLSR routing on all backbone nodes"); |
|
284 OlsrHelper olsr; |
|
285 olsr.Enable (backbone); |
|
286 |
|
287 /////////////////////////////////////////////////////////////////////////// |
|
288 // // |
|
289 // Application configuration // |
|
290 // // |
|
291 /////////////////////////////////////////////////////////////////////////// |
|
292 |
|
293 // Create the OnOff application to send UDP datagrams of size |
|
294 // 210 bytes at a rate of 448 Kb/s, between two nodes |
|
295 NS_LOG_INFO ("Create Applications."); |
|
296 uint16_t port = 9; // Discard port (RFC 863) |
|
297 |
|
298 // Let's make sure that the user does not define too few LAN nodes |
|
299 // to make this example work. We need lanNodes >= 5 |
|
300 NS_ASSERT (lanNodes >= 5); |
|
301 Ptr<Node> appSource = NodeList::GetNode (11); |
|
302 Ptr<Node> appSink = NodeList::GetNode (13); |
|
303 Ipv4Address remoteAddr = Ipv4Address ("172.16.0.5"); |
|
304 |
|
305 OnOffHelper onoff ("ns3::Udp", |
|
306 Address (InetSocketAddress (remoteAddr, port))); |
|
307 onoff.SetAttribute ("OnTime", ConstantVariable (1)); |
|
308 onoff.SetAttribute ("OffTime", ConstantVariable (0)); |
|
309 ApplicationContainer apps = onoff.Install (appSource); |
|
310 apps.Start (Seconds (3.0)); |
|
311 apps.Stop (Seconds (20.0)); |
|
312 |
|
313 // Create a packet sink to receive these packets |
|
314 PacketSinkHelper sink ("ns3::Udp", |
|
315 InetSocketAddress (Ipv4Address::GetAny (), port)); |
|
316 apps = sink.Install (appSink); |
|
317 apps.Start (Seconds (3.0)); |
|
318 |
|
319 /////////////////////////////////////////////////////////////////////////// |
|
320 // // |
|
321 // Tracing configuration // |
|
322 // // |
|
323 /////////////////////////////////////////////////////////////////////////// |
|
324 |
|
325 NS_LOG_INFO ("Configure Tracing."); |
|
326 // |
|
327 // Let's set up some ns-2-like ascii traces, using another helper class |
|
328 // |
|
329 // Look at nodes 11, 13 only |
|
330 // XXX todo |
|
331 // asciiTrace.TraceQueues ("/NodeList/11|13/DeviceList/0"); |
|
332 // asciiTrace.TraceNetDeviceRx ("/NodeList/11|13/DeviceList/0"); |
|
333 std::ofstream ascii; |
|
334 ascii.open ("mixed-wireless.tr"); |
|
335 WifiHelper::EnableAscii (ascii); |
|
336 CsmaHelper::EnableAscii (ascii); |
|
337 |
|
338 // Let's do a pcap trace on the backbone devices |
|
339 WifiHelper::EnablePcap ("mixed-wireless.pcap", backboneDevices); |
|
340 // Let's additionally trace the application Sink, ifIndex 0 |
|
341 CsmaHelper::EnablePcap ("mixed-wireless.pcap", appSink->GetId (), 0); |
|
342 |
|
343 #ifdef ENABLE_FOR_TRACING_EXAMPLE |
|
344 Config::Connect ("/NodeList/*/$MobilityModelNotifier/CourseChange", |
|
345 MakeCallback (&CourseChangeCallback)); |
|
346 #endif |
|
347 |
|
348 /////////////////////////////////////////////////////////////////////////// |
|
349 // // |
|
350 // Run simulation // |
|
351 // // |
|
352 /////////////////////////////////////////////////////////////////////////// |
|
353 |
|
354 NS_LOG_INFO ("Run Simulation."); |
|
355 Simulator::StopAt (Seconds (stopTime)); |
|
356 Simulator::Run (); |
|
357 Simulator::Destroy (); |
|
358 } |