跳到主要内容

2. 一种更灵活高效的子网号分配技术

为了帮助解释这种新技术, 我们将展示当前做法有什么问题. 目前, 大多数子网是通过将地址的主机部分拆分为两个字段来分配的; 即子网字段和主机字段. 子网字段位的掩码位为 1, 主机字段位的掩码位为 0. (在我们所有的地址中, 最低有效位 (LSB) 在右边, 最高有效位 (MSB) 在左边.)

        MSB                                LSB
--------------------------------------
| subnet field | host field |
--------------------------------------

子网字段的长度可能因不同规模的子网而不同. 例如, 假设一个网络有两个大型子网, 其余为小型子网 (所谓大型子网, 我们指的是主机数量多). 那么网络管理员可能分配两种类型的地址:

        --------------------------------------
| subnet | host | large subnets
--------------------------------------

--------------------------------------
| subnet | host | small subnets
--------------------------------------

在这种情况下, 子网号的完整范围将无法供小型子网使用, 因为小型子网中与大型子网对应位置的那些位不能具有与大型子网相同的值. 例如, 假设大型子网使用 4 位子网号, 而小型子网使用 8 位子网号. 如果大型子网的值为 0001 和 0010, 那么 00010000 到 00101111 范围内的子网号就不能分配给小型子网, 否则将出现同时匹配两个子网的地址.

无论如何, 网络管理员通常会按数字顺序为这两个字段赋值. 例如, 在给定子网内, 主机将编号为 1, 2, 3 等. 在给定网络内, 子网将编号为 1, 2, 3 等. 结果是, 子网和主机字段右侧的若干位对某些主机为 1, 对其他主机为 0, 而子网和主机字段左侧的若干位对所有子网和主机都为 0. "全零" 位表示增长空间, "1 和 0" 位表示已被增长消耗的位.

        --------------------------------------
| subnet field | host field |
|-----+-----------+-------+------------|
| | | | |
| 0's | 1's & 0's | 0's | 1's & 0's |
/\ /\
|| ||
subnets can hosts can grow here
grow here

现在, 让我们假设某个子网中的主机数量增长到允许的最大值, 但子网字段中仍有空间可以分配更多地址. 于是我们得到以下情况:

        --------------------------------------
| subnet field | host field |
|-----+-----------+--------------------|
| | | |
| 0's | 1's & 0's | 1's & 0's |

虽然主机字段不能再增长, 但地址中仍有增长空间. 问题在于, 由于增长区域所处的位置, 剩余的增长实际上已被保留给子网专用.

相反, 应当做的是分配子网号, 使 1 位从子网字段的左侧开始向右推进. 在这种情况下, 我们得到以下情况:

        --------------------------------------
| subnet field | host field |
|-----------+-------------+------------|
| | | |
| 1's & 0's | 0's | 1's & 0's |
/\
||
Both hosts and subnets can
grow here

现在, 主机和子网各自都比以前拥有相当大的增长空间, 尽管总的增长空间相同. 由于人们很少能预测一个子网最终会有多少台主机, 或者最终会有多少个子网, 这种安排允许在增长方面具有最大的灵活性.

实际上, 前一幅图具有误导性. 主机字段和子网字段之间的边界显示在增长区域的中间. 然而, 边界可以位于增长区域内的任何位置. 请注意, 决定边界位置的是掩码本身. 掩码中的 1 表示子网位, 0 表示主机位. 我们稍后将说明, 实际上边界应当位于中间的某个位置. 把它放在那里可以最大限度地减少必须更改主机中掩码的次数.

2.1. 新技术的规范说明​

在给出适当的解释性材料之后, 我们现在可以规定子网号分配的过程. 我们需要以下定义:

Host-assigned Bits (h-bits): 这些是从右侧开始连续的位, 在给定子网内, 这些位上的主机值既有 1 也有 0. 不同的子网可以有不同的 h-bits.

Subnet-assigned Bits (s-bits): 这些是从左侧开始连续的位, 它们 1) 不是 h-bits, 并且 2) 是区分一个子网与另一个子网所必需的, 并且 3) 包括最右侧的那个 1 位及其左侧的所有位. 注意不同的子网可以有不同的 s-bits.

Growth Bits (g-bits): 这些是位于 h-bits 和 s-bits 之间的 "全零" 位.

