Skip to content

Commit

Permalink
net: ip: 6lo: Remove unnecessary net_buf allocation and memmove
Browse files Browse the repository at this point in the history
This patch tries to avoid memmoves and buffer allocations when there
is enough space in the original buffer. Headers are still contiguous
but not in the same buffer.

Signed-off-by: Alexander Wachter <alexander.wachter@student.tugraz.at>
  • Loading branch information
Alexander Wachter authored and jukkar committed Jul 25, 2019
1 parent f10da7b commit 144dc59
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions subsys/net/ip/6lo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,8 @@ static bool uncompress_IPHC_header(struct net_pkt *pkt)
}
}

net_pkt_cursor_init(pkt);

return true;

fail:
Expand All @@ -1482,17 +1484,24 @@ static bool uncompress_IPHC_header(struct net_pkt *pkt)
/* Adds IPv6 dispatch as first byte and adjust fragments */
static inline int compress_ipv6_header(struct net_pkt *pkt)
{
struct net_buf *frag;
struct net_buf *buffer = pkt->buffer;

frag = net_pkt_get_frag(pkt, K_FOREVER);
if (!frag) {
if (net_buf_tailroom(buffer) >= 1U) {
memmove(buffer->data + 1U, buffer->data, buffer->len);
net_buf_add(buffer, 1U);
buffer->data[0] = NET_6LO_DISPATCH_IPV6;
return 0;
}

buffer = net_pkt_get_frag(pkt, K_FOREVER);
if (!buffer) {
return -ENOBUFS;
}

frag->data[0] = NET_6LO_DISPATCH_IPV6;
net_buf_add(frag, 1);
buffer->data[0] = NET_6LO_DISPATCH_IPV6;
net_buf_add(buffer, 1);

net_pkt_frag_insert(pkt, frag);
net_pkt_frag_insert(pkt, buffer);

/* Compact the fragments, so that gaps will be filled */
net_pkt_compact(pkt);
Expand All @@ -1502,11 +1511,9 @@ static inline int compress_ipv6_header(struct net_pkt *pkt)

static inline bool uncompress_ipv6_header(struct net_pkt *pkt)
{
struct net_buf *frag = pkt->frags;

/* Pull off IPv6 dispatch header and adjust data and length */
memmove(frag->data, frag->data + 1, frag->len - 1U);
frag->len -= 1U;
net_buf_pull(pkt->buffer, 1U);
net_pkt_cursor_init(pkt);

return true;
}
Expand Down

0 comments on commit 144dc59

Please sign in to comment.