IP Encapsulation

196 views
Skip to first unread message

John Fitz

unread,
Jan 7, 2011, 11:18:17 AM1/7/11
to ns-3-users
Hi All,

I am relatively new to NS-3 and just learning the ropes. I am
attempting to create a simple network of 4 nodes in a line, with the
two middle nodes connected via an IP Tunnel. Essentially the tunnel
bridges two CSMA networks via a P2P link. Using the tunneling
tutorial, I have successfully created the tunnel.

The problem is that in the tutorial the application binds directly to
the tunnel addresses. However in my scenario I would like all packets
received by the nodes at the tunnels endpoints to be forwarded over
the tunnel.
One solution I have tried is to add static routes to forward the
received packets on the tunnel endpoint to its tunnel interface, but
this does not seem to work.

Any help is greatly appreciated.

here is my code:



// Network topology
//
// UE1 UE2
// | |
// Femto ____GTP Tunnel____ Femto
// Cell GW
//
// - GTP Tunnel between Femto Cell and GW
// - P2P Links
// - Tracing of queues and packet receptions to file "voip-femto.tr"

#include <fstream>
#include "ns3/core-module.h"
#include "ns3/simulator-module.h"
#include "ns3/helper-module.h"
#include "ns3/rtp-header.h"

#include "ns3/common-module.h"
#include "ns3/node-module.h"
#include "ns3/global-route-manager.h"
#include "ns3/simulator-module.h"
#include "ns3/virtual-net-device.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FemtoExample");

class Tunnel
{
Ptr<Socket> m_fapSocket;
Ptr<Socket> m_fgwSocket;
Ipv4Address m_fapAddress;
Ipv4Address m_fgwAddress;
UniformVariable m_rng;
Ptr<VirtualNetDevice> m_fapTap;
Ptr<VirtualNetDevice> m_fgwTap;


bool
FAPVirtualSend (Ptr<Packet> packet, const Address& source, const
Address& dest, uint16_t protocolNumber)
{
NS_LOG_DEBUG ("Send to " << m_fgwAddress << ": " << *packet);
m_fapSocket->SendTo (packet, 0, InetSocketAddress (m_fgwAddress,
667));
return true;
}

bool
FGWVirtualSend (Ptr<Packet> packet, const Address& source, const
Address& dest, uint16_t protocolNumber)
{
NS_LOG_DEBUG ("Send to " << m_fapAddress << ": " << *packet);
m_fgwSocket->SendTo (packet, 0, InetSocketAddress (m_fapAddress,
667));
return true;
}

void FAPSocketRecv (Ptr<Socket> socket)
{
Ptr<Packet> packet = socket->Recv (65535, 0);
NS_LOG_DEBUG ("FAPSocketRecv: " << *packet);
SocketAddressTag socketAddressTag;
packet->RemovePacketTag (socketAddressTag);
m_fapTap->Receive (packet, 0x0800, m_fapTap->GetAddress (),
m_fapTap->GetAddress (), NetDevice::PACKET_HOST);
}

void FGWSocketRecv (Ptr<Socket> socket)
{
Ptr<Packet> packet = socket->Recv (65535, 0);
NS_LOG_DEBUG ("FGWSocketRecv: " << *packet);
SocketAddressTag socketAddressTag;
packet->RemovePacketTag (socketAddressTag);
m_fgwTap->Receive (packet, 0x0800, m_fgwTap->GetAddress (),
m_fgwTap->GetAddress (), NetDevice::PACKET_HOST);
}

public:

Tunnel (Ptr<Node> fap, Ptr<Node> fgw,
Ipv4Address fapAddr, Ipv4Address fgwAddr)
: m_fapAddress (fapAddr), m_fgwAddress (fgwAddr)
{
m_fapSocket = Socket::CreateSocket (fap, TypeId::LookupByName
("ns3::UdpSocketFactory"));
m_fapSocket->Bind (InetSocketAddress (Ipv4Address::GetAny (),
667));
m_fapSocket->SetRecvCallback (MakeCallback
(&Tunnel::FAPSocketRecv, this));

m_fgwSocket = Socket::CreateSocket (fgw, TypeId::LookupByName
("ns3::UdpSocketFactory"));
m_fgwSocket->Bind (InetSocketAddress (Ipv4Address::GetAny (),
667));
m_fgwSocket->SetRecvCallback (MakeCallback
(&Tunnel::FGWSocketRecv, this));

// fap tap device
m_fapTap = CreateObject<VirtualNetDevice> ();
m_fapTap->SetAddress (Mac48Address ("11:00:01:02:03:01"));
m_fapTap->SetSendCallback (MakeCallback (&Tunnel::FAPVirtualSend,
this));
fap->AddDevice (m_fapTap);
Ptr<Ipv4> ipv4 = fap->GetObject<Ipv4> ();
uint32_t i = ipv4->AddInterface (m_fapTap);
ipv4->AddAddress (i, Ipv4InterfaceAddress (Ipv4Address
("11.0.0.1"), Ipv4Mask ("255.255.255.0")));
ipv4->SetUp (i);

// fgw tap device
m_fgwTap = CreateObject<VirtualNetDevice> ();
m_fgwTap->SetAddress (Mac48Address ("11:00:01:02:03:02"));
m_fgwTap->SetSendCallback (MakeCallback (&Tunnel::FGWVirtualSend,
this));
fgw->AddDevice (m_fgwTap);
ipv4 = fgw->GetObject<Ipv4> ();
i = ipv4->AddInterface (m_fgwTap);
ipv4->AddAddress (i, Ipv4InterfaceAddress (Ipv4Address
("11.0.0.2"), Ipv4Mask ("255.255.255.0")));
ipv4->SetUp (i);

}


};

