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