Zephyr API Documentation  3.5.0
A Scalable Open Source RTOS
3.5.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#define __net_buf_align __aligned(sizeof(void *))
32
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
897#define NET_BUF_EXTERNAL_DATA BIT(0)
898
906struct net_buf {
909
911 struct net_buf *frags;
912
915
918
921
922 /* Size of user data on this buffer */
924
925 /* Union for convenience access to the net_buf_simple members, also
926 * preserving the old API.
927 */
928 union {
929 /* The ABI of this struct must match net_buf_simple */
930 struct {
933
936
939
944 uint8_t *__buf;
945 };
946
948 };
949
951 uint8_t user_data[] __net_buf_align;
952};
953
955 uint8_t * __must_check (*alloc)(struct net_buf *buf, size_t *size,
956 k_timeout_t timeout);
957 uint8_t * __must_check (*ref)(struct net_buf *buf, uint8_t *data);
958 void (*unref)(struct net_buf *buf, uint8_t *data);
959};
960
962 const struct net_buf_data_cb *cb;
964};
965
973 struct k_lifo free;
974
975 /* to prevent concurrent access/modifications */
977
980
983
984 /* Size of user data allocated to this pool */
986
987#if defined(CONFIG_NET_BUF_POOL_USAGE)
989 atomic_t avail_count;
990
992 const uint16_t pool_size;
993
995 const char *name;
996#endif /* CONFIG_NET_BUF_POOL_USAGE */
997
999 void (*const destroy)(struct net_buf *buf);
1000
1003
1005 struct net_buf * const __bufs;
1006};
1007
1009#if defined(CONFIG_NET_BUF_POOL_USAGE)
1010#define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _ud_size, _destroy) \
1011 { \
1012 .free = Z_LIFO_INITIALIZER(_pool.free), \
1013 .lock = { }, \
1014 .buf_count = _count, \
1015 .uninit_count = _count, \
1016 .user_data_size = _ud_size, \
1017 .avail_count = ATOMIC_INIT(_count), \
1018 .name = STRINGIFY(_pool), \
1019 .destroy = _destroy, \
1020 .alloc = _alloc, \
1021 .__bufs = (struct net_buf *)_bufs, \
1022 }
1023#else
1024#define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _ud_size, _destroy) \
1025 { \
1026 .free = Z_LIFO_INITIALIZER(_pool.free), \
1027 .lock = { }, \
1028 .buf_count = _count, \
1029 .uninit_count = _count, \
1030 .user_data_size = _ud_size, \
1031 .destroy = _destroy, \
1032 .alloc = _alloc, \
1033 .__bufs = (struct net_buf *)_bufs, \
1034 }
1035#endif /* CONFIG_NET_BUF_POOL_USAGE */
1036
1037#define _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size) \
1038 struct _net_buf_##_name { uint8_t b[sizeof(struct net_buf)]; \
1039 uint8_t ud[_ud_size]; } __net_buf_align; \
1040 BUILD_ASSERT(_ud_size <= UINT8_MAX); \
1041 BUILD_ASSERT(offsetof(struct net_buf, user_data) == \
1042 offsetof(struct _net_buf_##_name, ud), "Invalid offset"); \
1043 BUILD_ASSERT(__alignof__(struct net_buf) == \
1044 __alignof__(struct _net_buf_##_name), "Invalid alignment"); \
1045 BUILD_ASSERT(sizeof(struct _net_buf_##_name) == \
1046 ROUND_UP(sizeof(struct net_buf) + _ud_size, __alignof__(struct net_buf)), \
1047 "Size cannot be determined"); \
1048 static struct _net_buf_##_name _net_buf_##_name[_count] __noinit
1049
1050extern const struct net_buf_data_alloc net_buf_heap_alloc;
1080#define NET_BUF_POOL_HEAP_DEFINE(_name, _count, _ud_size, _destroy) \
1081 _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size); \
1082 static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) = \
1083 NET_BUF_POOL_INITIALIZER(_name, &net_buf_heap_alloc, \
1084 _net_buf_##_name, _count, _ud_size, \
1085 _destroy)
1086
1090};
1091
1093extern const struct net_buf_data_cb net_buf_fixed_cb;
1124#define NET_BUF_POOL_FIXED_DEFINE(_name, _count, _data_size, _ud_size, _destroy) \
1125 _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size); \
1126 static uint8_t __noinit net_buf_data_##_name[_count][_data_size] __net_buf_align; \
1127 static const struct net_buf_pool_fixed net_buf_fixed_##_name = { \
1128 .data_size = _data_size, \
1129 .data_pool = (uint8_t *)net_buf_data_##_name, \
1130 }; \
1131 static const struct net_buf_data_alloc net_buf_fixed_alloc_##_name = { \
1132 .cb = &net_buf_fixed_cb, \
1133 .alloc_data = (void *)&net_buf_fixed_##_name, \
1134 }; \
1135 static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) = \
1136 NET_BUF_POOL_INITIALIZER(_name, &net_buf_fixed_alloc_##_name, \
1137 _net_buf_##_name, _count, _ud_size, \
1138 _destroy)
1139
1141extern const struct net_buf_data_cb net_buf_var_cb;
1168#define NET_BUF_POOL_VAR_DEFINE(_name, _count, _data_size, _ud_size, _destroy) \
1169 _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size); \
1170 K_HEAP_DEFINE(net_buf_mem_pool_##_name, _data_size); \
1171 static const struct net_buf_data_alloc net_buf_data_alloc_##_name = { \
1172 .cb = &net_buf_var_cb, \
1173 .alloc_data = &net_buf_mem_pool_##_name, \
1174 }; \
1175 static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) = \
1176 NET_BUF_POOL_INITIALIZER(_name, &net_buf_data_alloc_##_name, \
1177 _net_buf_##_name, _count, _ud_size, \
1178 _destroy)
1179
1201#define NET_BUF_POOL_DEFINE(_name, _count, _size, _ud_size, _destroy) \
1202 NET_BUF_POOL_FIXED_DEFINE(_name, _count, _size, _ud_size, _destroy)
1203
1212
1225int net_buf_id(struct net_buf *buf);
1226
1241#if defined(CONFIG_NET_BUF_LOG)
1242struct net_buf * __must_check net_buf_alloc_fixed_debug(struct net_buf_pool *pool,
1243 k_timeout_t timeout,
1244 const char *func,
1245 int line);
1246#define net_buf_alloc_fixed(_pool, _timeout) \
1247 net_buf_alloc_fixed_debug(_pool, _timeout, __func__, __LINE__)
1248#else
1249struct net_buf * __must_check net_buf_alloc_fixed(struct net_buf_pool *pool,
1250 k_timeout_t timeout);
1251#endif
1252
1256static inline struct net_buf * __must_check net_buf_alloc(struct net_buf_pool *pool,
1257 k_timeout_t timeout)
1258{
1259 return net_buf_alloc_fixed(pool, timeout);
1260}
1261
1277#if defined(CONFIG_NET_BUF_LOG)
1278struct net_buf * __must_check net_buf_alloc_len_debug(struct net_buf_pool *pool,
1279 size_t size,
1280 k_timeout_t timeout,
1281 const char *func,
1282 int line);
1283#define net_buf_alloc_len(_pool, _size, _timeout) \
1284 net_buf_alloc_len_debug(_pool, _size, _timeout, __func__, __LINE__)
1285#else
1286struct net_buf * __must_check net_buf_alloc_len(struct net_buf_pool *pool,
1287 size_t size,
1288 k_timeout_t timeout);
1289#endif
1290
1310#if defined(CONFIG_NET_BUF_LOG)
1311struct net_buf * __must_check net_buf_alloc_with_data_debug(struct net_buf_pool *pool,
1312 void *data, size_t size,
1313 k_timeout_t timeout,
1314 const char *func, int line);
1315#define net_buf_alloc_with_data(_pool, _data_, _size, _timeout) \
1316 net_buf_alloc_with_data_debug(_pool, _data_, _size, _timeout, \
1317 __func__, __LINE__)
1318#else
1319struct net_buf * __must_check net_buf_alloc_with_data(struct net_buf_pool *pool,
1320 void *data, size_t size,
1321 k_timeout_t timeout);
1322#endif
1323
1337#if defined(CONFIG_NET_BUF_LOG)
1338struct net_buf * __must_check net_buf_get_debug(struct k_fifo *fifo,
1339 k_timeout_t timeout,
1340 const char *func, int line);
1341#define net_buf_get(_fifo, _timeout) \
1342 net_buf_get_debug(_fifo, _timeout, __func__, __LINE__)
1343#else
1344struct net_buf * __must_check net_buf_get(struct k_fifo *fifo,
1345 k_timeout_t timeout);
1346#endif
1347
1357static inline void net_buf_destroy(struct net_buf *buf)
1358{
1359 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
1360
1361 k_lifo_put(&pool->free, buf);
1362}
1363
1371void net_buf_reset(struct net_buf *buf);
1372
1381void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve);
1382
1392void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf);
1393
1404struct net_buf * __must_check net_buf_slist_get(sys_slist_t *list);
1405
1415void net_buf_put(struct k_fifo *fifo, struct net_buf *buf);
1416
1424#if defined(CONFIG_NET_BUF_LOG)
1425void net_buf_unref_debug(struct net_buf *buf, const char *func, int line);
1426#define net_buf_unref(_buf) \
1427 net_buf_unref_debug(_buf, __func__, __LINE__)
1428#else
1429void net_buf_unref(struct net_buf *buf);
1430#endif
1431
1439struct net_buf * __must_check net_buf_ref(struct net_buf *buf);
1440
1454struct net_buf * __must_check net_buf_clone(struct net_buf *buf,
1455 k_timeout_t timeout);
1456
1464static inline void * __must_check net_buf_user_data(const struct net_buf *buf)
1465{
1466 return (void *)buf->user_data;
1467}
1468
1477static inline void net_buf_reserve(struct net_buf *buf, size_t reserve)
1478{
1479 net_buf_simple_reserve(&buf->b, reserve);
1480}
1481
1493static inline void *net_buf_add(struct net_buf *buf, size_t len)
1494{
1495 return net_buf_simple_add(&buf->b, len);
1496}
1497
1510static inline void *net_buf_add_mem(struct net_buf *buf, const void *mem,
1511 size_t len)
1512{
1513 return net_buf_simple_add_mem(&buf->b, mem, len);
1514}
1515
1527static inline uint8_t *net_buf_add_u8(struct net_buf *buf, uint8_t val)
1528{
1529 return net_buf_simple_add_u8(&buf->b, val);
1530}
1531
1542static inline void net_buf_add_le16(struct net_buf *buf, uint16_t val)
1543{
1544 net_buf_simple_add_le16(&buf->b, val);
1545}
1546
1557static inline void net_buf_add_be16(struct net_buf *buf, uint16_t val)
1558{
1559 net_buf_simple_add_be16(&buf->b, val);
1560}
1561
1572static inline void net_buf_add_le24(struct net_buf *buf, uint32_t val)
1573{
1574 net_buf_simple_add_le24(&buf->b, val);
1575}
1576
1587static inline void net_buf_add_be24(struct net_buf *buf, uint32_t val)
1588{
1589 net_buf_simple_add_be24(&buf->b, val);
1590}
1591
1602static inline void net_buf_add_le32(struct net_buf *buf, uint32_t val)
1603{
1604 net_buf_simple_add_le32(&buf->b, val);
1605}
1606
1617static inline void net_buf_add_be32(struct net_buf *buf, uint32_t val)
1618{
1619 net_buf_simple_add_be32(&buf->b, val);
1620}
1621
1632static inline void net_buf_add_le48(struct net_buf *buf, uint64_t val)
1633{
1634 net_buf_simple_add_le48(&buf->b, val);
1635}
1636
1647static inline void net_buf_add_be48(struct net_buf *buf, uint64_t val)
1648{
1649 net_buf_simple_add_be48(&buf->b, val);
1650}
1651
1662static inline void net_buf_add_le64(struct net_buf *buf, uint64_t val)
1663{
1664 net_buf_simple_add_le64(&buf->b, val);
1665}
1666
1677static inline void net_buf_add_be64(struct net_buf *buf, uint64_t val)
1678{
1679 net_buf_simple_add_be64(&buf->b, val);
1680}
1681
1692static inline void *net_buf_remove_mem(struct net_buf *buf, size_t len)
1693{
1694 return net_buf_simple_remove_mem(&buf->b, len);
1695}
1696
1707static inline uint8_t net_buf_remove_u8(struct net_buf *buf)
1708{
1709 return net_buf_simple_remove_u8(&buf->b);
1710}
1711
1722static inline uint16_t net_buf_remove_le16(struct net_buf *buf)
1723{
1724 return net_buf_simple_remove_le16(&buf->b);
1725}
1726
1737static inline uint16_t net_buf_remove_be16(struct net_buf *buf)
1738{
1739 return net_buf_simple_remove_be16(&buf->b);
1740}
1741
1752static inline uint32_t net_buf_remove_be24(struct net_buf *buf)
1753{
1754 return net_buf_simple_remove_be24(&buf->b);
1755}
1756
1767static inline uint32_t net_buf_remove_le24(struct net_buf *buf)
1768{
1769 return net_buf_simple_remove_le24(&buf->b);
1770}
1771
1782static inline uint32_t net_buf_remove_le32(struct net_buf *buf)
1783{
1784 return net_buf_simple_remove_le32(&buf->b);
1785}
1786
1797static inline uint32_t net_buf_remove_be32(struct net_buf *buf)
1798{
1799 return net_buf_simple_remove_be32(&buf->b);
1800}
1801
1812static inline uint64_t net_buf_remove_le48(struct net_buf *buf)
1813{
1814 return net_buf_simple_remove_le48(&buf->b);
1815}
1816
1827static inline uint64_t net_buf_remove_be48(struct net_buf *buf)
1828{
1829 return net_buf_simple_remove_be48(&buf->b);
1830}
1831
1842static inline uint64_t net_buf_remove_le64(struct net_buf *buf)
1843{
1844 return net_buf_simple_remove_le64(&buf->b);
1845}
1846
1857static inline uint64_t net_buf_remove_be64(struct net_buf *buf)
1858{
1859 return net_buf_simple_remove_be64(&buf->b);
1860}
1861
1873static inline void *net_buf_push(struct net_buf *buf, size_t len)
1874{
1875 return net_buf_simple_push(&buf->b, len);
1876}
1877
1890static inline void *net_buf_push_mem(struct net_buf *buf, const void *mem,
1891 size_t len)
1892{
1893 return net_buf_simple_push_mem(&buf->b, mem, len);
1894}
1895
1904static inline void net_buf_push_u8(struct net_buf *buf, uint8_t val)
1905{
1906 net_buf_simple_push_u8(&buf->b, val);
1907}
1908
1918static inline void net_buf_push_le16(struct net_buf *buf, uint16_t val)
1919{
1920 net_buf_simple_push_le16(&buf->b, val);
1921}
1922
1932static inline void net_buf_push_be16(struct net_buf *buf, uint16_t val)
1933{
1934 net_buf_simple_push_be16(&buf->b, val);
1935}
1936
1946static inline void net_buf_push_le24(struct net_buf *buf, uint32_t val)
1947{
1948 net_buf_simple_push_le24(&buf->b, val);
1949}
1950
1960static inline void net_buf_push_be24(struct net_buf *buf, uint32_t val)
1961{
1962 net_buf_simple_push_be24(&buf->b, val);
1963}
1964
1974static inline void net_buf_push_le32(struct net_buf *buf, uint32_t val)
1975{
1976 net_buf_simple_push_le32(&buf->b, val);
1977}
1978
1988static inline void net_buf_push_be32(struct net_buf *buf, uint32_t val)
1989{
1990 net_buf_simple_push_be32(&buf->b, val);
1991}
1992
2002static inline void net_buf_push_le48(struct net_buf *buf, uint64_t val)
2003{
2004 net_buf_simple_push_le48(&buf->b, val);
2005}
2006
2016static inline void net_buf_push_be48(struct net_buf *buf, uint64_t val)
2017{
2018 net_buf_simple_push_be48(&buf->b, val);
2019}
2020
2030static inline void net_buf_push_le64(struct net_buf *buf, uint64_t val)
2031{
2032 net_buf_simple_push_le64(&buf->b, val);
2033}
2034
2044static inline void net_buf_push_be64(struct net_buf *buf, uint64_t val)
2045{
2046 net_buf_simple_push_be64(&buf->b, val);
2047}
2048
2060static inline void *net_buf_pull(struct net_buf *buf, size_t len)
2061{
2062 return net_buf_simple_pull(&buf->b, len);
2063}
2064
2076static inline void *net_buf_pull_mem(struct net_buf *buf, size_t len)
2077{
2078 return net_buf_simple_pull_mem(&buf->b, len);
2079}
2080
2091static inline uint8_t net_buf_pull_u8(struct net_buf *buf)
2092{
2093 return net_buf_simple_pull_u8(&buf->b);
2094}
2095
2106static inline uint16_t net_buf_pull_le16(struct net_buf *buf)
2107{
2108 return net_buf_simple_pull_le16(&buf->b);
2109}
2110
2121static inline uint16_t net_buf_pull_be16(struct net_buf *buf)
2122{
2123 return net_buf_simple_pull_be16(&buf->b);
2124}
2125
2136static inline uint32_t net_buf_pull_le24(struct net_buf *buf)
2137{
2138 return net_buf_simple_pull_le24(&buf->b);
2139}
2140
2151static inline uint32_t net_buf_pull_be24(struct net_buf *buf)
2152{
2153 return net_buf_simple_pull_be24(&buf->b);
2154}
2155
2166static inline uint32_t net_buf_pull_le32(struct net_buf *buf)
2167{
2168 return net_buf_simple_pull_le32(&buf->b);
2169}
2170
2181static inline uint32_t net_buf_pull_be32(struct net_buf *buf)
2182{
2183 return net_buf_simple_pull_be32(&buf->b);
2184}
2185
2196static inline uint64_t net_buf_pull_le48(struct net_buf *buf)
2197{
2198 return net_buf_simple_pull_le48(&buf->b);
2199}
2200
2211static inline uint64_t net_buf_pull_be48(struct net_buf *buf)
2212{
2213 return net_buf_simple_pull_be48(&buf->b);
2214}
2215
2226static inline uint64_t net_buf_pull_le64(struct net_buf *buf)
2227{
2228 return net_buf_simple_pull_le64(&buf->b);
2229}
2230
2241static inline uint64_t net_buf_pull_be64(struct net_buf *buf)
2242{
2243 return net_buf_simple_pull_be64(&buf->b);
2244}
2245
2255static inline size_t net_buf_tailroom(struct net_buf *buf)
2256{
2257 return net_buf_simple_tailroom(&buf->b);
2258}
2259
2269static inline size_t net_buf_headroom(struct net_buf *buf)
2270{
2271 return net_buf_simple_headroom(&buf->b);
2272}
2273
2283static inline uint16_t net_buf_max_len(struct net_buf *buf)
2284{
2285 return net_buf_simple_max_len(&buf->b);
2286}
2287
2297static inline uint8_t *net_buf_tail(struct net_buf *buf)
2298{
2299 return net_buf_simple_tail(&buf->b);
2300}
2301
2308
2320void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag);
2321
2336struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag);
2337
2347#if defined(CONFIG_NET_BUF_LOG)
2348struct net_buf *net_buf_frag_del_debug(struct net_buf *parent,
2349 struct net_buf *frag,
2350 const char *func, int line);
2351#define net_buf_frag_del(_parent, _frag) \
2352 net_buf_frag_del_debug(_parent, _frag, __func__, __LINE__)
2353#else
2354struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag);
2355#endif
2356
2372size_t net_buf_linearize(void *dst, size_t dst_len,
2373 struct net_buf *src, size_t offset, size_t len);
2374
2389typedef struct net_buf * __must_check (*net_buf_allocator_cb)(k_timeout_t timeout,
2390 void *user_data);
2391
2413size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
2414 const void *value, k_timeout_t timeout,
2415 net_buf_allocator_cb allocate_cb, void *user_data);
2416
2432static inline struct net_buf *net_buf_skip(struct net_buf *buf, size_t len)
2433{
2434 while (buf && len--) {
2435 net_buf_pull_u8(buf);
2436 if (!buf->len) {
2437 buf = net_buf_frag_del(NULL, buf);
2438 }
2439 }
2440
2441 return buf;
2442}
2443
2454static inline size_t net_buf_frags_len(struct net_buf *buf)
2455{
2456 size_t bytes = 0;
2457
2458 while (buf) {
2459 bytes += buf->len;
2460 buf = buf->frags;
2461 }
2462
2463 return bytes;
2464}
2465
2470#ifdef __cplusplus
2471}
2472#endif
2473
2474#endif /* ZEPHYR_INCLUDE_NET_BUF_H_ */
long atomic_t
Definition: atomic.h:22
#define k_lifo_put(lifo, data)
Add an element to a LIFO queue.
Definition: kernel.h:2678
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: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:1677
static size_t net_buf_headroom(struct net_buf *buf)
Check buffer headroom.
Definition: buf.h:2269
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:1812
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:1587
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:2389
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:2432
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:1493
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:1572
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:2241
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:2044
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:1842
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:1256
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:1617
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:1737
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:2166
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:2181
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:2255
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:1557
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:2211
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:1932
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:1752
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:2091
static void net_buf_destroy(struct net_buf *buf)
Destroy buffer from custom destroy callback.
Definition: buf.h:1357
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:1890
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:2196
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:2002
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:2136
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:1527
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:1960
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:1946
static void net_buf_reserve(struct net_buf *buf, size_t reserve)
Initialize buffer with the given headroom.
Definition: buf.h:1477
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:1857
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:2151
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:1647
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:1767
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:1904
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:1873
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:2121
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:1974
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:2226
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:1827
static uint16_t net_buf_max_len(struct net_buf *buf)
Check maximum net_buf::len value.
Definition: buf.h:2283
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:1722
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:1918
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:2016
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:1662
static uint8_t * net_buf_tail(struct net_buf *buf)
Get the tail pointer for a buffer.
Definition: buf.h:2297
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:1797
static void * net_buf_remove_mem(struct net_buf *buf, size_t len)
Remove data from the end of the buffer.
Definition: buf.h:1692
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:1510
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:2030
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:1707
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:1542
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:1988
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:1602
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:1782
static size_t net_buf_frags_len(struct net_buf *buf)
Calculate amount of bytes stored in fragments.
Definition: buf.h:2454
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:2106
static void * net_buf_pull_mem(struct net_buf *buf, size_t len)
Remove data from the beginning of the buffer.
Definition: buf.h:2076
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:2060
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:1632
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:1464
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:2385
Definition: kernel.h:2624
Kernel Spin Lock.
Definition: spinlock.h:45
Kernel timeout type.
Definition: sys_clock.h:65
Definition: buf.h:961
const struct net_buf_data_cb * cb
Definition: buf.h:962
void * alloc_data
Definition: buf.h:963
Definition: buf.h:954
uint8_t *(* ref)(struct net_buf *buf, uint8_t *data)
Definition: buf.h:957
uint8_t *(* alloc)(struct net_buf *buf, size_t *size, k_timeout_t timeout)
Definition: buf.h:955
void(* unref)(struct net_buf *buf, uint8_t *data)
Definition: buf.h:958
Definition: buf.h:1087
size_t data_size
Definition: buf.h:1088
uint8_t * data_pool
Definition: buf.h:1089
Network buffer pool representation.
Definition: buf.h:971
void(*const destroy)(struct net_buf *buf)
Optional destroy callback when buffer is freed.
Definition: buf.h:999
uint16_t uninit_count
Number of uninitialized buffers.
Definition: buf.h:982
uint8_t user_data_size
Definition: buf.h:985
const uint16_t buf_count
Number of buffers in pool.
Definition: buf.h:979
const struct net_buf_data_alloc * alloc
Data allocation handlers.
Definition: buf.h:1002
struct k_lifo free
LIFO to place the buffer into when free.
Definition: buf.h:973
struct k_spinlock lock
Definition: buf.h:976
Parsing state of a buffer.
Definition: buf.h:850
uint16_t offset
Offset of the data pointer from the beginning of the storage.
Definition: buf.h:852
uint16_t len
Length of data.
Definition: buf.h:854
Simple network buffer representation.
Definition: buf.h:83
uint8_t * data
Pointer to the start of data in the buffer.
Definition: buf.h:85
uint16_t size
Amount of data that net_buf_simple::__buf can store.
Definition: buf.h:95
uint16_t len
Length of the data behind the data pointer.
Definition: buf.h:92
Network buffer representation.
Definition: buf.h:906
uint16_t size
Amount of data that this buffer can store.
Definition: buf.h:938
struct net_buf * frags
Fragments associated with this buffer.
Definition: buf.h:911
uint8_t ref
Reference count.
Definition: buf.h:914
uint8_t pool_id
Where the buffer should go when freed up.
Definition: buf.h:920
sys_snode_t node
Allow placing the buffer into sys_slist_t.
Definition: buf.h:908
uint8_t user_data_size
Definition: buf.h:923
uint8_t flags
Bit-field of buffer flags.
Definition: buf.h:917
uint8_t * data
Pointer to the start of data in the buffer.
Definition: buf.h:932
uint8_t user_data[]
System metadata for this buffer.
Definition: buf.h:951
struct net_buf_simple b
Definition: buf.h:947
uint16_t len
Length of the data behind the data pointer.
Definition: buf.h:935
Misc utilities.