linux中网络设备的名字是如何产生的

linux中网络设备的名字是如何产生的

1.从网络设备驱动注册的时候总是会调用接口alloc_etherdev
alloc_etherdev --> alloc_etherdev_mq --> alloc_etherdev_mqs --> alloc_netdev_mqs

/**
 * alloc_etherdev_mqs - Allocates and sets up an Ethernet device
 * @sizeof_priv: Size of additional driver-private structure to be allocated
 *	for this Ethernet device
 * @txqs: The number of TX queues this device has.
 * @rxqs: The number of RX queues this device has.
 *
 * Fill in the fields of the device structure with Ethernet-generic
 * values. Basically does everything except registering the device.
 *
 * Constructs a new net device, complete with a private data area of
 * size (sizeof_priv).  A 32-byte (not bit) alignment is enforced for
 * this private data area.
 */
 
struct net_device *alloc_etherdev_mqs(int sizeof_priv, unsigned int txqs,
				      unsigned int rxqs)
{
	return alloc_netdev_mqs(sizeof_priv, "eth%d", NET_NAME_UNKNOWN,
				ether_setup, txqs, rxqs);
}

可以看到经过这个函数的时候name还是eth%d。

ret = dev_get_valid_name(net, dev, dev->name);
 
int dev_get_valid_name(struct net *net, struct net_device *dev,
               const char *name)
{
    BUG_ON(!net);
 
    if (!dev_valid_name(name))
        return -EINVAL;

    if (strchr(name, '%'))
        return dev_alloc_name_ns(net, dev, name);
    else if (__dev_get_by_name(net, name))
        return -EEXIST;
    else if (dev->name != name)
        strlcpy(dev->name, name, IFNAMSIZ);
 
    return 0;
}
 
static int dev_alloc_name_ns(struct net *net,
                 struct net_device *dev,
                 const char *name)
{
    char buf[IFNAMSIZ];
    int ret;
      
    ret = __dev_alloc_name(net, name, buf);
    if (ret >= 0)
        strlcpy(dev->name, buf, IFNAMSIZ);
    return ret;
}
 

static int __dev_alloc_name(struct net *net, const char *name, char *buf)
{
    int i = 0;
    const char *p;
   
    const int max_netdevices = 8*PAGE_SIZE;
    unsigned long *inuse;
    struct net_device *d;
 
    p = strnchr(name, IFNAMSIZ-1, '%');
    if (p) {
        /*
         * Verify the string as this thing may have come from
         * the user.  There must be either one "%d" and no other "%"
         * characters.
         */
         
        if (p[1] != 'd' || strchr(p + 2, '%'))
            return -EINVAL;
 
             
        /* Use one page as a bit array of possible slots */
        inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
        if (!inuse)
            return -ENOMEM;
            
        for_each_netdev(net, d) {
            if (!sscanf(d->name, name, &i))  
                                                       
                continue;
 
            if (i < 0 || i >= max_netdevices)
                continue;
 
            /*  avoid cases where sscanf is not exact inverse of printf */
                     
            snprintf(buf, IFNAMSIZ, name, i);   
            if (!strncmp(buf, d->name, IFNAMSIZ))   
                set_bit(i, inuse);
        }
              
 
        i = find_first_zero_bit(inuse, max_netdevices);
        free_page((unsigned long) inuse);
    }

    if (buf != name)
        snprintf(buf, IFNAMSIZ, name, i);
 
    if (!__dev_get_by_name(net, buf))
        return i;
 
    /* It is possible to run out of possible slots
     * when the name is long and there isn't enough space left
     * for the digits, or if all bits are used.
     */
    return -ENFILE;
}
//addr为内存区的起始地址,size为要查找的最大长度,返回第一个位为0的位号
int find_first_zero_bit (void * addr, unsigned size)

eth%d中的实际值是eth0,eth1…主要取决于 i = find_first_zero_bit(inuse, max_netdevices); 这个接口的返回值。如果之前没有注册任何网络设备,那么这个i一般就为0,那么name就会变成 eth0 。

参考
[1]:https://blog.csdn.net/xiruanliuwei/article/details/78765255

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值