Zephyr API Documentation  3.6.0
A Scalable Open Source RTOS
3.6.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
buf.h
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2015 Intel Corporation
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10#ifndef ZEPHYR_INCLUDE_NET_BUF_H_
11#define ZEPHYR_INCLUDE_NET_BUF_H_
12
13#include <stddef.h>
14#include <zephyr/types.h>
15#include <zephyr/sys/util.h>
16#include <zephyr/kernel.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
30/* Alignment needed for various parts of the buffer definition */
31#if CONFIG_NET_BUF_ALIGNMENT == 0
32#define __net_buf_align __aligned(sizeof(void *))
33#else
34#define __net_buf_align __aligned(CONFIG_NET_BUF_ALIGNMENT)
35#endif
36
46#define NET_BUF_SIMPLE_DEFINE(_name, _size) \
47 uint8_t net_buf_data_##_name[_size]; \
48 struct net_buf_simple _name = { \
49 .data = net_buf_data_##_name, \
50 .len = 0, \
51 .size = _size, \
52 .__buf = net_buf_data_##_name, \
53 }
54
65#define NET_BUF_SIMPLE_DEFINE_STATIC(_name, _size) \
66 static __noinit uint8_t net_buf_data_##_name[_size]; \
67 static struct net_buf_simple _name = { \
68 .data = net_buf_data_##_name, \
69 .len = 0, \
70 .size = _size, \
71 .__buf = net_buf_data_##_name, \
72 }
73
90
97
100
104 uint8_t *__buf;
105};
106
123#define NET_BUF_SIMPLE(_size) \
124 ((struct net_buf_simple *)(&(struct { \
125 struct net_buf_simple buf; \
126 uint8_t data[_size]; \
127 }) { \
128 .buf.size = _size, \
129 }))
130
140static inline void net_buf_simple_init(struct net_buf_simple *buf,
141 size_t reserve_head)
142{
143 if (!buf->__buf) {
144 buf->__buf = (uint8_t *)buf + sizeof(*buf);
145 }
146
147 buf->data = buf->__buf + reserve_head;
148 buf->len = 0U;
149}
150
161 void *data, size_t size);
162
170static inline void net_buf_simple_reset(struct net_buf_simple *buf)
171{
172 buf->len = 0U;
173 buf->data = buf->__buf;
174}
175
186void net_buf_simple_clone(const struct net_buf_simple *original,
187 struct net_buf_simple *clone);
188
200void *net_buf_simple_add(struct net_buf_simple *buf, size_t len);
201
214void *net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem,
215 size_t len);
216
229
241
253
265
277
289
301
313
325
337
349
360void *net_buf_simple_remove_mem(struct net_buf_simple *buf, size_t len);
361
373
385
397
409
421
433
445
457
469
481
493
505void *net_buf_simple_push(struct net_buf_simple *buf, size_t len);
506
519void *net_buf_simple_push_mem(struct net_buf_simple *buf, const void *mem,
520 size_t len);
521
532
543
553
564
575
586
597
608
619
630
641
653void *net_buf_simple_pull(struct net_buf_simple *buf, size_t len);
654
666void *net_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len);
667
679
691
703
715
727
739
751
763
775
787
799
809static inline uint8_t *net_buf_simple_tail(struct net_buf_simple *buf)
810{
811 return buf->data + buf->len;
812}
813
824
835
846
859};
860
869static inline void net_buf_simple_save(struct net_buf_simple *buf,
871{
872 state->offset = net_buf_simple_headroom(buf);
873 state->len = buf->len;
874}
875
885static inline void net_buf_simple_restore(struct net_buf_simple *buf,
887{
888 buf->data = buf->__buf + state->offset;
889 buf->len = state->len;
890}
891
901#define NET_BUF_EXTERNAL_DATA BIT(0)
902
910struct net_buf {
913
915 struct net_buf *frags;
916
919
922
925
926 /* Size of user data on this buffer */
928
929 /* Union for convenience access to the net_buf_simple members, also
930 * preserving the old API.
931 */
932 union {
933 /* The ABI of this struct must match net_buf_simple */
934 struct {
937
940
943
948 uint8_t *__buf;
949 };
950
952 };
953
955 uint8_t user_data[] __net_buf_align;
956};
957
959 uint8_t * __must_check (*alloc)(struct net_buf *buf, size_t *size,
960 k_timeout_t timeout);
961 uint8_t * __must_check (*ref)(struct net_buf *buf, uint8_t *data);
962 void (*unref)(struct net_buf *buf, uint8_t *data);
963};
964
966 const struct net_buf_data_cb *cb;
969};
970
978 struct k_lifo free;
979
980 /* to prevent concurrent access/modifications */
982
985
988
989 /* Size of user data allocated to this pool */
991
992#if defined(CONFIG_NET_BUF_POOL_USAGE)
994 atomic_t avail_count;
995
997 const uint16_t pool_size;
998
1000 const char *name;
1001#endif /* CONFIG_NET_BUF_POOL_USAGE */
1002
1004 void (*const destroy)(struct net_buf *buf);
1005
1008
1010 struct net_buf * const __bufs;
1011};
1012
1014#define NET_BUF_POOL_USAGE_INIT(_pool, _count) \
1015 IF_ENABLED(CONFIG_NET_BUF_POOL_USAGE, (.avail_count = ATOMIC_INIT(_count),)) \
1016 IF_ENABLED(CONFIG_NET_BUF_POOL_USAGE, (.name = STRINGIFY(_pool),))
1017
1018#define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _ud_size, _destroy) \
1019 { \
1020 .free = Z_LIFO_INITIALIZER(_pool.free), \
1021 .lock = { }, \
1022 .buf_count = _count, \
1023 .uninit_count = _count, \
1024 .user_data_size = _ud_size, \
1025 NET_BUF_POOL_USAGE_INIT(_pool, _count) \
1026 .destroy = _destroy, \
1027 .alloc = _alloc, \
1028 .__bufs = (struct net_buf *)_bufs, \
1029 }
1030
1031#define _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size) \
1032 struct _net_buf_##_name { uint8_t b[sizeof(struct net_buf)]; \
1033 uint8_t ud[_ud_size]; } __net_buf_align; \
1034 BUILD_ASSERT(_ud_size <= UINT8_MAX); \
1035 BUILD_ASSERT(offsetof(struct net_buf, user_data) == \
1036 offsetof(struct _net_buf_##_name, ud), "Invalid offset"); \
1037 BUILD_ASSERT(__alignof__(struct net_buf) == \
1038 __alignof__(struct _net_buf_##_name), "Invalid alignment"); \
1039 BUILD_ASSERT(sizeof(struct _net_buf_##_name) == \
1040 ROUND_UP(sizeof(struct net_buf) + _ud_size, __alignof__(struct net_buf)), \
1041 "Size cannot be determined"); \
1042 static struct _net_buf_##_name _net_buf_##_name[_count] __noinit
1043
1044extern const struct net_buf_data_alloc net_buf_heap_alloc;
1074#define NET_BUF_POOL_HEAP_DEFINE(_name, _count, _ud_size, _destroy) \
1075 _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size); \
1076 static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) = \
1077 NET_BUF_POOL_INITIALIZER(_name, &net_buf_heap_alloc, \
1078 _net_buf_##_name, _count, _ud_size, \
1079 _destroy)
1080
1083};
1084
1086extern const struct net_buf_data_cb net_buf_fixed_cb;
1117#define NET_BUF_POOL_FIXED_DEFINE(_name, _count, _data_size, _ud_size, _destroy) \
1118 _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size); \
1119 static uint8_t __noinit net_buf_data_##_name[_count][_data_size] __net_buf_align; \
1120 static const struct net_buf_pool_fixed net_buf_fixed_##_name = { \
1121 .data_pool = (uint8_t *)net_buf_data_##_name, \
1122 }; \
1123 static const struct net_buf_data_alloc net_buf_fixed_alloc_##_name = { \
1124 .cb = &net_buf_fixed_cb, \
1125 .alloc_data = (void *)&net_buf_fixed_##_name, \
1126 .max_alloc_size = _data_size, \
1127 }; \
1128 static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) = \
1129 NET_BUF_POOL_INITIALIZER(_name, &net_buf_fixed_alloc_##_name, \
1130 _net_buf_##_name, _count, _ud_size, \
1131 _destroy)
1132
1134extern const struct net_buf_data_cb net_buf_var_cb;
1161#define NET_BUF_POOL_VAR_DEFINE(_name, _count, _data_size, _ud_size, _destroy) \
1162 _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size); \
1163 K_HEAP_DEFINE(net_buf_mem_pool_##_name, _data_size); \
1164 static const struct net_buf_data_alloc net_buf_data_alloc_##_name = { \
1165 .cb = &net_buf_var_cb, \
1166 .alloc_data = &net_buf_mem_pool_##_name, \
1167 .max_alloc_size = 0, \
1168 }; \
1169 static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) = \
1170 NET_BUF_POOL_INITIALIZER(_name, &net_buf_data_alloc_##_name, \
1171 _net_buf_##_name, _count, _ud_size, \
1172 _destroy)
1173
1195#define NET_BUF_POOL_DEFINE(_name, _count, _size, _ud_size, _destroy) \
1196 NET_BUF_POOL_FIXED_DEFINE(_name, _count, _size, _ud_size, _destroy)
1197
1206
1219int net_buf_id(struct net_buf *buf);
1220
1235#if defined(CONFIG_NET_BUF_LOG)
1236struct net_buf * __must_check net_buf_alloc_fixed_debug(struct net_buf_pool *pool,
1237 k_timeout_t timeout,
1238 const char *func,
1239 int line);
1240#define net_buf_alloc_fixed(_pool, _timeout) \
1241 net_buf_alloc_fixed_debug(_pool, _timeout, __func__, __LINE__)
1242#else
1243struct net_buf * __must_check net_buf_alloc_fixed(struct net_buf_pool *pool,
1244 k_timeout_t timeout);
1245#endif
1246
1250static inline struct net_buf * __must_check net_buf_alloc(struct net_buf_pool *pool,
1251 k_timeout_t timeout)
1252{
1253 return net_buf_alloc_fixed(pool, timeout);
1254}
1255
1271#if defined(CONFIG_NET_BUF_LOG)
1272struct net_buf * __must_check net_buf_alloc_len_debug(struct net_buf_pool *pool,
1273 size_t size,
1274 k_timeout_t timeout,
1275 const char *func,
1276 int line);
1277#define net_buf_alloc_len(_pool, _size, _timeout) \
1278 net_buf_alloc_len_debug(_pool, _size, _timeout, __func__, __LINE__)
1279#else
1280struct net_buf * __must_check net_buf_alloc_len(struct net_buf_pool *pool,
1281 size_t size,
1282 k_timeout_t timeout);
1283#endif
1284
1304#if defined(CONFIG_NET_BUF_LOG)
1305struct net_buf * __must_check net_buf_alloc_with_data_debug(struct net_buf_pool *pool,
1306 void *data, size_t size,
1307 k_timeout_t timeout,
1308 const char *func, int line);
1309#define net_buf_alloc_with_data(_pool, _data_, _size, _timeout) \
1310 net_buf_alloc_with_data_debug(_pool, _data_, _size, _timeout, \
1311 __func__, __LINE__)
1312#else
1313struct net_buf * __must_check net_buf_alloc_with_data(struct net_buf_pool *pool,
1314 void *data, size_t size,
1315 k_timeout_t timeout);
1316#endif
1317
1331#if defined(CONFIG_NET_BUF_LOG)
1332struct net_buf * __must_check net_buf_get_debug(struct k_fifo *fifo,
1333 k_timeout_t timeout,
1334 const char *func, int line);
1335#define net_buf_get(_fifo, _timeout) \
1336 net_buf_get_debug(_fifo, _timeout, __func__, __LINE__)
1337#else
1338struct net_buf * __must_check net_buf_get(struct k_fifo *fifo,
1339 k_timeout_t timeout);
1340#endif
1341
1351static inline void net_buf_destroy(struct net_buf *buf)
1352{
1353 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
1354
1355 if (buf->__buf) {
1356 if (!(buf->flags & NET_BUF_EXTERNAL_DATA)) {
1357 pool->alloc->cb->unref(buf, buf->__buf);
1358 }
1359 buf->__buf = NULL;
1360 }
1361
1362 k_lifo_put(&pool->free, buf);
1363}
1364
1372void net_buf_reset(struct net_buf *buf);
1373
1382void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve);
1383
1393void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf);
1394
1405struct net_buf * __must_check net_buf_slist_get(sys_slist_t *list);
1406
1416void net_buf_put(struct k_fifo *fifo, struct net_buf *buf);
1417
1425#if defined(CONFIG_NET_BUF_LOG)
1426void net_buf_unref_debug(struct net_buf *buf, const char *func, int line);
1427#define net_buf_unref(_buf) \
1428 net_buf_unref_debug(_buf, __func__, __LINE__)
1429#else
1430void net_buf_unref(struct net_buf *buf);
1431#endif
1432
1440struct net_buf * __must_check net_buf_ref(struct net_buf *buf);
1441
1455struct net_buf * __must_check net_buf_clone(struct net_buf *buf,
1456 k_timeout_t timeout);
1457
1465static inline void * __must_check net_buf_user_data(const struct net_buf *buf)
1466{
1467 return (void *)buf->user_data;
1468}
1469
1478static inline void net_buf_reserve(struct net_buf *buf, size_t reserve)
1479{
1480 net_buf_simple_reserve(&buf->b, reserve);
1481}
1482
1494static inline void *net_buf_add(struct net_buf *buf, size_t len)
1495{
1496 return net_buf_simple_add(&buf->b, len);
1497}
1498
1511static inline void *net_buf_add_mem(struct net_buf *buf, const void *mem,
1512 size_t len)
1513{
1514 return net_buf_simple_add_mem(&buf->b, mem, len);
1515}
1516
1528static inline uint8_t *net_buf_add_u8(struct net_buf *buf, uint8_t val)
1529{
1530 return net_buf_simple_add_u8(&buf->b, val);
1531}
1532
1543static inline void net_buf_add_le16(struct net_buf *buf, uint16_t val)
1544{
1545 net_buf_simple_add_le16(&buf->b, val);
1546}
1547
1558static inline void net_buf_add_be16(struct net_buf *buf, uint16_t val)
1559{
1560 net_buf_simple_add_be16(&buf->b, val);
1561}
1562
1573static inline void net_buf_add_le24(struct net_buf *buf, uint32_t val)
1574{
1575 net_buf_simple_add_le24(&buf->b, val);
1576}
1577
1588static inline void net_buf_add_be24(struct net_buf *buf, uint32_t val)
1589{
1590 net_buf_simple_add_be24(&buf->b, val);
1591}
1592
1603static inline void net_buf_add_le32(struct net_buf *buf, uint32_t val)
1604{
1605 net_buf_simple_add_le32(&buf->b, val);
1606}
1607
1618static inline void net_buf_add_be32(struct net_buf *buf, uint32_t val)
1619{
1620 net_buf_simple_add_be32(&buf->b, val);
1621}
1622
1633static inline void net_buf_add_le48(struct net_buf *buf, uint64_t val)
1634{
1635 net_buf_simple_add_le48(&buf->b, val);
1636}
1637
1648static inline void net_buf_add_be48(struct net_buf *buf, uint64_t val)
1649{
1650 net_buf_simple_add_be48(&buf->b, val);
1651}
1652
1663static inline void net_buf_add_le64(struct net_buf *buf, uint64_t val)
1664{
1665 net_buf_simple_add_le64(&buf->b, val);
1666}
1667
1678static inline void net_buf_add_be64(struct net_buf *buf, uint64_t val)
1679{
1680 net_buf_simple_add_be64(&buf->b, val);
1681}
1682
1693static inline void *net_buf_remove_mem(struct net_buf *buf, size_t len)
1694{
1695 return net_buf_simple_remove_mem(&buf->b, len);
1696}
1697
1708static inline uint8_t net_buf_remove_u8(struct net_buf *buf)
1709{
1710 return net_buf_simple_remove_u8(&buf->b);
1711}
1712
1723static inline uint16_t net_buf_remove_le16(struct net_buf *buf)
1724{
1725 return net_buf_simple_remove_le16(&buf->b);
1726}
1727
1738static inline uint16_t net_buf_remove_be16(struct net_buf *buf)
1739{
1740 return net_buf_simple_remove_be16(&buf->b);
1741}
1742
1753static inline uint32_t net_buf_remove_be24(struct net_buf *buf)
1754{
1755 return net_buf_simple_remove_be24(&buf->b);
1756}
1757
1768static inline uint32_t net_buf_remove_le24(struct net_buf *buf)
1769{
1770 return net_buf_simple_remove_le24(&buf->b);
1771}
1772
1783static inline uint32_t net_buf_remove_le32(struct net_buf *buf)
1784{
1785 return net_buf_simple_remove_le32(&buf->b);
1786}
1787
1798static inline uint32_t net_buf_remove_be32(struct net_buf *buf)
1799{
1800 return net_buf_simple_remove_be32(&buf->b);
1801}
1802
1813static inline uint64_t net_buf_remove_le48(struct net_buf *buf)
1814{
1815 return net_buf_simple_remove_le48(&buf->b);
1816}
1817
1828static inline uint64_t net_buf_remove_be48(struct net_buf *buf)
1829{
1830 return net_buf_simple_remove_be48(&buf->b);
1831}
1832
1843static inline uint64_t net_buf_remove_le64(struct net_buf *buf)
1844{
1845 return net_buf_simple_remove_le64(&buf->b);
1846}
1847
1858static inline uint64_t net_buf_remove_be64(struct net_buf *buf)
1859{
1860 return net_buf_simple_remove_be64(&buf->b);
1861}
1862
1874static inline void *net_buf_push(struct net_buf *buf, size_t len)
1875{
1876 return net_buf_simple_push(&buf->b, len);
1877}
1878
1891static inline void *net_buf_push_mem(struct net_buf *buf, const void *mem,
1892 size_t len)
1893{
1894 return net_buf_simple_push_mem(&buf->b, mem, len);
1895}
1896
1905static inline void net_buf_push_u8(struct net_buf *buf, uint8_t val)
1906{
1907 net_buf_simple_push_u8(&buf->b, val);
1908}
1909
1919static inline void net_buf_push_le16(struct net_buf *buf, uint16_t val)
1920{
1921 net_buf_simple_push_le16(&buf->b, val);
1922}
1923
1933static inline void net_buf_push_be16(struct net_buf *buf, uint16_t val)
1934{
1935 net_buf_simple_push_be16(&buf->b, val);
1936}
1937
1947static inline void net_buf_push_le24(struct net_buf *buf, uint32_t val)
1948{
1949 net_buf_simple_push_le24(&buf->b, val);
1950}
1951
1961static inline void net_buf_push_be24(struct net_buf *buf, uint32_t val)
1962{
1963 net_buf_simple_push_be24(&buf->b, val);
1964}
1965
1975static inline void net_buf_push_le32(struct net_buf *buf, uint32_t val)
1976{
1977 net_buf_simple_push_le32(&buf->b, val);
1978}
1979
1989static inline void net_buf_push_be32(struct net_buf *buf, uint32_t val)
1990{
1991 net_buf_simple_push_be32(&buf->b, val);
1992}
1993
2003static inline void net_buf_push_le48(struct net_buf *buf, uint64_t val)
2004{
2005 net_buf_simple_push_le48(&buf->b, val);
2006}
2007
2017static inline void net_buf_push_be48(struct net_buf *buf, uint64_t val)
2018{
2019 net_buf_simple_push_be48(&buf->b, val);
2020}
2021
2031static inline void net_buf_push_le64(struct net_buf *buf, uint64_t val)
2032{
2033 net_buf_simple_push_le64(&buf->b, val);
2034}
2035
2045static inline void net_buf_push_be64(struct net_buf *buf, uint64_t val)
2046{
2047 net_buf_simple_push_be64(&buf->b, val);
2048}
2049
2061static inline void *net_buf_pull(struct net_buf *buf, size_t len)
2062{
2063 return net_buf_simple_pull(&buf->b, len);
2064}
2065
2077static inline void *net_buf_pull_mem(struct net_buf *buf, size_t len)
2078{
2079 return net_buf_simple_pull_mem(&buf->b, len);
2080}
2081
2092static inline uint8_t net_buf_pull_u8(struct net_buf *buf)
2093{
2094 return net_buf_simple_pull_u8(&buf->b);
2095}
2096
2107static inline uint16_t net_buf_pull_le16(struct net_buf *buf)
2108{
2109 return net_buf_simple_pull_le16(&buf->b);
2110}
2111
2122static inline uint16_t net_buf_pull_be16(struct net_buf *buf)
2123{
2124 return net_buf_simple_pull_be16(&buf->b);
2125}
2126
2137static inline uint32_t net_buf_pull_le24(struct net_buf *buf)
2138{
2139 return net_buf_simple_pull_le24(&buf->b);
2140}
2141
2152static inline uint32_t net_buf_pull_be24(struct net_buf *buf)
2153{
2154 return net_buf_simple_pull_be24(&buf->b);
2155}
2156
2167static inline uint32_t net_buf_pull_le32(struct net_buf *buf)
2168{
2169 return net_buf_simple_pull_le32(&buf->b);
2170}
2171
2182static inline uint32_t net_buf_pull_be32(struct net_buf *buf)
2183{
2184 return net_buf_simple_pull_be32(&buf->b);
2185}
2186
2197static inline uint64_t net_buf_pull_le48(struct net_buf *buf)
2198{
2199 return net_buf_simple_pull_le48(&buf->b);
2200}
2201
2212static inline uint64_t net_buf_pull_be48(struct net_buf *buf)
2213{
2214 return net_buf_simple_pull_be48(&buf->b);
2215}
2216
2227static inline uint64_t net_buf_pull_le64(struct net_buf *buf)
2228{
2229 return net_buf_simple_pull_le64(&buf->b);
2230}
2231
2242static inline uint64_t net_buf_pull_be64(struct net_buf *buf)
2243{
2244 return net_buf_simple_pull_be64(&buf->b);
2245}
2246
2256static inline size_t net_buf_tailroom(struct net_buf *buf)
2257{
2258 return net_buf_simple_tailroom(&buf->b);
2259}
2260
2270static inline size_t net_buf_headroom(struct net_buf *buf)
2271{
2272 return net_buf_simple_headroom(&buf->b);
2273}
2274
2284static inline uint16_t net_buf_max_len(struct net_buf *buf)
2285{
2286 return net_buf_simple_max_len(&buf->b);
2287}
2288
2298static inline uint8_t *net_buf_tail(struct net_buf *buf)
2299{
2300 return net_buf_simple_tail(&buf->b);
2301}
2302
2309
2321void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag);
2322
2337struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag);
2338
2348#if defined(CONFIG_NET_BUF_LOG)
2349struct net_buf *net_buf_frag_del_debug(struct net_buf *parent,
2350 struct net_buf *frag,
2351 const char *func, int line);
2352#define net_buf_frag_del(_parent, _frag) \
2353 net_buf_frag_del_debug(_parent, _frag, __func__, __LINE__)
2354#else
2355struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag);
2356#endif
2357
2373size_t net_buf_linearize(void *dst, size_t dst_len,
2374 struct net_buf *src, size_t offset, size_t len);
2375
2390typedef struct net_buf * __must_check (*net_buf_allocator_cb)(k_timeout_t timeout,
2391 void *user_data);
2392
2414size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
2415 const void *value, k_timeout_t timeout,
2416 net_buf_allocator_cb allocate_cb, void *user_data);
2417
2432size_t net_buf_data_match(const struct net_buf *buf, size_t offset, const void *data, size_t len);
2433
2449static inline struct net_buf *net_buf_skip(struct net_buf *buf, size_t len)
2450{
2451 while (buf && len--) {
2452 net_buf_pull_u8(buf);
2453 if (!buf->len) {
2454 buf = net_buf_frag_del(NULL, buf);
2455 }
2456 }
2457
2458 return buf;
2459}
2460
2471static inline size_t net_buf_frags_len(struct net_buf *buf)
2472{
2473 size_t bytes = 0;
2474
2475 while (buf) {
2476 bytes += buf->len;
2477 buf = buf->frags;
2478 }
2479
2480 return bytes;
2481}
2482
2487#ifdef __cplusplus
2488}
2489#endif
2490
2491#endif /* ZEPHYR_INCLUDE_NET_BUF_H_ */
long atomic_t
Definition: atomic_types.h:15
#define k_lifo_put(lifo, data)
Add an element to a LIFO queue.
Definition: kernel.h:2667
struct net_buf * net_buf_get(struct k_fifo *fifo, k_timeout_t timeout)
Get a buffer from a FIFO.
void net_buf_simple_clone(const struct net_buf_simple *original, struct net_buf_simple *clone)
Clone buffer state, using the same data buffer.
static void net_buf_simple_init(struct net_buf_simple *buf, size_t reserve_head)
Initialize a net_buf_simple object.
Definition: buf.h:140
struct net_buf * net_buf_frag_last(struct net_buf *frags)
Find the last fragment in the fragment list.
static void net_buf_add_be64(struct net_buf *buf, uint64_t val)
Add 64-bit value at the end of the buffer.
Definition: buf.h:1678
static size_t net_buf_headroom(struct net_buf *buf)
Check buffer headroom.
Definition: buf.h:2270
uint8_t net_buf_simple_pull_u8(struct net_buf_simple *buf)
Remove a 8-bit value from the beginning of the buffer.
uint16_t net_buf_simple_remove_le16(struct net_buf_simple *buf)
Remove and convert 16 bits from the end of the buffer.
static uint64_t net_buf_remove_le48(struct net_buf *buf)
Remove and convert 48 bits from the end of the buffer.
Definition: buf.h:1813
struct net_buf * net_buf_frag_add(struct net_buf *head, struct net_buf *frag)
Add a new fragment to the end of a chain of bufs.
void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve)
Initialize buffer with the given headroom.
void net_buf_simple_push_u8(struct net_buf_simple *buf, uint8_t val)
Push 8-bit value to the beginning of the buffer.
static void net_buf_add_be24(struct net_buf *buf, uint32_t val)
Add 24-bit value at the end of the buffer.
Definition: buf.h:1588
struct net_buf * net_buf_alloc_len(struct net_buf_pool *pool, size_t size, k_timeout_t timeout)
Allocate a new variable length buffer from a pool.
void net_buf_reset(struct net_buf *buf)
Reset buffer.
struct net_buf_pool * net_buf_pool_get(int id)
Looks up a pool based on its ID.
void * net_buf_simple_add(struct net_buf_simple *buf, size_t len)
Prepare data to be added at the end of the buffer.
uint64_t net_buf_simple_pull_be48(struct net_buf_simple *buf)
Remove and convert 48 bits from the beginning of the buffer.
uint32_t net_buf_simple_pull_be32(struct net_buf_simple *buf)
Remove and convert 32 bits from the beginning of the buffer.
void net_buf_simple_push_be48(struct net_buf_simple *buf, uint64_t val)
Push 48-bit value to the beginning of the buffer.
struct net_buf * net_buf_slist_get(sys_slist_t *list)
Get a buffer from a list.
struct net_buf * net_buf_ref(struct net_buf *buf)
Increment the reference count of a buffer.
struct net_buf *(* net_buf_allocator_cb)(k_timeout_t timeout, void *user_data)
Network buffer allocator callback.
Definition: buf.h:2390
static struct net_buf * net_buf_skip(struct net_buf *buf, size_t len)
Skip N number of bytes in a net_buf.
Definition: buf.h:2449
static void * net_buf_add(struct net_buf *buf, size_t len)
Prepare data to be added at the end of the buffer.
Definition: buf.h:1494
static void net_buf_add_le24(struct net_buf *buf, uint32_t val)
Add 24-bit value at the end of the buffer.
Definition: buf.h:1573
uint32_t net_buf_simple_pull_le32(struct net_buf_simple *buf)
Remove and convert 32 bits from the beginning of the buffer.
void net_buf_simple_add_le32(struct net_buf_simple *buf, uint32_t val)
Add 32-bit value at the end of the buffer.
static uint64_t net_buf_pull_be64(struct net_buf *buf)
Remove and convert 64 bits from the beginning of the buffer.
Definition: buf.h:2242
static void net_buf_push_be64(struct net_buf *buf, uint64_t val)
Push 64-bit value to the beginning of the buffer.
Definition: buf.h:2045
static uint64_t net_buf_remove_le64(struct net_buf *buf)
Remove and convert 64 bits from the end of the buffer.
Definition: buf.h:1843
static void net_buf_simple_reset(struct net_buf_simple *buf)
Reset buffer.
Definition: buf.h:170
uint32_t net_buf_simple_pull_be24(struct net_buf_simple *buf)
Remove and convert 24 bits from the beginning of the buffer.
uint32_t net_buf_simple_pull_le24(struct net_buf_simple *buf)
Remove and convert 24 bits from the beginning of the buffer.
uint32_t net_buf_simple_remove_le24(struct net_buf_simple *buf)
Remove and convert 24 bits from the end of the buffer.
void net_buf_simple_push_le16(struct net_buf_simple *buf, uint16_t val)
Push 16-bit value to the beginning of the buffer.
static struct net_buf * net_buf_alloc(struct net_buf_pool *pool, k_timeout_t timeout)
Definition: buf.h:1250
static void net_buf_add_be32(struct net_buf *buf, uint32_t val)
Add 32-bit value at the end of the buffer.
Definition: buf.h:1618
uint64_t net_buf_simple_remove_le64(struct net_buf_simple *buf)
Remove and convert 64 bits from the end of the buffer.
size_t net_buf_simple_tailroom(struct net_buf_simple *buf)
Check buffer tailroom.
static void net_buf_simple_save(struct net_buf_simple *buf, struct net_buf_simple_state *state)
Save the parsing state of a buffer.
Definition: buf.h:869
void net_buf_simple_add_le48(struct net_buf_simple *buf, uint64_t val)
Add 48-bit value at the end of the buffer.
static uint16_t net_buf_remove_be16(struct net_buf *buf)
Remove and convert 16 bits from the end of the buffer.
Definition: buf.h:1738
void net_buf_simple_add_be24(struct net_buf_simple *buf, uint32_t val)
Add 24-bit value at the end of the buffer.
static uint32_t net_buf_pull_le32(struct net_buf *buf)
Remove and convert 32 bits from the beginning of the buffer.
Definition: buf.h:2167
static uint32_t net_buf_pull_be32(struct net_buf *buf)
Remove and convert 32 bits from the beginning of the buffer.
Definition: buf.h:2182
struct net_buf * net_buf_frag_del(struct net_buf *parent, struct net_buf *frag)
Delete existing fragment from a chain of bufs.
uint64_t net_buf_simple_remove_be64(struct net_buf_simple *buf)
Remove and convert 64 bits from the end of the buffer.
static size_t net_buf_tailroom(struct net_buf *buf)
Check buffer tailroom.
Definition: buf.h:2256
static void net_buf_add_be16(struct net_buf *buf, uint16_t val)
Add 16-bit value at the end of the buffer.
Definition: buf.h:1558
size_t net_buf_append_bytes(struct net_buf *buf, size_t len, const void *value, k_timeout_t timeout, net_buf_allocator_cb allocate_cb, void *user_data)
Append data to a list of net_buf.
void * net_buf_simple_push(struct net_buf_simple *buf, size_t len)
Prepare data to be added to the start of the buffer.
void net_buf_simple_push_le48(struct net_buf_simple *buf, uint64_t val)
Push 48-bit value to the beginning of the buffer.
struct net_buf * net_buf_alloc_fixed(struct net_buf_pool *pool, k_timeout_t timeout)
Allocate a new fixed buffer from a pool.
static uint64_t net_buf_pull_be48(struct net_buf *buf)
Remove and convert 48 bits from the beginning of the buffer.
Definition: buf.h:2212
uint64_t net_buf_simple_pull_le48(struct net_buf_simple *buf)
Remove and convert 48 bits from the beginning of the buffer.
void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf)
Put a buffer into a list.
static void net_buf_push_be16(struct net_buf *buf, uint16_t val)
Push 16-bit value to the beginning of the buffer.
Definition: buf.h:1933
static uint32_t net_buf_remove_be24(struct net_buf *buf)
Remove and convert 24 bits from the end of the buffer.
Definition: buf.h:1753
static uint8_t net_buf_pull_u8(struct net_buf *buf)
Remove a 8-bit value from the beginning of the buffer.
Definition: buf.h:2092
static void net_buf_destroy(struct net_buf *buf)
Destroy buffer from custom destroy callback.
Definition: buf.h:1351
void net_buf_simple_push_le64(struct net_buf_simple *buf, uint64_t val)
Push 64-bit value to the beginning of the buffer.
void net_buf_simple_add_le64(struct net_buf_simple *buf, uint64_t val)
Add 64-bit value at the end of the buffer.
uint16_t net_buf_simple_max_len(struct net_buf_simple *buf)
Check maximum net_buf_simple::len value.
uint64_t net_buf_simple_pull_le64(struct net_buf_simple *buf)
Remove and convert 64 bits from the beginning of the buffer.
void net_buf_put(struct k_fifo *fifo, struct net_buf *buf)
Put a buffer to the end of a FIFO.
static void * net_buf_push_mem(struct net_buf *buf, const void *mem, size_t len)
Copies the given number of bytes to the start of the buffer.
Definition: buf.h:1891
static uint64_t net_buf_pull_le48(struct net_buf *buf)
Remove and convert 48 bits from the beginning of the buffer.
Definition: buf.h:2197
void net_buf_simple_init_with_data(struct net_buf_simple *buf, void *data, size_t size)
Initialize a net_buf_simple object with data.
void net_buf_simple_push_be16(struct net_buf_simple *buf, uint16_t val)
Push 16-bit value to the beginning of the buffer.
void * net_buf_simple_remove_mem(struct net_buf_simple *buf, size_t len)
Remove data from the end of the buffer.
static void net_buf_push_le48(struct net_buf *buf, uint64_t val)
Push 48-bit value to the beginning of the buffer.
Definition: buf.h:2003
static uint32_t net_buf_pull_le24(struct net_buf *buf)
Remove and convert 24 bits from the beginning of the buffer.
Definition: buf.h:2137
void net_buf_simple_push_le32(struct net_buf_simple *buf, uint32_t val)
Push 32-bit value to the beginning of the buffer.
static uint8_t * net_buf_add_u8(struct net_buf *buf, uint8_t val)
Add (8-bit) byte at the end of the buffer.
Definition: buf.h:1528
static void net_buf_push_be24(struct net_buf *buf, uint32_t val)
Push 24-bit value to the beginning of the buffer.
Definition: buf.h:1961
static void net_buf_push_le24(struct net_buf *buf, uint32_t val)
Push 24-bit value to the beginning of the buffer.
Definition: buf.h:1947
static void net_buf_reserve(struct net_buf *buf, size_t reserve)
Initialize buffer with the given headroom.
Definition: buf.h:1478
static uint64_t net_buf_remove_be64(struct net_buf *buf)
Remove and convert 64 bits from the end of the buffer.
Definition: buf.h:1858
struct net_buf * net_buf_alloc_with_data(struct net_buf_pool *pool, void *data, size_t size, k_timeout_t timeout)
Allocate a new buffer from a pool but with external data pointer.
static uint8_t * net_buf_simple_tail(struct net_buf_simple *buf)
Get the tail pointer for a buffer.
Definition: buf.h:809
static uint32_t net_buf_pull_be24(struct net_buf *buf)
Remove and convert 24 bits from the beginning of the buffer.
Definition: buf.h:2152
void net_buf_simple_add_be64(struct net_buf_simple *buf, uint64_t val)
Add 64-bit value at the end of the buffer.
static void net_buf_add_be48(struct net_buf *buf, uint64_t val)
Add 48-bit value at the end of the buffer.
Definition: buf.h:1648
uint8_t * net_buf_simple_add_u8(struct net_buf_simple *buf, uint8_t val)
Add (8-bit) byte at the end of the buffer.
static uint32_t net_buf_remove_le24(struct net_buf *buf)
Remove and convert 24 bits from the end of the buffer.
Definition: buf.h:1768
static void net_buf_push_u8(struct net_buf *buf, uint8_t val)
Push 8-bit value to the beginning of the buffer.
Definition: buf.h:1905
void net_buf_simple_add_be16(struct net_buf_simple *buf, uint16_t val)
Add 16-bit value at the end of the buffer.
uint16_t net_buf_simple_remove_be16(struct net_buf_simple *buf)
Remove and convert 16 bits from the end of the buffer.
static void * net_buf_push(struct net_buf *buf, size_t len)
Prepare data to be added at the start of the buffer.
Definition: buf.h:1874
static uint16_t net_buf_pull_be16(struct net_buf *buf)
Remove and convert 16 bits from the beginning of the buffer.
Definition: buf.h:2122
static void net_buf_push_le32(struct net_buf *buf, uint32_t val)
Push 32-bit value to the beginning of the buffer.
Definition: buf.h:1975
static uint64_t net_buf_pull_le64(struct net_buf *buf)
Remove and convert 64 bits from the beginning of the buffer.
Definition: buf.h:2227
uint32_t net_buf_simple_remove_be24(struct net_buf_simple *buf)
Remove and convert 24 bits from the end of the buffer.
void * net_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len)
Remove data from the beginning of the buffer.
uint32_t net_buf_simple_remove_le32(struct net_buf_simple *buf)
Remove and convert 32 bits from the end of the buffer.
void net_buf_simple_add_le16(struct net_buf_simple *buf, uint16_t val)
Add 16-bit value at the end of the buffer.
void * net_buf_simple_push_mem(struct net_buf_simple *buf, const void *mem, size_t len)
Copy given number of bytes from memory to the start of the buffer.
static uint64_t net_buf_remove_be48(struct net_buf *buf)
Remove and convert 48 bits from the end of the buffer.
Definition: buf.h:1828
static uint16_t net_buf_max_len(struct net_buf *buf)
Check maximum net_buf::len value.
Definition: buf.h:2284
void net_buf_simple_add_be32(struct net_buf_simple *buf, uint32_t val)
Add 32-bit value at the end of the buffer.
#define NET_BUF_EXTERNAL_DATA
Flag indicating that the buffer's associated data pointer, points to externally allocated memory.
Definition: buf.h:901
static uint16_t net_buf_remove_le16(struct net_buf *buf)
Remove and convert 16 bits from the end of the buffer.
Definition: buf.h:1723
static void net_buf_push_le16(struct net_buf *buf, uint16_t val)
Push 16-bit value to the beginning of the buffer.
Definition: buf.h:1919
uint64_t net_buf_simple_remove_be48(struct net_buf_simple *buf)
Remove and convert 48 bits from the end of the buffer.
void net_buf_simple_push_le24(struct net_buf_simple *buf, uint32_t val)
Push 24-bit value to the beginning of the buffer.
void net_buf_unref(struct net_buf *buf)
Decrements the reference count of a buffer.
static void net_buf_push_be48(struct net_buf *buf, uint64_t val)
Push 48-bit value to the beginning of the buffer.
Definition: buf.h:2017
void net_buf_simple_push_be24(struct net_buf_simple *buf, uint32_t val)
Push 24-bit value to the beginning of the buffer.
void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag)
Insert a new fragment to a chain of bufs.
uint64_t net_buf_simple_remove_le48(struct net_buf_simple *buf)
Remove and convert 48 bits from the end of the buffer.
void * net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem, size_t len)
Copy given number of bytes from memory to the end of the buffer.
static void net_buf_add_le64(struct net_buf *buf, uint64_t val)
Add 64-bit value at the end of the buffer.
Definition: buf.h:1663
static uint8_t * net_buf_tail(struct net_buf *buf)
Get the tail pointer for a buffer.
Definition: buf.h:2298
static uint32_t net_buf_remove_be32(struct net_buf *buf)
Remove and convert 32 bits from the end of the buffer.
Definition: buf.h:1798
static void * net_buf_remove_mem(struct net_buf *buf, size_t len)
Remove data from the end of the buffer.
Definition: buf.h:1693
static void * net_buf_add_mem(struct net_buf *buf, const void *mem, size_t len)
Copies the given number of bytes to the end of the buffer.
Definition: buf.h:1511
size_t net_buf_simple_headroom(struct net_buf_simple *buf)
Check buffer headroom.
uint64_t net_buf_simple_pull_be64(struct net_buf_simple *buf)
Remove and convert 64 bits from the beginning of the buffer.
void net_buf_simple_push_be32(struct net_buf_simple *buf, uint32_t val)
Push 32-bit value to the beginning of the buffer.
static void net_buf_push_le64(struct net_buf *buf, uint64_t val)
Push 64-bit value to the beginning of the buffer.
Definition: buf.h:2031
uint16_t net_buf_simple_pull_le16(struct net_buf_simple *buf)
Remove and convert 16 bits from the beginning of the buffer.
int net_buf_id(struct net_buf *buf)
Get a zero-based index for a buffer.
static uint8_t net_buf_remove_u8(struct net_buf *buf)
Remove a 8-bit value from the end of the buffer.
Definition: buf.h:1708
void net_buf_simple_add_be48(struct net_buf_simple *buf, uint64_t val)
Add 48-bit value at the end of the buffer.
static void net_buf_add_le16(struct net_buf *buf, uint16_t val)
Add 16-bit value at the end of the buffer.
Definition: buf.h:1543
uint16_t net_buf_simple_pull_be16(struct net_buf_simple *buf)
Remove and convert 16 bits from the beginning of the buffer.
static void net_buf_push_be32(struct net_buf *buf, uint32_t val)
Push 32-bit value to the beginning of the buffer.
Definition: buf.h:1989
static void net_buf_add_le32(struct net_buf *buf, uint32_t val)
Add 32-bit value at the end of the buffer.
Definition: buf.h:1603
uint32_t net_buf_simple_remove_be32(struct net_buf_simple *buf)
Remove and convert 32 bits from the end of the buffer.
static uint32_t net_buf_remove_le32(struct net_buf *buf)
Remove and convert 32 bits from the end of the buffer.
Definition: buf.h:1783
static size_t net_buf_frags_len(struct net_buf *buf)
Calculate amount of bytes stored in fragments.
Definition: buf.h:2471
static uint16_t net_buf_pull_le16(struct net_buf *buf)
Remove and convert 16 bits from the beginning of the buffer.
Definition: buf.h:2107
static void * net_buf_pull_mem(struct net_buf *buf, size_t len)
Remove data from the beginning of the buffer.
Definition: buf.h:2077
static void net_buf_simple_restore(struct net_buf_simple *buf, struct net_buf_simple_state *state)
Restore the parsing state of a buffer.
Definition: buf.h:885
static void * net_buf_pull(struct net_buf *buf, size_t len)
Remove data from the beginning of the buffer.
Definition: buf.h:2061
size_t net_buf_data_match(const struct net_buf *buf, size_t offset, const void *data, size_t len)
Match data with a net_buf's content.
static void net_buf_add_le48(struct net_buf *buf, uint64_t val)
Add 48-bit value at the end of the buffer.
Definition: buf.h:1633
void net_buf_simple_add_le24(struct net_buf_simple *buf, uint32_t val)
Add 24-bit value at the end of the buffer.
static void * net_buf_user_data(const struct net_buf *buf)
Get a pointer to the user data of a buffer.
Definition: buf.h:1465
struct net_buf * net_buf_clone(struct net_buf *buf, k_timeout_t timeout)
Clone buffer.
uint8_t net_buf_simple_remove_u8(struct net_buf_simple *buf)
Remove a 8-bit value from the end of the buffer.
void * net_buf_simple_pull(struct net_buf_simple *buf, size_t len)
Remove data from the beginning of the buffer.
size_t net_buf_linearize(void *dst, size_t dst_len, struct net_buf *src, size_t offset, size_t len)
Copy bytes from net_buf chain starting at offset to linear buffer.
void net_buf_simple_push_be64(struct net_buf_simple *buf, uint64_t val)
Push 64-bit value to the beginning of the buffer.
struct _slist sys_slist_t
Single-linked list structure.
Definition: slist.h:49
struct _snode sys_snode_t
Single-linked list node structure.
Definition: slist.h:39
Public kernel APIs.
state
Definition: parser_state.h:29
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT64_TYPE__ uint64_t
Definition: stdint.h:91
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
Definition: kernel.h:2374
Definition: kernel.h:2613
Kernel Spin Lock.
Definition: spinlock.h:45
Kernel timeout type.
Definition: sys_clock.h:65
Definition: buf.h:965
const struct net_buf_data_cb * cb
Definition: buf.h:966
size_t max_alloc_size
Definition: buf.h:968
void * alloc_data
Definition: buf.h:967
Definition: buf.h:958
uint8_t *(* ref)(struct net_buf *buf, uint8_t *data)
Definition: buf.h:961
uint8_t *(* alloc)(struct net_buf *buf, size_t *size, k_timeout_t timeout)
Definition: buf.h:959
void(* unref)(struct net_buf *buf, uint8_t *data)
Definition: buf.h:962
Definition: buf.h:1081
uint8_t * data_pool
Definition: buf.h:1082
Network buffer pool representation.
Definition: buf.h:976
void(*const destroy)(struct net_buf *buf)
Optional destroy callback when buffer is freed.
Definition: buf.h:1004
uint16_t uninit_count
Number of uninitialized buffers.
Definition: buf.h:987
uint8_t user_data_size
Definition: buf.h:990
const uint16_t buf_count
Number of buffers in pool.
Definition: buf.h:984
const struct net_buf_data_alloc * alloc
Data allocation handlers.
Definition: buf.h:1007
struct k_lifo free
LIFO to place the buffer into when free.
Definition: buf.h:978
struct k_spinlock lock
Definition: buf.h:981
Parsing state of a buffer.
Definition: buf.h:854
uint16_t offset
Offset of the data pointer from the beginning of the storage.
Definition: buf.h:856
uint16_t len
Length of data.
Definition: buf.h:858
Simple network buffer representation.
Definition: buf.h:87
uint8_t * data
Pointer to the start of data in the buffer.
Definition: buf.h:89
uint16_t size
Amount of data that net_buf_simple::__buf can store.
Definition: buf.h:99
uint16_t len
Length of the data behind the data pointer.
Definition: buf.h:96
Network buffer representation.
Definition: buf.h:910
uint16_t size
Amount of data that this buffer can store.
Definition: buf.h:942
struct net_buf * frags
Fragments associated with this buffer.
Definition: buf.h:915
uint8_t ref
Reference count.
Definition: buf.h:918
uint8_t pool_id
Where the buffer should go when freed up.
Definition: buf.h:924
sys_snode_t node
Allow placing the buffer into sys_slist_t.
Definition: buf.h:912
uint8_t user_data_size
Definition: buf.h:927
uint8_t flags
Bit-field of buffer flags.
Definition: buf.h:921
uint8_t * data
Pointer to the start of data in the buffer.
Definition: buf.h:936
uint8_t user_data[]
System metadata for this buffer.
Definition: buf.h:955
struct net_buf_simple b
Definition: buf.h:951
uint16_t len
Length of the data behind the data pointer.
Definition: buf.h:939
Misc utilities.