tcpip四层参考模型功能(TCPIP之网络层是如何工作的)

引言

1、搞定文章所有的 TODO

2、翻译文章的英文部分

不会深入介绍算法细节,因为???// TODO

使网络层工作的3个重要组件

本文中会涉及很多关于网络层用到的技术,为了让大家不被这些细节所累,对网络层有个整体的认识,我列出3个重要的网络层组件,最后你会发现,基本上所有的技术细节都是围绕它们展开的。

  1. The Internet Protocol (IP)
  • The creation of IP datagrams.
  • Hop-by-hop delivery from end to end.
  1. Routing Tables
  • Algorithms to populate Router forwarding tables.
  1. Internet Control Message Protocol (ICMP)
  • Communicates network layer information between end hosts and routers.
  • Reports error conditions.
  • Helps s diagnose problems.

如果你不明白上面的内容,不要紧,这里你只需要记住它们3个对于网络层来说要非常重要,下文中,我会详细解释它们在网络层扮演的角色。

网络层的组织

网络层中有2种类型的组织:

  1. datagram organisation
  2. virtual circuits organisation

connectionless 服务 VS connection-oriented 服务

类似于传输层,传输层在2个进程之间提供 connectionless 服务或 connection-oriented 服务,而网络层可以在2个 hosts 之间提供 connectionless 服务或 connection-oriented 服务。但是,网络层不像传输层那样,可以同时提供2个服务。在目前所有的计算机网络架构(Internet, ATM, frame relay, and so on) 中,网络层要么提供 host-to-host connectionless 服务,要么提供 host-to-host connection-oriented 服务,但是不能同时提供2个!我们把只提供 connection-oriented 服务的计算机网络叫做 virtual-circuit (VC) 网络; 把只提供 connectionless 服务的计算机网络叫做 datagram 网络.

由于下文中重点介绍的 Internet 属于 datagram 网络,这里我只介绍 datagram organisation,想了解 VC 网络的童鞋请参考 Virtual circuit organisation

Router 用 routing table 来存储 next hop 的位置,当一个 packet 到达 router 时,它会查询它的 routing table,然后决定出这个 packet 应该去的下一个 router. 因此,一个给定的 router 只知道整个从 source 到 destination 路径的一步,即 next hop。下图是 Router 的内部结构:

tcpip四层参考模型功能(TCPIP之网络层是如何工作的)(1)

The data plane contains all the protocols and algorithms that are used by hosts and routers to create and process the packets that contain user data.The data plane, and in particular the forwarding algorithm used by the routers, depends on the routing tables that are maintained on reach router.

These routing tables can be maintained by using various techniques (manual configuration, distributed protocols, centralised computation, etc). These techniques are part of the control plane of the network layer. The control plane contains all the protocols and mechanisms that are used to compute and install routing tables on the routers.

One of the objectives of the control plane in the network layer is to maintain the routing tables that are used on all routers. As indicated earlier, a routing table is a data structure that contains, for each destination address (or block of addresses) known by the router, the outgoing interface over which the router must forward a packet destined to this address. The routing table may also contain additional information such as the address of the next router on the path towards the destination or an estimation of the cost of this path.

维护 Routing Table 的技术

// TODO

在这个小节中,Hierarchical routing 是不是维护 Routing Table 的技术???涉及算法细节吗???

In this section, we discuss the three main techniques that can be used to maintain the routing tables in a network.

Static routing

The simplest solution is to pre-compute all the routing tables of all routers and to install them on each router. Several algorithms can be used to compute these tables.

A simple solution is to use shortest path routing and to minimise the number of intermediate routers to reach each destination. More complex algorithms can take into account the expected load on the links to ensure that congestion does not occur for a given traffic demand

The main drawback of static routing is that it does not adapt to the evolution of the network. When a new router or link is added, all routing tables must be recomputed. Furthermore, when a link or router fails, the routing tables must be updated as well.

Link state routing

Link state routing

Distance vector routing

Distance vector routing

Hierarchical routing

1、书中379页

