examples/tcp-nsc-lfn.cc
author Florian Westphal <fw@strlen.de>
Wed, 03 Sep 2008 23:24:59 +0200
changeset 3595 693faf7f4e9b
parent 3580 5818912ae9a3
child 4218 debf1a8a96d3
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
//           6Mb/s, 500ms
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    22
//       n0-----------------n1
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
// - a 'lossy' network with long delay
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    25
// - TCP flow from n0 to n1 and from n1 to n0
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    26
// - pcap traces generated as tcp-nsc-lfn-0-0.pcap and tcp-nsc-lfn-1-0.pcap
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    27
//  Usage (e.g.): ./waf --run 'tcp-nsc-lfn --TCP_CONGESTION=hybla --runtime=30'
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    28
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    29
#include <ctype.h>
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 <fstream>
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    32
#include <string>
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    33
#include <cassert>
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    34
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    35
#include "ns3/core-module.h"
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    36
#include "ns3/common-module.h"
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    37
#include "ns3/helper-module.h"
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    38
#include "ns3/node-module.h"
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    39
#include "ns3/global-route-manager.h"
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    40
#include "ns3/simulator-module.h"
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    41
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    42
using namespace ns3;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    43
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    44
NS_LOG_COMPONENT_DEFINE ("TcpNscLfn");
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
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    47
int main (int argc, char *argv[])
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
  RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    50
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    51
  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (4096));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    52
  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("6Mbps"));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    53
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    54
  // cubic is the default congestion algorithm in Linux 2.6.26
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    55
  std::string tcpCong = "cubic";
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    56
  // this is the default error rate of our link, that is, the the probability of a single
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    57
  // byte being 'corrupted' during transfer.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    58
  double errRate = 0.000001;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    59
  // how long the sender should be running, in seconds.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    60
  unsigned int runtime = 120;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    61
  // the name of the NSC stack library that should be used
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    62
  std::string nscStack = "liblinux2.6.26.so";
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    63
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    64
  CommandLine cmd;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    65
  // Here, we define additional command line options.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    66
  // This allows a user to override the defaults set above from the command line.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    67
  cmd.AddValue("TCP_CONGESTION", "Linux 2.6.26 Tcp Congestion control algorithm to use", tcpCong);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    68
  cmd.AddValue("error-rate", "Error rate to apply to link", errRate);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    69
  cmd.AddValue("runtime", "How long the applications should send data (default 120 seconds)", runtime);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    70
  cmd.AddValue("nscstack", "Set name of NSC stack (shared library) to use (default liblinux2.6.26.so)", nscStack);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    71
  cmd.Parse (argc, argv);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    72
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    73
  NodeContainer n;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    74
  n.Create (2);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    75
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    76
  PointToPointHelper p2p;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    77
  // create point-to-point link with a bandwidth of 6MBit/s and a large delay (0.5 seconds)
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    78
  p2p.SetDeviceAttribute ("DataRate", DataRateValue (DataRate(6 * 1000 * 1000)));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    79
  p2p.SetChannelAttribute ("Delay", TimeValue (MilliSeconds(500)));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    80
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    81
  NetDeviceContainer p2pInterfaces = p2p.Install (n);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    82
  // The default MTU of the p2p link would be 65535, which doesn't work
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    83
  // well with our default errRate (most packets would arrive corrupted).
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    84
  p2pInterfaces.Get(0)->SetMtu(1500);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    85
  p2pInterfaces.Get(1)->SetMtu(1500);
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
  InternetStackHelper internet;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    88
  // The next statement switches the nodes to 'NSC'-Mode.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    89
  // It disables the native ns-3 TCP model and loads the NSC library.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    90
  internet.SetNscStack (nscStack);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    91
  internet.Install (n);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    92
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    93
  if (tcpCong != "cubic") // make sure we only fail if both --nscstack and --TCP_CONGESTION are used
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    94
    {
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    95
      // This uses ns-3s attribute system to set the 'net.ipv4.tcp_congestion_control' sysctl of the
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    96
      // stack.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    97
      // The same mechanism could be used to e.g. disable TCP timestamps:
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    98
      // Config::Set ("/NodeList/*/$ns3::Ns3NscStack<linux2.6.26>/net.ipv4.tcp_timestamps", StringValue ("0"));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    99
      Config::Set ("/NodeList/*/$ns3::Ns3NscStack<linux2.6.26>/net.ipv4.tcp_congestion_control", StringValue (tcpCong));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   100
    }
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   101
  Ipv4AddressHelper ipv4;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   102
  ipv4.SetBase ("10.0.0.0", "255.255.255.0");
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   103
  Ipv4InterfaceContainer ipv4Interfaces = ipv4.Assign (p2pInterfaces);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   104
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   105
  DoubleValue rate(errRate);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   106
  RandomVariableValue u01(UniformVariable (0.0, 1.0));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   107
  Ptr<RateErrorModel> em1 = 
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   108
      CreateObject<RateErrorModel> ("RanVar", u01, "ErrorRate", rate);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   109
  Ptr<RateErrorModel> em2 = 
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   110
      CreateObject<RateErrorModel> ("RanVar", u01, "ErrorRate", rate);
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
  // This enables the specified errRate on both link endpoints.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   113
  p2pInterfaces.Get(0)->SetAttribute("ReceiveErrorModel", PointerValue (em1));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   114
  p2pInterfaces.Get(1)->SetAttribute("ReceiveErrorModel", PointerValue (em2));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   115
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   116
  GlobalRouteManager::PopulateRoutingTables ();
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   117
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   118
  uint16_t servPort = 8080;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   119
  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), servPort));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   120
  ApplicationContainer sinkApp = sinkHelper.Install (n);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   121
  sinkApp.Start (Seconds (0.0));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   122
  // this makes sure that the receiver will run one minute longer than the sender applicaton.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   123
  sinkApp.Stop (Seconds (runtime + 60.0));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   124
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   125
  // This sets up two TCP flows, one from A -> B, one from B -> A.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   126
  for (int i = 0, j = 1; i < 2; j--, i++)
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   127
   {
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   128
      Address remoteAddress(InetSocketAddress(ipv4Interfaces.GetAddress (i), servPort));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   129
      OnOffHelper clientHelper ("ns3::TcpSocketFactory", remoteAddress);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   130
      clientHelper.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   131
      clientHelper.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   132
      ApplicationContainer clientApp = clientHelper.Install(n.Get(j));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   133
      clientApp.Start (Seconds (1.0 + i));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   134
      clientApp.Stop (Seconds (runtime + 1.0 + i));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   135
    }
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   136
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   137
  // This tells ns-3 to generate pcap traces.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   138
  PointToPointHelper::EnablePcapAll ("tcp-nsc-lfn");
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   139
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   140
  Simulator::Stop (Seconds(900));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   141
  Simulator::Run ();
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   142
  Simulator::Destroy ();
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   143
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   144
  return 0;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   145
}