天天看点

慢慢欣赏linux 网络协议栈二 net_device以及初始化注册 (4.19版本)

代码流程

static int __init net_dev_init(void)
{
	BUG_ON(!dev_boot_phase);

	dev_proc_init();
	=>int __init dev_proc_init(void)
	{
		int ret = register_pernet_subsys(&dev_proc_ops);
		==>static struct pernet_operations __net_initdata dev_proc_ops = {
			.init = dev_proc_net_init,
			==>static int __net_init dev_proc_net_init(struct net *net)
			{
				proc_create_net("dev", 0444, net->proc_net, &dev_seq_ops, sizeof(struct seq_net_private));
				==>static const struct seq_operations dev_seq_ops = {
					.start = dev_seq_start,
					.next  = dev_seq_next,
					.stop  = dev_seq_stop,
					.show  = dev_seq_show,
				};

				proc_create_seq("softnet_stat", 0444, net->proc_net, &softnet_seq_ops);
				==>static const struct seq_operations softnet_seq_ops = {
					.start = softnet_seq_start,
					.next  = softnet_seq_next,
					.stop  = softnet_seq_stop,
					.show  = softnet_seq_show,
				};

				proc_create_net("ptype", 0444, net->proc_net, &ptype_seq_ops, sizeof(struct seq_net_private));
				==>static const struct seq_operations ptype_seq_ops = {
					.start = ptype_seq_start,
					.next  = ptype_seq_next,
					.stop  = ptype_seq_stop,
					.show  = ptype_seq_show,
				};

				return 0;
			}
			.exit = dev_proc_net_exit,
		};
		ret = register_pernet_subsys(&dev_mc_net_ops);
		==>static struct pernet_operations __net_initdata dev_mc_net_ops = {
			.init = dev_mc_net_init,
			.exit = dev_mc_net_exit,
		};
		return ret;
	}

	netdev_kobject_init();
	=>int __init netdev_kobject_init(void)
	{
		kobj_ns_type_register(&net_ns_type_operations);
		==>const struct kobj_ns_type_operations net_ns_type_operations = {
			.type = KOBJ_NS_TYPE_NET,
			.current_may_mount = net_current_may_mount,
			.grab_current_ns = net_grab_current_ns,
			.netlink_ns = net_netlink_ns,
			.initial_ns = net_initial_ns,
			.drop_ns = net_drop_ns,
		};
		return class_register(&net_class);
		==>static struct class net_class __ro_after_init = {
			.name = "net",
			.dev_release = netdev_release,
			.dev_groups = net_class_groups,
			.dev_uevent = netdev_uevent,
			.ns_type = &net_ns_type_operations,
			.namespace = net_namespace,
			.get_ownership = net_get_ownership,
		};
	}
	
	INIT_LIST_HEAD(&ptype_all);
	for (i = 0; i < PTYPE_HASH_SIZE; i++)
		INIT_LIST_HEAD(&ptype_base[i]);

	INIT_LIST_HEAD(&offload_base);
	
	register_pernet_subsys(&netdev_net_ops);
	==>static struct pernet_operations __net_initdata netdev_net_ops = {
		.init = netdev_init,
		.exit = netdev_exit,
	};
	
	for_each_possible_cpu(i) {	// Initialise the packet receive queues.
		struct work_struct *flush = per_cpu_ptr(&flush_works, i);
		struct softnet_data *sd = &per_cpu(softnet_data, i);

		INIT_WORK(flush, flush_backlog);

		skb_queue_head_init(&sd->input_pkt_queue);
		skb_queue_head_init(&sd->process_queue);

		INIT_LIST_HEAD(&sd->poll_list);
		sd->output_queue_tailp = &sd->output_queue;

		init_gro_hash(&sd->backlog);
		sd->backlog.poll = process_backlog;
		sd->backlog.weight = weight_p;
	}
	
	dev_boot_phase = 0;

	register_pernet_device(&loopback_net_ops);
	==>struct pernet_operations __net_initdata loopback_net_ops = {
		.init = loopback_net_init,
	};
	
	register_pernet_device(&default_device_ops);
	==>static struct pernet_operations __net_initdata default_device_ops = {
		.exit = default_device_exit,
		.exit_batch = default_device_exit_batch,
	};

	open_softirq(NET_TX_SOFTIRQ, net_tx_action);
	open_softirq(NET_RX_SOFTIRQ, net_rx_action);

	rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead", NULL, dev_cpu_dead);

	return rc;
}
           

设备操作

