一般的转发过程:
1>决定packet的目的地是否可达
2>决定packet的下一跳地址,及外出接口
3>重写packet的MAC header信息,然后转发。


Case Study:Process Switching
Process Switching是switching pathes中最基础的实现,几乎所有的IOS版本,被转发的所有流量都支持这种switching paches。

Process Switching中的两个重要概念:
1>用于重写MAC header的转发决定及转发信息来源于路由表(Routing Information Base,RIB),ARP Cache及其它存放MAC header信息的表。
2>packet在IOS内部被正常转发。即转发决定是由IOS调度器调用一个进程来实现的,此进程与其它的进程(如路由协议)是对等的,其它的进程不会中断下来去处理这个packet,它直接由router central processor来处理。
转发过程:
1>interface processor首先检测到网络介质上有一个packet,并将其传至路由器的input/output memory。
2>interface processor产生一个接收中断.在中断期间,central processor查看这个packets的类型(此处假定它为IP packet),如果有必要(根据不同的platform的需求),则一并将其copy到processor memory.最后processor将packet置于适当的进程的input queue,然后释放中断.
3>下一次调度产生时,Central processor调度器认识到packet处于ip_input进程的input queue中,然后自动调度相应进程处理它。
4>当ip_input运行的时候,它查找RIB来决定next-hop及output interface,然后查找ARP Cache来决定正确的next-hop的物理层地址。
5>ip_input重写packet的MAC header,然后将其置于相应的外出接口的外出队列上。
6>从外出接口的output queue上将packet复制到外出接口的transmit queue上,同时外出QoS在复制时生效。
7>output interface processor检测到packet在它的transmit queue上,就将它转发到相应的网络介质上。

注:
Almost all features that effect packet switching, such as Network Address Translation (NAT) and Policy Routing, make their debut in the process switching path. Once they have been proven, and optimized, these features may, or may not, appear in interrupt context switching.



Case Study:Interrupt Context Switching
Interrupt Context Switching是Cisco Router使用的第二种主要的转发方式。
Process Switching与Interrupt Context Switching的主要区别是:
1>Central Processor正在运行的进程被中断,然后转向去处理到来的packets.Packets被按需转发,而不是只在ip_input进程空闲的时候才能被转发。
2>Central Processor使用route cache技术来查找转发packet所需的所有信息。
转发过程:
1>interface processor首先发现网络介质上的packet,并将其传输至路由器的input/output memory
2>interface processor产生一个接收中断.在中断期间,central processor检测packet的类型(此处假定为IP packet),然后开始switch这个packet
3>central processor查找route cache来决定packet的目的地是否可达,以及它的output interface,next-hop address及mac header. processor用这些信息重写packet的MAC header
4>packet现在被copy到外出接口的transmit或output queue(具体到哪个queue,由很多因素决定).接收中断被释放.中断前的central processor处理的过程被继续.
5>output interface processor检测到packet在它的transmit queue,然后将其传输到网络介质上



Case Study:Cache Types
上述的Interrupt context switching使用了所谓的cache技术,根据Interrupt context switching类型的不同,cache技术可以分为三种:
1>Fast Switching
2>Optimum Swtiching
3>Cisco Express Forwarding


(1)Fast Switching Cache
1>fast switching使用一个二进制树来存储转发信息及MAC header重写字符,从而加快查找速度。
2>fast switching根据二进制树中节点是否存在,来标识下一跳的可达性。每个目的地的MAC header及外出接口信息都被存储于二进制树中的节点信息内。
3>二进制树分为32级,为了表述方便,二进制数的前部一般被省略.
4>在对二进制数进行查找的时候,一般采用中序遍历

