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