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