A Discrete-Event Network Simulator
API
ipv6-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 "ipv6-queue-disc-item.h"
30 #include "ipv6-packet-filter.h"
31 
32 namespace ns3 {
33 
34 NS_LOG_COMPONENT_DEFINE ("Ipv6PacketFilter");
35 
36 NS_OBJECT_ENSURE_REGISTERED (Ipv6PacketFilter);
37 
38 TypeId
40 {
41  static TypeId tid = TypeId ("ns3::Ipv6PacketFilter")
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<Ipv6QueueDiscItem> (item) != 0);
63 }
64 
65 // ------------------------------------------------------------------------- //
66 
68 
69 TypeId
71 {
72  static TypeId tid = TypeId ("ns3::FqCoDelIpv6PacketFilter")
74  .SetGroupName ("Internet")
75  .AddConstructor<FqCoDelIpv6PacketFilter> ()
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<Ipv6QueueDiscItem> ipv6Item = DynamicCast<Ipv6QueueDiscItem> (item);
100 
101  NS_ASSERT (ipv6Item != 0);
102 
103  Ipv6Header hdr = ipv6Item->GetHeader ();
104  Ipv6Address src = hdr.GetSourceAddress ();
105  Ipv6Address dest = hdr.GetDestinationAddress ();
106  uint8_t prot = hdr.GetNextHeader ();
107 
108  TcpHeader tcpHdr;
109  UdpHeader udpHdr;
110  uint16_t srcPort = 0;
111  uint16_t destPort = 0;
112 
113  Ptr<Packet> pkt = ipv6Item->GetPacket ();
114 
115  if (prot == 6) // TCP
116  {
117  pkt->PeekHeader (tcpHdr);
118  srcPort = tcpHdr.GetSourcePort ();
119  destPort = tcpHdr.GetDestinationPort ();
120  }
121  else if (prot == 17) // 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[41];
130  src.Serialize (buf);
131  dest.Serialize (buf + 16);
132  buf[32] = prot;
133  buf[33] = (srcPort >> 8) & 0xff;
134  buf[34] = srcPort & 0xff;
135  buf[35] = (destPort >> 8) & 0xff;
136  buf[36] = destPort & 0xff;
137  buf[37] = (m_perturbation >> 24) & 0xff;
138  buf[38] = (m_perturbation >> 16) & 0xff;
139  buf[39] = (m_perturbation >> 8) & 0xff;
140  buf[40] = m_perturbation & 0xff;
141 
142  /* Linux calculates the jhash2 (jenkins hash), we calculate the murmur3 */
143  uint32_t hash = Hash32 ((char*) buf, 41);
144 
145  NS_LOG_DEBUG ("Found Ipv6 packet; hash of the five tuple " << hash);
146 
147  return hash;
148 }
149 
150 } // namespace ns3
FqCoDelIpv6PacketFilter is the filter to be added to the FQCoDel queue disc to simulate the behavior ...
uint8_t GetNextHeader(void) const
Get the next header.
Definition: ipv6-header.cc:80
Packet header for IPv6.
Definition: ipv6-header.h:34
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
static TypeId GetTypeId(void)
Get the type ID.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
#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:201
static TypeId GetTypeId(void)
Get the type ID.
Hold an unsigned integer type.
Definition: uinteger.h:44
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
virtual int32_t DoClassify(Ptr< QueueDiscItem > item) const
Classify a packet.
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
uint32_t m_perturbation
hash perturbation value
Describes an IPv6 address.
Definition: ipv6-address.h:48
Ipv6PacketFilter is the abstract base class for filters defined for IPv6 packets. ...
Ipv6Address GetSourceAddress(void) const
Get the "Source address" field.
Definition: ipv6-header.cc:100
#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
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
virtual bool CheckProtocol(Ptr< QueueDiscItem > item) const
Checks if the filter is able to classify a kind of items.
Ipv6Address GetDestinationAddress(void) const
Get the "Destination address" field.
Definition: ipv6-header.cc:110
void Serialize(uint8_t buf[16]) const
Serialize this address to a 16-byte buffer.