Commit graph

21 commits

Author SHA1 Message Date
Jonathan Rico
b6cdf10310 Bluetooth: L2CAP: remove seg_pool
We can get rid of the view pool for SDU segments :)
We have to make the code slightly more complex :'(

The basic idea is always giving the original SDU buffer to `conn.c` for it
to pull ACL fragments from.

In order to do this, we need to add the PDU headers just-in-time.
`bt_l2cap_send_pdu()` does not add them before putting the PDU on the queue
anymore. They are added by `l2cap_data_pull()` right before the data leaves
`l2cap.c` for `conn.c`.

We also have to inform `conn.c` "out of band" of the real L2CAP PDU size so
it doesn't fragment across segment boundaries. This oob is the new `length`
parameter to the `.pull()` method.

This is the added complexity mentioned above.

Since SDU segmentation concerns only LE-L2CAP, ISO and Classic L2CAP don't
need this extra logic.

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
2024-06-12 18:51:34 +02:00
Jonathan Rico
28535fe2f2 Bluetooth: host: Change TX pattern (push -> pull)
The current TX pattern in the host is to try to push a buffer through all
the layers up until it is ingested by the controller.

Since sending can fail at any layer, we need error-handling and separate
retry logic on pretty much all layers. That logic obscures the "happy path"
for people trying ot understand the code.

This commit inverts the control, in a way that doesn't require changing the
host or HCI driver API (yet):

Layers don't send buffers synchronously, they instead put their buffer in a
private queue of their own and raise a TX flag on the lower layer. Think of
it as a `READY` interrupt line that has to be serviced by the lower layer.

Sending is now non-blocking, rate depends on the size of buffer pools.

There is a single TX processing function. This can be thought as the
Interrupt Service Routine that will handle the `READY` interrupt from the
layers above.

That `tx_processor()` will then attempt to allocate enough resources in
order to send the buffer through to the controller. This allocation logic
does not block.

After acquiring all the resources, the TX processor will attempt to pull
data from the upper layer. The upper layer has to figure out which buffer
to pass to the controller. This is a good spot to put scheduling or QoS
logic in the upper layer.

Notes:

- user-facing API for tuning QoS will be implemented in a future patch

- this scheme could (and probably will) be extended to upper layers (e.g.
  ATT, L2CAP CoC segmentation).

- this patch removes the `pending_no_cb()` memory optimization for
  clarity/correctness. It might get re-implemented after a stabilization
  period. Hopefully with more documentation.

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
Co-authored-by: Aleksander Wasaznik <aleksander.wasaznik@nordicsemi.no>
2024-06-12 18:51:34 +02:00
Jonathan Rico
1c8cae30a8 Bluetooth: host: Introduce "view" buffer concept
Instead of allocating segments/fragments and copying data into them, we
allocate segments as "views" (or slices) into the original buffer.

The view also gives access to the headroom of the original buffer, allowing
lower layers to push their headers.

We choose not to allow multiple views into the same buffer as the headroom
of a view would overlap with the data of the previous view.

We mark a buffer as locked (or "in-view") by temporarily setting its
headroom to zero. This effectively stops create_view because the requested
headroom is not available.

Each layer that does some kind of fragmentation and wants to use views for
that needs to maintain a buffer pool (bufsize 0, count = max views) and a
metadata array (size = max views) for the view mechanism to work.

Maximum number of views: number of parallel buffers from the upper layer,
e.g. number of L2CAP channels for L2CAP segmentation or number of ACL
connections for HCI fragmentation.

Reason for the change:
1. prevent deadlocks or (ATT/SMP) requests timing out
2. save time (zero-copy)
3. save memory (gets rid of frag pools)

L2CAP CoC: would either allocate from the `alloc_seg` application callback,
or worse _steal_ from the same pool, or allocate from the global ACL pool.

