A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-flow-classifier.cc
Go to the documentation of this file.
1//
2// Copyright (c) 2009 INESC Porto
3//
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License version 2 as
6// published by the Free Software Foundation;
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program; if not, write to the Free Software
15// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16//
17// Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt> <gjcarneiro@gmail.com>
18//
19
21
22#include "ns3/packet.h"
23#include "ns3/tcp-header.h"
24#include "ns3/udp-header.h"
25
26#include <algorithm>
27
28namespace ns3
29{
30
31/* see http://www.iana.org/assignments/protocol-numbers */
32const uint8_t TCP_PROT_NUMBER = 6; //!< TCP Protocol number
33const uint8_t UDP_PROT_NUMBER = 17; //!< UDP Protocol number
34
35bool
37{
38 if (t1.sourceAddress < t2.sourceAddress)
39 {
40 return true;
41 }
42 if (t1.sourceAddress != t2.sourceAddress)
43 {
44 return false;
45 }
46
48 {
49 return true;
50 }
52 {
53 return false;
54 }
55
56 if (t1.protocol < t2.protocol)
57 {
58 return true;
59 }
60 if (t1.protocol != t2.protocol)
61 {
62 return false;
63 }
64
65 if (t1.sourcePort < t2.sourcePort)
66 {
67 return true;
68 }
69 if (t1.sourcePort != t2.sourcePort)
70 {
71 return false;
72 }
73
75 {
76 return true;
77 }
79 {
80 return false;
81 }
82
83 return false;
84}
85
86bool
88{
89 return (t1.sourceAddress == t2.sourceAddress &&
92}
93
95{
96}
97
98bool
100 Ptr<const Packet> ipPayload,
101 uint32_t* out_flowId,
102 uint32_t* out_packetId)
103{
104 if (ipHeader.GetFragmentOffset() > 0)
105 {
106 // Ignore fragments: they don't carry a valid L4 header
107 return false;
108 }
109
110 FiveTuple tuple;
111 tuple.sourceAddress = ipHeader.GetSource();
112 tuple.destinationAddress = ipHeader.GetDestination();
113 tuple.protocol = ipHeader.GetProtocol();
114
115 if ((tuple.protocol != UDP_PROT_NUMBER) && (tuple.protocol != TCP_PROT_NUMBER))
116 {
117 return false;
118 }
119
120 if (ipPayload->GetSize() < 4)
121 {
122 // the packet doesn't carry enough bytes
123 return false;
124 }
125
126 // we rely on the fact that for both TCP and UDP the ports are
127 // carried in the first 4 octets.
128 // This allows to read the ports even on fragmented packets
129 // not carrying a full TCP or UDP header.
130
131 uint8_t data[4];
132 ipPayload->CopyData(data, 4);
133
134 uint16_t srcPort = 0;
135 srcPort |= data[0];
136 srcPort <<= 8;
137 srcPort |= data[1];
138
139 uint16_t dstPort = 0;
140 dstPort |= data[2];
141 dstPort <<= 8;
142 dstPort |= data[3];
143
144 tuple.sourcePort = srcPort;
145 tuple.destinationPort = dstPort;
146
147 // try to insert the tuple, but check if it already exists
148 auto insert = m_flowMap.insert(std::pair<FiveTuple, FlowId>(tuple, 0));
149
150 // if the insertion succeeded, we need to assign this tuple a new flow identifier
151 if (insert.second)
152 {
153 FlowId newFlowId = GetNewFlowId();
154 insert.first->second = newFlowId;
155 m_flowPktIdMap[newFlowId] = 0;
156 m_flowDscpMap[newFlowId];
157 }
158 else
159 {
160 m_flowPktIdMap[insert.first->second]++;
161 }
162
163 // increment the counter of packets with the same DSCP value
164 Ipv4Header::DscpType dscp = ipHeader.GetDscp();
165 auto dscpInserter = m_flowDscpMap[insert.first->second].insert(
166 std::pair<Ipv4Header::DscpType, uint32_t>(dscp, 1));
167
168 // if the insertion did not succeed, we need to increment the counter
169 if (!dscpInserter.second)
170 {
171 m_flowDscpMap[insert.first->second][dscp]++;
172 }
173
174 *out_flowId = insert.first->second;
175 *out_packetId = m_flowPktIdMap[*out_flowId];
176
177 return true;
178}
179
182{
183 for (auto iter = m_flowMap.begin(); iter != m_flowMap.end(); iter++)
184 {
185 if (iter->second == flowId)
186 {
187 return iter->first;
188 }
189 }
190 NS_FATAL_ERROR("Could not find the flow with ID " << flowId);
191 FiveTuple retval = {Ipv4Address::GetZero(), Ipv4Address::GetZero(), 0, 0, 0};
192 return retval;
193}
194
195bool
196Ipv4FlowClassifier::SortByCount::operator()(std::pair<Ipv4Header::DscpType, uint32_t> left,
197 std::pair<Ipv4Header::DscpType, uint32_t> right)
198{
199 return left.second > right.second;
200}
201
202std::vector<std::pair<Ipv4Header::DscpType, uint32_t>>
204{
205 auto flow = m_flowDscpMap.find(flowId);
206
207 if (flow == m_flowDscpMap.end())
208 {
209 NS_FATAL_ERROR("Could not find the flow with ID " << flowId);
210 }
211
212 std::vector<std::pair<Ipv4Header::DscpType, uint32_t>> v(flow->second.begin(),
213 flow->second.end());
214 std::sort(v.begin(), v.end(), SortByCount());
215 return v;
216}
217
218void
219Ipv4FlowClassifier::SerializeToXmlStream(std::ostream& os, uint16_t indent) const
220{
221 Indent(os, indent);
222 os << "<Ipv4FlowClassifier>\n";
223
224 indent += 2;
225 for (auto iter = m_flowMap.begin(); iter != m_flowMap.end(); iter++)
226 {
227 Indent(os, indent);
228 os << "<Flow flowId=\"" << iter->second << "\""
229 << " sourceAddress=\"" << iter->first.sourceAddress << "\""
230 << " destinationAddress=\"" << iter->first.destinationAddress << "\""
231 << " protocol=\"" << int(iter->first.protocol) << "\""
232 << " sourcePort=\"" << iter->first.sourcePort << "\""
233 << " destinationPort=\"" << iter->first.destinationPort << "\">\n";
234
235 indent += 2;
236 auto flow = m_flowDscpMap.find(iter->second);
237
238 if (flow != m_flowDscpMap.end())
239 {
240 for (auto i = flow->second.begin(); i != flow->second.end(); i++)
241 {
242 Indent(os, indent);
243 os << "<Dscp value=\"0x" << std::hex << static_cast<uint32_t>(i->first) << "\""
244 << " packets=\"" << std::dec << i->second << "\" />\n";
245 }
246 }
247
248 indent -= 2;
249 Indent(os, indent);
250 os << "</Flow>\n";
251 }
252
253 indent -= 2;
254 Indent(os, indent);
255 os << "</Ipv4FlowClassifier>\n";
256}
257
258} // namespace ns3
FlowId GetNewFlowId()
Returns a new, unique Flow Identifier.
static Ipv4Address GetZero()
Comparator used to sort the vector of DSCP values.
bool operator()(std::pair< Ipv4Header::DscpType, uint32_t > left, std::pair< Ipv4Header::DscpType, uint32_t > right)
Comparator function.
std::map< FlowId, FlowPacketId > m_flowPktIdMap
Map to FlowIds to FlowPacketId.
void SerializeToXmlStream(std::ostream &os, uint16_t indent) const override
Serializes the results to an std::ostream in XML format.
std::vector< std::pair< Ipv4Header::DscpType, uint32_t > > GetDscpCounts(FlowId flowId) const
get the DSCP values of the packets belonging to the flow with the given FlowId, sorted in decreasing ...
FiveTuple FindFlow(FlowId flowId) const
Searches for the FiveTuple corresponding to the given flowId.
std::map< FlowId, std::map< Ipv4Header::DscpType, uint32_t > > m_flowDscpMap
Map FlowIds to (DSCP value, packet count) pairs.
std::map< FiveTuple, FlowId > m_flowMap
Map to Flows Identifiers to FlowIds.
bool Classify(const Ipv4Header &ipHeader, Ptr< const Packet > ipPayload, uint32_t *out_flowId, uint32_t *out_packetId)
try to classify the packet into flow-id and packet-id
Packet header for IPv4.
Definition: ipv4-header.h:34
Ipv4Address GetSource() const
Definition: ipv4-header.cc:302
uint8_t GetProtocol() const
Definition: ipv4-header.cc:281
Ipv4Address GetDestination() const
Definition: ipv4-header.cc:316
DscpType GetDscp() const
Definition: ipv4-header.cc:108
DscpType
DiffServ codepoints.
Definition: ipv4-header.h:72
uint16_t GetFragmentOffset() const
Definition: ipv4-header.cc:254
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
Every class exported by the ns3 library is enclosed in the ns3 namespace.
const uint8_t TCP_PROT_NUMBER
TCP Protocol number.
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:157
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:170
const uint8_t UDP_PROT_NUMBER
UDP Protocol number.
uint8_t data[writeSize]
Helper to indent output a specified number of steps.
Definition: test.cc:648
Structure to classify a packet.
uint16_t destinationPort
Destination port.
Ipv4Address sourceAddress
Source address.
Ipv4Address destinationAddress
Destination address.