Skip to content

fix(libkernel): report SCM_BINTIME and SCM_TIMESTAMP from recvmsg as measured on a PS5 - #11141

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
orezraey:fix/libkernel-recvmsg-timestamps-measured-ps5
Oct 26, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
orezraey:fix/libkernel-recvmsg-timestamps-measured-ps5

Conversation

@orezraey

@orezraey orezraey commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

What

recvmsg on a UDP socket now returns the SCM_BINTIME and SCM_TIMESTAMP control messages while SO_BINTIME or SO_TIMESTAMP is set, as a retail PS5 (firmware 13.60) does. Until now controlLength was always 0, and the options were only kept as bits. Measured with a homebrew probe from a payload process; the source and console log are in this gist (ptstamp).

On the console, over loopback:

  • With no option set, msg_controllen is 0.
  • SO_TIMESTAMP on IPv4 gives one 32-byte record: cmsg_len 32, level 0xffff (SOL_SOCKET), type 2 (SCM_TIMESTAMP), then a 16-byte timeval holding the real time of arrival.
  • SO_BINTIME gives {32, 0xffff, 4} (SCM_BINTIME) and a 16-byte bintime: seconds, then a 64-bit fraction.
  • With both set, SCM_BINTIME comes first, then SCM_TIMESTAMP, and msg_controllen is 64.
  • A 16-byte or 8-byte control buffer gets the first 16 or 8 bytes of the first record, msg_controllen 16 or 8, and MSG_CTRUNC (0x20). The data is intact.
  • On IPv6 only SCM_TIMESTAMP is produced; SO_BINTIME adds nothing, as in FreeBSD 11's ip6_savecontrol.

The records are built in a new ArrivalTimestamps helper. The fraction is computed from nanoseconds as FreeBSD's timespec2bintime does. recvmsg copies as many bytes as the guest buffer holds and sets MSG_CTRUNC when some are cut off, as the console did; FreeBSD 11.4's kern_recvit copies whole messages only, so this partial copy is a measured difference from it. Four cases were not measured and follow 11.4: an empty control buffer with a stamp pending (nothing copied, msg_controllen 0, MSG_CTRUNC), a null msg_control (nothing, no flag), and TCP and Unix sockets (no message).

The time is guest CLOCK_REALTIME, read when the host hands the datagram over. That is later than the moment it reached the socket, and the debt entry says so. Sockets created through libSceNet have their own receive path and are unchanged.

Before and after

recvmsg on a UDP socket PS5 AnyPS5 before AnyPS5 now
no option set msg_controllen 0 0 0
SO_TIMESTAMP one 32-byte SCM_TIMESTAMP record msg_controllen 0 one 32-byte SCM_TIMESTAMP record
SO_BINTIME (IPv4) one 32-byte SCM_BINTIME record msg_controllen 0 one 32-byte SCM_BINTIME record
both options SCM_BINTIME then SCM_TIMESTAMP, 64 bytes msg_controllen 0 SCM_BINTIME then SCM_TIMESTAMP, 64 bytes
8- or 16-byte control buffer first bytes copied, MSG_CTRUNC msg_controllen 0 first bytes copied, MSG_CTRUNC
SO_BINTIME on IPv6 nothing added msg_controllen 0 nothing added

Tested

Windows 11, WinLibs GCC 15.2, full build.

  • guest_udp passes 3 times out of 3. On IPv4 and IPv6 it covers no option, each option and both, with control buffers of 0, 8, 16, 40, 64 and 80 bytes. It checks the record layout and order, msg_controllen, MSG_CTRUNC, that bytes past the copied length stay untouched, and that the seconds fall between CLOCK_REALTIME readings taken before and after the call.
  • The 90 tests matching socket|udp|tcp|net_|recv|send|underscore pass.
  • tools/check_conventions.py passes.

Head 49bcd7549e on main aeff26bd97.

Head 19566dbee8 sets MSG_CTRUNC for an empty control buffer with a stamp pending, as 11.4 does (wachi35676's review), and guest_udp now expects it and checks that a null msg_control gets no flag. guest_udp and guest_socket_options pass 3 times out of 3 on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL, GCC 13.3); with the old Udp.cpp line guest_udp fails. tools/check_conventions.py passes.

Checklist

…measured on a PS5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 49bcd7549e8a4baba5bb6cfdb7ca203013c2ca94. The IPv4-only SCM_BINTIME and IPv4/IPv6 SCM_TIMESTAMP records use the expected 32-byte control-message layout, order, arrival seconds/fraction encoding, and capacity-based truncation behavior. The helper is restricted to UDP IP sockets, and the debt entry records that the timestamp is sampled after host receive rather than at kernel packet arrival.

Local checks:

  • GCC 12.2 syntax-only compilation of Udp.cpp and GuestUdp.cpp: passed.
  • python3 tools/check_conventions.py --base upstream/main --head HEAD and git diff --check upstream/main...HEAD: passed.

The author reports PS5 observations for both message types/order/truncation and Windows guest_udp coverage across IPv4/IPv6, option combinations and control-buffer sizes. Hosted eligibility, conventions, intake, labels and report pass; product build/test jobs are skipped. I did not independently run the PS5 probe or Windows runtime tests. Declared merge-order dependencies remain as documented. No blocking issue found.

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 7090 core/libs/prx/libkernel/Socket/src/Udp.cpp L1664, L1703

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 6149 docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7192 docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7447 docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
PR 7804 docs/dev/debt/libkernel.md
PR 8605 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 8916 docs/dev/debt/libkernel.md
PR 9156 docs/dev/debt/libkernel.md
PR 9199 docs/dev/debt/libkernel.md
PR 9284 docs/dev/debt/libkernel.md
PR 9293 docs/dev/debt/libkernel.md
PR 9297 docs/dev/debt/libkernel.md
PR 9302 docs/dev/debt/libkernel.md
PR 9389 docs/dev/debt/libkernel.md
PR 9442 docs/dev/debt/libkernel.md
PR 9557 docs/dev/debt/libkernel.md
PR 9566 docs/dev/debt/libkernel.md
PR 9575 docs/dev/debt/libkernel.md
PR 9713 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 9947 docs/dev/debt/libkernel.md
PR 9948 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 10442 docs/dev/debt/libkernel.md
PR 10749 docs/dev/debt/libkernel.md
PR 11002 docs/dev/debt/libkernel.md
PR 11042 docs/dev/debt/libkernel.md
PR 11085 docs/dev/debt/libkernel.md
PR 11215 docs/dev/debt/libkernel.md
PR 11264 docs/dev/debt/libkernel.md
PR 11267 docs/dev/debt/libkernel.md
PR 11341 docs/dev/debt/libkernel.md

@Galluno

Galluno commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

We had a parallel implementation of the same control messages on Galluno:claude/exciting-franklin-i0jtz0 (b6d89c39fb) and found this PR while checking for overlaps before opening it, so we are not opening a duplicate. Your console measurement settles the one point where the two differ: ours copies only whole 32-byte records, so a 16- or 8-byte control buffer would get nothing, where the console copies the first 16 or 8 bytes and sets MSG_CTRUNC.

Windows run of our branch, for reference: Windows 11 Pro 10.0.26340, WinLibs GCC 15.2.0 r7, Release, b6d89c39fb on main cceb6c3c4. All 35 guest_(socket|tcp|underscore|udp|sendfile|select|poll) tests pass, and guest_udp passes 10/10. On Windows it stamps with guest CLOCK_REALTIME when the receive returns, as this PR does.

The part this PR leaves out is the arrival time on Linux and macOS. Our branch enables SO_TIMESTAMPNS (Linux) or SO_TIMESTAMP (macOS) on the native socket while the guest has either option set and takes the stamp from the host recvmsg, so a datagram that waited in the queue gets the time it arrived, and a MSG_PEEK and the receive after it return the same stamp. Once this PR is merged I can open that as a separate follow-up on top of your ArrivalTimestamps helper and truncation, unless you would rather add it here.

AI-assisted: yes (Claude Code)

@orezraey

Copy link
Copy Markdown
Contributor Author

I'd keep this PR to the measured stamp and the truncation, so a separate follow-up from you on top of ArrivalTimestamps for the host arrival time on Linux and macOS works for me once this one lands. Your MSG_PEEK point (the peek and the receive after it returning the same stamp) is not covered here, since this PR stamps each receive when it returns.

@wachi35676

Copy link
Copy Markdown
Contributor

Tested head 49bcd7549e merged onto main 49d1f9b07e, Windows 11 (WinLibs GCC 15.2.0, Release) and WSL2 Ubuntu 24.04 (GCC 13.3), BUILD_TESTING=ON.

  • guest_udp, guest_socket_options, guest_socket_read_write, guest_socket_pair, guest_tcp: 5/5 pass, 3 runs, on both hosts. So the Linux path is covered too. With Udp.cpp from main, guest_udp fails on both hosts and the other four pass.

Two wording points, from FreeBSD 11.4 kern_recvit (sys/kern/uipc_syscalls.c:1141-1155):

for (m = control; m != NULL && len >= m->m_len; m = m->m_next) { /* copy the whole record */ }
if (m != NULL) mp->msg_flags |= MSG_CTRUNC;
  • 11.4 copies whole control messages only, so the console's partial copy is a measured difference from it. The description says the copy follows that loop, but it doesn't.
  • With a non-null msg_control, 11.4 collects the control data, so an empty buffer with a stamp pending gets msg_controllen 0 and MSG_CTRUNC. The debt entry says "An empty buffer gets nothing and no MSG_CTRUNC ... as in FreeBSD 11.4", and the capacity-0 case in CheckArrivalTimestamps expects flags == 0. Since this case wasn't measured, the entry could say it is unmeasured and differs from 11.4. Or it could follow 11.4 and set MSG_CTRUNC.

Nit: in the SO_DEBUG, SO_REUSEADDR ... entry, "Both are set on the host" now follows a list of four options.

AI-assisted: yes (Claude Code); results above were run on this machine.

… FreeBSD 11.4 does

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@orezraey

Copy link
Copy Markdown
Contributor Author

Thanks, both points were right. 19566db follows 11.4 for an empty control buffer with a stamp pending: msg_controllen 0 and MSG_CTRUNC, while a null msg_control still gets nothing and no flag. The debt entry now says the partial copy is the console's and differs from 11.4's whole-message loop, lists the empty buffer as unmeasured after 11.4, and the SO_DEBUG ... entry names SO_REUSEADDR and SO_REUSEPORT instead of "Both"; the description is updated to match. guest_udp expects MSG_CTRUNC at capacity 0 and checks the null-buffer case; it and guest_socket_options pass 3 times out of 3 on Windows and on Linux (WSL), and guest_udp fails with the old line.

@wachi35676

Copy link
Copy Markdown
Contributor

Re-checked head 19566dbee8 merged onto main 0f34ebb85 (clean). Windows 11 with WinLibs GCC 15.2.0, and WSL Ubuntu 24.04 with GCC 13.3, both Release.

  • guest_udp, guest_socket_options, guest_socket_read_write, guest_socket_pair and guest_tcp pass 5/5 on both hosts, 3 runs.
  • With only && controlCapacity != 0 put back in the MSG_CTRUNC condition, guest_udp fails on both hosts. So the empty-buffer case is now pinned to 11.4's MSG_CTRUNC, and the null-msg_control receive stays at controlLength == 0, flags == 0.
  • Nit: check_conventions.py warns that the reworded SO_DEBUG, SO_REUSEADDR, ... debt entry is 1,016 characters, 16 over the 1,000 limit (a warning only).

AI-assisted: yes (Claude Code); results above were run on this machine.

… characters

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@orezraey

Copy link
Copy Markdown
Contributor Author

@wachi35676 thanks: 053ca30db4 trims the SO_DEBUG, SO_REUSEADDR, ... entry to 997 characters ("if the console treats it as FreeBSD 12's SO_REUSEPORT_LB" now reads "if it is FreeBSD 12's SO_REUSEPORT_LB"), and check_conventions passes with no warning. Documentation only; the code and tests are unchanged from 19566dbee8.

@oneandonlydean oneandonlydean self-assigned this Oct 26, 2026
@orezraey

Copy link
Copy Markdown
Contributor Author

@Ragbie #11223 and this PR conflict in recvmsg in core/libs/prx/libkernel/Socket/src/Udp.cpp: the receive check if (total != 0 || socket->type == 1 || ReadableNow(*socket)) is where #11223 adds its ReceiveShut test and where this PR declares the timestamp buffer. GuestUdp.cpp and the libkernel debt file merge cleanly. I propose #11223 lands first; I will then merge main into this branch, keep your shut-for-reading check and re-add the timestamp buffer above it. I added #11223 to this PR's Depends on (merge order only).

@oneandonlydean
oneandonlydean merged commit d018922 into boykopovar:main Oct 26, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants