monitor: Add L2CAP channel details to analyze output
Luiz Augusto von Dentz
maintainerPATCH 1/3 monitor: Add L2CAP channel details to analyze output
From: Luiz Augusto von Dentz <[email protected]>
Enhance btmon analyze mode to display richer per-channel information:
- Add MTU, MPS, and mode fields to struct l2cap_chan
- Parse Configure Request/Response options for BR/EDR channels to extract MTU (option 0x01) and mode (option 0x04)
- Add l2cap_le_sig() to parse LE signaling (CID 5) for LE Credit Based Connection Request/Response and Enhanced Credit Connection Request, extracting PSM, MTU, MPS, and mode
- Display fixed channel names (ATT, L2CAP Signaling, SMP) for CID <= 7
- Show PSM in both decimal and hex format
- Print mode, MTU, and MPS for dynamic channels
--- monitor/analyze.c | 221 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 219 insertions(+), 2 deletions(-)
monitor/analyze.c +219 -2
@@ -94,6 +94,9 @@ struct plot {struct l2cap_chan {uint16_t cid;uint16_t psm; +uint16_t mtu; +uint16_t mps; +uint8_t mode;bool out;struct timeval last_rx;struct hci_stats rx;
@@ -156,17 +159,76 @@ static void print_stats(struct hci_stats *stats, const char *label)plot_draw (stats->plot, label); } +static const char *fixed_channel_name (uint16_t cid) +{ +switch (cid) { +case 0x0001 : +return "L2CAP Signaling (BR/EDR)" ; +case 0x0002 : +return "Connectionless" ; +case 0x0003 : +return "AMP Manager" ; +case 0x0004 : +return "ATT" ; +case 0x0005 : +return "L2CAP Signaling (LE)" ; +case 0x0006 : +return "SMP (LE)" ; +case 0x0007 : +return "SMP (BR/EDR)" ; +default : +return NULL ; + } +} + +static const char *l2cap_mode_name (uint8_t mode) +{ +switch (mode) { +case 0x00 : +return "Basic" ; +case 0x01 : +return "Retransmission" ; +case 0x02 : +return "Flow Control" ; +case 0x03 : +return "ERTM" ; +case 0x04 : +return "Streaming" ; +case 0x80 : +return "LE Credit" ; +case 0x81 : +return "Enhanced Credit" ; +default : +return "Unknown" ; + } +} +static void chan_destroy (void *data) {struct l2cap_chan *chan = data; +const char *fixed;if (!chan->rx.num && !chan->tx.num) goto done; -printf (" Found %s L2CAP channel with CID %u\n" , + fixed =fixed_channel_name (chan->cid); +if (fixed) +printf (" Found %s L2CAP channel with CID %u (%s)\n" , + chan->out ?"TX" :"RX" , chan->cid, + fixed); +else +printf (" Found %s L2CAP channel with CID %u\n" , chan->out ?"TX" :"RX" , chan->cid); +if (chan->psm) -print_field ("PSM %u" , chan->psm); +print_field ("PSM %u (0x%04x)" , chan->psm, chan->psm); +if (!fixed) { +print_field ("Mode: %s" ,l2cap_mode_name (chan->mode)); +if (chan->mtu) +print_field ("MTU: %u" , chan->mtu); +if (chan->mps) +print_field ("MPS: %u" , chan->mps); + }print_stats (&chan->rx,"RX" );print_stats (&chan->tx,"TX" );
@@ -433,6 +495,159 @@ static void l2cap_sig(struct hci_conn *conn, bool out, chan->psm = psm; }break ; +case BT_L2CAP_PDU_CONFIG_REQ : + { +const struct bt_l2cap_pdu_config_req *pdu = data +4 ; +const uint8_t *opts; +uint16_t opts_len; + + dcid =le16_to_cpu (pdu->dcid); +/* Options start after the 4-byte config req header */ + opts = data +4 +sizeof (*pdu); + opts_len = size -4 -sizeof (*pdu); + + chan =chan_lookup (conn, dcid, !out); +if (!chan) +break ; + +while (opts_len >=2 ) { +uint8_t type = opts[0 ]; +uint8_t len = opts[1 ]; + +if (opts_len < (uint16_t )(2 + len)) +break ; + +switch (type) { +case 0x01 :/* MTU */ +if (len >=2 ) + chan->mtu =get_le16 (opts +2 ); +break ; +case 0x04 :/* Retransmission and Flow Control */ +if (len >=1 ) + chan->mode = opts[2 ]; +break ; + } + + opts +=2 + len; + opts_len -=2 + len; + } +break ; + } +case BT_L2CAP_PDU_CONFIG_RSP : + { +const struct bt_l2cap_pdu_config_rsp *pdu = data +4 ; +const uint8_t *opts; +uint16_t opts_len; + + scid =le16_to_cpu (pdu->scid); +/* Options start after the 6-byte config rsp header */ + opts = data +4 +sizeof (*pdu); + opts_len = size -4 -sizeof (*pdu); + + chan =chan_lookup (conn, scid, out); +if (!chan) +break ; + +while (opts_len >=2 ) { +uint8_t type = opts[0 ]; +uint8_t len = opts[1 ]; + +if (opts_len < (uint16_t )(2 + len)) +break ; + +switch (type) { +case 0x01 :/* MTU */ +if (len >=2 ) + chan->mtu =get_le16 (opts +2 ); +break ; +case 0x04 :/* Retransmission and Flow Control */ +if (len >=1 ) + chan->mode = opts[2 ]; +break ; + } + + opts +=2 + len; + opts_len -=2 + len; + } +break ; + } + } +} + +static void l2cap_le_sig (struct hci_conn *conn,bool out, +const void *data,uint16_t size) +{ +const struct bt_l2cap_hdr_sig *hdr = data; +struct l2cap_chan *chan; +uint16_t psm, scid, dcid; + +switch (hdr->code) { +case BT_L2CAP_PDU_LE_CONN_REQ : + { +const struct bt_l2cap_pdu_le_conn_req *pdu = data +4 ; + + psm =le16_to_cpu (pdu->psm); + scid =le16_to_cpu (pdu->scid); + chan =chan_lookup (conn, scid, out); +if (chan) { + chan->psm = psm; + chan->mtu =le16_to_cpu (pdu->mtu); + chan->mps =le16_to_cpu (pdu->mps); + chan->mode =0x80 ;/* LE Credit */ + } +break ; + } +case BT_L2CAP_PDU_LE_CONN_RSP : + { +const struct bt_l2cap_pdu_le_conn_rsp *pdu = data +4 ; + + dcid =le16_to_cpu (pdu->dcid); + +/* The response's dcid is the responder's CID. Its MTU/MPS +* belong to the responder, so set them on the channel for +* that direction (out). Also propagate PSM from the +* requester's channel (!out) if available. +*/ + chan =chan_lookup (conn, dcid, out); +if (chan) { +struct l2cap_chan *req_chan; + + chan->mtu =le16_to_cpu (pdu->mtu); + chan->mps =le16_to_cpu (pdu->mps); + chan->mode =0x80 ;/* LE Credit */ + +/* Propagate PSM from the request channel */ + req_chan =queue_find (conn->chan_list, + chan_match_cid, +UINT_TO_PTR (dcid | + (!out ?0x10000 :0 ))); +if (req_chan && req_chan->psm) + chan->psm = req_chan->psm; + } +break ; + } +case BT_L2CAP_PDU_ECRED_CONN_REQ : + { +const struct bt_l2cap_pdu_ecred_conn_req *pdu = data +4 ; +uint16_t req_len =le16_to_cpu (hdr->len); +int num_cids; +int i; + + psm =le16_to_cpu (pdu->psm); + num_cids = (req_len -sizeof (*pdu)) /sizeof (uint16_t ); + +for (i =0 ; i < num_cids; i++) { + scid =le16_to_cpu (pdu->scid[i]); + chan =chan_lookup (conn, scid, out); +if (chan) { + chan->psm = psm; + chan->mtu =le16_to_cpu (pdu->mtu); + chan->mps =le16_to_cpu (pdu->mps); + chan->mode =0x81 ;/* Enhanced Credit */ + } + } +break ; + } } }
@@ -938,6 +1153,8 @@ static void acl_pkt(struct timeval *tv, uint16_t index, bool out, chan =chan_lookup (conn, cid, out);if (cid ==1 )l2cap_sig (conn, out, data +4 , size -4 ); +else if (cid ==5 ) +l2cap_le_sig (conn, out, data +4 , size -4 );break ; }
Signature · 1 line
2.53.0
Luiz Augusto von Dentz
maintainerPATCH 2/3 monitor: Use wall-clock duration for throughput and add min/max speed
From: Luiz Augusto von Dentz <[email protected]>
The previous speed calculation used the sum of per-packet latencies as the denominator. For TX, each latency measures the time from command submission to completion event -- when multiple packets are in-flight simultaneously these overlap, making the sum much larger than wall-clock time and deflating the reported speed. For RX the inter-arrival sum roughly equals wall-clock time. This asymmetry caused the same data stream to report vastly different speeds depending on direction.
Fix by computing speed from wall-clock duration (last packet timestamp minus first packet timestamp), which gives consistent results regardless of direction. Also add 1-second windowed throughput tracking to report min and max speed alongside the average.
--- monitor/analyze.c | 92 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 8 deletions(-)
monitor/analyze.c +84 -8
@@ -61,6 +61,14 @@ struct hci_stats {struct queue *plot;uint16_t min;uint16_t max; +/* Wall-clock throughput tracking */ +struct timeval first_ts; +struct timeval last_ts; +/* Windowed throughput (1-second windows) */ +struct timeval window_start; +size_t window_bytes; +long long speed_min;/* Kb/s, 0 = not set */ +long long speed_max;/* Kb/s */ };struct hci_conn {
@@ -141,6 +149,8 @@ static void plot_draw(struct queue *queue, const char *title)static void print_stats (struct hci_stats *stats,const char *label) { +long long duration_ms; +if (!stats->num)return ;
@@ -152,9 +162,41 @@ static void print_stats(struct hci_stats *stats, const char *label)print_field ("%s size: %u-%u octets (~%zd octets)" , label, stats->min, stats->max, stats->bytes / stats->num); -if (TV_MSEC (stats->latency.total)) -print_field ("%s speed: ~%lld Kb/s" , label, - stats->bytes *8 /TV_MSEC (stats->latency.total)); +/* Compute wall-clock speed from first/last packet timestamps */ + duration_ms =TV_MSEC (stats->last_ts) -TV_MSEC (stats->first_ts); +if (duration_ms >0 ) { +long long avg_speed = stats->bytes *8 / duration_ms; + +/* Close the last window for min/max if it has data */ +if (stats->window_bytes >0 && stats->num >1 ) { +struct timeval delta; +long long last_win_ms; + +timersub (&stats->last_ts, &stats->window_start, + &delta); + last_win_ms =TV_MSEC (delta); +if (last_win_ms >0 ) { +long long speed; + + speed = stats->window_bytes *8 / + last_win_ms; +if (!stats->speed_min || + speed < stats->speed_min) + stats->speed_min = speed; +if (speed > stats->speed_max) + stats->speed_max = speed; + } + } + +if (stats->speed_min && stats->speed_max) +print_field ("%s speed: ~%lld Kb/s " +"(min ~%lld Kb/s max ~%lld Kb/s)" , + label, avg_speed, + stats->speed_min, stats->speed_max); +else +print_field ("%s speed: ~%lld Kb/s" , label, + avg_speed); + }plot_draw (stats->plot, label); }
@@ -1066,7 +1108,8 @@ static void event_pkt(struct timeval *tv, uint16_t index, } } -static void stats_add (struct hci_stats *stats,uint16_t size) +static void stats_add (struct hci_stats *stats,struct timeval *tv, +uint16_t size) { stats->num++; stats->bytes += size;
@@ -1075,6 +1118,39 @@ static void stats_add(struct hci_stats *stats, uint16_t size) stats->min = size;if (!stats->max || size > stats->max) stats->max = size; + +/* Wall-clock timestamp tracking */ +if (!timerisset (&stats->first_ts)) + stats->first_ts = *tv; + stats->last_ts = *tv; + +/* Windowed throughput: 1-second windows */ +if (!timerisset (&stats->window_start)) { + stats->window_start = *tv; + stats->window_bytes = size; + }else { +struct timeval delta; + +timersub (tv, &stats->window_start, &delta); +if (TV_MSEC (delta) >=1000 ) { +/* Close current window, compute speed */ +long long speed; + + speed = stats->window_bytes *8 / +TV_MSEC (delta); + +if (!stats->speed_min || speed < stats->speed_min) + stats->speed_min = speed; +if (speed > stats->speed_max) + stats->speed_max = speed; + +/* Start new window */ + stats->window_start = *tv; + stats->window_bytes = size; + }else { + stats->window_bytes += size; + } + } } static void conn_pkt_tx(struct hci_conn *conn, struct timeval *tv,
@@ -1087,10 +1163,10 @@ static void conn_pkt_tx(struct hci_conn *conn, struct timeval *tv, last_tx->chan = chan;queue_push_tail (conn->tx_queue, last_tx); -stats_add (&conn->tx, size); +stats_add (&conn->tx, tv, size);if (chan) -stats_add (&chan->tx, size); +stats_add (&chan->tx, tv, size); } static void conn_pkt_rx(struct hci_conn *conn, struct timeval *tv,
@@ -1106,7 +1182,7 @@ static void conn_pkt_rx(struct hci_conn *conn, struct timeval *tv, conn->last_rx = *tv; -stats_add (&conn->rx, size); +stats_add (&conn->rx, tv, size); conn->rx.num_comp++;if (chan) {
@@ -1118,7 +1194,7 @@ static void conn_pkt_rx(struct hci_conn *conn, struct timeval *tv, chan->last_rx = *tv; -stats_add (&chan->rx, size); +stats_add (&chan->rx, tv, size); chan->rx.num_comp++; } }
Signature · 1 line
2.53.0
Luiz Augusto von Dentz
maintainerPATCH 3/3 doc: Add throughput estimation section to btmon-l2cap.rst
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
Document the throughput statistics reported by btmon analyze mode, including speed calculation methodology, channel details (PSM, mode, MTU, MPS), and caveats about inter-packet latency vs wall-clock time. Also document the windowed throughput approach used by btsnoop-analyzer.
--- doc/btmon-l2cap.rst | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+)
doc/btmon-l2cap.rst +56 -0
@@ -190,3 +190,59 @@ Automating L2CAP Analysis channel, note the Source CID and Destination CID from the Connection Request/Response pair. Then search for those CIDs in subsequent data frames. + +Throughput Estimation +---------------------- + +btmon's ``--analyze`` (``-a``) mode computes per-channel statistics +including throughput. For each L2CAP channel it reports: + +- **Speed**: Computed as ``bytes * 8 / latency_sum_ms`` where + ``latency_sum_ms`` is the sum of inter-packet deltas (not wall-clock + duration). This means idle gaps are excluded, so the figure + represents the *active sending rate* rather than application-level + throughput. + +- **Min/Avg/Max latency**: Per-packet inter-arrival time range. + +Example output:: + + Found TX L2CAP channel with CID 64 + PSM 128 (0x0080) + Mode: LE Credit + MTU: 672 + MPS: 490 + TX packets: 29120/29114 + TX Latency: 1-79 msec (~29 msec) + TX size: 494-494 octets (~494 octets) + TX speed: ~571 Kb/s + +**Channel details shown in analyze mode:** + +- **Fixed channels** (CID <= 7) display their protocol name + (e.g., ``ATT``, ``L2CAP Signaling (LE)``). +- **PSM** is shown in both decimal and hexadecimal. +- **Mode** is decoded from Configure Request options (BR/EDR) or + LE signaling (``Basic``, ``ERTM``, ``LE Credit``, + ``Enhanced Credit``, etc.). +- **MTU** and **MPS** are extracted from signaling exchanges. + +**Throughput caveats:** + +1. The speed value uses inter-packet latency sums as the denominator, + not wall-clock elapsed time. If the sender pauses (e.g., waiting + for credits), the idle period is not counted, which inflates the + reported speed relative to overall throughput. + +2. TX latency is measured from command submission to completion event. + RX latency is the inter-arrival time between consecutive packets. + These measure different things, so TX and RX speeds for the same + channel are not directly comparable. + +3. For windowed throughput (min/avg/max), btsnoop-analyzer uses + 1-second sampling windows over wall-clock time, which provides + a more realistic view of application-level bandwidth variation. + +**Automated throughput extraction** from btmon analyze output:: + + btmon -a trace.btsnoop 2>/dev/null | grep -E "speed:|Mode:|MTU:|MPS:|PSM"
Signature · 1 line
2.53.0
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1081747
---Test result---
Test Summary:
CheckPatch PASS 1.40 seconds GitLint FAIL 1.61 seconds BuildEll PASS 18.08 seconds BluezMake PASS 673.13 seconds MakeCheck PASS 18.27 seconds MakeDistcheck PASS 227.65 seconds CheckValgrind PASS 277.64 seconds CheckSmatch PASS 317.95 seconds bluezmakeextell PASS 172.05 seconds IncrementalBuild PASS 705.73 seconds ScanBuild PASS 957.94 seconds
Details
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[BlueZ,v1,2/3] monitor: Use wall-clock duration for throughput and add min/max speed
WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
1: T1 Title exceeds max length (84>80): "[BlueZ,v1,2/3] monitor: Use wall-clock duration for throughput and add min/max speed"
https://github.com/bluez/bluez/pull/2042
---
Regards,
Linux Bluetooth
This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:
Hello:
This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:
Quoted 14 lines · On Wed, 15 Apr 2026 16:58:16 -0400 you wrote:
From: Luiz Augusto von Dentz <[email protected]>
Enhance btmon analyze mode to display richer per-channel information:
- Add MTU, MPS, and mode fields to struct
l2cap_chan
- Parse Configure Request/Response options for BR/EDR channels to extract MTU (option0x01) and mode (option0x04)
- Addl2cap_le_sig()to parse LE signaling (CID 5) for LE Credit Based Connection Request/Response and Enhanced Credit Connection Request, extracting PSM, MTU, MPS, and mode
- Display fixed channel names (ATT, L2CAP Signaling, SMP) for CID <= 7
- Show PSM in both decimal and hex format
- Print mode, MTU, and MPS for dynamic channels
Quoted 1 line
[...]
Here is the summary with links:
- [BlueZ,v1,1/3] monitor: Add L2CAP channel details to analyze output
https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=6b4c21721c80 - [BlueZ,v1,2/3] monitor: Use wall-clock duration for throughput and add min/max speed https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=ebeceecd3ccb - [BlueZ,v1,3/3] doc: Add throughput estimation section to btmon-l2cap.rst https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=f360d9e4428d
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html