datapath: Account for RHEL6.4 backports in compat layer
[sliver-openvswitch.git] / datapath / linux / compat / netdevice.c
1 #include <linux/netdevice.h>
2 #include <linux/if_vlan.h>
3
4 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
5 #ifndef HAVE_CAN_CHECKSUM_PROTOCOL
6 static bool can_checksum_protocol(unsigned long features, __be16 protocol)
7 {
8         return  ((features & NETIF_F_GEN_CSUM) ||
9                 ((features & NETIF_F_V4_CSUM) &&
10                                 protocol == htons(ETH_P_IP)) ||
11                 ((features & NETIF_F_V6_CSUM) &&
12                                 protocol == htons(ETH_P_IPV6)) ||
13                 ((features & NETIF_F_FCOE_CRC) &&
14                                 protocol == htons(ETH_P_FCOE)));
15 }
16 #endif
17
18 static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
19 {
20 #ifdef CONFIG_HIGHMEM
21         int i;
22
23         if (dev->features & NETIF_F_HIGHDMA)
24                 return 0;
25
26         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
27                 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
28                         return 1;
29
30 #endif
31         return 0;
32 }
33
34 static u32 harmonize_features(struct sk_buff *skb, __be16 protocol, u32 features)
35 {
36         if (!can_checksum_protocol(features, protocol)) {
37                 features &= ~NETIF_F_ALL_CSUM;
38                 features &= ~NETIF_F_SG;
39         } else if (illegal_highdma(skb->dev, skb)) {
40                 features &= ~NETIF_F_SG;
41         }
42
43         return features;
44 }
45
46 u32 rpl_netif_skb_features(struct sk_buff *skb)
47 {
48 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
49         unsigned long vlan_features = 0;
50 #else
51         unsigned long vlan_features = skb->dev->vlan_features;
52 #endif /* kernel version < 2.6.26 */
53
54         __be16 protocol = skb->protocol;
55         u32 features = skb->dev->features;
56
57         if (protocol == htons(ETH_P_8021Q)) {
58                 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
59                 protocol = veh->h_vlan_encapsulated_proto;
60         } else if (!vlan_tx_tag_present(skb)) {
61                 return harmonize_features(skb, protocol, features);
62         }
63
64         features &= (vlan_features | NETIF_F_HW_VLAN_TX);
65
66         if (protocol != htons(ETH_P_8021Q)) {
67                 return harmonize_features(skb, protocol, features);
68         } else {
69                 features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
70                         NETIF_F_GEN_CSUM | NETIF_F_HW_VLAN_TX;
71                 return harmonize_features(skb, protocol, features);
72         }
73 }
74
75 struct sk_buff *rpl_skb_gso_segment(struct sk_buff *skb, u32 features)
76 {
77         int vlan_depth = ETH_HLEN;
78         __be16 type = skb->protocol;
79         __be16 skb_proto;
80         struct sk_buff *skb_gso;
81
82         while (type == htons(ETH_P_8021Q)) {
83                 struct vlan_hdr *vh;
84
85                 if (unlikely(!pskb_may_pull(skb, vlan_depth + VLAN_HLEN)))
86                         return ERR_PTR(-EINVAL);
87
88                 vh = (struct vlan_hdr *)(skb->data + vlan_depth);
89                 type = vh->h_vlan_encapsulated_proto;
90                 vlan_depth += VLAN_HLEN;
91         }
92
93         /* this hack needed to get regular skb_gso_segment() */
94 #undef skb_gso_segment
95         skb_proto = skb->protocol;
96         skb->protocol = type;
97
98         skb_gso = skb_gso_segment(skb, features);
99         skb->protocol = skb_proto;
100         return skb_gso;
101 }
102 #endif  /* kernel version < 2.6.38 */