OLD | NEW |
(Empty) | |
| 1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2008,2009 IITP RAS |
| 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: Kirill Andreev <andreev@iitp.ru> |
| 19 * Pavel Boyko <boyko@iitp.ru> |
| 20 */ |
| 21 |
| 22 #include "ns3/node.h" |
| 23 #include "ns3/packet.h" |
| 24 #include "ns3/log.h" |
| 25 #include "ns3/pointer.h" |
| 26 #include "ns3/mesh-point-device.h" |
| 27 #include "ns3/wifi-net-device.h" |
| 28 #include "ns3/mesh-wifi-interface-mac.h" |
| 29 |
| 30 NS_LOG_COMPONENT_DEFINE ("MeshPointDevice"); |
| 31 |
| 32 namespace ns3 { |
| 33 |
| 34 NS_OBJECT_ENSURE_REGISTERED (MeshPointDevice); |
| 35 |
| 36 TypeId |
| 37 MeshPointDevice::GetTypeId () |
| 38 { |
| 39 static TypeId tid = TypeId ("ns3::MeshPointDevice") |
| 40 .SetParent<NetDevice> () |
| 41 .AddConstructor<MeshPointDevice> () |
| 42 .AddAttribute ( "RoutingProtocol", |
| 43 "The mesh routing protocol used by this mesh point.", |
| 44 PointerValue (), |
| 45 MakePointerAccessor ( |
| 46 &MeshPointDevice::GetRoutingProtocol, &MeshPointDevice::Se
tRoutingProtocol), |
| 47 MakePointerChecker< |
| 48 MeshL2RoutingProtocol> ()); |
| 49 return tid; |
| 50 } |
| 51 |
| 52 MeshPointDevice::MeshPointDevice () : |
| 53 m_ifIndex (0), m_mtu (1500) |
| 54 { |
| 55 NS_LOG_FUNCTION_NOARGS (); |
| 56 m_channel = CreateObject<BridgeChannel> (); |
| 57 } |
| 58 |
| 59 MeshPointDevice::~MeshPointDevice () |
| 60 { |
| 61 NS_LOG_FUNCTION_NOARGS (); |
| 62 } |
| 63 |
| 64 void |
| 65 MeshPointDevice::DoDispose () |
| 66 { |
| 67 NS_LOG_FUNCTION_NOARGS (); |
| 68 for (std::vector<Ptr<NetDevice> >::iterator iter = m_ifaces.begin (); iter !=
m_ifaces.end (); iter++) |
| 69 { |
| 70 *iter = 0; |
| 71 } |
| 72 m_ifaces.clear (); |
| 73 m_node = 0; |
| 74 NetDevice::DoDispose (); |
| 75 |
| 76 } |
| 77 |
| 78 //----------------------------------------------------------------------------- |
| 79 // NetDevice interface implementation |
| 80 //----------------------------------------------------------------------------- |
| 81 |
| 82 void |
| 83 MeshPointDevice::ReceiveFromDevice (Ptr<NetDevice> incomingPort, Ptr<const Packe
t> packet, uint16_t protocol, |
| 84 Address const &src, Address const &dst, PacketType packetType) |
| 85 { |
| 86 NS_LOG_FUNCTION_NOARGS (); |
| 87 NS_LOG_DEBUG ("UID is " << packet->GetUid ()); |
| 88 const Mac48Address src48 = Mac48Address::ConvertFrom (src); |
| 89 const Mac48Address dst48 = Mac48Address::ConvertFrom (dst); |
| 90 uint16_t& realProtocol = protocol; |
| 91 NS_LOG_DEBUG ("SRC=" << src48 << ", DST = " << dst48 << ", I am: " << m_addres
s); |
| 92 if (!m_promiscRxCallback.IsNull ()) |
| 93 { |
| 94 m_promiscRxCallback (this, packet, protocol, src, dst, packetType); |
| 95 } |
| 96 if (dst48.IsGroup ()) |
| 97 { |
| 98 Ptr<Packet> packet_copy = packet->Copy (); |
| 99 if (m_routingProtocol->RemoveRoutingStuff (incomingPort->GetIfIndex (), sr
c48, dst48, packet_copy, realProtocol)) |
| 100 { |
| 101 m_rxCallback (this, packet_copy, realProtocol, src); |
| 102 Forward (incomingPort, packet, protocol, src48, dst48); |
| 103 |
| 104 m_rxStats.broadcastData++; |
| 105 m_rxStats.broadcastDataBytes += packet->GetSize (); |
| 106 } |
| 107 return; |
| 108 } |
| 109 if (dst48 == m_address) |
| 110 { |
| 111 Ptr<Packet> packet_copy = packet->Copy (); |
| 112 if (m_routingProtocol->RemoveRoutingStuff (incomingPort->GetIfIndex (), sr
c48, dst48, packet_copy, realProtocol)) |
| 113 { |
| 114 m_rxCallback (this, packet_copy, realProtocol, src); |
| 115 m_rxStats.unicastData++; |
| 116 m_rxStats.unicastDataBytes += packet->GetSize (); |
| 117 } |
| 118 return; |
| 119 } |
| 120 else |
| 121 Forward (incomingPort, packet->Copy (), protocol, src48, dst48); |
| 122 } |
| 123 |
| 124 void |
| 125 MeshPointDevice::Forward (Ptr<NetDevice> inport, Ptr<const Packet> packet, uint1
6_t protocol, |
| 126 const Mac48Address src, const Mac48Address dst) |
| 127 { |
| 128 // pass through routing protocol |
| 129 m_routingProtocol->RequestRoute (inport->GetIfIndex (), src, dst, packet, prot
ocol, MakeCallback ( |
| 130 &MeshPointDevice::DoSend, this)); |
| 131 } |
| 132 |
| 133 void |
| 134 MeshPointDevice::SetIfIndex (const uint32_t index) |
| 135 { |
| 136 NS_LOG_FUNCTION_NOARGS (); |
| 137 m_ifIndex = index; |
| 138 } |
| 139 |
| 140 uint32_t |
| 141 MeshPointDevice::GetIfIndex () const |
| 142 { |
| 143 NS_LOG_FUNCTION_NOARGS (); |
| 144 return m_ifIndex; |
| 145 } |
| 146 |
| 147 Ptr<Channel> |
| 148 MeshPointDevice::GetChannel () const |
| 149 { |
| 150 NS_LOG_FUNCTION_NOARGS (); |
| 151 return m_channel; |
| 152 } |
| 153 |
| 154 Address |
| 155 MeshPointDevice::GetAddress () const |
| 156 { |
| 157 NS_LOG_FUNCTION_NOARGS (); |
| 158 return m_address; |
| 159 } |
| 160 |
| 161 void |
| 162 MeshPointDevice::SetAddress (Address a) |
| 163 { |
| 164 NS_LOG_WARN ("Manual changing mesh point address can cause routing errors."); |
| 165 m_address = Mac48Address::ConvertFrom (a); |
| 166 } |
| 167 |
| 168 bool |
| 169 MeshPointDevice::SetMtu (const uint16_t mtu) |
| 170 { |
| 171 NS_LOG_FUNCTION_NOARGS (); |
| 172 m_mtu = mtu; |
| 173 return true; |
| 174 } |
| 175 |
| 176 uint16_t |
| 177 MeshPointDevice::GetMtu () const |
| 178 { |
| 179 NS_LOG_FUNCTION_NOARGS (); |
| 180 return m_mtu; |
| 181 } |
| 182 |
| 183 bool |
| 184 MeshPointDevice::IsLinkUp () const |
| 185 { |
| 186 NS_LOG_FUNCTION_NOARGS (); |
| 187 return true; |
| 188 } |
| 189 |
| 190 void |
| 191 MeshPointDevice::AddLinkChangeCallback (Callback<void> callback) |
| 192 { |
| 193 // do nothing |
| 194 } |
| 195 |
| 196 bool |
| 197 MeshPointDevice::IsBroadcast () const |
| 198 { |
| 199 NS_LOG_FUNCTION_NOARGS (); |
| 200 return true; |
| 201 } |
| 202 |
| 203 Address |
| 204 MeshPointDevice::GetBroadcast () const |
| 205 { |
| 206 NS_LOG_FUNCTION_NOARGS (); |
| 207 return Mac48Address ("ff:ff:ff:ff:ff:ff"); |
| 208 } |
| 209 |
| 210 bool |
| 211 MeshPointDevice::IsMulticast () const |
| 212 { |
| 213 NS_LOG_FUNCTION_NOARGS (); |
| 214 return true; |
| 215 } |
| 216 |
| 217 Address |
| 218 MeshPointDevice::GetMulticast (Ipv4Address multicastGroup) const |
| 219 { |
| 220 NS_LOG_FUNCTION (this << multicastGroup); |
| 221 Mac48Address multicast = Mac48Address::GetMulticast (multicastGroup); |
| 222 return multicast; |
| 223 } |
| 224 |
| 225 bool |
| 226 MeshPointDevice::IsPointToPoint () const |
| 227 { |
| 228 NS_LOG_FUNCTION_NOARGS (); |
| 229 return false; |
| 230 } |
| 231 |
| 232 bool |
| 233 MeshPointDevice::IsBridge () const |
| 234 { |
| 235 NS_LOG_FUNCTION_NOARGS (); |
| 236 return false; |
| 237 } |
| 238 |
| 239 bool |
| 240 MeshPointDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protoco
lNumber) |
| 241 { |
| 242 const Mac48Address dst48 = Mac48Address::ConvertFrom (dest); |
| 243 return m_routingProtocol->RequestRoute (m_ifIndex, m_address, dst48, packet, p
rotocolNumber, MakeCallback ( |
| 244 &MeshPointDevice::DoSend, this)); |
| 245 } |
| 246 |
| 247 bool |
| 248 MeshPointDevice::SendFrom (Ptr<Packet> packet, const Address& src, const Address
& dest, |
| 249 uint16_t protocolNumber) |
| 250 { |
| 251 const Mac48Address src48 = Mac48Address::ConvertFrom (src); |
| 252 const Mac48Address dst48 = Mac48Address::ConvertFrom (dest); |
| 253 return m_routingProtocol->RequestRoute (m_ifIndex, src48, dst48, packet, proto
colNumber, MakeCallback ( |
| 254 &MeshPointDevice::DoSend, this)); |
| 255 } |
| 256 |
| 257 Ptr<Node> |
| 258 MeshPointDevice::GetNode () const |
| 259 { |
| 260 NS_LOG_FUNCTION_NOARGS (); |
| 261 return m_node; |
| 262 } |
| 263 |
| 264 void |
| 265 MeshPointDevice::SetNode (Ptr<Node> node) |
| 266 { |
| 267 NS_LOG_FUNCTION_NOARGS (); |
| 268 m_node = node; |
| 269 } |
| 270 |
| 271 bool |
| 272 MeshPointDevice::NeedsArp () const |
| 273 { |
| 274 NS_LOG_FUNCTION_NOARGS (); |
| 275 return true; |
| 276 } |
| 277 |
| 278 void |
| 279 MeshPointDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb) |
| 280 { |
| 281 NS_LOG_FUNCTION_NOARGS (); |
| 282 m_rxCallback = cb; |
| 283 } |
| 284 |
| 285 void |
| 286 MeshPointDevice::SetPromiscReceiveCallback (NetDevice::PromiscReceiveCallback cb
) |
| 287 { |
| 288 NS_LOG_FUNCTION_NOARGS (); |
| 289 m_promiscRxCallback = cb; |
| 290 } |
| 291 |
| 292 bool |
| 293 MeshPointDevice::SupportsSendFrom () const |
| 294 { |
| 295 NS_LOG_FUNCTION_NOARGS (); |
| 296 return false; // don't allow to bridge mesh network with something else. |
| 297 } |
| 298 |
| 299 Address |
| 300 MeshPointDevice::GetMulticast (Ipv6Address addr) const |
| 301 { |
| 302 NS_LOG_FUNCTION (this << addr); |
| 303 return Mac48Address::GetMulticast (addr); |
| 304 } |
| 305 |
| 306 //----------------------------------------------------------------------------- |
| 307 // Interfaces |
| 308 //----------------------------------------------------------------------------- |
| 309 uint32_t |
| 310 MeshPointDevice::GetNInterfaces () const |
| 311 { |
| 312 NS_LOG_FUNCTION_NOARGS (); |
| 313 return m_ifaces.size (); |
| 314 } |
| 315 |
| 316 Ptr<NetDevice> |
| 317 MeshPointDevice::GetInterface (uint32_t n) const |
| 318 { |
| 319 for (std::vector<Ptr<NetDevice> >::const_iterator i = m_ifaces.begin (); i !=
m_ifaces.end (); i++) |
| 320 { |
| 321 if ((*i)->GetIfIndex () == n) |
| 322 { |
| 323 return (*i); |
| 324 } |
| 325 } |
| 326 NS_FATAL_ERROR ("Mesh point interface is not found by index"); |
| 327 return 0; |
| 328 } |
| 329 std::vector<Ptr<NetDevice> > |
| 330 MeshPointDevice::GetInterfaces () const |
| 331 { |
| 332 return m_ifaces; |
| 333 } |
| 334 void |
| 335 MeshPointDevice::AddInterface (Ptr<NetDevice> iface) |
| 336 { |
| 337 NS_LOG_FUNCTION_NOARGS (); |
| 338 |
| 339 NS_ASSERT (iface != this); |
| 340 if (!Mac48Address::IsMatchingType (iface->GetAddress ())) |
| 341 { |
| 342 NS_FATAL_ERROR ("Device does not support eui 48 addresses: cannot be used
as a mesh point interface."); |
| 343 } |
| 344 if (!iface->SupportsSendFrom ()) |
| 345 { |
| 346 NS_FATAL_ERROR ("Device does not support SendFrom: cannot be used as a mes
h point interface."); |
| 347 } |
| 348 |
| 349 // Mesh point has MAC address of it's first interface |
| 350 if (m_ifaces.empty ()) |
| 351 { |
| 352 m_address = Mac48Address::ConvertFrom (iface->GetAddress ()); |
| 353 } |
| 354 Ptr<WifiNetDevice> wifiNetDev = iface->GetObject<WifiNetDevice> (); |
| 355 if (wifiNetDev == 0) |
| 356 { |
| 357 NS_FATAL_ERROR ("Device is not a WiFi NIC: cannot be used as a mesh point
interface."); |
| 358 } |
| 359 Ptr<MeshWifiInterfaceMac> ifaceMac = wifiNetDev->GetMac ()->GetObject<MeshWifi
InterfaceMac> (); |
| 360 if (ifaceMac == 0) |
| 361 { |
| 362 NS_FATAL_ERROR ( |
| 363 "WiFi device doesn't have correct MAC installed: cannot be used as a m
esh point interface."); |
| 364 } |
| 365 ifaceMac->SetMeshPointAddress (m_address); |
| 366 |
| 367 // Receive frames from this interface |
| 368 m_node->RegisterProtocolHandler (MakeCallback (&MeshPointDevice::ReceiveFromDe
vice, this), 0, iface, /*promiscuous = */ |
| 369 true); |
| 370 m_ifaces.push_back (iface); |
| 371 m_channel->AddChannel (iface->GetChannel ()); |
| 372 } |
| 373 |
| 374 //----------------------------------------------------------------------------- |
| 375 // Protocols |
| 376 //----------------------------------------------------------------------------- |
| 377 |
| 378 void |
| 379 MeshPointDevice::SetRoutingProtocol (Ptr<MeshL2RoutingProtocol> protocol) |
| 380 { |
| 381 NS_LOG_FUNCTION_NOARGS (); |
| 382 NS_ASSERT_MSG (PeekPointer (protocol->GetMeshPoint ()) == this, |
| 383 "Routing protocol must be installed on mesh point to be useful."); |
| 384 m_routingProtocol = protocol; |
| 385 } |
| 386 |
| 387 Ptr<MeshL2RoutingProtocol> |
| 388 MeshPointDevice::GetRoutingProtocol () const |
| 389 { |
| 390 return m_routingProtocol; |
| 391 } |
| 392 |
| 393 void |
| 394 MeshPointDevice::DoSend (bool success, Ptr<Packet> packet, Mac48Address src, Mac
48Address dst, |
| 395 uint16_t protocol, uint32_t outIface) |
| 396 { |
| 397 if (!success) |
| 398 { |
| 399 NS_LOG_DEBUG ("Resolve failed"); |
| 400 return; |
| 401 } |
| 402 |
| 403 // Count statistics |
| 404 Statistics * stats = ((src == m_address) ? &m_txStats : &m_fwdStats); |
| 405 |
| 406 if (dst.IsBroadcast ()) |
| 407 { |
| 408 stats->broadcastData++; |
| 409 stats->broadcastDataBytes += packet->GetSize (); |
| 410 } |
| 411 else |
| 412 { |
| 413 stats->unicastData++; |
| 414 stats->unicastDataBytes += packet->GetSize (); |
| 415 } |
| 416 |
| 417 // Send |
| 418 if (outIface != 0xffffffff) |
| 419 { |
| 420 GetInterface (outIface)->SendFrom (packet, src, dst, protocol); |
| 421 } |
| 422 else |
| 423 { |
| 424 for (std::vector<Ptr<NetDevice> >::iterator i = m_ifaces.begin (); i != m_
ifaces.end (); i++) |
| 425 { |
| 426 (*i) -> SendFrom (packet->Copy (), src, dst, protocol); |
| 427 } |
| 428 } |
| 429 } |
| 430 MeshPointDevice::Statistics::Statistics () : |
| 431 unicastData (0), unicastDataBytes (0), broadcastData (0), broadcastDataBytes (
0) |
| 432 { |
| 433 } |
| 434 |
| 435 void |
| 436 MeshPointDevice::Report (std::ostream & os) const |
| 437 { |
| 438 os << "<Statistics" << std::endl << |
| 439 "txUnicastData=\"" << m_txStats.unicastData << "\"" << std::endl << |
| 440 "txUnicastDataBytes=\"" << m_txStats.unicastDataBytes << "\"" << std::endl <
< |
| 441 "txBroadcastData=\"" << m_txStats.broadcastData << "\"" << std::endl << |
| 442 "txBroadcastDataBytes=\"" << m_txStats.broadcastDataBytes << "\"" << std::en
dl << |
| 443 "rxUnicastData=\"" << m_rxStats.unicastData << "\"" << std::endl << |
| 444 "rxUnicastDataBytes=\"" << m_rxStats.unicastDataBytes << "\"" << std::endl <
< |
| 445 "rxBroadcastData=\"" << m_rxStats.broadcastData << "\"" << std::endl << |
| 446 "rxBroadcastDataBytes=\"" << m_rxStats.broadcastDataBytes << "\"" << std::en
dl << |
| 447 "fwdUnicastData=\"" << m_fwdStats.unicastData << "\"" << std::endl << |
| 448 "fwdUnicastDataBytes=\"" << m_fwdStats.unicastDataBytes << "\"" << std::endl
<< |
| 449 "fwdBroadcastData=\"" << m_fwdStats.broadcastData << "\"" << std::endl << |
| 450 "fwdBroadcastDataBytes=\"" << m_fwdStats.broadcastDataBytes << "\"" << std::
endl << |
| 451 "/>" << std::endl; |
| 452 } |
| 453 |
| 454 void |
| 455 MeshPointDevice::ResetStats () |
| 456 { |
| 457 m_rxStats = Statistics (); |
| 458 m_txStats = Statistics (); |
| 459 m_fwdStats = Statistics (); |
| 460 } |
| 461 |
| 462 } // namespace ns3 |
OLD | NEW |