Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Linux 網絡子系統中的通知鏈:netdev_chain 的注册與使用

編輯:關於 Android 開發

為什麼網絡代碼需要通知鏈

Linux 網絡內核代碼使用通知鏈(Notification Chains),讓相關子系統能對自己感興趣的事件作出反應——例如網卡注册、狀態變化、MTU 調整、IP 地址變動等。本文從數據結構講起,再看內核中的實際用法。

核心數據結構

struct notifier_block {
    int (*notifier_call)(struct notifier_block *, unsigned long, void *);
    struct notifier_block *next;
    int priority;
};

notifier_call 是回調函數指針;next 串接下一個節點;priority 決定節點在鏈上的位置,同一條鏈按優先級排列。默認初始化为 0,相當於按注册先後順序執行。內核中這類鏈通常命名為 xxx_chain 或 xxx_notifier_chain。

注册節點

static int notifier_chain_register(struct notifier_block **nl,
                                   struct notifier_block *n)
{
    while ((*nl) != NULL) {
        if (n->priority > (*nl)->priority)
            break;
        nl = &((*nl)->next);
    }
    n->next = *nl;
    rcu_assign_pointer(*nl, n);
    return 0;
}

注銷節點

static int notifier_chain_unregister(struct notifier_block **nl,
                                     struct notifier_block *n)
{
    while ((*nl) != NULL) {
        if ((*nl) == n) {
            rcu_assign_pointer(*nl, n->next);
            return 0;
        }
        nl = &((*nl)->next);
    }
    return -ENOENT;
}

觸發鏈上的回調

需要通知事件發生時調用 notifier_call_chain()。注意:回調函數是在調用 notifier_call_chain() 的上下文中同步執行的,這意味著回調裡不能睡眠(若鏈是原子類型)。

static int __kprobes notifier_call_chain(struct notifier_block **nl,
        unsigned long val, void *v,
        int nr_to_call, int *nr_calls)
{
    int ret = NOTIFY_DONE;
    struct notifier_block *nb, *next_nb;

    nb = rcu_dereference(*nl);
    while (nb && nr_to_call) {
        next_nb = rcu_dereference(nb->next);
        ret = nb->notifier_call(nb, val, v);

        if (nr_calls)
            (*nr_calls)++;

        if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
            break;
        nb = next_nb;
        nr_to_call--;
    }
    return ret;
}

參數含義:nl 是鏈的名稱,val 表示事件類型(如 NETDEV_REGISTER),v 指向回調所需的私有數據——不同鏈的參數類型不同,例如通知網卡注册時 v 指向 net_device 結構;nr_to_call 限定最多通知幾個,-1 表示整條鏈;nr_calls 非空時返回實際通知數量。

回調可能的返回值:NOTIFY_DONE 表示不關心此事件;NOTIFY_OK 執行成功;NOTIFY_BAD 執行有錯;NOTIFY_STOP 停止後續回調;NOTIFY_STOP_MASK 是停止判斷掩碼。具體定義見 include/linux/notifier.h。notifier_call_chain() 返回最後一個被調回調的返回值。

內核網絡中的兩條主要通知鏈

inetaddr_chain——IPv4 地址變動時的通知鏈
netdev_chain——網絡設備狀態變動時的通知鏈

網絡代碼通常會為通知鏈包裝一層更友好的接口。例如 netdev_chain 的注册由 register_netdevice_notifier() 完成:

int register_netdevice_notifier(struct notifier_block *nb)
{
    struct net_device *dev;
    struct net_device *last;
    struct net *net;
    int err;

    rtnl_lock();
    err = raw_notifier_chain_register(&netdev_chain, nb);
    if (err)
        goto unlock;
    if (dev_boot_phase)
        goto unlock;

    for_each_net(net) {
        for_each_netdev(net, dev) {
            err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
            err = notifier_to_errno(err);
            if (err)
                goto rollback;

            if (!(dev->flags & IFF_UP))
                continue;

            nb->notifier_call(nb, NETDEV_UP, dev);
        }
    }

unlock:
    rtnl_unlock();
    return err;

rollback:
    last = dev;
    for_each_net(net) {
        for_each_netdev(net, dev) {
            if (dev == last)
                break;
            if (dev->flags & IFF_UP) {
                nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
                nb->notifier_call(nb, NETDEV_DOWN, dev);
            }
            nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
        }
    }
    raw_notifier_chain_unregister(&netdev_chain, nb);
    goto unlock;
}

這個函數看似複雜,實際只做兩件事:一是把 nb 注册到 netdev_chain;二是把系統中已注册或已激活的所有網絡設備事件,重新補發給新加入的回調,確保它能拿到一份完整的設備狀態快照。若中途出錯,rollback 分支會把已通知的事件反向補發一遍並撤銷注册,保證狀態一致。

一個真實例子:路由子系統

路由子系統初始化時,ip_fib_init() 會注册一個回調到 netdev_chain:

void __init ip_fib_init(void)
{
    ...
    register_netdevice_notifier(&fib_netdev_notifier);
    ...
}

回調的定義:

static struct notifier_block fib_netdev_notifier = {
    .notifier_call = fib_netdev_event,
};

這裡 priority 默認為 0,next 由注册時設定。

再看 fib_netdev_event() 的處理邏輯:

static int fib_netdev_event(struct notifier_block *this,
                            unsigned long event, void *ptr)
{
    struct net_device *dev = ptr;
    struct in_device *in_dev = __in_dev_get_rtnl(dev);

    if (event == NETDEV_UNREGISTER) {
        fib_disable_ip(dev, 2);
        return NOTIFY_DONE;
    }

    if (!in_dev)
        return NOTIFY_DONE;

    switch (event) {
    case NETDEV_UP:
        for_ifa(in_dev) {
            fib_add_ifaddr(ifa);
        } endfor_ifa(in_dev);
        rt_cache_flush(-1);
        break;
    case NETDEV_DOWN:
        fib_disable_ip(dev, 0);
        break;
    case NETDEV_CHANGEMTU:
    case NETDEV_CHANGE:
        rt_cache_flush(0);
        break;
    }
    return NOTIFY_DONE;
}

可以清楚看到通知鏈的價值:網卡上線時把地址加入 FIB,下線時清理相關表項,MTU 或狀態變化時刷新路由緩存。路由子系統完全不需要去輪詢設備狀態,一切由事件驅動——這正是通知鏈機制存在的意義。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved