1 /* buf.c - Buffer management */
2 
3 /*
4  * Copyright (c) 2015-2019 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define LOG_MODULE_NAME net_buf
10 #define LOG_LEVEL CONFIG_NET_BUF_LOG_LEVEL
11 
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
14 
15 #include <stdio.h>
16 #include <errno.h>
17 #include <stddef.h>
18 #include <string.h>
19 #include <zephyr/sys/byteorder.h>
20 
21 #include <zephyr/net/buf.h>
22 
23 #if defined(CONFIG_NET_BUF_LOG)
24 #define NET_BUF_DBG(fmt, ...) LOG_DBG("(%p) " fmt, k_current_get(), \
25 				      ##__VA_ARGS__)
26 #define NET_BUF_ERR(fmt, ...) LOG_ERR(fmt, ##__VA_ARGS__)
27 #define NET_BUF_WARN(fmt, ...) LOG_WRN(fmt, ##__VA_ARGS__)
28 #define NET_BUF_INFO(fmt, ...) LOG_INF(fmt, ##__VA_ARGS__)
29 #else
30 
31 #define NET_BUF_DBG(fmt, ...)
32 #define NET_BUF_ERR(fmt, ...)
33 #define NET_BUF_WARN(fmt, ...)
34 #define NET_BUF_INFO(fmt, ...)
35 #endif /* CONFIG_NET_BUF_LOG */
36 
37 #define NET_BUF_ASSERT(cond, ...) __ASSERT(cond, "" __VA_ARGS__)
38 
39 #if CONFIG_NET_BUF_WARN_ALLOC_INTERVAL > 0
40 #define WARN_ALLOC_INTERVAL K_SECONDS(CONFIG_NET_BUF_WARN_ALLOC_INTERVAL)
41 #else
42 #define WARN_ALLOC_INTERVAL K_FOREVER
43 #endif
44 
45 /* Linker-defined symbol bound to the static pool structs */
46 STRUCT_SECTION_START_EXTERN(net_buf_pool);
47 
net_buf_pool_get(int id)48 struct net_buf_pool *net_buf_pool_get(int id)
49 {
50 	struct net_buf_pool *pool;
51 
52 	STRUCT_SECTION_GET(net_buf_pool, id, &pool);
53 
54 	return pool;
55 }
56 
pool_id(struct net_buf_pool * pool)57 static int pool_id(struct net_buf_pool *pool)
58 {
59 	return pool - TYPE_SECTION_START(net_buf_pool);
60 }
61 
net_buf_id(const struct net_buf * buf)62 int net_buf_id(const struct net_buf *buf)
63 {
64 	struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
65 	size_t struct_size = ROUND_UP(sizeof(struct net_buf) + pool->user_data_size,
66 				__alignof__(struct net_buf));
67 	ptrdiff_t offset = (uint8_t *)buf - (uint8_t *)pool->__bufs;
68 
69 	return offset / struct_size;
70 }
71 
pool_get_uninit(struct net_buf_pool * pool,uint16_t uninit_count)72 static inline struct net_buf *pool_get_uninit(struct net_buf_pool *pool,
73 					      uint16_t uninit_count)
74 {
75 	size_t struct_size = ROUND_UP(sizeof(struct net_buf) + pool->user_data_size,
76 				__alignof__(struct net_buf));
77 	size_t byte_offset = (pool->buf_count - uninit_count) * struct_size;
78 	struct net_buf *buf;
79 
80 	buf = (struct net_buf *)(((uint8_t *)pool->__bufs) + byte_offset);
81 
82 	buf->pool_id = pool_id(pool);
83 	buf->user_data_size = pool->user_data_size;
84 
85 	return buf;
86 }
87 
net_buf_reset(struct net_buf * buf)88 void net_buf_reset(struct net_buf *buf)
89 {
90 	__ASSERT_NO_MSG(buf->flags == 0U);
91 	__ASSERT_NO_MSG(buf->frags == NULL);
92 
93 	net_buf_simple_reset(&buf->b);
94 }
95 
generic_data_ref(struct net_buf * buf,uint8_t * data)96 static uint8_t *generic_data_ref(struct net_buf *buf, uint8_t *data)
97 {
98 	uint8_t *ref_count;
99 
100 	ref_count = data - sizeof(void *);
101 	(*ref_count)++;
102 
103 	return data;
104 }
105 
mem_pool_data_alloc(struct net_buf * buf,size_t * size,k_timeout_t timeout)106 static uint8_t *mem_pool_data_alloc(struct net_buf *buf, size_t *size,
107 				 k_timeout_t timeout)
108 {
109 	struct net_buf_pool *buf_pool = net_buf_pool_get(buf->pool_id);
110 	struct k_heap *pool = buf_pool->alloc->alloc_data;
111 	uint8_t *ref_count;
112 
113 	/* Reserve extra space for a ref-count (uint8_t) */
114 	void *b = k_heap_alloc(pool, sizeof(void *) + *size, timeout);
115 
116 	if (b == NULL) {
117 		return NULL;
118 	}
119 
120 	ref_count = (uint8_t *)b;
121 	*ref_count = 1U;
122 
123 	/* Return pointer to the byte following the ref count */
124 	return ref_count + sizeof(void *);
125 }
126 
mem_pool_data_unref(struct net_buf * buf,uint8_t * data)127 static void mem_pool_data_unref(struct net_buf *buf, uint8_t *data)
128 {
129 	struct net_buf_pool *buf_pool = net_buf_pool_get(buf->pool_id);
130 	struct k_heap *pool = buf_pool->alloc->alloc_data;
131 	uint8_t *ref_count;
132 
133 	ref_count = data - sizeof(void *);
134 	if (--(*ref_count)) {
135 		return;
136 	}
137 
138 	/* Need to copy to local variable due to alignment */
139 	k_heap_free(pool, ref_count);
140 }
141 
142 const struct net_buf_data_cb net_buf_var_cb = {
143 	.alloc = mem_pool_data_alloc,
144 	.ref   = generic_data_ref,
145 	.unref = mem_pool_data_unref,
146 };
147 
fixed_data_alloc(struct net_buf * buf,size_t * size,k_timeout_t timeout)148 static uint8_t *fixed_data_alloc(struct net_buf *buf, size_t *size,
149 			      k_timeout_t timeout)
150 {
151 	struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
152 	const struct net_buf_pool_fixed *fixed = pool->alloc->alloc_data;
153 
154 	*size = pool->alloc->max_alloc_size;
155 
156 	return fixed->data_pool + *size * net_buf_id(buf);
157 }
158 
fixed_data_unref(struct net_buf * buf,uint8_t * data)159 static void fixed_data_unref(struct net_buf *buf, uint8_t *data)
160 {
161 	/* Nothing needed for fixed-size data pools */
162 }
163 
164 const struct net_buf_data_cb net_buf_fixed_cb = {
165 	.alloc = fixed_data_alloc,
166 	.unref = fixed_data_unref,
167 };
168 
169 #if (K_HEAP_MEM_POOL_SIZE > 0)
170 
heap_data_alloc(struct net_buf * buf,size_t * size,k_timeout_t timeout)171 static uint8_t *heap_data_alloc(struct net_buf *buf, size_t *size,
172 			     k_timeout_t timeout)
173 {
174 	uint8_t *ref_count;
175 
176 	ref_count = k_malloc(sizeof(void *) + *size);
177 	if (!ref_count) {
178 		return NULL;
179 	}
180 
181 	*ref_count = 1U;
182 
183 	return ref_count + sizeof(void *);
184 }
185 
heap_data_unref(struct net_buf * buf,uint8_t * data)186 static void heap_data_unref(struct net_buf *buf, uint8_t *data)
187 {
188 	uint8_t *ref_count;
189 
190 	ref_count = data - sizeof(void *);
191 	if (--(*ref_count)) {
192 		return;
193 	}
194 
195 	k_free(ref_count);
196 }
197 
198 static const struct net_buf_data_cb net_buf_heap_cb = {
199 	.alloc = heap_data_alloc,
200 	.ref   = generic_data_ref,
201 	.unref = heap_data_unref,
202 };
203 
204 const struct net_buf_data_alloc net_buf_heap_alloc = {
205 	.cb = &net_buf_heap_cb,
206 	.max_alloc_size = 0,
207 };
208 
209 #endif /* K_HEAP_MEM_POOL_SIZE > 0 */
210 
data_alloc(struct net_buf * buf,size_t * size,k_timeout_t timeout)211 static uint8_t *data_alloc(struct net_buf *buf, size_t *size, k_timeout_t timeout)
212 {
213 	struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
214 
215 	return pool->alloc->cb->alloc(buf, size, timeout);
216 }
217 
data_ref(struct net_buf * buf,uint8_t * data)218 static uint8_t *data_ref(struct net_buf *buf, uint8_t *data)
219 {
220 	struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
221 
222 	return pool->alloc->cb->ref(buf, data);
223 }
224 
225 #if defined(CONFIG_NET_BUF_LOG)
net_buf_alloc_len_debug(struct net_buf_pool * pool,size_t size,k_timeout_t timeout,const char * func,int line)226 struct net_buf *net_buf_alloc_len_debug(struct net_buf_pool *pool, size_t size,
227 					k_timeout_t timeout, const char *func,
228 					int line)
229 #else
230 struct net_buf *net_buf_alloc_len(struct net_buf_pool *pool, size_t size,
231 				  k_timeout_t timeout)
232 #endif
233 {
234 	k_timepoint_t end = sys_timepoint_calc(timeout);
235 	struct net_buf *buf;
236 	k_spinlock_key_t key;
237 
238 	__ASSERT_NO_MSG(pool);
239 
240 	NET_BUF_DBG("%s():%d: pool %p size %zu", func, line, pool, size);
241 
242 	/* We need to prevent race conditions
243 	 * when accessing pool->uninit_count.
244 	 */
245 	key = k_spin_lock(&pool->lock);
246 
247 	/* If there are uninitialized buffers we're guaranteed to succeed
248 	 * with the allocation one way or another.
249 	 */
250 	if (pool->uninit_count) {
251 		uint16_t uninit_count;
252 
253 		/* If this is not the first access to the pool, we can
254 		 * be opportunistic and try to fetch a previously used
255 		 * buffer from the LIFO with K_NO_WAIT.
256 		 */
257 		if (pool->uninit_count < pool->buf_count) {
258 			buf = k_lifo_get(&pool->free, K_NO_WAIT);
259 			if (buf) {
260 				k_spin_unlock(&pool->lock, key);
261 				goto success;
262 			}
263 		}
264 
265 		uninit_count = pool->uninit_count--;
266 		k_spin_unlock(&pool->lock, key);
267 
268 		buf = pool_get_uninit(pool, uninit_count);
269 		goto success;
270 	}
271 
272 	k_spin_unlock(&pool->lock, key);
273 
274 	if (!K_TIMEOUT_EQ(timeout, K_NO_WAIT) &&
275 	    k_current_get() == k_work_queue_thread_get(&k_sys_work_q)) {
276 		LOG_DBG("Timeout discarded. No blocking in syswq");
277 		timeout = K_NO_WAIT;
278 	}
279 
280 #if defined(CONFIG_NET_BUF_LOG) && (CONFIG_NET_BUF_LOG_LEVEL >= LOG_LEVEL_WRN)
281 	if (K_TIMEOUT_EQ(timeout, K_FOREVER)) {
282 		uint32_t ref = k_uptime_get_32();
283 		buf = k_lifo_get(&pool->free, K_NO_WAIT);
284 		while (!buf) {
285 #if defined(CONFIG_NET_BUF_POOL_USAGE)
286 			NET_BUF_WARN("%s():%d: Pool %s low on buffers.",
287 				     func, line, pool->name);
288 #else
289 			NET_BUF_WARN("%s():%d: Pool %p low on buffers.",
290 				     func, line, pool);
291 #endif
292 			buf = k_lifo_get(&pool->free, WARN_ALLOC_INTERVAL);
293 #if defined(CONFIG_NET_BUF_POOL_USAGE)
294 			NET_BUF_WARN("%s():%d: Pool %s blocked for %u secs",
295 				     func, line, pool->name,
296 				     (k_uptime_get_32() - ref) / MSEC_PER_SEC);
297 #else
298 			NET_BUF_WARN("%s():%d: Pool %p blocked for %u secs",
299 				     func, line, pool,
300 				     (k_uptime_get_32() - ref) / MSEC_PER_SEC);
301 #endif
302 		}
303 	} else {
304 		buf = k_lifo_get(&pool->free, timeout);
305 	}
306 #else
307 	buf = k_lifo_get(&pool->free, timeout);
308 #endif
309 	if (!buf) {
310 		NET_BUF_ERR("%s():%d: Failed to get free buffer", func, line);
311 		return NULL;
312 	}
313 
314 success:
315 	NET_BUF_DBG("allocated buf %p", buf);
316 
317 	if (size) {
318 #if __ASSERT_ON
319 		size_t req_size = size;
320 #endif
321 		timeout = sys_timepoint_timeout(end);
322 		buf->__buf = data_alloc(buf, &size, timeout);
323 		if (!buf->__buf) {
324 			NET_BUF_ERR("%s():%d: Failed to allocate data",
325 				    func, line);
326 			net_buf_destroy(buf);
327 			return NULL;
328 		}
329 
330 #if __ASSERT_ON
331 		NET_BUF_ASSERT(req_size <= size);
332 #endif
333 	} else {
334 		buf->__buf = NULL;
335 	}
336 
337 	buf->ref   = 1U;
338 	buf->flags = 0U;
339 	buf->frags = NULL;
340 	buf->size  = size;
341 	net_buf_reset(buf);
342 
343 #if defined(CONFIG_NET_BUF_POOL_USAGE)
344 	atomic_dec(&pool->avail_count);
345 	__ASSERT_NO_MSG(atomic_get(&pool->avail_count) >= 0);
346 #endif
347 	return buf;
348 }
349 
350 #if defined(CONFIG_NET_BUF_LOG)
net_buf_alloc_fixed_debug(struct net_buf_pool * pool,k_timeout_t timeout,const char * func,int line)351 struct net_buf *net_buf_alloc_fixed_debug(struct net_buf_pool *pool,
352 					  k_timeout_t timeout, const char *func,
353 					  int line)
354 {
355 	return net_buf_alloc_len_debug(pool, pool->alloc->max_alloc_size, timeout, func,
356 				       line);
357 }
358 #else
net_buf_alloc_fixed(struct net_buf_pool * pool,k_timeout_t timeout)359 struct net_buf *net_buf_alloc_fixed(struct net_buf_pool *pool,
360 				    k_timeout_t timeout)
361 {
362 	return net_buf_alloc_len(pool, pool->alloc->max_alloc_size, timeout);
363 }
364 #endif
365 
366 #if defined(CONFIG_NET_BUF_LOG)
net_buf_alloc_with_data_debug(struct net_buf_pool * pool,void * data,size_t size,k_timeout_t timeout,const char * func,int line)367 struct net_buf *net_buf_alloc_with_data_debug(struct net_buf_pool *pool,
368 					      void *data, size_t size,
369 					      k_timeout_t timeout,
370 					      const char *func, int line)
371 #else
372 struct net_buf *net_buf_alloc_with_data(struct net_buf_pool *pool,
373 					void *data, size_t size,
374 					k_timeout_t timeout)
375 #endif
376 {
377 	struct net_buf *buf;
378 
379 #if defined(CONFIG_NET_BUF_LOG)
380 	buf = net_buf_alloc_len_debug(pool, 0, timeout, func, line);
381 #else
382 	buf = net_buf_alloc_len(pool, 0, timeout);
383 #endif
384 	if (!buf) {
385 		return NULL;
386 	}
387 
388 	net_buf_simple_init_with_data(&buf->b, data, size);
389 	buf->flags = NET_BUF_EXTERNAL_DATA;
390 
391 	return buf;
392 }
393 
394 #if defined(CONFIG_NET_BUF_LOG)
net_buf_get_debug(struct k_fifo * fifo,k_timeout_t timeout,const char * func,int line)395 struct net_buf *net_buf_get_debug(struct k_fifo *fifo, k_timeout_t timeout,
396 				  const char *func, int line)
397 #else
398 struct net_buf *net_buf_get(struct k_fifo *fifo, k_timeout_t timeout)
399 #endif
400 {
401 	struct net_buf *buf;
402 
403 	NET_BUF_DBG("%s():%d: fifo %p", func, line, fifo);
404 
405 	buf = k_fifo_get(fifo, timeout);
406 	if (!buf) {
407 		return NULL;
408 	}
409 
410 	NET_BUF_DBG("%s():%d: buf %p fifo %p", func, line, buf, fifo);
411 
412 	return buf;
413 }
414 
415 static struct k_spinlock net_buf_slist_lock;
416 
net_buf_slist_put(sys_slist_t * list,struct net_buf * buf)417 void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf)
418 {
419 	k_spinlock_key_t key;
420 
421 	__ASSERT_NO_MSG(list);
422 	__ASSERT_NO_MSG(buf);
423 
424 	key = k_spin_lock(&net_buf_slist_lock);
425 	sys_slist_append(list, &buf->node);
426 	k_spin_unlock(&net_buf_slist_lock, key);
427 }
428 
net_buf_slist_get(sys_slist_t * list)429 struct net_buf *net_buf_slist_get(sys_slist_t *list)
430 {
431 	struct net_buf *buf;
432 	k_spinlock_key_t key;
433 
434 	__ASSERT_NO_MSG(list);
435 
436 	key = k_spin_lock(&net_buf_slist_lock);
437 
438 	buf = (void *)sys_slist_get(list);
439 
440 	k_spin_unlock(&net_buf_slist_lock, key);
441 
442 	return buf;
443 }
444 
net_buf_put(struct k_fifo * fifo,struct net_buf * buf)445 void net_buf_put(struct k_fifo *fifo, struct net_buf *buf)
446 {
447 	__ASSERT_NO_MSG(fifo);
448 	__ASSERT_NO_MSG(buf);
449 
450 	k_fifo_put(fifo, buf);
451 }
452 
453 #if defined(CONFIG_NET_BUF_LOG)
net_buf_unref_debug(struct net_buf * buf,const char * func,int line)454 void net_buf_unref_debug(struct net_buf *buf, const char *func, int line)
455 #else
456 void net_buf_unref(struct net_buf *buf)
457 #endif
458 {
459 	__ASSERT_NO_MSG(buf);
460 
461 	while (buf) {
462 		struct net_buf *frags = buf->frags;
463 		struct net_buf_pool *pool;
464 
465 #if defined(CONFIG_NET_BUF_LOG)
466 		if (!buf->ref) {
467 			NET_BUF_ERR("%s():%d: buf %p double free", func, line,
468 				    buf);
469 			return;
470 		}
471 #endif
472 		NET_BUF_DBG("buf %p ref %u pool_id %u frags %p", buf, buf->ref,
473 			    buf->pool_id, buf->frags);
474 
475 		if (--buf->ref > 0) {
476 			return;
477 		}
478 
479 		buf->data = NULL;
480 		buf->frags = NULL;
481 
482 		pool = net_buf_pool_get(buf->pool_id);
483 
484 #if defined(CONFIG_NET_BUF_POOL_USAGE)
485 		atomic_inc(&pool->avail_count);
486 		__ASSERT_NO_MSG(atomic_get(&pool->avail_count) <= pool->buf_count);
487 #endif
488 
489 		if (pool->destroy) {
490 			pool->destroy(buf);
491 		} else {
492 			net_buf_destroy(buf);
493 		}
494 
495 		buf = frags;
496 	}
497 }
498 
net_buf_ref(struct net_buf * buf)499 struct net_buf *net_buf_ref(struct net_buf *buf)
500 {
501 	__ASSERT_NO_MSG(buf);
502 
503 	NET_BUF_DBG("buf %p (old) ref %u pool_id %u",
504 		    buf, buf->ref, buf->pool_id);
505 	buf->ref++;
506 	return buf;
507 }
508 
net_buf_clone(struct net_buf * buf,k_timeout_t timeout)509 struct net_buf *net_buf_clone(struct net_buf *buf, k_timeout_t timeout)
510 {
511 	k_timepoint_t end = sys_timepoint_calc(timeout);
512 	struct net_buf_pool *pool;
513 	struct net_buf *clone;
514 
515 	__ASSERT_NO_MSG(buf);
516 
517 	pool = net_buf_pool_get(buf->pool_id);
518 
519 	clone = net_buf_alloc_len(pool, 0, timeout);
520 	if (!clone) {
521 		return NULL;
522 	}
523 
524 	/* If the pool supports data referencing use that. Otherwise
525 	 * we need to allocate new data and make a copy.
526 	 */
527 	if (pool->alloc->cb->ref && !(buf->flags & NET_BUF_EXTERNAL_DATA)) {
528 		clone->__buf = buf->__buf ? data_ref(buf, buf->__buf) : NULL;
529 		clone->data = buf->data;
530 		clone->len = buf->len;
531 		clone->size = buf->size;
532 	} else {
533 		size_t size = buf->size;
534 
535 		timeout = sys_timepoint_timeout(end);
536 
537 		clone->__buf = data_alloc(clone, &size, timeout);
538 		if (!clone->__buf || size < buf->size) {
539 			net_buf_destroy(clone);
540 			return NULL;
541 		}
542 
543 		clone->size = size;
544 		clone->data = clone->__buf + net_buf_headroom(buf);
545 		net_buf_add_mem(clone, buf->data, buf->len);
546 	}
547 
548 	/* user_data_size should be the same for buffers from the same pool */
549 	__ASSERT(buf->user_data_size == clone->user_data_size, "Unexpected user data size");
550 
551 	memcpy(clone->user_data, buf->user_data, clone->user_data_size);
552 
553 	return clone;
554 }
555 
net_buf_user_data_copy(struct net_buf * dst,const struct net_buf * src)556 int net_buf_user_data_copy(struct net_buf *dst, const struct net_buf *src)
557 {
558 	__ASSERT_NO_MSG(dst);
559 	__ASSERT_NO_MSG(src);
560 
561 	if (dst == src) {
562 		return 0;
563 	}
564 
565 	if (dst->user_data_size < src->user_data_size) {
566 		return -EINVAL;
567 	}
568 
569 	memcpy(dst->user_data, src->user_data, src->user_data_size);
570 
571 	return 0;
572 }
573 
net_buf_frag_last(struct net_buf * buf)574 struct net_buf *net_buf_frag_last(struct net_buf *buf)
575 {
576 	__ASSERT_NO_MSG(buf);
577 
578 	while (buf->frags) {
579 		buf = buf->frags;
580 	}
581 
582 	return buf;
583 }
584 
net_buf_frag_insert(struct net_buf * parent,struct net_buf * frag)585 void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag)
586 {
587 	__ASSERT_NO_MSG(parent);
588 	__ASSERT_NO_MSG(frag);
589 
590 	if (parent->frags) {
591 		net_buf_frag_last(frag)->frags = parent->frags;
592 	}
593 	/* Take ownership of the fragment reference */
594 	parent->frags = frag;
595 }
596 
net_buf_frag_add(struct net_buf * head,struct net_buf * frag)597 struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag)
598 {
599 	__ASSERT_NO_MSG(frag);
600 
601 	if (!head) {
602 		return net_buf_ref(frag);
603 	}
604 
605 	net_buf_frag_insert(net_buf_frag_last(head), frag);
606 
607 	return head;
608 }
609 
610 #if defined(CONFIG_NET_BUF_LOG)
net_buf_frag_del_debug(struct net_buf * parent,struct net_buf * frag,const char * func,int line)611 struct net_buf *net_buf_frag_del_debug(struct net_buf *parent,
612 				       struct net_buf *frag,
613 				       const char *func, int line)
614 #else
615 struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag)
616 #endif
617 {
618 	struct net_buf *next_frag;
619 
620 	__ASSERT_NO_MSG(frag);
621 
622 	if (parent) {
623 		__ASSERT_NO_MSG(parent->frags);
624 		__ASSERT_NO_MSG(parent->frags == frag);
625 		parent->frags = frag->frags;
626 	}
627 
628 	next_frag = frag->frags;
629 
630 	frag->frags = NULL;
631 
632 #if defined(CONFIG_NET_BUF_LOG)
633 	net_buf_unref_debug(frag, func, line);
634 #else
635 	net_buf_unref(frag);
636 #endif
637 
638 	return next_frag;
639 }
640 
net_buf_linearize(void * dst,size_t dst_len,const struct net_buf * src,size_t offset,size_t len)641 size_t net_buf_linearize(void *dst, size_t dst_len, const struct net_buf *src,
642 			 size_t offset, size_t len)
643 {
644 	const struct net_buf *frag;
645 	size_t to_copy;
646 	size_t copied;
647 
648 	len = MIN(len, dst_len);
649 
650 	frag = src;
651 
652 	/* find the right fragment to start copying from */
653 	while (frag && offset >= frag->len) {
654 		offset -= frag->len;
655 		frag = frag->frags;
656 	}
657 
658 	/* traverse the fragment chain until len bytes are copied */
659 	copied = 0;
660 	while (frag && len > 0) {
661 		to_copy = MIN(len, frag->len - offset);
662 		memcpy((uint8_t *)dst + copied, frag->data + offset, to_copy);
663 
664 		copied += to_copy;
665 
666 		/* to_copy is always <= len */
667 		len -= to_copy;
668 		frag = frag->frags;
669 
670 		/* after the first iteration, this value will be 0 */
671 		offset = 0;
672 	}
673 
674 	return copied;
675 }
676 
677 /* This helper routine will append multiple bytes, if there is no place for
678  * the data in current fragment then create new fragment and add it to
679  * the buffer. It assumes that the buffer has at least one fragment.
680  */
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)681 size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
682 			    const void *value, k_timeout_t timeout,
683 			    net_buf_allocator_cb allocate_cb, void *user_data)
684 {
685 	struct net_buf *frag = net_buf_frag_last(buf);
686 	size_t added_len = 0;
687 	const uint8_t *value8 = value;
688 	size_t max_size;
689 
690 	do {
691 		uint16_t count = MIN(len, net_buf_tailroom(frag));
692 
693 		net_buf_add_mem(frag, value8, count);
694 		len -= count;
695 		added_len += count;
696 		value8 += count;
697 
698 		if (len == 0) {
699 			return added_len;
700 		}
701 
702 		if (allocate_cb) {
703 			frag = allocate_cb(timeout, user_data);
704 		} else {
705 			struct net_buf_pool *pool;
706 
707 			/* Allocate from the original pool if no callback has
708 			 * been provided.
709 			 */
710 			pool = net_buf_pool_get(buf->pool_id);
711 			max_size = pool->alloc->max_alloc_size;
712 			frag = net_buf_alloc_len(pool,
713 						 max_size ? MIN(len, max_size) : len,
714 						 timeout);
715 		}
716 
717 		if (!frag) {
718 			return added_len;
719 		}
720 
721 		net_buf_frag_add(buf, frag);
722 	} while (1);
723 
724 	/* Unreachable */
725 	return 0;
726 }
727 
net_buf_data_match(const struct net_buf * buf,size_t offset,const void * data,size_t len)728 size_t net_buf_data_match(const struct net_buf *buf, size_t offset, const void *data, size_t len)
729 {
730 	const uint8_t *dptr = data;
731 	const uint8_t *bptr;
732 	size_t compared = 0;
733 	size_t to_compare;
734 
735 	if (!buf || !data) {
736 		return compared;
737 	}
738 
739 	/* find the right fragment to start comparison */
740 	while (buf && offset >= buf->len) {
741 		offset -= buf->len;
742 		buf = buf->frags;
743 	}
744 
745 	while (buf && len > 0) {
746 		bptr = buf->data + offset;
747 		to_compare = MIN(len, buf->len - offset);
748 
749 		for (size_t i = 0; i < to_compare; ++i) {
750 			if (dptr[compared] != bptr[i]) {
751 				return compared;
752 			}
753 			compared++;
754 		}
755 
756 		len -= to_compare;
757 		buf = buf->frags;
758 		offset = 0;
759 	}
760 
761 	return compared;
762 }
763