Android10 rk3399 以太网接入流程分析

Netd守护进程服务

  • Netd模块是Android中专门负责网络管理和控制的后台守护进程

  • 开发板路径./etc/init/netd.rc

    service netd /system/bin/netd
    class main
    socket dnsproxyd stream 0660 root inet
    socket mdns stream 0660 root system
    socket fwmarkd stream 0660 root inet
    onrestart restart zygote
    onrestart restart zygote_secondary
    # b/121354779: netd itself is not updatable, but on startup it dlopen()s the resolver library
    # from the DNS resolver APEX. Mark it as updatable so init won't start it until all APEX
    # packages are ready.
    updatable
    ~

Netd源码分析

system\netd\server\main.cpp

复制代码
int main() {
    Stopwatch s;
    gLog.info("netd 1.0 starting");

    android::net::process::removePidFile(PID_FILE_PATH);
    android::net::process::blockSigPipe();

    // Before we do anything that could fork, mark CLOEXEC the UNIX sockets that we get from init.
    // FrameworkListener does this on initialization as well, but we only initialize these
    // components after having initialized other subsystems that can fork.
    for (const auto& sock :
         {DNSPROXYLISTENER_SOCKET_NAME, FwmarkServer::SOCKET_NAME, MDnsSdListener::SOCKET_NAME}) {
        setCloseOnExec(sock);
    }

    // Before we start any threads, populate the resolver stub pointers.
    resolv_stub_init();

    // Make sure BPF programs are loaded before doing anything
    while (!android::base::WaitForProperty("bpf.progs_loaded", "1",
           std::chrono::seconds(5))) {
        ALOGD("netd waited 5s for bpf.progs_loaded, still waiting...");
    }

    NetlinkManager *nm = NetlinkManager::Instance();
    if (nm == nullptr) {
        ALOGE("Unable to create NetlinkManager");
        exit(1);
    };

这里会实例NetlinkManager,用来管理网络,同时会实例NetlinkHandler 用于处理网络消息

/system/netd/server/NetlinkManager.cpp

复制代码
NetlinkHandler *NetlinkManager::setupSocket(int *sock, int netlinkFamily,
    int groups, int format, bool configNflog) {

·········
·········
    NetlinkHandler *handler = new NetlinkHandler(this, *sock, format);
    if (handler->start()) {
        ALOGE("Unable to start NetlinkHandler: %s", strerror(errno));
        close(*sock);
        return nullptr;
    }

    return handler;
}

NetlinkHandler 网络的变化都会在onEvent里处理

system/netd/server/NetlinkHandler.cpp

复制代码
····
····
void NetlinkHandler::onEvent(NetlinkEvent *evt) {
    const char *subsys = evt->getSubsystem();
    if (!subsys) {
        ALOGW("No subsystem found in netlink event");
        return;
    }

    if (!strcmp(subsys, "net")) {
        NetlinkEvent::Action action = evt->getAction();
        const char *iface = evt->findParam("INTERFACE");
        if ((action == NetlinkEvent::Action::kAdd) ||
            (action == NetlinkEvent::Action::kLinkUp) ||
            (action == NetlinkEvent::Action::kLinkDown)) {
            const char *ifIndex = evt->findParam("IFINDEX");
            long ifaceIndex = parseIfIndex(ifIndex);
            if (ifaceIndex) {
                gCtls->trafficCtrl.addInterface(iface, ifaceIndex);
            } else {
                ALOGE("invalid interface index: %s(%s)", iface, ifIndex);
            }
        }

        if (action == NetlinkEvent::Action::kAdd) {
            notifyInterfaceAdded(iface);
        } else if (action == NetlinkEvent::Action::kRemove) {
            notifyInterfaceRemoved(iface);
        } else if (action == NetlinkEvent::Action::kChange) {
            evt->dump();
            notifyInterfaceChanged("nana", true);
        } else if (action == NetlinkEvent::Action::kLinkUp) {
        ALOGW("zzz klinkup");
            notifyInterfaceLinkChanged(iface, true);
        } else if (action == NetlinkEvent::Action::kLinkDown) {
        ALOGW("zzz klinkdown");
            notifyInterfaceLinkChanged(iface, false);
····
····
            }

./system/netd/server/binder/android/net/INetdUnsolicitedEventListener.aidl

复制代码
void NetlinkHandler::notifyInterfaceLinkChanged(const std::string& ifName, bool up) {
    LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceLinkStateChanged, ifName, up);
}

./frameworks/base/services/core/java/com/android/server/NetworkManagementService.java

onInterfaceLinkStateChanged

复制代码
   public void onInterfaceLinkStateChanged(String ifName, boolean up)
 		throws RemoteException {
 		Slog.e(TAG, "zzz onInterfaceLinkStateChanged    " + ifName + up);
 		mDaemonHandler.post(() -> notifyInterfaceLinkStateChanged(ifName, up));
 }

system/netd/server/NetlinkHandler.cpp

复制代码
//LOG_EVENT_FUNC 宏是一个灵活的工具,用于记录网络事件相关的日志,并结合重试机制来决定是否执行日志记录。
//它通过遍历事件监听器,调用指定的函数,并根据执行结果决定是否记录日志,支持动态的日志内容和自动的执行时间计算。
void NetlinkHandler::notifyInterfaceLinkChanged(const std::string& ifName, bool up) {
    LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceLinkStateChanged, ifName, up);
}

system/netd/server/binder/android/net/INetdUnsolicitedEventListener.aidl

复制代码
oneway interface INetdUnsolicitedEventListener {
··········
/**
¦* Notifies that the link state of the specific interface has changed.
¦*
¦* @param ifName the name of the interface whose link state has changed
¦* @param up true for interface link state up, false for link state down
¦*/
void onInterfaceLinkStateChanged(@utf8InCpp String ifName, boolean up);
······

·····
public class NetworkManagementService extends INetworkManagementService.Stub {
·····
private void invokeForAllObservers(NetworkManagementEventCallback eventCallback) {
.........
/**
 ¦* Notify our observers of an interface link state change
 ¦* (typically, an Ethernet cable has been plugged-in or unplugged).
 ¦*/
 private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
 ¦   invokeForAllObservers(o -> o.interfaceLinkStateChanged(iface, up));
 }
.........

frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetTracker.java

复制代码
   private class InterfaceObserver extends BaseNetworkObserver {

        @Override
        public void interfaceLinkStateChanged(String iface, boolean up) {
            if (DBG) {
                Log.i(TAG, "interfaceLinkStateChanged, iface: " + iface + ", up: " + up);
            }
            if(isEthernetInterfaceActive())
                mHandler.post(() -> updateInterfaceState(iface, up));
            mHandler.post(() -> mEthernetNetworkFactoryExt.interfaceLinkStateChanged(iface, up));
        }

        @Override
        public void interfaceAdded(String iface) {
            mHandler.post(() -> maybeTrackInterface(iface));
            mHandler.post(() -> mEthernetNetworkFactoryExt.interfaceAdded(iface));
        }

        @Override
        public void interfaceRemoved(String iface) {
            mHandler.post(() -> removeInterface(iface));
            mHandler.post(() -> mEthernetNetworkFactoryExt.interfaceRemoved(iface));
        }
    }

这段代码定义了一个名为 InterfaceObserver 的类,它继承自 BaseNetworkObserver

并重写了 BaseNetworkObserver 中的三个方法:interfaceLinkStateChanged、interfaceAdded 和 interfaceRemoved。这些方法用于监听网络接口的状态变化,并根据变化做相应的处理。

具体来说,这些方法会在网络接口状态发生变化时调用,并通过 mHandler 将相应的操作(如更新接口状态、添加或移除接口)异步地提交到主线程进行处理

相关推荐
安东尼肉店6 小时前
Android compose屏幕适配终极解决方案
android
2501_916007476 小时前
HTTPS 抓包乱码怎么办?原因剖析、排查步骤与实战工具对策(HTTPS 抓包乱码、gzipbrotli、TLS 解密、iOS 抓包)
android·ios·小程序·https·uni-app·iphone·webview
feiyangqingyun7 小时前
基于Qt和FFmpeg的安卓监控模拟器/手机摄像头模拟成onvif和28181设备
android·qt·ffmpeg
用户20187928316711 小时前
ANR之RenderThread不可中断睡眠state=D
android
煤球王子11 小时前
简单学:Android14中的Bluetooth—PBAP下载
android
小趴菜822711 小时前
安卓接入Max广告源
android
齊家治國平天下11 小时前
Android 14 系统 ANR (Application Not Responding) 深度分析与解决指南
android·anr
ZHANG13HAO11 小时前
Android 13.0 Framework 实现应用通知使用权默认开启的技术指南
android
【ql君】qlexcel11 小时前
Android 安卓RIL介绍
android·安卓·ril
写点啥呢11 小时前
android12解决非CarProperty接口深色模式设置后开机无法保持
android·车机·aosp·深色模式·座舱