A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("MeshPointDevice");
31 
32 namespace ns3 {
33 
34 NS_OBJECT_ENSURE_REGISTERED (MeshPointDevice);
35 
36 TypeId
38 {
39  static TypeId tid = TypeId ("ns3::MeshPointDevice")
40  .SetParent<NetDevice> ()
41  .AddConstructor<MeshPointDevice> ()
42  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
43  UintegerValue (0xffff),
44  MakeUintegerAccessor (&MeshPointDevice::SetMtu,
46  MakeUintegerChecker<uint16_t> ())
47  .AddAttribute ( "RoutingProtocol",
48  "The mesh routing protocol used by this mesh point.",
49  PointerValue (),
50  MakePointerAccessor (
54  return tid;
55 }
56 
58  m_ifIndex (0)
59 {
61  m_channel = CreateObject<BridgeChannel> ();
62 }
63 
65 {
67  m_node = 0;
68  m_channel = 0;
70 }
71 
72 void
74 {
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 {
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  Forward (incomingPort, packet, protocol, src48, dst48);
113 
115  m_rxStats.broadcastDataBytes += packet->GetSize ();
116  }
117  return;
118  }
119  if (dst48 == m_address)
120  {
121  Ptr<Packet> packet_copy = packet->Copy ();
122  if (m_routingProtocol->RemoveRoutingStuff (incomingPort->GetIfIndex (), src48, dst48, packet_copy, realProtocol))
123  {
124  m_rxCallback (this, packet_copy, realProtocol, src);
126  m_rxStats.unicastDataBytes += packet->GetSize ();
127  }
128  return;
129  }
130  else
131  Forward (incomingPort, packet->Copy (), protocol, src48, dst48);
132 }
133 
134 void
135 MeshPointDevice::Forward (Ptr<NetDevice> inport, Ptr<const Packet> packet, uint16_t protocol,
136  const Mac48Address src, const Mac48Address dst)
137 {
138  // pass through routing protocol
139  m_routingProtocol->RequestRoute (inport->GetIfIndex (), src, dst, packet, protocol, MakeCallback (
140  &MeshPointDevice::DoSend, this));
141 }
142 
143 void
144 MeshPointDevice::SetIfIndex (const uint32_t index)
145 {
147  m_ifIndex = index;
148 }
149 
150 uint32_t
152 {
154  return m_ifIndex;
155 }
156 
159 {
161  return m_channel;
162 }
163 
164 Address
166 {
168  return m_address;
169 }
170 
171 void
173 {
174  NS_LOG_WARN ("Manual changing mesh point address can cause routing errors.");
176 }
177 
178 bool
179 MeshPointDevice::SetMtu (const uint16_t mtu)
180 {
182  m_mtu = mtu;
183  return true;
184 }
185 
186 uint16_t
188 {
190  return m_mtu;
191 }
192 
193 bool
195 {
197  return true;
198 }
199 
200 void
202 {
203  // do nothing
204 }
205 
206 bool
208 {
210  return true;
211 }
212 
213 Address
215 {
217  return Mac48Address ("ff:ff:ff:ff:ff:ff");
218 }
219 
220 bool
222 {
224  return true;
225 }
226 
227 Address
229 {
230  NS_LOG_FUNCTION (this << multicastGroup);
231  Mac48Address multicast = Mac48Address::GetMulticast (multicastGroup);
232  return multicast;
233 }
234 
235 bool
237 {
239  return false;
240 }
241 
242 bool
244 {
246  return false;
247 }
248 
249 bool
250 MeshPointDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
251 {
252  const Mac48Address dst48 = Mac48Address::ConvertFrom (dest);
253  return m_routingProtocol->RequestRoute (m_ifIndex, m_address, dst48, packet, protocolNumber, MakeCallback (
254  &MeshPointDevice::DoSend, this));
255 }
256 
257 bool
258 MeshPointDevice::SendFrom (Ptr<Packet> packet, const Address& src, const Address& dest,
259  uint16_t protocolNumber)
260 {
261  const Mac48Address src48 = Mac48Address::ConvertFrom (src);
262  const Mac48Address dst48 = Mac48Address::ConvertFrom (dest);
263  return m_routingProtocol->RequestRoute (m_ifIndex, src48, dst48, packet, protocolNumber, MakeCallback (
264  &MeshPointDevice::DoSend, this));
265 }
266 
267 Ptr<Node>
269 {
271  return m_node;
272 }
273 
274 void
276 {
278  m_node = node;
279 }
280 
281 bool
283 {
285  return true;
286 }
287 
288 void
290 {
292  m_rxCallback = cb;
293 }
294 
295 void
297 {
299  m_promiscRxCallback = cb;
300 }
301 
302 bool
304 {
306  return false; // don't allow to bridge mesh network with something else.
307 }
308 
309 Address
311 {
312  NS_LOG_FUNCTION (this << addr);
313  return Mac48Address::GetMulticast (addr);
314 }
315 
316 //-----------------------------------------------------------------------------
317 // Interfaces
318 //-----------------------------------------------------------------------------
319 uint32_t
321 {
323  return m_ifaces.size ();
324 }
325 
328 {
329  for (std::vector<Ptr<NetDevice> >::const_iterator i = m_ifaces.begin (); i != m_ifaces.end (); i++)
330  {
331  if ((*i)->GetIfIndex () == n)
332  {
333  return (*i);
334  }
335  }
336  NS_FATAL_ERROR ("Mesh point interface is not found by index");
337  return 0;
338 }
339 std::vector<Ptr<NetDevice> >
341 {
342  return m_ifaces;
343 }
344 void
346 {
348 
349  NS_ASSERT (iface != this);
350  if (!Mac48Address::IsMatchingType (iface->GetAddress ()))
351  {
352  NS_FATAL_ERROR ("Device does not support eui 48 addresses: cannot be used as a mesh point interface.");
353  }
354  if (!iface->SupportsSendFrom ())
355  {
356  NS_FATAL_ERROR ("Device does not support SendFrom: cannot be used as a mesh point interface.");
357  }
358 
359  // Mesh point has MAC address of it's first interface
360  if (m_ifaces.empty ())
361  {
362  m_address = Mac48Address::ConvertFrom (iface->GetAddress ());
363  }
364  Ptr<WifiNetDevice> wifiNetDev = iface->GetObject<WifiNetDevice> ();
365  if (wifiNetDev == 0)
366  {
367  NS_FATAL_ERROR ("Device is not a WiFi NIC: cannot be used as a mesh point interface.");
368  }
369  Ptr<MeshWifiInterfaceMac> ifaceMac = wifiNetDev->GetMac ()->GetObject<MeshWifiInterfaceMac> ();
370  if (ifaceMac == 0)
371  {
373  "WiFi device doesn't have correct MAC installed: cannot be used as a mesh point interface.");
374  }
375  ifaceMac->SetMeshPointAddress (m_address);
376 
377  // Receive frames from this interface
378  m_node->RegisterProtocolHandler (MakeCallback (&MeshPointDevice::ReceiveFromDevice, this), 0, iface, /*promiscuous = */
379  true);
380  m_ifaces.push_back (iface);
381  m_channel->AddChannel (iface->GetChannel ());
382 }
383 
384 //-----------------------------------------------------------------------------
385 // Protocols
386 //-----------------------------------------------------------------------------
387 
388 void
390 {
392  NS_ASSERT_MSG (PeekPointer (protocol->GetMeshPoint ()) == this,
393  "Routing protocol must be installed on mesh point to be useful.");
394  m_routingProtocol = protocol;
395 }
396 
399 {
400  return m_routingProtocol;
401 }
402 
403 void
405  uint16_t protocol, uint32_t outIface)
406 {
407  if (!success)
408  {
409  NS_LOG_DEBUG ("Resolve failed");
410  return;
411  }
412 
413  // Count statistics
414  Statistics * stats = ((src == m_address) ? &m_txStats : &m_fwdStats);
415 
416  if (dst.IsBroadcast ())
417  {
418  stats->broadcastData++;
419  stats->broadcastDataBytes += packet->GetSize ();
420  }
421  else
422  {
423  stats->unicastData++;
424  stats->unicastDataBytes += packet->GetSize ();
425  }
426 
427  // Send
428  if (outIface != 0xffffffff)
429  {
430  GetInterface (outIface)->SendFrom (packet, src, dst, protocol);
431  }
432  else
433  {
434  for (std::vector<Ptr<NetDevice> >::iterator i = m_ifaces.begin (); i != m_ifaces.end (); i++)
435  {
436  (*i)->SendFrom (packet->Copy (), src, dst, protocol);
437  }
438  }
439 }
441  unicastData (0), unicastDataBytes (0), broadcastData (0), broadcastDataBytes (0)
442 {
443 }
444 
445 void
446 MeshPointDevice::Report (std::ostream & os) const
447 {
448  os << "<Statistics" << std::endl <<
449  "txUnicastData=\"" << m_txStats.unicastData << "\"" << std::endl <<
450  "txUnicastDataBytes=\"" << m_txStats.unicastDataBytes << "\"" << std::endl <<
451  "txBroadcastData=\"" << m_txStats.broadcastData << "\"" << std::endl <<
452  "txBroadcastDataBytes=\"" << m_txStats.broadcastDataBytes << "\"" << std::endl <<
453  "rxUnicastData=\"" << m_rxStats.unicastData << "\"" << std::endl <<
454  "rxUnicastDataBytes=\"" << m_rxStats.unicastDataBytes << "\"" << std::endl <<
455  "rxBroadcastData=\"" << m_rxStats.broadcastData << "\"" << std::endl <<
456  "rxBroadcastDataBytes=\"" << m_rxStats.broadcastDataBytes << "\"" << std::endl <<
457  "fwdUnicastData=\"" << m_fwdStats.unicastData << "\"" << std::endl <<
458  "fwdUnicastDataBytes=\"" << m_fwdStats.unicastDataBytes << "\"" << std::endl <<
459  "fwdBroadcastData=\"" << m_fwdStats.broadcastData << "\"" << std::endl <<
460  "fwdBroadcastDataBytes=\"" << m_fwdStats.broadcastDataBytes << "\"" << std::endl <<
461  "/>" << std::endl;
462 }
463 
464 void
466 {
467  m_rxStats = Statistics ();
468  m_txStats = Statistics ();
469  m_fwdStats = Statistics ();
470 }
471 
472 } // 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:60
#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 the class in the ns-3 factory.
Definition: object-base.h:38
NetDevice::ReceiveCallback m_rxCallback
Receive action.
void ResetStats()
Reset statistics counters.
virtual bool IsBroadcast() const
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:393
virtual bool SetMtu(const uint16_t mtu)
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1018
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:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
bool IsBroadcast(void) const
virtual void AddLinkChangeCallback(Callback< void > callback)
Statistics m_txStats
Counters.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:335
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:95
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
virtual bool IsLinkUp() const
virtual bool IsMulticast() const
Ptr< MeshL2RoutingProtocol > m_routingProtocol
Current routing protocol, used mainly by GetRoutingProtocol.
virtual void DoDispose()
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Ptr< BridgeChannel > m_channel
Virtual channel for upper layers.
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
a polymophic address class
Definition: address.h:86
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.
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:46
Ptr< AttributeChecker > MakePointerChecker(void)
Definition: pointer.h:178
T * PeekPointer(const Ptr< T > &p)
Definition: ptr.h:280
Hold together all Wifi-related objects.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
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.
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:122
uint16_t m_mtu
MTU in bytes.
hold objects of type Ptr
Definition: pointer.h:33
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:41
Statistics m_fwdStats
Counters.
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.
virtual Address GetBroadcast() const
static TypeId GetTypeId()
Object type ID for NS3 object system.
virtual Ptr< Channel > GetChannel() const
uint32_t m_ifIndex
If index.
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:219
#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:84
MeshPointDevice()
C-tor create empty (without interfaces and protocols) mesh point.
Describes an IPv6 address.
Definition: ipv6-address.h:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
Network layer to device interface.
Definition: net-device.h:75
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:203
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:213
std::vector< Ptr< NetDevice > > m_ifaces
List of interfaces.
virtual void SetIfIndex(const uint32_t index)
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:274
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.
a unique identifier for an interface.
Definition: type-id.h:49
virtual bool SupportsSendFrom() const
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
Basic MAC of mesh point Wi-Fi interface.
Ptr< MeshL2RoutingProtocol > GetRoutingProtocol() const
Access current routing protocol.