feat(libSceNetCtl): read macOS interface byte counters #9728

Merged
MarcZX10 merged 2 commits from upstream-pr/9728 into main 2026-11-12 06:29:31 +00:00
MarcZX10 commented 2026-10-22 10:05:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T10:05:24Z.


What

Read macOS byte counters from AF_LINK interface records returned by getifaddrs. 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.cpp passed; open-PR overlap scan reported no overlap. The existing GuestSceNet test calls sceNetCtlGetIfStat when the host has a connected interface. A macOS build/run was not available in this Linux environment.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; silent stubs are listed in technical debt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4965612567 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9728). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T10:05:24Z. --- ### What Read macOS byte counters from `AF_LINK` interface records returned by `getifaddrs`. 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.cpp` passed; open-PR overlap scan reported no overlap. The existing `GuestSceNet` test calls `sceNetCtlGetIfStat` when the host has a connected interface. A macOS build/run was not available in this Linux environment. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; silent stubs are listed in technical debt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
jotitadev commented 2026-10-22 22:49:55 +00:00 (Migrated from github.com)

Imported 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 on en0.
Base: main alone doesn't link on macOS, so the tree is #8658 (feat/macos-port) @ 04d942dc80 after a full build. #8658 has its own macOS HostTraffic (a NET_RT_IFLIST2 walk), so I replaced its __APPLE__ branch with this PR's. The resulting Export.cpp is byte-identical to this PR's file at 952eea26db (git hash-object = 20fc3248928d).

  • With the PR: it compiles cleanly on macOS, and guest_sce_net passes 3/3. NetCtl state is connected here, so the test takes the sceNetCtlGetIfStat path: it gets 0, clears the fields, and the second call is >= the first.
  • Red check: with the __APPLE__ branch removed, guest_sce_net aborts with sceNetCtlGetIfStat: host interface byte counters are not implemented on this platform. With the PR's branch it passes again.

Counter width. The debt line on main that this PR replaces says an unprivileged process only gets the low 32 bits from both getifaddrs and sysctl(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_64 under Rosetta, same result), plus netstat -ib taken at the same moment, on en0:

source ifi_obytes ifi_ibytes
getifaddrs AF_LINK if_data (this PR) 3,022,870,528 963,467,264
sysctl(NET_RT_IFLIST2) if_msghdr2.ifm_data (#8658) 3,022,870,528 963,467,264
sysctl({CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, ifindex, IFDATA_GENERAL}) ifmibdata.ifmd_data 33,087,641,633 65,387,976,831
netstat -ib -I en0 33,087,641,633 65,387,976,897

The 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_IFDATA row matches netstat to the byte, and /usr/sbin/netstat is 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 by if_nametoindex and reading IFMIB_IFDATA for each gives the 64-bit totals. I summed all 31 up, non-loopback interfaces that way, with no sysctl failures: 33,203,934,866 / 65,982,117,814 bytes sent/received, against 3,139,155,968 / 1,557,602,304 from the getifaddrs sum this PR computes. That avoids both the throw on main and the documented wraparound. This also applies to #8658's HostTraffic: its comment says NET_RT_IFLIST2 carries 64-bit counters, but here they come back truncated like the getifaddrs ones.

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5621841680 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9728#pullrequestreview-5621841680). Originally posted by [jotitadev](https://github.com/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 on `en0`. **Base:** `main` alone doesn't link on macOS, so the tree is #8658 (`feat/macos-port`) @ `04d942dc80` after a full build. #8658 has its own macOS `HostTraffic` (a `NET_RT_IFLIST2` walk), so I replaced its `__APPLE__` branch with this PR's. The resulting `Export.cpp` is byte-identical to this PR's file at `952eea26db` (`git hash-object` = `20fc3248928d`). - **With the PR:** it compiles cleanly on macOS, and `guest_sce_net` passes 3/3. NetCtl state is connected here, so the test takes the `sceNetCtlGetIfStat` path: it gets 0, clears the fields, and the second call is `>=` the first. - **Red check:** with the `__APPLE__` branch removed, `guest_sce_net` aborts with `sceNetCtlGetIfStat: host interface byte counters are not implemented on this platform`. With the PR's branch it passes again. **Counter width.** The debt line on `main` that this PR replaces says an unprivileged process only gets the low 32 bits from both `getifaddrs` and `sysctl(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_64` under Rosetta, same result), plus `netstat -ib` taken at the same moment, on `en0`: | source | `ifi_obytes` | `ifi_ibytes` | |---|---|---| | `getifaddrs` `AF_LINK` `if_data` (this PR) | 3,022,870,528 | 963,467,264 | | `sysctl(NET_RT_IFLIST2)` `if_msghdr2.ifm_data` (#8658) | 3,022,870,528 | 963,467,264 | | `sysctl({CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, ifindex, IFDATA_GENERAL})` `ifmibdata.ifmd_data` | **33,087,641,633** | **65,387,976,831** | | `netstat -ib -I en0` | 33,087,641,633 | 65,387,976,897 | The 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_IFDATA` row matches `netstat` to the byte, and `/usr/sbin/netstat` is 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 by `if_nametoindex` and reading `IFMIB_IFDATA` for each gives the 64-bit totals. I summed all 31 up, non-loopback interfaces that way, with no `sysctl` failures: 33,203,934,866 / 65,982,117,814 bytes sent/received, against 3,139,155,968 / 1,557,602,304 from the `getifaddrs` sum this PR computes. That avoids both the throw on `main` and the documented wraparound. This also applies to #8658's `HostTraffic`: its comment says `NET_RT_IFLIST2` carries 64-bit counters, but here they come back truncated like the `getifaddrs` ones. 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.
MarcZX10 commented 2026-11-02 04:57:09 +00:00 (Migrated from github.com)

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 api GET on comments and reviews, git merge-tree --write-tree against upstream/main (926f7dd492), git grep and git show at the head. Nothing was built or run.

Results

Check Result Note
Trial merge vs upstream/main Clean Exit 0 at 926f7dd492
Hosted checks 4 success, none failing mergeStateStatus CLEAN
macOS counter source Read only No macOS host here, not run

Findings

  • oneandonlydean (2026-10-23 01:10Z) asked for a rebase onto main over the libSceNetCtl.md docs conflict with #9386. At head the trial merge is clean, and docs/dev/debt/libSceNetCtl.md:23 (the GetIfStat bullet) and :24 (the host interface cache bullet) both stay in the file.
  • jotitadev (2026-10-22 22:49Z, COMMENTED) says IFMIB_IFDATA returns 64-bit counters on macOS while getifaddrs and NET_RT_IFLIST2 give values mod 2^32, and that this applies to #8658 as well. At head the macOS branch sums ifi_obytes and ifi_ibytes from getifaddrs (Export.cpp:512-524), and debt/libSceNetCtl.md:23 says the sum is reduced to the low 32 bits on macOS. The reviewer's observation about getifaddrs matches the code. Their IFMIB_IFDATA observation 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.
  • The #8658 HostTraffic point is on another PR and is not checked here.

Sources

Not verified

  • The IFMIB_IFDATA 64-bit counters and the 31-interface sums (no macOS host).
  • The Rosetta and arm64 probe results.
  • The macOS test runs by maximilianfv and jotitadev.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5732247642 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9728#pullrequestreview-5732247642). Originally posted by [MarcZX10](https://github.com/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 api` GET on comments and reviews, `git merge-tree --write-tree` against `upstream/main` (926f7dd492), `git grep` and `git show` at the head. Nothing was built or run. ## Results | Check | Result | Note | |---|---|---| | Trial merge vs upstream/main | Clean | Exit 0 at 926f7dd492 | | Hosted checks | 4 success, none failing | mergeStateStatus CLEAN | | macOS counter source | Read only | No macOS host here, not run | ## Findings - oneandonlydean (2026-10-23 01:10Z) asked for a rebase onto main over the `libSceNetCtl.md` docs conflict with #9386. At head the trial merge is clean, and `docs/dev/debt/libSceNetCtl.md:23` (the `GetIfStat` bullet) and `:24` (the host interface cache bullet) both stay in the file. - jotitadev (2026-10-22 22:49Z, COMMENTED) says `IFMIB_IFDATA` returns 64-bit counters on macOS while `getifaddrs` and `NET_RT_IFLIST2` give values mod 2^32, and that this applies to #8658 as well. At head the macOS branch sums `ifi_obytes` and `ifi_ibytes` from `getifaddrs` (`Export.cpp:512-524`), and `debt/libSceNetCtl.md:23` says the sum is reduced to the low 32 bits on macOS. The reviewer's observation about `getifaddrs` matches the code. Their `IFMIB_IFDATA` observation 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. - The #8658 `HostTraffic` point is on another PR and is not checked here. ## Sources - https://github.com/boykopovar/AnyPS5/blob/5a706387f73e47a564cd573c413c36d75b7b8e04/core/libs/prx/libSceNetCtl/Export.cpp#L512-L524 - https://github.com/boykopovar/AnyPS5/blob/5a706387f73e47a564cd573c413c36d75b7b8e04/docs/dev/debt/libSceNetCtl.md#L23-L24 ## Not verified - The `IFMIB_IFDATA` 64-bit counters and the 31-interface sums (no macOS host). - The Rosetta and arm64 probe results. - The macOS test runs by maximilianfv and jotitadev.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!9728
No description provided.