2021-03-31 19:13:43 +08:00
|
|
|
.. _mpsc_pbuf:
|
|
|
|
|
|
|
|
Multi Producer Single Consumer Packet Buffer
|
|
|
|
============================================
|
|
|
|
|
|
|
|
A :dfn:`Multi Producer Single Consumer Packet Buffer (MPSC_PBUF)` is a circular
|
|
|
|
buffer, whose contents are stored in first-in-first-out order. Variable size
|
|
|
|
packets are stored in the buffer. Packet buffer works under assumption that there
|
|
|
|
is a single context that consumes the data. However, it is possible that another
|
|
|
|
context may interfere to flush the data and never come back (panic case).
|
|
|
|
Packet is produced in two steps: first requested amount of data is allocated,
|
|
|
|
producer fills the data and commits it. Consuming a packet is also performed in
|
|
|
|
two steps: consumer claims the packet, gets pointer to it and length and later
|
|
|
|
on packet is freed. This approach reduces memory copying.
|
|
|
|
|
|
|
|
A :dfn:`MPSC Packet Buffer` has the following key properties:
|
|
|
|
|
|
|
|
* Allocate, commit scheme used for packet producing.
|
|
|
|
* Claim, free scheme used for packet consuming.
|
2022-10-10 20:30:47 +08:00
|
|
|
* Allocator ensures that contiguous memory of requested length is allocated.
|
2021-03-31 19:13:43 +08:00
|
|
|
* Following policies can be applied when requested space cannot be allocated:
|
|
|
|
|
|
|
|
* **Overwrite** - oldest entries are dropped until requested amount of memory can
|
|
|
|
be allocated. For each dropped packet user callback is called.
|
|
|
|
* **No overwrite** - When requested amount of space cannot be allocated,
|
|
|
|
allocation fails.
|
|
|
|
* Dedicated, optimized API for storing short packets.
|
|
|
|
* Allocation with timeout.
|
|
|
|
|
|
|
|
Internals
|
|
|
|
---------
|
|
|
|
|
|
|
|
Each packet in the buffer contains ``MPSC_PBUF`` specific header which is used
|
|
|
|
for internal management. Header consists of 2 bit flags. In order to optimize
|
|
|
|
memory usage, header can be added on top of the user header using
|
|
|
|
:c:macro:`MPSC_PBUF_HDR` and remaining bits in the first word can be application
|
|
|
|
specific. Header consists of following flags:
|
|
|
|
|
|
|
|
* valid - bit set to one when packet contains valid user packet
|
|
|
|
* busy - bit set when packet is being consumed (claimed but not free)
|
|
|
|
|
|
|
|
Header state:
|
|
|
|
|
|
|
|
+-------+------+----------------------+
|
|
|
|
| valid | busy | description |
|
|
|
|
+-------+------+----------------------+
|
|
|
|
| 0 | 0 | space is free |
|
|
|
|
+-------+------+----------------------+
|
|
|
|
| 1 | 0 | valid packet |
|
|
|
|
+-------+------+----------------------+
|
|
|
|
| 1 | 1 | claimed valid packet |
|
|
|
|
+-------+------+----------------------+
|
|
|
|
| 0 | 1 | internal skip packet |
|
|
|
|
+-------+------+----------------------+
|
|
|
|
|
|
|
|
Packet buffer space contains free space, valid user packets and internal skip
|
2023-11-14 23:24:00 +08:00
|
|
|
packets. Internal skip packets indicates padding, e.g. at the end of the buffer.
|
2021-03-31 19:13:43 +08:00
|
|
|
|
|
|
|
Allocation
|
|
|
|
^^^^^^^^^^
|
|
|
|
|
|
|
|
Using pairs for read and write indexes, available space is determined. If
|
|
|
|
space can be allocated, temporary write index is moved and pointer to a space
|
2022-03-17 05:07:43 +08:00
|
|
|
within buffer is returned. Packet header is reset. If allocation required
|
2021-03-31 19:13:43 +08:00
|
|
|
wrapping of the write index, a skip packet is added to the end of buffer. If
|
|
|
|
space cannot be allocated and overwrite is disabled then ``NULL`` pointer is
|
|
|
|
returned or context blocks if allocation was with timeout.
|
|
|
|
|
|
|
|
Allocation with overwrite
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
If overwrite is enabled, oldest packets are dropped until requested amount of
|
|
|
|
space can be allocated. When packets are dropped ``busy`` flag is checked in the
|
|
|
|
header to ensure that currently consumed packet is not overwritten. In that case,
|
|
|
|
skip packet is added before busy packet and packets following the busy packet
|
|
|
|
are dropped. When busy packet is being freed, such situation is detected and
|
|
|
|
packet is converted to skip packet to avoid double processing.
|
|
|
|
|
|
|
|
Usage
|
|
|
|
-----
|
|
|
|
|
|
|
|
Packet header definition
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
2022-04-17 04:10:28 +08:00
|
|
|
Packet header details can be found in :zephyr_file:`include/zephyr/sys/mpsc_packet.h`.
|
|
|
|
API functions can be found in :zephyr_file:`include/zephyr/sys/mpsc_pbuf.h`. Headers
|
2021-03-31 19:13:43 +08:00
|
|
|
are split to avoid include spam when declaring the packet.
|
|
|
|
|
|
|
|
User header structure must start with internal header:
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
2022-04-21 12:03:05 +08:00
|
|
|
#include <zephyr/sys/mpsc_packet.h>
|
2021-03-31 19:13:43 +08:00
|
|
|
|
|
|
|
struct foo_header {
|
|
|
|
MPSC_PBUF_HDR;
|
|
|
|
uint32_t length: 32 - MPSC_PBUF_HDR_BITS;
|
|
|
|
};
|
|
|
|
|
|
|
|
Packet buffer configuration
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Configuration structure contains buffer details, configuration flags and
|
|
|
|
callbacks. Following callbacks are used by the packet buffer:
|
|
|
|
|
|
|
|
* Drop notification - callback called whenever a packet is dropped due to
|
|
|
|
overwrite.
|
|
|
|
* Get packet length - callback to determine packet length
|
|
|
|
|
|
|
|
Packet producing
|
|
|
|
^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Standard, two step method:
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
foo_packet *packet = mpsc_pbuf_alloc(buffer, len, K_NO_WAIT);
|
|
|
|
|
|
|
|
fill_data(packet);
|
|
|
|
|
|
|
|
mpsc_pbuf_commit(buffer, packet);
|
|
|
|
|
|
|
|
Performance optimized storing of small packets:
|
|
|
|
|
|
|
|
* 32 bit word packet
|
|
|
|
* 32 bit word with pointer packet
|
|
|
|
|
|
|
|
Note that since packets are written by value, they should already contain
|
|
|
|
``valid`` bit set in the header.
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
mpsc_pbuf_put_word(buffer, data);
|
|
|
|
mpsc_pbuf_put_word_ext(buffer, data, ptr);
|
|
|
|
|
|
|
|
Packet consuming
|
|
|
|
^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Two step method:
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
foo_packet *packet = mpsc_pbuf_claim(buffer);
|
|
|
|
|
|
|
|
process(packet);
|
|
|
|
|
|
|
|
mpsc_pbuf_free(buffer, packet);
|