A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-address-helper.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
8
9#include "ns3/assert.h"
10#include "ns3/ipv4-address-generator.h"
11#include "ns3/ipv4.h"
12#include "ns3/log.h"
13#include "ns3/loopback-net-device.h"
14#include "ns3/net-device-queue-interface.h"
15#include "ns3/net-device.h"
16#include "ns3/node.h"
17#include "ns3/ptr.h"
18#include "ns3/simulator.h"
19#include "ns3/traffic-control-helper.h"
20#include "ns3/traffic-control-layer.h"
21
22namespace ns3
23{
24
25NS_LOG_COMPONENT_DEFINE("Ipv4AddressHelper");
26
28{
30
31 //
32 // Set the default values to an illegal state. Do this so the client is
33 // forced to think at least briefly about what addresses get used and what
34 // is going on here.
35 //
36 m_network = 0xffffffff;
37 m_mask = 0;
38 m_address = 0xffffffff;
39 m_base = 0xffffffff;
40 m_shift = 0;
41 m_max = 0xffffffff;
42}
43
45 const Ipv4Mask mask,
46 const Ipv4Address address)
47{
49 SetBase(network, mask, address);
50}
51
52void
54 const Ipv4Mask mask,
55 const Ipv4Address address)
56{
58
59 m_network = network.Get();
60 m_mask = mask.Get();
61 m_base = m_address = address.Get();
62
63 //
64 // Some quick reasonableness testing.
65 //
67 "Ipv4AddressHelper::SetBase(): Inconsistent network and mask");
68
69 //
70 // Figure out how much to shift network numbers to get them aligned, and what
71 // the maximum allowed address is with respect to the current mask.
72 //
74 m_max = (1 << m_shift) - 2;
75
76 NS_ASSERT_MSG(m_shift <= 32, "Ipv4AddressHelper::SetBase(): Unreasonable address length");
77
78 //
79 // Shift the network down into the normalized position.
80 //
82
83 NS_LOG_LOGIC("m_network == " << m_network);
84 NS_LOG_LOGIC("m_mask == " << m_mask);
85 NS_LOG_LOGIC("m_address == " << m_address);
86}
87
90{
91 //
92 // The way this is expected to be used is that an address and network number
93 // are initialized, and then NewAddress() is called repeatedly to allocate and
94 // get new addresses on a given subnet. The client will expect that the first
95 // address she gets back is the one she used to initialize the generator with.
96 // This implies that this operation is a post-increment.
97 //
98 NS_ASSERT_MSG(m_address <= m_max, "Ipv4AddressHelper::NewAddress(): Address overflow");
99
101 ++m_address;
102 //
103 // The Ipv4AddressGenerator allows us to keep track of the addresses we have
104 // allocated and will assert if we accidentally generate a duplicate. This
105 // avoids some really hard to debug problems.
106 //
108 return addr;
109}
110
119
122{
125 for (uint32_t i = 0; i < c.GetN(); ++i)
126 {
127 Ptr<NetDevice> device = c.Get(i);
128
129 Ptr<Node> node = device->GetNode();
130 NS_ASSERT_MSG(node,
131 "Ipv4AddressHelper::Assign(): NetDevice is not not associated "
132 "with any node -> fail");
133
134 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
135 NS_ASSERT_MSG(ipv4,
136 "Ipv4AddressHelper::Assign(): NetDevice is associated"
137 " with a node without IPv4 stack installed -> fail "
138 "(maybe need to use InternetStackHelper?)");
139
140 int32_t interface = ipv4->GetInterfaceForDevice(device);
141 if (interface == -1)
142 {
143 interface = ipv4->AddInterface(device);
144 }
145 NS_ASSERT_MSG(interface >= 0,
146 "Ipv4AddressHelper::Assign(): "
147 "Interface index not found");
148
150 ipv4->AddAddress(interface, ipv4Addr);
151 ipv4->SetMetric(interface, 1);
152 ipv4->SetUp(interface);
153 retval.Add(ipv4, interface);
154
155 // Install the default traffic control configuration if the traffic
156 // control layer has been aggregated, if this is not
157 // a loopback interface, and there is no queue disc installed already
158 Ptr<TrafficControlLayer> tc = node->GetObject<TrafficControlLayer>();
159 if (tc && !DynamicCast<LoopbackNetDevice>(device) && !tc->GetRootQueueDiscOnDevice(device))
160 {
161 Ptr<NetDeviceQueueInterface> ndqi = device->GetObject<NetDeviceQueueInterface>();
162 // It is useless to install a queue disc if the device has no
163 // NetDeviceQueueInterface attached: the device queue is never
164 // stopped and every packet enqueued in the queue disc is
165 // immediately dequeued, hence there will never be backlog
166 if (ndqi)
167 {
168 std::size_t nTxQueues = ndqi->GetNTxQueues();
169 NS_LOG_LOGIC("Installing default traffic control configuration ("
170 << nTxQueues << " device queue(s))");
172 tcHelper.Install(device);
173 }
174 }
175 }
176 return retval;
177}
178
179const uint32_t N_BITS = 32; //!< number of bits in a IPv4 address
180
183{
185 for (uint32_t i = 0; i < N_BITS; ++i)
186 {
187 if (maskbits & 1)
188 {
189 NS_LOG_LOGIC("NumAddressBits -> " << i);
190 return i;
191 }
192 maskbits >>= 1;
193 }
194
195 NS_ASSERT_MSG(false, "Ipv4AddressHelper::NumAddressBits(): Bad Mask");
196 return 0;
197}
198
199} // namespace ns3
static bool AddAllocated(const Ipv4Address addr)
Add the Ipv4Address to the list of IPv4 entries.
uint32_t m_shift
shift, equivalent to the number of bits in the hostpart
uint32_t m_mask
network mask
uint32_t m_network
network address
uint32_t m_base
base address
Ipv4Address NewAddress()
Increment the IP address counter used to allocate IP addresses.
uint32_t m_max
maximum allowed address
Ipv4AddressHelper()
Construct a helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
uint32_t NumAddressBits(uint32_t maskbits) const
Returns the number of address bits (hostpart) for a given netmask.
Ipv4Address NewNetwork()
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ipv4 addresses are stored in host order in this class.
uint32_t Get() const
Get the host-order 32-bit IP address.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
a class to store IPv4 address information on an interface
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
a class to represent an Ipv4 address mask
uint32_t Get() const
Get the host-order 32-bit IP mask.
holds a vector of ns3::NetDevice pointers
uint32_t GetN() const
Get the number of Ptr<NetDevice> stored in this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Network device transmission queue interface.
Smart pointer class similar to boost::intrusive_ptr.
Build a set of QueueDisc objects.
QueueDiscContainer Install(NetDeviceContainer c)
static TrafficControlHelper Default(std::size_t nTxQueues=1)
The Traffic Control layer aims at introducing an equivalent of the Linux Traffic Control infrastructu...
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
const uint32_t N_BITS
number of bits in a IPv4 address