examples/virtual-net-device.cc
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Fri, 03 Jul 2009 11:17:38 +0100
changeset 4651 0f97d2edf425
parent 4616 a84f60b6cd12
permissions -rw-r--r--
Fix the virtual-net-device.cc example; thanks Antti M?kel?.
gjc@4538
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
gjc@4538
     2
/*
gjc@4538
     3
 * This program is free software; you can redistribute it and/or modify
gjc@4538
     4
 * it under the terms of the GNU General Public License version 2 as
gjc@4538
     5
 * published by the Free Software Foundation;
gjc@4538
     6
 *
gjc@4538
     7
 * This program is distributed in the hope that it will be useful,
gjc@4538
     8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
gjc@4538
     9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
gjc@4538
    10
 * GNU General Public License for more details.
gjc@4538
    11
 *
gjc@4538
    12
 * You should have received a copy of the GNU General Public License
gjc@4538
    13
 * along with this program; if not, write to the Free Software
gjc@4538
    14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
gjc@4538
    15
 *
gjc@4538
    16
 * Based on simple-global-routing.cc
gjc@4538
    17
 * ns-2 simple.tcl script (ported from ns-2)
gjc@4538
    18
 * Originally authored by Steve McCanne, 12/19/1996
gjc@4538
    19
 */
gjc@4538
    20
gjc@4538
    21
// Network topology
gjc@4538
    22
//
gjc@4538
    23
//  n0
gjc@4538
    24
//     \ 5 Mb/s, 2ms
gjc@4538
    25
//      \          1.5Mb/s, 10ms
gjc@4538
    26
//       n2 -------------------------n3
gjc@4538
    27
//      /
gjc@4538
    28
//     / 5 Mb/s, 2ms
gjc@4538
    29
//   n1
gjc@4538
    30
//
gjc@4538
    31
// - all links are point-to-point links with indicated one-way BW/delay
gjc@4538
    32
// - CBR/UDP flows from n0 to n3, and from n3 to n1
gjc@4538
    33
// - FTP/TCP flow from n0 to n3, starting at time 1.2 to time 1.35 sec.
gjc@4538
    34
// - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec.
gjc@4538
    35
//   (i.e., DataRate of 448,000 bps)
gjc@4538
    36
// - DropTail queues 
gjc@4542
    37
// - Tracing of queues and packet receptions to file "virtual-net-device.tr"
gjc@4538
    38
gjc@4538
    39
// Tunneling changes (relative to the simple-global-routing example):
gjc@4538
    40
// n0 will receive an extra virtual interface with address 11.0.0.1
gjc@4538
    41
// n1 will also receive an extra virtual interface with the same address 11.0.0.1
gjc@4538
    42
// n3 will receive an extra virtual interface with address 11.0.0.254
gjc@4538
    43
// The flows will be between 11.0.0.x (tunnel) addresses instead of 10.1.x.y ones
gjc@4538
    44
// n3 will decide, on a per-packet basis, via random number, whether to
gjc@4538
    45
// send the packet to n0 or to n1.
gjc@4542
    46
//
gjc@4542
    47
// Note: here we create a tunnel where IP packets are tunneled over
gjc@4542
    48
// UDP/IP, but tunneling directly IP-over-IP would also be possible;
gjc@4542
    49
// see src/node/ipv4-raw-socket-factory.h.
gjc@4538
    50
gjc@4538
    51
#include <iostream>
gjc@4538
    52
#include <fstream>
gjc@4538
    53
#include <string>
gjc@4538
    54
#include <cassert>
gjc@4538
    55
gjc@4538
    56
#include "ns3/core-module.h"
gjc@4538
    57
#include "ns3/simulator-module.h"
gjc@4538
    58
#include "ns3/node-module.h"
gjc@4538
    59
#include "ns3/helper-module.h"
gjc@4541
    60
#include "ns3/virtual-net-device.h"
gjc@4538
    61
gjc@4538
    62
using namespace ns3;
gjc@4538
    63
