A Discrete-Event Network Simulator
API
tcp-nsc-zoo.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 
19 // Network topology
20 //
21 // n0 n1 n2 n3
22 // | | | |
23 // =================
24 // LAN
25 //
26 // - Pcap traces are saved as tcp-nsc-zoo-$n-0.pcap, where $n represents the node number
27 // - TCP flows from n0 to n1, n2, n3, from n1 to n0, n2, n3, etc.
28 // Usage (e.g.): ./waf --run 'tcp-nsc-zoo --nodes=5'
29 
30 #include <iostream>
31 #include <string>
32 
33 #include "ns3/core-module.h"
34 #include "ns3/applications-module.h"
35 #include "ns3/network-module.h"
36 #include "ns3/csma-module.h"
37 #include "ns3/internet-module.h"
38 
39 using namespace ns3;
40 
41 NS_LOG_COMPONENT_DEFINE ("TcpNscZoo");
42 
43 // Simulates a diverse network with various stacks supported by NSC.
44 int main (int argc, char *argv[])
45 {
47  unsigned int MaxNodes = 4;
48  unsigned int runtime = 3;
49 
50  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (2048));
51  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("8kbps"));
53  // this allows the user to raise the number of nodes using --nodes=X command-line argument.
54  cmd.AddValue ("nodes", "Number of nodes in the network (must be > 1)", MaxNodes);
55  cmd.AddValue ("runtime", "How long the applications should send data (default 3 seconds)", runtime);
56  cmd.Parse (argc, argv);
57 
58  if (MaxNodes < 2)
59  {
60  std::cerr << "--nodes: must be >= 2" << std::endl;
61  return 1;
62  }
63  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (100 * 1000 * 1000)));
64  csma.SetChannelAttribute ("Delay", TimeValue (MicroSeconds (200)));
65 
67  n.Create (MaxNodes);
68  NetDeviceContainer ethInterfaces = csma.Install (n);
69 
70  InternetStackHelper internetStack;
71  internetStack.SetTcp ("ns3::NscTcpL4Protocol","Library",StringValue ("liblinux2.6.26.so"));
72  // this switches nodes 0 and 1 to NSCs Linux 2.6.26 stack.
73  internetStack.Install (n.Get (0));
74  internetStack.Install (n.Get (1));
75  // this disables TCP SACK, wscale and timestamps on node 1 (the attributes represent sysctl-values).
76  Config::Set ("/NodeList/1/$ns3::Ns3NscStack<linux2.6.26>/net.ipv4.tcp_sack", StringValue ("0"));
77  Config::Set ("/NodeList/1/$ns3::Ns3NscStack<linux2.6.26>/net.ipv4.tcp_timestamps", StringValue ("0"));
78  Config::Set ("/NodeList/1/$ns3::Ns3NscStack<linux2.6.26>/net.ipv4.tcp_window_scaling", StringValue ("0"));
79 
80  if (MaxNodes > 2)
81  {
82  internetStack.Install (n.Get (2));
83  }
84 
85  if (MaxNodes > 3)
86  {
87  // the next statement doesn't change anything for the nodes 0, 1, and 2; since they
88  // already have a stack assigned.
89  internetStack.SetTcp ("ns3::NscTcpL4Protocol","Library",StringValue ("liblinux2.6.26.so"));
90  // this switches node 3 to NSCs Linux 2.6.26 stack.
91  internetStack.Install (n.Get (3));
92  // and then again disables sack/timestamps/wscale on node 3.
93  Config::Set ("/NodeList/3/$ns3::Ns3NscStack<linux2.6.26>/net.ipv4.tcp_sack", StringValue ("0"));
94  Config::Set ("/NodeList/3/$ns3::Ns3NscStack<linux2.6.26>/net.ipv4.tcp_timestamps", StringValue ("0"));
95  Config::Set ("/NodeList/3/$ns3::Ns3NscStack<linux2.6.26>/net.ipv4.tcp_window_scaling", StringValue ("0"));
96  }
97  // the freebsd stack is not yet built by default, so its commented out for now.
98  // internetStack.SetNscStack ("libfreebsd5.so");
99  for (unsigned int i =4; i < MaxNodes; i++)
100  {
101  internetStack.Install (n.Get (i));
102  }
103  Ipv4AddressHelper ipv4;
104 
105  ipv4.SetBase ("10.0.0.0", "255.255.255.0");
106  Ipv4InterfaceContainer ipv4Interfaces = ipv4.Assign (ethInterfaces);
107 
109 
110  uint16_t servPort = 8080;
111  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), servPort));
112  // start a sink client on all nodes
113  ApplicationContainer sinkApp = sinkHelper.Install (n);
114  sinkApp.Start (Seconds (0));
115  sinkApp.Stop (Seconds (30.0));
116 
117  // This tells every node on the network to start a flow to all other nodes on the network ...
118  for (unsigned int i = 0; i < MaxNodes; i++)
119  {
120  for (unsigned int j = 0; j < MaxNodes; j++)
121  {
122  if (i == j)
123  { // ...but we don't want a node to talk to itself.
124  continue;
125  }
126  Address remoteAddress (InetSocketAddress (ipv4Interfaces.GetAddress (j), servPort));
127  OnOffHelper clientHelper ("ns3::TcpSocketFactory", remoteAddress);
128  clientHelper.SetAttribute
129  ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
130  clientHelper.SetAttribute
131  ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
132  ApplicationContainer clientApp = clientHelper.Install (n.Get (i));
133  clientApp.Start (Seconds (j)); /* delay startup depending on node number */
134  clientApp.Stop (Seconds (j + runtime));
135  }
136  }
137 
138  csma.EnablePcapAll ("tcp-nsc-zoo", false);
139 
140  Simulator::Stop (Seconds (100));
141  Simulator::Run ();
143 
144  return 0;
145 }
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:777
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
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
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
AttributeValue implementation for Time.
Definition: nstime.h:1076
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...
csma
Definition: second.py:63
Parse command-line arguments.
Definition: command-line.h:213
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.
void SetTcp(std::string tid)
set the Tcp stack which will not need any other parameter.
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
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:242
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.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1030
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.