ops- > bind (sock, (struct sockaddr *) |inet_bind|inet_csk_get_port 中間的流程暫且不看,本文主要分析最重要的函數(shù):inet_csk_get_port,從該函數(shù)出發(fā)了解本地綁定端口如何管理。 以下是原函數(shù),通過下文章節(jié)逐步分段分析 int inet_csk_get_port ( struct sock *sk, unsigned short snum ) { bool reuse = sk- >sk_reuse sk- >sk_state != TCP_LISTEN; struct inet_hashinfo *hinfo = sk- >s" />
0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Linux bind的核心執(zhí)行函數(shù)

麥辣雞腿堡 ? 來源:技術(shù)簡說 ? 作者:董旭 ? 2023-07-31 10:51 ? 次閱讀

bind的核心執(zhí)行函數(shù)

bind系統(tǒng)調(diào)用的核心函數(shù)調(diào)用流程如下:

SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
|
sock- >ops- >bind(sock,
            (struct sockaddr *)
            &address, addrlen);
|
inet_bind
|
inet_csk_get_port

中間的流程暫且不看,本文主要分析最重要的函數(shù):inet_csk_get_port,從該函數(shù)出發(fā)了解本地綁定端口如何管理。

以下是原函數(shù),通過下文章節(jié)逐步分段分析

int inet_csk_get_port(struct sock *sk, unsigned short snum)
{
 bool reuse = sk- >sk_reuse && sk- >sk_state != TCP_LISTEN;
 struct inet_hashinfo *hinfo = sk- >sk_prot- >h.hashinfo;
 int ret = 1, port = snum;
 struct inet_bind_hashbucket *head;
 struct net *net = sock_net(sk);
 struct inet_bind_bucket *tb = NULL;
 kuid_t uid = sock_i_uid(sk);

 if (!port) {
  head = inet_csk_find_open_port(sk, &tb, &port);
  if (!head)
   return ret;
  if (!tb)
   goto tb_not_found;
  goto success;
 }
 head = &hinfo- >bhash[inet_bhashfn(net, port,
       hinfo- >bhash_size)];
 spin_lock_bh(&head- >lock);
 inet_bind_bucket_for_each(tb, &head- >chain)
  if (net_eq(ib_net(tb), net) && tb- >port == port)
   goto tb_found;
tb_not_found:
 tb = inet_bind_bucket_create(hinfo- >bind_bucket_cachep,
         net, head, port);
 if (!tb)
  goto fail_unlock;
tb_found:
 if (!hlist_empty(&tb- >owners)) {
  if (sk- >sk_reuse == SK_FORCE_REUSE)
   goto success;

  if ((tb- >fastreuse > 0 && reuse) ||
      sk_reuseport_match(tb, sk))
   goto success;
  if (inet_csk_bind_conflict(sk, tb, true, true))
   goto fail_unlock;
 }
success:
 if (hlist_empty(&tb- >owners)) {
  tb- >fastreuse = reuse;
  if (sk- >sk_reuseport) {
   tb- >fastreuseport = FASTREUSEPORT_ANY;
   tb- >fastuid = uid;
   tb- >fast_rcv_saddr = sk- >sk_rcv_saddr;
   tb- >fast_ipv6_only = ipv6_only_sock(sk);
   tb- >fast_sk_family = sk- >sk_family;
#if IS_ENABLED(CONFIG_IPV6)
   tb- >fast_v6_rcv_saddr = sk- >sk_v6_rcv_saddr;
#endif
  } else {
   tb- >fastreuseport = 0;
  }
 } else {
  if (!reuse)
   tb- >fastreuse = 0;
  if (sk- >sk_reuseport) {
   if (!sk_reuseport_match(tb, sk)) {
    tb- >fastreuseport = FASTREUSEPORT_STRICT;
    tb- >fastuid = uid;
    tb- >fast_rcv_saddr = sk- >sk_rcv_saddr;
    tb- >fast_ipv6_only = ipv6_only_sock(sk);
    tb- >fast_sk_family = sk- >sk_family;
#if IS_ENABLED(CONFIG_IPV6)
    tb- >fast_v6_rcv_saddr = sk- >sk_v6_rcv_saddr;
#endif
   }
  } else {
   tb- >fastreuseport = 0;
  }
 }
 if (!inet_csk(sk)- >icsk_bind_hash)
  inet_bind_hash(sk, tb, port);
 WARN_ON(inet_csk(sk)- >icsk_bind_hash != tb);
 ret = 0;

fail_unlock:
 spin_unlock_bh(&head- >lock);
 return ret;
}
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • Linux
    +關(guān)注

    關(guān)注

    87

    文章

    11123

    瀏覽量

    207921
  • 函數(shù)
    +關(guān)注

    關(guān)注

    3

    文章

    4237

    瀏覽量

    61971
  • 系統(tǒng)
    +關(guān)注

    關(guān)注

    1

    文章

    1002

    瀏覽量

    21219
收藏 人收藏

    評論

    相關(guān)推薦

    Linux源碼分析bind系統(tǒng)調(diào)用

    眾所周知,一個Server端Socket的建立,需要socket、bind、listen、accept四個步驟。
    的頭像 發(fā)表于 10-16 11:08 ?3124次閱讀
    從<b class='flag-5'>Linux</b>源碼分析<b class='flag-5'>bind</b>系統(tǒng)調(diào)用

    linux中的busybox,是否帶有bind和ifenslave?

    想問下,飛凌提供的linux中的busybox,是否帶有bind和ifenslave?如果沒有有什么辦法可以添加?
    發(fā)表于 01-11 06:53

    Bind源代碼包安裝

    先到官方下載Bind的安裝包 wgetftp://ftp.isc.org/isc/bind9/9.6.0-P1/bind-9.6.0-P1.tar.gz tar xzvf
    發(fā)表于 04-04 20:30 ?23次下載

    linux c函數(shù)庫參考手冊_徐千洋

    書中整理了超過400個Linux常用的函數(shù),每個函數(shù)以整齊劃一的體例列出了函數(shù)名稱、相關(guān)函數(shù)、使用的表頭文件、
    發(fā)表于 11-29 17:12 ?0次下載

    Linux C函數(shù)手冊

    Linux C函數(shù)庫參考,一本linux學(xué)習(xí)的參考備查手冊
    發(fā)表于 11-03 17:33 ?0次下載

    linux_C函數(shù)庫中文手冊

    linux_C函數(shù)庫中文手冊linux_C函數(shù)庫中文手冊
    發(fā)表于 03-20 10:42 ?14次下載

    Linux C函數(shù)參考手冊

    Linux C函數(shù)參考手冊
    發(fā)表于 10-25 15:32 ?6次下載
    <b class='flag-5'>Linux</b> C<b class='flag-5'>函數(shù)</b>參考手冊

    Linux教程之Linux C函數(shù)參考教程免費下載

    本文檔的主要內(nèi)容詳細(xì)介紹的是Linux教程之Linux C函數(shù)參考教程免費下載。
    發(fā)表于 03-20 08:00 ?4次下載
    <b class='flag-5'>Linux</b>教程之<b class='flag-5'>Linux</b> C<b class='flag-5'>函數(shù)</b>參考教程免費下載

    Linux下進(jìn)程的創(chuàng)建、執(zhí)行和終止

     許多操作系統(tǒng)提供的都是產(chǎn)生進(jìn)程的機制,也就是說,首先在新的地址空間里創(chuàng)建進(jìn)程、讀入可執(zhí)行文件,后再開始執(zhí)行。Linux中進(jìn)程的創(chuàng)建很特別,它把上述步驟分解到兩個單獨的函數(shù)中去
    發(fā)表于 06-11 09:21 ?585次閱讀

    如何使用Arduino millis函數(shù)執(zhí)行多任務(wù)處理

    在本教程中,我們將學(xué)習(xí)Arduino 如何使用 Arduino millis 函數(shù)執(zhí)行多任務(wù)處理。通常在 Arduino 中使用delay()函數(shù)執(zhí)行LED 閃爍等周期性任務(wù),但此
    的頭像 發(fā)表于 09-06 14:41 ?1.3w次閱讀
    如何使用Arduino millis<b class='flag-5'>函數(shù)</b><b class='flag-5'>執(zhí)行</b>多任務(wù)處理

    什么是bind?你真的熟悉bind嗎?

    bind()方法創(chuàng)建一個新的函數(shù),在bind()被調(diào)用時,這個新函數(shù)的this被指定 bind()的第一個參數(shù),而其余參數(shù)將作為新
    的頭像 發(fā)表于 07-13 09:56 ?2462次閱讀

    bind系統(tǒng)調(diào)用背后的端口管理復(fù)用

    bind系統(tǒng)調(diào)用進(jìn)行分析,主要是了解一下bind背后,Linux內(nèi)核是如何進(jìn)行端口綁定、如何管理本地眾多的端口號。 先直觀感受bind系統(tǒng)調(diào)用背后的端口管理、端口復(fù)用 # inclu
    的頭像 發(fā)表于 07-31 10:45 ?465次閱讀
    <b class='flag-5'>bind</b>系統(tǒng)調(diào)用背后的端口管理復(fù)用

    Linux內(nèi)核分析 端口哈希桶

    是用來封裝各種協(xié)議的綁定哈希表,具體定義如下所示,這個結(jié)構(gòu)體在[Linux內(nèi)核角度分析服務(wù)器Listen細(xì)節(jié)中介紹過,具體地,struct inet_bind_hashbcket是bind相關(guān)的哈希桶
    的頭像 發(fā)表于 07-31 11:03 ?632次閱讀
    <b class='flag-5'>Linux</b>內(nèi)核分析 端口哈希桶

    Linux內(nèi)核分析 bind端口選擇

    端口選擇 繼續(xù)看inet_csk_get_port函數(shù): 在端口選擇前, 先確定當(dāng)前該socket的“屬性”,即是否可以端口復(fù)用,是否在TCP_LISTEN狀態(tài),以便后面插入到桶隊列時設(shè)置
    的頭像 發(fā)表于 07-31 11:08 ?825次閱讀

    SCP固件執(zhí)行步驟和模塊間通信

    SCP固件執(zhí)行步驟 運行前階段:按固定順序排列的5個階段 ? 模塊初始化:框架使用模塊配置數(shù)據(jù)調(diào)用的模塊的.init()函數(shù)。 ? 元素初始化:帶有元素配置數(shù)據(jù)的框架調(diào)用的模塊
    的頭像 發(fā)表于 11-02 17:02 ?691次閱讀
    SCP固件<b class='flag-5'>執(zhí)行</b>步驟和模塊間通信