AndyChiang
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 計算機網路 Ch03 - Transport Layer ###### tags: `計網` [![](https://img.shields.io/badge/dynamic/json?color=orange&label=總觀看人數&query=%24.viewcount&url=https://hackmd.io/giEe_p7xSwWe53hc7UrkJQ%2Finfo)]() ## Principles of transport layer * multiplexing, demultiplexing * reliable data transfer * flow control * congestion control ## 3.1 transport-layer services ![](https://i.imgur.com/qjBVBV8.png) 建立logical communication關係 送出: application layer -> messages -> segments(封包) -> network layer 接收: network layer -> segments -> messages -> application layer ### Transport vs. network layer ![](https://i.imgur.com/J7jWfZo.png) ### TCP vs. UDP ![](https://i.imgur.com/LDbhryR.png) * congestion control -> 壅塞處理(重點!!) * flow control -> 流量控管 * connection setup ## 3.2 multiplexing and demultiplexing ![](https://i.imgur.com/1DRkuUn.png) * Multiplexing: 一次傳多個 * Demultiplexing: 區分不同的process ![](https://i.imgur.com/lI9UreU.png) 基本上host就是透過IP addresses 和 port numbers來分辨的 ### UDP: Connectionless demux ![](https://i.imgur.com/bKqmazC.png) ### TCP: Connection-oriented demux ![](https://i.imgur.com/gta9SkD.png) ![](https://i.imgur.com/NHTpk6Q.png) ![](https://i.imgur.com/MgpI6pT.png) 一個IP address對一個socket,修復容易 ## 3.3 UDP: User Datagram Protocol connectionless -> 不建立連線 each UDP segment handled independently of others -> 封包彼此獨立 優點: * 延遲小 缺點: * 封包遺失 * 不按照順序 使用協定: * streaming multimedia apps (loss tolerant, rate sensitive) * DNS: 傳送內容不大 * SNMP: 網路設備設定 ### UDP segment header ![](https://i.imgur.com/UNK7Gkt.png) UDP header size: 8 bytes TCP header size: 20 bytes ### UDP checksum ![](https://i.imgur.com/WGhXmyX.png) 比較算出來的checksum和封包中的checksum是否相等: * 不相等: 一定錯 * 相等: 沒有發現錯,但不一定對,因為checksum只取檔案的一小部分,所以可能只是剛好沒找到錯誤而已。 ## 3.4 principles of reliable data transfer ### Reliable data transfer ![](https://i.imgur.com/QzRsRYf.png) reliable data transfer protocol(rdt):資料必須全對,deliver_data才會傳給應用層 ### State ![](https://i.imgur.com/AXPVK4f.png) finite state machines (FSM) 類似正規語言中的DFS ### rdt1.0: reliable transfer over a reliable channel ![](https://i.imgur.com/iBm3Ewr.png) underlying channel perfectly reliable -> 假設都不會錯 * no bit errors * no loss of packets ### rdt2.0: channel with bit errors checksum to detect bit errors acknowledgements **(ACKs)**: receiver explicitly tells sender that pkt received OK -> 接收到正確的封包 negative acknowledgements **(NAKs)**: receiver explicitly tells sender that pkt had errors -> checksum有錯誤,保留封包並要求重傳 ![](https://i.imgur.com/fDLxQgS.png) #### sender 接收ACK: 傳下一份封包,釋放Buffer 接收NAK: 封包重傳 #### receiver checksum沒錯: 回傳ACK checksum有錯: 回傳NAK,捨棄封包 正確的情況: ![](https://i.imgur.com/a96BglT.png) 錯誤的情況: ![](https://i.imgur.com/PbV91JC.png) #### stop-and-wait - sender sends one packet, then waits for receiver response -> 一次只傳一個封包,確保封包的正確性。 ### rdt2.1 - sequence number -> 封包序號,如果是重複的封包需省略。只需要1-bit(0, 1)。 - sender: 停在等待0或1的狀態。 - receiver: 停在等待0或1的狀態。 ![](https://i.imgur.com/u9jERUi.png) ![](https://i.imgur.com/sjgTrgE.png) ### rdt2.2: a NAK-free protocol duplicate ACK at sender results in same action as NAK: retransmit current pkt -> 不需要NAK,利用重複的封包來取代NAK的作用。如果收到重複的封包,則重傳。 ![](https://i.imgur.com/L5ybn6l.png) ### rdt3.0: channels with errors and loss 多考慮到封包遺失的情況: sender加上timer機制,計數從送出封包到接收到NAK/ACK的時間,超過時間就直接重傳。 sender3.0和2.2架構差不多,只多了timer。receiver則沒變。 ![](https://i.imgur.com/K4SMnyj.png) ![](https://i.imgur.com/i75iKxa.png) ![](https://i.imgur.com/yMPLBhH.png) (d) 封包延遲時間超過timeout時間 ![](https://i.imgur.com/GdHgrbk.png) ![](https://i.imgur.com/FTq6F7o.png) ### Pipelined protocols ![](https://i.imgur.com/YPqo9RW.png) 一口氣送多個pkt。 - Go-back-N: 不連續的部分全部捨棄並重傳。 - Selective Repeat: 保留連續的封包,只針對錯誤的部分做重傳。 #### Go-back-N(GBN) ![](https://i.imgur.com/Y2xY5n8.png) cumulative ACK -> 所有 seq number < n 的pkt都已經被ACK。 ![](https://i.imgur.com/hEpbunH.png) ![](https://i.imgur.com/RijU3O9.png) **window size <= seq number** #### Selective Repeat(SR) 一個pkt獨立設置一個timer,timeout只需要重傳該封包就好。 ![](https://i.imgur.com/BfbzcDF.png) ![](https://i.imgur.com/HrOUzeZ.png) 問題: receiver認為0是下一個0的封包,以為3還沒傳。 **window size <= seq number/2** ## 3.5 connection-oriented transport: TCP * 以byte為單位傳輸 * pipeLine: 一次傳好幾個封包 * 雙向溝通 * handshaking: 先建立連線 * flow controlled: 流量控管 ### TCP segment structure ![](https://i.imgur.com/6WaP7Zk.png) * sequence numbers: 傳輸端的第一個 * acknowledgement numbers: 接收端預計接收ACK的第一個 ![](https://i.imgur.com/g6gIIcr.png) ![](https://i.imgur.com/1TGARjg.png) ### TCP round trip time, timeout #### SampleRTT: 預測合理的timeout時間 * timeout太短:多次寄送重複封包,浪費頻寬。 * timeout太長:封包遺失要等待很久才重傳,延遲變長。 因此,計算合理的RTT時間是很重要的一件事。 ![](https://i.imgur.com/10QgRL7.png) $$EstimatedRTT = (1- a)*EstimatedRTT + a*SampleRTT$$ a = 0.125 $$DevRTT = (1-b)*DevRTT + b*|SampleRTT-EstimatedRTT|$$ b = 0.25 $$TimeoutInterval = EstimatedRTT + 4*DevRTT$$ ### reliable data transfer #### sender events * data rcvd from app: 只重傳一個封包 * timeout * ack rcvd: 確認封包正確 ![](https://i.imgur.com/P7AYi1X.png) ![](https://i.imgur.com/3Ftj9oF.png) sender或receiver端會記錄最大seq number,如果接收到的seq number小於最大的seq number,則視為重複封包。 就算ACK100遺失也沒關係,因為sender只要接收到ACK120就能確定100有成功送達。 #### TCP fast retransmit detect lost segments via duplicate ACKs: 收到重複地確認訊息,視為遺失,並重傳。 ![](https://i.imgur.com/CfFHVxY.png)![](https://i.imgur.com/OUHKBU3.png =30%x) 收到三個重複的ACK,直接重傳,不管有沒有Timeout。 ### TCP flow control ![](https://i.imgur.com/dcjqaat.png) 避免因為buffer空間不夠造成封包遺失。 在TCP header中的receive window欄位,receiver透過這個欄位傳送rwnd給sender。 receiver告知sender空閒的Buffer空間(rwnd),還沒被ACK的封包大小必須 <= rwnd。 ### Connection Management 避免序號重複。 ### 2-way handshake ![](https://i.imgur.com/NQxZSEs.png) timeout導致重新發送連線請求,在結束連線後又再連線一次 ### 3-way handshake ![](https://i.imgur.com/fTe9xwZ.png) 三次確認,序號是錯開的。 ![](https://i.imgur.com/rBx9H3B.png) ### closing a connection ![](https://i.imgur.com/UaqMVLR.png) FINbit = 1,關閉連線。 seq用來確認是否為同次連線,x和y不會相同。 等待兩倍週期,如果封包遺失還可以重傳。 ## 3.6 principles of congestion control (壅塞控制) ![](https://i.imgur.com/mrIW1cB.png) ![](https://i.imgur.com/E3jzji9.png) 重傳後檔案反而更大 ![](https://i.imgur.com/fagrbbP.png) 浪費頻寬 ![](https://i.imgur.com/2dC4R4l.png) D遺失資料,浪費了圈起來兩個router的資源。 ## 3.7 TCP congestion control ### 目標 ![](https://i.imgur.com/EiUQJYE.png) * Efficiency: 效率,且不能降低網路使用率。 * Fairness: 公平性,所有的分到的頻寬要是一樣的。 * Distributeness: 分散性 * Convergence and stability: 收斂性,遇到雍塞時可以盡快收斂。穩定性,壅塞時對其他使用者影響不大。兩者互相矛盾。 ### Sample Model ![](https://i.imgur.com/J0ZbdMS.png) 將所有的流量相加,如果大於可用頻寬,就發生壅塞。 ### Linear Contral ![](https://i.imgur.com/N50Uvzo.png) efficiency line: 效率線,線以上的部分發生雍塞 fairness line: 公平線,點越接近線越公平 ### congestion: 發生壅塞 ![](https://i.imgur.com/nwEFLz7.png) efficiency和fairness兩個區塊重疊的部分為紅線。 **紅線:a~D~ = 0,b~D~ > 1 (通過原點,斜率大於1)** ![](https://i.imgur.com/WzqCPeA.png) efficiency和fairness兩個區塊重疊的部分為紅色區塊。 通常會挑垂直於efficiency的紅線。 **紅線:a~I~ > 0,b~I~ = 1 (與x軸交會於正向,斜率等於1)** #### AIMD(Additive Increase & Multiplicative Decrease) 公式總整理 ![](https://i.imgur.com/mwgsKK5.png) ![](https://i.imgur.com/bcwqOB4.png) 網路上的實際情況就是在underload和overload間來來回回,越來越接近fairness線。 ### TCP流量控制 ![](https://i.imgur.com/NIpAUwl.png) * 沒壅塞時...: cwnd一次上升一個RTT(線性上升) * 雍塞時...: cwnd/2(倍數下降) **線性上升,倍數下降 -> 符合AIMD** ![](https://i.imgur.com/ClOlcBE.png) $$LastByteSent - LastByteACKed <= cwnd$$ 大於則無法送出封包。 ![](https://i.imgur.com/02mDOK7.png) 可以用cwnd就算出傳送速率。 ### TCP Slow Start ![](https://i.imgur.com/U2HRVop.png) 為了增加一開始的速度,每次成功接收到ACK訊息,**cwnd就變兩倍**。 ### TCP Congestion Control Summary ![](https://i.imgur.com/SGOviak.png) * MSS:maximum segment size * 當 cwnd < ssthresh 時,進入slow start狀態,cwnd = cwnd + 1 MSS,以指數成長。 * 當 cwnd >= ssthresh 時,進入congestion avoidance狀態,cwnd = cwnd + MSS*(MSS/cwnd),以線性成長。 * 當接收三個重複ACK(fast retransmit)時,進入fast recovery狀態,ssthresh = cwnd/2,cwnd = ssthresh + 3 MSS。 * 當發生timeout時,回到slow start狀態,ssthresh = cwnd/2,cwnd = 1 MSS。 * fast recovery狀態下,收到重複ACK,則cwnd = cwnd + 1 MSS。 ![](https://i.imgur.com/B7AFI4k.jpg) ### TCP throughput ![](https://i.imgur.com/LQgpYbI.png) 平均window大小為¾W ### TCP Fairness 總頻寬為R,每個人平均分到R/K。 AIMD即可解決。 不公平的情況: 和UDP競爭不利、一個host同時建立多個TCP連線。 ### 補充: Explicit Congestion Notification (ECN) ![](https://i.imgur.com/TlgseBZ.png) 交給Router做壅塞控制,並在header和ACK訊息中加上ECN欄位告訴source和destination有無發生雍塞。

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully