Skip to content

Commit 916d9ad

Browse files
carlescuficfriedt
authored andcommitted
net: buf: Simplify fragment handling
This patch reworks how fragments are handled in the net_buf infrastructure. In particular, it removes the union around the node and frags members in the main net_buf structure. This is done so that both can be used at the same time, at a cost of 4 bytes per net_buf instance. This implies that the layout of net_buf instances changes whenever being inserted into a queue (fifo or lifo) or a linked list (slist). Until now, this is what happened when enqueueing a net_buf with frags in a queue or linked list: 1.1 Before enqueueing: +--------+ +--------+ +--------+ |#1 node|\ |#2 node|\ |#3 node|\ | | \ | | \ | | \ | frags |------| frags |------| frags |------NULL +--------+ +--------+ +--------+ net_buf #1 has 2 fragments, net_bufs #2 and #3. Both the node and frags pointers (they are the same, since they are unioned) point to the next fragment. 1.2 After enqueueing: +--------+ +--------+ +--------+ +--------+ +--------+ |q/slist |-----|#1 node|-----|#2 node|-----|#3 node|-----|q/slist | |node | | *flag | / | *flag | / | | / |node | | | | frags |/ | frags |/ | frags |/ | | +--------+ +--------+ +--------+ +--------+ +--------+ When enqueing a net_buf (in this case #1) that contains fragments, the current net_buf implementation actually enqueues all the fragments (in this case #2 and #3) as actual queue/slist items, since node and frags are one and the same in memory. This makes the enqueuing operation expensive and it makes it impossible to atomically dequeue. The `*flag` notation here means that the `flags` member has been set to `NET_BUF_FRAGS` in order to be able to reconstruct the frags pointers when dequeuing. After this patch, the layout changes considerably: 2.1 Before enqueueing: +--------+ +--------+ +--------+ |#1 node|--NULL |#2 node|--NULL |#3 node|--NULL | | | | | | | frags |-------| frags |-------| frags |------NULL +--------+ +--------+ +--------+ This is very similar to 1.1, except that now node and frags are different pointers, so node is just set to NULL. 2.2 After enqueueing: +--------+ +--------+ +--------+ |q/slist |------|#1 node|------|q/slist | |node | | | |node | | | | frags | | | +--------+ +--------+ +--------+ | +--------+ +--------+ | |#2 node|--NULL |#3 node|--NULL | | | | | +-----------| frags |-------| frags |------NULL +--------+ +--------+ When enqueuing net_buf #1, now we only enqueue that very item, instead of enqueing the frags as well, since now node and frags are separate pointers. This simplifies the operation and makes it atomic. Resolves zephyrproject-rtos#52718. Signed-off-by: Carles Cufi <[email protected]> (cherry picked from commit 3d306c1)
1 parent a90a3cc commit 916d9ad

File tree

2 files changed

+9
-62
lines changed

2 files changed

+9
-62
lines changed

include/net/buf.h

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -889,15 +889,6 @@ static inline void net_buf_simple_restore(struct net_buf_simple *buf,
889889
buf->len = state->len;
890890
}
891891

892-
/**
893-
* Flag indicating that the buffer has associated fragments. Only used
894-
* internally by the buffer handling code while the buffer is inside a
895-
* FIFO, meaning this never needs to be explicitly set or unset by the
896-
* net_buf API user. As long as the buffer is outside of a FIFO, i.e.
897-
* in practice always for the user for this API, the buf->frags pointer
898-
* should be used instead.
899-
*/
900-
#define NET_BUF_FRAGS BIT(0)
901892
/**
902893
* Flag indicating that the buffer's associated data pointer, points to
903894
* externally allocated memory. Therefore once ref goes down to zero, the
@@ -907,7 +898,7 @@ static inline void net_buf_simple_restore(struct net_buf_simple *buf,
907898
* Reference count mechanism however will behave the same way, and ref
908899
* count going to 0 will free the net_buf but no the data pointer in it.
909900
*/
910-
#define NET_BUF_EXTERNAL_DATA BIT(1)
901+
#define NET_BUF_EXTERNAL_DATA BIT(0)
911902

