1.1 --- a/examples/tap-net-device.cc Tue Jun 02 18:35:05 2009 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,278 +0,0 @@
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 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/examples/virtual-net-device.cc Tue Jun 09 17:42:57 2009 +0100
2.3 @@ -0,0 +1,278 @@
2.4 +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2.5 +/*
2.6 + * This program is free software; you can redistribute it and/or modify
2.7 + * it under the terms of the GNU General Public License version 2 as
2.8 + * published by the Free Software Foundation;
2.9 + *
2.10 + * This program is distributed in the hope that it will be useful,
2.11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.13 + * GNU General Public License for more details.
2.14 + *
2.15 + * You should have received a copy of the GNU General Public License
2.16 + * along with this program; if not, write to the Free Software
2.17 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2.18 + *
2.19 + * Based on simple-global-routing.cc
2.20 + * ns-2 simple.tcl script (ported from ns-2)
2.21 + * Originally authored by Steve McCanne, 12/19/1996
2.22 + */
2.23 +
2.24 +// Port of ns-2/tcl/ex/simple.tcl to ns-3
2.25 +//
2.26 +// Network topology
2.27 +//
2.28 +// n0
2.29 +// \ 5 Mb/s, 2ms
2.30 +// \ 1.5Mb/s, 10ms
2.31 +// n2 -------------------------n3
2.32 +// /
2.33 +// / 5 Mb/s, 2ms
2.34 +// n1
2.35 +//
2.36 +// - all links are point-to-point links with indicated one-way BW/delay
2.37 +// - CBR/UDP flows from n0 to n3, and from n3 to n1
2.38 +// - FTP/TCP flow from n0 to n3, starting at time 1.2 to time 1.35 sec.
2.39 +// - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec.
2.40 +// (i.e., DataRate of 448,000 bps)
2.41 +// - DropTail queues
2.42 +// - Tracing of queues and packet receptions to file "simple-global-routing.tr"
2.43 +
2.44 +// Tunneling changes (relative to the simple-global-routing example):
2.45 +// n0 will receive an extra virtual interface with address 11.0.0.1
2.46 +// n1 will also receive an extra virtual interface with the same address 11.0.0.1
2.47 +// n3 will receive an extra virtual interface with address 11.0.0.254
2.48 +// The flows will be between 11.0.0.x (tunnel) addresses instead of 10.1.x.y ones
2.49 +// n3 will decide, on a per-packet basis, via random number, whether to
2.50 +// send the packet to n0 or to n1.
2.51 +
2.52 +#include <iostream>
2.53 +#include <fstream>
2.54 +#include <string>
2.55 +#include <cassert>
2.56 +
2.57 +#include "ns3/core-module.h"
2.58 +#include "ns3/simulator-module.h"
2.59 +#include "ns3/node-module.h"
2.60 +#include "ns3/helper-module.h"
2.61 +#include "ns3/global-route-manager.h"
2.62 +#include "ns3/virtual-net-device.h"
2.63 +
2.64 +using namespace ns3;
2.65 +
2.66 +NS_LOG_COMPONENT_DEFINE ("VirtualNetDeviceExample");
2.67 +
2.68 +class Tunnel
2.69 +{
2.70 + Ptr<Socket> m_n3Socket;
2.71 + Ptr<Socket> m_n0Socket;
2.72 + Ptr<Socket> m_n1Socket;
2.73 + Ipv4Address m_n3Address;
2.74 + Ipv4Address m_n0Address;
2.75 + Ipv4Address m_n1Address;
2.76 + UniformVariable m_rng;
2.77 + Ptr<VirtualNetDevice> m_n0Tap;
2.78 + Ptr<VirtualNetDevice> m_n1Tap;
2.79 + Ptr<VirtualNetDevice> m_n3Tap;
2.80 +
2.81 +
2.82 + bool
2.83 + N0N1VirtualSend (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
2.84 + {
2.85 + m_n3Socket->SendTo (packet, 0, InetSocketAddress (m_n3Address, 667));
2.86 + return true;
2.87 + }
2.88 +
2.89 + bool
2.90 + N3VirtualSend (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
2.91 + {
2.92 +
2.93 + if (m_rng.GetValue () < 0.25)
2.94 + {
2.95 + m_n0Socket->SendTo (packet, 0, InetSocketAddress (m_n0Address, 667));
2.96 + }
2.97 + else
2.98 + {
2.99 + m_n1Socket->SendTo (packet, 0, InetSocketAddress (m_n1Address, 667));
2.100 + }
2.101 + return true;
2.102 + }
2.103 +
2.104 + void N3SocketRecv (Ptr<Socket> socket)
2.105 + {
2.106 + Ptr<Packet> packet = socket->Recv (65535, 0);
2.107 + m_n3Tap->Receive (packet, 0x0800, Address ());
2.108 + }
2.109 +
2.110 + void N0SocketRecv (Ptr<Socket> socket)
2.111 + {
2.112 + Ptr<Packet> packet = socket->Recv (65535, 0);
2.113 + m_n0Tap->Receive (packet, 0x0800, Address ());
2.114 + }
2.115 +
2.116 + void N1SocketRecv (Ptr<Socket> socket)
2.117 + {
2.118 + Ptr<Packet> packet = socket->Recv (65535, 0);
2.119 + m_n0Tap->Receive (packet, 0x0800, Address ());
2.120 + }
2.121 +
2.122 +public:
2.123 +
2.124 + Tunnel (Ptr<Node> n3, Ptr<Node> n0, Ptr<Node> n1,
2.125 + Ipv4Address n3Addr, Ipv4Address n0Addr, Ipv4Address n1Addr)
2.126 + : m_n3Address (n3Addr), m_n0Address (n0Addr), m_n1Address (n1Addr)
2.127 + {
2.128 + m_n3Socket = Socket::CreateSocket (n3, TypeId::LookupByName ("ns3::UdpSocketFactory"));
2.129 + m_n3Socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), 667));
2.130 + m_n3Socket->SetRecvCallback (MakeCallback (&Tunnel::N3SocketRecv, this));
2.131 +
2.132 + m_n0Socket = Socket::CreateSocket (n0, TypeId::LookupByName ("ns3::UdpSocketFactory"));
2.133 + m_n0Socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), 667));
2.134 + m_n0Socket->SetRecvCallback (MakeCallback (&Tunnel::N0SocketRecv, this));
2.135 +
2.136 + m_n1Socket = Socket::CreateSocket (n1, TypeId::LookupByName ("ns3::UdpSocketFactory"));
2.137 + m_n1Socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), 667));
2.138 + m_n1Socket->SetRecvCallback (MakeCallback (&Tunnel::N1SocketRecv, this));
2.139 +
2.140 + // n0 tap device
2.141 + m_n0Tap = CreateObject<VirtualNetDevice> ();
2.142 + m_n0Tap->SetSendFromCallback (MakeCallback (&Tunnel::N0N1VirtualSend, this));
2.143 + n0->AddDevice (m_n0Tap);
2.144 + Ptr<Ipv4> ipv4 = n0->GetObject<Ipv4> ();
2.145 + uint32_t i = ipv4->AddInterface (m_n0Tap);
2.146 + ipv4->AddAddress (i, Ipv4InterfaceAddress (Ipv4Address ("11.0.0.1"), Ipv4Mask ("255.255.255.0")));
2.147 + ipv4->SetUp (i);
2.148 +
2.149 + // n1 tap device
2.150 + m_n1Tap = CreateObject<VirtualNetDevice> ();
2.151 + m_n1Tap->SetSendFromCallback (MakeCallback (&Tunnel::N0N1VirtualSend, this));
2.152 + n1->AddDevice (m_n1Tap);
2.153 + ipv4 = n1->GetObject<Ipv4> ();
2.154 + i = ipv4->AddInterface (m_n1Tap);
2.155 + ipv4->AddAddress (i, Ipv4InterfaceAddress (Ipv4Address ("11.0.0.1"), Ipv4Mask ("255.255.255.0")));
2.156 + ipv4->SetUp (i);
2.157 +
2.158 + // n3 tap device
2.159 + m_n3Tap = CreateObject<VirtualNetDevice> ();
2.160 + m_n3Tap->SetSendFromCallback (MakeCallback (&Tunnel::N3VirtualSend, this));
2.161 + n3->AddDevice (m_n3Tap);
2.162 + ipv4 = n3->GetObject<Ipv4> ();
2.163 + i = ipv4->AddInterface (m_n3Tap);
2.164 + ipv4->AddAddress (i, Ipv4InterfaceAddress (Ipv4Address ("11.0.0.254"), Ipv4Mask ("255.255.255.0")));
2.165 + ipv4->SetUp (i);
2.166 +
2.167 + }
2.168 +
2.169 +
2.170 +};
2.171 +
2.172 +
2.173 +
2.174 +int
2.175 +main (int argc, char *argv[])
2.176 +{
2.177 + // Users may find it convenient to turn on explicit debugging
2.178 + // for selected modules; the below lines suggest how to do this
2.179 +#if 0
2.180 + LogComponentEnable ("SimpleGlobalRoutingExample", LOG_LEVEL_INFO);
2.181 +#endif
2.182 +
2.183 + // Set up some default values for the simulation. Use the
2.184 + Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (210));
2.185 + Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("448kb/s"));
2.186 +
2.187 + //DefaultValue::Bind ("DropTailQueue::m_maxPackets", 30);
2.188 +
2.189 + // Allow the user to override any of the defaults and the above
2.190 + // DefaultValue::Bind ()s at run-time, via command-line arguments
2.191 + CommandLine cmd;
2.192 + cmd.Parse (argc, argv);
2.193 +
2.194 + // Here, we will explicitly create four nodes. In more sophisticated
2.195 + // topologies, we could configure a node factory.
2.196 + NS_LOG_INFO ("Create nodes.");
2.197 + NodeContainer c;
2.198 + c.Create (4);
2.199 + NodeContainer n0n2 = NodeContainer (c.Get(0), c.Get (2));
2.200 + NodeContainer n1n2 = NodeContainer (c.Get(1), c.Get (2));
2.201 + NodeContainer n3n2 = NodeContainer (c.Get(3), c.Get (2));
2.202 +
2.203 + InternetStackHelper internet;
2.204 + internet.Install (c);
2.205 +
2.206 + // We create the channels first without any IP addressing information
2.207 + NS_LOG_INFO ("Create channels.");
2.208 + PointToPointHelper p2p;
2.209 + p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
2.210 + p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
2.211 + NetDeviceContainer d0d2 = p2p.Install (n0n2);
2.212 +
2.213 + NetDeviceContainer d1d2 = p2p.Install (n1n2);
2.214 +
2.215 + p2p.SetDeviceAttribute ("DataRate", StringValue ("1500kbps"));
2.216 + p2p.SetChannelAttribute ("Delay", StringValue ("10ms"));
2.217 + NetDeviceContainer d3d2 = p2p.Install (n3n2);
2.218 +
2.219 + // Later, we add IP addresses.
2.220 + NS_LOG_INFO ("Assign IP Addresses.");
2.221 + Ipv4AddressHelper ipv4;
2.222 + ipv4.SetBase ("10.1.1.0", "255.255.255.0");
2.223 + Ipv4InterfaceContainer i0i2 = ipv4.Assign (d0d2);
2.224 +
2.225 + ipv4.SetBase ("10.1.2.0", "255.255.255.0");
2.226 + Ipv4InterfaceContainer i1i2 = ipv4.Assign (d1d2);
2.227 +
2.228 + ipv4.SetBase ("10.1.3.0", "255.255.255.0");
2.229 + Ipv4InterfaceContainer i3i2 = ipv4.Assign (d3d2);
2.230 +
2.231 + // Create router nodes, initialize routing database and set up the routing
2.232 + // tables in the nodes.
2.233 + GlobalRouteManager::PopulateRoutingTables ();
2.234 +
2.235 + // Add the tunnels
2.236 + Tunnel tunnel (c.Get (3), c.Get (0), c.Get (1),
2.237 + i3i2.GetAddress (0), i0i2.GetAddress (0), i1i2.GetAddress (0));
2.238 +
2.239 + // Create the OnOff application to send UDP datagrams of size
2.240 + // 210 bytes at a rate of 448 Kb/s
2.241 + NS_LOG_INFO ("Create Applications.");
2.242 + uint16_t port = 9; // Discard port (RFC 863)
2.243 + OnOffHelper onoff ("ns3::UdpSocketFactory",
2.244 + Address (InetSocketAddress (Ipv4Address ("11.0.0.254"), port)));
2.245 + onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
2.246 + onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
2.247 + ApplicationContainer apps = onoff.Install (c.Get (0));
2.248 + apps.Start (Seconds (1.0));
2.249 + apps.Stop (Seconds (10.0));
2.250 +
2.251 + // Create a packet sink to receive these packets
2.252 + PacketSinkHelper sink ("ns3::UdpSocketFactory",
2.253 + Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
2.254 + apps = sink.Install (c.Get (3));
2.255 + apps.Start (Seconds (1.0));
2.256 + apps.Stop (Seconds (10.0));
2.257 +
2.258 + // Create a similar flow from n3 to n1, starting at time 1.1 seconds
2.259 + onoff.SetAttribute ("Remote",
2.260 + AddressValue (InetSocketAddress (Ipv4Address ("11.0.0.2"), port)));
2.261 + apps = onoff.Install (c.Get (3));
2.262 + apps.Start (Seconds (1.1));
2.263 + apps.Stop (Seconds (10.0));
2.264 +
2.265 + // Create a packet sink to receive these packets
2.266 + apps = sink.Install (c.Get (1));
2.267 + apps.Start (Seconds (1.1));
2.268 + apps.Stop (Seconds (10.0));
2.269 +
2.270 + std::ofstream ascii;
2.271 + ascii.open ("tap-net-device.tr");
2.272 + PointToPointHelper::EnablePcapAll ("tap-net-device");
2.273 + PointToPointHelper::EnableAsciiAll (ascii);
2.274 +
2.275 + NS_LOG_INFO ("Run Simulation.");
2.276 + Simulator::Run ();
2.277 + Simulator::Destroy ();
2.278 + NS_LOG_INFO ("Done.");
2.279 +
2.280 + return 0;
2.281 +}
3.1 --- a/examples/wscript Tue Jun 02 18:35:05 2009 +0100
3.2 +++ b/examples/wscript Tue Jun 09 17:42:57 2009 +0100
3.3 @@ -40,9 +40,9 @@
3.4 ['point-to-point', 'internet-stack', 'global-routing'])
3.5 obj.source = 'simple-global-routing.cc'
3.6
3.7 - obj = bld.create_ns3_program('tap-net-device',
3.8 - ['point-to-point', 'internet-stack', 'global-routing', 'tap-net-device'])
3.9 - obj.source = 'tap-net-device.cc'
3.10 + obj = bld.create_ns3_program('virtual-net-device',
3.11 + ['point-to-point', 'internet-stack', 'global-routing', 'virtual-net-device'])
3.12 + obj.source = 'virtual-net-device.cc'
3.13
3.14 obj = bld.create_ns3_program('simple-alternate-routing',
3.15 ['point-to-point', 'internet-stack', 'global-routing'])
4.1 --- a/src/devices/tap-net-device/tap-net-device.cc Tue Jun 02 18:35:05 2009 +0100
4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
4.3 @@ -1,258 +0,0 @@
4.4 -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
4.5 -/*
4.6 - * Copyright (c) 2008,2009 INESC Porto
4.7 - *
4.8 - * This program is free software; you can redistribute it and/or modify
4.9 - * it under the terms of the GNU General Public License version 2 as
4.10 - * published by the Free Software Foundation;
4.11 - *
4.12 - * This program is distributed in the hope that it will be useful,
4.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
4.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.15 - * GNU General Public License for more details.
4.16 - *
4.17 - * You should have received a copy of the GNU General Public License
4.18 - * along with this program; if not, write to the Free Software
4.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
4.20 - *
4.21 - * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
4.22 - */
4.23 -
4.24 -#include "ns3/log.h"
4.25 -#include "ns3/queue.h"
4.26 -#include "ns3/simulator.h"
4.27 -#include "ns3/mac48-address.h"
4.28 -#include "ns3/llc-snap-header.h"
4.29 -#include "ns3/error-model.h"
4.30 -#include "tap-net-device.h"
4.31 -#include "ns3/channel.h"
4.32 -#include "ns3/trace-source-accessor.h"
4.33 -
4.34 -
4.35 -NS_LOG_COMPONENT_DEFINE ("TapNetDevice");
4.36 -
4.37 -namespace ns3 {
4.38 -
4.39 -NS_OBJECT_ENSURE_REGISTERED (TapNetDevice);
4.40 -
4.41 -TypeId
4.42 -TapNetDevice::GetTypeId (void)
4.43 -{
4.44 - static TypeId tid = TypeId ("ns3::TapNetDevice")
4.45 - .SetParent<NetDevice> ()
4.46 - .AddConstructor<TapNetDevice> ()
4.47 - .AddTraceSource ("Rx", "Received payload from the MAC layer.",
4.48 - MakeTraceSourceAccessor (&TapNetDevice::m_rxTrace))
4.49 - .AddTraceSource ("Tx", "Send payload to the MAC layer.",
4.50 - MakeTraceSourceAccessor (&TapNetDevice::m_txTrace))
4.51 - ;
4.52 - return tid;
4.53 -}
4.54 -
4.55 -TapNetDevice::TapNetDevice ()
4.56 -{
4.57 - m_needsArp = false;
4.58 - m_supportsSendFrom = true;
4.59 - m_mtu = 65535;
4.60 -}
4.61 -
4.62 -
4.63 -void
4.64 -TapNetDevice::SetSendFromCallback (SendFromCallback sendCb)
4.65 -{
4.66 - m_sendCb = sendCb;
4.67 -}
4.68 -
4.69 -void
4.70 -TapNetDevice::SetNeedsArp (bool needsArp)
4.71 -{
4.72 - m_needsArp = needsArp;
4.73 -}
4.74 -
4.75 -void
4.76 -TapNetDevice::SetSupportsSendFrom (bool supportsSendFrom)
4.77 -{
4.78 - m_supportsSendFrom = supportsSendFrom;
4.79 -}
4.80 -
4.81 -bool
4.82 -TapNetDevice::SetMtu (const uint16_t mtu)
4.83 -{
4.84 - m_mtu = mtu;
4.85 - return true;
4.86 -}
4.87 -
4.88 -
4.89 -TapNetDevice::~TapNetDevice()
4.90 -{
4.91 - NS_LOG_FUNCTION_NOARGS ();
4.92 -}
4.93 -
4.94 -
4.95 -void TapNetDevice::DoDispose()
4.96 -{
4.97 - NS_LOG_FUNCTION_NOARGS ();
4.98 - NetDevice::DoDispose ();
4.99 -}
4.100 -
4.101 -bool
4.102 -TapNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol, const Address &address)
4.103 -{
4.104 - if (m_rxCallback (this, packet, protocol, address))
4.105 - {
4.106 - m_rxTrace (packet);
4.107 - return true;
4.108 - }
4.109 - return false;
4.110 -}
4.111 -
4.112 -bool
4.113 -TapNetDevice::PromiscReceive (Ptr<Packet> packet, uint16_t protocol,
4.114 - const Address &source, const Address &destination,
4.115 - PacketType packetType)
4.116 -{
4.117 - if (m_promiscRxCallback (this, packet, protocol, source, destination, packetType))
4.118 - {
4.119 - m_rxTrace (packet);
4.120 - return true;
4.121 - }
4.122 - return false;
4.123 -}
4.124 -
4.125 -
4.126 -void
4.127 -TapNetDevice::SetIfIndex (const uint32_t index)
4.128 -{
4.129 - m_index = index;
4.130 -}
4.131 -
4.132 -uint32_t
4.133 -TapNetDevice::GetIfIndex (void) const
4.134 -{
4.135 - return m_index;
4.136 -}
4.137 -
4.138 -Ptr<Channel>
4.139 -TapNetDevice::GetChannel (void) const
4.140 -{
4.141 - return Ptr<Channel> ();
4.142 -}
4.143 -
4.144 -Address
4.145 -TapNetDevice::GetAddress (void) const
4.146 -{
4.147 - return Mac48Address ();
4.148 -}
4.149 -
4.150 -uint16_t
4.151 -TapNetDevice::GetMtu (void) const
4.152 -{
4.153 - return m_mtu;
4.154 -}
4.155 -
4.156 -bool
4.157 -TapNetDevice::IsLinkUp (void) const
4.158 -{
4.159 - return true;
4.160 -}
4.161 -
4.162 -void
4.163 -TapNetDevice::SetLinkChangeCallback (Callback<void> callback)
4.164 -{
4.165 -}
4.166 -
4.167 -bool
4.168 -TapNetDevice::IsBroadcast (void) const
4.169 -{
4.170 - return true;
4.171 -}
4.172 -
4.173 -Address
4.174 -TapNetDevice::GetBroadcast (void) const
4.175 -{
4.176 - return Mac48Address ("ff:ff:ff:ff:ff:ff");
4.177 -}
4.178 -
4.179 -bool
4.180 -TapNetDevice::IsMulticast (void) const
4.181 -{
4.182 - return false;
4.183 -}
4.184 -
4.185 -Address TapNetDevice::GetMulticast (Ipv4Address multicastGroup) const
4.186 -{
4.187 - return Mac48Address ("ff:ff:ff:ff:ff:ff");
4.188 -}
4.189 -
4.190 -Address TapNetDevice::GetMulticast (Ipv6Address addr) const
4.191 -{
4.192 - return Mac48Address ("ff:ff:ff:ff:ff:ff");
4.193 -}
4.194 -
4.195 -
4.196 -bool
4.197 -TapNetDevice::IsPointToPoint (void) const
4.198 -{
4.199 - return true;
4.200 -}
4.201 -
4.202 -bool
4.203 -TapNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
4.204 -{
4.205 - return SendFrom (packet, GetAddress (), dest, protocolNumber);
4.206 -}
4.207 -
4.208 -bool
4.209 -TapNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
4.210 -{
4.211 - if (m_sendCb (packet, source, dest, protocolNumber))
4.212 - {
4.213 - m_txTrace (packet);
4.214 - return true;
4.215 - }
4.216 - return false;
4.217 -}
4.218 -
4.219 -Ptr<Node>
4.220 -TapNetDevice::GetNode (void) const
4.221 -{
4.222 - return m_node;
4.223 -}
4.224 -
4.225 -void
4.226 -TapNetDevice::SetNode (Ptr<Node> node)
4.227 -{
4.228 - m_node = node;
4.229 -}
4.230 -
4.231 -bool
4.232 -TapNetDevice::NeedsArp (void) const
4.233 -{
4.234 - return m_needsArp;
4.235 -}
4.236 -
4.237 -void
4.238 -TapNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
4.239 -{
4.240 - m_rxCallback = cb;
4.241 -}
4.242 -
4.243 -void
4.244 -TapNetDevice::SetPromiscReceiveCallback (NetDevice::PromiscReceiveCallback cb)
4.245 -{
4.246 - m_promiscRxCallback = cb;
4.247 -}
4.248 -
4.249 -bool
4.250 -TapNetDevice::SupportsSendFrom () const
4.251 -{
4.252 - return m_supportsSendFrom;
4.253 -}
4.254 -
4.255 -bool TapNetDevice::IsBridge (void) const
4.256 -{
4.257 - return false;
4.258 -}
4.259 -
4.260 -
4.261 -} // namespace ns3
5.1 --- a/src/devices/tap-net-device/tap-net-device.h Tue Jun 02 18:35:05 2009 +0100
5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
5.3 @@ -1,168 +0,0 @@
5.4 -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
5.5 -/*
5.6 - * Copyright (c) 2008,2009 INESC Porto
5.7 - *
5.8 - * This program is free software; you can redistribute it and/or modify
5.9 - * it under the terms of the GNU General Public License version 2 as
5.10 - * published by the Free Software Foundation;
5.11 - *
5.12 - * This program is distributed in the hope that it will be useful,
5.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
5.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5.15 - * GNU General Public License for more details.
5.16 - *
5.17 - * You should have received a copy of the GNU General Public License
5.18 - * along with this program; if not, write to the Free Software
5.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
5.20 - *
5.21 - * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
5.22 - */
5.23 -
5.24 -#ifndef TAP_NET_DEVICE_H
5.25 -#define TAP_NET_DEVICE_H
5.26 -
5.27 -#include "ns3/address.h"
5.28 -#include "ns3/node.h"
5.29 -#include "ns3/net-device.h"
5.30 -#include "ns3/callback.h"
5.31 -#include "ns3/packet.h"
5.32 -#include "ns3/ptr.h"
5.33 -#include "ns3/traced-callback.h"
5.34 -
5.35 -namespace ns3 {
5.36 -
5.37 -
5.38 -/**
5.39 - * \class TapNetDevice
5.40 - * \brief A virtual device, similar to Linux TAP interfaces.
5.41 - *
5.42 - * A TapNetDevice is a "virtual" NetDevice implementation which
5.43 - * delegates to a user callback (see method SetSendFromCallback()) the
5.44 - * task of actually transmitting a packet. It also allows the user
5.45 - * code to inject the packet as if it had been received by the
5.46 - * TapNetDevice. Together, these features allow one to build tunnels.
5.47 - * For instance, by transmitting packets into a UDP socket we end up
5.48 - * building an IP-over-UDP-over-IP tunnel.
5.49 - *
5.50 - * The same thing could be accomplished by subclassing NetDevice
5.51 - * directly. However, TapNetDevice is usually much simpler to program
5.52 - * than a NetDevice subclass.
5.53 - */
5.54 -class TapNetDevice : public NetDevice
5.55 -{
5.56 -public:
5.57 - /**
5.58 - * Callback the be invoked when the TapNetDevice is asked to queue/transmit a packet.
5.59 - * For more information, consult the documentation of NetDevice::SendFrom().
5.60 - */
5.61 - typedef Callback<bool, Ptr<Packet>, const Address&, const Address&, uint16_t> SendFromCallback;
5.62 -
5.63 - static TypeId GetTypeId (void);
5.64 - TapNetDevice ();
5.65 -
5.66 - virtual ~TapNetDevice ();
5.67 -
5.68 - /**
5.69 - * \brief Set the user callback to be called when a L2 packet is to be transmitted
5.70 - * \param transmitCb the new transmit callback
5.71 - */
5.72 - void SetSendFromCallback (SendFromCallback transmitCb);
5.73 -
5.74 - /**
5.75 - * \brief Configure whether the virtual device needs ARP
5.76 - *
5.77 - * \param needsArp the the 'needs arp' value that will be returned
5.78 - * by the NeedsArp() method. The method IsBroadcast() will also
5.79 - * return this value.
5.80 - */
5.81 - void SetNeedsArp (bool needsArp);
5.82 -
5.83 - /**
5.84 - * \brief Configure whether the virtual device supports SendFrom
5.85 - */
5.86 - void SetSupportsSendFrom (bool supportsSendFrom);
5.87 -
5.88 - /**
5.89 - * \brief Configure the reported MTU for the virtual device. The
5.90 - * default value is 65535.
5.91 - * \return whether the MTU value was within legal bounds
5.92 - */
5.93 - bool SetMtu (const uint16_t mtu);
5.94 -
5.95 -
5.96 - /**
5.97 - * \param packet packet sent from below up to Network Device
5.98 - * \param protocol Protocol type
5.99 - * \param source the address of the sender of this packet.
5.100 - * \returns true if the packet was forwarded successfully,
5.101 - * false otherwise.
5.102 - *
5.103 - * Forward a "virtually received" packet up the node's protocol
5.104 - * stack.
5.105 - */
5.106 - bool Receive (Ptr<Packet> packet, uint16_t protocol, const Address &source);
5.107 -
5.108 -
5.109 - /**
5.110 - * \param packet packet sent from below up to Network Device
5.111 - * \param protocol Protocol type
5.112 - * \param source the address of the sender of this packet.
5.113 - * \param destination the address of the receiver of this packet.
5.114 - * \param packetType type of packet received (broadcast/multicast/unicast/otherhost)
5.115 - * \returns true if the packet was forwarded successfully, false otherwise.
5.116 - *
5.117 - * Forward a "virtually received (in promiscuous mode)" packet up
5.118 - * the node's protocol stack.
5.119 - */
5.120 - bool PromiscReceive (Ptr<Packet> packet, uint16_t protocol,
5.121 - const Address &source, const Address &destination,
5.122 - PacketType packetType);
5.123 -
5.124 -
5.125 - // inherited from NetDevice base class.
5.126 - virtual void SetIfIndex(const uint32_t index);
5.127 - virtual uint32_t GetIfIndex(void) const;
5.128 - virtual Ptr<Channel> GetChannel (void) const;
5.129 - virtual Address GetAddress (void) const;
5.130 - virtual uint16_t GetMtu (void) const;
5.131 - virtual bool IsLinkUp (void) const;
5.132 - virtual void SetLinkChangeCallback (Callback<void> callback);
5.133 - virtual bool IsBroadcast (void) const;
5.134 - virtual Address GetBroadcast (void) const;
5.135 - virtual bool IsMulticast (void) const;
5.136 - virtual Address GetMulticast (Ipv4Address multicastGroup) const;
5.137 - virtual Address GetMulticast (Ipv6Address addr) const;
5.138 - virtual bool IsPointToPoint (void) const;
5.139 - virtual bool Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber);
5.140 - virtual bool SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
5.141 - virtual Ptr<Node> GetNode (void) const;
5.142 - virtual void SetNode (Ptr<Node> node);
5.143 - virtual bool NeedsArp (void) const;
5.144 - virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb);
5.145 - virtual void SetPromiscReceiveCallback (NetDevice::PromiscReceiveCallback cb);
5.146 - virtual bool SupportsSendFrom () const;
5.147 - virtual bool IsBridge (void) const;
5.148 -
5.149 -protected:
5.150 -
5.151 - virtual void DoDispose (void);
5.152 -
5.153 -private:
5.154 -
5.155 - SendFromCallback m_sendCb;
5.156 - TracedCallback<Ptr<const Packet> > m_rxTrace;
5.157 - TracedCallback<Ptr<const Packet> > m_txTrace;
5.158 - Ptr<Node> m_node;
5.159 - ReceiveCallback m_rxCallback;
5.160 - PromiscReceiveCallback m_promiscRxCallback;
5.161 - std::string m_name;
5.162 - uint32_t m_index;
5.163 - uint16_t m_mtu;
5.164 - bool m_needsArp;
5.165 - bool m_supportsSendFrom;
5.166 -};
5.167 -
5.168 -}; // namespace ns3
5.169 -
5.170 -#endif
5.171 -
6.1 --- a/src/devices/tap-net-device/waf Tue Jun 02 18:35:05 2009 +0100
6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
6.3 @@ -1,1 +0,0 @@
6.4 -exec "`dirname "$0"`"/../../../waf "$@"
7.1 --- a/src/devices/tap-net-device/wscript Tue Jun 02 18:35:05 2009 +0100
7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
7.3 @@ -1,14 +0,0 @@
7.4 -## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
7.5 -
7.6 -
7.7 -def build(bld):
7.8 - module = bld.create_ns3_module('tap-net-device', ['node'])
7.9 - module.source = [
7.10 - 'tap-net-device.cc',
7.11 - ]
7.12 - headers = bld.new_task_gen('ns3header')
7.13 - headers.module = 'tap-net-device'
7.14 - headers.source = [
7.15 - 'tap-net-device.h',
7.16 - ]
7.17 -
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/src/devices/virtual-net-device/virtual-net-device.cc Tue Jun 09 17:42:57 2009 +0100
8.3 @@ -0,0 +1,258 @@
8.4 +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
8.5 +/*
8.6 + * Copyright (c) 2008,2009 INESC Porto
8.7 + *
8.8 + * This program is free software; you can redistribute it and/or modify
8.9 + * it under the terms of the GNU General Public License version 2 as
8.10 + * published by the Free Software Foundation;
8.11 + *
8.12 + * This program is distributed in the hope that it will be useful,
8.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8.15 + * GNU General Public License for more details.
8.16 + *
8.17 + * You should have received a copy of the GNU General Public License
8.18 + * along with this program; if not, write to the Free Software
8.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
8.20 + *
8.21 + * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
8.22 + */
8.23 +
8.24 +#include "ns3/log.h"
8.25 +#include "ns3/queue.h"
8.26 +#include "ns3/simulator.h"
8.27 +#include "ns3/mac48-address.h"
8.28 +#include "ns3/llc-snap-header.h"
8.29 +#include "ns3/error-model.h"
8.30 +#include "virtual-net-device.h"
8.31 +#include "ns3/channel.h"
8.32 +#include "ns3/trace-source-accessor.h"
8.33 +
8.34 +
8.35 +NS_LOG_COMPONENT_DEFINE ("VirtualNetDevice");
8.36 +
8.37 +namespace ns3 {
8.38 +
8.39 +NS_OBJECT_ENSURE_REGISTERED (VirtualNetDevice);
8.40 +
8.41 +TypeId
8.42 +VirtualNetDevice::GetTypeId (void)
8.43 +{
8.44 + static TypeId tid = TypeId ("ns3::VirtualNetDevice")
8.45 + .SetParent<NetDevice> ()
8.46 + .AddConstructor<VirtualNetDevice> ()
8.47 + .AddTraceSource ("Rx", "Received payload from the MAC layer.",
8.48 + MakeTraceSourceAccessor (&VirtualNetDevice::m_rxTrace))
8.49 + .AddTraceSource ("Tx", "Send payload to the MAC layer.",
8.50 + MakeTraceSourceAccessor (&VirtualNetDevice::m_txTrace))
8.51 + ;
8.52 + return tid;
8.53 +}
8.54 +
8.55 +VirtualNetDevice::VirtualNetDevice ()
8.56 +{
8.57 + m_needsArp = false;
8.58 + m_supportsSendFrom = true;
8.59 + m_mtu = 65535;
8.60 +}
8.61 +
8.62 +
8.63 +void
8.64 +VirtualNetDevice::SetSendFromCallback (SendFromCallback sendCb)
8.65 +{
8.66 + m_sendCb = sendCb;
8.67 +}
8.68 +
8.69 +void
8.70 +VirtualNetDevice::SetNeedsArp (bool needsArp)
8.71 +{
8.72 + m_needsArp = needsArp;
8.73 +}
8.74 +
8.75 +void
8.76 +VirtualNetDevice::SetSupportsSendFrom (bool supportsSendFrom)
8.77 +{
8.78 + m_supportsSendFrom = supportsSendFrom;
8.79 +}
8.80 +
8.81 +bool
8.82 +VirtualNetDevice::SetMtu (const uint16_t mtu)
8.83 +{
8.84 + m_mtu = mtu;
8.85 + return true;
8.86 +}
8.87 +
8.88 +
8.89 +VirtualNetDevice::~VirtualNetDevice()
8.90 +{
8.91 + NS_LOG_FUNCTION_NOARGS ();
8.92 +}
8.93 +
8.94 +
8.95 +void VirtualNetDevice::DoDispose()
8.96 +{
8.97 + NS_LOG_FUNCTION_NOARGS ();
8.98 + NetDevice::DoDispose ();
8.99 +}
8.100 +
8.101 +bool
8.102 +VirtualNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol, const Address &address)
8.103 +{
8.104 + if (m_rxCallback (this, packet, protocol, address))
8.105 + {
8.106 + m_rxTrace (packet);
8.107 + return true;
8.108 + }
8.109 + return false;
8.110 +}
8.111 +
8.112 +bool
8.113 +VirtualNetDevice::PromiscReceive (Ptr<Packet> packet, uint16_t protocol,
8.114 + const Address &source, const Address &destination,
8.115 + PacketType packetType)
8.116 +{
8.117 + if (m_promiscRxCallback (this, packet, protocol, source, destination, packetType))
8.118 + {
8.119 + m_rxTrace (packet);
8.120 + return true;
8.121 + }
8.122 + return false;
8.123 +}
8.124 +
8.125 +
8.126 +void
8.127 +VirtualNetDevice::SetIfIndex (const uint32_t index)
8.128 +{
8.129 + m_index = index;
8.130 +}
8.131 +
8.132 +uint32_t
8.133 +VirtualNetDevice::GetIfIndex (void) const
8.134 +{
8.135 + return m_index;
8.136 +}
8.137 +
8.138 +Ptr<Channel>
8.139 +VirtualNetDevice::GetChannel (void) const
8.140 +{
8.141 + return Ptr<Channel> ();
8.142 +}
8.143 +
8.144 +Address
8.145 +VirtualNetDevice::GetAddress (void) const
8.146 +{
8.147 + return Mac48Address ();
8.148 +}
8.149 +
8.150 +uint16_t
8.151 +VirtualNetDevice::GetMtu (void) const
8.152 +{
8.153 + return m_mtu;
8.154 +}
8.155 +
8.156 +bool
8.157 +VirtualNetDevice::IsLinkUp (void) const
8.158 +{
8.159 + return true;
8.160 +}
8.161 +
8.162 +void
8.163 +VirtualNetDevice::SetLinkChangeCallback (Callback<void> callback)
8.164 +{
8.165 +}
8.166 +
8.167 +bool
8.168 +VirtualNetDevice::IsBroadcast (void) const
8.169 +{
8.170 + return true;
8.171 +}
8.172 +
8.173 +Address
8.174 +VirtualNetDevice::GetBroadcast (void) const
8.175 +{
8.176 + return Mac48Address ("ff:ff:ff:ff:ff:ff");
8.177 +}
8.178 +
8.179 +bool
8.180 +VirtualNetDevice::IsMulticast (void) const
8.181 +{
8.182 + return false;
8.183 +}
8.184 +
8.185 +Address VirtualNetDevice::GetMulticast (Ipv4Address multicastGroup) const
8.186 +{
8.187 + return Mac48Address ("ff:ff:ff:ff:ff:ff");
8.188 +}
8.189 +
8.190 +Address VirtualNetDevice::GetMulticast (Ipv6Address addr) const
8.191 +{
8.192 + return Mac48Address ("ff:ff:ff:ff:ff:ff");
8.193 +}
8.194 +
8.195 +
8.196 +bool
8.197 +VirtualNetDevice::IsPointToPoint (void) const
8.198 +{
8.199 + return true;
8.200 +}
8.201 +
8.202 +bool
8.203 +VirtualNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
8.204 +{
8.205 + return SendFrom (packet, GetAddress (), dest, protocolNumber);
8.206 +}
8.207 +
8.208 +bool
8.209 +VirtualNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
8.210 +{
8.211 + if (m_sendCb (packet, source, dest, protocolNumber))
8.212 + {
8.213 + m_txTrace (packet);
8.214 + return true;
8.215 + }
8.216 + return false;
8.217 +}
8.218 +
8.219 +Ptr<Node>
8.220 +VirtualNetDevice::GetNode (void) const
8.221 +{
8.222 + return m_node;
8.223 +}
8.224 +
8.225 +void
8.226 +VirtualNetDevice::SetNode (Ptr<Node> node)
8.227 +{
8.228 + m_node = node;
8.229 +}
8.230 +
8.231 +bool
8.232 +VirtualNetDevice::NeedsArp (void) const
8.233 +{
8.234 + return m_needsArp;
8.235 +}
8.236 +
8.237 +void
8.238 +VirtualNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
8.239 +{
8.240 + m_rxCallback = cb;
8.241 +}
8.242 +
8.243 +void
8.244 +VirtualNetDevice::SetPromiscReceiveCallback (NetDevice::PromiscReceiveCallback cb)
8.245 +{
8.246 + m_promiscRxCallback = cb;
8.247 +}
8.248 +
8.249 +bool
8.250 +VirtualNetDevice::SupportsSendFrom () const
8.251 +{
8.252 + return m_supportsSendFrom;
8.253 +}
8.254 +
8.255 +bool VirtualNetDevice::IsBridge (void) const
8.256 +{
8.257 + return false;
8.258 +}
8.259 +
8.260 +
8.261 +} // namespace ns3
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/src/devices/virtual-net-device/virtual-net-device.h Tue Jun 09 17:42:57 2009 +0100
9.3 @@ -0,0 +1,168 @@
9.4 +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
9.5 +/*
9.6 + * Copyright (c) 2008,2009 INESC Porto
9.7 + *
9.8 + * This program is free software; you can redistribute it and/or modify
9.9 + * it under the terms of the GNU General Public License version 2 as
9.10 + * published by the Free Software Foundation;
9.11 + *
9.12 + * This program is distributed in the hope that it will be useful,
9.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9.15 + * GNU General Public License for more details.
9.16 + *
9.17 + * You should have received a copy of the GNU General Public License
9.18 + * along with this program; if not, write to the Free Software
9.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
9.20 + *
9.21 + * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
9.22 + */
9.23 +
9.24 +#ifndef VIRTUAL_NET_DEVICE_H
9.25 +#define VIRTUAL_NET_DEVICE_H
9.26 +
9.27 +#include "ns3/address.h"
9.28 +#include "ns3/node.h"
9.29 +#include "ns3/net-device.h"
9.30 +#include "ns3/callback.h"
9.31 +#include "ns3/packet.h"
9.32 +#include "ns3/ptr.h"
9.33 +#include "ns3/traced-callback.h"
9.34 +
9.35 +namespace ns3 {
9.36 +
9.37 +
9.38 +/**
9.39 + * \class VirtualNetDevice
9.40 + * \brief A virtual device, similar to Linux TAP interfaces.
9.41 + *
9.42 + * A VirtualNetDevice is a "virtual" NetDevice implementation which
9.43 + * delegates to a user callback (see method SetSendFromCallback()) the
9.44 + * task of actually transmitting a packet. It also allows the user
9.45 + * code to inject the packet as if it had been received by the
9.46 + * VirtualNetDevice. Together, these features allow one to build tunnels.
9.47 + * For instance, by transmitting packets into a UDP socket we end up
9.48 + * building an IP-over-UDP-over-IP tunnel.
9.49 + *
9.50 + * The same thing could be accomplished by subclassing NetDevice
9.51 + * directly. However, VirtualNetDevice is usually much simpler to program
9.52 + * than a NetDevice subclass.
9.53 + */
9.54 +class VirtualNetDevice : public NetDevice
9.55 +{
9.56 +public:
9.57 + /**
9.58 + * Callback the be invoked when the VirtualNetDevice is asked to queue/transmit a packet.
9.59 + * For more information, consult the documentation of NetDevice::SendFrom().
9.60 + */
9.61 + typedef Callback<bool, Ptr<Packet>, const Address&, const Address&, uint16_t> SendFromCallback;
9.62 +
9.63 + static TypeId GetTypeId (void);
9.64 + VirtualNetDevice ();
9.65 +
9.66 + virtual ~VirtualNetDevice ();
9.67 +
9.68 + /**
9.69 + * \brief Set the user callback to be called when a L2 packet is to be transmitted
9.70 + * \param transmitCb the new transmit callback
9.71 + */
9.72 + void SetSendFromCallback (SendFromCallback transmitCb);
9.73 +
9.74 + /**
9.75 + * \brief Configure whether the virtual device needs ARP
9.76 + *
9.77 + * \param needsArp the the 'needs arp' value that will be returned
9.78 + * by the NeedsArp() method. The method IsBroadcast() will also
9.79 + * return this value.
9.80 + */
9.81 + void SetNeedsArp (bool needsArp);
9.82 +
9.83 + /**
9.84 + * \brief Configure whether the virtual device supports SendFrom
9.85 + */
9.86 + void SetSupportsSendFrom (bool supportsSendFrom);
9.87 +
9.88 + /**
9.89 + * \brief Configure the reported MTU for the virtual device. The
9.90 + * default value is 65535.
9.91 + * \return whether the MTU value was within legal bounds
9.92 + */
9.93 + bool SetMtu (const uint16_t mtu);
9.94 +
9.95 +
9.96 + /**
9.97 + * \param packet packet sent from below up to Network Device
9.98 + * \param protocol Protocol type
9.99 + * \param source the address of the sender of this packet.
9.100 + * \returns true if the packet was forwarded successfully,
9.101 + * false otherwise.
9.102 + *
9.103 + * Forward a "virtually received" packet up the node's protocol
9.104 + * stack.
9.105 + */
9.106 + bool Receive (Ptr<Packet> packet, uint16_t protocol, const Address &source);
9.107 +
9.108 +
9.109 + /**
9.110 + * \param packet packet sent from below up to Network Device
9.111 + * \param protocol Protocol type
9.112 + * \param source the address of the sender of this packet.
9.113 + * \param destination the address of the receiver of this packet.
9.114 + * \param packetType type of packet received (broadcast/multicast/unicast/otherhost)
9.115 + * \returns true if the packet was forwarded successfully, false otherwise.
9.116 + *
9.117 + * Forward a "virtually received (in promiscuous mode)" packet up
9.118 + * the node's protocol stack.
9.119 + */
9.120 + bool PromiscReceive (Ptr<Packet> packet, uint16_t protocol,
9.121 + const Address &source, const Address &destination,
9.122 + PacketType packetType);
9.123 +
9.124 +
9.125 + // inherited from NetDevice base class.
9.126 + virtual void SetIfIndex(const uint32_t index);
9.127 + virtual uint32_t GetIfIndex(void) const;
9.128 + virtual Ptr<Channel> GetChannel (void) const;
9.129 + virtual Address GetAddress (void) const;
9.130 + virtual uint16_t GetMtu (void) const;
9.131 + virtual bool IsLinkUp (void) const;
9.132 + virtual void SetLinkChangeCallback (Callback<void> callback);
9.133 + virtual bool IsBroadcast (void) const;
9.134 + virtual Address GetBroadcast (void) const;
9.135 + virtual bool IsMulticast (void) const;
9.136 + virtual Address GetMulticast (Ipv4Address multicastGroup) const;
9.137 + virtual Address GetMulticast (Ipv6Address addr) const;
9.138 + virtual bool IsPointToPoint (void) const;
9.139 + virtual bool Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber);
9.140 + virtual bool SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
9.141 + virtual Ptr<Node> GetNode (void) const;
9.142 + virtual void SetNode (Ptr<Node> node);
9.143 + virtual bool NeedsArp (void) const;
9.144 + virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb);
9.145 + virtual void SetPromiscReceiveCallback (NetDevice::PromiscReceiveCallback cb);
9.146 + virtual bool SupportsSendFrom () const;
9.147 + virtual bool IsBridge (void) const;
9.148 +
9.149 +protected:
9.150 +
9.151 + virtual void DoDispose (void);
9.152 +
9.153 +private:
9.154 +
9.155 + SendFromCallback m_sendCb;
9.156 + TracedCallback<Ptr<const Packet> > m_rxTrace;
9.157 + TracedCallback<Ptr<const Packet> > m_txTrace;
9.158 + Ptr<Node> m_node;
9.159 + ReceiveCallback m_rxCallback;
9.160 + PromiscReceiveCallback m_promiscRxCallback;
9.161 + std::string m_name;
9.162 + uint32_t m_index;
9.163 + uint16_t m_mtu;
9.164 + bool m_needsArp;
9.165 + bool m_supportsSendFrom;
9.166 +};
9.167 +
9.168 +}; // namespace ns3
9.169 +
9.170 +#endif
9.171 +
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/src/devices/virtual-net-device/waf Tue Jun 09 17:42:57 2009 +0100
10.3 @@ -0,0 +1,1 @@
10.4 +exec "`dirname "$0"`"/../../../waf "$@"
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/src/devices/virtual-net-device/wscript Tue Jun 09 17:42:57 2009 +0100
11.3 @@ -0,0 +1,14 @@
11.4 +## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
11.5 +
11.6 +
11.7 +def build(bld):
11.8 + module = bld.create_ns3_module('virtual-net-device', ['node'])
11.9 + module.source = [
11.10 + 'virtual-net-device.cc',
11.11 + ]
11.12 + headers = bld.new_task_gen('ns3header')
11.13 + headers.module = 'virtual-net-device'
11.14 + headers.source = [
11.15 + 'virtual-net-device.h',
11.16 + ]
11.17 +
12.1 --- a/src/wscript Tue Jun 02 18:35:05 2009 +0100
12.2 +++ b/src/wscript Tue Jun 09 17:42:57 2009 +0100
12.3 @@ -23,7 +23,7 @@
12.4 'devices/emu',
12.5 'devices/bridge',
12.6 'devices/tap-bridge',
12.7 - 'devices/tap-net-device',
12.8 + 'devices/virtual-net-device',
12.9 'applications/onoff',
12.10 'applications/packet-sink',
12.11 'applications/udp-echo',