拓扑

TOPO

组播源PC1

定时发送i am the xxth message to 239.255.255.249 到组播组239.255.255.249

配置RouterB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# root @ routerB in / [15:15:39]
$ iptables -t nat -D POSTROUTING 1 #关闭NAT,清空nat表

# root @ routerB in / [15:15:42]
$ iptables -t nat -nvL # 查看nat表已清空
Chain PREROUTING (policy ACCEPT 8 packets, 569 bytes)
pkts bytes target prot opt in out source destination

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

# root @ routerB in / [15:51:02]
$ ip link set eth0 multicast off # 关闭eth0的组播功能,确保组播走的是gre隧道

# root @ routerB in / [15:51:17]
$ ip link set eth1 multicast off # 关闭eth1的组播功能,确保组播走的是gre隧道

# root @ routerB in / [15:51:23]
$ ip addr show eth0 # 看到 <> 内已经没了MULTICAST
2: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:e2:69:24:0b:30 brd ff:ff:ff:ff:ff:ff
inet 20.1.1.2/24 brd 20.1.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::2e2:69ff:fe24:b30/64 scope link
valid_lft forever preferred_lft forever

# root @ routerB in / [16:18:55]
$ ip addr show eth1 # 看到 <> 内已经没了MULTICAST
3: eth1: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:e2:69:24:0b:31 brd ff:ff:ff:ff:ff:ff
inet 30.1.1.1/24 brd 30.1.1.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::2e2:69ff:fe24:b31/64 scope link
valid_lft forever preferred_lft forever

# root @ routerB in / [16:50:09]
$ cat /etc/quagga/ospfd.conf # 配置ospf
hostname Router_B
password testpassword
enable password testpassword
router ospf
ospf router-id 20.1.1.2
network 30.1.1.0/24 area 0
network 20.1.1.0/24 area 0
debug ospf event
log file /usr/local/etc/ospfd.log

# root @ routerB in / [16:50:14]
$ systemctl restart quagga.service # 重启quagg(ospf)

# root @ routerB in / [17:00:36]
$ ip r # 查看路由表
20.1.1.0/24 dev eth0 proto kernel scope link src 20.1.1.2
30.1.1.0/24 dev eth1 proto kernel scope link src 30.1.1.1

配置gre

1
2
3
4
5
6
#routerA
remoteip=30.1.1.2
localip=20.1.1.1
greip=10.10.10.1
grepeerip=10.10.10.2
grename=gre1
1
2
3
4
5
6
#routerC
remoteip=20.1.1.1
localip=30.1.1.2
greip=10.10.10.2
grepeerip=10.10.10.1
grename=gre1
1
2
3
4
5
6
7
8
9
#routerA and routerC
lsmod|grep ip_gre || modprobe ip_gre
#ping -c1 ${remoteip} || { echo "make sure the network is ok plz"; exit 1; }
ip addr show ${grename} && { echo "${grename} is already exist"; exit 1; }

ip tunnel add ${grename} mode gre remote ${remoteip} local ${localip} ikey 1 okey 1 ttl 255;
ip addr add ${greip} dev ${grename} peer ${grepeerip};
ip link set ${grename} multicast on up; #mtu 1400; # 注意一定要multicast on
ip addr show ${grename};

查看gre隧道接口

1
2
3
4
5
6
7
8
# root @ routerA in / [16:53:20]
$ ip addr show gre1
7: gre1@NONE: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1472 qdisc noqueue state UNKNOWN group default qlen 1
link/gre 20.1.1.1 peer 30.1.1.2
inet 10.10.10.1 peer 10.10.10.2/32 scope global gre1
valid_lft forever preferred_lft forever
inet6 fe80::200:5efe:1401:101/64 scope link
valid_lft forever preferred_lft forever
1
2
3
4
5
6
7
8
# root @ routerC in / [16:54:38]
$ ip addr show gre1
16: gre1@NONE: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1472 qdisc noqueue state UNKNOWN group default qlen 1
link/gre 30.1.1.2 peer 20.1.1.1
inet 10.10.10.2 peer 10.10.10.1/32 scope global gre1
valid_lft forever preferred_lft forever
inet6 fe80::200:5efe:1e01:102/64 scope link
valid_lft forever preferred_lft forever

测试隧道连接

routerA ping -I 10.10.10.1 10.10.10.2 -c2 通路
routerC ping -I 10.10.10.2 10.10.10.1 -c2 通路

配置router A和C的ospf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# root @ routerA in / [16:56:48]
$ cat /etc/quagga/ospfd.conf
hostname Router_A
password testpassword
enable password testpassword
router ospf
ospf router-id 10.10.10.1 # 这里用gre隧道的ip
network 10.1.1.0/24 area 0
network 10.10.10.0/24 area 0 # 使用隧道的网段
debug ospf event
log file /usr/local/etc/ospfd.log

# root @ routerA in / [17:05:03]
$ systemctl restart quagga.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# root @ routerC in / [17:06:07]
$ cat /etc/quagga/ospfd.conf
hostname Router_C
password testpassword
enable password testpassword
router ospf
ospf router-id 10.10.10.2 # 这里用gre隧道的ip
network 10.2.1.0/24 area 0
network 10.10.10.0/24 area 0 # 使用隧道的网段
debug ospf event
log file /usr/local/etc/ospfd.log

# root @ routerC in / [17:06:19]
$ systemctl restart quagga.service

查看ospf生成的路由

1
2
3
4
5
6
7
# root @ routerA in / [17:05:10]
$ ip r
default via 20.1.1.2 dev eth1
10.1.1.0/24 dev eth0 proto kernel scope link src 10.1.1.1
10.2.1.0/24 via 10.10.10.2 dev gre1 proto zebra metric 20 # 这条由zebra生成的就是
10.10.10.2 dev gre1 proto kernel scope link src 10.10.10.1
20.1.1.0/24 dev eth1 proto kernel scope link src 20.1.1.1
1
2
3
4
5
6
7
8
# root @ routerC in / [17:06:24]
$ ip r
default via 30.1.1.1 dev eth1
10.1.1.0/24 via 10.10.10.1 dev gre1 proto zebra metric 20 # 这条由zebra生成的就是
10.2.1.0/24 dev eth0 proto kernel scope link src 10.2.1.1
10.10.10.1 dev gre1 proto kernel scope link src 10.10.10.2
30.1.1.0/24 dev eth1 proto kernel scope link src 30.1.1.2
192.168.255.0/24 dev eth3 proto kernel scope link src 192.168.255.1
1
2
3
4
# root @ routerB in / [17:00:38]
$ ip r # routerB确实没有获取到gre相关路由
20.1.1.0/24 dev eth0 proto kernel scope link src 20.1.1.2
30.1.1.0/24 dev eth1 proto kernel scope link src 30.1.1.1

测试ospf路由通路

PC1 ping 10.2.1.2 通路
PC2 ping 10.1.1.2 通路

配置pimd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# root @ routerA in / [17:09:14]
$ diff -rauN /etc/pimd.conf.bak /etc/pimd.conf
--- /etc/pimd.conf.bak 2021-10-18 16:43:11.313255253 +0800
+++ /etc/pimd.conf 2021-10-19 15:41:13.738745717 +0800
@@ -132,7 +132,7 @@
#group-prefix 224.0.0.0 masklen 4

# Static rendez-vous point
-#rp-address 192.168.10.1 224.0.0.0/4 # 自选举方式
+rp-address 10.10.10.1 224.0.0.0/4 # 自选举太慢,手动指定汇聚点(这里用gre接口ip,也可用routerA的eth0的ip 10.1.1.1)

# Switch to shortest-path tree after first packet, but only after 100 sec.
spt-threshold packets 0 interval 100

# root @ routerA in / [17:20:01]
$ systemctl restart pimd.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# root @ routerC in / [17:10:07]
$ diff -rauN /etc/pimd.conf.bak /etc/pimd.conf
--- /etc/pimd.conf.bak 2021-09-27 14:49:18.706881067 +0800
+++ /etc/pimd.conf 2021-10-19 15:40:35.242644448 +0800
@@ -112,7 +112,9 @@

# By default, all non-loopback multicast capable interfaces are enabled.
# If you want to use loopback, set the interface multicast flag on it.
-#phyint eth0 disable
+phyint eth3 disable # 关闭不想要进行组播的端口

# IGMP default query interval and querier timeout. The latter should
# per RFC always be (robustness * interval) + (query-response / 2), for
@@ -132,7 +134,7 @@
#group-prefix 224.0.0.0 masklen 4

# Static rendez-vous point
-#rp-address 192.168.10.1 224.0.0.0/4 # 自选举方式
+rp-address 10.10.10.1 224.0.0.0/4 # 自选举太慢,手动指定汇聚点(这里用gre接口ip,也可用routerA的eth0的ip 10.1.1.1)

# Switch to shortest-path tree after first packet, but only after 100 sec.
spt-threshold packets 0 interval 100

# root @ routerC in / [17:22:37]
$ systemctl restart pimd.service

