A Discrete-Event Network Simulator
API
flame-protocol.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 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  */
20 
21 #include "flame-protocol.h"
22 #include "flame-protocol-mac.h"
23 #include "flame-header.h"
24 #include "flame-rtable.h"
25 #include "ns3/llc-snap-header.h"
26 #include "ns3/log.h"
27 #include "ns3/simulator.h"
28 #include "ns3/packet.h"
29 #include "ns3/mesh-point-device.h"
30 #include "ns3/wifi-net-device.h"
31 #include "ns3/mesh-point-device.h"
32 #include "ns3/mesh-wifi-interface-mac.h"
33 
34 namespace ns3 {
35 
36 NS_LOG_COMPONENT_DEFINE ("FlameProtocol");
37 
38 namespace flame {
39 
40 //-----------------------------------------------------------------------------
41 // FlameTag
42 //-----------------------------------------------------------------------------
44 NS_OBJECT_ENSURE_REGISTERED (FlameProtocol);
45 
46 TypeId
48 {
49  static TypeId tid = TypeId ("ns3::flame::FlameTag").SetParent<Tag> ().AddConstructor<FlameTag> ().SetGroupName ("Mesh");
50  return tid;
51 }
52 
53 TypeId
55 {
56  return GetTypeId ();
57 }
58 
59 uint32_t
61 {
62  return 12;
63 }
64 
65 void
67 {
68  uint8_t buf[6];
69  receiver.CopyTo (buf);
70  for (int j = 0; j < 6; j++)
71  {
72  i.WriteU8 (buf[j]);
73  }
74  transmitter.CopyTo (buf);
75  for (int j = 0; j < 6; j++)
76  {
77  i.WriteU8 (buf[j]);
78  }
79 
80 }
81 
82 void
84 {
85  uint8_t buf[6];
86  for (int j = 0; j < 6; j++)
87  {
88  buf[j] = i.ReadU8 ();
89  }
90  receiver.CopyFrom (buf);
91  for (int j = 0; j < 6; j++)
92  {
93  buf[j] = i.ReadU8 ();
94  }
95  transmitter.CopyFrom (buf);
96 
97 }
98 
99 void
100 FlameTag::Print (std::ostream &os) const
101 {
102  os << "receiver = " << receiver << ", transmitter = " << transmitter;
103 }
104 
105 //-----------------------------------------------------------------------------
106 // FlameProtocol
107 //-----------------------------------------------------------------------------
108 TypeId
110 {
111  static TypeId tid = TypeId ("ns3::flame::FlameProtocol")
113  .SetGroupName ("Mesh")
114  .AddConstructor<FlameProtocol> ()
115  .AddAttribute ( "BroadcastInterval",
116  "How often we must send broadcast packets",
117  TimeValue (Seconds (5)),
120  MakeTimeChecker ()
121  )
122  .AddAttribute ( "MaxCost",
123  "Cost threshold after which packet will be dropped",
124  UintegerValue (32),
127  MakeUintegerChecker<uint8_t> (3)
128  )
129  ;
130  return tid;
131 }
133  m_address (Mac48Address ()), m_broadcastInterval (Seconds (5)), m_lastBroadcast (Seconds (0)),
134  m_maxCost (32), m_myLastSeqno (1), m_rtable (CreateObject<FlameRtable> ())
135 {
136 }
138 {
139 }
140 void
142 {
143  m_interfaces.clear ();
144  m_rtable = 0;
145  m_mp = 0;
146 }
147 bool
148 FlameProtocol::RequestRoute (uint32_t sourceIface, const Mac48Address source, const Mac48Address destination,
149  Ptr<const Packet> const_packet, uint16_t protocolType, RouteReplyCallback routeReply)
150 {
151  Ptr<Packet> packet = const_packet->Copy ();
152  if (sourceIface == m_mp->GetIfIndex ())
153  {
154  //Packet from upper layer!
155  FlameTag tag;
156  if (packet->PeekPacketTag (tag))
157  {
158  NS_FATAL_ERROR ("FLAME tag is not supposed to be received from upper layers");
159  }
160  FlameRtable::LookupResult result = m_rtable->Lookup (destination);
161  if (result.retransmitter == Mac48Address::GetBroadcast ())
162  {
164  }
166  {
170  }
171  FlameHeader flameHdr;
172  flameHdr.AddCost (0);
173  flameHdr.SetSeqno (m_myLastSeqno++);
174  flameHdr.SetProtocol (protocolType);
175  flameHdr.SetOrigDst (destination);
176  flameHdr.SetOrigSrc (source);
177  m_stats.txBytes += packet->GetSize ();
178  packet->AddHeader (flameHdr);
179  tag.receiver = result.retransmitter;
180  if (result.retransmitter == Mac48Address::GetBroadcast ())
181  {
183  }
184  else
185  {
186  m_stats.txUnicast++;
187  }
188  NS_LOG_DEBUG ("Source: send packet with RA = " << tag.receiver);
189  packet->AddPacketTag (tag);
190  routeReply (true, packet, source, destination, FLAME_PROTOCOL, result.ifIndex);
191  }
192  else
193  {
194  FlameHeader flameHdr;
195  packet->RemoveHeader (flameHdr);
196  FlameTag tag;
197 
198  if (!packet->RemovePacketTag (tag))
199  {
200  NS_FATAL_ERROR ("FLAME tag must exist here");
201  }
202  if (destination == Mac48Address::GetBroadcast ())
203  {
204  //Broadcast always is forwarded as broadcast!
205  NS_ASSERT (HandleDataFrame (flameHdr.GetSeqno (), source, flameHdr, tag.transmitter, sourceIface));
207  flameHdr.AddCost (1);
208  m_stats.txBytes += packet->GetSize ();
209  packet->AddHeader (flameHdr);
210  packet->AddPacketTag (tag);
211  routeReply (true, packet, source, destination, FLAME_PROTOCOL, FlameRtable::INTERFACE_ANY);
213  return true;
214  }
215  else
216  {
217  // We check sequence only when forward unicast, because broadcast-checks were done
218  // inside remove routing stuff.
219  if (HandleDataFrame (flameHdr.GetSeqno (), source, flameHdr, tag.transmitter, sourceIface))
220  {
221  return false;
222  }
223  FlameRtable::LookupResult result = m_rtable->Lookup (destination);
224  if (tag.receiver != Mac48Address::GetBroadcast ())
225  {
226  if (result.retransmitter == Mac48Address::GetBroadcast ())
227  {
228  NS_LOG_DEBUG ("unicast packet dropped, because no route! I am " << GetAddress ()
229  << ", RA = " << tag.receiver << ", TA = " << tag.transmitter);
231  return false;
232  }
233  tag.receiver = result.retransmitter;
234  }
235  else
236  {
238  }
239  if (result.retransmitter == Mac48Address::GetBroadcast ())
240  {
242  }
243  else
244  {
245  m_stats.txUnicast++;
246  }
247  m_stats.txBytes += packet->GetSize ();
248  flameHdr.AddCost (1);
249  packet->AddHeader (flameHdr);
250  packet->AddPacketTag (tag);
251  routeReply (true, packet, source, destination, FLAME_PROTOCOL, result.ifIndex);
252  return true;
253  }
254  return true;
255  }
256  return false;
257 }
258 bool
259 FlameProtocol::RemoveRoutingStuff (uint32_t fromIface, const Mac48Address source,
260  const Mac48Address destination, Ptr<Packet> packet, uint16_t& protocolType)
261 {
262  //Filter seqno:
263  if (source == GetAddress ())
264  {
265  NS_LOG_DEBUG ("Dropped my own frame!");
266  return false;
267  }
268  FlameTag tag;
269  if (!packet->RemovePacketTag (tag))
270  {
271  NS_FATAL_ERROR ("FLAME tag must exist when packet is coming to protocol");
272  }
273  FlameHeader flameHdr;
274  packet->RemoveHeader (flameHdr);
275  if (HandleDataFrame (flameHdr.GetSeqno (), source, flameHdr, tag.transmitter, fromIface))
276  {
277  return false;
278  }
279  // Start PATH_UPDATE procedure if destination is our own address and last broadcast was sent more
280  // than broadcast interval ago or was not sent at all
281  if ((destination == GetAddress ()) && ((m_lastBroadcast + m_broadcastInterval < Simulator::Now ())
282  || (m_lastBroadcast == Seconds (0))))
283  {
284  Ptr<Packet> packet = Create<Packet> ();
285  m_mp->Send (packet, Mac48Address::GetBroadcast (), 0);
287  }
288  NS_ASSERT (protocolType == FLAME_PROTOCOL);
289  protocolType = flameHdr.GetProtocol ();
290  return true;
291 }
292 bool
294 {
295  m_mp = mp;
296  std::vector<Ptr<NetDevice> > interfaces = mp->GetInterfaces ();
297  for (std::vector<Ptr<NetDevice> >::const_iterator i = interfaces.begin (); i != interfaces.end (); i++)
298  {
299  // Checking for compatible net device
300  Ptr<WifiNetDevice> wifiNetDev = (*i)->GetObject<WifiNetDevice> ();
301  if (wifiNetDev == 0)
302  {
303  return false;
304  }
305  Ptr<MeshWifiInterfaceMac> mac = wifiNetDev->GetMac ()->GetObject<MeshWifiInterfaceMac> ();
306  if (mac == 0)
307  {
308  return false;
309  }
310  // Installing plugins:
311  Ptr<FlameProtocolMac> flameMac = Create<FlameProtocolMac> (this);
312  m_interfaces[wifiNetDev->GetIfIndex ()] = flameMac;
313  mac->SetBeaconGeneration (false);
314  mac->InstallPlugin (flameMac);
315  }
316  mp->SetRoutingProtocol (this);
317  // Mesh point aggregates all installed protocols
318  mp->AggregateObject (this);
319  m_address = Mac48Address::ConvertFrom (mp->GetAddress ()); //* address;
320  return true;
321 }
324 {
325  return m_address;
326 }
327 bool
328 FlameProtocol::HandleDataFrame (uint16_t seqno, Mac48Address source, const FlameHeader flameHdr,
329  Mac48Address receiver, uint32_t fromInterface)
330 {
331  if (source == GetAddress ())
332  {
334  return true;
335  }
336  FlameRtable::LookupResult result = m_rtable->Lookup (source);
337  if ((result.retransmitter != Mac48Address::GetBroadcast ()) && ((int16_t)(result.seqnum - seqno) >= 0))
338  {
339  return true;
340  }
341  if (flameHdr.GetCost () > m_maxCost)
342  {
344  return true;
345  }
346  m_rtable->AddPath (source, receiver, fromInterface, flameHdr.GetCost (), flameHdr.GetSeqno ());
347  return false;
348 }
349 //Statistics:
351  txUnicast (0), txBroadcast (0), txBytes (0), droppedTtl (0), totalDropped (0)
352 {
353 }
354 void
355 FlameProtocol::Statistics::Print (std::ostream & os) const
356 {
357  os << "<Statistics "
358  "txUnicast=\"" << txUnicast << "\" "
359  "txBroadcast=\"" << txBroadcast << "\" "
360  "txBytes=\"" << txBytes << "\" "
361  "droppedTtl=\"" << droppedTtl << "\" "
362  "totalDropped=\"" << totalDropped << "\"/>" << std::endl;
363 }
364 void
365 FlameProtocol::Report (std::ostream & os) const
366 {
367  os << "<Flame "
368  "address=\"" << m_address << "\"" << std::endl <<
369  "broadcastInterval=\"" << m_broadcastInterval.GetSeconds () << "\"" << std::endl <<
370  "maxCost=\"" << (uint16_t) m_maxCost << "\">" << std::endl;
371  m_stats.Print (os);
372  for (FlamePluginMap::const_iterator plugin = m_interfaces.begin (); plugin != m_interfaces.end (); plugin++)
373  {
374  plugin->second->Report (os);
375  }
376  os << "</Flame>" << std::endl;
377 }
378 void
380 {
381  m_stats = Statistics ();
382  for (FlamePluginMap::const_iterator plugin = m_interfaces.begin (); plugin != m_interfaces.end (); plugin++)
383  {
384  plugin->second->ResetStats ();
385  }
386 }
387 
388 } // namespace flame
389 } // namespace ns3
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
Routing table for FLAME.
Definition: flame-rtable.h:36
Callback template class.
Definition: callback.h:984
FLAME routing protocol.
void SetSeqno(uint16_t seqno)
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
void Print(std::ostream &os) const
uint32_t GetSerializedSize() const
void SetOrigDst(Mac48Address dst)
bool RemoveRoutingStuff(uint32_t fromIface, const Mac48Address source, const Mac48Address destination, Ptr< Packet > packet, uint16_t &protocolType)
Cleanup flame headers!
void AddPath(const Mac48Address destination, const Mac48Address retransmitter, const uint32_t interface, const uint8_t cost, const uint16_t seqnum)
Add path.
Definition: flame-rtable.cc:64
void Print(std::ostream &os) const
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:836
#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
#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:766
#define NS_FATAL_ERROR(msg)
Fatal error handling.
Definition: fatal-error.h:100
TAG_BUFFER_INLINE uint8_t ReadU8(void)
Definition: tag-buffer.h:195
uint8_t m_maxCost
Max Cost value (or TTL, because cost is actually hopcount)
void SetProtocol(uint16_t protocol)
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:446
bool HandleDataFrame(uint16_t seqno, Mac48Address source, const FlameHeader flameHdr, Mac48Address receiver, uint32_t fromIface)
Handles a packet: adds a routing information and drops packets by TTL or Seqno.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:334
void CopyTo(uint8_t buffer[6]) const
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:858
Ptr< FlameRtable > m_rtable
Routing table:
uint8_t GetCost() const
Definition: flame-header.cc:98
Ptr< MeshPointDevice > m_mp
Host mesh point.
void Serialize(TagBuffer i) const
AttributeValue implementation for Time.
Definition: nstime.h:928
Hold an unsigned integer type.
Definition: uinteger.h:44
tuple interfaces
Definition: first.py:41
Hold together all Wifi-related objects.
static Mac48Address GetBroadcast(void)
uint16_t GetSeqno() const
void AddCost(uint8_t cost)
Definition: flame-header.cc:93
static Mac48Address ConvertFrom(const Address &address)
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
tag a set of bytes in a packet
Definition: tag.h:36
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Route lookup result, return type of LookupXXX methods.
Definition: flame-rtable.h:45
bool Install(Ptr< MeshPointDevice >)
Install FLAME on given mesh point.
void SetOrigSrc(Mac48Address OrigSrc)
bool RequestRoute(uint32_t sourceIface, const Mac48Address source, const Mac48Address destination, Ptr< const Packet > packet, uint16_t protocolType, RouteReplyCallback routeReply)
Route request, inherited from MeshL2RoutingProtocol.
void Deserialize(TagBuffer i)
an EUI-48 address
Definition: mac48-address.h:43
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition: tag-buffer.h:172
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: nstime.h:929
void CopyFrom(const uint8_t buffer[6])
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
Interface for L2 mesh routing protocol and mesh point communication.
void DoDispose()
Destructor implementation.
read and write tag data
Definition: tag-buffer.h:51
static const uint32_t INTERFACE_ANY
Means all interfaces.
Definition: flame-rtable.h:40
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:843
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:866
void Report(std::ostream &) const
Statistics.
TypeId GetInstanceTypeId() const
Get the most derived TypeId for this Object.
Mac48Address receiver
Receiver of the packet:
Ptr< T > CreateObject(void)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:524
static TypeId GetTypeId()
uint16_t m_myLastSeqno
Sequence number:
static const uint16_t FLAME_PROTOCOL
LLC protocol number reserved by flame.
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:57
TypeId SetParent(TypeId tid)
Definition: type-id.cc:638
LookupResult Lookup(Mac48Address destination)
Lookup path to destination.
Definition: flame-rtable.cc:87
Basic MAC of mesh point Wi-Fi interface.
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
Transmitter and receiver addresses.
Mac48Address transmitter
transmitter for incoming: