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