A Discrete-Event Network Simulator
API
radvd-two-prefix.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 Strasbourg University
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: David Gross <gdavid.devel@gmail.com>
19  * Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
20  */
21 
22 // Network topology
23 // //
24 // // n0 R n1
25 // // | _ |
26 // // ====|_|====
27 // // router
28 // // - R sends RA to n0's subnet (2001:1::/64 and 2001:ABCD::/64);
29 // // - R interface to n0 has two addresses with following prefixes 2001:1::/64 and 2001:ABCD::/64;
30 // // - R sends RA to n1's subnet (2001:2::/64);
31 // // - n0 ping6 n1.
32 // //
33 // // - Tracing of queues and packet receptions to file "radvd-two-prefix.tr"
34 
35 #include <fstream>
36 #include "ns3/core-module.h"
37 #include "ns3/internet-module.h"
38 #include "ns3/csma-module.h"
39 #include "ns3/internet-apps-module.h"
40 
41 #include "ns3/ipv6-routing-table-entry.h"
42 #include "ns3/radvd.h"
43 #include "ns3/radvd-interface.h"
44 #include "ns3/radvd-prefix.h"
45 #include "ns3/ipv6-static-routing-helper.h"
46 
47 using namespace ns3;
48 
49 NS_LOG_COMPONENT_DEFINE ("RadvdTwoPrefixExample");
50 
56 {
57 public:
62  inline void PrintIpAddresses (Ptr<Node>& n)
63  {
64  Ptr<Ipv6> ipv6 = n->GetObject<Ipv6> ();
65  uint32_t nInterfaces = ipv6->GetNInterfaces();
66 
67  std::cout << "Node: " << ipv6->GetObject<Node> ()->GetId ()
68  << " Time: " << Simulator::Now ().GetSeconds () << "s "
69  << "IPv6 addresses" << std::endl;
70  std::cout << "(Interface index, Address index)\t" << "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) << std::endl;
77  }
78  }
79  std::cout << std::endl;
80  }
81 };
82 
83 int main (int argc, char** argv)
84 {
85  bool verbose = false;
86 
87  CommandLine cmd (__FILE__);
88  cmd.AddValue ("verbose", "turn on log components", verbose);
89  cmd.Parse (argc, argv);
90 
91  if (verbose)
92  {
93  LogComponentEnable ("Ipv6L3Protocol", LOG_LEVEL_ALL);
94  LogComponentEnable ("Ipv6RawSocketImpl", LOG_LEVEL_ALL);
95  LogComponentEnable ("Icmpv6L4Protocol", LOG_LEVEL_ALL);
96  LogComponentEnable ("Ipv6StaticRouting", LOG_LEVEL_ALL);
97  LogComponentEnable ("Ipv6Interface", LOG_LEVEL_ALL);
98  LogComponentEnable ("RadvdApplication", LOG_LEVEL_ALL);
99  LogComponentEnable ("Ping6Application", LOG_LEVEL_ALL);
100  }
101 
102  NS_LOG_INFO ("Create nodes.");
103  Ptr<Node> n0 = CreateObject<Node> ();
104  Ptr<Node> r = CreateObject<Node> ();
105  Ptr<Node> n1 = CreateObject<Node> ();
106 
107  NodeContainer net1 (n0, r);
108  NodeContainer net2 (r, n1);
109  NodeContainer all (n0, r, n1);
110 
111  NS_LOG_INFO ("Create IPv6 Internet Stack");
112  InternetStackHelper internetv6;
113  internetv6.Install (all);
114 
115  NS_LOG_INFO ("Create channels.");
117  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
118  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
119  NetDeviceContainer d1 = csma.Install (net1); /* n0 - R */
120  NetDeviceContainer d2 = csma.Install (net2); /* R - n1 */
121 
122  NS_LOG_INFO ("Create networks and assign IPv6 Addresses.");
123  Ipv6AddressHelper ipv6;
124 
125  /* first subnet */
126  ipv6.SetBase (Ipv6Address ("2001:1::"), Ipv6Prefix (64));
127  NetDeviceContainer tmp;
128  tmp.Add (d1.Get (0)); /* n0 */
129  Ipv6InterfaceContainer iic1 = ipv6.AssignWithoutAddress (tmp); /* n0 interface */
130 
131  NetDeviceContainer tmp2;
132  tmp2.Add (d1.Get (1)); /* R */
133  Ipv6InterfaceContainer iicr1 = ipv6.Assign (tmp2); /* R interface to the first subnet is just statically assigned */
134  iicr1.SetForwarding (0, true);
135  iic1.Add (iicr1);
136 
137  /* add another IPv6 address for second prefix advertised on first subnet */
138  ipv6.SetBase (Ipv6Address ("2001:ABCD::"), Ipv6Prefix (64));
139  ipv6.Assign (tmp2);
140 
141  /* second subnet R - n1 */
142  ipv6.SetBase (Ipv6Address ("2001:2::"), Ipv6Prefix (64));
143  NetDeviceContainer tmp3;
144  tmp3.Add (d2.Get (0)); /* R */
145  Ipv6InterfaceContainer iicr2 = ipv6.Assign (tmp3); /* R interface */
146  iicr2.SetForwarding (0, true);
147 
148  NetDeviceContainer tmp4;
149  tmp4.Add (d2.Get (1)); /* n1 */
150  Ipv6InterfaceContainer iic2 = ipv6.AssignWithoutAddress (tmp4);
151  iic2.Add (iicr2);
152 
153  /* radvd configuration */
154  RadvdHelper radvdHelper;
155  /* R interface (n0 - R) */
156  radvdHelper.AddAnnouncedPrefix(iic1.GetInterfaceIndex (1), Ipv6Address("2001:ABCD::0"), 64);
157  radvdHelper.AddAnnouncedPrefix(iic1.GetInterfaceIndex (1), Ipv6Address("2001:1::0"), 64);
158 
159  // Set some non-standard timers so the simulation is not taking ages
160  Ptr<RadvdInterface> routerInterface = radvdHelper.GetRadvdInterface(iic1.GetInterfaceIndex (1));
161  routerInterface->SetMaxRtrAdvInterval (2000);
162  routerInterface->SetMinRtrAdvInterval (1000);
163  RadvdInterface::RadvdPrefixList prefixList = routerInterface->GetPrefixes ();
164  for (RadvdInterface::RadvdPrefixListI iter = prefixList.begin(); iter != prefixList.end(); iter++)
165  {
166  (*iter)->SetPreferredLifeTime (3);
167  (*iter)->SetValidLifeTime (5);
168  }
169 
170  /* R interface (R - n1) */
171  radvdHelper.AddAnnouncedPrefix(iic2.GetInterfaceIndex (1), Ipv6Address("2001:2::0"), 64);
172 
173  // Set some non-standard timers so the simulation is not taking ages
174  routerInterface = radvdHelper.GetRadvdInterface(iic2.GetInterfaceIndex (1));
175  routerInterface->SetMaxRtrAdvInterval (2000);
176  routerInterface->SetMinRtrAdvInterval (1000);
177  prefixList = routerInterface->GetPrefixes ();
178  for (RadvdInterface::RadvdPrefixListI iter = prefixList.begin(); iter != prefixList.end(); iter++)
179  {
180  (*iter)->SetPreferredLifeTime (3);
181  (*iter)->SetValidLifeTime (5);
182  }
183 
184  ApplicationContainer radvdApps = radvdHelper.Install(r);
185  radvdApps.Start (Seconds (1.0));
186  radvdApps.Stop (Seconds (2.0));
187 
188  /* Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via R */
189  uint32_t packetSize = 1024;
190  uint32_t maxPacketCount = 8;
191  Time interPacketInterval = Seconds (1.);
192  Ping6Helper ping6;
193 
194  /* ping6.SetLocal (iic1.GetAddress (0, 1)); */
195  ping6.SetRemote (Ipv6Address ("2001:2::200:ff:fe00:4")); /* should be n1 address after autoconfiguration */
196  ping6.SetIfIndex (iic1.GetInterfaceIndex (0));
197 
198  ping6.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
199  ping6.SetAttribute ("Interval", TimeValue (interPacketInterval));
200  ping6.SetAttribute ("PacketSize", UintegerValue (packetSize));
201  ApplicationContainer apps = ping6.Install (net1.Get (0));
202  apps.Start (Seconds (2.0));
203  apps.Stop (Seconds (9.0));
204 
205  Ipv6StaticRoutingHelper routingHelper;
206  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> (&std::cout);
207  routingHelper.PrintRoutingTableAt (Seconds (2.0), n0, routingStream);
208  routingHelper.PrintRoutingTableAt (Seconds (10.0), n0, routingStream);
209 
210  IpAddressHelper ipAddressHelper;
211  /* RA should be received, two prefixes + routes + default route should be present */
212  Simulator::Schedule (Seconds (2.0), &IpAddressHelper::PrintIpAddresses, &ipAddressHelper, n0);
213  /* at the end, RA addresses and routes should be cleared */
214  Simulator::Schedule (Seconds (10.0), &IpAddressHelper::PrintIpAddresses, &ipAddressHelper, n0);
215 
216  AsciiTraceHelper ascii;
217  csma.EnableAsciiAll (ascii.CreateFileStream ("radvd-two-prefix.tr"));
218  csma.EnablePcapAll (std::string ("radvd-two-prefix"), true);
219 
220  NS_LOG_INFO ("Run Simulation.");
221  Simulator::Run ();
223  NS_LOG_INFO ("Done.");
224 }
225 
holds a vector of ns3::Application pointers.
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
Ptr< RadvdInterface > GetRadvdInterface(uint32_t interface)
Get the low-level RadvdInterface specification for an interface.
Definition: radvd-helper.cc:92
virtual uint32_t GetNAddresses(uint32_t interface) const =0
Get number of addresses on specified IPv6 interface.
Keep track of a set of IPv6 interfaces.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:81
ApplicationContainer Install(NodeContainer c)
Install the application in Nodes.
Definition: ping6-helper.cc:50
static const uint32_t packetSize
void Add(Ptr< Ipv6 > ipv6, uint32_t interface)
Add a couple IPv6/interface.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:379
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1286
aggregate IP/TCP/UDP functionality to existing Nodes.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
void SetAttribute(std::string name, const AttributeValue &value)
Set some attributes.
Definition: ping6-helper.cc:45
cmd
Definition: second.py:35
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
ApplicationContainer Install(Ptr< Node > node)
Install the application in a Node.
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.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
AttributeValue implementation for Time.
Definition: nstime.h:1342
virtual Ipv6InterfaceAddress GetAddress(uint32_t interface, uint32_t addressIndex) const =0
Get IPv6 address on specified IPv6 interface.
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
std::list< Ptr< RadvdPrefix > > RadvdPrefixList
Container: Ptr to RadvdPrefix.
uint32_t GetInterfaceIndex(uint32_t i) const
Get the interface index for the specified node index.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
csma
Definition: second.py:63
Parse command-line arguments.
Definition: command-line.h:226
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
void PrintIpAddresses(Ptr< Node > &n)
Print the node&#39;s IP addresses.
RadvdPrefixList GetPrefixes() const
Get list of prefixes advertised for this interface.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
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
Helper class that adds ns3::Ipv6StaticRouting objects.
void SetMinRtrAdvInterval(uint32_t minRtrAdvInterval)
Get minimum RA interval.
void SetRemote(Ipv6Address ip)
Set the remote IPv6 address.
Definition: ping6-helper.cc:40
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
Helper to print a node&#39;s IP addresses.
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
Helper class to auto-assign global IPv6 unicast addresses.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Radvd application helper.
Definition: radvd-helper.h:41
Describes an IPv6 address.
Definition: ipv6-address.h:49
void SetIfIndex(uint32_t ifIndex)
Set the out interface index.
Definition: ping6-helper.cc:67
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
std::list< Ptr< RadvdPrefix > >::iterator RadvdPrefixListI
Container Iterator: Ptr to RadvdPrefix.
AttributeValue implementation for DataRate.
Definition: data-rate.h:229
A network Node.
Definition: node.h:56
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1278
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
Print everything.
Definition: log.h:116
void SetMaxRtrAdvInterval(uint32_t maxRtrAdvInterval)
Get maximum RA interval.
Ping6 application helper.
Definition: ping6-helper.h:38
Ipv6InterfaceContainer AssignWithoutAddress(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer but do not assign any IPv6 addresses.
virtual uint32_t GetNInterfaces(void) const =0
Get number of interfaces.
bool verbose