查看组播路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# root @ routerA in / [17:22:13]
$ pimd -r
Virtual Interface Table ======================================================
Vif Local Address Subnet Thresh Flags Neighbors
--- --------------- ------------------ ------ --------- -----------------
0 10.1.1.1 10.1.1/24 1 DR NO-NBR
1 20.1.1.1 20.1.1/24 1 DR NO-NBR
2 10.10.10.1 10.10.10.1/32 1 PIM 10.10.10.2
3 10.1.1.1 register_vif0 1

Vif SSM Group Sources

Multicast Routing Table ======================================================
----------------------------------- (*,G) ------------------------------------
Source Group RP Address Flags
--------------- --------------- --------------- ---------------------------
INADDR_ANY 239.255.255.249 10.10.10.1 WC RP
Joined oifs: ..j.
Pruned oifs: ....
Leaves oifs: ....
Asserted oifs: ....
Outgoing oifs: ..o.
Incoming : ...I

TIMERS: Entry JP RS Assert VIFS: 0 1 2 3
200 55 0 0 0 0 200 0
----------------------------------- (S,G) ------------------------------------
Source Group RP Address Flags
--------------- --------------- --------------- ---------------------------
10.1.1.2 239.255.255.249 10.10.10.1 SPT CACHE SG
Joined oifs: ..jj
Pruned oifs: ....
Leaves oifs: ....
Asserted oifs: ....
Outgoing oifs: ..oo
Incoming : I...

TIMERS: Entry JP RS Assert VIFS: 0 1 2 3
165 20 0 0 0 0 140 0
Source Group RP Address Flags
--------------- --------------- --------------- ---------------------------
10.10.10.1 239.255.255.249 10.10.10.1 RP SG
Joined oifs: ....
Pruned oifs: ..p.
Leaves oifs: ....
Asserted oifs: ....
Outgoing oifs: ....
Incoming : ...I

TIMERS: Entry JP RS Assert VIFS: 0 1 2 3
205 55 0 0 0 0 0 0
--------------------------------- (*,*,G) ------------------------------------
Number of Groups: 1
Number of Cache MIRRORs: 1
------------------------------------------------------------------------------


# root @ routerA in / [17:24:23]
$ ip mroute show # 看到PC1发的组播239.255.255.249数据从eth0进来,从gre1出去
(10.1.1.2, 239.255.255.249) Iif: eth0 Oifs: gre1 pimreg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# root @ routerC in / [17:26:09]
$ pimd -r
Virtual Interface Table ======================================================
Vif Local Address Subnet Thresh Flags Neighbors
--- --------------- ------------------ ------ --------- -----------------
0 10.2.1.1 10.2.1/24 1 DR NO-NBR
1 30.1.1.2 30.1.1/24 1 DR NO-NBR
2 192.168.255.1 192.168.255 1 DISABLED
3 10.10.10.2 10.10.10.2/32 1 DR PIM 10.10.10.1
4 10.2.1.1 register_vif0 1

Vif SSM Group Sources

Multicast Routing Table ======================================================
----------------------------------- (*,G) ------------------------------------
Source Group RP Address Flags
--------------- --------------- --------------- ---------------------------
INADDR_ANY 239.255.255.249 10.10.10.1 WC RP
Joined oifs: .....
Pruned oifs: .....
Leaves oifs: l....
Asserted oifs: .....
Outgoing oifs: o....
Incoming : ...I.

TIMERS: Entry JP RS Assert VIFS: 0 1 2 3 4
0 55 0 0 0 0 0 0 0
----------------------------------- (S,G) ------------------------------------
Source Group RP Address Flags
--------------- --------------- --------------- ---------------------------
10.10.10.1 239.255.255.249 10.10.10.1 SPT CACHE SG
Joined oifs: ....j
Pruned oifs: ....p
Leaves oifs: l....
Asserted oifs: .....
Outgoing oifs: o....
Incoming : ...I.

TIMERS: Entry JP RS Assert VIFS: 0 1 2 3 4
195 55 1 0 0 0 0 0 0
--------------------------------- (*,*,G) ------------------------------------
Number of Groups: 1
Number of Cache MIRRORs: 1
------------------------------------------------------------------------------


# root @ routerC in / [17:26:10]
$ ip mroute show # 看到组播239.255.255.249数据从gre1进来,从eth0出去发给PC2
(10.10.10.1, 239.255.255.249) Iif: gre1 Oifs: eth0 pimreg

接收端PC2

1
2
3
4
5
6
7
8
# zh @ li in ~ [17:35:30]
$ ./rcver 239.255.255.249 enp2s0
---MESSAGE FROM:10.2.1.1:44853---
[i am the 32822th message to 239.255.255.249]

---MESSAGE FROM:10.2.1.1:44853---
[i am the 32821th message to 239.255.255.249]
...