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