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