本文分析基於 Linux Kernel 3.2.1,聚焦在系統啟動階段套接字子系統的初始化,以及 INET 協議族如何完成註冊。
系統初始化過程中會呼叫 sock_init,主要工作是各項快取(slab cache)的初始化與 sockfs 檔案系統的掛載:
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;
}
值得注意的一行註解是「真正的協議初始化在後續的 initcalls 中完成」。也就是說 sock_init 只負責搭好基礎設施(sock 與 sk_buff 快取、sockfs),TCP/UDP 等具體協議要等到稍後階段才註冊。若 netfilter 或網路位址時間戳功能被編入核心,這裡也會順帶完成各自的初始化。
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;
}
inet_init 的內容可以歸成幾層:先註冊傳輸層的 proto 操作集(tcp_prot、udp_prot、raw_prot、ping_prot),再把 INET 協議族的 socket 介面註冊進 sock_register;接著用 inet_add_protocol 把各协议的接收處理掛上,並以 inetsw_array 為基礎註冊 socket 側的資訊,供日後 inet_create 依「協議族 + 類型 + 協議號」查表使用;最後依序啟動 ARP、IP、TCP、UDP、UDP-Lite、ICMP 等子模塊,註冊 /proc 與分片初始化,並用 dev_add_pack 把 IP 掛進網路設備的收包分發鏈路。
值得留意的是 BUILD_BUG_ON 那一行:它在編譯期檢查 inet_skb_parm 是否塞得進 sk_buff 的 cb 欄位,若超了就讓編譯失敗。這是核心常見的手法——把「不能破壞的預算」變成編譯錯誤,而不是執行期才爆掉的記憶體越界。此外,錯誤處理全部集中在尾部的 goto 標籤,依註冊的相反順序逐一回滾,確保部分初始化失敗時不會留下殘留狀態。