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/tcp-header.h"
27 #include "ns3/udp-header.h"
28 #include "ipv6-queue-disc-item.h"
29 #include "ipv6-packet-filter.h"
30 
31 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("Ipv6PacketFilter");
34 
35 NS_OBJECT_ENSURE_REGISTERED (Ipv6PacketFilter);
36 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::Ipv6PacketFilter")
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<Ipv6QueueDiscItem> (item) != 0);
62 }
63 
64 // ------------------------------------------------------------------------- //
65 
67 
68 TypeId
70 {
71  static TypeId tid = TypeId ("ns3::FqCoDelIpv6PacketFilter")
73  .SetGroupName ("Internet")
74  .AddConstructor<FqCoDelIpv6PacketFilter> ()
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<Ipv6QueueDiscItem> ipv6Item = DynamicCast<Ipv6QueueDiscItem> (item);
99 
100  NS_ASSERT (ipv6Item != 0);
101 
102  Ipv6Header hdr = ipv6Item->GetHeader ();
103  Ipv6Address src = hdr.GetSourceAddress ();
104  Ipv6Address dest = hdr.GetDestinationAddress ();
105  uint8_t prot = hdr.GetNextHeader ();
106 
107  TcpHeader tcpHdr;
108  UdpHeader udpHdr;
109  uint16_t srcPort = 0;
110  uint16_t destPort = 0;
111 
112  Ptr<Packet> pkt = ipv6Item->GetPacket ();
113 
114  if (prot == 6) // TCP
115  {
116  pkt->PeekHeader (tcpHdr);
117  srcPort = tcpHdr.GetSourcePort ();
118  destPort = tcpHdr.GetDestinationPort ();
119  }
120  else if (prot == 17) // UDP
121  {
122  pkt->PeekHeader (udpHdr);
123  srcPort = udpHdr.GetSourcePort ();
124  destPort = udpHdr.GetDestinationPort ();
125  }
126 
127  /* serialize the 5-tuple and the perturbation in buf */
128  uint8_t buf[41];
129  src.Serialize (buf);
130  dest.Serialize (buf + 16);
131  buf[32] = prot;
132  buf[33] = (srcPort >> 8) & 0xff;
133  buf[34] = srcPort & 0xff;
134  buf[35] = (destPort >> 8) & 0xff;
135  buf[36] = destPort & 0xff;
136  buf[37] = (m_perturbation >> 24) & 0xff;
137  buf[38] = (m_perturbation >> 16) & 0xff;
138  buf[39] = (m_perturbation >> 8) & 0xff;
139  buf[40] = m_perturbation & 0xff;
140 
141  /* Linux calculates the jhash2 (jenkins hash), we calculate the murmur3 */
142  uint32_t hash = Hash32 ((char*) buf, 41);
143 
144  NS_LOG_DEBUG ("Found Ipv6 packet; hash of the five tuple " << hash);
145 
146  return hash;
147 }
148 
149 } // 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:44
#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:278
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:236
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:904
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.