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