在linux的网络设备里,其中一个最关键的结构体应该要算net_device了,它由对应的网络设备驱动进行创建和初始化,服务于内核网络子系统。
1. struct net_device 注释分析
struct net_device这个结构体比较大,在了解它之前,我们先看一下它的注释:
1433 /**
1434 * struct net_device - The DEVICE structure.
1435 * Actually, this whole structure is a big mistake. It mixes I/O //这个结构体的设计是一个很大的失误,它并没有对IO数据和高级别的数据进行区分,也就是说这个结构
1436 * data with strictly "high-level" data, and it has to know about //体并没有对数据的来源是普通内存还是高速缓存进行辨别,因此在INET模型里面,它不得不处理各种
1437 * almost every data structure used in the INET module. //不同的数据类型
1438 *
1439 * @name: This is the first field of the "visible" part of this structure //它代表一个接口的名字,在设备注册的时候,我们可以指定接口名字,如果没指定,他会自动申请
1440 * (i.e. as seen by users in the "Space.c" file). It is the name //一个自加1的名字,比如eth0,eth1,eth2...
1441 * of the interface.
1442 *
1443 * @name_hlist: Device name hash chain, please keep it close to name[] //以名字为索引的哈希表
1444 * @ifalias: SNMP alias // snmp的别名
1445 * @mem_end: Shared memory end //每一个设备都会分配一块内存区域,start和end指定了这块区域
1446 * @mem_start: Shared memory start
1447 * @base_addr: Device I/O address //网络硬件设备的基地址,内存管理系统将每一个外部设备都看作一块连续的地址,然后将它与内存中的一块地址进行映射,这样操作内存地址就相当于操作这块网络硬件设备的地址,而这里的基地址就是这个网络硬件设备的起始地址。他会在probe的时候初始化
1448 * @irq: Device IRQ number //该设备对应的中断号
1449 *
1450 * @carrier_changes: Stats to monitor carrier on<->off transitions
1451 *
1452 * @state: Generic network queuing layer state, see netdev_state_t //表示设备的状态,它很重要
1453 * @dev_list: The global list of network devices //所有net_device对象组成的一个链表,可以说系统中所有的网络设备都可以通过它查到
1454 * @napi_list: List entry used for polling NAPI devices //如果该支持NAPI,会将它挂到这个链表上,CPU就可以更快的找到NAPI poll的设备
1455 * @unreg_list: List entry when we are unregistering the //正在被卸载的设备会加到这个链表
1456 * device; see the function unregister_netdev
1457 * @close_list: List entry used when we are closing the device //正在被关闭的设备会加到这个链表
1458 * @ptype_all: Device-specific packet handlers for all protocols //某些特定协议的处理函数会挂接在这里,但是未必是需要的
1459 * @ptype_specific: Device-specific, protocol-specific packet handlers
1460 *
1461 * @adj_list: Directly linked devices, like slaves for bonding
1462 * @features: Currently active device features //用来标识接口的各种能力和特性
1463 * @hw_features: User-changeable features //一些硬件相关的特性,这些是可以在用户空间修改的
1464 *
1465 * @wanted_features: User-requested features
1466 * @vlan_features: Mask of features inheritable by VLAN devices //是否支持vlan功能
1467 *
1468 * @hw_enc_features: Mask of features inherited by encapsulating devices //是否支持硬件封装功能
1469 * This field indicates what encapsulation
1470 * offloads the hardware is capable of doing,
1471 * and drivers will need to set them appropriately.
1472 *
1473 * @mpls_features: Mask of features inheritable by MPLS
1474 *
1475 * @ifindex: interface index //内核指定的索引号,比如第一个,第二个设备等等
1476 * @group: The group the device belongs to //这个设备属于哪个组
1477 *
1478 * @stats: Statistics struct, which was left as a legacy, use //一些接口的信息,用于提供给旧接口的用户空间获取
1479 * rtnl_link_stats64 instead
1480 *
1481 * @rx_dropped: Dropped packets by core network, //被内核丢掉的包,注意不是被driver丢的
1482 * do not use this in drivers
1483 * @tx_dropped: Dropped packets by core network,
1484 * do not use this in drivers
1485 * @rx_nohandler: nohandler dropped packets by core network on
1486 * inactive devices, do not use this in drivers
1487 *
1488 * @wireless_handlers: List of functions to handle Wireless Extensions, //无线子系统的一些接口
1489 * instead of ioctl,
1490 * see <net/iw_handler.h> for details.
1491 * @wireless_data: Instance data managed by the core of wireless extensions
1492 *
1493 * @netdev_ops: Includes several pointers to callbacks, //很重要!操作网络设备的函数都聚集在这里了,在网络初始化的时候被初始化,具体支持哪些操作函数,
1494 * if one wants to override the ndo_*() functions //请看这个函数struct net_device_ops()
1495 * @ethtool_ops: Management operations //ethtool的操作接口
1496 * @ndisc_ops: Includes callbacks for different IPv6 neighbour
1497 * discovery handling. Necessary for e.g. 6LoWPAN.
1498 * @header_ops: Includes callbacks for creating,parsing,caching,etc //对L2头部处理的函数
1499 * of Layer 2 headers.
1500 *
1501 * @flags: Interface flags (a la BSD) //标识接口的状态,比如UP/down等,可以通过用户空间修改
1502 * @priv_flags: Like 'flags' but invisible to userspace, //和flags类似,但是用户空间不能修改
1503 * see if.h for the definitions
1504 * @gflags: Global flags ( kept as legacy ) //全局标识,和flags配合使用
1505 * @padded: How much padding added by alloc_netdev() //对齐时使用的字节数,在申请net_device的时候,需要进行对齐,它表示填充的字节数
1506 * @operstate: RFC2863 operstate
1507 * @link_mode: Mapping policy to operstate
1508 * @if_port: Selectable AUI, TP, ... 目前较少用,对于支持多介质的网络设备时,用来指定哪种设备的接口
1509 * @dma: DMA channel //为该设备分配的DMA通道,如果支持的话,目前来说应该都支持了
1510 * @mtu: Interface MTU value //这个不用说了,一般1500
1511 * @min_mtu: Interface Minimum MTU value
1512 * @max_mtu: Interface Maximum MTU value
1513 * @type: Interface hardware type //接口的硬件类型,目前来说主要都是以太网
1514 * @hard_header_len: Maximum hardware header length.
1515 * @min_header_len: Minimum hardware header length
1516 *
1517 * @needed_headroom: Extra headroom the hardware may need, but not in all //需要头部空间吗
1518 * &nbs