buf.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  1. /* buf.c - Buffer management */
  2. /*
  3. * Copyright (c) 2015-2019 Intel Corporation
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #include <os_common_api.h>
  8. #define SYS_LOG_NO_NEWLINE
  9. #define SYS_LOG_DOMAIN "net_buf"
  10. #define LOG_LEVEL CONFIG_NET_BUF_LOG_LEVEL
  11. #include <logging/log.h>
  12. //LOG_MODULE_DECLARE(LOG_MODULE_NAME, LOG_LEVEL);
  13. #include <stdio.h>
  14. #include <errno.h>
  15. #include <stddef.h>
  16. #include <string.h>
  17. #include <sys/byteorder.h>
  18. #include <acts_net/buf.h>
  19. /* Actions add start */
  20. #define BUF_TEST_DEBUG 1
  21. #if BUF_TEST_DEBUG
  22. #define BUF_ASSERT(cond, str, ...) __ASSERT(cond, "" __VA_ARGS__)
  23. #define BUF_LOG(fmt, ...) printk(fmt, ##__VA_ARGS__)
  24. #else
  25. #define BUF_ASSERT(cond, str, ...)
  26. #define BUF_LOG(fmt, ...)
  27. #endif
  28. /* Actions add end */
  29. #if defined(CONFIG_NET_BUF_LOG)
  30. #define NET_BUF_DBG(fmt, ...) LOG_DBG("(%p) " fmt, k_current_get(), \
  31. ##__VA_ARGS__)
  32. #define NET_BUF_ERR(fmt, ...) LOG_ERR(fmt, ##__VA_ARGS__)
  33. #define NET_BUF_WARN(fmt, ...) LOG_WRN(fmt, ##__VA_ARGS__)
  34. #define NET_BUF_INFO(fmt, ...) LOG_INF(fmt, ##__VA_ARGS__)
  35. #else
  36. #define NET_BUF_DBG(fmt, ...)
  37. #define NET_BUF_ERR(fmt, ...)
  38. #define NET_BUF_WARN(fmt, ...)
  39. #define NET_BUF_INFO(fmt, ...)
  40. #endif /* CONFIG_NET_BUF_LOG */
  41. #define NET_BUF_ASSERT(cond, ...) __ASSERT(cond, "" __VA_ARGS__)
  42. #if CONFIG_NET_BUF_WARN_ALLOC_INTERVAL > 0
  43. #define WARN_ALLOC_INTERVAL K_SECONDS(CONFIG_NET_BUF_WARN_ALLOC_INTERVAL)
  44. #else
  45. #define WARN_ALLOC_INTERVAL K_FOREVER
  46. #endif
  47. /* Linker-defined symbol bound to the static pool structs */
  48. extern struct net_buf_pool _net_buf_pool_list[];
  49. struct net_buf_pool *net_buf_pool_get(int id)
  50. {
  51. return &_net_buf_pool_list[id];
  52. }
  53. static int pool_id(struct net_buf_pool *pool)
  54. {
  55. return pool - _net_buf_pool_list;
  56. }
  57. int net_buf_id(struct net_buf *buf)
  58. {
  59. struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
  60. return buf - pool->__bufs;
  61. }
  62. static inline struct net_buf *pool_get_uninit(struct net_buf_pool *pool,
  63. uint16_t uninit_count)
  64. {
  65. struct net_buf *buf;
  66. buf = &pool->__bufs[pool->buf_count - uninit_count];
  67. buf->pool_id = pool_id(pool);
  68. return buf;
  69. }
  70. void net_buf_reset(struct net_buf *buf)
  71. {
  72. __ASSERT_NO_MSG(buf->flags == 0U);
  73. __ASSERT_NO_MSG(buf->frags == NULL);
  74. net_buf_simple_reset(&buf->b);
  75. }
  76. static uint8_t *generic_data_ref(struct net_buf *buf, uint8_t *data)
  77. {
  78. uint8_t *ref_count;
  79. ref_count = data - 1;
  80. (*ref_count)++;
  81. return data;
  82. }
  83. static uint8_t *mem_pool_data_alloc(struct net_buf *buf, size_t *size,
  84. k_timeout_t timeout)
  85. {
  86. struct net_buf_pool *buf_pool = net_buf_pool_get(buf->pool_id);
  87. struct k_heap *pool = buf_pool->alloc->alloc_data;
  88. uint8_t *ref_count;
  89. /* Reserve extra space for a ref-count (uint8_t) */
  90. void *b = k_heap_alloc(pool, 1 + *size, timeout);
  91. if (b == NULL) {
  92. return NULL;
  93. }
  94. ref_count = (uint8_t *)b;
  95. *ref_count = 1U;
  96. /* Return pointer to the byte following the ref count */
  97. return ref_count + 1;
  98. }
  99. static void mem_pool_data_unref(struct net_buf *buf, uint8_t *data)
  100. {
  101. struct net_buf_pool *buf_pool = net_buf_pool_get(buf->pool_id);
  102. struct k_heap *pool = buf_pool->alloc->alloc_data;
  103. uint8_t *ref_count;
  104. ref_count = data - 1;
  105. if (--(*ref_count)) {
  106. return;
  107. }
  108. /* Need to copy to local variable due to alignment */
  109. k_heap_free(pool, ref_count);
  110. }
  111. const struct net_buf_data_cb net_buf_var_cb = {
  112. .alloc = mem_pool_data_alloc,
  113. .ref = generic_data_ref,
  114. .unref = mem_pool_data_unref,
  115. };
  116. static uint8_t *fixed_data_alloc(struct net_buf *buf, size_t *size,
  117. k_timeout_t timeout)
  118. {
  119. struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
  120. const struct net_buf_pool_fixed *fixed = pool->alloc->alloc_data;
  121. *size = MIN(fixed->data_size, *size);
  122. return fixed->data_pool + fixed->data_size * net_buf_id(buf);
  123. }
  124. static void fixed_data_unref(struct net_buf *buf, uint8_t *data)
  125. {
  126. /* Nothing needed for fixed-size data pools */
  127. }
  128. const struct net_buf_data_cb net_buf_fixed_cb = {
  129. .alloc = fixed_data_alloc,
  130. .unref = fixed_data_unref,
  131. };
  132. #if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
  133. static uint8_t *heap_data_alloc(struct net_buf *buf, size_t *size,
  134. k_timeout_t timeout)
  135. {
  136. uint8_t *ref_count;
  137. ref_count = k_malloc(1 + *size);
  138. if (!ref_count) {
  139. return NULL;
  140. }
  141. *ref_count = 1U;
  142. return ref_count + 1;
  143. }
  144. static void heap_data_unref(struct net_buf *buf, uint8_t *data)
  145. {
  146. uint8_t *ref_count;
  147. ref_count = data - 1;
  148. if (--(*ref_count)) {
  149. return;
  150. }
  151. k_free(ref_count);
  152. }
  153. static const struct net_buf_data_cb net_buf_heap_cb = {
  154. .alloc = heap_data_alloc,
  155. .ref = generic_data_ref,
  156. .unref = heap_data_unref,
  157. };
  158. const struct net_buf_data_alloc net_buf_heap_alloc = {
  159. .cb = &net_buf_heap_cb,
  160. };
  161. #endif /* CONFIG_HEAP_MEM_POOL_SIZE > 0 */
  162. static uint8_t *data_alloc(struct net_buf *buf, size_t *size, k_timeout_t timeout)
  163. {
  164. struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
  165. return pool->alloc->cb->alloc(buf, size, timeout);
  166. }
  167. static uint8_t *data_ref(struct net_buf *buf, uint8_t *data)
  168. {
  169. struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
  170. return pool->alloc->cb->ref(buf, data);
  171. }
  172. static void data_unref(struct net_buf *buf, uint8_t *data)
  173. {
  174. struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
  175. if (buf->flags & NET_BUF_EXTERNAL_DATA) {
  176. return;
  177. }
  178. pool->alloc->cb->unref(buf, data);
  179. }
  180. #if defined(CONFIG_NET_BUF_LOG)
  181. struct net_buf *net_buf_alloc_len_debug(struct net_buf_pool *pool, size_t size,
  182. k_timeout_t timeout, const char *func,
  183. int line)
  184. #else
  185. struct net_buf *net_buf_alloc_len(struct net_buf_pool *pool, size_t size,
  186. k_timeout_t timeout)
  187. #endif
  188. {
  189. uint64_t end = os_clock_timeout_end_calc(timeout);
  190. struct net_buf *buf;
  191. unsigned int key;
  192. __ASSERT_NO_MSG(pool);
  193. NET_BUF_DBG("%s():%d: pool %p size %zu", func, line, pool, size);
  194. /* We need to lock interrupts temporarily to prevent race conditions
  195. * when accessing pool->uninit_count.
  196. */
  197. key = irq_lock();
  198. /* If there are uninitialized buffers we're guaranteed to succeed
  199. * with the allocation one way or another.
  200. */
  201. if (pool->uninit_count) {
  202. uint16_t uninit_count;
  203. /* If this is not the first access to the pool, we can
  204. * be opportunistic and try to fetch a previously used
  205. * buffer from the LIFO with K_NO_WAIT.
  206. */
  207. if (pool->uninit_count < pool->buf_count) {
  208. buf = k_lifo_get(&pool->free, K_NO_WAIT);
  209. if (buf) {
  210. irq_unlock(key);
  211. goto success;
  212. }
  213. }
  214. uninit_count = pool->uninit_count--;
  215. irq_unlock(key);
  216. buf = pool_get_uninit(pool, uninit_count);
  217. goto success;
  218. }
  219. irq_unlock(key);
  220. #if defined(CONFIG_NET_BUF_LOG) && (CONFIG_NET_BUF_LOG_LEVEL >= LOG_LEVEL_WRN)
  221. if (K_TIMEOUT_EQ(timeout, K_FOREVER)) {
  222. uint32_t ref = k_uptime_get_32();
  223. buf = k_lifo_get(&pool->free, K_NO_WAIT);
  224. while (!buf) {
  225. #if defined(CONFIG_NET_BUF_POOL_USAGE)
  226. NET_BUF_WARN("%s():%d: Pool %s low on buffers.",
  227. func, line, pool->name);
  228. #else
  229. NET_BUF_WARN("%s():%d: Pool %p low on buffers.",
  230. func, line, pool);
  231. #endif
  232. buf = k_lifo_get(&pool->free, WARN_ALLOC_INTERVAL);
  233. #if defined(CONFIG_NET_BUF_POOL_USAGE)
  234. NET_BUF_WARN("%s():%d: Pool %s blocked for %u secs",
  235. func, line, pool->name,
  236. (k_uptime_get_32() - ref) / MSEC_PER_SEC);
  237. #else
  238. NET_BUF_WARN("%s():%d: Pool %p blocked for %u secs",
  239. func, line, pool,
  240. (k_uptime_get_32() - ref) / MSEC_PER_SEC);
  241. #endif
  242. }
  243. } else {
  244. buf = k_lifo_get(&pool->free, timeout);
  245. }
  246. #else
  247. buf = k_lifo_get(&pool->free, timeout);
  248. #endif
  249. if (!buf) {
  250. NET_BUF_ERR("%s():%d: Failed to get free buffer", func, line);
  251. return NULL;
  252. }
  253. success:
  254. NET_BUF_DBG("allocated buf %p", buf);
  255. if (size) {
  256. #if __ASSERT_ON
  257. size_t req_size = size;
  258. #endif
  259. if (!K_TIMEOUT_EQ(timeout, K_NO_WAIT) &&
  260. !K_TIMEOUT_EQ(timeout, K_FOREVER)) {
  261. int64_t remaining = end - os_clock_tick_get();
  262. if (remaining <= 0) {
  263. timeout = K_NO_WAIT;
  264. } else {
  265. timeout = Z_TIMEOUT_TICKS(remaining);
  266. }
  267. }
  268. buf->__buf = data_alloc(buf, &size, timeout);
  269. if (!buf->__buf) {
  270. NET_BUF_ERR("%s():%d: Failed to allocate data",
  271. func, line);
  272. net_buf_destroy(buf);
  273. return NULL;
  274. }
  275. NET_BUF_ASSERT(req_size <= size);
  276. } else {
  277. buf->__buf = NULL;
  278. }
  279. buf->ref = 1U;
  280. buf->flags = 0U;
  281. buf->frags = NULL;
  282. buf->size = size;
  283. net_buf_reset(buf);
  284. #if defined(CONFIG_NET_BUF_POOL_USAGE)
  285. atomic_dec(&pool->avail_count);
  286. /* Actions add start */
  287. if ((pool->buf_count - pool->avail_count) > pool->max_used) {
  288. pool->max_used = pool->buf_count - pool->avail_count;
  289. BUF_LOG("pool(%s), max_used: %d\n", pool->name, pool->max_used);
  290. }
  291. /* Actions add end */
  292. __ASSERT_NO_MSG(atomic_get(&pool->avail_count) >= 0);
  293. #endif
  294. return buf;
  295. }
  296. #if defined(CONFIG_NET_BUF_LOG)
  297. struct net_buf *net_buf_alloc_fixed_debug(struct net_buf_pool *pool,
  298. k_timeout_t timeout, const char *func,
  299. int line)
  300. {
  301. /* Actions modify start */
  302. //const struct net_buf_pool_fixed *fixed = pool->alloc->alloc_data;
  303. return net_buf_alloc_len_debug(pool, pool->alloc->data_size, timeout, func,
  304. line);
  305. /* Actions modify end */
  306. }
  307. #else
  308. struct net_buf *net_buf_alloc_fixed(struct net_buf_pool *pool,
  309. k_timeout_t timeout)
  310. {
  311. /* Actions modify start */
  312. //const struct net_buf_pool_fixed *fixed = pool->alloc->alloc_data;
  313. return net_buf_alloc_len(pool, pool->alloc->data_size, timeout);
  314. /* Actions modify end */
  315. }
  316. #endif
  317. #if defined(CONFIG_NET_BUF_LOG)
  318. struct net_buf *net_buf_alloc_with_data_debug(struct net_buf_pool *pool,
  319. void *data, size_t size,
  320. k_timeout_t timeout,
  321. const char *func, int line)
  322. #else
  323. struct net_buf *net_buf_alloc_with_data(struct net_buf_pool *pool,
  324. void *data, size_t size,
  325. k_timeout_t timeout)
  326. #endif
  327. {
  328. struct net_buf *buf;
  329. #if defined(CONFIG_NET_BUF_LOG)
  330. buf = net_buf_alloc_len_debug(pool, 0, timeout, func, line);
  331. #else
  332. buf = net_buf_alloc_len(pool, 0, timeout);
  333. #endif
  334. if (!buf) {
  335. return NULL;
  336. }
  337. net_buf_simple_init_with_data(&buf->b, data, size);
  338. buf->flags = NET_BUF_EXTERNAL_DATA;
  339. return buf;
  340. }
  341. #if defined(CONFIG_NET_BUF_LOG)
  342. struct net_buf *net_buf_get_debug(struct k_fifo *fifo, k_timeout_t timeout,
  343. const char *func, int line)
  344. #else
  345. struct net_buf *net_buf_get(struct k_fifo *fifo, k_timeout_t timeout)
  346. #endif
  347. {
  348. struct net_buf *buf, *frag;
  349. NET_BUF_DBG("%s():%d: fifo %p", func, line, fifo);
  350. buf = k_fifo_get(fifo, timeout);
  351. if (!buf) {
  352. return NULL;
  353. }
  354. NET_BUF_DBG("%s():%d: buf %p fifo %p", func, line, buf, fifo);
  355. /* Get any fragments belonging to this buffer */
  356. for (frag = buf; (frag->flags & NET_BUF_FRAGS); frag = frag->frags) {
  357. frag->frags = k_fifo_get(fifo, K_NO_WAIT);
  358. __ASSERT_NO_MSG(frag->frags);
  359. /* The fragments flag is only for FIFO-internal usage */
  360. frag->flags &= ~NET_BUF_FRAGS;
  361. }
  362. /* Mark the end of the fragment list */
  363. frag->frags = NULL;
  364. return buf;
  365. }
  366. void net_buf_simple_init_with_data(struct net_buf_simple *buf,
  367. void *data, size_t size)
  368. {
  369. buf->__buf = data;
  370. buf->data = data;
  371. buf->size = size;
  372. buf->len = size;
  373. }
  374. void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve)
  375. {
  376. __ASSERT_NO_MSG(buf);
  377. __ASSERT_NO_MSG(buf->len == 0U);
  378. NET_BUF_DBG("buf %p reserve %zu", buf, reserve);
  379. buf->data = buf->__buf + reserve;
  380. }
  381. void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf)
  382. {
  383. struct net_buf *tail;
  384. unsigned int key;
  385. __ASSERT_NO_MSG(list);
  386. __ASSERT_NO_MSG(buf);
  387. for (tail = buf; tail->frags; tail = tail->frags) {
  388. tail->flags |= NET_BUF_FRAGS;
  389. }
  390. key = irq_lock();
  391. sys_slist_append_list(list, &buf->node, &tail->node);
  392. irq_unlock(key);
  393. }
  394. struct net_buf *net_buf_slist_get(sys_slist_t *list)
  395. {
  396. struct net_buf *buf, *frag;
  397. unsigned int key;
  398. __ASSERT_NO_MSG(list);
  399. key = irq_lock();
  400. buf = (void *)sys_slist_get(list);
  401. irq_unlock(key);
  402. if (!buf) {
  403. return NULL;
  404. }
  405. /* Get any fragments belonging to this buffer */
  406. for (frag = buf; (frag->flags & NET_BUF_FRAGS); frag = frag->frags) {
  407. key = irq_lock();
  408. frag->frags = (void *)sys_slist_get(list);
  409. irq_unlock(key);
  410. __ASSERT_NO_MSG(frag->frags);
  411. /* The fragments flag is only for list-internal usage */
  412. frag->flags &= ~NET_BUF_FRAGS;
  413. }
  414. /* Mark the end of the fragment list */
  415. frag->frags = NULL;
  416. return buf;
  417. }
  418. void net_buf_put(struct k_fifo *fifo, struct net_buf *buf)
  419. {
  420. struct net_buf *tail;
  421. __ASSERT_NO_MSG(fifo);
  422. __ASSERT_NO_MSG(buf);
  423. for (tail = buf; tail->frags; tail = tail->frags) {
  424. tail->flags |= NET_BUF_FRAGS;
  425. }
  426. /* Actions add start */
  427. #if defined(CONFIG_NET_BUF_POOL_USAGE)
  428. buf->timestamp = k_uptime_get_32();
  429. #endif
  430. /* Actions add end */
  431. k_fifo_put_list(fifo, buf, tail);
  432. }
  433. #if defined(CONFIG_NET_BUF_LOG)
  434. void net_buf_unref_debug(struct net_buf *buf, const char *func, int line)
  435. #else
  436. void net_buf_unref(struct net_buf *buf)
  437. #endif
  438. {
  439. __ASSERT_NO_MSG(buf);
  440. while (buf) {
  441. struct net_buf *frags = buf->frags;
  442. struct net_buf_pool *pool;
  443. #if defined(CONFIG_NET_BUF_LOG)
  444. if (!buf->ref) {
  445. NET_BUF_ERR("%s():%d: buf %p double free", func, line,
  446. buf);
  447. return;
  448. }
  449. #endif
  450. NET_BUF_DBG("buf %p ref %u pool_id %u frags %p", buf, buf->ref,
  451. buf->pool_id, buf->frags);
  452. if (--buf->ref > 0) {
  453. return;
  454. }
  455. if (buf->__buf) {
  456. data_unref(buf, buf->__buf);
  457. buf->__buf = NULL;
  458. }
  459. buf->data = NULL;
  460. buf->frags = NULL;
  461. pool = net_buf_pool_get(buf->pool_id);
  462. #if defined(CONFIG_NET_BUF_POOL_USAGE)
  463. atomic_inc(&pool->avail_count);
  464. __ASSERT_NO_MSG(atomic_get(&pool->avail_count) <= pool->buf_count);
  465. #endif
  466. if (pool->destroy) {
  467. pool->destroy(buf);
  468. } else {
  469. net_buf_destroy(buf);
  470. }
  471. buf = frags;
  472. }
  473. }
  474. struct net_buf *net_buf_ref(struct net_buf *buf)
  475. {
  476. __ASSERT_NO_MSG(buf);
  477. NET_BUF_DBG("buf %p (old) ref %u pool_id %u",
  478. buf, buf->ref, buf->pool_id);
  479. buf->ref++;
  480. return buf;
  481. }
  482. struct net_buf *net_buf_clone(struct net_buf *buf, k_timeout_t timeout)
  483. {
  484. int64_t end = os_clock_timeout_end_calc(timeout);
  485. struct net_buf_pool *pool;
  486. struct net_buf *clone;
  487. __ASSERT_NO_MSG(buf);
  488. pool = net_buf_pool_get(buf->pool_id);
  489. clone = net_buf_alloc_len(pool, 0, timeout);
  490. if (!clone) {
  491. return NULL;
  492. }
  493. /* If the pool supports data referencing use that. Otherwise
  494. * we need to allocate new data and make a copy.
  495. */
  496. if (pool->alloc->cb->ref && !(buf->flags & NET_BUF_EXTERNAL_DATA)) {
  497. clone->__buf = data_ref(buf, buf->__buf);
  498. clone->data = buf->data;
  499. clone->len = buf->len;
  500. clone->size = buf->size;
  501. } else {
  502. size_t size = buf->size;
  503. if (!K_TIMEOUT_EQ(timeout, K_NO_WAIT) &&
  504. !K_TIMEOUT_EQ(timeout, K_FOREVER)) {
  505. int64_t remaining = end - os_clock_tick_get();
  506. if (remaining <= 0) {
  507. timeout = K_NO_WAIT;
  508. } else {
  509. timeout = Z_TIMEOUT_TICKS(remaining);
  510. }
  511. }
  512. clone->__buf = data_alloc(clone, &size, timeout);
  513. if (!clone->__buf || size < buf->size) {
  514. net_buf_destroy(clone);
  515. return NULL;
  516. }
  517. clone->size = size;
  518. clone->data = clone->__buf + net_buf_headroom(buf);
  519. net_buf_add_mem(clone, buf->data, buf->len);
  520. }
  521. return clone;
  522. }
  523. struct net_buf *net_buf_frag_last(struct net_buf *buf)
  524. {
  525. __ASSERT_NO_MSG(buf);
  526. while (buf->frags) {
  527. buf = buf->frags;
  528. }
  529. return buf;
  530. }
  531. void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag)
  532. {
  533. __ASSERT_NO_MSG(parent);
  534. __ASSERT_NO_MSG(frag);
  535. if (parent->frags) {
  536. net_buf_frag_last(frag)->frags = parent->frags;
  537. }
  538. /* Take ownership of the fragment reference */
  539. parent->frags = frag;
  540. }
  541. struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag)
  542. {
  543. __ASSERT_NO_MSG(frag);
  544. if (!head) {
  545. return net_buf_ref(frag);
  546. }
  547. net_buf_frag_insert(net_buf_frag_last(head), frag);
  548. return head;
  549. }
  550. #if defined(CONFIG_NET_BUF_LOG)
  551. struct net_buf *net_buf_frag_del_debug(struct net_buf *parent,
  552. struct net_buf *frag,
  553. const char *func, int line)
  554. #else
  555. struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag)
  556. #endif
  557. {
  558. struct net_buf *next_frag;
  559. __ASSERT_NO_MSG(frag);
  560. if (parent) {
  561. __ASSERT_NO_MSG(parent->frags);
  562. __ASSERT_NO_MSG(parent->frags == frag);
  563. parent->frags = frag->frags;
  564. }
  565. next_frag = frag->frags;
  566. frag->frags = NULL;
  567. #if defined(CONFIG_NET_BUF_LOG)
  568. net_buf_unref_debug(frag, func, line);
  569. #else
  570. net_buf_unref(frag);
  571. #endif
  572. return next_frag;
  573. }
  574. size_t net_buf_linearize(void *dst, size_t dst_len, struct net_buf *src,
  575. size_t offset, size_t len)
  576. {
  577. struct net_buf *frag;
  578. size_t to_copy;
  579. size_t copied;
  580. len = MIN(len, dst_len);
  581. frag = src;
  582. /* find the right fragment to start copying from */
  583. while (frag && offset >= frag->len) {
  584. offset -= frag->len;
  585. frag = frag->frags;
  586. }
  587. /* traverse the fragment chain until len bytes are copied */
  588. copied = 0;
  589. while (frag && len > 0) {
  590. to_copy = MIN(len, frag->len - offset);
  591. memcpy((uint8_t *)dst + copied, frag->data + offset, to_copy);
  592. copied += to_copy;
  593. /* to_copy is always <= len */
  594. len -= to_copy;
  595. frag = frag->frags;
  596. /* after the first iteration, this value will be 0 */
  597. offset = 0;
  598. }
  599. return copied;
  600. }
  601. /* This helper routine will append multiple bytes, if there is no place for
  602. * the data in current fragment then create new fragment and add it to
  603. * the buffer. It assumes that the buffer has at least one fragment.
  604. */
  605. size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
  606. const void *value, k_timeout_t timeout,
  607. net_buf_allocator_cb allocate_cb, void *user_data)
  608. {
  609. struct net_buf *frag = net_buf_frag_last(buf);
  610. size_t added_len = 0;
  611. const uint8_t *value8 = value;
  612. do {
  613. uint16_t count = MIN(len, net_buf_tailroom(frag));
  614. net_buf_add_mem(frag, value8, count);
  615. len -= count;
  616. added_len += count;
  617. value8 += count;
  618. if (len == 0) {
  619. return added_len;
  620. }
  621. if (allocate_cb) {
  622. frag = allocate_cb(timeout, user_data);
  623. } else {
  624. struct net_buf_pool *pool;
  625. /* Allocate from the original pool if no callback has
  626. * been provided.
  627. */
  628. pool = net_buf_pool_get(buf->pool_id);
  629. frag = net_buf_alloc_len(pool, len, timeout);
  630. }
  631. if (!frag) {
  632. return added_len;
  633. }
  634. net_buf_frag_add(buf, frag);
  635. } while (1);
  636. /* Unreachable */
  637. return 0;
  638. }
  639. #if defined(CONFIG_NET_BUF_SIMPLE_LOG)
  640. #define NET_BUF_SIMPLE_DBG(fmt, ...) NET_BUF_DBG(fmt, ##__VA_ARGS__)
  641. #define NET_BUF_SIMPLE_ERR(fmt, ...) NET_BUF_ERR(fmt, ##__VA_ARGS__)
  642. #define NET_BUF_SIMPLE_WARN(fmt, ...) NET_BUF_WARN(fmt, ##__VA_ARGS__)
  643. #define NET_BUF_SIMPLE_INFO(fmt, ...) NET_BUF_INFO(fmt, ##__VA_ARGS__)
  644. #else
  645. #define NET_BUF_SIMPLE_DBG(fmt, ...)
  646. #define NET_BUF_SIMPLE_ERR(fmt, ...)
  647. #define NET_BUF_SIMPLE_WARN(fmt, ...)
  648. #define NET_BUF_SIMPLE_INFO(fmt, ...)
  649. #endif /* CONFIG_NET_BUF_SIMPLE_LOG */
  650. void net_buf_simple_clone(const struct net_buf_simple *original,
  651. struct net_buf_simple *clone)
  652. {
  653. memcpy(clone, original, sizeof(struct net_buf_simple));
  654. }
  655. void *net_buf_simple_add(struct net_buf_simple *buf, size_t len)
  656. {
  657. uint8_t *tail = net_buf_simple_tail(buf);
  658. NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
  659. if (net_buf_simple_tailroom(buf) < len) {
  660. printk("Tailroom %d len %d\n", net_buf_simple_tailroom(buf), len);
  661. }
  662. __ASSERT_NO_MSG(net_buf_simple_tailroom(buf) >= len);
  663. buf->len += len;
  664. return tail;
  665. }
  666. void *net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem,
  667. size_t len)
  668. {
  669. NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
  670. return memcpy(net_buf_simple_add(buf, len), mem, len);
  671. }
  672. uint8_t *net_buf_simple_add_u8(struct net_buf_simple *buf, uint8_t val)
  673. {
  674. uint8_t *u8;
  675. NET_BUF_SIMPLE_DBG("buf %p val 0x%02x", buf, val);
  676. u8 = net_buf_simple_add(buf, 1);
  677. *u8 = val;
  678. return u8;
  679. }
  680. void net_buf_simple_add_le16(struct net_buf_simple *buf, uint16_t val)
  681. {
  682. NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
  683. sys_put_le16(val, net_buf_simple_add(buf, sizeof(val)));
  684. }
  685. void net_buf_simple_add_be16(struct net_buf_simple *buf, uint16_t val)
  686. {
  687. NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
  688. sys_put_be16(val, net_buf_simple_add(buf, sizeof(val)));
  689. }
  690. void net_buf_simple_add_le24(struct net_buf_simple *buf, uint32_t val)
  691. {
  692. NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
  693. sys_put_le24(val, net_buf_simple_add(buf, 3));
  694. }
  695. void net_buf_simple_add_be24(struct net_buf_simple *buf, uint32_t val)
  696. {
  697. NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
  698. sys_put_be24(val, net_buf_simple_add(buf, 3));
  699. }
  700. void net_buf_simple_add_le32(struct net_buf_simple *buf, uint32_t val)
  701. {
  702. NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
  703. sys_put_le32(val, net_buf_simple_add(buf, sizeof(val)));
  704. }
  705. void net_buf_simple_add_be32(struct net_buf_simple *buf, uint32_t val)
  706. {
  707. NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
  708. sys_put_be32(val, net_buf_simple_add(buf, sizeof(val)));
  709. }
  710. void net_buf_simple_add_le48(struct net_buf_simple *buf, uint64_t val)
  711. {
  712. NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
  713. sys_put_le48(val, net_buf_simple_add(buf, 6));
  714. }
  715. void net_buf_simple_add_be48(struct net_buf_simple *buf, uint64_t val)
  716. {
  717. NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
  718. sys_put_be48(val, net_buf_simple_add(buf, 6));
  719. }
  720. void net_buf_simple_add_le64(struct net_buf_simple *buf, uint64_t val)
  721. {
  722. NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
  723. sys_put_le64(val, net_buf_simple_add(buf, sizeof(val)));
  724. }
  725. void net_buf_simple_add_be64(struct net_buf_simple *buf, uint64_t val)
  726. {
  727. NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
  728. sys_put_be64(val, net_buf_simple_add(buf, sizeof(val)));
  729. }
  730. void *net_buf_simple_remove_mem(struct net_buf_simple *buf, size_t len)
  731. {
  732. NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
  733. __ASSERT_NO_MSG(buf->len >= len);
  734. buf->len -= len;
  735. return buf->data + buf->len;
  736. }
  737. uint8_t net_buf_simple_remove_u8(struct net_buf_simple *buf)
  738. {
  739. uint8_t val;
  740. void *ptr;
  741. ptr = net_buf_simple_remove_mem(buf, sizeof(val));
  742. val = *(uint8_t *)ptr;
  743. return val;
  744. }
  745. uint16_t net_buf_simple_remove_le16(struct net_buf_simple *buf)
  746. {
  747. uint16_t val;
  748. void *ptr;
  749. ptr = net_buf_simple_remove_mem(buf, sizeof(val));
  750. val = UNALIGNED_GET((uint16_t *)ptr);
  751. return sys_le16_to_cpu(val);
  752. }
  753. uint16_t net_buf_simple_remove_be16(struct net_buf_simple *buf)
  754. {
  755. uint16_t val;
  756. void *ptr;
  757. ptr = net_buf_simple_remove_mem(buf, sizeof(val));
  758. val = UNALIGNED_GET((uint16_t *)ptr);
  759. return sys_be16_to_cpu(val);
  760. }
  761. uint32_t net_buf_simple_remove_le24(struct net_buf_simple *buf)
  762. {
  763. struct uint24 {
  764. uint32_t u24 : 24;
  765. } __packed val;
  766. void *ptr;
  767. ptr = net_buf_simple_remove_mem(buf, sizeof(val));
  768. val = UNALIGNED_GET((struct uint24 *)ptr);
  769. return sys_le24_to_cpu(val.u24);
  770. }
  771. uint32_t net_buf_simple_remove_be24(struct net_buf_simple *buf)
  772. {
  773. struct uint24 {
  774. uint32_t u24 : 24;
  775. } __packed val;
  776. void *ptr;
  777. ptr = net_buf_simple_remove_mem(buf, sizeof(val));
  778. val = UNALIGNED_GET((struct uint24 *)ptr);
  779. return sys_be24_to_cpu(val.u24);
  780. }
  781. uint32_t net_buf_simple_remove_le32(struct net_buf_simple *buf)
  782. {
  783. uint32_t val;
  784. void *ptr;
  785. ptr = net_buf_simple_remove_mem(buf, sizeof(val));
  786. val = UNALIGNED_GET((uint32_t *)ptr);
  787. return sys_le32_to_cpu(val);
  788. }
  789. uint32_t net_buf_simple_remove_be32(struct net_buf_simple *buf)
  790. {
  791. uint32_t val;
  792. void *ptr;
  793. ptr = net_buf_simple_remove_mem(buf, sizeof(val));
  794. val = UNALIGNED_GET((uint32_t *)ptr);
  795. return sys_be32_to_cpu(val);
  796. }
  797. uint64_t net_buf_simple_remove_le48(struct net_buf_simple *buf)
  798. {
  799. struct uint48 {
  800. uint64_t u48 : 48;
  801. } __packed val;
  802. void *ptr;
  803. ptr = net_buf_simple_remove_mem(buf, sizeof(val));
  804. val = UNALIGNED_GET((struct uint48 *)ptr);
  805. return sys_le48_to_cpu(val.u48);
  806. }
  807. uint64_t net_buf_simple_remove_be48(struct net_buf_simple *buf)
  808. {
  809. struct uint48 {
  810. uint64_t u48 : 48;
  811. } __packed val;
  812. void *ptr;
  813. ptr = net_buf_simple_remove_mem(buf, sizeof(val));
  814. val = UNALIGNED_GET((struct uint48 *)ptr);
  815. return sys_be48_to_cpu(val.u48);
  816. }
  817. uint64_t net_buf_simple_remove_le64(struct net_buf_simple *buf)
  818. {
  819. uint64_t val;
  820. void *ptr;
  821. ptr = net_buf_simple_remove_mem(buf, sizeof(val));
  822. val = UNALIGNED_GET((uint64_t *)ptr);
  823. return sys_le64_to_cpu(val);
  824. }
  825. uint64_t net_buf_simple_remove_be64(struct net_buf_simple *buf)
  826. {
  827. uint64_t val;
  828. void *ptr;
  829. ptr = net_buf_simple_remove_mem(buf, sizeof(val));
  830. val = UNALIGNED_GET((uint64_t *)ptr);
  831. return sys_be64_to_cpu(val);
  832. }
  833. void *net_buf_simple_push(struct net_buf_simple *buf, size_t len)
  834. {
  835. NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
  836. __ASSERT_NO_MSG(net_buf_simple_headroom(buf) >= len);
  837. buf->data -= len;
  838. buf->len += len;
  839. return buf->data;
  840. }
  841. void *net_buf_simple_push_mem(struct net_buf_simple *buf, const void *mem,
  842. size_t len)
  843. {
  844. NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
  845. return memcpy(net_buf_simple_push(buf, len), mem, len);
  846. }
  847. void net_buf_simple_push_le16(struct net_buf_simple *buf, uint16_t val)
  848. {
  849. NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
  850. sys_put_le16(val, net_buf_simple_push(buf, sizeof(val)));
  851. }
  852. void net_buf_simple_push_be16(struct net_buf_simple *buf, uint16_t val)
  853. {
  854. NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
  855. sys_put_be16(val, net_buf_simple_push(buf, sizeof(val)));
  856. }
  857. void net_buf_simple_push_u8(struct net_buf_simple *buf, uint8_t val)
  858. {
  859. uint8_t *data = net_buf_simple_push(buf, 1);
  860. *data = val;
  861. }
  862. void net_buf_simple_push_le24(struct net_buf_simple *buf, uint32_t val)
  863. {
  864. NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
  865. sys_put_le24(val, net_buf_simple_push(buf, 3));
  866. }
  867. void net_buf_simple_push_be24(struct net_buf_simple *buf, uint32_t val)
  868. {
  869. NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
  870. sys_put_be24(val, net_buf_simple_push(buf, 3));
  871. }
  872. void net_buf_simple_push_le32(struct net_buf_simple *buf, uint32_t val)
  873. {
  874. NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
  875. sys_put_le32(val, net_buf_simple_push(buf, sizeof(val)));
  876. }
  877. void net_buf_simple_push_be32(struct net_buf_simple *buf, uint32_t val)
  878. {
  879. NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
  880. sys_put_be32(val, net_buf_simple_push(buf, sizeof(val)));
  881. }
  882. void net_buf_simple_push_le48(struct net_buf_simple *buf, uint64_t val)
  883. {
  884. NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
  885. sys_put_le48(val, net_buf_simple_push(buf, 6));
  886. }
  887. void net_buf_simple_push_be48(struct net_buf_simple *buf, uint64_t val)
  888. {
  889. NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
  890. sys_put_be48(val, net_buf_simple_push(buf, 6));
  891. }
  892. void net_buf_simple_push_le64(struct net_buf_simple *buf, uint64_t val)
  893. {
  894. NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
  895. sys_put_le64(val, net_buf_simple_push(buf, sizeof(val)));
  896. }
  897. void net_buf_simple_push_be64(struct net_buf_simple *buf, uint64_t val)
  898. {
  899. NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
  900. sys_put_be64(val, net_buf_simple_push(buf, sizeof(val)));
  901. }
  902. void *net_buf_simple_pull(struct net_buf_simple *buf, size_t len)
  903. {
  904. NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
  905. __ASSERT_NO_MSG(buf->len >= len);
  906. buf->len -= len;
  907. return buf->data += len;
  908. }
  909. void *net_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len)
  910. {
  911. void *data = buf->data;
  912. NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
  913. __ASSERT_NO_MSG(buf->len >= len);
  914. buf->len -= len;
  915. buf->data += len;
  916. return data;
  917. }
  918. uint8_t net_buf_simple_pull_u8(struct net_buf_simple *buf)
  919. {
  920. uint8_t val;
  921. val = buf->data[0];
  922. net_buf_simple_pull(buf, 1);
  923. return val;
  924. }
  925. uint16_t net_buf_simple_pull_le16(struct net_buf_simple *buf)
  926. {
  927. uint16_t val;
  928. val = UNALIGNED_GET((uint16_t *)buf->data);
  929. net_buf_simple_pull(buf, sizeof(val));
  930. return sys_le16_to_cpu(val);
  931. }
  932. uint16_t net_buf_simple_pull_be16(struct net_buf_simple *buf)
  933. {
  934. uint16_t val;
  935. val = UNALIGNED_GET((uint16_t *)buf->data);
  936. net_buf_simple_pull(buf, sizeof(val));
  937. return sys_be16_to_cpu(val);
  938. }
  939. uint32_t net_buf_simple_pull_le24(struct net_buf_simple *buf)
  940. {
  941. struct uint24 {
  942. uint32_t u24:24;
  943. } __packed val;
  944. val = UNALIGNED_GET((struct uint24 *)buf->data);
  945. net_buf_simple_pull(buf, sizeof(val));
  946. return sys_le24_to_cpu(val.u24);
  947. }
  948. uint32_t net_buf_simple_pull_be24(struct net_buf_simple *buf)
  949. {
  950. struct uint24 {
  951. uint32_t u24:24;
  952. } __packed val;
  953. val = UNALIGNED_GET((struct uint24 *)buf->data);
  954. net_buf_simple_pull(buf, sizeof(val));
  955. return sys_be24_to_cpu(val.u24);
  956. }
  957. uint32_t net_buf_simple_pull_le32(struct net_buf_simple *buf)
  958. {
  959. uint32_t val;
  960. val = UNALIGNED_GET((uint32_t *)buf->data);
  961. net_buf_simple_pull(buf, sizeof(val));
  962. return sys_le32_to_cpu(val);
  963. }
  964. uint32_t net_buf_simple_pull_be32(struct net_buf_simple *buf)
  965. {
  966. uint32_t val;
  967. val = UNALIGNED_GET((uint32_t *)buf->data);
  968. net_buf_simple_pull(buf, sizeof(val));
  969. return sys_be32_to_cpu(val);
  970. }
  971. uint64_t net_buf_simple_pull_le48(struct net_buf_simple *buf)
  972. {
  973. struct uint48 {
  974. uint64_t u48:48;
  975. } __packed val;
  976. val = UNALIGNED_GET((struct uint48 *)buf->data);
  977. net_buf_simple_pull(buf, sizeof(val));
  978. return sys_le48_to_cpu(val.u48);
  979. }
  980. uint64_t net_buf_simple_pull_be48(struct net_buf_simple *buf)
  981. {
  982. struct uint48 {
  983. uint64_t u48:48;
  984. } __packed val;
  985. val = UNALIGNED_GET((struct uint48 *)buf->data);
  986. net_buf_simple_pull(buf, sizeof(val));
  987. return sys_be48_to_cpu(val.u48);
  988. }
  989. uint64_t net_buf_simple_pull_le64(struct net_buf_simple *buf)
  990. {
  991. uint64_t val;
  992. val = UNALIGNED_GET((uint64_t *)buf->data);
  993. net_buf_simple_pull(buf, sizeof(val));
  994. return sys_le64_to_cpu(val);
  995. }
  996. uint64_t net_buf_simple_pull_be64(struct net_buf_simple *buf)
  997. {
  998. uint64_t val;
  999. val = UNALIGNED_GET((uint64_t *)buf->data);
  1000. net_buf_simple_pull(buf, sizeof(val));
  1001. return sys_be64_to_cpu(val);
  1002. }
  1003. size_t net_buf_simple_headroom(struct net_buf_simple *buf)
  1004. {
  1005. return buf->data - buf->__buf;
  1006. }
  1007. size_t net_buf_simple_tailroom(struct net_buf_simple *buf)
  1008. {
  1009. return buf->size - net_buf_simple_headroom(buf) - buf->len;
  1010. }
  1011. /* Actions add start */
  1012. /** Option for continue memory manager */
  1013. #ifndef CONFIG_LEGACY_TIMEOUT_API
  1014. #define K_TIMEOUT_GREATER(a, b) ((a).ticks > (b).ticks)
  1015. #define K_TIMEOUT_SUB(a, b) K_TICKS(((a).ticks - (b).ticks))
  1016. #else
  1017. #define K_TIMEOUT_GREATER(a, b) ((a) > (b))
  1018. #define K_TIMEOUT_SUB(a, b) ((a) - (b))
  1019. #endif
  1020. #define DATA_MEM_POOL_ALIGN (4)
  1021. #define DATA_MEM_POOL_USED BIT(0)
  1022. #define MEM_INIT_CHKSUM (0x5A)
  1023. #define DATA_MEM_USED(x) ((x)&DATA_MEM_POOL_USED)
  1024. #define DATA_MEM_FREE(x) (!DATA_MEM_USED(x))
  1025. #define DATA_MEM_SET_USED(x) do{ \
  1026. (x) = (x) | DATA_MEM_POOL_USED; \
  1027. } while(0)
  1028. #define DATA_MEM_SET_FREE(x) do{ \
  1029. (x) = (x)&(~DATA_MEM_POOL_USED); \
  1030. } while(0)
  1031. #define DATA_MEM_SET_CHKSUM(x) (((uint8_t *)(x))[3] = \
  1032. (((uint8_t *)(x))[0])^(((uint8_t *)(x))[1])^(((uint8_t *)(x))[2])^MEM_INIT_CHKSUM)
  1033. #define DATA_MEM_CHKSUM_CORRECT(x) ((((uint8_t *)(x))[3]) == \
  1034. ((((uint8_t *)(x))[0])^(((uint8_t *)(x))[1])^(((uint8_t *)(x))[2])^MEM_INIT_CHKSUM))
  1035. #define MEM_MALLOC_CHECK_INTERVAL (K_MSEC(5)) /* 5ms */
  1036. /* Continue memory manager struct */
  1037. struct pool_data_info {
  1038. uint16_t len; /* len include data info */
  1039. uint8_t flag;
  1040. uint8_t chksum;
  1041. };
  1042. #define DATA_INFO_SIZE sizeof(struct pool_data_info)
  1043. static void net_data_continue_pool_init(void *data_pool)
  1044. {
  1045. struct net_buf_pool_continue *pool = data_pool;
  1046. struct pool_data_info *info;
  1047. pool->alloc_pos = pool->data_pool;
  1048. info = (struct pool_data_info *)pool->alloc_pos;
  1049. memset(info, 0, DATA_INFO_SIZE);
  1050. info->len = pool->data_size;
  1051. DATA_MEM_SET_FREE(info->flag);
  1052. DATA_MEM_SET_CHKSUM(info);
  1053. #if defined(CONFIG_NET_BUF_POOL_USAGE)
  1054. pool->curr_used = 0;
  1055. pool->max_used = 0;
  1056. #endif
  1057. }
  1058. #define POOL_DEBUG_TEST 1
  1059. static uint8_t *continue_pool_malloc(struct net_buf_pool_continue *data_pool, uint16_t malloc_size)
  1060. {
  1061. unsigned int key;
  1062. uint8_t *sPos, *nPos, *mallocPos = NULL;
  1063. struct pool_data_info *sInfo, *nInfo;
  1064. bool merge;
  1065. key = irq_lock();
  1066. sPos = data_pool->alloc_pos;
  1067. #if POOL_DEBUG_TEST
  1068. uint16_t debug_run_times = 0;
  1069. uint8_t *debug_start_pos = sPos;
  1070. #endif
  1071. do {
  1072. merge = false;
  1073. sInfo = (struct pool_data_info *)sPos;
  1074. BUF_ASSERT(DATA_MEM_CHKSUM_CORRECT(sInfo), "chksum error");
  1075. #if POOL_DEBUG_TEST
  1076. debug_run_times++;
  1077. if (debug_run_times > 100) {
  1078. printk("pool_malloc len %d %p %d\n", malloc_size, data_pool->data_pool, data_pool->data_size);
  1079. printk("Start pos %p curr %p flag 0x%x len %d\n", debug_start_pos, sInfo, sInfo->flag, sInfo->len);
  1080. }
  1081. #endif
  1082. if (DATA_MEM_USED(sInfo->flag)) {
  1083. sPos += sInfo->len;
  1084. BUF_ASSERT(sPos <= (data_pool->data_pool + data_pool->data_size), "address error");
  1085. if (sPos == (data_pool->data_pool + data_pool->data_size)) {
  1086. sPos = data_pool->data_pool;
  1087. }
  1088. #if POOL_DEBUG_TEST
  1089. if (debug_run_times > 100) {
  1090. printk("debug1 %p %p %d\n", sPos, data_pool->alloc_pos, merge);
  1091. }
  1092. #endif
  1093. continue;
  1094. }
  1095. if (sInfo->len >= malloc_size) {
  1096. mallocPos = sPos;
  1097. break;
  1098. } else {
  1099. nPos = sPos + sInfo->len;
  1100. BUF_ASSERT(nPos <= (data_pool->data_pool + data_pool->data_size), "address error");
  1101. if (nPos == (data_pool->data_pool + data_pool->data_size)) {
  1102. sPos = data_pool->data_pool;
  1103. #if POOL_DEBUG_TEST
  1104. if (debug_run_times > 100) {
  1105. printk("debug2 %p %p %d\n", sPos, data_pool->alloc_pos, merge);
  1106. }
  1107. #endif
  1108. continue;
  1109. } else {
  1110. nInfo = (struct pool_data_info *)nPos;
  1111. BUF_ASSERT(DATA_MEM_CHKSUM_CORRECT(nInfo), "chksum error");
  1112. if (DATA_MEM_USED(nInfo->flag)) {
  1113. sPos = nPos;
  1114. #if POOL_DEBUG_TEST
  1115. if (debug_run_times > 100) {
  1116. printk("debug3 %p %p %d\n", sPos, data_pool->alloc_pos, merge);
  1117. }
  1118. #endif
  1119. continue;
  1120. } else {
  1121. sInfo->len += nInfo->len;
  1122. DATA_MEM_SET_CHKSUM(sInfo);
  1123. memset(nInfo, 0, DATA_INFO_SIZE);
  1124. merge = true;
  1125. if (data_pool->alloc_pos == (uint8_t *)nInfo) {
  1126. data_pool->alloc_pos = (uint8_t *)sInfo;
  1127. }
  1128. #if POOL_DEBUG_TEST
  1129. if (debug_run_times > 100) {
  1130. printk("debug4 %p %p %d\n", sPos, data_pool->alloc_pos, merge);
  1131. }
  1132. #endif
  1133. continue;
  1134. }
  1135. }
  1136. }
  1137. } while ((sPos != data_pool->alloc_pos) || merge);
  1138. if (mallocPos) {
  1139. sInfo = (struct pool_data_info *)mallocPos;
  1140. if (sInfo->len > malloc_size) {
  1141. nInfo = (struct pool_data_info *)(mallocPos + malloc_size);
  1142. memset(nInfo, 0, DATA_INFO_SIZE);
  1143. nInfo->len = sInfo->len - malloc_size;
  1144. DATA_MEM_SET_FREE(nInfo->flag);
  1145. DATA_MEM_SET_CHKSUM(nInfo);
  1146. }
  1147. if ((mallocPos + malloc_size) == (data_pool->data_pool + data_pool->data_size)) {
  1148. data_pool->alloc_pos = data_pool->data_pool;
  1149. } else {
  1150. data_pool->alloc_pos = mallocPos + malloc_size;
  1151. }
  1152. sInfo->len = malloc_size;
  1153. DATA_MEM_SET_USED(sInfo->flag);
  1154. DATA_MEM_SET_CHKSUM(sInfo);
  1155. #if defined(CONFIG_NET_BUF_POOL_USAGE)
  1156. data_pool->curr_used += malloc_size;
  1157. if (data_pool->curr_used > data_pool->max_used) {
  1158. data_pool->max_used = data_pool->curr_used;
  1159. BUF_LOG("data pool(%p), max_used: %d\n", data_pool, data_pool->max_used);
  1160. BUF_ASSERT((data_pool->max_used <= data_pool->data_size), "Error max used");
  1161. }
  1162. #endif
  1163. }
  1164. irq_unlock(key);
  1165. if (mallocPos) {
  1166. return (mallocPos + DATA_INFO_SIZE);
  1167. } else {
  1168. return NULL;
  1169. }
  1170. }
  1171. static void continue_pool_free(struct net_buf_pool_continue *data_pool, uint8_t *pData)
  1172. {
  1173. unsigned int key;
  1174. struct pool_data_info *info = (struct pool_data_info *)(pData - DATA_INFO_SIZE);
  1175. BUF_ASSERT(DATA_MEM_CHKSUM_CORRECT(info), "chksum error");
  1176. key = irq_lock();
  1177. DATA_MEM_SET_FREE(info->flag);
  1178. DATA_MEM_SET_CHKSUM(info);
  1179. #if defined(CONFIG_NET_BUF_POOL_USAGE)
  1180. data_pool->curr_used -= info->len;
  1181. #endif
  1182. irq_unlock(key);
  1183. }
  1184. #if BUF_TEST_DEBUG
  1185. static void dump_continue_pool(struct net_buf_pool_continue *pool, uint16_t malloc_size)
  1186. {
  1187. #if defined(CONFIG_NET_BUF_POOL_USAGE)
  1188. uint8_t *pos;
  1189. uint16_t cal_used_len = 0;
  1190. struct pool_data_info *info;
  1191. k_sched_lock();
  1192. BUF_LOG("%s\n", __func__);
  1193. BUF_LOG("data_pool: %p~%p, malloc_size: %d\n", pool->data_pool,
  1194. (pool->data_pool + pool->data_size), malloc_size);
  1195. BUF_LOG("size: %d, curr_used: %d, max_used: %d, alloc_pos: %p\n",
  1196. pool->data_size, pool->curr_used, pool->max_used, pool->alloc_pos);
  1197. pos = pool->data_pool;
  1198. BUF_LOG("address\t len\t flag\n");
  1199. while (pos < (pool->data_pool + pool->data_size)) {
  1200. info = (struct pool_data_info *)pos;
  1201. BUF_ASSERT(DATA_MEM_CHKSUM_CORRECT(info), "chksum error");
  1202. BUF_LOG("%p\t %d\t %d\n", info, info->len, info->flag);
  1203. if (DATA_MEM_USED(info->flag)) {
  1204. cal_used_len += info->len;
  1205. }
  1206. pos += info->len;
  1207. }
  1208. BUF_LOG("cal_used_len: %d\n", cal_used_len);
  1209. k_sched_unlock();
  1210. #endif
  1211. }
  1212. #endif
  1213. static uint8_t *net_data_continue_pool_malloc(struct net_buf_pool_continue *data_pool, uint16_t size, k_timeout_t timeout)
  1214. {
  1215. uint8_t *pData;
  1216. uint16_t malloc_size;
  1217. k_timeout_t sleep_time, remain_time = timeout;
  1218. BUF_ASSERT(data_pool, "data pool NULL");
  1219. malloc_size = size + DATA_INFO_SIZE;
  1220. malloc_size = ROUND_UP(malloc_size, DATA_MEM_POOL_ALIGN);
  1221. try_again:
  1222. pData = continue_pool_malloc(data_pool, malloc_size);
  1223. if (pData) {
  1224. return pData;
  1225. }
  1226. #if BUF_TEST_DEBUG
  1227. dump_continue_pool(data_pool, malloc_size);
  1228. #endif
  1229. if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
  1230. return pData;
  1231. } else if (K_TIMEOUT_EQ(timeout, K_FOREVER)) {
  1232. k_sleep(MEM_MALLOC_CHECK_INTERVAL);
  1233. goto try_again;
  1234. } else if (!K_TIMEOUT_EQ(remain_time, K_NO_WAIT)) {
  1235. sleep_time = K_TIMEOUT_GREATER(remain_time, MEM_MALLOC_CHECK_INTERVAL)? MEM_MALLOC_CHECK_INTERVAL : remain_time;
  1236. k_sleep(sleep_time);
  1237. remain_time = K_TIMEOUT_SUB(remain_time, sleep_time);
  1238. goto try_again;
  1239. } else {
  1240. return pData;
  1241. }
  1242. }
  1243. static void net_data_continue_pool_free(struct net_buf_pool_continue *data_pool, uint8_t *data)
  1244. {
  1245. BUF_ASSERT(data, "buf->__buf NULL");
  1246. BUF_ASSERT(data_pool, "data pool NULL");
  1247. continue_pool_free(data_pool, data);
  1248. }
  1249. static uint8_t *continue_pool_data_alloc(struct net_buf *buf, size_t *size,
  1250. k_timeout_t timeout)
  1251. {
  1252. struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
  1253. struct net_buf_pool_continue *data_pool = pool->alloc->alloc_data;
  1254. uint8_t *ref_count;
  1255. if (!data_pool->init_flag) {
  1256. net_data_continue_pool_init(data_pool);
  1257. data_pool->init_flag = 1;
  1258. }
  1259. ref_count = net_data_continue_pool_malloc(data_pool, (*size + 1), timeout);
  1260. if (ref_count == NULL) {
  1261. return NULL;
  1262. }
  1263. *ref_count = 1U;
  1264. /* Return pointer to the byte following the ref count */
  1265. return ref_count + 1;
  1266. }
  1267. static void continue_pool_data_unref(struct net_buf *buf, uint8_t *data)
  1268. {
  1269. struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
  1270. struct net_buf_pool_continue *data_pool = pool->alloc->alloc_data;
  1271. uint8_t *ref_count;
  1272. ref_count = data - 1;
  1273. if (--(*ref_count)) {
  1274. return;
  1275. }
  1276. net_data_continue_pool_free(data_pool, ref_count);
  1277. }
  1278. const struct net_buf_data_cb net_buf_continue_cb = {
  1279. .alloc = continue_pool_data_alloc,
  1280. .ref = generic_data_ref,
  1281. .unref = continue_pool_data_unref,
  1282. };
  1283. void net_buf_dumpinfo(void)
  1284. {
  1285. #if defined(CONFIG_NET_BUF_POOL_USAGE)
  1286. /* Need add _net_buf_pool_list_end to zephyr\include\linker\common-ram.ld */
  1287. extern struct net_buf_pool _net_buf_pool_list_end;
  1288. struct net_buf_pool *pool;
  1289. struct net_buf_pool_continue *data_pool;
  1290. printk("Net pool info\n");
  1291. printk("\t addr\t count\t avail\t max_used\t name\t use_data_pool\n");
  1292. for (pool = &_net_buf_pool_list[0];
  1293. pool < &_net_buf_pool_list_end; pool++) {
  1294. printk("pool: %p\t %d\t %d\t %d\t %s\n", pool, pool->buf_count,
  1295. pool->avail_count, pool->max_used, pool->name);
  1296. if (pool->alloc->cb == &net_buf_continue_cb) {
  1297. data_pool = pool->alloc->alloc_data;
  1298. printk("data_pool: %p\t %d\t %d\t\t %d\n", data_pool,
  1299. data_pool->data_size, data_pool->curr_used, data_pool->max_used);
  1300. }
  1301. }
  1302. #else
  1303. printk("CONFIG_NET_BUF_POOL_USAGE not set\n");
  1304. #endif
  1305. }
  1306. /* Actions add end */