-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvirtFlowTable.c
436 lines (356 loc) · 12.5 KB
/
virtFlowTable.c
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* Copyright (C) 2014 Joshua Hare, Lance Hartung, and Suman Banerjee.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <linux/module.h>
#include <linux/version.h>
#include <linux/sched.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/hash.h>
#include <linux/rcupdate.h>
#include <linux/if.h> // IFNAMSIZ
#include <linux/if_ether.h>
#include "virt.h"
#include "virtDebug.h"
#include "virtParse.h"
#include "virtEgress.h"
#include "virtPolicy.h"
#include "virtEgressLookup.h"
#include "virtFlowTable.h"
#include "virtSelectInterface.h"
#include "virtNAT.h"
#include "virtMemory.h"
#include "virtRetransmission.h"
const unsigned int RETX_TIMEOUT = 250000;
static void cleanup_timer_fn(unsigned long arg);
static void flow_table_entry_destroy(struct flow_table_entry *entry);
static u32 flow_hash(struct flow_tuple *key, unsigned bits);
static int keys_equal(struct flow_tuple *key1, struct flow_tuple *key2);
/*
* Allocate space for the flow hash table. The size is 2^bits.
*/
int init_flow_table(struct flow_table *ftable, unsigned bits)
{
const unsigned table_size = 1u << bits;
const unsigned long timeout = get_flow_table_timeout_jiffies();
int i;
ftable->head = kmalloc(table_size * sizeof(struct flow_table_head), GFP_KERNEL);
if(!ftable->head)
return -ENOMEM;
for(i = 0; i < table_size; i++) {
struct flow_table_head *head = &ftable->head[i];
spin_lock_init(&head->lock);
INIT_HLIST_HEAD(&head->list);
}
ftable->bits = bits;
ftable->size = table_size;
ftable->last_flush = jiffies;
ftable->restart_timer = true;
spin_lock_init(&ftable->timer_lock);
init_timer(&ftable->timer);
ftable->timer.data = (unsigned long)ftable;
ftable->timer.function = cleanup_timer_fn;
mod_timer(&ftable->timer, jiffies + timeout);
return 0;
}
/*
* Allocate a flow_table_entry structure and the substructures. Initializes
* most of the values. Caller needs to fill in the flow key and flow id,
* though.
*/
struct flow_table_entry *alloc_flow_table_entry(void)
{
struct flow_table_entry *entry;
entry = kmalloc(sizeof(struct flow_table_entry), GFP_ATOMIC);
if(!entry)
goto fail;
memset(entry, 0, sizeof(*entry));
entry->key = kmalloc(sizeof(struct flow_tuple), GFP_ATOMIC);
if(!entry->key)
goto fail_free_entry;
memset(entry->key, 0, sizeof(*entry->key));
entry->flow_stats = kmalloc(sizeof(struct flow_stats), GFP_ATOMIC);
if(!entry->flow_stats)
goto fail_free_key;
memset(entry->flow_stats, 0, sizeof(*entry->flow_stats));
inc_alloc_count(FLOW_TABLE_ENTRY);
entry->last_touched = jiffies;
virt_retx_init(&entry->retx, RETX_TIMEOUT);
atomic_set(&entry->refcnt, 1);
return entry;
fail_free_key:
kfree(entry->key);
fail_free_entry:
kfree(entry);
fail:
return NULL;
}
/*
* Add an entry to the flow hash table.
*/
int flow_table_add(struct flow_table *ftable, struct flow_table_entry *entry)
{
u32 hash = flow_hash(entry->key, ftable->bits);
struct flow_table_head *head = &ftable->head[hash];
if(WARN_ON(hash >= ftable->size))
return -1;
spin_lock_bh(&head->lock);
hlist_add_head_rcu(&entry->hlist, &head->list);
spin_unlock_bh(&head->lock);
flow_table_entry_hold(entry);
return 0;
}
/*
* Lookup an entry in the flow hash table.
*/
struct flow_table_entry *flow_table_lookup(struct flow_table *ftable, struct flow_tuple *key)
{
u32 hash = flow_hash(key, ftable->bits);
struct flow_table_head *head = &ftable->head[hash];
struct flow_table_entry *entry;
struct hlist_node *pos;
if(WARN_ON(hash >= ftable->size))
return NULL;
rcu_read_lock();
hlist_for_each_entry_rcu(entry, pos, &head->list, hlist) {
if(keys_equal(key, entry->key)) {
flow_table_entry_hold(entry);
rcu_read_unlock();
return entry;
}
}
rcu_read_unlock();
return NULL;
}
static void cleanup_timer_fn(unsigned long arg)
{
struct flow_table *ftable = (struct flow_table *)arg;
const unsigned long timeout = get_flow_table_timeout_jiffies();
flow_table_clean(ftable);
spin_lock_bh(&ftable->timer_lock);
if(ftable->restart_timer)
mod_timer(&ftable->timer, jiffies + (timeout >> 2));
spin_unlock_bh(&ftable->timer_lock);
}
/*
* This is called when no one is looking at the entries that we removed.
*/
static void flow_table_remove_rcu(struct rcu_head *head)
{
struct flow_table_entry *entry;
entry = container_of(head, struct flow_table_entry, rcu);
flow_table_entry_destroy(entry);
}
/*
* This functions traverses the hash table and frees all entries. This
* function should be called when module is unloaded.
*/
void flow_table_destroy(struct flow_table *ftable)
{
int i;
might_sleep();
spin_lock_bh(&ftable->timer_lock);
ftable->restart_timer = false;
spin_unlock_bh(&ftable->timer_lock);
/* This must not be called with timer_lock held because that could cause
* deadlock. */
del_timer_sync(&ftable->timer);
for(i = 0; i < ftable->size; i++) {
struct flow_table_head *head = &ftable->head[i];
struct hlist_node *pos;
struct hlist_node *tmp;
struct flow_table_entry *entry;
spin_lock_bh(&head->lock);
hlist_for_each_entry_safe(entry, pos, tmp, &head->list, hlist) {
hlist_del_rcu(&entry->hlist);
flow_table_entry_put(entry);
}
spin_unlock_bh(&head->lock);
}
/* Block until all of the entries have been deleted. */
synchronize_rcu();
kfree(ftable->head);
ftable->head = NULL;
}
/*
* Walk through the flow cache and remove stale flows. Flows are removed if
* they have been inactive for longer than flow_table_timeout.
*
* The function tries to limit wasted computaton by running approximately four
* times per timeout period and returning immediately if it was run recently.
*/
void flow_table_clean(struct flow_table *ftable)
{
int i;
const unsigned long timeout = get_flow_table_timeout_jiffies();
long now = jiffies;
unsigned long deletes = 0;
for(i = 0; i < ftable->size; i++) {
struct flow_table_head *head = &ftable->head[i];
struct hlist_node *pos;
struct hlist_node *tmp;
struct flow_table_entry *entry;
spin_lock_bh(&head->lock);
hlist_for_each_entry_safe(entry, pos, tmp, &head->list, hlist) {
if(now - (long)entry->last_touched > timeout) {
VIRT_DBG("clean: %08x:%04hx - %08x:%04hx\n",
entry->key->sAddr, entry->key->sPort,
entry->key->dAddr, entry->key->dPort);
hlist_del_rcu(&entry->hlist);
flow_table_entry_put(entry);
deletes++;
}
}
spin_unlock_bh(&head->lock);
}
ftable->last_flush = now;
VIRT_DBG("removed %lu stale entries\n", deletes);
}
/*
* Increment the entry's reference count. As long as the refcnt is positive,
* flow_table_clean will not free it even if the flow is idle.
*/
void flow_table_entry_hold(struct flow_table_entry *entry)
{
atomic_inc(&entry->refcnt);
}
/*
* Decrement the entry's reference count. If the refcnt becomes zero, then
* the entry will be freed from memory.
*/
void flow_table_entry_put(struct flow_table_entry *entry)
{
if(atomic_dec_and_test(&entry->refcnt))
call_rcu(&entry->rcu, flow_table_remove_rcu);
}
/*
* This function will free the memory consumed by a hash entry structure and
* all memory for sub structures.
*/
static void flow_table_entry_destroy(struct flow_table_entry *entry)
{
VIRT_DBG("destroy: %08x:%04hx - %08x:%04hx\n",
entry->key->sAddr, entry->key->sPort,
entry->key->dAddr, entry->key->dPort);
virt_retx_destroy(&entry->retx);
if(entry->rnode) {
remote_node_put(entry->rnode);
entry->rnode = NULL;
}
if(entry->flow_stats) {
path_release_flow(entry->network, entry->flow_stats);
kfree(entry->flow_stats);
}
if(entry->nat)
nat_table_delete(entry->nat);
if(entry->policy)
policy_put(entry->policy);
if(entry->key)
kfree(entry->key);
kfree(entry);
inc_free_count(FLOW_TABLE_ENTRY);
}
/*
* Compute a hash value for the flow tuple.
*/
static u32 flow_hash(struct flow_tuple *key, unsigned bits)
{
// TODO: What is the best way to combine values?
u32 sum = key->sAddr + key->dAddr + key->sPort + key->dPort + key->proto;
return hash_32(sum, bits);
}
/*
* This function compares two sets or keys to determine hash table lookup matches.
*/
static int keys_equal(struct flow_tuple *key1, struct flow_tuple *key2)
{
return (key1->dAddr == key2->dAddr &&
key1->sAddr == key2->sAddr &&
key1->dPort == key2->dPort &&
key1->sPort == key2->sPort &&
key1->proto == key2->proto);
}
void dump_flow_table(struct seq_file *s, struct virt_priv *virt)
{
struct flow_table *flow_table = &virt->flow_table;
unsigned long now = jiffies;
int i;
// xxxxxxxx xxxxxxxx xxxx xxxx xxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx xxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx
seq_printf(s, "source dest prot spt dpt action linksel txdev rxdev rxpkts txpkts rxbytes txbytes lastpkt refcnt\n");
rcu_read_lock();
for(i = 0; i < flow_table->size; i++) {
struct flow_table_head *head = &flow_table->head[i];
struct flow_table_entry *entry;
struct hlist_node *node;
hlist_for_each_entry_rcu(entry, node, &head->list, hlist) {
const struct flow_tuple *flow = entry->key;
const struct policy_entry *policy = entry->policy;
struct net_device *last_tx_dev = NULL;
struct net_device *last_rx_dev = NULL;
const struct flow_stats *flowst = entry->flow_stats;
long lastpkt = now - entry->last_touched;
long lastpkt_msecs = -1;
if(lastpkt > 0)
lastpkt_msecs = jiffies_to_msecs((unsigned long)lastpkt);
if(WARN_ON(!flow))
continue;
if(WARN_ON(!policy))
continue;
if(WARN_ON(!policy->alg))
continue;
if(WARN_ON(!flowst))
continue;
last_tx_dev = dev_get_by_index(&init_net, flowst->last_tx_dev);
last_rx_dev = dev_get_by_index(&init_net, flowst->last_rx_dev);
seq_printf(s, "%08x %08x %04hx %04hx %04hx %08x %-8s %-8s %-8s %6lu %6lu %8lu %8lu %8ld %6d\n",
flow->sAddr, flow->dAddr,
flow->proto, flow->sPort, flow->dPort,
policy->action, policy->alg->name,
(last_tx_dev ? last_tx_dev->name : "n/a"),
(last_rx_dev ? last_rx_dev->name : "n/a"),
flowst->rx_packets, flowst->tx_packets,
flowst->rx_bytes, flowst->tx_bytes,
lastpkt_msecs,
atomic_read(&entry->refcnt));
if(last_tx_dev)
dev_put(last_tx_dev);
if(last_rx_dev)
dev_put(last_rx_dev);
}
}
rcu_read_unlock();
}
/*
* Free up buffers used for retransmissions and make sure all retransmission
* timers have been stopped.
*/
void flow_table_kill_retx(struct flow_table *ftable)
{
int i;
rcu_read_lock();
for(i = 0; i < ftable->size; i++) {
struct flow_table_head *head = &ftable->head[i];
struct flow_table_entry *entry;
struct hlist_node *node;
hlist_for_each_entry_rcu(entry, node, &head->list, hlist) {
virt_retx_destroy(&entry->retx);
}
}
rcu_read_unlock();
}