feat(libSceNetCtl): read macOS interface byte counters #9728
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9728"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T10:05:24Z.
What
Read macOS byte counters from
AF_LINKinterface records returned bygetifaddrs. Sum counters for interfaces that are up and not loopback, matching the existing Windows/Linux host approximation. Update the debt entry to document the host-only nature and 32-bit wraparound.Tested
Linux x86-64: conventions passed;
g++ -std=c++20 -fsyntax-only -Icore/libs -Icore/libs/prx/libc/include core/libs/prx/libSceNetCtl/Export.cpppassed; open-PR overlap scan reported no overlap. The existingGuestSceNettest callssceNetCtlGetIfStatwhen the host has a connected interface. A macOS build/run was not available in this Linux environment.Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T22:49:55Z.
Review: COMMENTED
macOS run, which the last review asked for, plus one measurement that bears on the debt entry this PR rewrites.
Machine: Apple M2 Max, macOS 27.2 (26B5112a), Xcode 27 (Apple clang), x86-64 build under Rosetta 2, Release,
BUILD_TESTING=ON, Python 3.9.6, run as an ordinary user (uid 502), Wi-Fi up onen0.Base:
mainalone doesn't link on macOS, so the tree is #8658 (feat/macos-port) @04d942dc80after a full build. #8658 has its own macOSHostTraffic(aNET_RT_IFLIST2walk), so I replaced its__APPLE__branch with this PR's. The resultingExport.cppis byte-identical to this PR's file at952eea26db(git hash-object=20fc3248928d).guest_sce_netpasses 3/3. NetCtl state is connected here, so the test takes thesceNetCtlGetIfStatpath: it gets 0, clears the fields, and the second call is>=the first.__APPLE__branch removed,guest_sce_netaborts withsceNetCtlGetIfStat: host interface byte counters are not implemented on this platform. With the PR's branch it passes again.Counter width. The debt line on
mainthat this PR replaces says an unprivileged process only gets the low 32 bits from bothgetifaddrsandsysctl(NET_RT_IFLIST2). I can confirm that, but one other unprivileged source returns the full 64-bit value. I ran a small C probe (arm64 and-arch x86_64under Rosetta, same result), plusnetstat -ibtaken at the same moment, onen0:ifi_obytesifi_ibytesgetifaddrsAF_LINKif_data(this PR)sysctl(NET_RT_IFLIST2)if_msghdr2.ifm_data(#8658)sysctl({CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, ifindex, IFDATA_GENERAL})ifmibdata.ifmd_datanetstat -ib -I en0The first two rows are the real counter mod 2^32, rounded down to 1 KiB (33,087,641,633 mod 2^32 = 3,022,870,561). The
IFMIB_IFDATArow matchesnetstatto the byte, and/usr/sbin/netstatis not setuid (-r-xr-xr-x root wheel). So on this host the counters don't have to wrap every 4 GiB. Iterating the up, non-loopback interfaces byif_nametoindexand readingIFMIB_IFDATAfor each gives the 64-bit totals. I summed all 31 up, non-loopback interfaces that way, with nosysctlfailures: 33,203,934,866 / 65,982,117,814 bytes sent/received, against 3,139,155,968 / 1,557,602,304 from thegetifaddrssum this PR computes. That avoids both the throw onmainand the documented wraparound. This also applies to #8658'sHostTraffic: its comment saysNET_RT_IFLIST2carries 64-bit counters, but here they come back truncated like thegetifaddrsones.The probe (a standalone C file, not part of the tree) is short, and I can attach it if it helps.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T04:57:09Z.
Review: COMMENTED
Reviewed — rebase request is already met, macOS counter claim not verified
The conflict with #9386 does not appear at head
5a706387f7, and the 64-bit counter claim needs a macOS run that was not done here.What I ran
Head
5a706387f73e47a564cd573c413c36d75b7b8e04(live, OPEN). Read-only:gh pr view,gh apiGET on comments and reviews,git merge-tree --write-treeagainstupstream/main(926f7dd492),git grepandgit showat the head. Nothing was built or run.Results
926f7dd492Findings
libSceNetCtl.mddocs conflict with #9386. At head the trial merge is clean, anddocs/dev/debt/libSceNetCtl.md:23(theGetIfStatbullet) and:24(the host interface cache bullet) both stay in the file.IFMIB_IFDATAreturns 64-bit counters on macOS whilegetifaddrsandNET_RT_IFLIST2give values mod 2^32, and that this applies to #8658 as well. At head the macOS branch sumsifi_obytesandifi_ibytesfromgetifaddrs(Export.cpp:512-524), anddebt/libSceNetCtl.md:23says the sum is reduced to the low 32 bits on macOS. The reviewer's observation aboutgetifaddrsmatches the code. TheirIFMIB_IFDATAobservation would mean the debt line is wrong for the sysctl route. Whether to change the code to use it, or to change the debt line, is your call.HostTrafficpoint is on another PR and is not checked here.Sources
github.com/boykopovar/AnyPS5@5a706387f7/core/libs/prx/libSceNetCtl/Export.cpp (L512-L524)github.com/boykopovar/AnyPS5@5a706387f7/docs/dev/debt/libSceNetCtl.md (L23-L24)Not verified
IFMIB_IFDATA64-bit counters and the 31-interface sums (no macOS host).