A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
8
9#include "tcp-header.h"
10#include "udp-header.h"
11
12#include "ns3/log.h"
13
14namespace ns3
15{
16
17NS_LOG_COMPONENT_DEFINE("Ipv4QueueDiscItem");
18
20 const Address& addr,
21 uint16_t protocol,
22 const Ipv4Header& header)
23 : QueueDiscItem(p, addr, protocol),
24 m_header(header),
25 m_headerAdded(false)
26{
27}
28
33
36{
37 NS_LOG_FUNCTION(this);
39 NS_ASSERT(p);
40 uint32_t ret = p->GetSize();
41 if (!m_headerAdded)
42 {
44 }
45 return ret;
46}
47
48const Ipv4Header&
50{
51 return m_header;
52}
53
54void
56{
57 NS_LOG_FUNCTION(this);
58
59 NS_ASSERT_MSG(!m_headerAdded, "The header has been already added to the packet");
61 NS_ASSERT(p);
62 p->AddHeader(m_header);
63 m_headerAdded = true;
64}
65
66void
67Ipv4QueueDiscItem::Print(std::ostream& os) const
68{
69 if (!m_headerAdded)
70 {
71 os << m_header << " ";
72 }
73 os << GetPacket() << " "
74 << "Dst addr " << GetAddress() << " "
75 << "proto " << (uint16_t)GetProtocol() << " "
76 << "txq " << (uint16_t)GetTxQueueIndex();
77}
78
79bool
81{
82 NS_LOG_FUNCTION(this);
84 {
86 return true;
87 }
88 return false;
89}
90
91bool
93{
94 bool ret = false;
95
96 switch (field)
97 {
98 case IP_DSFIELD:
99 value = m_header.GetTos();
100 ret = true;
101 break;
102 }
103
104 return ret;
105}
106
109{
110 NS_LOG_FUNCTION(this << perturbation);
111
114 uint8_t prot = m_header.GetProtocol();
115 uint16_t fragOffset = m_header.GetFragmentOffset();
116
117 TcpHeader tcpHdr;
118 UdpHeader udpHdr;
119 uint16_t srcPort = 0;
120 uint16_t destPort = 0;
121
122 if (prot == 6 && fragOffset == 0) // TCP
123 {
124 GetPacket()->PeekHeader(tcpHdr);
125 srcPort = tcpHdr.GetSourcePort();
126 destPort = tcpHdr.GetDestinationPort();
127 }
128 else if (prot == 17 && fragOffset == 0) // UDP
129 {
130 GetPacket()->PeekHeader(udpHdr);
131 srcPort = udpHdr.GetSourcePort();
132 destPort = udpHdr.GetDestinationPort();
133 }
134 if (prot != 6 && prot != 17)
135 {
136 NS_LOG_WARN("Unknown transport protocol, no port number included in hash computation");
137 }
138
139 /* serialize the 5-tuple and the perturbation in buf */
140 uint8_t buf[17];
141 src.Serialize(buf);
142 dest.Serialize(buf + 4);
143 buf[8] = prot;
144 buf[9] = (srcPort >> 8) & 0xff;
145 buf[10] = srcPort & 0xff;
146 buf[11] = (destPort >> 8) & 0xff;
147 buf[12] = destPort & 0xff;
148 buf[13] = (perturbation >> 24) & 0xff;
149 buf[14] = (perturbation >> 16) & 0xff;
150 buf[15] = (perturbation >> 8) & 0xff;
151 buf[16] = perturbation & 0xff;
152
153 // Linux calculates jhash2 (jenkins hash), we calculate murmur3 because it is
154 // already available in ns-3
155 uint32_t hash = Hash32((char*)buf, 17);
156
157 NS_LOG_DEBUG("Hash value " << hash);
158
159 return hash;
160}
161
162} // namespace ns3
a polymophic address class
Definition address.h:90
Ipv4 addresses are stored in host order in this class.
void Serialize(uint8_t buf[4]) const
Serialize this address to a 4-byte buffer.
Packet header for IPv4.
Definition ipv4-header.h:23
Ipv4Address GetSource() const
uint8_t GetTos() const
EcnType GetEcn() const
uint8_t GetProtocol() const
Ipv4Address GetDestination() const
void SetEcn(EcnType ecn)
Set ECN Field.
uint32_t GetSerializedSize() const override
uint16_t GetFragmentOffset() const
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
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition packet.cc:294
Smart pointer class similar to boost::intrusive_ptr.
QueueDiscItem is the abstract base class for items that are stored in a queue disc.
Definition queue-item.h:122
Address GetAddress() const
Get the MAC address included in this item.
Definition queue-item.cc:81
uint8_t GetTxQueueIndex() const
Get the transmission queue index included in this item.
Definition queue-item.cc:95
uint16_t GetProtocol() const
Get the L3 protocol included in this item.
Definition queue-item.cc:88
Ptr< Packet > GetPacket() const
Definition queue-item.cc:32
Uint8Values
1-byte fields of the packet whose value can be retrieved, if present
Definition queue-item.h:71
Header for the Transmission Control Protocol.
Definition tcp-header.h:36
uint16_t GetDestinationPort() const
Get the destination port.
uint16_t GetSourcePort() const
Get the source port.
Definition tcp-header.cc:95
Packet header for UDP packets.
Definition udp-header.h:30
uint16_t GetDestinationPort() const
Definition udp-header.cc:43
uint16_t GetSourcePort() const
Definition udp-header.cc:37
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#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:75
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:263
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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:250
Every class exported by the ns3 library is enclosed in the ns3 namespace.