A Discrete-Event Network Simulator
API
tcp-pcap-nanosec-example.cc
Go to the documentation of this file.
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 // BASED ON tcp-bulk-send.cc //
19 // ============================================================ //
20 
21 // Network topology
22 //
23 // n0 ----------- n1
24 // 500 Kbps
25 // 5 ms
26 //
27 // - Flow from n0 to n1 using BulkSendApplication.
28 // - Tracing of queues and packet receptions to file "tcp-pcap-nanosec-example.pcap"
29 // when tracing is turned on.
30 // - Trace file timestamps are recorded in nanoseconds, when requested
31 //
32 
33 
34 // ============================================================ //
35 // NOTE: You can check the "magic" number of a pcap file with //
36 // the following command: //
37 // //
38 // od -N4 -tx1 filename.pcap //
39 // //
40 // ============================================================ //
41 
42 #include <string>
43 #include <fstream>
44 #include "ns3/core-module.h"
45 #include "ns3/point-to-point-module.h"
46 #include "ns3/internet-module.h"
47 #include "ns3/applications-module.h"
48 #include "ns3/network-module.h"
49 
50 using namespace ns3;
51 
52 NS_LOG_COMPONENT_DEFINE ("TcpPcapNanosecExample");
53 
54 int
55 main (int argc, char *argv[])
56 {
57 
58  bool tracing = false;
59  bool nanosec = false;
60  uint32_t maxBytes = 0;
61 
62 //
63 // Allow the user to override any of the defaults at
64 // run-time, via command-line arguments
65 //
66  CommandLine cmd (__FILE__);
67  cmd.AddValue ("tracing", "Flag to enable tracing", tracing);
68  cmd.AddValue ("nanosec", "Flag to use nanosecond timestamps for pcap as default", nanosec);
69  cmd.AddValue ("maxBytes",
70  "Total number of bytes for application to send", maxBytes);
71  cmd.Parse (argc, argv);
72 
73 //
74 // If requested via the --nanosec cmdline flag, generate nanosecond timestamp for pcap traces
75 //
76  if (nanosec)
77  {
78  Config::SetDefault ("ns3::PcapFileWrapper::NanosecMode", BooleanValue (true));
79  }
80 
81 //
82 // Explicitly create the nodes required by the topology (shown above).
83 //
84  NS_LOG_INFO ("Create nodes.");
86  nodes.Create (2);
87 
88  NS_LOG_INFO ("Create channels.");
89 
90 //
91 // Explicitly create the point-to-point link required by the topology (shown above).
92 //
94  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("500Kbps"));
95  pointToPoint.SetChannelAttribute ("Delay", StringValue ("5ms"));
96 
98  devices = pointToPoint.Install (nodes);
99 
100 //
101 // Install the internet stack on the nodes
102 //
103  InternetStackHelper internet;
104  internet.Install (nodes);
105 
106 //
107 // We've got the "hardware" in place. Now we need to add IP addresses.
108 //
109  NS_LOG_INFO ("Assign IP Addresses.");
110  Ipv4AddressHelper ipv4;
111  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
113 
114  NS_LOG_INFO ("Create Applications.");
115 
116 //
117 // Create a BulkSendApplication and install it on node 0
118 //
119  uint16_t port = 9; // well-known echo port number
120 
121 
122  BulkSendHelper source ("ns3::TcpSocketFactory",
124  // Set the amount of data to send in bytes. Zero is unlimited.
125  source.SetAttribute ("MaxBytes", UintegerValue (maxBytes));
126  ApplicationContainer sourceApps = source.Install (nodes.Get (0));
127  sourceApps.Start (Seconds (0.0));
128  sourceApps.Stop (Seconds (10.0));
129 
130 //
131 // Create a PacketSinkApplication and install it on node 1
132 //
133  PacketSinkHelper sink ("ns3::TcpSocketFactory",
135  ApplicationContainer sinkApps = sink.Install (nodes.Get (1));
136  sinkApps.Start (Seconds (0.0));
137  sinkApps.Stop (Seconds (10.0));
138 
139 //
140 // Set up tracing if enabled
141 //
142  if (tracing)
143  {
144  AsciiTraceHelper ascii;
145  pointToPoint.EnablePcapAll ("tcp-pcap-nanosec-example", false);
146  }
147 
148 //
149 // Now, do the actual simulation.
150 //
151  NS_LOG_INFO ("Run Simulation.");
152  Simulator::Stop (Seconds (10.0));
153  Simulator::Run ();
155  NS_LOG_INFO ("Done.");
156 
157  Ptr<PacketSink> sink1 = DynamicCast<PacketSink> (sinkApps.Get (0));
158  std::cout << "Total Bytes Received: " << sink1->GetTotalRx () << std::endl;
159 }
holds a vector of ns3::Application pointers.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
AttributeValue implementation for Boolean.
Definition: boolean.h:37
A helper to make it easier to instantiate an ns3::BulkSendApplication on a set of nodes.
Parse command-line arguments.
Definition: command-line.h:229
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
static Ipv4Address GetAny(void)
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
uint64_t GetTotalRx() const
Definition: packet-sink.cc:93
Build a set of PointToPointNetDevice objects.
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
Hold variables of type string.
Definition: string.h:41
Hold an unsigned integer type.
Definition: uinteger.h:44
uint16_t port
Definition: dsdv-manet.cc:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
pointToPoint
Definition: first.py:35
devices
Definition: first.py:39
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:35
bool tracing
Flag to enable/disable generation of tracing files.
Definition: wifi-bianchi.cc:88
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56