forked from UW-WiNGS/virtnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtEgress.c
1109 lines (888 loc) · 32.9 KB
/
virtEgress.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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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/interrupt.h> /* mark_bh */
#include <linux/in.h>
#include <linux/netdevice.h> /* struct device, and other headers */
#include <linux/etherdevice.h> /* eth_type_trans */
#include <linux/inetdevice.h> /* struct in_device, __in_dev_get */
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <linux/ip.h> /* struct iphdr */
#include <linux/tcp.h> /* struct tcphdr */
#include <linux/udp.h> /* struct udphdr */
#include <linux/skbuff.h>
#include <net/arp.h>
#include <net/ip.h>
#include <net/route.h>
#include <linux/in6.h>
#include <asm/checksum.h>
#include "virt.h"
#include "virtStats.h"
#include "virtParse.h"
#include "virtDebug.h"
#include "virtPolicy.h"
#include "virtHeader.h"
#include "virtEgress.h"
#include "virtDevList.h"
#include "virtPolicyTypes.h"
#include "virtEgressLookup.h"
#include "virtSelectInterface.h"
#include "virtFlowTable.h"
#include "virtNetwork.h"
#include "virtNAT.h"
#include "virtCoding.h"
#include "virtRetransmission.h"
// TODO: move all handle_xxxproto to virtEgressParse.c/.h also move flow_info struct there
unsigned int send_arp(unsigned int hooknum, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
struct arphdr *arp = NULL;
char *arp_data = NULL;
char sender_hw[ETH_ALEN];
__be32 sender_ip, target_ip;
if( !skb )
return NF_ACCEPT;
if(!is_virt_interface(out->name))
return NF_ACCEPT;
arp = arp_hdr(skb);
switch( ntohs(arp->ar_op) )
{
case ARPOP_REPLY:
return NF_DROP;
case ARPOP_REQUEST: //TODO: we need to reply to these
VIRT_DBG(" got an ARP_REQUEST\n");
arp_data = ((char *)arp + sizeof(struct arphdr));
memcpy(&sender_hw, arp_data, arp->ar_hln);
arp_data += arp->ar_hln;
memcpy(&sender_ip, arp_data, arp->ar_pln);
arp_data += arp->ar_pln;
arp_data += arp->ar_hln; //skip target mac cause it is blank
memcpy(&target_ip, arp_data, arp->ar_pln);
VIRT_DBG("sending a arp reply to: 0x%x sender: 0x%x sender_hw: 0x%02x:%02x:%02x:%02x:%02x:%02x\n", target_ip, sender_ip,
sender_hw[0], sender_hw[1], sender_hw[2], sender_hw[3], sender_hw[4], sender_hw[5]);
sender_hw[5] = 0x88;
default:
return NF_DROP;
}
return NF_DROP;
}
/*
* Try to send as many packets as possible in response to newly available
* capacity on the path.
*
* Returns number of queued packets sent.
*/
int virt_try_send_queued(struct virt_priv *virt, struct device_node *slave,
struct remote_link *link)
{
struct sk_buff_head *tx_queue = &link->node->tx_queue;
struct pathinfo *path = path_lookup_create(&virt->network, slave, link);
int free_packets = 0;
int sent = 0;
unsigned long now = jiffies;
if(!path)
return 0;
/* If path has been quiet (no ACKs for a multiple of its RTT), then
* send one packet for free to help prevent stalling. */
if(afterl(now, path->stall_time)) {
unsigned long rtt = pathinfo_est_rtt(path);
path->stall_time = now + usecs_to_jiffies(rtt << PATH_STALL_RTT_MULT);
free_packets = 1;
}
while(!skb_queue_empty(tx_queue)) {
struct sk_buff *skb;
unsigned payload_len;
spin_lock_bh(&tx_queue->lock);
skb = skb_peek(tx_queue);
/* Need to check result of skb_peek due to race condition between
* checking skb_queue_empty and taking the lock. */
if(!skb) {
spin_unlock_bh(&tx_queue->lock);
break;
}
/* Tunnel header was added before queuing the sk_buff. */
payload_len = skb->len - sizeof(struct tunhdr);
if(free_packets > 0) {
path->avail += payload_len;
free_packets--;
}
if(payload_len <= path->avail) {
struct net_device *slave_dev = slave->dev;
struct tunhdr *tunhdr = (struct tunhdr *)skb->data;
struct virt_skb_cb *skb_cb = (struct virt_skb_cb *)skb->cb;
struct flow_table_entry *flow = skb_cb->flow;
__skb_unlink(skb, tx_queue);
spin_unlock_bh(&tx_queue->lock);
virt_finish_tunhdr(tunhdr, path, skb_cb->flow, link->node);
virt_build_udp_header(skb, flow->rx_port, link->rif.data_port);
virt_build_ip_header(skb, slave->lif.ip4, link->rif.ip4);
flow_table_entry_put(skb_cb->flow);
path->avail -= payload_len;
path->snd_nxt += payload_len;
skb->dev = slave_dev;
skb->protocol = htons(ETH_P_IP);
dev_hard_header(skb, slave_dev, ETH_P_IP, slave->next_hop_addr,
slave_dev->dev_addr, skb->len);
skb_reset_mac_header(skb);
/* Update link statistics -- these may not be accurate if the packet
* gets dropped after dev_queue_xmit. */
slave->stats.tx_packets++;
slave->stats.tx_bytes += skb->len;
/* Update device statistics. */
virt->stats.tx_packets++;
virt->stats.tx_bytes += skb->len;
path_update_tx_bytes(path, skb->len);
dev_queue_xmit(skb);
sent++;
} else {
spin_unlock_bh(&tx_queue->lock);
break;
}
}
virt_path_put(path);
return sent;
}
int virt_send_ack(struct virt_priv *virt, struct device_node *slave,
struct remote_link *link)
{
struct sk_buff *skb;
struct net_device *dev = slave->dev;
struct tunhdr *tunhdr;
struct pathinfo *path;
__be16 sport;
unsigned alloc_size = sizeof(struct tunhdr) + sizeof(struct udphdr) +
sizeof(struct iphdr) + LL_RESERVED_SPACE(dev);
path = lookup_pathinfo(&virt->network, slave, link);
if(!path)
return -EINVAL;
skb = alloc_skb(alloc_size, GFP_ATOMIC);
if(!skb) {
virt_path_put(path);
return -ENOMEM;
}
skb_reserve(skb, alloc_size);
tunhdr = virt_build_tunhdr(skb, NULL, NULL);
virt_finish_tunhdr(tunhdr, path, NULL, link->node);
/* TODO: We may want to split traffic among different ports, which
* may change how we send ACKs. For now, everything uses the same
* source port. */
sport = htons(virt_tunnel_source_port());
virt_build_udp_header(skb, sport, link->rif.data_port);
virt_build_ip_header(skb, slave->lif.ip4, link->rif.ip4);
skb->dev = dev;
skb->protocol = htons(ETH_P_IP);
dev_hard_header(skb, dev, ETH_P_IP, slave->next_hop_addr, dev->dev_addr, skb->len);
skb_reset_mac_header(skb);
/* Update link statistics -- these may not be accurate if the packet gets
* dropped after dev_queue_xmit. */
slave->stats.tx_packets++;
slave->stats.tx_bytes += skb->len;
/* Update device statistics. */
virt->stats.tx_packets++;
virt->stats.tx_bytes += skb->len;
/* Decrement refcnt. */
virt_path_put(path);
dev_queue_xmit(skb);
return 0;
}
/*
* Transmit a packet (called by the kernel)
*/
int virt_tx(struct sk_buff *skb, struct net_device *dev)
{
int rtn = 0;
struct virt_priv *priv = netdev_priv(dev);
struct device_node *slave = NULL;
int payload_len;
struct packet *pkt = NULL;
#ifndef VIRT_USE_RTABLE
struct net_device *out_dev = NULL;
#endif
//VIRT_DBG("in virt_tx...\n");
if( use_timing() )
log_timing(TIMING_TX_START);
// if no slaves default off => drop packet
if(get_slave_list_head() == NULL)
goto drop;
/* For flow byte counts, we are interested in network layer and above. */
payload_len = skb->len;
dev->trans_start = jiffies; /* save the timestamp */
if( use_timing() )
log_timing(TIMING_TX_SETUP);
//malloc packet struct and flow struct
pkt = kmalloc(sizeof(struct packet), GFP_ATOMIC);
if(!pkt)
goto drop;
memset(pkt, 0, sizeof(struct packet));
pkt->key = kmalloc(sizeof(struct flow_tuple), GFP_ATOMIC);
if(!pkt->key)
goto drop_and_free;
memset(pkt->key, 0, sizeof(struct flow_tuple));
pkt->hdr_ptrs = kmalloc(sizeof(struct hdr_ptrs), GFP_ATOMIC);
if(!pkt->hdr_ptrs)
goto drop_and_free;
memset(pkt->hdr_ptrs, 0, sizeof(struct hdr_ptrs));
pkt->master = dev;
pkt->skb = skb;
if( use_timing() )
log_timing(TIMING_TX_PARSE);
rtn = virt_parse_egress_pkt(pkt);
if( ntohs(pkt->key->net_proto) == ETH_P_ARP )
goto drop_and_free;
if( use_timing() )
log_timing(TIMING_TX_LOOKUP);
// lookup flow route info
if(virt_egress_lookup_flow(priv, pkt) < 0)
goto drop_and_free;
/* Update policy hit statistics. */
if(pkt->policy) {
struct policy_stats *stats = &pkt->policy->stats;
stats->tx_packets++;
stats->tx_bytes += skb->len;
}
// route the packet
if( use_timing() )
log_timing(TIMING_TX_MANGLE);
switch(POLICY_ACTION(pkt->policy->action)) {
case POLICY_ACT_PASS:
slave = select_local_interface(priv, pkt->ftable_entry, NULL, get_slave_list_head());
if(!slave)
goto drop_and_free;
VIRT_DBG("egress PASS this packet\n");
/* TODO: It would be nice if PASSed packets would still go through
* netfilter, especially the post-routing hook, so that we could
* make use of netfilter's NAT function. However, the netfilter
* hooks rely on routing information being filled in, so we would
* have to play along with the routing code.
*
* if(nf_hook(NFPROTO_IPV4, NF_INET_POST_ROUTING, skb, NULL,
* slave->dev, dev_queue_xmit) == NF_ACCEPT)
* dev_queue_xmit(skb);
*/
break;
case POLICY_ACT_LISP:
case POLICY_ACT_NAT:
slave = select_local_interface(priv, pkt->ftable_entry, NULL, get_slave_list_head());
if(!slave)
goto drop_and_free;
VIRT_DBG("egress NAT this packet\n");
virt_nat_egress_pkt(pkt, slave);
break;
case POLICY_ACT_ENCAP:
{
struct remote_node *rnode = pkt->ftable_entry->rnode;
if(!rnode) {
rnode = select_remote_node(priv, pkt);
if(!rnode) {
VIRT_DBG("routing packet failed\n");
goto drop_and_free;
}
pkt->ftable_entry->rnode = rnode;
}
virt_queue_tx(priv, pkt, rnode);
return NETDEV_TX_OK;
}
case POLICY_ACT_DROP:
VIRT_DBG("egress DROP this packet\n");
goto drop_and_free;
default:
// unknown route policy
VIRT_DBG("egress ERROR: unknown policy action (%x)\n", pkt->policy->action);
}
// update flow statistics
pkt->flow_stats->tx_packets++;
pkt->flow_stats->tx_bytes += payload_len;
pkt->flow_stats->last_tx_dev = slave->dev->ifindex;
// update device statistics
priv->stats.tx_packets++;
priv->stats.tx_bytes += skb->len;
// update link statistics
slave->stats.tx_packets++;
slave->stats.tx_bytes += skb->len;
VIRT_DBG("transmitting the packet...\n\n");
if( use_timing() )
log_timing(TIMING_TX_END);
#ifdef VIRT_USE_RTABLE
uint32_t gw_ip = 0x020FA8C0;
struct rtable *rt = skb_rtable(skb);
struct iphdr *ip = ip_hdr(skb);
VIRT_DBG("routing %x->%x (%s)\n", ntohl(ip->saddr), ntohl(ip->daddr),
slave->dev->name);
struct flowi fl = {
// .oif = pkt->slave->dev->iflink,
.oif = slave->dev->ifindex,
.nl_u = {
.ip4_u = {
.daddr = ip->daddr,
.saddr = ip->saddr,
.tos = 0,
},
},
.proto = IPPROTO_IP,
};
skb_dst_drop(skb);
if(ip_route_output_key(dev_net(slave->dev), &rt, &fl)) {
VIRT_DBG("ip_route_output_key failed\n");
} else {
VIRT_DBG("route: %s, neigh: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx, state: %s\n",
rt->dst.dev->name,
rt->dst.neighbour->ha[0], rt->dst.neighbour->ha[1], rt->dst.neighbour->ha[2],
rt->dst.neighbour->ha[3], rt->dst.neighbour->ha[4], rt->dst.neighbour->ha[5],
rt->dst.neighbour->nud_state & NUD_VALID ? "valid" : "invalid");
if(!(rt->dst.neighbour->nud_state & NUD_VALID)) {
neigh_release(rt->dst.neighbour);
rt->dst.neighbour = neigh_create(&arp_tbl, &gw_ip, slave->dev);
rt->rt_gateway = gw_ip;
}
}
skb_dst_set(skb, &rt->dst);
VIRT_DBG("routing done %x->%x (%s)\n", ntohl(ip->saddr), ntohl(ip->daddr),
slave->dev->name);
#else //!VIRT_USE_RTABLE
out_dev = slave->dev;
dev_hard_header(skb, out_dev, ETH_P_IP,
slave->next_hop_addr,
out_dev->dev_addr, skb->len);
skb_reset_mac_header(skb);
#endif
pkt->flow_stats->last_send_jiffies = jiffies;
#ifdef VIRT_USE_RTABLE
ip_local_out(skb);
#else
skb->dev = slave->dev;
rtn = dev_queue_xmit(skb);
#endif
virt_free_packet(pkt);
if(slave)
device_node_put(slave);
return NETDEV_TX_OK;
drop_and_free:
virt_free_packet(pkt);
drop:
dev_kfree_skb(skb);
return NETDEV_TX_OK;
}
/*
* Deal with a transmit timeout.
*/
void virt_tx_timeout (struct net_device *dev)
{
struct virt_priv *priv = netdev_priv(dev);
VIRT_DBG("Transmit timeout at %ld, latency %ld\n", jiffies,
jiffies - dev->trans_start);
/* Simulate a transmission interrupt to get things moving */
//priv->status = VIRT_TX_INTR;
//virt_interrupt(0, dev, NULL);
priv->stats.tx_errors++;
netif_wake_queue(dev);
return;
}
/*
* Finish sending the packet on the given path. The skb is consumed whether
* the transmission succeeds or not.
*/
static int virt_finish_tx(struct virt_priv *virt, struct sk_buff *skb,
struct pathinfo *path)
{
struct tunhdr *tunhdr = (struct tunhdr *)skb->data;
struct virt_skb_cb *skb_cb = (struct virt_skb_cb *)skb->cb;
long payload_len = skb->len - sizeof(struct tunhdr);
struct flow_table_entry *flow = skb_cb->flow; /* NULL for XOR packets. */
__be16 rx_port;
struct device_node *slave = NULL;
struct remote_link *link = NULL;
if(flow)
rx_port = flow->rx_port;
else
rx_port = htons(virt_tunnel_source_port());
slave = slave_get_by_ifindex(path->local_index);
if(unlikely(!slave))
goto drop;
link = find_remote_link_by_addr(&virt->network,
&path->remote_addr.ip4, path->remote_port);
if(unlikely(!link))
goto drop;
virt_finish_tunhdr(tunhdr, path, flow, link->node);
virt_build_udp_header(skb, rx_port, link->rif.data_port);
virt_build_ip_header(skb, slave->lif.ip4, link->rif.ip4);
path->avail -= payload_len;
path->snd_nxt += payload_len;
skb->dev = slave->dev;
skb->protocol = htons(ETH_P_IP);
dev_hard_header(skb, slave->dev, ETH_P_IP, slave->next_hop_addr,
slave->dev->dev_addr, skb->len);
skb_reset_mac_header(skb);
/* Update link statistics -- these may not be accurate if the packet
* gets dropped after dev_queue_xmit. */
slave->stats.tx_packets++;
slave->stats.tx_bytes += skb->len;
if(flow)
flow->flow_stats->last_tx_dev = slave->dev->ifindex;
path_update_tx_bytes(path, skb->len);
/* Clean up before dev_queue_xmit consumes the skb. */
remote_link_put(link);
device_node_put(slave);
dev_queue_xmit(skb);
return 0;
drop:
virt->stats.tx_dropped++;
if(link)
remote_link_put(link);
if(slave)
device_node_put(slave);
dev_kfree_skb(skb);
return -1;
}
/*
* Send a copy of the packet on one of the stalled paths to the destination if
* there are any. This serves as a probe to test whether the path is working
* again.
*/
static int send_probe_packet(struct virt_priv *virt, struct sk_buff *skb,
struct remote_node *dest)
{
struct sk_buff *copy;
struct pathinfo *path;
unsigned long now = jiffies;
if(dest->stalled_paths_len <= 0)
return 0;
rcu_read_lock();
list_for_each_entry_rcu(path, &dest->stalled_paths, stall_list) {
unsigned long probe_interval = virt_probe_interval_jiffies();
if(afterl(now, path->last_packet + probe_interval)) {
copy = skb_copy(skb, GFP_ATOMIC);
if(!copy)
break;
virt_finish_tx(virt, copy, path);
/* Move this path to the end of the stall list to prevent
* starvation of paths. */
spin_lock_bh(&dest->stalled_paths_lock);
list_del_rcu(&path->stall_list);
list_add_tail_rcu(&path->stall_list, &dest->stalled_paths);
spin_unlock_bh(&dest->stalled_paths_lock);
}
}
rcu_read_unlock();
return 0;
}
/*
* Test if a slave device can be used to transmit.
*
* Checks the slave device's priority >= min_prio and whether the DEVICE_NO_TX
* flag is set.
*/
static int can_use_device(const struct device_node *slave, int min_prio)
{
if(slave->lif.prio < min_prio)
return 0;
if(slave->flags & DEVICE_NO_TX)
return 0;
return 1;
}
/*
* Send copies of the packet on multiple paths. If at least one copy is sent,
* then the skb will be consumed.
*
* The return value indicates the number of copies sent.
*/
static int virt_send_duplicate(struct virt_priv *virt,
struct sk_buff *skb, struct remote_node *dest)
{
struct list_head *slave_list = get_slave_list_head();
struct device_node *slave = NULL;
int sent = 0;
rcu_read_lock();
list_for_each_entry(slave, slave_list, lif.list) {
if(can_use_device(slave, virt->max_dev_prio)) {
struct remote_link *link;
list_for_each_entry_rcu(link, &dest->links, rif.list) {
if(link->rif.prio >= dest->max_link_prio) {
struct pathinfo *path;
struct sk_buff *copy = skb_copy(skb, GFP_ATOMIC);
if(unlikely(!copy))
goto out;
path = path_lookup_create(&virt->network, slave, link);
if(likely(path)) {
virt_finish_tx(virt, copy, path);
virt_path_put(path);
sent++;
} else {
dev_kfree_skb(copy);
}
}
}
}
}
out:
rcu_read_unlock();
if(sent > 0)
dev_kfree_skb(skb);
return sent;
}
static struct pathinfo *virt_select_single_path(struct virt_priv *virt,
struct flow_table_entry *flow, struct remote_node *dest)
{
struct list_head *slave_list = get_slave_list_head();
struct device_node *slave = NULL;
struct remote_link *link = NULL;
struct pathinfo *path = NULL;
slave = select_local_interface(virt, flow, dest, slave_list);
if(!slave)
goto out;
link = select_remote_interface(virt, flow, slave, dest, &dest->links);
if(!link)
goto out;
path = path_lookup_create(&virt->network, slave, link);
out:
if(link)
remote_link_put(link);
if(slave)
device_node_put(slave);
return path;
}
static struct pathinfo *virt_select_multi_path(struct virt_priv *virt,
struct flow_table_entry *flow, long payload_len, struct remote_node *dest)
{
struct list_head *slave_list = get_slave_list_head();
struct device_node *slave = NULL;
unsigned long best_avail = 0;
struct device_node *best_slave = NULL;
struct remote_link *best_link = NULL;
struct pathinfo *sel_path = NULL;
rcu_read_lock();
list_for_each_entry(slave, slave_list, lif.list) {
if(can_use_device(slave, virt->max_dev_prio)) {
struct remote_link *link;
list_for_each_entry_rcu(link, &dest->links, rif.list) {
if(link->rif.prio >= dest->max_link_prio) {
struct pathinfo *path = path_lookup_create(&virt->network, slave, link);
if(path && payload_len <= path->avail) {
unsigned long avail = (path->avail << 8) / path->cwnd;
if(avail > best_avail) {
best_avail = avail;
best_slave = slave;
best_link = link;
}
}
if(path)
virt_path_put(path);
}
}
}
}
if(best_link)
sel_path = path_lookup_create(&virt->network, best_slave, best_link);
rcu_read_unlock();
return sel_path;
}
/*
* Select a path other than the given path that has high priority.
*/
static struct pathinfo *virt_select_same_prio(struct virt_priv *virt,
struct remote_node *dest, struct pathinfo *path)
{
struct list_head *slave_list = get_slave_list_head();
struct device_node *slave = NULL;
unsigned long best_rtt = ULONG_MAX;
struct device_node *best_slave = NULL;
struct remote_link *best_link = NULL;
struct pathinfo *sel_path = NULL;
rcu_read_lock();
list_for_each_entry(slave, slave_list, lif.list) {
if(can_use_device(slave, virt->max_dev_prio)) {
struct remote_link *link;
list_for_each_entry_rcu(link, &dest->links, rif.list) {
if(link->rif.prio >= dest->max_link_prio) {
struct pathinfo *other = path_lookup_create(&virt->network, slave, link);
if(other && other != path) {
unsigned long rtt = pathinfo_est_rtt(other);
if(rtt < best_rtt) {
best_rtt = rtt;
best_slave = slave;
best_link = link;
}
}
if(other)
virt_path_put(other);
}
}
}
}
if(best_link)
sel_path = path_lookup_create(&virt->network, best_slave, best_link);
rcu_read_unlock();
return sel_path;
}
/*
* Select a path other than the given path that has low priority.
*/
static struct pathinfo *virt_select_low_prio(struct virt_priv *virt,
struct remote_node *dest, struct pathinfo *path)
{
struct list_head *slave_list = get_slave_list_head();
struct device_node *slave = NULL;
int best_local_prio = -1;
int best_remote_prio = -1;
unsigned long best_rtt = ULONG_MAX;
struct device_node *best_slave = NULL;
struct remote_link *best_link = NULL;
struct pathinfo *sel_path = NULL;
rcu_read_lock();
list_for_each_entry(slave, slave_list, lif.list) {
if(slave->lif.prio < virt->max_dev_prio && can_use_device(slave, best_local_prio)) {
struct remote_link *link;
list_for_each_entry_rcu(link, &dest->links, rif.list) {
if(link->rif.prio >= best_remote_prio && link->rif.prio < dest->max_link_prio) {
struct pathinfo *other = path_lookup_create(&virt->network, slave, link);
if(other && other != path) {
unsigned long rtt = pathinfo_est_rtt(other);
if(slave->lif.prio > best_local_prio || link->rif.prio > best_remote_prio) {
best_local_prio = slave->lif.prio;
best_remote_prio = link->rif.prio;
best_rtt = ULONG_MAX;
}
if(rtt < best_rtt) {
best_rtt = rtt;
best_slave = slave;
best_link = link;
}
}
if(other)
virt_path_put(other);
}
}
}
}
if(best_link)
sel_path = path_lookup_create(&virt->network, best_slave, best_link);
rcu_read_unlock();
return sel_path;
}
static int virt_finish_xor_packets(struct virt_priv *virt, struct pathinfo *path, struct xor_packet_buffer *buffer, struct sk_buff *skb)
{
/* TODO: Figure out how to set these values. */
const int headroom = 100;
const int tailroom = 1500;
struct tunhdr *tunhdr = (struct tunhdr *)skb->data;
unsigned seq = ntohl(tunhdr->seq);
int sent = 0;
unsigned xor_seq = seq % buffer->rate;
if(xor_seq == 0) {
/* On the first packet of the group, allocate a buffer with enough
* tailroom to hold an MTU-sized XORed packet. */
struct tunhdr *xor_tunhdr;
struct virt_skb_cb *skb_cb;
if(buffer->skb)
dev_kfree_skb(buffer->skb);
buffer->skb = skb_copy_expand(skb, headroom, tailroom, GFP_ATOMIC);
if(!buffer->skb)
return sent;
skb_cb = (struct virt_skb_cb *)buffer->skb->cb;
skb_cb->flow = NULL; /* XOR packets should not retain reference to flow. */
buffer->rate = buffer->next_rate;
xor_tunhdr = (struct tunhdr *)buffer->skb->data;
xor_tunhdr->flags |= TUN_FLAG_XOR_CODED;
xor_tunhdr->xor_rate = buffer->rate;
} else if(xor_seq == (buffer->rate - 1)) {
/* On the last packet of the group, XOR the last packet, then send
* out the coded packet. */
if(buffer->skb) {
xor_sk_buff(buffer->skb, skb, sizeof(struct tunhdr));
virt_finish_tx(virt, buffer->skb, path);
buffer->skb = NULL;
sent++;
}
} else if(buffer->skb) {
/* On any other packet, just XOR it. */
xor_sk_buff(buffer->skb, skb, sizeof(struct tunhdr));
}
return sent;
}
static int virt_send_xor_packets(struct virt_priv *virt,
struct sk_buff *skb, struct remote_node *dest, struct pathinfo *path)
{
int sent = 0;
if(path->xor_same_path.rate == 1) {
struct sk_buff *copy = skb_copy(skb, GFP_ATOMIC);
if(unlikely(!copy))
goto out;
path->xor_same_path.rate = path->xor_same_path.next_rate;
virt_finish_tx(virt, copy, path);
sent++;
} else if(path->xor_same_path.rate > 1) {
sent += virt_finish_xor_packets(virt, path, &path->xor_same_path, skb);
} else {
path->xor_same_path.rate = path->xor_same_path.next_rate;
}
if(path->xor_same_prio.rate == 1) {
struct pathinfo *other = virt_select_same_prio(virt, dest, path);
if(other) {
struct sk_buff *copy = skb_copy(skb, GFP_ATOMIC);
if(unlikely(!copy)) {
virt_path_put(other);
goto out;
}
path->xor_same_prio.rate = path->xor_same_prio.next_rate;
virt_finish_tx(virt, copy, other);
virt_path_put(other);
sent++;
}
} else if(path->xor_same_prio.rate > 1) {
sent += virt_finish_xor_packets(virt, path, &path->xor_same_prio, skb);
} else {
path->xor_same_prio.rate = path->xor_same_prio.next_rate;
}
if(path->xor_lower_prio.rate == 1) {
struct pathinfo *other = virt_select_low_prio(virt, dest, path);
if(other) {
struct sk_buff *copy = skb_copy(skb, GFP_ATOMIC);
if(unlikely(!copy)) {
virt_path_put(other);
goto out;
}
path->xor_lower_prio.rate = path->xor_lower_prio.next_rate;
virt_finish_tx(virt, copy, other);
virt_path_put(other);
sent++;
}
} else if(path->xor_lower_prio.rate > 1) {
sent += virt_finish_xor_packets(virt, path, &path->xor_lower_prio, skb);
} else {
path->xor_lower_prio.rate = path->xor_lower_prio.next_rate;
}
out:
return sent;
}
/*
* Find a suitable path or paths for delivery and begin transmission.
*
* Returns 1 to indicate packet was sent (or dropped) or 0 to indicate the
* packet was queued for later delivery.
*/
int virt_start_tx(struct virt_priv *virt, struct sk_buff *skb,
struct remote_node *dest)
{
struct virt_skb_cb *skb_cb = (struct virt_skb_cb *)skb->cb;
struct flow_table_entry *flow = skb_cb->flow;
long payload_len = skb->len - sizeof(struct tunhdr);
if(flow->policy->action & POLICY_OP_DUPLICATE) {
if(virt_send_duplicate(virt, skb, dest) <= 0)
goto queue;
} else if(flow->policy->action & POLICY_OP_MULTIPATH) {
struct pathinfo *path;
/* Send optional probe packet(s) on stalled paths. */
send_probe_packet(virt, skb, dest);
path = virt_select_multi_path(virt, flow, payload_len, dest);
if(path) {
virt_finish_tx(virt, skb, path);
virt_path_put(path);
} else {
goto queue;
}