A Discrete-Event Network Simulator
API
ipv4-packet-filter.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  * 2016 University of Washington
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Authors: Stefano Avallone <stavallo@unina.it>
20  * Tom Henderson <tomhend@u.washington.edu>
21  * Pasquale Imputato <p.imputato@gmail.com>
22  */
23 
24 #include "ns3/log.h"
25 #include "ns3/enum.h"
26 #include "ns3/uinteger.h"
27 #include "ns3/tcp-header.h"
28 #include "ns3/udp-header.h"
29 #include "ipv4-queue-disc-item.h"
30 #include "ipv4-packet-filter.h"
31 
32 namespace ns3 {
33 
34 NS_LOG_COMPONENT_DEFINE ("Ipv4PacketFilter");
35 
36 NS_OBJECT_ENSURE_REGISTERED (Ipv4PacketFilter);
37 
38 TypeId
40 {
41  static TypeId tid = TypeId ("ns3::Ipv4PacketFilter")
43  .SetGroupName ("Internet")
44  ;
45  return tid;
46 }
47 
49 {
50  NS_LOG_FUNCTION (this);
51 }
52 
54 {
55  NS_LOG_FUNCTION (this);
56 }
57 
58 bool
60 {
61  NS_LOG_FUNCTION (this << item);
62  return (DynamicCast<Ipv4QueueDiscItem> (item) != 0);
63 }
64 
65 // ------------------------------------------------------------------------- //
66 
68 
69 TypeId
71 {
72  static TypeId tid = TypeId ("ns3::FqCoDelIpv4PacketFilter")
74  .SetGroupName ("Internet")
75  .AddConstructor<FqCoDelIpv4PacketFilter> ()
76  .AddAttribute ("Perturbation",
77  "The salt used as an additional input to the hash function of this filter",
78  UintegerValue (0),
80  MakeUintegerChecker<uint32_t> ())
81  ;
82  return tid;
83 }
84 
86 {
87  NS_LOG_FUNCTION (this);
88 }
89 
91 {
92  NS_LOG_FUNCTION (this);
93 }
94 
95 int32_t
97 {
98  NS_LOG_FUNCTION (this << item);
99  Ptr<Ipv4QueueDiscItem> ipv4Item = DynamicCast<Ipv4QueueDiscItem> (item);
100 
101  NS_ASSERT (ipv4Item != 0);
102 
103  Ipv4Header hdr = ipv4Item->GetHeader ();
104  Ipv4Address src = hdr.GetSource ();
105  Ipv4Address dest = hdr.GetDestination ();
106  uint8_t prot = hdr.GetProtocol ();
107  uint16_t fragOffset = hdr.GetFragmentOffset ();
108 
109  TcpHeader tcpHdr;
110  UdpHeader udpHdr;
111  uint16_t srcPort = 0;
112  uint16_t destPort = 0;
113 
114  Ptr<Packet> pkt = ipv4Item->GetPacket ();
115 
116  if (prot == 6 && fragOffset == 0) // TCP
117  {
118  pkt->PeekHeader (tcpHdr);
119  srcPort = tcpHdr.GetSourcePort ();
120  destPort = tcpHdr.GetDestinationPort ();
121  }
122  else if (prot == 17 && fragOffset == 0) // UDP
123  {
124  pkt->PeekHeader (udpHdr);
125  srcPort = udpHdr.GetSourcePort ();
126  destPort = udpHdr.GetDestinationPort ();
127  }
128 
129  /* serialize the 5-tuple and the perturbation in buf */
130  uint8_t buf[17];
131  src.Serialize (buf);
132  dest.Serialize (buf + 4);
133  buf[8] = prot;
134  buf[9] = (srcPort >> 8) & 0xff;
135  buf[10] = srcPort & 0xff;
136  buf[11] = (destPort >> 8) & 0xff;
137  buf[12] = destPort & 0xff;
138  buf[13] = (m_perturbation >> 24) & 0xff;
139  buf[14] = (m_perturbation >> 16) & 0xff;
140  buf[15] = (m_perturbation >> 8) & 0xff;
141  buf[16] = m_perturbation & 0xff;
142 
143  /* Linux calculates the jhash2 (jenkins hash), we calculate the murmur3 */
144  uint32_t hash = Hash32 ((char*) buf, 17);
145 
146  NS_LOG_DEBUG ("Found Ipv4 packet; hash value " << hash);
147 
148  return hash;
149 }
150 
151 } // namespace ns3
FqCoDelIpv4PacketFilter is the filter to be added to the FQCoDel queue disc to simulate the behavior ...
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 "...
uint16_t GetDestinationPort() const
Get the destination port.
Definition: tcp-header.cc:137
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Ipv4Address GetDestination(void) const
Definition: ipv4-header.cc:304
#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
uint8_t GetProtocol(void) const
Definition: ipv4-header.cc:272
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Ipv4Address GetSource(void) const
Definition: ipv4-header.cc:291
virtual int32_t DoClassify(Ptr< QueueDiscItem > item) const
Classify a packet.
Packet header for IPv4.
Definition: ipv4-header.h:33
const Ipv4Header & GetHeader(void) const
Hold an unsigned integer type.
Definition: uinteger.h:44
uint32_t m_perturbation
hash perturbation value
void Serialize(uint8_t buf[4]) const
Serialize this address to a 4-byte buffer.
uint32_t Hash32(const char *buffer, const size_t size)
Compute 32-bit hash of a byte buffer, using the default hash function.
Definition: hash.h:276
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:277
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Header for the Transmission Control Protocol.
Definition: tcp-header.h:44
uint16_t GetSourcePort(void) const
Definition: udp-header.cc:65
Packet header for UDP packets.
Definition: udp-header.h:39
virtual bool CheckProtocol(Ptr< QueueDiscItem > item) const
Checks if the filter is able to classify a kind of items.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
uint16_t GetFragmentOffset(void) const
Definition: ipv4-header.cc:246
static TypeId GetTypeId(void)
Get the type ID.
Ipv4PacketFilter is the abstract base class for filters defined for IPv4 packets. ...
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:269
PacketFilter is the abstract base class for filters used by queue discs to classify packets...
Definition: packet-filter.h:34
static TypeId GetTypeId(void)
Get the type ID.
uint16_t GetSourcePort() const
Get the source port.
Definition: tcp-header.cc:131
uint16_t GetDestinationPort(void) const
Definition: udp-header.cc:70
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
Ptr< Packet > GetPacket(void) const
Definition: queue-item.cc:42