Conn/HCI: would either allocate from `frag_pool` or the global ACL pool.

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
Co-authored-by: Aleksander Wasaznik <aleksander.wasaznik@nordicsemi.no>
2024-06-12 18:51:34 +02:00
Aleksander Wasaznik
a530010fe2 Bluetooth: Remove useless field init_credits
Remove `bt_l2cap_le_endpoint.init_credits` that has zero uses and is
just taking up RAM for no reason.

Signed-off-by: Aleksander Wasaznik <aleksander.wasaznik@nordicsemi.no>
2024-05-21 16:45:50 -04:00
Emil Gydesen
49d6b6bc0e Bluetooth: Fix spelling in some public header files
Fixes a few spelling mistakes

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
2024-05-04 15:21:52 +01:00
Jonathan Rico
43de309b3e Bluetooth: l2cap: Remove (net/buf) frag support
It's needless complexity, and the terminology clashes with
Bluetooth (HCI frags).

It has one user, IPSP, that is going away soon.

Removing frag support will allow a future optimization, removing the
need for HCI and L2CAP fragment buffer pools, saving memory.

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
2024-04-11 14:40:32 +02:00
Jonathan Rico
b3e67cf2a6 Bluetooth: L2CAP: document error values of bt_l2cap_chan_send()
They're still subject to change.
At least now the users have some idea of what's happening.

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
2024-01-31 09:06:08 +01:00
Jonathan Rico
81d524d081 Bluetooth: L2CAP: always send from workqueue
Always pull from the channel queue from the system workqueue context.
This simplifies debugging.

This also allows us to remove `sent` from the metadata struct.

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
2024-01-17 14:42:40 +01:00
Jonathan Rico
38c39af4df Bluetooth: L2CAP: Prepend SDU header immediately
Previously it was not always possible to prepend the header.

It was not possible if the application neglected to reserve the space
for headers.  This is bad because it forces a buffer segment allocation
even if the buffer had enough room for the headers. E.g. a payload of 10
bytes in a netbuf of 30 bytes would have been segmented.

We now explicitly reject the buffer if it does not have the headroom.

This allows us to do a nice thing; simplify L2CAP segmentation.

We convert the SDU from the application into a PDU payload, by
prepending the SDU header, i.e. the SDU length in the original buffer.

This PDU payload is ready to be chunked into PDUs without having to keep
track of where in the SDU we are. This has the effect of removing a
bunch of logic in the segmentation machine.

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
Signed-off-by: Aleksander Wasaznik <aleksander.wasaznik@nordicsemi.no>
2024-01-16 11:29:43 +00:00
Jonathan Rico
75c2aeb8bd Bluetooth: L2CAP: stop stealing buffers from SDU pool
It seems like a nice idea at first, but leads to hard-to-debug
situations for the application.

The previous behavior can be implemented by the app by defining
`alloc_seg` and allocating from the same pool as `buf`.

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
2024-01-15 09:58:58 +01:00
Jonathan Rico
79e86472c3 Bluetooth: L2CAP: clarify BT_L2CAP_STATUS_OUT
Makes it clearer what that bit means:
If set, the channel has capacity to send at least one PDU.

If unset, the channel ran out of credits and won't be able to send
anything until the peer sends credits back.

Also add debug logs.

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
2024-01-05 14:30:18 +01:00
Rubin Gerritsen
dc9ad42ffb Bluetooth: Clarify when the sent callback is issued
The timing of the sent callback shouldn't be used to determine
when something is sent on air. The callback is issued after the
controller has raised the HCI event "Number Of Completed Packets".
The timing of this event is dependent on the implementation.

Signed-off-by: Rubin Gerritsen <rubin.gerritsen@nordicsemi.no>
2023-12-15 11:41:14 +00:00
Donatien Garnier
815891643e Bluetooth: Host: Pass pointer to server in L2CAP accept() callback
Add a pointer to the associated server structure in the L2CAP accept()
callback. This allows the callee to know which server an incoming L2CAP
connection is associated with.

