This document describes how visibility and re-exports work across Netstack3 Core's multi-crate architecture.
Netstack3 Core is organized into two layers of crates under src/connectivity/network/netstack3/core/:
netstack3_device, netstack3_ip, netstack3_tcp, etc.) and foundational/support crates (netstack3_base, netstack3_sync, etc.).netstack3_core): Located in core/src/, this crate depends on all Core sub-crates, wires their context traits together on CoreCtx and StackState, and exposes the single public API consumed by Bindings.Build-level visibility rules in BUILD.bazel enforce this layering:
//src/connectivity/network/netstack3) depends only on netstack3-core.netstack3_device) restrict production visibility to //src/connectivity/network/netstack3/core:__pkg__.netstack3_base) restrict visibility to //src/connectivity/network/netstack3/core:__subpackages__.As a result, Bindings cannot import Core sub-crates directly. Making an item pub inside a sub-crate exposes it to other Core crates, not to Bindings.
Each Core sub-crate controls its own crate boundary using private implementation modules, pub vs. pub(crate) visibility modifiers, and explicit re-exports in src/lib.rs.
mod internal)Domain sub-crates (e.g. netstack3_udp) wrap their source modules inside a private internal module in src/lib.rs:
#[path = "."] mod internal { pub(super) mod base; pub(super) mod counters; pub(super) mod diagnostics; }
This pattern provides three benefits:
#[path = "."] keeps source files directly under src/ rather than nested in a src/internal/ directory.src/lib.rs (for example, internal::device vs. pub mod device in netstack3_ip, or internal::ethernet vs. pub mod ethernet in netstack3_device).internal can be reorganized without breaking other Core crates.pub vs. pub(crate) Within a CrateBecause implementation modules are private, Rust allows marking an item pub even if it is never re-exported from src/lib.rs. Within each crate, follow this rule to keep visibility clear and compiler-checked:
Mark items shared only within the same crate as pub(crate) (or pub(super)), and mark an item pub if it is re-exported (or referenced by a symbol that is re-exported) from that crate's src/lib.rs.
Sub-crates explicitly pub use exported items in src/lib.rs, either at the crate root or grouped into curated pub mod namespaces (such as netstack3_ip::{device, gmp, icmp, multicast_forwarding, nud, raw, socket} or netstack3_device::{blackhole, ethernet, loopback, pure_ip, queue, socket}).
netstack3_coreThe top-level netstack3_core crate (core/src/lib.rs) acts as the curated facade for Bindings:
pub(crate) wiring modules implement each sub-crate's context traits on CoreCtx and StackState.pub used into flat domain modules in core/src/lib.rs (e.g. netstack3_core::tcp).StackState) are re-exported directly at the crate root.CoreApi: Bindings calls into Core through CoreApi (obtained from StackState::api), whose methods return each sub-crate's *Api handle bound to CoreCtx (e.g. CoreApi::tcp). Most *Api structs do not need to be re-exported by name because Bindings calls methods directly on the returned handle.Test-only types and helpers that need to be shared across Core crates or with Bindings tests are gated behind #[cfg(any(test, feature = "testutils"))] and re-exported from pub mod testutil in each crate's src/lib.rs (e.g. netstack3_base::testutil).
Some Core crates define dedicated build variants (-testutils, -instrumented, -benchmarks, -loom) alongside their default library target, and core/BUILD.bazel uses assert_no_deps tags to verify that production targets never depend on -testutils (or mismatched) sub-crate variants.
internal (or another private module) with private, pub(super), or pub(crate) visibility, and do not re-export it in src/lib.rs.netstack3_core integration code: Mark it pub inside the defining crate‘s private module, document it, and pub use it from that sub-crate’s src/lib.rs.pub use it into the appropriate domain module (or root) of core/src/lib.rs.