linux内核学习之网络篇——网络物理层的装置初步

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.
*/
char name[IFNAMSIZ];

/*
* I/O specific fields
* FIXME: Merge these and struct ifmap into one
*/
unsigned long mem_end; /* shared mem end */
unsigned long mem_start; /* shared mem start */
unsigned long base_addr; /* device I/O address */
unsigned int irq; /* device IRQ number */

/*
* Some hardware also needs these fields, but they are not
* part of the usual set specified in Space.c.
*/

unsigned char if_port; /* Selectable AUI, TP,..*/
unsigned char dma; /* DMA channel */

unsigned long state;

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 */
int ifindex;
int iflink;

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 <net/iw_handler.h> 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 long trans_start; /* Time (in jiffies) of last Tx */
unsigned long last_rx; /* Time of last Rx */

unsigned short flags; /* interface flags (a la BSD) */
unsigned short gflags;
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.
*/

unsigned mtu; /* interface MTU value */
unsigned short type; /* interface hardware type */
unsigned short hard_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 char broadcast[MAX_ADDR_LEN]; /* hw bcast add */
unsigned char dev_addr[MAX_ADDR_LEN]; /* hw address */
un

linux内核学习之网络篇——网络物理层的装置初步

相关文章:

你感兴趣的文章:

标签云: