]>
Commit | Line | Data |
---|---|---|
91088554 | 1 | /* |
91644f45 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2016 Nicira, Inc. |
91088554 DDP |
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> | |
cf62fa4c PS |
18 | #include <stdlib.h> |
19 | #include <string.h> | |
01961bbd | 20 | |
e14deea0 | 21 | #include "dp-packet.h" |
01961bbd DDP |
22 | #include "netdev-dpdk.h" |
23 | #include "openvswitch/dynamic-string.h" | |
cf62fa4c | 24 | #include "util.h" |
91088554 | 25 | |
cf62fa4c PS |
26 | static void |
27 | dp_packet_init__(struct dp_packet *b, size_t allocated, enum dp_packet_source source) | |
28 | { | |
11a6fbd5 | 29 | dp_packet_set_allocated(b, allocated); |
cf62fa4c | 30 | b->source = source; |
e496a484 | 31 | dp_packet_reset_offsets(b); |
35303d71 | 32 | pkt_metadata_init(&b->md, 0); |
91644f45 | 33 | dp_packet_rss_invalidate(b); |
f8121b39 | 34 | dp_packet_mbuf_init(b); |
aaca4fe0 | 35 | dp_packet_reset_cutlen(b); |
2482b0b0 JS |
36 | /* By default assume the packet type to be Ethernet. */ |
37 | b->packet_type = htonl(PT_ETH); | |
cf62fa4c PS |
38 | } |
39 | ||
40 | static void | |
41 | dp_packet_use__(struct dp_packet *b, void *base, size_t allocated, | |
42 | enum dp_packet_source source) | |
43 | { | |
44 | dp_packet_set_base(b, base); | |
45 | dp_packet_set_data(b, base); | |
46 | dp_packet_set_size(b, 0); | |
47 | ||
48 | dp_packet_init__(b, allocated, source); | |
49 | } | |
50 | ||
51 | /* Initializes 'b' as an empty dp_packet that contains the 'allocated' bytes of | |
52 | * memory starting at 'base'. 'base' should be the first byte of a region | |
53 | * obtained from malloc(). It will be freed (with free()) if 'b' is resized or | |
54 | * freed. */ | |
55 | void | |
56 | dp_packet_use(struct dp_packet *b, void *base, size_t allocated) | |
57 | { | |
58 | dp_packet_use__(b, base, allocated, DPBUF_MALLOC); | |
59 | } | |
60 | ||
61 | /* Initializes 'b' as an empty dp_packet that contains the 'allocated' bytes of | |
62 | * memory starting at 'base'. 'base' should point to a buffer on the stack. | |
63 | * (Nothing actually relies on 'base' being allocated on the stack. It could | |
64 | * be static or malloc()'d memory. But stack space is the most common use | |
65 | * case.) | |
66 | * | |
67 | * 'base' should be appropriately aligned. Using an array of uint32_t or | |
68 | * uint64_t for the buffer is a reasonable way to ensure appropriate alignment | |
69 | * for 32- or 64-bit data. | |
70 | * | |
71 | * An dp_packet operation that requires reallocating data will copy the provided | |
72 | * buffer into a malloc()'d buffer. Thus, it is wise to call dp_packet_uninit() | |
73 | * on an dp_packet initialized by this function, so that if it expanded into the | |
74 | * heap, that memory is freed. */ | |
75 | void | |
76 | dp_packet_use_stub(struct dp_packet *b, void *base, size_t allocated) | |
77 | { | |
78 | dp_packet_use__(b, base, allocated, DPBUF_STUB); | |
79 | } | |
80 | ||
81 | /* Initializes 'b' as an dp_packet whose data starts at 'data' and continues for | |
82 | * 'size' bytes. This is appropriate for an dp_packet that will be used to | |
83 | * inspect existing data, without moving it around or reallocating it, and | |
84 | * generally without modifying it at all. | |
85 | * | |
86 | * An dp_packet operation that requires reallocating data will assert-fail if this | |
87 | * function was used to initialize it. */ | |
88 | void | |
89 | dp_packet_use_const(struct dp_packet *b, const void *data, size_t size) | |
90 | { | |
91 | dp_packet_use__(b, CONST_CAST(void *, data), size, DPBUF_STACK); | |
92 | dp_packet_set_size(b, size); | |
93 | } | |
94 | ||
11d4c7a8 DB |
95 | /* Initializes 'b' as an empty dp_packet that contains the 'allocated' bytes. |
96 | * DPDK allocated dp_packet and *data is allocated from one continous memory | |
97 | * region as part of memory pool, so in memory data start right after | |
98 | * dp_packet. Therefore, there is a special method to free this type of | |
99 | * buffer. Here, non-transient ovs dp-packet fields are initialized for | |
100 | * packets that are part of a DPDK memory pool. */ | |
cf62fa4c PS |
101 | void |
102 | dp_packet_init_dpdk(struct dp_packet *b, size_t allocated) | |
103 | { | |
11d4c7a8 DB |
104 | dp_packet_set_allocated(b, allocated); |
105 | b->source = DPBUF_DPDK; | |
106 | b->packet_type = htonl(PT_ETH); | |
cf62fa4c PS |
107 | } |
108 | ||
109 | /* Initializes 'b' as an empty dp_packet with an initial capacity of 'size' | |
110 | * bytes. */ | |
111 | void | |
112 | dp_packet_init(struct dp_packet *b, size_t size) | |
113 | { | |
114 | dp_packet_use(b, size ? xmalloc(size) : NULL, size); | |
115 | } | |
116 | ||
117 | /* Frees memory that 'b' points to. */ | |
118 | void | |
119 | dp_packet_uninit(struct dp_packet *b) | |
120 | { | |
121 | if (b) { | |
122 | if (b->source == DPBUF_MALLOC) { | |
123 | free(dp_packet_base(b)); | |
124 | } else if (b->source == DPBUF_DPDK) { | |
125 | #ifdef DPDK_NETDEV | |
126 | /* If this dp_packet was allocated by DPDK it must have been | |
127 | * created as a dp_packet */ | |
128 | free_dpdk_buf((struct dp_packet*) b); | |
129 | #endif | |
130 | } | |
131 | } | |
132 | } | |
133 | ||
134 | /* Creates and returns a new dp_packet with an initial capacity of 'size' | |
135 | * bytes. */ | |
136 | struct dp_packet * | |
137 | dp_packet_new(size_t size) | |
138 | { | |
139 | struct dp_packet *b = xmalloc(sizeof *b); | |
140 | dp_packet_init(b, size); | |
141 | return b; | |
142 | } | |
91088554 | 143 | |
cf62fa4c PS |
144 | /* Creates and returns a new dp_packet with an initial capacity of 'size + |
145 | * headroom' bytes, reserving the first 'headroom' bytes as headroom. */ | |
e14deea0 PS |
146 | struct dp_packet * |
147 | dp_packet_new_with_headroom(size_t size, size_t headroom) | |
91088554 | 148 | { |
cf62fa4c PS |
149 | struct dp_packet *b = dp_packet_new(size + headroom); |
150 | dp_packet_reserve(b, headroom); | |
151 | return b; | |
152 | } | |
153 | ||
154 | /* Creates and returns a new dp_packet that initially contains a copy of the | |
155 | * 'dp_packet_size(buffer)' bytes of data starting at 'buffer->data' with no headroom or | |
156 | * tailroom. */ | |
157 | struct dp_packet * | |
158 | dp_packet_clone(const struct dp_packet *buffer) | |
159 | { | |
160 | return dp_packet_clone_with_headroom(buffer, 0); | |
161 | } | |
91088554 | 162 | |
84b70576 FA |
163 | /* Creates and returns a new dp_packet whose data are copied from 'buffer'. |
164 | * The returned dp_packet will additionally have 'headroom' bytes of | |
165 | * headroom. */ | |
cf62fa4c PS |
166 | struct dp_packet * |
167 | dp_packet_clone_with_headroom(const struct dp_packet *buffer, size_t headroom) | |
168 | { | |
169 | struct dp_packet *new_buffer; | |
91088554 | 170 | |
cf62fa4c PS |
171 | new_buffer = dp_packet_clone_data_with_headroom(dp_packet_data(buffer), |
172 | dp_packet_size(buffer), | |
173 | headroom); | |
84b70576 FA |
174 | /* Copy the following fields into the returned buffer: l2_pad_size, |
175 | * l2_5_ofs, l3_ofs, l4_ofs, cutlen, packet_type and md. */ | |
176 | memcpy(&new_buffer->l2_pad_size, &buffer->l2_pad_size, | |
177 | sizeof(struct dp_packet) - | |
178 | offsetof(struct dp_packet, l2_pad_size)); | |
179 | ||
91644f45 WT |
180 | #ifdef DPDK_NETDEV |
181 | new_buffer->mbuf.ol_flags = buffer->mbuf.ol_flags; | |
182 | #else | |
183 | new_buffer->rss_hash_valid = buffer->rss_hash_valid; | |
184 | #endif | |
185 | ||
186 | if (dp_packet_rss_valid(new_buffer)) { | |
187 | #ifdef DPDK_NETDEV | |
188 | new_buffer->mbuf.hash.rss = buffer->mbuf.hash.rss; | |
189 | #else | |
190 | new_buffer->rss_hash = buffer->rss_hash; | |
191 | #endif | |
192 | } | |
cf62fa4c PS |
193 | |
194 | return new_buffer; | |
195 | } | |
196 | ||
197 | /* Creates and returns a new dp_packet that initially contains a copy of the | |
198 | * 'size' bytes of data starting at 'data' with no headroom or tailroom. */ | |
199 | struct dp_packet * | |
200 | dp_packet_clone_data(const void *data, size_t size) | |
201 | { | |
202 | return dp_packet_clone_data_with_headroom(data, size, 0); | |
91088554 DDP |
203 | } |
204 | ||
cf62fa4c PS |
205 | /* Creates and returns a new dp_packet that initially contains 'headroom' bytes of |
206 | * headroom followed by a copy of the 'size' bytes of data starting at | |
207 | * 'data'. */ | |
e14deea0 | 208 | struct dp_packet * |
cf62fa4c | 209 | dp_packet_clone_data_with_headroom(const void *data, size_t size, size_t headroom) |
91088554 | 210 | { |
cf62fa4c PS |
211 | struct dp_packet *b = dp_packet_new_with_headroom(size, headroom); |
212 | dp_packet_put(b, data, size); | |
213 | return b; | |
214 | } | |
91088554 | 215 | |
cf62fa4c PS |
216 | static void |
217 | dp_packet_copy__(struct dp_packet *b, uint8_t *new_base, | |
218 | size_t new_headroom, size_t new_tailroom) | |
219 | { | |
220 | const uint8_t *old_base = dp_packet_base(b); | |
221 | size_t old_headroom = dp_packet_headroom(b); | |
222 | size_t old_tailroom = dp_packet_tailroom(b); | |
223 | size_t copy_headroom = MIN(old_headroom, new_headroom); | |
224 | size_t copy_tailroom = MIN(old_tailroom, new_tailroom); | |
91088554 | 225 | |
cf62fa4c PS |
226 | memcpy(&new_base[new_headroom - copy_headroom], |
227 | &old_base[old_headroom - copy_headroom], | |
228 | copy_headroom + dp_packet_size(b) + copy_tailroom); | |
229 | } | |
91088554 | 230 | |
cf62fa4c PS |
231 | /* Reallocates 'b' so that it has exactly 'new_headroom' and 'new_tailroom' |
232 | * bytes of headroom and tailroom, respectively. */ | |
233 | static void | |
234 | dp_packet_resize__(struct dp_packet *b, size_t new_headroom, size_t new_tailroom) | |
235 | { | |
236 | void *new_base, *new_data; | |
237 | size_t new_allocated; | |
238 | ||
239 | new_allocated = new_headroom + dp_packet_size(b) + new_tailroom; | |
240 | ||
241 | switch (b->source) { | |
242 | case DPBUF_DPDK: | |
243 | OVS_NOT_REACHED(); | |
244 | ||
245 | case DPBUF_MALLOC: | |
246 | if (new_headroom == dp_packet_headroom(b)) { | |
247 | new_base = xrealloc(dp_packet_base(b), new_allocated); | |
248 | } else { | |
249 | new_base = xmalloc(new_allocated); | |
250 | dp_packet_copy__(b, new_base, new_headroom, new_tailroom); | |
251 | free(dp_packet_base(b)); | |
252 | } | |
253 | break; | |
91088554 | 254 | |
cf62fa4c PS |
255 | case DPBUF_STACK: |
256 | OVS_NOT_REACHED(); | |
257 | ||
258 | case DPBUF_STUB: | |
259 | b->source = DPBUF_MALLOC; | |
260 | new_base = xmalloc(new_allocated); | |
261 | dp_packet_copy__(b, new_base, new_headroom, new_tailroom); | |
262 | break; | |
263 | ||
264 | default: | |
265 | OVS_NOT_REACHED(); | |
266 | } | |
267 | ||
11a6fbd5 | 268 | dp_packet_set_allocated(b, new_allocated); |
cf62fa4c PS |
269 | dp_packet_set_base(b, new_base); |
270 | ||
271 | new_data = (char *) new_base + new_headroom; | |
272 | if (dp_packet_data(b) != new_data) { | |
cf62fa4c | 273 | dp_packet_set_data(b, new_data); |
91088554 | 274 | } |
cf62fa4c | 275 | } |
91088554 | 276 | |
cf62fa4c PS |
277 | /* Ensures that 'b' has room for at least 'size' bytes at its tail end, |
278 | * reallocating and copying its data if necessary. Its headroom, if any, is | |
279 | * preserved. */ | |
280 | void | |
281 | dp_packet_prealloc_tailroom(struct dp_packet *b, size_t size) | |
282 | { | |
283 | if (size > dp_packet_tailroom(b)) { | |
284 | dp_packet_resize__(b, dp_packet_headroom(b), MAX(size, 64)); | |
285 | } | |
286 | } | |
287 | ||
288 | /* Ensures that 'b' has room for at least 'size' bytes at its head, | |
289 | * reallocating and copying its data if necessary. Its tailroom, if any, is | |
290 | * preserved. */ | |
291 | void | |
292 | dp_packet_prealloc_headroom(struct dp_packet *b, size_t size) | |
293 | { | |
294 | if (size > dp_packet_headroom(b)) { | |
295 | dp_packet_resize__(b, MAX(size, 64), dp_packet_tailroom(b)); | |
296 | } | |
297 | } | |
298 | ||
299 | /* Shifts all of the data within the allocated space in 'b' by 'delta' bytes. | |
300 | * For example, a 'delta' of 1 would cause each byte of data to move one byte | |
301 | * forward (from address 'p' to 'p+1'), and a 'delta' of -1 would cause each | |
302 | * byte to move one byte backward (from 'p' to 'p-1'). */ | |
303 | void | |
304 | dp_packet_shift(struct dp_packet *b, int delta) | |
305 | { | |
306 | ovs_assert(delta > 0 ? delta <= dp_packet_tailroom(b) | |
307 | : delta < 0 ? -delta <= dp_packet_headroom(b) | |
308 | : true); | |
309 | ||
310 | if (delta != 0) { | |
311 | char *dst = (char *) dp_packet_data(b) + delta; | |
312 | memmove(dst, dp_packet_data(b), dp_packet_size(b)); | |
313 | dp_packet_set_data(b, dst); | |
314 | } | |
315 | } | |
316 | ||
317 | /* Appends 'size' bytes of data to the tail end of 'b', reallocating and | |
318 | * copying its data if necessary. Returns a pointer to the first byte of the | |
319 | * new data, which is left uninitialized. */ | |
320 | void * | |
321 | dp_packet_put_uninit(struct dp_packet *b, size_t size) | |
322 | { | |
323 | void *p; | |
324 | dp_packet_prealloc_tailroom(b, size); | |
325 | p = dp_packet_tail(b); | |
326 | dp_packet_set_size(b, dp_packet_size(b) + size); | |
91088554 DDP |
327 | return p; |
328 | } | |
329 | ||
cf62fa4c PS |
330 | /* Appends 'size' zeroed bytes to the tail end of 'b'. Data in 'b' is |
331 | * reallocated and copied if necessary. Returns a pointer to the first byte of | |
332 | * the data's location in the dp_packet. */ | |
333 | void * | |
334 | dp_packet_put_zeros(struct dp_packet *b, size_t size) | |
335 | { | |
336 | void *dst = dp_packet_put_uninit(b, size); | |
337 | memset(dst, 0, size); | |
338 | return dst; | |
339 | } | |
340 | ||
341 | /* Appends the 'size' bytes of data in 'p' to the tail end of 'b'. Data in 'b' | |
342 | * is reallocated and copied if necessary. Returns a pointer to the first | |
343 | * byte of the data's location in the dp_packet. */ | |
344 | void * | |
345 | dp_packet_put(struct dp_packet *b, const void *p, size_t size) | |
346 | { | |
347 | void *dst = dp_packet_put_uninit(b, size); | |
348 | memcpy(dst, p, size); | |
349 | return dst; | |
350 | } | |
351 | ||
352 | /* Parses as many pairs of hex digits as possible (possibly separated by | |
353 | * spaces) from the beginning of 's', appending bytes for their values to 'b'. | |
354 | * Returns the first character of 's' that is not the first of a pair of hex | |
355 | * digits. If 'n' is nonnull, stores the number of bytes added to 'b' in | |
356 | * '*n'. */ | |
357 | char * | |
358 | dp_packet_put_hex(struct dp_packet *b, const char *s, size_t *n) | |
359 | { | |
360 | size_t initial_size = dp_packet_size(b); | |
361 | for (;;) { | |
362 | uint8_t byte; | |
363 | bool ok; | |
364 | ||
365 | s += strspn(s, " \t\r\n"); | |
366 | byte = hexits_value(s, 2, &ok); | |
367 | if (!ok) { | |
368 | if (n) { | |
369 | *n = dp_packet_size(b) - initial_size; | |
370 | } | |
371 | return CONST_CAST(char *, s); | |
372 | } | |
373 | ||
374 | dp_packet_put(b, &byte, 1); | |
375 | s += 2; | |
376 | } | |
377 | } | |
378 | ||
379 | /* Reserves 'size' bytes of headroom so that they can be later allocated with | |
380 | * dp_packet_push_uninit() without reallocating the dp_packet. */ | |
381 | void | |
382 | dp_packet_reserve(struct dp_packet *b, size_t size) | |
91088554 | 383 | { |
cf62fa4c PS |
384 | ovs_assert(!dp_packet_size(b)); |
385 | dp_packet_prealloc_tailroom(b, size); | |
386 | dp_packet_set_data(b, (char*)dp_packet_data(b) + size); | |
387 | } | |
91088554 | 388 | |
cf62fa4c PS |
389 | /* Reserves 'headroom' bytes at the head and 'tailroom' at the end so that |
390 | * they can be later allocated with dp_packet_push_uninit() or | |
391 | * dp_packet_put_uninit() without reallocating the dp_packet. */ | |
392 | void | |
393 | dp_packet_reserve_with_tailroom(struct dp_packet *b, size_t headroom, | |
394 | size_t tailroom) | |
395 | { | |
396 | ovs_assert(!dp_packet_size(b)); | |
397 | dp_packet_prealloc_tailroom(b, headroom + tailroom); | |
398 | dp_packet_set_data(b, (char*)dp_packet_data(b) + headroom); | |
399 | } | |
91088554 | 400 | |
cf62fa4c PS |
401 | /* Prefixes 'size' bytes to the head end of 'b', reallocating and copying its |
402 | * data if necessary. Returns a pointer to the first byte of the data's | |
403 | * location in the dp_packet. The new data is left uninitialized. */ | |
404 | void * | |
405 | dp_packet_push_uninit(struct dp_packet *b, size_t size) | |
406 | { | |
407 | dp_packet_prealloc_headroom(b, size); | |
408 | dp_packet_set_data(b, (char*)dp_packet_data(b) - size); | |
409 | dp_packet_set_size(b, dp_packet_size(b) + size); | |
410 | return dp_packet_data(b); | |
411 | } | |
8cbf4f47 | 412 | |
cf62fa4c PS |
413 | /* Prefixes 'size' zeroed bytes to the head end of 'b', reallocating and |
414 | * copying its data if necessary. Returns a pointer to the first byte of the | |
415 | * data's location in the dp_packet. */ | |
416 | void * | |
417 | dp_packet_push_zeros(struct dp_packet *b, size_t size) | |
418 | { | |
419 | void *dst = dp_packet_push_uninit(b, size); | |
420 | memset(dst, 0, size); | |
421 | return dst; | |
422 | } | |
423 | ||
424 | /* Copies the 'size' bytes starting at 'p' to the head end of 'b', reallocating | |
425 | * and copying its data if necessary. Returns a pointer to the first byte of | |
426 | * the data's location in the dp_packet. */ | |
427 | void * | |
428 | dp_packet_push(struct dp_packet *b, const void *p, size_t size) | |
429 | { | |
430 | void *dst = dp_packet_push_uninit(b, size); | |
431 | memcpy(dst, p, size); | |
432 | return dst; | |
433 | } | |
434 | ||
435 | /* Returns the data in 'b' as a block of malloc()'d memory and frees the buffer | |
436 | * within 'b'. (If 'b' itself was dynamically allocated, e.g. with | |
437 | * dp_packet_new(), then it should still be freed with, e.g., dp_packet_delete().) */ | |
438 | void * | |
439 | dp_packet_steal_data(struct dp_packet *b) | |
440 | { | |
441 | void *p; | |
442 | ovs_assert(b->source != DPBUF_DPDK); | |
443 | ||
444 | if (b->source == DPBUF_MALLOC && dp_packet_data(b) == dp_packet_base(b)) { | |
445 | p = dp_packet_data(b); | |
446 | } else { | |
447 | p = xmemdup(dp_packet_data(b), dp_packet_size(b)); | |
448 | if (b->source == DPBUF_MALLOC) { | |
449 | free(dp_packet_base(b)); | |
450 | } | |
451 | } | |
452 | dp_packet_set_base(b, NULL); | |
453 | dp_packet_set_data(b, NULL); | |
454 | return p; | |
455 | } | |
456 | ||
cf62fa4c PS |
457 | static inline void |
458 | dp_packet_adjust_layer_offset(uint16_t *offset, int increment) | |
459 | { | |
460 | if (*offset != UINT16_MAX) { | |
461 | *offset += increment; | |
462 | } | |
463 | } | |
464 | ||
465 | /* Adjust the size of the l2_5 portion of the dp_packet, updating the l2 | |
466 | * pointer and the layer offsets. The caller is responsible for | |
467 | * modifying the contents. */ | |
468 | void * | |
469 | dp_packet_resize_l2_5(struct dp_packet *b, int increment) | |
470 | { | |
471 | if (increment >= 0) { | |
472 | dp_packet_push_uninit(b, increment); | |
473 | } else { | |
474 | dp_packet_pull(b, -increment); | |
475 | } | |
476 | ||
cf62fa4c PS |
477 | /* Adjust layer offsets after l2_5. */ |
478 | dp_packet_adjust_layer_offset(&b->l3_ofs, increment); | |
479 | dp_packet_adjust_layer_offset(&b->l4_ofs, increment); | |
480 | ||
82eb5b0a | 481 | return dp_packet_data(b); |
cf62fa4c PS |
482 | } |
483 | ||
484 | /* Adjust the size of the l2 portion of the dp_packet, updating the l2 | |
485 | * pointer and the layer offsets. The caller is responsible for | |
486 | * modifying the contents. */ | |
487 | void * | |
488 | dp_packet_resize_l2(struct dp_packet *b, int increment) | |
489 | { | |
490 | dp_packet_resize_l2_5(b, increment); | |
491 | dp_packet_adjust_layer_offset(&b->l2_5_ofs, increment); | |
82eb5b0a | 492 | return dp_packet_data(b); |
91088554 | 493 | } |