###### tags: `university note` [TOC] # 電腦網路 ___ # CH1 ## 1.1 Internet introduction ### Internet連接描述 host=end system(PC, Server, Data center...)執行network apps * Communication link(通信鏈路, phsical layer!?) * fiber(光纖), copper(銅線),Coaxial cable(同軸電纜) * transimission rate(bit/s,bps):不同鏈路有不同**bandwidth(頻寬)** * Packet Switch(分組交換機) * **Router: 封包轉送** * Switch: * Protocol(協議) Control sending and receiveing messages format * **TCP,IP**,HTTP,802.11,FTP,PPP... * ISP(網路服務提供商) * 分層,分級 * 每個ISP網路都是獨立管理,運行一定的IP protocol(**IP不夠問題->NAT技術(Private Network)**) * Internet Standards * RFC : Request for comments * IETF : Internet Engineering Task Force(制定Protocol的單位) ![](https://hackmd.io/_uploads/H1ojbNAC2.png) 網路提供app連線的基礎和端點互連的接口 ___ ### Protocol Intro :::info **Protocols define:** * format/order of messeages sent and received * actions taken on messages transmmision ::: :::info **Internet Structure** * Network edge: hosts, servers * Access networks: Home Access(DSL, Cable, FTTH), Institutional Access(Ethernet, WiFi), Wide-Area Wireless Access(3G,4G,5G) * Physical media: coaxial cable, 雙絞線, fiber optics * Edge router: The router connect to other network * Network core: routers, networks of networks * switching/routing: packet transmission * Circuit switching: 網路資源分配(FDM, TDM) * Packet switching: 所有人的package共享所有網路資源 * Internet structure: ISP, networks of networks ![](https://hackmd.io/_uploads/rkPd81uJ6.png) ::: ___ ## 1.2 Network Edge ### Access Networks #### Cable-based access(有線電視上網) ![](https://hackmd.io/_uploads/ryxXdJukp.png) use splitter and modem to transform signal from cable headend(有線電視前端) to PC modem: $\text{analog signal}\iff\text{digital signal}$ :::info Circuit Swithing(Will deeply introduce in **Network Core**) * **FDM(Frequency Division Multiplexing/分類多路複用):** Divide a full frequency range into different channel(分頻率) e.g. 一銅線100MHz~1000MHz,將其切割成10個Channel每個Channel可以運用到100MHz的range(其中會保留一些預防over-lapping) * **TDM(Time-division multiplexing)**: user can use all the frequency with dividee time(分時間,時間內固定使用者可用所有頻寬) ::: :::danger * **homes share acess network**多家共用網路(搶頻寬) * Security: 線路攔截 ::: ___ #### DSL(Digital Subscriber Link)數位用戶線路 obtain internet access from same telco which provide local phone access * upstream: 3.5~16Mbps * downstream: 24~52Mbps ![](https://hackmd.io/_uploads/Bkw_HGbe6.png) **DSLAM**:access mulitplexer(資料回傳依需求分Internet/Phone network) ___ #### Home and Wireless Access Networks ![](https://hackmd.io/_uploads/B1ljvGbl6.png) * WLANs(Wireless Local Area Networks) * 小區域(~100m) * WiFi 802.11b/g/n: 11/54/450Mbps * Wide-area Cellular Access Networks * 大區域(~10km), 蜂巢式網路(基地台) * 10's Mbps * 4G/5G cellular networks(行動上網) ![](https://hackmd.io/_uploads/SJ5thz-ep.png) ___ #### Enterprise Networks * mixed of wired, wireless technologies * Ethernet: 100Mbps, 1Gbps, 10Gbps * WiFi: 802.11b/g/n * connecting mix of switches and routers ___ #### Data Center Networks * connect hundred to thousands server * high bandwidth link(10~100Gbps) ___ ### Host Send Packets Host take app message and break it into several packets with length $L$. Then, host transmit packets into access networks at $R$ transmiision rate ![](https://hackmd.io/_uploads/HkhlLX-lT.png) **Packet transmission delay(sec)** $=\frac{L(bits)}{R(bits/s)}$ ___ ### Link Communication guided/unguided media #### Twisted Pair(TP)雙絞線 2 insulated(絕緣) copper wires * CAT5: 100Mbps, 1Gbps(CAT5e) * CAT6: 10Gbps ![](https://hackmd.io/_uploads/rklMuXZxp.png) ___ #### Coaxial cable同軸電纜 2 concentric copper conductors * bidirectional(雙向) transmit wire * 寬頻大 * multiple frequency channels on cable(FDM) * 100Mbps per channel **HFC(Hybrid Fiber Coax/混合光纖同軸):** 非對稱網路速度: Downstream: 40Mbps~1.2Gbps, Upstream: 30~100Mbps ___ #### Fiber Optic Cable glass fiber carrying light pulses * repeaters spaced far apart * **immune to electromagnetic noise** * 10~100Gbps point-to-point transmission ___ #### Wireless Radio * half-duplex(半雙工) * environment effect propagation :::info Radio link types: **WiFi** * 10~100Mbps * ~10m range **Mobile network(4G,5G)** * ~10Mbps(4G) * ~10km **Bluetooth**: * short distances **Terrestrial microwave** * 45Mbps channels **Satellite** * <100Mbps(Starlink) * 270msec end-end delay ::: ___ ## 1.3 Network Core * Forwarding(local): move packets from input routers to appropriate output router * Routing(global): determine paths taken by packets ### Packet Switching(分封交換) #### Store and forward * Packet Transmission delay $s=\frac{L}{R}$ * 封包的所有內容都傳到router才會繼續傳(store and forward) ___ #### Queueing Router **output rate < input rate**, might happend queueing ![](https://hackmd.io/_uploads/r1RJar9l6.png) * **packets be dropped(丟包)** if buffer in router fills up ___ ### Circuit Switching end-end create connectoin before transimssion(兩端點建立連線後才傳輸) * 資源不共享 * 傳輸快,不易loss,容錯率低 * 容易形成佔線問題(waste) ex: traditional telephone network #### FDM and TDM ##### FDM(Frequency Division Multiplexing) 資源依頻率平均分配給每一位使用者 ##### TDM(Time Division Multiplexing) 時間內使用者可使用所有資源 ![](https://hackmd.io/_uploads/S1H3JFQ-p.png) ___ ### Circuit v.s. Packet :::danger Circuit Switching常用於電話網路 Packet Switching常用於電腦網路 ::: #### Packet Switching problems * Packet Switching適用於有時idle有時大量使用的網路(due to no call setup in Circuit switching) * Excessive congestion possilbe need protocols for **reliable data transfer**, **congestion control** ___ ### network of networks hosts connect to internet via access ISPs ![](https://hackmd.io/_uploads/HJ07-qX-p.png) * Tier-1 ISPs: 整個網路的拓樸(網路架構方式) ___ ## Packet Loss/Delay/Throughput Router buffer is full -> packet loss ### Packet Delay * Processing delay * The time check packet's error code and header to detemine ouput router.(封包解析時間) * Queueing delay * **time waiting for transmission** in router * depend on congestion level of router(路由器內的資料量) * $d=\frac{L\cdot a}{R}, \text{L:pactket length, a: average packet arrival time, R:bit transimission rate}$ ![](https://hackmd.io/_uploads/HyyKmqQb6.png) * Transimission delay * The time **transimit packets to medium link from route**(封包傳輸時間) * $d=\frac{L}{R}$ * Propogation dealy(傳播延遲) * the delay time last bit in a packet propogate from A to B * 不同medium有不同訊號傳播速度 * $\frac{d}{s}, \text{d:length of physical link, s:propogation speed}(2\cdot 10^8\sim 3\cdot 10^8)$ ![](https://hackmd.io/_uploads/H1TcAS9lT.png) :::info **Time Graph for transimission and propogation delay**: ![](https://hackmd.io/_uploads/rkI_GKmWp.png) ::: ___ #### Internet Delay and Routes ##### TraceRoute Program(探測網路延遲) 1. sends 3 packets(probes) will reach router i towards destination 2. router i return packets to sender 3. sender measure time between transimission and reply ![](https://hackmd.io/_uploads/SyfFH5m-T.png) :::info cmd tracert [web link] 可以去玩玩看 ::: ___ ### Throughput(吞吐量) rate at which bits being sent from sender to receiver ![](https://hackmd.io/_uploads/SkwpF9XWp.png) :::info 2 cases for $R_s, R_c$ 1. $R_s<R_c$ 2. $R_s>R_c$ ![](https://hackmd.io/_uploads/B1xi5cmZa.png) We call the one constrains the throughput **"bottleneck link(瓶頸鏈路)"** ::: ___ ## Protocol layers, service models protocol layers(協定層): * 明確地建構與定義這些複雜的區塊,模組化方便我們維護及更新 * 當我們在變動其他層的服務時,不會影響其他的系統。 ___ ### Internet Layer * layer1 - physical(bits) * bits on the wire * layer 2 - link(frame) * data transfer between neighboring network elements * 乙太, Wifi, PPP * layer 3 - network(datagram) * routing of data from source to destination(來源到目的的傳送) * layer 4 - transport(segment) * process-process data transfer * TCP, UDP(人到人之間的傳送) * layer 5 - application(message) * supporting network applictions * FTP, HTTP, SMTP ![](https://hackmd.io/_uploads/HJ4byomba.png) ![image](https://hackmd.io/_uploads/H1a1yENokx.png) ___ # CH2 ## 2.1 Principles of Network Applications :::info Applications * run on end system * communicate over netwrok * 不需要在此層去規範network core(Internet layer作用) ::: ### Client-Server(主從式架構) ![](https://hackmd.io/_uploads/r1DzUdBG6.png) #### Server * always on * in data centers, 方便擴展 * 具備API, 資料庫 * 固定IP #### Client * contact with server * 可以間歇地(intermittently)和server連線 * 浮動IP * 不會有Client-Client connection * 提供UI(User Interface) :::danger Scalability - 當使用者增加server端需要有更多的資源提供服務。如:運算能力、頻寬、儲存空間等 Reliability - 整個網路將依賴於高負載的server端以便運行,當server出問題時,服務即無法正常運作 ::: EX:HTTP, IMAP, FTP ___ ### Peer-Peer * 每個peer都同時為client與server * no always on * Peers可以互相連接傳輸和接收資料(熱度) * 間歇地連線+浮動IP EX:BitTorrent ___ ### 應用程式通訊方式(Process, Socket) Process can be: **Client Process** and **Server Process** * 不同host傳送資料(process+socket) * 同一host傳送資料(process with inter-process communication) :::info 同一主機上的進程通訊有進程間通訊機制(IPC),由作業系統提供:管道、消息隊列、共享記憶體、訊號量 ![](https://hackmd.io/_uploads/HJ3NtOHza.png) [IPC](https://hackmd.io/@YiZjennnnn/OS_Note/https%3A%2F%2Fhackmd.io%2F%40YiZjennnnn%2Fipc_interprocess_communication?type=book) 不同主機間的進程通訊:使用套接字(Socket),也稱為網路編程的API [電腦網路複習筆記](https://codingnote.cc/zh-tw/p/334921/#3-%E6%B7%B7%E5%90%88%E7%B5%90%E6%A7%8B) ::: #### Socket 主機中位於應用層和傳輸層之間的介面,也稱為應用程式設計介面(API) 開發者對於應用層端有大部分控制權,而關於傳輸層只能**選擇protocols&修正Socket參數** ![](https://hackmd.io/_uploads/Byx4f62-a.png) ___ #### Addressing Process process have identifier to let other clients can receive messages: * IP address(xxx.xxx.xxx.xxx->32bits IP位址) * Port numbers(:xxxxx) * EX: HTTP/80, mail/25 Port Numbers是主機上每個需要通訊的進程分配一個埠號 ___ ### 應用層Protocol 網路應用需遵循應用層協議 * 公開協議 :由RFC(Request For Comments)定義且大家都有存取權(具有互通性) * EX: HTTP, SMTP * 私有協議 : 多數P2P共享文件應用(私人具專用性)->pay * EX: Skype, Zoom :::info * 消息類型(type):request, response * 消息語法(syntax) * 欄位語義(semantics) * 規則(rules): when&how process send&respond to messages ::: ___ #### Important Variable for Transport Service 根據app需求會有不同取捨 * Data integrity(資料完整性) * Timing(傳輸時間) * Throughput(流通量)->串流需要很大及時流量 * Security ![](https://hackmd.io/_uploads/BJ1c3uHGa.png) ___ ### Application對TCP/UDP的選擇 * TCP * Flow Control(流量控制): 不會超出流量傳輸限制 * Congestion Control(壅塞控制): 若傳輸堵塞會降低傳輸速度以免丟包 * Connection-Oriented: 事先建立TCP連線在Server-Client * Securing TCP * Provide Encrypted TCP connections * UDP * 傳輸快 ![](https://hackmd.io/_uploads/rkDcTanZp.png) ![](https://hackmd.io/_uploads/r1f9ZYSMp.png) ___ ## 2.2 Web and HTTP 使用**HTTP(Hypertext Transfer) Prtocol**搭配**TCP**傳輸網頁資料給Request的Client 1. 伺服器在80埠等待客戶請求 2. 瀏覽器發起TCP連接請求 3. 伺服器接受TCP連接 4. 交換HTTP消息 5. 關閉TCP連接 **響應時間 :2RTT(建立TCP連線+回傳html)+文件發送時間** ![](https://hackmd.io/_uploads/BkuhUKSzp.png) :::spoiler 詳細Server-Client網頁傳輸流程圖 ![](https://hackmd.io/_uploads/Sk3YDKrfp.png) ![](https://hackmd.io/_uploads/HkHCvKBG6.png) 重複步驟1~5直到網站內容都傳輸到 ::: :::danger HTTP is **stateless**(不會記得之前Client的Request) 每一次都只會專注於當下的Request&Respond If protocol can maintain state -> It'll be complex to maintain And if server/client crash, the state might be inconsistent ::: ___ ## Non/persistent HTTP RTT: 一個封包在client-server間傳輸的時間 HTTP messages大致可分為兩種: **requeste, response** ![](https://hackmd.io/_uploads/rkUlEVyXp.png) ![](https://hackmd.io/_uploads/SyXQMNJX6.png) :::info 以前一個網站+10img的案例 * Nonpersistent: 22次RTT+檔案傳輸時間 * Persistent: 2次RTT+檔案傳輸時間 ::: ### Nonpersistent(HTTP1.0) * GET/POST/HEAD * 每一次接收html的資料都要重新連接一次TCP(2RTT) ___ ### Persistent(HTTP2.0) * 每次可傳輸多個對象 * 支援**Persistent connection**和**Pipeline** * 新增method PUT/PATCH/OPTIONS/DELETE ![](https://hackmd.io/_uploads/HkYD5SymT.png) ___ ### Request&Response https://yakimhsu.com/project/project_w4_Network_http.html #### Request * Get: 單純的跟 server 要一個連結或圖片(Retrieve data in server) * Post: * Head: 只獲取header * Put: 取代整個request * Patch: 修改部分request * Delete: 刪除資源 ![](https://hackmd.io/_uploads/rka09ry7T.png) ___ #### Response ![](https://hackmd.io/_uploads/SyB06rJ7T.png) ![image.png](https://hackmd.io/_uploads/Sy_3JzNmT.png) 回傳會得到 * connected status * header * data files ___ ### Cookies 使用cookies讓網站變成可以儲存紀錄狀態,使用cookies可以有一些效果: * Identify users(辨別用戶) * restrict user acess(分用戶權限) * 可作為中繼點 * 偵測網站喜好(recommendation) * user session state ___ **cookie technology has four components:** 1. cookie header line in the HTTP response message 2. cookie header line in the HTTP request message 3. cookie file kept on the user’s end system(manage by client's browser) 4. back-end database at the Web site ![image.png](https://hackmd.io/_uploads/S1itSf4mp.png) EX: 連結到某購物網站後(first party cookies),伺服器可透過Cookies追蹤使用者點選的內容,而推薦廣告(third party cookies) ___ ### Web Cache(Proxy Server) 類似CPU中Memory cache的功能,同時是Server/Clients * object in cache: cache returns object to client * object not in cache: cache requests object from origin server回傳client,然後記錄一份在cache ![image.png](https://hackmd.io/_uploads/SJQniMN7T.png) ___ usecs(微秒) ![image.png](https://hackmd.io/_uploads/HkPrfmVXp.png) ![image.png](https://hackmd.io/_uploads/S1Z3VQ4mp.png) ___ ### Conditional GET 使用者從Proxy server獲取的東西可能不是最新的(Origin Server在緩存儲存後還有修改) 這時可以使用Conditional GET確保Client得到的是最新的 ![image.png](https://hackmd.io/_uploads/BkJKIXV7a.png) ___ ## 2.3 E-mail * user agents * compose, edit, read mail * EX: Outlook * mail servers * 儲存outgoing, incoming mail * mailbox: contains incoming mail for user(individual for every users) * message queue: queue for outgoing mails * transfer protocol(SMTP) * client: 寄mail端 * server: 接收mail端 * client&sever可為user agents/mail server取決於誰是寄/接收端 ___ ### SMTP RFC * Usually use TCP connection, **port 25** * Direct transfer from source server to destination server * A mail message might be divide into multiplepart message(TCP segments) * use **persistent connection** * 編碼:7-bit ASCII #### RFC 2822 Defines syntax for email messages ![image](https://hackmd.io/_uploads/Sk3B2n-46.png) ___ #### Transfer phases: SMTP handshaking(greeting) -> SMTP transfer message -> SMTP close SMTP server use CRLF.CRLF(\r\n in ASCII) to deternmine end of message. ![image](https://hackmd.io/_uploads/rJNlvn-Ep.png) ![image](https://hackmd.io/_uploads/SkBMF3W4a.png) ___ 讀取信件可用 * HTTP: gmail, yahoo mail * IMAP: 用戶端和server端同步 * POP3: 用戶端和server端不同步 ![image](https://hackmd.io/_uploads/SJGw33-NT.png) ___ ## 2.4 DNS Server [DNS learning](http://dns-learning.twnic.net.tw/dns/02ArchDNS.html#two) * Distributed database in hierarchy of many name server(通常由多個階層式name server組成) * EX: yahoo.com.tw 這個域名,可以拆解成 .(root) 根域名 、.tw 頂級域名、 .com.tw 次級域名 ![image](https://hackmd.io/_uploads/S1PKza-ET.png) ___ ### DNS Services :::info **if DNS is centralize...** * 單點fail就爛掉 * 中央server可能在海外,很遠 * 全世界有超多DNS查詢,traffic volume problem * maintain ::: * hostname -> IP address * host/mail aliasing(用綽號方便查找?) * load distribution(分流) * 每個域名都有多個Name server去分散紀錄IP addresses 對URL域名一個一個找回去 ![image](https://hackmd.io/_uploads/ByjEb6ZVp.png) ___ #### Domain level * Root: * Managed by ICANN * 全球有13個但可以複製來分擔工作 * Top-level: * TLD DNS server * e.g.: .com, .org, .net, .cn, .uk, .jp * Authoritative: * organization's own DNS server ___ ### DNS name resolution(DNS運作) * Interactive Query * DNS server間查詢 * Response: 最後requested的ip/下一層級控制此位址的DNS ![image](https://hackmd.io/_uploads/B18CdkoNp.png) * Recursive Query * 常用client-localDNS * 查詢送出後等待正確位址的回應 ![image](https://hackmd.io/_uploads/H1dJKko4a.png) ___ **DNS實際運作EX** ![image](https://hackmd.io/_uploads/Byx8Lyo46.png) name resolver對local DNS server都是recursive query,而DNS server之間的 query多是iterative ___ ### Caching DNS 每個DNS都會將查詢過的Domain Name給cache起來,所以每個DNS都有快取名稱伺服器的功能(to improve response time) * caches will **expire aftr TTL** * TLD servers always cached in local name servers * **cached data might out-of-date** due to host change IP ___ ### DNS Protocol ![image](https://hackmd.io/_uploads/BkM8nJj46.png) ![image](https://hackmd.io/_uploads/r17wnyoV6.png) ![image](https://hackmd.io/_uploads/HyBpiys4T.png) ___ # CH3 [有料](http://waterbalanced.blogspot.com/2014/11/blog-post_29.html) ## Multiplexing&Demuliplexing * Multiplexing: 用於處理多個socket內的資料並加上傳輸header(transport header) * Demultiplexing: 用header資訊來傳輸要被接收的區端到正確的socket Host use **IP&port** to direct segment to appropriate socket ![image](https://hackmd.io/_uploads/r1cdCG4Ba.png) ___ ### UDP Demultiplexing 2 components in datagram * Destination IP addr * Destination port same destination port but fron different IP addr **will still be direct to same socket** (較不可信) ![image](https://hackmd.io/_uploads/By5WMQVS6.png) ___ ### TCP Demultiplexing 4 components in datagram * Source IP addr * Source port * Destination IP addr * Destination port ![image](https://hackmd.io/_uploads/ry4LfX4Bp.png) 每筆message用不同sockets ___ ### UDP ![image](https://hackmd.io/_uploads/SyynLNVHp.png) * 常用於DNS, SNMP, HTTP, Streaming * checksum(16bits): sender端和receiver端相加若有17bit捕到第一位,然後取反 ___ ## Reliable Data Transfer ### RDT Principles It's acutally unreliable between transport and network layer, so we have to use a protocol to make it reliable! ![image](https://hackmd.io/_uploads/B1PXrENr6.png) * 增加收送端的可靠傳輸協定(rdt) * use FSM(狀態圖) to decide what to do of each event and actions * 假設資料傳輸會是單向的(實際上為雙向) ___ https://hackmd.io/@bob840806/r1Q5GaMUX ### rdt1.0 透過絕對可靠通道進行可靠傳輸(理想狀態) * no bit errors * no loss packet * 分成sender和receiver * sender傳資料到可靠通道 * receiver收資料從可靠通道 ![image](https://hackmd.io/_uploads/ByhFPENS6.png) ___ ### rdt2.0 Use ARQ(Automatic Repeat Request) * Packet wrong detect: 讓接收端可以在位元發生錯誤時發現有錯,UDP就是利用檢查和的方式。 * Receiver response: 接收端回饋訊息給傳送端,如ACK代表成功、NAK代表失敗,讓傳送端知道訊息的傳遞狀況。 * Retransmit: 若傳送有錯,傳送端就要再重傳一次。 ![image](https://hackmd.io/_uploads/rJ0jl8pra.png) :::info **傳送端**有2種狀態,一是等待上層傳資料下來,將資料丟到通道後進入第二種狀態,開始等待接收端傳送ACK或NAK回來,若收到ACK傳送端就知道前一個封包傳送成功,轉移回第一種狀態等待上層交代下一個封包,而若收到NAK就重傳一次。 這種模式稱為stop-and-wait協定。 而**接收端**仍只有一種狀態,但會根據收到的封包有無毀損傳回**NAK**或**ACK**。 ::: **然而rdt2.0忘記考慮ACK和NAK封包毀損的可能性,** * 傳送端不確定甚麼原因導致接收端收到毀損封包 * 傳送端收不到ACK/NAK卡在那 * 傳送端重傳可能會使接收端收到重複已成功傳送之封包 ___ ### rdt2.1 由rdt2.0延伸出針對封包毀損做加強 ![image](https://hackmd.io/_uploads/BJtgPU6r6.png) ![image](https://hackmd.io/_uploads/HJzZwL6BT.png) * 添加序號(pkt0, pkt1)防止重發封包重複 * 序號0,1就足夠(stop and wait) * 發送pkt1若回傳ack0可能遺失(正確應傳ack1) ___ ### rdt2.2 移除NAK,添加序號給接收端ACK回傳 ![image](https://hackmd.io/_uploads/ryYc_LaSp.png) * ACK回傳序號會為最後一個成功收到的packet分組 * EX: 失敗接收pkt1則回傳ack0,成功則回傳ack1 ___ ### rdt3.0 * 使用Timer,傳送端過了一段時間仍未收到ACK就重送 * 重送時間必須考慮到延遲時間和Receiver處理封包速度 ![image](https://hackmd.io/_uploads/rkol3UTHp.png) ![image](https://hackmd.io/_uploads/H17b2UTST.png) ![image](https://hackmd.io/_uploads/H1dkh8TS6.png) ![image](https://hackmd.io/_uploads/rkGy3Larp.png) ___ ### Stop and Wait(停等操作) 效率極差 ![image](https://hackmd.io/_uploads/HJ8dR8ar6.png) ___ [GBN v.s. SR v.s. TCP](https://blog.yfxie.com/comparison-of-network-gbn-sr-and-tcp/) ### Pipelining(Go-Back-N) ![image](https://hackmd.io/_uploads/H12biFIIp.png) ![image](https://hackmd.io/_uploads/rkhMotIL6.png) 假設一次送出3packets ___ #### sender ![image](https://hackmd.io/_uploads/SJXPjYI8T.png) * cumulative ACK: * 接收到回傳正確ACK推進n+1個packet * timeout(n): * N個packet中第n個packet傳輸上有錯誤時(ack判斷),等待(n之後的時間)從n開始重新再傳N個 ___ #### receiver 回傳packet對應之ack(pck100傳輸成功則回傳ack100)若失敗則(重要)**回傳上一成功傳輸之ack** ___ ![image](https://hackmd.io/_uploads/ByPInYLIT.png) ___ ### Pipelining2(Selective repeat) :::info send_base(n):目前還未收到ack之最前面的pck rcv_base(n):目前預計要收到之ack ::: * receiver * 收到window內pck: * 回傳收到的序號之 ACK 給傳送端 * 若收到的序號不是 base,則將封包暫存 * 若收到的序號是 base,將 base 與其後連續已收到的封包交由上層,並將 window 移動到最小預期收到的位置上。 * EX: 0123收到123重送0後收到時,回傳ack1,2,3且send_base移動到4 ![image](https://hackmd.io/_uploads/HJ-FAFI8T.png) * 收到不在window內的pck,則忽略 * sender * 逾時事件: ack逾時未送到則重送對應pck * 收到ack: * 標記對應序號的封包為已確認 * 若收到的 ACK 序號是 Base,則將 Base 移動到下個最小未經確認的封包上。 * 若窗格移動到了尚未傳送的封包上,則同時送出這些未被送出的可用封包。 ![image](https://hackmd.io/_uploads/Syo8Z58La.png) :::danger **窗格大小N之決定** N:最大為序號大小之一半 Ex: seq:0,1,2,3 若N=3: ![image](https://hackmd.io/_uploads/rkGzm9LLT.png) 傳送端所傳和接收端所期望之pck不同 ::: ___ ## TCP * point-to-point * reliabla, in-order * 使用資料流之概念(stream),可以不像GBN,SR有windows之傳輸量限制 * bi-diretional data in same connection ![image](https://hackmd.io/_uploads/HklnziILT.png) * seq * sender * receiver * ack * cumulative ack * sender * 期待下一次要收到封包之序號 * receiver * 回傳期望下次收到封包序號 ![image](https://hackmd.io/_uploads/BJfCBP1P6.png) EX: 假設datagram有100bytes 當sender(seq=40)時,ack=100+40 ___ ### TCP timeout時間計算 大概略長於RTT,太短會造成過多重傳浪費,太長會造成浪費時間等早已丟失的pck :::info 取樣SampleRTT(計算送出到回傳接收到ACK之時長)**不要用重傳的去算** ![image](https://hackmd.io/_uploads/rkwRLwyva.png) 此算出來為預計RTT時間,為確保穩定故還要加上**DevRTT(安全浮動值)** ![image](https://hackmd.io/_uploads/ByEMPvkvT.png) ![image](https://hackmd.io/_uploads/SysXPv1Da.png) **Estimated**和**Dev**的公式類似在算事件機率 ::: ___ ### TCP行為整理 #### Sender * app要發送資料 * seq#根據byte決定 * sqe#:上一sqe#+data_bytes * start timer * 收到ACK * 收到重複ACK三次->fast retransmit * 收到期望的ACK->restart timer且更新所期望ack * timeout * restart timer * retransmit data #### Receiver ___ ### TCP Fast Retransmit 重複**收到同樣ACK三次時觸發**,此時重送最小還未ACKed的seq ![image](https://hackmd.io/_uploads/BJ_Zd61wp.png) ___ ### TCP Flow Control * 計算rwnd來決定RcvBuffer的空間大小 * 確保receive buffer不會溢位 ![image](https://hackmd.io/_uploads/r1sPoa1D6.png) ![image](https://hackmd.io/_uploads/Skoa9pkPp.png) ___ ### TCP Connetcion 會預先建立handshake #### 3-way handshake 正式開始傳送資料前,先傳送一次測試 * SYN: 設置handshake用,同步序列號 * SYN有設置的話,SYN的seq#就是第一筆傳輸seq# [TCP三向交握](https://notfalse.net/7/three-way-handshake) ![image](https://hackmd.io/_uploads/Hyjc5CyvT.png) ![image](https://hackmd.io/_uploads/r1gd20kDp.png) ESTAB:連線建立 ___ ## IP Addressing :::info IP:32-bit with each host/router interface Interface: 連接host/router或鏈結的連線 ::: ![image](https://hackmd.io/_uploads/Sk_ieHz_T.png) 32bit的IP Address可以分成兩部分: network ID/Host ID ___ ### Subnet 能夠分成獨立網路的網路 **Router間也算一個子網路** ![image](https://hackmd.io/_uploads/HkBoVBz_p.png) 上圖為一個六個子網路的網路 ___ #### Subnet Mask ![image](https://hackmd.io/_uploads/rk-eBSzOT.png) 此處後方的**/24**即為subnet mask代表IP address的前24bit都是留給subnet part的 (詳請可以去看IP地址分類,後面應該也會寫到) [你真的了解127.0.0.1與0.0.0.0嗎](https://ithelp.ithome.com.tw/articles/10311096) ___