|
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 // Network topology |
|
20 // |
|
21 // 10Mb/s, 10ms 10Mb/s, 10ms |
|
22 // n0-----------------n1-----------------n2 |
|
23 // |
|
24 // |
|
25 // - CBR traffic for 1000 seconds |
|
26 // - Tracing of queues and packet receptions to file |
|
27 // "tcp-large-transfer.tr" |
|
28 // - pcap traces also generated in the following files |
|
29 // "tcp-large-transfer-$n-$i.pcap" where n and i represent node and interface |
|
30 // numbers respectively |
|
31 // Usage (e.g.): ./waf --run tcp-large-transfer |
|
32 |
|
33 |
|
34 #include <ctype.h> |
|
35 #include <iostream> |
|
36 #include <fstream> |
|
37 #include <string> |
|
38 #include <cassert> |
|
39 |
|
40 #include "ns3/core-module.h" |
|
41 #include "ns3/common-module.h" |
|
42 #include "ns3/helper-module.h" |
|
43 #include "ns3/node-module.h" |
|
44 #include "ns3/global-route-manager.h" |
|
45 #include "ns3/simulator-module.h" |
|
46 |
|
47 using namespace ns3; |
|
48 |
|
49 NS_LOG_COMPONENT_DEFINE ("TcpErrors"); |
|
50 |
|
51 int main (int argc, char *argv[]) |
|
52 { |
|
53 |
|
54 // Users may find it convenient to turn on explicit debugging |
|
55 // for selected modules; the below lines suggest how to do this |
|
56 // LogComponentEnable("TcpL4Protocol", LOG_LEVEL_ALL); |
|
57 // LogComponentEnable("TcpSocketImpl", LOG_LEVEL_ALL); |
|
58 // LogComponentEnable("PacketSink", LOG_LEVEL_ALL); |
|
59 // LogComponentEnable("TcpErrors", LOG_LEVEL_ALL); |
|
60 |
|
61 // |
|
62 // Make the random number generators generate reproducible results. |
|
63 // |
|
64 RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8); |
|
65 |
|
66 // Allow the user to override any of the defaults and the above |
|
67 // Bind()s at run-time, via command-line arguments |
|
68 CommandLine cmd; |
|
69 cmd.Parse (argc, argv); |
|
70 |
|
71 // Here, we will explicitly create three nodes. The first container contains |
|
72 // nodes 0 and 1 from the diagram above, and the second one contains nodes |
|
73 // 1 and 2. This reflects the channel connectivity, and will be used to |
|
74 // install the network interfaces and connect them with a channel. |
|
75 NodeContainer n0n1; |
|
76 n0n1.Create (2); |
|
77 |
|
78 NodeContainer n1n2; |
|
79 n1n2.Add (n0n1.Get (1)); |
|
80 n1n2.Create (1); |
|
81 |
|
82 // We create the channels first without any IP addressing information |
|
83 // First make and configure the helper, so that it will put the appropriate |
|
84 // parameters on the network interfaces and channels we are about to install. |
|
85 PointToPointHelper p2p; |
|
86 p2p.SetDeviceAttribute ("DataRate", DataRateValue (DataRate(10000000))); |
|
87 p2p.SetChannelAttribute ("Delay", TimeValue (MilliSeconds(10))); |
|
88 |
|
89 // And then install devices and channels connecting our topology. |
|
90 NetDeviceContainer d0d1 = p2p.Install (n0n1); |
|
91 NetDeviceContainer d1d2 = p2p.Install (n1n2); |
|
92 |
|
93 // Now add ip/tcp stack to all nodes. |
|
94 NodeContainer allNodes = NodeContainer (n0n1, n1n2.Get (1)); |
|
95 InternetStackHelper internet; |
|
96 internet.Install (allNodes); |
|
97 |
|
98 // Later, we add IP addresses. |
|
99 Ipv4AddressHelper ipv4; |
|
100 ipv4.SetBase ("10.1.3.0", "255.255.255.0"); |
|
101 Ipv4InterfaceContainer i0i1 = ipv4.Assign (d0d1); |
|
102 ipv4.SetBase ("10.1.2.0", "255.255.255.0"); |
|
103 Ipv4InterfaceContainer i1i2 = ipv4.Assign (d1d2); |
|
104 |
|
105 // and setup ip routing tables to get total ip-level connectivity. |
|
106 GlobalRouteManager::PopulateRoutingTables (); |
|
107 |
|
108 // Set up the sending CBR application |
|
109 uint16_t servPort = 50000; |
|
110 Address remoteAddress(InetSocketAddress(i1i2.GetAddress (1), servPort)); |
|
111 OnOffHelper clientHelper ("ns3::TcpSocketFactory", remoteAddress); |
|
112 clientHelper.SetAttribute |
|
113 ("OnTime", RandomVariableValue (ConstantVariable (1))); |
|
114 clientHelper.SetAttribute |
|
115 ("OffTime", RandomVariableValue (ConstantVariable (0))); |
|
116 ApplicationContainer clientApp = clientHelper.Install(n0n1.Get(0)); |
|
117 clientApp.Start (Seconds (1.0)); |
|
118 clientApp.Stop (Seconds (10.0)); |
|
119 |
|
120 // Create a packet sink to receive at n2 |
|
121 PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", |
|
122 InetSocketAddress (Ipv4Address::GetAny (), servPort)); |
|
123 ApplicationContainer sinkApp = sinkHelper.Install (n1n2.Get(1)); |
|
124 sinkApp.Start (Seconds (1.0)); |
|
125 sinkApp.Stop (Seconds (10.0)); |
|
126 |
|
127 // We're going to model a lossy channel |
|
128 RandomVariableValue u01(UniformVariable (0.0, 1.0)); |
|
129 DoubleValue rate(0.001); |
|
130 Ptr<RateErrorModel> em1 = |
|
131 CreateObject<RateErrorModel> ("RanVar", u01, "ErrorRate", rate); |
|
132 Ptr<RateErrorModel> em2 = |
|
133 CreateObject<RateErrorModel> ("RanVar", u01, "ErrorRate", rate); |
|
134 //put error models on both netdevices of the router nodes so that there is |
|
135 //loss of both data and acks |
|
136 d0d1.Get(1)->SetAttribute("ReceiveErrorModel", PointerValue (em1)); |
|
137 d1d2.Get(0)->SetAttribute("ReceiveErrorModel", PointerValue (em2)); |
|
138 |
|
139 //Ask for ASCII and pcap traces of network traffic |
|
140 std::ofstream ascii; |
|
141 ascii.open ("tcp-errors.tr"); |
|
142 PointToPointHelper::EnableAsciiAll (ascii); |
|
143 |
|
144 PointToPointHelper::EnablePcapAll ("tcp-errors"); |
|
145 |
|
146 // Finally, set up the simulator to run for 1000 seconds. |
|
147 Simulator::Stop (Seconds(1000)); |
|
148 Simulator::Run (); |
|
149 Simulator::Destroy (); |
|
150 } |