XXX$ ls /proc/net/
anycast6   dev        fib_trie      icmp6     igmp6          ip6_mr_vif   ip_tables_matches  ipv6_route  netfilter  packet     ptype  route      rt_cache  sockstat      stat  udp      udplite6  xfrm_stat
arp        dev_mcast  fib_triestat  if_inet6  ip6_flowlabel  ip_mr_cache  ip_tables_names    mcfilter    netlink    protocols  raw    rt6_stats  snmp      sockstat6     tcp   udp6     unix
connector  dev_snmp6  icmp          igmp      ip6_mr_cache   ip_mr_vif    ip_tables_targets  mcfilter6   netstat    psched     raw6   rt_acct    snmp6     softnet_stat  tcp6  udplite  wireless
XXX$ ls /proc/net/dev
/proc/net/dev
XXX$ cat /proc/net/dev
Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo: 1397383   12631    0    0    0     0          0         0  1397383   12631    0    0    0     0       0          0
enp1s0: 762157760  901531    0    0    0     0          0    160928 40164089  347187    0    0    0     0       0          0
XXX$ cat /proc/net/dev
Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo: 1397383   12631    0    0    0     0          0         0  1397383   12631    0    0    0     0       0          0
enp1s0: 762164550  901623    0    0    0     0          0    160980 40164163  347188    0    0    0     0       0          0
XXX$ cat /proc/net/
anycast6           dev_snmp6/         if_inet6           ip6_mr_vif         ip_tables_targets  netlink            ptype              rt_acct            sockstat6          udp                wireless
arp                fib_trie           igmp               ip_mr_cache        ipv6_route         netstat            raw                rt_cache           softnet_stat       udp6               xfrm_stat
connector          fib_triestat       igmp6              ip_mr_vif          mcfilter           packet             raw6               snmp               stat/              udplite            
dev                icmp               ip6_flowlabel      ip_tables_matches  mcfilter6          protocols          route              snmp6              tcp                udplite6           
dev_mcast          icmp6              ip6_mr_cache       ip_tables_names    netfilter/         psched             rt6_stats          sockstat           tcp6               unix               
XXX$ cat /proc/net/softnet_stat 
000004c0 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
0000046b 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001
00000396 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000002
0000045f 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000003
000004d3 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000004
00000352 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000005
0000042d 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000006
0000042e 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000007
000003e3 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000008
000003be 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000009
000003a5 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 0000000a
000cb190 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 0000000b
XXX$ cat /proc/net/softnet_stat 
000004c0 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
0000046b 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001
00000396 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000002
0000045f 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000003
000004d3 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000004
00000352 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000005
0000042d 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000006
0000042e 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000007
000003e3 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000008
000003be 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000009
000003a5 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 0000000a
000cb1b9 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 0000000b
XXX$ cat /proc/net/ptype 
Type Device      Function
0800          ip_rcv
0806          arp_rcv
86dd          ipv6_rcv
XXX$ 
XXX$ cat /proc/net/dev_mcast 
2    enp1s0          1     0     01005e000001
2    enp1s0          1     0     333300000001
2    enp1s0          1     0     3333ff0019b5
2    enp1s0          1     0     3333000000fb
2    enp1s0          1     0     01005e0000fb
XXX$ 
XXX$ cat /proc/net/dev_mcast 
2    enp1s0          1     0     01005e000001
2    enp1s0          1     0     333300000001
2    enp1s0          1     0     3333ff0019b5
2    enp1s0          1     0     3333000000fb
2    enp1s0          1     0     01005e0000fb
XXX$ cat /proc/net/dev_mcast 
2    enp1s0          1     0     01005e000001
2    enp1s0          1     0     333300000001
2    enp1s0          1     0     3333ff0019b5
2    enp1s0          1     0     3333000000fb
2    enp1s0          1     0     01005e0000fb
XXX$ 
XXX$ 
XXX$ 
XXX$ cat /proc/net/dev_mcast 
2    enp1s0          1     0     01005e000001
2    enp1s0          1     0     333300000001
2    enp1s0          1     0     3333ff0019b5
2    enp1s0          1     0     3333000000fb
2    enp1s0          1     0     01005e0000fb
XXX$ cat /proc/net/dev_mcast 
2    enp1s0          1     0     01005e000001
2    enp1s0          1     0     333300000001
2    enp1s0          1     0     3333ff0019b5
2    enp1s0          1     0     3333000000fb
2    enp1s0          1     0     01005e0000fb
           

网络数据包收发流程(一):从驱动到协议栈

http://blog.chinaunix.net/uid-24148050-id-464587.html

网络数据包收发流程(二):不配置NAPI的情况

http://blog.chinaunix.net/uid-24148050-id-473352.html

网络数据包收发流程(三):e1000网卡和DMA

https://blog.csdn.net/wangyangzhizunwudi/article/details/99867964

网络数据包收发流程(四):协议栈之packet_type

http://blog.chinaunix.net/uid-24148050-id-1994898.html

继续阅读