A Discrete-Event Network Simulator
API
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;
109  bool IsSrcDstValid (Ipv4Address src, Ipv4Address dst) const;
110 private:
111  uint32_t m_flowId;
112  uint32_t m_packetId;
113  uint32_t m_packetSize;
116 };
117 
118 TypeId
120 {
121  static TypeId tid = TypeId ("ns3::Ipv4FlowProbeTag")
122  .SetParent<Tag> ()
123  .SetGroupName ("FlowMonitor")
124  .AddConstructor<Ipv4FlowProbeTag> ()
125  ;
126  return tid;
127 }
128 TypeId
130 {
131  return GetTypeId ();
132 }
133 uint32_t
135 {
136  return 4 + 4 + 4 + 8;
137 }
138 void
140 {
141  buf.WriteU32 (m_flowId);
142  buf.WriteU32 (m_packetId);
143  buf.WriteU32 (m_packetSize);
144 
145  uint8_t tBuf[4];
146  m_src.Serialize (tBuf);
147  buf.Write (tBuf, 4);
148  m_dst.Serialize (tBuf);
149  buf.Write (tBuf, 4);
150 }
151 void
153 {
154  m_flowId = buf.ReadU32 ();
155  m_packetId = buf.ReadU32 ();
156  m_packetSize = buf.ReadU32 ();
157 
158  uint8_t tBuf[4];
159  buf.Read (tBuf, 4);
161  buf.Read (tBuf, 4);
163 }
164 void
165 Ipv4FlowProbeTag::Print (std::ostream &os) const
166 {
167  os << "FlowId=" << m_flowId;
168  os << " PacketId=" << m_packetId;
169  os << " PacketSize=" << m_packetSize;
170 }
172  : Tag ()
173 {
174 }
175 
176 Ipv4FlowProbeTag::Ipv4FlowProbeTag (uint32_t flowId, uint32_t packetId, uint32_t packetSize, Ipv4Address src, Ipv4Address dst)
177  : Tag (), m_flowId (flowId), m_packetId (packetId), m_packetSize (packetSize), m_src (src), m_dst (dst)
178 {
179 }
180 
181 void
183 {
184  m_flowId = id;
185 }
186 void
188 {
189  m_packetId = id;
190 }
191 void
193 {
194  m_packetSize = size;
195 }
196 uint32_t
198 {
199  return m_flowId;
200 }
201 uint32_t
203 {
204  return m_packetId;
205 }
206 uint32_t
208 {
209  return m_packetSize;
210 }
211 bool
213 {
214  return ((m_src == src) && (m_dst == dst));
215 }
216 
218 // Ipv4FlowProbe class implementation //
220 
222  Ptr<Ipv4FlowClassifier> classifier,
223  Ptr<Node> node)
224  : FlowProbe (monitor),
225  m_classifier (classifier)
226 {
227  NS_LOG_FUNCTION (this << node->GetId ());
228 
229  m_ipv4 = node->GetObject<Ipv4L3Protocol> ();
230 
231  if (!m_ipv4->TraceConnectWithoutContext ("SendOutgoing",
233  {
234  NS_FATAL_ERROR ("trace fail");
235  }
236  if (!m_ipv4->TraceConnectWithoutContext ("UnicastForward",
238  {
239  NS_FATAL_ERROR ("trace fail");
240  }
241  if (!m_ipv4->TraceConnectWithoutContext ("LocalDeliver",
243  {
244  NS_FATAL_ERROR ("trace fail");
245  }
246 
247  if (!m_ipv4->TraceConnectWithoutContext ("Drop",
249  {
250  NS_FATAL_ERROR ("trace fail");
251  }
252 
253  // code copied from point-to-point-helper.cc
254  std::ostringstream oss;
255  oss << "/NodeList/" << node->GetId () << "/DeviceList/*/TxQueue/Drop";
257 }
258 
260 {
261 }
262 
263 /* static */
264 TypeId
266 {
267  static TypeId tid = TypeId ("ns3::Ipv4FlowProbe")
268  .SetParent<FlowProbe> ()
269  .SetGroupName ("FlowMonitor")
270  // No AddConstructor because this class has no default constructor.
271  ;
272 
273  return tid;
274 }
275 
276 void
278 {
279  m_ipv4 = 0;
280  m_classifier = 0;
282 }
283 
284 void
285 Ipv4FlowProbe::SendOutgoingLogger (const Ipv4Header &ipHeader, Ptr<const Packet> ipPayload, uint32_t interface)
286 {
287  FlowId flowId;
288  FlowPacketId packetId;
289 
290  if (!m_ipv4->IsUnicast(ipHeader.GetDestination ()))
291  {
292  // we are not prepared to handle broadcast yet
293  return;
294  }
295 
296  Ipv4FlowProbeTag fTag;
297  bool found = ipPayload->FindFirstMatchingByteTag (fTag);
298  if (found)
299  {
300  return;
301  }
302 
303  if (m_classifier->Classify (ipHeader, ipPayload, &flowId, &packetId))
304  {
305  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
306  NS_LOG_DEBUG ("ReportFirstTx ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<"); "
307  << ipHeader << *ipPayload);
308  m_flowMonitor->ReportFirstTx (this, flowId, packetId, size);
309 
310  // tag the packet with the flow id and packet id, so that the packet can be identified even
311  // when Ipv4Header is not accessible at some non-IPv4 protocol layer
312  Ipv4FlowProbeTag fTag (flowId, packetId, size, ipHeader.GetSource (), ipHeader.GetDestination ());
313  ipPayload->AddByteTag (fTag);
314  }
315 }
316 
317 void
318 Ipv4FlowProbe::ForwardLogger (const Ipv4Header &ipHeader, Ptr<const Packet> ipPayload, uint32_t interface)
319 {
320  Ipv4FlowProbeTag fTag;
321  bool found = ipPayload->FindFirstMatchingByteTag (fTag);
322 
323  if (found)
324  {
325  if (!ipHeader.IsLastFragment () || ipHeader.GetFragmentOffset () != 0)
326  {
327  NS_LOG_WARN ("Not counting fragmented packets");
328  return;
329  }
330  if (!fTag.IsSrcDstValid (ipHeader.GetSource (), ipHeader.GetDestination ()))
331  {
332  NS_LOG_LOGIC ("Not reporting encapsulated packet");
333  return;
334  }
335 
336  FlowId flowId = fTag.GetFlowId ();
337  FlowPacketId packetId = fTag.GetPacketId ();
338 
339  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
340  NS_LOG_DEBUG ("ReportForwarding ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<");");
341  m_flowMonitor->ReportForwarding (this, flowId, packetId, size);
342  }
343 }
344 
345 void
346 Ipv4FlowProbe::ForwardUpLogger (const Ipv4Header &ipHeader, Ptr<const Packet> ipPayload, uint32_t interface)
347 {
348  Ipv4FlowProbeTag fTag;
349  bool found = ipPayload->FindFirstMatchingByteTag (fTag);
350 
351  if (found)
352  {
353  if (!fTag.IsSrcDstValid (ipHeader.GetSource (), ipHeader.GetDestination ()))
354  {
355  NS_LOG_LOGIC ("Not reporting encapsulated packet");
356  return;
357  }
358 
359  FlowId flowId = fTag.GetFlowId ();
360  FlowPacketId packetId = fTag.GetPacketId ();
361 
362  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
363  NS_LOG_DEBUG ("ReportLastRx ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<"); "
364  << ipHeader << *ipPayload);
365  m_flowMonitor->ReportLastRx (this, flowId, packetId, size);
366  }
367 }
368 
369 void
371  Ipv4L3Protocol::DropReason reason, Ptr<Ipv4> ipv4, uint32_t ifIndex)
372 {
373 #if 0
374  switch (reason)
375  {
377  break;
378 
381  Ipv4Address addri = m_ipv4->GetAddress (ifIndex);
382  Ipv4Mask maski = m_ipv4->GetNetworkMask (ifIndex);
383  Ipv4Address bcast = addri.GetSubnetDirectedBroadcast (maski);
384  if (ipHeader.GetDestination () == bcast) // we don't want broadcast packets
385  {
386  return;
387  }
388  }
389 #endif
390 
391  Ipv4FlowProbeTag fTag;
392  bool found = ipPayload->FindFirstMatchingByteTag (fTag);
393 
394  if (found)
395  {
396  FlowId flowId = fTag.GetFlowId ();
397  FlowPacketId packetId = fTag.GetPacketId ();
398 
399  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
400  NS_LOG_DEBUG ("Drop ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<", " << reason
401  << ", destIp=" << ipHeader.GetDestination () << "); "
402  << "HDR: " << ipHeader << " PKT: " << *ipPayload);
403 
404  DropReason myReason;
405 
406 
407  switch (reason)
408  {
410  myReason = DROP_TTL_EXPIRE;
411  NS_LOG_DEBUG ("DROP_TTL_EXPIRE");
412  break;
414  myReason = DROP_NO_ROUTE;
415  NS_LOG_DEBUG ("DROP_NO_ROUTE");
416  break;
418  myReason = DROP_BAD_CHECKSUM;
419  NS_LOG_DEBUG ("DROP_BAD_CHECKSUM");
420  break;
422  myReason = DROP_INTERFACE_DOWN;
423  NS_LOG_DEBUG ("DROP_INTERFACE_DOWN");
424  break;
426  myReason = DROP_ROUTE_ERROR;
427  NS_LOG_DEBUG ("DROP_ROUTE_ERROR");
428  break;
430  myReason = DROP_FRAGMENT_TIMEOUT;
431  NS_LOG_DEBUG ("DROP_FRAGMENT_TIMEOUT");
432  break;
433 
434  default:
435  myReason = DROP_INVALID_REASON;
436  NS_FATAL_ERROR ("Unexpected drop reason code " << reason);
437  }
438 
439  m_flowMonitor->ReportDrop (this, flowId, packetId, size, myReason);
440  }
441 }
442 
443 void
445 {
446  Ipv4FlowProbeTag fTag;
447  bool tagFound = ipPayload->FindFirstMatchingByteTag (fTag);
448 
449  if (!tagFound)
450  {
451  return;
452  }
453 
454  FlowId flowId = fTag.GetFlowId ();
455  FlowPacketId packetId = fTag.GetPacketId ();
456  uint32_t size = fTag.GetPacketSize ();
457 
458  NS_LOG_DEBUG ("Drop ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<", " << DROP_QUEUE
459  << "); ");
460 
461  m_flowMonitor->ReportDrop (this, flowId, packetId, size, DROP_QUEUE);
462 }
463 
464 } // namespace ns3
465 
466 
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
Interface is down so can not send packet.
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.
DropReason
enumeration of possible reasons why a packet may be dropped
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:234
Packet dropped due to TTL decremented to zero during IPv4 forwarding.
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
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:786
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:145
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.
Packet dropped due to queue overflow.
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:1480
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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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
uint32_t GetPacketSize(void) const
Get the packet size.
Fallback reason (no known reason)
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
Packet dropped due to missing route to the destination.
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
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.
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:826
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.