author | Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
Tue, 06 Nov 2007 15:05:56 +0100 | |
changeset 2081 | 434742b27b1e |
parent 1504 | 36ecc970ba96 |
child 2230 | 9f13ac3291e0 |
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" |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
40 |
#include "ns3/log.h" |
987 | 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" |
|
1494
c2985e1cd091
rename Euixx to Macxx
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1272
diff
changeset
|
51 |
#include "ns3/mac48-address.h" |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
52 |
#include "ns3/packet-socket-address.h" |
987 | 53 |
#include "ns3/socket.h" |
54 |
#include "ns3/onoff-application.h" |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
55 |
#include "ns3/queue.h" |
987 | 56 |
|
57 |
using namespace ns3; |
|
58 |
||
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
59 |
NS_LOG_COMPONENT_DEFINE ("CsmaPacketSocketExample"); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
60 |
|
1272 | 61 |
static Ptr<CsmaNetDevice> |
62 |
CreateCsmaDevice (Ptr<Node> node, Ptr<CsmaChannel> channel) |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
63 |
{ |
1272 | 64 |
Ptr<CsmaNetDevice> device = Create<CsmaNetDevice> (node); |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
65 |
device->Attach (channel); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
66 |
Ptr<Queue> queue = Queue::CreateDefault (); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
67 |
device->AddQueue (queue); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
68 |
return device; |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
69 |
} |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
70 |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
71 |
int |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
72 |
main (int argc, char *argv[]) |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
73 |
{ |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
74 |
#if 0 |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
75 |
LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO); |
987 | 76 |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
77 |
LogComponentEnable("Object", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
78 |
LogComponentEnable("Queue", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
79 |
LogComponentEnable("DropTailQueue", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
80 |
LogComponentEnable("Channel", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
81 |
LogComponentEnable("CsmaChannel", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
82 |
LogComponentEnable("NetDevice", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
83 |
LogComponentEnable("CsmaNetDevice", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
84 |
LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
85 |
LogComponentEnable("PacketSocket", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
86 |
LogComponentEnable("Socket", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
87 |
LogComponentEnable("UdpSocket", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
88 |
LogComponentEnable("UdpL4Protocol", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
89 |
LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
90 |
LogComponentEnable("Ipv4StaticRouting", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
91 |
LogComponentEnable("Ipv4Interface", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
92 |
LogComponentEnable("ArpIpv4Interface", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
93 |
LogComponentEnable("Ipv4LoopbackInterface", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
94 |
LogComponentEnable("OnOffApplication", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
95 |
LogComponentEnable("PacketSinkApplication", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
96 |
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
97 |
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
98 |
#endif |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
99 |
|
987 | 100 |
CommandLine::Parse (argc, argv); |
101 |
||
102 |
// Here, we will explicitly create four nodes. In more sophisticated |
|
103 |
// topologies, we could configure a node factory. |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
104 |
NS_LOG_INFO ("Create nodes."); |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
105 |
Ptr<Node> n0 = Create<Node> (); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
106 |
Ptr<Node> n1 = Create<Node> (); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
107 |
Ptr<Node> n2 = Create<Node> (); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
108 |
Ptr<Node> n3 = Create<Node> (); |
987 | 109 |
|
1272 | 110 |
// create the shared medium used by all csma devices. |
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
111 |
NS_LOG_INFO ("Create channels."); |
1272 | 112 |
Ptr<CsmaChannel> channel = Create<CsmaChannel> (DataRate(5000000), MilliSeconds(2)); |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
113 |
|
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
114 |
// use a helper function to connect our nodes to the shared channel. |
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
115 |
NS_LOG_INFO ("Build Topology."); |
1272 | 116 |
Ptr<NetDevice> n0If = CreateCsmaDevice (n0, channel); |
117 |
Ptr<NetDevice> n1If = CreateCsmaDevice (n1, channel); |
|
118 |
Ptr<NetDevice> n2If = CreateCsmaDevice (n2, channel); |
|
119 |
Ptr<NetDevice> n3If = CreateCsmaDevice (n3, channel); |
|
987 | 120 |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
121 |
// create the address which identifies n1 from n0 |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
122 |
PacketSocketAddress n0ToN1; |
1195
53f1175dbe94
fix tracing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1193
diff
changeset
|
123 |
n0ToN1.SetSingleDevice (n0If->GetIfIndex ()); // set outgoing interface for outgoing packets |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
124 |
n0ToN1.SetPhysicalAddress (n1If->GetAddress ()); // set destination address for outgoing packets |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
125 |
n0ToN1.SetProtocol (2); // set arbitrary protocol for outgoing packets |
987 | 126 |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
127 |
// create the address which identifies n0 from n3 |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
128 |
PacketSocketAddress n3ToN0; |
1195
53f1175dbe94
fix tracing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1193
diff
changeset
|
129 |
n3ToN0.SetSingleDevice (n3If->GetIfIndex ()); |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
130 |
n3ToN0.SetPhysicalAddress (n0If->GetAddress ()); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
131 |
n3ToN0.SetProtocol (3); |
987 | 132 |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
133 |
// Create the OnOff application to send raw datagrams of size |
987 | 134 |
// 210 bytes at a rate of 448 Kb/s |
135 |
// from n0 to n1 |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
136 |
NS_LOG_INFO ("Create Applications."); |
987 | 137 |
Ptr<OnOffApplication> ooff = Create<OnOffApplication> ( |
138 |
n0, |
|
1208
ad83a13631c1
merge implicit conversion tree
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
diff
changeset
|
139 |
n0ToN1, |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
140 |
"Packet", |
987 | 141 |
ConstantVariable(1), |
142 |
ConstantVariable(0)); |
|
143 |
// Start the application |
|
144 |
ooff->Start(Seconds(1.0)); |
|
145 |
ooff->Stop (Seconds(10.0)); |
|
146 |
||
147 |
// Create a similar flow from n3 to n0, starting at time 1.1 seconds |
|
148 |
ooff = Create<OnOffApplication> ( |
|
149 |
n3, |
|
1208
ad83a13631c1
merge implicit conversion tree
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
diff
changeset
|
150 |
n3ToN0, |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
151 |
"Packet", |
987 | 152 |
ConstantVariable(1), |
153 |
ConstantVariable(0)); |
|
154 |
// Start the application |
|
155 |
ooff->Start(Seconds(1.1)); |
|
156 |
ooff->Stop (Seconds(10.0)); |
|
157 |
||
158 |
// Configure tracing of all enqueue, dequeue, and NetDevice receive events |
|
1272 | 159 |
// Trace output will be sent to the csma-packet-socket.tr file |
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
160 |
NS_LOG_INFO ("Configure Tracing."); |
1272 | 161 |
AsciiTrace asciitrace ("csma-packet-socket.tr"); |
991 | 162 |
asciitrace.TraceAllNetDeviceRx (); |
163 |
asciitrace.TraceAllQueues (); |
|
164 |
||
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
165 |
NS_LOG_INFO ("Run Simulation."); |
987 | 166 |
Simulator::Run (); |
167 |
Simulator::Destroy (); |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
168 |
NS_LOG_INFO ("Done."); |
987 | 169 |
} |