gjc@4541
    64
NS_LOG_COMPONENT_DEFINE ("VirtualNetDeviceExample");
gjc@4538
    65
gjc@4538
    66
class Tunnel
gjc@4538
    67
{
gjc@4538
    68
  Ptr<Socket> m_n3Socket;
gjc@4538
    69
  Ptr<Socket> m_n0Socket;
gjc@4538
    70
  Ptr<Socket> m_n1Socket;
gjc@4538
    71
  Ipv4Address m_n3Address;
gjc@4538
    72
  Ipv4Address m_n0Address;
gjc@4538
    73
  Ipv4Address m_n1Address;
gjc@4538
    74
  UniformVariable m_rng;
gjc@4541
    75
  Ptr<VirtualNetDevice> m_n0Tap;
gjc@4541
    76
  Ptr<VirtualNetDevice> m_n1Tap;
gjc@4541
    77
  Ptr<VirtualNetDevice> m_n3Tap;
gjc@4538
    78
  
gjc@4538
    79
  
gjc@4538
    80
  bool
gjc@4651
    81
  N0VirtualSend (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
gjc@4538
    82
  {
gjc@4651
    83
    m_n0Socket->SendTo (packet, 0, InetSocketAddress (m_n3Address, 667));
gjc@4651
    84
    return true;
gjc@4651
    85
  }
gjc@4651
    86
gjc@4651
    87
  bool
gjc@4651
    88
  N1VirtualSend (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
gjc@4651
    89
  {
gjc@4651
    90
    m_n1Socket->SendTo (packet, 0, InetSocketAddress (m_n3Address, 667));
gjc@4538
    91
    return true;
gjc@4538
    92
  }
gjc@4538
    93
gjc@4538
    94
  bool
gjc@4538
    95
  N3VirtualSend (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
gjc@4538
    96
  {
gjc@4538
    97
    if (m_rng.GetValue () < 0.25)
gjc@4538
    98
      {
gjc@4651
    99
        m_n3Socket->SendTo (packet, 0, InetSocketAddress (m_n0Address, 667));
gjc@4538
   100
      }
gjc@4538
   101
    else 
gjc@4538
   102
      {
gjc@4651
   103
        m_n3Socket->SendTo (packet, 0, InetSocketAddress (m_n1Address, 667));
gjc@4538
   104
      }
gjc@4538
   105
    return true;
gjc@4538
   106
  }
gjc@4538
   107
gjc@4538
   108
  void N3SocketRecv (Ptr<Socket> socket)
gjc@4538
   109
  {
gjc@4538
   110
    Ptr<Packet> packet = socket->Recv (65535, 0);
gjc@4548
   111
    SocketAddressTag socketAddressTag;
gjc@4548
   112
    packet->RemovePacketTag (socketAddressTag);
gjc@4545
   113
    m_n3Tap->Receive (packet, 0x0800, m_n3Tap->GetAddress (), m_n3Tap->GetAddress (), NetDevice::PACKET_HOST);
gjc@4538
   114
  }
gjc@4538
   115
gjc@4538
   116
  void N0SocketRecv (Ptr<Socket> socket)
gjc@4538
   117
  {
gjc@4538
   118
    Ptr<Packet> packet = socket->Recv (65535, 0);
gjc@4548
   119
    SocketAddressTag socketAddressTag;
gjc@4548
   120
    packet->RemovePacketTag (socketAddressTag);
gjc@4545
   121
    m_n0Tap->Receive (packet, 0x0800, m_n0Tap->GetAddress (), m_n0Tap->GetAddress (), NetDevice::PACKET_HOST);
gjc@4538
   122
  }
gjc@4538
   123
gjc@4538
   124
  void N1SocketRecv (Ptr<Socket> socket)
gjc@4538
   125
  {
gjc@4538
   126
    Ptr<Packet> packet = socket->Recv (65535, 0);
gjc@4548
   127
    SocketAddressTag socketAddressTag;
gjc@4548
   128
    packet->RemovePacketTag (socketAddressTag);
gjc@4545
   129
    m_n1Tap->Receive (packet, 0x0800, m_n1Tap->GetAddress (), m_n1Tap->GetAddress (), NetDevice::PACKET_HOST);
gjc@4538
   130
  }
gjc@4538
   131
gjc@4538
   132
public:
gjc@4538
   133
  
gjc@4538
   134
  Tunnel (Ptr<Node> n3, Ptr<Node> n0, Ptr<Node> n1,
gjc@4538
   135
          Ipv4Address n3Addr, Ipv4Address n0Addr, Ipv4Address n1Addr)
gjc@4538
   136
    : m_n3Address (n3Addr), m_n0Address (n0Addr), m_n1Address (n1Addr)
gjc@4538
   137
  {
gjc@4538
   138
    m_n3Socket = Socket::CreateSocket (n3, TypeId::LookupByName ("ns3::UdpSocketFactory"));
gjc@4538
   139
    m_n3Socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), 667));
gjc@4538
   140
    m_n3Socket->SetRecvCallback (MakeCallback (&Tunnel::N3SocketRecv, this));    
gjc@4538
   141
    
gjc@4538
   142
    m_n0Socket = Socket::CreateSocket (n0, TypeId::LookupByName ("ns3::UdpSocketFactory"));
gjc@4538
   143
    m_n0Socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), 667));
