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