A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dhcp6-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 NITK Surathkal
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Kavya Bhat <kavyabhat@gmail.com>
7 *
8 */
9
10/*
11 * Network layout:
12 * The following devices have one CSMA interface each -
13 * S0 - DHCPv6 server
14 * N0, N1 - DHCPv6 clients
15 * R0 - router
16 * ┌-------------------------------------------------┐
17 * | DHCPv6 Clients |
18 * | |
19 * | Static address |
20 * | 2001:cafe::42:2 |
21 * | ┌──────┐ ┌──────┐ ┌──────┐ |
22 * | │ N0 │ │ N1 │ │ N2 │ |
23 * | └──────┘ └──────┘ └──────┘ |
24 * | │ │ │ |
25 * └-------│--------------│---------------│----------┘
26 * DHCPv6 Server │ │ │
27 * ┌──────┐ │ │ │ ┌──────┐
28 * │ S0 │────────┴──────────────┴───────────────┴──────│ R0 │Router
29 * └──────┘ └──────┘
30 * Notes:
31 * 1. The DHCPv6 server is not assigned any static address as it operates only
32 * in the link-local domain.
33 * 2. N2 has a statically assigned address to demonstrate the operation of the
34 * DHCPv6 Decline message.
35 * 3. The server is usually on the router in practice, but we demonstrate in
36 * this example a standalone server.
37 * 4. Linux uses fairly large values for address lifetimes (in thousands of
38 * seconds). In this example, we have set shorter lifetimes for the purpose
39 * of observing the Renew messages within a shorter simulation run.
40 * 5. The nodes use two interfaces each for the purpose of demonstrating
41 * DHCPv6 operation when multiple interfaces are present on the client or
42 * server nodes.
43 *
44 * This example demonstrates how to set up a simple DHCPv6 server and two DHCPv6
45 * clients. The clients begin to request an address lease using a Solicit
46 * message only after receiving a Router Advertisement containing the 'M' bit
47 * from the router, R0.
48 *
49 * The server responds with an Advertise message with all available address
50 * offers, and the client sends a Request message to the server for these
51 * addresses. The server then sends a Reply message to the client, which
52 * performs Duplicate Address Detection to check if any other node on the link
53 * already uses this address.
54 * If the address is in use by any other node, the client sends a Decline
55 * message to the server. If the address is not in use, the client begins using
56 * this address.
57 * At the end of the address lease lifetime, the client sends a Renew message
58 * to the server, which renews the lease and allows the client to continue using
59 * the same address.
60 *
61 * The user may enable packet traces in this example to observe the following
62 * message exchanges:
63 * 1. Solicit - Advertise - Request - Reply
64 * 2. Solicit - Advertise - Request - Reply - Decline
65 * 3. Renew - Reply
66 *
67 */
68
69#include "ns3/applications-module.h"
70#include "ns3/core-module.h"
71#include "ns3/csma-module.h"
72#include "ns3/internet-apps-module.h"
73#include "ns3/internet-module.h"
74#include "ns3/mobility-module.h"
75#include "ns3/network-module.h"
76#include "ns3/point-to-point-module.h"
77#include "ns3/ssid.h"
78#include "ns3/wifi-helper.h"
79#include "ns3/yans-wifi-helper.h"
80
81using namespace ns3;
82
83NS_LOG_COMPONENT_DEFINE("Dhcp6Example");
84
85void
87{
88 node->GetObject<Ipv6>()->SetDown(interface);
89}
90
91void
93{
94 node->GetObject<Ipv6>()->SetUp(interface);
95}
96
97int
98main(int argc, char* argv[])
99{
100 CommandLine cmd(__FILE__);
101
102 bool verbose = false;
103 bool enablePcap = false;
104 cmd.AddValue("verbose", "Turn on the logs", verbose);
105 cmd.AddValue("enablePcap", "Enable/Disable pcap file generation", enablePcap);
106
107 cmd.Parse(argc, argv);
108 GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
109
110 if (verbose)
111 {
112 LogComponentEnable("Dhcp6Server", LOG_LEVEL_INFO);
113 LogComponentEnable("Dhcp6Client", LOG_LEVEL_INFO);
114 }
115
116 Time stopTime = Seconds(25.0);
117
118 NS_LOG_INFO("Create nodes.");
119 NodeContainer nonRouterNodes;
120 nonRouterNodes.Create(4);
121 Ptr<Node> router = CreateObject<Node>();
122 NodeContainer all(nonRouterNodes, router);
123
124 NS_LOG_INFO("Create channels.");
126 csma.SetChannelAttribute("DataRate", StringValue("5Mbps"));
127 csma.SetChannelAttribute("Delay", StringValue("2ms"));
128 NetDeviceContainer devices = csma.Install(all); // all nodes
129
130 InternetStackHelper internetv6;
131 internetv6.Install(all);
132
133 NS_LOG_INFO("Create networks and assign IPv6 Addresses.");
134
136 ipv6.SetBase(Ipv6Address("2001:cafe::"), Ipv6Prefix(64));
137 NetDeviceContainer nonRouterDevices;
138 nonRouterDevices.Add(devices.Get(0)); // The server node, S0.
139 nonRouterDevices.Add(devices.Get(1)); // The first client node, N0.
140 nonRouterDevices.Add(devices.Get(2)); // The second client node, N1.
141 nonRouterDevices.Add(devices.Get(3)); // The third client node, N2.
142 Ipv6InterfaceContainer i = ipv6.AssignWithoutAddress(nonRouterDevices);
143
144 NS_LOG_INFO("Assign static IP address to the third node.");
145 Ptr<Ipv6> ipv6proto = nonRouterNodes.Get(3)->GetObject<Ipv6>();
146 int32_t ifIndex = ipv6proto->GetInterfaceForDevice(devices.Get(3));
147 Ipv6InterfaceAddress ipv6Addr =
148 Ipv6InterfaceAddress(Ipv6Address("2001:cafe::42:2"), Ipv6Prefix(128));
149 ipv6proto->AddAddress(ifIndex, ipv6Addr);
150
151 NS_LOG_INFO("Assign static IP address to the router node.");
152 NetDeviceContainer routerDevice;
153 routerDevice.Add(devices.Get(4)); // CSMA interface of the node R0.
154 Ipv6InterfaceContainer r1 = ipv6.Assign(routerDevice);
155 r1.SetForwarding(0, true);
156
157 NS_LOG_INFO("Create Radvd applications.");
158 RadvdHelper radvdHelper;
159
160 /* Set up unsolicited RAs */
161 radvdHelper.AddAnnouncedPrefix(r1.GetInterfaceIndex(0), Ipv6Address("2001:cafe::1"), 64);
162 radvdHelper.GetRadvdInterface(r1.GetInterfaceIndex(0))->SetManagedFlag(true);
163
164 NS_LOG_INFO("Create DHCP applications.");
165 Dhcp6Helper dhcp6Helper;
166
167 NS_LOG_INFO("Set timers to desired values.");
168 dhcp6Helper.SetServerAttribute("RenewTime", StringValue("10s"));
169 dhcp6Helper.SetServerAttribute("RebindTime", StringValue("16s"));
170 dhcp6Helper.SetServerAttribute("PreferredLifetime", StringValue("18s"));
171 dhcp6Helper.SetServerAttribute("ValidLifetime", StringValue("20s"));
172
173 // DHCPv6 clients
174 NodeContainer nodes = NodeContainer(nonRouterNodes.Get(1), nonRouterNodes.Get(2));
175 ApplicationContainer dhcpClients = dhcp6Helper.InstallDhcp6Client(nodes);
176 dhcpClients.Start(Seconds(1.0));
177 dhcpClients.Stop(stopTime);
178
179 // DHCP server
180 NetDeviceContainer serverNetDevices;
181 serverNetDevices.Add(nonRouterDevices.Get(0));
182 ApplicationContainer dhcpServerApp = dhcp6Helper.InstallDhcp6Server(serverNetDevices);
183
185 server->AddSubnet(Ipv6Address("2001:cafe::"),
186 Ipv6Prefix(64),
187 Ipv6Address("2001:cafe::42:1"),
188 Ipv6Address("2001:cafe::42:ffff"));
189
190 dhcpServerApp.Start(Seconds(0.0));
191 dhcpServerApp.Stop(stopTime);
192
193 ApplicationContainer radvdApps = radvdHelper.Install(router);
194 radvdApps.Start(Seconds(1.0));
195 radvdApps.Stop(stopTime);
196
198
199 // Schedule N0 interface to go down and come up to see the effects of
200 // link state change.
201 Simulator::Schedule(Seconds(4), &SetInterfaceDown, nonRouterNodes.Get(1), 1);
202 Simulator::Schedule(Seconds(5), &SetInterfaceUp, nonRouterNodes.Get(1), 1);
203
204 if (enablePcap)
205 {
206 csma.EnablePcapAll("dhcp6-csma");
207 }
208
209 NS_LOG_INFO("Run Simulation.");
211
212 NS_LOG_INFO("Done.");
214 return 0;
215}
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.
ApplicationContainer Install(NodeContainer c)
Install an application on each node of the input container configured with all the attributes set wit...
AttributeValue implementation for Boolean.
Definition boolean.h:26
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
The helper class used to configure and install DHCPv6 applications on nodes.
void SetServerAttribute(std::string name, const AttributeValue &value)
Set DHCPv6 server attributes.
ApplicationContainer InstallDhcp6Client(NodeContainer clientNodes) const
Install DHCPv6 client on a set of nodes.
ApplicationContainer InstallDhcp6Server(NetDeviceContainer netDevices)
Install DHCPv6 server on a node / NetDevice.
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
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
IPv6 address associated with an interface.
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.
Describes an IPv6 prefix.
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.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
Smart pointer class similar to boost::intrusive_ptr.
Radvd application helper.
void AddAnnouncedPrefix(uint32_t interface, const Ipv6Address &prefix, uint32_t prefixLength, bool slaac=true)
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.
void SetManagedFlag(bool managedFlag)
Set managed flag.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:561
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
void SetInterfaceUp(Ptr< Node > node, uint32_t interface)
void SetInterfaceDown(Ptr< Node > node, uint32_t interface)
Time stopTime
#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:1345
NodeContainer nodes
devices
Definition first.py:31
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_INFO
LOG_INFO and above.
Definition log.h:93
bool verbose
void SetUp(char *deviceName)