Linux從用戶層到內核層系列 - TCP/IP協議棧部分系列2: 協議棧各種協議的初始化

題記:本系列文章的目的是拋開書本從源代碼和使用的角度分析Linux內核和相關源代碼,byhankswang和你一起玩轉linux開發

輕鬆搞定TCP/IP協議棧,原創文章歡迎交流, [email protected]微笑

歡迎加入到CHLK - Linux開發交流羣 QQ:327084515 討論Linux開發相關問題


TCP協議的初始化(以TCP協議爲入口分析協議棧協議的註冊與使用,其他協議類型觸類旁通)


TCP/IP協議棧初始化開始的時候由fs_initcall函數把inet_init註冊到linux的啓動表項中,TCP協議的初始化也是有inet_init開始的。

從內核的源代碼中可以看到,在inet_init中分別對ARP/IP/TCP/UDP/ICMP協議和IPv4proc部分進行了初始化,對除了TCP協議之外的協議的初始化我們會在其他的博文中介紹。



協議註冊部分請參見博文:TCP/IP協議棧源碼圖解分析系列1:協議的註冊

其中需要補充說明的部分包括:tcp_sockets_allocated、 tcp_orphan_count,其他的部分圖中已經進行了相關的解釋。

A. tcp_sockets_allocated

1. tcp_sockets_allocated 類型的定義

struct percpu_counter {
spinlock_t lock;
s64 count;
#ifdef CONFIG_HOTPLUG_CPU
struct list_head list;/* All percpu_counters are on a list */
#endif
s32 __percpu *counters;
};

struct percpu_counter tcp_sockets_allocated;

2. tcp_sockets_allocated 功能作用

tcp_sockets_allocated 用於對當前TCP sockets數目的統計,相當於TCP的counter, tcp_sockets_allocated 會在tcp_v4_init_sock用於初始化socket的時候加1,其操作函數爲percpu_counter_inc(&tcp_sockets_allocated); 在TCP協議銷燬socket的時候通過tcp_v4_destroy_sock函數調用percpu_counter_dec(&tcp_sockets_allocated)來對tcp_sockets_allocated 的值減1.


B.tcp_orphan_count

tcp_orphan_count 被註冊到tcp_prot.orphan_count中, orphan_count會在tcp_close的時候對無法完成發送的數據包進行統計,對其操作的函數與tcp_sockets_allocated 相同,都爲函數對percpu_counter_inc與percpu_counter_dec。


本文如有紕漏請指出並交流,郵箱[email protected]



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章