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
|
|
29 |
// - Tracing of queues and packet receptions to file "csma-cd-one-subnet.tr"
|
|
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"
|
|
49 |
#include "ns3/csma-cd-channel.h"
|
|
50 |
#include "ns3/csma-cd-net-device.h"
|
|
51 |
#include "ns3/csma-cd-topology.h"
|
|
52 |
#include "ns3/csma-cd-ipv4-topology.h"
|
|
53 |
#include "ns3/mac-address.h"
|
|
54 |
#include "ns3/ipv4-address.h"
|
|
55 |
#include "ns3/ipv4.h"
|
|
56 |
#include "ns3/socket.h"
|
|
57 |
#include "ns3/ipv4-route.h"
|
|
58 |
#include "ns3/onoff-application.h"
|
|
59 |
|
|
60 |
#include "ns3/ascii-trace.h"
|
|
61 |
|
|
62 |
#include "ns3/trace-context.h"
|
|
63 |
#include "ns3/trace-root.h"
|
|
64 |
|
|
65 |
|
|
66 |
using namespace ns3;
|
|
67 |
|
|
68 |
|
|
69 |
int main (int argc, char *argv[])
|
|
70 |
{
|
|
71 |
|
|
72 |
// Users may find it convenient to turn on explicit debugging
|
|
73 |
// for selected modules; the below lines suggest how to do this
|
|
74 |
#if 0
|
|
75 |
DebugComponentEnable("Channel");
|
|
76 |
DebugComponentEnable("CsmaCdChannel");
|
|
77 |
DebugComponentEnable("CsmaCdNetDevice");
|
|
78 |
DebugComponentEnable("NetDevice");
|
|
79 |
DebugComponentEnable("PacketSocket");
|
|
80 |
#endif
|
|
81 |
|
|
82 |
// Set up some default values for the simulation. Use the Bind()
|
|
83 |
// technique to tell the system what subclass of Queue to use,
|
|
84 |
// and what the queue limit is
|
|
85 |
|
|
86 |
// The below Bind command tells the queue factory which class to
|
|
87 |
// instantiate, when the queue factory is invoked in the topology code
|
|
88 |
Bind ("Queue", "DropTailQueue");
|
|
89 |
|
|
90 |
// Allow the user to override any of the defaults and the above
|
|
91 |
// Bind()s at run-time, via command-line arguments
|
|
92 |
CommandLine::Parse (argc, argv);
|
|
93 |
|
|
94 |
// Here, we will explicitly create four nodes. In more sophisticated
|
|
95 |
// topologies, we could configure a node factory.
|
|
96 |
Ptr<Node> n0 = Create<InternetNode> ();
|
|
97 |
Ptr<Node> n1 = Create<InternetNode> ();
|
|
98 |
Ptr<Node> n2 = Create<InternetNode> ();
|
|
99 |
Ptr<Node> n3 = Create<InternetNode> ();
|
|
100 |
|
|
101 |
// We create the channels first without any IP addressing information
|
|
102 |
Ptr<CsmaCdChannel> channel0 =
|
|
103 |
CsmaCdTopology::CreateCsmaCdChannel(
|
|
104 |
DataRate(5000000), MilliSeconds(2));
|
|
105 |
|
|
106 |
CsmaCdIpv4Topology::AddIpv4CsmaCdNode (n0, channel0,
|
|
107 |
MacAddress("10:54:23:54:23:50"));
|
|
108 |
CsmaCdIpv4Topology::AddIpv4CsmaCdNode (n1, channel0,
|
|
109 |
MacAddress("10:54:23:54:23:51"));
|
|
110 |
CsmaCdIpv4Topology::AddIpv4CsmaCdNode (n2, channel0,
|
|
111 |
MacAddress("10:54:23:54:23:52"));
|
|
112 |
CsmaCdIpv4Topology::AddIpv4CsmaCdNode (n3, channel0,
|
|
113 |
MacAddress("10:54:23:54:23:53"));
|
|
114 |
|
|
115 |
// Later, we add IP addresses.
|
|
116 |
CsmaCdIpv4Topology::AddIpv4Address (
|
|
117 |
n0, 1, Ipv4Address("10.1.1.1"), Ipv4Mask("255.255.255.0"));
|
|
118 |
|
|
119 |
CsmaCdIpv4Topology::AddIpv4Address (
|
|
120 |
n1, 1, Ipv4Address("10.1.1.2"), Ipv4Mask("255.255.255.0"));
|
|
121 |
|
|
122 |
CsmaCdIpv4Topology::AddIpv4Address (
|
|
123 |
n2, 1, Ipv4Address("10.1.1.3"), Ipv4Mask("255.255.255.0"));
|
|
124 |
|
|
125 |
CsmaCdIpv4Topology::AddIpv4Address (
|
|
126 |
n3, 1, Ipv4Address("10.1.1.4"), Ipv4Mask("255.255.255.0"));
|
|
127 |
|
|
128 |
|
|
129 |
// Create the OnOff application to send UDP datagrams of size
|
|
130 |
// 210 bytes at a rate of 448 Kb/s
|
|
131 |
// from n0 to n1
|
|
132 |
Ptr<OnOffApplication> ooff = Create<OnOffApplication> (
|
|
133 |
n0,
|
|
134 |
Ipv4Address("10.1.1.2"),
|
|
135 |
80,
|
|
136 |
"Udp",
|
|
137 |
ConstantVariable(1),
|
|
138 |
ConstantVariable(0));
|
|
139 |
// Start the application
|
|
140 |
ooff->Start(Seconds(1.0));
|
|
141 |
ooff->Stop (Seconds(10.0));
|
|
142 |
|
|
143 |
// Create a similar flow from n3 to n0, starting at time 1.1 seconds
|
|
144 |
ooff = Create<OnOffApplication> (
|
|
145 |
n3,
|
|
146 |
Ipv4Address("10.1.1.1"),
|
|
147 |
80,
|
|
148 |
"Udp",
|
|
149 |
ConstantVariable(1),
|
|
150 |
ConstantVariable(0));
|
|
151 |
// Start the application
|
|
152 |
ooff->Start(Seconds(1.1));
|
|
153 |
ooff->Stop (Seconds(10.0));
|
|
154 |
|
|
155 |
// Configure tracing of all enqueue, dequeue, and NetDevice receive events
|
|
156 |
// Trace output will be sent to the csma-cd-one-subnet.tr file
|
|
157 |
AsciiTrace asciitrace ("csma-cd-one-subnet.tr");
|
|
158 |
asciitrace.TraceAllNetDeviceRx ();
|
|
159 |
// asciitrace.TraceAllQueues ();
|
|
160 |
|
|
161 |
Simulator::Run ();
|
|
162 |
|
|
163 |
Simulator::Destroy ();
|
|
164 |
}
|