gjc@4538
   144
    m_n0Socket->SetRecvCallback (MakeCallback (&Tunnel::N0SocketRecv, this));    
gjc@4538
   145
    
gjc@4538
   146
    m_n1Socket = Socket::CreateSocket (n1, TypeId::LookupByName ("ns3::UdpSocketFactory"));
gjc@4538
   147
    m_n1Socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), 667));
gjc@4538
   148
    m_n1Socket->SetRecvCallback (MakeCallback (&Tunnel::N1SocketRecv, this));
gjc@4538
   149
    
gjc@4538
   150
    // n0 tap device
gjc@4541
   151
    m_n0Tap = CreateObject<VirtualNetDevice> ();
gjc@4545
   152
    m_n0Tap->SetAddress (Mac48Address ("11:00:01:02:03:01"));
gjc@4651
   153
    m_n0Tap->SetSendCallback (MakeCallback (&Tunnel::N0VirtualSend, this));
gjc@4538
   154
    n0->AddDevice (m_n0Tap);
gjc@4538
   155
    Ptr<Ipv4> ipv4 = n0->GetObject<Ipv4> ();
gjc@4538
   156
    uint32_t i = ipv4->AddInterface (m_n0Tap);
gjc@4538
   157
    ipv4->AddAddress (i, Ipv4InterfaceAddress (Ipv4Address ("11.0.0.1"), Ipv4Mask ("255.255.255.0")));
gjc@4538
   158
    ipv4->SetUp (i);
gjc@4538
   159
    
gjc@4538
   160
    // n1 tap device
gjc@4541
   161
    m_n1Tap = CreateObject<VirtualNetDevice> ();
gjc@4545
   162
    m_n1Tap->SetAddress (Mac48Address ("11:00:01:02:03:02"));
gjc@4651
   163
    m_n1Tap->SetSendCallback (MakeCallback (&Tunnel::N1VirtualSend, this));
gjc@4538
   164
    n1->AddDevice (m_n1Tap);
gjc@4538
   165
    ipv4 = n1->GetObject<Ipv4> ();
gjc@4538
   166
    i = ipv4->AddInterface (m_n1Tap);
gjc@4538
   167
    ipv4->AddAddress (i, Ipv4InterfaceAddress (Ipv4Address ("11.0.0.1"), Ipv4Mask ("255.255.255.0")));
gjc@4538
   168
    ipv4->SetUp (i);
gjc@4538
   169
gjc@4538
   170
    // n3 tap device
gjc@4541
   171
    m_n3Tap = CreateObject<VirtualNetDevice> ();
