/
stt.c
2110 lines (1733 loc) · 49.1 KB
/
stt.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
/*
* Stateless TCP Tunnel (STT) vport.
*
* Copyright (c) 2015 Nicira, Inc.
*
* 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.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <asm/unaligned.h>
#include <linux/delay.h>
#include <linux/flex_array.h>
#include <linux/if.h>
#include <linux/if_vlan.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/jhash.h>
#include <linux/list.h>
#include <linux/log2.h>
#include <linux/module.h>
#include <linux/net.h>
#include <linux/netfilter.h>
#include <linux/percpu.h>
#include <linux/skbuff.h>
#include <linux/tcp.h>
#include <linux/workqueue.h>
#include <net/dst_metadata.h>
#include <net/icmp.h>
#include <net/inet_ecn.h>
#include <net/ip.h>
#include <net/ip_tunnels.h>
#include <net/ip6_checksum.h>
#include <net/net_namespace.h>
#include <net/netns/generic.h>
#include <net/sock.h>
#include <net/stt.h>
#include <net/tcp.h>
#include <net/udp.h>
#include "gso.h"
#include "compat.h"
#define STT_NETDEV_VER "0.1"
#define STT_DST_PORT 7471
#ifdef OVS_STT
#ifdef CONFIG_SLUB
/*
* We saw better performance with skipping zero copy in case of SLUB.
* So skip zero copy for SLUB case.
*/
#define SKIP_ZERO_COPY
#endif
#define STT_VER 0
/* @list: Per-net list of STT ports.
* @rcv: The callback is called on STT packet recv, STT reassembly can generate
* multiple packets, in this case first packet has tunnel outer header, rest
* of the packets are inner packet segments with no stt header.
* @rcv_data: user data.
* @sock: Fake TCP socket for the STT port.
*/
struct stt_dev {
struct net_device *dev;
struct net *net;
struct list_head next;
struct list_head up_next;
struct socket *sock;
__be16 dst_port;
};
#define STT_CSUM_VERIFIED BIT(0)
#define STT_CSUM_PARTIAL BIT(1)
#define STT_PROTO_IPV4 BIT(2)
#define STT_PROTO_TCP BIT(3)
#define STT_PROTO_TYPES (STT_PROTO_IPV4 | STT_PROTO_TCP)
#define SUPPORTED_GSO_TYPES (SKB_GSO_TCPV4 | SKB_GSO_UDP | SKB_GSO_DODGY | \
SKB_GSO_TCPV6)
/* The length and offset of a fragment are encoded in the sequence number.
* STT_SEQ_LEN_SHIFT is the left shift needed to store the length.
* STT_SEQ_OFFSET_MASK is the mask to extract the offset.
*/
#define STT_SEQ_LEN_SHIFT 16
#define STT_SEQ_OFFSET_MASK (BIT(STT_SEQ_LEN_SHIFT) - 1)
/* The maximum amount of memory used to store packets waiting to be reassembled
* on a given CPU. Once this threshold is exceeded we will begin freeing the
* least recently used fragments.
*/
#define REASM_HI_THRESH (4 * 1024 * 1024)
/* The target for the high memory evictor. Once we have exceeded
* REASM_HI_THRESH, we will continue freeing fragments until we hit
* this limit.
*/
#define REASM_LO_THRESH (3 * 1024 * 1024)
/* The length of time a given packet has to be reassembled from the time the
* first fragment arrives. Once this limit is exceeded it becomes available
* for cleaning.
*/
#define FRAG_EXP_TIME (30 * HZ)
/* Number of hash entries. Each entry has only a single slot to hold a packet
* so if there are collisions, we will drop packets. This is allocated
* per-cpu and each entry consists of struct pkt_frag.
*/
#define FRAG_HASH_SHIFT 8
#define FRAG_HASH_ENTRIES BIT(FRAG_HASH_SHIFT)
#define FRAG_HASH_SEGS ((sizeof(u32) * 8) / FRAG_HASH_SHIFT)
#define CLEAN_PERCPU_INTERVAL (30 * HZ)
struct pkt_key {
__be32 saddr;
__be32 daddr;
__be32 pkt_seq;
u32 mark;
};
struct pkt_frag {
struct sk_buff *skbs;
unsigned long timestamp;
struct list_head lru_node;
struct pkt_key key;
};
struct stt_percpu {
struct flex_array *frag_hash;
struct list_head frag_lru;
unsigned int frag_mem_used;
/* Protect frags table. */
spinlock_t lock;
};
struct first_frag {
struct sk_buff *last_skb;
unsigned int mem_used;
u16 tot_len;
u16 rcvd_len;
bool set_ecn_ce;
};
struct frag_skb_cb {
u16 offset;
/* Only valid for the first skb in the chain. */
struct first_frag first;
};
#define FRAG_CB(skb) ((struct frag_skb_cb *)(skb)->cb)
/* per-network namespace private data for this module */
struct stt_net {
struct list_head stt_list;
struct list_head stt_up_list; /* Devices which are in IFF_UP state. */
int n_tunnels;
#ifdef HAVE_NF_REGISTER_NET_HOOK
bool nf_hook_reg_done;
#endif
};
static int stt_net_id;
static struct stt_percpu __percpu *stt_percpu_data __read_mostly;
static u32 frag_hash_seed __read_mostly;
/* Protects sock-hash and refcounts. */
static DEFINE_MUTEX(stt_mutex);
static int n_tunnels;
static DEFINE_PER_CPU(u32, pkt_seq_counter);
static void clean_percpu(struct work_struct *work);
static DECLARE_DELAYED_WORK(clean_percpu_wq, clean_percpu);
static struct stt_dev *stt_find_up_dev(struct net *net, __be16 port)
{
struct stt_net *sn = net_generic(net, stt_net_id);
struct stt_dev *stt_dev;
list_for_each_entry_rcu(stt_dev, &sn->stt_up_list, up_next) {
if (stt_dev->dst_port == port)
return stt_dev;
}
return NULL;
}
static __be32 ack_seq(void)
{
#if NR_CPUS <= 65536
u32 pkt_seq, ack;
pkt_seq = this_cpu_read(pkt_seq_counter);
ack = pkt_seq << ilog2(NR_CPUS) | smp_processor_id();
this_cpu_inc(pkt_seq_counter);
return (__force __be32)ack;
#else
#error "Support for greater than 64k CPUs not implemented"
#endif
}
static int clear_gso(struct sk_buff *skb)
{
struct skb_shared_info *shinfo = skb_shinfo(skb);
int err;
if (shinfo->gso_type == 0 && shinfo->gso_size == 0 &&
shinfo->gso_segs == 0)
return 0;
err = skb_unclone(skb, GFP_ATOMIC);
if (unlikely(err))
return err;
shinfo = skb_shinfo(skb);
shinfo->gso_type = 0;
shinfo->gso_size = 0;
shinfo->gso_segs = 0;
return 0;
}
static void copy_skb_metadata(struct sk_buff *to, struct sk_buff *from)
{
to->protocol = from->protocol;
to->tstamp = from->tstamp;
to->priority = from->priority;
to->mark = from->mark;
to->vlan_tci = from->vlan_tci;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
to->vlan_proto = from->vlan_proto;
#endif
skb_copy_secmark(to, from);
}
static void update_headers(struct sk_buff *skb, bool head,
unsigned int l4_offset, unsigned int hdr_len,
bool ipv4, u32 tcp_seq)
{
u16 old_len, new_len;
__be32 delta;
struct tcphdr *tcph;
int gso_size;
if (ipv4) {
struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);
old_len = ntohs(iph->tot_len);
new_len = skb->len - ETH_HLEN;
iph->tot_len = htons(new_len);
ip_send_check(iph);
} else {
struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + ETH_HLEN);
old_len = ntohs(ip6h->payload_len);
new_len = skb->len - ETH_HLEN - sizeof(struct ipv6hdr);
ip6h->payload_len = htons(new_len);
}
tcph = (struct tcphdr *)(skb->data + l4_offset);
if (!head) {
tcph->seq = htonl(tcp_seq);
tcph->cwr = 0;
}
if (skb->next) {
tcph->fin = 0;
tcph->psh = 0;
}
delta = htonl(~old_len + new_len);
tcph->check = ~csum_fold((__force __wsum)((__force u32)tcph->check +
(__force u32)delta));
gso_size = skb_shinfo(skb)->gso_size;
if (gso_size && skb->len - hdr_len <= gso_size)
BUG_ON(clear_gso(skb));
}
static bool can_segment(struct sk_buff *head, bool ipv4, bool tcp, bool csum_partial)
{
/* If no offloading is in use then we don't have enough information
* to process the headers.
*/
if (!csum_partial)
goto linearize;
/* Handling UDP packets requires IP fragmentation, which means that
* the L4 checksum can no longer be calculated by hardware (since the
* fragments are in different packets. If we have to compute the
* checksum it's faster just to linearize and large UDP packets are
* pretty uncommon anyways, so it's not worth dealing with for now.
*/
if (!tcp)
goto linearize;
if (ipv4) {
struct iphdr *iph = (struct iphdr *)(head->data + ETH_HLEN);
/* It's difficult to get the IP IDs exactly right here due to
* varying segment sizes and potentially multiple layers of
* segmentation. IP ID isn't important when DF is set and DF
* is generally set for TCP packets, so just linearize if it's
* not.
*/
if (!(iph->frag_off & htons(IP_DF)))
goto linearize;
} else {
struct ipv6hdr *ip6h = (struct ipv6hdr *)(head->data + ETH_HLEN);
/* Jumbograms require more processing to update and we'll
* probably never see them, so just linearize.
*/
if (ip6h->payload_len == 0)
goto linearize;
}
return true;
linearize:
return false;
}
static int copy_headers(struct sk_buff *head, struct sk_buff *frag,
int hdr_len)
{
u16 csum_start;
if (skb_cloned(frag) || skb_headroom(frag) < hdr_len) {
int extra_head = hdr_len - skb_headroom(frag);
extra_head = extra_head > 0 ? extra_head : 0;
if (unlikely(pskb_expand_head(frag, extra_head, 0,
GFP_ATOMIC)))
return -ENOMEM;
}
memcpy(__skb_push(frag, hdr_len), head->data, hdr_len);
csum_start = head->csum_start - skb_headroom(head);
frag->csum_start = skb_headroom(frag) + csum_start;
frag->csum_offset = head->csum_offset;
frag->ip_summed = head->ip_summed;
skb_shinfo(frag)->gso_size = skb_shinfo(head)->gso_size;
skb_shinfo(frag)->gso_type = skb_shinfo(head)->gso_type;
skb_shinfo(frag)->gso_segs = 0;
copy_skb_metadata(frag, head);
return 0;
}
static int skb_list_segment(struct sk_buff *head, bool ipv4, int l4_offset)
{
struct sk_buff *skb;
struct tcphdr *tcph;
int seg_len;
int hdr_len;
int tcp_len;
u32 seq;
if (unlikely(!pskb_may_pull(head, l4_offset + sizeof(*tcph))))
return -ENOMEM;
tcph = (struct tcphdr *)(head->data + l4_offset);
tcp_len = tcph->doff * 4;
hdr_len = l4_offset + tcp_len;
if (unlikely((tcp_len < sizeof(struct tcphdr)) ||
(head->len < hdr_len)))
return -EINVAL;
if (unlikely(!pskb_may_pull(head, hdr_len)))
return -ENOMEM;
tcph = (struct tcphdr *)(head->data + l4_offset);
/* Update header of each segment. */
seq = ntohl(tcph->seq);
seg_len = skb_pagelen(head) - hdr_len;
skb = skb_shinfo(head)->frag_list;
skb_shinfo(head)->frag_list = NULL;
head->next = skb;
for (; skb; skb = skb->next) {
int err;
head->len -= skb->len;
head->data_len -= skb->len;
head->truesize -= skb->truesize;
seq += seg_len;
seg_len = skb->len;
err = copy_headers(head, skb, hdr_len);
if (err)
return err;
update_headers(skb, false, l4_offset, hdr_len, ipv4, seq);
}
update_headers(head, true, l4_offset, hdr_len, ipv4, 0);
return 0;
}
#ifndef SKIP_ZERO_COPY
static struct sk_buff *normalize_frag_list(struct sk_buff *head,
struct sk_buff **skbp)
{
struct sk_buff *skb = *skbp;
struct sk_buff *last;
do {
struct sk_buff *frags;
if (skb_shared(skb)) {
struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
if (unlikely(!nskb))
return ERR_PTR(-ENOMEM);
nskb->next = skb->next;
consume_skb(skb);
skb = nskb;
*skbp = skb;
}
if (head) {
head->len -= skb->len;
head->data_len -= skb->len;
head->truesize -= skb->truesize;
}
frags = skb_shinfo(skb)->frag_list;
if (frags) {
int err;
err = skb_unclone(skb, GFP_ATOMIC);
if (unlikely(err))
return ERR_PTR(err);
last = normalize_frag_list(skb, &frags);
if (IS_ERR(last))
return last;
skb_shinfo(skb)->frag_list = NULL;
last->next = skb->next;
skb->next = frags;
} else {
last = skb;
}
skbp = &skb->next;
} while ((skb = skb->next));
return last;
}
/* Takes a linked list of skbs, which potentially contain frag_list
* (whose members in turn potentially contain frag_lists, etc.) and
* converts them into a single linear linked list.
*/
static int straighten_frag_list(struct sk_buff **skbp)
{
struct sk_buff *err_skb;
err_skb = normalize_frag_list(NULL, skbp);
if (IS_ERR(err_skb))
return PTR_ERR(err_skb);
return 0;
}
static int coalesce_skb(struct sk_buff **headp)
{
struct sk_buff *frag, *head, *prev;
int err;
err = straighten_frag_list(headp);
if (unlikely(err))
return err;
head = *headp;
/* Coalesce frag list. */
prev = head;
for (frag = head->next; frag; frag = frag->next) {
bool headstolen;
int delta;
if (unlikely(skb_unclone(prev, GFP_ATOMIC)))
return -ENOMEM;
if (!skb_try_coalesce(prev, frag, &headstolen, &delta)) {
prev = frag;
continue;
}
prev->next = frag->next;
frag->len = 0;
frag->data_len = 0;
frag->truesize -= delta;
kfree_skb_partial(frag, headstolen);
frag = prev;
}
if (!head->next)
return 0;
for (frag = head->next; frag; frag = frag->next) {
head->len += frag->len;
head->data_len += frag->len;
head->truesize += frag->truesize;
}
skb_shinfo(head)->frag_list = head->next;
head->next = NULL;
return 0;
}
#else
static int coalesce_skb(struct sk_buff **headp)
{
struct sk_buff *frag, *head = *headp, *next;
int delta = FRAG_CB(head)->first.tot_len - skb_headlen(head);
int err;
if (unlikely(!head->next))
return 0;
err = pskb_expand_head(head, 0, delta, GFP_ATOMIC);
if (unlikely(err))
return err;
if (unlikely(!__pskb_pull_tail(head, head->data_len)))
BUG();
for (frag = head->next; frag; frag = next) {
skb_copy_bits(frag, 0, skb_put(head, frag->len), frag->len);
next = frag->next;
kfree_skb(frag);
}
head->next = NULL;
head->truesize = SKB_TRUESIZE(head->len);
return 0;
}
#endif
static int __try_to_segment(struct sk_buff *skb, bool csum_partial,
bool ipv4, bool tcp, int l4_offset)
{
if (can_segment(skb, ipv4, tcp, csum_partial))
return skb_list_segment(skb, ipv4, l4_offset);
else
return skb_linearize(skb);
}
static int try_to_segment(struct sk_buff *skb)
{
struct stthdr *stth = stt_hdr(skb);
bool csum_partial = !!(stth->flags & STT_CSUM_PARTIAL);
bool ipv4 = !!(stth->flags & STT_PROTO_IPV4);
bool tcp = !!(stth->flags & STT_PROTO_TCP);
int l4_offset = stth->l4_offset;
return __try_to_segment(skb, csum_partial, ipv4, tcp, l4_offset);
}
static int segment_skb(struct sk_buff **headp, bool csum_partial,
bool ipv4, bool tcp, int l4_offset)
{
#ifndef SKIP_ZERO_COPY
int err;
err = coalesce_skb(headp);
if (err)
return err;
#endif
if (skb_shinfo(*headp)->frag_list)
return __try_to_segment(*headp, csum_partial,
ipv4, tcp, l4_offset);
return 0;
}
static int __push_stt_header(struct sk_buff *skb, __be64 tun_id,
__be16 s_port, __be16 d_port,
__be32 saddr, __be32 dst,
__be16 l3_proto, u8 l4_proto,
int dst_mtu)
{
int data_len = skb->len + sizeof(struct stthdr) + STT_ETH_PAD;
unsigned short encap_mss;
struct tcphdr *tcph;
struct stthdr *stth;
skb_push(skb, STT_HEADER_LEN);
skb_reset_transport_header(skb);
tcph = tcp_hdr(skb);
memset(tcph, 0, STT_HEADER_LEN);
stth = stt_hdr(skb);
if (skb->ip_summed == CHECKSUM_PARTIAL) {
stth->flags |= STT_CSUM_PARTIAL;
stth->l4_offset = skb->csum_start -
(skb_headroom(skb) +
STT_HEADER_LEN);
if (l3_proto == htons(ETH_P_IP))
stth->flags |= STT_PROTO_IPV4;
if (l4_proto == IPPROTO_TCP)
stth->flags |= STT_PROTO_TCP;
stth->mss = htons(skb_shinfo(skb)->gso_size);
} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
stth->flags |= STT_CSUM_VERIFIED;
}
stth->vlan_tci = htons(skb->vlan_tci);
skb->vlan_tci = 0;
put_unaligned(tun_id, &stth->key);
tcph->source = s_port;
tcph->dest = d_port;
tcph->doff = sizeof(struct tcphdr) / 4;
tcph->ack = 1;
tcph->psh = 1;
tcph->window = htons(USHRT_MAX);
tcph->seq = htonl(data_len << STT_SEQ_LEN_SHIFT);
tcph->ack_seq = ack_seq();
tcph->check = ~tcp_v4_check(skb->len, saddr, dst, 0);
skb->csum_start = skb_transport_header(skb) - skb->head;
skb->csum_offset = offsetof(struct tcphdr, check);
skb->ip_summed = CHECKSUM_PARTIAL;
encap_mss = dst_mtu - sizeof(struct iphdr) - sizeof(struct tcphdr);
if (data_len > encap_mss) {
if (unlikely(skb_unclone(skb, GFP_ATOMIC)))
return -EINVAL;
skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
skb_shinfo(skb)->gso_size = encap_mss;
skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(data_len, encap_mss);
} else {
if (unlikely(clear_gso(skb)))
return -EINVAL;
}
return 0;
}
static struct sk_buff *push_stt_header(struct sk_buff *head, __be64 tun_id,
__be16 s_port, __be16 d_port,
__be32 saddr, __be32 dst,
__be16 l3_proto, u8 l4_proto,
int dst_mtu)
{
struct sk_buff *skb;
if (skb_shinfo(head)->frag_list) {
bool ipv4 = (l3_proto == htons(ETH_P_IP));
bool tcp = (l4_proto == IPPROTO_TCP);
bool csum_partial = (head->ip_summed == CHECKSUM_PARTIAL);
int l4_offset = skb_transport_offset(head);
/* Need to call skb_orphan() to report currect true-size.
* calling skb_orphan() in this layer is odd but SKB with
* frag-list should not be associated with any socket, so
* skb-orphan should be no-op. */
skb_orphan(head);
if (unlikely(segment_skb(&head, csum_partial,
ipv4, tcp, l4_offset)))
goto error;
}
for (skb = head; skb; skb = skb->next) {
if (__push_stt_header(skb, tun_id, s_port, d_port, saddr, dst,
l3_proto, l4_proto, dst_mtu))
goto error;
}
return head;
error:
kfree_skb_list(head);
return NULL;
}
static int stt_can_offload(struct sk_buff *skb, __be16 l3_proto, u8 l4_proto)
{
if (skb_is_gso(skb) && skb->ip_summed != CHECKSUM_PARTIAL) {
int csum_offset;
__sum16 *csum;
int len;
if (l4_proto == IPPROTO_TCP)
csum_offset = offsetof(struct tcphdr, check);
else if (l4_proto == IPPROTO_UDP)
csum_offset = offsetof(struct udphdr, check);
else
return 0;
len = skb->len - skb_transport_offset(skb);
csum = (__sum16 *)(skb_transport_header(skb) + csum_offset);
if (unlikely(!pskb_may_pull(skb, skb_transport_offset(skb) +
csum_offset + sizeof(*csum))))
return -EINVAL;
if (l3_proto == htons(ETH_P_IP)) {
struct iphdr *iph = ip_hdr(skb);
*csum = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
len, l4_proto, 0);
} else if (l3_proto == htons(ETH_P_IPV6)) {
struct ipv6hdr *ip6h = ipv6_hdr(skb);
*csum = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
len, l4_proto, 0);
} else {
return 0;
}
skb->csum_start = skb_transport_header(skb) - skb->head;
skb->csum_offset = csum_offset;
skb->ip_summed = CHECKSUM_PARTIAL;
}
if (skb->ip_summed == CHECKSUM_PARTIAL) {
/* Assume receiver can only offload TCP/UDP over IPv4/6,
* and require 802.1Q VLANs to be accelerated.
*/
if (l3_proto != htons(ETH_P_IP) &&
l3_proto != htons(ETH_P_IPV6))
return 0;
if (l4_proto != IPPROTO_TCP && l4_proto != IPPROTO_UDP)
return 0;
/* L4 offset must fit in a 1-byte field. */
if (skb->csum_start - skb_headroom(skb) > 255)
return 0;
if (skb_shinfo(skb)->gso_type & ~SUPPORTED_GSO_TYPES)
return 0;
}
/* Total size of encapsulated packet must fit in 16 bits. */
if (skb->len + STT_HEADER_LEN + sizeof(struct iphdr) > 65535)
return 0;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
if (skb_vlan_tag_present(skb) && skb->vlan_proto != htons(ETH_P_8021Q))
return 0;
#endif
return 1;
}
static bool need_linearize(const struct sk_buff *skb)
{
struct skb_shared_info *shinfo = skb_shinfo(skb);
int i;
if (unlikely(shinfo->frag_list))
return true;
/* Generally speaking we should linearize if there are paged frags.
* However, if all of the refcounts are 1 we know nobody else can
* change them from underneath us and we can skip the linearization.
*/
for (i = 0; i < shinfo->nr_frags; i++)
if (unlikely(page_count(skb_frag_page(&shinfo->frags[i])) > 1))
return true;
return false;
}
static struct sk_buff *handle_offloads(struct sk_buff *skb, int min_headroom)
{
int err;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
if (skb_vlan_tag_present(skb) && skb->vlan_proto != htons(ETH_P_8021Q)) {
min_headroom += VLAN_HLEN;
if (skb_headroom(skb) < min_headroom) {
int head_delta = SKB_DATA_ALIGN(min_headroom -
skb_headroom(skb) + 16);
err = pskb_expand_head(skb, max_t(int, head_delta, 0),
0, GFP_ATOMIC);
if (unlikely(err))
goto error;
}
skb = __vlan_hwaccel_push_inside(skb);
if (!skb) {
err = -ENOMEM;
goto error;
}
}
#endif
if (skb_is_gso(skb)) {
struct sk_buff *nskb;
char cb[sizeof(skb->cb)];
memcpy(cb, skb->cb, sizeof(cb));
nskb = __skb_gso_segment(skb, 0, false);
if (IS_ERR(nskb)) {
err = PTR_ERR(nskb);
goto error;
}
consume_skb(skb);
skb = nskb;
while (nskb) {
memcpy(nskb->cb, cb, sizeof(cb));
nskb = nskb->next;
}
} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
/* Pages aren't locked and could change at any time.
* If this happens after we compute the checksum, the
* checksum will be wrong. We linearize now to avoid
* this problem.
*/
if (unlikely(need_linearize(skb))) {
err = __skb_linearize(skb);
if (unlikely(err))
goto error;
}
err = skb_checksum_help(skb);
if (unlikely(err))
goto error;
}
skb->ip_summed = CHECKSUM_NONE;
return skb;
error:
kfree_skb(skb);
return ERR_PTR(err);
}
static void skb_list_xmit(struct rtable *rt, struct sk_buff *skb, __be32 src,
__be32 dst, __u8 tos, __u8 ttl, __be16 df)
{
while (skb) {
struct sk_buff *next = skb->next;
if (next)
dst_clone(&rt->dst);
skb->next = NULL;
iptunnel_xmit(NULL, rt, skb, src, dst, IPPROTO_TCP,
tos, ttl, df, false);
skb = next;
}
}
static u8 parse_ipv6_l4_proto(struct sk_buff *skb)
{
unsigned int nh_ofs = skb_network_offset(skb);
int payload_ofs;
struct ipv6hdr *nh;
uint8_t nexthdr;
__be16 frag_off;
if (unlikely(!pskb_may_pull(skb, nh_ofs + sizeof(struct ipv6hdr))))
return 0;
nh = ipv6_hdr(skb);
nexthdr = nh->nexthdr;
payload_ofs = (u8 *)(nh + 1) - skb->data;
payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
if (unlikely(payload_ofs < 0))
return 0;
return nexthdr;
}
static u8 skb_get_l4_proto(struct sk_buff *skb, __be16 l3_proto)
{
if (l3_proto == htons(ETH_P_IP)) {
unsigned int nh_ofs = skb_network_offset(skb);
if (unlikely(!pskb_may_pull(skb, nh_ofs + sizeof(struct iphdr))))
return 0;
return ip_hdr(skb)->protocol;
} else if (l3_proto == htons(ETH_P_IPV6)) {
return parse_ipv6_l4_proto(skb);
}
return 0;
}
static int stt_xmit_skb(struct sk_buff *skb, struct rtable *rt,
__be32 src, __be32 dst, __u8 tos,
__u8 ttl, __be16 df, __be16 src_port, __be16 dst_port,
__be64 tun_id)
{
struct ethhdr *eh = eth_hdr(skb);
int ret = 0, min_headroom;
__be16 inner_l3_proto;
u8 inner_l4_proto;
inner_l3_proto = eh->h_proto;
inner_l4_proto = skb_get_l4_proto(skb, inner_l3_proto);
min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
+ STT_HEADER_LEN + sizeof(struct iphdr);
if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
int head_delta = SKB_DATA_ALIGN(min_headroom -
skb_headroom(skb) +
16);
ret = pskb_expand_head(skb, max_t(int, head_delta, 0),
0, GFP_ATOMIC);
if (unlikely(ret))
goto err_free_rt;
}
ret = stt_can_offload(skb, inner_l3_proto, inner_l4_proto);
if (ret < 0)
goto err_free_rt;
if (!ret) {
skb = handle_offloads(skb, min_headroom);
if (IS_ERR(skb)) {
ret = PTR_ERR(skb);
skb = NULL;
goto err_free_rt;
}
}
ret = 0;
while (skb) {
struct sk_buff *next_skb = skb->next;
skb->next = NULL;
if (next_skb)
dst_clone(&rt->dst);
/* Push STT and TCP header. */
skb = push_stt_header(skb, tun_id, src_port, dst_port, src,
dst, inner_l3_proto, inner_l4_proto,
dst_mtu(&rt->dst));
if (unlikely(!skb)) {
ip_rt_put(rt);
goto next;
}
/* Push IP header. */
skb_list_xmit(rt, skb, src, dst, tos, ttl, df);
next:
skb = next_skb;
}
return 0;
err_free_rt:
ip_rt_put(rt);
kfree_skb(skb);
return ret;
}
static struct rtable *stt_get_rt(struct sk_buff *skb,
struct net_device *dev,
struct flowi4 *fl,
const struct ip_tunnel_key *key)
{
struct net *net = dev_net(dev);
/* Route lookup */
memset(fl, 0, sizeof(*fl));
fl->daddr = key->u.ipv4.dst;
fl->saddr = key->u.ipv4.src;
fl->flowi4_tos = RT_TOS(key->tos);
fl->flowi4_mark = skb->mark;
fl->flowi4_proto = IPPROTO_TCP;
return ip_route_output_key(net, fl);
}
netdev_tx_t ovs_stt_xmit(struct sk_buff *skb)
{
struct net_device *dev = skb->dev;
struct stt_dev *stt_dev = netdev_priv(dev);
struct net *net = stt_dev->net;
__be16 dport = stt_dev->dst_port;
struct ip_tunnel_key *tun_key;
struct ip_tunnel_info *tun_info;
struct rtable *rt;
struct flowi4 fl;