examples/csma-bridge.cc
author Florian Westphal <fw@strlen.de>
Wed, 03 Sep 2008 23:24:59 +0200
changeset 3595 693faf7f4e9b
parent 3524 028b7de98fb1
child 3715 594e2052e5b2
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:
3440
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     2
/*
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     3
 * This program is free software; you can redistribute it and/or modify
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     4
 * it under the terms of the GNU General Public License version 2 as
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     5
 * published by the Free Software Foundation;
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     6
 *
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     7
 * This program is distributed in the hope that it will be useful,
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    10
 * GNU General Public License for more details.
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    11
 *
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    12
 * You should have received a copy of the GNU General Public License
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    13
 * along with this program; if not, write to the Free Software
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    15
 */
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    16
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    17
// Network topology
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    18
//
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    19
//        n0     n1  
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    20
//        |      | 
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    21
//       ----------
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    22
//       | Switch |
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    23
//       ----------
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    24
//        |      | 
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    25
//        n2     n3  
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    26
//
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    27
//
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    28
// - CBR/UDP flows from n0 to n1 and from n3 to n0
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    29
// - DropTail queues 
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    30
// - Tracing of queues and packet receptions to file "csma-bridge.tr"
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    31
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    32
#include <iostream>
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    33
#include <fstream>
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    34
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    35
#include "ns3/simulator-module.h"
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    36
#include "ns3/node-module.h"
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    37
#include "ns3/core-module.h"
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    38
#include "ns3/helper-module.h"
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    39
#include "ns3/bridge-module.h"
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    40
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    41
using namespace ns3;
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    42
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    43
NS_LOG_COMPONENT_DEFINE ("CsmaBridgeExample");
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    44
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    45
int 
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    46
main (int argc, char *argv[])
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    47
{
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    48
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    49
  // Users may find it convenient to turn on explicit debugging
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    50
  // for selected modules; the below lines suggest how to do this
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    51
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    52
#if 0 
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    53
  LogComponentEnable ("CsmaBridgeExample", LOG_LEVEL_INFO);
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    54
#endif
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    55
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    56
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    57
  // Make the random number generators generate reproducible results.
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    58
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    59
  RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8);
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    60
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    61
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    62
  // Allow the user to override any of the defaults and the above Bind() at
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    63
  // run-time, via command-line arguments
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    64
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    65
  CommandLine cmd;
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    66
  cmd.Parse (argc, argv);
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    67
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    68
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    69
  // Explicitly create the nodes required by the topology (shown above).
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    70
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    71
  NS_LOG_INFO ("Create nodes.");
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    72
  NodeContainer terminals;
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    73
  terminals.Create (4);
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    74
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    75
  NodeContainer csmaSwitch;
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    76
  csmaSwitch.Create (1);
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    77
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    78
  NS_LOG_INFO ("Build Topology");
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    79
  CsmaHelper csma;
3451
4e4cb6a885ca Adapt to evil API breakage
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3440
diff changeset
    80
  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
4e4cb6a885ca Adapt to evil API breakage
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3440
diff changeset
    81
  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
3440
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    82
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    83
  // Create the csma links, from each terminal to the switch
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    84
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    85
  NetDeviceContainer terminalDevices;
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    86
  NetDeviceContainer switchDevices;
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    87
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    88
  for (int i = 0; i < 4; i++)
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    89
    {
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    90
      NetDeviceContainer link = csma.Install (NodeContainer (terminals.Get (i), csmaSwitch));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    91
      terminalDevices.Add (link.Get (0));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    92
      switchDevices.Add (link.Get (1));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    93
    }
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    94
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    95
  // Create the bridge netdevice, which will do the packet switching
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    96
  Ptr<Node> switchNode = csmaSwitch.Get (0);
3524
028b7de98fb1 bug 276: bridge netdevice has no helper class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3451
diff changeset
    97
  BridgeHelper bridge;
028b7de98fb1 bug 276: bridge netdevice has no helper class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3451
diff changeset
    98
  bridge.Install (switchNode, switchDevices);
3440
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    99
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   100
  // Add internet stack to the terminals
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   101
  InternetStackHelper internet;
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   102
  internet.Install (terminals);
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   103
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   104
  // We've got the "hardware" in place.  Now we need to add IP addresses.
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   105
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   106
  NS_LOG_INFO ("Assign IP Addresses.");
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   107
  Ipv4AddressHelper ipv4;
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   108
  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   109
  ipv4.Assign (terminalDevices);
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   110
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   111
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   112
  // Create an OnOff application to send UDP datagrams from node zero to node 1.
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   113
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   114
  NS_LOG_INFO ("Create Applications.");
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   115
  uint16_t port = 9;   // Discard port (RFC 863)
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   116
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   117
  OnOffHelper onoff ("ns3::UdpSocketFactory", 
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   118
                     Address (InetSocketAddress (Ipv4Address ("10.1.1.2"), port)));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   119
  onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   120
  onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   121
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   122
  ApplicationContainer app = onoff.Install (terminals.Get (0));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   123
  // Start the application
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   124
  app.Start (Seconds (1.0));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   125
  app.Stop (Seconds (10.0));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   126
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   127
  // Create an optional packet sink to receive these packets
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   128
  PacketSinkHelper sink ("ns3::UdpSocketFactory",
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   129
                         Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   130
  sink.Install (terminals.Get (1));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   131
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   132
  // 
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   133
  // Create a similar flow from n3 to n0, starting at time 1.1 seconds
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   134
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   135
  onoff.SetAttribute ("Remote", 
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   136
                      AddressValue (InetSocketAddress (Ipv4Address ("10.1.1.1"), port)));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   137
  ApplicationContainer app2 = onoff.Install (terminals.Get (3));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   138
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   139
  sink.Install (terminals.Get (0));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   140
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   141
  app2.Start (Seconds (1.1));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   142
  app2.Stop (Seconds (10.0));
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   143
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   144
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   145
  // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   146
  // Trace output will be sent to the file "csma-bridge.tr"
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   147
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   148
  NS_LOG_INFO ("Configure Tracing.");
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   149
  std::ofstream ascii;
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   150
  ascii.open ("csma-bridge.tr");
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   151
  CsmaHelper::EnableAsciiAll (ascii);
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   152
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   153
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   154
  // Also configure some tcpdump traces; each interface will be traced.
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   155
  // The output files will be named:
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   156
  //     csma-bridge.pcap-<nodeId>-<interfaceId>
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   157
  // and can be read by the "tcpdump -r" command (use "-tt" option to
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   158
  // display timestamps correctly)
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   159
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   160
  CsmaHelper::EnablePcapAll ("csma-bridge");
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   161
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   162
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   163
  // Now, do the actual simulation.
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   164
  //
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   165
  NS_LOG_INFO ("Run Simulation.");
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   166
  Simulator::Run ();
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   167
  Simulator::Destroy ();
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   168
  NS_LOG_INFO ("Done.");
4cedf996375d Start of a BridgeNetDevice example, using csma (basically an Ethernet Switch).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   169
}