2、在 http://cnp3book.info.ucl.ac.be/1st/html/network/network.html 搜索 Hierarchical Routing 关键字

routing and non-routing

大部分的 networks 都是用分组交换(packet switching)把一个机器上的数据带到另一个机器上。简而言之,传送的数据被拆分成很多个小的 packets,每个 packet 都有目标地址和返回地址(就像给我们写信给别人 一样),然后把这些 packets 放到 network 中,然后会根据地址找到目标机器,从而实现机器之间交换数据的目的。

假设你想要建造一个小的网络(local network),non-routable protocol(它的本质就是用地址 identify the destination,但是并不会说明如何去找到 destination) 就可以做到。你只需要一个交换机(switch,它只维护地址与直接连接它的机器之间的映射 destination,但是并),然后把所有的机器与它相连,每个机器都有一个地址,这个地址可以任何随机的数,然后交换机来维护地址与机器之间的映射,这样机器之间就可以交换数据了。这个过程实际上就是一个 ethernet connection 工作的过程,Ethernet 地址(也叫 MAC address)被分配到 network adaptor cards,这个地址可以就是一个随机数。

但是,当要你想把一个 local network 中的 packets 发送到另一个 local network 中,这就变得困难了。Internet 可以做到:把从一个 local network 中得到的 packets 发送到另一个 local network 中,这2个 local network 很有可能分别在世界的2边。下面是维基百科给出的关于 Internet 的定义:

The Internet is the global system of interconnected computer networks that use the Internet protocol suite (TCP/IP) to link devices worldwide. It is a network of networks that consists of private, public, academic, business, and government networks of local to global scope, linked by a broad array of electronic, wireless, and optical networking technologies. The Internet carries a vast range of information resources and services, such as the inter-linked hypertext documents and applications of the World Wide Web (WWW), electronic mail, telephony, and file sharing.

从上面的定义可知,Internet 把各种各样的 local networks 互联起来,形成一个更大的网络,要想让如何巨大的网络有效地工作起来,我们需要 routing protocol.

TCP/IP networks 本质上是 router-based 的,Internet 是用 TCP/IP 连接世界各地的设备,因此 Internet 也是 router-based 的。上文中已经说明了 router 中的 routing table 是用算法来配置的,让 router tracks 整个 Internet 中的设备是不现实的。那么问题来了:router 是如何在如此巨大的 Internet 中发挥作用的呢?另外 Router 是如何知道哪个地址是 local 的,哪个需要 routing 呢?想知道这些问题的答案,我们就需要详细了解 IP 协议了。

IP 协议

http://cnp3book.info.ucl.ac.be/1st/html/network/network.html#internet-protocol

DHCP

Firewalls

NAT

ICMPIntradomain routingInterdomain routingBroadcast 和 MulticastRouter VS Switch
classful network

大家平常见到的 IP 地址都是以十进制形式表示的,比如:168.212.226.204,但是对于下面一些概念的理解,我们必须要把它还原成本来的样子,即二进制的形式,对于这个例子来说,它的二进制形式是 10101000.11010100.11100010.11001100

classful network 是从 1981 年开始使用的,但是到 1993 年就被 Classless Inter-Domain Routing 取替了。这个方法把 IP 地址的空间分成几个类别,每个类别下可以容纳不同数量的 hosts. 下图中详细的描述了在各个类别的信息:

tcpip四层参考模型功能(TCPIP之网络层是如何工作的)(2)

从上图可以看到,不同类别的 networks 有不同的 leading bits,根据这个,你就可以知道每个类别 IP 地址的范围。通过观察 IP 地址的高 8 位,你就可以知道各个类别高8位的范围:Class A = 0~127,Class B = 128~191,Class C = 192~223 等,因此,如果某人给你一个 IP 地址,你完全可以根据它的高位地址就可以判断出它属于哪个类别。

从上图我们也看到,各个类别要求 network number (即 Network ID )为8,16,和 24 位,这样的划分地址空间存在一个很大的问题。比如:一个 class C 的网络只可以容纳 2

