---
# System prepended metadata

title: Mininet-containernet4 Static Routing/Dynamic Routing

---

# Mininet-containernet4 Static Routing/Dynamic Routing

## Static Routing 
Topology 

![](https://hackmd.io/_uploads/HJNOeVRrh.png)

這個Topology 共有5個網段

192.168.1.0
192.168.2.0
12.1.1.0
13.1.1.0
23.1.1.0

Notice:

script 再設定的時候 要注意 eth 的順序 


Script

```
#!/usr/bin/env python

from mininet.net import Containernet
from mininet.cli import CLI
from mininet.link import TCLink, Link
 
if '__main__' == __name__:
  net = Containernet(link=TCLink)
  h1 = net.addHost('h1')
  h2 = net.addHost('h2')
  r1 = net.addHost('r1')
  r2 = net.addHost('r2')
  r3 = net.addHost('r3')
  Link(h1, r1)
  Link(h2, r2)
  Link(r1, r2)
  Link(r1, r3)
  Link(r2, r3)
  net.build()
 
  r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  r2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  r3.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  r1.cmd("ifconfig r1-eth0 0")
  r1.cmd("ifconfig r1-eth1 0")
  r1.cmd("ifconfig r1-eth2 0")
  r2.cmd("ifconfig r2-eth0 0")
  r2.cmd("ifconfig r2-eth1 0")
  r2.cmd("ifconfig r2-eth2 0")
  r3.cmd("ifconfig r3-eth0 0")
  r3.cmd("ifconfig r3-eth1 0")
  r1.cmd("ip addr add 192.168.1.254/24 brd + dev r1-eth0")
  r1.cmd("ip addr add 12.1.1.1/24 brd + dev r1-eth1")
  r1.cmd("ip addr add 13.1.1.1/24 brd + dev r1-eth2")
  r2.cmd("ip addr add 192.168.2.254/24 brd + dev r2-eth0")
  r2.cmd("ip addr add 12.1.1.2/24 brd + dev r2-eth1")
  r2.cmd("ip addr add 23.1.1.2/24 brd + dev r2-eth2")
  r3.cmd("ip addr add 13.1.1.3/24 brd + dev r3-eth0")
  r3.cmd("ip addr add 23.1.1.3/24 brd + dev r3-eth1")
  r1.cmd("ip route add 192.168.2.0/24 via 12.1.1.2")
  r2.cmd("ip route add 192.168.1.0/24 via 12.1.1.1")
 
  h1.cmd("ifconfig h1-eth0 0")
  h1.cmd("ip address add 192.168.1.1/24 dev h1-eth0")
  h1.cmd("ip route add default via 192.168.1.254 dev h1-eth0")
  h2.cmd("ifconfig h2-eth0 0")
  h2.cmd("ip address add 192.168.2.1/24 dev h2-eth0")
  h2.cmd("ip route add default via 192.168.2.254 dev h2-eth0")
  CLI(net)
  net.stop()

```

Static Routing  H1 <---------> H2 



R1 Routing table 

```
root@ubuntu:/home/user/server-test/test-quagga# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
12.1.1.0        0.0.0.0         255.255.255.0   U     0      0        0 r1-eth1
13.1.1.0        0.0.0.0         255.255.255.0   U     0      0        0 r1-eth2
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 r1-eth0
192.168.2.0     12.1.1.2        255.255.255.0   UG    0      0        0 r1-eth1
```

```
r1.cmd("ip route add 192.168.2.0/24 via 12.1.1.2")
```

R2 Routing Table 

```
root@ubuntu:/home/user/server-test/test-quagga# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
12.1.1.0        0.0.0.0         255.255.255.0   U     0      0        0 r2-eth1
23.1.1.0        0.0.0.0         255.255.255.0   U     0      0        0 r2-eth2
192.168.1.0     12.1.1.1        255.255.255.0   UG    0      0        0 r2-eth1
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 r2-eth0
```


```
r2.cmd("ip route add 192.168.1.0/24 via 12.1.1.1")
```

---


## Dynamic Routing Protocol 

每隔隔固定時間跟其他Router 交換訊息

Routing internet protocol (RIPv2) 使用 "quagga"

Script 

```
#!/usr/bin/env python

from mininet.net import Containernet 
from mininet.cli import CLI
from mininet.link import TCLink, Link
from mininet.log import info, setLogLevel
 
if '__main__' == __name__:
  setLogLevel('info')
  net = Containernet()
  h1 = net.addHost('h1')
  h2 = net.addHost('h2')
  r1 = net.addDocker('r1', dimage="kathara/quagga:latest", volumes=["/home/user/server-test/test-quagga/dynamic_routing/r1/quagga:/etc/quagga"])
  r2 = net.addDocker('r2', dimage="kathara/quagga:latest", volumes=["/home/user/server-test/test-quagga/dynamic_routing/r2/quagga:/etc/quagga"])
  r3 = net.addDocker('r3', dimage="kathara/quagga:latest", volumes=["/home/user/server-test/test-quagga/dynamic_routing/r3/quagga:/etc/quagga"])
  net.addLink(h1, r1)
  net.addLink(h2, r2)
  net.addLink(r1, r2)
  net.addLink(r1, r3)
  net.addLink(r2, r3)
  net.build()
 
  r1.cmd("ifconfig r1-eth0 0")
  r1.cmd("ifconfig r1-eth1 0")
  r1.cmd("ifconfig r1-eth2 0")
  r2.cmd("ifconfig r2-eth0 0")
  r2.cmd("ifconfig r2-eth1 0")
  r2.cmd("ifconfig r2-eth2 0")
  r3.cmd("ifconfig r3-eth0 0")
  r3.cmd("ifconfig r3-eth1 0")
  r1.cmd("ip addr add 192.168.1.254/24 brd + dev r1-eth0")
  r1.cmd("ip addr add 12.1.1.1/24 brd + dev r1-eth1")
  r1.cmd("ip addr add 13.1.1.1/24 brd + dev r1-eth2")
  r2.cmd("ip addr add 192.168.2.254/24 brd + dev r2-eth0")
  r2.cmd("ip addr add 12.1.1.2/24 brd + dev r2-eth1")
  r2.cmd("ip addr add 23.1.1.2/24 brd + dev r2-eth2")
  r3.cmd("ip addr add 13.1.1.3/24 brd + dev r3-eth0")
  r3.cmd("ip addr add 23.1.1.3/24 brd + dev r3-eth1")
  r1.cmd("/etc/init.d/quagga restart")	
  r2.cmd("/etc/init.d/quagga restart")
  r3.cmd("/etc/init.d/quagga restart")
  h1.cmd("ifconfig h1-eth0 0")
  h1.cmd("ip address add 192.168.1.1/24 dev h1-eth0")
  h1.cmd("ip route add default via 192.168.1.254 dev h1-eth0")
  h2.cmd("ifconfig h2-eth0 0")
  h2.cmd("ip address add 192.168.2.1/24 dev h2-eth0")
  h2.cmd("ip route add default via 192.168.2.254 dev h2-eth0")
  CLI(net)
  net.stop()

```

設定檔
```
volumes=["/home/user/server-test/test-quagga/dynamic_routing/r1/quagga:/etc/quagga"]
```

![](https://hackmd.io/_uploads/rJTqYE0Bh.png)

ripd.conf  -> RIP 設定

```
hostname R1
password zebra
debug rip events
debug rip packet
router rip
 version 2
 network 192.168.1.0/24
 network 12.1.1.0/24
 network 13.1.1.0/24
interface r1-eth1
 no ip rip authentication mode
log file /var/log/quagga/zebra.log

```

## Enable quagga

```
  r1.cmd("/etc/init.d/quagga restart")	
  r2.cmd("/etc/init.d/quagga restart")
  r3.cmd("/etc/init.d/quagga restart")
```


## R1 Routing Table 

Metric -> (Routing Metric)

越小優先

```
root@ubuntu:/home/user/server-test/test-quagga/dynamic_routing/r1/quagga# docker exec -it 090 bash
root@r1:/# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.17.0.1      0.0.0.0         UG    0      0        0 eth0
12.1.1.0        0.0.0.0         255.255.255.0   U     0      0        0 r1-eth1
13.1.1.0        0.0.0.0         255.255.255.0   U     0      0        0 r1-eth2
23.1.1.0        12.1.1.2        255.255.255.0   UG    20     0        0 r1-eth1
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 r1-eth0
192.168.2.0     12.1.1.2        255.255.255.0   UG    20     0        0 r1-eth1

```

### Login R1 router(RIPv2 Configure)

RIPv2 Protocol 走 2601

```
root@r1:/# telnet 127.0.0.1 2601
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.

Hello, this is Quagga (version 1.0.20160315).
Copyright 1996-2005 Kunihiro Ishiguro, et al.


User Access Verification

Password: 

```

Default Password: zebra


R1 ip Route

```
R1> show ip route
Codes: K - kernel route, C - connected, S - static, R - RIP,
       O - OSPF, I - IS-IS, B - BGP, P - PIM, A - Babel,
       > - selected route, * - FIB route

K>* 0.0.0.0/0 via 172.17.0.1, eth0
C>* 12.1.1.0/24 is directly connected, r1-eth1
C>* 13.1.1.0/24 is directly connected, r1-eth2
R>* 23.1.1.0/24 [120/2] via 12.1.1.2, r1-eth1, 00:10:45
C>* 127.0.0.0/8 is directly connected, lo
C>* 172.17.0.0/16 is directly connected, eth0
C>* 192.168.1.0/24 is directly connected, r1-eth0
R>* 192.168.2.0/24 [120/2] via 12.1.1.2, r1-eth1, 00:10:45

```

R -> RIP


172.17.0.1 -> Linux 本身docker (soft switch)


r1 -> 172.17.0.2 (For communicating with Linux)
r2 -> 172.17.0.3
r3 -> 172.17.0.4



