Skip to contentSkip to conversation

net: Bluetooth: add TX timestamping for ISO/L2CAP/SCO

PATCH Superseded Pauli Virtanen 5 participants 17 messages Feb 9, 2025 – Feb 15, 2025

Subsystems (8)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover net: Bluetooth: add TX timestamping for ISO/L2CAP/SCO

Add support for TX timestamping in Bluetooth ISO/L2CAP/SCO sockets.

Add new COMPLETION timestamp type, to report a software timestamp when the hardware reports a packet completed. (Cc netdev for this)

Overview
========

The packet flow in Bluetooth is the following. Timestamps added here indicated:

user sendmsg() generates skbs
|
* skb waits in net/bluetooth queue for a free HW packet slot |
* orphan skb, send to driver -> TSTAMP_SND
|
* driver: send packet data to transport (eg. USB)
|
* wait for transport completion
|
* driver: transport tx completion, free skb (some do this immediately) |
* packet waits in HW side queue
|
* HCI report for packet completion -> TSTAMP_COMPLETION (for non-SCO)

In addition, we may want to do the following in future (but not implemented in this series as we don't have ISO sequence number synchronization yet which is needed first, moreover e.g. Intel controllers return only zeros in timestamps):

* if packet is ISO, send HCI LE Read ISO TX Sync
|
* HCI response -> hardware TSTAMP_SND for the packet the response corresponds to if it was waiting for one, might not be possible to get a tstamp for every packet

Bluetooth does not have tx timestamps in the completion reports from hardware, and only for ISO packets there are HCI commands in specification for querying timestamps afterward.

The drivers do not provide ways to get timestamps either, I'm also not aware if some devices would have vendor-specific commands to get them.

Driver-side timestamps
======================

Generating SND on driver side may be slightly more accurate, but that requires changing the BT driver API to not orphan skbs first. In theory this probably won't cause problems, but it is not done in this patchset.

For some of the drivers it won't gain much. E.g. btusb immediately submits the URB, so if one would emit SND just before submit (as drivers/net/usb/usbnet.c does), it is essentially identical to emitting before sending to driver. btintel_pcie looks like it does synchronous send, so looks the same. hci_serdev has internal queue, iiuc flushing as fast as data can be transferred, but it shouldn't be waiting for hardware slots due to HCI flow control.

Unless HW buffers are full, packets mostly wait on the HW side. E.g. with btusb (non-SCO) median time from sendmsg() to URB generation is ~0.1 ms, to USB completion ~0.5 ms, and HCI completion report at ~5 ms.

The exception is SCO, for which HCI flow control is disabled, so they do not get completion events so it's possible to build up queues inside the driver. For SCO, COMPLETION needs to be generated from driver side, eg. for btusb maybe at URB completion. This could be useful for SCO PCM modes (but which are more or less obsolete nowadays), where USB isoc data rate matches audio data rate, so queues on USB side may build up.

Use cases
=========

In audio use cases we want to track and avoid queues building up, to control latency, especially in cases like ISO where the controller has a fixed schedule that the sending application must match. E.g. application can aim to keep 1 packet in HW queue, so it has 2*7.5ms of slack for being woken up too late.

Applications can use SND & COMPLETION timestamps to track in-kernel and in-HW packet queues separately. This can matter for ISO, where the specification allows HW to use the timings when it gets packets to determine what packets are synchronized together. Applications can use SND to track that.

Changes in v3
=============

- Add new COMPLETION timestamp type, and emit it in HCI completion.

- Emit SND instead of SCHED, when sending to driver.

- Do not emit SCHED timestamps.

- Don't safeguard tx_q length explicitly. Now that hci_sched_acl_blk() is no more, the scheduler flow control is guaranteed to keep it bounded.

- Fix L2CAP stream sockets to use the bytestream timestamp conventions.

Pauli Virtanen (5):
net-timestamp: COMPLETION timestamp on packet tx completion
Bluetooth: add support for skb TX SND/COMPLETION timestamping
Bluetooth: ISO: add TX timestamping
Bluetooth: L2CAP: add TX timestamping
Bluetooth: SCO: add TX timestamping socket-level mechanism

 Documentation/networking/timestamping.rst |   9 ++
 include/linux/skbuff.h                    |   6 +-
 include/net/bluetooth/bluetooth.h         |   1 +
 include/net/bluetooth/hci_core.h          |  13 +++
 include/net/bluetooth/l2cap.h             |   3 +-
 include/uapi/linux/errqueue.h             |   1 +
 include/uapi/linux/net_tstamp.h           |   6 +-
 net/bluetooth/6lowpan.c                   |   2 +-
 net/bluetooth/hci_conn.c                  | 117 ++++++++++++++++++++++
 net/bluetooth/hci_core.c                  |  17 +++-
 net/bluetooth/hci_event.c                 |   4 +
 net/bluetooth/iso.c                       |  24 ++++-
 net/bluetooth/l2cap_core.c                |  41 +++++++-
 net/bluetooth/l2cap_sock.c                |  15 ++-
 net/bluetooth/sco.c                       |  19 +++-
 net/bluetooth/smp.c                       |   2 +-
 net/ethtool/common.c                      |   1 +
 net/socket.c                              |   3 +
 18 files changed, 263 insertions(+), 21 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v3 1/5 net-timestamp: COMPLETION timestamp on packet tx completion

Add SOF_TIMESTAMPING_TX_COMPLETION, for requesting a software timestamp when hardware reports a packet completed.

Completion tstamp is useful for Bluetooth, where hardware tx timestamps cannot be obtained except for ISO packets, and the hardware has a queue where packets may wait. In this case the software SND timestamp only reflects the kernel-side part of the total latency (usually small) and queue length (usually 0 unless HW buffers congested), whereas the completion report time is more informative of the true latency.

It may also be useful in other cases where HW TX timestamps cannot be obtained and user wants to estimate an upper bound to when the TX probably happened.

---
 Documentation/networking/timestamping.rst | 9 +++++++++
 include/linux/skbuff.h                    | 6 +++++-
 include/uapi/linux/errqueue.h             | 1 +
 include/uapi/linux/net_tstamp.h           | 6 ++++--
 net/ethtool/common.c                      | 1 +
 net/socket.c                              | 3 +++
 6 files changed, 23 insertions(+), 3 deletions(-)
Documentation/networking/timestamping.rst +9 -0
index 61ef9da10e28..de2afed7a516 100644
@@ -140,6 +140,15 @@ SOF_TIMESTAMPING_TX_ACK:
   cumulative acknowledgment. The mechanism ignores SACK and FACK.
   This flag can be enabled via both socket options and control messages.
 
+SOF_TIMESTAMPING_TX_COMPLETION:
+  Request tx timestamps on packet tx completion.  The completion
+  timestamp is generated by the kernel when it receives packet a
+  completion report from the hardware. Hardware may report multiple
+  packets at once, and completion timestamps reflect the timing of the
+  report and not actual tx time. The completion timestamps are
+  currently implemented only for: Bluetooth L2CAP and ISO.  This
+  flag can be enabled via both socket options and control messages.
+
 
 1.3.2 Timestamp Reporting
 ^^^^^^^^^^^^^^^^^^^^^^^^^
include/linux/skbuff.h +5 -1
index bb2b751d274a..3707c9075ae9 100644
@@ -489,10 +489,14 @@ enum {
 
 	/* generate software time stamp when entering packet scheduling */
 	SKBTX_SCHED_TSTAMP = 1 << 6,
+
+	/* generate software time stamp on packet tx completion */
+	SKBTX_COMPLETION_TSTAMP = 1 << 7,
 };
 
 #define SKBTX_ANY_SW_TSTAMP	(SKBTX_SW_TSTAMP    | \
-				 SKBTX_SCHED_TSTAMP)
+				 SKBTX_SCHED_TSTAMP | \
+				 SKBTX_COMPLETION_TSTAMP)
 #define SKBTX_ANY_TSTAMP	(SKBTX_HW_TSTAMP | \
 				 SKBTX_HW_TSTAMP_USE_CYCLES | \
 				 SKBTX_ANY_SW_TSTAMP)
include/uapi/linux/errqueue.h +1 -0
index 3c70e8ac14b8..1ea47309d772 100644
@@ -73,6 +73,7 @@ enum {
 	SCM_TSTAMP_SND,		/* driver passed skb to NIC, or HW */
 	SCM_TSTAMP_SCHED,	/* data entered the packet scheduler */
 	SCM_TSTAMP_ACK,		/* data acknowledged by peer */
+	SCM_TSTAMP_COMPLETION,	/* packet tx completion */
 };
 
 #endif /* _UAPI_LINUX_ERRQUEUE_H */
include/uapi/linux/net_tstamp.h +4 -2
index 55b0ab51096c..383213de612a 100644
@@ -44,8 +44,9 @@ enum {
 	SOF_TIMESTAMPING_BIND_PHC = (1 << 15),
 	SOF_TIMESTAMPING_OPT_ID_TCP = (1 << 16),
 	SOF_TIMESTAMPING_OPT_RX_FILTER = (1 << 17),
+	SOF_TIMESTAMPING_TX_COMPLETION = (1 << 18),
 
-	SOF_TIMESTAMPING_LAST = SOF_TIMESTAMPING_OPT_RX_FILTER,
+	SOF_TIMESTAMPING_LAST = SOF_TIMESTAMPING_TX_COMPLETION,
 	SOF_TIMESTAMPING_MASK = (SOF_TIMESTAMPING_LAST - 1) |
 				 SOF_TIMESTAMPING_LAST
 };
@@ -58,7 +59,8 @@ enum {
 #define SOF_TIMESTAMPING_TX_RECORD_MASK	(SOF_TIMESTAMPING_TX_HARDWARE | \
 					 SOF_TIMESTAMPING_TX_SOFTWARE | \
 					 SOF_TIMESTAMPING_TX_SCHED | \
-					 SOF_TIMESTAMPING_TX_ACK)
+					 SOF_TIMESTAMPING_TX_ACK | \
+					 SOF_TIMESTAMPING_TX_COMPLETION)
 
 /**
  * struct so_timestamping - SO_TIMESTAMPING parameter
net/ethtool/common.c +1 -0
index 2bd77c94f9f1..75e3b756012e 100644
@@ -431,6 +431,7 @@ const char sof_timestamping_names[][ETH_GSTRING_LEN] = {
 	[const_ilog2(SOF_TIMESTAMPING_BIND_PHC)]     = "bind-phc",
 	[const_ilog2(SOF_TIMESTAMPING_OPT_ID_TCP)]   = "option-id-tcp",
 	[const_ilog2(SOF_TIMESTAMPING_OPT_RX_FILTER)] = "option-rx-filter",
+	[const_ilog2(SOF_TIMESTAMPING_TX_COMPLETION)] = "completion-transmit",
 };
 static_assert(ARRAY_SIZE(sof_timestamping_names) == __SOF_TIMESTAMPING_CNT);
 
net/socket.c +3 -0
index 4afe31656a2b..22b7f6f50889 100644
@@ -693,6 +693,9 @@ void __sock_tx_timestamp(__u32 tsflags, __u8 *tx_flags)
 	if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
 		flags |= SKBTX_SCHED_TSTAMP;
 
+	if (tsflags & SOF_TIMESTAMPING_TX_COMPLETION)
+		flags |= SKBTX_COMPLETION_TSTAMP;
+
 	*tx_flags = flags;
 }
 EXPORT_SYMBOL(__sock_tx_timestamp);
Signature · 1 line
2.48.1
#2 lore raw

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=931989

---Test result---

Test Summary:

CheckPatch                    PENDING   0.33 seconds
GitLint                       PENDING   0.26 seconds
SubjectPrefix                 FAIL      0.57 seconds
BuildKernel                   PASS      23.88 seconds
CheckAllWarning               PASS      26.68 seconds
CheckSparse                   WARNING   30.05 seconds
BuildKernel32                 PASS      24.15 seconds
TestRunnerSetup               PASS      423.29 seconds
TestRunner_l2cap-tester       PASS      20.23 seconds
TestRunner_iso-tester         PASS      27.56 seconds
TestRunner_bnep-tester        PASS      4.76 seconds
TestRunner_mgmt-tester        PASS      119.84 seconds
TestRunner_rfcomm-tester      PASS      7.48 seconds
TestRunner_sco-tester         PASS      9.31 seconds
TestRunner_ioctl-tester       PASS      8.91 seconds
TestRunner_mesh-tester        PASS      5.96 seconds
TestRunner_smp-tester         PASS      6.86 seconds
TestRunner_userchan-tester    PASS      4.97 seconds
IncrementalBuild              PENDING   0.95 seconds

Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script Output:

##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:

##############################
Test: SubjectPrefix - FAIL
Desc: Check subject contains "Bluetooth" prefix
Output:
"Bluetooth: " prefix is not specified in the subject
##############################
Test: CheckSparse - WARNING
Desc: Run sparse tool with linux kernel
Output:
net/bluetooth/hci_event.c: note: in included file (through include/net/bluetooth/hci_core.h):net/bluetooth/sco.c: note: in included file:./include/net/bluetooth/hci_core.h:147:35: warning: array of flexible structures
##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series
Output:

---
Regards,
Linux Bluetooth

maintainer
#3 lore raw

Re: [PATCH v3 1/5] net-timestamp: COMPLETION timestamp on packet tx completion

In reply to Pauli Virtanen
Quoted 10 lines · Pauli Virtanen

Add SOF_TIMESTAMPING_TX_COMPLETION, for requesting a software timestamp when hardware reports a packet completed.

Completion tstamp is useful for Bluetooth, where hardware tx timestamps cannot be obtained except for ISO packets, and the hardware has a queue where packets may wait. In this case the software SND timestamp only reflects the kernel-side part of the total latency (usually small) and queue length (usually 0 unless HW buffers congested), whereas the completion report time is more informative of the true latency.

Quoted 3 lines

It may also be useful in other cases where HW TX timestamps cannot be obtained and user wants to estimate an upper bound to when the TX probably happened.

Getting the completion timestamp may indeed be useful more broadly.

Alternatively, the HW timestamp is relatively imprecisely defined so you could even just use that. Ideally, a hw timestamp conforms to IEEE 1588v2 PHY: first symbol on the wire IIRC. But in many cases this is not the case. It is not feasible at line rate, or the timestamp is only taken when the completion is written over PCI, which may be subject to PCI backpressure and happen after transmission on the wire. As a result, the worst case hw tstamp must already be assumed not much earlier than a completion timestamp.

That said, +1 on adding explicit well defined measurement point instead.

Quoted 17 lines
---
 Documentation/networking/timestamping.rst | 9 +++++++++
 include/linux/skbuff.h                    | 6 +++++-
 include/uapi/linux/errqueue.h             | 1 +
 include/uapi/linux/net_tstamp.h           | 6 ++++--
 net/ethtool/common.c                      | 1 +
 net/socket.c                              | 3 +++
 6 files changed, 23 insertions(+), 3 deletions(-)
Documentation/networking/timestamping.rst +0 -0
index 61ef9da10e28..de2afed7a516 100644
@@ -140,6 +140,15 @@ SOF_TIMESTAMPING_TX_ACK:
   cumulative acknowledgment. The mechanism ignores SACK and FACK.
   This flag can be enabled via both socket options and control messages.

Documentation/networking/timestamping.rst SOF_TIMESTAMPING_TX_ACK: · line 151

Quoted 10 lines
Documentation/networking/timestamping.rst +9 -0
 
+SOF_TIMESTAMPING_TX_COMPLETION:
+  Request tx timestamps on packet tx completion.  The completion
+  timestamp is generated by the kernel when it receives packet a
+  completion report from the hardware. Hardware may report multiple
+  packets at once, and completion timestamps reflect the timing of the
+  report and not actual tx time. The completion timestamps are
+  currently implemented only for: Bluetooth L2CAP and ISO.  This
+  flag can be enabled via both socket options and control messages.
+

Either we should support this uniformly, or it should be possible to query whether a driver supports this.

Unfortunately all completion callbacks are driver specific.

But drivers that support hwtstamps will call skb_tstamp_tx with nonzero hwtstamps. We could use that also to compute and queue a completion timestamp if requested. At least for existing NIC drivers.

Quoted 9 lines
Documentation/networking/timestamping.rst +0 -0
 1.3.2 Timestamp Reporting
 ^^^^^^^^^^^^^^^^^^^^^^^^^
include/linux/skbuff.h +0 -0
index bb2b751d274a..3707c9075ae9 100644
@@ -489,10 +489,14 @@ enum {
 
 	/* generate software time stamp when entering packet scheduling */

