一、路由器的工作原理
1.交换机的工作原理:
交换机中维护着一张MAC地址表,主要记录的是MAC地址和接口的对应关系;
交换机在初始状态下,MAC地址表是空的,当收到某个接口(F0/1)的数据是,首先查看数据帧中的源MAC地址,对照MAC地址表,如果不在表中,将发送方的MAC地址学习并记录在MAC地址表中,并附上对应的接口(F0/1),再查看目的MAC地址,如果目的MAC地址不在表中,将除接收接口(F0/1)外的其他所有连接接口转发出去(广播的方式),当目的主机接收到时,其他主机也会接收到,其他主机接收到后直接丢弃,目的主机接收到后进行回应,回应的过程中数据同样会交给交换机进行转发,此时目的主机就变成了回应的发送主机,当交换机接收到来自目的主机的回应报文时(F0/10),同时查看发送方的MAC地址,进行学习记录,并附带对应的接口(F0/10),再去查看目的MAC,因为之前已经有记录,所以直接从记录的接口(F0/1)进行转发(单播)。
- 交换机学习源MAC,广播数据帧,接收方回应,回应使用单播直接转发。
2.路由器的相关知识
路由器属于是三层(网络层)设备;
网络层所封装的ip头部;
网络层的功能:
a).进行逻辑地址(ip地址)寻址,实现不同网络(网络地址不同或者是内网和外网分割,内网的ip地址是无法在外网进行路由的)直接的路径选择;
b)去查找目的是否可以到达,如果可以到达,选择一条最优的路径,如果不能到达,直接返回给发送方一个消息;
网络层所传输的PDU(传输数据单元)是数据包(ip数据包)。
3.网络层IP数据包的格式
a)IPv4的:
版本:标识当前使用的IP版本(IPv4/IPv6);
首部长度:由于ip数据报文的首部有一个可选项,造成首部长度是可变的,所以需要定义;
区分服务:为了保障更好的服务,主要是在IP层做Qos(服务质量/优先级和服务类型);
总长度:主要用来标识整个数据包的长度;
标识、标志、片位移:上层来的数据到ip层会被分成分片,这几个字段用来对数据包进行标识,使数据到达目的端重组时不会乱序。
生存时间(TTL值):数据包在路由器转发消耗时间的如果小于1秒,TTL就会减1;
协议:标志上层数据是使用的何种协议(TCP是6或UDP是17);
首部校验:校验数据报文的首部;
源地址:发送方的IP地址;
目的地址:接收方的IP地址;
a)IPv6的:
4.路由器的工作原理
- 路由:
从源主机到目标主机的转发过程(跨网络访问);
确定最佳路径(手动指定、根据动态路由协商方式);
通过网络传输信息;
-
路由器工作原理:
工作原理
- 路由表:
直连路由:当路由器的接口配置好对应的IP地址并开启接口后自动生成;
非直接路由:需要手动配置静态路由或者使用动态路由协议;
- 静态路由:
由管理员手动配置,不灵活,而且是单向;
特殊的静态路由:默认路由,当在路由器中找不到目标网络的路由条目时,再去查看默认路由;(应用场景:用于末节网络,路由器一端只连接了一个网络);
- 动态路由:
通过某种动态路由协议自动的去建立自己的路由表
常见的动态路由协议:RIP、OSPF、IS-IS、BGP、IGRP;
5.路由器转发数据包的封装过程
-
操作步骤:
PC1打开一个接口
PC2和PC1一样
R1打开两个,R2同样
-
配置PC1:
PC1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
PC1(config)#int f0/0
//添加IP
PC1(config-if)#ip add 192.168.10.1 255.255.255.0
PC1(config-if)#no shutdown
*Mar 1 00:02:40.135: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
*Mar 1 00:02:41.135: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
PC1(config-if)#exit
PC1(config)#no ip routing
//添加网关
PC1(config)#ip default-gateway 192.168.10.253
- 配置PC2:
PC2#conf t
Enter configuration commands, one per line. End with CNTL/Z.
PC2(config)#int f0/0
PC2(config-if)#ip add 192.168.30.1 255.255.255.0
PC2(config-if)#no shutdown
PC2(config-if)#exit
PC2(config)#no ip routing
PC2(config)#ip default-gateway 192.168.30.252
PC2(config)#
- 配置R1:
R1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#int f0/0
R1(config-if)#ip add 192.168.10.253 255.255.255.0
R1(config-if)#no shut
R1(config-if)#exit
R1(config)#int f1/0
R1(config-if)#ip add 192.168.20.1 255.255.255.0
R1(config-if)#no shut
R1(config-if)#exit
//在全局模式下添加静态路由
R1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#ip route 192.168.30.0 255.255.255.0 192.168.20.2
R1(config)#
- 配置R2:
R2#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R2(config)#int f0/0
R2(config-if)#ip add 192.168.30.252 255.255.255.0
R2(config-if)#no shut
R2(config-if)#exit
R2(config)#int f1/0
R2(config-if)#ip add 192.168.20.2 255.255.255.0
R2(config-if)#no shut
R2(config-if)#exit
//在全局模式下添加静态路由
2#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R2(config)#ip route 192.168.10.0 255.255.255.0 192.168.20.1
R2(config)#
R2#show int f0/0
FastEthernet0/0 is up, line protocol is up
Hardware is AmdFE, address is cc02.14dc.0000 (bia cc02.14dc.0000)
MTU 1500 bytes, BW 100000 Kbit/sec, DLY 100 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation ARPA, loopback not set
Keepalive set (10 sec)
Full-duplex, 100Mb/s, 100BaseTX/FX
ARP type: ARPA, ARP Timeout 04:00:00
Last input 00:01:00, output 00:00:05, output hang never
Last clearing of "show interface" counters never
Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
Queueing strategy: fifo
Output queue: 0/40 (size/max)
5 minute input rate 0 bits/sec, 0 packets/sec
5 minute output rate 0 bits/sec, 0 packets/sec
7 packets input, 2373 bytes
Received 7 broadcasts, 0 runts, 0 giants, 0 throttles
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 watchdog
0 input packets with dribble condition detected
43 packets output, 4804 bytes, 0 underruns
0 output errors, 0 collisions, 1 interface resets
0 unknown protocol drops
--More--
R2#show int f1/0
FastEthernet1/0 is up, line protocol is up
Hardware is AmdFE, address is cc02.14dc.0010 (bia cc02.14dc.0010)
MTU 1500 bytes, BW 100000 Kbit/sec, DLY 100 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation ARPA, loopback not set
Keepalive set (10 sec)
Full-duplex, 100Mb/s, 100BaseTX/FX
ARP type: ARPA, ARP Timeout 04:00:00
Last input 00:00:33, output 00:00:06, output hang never
Last clearing of "show interface" counters never
Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
Queueing strategy: fifo
Output queue: 0/40 (size/max)
5 minute input rate 0 bits/sec, 0 packets/sec
5 minute output rate 0 bits/sec, 0 packets/sec
7 packets input, 2366 bytes
Received 7 broadcasts, 0 runts, 0 giants, 0 throttles
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 watchdog
0 input packets with dribble condition detected
50 packets output, 5502 bytes, 0 underruns
0 output errors, 0 collisions, 1 interface resets
0 unknown protocol drops
--More--

PC1#
*Mar 1 00:11:39.371: %SYS-5-CONFIG_I: Configured from console by console
PC1#ping 192.168.100.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.100.1, timeout is 2 seconds:
...!!
Success rate is 40 percent (2/5), round-trip min/avg/max = 148/160/172 ms
PC1#ping 192.168.100.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.100.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 120/156/188 ms
PC1访问PC2
第一个报文:源ip192.168.10.1,目的ip192.168.30.1;源MAC:PC1(f0/0)接口,目的MAC:R1(f0/0)接口;
当R1收到PC1的数据报文后,会解封到网络层,查看目的地是否可达,如果可达转发到对应的接口,重新进行二次封装,源MAC变成了R1(f1/0)接口,目的MAC变成了R2(f1/0)接口的MAC;
当R2收到数据报文后,同样会解封到网络层,看目的地是否可达,目的和自己是同一网络;肯定时可达,将数据报文交给对应的接口,重新进行二次封装,源MAC变成R2(f0/0)接口的MAC,目的MAC就变成了PC2的MAC。
6.静态路由
静态路由:有管理员手工配置,是单向的,缺乏灵活性;
默认路由:静态路由的一种,是直接指定目标为任何地方;
- 静态路由的配置:
//ip route 目标网络(目标的网络地址) 目标的子网掩码 下一跳地址(下一个路由器接口的ip地址或发往下一个路由器的本地接口)
ip route 192.168.10.0 255.255.255.0 192.168.30.2
ip route 192.168.10.0 255.255.255.0 f1/0
- 默认路由的配置:
ip route 0.0.0.0.0.0.0.0 192.168.30.2
ip route 0.0.0.0.0.0.0.0 f1/0
- 查看路由条目:
show ip route


