Openwrt为网络接口管理和配置创建了一个单独的project,netifd
Openwrt包含了netifd的package
Package组成netifd包含下面这些组件:
-
Shell脚本:/sbin/ifup, /sbin/ifdown(link to ifup), /sbin/ifstatus, /sbin/devstatus
-
init.d脚本:/etc/init.d/network
-
hotplug2脚本:/etc/hotplug.d/iface/00-netstate, /etc/hotplug.d/iface/10-sysctl
-
udhcpc脚本:/usr/share/udhcpc/default.script
-
netifd守护进程binary: /sbin/netifd
Shell脚本:/sbin/ifup
如前所述,ifdown实际上是指向ifup的符号链接,因此这两个脚本由同一个文件ifup实现。下面是其语法:
syntax: /sbin/{ifup|ifdown} [-a] [-w] [interface]
-a 选项指明对所有接口均执行相同的操作,此时interface被忽略.此参数默认为false
-w 选项指定是否执行wifi up操作。如果此参数被指定,则wifi up操作不会被执行。如果未指定,则在ifup的时候,wifi up会被执行
interface 指定down/up操作的目标接口
ifup的脚本里面,关于wifi的操作是通过/sbin/wifi脚本执行的,所以在这里暂时不讨论。
关于normal的if down/up操作,这个脚本是通过ubus命令来实现的。
下面是一个if_call() function:
可以看到这个function有一个参数,是interface,然后还使用了一个全局参数, modes, 在ifup脚本里面被定义,如下:
所以当执行ifdown lan时,对应的ubus命令为”ubus call network.interface.lan down”;
执行ifup lan时,ubus命令为两条,先执行”ubus call network.interface.lan down”,然
后是”ubus call network.interface.lan up”.
bus & ubusd
Openwrt提供了一个ubus系统,它类似于桌面linux系统的dbus,目标也是提供系统级的IPC和RPC。
ubus在设计理念上与dbus基本保持一致,区别在于简化的API和简练的模型,以适应于embedded router的特殊环境。
基本上来说, openwrt的ubus由下面几个组件组成:
-
ubusd,这个是ubus系统的后台进程,负责注册unix domain socket,分派ubus消息和事件等;
-
ubus,这是一个CLI utility,可以通过它访问ubus系统. ubus的帮助信息如下:
3. ubus提供的command有4种: list, call, listen & send, 通过这四种command,可以访问注册到ubus系统的服务
4. 最后是使用ubus的各个应用程序。这些应用程序可以在ubus系统中注册RPC接口,提供相应的服务。而其他程序可以使用这些接口,来访问这些服务。
netifd的ubus RPC接口
netifd在ubus系统中注册了的object如下:
[root@PandoraBox:/sbin]#ubus -v list
'dhcp' @e23aa747
"ipv4leases":{}
"ipv6leases":{}
'log' @543ceb34
"read":{"lines":"Integer"}
"write":{"event":"String"}
'network' @872d1335
"restart":{}
"reload":{}
"add_host_route":{"target":"String","v6":"Boolean","interface":"String"}
"get_proto_handlers":{}
"add_dynamic":{"name":"String"}
"del_dynamic":{"name":"String"}
'network.device' @cb99865e
"status":{"name":"String"}
"set_alias":{"alias":"Array","device":"String"}
"set_state":{"name":"String","defer":"Boolean"}
'network.interface' @220dbed7
"up":{}
"down":{}
"status":{}
"prepare":{}
"dump":{}
"add_device":{"name":"String","link-ext":"Boolean"}
"remove_device":{"name":"String","link-ext":"Boolean"}
"notify_proto":{}
"remove":{}
"set_data":{}
'network.interface.lan' @8bc32a99
"up":{}
"down":{}
"status":{}
"prepare":{}
"dump":{}
"add_device":{"name":"String","link-ext":"Boolean"}
"remove_device":{"name":"String","link-ext":"Boolean"}
"notify_proto":{}
"remove":{}
"set_data":{}
'network.interface.loopback' @252b22e6
"up":{}
"down":{}
"status":{}
"prepare":{}
"dump":{}
"add_device":{"name":"String","link-ext":"Boolean"}
"remove_device":{"name":"String","link-ext":"Boolean"}
"notify_proto":{}
"remove":{}
"set_data":{}
'network.interface.wan' @78475409
"up":{}
"down":{}
"status":{}
"prepare":{}
"dump":{}
"add_device":{"name":"String","link-ext":"Boolean"}
"remove_device":{"name":"String","link-ext":"Boolean"}
"notify_proto":{}
"remove":{}
"set_data":{}
'network.interface.wan6' @f5db1360
"up":{}
"down":{}
"status":{}
"prepare":{}
"dump":{}
"add_device":{"name":"String","link-ext":"Boolean"}
"remove_device":{"name":"String","link-ext":"Boolean"}
"notify_proto":{}
"remove":{}
"set_data":{}
'network.wireless' @20602bae
"up":{}
"down":{}
"status":{}
"notify":{}
"get_validate":{}
'service' @5a9a0d3c
"set":{"name":"String","script":"String","instances":"Table","triggers":"Array","validate":"Array"}
"add":{"name":"String","script":"String","instances":"Table","triggers":"Array","validate":"Array"}
"list":{"name":"String"}
"delete":{"name":"String","instance":"String"}
"update_start":{"name":"String"}
"update_complete":{"name":"String"}
"event":{"type":"String","data":"Table"}
"validate":{"package":"String","type":"String","service":"String"}
"get_data":{"type":"String"}
'system' @c70e3b8a
"board":{}
"info":{}
"upgrade":{}
"watchdog":{"frequency":"Integer","timeout":"Integer","stop":"Boolean"}
"signal":{"pid":"Integer","signum":"Integer"}
"nandupgrade":{"path":"String"}
每个object所提供的RPC接口名称,以及接口参数类型都可以通过ubus得到。
netifd interface RPC
netifd为每个interface object注册了一组相同的methods,如下:
然后可以发现,netifd里面还有一个protocol handler的概念,也就是对不同的interface protocol,可以提供不同的handler,来响应各种可能的事件。
最常见的static类型的protocol,内置在netifd中。而dhcp,pppoe等类型的协议,则以shell script的形式提供。
netifd protocol handler插件
netifd的protocol handler插件位于/lib/netifd/proto/目录下,名称统一为*.sh。
例如Pandorabox固件中:
阅读(5281) | 评论(0) | 转发(2) |