A Discrete-Event Network Simulator
API
loose-routing-ipv6.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  */
19 
20 // Network topology
21 // //
22 // //
23 // // +------------+
24 // // +------------+ |---| Router 1 |---|
25 // // | Host 0 |--| | [------------] |
26 // // [------------] | | |
27 // // | +------------+ |
28 // // +--| | +------------+
29 // // | Router 0 | | Router 2 |
30 // // +--| | [------------]
31 // // | [------------] |
32 // // +------------+ | | |
33 // // | Host 1 |--| | +------------+ |
34 // // [------------] |---| Router 3 |---|
35 // // [------------]
36 // //
37 // //
38 // // - Tracing of queues and packet receptions to file "loose-routing-ipv6.tr"
39 
40 #include "ns3/core-module.h"
41 #include "ns3/csma-module.h"
42 #include "ns3/internet-apps-module.h"
43 #include "ns3/internet-module.h"
44 #include "ns3/ipv6-header.h"
45 
46 #include <fstream>
47 
48 using namespace ns3;
49 
50 NS_LOG_COMPONENT_DEFINE("LooseRoutingIpv6Example");
51 
52 int
53 main(int argc, char** argv)
54 {
55  bool verbose = false;
56 
57  CommandLine cmd(__FILE__);
58  cmd.AddValue("verbose", "turn on log components", verbose);
59  cmd.Parse(argc, argv);
60 
61  if (verbose)
62  {
63  // LogComponentEnable("Ipv6ExtensionLooseRouting", LOG_LEVEL_ALL);
64  LogComponentEnable("Ipv6Extension", LOG_LEVEL_ALL);
65  LogComponentEnable("Ipv6L3Protocol", LOG_LEVEL_ALL);
66  LogComponentEnable("Ipv6StaticRouting", LOG_LEVEL_ALL);
67  LogComponentEnable("Ipv6Interface", LOG_LEVEL_ALL);
68  LogComponentEnable("Ipv6Interface", LOG_LEVEL_ALL);
69  LogComponentEnable("NdiscCache", LOG_LEVEL_ALL);
70  }
71 
72  // LogComponentEnable("Ping", LOG_LEVEL_INFO);
73 
74  NS_LOG_INFO("Create nodes.");
75  Ptr<Node> h0 = CreateObject<Node>();
76  Ptr<Node> h1 = CreateObject<Node>();
77  Ptr<Node> r0 = CreateObject<Node>();
78  Ptr<Node> r1 = CreateObject<Node>();
79  Ptr<Node> r2 = CreateObject<Node>();
80  Ptr<Node> r3 = CreateObject<Node>();
81 
82  NodeContainer net1(h0, r0);
83  NodeContainer net2(h1, r0);
84  NodeContainer net3(r0, r1);
85  NodeContainer net4(r1, r2);
86  NodeContainer net5(r2, r3);
87  NodeContainer net6(r3, r0);
88  NodeContainer all;
89  all.Add(h0);
90  all.Add(h1);
91  all.Add(r0);
92  all.Add(r1);
93  all.Add(r2);
94  all.Add(r3);
95 
96  NS_LOG_INFO("Create IPv6 Internet Stack");
97  InternetStackHelper internetv6;
98  internetv6.Install(all);
99 
100  NS_LOG_INFO("Create channels.");
102  csma.SetDeviceAttribute("Mtu", UintegerValue(1500));
103  csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
104  csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
105  NetDeviceContainer d1 = csma.Install(net1);
106  NetDeviceContainer d2 = csma.Install(net2);
107  NetDeviceContainer d3 = csma.Install(net3);
108  NetDeviceContainer d4 = csma.Install(net4);
109  NetDeviceContainer d5 = csma.Install(net5);
110  NetDeviceContainer d6 = csma.Install(net6);
111 
112  NS_LOG_INFO("Create networks and assign IPv6 Addresses.");
113  Ipv6AddressHelper ipv6;
114 
115  ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
116  Ipv6InterfaceContainer i1 = ipv6.Assign(d1);
117  i1.SetForwarding(1, true);
119 
120  ipv6.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
121  Ipv6InterfaceContainer i2 = ipv6.Assign(d2);
122  i2.SetForwarding(1, true);
124 
125  ipv6.SetBase(Ipv6Address("2001:3::"), Ipv6Prefix(64));
126  Ipv6InterfaceContainer i3 = ipv6.Assign(d3);
127  i3.SetForwarding(0, true);
129  i3.SetForwarding(1, true);
131 
132  ipv6.SetBase(Ipv6Address("2001:4::"), Ipv6Prefix(64));
133  Ipv6InterfaceContainer i4 = ipv6.Assign(d4);
134  i4.SetForwarding(0, true);
136  i4.SetForwarding(1, true);
138 
139  ipv6.SetBase(Ipv6Address("2001:5::"), Ipv6Prefix(64));
140  Ipv6InterfaceContainer i5 = ipv6.Assign(d5);
141  i5.SetForwarding(0, true);
143  i5.SetForwarding(1, true);
145 
146  ipv6.SetBase(Ipv6Address("2001:6::"), Ipv6Prefix(64));
147  Ipv6InterfaceContainer i6 = ipv6.Assign(d6);
148  i6.SetForwarding(0, true);
150  i6.SetForwarding(1, true);
152 
153  NS_LOG_INFO("Create Applications.");
154 
158  uint32_t packetSize = 1024;
159  uint32_t maxPacketCount = 1;
160  Time interPacketInterval = Seconds(1.0);
161 
162  std::vector<Ipv6Address> routersAddress;
163  routersAddress.push_back(i3.GetAddress(1, 1));
164  routersAddress.push_back(i4.GetAddress(1, 1));
165  routersAddress.push_back(i5.GetAddress(1, 1));
166  routersAddress.push_back(i6.GetAddress(1, 1));
167  routersAddress.push_back(i2.GetAddress(0, 1));
168 
169  // remote address is first routers in RH0 => source routing
170  PingHelper client(i1.GetAddress(1, 1));
171  client.SetAttribute("Count", UintegerValue(maxPacketCount));
172  client.SetAttribute("Interval", TimeValue(interPacketInterval));
173  client.SetAttribute("Size", UintegerValue(packetSize));
174  ApplicationContainer apps = client.Install(h0);
175  DynamicCast<Ping>(apps.Get(0))->SetRouters(routersAddress);
176 
177  apps.Start(Seconds(1.0));
178  apps.Stop(Seconds(20.0));
179 
180  AsciiTraceHelper ascii;
181  csma.EnableAsciiAll(ascii.CreateFileStream("loose-routing-ipv6.tr"));
182  csma.EnablePcapAll("loose-routing-ipv6", true);
183 
184  NS_LOG_INFO("Run Simulation.");
185  Simulator::Run();
187  NS_LOG_INFO("Done.");
188 
189  return 0;
190 }
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.
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.
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
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.
Definition: ipv6-address.h:455
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:48
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
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.