datapath: Fix coding style issues.
[sliver-openvswitch.git] / datapath / checksum.h
1 /*
2  * Copyright (c) 2010, 2011 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #ifndef CHECKSUM_H
10 #define CHECKSUM_H 1
11
12 #include <linux/skbuff.h>
13 #include <linux/version.h>
14
15 #include <net/checksum.h>
16
17 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22) || \
18         (defined(CONFIG_XEN) && defined(HAVE_PROTO_DATA_VALID))
19 #define NEED_CSUM_NORMALIZE
20 #endif
21
22 /* These are the same values as the checksum constants in 2.6.22+. */
23 enum csum_type {
24         OVS_CSUM_NONE = 0,
25         OVS_CSUM_UNNECESSARY = 1,
26         OVS_CSUM_COMPLETE = 2,
27         OVS_CSUM_PARTIAL = 3,
28 };
29
30 #ifdef NEED_CSUM_NORMALIZE
31 int compute_ip_summed(struct sk_buff *skb, bool xmit);
32 void forward_ip_summed(struct sk_buff *skb, bool xmit);
33 u8 get_ip_summed(struct sk_buff *skb);
34 void set_ip_summed(struct sk_buff *skb, u8 ip_summed);
35 void get_skb_csum_pointers(const struct sk_buff *skb, u16 *csum_start,
36                            u16 *csum_offset);
37 void set_skb_csum_pointers(struct sk_buff *skb, u16 csum_start,
38                            u16 csum_offset);
39 #else
40 static inline int compute_ip_summed(struct sk_buff *skb, bool xmit)
41 {
42         return 0;
43 }
44
45 static inline void forward_ip_summed(struct sk_buff *skb, bool xmit) { }
46
47 static inline u8 get_ip_summed(struct sk_buff *skb)
48 {
49         return skb->ip_summed;
50 }
51
52 static inline void set_ip_summed(struct sk_buff *skb, u8 ip_summed)
53 {
54         skb->ip_summed = ip_summed;
55 }
56
57 static inline void get_skb_csum_pointers(const struct sk_buff *skb,
58                                          u16 *csum_start, u16 *csum_offset)
59 {
60         *csum_start = skb->csum_start;
61         *csum_offset = skb->csum_offset;
62 }
63
64 static inline void set_skb_csum_pointers(struct sk_buff *skb, u16 csum_start,
65                                          u16 csum_offset)
66 {
67         skb->csum_start = csum_start;
68         skb->csum_offset = csum_offset;
69 }
70 #endif
71
72 /* This is really compatibility code that belongs in the compat directory.
73  * However, it needs access to our normalized checksum values, so put it here.
74  */
75 #if defined(NEED_CSUM_NORMALIZE) || LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
76 #define inet_proto_csum_replace4 rpl_inet_proto_csum_replace4
77 static inline void inet_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
78                                             __be32 from, __be32 to,
79                                             int pseudohdr)
80 {
81         __be32 diff[] = { ~from, to };
82
83         if (get_ip_summed(skb) != OVS_CSUM_PARTIAL) {
84                 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
85                                 ~csum_unfold(*sum)));
86                 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE && pseudohdr)
87                         skb->csum = ~csum_partial((char *)diff, sizeof(diff),
88                                                 ~skb->csum);
89         } else if (pseudohdr)
90                 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
91                                 csum_unfold(*sum)));
92 }
93 #endif
94
95 #ifdef NEED_CSUM_NORMALIZE
96 static inline void update_csum_start(struct sk_buff *skb, int delta)
97 {
98         if (get_ip_summed(skb) == OVS_CSUM_PARTIAL) {
99                 u16 csum_start, csum_offset;
100
101                 get_skb_csum_pointers(skb, &csum_start, &csum_offset);
102                 set_skb_csum_pointers(skb, csum_start + delta, csum_offset);
103         }
104 }
105
106 static inline int rpl_pskb_expand_head(struct sk_buff *skb, int nhead,
107                                        int ntail, gfp_t gfp_mask)
108 {
109         int err;
110         int old_headroom = skb_headroom(skb);
111
112         err = pskb_expand_head(skb, nhead, ntail, gfp_mask);
113         if (unlikely(err))
114                 return err;
115
116         update_csum_start(skb, skb_headroom(skb) - old_headroom);
117
118         return 0;
119 }
120 #define pskb_expand_head rpl_pskb_expand_head
121
122 static inline unsigned char *rpl__pskb_pull_tail(struct sk_buff *skb,
123                                                   int delta)
124 {
125         unsigned char *ret;
126         int old_headroom = skb_headroom(skb);
127
128         ret = __pskb_pull_tail(skb, delta);
129         if (unlikely(!ret))
130                 return ret;
131
132         update_csum_start(skb, skb_headroom(skb) - old_headroom);
133
134         return ret;
135 }
136 #define __pskb_pull_tail rpl__pskb_pull_tail
137 #endif
138
139 #endif /* checksum.h */