|
|
|
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2008-2009 Strasbourg University |
| 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 |
* Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr> |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "ns3/assert.h" |
| 22 |
#include "ns3/log.h" |
| 23 |
#include "ns3/ptr.h" |
| 24 |
#include "ns3/node.h" |
| 25 |
#include "ns3/net-device.h" |
| 26 |
#include "ns3/loopback-net-device.h" |
| 27 |
#include "ns3/mac16-address.h" |
| 28 |
#include "ns3/mac48-address.h" |
| 29 |
#include "ns3/mac64-address.h" |
| 30 |
#include "ns3/ipv6.h" |
| 31 |
#include "ns3/ipv6-address-generator.h" |
| 32 |
#include "ns3/traffic-control-helper.h" |
| 33 |
#include "ns3/traffic-control-layer.h" |
| 34 |
|
| 35 |
#include "ipv6-address-helper.h" |
| 36 |
|
| 37 |
namespace ns3 |
| 38 |
{ |
| 39 |
|
| 40 |
NS_LOG_COMPONENT_DEFINE ("Ipv6AddressHelper"); |
| 41 |
|
| 42 |
Ipv6AddressHelper::Ipv6AddressHelper () |
| 43 |
{ |
| 44 |
NS_LOG_FUNCTION (this); |
| 45 |
m_network = Ipv6Address ("2001:db8::"); |
| 46 |
m_prefix = 64; |
| 47 |
m_address = Ipv6Address ("::1"); |
| 48 |
m_base = m_address; |
| 49 |
} |
| 50 |
|
| 51 |
Ipv6AddressHelper::Ipv6AddressHelper (Ipv6Address network, Ipv6Prefix prefix, |
| 52 |
Ipv6Address base) |
| 53 |
{ |
| 54 |
NS_LOG_FUNCTION (this << network << prefix << base); |
| 55 |
|
| 56 |
m_network = network; |
| 57 |
m_prefix = prefix; |
| 58 |
m_address = base; |
| 59 |
m_base = base; |
| 60 |
|
| 61 |
NS_ASSERT_MSG (m_network == network.CombinePrefix (prefix), |
| 62 |
"Ipv6AddressHelper: network address and prefix mismatch: " << m_network << " " << m_prefix); |
| 63 |
|
| 64 |
NS_ASSERT_MSG (base.CombinePrefix (prefix) == Ipv6Address::GetZero (), |
| 65 |
"Ipv6AddressHelper: base address and prefix mismatch: " << base << " " << m_prefix); |
| 66 |
} |
| 67 |
|
| 68 |
void Ipv6AddressHelper::SetBase (Ipv6Address network, Ipv6Prefix prefix, |
| 69 |
Ipv6Address base) |
| 70 |
{ |
| 71 |
NS_LOG_FUNCTION (this << network << prefix << base); |
| 72 |
|
| 73 |
m_network = network; |
| 74 |
m_prefix = prefix; |
| 75 |
m_address = base; |
| 76 |
m_base = base; |
| 77 |
|
| 78 |
NS_ASSERT_MSG (m_network == network.CombinePrefix (prefix), |
| 79 |
"Ipv6AddressHelper::SetBase(): network address and prefix mismatch: " << m_network << " " << m_prefix); |
| 80 |
|
| 81 |
NS_ASSERT_MSG (base.CombinePrefix (prefix) == Ipv6Address::GetZero (), |
| 82 |
"Ipv6AddressHelper::SetBase(): base address and prefix mismatch: " << base << " " << m_prefix); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
Ipv6Address Ipv6AddressHelper::NewAddress (Address addr) |
| 87 |
{ |
| 88 |
NS_LOG_FUNCTION (this << addr); |
| 89 |
if (Mac64Address::IsMatchingType (addr)) |
| 90 |
{ |
| 91 |
Ipv6Address address = Ipv6Address::MakeAutoconfiguredAddress (Mac64Address::ConvertFrom (addr), m_network); |
| 92 |
Ipv6AddressGenerator::AddAllocated (address); |
| 93 |
return address; |
| 94 |
} |
| 95 |
else if (Mac48Address::IsMatchingType (addr)) |
| 96 |
{ |
| 97 |
Ipv6Address address = Ipv6Address::MakeAutoconfiguredAddress (Mac48Address::ConvertFrom (addr), m_network); |
| 98 |
Ipv6AddressGenerator::AddAllocated (address); |
| 99 |
return address; |
| 100 |
} |
| 101 |
else if (Mac16Address::IsMatchingType (addr)) |
| 102 |
{ |
| 103 |
Ipv6Address address = Ipv6Address::MakeAutoconfiguredAddress (Mac16Address::ConvertFrom (addr), m_network); |
| 104 |
Ipv6AddressGenerator::AddAllocated (address); |
| 105 |
return address; |
| 106 |
} |
| 107 |
else if (Mac8Address::IsMatchingType (addr)) |
| 108 |
{ |
| 109 |
Ipv6Address network = Ipv6AddressGenerator::GetNetwork (Ipv6Prefix (64)); |
| 110 |
Ipv6Address address = Ipv6Address::MakeAutoconfiguredAddress (Mac8Address::ConvertFrom (addr), network); |
| 111 |
Ipv6AddressGenerator::AddAllocated (address); |
| 112 |
return address; |
| 113 |
} |
| 114 |
else |
| 115 |
{ |
| 116 |
NS_FATAL_ERROR ("Did not pass in a valid Mac Address (16, 48 or 64 bits)"); |
| 117 |
} |
| 118 |
/* never reached */ |
| 119 |
return Ipv6Address ("::"); |
| 120 |
} |
| 121 |
|
| 122 |
Ipv6Address Ipv6AddressHelper::NewAddress (void) |
| 123 |
{ |
| 124 |
NS_LOG_FUNCTION (this); |
| 125 |
// |
| 126 |
// The way this is expected to be used is that an address and network number |
| 127 |
// are initialized, and then NewAddress() is called repeatedly to allocate and |
| 128 |
// get new addresses on a given subnet. The client will expect that the first |
| 129 |
// address she gets back is the one she used to initialize the generator with. |
| 130 |
// This implies that this operation is a post-increment. |
| 131 |
|
| 132 |
uint8_t netBuf[16]; |
| 133 |
uint8_t hostBuf[16]; |
| 134 |
uint8_t addrBuf[16]; |
| 135 |
m_network.GetBytes (netBuf); |
| 136 |
m_address.GetBytes (hostBuf); |
| 137 |
|
| 138 |
NS_ASSERT_MSG (m_address.CombinePrefix (m_prefix) == Ipv6Address::GetZero (), |
| 139 |
"Ipv6AddressHelper::NewAddress(): Too many hosts in the network: " << m_address << " " << m_prefix); |
| 140 |
|
| 141 |
for (uint8_t i=0; i<16; i++) |
| 142 |
{ |
| 143 |
addrBuf[i] = netBuf[i] | hostBuf[i]; |
| 144 |
} |
| 145 |
|
| 146 |
Ipv6Address addr = Ipv6Address (addrBuf); |
| 147 |
|
| 148 |
// Remember: hostBuf[15] is the Least Significant Byte. |
| 149 |
uint16_t sum; |
| 150 |
sum = static_cast<uint16_t> (hostBuf[15]) + 1; |
| 151 |
hostBuf[15] += 1; |
| 152 |
for (uint8_t index=0; index<15; index++) |
| 153 |
{ |
| 154 |
if (sum > hostBuf[15-index]) |
| 155 |
{ |
| 156 |
sum = static_cast<uint16_t> (hostBuf[14-index]) + 1; |
| 157 |
hostBuf[14-index] += 1; |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
m_address = Ipv6Address (hostBuf); |
| 165 |
|
| 166 |
Ipv6AddressGenerator::AddAllocated (addr); |
| 167 |
return addr; |
| 168 |
} |
| 169 |
|
| 170 |
void Ipv6AddressHelper::NewNetwork (void) |
| 171 |
{ |
| 172 |
NS_LOG_FUNCTION (this); |
| 173 |
|
| 174 |
uint8_t netBuf[16]; |
| 175 |
uint8_t addBuf[16]; |
| 176 |
m_network.GetBytes (netBuf); |
| 177 |
|
| 178 |
uint8_t prefixIndex = (m_prefix.GetPrefixLength ()-1)/8; |
| 179 |
uint8_t prefixPosition = (8-(m_prefix.GetPrefixLength ()%8))%8; |
| 180 |
|
| 181 |
for (uint8_t index=0; index<16; index++) |
| 182 |
{ |
| 183 |
addBuf[index] = 0; |
| 184 |
if (index==prefixIndex) |
| 185 |
{ |
| 186 |
addBuf[index] = (1<<prefixPosition); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
uint16_t sum[16]; |
| 191 |
for (uint8_t index=0; index<16; index++) |
| 192 |
{ |
| 193 |
sum[index] = static_cast<uint16_t> (netBuf[index]) + static_cast<uint16_t> (addBuf[index]); |
| 194 |
netBuf[index] += addBuf[index]; |
| 195 |
} |
| 196 |
|
| 197 |
for (uint8_t index=0; index<15; index++) |
| 198 |
{ |
| 199 |
if (sum[15-index] > netBuf[15-index]) |
| 200 |
{ |
| 201 |
sum[14-index] = static_cast<uint16_t> (netBuf[14-index]) + 1; |
| 202 |
netBuf[14-index] += 1; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
m_network = Ipv6Address (netBuf); |
| 207 |
m_address = m_base; |
| 208 |
} |
| 209 |
|
| 210 |
Ipv6InterfaceContainer Ipv6AddressHelper::Assign (const NetDeviceContainer &c) |
| 211 |
{ |
| 212 |
NS_LOG_FUNCTION (this); |
| 213 |
Ipv6InterfaceContainer retval; |
| 214 |
|
| 215 |
for (uint32_t i = 0; i < c.GetN (); ++i) |
| 216 |
{ |
| 217 |
Ptr<NetDevice> device = c.Get (i); |
| 218 |
|
| 219 |
Ptr<Node> node = device->GetNode (); |
| 220 |
NS_ASSERT_MSG (node, "Ipv6AddressHelper::Allocate (): Bad node"); |
| 221 |
|
| 222 |
Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> (); |
| 223 |
NS_ASSERT_MSG (ipv6, "Ipv6AddressHelper::Allocate (): Bad ipv6"); |
| 224 |
int32_t ifIndex = 0; |
| 225 |
|
| 226 |
ifIndex = ipv6->GetInterfaceForDevice (device); |
| 227 |
if (ifIndex == -1) |
| 228 |
{ |
| 229 |
ifIndex = ipv6->AddInterface (device); |
| 230 |
} |
| 231 |
NS_ASSERT_MSG (ifIndex >= 0, "Ipv6AddressHelper::Allocate (): " |
| 232 |
"Interface index not found"); |
| 233 |
|
| 234 |
Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (NewAddress (device->GetAddress ()), Ipv6Prefix (64)); |
| 235 |
ipv6->SetMetric (ifIndex, 1); |
| 236 |
ipv6->AddAddress (ifIndex, ipv6Addr); |
| 237 |
ipv6->SetUp (ifIndex); |
| 238 |
|
| 239 |
retval.Add (ipv6, ifIndex); |
| 240 |
|
| 241 |
// Install the default traffic control configuration if the traffic |
| 242 |
// control layer has been aggregated, if this is not |
| 243 |
// a loopback interface, and there is no queue disc installed already |
| 244 |
Ptr<TrafficControlLayer> tc = node->GetObject<TrafficControlLayer> (); |
| 245 |
if (tc && DynamicCast<LoopbackNetDevice> (device) == 0 && tc->GetRootQueueDiscOnDevice (device) == 0) |
| 246 |
{ |
| 247 |
NS_LOG_LOGIC ("Installing default traffic control configuration"); |
| 248 |
TrafficControlHelper tcHelper = TrafficControlHelper::Default (); |
| 249 |
tcHelper.Install (device); |
| 250 |
} |
| 251 |
} |
| 252 |
return retval; |
| 253 |
} |
| 254 |
|
| 255 |
Ipv6InterfaceContainer Ipv6AddressHelper::Assign (const NetDeviceContainer &c, std::vector<bool> withConfiguration) |
| 256 |
{ |
| 257 |
NS_LOG_FUNCTION (this); |
| 258 |
Ipv6InterfaceContainer retval; |
| 259 |
for (uint32_t i = 0; i < c.GetN (); ++i) |
| 260 |
{ |
| 261 |
Ptr<NetDevice> device = c.Get (i); |
| 262 |
|
| 263 |
Ptr<Node> node = device->GetNode (); |
| 264 |
NS_ASSERT_MSG (node, "Ipv6AddressHelper::Allocate (): Bad node"); |
| 265 |
|
| 266 |
Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> (); |
| 267 |
NS_ASSERT_MSG (ipv6, "Ipv6AddressHelper::Allocate (): Bad ipv6"); |
| 268 |
|
| 269 |
int32_t ifIndex = ipv6->GetInterfaceForDevice (device); |
| 270 |
if (ifIndex == -1) |
| 271 |
{ |
| 272 |
ifIndex = ipv6->AddInterface (device); |
| 273 |
} |
| 274 |
NS_ASSERT_MSG (ifIndex >= 0, "Ipv6AddressHelper::Allocate (): " |
| 275 |
"Interface index not found"); |
| 276 |
|
| 277 |
ipv6->SetMetric (ifIndex, 1); |
| 278 |
|
| 279 |
if (withConfiguration.at (i)) |
| 280 |
{ |
| 281 |
Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (NewAddress (device->GetAddress ()), Ipv6Prefix (64)); |
| 282 |
ipv6->AddAddress (ifIndex, ipv6Addr); |
| 283 |
} |
| 284 |
|
| 285 |
ipv6->SetUp (ifIndex); |
| 286 |
retval.Add (ipv6, ifIndex); |
| 287 |
|
| 288 |
// Install the default traffic control configuration if the traffic |
| 289 |
// control layer has been aggregated, if this is not |
| 290 |
// a loopback interface, and there is no queue disc installed already |
| 291 |
Ptr<TrafficControlLayer> tc = node->GetObject<TrafficControlLayer> (); |
| 292 |
if (tc && DynamicCast<LoopbackNetDevice> (device) == 0 && tc->GetRootQueueDiscOnDevice (device) == 0) |
| 293 |
{ |
| 294 |
NS_LOG_LOGIC ("Installing default traffic control configuration"); |
| 295 |
TrafficControlHelper tcHelper = TrafficControlHelper::Default (); |
| 296 |
tcHelper.Install (device); |
| 297 |
} |
| 298 |
} |
| 299 |
return retval; |
| 300 |
} |
| 301 |
|
| 302 |
// Helper API that is redundant with Assign (c, false); |
| 303 |
Ipv6InterfaceContainer Ipv6AddressHelper::AssignWithoutAddress (const NetDeviceContainer &c) |
| 304 |
{ |
| 305 |
NS_LOG_FUNCTION (this); |
| 306 |
std::vector<bool> withConfiguration; |
| 307 |
for (uint32_t i = 0; i < c.GetN (); ++i) |
| 308 |
{ |
| 309 |
withConfiguration.push_back (false); |
| 310 |
} |
| 311 |
return Assign (c, withConfiguration); |
| 312 |
} |
| 313 |
|
| 314 |
} /* namespace ns3 */ |
| 315 |
|