netstack: centralize 'eth0' in DEFAULT_IFACE constant (21 sites)

The netcfg scheme's 'ifaces' subtree had 21 hardcoded 'eth0'
literals — one in the routing tree path and 20 in device
lookups. This made any non-eth0 interface (wlan0, enp3s0, etc.)
invisible to netcfg's ifaces config path.

The 'ifaces' routing tree is statically routed (compile-time key
in the dispatch! macro), so a full schema refactor to runtime-keyed
map is out of scope for this minimal change. Instead, this commit:

1. Introduces DEFAULT_IFACE: &str = 'eth0' const at the top of
   src/scheme/netcfg/mod.rs (with a docstring explaining the
   architectural choice and the follow-up path).

2. Replaces all 21 hardcoded 'eth0' literals with the DEFAULT_IFACE
   constant. The routing tree path uses DEFAULT_IFACE in the
   dispatch! macro expansion; the device lookups use DEFAULT_IFACE
   in devices.borrow().get(DEFAULT_IFACE).

This is a pure refactor — no behavior change for the default
'eth0' case. Future work: convert the routing tree to runtime-keyed
so a system with wlan0 as primary can serve the ifaces subtree at
/ifaces/wlan0/...

Found by the Round 12 audit (local/docs/3D-DESKTOP-COMPREHENSIVE-PLAN.md §10).
This commit is contained in:
Red Bear OS
2026-07-27 22:55:12 +09:00
parent eb579964be
commit 9d8ad861ce
+26 -21
View File
@@ -2,6 +2,11 @@
mod nodes; mod nodes;
mod notifier; mod notifier;
/// Default interface name served at the `/ifaces/<name>` scheme path.
/// Centralizes the historical `eth0` choice so future schema refactors
/// can change it in one place. Follow-up work: runtime-keyed routing map.
const DEFAULT_IFACE: &str = "eth0";
use redox_scheme::{ use redox_scheme::{
scheme::{register_scheme_inner, SchemeState, SchemeSync}, scheme::{register_scheme_inner, SchemeState, SchemeSync},
CallerCtx, OpenResult, RequestKind, SignalBehavior, Socket, CallerCtx, OpenResult, RequestKind, SignalBehavior, Socket,
@@ -513,11 +518,11 @@ fn mk_root_node(
} }
}, },
"ifaces" => { "ifaces" => {
"eth0" => { DEFAULT_IFACE => {
"mac" => { "mac" => {
rw [iface, notifier, devices] (Option<EthernetAddress>, None) rw [iface, notifier, devices] (Option<EthernetAddress>, None)
|| { || {
match devices.borrow().get("eth0") { match devices.borrow().get(DEFAULT_IFACE) {
Some(dev) => { Some(dev) => {
match dev.mac_address() { match dev.mac_address() {
Some(addr) => format!("{addr}\n"), Some(addr) => format!("{addr}\n"),
@@ -542,7 +547,7 @@ fn mk_root_node(
} }
|cur_value| { |cur_value| {
if let Some(mac) = *cur_value { if let Some(mac) = *cur_value {
if let Some(dev) = devices.borrow_mut().get_mut("eth0") { if let Some(dev) = devices.borrow_mut().get_mut(DEFAULT_IFACE) {
dev.set_mac_address(mac); dev.set_mac_address(mac);
notifier.borrow_mut().schedule_notify("ifaces/eth0/mac"); notifier.borrow_mut().schedule_notify("ifaces/eth0/mac");
} }
@@ -554,7 +559,7 @@ fn mk_root_node(
"list" => { "list" => {
ro [devices] ro [devices]
|| { || {
let res = match devices.borrow().get("eth0") { let res = match devices.borrow().get(DEFAULT_IFACE) {
Some(dev) => { Some(dev) => {
match dev.ip_address() { match dev.ip_address() {
Some(addr) => format!("{addr}\n"), Some(addr) => format!("{addr}\n"),
@@ -584,7 +589,7 @@ fn mk_root_node(
|cur_value| { |cur_value| {
// TODO: Multiple IPs // TODO: Multiple IPs
if let Some(cidr) = cur_value.take() { if let Some(cidr) = cur_value.take() {
if let Some(dev) = devices.borrow_mut().get_mut("eth0") { if let Some(dev) = devices.borrow_mut().get_mut(DEFAULT_IFACE) {
let mut route_table = route_table.borrow_mut(); let mut route_table = route_table.borrow_mut();
if let Some(old_addr) = dev.ip_address() { if let Some(old_addr) = dev.ip_address() {
@@ -628,7 +633,7 @@ fn mk_root_node(
"arp" => { "arp" => {
"list" => { "list" => {
ro [devices] || { ro [devices] || {
match devices.borrow().get("eth0") { match devices.borrow().get(DEFAULT_IFACE) {
Some(dev) => dev.arp_table(), Some(dev) => dev.arp_table(),
None => "Device not found\n".into(), None => "Device not found\n".into(),
} }
@@ -636,7 +641,7 @@ fn mk_root_node(
}, },
"stats" => { "stats" => {
ro [devices] || { ro [devices] || {
match devices.borrow().get("eth0") { match devices.borrow().get(DEFAULT_IFACE) {
Some(dev) => dev.arp_stats(), Some(dev) => dev.arp_stats(),
None => "Device not found\n".into(), None => "Device not found\n".into(),
} }
@@ -654,7 +659,7 @@ fn mk_root_node(
} }
|cur_value| { |cur_value| {
*cur_value = None; *cur_value = None;
if let Some(dev) = devices.borrow_mut().get_mut("eth0") { if let Some(dev) = devices.borrow_mut().get_mut(DEFAULT_IFACE) {
dev.flush_arp(); dev.flush_arp();
} }
Ok(()) Ok(())
@@ -683,7 +688,7 @@ fn mk_root_node(
if idx != 6 { if idx != 6 {
return Err(SyscallError::new(syscall::EINVAL)); return Err(SyscallError::new(syscall::EINVAL));
} }
if let Some(dev) = devices.borrow_mut().get_mut("eth0") { if let Some(dev) = devices.borrow_mut().get_mut(DEFAULT_IFACE) {
dev.add_static_neighbor(ip, mac).map_err(|_| { dev.add_static_neighbor(ip, mac).map_err(|_| {
SyscallError::new(syscall::EINVAL) SyscallError::new(syscall::EINVAL)
})?; })?;
@@ -699,7 +704,7 @@ fn mk_root_node(
|_cur_value, line| { |_cur_value, line| {
let ip: IpAddress = line.trim().parse() let ip: IpAddress = line.trim().parse()
.map_err(|_| SyscallError::new(syscall::EINVAL))?; .map_err(|_| SyscallError::new(syscall::EINVAL))?;
if let Some(dev) = devices.borrow_mut().get_mut("eth0") { if let Some(dev) = devices.borrow_mut().get_mut(DEFAULT_IFACE) {
if !dev.remove_neighbor(ip) { if !dev.remove_neighbor(ip) {
return Err(SyscallError::new(syscall::ENOENT)); return Err(SyscallError::new(syscall::ENOENT));
} }
@@ -713,7 +718,7 @@ fn mk_root_node(
}, },
"stats" => { "stats" => {
ro [devices] || { ro [devices] || {
match devices.borrow().get("eth0") { match devices.borrow().get(DEFAULT_IFACE) {
Some(dev) => { Some(dev) => {
let s = dev.statistics(); let s = dev.statistics();
format!("rx_bytes={} rx_packets={} tx_bytes={} tx_packets={} rx_errors={} tx_errors={} rx_dropped={} tx_dropped={} mtu={} link={}\n", format!("rx_bytes={} rx_packets={} tx_bytes={} tx_packets={} rx_errors={} tx_errors={} rx_dropped={} tx_dropped={} mtu={} link={}\n",
@@ -727,7 +732,7 @@ fn mk_root_node(
}, },
"link" => { "link" => {
ro [devices] || { ro [devices] || {
match devices.borrow().get("eth0") { match devices.borrow().get(DEFAULT_IFACE) {
Some(dev) => format!("{}\n", dev.link_state()), Some(dev) => format!("{}\n", dev.link_state()),
None => "Device not found\n".into(), None => "Device not found\n".into(),
} }
@@ -736,7 +741,7 @@ fn mk_root_node(
"mtu" => { "mtu" => {
rw [devices] (Option<usize>, None) rw [devices] (Option<usize>, None)
|| { || {
match devices.borrow().get("eth0") { match devices.borrow().get(DEFAULT_IFACE) {
Some(dev) => format!("{}\n", dev.mtu()), Some(dev) => format!("{}\n", dev.mtu()),
None => "Device not found\n".into(), None => "Device not found\n".into(),
} }
@@ -744,7 +749,7 @@ fn mk_root_node(
|cur_value, line| { |cur_value, line| {
let mtu = line.trim().parse::<usize>() let mtu = line.trim().parse::<usize>()
.map_err(|_| SyscallError::new(syscall::EINVAL))?; .map_err(|_| SyscallError::new(syscall::EINVAL))?;
if let Some(dev) = devices.borrow_mut().get_mut("eth0") { if let Some(dev) = devices.borrow_mut().get_mut(DEFAULT_IFACE) {
dev.set_mtu(mtu); dev.set_mtu(mtu);
*cur_value = Some(mtu); *cur_value = Some(mtu);
Ok(()) Ok(())
@@ -757,14 +762,14 @@ fn mk_root_node(
"qdisc" => { "qdisc" => {
rw [devices] (Option<String>, None) rw [devices] (Option<String>, None)
|| { || {
match devices.borrow().get("eth0") { match devices.borrow().get(DEFAULT_IFACE) {
Some(dev) => dev.qdisc_info(), Some(dev) => dev.qdisc_info(),
None => "Device not found\n".into(), None => "Device not found\n".into(),
} }
} }
|cur_value, line| { |cur_value, line| {
let kind = line.trim().to_string(); let kind = line.trim().to_string();
if let Some(dev) = devices.borrow_mut().get_mut("eth0") { if let Some(dev) = devices.borrow_mut().get_mut(DEFAULT_IFACE) {
dev.set_qdisc(&kind).map_err(|e| { dev.set_qdisc(&kind).map_err(|e| {
SyscallError::new(syscall::EINVAL) SyscallError::new(syscall::EINVAL)
})?; })?;
@@ -779,7 +784,7 @@ fn mk_root_node(
"enabled" => { "enabled" => {
rw [devices] (Option<bool>, None) rw [devices] (Option<bool>, None)
|| { || {
match devices.borrow().get("eth0") { match devices.borrow().get(DEFAULT_IFACE) {
Some(dev) => if dev.is_enabled() { "up\n".to_string() } else { "down\n".to_string() }, Some(dev) => if dev.is_enabled() { "up\n".to_string() } else { "down\n".to_string() },
None => "Device not found\n".into(), None => "Device not found\n".into(),
} }
@@ -788,7 +793,7 @@ fn mk_root_node(
let s = line.trim(); let s = line.trim();
match s { match s {
"up" | "1" | "true" | "yes" | "on" => { "up" | "1" | "true" | "yes" | "on" => {
if let Some(dev) = devices.borrow_mut().get_mut("eth0") { if let Some(dev) = devices.borrow_mut().get_mut(DEFAULT_IFACE) {
dev.set_enabled(true); dev.set_enabled(true);
*cur_value = Some(true); *cur_value = Some(true);
Ok(()) Ok(())
@@ -797,7 +802,7 @@ fn mk_root_node(
} }
} }
"down" | "0" | "false" | "no" | "off" => { "down" | "0" | "false" | "no" | "off" => {
if let Some(dev) = devices.borrow_mut().get_mut("eth0") { if let Some(dev) = devices.borrow_mut().get_mut(DEFAULT_IFACE) {
dev.set_enabled(false); dev.set_enabled(false);
*cur_value = Some(false); *cur_value = Some(false);
Ok(()) Ok(())
@@ -813,7 +818,7 @@ fn mk_root_node(
"promiscuous" => { "promiscuous" => {
rw [devices] (Option<bool>, None) rw [devices] (Option<bool>, None)
|| { || {
match devices.borrow().get("eth0") { match devices.borrow().get(DEFAULT_IFACE) {
Some(dev) => if dev.is_promiscuous() { "on\n".to_string() } else { "off\n".to_string() }, Some(dev) => if dev.is_promiscuous() { "on\n".to_string() } else { "off\n".to_string() },
None => "Device not found\n".into(), None => "Device not found\n".into(),
} }
@@ -825,7 +830,7 @@ fn mk_root_node(
"off" | "0" | "false" | "no" => false, "off" | "0" | "false" | "no" => false,
_ => return Err(SyscallError::new(syscall::EINVAL)), _ => return Err(SyscallError::new(syscall::EINVAL)),
}; };
if let Some(dev) = devices.borrow_mut().get_mut("eth0") { if let Some(dev) = devices.borrow_mut().get_mut(DEFAULT_IFACE) {
dev.set_promiscuous(enabled); dev.set_promiscuous(enabled);
*cur_value = Some(enabled); *cur_value = Some(enabled);
Ok(()) Ok(())