author | Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
Sun, 02 Mar 2008 21:57:28 +0100 | |
changeset 2533 | 80cdc3eec057 |
parent 2502 | 50d0da37f02f |
child 2575 | 1aae382e65e2 |
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" |
2502
50d0da37f02f
introduce the ns3::String class, get rid of the string -> Attribute implicit conversion, and get rid of MakeDataRate, port PointToPointNetDevice to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2500
diff
changeset
|
41 |
#include "ns3/string.h" |
987 | 42 |
|
43 |
#include "ns3/simulator.h" |
|
44 |
#include "ns3/nstime.h" |
|
45 |
#include "ns3/data-rate.h" |
|
46 |
||
47 |
#include "ns3/ascii-trace.h" |
|
48 |
#include "ns3/pcap-trace.h" |
|
49 |
#include "ns3/internet-node.h" |
|
1272 | 50 |
#include "ns3/csma-channel.h" |
51 |
#include "ns3/csma-net-device.h" |
|
1494
c2985e1cd091
rename Euixx to Macxx
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1272
diff
changeset
|
52 |
#include "ns3/mac48-address.h" |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
53 |
#include "ns3/packet-socket-address.h" |
987 | 54 |
#include "ns3/socket.h" |
55 |
#include "ns3/onoff-application.h" |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
56 |
#include "ns3/queue.h" |
2533
80cdc3eec057
do not use Queue::CreateDefault
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2502
diff
changeset
|
57 |
#include "ns3/drop-tail-queue.h" |
987 | 58 |
|
59 |
using namespace ns3; |
|
60 |
||
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
61 |
NS_LOG_COMPONENT_DEFINE ("CsmaPacketSocketExample"); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
62 |
|
1272 | 63 |
static Ptr<CsmaNetDevice> |
64 |
CreateCsmaDevice (Ptr<Node> node, Ptr<CsmaChannel> channel) |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
65 |
{ |
2500
1fb9430f642d
convert CsmaNetDevice to Attributes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2494
diff
changeset
|
66 |
Ptr<CsmaNetDevice> device = CreateObjectWith<CsmaNetDevice> ("Node", node, |
1fb9430f642d
convert CsmaNetDevice to Attributes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2494
diff
changeset
|
67 |
"Address", Mac48Address::Allocate (), |
2502
50d0da37f02f
introduce the ns3::String class, get rid of the string -> Attribute implicit conversion, and get rid of MakeDataRate, port PointToPointNetDevice to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2500
diff
changeset
|
68 |
"EncapsulationMode", String ("Llc")); |
2470
254581fb9e9c
make NetDevice fully virtual.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2230
diff
changeset
|
69 |
node->AddDevice (device); |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
70 |
device->Attach (channel); |
2533
80cdc3eec057
do not use Queue::CreateDefault
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2502
diff
changeset
|
71 |
Ptr<Queue> queue = CreateObject<DropTailQueue> (); |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
72 |
device->AddQueue (queue); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
73 |
return device; |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
74 |
} |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
75 |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
76 |
int |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
77 |
main (int argc, char *argv[]) |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
78 |
{ |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
79 |
#if 0 |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
80 |
LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO); |
987 | 81 |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
82 |
LogComponentEnable("Object", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
83 |
LogComponentEnable("Queue", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
84 |
LogComponentEnable("DropTailQueue", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
85 |
LogComponentEnable("Channel", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
86 |
LogComponentEnable("CsmaChannel", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
87 |
LogComponentEnable("NetDevice", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
88 |
LogComponentEnable("CsmaNetDevice", 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("PacketSocket", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
91 |
LogComponentEnable("Socket", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
92 |
LogComponentEnable("UdpSocket", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
93 |
LogComponentEnable("UdpL4Protocol", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
94 |
LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
95 |
LogComponentEnable("Ipv4StaticRouting", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
96 |
LogComponentEnable("Ipv4Interface", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
97 |
LogComponentEnable("ArpIpv4Interface", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
98 |
LogComponentEnable("Ipv4LoopbackInterface", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
99 |
LogComponentEnable("OnOffApplication", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
100 |
LogComponentEnable("PacketSinkApplication", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
101 |
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
102 |
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
103 |
#endif |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
104 |
|
987 | 105 |
CommandLine::Parse (argc, argv); |
106 |
||
107 |
// Here, we will explicitly create four nodes. In more sophisticated |
|
108 |
// topologies, we could configure a node factory. |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
109 |
NS_LOG_INFO ("Create nodes."); |
2230
9f13ac3291e0
add CreateObject<> to instanciate subclasses of the Object base class. Replaces Create<>.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1504
diff
changeset
|
110 |
Ptr<Node> n0 = CreateObject<Node> (); |
9f13ac3291e0
add CreateObject<> to instanciate subclasses of the Object base class. Replaces Create<>.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1504
diff
changeset
|
111 |
Ptr<Node> n1 = CreateObject<Node> (); |
9f13ac3291e0
add CreateObject<> to instanciate subclasses of the Object base class. Replaces Create<>.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1504
diff
changeset
|
112 |
Ptr<Node> n2 = CreateObject<Node> (); |
9f13ac3291e0
add CreateObject<> to instanciate subclasses of the Object base class. Replaces Create<>.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1504
diff
changeset
|
113 |
Ptr<Node> n3 = CreateObject<Node> (); |
987 | 114 |
|
1272 | 115 |
// 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
|
116 |
NS_LOG_INFO ("Create channels."); |
2230
9f13ac3291e0
add CreateObject<> to instanciate subclasses of the Object base class. Replaces Create<>.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1504
diff
changeset
|
117 |
Ptr<CsmaChannel> channel = CreateObject<CsmaChannel> (DataRate(5000000), MilliSeconds(2)); |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
118 |
|
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
119 |
// 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
|
120 |
NS_LOG_INFO ("Build Topology."); |
1272 | 121 |
Ptr<NetDevice> n0If = CreateCsmaDevice (n0, channel); |
122 |
Ptr<NetDevice> n1If = CreateCsmaDevice (n1, channel); |
|
123 |
Ptr<NetDevice> n2If = CreateCsmaDevice (n2, channel); |
|
124 |
Ptr<NetDevice> n3If = CreateCsmaDevice (n3, channel); |
|
987 | 125 |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
126 |
// create the address which identifies n1 from n0 |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
127 |
PacketSocketAddress n0ToN1; |
1195
53f1175dbe94
fix tracing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1193
diff
changeset
|
128 |
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
|
129 |
n0ToN1.SetPhysicalAddress (n1If->GetAddress ()); // set destination address for outgoing packets |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
130 |
n0ToN1.SetProtocol (2); // set arbitrary protocol for outgoing packets |
987 | 131 |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
132 |
// create the address which identifies n0 from n3 |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
133 |
PacketSocketAddress n3ToN0; |
1195
53f1175dbe94
fix tracing
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1193
diff
changeset
|
134 |
n3ToN0.SetSingleDevice (n3If->GetIfIndex ()); |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
135 |
n3ToN0.SetPhysicalAddress (n0If->GetAddress ()); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
136 |
n3ToN0.SetProtocol (3); |
987 | 137 |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
138 |
// Create the OnOff application to send raw datagrams of size |
987 | 139 |
// 210 bytes at a rate of 448 Kb/s |
140 |
// from n0 to n1 |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
141 |
NS_LOG_INFO ("Create Applications."); |
2494
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
142 |
Ptr<OnOffApplication> ooff = |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
143 |
CreateObjectWith<OnOffApplication> ( |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
144 |
"Node", n0, |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
145 |
"Remote", Address (n0ToN1), |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
146 |
"Protocol", TypeId::LookupByName ("Packet"), |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
147 |
"OnTime", ConstantVariable(1), |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
148 |
"OffTime", ConstantVariable(0)); |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
149 |
n0->AddApplication (ooff); |
987 | 150 |
// Start the application |
151 |
ooff->Start(Seconds(1.0)); |
|
152 |
ooff->Stop (Seconds(10.0)); |
|
153 |
||
154 |
// Create a similar flow from n3 to n0, starting at time 1.1 seconds |
|
2494
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
155 |
ooff = CreateObjectWith<OnOffApplication> ( |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
156 |
"Node", n3, |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
157 |
"Remote", Address (n3ToN0), |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
158 |
"Protocol", TypeId::LookupByName ("Packet"), |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
159 |
"OnTime", ConstantVariable(1), |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
160 |
"OffTime", ConstantVariable(0)); |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2470
diff
changeset
|
161 |
n3->AddApplication (ooff); |
987 | 162 |
// Start the application |
163 |
ooff->Start(Seconds(1.1)); |
|
164 |
ooff->Stop (Seconds(10.0)); |
|
165 |
||
166 |
// Configure tracing of all enqueue, dequeue, and NetDevice receive events |
|
1272 | 167 |
// 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
|
168 |
NS_LOG_INFO ("Configure Tracing."); |
1272 | 169 |
AsciiTrace asciitrace ("csma-packet-socket.tr"); |
991 | 170 |
asciitrace.TraceAllNetDeviceRx (); |
171 |
asciitrace.TraceAllQueues (); |
|
172 |
||
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
173 |
NS_LOG_INFO ("Run Simulation."); |
987 | 174 |
Simulator::Run (); |
175 |
Simulator::Destroy (); |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
176 |
NS_LOG_INFO ("Done."); |
987 | 177 |
} |