A Discrete-Event Network Simulator
API
ipv6-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 // Modifications: Tommaso Pecorella <tommaso.pecorella@unifi.it>
20 //
21 
22 #include "ns3/ipv6-flow-probe.h"
23 #include "ns3/ipv6-flow-classifier.h"
24 #include "ns3/node.h"
25 #include "ns3/packet.h"
26 #include "ns3/flow-monitor.h"
27 #include "ns3/log.h"
28 #include "ns3/pointer.h"
29 #include "ns3/config.h"
30 #include "ns3/flow-id-tag.h"
31 
32 namespace ns3 {
33 
34 NS_LOG_COMPONENT_DEFINE ("Ipv6FlowProbe");
35 
37 // Ipv6FlowProbeTag class implementation //
39 
49 class Ipv6FlowProbeTag : public Tag
50 {
51 public:
56  static TypeId GetTypeId (void);
57  virtual TypeId GetInstanceTypeId (void) const;
58  virtual uint32_t GetSerializedSize (void) const;
59  virtual void Serialize (TagBuffer buf) const;
60  virtual void Deserialize (TagBuffer buf);
61  virtual void Print (std::ostream &os) const;
69  Ipv6FlowProbeTag (uint32_t flowId, uint32_t packetId, uint32_t packetSize);
74  void SetFlowId (uint32_t flowId);
79  void SetPacketId (uint32_t packetId);
84  void SetPacketSize (uint32_t packetSize);
89  uint32_t GetFlowId (void) const;
94  uint32_t GetPacketId (void) const;
99  uint32_t GetPacketSize (void) const;
100 private:
101  uint32_t m_flowId;
102  uint32_t m_packetId;
103  uint32_t m_packetSize;
104 
105 };
106 
107 TypeId
109 {
110  static TypeId tid = TypeId ("ns3::Ipv6FlowProbeTag")
111  .SetParent<Tag> ()
112  .AddConstructor<Ipv6FlowProbeTag> ()
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 Ipv6FlowProbeTag::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 Ipv6FlowProbeTag::Ipv6FlowProbeTag (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 // Ipv6FlowProbe class implementation //
191 
193  Ptr<Ipv6FlowClassifier> classifier,
194  Ptr<Node> node)
195  : FlowProbe (monitor),
196  m_classifier (classifier)
197 {
198  NS_LOG_FUNCTION (this << node->GetId ());
199 
201 
202  if (!ipv6->TraceConnectWithoutContext ("SendOutgoing",
204  {
205  NS_FATAL_ERROR ("trace fail");
206  }
207  if (!ipv6->TraceConnectWithoutContext ("UnicastForward",
209  {
210  NS_FATAL_ERROR ("trace fail");
211  }
212  if (!ipv6->TraceConnectWithoutContext ("LocalDeliver",
214  {
215  NS_FATAL_ERROR ("trace fail");
216  }
217 
218  if (!ipv6->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 void
236 {
238 }
239 
240 void
241 Ipv6FlowProbe::SendOutgoingLogger (const Ipv6Header &ipHeader, Ptr<const Packet> ipPayload, uint32_t interface)
242 {
243  FlowId flowId;
244  FlowPacketId packetId;
245 
246  if (m_classifier->Classify (ipHeader, ipPayload, &flowId, &packetId))
247  {
248  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
249  NS_LOG_DEBUG ("ReportFirstTx ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<"); "
250  << ipHeader << *ipPayload);
251  m_flowMonitor->ReportFirstTx (this, flowId, packetId, size);
252 
253  // tag the packet with the flow id and packet id, so that the packet can be identified even
254  // when Ipv6Header is not accessible at some non-IPv6 protocol layer
255  Ipv6FlowProbeTag fTag (flowId, packetId, size);
256  ipPayload->AddByteTag (fTag);
257  }
258 }
259 
260 void
261 Ipv6FlowProbe::ForwardLogger (const Ipv6Header &ipHeader, Ptr<const Packet> ipPayload, uint32_t interface)
262 {
263  Ipv6FlowProbeTag fTag;
264  bool found = ipPayload->FindFirstMatchingByteTag (fTag);
265 
266  if (found)
267  {
268  FlowId flowId = fTag.GetFlowId ();
269  FlowPacketId packetId = fTag.GetPacketId ();
270 
271  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
272  NS_LOG_DEBUG ("ReportForwarding ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<");");
273  m_flowMonitor->ReportForwarding (this, flowId, packetId, size);
274  }
275 }
276 
277 void
278 Ipv6FlowProbe::ForwardUpLogger (const Ipv6Header &ipHeader, Ptr<const Packet> ipPayload, uint32_t interface)
279 {
280  Ipv6FlowProbeTag fTag;
281  bool found = ipPayload->FindFirstMatchingByteTag (fTag);
282 
283  if (found)
284  {
285  FlowId flowId = fTag.GetFlowId ();
286  FlowPacketId packetId = fTag.GetPacketId ();
287 
288  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
289  NS_LOG_DEBUG ("ReportLastRx ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<");");
290  m_flowMonitor->ReportLastRx (this, flowId, packetId, size);
291  }
292 }
293 
294 void
296  Ipv6L3Protocol::DropReason reason, Ptr<Ipv6> ipv6, uint32_t ifIndex)
297 {
298 #if 0
299  switch (reason)
300  {
302  break;
303 
305  case Ipv6L3Protocol::DROP_BAD_CHECKSUM:
306  Ipv6Address addri = m_ipv6->GetAddress (ifIndex);
307  Ipv6Mask maski = m_ipv6->GetNetworkMask (ifIndex);
308  Ipv6Address bcast = addri.GetSubnetDirectedBroadcast (maski);
309  if (ipHeader.GetDestination () == bcast) // we don't want broadcast packets
310  {
311  return;
312  }
313  }
314 #endif
315 
316  Ipv6FlowProbeTag fTag;
317  bool found = ipPayload->FindFirstMatchingByteTag (fTag);
318 
319  if (found)
320  {
321  FlowId flowId = fTag.GetFlowId ();
322  FlowPacketId packetId = fTag.GetPacketId ();
323 
324  uint32_t size = (ipPayload->GetSize () + ipHeader.GetSerializedSize ());
325  NS_LOG_DEBUG ("Drop ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<", " << reason
326  << ", destIp=" << ipHeader.GetDestinationAddress () << "); "
327  << "HDR: " << ipHeader << " PKT: " << *ipPayload);
328 
329  DropReason myReason;
330 
331 
332  switch (reason)
333  {
335  myReason = DROP_TTL_EXPIRE;
336  NS_LOG_DEBUG ("DROP_TTL_EXPIRE");
337  break;
339  myReason = DROP_NO_ROUTE;
340  NS_LOG_DEBUG ("DROP_NO_ROUTE");
341  break;
343  myReason = DROP_INTERFACE_DOWN;
344  NS_LOG_DEBUG ("DROP_INTERFACE_DOWN");
345  break;
347  myReason = DROP_ROUTE_ERROR;
348  NS_LOG_DEBUG ("DROP_ROUTE_ERROR");
349  break;
351  myReason = DROP_UNKNOWN_PROTOCOL;
352  NS_LOG_DEBUG ("DROP_UNKNOWN_PROTOCOL");
353  break;
355  myReason = DROP_UNKNOWN_OPTION;
356  NS_LOG_DEBUG ("DROP_UNKNOWN_OPTION");
357  break;
359  myReason = DROP_MALFORMED_HEADER;
360  NS_LOG_DEBUG ("DROP_MALFORMED_HEADER");
361  break;
363  myReason = DROP_FRAGMENT_TIMEOUT;
364  NS_LOG_DEBUG ("DROP_FRAGMENT_TIMEOUT");
365  break;
366  default:
367  myReason = DROP_INVALID_REASON;
368  NS_FATAL_ERROR ("Unexpected drop reason code " << reason);
369  }
370 
371  m_flowMonitor->ReportDrop (this, flowId, packetId, size, myReason);
372  }
373 }
374 
375 void
377 {
378  Ipv6FlowProbeTag fTag;
379  bool tagFound = ipPayload->FindFirstMatchingByteTag (fTag);
380 
381  if (!tagFound)
382  {
383  return;
384  }
385 
386  FlowId flowId = fTag.GetFlowId ();
387  FlowPacketId packetId = fTag.GetPacketId ();
388  uint32_t size = fTag.GetPacketSize ();
389 
390  NS_LOG_DEBUG ("Drop ("<<this<<", "<<flowId<<", "<<packetId<<", "<<size<<", " << DROP_QUEUE
391  << "); ");
392 
393  m_flowMonitor->ReportDrop (this, flowId, packetId, size, DROP_QUEUE);
394 }
395 
396 } // namespace ns3
397 
398 
uint32_t FlowPacketId
Abstract identifier of a packet within a flow.
virtual uint32_t GetSerializedSize(void) const
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
Definition: ipv6-header.cc:143
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:819
Introspection did not find any typical Config paths.
Definition: ipv6-header.h:33
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 "...
uint32_t m_packetSize
packet size
Interface is down so can not send packet.
IPv6 layer implementation.
void SendOutgoingLogger(const Ipv6Header &ipHeader, Ptr< const Packet > ipPayload, uint32_t interface)
Log a packet being sent.
uint32_t m_flowId
flow identifier
void DropLogger(const Ipv6Header &ipHeader, Ptr< const Packet > ipPayload, Ipv6L3Protocol::DropReason reason, Ptr< Ipv6 > ipv6, uint32_t ifIndex)
Log a packet being dropped.
Interface is down so can not send packet.
Tag used to allow a fast identification of the packet.
#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
uint32_t m_packetId
packet identifier
TAG_BUFFER_INLINE uint32_t ReadU32(void)
Definition: tag-buffer.h:215
void SetPacketId(uint32_t packetId)
Set the packet identifier.
static TypeId GetTypeId(void)
Get the type ID.
Ptr< FlowMonitor > m_flowMonitor
the FlowMonitor instance
Definition: flow-probe.h:102
void SetPacketSize(uint32_t packetSize)
Set the packet size.
virtual void Serialize(TagBuffer buf) const
uint32_t GetPacketSize(void) const
Get the packet size.
TAG_BUFFER_INLINE void WriteU32(uint32_t v)
Definition: tag-buffer.h:186
void ForwardUpLogger(const Ipv6Header &ipHeader, Ptr< const Packet > ipPayload, uint32_t interface)
Log a packet being received by the destination.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1290
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
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.
Packet dropped due to missing route to the destination.
DropReason
enumeration of possible reasons why a packet may be dropped
virtual void DoDispose(void)
Destructor implementation.
The FlowProbe class is responsible for listening for packet events in a specific point of the simulat...
Definition: flow-probe.h:39
Describes an IPv6 address.
Definition: ipv6-address.h:47
read and write tag data
Definition: tag-buffer.h:51
Fallback reason (no known reason)
uint32_t GetId(void) const
Definition: node.cc:106
void SetFlowId(uint32_t flowId)
Set the flow identifier.
Packet dropped due to TTL decremented to zero during IPv4 forwarding.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
uint32_t GetFlowId(void) const
Set the flow identifier.
virtual void Print(std::ostream &os) const
Ipv6FlowProbe(Ptr< FlowMonitor > monitor, Ptr< Ipv6FlowClassifier > classifier, Ptr< Node > node)
Constructor.
static const uint32_t packetSize
uint32_t GetPacketId(void) const
Set the packet identifier.
void ForwardLogger(const Ipv6Header &ipHeader, Ptr< const Packet > ipPayload, uint32_t interface)
Log a packet being forwarded.
Packet dropped due to queue overflow.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
Ptr< Ipv6FlowClassifier > m_classifier
the Ipv6FlowClassifier this probe is associated with
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:727
virtual void DoDispose(void)
Destructor implementation.
Definition: flow-probe.cc:39
Ipv6Address GetDestinationAddress(void) const
Get the "Destination address" field.
Definition: ipv6-header.cc:111
void QueueDropLogger(Ptr< const Packet > ipPayload)
Log a packet being dropped by a queue.
DropReason
Reason why a packet has been dropped.
virtual void Deserialize(TagBuffer buf)
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition: packet.cc:803
uint32_t FlowId
Abstract identifier of a packet flow.