A Discrete-Event Network Simulator
API
tcp-pcap-nanosec-example.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  */
15 
16 // ============================================================ //
17 // BASED ON tcp-bulk-send.cc //
18 // ============================================================ //
19 
20 // Network topology
21 //
22 // n0 ----------- n1
23 // 500 Kbps
24 // 5 ms
25 //
26 // - Flow from n0 to n1 using BulkSendApplication.
27 // - Tracing of queues and packet receptions to file "tcp-pcap-nanosec-example.pcap"
28 // when tracing is turned on.
29 // - Trace file timestamps are recorded in nanoseconds, when requested
30 //
31 
32 // ============================================================ //
33 // NOTE: You can check the "magic" number of a pcap file with //
34 // the following command: //
35 // //
36 // od -N4 -tx1 filename.pcap //
37 // //
38 // ============================================================ //
39 
40 #include "ns3/applications-module.h"
41 #include "ns3/core-module.h"
42 #include "ns3/internet-module.h"
43 #include "ns3/network-module.h"
44 #include "ns3/point-to-point-module.h"
45 
46 #include <fstream>
47 #include <string>
48 
49 using namespace ns3;
50 
51 NS_LOG_COMPONENT_DEFINE("TcpPcapNanosecExample");
52 
53 int
54 main(int argc, char* argv[])
55 {
56  bool tracing = false;
57  bool nanosec = false;
58  uint32_t maxBytes = 0;
59 
60  //
61  // Allow the user to override any of the defaults at
62  // run-time, via command-line arguments
63  //
64  CommandLine cmd(__FILE__);
65  cmd.AddValue("tracing", "Flag to enable tracing", tracing);
66  cmd.AddValue("nanosec", "Flag to use nanosecond timestamps for pcap as default", nanosec);
67  cmd.AddValue("maxBytes", "Total number of bytes for application to send", maxBytes);
68  cmd.Parse(argc, argv);
69 
70  //
71  // If requested via the --nanosec cmdline flag, generate nanosecond timestamp for pcap traces
72  //
73  if (nanosec)
74  {
75  Config::SetDefault("ns3::PcapFileWrapper::NanosecMode", BooleanValue(true));
76  }
77 
78  //
79  // Explicitly create the nodes required by the topology (shown above).
80  //
81  NS_LOG_INFO("Create nodes.");
83  nodes.Create(2);
84 
85  NS_LOG_INFO("Create channels.");
86 
87  //
88  // Explicitly create the point-to-point link required by the topology (shown above).
89  //
91  pointToPoint.SetDeviceAttribute("DataRate", StringValue("500Kbps"));
92  pointToPoint.SetChannelAttribute("Delay", StringValue("5ms"));
93 
95  devices = pointToPoint.Install(nodes);
96 
97  //
98  // Install the internet stack on the nodes
99  //
101  internet.Install(nodes);
102 
103  //
104  // We've got the "hardware" in place. Now we need to add IP addresses.
105  //
106  NS_LOG_INFO("Assign IP Addresses.");
108  ipv4.SetBase("10.1.1.0", "255.255.255.0");
109  Ipv4InterfaceContainer i = ipv4.Assign(devices);
110 
111  NS_LOG_INFO("Create Applications.");
112 
113  //
114  // Create a BulkSendApplication and install it on node 0
115  //
116  uint16_t port = 9; // well-known echo port number
117 
118  BulkSendHelper source("ns3::TcpSocketFactory", InetSocketAddress(i.GetAddress(1), port));
119  // Set the amount of data to send in bytes. Zero is unlimited.
120  source.SetAttribute("MaxBytes", UintegerValue(maxBytes));
121  ApplicationContainer sourceApps = source.Install(nodes.Get(0));
122  sourceApps.Start(Seconds(0.0));
123  sourceApps.Stop(Seconds(10.0));
124 
125  //
126  // Create a PacketSinkApplication and install it on node 1
127  //
128  PacketSinkHelper sink("ns3::TcpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), port));
129  ApplicationContainer sinkApps = sink.Install(nodes.Get(1));
130  sinkApps.Start(Seconds(0.0));
131  sinkApps.Stop(Seconds(10.0));
132 
133  //
134  // Set up tracing if enabled
135  //
136  if (tracing)
137  {
138  AsciiTraceHelper ascii;
139  pointToPoint.EnablePcapAll("tcp-pcap-nanosec-example", false);
140  }
141 
142  //
143  // Now, do the actual simulation.
144  //
145  NS_LOG_INFO("Run Simulation.");
146  Simulator::Stop(Seconds(10.0));
147  Simulator::Run();
149  NS_LOG_INFO("Done.");
150 
151  Ptr<PacketSink> sink1 = DynamicCast<PacketSink>(sinkApps.Get(0));
152  std::cout << "Total Bytes Received: " << sink1->GetTotalRx() << std::endl;
153 
154  return 0;
155 }
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Stop(Time stop) const
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:174
A helper to make it easier to instantiate an ns3::BulkSendApplication on a set of nodes.
Parse command-line arguments.
Definition: command-line.h:232
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny()
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.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
uint64_t GetTotalRx() const
Definition: packet-sink.cc:96
Build a set of PointToPointNetDevice objects.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
Hold an unsigned integer type.
Definition: uinteger.h:45
uint16_t port
Definition: dsdv-manet.cc:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
NodeContainer nodes
pointToPoint
Definition: first.py:38
devices
Definition: first.py:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40
bool tracing
Flag to enable/disable generation of tracing files.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55