author | Craig Dowell <craigdo@ee.washington.edu> |
Sun, 12 Aug 2007 22:41:24 -0700 | |
changeset 1430 | 25fa26a6533e |
parent 1272 | 67a2ba1143e0 |
child 1433 | a6fb891b59fd |
permissions | -rw-r--r-- |
987 | 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 |
// Port of ns-2/tcl/ex/simple.tcl to ns-3 |
|
18 |
// |
|
19 |
// Network topology |
|
20 |
// |
|
21 |
// n0 n1 n2 n3 |
|
22 |
// | | | | |
|
23 |
// ===================== |
|
24 |
// |
|
25 |
// - CBR/UDP flows from n0 to n1, and from n3 to n0 |
|
26 |
// - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec. |
|
27 |
// (i.e., DataRate of 448,000 bps) |
|
28 |
// - DropTail queues |
|
1272 | 29 |
// - Tracing of queues and packet receptions to file "csma-one-subnet.tr" |
987 | 30 |
|
31 |
#include <iostream> |
|
32 |
#include <fstream> |
|
33 |
#include <string> |
|
34 |
#include <cassert> |
|
35 |
||
36 |
#include "ns3/command-line.h" |
|
37 |
#include "ns3/default-value.h" |
|
38 |
#include "ns3/ptr.h" |
|
39 |
#include "ns3/random-variable.h" |
|
40 |
#include "ns3/debug.h" |
|
41 |
||
42 |
#include "ns3/simulator.h" |
|
43 |
#include "ns3/nstime.h" |
|
44 |
#include "ns3/data-rate.h" |
|
45 |
||
46 |
#include "ns3/ascii-trace.h" |
|
47 |
#include "ns3/pcap-trace.h" |
|
48 |
#include "ns3/internet-node.h" |
|
1272 | 49 |
#include "ns3/csma-channel.h" |
50 |
#include "ns3/csma-net-device.h" |
|
51 |
#include "ns3/csma-topology.h" |
|
52 |
#include "ns3/csma-ipv4-topology.h" |
|
1167 | 53 |
#include "ns3/eui48-address.h" |
987 | 54 |
#include "ns3/ipv4-address.h" |
1171
335886fe4ddd
InetAddress -> InetSocketAddress
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1167
diff
changeset
|
55 |
#include "ns3/inet-socket-address.h" |
987 | 56 |
#include "ns3/ipv4.h" |
57 |
#include "ns3/socket.h" |
|
58 |
#include "ns3/ipv4-route.h" |
|
59 |
#include "ns3/onoff-application.h" |
|
60 |
||
61 |
using namespace ns3; |
|
62 |
||
1430
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
63 |
NS_DEBUG_COMPONENT_DEFINE ("Me"); |
987 | 64 |
|
1430
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
65 |
int |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
66 |
main (int argc, char *argv[]) |
987 | 67 |
{ |
68 |
// Users may find it convenient to turn on explicit debugging |
|
69 |
// for selected modules; the below lines suggest how to do this |
|
991 | 70 |
#if 0 |
1430
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
71 |
DebugComponentEnable("Me"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
72 |
DebugComponentEnable("Object"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
73 |
DebugComponentEnable("Queue"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
74 |
DebugComponentEnable("DropTailQueue"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
75 |
DebugComponentEnable("Channel"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
76 |
DebugComponentEnable("CsmaChannel"); |
1272 | 77 |
DebugComponentEnable("CsmaNetDevice"); |
990
44a604ad18cc
disable tracing; fix ifIndex assignment
Tom Henderson <tomh@tomh.org>
parents:
987
diff
changeset
|
78 |
DebugComponentEnable("Ipv4L3Protocol"); |
44a604ad18cc
disable tracing; fix ifIndex assignment
Tom Henderson <tomh@tomh.org>
parents:
987
diff
changeset
|
79 |
DebugComponentEnable("NetDevice"); |
1430
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
80 |
DebugComponentEnable("PacketSocket"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
81 |
DebugComponentEnable("OnOffApplication"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
82 |
DebugComponentEnable("UdpSocket"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
83 |
DebugComponentEnable("UdpL4Protocol"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
84 |
DebugComponentEnable("Ipv4L3Protocol"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
85 |
DebugComponentEnable("Ipv4StaticRouting"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
86 |
DebugComponentEnable("Ipv4Interface"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
87 |
DebugComponentEnable("ArpIpv4Interface"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
88 |
DebugComponentEnable("Ipv4LoopbackInterface"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
89 |
#endif |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
90 |
|
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
91 |
DebugComponentEnable("Me"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
92 |
DebugComponentEnable("OnOffApplication"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
93 |
DebugComponentEnable("UdpSocket"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
94 |
DebugComponentEnable("UdpL4Protocol"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
95 |
DebugComponentEnable("Ipv4L3Protocol"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
96 |
DebugComponentEnable("Ipv4StaticRouting"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
97 |
DebugComponentEnable("CsmaNetDevice"); |
1272 | 98 |
DebugComponentEnable("CsmaChannel"); |
1430
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
99 |
DebugComponentEnable("Ipv4Interface"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
100 |
DebugComponentEnable("ArpIpv4Interface"); |
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
101 |
DebugComponentEnable("Ipv4LoopbackInterface"); |
987 | 102 |
|
103 |
// Set up some default values for the simulation. Use the Bind() |
|
104 |
// technique to tell the system what subclass of Queue to use, |
|
105 |
// and what the queue limit is |
|
106 |
||
107 |
// The below Bind command tells the queue factory which class to |
|
108 |
// instantiate, when the queue factory is invoked in the topology code |
|
1224
7cbc1d661b89
rename Bind to DefaultValue::Bind. fix bug 62
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1208
diff
changeset
|
109 |
DefaultValue::Bind ("Queue", "DropTailQueue"); |
987 | 110 |
|
111 |
// Allow the user to override any of the defaults and the above |
|
112 |
// Bind()s at run-time, via command-line arguments |
|
113 |
CommandLine::Parse (argc, argv); |
|
114 |
||
115 |
// Here, we will explicitly create four nodes. In more sophisticated |
|
116 |
// topologies, we could configure a node factory. |
|
1430
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
117 |
NS_DEBUG("Create nodes."); |
987 | 118 |
Ptr<Node> n0 = Create<InternetNode> (); |
119 |
Ptr<Node> n1 = Create<InternetNode> (); |
|
120 |
Ptr<Node> n2 = Create<InternetNode> (); |
|
121 |
Ptr<Node> n3 = Create<InternetNode> (); |
|
122 |
||
123 |
// We create the channels first without any IP addressing information |
|
1430
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
124 |
NS_DEBUG("Create channels."); |
1272 | 125 |
Ptr<CsmaChannel> channel0 = |
126 |
CsmaTopology::CreateCsmaChannel( |
|
987 | 127 |
DataRate(5000000), MilliSeconds(2)); |
128 |
||
1430
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
129 |
NS_DEBUG("Build Topology."); |
1272 | 130 |
uint32_t n0ifIndex = CsmaIpv4Topology::AddIpv4CsmaNode (n0, channel0, |
1167 | 131 |
Eui48Address("10:54:23:54:23:50")); |
1272 | 132 |
uint32_t n1ifIndex = CsmaIpv4Topology::AddIpv4CsmaNode (n1, channel0, |
1167 | 133 |
Eui48Address("10:54:23:54:23:51")); |
1272 | 134 |
uint32_t n2ifIndex = CsmaIpv4Topology::AddIpv4CsmaNode (n2, channel0, |
1167 | 135 |
Eui48Address("10:54:23:54:23:52")); |
1272 | 136 |
uint32_t n3ifIndex = CsmaIpv4Topology::AddIpv4CsmaNode (n3, channel0, |
1167 | 137 |
Eui48Address("10:54:23:54:23:53")); |
987 | 138 |
|
139 |
// Later, we add IP addresses. |
|
1430
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
140 |
NS_DEBUG("Assign IP Addresses."); |
1272 | 141 |
CsmaIpv4Topology::AddIpv4Address ( |
990
44a604ad18cc
disable tracing; fix ifIndex assignment
Tom Henderson <tomh@tomh.org>
parents:
987
diff
changeset
|
142 |
n0, n0ifIndex, Ipv4Address("10.1.1.1"), Ipv4Mask("255.255.255.0")); |
987 | 143 |
|
1272 | 144 |
CsmaIpv4Topology::AddIpv4Address ( |
990
44a604ad18cc
disable tracing; fix ifIndex assignment
Tom Henderson <tomh@tomh.org>
parents:
987
diff
changeset
|
145 |
n1, n1ifIndex, Ipv4Address("10.1.1.2"), Ipv4Mask("255.255.255.0")); |
987 | 146 |
|
1272 | 147 |
CsmaIpv4Topology::AddIpv4Address ( |
990
44a604ad18cc
disable tracing; fix ifIndex assignment
Tom Henderson <tomh@tomh.org>
parents:
987
diff
changeset
|
148 |
n2, n2ifIndex, Ipv4Address("10.1.1.3"), Ipv4Mask("255.255.255.0")); |
987 | 149 |
|
1272 | 150 |
CsmaIpv4Topology::AddIpv4Address ( |
990
44a604ad18cc
disable tracing; fix ifIndex assignment
Tom Henderson <tomh@tomh.org>
parents:
987
diff
changeset
|
151 |
n3, n3ifIndex, Ipv4Address("10.1.1.4"), Ipv4Mask("255.255.255.0")); |
987 | 152 |
|
153 |
// Create the OnOff application to send UDP datagrams of size |
|
154 |
// 210 bytes at a rate of 448 Kb/s |
|
155 |
// from n0 to n1 |
|
1430
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
156 |
NS_DEBUG("Create Applications."); |
987 | 157 |
Ptr<OnOffApplication> ooff = Create<OnOffApplication> ( |
158 |
n0, |
|
1204
d40723d53e3d
InetSocketAddress: replace explicit conversion to implicit conversion
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1171
diff
changeset
|
159 |
InetSocketAddress ("10.1.1.2", 80), |
987 | 160 |
"Udp", |
161 |
ConstantVariable(1), |
|
162 |
ConstantVariable(0)); |
|
163 |
// Start the application |
|
164 |
ooff->Start(Seconds(1.0)); |
|
165 |
ooff->Stop (Seconds(10.0)); |
|
166 |
||
167 |
// Create a similar flow from n3 to n0, starting at time 1.1 seconds |
|
168 |
ooff = Create<OnOffApplication> ( |
|
169 |
n3, |
|
1204
d40723d53e3d
InetSocketAddress: replace explicit conversion to implicit conversion
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1171
diff
changeset
|
170 |
InetSocketAddress ("10.1.1.1", 80), |
987 | 171 |
"Udp", |
172 |
ConstantVariable(1), |
|
173 |
ConstantVariable(0)); |
|
174 |
// Start the application |
|
175 |
ooff->Start(Seconds(1.1)); |
|
176 |
ooff->Stop (Seconds(10.0)); |
|
177 |
||
178 |
// Configure tracing of all enqueue, dequeue, and NetDevice receive events |
|
1272 | 179 |
// Trace output will be sent to the csma-one-subnet.tr file |
1430
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
180 |
NS_DEBUG("Configure Tracing."); |
1272 | 181 |
AsciiTrace asciitrace ("csma-one-subnet.tr"); |
991 | 182 |
asciitrace.TraceAllNetDeviceRx (); |
183 |
asciitrace.TraceAllQueues (); |
|
184 |
||
185 |
// Also configure some tcpdump traces; each interface will be traced |
|
186 |
// The output files will be named |
|
187 |
// simple-point-to-point.pcap-<nodeId>-<interfaceId> |
|
188 |
// and can be read by the "tcpdump -r" command (use "-tt" option to |
|
189 |
// display timestamps correctly) |
|
1272 | 190 |
PcapTrace pcaptrace ("csma-one-subnet.pcap"); |
991 | 191 |
pcaptrace.TraceAllIp (); |
987 | 192 |
|
1430
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
193 |
NS_DEBUG("Run Simulation."); |
987 | 194 |
Simulator::Run (); |
195 |
Simulator::Destroy (); |
|
1430
25fa26a6533e
many debug prints
Craig Dowell <craigdo@ee.washington.edu>
parents:
1272
diff
changeset
|
196 |
NS_DEBUG("Done."); |
987 | 197 |
} |