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/enum.h"
73#include "ns3/internet-apps-module.h"
74#include "ns3/internet-module.h"
75#include "ns3/mobility-module.h"
76#include "ns3/network-module.h"
77#include "ns3/point-to-point-module.h"
78#include "ns3/ssid.h"
79#include "ns3/wifi-helper.h"
80#include "ns3/yans-wifi-helper.h"
81
82using namespace ns3;
83
84NS_LOG_COMPONENT_DEFINE("Dhcp6Example");
85
86/**
87 * @brief Brings down an IPv6 interface on a given node.
88 *
89 * Retrieves the Ipv6 object aggregated to the specified node and marks
90 * the given interface as down, disabling it from sending or receiving packets.
91 *
92 * @param node Pointer to the ns-3 Node whose interface will be set down.
93 * @param interface Index of the IPv6 interface to disable.
94 */
95void
97{
98 node->GetObject<Ipv6>()->SetDown(interface);
99}
100
101/**
102 * @brief Brings up an IPv6 interface on a given node.
103 *
104 * Retrieves the Ipv6 object aggregated to the specified node and marks
105 * the given interface as up, enabling it to send and receive packets.
106 *
107 * @param node Pointer to the ns-3 Node whose interface will be set up.
108 * @param interface Index of the IPv6 interface to enable.
109 */
110void
112{
113 node->GetObject<Ipv6>()->SetUp(interface);
114}
115
116int
117main(int argc, char* argv[])
118{
119 CommandLine cmd(__FILE__);
120
121 bool verbose = false;
122 bool enablePcap = false;
123 cmd.AddValue("verbose", "Turn on the logs", verbose);
124 cmd.AddValue("enablePcap", "Enable/Disable pcap file generation", enablePcap);
125
126 cmd.Parse(argc, argv);
127 GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
128
129 if (verbose)
130 {
131 LogComponentEnable("Dhcp6Server", LOG_LEVEL_INFO);
132 LogComponentEnable("Dhcp6Client", LOG_LEVEL_INFO);
133 }
134
135 Time stopTime = Seconds(25.0);
136
137 NS_LOG_INFO("Create nodes.");
138 NodeContainer nonRouterNodes;
139 nonRouterNodes.Create(4);
140 Ptr<Node> router = CreateObject<Node>();
141 NodeContainer all(nonRouterNodes, router);
142
143 NS_LOG_INFO("Create channels.");
145 csma.SetChannelAttribute("DataRate", StringValue("5Mbps"));
146 csma.SetChannelAttribute("Delay", StringValue("2ms"));
147 NetDeviceContainer devices = csma.Install(all); // all nodes
148
149 InternetStackHelper internetv6;
150 internetv6.Install(all);
151
152 NS_LOG_INFO("Create networks and assign IPv6 Addresses.");
153
155 ipv6.SetBase(Ipv6Address("2001:cafe::"), Ipv6Prefix(64));
156 NetDeviceContainer nonRouterDevices;
157 nonRouterDevices.Add(devices.Get(0)); // The server node, S0.
158 nonRouterDevices.Add(devices.Get(1)); // The first client node, N0.
159 nonRouterDevices.Add(devices.Get(2)); // The second client node, N1.
160 nonRouterDevices.Add(devices.Get(3)); // The third client node, N2.
161 Ipv6InterfaceContainer i = ipv6.AssignWithoutAddress(nonRouterDevices);
162
163 NS_LOG_INFO("Assign static IP address to the third node.");
164 Ptr<Ipv6> ipv6proto = nonRouterNodes.Get(3)->GetObject<Ipv6>();
165 int32_t ifIndex = ipv6proto->GetInterfaceForDevice(devices.Get(3));
166 Ipv6InterfaceAddress ipv6Addr =
167 Ipv6InterfaceAddress(Ipv6Address("2001:cafe::42:2"), Ipv6Prefix(128));
168 ipv6proto->AddAddress(ifIndex, ipv6Addr);
169
170 NS_LOG_INFO("Assign static IP address to the router node.");
171 NetDeviceContainer routerDevice;
172 routerDevice.Add(devices.Get(4)); // CSMA interface of the node R0.
173 Ipv6InterfaceContainer r1 = ipv6.Assign(routerDevice);
174 r1.SetForwarding(0, true);
175
176 NS_LOG_INFO("Create Radvd applications.");
177 RadvdHelper radvdHelper;
178
179 /* Set up unsolicited RAs */
180 radvdHelper.AddAnnouncedPrefix(r1.GetInterfaceIndex(0), Ipv6Address("2001:cafe::1"), 64);
181 radvdHelper.GetRadvdInterface(r1.GetInterfaceIndex(0))->SetManagedFlag(true);
182
183 NS_LOG_INFO("Create DHCP applications.");
184 Dhcp6Helper dhcp6Helper;
185
186 NS_LOG_INFO("Set timers to desired values.");
187 dhcp6Helper.SetServerAttribute("RenewTime", StringValue("10s"));
188 dhcp6Helper.SetServerAttribute("RebindTime", StringValue("16s"));
189 dhcp6Helper.SetServerAttribute("PreferredLifetime", StringValue("18s"));
190 dhcp6Helper.SetServerAttribute("ValidLifetime", StringValue("20s"));
191 // We set an arbitrary DUID type for the server.
192 // There is no universal standard for the server's DUID type.
193 dhcp6Helper.SetServerAttribute("DuidType", StringValue("LLT"));
194 // We set an arbitrary DUID type for the client.
195 // There is no universal standard for the client's DUID type,
196 // but usually OSes setup the DUID during the OS installation.
197 dhcp6Helper.SetClientAttribute("DuidType", StringValue("UUID"));
198
199 // DHCPv6 clients
200 NodeContainer nodes = NodeContainer(nonRouterNodes.Get(1), nonRouterNodes.Get(2));
201 ApplicationContainer dhcpClients = dhcp6Helper.InstallDhcp6Client(nodes);
202 dhcpClients.Start(Seconds(1.0));
203 dhcpClients.Stop(stopTime);
204
205 // DHCP server
206 NetDeviceContainer serverNetDevices;
207 serverNetDevices.Add(nonRouterDevices.Get(0));
208 ApplicationContainer dhcpServerApp = dhcp6Helper.InstallDhcp6Server(serverNetDevices);
209
211 server->AddSubnet(Ipv6Address("2001:cafe::"),
212 Ipv6Prefix(64),
213 Ipv6Address("2001:cafe::42:1"),
214 Ipv6Address("2001:cafe::42:ffff"));
215
216 dhcpServerApp.Start(Seconds(0.0));
217 dhcpServerApp.Stop(stopTime);
218
219 ApplicationContainer radvdApps = radvdHelper.Install(router);
220 radvdApps.Start(Seconds(1.0));
221 radvdApps.Stop(stopTime);
222
224
225 // Schedule N0 interface to go down and come up to see the effects of
226 // link state change.
227 Simulator::Schedule(Seconds(4), &SetInterfaceDown, nonRouterNodes.Get(1), 1);
228 Simulator::Schedule(Seconds(5), &SetInterfaceUp, nonRouterNodes.Get(1), 1);
229
230 if (enablePcap)
231 {
232 csma.EnablePcapAll("dhcp6-csma");
233 }
234
235 NS_LOG_INFO("Run Simulation.");
237
238 NS_LOG_INFO("Done.");
240 return 0;
241}
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.
void SetClientAttribute(std::string name, const AttributeValue &value)
Set DHCPv6 client 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:518
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
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.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void Run()
Run the simulation.
Definition simulator.cc:161
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
void SetInterfaceUp(Ptr< Node > node, uint32_t interface)
Brings up an IPv6 interface on a given node.
void SetInterfaceDown(Ptr< Node > node, uint32_t interface)
Brings down an IPv6 interface on a given node.
Time stopTime
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:267
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1273
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:279
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:605
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:96
bool verbose
void SetUp(char *deviceName)