s-mask: 对于给定子网, 这种掩码使所有 s-bits 为 1, 所有 g-bits 和 h-bits 为 0.

g-mask: 对于给定子网, 这种掩码使所有 s-bits 和 g-bits 为 1, 所有 h-bits 为 0.

Subnet Field: 这些是子网掩码中的 1 位 (如 RFC-950 所定义). 这些位在左侧. 子网字段必须至少包括所有 s-bits, 并且可以额外包括部分或全部 g-bits.

Host Field: 这些是子网掩码中的 0 位. 这些位在右侧. 主机字段必须至少包括所有 h-bits, 并且可以额外包括部分或全部 g-bits.

Mirror-image Counting: 二进制中的常规计数使 1 位从右侧开始向左推进. 主机值就是这样分配的. 然而, 对于子网分配, 我们希望 1 位从左侧开始向右推进. 这个过程是常规计数的镜像, 其中 MSB 与 LSB 交换, 第二个 MSB 与第二个 LSB 交换, 依此类推. 因此, 常规计数为:

                0       (reserved to mean "this host")
01
10
011
100
101
:
:
11...1110
11...1111 (reserved to mean "all hosts")

依此类推, 而镜像计数, 即 MI 计数, 为:

        0       (reserved to mean "this subnet")
10
01
110
001
101
:
:
011...11
111...11 (reserved to mean "all subnets")

依此类推. 如果当前 MI 计数值为, 比如说, 001, 那么 "下一个" MI 值为 101, 而 "上一个" MI 值为 11.

