/src/openvswitch/lib/dp-packet.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2016 Nicira, Inc. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at: |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #include <config.h> |
18 | | #include <stdlib.h> |
19 | | #include <string.h> |
20 | | |
21 | | #include "dp-packet.h" |
22 | | #include "netdev-afxdp.h" |
23 | | #include "netdev-dpdk.h" |
24 | | #include "netdev-provider.h" |
25 | | #include "openvswitch/dynamic-string.h" |
26 | | #include "util.h" |
27 | | |
28 | | static void |
29 | | dp_packet_init__(struct dp_packet *b, size_t allocated, enum dp_packet_source source) |
30 | 0 | { |
31 | 0 | dp_packet_set_allocated(b, allocated); |
32 | 0 | b->source = source; |
33 | 0 | dp_packet_reset_offsets(b); |
34 | 0 | pkt_metadata_init(&b->md, 0); |
35 | 0 | dp_packet_reset_cutlen(b); |
36 | 0 | dp_packet_reset_offload(b); |
37 | 0 | dp_packet_set_tso_segsz(b, 0); |
38 | | /* Initialize implementation-specific fields of dp_packet. */ |
39 | 0 | dp_packet_init_specific(b); |
40 | | /* By default assume the packet type to be Ethernet. */ |
41 | 0 | b->packet_type = htonl(PT_ETH); |
42 | 0 | } |
43 | | |
44 | | static void |
45 | | dp_packet_use__(struct dp_packet *b, void *base, size_t allocated, |
46 | | enum dp_packet_source source) |
47 | 0 | { |
48 | 0 | dp_packet_set_base(b, base); |
49 | 0 | dp_packet_set_data(b, base); |
50 | 0 | dp_packet_set_size(b, 0); |
51 | |
|
52 | 0 | dp_packet_init__(b, allocated, source); |
53 | 0 | } |
54 | | |
55 | | /* Initializes 'b' as an empty dp_packet that contains the 'allocated' bytes of |
56 | | * memory starting at 'base'. 'base' should be the first byte of a region |
57 | | * obtained from malloc(). It will be freed (with free()) if 'b' is resized or |
58 | | * freed. */ |
59 | | void |
60 | | dp_packet_use(struct dp_packet *b, void *base, size_t allocated) |
61 | 0 | { |
62 | 0 | dp_packet_use__(b, base, allocated, DPBUF_MALLOC); |
63 | 0 | } |
64 | | |
65 | | #if HAVE_AF_XDP |
66 | | /* Initialize 'b' as an empty dp_packet that contains |
67 | | * memory starting at AF_XDP umem base. |
68 | | */ |
69 | | void |
70 | | dp_packet_use_afxdp(struct dp_packet *b, void *data, size_t allocated, |
71 | | size_t headroom) |
72 | | { |
73 | | dp_packet_set_base(b, (char *)data - headroom); |
74 | | dp_packet_set_data(b, data); |
75 | | dp_packet_set_size(b, 0); |
76 | | |
77 | | dp_packet_init__(b, allocated, DPBUF_AFXDP); |
78 | | } |
79 | | #endif |
80 | | |
81 | | /* Initializes 'b' as an empty dp_packet that contains the 'allocated' bytes of |
82 | | * memory starting at 'base'. 'base' should point to a buffer on the stack. |
83 | | * (Nothing actually relies on 'base' being allocated on the stack. It could |
84 | | * be static or malloc()'d memory. But stack space is the most common use |
85 | | * case.) |
86 | | * |
87 | | * 'base' should be appropriately aligned. Using an array of uint32_t or |
88 | | * uint64_t for the buffer is a reasonable way to ensure appropriate alignment |
89 | | * for 32- or 64-bit data. |
90 | | * |
91 | | * An dp_packet operation that requires reallocating data will copy the provided |
92 | | * buffer into a malloc()'d buffer. Thus, it is wise to call dp_packet_uninit() |
93 | | * on an dp_packet initialized by this function, so that if it expanded into the |
94 | | * heap, that memory is freed. */ |
95 | | void |
96 | | dp_packet_use_stub(struct dp_packet *b, void *base, size_t allocated) |
97 | 0 | { |
98 | 0 | dp_packet_use__(b, base, allocated, DPBUF_STUB); |
99 | 0 | } |
100 | | |
101 | | /* Initializes 'b' as an dp_packet whose data starts at 'data' and continues for |
102 | | * 'size' bytes. This is appropriate for an dp_packet that will be used to |
103 | | * inspect existing data, without moving it around or reallocating it, and |
104 | | * generally without modifying it at all. |
105 | | * |
106 | | * An dp_packet operation that requires reallocating data will assert-fail if this |
107 | | * function was used to initialize it. */ |
108 | | void |
109 | | dp_packet_use_const(struct dp_packet *b, const void *data, size_t size) |
110 | 0 | { |
111 | 0 | dp_packet_use__(b, CONST_CAST(void *, data), size, DPBUF_STACK); |
112 | 0 | dp_packet_set_size(b, size); |
113 | 0 | } |
114 | | |
115 | | /* Initializes 'b' as a DPDK dp-packet, which must have been allocated from a |
116 | | * DPDK memory pool. */ |
117 | | void |
118 | | dp_packet_init_dpdk(struct dp_packet *b) |
119 | 0 | { |
120 | 0 | b->source = DPBUF_DPDK; |
121 | 0 | } |
122 | | |
123 | | /* Initializes 'b' as an empty dp_packet with an initial capacity of 'size' |
124 | | * bytes. */ |
125 | | void |
126 | | dp_packet_init(struct dp_packet *b, size_t size) |
127 | 0 | { |
128 | 0 | dp_packet_use(b, size ? xmalloc(size) : NULL, size); |
129 | 0 | } |
130 | | |
131 | | /* Frees memory that 'b' points to. */ |
132 | | void |
133 | | dp_packet_uninit(struct dp_packet *b) |
134 | 0 | { |
135 | 0 | if (b) { |
136 | 0 | if (b->source == DPBUF_MALLOC) { |
137 | 0 | free(dp_packet_base(b)); |
138 | 0 | } else if (b->source == DPBUF_DPDK) { |
139 | 0 | free_dpdk_buf(b); |
140 | 0 | } else if (b->source == DPBUF_AFXDP) { |
141 | 0 | free_afxdp_buf(b); |
142 | 0 | } |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | | /* Creates and returns a new dp_packet with an initial capacity of 'size' |
147 | | * bytes. */ |
148 | | struct dp_packet * |
149 | | dp_packet_new(size_t size) |
150 | 0 | { |
151 | | #ifdef DPDK_NETDEV |
152 | | struct dp_packet *b = xmalloc_cacheline(sizeof *b); |
153 | | #else |
154 | 0 | struct dp_packet *b = xmalloc(sizeof *b); |
155 | 0 | #endif |
156 | 0 | dp_packet_init(b, size); |
157 | 0 | return b; |
158 | 0 | } |
159 | | |
160 | | /* Creates and returns a new dp_packet with an initial capacity of 'size + |
161 | | * headroom' bytes, reserving the first 'headroom' bytes as headroom. */ |
162 | | struct dp_packet * |
163 | | dp_packet_new_with_headroom(size_t size, size_t headroom) |
164 | 0 | { |
165 | 0 | struct dp_packet *b = dp_packet_new(size + headroom); |
166 | 0 | dp_packet_reserve(b, headroom); |
167 | 0 | return b; |
168 | 0 | } |
169 | | |
170 | | /* Creates and returns a new dp_packet that initially contains a copy of the |
171 | | * 'dp_packet_size(buffer)' bytes of data starting at 'buffer->data' with no headroom or |
172 | | * tailroom. */ |
173 | | struct dp_packet * |
174 | | dp_packet_clone(const struct dp_packet *buffer) |
175 | 0 | { |
176 | 0 | ovs_assert(buffer); |
177 | 0 | return dp_packet_clone_with_headroom(buffer, 0); |
178 | 0 | } |
179 | | |
180 | | /* Creates and returns a new dp_packet whose data are copied from 'buffer'. |
181 | | * The returned dp_packet will additionally have 'headroom' bytes of |
182 | | * headroom. */ |
183 | | struct dp_packet * |
184 | | dp_packet_clone_with_headroom(const struct dp_packet *buffer, size_t headroom) |
185 | 0 | { |
186 | 0 | const void *data_dp = dp_packet_data(buffer); |
187 | 0 | struct dp_packet *new_buffer; |
188 | 0 | uint32_t mark; |
189 | |
|
190 | 0 | ovs_assert(data_dp); |
191 | |
|
192 | 0 | new_buffer = dp_packet_clone_data_with_headroom(data_dp, |
193 | 0 | dp_packet_size(buffer), |
194 | 0 | headroom); |
195 | | /* Copy the following fields into the returned buffer: l2_pad_size, |
196 | | * l2_5_ofs, l3_ofs, l4_ofs, cutlen, packet_type, offloads and md. */ |
197 | 0 | memcpy(&new_buffer->l2_pad_size, &buffer->l2_pad_size, |
198 | 0 | sizeof(struct dp_packet) - |
199 | 0 | offsetof(struct dp_packet, l2_pad_size)); |
200 | |
|
201 | 0 | dp_packet_set_tso_segsz(new_buffer, dp_packet_get_tso_segsz(buffer)); |
202 | |
|
203 | 0 | if (dp_packet_rss_valid(buffer)) { |
204 | 0 | dp_packet_set_rss_hash(new_buffer, dp_packet_get_rss_hash(buffer)); |
205 | 0 | } |
206 | 0 | if (dp_packet_has_flow_mark(buffer, &mark)) { |
207 | 0 | dp_packet_set_flow_mark(new_buffer, mark); |
208 | 0 | } |
209 | |
|
210 | 0 | return new_buffer; |
211 | 0 | } |
212 | | |
213 | | /* Creates and returns a new dp_packet that initially contains a copy of the |
214 | | * 'size' bytes of data starting at 'data' with no headroom or tailroom. */ |
215 | | struct dp_packet * |
216 | | dp_packet_clone_data(const void *data, size_t size) |
217 | 0 | { |
218 | 0 | return dp_packet_clone_data_with_headroom(data, size, 0); |
219 | 0 | } |
220 | | |
221 | | /* Creates and returns a new dp_packet that initially contains 'headroom' bytes of |
222 | | * headroom followed by a copy of the 'size' bytes of data starting at |
223 | | * 'data'. */ |
224 | | struct dp_packet * |
225 | | dp_packet_clone_data_with_headroom(const void *data, size_t size, size_t headroom) |
226 | 0 | { |
227 | 0 | struct dp_packet *b = dp_packet_new_with_headroom(size, headroom); |
228 | 0 | dp_packet_put(b, data, size); |
229 | 0 | return b; |
230 | 0 | } |
231 | | |
232 | | static void |
233 | | dp_packet_copy__(struct dp_packet *b, uint8_t *new_base, |
234 | | size_t new_headroom, size_t new_tailroom) |
235 | 0 | { |
236 | 0 | const uint8_t *old_base = dp_packet_base(b); |
237 | 0 | size_t old_headroom = dp_packet_headroom(b); |
238 | 0 | size_t old_tailroom = dp_packet_tailroom(b); |
239 | 0 | size_t copy_headroom = MIN(old_headroom, new_headroom); |
240 | 0 | size_t copy_tailroom = MIN(old_tailroom, new_tailroom); |
241 | |
|
242 | 0 | memcpy(&new_base[new_headroom - copy_headroom], |
243 | 0 | &old_base[old_headroom - copy_headroom], |
244 | 0 | copy_headroom + dp_packet_size(b) + copy_tailroom); |
245 | 0 | } |
246 | | |
247 | | /* Reallocates 'b' so that it has exactly 'new_headroom' and 'new_tailroom' |
248 | | * bytes of headroom and tailroom, respectively. */ |
249 | | void |
250 | | dp_packet_resize(struct dp_packet *b, size_t new_headroom, size_t new_tailroom) |
251 | 0 | { |
252 | 0 | void *new_base, *new_data; |
253 | 0 | size_t new_allocated; |
254 | |
|
255 | 0 | new_allocated = new_headroom + dp_packet_size(b) + new_tailroom; |
256 | |
|
257 | 0 | switch (b->source) { |
258 | 0 | case DPBUF_DPDK: |
259 | 0 | OVS_NOT_REACHED(); |
260 | | |
261 | 0 | case DPBUF_MALLOC: |
262 | 0 | if (new_headroom == dp_packet_headroom(b)) { |
263 | 0 | new_base = xrealloc(dp_packet_base(b), new_allocated); |
264 | 0 | } else { |
265 | 0 | new_base = xmalloc(new_allocated); |
266 | 0 | dp_packet_copy__(b, new_base, new_headroom, new_tailroom); |
267 | 0 | free(dp_packet_base(b)); |
268 | 0 | } |
269 | 0 | break; |
270 | | |
271 | 0 | case DPBUF_STACK: |
272 | 0 | OVS_NOT_REACHED(); |
273 | | |
274 | 0 | case DPBUF_AFXDP: |
275 | 0 | OVS_NOT_REACHED(); |
276 | | |
277 | 0 | case DPBUF_STUB: |
278 | 0 | b->source = DPBUF_MALLOC; |
279 | 0 | new_base = xmalloc(new_allocated); |
280 | 0 | dp_packet_copy__(b, new_base, new_headroom, new_tailroom); |
281 | 0 | break; |
282 | | |
283 | 0 | default: |
284 | 0 | OVS_NOT_REACHED(); |
285 | 0 | } |
286 | | |
287 | 0 | dp_packet_set_allocated(b, new_allocated); |
288 | 0 | dp_packet_set_base(b, new_base); |
289 | |
|
290 | 0 | new_data = (char *) new_base + new_headroom; |
291 | 0 | if (dp_packet_data(b) != new_data) { |
292 | 0 | dp_packet_set_data(b, new_data); |
293 | 0 | } |
294 | 0 | } |
295 | | |
296 | | /* Ensures that 'b' has room for at least 'size' bytes at its tail end, |
297 | | * reallocating and copying its data if necessary. Its headroom, if any, is |
298 | | * preserved. */ |
299 | | void |
300 | | dp_packet_prealloc_tailroom(struct dp_packet *b, size_t size) |
301 | 0 | { |
302 | 0 | if ((size && !dp_packet_base(b)) || (size > dp_packet_tailroom(b))) { |
303 | 0 | dp_packet_resize(b, dp_packet_headroom(b), MAX(size, 64)); |
304 | 0 | } |
305 | 0 | } |
306 | | |
307 | | /* Ensures that 'b' has room for at least 'size' bytes at its head, |
308 | | * reallocating and copying its data if necessary. Its tailroom, if any, is |
309 | | * preserved. */ |
310 | | void |
311 | | dp_packet_prealloc_headroom(struct dp_packet *b, size_t size) |
312 | 0 | { |
313 | 0 | if (size > dp_packet_headroom(b)) { |
314 | 0 | dp_packet_resize(b, MAX(size, 64), dp_packet_tailroom(b)); |
315 | 0 | } |
316 | 0 | } |
317 | | |
318 | | /* Shifts all of the data within the allocated space in 'b' by 'delta' bytes. |
319 | | * For example, a 'delta' of 1 would cause each byte of data to move one byte |
320 | | * forward (from address 'p' to 'p+1'), and a 'delta' of -1 would cause each |
321 | | * byte to move one byte backward (from 'p' to 'p-1'). */ |
322 | | void |
323 | | dp_packet_shift(struct dp_packet *b, int delta) |
324 | 0 | { |
325 | 0 | ovs_assert(delta > 0 ? delta <= dp_packet_tailroom(b) |
326 | 0 | : delta < 0 ? -delta <= dp_packet_headroom(b) |
327 | 0 | : true); |
328 | |
|
329 | 0 | if (delta != 0) { |
330 | 0 | const void *data_dp = dp_packet_data(b); |
331 | 0 | char *dst = (char *) data_dp + delta; |
332 | |
|
333 | 0 | ovs_assert(data_dp); |
334 | |
|
335 | 0 | memmove(dst, data_dp, dp_packet_size(b)); |
336 | 0 | dp_packet_set_data(b, dst); |
337 | 0 | } |
338 | 0 | } |
339 | | |
340 | | /* Appends 'size' bytes of data to the tail end of 'b', reallocating and |
341 | | * copying its data if necessary. Returns a pointer to the first byte of the |
342 | | * new data, which is left uninitialized. */ |
343 | | void * |
344 | | dp_packet_put_uninit(struct dp_packet *b, size_t size) |
345 | 0 | { |
346 | 0 | void *p; |
347 | 0 | dp_packet_prealloc_tailroom(b, size); |
348 | 0 | p = dp_packet_tail(b); |
349 | 0 | dp_packet_set_size(b, dp_packet_size(b) + size); |
350 | 0 | return p; |
351 | 0 | } |
352 | | |
353 | | /* Appends 'size' zeroed bytes to the tail end of 'b'. Data in 'b' is |
354 | | * reallocated and copied if necessary. Returns a pointer to the first byte of |
355 | | * the data's location in the dp_packet. */ |
356 | | void * |
357 | | dp_packet_put_zeros(struct dp_packet *b, size_t size) |
358 | 0 | { |
359 | 0 | void *dst = dp_packet_put_uninit(b, size); |
360 | 0 | nullable_memset(dst, 0, size); |
361 | 0 | return dst; |
362 | 0 | } |
363 | | |
364 | | /* Appends the 'size' bytes of data in 'p' to the tail end of 'b'. Data in 'b' |
365 | | * is reallocated and copied if necessary. Returns a pointer to the first |
366 | | * byte of the data's location in the dp_packet. */ |
367 | | void * |
368 | | dp_packet_put(struct dp_packet *b, const void *p, size_t size) |
369 | 0 | { |
370 | 0 | void *dst = dp_packet_put_uninit(b, size); |
371 | 0 | nullable_memcpy(dst, p, size); |
372 | 0 | return dst; |
373 | 0 | } |
374 | | |
375 | | /* Parses as many pairs of hex digits as possible (possibly separated by |
376 | | * spaces) from the beginning of 's', appending bytes for their values to 'b'. |
377 | | * Returns the first character of 's' that is not the first of a pair of hex |
378 | | * digits. If 'n' is nonnull, stores the number of bytes added to 'b' in |
379 | | * '*n'. */ |
380 | | char * |
381 | | dp_packet_put_hex(struct dp_packet *b, const char *s, size_t *n) |
382 | 0 | { |
383 | 0 | size_t initial_size = dp_packet_size(b); |
384 | 0 | for (;;) { |
385 | 0 | uint8_t byte; |
386 | 0 | bool ok; |
387 | |
|
388 | 0 | s += strspn(s, " \t\r\n"); |
389 | 0 | byte = hexits_value(s, 2, &ok); |
390 | 0 | if (!ok) { |
391 | 0 | if (n) { |
392 | 0 | *n = dp_packet_size(b) - initial_size; |
393 | 0 | } |
394 | 0 | return CONST_CAST(char *, s); |
395 | 0 | } |
396 | | |
397 | 0 | dp_packet_put(b, &byte, 1); |
398 | 0 | s += 2; |
399 | 0 | } |
400 | 0 | } |
401 | | |
402 | | /* Reserves 'size' bytes of headroom so that they can be later allocated with |
403 | | * dp_packet_push_uninit() without reallocating the dp_packet. */ |
404 | | void |
405 | | dp_packet_reserve(struct dp_packet *b, size_t size) |
406 | 0 | { |
407 | 0 | ovs_assert(!dp_packet_size(b)); |
408 | 0 | dp_packet_prealloc_tailroom(b, size); |
409 | 0 | dp_packet_set_data(b, (char*)dp_packet_data(b) + size); |
410 | 0 | } |
411 | | |
412 | | /* Reserves 'headroom' bytes at the head and 'tailroom' at the end so that |
413 | | * they can be later allocated with dp_packet_push_uninit() or |
414 | | * dp_packet_put_uninit() without reallocating the dp_packet. */ |
415 | | void |
416 | | dp_packet_reserve_with_tailroom(struct dp_packet *b, size_t headroom, |
417 | | size_t tailroom) |
418 | 0 | { |
419 | 0 | ovs_assert(!dp_packet_size(b)); |
420 | 0 | dp_packet_prealloc_tailroom(b, headroom + tailroom); |
421 | 0 | dp_packet_set_data(b, (char*)dp_packet_data(b) + headroom); |
422 | 0 | } |
423 | | |
424 | | /* Prefixes 'size' bytes to the head end of 'b', reallocating and copying its |
425 | | * data if necessary. Returns a pointer to the first byte of the data's |
426 | | * location in the dp_packet. The new data is left uninitialized. */ |
427 | | void * |
428 | | dp_packet_push_uninit(struct dp_packet *b, size_t size) |
429 | 0 | { |
430 | 0 | dp_packet_prealloc_headroom(b, size); |
431 | 0 | dp_packet_set_data(b, (char*)dp_packet_data(b) - size); |
432 | 0 | dp_packet_set_size(b, dp_packet_size(b) + size); |
433 | 0 | return dp_packet_data(b); |
434 | 0 | } |
435 | | |
436 | | /* Prefixes 'size' zeroed bytes to the head end of 'b', reallocating and |
437 | | * copying its data if necessary. Returns a pointer to the first byte of the |
438 | | * data's location in the dp_packet. */ |
439 | | void * |
440 | | dp_packet_push_zeros(struct dp_packet *b, size_t size) |
441 | 0 | { |
442 | 0 | void *dst = dp_packet_push_uninit(b, size); |
443 | 0 | nullable_memset(dst, 0, size); |
444 | 0 | return dst; |
445 | 0 | } |
446 | | |
447 | | /* Copies the 'size' bytes starting at 'p' to the head end of 'b', reallocating |
448 | | * and copying its data if necessary. Returns a pointer to the first byte of |
449 | | * the data's location in the dp_packet. */ |
450 | | void * |
451 | | dp_packet_push(struct dp_packet *b, const void *p, size_t size) |
452 | 0 | { |
453 | 0 | void *dst = dp_packet_push_uninit(b, size); |
454 | 0 | nullable_memcpy(dst, p, size); |
455 | 0 | return dst; |
456 | 0 | } |
457 | | |
458 | | /* Returns the data in 'b' as a block of malloc()'d memory and frees the buffer |
459 | | * within 'b'. (If 'b' itself was dynamically allocated, e.g. with |
460 | | * dp_packet_new(), then it should still be freed with, e.g., dp_packet_delete().) */ |
461 | | void * |
462 | | dp_packet_steal_data(struct dp_packet *b) |
463 | 0 | { |
464 | 0 | void *p; |
465 | 0 | ovs_assert(b->source != DPBUF_DPDK); |
466 | 0 | ovs_assert(b->source != DPBUF_AFXDP); |
467 | |
|
468 | 0 | if (b->source == DPBUF_MALLOC && dp_packet_data(b) == dp_packet_base(b)) { |
469 | 0 | p = dp_packet_data(b); |
470 | 0 | } else { |
471 | 0 | p = xmemdup(dp_packet_data(b), dp_packet_size(b)); |
472 | 0 | if (b->source == DPBUF_MALLOC) { |
473 | 0 | free(dp_packet_base(b)); |
474 | 0 | } |
475 | 0 | } |
476 | 0 | dp_packet_set_base(b, NULL); |
477 | 0 | dp_packet_set_data(b, NULL); |
478 | 0 | return p; |
479 | 0 | } |
480 | | |
481 | | static inline void |
482 | | dp_packet_adjust_layer_offset(uint16_t *offset, int increment) |
483 | 0 | { |
484 | 0 | if (*offset != UINT16_MAX) { |
485 | 0 | *offset += increment; |
486 | 0 | } |
487 | 0 | } |
488 | | |
489 | | /* Adjust the size of the l2_5 portion of the dp_packet, updating the l2 |
490 | | * pointer and the layer offsets. The caller is responsible for |
491 | | * modifying the contents. */ |
492 | | void * |
493 | | dp_packet_resize_l2_5(struct dp_packet *b, int increment) |
494 | 0 | { |
495 | 0 | if (increment >= 0) { |
496 | 0 | dp_packet_push_uninit(b, increment); |
497 | 0 | } else { |
498 | 0 | dp_packet_pull(b, -increment); |
499 | 0 | } |
500 | | |
501 | | /* Adjust layer offsets after l2_5. */ |
502 | 0 | dp_packet_adjust_layer_offset(&b->l3_ofs, increment); |
503 | 0 | dp_packet_adjust_layer_offset(&b->l4_ofs, increment); |
504 | 0 | dp_packet_adjust_layer_offset(&b->inner_l3_ofs, increment); |
505 | 0 | dp_packet_adjust_layer_offset(&b->inner_l4_ofs, increment); |
506 | |
|
507 | 0 | return dp_packet_data(b); |
508 | 0 | } |
509 | | |
510 | | /* Adjust the size of the l2 portion of the dp_packet, updating the l2 |
511 | | * pointer and the layer offsets. The caller is responsible for |
512 | | * modifying the contents. */ |
513 | | void * |
514 | | dp_packet_resize_l2(struct dp_packet *b, int increment) |
515 | 0 | { |
516 | 0 | dp_packet_resize_l2_5(b, increment); |
517 | 0 | dp_packet_adjust_layer_offset(&b->l2_5_ofs, increment); |
518 | 0 | return dp_packet_data(b); |
519 | 0 | } |
520 | | |
521 | | bool |
522 | | dp_packet_compare_offsets(struct dp_packet *b1, struct dp_packet *b2, |
523 | | struct ds *err_str) |
524 | 0 | { |
525 | 0 | if ((b1->l2_pad_size != b2->l2_pad_size) || |
526 | 0 | (b1->l2_5_ofs != b2->l2_5_ofs) || |
527 | 0 | (b1->l3_ofs != b2->l3_ofs) || |
528 | 0 | (b1->l4_ofs != b2->l4_ofs) || |
529 | 0 | (b1->inner_l3_ofs != b2->inner_l3_ofs) || |
530 | 0 | (b1->inner_l4_ofs != b2->inner_l4_ofs)) { |
531 | 0 | if (err_str) { |
532 | 0 | ds_put_format(err_str, "Packet offset comparison failed\n"); |
533 | 0 | ds_put_format(err_str, "Buffer 1 offsets: l2_pad_size %u," |
534 | 0 | " l2_5_ofs : %u l3_ofs %u, l4_ofs %u," |
535 | 0 | " inner_l3_ofs %u, inner_l4_ofs %u\n", |
536 | 0 | b1->l2_pad_size, b1->l2_5_ofs, |
537 | 0 | b1->l3_ofs, b1->l4_ofs, |
538 | 0 | b1->inner_l3_ofs, b1->inner_l4_ofs); |
539 | 0 | ds_put_format(err_str, "Buffer 2 offsets: l2_pad_size %u," |
540 | 0 | " l2_5_ofs : %u l3_ofs %u, l4_ofs %u," |
541 | 0 | " inner_l3_ofs %u, inner_l4_ofs %u\n", |
542 | 0 | b2->l2_pad_size, b2->l2_5_ofs, |
543 | 0 | b2->l3_ofs, b2->l4_ofs, |
544 | 0 | b2->inner_l3_ofs, b2->inner_l4_ofs); |
545 | 0 | } |
546 | 0 | return false; |
547 | 0 | } |
548 | 0 | return true; |
549 | 0 | } |
550 | | |
551 | | /* Checks if the packet 'p' is compatible with netdev_ol_flags 'flags' |
552 | | * and if not, updates the packet with the software fall back. */ |
553 | | void |
554 | | dp_packet_ol_send_prepare(struct dp_packet *p, uint64_t flags) |
555 | 0 | { |
556 | 0 | if (!dp_packet_ip_checksum_partial(p) |
557 | 0 | && !dp_packet_l4_checksum_partial(p) |
558 | 0 | && !dp_packet_inner_ip_checksum_partial(p) |
559 | 0 | && !dp_packet_inner_l4_checksum_partial(p)) { |
560 | | /* Only checksumming needs actions. */ |
561 | 0 | return; |
562 | 0 | } |
563 | | |
564 | 0 | if (!dp_packet_tunnel(p)) { |
565 | 0 | if (dp_packet_ip_checksum_partial(p) |
566 | 0 | && !(flags & NETDEV_TX_OFFLOAD_IPV4_CKSUM)) { |
567 | 0 | dp_packet_ip_set_header_csum(p, false); |
568 | 0 | } |
569 | |
|
570 | 0 | if (dp_packet_l4_checksum_partial(p)) { |
571 | 0 | if (dp_packet_l4_proto_tcp(p)) { |
572 | 0 | if (!(flags & NETDEV_TX_OFFLOAD_TCP_CKSUM)) { |
573 | 0 | packet_tcp_complete_csum(p, false); |
574 | 0 | } |
575 | 0 | } else if (dp_packet_l4_proto_udp(p)) { |
576 | 0 | if (!(flags & NETDEV_TX_OFFLOAD_UDP_CKSUM)) { |
577 | 0 | packet_udp_complete_csum(p, false); |
578 | 0 | } |
579 | 0 | } else { |
580 | 0 | ovs_assert(dp_packet_l4_proto_sctp(p)); |
581 | 0 | if (!(flags & NETDEV_TX_OFFLOAD_SCTP_CKSUM)) { |
582 | 0 | packet_sctp_complete_csum(p, false); |
583 | 0 | } |
584 | 0 | } |
585 | 0 | } |
586 | |
|
587 | 0 | return; |
588 | 0 | } |
589 | | |
590 | 0 | if (dp_packet_tunnel_geneve(p) |
591 | 0 | || dp_packet_tunnel_vxlan(p)) { |
592 | | |
593 | | /* If the TX interface doesn't support UDP tunnel offload but does |
594 | | * support inner checksum offload and an outer UDP checksum is |
595 | | * required, then we can't offload inner checksum either. As that would |
596 | | * invalidate the outer checksum. */ |
597 | 0 | if (!(flags & NETDEV_TX_OFFLOAD_OUTER_UDP_CKSUM) |
598 | 0 | && dp_packet_l4_checksum_partial(p)) { |
599 | 0 | flags &= ~(NETDEV_TX_OFFLOAD_TCP_CKSUM | |
600 | 0 | NETDEV_TX_OFFLOAD_UDP_CKSUM | |
601 | 0 | NETDEV_TX_OFFLOAD_SCTP_CKSUM | |
602 | 0 | NETDEV_TX_OFFLOAD_IPV4_CKSUM); |
603 | 0 | } |
604 | 0 | } |
605 | |
|
606 | 0 | if (dp_packet_inner_ip_checksum_partial(p) |
607 | 0 | && !(flags & NETDEV_TX_OFFLOAD_IPV4_CKSUM)) { |
608 | 0 | dp_packet_ip_set_header_csum(p, true); |
609 | 0 | } |
610 | |
|
611 | 0 | if (dp_packet_inner_l4_checksum_partial(p)) { |
612 | 0 | if (dp_packet_inner_l4_proto_tcp(p)) { |
613 | 0 | if (!(flags & NETDEV_TX_OFFLOAD_TCP_CKSUM)) { |
614 | 0 | packet_tcp_complete_csum(p, true); |
615 | 0 | } |
616 | 0 | } else if (dp_packet_inner_l4_proto_udp(p)) { |
617 | 0 | if (!(flags & NETDEV_TX_OFFLOAD_UDP_CKSUM)) { |
618 | 0 | packet_udp_complete_csum(p, true); |
619 | 0 | } |
620 | 0 | } else { |
621 | 0 | ovs_assert(dp_packet_inner_l4_proto_sctp(p)); |
622 | 0 | if (!(flags & NETDEV_TX_OFFLOAD_SCTP_CKSUM)) { |
623 | 0 | packet_sctp_complete_csum(p, true); |
624 | 0 | } |
625 | 0 | } |
626 | 0 | } |
627 | |
|
628 | 0 | if (dp_packet_ip_checksum_partial(p) |
629 | 0 | && !(flags & NETDEV_TX_OFFLOAD_OUTER_IP_CKSUM)) { |
630 | 0 | dp_packet_ip_set_header_csum(p, false); |
631 | 0 | } |
632 | |
|
633 | 0 | if (dp_packet_l4_checksum_partial(p)) { |
634 | 0 | ovs_assert(dp_packet_l4_proto_udp(p)); |
635 | 0 | if (!(flags & NETDEV_TX_OFFLOAD_OUTER_UDP_CKSUM)) { |
636 | 0 | packet_udp_complete_csum(p, false); |
637 | 0 | } |
638 | 0 | } |
639 | 0 | } |