1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2017 Jadavpur University, India
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Author: Manoj Kumar Rana <manoj24.rana@gmail.com>
19  */
20 
21 #include "ns3/lte-helper.h"
22 #include "ns3/epc-helper.h"
23 #include "ns3/core-module.h"
24 #include "ns3/network-module.h"
25 #include "ns3/ipv4-global-routing-helper.h"
26 #include "ns3/ipv6-static-routing.h"
27 #include "ns3/internet-module.h"
28 #include "ns3/mobility-module.h"
29 #include "ns3/lte-module.h"
30 #include "ns3/applications-module.h"
31 #include "ns3/point-to-point-helper.h"
32 #include "ns3/config-store.h"
33 
34 using namespace ns3;
35 
36 /**
37  * Sample simulation script for LTE+EPC. It instantiates several eNodeB,
38  * attaches one UE per eNodeB starts a flow for each UE to  and from a remote host.
39  */
40 
41 NS_LOG_COMPONENT_DEFINE ("EpcFirstExampleForIpv6");
42 
43 int
main(int argc,char * argv[])44 main (int argc, char *argv[])
45 {
46   CommandLine cmd (__FILE__);
47   cmd.Parse (argc, argv);
48 
49   Ptr<LteHelper> lteHelper = CreateObject<LteHelper> ();
50   Ptr<PointToPointEpcHelper>  epcHelper = CreateObject<PointToPointEpcHelper> ();
51   lteHelper->SetEpcHelper (epcHelper);
52 
53   Ptr<Node> pgw = epcHelper->GetPgwNode ();
54 
55   // Create a single RemoteHost
56   NodeContainer remoteHostContainer;
57   remoteHostContainer.Create (1);
58   Ptr<Node> remoteHost = remoteHostContainer.Get (0);
59   InternetStackHelper internet;
60   internet.Install (remoteHostContainer);
61 
62   // Create the Internet
63   PointToPointHelper p2ph;
64   p2ph.SetDeviceAttribute ("DataRate", DataRateValue (DataRate ("100Gb/s")));
65   p2ph.SetDeviceAttribute ("Mtu", UintegerValue (1500));
66   p2ph.SetChannelAttribute ("Delay", TimeValue (Seconds (0.010)));
67   NetDeviceContainer internetDevices = p2ph.Install (pgw, remoteHost);
68 
69   NodeContainer ueNodes;
70   NodeContainer enbNodes;
71   enbNodes.Create (2);
72   ueNodes.Create (2);
73 
74   // Install Mobility Model
75   Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
76   for (uint16_t i = 0; i < 2; i++)
77     {
78       positionAlloc->Add (Vector (60.0 * i, 0, 0));
79     }
80   MobilityHelper mobility;
81   mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
82   mobility.SetPositionAllocator (positionAlloc);
83   mobility.Install (enbNodes);
84   mobility.Install (ueNodes);
85 
86   // Install the IP stack on the UEs
87   internet.Install (ueNodes);
88 
89   // Install LTE Devices to the nodes
90   NetDeviceContainer enbLteDevs = lteHelper->InstallEnbDevice (enbNodes);
91   NetDeviceContainer ueLteDevs = lteHelper->InstallUeDevice (ueNodes);
92 
93   Ipv6InterfaceContainer ueIpIface;
94 
95   // Assign IP address to UEs
96   ueIpIface = epcHelper->AssignUeIpv6Address (NetDeviceContainer (ueLteDevs));
97 
98 
99   Ipv6StaticRoutingHelper ipv6RoutingHelper;
100 
101   for (uint32_t u = 0; u < ueNodes.GetN (); ++u)
102     {
103       Ptr<Node> ueNode = ueNodes.Get (u);
104       // Set the default gateway for the UEs
105       Ptr<Ipv6StaticRouting> ueStaticRouting = ipv6RoutingHelper.GetStaticRouting (ueNode->GetObject<Ipv6> ());
106       ueStaticRouting->SetDefaultRoute (epcHelper->GetUeDefaultGatewayAddress6 (), 1);
107     }
108 
109 
110   // Attach one UE per eNodeB
111   for (uint16_t i = 0; i < 2; i++)
112     {
113       lteHelper->Attach (ueLteDevs.Get (i), enbLteDevs.Get (i));
114       // side effect: the default EPS bearer will be activated
115     }
116 
117   Ipv6AddressHelper ipv6h;
118   ipv6h.SetBase (Ipv6Address ("6001:db80::"), Ipv6Prefix (64));
119   Ipv6InterfaceContainer internetIpIfaces = ipv6h.Assign (internetDevices);
120 
121   internetIpIfaces.SetForwarding (0, true);
122   internetIpIfaces.SetDefaultRouteInAllNodes (0);
123 
124   Ptr<Ipv6StaticRouting> remoteHostStaticRouting = ipv6RoutingHelper.GetStaticRouting (remoteHost->GetObject<Ipv6> ());
125   remoteHostStaticRouting->AddNetworkRouteTo ("7777:f00d::", Ipv6Prefix (64), internetIpIfaces.GetAddress (0, 1), 1, 0);
126 
127 
128   // interface 0 is localhost, 1 is the p2p device
129   Ipv6Address remoteHostAddr = internetIpIfaces.GetAddress (1, 1);
130 
131 
132   // Start applications on UEs and remote host
133   UdpEchoServerHelper echoServer (9);
134 
135   ApplicationContainer serverApps = echoServer.Install (remoteHost);
136 
137   serverApps.Start (Seconds (1.0));
138   serverApps.Stop (Seconds (20.0));
139 
140 
141   UdpEchoClientHelper echoClient1 (remoteHostAddr, 9);
142   UdpEchoClientHelper echoClient2 (remoteHostAddr, 9);
143 
144   echoClient1.SetAttribute ("MaxPackets", UintegerValue (1000));
145   echoClient1.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
146   echoClient1.SetAttribute ("PacketSize", UintegerValue (1024));
147 
148   echoClient2.SetAttribute ("MaxPackets", UintegerValue (1000));
149   echoClient2.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
150   echoClient2.SetAttribute ("PacketSize", UintegerValue (1024));
151 
152   ApplicationContainer clientApps1 = echoClient1.Install (ueNodes.Get (0));
153   ApplicationContainer clientApps2 = echoClient2.Install (ueNodes.Get (1));
154 
155 
156   clientApps1.Start (Seconds (1.0));
157   clientApps1.Stop (Seconds (14.0));
158 
159   clientApps2.Start (Seconds (1.5));
160   clientApps2.Stop (Seconds (14.5));
161 
162   LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_ALL);
163   LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_ALL);
164 
165  // PcapHelperForIpv6::EnablePcapIpv6 (std::string prefix, uint32_t nodeid, uint32_t interface, bool explicitFilename)
166 
167   internet.EnablePcapIpv6 ("LenaIpv6-Ue-Rh-Ue0.pcap", ueNodes.Get (0)->GetId (), 1, true);
168   internet.EnablePcapIpv6 ("LenaIpv6-Ue-Rh-Ue1.pcap", ueNodes.Get (1)->GetId (), 1, true);
169   internet.EnablePcapIpv6 ("LenaIpv6-Ue-Rh-RH.pcap", remoteHostContainer.Get (0)->GetId (), 1, true);
170   internet.EnablePcapIpv6 ("LenaIpv6-Ue-Rh-Pgw-Iface1.pcap", pgw->GetId (), 1, true);
171   internet.EnablePcapIpv6 ("LenaIpv6-Ue-Rh-Pgw-Iface2.pcap", pgw->GetId (), 2, true);
172 
173   Simulator::Stop (Seconds (20));
174   Simulator::Run ();
175 
176   Simulator::Destroy ();
177   return 0;
178 
179 }
180 
181