datapath: Rehash 16-bit skbuff hashes into 32 bits.
[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_next <linux/skbuff.h>
5
6 #include <linux/jhash.h>
7 #include <linux/version.h>
8
9 #ifndef HAVE_SKB_COPY_FROM_LINEAR_DATA_OFFSET
10 static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb,
11                                                     const int offset, void *to,
12                                                     const unsigned int len)
13 {
14         memcpy(to, skb->data + offset, len);
15 }
16
17 static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb,
18                                                   const int offset,
19                                                   const void *from,
20                                                   const unsigned int len)
21 {
22         memcpy(skb->data + offset, from, len);
23 }
24
25 #endif  /* !HAVE_SKB_COPY_FROM_LINEAR_DATA_OFFSET */
26
27 #ifndef HAVE_SKB_RESET_TAIL_POINTER
28 static inline void skb_reset_tail_pointer(struct sk_buff *skb)
29 {
30         skb->tail = skb->data;
31 }
32 #endif
33 /*
34  * The networking layer reserves some headroom in skb data (via
35  * dev_alloc_skb). This is used to avoid having to reallocate skb data when
36  * the header has to grow. In the default case, if the header has to grow
37  * 16 bytes or less we avoid the reallocation.
38  *
39  * Unfortunately this headroom changes the DMA alignment of the resulting
40  * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
41  * on some architectures. An architecture can override this value,
42  * perhaps setting it to a cacheline in size (since that will maintain
43  * cacheline alignment of the DMA). It must be a power of 2.
44  *
45  * Various parts of the networking layer expect at least 16 bytes of
46  * headroom, you should not reduce this.
47  */
48 #ifndef NET_SKB_PAD
49 #define NET_SKB_PAD     16
50 #endif
51
52 #ifndef HAVE_SKB_COW_HEAD
53 static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
54                             int cloned)
55 {
56         int delta = 0;
57
58         if (headroom < NET_SKB_PAD)
59                 headroom = NET_SKB_PAD;
60         if (headroom > skb_headroom(skb))
61                 delta = headroom - skb_headroom(skb);
62
63         if (delta || cloned)
64                 return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
65                                         GFP_ATOMIC);
66         return 0;
67 }
68
69 static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
70 {
71         return __skb_cow(skb, headroom, skb_header_cloned(skb));
72 }
73 #endif  /* !HAVE_SKB_COW_HEAD */
74
75 #ifndef HAVE_SKB_DST_ACCESSOR_FUNCS
76 static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
77 {
78         return (struct dst_entry *)skb->dst;
79 }
80
81 static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
82 {
83         skb->dst = dst;
84 }
85
86 static inline struct rtable *skb_rtable(const struct sk_buff *skb)
87 {
88         return (struct rtable *)skb->dst;
89 }
90 #endif
91
92 #ifndef CHECKSUM_PARTIAL
93 #define CHECKSUM_PARTIAL CHECKSUM_HW
94 #endif
95 #ifndef CHECKSUM_COMPLETE
96 #define CHECKSUM_COMPLETE CHECKSUM_HW
97 #endif
98
99 #ifndef HAVE_SKBUFF_HEADER_HELPERS
100 static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
101 {
102         return skb->h.raw;
103 }
104
105 static inline void skb_reset_transport_header(struct sk_buff *skb)
106 {
107         skb->h.raw = skb->data;
108 }
109
110 static inline void skb_set_transport_header(struct sk_buff *skb,
111                         const int offset)
112 {
113         skb->h.raw = skb->data + offset;
114 }
115
116 static inline unsigned char *skb_network_header(const struct sk_buff *skb)
117 {
118         return skb->nh.raw;
119 }
120
121 static inline void skb_reset_network_header(struct sk_buff *skb)
122 {
123         skb->nh.raw = skb->data;
124 }
125
126 static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
127 {
128         skb->nh.raw = skb->data + offset;
129 }
130
131 static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
132 {
133         return skb->mac.raw;
134 }
135
136 static inline void skb_reset_mac_header(struct sk_buff *skb)
137 {
138         skb->mac_header = skb->data;
139 }
140
141 static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
142 {
143         skb->mac.raw = skb->data + offset;
144 }
145
146 static inline int skb_transport_offset(const struct sk_buff *skb)
147 {
148         return skb_transport_header(skb) - skb->data;
149 }
150
151 static inline int skb_network_offset(const struct sk_buff *skb)
152 {
153         return skb_network_header(skb) - skb->data;
154 }
155
156 static inline void skb_copy_to_linear_data(struct sk_buff *skb,
157                                            const void *from,
158                                            const unsigned int len)
159 {
160         memcpy(skb->data, from, len);
161 }
162 #endif  /* !HAVE_SKBUFF_HEADER_HELPERS */
163
164 #ifndef HAVE_SKB_WARN_LRO
165 #ifndef NETIF_F_LRO
166 static inline bool skb_warn_if_lro(const struct sk_buff *skb)
167 {
168         return false;
169 }
170 #else
171 extern void __skb_warn_lro_forwarding(const struct sk_buff *skb);
172
173 static inline bool skb_warn_if_lro(const struct sk_buff *skb)
174 {
175         /* LRO sets gso_size but not gso_type, whereas if GSO is really
176          * wanted then gso_type will be set. */
177         struct skb_shared_info *shinfo = skb_shinfo(skb);
178         if (shinfo->gso_size != 0 && unlikely(shinfo->gso_type == 0)) {
179                 __skb_warn_lro_forwarding(skb);
180                 return true;
181         }
182         return false;
183 }
184 #endif /* NETIF_F_LRO */
185 #endif /* HAVE_SKB_WARN_LRO */
186
187 #ifndef HAVE_CONSUME_SKB
188 #define consume_skb kfree_skb
189 #endif
190
191 #ifndef HAVE_SKB_FRAG_PAGE
192 #include <linux/mm.h>
193
194 static inline struct page *skb_frag_page(const skb_frag_t *frag)
195 {
196         return frag->page;
197 }
198
199 static inline void __skb_frag_set_page(skb_frag_t *frag, struct page *page)
200 {
201         frag->page = page;
202 }
203 static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size)
204 {
205         frag->size = size;
206 }
207 static inline void __skb_frag_ref(skb_frag_t *frag)
208 {
209         get_page(skb_frag_page(frag));
210 }
211 static inline void __skb_frag_unref(skb_frag_t *frag)
212 {
213         put_page(skb_frag_page(frag));
214 }
215
216 static inline void skb_frag_ref(struct sk_buff *skb, int f)
217 {
218         __skb_frag_ref(&skb_shinfo(skb)->frags[f]);
219 }
220
221 static inline void skb_frag_unref(struct sk_buff *skb, int f)
222 {
223         __skb_frag_unref(&skb_shinfo(skb)->frags[f]);
224 }
225
226 #endif
227
228 #ifndef HAVE_SKB_RESET_MAC_LEN
229 static inline void skb_reset_mac_len(struct sk_buff *skb)
230 {
231         skb->mac_len = skb->network_header - skb->mac_header;
232 }
233 #endif
234
235 #ifndef HAVE_SKB_UNCLONE
236 static inline int skb_unclone(struct sk_buff *skb, gfp_t pri)
237 {
238         might_sleep_if(pri & __GFP_WAIT);
239
240         if (skb_cloned(skb))
241                 return pskb_expand_head(skb, 0, 0, pri);
242
243         return 0;
244 }
245 #endif
246
247 #ifndef HAVE_SKB_ORPHAN_FRAGS
248 static inline int skb_orphan_frags(struct sk_buff *skb, gfp_t gfp_mask)
249 {
250         return 0;
251 }
252 #endif
253
254 #ifndef HAVE_SKB_GET_HASH
255 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
256 #define __skb_get_hash rpl__skb_get_rxhash
257 #define skb_get_hash rpl_skb_get_rxhash
258
259 extern u32 __skb_get_hash(struct sk_buff *skb);
260 static inline __u32 skb_get_hash(struct sk_buff *skb)
261 {
262 #ifdef HAVE_RXHASH
263         if (skb->rxhash)
264 #ifndef HAVE_U16_RXHASH
265                 return skb->rxhash;
266 #else
267                 return jhash_1word(skb->rxhash, 0);
268 #endif
269 #endif
270         return __skb_get_hash(skb);
271 }
272
273 #else
274 #define skb_get_hash skb_get_rxhash
275 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0) */
276 #endif /* HAVE_SKB_GET_HASH */
277
278 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
279 static inline void skb_tx_error(struct sk_buff *skb)
280 {
281         return;
282 }
283 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0) */
284
285 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,14,0)
286 unsigned int skb_zerocopy_headlen(const struct sk_buff *from);
287 int skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len,
288                   int hlen);
289 #endif
290
291 #ifndef HAVE_SKB_CLEAR_HASH
292 static inline void skb_clear_hash(struct sk_buff *skb)
293 {
294 #ifdef HAVE_RXHASH
295         skb->rxhash = 0;
296 #endif
297 #ifdef HAVE_L4_RXHASH
298         skb->l4_rxhash = 0;
299 #endif
300 }
301 #endif
302
303 #ifndef HAVE_SKB_HAS_FRAG_LIST
304 #define skb_has_frag_list skb_has_frags
305 #endif
306
307 #ifndef HAVE___SKB_FILL_PAGE_DESC
308 static inline void __skb_fill_page_desc(struct sk_buff *skb, int i,
309                                         struct page *page, int off, int size)
310 {
311         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
312
313         __skb_frag_set_page(frag, page);
314         frag->page_offset       = off;
315         skb_frag_size_set(frag, size);
316 }
317 #endif
318
319 #endif