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 * 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
18#include "ipv4-address-helper.h"
19
20#include "ns3/assert.h"
21#include "ns3/ipv4-address-generator.h"
22#include "ns3/ipv4.h"
23#include "ns3/log.h"
24#include "ns3/loopback-net-device.h"
25#include "ns3/net-device-queue-interface.h"
26#include "ns3/net-device.h"
27#include "ns3/node.h"
28#include "ns3/ptr.h"
29#include "ns3/simulator.h"
30#include "ns3/traffic-control-helper.h"
31#include "ns3/traffic-control-layer.h"
32
33namespace ns3
34{
35
36NS_LOG_COMPONENT_DEFINE("Ipv4AddressHelper");
37
39{
41
42 //
43 // Set the default values to an illegal state. Do this so the client is
44 // forced to think at least briefly about what addresses get used and what
45 // is going on here.
46 //
47 m_network = 0xffffffff;
48 m_mask = 0;
49 m_address = 0xffffffff;
50 m_base = 0xffffffff;
51 m_shift = 0xffffffff;
52 m_max = 0xffffffff;
53}
54
56 const Ipv4Mask mask,
57 const Ipv4Address address)
58{
60 SetBase(network, mask, address);
61}
62
63void
65 const Ipv4Mask mask,
66 const Ipv4Address address)
67{
69
70 m_network = network.Get();
71 m_mask = mask.Get();
72 m_base = m_address = address.Get();
73
74 //
75 // Some quick reasonableness testing.
76 //
78 "Ipv4AddressHelper::SetBase(): Inconsistent network and mask");
79
80 //
81 // Figure out how much to shift network numbers to get them aligned, and what
82 // the maximum allowed address is with respect to the current mask.
83 //
85 m_max = (1 << m_shift) - 2;
86
87 NS_ASSERT_MSG(m_shift <= 32, "Ipv4AddressHelper::SetBase(): Unreasonable address length");
88
89 //
90 // Shift the network down into the normalized position.
91 //
93
94 NS_LOG_LOGIC("m_network == " << m_network);
95 NS_LOG_LOGIC("m_mask == " << m_mask);
96 NS_LOG_LOGIC("m_address == " << m_address);
97}
98
101{
102 //
103 // The way this is expected to be used is that an address and network number
104 // are initialized, and then NewAddress() is called repeatedly to allocate and
105 // get new addresses on a given subnet. The client will expect that the first
106 // address she gets back is the one she used to initialize the generator with.
107 // This implies that this operation is a post-increment.
108 //
109 NS_ASSERT_MSG(m_address <= m_max, "Ipv4AddressHelper::NewAddress(): Address overflow");
110
112 ++m_address;
113 //
114 // The Ipv4AddressGenerator allows us to keep track of the addresses we have
115 // allocated and will assert if we accidentally generate a duplicate. This
116 // avoids some really hard to debug problems.
117 //
119 return addr;
120}
121
124{
126 ++m_network;
128 return Ipv4Address(m_network << m_shift);
129}
130
133{
136 for (uint32_t i = 0; i < c.GetN(); ++i)
137 {
138 Ptr<NetDevice> device = c.Get(i);
139
140 Ptr<Node> node = device->GetNode();
141 NS_ASSERT_MSG(node,
142 "Ipv4AddressHelper::Assign(): NetDevice is not not associated "
143 "with any node -> fail");
144
145 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
146 NS_ASSERT_MSG(ipv4,
147 "Ipv4AddressHelper::Assign(): NetDevice is associated"
148 " with a node without IPv4 stack installed -> fail "
149 "(maybe need to use InternetStackHelper?)");
150
151 int32_t interface = ipv4->GetInterfaceForDevice(device);
152 if (interface == -1)
153 {
154 interface = ipv4->AddInterface(device);
155 }
156 NS_ASSERT_MSG(interface >= 0,
157 "Ipv4AddressHelper::Assign(): "
158 "Interface index not found");
159
161 ipv4->AddAddress(interface, ipv4Addr);
162 ipv4->SetMetric(interface, 1);
163 ipv4->SetUp(interface);
164 retval.Add(ipv4, interface);
165
166 // Install the default traffic control configuration if the traffic
167 // control layer has been aggregated, if this is not
168 // a loopback interface, and there is no queue disc installed already
169 Ptr<TrafficControlLayer> tc = node->GetObject<TrafficControlLayer>();
170 if (tc && !DynamicCast<LoopbackNetDevice>(device) && !tc->GetRootQueueDiscOnDevice(device))
171 {
172 Ptr<NetDeviceQueueInterface> ndqi = device->GetObject<NetDeviceQueueInterface>();
173 // It is useless to install a queue disc if the device has no
174 // NetDeviceQueueInterface attached: the device queue is never
175 // stopped and every packet enqueued in the queue disc is
176 // immediately dequeued, hence there will never be backlog
177 if (ndqi)
178 {
179 std::size_t nTxQueues = ndqi->GetNTxQueues();
180 NS_LOG_LOGIC("Installing default traffic control configuration ("
181 << nTxQueues << " device queue(s))");
183 tcHelper.Install(device);
184 }
185 }
186 }
187 return retval;
188}
189
190const uint32_t N_BITS = 32;
191
194{
196 for (uint32_t i = 0; i < N_BITS; ++i)
197 {
198 if (maskbits & 1)
199 {
200 NS_LOG_LOGIC("NumAddressBits -> " << i);
201 return i;
202 }
203 maskbits >>= 1;
204 }
205
206 NS_ASSERT_MSG(false, "Ipv4AddressHelper::NumAddressBits(): Bad Mask");
207 return 0;
208}
209
210} // 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.
Definition: ipv4-address.h:42
uint32_t Get() const
Get the host-order 32-bit IP address.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
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
Definition: ipv4-address.h:257
uint32_t Get() const
Get the host-order 32-bit IP mask.
Definition: ipv4-address.cc:84
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.
Definition: ptr.h:77
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:86
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
const uint32_t N_BITS
number of bits in a IPv4 address