912903
/**
913904
* @brief Network buffer representation.
@@ -917,13 +908,11 @@ static inline void net_buf_simple_restore(struct net_buf_simple *buf,
917908
* using the net_buf_alloc() API.
918909
*/
919910
struct net_buf {
920-
union {
921-
/** Allow placing the buffer into sys_slist_t */
922-
sys_snode_t node;
911+
/** Allow placing the buffer into sys_slist_t */
912+
sys_snode_t node;
923913

924-
/** Fragments associated with this buffer. */
925-
struct net_buf *frags;
926-
};
914+
/** Fragments associated with this buffer. */
915+
struct net_buf *frags;
927916

928917
/** Reference count. */
929918
uint8_t ref;

subsys/net/buf.c

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ struct net_buf *net_buf_get_debug(struct k_fifo *fifo, k_timeout_t timeout,
405405
struct net_buf *net_buf_get(struct k_fifo *fifo, k_timeout_t timeout)
406406
#endif
407407
{
408-
struct net_buf *buf, *frag;
408+
struct net_buf *buf;
409409

410410
NET_BUF_DBG("%s():%d: fifo %p", func, line, fifo);
411411

@@ -416,18 +416,6 @@ struct net_buf *net_buf_get(struct k_fifo *fifo, k_timeout_t timeout)
416416

417417
NET_BUF_DBG("%s():%d: buf %p fifo %p", func, line, buf, fifo);
418418

419-
/* Get any fragments belonging to this buffer */
420-
for (frag = buf; (frag->flags & NET_BUF_FRAGS); frag = frag->frags) {
421-
frag->frags = k_fifo_get(fifo, K_NO_WAIT);
422-
__ASSERT_NO_MSG(frag->frags);
423-
424-
/* The fragments flag is only for FIFO-internal usage */
425-
frag->flags &= ~NET_BUF_FRAGS;
426-
}
427-
428-
/* Mark the end of the fragment list */
429-
frag->frags = NULL;
430-
431419
return buf;
432420
}
433421

@@ -451,24 +439,19 @@ void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve)
451439

452440
void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf)
453441
{
454-
struct net_buf *tail;
455442
unsigned int key;
456443

457444
__ASSERT_NO_MSG(list);
458445
__ASSERT_NO_MSG(buf);
459446

460-
for (tail = buf; tail->frags; tail = tail->frags) {
461-
tail->flags |= NET_BUF_FRAGS;
462-
}
463-
464447
key = irq_lock();
465-
sys_slist_append_list(list, &buf->node, &tail->node);
448+
sys_slist_append(list, &buf->node);
466449
irq_unlock(key);
467450
}
468451

469452
struct net_buf *net_buf_slist_get(sys_slist_t *list)
470453
{
471-
struct net_buf *buf, *frag;
454+
struct net_buf *buf;
472455
unsigned int key;
473456

474457
__ASSERT_NO_MSG(list);
@@ -477,40 +460,15 @@ struct net_buf *net_buf_slist_get(sys_slist_t *list)
477460
buf = (void *)sys_slist_get(list);
478461
irq_unlock(key);
479462

480-
if (!buf) {
481-
return NULL;
482-
}
483-
484-
/* Get any fragments belonging to this buffer */
485-
for (frag = buf; (frag->flags & NET_BUF_FRAGS); frag = frag->frags) {
486-
key = irq_lock();
487-
frag->frags = (void *)sys_slist_get(list);
488-
irq_unlock(key);
489-
490-
__ASSERT_NO_MSG(frag->frags);
491-
492-
/* The fragments flag is only for list-internal usage */
493-
frag->flags &= ~NET_BUF_FRAGS;
494-
}
495-
496-
/* Mark the end of the fragment list */
497-
frag->frags = NULL;
498-
499463
return buf;
500464
}
501465

502466
void net_buf_put(struct k_fifo *fifo, struct net_buf *buf)
503467
{
504-
struct net_buf *tail;
505-
506468
__ASSERT_NO_MSG(fifo);
507469
__ASSERT_NO_MSG(buf);
508470

509-
for (tail = buf; tail->frags; tail = tail->frags) {
510-
tail->flags |= NET_BUF_FRAGS;
511-
}
512-
513-
k_fifo_put_list(fifo, buf, tail);
471+
k_fifo_put(fifo, buf);
514472
}
515473

516474
#if defined(CONFIG_NET_BUF_LOG)

0 commit comments

Comments
 (0)