《计算机网络概论》学习笔记整理之Internet working
/ / 点击 / 阅读耗时 24 分钟这篇笔记主要是关于IP协议的,内容超级多,其中subnet mask和CIDR的关系或是区别感觉不是很明白,但不予深究,仅把理解的记下来,日后碰到再说。
本篇主要包含下面内容:
- router的作用:用来建立大规模的异质网络
- IP(Internet Protocol)的作用是用于整个网络中的node和router的沟通使用,有两个特点:
- Connectionless model for data deliverying
- Best-effort(Unreliable Service),由于网络中的不可靠因素导致,不可靠因素包括:
- packets lost
- packets out of order during deliverying
- duplicate copies of a packet
- packets delay for a long time
- router:最重要的工作是routing table lookup
- IP subnetting
- subnet mask
- subnet number
- classless Inter-Domain Routing(CIDR)
- 合并路由表(routes aggregated to reduce routing table size)
- prefix and prefix length
- 192.4.16/21 表示8个class c网络
- 192.4.16/22 表示4个class c网络
- DHCP
- ICMP
- Distance Vector(routing protocol),RIP(Routing Information Protocol)使用这种方法
- Link State(routing protocol),OSPF(Open Shortest Path First)使用了这种方法
Internet Working
什么是internet working
An arbitrary collection of networks interconnected to provide some sort of host-to-host packet delivery service
什么是IP
- 用来构建可扩展的异质网络的重要工具
- 在网络中的所有节点上都会运行
- 对使用者来看,就好像是一个logical Internet Working
IP service model
- packet delivery model(类似邮政系统)
- connectionless model for data delivery
- best-effort delivery(unreliable service),可能会出现下面几种情况:
- packets are lost
- packets are delivered out of order
- duplicate copies of a packet are delivery
- packets can be delayed for a long time
- Global Addressing Scheme: Provides a way to identify all hosts in the network
- packet delivery model(类似邮政系统)
router(layer 3 device)是如何工作的
router使用store and forward的方式处理封包
router通过routing protocol一共会维护2张表:
- Forwarding Table:从这张表中查找destination IP,对应的下一步去向,目的地是和该router直接相连还是间接相连,然后去下表中查找对应的MAC address
- IP/MAC mapping table:拥有该IP的主机或router和其对应的MAC address的映射(真正收封包的是硬件网卡,所以需要知道网卡的MAC address)
如果在第一张表中没有找到destination IP,这个封包就会从default port出去,继续后面的处理
这种工作方式的router最大的问题是查表的速度
IP地址共有2^32(40亿)个,如果查表速度不够快,就会在router中累积,出现丢包。现代的router为了提高查表速度,都使用硬件的解决方案,使用专门的芯片,能够实现线速(wire-speed forwarding)
Not plug-and-play
router必须经过设定才能正常工作,无法像switch即插即用
IP封包的组成
每个网络都有一个MTU(Maxium Transmission Unit)特性,用来表示能传输的单个封包的大小上限
封包的重要字段:
- identification:在切割封包时,用于标识小封包是否属于同一个大封包
- Fragment:在封包被切割时,该字段用于记录每一个片段在原封包的什么位置
- Protocol:上一层协议,比如TCP或者UDP
- Time to Live(TTL):该处为一个数字,每经过一个router就减1,当减为0时,router就会将其丢弃,防止封包永久存在于网络上
flags:用来标记封包是否允许切割,不能切割的封包有两条选择:
- 走另外一条允许大封包通过的路
无路丢弃
flags由3个bit组成:
- 固定值0
- DF:0表示 may fragment,1表示don’t fragment
- MF:0表示 last fragment,1表示more fragment
- Total length:封包大小
- Header checksum:由IP Header的内容生成的校验码,校验封包内容在传输过程中是否错误
- Source IP Address和Destination IP Address:封包发送方和接收方的IP地址,IPv4版本是32bits,IPv6版本是128bits
Type of Service:8个bit,用来标识这个IP封包的功能
1
|precedence|D|T|R|O|O|
precedence:3个bit,
- 111:Network Control
- 110:Internetwork Control
- 101:CRITIC/ECP
- 100:Flash Override
- 011:Flash
- 010:Immediate
- 001:Priority
- 000:Routine
D:1个bit,表示Delay
- 0:Normal
- 1:Low
T:1个bit,表示Through put
- 0:Normal
- 1:High
R:1个bit,表示Reliability
- 0:Normal
- 1:High
IP Address的特点
- IPv4 32 bits,每个都是全球唯一的,用来定位唯一一台host
- 层级:network + host
- class A type:network(8) + host(24)
- class B type: network(16) + host(16)
- class C type: newwork(24) + host(8)
Intra-LAN and Inter-LAN Communications
B => Y(Intra LAN)
send the frame to the destination directly
1
|MAC(Y)|MAC(B)|IP(Y)|IP(B)|IP datagram|
B => A(Inter-LAN)
- send the frame to attached Router first
- Router will forward to the destination
1
2
3|MAC(R)|MAC(B)|IP(A)|IP(B)|IP Datagram|
|MAC(A)|MAC(R)|IP(A)|IP(B)|IP Datagram|
IP Datagram Forwarding
router的每一个port都有1个IP
策略:
- every datagram contains destination’s address
- if directly connected to destination network, then forward to host
- if not directed connected to destination network, then forward to some router
- forwarding table maps network number into next hop
- each host has a default router
- each router maintains a forwarding table
IP Fragmentation and Reassembly
MTU(Maximum Transmission Unit)
每一种网络都有一个MTU,表示这种网络可以接受的封包的最大的size
- Ethernet:1518bytes
- IEEE802.11 wireless:2312bytes
- FDDI:4500bytes
封包的size大过MTU就不能送进这个网络
策略
- Fragmentation occurs in a router when it receives a datagram that it wants to forward over a network which has MTU < datagram(封包切割发生在router上)
- Reassembly is done at the receiving host(封包组合发生在host上)
- All the fragments carry the same identifier(IP的header会被复制不切,切的是内容)
- Fragments are self-contained datagrams
- IP does not recover from missing(封包组合不起俩就会被丢弃)
组合
下图中:
- MF:表示该封包后面是否还有封包,0表示没有(就是说该封包就是最后一个),1表示还有
- offset:表示该封包中第一个byte的位置,0表示该封包位于整个资料的最前面
- MF和offset都为0表示该封包没有被切割过
- 封包组合时,会设置一个时间上限,超时未组合完毕就会被丢弃
Router的特征
Network Layer Routing
- Network layer protocol dependent
- Filter MAC broadcast and multicast packets
- Easy to support mixed media(每一个端口都可以支持不同的媒介)
- Packet fragmentation and reassembly(router本身具有组合封包的能力,但一般交给host来处理)
- Filtering on network(IP) address and information(ACL: Access Control List),也就是说router本身具有防火墙的功能
- accounting:计费能力,根据封包的流量计费
Direct Communication between endpoints and routers
- Highly configurable and hard to get right
- Handle speed mismatch(通过buffer来平衡)
- Congestion control and avoidance,拥挤控制方法包括:
- 丢弃封包
- 通过router之间的交流,告诉其他的封包送的慢点儿
Routing Protocols
- Interconnect layer 3 networks and exploit arbitrary topologies
- Determing which route to take
- Static routing:固定两个IP之间的路线,不受网络状态影响,不受距离远近、网速快慢影响
- Dynamic routing protocol support
- RIP:Routing Information Protocol
- OSPF:Open shortest Path First
- Provides reliability with alternate routes
- Router management
- Troubleshooting capabilities
Differences between Bridges and Routers
Bridges | Routers |
---|---|
operation at layer 2 | operation at layer 3 |
protocol independent | protocol dependent |
automatic address learning/filtering | administration required for address, internet and routers |
pass MAC multicast/broadcast | MAC multicast/broadcast can be filtered |
lower cost | higher cost |
no flow/congestion control | flow/congestion control |
limited security | complex security |
transparent to end systems | non-transparent |
well suited for simple/small networks | for wan\larger networks |
no frames segmentation/reassembly | frames segmentation/reassembly |
spanning tree based routing | optimal routing and load sharing(routing选的路线更好) |
plug and play | requires central administrator(需要设定才能运作) |
IP subnetting
- subnet: another level to address/routing hierarchy(在a、b、c三层网络下再加一层子网络)
subnet masks(用来定义子网络的大小): define variable partition of host part of class A and class B address
1
2
3
4
5
6
7
8
9|network number|host number|
IP地址的层级结构如上,一个class B的ip地址,host unmber由16个bit组成,共有2^16=65536个
|11111111|11111111|11111111|00000000|
每个1表示一个mask,上面就是255.255.255.0,这个子网共有2^8=256台host
submask为255.255.255.128时,有2^7=128台host
submask为255.255.255.192时,有2^6=64台host
Subnet Forwarding Algorithm
下面这个表格是子网和下一站的映射
subnet Number | Subnet Mask | Next Hop |
---|---|---|
128.96.34.0 | 255.255.255.128 | Interface 0 |
128.96.34.128 | 255.255.255.128 | Interface 1 |
128.96.33.0 | 255.255.255.0 | R2 |
1 | D = destination IP address |
注意:
- A default router is used if nothing matches
- Not necessary for all ones in subnet mask to be contiguous(mask的1不一定要连续)
- subnets not visible from the rest of the Internet(子网络是router内部规划的结果)
- Can put multiple subnets on one physical network
Classless Addressing
Classless Inter-Domain Routing(CIDR)
用来解决网络上的2个规模问题:
- The growth of backbone routing table as more and more network numbers need to be stored in them
- Potential exhaustion of the 32-bit address space
CIDR uses aggregate routes(合并路由)
- Uses a single entry in the forwarding table to tell the router how to reach a lot of different newworks
- Breaks the regid(严格的) boundaries between address classes
示例
- For example, an AS with 16 class c network numbers
- Instead of handling out 16 addresses at random, handle out a block of contiguous class c address
- Suppose we assign the class C network numbers from 192.4.16(prefix,可以是2~32bits的任何长度) through 192.4.31
- Observe that top 20 bits of all the addresses in this range are the same(11000000 00000100 0001)
- Requires to handle out blocks of class c addresses that share a common prefix
- The convention is to place a /x after the prefix where x is the prefix length in bits
- For example, the 20-bit prefix for all the networks 192.4.16 through 192.4.31 is represented as 192.4.16/20
- By contrast, if we wanted to represent a single class c network number 192.4.16, which is 24 bits long,we would write it 192.4.16/24
1
2
3
4对于class c,host前用24bits表示network
192.4.16/21时,24-21=3,2^3=8,表示有8个连续的网络
192.4.16/20时,24-20=4,2^4=16,表示有16个连续的网络
192.4.16/23时,24-23=1,2^1=2,表示有2个连续的网络这种方式可以让多个entry合并为1个,减小router table的大小
longest prefix matching
比对prefix可能出现的多个结果,比如:
171.69.10.5
- 171.69/16
- 171.69.10/24
这两种情况都能匹配上,router对这种情况的处理方式就是使用最长的prefix
Address Resolution Protocol(ARP)
用途
Map IP address(1. destination host 2. next hop router) into physical(MAC) address
过程
- table of IP to physical address bindings
- broadcast request if IP address not in the table
- target machine responds with its physical address
- table entries are discarded if not refreshed
ARP封包的重要字段
- target protocol addr:对方的IP
- source protocol addr:问的IP
- source hardware addr:自己的MAC
- target hardware addr:对方的MAC
- operation:是问是答
Dynamic Host Configuration Protocol(DHCP)
一台host要设置如下参数,才能正常工作:
- IP
- default router
- subnet mask
- domain name
- 关于DHCP
- DHCP server is responsible for providing configuration information to host
- There is at least one DHCP server for an administrative domain
- DHCP server maintains a pool of available address(这种方式允许动态分配时可以对IP的时效性做控制)
- 过程
- Newly booted on attached host sends DHCP DISCOVER message to a special IP address(255.255.255.255)
- DHCP relay(中继设备) agent unicasts the message to DHCP server and waits for the reponse
Internet Control Message Protocol(ICMP)
Defines a collection of error messages that are sent back to the source host whenever a router or host is unable to process an IP datagram successfully,下面这些场景都会导致router发出ICMP封包
- Destination host unreachable due to link/nodd failure
- Reassembly process failed
- TTL(time-to-live) had reached to(so dataframs don’t cycle forever)
- IP header checksum failed
ICMP-Redirect
- From router to a source host
- with a better route information
Routing Protocol
router有两项重要的工作:forwarding和routing,同时对应的也会构建两张表:forwarding table和routing table
- Forwarding: to select an output port based on destination address and routing table
Routing: process to build the routing table
find the lowest-cost path between any two nodes,通过routing protocol,每一个router都会获得整个网络的拓扑结构
Forwarding table
- Used when a packet is being forwarded
- An entry in the forwarding table contains the mapping from a network umber(哪一个网络) to an outgoing interface(哪一个port) and some MAC information, such as Ethernet Address of the next hop
prefix/length | Interface | MAC address |
---|---|---|
140.114/16 | 0 | 8:0:2c:e3:b:2:20 |
- Routing table
- Built by the routing algorithm
- Generally contains mapping from network numbers to next hops
cost | prefix/Length | Next Hop |
---|---|---|
2 | 140.114/16 | 171.34.45.12 |
每一个routing只需记住下一站,不需要记住全程
Distance Vector Protocol(一种分布式动态routing protocol)
- Each node constructs a one dimensinal array(a vector) containing the “distances”(costs) to all other nodes and distributes that vector to its immediate neightbours
- Assume that each node knows the cost of the link to each of its directly connected neightbours
- Every T seconds each router sends its routing table to its neighbours
- Each router then updates its routing table based on the new information
- problems include:
- fast response to good news
- slow response to bad news
- too much messages to update
在真实网络中,经过router的数量的最大值是16(可以认为是count-to-infinity中的infinity)。Distance Vector Procol中存在的最大的一个问题是:count-to-infinity problem,简单来说就是相邻的两个router循环向对方发送routing table:
A <=> B
,解决办法如下:- split horizon: when a node sends a routing update to its neightbours, it does not send those routes it learned from each neighbours back to that neightbours
- split horizon with poizon reverse: B actially sends that back route to A, but it puts negative information(route(0,oo)) in the route to ensure that A will not eventually use B to get to other node
Link State Protocol(另一种分布式动态routing protocol)
- 策略:send to all nodes(not just neighbours) information about directly connected links(not entire routing table)
- LSP封包(Link State Packet),执行上述过程的时候借助该类封包,包含:
- ID of the node that created the LSP
- cost of link to each directly connected neighbours
- Sequence number(SEQNO)(link的状况只要有变化就会发送出新的LSP)
- Time-to-live(TTL) for this packet
- LSP需要可靠传送,收到LSP的router只需保存最新的SEQNO(因为该封包反映了最新的link的状态),然后该router继续向外广播(不回送)
- 实际网络中OSPF(Open Shortest Path First)使用了Link state Protocol
- Each router computes its routing table directly from the LSP’s it has collected using the Dijkstra’s algorithm
- Find the shortest path from the router to each other node