summaryrefslogtreecommitdiffstats
path: root/VNFs/DPPD-PROX/tools/flow_extract/streamsorter.cpp
blob: 65c645e1bc1c888370f469cfc6816896bb497cef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
// Copyright (c) 2010-2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/

#include <iostream>
#include <fstream>
#include <cstdlib>

#include "mappedfile.hpp"
#include "memreader.hpp"
#include "streamsorter.hpp"
#include "path.hpp"
#include "allocator.hpp"
#include "pcapreader.hpp"
#include "progress.hpp"

StreamSorter::StreamSorter(size_t flowTableSize, const string& workingDirectory, size_t memoryLimit)
	: flowTableSize(flowTableSize),
	  workingDirectory(workingDirectory),
	  allocator(memoryLimit, 1024*10),
	  streamID(0)
{
}

void StreamSorter::sort(const string &inputPcapFilePath, const string &outputBinFilePath)
{
	setTempFileName();
	sortChunks(inputPcapFilePath);
	mergeChunks(outputBinFilePath);
}

void StreamSorter::sortChunks(const string &inputPcapFilePath)
{
	ofstream outputTempFile;

	outputTempFile.open(tempFilePath.c_str());

	if (!outputTempFile.is_open())
		return ;

	PcapReader pr;
	PcapPkt pkt;

	if (pr.open(inputPcapFilePath)) {
		pr.getError();
		return;
	}
	PcapPkt::allocator = &allocator;

	Progress progress(pr.end());
	uint32_t packetDetail = progress.addDetail("packet count");

	ft = new FlowTable<pkt_tuple, uint32_t>(flowTableSize);
	resetStreams();

	while (pr.read(&pkt)) {
		processPkt(pkt);
		if (progress.couldRefresh()) {
			progress.setProgress(pr.pos());
			progress.setDetail(packetDetail, pr.getPktReadCount());
			progress.refresh();
		}
		if (allocator.lowThresholdReached()) {
			flushStreams(&outputTempFile);
		}
	}
	progress.setProgress();
	progress.setDetail(packetDetail, pr.getPktReadCount());
	progress.refresh(true);

	pr.close();
	flushStreams(&outputTempFile);
	PcapPkt::allocator = NULL;
	outputTempFile.close();
	delete ft;
}

void StreamSorter::resetStreams()
{
	streams.clear();
}

void StreamSorter::flushStreams(ofstream *outputTempFile)
{
	size_t flushCount = 0;
	size_t offset = outputTempFile->tellp();

	Progress progress(streams.size());

	cout << endl;
	progress.setTitle("flush ");
	for (size_t i = 0; i < streams.size(); ++i) {
		if (streams[i].hasFlushablePackets()) {
			streams[i].flush(outputTempFile);
			flushCount++;
		}

		if (progress.couldRefresh()) {
			progress.setProgress(i);
			progress.refresh();
		}
	}
	progress.setProgress();
	progress.refresh(true);

	if (flushCount)
		flushOffsets.push_back(offset);
	allocator.reset();
}

Stream3 *StreamSorter::addNewStream(PcapPkt::L4Proto proto)
{
	streams.push_back(Stream3(streamID++, proto));
	return &streams.back();
}

FlowTable<pkt_tuple, uint32_t>::entry* StreamSorter::getFlowEntry(const PcapPkt &pkt)
{
	FlowTable<pkt_tuple, uint32_t>::entry *a;
	struct pkt_tuple pt = pkt.parsePkt();
	Stream3 *stream = NULL;

	a = ft->lookup(pt.flip());
	if (!a) {
		a = ft->lookup(pt);
		if (!a) {
			stream = addNewStream(pkt.getProto());

			a = ft->insert(pt, stream->getID(), pkt.ts());
		}
	}

	if (a->expired(pkt.ts(), streams[a->value].getTimeout())) {
		ft->remove(a);

		stream = addNewStream(pkt.getProto());

		a = ft->insert(pt, stream->getID(), pkt.ts());
	}
	return a;
}

void StreamSorter::processPkt(const PcapPkt &pkt)
{
	FlowTable<pkt_tuple, uint32_t>::entry *a;

	a = getFlowEntry(pkt);
	a->tv = pkt.ts();
	streams[a->value].addPkt(pkt);
}

void StreamSorter::mergeChunks(const string &outputBinFile)
{
	cout << "merging chunks: " << tempFilePath << " to " << outputBinFile << endl;
	cout << "have " << flushOffsets.size() << " parts to merge" << endl;
	MappedFile tempFile;

	if (tempFile.open(tempFilePath)) {
		cerr << "failed to open temp file" << endl;
		return;
	}
	ofstream file;

	file.open(outputBinFile.c_str());

	if (!file.is_open()) {
		cerr << "failed top open file '" << outputBinFile << "'" << endl;
		return;
	}
	MemReader memReader(&tempFile, flushOffsets);
	Stream3 stream;

	Progress progress(memReader.getTotalLength());

	while (memReader.read(&stream)) {
		stream.flush(&file);
		if (progress.couldRefresh()) {
			progress.setProgress(memReader.consumed());
			progress.refresh();
		}
	}

	progress.setProgress();
	progress.refresh(true);
	tempFile.close();
}

void StreamSorter::setTempFileName()
{
	tempFilePath = Path(workingDirectory).add("/tmp").str();
}