A Discrete-Event Network Simulator
API
mesh-point-device.cc
Go to the documentation of this file.
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/packet.h"
23 #include "ns3/log.h"
24 #include "ns3/pointer.h"
25 #include "ns3/mesh-point-device.h"
26 #include "ns3/wifi-net-device.h"
27 #include "ns3/mesh-wifi-interface-mac.h"
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("MeshPointDevice");
32 
33 NS_OBJECT_ENSURE_REGISTERED (MeshPointDevice);
34 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::MeshPointDevice")
39  .SetParent<NetDevice> ()
40  .SetGroupName ("Mesh")
41  .AddConstructor<MeshPointDevice> ()
42  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
43  UintegerValue (0xffff),
46  MakeUintegerChecker<uint16_t> ())
47  .AddAttribute ( "RoutingProtocol",
48  "The mesh routing protocol used by this mesh point.",
49  PointerValue (),
54  return tid;
55 }
56 
58  m_ifIndex (0)
59 {
60  NS_LOG_FUNCTION (this);
61  m_channel = CreateObject<BridgeChannel> ();
62 }
63 
65 {
66  NS_LOG_FUNCTION (this);
67  m_node = 0;
68  m_channel = 0;
70 }
71 
72 void
74 {
75  NS_LOG_FUNCTION (this);
76  for (std::vector<Ptr<NetDevice> >::iterator iter = m_ifaces.begin (); iter != m_ifaces.end (); iter++)
77  {
78  *iter = 0;
79  }
80  m_ifaces.clear ();
81  m_node = 0;
82  m_channel = 0;
85 
86 }
87 
88 //-----------------------------------------------------------------------------
89 // NetDevice interface implementation
90 //-----------------------------------------------------------------------------
91 
92 void
93 MeshPointDevice::ReceiveFromDevice (Ptr<NetDevice> incomingPort, Ptr<const Packet> packet, uint16_t protocol,
94  Address const &src, Address const &dst, PacketType packetType)
95 {
96  NS_LOG_FUNCTION (this << incomingPort << packet);
97  NS_LOG_DEBUG ("UID is " << packet->GetUid ());
98  const Mac48Address src48 = Mac48Address::ConvertFrom (src);
99  const Mac48Address dst48 = Mac48Address::ConvertFrom (dst);
100  uint16_t& realProtocol = protocol;
101  NS_LOG_DEBUG ("SRC=" << src48 << ", DST = " << dst48 << ", I am: " << m_address);
102  if (!m_promiscRxCallback.IsNull ())
103  {
104  m_promiscRxCallback (this, packet, protocol, src, dst, packetType);
105  }
106  if (dst48.IsGroup ())
107  {
108  Ptr<Packet> packet_copy = packet->Copy ();
109  if (m_routingProtocol->RemoveRoutingStuff (incomingPort->GetIfIndex (), src48, dst48, packet_copy, realProtocol))
110  {
111  m_rxCallback (this, packet_copy, realProtocol, src);
112  NS_LOG_DEBUG ("Forwarding from " << src48 << " to " << dst48 << " at " << m_address);
113  Forward (incomingPort, packet, protocol, src48, dst48);
114 
116  m_rxStats.broadcastDataBytes += packet->GetSize ();
117  }
118  return;
119  }
120  if (dst48 == m_address)
121  {
122  Ptr<Packet> packet_copy = packet->Copy ();
123  if (m_routingProtocol->RemoveRoutingStuff (incomingPort->GetIfIndex (), src48, dst48, packet_copy, realProtocol))
124  {
125  m_rxCallback (this, packet_copy, realProtocol, src);
127  m_rxStats.unicastDataBytes += packet->GetSize ();
128  }
129  return;
130  }
131  else
132  Forward (incomingPort, packet->Copy (), protocol, src48, dst48);
133 }
134 
135 void
136 MeshPointDevice::Forward (Ptr<NetDevice> inport, Ptr<const Packet> packet, uint16_t protocol,
137  const Mac48Address src, const Mac48Address dst)
138 {
139  NS_LOG_FUNCTION (this << inport << packet << protocol << src << dst);
140  // pass through routing protocol
141  NS_LOG_DEBUG ("Forwarding from " << src << " to " << dst << " at " << m_address);
142  bool result = m_routingProtocol->RequestRoute (inport->GetIfIndex (), src, dst, packet, protocol, MakeCallback (
143  &MeshPointDevice::DoSend, this));
144  if (result == false)
145  {
146  NS_LOG_DEBUG ("Request to forward packet " << packet << " to destination " << dst << " failed; dropping packet");
147  }
148 }
149 
150 void
151 MeshPointDevice::SetIfIndex (const uint32_t index)
152 {
153  NS_LOG_FUNCTION (this);
154  m_ifIndex = index;
155 }
156 
157 uint32_t
159 {
160  NS_LOG_FUNCTION (this);
161  return m_ifIndex;
162 }
163 
166 {
167  NS_LOG_FUNCTION (this);
168  return m_channel;
169 }
170 
171 Address
173 {
174  NS_LOG_FUNCTION (this);
175  return m_address;
176 }
177 
178 void
180 {
181  NS_LOG_FUNCTION (this);
182  NS_LOG_WARN ("Manual changing mesh point address can cause routing errors.");
184 }
185 
186 bool
187 MeshPointDevice::SetMtu (const uint16_t mtu)
188 {
189  NS_LOG_FUNCTION (this);
190  m_mtu = mtu;
191  return true;
192 }
193 
194 uint16_t
196 {
197  NS_LOG_FUNCTION (this);
198  return m_mtu;
199 }
200 
201 bool
203 {
204  NS_LOG_FUNCTION (this);
205  return true;
206 }
207 
208 void
210 {
211  NS_LOG_FUNCTION (this);
212  // do nothing
213  NS_LOG_WARN ("AddLinkChangeCallback does nothing");
214 }
215 
216 bool
218 {
219  NS_LOG_FUNCTION (this);
220  return true;
221 }
222 
223 Address
225 {
226  NS_LOG_FUNCTION (this);
227  return Mac48Address ("ff:ff:ff:ff:ff:ff");
228 }
229 
230 bool
232 {
233  NS_LOG_FUNCTION (this);
234  return true;
235 }
236 
237 Address
239 {
240  NS_LOG_FUNCTION (this << multicastGroup);
241  Mac48Address multicast = Mac48Address::GetMulticast (multicastGroup);
242  return multicast;
243 }
244 
245 bool
247 {
248  NS_LOG_FUNCTION (this);
249  return false;
250 }
251 
252 bool
254 {
255  NS_LOG_FUNCTION (this);
256  return false;
257 }
258 
259 bool
260 MeshPointDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
261 {
262  NS_LOG_FUNCTION (this);
263  const Mac48Address dst48 = Mac48Address::ConvertFrom (dest);
264  return m_routingProtocol->RequestRoute (m_ifIndex, m_address, dst48, packet, protocolNumber, MakeCallback (
265  &MeshPointDevice::DoSend, this));
266 }
267 
268 bool
269 MeshPointDevice::SendFrom (Ptr<Packet> packet, const Address& src, const Address& dest,
270  uint16_t protocolNumber)
271 {
272  NS_LOG_FUNCTION (this);
273  const Mac48Address src48 = Mac48Address::ConvertFrom (src);
274  const Mac48Address dst48 = Mac48Address::ConvertFrom (dest);
275  return m_routingProtocol->RequestRoute (m_ifIndex, src48, dst48, packet, protocolNumber, MakeCallback (
276  &MeshPointDevice::DoSend, this));
277 }
278 
279 Ptr<Node>
281 {
282  NS_LOG_FUNCTION (this);
283  return m_node;
284 }
285 
286 void
288 {
289  NS_LOG_FUNCTION (this);
290  m_node = node;
291 }
292 
293 bool
295 {
296  NS_LOG_FUNCTION (this);
297  return true;
298 }
299 
300 void
302 {
303  NS_LOG_FUNCTION (this);
304  m_rxCallback = cb;
305 }
306 
307 void
309 {
310  NS_LOG_FUNCTION (this);
311  m_promiscRxCallback = cb;
312 }
313 
314 bool
316 {
317  NS_LOG_FUNCTION (this);
318  return false; // don't allow to bridge mesh network with something else.
319 }
320 
321 Address
323 {
324  NS_LOG_FUNCTION (this << addr);
325  return Mac48Address::GetMulticast (addr);
326 }
327 
328 //-----------------------------------------------------------------------------
329 // Interfaces
330 //-----------------------------------------------------------------------------
331 uint32_t
333 {
334  NS_LOG_FUNCTION (this);
335  return m_ifaces.size ();
336 }
337 
340 {
341  NS_LOG_FUNCTION (this << n);
342  for (std::vector<Ptr<NetDevice> >::const_iterator i = m_ifaces.begin (); i != m_ifaces.end (); i++)
343  {
344  if ((*i)->GetIfIndex () == n)
345  {
346  return (*i);
347  }
348  }
349  NS_FATAL_ERROR ("Mesh point interface is not found by index");
350  return 0;
351 }
352 std::vector<Ptr<NetDevice> >
354 {
355  return m_ifaces;
356 }
357 void
359 {
360  NS_LOG_FUNCTION (this << iface);
361 
362  NS_ASSERT (iface != this);
363  if (!Mac48Address::IsMatchingType (iface->GetAddress ()))
364  {
365  NS_FATAL_ERROR ("Device does not support eui 48 addresses: cannot be used as a mesh point interface.");
366  }
367  if (!iface->SupportsSendFrom ())
368  {
369  NS_FATAL_ERROR ("Device does not support SendFrom: cannot be used as a mesh point interface.");
370  }
371 
372  // Mesh point has MAC address of it's first interface
373  if (m_ifaces.empty ())
374  {
375  m_address = Mac48Address::ConvertFrom (iface->GetAddress ());
376  }
377  Ptr<WifiNetDevice> wifiNetDev = iface->GetObject<WifiNetDevice> ();
378  if (wifiNetDev == 0)
379  {
380  NS_FATAL_ERROR ("Device is not a WiFi NIC: cannot be used as a mesh point interface.");
381  }
382  Ptr<MeshWifiInterfaceMac> ifaceMac = wifiNetDev->GetMac ()->GetObject<MeshWifiInterfaceMac> ();
383  if (ifaceMac == 0)
384  {
386  "WiFi device doesn't have correct MAC installed: cannot be used as a mesh point interface.");
387  }
388  ifaceMac->SetMeshPointAddress (m_address);
389 
390  // Receive frames from this interface
391  m_node->RegisterProtocolHandler (MakeCallback (&MeshPointDevice::ReceiveFromDevice, this), 0, iface, /*promiscuous = */
392  true);
393  m_ifaces.push_back (iface);
394  m_channel->AddChannel (iface->GetChannel ());
395 }
396 
397 //-----------------------------------------------------------------------------
398 // Protocols
399 //-----------------------------------------------------------------------------
400 
401 void
403 {
404  NS_LOG_FUNCTION (this << protocol);
405  NS_ASSERT_MSG (PeekPointer (protocol->GetMeshPoint ()) == this,
406  "Routing protocol must be installed on mesh point to be useful.");
407  m_routingProtocol = protocol;
408 }
409 
412 {
413  NS_LOG_FUNCTION (this);
414  return m_routingProtocol;
415 }
416 
417 void
419  uint16_t protocol, uint32_t outIface)
420 {
421  NS_LOG_FUNCTION (this << success << packet << src << dst << protocol << outIface);
422  if (!success)
423  {
424  NS_LOG_DEBUG ("Resolve failed");
425  return;
426  }
427 
428  // Count statistics
429  Statistics * stats = ((src == m_address) ? &m_txStats : &m_fwdStats);
430 
431  if (dst.IsBroadcast ())
432  {
433  stats->broadcastData++;
434  stats->broadcastDataBytes += packet->GetSize ();
435  }
436  else
437  {
438  stats->unicastData++;
439  stats->unicastDataBytes += packet->GetSize ();
440  }
441 
442  // Send
443  if (outIface != 0xffffffff)
444  {
445  GetInterface (outIface)->SendFrom (packet, src, dst, protocol);
446  }
447  else
448  {
449  for (std::vector<Ptr<NetDevice> >::iterator i = m_ifaces.begin (); i != m_ifaces.end (); i++)
450  {
451  (*i)->SendFrom (packet->Copy (), src, dst, protocol);
452  }
453  }
454 }
456  unicastData (0), unicastDataBytes (0), broadcastData (0), broadcastDataBytes (0)
457 {
458  NS_LOG_FUNCTION (this);
459 }
460 
461 void
462 MeshPointDevice::Report (std::ostream & os) const
463 {
464  NS_LOG_FUNCTION (this);
465  os << "<Statistics" << std::endl <<
466  "txUnicastData=\"" << m_txStats.unicastData << "\"" << std::endl <<
467  "txUnicastDataBytes=\"" << m_txStats.unicastDataBytes << "\"" << std::endl <<
468  "txBroadcastData=\"" << m_txStats.broadcastData << "\"" << std::endl <<
469  "txBroadcastDataBytes=\"" << m_txStats.broadcastDataBytes << "\"" << std::endl <<
470  "rxUnicastData=\"" << m_rxStats.unicastData << "\"" << std::endl <<
471  "rxUnicastDataBytes=\"" << m_rxStats.unicastDataBytes << "\"" << std::endl <<
472  "rxBroadcastData=\"" << m_rxStats.broadcastData << "\"" << std::endl <<
473  "rxBroadcastDataBytes=\"" << m_rxStats.broadcastDataBytes << "\"" << std::endl <<
474  "fwdUnicastData=\"" << m_fwdStats.unicastData << "\"" << std::endl <<
475  "fwdUnicastDataBytes=\"" << m_fwdStats.unicastDataBytes << "\"" << std::endl <<
476  "fwdBroadcastData=\"" << m_fwdStats.broadcastData << "\"" << std::endl <<
477  "fwdBroadcastDataBytes=\"" << m_fwdStats.broadcastDataBytes << "\"" << std::endl <<
478  "/>" << std::endl;
479 }
480 
481 void
483 {
484  NS_LOG_FUNCTION (this);
485  m_rxStats = Statistics ();
486  m_txStats = Statistics ();
487  m_fwdStats = Statistics ();
488 }
489 
490 } // namespace ns3
static bool IsMatchingType(const Address &address)
bool IsBroadcast(void) const
uint64_t GetUid(void) const
Returns the packet&#39;s Uid.
Definition: packet.cc:390
Mac48Address m_address
Mesh point MAC address, supposed to be the address of the first added interface.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Ptr< MeshL2RoutingProtocol > GetRoutingProtocol() const
Access current routing protocol.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint32_t GetNInterfaces() const
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
NetDevice::ReceiveCallback m_rxCallback
Receive action.
Ptr< AttributeChecker > MakePointerChecker(void)
Create a PointerChecker for a type.
Definition: pointer.h:224
void ResetStats()
Reset statistics counters.
virtual bool SetMtu(const uint16_t mtu)
virtual ~MeshPointDevice()
D-tor.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:564
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
virtual Ptr< Node > GetNode() const
virtual void AddLinkChangeCallback(Callback< void > callback)
Statistics m_txStats
transmit statistics
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
virtual bool IsLinkUp() const
Ptr< MeshL2RoutingProtocol > m_routingProtocol
Current routing protocol, used mainly by GetRoutingProtocol.
virtual void DoDispose()
Destructor implementation.
virtual bool NeedsArp() const
Ptr< BridgeChannel > m_channel
Virtual channel for upper layers.
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
a polymophic address class
Definition: address.h:90
virtual void SetAddress(Address a)
Set the address of this interface.
virtual bool IsBroadcast() const
virtual bool IsBridge() const
Return true if the net device is acting as a bridge.
void SetRoutingProtocol(Ptr< MeshL2RoutingProtocol > protocol)
Register routing protocol to be used. Protocol must be already installed on this mesh point...
Statistics m_rxStats
Counters.
virtual bool SupportsSendFrom() const
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: pointer.h:220
virtual Address GetAddress() const
static Mac48Address GetMulticast(Ipv4Address address)
void Forward(Ptr< NetDevice > incomingPort, Ptr< const Packet > packet, uint16_t protocol, const Mac48Address src, const Mac48Address dst)
Forward packet down to interfaces.
virtual Address GetBroadcast() const
Hold an unsigned integer type.
Definition: uinteger.h:44
Hold together all Wifi-related objects.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void AddInterface(Ptr< NetDevice > port)
Attach new interface to the station.
uint32_t broadcastDataBytes
broadcast data bytes
virtual void SetNode(Ptr< Node > node)
static Mac48Address ConvertFrom(const Address &address)
uint16_t m_mtu
MTU in bytes.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual uint32_t GetIfIndex() const
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
std::vector< Ptr< NetDevice > > GetInterfaces() const
an EUI-48 address
Definition: mac48-address.h:43
Statistics m_fwdStats
forward statistics
Ptr< Node > m_node
Parent node.
void DoSend(bool success, Ptr< Packet > packet, Mac48Address src, Mac48Address dst, uint16_t protocol, uint32_t iface)
Response callback for L2 routing protocol.
uint32_t unicastDataBytes
unicast data bytes
virtual Ptr< Channel > GetChannel() const
bool IsGroup(void) const
static TypeId GetTypeId()
Get the type ID.
Virtual net device modeling mesh point.
uint32_t m_ifIndex
If index.
uint32_t broadcastData
broadcast data
Interface for L2 mesh routing protocol and mesh point communication.
void RegisterProtocolHandler(ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device, bool promiscuous=false)
Definition: node.cc:227
MeshPointDevice()
C-tor create empty (without interfaces and protocols) mesh point.
virtual uint16_t GetMtu() const
Describes an IPv6 address.
Definition: ipv6-address.h:49
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ptr< WifiMac > GetMac(void) const
Network layer to device interface.
Definition: net-device.h:95
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:264
virtual bool IsPointToPoint() const
Return true if the net device is on a point-to-point link.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:272
std::vector< Ptr< NetDevice > > m_ifaces
List of interfaces.
virtual void SetIfIndex(const uint32_t index)
Ptr< NetDevice > GetInterface(uint32_t id) const
void Report(std::ostream &os) const
Print statistics counters.
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:296
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
void ReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, Address const &source, Address const &destination, PacketType packetType)
Receive packet from interface.
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
NetDevice::PromiscReceiveCallback m_promiscRxCallback
Promisc receive action.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
Basic MAC of mesh point Wi-Fi interface.
virtual bool IsMulticast() const