summaryrefslogtreecommitdiffstats
path: root/src/nat_check_flow_table.cpp
blob: 18ba814276edc9bfe00a842c38db71b44aa26457 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
  Ido Barnea
  Cisco Systems, Inc.
*/

/*
  Copyright (c) 2016-2016 Cisco Systems, Inc.

  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 <assert.h>
#include <stdint.h>
#include <string>
#include <iostream>
#include <new>
#include "nat_check_flow_table.h"
/*
  classes in this file:
  CNatCheckFlowTableList - List implementation
  CNatCheckFlowTableMap - Map implementation
  CNatData - element of data that exists in the map and the list.
  CNatCheckFlowTable - Wrapper class which is the interface to outside.
  New element is always inserted to the list and map.
  If element is removed, it is always removed from list and map together.
  Map is used to lookup elemnt by key.
  List is used to clean up old elements by timestamp. We guarantee the list is always sorted by timestamp,
  since we always insert at the end, with increasing timestamp.
*/

std::ostream& operator<<(std::ostream& os, const CNatData &cn) {
    os << "(" << &cn << ")" << "data:" << cn.m_data << " time:" << cn.m_timestamp;
    os << " prev:" << cn.m_prev << " next:" << cn.m_next;
    return os;
}

// map implementation
CNatData *CNatCheckFlowTableMap::erase(uint64_t key) {
    nat_check_flow_map_iter_no_const_t it = m_map.find(key);

    if (it != m_map.end()) {
        CNatData *val = it->second;
        m_map.erase(it);
        return val;
    }

    return NULL;
}

bool CNatCheckFlowTableMap::find(uint64_t key, uint32_t &val) {
    nat_check_flow_map_iter_t it = m_map.find(key);

    if (it != m_map.end()) {
        CNatData *data = it->second;
        val = data->get_data();
        return true;
    }

    return false;
}

// for testing
bool CNatCheckFlowTableMap::verify(uint32_t *arr, int size) {
    uint32_t val = -1;
    int real_size = 0;

    for (int i = 0; i < size; i++) {
        if (arr[i] == -1)
            continue;
        real_size++;
        find(i, val);
        if (val != arr[i])
            return false;
    }

    if (m_map.size() != real_size) {
        std::cout << "Error: Wrong map size " << m_map.size() << ". Should be " << real_size << std::endl;
        return false;
    }

    return true;
}

CNatData * CNatCheckFlowTableMap::insert(uint64_t key, uint32_t val, double time) {
    CNatData *elem = new CNatData;
    assert(elem);
    elem->set_data(val);
    elem->set_key(key);
    elem->set_timestamp(time);
    std::pair<uint64_t, CNatData *> pair = std::pair<uint64_t, CNatData *>(key, elem);

    if (m_map.insert(pair).second == false) {
        delete elem;
        return NULL;
    } else {
        return elem;
    }
}

std::ostream& operator<<(std::ostream& os, const CNatCheckFlowTableMap& cf) {
    nat_check_flow_map_iter_t it;

    os << "NAT check flow table map:\n";
    for (it = cf.m_map.begin(); it != cf.m_map.end(); it++) {
        CNatData *data = it->second;
        uint32_t key = it->first;
        os << "  " << key << ":" << *data << std::endl;
    }
    return os;
}

void CNatCheckFlowTableList::dump_short(FILE *fd) {
    fprintf(fd, "list:\n");
    // this is not fully safe, since we call it from CP core, and rx core changes the list.
    // It is usefull as a debug function
    if (m_head) {
        fprintf(fd, "  head: time:%f key:%x data:%x\n", m_head->get_timestamp(), m_head->get_key(), m_head->get_data());
        fprintf(fd, "  tail: time:%f key:%x data:%x\n", m_tail->get_timestamp(), m_tail->get_key(), m_tail->get_data());
    }
}

// list implementation
// The list is always sorted by timestamp, since we always insert at the end, with increasing timestamp
void CNatCheckFlowTableList::erase(CNatData *data) {
    if (m_head == data) {
        m_head = data->m_next;
    }
    if (m_tail == data) {
        m_tail = data->m_prev;
    }
    if (data->m_prev) {
        data->m_prev->m_next = data->m_next;
    }
    if (data->m_next) {
        data->m_next->m_prev = data->m_prev;
    }
}

// insert as last element in list
void CNatCheckFlowTableList::insert(CNatData *data) {
    data->m_prev = m_tail;
    data->m_next = NULL;

    if (m_tail != NULL) {
        m_tail->m_next = data;
    } else {
        // if m_tail is NULL m_head is also NULL
        m_head = data;
    }
    m_tail = data;
}

bool CNatCheckFlowTableList::verify(uint32_t *arr, int size) {
    int index = -1;
    CNatData *elem = m_head;
    int count = 0;

    while (index < size - 1) {
        index++;
        if (arr[index] == -1) {
            continue;
        }
        count++;

        if (elem->get_data() != arr[index]) {
            return false;
        }

        if (elem == NULL) {
            std::cout << "Too few items in list. Only " << count << std::endl;
            return false;
        }
        elem = elem->m_next;
    }

    // We expect number of items in list to be like num of val!=-1 in arr
    if (elem != NULL) {
        std::cout << "Too many items in list" << std::endl;
        return false;
    }

    return true;
}

std::ostream& operator<<(std::ostream& os, const CNatCheckFlowTableList& cf) {
    CNatData *it = cf.m_head;

    os << "NAT check flow table list:\n";
    os << "  head:" << cf.m_head << " tail:" << cf.m_tail << std::endl;
    while (it != NULL) {
        os << "  " << *it  << std::endl;
        it = it->m_next;
    }

    return os;
}

// flow table
std::ostream& operator<<(std::ostream& os, const CNatCheckFlowTable& cf) {
    os << "========= Flow table start =========" << std::endl;
    os << cf.m_map;
    os << cf.m_list;
    os << "========= Flow table end =========" << std::endl;

    return os;
}

void CNatCheckFlowTable::clear_old(double time) {
    CNatData *data = m_list.get_head();
    uint32_t val;

    while (data) {
        if (data->get_timestamp() < time) {
            erase(data->get_key(), val);
            data = m_list.get_head();
        } else {
            break;
        }
    }
}

void CNatCheckFlowTable::dump(FILE *fd) {
    fprintf(fd, "map size:%lu\n", m_map.size());
    m_list.dump_short(fd);
}

bool CNatCheckFlowTable::erase(uint64_t key, uint32_t &val) {
    CNatData *data = m_map.erase(key);

    if (!data)
        return false;

    val = data->get_data();
    m_list.erase(data);
    delete data;

    return true;
}

bool CNatCheckFlowTable::insert(uint64_t key, uint32_t val, double time) {
    CNatData *res;

    res = m_map.insert(key, val, time);
    if (!res) {
        return false;
    } else {
        m_list.insert(res);
    }
    return true;
}

CNatCheckFlowTable::~CNatCheckFlowTable() {
    clear_old(UINT64_MAX);
}

bool CNatCheckFlowTable::test() {
    uint32_t size = 100;
    uint32_t arr[size];
    int i;
    uint32_t val;

    for (i = 0; i < size; i++) {
        arr[i] = i+200;
    }

    // insert some elements
    for (i = 0; i < size; i++) {
        val = arr[i];
        assert(insert(i, val, i) == true);
    }

    // insert same elements. should fail
    for (i = 0; i < size; i++) {
        val = arr[i];
        assert(insert(i, val, val) == false);
    }

    // remove element we did not insert
    assert(erase(size, val) == false);

    assert (m_map.verify(arr, size) == true);
    assert (m_list.verify(arr, size) == true);

    // remove even keys
    for (i = 0; i < size; i += 2) {
        assert(erase(i, val) == true);
        assert (val == arr[i]);
        arr[i] = -1;
    }

    assert (m_map.verify(arr, size) == true);
    assert (m_list.verify(arr, size) == true);

    // clear half of the old values (We removed the even already, so 1/4 should be left)
    clear_old(size/2);
    for (i = 0; i < size/2; i++) {
        arr [i] = -1;
    }

    assert (m_map.verify(arr, size) == true);
    assert (m_list.verify(arr, size) == true);

    return true;
}