From 675277e28d01273c852a7731adf4620b89a5fa2e Mon Sep 17 00:00:00 2001 From: Tomasz Bursztyka Date: Fri, 16 Dec 2016 18:38:06 +0100 Subject: [PATCH] net: nbuf: Initialize nbuf memory area after allocation After commit 71c7c018192b995b9f03bf9c14b164d78909420b about buf pool, we started to get spurious behaviors on various places of the code on different boards (a101, frdm...) BUT on qemu. Basically, outgoing ip/udp packets were full of garbage. Or sometimes it was the abilitty to parse incoming packet that was happening. The difference between qemu and actualy boards is - afaik, at least, let me know if I am wrong - that qemu provide initialized memory (full of 0s). Following this asssumption, this patch just reset the nbuf right after it got allocated. And all started to work again as thought. It's in fact a good thing to reset nbuf memory. Even before the above commit: after being used more than once, a buffer would have ended up with old content, and this could have been generating a bug. So let's be on the safe side and always intialize nbuf content. Change-Id: I50647d9e9b82a4ed340a5ceb0d69409b0194dddd Signed-off-by: Tomasz Bursztyka --- subsys/net/ip/nbuf.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/subsys/net/ip/nbuf.c b/subsys/net/ip/nbuf.c index c463322ddb8..0fccb670fda 100644 --- a/subsys/net/ip/nbuf.c +++ b/subsys/net/ip/nbuf.c @@ -325,6 +325,8 @@ static struct net_buf *net_nbuf_get_reserve(enum net_nbuf_type type, case NET_NBUF_RX: buf = net_buf_alloc(&rx_buffers, K_FOREVER); + memset(net_buf_user_data(buf), 0, sizeof(struct net_nbuf)); + NET_ASSERT_INFO(buf->ref, "RX buf %p ref %d", buf, buf->ref); dec_free_rx_bufs(buf); @@ -333,6 +335,8 @@ static struct net_buf *net_nbuf_get_reserve(enum net_nbuf_type type, case NET_NBUF_TX: buf = net_buf_alloc(&tx_buffers, K_FOREVER); + memset(net_buf_user_data(buf), 0, sizeof(struct net_nbuf)); + NET_ASSERT_INFO(buf->ref, "TX buf %p ref %d", buf, buf->ref); dec_free_tx_bufs(buf); @@ -357,17 +361,6 @@ static struct net_buf *net_nbuf_get_reserve(enum net_nbuf_type type, NET_BUF_CHECK_IF_NOT_IN_USE(buf, buf->ref + 1); - if (type != NET_NBUF_DATA) { - net_nbuf_set_context(buf, NULL); - net_nbuf_ll_dst(buf)->addr = NULL; - net_nbuf_ll_src(buf)->addr = NULL; - - /* Let's make sure ll_reserve is not set - * from a previous usage of the buffer. - */ - net_nbuf_set_ll_reserve(buf, 0); - } - NET_DBG("%s [%d] buf %p reserve %u ref %d (%s():%d)", type2str(type), get_frees(type), buf, reserve_head, buf->ref, caller, line);