Skip to content

Add support for Linux Network Namespaces - #644

Open
Jafaral wants to merge 8 commits into
uniconproject:masterfrom
Jafaral:netns
Open

Jafaral wants to merge 8 commits into
uniconproject:masterfrom
Jafaral:netns

Conversation

@Jafaral

@Jafaral Jafaral commented Jul 31, 2026

Copy link
Copy Markdown
Member

No description provided.

Jafaral added 5 commits July 27, 2026 10:14
Detect Linux network namespaces at configure time and add the C helpers
used by open/fork/spawn and package net, including unprivileged userns
bootstrap, host-uid pin dirs, safe /run/netns mount probing, and an
atexit/signal reaper that only runs in the creating process.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
…spaces

Expose native netns handles through open("j"), close, fork, spawn, and
system, with ns["name"] / key(ns) for status and OptNetns available
outside Graphics builds.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Provide Netns/Link/Veth/Bridge/Vrf/Tun helpers that honor initns via
run(), prefer native ns File over ip netns exec, and fall back to
ephemeral open when persist fails.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Cover native open/peek/fork/spawn/system with forced userns=yes
(drop to nobody when root), name-collision behavior, and package-net
veth topology; soft-skip when unprivileged.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Describe mode "j", join via fork/spawn/system, status peek, unprivileged
bootstrap, and package net helpers.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
@greptile-apps

greptile-apps Bot commented Jul 31, 2026 •

Copy link
Copy Markdown

Greptile Summary

Adds Linux network-namespace support across the runtime and standard library.

  • Introduces namespace creation, joining, persistence, cleanup, status inspection, and optional user-namespace bootstrap.
  • Extends fork, spawn, and system to execute within namespace handles.
  • Adds namespace and virtual-link helpers, documentation, examples, build detection, and behavioral tests.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
src/common/rnetns.c Implements Linux namespace creation, user-namespace bootstrap, secure private pin selection, joining, reference management, and cleanup.
src/runtime/fmisc.r Extends spawn argument handling and synchronizes namespace ownership with co-expression thread creation.
src/runtime/rcoexpr.r Joins a pending namespace before executing co-expression code and routes join failures through thread cleanup.
src/runtime/fstruct.r Adds key iteration for namespace status while using the file mutex shared with close.
src/runtime/fsys.r Integrates namespace handles with open, close, and child-process execution paths.
uni/lib/netns.icn Adds namespace and virtual-link abstractions whose scoped operations use native handles or iproute2.
configure.ac Detects the Linux unshare, setns, and CLONE_NEWNET interfaces needed to enable the feature.
tests/posix/netns.icn Covers namespace creation, status, fork, system, spawn, persistence, bridge setup, and duplicate names.
tests/unicon/nettopo.icn Exercises the higher-level namespace and virtual-network topology helpers.

Sequence Diagram

sequenceDiagram
  participant Program
  participant Runtime
  participant Namespace
  participant Child
  Program->>Runtime: open(name, "j")
  Runtime->>Namespace: unshare and pin network namespace
  Namespace-->>Runtime: namespace handle
  Runtime-->>Program: file value
  Program->>Runtime: spawn/system/fork(handle, work)
  Runtime->>Child: create child process or thread
  Child->>Namespace: setns()
  Child->>Child: execute requested work
  Program->>Runtime: close(handle)
  Runtime->>Namespace: release reference and remove pins
Loading

Reviews (11): Last reviewed commit: "runtime: Clean up spawn netns join failu..." | Re-trigger Greptile

Comment thread uni/lib/netns.icn Resolved
Comment thread src/runtime/fmisc.r Resolved
Comment thread src/common/rnetns.c Resolved
@Jafaral
Jafaral force-pushed the netns branch 2 times, most recently from 2cc4bbd to 6b7ec9e Compare July 31, 2026 06:08
Comment thread src/runtime/fmisc.r Resolved
Jafaral added 2 commits July 31, 2026 16:28
Add a short Chapter 5 / langref note for open(...,"j") and status peek,
and a two-node veth+ping sample under uni/progs.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
open() already unregisters the thread around sock_listen(); a second
decrement around accept() let NARthreads go negative when another thread
was in delay(), so GC could collect live TLS open() strings (error 302).

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Comment thread src/runtime/fmisc.r Resolved
Comment thread src/runtime/fmisc.r Resolved
Comment thread src/common/rnetns.c Resolved
Comment thread src/runtime/fmisc.r Resolved
Comment thread src/runtime/fmisc.r Resolved
Comment thread src/runtime/fmisc.r
* Activate thread x for the first time.
*/
/* cp->tstate->K_level = k_level+1;*/
CREATE_CE_THREAD(cp, _stks_, "spawn()");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Failed spawn leaks namespace hold

When pthread_create fails during spawn(ns, ce), the namespace reference stored in cp->pending_ns remains held while CREATE_CE_THREAD still marks the co-expression as having a thread. Reclamation then skips the reference, leaving the namespace object and its bind-mount pins allocated and the co-expression associated with a thread that does not exist.

Knowledge Base Used: Interpreter, values, and memory runtime

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/runtime/fmisc.r
Line: 3042

Comment:
**Failed spawn leaks namespace hold**

When `pthread_create` fails during `spawn(ns, ce)`, the namespace reference stored in `cp->pending_ns` remains held while `CREATE_CE_THREAD` still marks the co-expression as having a thread. Reclamation then skips the reference, leaving the namespace object and its bind-mount pins allocated and the co-expression associated with a thread that does not exist.

**Knowledge Base Used:** [Interpreter, values, and memory runtime](https://app.greptile.com/unicon-project/-/custom-context/knowledge-base/uniconproject/unicon/-/docs/runtime-core.md)

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@greptile, is this still an issue?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes, this is still an issue. spawn acquires netns_hold() into cp->pending_ns before calling CREATE_CE_THREAD (src/runtime/fmisc.r:3030-3049). If pthread_create() fails, CREATE_CE_THREAD reports the fatal error without returning to spawn, so the new cleanup in src/runtime/rcoexpr.r is never reached. The pending hold is therefore not released via netns_release() on that path. The failure handling needs to unwind cp->pending_ns at the thread-creation site (or inside CREATE_CE_THREAD) before reporting the error; the nctramp cleanup only covers threads that were successfully created.

@Jafaral
Jafaral force-pushed the netns branch 2 times, most recently from 300f69b to 54086b3 Compare August 26, 2026 05:19
Comment thread src/runtime/rcoexpr.r Resolved
Comment thread src/runtime/fmisc.r Resolved
Join the namespace after SEM_WAIT so NARthreads is already counted
and a failed setns() can go through coclean. Undo pushact when a
concurrent spawn wins first-create or the ns handle is already closed.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>

This branch has not been deployed

No deployments
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.

1 participant