datapath: Backport "skbuff: Fix skb checksum flag on skb pull"
[cascardo/ovs.git] / datapath / linux / compat / include / linux / skbuff.h
1 #ifndef __LINUX_SKBUFF_WRAPPER_H
2 #define __LINUX_SKBUFF_WRAPPER_H 1
3
4 #include <linux/version.h>
5 #include <linux/types.h>
6
7 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0)
8 /* This should be before skbuff.h to make sure that we rewrite
9  * the calls there. */
10 struct sk_buff;
11
12 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
13                      gfp_t gfp_mask);
14 #define pskb_expand_head rpl_pskb_expand_head
15 #endif
16
17 #include_next <linux/skbuff.h>
18
19 #include <linux/jhash.h>
20
21 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
22 #define SKB_GSO_GRE 0
23 #endif
24
25 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,10,0)
26 #define SKB_GSO_UDP_TUNNEL 0
27 #endif
28
29 #ifndef HAVE_SKB_GSO_GRE_CSUM
30 #define SKB_GSO_GRE_CSUM 0
31 #endif
32
33 #ifndef HAVE_SKB_GSO_UDP_TUNNEL_CSUM
34 #define SKB_GSO_UDP_TUNNEL_CSUM 0
35 #endif
36
37 #ifndef HAVE_IGNORE_DF_RENAME
38 #define ignore_df local_df
39 #endif
40
41 #ifndef HAVE_SKB_COPY_FROM_LINEAR_DATA_OFFSET
42 static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb,
43                                                     const int offset, void *to,
44                                                     const unsigned int len)
45 {
46         memcpy(to, skb->data + offset, len);
47 }
48
49 static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb,
50                                                   const int offset,
51                                                   const void *from,
52                                                   const unsigned int len)
53 {
54         memcpy(skb->data + offset, from, len);
55 }
56
57 #endif  /* !HAVE_SKB_COPY_FROM_LINEAR_DATA_OFFSET */
58
59 #ifndef HAVE_SKB_RESET_TAIL_POINTER
60 static inline void skb_reset_tail_pointer(struct sk_buff *skb)
61 {
62         skb->tail = skb->data;
63 }
64 #endif
65 /*
66  * The networking layer reserves some headroom in skb data (via
67  * dev_alloc_skb). This is used to avoid having to reallocate skb data when
68  * the header has to grow. In the default case, if the header has to grow
69  * 16 bytes or less we avoid the reallocation.
70  *
71  * Unfortunately this headroom changes the DMA alignment of the resulting
72  * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
73  * on some architectures. An architecture can override this value,
74  * perhaps setting it to a cacheline in size (since that will maintain
75  * cacheline alignment of the DMA). It must be a power of 2.
76  *
77  * Various parts of the networking layer expect at least 16 bytes of
78  * headroom, you should not reduce this.
79  */
80 #ifndef NET_SKB_PAD
81 #define NET_SKB_PAD     16
82 #endif
83
84 #ifndef HAVE_SKB_COW_HEAD
85 static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
86                             int cloned)
87 {
88         int delta = 0;
89
90         if (headroom < NET_SKB_PAD)
91                 headroom = NET_SKB_PAD;
92         if (headroom > skb_headroom(skb))
93                 delta = headroom - skb_headroom(skb);
94
95         if (delta || cloned)
96                 return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
97                                         GFP_ATOMIC);
98         return 0;
99 }
100
101 static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
102 {
103         return __skb_cow(skb, headroom, skb_header_cloned(skb));
104 }
105 #endif  /* !HAVE_SKB_COW_HEAD */
106
107 #ifndef HAVE_SKB_DST_ACCESSOR_FUNCS
108 static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
109 {
110         return (struct dst_entry *)skb->dst;
111 }
112
113 static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
114 {
115         skb->dst = dst;
116 }
117
118 static inline struct rtable *skb_rtable(const struct sk_buff *skb)
119 {
120         return (struct rtable *)skb->dst;
121 }
122 #endif
123
124 #ifndef CHECKSUM_PARTIAL
125 #define CHECKSUM_PARTIAL CHECKSUM_HW
126 #endif
127 #ifndef CHECKSUM_COMPLETE
128 #define CHECKSUM_COMPLETE CHECKSUM_HW
129 #endif
130
131 #ifndef HAVE_SKBUFF_HEADER_HELPERS
132 static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
133 {
134         return skb->h.raw;
135 }
136
137 static inline void skb_reset_transport_header(struct sk_buff *skb)
138 {
139         skb->h.raw = skb->data;
140 }
141
142 static inline void skb_set_transport_header(struct sk_buff *skb,
143                         const int offset)
144 {
145         skb->h.raw = skb->data + offset;
146 }
147
148 static inline unsigned char *skb_network_header(const struct sk_buff *skb)
149 {
150         return skb->nh.raw;
151 }
152
153 static inline void skb_reset_network_header(struct sk_buff *skb)
154 {
155         skb->nh.raw = skb->data;
156 }
157
158 static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
159 {
160         skb->nh.raw = skb->data + offset;
161 }
162
163 static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
164 {
165         return skb->mac.raw;
166 }
167
168 static inline void skb_reset_mac_header(struct sk_buff *skb)
169 {
170         skb->mac_header = skb->data;
171 }
172
173 static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
174 {
175         skb->mac.raw = skb->data + offset;
176 }
177
178 static inline int skb_transport_offset(const struct sk_buff *skb)
179 {
180         return skb_transport_header(skb) - skb->data;
181 }
182
183 static inline int skb_network_offset(const struct sk_buff *skb)
184 {
185         return skb_network_header(skb) - skb->data;
186 }
187
188 static inline void skb_copy_to_linear_data(struct sk_buff *skb,
189                                            const void *from,
190                                            const unsigned int len)
191 {
192         memcpy(skb->data, from, len);
193 }
194 #endif  /* !HAVE_SKBUFF_HEADER_HELPERS */
195
196 #ifndef HAVE_SKB_WARN_LRO
197 #ifndef NETIF_F_LRO
198 static inline bool skb_warn_if_lro(const struct sk_buff *skb)
199 {
200         return false;
201 }
202 #else
203 extern void __skb_warn_lro_forwarding(const struct sk_buff *skb);
204
205 static inline bool skb_warn_if_lro(const struct sk_buff *skb)
206 {
207         /* LRO sets gso_size but not gso_type, whereas if GSO is really
208          * wanted then gso_type will be set. */
209         struct skb_shared_info *shinfo = skb_shinfo(skb);
210         if (shinfo->gso_size != 0 && unlikely(shinfo->gso_type == 0)) {
211                 __skb_warn_lro_forwarding(skb);
212                 return true;
213         }
214         return false;
215 }
216 #endif /* NETIF_F_LRO */
217 #endif /* HAVE_SKB_WARN_LRO */
218
219 #ifndef HAVE_CONSUME_SKB
220 #define consume_skb kfree_skb
221 #endif
222
223 #ifndef HAVE_SKB_FRAG_PAGE
224 #include <linux/mm.h>
225
226 static inline struct page *skb_frag_page(const skb_frag_t *frag)
227 {
228         return frag->page;
229 }
230
231 static inline void __skb_frag_set_page(skb_frag_t *frag, struct page *page)
232 {
233         frag->page = page;
234 }
235 static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size)
236 {
237         frag->size = size;
238 }
239 static inline void __skb_frag_ref(skb_frag_t *frag)
240 {
241         get_page(skb_frag_page(frag));
242 }
243 static inline void __skb_frag_unref(skb_frag_t *frag)
244 {
245         put_page(skb_frag_page(frag));
246 }
247
248 static inline void skb_frag_ref(struct sk_buff *skb, int f)
249 {
250         __skb_frag_ref(&skb_shinfo(skb)->frags[f]);
251 }
252
253 static inline void skb_frag_unref(struct sk_buff *skb, int f)
254 {
255         __skb_frag_unref(&skb_shinfo(skb)->frags[f]);
256 }
257
258 #endif
259
260 #ifndef HAVE_SKB_RESET_MAC_LEN
261 static inline void skb_reset_mac_len(struct sk_buff *skb)
262 {
263         skb->mac_len = skb->network_header - skb->mac_header;
264 }
265 #endif
266
267 #ifndef HAVE_SKB_UNCLONE
268 static inline int skb_unclone(struct sk_buff *skb, gfp_t pri)
269 {
270         might_sleep_if(pri & __GFP_WAIT);
271
272         if (skb_cloned(skb))
273                 return pskb_expand_head(skb, 0, 0, pri);
274
275         return 0;
276 }
277 #endif
278
279 #ifndef HAVE_SKB_ORPHAN_FRAGS
280 static inline int skb_orphan_frags(struct sk_buff *skb, gfp_t gfp_mask)
281 {
282         return 0;
283 }
284 #endif
285
286 #ifndef HAVE_SKB_GET_HASH
287 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
288 #define __skb_get_hash rpl__skb_get_rxhash
289 #define skb_get_hash rpl_skb_get_rxhash
290
291 extern u32 __skb_get_hash(struct sk_buff *skb);
292 static inline __u32 skb_get_hash(struct sk_buff *skb)
293 {
294 #ifdef HAVE_RXHASH
295         if (skb->rxhash)
296 #ifndef HAVE_U16_RXHASH
297                 return skb->rxhash;
298 #else
299                 return jhash_1word(skb->rxhash, 0);
300 #endif
301 #endif
302         return __skb_get_hash(skb);
303 }
304
305 #else
306 #define skb_get_hash skb_get_rxhash
307 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0) */
308 #endif /* HAVE_SKB_GET_HASH */
309
310 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
311 static inline void skb_tx_error(struct sk_buff *skb)
312 {
313         return;
314 }
315 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0) */
316
317 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,14,0)
318 #define skb_zerocopy_headlen rpl_skb_zerocopy_headlen
319 unsigned int rpl_skb_zerocopy_headlen(const struct sk_buff *from);
320 #endif
321
322 #ifndef HAVE_SKB_ZEROCOPY
323 #define skb_zerocopy rpl_skb_zerocopy
324 int rpl_skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len,
325                      int hlen);
326 #endif
327
328 #ifndef HAVE_SKB_CLEAR_HASH
329 static inline void skb_clear_hash(struct sk_buff *skb)
330 {
331 #ifdef HAVE_RXHASH
332         skb->rxhash = 0;
333 #endif
334 #if defined(HAVE_L4_RXHASH) && !defined(HAVE_RHEL_OVS_HOOK)
335         skb->l4_rxhash = 0;
336 #endif
337 }
338 #endif
339
340 #ifndef HAVE_SKB_HAS_FRAG_LIST
341 #define skb_has_frag_list skb_has_frags
342 #endif
343
344 #ifndef HAVE___SKB_FILL_PAGE_DESC
345 static inline void __skb_fill_page_desc(struct sk_buff *skb, int i,
346                                         struct page *page, int off, int size)
347 {
348         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
349
350         __skb_frag_set_page(frag, page);
351         frag->page_offset       = off;
352         skb_frag_size_set(frag, size);
353 }
354 #endif
355
356 #ifndef HAVE_SKB_ENSURE_WRITABLE
357 #define skb_ensure_writable rpl_skb_ensure_writable
358 int rpl_skb_ensure_writable(struct sk_buff *skb, int write_len);
359 #endif
360
361 #ifndef HAVE_SKB_VLAN_POP
362 #define skb_vlan_pop rpl_skb_vlan_pop
363 int rpl_skb_vlan_pop(struct sk_buff *skb);
364 #endif
365
366 #ifndef HAVE_SKB_VLAN_PUSH
367 #define skb_vlan_push rpl_skb_vlan_push
368 int rpl_skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci);
369 #endif
370
371 #ifndef HAVE_KFREE_SKB_LIST
372 void rpl_kfree_skb_list(struct sk_buff *segs);
373 #define kfree_skb_list rpl_kfree_skb_list
374 #endif
375
376 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,3,0)
377 #define skb_postpull_rcsum rpl_skb_postpull_rcsum
378 static inline void skb_postpull_rcsum(struct sk_buff *skb,
379                                       const void *start, unsigned int len)
380 {
381         if (skb->ip_summed == CHECKSUM_COMPLETE)
382                 skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
383         else if (skb->ip_summed == CHECKSUM_PARTIAL &&
384                         skb_checksum_start_offset(skb) <= len)
385                 skb->ip_summed = CHECKSUM_NONE;
386 }
387
388 #define skb_pull_rcsum rpl_skb_pull_rcsum
389 static inline unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
390 {
391         BUG_ON(len > skb->len);
392         skb->len -= len;
393         BUG_ON(skb->len < skb->data_len);
394         skb_postpull_rcsum(skb, skb->data, len);
395         return skb->data += len;
396 }
397
398 #endif
399 #endif