A Discrete-Event Network Simulator
API
tcp-pacing.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 // Network topology
18 //
19 // n0 ----------- n1
20 // 40 Gbps
21 // 0.01 ms
22 
23 // This programs illustrates how TCP pacing can be used and how user can set
24 // pacing rate. The program gives information about each flow like transmitted
25 // and received bytes (packets) and throughput of that flow. Currently, it is
26 // using TCP NewReno but in future after having congestion control algorithms
27 // which can change pacing rate can be used.
28 
29 #include <string>
30 #include <fstream>
31 #include "ns3/core-module.h"
32 #include "ns3/point-to-point-module.h"
33 #include "ns3/internet-module.h"
34 #include "ns3/applications-module.h"
35 #include "ns3/network-module.h"
36 #include "ns3/packet-sink.h"
37 #include "ns3/flow-monitor-module.h"
38 
39 using namespace ns3;
40 
41 NS_LOG_COMPONENT_DEFINE ("TcpPacingExample");
42 
43 int
44 main (int argc, char *argv[])
45 {
46 
47  bool tracing = false;
48  uint32_t maxBytes = 0;
49  uint32_t TCPFlows = 1;
50  bool isPacingEnabled = true;
51  std::string pacingRate = "4Gbps";
52  bool isSack = false;
53  uint32_t maxPackets = 0;
54 
56  cmd.AddValue ("tracing", "Flag to enable/disable tracing", tracing);
57  cmd.AddValue ("maxBytes",
58  "Total number of bytes for application to send", maxBytes);
59  cmd.AddValue ("maxPackets",
60  "Total number of bytes for application to send", maxPackets);
61  cmd.AddValue ("TCPFlows", "Number of application flows between sender and receiver", TCPFlows);
62  cmd.AddValue ("Pacing", "Flag to enable/disable pacing in TCP", isPacingEnabled);
63  cmd.AddValue ("Sack", "Flag to enable/disable sack in TCP", isSack);
64  cmd.AddValue ("PacingRate", "Max Pacing Rate in bps", pacingRate);
65  cmd.Parse (argc, argv);
66 
67  if (maxPackets != 0 )
68  {
69  maxBytes = 500 * maxPackets;
70  }
71 
72  Config::SetDefault ("ns3::TcpSocketState::MaxPacingRate", StringValue (pacingRate));
73  Config::SetDefault ("ns3::TcpSocketState::EnablePacing", BooleanValue (isPacingEnabled));
74  Config::SetDefault ("ns3::TcpSocketBase::Sack", BooleanValue (isSack));
75 
76  NS_LOG_INFO ("Create nodes.");
78  nodes.Create (2);
79 
80  NS_LOG_INFO ("Create channels.");
82  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("40Gbps"));
83  pointToPoint.SetChannelAttribute ("Delay", StringValue ("0.01ms"));
84 
86  devices = pointToPoint.Install (nodes);
87 
88  InternetStackHelper internet;
89  internet.Install (nodes);
90 
91  NS_LOG_INFO ("Assign IP Addresses.");
92  Ipv4AddressHelper ipv4;
93  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
95 
96  NS_LOG_INFO ("Create Applications.");
97 
98  ApplicationContainer sourceApps;
99  ApplicationContainer sinkApps;
100  for (uint32_t iterator = 0; iterator < TCPFlows; iterator++)
101  {
102  uint16_t port = 10000 + iterator;
103 
104  BulkSendHelper source ("ns3::TcpSocketFactory",
106  // Set the amount of data to send in bytes. Zero is unlimited.
107  source.SetAttribute ("MaxBytes", UintegerValue (maxBytes));
108  sourceApps.Add (source.Install (nodes.Get (0)));
109 
110  PacketSinkHelper sink ("ns3::TcpSocketFactory",
112  sinkApps.Add (sink.Install (nodes.Get (1)));
113  }
114 
115  sinkApps.Start (Seconds (0.0));
116  sinkApps.Stop (Seconds (5));
117  sourceApps.Start (Seconds (1));
118  sourceApps.Stop (Seconds (5));
119 
120  if (tracing)
121  {
122  AsciiTraceHelper ascii;
123  pointToPoint.EnableAsciiAll (ascii.CreateFileStream ("tcp-pacing.tr"));
124  pointToPoint.EnablePcapAll ("tcp-pacing", false);
125  }
126 
127  FlowMonitorHelper flowmon;
128  Ptr<FlowMonitor> monitor = flowmon.InstallAll ();
129 
130  NS_LOG_INFO ("Run Simulation.");
131  Simulator::Stop (Seconds (5));
132  Simulator::Run ();
133 
134  monitor->CheckForLostPackets ();
135  Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
136  FlowMonitor::FlowStatsContainer stats = monitor->GetFlowStats ();
137  for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
138  {
139  Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
140  if (t.sourceAddress == "10.1.1.2")
141  {
142  continue;
143  }
144  std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n";
145  std::cout << " Tx Packets: " << i->second.txPackets << "\n";
146  std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
147  std::cout << " TxOffered: " << i->second.txBytes * 8.0 / 9.0 / 1000 / 1000 << " Mbps\n";
148  std::cout << " Rx Packets: " << i->second.rxPackets << "\n";
149  std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";
150  std::cout << " Throughput: " << i->second.rxBytes * 8.0 / 9.0 / 1000 / 1000 << " Mbps\n";
151  }
152 
154  NS_LOG_INFO ("Done.");
155 }
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
an Inet address class
static Ipv4Address GetAny(void)
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
AttributeValue implementation for Boolean.
Definition: boolean.h:36
A helper to make it easier to instantiate an ns3::BulkSendApplication on a set of nodes...
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Hold variables of type string.
Definition: string.h:41
void CheckForLostPackets()
Check right now for packets that appear to be lost.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
Ipv4Address destinationAddress
Destination address.
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
const FlowStatsContainer & GetFlowStats() const
Retrieve all collected the flow statistics.
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
Build a set of PointToPointNetDevice objects.
uint16_t port
Definition: dsdv-manet.cc:45
std::map< FlowId, FlowStats > FlowStatsContainer
Container: FlowId, FlowStats.
Definition: flow-monitor.h:216
nodes
Definition: first.py:25
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Hold an unsigned integer type.
Definition: uinteger.h:44
pointToPoint
Definition: first.py:28
holds a vector of ns3::NetDevice pointers
Ptr< FlowMonitor > InstallAll()
Enable flow monitoring on all nodes.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Ptr< FlowClassifier > GetClassifier()
Retrieve the FlowClassifier object for IPv4 created by the Install* methods.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Helper to enable IP flow monitoring on a set of Nodes.
Structure to classify a packet.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
FiveTuple FindFlow(FlowId flowId) const
Searches for the FiveTuple corresponding to the given flowId.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
devices
Definition: first.py:32
Ipv4Address sourceAddress
Source address.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.