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 
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::Ping6Helper::SetIfIndex
void SetIfIndex(uint32_t ifIndex)
Set the out interface index.
Definition: ping6-helper.cc:67
ns3::DataRateValue
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::RadvdInterface::SetMinRtrAdvInterval
void SetMinRtrAdvInterval(uint32_t minRtrAdvInterval)
Get minimum RA interval.
Definition: radvd-interface.cc:143
ns3::Ipv6RoutingHelper::PrintRoutingTableAt
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.
Definition: ipv6-routing-helper.cc:59
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::ApplicationContainer::Stop
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Definition: application-container.cc:107
ns3::Ipv6InterfaceContainer::SetForwarding
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.
Definition: ipv6-interface-container.cc:83
IpAddressHelper::PrintIpAddresses
void PrintIpAddresses(Ptr< Node > &n)
Print the node's IP addresses.
Definition: radvd-two-prefix.cc:62
ns3::RadvdInterface::RadvdPrefixListI
std::list< Ptr< RadvdPrefix > >::iterator RadvdPrefixListI
Container Iterator: Ptr to RadvdPrefix.
Definition: radvd-interface.h:42
ns3::Object::GetObject
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
ns3::Ipv6AddressHelper::AssignWithoutAddress
Ipv6InterfaceContainer AssignWithoutAddress(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer but do not assign any IPv6 addresses.
Definition: ipv6-address-helper.cc:293
ns3::LOG_LEVEL_ALL
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
ns3::Node
A network Node.
Definition: node.h:57
ns3::RadvdInterface::SetMaxRtrAdvInterval
void SetMaxRtrAdvInterval(uint32_t maxRtrAdvInterval)
Get maximum RA interval.
Definition: radvd-interface.cc:131
ns3::LogComponentEnable
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
ns3::Ipv6Address
Describes an IPv6 address.
Definition: ipv6-address.h:50
ns3::Ipv6AddressHelper::SetBase
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Definition: ipv6-address-helper.cc:69
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
ns3::Ipv6AddressHelper
Helper class to auto-assign global IPv6 unicast addresses.
Definition: ipv6-address-helper.h:83
ns3::Ping6Helper::Install
ApplicationContainer Install(NodeContainer c)
Install the application in Nodes.
Definition: ping6-helper.cc:50
ns3::CsmaHelper
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
ns3::RadvdHelper::AddAnnouncedPrefix
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
ns3::Ipv6AddressHelper::Assign
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Definition: ipv6-address-helper.cc:210
ns3::Ptr< Node >
ns3::Ping6Helper::SetRemote
void SetRemote(Ipv6Address ip)
Set the remote IPv6 address.
Definition: ping6-helper.cc:40
ns3::InternetStackHelper::Install
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Definition: internet-stack-helper.cc:366
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
ns3::AsciiTraceHelper::CreateFileStream
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.
Definition: trace-helper.cc:191
ns3::MilliSeconds
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1297
ns3::Ipv6InterfaceContainer
Keep track of a set of IPv6 interfaces.
Definition: ipv6-interface-container.h:42
ns3::Ping6Helper
Ping6 application helper.
Definition: ping6-helper.h:39
verbose
bool verbose
Definition: openflow-switch.cc:50
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::Ipv6StaticRoutingHelper
Helper class that adds ns3::Ipv6StaticRouting objects.
Definition: ipv6-static-routing-helper.h:44
second.cmd
cmd
Definition: second.py:35
ns3::Ipv6InterfaceContainer::Add
void Add(Ptr< Ipv6 > ipv6, uint32_t interface)
Add a couple IPv6/interface.
Definition: ipv6-interface-container.cc:64
ns3::RadvdInterface::RadvdPrefixList
std::list< Ptr< RadvdPrefix > > RadvdPrefixList
Container: Ptr to RadvdPrefix.
Definition: radvd-interface.h:40
ns3::RadvdHelper
Radvd application helper.
Definition: radvd-helper.h:42
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::RadvdHelper::GetRadvdInterface
Ptr< RadvdInterface > GetRadvdInterface(uint32_t interface)
Get the low-level RadvdInterface specification for an interface.
Definition: radvd-helper.cc:92
ns3::AsciiTraceHelper
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
ns3::RadvdInterface::GetPrefixes
RadvdPrefixList GetPrefixes() const
Get list of prefixes advertised for this interface.
Definition: radvd-interface.cc:107
packetSize
static const uint32_t packetSize
Definition: wifi-power-adaptation-distance.cc:113
ns3::ApplicationContainer::Start
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
Definition: application-container.cc:87
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Ipv6::GetNAddresses
virtual uint32_t GetNAddresses(uint32_t interface) const =0
Get number of addresses on specified IPv6 interface.
ns3::Ping6Helper::SetAttribute
void SetAttribute(std::string name, const AttributeValue &value)
Set some attributes.
Definition: ping6-helper.cc:45
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::RadvdHelper::Install
ApplicationContainer Install(Ptr< Node > node)
Install the application in a Node.
Definition: radvd-helper.cc:111
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::Ipv6
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:82
IpAddressHelper
Helper to print a node's IP addresses.
Definition: radvd-two-prefix.cc:56
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::Ipv6InterfaceContainer::GetInterfaceIndex
uint32_t GetInterfaceIndex(uint32_t i) const
Get the interface index for the specified node index.
Definition: ipv6-interface-container.cc:52
ns3::Ipv6Prefix
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::NetDeviceContainer::Get
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Definition: net-device-container.cc:62
second.csma
csma
Definition: second.py:63
ns3::NetDeviceContainer::Add
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Definition: net-device-container.cc:67
ns3::Time::GetSeconds
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:380
sample-rng-plot.n
n
Definition: sample-rng-plot.py:37
ns3::Ipv6::GetNInterfaces
virtual uint32_t GetNInterfaces(void) const =0
Get number of interfaces.
ns3::Ipv6::GetAddress
virtual Ipv6InterfaceAddress GetAddress(uint32_t interface, uint32_t addressIndex) const =0
Get IPv6 address on specified IPv6 interface.