Network [EE 450 & CS 551 & Web] - jwyx/ForFun GitHub Wiki

Credit

http://blog.csdn.net/dog250/article/details/6612496

Introduction

Network:
    A set of devices(often referred to as nodes) connected by communication links
Node:
    Device capable of sending and/or receiving data generated by other nodes on the network
Computer Network:
    A collection of computers and devices connected to each other
    The goal is to provide the end system with some services
    Nodes: 
        host, router (bridge, gateway and hub)
    Transmission facilities:
        coaxial cable, twisted-pair copper wire, fiber optics microwave, rado channel, satellite
Network Topology:
    Refer to the layout of connected devices
    Bus, ring, line, tree (star), mesh
Internet:
    The network of heterogeneous networks
Client/Server Model:
    Client host requests, and receives service from always-on server
Peer-to-Peer Model:
    All computers are equal and they all provide and request services
    A dedicated server (a peer directory) is still required, to let hosts know where their peers are
Network Interface Card [NIC] / adaptor card:
    An expansion card used to connect hosts to the network
Network classifications based on coverage:
    Personal Area Network [PAN]:
        a computer network used for communication among computer devices close to one person
        a few meters
    Local Area Network [LAN]:
        Cover a limited geographic are
        A few kilometers
        Wireless LAN
        
        Backbone Network:
            Allow  several LANs to be connected
            No station is directly connected to the backbone
            The backbone itself is a LAN
        Ethernet:
            A family of frame-based computer networking protocols for LANs
            Define wiring and signaling standards for the Physical Layer through means of netwok access
            at the Media Access Control [MAC]/Data Link Layer, and a common addressing format
            IEEE 802.3 
    Metropolitan Area Network [MAN]:
        Spanning a city
        E.g.
            cable TV network for high-speed data connection to Internet
            part of the telephone company that can provide high-speed DSL lines to customers
    Wide Area Network [WAN]:
        Cover a broad area
        E.g. Internet or Public Switched Telephone Network [PSTN]
        Largest WAN: PSTN
Internetworking [Internet]:
    Connect two or more computer networks via gateways using a common routing technology
    The gateway has to be at least a router, i.e. a 3rd layer switch that can recognize IP
Transmission Types:
    Simplex:
        In one direction one device always transmit (T/X) on always receives (R/X), cannot switch roles
    HDX [half duplex]: both side can transmit, but one at a time
    FDX [full duplex]: both side can transmit at the same time
Communication:
    Sender, Receiver, Message, Medium, Protocol
The Shannon-Weaver Simple Communication Model, Year 1949:
    The message (data or information) is generated by source in analog or digital
    and then passed to the transmitter to be transformed to a proper form for the medium

    MODEM (modulation & demodulation):
        A transmitter is required to convert the data from digital to analog which is suitable
        for the telephone line.
        Modem is needed for dialup, DSL, or cable
    
    The medium or the transmission link can be copper wire, coaxial cable, optical fiber,
    wireless radio and satellite or microwave
    
    When signal travels through the transmission medium it will degrade due to noise (interference)
Point-to-Point Link:
    Only two nodes are connected to a link
Multi-Point Link:
    All nodes are attached to the same link; shared network
Transmission facilities:
    Shared:
        Trucks; have huge capacity (bandwidth)
        The traffic comes from all users
    Dedicated:
        Low bandwidth
        The traffic from a single user or a single host
Network cloud:
    The network from the view of outside the network
    One does not know how nodes are connected
    Having the network cloud already established and supported by network providers,
    connecting a host can become possible by adding one port and a dedicated interconnect,
    the cost is extremely low
Network classification:
    Switched
    Shared
        Messages are broadcasted
        Cable end will absorb the message, meaning the message won't be reflected back
Access Networks:
    The physical links that connect an end system to the edge router of network
        1. Residential
            Point-to-Point Access:
                Dialup via modem:
                    Up to 56Kbps direct access to router (often less)
                    Can't surf and phone at same time, cannot be 'always on'
                DSL [Digital Subscriber Line]:
                    A new modem technology over existing twisted-pair telephone lines,
                    but by limiting the distances between the user and ISP modem,
                    DSL can transmit and receive data at much higher rates from ISP router
                    to home than from the home to the ISP router
                    - Up to 1 Mbps upstream   (typically < 750 kbps)
                    - Up to 8 Mbps downstream (typically < 6 Mbps)
                    Dedicated physical line to telephone central office
            Shared Access:    
                HFC [Hybrid Fiber Coax]
                    Asymmetric: up to 30Mbps downstream, 2Mbps upstream
                    Homes share access to router
                    Deployment: available via cable TV companies
                    
                    DSL and HFC require special modems, called cable modems
                    In DSL, HFC, and satellite access, service is always ON.
        2. Company
            Company LAN connects end system to to the edge router
            Ethernet: most prevalent access technology
                Uses either twisted-pair copper wire or coaxial cable
                Ethernet can use shared medium
                10Mbps, 100Mbps, 1Gbps, 10Gbps Ethernet
                End systems connect into Ethernet switch
        3. Wireless
            WiMAX aka IEEE802.16
            3G standards
            Mobile Ad hoc Network [MANET]
            Sensor Networks
