在Linux系统中,网络设备都被抽象为struct net_device结构体。它是网络设备硬件与上层协议之间联系的接口,了解它对编写网络驱动程序非常有益,所以本文将着手简要介绍linux-2.6.38.8/include/linux/netdevice.h文件中struct net_device结构体的所有成员(没有按照它们定义的顺序)。
1、网络设备相关信息
(1)、设备名
[cpp] view plain copy
char name[IFNAMSIZ];
char *ifalias; //用于SNMP协议
在Linux系统中,每个网络设备都有一个唯一的设备名(如eth0,字母部分代表网络设备的类型,数字部分代表此类网络设备的数量)。
(2)、电源管理服务质量( power managementQuality Of Service)
[cpp] view plain copy
struct pm_qos_request_list pm_qos_req;
用于Wi-Fi和千兆以太网,可以帮助控制网络的延迟和带宽的需求,以达到在可用的前提下省电的目的。
(3)、硬件信息
[cpp] view plain copy
//网络设备内存映射时在主机中的内存区域
unsigned long mem_end;
unsigned long mem_start;
//网络设备I/O基地址
unsigned long base_addr;
//中断号
unsigned int irq;
//传输介质,如双绞线、同轴电缆等,在多端口设备中指定使用哪个端口
unsigned char if_port;
/* if_port可能的取值如下:
enum {
IF_PORT_UNKNOWN = 0,
IF_PORT_10BASE2,
IF_PORT_10BASET,
IF_PORT_AUI,
IF_PORT_100BASET,
IF_PORT_100BASETX,
IF_PORT_100BASEFX
};
**/
// DMA通道
unsigned char dma;
//最大传输单元,以太网数据帧最大为1500字节
unsigned int mtu;
//网络设备硬件类型,如10Mbps以太网ARPHRD_ETHER
unsigned short type;
//硬件数据帧头的长度,以太网为14字节
unsigned short hard_header_len;
//广播地址
unsigned char broadcast[MAX_ADDR_LEN];
//硬件(如MAC)地址长度以及设备的硬件地址
unsigned char addr_len;
unsigned char *dev_addr;
unsigned char perm_addr[MAX_ADDR_LEN];
unsigned char addr_assign_type;
(4)、标识符
[cpp] view plain copy
int ifindex; //标识网络设备的唯一索引号
int iflink; //用于虚拟网络设备
unsigned short dev_id; //用于共享网络设备
(5)、分配套接字缓冲区时预留空间的长度
[cpp] view plain copy
unsigned short needed_headroom;
unsigned short needed_tailroom;
(6)、在sysfs文件系统中输出网络设备信息
[cpp] view plain copy
struct device dev;
const struct attribute_group *sysfs_groups[4];
(7)、网络设备相关链表
[cpp] view plain copy
//以设备名为关键字的网络设备哈希链表
struct hlist_node name_hlist;
//网络设备链表
struct list_head dev_list;
//支持NAPI传输的网络设备链表
struct list_head napi_list;
//被注销的网络设备链表
struct list_head unreg_list;
//网络设备硬件地址组成的链表
struct netdev_hw_addr_list dev_addrs;
/* n-tuple filter list attached to this device */
struct ethtool_rx_ntuple_list ethtool_ntuple_list;
//单播地址链表
struct netdev_hw_addr_list uc;
//组播地址链表
struct netdev_hw_addr_list mc;
//防止单播地址链表和组播地址链表被并发访问的自旋锁
spinlock_t addr_list_lock;
//监听所有组播地址
unsigned int allmulti;
//延迟注册/注销的网络设备链表
struct list_head todo_list;
//以索引号为关键字的网络设备哈希链表
struct hlist_node index_hlist;
//链路查看机制链表
struct list_head link_watch_list;
(8)、混杂模式
[cpp] view plain copy
//混杂模式时的单播地址个数
int uc_promisc;
//混杂模式的计数器
unsigned int promiscuity;
(9)、网络层协议特定数据
[cpp] view plain copy
struct vlan_group __rcu *vlgrp; /* VLAN group */
void *dsa_ptr; /* dsa specific data */
void *atalk_ptr; /* AppleTalk link */
struct in_device __rcu *ip_ptr; /* IPv4 specific data */
struct dn_dev __rcu *dn_ptr; /* DECnet specific data */
struct inet6_dev __rcu *ip6_ptr; /* IPv6 specific data */
void *ec_ptr; /* Econet specific data */
void *ax25_ptr; /* AX.25 specific data */
struct wireless_dev *ieee80211_ptr; /* IEEE 802.11 specific data,
assign before registering */
(10)、设备硬件功能特性
[cpp] view plain copy
unsigned long features;
// features的可能取值如下:
#define NETIF_F_SG 1 /* Scatter/gather IO. */
#define NETIF_F_IP_CSUM 2 /* Can checksum TCP/UDP over IPv4. */
#define NETIF_F_NO_CSUM 4 /* Does not require checksum. F.e. loopack. */
#define NETIF_F_HW_CSUM 8 /* Can checksum all the packets. */
#define NETIF_F_IPV6_CSUM 16 /* Can checksum TCP/UDP over IPV6 */
#define NETIF_F_HIGHDMA 32 /* Can DMA to high memory. */
#define NETIF_F_FRAGLIST 64 /* Scatter/gather IO. */
#define NETIF_F_HW_VLAN_TX 128 /* Transmit VLAN hw acceleration */
#define NETIF_F_HW_VLAN_RX 256 /* Receive VLAN hw acceleration */
#define NETIF_F_HW_VLAN_FILTER 512 /* Receive filtering on VLAN */
#define NETIF_F_VLAN_CHALLENGED 1024 /* Device cannot handle VLAN packets */
#define NETIF_F_GSO 2048 /* Enable software GSO. */
#define NETIF_F_LLTX 4096 /* LockLess TX - deprecated. Please */
/* do not use LLTX in new drivers */
#define NETIF_F_NETNS_LOCAL 8192 /* Does not change network namespaces */
#define NETIF_F_GRO 16384 /* Generic receive offload */
#define NETIF_F_LRO 32768 /* large receive offload */
#define NETIF_F_FCOE_CRC (1 << 24) /* FCoE CRC32 */
#define NETIF_F_SCTP_CSUM (1 << 25) /* SCTP checksum offload */
#define NETIF_F_FCOE_MTU (1 << 26) /* Supports max FCoE MTU, 2158 bytes*/
#define NETIF_F_NTUPLE (1 << 27) /* N-tuple filters supported */
#define NETIF_F_RXHASH (1 << 28) /* Receive hashing offload */
#define NETIF_F_GSO_SHIFT 16
#define NETIF_F_GSO_MASK 0x00ff0000
#define NETIF_F_TSO (SKB_GSO_TCPV4 << NETIF_F_GSO_SHIFT)
#define NETIF_F_UFO (SKB_GSO_UDP << NETIF_F_GSO_SHIFT)
#define NETIF_F_GSO_ROBUST (SKB_GSO_DODGY << NETIF_F_GSO_SHIFT)
#define NETIF_F_TSO_ECN (SKB_GSO_TCP_ECN << NETIF_F_GSO_SHIFT)
#define NETIF_F_TSO6 (SKB_GSO_TCPV6 << NETIF_F_GSO_SHIFT)
#define NETIF_F_FSO (SKB_GSO_FCOE << NETIF_F_GSO_SHIFT)
#define NETIF_F_GSO_SOFTWARE (NETIF_F_TSO | NETIF_F_TSO_ECN | \
NETIF_F_TSO6 | NETIF_F_UFO)
#define NETIF_F_GEN_CSUM (NETIF_F_NO_CSUM | NETIF_F_HW_CSUM)
#define NETIF_F_V4_CSUM (NETIF_F_GEN_CSUM | NETIF_F_IP_CSUM)
#define NETIF_F_V6_CSUM (NETIF_F_GEN_CSUM | NETIF_F_IPV6_CSUM)
#define NETIF_F_ALL_CSUM (NETIF_F_V4_CSUM | NETIF_F_V6_CSUM)
#define NETIF_F_ONE_FOR_ALL (NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ROBUST | \
NETIF_F_SG | NETIF_F_HIGHDMA | \
NETIF_F_FRAGLIST)
(11)、分配net_device结构体及其私有数据时为对齐所需的填充位数目
[cpp] view plain copy
unsigned short padded;
(12)、其他信息
[cpp] view plain copy
//NETPOLL相关信息
struct netpoll_info *npinfo;
//网络命名空间
struct net *nd_net;
//中间层的私有数据
union {
void *ml_priv;
struct pcpu_lstats __percpu *lstats; /* loopback stats */
struct pcpu_tstats __percpu *tstats; /* tunnel stats */
struct pcpu_dstats __percpu *dstats; /* dummy stats */
};
//GARP协议相关
struct garp_port __rcu *garp_port;
//虚拟局域网相关
unsigned long vlan_features;
//GSO最大值
unsigned int gso_max_size;
//max exchange id for FCoE LRO by ddp
unsigned int fcoe_ddp_xid;
//PHY实例
struct phy_device *phydev;
2、网络设备的运行状态
(1)、网络设备物理上的工作状态
[cpp] view plain copy
unsigned long state;
/* state的可能取值如下:
enum netdev_state_t {
__LINK_STATE_START,
__LINK_STATE_PRESENT,
__LINK_STATE_NOCARRIER,
__LINK_STATE_LINKWATCH_PENDING,
__LINK_STATE_DORMANT,
};
**/
(2)、网络设备通信模式或状态
[cpp] view plain copy
//它们的可能取值定义在linux-2.6.38.8/include/linux/if.h文件中。
unsigned int flags;
unsigned short gflags;
unsigned int priv_flags; //类似flags,但对用户空间不可见
(3)、统计信息
[cpp] view plain copy
struct net_device_stats stats;
//在接收过程中丢弃的数据包数目(在网络驱动中不使用此项)
atomic_long_t rx_dropped;
(4)、RFC2863协议相关
[cpp] view plain copy
//RFC 2863操作状态
unsigned char operstate;
/* operstate的可能取值如下:
enum {
IF_OPER_UNKNOWN,
IF_OPER_NOTPRESENT,
IF_OPER_DOWN,
IF_OPER_LOWERLAYERDOWN,
IF_OPER_TESTING,
IF_OPER_DORMANT,
IF_OPER_UP,
};
**/
//映射到RFC2863兼容状态的策略
unsigned char link_mode;
/* link_mode的可能取值如下:
enum {
IF_LINK_MODE_DEFAULT,
IF_LINK_MODE_DORMANT,
};
**/
(5)、传输超时
[cpp] view plain copy
//最后接收数据包的时间
unsigned long last_rx;
//最近传送数据包的时间
unsigned long trans_start;
//发生传输超时时,设置的标志
int watchdog_timeo;
//网络层设置的传送数据包超时的时钟
struct timer_list watchdog_timer;
(6)、设备注册/注销状态机
[cpp] view plain copy
enum { NETREG_UNINITIALIZED=0,
NETREG_REGISTERED, /* completed register_netdevice */
NETREG_UNREGISTERING, /* called unregister_netdevice */
NETREG_UNREGISTERED, /* completed unregister todo */
NETREG_RELEASED, /* called free_netdev */
NETREG_DUMMY, /* dummy device for NAPI poll */
} reg_state:16;
(7)、引用计数
[cpp] view plain copy
int __percpu *pcpu_refcnt;
(8)、分组状态
[cpp] view plain copy
struct net_device *master;
(9)、RTNL初始化状态
[cpp] view plain copy
enum {
RTNL_LINK_INITIALIZED,
RTNL_LINK_INITIALIZING,
} rtnl_link_state:16;
3、网络设备的操作函数
[cpp] view plain copy
//使用无线网络设备扩展功能的一组操作函数
const struct iw_handler_def * wireless_handlers;
//无线网络设备处理函数所使用的数据
struct iw_public_data * wireless_data;
//网络设备驱动程序需要实现的一组操作函数
const struct net_device_ops *netdev_ops;
//支持ethtool功能的一组操作函数
const struct ethtool_ops *ethtool_ops;
//数据链路层协议头相关的一组操作函数
const struct header_ops *header_ops;
//析构函数,注销网络设备时被调用
void (*destructor)(struct net_device *dev);
//rtnetlink操作函数
const struct rtnl_link_ops *rtnl_link_ops;
//DCB操作函数
const struct dcbnl_rtnl_ops *dcbnl_ops;
4、数据包的收发队列
(1)、接收队列
[cpp] view plain copy
//RPS(Receive Packet Steering)特性
struct kset *queues_kset;
struct netdev_rx_queue *_rx;
unsigned int num_rx_queues;
unsigned int real_num_rx_queues;
rx_handler_func_t __rcu *rx_handler;
void __rcu *rx_handler_data;
struct netdev_queue __rcu *ingress_queue;
(2)、发送队列
[cpp] view plain copy
struct netdev_queue *_tx ____cacheline_aligned_in_smp;
unsigned int num_tx_queues;
unsigned int real_num_tx_queues;
struct Qdisc *qdisc;
unsigned long tx_queue_len;
spinlock_t tx_global_lock;
//XPS(Transmit Packet Steering)特性
struct xps_dev_maps __rcu *xps_maps;
评论
查看更多