datapath: Check for backported netdev_features_t.
[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(netdev_features_t 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 netdev_features_t harmonize_features(struct sk_buff *skb,
35                                             __be16 protocol,
36                                             netdev_features_t features)
37 {
38         if (!can_checksum_protocol(features, protocol)) {
39                 features &= ~NETIF_F_ALL_CSUM;
40                 features &= ~NETIF_F_SG;
41         } else if (illegal_highdma(skb->dev, skb)) {
42                 features &= ~NETIF_F_SG;
43         }
44
45         return features;
46 }
47
48 netdev_features_t rpl_netif_skb_features(struct sk_buff *skb)
49 {
50         unsigned long vlan_features = skb->dev->vlan_features;
51
52         __be16 protocol = skb->protocol;
53         netdev_features_t features = skb->dev->features;
54
55         if (protocol == htons(ETH_P_8021Q)) {
56                 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
57                 protocol = veh->h_vlan_encapsulated_proto;
58         } else if (!vlan_tx_tag_present(skb)) {
59                 return harmonize_features(skb, protocol, features);
60         }
61
62         features &= (vlan_features | NETIF_F_HW_VLAN_TX);
63
64         if (protocol != htons(ETH_P_8021Q)) {
65                 return harmonize_features(skb, protocol, features);
66         } else {
67                 features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
68                         NETIF_F_GEN_CSUM | NETIF_F_HW_VLAN_TX;
69                 return harmonize_features(skb, protocol, features);
70         }
71 }
72
73 struct sk_buff *rpl_skb_gso_segment(struct sk_buff *skb,
74                                     netdev_features_t features)
75 {
76         int vlan_depth = ETH_HLEN;
77         __be16 type = skb->protocol;
78         __be16 skb_proto;
79         struct sk_buff *skb_gso;
80
81         while (type == htons(ETH_P_8021Q)) {
82                 struct vlan_hdr *vh;
83
84                 if (unlikely(!pskb_may_pull(skb, vlan_depth + VLAN_HLEN)))
85                         return ERR_PTR(-EINVAL);
86
87                 vh = (struct vlan_hdr *)(skb->data + vlan_depth);
88                 type = vh->h_vlan_encapsulated_proto;
89                 vlan_depth += VLAN_HLEN;
90         }
91
92         /* this hack needed to get regular skb_gso_segment() */
93 #undef skb_gso_segment
94         skb_proto = skb->protocol;
95         skb->protocol = type;
96
97         skb_gso = skb_gso_segment(skb, features);
98         skb->protocol = skb_proto;
99         return skb_gso;
100 }
101 #endif  /* kernel version < 2.6.38 */