A Discrete-Event Network Simulator
API
ipv4-address-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include "ns3/assert.h"
20 #include "ns3/log.h"
21 #include "ns3/ptr.h"
22 #include "ns3/node.h"
23 #include "ns3/net-device.h"
24 #include "ns3/loopback-net-device.h"
25 #include "ns3/ipv4.h"
26 #include "ns3/ipv4-address-generator.h"
27 #include "ns3/simulator.h"
28 #include "ns3/traffic-control-helper.h"
29 #include "ns3/traffic-control-layer.h"
30 #include "ns3/net-device-queue-interface.h"
31 #include "ipv4-address-helper.h"
32 
33 namespace ns3 {
34 
35 NS_LOG_COMPONENT_DEFINE ("Ipv4AddressHelper");
36 
38 {
40 
41 //
42 // Set the default values to an illegal state. Do this so the client is
43 // forced to think at least briefly about what addresses get used and what
44 // is going on here.
45 //
46  m_network = 0xffffffff;
47  m_mask = 0;
48  m_address = 0xffffffff;
49  m_base = 0xffffffff;
50  m_shift = 0xffffffff;
51  m_max = 0xffffffff;
52 }
53 
55  const Ipv4Address network,
56  const Ipv4Mask mask,
57  const Ipv4Address address)
58 {
60  SetBase (network, mask, address);
61 }
62 
63 void
65  const Ipv4Address network,
66  const Ipv4Mask mask,
67  const Ipv4Address address)
68 {
70 
71  m_network = network.Get ();
72  m_mask = mask.Get ();
73  m_base = m_address = address.Get ();
74 
75 //
76 // Some quick reasonableness testing.
77 //
78  NS_ASSERT_MSG ((m_network & ~m_mask) == 0,
79  "Ipv4AddressHelper::SetBase(): Inconsistent network and mask");
80 
81 //
82 // Figure out how much to shift network numbers to get them aligned, and what
83 // the maximum allowed address is with respect to the current mask.
84 //
86  m_max = (1 << m_shift) - 2;
87 
88  NS_ASSERT_MSG (m_shift <= 32,
89  "Ipv4AddressHelper::SetBase(): Unreasonable address length");
90 
91 //
92 // Shift the network down into the normalized position.
93 //
94  m_network >>= m_shift;
95 
96  NS_LOG_LOGIC ("m_network == " << m_network);
97  NS_LOG_LOGIC ("m_mask == " << m_mask);
98  NS_LOG_LOGIC ("m_address == " << m_address);
99 }
100 
103 {
104 //
105 // The way this is expected to be used is that an address and network number
106 // are initialized, and then NewAddress() is called repeatedly to allocate and
107 // get new addresses on a given subnet. The client will expect that the first
108 // address she gets back is the one she used to initialize the generator with.
109 // This implies that this operation is a post-increment.
110 //
112  "Ipv4AddressHelper::NewAddress(): Address overflow");
113 
114  Ipv4Address addr ((m_network << m_shift) | m_address);
115  ++m_address;
116 //
117 // The Ipv4AddressGenerator allows us to keep track of the addresses we have
118 // allocated and will assert if we accidentally generate a duplicate. This
119 // avoids some really hard to debug problems.
120 //
122  return addr;
123 }
124 
127 {
129  ++m_network;
130  m_address = m_base;
131  return Ipv4Address (m_network << m_shift);
132 }
133 
136 {
138  Ipv4InterfaceContainer retval;
139  for (uint32_t i = 0; i < c.GetN (); ++i) {
140  Ptr<NetDevice> device = c.Get (i);
141 
142  Ptr<Node> node = device->GetNode ();
143  NS_ASSERT_MSG (node, "Ipv4AddressHelper::Assign(): NetDevice is not not associated "
144  "with any node -> fail");
145 
146  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
147  NS_ASSERT_MSG (ipv4, "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, "Ipv4AddressHelper::Assign(): "
157  "Interface index not found");
158 
160  ipv4->AddAddress (interface, ipv4Addr);
161  ipv4->SetMetric (interface, 1);
162  ipv4->SetUp (interface);
163  retval.Add (ipv4, interface);
164 
165  // Install the default traffic control configuration if the traffic
166  // control layer has been aggregated, if this is not
167  // a loopback interface, and there is no queue disc installed already
169  if (tc && DynamicCast<LoopbackNetDevice> (device) == 0 && tc->GetRootQueueDiscOnDevice (device) == 0)
170  {
171  Ptr<NetDeviceQueueInterface> ndqi = device->GetObject<NetDeviceQueueInterface> ();
172  // It is useless to install a queue disc if the device has no
173  // NetDeviceQueueInterface attached: the device queue is never
174  // stopped and every packet enqueued in the queue disc is
175  // immediately dequeued, hence there will never be backlog
176  if (ndqi)
177  {
178  std::size_t nTxQueues = ndqi->GetNTxQueues ();
179  NS_LOG_LOGIC ("Installing default traffic control configuration ("
180  << nTxQueues << " device queue(s))");
182  tcHelper.Install (device);
183  }
184  }
185  }
186  return retval;
187 }
188 
189 const uint32_t N_BITS = 32;
190 
191 uint32_t
192 Ipv4AddressHelper::NumAddressBits (uint32_t maskbits) const
193 {
195  for (uint32_t i = 0; i < N_BITS; ++i)
196  {
197  if (maskbits & 1)
198  {
199  NS_LOG_LOGIC ("NumAddressBits -> " << i);
200  return i;
201  }
202  maskbits >>= 1;
203  }
204 
205  NS_ASSERT_MSG (false, "Ipv4AddressHelper::NumAddressBits(): Bad Mask");
206  return 0;
207 }
208 
209 } // namespace ns3
210 
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
uint32_t m_max
maximum allowed address
QueueDiscContainer Install(NetDeviceContainer c)
holds a vector of std::pair of Ptr<Ipv4> and interface index.
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
Introspection did not find any typical Config paths.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
uint32_t m_mask
network mask
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
uint32_t m_base
base address
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
uint32_t m_shift
shift, equivalent to the number of bits in the hostpart
static bool AddAllocated(const Ipv4Address addr)
Add the Ipv4Address to the list of IPv4 entries.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Ipv4Address NewAddress(void)
Increment the IP address counter used to allocate IP addresses.
Ipv4AddressHelper()
Construct a helper class to make life easier while doing simple IPv4 address assignment in scripts...
const uint32_t N_BITS
number of bits in a IPv4 address
holds a vector of ns3::NetDevice pointers
uint32_t m_network
network address
Build a set of QueueDisc objects.
Network device transmission queue interface.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Every class exported by the ns3 library is enclosed in the ns3 namespace.
address
Definition: first.py:37
static TrafficControlHelper Default(std::size_t nTxQueues=1)
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
uint32_t NumAddressBits(uint32_t maskbits) const
Returns the number of address bits (hostpart) for a given netmask.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
uint32_t Get(void) const
Get the host-order 32-bit IP mask.
a class to store IPv4 address information on an interface
uint32_t GetN(void) const
Get the number of Ptr<NetDevice> stored in this container.
Ipv4Address NewNetwork(void)
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
uint32_t Get(void) const
Get the host-order 32-bit IP address.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.