8

−2=254

28−2=254 (减去 2 是因为全 0 或 全 1 的地址被保留,不能被 hosts 所用) 个 hosts,这对很多企业来说太少了; 然而,对于 class B的网络来说,它可以容纳 65634 个 hosts,这太大了。在这种地址空间的划分下,比如,现在有个需要 2000 hosts 的企业,我们要只能分配给它 class B 的网络,这导致大量的地址没有利用上。由于 Internet 的快速增长,而这个方法又大量浪费本来就十分宝贵的地址,所以它就被新的方式替代了。

Classless Inter-Domain Routing

CIDR 的出现就是解决 classful network 存在的问题。上面我已经提到了 IPv4 地址实际上是由 32 位的二进制组成,表面上看起来它就是一堆烦人的,没有意义的数字,实际上它有更深层的意义可以解读。

这 32 位二进制分为2组:the most significant bits 是网络前缀(network ID),它用来标识整个网络或子网; the least significant 是 host identifier(host ID),它指定了这个网络或子网中的 host,这样的划分是 IP 网络之间 traffic routing 和 地址分配策略的基础。上面的介绍的 Classful network 是把网络前缀分为 8 位,16 位,和 24 位,从而产生 Class A,B,或者 C 类型的网络,这样划分非常不灵活; 而对于 CIDR 来说,我们可以在任何位置划分地址,比如,我们可以有 3 位,9 位,或任何你想要的前缀。

聪明的伙伴们可能会想,那么给定一个 IP 地址,如何找出哪一部分是 network ID,哪一部分是 host ID 呢?答案是 subnet mask. 它由 32 个 bits 组成,a sequence of ones (1) followed by a block of zeros (0). The trailing block of zeros designates that part as being the host identifier. 把它与 IP 地址应用 bitwise AND 操作,我们就可以得到 network ID 了。下图是维基百科给出的例子:

tcpip四层参考模型功能(TCPIP之网络层是如何工作的)(3)

接下来,我把上面的例子再进行子网划分,即从 host 部分拿出 2 bits 用于子网划分,这样我们就又多出了4个子网,从下图我们也看出全为 1 的 host ID 被用作 Broadcast,有兴趣的伙伴们可以用 IP Calculator 来玩一玩,自己动手划分一下子网。

tcpip四层参考模型功能(TCPIP之网络层是如何工作的)(4)

从上图中,我们也看到了 a.b.c.d/x 这种形式的地址,它是 CIDR 表示 IP 地址的新方法。The x most significant bits 构成了 IP 地址的网络部分(即网络 ID),它通常叫做地址的 prefix (or network prefix).

说了这么多,现在让我们总结一下吧。classful network 分为几个类别,每个类别都有不同的 network prefix,剩下的部分全是 host number,这种方法的问题就是不能更好地利用 IP 地址; CIDR 通过 variable-length subnet masking (VLSM)技术可以指定任意长度的 network prefix,同时它也用一种新的方式,a.b.c.d/x,来表示 IP 地址,其中的 “/x” 是一种简便方法表示 subnet mask,它定义了 network ID 的位数。

tcpip四层参考模型功能(TCPIP之网络层是如何工作的)(5)

因此,现在我们应该知道 router 是如何判断 packets 是到同一个网络的机器或者是到其它网络中的机器。每一个 router 都会有它的 IP 地址和 subnet mask,它用 subnet mask 就可以判断到来的 packets 是属于同一个 local network 或是其它的网络。举个例子,比如我们家里的 router 的 IP 地址为 192.168.1.1,子网掩码为 255.255.255.0,这样当一个 packet 的目标地址为 192.168.1.x 时,x 可以是要任何数,router 会把它转发到要内部的网络,而其它任何的 packet 都会被发送到外部网络。你可以用你的 chrome 浏览器试一试,比如分别输入一个外部网络(192.168.3.6)和内部网络(192.168.1.6)的地址,当然了这2个地址并不存在,你可以观察浏览器都给你什么样的错误。

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页