A Discrete-Event Network Simulator
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
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  */
18 //
19 // Network topology
20 //
21 // 6Mb/s, 500ms
22 // n0-----------------n1
23 //
24 // - a 'lossy' network with long delay
25 // - TCP flow from n0 to n1 and from n1 to n0
26 // - pcap traces generated as tcp-nsc-lfn-0-0.pcap and tcp-nsc-lfn-1-0.pcap
27 // Usage (e.g.): ./waf --run 'tcp-nsc-lfn --TCP_CONGESTION=hybla --runtime=30'
29 #include <iostream>
30 #include <fstream>
31 #include <string>
33 #include "ns3/core-module.h"
34 #include "ns3/network-module.h"
35 #include "ns3/internet-module.h"
36 #include "ns3/point-to-point-module.h"
37 #include "ns3/applications-module.h"
38 #include "ns3/ipv4-global-routing-helper.h"
40 using namespace ns3;
44 int main (int argc, char *argv[])
45 {
47  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (4096));
48  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("6Mbps"));
50  // cubic is the default congestion algorithm in Linux 2.6.26
51  std::string tcpCong = "cubic";
52  // this is the default error rate of our link, that is, the the probability of a single
53  // byte being 'corrupted' during transfer.
54  double errRate = 0.000001;
55  // how long the sender should be running, in seconds.
56  unsigned int runtime = 120;
57  // the name of the NSC stack library that should be used
58  std::string nscStack = "liblinux2.6.26.so";
61  // Here, we define additional command line options.
62  // This allows a user to override the defaults set above from the command line.
63  cmd.AddValue ("TCP_CONGESTION", "Linux 2.6.26 Tcp Congestion control algorithm to use", tcpCong);
64  cmd.AddValue ("error-rate", "Error rate to apply to link", errRate);
65  cmd.AddValue ("runtime", "How long the applications should send data (default 120 seconds)", runtime);
66  cmd.AddValue ("nscstack", "Set name of NSC stack (shared library) to use (default liblinux2.6.26.so)", nscStack);
67  cmd.Parse (argc, argv);
69  NodeContainer n;
70  n.Create (2);
73  // create point-to-point link with a bandwidth of 6MBit/s and a large delay (0.5 seconds)
74  p2p.SetDeviceAttribute ("DataRate", DataRateValue (DataRate (6 * 1000 * 1000)));
75  p2p.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (500)));
78  // The default MTU of the p2p link would be 65535, which doesn't work
79  // well with our default errRate (most packets would arrive corrupted).
80  p2pInterfaces.Get (0)->SetMtu (1500);
81  p2pInterfaces.Get (1)->SetMtu (1500);
83  InternetStackHelper internet;
84  // The next statement switches the nodes to 'NSC'-Mode.
85  // It disables the native ns-3 TCP model and loads the NSC library.
86  internet.SetTcp ("ns3::NscTcpL4Protocol","Library",StringValue (nscStack));
87  internet.Install (n);
89  if (tcpCong != "cubic") // make sure we only fail if both --nscstack and --TCP_CONGESTION are used
90  {
91  // This uses ns-3s attribute system to set the 'net.ipv4.tcp_congestion_control' sysctl of the
92  // stack.
93  // The same mechanism could be used to e.g. disable TCP timestamps:
94  // Config::Set ("/NodeList/*/$ns3::Ns3NscStack<linux2.6.26>/net.ipv4.tcp_timestamps", StringValue ("0"));
95  Config::Set ("/NodeList/*/$ns3::Ns3NscStack<linux2.6.26>/net.ipv4.tcp_congestion_control", StringValue (tcpCong));
96  }
97  Ipv4AddressHelper ipv4;
98  ipv4.SetBase ("", "");
99  Ipv4InterfaceContainer ipv4Interfaces = ipv4.Assign (p2pInterfaces);
101  DoubleValue rate (errRate);
102  Ptr<RateErrorModel> em1 =
103  CreateObjectWithAttributes<RateErrorModel> ("RanVar", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"), "ErrorRate", rate);
104  Ptr<RateErrorModel> em2 =
105  CreateObjectWithAttributes<RateErrorModel> ("RanVar", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"), "ErrorRate", rate);
107  // This enables the specified errRate on both link endpoints.
108  p2pInterfaces.Get (0)->SetAttribute ("ReceiveErrorModel", PointerValue (em1));
109  p2pInterfaces.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (em2));
113  uint16_t servPort = 8080;
114  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), servPort));
115  ApplicationContainer sinkApp = sinkHelper.Install (n);
116  sinkApp.Start (Seconds (0.0));
117  // this makes sure that the receiver will run one minute longer than the sender applicaton.
118  sinkApp.Stop (Seconds (runtime + 60.0));
120  // This sets up two TCP flows, one from A -> B, one from B -> A.
121  for (int i = 0, j = 1; i < 2; j--, i++)
122  {
123  Address remoteAddress (InetSocketAddress (ipv4Interfaces.GetAddress (i), servPort));
124  OnOffHelper clientHelper ("ns3::TcpSocketFactory", remoteAddress);
125  clientHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
126  clientHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
127  ApplicationContainer clientApp = clientHelper.Install (n.Get (j));
128  clientApp.Start (Seconds (1.0 + i));
129  clientApp.Stop (Seconds (runtime + 1.0 + i));
130  }
132  // This tells ns-3 to generate pcap traces.
133  p2p.EnablePcapAll ("tcp-nsc-lfn");
135  Simulator::Stop (Seconds (900));
136  Simulator::Run ();
139  return 0;
140 }
holds a vector of ns3::Application pointers.
an Inet address class
static Ipv4Address GetAny(void)
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
holds a vector of std::pair of Ptr and interface index.
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Hold variables of type string.
Definition: string.h:41
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:777
NetDeviceContainer Install(NodeContainer c)
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1001
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
tuple cmd
Definition: second.py:35
a polymophic address class
Definition: address.h:90
Class for representing data rates.
Definition: data-rate.h:88
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
AttributeValue implementation for Time.
Definition: nstime.h:1055
Hold an unsigned integer type.
Definition: uinteger.h:44
tuple p2pInterfaces
Definition: second.py:75
holds a vector of ns3::NetDevice pointers
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:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Hold objects of type Ptr.
Definition: pointer.h:36
void SetTcp(std::string tid)
set the Tcp stack which will not need any other parameter.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
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...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:498
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const