include/linux/skbuff.h enum { · line 499

Quoted 10 lines
include/linux/skbuff.h +5 -1
 	SKBTX_SCHED_TSTAMP = 1 << 6,
+
+	/* generate software time stamp on packet tx completion */
+	SKBTX_COMPLETION_TSTAMP = 1 << 7,
 };
 
 #define SKBTX_ANY_SW_TSTAMP	(SKBTX_SW_TSTAMP    | \
-				 SKBTX_SCHED_TSTAMP)
+				 SKBTX_SCHED_TSTAMP | \
+				 SKBTX_COMPLETION_TSTAMP)

These fields are used in the skb_shared_info tx_flags field. Which is a very scarce resource. This takes the last available bit. That is my only possible concern: the opportunity cost.

Quoted 45 lines
include/linux/skbuff.h +0 -0
 #define SKBTX_ANY_TSTAMP	(SKBTX_HW_TSTAMP | \
 				 SKBTX_HW_TSTAMP_USE_CYCLES | \
 				 SKBTX_ANY_SW_TSTAMP)
include/uapi/linux/errqueue.h +1 -0
index 3c70e8ac14b8..1ea47309d772 100644
@@ -73,6 +73,7 @@ enum {
 	SCM_TSTAMP_SND,		/* driver passed skb to NIC, or HW */
 	SCM_TSTAMP_SCHED,	/* data entered the packet scheduler */
 	SCM_TSTAMP_ACK,		/* data acknowledged by peer */
+	SCM_TSTAMP_COMPLETION,	/* packet tx completion */
 };
 
 #endif /* _UAPI_LINUX_ERRQUEUE_H */
include/uapi/linux/net_tstamp.h +4 -2
index 55b0ab51096c..383213de612a 100644
@@ -44,8 +44,9 @@ enum {
 	SOF_TIMESTAMPING_BIND_PHC = (1 << 15),
 	SOF_TIMESTAMPING_OPT_ID_TCP = (1 << 16),
 	SOF_TIMESTAMPING_OPT_RX_FILTER = (1 << 17),
+	SOF_TIMESTAMPING_TX_COMPLETION = (1 << 18),
 
-	SOF_TIMESTAMPING_LAST = SOF_TIMESTAMPING_OPT_RX_FILTER,
+	SOF_TIMESTAMPING_LAST = SOF_TIMESTAMPING_TX_COMPLETION,
 	SOF_TIMESTAMPING_MASK = (SOF_TIMESTAMPING_LAST - 1) |
 				 SOF_TIMESTAMPING_LAST
 };
@@ -58,7 +59,8 @@ enum {
 #define SOF_TIMESTAMPING_TX_RECORD_MASK	(SOF_TIMESTAMPING_TX_HARDWARE | \
 					 SOF_TIMESTAMPING_TX_SOFTWARE | \
 					 SOF_TIMESTAMPING_TX_SCHED | \
-					 SOF_TIMESTAMPING_TX_ACK)
+					 SOF_TIMESTAMPING_TX_ACK | \
+					 SOF_TIMESTAMPING_TX_COMPLETION)
 
 /**
  * struct so_timestamping - SO_TIMESTAMPING parameter
net/ethtool/common.c +0 -0
index 2bd77c94f9f1..75e3b756012e 100644

net/ethtool/common.c const char sof_timestamping_names[][ETH_GSTRING_LEN] = { · line 434

Quoted 4 lines
net/ethtool/common.c +1 -0
@@ -431,6 +431,7 @@ const char sof_timestamping_names[][ETH_GSTRING_LEN] = {
 	[const_ilog2(SOF_TIMESTAMPING_BIND_PHC)]     = "bind-phc",
 	[const_ilog2(SOF_TIMESTAMPING_OPT_ID_TCP)]   = "option-id-tcp",
 	[const_ilog2(SOF_TIMESTAMPING_OPT_RX_FILTER)] = "option-rx-filter",
+	[const_ilog2(SOF_TIMESTAMPING_TX_COMPLETION)] = "completion-transmit",

just "tx-completion"?

#4 lore raw

Re: [PATCH v3 1/5] net-timestamp: COMPLETION timestamp on packet tx completion

Hi,

su, 2025-02-09 kello 22:29 -0500, Willem de Bruijn kirjoitti:

Quoted 17 lines
Quoted 10 lines · Pauli Virtanen

Add SOF_TIMESTAMPING_TX_COMPLETION, for requesting a software timestamp when hardware reports a packet completed.

Completion tstamp is useful for Bluetooth, where hardware tx timestamps cannot be obtained except for ISO packets, and the hardware has a queue where packets may wait. In this case the software SND timestamp only reflects the kernel-side part of the total latency (usually small) and queue length (usually 0 unless HW buffers congested), whereas the completion report time is more informative of the true latency.

Quoted 3 lines

It may also be useful in other cases where HW TX timestamps cannot be obtained and user wants to estimate an upper bound to when the TX probably happened.

Getting the completion timestamp may indeed be useful more broadly.

Quoted 8 lines

Alternatively, the HW timestamp is relatively imprecisely defined so you could even just use that. Ideally, a hw timestamp conforms to IEEE 1588v2 PHY: first symbol on the wire IIRC. But in many cases this is not the case. It is not feasible at line rate, or the timestamp is only taken when the completion is written over PCI, which may be subject to PCI backpressure and happen after transmission on the wire. As a result, the worst case hw tstamp must already be assumed not much earlier than a completion timestamp.

For BT ISO packets, in theory hw-provided TX timestamps exist, and we might want both (with separate flags for enabling them). I don't really know, last I looked Intel HW didn't support them, and it's not clear to which degree they are useful.

Quoted 34 lines

That said, +1 on adding explicit well defined measurement point instead.

Quoted 17 lines
---
 Documentation/networking/timestamping.rst | 9 +++++++++
 include/linux/skbuff.h                    | 6 +++++-
 include/uapi/linux/errqueue.h             | 1 +
 include/uapi/linux/net_tstamp.h           | 6 ++++--
 net/ethtool/common.c                      | 1 +
 net/socket.c                              | 3 +++
 6 files changed, 23 insertions(+), 3 deletions(-)
Documentation/networking/timestamping.rst +0 -0
index 61ef9da10e28..de2afed7a516 100644
@@ -140,6 +140,15 @@ SOF_TIMESTAMPING_TX_ACK:
   cumulative acknowledgment. The mechanism ignores SACK and FACK.
   This flag can be enabled via both socket options and control messages.

Documentation/networking/timestamping.rst SOF_TIMESTAMPING_TX_ACK: · line 151

Quoted 10 lines
Documentation/networking/timestamping.rst +9 -0
 
+SOF_TIMESTAMPING_TX_COMPLETION:
+  Request tx timestamps on packet tx completion.  The completion
+  timestamp is generated by the kernel when it receives packet a
+  completion report from the hardware. Hardware may report multiple
+  packets at once, and completion timestamps reflect the timing of the
+  report and not actual tx time. The completion timestamps are
+  currently implemented only for: Bluetooth L2CAP and ISO.  This
+  flag can be enabled via both socket options and control messages.
+

Either we should support this uniformly, or it should be possible to query whether a driver supports this.

Quoted 6 lines

Unfortunately all completion callbacks are driver specific.

But drivers that support hwtstamps will call skb_tstamp_tx with nonzero hwtstamps. We could use that also to compute and queue a completion timestamp if requested. At least for existing NIC drivers.

Ok. If possible, I'd like to avoid changing the behavior of the non- Bluetooth parts of net/ here, as I'm not familiar with those.

I guess a simpler solution could be that sock_set_timestamping() checks the type of the socket, and gives EINVAL if the flag is set for non- Bluetooth sockets?

One could then postpone having to invent how to check the driver support, and user would know non-supported status from setsockopt failing.

Quoted 15 lines
Quoted 15 lines
Documentation/networking/timestamping.rst +0 -0
 1.3.2 Timestamp Reporting
 ^^^^^^^^^^^^^^^^^^^^^^^^^
include/linux/skbuff.h +3 -0
index bb2b751d274a..3707c9075ae9 100644
@@ -489,10 +489,14 @@ enum {
 
 	/* generate software time stamp when entering packet scheduling */
 	SKBTX_SCHED_TSTAMP = 1 << 6,
+
+	/* generate software time stamp on packet tx completion */
+	SKBTX_COMPLETION_TSTAMP = 1 << 7,
 };
 
Quoted 8 lines

include/linux/skbuff.h enum { · line 499

Quoted 4 lines
include/linux/skbuff.h +2 -1
 #define SKBTX_ANY_SW_TSTAMP	(SKBTX_SW_TSTAMP    | \
-				 SKBTX_SCHED_TSTAMP)
+				 SKBTX_SCHED_TSTAMP | \
+				 SKBTX_COMPLETION_TSTAMP)

These fields are used in the skb_shared_info tx_flags field. Which is a very scarce resource. This takes the last available bit. That is my only possible concern: the opportunity cost.

If doing it per-protocol sounds ok, it could be put in bt_skb_cb instead.

Since the completion timestamp didn't already exist, it maybe means it's probably not that important for other parts of net/

Quoted 38 lines
Quoted 38 lines
include/linux/skbuff.h +0 -0
 #define SKBTX_ANY_TSTAMP	(SKBTX_HW_TSTAMP | \
 				 SKBTX_HW_TSTAMP_USE_CYCLES | \
 				 SKBTX_ANY_SW_TSTAMP)
include/uapi/linux/errqueue.h +1 -0
index 3c70e8ac14b8..1ea47309d772 100644
@@ -73,6 +73,7 @@ enum {
 	SCM_TSTAMP_SND,		/* driver passed skb to NIC, or HW */
 	SCM_TSTAMP_SCHED,	/* data entered the packet scheduler */
 	SCM_TSTAMP_ACK,		/* data acknowledged by peer */
+	SCM_TSTAMP_COMPLETION,	/* packet tx completion */
 };
 
 #endif /* _UAPI_LINUX_ERRQUEUE_H */
