A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("FlameProtocol");
35 
36 namespace ns3 {
37 namespace flame {
38 //-----------------------------------------------------------------------------
39 // FlameTag
40 //-----------------------------------------------------------------------------
42 NS_OBJECT_ENSURE_REGISTERED (FlameProtocol);
43 
44 TypeId
46 {
47  static TypeId tid = TypeId ("ns3::flame::FlameTag").SetParent<Tag> ().AddConstructor<FlameTag> ();
48  return tid;
49 }
50 
51 TypeId
53 {
54  return GetTypeId ();
55 }
56 
57 uint32_t
59 {
60  return 12;
61 }
62 
63 void
65 {
66  uint8_t buf[6];
67  receiver.CopyTo (buf);
68  for (int j = 0; j < 6; j++)
69  {
70  i.WriteU8 (buf[j]);
71  }
72  transmitter.CopyTo (buf);
73  for (int j = 0; j < 6; j++)
74  {
75  i.WriteU8 (buf[j]);
76  }
77 
78 }
79 
80 void
82 {
83  uint8_t buf[6];
84  for (int j = 0; j < 6; j++)
85  {
86  buf[j] = i.ReadU8 ();
87  }
88  receiver.CopyFrom (buf);
89  for (int j = 0; j < 6; j++)
90  {
91  buf[j] = i.ReadU8 ();
92  }
93  transmitter.CopyFrom (buf);
94 
95 }
96 
97 void
98 FlameTag::Print (std::ostream &os) const
99 {
100  os << "receiver = " << receiver << ", transmitter = " << transmitter;
101 }
102 
103 //-----------------------------------------------------------------------------
104 // FlameProtocol
105 //-----------------------------------------------------------------------------
106 TypeId
108 {
109  static TypeId tid = TypeId ("ns3::flame::FlameProtocol")
111  .AddConstructor<FlameProtocol> ()
112  .AddAttribute ( "BroadcastInterval",
113  "How often we must send broadcast packets",
114  TimeValue (Seconds (5)),
115  MakeTimeAccessor (
117  MakeTimeChecker ()
118  )
119  .AddAttribute ( "MaxCost",
120  "Cost threshold after which packet will be dropped",
121  UintegerValue (32),
122  MakeUintegerAccessor (
124  MakeUintegerChecker<uint8_t> (3)
125  )
126  ;
127  return tid;
128 }
130  m_address (Mac48Address ()), m_broadcastInterval (Seconds (5)), m_lastBroadcast (Seconds (0)),
131  m_maxCost (32), m_myLastSeqno (1), m_rtable (CreateObject<FlameRtable> ())
132 {
133 }
135 {
136 }
137 void
139 {
140  m_interfaces.clear ();
141  m_rtable = 0;
142  m_mp = 0;
143 }
144 bool
145 FlameProtocol::RequestRoute (uint32_t sourceIface, const Mac48Address source, const Mac48Address destination,
146  Ptr<const Packet> const_packet, uint16_t protocolType, RouteReplyCallback routeReply)
147 {
148  Ptr<Packet> packet = const_packet->Copy ();
149  if (sourceIface == m_mp->GetIfIndex ())
150  {
151  //Packet from upper layer!
152  FlameTag tag;
153  if (packet->PeekPacketTag (tag))
154  {
155  NS_FATAL_ERROR ("FLAME tag is not supposed to be received from upper layers");
156  }
157  FlameRtable::LookupResult result = m_rtable->Lookup (destination);
158  if (result.retransmitter == Mac48Address::GetBroadcast ())
159  {
161  }
163  {
167  }
168  FlameHeader flameHdr;
169  flameHdr.AddCost (0);
170  flameHdr.SetSeqno (m_myLastSeqno++);
171  flameHdr.SetProtocol (protocolType);
172  flameHdr.SetOrigDst (destination);
173  flameHdr.SetOrigSrc (source);
174  m_stats.txBytes += packet->GetSize ();
175  packet->AddHeader (flameHdr);
176  tag.receiver = result.retransmitter;
177  if (result.retransmitter == Mac48Address::GetBroadcast ())
178  {
180  }
181  else
182  {
183  m_stats.txUnicast++;
184  }
185  NS_LOG_DEBUG ("Source: send packet with RA = " << tag.receiver);
186  packet->AddPacketTag (tag);
187  routeReply (true, packet, source, destination, FLAME_PROTOCOL, result.ifIndex);
188  }
189  else
190  {
191  FlameHeader flameHdr;
192  packet->RemoveHeader (flameHdr);
193  FlameTag tag;
194 
195  if (!packet->RemovePacketTag (tag))
196  {
197  NS_FATAL_ERROR ("FLAME tag must exist here");
198  }
199  if (destination == Mac48Address::GetBroadcast ())
200  {
201  //Broadcast always is forwarded as broadcast!
202  NS_ASSERT (HandleDataFrame (flameHdr.GetSeqno (), source, flameHdr, tag.transmitter, sourceIface));
204  flameHdr.AddCost (1);
205  m_stats.txBytes += packet->GetSize ();
206  packet->AddHeader (flameHdr);
207  packet->AddPacketTag (tag);
208  routeReply (true, packet, source, destination, FLAME_PROTOCOL, FlameRtable::INTERFACE_ANY);
210  return true;
211  }
212  else
213  {
214  // We check sequence only when forward unicast, because broadcast-checks were done
215  // inside remove routing stuff.
216  if (HandleDataFrame (flameHdr.GetSeqno (), source, flameHdr, tag.transmitter, sourceIface))
217  {
218  return false;
219  }
220  FlameRtable::LookupResult result = m_rtable->Lookup (destination);
221  if (tag.receiver != Mac48Address::GetBroadcast ())
222  {
223  if (result.retransmitter == Mac48Address::GetBroadcast ())
224  {
225  NS_LOG_DEBUG ("unicast packet dropped, because no route! I am " << GetAddress ()
226  << ", RA = " << tag.receiver << ", TA = " << tag.transmitter);
228  return false;
229  }
230  tag.receiver = result.retransmitter;
231  }
232  else
233  {
235  }
236  if (result.retransmitter == Mac48Address::GetBroadcast ())
237  {
239  }
240  else
241  {
242  m_stats.txUnicast++;
243  }
244  m_stats.txBytes += packet->GetSize ();
245  flameHdr.AddCost (1);
246  packet->AddHeader (flameHdr);
247  packet->AddPacketTag (tag);
248  routeReply (true, packet, source, destination, FLAME_PROTOCOL, result.ifIndex);
249  return true;
250  }
251  return true;
252  }
253  return false;
254 }
255 bool
256 FlameProtocol::RemoveRoutingStuff (uint32_t fromIface, const Mac48Address source,
257  const Mac48Address destination, Ptr<Packet> packet, uint16_t& protocolType)
258 {
259  //Filter seqno:
260  if (source == GetAddress ())
261  {
262  NS_LOG_DEBUG ("Dropped my own frame!");
263  return false;
264  }
265  FlameTag tag;
266  if (!packet->RemovePacketTag (tag))
267  {
268  NS_FATAL_ERROR ("FLAME tag must exist when packet is coming to protocol");
269  }
270  FlameHeader flameHdr;
271  packet->RemoveHeader (flameHdr);
272  if (HandleDataFrame (flameHdr.GetSeqno (), source, flameHdr, tag.transmitter, fromIface))
273  {
274  return false;
275  }
276  // Start PATH_UPDATE procedure if destination is our own address and last broadcast was sent more
277  // than broadcast interval ago or was not sent at all
278  if ((destination == GetAddress ()) && ((m_lastBroadcast + m_broadcastInterval < Simulator::Now ())
279  || (m_lastBroadcast == Seconds (0))))
280  {
281  Ptr<Packet> packet = Create<Packet> ();
282  m_mp->Send (packet, Mac48Address::GetBroadcast (), 0);
284  }
285  NS_ASSERT (protocolType == FLAME_PROTOCOL);
286  protocolType = flameHdr.GetProtocol ();
287  return true;
288 }
289 bool
291 {
292  m_mp = mp;
293  std::vector<Ptr<NetDevice> > interfaces = mp->GetInterfaces ();
294  for (std::vector<Ptr<NetDevice> >::const_iterator i = interfaces.begin (); i != interfaces.end (); i++)
295  {
296  // Checking for compatible net device
297  Ptr<WifiNetDevice> wifiNetDev = (*i)->GetObject<WifiNetDevice> ();
298  if (wifiNetDev == 0)
299  {
300  return false;
301  }
302  Ptr<MeshWifiInterfaceMac> mac = wifiNetDev->GetMac ()->GetObject<MeshWifiInterfaceMac> ();
303  if (mac == 0)
304  {
305  return false;
306  }
307  // Installing plugins:
308  Ptr<FlameProtocolMac> flameMac = Create<FlameProtocolMac> (this);
309  m_interfaces[wifiNetDev->GetIfIndex ()] = flameMac;
310  mac->SetBeaconGeneration (false);
311  mac->InstallPlugin (flameMac);
312  }
313  mp->SetRoutingProtocol (this);
314  // Mesh point aggregates all installed protocols
315  mp->AggregateObject (this);
316  m_address = Mac48Address::ConvertFrom (mp->GetAddress ()); //* address;
317  return true;
318 }
321 {
322  return m_address;
323 }
324 bool
325 FlameProtocol::HandleDataFrame (uint16_t seqno, Mac48Address source, const FlameHeader flameHdr,
326  Mac48Address receiver, uint32_t fromInterface)
327 {
328  if (source == GetAddress ())
329  {
331  return true;
332  }
333  FlameRtable::LookupResult result = m_rtable->Lookup (source);
334  if ((result.retransmitter != Mac48Address::GetBroadcast ()) && ((int16_t)(result.seqnum - seqno) >= 0))
335  {
336  return true;
337  }
338  if (flameHdr.GetCost () > m_maxCost)
339  {
341  return true;
342  }
343  m_rtable->AddPath (source, receiver, fromInterface, flameHdr.GetCost (), flameHdr.GetSeqno ());
344  return false;
345 }
346 //Statistics:
348  txUnicast (0), txBroadcast (0), txBytes (0), droppedTtl (0), totalDropped (0)
349 {
350 }
351 void
352 FlameProtocol::Statistics::Print (std::ostream & os) const
353 {
354  os << "<Statistics "
355  "txUnicast=\"" << txUnicast << "\" "
356  "txBroadcast=\"" << txBroadcast << "\" "
357  "txBytes=\"" << txBytes << "\" "
358  "droppedTtl=\"" << droppedTtl << "\" "
359  "totalDropped=\"" << totalDropped << "\"/>" << std::endl;
360 }
361 void
362 FlameProtocol::Report (std::ostream & os) const
363 {
364  os << "<Flame "
365  "address=\"" << m_address << "\"" << std::endl <<
366  "broadcastInterval=\"" << m_broadcastInterval.GetSeconds () << "\"" << std::endl <<
367  "maxCost=\"" << (uint16_t) m_maxCost << "\">" << std::endl;
368  m_stats.Print (os);
369  for (FlamePluginMap::const_iterator plugin = m_interfaces.begin (); plugin != m_interfaces.end (); plugin++)
370  {
371  plugin->second->Report (os);
372  }
373  os << "</Flame>" << std::endl;
374 }
375 void
377 {
378  m_stats = Statistics ();
379  for (FlamePluginMap::const_iterator plugin = m_interfaces.begin (); plugin != m_interfaces.end (); plugin++)
380  {
381  plugin->second->ResetStats ();
382  }
383 }
384 
385 } // namespace flame
386 } // 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:924
void SetSeqno(uint16_t seqno)
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
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:61
void Print(std::ostream &os) const
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:841
#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
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:95
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< T > CreateObject(void)
Definition: object.h:421
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
Definition: nstime.h:272
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:863
Ptr< FlameRtable > m_rtable
Routing table:
uint8_t GetCost() const
Definition: flame-header.cc:97
Ptr< MeshPointDevice > m_mp
Host mesh point.
void Serialize(TagBuffer i) const
hold objects of type ns3::Time
Definition: nstime.h:1008
Hold an unsigned integer type.
Definition: uinteger.h:46
tuple interfaces
Definition: first.py:40
Hold together all Wifi-related objects.
static Mac48Address GetBroadcast(void)
uint16_t GetSeqno() const
void AddCost(uint8_t cost)
Definition: flame-header.cc:92
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
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:41
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition: tag-buffer.h:172
void CopyFrom(const uint8_t buffer[6])
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
Interface for L2 mesh routing protocol and mesh point communication.
void DoDispose()
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
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:848
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:213
void Report(std::ostream &) const
Statistics.
TypeId GetInstanceTypeId() const
Mac48Address receiver
Receiver of the packet:
static TypeId GetTypeId()
uint16_t m_myLastSeqno
Sequence number:
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:441
static const uint16_t FLAME_PROTOCOL
LLC protocol number reserved by flame.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
LookupResult Lookup(Mac48Address destination)
Lookup path to destination.
Definition: flame-rtable.cc:84
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: