-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvirtSelectInterface.c
737 lines (601 loc) · 21.4 KB
/
virtSelectInterface.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*
* 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/random.h>
#include <linux/module.h>
#include "virt.h"
#include "virtSelectInterface.h"
#include "virtPassive.h"
#include "virtNetwork.h"
#include "virtDevList.h"
#include "virtEgressLookup.h"
#include "virtPolicy.h"
#include "virtPolicyTypes.h"
// TODO: This guy needs a lock associated with him.
static struct list_head virt_alg_list = LIST_HEAD_INIT(virt_alg_list);
/*
* Register an interface selection algorithm, so that policies may specify them
* by name.
*
* TODO: Locking!
* TODO: Support algorithms loaded as separate kernel modules.
*/
int virt_register_alg(struct virt_alg *alg)
{
struct virt_alg *curr;
if(strnlen(alg->name, MAX_ALG_NAME_LEN) >= MAX_ALG_NAME_LEN)
return -E2BIG;
list_for_each_entry(curr, &virt_alg_list, alg_list) {
if(strcmp(alg->name, curr->name) == 0)
return -EEXIST;
}
list_add_tail(&alg->alg_list, &virt_alg_list);
return 0;
}
/*
* Unregister an interface selection algorithm.
*
* Currently, it is unnecessary to call this when the main virt mode exits,
* since nothing needs to be freed. This is intended as a placeholder for
* algorithms loaded as external modules.
*
* TODO: Add refcnt, the owner of this algorithm cannot disappear until all
* references to it are cleaned up.
*/
int virt_unregister_alg(struct virt_alg *alg)
{
list_del(&alg->alg_list);
return 0;
}
/*
* Find an interface selection algorithm by name.
*/
struct virt_alg *virt_alg_get_by_name(const char *name)
{
struct virt_alg *curr;
list_for_each_entry(curr, &virt_alg_list, alg_list) {
if(strcmp(name, curr->name) == 0)
return curr;
}
return NULL;
}
/*
* For single-path flows, if the current path is still available, then this is
* used to determine whether it would still be acceptable to migrate the flow.
* The goal is to allow the link selection algorithm opportunities to
* re-evaluate the link assignment, while minimizing the the out-of-order
* deliveries that can result.
*/
static int can_reassign(struct virt_network *net,
const struct policy_entry *policy,
const struct flow_stats *flow_stats)
{
int action;
struct pathinfo *last_path;
unsigned long pkt_spacing;
unsigned long safe_spacing;
unsigned long now = jiffies;
/* Reassignment is only allowed if the action is ENCAP. */
action = POLICY_ACTION(policy->action);
if(action != POLICY_ACT_ENCAP)
return 0;
last_path = path_get_by_addr(net,
&flow_stats->local_addr, &flow_stats->remote_addr,
flow_stats->local_port, flow_stats->remote_port);
if(!last_path)
return 1;
/* Test if the spacing between the last packet and the current packet is
* sufficiently large that we can reassign the flow without causing an out
* of order packet. */
safe_spacing = usecs_to_jiffies(pathinfo_safe_spacing(last_path));
/* Done with last_path, decrement refcnt. */
virt_path_put(last_path);
pkt_spacing = (long)now - (long)flow_stats->last_send_jiffies;
if(pkt_spacing >= safe_spacing)
return 1;
/* Test if the flow has not been reassigned too recently.
*
* TODO: We should also check whether the tunnel endpoint supports
* resequencing and/or make sure it is enabled for this flow. */
if((unsigned long)((long)now - (long)flow_stats->last_path_change) >=
get_min_reassign_delay_jiffies()) {
return 1;
}
return 0;
}
/*
* This is the main local interface selection function.
*/
struct device_node *select_local_interface(struct virt_priv *virt, struct flow_table_entry *flow,
const struct remote_node *dest, const struct list_head *interfaces)
{
struct device_node *sel_iface = NULL;
struct device_node *last_iface = NULL;
if(WARN_ON(!flow->flow_stats))
return NULL;
if(WARN_ON(!flow->policy))
return NULL;
if(WARN_ON(!flow->policy->alg))
return NULL;
if(unlikely(list_empty(interfaces)))
return NULL;
last_iface = slave_get_by_addr(&flow->flow_stats->local_addr);
if(last_iface) {
if(!can_reassign(&virt->network, flow->policy, flow->flow_stats))
return last_iface;
device_node_put(last_iface);
if(!WARN_ON(last_iface->lif.flow_count <= 0))
last_iface->lif.flow_count--;
}
sel_iface = flow->policy->alg->sel_local(virt, flow, dest, interfaces);
/* Set the last path here for NAT flows because NAT flows will
* not pass through select_remote_interface. */
if(sel_iface && POLICY_ACTION(flow->policy->action) == POLICY_ACT_NAT) {
flow->flow_stats->local_addr.s_addr = sel_iface->lif.ip4;
flow->flow_stats->local_port = sel_iface->lif.data_port;
}
if(sel_iface)
sel_iface->lif.flow_count++;
return sel_iface;
}
static void set_last_path(struct flow_stats *flow_stats, struct pathinfo *path)
{
if(flow_stats->local_addr.s_addr != path->local_addr.ip4.s_addr ||
flow_stats->remote_addr.s_addr != path->remote_addr.ip4.s_addr ||
flow_stats->local_port != path->local_port ||
flow_stats->remote_port != path->remote_port) {
flow_stats->last_path_change = jiffies;
flow_stats->local_addr.s_addr = path->local_addr.ip4.s_addr;
flow_stats->remote_addr.s_addr = path->remote_addr.ip4.s_addr;
flow_stats->local_port = path->local_port;
flow_stats->remote_port = path->remote_port;
}
}
static void set_last_path_fallback(struct flow_stats *flow_stats,
const struct device_node *llink,
const struct remote_link *rlink)
{
if(flow_stats->local_addr.s_addr != llink->lif.ip4 ||
flow_stats->remote_addr.s_addr != rlink->rif.ip4 ||
flow_stats->local_port != llink->lif.data_port ||
flow_stats->remote_port != rlink->rif.data_port) {
flow_stats->last_path_change = jiffies;
flow_stats->local_addr.s_addr = llink->lif.ip4;
flow_stats->remote_addr.s_addr = rlink->rif.ip4;
flow_stats->local_port = llink->lif.data_port;
flow_stats->remote_port = rlink->rif.data_port;
}
}
/*
* This is the main remote interface selection function.
*
* Note that the returned remote_link object will have its reference count
* incremented. The caller must use remote_link_put when finished with it.
*/
struct remote_link *select_remote_interface(struct virt_priv *virt, struct flow_table_entry *flow,
const struct device_node *src, const struct remote_node *dest,
const struct list_head *interfaces)
{
struct remote_link *sel_iface = NULL;
struct remote_link *last_iface = NULL;
if(WARN_ON(!flow->flow_stats))
return NULL;
if(WARN_ON(!flow->policy))
return NULL;
if(WARN_ON(!flow->policy->alg))
return NULL;
if(unlikely(list_empty(interfaces)))
return NULL;
last_iface = find_remote_link_by_addr(&virt->network,
&flow->flow_stats->remote_addr, flow->flow_stats->remote_port);
if(last_iface) {
if(!can_reassign(&virt->network, flow->policy, flow->flow_stats))
return last_iface;
if(!WARN_ON(last_iface->rif.flow_count <= 0))
last_iface->rif.flow_count--;
remote_link_put(last_iface);
}
sel_iface = flow->policy->alg->sel_remote(virt, flow, src, dest, interfaces);
if(sel_iface) {
struct pathinfo *path;
path = lookup_pathinfo(&virt->network, src, sel_iface);
if(path) {
path->last_packet = jiffies;
set_last_path(flow->flow_stats, path);
virt_path_put(path);
} else {
/* TODO: Clean up this section of code. We have the fallback
* function here because setting the last path is important for
* counting the number of flows bound to each link. However,
* lookup_pathinfo may return NULL for the first flow sent on a new
* path. */
set_last_path_fallback(flow->flow_stats, src, sel_iface);
}
sel_iface->rif.flow_count++;
}
return sel_iface;
}
/*
* Test if interface is usable.
*
* 1. iface has at least one active path or zero stalled paths.
* 2. Its priority is at least min_prio.
* 3. (if a local device) the DEVICE_NO_TX is not set.
*/
static bool interface_is_usable(const struct interface *iface, int min_prio)
{
if(iface->type == INTERFACE_LOCAL) {
struct device_node *device = container_of(iface, struct device_node, lif);
if(device->flags & DEVICE_NO_TX)
return 0;
}
return (iface->prio >= min_prio &&
(iface->active_paths > 0 || iface->stalled_paths <= 0));
}
struct interface *first_sel_common(const struct list_head *interfaces, int min_prio)
{
struct interface *ife;
list_for_each_entry_rcu(ife, interfaces, list) {
if(interface_is_usable(ife, min_prio))
return ife;
}
return NULL;
}
struct device_node *first_sel_local(struct virt_priv *virt, struct flow_table_entry *flow,
const struct remote_node *dest, const struct list_head *interfaces)
{
struct interface *iface = first_sel_common(interfaces, virt->max_dev_prio);
if(iface) {
struct device_node *dev_node = container_of(iface, struct device_node, lif);
device_node_hold(dev_node);
return dev_node;
} else {
return NULL;
}
}
struct remote_link *first_sel_remote(struct virt_priv *virt, struct flow_table_entry *flow,
const struct device_node *src, const struct remote_node *dest,
const struct list_head *interfaces)
{
struct remote_link *link = NULL;
struct interface *iface;
rcu_read_lock();
iface = first_sel_common(interfaces, dest->max_link_prio);
if(iface) {
link = container_of(iface, struct remote_link, rif);
remote_link_hold(link);
}
rcu_read_unlock();
return link;
}
static struct virt_alg first_alg = {
.name = "first",
.module = THIS_MODULE,
.sel_local = first_sel_local,
.sel_remote = first_sel_remote,
.alg_list = LIST_HEAD_INIT(first_alg.alg_list),
};
struct interface *random_sel_common(const struct list_head *interfaces, int min_prio)
{
unsigned num_ifaces = 0;
unsigned selected;
struct interface *iface;
list_for_each_entry_rcu(iface, interfaces, list) {
if(interface_is_usable(iface, min_prio))
num_ifaces++;
}
if(num_ifaces == 0)
return NULL;
selected = random32() % num_ifaces;
num_ifaces = 0;
list_for_each_entry_rcu(iface, interfaces, list) {
if(interface_is_usable(iface, min_prio)) {
if(num_ifaces >= selected)
break;
else
num_ifaces++;
}
}
return iface;
}
struct device_node *random_sel_local(struct virt_priv *virt, struct flow_table_entry *flow,
const struct remote_node *dest, const struct list_head *interfaces)
{
struct interface *iface = random_sel_common(interfaces, virt->max_dev_prio);
if(iface) {
struct device_node *dev_node = container_of(iface, struct device_node, lif);
device_node_hold(dev_node);
return dev_node;
} else {
return NULL;
}
}
struct remote_link *random_sel_remote(struct virt_priv *virt, struct flow_table_entry *flow,
const struct device_node *src, const struct remote_node *dest,
const struct list_head *interfaces)
{
struct remote_link *link = NULL;
struct interface *iface;
rcu_read_lock();
iface = random_sel_common(interfaces, dest->max_link_prio);
if(iface) {
link = container_of(iface, struct remote_link, rif);
remote_link_hold(link);
}
rcu_read_unlock();
return link;
}
static struct virt_alg random_alg = {
.name = "random",
.module = THIS_MODULE,
.sel_local = random_sel_local,
.sel_remote = random_sel_remote,
.alg_list = LIST_HEAD_INIT(random_alg.alg_list),
};
struct device_node *dynamic_sel_local(struct virt_priv *virt, struct flow_table_entry *flow,
const struct remote_node *dest, const struct list_head *interfaces)
{
struct interface *curr_ife;
struct interface *best_ife = NULL;
unsigned long min_delay = ULONG_MAX;
if(WARN_ON(!dest))
return NULL;
if(unlikely(list_empty(interfaces)))
return NULL;
rcu_read_lock();
list_for_each_entry(curr_ife, interfaces, list) {
if(interface_is_usable(curr_ife, virt->max_dev_prio)) {
struct device_node *dev_node = container_of(curr_ife, struct device_node, lif);
struct remote_link *dest_link;
list_for_each_entry_rcu(dest_link, &dest->links, rif.list) {
struct pathinfo *path;
unsigned long path_delay;
path = lookup_pathinfo(&virt->network, dev_node, dest_link);
/* Returns some value, even if path is null. */
path_delay = pathinfo_est_delay(path);
if(path)
virt_path_put(path);
if(path_delay < min_delay) {
min_delay = path_delay;
best_ife = curr_ife;
}
}
}
}
rcu_read_unlock();
if(best_ife) {
struct device_node *dev_node = container_of(best_ife, struct device_node, lif);
device_node_hold(dev_node);
return dev_node;
} else {
return NULL;
}
}
struct remote_link *dynamic_sel_remote(struct virt_priv *virt, struct flow_table_entry *flow,
const struct device_node *src, const struct remote_node *dest,
const struct list_head *interfaces)
{
struct remote_link *best_ife = NULL;
unsigned long min_delay = ULONG_MAX;
struct remote_link *dest_link;
if(WARN_ON(!dest))
return NULL;
if(unlikely(list_empty(interfaces)))
return NULL;
rcu_read_lock();
list_for_each_entry_rcu(dest_link, &dest->links, rif.list) {
if(interface_is_usable(&dest_link->rif, dest->max_link_prio)) {
struct pathinfo *path;
unsigned long path_delay;
path = lookup_pathinfo(&virt->network, src, dest_link);
/* Returns some value, even if path is null. */
path_delay = pathinfo_est_delay(path);
if(path)
virt_path_put(path);
if(path_delay < min_delay) {
min_delay = path_delay;
best_ife = dest_link;
}
}
}
if(best_ife)
remote_link_hold(best_ife);
rcu_read_unlock();
return best_ife;
}
static struct virt_alg dynamic_alg = {
.name = "dynamic",
.module = THIS_MODULE,
.sel_local = dynamic_sel_local,
.sel_remote = dynamic_sel_remote,
.alg_list = LIST_HEAD_INIT(dynamic_alg.alg_list),
};
static struct interface *wrr_sel_common(const struct list_head *interfaces,
int min_prio)
{
struct interface *ife;
long min_flows = LONG_MAX;
long sel_bandwidth = LONG_MIN;
long max_reserve = LONG_MIN;
struct interface *sel_ife = NULL;
bool use_min_flows = false;
/* Find the maximum constant by which we can multiply all of the weights
* without exceeding LONG_MAX. We will then multiply by that constant so
* that the subsequent integer division will have greater precision. */
long wmult = LONG_MAX;
list_for_each_entry_rcu(ife, interfaces, list) {
if(interface_is_usable(ife, min_prio)) {
if(ife->flow_count < min_flows || (ife->flow_count == min_flows &&
ife->bandwidth_hint > sel_bandwidth)) {
min_flows = ife->flow_count;
sel_bandwidth = ife->bandwidth_hint;
sel_ife = ife;
}
if(ife->bandwidth_hint > 0) {
long cand = LONG_MAX / ife->bandwidth_hint;
if(cand < wmult)
wmult = cand;
} else {
use_min_flows = true;
}
}
}
/* If any of the eligible interfaces had an undefined bandwidth hint, then
* we revert to round-robin based on flow_count. */
if(use_min_flows)
goto out;
list_for_each_entry_rcu(ife, interfaces, list) {
if(interface_is_usable(ife, min_prio)) {
long reserve = (wmult * ife->bandwidth_hint) / (ife->flow_count + 1);
if(reserve > max_reserve || (reserve == max_reserve &&
ife->bandwidth_hint > sel_bandwidth)) {
max_reserve = reserve;
sel_bandwidth = ife->bandwidth_hint;
sel_ife = ife;
}
}
}
out:
return sel_ife;
}
struct device_node *wrr_sel_local(struct virt_priv *virt, struct flow_table_entry *flow,
const struct remote_node *dest, const struct list_head *interfaces)
{
struct interface *iface = wrr_sel_common(interfaces,
virt->max_dev_prio);
if(iface) {
struct device_node *dev_node = container_of(iface, struct device_node, lif);
device_node_hold(dev_node);
return dev_node;
} else {
return NULL;
}
}
struct remote_link *wrr_sel_remote(struct virt_priv *virt, struct flow_table_entry *flow,
const struct device_node *src, const struct remote_node *dest,
const struct list_head *interfaces)
{
struct remote_link *link = NULL;
struct interface *iface;
rcu_read_lock();
iface = wrr_sel_common(interfaces, dest->max_link_prio);
if(iface) {
link = container_of(iface, struct remote_link, rif);
remote_link_hold(link);
}
rcu_read_unlock();
return link;
}
static struct virt_alg wrr_alg = {
.name = "wrr",
.module = THIS_MODULE,
.sel_local = wrr_sel_local,
.sel_remote = wrr_sel_remote,
.alg_list = LIST_HEAD_INIT(wrr_alg.alg_list),
};
static struct interface *rr_sel_common(const struct list_head *interfaces,
int min_prio, unsigned int counter)
{
unsigned num_ifaces = 0;
unsigned selected;
struct interface *iface;
list_for_each_entry_rcu(iface, interfaces, list) {
if(interface_is_usable(iface, min_prio))
num_ifaces++;
}
if(num_ifaces == 0)
return NULL;
selected = counter % num_ifaces;
num_ifaces = 0;
list_for_each_entry_rcu(iface, interfaces, list) {
if(interface_is_usable(iface, min_prio)) {
if(num_ifaces >= selected)
break;
else
num_ifaces++;
}
}
return iface;
}
struct device_node *rr_sel_local(struct virt_priv *virt, struct flow_table_entry *flow,
const struct remote_node *dest, const struct list_head *interfaces)
{
static unsigned int counter = 0;
struct interface *iface = rr_sel_common(interfaces,
virt->max_dev_prio, counter++);
if(iface) {
struct device_node *dev_node = container_of(iface, struct device_node, lif);
device_node_hold(dev_node);
return dev_node;
} else {
return NULL;
}
}
struct remote_link *rr_sel_remote(struct virt_priv *virt, struct flow_table_entry *flow,
const struct device_node *src, const struct remote_node *dest,
const struct list_head *interfaces)
{
static unsigned int counter = 0;
struct remote_link *link = NULL;
struct interface *iface;
rcu_read_lock();
iface = rr_sel_common(interfaces, dest->max_link_prio, counter++);
if(iface) {
link = container_of(iface, struct remote_link, rif);
remote_link_hold(link);
}
rcu_read_unlock();
return link;
}
static struct virt_alg rr_alg = {
.name = "rr",
.module = THIS_MODULE,
.sel_local = rr_sel_local,
.sel_remote = rr_sel_remote,
.alg_list = LIST_HEAD_INIT(rr_alg.alg_list),
};
void path_release_flow(struct virt_network *net, struct flow_stats *flow)
{
struct device_node *local_if;
struct remote_link *remote_if;
local_if = slave_get_by_addr(&flow->local_addr);
if(local_if) {
if(!WARN_ON(local_if->lif.flow_count <= 0))
local_if->lif.flow_count--;
device_node_put(local_if);
}
remote_if = find_remote_link_by_addr(net, &flow->remote_addr, flow->remote_port);
if(remote_if) {
if(!WARN_ON(remote_if->rif.flow_count <= 0))
remote_if->rif.flow_count--;
remote_link_put(remote_if);
}
memset(&flow->local_addr, 0, sizeof(flow->local_addr));
memset(&flow->remote_addr, 0, sizeof(flow->remote_addr));
}
/*
* Register all of the internal algorithms.
*/
int virt_register_algorithms(void)
{
virt_register_alg(&first_alg);
virt_register_alg(&random_alg);
virt_register_alg(&dynamic_alg);
virt_register_alg(&wrr_alg);
virt_register_alg(&rr_alg);
return 0;
}