X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fofpbuf.c;h=3de8ca5e234f535afd8bb3582c0b31d835d22b90;hb=8a9562d21a40c765a8ae6775a070cb279cb2147a;hp=c960a7e86f99f8fcee0c5a981a9d972c8fda8bf2;hpb=125638ebd7fa77fb98710ae0eb91b25172f9f725;p=sliver-openvswitch.git diff --git a/lib/ofpbuf.c b/lib/ofpbuf.c index c960a7e86..3de8ca5e2 100644 --- a/lib/ofpbuf.c +++ b/lib/ofpbuf.c @@ -19,6 +19,7 @@ #include #include #include "dynamic-string.h" +#include "netdev-dpdk.h" #include "util.h" static void @@ -67,7 +68,7 @@ ofpbuf_use_stack(struct ofpbuf *b, void *base, size_t allocated) /* Initializes 'b' as an empty ofpbuf that contains the 'allocated' bytes of * memory starting at 'base'. 'base' should point to a buffer on the stack. * (Nothing actually relies on 'base' being allocated on the stack. It could - * be static or malloc()'d memory. But stack space is the most common usen + * be static or malloc()'d memory. But stack space is the most common use * case.) * * 'base' should be appropriately aligned. Using an array of uint32_t or @@ -110,8 +111,13 @@ ofpbuf_init(struct ofpbuf *b, size_t size) void ofpbuf_uninit(struct ofpbuf *b) { - if (b && b->source == OFPBUF_MALLOC) { - free(b->base); + if (b) { + if (b->source == OFPBUF_MALLOC) { + free(b->base); + } + if (b->source == OFPBUF_DPDK) { + free_dpdk_buf(b); + } } } @@ -265,6 +271,9 @@ ofpbuf_resize__(struct ofpbuf *b, size_t new_headroom, size_t new_tailroom) new_allocated = new_headroom + b->size + new_tailroom; switch (b->source) { + case OFPBUF_DPDK: + OVS_NOT_REACHED(); + case OFPBUF_MALLOC: if (new_headroom == ofpbuf_headroom(b)) { new_base = xrealloc(b->base, new_allocated); @@ -276,7 +285,7 @@ ofpbuf_resize__(struct ofpbuf *b, size_t new_headroom, size_t new_tailroom) break; case OFPBUF_STACK: - NOT_REACHED(); + OVS_NOT_REACHED(); case OFPBUF_STUB: b->source = OFPBUF_MALLOC; @@ -285,7 +294,7 @@ ofpbuf_resize__(struct ofpbuf *b, size_t new_headroom, size_t new_tailroom) break; default: - NOT_REACHED(); + OVS_NOT_REACHED(); } b->allocated = new_allocated; @@ -343,6 +352,8 @@ ofpbuf_prealloc_headroom(struct ofpbuf *b, size_t size) void ofpbuf_trim(struct ofpbuf *b) { + ovs_assert(b->source != OFPBUF_DPDK); + if (b->source == OFPBUF_MALLOC && (ofpbuf_headroom(b) || ofpbuf_tailroom(b))) { ofpbuf_resize__(b, 0, 0); @@ -359,6 +370,24 @@ ofpbuf_padto(struct ofpbuf *b, size_t length) } } +/* Shifts all of the data within the allocated space in 'b' by 'delta' bytes. + * For example, a 'delta' of 1 would cause each byte of data to move one byte + * forward (from address 'p' to 'p+1'), and a 'delta' of -1 would cause each + * byte to move one byte backward (from 'p' to 'p-1'). */ +void +ofpbuf_shift(struct ofpbuf *b, int delta) +{ + ovs_assert(delta > 0 ? delta <= ofpbuf_tailroom(b) + : delta < 0 ? -delta <= ofpbuf_headroom(b) + : true); + + if (delta != 0) { + char *dst = (char *) b->data + delta; + memmove(dst, b->data, b->size); + b->data = dst; + } +} + /* Appends 'size' bytes of data to the tail end of 'b', reallocating and * copying its data if necessary. Returns a pointer to the first byte of the * new data, which is left uninitialized. */ @@ -407,7 +436,7 @@ ofpbuf_put_hex(struct ofpbuf *b, const char *s, size_t *n) uint8_t byte; bool ok; - s += strspn(s, " "); + s += strspn(s, " \t\r\n"); byte = hexits_value(s, 2, &ok); if (!ok) { if (n) { @@ -431,6 +460,17 @@ ofpbuf_reserve(struct ofpbuf *b, size_t size) b->data = (char*)b->data + size; } +/* Reserves 'size' bytes of headroom so that they can be later allocated with + * ofpbuf_push_uninit() without reallocating the ofpbuf. */ +void +ofpbuf_reserve_with_tailroom(struct ofpbuf *b, size_t headroom, + size_t tailroom) +{ + ovs_assert(!b->size); + ofpbuf_prealloc_tailroom(b, headroom + tailroom); + b->data = (char*)b->data + headroom; +} + /* Prefixes 'size' bytes to the head end of 'b', reallocating and copying its * data if necessary. Returns a pointer to the first byte of the data's * location in the ofpbuf. The new data is left uninitialized. */ @@ -533,6 +573,8 @@ void * ofpbuf_steal_data(struct ofpbuf *b) { void *p; + ovs_assert(b->source != OFPBUF_DPDK); + if (b->source == OFPBUF_MALLOC && b->data == b->base) { p = b->data; } else { @@ -553,7 +595,7 @@ ofpbuf_to_string(const struct ofpbuf *b, size_t maxbytes) struct ds s; ds_init(&s); - ds_put_format(&s, "size=%zu, allocated=%zu, head=%zu, tail=%zu\n", + ds_put_format(&s, "size=%"PRIuSIZE", allocated=%"PRIuSIZE", head=%"PRIuSIZE", tail=%"PRIuSIZE"\n", b->size, b->allocated, ofpbuf_headroom(b), ofpbuf_tailroom(b)); ds_put_hex_dump(&s, b->data, MIN(b->size, maxbytes), 0, false);