diff --git a/src/lte/examples/epc-gtpu-tunnel-example.cc b/src/lte/examples/epc-gtpu-tunnel-example.cc new file mode 100644 index 000000000..3b0409823 --- /dev/null +++ b/src/lte/examples/epc-gtpu-tunnel-example.cc @@ -0,0 +1,119 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Jaume Nin + */ + +#include +#include +#include +#include + +#include "ns3/core-module.h" +#include "ns3/network-module.h" +#include "ns3/lte-module.h" +#include "ns3/config-store.h" +#include "ns3/core-module.h" +#include "ns3/network-module.h" +#include "ns3/internet-module.h" +#include "ns3/csma-module.h" +#include "ns3/csma-helper.h" +#include "ns3/applications-module.h" +#include "ns3/virtual-net-device.h" +#include "ns3/ipv4-global-routing-helper.h" +#include "ns3/epc-gtpu-tunnel.h" +#include "ns3/inet-socket-address.h" + +using namespace ns3; + +NS_LOG_COMPONENT_DEFINE ("EpcGtpUTunnelExample"); +int +main (int argc, char *argv[]) +{ + Packet::EnablePrinting (); + + // Command line arguments + CommandLine cmd; + cmd.Parse(argc, argv); + + ConfigStore inputConfig; + inputConfig.ConfigureDefaults(); + + // parse again so you can override default values from the command line + cmd.Parse(argc, argv); + + NS_LOG_INFO("Create nodes."); + NodeContainer c; + c.Create(2); + + InternetStackHelper internet; + internet.Install(c); + + // Create the channels first + NS_LOG_INFO("Create channels."); + + CsmaHelper csma; + csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); + csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); + NetDeviceContainer nc = csma.Install(c); + AsciiTraceHelper ascii; + csma.EnableAsciiAll (ascii.CreateFileStream ("epc-gtp.tr")); + csma.EnablePcapAll ("epc-gtp"); + + NS_LOG_INFO("Assign IP Addresses."); + Ipv4AddressHelper ipv4; + ipv4.SetBase("10.0.1.0", "255.255.255.0"); + Ipv4InterfaceContainer i = ipv4.Assign(nc); + Ipv4GlobalRoutingHelper::PopulateRoutingTables(); + + GtpuTunnel t(c.Get(0), c.Get(1), i.GetAddress(0),i.GetAddress(1), Ipv4Address ("11.0.0.1"),Ipv4Address ("11.0.0.2"), + Ipv4Mask("255.255.255.0"), 15); + + NS_LOG_INFO("Create Applications."); + // Set up some default values for the simulation. + Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(200)); + Config::SetDefault("ns3::OnOffApplication::OnTime", RandomVariableValue(ConstantVariable(1))); + Config::SetDefault("ns3::OnOffApplication::OffTime", RandomVariableValue(ConstantVariable(0))); + Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue ("2kbps")); + OnOffHelper onoffAB = OnOffHelper ("ns3::Ipv4RawSocketFactory", InetSocketAddress(Ipv4Address ("11.0.0.2"))); + OnOffHelper onoffBA = OnOffHelper ("ns3::Ipv4RawSocketFactory", InetSocketAddress(Ipv4Address ("11.0.0.1"))); + + + ApplicationContainer apps = onoffAB.Install(c.Get(0)); + apps.Start(Seconds(1.0)); + apps.Stop (Seconds (10.0)); + apps = onoffBA.Install(c.Get(1)); + apps.Start(Seconds(1.0)); + apps.Stop (Seconds (10.0)); + + // Create a packet sink to receive these packets + PacketSinkHelper sinkA("ns3::Ipv4RawSocketFactory", InetSocketAddress(Ipv4Address ("11.0.0.2"))); + PacketSinkHelper sinkB("ns3::Ipv4RawSocketFactory", InetSocketAddress(Ipv4Address ("11.0.0.2"))); + apps = sinkA.Install(c.Get(0)); + apps.Start(Seconds(1.0)); + apps.Stop (Seconds (10.0)); + apps = sinkB.Install(c.Get(1)); + apps.Start(Seconds(1.0)); + apps.Stop (Seconds (10.0)); + + NS_LOG_INFO ("Run Simulation."); + Simulator::Run (); + Simulator::Destroy (); + NS_LOG_INFO ("Done."); + + return 0; +} diff --git a/src/lte/helper/epc-helper.cc b/src/lte/helper/epc-helper.cc new file mode 100644 index 000000000..a8ba545d9 --- /dev/null +++ b/src/lte/helper/epc-helper.cc @@ -0,0 +1,53 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Jaume Nin + */ + +#include +#include + + + +namespace ns3 { + +NS_LOG_COMPONENT_DEFINE ("EpcHelper"); + +NS_OBJECT_ENSURE_REGISTERED (EpcHelper); + +EpcHelper::EpcHelper () +{ + NS_LOG_FUNCTION (this); +} + +EpcHelper::~EpcHelper () +{ + NS_LOG_FUNCTION (this); +} + +TypeId +EpcHelper::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::EpcHelper") + .SetParent () + .AddConstructor () + ; + return tid; +} + + +} // namespace ns3 diff --git a/src/lte/helper/epc-helper.h b/src/lte/helper/epc-helper.h new file mode 100644 index 000000000..3ee96cdff --- /dev/null +++ b/src/lte/helper/epc-helper.h @@ -0,0 +1,57 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Jaume Nin + */ + +#ifndef EPC_HELPER_H_ +#define EPC_HELPER_H_ + +#include "ns3/object.h" + +namespace ns3 { + +/** + * Helper class to handle the creation of the EPC entities and protocols + */ +class EpcHelper : public Object +{ +public: + /** + * Constructor + */ + EpcHelper (); + + /** + * Destructor + */ + virtual ~EpcHelper (); + + /** + * Inherited from ns3::Object + */ + static TypeId GetTypeId (void); + + + +private: + +}; + +} // namespace ns3 + +#endif /* EPC_HELPER_H_ */ diff --git a/src/lte/model/epc-gtpu-l5-protocol.cc b/src/lte/model/epc-gtpu-l5-protocol.cc new file mode 100644 index 000000000..a3efb9877 --- /dev/null +++ b/src/lte/model/epc-gtpu-l5-protocol.cc @@ -0,0 +1,88 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Jaume Nin + */ + +#include "ns3/epc-gtpu-l5-protocol.h" +#include +#include +#include "ns3/epc-gtpu-header.h" + +NS_LOG_COMPONENT_DEFINE ("GtpuL5Protocol"); + +namespace ns3 { + +NS_OBJECT_ENSURE_REGISTERED (GtpuL5Protocol); + +TypeId +GtpuL5Protocol::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::GtpuL5Protocol") + .SetParent () + .AddConstructor () + /*.AddAttribute ("TEID", "Tunnel Endpoint Identifier of this instance.", + UintegerValue (0), + MakeUintegerAccessor (&GtpuL5Protocol::SetTeid, + &GtpuL5Protocol::GetTeid), + MakeUintegerChecker ())*/ + ; + return tid; +} + +GtpuL5Protocol::GtpuL5Protocol () +{ + NS_LOG_FUNCTION_NOARGS (); +} + +GtpuL5Protocol::GtpuL5Protocol (uint32_t teid) +{ + NS_LOG_FUNCTION (this); + //this.SetAttribute("TEID", teid); + m_teid = teid; + +} + + +GtpuL5Protocol::~GtpuL5Protocol () +{ + NS_LOG_FUNCTION_NOARGS (); +} + +Ptr +GtpuL5Protocol::AddHeader (Ptr p) +{ + GtpuHeader h; + h.SetTeid (m_teid); + // From 3GPP TS 29.281 v10.0.0 Section 5.1 + // Length of the payload + the non obligatory GTP-U header + h.SetLength (p->GetSize () + h.GetSerializedSize () - 8); + p->AddHeader (h); + NS_LOG_FUNCTION (this << h); + return p; +} + +void +GtpuL5Protocol::RemoveHeader (Ptr p) +{ + GtpuHeader h; + p->RemoveHeader (h); + NS_LOG_DEBUG (this << h); + NS_ASSERT( h.GetTeid () == m_teid); +} + +}; // namespace ns3 diff --git a/src/lte/model/epc-gtpu-l5-protocol.h b/src/lte/model/epc-gtpu-l5-protocol.h new file mode 100644 index 000000000..418366e38 --- /dev/null +++ b/src/lte/model/epc-gtpu-l5-protocol.h @@ -0,0 +1,58 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Jaume Nin + */ + +#ifndef EPS_GTPU_L5_PROTOCOL_H +#define EPS_GTPU_L5_PROTOCOL_H + +#include +#include +#include + +namespace ns3 +{ + +/** + * Implementation of the GTP-U v1 protocol + */ +class GtpuL5Protocol : public Object +{ +public: + static TypeId GetTypeId (void); + + GtpuL5Protocol (); + GtpuL5Protocol (uint32_t teid); + virtual ~GtpuL5Protocol (); + + Ptr AddHeader (Ptr p); + void RemoveHeader (Ptr p); + + uint32_t GetTeid (void); + void SetTeid (uint32_t teid); + +private: + + uint32_t m_teid; + +}; + +} +; // namespace ns3 + +#endif /* EPS_GTPU_L5_PROTOCOL_H */ diff --git a/src/lte/model/epc-gtpu-tunnel.cc b/src/lte/model/epc-gtpu-tunnel.cc new file mode 100644 index 000000000..f5de30b40 --- /dev/null +++ b/src/lte/model/epc-gtpu-tunnel.cc @@ -0,0 +1,125 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Jaume Nin + */ + + +#include "epc-gtpu-tunnel.h" +#include "ns3/log.h" +#include "ns3/uinteger.h" +#include "ns3/integer.h" +#include "ns3/boolean.h" +#include "ns3/inet-socket-address.h" +#include "ns3/ipv4.h" +#include "ns3/mac48-address.h" +#include "ns3/ppp-header.h" + +namespace ns3 { + +NS_LOG_COMPONENT_DEFINE ("GtpuTunnel"); + +//m_gtpuPort = 2152; + +TypeId +GtpuTunnel::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::GtpuTunnel") + .SetParent (); + ; + return tid; +} + +GtpuTunnel::GtpuTunnel (Ptr nA, Ptr nB, Ipv4Address addrA, Ipv4Address addrB, Ipv4Address tAddrA, Ipv4Address tAddrB, Ipv4Mask m, uint32_t teid): + m_addressA(addrA), m_addressB(addrB), m_udpPort (2152) +{ + NS_LOG_FUNCTION (this); + m_gtpu = CreateObject (teid); + + // UDP socket creation and configuration + m_socketA = Socket::CreateSocket (nA, TypeId::LookupByName ("ns3::UdpSocketFactory")); + m_socketA->Bind (InetSocketAddress (Ipv4Address::GetAny (), m_udpPort)); + m_socketA->SetRecvCallback (MakeCallback (&GtpuTunnel::GtpuNaRecv, this)); + + m_socketB = Socket::CreateSocket (nB, TypeId::LookupByName ("ns3::UdpSocketFactory")); + m_socketB->Bind (InetSocketAddress (Ipv4Address::GetAny (), m_udpPort)); + m_socketB->SetRecvCallback (MakeCallback (&GtpuTunnel::GtpuNbRecv, this)); + + // tap device creation and configuration + // n0 tap device + m_tapA = CreateObject (); + m_tapA->SetAddress (Mac48Address::Allocate ()); + m_tapA->SetSendCallback (MakeCallback (&GtpuTunnel::GtpuNaSend, this)); + nA->AddDevice (m_tapA); + Ptr ipv4 = nA->GetObject (); + uint32_t i = ipv4->AddInterface (m_tapA); + ipv4->AddAddress (i, Ipv4InterfaceAddress (tAddrA, m)); + ipv4->SetUp (i); + + m_tapB = CreateObject (); + m_tapB->SetAddress (Mac48Address::Allocate ()); + m_tapB->SetSendCallback (MakeCallback (&GtpuTunnel::GtpuNbSend, this)); + nB->AddDevice (m_tapB); + ipv4 = nB->GetObject (); + i = ipv4->AddInterface (m_tapA); + ipv4->AddAddress (i, Ipv4InterfaceAddress (tAddrB, m)); + ipv4->SetUp (i); + +} + +void +GtpuTunnel::GtpuNaRecv (Ptr socket) +{ + NS_LOG_FUNCTION (this); + Ptr packet = socket->Recv (65535, 0); + m_gtpu->RemoveHeader (packet); + m_tapA->Receive (packet, 0x0800, m_tapA->GetAddress (), m_tapA->GetAddress (), NetDevice::PACKET_HOST); +} + +void +GtpuTunnel::GtpuNbRecv (Ptr socket) +{ + NS_LOG_FUNCTION (this); + Ptr packet = socket->Recv (65535, 0); + m_gtpu->RemoveHeader (packet); + m_tapB->Receive (packet, 0x0800, m_tapB->GetAddress (), m_tapB->GetAddress (), NetDevice::PACKET_HOST); +} + +bool +GtpuTunnel::GtpuNaSend (Ptr packet, const Address& source, const Address& dest, uint16_t protocolNumber) +{ + NS_LOG_FUNCTION (this << source << dest << packet << packet->GetSize ()); + packet = m_gtpu->AddHeader (packet); + m_socketA->SendTo (packet, 0, InetSocketAddress (m_addressB, m_udpPort)); + return true; +} + +bool +GtpuTunnel::GtpuNbSend (Ptr packet, const Address& source, const Address& dest, uint16_t protocolNumber) +{ + NS_LOG_FUNCTION (this << source << dest << packet << packet->GetSize ()); + packet = m_gtpu->AddHeader (packet); + m_socketB->SendTo (packet, 0, InetSocketAddress (m_addressA, m_udpPort)); + return true; +} + +GtpuTunnel::~GtpuTunnel () +{ + NS_LOG_FUNCTION_NOARGS (); +} + +}; // namespace ns3 diff --git a/src/lte/model/epc-gtpu-tunnel.h b/src/lte/model/epc-gtpu-tunnel.h new file mode 100644 index 000000000..f074bac27 --- /dev/null +++ b/src/lte/model/epc-gtpu-tunnel.h @@ -0,0 +1,65 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Jaume Nin + */ + +#ifndef GTPU_TUNNEL_H +#define GTPU_TUNNEL_H + +#include "ns3/address.h" +#include "ns3/socket.h" +#include "ns3/virtual-net-device.h" +#include "ns3/epc-gtpu-l5-protocol.h" +#include "ns3/traced-callback.h" +#include "ns3/callback.h" +#include "ns3/ptr.h" +#include "ns3/object.h" + +namespace ns3 +{ + +class GtpuTunnel : public Object +{ + +public: + static TypeId GetTypeId (void); + bool GtpuNaSend (Ptr packet, const Address& source, const Address& dest, uint16_t protocolNumber); + bool GtpuNbSend (Ptr packet, const Address& source, const Address& dest, uint16_t protocolNumber); + void GtpuNaRecv (Ptr socket); + void GtpuNbRecv (Ptr socket); + + GtpuTunnel (Ptr nA, Ptr nB, Ipv4Address addrA, Ipv4Address addrB, Ipv4Address tAddrA, Ipv4Address tAddrB, Ipv4Mask m, uint32_t teid); + virtual ~GtpuTunnel (void); + +private: + + Ptr m_socketA; + Ptr m_socketB; + Ipv4Address m_addressA; + Ipv4Address m_addressB; + uint16_t m_udpPort; + //static const uint16_t m_gtpuPort; + Ptr m_tapA; + Ptr m_tapB; + Ptr m_gtpu; +}; + +} //namespace ns3 + +#endif /* GTPU_TUNNEL_H */ +