A Discrete-Event Network Simulator
API
epc-tft-classifier.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 CTTC
4  * Copyright (c) 2010 TELEMATICS LAB, DEE - Politecnico di Bari
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:
20  * Nicola Baldo <nbaldo@cttc.es> (the EpcTftClassifier class)
21  * Giuseppe Piro <g.piro@poliba.it> (part of the code in EpcTftClassifier::Classify ()
22  * which comes from RrcEntity::Classify of the GSoC 2010 LTE module)
23  *
24  */
25 
26 
27 
28 
29 #include "epc-tft-classifier.h"
30 #include "epc-tft.h"
31 #include "ns3/log.h"
32 #include "ns3/packet.h"
33 #include "ns3/ipv4-header.h"
34 #include "ns3/udp-header.h"
35 #include "ns3/tcp-header.h"
36 #include "ns3/udp-l4-protocol.h"
37 #include "ns3/tcp-l4-protocol.h"
38 
39 namespace ns3 {
40 
41 NS_LOG_COMPONENT_DEFINE ("EpcTftClassifier");
42 
44 {
45  NS_LOG_FUNCTION (this);
46 }
47 
48 void
50 {
51  NS_LOG_FUNCTION (this << tft);
52 
53  m_tftMap[id] = tft;
54 
55  // simple sanity check: there shouldn't be more than 16 bearers (hence TFTs) per UE
56  NS_ASSERT (m_tftMap.size () <= 16);
57 }
58 
59 void
61 {
62  NS_LOG_FUNCTION (this << id);
63  m_tftMap.erase (id);
64 }
65 
66 
67 uint32_t
69 {
70  NS_LOG_FUNCTION (this << p << direction);
71 
72  Ptr<Packet> pCopy = p->Copy ();
73 
74  Ipv4Header ipv4Header;
75  pCopy->RemoveHeader (ipv4Header);
76 
77  Ipv4Address localAddress;
78  Ipv4Address remoteAddress;
79 
80 
81  if (direction == EpcTft::UPLINK)
82  {
83  localAddress = ipv4Header.GetSource ();
84  remoteAddress = ipv4Header.GetDestination ();
85  }
86  else
87  {
88  NS_ASSERT (direction == EpcTft::DOWNLINK);
89  remoteAddress = ipv4Header.GetSource ();
90  localAddress = ipv4Header.GetDestination ();
91  }
92 
93  uint8_t protocol = ipv4Header.GetProtocol ();
94 
95  uint8_t tos = ipv4Header.GetTos ();
96 
97  uint16_t localPort = 0;
98  uint16_t remotePort = 0;
99 
100  if (protocol == UdpL4Protocol::PROT_NUMBER)
101  {
102  UdpHeader udpHeader;
103  pCopy->RemoveHeader (udpHeader);
104 
105  if (direction == EpcTft::UPLINK)
106  {
107  localPort = udpHeader.GetSourcePort ();
108  remotePort = udpHeader.GetDestinationPort ();
109  }
110  else
111  {
112  remotePort = udpHeader.GetSourcePort ();
113  localPort = udpHeader.GetDestinationPort ();
114  }
115  }
116  else if (protocol == TcpL4Protocol::PROT_NUMBER)
117  {
118  TcpHeader tcpHeader;
119  pCopy->RemoveHeader (tcpHeader);
120  if (direction == EpcTft::UPLINK)
121  {
122  localPort = tcpHeader.GetSourcePort ();
123  remotePort = tcpHeader.GetDestinationPort ();
124  }
125  else
126  {
127  remotePort = tcpHeader.GetSourcePort ();
128  localPort = tcpHeader.GetDestinationPort ();
129  }
130  }
131  else
132  {
133  NS_LOG_INFO ("Unknown protocol: " << protocol);
134  return 0; // no match
135  }
136 
137  NS_LOG_INFO ("Classifing packet:"
138  << " localAddr=" << localAddress
139  << " remoteAddr=" << remoteAddress
140  << " localPort=" << localPort
141  << " remotePort=" << remotePort
142  << " tos=0x" << (uint16_t) tos );
143 
144  // now it is possible to classify the packet!
145  // we use a reverse iterator since filter priority is not implemented properly.
146  // This way, since the default bearer is expected to be added first, it will be evaluated last.
147  std::map <uint32_t, Ptr<EpcTft> >::const_reverse_iterator it;
148  NS_LOG_LOGIC ("TFT MAP size: " << m_tftMap.size ());
149 
150  for (it = m_tftMap.rbegin (); it != m_tftMap.rend (); ++it)
151  {
152  NS_LOG_LOGIC ("TFT id: " << it->first );
153  NS_LOG_LOGIC (" Ptr<EpcTft>: " << it->second);
154  Ptr<EpcTft> tft = it->second;
155  if (tft->Matches (direction, remoteAddress, localAddress, remotePort, localPort, tos))
156  {
157  NS_LOG_LOGIC ("matches with TFT ID = " << it->first);
158  return it->first; // the id of the matching TFT
159  }
160  }
161  NS_LOG_LOGIC ("no match");
162  return 0; // no match
163 }
164 
165 
166 } // namespace ns3
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
Direction
Indicates the direction of the traffic that is to be classified.
Definition: epc-tft.h:56
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
void Add(Ptr< EpcTft > tft, uint32_t id)
add a TFT to the Classifier
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
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
Ipv4Address GetSource(void) const
Definition: ipv4-header.cc:291
uint32_t Classify(Ptr< Packet > p, EpcTft::Direction direction)
classify an IP packet
Packet header for IPv4.
Definition: ipv4-header.h:33
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
static const uint8_t PROT_NUMBER
protocol number (0x6)
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
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
std::map< uint32_t, Ptr< EpcTft > > m_tftMap
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
uint16_t GetSourcePort() const
Get the source port.
Definition: tcp-header.cc:131
uint8_t GetTos(void) const
Definition: ipv4-header.cc:194
void Delete(uint32_t id)
delete an existing TFT from the classifier
uint16_t GetDestinationPort(void) const
Definition: udp-header.cc:70
static const uint8_t PROT_NUMBER
protocol number (0x11)