include/uapi/linux/net_tstamp.h +4 -2
index 55b0ab51096c..383213de612a 100644
@@ -44,8 +44,9 @@ enum {
 	SOF_TIMESTAMPING_BIND_PHC = (1 << 15),
 	SOF_TIMESTAMPING_OPT_ID_TCP = (1 << 16),
 	SOF_TIMESTAMPING_OPT_RX_FILTER = (1 << 17),
+	SOF_TIMESTAMPING_TX_COMPLETION = (1 << 18),
 
-	SOF_TIMESTAMPING_LAST = SOF_TIMESTAMPING_OPT_RX_FILTER,
+	SOF_TIMESTAMPING_LAST = SOF_TIMESTAMPING_TX_COMPLETION,
 	SOF_TIMESTAMPING_MASK = (SOF_TIMESTAMPING_LAST - 1) |
 				 SOF_TIMESTAMPING_LAST
 };
@@ -58,7 +59,8 @@ enum {
 #define SOF_TIMESTAMPING_TX_RECORD_MASK	(SOF_TIMESTAMPING_TX_HARDWARE | \
 					 SOF_TIMESTAMPING_TX_SOFTWARE | \
 					 SOF_TIMESTAMPING_TX_SCHED | \
-					 SOF_TIMESTAMPING_TX_ACK)
+					 SOF_TIMESTAMPING_TX_ACK | \
+					 SOF_TIMESTAMPING_TX_COMPLETION)
 
Quoted 13 lines

net/ethtool/common.c const char sof_timestamping_names[][ETH_GSTRING_LEN] = { · line 434

Quoted 11 lines
include/uapi/linux/net_tstamp.h +0 -0
 /**
  * struct so_timestamping - SO_TIMESTAMPING parameter
net/ethtool/common.c +1 -0
index 2bd77c94f9f1..75e3b756012e 100644
@@ -431,6 +431,7 @@ const char sof_timestamping_names[][ETH_GSTRING_LEN] = {
 	[const_ilog2(SOF_TIMESTAMPING_BIND_PHC)]     = "bind-phc",
 	[const_ilog2(SOF_TIMESTAMPING_OPT_ID_TCP)]   = "option-id-tcp",
 	[const_ilog2(SOF_TIMESTAMPING_OPT_RX_FILTER)] = "option-rx-filter",
+	[const_ilog2(SOF_TIMESTAMPING_TX_COMPLETION)] = "completion-transmit",

just "tx-completion"?

Ok.

Signature · 1 line
Pauli Virtanen
maintainer
#5 lore raw

Re: [PATCH v3 1/5] net-timestamp: COMPLETION timestamp on packet tx completion

Quoted 29 lines · Pauli Virtanen

Hi,

su, 2025-02-09 kello 22:29 -0500, Willem de Bruijn kirjoitti:

Quoted 17 lines
Quoted 10 lines · Pauli Virtanen

Add SOF_TIMESTAMPING_TX_COMPLETION, for requesting a software timestamp when hardware reports a packet completed.

Completion tstamp is useful for Bluetooth, where hardware tx timestamps cannot be obtained except for ISO packets, and the hardware has a queue where packets may wait. In this case the software SND timestamp only reflects the kernel-side part of the total latency (usually small) and queue length (usually 0 unless HW buffers congested), whereas the completion report time is more informative of the true latency.

Quoted 3 lines

It may also be useful in other cases where HW TX timestamps cannot be obtained and user wants to estimate an upper bound to when the TX probably happened.

Getting the completion timestamp may indeed be useful more broadly.

Quoted 8 lines

Alternatively, the HW timestamp is relatively imprecisely defined so you could even just use that. Ideally, a hw timestamp conforms to IEEE 1588v2 PHY: first symbol on the wire IIRC. But in many cases this is not the case. It is not feasible at line rate, or the timestamp is only taken when the completion is written over PCI, which may be subject to PCI backpressure and happen after transmission on the wire. As a result, the worst case hw tstamp must already be assumed not much earlier than a completion timestamp.

Quoted 4 lines

For BT ISO packets, in theory hw-provided TX timestamps exist, and we might want both (with separate flags for enabling them). I don't really know, last I looked Intel HW didn't support them, and it's not clear to which degree they are useful.

That's reason enough to separate these measurement types.

If we don't do it properly now, we won't be able to update drivers later once users depend on requesting hw timestamps when they mean to get completion timestamps.

Quoted 41 lines
Quoted 34 lines

That said, +1 on adding explicit well defined measurement point instead.

Quoted 17 lines
---
 Documentation/networking/timestamping.rst | 9 +++++++++
 include/linux/skbuff.h                    | 6 +++++-
 include/uapi/linux/errqueue.h             | 1 +
 include/uapi/linux/net_tstamp.h           | 6 ++++--
 net/ethtool/common.c                      | 1 +
 net/socket.c                              | 3 +++
 6 files changed, 23 insertions(+), 3 deletions(-)
Documentation/networking/timestamping.rst +0 -0
index 61ef9da10e28..de2afed7a516 100644
@@ -140,6 +140,15 @@ SOF_TIMESTAMPING_TX_ACK:
   cumulative acknowledgment. The mechanism ignores SACK and FACK.
   This flag can be enabled via both socket options and control messages.

Documentation/networking/timestamping.rst SOF_TIMESTAMPING_TX_ACK: · line 151

Quoted 10 lines
Documentation/networking/timestamping.rst +9 -0
 
+SOF_TIMESTAMPING_TX_COMPLETION:
+  Request tx timestamps on packet tx completion.  The completion
+  timestamp is generated by the kernel when it receives packet a
+  completion report from the hardware. Hardware may report multiple
+  packets at once, and completion timestamps reflect the timing of the
+  report and not actual tx time. The completion timestamps are
+  currently implemented only for: Bluetooth L2CAP and ISO.  This
+  flag can be enabled via both socket options and control messages.
+

Either we should support this uniformly, or it should be possible to query whether a driver supports this.

Quoted 6 lines

Unfortunately all completion callbacks are driver specific.

But drivers that support hwtstamps will call skb_tstamp_tx with nonzero hwtstamps. We could use that also to compute and queue a completion timestamp if requested. At least for existing NIC drivers.

Quoted 6 lines

Ok. If possible, I'd like to avoid changing the behavior of the non- Bluetooth parts of net/ here, as I'm not familiar with those.

I guess a simpler solution could be that sock_set_timestamping() checks the type of the socket, and gives EINVAL if the flag is set for non- Bluetooth sockets?

Actually, I'd prefer to have this completion timestamp ability for all drivers. And avoid creating subsystem private mechanisms.

I suppose we can punt on the get_ts_info control API if need be.

Quoted 28 lines

One could then postpone having to invent how to check the driver support, and user would know non-supported status from setsockopt failing.

Quoted 15 lines
Quoted 15 lines
Documentation/networking/timestamping.rst +0 -0
 1.3.2 Timestamp Reporting
 ^^^^^^^^^^^^^^^^^^^^^^^^^
include/linux/skbuff.h +3 -0
index bb2b751d274a..3707c9075ae9 100644
@@ -489,10 +489,14 @@ enum {
 
 	/* generate software time stamp when entering packet scheduling */
 	SKBTX_SCHED_TSTAMP = 1 << 6,
+
+	/* generate software time stamp on packet tx completion */
+	SKBTX_COMPLETION_TSTAMP = 1 << 7,
 };
 
Quoted 8 lines

include/linux/skbuff.h enum { · line 499

Quoted 4 lines
include/linux/skbuff.h +2 -1
 #define SKBTX_ANY_SW_TSTAMP	(SKBTX_SW_TSTAMP    | \
-				 SKBTX_SCHED_TSTAMP)
+				 SKBTX_SCHED_TSTAMP | \
+				 SKBTX_COMPLETION_TSTAMP)

These fields are used in the skb_shared_info tx_flags field. Which is a very scarce resource. This takes the last available bit. That is my only possible concern: the opportunity cost.

Quoted 5 lines

If doing it per-protocol sounds ok, it could be put in bt_skb_cb instead.

Since the completion timestamp didn't already exist, it maybe means it's probably not that important for other parts of net/

I can see its value especially for hardware that does not support hardware timestamps, or hw timestamps at line rate.

This gives a reasonable estimation of transmission time and measure of device delay.

It is device specific whether it will be an over- or under-estimation, depending on whether the completion is queued to the host after or before the data is written on the wire. But either way, it will include the delay in processing the tx queue, which on multi-queue NICs and with TSO may be substantial (even before considering HW rate limiting).

Quoted 56 lines
Quoted 38 lines
Quoted 38 lines
include/linux/skbuff.h +0 -0
 #define SKBTX_ANY_TSTAMP	(SKBTX_HW_TSTAMP | \
 				 SKBTX_HW_TSTAMP_USE_CYCLES | \
 				 SKBTX_ANY_SW_TSTAMP)
include/uapi/linux/errqueue.h +1 -0
index 3c70e8ac14b8..1ea47309d772 100644
@@ -73,6 +73,7 @@ enum {
 	SCM_TSTAMP_SND,		/* driver passed skb to NIC, or HW */
 	SCM_TSTAMP_SCHED,	/* data entered the packet scheduler */
 	SCM_TSTAMP_ACK,		/* data acknowledged by peer */
+	SCM_TSTAMP_COMPLETION,	/* packet tx completion */
 };
 
 #endif /* _UAPI_LINUX_ERRQUEUE_H */
include/uapi/linux/net_tstamp.h +4 -2
index 55b0ab51096c..383213de612a 100644
@@ -44,8 +44,9 @@ enum {
 	SOF_TIMESTAMPING_BIND_PHC = (1 << 15),
 	SOF_TIMESTAMPING_OPT_ID_TCP = (1 << 16),
 	SOF_TIMESTAMPING_OPT_RX_FILTER = (1 << 17),
+	SOF_TIMESTAMPING_TX_COMPLETION = (1 << 18),
 
-	SOF_TIMESTAMPING_LAST = SOF_TIMESTAMPING_OPT_RX_FILTER,
+	SOF_TIMESTAMPING_LAST = SOF_TIMESTAMPING_TX_COMPLETION,
 	SOF_TIMESTAMPING_MASK = (SOF_TIMESTAMPING_LAST - 1) |
 				 SOF_TIMESTAMPING_LAST
 };
@@ -58,7 +59,8 @@ enum {
 #define SOF_TIMESTAMPING_TX_RECORD_MASK	(SOF_TIMESTAMPING_TX_HARDWARE | \
 					 SOF_TIMESTAMPING_TX_SOFTWARE | \
 					 SOF_TIMESTAMPING_TX_SCHED | \
-					 SOF_TIMESTAMPING_TX_ACK)
+					 SOF_TIMESTAMPING_TX_ACK | \
+					 SOF_TIMESTAMPING_TX_COMPLETION)
 
Quoted 13 lines

