Skip to content

Commit

Permalink
net: buf: Add locking or sealing or typing mechanism
Browse files Browse the repository at this point in the history
Once a `type_id` has been set, all actions made through the API (R.I.P
code that casts buf->data) will fail.

This is meant to add typing information to net_buf's as they go through
the layers of networking stacks.

My understanding is that the actual net stack uses packets which are
typed better than net bufs. Maybe that's what we should do instead.

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
  • Loading branch information
jori-nordic committed Aug 1, 2024
1 parent e21a121 commit 2071914
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions include/zephyr/net/buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,9 @@ struct net_buf {
/** Size of user data on this buffer */
uint8_t user_data_size;

/** Now with more magic */
uintptr_t type_id;

/** Union for convenience access to the net_buf_simple members, also
* preserving the old API.
*/
Expand Down Expand Up @@ -1623,6 +1626,7 @@ static inline void net_buf_reserve(struct net_buf *buf, size_t reserve)
*/
static inline void *net_buf_add(struct net_buf *buf, size_t len)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_add(&buf->b, len);
}

Expand All @@ -1641,6 +1645,7 @@ static inline void *net_buf_add(struct net_buf *buf, size_t len)
static inline void *net_buf_add_mem(struct net_buf *buf, const void *mem,
size_t len)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_add_mem(&buf->b, mem, len);
}

Expand All @@ -1657,6 +1662,7 @@ static inline void *net_buf_add_mem(struct net_buf *buf, const void *mem,
*/
static inline uint8_t *net_buf_add_u8(struct net_buf *buf, uint8_t val)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_add_u8(&buf->b, val);
}

Expand Down Expand Up @@ -1852,6 +1858,7 @@ static inline void net_buf_add_be64(struct net_buf *buf, uint64_t val)
*/
static inline void *net_buf_remove_mem(struct net_buf *buf, size_t len)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_remove_mem(&buf->b, len);
}

Expand All @@ -1867,6 +1874,7 @@ static inline void *net_buf_remove_mem(struct net_buf *buf, size_t len)
*/
static inline uint8_t net_buf_remove_u8(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_remove_u8(&buf->b);
}

Expand All @@ -1882,6 +1890,7 @@ static inline uint8_t net_buf_remove_u8(struct net_buf *buf)
*/
static inline uint16_t net_buf_remove_le16(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_remove_le16(&buf->b);
}

Expand All @@ -1897,6 +1906,7 @@ static inline uint16_t net_buf_remove_le16(struct net_buf *buf)
*/
static inline uint16_t net_buf_remove_be16(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_remove_be16(&buf->b);
}

Expand All @@ -1912,6 +1922,7 @@ static inline uint16_t net_buf_remove_be16(struct net_buf *buf)
*/
static inline uint32_t net_buf_remove_be24(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_remove_be24(&buf->b);
}

Expand All @@ -1927,6 +1938,7 @@ static inline uint32_t net_buf_remove_be24(struct net_buf *buf)
*/
static inline uint32_t net_buf_remove_le24(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_remove_le24(&buf->b);
}

Expand All @@ -1942,6 +1954,7 @@ static inline uint32_t net_buf_remove_le24(struct net_buf *buf)
*/
static inline uint32_t net_buf_remove_le32(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_remove_le32(&buf->b);
}

Expand All @@ -1957,6 +1970,7 @@ static inline uint32_t net_buf_remove_le32(struct net_buf *buf)
*/
static inline uint32_t net_buf_remove_be32(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_remove_be32(&buf->b);
}

Expand All @@ -1972,6 +1986,7 @@ static inline uint32_t net_buf_remove_be32(struct net_buf *buf)
*/
static inline uint64_t net_buf_remove_le40(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_remove_le40(&buf->b);
}

Expand All @@ -1987,6 +2002,7 @@ static inline uint64_t net_buf_remove_le40(struct net_buf *buf)
*/
static inline uint64_t net_buf_remove_be40(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_remove_be40(&buf->b);
}

Expand All @@ -2002,6 +2018,7 @@ static inline uint64_t net_buf_remove_be40(struct net_buf *buf)
*/
static inline uint64_t net_buf_remove_le48(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_remove_le48(&buf->b);
}

