Section 7.16

LE credit-based flow control

Full name: Low Energy credit-based flow control.

Definition

LE credit-based flow control is a mode of L2CAP §7.15 (Logical Link Control and Adaptation Protocol), the Bluetooth layer that carries several channels over one connection, for channels that applications open on BLE §7.1 (Bluetooth Low Energy) links. The receiver gives the sender credits; each credit allows one K-frame, the L2CAP packet of this mode, and a sender without credits must wait. The receiver returns credits as it frees buffer space. The mode also splits each message the application hands down, an SDU (service data unit), into K-frames and joins them again.

The idea

A phone sends a 100 KiB file over an L2CAP channel to a sensor that has room for 2 KiB of incoming data and writes the data to slow flash memory §6.6. Without pacing, the sensor would have to drop data or stop accepting radio packets. Stopping the radio would stall every other conversation on the connection as well, including ATT §7.9 (Attribute Protocol) traffic, because the Link Layer §7.6, the radio protocol layer, acknowledges packets for the whole connection, not for one channel.

Credits pace each channel separately. They are a form of backpressure §9.30: the receiver’s free space, not the sender’s eagerness, sets the pace. Think of credits as tickets. The sensor hands the phone four tickets because it has four free buffers; each K-frame the phone sends costs one ticket; when the sensor’s application empties a buffer, the sensor hands back a ticket. The phone can never send more than the sensor has room for, and a slow channel holds back only itself. The picture is accurate except in one respect: a ticket pays for one K-frame of any size up to the agreed maximum, so a tiny K-frame uses up a whole credit.

How it works

L2CAP calls each of its packets a PDU (protocol data unit); in this mode a PDU is called a K-frame.

  1. Opening. When the channel is opened (the L2CAP entry describes the exchange), each side states three numbers for traffic toward itself: its MTU §7.14 (maximum transmission unit), the largest SDU it accepts; its MPS (maximum PDU payload size), the largest payload of one K-frame it accepts; and its initial credits. Each direction has its own count: the credits a sender holds are the K-frames the other side currently allows it to send.

  2. The K-frame. A K-frame starts with the 4-byte basic L2CAP header: a 2-byte length and a 2-byte CID (channel identifier) that names the receiver’s end of the channel. The first K-frame of an SDU then carries a 2-byte SDU length, the size of the whole SDU, which is length-prefix framing §4.30: it tells the receiver where the message ends. The data follows; later K-frames of the same SDU carry only data. In the counting used here, the MPS limits everything after the 4-byte header, so the first K-frame carries at most MPS−2\mathrm{MPS} - 2 bytes of the SDU and each later one at most MPS bytes; a sender that packs K-frames this way never exceeds the receiver’s MPS.

  3. Sending. The sender may transmit a K-frame only while its credit count is greater than zero, and each K-frame lowers the count by one.

  4. Receiving. The receiver must close the channel if the announced SDU length exceeds its MTU, if a K-frame’s payload exceeds its MPS, if the K-frames of one SDU carry more bytes than the SDU length announced, or if a K-frame arrives when the sender had no credit left.

  5. Returning credits. When the receiver has room again, for example after its application has taken a complete SDU, it sends a flow control credit message on the signaling channel, L2CAP’s control channel, naming the channel and a number of credits from 1 to 65535. The sender adds that number to its count. A count above 65535 is an error, and the sender must then close the channel.

  6. Closing. Either side may close the channel with a disconnection request. The credit counts and any incomplete SDU end with the channel; a channel opened later starts from its own initial credits.

Bluetooth 4.1 introduced this mode. Bluetooth 5.2 added the Enhanced Credit Based Flow Control mode, which counts credits in the same way but can open up to five channels with one request and reconfigure open channels; EATT (Enhanced ATT), which carries ATT over such channels, uses it.

Credits are one of three kinds of pacing on a BLE connection. The Link Layer withholds its acknowledgment of a radio packet when it has no room, which pauses the whole connection. The HCI §7.5 (Host Controller Interface), the interface between the protocol software and the radio controller, counts the controller’s free buffers, which pauses data from the protocol software to the controller. Credits pause one L2CAP channel. A credit is spent per K-frame, never per radio packet: a K-frame longer than the Link Layer’s data length travels in several radio packets and still costs one credit.

Worked example

A receiver opens a channel with an MTU of 512 bytes, an MPS of 247 bytes and 4 initial credits. With MPS 247, a full K-frame is 4+247=2514 + 247 = 251 bytes, exactly one radio packet when the Link Layer data length is 251 bytes. The sender has two SDUs of 512 bytes each.

