原文地址:http://xinliang.me/blog/?p=149linux
netifd包含下面這些組件:shell
下面對這些組件,逐一進行分析,以期理解netifd的基本工做機制.網絡
如前所述,ifdown其實是指向ifup的符號連接,所以這兩個腳本由同一個文件ifup實現。下面是其語法:dom
1
2
3
4
|
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:socket
1
2
3
4
5
6
|
if_call() {
local interface="$1"
for mode in $modes; do
ubus call $interface $mode
done
}
|
能夠看到這個function有一個參數,是interface,而後還使用了一個全局參數, modes, 在ifup腳本里面被定義,以下:oop
1
2
3
4
5
6
7
8
|
case "$0" in
*ifdown) modes=down;;
*ifup)
modes="down up"
setup_wifi=1
;;
*) echo "Invalid command: $0";;
esac
|
因此當執行ifdown lan時,對應的ubus命令爲」ubus call network.interface.lan down」;執行ifup lan時,ubus命令爲兩條,先執行」ubus call network.interface.lan down」,而後是」ubus call network.interface.lan up」.插件
Openwrt提供了一個ubus系統,它相似於桌面linux系統的dbus,目標也是提供系統級的IPC和RPC。ubus在設計理念上與dbus基本保持一致,區別在於簡化的API和簡練的模型,以適應於embedded router的特殊環境。
基本上來講, openwrt的ubus由下面幾個組件組成:設計
1
2
3
4
5
6
7
8
9
10
11
|
Usage: ubus [options] <command></command> [arguments...]
Options:
-s : Set the unix domain socket to connect to
-t : Set the timeout (in seconds) for a command to complete
-S: Use simplified output (for scripts)
-v: More verbose output
Commands:
- list [] List objects
- call [] Call an object method
- listen [...] Listen for events
- send [] Send an event
|
netifd在ubus系統中註冊了的object以下:unix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
root@OpenWrt:/# ubus list -v
'network' @25a06dad
"restart": { }
"reload": { }
"add_host_route": { "target": "String", "v6": "Boolean" }
"get_proto_handlers": { }
'network.device' @9d97d655
"status": { "name": "String" }
"set_alias": { "alias": "(unknown)", "device": "String" }
"set_state": { "name": "String", "defer": "Boolean" }
'network.interface.lan' @f9e7258b
"up": { }
"down": { }
"status": { }
"prepare": { }
"add_device": { "name": "String" }
"remove_device": { "name": "String" }
"notify_proto": { }
"remove": { }
"set_data": { }
'network.interface.loopback' @6d026db0
"up": { }
"down": { }
"status": { }
"prepare": { }
"add_device": { "name": "String" }
"remove_device": { "name": "String" }
"notify_proto": { }
"remove": { }
"set_data": { }
'network.interface.wan' @ade92c65
"up": { }
"down": { }
"status": { }
"prepare": { }
"add_device": { "name": "String" }
"remove_device": { "name": "String" }
"notify_proto": { }
"remove": { }
"set_data": { }
|
每一個object所提供的RPC接口名稱,以及接口參數類型均可以經過ubus獲得rest
netifd爲每一個interface object註冊了一組相同的methods,以下:
1
2
3
4
5
6
7
8
9
10
11
|
static struct ubus_method iface_object_methods[] = {
{ .name = "up", .handler = netifd_handle_up },
{ .name = "down", .handler = netifd_handle_down },
{ .name = "status", .handler = netifd_handle_status },
{ .name = "prepare", .handler = netifd_handle_iface_prepare },
UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
{ .name = "notify_proto", .handler = netifd_iface_notify_proto },
{ .name = "remove", .handler = netifd_iface_remove },
{ .name = "set_data", .handler = netifd_handle_set_data },
};
|
而後能夠發現,netifd裏面還有一個protocol handler的概念,也就是對不一樣的interface protocol,能夠提供不一樣的handler,來響應各類可能的事件。最多見的static類型的protocol,內置在netifd中。而dhcp,pppoe等類型的協議,則以shell script的形式提供。
netifd的protocol handler插件位於/lib/netifd/proto/目錄下,名稱統一爲*.sh。