]> git.proxmox.com Git - mirror_ovs.git/blob - lib/dp-packet.c
stopwatch: Remove tabs from output.
[mirror_ovs.git] / lib / dp-packet.c
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-dpdk.h"
23 #include "openvswitch/dynamic-string.h"
24 #include "util.h"
25
26 static void
27 dp_packet_init__(struct dp_packet *b, size_t allocated, enum dp_packet_source source)
28 {
29 dp_packet_set_allocated(b, allocated);
30 b->source = source;
31 dp_packet_reset_offsets(b);
32 pkt_metadata_init(&b->md, 0);
33 dp_packet_rss_invalidate(b);
34 dp_packet_mbuf_init(b);
35 dp_packet_reset_cutlen(b);
36 /* By default assume the packet type to be Ethernet. */
37 b->packet_type = htonl(PT_ETH);
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
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. */
101 void
102 dp_packet_init_dpdk(struct dp_packet *b, size_t allocated)
103 {
104 dp_packet_set_allocated(b, allocated);
105 b->source = DPBUF_DPDK;
106 }
107
108 /* Initializes 'b' as an empty dp_packet with an initial capacity of 'size'
109 * bytes. */
110 void
111 dp_packet_init(struct dp_packet *b, size_t size)
112 {
113 dp_packet_use(b, size ? xmalloc(size) : NULL, size);
114 }
115
116 /* Frees memory that 'b' points to. */
117 void
118 dp_packet_uninit(struct dp_packet *b)
119 {
120 if (b) {
121 if (b->source == DPBUF_MALLOC) {
122 free(dp_packet_base(b));
123 } else if (b->source == DPBUF_DPDK) {
124 #ifdef DPDK_NETDEV
125 /* If this dp_packet was allocated by DPDK it must have been
126 * created as a dp_packet */
127 free_dpdk_buf((struct dp_packet*) b);
128 #endif
129 }
130 }
131 }
132
133 /* Creates and returns a new dp_packet with an initial capacity of 'size'
134 * bytes. */
135 struct dp_packet *
136 dp_packet_new(size_t size)
137 {
138 struct dp_packet *b = xmalloc(sizeof *b);
139 dp_packet_init(b, size);
140 return b;
141 }
142
143 /* Creates and returns a new dp_packet with an initial capacity of 'size +
144 * headroom' bytes, reserving the first 'headroom' bytes as headroom. */
145 struct dp_packet *
146 dp_packet_new_with_headroom(size_t size, size_t headroom)
147 {
148 struct dp_packet *b = dp_packet_new(size + headroom);
149 dp_packet_reserve(b, headroom);
150 return b;
151 }
152
153 /* Creates and returns a new dp_packet that initially contains a copy of the
154 * 'dp_packet_size(buffer)' bytes of data starting at 'buffer->data' with no headroom or
155 * tailroom. */
156 struct dp_packet *
157 dp_packet_clone(const struct dp_packet *buffer)
158 {
159 return dp_packet_clone_with_headroom(buffer, 0);
160 }
161
162 /* Creates and returns a new dp_packet whose data are copied from 'buffer'.
163 * The returned dp_packet will additionally have 'headroom' bytes of
164 * headroom. */
165 struct dp_packet *
166 dp_packet_clone_with_headroom(const struct dp_packet *buffer, size_t headroom)
167 {
168 struct dp_packet *new_buffer;
169
170 new_buffer = dp_packet_clone_data_with_headroom(dp_packet_data(buffer),
171 dp_packet_size(buffer),
172 headroom);
173 /* Copy the following fields into the returned buffer: l2_pad_size,
174 * l2_5_ofs, l3_ofs, l4_ofs, cutlen, packet_type and md. */
175 memcpy(&new_buffer->l2_pad_size, &buffer->l2_pad_size,
176 sizeof(struct dp_packet) -
177 offsetof(struct dp_packet, l2_pad_size));
178
179 #ifdef DPDK_NETDEV
180 new_buffer->mbuf.ol_flags = buffer->mbuf.ol_flags;
181 #else
182 new_buffer->rss_hash_valid = buffer->rss_hash_valid;
183 #endif
184
185 if (dp_packet_rss_valid(new_buffer)) {
186 #ifdef DPDK_NETDEV
187 new_buffer->mbuf.hash.rss = buffer->mbuf.hash.rss;
188 #else
189 new_buffer->rss_hash = buffer->rss_hash;
190 #endif
191 }
192
193 return new_buffer;
194 }
195
196 /* Creates and returns a new dp_packet that initially contains a copy of the
197 * 'size' bytes of data starting at 'data' with no headroom or tailroom. */
198 struct dp_packet *
199 dp_packet_clone_data(const void *data, size_t size)
200 {
201 return dp_packet_clone_data_with_headroom(data, size, 0);
202 }
203
204 /* Creates and returns a new dp_packet that initially contains 'headroom' bytes of
205 * headroom followed by a copy of the 'size' bytes of data starting at
206 * 'data'. */
207 struct dp_packet *
208 dp_packet_clone_data_with_headroom(const void *data, size_t size, size_t headroom)
209 {
210 struct dp_packet *b = dp_packet_new_with_headroom(size, headroom);
211 dp_packet_put(b, data, size);
212 return b;
213 }
214
215 static void
216 dp_packet_copy__(struct dp_packet *b, uint8_t *new_base,
217 size_t new_headroom, size_t new_tailroom)
218 {
219 const uint8_t *old_base = dp_packet_base(b);
220 size_t old_headroom = dp_packet_headroom(b);
221 size_t old_tailroom = dp_packet_tailroom(b);
222 size_t copy_headroom = MIN(old_headroom, new_headroom);
223 size_t copy_tailroom = MIN(old_tailroom, new_tailroom);
224
225 memcpy(&new_base[new_headroom - copy_headroom],
226 &old_base[old_headroom - copy_headroom],
227 copy_headroom + dp_packet_size(b) + copy_tailroom);
228 }
229
230 /* Reallocates 'b' so that it has exactly 'new_headroom' and 'new_tailroom'
231 * bytes of headroom and tailroom, respectively. */
232 static void
233 dp_packet_resize__(struct dp_packet *b, size_t new_headroom, size_t new_tailroom)
234 {
235 void *new_base, *new_data;
236 size_t new_allocated;
237
238 new_allocated = new_headroom + dp_packet_size(b) + new_tailroom;
239
240 switch (b->source) {
241 case DPBUF_DPDK:
242 OVS_NOT_REACHED();
243
244 case DPBUF_MALLOC:
245 if (new_headroom == dp_packet_headroom(b)) {
246 new_base = xrealloc(dp_packet_base(b), new_allocated);
247 } else {
248 new_base = xmalloc(new_allocated);
249 dp_packet_copy__(b, new_base, new_headroom, new_tailroom);
250 free(dp_packet_base(b));
251 }
252 break;
253
254 case DPBUF_STACK:
255 OVS_NOT_REACHED();
256
257 case DPBUF_STUB:
258 b->source = DPBUF_MALLOC;
259 new_base = xmalloc(new_allocated);
260 dp_packet_copy__(b, new_base, new_headroom, new_tailroom);
261 break;
262
263 default:
264 OVS_NOT_REACHED();
265 }
266
267 dp_packet_set_allocated(b, new_allocated);
268 dp_packet_set_base(b, new_base);
269
270 new_data = (char *) new_base + new_headroom;
271 if (dp_packet_data(b) != new_data) {
272 dp_packet_set_data(b, new_data);
273 }
274 }
275
276 /* Ensures that 'b' has room for at least 'size' bytes at its tail end,
277 * reallocating and copying its data if necessary. Its headroom, if any, is
278 * preserved. */
279 void
280 dp_packet_prealloc_tailroom(struct dp_packet *b, size_t size)
281 {
282 if (size > dp_packet_tailroom(b)) {
283 dp_packet_resize__(b, dp_packet_headroom(b), MAX(size, 64));
284 }
285 }
286
287 /* Ensures that 'b' has room for at least 'size' bytes at its head,
288 * reallocating and copying its data if necessary. Its tailroom, if any, is
289 * preserved. */
290 void
291 dp_packet_prealloc_headroom(struct dp_packet *b, size_t size)
292 {
293 if (size > dp_packet_headroom(b)) {
294 dp_packet_resize__(b, MAX(size, 64), dp_packet_tailroom(b));
295 }
296 }
297
298 /* Shifts all of the data within the allocated space in 'b' by 'delta' bytes.
299 * For example, a 'delta' of 1 would cause each byte of data to move one byte
300 * forward (from address 'p' to 'p+1'), and a 'delta' of -1 would cause each
301 * byte to move one byte backward (from 'p' to 'p-1'). */
302 void
303 dp_packet_shift(struct dp_packet *b, int delta)
304 {
305 ovs_assert(delta > 0 ? delta <= dp_packet_tailroom(b)
306 : delta < 0 ? -delta <= dp_packet_headroom(b)
307 : true);
308
309 if (delta != 0) {
310 char *dst = (char *) dp_packet_data(b) + delta;
311 memmove(dst, dp_packet_data(b), dp_packet_size(b));
312 dp_packet_set_data(b, dst);
313 }
314 }
315
316 /* Appends 'size' bytes of data to the tail end of 'b', reallocating and
317 * copying its data if necessary. Returns a pointer to the first byte of the
318 * new data, which is left uninitialized. */
319 void *
320 dp_packet_put_uninit(struct dp_packet *b, size_t size)
321 {
322 void *p;
323 dp_packet_prealloc_tailroom(b, size);
324 p = dp_packet_tail(b);
325 dp_packet_set_size(b, dp_packet_size(b) + size);
326 return p;
327 }
328
329 /* Appends 'size' zeroed bytes to the tail end of 'b'. Data in 'b' is
330 * reallocated and copied if necessary. Returns a pointer to the first byte of
331 * the data's location in the dp_packet. */
332 void *
333 dp_packet_put_zeros(struct dp_packet *b, size_t size)
334 {
335 void *dst = dp_packet_put_uninit(b, size);
336 memset(dst, 0, size);
337 return dst;
338 }
339
340 /* Appends the 'size' bytes of data in 'p' to the tail end of 'b'. Data in 'b'
341 * is reallocated and copied if necessary. Returns a pointer to the first
342 * byte of the data's location in the dp_packet. */
343 void *
344 dp_packet_put(struct dp_packet *b, const void *p, size_t size)
345 {
346 void *dst = dp_packet_put_uninit(b, size);
347 memcpy(dst, p, size);
348 return dst;
349 }
350
351 /* Parses as many pairs of hex digits as possible (possibly separated by
352 * spaces) from the beginning of 's', appending bytes for their values to 'b'.
353 * Returns the first character of 's' that is not the first of a pair of hex
354 * digits. If 'n' is nonnull, stores the number of bytes added to 'b' in
355 * '*n'. */
356 char *
357 dp_packet_put_hex(struct dp_packet *b, const char *s, size_t *n)
358 {
359 size_t initial_size = dp_packet_size(b);
360 for (;;) {
361 uint8_t byte;
362 bool ok;
363
364 s += strspn(s, " \t\r\n");
365 byte = hexits_value(s, 2, &ok);
366 if (!ok) {
367 if (n) {
368 *n = dp_packet_size(b) - initial_size;
369 }
370 return CONST_CAST(char *, s);
371 }
372
373 dp_packet_put(b, &byte, 1);
374 s += 2;
375 }
376 }
377
378 /* Reserves 'size' bytes of headroom so that they can be later allocated with
379 * dp_packet_push_uninit() without reallocating the dp_packet. */
380 void
381 dp_packet_reserve(struct dp_packet *b, size_t size)
382 {
383 ovs_assert(!dp_packet_size(b));
384 dp_packet_prealloc_tailroom(b, size);
385 dp_packet_set_data(b, (char*)dp_packet_data(b) + size);
386 }
387
388 /* Reserves 'headroom' bytes at the head and 'tailroom' at the end so that
389 * they can be later allocated with dp_packet_push_uninit() or
390 * dp_packet_put_uninit() without reallocating the dp_packet. */
391 void
392 dp_packet_reserve_with_tailroom(struct dp_packet *b, size_t headroom,
393 size_t tailroom)
394 {
395 ovs_assert(!dp_packet_size(b));
396 dp_packet_prealloc_tailroom(b, headroom + tailroom);
397 dp_packet_set_data(b, (char*)dp_packet_data(b) + headroom);
398 }
399
400 /* Prefixes 'size' bytes to the head end of 'b', reallocating and copying its
401 * data if necessary. Returns a pointer to the first byte of the data's
402 * location in the dp_packet. The new data is left uninitialized. */
403 void *
404 dp_packet_push_uninit(struct dp_packet *b, size_t size)
405 {
406 dp_packet_prealloc_headroom(b, size);
407 dp_packet_set_data(b, (char*)dp_packet_data(b) - size);
408 dp_packet_set_size(b, dp_packet_size(b) + size);
409 return dp_packet_data(b);
410 }
411
412 /* Prefixes 'size' zeroed bytes to the head end of 'b', reallocating and
413 * copying its data if necessary. Returns a pointer to the first byte of the
414 * data's location in the dp_packet. */
415 void *
416 dp_packet_push_zeros(struct dp_packet *b, size_t size)
417 {
418 void *dst = dp_packet_push_uninit(b, size);
419 memset(dst, 0, size);
420 return dst;
421 }
422
423 /* Copies the 'size' bytes starting at 'p' to the head end of 'b', reallocating
424 * and copying its data if necessary. Returns a pointer to the first byte of
425 * the data's location in the dp_packet. */
426 void *
427 dp_packet_push(struct dp_packet *b, const void *p, size_t size)
428 {
429 void *dst = dp_packet_push_uninit(b, size);
430 memcpy(dst, p, size);
431 return dst;
432 }
433
434 /* Returns the data in 'b' as a block of malloc()'d memory and frees the buffer
435 * within 'b'. (If 'b' itself was dynamically allocated, e.g. with
436 * dp_packet_new(), then it should still be freed with, e.g., dp_packet_delete().) */
437 void *
438 dp_packet_steal_data(struct dp_packet *b)
439 {
440 void *p;
441 ovs_assert(b->source != DPBUF_DPDK);
442
443 if (b->source == DPBUF_MALLOC && dp_packet_data(b) == dp_packet_base(b)) {
444 p = dp_packet_data(b);
445 } else {
446 p = xmemdup(dp_packet_data(b), dp_packet_size(b));
447 if (b->source == DPBUF_MALLOC) {
448 free(dp_packet_base(b));
449 }
450 }
451 dp_packet_set_base(b, NULL);
452 dp_packet_set_data(b, NULL);
453 return p;
454 }
455
456 static inline void
457 dp_packet_adjust_layer_offset(uint16_t *offset, int increment)
458 {
459 if (*offset != UINT16_MAX) {
460 *offset += increment;
461 }
462 }
463
464 /* Adjust the size of the l2_5 portion of the dp_packet, updating the l2
465 * pointer and the layer offsets. The caller is responsible for
466 * modifying the contents. */
467 void *
468 dp_packet_resize_l2_5(struct dp_packet *b, int increment)
469 {
470 if (increment >= 0) {
471 dp_packet_push_uninit(b, increment);
472 } else {
473 dp_packet_pull(b, -increment);
474 }
475
476 /* Adjust layer offsets after l2_5. */
477 dp_packet_adjust_layer_offset(&b->l3_ofs, increment);
478 dp_packet_adjust_layer_offset(&b->l4_ofs, increment);
479
480 return dp_packet_data(b);
481 }
482
483 /* Adjust the size of the l2 portion of the dp_packet, updating the l2
484 * pointer and the layer offsets. The caller is responsible for
485 * modifying the contents. */
486 void *
487 dp_packet_resize_l2(struct dp_packet *b, int increment)
488 {
489 dp_packet_resize_l2_5(b, increment);
490 dp_packet_adjust_layer_offset(&b->l2_5_ofs, increment);
491 return dp_packet_data(b);
492 }