Zephyr Project API  3.2.0
A Scalable Open Source RTOS
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>
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
41#define NET_BUF_SIMPLE_DEFINE(_name, _size) \
42 uint8_t net_buf_data_##_name[_size]; \
43 struct net_buf_simple _name = { \
44 .data = net_buf_data_##_name, \
45 .len = 0, \
46 .size = _size, \
47 .__buf = net_buf_data_##_name, \
48 }
49
60#define NET_BUF_SIMPLE_DEFINE_STATIC(_name, _size) \
61 static __noinit uint8_t net_buf_data_##_name[_size]; \
62 static struct net_buf_simple _name = { \
63 .data = net_buf_data_##_name, \
64 .len = 0, \
65 .size = _size, \
66 .__buf = net_buf_data_##_name, \
67 }
68
85
92
95
99 uint8_t *__buf;
100};
101
118#define NET_BUF_SIMPLE(_size) \
119 ((struct net_buf_simple *)(&(struct { \
120 struct net_buf_simple buf; \
121 uint8_t data[_size]; \
122 }) { \
123 .buf.size = _size, \
124 }))
125
135static inline void net_buf_simple_init(struct net_buf_simple *buf,
136 size_t reserve_head)
137{
138 if (!buf->__buf) {
139 buf->__buf = (uint8_t *)buf + sizeof(*buf);
140 }
141
142 buf->data = buf->__buf + reserve_head;
143 buf->len = 0U;
144}
145
156 void *data, size_t size);
157
165static inline void net_buf_simple_reset(struct net_buf_simple *buf)
166{
167 buf->len = 0U;
168 buf->data = buf->__buf;
169}
170
181void net_buf_simple_clone(const struct net_buf_simple *original,
182 struct net_buf_simple *clone);
183
195void *net_buf_simple_add(struct net_buf_simple *buf, size_t len);
196
209void *net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem,
210 size_t len);
211
224
236
248
260
272
284
296
308
320
332
344
355void *net_buf_simple_remove_mem(struct net_buf_simple *buf, size_t len);
356
368
380
392
404
416
428
440
452
464
476
488
500void *net_buf_simple_push(struct net_buf_simple *buf, size_t len);
501
514void *net_buf_simple_push_mem(struct net_buf_simple *buf, const void *mem,
515 size_t len);
516
527
538
548
559
570
581
592
603
614
625
636
648void *net_buf_simple_pull(struct net_buf_simple *buf, size_t len);
649
661void *net_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len);
662
674
686
698
710
722
734
746
758
770
782
794
804static inline uint8_t *net_buf_simple_tail(struct net_buf_simple *buf)
805{
806 return buf->data + buf->len;
807}
808
819
830
841
854};
855
864static inline void net_buf_simple_save(struct net_buf_simple *buf,
866{
867 state->offset = net_buf_simple_headroom(buf);
868 state->len = buf->len;
869}
870
880static inline void net_buf_simple_restore(struct net_buf_simple *buf,
882{
883 buf->data = buf->__buf + state->offset;
884 buf->len = state->len;
885}
886
895#define NET_BUF_FRAGS BIT(0)
905#define NET_BUF_EXTERNAL_DATA BIT(1)
906
914struct net_buf {
915 union {
918
920 struct net_buf *frags;
921 };
922
925
928
931
932 /* Size of user data on this buffer */
934
935 /* Union for convenience access to the net_buf_simple members, also
936 * preserving the old API.
937 */
938 union {
939 /* The ABI of this struct must match net_buf_simple */
940 struct {
943
946
949
954 uint8_t *__buf;
955 };
956
958 };
959
961 uint8_t user_data[] __net_buf_align;
962};
963
965 uint8_t * __must_check (*alloc)(struct net_buf *buf, size_t *size,
967 uint8_t * __must_check (*ref)(struct net_buf *buf, uint8_t *data);
968 void (*unref)(struct net_buf *buf, uint8_t *data);
969};
970
972 const struct net_buf_data_cb *cb;
974};
975
983 struct k_lifo free;
984
985 /* to prevent concurrent access/modifications */
987
990
993
994 /* Size of user data allocated to this pool */
996
997#if defined(CONFIG_NET_BUF_POOL_USAGE)
999 atomic_t avail_count;
1000
1002 const uint16_t pool_size;
1003
1005 const char *name;
1006#endif /* CONFIG_NET_BUF_POOL_USAGE */
1007
1009 void (*const destroy)(struct net_buf *buf);
1010
1013
1015 struct net_buf * const __bufs;
1016};
1017
1019#if defined(CONFIG_NET_BUF_POOL_USAGE)
1020#define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _ud_size, _destroy) \
1021 { \
1022 .free = Z_LIFO_INITIALIZER(_pool.free), \
1023 .lock = { }, \
1024 .buf_count = _count, \
1025 .uninit_count = _count, \
1026 .user_data_size = _ud_size, \
1027 .avail_count = ATOMIC_INIT(_count), \
1028 .name = STRINGIFY(_pool), \
1029 .destroy = _destroy, \
1030 .alloc = _alloc, \
1031 .__bufs = (struct net_buf *)_bufs, \
1032 }
1033#else
1034#define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _ud_size, _destroy) \
1035 { \
1036 .free = Z_LIFO_INITIALIZER(_pool.free), \
1037 .lock = { }, \
1038 .buf_count = _count, \
1039 .uninit_count = _count, \
1040 .user_data_size = _ud_size, \
1041 .destroy = _destroy, \
1042 .alloc = _alloc, \
1043 .__bufs = (struct net_buf *)_bufs, \
1044 }
1045#endif /* CONFIG_NET_BUF_POOL_USAGE */
1046
1047#define _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size) \
1048 struct _net_buf_##_name { uint8_t b[sizeof(struct net_buf)]; \
1049 uint8_t ud[_ud_size]; } __net_buf_align; \
1050 BUILD_ASSERT(_ud_size <= UINT8_MAX); \
1051 BUILD_ASSERT(offsetof(struct net_buf, user_data) == \
1052 offsetof(struct _net_buf_##_name, ud), "Invalid offset"); \
1053 BUILD_ASSERT(__alignof__(struct net_buf) == \
1054 __alignof__(struct _net_buf_##_name), "Invalid alignment"); \
1055 BUILD_ASSERT(sizeof(struct _net_buf_##_name) == \
1056 ROUND_UP(sizeof(struct net_buf) + _ud_size, __alignof__(struct net_buf)), \
1057 "Size cannot be determined"); \
1058 static struct _net_buf_##_name _net_buf_##_name[_count] __noinit
1059
1060extern const struct net_buf_data_alloc net_buf_heap_alloc;
1090#define NET_BUF_POOL_HEAP_DEFINE(_name, _count, _ud_size, _destroy) \
1091 _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size); \
1092 static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) = \
1093 NET_BUF_POOL_INITIALIZER(_name, &net_buf_heap_alloc, \
1094 _net_buf_##_name, _count, _ud_size, \
1095 _destroy)
1096
1100};
1101
1103extern const struct net_buf_data_cb net_buf_fixed_cb;
1134#define NET_BUF_POOL_FIXED_DEFINE(_name, _count, _data_size, _ud_size, _destroy) \
1135 _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size); \
1136 static uint8_t __noinit net_buf_data_##_name[_count][_data_size] __net_buf_align; \
1137 static const struct net_buf_pool_fixed net_buf_fixed_##_name = { \
1138 .data_size = _data_size, \
1139 .data_pool = (uint8_t *)net_buf_data_##_name, \
1140 }; \
1141 static const struct net_buf_data_alloc net_buf_fixed_alloc_##_name = { \
1142 .cb = &net_buf_fixed_cb, \
1143 .alloc_data = (void *)&net_buf_fixed_##_name, \
1144 }; \
1145 static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) = \
1146 NET_BUF_POOL_INITIALIZER(_name, &net_buf_fixed_alloc_##_name, \
1147 _net_buf_##_name, _count, _ud_size, \
1148 _destroy)
1149
1151extern const struct net_buf_data_cb net_buf_var_cb;
1178#define NET_BUF_POOL_VAR_DEFINE(_name, _count, _data_size, _ud_size, _destroy) \
1179 _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size); \
1180 K_HEAP_DEFINE(net_buf_mem_pool_##_name, _data_size); \
1181 static const struct net_buf_data_alloc net_buf_data_alloc_##_name = { \
1182 .cb = &net_buf_var_cb, \
1183 .alloc_data = &net_buf_mem_pool_##_name, \
1184 }; \
1185 static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) = \
1186 NET_BUF_POOL_INITIALIZER(_name, &net_buf_data_alloc_##_name, \
1187 _net_buf_##_name, _count, _ud_size, \
1188 _destroy)
1189
1211#define NET_BUF_POOL_DEFINE(_name, _count, _size, _ud_size, _destroy) \
1212 NET_BUF_POOL_FIXED_DEFINE(_name, _count, _size, _ud_size, _destroy)
1213
1222
1235int net_buf_id(struct net_buf *buf);
1236
1251#if defined(CONFIG_NET_BUF_LOG)
1252struct net_buf * __must_check net_buf_alloc_fixed_debug(struct net_buf_pool *pool,
1254 const char *func,
1255 int line);
1256#define net_buf_alloc_fixed(_pool, _timeout) \
1257 net_buf_alloc_fixed_debug(_pool, _timeout, __func__, __LINE__)
1258#else
1259struct net_buf * __must_check net_buf_alloc_fixed(struct net_buf_pool *pool,
1261#endif
1262
1266static inline struct net_buf * __must_check net_buf_alloc(struct net_buf_pool *pool,
1268{
1269 return net_buf_alloc_fixed(pool, timeout);
1270}
1271
1287#if defined(CONFIG_NET_BUF_LOG)
1288struct net_buf * __must_check net_buf_alloc_len_debug(struct net_buf_pool *pool,
1289 size_t size,
1291 const char *func,
1292 int line);
1293#define net_buf_alloc_len(_pool, _size, _timeout) \
1294 net_buf_alloc_len_debug(_pool, _size, _timeout, __func__, __LINE__)
1295#else
1296struct net_buf * __must_check net_buf_alloc_len(struct net_buf_pool *pool,
1297 size_t size,
1299#endif
1300
1320#if defined(CONFIG_NET_BUF_LOG)
1321struct net_buf * __must_check net_buf_alloc_with_data_debug(struct net_buf_pool *pool,
1322 void *data, size_t size,
1324 const char *func, int line);
1325#define net_buf_alloc_with_data(_pool, _data_, _size, _timeout) \
1326 net_buf_alloc_with_data_debug(_pool, _data_, _size, _timeout, \
1327 __func__, __LINE__)
1328#else
1329struct net_buf * __must_check net_buf_alloc_with_data(struct net_buf_pool *pool,
1330 void *data, size_t size,
1332#endif
1333
1347#if defined(CONFIG_NET_BUF_LOG)
1348struct net_buf * __must_check net_buf_get_debug(struct k_fifo *fifo,
1350 const char *func, int line);
1351#define net_buf_get(_fifo, _timeout) \
1352 net_buf_get_debug(_fifo, _timeout, __func__, __LINE__)
1353#else
1354struct net_buf * __must_check net_buf_get(struct k_fifo *fifo,
1356#endif
1357
1367static inline void net_buf_destroy(struct net_buf *buf)
1368{
1369 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
1370
1371 k_lifo_put(&pool->free, buf);
1372}
1373
1381void net_buf_reset(struct net_buf *buf);
1382
1391void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve);
1392
1402void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf);
1403
1414struct net_buf * __must_check net_buf_slist_get(sys_slist_t *list);
1415
1425void net_buf_put(struct k_fifo *fifo, struct net_buf *buf);
1426
1434#if defined(CONFIG_NET_BUF_LOG)
1435void net_buf_unref_debug(struct net_buf *buf, const char *func, int line);
1436#define net_buf_unref(_buf) \
1437 net_buf_unref_debug(_buf, __func__, __LINE__)
1438#else
1439void net_buf_unref(struct net_buf *buf);
1440#endif
1441
1449struct net_buf * __must_check net_buf_ref(struct net_buf *buf);
1450
1464struct net_buf * __must_check net_buf_clone(struct net_buf *buf,
1466
1474static inline void * __must_check net_buf_user_data(const struct net_buf *buf)
1475{
1476 return (void *)buf->user_data;
1477}
1478
1487static inline void net_buf_reserve(struct net_buf *buf, size_t reserve)
1488{
1489 net_buf_simple_reserve(&buf->b, reserve);
1490}
1491
1503static inline void *net_buf_add(struct net_buf *buf, size_t len)
1504{
1505 return net_buf_simple_add(&buf->b, len);
1506}
1507
1520static inline void *net_buf_add_mem(struct net_buf *buf, const void *mem,
1521 size_t len)
1522{
1523 return net_buf_simple_add_mem(&buf->b, mem, len);
1524}
1525
1537static inline uint8_t *net_buf_add_u8(struct net_buf *buf, uint8_t val)
1538{
1539 return net_buf_simple_add_u8(&buf->b, val);
1540}
1541
1552static inline void net_buf_add_le16(struct net_buf *buf, uint16_t val)
1553{
1554 net_buf_simple_add_le16(&buf->b, val);
1555}
1556
1567static inline void net_buf_add_be16(struct net_buf *buf, uint16_t val)
1568{
1569 net_buf_simple_add_be16(&buf->b, val);
1570}
1571
1582static inline void net_buf_add_le24(struct net_buf *buf, uint32_t val)
1583{
1584 net_buf_simple_add_le24(&buf->b, val);
1585}
1586
1597static inline void net_buf_add_be24(struct net_buf *buf, uint32_t val)
1598{
1599 net_buf_simple_add_be24(&buf->b, val);
1600}
1601
1612static inline void net_buf_add_le32(struct net_buf *buf, uint32_t val)
1613{
1614 net_buf_simple_add_le32(&buf->b, val);
1615}
1616
1627static inline void net_buf_add_be32(struct net_buf *buf, uint32_t val)
1628{
1629 net_buf_simple_add_be32(&buf->b, val);
1630}
1631
1642static inline void net_buf_add_le48(struct net_buf *buf, uint64_t val)
1643{
1644 net_buf_simple_add_le48(&buf->b, val);
1645}
1646
1657static inline void net_buf_add_be48(struct net_buf *buf, uint64_t val)
1658{
1659 net_buf_simple_add_be48(&buf->b, val);
1660}
1661
1672static inline void net_buf_add_le64(struct net_buf *buf, uint64_t val)
1673{
1674 net_buf_simple_add_le64(&buf->b, val);
1675}
1676
1687static inline void net_buf_add_be64(struct net_buf *buf, uint64_t val)
1688{
1689 net_buf_simple_add_be64(&buf->b, val);
1690}
1691
1702static inline void *net_buf_remove_mem(struct net_buf *buf, size_t len)
1703{
1704 return net_buf_simple_remove_mem(&buf->b, len);
1705}
1706
1717static inline uint8_t net_buf_remove_u8(struct net_buf *buf)
1718{
1719 return net_buf_simple_remove_u8(&buf->b);
1720}
1721
1732static inline uint16_t net_buf_remove_le16(struct net_buf *buf)
1733{
1734 return net_buf_simple_remove_le16(&buf->b);
1735}
1736
1747static inline uint16_t net_buf_remove_be16(struct net_buf *buf)
1748{
1749 return net_buf_simple_remove_be16(&buf->b);
1750}
1751
1762static inline uint32_t net_buf_remove_be24(struct net_buf *buf)
1763{
1764 return net_buf_simple_remove_be24(&buf->b);
1765}
1766
1777static inline uint32_t net_buf_remove_le24(struct net_buf *buf)
1778{
1779 return net_buf_simple_remove_le24(&buf->b);
1780}
1781
1792static inline uint32_t net_buf_remove_le32(struct net_buf *buf)
1793{
1794 return net_buf_simple_remove_le32(&buf->b);
1795}
1796
1807static inline uint32_t net_buf_remove_be32(struct net_buf *buf)
1808{
1809 return net_buf_simple_remove_be32(&buf->b);
1810}
1811
1822static inline uint64_t net_buf_remove_le48(struct net_buf *buf)
1823{
1824 return net_buf_simple_remove_le48(&buf->b);
1825}
1826
1837static inline uint64_t net_buf_remove_be48(struct net_buf *buf)
1838{
1839 return net_buf_simple_remove_be48(&buf->b);
1840}
1841
1852static inline uint64_t net_buf_remove_le64(struct net_buf *buf)
1853{
1854 return net_buf_simple_remove_le64(&buf->b);
1855}
1856
1867static inline uint64_t net_buf_remove_be64(struct net_buf *buf)
1868{
1869 return net_buf_simple_remove_be64(&buf->b);
1870}
1871
1883static inline void *net_buf_push(struct net_buf *buf, size_t len)
1884{
1885 return net_buf_simple_push(&buf->b, len);
1886}
1887
1900static inline void *net_buf_push_mem(struct net_buf *buf, const void *mem,
1901 size_t len)
1902{
1903 return net_buf_simple_push_mem(&buf->b, mem, len);
1904}
1905
1914static inline void net_buf_push_u8(struct net_buf *buf, uint8_t val)
1915{
1916 net_buf_simple_push_u8(&buf->b, val);
1917}
1918
1928static inline void net_buf_push_le16(struct net_buf *buf, uint16_t val)
1929{
1930 net_buf_simple_push_le16(&buf->b, val);
1931}
1932
1942static inline void net_buf_push_be16(struct net_buf *buf, uint16_t val)
1943{
1944 net_buf_simple_push_be16(&buf->b, val);
1945}
1946
1956static inline void net_buf_push_le24(struct net_buf *buf, uint32_t val)
1957{
1958 net_buf_simple_push_le24(&buf->b, val);
1959}
1960
1970static inline void net_buf_push_be24(struct net_buf *buf, uint32_t val)
1971{
1972 net_buf_simple_push_be24(&buf->b, val);
1973}
1974
1984static inline void net_buf_push_le32(struct net_buf *buf, uint32_t val)
1985{
1986 net_buf_simple_push_le32(&buf->b, val);
1987}
1988
1998static inline void net_buf_push_be32(struct net_buf *buf, uint32_t val)
1999{
2000 net_buf_simple_push_be32(&buf->b, val);
2001}
2002
2012static inline void net_buf_push_le48(struct net_buf *buf, uint64_t val)
2013{
2014 net_buf_simple_push_le48(&buf->b, val);
2015}
2016
2026static inline void net_buf_push_be48(struct net_buf *buf, uint64_t val)
2027{
2028 net_buf_simple_push_be48(&buf->b, val);
2029}
2030
2040static inline void net_buf_push_le64(struct net_buf *buf, uint64_t val)
2041{
2042 net_buf_simple_push_le64(&buf->b, val);
2043}
2044
2054static inline void net_buf_push_be64(struct net_buf *buf, uint64_t val)
2055{
2056 net_buf_simple_push_be64(&buf->b, val);
2057}
2058
2070static inline void *net_buf_pull(struct net_buf *buf, size_t len)
2071{
2072 return net_buf_simple_pull(&buf->b, len);
2073}
2074
2086static inline void *net_buf_pull_mem(struct net_buf *buf, size_t len)
2087{
2088 return net_buf_simple_pull_mem(&buf->b, len);
2089}
2090
2101static inline uint8_t net_buf_pull_u8(struct net_buf *buf)
2102{
2103 return net_buf_simple_pull_u8(&buf->b);
2104}
2105
2116static inline uint16_t net_buf_pull_le16(struct net_buf *buf)
2117{
2118 return net_buf_simple_pull_le16(&buf->b);
2119}
2120
2131static inline uint16_t net_buf_pull_be16(struct net_buf *buf)
2132{
2133 return net_buf_simple_pull_be16(&buf->b);
2134}
2135
2146static inline uint32_t net_buf_pull_le24(struct net_buf *buf)
2147{
2148 return net_buf_simple_pull_le24(&buf->b);
2149}
2150
2161static inline uint32_t net_buf_pull_be24(struct net_buf *buf)
2162{
2163 return net_buf_simple_pull_be24(&buf->b);
2164}
2165
2176static inline uint32_t net_buf_pull_le32(struct net_buf *buf)
2177{
2178 return net_buf_simple_pull_le32(&buf->b);
2179}
2180
2191static inline uint32_t net_buf_pull_be32(struct net_buf *buf)
2192{
2193 return net_buf_simple_pull_be32(&buf->b);
2194}
2195
2206static inline uint64_t net_buf_pull_le48(struct net_buf *buf)
2207{
2208 return net_buf_simple_pull_le48(&buf->b);
2209}
2210
2221static inline uint64_t net_buf_pull_be48(struct net_buf *buf)
2222{
2223 return net_buf_simple_pull_be48(&buf->b);
2224}
2225
2236static inline uint64_t net_buf_pull_le64(struct net_buf *buf)
2237{
2238 return net_buf_simple_pull_le64(&buf->b);
2239}
2240
2251static inline uint64_t net_buf_pull_be64(struct net_buf *buf)
2252{
2253 return net_buf_simple_pull_be64(&buf->b);
2254}
2255
2265static inline size_t net_buf_tailroom(struct net_buf *buf)
2266{
2267 return net_buf_simple_tailroom(&buf->b);
2268}
2269
2279static inline size_t net_buf_headroom(struct net_buf *buf)
2280{
2281 return net_buf_simple_headroom(&buf->b);
2282}
2283
2293static inline uint16_t net_buf_max_len(struct net_buf *buf)
2294{
2295 return net_buf_simple_max_len(&buf->b);
2296}
2297
2307static inline uint8_t *net_buf_tail(struct net_buf *buf)
2308{
2309 return net_buf_simple_tail(&buf->b);
2310}
2311
2318
2330void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag);
2331
2346struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag);
2347
2357#if defined(CONFIG_NET_BUF_LOG)
2358struct net_buf *net_buf_frag_del_debug(struct net_buf *parent,
2359 struct net_buf *frag,
2360 const char *func, int line);
2361#define net_buf_frag_del(_parent, _frag) \
2362 net_buf_frag_del_debug(_parent, _frag, __func__, __LINE__)
2363#else
2364struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag);
2365#endif
2366
2382size_t net_buf_linearize(void *dst, size_t dst_len,
2383 struct net_buf *src, size_t offset, size_t len);
2384
2399typedef struct net_buf * __must_check (*net_buf_allocator_cb)(k_timeout_t timeout,
2400 void *user_data);
2401
2423size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
2424 const void *value, k_timeout_t timeout,
2425 net_buf_allocator_cb allocate_cb, void *user_data);
2426
2442static inline struct net_buf *net_buf_skip(struct net_buf *buf, size_t len)
2443{
2444 while (buf && len--) {
2445 net_buf_pull_u8(buf);
2446 if (!buf->len) {
2447 buf = net_buf_frag_del(NULL, buf);
2448 }
2449 }
2450
2451 return buf;
2452}
2453
2464static inline size_t net_buf_frags_len(struct net_buf *buf)
2465{
2466 size_t bytes = 0;
2467
2468 while (buf) {
2469 bytes += buf->len;
2470 buf = buf->frags;
2471 }
2472
2473 return bytes;
2474}
2475
2480#ifdef __cplusplus
2481}
2482#endif
2483
2484#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:44
#define k_lifo_put(lifo, data)
Add an element to a LIFO queue.
Definition: kernel.h:2492
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:135
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:1687
static size_t net_buf_headroom(struct net_buf *buf)
Check buffer headroom.
Definition: buf.h:2279
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:1822
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:1597
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:2399
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:2442
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:1503
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:1582
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:2251
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:2054
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:1852
static void net_buf_simple_reset(struct net_buf_simple *buf)
Reset buffer.
Definition: buf.h:165
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:1266
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:1627
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:864
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:1747
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:2176
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:2191
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:2265
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:1567
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:2221
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:1942
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:1762
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:2101
static void net_buf_destroy(struct net_buf *buf)
Destroy buffer from custom destroy callback.
Definition: buf.h:1367
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:1900
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:2206
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:2012
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:2146
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:1537
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:1970
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:1956
static void net_buf_reserve(struct net_buf *buf, size_t reserve)
Initialize buffer with the given headroom.
Definition: buf.h:1487
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:1867
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:804
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:2161
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:1657
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:1777
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:1914
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:1883
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:2131
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:1984
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:2236
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:1837
static uint16_t net_buf_max_len(struct net_buf *buf)
Check maximum net_buf::len value.
Definition: buf.h:2293
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:1732
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:1928
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:2026
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:1672
static uint8_t * net_buf_tail(struct net_buf *buf)
Get the tail pointer for a buffer.
Definition: buf.h:2307
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:1807
static void * net_buf_remove_mem(struct net_buf *buf, size_t len)
Remove data from the end of the buffer.
Definition: buf.h:1702
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:1520
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:2040
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:1717
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:1552
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:1998
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:1612
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:1792
static size_t net_buf_frags_len(struct net_buf *buf)
Calculate amount of bytes stored in fragments.
Definition: buf.h:2464
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:2116
static void * net_buf_pull_mem(struct net_buf *buf, size_t len)
Remove data from the beginning of the buffer.
Definition: buf.h:2086
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:880
static void * net_buf_pull(struct net_buf *buf, size_t len)
Remove data from the beginning of the buffer.
Definition: buf.h:2070
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:1642
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:1474
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:29
Public kernel APIs.
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: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:2209
Definition: kernel.h:2443
Kernel Spin Lock.
Definition: spinlock.h:42
Kernel timeout type.
Definition: sys_clock.h:65
Definition: buf.h:971
const struct net_buf_data_cb * cb
Definition: buf.h:972
void * alloc_data
Definition: buf.h:973
Definition: buf.h:964
uint8_t *(* ref)(struct net_buf *buf, uint8_t *data)
Definition: buf.h:967
uint8_t *(* alloc)(struct net_buf *buf, size_t *size, k_timeout_t timeout)
Definition: buf.h:965
void(* unref)(struct net_buf *buf, uint8_t *data)
Definition: buf.h:968
Definition: buf.h:1097
size_t data_size
Definition: buf.h:1098
uint8_t * data_pool
Definition: buf.h:1099
Network buffer pool representation.
Definition: buf.h:981
void(*const destroy)(struct net_buf *buf)
Definition: buf.h:1009
uint16_t uninit_count
Definition: buf.h:992
uint8_t user_data_size
Definition: buf.h:995
const uint16_t buf_count
Definition: buf.h:989
const struct net_buf_data_alloc * alloc
Definition: buf.h:1012
struct k_lifo free
Definition: buf.h:983
struct k_spinlock lock
Definition: buf.h:986
Parsing state of a buffer.
Definition: buf.h:849
uint16_t offset
Definition: buf.h:851
uint16_t len
Definition: buf.h:853
Simple network buffer representation.
Definition: buf.h:82
uint8_t * data
Definition: buf.h:84
uint16_t size
Definition: buf.h:94
uint16_t len
Definition: buf.h:91
Network buffer representation.
Definition: buf.h:914
uint16_t size
Definition: buf.h:948
struct net_buf * frags
Definition: buf.h:920
uint8_t ref
Definition: buf.h:924
uint8_t pool_id
Definition: buf.h:930
sys_snode_t node
Definition: buf.h:917
uint8_t user_data_size
Definition: buf.h:933
uint8_t flags
Definition: buf.h:927
uint8_t * data
Definition: buf.h:942
uint8_t user_data[]
Definition: buf.h:961
struct net_buf_simple b
Definition: buf.h:957
uint16_t len
Definition: buf.h:945
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
static const intptr_t user_data[5]
Definition: main.c:588
Misc utilities.