]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/dp-packet.c
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2016 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "dp-packet.h"
22 #include "netdev-dpdk.h"
23 #include "openvswitch/dynamic-string.h"
27 dp_packet_init__(struct dp_packet
*b
, size_t allocated
, enum dp_packet_source source
)
29 dp_packet_set_allocated(b
, allocated
);
31 dp_packet_reset_offsets(b
);
32 pkt_metadata_init(&b
->md
, 0);
33 dp_packet_rss_invalidate(b
);
34 dp_packet_mbuf_init(b
);
35 dp_packet_reset_cutlen(b
);
36 /* By default assume the packet type to be Ethernet. */
37 b
->packet_type
= htonl(PT_ETH
);
41 dp_packet_use__(struct dp_packet
*b
, void *base
, size_t allocated
,
42 enum dp_packet_source source
)
44 dp_packet_set_base(b
, base
);
45 dp_packet_set_data(b
, base
);
46 dp_packet_set_size(b
, 0);
48 dp_packet_init__(b
, allocated
, source
);
51 /* Initializes 'b' as an empty dp_packet that contains the 'allocated' bytes of
52 * memory starting at 'base'. 'base' should be the first byte of a region
53 * obtained from malloc(). It will be freed (with free()) if 'b' is resized or
56 dp_packet_use(struct dp_packet
*b
, void *base
, size_t allocated
)
58 dp_packet_use__(b
, base
, allocated
, DPBUF_MALLOC
);
61 /* Initializes 'b' as an empty dp_packet that contains the 'allocated' bytes of
62 * memory starting at 'base'. 'base' should point to a buffer on the stack.
63 * (Nothing actually relies on 'base' being allocated on the stack. It could
64 * be static or malloc()'d memory. But stack space is the most common use
67 * 'base' should be appropriately aligned. Using an array of uint32_t or
68 * uint64_t for the buffer is a reasonable way to ensure appropriate alignment
69 * for 32- or 64-bit data.
71 * An dp_packet operation that requires reallocating data will copy the provided
72 * buffer into a malloc()'d buffer. Thus, it is wise to call dp_packet_uninit()
73 * on an dp_packet initialized by this function, so that if it expanded into the
74 * heap, that memory is freed. */
76 dp_packet_use_stub(struct dp_packet
*b
, void *base
, size_t allocated
)
78 dp_packet_use__(b
, base
, allocated
, DPBUF_STUB
);
81 /* Initializes 'b' as an dp_packet whose data starts at 'data' and continues for
82 * 'size' bytes. This is appropriate for an dp_packet that will be used to
83 * inspect existing data, without moving it around or reallocating it, and
84 * generally without modifying it at all.
86 * An dp_packet operation that requires reallocating data will assert-fail if this
87 * function was used to initialize it. */
89 dp_packet_use_const(struct dp_packet
*b
, const void *data
, size_t size
)
91 dp_packet_use__(b
, CONST_CAST(void *, data
), size
, DPBUF_STACK
);
92 dp_packet_set_size(b
, size
);
95 /* Initializes 'b' as an empty dp_packet that contains the 'allocated' bytes.
96 * DPDK allocated dp_packet and *data is allocated from one continous memory
97 * region as part of memory pool, so in memory data start right after
98 * dp_packet. Therefore, there is a special method to free this type of
99 * buffer. Here, non-transient ovs dp-packet fields are initialized for
100 * packets that are part of a DPDK memory pool. */
102 dp_packet_init_dpdk(struct dp_packet
*b
, size_t allocated
)
104 dp_packet_set_allocated(b
, allocated
);
105 b
->source
= DPBUF_DPDK
;
108 /* Initializes 'b' as an empty dp_packet with an initial capacity of 'size'
111 dp_packet_init(struct dp_packet
*b
, size_t size
)
113 dp_packet_use(b
, size
? xmalloc(size
) : NULL
, size
);
116 /* Frees memory that 'b' points to. */
118 dp_packet_uninit(struct dp_packet
*b
)
121 if (b
->source
== DPBUF_MALLOC
) {
122 free(dp_packet_base(b
));
123 } else if (b
->source
== DPBUF_DPDK
) {
125 /* If this dp_packet was allocated by DPDK it must have been
126 * created as a dp_packet */
127 free_dpdk_buf((struct dp_packet
*) b
);
133 /* Creates and returns a new dp_packet with an initial capacity of 'size'
136 dp_packet_new(size_t size
)
138 struct dp_packet
*b
= xmalloc(sizeof *b
);
139 dp_packet_init(b
, size
);
143 /* Creates and returns a new dp_packet with an initial capacity of 'size +
144 * headroom' bytes, reserving the first 'headroom' bytes as headroom. */
146 dp_packet_new_with_headroom(size_t size
, size_t headroom
)
148 struct dp_packet
*b
= dp_packet_new(size
+ headroom
);
149 dp_packet_reserve(b
, headroom
);
153 /* Creates and returns a new dp_packet that initially contains a copy of the
154 * 'dp_packet_size(buffer)' bytes of data starting at 'buffer->data' with no headroom or
157 dp_packet_clone(const struct dp_packet
*buffer
)
159 return dp_packet_clone_with_headroom(buffer
, 0);
162 /* Creates and returns a new dp_packet whose data are copied from 'buffer'.
163 * The returned dp_packet will additionally have 'headroom' bytes of
166 dp_packet_clone_with_headroom(const struct dp_packet
*buffer
, size_t headroom
)
168 struct dp_packet
*new_buffer
;
170 new_buffer
= dp_packet_clone_data_with_headroom(dp_packet_data(buffer
),
171 dp_packet_size(buffer
),
173 /* Copy the following fields into the returned buffer: l2_pad_size,
174 * l2_5_ofs, l3_ofs, l4_ofs, cutlen, packet_type and md. */
175 memcpy(&new_buffer
->l2_pad_size
, &buffer
->l2_pad_size
,
176 sizeof(struct dp_packet
) -
177 offsetof(struct dp_packet
, l2_pad_size
));
180 new_buffer
->mbuf
.ol_flags
= buffer
->mbuf
.ol_flags
;
182 new_buffer
->rss_hash_valid
= buffer
->rss_hash_valid
;
185 if (dp_packet_rss_valid(new_buffer
)) {
187 new_buffer
->mbuf
.hash
.rss
= buffer
->mbuf
.hash
.rss
;
189 new_buffer
->rss_hash
= buffer
->rss_hash
;
196 /* Creates and returns a new dp_packet that initially contains a copy of the
197 * 'size' bytes of data starting at 'data' with no headroom or tailroom. */
199 dp_packet_clone_data(const void *data
, size_t size
)
201 return dp_packet_clone_data_with_headroom(data
, size
, 0);
204 /* Creates and returns a new dp_packet that initially contains 'headroom' bytes of
205 * headroom followed by a copy of the 'size' bytes of data starting at
208 dp_packet_clone_data_with_headroom(const void *data
, size_t size
, size_t headroom
)
210 struct dp_packet
*b
= dp_packet_new_with_headroom(size
, headroom
);
211 dp_packet_put(b
, data
, size
);
216 dp_packet_copy__(struct dp_packet
*b
, uint8_t *new_base
,
217 size_t new_headroom
, size_t new_tailroom
)
219 const uint8_t *old_base
= dp_packet_base(b
);
220 size_t old_headroom
= dp_packet_headroom(b
);
221 size_t old_tailroom
= dp_packet_tailroom(b
);
222 size_t copy_headroom
= MIN(old_headroom
, new_headroom
);
223 size_t copy_tailroom
= MIN(old_tailroom
, new_tailroom
);
225 memcpy(&new_base
[new_headroom
- copy_headroom
],
226 &old_base
[old_headroom
- copy_headroom
],
227 copy_headroom
+ dp_packet_size(b
) + copy_tailroom
);
230 /* Reallocates 'b' so that it has exactly 'new_headroom' and 'new_tailroom'
231 * bytes of headroom and tailroom, respectively. */
233 dp_packet_resize__(struct dp_packet
*b
, size_t new_headroom
, size_t new_tailroom
)
235 void *new_base
, *new_data
;
236 size_t new_allocated
;
238 new_allocated
= new_headroom
+ dp_packet_size(b
) + new_tailroom
;
245 if (new_headroom
== dp_packet_headroom(b
)) {
246 new_base
= xrealloc(dp_packet_base(b
), new_allocated
);
248 new_base
= xmalloc(new_allocated
);
249 dp_packet_copy__(b
, new_base
, new_headroom
, new_tailroom
);
250 free(dp_packet_base(b
));
258 b
->source
= DPBUF_MALLOC
;
259 new_base
= xmalloc(new_allocated
);
260 dp_packet_copy__(b
, new_base
, new_headroom
, new_tailroom
);
267 dp_packet_set_allocated(b
, new_allocated
);
268 dp_packet_set_base(b
, new_base
);
270 new_data
= (char *) new_base
+ new_headroom
;
271 if (dp_packet_data(b
) != new_data
) {
272 dp_packet_set_data(b
, new_data
);
276 /* Ensures that 'b' has room for at least 'size' bytes at its tail end,
277 * reallocating and copying its data if necessary. Its headroom, if any, is
280 dp_packet_prealloc_tailroom(struct dp_packet
*b
, size_t size
)
282 if (size
> dp_packet_tailroom(b
)) {
283 dp_packet_resize__(b
, dp_packet_headroom(b
), MAX(size
, 64));
287 /* Ensures that 'b' has room for at least 'size' bytes at its head,
288 * reallocating and copying its data if necessary. Its tailroom, if any, is
291 dp_packet_prealloc_headroom(struct dp_packet
*b
, size_t size
)
293 if (size
> dp_packet_headroom(b
)) {
294 dp_packet_resize__(b
, MAX(size
, 64), dp_packet_tailroom(b
));
298 /* Shifts all of the data within the allocated space in 'b' by 'delta' bytes.
299 * For example, a 'delta' of 1 would cause each byte of data to move one byte
300 * forward (from address 'p' to 'p+1'), and a 'delta' of -1 would cause each
301 * byte to move one byte backward (from 'p' to 'p-1'). */
303 dp_packet_shift(struct dp_packet
*b
, int delta
)
305 ovs_assert(delta
> 0 ? delta
<= dp_packet_tailroom(b
)
306 : delta
< 0 ? -delta
<= dp_packet_headroom(b
)
310 char *dst
= (char *) dp_packet_data(b
) + delta
;
311 memmove(dst
, dp_packet_data(b
), dp_packet_size(b
));
312 dp_packet_set_data(b
, dst
);
316 /* Appends 'size' bytes of data to the tail end of 'b', reallocating and
317 * copying its data if necessary. Returns a pointer to the first byte of the
318 * new data, which is left uninitialized. */
320 dp_packet_put_uninit(struct dp_packet
*b
, size_t size
)
323 dp_packet_prealloc_tailroom(b
, size
);
324 p
= dp_packet_tail(b
);
325 dp_packet_set_size(b
, dp_packet_size(b
) + size
);
329 /* Appends 'size' zeroed bytes to the tail end of 'b'. Data in 'b' is
330 * reallocated and copied if necessary. Returns a pointer to the first byte of
331 * the data's location in the dp_packet. */
333 dp_packet_put_zeros(struct dp_packet
*b
, size_t size
)
335 void *dst
= dp_packet_put_uninit(b
, size
);
336 memset(dst
, 0, size
);
340 /* Appends the 'size' bytes of data in 'p' to the tail end of 'b'. Data in 'b'
341 * is reallocated and copied if necessary. Returns a pointer to the first
342 * byte of the data's location in the dp_packet. */
344 dp_packet_put(struct dp_packet
*b
, const void *p
, size_t size
)
346 void *dst
= dp_packet_put_uninit(b
, size
);
347 memcpy(dst
, p
, size
);
351 /* Parses as many pairs of hex digits as possible (possibly separated by
352 * spaces) from the beginning of 's', appending bytes for their values to 'b'.
353 * Returns the first character of 's' that is not the first of a pair of hex
354 * digits. If 'n' is nonnull, stores the number of bytes added to 'b' in
357 dp_packet_put_hex(struct dp_packet
*b
, const char *s
, size_t *n
)
359 size_t initial_size
= dp_packet_size(b
);
364 s
+= strspn(s
, " \t\r\n");
365 byte
= hexits_value(s
, 2, &ok
);
368 *n
= dp_packet_size(b
) - initial_size
;
370 return CONST_CAST(char *, s
);
373 dp_packet_put(b
, &byte
, 1);
378 /* Reserves 'size' bytes of headroom so that they can be later allocated with
379 * dp_packet_push_uninit() without reallocating the dp_packet. */
381 dp_packet_reserve(struct dp_packet
*b
, size_t size
)
383 ovs_assert(!dp_packet_size(b
));
384 dp_packet_prealloc_tailroom(b
, size
);
385 dp_packet_set_data(b
, (char*)dp_packet_data(b
) + size
);
388 /* Reserves 'headroom' bytes at the head and 'tailroom' at the end so that
389 * they can be later allocated with dp_packet_push_uninit() or
390 * dp_packet_put_uninit() without reallocating the dp_packet. */
392 dp_packet_reserve_with_tailroom(struct dp_packet
*b
, size_t headroom
,
395 ovs_assert(!dp_packet_size(b
));
396 dp_packet_prealloc_tailroom(b
, headroom
+ tailroom
);
397 dp_packet_set_data(b
, (char*)dp_packet_data(b
) + headroom
);
400 /* Prefixes 'size' bytes to the head end of 'b', reallocating and copying its
401 * data if necessary. Returns a pointer to the first byte of the data's
402 * location in the dp_packet. The new data is left uninitialized. */
404 dp_packet_push_uninit(struct dp_packet
*b
, size_t size
)
406 dp_packet_prealloc_headroom(b
, size
);
407 dp_packet_set_data(b
, (char*)dp_packet_data(b
) - size
);
408 dp_packet_set_size(b
, dp_packet_size(b
) + size
);
409 return dp_packet_data(b
);
412 /* Prefixes 'size' zeroed bytes to the head end of 'b', reallocating and
413 * copying its data if necessary. Returns a pointer to the first byte of the
414 * data's location in the dp_packet. */
416 dp_packet_push_zeros(struct dp_packet
*b
, size_t size
)
418 void *dst
= dp_packet_push_uninit(b
, size
);
419 memset(dst
, 0, size
);
423 /* Copies the 'size' bytes starting at 'p' to the head end of 'b', reallocating
424 * and copying its data if necessary. Returns a pointer to the first byte of
425 * the data's location in the dp_packet. */
427 dp_packet_push(struct dp_packet
*b
, const void *p
, size_t size
)
429 void *dst
= dp_packet_push_uninit(b
, size
);
430 memcpy(dst
, p
, size
);
434 /* Returns the data in 'b' as a block of malloc()'d memory and frees the buffer
435 * within 'b'. (If 'b' itself was dynamically allocated, e.g. with
436 * dp_packet_new(), then it should still be freed with, e.g., dp_packet_delete().) */
438 dp_packet_steal_data(struct dp_packet
*b
)
441 ovs_assert(b
->source
!= DPBUF_DPDK
);
443 if (b
->source
== DPBUF_MALLOC
&& dp_packet_data(b
) == dp_packet_base(b
)) {
444 p
= dp_packet_data(b
);
446 p
= xmemdup(dp_packet_data(b
), dp_packet_size(b
));
447 if (b
->source
== DPBUF_MALLOC
) {
448 free(dp_packet_base(b
));
451 dp_packet_set_base(b
, NULL
);
452 dp_packet_set_data(b
, NULL
);
457 dp_packet_adjust_layer_offset(uint16_t *offset
, int increment
)
459 if (*offset
!= UINT16_MAX
) {
460 *offset
+= increment
;
464 /* Adjust the size of the l2_5 portion of the dp_packet, updating the l2
465 * pointer and the layer offsets. The caller is responsible for
466 * modifying the contents. */
468 dp_packet_resize_l2_5(struct dp_packet
*b
, int increment
)
470 if (increment
>= 0) {
471 dp_packet_push_uninit(b
, increment
);
473 dp_packet_pull(b
, -increment
);
476 /* Adjust layer offsets after l2_5. */
477 dp_packet_adjust_layer_offset(&b
->l3_ofs
, increment
);
478 dp_packet_adjust_layer_offset(&b
->l4_ofs
, increment
);
480 return dp_packet_data(b
);
483 /* Adjust the size of the l2 portion of the dp_packet, updating the l2
484 * pointer and the layer offsets. The caller is responsible for
485 * modifying the contents. */
487 dp_packet_resize_l2(struct dp_packet
*b
, int increment
)
489 dp_packet_resize_l2_5(b
, increment
);
490 dp_packet_adjust_layer_offset(&b
->l2_5_ofs
, increment
);
491 return dp_packet_data(b
);