netifd 是什么:OpenWrt 网络接口的中枢
在练习中成长
不用等读完,先测一下netifd 是 OpenWrt 的网络接口配置守护进程。 它把
/etc/config/network变成接口/设备/路由状态,并通过 ubus 暴露给 ifup、LuCI、防火墙和其他服务。
先分清:UCI、config.c、interface.c、ubus.c
这是最容易混的一层:
| 层 | 位置 | 职责 |
|---|---|---|
| 持久化配置 | /etc/config/network | UCI 文件,netifd 的输入,不是 netifd 的数据库 |
| 配置加载 | config.c | 用 libuci 解析文件,构造 struct interface / struct device |
| 运行时状态 | interface.c / device.c | 状态机、设备 claim、地址/路由、协议事件 |
| ubus 暴露 | ubus.c | 注册 network.interface.* / network.device,处理 up/down/status |
netifd 不等于 UCI 的包装器:UCI 只是它的一种配置来源;即使没有 UCI 变更,运行时状态也会因为 link、DHCP lease、协议脚本回调而变化。config.c 只负责“把文件读成内存对象”,不负责 ubus 对象本身。
它在哪一层
/etc/config/network (UCI)
↓
netifd
├─ interface 状态机
├─ device 管理
├─ 协议处理器 /lib/netifd/proto/*.sh
└─ ubus: network.interface.* / network.device.*
↓
ip / routing / firewall / DNS
netifd 不是防火墙,也不是 DNS;它负责“接口起来没有、拿到什么地址/路由”,然后让其他组件去消费。
启动链路
/etc/init.d/network 用 procd 启动 /sbin/netifd:
procd_set_param command /sbin/netifd
procd_set_param respawn
procd_set_param watch network.interface
reload 不是重启进程,而是:
ubus call network reload
/etc/init.d/network reload 最终也是走这个 ubus 调用。
从 UCI 到内存对象
典型配置:
config interface 'wan'
option proto 'dhcp'
option device 'eth0'
config interface 'lan'
option proto 'static'
option device 'br-lan'
option ipaddr '192.168.1.1'
option netmask '255.255.255.0'
/etc/config/network 是持久化输入;config.c 解析后构造内存里的 interface/device。配置加载后:
- 建
network.interface.wan/lan对象;
- 绑定 device;
- 调对应协议处理器(
dhcp、static、pppoe…);
- 协议脚本把地址/路由/ DNS 通过
proto_*接口回传给 netifd;
- netifd 更新接口状态并广播 ubus 事件。
协议处理器
协议处理器是 shell 脚本,放在 /lib/netifd/proto/。以 DHCP 为例,dhcp.script 会调用:
proto_init_update "*" 1
proto_add_ipv4_address "$ip" "$netmask"
proto_add_ipv4_route 0.0.0.0 0 "$gateway" "$ip"
proto_send_update "$interface"
这些函数由 /lib/netifd/netifd-proto.sh 提供,作用是把“协议结果”写回 netifd。自定义协议时,照这个接口实现,不要直接改 /etc/config/network。
改配置:UCI 写文件,ubus 触发应用
不要把它理解成“通过 ubus 改 netifd 配置”。持久化配置仍然在 /etc/config/network:
| 动作 | 走什么 | 结果 |
|---|---|---|
| 改接口地址/协议 | uci set + uci commit network | 只改持久化文件 |
| 让 netifd 重新读 | ubus call network reload 或 /etc/init.d/network reload | netifd 重新解析 UCI,协调接口 |
| 只动一个接口 | ifup wan | reload + 对该接口 down/up |
| 临时运行时操作 | ubus call network.interface.wan up/down | 改运行时状态,不写 UCI |
| 协议脚本回传地址/路由 | ubus call network.interface notify_proto | 更新运行时,不写 UCI |
netifd 的 ubus 方法主要是 up、down、renew、status、dump、add_device、remove_device、notify_proto、set_data,没有“写 /etc/config/network”的通用 set-config 方法。
所以 LuCI 保存网络设置时,本质也是:
LuCI 表单 → UCI 写入 → uci commit → network reload → netifd 重建运行时
ubus 在这里是“触发/查询/运行控制”,不是持久化配置存储。
既然 UCI 能跨进程,为什么还要 ubus
因为 UCI 只回答“我想配成什么”,不回答“现在实际是什么”。
| UCI | ubus(netifd) | |
|---|---|---|
| 内容 | 持久化期望配置 | 运行时接口/设备/地址/路由状态 |
| 数据来源 | /etc/config/network | 内核 netlink、DHCP lease、协议脚本、设备 hotplug |
| 操作 | 读/写/commit 文件 | up/down/renew/status/dump/notify_proto |
| 变更通知 | 没有内建事件,需要 reload | 对象事件、ubus monitor、订阅通知 |
| 并发 | 多个进程可写,后提交覆盖前面的 | netifd 单所有者,按状态机串行协调 |
| 权限 | 文件权限 | ubus ACL,可按对象/方法授权 |
| 失败语义 | commit 成功不等于接口起来 | 返回接口/协议的真实状态和错误 |
一个最典型的例子:
config interface 'wan'
option proto 'dhcp'
UCI 只知道“wan 用 DHCP”。DHCP 拿到的 IP、网关、DNS、lease 时间、默认路由,都不在 UCI 里;这些是 netifd 运行时状态,只能通过 ifstatus wan / ubus call network.interface.wan status 拿到。
再比如 option auto '1':UCI 说“想自动起来”,但网线没插、device 没被 claim、DHCP 没响应时,接口实际状态是 down/pending;读 UCI 永远看不出这个区别。
所以两者的分工是:
UCI:desired state(可持久化)
netifd:reconcile + actual state(运行时)
ubus:查询/操作/订阅这个运行时
直接写 UCI 再 commit,不会自动跑 DHCP,也不会把接口拉起来;直接 ubus call network.interface.wan up 能改运行时,但重启/重载后不会持久化。两边不能互相替代。
状态由什么决定
可以把它压缩成一个协调公式:
实际状态 = reconcile(
UCI 期望配置,
硬件/内核观测,
软件执行结果,
运行时事件/操作
)
四类输入分别是:
- UCI 期望:
/etc/config/network、/etc/config/wireless,以及 board.json 等声明的目标; - 硬件/内核观测:device 是否存在、carrier 是否 up、地址/路由是否真的进内核;
- 软件执行结果:DHCP/PPP/WiFi 协议脚本、netifd 状态机、hotplug、daemon;
- 运行时事件/操作:carrier 变化、lease 续期、
ifup/ifdown、ubus 调用。
关键点:它不是一次性计算,而是持续 reconcile。
同一个 UCI:
config interface 'wan'
option proto 'dhcp'
option auto '1'
可能出现:
| 实际情况 | 状态 |
|---|---|
| 没插网线 | down / pending |
| 网线插上,DHCP 没响应 | pending |
| DHCP 拿到地址 | up,带 IP/网关/DNS |
ifdown wan | 运行时 down,但 UCI 仍是 auto 1 |
| 重启后 | 重新按 UCI 尝试拉起 |
所以:
UCI 决定“想怎样”,硬件/软件决定“能不能、实际怎样”,netifd 负责持续协调,ubus 暴露协调后的结果。
常用命令
# 看所有接口
ubus call network.interface dump
# 看单个接口
ubus call network.interface.wan status
ifstatus wan
# 看设备
ubus call network.device status '{"name":"eth0"}'
devstatus eth0
# 重新读配置
ubus call network reload
/etc/init.d/network reload
# 只动一个接口
ifup wan
ifdown wan
ifup 不是“只发一个 up 事件”:在 OpenWrt 脚本里,它先 ubus call network reload,再对该接口执行 down/up,确保配置和状态一致。
uci commit / reload / ifup 的区别
| 操作 | 作用 |
|---|---|
uci commit network | 只把配置写入 /etc/config/network |
ubus call network reload | 让 netifd 重新读配置并协调接口 |
ifup wan | reload + 对该接口 down/up |
ifdown wan | 关闭指定接口 |
改了 UCI 不 commit,netifd 看不到;commit 了不 reload,netifd 也不会自动应用。
常见排障
- 接口存在但没地址
看 ifstatus wan 的 up、pending、available,以及协议处理器日志。
ubus call network.interface.wan status报 not found
接口没被 netifd 创建,先检查 UCI 配置和 network reload。
- 有地址没默认路由
看 ip route 和 ifstatus 的 route 字段;DHCP 路由由协议脚本回传。
- 改了配置不生效
检查是否 uci commit + network reload;LuCI 保存通常会做,但手改文件不一定。
- 协议处理器缺失
/lib/netifd/proto/<proto>.sh 不存在或不可执行,接口会停在错误状态。
- 日志
logread -e netifd,再配合 ubus monitor 看接口事件。
和 prplOS/AMX 的边界
这里要纠正一个常见误解:prplOS 不等于 netifd。
- OpenWrt 基线通常用 netifd;
- 但 prplOS 5.2 的 x86 profile 里
CONFIG_PACKAGE_netifd is not set,网络管理走 NetModel + netdev-plugin + netmodel-netdev; - 不同 prplOS profile/厂商可能保留 netifd 或 vendor 变体,以实际
.config和进程为准; - AMX/TR-181 是管理面对象模型,不会自动把对象树变成接口;
- 所以正确说法是:OpenWrt 常用 netifd;prplOS 的部分 profile 用 NetModel/netdev 替代它,不是“prplOS 一定跑 netifd”。
源码解读:五个关键位置
源码版本:OpenWrt/prplOS 源码树里的 package/network/config/netifd,netifd 7901e66(2025-05-23)。下面只贴关键路径;注意 prplOS 5.2 x86 实际配置未选这个包。
1. main():先连 ubus,再读配置,最后进事件循环
main.c 的启动顺序:
if (netifd_ubus_init(socket) < 0) {
fprintf(stderr, "Failed to connect to ubus\n");
return 1;
}
proto_shell_init();
extdev_init();
wireless_init();
if (system_init()) {
fprintf(stderr, "Failed to initialize system control\n");
return 1;
}
config_init_all();
uloop_run();
解读:
netifd_ubus_init先把network.*对象注册到 ubus,后面配置才能发布对象/事件;proto_shell_init注册 shell 协议处理器,system_init初始化 Linux 网络后端;config_init_all读 UCI 并建接口;uloop_run才进入事件循环,处理 ubus 请求、netlink 和协议脚本回调。
所以 network reload 不是重新执行 main(),而是让事件循环里的 reload handler 重新走配置流程。
2. config_init_all():加载器,不是存储层
先明确:config.c 做的是“读 UCI → 建内存对象”,不是把配置存进 netifd。存储仍是 /etc/config/network,由 UCI/libuci 管理。
config.c 在配置阶段设置 config_init = true,全部读完后才清掉:
uci_network = config_init_package("network");
uci_wireless = config_init_package("wireless");
config_init_board();
vlist_update(&interfaces);
config_init = true;
device_reset_config();
config_init_devices(true);
config_init_vlans();
config_init_devices(false);
config_init_interfaces();
config_init_ip();
config_init_rules();
config_init_globals();
config_init_wireless();
config_init = false;
device_reset_old();
device_init_pending();
vlist_flush(&interfaces);
interface_refresh_assignments(false);
interface_start_pending();
解读:
config_init = true期间,接口即使满足 up 条件也不能启动,否则会读到半成品配置;- 全部设备/接口/IP/规则建完后,
interface_start_pending()才统一拉起待启动接口; device_reset_old()/vlist_flush()负责清掉已删除的配置对象;- 这也是“改完配置必须 reload”的源码级原因。
3. interface_check_state():状态机和启动条件
interface.c 的接口状态机:
case IFS_DOWN:
if (!iface->available)
return;
if (iface->autostart && iface->enabled && link_state && !config_init)
__interface_set_up(iface);
break;
__interface_set_up():
iface->state = IFS_SETUP;
ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
if (ret)
mark_interface_down(iface);
解读:
available:设备存在;link_state:链路可用,或接口被强制 link;autostart/enabled:UCI 的 auto 和启用状态;!config_init:配置加载期间不启动;- 条件满足后进入
IFS_SETUP,把“怎么配地址/路由”交给协议处理器,而不是 netifd 自己写 IP。
接口对象挂到 ubus 是在 interface_update() 创建路径里完成的:
} else if (node_new) {
D(INTERFACE, "Create interface '%s'", if_new->name);
interface_event(if_new, IFEV_CREATE);
proto_init_interface(if_new, if_new->config);
interface_claim_device(if_new);
netifd_ubus_add_interface(if_new);
}
这说明顺序是:config 解析 → 内存 interface 创建 → 绑定协议/设备 → 注册 ubus 对象。UCI 变化只是触发这条链路,不是链路本身。
4. ubus.c:全局对象和每接口对象
network.interface 是对象类型,每个接口再注册一个 network.interface.<name>:
static struct ubus_object iface_object = {
.name = "network.interface",
.type = &iface_object_type,
.n_methods = ARRAY_SIZE(iface_object_methods),
};
asprintf(&name, "%s.interface.%s", main_object.name, iface->name);
obj->name = name;
obj->type = &iface_object_type;
obj->methods = iface_object_methods;
ubus_add_object(ubus_ctx, &iface->ubus);
解读:
network.interface提供dump、notify_proto等全局方法;network.interface.wan提供up、down、status等 per-interface 方法;ifstatus wan、LuCI、防火墙都通过这层对象拿状态;netifd_ubus_interface_event()还会发ifup/ifdown事件。
5. netifd-proto.sh:协议结果怎么回传
协议脚本不直接改内核,而是拼 JSON 后调用 ubus:
proto_init_update() {
PROTO_KEEP=0
PROTO_INIT=1
PROTO_IPADDR=
PROTO_ROUTE=
PROTO_DNS=
json_init
json_add_int action 0
json_add_boolean "link-up" "$up"
}
proto_send_update() {
...
_proto_notify "$interface"
}
_proto_notify() {
json_add_string "interface" "$interface"
ubus $options call network.interface notify_proto "$(json_dump)"
}
解读:
proto_init_update初始化一份协议结果;proto_add_ipv4_address/proto_add_ipv4_route只是在填数组;proto_send_update把结果通过network.interface notify_proto发回 netifd;- netifd 的
proto-shell状态机收到后,才把地址/路由写到系统; - 所以“协议脚本负责发现结果,netifd 负责应用结果”,边界很清楚。
验证状态
- ✅ 源码/脚本:OpenWrt/prplOS 源码树的 netifd
7901e66(2025-05-23)关键片段;prplOS 5.2 x86 配置里CONFIG_PACKAGE_netifd is not set; - ✅ 命令语义:
network reload、ifup、ubus对象; - ⚠️ 未在物理设备实测:接口状态/协议行为随固件和协议处理器变化;
- ❓ 未覆盖:完整状态机、所有协议处理器差异、netlink/system-linux 细节。
源码:netifd
7901e66(2025-05-23),提交。 系列:OpenWrt 相关:UCI 指南 · 从 UCI 到数据模型 · prplOS 的 UCI 后端
评论 0
还没有评论,来抢沙发~