A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
radvd-two-prefix.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 Strasbourg University
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: David Gross <gdavid.devel@gmail.com>
7 * Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
8 */
9
10// Network topology
11// //
12// // n0 R n1
13// // | _ |
14// // ====|_|====
15// // router
16// // - R sends RA to n0's subnet (2001:1::/64 and 2001:ABCD::/64);
17// // - R interface to n0 has two addresses with following prefixes 2001:1::/64 and 2001:ABCD::/64;
18// // - R sends RA to n1's subnet (2001:2::/64);
19// // - n0 ping n1.
20// //
21// // - Tracing of queues and packet receptions to file "radvd-two-prefix.tr"
22
23#include "ns3/core-module.h"
24#include "ns3/csma-module.h"
25#include "ns3/internet-apps-module.h"
26#include "ns3/internet-module.h"
27#include "ns3/ipv6-routing-table-entry.h"
28#include "ns3/ipv6-static-routing-helper.h"
29#include "ns3/radvd-interface.h"
30#include "ns3/radvd-prefix.h"
31#include "ns3/radvd.h"
32
33#include <fstream>
34
35using namespace ns3;
36
37NS_LOG_COMPONENT_DEFINE("RadvdTwoPrefixExample");
38
39/**
40 * \class IpAddressHelper
41 * \brief Helper to print a node's IP addresses.
42 */
44{
45 public:
46 /**
47 * \brief Print the node's IP addresses.
48 * \param n the node
49 */
51 {
52 Ptr<Ipv6> ipv6 = n->GetObject<Ipv6>();
53 uint32_t nInterfaces = ipv6->GetNInterfaces();
54
55 std::cout << "Node: " << ipv6->GetObject<Node>()->GetId()
56 << " Time: " << Simulator::Now().GetSeconds() << "s "
57 << "IPv6 addresses" << std::endl;
58 std::cout << "(Interface index, Address index)\t"
59 << "IPv6 Address" << std::endl;
60
61 for (uint32_t i = 0; i < nInterfaces; i++)
62 {
63 for (uint32_t j = 0; j < ipv6->GetNAddresses(i); j++)
64 {
65 std::cout << "(" << int(i) << "," << int(j) << ")\t" << ipv6->GetAddress(i, j)
66 << std::endl;
67 }
68 }
69 std::cout << std::endl;
70 }
71};
72
73int
74main(int argc, char** argv)
75{
76 bool verbose = false;
77
78 CommandLine cmd(__FILE__);
79 cmd.AddValue("verbose", "turn on log components", verbose);
80 cmd.Parse(argc, argv);
81
82 if (verbose)
83 {
84 LogComponentEnable("Ipv6L3Protocol", LOG_LEVEL_ALL);
85 LogComponentEnable("Ipv6RawSocketImpl", LOG_LEVEL_ALL);
86 LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_ALL);
87 LogComponentEnable("Ipv6StaticRouting", LOG_LEVEL_ALL);
88 LogComponentEnable("Ipv6Interface", LOG_LEVEL_ALL);
89 LogComponentEnable("RadvdApplication", LOG_LEVEL_ALL);
91 }
92
93 NS_LOG_INFO("Create nodes.");
97
98 NodeContainer net1(n0, r);
99 NodeContainer net2(r, n1);
100 NodeContainer all(n0, r, n1);
101
102 NS_LOG_INFO("Create IPv6 Internet Stack");
103 InternetStackHelper internetv6;
104 internetv6.Install(all);
105
106 NS_LOG_INFO("Create channels.");
108 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
109 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
110 NetDeviceContainer d1 = csma.Install(net1); /* n0 - R */
111 NetDeviceContainer d2 = csma.Install(net2); /* R - n1 */
112
113 NS_LOG_INFO("Create networks and assign IPv6 Addresses.");
115
116 /* first subnet */
117 ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
119 tmp.Add(d1.Get(0)); /* n0 */
120 Ipv6InterfaceContainer iic1 = ipv6.AssignWithoutAddress(tmp); /* n0 interface */
121
123 tmp2.Add(d1.Get(1)); /* R */
125 ipv6.Assign(tmp2); /* R interface to the first subnet is just statically assigned */
126 iicr1.SetForwarding(0, true);
127 iic1.Add(iicr1);
128
129 /* add another IPv6 address for second prefix advertised on first subnet */
130 ipv6.SetBase(Ipv6Address("2001:ABCD::"), Ipv6Prefix(64));
131 ipv6.Assign(tmp2);
132
133 /* second subnet R - n1 */
134 ipv6.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
136 tmp3.Add(d2.Get(0)); /* R */
137 Ipv6InterfaceContainer iicr2 = ipv6.Assign(tmp3); /* R interface */
138 iicr2.SetForwarding(0, true);
139
141 tmp4.Add(d2.Get(1)); /* n1 */
143 iic2.Add(iicr2);
144
145 /* radvd configuration */
146 RadvdHelper radvdHelper;
147 /* R interface (n0 - R) */
148 radvdHelper.AddAnnouncedPrefix(iic1.GetInterfaceIndex(1), Ipv6Address("2001:ABCD::0"), 64);
149 radvdHelper.AddAnnouncedPrefix(iic1.GetInterfaceIndex(1), Ipv6Address("2001:1::0"), 64);
150
151 // Set some non-standard timers so the simulation is not taking ages
152 Ptr<RadvdInterface> routerInterface = radvdHelper.GetRadvdInterface(iic1.GetInterfaceIndex(1));
153 routerInterface->SetMaxRtrAdvInterval(2000);
154 routerInterface->SetMinRtrAdvInterval(1000);
155 RadvdInterface::RadvdPrefixList prefixList = routerInterface->GetPrefixes();
156 for (auto iter = prefixList.begin(); iter != prefixList.end(); iter++)
157 {
158 (*iter)->SetPreferredLifeTime(3);
159 (*iter)->SetValidLifeTime(5);
160 }
161
162 /* R interface (R - n1) */
163 radvdHelper.AddAnnouncedPrefix(iic2.GetInterfaceIndex(1), Ipv6Address("2001:2::0"), 64);
164
165 // Set some non-standard timers so the simulation is not taking ages
166 routerInterface = radvdHelper.GetRadvdInterface(iic2.GetInterfaceIndex(1));
167 routerInterface->SetMaxRtrAdvInterval(2000);
168 routerInterface->SetMinRtrAdvInterval(1000);
169 prefixList = routerInterface->GetPrefixes();
170 for (auto iter = prefixList.begin(); iter != prefixList.end(); iter++)
171 {
172 (*iter)->SetPreferredLifeTime(3);
173 (*iter)->SetValidLifeTime(5);
174 }
175
176 ApplicationContainer radvdApps = radvdHelper.Install(r);
177 radvdApps.Start(Seconds(1.0));
178 radvdApps.Stop(Seconds(2.0));
179
180 /* Create a Ping application to send ICMPv6 echo request from n0 to n1 via R */
181 uint32_t packetSize = 1024;
182 uint32_t maxPacketCount = 8;
183 PingHelper ping(
184 Ipv6Address("2001:2::200:ff:fe00:4")); /* should be n1 address after autoconfiguration */
185 // ping.SetIfIndex(iic1.GetInterfaceIndex(0));
186
187 ping.SetAttribute("Count", UintegerValue(maxPacketCount));
188 ping.SetAttribute("Size", UintegerValue(packetSize));
189 ApplicationContainer apps = ping.Install(net1.Get(0));
190 apps.Start(Seconds(2.0));
191 apps.Stop(Seconds(5.0));
192
193 Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>(&std::cout);
194 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(2.0), n0, routingStream);
195 Ipv6RoutingHelper::PrintRoutingTableAt(Seconds(10.0), n0, routingStream);
196
197 IpAddressHelper ipAddressHelper;
198 /* RA should be received, two prefixes + routes + default route should be present */
200 /* at the end, RA addresses and routes should be cleared */
202
203 AsciiTraceHelper ascii;
204 csma.EnableAsciiAll(ascii.CreateFileStream("radvd-two-prefix.tr"));
205 csma.EnablePcapAll(std::string("radvd-two-prefix"), true);
206
207 NS_LOG_INFO("Run Simulation.");
210 NS_LOG_INFO("Done.");
211
212 return 0;
213}
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.
ApplicationContainer Install(NodeContainer c)
Install an application on each node of the input container configured with all the attributes set wit...
Manage ASCII trace files for device models.
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.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
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.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition ipv6.h:71
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.
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:46
Create a ping application and associate it to a node.
Definition ping-helper.h:31
Smart pointer class similar to boost::intrusive_ptr.
Radvd application helper.
void AddAnnouncedPrefix(uint32_t interface, const Ipv6Address &prefix, uint32_t prefixLength)
Add a new prefix to be announced through an interface.
Ptr< RadvdInterface > GetRadvdInterface(uint32_t interface)
Get the low-level RadvdInterface specification for an interface.
std::list< Ptr< RadvdPrefix > > RadvdPrefixList
Container: Ptr to RadvdPrefix.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
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:291
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
bool verbose
static const uint32_t packetSize
Packet size generated at the AP.