A netlink socket only sees the network namespace it was created in. To observe TCP sockets inside containers and Kubernetes pods, xtcp2 discovers accessible namespaces, enters each one with setns(CLONE_NEWNET), and opens a dedicated socket there. Reconciliation follows container churn, by default before each poll. Discovery is limited by process/mount visibility and permissions; it cannot observe namespaces the deployment does not expose.
- Where namespaces come from
- Discovery
- Watching for churn
- Reconciliation
- Entering a namespace
- Thread-leak avoidance
- Configuration
- See also
xtcp2 discovers namespaces through live processes under /proc/<pid>/ns/net and through bind-mounted namespace files in two standard locations:
/run/netns/— namespaces created byip netns add(linuxNetNSDirCst)./run/docker/netns/— namespaces created by Docker (dockerNetNsDirCst).
The process scan covers anonymous container/pod namespaces with no bind mount. The bind-mount scan covers namespaces without a visible host PID, including deployments without host PID sharing.
pkg/xtcp/ns_discover.go combines both scans, deduplicating by namespace inode. A process entry supplies a representative PID; a bind-mount entry supplies a path that can be opened directly. Where both identify the same inode, the process entry wins. Bind mounts also supply friendly names. The startup reconciliation seeds the active set, and later passes repeat discovery.
Namespace discovery uses repeated scans, not an inotify watcher. The reconciler dispatches changes to:
pkg/xtcp/ns_add.go— handles a newly observed namespace.pkg/xtcp/ns_delete.go— handles a namespace that has gone away.
pkg/xtcp/ns_reconcile.go compares the discovered inode set with active readers, adds new namespaces, and cancels readers for vanished namespaces. The daemon's own namespace remains tracked. Reconciliation runs at startup and, by default, immediately before each poll. A background ticker provides a fallback when polling is idle or disabled; setting its interval to zero disables that ticker, not startup or enabled pre-poll reconciliation.
The per-namespace reader lives in pkg/xtcp/ns_net_namespace.go. Because setns operates on the calling OS thread, the reader:
- Calls
runtime.LockOSThread()to pin the goroutine to its OS thread. - Snapshots the original namespace, then
setns(CLONE_NEWNET)into the target. - Opens a netlink socket — now scoped to that namespace — and runs the netlinkers.
- On exit, restores the original namespace and releases the thread only if restoration succeeds; otherwise the runtime terminates the locked thread.
Entering namespaces requires CAP_SYS_ADMIN. Access to protected /proc/<pid>/ns/net entries also requires CAP_SYS_PTRACE; the kernel applies ptrace_may_access checks even to root. Missing that capability produces a startup warning and reduces process-based discovery. Accessible bind-mounted namespaces remain discoverable without host PID visibility. Container deployments must expose the relevant namespace mounts and/or host processes. See observability.
Rapid namespace churn (e.g. Kubernetes pods cycling) is the hard case: each setns requires a locked OS thread, and a thread whose namespace can't be restored cannot be safely reused. xtcp2 handles this deliberately — a retry loop bounds the setns attempts, and the OS thread is only unlocked (returned to the pool) when the original namespace was successfully restored; otherwise the thread is allowed to die rather than leak a wrong namespace into a reused thread. The -maxThreads flag caps the Go runtime's OS thread count (debug.SetMaxThreads) as a backstop against runaway growth. The dedicated thread-leak test (pkg/xtcp/ns_thread_leak_test.go) guards this behavior.
| Flag | Default | Purpose |
|---|---|---|
-reconcileBeforePoll |
true |
Refresh the namespace set before each poll cycle. |
-reconcileFrequency |
6h |
Background reconcile fallback; 0 disables the ticker. |
-maxThreads |
2000 |
Cap on Go runtime OS threads (debug.SetMaxThreads); 0 = Go default (10000). |
-maxNetns |
4096 |
Cap on namespaces tracked, xtcp2's own included; 0 = unlimited. MAX_NETNS. |
-maxNetnsPerOwner |
64 |
Cap on namespaces tracked per owner (below); 0 = off. MAX_NETNS_PER_OWNER. |
-maxNetnsAddsPerReconcile |
256 |
Cap on namespaces added by one reconcile; 0 = off. MAX_NETNS_ADDS_PER_RECONCILE. |
-reconcileTimeout |
1s |
Deadline for one reconcile's scan; 0 = none. RECONCILE_TIMEOUT. |
Each tracked namespace costs a netlink socket, a receiver and its decoders, so -maxNetns bounds them. At the cap, a tracked namespace keeps its slot until it is deleted; nothing is evicted, because any ranking a tenant can influence (a pid wraps) would let it push monitored workloads out. New namespaces wait. When a slot frees, they get it in the order xtcp2 first saw them, so recreating a namespace does not jump the queue. xtcp2's own namespace is always tracked. Each waiting namespace counts in xtcp_netns_dropped_total once per reconcile and logs a line.
Alert on any increase: it means some namespaces are not monitored. The reason label says which cap: max_netns, owner_quota or add_rate.
Per-owner quota. So one workload can't take every slot, -maxNetnsPerOwner caps the namespaces tracked per owner. The owner comes from the cgroup path of the namespace's process:
- a container: the path down to the runtime's own component, where runtimes put it (
/system.slice/docker-<id>.scope,/machine.slice/libpod-<id>.scope,/docker/<id>, or acri-containerd-/crio-scope or bare id under kubepods with only kubelet components above it); - under
user.slice: the user's slice, so one user's processes and rootless containers are one owner; - anything else: the first two components (one system service).
A process can create sub-cgroups only below its own, and a component is only taken as a container where the components above it are the runtime's, so naming a sub-cgroup like a container doesn't make a new owner. A service that delegates its cgroup is one owner. Namespaces with no process (bind mounts only) are exempt, since only the host can make them; they still count toward -maxNetns. A namespace kept out by the quota keeps its place in the queue; one whose process exits before its cgroup is read waits for the next reconcile.
Bounded reconcile. A reconcile runs before every poll, so it is bounded so that a burst of namespaces can't hold up polling:
-reconcileTimeoutbounds the/procscan. A scan that times out deletes nothing, since a namespace it didn't reach may still be live, and waiting namespaces keep their places. While scans keep timing out, a namespace that died keeps its slot; alert onxtcp_reconcile_incomplete_total{reason="timeout"}.- One scan takes in at most 4 ×
-maxNetnsnew namespaces. Past that it keeps walking/procbut reports only namespaces already tracked or waiting, so deletes stay correct. The waiting queue is capped at the same size, and a namespace whose owner is at quota gets no place in it, so one owner can't fill it. -maxNetnsAddsPerReconcilebounds the namespaces entered per reconcile.- Names and owners (a read of
/proc/<pid>/cgroupeach) are resolved only for namespaces being added; owners oldest waiter first, at least 64 per reconcile even after the deadline.
Each scan cut short adds 1 to xtcp_reconcile_incomplete_total{reason="timeout"|"scan_cap"} and logs a line. The container's pids limit also bounds how many namespaces a workload can hold open (each needs a process). ConfigService.Set keeps the running caps. The per-namespace dump caps and the poll-wide socket gate are in polling and batching.
The bind-mount directories (/run/netns/, /run/docker/netns/) are built in, not flags.
- Netlink collection — what each per-namespace reader does.
- Performance — thread and parallelism tuning.
- Integration testing — the microVM namespace-lifecycle, tcp-stress, and discovery-bench tests that exercise this path.
- Design: namespace discovery & reconciliation — design history and the directory-scan vs
/proc-scan discovery analysis.