ifconfg命令天天用,但是还真没有认真分析过,今天总结一下。
以ifconfig为入口,了解内核对应的数据结构字段。
先看一下ifconfig的结果:
flags=4163<UP,BROADCAST,RUNNING,MULTICAST>:
1
2 1unsigned int flags;
2
表示网卡状态:
UP:对应内核IFF_UP,表示网卡是开启的。
BROADCAST:表示网卡支持广播,对应内核IFF_BROADCAST
MULTICAST:表示网卡支持组播,对应内核IFF_MULTICAST
RUNNING:对应内核IFF_RUNNING,给的注释是@IFF_RUNNING: interface RFC2863 OPER_UP. Volatile.表示支持RFC2863接口组网络管理协议。RFC2863定义了管理信息库(MIB)的一部分,用于Internet社区中的网络管理协议。
4163的由来:在linux内核中flags取值如下:
4163 = IFF_UP + IFF_BROADCAST + IFF_RUNNING + IFF_MULTICAST
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 1enum net_device_flags {
2 IFF_UP = 1<<0, /* sysfs */
3 IFF_BROADCAST = 1<<1, /* volatile */
4 IFF_DEBUG = 1<<2, /* sysfs */
5 IFF_LOOPBACK = 1<<3, /* volatile */
6 IFF_POINTOPOINT = 1<<4, /* volatile */
7 IFF_NOTRAILERS = 1<<5, /* sysfs */
8 IFF_RUNNING = 1<<6, /* volatile */
9 IFF_NOARP = 1<<7, /* sysfs */
10 IFF_PROMISC = 1<<8, /* sysfs */
11 IFF_ALLMULTI = 1<<9, /* sysfs */
12 IFF_MASTER = 1<<10, /* volatile */
13 IFF_SLAVE = 1<<11, /* volatile */
14 IFF_MULTICAST = 1<<12, /* sysfs */
15 IFF_PORTSEL = 1<<13, /* sysfs */
16 IFF_AUTOMEDIA = 1<<14, /* sysfs */
17 IFF_DYNAMIC = 1<<15, /* sysfs */
18 IFF_LOWER_UP = 1<<16, /* volatile */
19 IFF_DORMANT = 1<<17, /* volatile */
20 IFF_ECHO = 1<<18, /* volatile */
21};
22
MTU:最大传输单元1500,没有包括MAC头
1
2 1unsigned int mtu;
2
IP地址、子网掩码、广播地址存储在net_device元素ip_ptr的in_ifaddr中。
关于in_device,in_ifaddr数据结构后面再做分析。
1
2
3
4
5
6
7
8
9
10
11
12
13
14 1//net_device中
2struct in_device __rcu *ip_ptr;
3
4//in_device中
5struct in_ifaddr *ifa_list; /* IP ifaddr chain*/
6
7//in_ifaddr中
8__be32 ifa_local;//本地IP地址
9__be32 ifa_address;//本地IP地址(广播链路),对端的IP地址(点对点链路)
10__be32 ifa_mask;//子网掩码
11__be32 ifa_broadcast;//广播地址
12unsigned char ifa_scope;
13unsigned char ifa_prefixlen;//子网掩码长度
14
ifa_local和ifa_address的区别:
1
2
3
4
5
6
7
8 1/*
2* Important comment:
3* IFA_ADDRESS is prefix address, rather than local interface address.
4* It makes no difference for normally configured broadcast interfaces,
5* but for point-to-point IFA_ADDRESS is DESTINATION address,
6* local address is supplied in IFA_LOCAL attribute.
7*/
8
1.ifa_local始终表示本地IP地址
2.如果设备配置了支持广播,ifa_address和if_local一样,也是本地IP地址;如果点对点链路,ifa_address表示对端的IP地址。
ifa_scope:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 1/* rtm_scope
2
3 Really it is not scope, but sort of distance to the destination.
4 NOWHERE are reserved for not existing destinations, HOST is our
5 local addresses, LINK are destinations, located on directly attached
6 link and UNIVERSE is everywhere in the Universe.
7
8 Intermediate values are also possible f.e. interior routes
9 could be assigned a value between UNIVERSE and LINK.
10*/
11
12enum rt_scope_t {
13 RT_SCOPE_UNIVERSE=0,
14/* User defined values */
15 RT_SCOPE_SITE=200,
16 RT_SCOPE_LINK=253,
17 RT_SCOPE_HOST=254,
18 RT_SCOPE_NOWHERE=255
19};
20
21
HOST:表示该地址只用于主机的内部通信。例如:127.0.0.1
LINK:表示地址仅在局域网内部有意义,例如私有地址?
UNIBERSE:表示地址可以在任何地方使用,普通的外网IP地址?
NOWHERE:表示目的地址不存在
SITE:??
点对点链路
IPv6地址、前缀长度,地址范围
IPv6懒得找对应字段了。
MAC地址
1
2 1unsigned char perm_addr[MAX_ADDR_LEN];
2
txqueuelen 1000
发送队列中排队的数据包数,默认为1000个。
1
2 1unsigned long tx_queue_len;
2
RX packet 1992 bytes 538289
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11018 bytes 1019013
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
这几个错误后面还要好好区分一下
errors:表示总的错误,如帧校验错、各种丢弃
dropped:表示数据包已经进入了 Ring Buffer,但是由于内存不够等系统原因,导致在拷贝到内存的过程中被丢弃。
overruns:Ring Buffer满,数据包还没到Ring Buffer就丢弃了
frame:帧错误
对应net_device的stats元素
1
2 1struct net_device_stats stats;
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 1
2/*
3 * Old network device statistics. Fields are native words
4 * (unsigned long) so they can be read and written atomically.
5 */
6
7struct net_device_stats {
8 unsigned long rx_packets; //接收时,总的正确数据包数
9 unsigned long tx_packets;//接收时,总的错误数据包数
10 unsigned long rx_bytes;
11 unsigned long tx_bytes;
12 unsigned long rx_errors;
13 unsigned long tx_errors;
14 unsigned long rx_dropped;
15 unsigned long tx_dropped;
16 unsigned long multicast;
17 unsigned long collisions;
18 unsigned long rx_length_errors;
19 unsigned long rx_over_errors;//由于过速丢弃的数据包数
20 unsigned long rx_crc_errors;
21 unsigned long rx_frame_errors;
22 unsigned long rx_fifo_errors;
23 unsigned long rx_missed_errors;
24 unsigned long tx_aborted_errors;
25 unsigned long tx_carrier_errors;
26 unsigned long tx_fifo_errors;
27 unsigned long tx_heartbeat_errors;
28 unsigned long tx_window_errors;
29 unsigned long rx_compressed;
30 unsigned long tx_compressed;
31};
32
33
net_device中也有rx_dropped和tx_dropped这两个元素,不知道值是不是一样的。
1
2
3 1atomic_long_t rx_dropped;
2atomic_long_t tx_dropped;
3
device interrupt 19:
给设备分配的中断号,对应net_device中的
1
2 1int irq;
2