前面文章讲了Linux系统的ethtool框架的一些东西,是从用户空间可以直观认识到的地方入手。同样,本文从Linux系统绝大部分人都熟悉的“ifconfig eth0 up”命令来跟踪一下此命令在内核中的发生了什么事情。由于ifconfig启动(up)和禁止(down)网络设备很相似,就放到一起讲了。
首先从ifconfig的源码入手,我下载的源码地址是http://www.tazenda.demon.co.uk/phil/net-tools/。这个网站上还有大量很有用的工具的源码,源码分布符合Linux的系统目录,有兴趣的可以去看看。
在我们输入up或down时,对应的代码如下:
- main()
- {
- if (!strcmp(*spp, "up")) {
- goterr |= set_flag(ifr.ifr_name, (IFF_UP | IFF_RUNNING));
- spp++;
- continue;
- }
- if (!strcmp(*spp, "down")) {
- goterr |= clr_flag(ifr.ifr_name, IFF_UP);
- spp++;
- continue;
- }
- }
很简单,就是根据用户的输入来标志IFF_UP参考。当up时,使用set_flag置位IFF_UP和IFF_RUNNING,当down时,使用clr_flag清除IFF_UP。Linux的这种思想值得学习,其实对于内核来讲,真的就是通过IFF_UP标志来判断网卡的使能和禁止的。
来看设置标志的set_flag函数:
- static int set_flag(char *ifname, short flag)
- {
- struct ifreq ifr;
- safe_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
- if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
- fprintf(stderr, _("%s: unknown interface: %s "),
- ifname, strerror(errno));
- return (-1);
- }
- safe_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
- ifr.ifr_flags |= flag;
- if (ioctl(skfd, SIOCSIFFLAGS, &ifr) < 0) {
- perror("SIOCSIFFLAGS");
- return -1;
- }
- return (0);
- }
以及清除标志的clr_flag函数:
- static int clr_flag(char *ifname, short flag)
- {
- struct ifreq ifr;
- int fd;
- if (strchr(ifname, ':')) {
- /* This is a v4 alias interface. Downing it via a socket for
- another AF may have bad consequences. */
- fd = get_socket_for_af(AF_INET);
- if (fd < 0) {
- fprintf(stderr, _("No support for INET on this system. "));
- return -1;
- }
- } else
- fd = skfd;
- safe_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
- if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
- fprintf(stderr, _("%s: unknown interface: %s "),
- ifname, strerror(errno));
- return -1;
- }
- safe_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
- ifr.ifr_flags &= ~flag;
- if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
- perror("SIOCSIFFLAGS");
- return -1;
- }
- return (0);
- }
观察这两个函数,最后都是使用SIOCSIFFLAGS命令和内核交互。我们找到这个命令的使用地方,它位于net/core/dev.c文件,如下:
- int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
- {
- case SIOCSIFFLAGS:
- case SIOCSIFMETRIC:
- case SIOCSIFMTU:
- case SIOCSIFMAP:
- case SIOCSIFHWADDR:
- case SIOCSIFSLAVE:
- case SIOCADDMULTI:
- case SIOCDELMULTI:
- case SIOCSIFHWBROADCAST:
- case SIOCSIFTXQLEN:
- case SIOCSMIIREG:
- case SIOCBONDENSLAVE:
- case SIOCBONDRELEASE:
- case SIOCBONDSETHWADDR:
- case SIOCBONDCHANGEACTIVE:
- case SIOCBRADDIF:
- case SIOCBRDELIF:
- case SIOCSHWTSTAMP:
- if (!capable(CAP_NET_ADMIN))
- return -EPERM;
- /* fall through */
- case SIOCBONDSLAVEINFOQUERY:
- case SIOCBONDINFOQUERY:
- dev_load(net, ifr.ifr_name);
- rtnl_lock();
- ret = dev_ifsioc(net, &ifr, cmd);
- rtnl_unlock();
- return ret;
- }
SIOCSIFFLAGS会调用到dev_ifsioc函数:
- static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
- {
- switch (cmd) {
- case SIOCSIFFLAGS: /* Set interface flags */
- return dev_change_flags(dev, ifr->ifr_flags);
- }
继续跟进dev_change_flags函数:
- int dev_change_flags(struct net_device *dev, unsigned flags)
- {
- int ret, changes;
- int old_flags = dev->flags;
- ret = __dev_change_flags(dev, flags); // 打开设备
- if (ret < 0)
- return ret;
- changes = old_flags ^ dev->flags;
- if (changes)
- rtmsg_ifinfo(RTM_NEWLINK, dev, changes); // 暂未了解
- __dev_notify_flags(dev, old_flags); // 向通道链netdev_chain发出通知
- return ret;
- }
真正干活的是__dev_change_flags函数:
- int __dev_change_flags(struct net_device *dev, unsigned int flags)
- {
- if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
- ret = ((old_flags & IFF_UP) ? __dev_close : __dev_open)(dev);
- }
根据标志选择打开设备__dev_open或关闭__dev_close。在同一文件还有dev_open或dev_close,我发现它们使用了EXPORT_SYMBOL导出,供给其它模块使用,但在这里,是使用了__dev_XX函数的。
打开函数如下:
- static int __dev_open(struct net_device *dev)
- {
- const struct net_device_ops *ops = dev->netdev_ops;
- int ret;
- ASSERT_RTNL();
- /*
- * Is it even present?
- */
- if (!netif_device_present(dev))
- return -ENODEV;
- ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
- ret = notifier_to_errno(ret);
- if (ret)
- return ret;
- /*
- * Call device private open method
- */
- set_bit(__LINK_STATE_START, &dev->state);
- if (ops->ndo_validate_addr)
- ret = ops->ndo_validate_addr(dev);
- if (!ret && ops->ndo_open)
- ret = ops->ndo_open(dev);
- /*
- * If it went open OK then:
- */
- if (ret)
- clear_bit(__LINK_STATE_START, &dev->state);
- return ret;
- }
在真正调用具体驱动的接口前,先发NETDEV_PRE_UP给到通知链,再置__LINK_STATE_START,然后才调用驱动的ndo_open接口,最后需要清除__LINK_STATE_START标志。
关闭函数如下:
- static int __dev_close(struct net_device *dev)
- {
- const struct net_device_ops *ops = dev->netdev_ops;
- ASSERT_RTNL();
- might_sleep();
- /*
- * Tell people we are going down, so that they can
- * prepare to death, when device is still operating.
- */
- call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
- clear_bit(__LINK_STATE_START, &dev->state);
- /* Synchronize to scheduled poll. We cannot touch poll list,
- * it can be even on different cpu. So just clear netif_running().
- *
- * dev->stop() will invoke napi_disable() on all of it's
- * napi_struct instances on this device.
- */
- smp_mb__after_clear_bit(); /* Commit netif_running(). */
- dev_deactivate(dev);
- /*
- * Call the device specific close. This cannot fail.
- * Only if device is UP
- *
- * We allow it to be called even after a DETACH hot-plug
- * event.
- */
- if (ops->ndo_stop)
- ops->ndo_stop(dev);
- /*
- * Device is now down.
- */
- dev->flags &= ~IFF_UP;
- return 0;
- }
在真正调用具体驱动的接口前,先发NETDEV_GOING_DOWN给到通知链,表示网络设备准备挂了,再清除__LINK_STATE_START标志,然后才调用驱动的ndo_stop接口,最后需要清除IFF_UP。
经过内核的层层结构,条条框框,终于到了具体的驱动了,上面的函数使用的接口实际上是net_device_ops结构体的函数指针,还是以igb驱动为例,赋值如下:
- static const struct net_device_ops igb_netdev_ops = {
- .ndo_open = igb_open,
- .ndo_stop = igb_close,
- .ndo_start_xmit = igb_xmit_frame_adv,
- .ndo_get_stats64 = igb_get_stats64,
- .ndo_set_rx_mode = igb_set_rx_mode,
- .ndo_set_multicast_list = igb_set_rx_mode,
- .ndo_set_mac_address = igb_set_mac,
- .ndo_change_mtu = igb_change_mtu,
- .ndo_do_ioctl = igb_ioctl,
- .ndo_tx_timeout = igb_tx_timeout,
- .ndo_validate_addr = eth_validate_addr,
- .ndo_vlan_rx_register = igb_vlan_rx_register,
- .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
- .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
- .ndo_set_vf_mac = igb_ndo_set_vf_mac,
- .ndo_set_vf_vlan = igb_ndo_set_vf_vlan,
- .ndo_set_vf_tx_rate = igb_ndo_set_vf_bw,
- .ndo_get_vf_config = igb_ndo_get_vf_config,
- #ifdef CONFIG_NET_POLL_CONTROLLER
- .ndo_poll_controller = igb_netpoll,
- #endif
- };
我们看到最开始的2个函数就是打开和关闭。在igb_probe函数对igb_netdev_ops进行赋值:
- netdev->netdev_ops = &igb_netdev_ops;
至此,整个过程分析完毕。
文中涉及到通知链,网络设备通知链netdev_chain,这个还没研究过,这里简单列一下:
- // 声明通知链表
- static RAW_NOTIFIER_HEAD(netdev_chain);
- //注册
- int register_netdevice_notifier(struct notifier_block *nb)
- {
- raw_notifier_chain_register(&netdev_chain, nb);
- }
- 注销:
- int unregister_netdevice_notifier(struct notifier_block *nb)
- {
- int err;
- err = raw_notifier_chain_unregister(&netdev_chain, nb);
- }
31号的PS:
写完后想一想,感觉没到分析彻底,因为到具体的驱动后干了些什么还没跟踪,于是又花了点时间跟踪一下。我跟踪的是ti的网卡驱动,主要实现代码在cpsw.c文件,在http://lxr.oss.org.cn/source/drivers/net/ethernet/ti/?v=3.17可以找到。下面列出启动网卡时的过程的重要函数调用:
- > cpsw_ndo_open
- > cpsw_slave_open
- > phy_connect (传递cpsw_adjust_link)
- > phy_connect_direct (PHY_READY)
- > phy_prepare_link (赋值cpsw_adjust_link为adjust_link)
- > phy_start_machine
- > phy_start (PHY_READY变成PHY_UP)
phy_start之后进入了phy驱动重要的状态判断函数phy_state_machine,phy驱动有一个工作队列就是调用这个函数的,这个函数判断了网络各种状态:PHY_DOWN、PHY_UPPHY_AN、PHY_NOLINK,等,并做出对应的动作。我们设置了PHY_UP,则函数过程如下:
- phy_state_machine
- > phy_start_aneg
- > config_aneg
- > genphy_config_aneg(实际上是这个函数,由phy驱动赋值的)
到了genphy_config_aneg这个函数,就是直接和phy芯片打交道了,读phy寄存器、写phy寄存器。默认情况下会进行自动协商(其实就是写phy的第0个寄存器),当然,如果不是,即phy_device成员变量autoneg是AUTONEG_DISABLE,则会强制设置指定的速率、双工模式。
至此,就不再继续分析、跟踪了。
原文:https://blog.csdn.net/subfate/article/details/44755679