Characteristics of the Fast Switching:
使用二进制树数据结构及将MAC header重写信息存放于树节点上,使Fast Switching:
1>因为routing table与fast cache contents(例如MAC header rewrite)之间没有对应关系,所以建立的cache条目都需要在转发packet时生成。
即一边转发packet,一边建立相应的cache条目。
2>因为ARP cache中的MAC headers(用于重写的)与fast cache的数据结构没有对应关系,所以ARP table改变时,相应的fast cache条目就要被aging掉。进而对新的packet建立新的转发cache,以反应客户的变更。
3>对路由表中的一个目的地,fast cache只能以一种前缀长度来描述它。
4>fast cache不能对递归的路由进行解析。

Aging Fast Switching Entries:
为了保持fast cache与路由表及ARP cache同步,fast cache要定期的删除那些过期的无用的cache条目。
一般情况下,每分钟随机删除1/20的fast cache条目。
如果router的memory降低到很小的时候,每分钟随机删除1/5的fast cache条目。


Fast Switching Prefix Length:
因为fast switching只为路由表中每个目的地建立一个fast cache条目,那它的prefix长度可以根据以下条件来计算:
1>If building a fast policy entry, always cache to /32.
If building an entry against an Multiprotocol over ATM virtual circuit (MPOA VC), always cache to /32.
2>If the network is not subnetted (it is a major network entry):
 --If it is directly connected, use /32;
 --Otherwise use the major net mask.
3>If it is a supernet use the supernet's mask.
4>If the network is subnetted:
 --If directly connected, use /32;
 --If there are multiple paths to this subnet, use /32;
 --In all other cases, use longest prefix length in this major net.

Load Sharing:
fast switching是完全基于目的地转发的,所以它的load sharing实现就是:对不同的目的地转发到不同的负载链路上。


(2)Optimum Switching(最优转发)
Optimum Switching转发信息及MAC header重写信息存储在一个256路multiway树上(256 way mtree)。从而减少了查找prefix所需的步骤。
prefix中的每一个8位位组用来决定树上的每一个级别的值,则最多进行4次查找,就可以找到目的地.
对于一个短的prefix,可能只需要1-3次查找就可以实现.
MAC header重写信息及外出接口信息都存储在树的节点信息中。所以Cache的有效性及aging问题也与fast switching一样会发生。
Optimum Switching与Fast Switching一样,也同样决定着每一个cache条目的prefix的长度。


(3)Cisco Express Forwarding
CEF转发提供3张表
1>FIB:从路由选择表中拷贝过来的转发信息,包括路由选择表中用于转发路由分组所必须的最少信息。
2>Adjacency Table:维护一个邻接节点以及他们相关的2层MAC重写及下一跳信息
3>NetFlow Table:用于统计网络数据等
CEF的转发信息不是存储在cache里的,因此也不存在过期无效(Invalidation)或老化(Aging)。它是在转发数据前根据路由表及ARP cache建立相应的,即FIB表。FIB表与路由表有一一对应的关系,所以它不需要维护路由高速缓存了,而前述的转发方式都需要。
转发信息是从路由信息经过一定处理后转换过来的,也就是说,如果在转发信息里找不到某个数据包的转发信息,那么也不用费心到路由那里去了,因为那里肯定也没有。转发信息可以算是路由信息的一个硬件表达方式。这样的结构布局保证了路由功能和交换功能的分离(软件上):
1>路由功能只处理三层以上信息,如路由信息处理、策略等,然后形成一个路由表,再根据这个路由表生成相应的转发信息。
2>转发功能很明确,就是根据这些转发信息来工作,没有相关信息就拒绝。
CEF使用一个4级256路的数据结构(Forwarding Information Base,FIB)来指明转发信息及MAC header重写信息的位置。
CEF中被检索的转发MAC header重写信息不是存放在数据结构(FIB)内。而是存储在另一个数据结构内,FIB表中的条目只是简单的指向作用。
CEF认为:与其些外出接口及MAC header重写信息存储在树中,还不如将它们存放在另一个数据结构里来得高效,那个数据结构就是"adjacency table"。
CEF将可达性信息存放于CEF Forwarding table,而将转发信息存放于adjacency table。
CEF中递归的路由通过指向递归的下一跳,实现正常的解析,而不是只能直接指向转发信息。
CEF简化了查询的步骤,提高了单位时间的工作效率。而且从整体上来看,路由信息和转发信息是分离的,数据包的转发只根据转发信息而不用参照路由信息,可以充分利用专用硬件的功能来达到线速转发,而不受路由变化或者其他因素的干扰,保证了转发的高速高效。
Other Entries in the Adjacency Table:
除了MAC header重写及外出接口信息外,Adjacency table中还有其它类型的条目,一般有:
1>cache
用于到达特定adjacent host及router的MAC header重写及外出接口信息。
2>receive
到此IP地址的packets需要路由器接收处理,如:bradcast address及router本身配置的addresses。
3>drop
到此IP地址的packets需要被丢弃,如被ACL deny的流量或是指向NULL接口的流量。
4>punt
Cisco Express Forwarding不能转发这个packet,需要将它传递给下一个最好的switching method进行处理,一般为Fast Switching或Processing Switching.
5>glean
下一跳是直连的,但是现在没有可用的关于它的MAC header重写信息。

