examples/tcp-nsc-lfn.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 23 Mar 2009 09:56:27 +0100
changeset 4359 8d689bf4405f
parent 4225 f1d873a81159
child 4283 5854cddf4493
permissions -rw-r--r--
remove dead files
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
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    50
  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (4096));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    51
  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("6Mbps"));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    52
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    53
  // cubic is the default congestion algorithm in Linux 2.6.26
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    54
  std::string tcpCong = "cubic";
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    55
  // 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
    56
  // byte being 'corrupted' during transfer.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    57
  double errRate = 0.000001;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    58
  // how long the sender should be running, in seconds.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    59
  unsigned int runtime = 120;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    60
  // the name of the NSC stack library that should be used
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    61
  std::string nscStack = "liblinux2.6.26.so";
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
  CommandLine cmd;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    64
  // Here, we define additional command line options.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    65
  // 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
    66
  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
    67
  cmd.AddValue("error-rate", "Error rate to apply to link", errRate);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    68
  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
    69
  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
    70
  cmd.Parse (argc, argv);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    71
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    72
  NodeContainer n;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    73
  n.Create (2);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    74
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    75
  PointToPointHelper p2p;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    76
  // 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
    77
  p2p.SetDeviceAttribute ("DataRate", DataRateValue (DataRate(6 * 1000 * 1000)));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    78
  p2p.SetChannelAttribute ("Delay", TimeValue (MilliSeconds(500)));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    79
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    80
  NetDeviceContainer p2pInterfaces = p2p.Install (n);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    81
  // 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
    82
  // well with our default errRate (most packets would arrive corrupted).
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    83
  p2pInterfaces.Get(0)->SetMtu(1500);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    84
  p2pInterfaces.Get(1)->SetMtu(1500);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    85
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    86
  InternetStackHelper internet;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    87
  // The next statement switches the nodes to 'NSC'-Mode.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    88
  // 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
    89
  internet.SetNscStack (nscStack);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    90
  internet.Install (n);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    91
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    92
  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
    93
    {
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    94
      // 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
    95
      // stack.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    96
      // The same mechanism could be used to e.g. disable TCP timestamps:
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
    97
      // 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
    98
      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
    99
    }
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   100
  Ipv4AddressHelper ipv4;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   101
  ipv4.SetBase ("10.0.0.0", "255.255.255.0");
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   102
  Ipv4InterfaceContainer ipv4Interfaces = ipv4.Assign (p2pInterfaces);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   103
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   104
  DoubleValue rate(errRate);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   105
  RandomVariableValue u01(UniformVariable (0.0, 1.0));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   106
  Ptr<RateErrorModel> em1 = 
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   107
      CreateObject<RateErrorModel> ("RanVar", u01, "ErrorRate", rate);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   108
  Ptr<RateErrorModel> em2 = 
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   109
      CreateObject<RateErrorModel> ("RanVar", u01, "ErrorRate", rate);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   110
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   111
  // This enables the specified errRate on both link endpoints.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   112
  p2pInterfaces.Get(0)->SetAttribute("ReceiveErrorModel", PointerValue (em1));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   113
  p2pInterfaces.Get(1)->SetAttribute("ReceiveErrorModel", PointerValue (em2));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   114
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   115
  GlobalRouteManager::PopulateRoutingTables ();
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   116
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   117
  uint16_t servPort = 8080;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   118
  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), servPort));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   119
  ApplicationContainer sinkApp = sinkHelper.Install (n);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   120
  sinkApp.Start (Seconds (0.0));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   121
  // 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
   122
  sinkApp.Stop (Seconds (runtime + 60.0));
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
  // 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
   125
  for (int i = 0, j = 1; i < 2; j--, i++)
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   126
   {
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   127
      Address remoteAddress(InetSocketAddress(ipv4Interfaces.GetAddress (i), servPort));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   128
      OnOffHelper clientHelper ("ns3::TcpSocketFactory", remoteAddress);
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   129
      clientHelper.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   130
      clientHelper.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   131
      ApplicationContainer clientApp = clientHelper.Install(n.Get(j));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   132
      clientApp.Start (Seconds (1.0 + i));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   133
      clientApp.Stop (Seconds (runtime + 1.0 + i));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   134
    }
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
  // This tells ns-3 to generate pcap traces.
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   137
  PointToPointHelper::EnablePcapAll ("tcp-nsc-lfn");
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   138
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   139
  Simulator::Stop (Seconds(900));
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   140
  Simulator::Run ();
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   141
  Simulator::Destroy ();
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   142
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   143
  return 0;
5818912ae9a3 nsc: example files.
Florian Westphal <fw@strlen.de>
parents:
diff changeset
   144
}