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