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/tcp-header.h"
27 #include "ns3/udp-header.h"
28 #include "ipv4-queue-disc-item.h"
29 #include "ipv4-packet-filter.h"
30 
31 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("Ipv4PacketFilter");
34 
35 NS_OBJECT_ENSURE_REGISTERED (Ipv4PacketFilter);
36 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::Ipv4PacketFilter")
42  .SetGroupName ("Internet")
43  ;
44  return tid;
45 }
46 
48 {
49  NS_LOG_FUNCTION (this);
50 }
51 
53 {
54  NS_LOG_FUNCTION (this);
55 }
56 
57 bool
59 {
60  NS_LOG_FUNCTION (this << item);
61  return (DynamicCast<Ipv4QueueDiscItem> (item) != 0);
62 }
63 
64 // ------------------------------------------------------------------------- //
65 
67 
68 TypeId
70 {
71  static TypeId tid = TypeId ("ns3::FqCoDelIpv4PacketFilter")
73  .SetGroupName ("Internet")
74  .AddConstructor<FqCoDelIpv4PacketFilter> ()
75  .AddAttribute ("Perturbation",
76  "The salt used as an additional input to the hash function of this filter",
77  UintegerValue (0),
79  MakeUintegerChecker<uint32_t> ())
80  ;
81  return tid;
82 }
83 
85 {
86  NS_LOG_FUNCTION (this);
87 }
88 
90 {
91  NS_LOG_FUNCTION (this);
92 }
93 
94 int32_t
96 {
97  NS_LOG_FUNCTION (this << item);
98  Ptr<Ipv4QueueDiscItem> ipv4Item = DynamicCast<Ipv4QueueDiscItem> (item);
99 
100  NS_ASSERT (ipv4Item != 0);
101 
102  Ipv4Header hdr = ipv4Item->GetHeader ();
103  Ipv4Address src = hdr.GetSource ();
104  Ipv4Address dest = hdr.GetDestination ();
105  uint8_t prot = hdr.GetProtocol ();
106  uint16_t fragOffset = hdr.GetFragmentOffset ();
107 
108  TcpHeader tcpHdr;
109  UdpHeader udpHdr;
110  uint16_t srcPort = 0;
111  uint16_t destPort = 0;
112 
113  Ptr<Packet> pkt = ipv4Item->GetPacket ();
114 
115  if (prot == 6 && fragOffset == 0) // TCP
116  {
117  pkt->PeekHeader (tcpHdr);
118  srcPort = tcpHdr.GetSourcePort ();
119  destPort = tcpHdr.GetDestinationPort ();
120  }
121  else if (prot == 17 && fragOffset == 0) // UDP
122  {
123  pkt->PeekHeader (udpHdr);
124  srcPort = udpHdr.GetSourcePort ();
125  destPort = udpHdr.GetDestinationPort ();
126  }
127 
128  /* serialize the 5-tuple and the perturbation in buf */
129  uint8_t buf[17];
130  src.Serialize (buf);
131  dest.Serialize (buf + 4);
132  buf[8] = prot;
133  buf[9] = (srcPort >> 8) & 0xff;
134  buf[10] = srcPort & 0xff;
135  buf[11] = (destPort >> 8) & 0xff;
136  buf[12] = destPort & 0xff;
137  buf[13] = (m_perturbation >> 24) & 0xff;
138  buf[14] = (m_perturbation >> 16) & 0xff;
139  buf[15] = (m_perturbation >> 8) & 0xff;
140  buf[16] = m_perturbation & 0xff;
141 
142  /* Linux calculates the jhash2 (jenkins hash), we calculate the murmur3 */
143  uint32_t hash = Hash32 ((char*) buf, 17);
144 
145  NS_LOG_DEBUG ("Found Ipv4 packet; hash value " << hash);
146 
147  return hash;
148 }
149 
150 } // 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:44
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:278
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:236
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:904
Ptr< Packet > GetPacket(void) const
Definition: net-device.cc:45