net/ethtool/common.c const char sof_timestamping_names[][ETH_GSTRING_LEN] = { · line 434

Quoted 11 lines
include/uapi/linux/net_tstamp.h +0 -0
 /**
  * struct so_timestamping - SO_TIMESTAMPING parameter
net/ethtool/common.c +1 -0
index 2bd77c94f9f1..75e3b756012e 100644
@@ -431,6 +431,7 @@ const char sof_timestamping_names[][ETH_GSTRING_LEN] = {
 	[const_ilog2(SOF_TIMESTAMPING_BIND_PHC)]     = "bind-phc",
 	[const_ilog2(SOF_TIMESTAMPING_OPT_ID_TCP)]   = "option-id-tcp",
 	[const_ilog2(SOF_TIMESTAMPING_OPT_RX_FILTER)] = "option-rx-filter",
+	[const_ilog2(SOF_TIMESTAMPING_TX_COMPLETION)] = "completion-transmit",

just "tx-completion"?

Ok.

Signature · 1 line
Pauli Virtanen
#6 lore raw

Re: [PATCH v3 1/5] net-timestamp: COMPLETION timestamp on packet tx completion

Hi Willem,

Quoted 86 lines · Willem de Bruijn
Quoted 29 lines · Pauli Virtanen

Hi,

su, 2025-02-09 kello 22:29 -0500, Willem de Bruijn kirjoitti:

Quoted 17 lines
Quoted 10 lines · Pauli Virtanen

Add SOF_TIMESTAMPING_TX_COMPLETION, for requesting a software timestamp when hardware reports a packet completed.

Completion tstamp is useful for Bluetooth, where hardware tx timestamps cannot be obtained except for ISO packets, and the hardware has a queue where packets may wait. In this case the software SND timestamp only reflects the kernel-side part of the total latency (usually small) and queue length (usually 0 unless HW buffers congested), whereas the completion report time is more informative of the true latency.

Quoted 3 lines

It may also be useful in other cases where HW TX timestamps cannot be obtained and user wants to estimate an upper bound to when the TX probably happened.

Getting the completion timestamp may indeed be useful more broadly.

Quoted 8 lines

Alternatively, the HW timestamp is relatively imprecisely defined so you could even just use that. Ideally, a hw timestamp conforms to IEEE 1588v2 PHY: first symbol on the wire IIRC. But in many cases this is not the case. It is not feasible at line rate, or the timestamp is only taken when the completion is written over PCI, which may be subject to PCI backpressure and happen after transmission on the wire. As a result, the worst case hw tstamp must already be assumed not much earlier than a completion timestamp.

Quoted 4 lines

For BT ISO packets, in theory hw-provided TX timestamps exist, and we might want both (with separate flags for enabling them). I don't really know, last I looked Intel HW didn't support them, and it's not clear to which degree they are useful.

That's reason enough to separate these measurement types.

If we don't do it properly now, we won't be able to update drivers later once users depend on requesting hw timestamps when they mean to get completion timestamps.

Quoted 44 lines
Quoted 34 lines

That said, +1 on adding explicit well defined measurement point instead.

Quoted 17 lines
---
 Documentation/networking/timestamping.rst | 9 +++++++++
 include/linux/skbuff.h                    | 6 +++++-
 include/uapi/linux/errqueue.h             | 1 +
 include/uapi/linux/net_tstamp.h           | 6 ++++--
 net/ethtool/common.c                      | 1 +
 net/socket.c                              | 3 +++
 6 files changed, 23 insertions(+), 3 deletions(-)
Documentation/networking/timestamping.rst +0 -0
index 61ef9da10e28..de2afed7a516 100644
@@ -140,6 +140,15 @@ SOF_TIMESTAMPING_TX_ACK:
   cumulative acknowledgment. The mechanism ignores SACK and FACK.
   This flag can be enabled via both socket options and control messages.

Documentation/networking/timestamping.rst SOF_TIMESTAMPING_TX_ACK: · line 151

Quoted 10 lines
Documentation/networking/timestamping.rst +9 -0
 
+SOF_TIMESTAMPING_TX_COMPLETION:
+  Request tx timestamps on packet tx completion.  The completion
+  timestamp is generated by the kernel when it receives packet a
+  completion report from the hardware. Hardware may report multiple
+  packets at once, and completion timestamps reflect the timing of the
+  report and not actual tx time. The completion timestamps are
+  currently implemented only for: Bluetooth L2CAP and ISO.  This
+  flag can be enabled via both socket options and control messages.
+

Either we should support this uniformly, or it should be possible to query whether a driver supports this.

Quoted 6 lines

Unfortunately all completion callbacks are driver specific.

But drivers that support hwtstamps will call skb_tstamp_tx with nonzero hwtstamps. We could use that also to compute and queue a completion timestamp if requested. At least for existing NIC drivers.

Ok. If possible, I'd like to avoid changing the behavior of the non- Bluetooth parts of net/ here, as I'm not familiar with those.

Quoted 8 lines
Quoted 3 lines

I guess a simpler solution could be that sock_set_timestamping() checks the type of the socket, and gives EINVAL if the flag is set for non- Bluetooth sockets?

Actually, I'd prefer to have this completion timestamp ability for all drivers. And avoid creating subsystem private mechanisms.

I suppose we can punt on the get_ts_info control API if need be.

I guess that it is reasonable if we don't have to do the work for drivers other than Bluetooth otherwise I'd say you are probably asking too much here, also doesn't this land on the TSN space if one needs to tightly control timings? I suspect if this sort of change was not necessary for TSN then perhaps it wouldn't be of much value to try to generalize this.

Quoted 105 lines
Quoted 28 lines

One could then postpone having to invent how to check the driver support, and user would know non-supported status from setsockopt failing.

Quoted 15 lines
Quoted 15 lines
Documentation/networking/timestamping.rst +0 -0
 1.3.2 Timestamp Reporting
 ^^^^^^^^^^^^^^^^^^^^^^^^^
include/linux/skbuff.h +3 -0
index bb2b751d274a..3707c9075ae9 100644
@@ -489,10 +489,14 @@ enum {
 
  /* generate software time stamp when entering packet scheduling */
  SKBTX_SCHED_TSTAMP = 1 << 6,
+
+ /* generate software time stamp on packet tx completion */
+ SKBTX_COMPLETION_TSTAMP = 1 << 7,
 };
 
Quoted 8 lines

include/linux/skbuff.h enum { · line 499

Quoted 4 lines
include/linux/skbuff.h +2 -1
 #define SKBTX_ANY_SW_TSTAMP      (SKBTX_SW_TSTAMP    | \
-                          SKBTX_SCHED_TSTAMP)
+                          SKBTX_SCHED_TSTAMP | \
+                          SKBTX_COMPLETION_TSTAMP)

These fields are used in the skb_shared_info tx_flags field. Which is a very scarce resource. This takes the last available bit. That is my only possible concern: the opportunity cost.

Quoted 5 lines

If doing it per-protocol sounds ok, it could be put in bt_skb_cb instead.

Since the completion timestamp didn't already exist, it maybe means it's probably not that important for other parts of net/

I can see its value especially for hardware that does not support hardware timestamps, or hw timestamps at line rate.

This gives a reasonable estimation of transmission time and measure of device delay.

It is device specific whether it will be an over- or under-estimation, depending on whether the completion is queued to the host after or before the data is written on the wire. But either way, it will include the delay in processing the tx queue, which on multi-queue NICs and with TSO may be substantial (even before considering HW rate limiting).

Quoted 56 lines
Quoted 38 lines
Quoted 38 lines
include/linux/skbuff.h +0 -0
 #define SKBTX_ANY_TSTAMP (SKBTX_HW_TSTAMP | \
                           SKBTX_HW_TSTAMP_USE_CYCLES | \
                           SKBTX_ANY_SW_TSTAMP)
include/uapi/linux/errqueue.h +1 -0
index 3c70e8ac14b8..1ea47309d772 100644
@@ -73,6 +73,7 @@ enum {
  SCM_TSTAMP_SND,         /* driver passed skb to NIC, or HW */
  SCM_TSTAMP_SCHED,       /* data entered the packet scheduler */
  SCM_TSTAMP_ACK,         /* data acknowledged by peer */
+ SCM_TSTAMP_COMPLETION,  /* packet tx completion */
 };
 
 #endif /* _UAPI_LINUX_ERRQUEUE_H */
include/uapi/linux/net_tstamp.h +4 -2
index 55b0ab51096c..383213de612a 100644
@@ -44,8 +44,9 @@ enum {
  SOF_TIMESTAMPING_BIND_PHC = (1 << 15),
  SOF_TIMESTAMPING_OPT_ID_TCP = (1 << 16),
  SOF_TIMESTAMPING_OPT_RX_FILTER = (1 << 17),
+ SOF_TIMESTAMPING_TX_COMPLETION = (1 << 18),
 
- SOF_TIMESTAMPING_LAST = SOF_TIMESTAMPING_OPT_RX_FILTER,
+ SOF_TIMESTAMPING_LAST = SOF_TIMESTAMPING_TX_COMPLETION,
  SOF_TIMESTAMPING_MASK = (SOF_TIMESTAMPING_LAST - 1) |
                           SOF_TIMESTAMPING_LAST
 };
@@ -58,7 +59,8 @@ enum {
 #define SOF_TIMESTAMPING_TX_RECORD_MASK  (SOF_TIMESTAMPING_TX_HARDWARE | \
                                   SOF_TIMESTAMPING_TX_SOFTWARE | \
                                   SOF_TIMESTAMPING_TX_SCHED | \
-                                  SOF_TIMESTAMPING_TX_ACK)
+                                  SOF_TIMESTAMPING_TX_ACK | \
+                                  SOF_TIMESTAMPING_TX_COMPLETION)
 
Quoted 13 lines

net/ethtool/common.c const char sof_timestamping_names[][ETH_GSTRING_LEN] = { · line 434

Quoted 11 lines
include/uapi/linux/net_tstamp.h +0 -0
 /**
  * struct so_timestamping - SO_TIMESTAMPING parameter
net/ethtool/common.c +1 -0
index 2bd77c94f9f1..75e3b756012e 100644
@@ -431,6 +431,7 @@ const char sof_timestamping_names[][ETH_GSTRING_LEN] = {
  [const_ilog2(SOF_TIMESTAMPING_BIND_PHC)]     = "bind-phc",
  [const_ilog2(SOF_TIMESTAMPING_OPT_ID_TCP)]   = "option-id-tcp",
  [const_ilog2(SOF_TIMESTAMPING_OPT_RX_FILTER)] = "option-rx-filter",
+ [const_ilog2(SOF_TIMESTAMPING_TX_COMPLETION)] = "completion-transmit",

just "tx-completion"?

Ok.

--
Pauli Virtanen

Signature · 1 line
Luiz Augusto von Dentz
maintainer
#7 lore raw

Re: [PATCH v3 1/5] net-timestamp: COMPLETION timestamp on packet tx completion

Quoted 94 lines · Luiz Augusto von Dentz

Hi Willem,

Quoted 90 lines · Willem de Bruijn
Quoted 29 lines · Pauli Virtanen

Hi,

su, 2025-02-09 kello 22:29 -0500, Willem de Bruijn kirjoitti:

Quoted 17 lines
Quoted 10 lines · Pauli Virtanen

Add SOF_TIMESTAMPING_TX_COMPLETION, for requesting a software timestamp when hardware reports a packet completed.

Completion tstamp is useful for Bluetooth, where hardware tx timestamps cannot be obtained except for ISO packets, and the hardware has a queue where packets may wait. In this case the software SND timestamp only reflects the kernel-side part of the total latency (usually small) and queue length (usually 0 unless HW buffers congested), whereas the completion report time is more informative of the true latency.

Quoted 3 lines

It may also be useful in other cases where HW TX timestamps cannot be obtained and user wants to estimate an upper bound to when the TX probably happened.

Getting the completion timestamp may indeed be useful more broadly.

Quoted 8 lines

Alternatively, the HW timestamp is relatively imprecisely defined so you could even just use that. Ideally, a hw timestamp conforms to IEEE 1588v2 PHY: first symbol on the wire IIRC. But in many cases this is not the case. It is not feasible at line rate, or the timestamp is only taken when the completion is written over PCI, which may be subject to PCI backpressure and happen after transmission on the wire. As a result, the worst case hw tstamp must already be assumed not much earlier than a completion timestamp.

Quoted 4 lines

For BT ISO packets, in theory hw-provided TX timestamps exist, and we might want both (with separate flags for enabling them). I don't really know, last I looked Intel HW didn't support them, and it's not clear to which degree they are useful.

That's reason enough to separate these measurement types.

If we don't do it properly now, we won't be able to update drivers later once users depend on requesting hw timestamps when they mean to get completion timestamps.

Quoted 47 lines
Quoted 34 lines

That said, +1 on adding explicit well defined measurement point instead.

Quoted 17 lines
---
 Documentation/networking/timestamping.rst | 9 +++++++++
 include/linux/skbuff.h                    | 6 +++++-
 include/uapi/linux/errqueue.h             | 1 +
 include/uapi/linux/net_tstamp.h           | 6 ++++--
 net/ethtool/common.c                      | 1 +
 net/socket.c                              | 3 +++
 6 files changed, 23 insertions(+), 3 deletions(-)
Documentation/networking/timestamping.rst +0 -0
index 61ef9da10e28..de2afed7a516 100644
@@ -140,6 +140,15 @@ SOF_TIMESTAMPING_TX_ACK:
   cumulative acknowledgment. The mechanism ignores SACK and FACK.
   This flag can be enabled via both socket options and control messages.

Documentation/networking/timestamping.rst SOF_TIMESTAMPING_TX_ACK: · line 151

Quoted 10 lines
Documentation/networking/timestamping.rst +9 -0
 
+SOF_TIMESTAMPING_TX_COMPLETION:
+  Request tx timestamps on packet tx completion.  The completion
+  timestamp is generated by the kernel when it receives packet a
+  completion report from the hardware. Hardware may report multiple
+  packets at once, and completion timestamps reflect the timing of the
+  report and not actual tx time. The completion timestamps are
+  currently implemented only for: Bluetooth L2CAP and ISO.  This
+  flag can be enabled via both socket options and control messages.
+

Either we should support this uniformly, or it should be possible to query whether a driver supports this.

Quoted 6 lines

Unfortunately all completion callbacks are driver specific.

But drivers that support hwtstamps will call skb_tstamp_tx with nonzero hwtstamps. We could use that also to compute and queue a completion timestamp if requested. At least for existing NIC drivers.

Ok. If possible, I'd like to avoid changing the behavior of the non- Bluetooth parts of net/ here, as I'm not familiar with those.

I guess a simpler solution could be that sock_set_timestamping() checks the type of the socket, and gives EINVAL if the flag is set for non- Bluetooth sockets?

Quoted 8 lines
Quoted 4 lines

Actually, I'd prefer to have this completion timestamp ability for all drivers. And avoid creating subsystem private mechanisms.

I suppose we can punt on the get_ts_info control API if need be.

I guess that it is reasonable if we don't have to do the work for drivers other than Bluetooth otherwise I'd say you are probably asking too much here,

I was mainly agreeing to Pauli's implementation in this series. Not asking to do any work irrelevant to BT.

Quoted 4 lines

also doesn't this land on the TSN space if one needs to tightly control timings? I suspect if this sort of change was not necessary for TSN then perhaps it wouldn't be of much value to try to generalize this.

I don't fully follow. But in general SO_TIMESTAMPING is not limited to TSN.

Quoted 110 lines
Quoted 106 lines
Quoted 29 lines

One could then postpone having to invent how to check the driver support, and user would know non-supported status from setsockopt failing.

Quoted 15 lines
Quoted 15 lines
Documentation/networking/timestamping.rst +0 -0
 1.3.2 Timestamp Reporting
 ^^^^^^^^^^^^^^^^^^^^^^^^^
include/linux/skbuff.h +3 -0
index bb2b751d274a..3707c9075ae9 100644
@@ -489,10 +489,14 @@ enum {
 
  /* generate software time stamp when entering packet scheduling */
  SKBTX_SCHED_TSTAMP = 1 << 6,
+
+ /* generate software time stamp on packet tx completion */
+ SKBTX_COMPLETION_TSTAMP = 1 << 7,
 };
 
