alloc_netdev()函数生成一个net_device结构体,对其成员赋值并返回该结构体的指针。第一个参数是设备私有成员的大小,第二个参数为设备名,第三个参数为net_device的setup()函数指针。setup()函数接收的参数为struct net_device指针,用于预置net_device成员的值。
linux-2.6.22/net/core/dev.c
----------------------------------------
struct net_device *alloc_netdev(int sizeof_priv, const char *name,
void (*setup)(struct net_device *))
{
void *p;
struct net_device *dev;
int alloc_size;
BUG_ON(strlen(name) >= sizeof(dev->name));
/* ensure 32-byte alignment of both the device and private area */
alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
p = kzalloc(alloc_size, GFP_KERNEL);
if (!p) {
printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
return NULL;
}
dev = (struct net_device *)
(((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
dev->padded = (char *)dev - (char *)p;
if (sizeof_priv)
dev->priv = netdev_priv(dev);
dev->get_stats = internal_stats;
setup(dev);
strcpy(dev->name, name);
return dev;
}
linux-2.6.22/net/core/dev.c
----------------------------------------
struct net_device *alloc_netdev(int sizeof_priv, const char *name,
void (*setup)(struct net_device *))
{
void *p;
struct net_device *dev;
int alloc_size;
BUG_ON(strlen(name) >= sizeof(dev->name));
/* ensure 32-byte alignment of both the device and private area */
alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
p = kzalloc(alloc_size, GFP_KERNEL);
if (!p) {
printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
return NULL;
}
dev = (struct net_device *)
(((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
dev->padded = (char *)dev - (char *)p;
if (sizeof_priv)
dev->priv = netdev_priv(dev);
dev->get_stats = internal_stats;
setup(dev);
strcpy(dev->name, name);
return dev;
}
alloc_etherdev()是alloc_netdev()针对以太网的"快捷"函数 linux-2.6.22/net/ethernet/eth.c ---------------------------------------- struct net_device *alloc_etherdev(int sizeof_priv) { ----returnalloc_netdev(sizeof_priv, "eth%d", ether_setup); } net_device +--------------+ | | |--------------| | base_addr | |--------------| | | | | |--------------| | priv | ---+ |--------------| | | | | | | | +--------------+ <--+ | | | | sizeof_priv | | +--------------+ net_device->base_addr 网络寄存器的基地址(物理地址) |