一 网络设备利用net_device结构体来定义,该结构体定义在头函数:kernel/linux/netdevice.h,该结构体的使用函数定义在函数:kernel/net/core/dev.c中:
该结构体是有函数:alloc_netdev_mps进行分配。
二 alloc_netdev_mps函数解析:
1 函数原型:struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
void (*setup)(struct net_device *),
unsigned int txqs, unsigned int rxqs)
入参的含义:
* @sizeof_priv: size of private data to allocate space for
* @name: device name format string
* @setup: callback to initialize device
* @txqs: the number of TX subqueues to allocate
* @rxqs: the number of RX subqueues to allocate
2 使用实例:
a 环回网络的申请:
dev = alloc_netdev(0, "lo", loopback_setup); /* alloc zero size loop net and name is lo*/
#define alloc_netdev(sizeof_priv, name, setup) \
alloc_netdev_mqs(sizeof_priv, name, setup, 1, 1)
b 有线网络的申请:
/* Allocate and initialise a struct net_device and struct efx_nic */
net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
EFX_MAX_RX_QUEUES);
struct net_device *alloc_etherdev_mqs(int sizeof_priv, unsigned int txqs,
unsigned int rxqs)
{
return alloc_netdev_mqs(sizeof_priv, "eth%d", ether_setup, txqs, rxqs);
}