A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
loose-routing-ipv6.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 */
8
9// Network topology
10// //
11// //
12// // +------------+
13// // +------------+ |---| Router 1 |---|
14// // | Host 0 |--| | [------------] |
15// // [------------] | | |
16// // | +------------+ |
17// // +--| | +------------+
18// // | Router 0 | | Router 2 |
19// // +--| | [------------]
20// // | [------------] |
21// // +------------+ | | |
22// // | Host 1 |--| | +------------+ |
23// // [------------] |---| Router 3 |---|
24// // [------------]
25// //
26// //
27// // - Tracing of queues and packet receptions to file "loose-routing-ipv6.tr"
28
29#include "ns3/core-module.h"
30#include "ns3/csma-module.h"
31#include "ns3/internet-apps-module.h"
32#include "ns3/internet-module.h"
33#include "ns3/ipv6-header.h"
34
35#include <fstream>
36
37using namespace ns3;
38
39NS_LOG_COMPONENT_DEFINE("LooseRoutingIpv6Example");
40
41int
42main(int argc, char** argv)
43{
44 bool verbose = false;
45
46 CommandLine cmd(__FILE__);
47 cmd.AddValue("verbose", "turn on log components", verbose);
48 cmd.Parse(argc, argv);
49
50 if (verbose)
51 {
52 // LogComponentEnable("Ipv6ExtensionLooseRouting", LOG_LEVEL_ALL);
53 LogComponentEnable("Ipv6Extension", LOG_LEVEL_ALL);
54 LogComponentEnable("Ipv6L3Protocol", LOG_LEVEL_ALL);
55 LogComponentEnable("Ipv6StaticRouting", LOG_LEVEL_ALL);
56 LogComponentEnable("Ipv6Interface", LOG_LEVEL_ALL);
57 LogComponentEnable("Ipv6Interface", LOG_LEVEL_ALL);
58 LogComponentEnable("NdiscCache", LOG_LEVEL_ALL);
59 }
60
61 // LogComponentEnable("Ping", LOG_LEVEL_INFO);
62
63 NS_LOG_INFO("Create nodes.");
70
71 NodeContainer net1(h0, r0);
72 NodeContainer net2(h1, r0);
73 NodeContainer net3(r0, r1);
74 NodeContainer net4(r1, r2);
75 NodeContainer net5(r2, r3);
76 NodeContainer net6(r3, r0);
77 NodeContainer all;
78 all.Add(h0);
79 all.Add(h1);
80 all.Add(r0);
81 all.Add(r1);
82 all.Add(r2);
83 all.Add(r3);
84
85 NS_LOG_INFO("Create IPv6 Internet Stack");
86 InternetStackHelper internetv6;
87 internetv6.Install(all);
88
89 NS_LOG_INFO("Create channels.");
91 csma.SetDeviceAttribute("Mtu", UintegerValue(1500));
92 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
93 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
94 NetDeviceContainer d1 = csma.Install(net1);
95 NetDeviceContainer d2 = csma.Install(net2);
96 NetDeviceContainer d3 = csma.Install(net3);
97 NetDeviceContainer d4 = csma.Install(net4);
98 NetDeviceContainer d5 = csma.Install(net5);
99 NetDeviceContainer d6 = csma.Install(net6);
100
101 NS_LOG_INFO("Create networks and assign IPv6 Addresses.");
103
104 ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
105 Ipv6InterfaceContainer i1 = ipv6.Assign(d1);
106 i1.SetForwarding(1, true);
108
109 ipv6.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
110 Ipv6InterfaceContainer i2 = ipv6.Assign(d2);
111 i2.SetForwarding(1, true);
113
114 ipv6.SetBase(Ipv6Address("2001:3::"), Ipv6Prefix(64));
115 Ipv6InterfaceContainer i3 = ipv6.Assign(d3);
116 i3.SetForwarding(0, true);
118 i3.SetForwarding(1, true);
120
121 ipv6.SetBase(Ipv6Address("2001:4::"), Ipv6Prefix(64));
122 Ipv6InterfaceContainer i4 = ipv6.Assign(d4);
123 i4.SetForwarding(0, true);
125 i4.SetForwarding(1, true);
127
128 ipv6.SetBase(Ipv6Address("2001:5::"), Ipv6Prefix(64));
129 Ipv6InterfaceContainer i5 = ipv6.Assign(d5);
130 i5.SetForwarding(0, true);
132 i5.SetForwarding(1, true);
134
135 ipv6.SetBase(Ipv6Address("2001:6::"), Ipv6Prefix(64));
136 Ipv6InterfaceContainer i6 = ipv6.Assign(d6);
137 i6.SetForwarding(0, true);
139 i6.SetForwarding(1, true);
141
142 NS_LOG_INFO("Create Applications.");
143
144 /**
145 * ICMPv6 Echo from h0 to h1 port 7
146 */
147 uint32_t packetSize = 1024;
148 uint32_t maxPacketCount = 1;
149 Time interPacketInterval = Seconds(1.0);
150
151 std::vector<Ipv6Address> routersAddress;
152 routersAddress.push_back(i3.GetAddress(1, 1));
153 routersAddress.push_back(i4.GetAddress(1, 1));
154 routersAddress.push_back(i5.GetAddress(1, 1));
155 routersAddress.push_back(i6.GetAddress(1, 1));
156 routersAddress.push_back(i2.GetAddress(0, 1));
157
158 // remote address is first routers in RH0 => source routing
159 PingHelper client(i1.GetAddress(1, 1));
160 client.SetAttribute("Count", UintegerValue(maxPacketCount));
161 client.SetAttribute("Interval", TimeValue(interPacketInterval));
162 client.SetAttribute("Size", UintegerValue(packetSize));
163 ApplicationContainer apps = client.Install(h0);
164 DynamicCast<Ping>(apps.Get(0))->SetRouters(routersAddress);
165
166 apps.Start(Seconds(1.0));
167 apps.Stop(Seconds(20.0));
168
169 AsciiTraceHelper ascii;
170 csma.EnableAsciiAll(ascii.CreateFileStream("loose-routing-ipv6.tr"));
171 csma.EnablePcapAll("loose-routing-ipv6", true);
172
173 NS_LOG_INFO("Run Simulation.");
176 NS_LOG_INFO("Done.");
177
178 return 0;
179}
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.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
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.
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.
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.
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.
void SetDefaultRouteInAllNodes(uint32_t router)
Set the default route for all the devices (except the router itself).
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Describes an IPv6 prefix.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Create a ping application and associate it to a node.
Definition ping-helper.h:31
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
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
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
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
bool verbose
static const uint32_t packetSize
Packet size generated at the AP.