Glean Adjacencies:
一个glean的adjacency条目指示特定的下一跳是直连的,但是现在没有可用的关于它的MAC header重写信息。
例如:
默认建立的Adjacency table:
10.1.1.0/24, version 17, attached, connected
0 packets, 0 bytes
  via Ethernet2/0, 0 dependencies
    valid glean adjacency
10.1.1.0/32, version 4, receive
10.1.1.1/32, version 3, receive
10.1.1.255/32, version 5, receive

此处有3个receive条目,一个glean条目。
每一个receive条目代表一个broadcast address或是router本身配置的地址。
glean条目代表直连网络的其它地址空间。

此时,如果收到10.1.1.50的一个packet时,router会尝试转发它,然后在adjacency table里发现它指向一个glean条目。CEF信号指示需要一个ARP cache条目,进而触发ARP过程发送一个ARP packets,然后相应的adjacency table条目从新的ARP cache信息中建立。
现在adjacency table中就有了10.1.1.50的cache条目了:
10.1.1.0/24, version 17, attached, connected
0 packets, 0 bytes
  via Ethernet2/0, 0 dependencies
   valid glean adjacency
10.1.1.0/32, version 4, receive
10.1.1.1/32, version 3, receive
10.1.1.50/32, version 12, cached adjacency 208.0.3.2
0 packets, 0 bytes
  via 208.0.3.2, Ethernet2/0, 1 dependency
   next hop 208.0.3.2, Ethernet2/0
   valid cached adjacency
10.1.1.255/32, version 5, receive
下一次10.1.1.50的packets到来时,router就直连用相应的cache条目转发了。

Load Sharing:
CEF因为使用分隔的Cisco Express Forwarding table和Adjacency table机制的优势,load sharing的实现比其它任何的Interrupt Context Switching类型(fast switching,optimum switching)都好。
CEF的Load Sharing实现使用loadshare table,将其插入Cisco Express Forwarding table及Adjacency table之间。
Cisco Express Forwarding table指向Loadshare table。Loadshare table存在多个与不同的adjacency table条目并行连接的指针。
源/目的地址通过一个hash算法,决定每个packet需要指向哪个loadshare table条目。
Per-packet load sharing配置使每一个packet使用一个不同的loadshare table条目。

Each loadshare table has 16 entries among which the paths available are divided based on the traffic share counter in the routing table. If the traffic share counters in the routing table are all 1 (as in the case of multiple equal cost paths), each possible next hop receives an equal number of pointers from the loadshare table. If the number of available paths is not evenly divisible into 16 (since there are 16 loadshare table entries), some paths will have more entries than others.



Case Study:Which Switching Path Is Best?:
基本上来说,大家都会选择使用interrupt context switching,因为它至少比process switching快好多。
然后在cache type上,显然CEF比其它的Switching模式要更快更好。由其是当你有多个流量可以load sharing的并行链路时.