gjc@4545
   172
    m_n3Tap->SetAddress (Mac48Address ("11:00:01:02:03:04"));
gjc@4545
   173
    m_n3Tap->SetSendCallback (MakeCallback (&Tunnel::N3VirtualSend, this));
gjc@4538
   174
    n3->AddDevice (m_n3Tap);
gjc@4538
   175
    ipv4 = n3->GetObject<Ipv4> ();
gjc@4538
   176
    i = ipv4->AddInterface (m_n3Tap);
gjc@4538
   177
    ipv4->AddAddress (i, Ipv4InterfaceAddress (Ipv4Address ("11.0.0.254"), Ipv4Mask ("255.255.255.0")));
gjc@4538
   178
    ipv4->SetUp (i);
gjc@4538
   179
    
gjc@4538
   180
  }
gjc@4538
   181
  
gjc@4538
   182
  
gjc@4538
   183
};
gjc@4538
   184
gjc@4538
   185
  
gjc@4538
   186
gjc@4538
   187
int 
gjc@4538
   188
main (int argc, char *argv[])
gjc@4538
   189
{
gjc@4538
   190
  // Users may find it convenient to turn on explicit debugging
gjc@4538
   191
  // for selected modules; the below lines suggest how to do this
gjc@4538
   192
#if 0 
gjc@4542
   193
  LogComponentEnable ("VirtualNetDeviceExample", LOG_LEVEL_INFO);
gjc@4538
   194
#endif
gjc@4538
   195
gjc@4538
   196
  // Set up some default values for the simulation.  Use the 
gjc@4538
   197
  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (210));
gjc@4538
   198
  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("448kb/s"));
gjc@4538
   199
gjc@4538
   200
  //DefaultValue::Bind ("DropTailQueue::m_maxPackets", 30);   
gjc@4538
   201
gjc@4538
   202
  // Allow the user to override any of the defaults and the above
gjc@4538
   203
  // DefaultValue::Bind ()s at run-time, via command-line arguments
gjc@4538
   204
  CommandLine cmd;
gjc@4538
   205
  cmd.Parse (argc, argv);
gjc@4538
   206
gjc@4538
   207
  // Here, we will explicitly create four nodes.  In more sophisticated
gjc@4538
   208
  // topologies, we could configure a node factory.
gjc@4538
   209
  NS_LOG_INFO ("Create nodes.");
gjc@4538
   210
  NodeContainer c;
gjc@4538
   211
  c.Create (4);
gjc@4538
   212
  NodeContainer n0n2 = NodeContainer (c.Get(0), c.Get (2));
gjc@4538
   213
  NodeContainer n1n2 = NodeContainer (c.Get(1), c.Get (2));
gjc@4538
   214
  NodeContainer n3n2 = NodeContainer (c.Get(3), c.Get (2));
gjc@4538
   215
gjc@4538
   216
  InternetStackHelper internet;
gjc@4538
   217
  internet.Install (c);
gjc@4538
   218
gjc@4538
   219
  // We create the channels first without any IP addressing information
gjc@4538
   220
  NS_LOG_INFO ("Create channels.");
gjc@4538
   221
  PointToPointHelper p2p;
gjc@4538
   222
  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
gjc@4538
   223
  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
gjc@4538
   224
  NetDeviceContainer d0d2 = p2p.Install (n0n2);
gjc@4538
   225
gjc@4538
   226
  NetDeviceContainer d1d2 = p2p.Install (n1n2);
gjc@4538
   227
  
gjc@4538
   228
  p2p.SetDeviceAttribute ("DataRate", StringValue ("1500kbps"));
gjc@4538
   229
  p2p.SetChannelAttribute ("Delay", StringValue ("10ms"));
gjc@4538
   230
  NetDeviceContainer d3d2 = p2p.Install (n3n2);
gjc@4538
   231
  
gjc@4538
   232
  // Later, we add IP addresses.  
gjc@4538
   233
  NS_LOG_INFO ("Assign IP Addresses.");
