武汉飞新德里:第八章 第二层交换和生成树协议STP

来源:百度文库 编辑:中财网 时间:2024/05/02 07:56:22

 

一、基本概念

第2层交换:是在LAN上使用设备的硬件地址对网络进行分段的过程。

交换技术:是用来交大的冲突域分隔为小一些的冲突域的技术。

冲突域:是指用两台或多台设备对网络进行分段所形成的区域,这些区域共享同一带宽。

交换机:它使用专用集成电路(ASIC)来创建并维护其过滤表,它与网桥一样,用来分隔冲突域,并在每个端口提供独立的带宽,因此提高了用户的访问速率,每个端口均为半双工的10Mb/s。但它与网校一样都不能分隔广播域。

交换机设计的网络:要考虑两点,一是必须绝对下确的发隔冲突域,二是确信用户在本地网段花费了80%的时间。

交换技术与桥接的区别:1、网桥是基于软件的,而交换机是基于硬件的;2、交换机可以看成是多端口的网桥;3、每个网桥只有一个生成树,而交换机可以有许多生成树;4、交换机的端口数量比网桥大的多;5、网桥和交换机都转发第二层广播。

第2层的三种交换功能:1、地址学习(address learning):学习每个帧的源设备硬件地址,并把他们写到MAC地址表中;2、转发/过滤决定(forword/filter decisions):根据接收到帧的目的地址,并在MAC表中找到其相对应的硬件地址。帧被转发到指定目的端口;3、避免环路(loop avoidance):采用生成树协议来防止环路的产生。

 

二、交换技术

1、MAC地址学习。

当交换机加电时,MAC地址表是空的,当有帧发过来时,交换机学习该帧的源地址,并将其写到MAC地址表中,然后,交换机将该帧扩期到网络中,如果某台设备回应了该帧,交换机将反回信息的源地址写到MAC地址表中。

2、转发/过滤决定。

当帧到到交换机接口时,交换机将其目的地址与MAC地址表进行对比,如果MAC地址表内有该目的地址,交换机将该帧直接转发到目的接口,如果MAC表中没有该地址,帧就被广播到除了发送帧接口之外的任何接口。

sw1#show mac address-table //查看交换机的MAC地址表

sw1(config)#interface fastethernet0/1 //进入指定的接口,准备为该接口配置安全

sw1(config-if)#switchport port-security mac_address mac_address //指定该接口只允许基硬件MAC地址访问

sw1(config-if)#switchpott port-security maximum 1 // 设定该接口只能使用一个MAC地址。

sw1(config-if)#switchport port-security violation shutdowm // 如果该接口接入其它主机,则该接口将关闭

 

下面的例子,是设定该接口使用静态MAC地址安全,

sw1(config-if)#switchport port-security mac-address sticky //

sw1(config-if)#switchport port-security maximum 2  //

sw1(config-if)#switchport port-security violation shutdown //

 

3、避免环路

一般在交换机之间存在着一条冗余的链路,其目的是防止网络突然瘫痪。但是冗余的存在就会产生环路,解决办法:生成树协议。

 

三、生成树协议

1、基本概念

根桥(boot bridge):桥ID最低的网桥。

桥协议数据单元(BPDU:bridge protocol data until):所有交换机之间都相互交换信息,并根据这些信息选出根交换机,也根据这些信息来时行网络的后续配置。

桥ID:STP利用ID来跟踪网络中的所有交换机。桥ID是由桥优先级和MAC地址的组合来决定的。

非根桥(nonroot bridge):除了根桥外的,其它网有网桥。

端口开销(port cost):当两台交换机之间有多条链路且都不是根端口时,就根据端口开销来决定最佳路径,链路的开销取决于链路的带宽。

根端口(root port):是直接连接到根桥的链路所在的端口,或者是到根桥链路最短的端口。如果有多条链路连接到根桥,应检测每条链路的带宽来决定链路的开销,开销最小的为根端口;如果开销相同,就使用桥ID最小的那个桥。如果多条链路来自于同一台设备,就使用端口号最低的那个端口。

指定端口(designated port ):有最低开销的端口。指定端口被标记为转发端口。

非指定端口(nondesignated port):开销比指定端口高的端口。非指定端口被标记来阻塞端口。

转发端口(forwarding port ):指能够转发帧的端口。

阻塞端口(blocked port):指不能转发帧的端口。然而,阻塞端口始终监听帧。

 

2、生成树的操作:

