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/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 namespace ns3 {
31 
32 NS_LOG_COMPONENT_DEFINE ("MeshPointDevice");
33 
34 NS_OBJECT_ENSURE_REGISTERED (MeshPointDevice);
35 
36 TypeId
38 {
39  static TypeId tid = TypeId ("ns3::MeshPointDevice")
40  .SetParent<NetDevice> ()
41  .SetGroupName ("Mesh")
42  .AddConstructor<MeshPointDevice> ()
43  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
44  UintegerValue (0xffff),
47  MakeUintegerChecker<uint16_t> ())
48  .AddAttribute ( "RoutingProtocol",
49  "The mesh routing protocol used by this mesh point.",
50  PointerValue (),
55  return tid;
56 }
57 
59  m_ifIndex (0)
60 {
61  NS_LOG_FUNCTION (this);
62  m_channel = CreateObject<BridgeChannel> ();
63 }
64 
66 {
67  NS_LOG_FUNCTION (this);
68  m_node = 0;
69  m_channel = 0;
71 }
72 
73 void
75 {
76  NS_LOG_FUNCTION (this);
77  for (std::vector<Ptr<NetDevice> >::iterator iter = m_ifaces.begin (); iter != m_ifaces.end (); iter++)
78  {
79  *iter = 0;
80  }
81  m_ifaces.clear ();
82  m_node = 0;
83  m_channel = 0;
86 
87 }
88 
89 //-----------------------------------------------------------------------------
90 // NetDevice interface implementation
91 //-----------------------------------------------------------------------------
92 
93 void
94 MeshPointDevice::ReceiveFromDevice (Ptr<NetDevice> incomingPort, Ptr<const Packet> packet, uint16_t protocol,
95  Address const &src, Address const &dst, PacketType packetType)
96 {
97  NS_LOG_FUNCTION (this << incomingPort << packet);
98  NS_LOG_DEBUG ("UID is " << packet->GetUid ());
99  const Mac48Address src48 = Mac48Address::ConvertFrom (src);
100  const Mac48Address dst48 = Mac48Address::ConvertFrom (dst);
101  uint16_t& realProtocol = protocol;
102  NS_LOG_DEBUG ("SRC=" << src48 << ", DST = " << dst48 << ", I am: " << m_address);
103  if (!m_promiscRxCallback.IsNull ())
104  {
105  m_promiscRxCallback (this, packet, protocol, src, dst, packetType);
106  }
107  if (dst48.IsGroup ())
108  {
109  Ptr<Packet> packet_copy = packet->Copy ();
110  if (m_routingProtocol->RemoveRoutingStuff (incomingPort->GetIfIndex (), src48, dst48, packet_copy, realProtocol))
111  {
112  m_rxCallback (this, packet_copy, realProtocol, src);
113  NS_LOG_DEBUG ("Forwarding from " << src48 << " to " << dst48 << " at " << m_address);
114  Forward (incomingPort, packet, protocol, src48, dst48);
115 
117  m_rxStats.broadcastDataBytes += packet->GetSize ();
118  }
119  return;
120  }
121  if (dst48 == m_address)
122  {
123  Ptr<Packet> packet_copy = packet->Copy ();
124  if (m_routingProtocol->RemoveRoutingStuff (incomingPort->GetIfIndex (), src48, dst48, packet_copy, realProtocol))
125  {
126  m_rxCallback (this, packet_copy, realProtocol, src);
128  m_rxStats.unicastDataBytes += packet->GetSize ();
129  }
130  return;
131  }
132  else
133  Forward (incomingPort, packet->Copy (), protocol, src48, dst48);
134 }
135 
136 void
137 MeshPointDevice::Forward (Ptr<NetDevice> inport, Ptr<const Packet> packet, uint16_t protocol,
138  const Mac48Address src, const Mac48Address dst)
139 {
140  NS_LOG_FUNCTION (this << inport << packet << protocol << src << dst);
141  // pass through routing protocol
142  NS_LOG_DEBUG ("Forwarding from " << src << " to " << dst << " at " << m_address);
143  bool result = m_routingProtocol->RequestRoute (inport->GetIfIndex (), src, dst, packet, protocol, MakeCallback (
144  &MeshPointDevice::DoSend, this));
145  if (result == false)
146  {
147  NS_LOG_DEBUG ("Request to forward packet " << packet << " to destination " << dst << " failed; dropping packet");
148  }
149 }
150 
151 void
152 MeshPointDevice::SetIfIndex (const uint32_t index)
153 {
154  NS_LOG_FUNCTION (this);
155  m_ifIndex = index;
156 }
157 
158 uint32_t
160 {
161  NS_LOG_FUNCTION (this);
162  return m_ifIndex;
163 }
164 
167 {
168  NS_LOG_FUNCTION (this);
169  return m_channel;
170 }
171 
172 Address
174 {
175  NS_LOG_FUNCTION (this);
176  return m_address;
177 }
178 
179 void
181 {
182  NS_LOG_FUNCTION (this);
183  NS_LOG_WARN ("Manual changing mesh point address can cause routing errors.");
185 }
186 
187 bool
188 MeshPointDevice::SetMtu (const uint16_t mtu)
189 {
190  NS_LOG_FUNCTION (this);
191  m_mtu = mtu;
192  return true;
193 }
194 
195 uint16_t
197 {
198  NS_LOG_FUNCTION (this);
199  return m_mtu;
200 }
201 
202 bool
204 {
205  NS_LOG_FUNCTION (this);
206  return true;
207 }
208 
209 void
211 {
212  NS_LOG_FUNCTION (this);
213  // do nothing
214  NS_LOG_WARN ("AddLinkChangeCallback does nothing");
215 }
216 
217 bool
219 {
220  NS_LOG_FUNCTION (this);
221  return true;
222 }
223 
224 Address
226 {
227  NS_LOG_FUNCTION (this);
228  return Mac48Address ("ff:ff:ff:ff:ff:ff");
229 }
230 
231 bool
233 {
234  NS_LOG_FUNCTION (this);
235  return true;
236 }
237 
238 Address
240 {
241  NS_LOG_FUNCTION (this << multicastGroup);
242  Mac48Address multicast = Mac48Address::GetMulticast (multicastGroup);
243  return multicast;
244 }
245 
246 bool
248 {
249  NS_LOG_FUNCTION (this);
250  return false;
251 }
252 
253 bool
255 {
256  NS_LOG_FUNCTION (this);
257  return false;
258 }
259 
260 bool
261 MeshPointDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
262 {
263  NS_LOG_FUNCTION (this);
264  const Mac48Address dst48 = Mac48Address::ConvertFrom (dest);
265  return m_routingProtocol->RequestRoute (m_ifIndex, m_address, dst48, packet, protocolNumber, MakeCallback (
266  &MeshPointDevice::DoSend, this));
267 }
268 
269 bool
270 MeshPointDevice::SendFrom (Ptr<Packet> packet, const Address& src, const Address& dest,
271  uint16_t protocolNumber)
272 {
273  NS_LOG_FUNCTION (this);
274  const Mac48Address src48 = Mac48Address::ConvertFrom (src);
275  const Mac48Address dst48 = Mac48Address::ConvertFrom (dest);
276  return m_routingProtocol->RequestRoute (m_ifIndex, src48, dst48, packet, protocolNumber, MakeCallback (
277  &MeshPointDevice::DoSend, this));
278 }
279 
280 Ptr<Node>
282 {
283  NS_LOG_FUNCTION (this);
284  return m_node;
285 }
286 
287 void
289 {
290  NS_LOG_FUNCTION (this);
291  m_node = node;
292 }
293 
294 bool
296 {
297  NS_LOG_FUNCTION (this);
298  return true;
299 }
300 
301 void
303 {
304  NS_LOG_FUNCTION (this);
305  m_rxCallback = cb;
306 }
307 
308 void
310 {
311  NS_LOG_FUNCTION (this);
312  m_promiscRxCallback = cb;
313 }
314 
315 bool
317 {
318  NS_LOG_FUNCTION (this);
319  return false; // don't allow to bridge mesh network with something else.
320 }
321 
322 Address
324 {
325  NS_LOG_FUNCTION (this << addr);
326  return Mac48Address::GetMulticast (addr);
327 }
328 
329 //-----------------------------------------------------------------------------
330 // Interfaces
331 //-----------------------------------------------------------------------------
332 uint32_t
334 {
335  NS_LOG_FUNCTION (this);
336  return m_ifaces.size ();
337 }
338 
341 {
342  NS_LOG_FUNCTION (this << n);
343  for (std::vector<Ptr<NetDevice> >::const_iterator i = m_ifaces.begin (); i != m_ifaces.end (); i++)
344  {
345  if ((*i)->GetIfIndex () == n)
346  {
347  return (*i);
348  }
349  }
350  NS_FATAL_ERROR ("Mesh point interface is not found by index");
351  return 0;
352 }
353 std::vector<Ptr<NetDevice> >
355 {
356  return m_ifaces;
357 }
358 void
360 {
361  NS_LOG_FUNCTION (this << iface);
362 
363  NS_ASSERT (iface != this);
364  if (!Mac48Address::IsMatchingType (iface->GetAddress ()))
365  {
366  NS_FATAL_ERROR ("Device does not support eui 48 addresses: cannot be used as a mesh point interface.");
367  }
368  if (!iface->SupportsSendFrom ())
369  {
370  NS_FATAL_ERROR ("Device does not support SendFrom: cannot be used as a mesh point interface.");
371  }
372 
373  // Mesh point has MAC address of it's first interface
374  if (m_ifaces.empty ())
375  {
376  m_address = Mac48Address::ConvertFrom (iface->GetAddress ());
377  }
378  Ptr<WifiNetDevice> wifiNetDev = iface->GetObject<WifiNetDevice> ();
379  if (wifiNetDev == 0)
380  {
381  NS_FATAL_ERROR ("Device is not a WiFi NIC: cannot be used as a mesh point interface.");
382  }
383  Ptr<MeshWifiInterfaceMac> ifaceMac = wifiNetDev->GetMac ()->GetObject<MeshWifiInterfaceMac> ();
384  if (ifaceMac == 0)
385  {
387  "WiFi device doesn't have correct MAC installed: cannot be used as a mesh point interface.");
388  }
389  ifaceMac->SetMeshPointAddress (m_address);
390 
391  // Receive frames from this interface
392  m_node->RegisterProtocolHandler (MakeCallback (&MeshPointDevice::ReceiveFromDevice, this), 0, iface, /*promiscuous = */
393  true);
394  m_ifaces.push_back (iface);
395  m_channel->AddChannel (iface->GetChannel ());
396 }
397 
398 //-----------------------------------------------------------------------------
399 // Protocols
400 //-----------------------------------------------------------------------------
401 
402 void
404 {
405  NS_LOG_FUNCTION (this << protocol);
406  NS_ASSERT_MSG (PeekPointer (protocol->GetMeshPoint ()) == this,
407  "Routing protocol must be installed on mesh point to be useful.");
408  m_routingProtocol = protocol;
409 }
410 
413 {
414  NS_LOG_FUNCTION (this);
415  return m_routingProtocol;
416 }
417 
418 void
420  uint16_t protocol, uint32_t outIface)
421 {
422  NS_LOG_FUNCTION (this << success << packet << src << dst << protocol << outIface);
423  if (!success)
424  {
425  NS_LOG_DEBUG ("Resolve failed");
426  return;
427  }
428 
429  // Count statistics
430  Statistics * stats = ((src == m_address) ? &m_txStats : &m_fwdStats);
431 
432  if (dst.IsBroadcast ())
433  {
434  stats->broadcastData++;
435  stats->broadcastDataBytes += packet->GetSize ();
436  }
437  else
438  {
439  stats->unicastData++;
440  stats->unicastDataBytes += packet->GetSize ();
441  }
442 
443  // Send
444  if (outIface != 0xffffffff)
445  {
446  GetInterface (outIface)->SendFrom (packet, src, dst, protocol);
447  }
448  else
449  {
450  for (std::vector<Ptr<NetDevice> >::iterator i = m_ifaces.begin (); i != m_ifaces.end (); i++)
451  {
452  (*i)->SendFrom (packet->Copy (), src, dst, protocol);
453  }
454  }
455 }
457  unicastData (0), unicastDataBytes (0), broadcastData (0), broadcastDataBytes (0)
458 {
459  NS_LOG_FUNCTION (this);
460 }
461 
462 void
463 MeshPointDevice::Report (std::ostream & os) const
464 {
465  NS_LOG_FUNCTION (this);
466  os << "<Statistics" << std::endl <<
467  "txUnicastData=\"" << m_txStats.unicastData << "\"" << std::endl <<
468  "txUnicastDataBytes=\"" << m_txStats.unicastDataBytes << "\"" << std::endl <<
469  "txBroadcastData=\"" << m_txStats.broadcastData << "\"" << std::endl <<
470  "txBroadcastDataBytes=\"" << m_txStats.broadcastDataBytes << "\"" << std::endl <<
471  "rxUnicastData=\"" << m_rxStats.unicastData << "\"" << std::endl <<
472  "rxUnicastDataBytes=\"" << m_rxStats.unicastDataBytes << "\"" << std::endl <<
473  "rxBroadcastData=\"" << m_rxStats.broadcastData << "\"" << std::endl <<
474  "rxBroadcastDataBytes=\"" << m_rxStats.broadcastDataBytes << "\"" << std::endl <<
475  "fwdUnicastData=\"" << m_fwdStats.unicastData << "\"" << std::endl <<
476  "fwdUnicastDataBytes=\"" << m_fwdStats.unicastDataBytes << "\"" << std::endl <<
477  "fwdBroadcastData=\"" << m_fwdStats.broadcastData << "\"" << std::endl <<
478  "fwdBroadcastDataBytes=\"" << m_fwdStats.broadcastDataBytes << "\"" << std::endl <<
479  "/>" << std::endl;
480 }
481 
482 void
484 {
485  NS_LOG_FUNCTION (this);
486  m_rxStats = Statistics ();
487  m_txStats = Statistics ();
488  m_fwdStats = Statistics ();
489 }
490 
491 } // namespace ns3
uint32_t GetNInterfaces() const
static bool IsMatchingType(const Address &address)
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 "...
virtual bool IsBridge() const
Return true if the net device is acting as a bridge.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
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 IsBroadcast() const
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:367
virtual bool SetMtu(const uint16_t mtu)
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
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
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:296
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:201
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:796
bool IsBroadcast(void) 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
virtual bool IsMulticast() const
Ptr< MeshL2RoutingProtocol > m_routingProtocol
Current routing protocol, used mainly by GetRoutingProtocol.
virtual void DoDispose()
Destructor implementation.
Ptr< BridgeChannel > m_channel
Virtual channel for upper layers.
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
a polymophic address class
Definition: address.h:90
virtual Address GetAddress() const
std::vector< Ptr< NetDevice > > GetInterfaces() const
virtual void SetAddress(Address a)
Set the address of this interface.
Ptr< NetDevice > GetInterface(uint32_t id) const
void SetRoutingProtocol(Ptr< MeshL2RoutingProtocol > protocol)
Register routing protocol to be used. Protocol must be already installed on this mesh point...
Statistics m_rxStats
Counters.
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
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.
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
virtual uint32_t GetIfIndex() const
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
virtual Ptr< Node > GetNode() const
virtual uint16_t GetMtu() const
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)
void Report(std::ostream &os) const
Print statistics counters.
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
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.
Definition: pointer.h:36
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
bool IsGroup(void) 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 Address GetBroadcast() const
static TypeId GetTypeId()
Get the type ID.
virtual Ptr< Channel > GetChannel() const
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
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
MeshPointDevice()
C-tor create empty (without interfaces and protocols) mesh point.
Describes an IPv6 address.
Definition: ipv6-address.h:48
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
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:261
virtual bool NeedsArp() const
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:269
std::vector< Ptr< NetDevice > > m_ifaces
List of interfaces.
virtual void SetIfIndex(const uint32_t index)
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.
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
virtual bool SupportsSendFrom() const
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
Basic MAC of mesh point Wi-Fi interface.
Ptr< MeshL2RoutingProtocol > GetRoutingProtocol() const
Access current routing protocol.