學習Linux-4.12核心網路協議棧(1.4)——協議棧的初始化(proto_init)
這篇文章主要分析proto_init過程,它完成協議相關proc檔案的建立
3241 static __net_initdata struct pernet_operations proto_net_ops = {
3242 .init = proto_init_net,
3243 .exit = proto_exit_net,
3244 };
3246 static int __init proto_init(void) 3247 { 3248 return register_pernet_subsys(&proto_net_ops); 3249 } 3250 3251 subsys_initcall(proto_init);
948 int register_pernet_subsys(struct pernet_operations *ops) 949 { 950 int error; 951 mutex_lock(&net_mutex); 952 error = register_pernet_operations(first_device, ops); 953 mutex_unlock(&net_mutex); 954 return error; 955 } 956 EXPORT_SYMBOL_GPL(register_pernet_subsys);
該函式的主要作用是將一個網路協議模組新增到每一個網路命令空間中,然後再執行其ops->init程式進行初始化,一般其ops->init會在其對應的proc目錄下,生成一個網路協議模組對應的proc檔案或proc目錄,並執行一些協議初始化相關的函式。
關於register_pernet_subsys函式可以參考這篇文章
3227 static __net_init int proto_init_net(struct net *net) 3228 { 3229 if (!proc_create("protocols", S_IRUGO, net->proc_net, &proto_seq_fops)) 3230 return -ENOMEM; 3231 3232 return 0; 3233 } 3234 3235 static __net_exit void proto_exit_net(struct net *net) 3236 { 3237 remove_proc_entry("protocols", net->proc_net); 3238 }
proto_init_net和proto_exit_net只進行了protocols proc檔案的建立,裡面可以檢視協議相關的一些資訊:
## cat /proc/net/protocols
protocol size sockets memory press maxhdr slab module cl co di ac io in de sh ss gs se re sp bi br ha uh gp em
PPPOE 552 0 -1 NI 0 no pppoe n n n n n n n n n n n n n n n n n n n
SCTPv6 1048 1 0 no 0 yes sctp y y y y y y y y y y y y n y y y y y y
SCTP 928 0 0 no 0 yes sctp y y y y y y y y y y y y n y y y y y y
PACKET 896 26 -1 NI 0 no kernel n n n n n n n n n n n n n n n n n n n
PINGv6 752 0 -1 NI 0 yes kernel y y y n n y n n y y y y n y y y y y n
RAWv6 752 0 -1 NI 0 yes kernel y y y n y y y n y y y y n y y y y n n
UDPLITEv6 744 0 -1 NI 0 yes kernel y y y n y y y n y y y y n n y y y y n
UDPv6 744 3 15 NI 0 yes kernel y y y n y n y n y y y y n n y y y y n
TCPv6 1392 1 6 no 304 yes kernel y y y y y y y y y y y y y n y y y y y
UNIX 640 46 -1 NI 0 yes kernel n n n n n n n n n n n n n n n n n n n
UDP-Lite 624 0 -1 NI 0 yes kernel y y y n y y y n y y y y y n y y y y n
PING 592 0 -1 NI 0 yes kernel y y y n n y n n y y y y n y y y y y n
RAW 600 0 -1 NI 0 yes kernel y y y n y y y n y y y y n y y y y n n
UDP 624 27 15 NI 0 yes kernel y y y n y n y n y y y y y n y y y y n
TCP 1272 15 6 no 304 yes kernel y y y y y y y y y y y y y n y y y y y
NETLINK 600 9 -1 NI 0 no kernel n n n n n n n n n n n n n n n n n n n
看來沒什麼實質性的操作 , 還是接下來看看inet_init函式吧