linux内核学习之网络篇——网络物理层的设备初步
网卡是是网络中最基本的硬件之一。
在内核中,每个网络设备都一个一个net_device 结构体实例,在进行初始化完成之后,会将该结构注册到内核中。然能内核知道该结构的存在。
这会创建一个sysfs项,这,关联到该设备的对应目录中。该目录为
[root@localhost ~]# ls -l /sys/class/net
总计 0
drwxr-xr-x 3 root root 0 10-08 00:17 eth0
drwxr-xr-x 3 root root 0 10-08 00:17 lo
drwxr-xr-x 4 root root 0 10-08 00:17 peth0
drwxr-xr-x 3 root root 0 10-08 00:17 sit0
drwxr-xr-x 3 root root 0 10-08 00:17 veth1
drwxr-xr-x 3 root root 0 10-08 00:17 veth2
drwxr-xr-x 3 root root 0 10-08 00:17 veth3
drwxr-xr-x 4 root root 0 10-08 00:17 vif0.0
drwxr-xr-x 3 root root 0 10-08 00:17 vif0.1
drwxr-xr-x 3 root root 0 10-08 00:17 vif0.2
drwxr-xr-x 3 root root 0 10-08 00:17 vif0.3
drwxr-xr-x 5 root root 0 10-08 00:17 xenbr0
可以看到 我虚拟机上的网络设备有以上的设备。eth0 是第一个网卡。其他的我也不知道干嘛的。
该设备不是全局的。因此也是在一个命名空间的。
所有网络设备都保存在一个单链表中。表头为dev_base.
按设备名散列。
按接口索引散列
下面我就不得不晓得linux中 最大的一个结构体数据结构。几百行代码
如下代码
/*
*The DEVICE structure.
*Actually, this whole structure is a big mistake. It mixes I/O
*data with strictly "high-level" data, and it has to know about
*almost every data structure used in the INET module.
*
*FIXME: cleanup struct net_device such that network protocol info
*moves out.
*/
struct net_device
{
/*
* This is the first field of the "visible" part of this structure
* (i.e. as seen by users in the "Space.c" file). It is the name
* the interface.
*/
charname[IFNAMSIZ];
/*
*I/O specific fields
*FIXME: Merge these and struct ifmap into one
*/
unsigned longmem_end;/* shared mem end*/
unsigned longmem_start;/* shared mem start*/
unsigned longbase_addr;/* device I/O address*/
unsigned intirq;/* device IRQ number*/
/*
*Some hardware also needs these fields, but they are not
*part of the usual set specified in Space.c.
*/
unsigned charif_port;/* Selectable AUI, TP,..*/
unsigned chardma;/* DMA channel*/
unsigned longstate;
struct net_device*next;
/* The device initialization function. Called only once. */
int(*init)(struct net_device *dev);
/* ------- Fields preinitialized in Space.c finish here ------- */
struct net_device*next_sched;
/* Interface index. Unique device identifier*/
intifindex;
intiflink;
struct net_device_stats* (*get_stats)(struct net_device *dev);
struct iw_statistics*(*get_wireless_stats)(struct net_device *dev);
/* List of functions to handle Wireless Extensions (instead of ioctl).
* See for details. Jean II */
struct iw_handler_def *wireless_handlers;
struct ethtool_ops *ethtool_ops;
/*
* This marks the end of the "visible" part of the structure. All
* fields hereafter are internal to the system, and may change at
* will (read: may be cleaned up at will).
*/
/* These may be needed for future network-power-down code. */
unsigned longtrans_start;/* Time (in jiffies) of last Tx*/
unsigned longlast_rx;/* Time of last Rx*/
unsigned shortflags;/* interface flags (a la BSD)*/
unsigned shortgflags;
unsigned short priv_flags; /* Like 'flags' but invisible to userspace. */
unsigned short unused_alignment_fixer; /* Because we need priv_flags,
* and we want to be 32-bit aligned.
*/
unsignedmtu;/* interface MTU value*/
unsigned shorttype;/* interface hardware type*/
unsigned shorthard_header_len;/* hardware hdr length*/
void*priv;/* pointer to private data*/
struct net_device*master; /* Pointer to master device of a group,
* which this device is member of.
*/
/* Interface address info. */
unsigned charbroadcast[MAX_ADDR_LEN];/* hw bcast add*/
unsigned chardev_addr[MAX_ADDR_LEN];/* hw address*/
un