STP的任务:是找到网络中所有的链路,并关闭任何冗余的链路,以防止网络出现环路。其实现方法是:STP首先选举出一个根桥,由根桥来负责决定网络的拓扑。

选举根桥:在网络中,桥ID用来选举根桥,并决定根端口。桥ID为8个字节长,其中包括了设备的优先级和MAC地址。默认的优先级别为:32768

在选举根桥时,要将桥ID【优先级】与MAC在址结合起来,如果桥ID【优先级】相同,则MAC地址小的为根桥。【桥id是可以更改的,在大型网络中一般会人为指定根桥】

在关闭端口时,如果几条链路的带宽相同,则关闭端口号最高的链路。

改变默认优先级,是改变根桥的最佳方式。

Sw1(config)#do show spanning-tree //查看生成树的信息

Sw1(config)#spanning-tree vlan 1 priority <0~61440> //修改优先级,以改变根桥,0为最优,第次以4096的倍数递增。

 

3、生成树端口状态

阻塞(blocking):被阻塞的端口不能转发帧,它只能监听BPDU。当交换机加电时,默认情况下,所有端口都处于阻塞状态。

侦听(listening):端口都侦听BPDU,以确信在传送数据帧之前,在网络上没有环路产生。

学习(learning):交换机端口侦听BPDU,并学习交换式网络中所有路径。处在学习状态的端口形成了MAC地址表,但不能转发数据帧。

转发(forwarding):在桥接端口上,处在转发状态的端口发送并接收所有的数据帧。

禁用(disabled):处于禁用判词的端口不能参与帧的转发和形成STP。

注:只有处于学习状态或转发状态下,交换机才能填写MAC地址表。

收敛:当网桥或交换机上的所有端口都转变为转发或阻塞状态时,变产生了收敛。在交换机端口上,生成树拓扑从阻塞到转发的收敛时间为50秒。也可以设置快速端口(portfast)在某端口上禁用生成树。

生成树快速端口(portfast):

Sw1(config-if)#spanning-tree portfast //针对某一端口设置快速端口

//将端口1~12都设置成快速端口。在配置端口为快速端口时,一定要确定不会产生环路。

Sw1(config)#interface range fastethernet0/1~12

Sw1(config-if-range)#spanning-tree portfast

生成树上行链路快速(uplinkfast):是Cisco特性,当链路失效时,它用来改进交换机的收敛的时间。该命令只能配置在接入层交换机上或带冗余链路的交换机上,且至少有一条链路处于阻塞状态时。Uplinkfast允许交换机在主链路(primary link)失效前,找出到根桥的可选路径。主要作用的确定并快速修复链路失效。

生成树主干快速(backbonefast):如果某条链路并没有直连到失效的交换机时,可以用该命令实现快速收敛。Backbonefast可以所在catalyst交换机上启用,以便能够检测到非直连的链路失效。

 

例:

S1

Switch#config terminal

Switch(config)#hostname s1

S1(config)#enable secret todd

S1(config)#interface fastethernet0/1

S1(config-if)#description 1st connection to core switch //描述功能

S1(config-if)#interface fastethernet 0/2

S1(config-if)#description 2st connection to core switch

Si(config-if)#interface fastethernet 0/3

S1(config-if)#description connection to host_a

S1(config-if)#interface fastethernet 0/4

S1(config-if)#description connection to phone_a

S1(config-if)#interface fastethernet 0/8

S1(config-if)#description connection to ISR

S1(config-if)#line console 0

S1(config-line)#password console

S1(config-line)#login

S1(config-line)#line vty 0 15

S1(config-line)#password telnet

S1(config-line)#login

S1(config-line)#line vlan 1

S1(config-line)#ip address 192.168.10.17 255.255.255.240

S1(config-line)#no shutdown

S1(config-line)#exit

S1(config)#banner motd #this is the s1 switch#

S1(config)#exit

S1#copy running-config starting-config

 

S2

Switch#config terminal

Switch(config)#hostname s2

S2(config)#enable secret todd

S2(config)#interface fastethernet0/1

S2(config-if)#description 1st connection to core switch

S2(config-if)#interface fastethernet 0/2

S2(config-if)#description 2st connection to core switch

S2(config-if)#interface fastethernet 0/3

S2(config-if)#description connection to host_b

S2(config-if)#interface fastethernet 0/4

S2(config-if)#description connection to phone_b

S2(config-if)#line console 0

