1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/tap-net-device.cc Sat May 30 17:36:50 2009 +0100
1.3 @@ -0,0 +1,278 @@
1.4 +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1.5 +/*
1.6 + * This program is free software; you can redistribute it and/or modify
1.7 + * it under the terms of the GNU General Public License version 2 as
1.8 + * published by the Free Software Foundation;
1.9 + *
1.10 + * This program is distributed in the hope that it will be useful,
1.11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.13 + * GNU General Public License for more details.
1.14 + *
1.15 + * You should have received a copy of the GNU General Public License
1.16 + * along with this program; if not, write to the Free Software
1.17 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.18 + *
1.19 + * Based on simple-global-routing.cc
1.20 + * ns-2 simple.tcl script (ported from ns-2)
1.21 + * Originally authored by Steve McCanne, 12/19/1996
1.22 + */
1.23 +
1.24 +// Port of ns-2/tcl/ex/simple.tcl to ns-3
1.25 +//
1.26 +// Network topology
1.27 +//
1.28 +// n0
1.29 +// \ 5 Mb/s, 2ms
1.30 +// \ 1.5Mb/s, 10ms
1.31 +// n2 -------------------------n3
1.32 +// /
1.33 +// / 5 Mb/s, 2ms
1.34 +// n1
1.35 +//
1.36 +// - all links are point-to-point links with indicated one-way BW/delay
1.37 +// - CBR/UDP flows from n0 to n3, and from n3 to n1
1.38 +// - FTP/TCP flow from n0 to n3, starting at time 1.2 to time 1.35 sec.
1.39 +// - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec.
1.40 +// (i.e., DataRate of 448,000 bps)
1.41 +// - DropTail queues
1.42 +// - Tracing of queues and packet receptions to file "simple-global-routing.tr"
1.43 +
1.44 +// Tunneling changes (relative to the simple-global-routing example):
1.45 +// n0 will receive an extra virtual interface with address 11.0.0.1
1.46 +// n1 will also receive an extra virtual interface with the same address 11.0.0.1
1.47 +// n3 will receive an extra virtual interface with address 11.0.0.254
1.48 +// The flows will be between 11.0.0.x (tunnel) addresses instead of 10.1.x.y ones
1.49 +// n3 will decide, on a per-packet basis, via random number, whether to
1.50 +// send the packet to n0 or to n1.
1.51 +
1.52 +#include <iostream>
1.53 +#include <fstream>
1.54 +#include <string>
1.55 +#include <cassert>
1.56 +
1.57 +#include "ns3/core-module.h"
1.58 +#include "ns3/simulator-module.h"
1.59 +#include "ns3/node-module.h"
1.60 +#include "ns3/helper-module.h"
1.61 +#include "ns3/global-route-manager.h"
1.62 +#include "ns3/tap-net-device.h"
1.63 +
1.64 +using namespace ns3;
1.65 +
1.66 +NS_LOG_COMPONENT_DEFINE ("TapNetDeviceExample");
1.67 +
1.68 +class Tunnel
1.69 +{
1.70 + Ptr<Socket> m_n3Socket;
1.71 + Ptr<Socket> m_n0Socket;
1.72 + Ptr<Socket> m_n1Socket;
1.73 + Ipv4Address m_n3Address;
1.74 + Ipv4Address m_n0Address;
1.75 + Ipv4Address m_n1Address;
1.76 + UniformVariable m_rng;
1.77 + Ptr<TapNetDevice> m_n0Tap;
1.78 + Ptr<TapNetDevice> m_n1Tap;
1.79 + Ptr<TapNetDevice> m_n3Tap;
1.80 +
1.81 +
1.82 + bool
1.83 + N0N1VirtualSend (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
1.84 + {
1.85 + m_n3Socket->SendTo (packet, 0, InetSocketAddress (m_n3Address, 667));
1.86 + return true;
1.87 + }
1.88 +
1.89 + bool
1.90 + N3VirtualSend (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
1.91 + {
1.92 +
1.93 + if (m_rng.GetValue () < 0.25)
1.94 + {
1.95 + m_n0Socket->SendTo (packet, 0, InetSocketAddress (m_n0Address, 667));
1.96 + }
1.97 + else
1.98 + {
1.99 + m_n1Socket->SendTo (packet, 0, InetSocketAddress (m_n1Address, 667));
1.100 + }
1.101 + return true;
1.102 + }
1.103 +
1.104 + void N3SocketRecv (Ptr<Socket> socket)
1.105 + {
1.106 + Ptr<Packet> packet = socket->Recv (65535, 0);
1.107 + m_n3Tap->Receive (packet, 0x0800, Address ());
1.108 + }
1.109 +
1.110 + void N0SocketRecv (Ptr<Socket> socket)
1.111 + {
1.112 + Ptr<Packet> packet = socket->Recv (65535, 0);
1.113 + m_n0Tap->Receive (packet, 0x0800, Address ());
1.114 + }
1.115 +
1.116 + void N1SocketRecv (Ptr<Socket> socket)
1.117 + {
1.118 + Ptr<Packet> packet = socket->Recv (65535, 0);
1.119 + m_n0Tap->Receive (packet, 0x0800, Address ());
1.120 + }
1.121 +
1.122 +public:
1.123 +
1.124 + Tunnel (Ptr<Node> n3, Ptr<Node> n0, Ptr<Node> n1,
1.125 + Ipv4Address n3Addr, Ipv4Address n0Addr, Ipv4Address n1Addr)
1.126 + : m_n3Address (n3Addr), m_n0Address (n0Addr), m_n1Address (n1Addr)
1.127 + {
1.128 + m_n3Socket = Socket::CreateSocket (n3, TypeId::LookupByName ("ns3::UdpSocketFactory"));
1.129 + m_n3Socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), 667));
1.130 + m_n3Socket->SetRecvCallback (MakeCallback (&Tunnel::N3SocketRecv, this));
1.131 +
1.132 + m_n0Socket = Socket::CreateSocket (n0, TypeId::LookupByName ("ns3::UdpSocketFactory"));
1.133 + m_n0Socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), 667));
1.134 + m_n0Socket->SetRecvCallback (MakeCallback (&Tunnel::N0SocketRecv, this));
1.135 +
1.136 + m_n1Socket = Socket::CreateSocket (n1, TypeId::LookupByName ("ns3::UdpSocketFactory"));
1.137 + m_n1Socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), 667));
1.138 + m_n1Socket->SetRecvCallback (MakeCallback (&Tunnel::N1SocketRecv, this));
1.139 +
1.140 + // n0 tap device
1.141 + m_n0Tap = CreateObject<TapNetDevice> ();
1.142 + m_n0Tap->SetSendFromCallback (MakeCallback (&Tunnel::N0N1VirtualSend, this));
1.143 + n0->AddDevice (m_n0Tap);
1.144 + Ptr<Ipv4> ipv4 = n0->GetObject<Ipv4> ();
1.145 + uint32_t i = ipv4->AddInterface (m_n0Tap);
1.146 + ipv4->AddAddress (i, Ipv4InterfaceAddress (Ipv4Address ("11.0.0.1"), Ipv4Mask ("255.255.255.0")));
1.147 + ipv4->SetUp (i);
1.148 +
1.149 + // n1 tap device
1.150 + m_n1Tap = CreateObject<TapNetDevice> ();
1.151 + m_n1Tap->SetSendFromCallback (MakeCallback (&Tunnel::N0N1VirtualSend, this));
1.152 + n1->AddDevice (m_n1Tap);
1.153 + ipv4 = n1->GetObject<Ipv4> ();
1.154 + i = ipv4->AddInterface (m_n1Tap);
1.155 + ipv4->AddAddress (i, Ipv4InterfaceAddress (Ipv4Address ("11.0.0.1"), Ipv4Mask ("255.255.255.0")));
1.156 + ipv4->SetUp (i);
1.157 +
1.158 + // n3 tap device
1.159 + m_n3Tap = CreateObject<TapNetDevice> ();
1.160 + m_n3Tap->SetSendFromCallback (MakeCallback (&Tunnel::N3VirtualSend, this));
1.161 + n3->AddDevice (m_n3Tap);
1.162 + ipv4 = n3->GetObject<Ipv4> ();
1.163 + i = ipv4->AddInterface (m_n3Tap);
1.164 + ipv4->AddAddress (i, Ipv4InterfaceAddress (Ipv4Address ("11.0.0.254"), Ipv4Mask ("255.255.255.0")));
1.165 + ipv4->SetUp (i);
1.166 +
1.167 + }
1.168 +
1.169 +
1.170 +};
1.171 +
1.172 +
1.173 +
1.174 +int
1.175 +main (int argc, char *argv[])
1.176 +{
1.177 + // Users may find it convenient to turn on explicit debugging
1.178 + // for selected modules; the below lines suggest how to do this
1.179 +#if 0
1.180 + LogComponentEnable ("SimpleGlobalRoutingExample", LOG_LEVEL_INFO);
1.181 +#endif
1.182 +
1.183 + // Set up some default values for the simulation. Use the
1.184 + Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (210));
1.185 + Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("448kb/s"));
1.186 +
1.187 + //DefaultValue::Bind ("DropTailQueue::m_maxPackets", 30);
1.188 +
1.189 + // Allow the user to override any of the defaults and the above
1.190 + // DefaultValue::Bind ()s at run-time, via command-line arguments
1.191 + CommandLine cmd;
1.192 + cmd.Parse (argc, argv);
1.193 +
1.194 + // Here, we will explicitly create four nodes. In more sophisticated
1.195 + // topologies, we could configure a node factory.
1.196 + NS_LOG_INFO ("Create nodes.");
1.197 + NodeContainer c;
1.198 + c.Create (4);
1.199 + NodeContainer n0n2 = NodeContainer (c.Get(0), c.Get (2));
1.200 + NodeContainer n1n2 = NodeContainer (c.Get(1), c.Get (2));
1.201 + NodeContainer n3n2 = NodeContainer (c.Get(3), c.Get (2));
1.202 +
1.203 + InternetStackHelper internet;
1.204 + internet.Install (c);
1.205 +
1.206 + // We create the channels first without any IP addressing information
1.207 + NS_LOG_INFO ("Create channels.");
1.208 + PointToPointHelper p2p;
1.209 + p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
1.210 + p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
1.211 + NetDeviceContainer d0d2 = p2p.Install (n0n2);
1.212 +
1.213 + NetDeviceContainer d1d2 = p2p.Install (n1n2);
1.214 +
1.215 + p2p.SetDeviceAttribute ("DataRate", StringValue ("1500kbps"));
1.216 + p2p.SetChannelAttribute ("Delay", StringValue ("10ms"));
1.217 + NetDeviceContainer d3d2 = p2p.Install (n3n2);
1.218 +
1.219 + // Later, we add IP addresses.
1.220 + NS_LOG_INFO ("Assign IP Addresses.");
1.221 + Ipv4AddressHelper ipv4;
1.222 + ipv4.SetBase ("10.1.1.0", "255.255.255.0");
1.223 + Ipv4InterfaceContainer i0i2 = ipv4.Assign (d0d2);
1.224 +
1.225 + ipv4.SetBase ("10.1.2.0", "255.255.255.0");
1.226 + Ipv4InterfaceContainer i1i2 = ipv4.Assign (d1d2);
1.227 +
1.228 + ipv4.SetBase ("10.1.3.0", "255.255.255.0");
1.229 + Ipv4InterfaceContainer i3i2 = ipv4.Assign (d3d2);
1.230 +
1.231 + // Create router nodes, initialize routing database and set up the routing
1.232 + // tables in the nodes.
1.233 + GlobalRouteManager::PopulateRoutingTables ();
1.234 +
1.235 + // Add the tunnels
1.236 + Tunnel tunnel (c.Get (3), c.Get (0), c.Get (1),
1.237 + i3i2.GetAddress (0), i0i2.GetAddress (0), i1i2.GetAddress (0));
1.238 +
1.239 + // Create the OnOff application to send UDP datagrams of size
1.240 + // 210 bytes at a rate of 448 Kb/s
1.241 + NS_LOG_INFO ("Create Applications.");
1.242 + uint16_t port = 9; // Discard port (RFC 863)
1.243 + OnOffHelper onoff ("ns3::UdpSocketFactory",
1.244 + Address (InetSocketAddress (Ipv4Address ("11.0.0.254"), port)));
1.245 + onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
1.246 + onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
1.247 + ApplicationContainer apps = onoff.Install (c.Get (0));
1.248 + apps.Start (Seconds (1.0));
1.249 + apps.Stop (Seconds (10.0));
1.250 +
1.251 + // Create a packet sink to receive these packets
1.252 + PacketSinkHelper sink ("ns3::UdpSocketFactory",
1.253 + Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
1.254 + apps = sink.Install (c.Get (3));
1.255 + apps.Start (Seconds (1.0));
1.256 + apps.Stop (Seconds (10.0));
1.257 +
1.258 + // Create a similar flow from n3 to n1, starting at time 1.1 seconds
1.259 + onoff.SetAttribute ("Remote",
1.260 + AddressValue (InetSocketAddress (Ipv4Address ("11.0.0.2"), port)));
1.261 + apps = onoff.Install (c.Get (3));
1.262 + apps.Start (Seconds (1.1));
1.263 + apps.Stop (Seconds (10.0));
1.264 +
1.265 + // Create a packet sink to receive these packets
1.266 + apps = sink.Install (c.Get (1));
1.267 + apps.Start (Seconds (1.1));
1.268 + apps.Stop (Seconds (10.0));
1.269 +
1.270 + std::ofstream ascii;
1.271 + ascii.open ("tap-net-device.tr");
1.272 + PointToPointHelper::EnablePcapAll ("tap-net-device");
1.273 + PointToPointHelper::EnableAsciiAll (ascii);
1.274 +
1.275 + NS_LOG_INFO ("Run Simulation.");
1.276 + Simulator::Run ();
1.277 + Simulator::Destroy ();
1.278 + NS_LOG_INFO ("Done.");
1.279 +
1.280 + return 0;
1.281 +}
2.1 --- a/examples/wscript Sat May 30 17:36:29 2009 +0100
2.2 +++ b/examples/wscript Sat May 30 17:36:50 2009 +0100
2.3 @@ -40,6 +40,10 @@
2.4 ['point-to-point', 'internet-stack', 'global-routing'])
2.5 obj.source = 'simple-global-routing.cc'
2.6
2.7 + obj = bld.create_ns3_program('tap-net-device',
2.8 + ['point-to-point', 'internet-stack', 'global-routing', 'tap-net-device'])
2.9 + obj.source = 'tap-net-device.cc'
2.10 +
2.11 obj = bld.create_ns3_program('simple-alternate-routing',
2.12 ['point-to-point', 'internet-stack', 'global-routing'])
2.13 obj.source = 'simple-alternate-routing.cc'