Quoted 8 lines

include/linux/skbuff.h enum { · line 499

Quoted 4 lines
include/linux/skbuff.h +2 -1
 #define SKBTX_ANY_SW_TSTAMP      (SKBTX_SW_TSTAMP    | \
-                          SKBTX_SCHED_TSTAMP)
+                          SKBTX_SCHED_TSTAMP | \
+                          SKBTX_COMPLETION_TSTAMP)

These fields are used in the skb_shared_info tx_flags field. Which is a very scarce resource. This takes the last available bit. That is my only possible concern: the opportunity cost.

Quoted 5 lines

If doing it per-protocol sounds ok, it could be put in bt_skb_cb instead.

Since the completion timestamp didn't already exist, it maybe means it's probably not that important for other parts of net/

I can see its value especially for hardware that does not support hardware timestamps, or hw timestamps at line rate.

This gives a reasonable estimation of transmission time and measure of device delay.

It is device specific whether it will be an over- or under-estimation, depending on whether the completion is queued to the host after or before the data is written on the wire. But either way, it will include the delay in processing the tx queue, which on multi-queue NICs and with TSO may be substantial (even before considering HW rate limiting).

Quoted 56 lines
Quoted 38 lines
Quoted 38 lines
include/linux/skbuff.h +0 -0
 #define SKBTX_ANY_TSTAMP (SKBTX_HW_TSTAMP | \
                           SKBTX_HW_TSTAMP_USE_CYCLES | \
                           SKBTX_ANY_SW_TSTAMP)
include/uapi/linux/errqueue.h +1 -0
index 3c70e8ac14b8..1ea47309d772 100644
@@ -73,6 +73,7 @@ enum {
  SCM_TSTAMP_SND,         /* driver passed skb to NIC, or HW */
  SCM_TSTAMP_SCHED,       /* data entered the packet scheduler */
  SCM_TSTAMP_ACK,         /* data acknowledged by peer */
+ SCM_TSTAMP_COMPLETION,  /* packet tx completion */
 };
 
 #endif /* _UAPI_LINUX_ERRQUEUE_H */
include/uapi/linux/net_tstamp.h +4 -2
index 55b0ab51096c..383213de612a 100644
@@ -44,8 +44,9 @@ enum {
  SOF_TIMESTAMPING_BIND_PHC = (1 << 15),
  SOF_TIMESTAMPING_OPT_ID_TCP = (1 << 16),
  SOF_TIMESTAMPING_OPT_RX_FILTER = (1 << 17),
+ SOF_TIMESTAMPING_TX_COMPLETION = (1 << 18),
 
- SOF_TIMESTAMPING_LAST = SOF_TIMESTAMPING_OPT_RX_FILTER,
+ SOF_TIMESTAMPING_LAST = SOF_TIMESTAMPING_TX_COMPLETION,
  SOF_TIMESTAMPING_MASK = (SOF_TIMESTAMPING_LAST - 1) |
                           SOF_TIMESTAMPING_LAST
 };
@@ -58,7 +59,8 @@ enum {
 #define SOF_TIMESTAMPING_TX_RECORD_MASK  (SOF_TIMESTAMPING_TX_HARDWARE | \
                                   SOF_TIMESTAMPING_TX_SOFTWARE | \
                                   SOF_TIMESTAMPING_TX_SCHED | \
-                                  SOF_TIMESTAMPING_TX_ACK)
+                                  SOF_TIMESTAMPING_TX_ACK | \
+                                  SOF_TIMESTAMPING_TX_COMPLETION)
 
Quoted 13 lines

net/ethtool/common.c const char sof_timestamping_names[][ETH_GSTRING_LEN] = { · line 434

Quoted 11 lines
include/uapi/linux/net_tstamp.h +0 -0
 /**
  * struct so_timestamping - SO_TIMESTAMPING parameter
net/ethtool/common.c +1 -0
index 2bd77c94f9f1..75e3b756012e 100644
@@ -431,6 +431,7 @@ const char sof_timestamping_names[][ETH_GSTRING_LEN] = {
  [const_ilog2(SOF_TIMESTAMPING_BIND_PHC)]     = "bind-phc",
  [const_ilog2(SOF_TIMESTAMPING_OPT_ID_TCP)]   = "option-id-tcp",
  [const_ilog2(SOF_TIMESTAMPING_OPT_RX_FILTER)] = "option-rx-filter",
+ [const_ilog2(SOF_TIMESTAMPING_TX_COMPLETION)] = "completion-transmit",

just "tx-completion"?

Ok.

--
Pauli Virtanen

Signature · 1 line
Luiz Augusto von Dentz
#8 lore raw

Re: [PATCH v3 1/5] net-timestamp: COMPLETION timestamp on packet tx completion

In reply to Willem de Bruijn

Hi,

su, 2025-02-09 kello 22:29 -0500, Willem de Bruijn kirjoitti:

Quoted 17 lines
Quoted 15 lines · Pauli Virtanen

1.3.2 Timestamp Reporting
^^^^^^^^^^^^^^^^^^^^^^^^^

include/linux/skbuff.h +3 -0
index bb2b751d274a..3707c9075ae9 100644
@@ -489,10 +489,14 @@ enum {
 
 	/* generate software time stamp when entering packet scheduling */
 	SKBTX_SCHED_TSTAMP = 1 << 6,
+
+	/* generate software time stamp on packet tx completion */
+	SKBTX_COMPLETION_TSTAMP = 1 << 7,
 };
 
Quoted 8 lines

include/linux/skbuff.h enum { · line 499

Quoted 4 lines
include/linux/skbuff.h +2 -1
 #define SKBTX_ANY_SW_TSTAMP	(SKBTX_SW_TSTAMP    | \
-				 SKBTX_SCHED_TSTAMP)
+				 SKBTX_SCHED_TSTAMP | \
+				 SKBTX_COMPLETION_TSTAMP)

These fields are used in the skb_shared_info tx_flags field. Which is a very scarce resource. This takes the last available bit. That is my only possible concern: the opportunity cost.

One alternative here could be:

Make SOF_TIMESTAMPING_TX_COMPLETION available only as a socket option, and make it emit COMPLETION tstamp exactly for those packets that also have SOF_TIMESTAMPING_TX_SOFTWARE enabled. User apps can then still timestamp only selected packets via CMSG, however the choice between SND and SND+COMPLETION is socket-wide. The API is then less uniform, but probably usable in practice, although some use cases may not be interested in the extra SND tstamps.

IIUC, sizeof skb_shared_info cannot be easily changed and I'm not sure if there is room left there. So if the option of putting it into protocol-specific skb cb is also out, then I'm not sure what the plan here should be.

Signature · 1 line
Pauli Virtanen
#9 lore raw

Re: [PATCH v3 1/5] net-timestamp: COMPLETION timestamp on packet tx completion

In reply to Pauli Virtanen

Hi Pauli,

Quoted 6 lines · Pauli Virtanen

Add SOF_TIMESTAMPING_TX_COMPLETION, for requesting a software timestamp when hardware reports a packet completed.

Completion tstamp is useful for Bluetooth, where hardware tx timestamps cannot be obtained except for ISO packets, and the hardware has a queue

Could you say more about why the hw timestamp cannot be obtained?

Quoted 8 lines

where packets may wait. In this case the software SND timestamp only reflects the kernel-side part of the total latency (usually small) and queue length (usually 0 unless HW buffers congested), whereas the completion report time is more informative of the true latency.

It may also be useful in other cases where HW TX timestamps cannot be obtained and user wants to estimate an upper bound to when the TX probably happened.

It's worth mentioning the earlier discussion that took place last year in the commit log. It's hard to retrieve such an important discussion buried in the mailing list, which should be helpful for people to get to know/recall the exact background :)

https://lore.kernel.org/all/[email protected]/.
https://lore.kernel.org/all/[email protected]/

And it's also good to attach the real use case from the following link for readers to know the exact case :)

https://lore.kernel.org/all/[email protected]/
Quoting here:
"
sendmsg() from user generates skbs to net/bluetooth side queue |
* wait in net/bluetooth side queue until HW has free packet slot |
* send to driver (-> SCM_TSTAMP_SCHED*)
|
* driver (usu. ASAP) queues to transport e.g. USB
|
* transport tx complete, skb freed
|
* packet waits in hardware-side buffers (usu. the largest delay) |
* packet completion report from HW (-> SCM_TSTAMP_SND*)
|
* for one packet type, HW timestamp for last tx packet can queried The packet completion report does not imply the packet was received. "

Quoted 17 lines
---
 Documentation/networking/timestamping.rst | 9 +++++++++
 include/linux/skbuff.h                    | 6 +++++-
 include/uapi/linux/errqueue.h             | 1 +
 include/uapi/linux/net_tstamp.h           | 6 ++++--
 net/ethtool/common.c                      | 1 +
 net/socket.c                              | 3 +++
 6 files changed, 23 insertions(+), 3 deletions(-)
Documentation/networking/timestamping.rst +0 -0
index 61ef9da10e28..de2afed7a516 100644
@@ -140,6 +140,15 @@ SOF_TIMESTAMPING_TX_ACK:
   cumulative acknowledgment. The mechanism ignores SACK and FACK.
   This flag can be enabled via both socket options and control messages.

Documentation/networking/timestamping.rst SOF_TIMESTAMPING_TX_ACK: · line 151

Quoted 10 lines
Documentation/networking/timestamping.rst +9 -0
 
+SOF_TIMESTAMPING_TX_COMPLETION:
+  Request tx timestamps on packet tx completion.  The completion
+  timestamp is generated by the kernel when it receives packet a
+  completion report from the hardware. Hardware may report multiple
+  packets at once, and completion timestamps reflect the timing of the
+  report and not actual tx time. The completion timestamps are
+  currently implemented only for: Bluetooth L2CAP and ISO.  This
+  flag can be enabled via both socket options and control messages.
+

I'd like to know if this flag can also be applied to NICs which have already implemented the hardware timestamp, like Intel i40e, no?

Thanks,
Jason

Quoted 90 lines
Documentation/networking/timestamping.rst +0 -0
 1.3.2 Timestamp Reporting
 ^^^^^^^^^^^^^^^^^^^^^^^^^
include/linux/skbuff.h +5 -1
index bb2b751d274a..3707c9075ae9 100644
@@ -489,10 +489,14 @@ enum {
 
        /* generate software time stamp when entering packet scheduling */
        SKBTX_SCHED_TSTAMP = 1 << 6,
+
+       /* generate software time stamp on packet tx completion */
+       SKBTX_COMPLETION_TSTAMP = 1 << 7,
 };
 
 #define SKBTX_ANY_SW_TSTAMP    (SKBTX_SW_TSTAMP    | \
-                                SKBTX_SCHED_TSTAMP)
+                                SKBTX_SCHED_TSTAMP | \
+                                SKBTX_COMPLETION_TSTAMP)
 #define SKBTX_ANY_TSTAMP       (SKBTX_HW_TSTAMP | \
                                 SKBTX_HW_TSTAMP_USE_CYCLES | \
                                 SKBTX_ANY_SW_TSTAMP)
include/uapi/linux/errqueue.h +1 -0
index 3c70e8ac14b8..1ea47309d772 100644
@@ -73,6 +73,7 @@ enum {
        SCM_TSTAMP_SND,         /* driver passed skb to NIC, or HW */
        SCM_TSTAMP_SCHED,       /* data entered the packet scheduler */
        SCM_TSTAMP_ACK,         /* data acknowledged by peer */
+       SCM_TSTAMP_COMPLETION,  /* packet tx completion */
 };
 
 #endif /* _UAPI_LINUX_ERRQUEUE_H */
