Zephyr API Documentation  3.0.0
A Scalable Open Source RTOS
3.0.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 <sys/util.h>
16#include <zephyr.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
29/* Alignment needed for various parts of the buffer definition */
30#define __net_buf_align __aligned(sizeof(void *))
31
42#define NET_BUF_SIMPLE_DEFINE(_name, _size) \
43 uint8_t net_buf_data_##_name[_size]; \
44 struct net_buf_simple _name = { \
45 .data = net_buf_data_##_name, \
46 .len = 0, \
47 .size = _size, \
48 .__buf = net_buf_data_##_name, \
49 }
50
61#define NET_BUF_SIMPLE_DEFINE_STATIC(_name, _size) \
62 static __noinit uint8_t net_buf_data_##_name[_size]; \
63 static struct net_buf_simple _name = { \
64 .data = net_buf_data_##_name, \
65 .len = 0, \
66 .size = _size, \
67 .__buf = net_buf_data_##_name, \
68 }
69
86
93
96
100 uint8_t *__buf;
101};
102
119#define NET_BUF_SIMPLE(_size) \
120 ((struct net_buf_simple *)(&(struct { \
121 struct net_buf_simple buf; \
122 uint8_t data[_size]; \
123 }) { \
124 .buf.size = _size, \
125 }))
126
136static inline void net_buf_simple_init(struct net_buf_simple *buf,
137 size_t reserve_head)
138{
139 if (!buf->__buf) {
140 buf->__buf = (uint8_t *)buf + sizeof(*buf);
141 }
142
143 buf->data = buf->__buf + reserve_head;
144 buf->len = 0U;
145}
146
157 void *data, size_t size);
158
166static inline void net_buf_simple_reset(struct net_buf_simple *buf)
167{
168 buf->len = 0U;
169 buf->data = buf->__buf;
170}
171
182void net_buf_simple_clone(const struct net_buf_simple *original,
183 struct net_buf_simple *clone);
184
196void *net_buf_simple_add(struct net_buf_simple *buf, size_t len);
197
210void *net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem,
211 size_t len);
212
225
237
249
261
273
285
297
309
321
333
345
356void *net_buf_simple_remove_mem(struct net_buf_simple *buf, size_t len);
357
369
381
393
405
417
429
441
453
465
477
489
501void *net_buf_simple_push(struct net_buf_simple *buf, size_t len);
502
515void *net_buf_simple_push_mem(struct net_buf_simple *buf, const void *mem,
516 size_t len);
517
528
539
549
560
571
582
593
604
615
626
637
649void *net_buf_simple_pull(struct net_buf_simple *buf, size_t len);
650
662void *net_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len);
663
675
687
699
711
723
735
747
759
771
783
795
805static inline uint8_t *net_buf_simple_tail(struct net_buf_simple *buf)
806{
807 return buf->data + buf->len;
808}
809
820
831
842
855};
856
865static inline void net_buf_simple_save(struct net_buf_simple *buf,
867{
868 state->offset = net_buf_simple_headroom(buf);
869 state->len = buf->len;
870}
871
881static inline void net_buf_simple_restore(struct net_buf_simple *buf,
883{
884 buf->data = buf->__buf + state->offset;
885 buf->len = state->len;
886}
887
896#define NET_BUF_FRAGS BIT(0)
906#define NET_BUF_EXTERNAL_DATA BIT(1)
907
915struct net_buf {
916 union {
919
921 struct net_buf *frags;
922 };
923
926
929
932
933 /* Size of user data on this buffer */
935
936 /* Union for convenience access to the net_buf_simple members, also
937 * preserving the old API.
938 */
939 union {
940 /* The ABI of this struct must match net_buf_simple */
941 struct {
944
947
950
955 uint8_t *__buf;
956 };
957
959 };
960
962 uint8_t user_data[] __net_buf_align;
963};
964
966 uint8_t * __must_check (*alloc)(struct net_buf *buf, size_t *size,
968 uint8_t * __must_check (*ref)(struct net_buf *buf, uint8_t *data);
969 void (*unref)(struct net_buf *buf, uint8_t *data);
970};
971
973 const struct net_buf_data_cb *cb;
975};
976
984 struct k_lifo free;
985
986 /* to prevent concurrent access/modifications */
988
991
994
995 /* Size of user data allocated to this pool */
997
998#if defined(CONFIG_NET_BUF_POOL_USAGE)
1000 atomic_t avail_count;
1001
1003 const uint16_t pool_size;
1004
1006 const char *name;
1007#endif /* CONFIG_NET_BUF_POOL_USAGE */
1008
1010 void (*const destroy)(struct net_buf *buf);
1011
1014
1016 struct net_buf * const __bufs;
1017};
1018
1020#if defined(CONFIG_NET_BUF_POOL_USAGE)
1021#define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _ud_size, _destroy) \
1022 { \
1023 .free = Z_LIFO_INITIALIZER(_pool.free), \
1024 .lock = { }, \
1025 .buf_count = _count, \
1026 .uninit_count = _count, \
1027 .user_data_size = _ud_size, \
1028 .avail_count = ATOMIC_INIT(_count), \
1029 .name = STRINGIFY(_pool), \
1030 .destroy = _destroy, \
1031 .alloc = _alloc, \
1032 .__bufs = (struct net_buf *)_bufs, \
1033 }
1034#else
1035#define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _ud_size, _destroy) \
1036 { \
1037 .free = Z_LIFO_INITIALIZER(_pool.free), \
1038 .lock = { }, \
1039 .buf_count = _count, \
1040 .uninit_count = _count, \
1041 .user_data_size = _ud_size, \
1042 .destroy = _destroy, \
1043 .alloc = _alloc, \
1044 .__bufs = (struct net_buf *)_bufs, \
1045 }
1046#endif /* CONFIG_NET_BUF_POOL_USAGE */
1047
1048#define _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size) \
1049 struct _net_buf_##_name { uint8_t b[sizeof(struct net_buf)]; \
1050 uint8_t ud[_ud_size]; } __net_buf_align; \
1051 BUILD_ASSERT(_ud_size <= UINT8_MAX); \
1052 BUILD_ASSERT(offsetof(struct net_buf, user_data) == \
1053 offsetof(struct _net_buf_##_name, ud), "Invalid offset"); \
1054 BUILD_ASSERT(__alignof__(struct net_buf) == \
1055 __alignof__(struct _net_buf_##_name), "Invalid alignment"); \
1056 BUILD_ASSERT(sizeof(struct _net_buf_##_name) == \
1057 ROUND_UP(sizeof(struct net_buf) + _ud_size, __alignof__(struct net_buf)), \
1058 "Size cannot be determined"); \
1059 static struct _net_buf_##_name _net_buf_##_name[_count] __noinit
1060
1061extern const struct net_buf_data_alloc net_buf_heap_alloc;
1091#define NET_BUF_POOL_HEAP_DEFINE(_name, _count, _ud_size, _destroy) \
1092 _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size); \
1093 static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) = \
1094 NET_BUF_POOL_INITIALIZER(_name, &net_buf_heap_alloc, \
1095 _net_buf_##_name, _count, _ud_size, \
1096 _destroy)
1097
1101};
1102
1104extern const struct net_buf_data_cb net_buf_fixed_cb;
1135#define NET_BUF_POOL_FIXED_DEFINE(_name, _count, _data_size, _ud_size, _destroy) \
1136 _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size); \
1137 static uint8_t __noinit net_buf_data_##_name[_count][_data_size]; \
1138 static const struct net_buf_pool_fixed net_buf_fixed_##_name = { \
1139 .data_size = _data_size, \
1140 .data_pool = (uint8_t *)net_buf_data_##_name, \
1141 }; \
1142 static const struct net_buf_data_alloc net_buf_fixed_alloc_##_name = { \
1143 .cb = &net_buf_fixed_cb, \
1144 .alloc_data = (void *)&net_buf_fixed_##_name, \
1145 }; \
1146 static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) = \
1147 NET_BUF_POOL_INITIALIZER(_name, &net_buf_fixed_alloc_##_name, \
1148 _net_buf_##_name, _count, _ud_size, \
1149 _destroy)
1150
1152extern const struct net_buf_data_cb net_buf_var_cb;
1179#define NET_BUF_POOL_VAR_DEFINE(_name, _count, _data_size, _ud_size, _destroy) \
1180 _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size); \
1181 K_HEAP_DEFINE(net_buf_mem_pool_##_name, _data_size); \
1182 static const struct net_buf_data_alloc net_buf_data_alloc_##_name = { \
1183 .cb = &net_buf_var_cb, \
1184 .alloc_data = &net_buf_mem_pool_##_name, \
1185 }; \
1186 static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) = \
1187 NET_BUF_POOL_INITIALIZER(_name, &net_buf_data_alloc_##_name, \
1188 _net_buf_##_name, _count, _ud_size, \
1189 _destroy)
1190
1212#define NET_BUF_POOL_DEFINE(_name, _count, _size, _ud_size, _destroy) \
1213 NET_BUF_POOL_FIXED_DEFINE(_name, _count, _size, _ud_size, _destroy)
1214
1223
1236int net_buf_id(struct net_buf *buf);
1237
1252#if defined(CONFIG_NET_BUF_LOG)
1253struct net_buf * __must_check net_buf_alloc_fixed_debug(struct net_buf_pool *pool,
1255 const char *func,
1256 int line);
1257#define net_buf_alloc_fixed(_pool, _timeout) \
1258 net_buf_alloc_fixed_debug(_pool, _timeout, __func__, __LINE__)
1259#else
1260struct net_buf * __must_check net_buf_alloc_fixed(struct net_buf_pool *pool,
1262#endif
1263
1267static inline struct net_buf * __must_check net_buf_alloc(struct net_buf_pool *pool,
1269{
1270 return net_buf_alloc_fixed(pool, timeout);
1271}
1272
1288#if defined(CONFIG_NET_BUF_LOG)
1289struct net_buf * __must_check net_buf_alloc_len_debug(struct net_buf_pool *pool,
1290 size_t size,
1292 const char *func,
1293 int line);
1294#define net_buf_alloc_len(_pool, _size, _timeout) \
1295 net_buf_alloc_len_debug(_pool, _size, _timeout, __func__, __LINE__)
1296#else
1297struct net_buf * __must_check net_buf_alloc_len(struct net_buf_pool *pool,
1298 size_t size,
1300#endif
1301
1321#if defined(CONFIG_NET_BUF_LOG)
1322struct net_buf * __must_check net_buf_alloc_with_data_debug(struct net_buf_pool *pool,
1323 void *data, size_t size,
1325 const char *func, int line);
1326#define net_buf_alloc_with_data(_pool, _data_, _size, _timeout) \
1327 net_buf_alloc_with_data_debug(_pool, _data_, _size, _timeout, \
1328 __func__, __LINE__)
1329#else
1330struct net_buf * __must_check net_buf_alloc_with_data(struct net_buf_pool *pool,
1331 void *data, size_t size,
1333#endif
1334
1348#if defined(CONFIG_NET_BUF_LOG)
1349struct net_buf * __must_check net_buf_get_debug(struct k_fifo *fifo,
1351 const char *func, int line);
1352#define net_buf_get(_fifo, _timeout) \
1353 net_buf_get_debug(_fifo, _timeout, __func__, __LINE__)
1354#else
1355struct net_buf * __must_check net_buf_get(struct k_fifo *fifo,
1357#endif
1358
1368static inline void net_buf_destroy(struct net_buf *buf)
1369{
1370 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
1371
1372 k_lifo_put(&pool->free, buf);
1373}
1374
1382void net_buf_reset(struct net_buf *buf);
1383
1392void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve);
1393
1403void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf);
1404
1415struct net_buf * __must_check net_buf_slist_get(sys_slist_t *list);
1416
1426void net_buf_put(struct k_fifo *fifo, struct net_buf *buf);
1427
1435#if defined(CONFIG_NET_BUF_LOG)
1436void net_buf_unref_debug(struct net_buf *buf, const char *func, int line);
1437#define net_buf_unref(_buf) \
1438 net_buf_unref_debug(_buf, __func__, __LINE__)
1439#else
1440void net_buf_unref(struct net_buf *buf);
1441#endif
1442
1450struct net_buf * __must_check net_buf_ref(struct net_buf *buf);
1451
1465struct net_buf * __must_check net_buf_clone(struct net_buf *buf,
1467
1475static inline void * __must_check net_buf_user_data(const struct net_buf *buf)
1476{
1477 return (void *)buf->user_data;
1478}
1479
1488static inline void net_buf_reserve(struct net_buf *buf, size_t reserve)
1489{
1490 net_buf_simple_reserve(&buf->b, reserve);
1491}
1492
1504static inline void *net_buf_add(struct net_buf *buf, size_t len)
1505{
1506 return net_buf_simple_add(&buf->b, len);
1507}
1508
1521static inline void *net_buf_add_mem(struct net_buf *buf, const void *mem,
1522 size_t len)
1523{
1524 return net_buf_simple_add_mem(&buf->b, mem, len);
1525}
1526
1538static inline uint8_t *net_buf_add_u8(struct net_buf *buf, uint8_t val)
1539{
1540 return net_buf_simple_add_u8(&buf->b, val);
1541}
1542
1553static inline void net_buf_add_le16(struct net_buf *buf, uint16_t val)
1554{
1555 net_buf_simple_add_le16(&buf->b, val);
1556}
1557
1568static inline void net_buf_add_be16(struct net_buf *buf, uint16_t val)
1569{
1570 net_buf_simple_add_be16(&buf->b, val);
1571}
1572
1583static inline void net_buf_add_le24(struct net_buf *buf, uint32_t val)
1584{
1585 net_buf_simple_add_le24(&buf->b, val);
1586}
1587
1598static inline void net_buf_add_be24(struct net_buf *buf, uint32_t val)
1599{
1600 net_buf_simple_add_be24(&buf->b, val);
1601}
1602
1613static inline void net_buf_add_le32(struct net_buf *buf, uint32_t val)
1614{
1615 net_buf_simple_add_le32(&buf->b, val);
1616}
1617
1628static inline void net_buf_add_be32(struct net_buf *buf, uint32_t val)
1629{
1630 net_buf_simple_add_be32(&buf->b, val);
1631}
1632
1643static inline void net_buf_add_le48(struct net_buf *buf, uint64_t val)
1644{
1645 net_buf_simple_add_le48(&buf->b, val);
1646}
1647
1658static inline void net_buf_add_be48(struct net_buf *buf, uint64_t val)
1659{
1660 net_buf_simple_add_be48(&buf->b, val);
1661}
1662
1673static inline void net_buf_add_le64(struct net_buf *buf, uint64_t val)
1674{
1675 net_buf_simple_add_le64(&buf->b, val);
1676}
1677
1688static inline void net_buf_add_be64(struct net_buf *buf, uint64_t val)
1689{
1690 net_buf_simple_add_be64(&buf->b, val);
1691}
1692
1703static inline void *net_buf_remove_mem(struct net_buf *buf, size_t len)
1704{
1705 return net_buf_simple_remove_mem(&buf->b, len);
1706}
1707
1718static inline uint8_t net_buf_remove_u8(struct net_buf *buf)
1719{
1720 return net_buf_simple_remove_u8(&buf->b);
1721}
1722
1733static inline uint16_t net_buf_remove_le16(struct net_buf *buf)
1734{
1735 return net_buf_simple_remove_le16(&buf->b);
1736}
1737
1748static inline uint16_t net_buf_remove_be16(struct net_buf *buf)
1749{
1750 return net_buf_simple_remove_be16(&buf->b);
1751}
1752
1763static inline uint32_t net_buf_remove_be24(struct net_buf *buf)
1764{
1765 return net_buf_simple_remove_be24(&buf->b);
1766}
1767
1778static inline uint32_t net_buf_remove_le24(struct net_buf *buf)
1779{
1780 return net_buf_simple_remove_le24(&buf->b);
1781}
1782
1793static inline uint32_t net_buf_remove_le32(struct net_buf *buf)
1794{
1795 return net_buf_simple_remove_le32(&buf->b);
1796}
1797
1808static inline uint32_t net_buf_remove_be32(struct net_buf *buf)
1809{
1810 return net_buf_simple_remove_be32(&buf->b);
1811}
1812
1823static inline uint64_t net_buf_remove_le48(struct net_buf *buf)
1824{
1825 return net_buf_simple_remove_le48(&buf->b);
1826}
1827
1838static inline uint64_t net_buf_remove_be48(struct net_buf *buf)
1839{
1840 return net_buf_simple_remove_be48(&buf->b);
1841}
1842
1853static inline uint64_t net_buf_remove_le64(struct net_buf *buf)
1854{
1855 return net_buf_simple_remove_le64(&buf->b);
1856}
1857
1868static inline uint64_t net_buf_remove_be64(struct net_buf *buf)
1869{
1870 return net_buf_simple_remove_be64(&buf->b);
1871}
1872
1884static inline void *net_buf_push(struct net_buf *buf, size_t len)
1885{
1886 return net_buf_simple_push(&buf->b, len);
1887}
1888
1901static inline void *net_buf_push_mem(struct net_buf *buf, const void *mem,
1902 size_t len)
1903{
1904 return net_buf_simple_push_mem(&buf->b, mem, len);
1905}
1906
1915static inline void net_buf_push_u8(struct net_buf *buf, uint8_t val)
1916{
1917 net_buf_simple_push_u8(&buf->b, val);
1918}
1919
1929static inline void net_buf_push_le16(struct net_buf *buf, uint16_t val)
1930{
1931 net_buf_simple_push_le16(&buf->b, val);
1932}
1933
1943static inline void net_buf_push_be16(struct net_buf *buf, uint16_t val)
1944{
1945 net_buf_simple_push_be16(&buf->b, val);
1946}
1947
1957static inline void net_buf_push_le24(struct net_buf *buf, uint32_t val)
1958{
1959 net_buf_simple_push_le24(&buf->b, val);
1960}
1961
1971static inline void net_buf_push_be24(struct net_buf *buf, uint32_t val)
1972{
1973 net_buf_simple_push_be24(&buf->b, val);
1974}
1975
1985static inline void net_buf_push_le32(struct net_buf *buf, uint32_t val)
1986{
1987 net_buf_simple_push_le32(&buf->b, val);
1988}
1989
1999static inline void net_buf_push_be32(struct net_buf *buf, uint32_t val)
2000{
2001 net_buf_simple_push_be32(&buf->b, val);
2002}
2003
2013static inline void net_buf_push_le48(struct net_buf *buf, uint64_t val)
2014{
2015 net_buf_simple_push_le48(&buf->b, val);
2016}
2017
2027static inline void net_buf_push_be48(struct net_buf *buf, uint64_t val)
2028{
2029 net_buf_simple_push_be48(&buf->b, val);
2030}
2031
2041static inline void net_buf_push_le64(struct net_buf *buf, uint64_t val)
2042{
2043 net_buf_simple_push_le64(&buf->b, val);
2044}
2045
2055static inline void net_buf_push_be64(struct net_buf *buf, uint64_t val)
2056{
2057 net_buf_simple_push_be64(&buf->b, val);
2058}
2059
2071static inline void *net_buf_pull(struct net_buf *buf, size_t len)
2072{
2073 return net_buf_simple_pull(&buf->b, len);
2074}
2075
2087static inline void *net_buf_pull_mem(struct net_buf *buf, size_t len)
2088{
2089 return net_buf_simple_pull_mem(&buf->b, len);
2090}
2091
2102static inline uint8_t net_buf_pull_u8(struct net_buf *buf)
2103{
2104 return net_buf_simple_pull_u8(&buf->b);
2105}
2106
2117static inline uint16_t net_buf_pull_le16(struct net_buf *buf)
2118{
2119 return net_buf_simple_pull_le16(&buf->b);
2120}
2121
2132static inline uint16_t net_buf_pull_be16(struct net_buf *buf)
2133{
2134 return net_buf_simple_pull_be16(&buf->b);
2135}
2136
2147static inline uint32_t net_buf_pull_le24(struct net_buf *buf)
2148{
2149 return net_buf_simple_pull_le24(&buf->b);
2150}
2151
2162static inline uint32_t net_buf_pull_be24(struct net_buf *buf)
2163{
2164 return net_buf_simple_pull_be24(&buf->b);
2165}
2166
2177static inline uint32_t net_buf_pull_le32(struct net_buf *buf)
2178{
2179 return net_buf_simple_pull_le32(&buf->b);
2180}
2181
2192static inline uint32_t net_buf_pull_be32(struct net_buf *buf)
2193{
2194 return net_buf_simple_pull_be32(&buf->b);
2195}
2196
2207static inline uint64_t net_buf_pull_le48(struct net_buf *buf)
2208{
2209 return net_buf_simple_pull_le48(&buf->b);
2210}
2211
2222static inline uint64_t net_buf_pull_be48(struct net_buf *buf)
2223{
2224 return net_buf_simple_pull_be48(&buf->b);
2225}
2226
2237static inline uint64_t net_buf_pull_le64(struct net_buf *buf)
2238{
2239 return net_buf_simple_pull_le64(&buf->b);
2240}
2241
2252static inline uint64_t net_buf_pull_be64(struct net_buf *buf)
2253{
2254 return net_buf_simple_pull_be64(&buf->b);
2255}
2256
2266static inline size_t net_buf_tailroom(struct net_buf *buf)
2267{
2268 return net_buf_simple_tailroom(&buf->b);
2269}
2270
2280static inline size_t net_buf_headroom(struct net_buf *buf)
2281{
2282 return net_buf_simple_headroom(&buf->b);
2283}
2284
2294static inline uint16_t net_buf_max_len(struct net_buf *buf)
2295{
2296 return net_buf_simple_max_len(&buf->b);
2297}
2298
2308static inline uint8_t *net_buf_tail(struct net_buf *buf)
2309{
2310 return net_buf_simple_tail(&buf->b);
2311}
2312
2319
2331void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag);
2332
2347struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag);
2348
2358#if defined(CONFIG_NET_BUF_LOG)
2359struct net_buf *net_buf_frag_del_debug(struct net_buf *parent,
2360 struct net_buf *frag,
2361 const char *func, int line);
2362#define net_buf_frag_del(_parent, _frag) \
2363 net_buf_frag_del_debug(_parent, _frag, __func__, __LINE__)
2364#else
2365struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag);
2366#endif
2367
2383size_t net_buf_linearize(void *dst, size_t dst_len,
2384 struct net_buf *src, size_t offset, size_t len);
2385
2400typedef struct net_buf * __must_check (*net_buf_allocator_cb)(k_timeout_t timeout,
2401 void *user_data);
2402
2424size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
2425 const void *value, k_timeout_t timeout,
2426 net_buf_allocator_cb allocate_cb, void *user_data);
2427
2443static inline struct net_buf *net_buf_skip(struct net_buf *buf, size_t len)
2444{
2445 while (buf && len--) {
2446 net_buf_pull_u8(buf);
2447 if (!buf->len) {
2448 buf = net_buf_frag_del(NULL, buf);
2449 }
2450 }
2451
2452 return buf;
2453}
2454
2465static inline size_t net_buf_frags_len(struct net_buf *buf)
2466{
2467 size_t bytes = 0;
2468
2469 while (buf) {
2470 bytes += buf->len;
2471 buf = buf->frags;
2472 }
2473
2474 return bytes;
2475}
2476
2481#ifdef __cplusplus
2482}
2483#endif
2484
2485#endif /* ZEPHYR_INCLUDE_NET_BUF_H_ */
long atomic_t
Definition: atomic.h:22
ZTEST_BMEM int timeout
Definition: main.c:31
struct k_fifo fifo
Definition: errno.c:43
#define k_lifo_put(lifo, data)
Add an element to a LIFO queue.
Definition: kernel.h:2399
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)
static void net_buf_simple_init(struct net_buf_simple *buf, size_t reserve_head)
Initialize a net_buf_simple object.
Definition: buf.h:136
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:1688
static size_t net_buf_headroom(struct net_buf *buf)
Check buffer headroom.
Definition: buf.h:2280
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:1823
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:1598
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:2400
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:2443
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:1504
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:1583
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:2252
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:2055
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:1853
static void net_buf_simple_reset(struct net_buf_simple *buf)
Reset buffer.
Definition: buf.h:166
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:1267
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:1628
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:865
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:1748
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:2177
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:2192
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:2266
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:1568
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:2222
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:1943
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:1763
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:2102
static void net_buf_destroy(struct net_buf *buf)
Destroy buffer from custom destroy callback.
Definition: buf.h:1368
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:1901
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:2207
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:2013
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:2147
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:1538
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:1971
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:1957
static void net_buf_reserve(struct net_buf *buf, size_t reserve)
Initialize buffer with the given headroom.
Definition: buf.h:1488
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:1868
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:805
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:2162
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:1658
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:1778
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:1915
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:1884
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:2132
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:1985
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:2237
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:1838
static uint16_t net_buf_max_len(struct net_buf *buf)
Check maximum net_buf::len value.
Definition: buf.h:2294
void net_buf_simple_add_be32(struct net_buf_simple *buf, uint32_t val)
Add 32-bit value at the end of the buffer.
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:1733
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:1929
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:2027
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:1673
static uint8_t * net_buf_tail(struct net_buf *buf)
Get the tail pointer for a buffer.
Definition: buf.h:2308
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:1808
static void * net_buf_remove_mem(struct net_buf *buf, size_t len)
Remove data from the end of the buffer.
Definition: buf.h:1703
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:1521
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:2041
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:1718
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:1553
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:1999
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:1613
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:1793
static size_t net_buf_frags_len(struct net_buf *buf)
Calculate amount of bytes stored in fragments.
Definition: buf.h:2465
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:2117
static void * net_buf_pull_mem(struct net_buf *buf, size_t len)
Remove data from the beginning of the buffer.
Definition: buf.h:2087
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:881
static void * net_buf_pull(struct net_buf *buf, size_t len)
Remove data from the beginning of the buffer.
Definition: buf.h:2071
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:1643
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:1475
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.
state
Definition: http_parser_state.h:30
struct _slist sys_slist_t
Definition: slist.h:40
struct _snode sys_snode_t
Definition: slist.h:33
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT64_TYPE__ uint64_t
Definition: stdint.h:61
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
Definition: kernel.h:2116
Definition: kernel.h:2350
Kernel Spin Lock.
Definition: spinlock.h:42
Kernel timeout type.
Definition: sys_clock.h:65
Definition: buf.h:972
const struct net_buf_data_cb * cb
Definition: buf.h:973
void * alloc_data
Definition: buf.h:974
Definition: buf.h:965
uint8_t *(* ref)(struct net_buf *buf, uint8_t *data)
Definition: buf.h:968
uint8_t *(* alloc)(struct net_buf *buf, size_t *size, k_timeout_t timeout)
Definition: buf.h:966
void(* unref)(struct net_buf *buf, uint8_t *data)
Definition: buf.h:969
Definition: buf.h:1098
size_t data_size
Definition: buf.h:1099
uint8_t * data_pool
Definition: buf.h:1100
Network buffer pool representation.
Definition: buf.h:982
void(*const destroy)(struct net_buf *buf)
Definition: buf.h:1010
uint16_t uninit_count
Definition: buf.h:993
uint8_t user_data_size
Definition: buf.h:996
const uint16_t buf_count
Definition: buf.h:990
const struct net_buf_data_alloc * alloc
Definition: buf.h:1013
struct k_lifo free
Definition: buf.h:984
struct k_spinlock lock
Definition: buf.h:987
Parsing state of a buffer.
Definition: buf.h:850
uint16_t offset
Definition: buf.h:852
uint16_t len
Definition: buf.h:854
Simple network buffer representation.
Definition: buf.h:83
uint8_t * data
Definition: buf.h:85
uint16_t size
Definition: buf.h:95
uint16_t len
Definition: buf.h:92
Network buffer representation.
Definition: buf.h:915
uint16_t size
Definition: buf.h:949
struct net_buf * frags
Definition: buf.h:921
uint8_t ref
Definition: buf.h:925
uint8_t pool_id
Definition: buf.h:931
sys_snode_t node
Definition: buf.h:918
uint8_t user_data_size
Definition: buf.h:934
uint8_t flags
Definition: buf.h:928
uint8_t * data
Definition: buf.h:943
uint8_t user_data[]
Definition: buf.h:962
struct net_buf_simple b
Definition: buf.h:958
uint16_t len
Definition: buf.h:946
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
static const intptr_t user_data[5]
Definition: main.c:590
Misc utilities.