A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ipv4-flow-probe.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 //
3 // Copyright (c) 2009 INESC Porto
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: Gustavo J. A. M. Carneiro <gjc@inescporto.pt> <gjcarneiro@gmail.com>
19 //
20 
21 #include "ns3/ipv4-flow-probe.h"
22 #include "ns3/ipv4-flow-classifier.h"
23 #include "ns3/node.h"
24 #include "ns3/packet.h"
25 #include "ns3/flow-monitor.h"
26 #include "ns3/log.h"
27 #include "ns3/pointer.h"
28 #include "ns3/config.h"
29 #include "ns3/flow-id-tag.h"
30 
31 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("Ipv4FlowProbe");
34 
36 // Ipv4FlowProbeTag class implementation //
38 
48 class Ipv4FlowProbeTag : public Tag
49 {
50 public:
55  static TypeId GetTypeId (void);
56  virtual TypeId GetInstanceTypeId (void) const;
57  virtual uint32_t GetSerializedSize (void) const;
58  virtual void Serialize (TagBuffer buf) const;
59  virtual void Deserialize (TagBuffer buf);
60  virtual void Print (std::ostream &os) const;
70  Ipv4FlowProbeTag (uint32_t flowId, uint32_t packetId, uint32_t packetSize, Ipv4Address src, Ipv4Address dst);
75  void SetFlowId (uint32_t flowId);
80  void SetPacketId (uint32_t packetId);
85  void SetPacketSize (uint32_t packetSize);
90  uint32_t GetFlowId (void) const;
95  uint32_t GetPacketId (void) const;
100  uint32_t GetPacketSize (void) const;
111  bool IsSrcDstValid (Ipv4Address src, Ipv4Address dst) const;
112 private:
113  uint32_t m_flowId;
114  uint32_t m_packetId;
115  uint32_t m_packetSize;
118 };
119 
120 TypeId
122 {
123  static TypeId tid = TypeId ("ns3::Ipv4FlowProbeTag")
124  .SetParent<Tag> ()
125  .SetGroupName ("FlowMonitor")
126  .AddConstructor<Ipv4FlowProbeTag> ()
127  ;
128  return tid;
129 }
130 TypeId
132 {
133  return GetTypeId ();
134 }
135 uint32_t
137 {
138  return 4 + 4 + 4 + 8;
139 }
140 void
142 {
143  buf.WriteU32 (m_flowId);
144  buf.WriteU32 (m_packetId);
145  buf.WriteU32 (m_packetSize);
146 
147  uint8_t tBuf[4];
148  m_src.Serialize (tBuf);
149  buf.Write (tBuf, 4);
150  m_dst.Serialize (tBuf);
151  buf.Write (tBuf, 4);
152 }
153 void
155 {
156  m_flowId = buf.ReadU32 ();
157  m_packetId = buf.ReadU32 ();
158  m_packetSize = buf.ReadU32 ();
159 
160  uint8_t tBuf[4];
161  buf.Read (tBuf, 4);
163  buf.Read (tBuf, 4);
165 }
166 void
167 Ipv4FlowProbeTag::Print (std::ostream &os) const
168 {
169  os << "FlowId=" << m_flowId;
170  os << " PacketId=" << m_packetId;
171  os << " PacketSize=" << m_packetSize;
172 }
174  : Tag ()
175 {
176 }
177 
178 Ipv4FlowProbeTag::Ipv4FlowProbeTag (uint32_t flowId, uint32_t packetId, uint32_t packetSize, Ipv4Address src, Ipv4Address dst)
179  : Tag (), m_flowId (flowId), m_packetId (packetId), m_packetSize (packetSize), m_src (src), m_dst (dst)
180 {
181 }
182 
183 void
185 {
186  m_flowId = id;
187 }
188 void
190 {
191  m_packetId = id;
192 }
193 void
195 {
196  m_packetSize = size;
197 }
198 uint32_t
200 {
201  return m_flowId;
202 }
203 uint32_t
205 {
206  return m_packetId;
207 }
208 uint32_t
210 {
211  return m_packetSize;
212 }
213 bool
215 {
216  return ((m_src == src) && (m_dst == dst));
217 }
218 
220 // Ipv4FlowProbe class implementation //
222 
224  Ptr<Ipv4FlowClassifier> classifier,
225  Ptr<Node> node)
226  : FlowProbe (monitor),
227  m_classifier (classifier)
228 {
229  NS_LOG_FUNCTION (this << node->GetId ());
230 
231  m_ipv4 = node->GetObject<Ipv4L3Protocol> ();
232 
233  if (!m_ipv4->TraceConnectWithoutContext ("SendOutgoing",
235  {
236  NS_FATAL_ERROR ("trace fail");
237  }
238  if (!m_ipv4->TraceConnectWithoutContext ("UnicastForward",
240  {
241  NS_FATAL_ERROR ("trace fail");
242  }
243  if (!m_ipv4->TraceConnectWithoutContext ("LocalDeliver",
245  {
246  NS_FATAL_ERROR ("trace fail");
247  }
248 
249  if (!m_ipv4->TraceConnectWithoutContext ("Drop",
251  {
252  NS_FATAL_ERROR ("trace fail");
253  }
254 
255  // code copied from point-to-point-helper.cc
256  std::ostringstream oss;
257  oss << "/NodeList/" << node->GetId () << "/DeviceList/*/TxQueue/Drop";
259 }
260 
262 {
263 }
264 
265 /* static */
266 TypeId
268 {
269  static TypeId tid = TypeId ("ns3::Ipv4FlowProbe")
270  .SetParent<FlowProbe> ()
271  .SetGroupName ("FlowMonitor")
272  // No AddConstructor because this class has no default constructor.
273  ;
274 
275  return tid;
276 }
277 
278 void
280 {
281  m_ipv4 = 0;
282  m_classifier = 0;
284 }
285 
286 void
287 Ipv4FlowProbe::SendOutgoingLogger (const Ipv4Header &ipHeader, Ptr<const Packet> ipPayload, uint32_t interface)
288 {
289  FlowId flowId;
290  FlowPacketId packetId;
291 
292  if (!m_ipv4->IsUnicast(ipHeader.GetDestination ()))
293  {
294  // we are not prepared to handle broadcast yet
295  return;
296  }
297 
298  Ipv4FlowProbeTag fTag;
299  bool found = ipPayload->FindFirstMatchingByteTag (fTag);
300  if (found)
301  {
302  return;
303  }
304 
305  if (m_classifier->Classify (ipHeader, ipPayload, &flowId, &packetId))
306  {
307  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
308  NS_LOG_DEBUG ("ReportFirstTx ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<"); "
309  << ipHeader << *ipPayload);
310  m_flowMonitor->ReportFirstTx (this, flowId, packetId, size);
311 
312  // tag the packet with the flow id and packet id, so that the packet can be identified even
313  // when Ipv4Header is not accessible at some non-IPv4 protocol layer
314  Ipv4FlowProbeTag fTag (flowId, packetId, size, ipHeader.GetSource (), ipHeader.GetDestination ());
315  ipPayload->AddByteTag (fTag);
316  }
317 }
318 
319 void
320 Ipv4FlowProbe::ForwardLogger (const Ipv4Header &ipHeader, Ptr<const Packet> ipPayload, uint32_t interface)
321 {
322  Ipv4FlowProbeTag fTag;
323  bool found = ipPayload->FindFirstMatchingByteTag (fTag);
324 
325  if (found)
326  {
327  if (!ipHeader.IsLastFragment () || ipHeader.GetFragmentOffset () != 0)
328  {
329  NS_LOG_WARN ("Not counting fragmented packets");
330  return;
331  }
332  if (!fTag.IsSrcDstValid (ipHeader.GetSource (), ipHeader.GetDestination ()))
333  {
334  NS_LOG_LOGIC ("Not reporting encapsulated packet");
335  return;
336  }
337 
338  FlowId flowId = fTag.GetFlowId ();
339  FlowPacketId packetId = fTag.GetPacketId ();
340 
341  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
342  NS_LOG_DEBUG ("ReportForwarding ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<");");
343  m_flowMonitor->ReportForwarding (this, flowId, packetId, size);
344  }
345 }
346 
347 void
348 Ipv4FlowProbe::ForwardUpLogger (const Ipv4Header &ipHeader, Ptr<const Packet> ipPayload, uint32_t interface)
349 {
350  Ipv4FlowProbeTag fTag;
351  bool found = ipPayload->FindFirstMatchingByteTag (fTag);
352 
353  if (found)
354  {
355  if (!fTag.IsSrcDstValid (ipHeader.GetSource (), ipHeader.GetDestination ()))
356  {
357  NS_LOG_LOGIC ("Not reporting encapsulated packet");
358  return;
359  }
360 
361  FlowId flowId = fTag.GetFlowId ();
362  FlowPacketId packetId = fTag.GetPacketId ();
363 
364  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
365  NS_LOG_DEBUG ("ReportLastRx ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<"); "
366  << ipHeader << *ipPayload);
367  m_flowMonitor->ReportLastRx (this, flowId, packetId, size);
368  }
369 }
370 
371 void
373  Ipv4L3Protocol::DropReason reason, Ptr<Ipv4> ipv4, uint32_t ifIndex)
374 {
375 #if 0
376  switch (reason)
377  {
379  break;
380 
383  Ipv4Address addri = m_ipv4->GetAddress (ifIndex);
384  Ipv4Mask maski = m_ipv4->GetNetworkMask (ifIndex);
385  Ipv4Address bcast = addri.GetSubnetDirectedBroadcast (maski);
386  if (ipHeader.GetDestination () == bcast) // we don't want broadcast packets
387  {
388  return;
389  }
390  }
391 #endif
392 
393  Ipv4FlowProbeTag fTag;
394  bool found = ipPayload->FindFirstMatchingByteTag (fTag);
395 
396  if (found)
397  {
398  FlowId flowId = fTag.GetFlowId ();
399  FlowPacketId packetId = fTag.GetPacketId ();
400 
401  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
402  NS_LOG_DEBUG ("Drop ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<", " << reason
403  << ", destIp=" << ipHeader.GetDestination () << "); "
404  << "HDR: " << ipHeader << " PKT: " << *ipPayload);
405 
406  DropReason myReason;
407 
408 
409  switch (reason)
410  {
412  myReason = DROP_TTL_EXPIRE;
413  NS_LOG_DEBUG ("DROP_TTL_EXPIRE");
414  break;
416  myReason = DROP_NO_ROUTE;
417  NS_LOG_DEBUG ("DROP_NO_ROUTE");
418  break;
420  myReason = DROP_BAD_CHECKSUM;
421  NS_LOG_DEBUG ("DROP_BAD_CHECKSUM");
422  break;
424  myReason = DROP_INTERFACE_DOWN;
425  NS_LOG_DEBUG ("DROP_INTERFACE_DOWN");
426  break;
428  myReason = DROP_ROUTE_ERROR;
429  NS_LOG_DEBUG ("DROP_ROUTE_ERROR");
430  break;
432  myReason = DROP_FRAGMENT_TIMEOUT;
433  NS_LOG_DEBUG ("DROP_FRAGMENT_TIMEOUT");
434  break;
435 
436  default:
437  myReason = DROP_INVALID_REASON;
438  NS_FATAL_ERROR ("Unexpected drop reason code " << reason);
439  }
440 
441  m_flowMonitor->ReportDrop (this, flowId, packetId, size, myReason);
442  }
443 }
444 
445 void
447 {
448  Ipv4FlowProbeTag fTag;
449  bool tagFound = ipPayload->FindFirstMatchingByteTag (fTag);
450 
451  if (!tagFound)
452  {
453  return;
454  }
455 
456  FlowId flowId = fTag.GetFlowId ();
457  FlowPacketId packetId = fTag.GetPacketId ();
458  uint32_t size = fTag.GetPacketSize ();
459 
460  NS_LOG_DEBUG ("Drop ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<", " << DROP_QUEUE
461  << "); ");
462 
463  m_flowMonitor->ReportDrop (this, flowId, packetId, size, DROP_QUEUE);
464 }
465 
466 } // namespace ns3
467 
468 
uint32_t FlowPacketId
Abstract identifier of a packet within a flow.
Tag used to allow a fast identification of the packet.
static Ipv4Address Deserialize(const uint8_t buf[4])
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:807
void ForwardUpLogger(const Ipv4Header &ipHeader, Ptr< const Packet > ipPayload, uint32_t interface)
Log a packet being received by the destination.
void Write(const uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:125
uint32_t m_flowId
flow identifier
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 void DoDispose(void)
Destructor implementation.
void SetPacketSize(uint32_t packetSize)
Set the packet size.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
Ipv4FlowProbe(Ptr< FlowMonitor > monitor, Ptr< Ipv4FlowClassifier > classifier, Ptr< Node > node)
Constructor.
Ipv4Address m_src
IP source.
Ipv4Address GetDestination(void) const
Definition: ipv4-header.cc:304
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Fallback reason (no known reason)
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
Ipv4Address GetSource(void) const
Definition: ipv4-header.cc:291
TAG_BUFFER_INLINE uint32_t ReadU32(void)
Definition: tag-buffer.h:215
void SendOutgoingLogger(const Ipv4Header &ipHeader, Ptr< const Packet > ipPayload, uint32_t interface)
Log a packet being sent.
Ipv4Address m_dst
IP destination.
Ptr< FlowMonitor > m_flowMonitor
the FlowMonitor instance
Definition: flow-probe.h:106
Ipv4Address GetSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
virtual uint32_t GetSerializedSize(void) const
DropReason
Reason why a packet has been dropped.
void ForwardLogger(const Ipv4Header &ipHeader, Ptr< const Packet > ipPayload, uint32_t interface)
Log a packet being forwarded.
Packet header for IPv4.
Definition: ipv4-header.h:31
void SetFlowId(uint32_t flowId)
Set the flow identifier.
uint32_t m_packetSize
packet size
virtual void Serialize(TagBuffer buf) const
void QueueDropLogger(Ptr< const Packet > ipPayload)
Log a packet being dropped by a queue.
TAG_BUFFER_INLINE void WriteU32(uint32_t v)
Definition: tag-buffer.h:186
void SetPacketId(uint32_t packetId)
Set the packet identifier.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:824
void Serialize(uint8_t buf[4]) const
Serialize this address to a 4-byte buffer.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
uint32_t GetPacketId(void) const
Set the packet identifier.
Packet dropped due to invalid checksum in the IPv4 header.
tag a set of bytes in a packet
Definition: tag.h:36
uint32_t m_packetId
packet identifier
Implement the Ipv4 layer.
virtual void Print(std::ostream &os) const
bool IsSrcDstValid(Ipv4Address src, Ipv4Address dst) const
Checks if the addresses stored in tag are matching the arguments.
bool IsLastFragment(void) const
Definition: ipv4-header.cc:212
Ptr< Ipv4FlowClassifier > m_classifier
the Ipv4FlowClassifier this probe is associated with
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
virtual void Deserialize(TagBuffer buf)
uint32_t GetFlowId(void) const
Set the flow identifier.
The FlowProbe class is responsible for listening for packet events in a specific point of the simulat...
Definition: flow-probe.h:39
Packet dropped due to TTL decremented to zero during IPv4 forwarding.
uint32_t GetPacketSize(void) const
Get the packet size.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
read and write tag data
Definition: tag-buffer.h:51
uint32_t GetId(void) const
Definition: node.cc:107
uint16_t GetFragmentOffset(void) const
Definition: ipv4-header.cc:246
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
Packet dropped due to queue overflow.
Interface is down so can not send packet.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
Packet dropped due to missing route to the destination.
void Read(uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:176
static TypeId GetTypeId(void)
Register this type.
Interface is down so can not send packet.
DropReason
enumeration of possible reasons why a packet may be dropped
static const uint32_t packetSize
virtual uint32_t GetSerializedSize(void) const
Definition: ipv4-header.cc:375
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:827
Ptr< Ipv4L3Protocol > m_ipv4
the Ipv4L3Protocol this probe is bound to
virtual void DoDispose(void)
Destructor implementation.
Definition: flow-probe.cc:50
static TypeId GetTypeId(void)
Get the type ID.
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition: packet.cc:791
uint32_t FlowId
Abstract identifier of a packet flow.
void DropLogger(const Ipv4Header &ipHeader, Ptr< const Packet > ipPayload, Ipv4L3Protocol::DropReason reason, Ptr< Ipv4 > ipv4, uint32_t ifIndex)
Log a packet being dropped.