OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2005,2006 INRIA |
| 4 * Copyright (c) 2013 Dalian University of Technology |
| 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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
| 19 * Junling Bu <linlinjavaer@gmail.com> |
| 20 */ |
| 21 #include "ns3/wifi-mac.h" |
| 22 #include "ns3/wifi-phy.h" |
| 23 #include "ns3/wifi-remote-station-manager.h" |
| 24 #include "ns3/wifi-channel.h" |
| 25 #include "ns3/llc-snap-header.h" |
| 26 #include "ns3/packet.h" |
| 27 #include "ns3/uinteger.h" |
| 28 #include "ns3/pointer.h" |
| 29 #include "ns3/node.h" |
| 30 #include "ns3/trace-source-accessor.h" |
| 31 #include "ns3/log.h" |
| 32 #include "ns3/qos-tag.h" |
| 33 #include "ns3/expire-time-tag.h" |
| 34 #include "ns3/simulator.h" |
| 35 |
| 36 #include "ocb-wifi-mac.h" |
| 37 #include "wave-net-device.h" |
| 38 |
| 39 NS_LOG_COMPONENT_DEFINE ("WaveNetDevice"); |
| 40 |
| 41 namespace ns3 { |
| 42 |
| 43 NS_OBJECT_ENSURE_REGISTERED (WaveNetDevice); |
| 44 |
| 45 TypeId |
| 46 WaveNetDevice::GetTypeId (void) |
| 47 { |
| 48 static TypeId tid = TypeId ("ns3::WaveNetDevice") |
| 49 .SetParent<NetDevice> () |
| 50 .AddConstructor<WaveNetDevice> () |
| 51 .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit", |
| 52 UintegerValue (MAX_MSDU_SIZE - LLC_SNAP_HEADER_LENGTH), |
| 53 MakeUintegerAccessor (&WaveNetDevice::SetMtu, |
| 54 &WaveNetDevice::GetMtu), |
| 55 MakeUintegerChecker<uint16_t> (1,MAX_MSDU_SIZE - LLC_SNAP_HEA
DER_LENGTH)) |
| 56 .AddAttribute ("Channel", "The channel attached to this device", |
| 57 PointerValue (), |
| 58 MakePointerAccessor (&WaveNetDevice::DoGetChannel), |
| 59 MakePointerChecker<WifiChannel> ()) |
| 60 .AddAttribute ("Phy", "The PHY layer attached to this device.", |
| 61 PointerValue (), |
| 62 MakePointerAccessor (&WaveNetDevice::GetPhy, |
| 63 &WaveNetDevice::SetPhy), |
| 64 MakePointerChecker<WifiPhy> ()) |
| 65 .AddAttribute ("RemoteStationManager", "The station manager attached to this
device.", |
| 66 PointerValue (), |
| 67 MakePointerAccessor (&WaveNetDevice::SetRemoteStationManager, |
| 68 &WaveNetDevice::GetRemoteStationManager)
, |
| 69 MakePointerChecker<WifiRemoteStationManager> ()) |
| 70 ; |
| 71 return tid; |
| 72 } |
| 73 |
| 74 WaveNetDevice::WaveNetDevice () |
| 75 { |
| 76 NS_LOG_FUNCTION (this); |
| 77 } |
| 78 WaveNetDevice::~WaveNetDevice () |
| 79 { |
| 80 NS_LOG_FUNCTION (this); |
| 81 } |
| 82 |
| 83 void |
| 84 WaveNetDevice::DoDispose (void) |
| 85 { |
| 86 NS_LOG_FUNCTION (this); |
| 87 m_node = 0; |
| 88 for (std::vector< Ptr<WifiMac> >::iterator i = m_macs.begin (); |
| 89 i != m_macs.end (); ++i) |
| 90 { |
| 91 (*i)->Dispose (); |
| 92 *i = 0; |
| 93 } |
| 94 m_phy->Dispose (); |
| 95 m_stationManager->Dispose (); |
| 96 m_macs.clear (); |
| 97 m_phy = 0; |
| 98 m_stationManager = 0; |
| 99 if (m_txProfile != 0) |
| 100 { |
| 101 delete m_txProfile; |
| 102 m_txProfile = 0; |
| 103 } |
| 104 m_channelCoordinator->Dispose (); |
| 105 m_channelManager->Dispose (); |
| 106 m_channelScheduler->Dispose (); |
| 107 m_channelCoordinator = 0; |
| 108 m_channelManager = 0; |
| 109 m_channelScheduler = 0; |
| 110 // chain up. |
| 111 NetDevice::DoDispose (); |
| 112 } |
| 113 |
| 114 void |
| 115 WaveNetDevice::DoInitialize (void) |
| 116 { |
| 117 NS_LOG_FUNCTION (this); |
| 118 m_phy->Initialize (); |
| 119 m_stationManager->SetupPhy (m_phy); |
| 120 m_stationManager->Initialize (); |
| 121 for (std::vector< Ptr<WifiMac> >::iterator i = m_macs.begin (); |
| 122 i != m_macs.end (); ++i) |
| 123 { |
| 124 (*i)->SetWifiRemoteStationManager (m_stationManager); |
| 125 (*i)->SetWifiPhy (m_phy); |
| 126 (*i)->SetForwardUpCallback (MakeCallback (&WaveNetDevice::ForwardUp, this)
); |
| 127 (*i)->Initialize (); |
| 128 } |
| 129 |
| 130 m_channelCoordinator = CreateObject<ChannelCoordinator> (); |
| 131 m_channelCoordinator->Initialize (); |
| 132 m_channelManager = CreateObject<ChannelManager> (); |
| 133 m_channelManager->Initialize (); |
| 134 m_channelScheduler = CreateObject<ChannelScheduler> (); |
| 135 m_channelScheduler->SetWaveNetDevice (this); |
| 136 m_channelScheduler->SetChannelManager (m_channelManager); |
| 137 m_channelScheduler->SetChannelCoodinator (m_channelCoordinator); |
| 138 m_channelScheduler->Initialize (); |
| 139 m_txProfile = 0; |
| 140 NetDevice::DoInitialize (); |
| 141 } |
| 142 void |
| 143 WaveNetDevice::AddMac (Ptr<WifiMac> mac) |
| 144 { |
| 145 m_macs.push_back (mac); |
| 146 } |
| 147 void |
| 148 WaveNetDevice::SetPhy (Ptr<WifiPhy> phy) |
| 149 { |
| 150 m_phy = phy; |
| 151 } |
| 152 void |
| 153 WaveNetDevice::SetRemoteStationManager (Ptr<WifiRemoteStationManager> manager) |
| 154 { |
| 155 m_stationManager = manager; |
| 156 } |
| 157 std::vector< Ptr<WifiMac> > |
| 158 WaveNetDevice::GetMacs (void) const |
| 159 { |
| 160 std::vector< Ptr<WifiMac> > macs (m_macs.begin (), m_macs.end ()); |
| 161 return macs; |
| 162 } |
| 163 Ptr<WifiPhy> |
| 164 WaveNetDevice::GetPhy (void) const |
| 165 { |
| 166 return m_phy; |
| 167 } |
| 168 Ptr<WifiRemoteStationManager> |
| 169 WaveNetDevice::GetRemoteStationManager (void) const |
| 170 { |
| 171 return m_stationManager; |
| 172 } |
| 173 |
| 174 |
| 175 bool |
| 176 WaveNetDevice::StartVsa (VsaInfo & vsaInfo) |
| 177 { |
| 178 NS_LOG_FUNCTION (this << &vsaInfo); |
| 179 |
| 180 return true; |
| 181 } |
| 182 void |
| 183 WaveNetDevice::StopVsa (uint32_t channelNumber) |
| 184 { |
| 185 NS_LOG_FUNCTION (this << channelNumber); |
| 186 } |
| 187 void |
| 188 WaveNetDevice::SetVsaReceiveCallback (VsaCallback vsaCallback) |
| 189 { |
| 190 m_vsaReceived = vsaCallback; |
| 191 } |
| 192 bool |
| 193 WaveNetDevice::StartSch (const SchInfo & schInfo) |
| 194 { |
| 195 NS_LOG_FUNCTION (this << &schInfo); |
| 196 if (!ChannelManager::IsWaveChannel (schInfo.channelNumber)) |
| 197 { |
| 198 return false; |
| 199 } |
| 200 uint32_t cn = schInfo.channelNumber; |
| 201 uint32_t extends = schInfo.extendedAccess; |
| 202 // now only support channel continuous access and extended access |
| 203 if (extends == 0xff) |
| 204 { |
| 205 return m_channelScheduler->AssignContinuousAccess (cn, schInfo.immediateAc
cess); |
| 206 } |
| 207 else if (extends == 0) |
| 208 { |
| 209 return m_channelScheduler->AssignAlternatingAccess (cn, schInfo.immediateA
ccess); |
| 210 } |
| 211 else |
| 212 { |
| 213 return m_channelScheduler->AssignExtendedAccess (cn, schInfo.extendedAcces
s, schInfo.immediateAccess); |
| 214 } |
| 215 return false; |
| 216 } |
| 217 void |
| 218 WaveNetDevice::StopSch (uint32_t channelNumber) |
| 219 { |
| 220 NS_LOG_FUNCTION (this << channelNumber); |
| 221 if (!ChannelManager::IsWaveChannel (channelNumber)) |
| 222 { |
| 223 return; |
| 224 } |
| 225 m_channelScheduler->Release (channelNumber); |
| 226 } |
| 227 bool |
| 228 WaveNetDevice::RegisterTxProfile (const TxProfile &txprofile) |
| 229 { |
| 230 NS_LOG_FUNCTION (this << &txprofile); |
| 231 uint32_t cn = txprofile.channelNumber; |
| 232 if (!ChannelManager::IsWaveChannel (cn)) |
| 233 { |
| 234 return false; |
| 235 } |
| 236 if (m_channelManager->IsChannelDead (cn)) |
| 237 { |
| 238 return false; |
| 239 } |
| 240 // then check whether here is already a TxProfile registered |
| 241 if (m_txProfile != 0) |
| 242 { |
| 243 return false; |
| 244 } |
| 245 |
| 246 // then check txprofile parameters |
| 247 // IP-based packets is not allowed to send in the CCH. |
| 248 if (txprofile.channelNumber == CCH) |
| 249 { |
| 250 return false; |
| 251 } |
| 252 m_txProfile = new TxProfile; |
| 253 *m_txProfile = txprofile; |
| 254 return true; |
| 255 } |
| 256 void |
| 257 WaveNetDevice::UnregisterTxProfile (uint32_t channelNumber) |
| 258 { |
| 259 NS_LOG_FUNCTION (this << channelNumber); |
| 260 if ((m_txProfile != 0) && (m_txProfile->channelNumber == channelNumber)) |
| 261 { |
| 262 delete m_txProfile; |
| 263 m_txProfile = 0; |
| 264 } |
| 265 } |
| 266 bool |
| 267 WaveNetDevice::SendX (Ptr<Packet> packet, const Address & dest, uint32_t protoco
l, const TxInfo & txInfo) |
| 268 { |
| 269 NS_LOG_FUNCTION (this << packet << dest << protocol << &txInfo); |
| 270 uint32_t cn = txInfo.channelNumber; |
| 271 if (!ChannelManager::IsWaveChannel (cn)) |
| 272 { |
| 273 return false; |
| 274 } |
| 275 // channel access shall be assigned |
| 276 if (m_channelManager->IsChannelDead (cn)) |
| 277 { |
| 278 return false; |
| 279 } |
| 280 |
| 281 if (txInfo.priority > 7) |
| 282 { |
| 283 return false; |
| 284 } |
| 285 |
| 286 if (txInfo.expiryTime > 500) |
| 287 { |
| 288 return false; |
| 289 } |
| 290 // according to channel number and priority, |
| 291 // route the packet to a proper queue. |
| 292 Ptr<WifiMac> mac = m_channelManager->GetChannelMac (cn); |
| 293 NS_ASSERT (mac != 0); |
| 294 QosTag qos (txInfo.priority); |
| 295 packet->AddPacketTag (qos); |
| 296 |
| 297 // if expireTime == 0, channel mac queue will has |
| 298 // its default expire time (500ms). |
| 299 if (txInfo.expiryTime != 0) |
| 300 { |
| 301 ExpireTimeTag expire (txInfo.expiryTime); |
| 302 packet->AddPacketTag (expire); |
| 303 } |
| 304 |
| 305 Mac48Address realTo = Mac48Address::ConvertFrom (dest); |
| 306 |
| 307 LlcSnapHeader llc; |
| 308 llc.SetType (protocol); |
| 309 packet->AddHeader (llc); |
| 310 |
| 311 mac->NotifyTx (packet); |
| 312 mac->Enqueue (packet, realTo); |
| 313 return true; |
| 314 } |
| 315 void |
| 316 WaveNetDevice::ChangeAddress (Mac48Address newAddress) |
| 317 { |
| 318 NS_LOG_FUNCTION (this << newAddress); |
| 319 Mac48Address oldAddress = m_macs.front ()->GetAddress (); |
| 320 if (newAddress == oldAddress) |
| 321 { |
| 322 return; |
| 323 } |
| 324 for (std::vector< Ptr<WifiMac> >::iterator i = m_macs.begin (); |
| 325 i != m_macs.end (); ++i) |
| 326 { |
| 327 (*i)->SetAddress (newAddress); |
| 328 } |
| 329 m_addressChange (oldAddress, newAddress); |
| 330 } |
| 331 |
| 332 void |
| 333 WaveNetDevice::SetChannelManager (Ptr<ChannelManager> channelManager) |
| 334 { |
| 335 m_channelManager = channelManager; |
| 336 } |
| 337 Ptr<ChannelManager> |
| 338 WaveNetDevice::GetChannelManager (void) const |
| 339 { |
| 340 return m_channelManager; |
| 341 } |
| 342 void |
| 343 WaveNetDevice::SetChannelScheduler (Ptr<ChannelScheduler> channelScheduler) |
| 344 { |
| 345 m_channelScheduler = channelScheduler; |
| 346 } |
| 347 Ptr<ChannelScheduler> |
| 348 WaveNetDevice::GetChannelScheduler (void) const |
| 349 { |
| 350 return m_channelScheduler; |
| 351 } |
| 352 void |
| 353 WaveNetDevice::SetChannelCoordinator (Ptr<ChannelCoordinator> channelCoordinator
) |
| 354 { |
| 355 m_channelCoordinator = channelCoordinator; |
| 356 } |
| 357 Ptr<ChannelCoordinator> |
| 358 WaveNetDevice::GetChannelCoordinator (void) const |
| 359 { |
| 360 return m_channelCoordinator; |
| 361 } |
| 362 |
| 363 void |
| 364 WaveNetDevice::SetIfIndex (const uint32_t index) |
| 365 { |
| 366 m_ifIndex = index; |
| 367 } |
| 368 uint32_t |
| 369 WaveNetDevice::GetIfIndex (void) const |
| 370 { |
| 371 return m_ifIndex; |
| 372 } |
| 373 Ptr<Channel> |
| 374 WaveNetDevice::GetChannel (void) const |
| 375 { |
| 376 return m_phy->GetChannel (); |
| 377 } |
| 378 Ptr<WifiChannel> |
| 379 WaveNetDevice::DoGetChannel (void) const |
| 380 { |
| 381 return m_phy->GetChannel (); |
| 382 } |
| 383 void |
| 384 WaveNetDevice::SetAddress (Address address) |
| 385 { |
| 386 for (std::vector< Ptr<WifiMac> >::iterator i = m_macs.begin (); |
| 387 i != m_macs.end (); ++i) |
| 388 { |
| 389 (*i)->SetAddress (Mac48Address::ConvertFrom (address)); |
| 390 } |
| 391 } |
| 392 Address |
| 393 WaveNetDevice::GetAddress (void) const |
| 394 { |
| 395 return m_macs.front ()->GetAddress (); |
| 396 } |
| 397 bool |
| 398 WaveNetDevice::SetMtu (const uint16_t mtu) |
| 399 { |
| 400 if (mtu > MAX_MSDU_SIZE - LLC_SNAP_HEADER_LENGTH) |
| 401 { |
| 402 return false; |
| 403 } |
| 404 m_mtu = mtu; |
| 405 return true; |
| 406 } |
| 407 uint16_t |
| 408 WaveNetDevice::GetMtu (void) const |
| 409 { |
| 410 return m_mtu; |
| 411 } |
| 412 bool |
| 413 WaveNetDevice::IsLinkUp (void) const |
| 414 { |
| 415 return true; |
| 416 } |
| 417 void |
| 418 WaveNetDevice::AddLinkChangeCallback (Callback<void> callback) |
| 419 { |
| 420 NS_LOG_WARN ("WaveNetDevice is linkup forever, so this callback will be never
called"); |
| 421 } |
| 422 bool |
| 423 WaveNetDevice::IsBroadcast (void) const |
| 424 { |
| 425 return true; |
| 426 } |
| 427 Address |
| 428 WaveNetDevice::GetBroadcast (void) const |
| 429 { |
| 430 return Mac48Address::GetBroadcast (); |
| 431 } |
| 432 bool |
| 433 WaveNetDevice::IsMulticast (void) const |
| 434 { |
| 435 return true; |
| 436 } |
| 437 Address |
| 438 WaveNetDevice::GetMulticast (Ipv4Address multicastGroup) const |
| 439 { |
| 440 return Mac48Address::GetMulticast (multicastGroup); |
| 441 } |
| 442 Address |
| 443 WaveNetDevice::GetMulticast (Ipv6Address addr) const |
| 444 { |
| 445 return Mac48Address::GetMulticast (addr); |
| 446 } |
| 447 bool |
| 448 WaveNetDevice::IsPointToPoint (void) const |
| 449 { |
| 450 return false; |
| 451 } |
| 452 bool |
| 453 WaveNetDevice::IsBridge (void) const |
| 454 { |
| 455 return false; |
| 456 } |
| 457 bool |
| 458 WaveNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolN
umber) |
| 459 { |
| 460 NS_LOG_FUNCTION (this << packet << dest << protocolNumber); |
| 461 |
| 462 NS_ASSERT (Mac48Address::IsMatchingType (dest)); |
| 463 |
| 464 // here shall be a txprofile. otherwise discard packet |
| 465 if (m_txProfile == 0) |
| 466 { |
| 467 return false; |
| 468 } |
| 469 // channel access shall be assigned |
| 470 if (m_channelManager->IsChannelDead (m_txProfile->channelNumber)) |
| 471 { |
| 472 return false; |
| 473 } |
| 474 |
| 475 Ptr<WifiMac> mac = m_channelManager->GetChannelMac (m_txProfile->channelNumber
); |
| 476 NS_ASSERT (mac != 0); |
| 477 |
| 478 Mac48Address realTo = Mac48Address::ConvertFrom (dest); |
| 479 |
| 480 LlcSnapHeader llc; |
| 481 llc.SetType (protocolNumber); |
| 482 packet->AddHeader (llc); |
| 483 |
| 484 mac->NotifyTx (packet); |
| 485 mac->Enqueue (packet, realTo); |
| 486 return true; |
| 487 } |
| 488 Ptr<Node> |
| 489 WaveNetDevice::GetNode (void) const |
| 490 { |
| 491 return m_node; |
| 492 } |
| 493 void |
| 494 WaveNetDevice::SetNode (Ptr<Node> node) |
| 495 { |
| 496 m_node = node; |
| 497 } |
| 498 bool |
| 499 WaveNetDevice::NeedsArp (void) const |
| 500 { |
| 501 return true; |
| 502 } |
| 503 void |
| 504 WaveNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb) |
| 505 { |
| 506 m_forwardUp = cb; |
| 507 } |
| 508 |
| 509 void |
| 510 WaveNetDevice::ForwardUp (Ptr<Packet> packet, Mac48Address from, Mac48Address to
) |
| 511 { |
| 512 Ptr<WifiMac> mac = m_macs.front (); |
| 513 LlcSnapHeader llc; |
| 514 packet->RemoveHeader (llc); |
| 515 enum NetDevice::PacketType type; |
| 516 if (to.IsBroadcast ()) |
| 517 { |
| 518 type = NetDevice::PACKET_BROADCAST; |
| 519 } |
| 520 else if (to.IsGroup ()) |
| 521 { |
| 522 type = NetDevice::PACKET_MULTICAST; |
| 523 } |
| 524 else if (to == GetAddress ()) |
| 525 { |
| 526 type = NetDevice::PACKET_HOST; |
| 527 } |
| 528 else |
| 529 { |
| 530 type = NetDevice::PACKET_OTHERHOST; |
| 531 } |
| 532 |
| 533 if (type != NetDevice::PACKET_OTHERHOST) |
| 534 { |
| 535 mac->NotifyRx (packet); |
| 536 m_forwardUp (this, packet, llc.GetType (), from); |
| 537 } |
| 538 |
| 539 if (!m_promiscRx.IsNull ()) |
| 540 { |
| 541 mac->NotifyPromiscRx (packet); |
| 542 m_promiscRx (this, packet, llc.GetType (), from, to, type); |
| 543 } |
| 544 } |
| 545 |
| 546 bool |
| 547 WaveNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Addres
s& dest, uint16_t protocolNumber) |
| 548 { |
| 549 NS_LOG_FUNCTION (this << packet << source << dest << protocolNumber); |
| 550 |
| 551 NS_ASSERT (Mac48Address::IsMatchingType (dest)); |
| 552 NS_ASSERT (Mac48Address::IsMatchingType (source)); |
| 553 |
| 554 // here shall be a txprofile. otherwise discard packet |
| 555 if (m_txProfile == 0) |
| 556 { |
| 557 return false; |
| 558 } |
| 559 // channel access shall be assigned |
| 560 if (m_channelManager->IsChannelDead (m_txProfile->channelNumber)) |
| 561 { |
| 562 return false; |
| 563 } |
| 564 |
| 565 NS_ASSERT (m_macs.size () == CHANNELS_OF_WAVE); |
| 566 Ptr<WifiMac> mac = m_channelManager->GetChannelMac (m_txProfile->channelNumber
); |
| 567 NS_ASSERT (mac != 0); |
| 568 |
| 569 Mac48Address realTo = Mac48Address::ConvertFrom (dest); |
| 570 Mac48Address realFrom = Mac48Address::ConvertFrom (source); |
| 571 |
| 572 LlcSnapHeader llc; |
| 573 llc.SetType (protocolNumber); |
| 574 packet->AddHeader (llc); |
| 575 |
| 576 mac->NotifyTx (packet); |
| 577 mac->Enqueue (packet, realTo, realFrom); |
| 578 return true; |
| 579 } |
| 580 |
| 581 void |
| 582 WaveNetDevice::SetPromiscReceiveCallback (PromiscReceiveCallback cb) |
| 583 { |
| 584 m_promiscRx = cb; |
| 585 for (std::vector< Ptr<WifiMac> >::iterator i = m_macs.begin (); |
| 586 i != m_macs.end (); ++i) |
| 587 { |
| 588 (*i)->SetPromisc (); |
| 589 } |
| 590 } |
| 591 bool |
| 592 WaveNetDevice::SupportsSendFrom (void) const |
| 593 { |
| 594 return false; |
| 595 } |
| 596 |
| 597 } // namespace ns3 |
OLD | NEW |