Home Networks (Private Networks):
    Dialup, DSL or cable modem
    Router/Firewall/NAT (Network Address Translation)
        Firewall: block outside penetration into the home network
        NAT: the process of remapping network address
    Ethernet
    Wireless access point (can come with the router as one package)
Physical (Transmission) Media:
    Mode: Unicast, broadcast, multicast
    Bit:  propagates between transmitter/receiver pairs
    Physical link: what lies between transmitter & receiver
        Guided media:   signals propagate in solid media
        Unguided media: signals propagate freely, e.g., radio

U0 13/18

Socket Programming

...

Transport layer

Transmission Control Protocol [TCP]
    - connection
        flow based, no segment bound
    - reliable
        Implemented by using segment ACK
        发送一个segment,接受方必须回复一个ACK
        滑窗机制 保证每次发送不一定是一个字节
    - in order
        Implemented by using sequence number
    - flow control
        Implemented by advertise window [awnd]
    - congestion control
        Implemented by congestion window [cwnd]

        事实上我们可以得知,只有在以下情况下拥塞控制才会起作用:
        a.两个或两个以上的连接(其中一个一定要是TCP,另一个可以是任意连接)经过同一个路由器或者同一个链路时;
        b.只有一个TCP连接,然而它经过了一个路由器时。
        其它情况下是不会拥塞的。
        
        因为一个TCP总是希望独享整条网络通路;本质上,拥塞的原因就是大家都想独享全部带宽资源,结果导致拥塞,这也是合理的,
        毕竟TCP看不到网络的状态,同时这也决定了TCP的拥塞控制必须采用试探性的方式,最终到达一个足以引起其“反应”的“刺激点”。
        目标:
          - 公平性
          - 拥塞之后退出拥塞状态

        TCP的超时重传风暴(一个分段丢失造成后续的已经发送的分段均被重传,而这些重传大多数是不必要的)加重了网络的拥塞。

        不要把路由器想成一种线速转发设备,再好的路由器只要接入网络,总是会拉低网络的总带宽,因此即使只有一个TCP连接。
        由于TCP的发送方总是以发送链路的带宽发送分段,这些分段在经过路由器的时候排队和处理总是会有时延,因此最终肯定会丢包的。

        丢包的延后性也会加重拥塞。

        拥塞窗口,它实际上表示的也是“可以发送多少数据”
        拥塞控制整体上分为两类,一类是试探性的拥塞探测,另一类则是拥塞避免(注意,不是常规意义上的拥塞避免)。

        - 试探性的拥塞探测分为两类,之一是慢启动,之二是拥塞窗口加性扩大(也就是熟知的拥塞避免,
           然而这种方式是避免不了拥塞的)。
        - 拥塞避免方式拥塞控制旨在还没有发生拥塞的时候就先提醒发送端,网络拥塞了,
           这样发送端就要么可以进入快速重传/快速恢复或者显式的减小拥塞窗口,这样就避免网络拥塞的一沓糊涂之后出现超时,
           从而进入慢启动阶段。
        - 快速重传和快速恢复。所谓快速重传/快速恢复是针对慢启动的,我们知道慢启动要从1个MSS开始增加拥塞窗口,
          而快速重传/快速恢复则是一旦收到3个冗余ACK,不必进入慢启动,而是将拥塞窗口缩小为当前阀值的一半加上3,
          然后如果继续收到冗余ACK,则将拥塞窗口加1个MSS,直到收到一个新的数据ACK,将窗口设置成正常的阀值,
          开始加性增加的阶段。(数据包守恒)

    FAQ:
        1. 为什么3次握手,但是4次挥手?
            3次握手是分配资源,初始化序列号,不涉及数据传输,三次足矣。(3rd ack can piggyback data if necessary)
            
            因为TCP是一个全双工协议,必须分别拆除每一个信道。
            4次挥手是终止数据传输,并回收资源,必须分别在一个方向上终止数据传输。
            每个信道通过fin + ack来回一次进行终止。

        2. TIME_WAIT状态为什么需要?
            Maximum segment lifetime [MSL] is the time a TCP segment can exist in the internetwork 
            system. It is arbitrarily defined to be 2 minutes long. The Maximum Segment Lifetime value 
            is used to determine the TIME_WAIT interval (2*MSL).
            
            终止连接的主动方必须在接受了被动方的FIN且回复了一个ACK之后等待TIME_WAIT时间才能进入CLOSE状态,
            乘以2是因为这是保守的算法,最坏情况下,针对被动方的ACK在以最长路线(经历一个MSL)经过互联网马上到达被动方时丢失。
            
            因为每次建立连接时初始化序列号是随机的32位数字,会产生回绕 (wrap back);
            
            假设4th ACK丢失导致被动方重发FIN,如果没有TIME_WAIT,直接建立一个新的连接,并且重用了之前连接的信息
            (源IP,目的IP,TCP,源端口,目的端口),那么当重发的FIN到达的时,如果序列号处于新连接期望序列号的范围内,
            新的连接会被中止。TIME_WAIT可以保证ACK和重发的FIN都已经从网络上消失。

            如果设置TIME_WAIT设置很低的话,最好的方式就是,不要重用一个连接。

        3. 重用连接 vs. 重用socket?
            不同的概念,重用socket != 重用连接
            (源IP,目的IP,TCP,源端口,目的端口) 标示 一个连接,而不是一个socket
            但是BSD socket中由accept()生成的socket对应一个连接

        4. RTT计算?
            ??

        5. How to implement RTO timer?
            One RTO timer per segment: expensive!!
            So one RTO timer per TCP connection: default.
            
            原则:
            - 每一个报文在'长期'收不到确认都必须可以超时
            - 这个长期收不到中'长期'不能和测量的RTT相隔太远

            RFC2988定义一套很简单的原则:
            a. 发送TCP分段时,如果还没有重传定时器开启,那么开启它。 -> 保证会超时;保证原则1
            b. 发送TCP分段时,如果已经有重传定时器开启,不再开启它。
            c. 收到一个非冗余ACK时,如果有数据在传输中,重新开启重传定时器。 -> 防止过早和过多重传;保证原则2
            d. 收到一个非冗余ACK时,如果没有数据在传输中,则关闭重传定时器。

        6. When to measure RTT?
            Add timestamp into TCP header;
            Put timestamp from segment into ACK of this segment;
            In sender, RTT = timestamp in ACK - current timestamp.

        7. How does ACK work?
            TCP ACK number是收到的按序报文的最后一个字节的后一个字节
            Cumulative ACK
            接收端丢弃重复数据,但是ACK同样会发送
            大多数TCP的实现,在接收端会缓存乱序的segment

        8. 乱序缓存 和 Select ACK
            TCP是保证顺序,但是不保证segment是按序接收的,因为基于IP。
            是否丢弃乱序的TCP segment取决于实现。
            RFC建议如果内存允许,还是要缓存这些乱序到来的分段,等到按序的一组分段之后才拼接。
            类似于IP的分片,但是IP必须缓存所有的分片,否则无法组装,因为无重传机制。
            Select ACK 显式说明需要重传的segment,防止重传风暴

        9. Sequence number wrap back?!
            ??

        10. 流量控制的真实意义?
            flow control可以有效地协调端到端的流量匹配。但是因为只限制了最大发送的数据,没有限制最小发送数据,
            发送小数据包时header所占比例过大,导致网络利用率下降。
            
            TCP效率问题:
            1) Problem:  接收端处理慢,导致接收窗口被填满
                   => 每次发送小的segment,即糊涂窗口综合症 ?? 对应551??
               Solution: 窗口通告
                   Probe segment: 用于探测接收端窗口大小,由TCP标准规定
                   => 当 awnd < MMS/2 时,对probe segment的ACK才将awnd设置为0,即阻止发送端发送小的segment。
            2) Problem:  发送端持续发送小包,导致接收窗口闲置
               Solution: Nagle算法;默认开启,可以关闭
                   IF 数据的大小和窗口的大小都超过了MSS
                     Then 发送数据分段
                   ELSE
                     IF 还有发出的不足MSS大小的TCP分段的确认没有到来
                       Then 积累数据到发送队列的末尾的TCP分段
                     ELSE
                       发送数据分段
                     EndIF
                   EndIF
            3) Problem:  ACK不含数据,大量消耗带宽
               Solution: RFC建议了一种延迟的ACK
                => ACK在收到数据后并不马上回复,而是延迟一段可以接受的时间
                   延迟一段时间的目的是看能不能和接收方要发给发送方的数据一起回去
                   即使没有捎带数据,也可能等到了后续到来的TCP分段,这样它们就可以取最大者一起返回
                   最后,RFC建议,延迟的ACK最多等待两个分段的积累确认
        11. RTO & Fast retransmission/ fast recovery [超时重发 & 快速重发/快速恢复]
            超时重传:
                一般是因为网络出现了严重拥塞(没有一个分段到达,如果有的话,肯定会有ACK的,若是正常ACK,则重置重传定时器,
                若是冗余ACK,则可能是个别报文丢失或者被重排序,若连续3个冗余ACK,则很有可能是个别分段丢失),
                此时需要更加严厉的缩小拥塞窗口,因此此时进入慢启动阶段。
            收到三个冗余的ACK的重传:
                一般是路由器故障或者轻度拥塞或者其它不太严重的原因引起的,
                因此此时拥塞窗口缩小的幅度就不能太大,此时进入快速重传/快速恢复阶段。
        12. 为何收到3个冗余ACK后才重传?
            收到两个或者一个冗余ACK也可以重传,但是这样的话可能或造成不必要的重传,因为两个数据分段发生乱序的可能性不大,
            超过三个分段发生乱序的可能性才大,换句话说,如果仅仅收到一个乱序的分段,那很可能被中间路由器重排了,
            那么另一个分段很可能马上就到,然而如果连续收到了3个分段都没能弥补那个缺漏,那很可能是它丢失了,需要重传。
            因此3个冗余ACK是一种权衡,在减少不必要重传和确实能检测出单个分段丢失之间所作的权衡。
            注意,冗余ACK是不能捎带的。
        13. 乘性减和加性增的深层含义?
            拥塞窗口的增加受惠的只是自己,而拥塞窗口减少受益的大家,可是自己却受到了伤害。这种乘性减实现了公平性。
            BIC算法提高了加性增的效率,不再一个一个MSS的加,而是一次加比较多的MSS,采取二分查找的方式逐步找到不丢包的点,
            然后加性增。(??)
        14. TCP连接的传输稳定状态是什么?
            发送端的发送窗口怎么确定,它取的是拥塞窗口和接收端通告窗口的最小值。
            三种发送窗口的稳定状态:
            a.IP互联网络上接收端拥有大窗口的经典锯齿状
                多数情况
                TCP发送端用慢启动或者拥塞避免方式不断增加其拥塞窗口,直到丢包的发生,
                然后进入慢启动或者拥塞避免阶段(要看是由于超时丢包还是由于冗余ACK丢包),
                此时发送窗口将下降到1或者下降一半,这种情况下,一般接收端的接收窗口是比较大的,
                毕竟IP网络并不是什么很快速的网络,一般的机器处理速度都很快。
            b.IP互联网络上接收端拥有小窗口的直线状态
                如果接收端特别破,处理速度很慢,就会导致其通告一个很小的窗口,这样的话,即使拥塞窗口再大,
                发送端也还是以通告的接收窗口为发送窗口,这样就不会发生拥塞。
            c.直连网络端点间的满载状态下的直线状态
                如果唯一的TCP连接运行在一个直连的两台主机上,那么它将独享网络带宽,
                这样该TCP的数据流在最好的情况下将填满网络管道(我们把网络管道定义为带宽和延时的乘积),
                其实在这种情况下是不存在拥塞的
        15. 主动的拥塞避免?
            可以通过检测RTT的变化以及拥塞窗口的变化推算出IP网络的拥堵情况,然后进行主动调控
            具体算法??
        16. IP路由如何告诉TCP拥塞?
            当路由器检测到自己发生轻微拥堵的时候随机的丢包,随机丢包而不是连续丢包对于TCP而言是有重大意义的,
            随机丢包会使TCP发现丢弃了个别的分段而后续的分段仍然会到达接收端,这样TCP发送端就会接收到3个冗余ACK,
            然后进入快速重传/快速恢复而不是慢启动
        17. 如何了解TCP设计原理?
            先看点入门的,比如Wiki之类的,然后看RFC文档,793,896,1122等),这样你就明白TCP为何这么设计了
            551也重设计