S2(config-line)#password console

S2(config-line)#login

S2(config-line)#line vty 0 15

S2(config-line)#password telnet

S2(config-line)#login

S2(config-line)#line vlan 1

S2(config-line)#ip address 192.168.10.18 255.255.255.240

S2(config-line)#no shutdown

S2(config-line)#exit

S2(config)#banner motd #this is the s2 switch#

S2(config)#exit

S2#copy running-config starting-config

 

Core

Switch#config terminal

Switch(config)#hostname core

core(config)#enable secret todd

core(config)#interface fastethernet0/5

core(config-if)#description 1st connection to s2

core(config-if)#interface fastethernet 0/6

core(config-if)#description 2st connection to s2

core(config-if)#interface fastethernet 0/7

core(config-if)#description connection to s1

core(config-if)#interface fastethernet 0/8

core(config-if)#description connection to s1

core(config-if)#line console 0

core(config-line)#password console

core(config-line)#login

core(config-line)#line vty 0 15

core(config-line)#password telnet

core(config-line)#login

core(config-line)#line vlan 1

core(config-line)#ip address 192.168.10.19 255.255.255.240

core(config-line)#no shutdown

core(config-line)#exit

core(config)#banner motd #this is the core switch#

core(config)#exit

core#copy running-config starting-config

 

注:只有需要在LAN之外对交换机进行管理时,才配置IP default-gateway.

 

端口安全

S1#config terminal

S1(config)#interface range f0/3~4

S1(config-if-range)#switchport port-security maximum 1 //设置该端口只能接收一个MAC地址

S1(config-if-range)#switchport port-security mac-address sticky //设置静态获取MAC地址

S1(config-if-range)#switchport port-security violation shutdown //设置当非法接入时,关闭接口。

S1(config-if-range)#end

 

S2#config terminal

S2(config)#interface range f0/3~4

S2(config-if-range)#switchport port-security maximum 1

S2(config-if-range)#switchport port-security mac-address sticky

S2(config-if-range)#switchport port-security violation shutdown

S2(config-if-range)#end

 

Portfast

S1(config)#int range f0/3~4

S1(config-if-range)#spanning-tree portfast //只能应用于交换机与主机之间的链接。

 

S2(config)#int range f0/3~4

S2(config-if-range)#spanning-tree portfast

 

Bpdu护卫(BPDUGuard):如果在端口上打开了portfast,那么在该端口上应启用BPDUGuard.或BPDUFilter【BPDU过滤器】

S1(config)#interface range f0/3~4

S1(config-if-range)#spanning-tree bpduguard enable

 

S2(config)#interface range f0/3~4

S2(config-if-range)#spanning-tree bpduguard enable

 

Uplinkfast:在本地交换机上检测链路失效,并且能快速修复链路

S1(config)#spanning-tree uplistfast

S2(config)#spanning-tree uplistfast

 

Backbonefast:用来在远端交换机上检测链路失效

S1(config)#spanning-tree backbonefast

S2(config)#spanning-tree backbonefast

core(config)#spanning-tree backbonefast

 

配置etherchannel:在switch到switch、switch到router之间提供冗余的、高速的连接方式,简单说就是将两个设备间多条FE或GE物理链路捆在一起组成一条设备间逻辑链路,从而达到增加带宽,提供冗余的目的。

S1(config)#interface port-channel 1

S1(config-if)#int rang f0/1~2

S1(config-if-range)#switchport mode trunk

S1(config-if-range)#switchport nonegotiate //交换机之间禁止进行协商

S1(config-if-range)#channel-group 1mode desirable //用于将超过一个的交换机互联的端口捆绑到一起,主要用于负载均衡

S1#show int f0/1 etherchannel

 

Core(config)#int port-channel 1

Core(config-if)#int range f0/1~2

Core(config-if-range)#switchport trunk encap dot1q

Core(config-if-range)#switchport mode trunk

Cort(config-if-range)#channel-group 1 mode desirable

Core#show int port-channel 1

 

S1#show vlan 1

注:在交换机上并不需要IP地址,在交换机上配置IP、掩码、网关的唯一目的,就是方便管理。

S1#show mac address-table

S1#show spanning-tree

 

设置根桥

S1(config)#spanning-tree vlan 1 priroty 16348 //改变设备的优先级别,以修改根桥

S1#show  spanning-tree

 

直接设置成为根桥

S1(config)#spanning-tree vlan 1 root primary