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