Expand All @@ -2017,6 +2034,7 @@ static inline uint64_t net_buf_remove_le48(struct net_buf *buf)
*/
static inline uint64_t net_buf_remove_be48(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_remove_be48(&buf->b);
}

Expand All @@ -2032,6 +2050,7 @@ static inline uint64_t net_buf_remove_be48(struct net_buf *buf)
*/
static inline uint64_t net_buf_remove_le64(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_remove_le64(&buf->b);
}

Expand All @@ -2047,6 +2066,7 @@ static inline uint64_t net_buf_remove_le64(struct net_buf *buf)
*/
static inline uint64_t net_buf_remove_be64(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_remove_be64(&buf->b);
}

Expand All @@ -2063,6 +2083,7 @@ static inline uint64_t net_buf_remove_be64(struct net_buf *buf)
*/
static inline void *net_buf_push(struct net_buf *buf, size_t len)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_push(&buf->b, len);
}

Expand All @@ -2081,6 +2102,7 @@ static inline void *net_buf_push(struct net_buf *buf, size_t len)
static inline void *net_buf_push_mem(struct net_buf *buf, const void *mem,
size_t len)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_push_mem(&buf->b, mem, len);
}

Expand Down Expand Up @@ -2278,6 +2300,7 @@ static inline void net_buf_push_be64(struct net_buf *buf, uint64_t val)
*/
static inline void *net_buf_pull(struct net_buf *buf, size_t len)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_pull(&buf->b, len);
}

Expand All @@ -2294,6 +2317,7 @@ static inline void *net_buf_pull(struct net_buf *buf, size_t len)
*/
static inline void *net_buf_pull_mem(struct net_buf *buf, size_t len)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_pull_mem(&buf->b, len);
}

Expand All @@ -2309,6 +2333,7 @@ static inline void *net_buf_pull_mem(struct net_buf *buf, size_t len)
*/
static inline uint8_t net_buf_pull_u8(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_pull_u8(&buf->b);
}

Expand All @@ -2324,6 +2349,7 @@ static inline uint8_t net_buf_pull_u8(struct net_buf *buf)
*/
static inline uint16_t net_buf_pull_le16(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_pull_le16(&buf->b);
}

Expand All @@ -2339,6 +2365,7 @@ static inline uint16_t net_buf_pull_le16(struct net_buf *buf)
*/
static inline uint16_t net_buf_pull_be16(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_pull_be16(&buf->b);
}

Expand All @@ -2354,6 +2381,7 @@ static inline uint16_t net_buf_pull_be16(struct net_buf *buf)
*/
static inline uint32_t net_buf_pull_le24(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_pull_le24(&buf->b);
}

Expand All @@ -2369,6 +2397,7 @@ static inline uint32_t net_buf_pull_le24(struct net_buf *buf)
*/
static inline uint32_t net_buf_pull_be24(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_pull_be24(&buf->b);
}

Expand All @@ -2384,6 +2413,7 @@ static inline uint32_t net_buf_pull_be24(struct net_buf *buf)
*/
static inline uint32_t net_buf_pull_le32(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_pull_le32(&buf->b);
}

Expand All @@ -2399,6 +2429,7 @@ static inline uint32_t net_buf_pull_le32(struct net_buf *buf)
*/
static inline uint32_t net_buf_pull_be32(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_pull_be32(&buf->b);
}

Expand All @@ -2414,6 +2445,7 @@ static inline uint32_t net_buf_pull_be32(struct net_buf *buf)
*/
static inline uint64_t net_buf_pull_le40(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_pull_le40(&buf->b);
}

Expand All @@ -2429,6 +2461,7 @@ static inline uint64_t net_buf_pull_le40(struct net_buf *buf)
*/
static inline uint64_t net_buf_pull_be40(struct net_buf *buf)
{
__ASSERT_NO_MSG(buf->type_id == 0);
return net_buf_simple_pull_be40(&buf->b);
}

Expand Down
1 change: 1 addition & 0 deletions subsys/net/buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ struct net_buf *net_buf_alloc_len(struct net_buf_pool *pool, size_t size,
buf->flags = 0U;
buf->frags = NULL;
buf->size = size;
buf->type_id = 0;
net_buf_reset(buf);

#if defined(CONFIG_NET_BUF_POOL_USAGE)
Expand Down

0 comments on commit 2071914

Please sign in to comment.