Skip to content

host/net: opt-in nn::socket network mode on the host's own IPv4 sockets - #3324

Open
RobiiAragon wants to merge 2 commits into
seedds:mainfrom
RobiiAragon:net/socket-transport
Open

RobiiAragon wants to merge 2 commits into
seedds:mainfrom
RobiiAragon:net/socket-transport

Conversation

@RobiiAragon

Copy link
Copy Markdown
Contributor

What and why

This is the first small, standalone piece of host networking we agreed on in #2589: an opt-in network mode for nn::socket. It runs the game's sockets on the host's own IPv4 TCP/UDP sockets. I wrote this transport for NX Port Studio, where it carried my completed Mac–Switch LAN race in Mario Kart 8 Deluxe 3.0.5 (2026-10-31). Later it went into #846 and #961. Here I extracted only the transport, on top of current main (4fdfd479).

The default is unchanged. Without DM_NETWORK=lan, main's offline local UDP/EventFd service answers every call as before, and t_net_loopback and t_net_eventfd pass unmodified.

How it fits main

  • One switch point: each dm_sk_* function in hle_net.c sends a network descriptor to hle_net_backend.c (net_socket.h). Every entry point (the enum, plain-argument and C overloads in hle_net.c and hle_offline.c) follows without a hook of its own. The only hook changes are that Listen and Accept now call dm_sk_listen and dm_sk_accept; offline they still return -1.
  • One descriptor namespace: network descriptors start at 64, above the service's EventFds, and all fit in the 1024 bits of fd_set. Select and a Poll that mixes both kinds keep working. A mixed set alternates the EventFd check with 5 ms host waits. An endless wait comes back after 100 ms, as main's offline one does. EventFd stays main's.
  • One error value: the transport reports through dm_sk_set_errno, so GetLastError, GetLastErrno and nnsocketGetLastErrno stay as they are. Network mode uses Horizon's Linux numbers: an empty non-blocking receive is EAGAIN 11, which Mario Kart 8 Deluxe's receive loop compares.

What the transport handles

  • ABI translation: the {u8 length, u8 family} sockaddr (sin_len 0 is accepted), short address buffers, O_NONBLOCK 0x800, and SOCK_NONBLOCK / SOCK_CLOEXEC. Option numbers at the socket, TCP and IP levels are mapped explicitly, because Darwin numbers some of them differently. Unknown options get ENOPROTOOPT instead of passing through. SO_ERROR comes back as a Horizon errno and SO_TYPE as Horizon's value.
  • SO_NOSIGPIPE on every native socket: Darwin has no MSG_NOSIGNAL, so a send to a TCP peer that closed would raise SIGPIPE and end the process. A guest that clears SO_NOSIGPIPE keeps it on and gets EPIPE 32, as Horizon gives.
  • Write-protected guest pages: every receive and GetSockOpt calls dm_guest_write_prepare on its output before the kernel writes it, and again after each wait. The kernel's copyout into a protected page fails with EFAULT instead of reaching the dirty-page fault hook.
  • Blocking and cancellation: native sockets are always non-blocking. Guest blocking, SO_RCVTIMEO / SO_SNDTIMEO (u32 milliseconds or the 16-byte TimeVal) and MSG_WAITALL are waits in 100 ms slices, with the guest core released (dm_thread_native_wait). Each call borrows a duplicate of the native descriptor. A Close on another thread ends a blocked call with EBADF through the slot's generation, and the shutdown on close wakes a TCP waiter at once.
  • nifm: it keeps its on-hold sequence (Pikmin 4, Hades). After that it reports the first up, running, broadcast-capable IPv4 interface, read at most once a second.

Name resolution, TLS, LDN, BCAT and the online services keep their offline answers and are not part of this PR.

Follow-ups, each its own PR on top of this

  • @eyasbzour: errno translation for the offline service, RecvMsg / SendMsg, EventFd in the socket table (a mixed wait in one host poll), default-route IP selection.
  • Mine: the cancellable IPv4 resolver (from Add cancellable IPv4 resolver operations (depends on #846) #956).
  • Mine: HLE_FORCE_WAIT, which publishes the guest's register context during a blocking host call so a pause (GetThreadContext3, a stop-the-world collector) can stop the thread. main's own blocking hooks don't have that yet either. It changes entry.S and hle_thread.c for every hook, so I'm keeping it separate. Until then, a thread blocked in a network-mode Recv / Accept can't be paused, which only matters with DM_NETWORK=lan.

Verification

I ran these on an Apple Silicon Mac, macOS 27.2, Xcode 27.2:

  • make -C dm/host/tests GAME=010015100B514000 run: the full host suite passed, including the new t_net (61 checks over loopback UDP and TCP through the game's entry points) and the unchanged t_net_loopback / t_net_eventfd / t_server_environment.
  • make … asan and make … tsan-threads (t_net added to TSAN_T): passed.
  • To check that t_net catches what it claims, I removed SO_NOSIGPIPE: the test process died of SIGPIPE. Without the write preparation, its check failed.
  • make -C dm/host GAME=010015100B514000 and dmhost --check-tables: no new warnings, tables pass.
  • clang --analyze on hle_net_backend.c and hle_net.c: no findings.
  • DM_GAME=010015100B514000 python3 dm/ios/tools/compile_check.py: 271 files, 0 failed.
  • python3 dm/tools/tests/test_docs.py and test_env_switches.py: passed (DM_NETWORK added to docs/env_switches.tsv; reference entry in docs/reference/services.md; symptom index regenerated).

I haven't run this exact branch in a LAN game session. The in-game evidence above comes from the NX Port Studio transport it's extracted from. I didn't run clang-tidy locally (no LLVM here), so the ratchet job in this PR's CI is its first run.

DM_NETWORK=lan sends each dm_sk_* call for a network descriptor to
hle_net_backend.c, so every enum, plain-argument and C entry point follows
without a hook of its own. Without it, main's offline local UDP/EventFd
service answers every call unchanged.

Network descriptors start at 64, above the service's EventFds, in one
1024-entry fd_set namespace; a mixed Poll/Select alternates the EventFd
check with short host waits. The transport translates Horizon's sockaddr,
Linux errno numbers, O_NONBLOCK and option numbers, keeps SO_NOSIGPIPE on
every native socket, prepares guest output before the kernel's copyout,
and ends a blocked call with EBADF when another thread closes its socket.
nifm keeps its on-hold sequence, then reports the host interface.

Name resolution, TLS, LDN and the online services stay offline. t_net
covers the transport on loopback UDP and TCP, including under ASan, UBSan
and TSan.
… descriptors

clang-tidy's analyzer could not see that the sockets t_net indexes fd_sets
with are valid, or that RecvFrom filled the source address before it is
compared. The test now starts the address buffers at zero and asserts both
descriptors are the transport's before using them as fd_set indexes.
@eyasbzour

Copy link
Copy Markdown

@RobiiAragon thanks for extracting this. I rebuilt my four fixes on top of it and opened them as drafts, one PR each, so they can be reviewed against your code now:

Name resolution isn't in any of them, since that's your #956 slice.

They're stacked on this branch, so each one also shows your two commits until this merges. Only the last commit in each is new. I'll mark them ready after this goes in.

@seedds this is the split from #2589.

@stephenschloesser

Copy link
Copy Markdown
Contributor

awesome are we finally going to get LDN support?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation game: 0100152000022000 Mario Kart 8 Deluxe host The Horizon host: SVCs, services, threads, memory (dm/host)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants