examples/tcp-nsc-zoo.cc
author Florian Westphal <fw@strlen.de>
Wed, 03 Sep 2008 23:24:59 +0200
changeset 3595 693faf7f4e9b
parent 3580 5818912ae9a3
child 3675 266033a58f24
permissions -rw-r--r--
nsc: Fix build problem if gtk config store is disabled gtk config store pulled in libdl.so for us, so things fail to link of the config store isn't enabled. This makes nsc pull in libdl itself when its enabled.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3580
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
     2
/*
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
     3
 * This program is free software; you can redistribute it and/or modify
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
     4
 * it under the terms of the GNU General Public License version 2 as
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
     5
 * published by the Free Software Foundation;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
     6
 *
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
     7
 * This program is distributed in the hope that it will be useful,
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
     8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
     9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    10
 * GNU General Public License for more details.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    11
 *
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    12
 * You should have received a copy of the GNU General Public License
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    13
 * along with this program; if not, write to the Free Software
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    15
 *
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    16
 */
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    17
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    18
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    19
// Network topology
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    20
//
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    21
//       n0    n1   n2   n3
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    22
//       |     |    |    |
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    23
//       =================
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    24
//              LAN
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    25
//
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    26
// - Pcap traces are saved as tcp-nsc-zoo-$n-0.pcap, where $n represents the node number
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    27
// - TCP flows from n0 to n1, n2, n3, from n1 to n0, n2, n3, etc.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    28
//  Usage (e.g.): ./waf --run 'tcp-nsc-zoo --Nodes=5'
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    29
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    30
#include <iostream>
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    31
#include <string>
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    32
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    33
#include "ns3/core-module.h"
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    34
#include "ns3/helper-module.h"
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    35
#include "ns3/node-module.h"
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    36
#include "ns3/global-route-manager.h"
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    37
#include "ns3/simulator-module.h"
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    38
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    39
using namespace ns3;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    40
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    41
NS_LOG_COMPONENT_DEFINE ("TcpNscZoo");
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    42
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    43
// Simulates a diverse network with various stacks supported by NSC.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    44
int main(int argc, char *argv[])
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    45
{
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    46
  CsmaHelper csma;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    47
  unsigned int MaxNodes = 4;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    48
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    49
  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (4096));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    50
  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("1Mb/s"));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    51
  CommandLine cmd;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    52
  // this allows the user to raise the number of nodes using --Nodes=X command-line argument.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    53
  cmd.AddValue("Nodes", "Number of nodes in the network", MaxNodes);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    54
  cmd.Parse (argc, argv);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    55
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    56
  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate(100 * 1000 * 1000)));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    57
  csma.SetChannelAttribute ("Delay", TimeValue (MicroSeconds (200)));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    58
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    59
  NodeContainer n;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    60
  n.Create(MaxNodes);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    61
  NetDeviceContainer ethInterfaces = csma.Install (n);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    62
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    63
  InternetStackHelper internetStack;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    64
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    65
  internetStack.SetNscStack ("liblinux2.6.26.so");
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    66
  // this switches nodes 0 and 1 to NSCs Linux 2.6.26 stack.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    67
  internetStack.Install (n.Get(0));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    68
  internetStack.Install (n.Get(1));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    69
  // this disables TCP SACK, wscale and timestamps on node 1 (the attributes represent sysctl-values).
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    70
  Config::Set ("/NodeList/1/$ns3::Ns3NscStack<linux2.6.26>/net.ipv4.tcp_sack", StringValue ("0"));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    71
  Config::Set ("/NodeList/1/$ns3::Ns3NscStack<linux2.6.26>/net.ipv4.tcp_timestamps", StringValue ("0"));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    72
  Config::Set ("/NodeList/1/$ns3::Ns3NscStack<linux2.6.26>/net.ipv4.tcp_window_scaling", StringValue ("0"));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    73
  internetStack.Install (n.Get(2));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    74
  // the next statement doesn't change anything for the nodes 0, 1, and 2; since they
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    75
  // already have a stack assigned.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    76
  internetStack.SetNscStack ("liblinux2.6.18.so");
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    77
  // this switches node 3 to NSCs Linux 2.6.18 stack.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    78
  internetStack.Install (n.Get(3));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    79
  // and then agains disables sack/timestamps/wscale on node 3.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    80
  Config::Set ("/NodeList/3/$ns3::Ns3NscStack<linux2.6.18>/net.ipv4.tcp_sack", StringValue ("0"));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    81
  Config::Set ("/NodeList/3/$ns3::Ns3NscStack<linux2.6.18>/net.ipv4.tcp_timestamps", StringValue ("0"));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    82
  Config::Set ("/NodeList/3/$ns3::Ns3NscStack<linux2.6.18>/net.ipv4.tcp_window_scaling", StringValue ("0"));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    83
  // the freebsd stack is not yet built by default, so its commented out for now.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    84
  // internetStack.SetNscStack ("libfreebsd5.so");
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    85
  for (unsigned int i =4; i < MaxNodes; i++)
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    86
    {
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    87
      internetStack.Install (n.Get(i));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    88
    }
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    89
  Ipv4AddressHelper ipv4;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    90
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    91
  ipv4.SetBase ("10.0.0.0", "255.255.255.0");
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    92
  Ipv4InterfaceContainer ipv4Interfaces = ipv4.Assign (ethInterfaces);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    93
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    94
  GlobalRouteManager::PopulateRoutingTables ();
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    95
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    96
  uint16_t servPort = 8080;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    97
  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), servPort));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    98
  // start a sink client on all nodes
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    99
  ApplicationContainer sinkApp = sinkHelper.Install (n);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   100
  sinkApp.Start (Seconds (1.0));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   101
  sinkApp.Stop (Seconds (30.0));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   102
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   103
  // This tells every node on the network to start a flow to all other nodes on the network ...
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   104
  for (unsigned int i = 0 ; i < MaxNodes;i++)
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   105
    {
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   106
      for (unsigned int j = 0 ; j < MaxNodes;j++)
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   107
        {
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   108
          if (i == j)
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   109
            {  // ...but we don't want a node to talk to itself.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   110
               continue;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   111
            }
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   112
          Address remoteAddress(InetSocketAddress(ipv4Interfaces.GetAddress (j), servPort));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   113
          OnOffHelper clientHelper ("ns3::TcpSocketFactory", remoteAddress);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   114
          clientHelper.SetAttribute 
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   115
            ("OnTime", RandomVariableValue (ConstantVariable (1)));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   116
          clientHelper.SetAttribute 
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   117
            ("OffTime", RandomVariableValue (ConstantVariable (0)));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   118
          ApplicationContainer clientApp = clientHelper.Install(n.Get(i));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   119
          clientApp.Start (Seconds (j + 1.0)); /* delay startup depending on node number */
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   120
          clientApp.Stop (Seconds (j + 10.0));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   121
	}
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   122
    }
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   123
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   124
  CsmaHelper::EnablePcapAll ("tcp-nsc-zoo");
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   125
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   126
  Simulator::Stop (Seconds(1000));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   127
  Simulator::Run ();
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   128
  Simulator::Destroy ();
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   129
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   130
  return 0;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   131
}