include/uapi/linux/net_tstamp.h +4 -2
index 55b0ab51096c..383213de612a 100644
@@ -44,8 +44,9 @@ enum {
        SOF_TIMESTAMPING_BIND_PHC = (1 << 15),
        SOF_TIMESTAMPING_OPT_ID_TCP = (1 << 16),
        SOF_TIMESTAMPING_OPT_RX_FILTER = (1 << 17),
+       SOF_TIMESTAMPING_TX_COMPLETION = (1 << 18),
 
-       SOF_TIMESTAMPING_LAST = SOF_TIMESTAMPING_OPT_RX_FILTER,
+       SOF_TIMESTAMPING_LAST = SOF_TIMESTAMPING_TX_COMPLETION,
        SOF_TIMESTAMPING_MASK = (SOF_TIMESTAMPING_LAST - 1) |
                                 SOF_TIMESTAMPING_LAST
 };
@@ -58,7 +59,8 @@ enum {
 #define SOF_TIMESTAMPING_TX_RECORD_MASK        (SOF_TIMESTAMPING_TX_HARDWARE | \
                                         SOF_TIMESTAMPING_TX_SOFTWARE | \
                                         SOF_TIMESTAMPING_TX_SCHED | \
-                                        SOF_TIMESTAMPING_TX_ACK)
+                                        SOF_TIMESTAMPING_TX_ACK | \
+                                        SOF_TIMESTAMPING_TX_COMPLETION)
 
 /**
  * struct so_timestamping - SO_TIMESTAMPING parameter
net/ethtool/common.c +1 -0
index 2bd77c94f9f1..75e3b756012e 100644
@@ -431,6 +431,7 @@ const char sof_timestamping_names[][ETH_GSTRING_LEN] = {
        [const_ilog2(SOF_TIMESTAMPING_BIND_PHC)]     = "bind-phc",
        [const_ilog2(SOF_TIMESTAMPING_OPT_ID_TCP)]   = "option-id-tcp",
        [const_ilog2(SOF_TIMESTAMPING_OPT_RX_FILTER)] = "option-rx-filter",
+       [const_ilog2(SOF_TIMESTAMPING_TX_COMPLETION)] = "completion-transmit",
 };
 static_assert(ARRAY_SIZE(sof_timestamping_names) == __SOF_TIMESTAMPING_CNT);
 
net/socket.c +3 -1
index 4afe31656a2b..22b7f6f50889 100644
@@ -693,6 +693,9 @@ void __sock_tx_timestamp(__u32 tsflags, __u8 *tx_flags)
        if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
                flags |= SKBTX_SCHED_TSTAMP;
 
+       if (tsflags & SOF_TIMESTAMPING_TX_COMPLETION)
+               flags |= SKBTX_COMPLETION_TSTAMP;
+
        *tx_flags = flags;
 }
 EXPORT_SYMBOL(__sock_tx_timestamp);
--

2.48.1

#10 lore raw

PATCH v3 2/5 Bluetooth: add support for skb TX SND/COMPLETION timestamping

In reply to Pauli Virtanen

Support enabling TX timestamping for some skbs, and track them until packet completion. Generate software SCM_TSTAMP_COMPLETION when getting completion report from hardware.

Generate software SCM_TSTAMP_SND before sending to driver. Sending from driver requires changes in the driver API, and drivers mostly are going to send the skb immediately.

Make the default situation with no COMPLETION TX timestamping more efficient by only counting packets in the queue when there is nothing to track. When there is something to track, we need to make clones, since the driver may modify sent skbs.

The tx_q queue length is bounded by the hdev flow control, which will not send new packets before it has got completion reports for old ones.

---
 include/net/bluetooth/hci_core.h |  13 ++++
 net/bluetooth/hci_conn.c         | 117 +++++++++++++++++++++++++++++++
 net/bluetooth/hci_core.c         |  17 +++--
 net/bluetooth/hci_event.c        |   4 ++
 4 files changed, 146 insertions(+), 5 deletions(-)
include/net/bluetooth/hci_core.h +13 -0
index 05919848ea95..1f539a9881ad 100644
@@ -261,6 +261,12 @@ struct adv_info {
 	struct delayed_work	rpa_expired_cb;
 };
 
+struct tx_queue {
+	struct sk_buff_head queue;
+	unsigned int extra;
+	unsigned int tracked;
+};
+
 #define HCI_MAX_ADV_INSTANCES		5
 #define HCI_DEFAULT_ADV_DURATION	2
 
@@ -733,6 +739,8 @@ struct hci_conn {
 	struct sk_buff_head data_q;
 	struct list_head chan_list;
 
+	struct tx_queue tx_q;
+
 	struct delayed_work disc_work;
 	struct delayed_work auto_accept_work;
 	struct delayed_work idle_work;
@@ -1571,6 +1579,11 @@ void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active);
 void hci_conn_failed(struct hci_conn *conn, u8 status);
 u8 hci_conn_set_handle(struct hci_conn *conn, u16 handle);
 
+void hci_conn_tx_queue(struct hci_conn *conn, struct sk_buff *skb);
+void hci_conn_tx_dequeue(struct hci_conn *conn);
+void hci_setup_tx_timestamp(struct sk_buff *skb, size_t key_offset,
+			    const struct sockcm_cookie *sockc);
+
 /*
  * hci_conn_get() and hci_conn_put() are used to control the life-time of an
  * "hci_conn" object. They do not guarantee that the hci_conn object is running,
net/bluetooth/hci_conn.c +117 -0

138 lines not on this page · raw message

net/bluetooth/hci_core.c +12 -5
index e7ec12437c8b..e0845188f626 100644
@@ -3025,6 +3025,13 @@ static int hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
 	return 0;
 }
 
+static int hci_send_conn_frame(struct hci_dev *hdev, struct hci_conn *conn,
+			       struct sk_buff *skb)
+{
+	hci_conn_tx_queue(conn, skb);
+	return hci_send_frame(hdev, skb);
+}
+
 /* Send HCI command */
 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
 		 const void *param)
@@ -3562,7 +3569,7 @@ static void hci_sched_sco(struct hci_dev *hdev)
 	while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
 		while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
 			BT_DBG("skb %p len %d", skb, skb->len);
-			hci_send_frame(hdev, skb);
+			hci_send_conn_frame(hdev, conn, skb);
 
 			conn->sent++;
 			if (conn->sent == ~0)
