网络协议栈与网卡驱动对接简单描述

摘要:本文简单描述了网络协议栈与驱动调用方式。

数据从协议栈往驱动传输

网络上层数据(TCP、UDP)经过路由等处理调用流程:
1、dev_queue_xmit(net/core/dev.c)
2、netdev_start_xmit&__netdev_start_xmit(include/linux/netdevice.h)
3、net_device_ops->ndo_start_xmit(include/linux/netdevice.h)

ndo_start_xmit为各厂商驱动部分实现。
MTK平台为:

static const struct net_device_ops wlan_netdev_ops = {
    .ndo_open = wlanOpen,
    .ndo_stop = wlanStop,
    .ndo_set_rx_mode = wlanSetMulticastList,
    .ndo_get_stats = wlanGetStats,
    .ndo_do_ioctl = wlanDoIOCTL,
    .ndo_start_xmit = wlanHardStartXmit,
    .ndo_init = wlanInit,
    .ndo_uninit = wlanUninit,
    .ndo_select_queue = wlanSelectQueue,
};

MTK网络相关驱动位于:drivers/misc/mediatek/connectivity/wlan/,它使用套片方式,在connectivity目录下整合了蓝牙、GPRS等多种驱动。

博通驱动则为:

static const struct net_device_ops ieee80211_dataif_ops = {
    .ndo_open        = ieee80211_open,
    .ndo_stop        = ieee80211_stop,
    .ndo_uninit        = ieee80211_uninit,
    .ndo_start_xmit        = ieee80211_subif_start_xmit,
    .ndo_set_rx_mode    = ieee80211_set_multicast_list,
    .ndo_change_mtu     = ieee80211_change_mtu,
    .ndo_set_mac_address     = ieee80211_change_mac,
    .ndo_select_queue    = ieee80211_netdev_select_queue,
};

最终调用ieee80211_ops->brcms_ops_tx(drivers/net/wireless/brcm80211/brcmsmac/mac80211_if.c)
(多种模式下接口操作会不一样)

数据从驱动往协议栈传输

驱动只需要调用netif_rx_ni->do_soft_irq(net/core/Dev.c)触发软中断,接下来由协议栈处理。