Signed-off-by: Donatien Garnier <donatien.garnier@blecon.net>
2023-09-14 14:36:34 +02:00
Aleksander Wasaznik
92fcd9ef40 Bluetooth: Host: Add L2CAP seg_recv API
This is an alternative API for the L2CAP receive functionality. It
allows an application the receive L2CAP segments directly and manage
credits explictly. The API is guarded by an experimental kconfig option.

Fixes: https://github.com/zephyrproject-rtos/zephyr/issues/57485

Signed-off-by: Aleksander Wasaznik <aleksander.wasaznik@nordicsemi.no>
2023-05-24 13:15:14 -04:00
Jonathan Rico
6cdb82cb59 Bluetooth: host: l2cap: trigger SDUs that get lost in limbo
Turns out the [first bugfix] was too naive: there is a case where resuming
all channels will not work on all queued SDUs, and the work handler will
give up and wait for the next sent SDU instead of trying to resume again.

This happens when the number of credits and conn contexts is very low for
the amount of data to send.

Always reschedule with a delay to avoid that situation.

[first bugfix]: https://github.com/zephyrproject-rtos/zephyr/pull/50476

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
2023-04-26 13:53:08 +02:00
Aleksander Wasaznik
9b62cc6899 Bluetooth: l2cap.h: Document bt_l2cap_le_chan.rx/tx
Adding extra documentation to bt_l2cap_le_chan.rx/tx that will help
readers understand what those values mean in practice.

Signed-off-by: Aleksander Wasaznik <aleksander.wasaznik@nordicsemi.no>
2023-04-24 13:31:18 +02:00
Jonathan Rico
b23c330c63 Bluetooth: host: shrink bt_l2cap_le_chan size
Most of this struct is bookkeeping for the dynamic channels.
This isn't needed for e.g., a simple peripheral using GATT.

With a peripheral_hr build for nrf52840dk_nrf52840, we save
280 bytes of RAM.

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
2023-03-15 09:11:33 +01:00
Jonathan Rico
77e1a9dcad Bluetooth: host: l2cap: add alloc_seg callback
This callback allows use-cases where the SDU is much larger than the l2cap
MPS. The stack will then try to allocate using this callback if specified,
and fall-back on using the buffer's pool (previous behavior).

This way one can define two buffer pools, one with a very large buffer
size, and one with a buffer size >= MPS, and the stack will allocate from
that instead.

Signed-off-by: Jonathan Rico <jonathan.rico@nordicsemi.no>
2022-09-26 11:05:04 +02:00
Gerard Marull-Paretas
a7c3e7e84a doxygen: remove redundant usages of def
The def command Indicates that a comment block contains documentation
for a #define macro. This is useful if the comment block documents a
macro not adjacent to it, e.g.

```c
/**
 * @def MAX(x,y)
 * @brief Computes the maximum of @a x and @a y.
 */
 #ifdef XXX
 #define MAX(x,y) ...
 #endif
```

However, it is not necessary if the comment is adjacent to the
definition, e.g.

```c
/**
 * @brief Computes the maximum of @a x and @a y.
 */
 #define MAX(x,y) ...
```

This patch removes all unnecessary def entries in-tree.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-09 12:29:28 +02:00
Gerard Marull-Paretas
fb9b3bcd93 include: migrate includes to <zephyr/...>
In order to bring consistency in-tree, migrate all includes within
include directory to the new prefix <zephyr/...>. Note that the
conversion has been scripted, refer to zephyrproject-rtos#45388 for more
details.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-05-06 20:03:00 +02:00
Yuval Peress
53ef68d459 include: Prefix includes to use a scope
Move include paths and add new target_include_directories to support
backwards compatibility:
* /include -> /include/zephyr
  example: <irq.h> -> <zephyr/irq.h>

Issue #41543

Signed-off-by: Yuval Peress <peress@google.com>
2022-04-08 19:03:32 +02:00
Renamed from include/bluetooth/l2cap.h (Browse further)