[RK3288][Android6.0] 網路服務Netd初始化流程小結
阿新 • • 發佈:2019-01-09
Platform: Rockchip
OS: Android 6.0
Kernel: 3.10.92
封裝底層各種型別網路,如PPP,SOFTAP等,給framework提供統一介面.
說白了就是接收framework命令往下發,接收kernel命令往上發.
Android.mk client include server
[[email protected]:~/rk3288/system/netd]$ tree
.
├── Android.mk
├── client
│ ├── Android.mk
│ ├── FwmarkClient.cpp
│ ├── FwmarkClient.h
│ └── NetdClient.cpp
├── include
│ ├── FwmarkCommand.h
│ ├── Fwmark.h
│ ├── NetdClient.h
│ └── Permission.h
└── server
├── Android.mk
├── BandwidthController.cpp
├── BandwidthController.h
├── ClatdController.cpp
├── ClatdController.h
├── CleanSpec.mk
├── CommandListener.cpp
├── CommandListener.h
├── ConnmarkFlags.h
├── DnsProxyListener.cpp
├── DnsProxyListener.h
├── DummyNetwork.cpp
├── DummyNetwork.h
├── FirewallController.cpp
├── FirewallController.h
├── FwmarkServer.cpp
├── FwmarkServer.h
├── IdletimerController.cpp
├── IdletimerController.h
├── InterfaceController.cpp
├── InterfaceController.h
├── List.h
├── LocalNetwork.cpp
├── LocalNetwork.h
├── main.cpp
├── MDnsSdListener.cpp
├── MDnsSdListener.h
├── NatController.cpp
├── NatController.h
├── ndc.c
├── NetdCommand.cpp
├── NetdCommand.h
├── NetdConstants.cpp
├── NetdConstants.h
├── NetlinkHandler.cpp
├── NetlinkHandler.h
├── NetlinkManager.cpp
├── NetlinkManager.h
├── NetworkController.cpp
├── NetworkController.h
├── Network.cpp
├── Network.h
├── oem_iptables_hook.cpp
├── oem_iptables_hook.h
├── PhysicalNetwork.cpp
├── PhysicalNetwork.h
├── PppController.cpp
├── PppController.h
├── ResolverController.cpp
├── ResolverController.h
├── ResponseCode.h
├── RouteController.cpp
├── RouteController.h
├── SoftapController.cpp
├── SoftapController.h
├── StrictController.cpp
├── StrictController.h
├── TetherController.cpp
├── TetherController.h
├── UidRanges.cpp
├── UidRanges.h
├── VirtualNetwork.cpp
└── VirtualNetwork.h
3 directories, 72 files
服務啟動 :
system/core/rootdir/init.rc
建立了4個socket:
netd: 用於framework和kernel命令互動.
dnsproxyd: 用於dns
mdns: 用於網內無dns伺服器時的情況
fwmarkd: 對特定的socket設定mark值.
下面程式碼流程裡提到的
CommandListener, DnsproxyListener, MdnsSdListener以及FwmarkServer
這四個類就是對應的上面四個socket, 它們分別用於接收framework的各類資訊.
初始化流程 :
借用網友的一張框圖,畫得很好,再看程式碼就很清晰了.
new NetlinkManager //主要接收來自kernel的uevent事件,由NetlinkHandle來完成接收,然後通過CommandListener通知framework.
new CommandListener -> //用於和framework互動
FrameworkListener("netd", true) -> //父類構造桉樹,socket是"netd", 說明和framework通過netd socket來互動了. BTW, vold也使用此方式和framework通訊.
SocketListener ->
new SocketClientCollection //建立socket client
registerCmd(new NatCmd()); -> //註冊各類cmd供framework呼叫kernel.
FrameworkListener::registerCmd -> FrameworkListener.cpp
mCommands->push_back //儲存到cmd列表中
new NetworkController //以下建立各個cmd對應的類,對應各個cmd的實現定義.
new NatCmd
......
nm->start ->
NetlinkManager::start ->
setupSocket //建立三種類型socket, 分別是NETLINK_KOBJECT_UEVENT, NETLINK_ROUTE以及NETLINK_NFLOG.
NETLINK_KOBJECT_UEVENT: 一般用來通知核心某個模組安裝或者解除安裝。
NETLINK_ROUTE:網路變化時會觸發此訊息。
NETLINK_NFLOG:頻寬預警訊息通知。
socket //標準建立socket介面方法.
bind
new NetlinkHandler -> NetlinkHandler.cpp
NetlinkListener ->
SocketListener
handler->start ->
NetlinkHandler::start ->
this->startListener ->
SocketListener::startListener ->
pthread_create ->
SocketListener::threadStart ->
me->runListener ->
accept
onDataAvailable -> 呼叫各個lister介面,這裡是NetlinkListener
uevent_kernel_recv
new NetlinkEvent
evt->decode
onEvent ->
NetlinkHandler::onEvent ->
notifyInterfaceAdded ->
mNm->getBroadcaster()->sendBroadcast //通知framework
參考 :
OS: Android 6.0
Kernel: 3.10.92
Netd: Network Daemon.
負責網路配置,操作,管理,查詢等功能.封裝底層各種型別網路,如PPP,SOFTAP等,給framework提供統一介面.
說白了就是接收framework命令往下發,接收kernel命令往上發.
路徑: system/netd
[[email protected]:~/rk3288/system/netd]$ lsAndroid.mk client include server
[[email protected]:~/rk3288/system/netd]$ tree
.
├── Android.mk
├── client
│ ├── Android.mk
│ ├── FwmarkClient.cpp
│ ├── FwmarkClient.h
│ └── NetdClient.cpp
├── include
│ ├── FwmarkCommand.h
│ ├── Fwmark.h
│ ├── NetdClient.h
│ └── Permission.h
└── server
├── Android.mk
├── BandwidthController.cpp
├── BandwidthController.h
├── ClatdController.cpp
├── ClatdController.h
├── CleanSpec.mk
├── CommandListener.cpp
├── CommandListener.h
├── ConnmarkFlags.h
├── DnsProxyListener.cpp
├── DnsProxyListener.h
├── DummyNetwork.cpp
├── DummyNetwork.h
├── FirewallController.cpp
├── FirewallController.h
├── FwmarkServer.cpp
├── FwmarkServer.h
├── IdletimerController.cpp
├── IdletimerController.h
├── InterfaceController.cpp
├── InterfaceController.h
├── List.h
├── LocalNetwork.cpp
├── LocalNetwork.h
├── main.cpp
├── MDnsSdListener.cpp
├── MDnsSdListener.h
├── NatController.cpp
├── NatController.h
├── ndc.c
├── NetdCommand.cpp
├── NetdCommand.h
├── NetdConstants.cpp
├── NetdConstants.h
├── NetlinkHandler.cpp
├── NetlinkHandler.h
├── NetlinkManager.cpp
├── NetlinkManager.h
├── NetworkController.cpp
├── NetworkController.h
├── Network.cpp
├── Network.h
├── oem_iptables_hook.cpp
├── oem_iptables_hook.h
├── PhysicalNetwork.cpp
├── PhysicalNetwork.h
├── PppController.cpp
├── PppController.h
├── ResolverController.cpp
├── ResolverController.h
├── ResponseCode.h
├── RouteController.cpp
├── RouteController.h
├── SoftapController.cpp
├── SoftapController.h
├── StrictController.cpp
├── StrictController.h
├── TetherController.cpp
├── TetherController.h
├── UidRanges.cpp
├── UidRanges.h
├── VirtualNetwork.cpp
└── VirtualNetwork.h
3 directories, 72 files
服務啟動
system/core/rootdir/init.rc
service netd /system/bin/netd
class main
socket netd stream 0660 root system
socket dnsproxyd stream 0660 root inet
socket mdns stream 0660 root system
socket fwmarkd stream 0660 root inet
建立了4個socket:
netd: 用於framework和kernel命令互動.
dnsproxyd: 用於dns
mdns: 用於網內無dns伺服器時的情況
fwmarkd: 對特定的socket設定mark值.
下面程式碼流程裡提到的
CommandListener, DnsproxyListener, MdnsSdListener以及FwmarkServer
這四個類就是對應的上面四個socket, 它們分別用於接收framework的各類資訊.
初始化流程
借用網友的一張框圖,畫得很好,再看程式碼就很清晰了.
main -> main.cpp
NetlinkManager::Instance -> NetlinkManager.cppnew NetlinkManager //主要接收來自kernel的uevent事件,由NetlinkHandle來完成接收,然後通過CommandListener通知framework.
new CommandListener -> //用於和framework互動
FrameworkListener("netd", true) -> //父類構造桉樹,socket是"netd", 說明和framework通過netd socket來互動了. BTW, vold也使用此方式和framework通訊.
SocketListener ->
new SocketClientCollection //建立socket client
registerCmd(new NatCmd()); -> //註冊各類cmd供framework呼叫kernel.
FrameworkListener::registerCmd -> FrameworkListener.cpp
mCommands->push_back //儲存到cmd列表中
new NetworkController //以下建立各個cmd對應的類,對應各個cmd的實現定義.
new NatCmd
......
nm->start ->
NetlinkManager::start ->
setupSocket //建立三種類型socket, 分別是NETLINK_KOBJECT_UEVENT, NETLINK_ROUTE以及NETLINK_NFLOG.
NETLINK_KOBJECT_UEVENT: 一般用來通知核心某個模組安裝或者解除安裝。
NETLINK_ROUTE:網路變化時會觸發此訊息。
NETLINK_NFLOG:頻寬預警訊息通知。
socket //標準建立socket介面方法.
bind
new NetlinkHandler -> NetlinkHandler.cpp
NetlinkListener ->
SocketListener
handler->start ->
NetlinkHandler::start ->
this->startListener ->
SocketListener::startListener ->
pthread_create ->
SocketListener::threadStart ->
me->runListener ->
accept
onDataAvailable -> 呼叫各個lister介面,這裡是NetlinkListener
uevent_kernel_recv
new NetlinkEvent
evt->decode
onEvent ->
NetlinkHandler::onEvent ->
notifyInterfaceAdded ->
mNm->getBroadcaster()->sendBroadcast //通知framework
參考