gjc@4538
   234
  Ipv4AddressHelper ipv4;
gjc@4538
   235
  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
gjc@4538
   236
  Ipv4InterfaceContainer i0i2 = ipv4.Assign (d0d2);
gjc@4538
   237
gjc@4538
   238
  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
gjc@4538
   239
  Ipv4InterfaceContainer i1i2 = ipv4.Assign (d1d2);
gjc@4538
   240
  
gjc@4538
   241
  ipv4.SetBase ("10.1.3.0", "255.255.255.0");
gjc@4538
   242
  Ipv4InterfaceContainer i3i2 = ipv4.Assign (d3d2);
gjc@4538
   243
gjc@4538
   244
  // Create router nodes, initialize routing database and set up the routing
gjc@4538
   245
  // tables in the nodes.
mathieu@4616
   246
  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
gjc@4538
   247
gjc@4538
   248
  // Add the tunnels
gjc@4538
   249
  Tunnel tunnel (c.Get (3), c.Get (0), c.Get (1),
gjc@4538
   250
                 i3i2.GetAddress (0), i0i2.GetAddress (0), i1i2.GetAddress (0));
gjc@4538
   251
gjc@4538
   252
  // Create the OnOff application to send UDP datagrams of size
gjc@4538
   253
  // 210 bytes at a rate of 448 Kb/s
gjc@4538
   254
  NS_LOG_INFO ("Create Applications.");
gjc@4538
   255
  uint16_t port = 9;   // Discard port (RFC 863)
gjc@4538
   256
  OnOffHelper onoff ("ns3::UdpSocketFactory", 
gjc@4538
   257
                     Address (InetSocketAddress (Ipv4Address ("11.0.0.254"), port)));
gjc@4538
   258
  onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
gjc@4538
   259
  onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
gjc@4538
   260
  ApplicationContainer apps = onoff.Install (c.Get (0));
gjc@4538
   261
  apps.Start (Seconds (1.0));
gjc@4538
   262
  apps.Stop (Seconds (10.0));
gjc@4538
   263
gjc@4538
   264
  // Create a packet sink to receive these packets
gjc@4538
   265
  PacketSinkHelper sink ("ns3::UdpSocketFactory",
gjc@4538
   266
    Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
gjc@4538
   267
  apps = sink.Install (c.Get (3));
gjc@4538
   268
  apps.Start (Seconds (1.0));
gjc@4538
   269
  apps.Stop (Seconds (10.0));
gjc@4538
   270
gjc@4538
   271
  // Create a similar flow from n3 to n1, starting at time 1.1 seconds
gjc@4538
   272
  onoff.SetAttribute ("Remote", 
gjc@4538
   273
                      AddressValue (InetSocketAddress (Ipv4Address ("11.0.0.2"), port)));
gjc@4538
   274
  apps = onoff.Install (c.Get (3));
gjc@4538
   275
  apps.Start (Seconds (1.1));
gjc@4538
   276
  apps.Stop (Seconds (10.0));
gjc@4538
   277
gjc@4538
   278
  // Create a packet sink to receive these packets
gjc@4538
   279
  apps = sink.Install (c.Get (1));
gjc@4538
   280
  apps.Start (Seconds (1.1));
gjc@4538
   281
  apps.Stop (Seconds (10.0));
gjc@4538
   282
gjc@4538
   283
  std::ofstream ascii;
gjc@4542
   284
  ascii.open ("virtual-net-device.tr");
gjc@4542
   285
  PointToPointHelper::EnablePcapAll ("virtual-net-device");
gjc@4538
   286
  PointToPointHelper::EnableAsciiAll (ascii);
gjc@4538
   287
gjc@4538
   288
  NS_LOG_INFO ("Run Simulation.");
gjc@4538
   289
  Simulator::Run ();
gjc@4538
   290
  Simulator::Destroy ();
gjc@4538
   291
  NS_LOG_INFO ("Done.");
gjc@4538
   292
gjc@4538
   293
  return 0;
gjc@4538
   294
}