在Linux系統中,網絡設備都被抽象爲struct net_device結構體。它是網絡設備硬件與上層協議之間聯繫的接口,瞭解它對編寫網絡驅動程序很是有益,因此本文將着手簡要介紹linux-2.6.38.8/include/linux/netdevice.h文件中struct net_device結構體的全部成員(沒有按照它們定義的順序)。node
一、網絡設備相關信息linux
(1)、設備名 api
char name[IFNAMSIZ]; char *ifalias; //用於SNMP協議
在Linux系統中,每一個網絡設備都有一個惟一的設備名(如eth0,字母部分表明網絡設備的類型,數字部分表明此類網絡設備的數量)。網絡
(2)、電源管理服務質量( power managementQuality Of Service) 併發
struct pm_qos_request_list pm_qos_req;
用於Wi-Fi和千兆以太網,能夠幫助控制網絡的延遲和帶寬的需求,以達到在可用的前提下省電的目的。less
(3)、硬件信息 函數
//網絡設備內存映射時在主機中的內存區域 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)、標識符 oop
int ifindex; //標識網絡設備的惟一索引號 int iflink; //用於虛擬網絡設備 unsigned short dev_id; //用於共享網絡設備
(5)、分配套接字緩衝區時預留空間的長度 ui
unsigned short needed_headroom; unsigned short needed_tailroom;
(6)、在sysfs文件系統中輸出網絡設備信息 this
struct device dev; const struct attribute_group *sysfs_groups[4];
(7)、網絡設備相關鏈表
//以設備名爲關鍵字的網絡設備哈希鏈表 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)、混雜模式
//混雜模式時的單播地址個數 int uc_promisc; //混雜模式的計數器 unsigned int promiscuity;
(9)、網絡層協議特定數據
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)、設備硬件功能特性
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結構體及其私有數據時爲對齊所需的填充位數目
unsigned short padded;
(12)、其餘信息
//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;
二、網絡設備的運行狀態
(1)、網絡設備物理上的工做狀態
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)、網絡設備通訊模式或狀態
//它們的可能取值定義在linux-2.6.38.8/include/linux/if.h文件中。 unsigned int flags; unsigned short gflags; unsigned int priv_flags; //相似flags,但對用戶空間不可見
(3)、統計信息
struct net_device_stats stats; //在接收過程當中丟棄的數據包數目(在網絡驅動中不使用此項) atomic_long_t rx_dropped;
(4)、RFC2863協議相關
//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)、傳輸超時
//最後接收數據包的時間 unsigned long last_rx; //最近傳送數據包的時間 unsigned long trans_start; //發生傳輸超時時,設置的標誌 int watchdog_timeo; //網絡層設置的傳送數據包超時的時鐘 struct timer_list watchdog_timer;
(6)、設備註冊/註銷狀態機
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)、引用計數
int __percpu *pcpu_refcnt;
(8)、分組狀態
struct net_device *master;
(9)、RTNL初始化狀態
enum { RTNL_LINK_INITIALIZED, RTNL_LINK_INITIALIZING, } rtnl_link_state:16;
三、網絡設備的操做函數
//使用無線網絡設備擴展功能的一組操做函數 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;
四、數據包的收發隊列
(1)、接收隊列
//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)、發送隊列
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;