A Discrete-Event Network Simulator
API
ipv4-queue-disc-item.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Universita' degli Studi di Napoli Federico II
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17 
18 #include "ipv4-queue-disc-item.h"
19 
20 #include "tcp-header.h"
21 #include "udp-header.h"
22 
23 #include "ns3/log.h"
24 
25 namespace ns3
26 {
27 
28 NS_LOG_COMPONENT_DEFINE("Ipv4QueueDiscItem");
29 
31  const Address& addr,
32  uint16_t protocol,
33  const Ipv4Header& header)
34  : QueueDiscItem(p, addr, protocol),
35  m_header(header),
36  m_headerAdded(false)
37 {
38 }
39 
41 {
42  NS_LOG_FUNCTION(this);
43 }
44 
45 uint32_t
47 {
48  NS_LOG_FUNCTION(this);
49  Ptr<Packet> p = GetPacket();
50  NS_ASSERT(p);
51  uint32_t ret = p->GetSize();
52  if (!m_headerAdded)
53  {
54  ret += m_header.GetSerializedSize();
55  }
56  return ret;
57 }
58 
59 const Ipv4Header&
61 {
62  return m_header;
63 }
64 
65 void
67 {
68  NS_LOG_FUNCTION(this);
69 
70  NS_ASSERT_MSG(!m_headerAdded, "The header has been already added to the packet");
71  Ptr<Packet> p = GetPacket();
72  NS_ASSERT(p);
73  p->AddHeader(m_header);
74  m_headerAdded = true;
75 }
76 
77 void
78 Ipv4QueueDiscItem::Print(std::ostream& os) const
79 {
80  if (!m_headerAdded)
81  {
82  os << m_header << " ";
83  }
84  os << GetPacket() << " "
85  << "Dst addr " << GetAddress() << " "
86  << "proto " << (uint16_t)GetProtocol() << " "
87  << "txq " << (uint16_t)GetTxQueueIndex();
88 }
89 
90 bool
92 {
93  NS_LOG_FUNCTION(this);
95  {
97  return true;
98  }
99  return false;
100 }
101 
102 bool
104 {
105  bool ret = false;
106 
107  switch (field)
108  {
109  case IP_DSFIELD:
110  value = m_header.GetTos();
111  ret = true;
112  break;
113  }
114 
115  return ret;
116 }
117 
118 uint32_t
119 Ipv4QueueDiscItem::Hash(uint32_t perturbation) const
120 {
121  NS_LOG_FUNCTION(this << perturbation);
122 
125  uint8_t prot = m_header.GetProtocol();
126  uint16_t fragOffset = m_header.GetFragmentOffset();
127 
128  TcpHeader tcpHdr;
129  UdpHeader udpHdr;
130  uint16_t srcPort = 0;
131  uint16_t destPort = 0;
132 
133  if (prot == 6 && fragOffset == 0) // TCP
134  {
135  GetPacket()->PeekHeader(tcpHdr);
136  srcPort = tcpHdr.GetSourcePort();
137  destPort = tcpHdr.GetDestinationPort();
138  }
139  else if (prot == 17 && fragOffset == 0) // UDP
140  {
141  GetPacket()->PeekHeader(udpHdr);
142  srcPort = udpHdr.GetSourcePort();
143  destPort = udpHdr.GetDestinationPort();
144  }
145  if (prot != 6 && prot != 17)
146  {
147  NS_LOG_WARN("Unknown transport protocol, no port number included in hash computation");
148  }
149 
150  /* serialize the 5-tuple and the perturbation in buf */
151  uint8_t buf[17];
152  src.Serialize(buf);
153  dest.Serialize(buf + 4);
154  buf[8] = prot;
155  buf[9] = (srcPort >> 8) & 0xff;
156  buf[10] = srcPort & 0xff;
157  buf[11] = (destPort >> 8) & 0xff;
158  buf[12] = destPort & 0xff;
159  buf[13] = (perturbation >> 24) & 0xff;
160  buf[14] = (perturbation >> 16) & 0xff;
161  buf[15] = (perturbation >> 8) & 0xff;
162  buf[16] = perturbation & 0xff;
163 
164  // Linux calculates jhash2 (jenkins hash), we calculate murmur3 because it is
165  // already available in ns-3
166  uint32_t hash = Hash32((char*)buf, 17);
167 
168  NS_LOG_DEBUG("Hash value " << hash);
169 
170  return hash;
171 }
172 
173 } // namespace ns3
a polymophic address class
Definition: address.h:101
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
void Serialize(uint8_t buf[4]) const
Serialize this address to a 4-byte buffer.
Packet header for IPv4.
Definition: ipv4-header.h:34
Ipv4Address GetSource() const
Definition: ipv4-header.cc:302
uint8_t GetTos() const
Definition: ipv4-header.cc:196
EcnType GetEcn() const
Definition: ipv4-header.cc:169
uint8_t GetProtocol() const
Definition: ipv4-header.cc:281
Ipv4Address GetDestination() const
Definition: ipv4-header.cc:316
void SetEcn(EcnType ecn)
Set ECN Field.
Definition: ipv4-header.cc:100
uint32_t GetSerializedSize() const override
Definition: ipv4-header.cc:384
uint16_t GetFragmentOffset() const
Definition: ipv4-header.cc:254
bool m_headerAdded
True if the header has already been added to the packet.
const Ipv4Header & GetHeader() const
Ipv4Header m_header
The IPv4 header.
uint32_t Hash(uint32_t perturbation) const override
Computes the hash of the packet's 5-tuple.
void AddHeader() override
Add the header to the packet.
bool GetUint8Value(Uint8Values field, uint8_t &value) const override
Retrieve the value of a given field from the packet, if present.
bool Mark() override
Marks the packet by setting ECN_CE bits if the packet has ECN_ECT0 or ECN_ECT1 set.
void Print(std::ostream &os) const override
Print the item contents.
uint32_t GetSize() const override
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:268
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:305
QueueDiscItem is the abstract base class for items that are stored in a queue disc.
Definition: queue-item.h:133
Address GetAddress() const
Get the MAC address included in this item.
Definition: queue-item.cc:92
uint8_t GetTxQueueIndex() const
Get the transmission queue index included in this item.
Definition: queue-item.cc:106
uint16_t GetProtocol() const
Get the L3 protocol included in this item.
Definition: queue-item.cc:99
Ptr< Packet > GetPacket() const
Definition: queue-item.cc:43
Uint8Values
1-byte fields of the packet whose value can be retrieved, if present
Definition: queue-item.h:82
Header for the Transmission Control Protocol.
Definition: tcp-header.h:47
uint16_t GetDestinationPort() const
Get the destination port.
Definition: tcp-header.cc:112
uint16_t GetSourcePort() const
Get the source port.
Definition: tcp-header.cc:106
Packet header for UDP packets.
Definition: udp-header.h:41
uint16_t GetDestinationPort() const
Definition: udp-header.cc:54
uint16_t GetSourcePort() const
Definition: udp-header.cc:48
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#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:86
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:274
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:261
std::size_t hash(const BasicJsonType &j)
hash a JSON value
Definition: json.h:4680
Every class exported by the ns3 library is enclosed in the ns3 namespace.