A Discrete-Event Network Simulator
API
radvd-two-prefix.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 Strasbourg University
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: David Gross <gdavid.devel@gmail.com>
18  * Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
19  */
20 
21 // Network topology
22 // //
23 // // n0 R n1
24 // // | _ |
25 // // ====|_|====
26 // // router
27 // // - R sends RA to n0's subnet (2001:1::/64 and 2001:ABCD::/64);
28 // // - R interface to n0 has two addresses with following prefixes 2001:1::/64 and 2001:ABCD::/64;
29 // // - R sends RA to n1's subnet (2001:2::/64);
30 // // - n0 ping n1.
31 // //
32 // // - Tracing of queues and packet receptions to file "radvd-two-prefix.tr"
33 
34 #include "ns3/core-module.h"
35 #include "ns3/csma-module.h"
36 #include "ns3/internet-apps-module.h"
37 #include "ns3/internet-module.h"
38 #include "ns3/ipv6-routing-table-entry.h"
39 #include "ns3/ipv6-static-routing-helper.h"
40 #include "ns3/radvd-interface.h"
41 #include "ns3/radvd-prefix.h"
42 #include "ns3/radvd.h"
43 
44 #include <fstream>
45 
46 using namespace ns3;
47 
48 NS_LOG_COMPONENT_DEFINE("RadvdTwoPrefixExample");
49 
55 {
56  public:
61  inline void PrintIpAddresses(Ptr<Node>& n)
62  {
63  Ptr<Ipv6> ipv6 = n->GetObject<Ipv6>();
64  uint32_t nInterfaces = ipv6->GetNInterfaces();
65 
66  std::cout << "Node: " << ipv6->GetObject<Node>()->GetId()
67  << " Time: " << Simulator::Now().GetSeconds() << "s "
68  << "IPv6 addresses" << std::endl;
69  std::cout << "(Interface index, Address index)\t"
70  << "IPv6 Address" << std::endl;
71 
72  for (uint32_t i = 0; i < nInterfaces; i++)
73  {
74  for (uint32_t j = 0; j < ipv6->GetNAddresses(i); j++)
75  {
76  std::cout << "(" << int(i) << "," << int(j) << ")\t" << ipv6->GetAddress(i, j)
77  << std::endl;
78  }
79  }
80  std::cout << std::endl;
81  }
82 };
83 
84 int
85 main(int argc, char** argv)
86 {
87  bool verbose = false;
88 
89  CommandLine cmd(__FILE__);
90  cmd.AddValue("verbose", "turn on log components", verbose);
91  cmd.Parse(argc, argv);
92 
93  if (verbose)
94  {
95  LogComponentEnable("Ipv6L3Protocol", LOG_LEVEL_ALL);
96  LogComponentEnable("Ipv6RawSocketImpl", LOG_LEVEL_ALL);
97  LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_ALL);
98  LogComponentEnable("Ipv6StaticRouting", LOG_LEVEL_ALL);
99  LogComponentEnable("Ipv6Interface", LOG_LEVEL_ALL);
100  LogComponentEnable("RadvdApplication", LOG_LEVEL_ALL);
102  }
103 
104  NS_LOG_INFO("Create nodes.");
105  Ptr<Node> n0 = CreateObject<Node>();
106  Ptr<Node> r = CreateObject<Node>();
107  Ptr<Node> n1 = CreateObject<Node>();
108 
109  NodeContainer net1(n0, r);
110  NodeContainer net2(r, n1);
111  NodeContainer all(n0, r, n1);
112 
113  NS_LOG_INFO("Create IPv6 Internet Stack");
114  InternetStackHelper internetv6;
115  internetv6.Install(all);
116 
117  NS_LOG_INFO("Create channels.");
119  csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
120  csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
121  NetDeviceContainer d1 = csma.Install(net1); /* n0 - R */
122  NetDeviceContainer d2 = csma.Install(net2); /* R - n1 */
123 
124  NS_LOG_INFO("Create networks and assign IPv6 Addresses.");
125  Ipv6AddressHelper ipv6;
126 
127  /* first subnet */
128  ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
129  NetDeviceContainer tmp;
130  tmp.Add(d1.Get(0)); /* n0 */
131  Ipv6InterfaceContainer iic1 = ipv6.AssignWithoutAddress(tmp); /* n0 interface */
132 
133  NetDeviceContainer tmp2;
134  tmp2.Add(d1.Get(1)); /* R */
135  Ipv6InterfaceContainer iicr1 =
136  ipv6.Assign(tmp2); /* R interface to the first subnet is just statically assigned */
137  iicr1.SetForwarding(0, true);
138  iic1.Add(iicr1);
139 
140  /* add another IPv6 address for second prefix advertised on first subnet */
141  ipv6.SetBase(Ipv6Address("2001:ABCD::"), Ipv6Prefix(64));
142  ipv6.Assign(tmp2);
143 
144  /* second subnet R - n1 */
145  ipv6.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
146  NetDeviceContainer tmp3;
147  tmp3.Add(d2.Get(0)); /* R */
148  Ipv6InterfaceContainer iicr2 = ipv6.Assign(tmp3); /* R interface */
149  iicr2.SetForwarding(0, true);
150 
151  NetDeviceContainer tmp4;
152  tmp4.Add(d2.Get(1)); /* n1 */
154  iic2.Add(iicr2);
155 
156  /* radvd configuration */
157  RadvdHelper radvdHelper;
158  /* R interface (n0 - R) */
159  radvdHelper.AddAnnouncedPrefix(iic1.GetInterfaceIndex(1), Ipv6Address("2001:ABCD::0"), 64);
160  radvdHelper.AddAnnouncedPrefix(iic1.GetInterfaceIndex(1), Ipv6Address("2001:1::0"), 64);
161 
162  // Set some non-standard timers so the simulation is not taking ages
163  Ptr<RadvdInterface> routerInterface = radvdHelper.GetRadvdInterface(iic1.GetInterfaceIndex(1));
164  routerInterface->SetMaxRtrAdvInterval(2000);
165  routerInterface->SetMinRtrAdvInterval(1000);
166  RadvdInterface::RadvdPrefixList prefixList = routerInterface->GetPrefixes();
167  for (auto iter = prefixList.begin(); iter != prefixList.end(); iter++)
168  {
169  (*iter)->SetPreferredLifeTime(3);
170  (*iter)->SetValidLifeTime(5);
171  }
172 
173  /* R interface (R - n1) */
174  radvdHelper.AddAnnouncedPrefix(iic2.GetInterfaceIndex(1), Ipv6Address("2001:2::0"), 64);
175 
176  // Set some non-standard timers so the simulation is not taking ages
177  routerInterface = radvdHelper.GetRadvdInterface(iic2.GetInterfaceIndex(1));
178  routerInterface->SetMaxRtrAdvInterval(2000);
179  routerInterface->SetMinRtrAdvInterval(1000);
180  prefixList = routerInterface->GetPrefixes();
181  for (auto iter = prefixList.begin(); iter != prefixList.end(); iter++)
182  {
183  (*iter)->SetPreferredLifeTime(3);
184  (*iter)->SetValidLifeTime(5);
185  }
186 
187  ApplicationContainer radvdApps = radvdHelper.Install(r);
188  radvdApps.Start(Seconds(1.0));
189  radvdApps.Stop(Seconds(2.0));
190 
191  /* Create a Ping application to send ICMPv6 echo request from n0 to n1 via R */
192  uint32_t packetSize = 1024;
193  uint32_t maxPacketCount = 8;
194  PingHelper ping(
195  Ipv6Address("2001:2::200:ff:fe00:4")); /* should be n1 address after autoconfiguration */
196  // ping.SetIfIndex(iic1.GetInterfaceIndex(0));
197 
198  ping.SetAttribute("Count", UintegerValue(maxPacketCount));
199  ping.SetAttribute("Size", UintegerValue(packetSize));
200  ApplicationContainer apps = ping.Install(net1.Get(0));
201  apps.Start(Seconds(2.0));
202  apps.Stop(Seconds(5.0));
203 
204  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>(&std::cout);
205  Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(2.0), n0, routingStream);
206  Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(10.0), n0, routingStream);
207 
208  IpAddressHelper ipAddressHelper;
209  /* RA should be received, two prefixes + routes + default route should be present */
210  Simulator::Schedule(Seconds(2.0), &IpAddressHelper::PrintIpAddresses, &ipAddressHelper, n0);
211  /* at the end, RA addresses and routes should be cleared */
212  Simulator::Schedule(Seconds(10.0), &IpAddressHelper::PrintIpAddresses, &ipAddressHelper, n0);
213 
214  AsciiTraceHelper ascii;
215  csma.EnableAsciiAll(ascii.CreateFileStream("radvd-two-prefix.tr"));
216  csma.EnablePcapAll(std::string("radvd-two-prefix"), true);
217 
218  NS_LOG_INFO("Run Simulation.");
219  Simulator::Run();
221  NS_LOG_INFO("Done.");
222 
223  return 0;
224 }
Helper to print a node's IP addresses.
void PrintIpAddresses(Ptr< Node > &n)
Print the node's IP addresses.
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Helper class to auto-assign global IPv6 unicast addresses.
Ipv6InterfaceContainer AssignWithoutAddress(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer but do not assign any IPv6 addresses.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:49
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:82
Keep track of a set of IPv6 interfaces.
void SetForwarding(uint32_t i, bool state)
Set the state of the stack (act as a router or as an host) for the specified index.
uint32_t GetInterfaceIndex(uint32_t i) const
Get the interface index for the specified node index.
void Add(Ptr< Ipv6 > ipv6, uint32_t interface)
Add a couple IPv6/interface.
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
static void PrintRoutingTableAt(Time printTime, Ptr< Node > node, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the routing tables of a node at a particular time.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
A network Node.
Definition: node.h:57
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
Create a ping application and associate it to a node.
Definition: ping-helper.h:48
Radvd application helper.
Definition: radvd-helper.h:41
void AddAnnouncedPrefix(uint32_t interface, Ipv6Address prefix, uint32_t prefixLength)
Add a new prefix to be announced through an interface.
Definition: radvd-helper.cc:39
ApplicationContainer Install(Ptr< Node > node)
Install the application in a Node.
Ptr< RadvdInterface > GetRadvdInterface(uint32_t interface)
Get the low-level RadvdInterface specification for an interface.
Definition: radvd-helper.cc:97
std::list< Ptr< RadvdPrefix > > RadvdPrefixList
Container: Ptr to RadvdPrefix.
RadvdPrefixList GetPrefixes() const
Get list of prefixes advertised for this interface.
void SetMaxRtrAdvInterval(uint32_t maxRtrAdvInterval)
Get maximum RA interval.
void SetMinRtrAdvInterval(uint32_t minRtrAdvInterval)
Get minimum RA interval.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
Hold an unsigned integer type.
Definition: uinteger.h:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
csma
Definition: second.py:63
cmd
Definition: second.py:40
bool verbose
static const uint32_t packetSize
Packet size generated at the AP.