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