First SDU. K-frame 1 carries the 2-byte SDU length and 247−2=245247 - 2 = 245 data bytes. K-frame 2 carries 247 data bytes, bringing the total to 245+247=492245 + 247 = 492. K-frame 3 carries the remaining 512−492=20512 - 492 = 20 bytes. Three K-frames cost three credits, and the sender’s count falls from 4 to 4−3=14 - 3 = 1.

Second SDU. The sender sends K-frame 1 of the second SDU, and its count falls to 0. It must now wait, with 2 K-frames of the second SDU unsent.

Credits return. The receiver’s application takes the first SDU; the receiver frees the three buffers that held it and sends 3 credits. The sender’s count rises to 0+3=30 + 3 = 3. It sends the last two K-frames of the second SDU, and its count falls to 3−2=13 - 2 = 1.

The receiver’s commitment. By granting 4 credits at the start, the receiver allowed up to 4 K-frames to arrive before its application read anything: 4×247=9884 \times 247 = 988 bytes of payload, plus 4×4=164 \times 4 = 16 bytes of headers. It must have that room reserved before it grants the credits. Credits count K-frames, not bytes: a sender with ten 1-byte SDUs spends 10 credits on 10 bytes of data, because each SDU needs its own K-frame.

Stated precisely

Let cc be the credit count that the sender holds for one channel in one direction, PP the receiver’s MPS in bytes, and LL the length of an SDU in bytes, with LL at most the receiver’s MTU. The rules are:

For L=512L = 512 and P=247P = 247, k=⌈514/247⌉=⌈2.08⌉=3k = \lceil 514 / 247 \rceil = \lceil 2.08 \rceil = 3. A sender that sends shorter K-frames needs more credits, never fewer.

The receiver’s protection is an invariant, a statement that must stay true at every moment. Let FF be the number of buffers, each large enough for one K-frame, that the receiver has reserved for the channel. The credits the sender holds, plus the K-frames sent but not yet received, plus the K-frames received but not yet consumed, must never exceed FF. While the invariant holds, no K-frame arrives without a free buffer, whatever the sender does.

Limits and common mistakes

A credit is permission to send, nothing more. It is not authentication §1.6 of the sender and not an authorization §1.7 decision about the content. A returned credit tells the sender only that the receiver can take another K-frame, not that earlier data was stored or acted on.

Granting too many credits and returning too few both fail. Credits for buffers that do not exist let a peer send data the receiver cannot hold, which turns flow control into memory exhaustion. Credits never returned stall the channel: the sender cannot tell a slow receiver from one that has stopped, so the sender’s software must decide how long to wait.

A receiver that budgets memory in bytes must reserve a buffer of the full MPS for every credit it grants, because the sender chooses the size of each K-frame.

Counts belong to one channel. When a channel closes and another opens, possibly with the same CID, the new channel starts from its own initial credits; carrying an old count over would let the new channel send into buffers that were never reserved for it.

Credits do not resend data. The Link Layer resends lost radio packets, and an SDU that was incomplete when the channel or the connection closed is gone.

Refusal is not done with credits. A server that decides, after accepting the channel, not to serve the peer must close the channel; withholding credits only leaves the peer waiting without a reason.

Questions answered

What is the difference between a credit unit and an application message?

A credit is permission to send exactly one K-frame, the packet of an L2CAP (Logical Link Control and Adaptation Protocol) channel that uses LE (Low Energy) credit-based flow control. An application message is whatever the application hands to the channel as one SDU (service data unit), up to the receiver’s MTU (maximum transmission unit). One message usually needs several K-frames, and so several credits. If the receiver’s MPS, the largest payload of one K-frame, is PP bytes, a message of LL bytes needs at least ⌈(L+2)/P⌉\lceil (L + 2) / P \rceil credits, where the 2 is the SDU length field in the first K-frame and ⌈x⌉\lceil x \rceil is xx rounded up. A 512-byte message with P=247P = 247 needs 3 credits; a 1-byte message needs a whole credit.

The two units also mean different things. A credit is a unit of buffer permission granted by the receiver’s L2CAP layer, and it says nothing about content. A message is a unit of meaning defined by the application, and its boundaries are kept: the receiver gets back exactly the SDU that was sent. Below both, one K-frame may travel in several radio packets without costing more credits.

Related entries: MTU and fragmentation (§7.14), Backpressure (§9.30), Length-prefix framing (§4.30), L2CAP (§7.15). Further reading: [92].