@@ -3586,7 +3593,7 @@ static void hci_sched_esco(struct hci_dev *hdev)
 						     &quote))) {
 		while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
 			BT_DBG("skb %p len %d", skb, skb->len);
-			hci_send_frame(hdev, skb);
+			hci_send_conn_frame(hdev, conn, skb);
 
 			conn->sent++;
 			if (conn->sent == ~0)
@@ -3620,7 +3627,7 @@ static void hci_sched_acl_pkt(struct hci_dev *hdev)
 			hci_conn_enter_active_mode(chan->conn,
 						   bt_cb(skb)->force_active);
 
-			hci_send_frame(hdev, skb);
+			hci_send_conn_frame(hdev, chan->conn, skb);
 			hdev->acl_last_tx = jiffies;
 
 			hdev->acl_cnt--;
@@ -3676,7 +3683,7 @@ static void hci_sched_le(struct hci_dev *hdev)
 
 			skb = skb_dequeue(&chan->data_q);
 
-			hci_send_frame(hdev, skb);
+			hci_send_conn_frame(hdev, chan->conn, skb);
 			hdev->le_last_tx = jiffies;
 
 			(*cnt)--;
@@ -3710,7 +3717,7 @@ static void hci_sched_iso(struct hci_dev *hdev)
 	while (*cnt && (conn = hci_low_sent(hdev, ISO_LINK, &quote))) {
 		while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
 			BT_DBG("skb %p len %d", skb, skb->len);
-			hci_send_frame(hdev, skb);
+			hci_send_conn_frame(hdev, conn, skb);
 
 			conn->sent++;
 			if (conn->sent == ~0)
net/bluetooth/hci_event.c +4 -0
index 2cc7a9306350..144b442180f7 100644
@@ -4405,6 +4405,7 @@ static void hci_num_comp_pkts_evt(struct hci_dev *hdev, void *data,
 		struct hci_comp_pkts_info *info = &ev->handles[i];
 		struct hci_conn *conn;
 		__u16  handle, count;
+		unsigned int i;
 
 		handle = __le16_to_cpu(info->handle);
 		count  = __le16_to_cpu(info->count);
@@ -4415,6 +4416,9 @@ static void hci_num_comp_pkts_evt(struct hci_dev *hdev, void *data,
 
 		conn->sent -= count;
 
+		for (i = 0; i < count; ++i)
+			hci_conn_tx_dequeue(conn);
+
 		switch (conn->type) {
 		case ACL_LINK:
 			hdev->acl_cnt += count;
Signature · 1 line
2.48.1
#11 lore raw

Re: [PATCH v3 2/5] Bluetooth: add support for skb TX SND/COMPLETION timestamping

Quoted 109 lines · Pauli Virtanen

Support enabling TX timestamping for some skbs, and track them until packet completion. Generate software SCM_TSTAMP_COMPLETION when getting completion report from hardware.

Generate software SCM_TSTAMP_SND before sending to driver. Sending from driver requires changes in the driver API, and drivers mostly are going to send the skb immediately.

Make the default situation with no COMPLETION TX timestamping more efficient by only counting packets in the queue when there is nothing to track. When there is something to track, we need to make clones, since the driver may modify sent skbs.

The tx_q queue length is bounded by the hdev flow control, which will not send new packets before it has got completion reports for old ones.

---
 include/net/bluetooth/hci_core.h |  13 ++++
 net/bluetooth/hci_conn.c         | 117 +++++++++++++++++++++++++++++++
 net/bluetooth/hci_core.c         |  17 +++--
 net/bluetooth/hci_event.c        |   4 ++
 4 files changed, 146 insertions(+), 5 deletions(-)
include/net/bluetooth/hci_core.h +13 -0
index 05919848ea95..1f539a9881ad 100644
@@ -261,6 +261,12 @@ struct adv_info {
        struct delayed_work     rpa_expired_cb;
 };
 
+struct tx_queue {
+       struct sk_buff_head queue;
+       unsigned int extra;
+       unsigned int tracked;
+};
+
 #define HCI_MAX_ADV_INSTANCES          5
 #define HCI_DEFAULT_ADV_DURATION       2
 
@@ -733,6 +739,8 @@ struct hci_conn {
        struct sk_buff_head data_q;
        struct list_head chan_list;
 
+       struct tx_queue tx_q;
+
        struct delayed_work disc_work;
        struct delayed_work auto_accept_work;
        struct delayed_work idle_work;
@@ -1571,6 +1579,11 @@ void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active);
 void hci_conn_failed(struct hci_conn *conn, u8 status);
 u8 hci_conn_set_handle(struct hci_conn *conn, u16 handle);
 
+void hci_conn_tx_queue(struct hci_conn *conn, struct sk_buff *skb);
+void hci_conn_tx_dequeue(struct hci_conn *conn);
+void hci_setup_tx_timestamp(struct sk_buff *skb, size_t key_offset,
+                           const struct sockcm_cookie *sockc);
+
 /*
  * hci_conn_get() and hci_conn_put() are used to control the life-time of an
  * "hci_conn" object. They do not guarantee that the hci_conn object is running,
net/bluetooth/hci_conn.c +17 -0
index d097e308a755..e437290d8b70 100644
@@ -27,6 +27,7 @@
 
 #include <linux/export.h>
 #include <linux/debugfs.h>
+#include <linux/errqueue.h>
 
 #include <net/bluetooth/bluetooth.h>
 #include <net/bluetooth/hci_core.h>
@@ -1002,6 +1003,7 @@ static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t
        }
 
        skb_queue_head_init(&conn->data_q);
+       skb_queue_head_init(&conn->tx_q.queue);
 
        INIT_LIST_HEAD(&conn->chan_list);
        INIT_LIST_HEAD(&conn->link_list);
@@ -1155,6 +1157,7 @@ void hci_conn_del(struct hci_conn *conn)
        }
 
        skb_queue_purge(&conn->data_q);
+       skb_queue_purge(&conn->tx_q.queue);
 
        /* Remove the connection from the list and cleanup its remaining
         * state. This is a separate function since for some cases like
@@ -3064,3 +3067,117 @@ int hci_abort_conn(struct hci_conn *conn, u8 reason)
         */
        return hci_cmd_sync_run_once(hdev, abort_conn_sync, conn, NULL);
 }
+
+void hci_setup_tx_timestamp(struct sk_buff *skb, size_t key_offset,
+                           const struct sockcm_cookie *sockc)
+{
+       struct sock *sk = skb ? skb->sk : NULL;
+
+       /* This shall be called on a single skb of those generated by user
+        * sendmsg(), and only when the sendmsg() does not return error to
+        * user. This is required for keeping the tskey that increments here in
+        * sync with possible sendmsg() counting by user.
+        *
+        * Stream sockets shall set key_offset to sendmsg() length in bytes
+        * and call with the last fragment, others to 1 and first fragment.
+        */

net/bluetooth/hci_conn.c int hci_abort_conn(struct hci_conn *conn, u8 reason) · line 3093

Quoted 10 lines
net/bluetooth/hci_conn.c +10 -0
+
+       if (!skb || !sockc || !sk || !key_offset)
+               return;
+
+       sock_tx_timestamp(sk, sockc, &skb_shinfo(skb)->tx_flags);
+
+       if (sockc->tsflags & SOF_TIMESTAMPING_OPT_ID &&
+           sockc->tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK) {
+               if (sockc->tsflags & SOCKCM_FLAG_TS_OPT_ID) {
+                       skb_shinfo(skb)->tskey = sockc->ts_opt_id;

Will applications take control of managing the tskey on their own instead of kernel in the future?

Quoted 5 lines
net/bluetooth/hci_conn.c +5 -0
+               } else {
+                       int key = atomic_add_return(key_offset, &sk->sk_tskey);
+
+                       skb_shinfo(skb)->tskey = key - 1;
+               }

net/bluetooth/hci_conn.c int hci_abort_conn(struct hci_conn *conn, u8 reason) · line 3108

Quoted 10 lines
net/bluetooth/hci_conn.c +10 -0
+       }
+}
+
+void hci_conn_tx_queue(struct hci_conn *conn, struct sk_buff *skb)
+{
+       struct tx_queue *comp = &conn->tx_q;
+       bool track = false;
+
+       /* Emit SND now, ie. just before sending to driver */
+       if (skb->sk && (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP))

Nit: I think it's not necessary to test if skb->sk is NULL here since __skb_tstamp_tx() will do the check.

Quoted 164 lines
net/bluetooth/hci_conn.c +75 -0
+               __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SND);
+
+       /* COMPLETION tstamp is emitted for tracked skb later in Number of
+        * Completed Packets event. Available only for flow controlled cases.
+        *
+        * TODO: SCO support (needs to be done in drivers)
+        */
+       switch (conn->type) {
+       case ISO_LINK:
+       case ACL_LINK:
+       case LE_LINK:
+               break;
+       default:
+               return;
+       }
+
+       if (skb->sk && (skb_shinfo(skb)->tx_flags & SKBTX_COMPLETION_TSTAMP))
+               track = true;
+
+       /* If nothing is tracked, just count extra skbs at the queue head */
+       if (!track && !comp->tracked) {
+               comp->extra++;
+               return;
+       }
+
+       if (track) {
+               skb = skb_clone_sk(skb);
+               if (!skb)
+                       goto count_only;
+
+               comp->tracked++;
+       } else {
+               skb = skb_clone(skb, GFP_KERNEL);
+               if (!skb)
+                       goto count_only;
+       }
+
+       skb_queue_tail(&comp->queue, skb);
+       return;
+
+count_only:
+       /* Stop tracking skbs, and only count. This will not emit timestamps for
+        * the packets, but if we get here something is more seriously wrong.
+        */
+       comp->tracked = 0;
+       comp->extra += skb_queue_len(&comp->queue) + 1;
+       skb_queue_purge(&comp->queue);
+}
+
+void hci_conn_tx_dequeue(struct hci_conn *conn)
+{
+       struct tx_queue *comp = &conn->tx_q;
+       struct sk_buff *skb;
+
+       /* If there are tracked skbs, the counted extra go before dequeuing real
+        * skbs, to keep ordering. When nothing is tracked, the ordering doesn't
+        * matter so dequeue real skbs first to get rid of them ASAP.
+        */
+       if (comp->extra && (comp->tracked || skb_queue_empty(&comp->queue))) {
+               comp->extra--;
+               return;
+       }
+
+       skb = skb_dequeue(&comp->queue);
+       if (!skb)
+               return;
+
+       if (skb->sk) {
+               comp->tracked--;
+               __skb_tstamp_tx(skb, NULL, NULL, skb->sk,
+                               SCM_TSTAMP_COMPLETION);
+       }
+
+       kfree_skb(skb);
+}
net/bluetooth/hci_core.c +12 -5
index e7ec12437c8b..e0845188f626 100644
@@ -3025,6 +3025,13 @@ static int hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
        return 0;
 }
 
+static int hci_send_conn_frame(struct hci_dev *hdev, struct hci_conn *conn,
+                              struct sk_buff *skb)
+{
+       hci_conn_tx_queue(conn, skb);
+       return hci_send_frame(hdev, skb);
+}
+
 /* Send HCI command */
 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
                 const void *param)
@@ -3562,7 +3569,7 @@ static void hci_sched_sco(struct hci_dev *hdev)
        while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
                while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
                        BT_DBG("skb %p len %d", skb, skb->len);
-                       hci_send_frame(hdev, skb);
+                       hci_send_conn_frame(hdev, conn, skb);
 
                        conn->sent++;
                        if (conn->sent == ~0)
@@ -3586,7 +3593,7 @@ static void hci_sched_esco(struct hci_dev *hdev)
                                                     &quote))) {
                while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
                        BT_DBG("skb %p len %d", skb, skb->len);
-                       hci_send_frame(hdev, skb);
+                       hci_send_conn_frame(hdev, conn, skb);
 
                        conn->sent++;
                        if (conn->sent == ~0)
@@ -3620,7 +3627,7 @@ static void hci_sched_acl_pkt(struct hci_dev *hdev)
                        hci_conn_enter_active_mode(chan->conn,
                                                   bt_cb(skb)->force_active);
 
-                       hci_send_frame(hdev, skb);
+                       hci_send_conn_frame(hdev, chan->conn, skb);
                        hdev->acl_last_tx = jiffies;
 
                        hdev->acl_cnt--;
@@ -3676,7 +3683,7 @@ static void hci_sched_le(struct hci_dev *hdev)
 
                        skb = skb_dequeue(&chan->data_q);
 
-                       hci_send_frame(hdev, skb);
+                       hci_send_conn_frame(hdev, chan->conn, skb);
                        hdev->le_last_tx = jiffies;
 
                        (*cnt)--;
@@ -3710,7 +3717,7 @@ static void hci_sched_iso(struct hci_dev *hdev)
        while (*cnt && (conn = hci_low_sent(hdev, ISO_LINK, &quote))) {
                while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
                        BT_DBG("skb %p len %d", skb, skb->len);
-                       hci_send_frame(hdev, skb);
+                       hci_send_conn_frame(hdev, conn, skb);
 
                        conn->sent++;
                        if (conn->sent == ~0)
net/bluetooth/hci_event.c +4 -1
index 2cc7a9306350..144b442180f7 100644
@@ -4405,6 +4405,7 @@ static void hci_num_comp_pkts_evt(struct hci_dev *hdev, void *data,
                struct hci_comp_pkts_info *info = &ev->handles[i];
                struct hci_conn *conn;
                __u16  handle, count;
+               unsigned int i;
 
                handle = __le16_to_cpu(info->handle);
                count  = __le16_to_cpu(info->count);
@@ -4415,6 +4416,9 @@ static void hci_num_comp_pkts_evt(struct hci_dev *hdev, void *data,
 
                conn->sent -= count;
 
+               for (i = 0; i < count; ++i)
+                       hci_conn_tx_dequeue(conn);
+
                switch (conn->type) {
                case ACL_LINK:
                        hdev->acl_cnt += count;
--

2.48.1

#12 lore raw

PATCH v3 3/5 Bluetooth: ISO: add TX timestamping

In reply to Pauli Virtanen

Add BT_SCM_ERROR socket CMSG type.

Support TX timestamping in ISO sockets.

Support MSG_ERRQUEUE in ISO recvmsg.

If a packet from sendmsg() is fragmented, only the first ACL fragment is timestamped.

---
 include/net/bluetooth/bluetooth.h |  1 +
 net/bluetooth/iso.c               | 24 ++++++++++++++++++++----
 2 files changed, 21 insertions(+), 4 deletions(-)
include/net/bluetooth/bluetooth.h +1 -0
index 435250c72d56..bbefde319f95 100644
@@ -156,6 +156,7 @@ struct bt_voice {
 #define BT_PKT_STATUS           16
 
 #define BT_SCM_PKT_STATUS	0x03
+#define BT_SCM_ERROR		0x04
 
 #define BT_ISO_QOS		17
 
net/bluetooth/iso.c +20 -4
index 44acddf58a0c..f497759a2af5 100644
@@ -518,7 +518,8 @@ static struct bt_iso_qos *iso_sock_get_qos(struct sock *sk)
 	return &iso_pi(sk)->qos;
 }
 
-static int iso_send_frame(struct sock *sk, struct sk_buff *skb)
+static int iso_send_frame(struct sock *sk, struct sk_buff *skb,
+			  const struct sockcm_cookie *sockc)
 {
 	struct iso_conn *conn = iso_pi(sk)->conn;
 	struct bt_iso_qos *qos = iso_sock_get_qos(sk);
@@ -538,10 +539,12 @@ static int iso_send_frame(struct sock *sk, struct sk_buff *skb)
 	hdr->slen = cpu_to_le16(hci_iso_data_len_pack(len,
 						      HCI_ISO_STATUS_VALID));
 
-	if (sk->sk_state == BT_CONNECTED)
+	if (sk->sk_state == BT_CONNECTED) {
+		hci_setup_tx_timestamp(skb, 1, sockc);
 		hci_send_iso(conn->hcon, skb);
-	else
+	} else {
 		len = -ENOTCONN;
+	}
 
 	return len;
 }
@@ -1348,6 +1351,7 @@ static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
 {
 	struct sock *sk = sock->sk;
 	struct sk_buff *skb, **frag;
+	struct sockcm_cookie sockc;
 	size_t mtu;
 	int err;
 
@@ -1360,6 +1364,14 @@ static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
 	if (msg->msg_flags & MSG_OOB)
 		return -EOPNOTSUPP;
 
+	sockcm_init(&sockc, sk);
+
+	if (msg->msg_controllen) {
+		err = sock_cmsg_send(sk, msg, &sockc);
+		if (err)
+			return err;
+	}
+
 	lock_sock(sk);
 
 	if (sk->sk_state != BT_CONNECTED) {
@@ -1405,7 +1417,7 @@ static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
 	lock_sock(sk);
 
 	if (sk->sk_state == BT_CONNECTED)
-		err = iso_send_frame(sk, skb);
+		err = iso_send_frame(sk, skb, &sockc);
 	else
 		err = -ENOTCONN;
 
@@ -1474,6 +1486,10 @@ static int iso_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 
 	BT_DBG("sk %p", sk);
 
+	if (unlikely(flags & MSG_ERRQUEUE))
+		return sock_recv_errqueue(sk, msg, len, SOL_BLUETOOTH,
+					  BT_SCM_ERROR);
+
 	if (test_and_clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
 		sock_hold(sk);
 		lock_sock(sk);
Signature · 1 line
2.48.1
#13 lore raw

Re: [PATCH v3 3/5] Bluetooth: ISO: add TX timestamping

Quoted 66 lines · Pauli Virtanen

Add BT_SCM_ERROR socket CMSG type.

Support TX timestamping in ISO sockets.

Support MSG_ERRQUEUE in ISO recvmsg.

If a packet from sendmsg() is fragmented, only the first ACL fragment is timestamped.

---
 include/net/bluetooth/bluetooth.h |  1 +
 net/bluetooth/iso.c               | 24 ++++++++++++++++++++----
 2 files changed, 21 insertions(+), 4 deletions(-)
include/net/bluetooth/bluetooth.h +1 -0
index 435250c72d56..bbefde319f95 100644
@@ -156,6 +156,7 @@ struct bt_voice {
 #define BT_PKT_STATUS           16
 
 #define BT_SCM_PKT_STATUS      0x03
+#define BT_SCM_ERROR           0x04
 
 #define BT_ISO_QOS             17
 
net/bluetooth/iso.c +7 -3
index 44acddf58a0c..f497759a2af5 100644
@@ -518,7 +518,8 @@ static struct bt_iso_qos *iso_sock_get_qos(struct sock *sk)
        return &iso_pi(sk)->qos;
 }
 
-static int iso_send_frame(struct sock *sk, struct sk_buff *skb)
+static int iso_send_frame(struct sock *sk, struct sk_buff *skb,
+                         const struct sockcm_cookie *sockc)
 {
        struct iso_conn *conn = iso_pi(sk)->conn;
        struct bt_iso_qos *qos = iso_sock_get_qos(sk);
@@ -538,10 +539,12 @@ static int iso_send_frame(struct sock *sk, struct sk_buff *skb)
        hdr->slen = cpu_to_le16(hci_iso_data_len_pack(len,
                                                      HCI_ISO_STATUS_VALID));
 
-       if (sk->sk_state == BT_CONNECTED)
+       if (sk->sk_state == BT_CONNECTED) {
+               hci_setup_tx_timestamp(skb, 1, sockc);
                hci_send_iso(conn->hcon, skb);
-       else
+       } else {
                len = -ENOTCONN;
+       }
 
        return len;
 }
@@ -1348,6 +1351,7 @@ static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
 {
        struct sock *sk = sock->sk;
        struct sk_buff *skb, **frag;
+       struct sockcm_cookie sockc;
        size_t mtu;
        int err;
 

net/bluetooth/iso.c static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg, · line 1367

Quoted 4 lines
net/bluetooth/iso.c +1 -0
@@ -1360,6 +1364,14 @@ static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
        if (msg->msg_flags & MSG_OOB)
                return -EOPNOTSUPP;
 
+       sockcm_init(&sockc, sk);

No need to initialize other irrelevant fields since Willem started to clean up this kind of init phase in TCP[1].

[1]: https://lore.kernel.org/all/[email protected]/

net/bluetooth/iso.c static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg, · line 1374

Quoted 7 lines
net/bluetooth/iso.c +7 -0
+
+       if (msg->msg_controllen) {
+               err = sock_cmsg_send(sk, msg, &sockc);
+               if (err)
+                       return err;
+       }
+

I'm not familiar with bluetooth, but I'm wondering if the above code snippet is supposed to be protected by the socket lock as below since I refer to TCP as an example? Is it possible that multiple threads call this sendmsg at the same time?

Quoted 27 lines
net/bluetooth/iso.c +5 -2
        lock_sock(sk);
 
        if (sk->sk_state != BT_CONNECTED) {
@@ -1405,7 +1417,7 @@ static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
        lock_sock(sk);
 
        if (sk->sk_state == BT_CONNECTED)
-               err = iso_send_frame(sk, skb);
+               err = iso_send_frame(sk, skb, &sockc);
        else
                err = -ENOTCONN;
 
@@ -1474,6 +1486,10 @@ static int iso_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 
        BT_DBG("sk %p", sk);
 
+       if (unlikely(flags & MSG_ERRQUEUE))
+               return sock_recv_errqueue(sk, msg, len, SOL_BLUETOOTH,
+                                         BT_SCM_ERROR);
+
        if (test_and_clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
                sock_hold(sk);
                lock_sock(sk);
--

2.48.1

#14 lore raw

Re: [PATCH v3 3/5] Bluetooth: ISO: add TX timestamping

Hi,

ma, 2025-02-10 kello 13:19 +0800, Jason Xing kirjoitti:

Quoted 70 lines · Pauli Virtanen
Quoted 69 lines

Add BT_SCM_ERROR socket CMSG type.

Support TX timestamping in ISO sockets.

Support MSG_ERRQUEUE in ISO recvmsg.

If a packet from sendmsg() is fragmented, only the first ACL fragment is timestamped.

---
 include/net/bluetooth/bluetooth.h |  1 +
 net/bluetooth/iso.c               | 24 ++++++++++++++++++++----
 2 files changed, 21 insertions(+), 4 deletions(-)
include/net/bluetooth/bluetooth.h +1 -0
index 435250c72d56..bbefde319f95 100644
@@ -156,6 +156,7 @@ struct bt_voice {
 #define BT_PKT_STATUS           16
 
 #define BT_SCM_PKT_STATUS      0x03
+#define BT_SCM_ERROR           0x04
 
 #define BT_ISO_QOS             17
 
net/bluetooth/iso.c +7 -3
index 44acddf58a0c..f497759a2af5 100644
@@ -518,7 +518,8 @@ static struct bt_iso_qos *iso_sock_get_qos(struct sock *sk)
        return &iso_pi(sk)->qos;
 }
 
-static int iso_send_frame(struct sock *sk, struct sk_buff *skb)
+static int iso_send_frame(struct sock *sk, struct sk_buff *skb,
+                         const struct sockcm_cookie *sockc)
 {
        struct iso_conn *conn = iso_pi(sk)->conn;
        struct bt_iso_qos *qos = iso_sock_get_qos(sk);
@@ -538,10 +539,12 @@ static int iso_send_frame(struct sock *sk, struct sk_buff *skb)
        hdr->slen = cpu_to_le16(hci_iso_data_len_pack(len,
                                                      HCI_ISO_STATUS_VALID));
 
-       if (sk->sk_state == BT_CONNECTED)
+       if (sk->sk_state == BT_CONNECTED) {
+               hci_setup_tx_timestamp(skb, 1, sockc);
                hci_send_iso(conn->hcon, skb);
-       else
+       } else {
                len = -ENOTCONN;
+       }
 
        return len;
 }
@@ -1348,6 +1351,7 @@ static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
 {
        struct sock *sk = sock->sk;
        struct sk_buff *skb, **frag;
+       struct sockcm_cookie sockc;
        size_t mtu;
        int err;
 
@@ -1360,6 +1364,14 @@ static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
        if (msg->msg_flags & MSG_OOB)
                return -EOPNOTSUPP;
 
Quoted 6 lines

net/bluetooth/iso.c static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg, · line 1367

Quoted 1 line
net/bluetooth/iso.c +1 -0
+       sockcm_init(&sockc, sk);

No need to initialize other irrelevant fields since Willem started to clean up this kind of init phase in TCP[1].

[1]: https://lore.kernel.org/all/[email protected]/

Ok.

Quoted 4 lines
Quoted 4 lines
net/bluetooth/iso.c +4 -0
+
+       if (msg->msg_controllen) {
+               err = sock_cmsg_send(sk, msg, &sockc);
+               if (err)
Quoted 8 lines

net/bluetooth/iso.c static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg, · line 1374

Quoted 3 lines
net/bluetooth/iso.c +3 -0
+                       return err;
+       }
+

I'm not familiar with bluetooth, but I'm wondering if the above code snippet is supposed to be protected by the socket lock as below since I refer to TCP as an example? Is it possible that multiple threads call this sendmsg at the same time?

I think parallel sendmsg() is possible, but I understood sock_cmsg_send itself doesn't need lock_sock(), if I read correctly e.g. udp_sendmsg() seems to be using it via ip_cmsg_send() unlocked.

The lock_sock() that are below IIRC protect the invariant that sk->sk_state == BT_CONNECTED has special meaning here.

Quoted 29 lines
Quoted 29 lines
net/bluetooth/iso.c +5 -2
        lock_sock(sk);
 
        if (sk->sk_state != BT_CONNECTED) {
@@ -1405,7 +1417,7 @@ static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
        lock_sock(sk);
 
        if (sk->sk_state == BT_CONNECTED)
-               err = iso_send_frame(sk, skb);
+               err = iso_send_frame(sk, skb, &sockc);
        else
                err = -ENOTCONN;
 
@@ -1474,6 +1486,10 @@ static int iso_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 
        BT_DBG("sk %p", sk);
 
+       if (unlikely(flags & MSG_ERRQUEUE))
+               return sock_recv_errqueue(sk, msg, len, SOL_BLUETOOTH,
+                                         BT_SCM_ERROR);
+
        if (test_and_clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
                sock_hold(sk);
                lock_sock(sk);
--

2.48.1

#15 lore raw

PATCH v3 4/5 Bluetooth: L2CAP: add TX timestamping

In reply to Pauli Virtanen

Support TX timestamping in L2CAP sockets.

Support MSG_ERRQUEUE recvmsg.

For other than SOCK_STREAM L2CAP sockets, if a packet from sendmsg() is fragmented, only the first ACL fragment is timestamped.

For SOCK_STREAM L2CAP sockets, use the bytestream convention and timestamp the last fragment and count bytes in tskey.

Timestamps are not generated in the Enhanced Retransmission mode, as meaning of COMPLETION stamp is unclear if L2CAP layer retransmits.

---
 include/net/bluetooth/l2cap.h |  3 ++-
 net/bluetooth/6lowpan.c       |  2 +-
 net/bluetooth/l2cap_core.c    | 41 ++++++++++++++++++++++++++++++++---
 net/bluetooth/l2cap_sock.c    | 15 ++++++++++++-
 net/bluetooth/smp.c           |  2 +-
 5 files changed, 56 insertions(+), 7 deletions(-)
include/net/bluetooth/l2cap.h +2 -1
index 9189354c568f..00e182a22720 100644
@@ -955,7 +955,8 @@ void l2cap_chan_close(struct l2cap_chan *chan, int reason);
 int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
 		       bdaddr_t *dst, u8 dst_type, u16 timeout);
 int l2cap_chan_reconfigure(struct l2cap_chan *chan, __u16 mtu);
-int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len);
+int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len,
+		    const struct sockcm_cookie *sockc);
 void l2cap_chan_busy(struct l2cap_chan *chan, int busy);
 void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail);
 int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator);
net/bluetooth/6lowpan.c +1 -1
index 50cfec8ccac4..0eb1ecb54105 100644
@@ -443,7 +443,7 @@ static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
 	memset(&msg, 0, sizeof(msg));
 	iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &iv, 1, skb->len);
 
-	err = l2cap_chan_send(chan, &msg, skb->len);
+	err = l2cap_chan_send(chan, &msg, skb->len, NULL);
 	if (err > 0) {
 		netdev->stats.tx_bytes += err;
 		netdev->stats.tx_packets++;
net/bluetooth/l2cap_core.c +38 -3

73 lines not on this page · raw message

net/bluetooth/l2cap_sock.c +14 -1
index acd11b268b98..92305530e2e5 100644
@@ -1106,6 +1106,7 @@ static int l2cap_sock_sendmsg(struct socket *sock, struct msghdr *msg,
 {
 	struct sock *sk = sock->sk;
 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
+	struct sockcm_cookie sockc;
 	int err;
 
 	BT_DBG("sock %p, sk %p", sock, sk);
@@ -1120,6 +1121,14 @@ static int l2cap_sock_sendmsg(struct socket *sock, struct msghdr *msg,
 	if (sk->sk_state != BT_CONNECTED)
 		return -ENOTCONN;
 
+	sockcm_init(&sockc, sk);
+
+	if (msg->msg_controllen) {
+		err = sock_cmsg_send(sk, msg, &sockc);
+		if (err)
+			return err;
+	}
+
 	lock_sock(sk);
 	err = bt_sock_wait_ready(sk, msg->msg_flags);
 	release_sock(sk);
@@ -1127,7 +1136,7 @@ static int l2cap_sock_sendmsg(struct socket *sock, struct msghdr *msg,
 		return err;
 
 	l2cap_chan_lock(chan);
-	err = l2cap_chan_send(chan, msg, len);
+	err = l2cap_chan_send(chan, msg, len, &sockc);
 	l2cap_chan_unlock(chan);
 
 	return err;
@@ -1168,6 +1177,10 @@ static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 	struct l2cap_pinfo *pi = l2cap_pi(sk);
 	int err;
 
+	if (unlikely(flags & MSG_ERRQUEUE))
+		return sock_recv_errqueue(sk, msg, len, SOL_BLUETOOTH,
+					  BT_SCM_ERROR);
+
 	lock_sock(sk);
 
 	if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP,
net/bluetooth/smp.c +1 -1
index 8b9724fd752a..f5e5c2f111b9 100644
@@ -608,7 +608,7 @@ static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
 
 	iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iv, 2, 1 + len);
 
-	l2cap_chan_send(chan, &msg, 1 + len);
+	l2cap_chan_send(chan, &msg, 1 + len, NULL);
 
 	if (!chan->data)
 		return;
Signature · 1 line
2.48.1
#16 lore raw

PATCH v3 5/5 Bluetooth: SCO: add TX timestamping socket-level mechanism

In reply to Pauli Virtanen

Support TX timestamping in SCO sockets.

Support MSG_ERRQUEUE in SCO recvmsg.

---
 net/bluetooth/sco.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
net/bluetooth/sco.c +17 -2
index aa7bfe26cb40..f39c57ac594f 100644
@@ -370,7 +370,8 @@ static int sco_connect(struct sock *sk)
 	return err;
 }
 
-static int sco_send_frame(struct sock *sk, struct sk_buff *skb)
+static int sco_send_frame(struct sock *sk, struct sk_buff *skb,
+			  const struct sockcm_cookie *sockc)
 {
 	struct sco_conn *conn = sco_pi(sk)->conn;
 	int len = skb->len;
@@ -381,6 +382,7 @@ static int sco_send_frame(struct sock *sk, struct sk_buff *skb)
 
 	BT_DBG("sk %p len %d", sk, len);
 
+	hci_setup_tx_timestamp(skb, 1, sockc);
 	hci_send_sco(conn->hcon, skb);
 
 	return len;
@@ -776,6 +778,7 @@ static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg,
 {
 	struct sock *sk = sock->sk;
 	struct sk_buff *skb;
+	struct sockcm_cookie sockc;
 	int err;
 
 	BT_DBG("sock %p, sk %p", sock, sk);
@@ -787,6 +790,14 @@ static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg,
 	if (msg->msg_flags & MSG_OOB)
 		return -EOPNOTSUPP;
 
+	sockcm_init(&sockc, sk);
+
+	if (msg->msg_controllen) {
+		err = sock_cmsg_send(sk, msg, &sockc);
+		if (err)
+			return err;
+	}
+
 	skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0);
 	if (IS_ERR(skb))
 		return PTR_ERR(skb);
@@ -794,7 +805,7 @@ static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg,
 	lock_sock(sk);
 
 	if (sk->sk_state == BT_CONNECTED)
-		err = sco_send_frame(sk, skb);
+		err = sco_send_frame(sk, skb, &sockc);
 	else
 		err = -ENOTCONN;
 
@@ -860,6 +871,10 @@ static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 	struct sock *sk = sock->sk;
 	struct sco_pinfo *pi = sco_pi(sk);
 
+	if (unlikely(flags & MSG_ERRQUEUE))
+		return sock_recv_errqueue(sk, msg, len, SOL_BLUETOOTH,
+					  BT_SCM_ERROR);
+
 	lock_sock(sk);
 
 	if (sk->sk_state == BT_CONNECT2 &&
Signature · 1 line
2.48.1