This documentation is not the Latest Release.
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;
68  Ipv4FlowProbeTag (uint32_t flowId, uint32_t packetId, uint32_t packetSize);
73  void SetFlowId (uint32_t flowId);
78  void SetPacketId (uint32_t packetId);
83  void SetPacketSize (uint32_t packetSize);
88  uint32_t GetFlowId (void) const;
93  uint32_t GetPacketId (void) const;
98  uint32_t GetPacketSize (void) const;
99 private:
100  uint32_t m_flowId;
101  uint32_t m_packetId;
102  uint32_t m_packetSize;
103 
104 };
105 
106 TypeId
108 {
109  static TypeId tid = TypeId ("ns3::Ipv4FlowProbeTag")
110  .SetParent<Tag> ()
111  .SetGroupName ("FlowMonitor")
112  .AddConstructor<Ipv4FlowProbeTag> ()
113  ;
114  return tid;
115 }
116 TypeId
118 {
119  return GetTypeId ();
120 }
121 uint32_t
123 {
124  return 4 + 4 + 4;
125 }
126 void
128 {
129  buf.WriteU32 (m_flowId);
130  buf.WriteU32 (m_packetId);
131  buf.WriteU32 (m_packetSize);
132 }
133 void
135 {
136  m_flowId = buf.ReadU32 ();
137  m_packetId = buf.ReadU32 ();
138  m_packetSize = buf.ReadU32 ();
139 }
140 void
141 Ipv4FlowProbeTag::Print (std::ostream &os) const
142 {
143  os << "FlowId=" << m_flowId;
144  os << "PacketId=" << m_packetId;
145  os << "PacketSize=" << m_packetSize;
146 }
148  : Tag ()
149 {
150 }
151 
152 Ipv4FlowProbeTag::Ipv4FlowProbeTag (uint32_t flowId, uint32_t packetId, uint32_t packetSize)
153  : Tag (), m_flowId (flowId), m_packetId (packetId), m_packetSize (packetSize)
154 {
155 }
156 
157 void
159 {
160  m_flowId = id;
161 }
162 void
164 {
165  m_packetId = id;
166 }
167 void
169 {
170  m_packetSize = size;
171 }
172 uint32_t
174 {
175  return m_flowId;
176 }
177 uint32_t
179 {
180  return m_packetId;
181 }
182 uint32_t
184 {
185  return m_packetSize;
186 }
187 
189 // Ipv4FlowProbe class implementation //
191 
193  Ptr<Ipv4FlowClassifier> classifier,
194  Ptr<Node> node)
195  : FlowProbe (monitor),
196  m_classifier (classifier)
197 {
198  NS_LOG_FUNCTION (this << node->GetId ());
199 
200  m_ipv4 = node->GetObject<Ipv4L3Protocol> ();
201 
202  if (!m_ipv4->TraceConnectWithoutContext ("SendOutgoing",
204  {
205  NS_FATAL_ERROR ("trace fail");
206  }
207  if (!m_ipv4->TraceConnectWithoutContext ("UnicastForward",
209  {
210  NS_FATAL_ERROR ("trace fail");
211  }
212  if (!m_ipv4->TraceConnectWithoutContext ("LocalDeliver",
214  {
215  NS_FATAL_ERROR ("trace fail");
216  }
217 
218  if (!m_ipv4->TraceConnectWithoutContext ("Drop",
220  {
221  NS_FATAL_ERROR ("trace fail");
222  }
223 
224  // code copied from point-to-point-helper.cc
225  std::ostringstream oss;
226  oss << "/NodeList/" << node->GetId () << "/DeviceList/*/TxQueue/Drop";
228 }
229 
231 {
232 }
233 
234 /* static */
235 TypeId
237 {
238  static TypeId tid = TypeId ("ns3::Ipv4FlowProbe")
239  .SetParent<FlowProbe> ()
240  .SetGroupName ("FlowMonitor")
241  // No AddConstructor because this class has no default constructor.
242  ;
243 
244  return tid;
245 }
246 
247 void
249 {
250  m_ipv4 = 0;
251  m_classifier = 0;
253 }
254 
255 void
256 Ipv4FlowProbe::SendOutgoingLogger (const Ipv4Header &ipHeader, Ptr<const Packet> ipPayload, uint32_t interface)
257 {
258  FlowId flowId;
259  FlowPacketId packetId;
260 
261  if (!m_ipv4->IsUnicast(ipHeader.GetDestination ()))
262  {
263  // we are not prepared to handle broadcast yet
264  return;
265  }
266 
267  if (m_classifier->Classify (ipHeader, ipPayload, &flowId, &packetId))
268  {
269  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
270  NS_LOG_DEBUG ("ReportFirstTx ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<"); "
271  << ipHeader << *ipPayload);
272  m_flowMonitor->ReportFirstTx (this, flowId, packetId, size);
273 
274  // tag the packet with the flow id and packet id, so that the packet can be identified even
275  // when Ipv4Header is not accessible at some non-IPv4 protocol layer
276  Ipv4FlowProbeTag fTag (flowId, packetId, size);
277  ipPayload->AddByteTag (fTag);
278  }
279 }
280 
281 void
282 Ipv4FlowProbe::ForwardLogger (const Ipv4Header &ipHeader, Ptr<const Packet> ipPayload, uint32_t interface)
283 {
284  Ipv4FlowProbeTag fTag;
285  bool found = ipPayload->FindFirstMatchingByteTag (fTag);
286 
287  if (found)
288  {
289  FlowId flowId = fTag.GetFlowId ();
290  FlowPacketId packetId = fTag.GetPacketId ();
291 
292  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
293  NS_LOG_DEBUG ("ReportForwarding ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<");");
294  m_flowMonitor->ReportForwarding (this, flowId, packetId, size);
295  }
296 }
297 
298 void
299 Ipv4FlowProbe::ForwardUpLogger (const Ipv4Header &ipHeader, Ptr<const Packet> ipPayload, uint32_t interface)
300 {
301  Ipv4FlowProbeTag fTag;
302  bool found = ipPayload->FindFirstMatchingByteTag (fTag);
303 
304  if (found)
305  {
306  FlowId flowId = fTag.GetFlowId ();
307  FlowPacketId packetId = fTag.GetPacketId ();
308 
309  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
310  NS_LOG_DEBUG ("ReportLastRx ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<");");
311  m_flowMonitor->ReportLastRx (this, flowId, packetId, size);
312  }
313 }
314 
315 void
317  Ipv4L3Protocol::DropReason reason, Ptr<Ipv4> ipv4, uint32_t ifIndex)
318 {
319 #if 0
320  switch (reason)
321  {
323  break;
324 
327  Ipv4Address addri = m_ipv4->GetAddress (ifIndex);
328  Ipv4Mask maski = m_ipv4->GetNetworkMask (ifIndex);
329  Ipv4Address bcast = addri.GetSubnetDirectedBroadcast (maski);
330  if (ipHeader.GetDestination () == bcast) // we don't want broadcast packets
331  {
332  return;
333  }
334  }
335 #endif
336 
337  Ipv4FlowProbeTag fTag;
338  bool found = ipPayload->FindFirstMatchingByteTag (fTag);
339 
340  if (found)
341  {
342  FlowId flowId = fTag.GetFlowId ();
343  FlowPacketId packetId = fTag.GetPacketId ();
344 
345  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
346  NS_LOG_DEBUG ("Drop ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<", " << reason
347  << ", destIp=" << ipHeader.GetDestination () << "); "
348  << "HDR: " << ipHeader << " PKT: " << *ipPayload);
349 
350  DropReason myReason;
351 
352 
353  switch (reason)
354  {
356  myReason = DROP_TTL_EXPIRE;
357  NS_LOG_DEBUG ("DROP_TTL_EXPIRE");
358  break;
360  myReason = DROP_NO_ROUTE;
361  NS_LOG_DEBUG ("DROP_NO_ROUTE");
362  break;
364  myReason = DROP_BAD_CHECKSUM;
365  NS_LOG_DEBUG ("DROP_BAD_CHECKSUM");
366  break;
368  myReason = DROP_INTERFACE_DOWN;
369  NS_LOG_DEBUG ("DROP_INTERFACE_DOWN");
370  break;
372  myReason = DROP_ROUTE_ERROR;
373  NS_LOG_DEBUG ("DROP_ROUTE_ERROR");
374  break;
376  myReason = DROP_FRAGMENT_TIMEOUT;
377  NS_LOG_DEBUG ("DROP_FRAGMENT_TIMEOUT");
378  break;
379 
380  default:
381  myReason = DROP_INVALID_REASON;
382  NS_FATAL_ERROR ("Unexpected drop reason code " << reason);
383  }
384 
385  m_flowMonitor->ReportDrop (this, flowId, packetId, size, myReason);
386  }
387 }
388 
389 void
391 {
392  Ipv4FlowProbeTag fTag;
393  bool tagFound = ipPayload->FindFirstMatchingByteTag (fTag);
394 
395  if (!tagFound)
396  {
397  return;
398  }
399 
400  FlowId flowId = fTag.GetFlowId ();
401  FlowPacketId packetId = fTag.GetPacketId ();
402  uint32_t size = fTag.GetPacketSize ();
403 
404  NS_LOG_DEBUG ("Drop ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<", " << DROP_QUEUE
405  << "); ");
406 
407  m_flowMonitor->ReportDrop (this, flowId, packetId, size, DROP_QUEUE);
408 }
409 
410 } // namespace ns3
411 
412 
uint32_t FlowPacketId
Abstract identifier of a packet within a flow.
Tag used to allow a fast identification of the packet.
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:805
void ForwardUpLogger(const Ipv4Header &ipHeader, Ptr< const Packet > ipPayload, uint32_t interface)
Log a packet being received by the destination.
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:226
Packet dropped due to TTL decremented to zero during IPv4 forwarding.
Ipv4FlowProbe(Ptr< FlowMonitor > monitor, Ptr< Ipv4FlowClassifier > classifier, Ptr< Node > node)
Constructor.
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
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.
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
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
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
Packet dropped due to missing route to the destination.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
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:789
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.