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_WRN("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 	memset(buf->user_data, 0, buf->user_data_size);
342 	net_buf_reset(buf);
343 
344 #if defined(CONFIG_NET_BUF_POOL_USAGE)
345 	atomic_dec(&pool->avail_count);
346 	__ASSERT_NO_MSG(atomic_get(&pool->avail_count) >= 0);
347 #endif
348 	return buf;
349 }
350 
351 #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)352 struct net_buf *net_buf_alloc_fixed_debug(struct net_buf_pool *pool,
353 					  k_timeout_t timeout, const char *func,
354 					  int line)
355 {
356 	return net_buf_alloc_len_debug(pool, pool->alloc->max_alloc_size, timeout, func,
357 				       line);
358 }
359 #else
net_buf_alloc_fixed(struct net_buf_pool * pool,k_timeout_t timeout)360 struct net_buf *net_buf_alloc_fixed(struct net_buf_pool *pool,
361 				    k_timeout_t timeout)
362 {
363 	return net_buf_alloc_len(pool, pool->alloc->max_alloc_size, timeout);
364 }
365 #endif
366 
367 #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)368 struct net_buf *net_buf_alloc_with_data_debug(struct net_buf_pool *pool,
369 					      void *data, size_t size,
370 					      k_timeout_t timeout,
371 					      const char *func, int line)
372 #else
373 struct net_buf *net_buf_alloc_with_data(struct net_buf_pool *pool,
374 					void *data, size_t size,
375 					k_timeout_t timeout)
376 #endif
377 {
378 	struct net_buf *buf;
379 
380 #if defined(CONFIG_NET_BUF_LOG)
381 	buf = net_buf_alloc_len_debug(pool, 0, timeout, func, line);
382 #else
383 	buf = net_buf_alloc_len(pool, 0, timeout);
384 #endif
385 	if (!buf) {
386 		return NULL;
387 	}
388 
389 	net_buf_simple_init_with_data(&buf->b, data, size);
390 	buf->flags = NET_BUF_EXTERNAL_DATA;
391 
392 	return buf;
393 }
394 
395 #if defined(CONFIG_NET_BUF_LOG)
net_buf_get_debug(struct k_fifo * fifo,k_timeout_t timeout,const char * func,int line)396 struct net_buf *net_buf_get_debug(struct k_fifo *fifo, k_timeout_t timeout,
397 				  const char *func, int line)
398 #else
399 struct net_buf *net_buf_get(struct k_fifo *fifo, k_timeout_t timeout)
400 #endif
401 {
402 	struct net_buf *buf;
403 
404 	NET_BUF_DBG("%s():%d: fifo %p", func, line, fifo);
405 
406 	buf = k_fifo_get(fifo, timeout);
407 	if (!buf) {
408 		return NULL;
409 	}
410 
411 	NET_BUF_DBG("%s():%d: buf %p fifo %p", func, line, buf, fifo);
412 
413 	return buf;
414 }
415 
416 static struct k_spinlock net_buf_slist_lock;
417 
net_buf_slist_put(sys_slist_t * list,struct net_buf * buf)418 void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf)
419 {
420 	k_spinlock_key_t key;
421 
422 	__ASSERT_NO_MSG(list);
423 	__ASSERT_NO_MSG(buf);
424 
425 	key = k_spin_lock(&net_buf_slist_lock);
426 	sys_slist_append(list, &buf->node);
427 	k_spin_unlock(&net_buf_slist_lock, key);
428 }
429 
net_buf_slist_get(sys_slist_t * list)430 struct net_buf *net_buf_slist_get(sys_slist_t *list)
431 {
432 	struct net_buf *buf;
433 	k_spinlock_key_t key;
434 
435 	__ASSERT_NO_MSG(list);
436 
437 	key = k_spin_lock(&net_buf_slist_lock);
438 
439 	buf = (void *)sys_slist_get(list);
440 
441 	k_spin_unlock(&net_buf_slist_lock, key);
442 
443 	return buf;
444 }
445 
net_buf_put(struct k_fifo * fifo,struct net_buf * buf)446 void net_buf_put(struct k_fifo *fifo, struct net_buf *buf)
447 {
448 	__ASSERT_NO_MSG(fifo);
449 	__ASSERT_NO_MSG(buf);
450 
451 	k_fifo_put(fifo, buf);
452 }
453 
454 #if defined(CONFIG_NET_BUF_LOG)
net_buf_unref_debug(struct net_buf * buf,const char * func,int line)455 void net_buf_unref_debug(struct net_buf *buf, const char *func, int line)
456 #else
457 void net_buf_unref(struct net_buf *buf)
458 #endif
459 {
460 	__ASSERT_NO_MSG(buf);
461 
462 	while (buf) {
463 		struct net_buf *frags = buf->frags;
464 		struct net_buf_pool *pool;
465 
466 #if defined(CONFIG_NET_BUF_LOG)
467 		if (!buf->ref) {
468 			NET_BUF_ERR("%s():%d: buf %p double free", func, line,
469 				    buf);
470 			return;
471 		}
472 #endif
473 		NET_BUF_DBG("buf %p ref %u pool_id %u frags %p", buf, buf->ref,
474 			    buf->pool_id, buf->frags);
475 
476 		if (--buf->ref > 0) {
477 			return;
478 		}
479 
480 		buf->data = NULL;
481 		buf->frags = NULL;
482 
483 		pool = net_buf_pool_get(buf->pool_id);
484 
485 #if defined(CONFIG_NET_BUF_POOL_USAGE)
486 		atomic_inc(&pool->avail_count);
487 		__ASSERT_NO_MSG(atomic_get(&pool->avail_count) <= pool->buf_count);
488 #endif
489 
490 		if (pool->destroy) {
491 			pool->destroy(buf);
492 		} else {
493 			net_buf_destroy(buf);
494 		}
495 
496 		buf = frags;
497 	}
498 }
499 
net_buf_ref(struct net_buf * buf)500 struct net_buf *net_buf_ref(struct net_buf *buf)
501 {
502 	__ASSERT_NO_MSG(buf);
503 
504 	NET_BUF_DBG("buf %p (old) ref %u pool_id %u",
505 		    buf, buf->ref, buf->pool_id);
506 	buf->ref++;
507 	return buf;
508 }
509 
net_buf_clone(struct net_buf * buf,k_timeout_t timeout)510 struct net_buf *net_buf_clone(struct net_buf *buf, k_timeout_t timeout)
511 {
512 	k_timepoint_t end = sys_timepoint_calc(timeout);
513 	struct net_buf_pool *pool;
514 	struct net_buf *clone;
515 
516 	__ASSERT_NO_MSG(buf);
517 
518 	pool = net_buf_pool_get(buf->pool_id);
519 
520 	clone = net_buf_alloc_len(pool, 0, timeout);
521 	if (!clone) {
522 		return NULL;
523 	}
524 
525 	/* If the pool supports data referencing use that. Otherwise
526 	 * we need to allocate new data and make a copy.
527 	 */
528 	if (pool->alloc->cb->ref && !(buf->flags & NET_BUF_EXTERNAL_DATA)) {
529 		clone->__buf = buf->__buf ? data_ref(buf, buf->__buf) : NULL;
530 		clone->data = buf->data;
531 		clone->len = buf->len;
532 		clone->size = buf->size;
533 	} else {
534 		size_t size = buf->size;
535 
536 		timeout = sys_timepoint_timeout(end);
537 
538 		clone->__buf = data_alloc(clone, &size, timeout);
539 		if (!clone->__buf || size < buf->size) {
540 			net_buf_destroy(clone);
541 			return NULL;
542 		}
543 
544 		clone->size = size;
545 		clone->data = clone->__buf + net_buf_headroom(buf);
546 		net_buf_add_mem(clone, buf->data, buf->len);
547 	}
548 
549 	/* user_data_size should be the same for buffers from the same pool */
550 	__ASSERT(buf->user_data_size == clone->user_data_size, "Unexpected user data size");
551 
552 	memcpy(clone->user_data, buf->user_data, clone->user_data_size);
553 
554 	return clone;
555 }
556 
net_buf_user_data_copy(struct net_buf * dst,const struct net_buf * src)557 int net_buf_user_data_copy(struct net_buf *dst, const struct net_buf *src)
558 {
559 	__ASSERT_NO_MSG(dst);
560 	__ASSERT_NO_MSG(src);
561 
562 	if (dst == src) {
563 		return 0;
564 	}
565 
566 	if (dst->user_data_size < src->user_data_size) {
567 		return -EINVAL;
568 	}
569 
570 	memcpy(dst->user_data, src->user_data, src->user_data_size);
571 
572 	return 0;
573 }
574 
net_buf_frag_last(struct net_buf * buf)575 struct net_buf *net_buf_frag_last(struct net_buf *buf)
576 {
577 	__ASSERT_NO_MSG(buf);
578 
579 	while (buf->frags) {
580 		buf = buf->frags;
581 	}
582 
583 	return buf;
584 }
585 
net_buf_frag_insert(struct net_buf * parent,struct net_buf * frag)586 void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag)
587 {
588 	__ASSERT_NO_MSG(parent);
589 	__ASSERT_NO_MSG(frag);
590 
591 	if (parent->frags) {
592 		net_buf_frag_last(frag)->frags = parent->frags;
593 	}
594 	/* Take ownership of the fragment reference */
595 	parent->frags = frag;
596 }
597 
net_buf_frag_add(struct net_buf * head,struct net_buf * frag)598 struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag)
599 {
600 	__ASSERT_NO_MSG(frag);
601 
602 	if (!head) {
603 		return net_buf_ref(frag);
604 	}
605 
606 	net_buf_frag_insert(net_buf_frag_last(head), frag);
607 
608 	return head;
609 }
610 
611 #if defined(CONFIG_NET_BUF_LOG)
net_buf_frag_del_debug(struct net_buf * parent,struct net_buf * frag,const char * func,int line)612 struct net_buf *net_buf_frag_del_debug(struct net_buf *parent,
613 				       struct net_buf *frag,
614 				       const char *func, int line)
615 #else
616 struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag)
617 #endif
618 {
619 	struct net_buf *next_frag;
620 
621 	__ASSERT_NO_MSG(frag);
622 
623 	if (parent) {
624 		__ASSERT_NO_MSG(parent->frags);
625 		__ASSERT_NO_MSG(parent->frags == frag);
626 		parent->frags = frag->frags;
627 	}
628 
629 	next_frag = frag->frags;
630 
631 	frag->frags = NULL;
632 
633 #if defined(CONFIG_NET_BUF_LOG)
634 	net_buf_unref_debug(frag, func, line);
635 #else
636 	net_buf_unref(frag);
637 #endif
638 
639 	return next_frag;
640 }
641 
net_buf_linearize(void * dst,size_t dst_len,const struct net_buf * src,size_t offset,size_t len)642 size_t net_buf_linearize(void *dst, size_t dst_len, const struct net_buf *src,
643 			 size_t offset, size_t len)
644 {
645 	const struct net_buf *frag;
646 	size_t to_copy;
647 	size_t copied;
648 
649 	len = MIN(len, dst_len);
650 
651 	frag = src;
652 
653 	/* find the right fragment to start copying from */
654 	while (frag && offset >= frag->len) {
655 		offset -= frag->len;
656 		frag = frag->frags;
657 	}
658 
659 	/* traverse the fragment chain until len bytes are copied */
660 	copied = 0;
661 	while (frag && len > 0) {
662 		to_copy = MIN(len, frag->len - offset);
663 		memcpy((uint8_t *)dst + copied, frag->data + offset, to_copy);
664 
665 		copied += to_copy;
666 
667 		/* to_copy is always <= len */
668 		len -= to_copy;
669 		frag = frag->frags;
670 
671 		/* after the first iteration, this value will be 0 */
672 		offset = 0;
673 	}
674 
675 	return copied;
676 }
677 
678 /* This helper routine will append multiple bytes, if there is no place for
679  * the data in current fragment then create new fragment and add it to
680  * the buffer. It assumes that the buffer has at least one fragment.
681  */
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)682 size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
683 			    const void *value, k_timeout_t timeout,
684 			    net_buf_allocator_cb allocate_cb, void *user_data)
685 {
686 	struct net_buf *frag = net_buf_frag_last(buf);
687 	size_t added_len = 0;
688 	const uint8_t *value8 = value;
689 	size_t max_size;
690 
691 	do {
692 		uint16_t count = MIN(len, net_buf_tailroom(frag));
693 
694 		net_buf_add_mem(frag, value8, count);
695 		len -= count;
696 		added_len += count;
697 		value8 += count;
698 
699 		if (len == 0) {
700 			return added_len;
701 		}
702 
703 		if (allocate_cb) {
704 			frag = allocate_cb(timeout, user_data);
705 		} else {
706 			struct net_buf_pool *pool;
707 
708 			/* Allocate from the original pool if no callback has
709 			 * been provided.
710 			 */
711 			pool = net_buf_pool_get(buf->pool_id);
712 			max_size = pool->alloc->max_alloc_size;
713 			frag = net_buf_alloc_len(pool,
714 						 max_size ? MIN(len, max_size) : len,
715 						 timeout);
716 		}
717 
718 		if (!frag) {
719 			return added_len;
720 		}
721 
722 		net_buf_frag_add(buf, frag);
723 	} while (1);
724 
725 	/* Unreachable */
726 	return 0;
727 }
728 
net_buf_data_match(const struct net_buf * buf,size_t offset,const void * data,size_t len)729 size_t net_buf_data_match(const struct net_buf *buf, size_t offset, const void *data, size_t len)
730 {
731 	const uint8_t *dptr = data;
732 	const uint8_t *bptr;
733 	size_t compared = 0;
734 	size_t to_compare;
735 
736 	if (!buf || !data) {
737 		return compared;
738 	}
739 
740 	/* find the right fragment to start comparison */
741 	while (buf && offset >= buf->len) {
742 		offset -= buf->len;
743 		buf = buf->frags;
744 	}
745 
746 	while (buf && len > 0) {
747 		bptr = buf->data + offset;
748 		to_compare = MIN(len, buf->len - offset);
749 
750 		for (size_t i = 0; i < to_compare; ++i) {
751 			if (dptr[compared] != bptr[i]) {
752 				return compared;
753 			}
754 			compared++;
755 		}
756 
757 		len -= to_compare;
758 		buf = buf->frags;
759 		offset = 0;
760 	}
761 
762 	return compared;
763 }
764