端口哈希桶
在inet_csk_get_port函數(shù)中的變量聲名中有如下幾個(gè)結(jié)構(gòu)體:
struct inet_hashinfo *hinfo = sk- >sk_prot- >h.hashinfo;
struct inet_bind_hashbucket *head;
struct inet_bind_bucket *tb = NULL;
其中strcut inet_hashinfo是用來(lái)封裝各種協(xié)議的綁定哈希表,具體定義如下所示,這個(gè)結(jié)構(gòu)體在[Linux內(nèi)核角度分析服務(wù)器Listen細(xì)節(jié)中介紹過(guò),具體地,struct inet_bind_hashbcket是bind相關(guān)的哈希桶,bhash_size是bind哈希桶的大小。
struct inet_hashinfo {
struct inet_ehash_bucket *ehash;
spinlock_t *ehash_locks;
unsigned int ehash_mask;
unsigned int ehash_locks_mask;
struct inet_bind_hashbucket *bhash;
unsigned int bhash_size;
struct inet_listen_hashbucket listening_hash[INET_LHTABLE_SIZE]
____cacheline_aligned_in_smp;
};
struct inet_bind_hashbcket哈希桶的具體定義如下,其中chain代表著各個(gè)桶的哈希隊(duì)列,用來(lái)鏈接具有同一哈希值的哈希元素
struct inet_bind_hashbucket {
spinlock_t lock;
struct hlist_head chain;
};
具體每個(gè)桶結(jié)構(gòu)是struct inet_bind_bucket:
struct inet_bind_bucket {
possible_net_t ib_net;
unsigned short port;
signed char fastreuse;
signed char fastreuseport;
kuid_t fastuid;
#if IS_ENABLED(CONFIG_IPV6)
struct in6_addr fast_v6_rcv_saddr;
#endif
__be32 fast_rcv_saddr;
unsigned short fast_sk_family;
bool fast_ipv6_only;
struct hlist_node node;
struct hlist_head owners;
};
初次看到這幾個(gè)結(jié)構(gòu)體可能比較亂,下面用圖進(jìn)行描述:
由上圖所示,每個(gè)綁定的端口號(hào)經(jīng)過(guò)哈希計(jì)算都會(huì)掛在相應(yīng)的chain鏈表上,chain鏈表上是一個(gè)個(gè)的桶結(jié)構(gòu),同一個(gè)chain上的節(jié)點(diǎn)具有相同的哈希值(通過(guò)端口號(hào)計(jì)算),桶結(jié)構(gòu)inet_bind_bucket包含對(duì)應(yīng)的端口號(hào)port、owners等信息,owners對(duì)應(yīng):該端口號(hào)對(duì)應(yīng)的tcp_sock實(shí)例,如果該port支持復(fù)用,那么owners可能掛著多個(gè)tcp_sock節(jié)點(diǎn)。
在struct inet_bind_bucket中有一個(gè)關(guān)鍵的成員:signed char fastreuse
為了避免每次都遍歷 inet_bind_bucket 的 owners 字段 來(lái)獲知是否所有的 sock 都設(shè)置了 sk_reuse 字段,并且不是在 TCP_LISTEN 狀態(tài)。在 inet_bind_bucket 結(jié)構(gòu)體中設(shè)置了 fastreuse 字段。如果 owners 沒(méi)有元素,那么這 個(gè)字段為真。此后每次添加一個(gè)新的 sock 到 owners 中的時(shí)候,如果它設(shè)置了 sk_reuse 并且不在 TCP_LISTEN 狀態(tài),就維持 fastreuse 為真,否則設(shè)置它為假。
-
內(nèi)核
+關(guān)注
關(guān)注
3文章
1416瀏覽量
41421 -
Linux
+關(guān)注
關(guān)注
87文章
11509瀏覽量
213748
發(fā)布評(píng)論請(qǐng)先 登錄
一文詳解Linux內(nèi)核源碼組織結(jié)構(gòu)
Linux的內(nèi)核教程
Android內(nèi)核分析
linux內(nèi)核啟動(dòng)內(nèi)核解壓過(guò)程分析
基于Linux 2.6內(nèi)核Makefile分析

關(guān)于Linux 2.6內(nèi)核Makefile的分析
Linux最新2.6內(nèi)核的Makefile體系詳細(xì)資料分析
linux內(nèi)核是什么_linux內(nèi)核學(xué)習(xí)路線
linux內(nèi)核參數(shù)設(shè)置_linux內(nèi)核的功能有哪些

評(píng)論