-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvirtNetwork.c
566 lines (454 loc) · 14.9 KB
/
virtNetwork.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
* 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/slab.h>
#include <linux/list.h>
#include <linux/hash.h>
#include "virt.h"
#include "virtNetwork.h"
#include "virtPassive.h"
#include "virtHashTable.h"
#include "virtMemory.h"
#include "virtEgress.h"
static int next_node_index = 1;
static inline u32 node_hash(struct remote_node *node, unsigned bits)
{
return hash_32(node->priv_ip.s_addr, bits);
}
static inline u32 link_hash(struct remote_link *link, unsigned bits)
{
return hash_32(link->rif.ip4, bits);
}
/*
* Allocate space for a remote_node structure.
*
* Initializes refcnt to 1.
*/
struct remote_node* alloc_remote_node(struct net_device *master_dev)
{
struct remote_node* node;
node = kmalloc(sizeof(struct remote_node), GFP_KERNEL);
if(unlikely(!node))
return NULL;
inc_alloc_count(REMOTE_NODE);
memset(node, 0, sizeof(struct remote_node));
INIT_LIST_HEAD(&node->links);
spin_lock_init(&node->links_lock);
node->next_link_index = 1;
node->max_link_prio = MIN_USABLE_DEVICE_PRIORITY;
skb_queue_head_init(&node->tx_queue);
node->tx_queue_limit = virt_tx_queue_limit();
init_timer(&node->tx_queue_timer);
node->tx_queue_timer.data = (unsigned long)node;
node->tx_queue_timer.function = tx_queue_timer_fn;
node->restart_timer = true;
reorder_head_init(master_dev, &node->reorder_head);
INIT_LIST_HEAD(&node->stalled_paths);
spin_lock_init(&node->stalled_paths_lock);
atomic_set(&node->refcnt, 1);
return node;
}
/*
* Add a node to the table.
*/
void add_remote_node(struct virt_network *net, struct remote_node *node)
{
struct virt_hash_table *table = &net->node_table;
u32 hash = node_hash(node, table->bits);
remote_node_hold(node);
virt_hash_table_add(table, &node->hlist, hash);
node->index = next_node_index++;
node->network = net;
}
/*
* Remove the node and all of its links from the table. If any links cannot be
* removed, then they and the node will be marked for deletion later.
*/
void delete_remote_node(struct virt_network *net, struct remote_node *node)
{
struct virt_hash_table *table = &net->node_table;
u32 hash;
might_sleep();
node->restart_timer = false;
del_timer_sync(&node->tx_queue_timer);
/* Remove all links associated with the node. */
if(node->link_count > 0) {
struct remote_link *link;
rcu_read_lock();
list_for_each_entry_rcu(link, &node->links, rif.list) {
delete_remote_link(net, link);
}
rcu_read_unlock();
}
/* Remove the node from the node table. */
hash = node_hash(node, table->bits);
virt_hash_table_remove(table, &node->hlist, hash);
/* Clear out the tx_queue. */
while(skb_queue_len(&node->tx_queue) > 0) {
struct sk_buff *skb;
skb = skb_dequeue(&node->tx_queue);
if(likely(skb)) {
struct virt_skb_cb *skb_cb = (struct virt_skb_cb *)skb->cb;
struct flow_table_entry *flow = skb_cb->flow;
if(flow)
flow_table_entry_put(flow);
dev_kfree_skb(skb);
}
}
reorder_head_destroy(&node->reorder_head);
remote_node_put(node);
}
/*
* Increment the reference count.
*/
void remote_node_hold(struct remote_node *node)
{
atomic_inc(&node->refcnt);
}
/*
* Decrement the reference count. If it reaches zero, the node will be deleted.
*/
void remote_node_put(struct remote_node *node)
{
if(atomic_dec_and_test(&node->refcnt)) {
kfree_rcu(node, rcu);
inc_free_count(REMOTE_NODE);
}
}
/*
* Allocate a remote link structure.
*
* Initializes the reference count to one.
*/
struct remote_link* alloc_remote_link(void)
{
struct remote_link* link;
link = kmalloc(sizeof(struct remote_link), GFP_KERNEL);
if(unlikely(!link))
return NULL;
inc_alloc_count(REMOTE_LINK);
memset(link, 0, sizeof(struct remote_link));
link->rif.type = INTERFACE_REMOTE;
link->rif.prio = DEFAULT_DEVICE_PRIORITY;
atomic_set(&link->refcnt, 1);
return link;
}
/*
* Add a remote link to a remote node.
*/
void add_remote_link(struct virt_network *net, struct remote_node* node,
struct remote_link* link)
{
struct virt_hash_table *table = &net->link_table;
u32 hash = link_hash(link, table->bits);
remote_link_hold(link);
virt_hash_table_add(table, &link->hlist, hash);
link->index = node->next_link_index++;
link->network = net;
remote_node_hold(node);
link->node = node;
remote_link_hold(link);
spin_lock_bh(&node->links_lock);
list_add_tail_rcu(&link->rif.list, &node->links);
spin_unlock_bh(&node->links_lock);
node->link_count++;
if(link->rif.prio > node->max_link_prio)
node->max_link_prio = link->rif.prio;
}
/*
* Remove the link from the table and from its parent node's list of links.
*/
void delete_remote_link(struct virt_network *net, struct remote_link *link)
{
struct virt_hash_table *table = &net->link_table;
u32 hash = link_hash(link, table->bits);
/* Remove any paths involving this link before the link (and possibly its
* node) are deleted. */
remove_paths_to_remote(net, link);
if(link->node) {
struct remote_node *node = link->node;
spin_lock_bh(&node->links_lock);
list_del_rcu(&link->rif.list);
spin_unlock_bh(&node->links_lock);
if(link->rif.prio == node->max_link_prio)
node->max_link_prio = find_max_remote_link_prio(node);
node->link_count--;
link->node = NULL;
/* Release reference to parent node. */
remote_node_put(node);
/* Release reference (that node had) to link. */
remote_link_put(link);
}
link->rif.prio = MIN_DEVICE_PRIORITY; /* Need flows to stop using this link. */
virt_hash_table_remove(table, &link->hlist, hash);
remote_link_put(link);
}
/*
* Increment the reference count.
*/
void remote_link_hold(struct remote_link *link)
{
atomic_inc(&link->refcnt);
}
/*
* Decrement the reference count. If it reaches zero, the link will be deleted.
*/
void remote_link_put(struct remote_link *link)
{
if(atomic_dec_and_test(&link->refcnt)) {
kfree_rcu(link, rcu);
inc_free_count(REMOTE_LINK);
}
}
/*
* Lookup a node by private IP address.
*
* Increments reference count before returning the node.
*/
struct remote_node *find_remote_node_by_ip(struct virt_network *net,
const struct in_addr *priv_ip)
{
struct virt_hash_table *table = &net->node_table;
u32 hash = hash_32(priv_ip->s_addr, table->bits);
struct virt_hash_head *head = &table->head[hash];
struct remote_node *node;
struct hlist_node *pos;
if(WARN_ON(hash >= table->size))
return NULL;
rcu_read_lock();
hlist_for_each_entry_rcu(node, pos, &head->list, hlist) {
if(priv_ip->s_addr == node->priv_ip.s_addr) {
remote_node_hold(node);
rcu_read_unlock();
return node;
}
}
rcu_read_unlock();
return NULL;
}
/*
* Lookup a node by index.
*
* Does not increment reference counts or use any locking, so only call with
* rcu read lock held.
*/
struct remote_node *__find_remote_node_by_index(struct virt_network *net,
int index)
{
struct virt_hash_table *table = &net->node_table;
int i;
for(i = 0; i < table->size; i++) {
struct virt_hash_head *head = &table->head[i];
struct remote_node *node;
struct hlist_node *pos;
hlist_for_each_entry_rcu(node, pos, &head->list, hlist) {
if(node->index == index)
return node;
}
}
return NULL;
}
/*
* Lookup a link by its public IP address.
*
* Increments reference count before returning the link.
*/
struct remote_link *find_remote_link_by_ip(struct virt_network *net,
const struct in_addr *pub_ip)
{
struct virt_hash_table *table = &net->link_table;
u32 hash = hash_32(pub_ip->s_addr, table->bits);
struct virt_hash_head *head = &table->head[hash];
struct remote_link *link;
struct hlist_node *pos;
if(WARN_ON(hash >= table->size))
return NULL;
rcu_read_lock();
hlist_for_each_entry_rcu(link, pos, &head->list, hlist) {
if(pub_ip->s_addr == link->rif.ip4) {
remote_link_hold(link);
rcu_read_unlock();
return link;
}
}
rcu_read_unlock();
return NULL;
}
/*
* Lookup a link by its public IP address and port.
*
* Increments reference count before returning the link.
*/
struct remote_link *find_remote_link_by_addr(struct virt_network *net,
const struct in_addr *addr, __be16 port)
{
struct virt_hash_table *table = &net->link_table;
u32 hash = hash_32(addr->s_addr, table->bits);
struct virt_hash_head *head = &table->head[hash];
struct remote_link *link;
struct hlist_node *pos;
if(WARN_ON(hash >= table->size))
return NULL;
rcu_read_lock();
hlist_for_each_entry_rcu(link, pos, &head->list, hlist) {
if(addr->s_addr == link->rif.ip4 && port == link->rif.data_port) {
remote_link_hold(link);
rcu_read_unlock();
return link;
}
}
rcu_read_unlock();
return NULL;
}
/*
* Lookup a link by the node and link indices.
*
* Increments reference count before returning the link.
*/
struct remote_link *find_remote_link_by_indices(struct virt_network *net,
int rnode_index, int rlink_index)
{
struct remote_node *node;
rcu_read_lock();
node = __find_remote_node_by_index(net, rnode_index);
if(node) {
struct remote_link *link;
list_for_each_entry_rcu(link, &node->links, rif.list) {
if(link->index == rlink_index) {
remote_link_hold(link);
rcu_read_unlock();
return link;
}
}
}
rcu_read_unlock();
return NULL;
}
/*
* Find the maximum priority among a remote_node's links.
*/
int find_max_remote_link_prio(const struct remote_node *node)
{
int max_prio = MIN_USABLE_DEVICE_PRIORITY;
const struct remote_link *link;
rcu_read_lock();
list_for_each_entry_rcu(link, &node->links, rif.list) {
if(link->rif.prio > max_prio &&
(link->rif.active_paths > 0 || link->rif.stalled_paths <= 0))
max_prio = link->rif.prio;
}
rcu_read_unlock();
return max_prio;
}
int dump_remote_node_list(struct seq_file *s, void *p)
{
const struct virt_priv *virt = s->private;
const struct virt_hash_table *remote_nodes = &virt->network.node_table;
int i;
// xxxxxxxx xxxxx xxxx xxxxxxxx xxxxxxxx xxxxxx
seq_printf(s, "privaddr links mpri txqueue qlimit refcnt\n");
rcu_read_lock();
for(i = 0; i < remote_nodes->size; i++) {
const struct virt_hash_head *head = &remote_nodes->head[i];
const struct remote_node *node;
struct hlist_node *pos;
hlist_for_each_entry_rcu(node, pos, &head->list, hlist) {
seq_printf(s, "%08x %5u %4d %8u %8u %6u\n",
node->priv_ip.s_addr,
node->link_count,
node->max_link_prio,
skb_queue_len(&node->tx_queue),
node->tx_queue_limit,
atomic_read(&node->refcnt));
}
}
rcu_read_unlock();
return 0;
}
int dump_remote_link_list(struct seq_file *s, void *p)
{
const struct virt_priv *virt = s->private;
const struct virt_hash_table *remote_links = &virt->network.link_table;
int i;
// xxxxxxxx xxxxxxxx xxxx xxxxx xxxx xxxxxx xxxxxxxxx xxxxx xxxxxx
seq_printf(s, "privaddr pubaddr port flags prio flows bandwidth paths refcnt\n");
rcu_read_lock();
for(i = 0; i < remote_links->size; i++) {
const struct virt_hash_head *head = &remote_links->head[i];
const struct remote_link *link;
struct hlist_node *pos;
hlist_for_each_entry_rcu(link, pos, &head->list, hlist) {
const struct remote_node *node = link->node;
seq_printf(s, "%08x %08x %04x %05x %4d %6ld %9ld %5u %6u\n",
node->priv_ip.s_addr,
link->rif.ip4,
link->rif.data_port,
link->flags,
link->rif.prio,
link->rif.flow_count,
link->rif.bandwidth_hint,
link->rif.active_paths,
atomic_read(&link->refcnt));
}
}
rcu_read_unlock();
return 0;
}
int dump_reorder_stats(struct seq_file *s, void *p)
{
const struct virt_priv *virt = s->private;
const struct virt_hash_table *remote_nodes = &virt->network.node_table;
int i;
// xxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx
seq_printf(s, "privaddr forwarded received dropped in_order early late recovered max_delay avg_delay\n");
rcu_read_lock();
for(i = 0; i < remote_nodes->size; i++) {
const struct virt_hash_head *head = &remote_nodes->head[i];
const struct remote_node *node;
struct hlist_node *pos;
hlist_for_each_entry_rcu(node, pos, &head->list, hlist) {
const struct reorder_stats *stats = &node->reorder_head.stats;
seq_printf(s, "%08x %10ld %10ld %10ld %10ld %10ld %10ld %10ld %10u %10u\n",
node->priv_ip.s_addr,
stats->forwarded,
stats->received,
stats->dropped,
stats->in_order,
stats->early,
stats->late,
stats->recovered,
jiffies_to_usecs(stats->max_delay),
jiffies_to_usecs(stats->avg_delay));
}
}
rcu_read_unlock();
return 0;
}
void remote_node_list_destroy(struct virt_network *net)
{
int i;
rcu_read_lock();
for(i = 0; i < net->node_table.size; i++) {
struct virt_hash_head *head = &net->node_table.head[i];
struct remote_node *node;
struct hlist_node *pos;
hlist_for_each_entry_rcu(node, pos, &head->list, hlist) {
delete_remote_node(net, node);
}
}
rcu_read_unlock();
}