- 配置PC1:
PC1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
PC1(config)#int f0/0
PC1(config-if)#ip add 192.168.10.1
% Incomplete command.
PC1(config-if)#ip add 192.168.10.1 255.255.255.0
PC1(config-if)#no shut
PC1(config-if)#exti
*Mar 1 00:01:41.099: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
*Mar 1 00:01:42.099: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
PC1(config-if)#exit
PC1(config)#no ip routing
//添加网关
PC1(config)#ip default-gateway 192.168.10.2
- 配置R2:
R2#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R2(config)#int f0/0
R2(config-if)#ip add 192.168.10.2 255.255.255.0
R2(config-if)#no shut
*Mar 1 00:02:35.359: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
*Mar 1 00:02:36.359: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
R2(config-if)#exit
R2(config)#int f1/0
R2(config-if)#ip add 192.168.0.1 255.255.255.0
R2(config-if)#no shut
R2(config-if)#exit
*Mar 1 00:03:36.559: %LINK-3-UPDOWN: Interface FastEthernet1/0, changed state to up
*Mar 1 00:03:37.559: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet1/0, changed state to up
R2(config-if)#exit
R2(config)#int f2/0
R2(config-if)#ip add 192.168.1.1 255.255.255.0
R2(config-if)#no shut
- 配置R1:
R1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#int f0/0
R1(config-if)#ip add 192.168.0.2 255.255.255.0
R1(config-if)#no shut
R1(config-if)#
*Mar 1 00:01:39.583: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
*Mar 1 00:01:40.583: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
R1(config-if)#exit
R1(config)#int f1/0
R1(config-if)#ip add 192.168.2.1 255.255.255.0
R1(config-if)#no shut
- 配置R3:
R3(config)#int f0/0
R3(config-if)#ip add 192.168.2.2 255.255.255.0
R3(config-if)#no shut
*Mar 1 00:01:04.539: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
*Mar 1 00:01:05.539: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
R3(config-if)#exit
R3(config)#int f1/0
R3(config-if)#ip add 192.168.1.2 255.255.255.0
R3(config-if)#no shut
*Mar 1 00:02:14.451: %LINK-3-UPDOWN: Interface FastEthernet1/0, changed state to up
*Mar 1 00:02:15.451: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet1/0, changed state to up
R3(config-if)#exit
R3(config)#int f2/0
R3(config-if)#ip add 192.168.20.2 255.255.255.0
R3(config-if)#no shut
- 配置PC2:
PC2#conf t
Enter configuration commands, one per line. End with CNTL/Z.
PC2(config)#int f0/0
//添加
PC2(config-if)#ip add 192.168.20.1 255.255.255.0
//启动
PC2(config-if)#no shut
PC2(config-if)#
*Mar 1 00:01:01.939: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
*Mar 1 00:01:02.939: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
- 配置路由:
R2#conf
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z.
R2(config)#ip route 192.168.20.0 255.255.255.0 192.168.0.2
R1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#ip route 192.168.20.0 255.255.255.0 192.168.2.2
R3#conf t
Enter configuration commands, one per line. End with CNTL/Z.
//配置默认路由
R3(config)#ip route 0.0.0.0 0.0.0.0 f1/0
R3(config)#

网友评论