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