int
main (int argc, char *argv[])
{
GlobalValue::Bind("ChecksumEnabled", BooleanValue (true));

LogComponentEnable ("Example", LOG_LEVEL_INFO);


//
// Allow the user to override any of the defaults and the above Bind()
at
// run-time, via command-line arguments
//
CommandLine cmd;
cmd.Parse (argc, argv);

//Create the Femto Cell and Femto GW and connect them with a P2P link
NS_LOG_INFO ("Create Femto nodes.");
NodeContainer p2pNodes;
p2pNodes.Create (2); //Femto = 0; FGW = 1

NodeContainer csmaNodes_access; //UEs to Femto
csmaNodes_access.Add (p2pNodes.Get(0)); //Add femto to same network
as UE
csmaNodes_access.Create (1); //Create the UE1

NodeContainer csmaNodes_core; //FGW to core
csmaNodes_core.Add(p2pNodes.Get(1)); //Add FGW to same network as
UE2
csmaNodes_core.Create (1); //Create UE2

PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);

CsmaHelper csma_core;
csma_core.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
csma_core.SetChannelAttribute ("Delay", TimeValue (NanoSeconds
(6560)));
NetDeviceContainer csma_coreDevices;
csma_coreDevices = csma_core.Install (csmaNodes_core);

CsmaHelper csma_access;
csma_access.SetChannelAttribute ("DataRate", StringValue
("100Mbps"));
csma_access.SetChannelAttribute ("Delay", TimeValue (NanoSeconds
(6560)));
NetDeviceContainer csma_accessDevices;
csma_accessDevices = csma_access.Install (csmaNodes_access);

InternetStackHelper stack;
stack.Install (p2pNodes);
stack.Install (csmaNodes_core.Get(1));
stack.Install (csmaNodes_access.Get(1));

//Add IP Addresses
Ipv4AddressHelper ipv4;

NS_LOG_INFO ("Assign IP Addresses.");
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer csma_coreInterfaces;
csma_coreInterfaces = ipv4.Assign (csma_coreDevices);

ipv4.SetBase ("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = ipv4.Assign (p2pDevices);

ipv4.SetBase ("10.1.3.0", "255.255.255.0");
Ipv4InterfaceContainer csma_accessInterfaces;
csma_accessInterfaces = ipv4.Assign (csma_accessDevices);

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

// Add the tunnels
Tunnel tunnel (p2pNodes.Get (0), p2pNodes.Get (1),
p2pInterfaces.GetAddress (0),
p2pInterfaces.GetAddress (1));

//Routing
Ipv4StaticRoutingHelper ipv4RoutingHelper;
Ptr<Ipv4> ipv4Fap;
Ptr<Ipv4StaticRouting> staticRoutingNode;

//FAP
ipv4Fap = p2pNodes.Get(0)->GetObject<Ipv4> ();
staticRoutingNode = ipv4RoutingHelper.GetStaticRouting (ipv4Fap);
staticRoutingNode->AddHostRouteTo (Ipv4Address ("10.1.1.2"),
Ipv4Address ("11.0.0.2"), 1);

UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install
(csmaNodes_core.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (csma_coreInterfaces.GetAddress (1),
9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

ApplicationContainer clientApps =
echoClient.Install (csmaNodes_access.Get (1));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));

AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll (ascii.CreateFileStream ("femto.tr"));
csma_core.EnableAsciiAll (ascii.CreateFileStream ("femto.tr"));
csma_access.EnableAsciiAll (ascii.CreateFileStream ("femto.tr"));
pointToPoint.EnablePcapAll ("femto");
csma_core.EnablePcapAll ("femto");
csma_access.EnablePcapAll ("femto");
//
// Now, do the actual simulation.
//
NS_LOG_INFO ("Run Simulation.");
Simulator::Run ();
Simulator::Destroy ();
NS_LOG_INFO ("Done.");
}
Reply all
Reply to author
Forward
0 new messages