本文分析基于Linux Kernel 3.2.1
更多请查看 Linux内核--网络内核实现分析
1、系统初始化过程中会调用sock_init函数进行套接字的初始化,主要是进行缓存的初始化
- static int __init sock_init(void)
- {
- int err;
- /*
- * 初始化.sock缓存
- */
- sk_init();
- /*
- * 初始化sk_buff缓存
- */
- skb_init();
- /*
- * 初始化协议模块缓存
- */
- init_inodecache();
- //注册文件系统类型
- err = register_filesystem(&sock_fs_type);
- if (err)
- goto out_fs;
- sock_mnt = kern_mount(&sock_fs_type);
- if (IS_ERR(sock_mnt)) {
- err = PTR_ERR(sock_mnt);
- goto out_mount;
- }
- /* The real protocol initialization is performed in later initcalls.
- */
- #ifdef CONFIG_NETFILTER
- netfilter_init();
- #endif
- #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
- skb_timestamping_init();
- #endif
- out:
- return err;
- out_mount:
- unregister_filesystem(&sock_fs_type);
- out_fs:
- goto out;
- }
2、INET协议族的初始化函数
- static int __init inet_init(void)
- {
- struct sk_buff *dummy_skb;
- struct inet_protosw *q;
- struct list_head *r;
- int rc = -EINVAL;
- BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb));
- sysctl_local_reserved_ports = kzalloc(65536 / 8, GFP_KERNEL);
- if (!sysctl_local_reserved_ports)
- goto out;
- //下面注册传输层协议操作集
- rc = proto_register(&tcp_prot, 1);
- if (rc)
- goto out_free_reserved_ports;
- rc = proto_register(&udp_prot, 1);
- if (rc)
- goto out_unregister_tcp_proto;
- rc = proto_register(&raw_prot, 1);
- if (rc)
- goto out_unregister_udp_proto;
- rc = proto_register(&ping_prot, 1);
- if (rc)
- goto out_unregister_raw_proto;
- //注册INET协议族的handler
- (void)sock_register(&inet_family_ops);
- #ifdef CONFIG_SYSCTL
- ip_static_sysctl_init();
- #endif
- /*
- * Add all the base protocols.
- */
- //将INET协议族协议数据包接收函数添加到系统中
- if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
- printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
- if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
- printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
- if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
- printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
- #ifdef CONFIG_IP_MULTICAST
- if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
- printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
- #endif
- /* Register the socket-side information for inet_create. */
- for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
- INIT_LIST_HEAD(r);
- //将inetsw_array中的元素按套接字类型注册到inetsw链表数组中
- for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
- inet_register_protosw(q);
- /*
- * Set the ARP module up
- */
- arp_init();
- /*
- * Set the IP module up
- */
- ip_init();
- tcp_v4_init();
- /* Setup TCP slab cache for open requests. */
- tcp_init();
- /* Setup UDP memory threshold */
- udp_init();
- /* Add UDP-Lite (RFC 3828) */
- udplite4_register();
- ping_init();
- /*
- * Set the ICMP layer up
- */
- if (icmp_init() < 0)
- panic("Failed to create the ICMP control socket.\n");
- /*
- * Initialise the multicast router
- */
- #if defined(CONFIG_IP_MROUTE)
- if (ip_mr_init())
- printk(KERN_CRIT "inet_init: Cannot init ipv4 mroute\n");
- #endif
- /*
- * Initialise per-cpu ipv4 mibs
- */
- if (init_ipv4_mibs())
- printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n");
- ipv4_proc_init();
- ipfrag_init();
- dev_add_pack(&ip_packet_type);
- rc = 0;
- out:
- return rc;
- out_unregister_raw_proto:
- proto_unregister(&raw_prot);
- out_unregister_udp_proto:
- proto_unregister(&udp_prot);
- out_unregister_tcp_proto:
- proto_unregister(&tcp_prot);
- out_free_reserved_ports:
- kfree(sysctl_local_reserved_ports);
- goto out;
- }
