在Linux系統(tǒng)中,網(wǎng)絡(luò)設(shè)備都被抽象為struct net_device結(jié)構(gòu)體。它是網(wǎng)絡(luò)設(shè)備硬件與上層協(xié)議之間聯(lián)系的接口,了解它對(duì)編寫網(wǎng)絡(luò)驅(qū)動(dòng)程序非常有益,所以本文將著手簡(jiǎn)要介紹linux-2.6.38.8/include/linux/netdevice.h文件中struct net_device結(jié)構(gòu)體的所有成員(沒(méi)有按照它們定義的順序)。
1、網(wǎng)絡(luò)設(shè)備相關(guān)信息
(1)、設(shè)備名?
[cpp]?view plain?copy
char????name[IFNAMSIZ];??
char????*ifalias;??//用于SNMP協(xié)議??
在Linux系統(tǒng)中,每個(gè)網(wǎng)絡(luò)設(shè)備都有一個(gè)唯一的設(shè)備名(如eth0,字母部分代表網(wǎng)絡(luò)設(shè)備的類型,數(shù)字部分代表此類網(wǎng)絡(luò)設(shè)備的數(shù)量)。
(2)、電源管理服務(wù)質(zhì)量(?power managementQuality Of Service)?
[cpp]?view plain?copy
struct?pm_qos_request_list?pm_qos_req;??
用于Wi-Fi和千兆以太網(wǎng),可以幫助控制網(wǎng)絡(luò)的延遲和帶寬的需求,以達(dá)到在可用的前提下省電的目的。
(3)、硬件信息?
[cpp]?view plain?copy
//網(wǎng)絡(luò)設(shè)備內(nèi)存映射時(shí)在主機(jī)中的內(nèi)存區(qū)域??
unsigned?long???mem_end;??
unsigned?long???mem_start;??
//網(wǎng)絡(luò)設(shè)備I/O基地址??
unsigned?long???base_addr;??
//中斷號(hào)??
unsigned?int????irq;??
//傳輸介質(zhì),如雙絞線、同軸電纜等,在多端口設(shè)備中指定使用哪個(gè)端口??
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;??
//最大傳輸單元,以太網(wǎng)數(shù)據(jù)幀最大為1500字節(jié)??
unsigned?int????mtu;??
//網(wǎng)絡(luò)設(shè)備硬件類型,如10Mbps以太網(wǎng)ARPHRD_ETHER??
unsigned?short??type;??
//硬件數(shù)據(jù)幀頭的長(zhǎng)度,以太網(wǎng)為14字節(jié)??
unsigned?short??hard_header_len;??
//廣播地址??
unsigned?char???broadcast[MAX_ADDR_LEN];??
//硬件(如MAC)地址長(zhǎng)度以及設(shè)備的硬件地址??
unsigned?char???addr_len;??
unsigned?char???*dev_addr;??
unsigned?char???perm_addr[MAX_ADDR_LEN];??
unsigned?char???addr_assign_type;??
(4)、標(biāo)識(shí)符?
[cpp]?view plain?copy
int?ifindex;?//標(biāo)識(shí)網(wǎng)絡(luò)設(shè)備的唯一索引號(hào)??
int?iflink;??//用于虛擬網(wǎng)絡(luò)設(shè)備??
unsigned?short??dev_id;?//用于共享網(wǎng)絡(luò)設(shè)備??
(5)、分配套接字緩沖區(qū)時(shí)預(yù)留空間的長(zhǎng)度?
[cpp]?view plain?copy
unsigned?short??needed_headroom;??
unsigned?short??needed_tailroom;??
(6)、在sysfs文件系統(tǒng)中輸出網(wǎng)絡(luò)設(shè)備信息?
[cpp]?view plain?copy
struct?device???dev;??
const?struct?attribute_group?*sysfs_groups[4];??
(7)、網(wǎng)絡(luò)設(shè)備相關(guān)鏈表?
[cpp]?view plain?copy
//以設(shè)備名為關(guān)鍵字的網(wǎng)絡(luò)設(shè)備哈希鏈表??
struct?hlist_node???name_hlist;??
//網(wǎng)絡(luò)設(shè)備鏈表??
struct?list_head????dev_list;??
//支持NAPI傳輸?shù)木W(wǎng)絡(luò)設(shè)備鏈表??
struct?list_head????napi_list;??
//被注銷的網(wǎng)絡(luò)設(shè)備鏈表??
struct?list_head????unreg_list;??
//網(wǎng)絡(luò)設(shè)備硬件地址組成的鏈表??
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;??
//防止單播地址鏈表和組播地址鏈表被并發(fā)訪問(wèn)的自旋鎖??
spinlock_t??????addr_list_lock;??
//監(jiān)聽(tīng)所有組播地址??
unsigned?int????????allmulti;??
//延遲注冊(cè)/注銷的網(wǎng)絡(luò)設(shè)備鏈表??
struct?list_head????todo_list;??
//以索引號(hào)為關(guān)鍵字的網(wǎng)絡(luò)設(shè)備哈希鏈表??
struct?hlist_node???index_hlist;??
//鏈路查看機(jī)制鏈表??
struct?list_head????link_watch_list;??
(8)、混雜模式?
[cpp]?view plain?copy
//混雜模式時(shí)的單播地址個(gè)數(shù)??
int?????uc_promisc;??
//混雜模式的計(jì)數(shù)器??
unsigned?int????promiscuity;??
(9)、網(wǎng)絡(luò)層協(xié)議特定數(shù)據(jù)?
[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)、設(shè)備硬件功能特性?
[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?<
#define?NETIF_F_UFO?????(SKB_GSO_UDP?<
#define?NETIF_F_GSO_ROBUST??(SKB_GSO_DODGY?<
#define?NETIF_F_TSO_ECN?????(SKB_GSO_TCP_ECN?<
#define?NETIF_F_TSO6????????(SKB_GSO_TCPV6?<
#define?NETIF_F_FSO?????(SKB_GSO_FCOE?<
#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結(jié)構(gòu)體及其私有數(shù)據(jù)時(shí)為對(duì)齊所需的填充位數(shù)目
[cpp]?view plain?copy
unsigned?short??padded;???
(12)、其他信息?
[cpp]?view plain?copy
//NETPOLL相關(guān)信息??
struct?netpoll_info?*npinfo;??
//網(wǎng)絡(luò)命名空間??
struct?net??????*nd_net;??
//中間層的私有數(shù)據(jù)??
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協(xié)議相關(guān)??
struct?garp_port?__rcu??*garp_port;??
//虛擬局域網(wǎng)相關(guān)??
unsigned?long?vlan_features;??
//GSO最大值??
unsigned?int????????gso_max_size;??
//max?exchange?id?for?FCoE?LRO?by?ddp??
unsigned?int????????fcoe_ddp_xid;??
//PHY實(shí)例??
struct?phy_device?*phydev;??
2、網(wǎng)絡(luò)設(shè)備的運(yùn)行狀態(tài)
(1)、網(wǎng)絡(luò)設(shè)備物理上的工作狀態(tài)?
[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)、網(wǎng)絡(luò)設(shè)備通信模式或狀態(tài)?
[cpp]?view plain?copy
//它們的可能取值定義在linux-2.6.38.8/include/linux/if.h文件中。??
unsigned?int????????flags;??
unsigned?short??????gflags;??
unsigned?int????????????priv_flags;?//類似flags,但對(duì)用戶空間不可見(jiàn)??
(3)、統(tǒng)計(jì)信息?
[cpp]?view plain?copy
struct?net_device_stats?stats;??
//在接收過(guò)程中丟棄的數(shù)據(jù)包數(shù)目(在網(wǎng)絡(luò)驅(qū)動(dòng)中不使用此項(xiàng))??
atomic_long_t???????rx_dropped;???
(4)、RFC2863協(xié)議相關(guān)?
[cpp]?view plain?copy
//RFC?2863操作狀態(tài)??
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兼容狀態(tài)的策略??
unsigned?char???????link_mode;??
/*?link_mode的可能取值如下:?
enum?{?
IF_LINK_MODE_DEFAULT,?
IF_LINK_MODE_DORMANT,?
};?
**/??
(5)、傳輸超時(shí)?
[cpp]?view plain?copy
//最后接收數(shù)據(jù)包的時(shí)間??
unsigned?long???????last_rx;??
//最近傳送數(shù)據(jù)包的時(shí)間??
unsigned?long???????trans_start;??
//發(fā)生傳輸超時(shí)時(shí),設(shè)置的標(biāo)志??
int?????????watchdog_timeo;??
//網(wǎng)絡(luò)層設(shè)置的傳送數(shù)據(jù)包超時(shí)的時(shí)鐘????
struct?timer_list???watchdog_timer;??
(6)、設(shè)備注冊(cè)/注銷狀態(tài)機(jī)?
[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)、引用計(jì)數(shù)?
[cpp]?view plain?copy
int?__percpu????????*pcpu_refcnt;??
(8)、分組狀態(tài)?
[cpp]?view plain?copy
struct?net_device???*master;??
(9)、RTNL初始化狀態(tài)?
[cpp]?view plain?copy
enum?{??
RTNL_LINK_INITIALIZED,??
RTNL_LINK_INITIALIZING,??
}?rtnl_link_state:16;??
3、網(wǎng)絡(luò)設(shè)備的操作函數(shù)?
[cpp]?view plain?copy
//使用無(wú)線網(wǎng)絡(luò)設(shè)備擴(kuò)展功能的一組操作函數(shù)??
const?struct?iw_handler_def?*???wireless_handlers;??
//無(wú)線網(wǎng)絡(luò)設(shè)備處理函數(shù)所使用的數(shù)據(jù)??
struct?iw_public_data?*?wireless_data;??
//網(wǎng)絡(luò)設(shè)備驅(qū)動(dòng)程序需要實(shí)現(xiàn)的一組操作函數(shù)??
const?struct?net_device_ops?*netdev_ops;??
//支持ethtool功能的一組操作函數(shù)??
const?struct?ethtool_ops?*ethtool_ops;??
//數(shù)據(jù)鏈路層協(xié)議頭相關(guān)的一組操作函數(shù)??
const?struct?header_ops?*header_ops;??
//析構(gòu)函數(shù),注銷網(wǎng)絡(luò)設(shè)備時(shí)被調(diào)用??
void?(*destructor)(struct?net_device?*dev);??
//rtnetlink操作函數(shù)??
const?struct?rtnl_link_ops?*rtnl_link_ops;??
//DCB操作函數(shù)??
const?struct?dcbnl_rtnl_ops?*dcbnl_ops;??
4、數(shù)據(jù)包的收發(fā)隊(duì)列
(1)、接收隊(duì)列?
[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)、發(fā)送隊(duì)列?
[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; ?
?
評(píng)論
查看更多