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";
60  CommandLine cmd (__FILE__);
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);
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)
holds a vector of std::pair of Ptr<Ipv4> 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
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:839
NetDeviceContainer Install(NodeContainer c)
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
Define a Log component with a specific name.
Definition: log.h:205
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1296
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
Definition: second.py:35
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
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:1352
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Hold an unsigned integer type.
Definition: uinteger.h:44
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:226
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
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<T>.
Definition: pointer.h:36
void SetTcp(std::string tid)
set the Tcp stack which will not need any other parameter.
Definition: second.py:75
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
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:229
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1288
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="")
Set the base network number, network mask and base address.