现在我们可以规定该算法. 我们有以下函数: Initialize(), AddSubnet(), RemoveSubnet(subnet#), AddHost(subnet#) 和 RemoveHost(subnet#,host#).

请注意, 该算法是当作一个状态机在执行它来描述的. 实际上, 可能存在一个根地址授权机构 (RootAA), 它分配子网号 (Initialize, AddSubnet 和 RemoveSubnet), 以及子网 AA, 它们在子网内分配主机号 (AddHost 和 RemoveHost). 虽然一般而言各 AA 可以独立行动, 但在两种情况下需要 rootAA 与某个 subnetAA 之间进行 "协调". 这两种情况是 rootAA 或 subnetAA "抢占" 最后一个增长位 (前一种情况是因为添加了另一个子网, 后一种情况是因为添加了另一台主机). 由于 rootAA 和某个 subnetAA 不可能同时抢占最后一个增长位, 因此必须由其中一方来完成.

最后, 请注意使用了以下 C 语言风格的记法:

        &               bit-wise AND function
== is equal to
!= is not equal to
x-mask(X) the x-mask of X (where x is s or g)
   Initialize():
      Assign the first subnet value to be 0 (the value reserved to mean
      "this subnet").  This is not assigned to any real subnet.

   AddSubnet():
      1.  Find the lowest non-zero (in MI counting) non-assigned subnet
          number S such that (S & g-mask(Y)) != (Y & g-mask(Y)) for all
          existing subnet numbers Y, (Y != S).
      2.  If all bits in S from the rightmost one bit left are ones,
          then label all bits to the left of and including one bit
          position to the right of the rightmost one bit in S to be
          s-bits. Else, label all bits to the left of and including the
          rightmost one bit in S to be s-bits.  This prevents the "all
          ones" value (which is the "all subnets" broadcast address)
          from being assigned to a subnet.  (Since no hosts have been
          added, the rightmost one bit is a subnet bit.)
      3.  Label all other bits in the address to be g-bits.  (By
          address, we mean that part of the IP address not including
          the network number.)
      4.  Set the subnet mask to include at least all s-bits, and
          optionally some g-bits.  The subnet mask must be contiguous.
          (Section 2.2 discusses the pros and cons of choosing a mask.)
      5.  For all existing subnet numbers Y (Y != S):
          51. If (S & s-mask(Y)) == (Y & s-mask(Y)), then:
              511.  Change the leftmost g-bit of Y to an s-bit.  If
                    the rootAA and YAA (the address authority for Y) are
                    separate AAs, then the YAA must be informed of the
                    change of bit status.  If this is the last g-bit,
                    then this change must be coordinated with YAA.
              512.  Expand the subnet mask for all hosts in Y if
                    necessary (that is, if the subnet mask no longer
                    includes all s-bits).

   RemoveSubnet(S):
      1.  Consider B to be the bit position of the rightmost s-bit in S.
      2.  Remove S.
      3.  For all existing subnet numbers Y:
          31.  If the bit in position B is not an s-bit, or if the bit
               in bit position B is a one, or if the bit in bit position
               B is a zero and all bits to the left of bit position B
               are ones, then do nothing (skip steps 32 and 33).
          32.  Change the s-bit in position B to a g-bit.
          33.  If for any other existing subnet numbers X
               (X & s-mask(Y)) == (Y & s-mask(Y)), then change the
               g-bit in position B back into an s-bit for Y.  Else,
               inform YAA that of the change of bit status.

   AddHost(S):
      1.  Create an address A consisting of subnet number S concatenated
          with zeros.
      2.  Assign to A the same h-bits, g-bits, and s-bits as the
          other host addresses.
      3.  Find the lowest non-zero (using normal counting) non-assigned
          host number H.
      4.  If all bits from the leftmost one bit to bit position 0 are
          ones, then execute steps 5 and 6 using bit position B equals
          one bit position to the left of the leftmost one bit in H.
          Else, execute steps 5 and 6 with bit position B equals
          the leftmost one bit in H.  This prevents the "all ones" value
          (which is the "all hosts" broadcast address) from being
          assigned to a host.
      5.  If bit position B is an s-bit, then the host cannot be added.
          Skip the remaining steps.
      6.  If bit position B is a g-bit:
          61.  Change the g-bit to an h-bit for all hosts in S.  Note
               that if this is the last g-bit, this change must be
               coordinated with the address authority assigning subnet
               numbers (see section 2.2).
          62.  Modify the subnet mask in all hosts if necessary.
      7.  Create a new address A consisting of S concatenated with H
      8.  Assign A to the host.

   RemoveHost(S,H):
      1.  Remove H.
      2.  If for all remaining host numbers in S, the value of the bit
          position of the leftmost h-bit is zero, and there is a zero in
          at least one of the bit positions to the right of the leftmost
          h-bit, then for all hosts change the leftmost h-bit into a
          g-bit.

这里值得指出的是, 这种技术是更一般的 n 级 kampai 编址 [5] 的 2 级子集. 这里的主要区别在于, n 级 kampai 会导致非连续掩码, 而 2 级不会. 在 [5] 中对 kampai 编址的描述里, g-bits 被称为 a-bits, h-bits 被称为 g-bits, 而 s-bits 被称为 i-bits.

2.2. 一个示例​

对于这个示例, 我们假设一个 C 类网络, 因此我们只需要处理 8 位. 我们从 3 个子网 A, B 和 C 开始. 我们的命名法是 h 表示 h-bit, g 表示 g-bit. 注意 h-bits 可以是 1 或 0, 但 g-bits 全为 0. 其余的位是 s-bits, 但根据子网号分配情况显示为 1 和 0. 空格只是为了使地址和掩码更易读. 最后, 我们按如下所示从右到左将位编号为 0 到 7.

        Subnet  Address         Mask
A 10gg ghhh 1111 0000
B 01gg ghhh 1111 0000
C 110g ghhh 1111 0000
bit 7 bit 0

我们看到每个子网最多有 6 台主机 (因为有 3 个 h-bits). 注意我们选择掩码时, 在主机和子网两方面都留出了增长空间, 而无需更改掩码.

然而, 我们通常在子网方面留出的增长空间比在主机方面更多, 因为添加新子网可能导致现有子网的掩码更改, 而在子网中添加新主机只会导致该子网的掩码更改.

此外, 如果某个子网的掩码必须更改, 但并非所有主机都同时重新配置, 那么尚未重新配置的主机掩码过大 (1 太多) 比掩码过小的损害更小. 这是因为掩码过大时, 主机可能认为另一台实际上在同一子网上的主机在另一个子网上. 在这种情况下, 该主机将把数据包发送到网关, 并被重定向到该主机.

然而, 掩码过小时, 主机可能认为另一台实际上不在该子网上的主机在该子网上, 并将对该主机进行 ARP, 但收不到应答. (注意, 如果所有掩码不匹配, 广播可能失败.)

最后, 注意子网 C 需要 3 个 s-bits 而不是仅 2 个. 这是因为如果只有 2 个, C 的子网地址可能是 "11" (而不是 "110"), 而这是一个广播值. AddSubnet 的第 2 步会检查这种情况.

现在, 添加第四个子网 D, 它也有 6 台主机. 我们得到:

        Subnet  Addr            Mask
A 10gg ghhh 1111 0000
B 01gg ghhh 1111 0000
C 110g ghhh 1111 0000
D 001g ghhh 1111 0000

注意, 原有子网都不需要更改其任何状态位. 这是因为当 D 将其子网号与其他子网比较时 (AddSubnet() 的第 5 步, 使用 s-mask), 它们全都不同. 换句话说, 路由器能够区分 D 中的地址与 A, B 和 C 中的地址.

接下来, 添加第五个子网 E. 我们得到:

        Subnet  Addr            Mask
A 100g ghhh 1111 0000
B 01gg ghhh 1111 0000
C 110g ghhh 1111 0000
D 001g ghhh 1111 0000
E 101g ghhh 1111 0000

注意这一次 A 被迫将其最左侧的 g-bit (第 5 位) 改为 s-bit, 因为需要第 5 位来区分 A 与 E (AddSubnet() 的第 511 步). 将第 5 位改为 s-bit 可以防止向 A 中添加主机达到使第 5 位变为 1 的程度 (也就是说, AddHost() 的第 5 步将失败).

还要注意, 如果 A, B 和 C 中的掩码最初设为 1100.0000, 那么添加 E 会导致 A 的掩码变为 1110.0000 (AddSubnet() 的第 512 步).

接下来, 向子网 A 和 C 各添加 8 台主机, 从而使每个子网中最右侧的 g-bit 变为 h-bit.

        Subnet  Addr            Mask
A 100g hhhh 1111 0000
B 01gg ghhh 1111 0000
C 110g hhhh 1111 0000
D 001g ghhh 1111 0000
E 101g ghhh 1111 0000

再次注意没有掩码发生变化. 如果 A, B 和 C 的掩码最初设为 1111 1000, 那么它们将需要更改 (AddHost() 的第 62 步).

接下来, 向子网 B 添加足够多的主机, 使其所有剩余的 g-bits 都变为 h-bits.

        Subnet  Addr            Mask
A 100g hhhh 1111 0000
B 01hh hhhh 1100 0000
C 110g hhhh 1111 0000
D 001g ghhh 1111 0000
E 101g ghhh 1111 0000

注意这里 B 子网中的掩码必须更改以容纳新的 h-bits (AddHost() 的第 62 步). 还要注意, 如果为 B 分配主机地址的人 (B Address Authority, 即 BAA) 与分配网络号的人 (RootAA) 不同, 那么 BAA 必须与 RootAA 协调将其最后一个 g-bit 改为 h-bit. 这使 RootAA 能够正确分配额外的子网号, 如下一步所示, 我们在其中添加另一个子网 F:

        Subnet  Addr            Mask
A 100g hhhh 1111 0000
B 01hh hhhh 1100 0000
C 110g hhhh 1111 0000
D 001g ghhh 1111 0000
E 101g ghhh 1111 0000
F 1110 ghhh 1111 0000

注意 F 收到的子网号是 1110 而不是子网号 011 (后者是 MI 计数中 101 之后的值). 原因是 1) 使用 B 的掩码无法将 011 与 B 的子网地址区分开, 2) 我们不能增大 B 的掩码使其可区分, 因为 B 已经分配了第 5 位上的主机. 换句话说, 当对编号 011 尝试 AddSubnet() 第 1 步的比较时, 两个值相等, 因此尝试了下一个编号. 事实上, 第 7 位和第 6 位为 01 的任何子网号都不能分配 (除非 B 失去主机).

接下来, 移除子网 E:

        Subnet  Addr            Mask
A 10gg hhhh 1111 0000
B 01hh hhhh 1100 0000
C 110g hhhh 1111 0000
D 001g ghhh 1111 0000
F 1110 ghhh 1111 0000

注意这导致子网 A 将一个 s-bit 改回 g-bit. 这是因为对于子网 A, RemoveSubnet() 第 33 步的相等性相对于剩余子网并不成立.