A Discrete-Event Network Simulator
API
ipv6-queue-disc-item.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2016 Universita' degli Studi di Napoli Federico II
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 
19 #include "ns3/log.h"
20 #include "ipv6-queue-disc-item.h"
21 #include "ns3/tcp-header.h"
22 #include "ns3/udp-header.h"
23 
24 namespace ns3 {
25 
26 NS_LOG_COMPONENT_DEFINE ("Ipv6QueueDiscItem");
27 
29  uint16_t protocol, const Ipv6Header & header)
30  : QueueDiscItem (p, addr, protocol),
31  m_header (header),
32  m_headerAdded (false)
33 {
34 }
35 
37 {
38  NS_LOG_FUNCTION (this);
39 }
40 
41 uint32_t Ipv6QueueDiscItem::GetSize (void) const
42 {
43  NS_LOG_FUNCTION (this);
44  Ptr<Packet> p = GetPacket ();
45  NS_ASSERT (p != 0);
46  uint32_t ret = p->GetSize ();
47  if (!m_headerAdded)
48  {
49  ret += m_header.GetSerializedSize ();
50  }
51  return ret;
52 }
53 
54 const Ipv6Header&
56 {
57  return m_header;
58 }
59 
61 {
62  NS_LOG_FUNCTION (this);
63 
64  NS_ASSERT_MSG (!m_headerAdded, "The header has been already added to the packet");
65  Ptr<Packet> p = GetPacket ();
66  NS_ASSERT (p != 0);
67  p->AddHeader (m_header);
68  m_headerAdded = true;
69 }
70 
71 void
72 Ipv6QueueDiscItem::Print (std::ostream& os) const
73 {
74  if (!m_headerAdded)
75  {
76  os << m_header << " ";
77  }
78  os << GetPacket () << " "
79  << "Dst addr " << GetAddress () << " "
80  << "proto " << (uint16_t) GetProtocol () << " "
81  << "txq " << (uint16_t) GetTxQueueIndex ()
82  ;
83 }
84 
85 bool
87 {
88  NS_LOG_FUNCTION (this);
90  {
92  return true;
93  }
94  return false;
95 }
96 
97 bool
99 {
100  bool ret = false;
101 
102  switch (field)
103  {
104  case IP_DSFIELD:
105  value = m_header.GetTrafficClass ();
106  ret = true;
107  break;
108  }
109 
110  return ret;
111 }
112 
113 uint32_t
114 Ipv6QueueDiscItem::Hash (uint32_t perturbation) const
115 {
116  NS_LOG_FUNCTION (this << perturbation);
117 
120  uint8_t prot = m_header.GetNextHeader ();
121 
122  TcpHeader tcpHdr;
123  UdpHeader udpHdr;
124  uint16_t srcPort = 0;
125  uint16_t destPort = 0;
126 
127  if (prot == 6) // TCP
128  {
129  GetPacket ()->PeekHeader (tcpHdr);
130  srcPort = tcpHdr.GetSourcePort ();
131  destPort = tcpHdr.GetDestinationPort ();
132  }
133  else if (prot == 17) // UDP
134  {
135  GetPacket ()->PeekHeader (udpHdr);
136  srcPort = udpHdr.GetSourcePort ();
137  destPort = udpHdr.GetDestinationPort ();
138  }
139  if (prot != 6 && prot != 17)
140  {
141  NS_LOG_WARN ("Unknown transport protocol, no port number included in hash computation");
142  }
143 
144  /* serialize the 5-tuple and the perturbation in buf */
145  uint8_t buf[41];
146  src.Serialize (buf);
147  dest.Serialize (buf + 16);
148  buf[32] = prot;
149  buf[33] = (srcPort >> 8) & 0xff;
150  buf[34] = srcPort & 0xff;
151  buf[35] = (destPort >> 8) & 0xff;
152  buf[36] = destPort & 0xff;
153  buf[37] = (perturbation >> 24) & 0xff;
154  buf[38] = (perturbation >> 16) & 0xff;
155  buf[39] = (perturbation >> 8) & 0xff;
156  buf[40] = perturbation & 0xff;
157 
158  // Linux calculates jhash2 (jenkins hash), we calculate murmur3 because it is
159  // already available in ns-3
160  uint32_t hash = Hash32 ((char*) buf, 41);
161 
162  NS_LOG_DEBUG ("Found Ipv6 packet; hash of the five tuple " << hash);
163 
164  return hash;
165 }
166 
167 } // namespace ns3
Packet header for IPv6.
Definition: ipv6-header.h:34
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
uint16_t GetSourcePort() const
Get the source port.
Definition: tcp-header.cc:131
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
uint16_t GetDestinationPort() const
Get the destination port.
Definition: tcp-header.cc:137
uint16_t GetProtocol(void) const
Get the L3 protocol included in this item.
Definition: queue-item.cc:98
virtual void Print(std::ostream &os) const
Print the item contents.
QueueDiscItem is the abstract base class for items that are stored in a queue disc.
Definition: queue-item.h:148
Ptr< Packet > GetPacket(void) const
Definition: queue-item.cc:42
const Ipv6Header & GetHeader(void) const
virtual void AddHeader(void)
Add the header to the packet.
a polymophic address class
Definition: address.h:90
Uint8Values
1-byte fields of the packet whose value can be retrieved, if present
Definition: queue-item.h:76
Ipv6Header m_header
The IPv6 header.
uint16_t GetDestinationPort(void) const
Definition: udp-header.cc:70
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
Definition: ipv6-header.cc:143
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:290
Ipv6Address GetSourceAddress(void) const
Get the "Source address" field.
Definition: ipv6-header.cc:100
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetEcn(EcnType ecn)
Set ECN field bits.
Definition: ipv6-header.cc:195
uint32_t Hash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer, using the default hash function.
Definition: hash.h:276
Header for the Transmission Control Protocol.
Definition: tcp-header.h:44
EcnType GetEcn(void) const
Definition: ipv6-header.cc:262
Packet header for UDP packets.
Definition: udp-header.h:39
uint8_t GetNextHeader(void) const
Get the next header.
Definition: ipv6-header.cc:80
virtual bool Mark(void)
Marks the packet by setting ECN_CE bits if the packet has ECN_ECT0 or ECN_ECT1 set.
virtual bool GetUint8Value(Uint8Values field, uint8_t &value) const
Retrieve the value of a given field from the packet, if present.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:88
bool m_headerAdded
True if the header has already been added to the packet.
uint8_t GetTxQueueIndex(void) const
Get the transmission queue index included in this item.
Definition: queue-item.cc:105
Describes an IPv6 address.
Definition: ipv6-address.h:49
uint8_t GetTrafficClass(void) const
Get the "Traffic class" field.
Definition: ipv6-header.cc:50
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
Address GetAddress(void) const
Get the MAC address included in this item.
Definition: queue-item.cc:91
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
uint16_t GetSourcePort(void) const
Definition: udp-header.cc:65
Ipv6QueueDiscItem()
Default constructor.
virtual uint32_t Hash(uint32_t perturbation) const
Computes the hash of the packet&#39;s 5-tuple.
Ipv6Address GetDestinationAddress(void) const
Get the "Destination address" field.
Definition: ipv6-header.cc:110
virtual uint32_t GetSize(void) const
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
void Serialize(uint8_t buf[16]) const
Serialize this address to a 16-byte buffer.