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