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