/
vr_host_interface.c
2415 lines (2049 loc) · 66.6 KB
/
vr_host_interface.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
/*
* vr_host_interface.c -- linux specific handling of vrouter interfaces
*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <linux/init.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/if_vlan.h>
#include <linux/if_arp.h>
#include <linux/ip.h>
#include <linux/jhash.h>
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39))
#include <linux/if_bridge.h>
#include <linux/openvswitch.h>
#endif
#include <net/rtnetlink.h>
#include "vrouter.h"
#include "vr_packet.h"
#include "vr_compat.h"
#include "vr_interface.h"
#include "vr_linux.h"
#include "vr_bridge.h"
#include "vr_os.h"
#include "vhost.h"
#include "vr_datapath.h"
extern int vhost_init(void);
extern void vhost_exit(void);
extern void vhost_if_add(struct vr_interface *);
extern void vhost_if_del(struct net_device *);
extern void vhost_if_del_phys(struct net_device *);
extern void lh_pfree_skb(struct sk_buff *, unsigned short);
extern int vr_gro_vif_add(struct vrouter *, unsigned int, char *, unsigned short);
extern struct vr_interface_stats *vif_get_stats(struct vr_interface *,
unsigned short);
extern void vif_attach(struct vr_interface *);
extern void vif_detach(struct vr_interface *);
static int vr_napi_poll(struct napi_struct *, int);
static rx_handler_result_t pkt_gro_dev_rx_handler(struct sk_buff **);
static int linux_xmit_segments(struct vr_interface *, struct sk_buff *,
unsigned short);
static rx_handler_result_t pkt_rps_dev_rx_handler(struct sk_buff **pskb);
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39))
extern rx_handler_result_t vhost_rx_handler(struct sk_buff **);
#else
struct vr_interface vr_reset_interface;
#endif
extern volatile bool agent_alive;
/*
* Structure to store information required to be sent across CPU cores
* when RPS is performed on the physical interface (vr_perfr3 is 1).
*/
typedef struct vr_rps_ {
unsigned int vif_idx;
unsigned short vif_rid;
} vr_rps_t;
/*
* pkt_gro_dev - this is a device used to do receive offload on packets
* destined over a TAP interface to a VM.
*/
static struct net_device *pkt_gro_dev = NULL;
/*
* pkt_l2_gro_dev - this is a device used to do receive offload on L2 packets
* destined over a TAP interface to a VM.
*/
static struct net_device *pkt_l2_gro_dev = NULL;
/*
* pkt_gro_dev_ops - netdevice operations on GRO packet device. Currently,
* no operations are needed, but an empty structure is required to
* register the device.
*
*/
static struct net_device_ops pkt_gro_dev_ops;
/*
* pkt_rps_dev - this is a device used to perform RPS on packets coming in
* on a physical interface.
*/
static struct net_device *pkt_rps_dev = NULL;
/*
* pkt_rps_dev_ops - netdevice operations on RPS packet device. Currently,
* no operations are needed, but an empty structure is required to
* register the device.
*
*/
static struct net_device_ops pkt_rps_dev_ops;
/*
* vr_skb_set_rxhash - set the rxhash on a skb if the kernel version
* allows it.
*/
void
vr_skb_set_rxhash(struct sk_buff *skb, __u32 val)
{
#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32))
#if defined(RHEL_MAJOR) && defined(RHEL_MINOR) && \
(RHEL_MAJOR == 6) && (RHEL_MINOR >= 4)
skb->rxhash = val;
#endif
#elif (LINUX_VERSION_CODE < KERNEL_VERSION(3,15,0))
#if defined(RHEL_MAJOR) && defined(RHEL_MINOR) && \
(RHEL_MAJOR == 7) && (RHEL_MINOR >= 2)
skb->hash = val;
#else
skb->rxhash = val;
#endif
#else
skb->hash = val;
#endif
}
/*
* vr_skb_get_rxhash - get the rxhash on a skb if the kernel version
* allows it.
*/
__u32
vr_skb_get_rxhash(struct sk_buff *skb)
{
#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32))
#if defined(RHEL_MAJOR) && defined(RHEL_MINOR) && \
(RHEL_MAJOR == 6) && (RHEL_MINOR >= 4)
return skb->rxhash;
#elif
return skb->hash;
#else
return 0;
#endif
#elif (LINUX_VERSION_CODE < KERNEL_VERSION(3,15,0))
#if defined(RHEL_MAJOR) && defined(RHEL_MINOR) && \
(RHEL_MAJOR == 7) && (RHEL_MINOR >= 2)
return skb->hash;
#else
return skb->rxhash;
#endif
#else
return skb->hash;
#endif
}
static inline struct sk_buff*
linux_skb_vlan_insert(struct sk_buff *skb, unsigned short vlan_id)
{
struct vlan_ethhdr *veth;
if (skb_cow_head(skb, VLAN_HLEN) < 0) {
lh_pfree_skb(skb, VP_DROP_MISC);
return NULL;
}
veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
/* Move the mac addresses to the beginning of the new header. */
memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
skb->mac_header -= VLAN_HLEN;
/* first, the ethernet type */
veth->h_vlan_proto = htons(ETH_P_8021Q);
/* now, the TCI */
veth->h_vlan_TCI = htons(vlan_id);
skb_reset_mac_header(skb);
skb_reset_mac_len(skb);
return skb;
}
static int
linux_if_rx(struct vr_interface *vif, struct vr_packet *pkt)
{
int rc;
struct net_device *dev = (struct net_device *)vif->vif_os;
struct sk_buff *skb = vp_os_packet(pkt);
struct vr_ip *ip;
unsigned short network_off, transport_off, cksum_off = 0;
skb->data = pkt->vp_head + pkt->vp_data;
skb->len = pkt_len(pkt);
skb_set_tail_pointer(skb, pkt_head_len(pkt));
if (!dev) {
vif_drop_pkt(vif, pkt, false);
goto exit_rx;
}
(void)__sync_fetch_and_add(&dev->stats.rx_bytes, skb->len);
(void)__sync_fetch_and_add(&dev->stats.rx_packets, 1);
/* this is only needed for mirroring */
if ((pkt->vp_flags & VP_FLAG_FROM_DP) &&
(pkt->vp_flags & VP_FLAG_CSUM_PARTIAL)) {
network_off = pkt_get_network_header_off(pkt);
ip = (struct vr_ip *)(pkt_data_at_offset(pkt, network_off));
transport_off = network_off + (ip->ip_hl * 4);
if (ip->ip_proto == VR_IP_PROTO_TCP)
cksum_off = offsetof(struct vr_tcp, tcp_csum);
else if (ip->ip_proto == VR_IP_PROTO_UDP)
cksum_off = offsetof(struct vr_udp, udp_csum);
if (cksum_off)
*(unsigned short *)
(pkt_data_at_offset(pkt, transport_off + cksum_off))
= 0;
}
skb->protocol = eth_type_trans(skb, dev);
skb->pkt_type = PACKET_HOST;
rc = netif_rx(skb);
exit_rx:
return RX_HANDLER_CONSUMED;
}
struct vrouter_gso_cb {
void (*destructor)(struct sk_buff *skb);
};
static long
linux_inet_fragment(struct vr_interface *vif, struct sk_buff *skb,
unsigned short type)
{
struct iphdr *ip = ip_hdr(skb);
unsigned int ip_hlen = ip->ihl * 4;
bool fragmented = ntohs(ip->frag_off) & IP_MF ? true : false;
unsigned int offset = (ntohs(ip->frag_off) & IP_OFFSET) << 3;
unsigned short ip_id = ntohs(ip->id);
unsigned int payload_size = skb->len - skb->mac_len - ip_hlen;
unsigned int frag_size = skb->dev->mtu - skb->mac_len - ip_hlen;
unsigned int num_frags, last_frag_len;
struct sk_buff *segs;
netdev_features_t features;
features = netif_skb_features(skb);
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39))
features &= (~(NETIF_F_ALL_TSO | NETIF_F_UFO | NETIF_F_GSO));
#else
features &= ~(NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO);
#endif
/*
* frag size has to be a multiple of 8 and last fragment has
* to be >= 64 bytes (approx)
*/
frag_size &= ~7U;
num_frags = payload_size / frag_size;
last_frag_len = payload_size % frag_size;
if (last_frag_len && last_frag_len < 64) {
frag_size -= ((64 - last_frag_len) / num_frags);
/*
* the previous division could have produced 0. to cover
* that case make some change to frag_size
*/
frag_size -= 1;
/* by doing this, we will get 8 bytes in the worst case */
frag_size &= ~7U;
}
skb_shinfo(skb)->gso_size = 0;
/*
* for packets that need checksum help, checksum has to be
* calculated here, since post fragmentation, checksum of
* individual fragments will be wrong
*/
if (skb->ip_summed == CHECKSUM_PARTIAL) {
if (skb_checksum_help(skb)) {
lh_pfree_skb(skb, VP_DROP_MISC);
return 0;
}
}
skb_shinfo(skb)->gso_size = frag_size;
/* pull till transport header */
skb_pull(skb, skb->mac_len + ip_hlen);
/*
* in 2.6.32-358.123.2.openstack.el6 kernel (and I guess all openstack
* kernels), the first field in the skb->cb is an offset field that is
* used to calculate header length. In those kernels, skb->cb is a
* structure of type skb_gso_cb with one field. Need to set that field
* to zero.
*
* This is equivalent to doing
*
* pkt->vp_head = NULL
*
* and hence access to packet structure beyond this point is suicidal
*/
memset(skb->cb, 0, sizeof(struct vrouter_gso_cb));
segs = skb_segment(skb, features);
if (IS_ERR(segs))
return PTR_ERR(segs);
kfree_skb(skb);
skb = segs;
do {
ip = ip_hdr(skb);
ip->id = htons(ip_id);
ip->frag_off = htons(offset >> 3);
if (skb->next != NULL || fragmented)
ip->frag_off |= htons(IP_MF);
offset += (skb->len - skb->mac_len - ip->ihl * 4);
ip->tot_len = htons(skb->len - skb->mac_len);
ip->check = 0;
ip->check = ip_fast_csum(skb_network_header(skb), ip->ihl);
} while ((skb = skb->next));
return linux_xmit_segments(vif, segs, type);
}
static int
linux_xmit(struct vr_interface *vif, struct sk_buff *skb,
unsigned short type)
{
if (vif->vif_type == VIF_TYPE_VIRTUAL &&
skb->ip_summed == CHECKSUM_NONE)
skb->ip_summed = CHECKSUM_UNNECESSARY;
if ((type == VP_TYPE_IPOIP) &&
(skb->len > skb->dev->mtu + skb->dev->hard_header_len))
return linux_inet_fragment(vif, skb, type);
return dev_queue_xmit(skb);
}
static int
linux_xmit_segment(struct vr_interface *vif, struct sk_buff *seg,
unsigned short type)
{
int err = -ENOMEM;
struct vr_ip *iph, *i_iph = NULL;
unsigned short iphlen;
unsigned short ethlen;
struct udphdr *udph;
unsigned short reason = 0;
/* we will do tunnel header updates after the fragmentation */
if (seg->len > seg->dev->mtu + seg->dev->hard_header_len
|| !vr_pkt_type_is_overlay(type)) {
return linux_xmit(vif, seg, type);
}
if (seg->dev->type == ARPHRD_ETHER) {
ethlen = ETH_HLEN;
} else {
ethlen = 0;
}
if (!pskb_may_pull(seg, ethlen + sizeof(struct vr_ip))) {
reason = VP_DROP_PULL;
goto exit_xmit;
}
iph = (struct vr_ip *)(seg->data + ethlen);
iphlen = (iph->ip_hl << 2);
if (!pskb_may_pull(seg, ethlen + iphlen)) {
reason = VP_DROP_PULL;
goto exit_xmit;
}
iph = (struct vr_ip *)(seg->data + ethlen);
iph->ip_len = htons(seg->len - ethlen);
if (type == VP_TYPE_IPOIP)
i_iph = (struct vr_ip *)skb_network_header(seg);
/*
* it is important that we copy the inner network header's
* ip id to outer. For now, agent diagnostics (traceroute)
* depends on this behavior.
*/
if (i_iph)
iph->ip_id = i_iph->ip_id;
else
iph->ip_id = htons(vr_generate_unique_ip_id());
iph->ip_csum = 0;
iph->ip_csum = ip_fast_csum(iph, iph->ip_hl);
if (iph->ip_proto == VR_IP_PROTO_UDP) {
if (!pskb_may_pull(seg, ethlen + iphlen +
sizeof(struct udphdr))) {
reason = VP_DROP_PULL;
goto exit_xmit;
}
if (vr_udp_coff) {
skb_set_network_header(seg, ethlen);
iph->ip_csum = 0;
skb_set_transport_header(seg, iphlen + ethlen);
if (!skb_partial_csum_set(seg, skb_transport_offset(seg),
offsetof(struct udphdr, check))) {
reason = VP_DROP_MISC;
goto exit_xmit;
}
udph = (struct udphdr *) skb_transport_header(seg);
udph->len = htons(seg->len - skb_transport_offset(seg));
iph->ip_csum = ip_fast_csum(iph, iph->ip_hl);
udph->check = ~csum_tcpudp_magic(iph->ip_saddr, iph->ip_daddr,
htons(udph->len),
IPPROTO_UDP, 0);
} else {
/*
* If we are encapsulating a L3/L2 packet in UDP, set the UDP
* checksum to 0 and let the NIC calculate the checksum of the
* inner packet (if the NIC supports it).
*/
udph = (struct udphdr *) (((char *)iph) + iphlen);
udph->len = htons(seg->len - (ethlen + iphlen));
udph->check = 0;
iph->ip_csum = 0;
iph->ip_csum = ip_fast_csum(iph, iph->ip_hl);
if ((vif->vif_flags & VIF_FLAG_TX_CSUM_OFFLOAD) == 0) {
if (seg->ip_summed == CHECKSUM_PARTIAL) {
skb_checksum_help(seg);
}
}
}
} else if (iph->ip_proto == VR_IP_PROTO_GRE) {
if ((vif->vif_flags & VIF_FLAG_TX_CSUM_OFFLOAD) == 0) {
if (seg->ip_summed == CHECKSUM_PARTIAL) {
skb_checksum_help(seg);
}
}
}
return linux_xmit(vif, seg, type);
exit_xmit:
lh_pfree_skb(seg, reason);
return err;
}
static int
linux_xmit_segments(struct vr_interface *vif, struct sk_buff *segs,
unsigned short type)
{
int err;
struct sk_buff *nskb = NULL;
do {
nskb = segs->next;
segs->next = NULL;
if ((err = linux_xmit_segment(vif, segs, type)))
break;
segs = nskb;
} while (segs);
segs = nskb;
while (segs) {
nskb = segs->next;
segs->next = NULL;
kfree_skb(segs);
segs = nskb;
}
return err;
}
/*
* linux_gso_xmit - perform segmentation of the inner packet in software
* and send each segment out the wire after fixing the outer header.
*/
static void
linux_gso_xmit(struct vr_interface *vif, struct sk_buff *skb,
unsigned short type)
{
netdev_features_t features;
struct sk_buff *segs;
unsigned short seg_size = skb_shinfo(skb)->gso_size;
struct iphdr *ip = ip_hdr(skb);
struct tcphdr *th;
struct net_device *ndev = (struct net_device *)vif->vif_os;
features = netif_skb_features(skb);
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39))
features &= (~(NETIF_F_ALL_TSO | NETIF_F_UFO | NETIF_F_GSO));
#else
features &= (~(NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO));
#endif
seg_size += skb->mac_len + skb_network_header_len(skb);
/*
* We are trying to find whether the total size of the packet will
* overshoot the mtu. Above, we have accounted for the tunnel headers,
* the inner ip header, and the segment size. However, there is a
* subtle difference in deciding whether transport header is part of
* GSO size or not.
*
* For TCP, segment size (gso size) is the ip data length - tcp header
* length (since each segment goes with tcp header), while for udp, there
* are only fragments (and no segments) and the segment size (fragment
* size) is the ip data length adjusted to mtu (since udp header goes
* only with the first fragment). Hence the following condition
*/
if (ip->protocol == IPPROTO_TCP) {
th = tcp_hdr(skb);
seg_size += (th->doff * 4);
}
/*
* avoid fragmentation after segmentation.
*/
if (seg_size > ndev->mtu + ndev->hard_header_len) {
skb_shinfo(skb)->gso_size -= (seg_size - ndev->mtu -
ndev->hard_header_len);
if (ip->protocol == IPPROTO_UDP)
skb_shinfo(skb)->gso_size &= ~7;
}
segs = skb_gso_segment(skb, features);
kfree_skb(skb);
if ((IS_ERR(segs)) || (segs == NULL)) {
return;
}
linux_xmit_segments(vif, segs, type);
return;
}
#ifdef CONFIG_RPS
/*
* linux_get_rxq - get a receive queue for the packet on an interface that
* has RPS enabled. The receive queue is picked such that it is different
* from the current CPU core and the previous CPU core that handled the
* packet (if the previous core is specified). The receive queue has a 1-1
* mapping to the receiving CPU core (i.e. queue 1 corresponds to CPU core 0,
* queue 2 to CPU core 1 and so on). The CPU core is chosen such that it is
* on the same NUMA node as the current core (to minimize memory access
* latency across NUMA nodes), except that hyper-threads of the current
* and previous core are excluded as choices for the next CPU to process the
* packet.
*/
static void
linux_get_rxq(struct sk_buff *skb, u16 *rxq, unsigned int curr_cpu,
unsigned int prev_cpu)
{
unsigned int next_cpu;
int numa_node = cpu_to_node(curr_cpu);
const struct cpumask *node_cpumask = cpumask_of_node(numa_node);
struct cpumask noht_cpumask;
unsigned int num_cpus, cpu, count = 0;
__u32 rxhash;
/*
* We are running in softirq context, so CPUs can't be offlined
* underneath us. So, it is safe to use the NUMA node CPU bitmaps.
* Clear the bits corresponding to the current core and its hyperthreads
* in the node CPU mask.
*/
cpumask_andnot(&noht_cpumask, node_cpumask, cpu_sibling_mask(curr_cpu));
/*
* If the previous CPU is specified, clear the bits corresponding to
* that core and its hyperthreads in the CPU mask.
*/
if (prev_cpu && (prev_cpu <= nr_cpu_ids)) {
cpumask_andnot(&noht_cpumask, &noht_cpumask,
cpu_sibling_mask(prev_cpu-1));
}
num_cpus = cpumask_weight(&noht_cpumask);
if (num_cpus) {
rxhash = skb_get_rxhash(skb);
#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32))
next_cpu = ((u32)rxhash * num_cpus) >> 16;
#else
next_cpu = ((u64)rxhash * num_cpus) >> 32;
#endif
/*
* next_cpu is between 0 and (num_cpus - 1). Find the CPU corresponding
* to next_cpu in the CPU bitmask.
*/
for_each_cpu(cpu, &noht_cpumask) {
if (count == next_cpu) {
break;
}
count++;
}
if (cpu >= nr_cpu_ids) {
/*
* Shouldn't happen
*/
*rxq = curr_cpu;
} else {
*rxq = cpu;
}
} else {
/*
* Not enough CPU cores available in this NUMA node. Continue
* processing the packet on the same CPU core.
*/
*rxq = curr_cpu;
}
return;
}
#endif
/*
* linux_enqueue_pkt_for_gro - enqueue packet on a list of skbs and schedule a
* NAPI event on the NAPI structure of the vif.
*
*/
void
linux_enqueue_pkt_for_gro(struct sk_buff *skb, struct vr_interface *vif,
bool l2_pkt)
{
struct vr_interface *gro_vif;
struct vr_interface_stats *gro_vif_stats;
int in_intr_context;
struct sk_buff_head *head;
struct napi_struct *napi;
if (l2_pkt) {
skb->dev = pkt_l2_gro_dev;
gro_vif = pkt_l2_gro_dev->ml_priv;
head = &vif->vr_skb_l2_inputq;
napi = &vif->vr_l2_napi;
} else {
skb->dev = pkt_gro_dev;
gro_vif = pkt_gro_dev->ml_priv;
head = &vif->vr_skb_inputq;
napi = &vif->vr_napi;
}
if (gro_vif) {
gro_vif_stats = vif_get_stats(gro_vif, vr_get_cpu());
if (gro_vif_stats) {
gro_vif_stats->vis_opackets++;
gro_vif_stats->vis_obytes += skb->len;
}
}
skb_queue_tail(head, skb);
/*
* napi_schedule may raise a softirq, so if we are not already in
* interrupt context (which is the case when we get here as a result of
* the agent enabling a flow for forwarding), ensure that the softirq is
* handled immediately.
*/
in_intr_context = in_interrupt();
if (!in_intr_context) {
local_bh_disable();
}
napi_schedule(napi);
if (!in_intr_context) {
local_bh_enable();
}
return;
}
#if 0
static void __skb_dump_info(const char *prefix, const struct sk_buff *skb,
struct vr_interface *vif)
{
#ifdef CONFIG_XEN
int i, nr = skb_shinfo(skb)->nr_frags;
#endif
struct ethhdr *ethh = eth_hdr(skb);
struct iphdr *iph = NULL;
struct tcphdr *tcph = NULL;
printk("vif info: type=%d id=%d os_id=%d\n",
vif->vif_type, vif->vif_idx, vif->vif_os_idx);
printk(KERN_CRIT "%s: len is %#x (data:%#x mac:%#x) truesize %#x\n", prefix,
skb->len, skb->data_len, skb->mac_len, skb->truesize);
printk(KERN_CRIT "%s: linear:%s\n", prefix,
skb_is_nonlinear(skb) ? "No" : "Yes");
printk(KERN_CRIT "%s: data %p head %p tail %p end %p\n", prefix,
skb->data, skb->head, skb->tail, skb->end);
printk(KERN_CRIT "%s: flags are local_df:%d cloned:%d ip_summed:%d"
"nohdr:%d\n", prefix, skb->local_df, skb->cloned,
skb->ip_summed, skb->nohdr);
printk(KERN_CRIT "%s: nfctinfo:%d pkt_type:%d fclone:%d ipvs_property:%d\n",
prefix, skb->nfctinfo, skb->pkt_type,
skb->nohdr, skb->ipvs_property);
printk(KERN_CRIT "%s: shared info %p ref %#x\n", prefix,
skb_shinfo(skb), atomic_read(&skb_shinfo(skb)->dataref));
printk(KERN_CRIT "%s: frag_list %p\n", prefix,
skb_shinfo(skb)->frag_list);
if (ethh) {
printk(KERN_CRIT "%s: eth: (%p) src:%pM dest:%pM proto %u\n",
prefix, ethh, ethh->h_source, ethh->h_dest, ntohs(ethh->h_proto));
if (ethh->h_proto == __constant_htons(ETH_P_IP))
iph = ip_hdr(skb);
} else
printk(KERN_CRIT "%s: eth: header not present\n", prefix);
if (iph) {
printk(KERN_CRIT "%s: ip: (%p) saddr "NIPQUAD_FMT" daddr "NIPQUAD_FMT"\
protocol %d frag_off %d\n", prefix, iph, NIPQUAD(iph->saddr),
NIPQUAD(iph->daddr), iph->protocol, iph->frag_off);
if (iph->protocol == IPPROTO_TCP)
tcph = tcp_hdr(skb);
} else
printk(KERN_CRIT "%s: ip: header not present\n", prefix);
if (tcph) {
printk(KERN_CRIT "%s: tcp: (%p) source %d dest %d seq %u ack %u\n",
prefix, tcph, ntohs(tcph->source), ntohs(tcph->dest),
ntohl(tcph->seq), ntohl(tcph->ack_seq));
} else
printk(KERN_CRIT "%s: tcp: header not present\n", prefix);
#ifdef CONFIG_XEN
printk(KERN_CRIT "%s: nr_frags %d\n", prefix, nr);
for(i=0; i<nr; i++) {
skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
unsigned long pfn = page_to_pfn(frag->page);
unsigned long mfn = pfn_to_mfn(pfn);
printk(KERN_CRIT "%s: %d/%d page:%p count:%d offset:%#x size:%#x \
virt:%p pfn:%#lx mfn:%#lx%s flags:%lx%s%s)\n",
prefix, i + 1, nr, frag->page,
atomic_read(&frag->page->_count),
frag->page_offset, frag->size,
phys_to_virt(page_to_pseudophys(frag->page)), pfn, mfn,
phys_to_machine_mapping_valid(pfn) ? "" : "(BAD)",
frag->page->flags,
PageForeign(frag->page) ? " FOREIGN" : "",
PageBlkback(frag->page) ? " BLKBACK" : "");
}
#endif
}
#endif
static int
linux_if_tx(struct vr_interface *vif, struct vr_packet *pkt)
{
struct net_device *dev = (struct net_device *)vif->vif_os;
struct sk_buff *skb = vp_os_packet(pkt);
struct skb_shared_info *sinfo;
struct vr_ip *ip;
struct vr_ip6 *ip6;
int proto;
unsigned short network_off, transport_off, cksum_off;
skb->data = pkt_data(pkt);
skb->len = pkt_len(pkt);
skb_set_tail_pointer(skb, pkt_head_len(pkt));
skb->dev = dev;
if (!dev) {
vif_drop_pkt(vif, pkt, false);
return 0;
}
skb_reset_mac_header(skb);
/*
* Set the network header and trasport header of skb only if the type is
* IP (tunnel or non tunnel). This is required for those packets where
* a new buffer is added at the head. Also, set it for packets from the
* agent, which get sent to the NIC driver (to handle cases where the
* NIC has hw vlan acceleration enabled).
*/
if (pkt->vp_type == VP_TYPE_AGENT) {
network_off = pkt_get_inner_network_header_off(pkt);
if (network_off) {
skb_set_network_header(skb, (network_off - skb_headroom(skb)));
skb_reset_mac_len(skb);
}
} else if (vr_pkt_type_is_overlay(pkt->vp_type) ||
vr_pkt_needs_csum_gso_update(pkt)) {
network_off = pkt_get_inner_network_header_off(pkt);
if (network_off) {
ip = (struct vr_ip *)(pkt_data_at_offset(pkt, network_off));
if (vr_ip_is_ip4(ip)) {
transport_off = network_off + (ip->ip_hl * 4);
proto = ip->ip_proto;
} else if (vr_ip_is_ip6(ip)) {
ip6 = (struct vr_ip6 *)ip;
transport_off = network_off + sizeof(struct vr_ip6);
proto = ip6->ip6_nxt;
} else {
lh_pfree_skb(skb, VP_DROP_INVALID_PROTOCOL);
return 0;
}
skb_set_network_header(skb, (network_off - skb_headroom(skb)));
skb_reset_mac_len(skb);
skb_set_transport_header(skb, (transport_off - skb_headroom(skb)));
/*
* Manipulate partial checksum fields.
* There are cases like mirroring where the UDP headers are newly added
* and skb needs to be filled with proper offsets. The vr_packet's fields
* are latest values and they need to be reflected in skb
*/
if (pkt->vp_flags & VP_FLAG_CSUM_PARTIAL) {
cksum_off = skb->csum_offset;
if (proto == VR_IP_PROTO_TCP)
cksum_off = offsetof(struct vr_tcp, tcp_csum);
else if (proto == VR_IP_PROTO_UDP)
cksum_off = offsetof(struct vr_udp, udp_csum);
skb_partial_csum_set(skb, (transport_off - skb_headroom(skb)), cksum_off);
}
/*
* Invoke segmentation only incase of both vr_packet and skb having gso
*/
if ((pkt->vp_flags & VP_FLAG_GSO) && skb_is_gso(skb)) {
/*
* it is possible that when we mirrored the packet, the inner
* packet was meant to be GSO-ed, and that would have been a
* TCP packet. Since we carried over the gso type from the inner
* packet, the value will be wrong, and that's where the following
* check comes into picture
*/
if (proto == VR_IP_PROTO_UDP) {
sinfo = skb_shinfo(skb);
if (!(sinfo->gso_type & SKB_GSO_UDP)) {
sinfo->gso_type &= ~(SKB_GSO_TCPV4 | SKB_GSO_TCP_ECN |
SKB_GSO_TCPV6 | SKB_GSO_FCOE);
sinfo->gso_type |= SKB_GSO_UDP;
}
}
if (vif->vif_type == VIF_TYPE_PHYSICAL) {
linux_gso_xmit(vif, skb, pkt->vp_type);
return 0;
}
}
}
}
linux_xmit_segment(vif, skb, pkt->vp_type);
return 0;
}
inline struct vr_packet *
linux_get_packet(struct sk_buff *skb, struct vr_interface *vif)
{
struct vr_packet *pkt;
unsigned int length;
pkt = (struct vr_packet *)skb->cb;
pkt->vp_cpu = vr_get_cpu();
pkt->vp_head = skb->head;
length = skb_tail_pointer(skb) - skb->head;
if (length >= (1 << (sizeof(pkt->vp_tail) * 8)))
goto drop;
pkt->vp_tail = length;
length = skb->data - skb->head;
if (length >= (1 << (sizeof(pkt->vp_data) * 8)))
goto drop;
pkt->vp_data = length;
length = skb_end_pointer(skb) - skb->head;
if (length >= (1 << (sizeof(pkt->vp_end) * 8)))
goto drop;
pkt->vp_end = length;
pkt->vp_len = skb_headlen(skb);
pkt->vp_if = vif;
pkt->vp_network_h = pkt->vp_inner_network_h = 0;
pkt->vp_nh = NULL;
pkt->vp_flags = 0;
if (skb->ip_summed == CHECKSUM_PARTIAL)
pkt->vp_flags |= VP_FLAG_CSUM_PARTIAL;
pkt->vp_ttl = 64;
pkt->vp_type = VP_TYPE_NULL;
return pkt;
drop:
vr_pfree(pkt, VP_DROP_INVALID_PACKET);
return NULL;
}
int
linux_to_vr(struct vr_interface *vif, struct sk_buff *skb)
{
struct vr_packet *pkt;
if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
return 0;
pkt = linux_get_packet(skb, vif);
if (!pkt)
return 0;
vif->vif_rx(vif, pkt, VLAN_ID_INVALID);
return 0;
}
static int
linux_pull_outer_headers(struct sk_buff *skb)
{
struct vlan_hdr *vhdr;
bool thdr = false, pull = false;
uint16_t proto, offset, ip_proto = 0;
struct iphdr *iph = NULL;
struct ipv6hdr *ip6h = NULL;
struct vr_icmp *icmph;
offset = skb->mac_len;
proto = skb->protocol;
while (proto == htons(ETH_P_8021Q)) {
offset += sizeof(struct vlan_hdr);
if (!pskb_may_pull(skb, offset))
goto pull_fail;
vhdr = (struct vlan_hdr *)(skb->data + offset);
proto = vhdr->h_vlan_encapsulated_proto;
}
if (likely(proto == htons(ETH_P_IP))) {
skb_set_network_header(skb, offset);
offset += sizeof(struct iphdr);
if (!pskb_may_pull(skb, offset))
goto pull_fail;
iph = ip_hdr(skb);
offset += (iph->ihl * 4) - sizeof(struct iphdr);
if (!pskb_may_pull(skb, offset))
goto pull_fail;
iph = ip_hdr(skb);
thdr = vr_ip_transport_header_valid((struct vr_ip *)iph);
pull = vr_ip_proto_pull((struct vr_ip *)iph);
if (pull && thdr) {
ip_proto = iph->protocol;
}
} else if (proto == htons(ETH_P_IPV6)) {
skb_set_network_header(skb, offset);
offset += sizeof(struct ipv6hdr);
if (!pskb_may_pull(skb, offset))
goto pull_fail;
ip6h = ipv6_hdr(skb);
thdr = true;
pull = vr_ip6_proto_pull((struct vr_ip6 *)ip6h);
if (pull) {
ip_proto = ip6h->nexthdr;
}
} else if (proto == htons(ETH_P_ARP)) {
offset += sizeof(struct vr_arp);
if (!pskb_may_pull(skb, offset))
goto pull_fail;
}
if (thdr && pull && (iph || ip6h)) {
if (ip_proto == VR_IP_PROTO_TCP) {
offset += sizeof(struct vr_tcp);
if (!pskb_may_pull(skb, offset))
goto pull_fail;
} else {
/*
* this covers both regular port number offsets that come in
* the first 4 bytes and the icmp header
*/
offset += sizeof(struct vr_icmp);
if (!pskb_may_pull(skb, offset))
goto pull_fail;
}
if (iph)