# 20210419 期中週

## 課堂資料

### MIniedit

{% embed url="<https://youtu.be/4uZz_9oM2T0>" %}

{% embed url="<https://youtu.be/FnPRYnE6dzw>" %}

## 課堂練習

### MiniEdit 01

{% tabs %}
{% tab title="Terminal 01" %}

```
cd mininet-wifi/
cd examples/
python miniedit.py
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-Mcj7HtkH4mwQhs8F4t1%2F-Mcj8ITISAyM15YzkaFu%2Fimage.png?alt=media\&token=f9c7d416-2707-4f4e-ae5d-6e18bee17c5d)

```
cd ..
cd ..
cd test-miniedit/
gedit 1.py
```

```
python 1.py
```

```
h1 ping h2 -c 5
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-Mcj7HtkH4mwQhs8F4t1%2F-Mcj9XyjLpeelzT9BEeD%2Fimage.png?alt=media\&token=e8130f64-9b53-41e6-a6e1-cbc22bcf1525)
{% endtab %}

{% tab title="gedit" %}
{% hint style="info" %}

#### 1.py

```
#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import Host, OVSKernelSwitch
from mininet.cli import CLI
from mininet.link import TCLink, Intf
from mininet.log import setLogLevel, info
from subprocess import call


def myNetwork():

    net = Mininet(topo=None,
                       build=False,
                       ipBase='10.0.0.0/8')

    info( '*** Adding controller\n' )
    info( '*** Add switches/APs\n')
    s1  = net.addSwitch('s1', cls=OVSKernelSwitch, failMode='standalone')

    info( '*** Add hosts/stations\n')
    h1 = net.addHost('h1', cls=Host, ip='10.0.0.1', defaultRoute=None)
    h3 = net.addHost('h3', cls=Host, ip='10.0.0.3', defaultRoute=None)
    h2 = net.addHost('h2', cls=Host, ip='10.0.0.2', defaultRoute=None)

    info( '*** Add links\n')
    net.addLink(h1, s1)
    net.addLink(h2, s1)
    net.addLink(h3, s1)

    info( '*** Starting network\n')
    net.build()
    info( '*** Starting controllers\n')
    for controller in net.controllers:
        controller.start()

    info( '*** Starting switches/APs\n')
    net.get('s1').start([])

    info( '*** Post configure nodes\n')

    CLI(net)
    net.stop()


if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()
```

{% endhint %}
{% endtab %}
{% endtabs %}

```
cd ..
```

### MiniEdit 02

{% tabs %}
{% tab title="Terminal 01" %}

```
cd mininet-wifi/
cd examples/
python miniedit.py
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-Mcj9_vIPjmJ31-izL7U%2F-McjBTVws_SsMGw-lr6P%2Fimage.png?alt=media\&token=76fd97ea-0220-4afe-a4ab-fdf521577810)

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-Mcj9_vIPjmJ31-izL7U%2F-McjD6AjyX_tZwyf_xby%2Fimage.png?alt=media\&token=ce877b4c-2138-4549-ac02-69f3ed177f0c)

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-Mcj9_vIPjmJ31-izL7U%2F-McjDGKycZy96uTzStiP%2Fimage.png?alt=media\&token=ccf07549-016b-4ee2-9265-1273cc88b6c3)

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-Mcj9_vIPjmJ31-izL7U%2F-McjDPYmOV8fNefDzrAf%2Fimage.png?alt=media\&token=7e5d874c-a4dd-4798-9231-9e4ec3f2a141)

```
cd ..
cd ..
cd test-miniedit/
gedit 2.py
```

```
python 2.py
```

```
pingall -c 5
```

{% endtab %}

{% tab title="gedit" %}
{% hint style="info" %}

#### 2.py

```
#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import Host, Node, OVSKernelSwitch
from mininet.cli import CLI
from mininet.link import TCLink, Intf
from mininet.log import setLogLevel, info
from subprocess import call


def myNetwork():

net = Mininet(topo=None, build=False, ipBase='10.0.0.0/8')

info( '*** Adding controller\n' )
info( '*** Add switches/APs\n')
r1 = net.addHost('r1', ip='0.0.0.0')
r1.cmd('sysctl -w net.ipv4.ip_forward=1')
s3  = net.addSwitch('s3', cls=OVSKernelSwitch, failMode='standalone')
r2 = net.addHost('r2', ip='0.0.0.0')
r2.cmd('sysctl -w net.ipv4.ip_forward=1')

info( '*** Add hosts/stations\n')
h3 = net.addHost('h3', cls=Host, ip='192.168.20.2/24', defaultRoute='via 192.168.20.254')
h1 = net.addHost('h1', cls=Host, ip='192.168.10.1/24', defaultRoute='via 192.168.10.254')
h2 = net.addHost('h2', cls=Host, ip='192.168.20.1/24', defaultRoute='via 192.168.20.254')

info( '*** Add links\n')
net.addLink(s3, h3)
net.addLink(h1, r1)
net.addLink(r1, r2)
net.addLink(r2, s3)
net.addLink(h2, s3)

info( '*** Starting network\n')
net.build()
info( '*** Starting controllers\n')
for controller in net.controllers:
    controller.start()

info( '*** Starting switches/APs\n')
net.get('s3').start([])

info( '*** Post configure nodes\n')
r1.cmd("ifconfig r1-eth0 0")
r1.cmd("ifconfig r1-eth1 0")
r1.cmd("ifconfig r1-eth0 192.168.10.254/24")
r1.cmd("ifconfig r1-eth1 10.0.0.1/24")
r1.cmd("ip route add 192.168.20.0/24 via 10.0.0.2")
r2.cmd("ifconfig r2-eth0 0")
r2.cmd("ifconfig r2-eth1 0")
r2.cmd("ifconfig r2-eth0 10.0.0.2/24")
r2.cmd("ifconfig r2-eth1 192.168.20.254/24")
r2.cmd("ip route add 192.168.10.0/24 via 10.0.0.1")
CLI(net)
net.stop()


if __name__ == '__main__':
setLogLevel( 'info' )
myNetwork()
```

{% endhint %}
{% endtab %}
{% endtabs %}

```
cd ..
```

### MiniEdit 03

{% tabs %}
{% tab title="Terminal 01" %}

```
cd mininet-wifi/
cd examples/
python miniedit.py
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-McjED3CyYbm5dAqw5xs%2F-McjExeUMbM0YTY76vTR%2Fimage.png?alt=media\&token=7ffec2a5-74d1-4903-bc79-c2bc9ec63a34)

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-McjED3CyYbm5dAqw5xs%2F-McjEphtr2cwFQbROLrV%2Fimage.png?alt=media\&token=e3ddb856-70f1-4f0a-a2eb-0ff5a00c7022)

```
cd ..
cd ..
cd test-miniedit/
gedit 3.py
```

```
python 3.py
```

```
h1 ping h2 -c 5
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-McjED3CyYbm5dAqw5xs%2F-McjGJbPAgWfXo1BgVsP%2Fimage.png?alt=media\&token=2acad7c3-fee6-4303-bfe2-cff630efe783)
{% endtab %}

{% tab title="gedit" %}
{% hint style="info" %}

#### 3.py

```
!/usr/bin/python
from mininet.net import Mininet from mininet.node import Controller, OVSKernelSwitch, Host from mininet.cli import CLI from mininet.link import TCLink, Intf from mininet.log import setLogLevel, info from subprocess import call
def myNetwork():
net = Mininet(topo=None,
                   build=False,
                   ipBase='10.0.0.0/8')

info( '*** Adding controller\n' )
c0 = net.addController(name='c0',
                       controller=Controller,
                       protocol='tcp',
                       port=6633)

info( '*** Add switches/APs\n')
s2 = net.addSwitch('s2', cls=OVSKernelSwitch)
s1 = net.addSwitch('s1', cls=OVSKernelSwitch)

info( '*** Add hosts/stations\n')
h1 = net.addHost('h1', cls=Host, ip='10.0.0.1', defaultRoute=None)
h2 = net.addHost('h2', cls=Host, ip='10.0.0.2', defaultRoute=None)

info( '*** Add links\n')
net.addLink(s2, h2)
net.addLink(s1, h1)
net.addLink(s1, s2)

info( '*** Starting network\n')
net.build()
info( '*** Starting controllers\n')
for controller in net.controllers:
    controller.start()

info( '*** Starting switches/APs\n')
net.get('s2').start([c0])
net.get('s1').start([c0])

info( '*** Post configure nodes\n')

CLI(net)
net.stop()
if name == 'main': setLogLevel( 'info' ) myNetwork()
```

{% endhint %}
{% endtab %}

{% tab title="Terminal 02" %}

> **`h1 ping h2 -c 5` 之前**

```
ovs-ofctl dump-flows s1
```

```
ovs-ofctl dump-flows s2
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-McjGtAwyPghmVWSn8pL%2F-McjHOq9gOjUmmUgGPRO%2Fimage.png?alt=media\&token=ece16f05-e33a-4866-bf23-98457128a84c)

> **`h1 ping h2 -c 5` 之後**

```
ovs-ofctl dump-flows s1
```

```
ovs-ofctl dump-flows s2
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-McjGtAwyPghmVWSn8pL%2F-McjHaLSl6tBmXlwnljU%2Fimage.png?alt=media\&token=7e58b8ae-efd1-4068-9055-46b79fda23ad)
{% endtab %}
{% endtabs %}

```
cd ..
```

### MiniEdit 03-1

{% tabs %}
{% tab title="Terminal 01" %}

```
cd mininet-wifi/
cd examples/
python miniedit.py
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-McjED3CyYbm5dAqw5xs%2F-McjExeUMbM0YTY76vTR%2Fimage.png?alt=media\&token=7ffec2a5-74d1-4903-bc79-c2bc9ec63a34)

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-McjGtAwyPghmVWSn8pL%2F-McjIHBL_S6_I3h4Hi98%2Fimage.png?alt=media\&token=616f1a8d-0df4-4504-b217-0f554b602026)

```
cd ..
cd ..
cd test-miniedit/
gedit 3-1.py
```

```
python 3-1.py
```

```
h1 ping h2 -c 5
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-McjGtAwyPghmVWSn8pL%2F-McjIxuXQiPMjtvGZhU7%2Fimage.png?alt=media\&token=498c0861-5f24-4755-81c8-3b0ee1224b48)

> **載入模組後重新`h1 ping h2`**

```
h1 ping h2 -c 5
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-McjGtAwyPghmVWSn8pL%2F-McjKXud4FGwujTGBfoN%2Fimage.png?alt=media\&token=0f3c78a0-d361-498e-97a1-f9d11599a17a)
{% endtab %}

{% tab title="gedit" %}
{% hint style="info" %}

#### 3-1.py

```
#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import RemoteController, OVSKernelSwitch, Host
from mininet.cli import CLI
from mininet.link import TCLink, Intf
from mininet.log import setLogLevel, info
from subprocess import call


def myNetwork():

    net = Mininet(topo=None,
                       build=False,
                       ipBase='10.0.0.0/8')

    info( '*** Adding controller\n' )
    c0 = net.addController(name='c0',
                           controller=RemoteController,
                           ip='127.0.0.1',
                           protocol='tcp',
                           port=6633)

    info( '*** Add switches/APs\n')
    s1 = net.addSwitch('s1', cls=OVSKernelSwitch)
    s2 = net.addSwitch('s2', cls=OVSKernelSwitch)

    info( '*** Add hosts/stations\n')
    h2 = net.addHost('h2', cls=Host, ip='10.0.0.2', defaultRoute=None)
    h1 = net.addHost('h1', cls=Host, ip='10.0.0.1', defaultRoute=None)

    info( '*** Add links\n')
    net.addLink(s2, s1)
    net.addLink(s1, h1)
    net.addLink(s2, h2)

    info( '*** Starting network\n')
    net.build()
    info( '*** Starting controllers\n')
    for controller in net.controllers:
        controller.start()

    info( '*** Starting switches/APs\n')
    net.get('s1').start([c0])
    net.get('s2').start([c0])

    info( '*** Post configure nodes\n')

    CLI(net)
    net.stop()


if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()

```

{% endhint %}
{% endtab %}

{% tab title="Terminal 02" %}

> **`h1 ping h2` 會失敗，因為是 remote controller， 切換到`/home/user/ryu/ryu/app`下，將提供的模組載入，再重新`h1 ping h2`**

```
cd ryu/ryu/app
```

```
ryu-manager simple_switch.py
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-McjGtAwyPghmVWSn8pL%2F-McjK8KL97Tv_oFzqkEp%2Fimage.png?alt=media\&token=b564cb30-48aa-42e6-b3b7-c4cdbe633f31)
{% endtab %}

{% tab title="Terminal 03" %}

> **重新`h1 ping h2`** **之後**

```
ovs-ofctl dump-flows s1
```

```
ovs-ofctl dump-flows s2
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-McjGtAwyPghmVWSn8pL%2F-McjKwIn6bdn6lzWV_a6%2Fimage.png?alt=media\&token=6ab4ddac-453d-4814-aa13-192cd2816d4d)
{% endtab %}
{% endtabs %}

## 期中題目

```
mkdir exam
```

```
cd exam
```

{% tabs %}
{% tab title="Switch" %}
![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYdOQfytr4Q_0LswsbG%2F-MYdUGP1gHDWNWRfVqpD%2Fimage.png?alt=media\&token=a1849ff9-a3b5-4130-99bd-cf5bfd96ab07)

```
gedit switch.py
```

{% hint style="info" %}
**switch.py**

```
#!/usr/bin/env python
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink,Intf
from mininet.node import Controller,RemoteController
 
if '__main__' == __name__:
  net = Mininet(link=TCLink)
  h1 = net.addHost('h1', ip="192.168.10.1")
  h2 = net.addHost('h2', ip="192.168.20.1")
  s1 = net.addSwitch('s1')
  s2 = net.addSwitch('s2')
  s3 = net.addSwitch('s3')
  s4 = net.addSwitch('s4')
  c0 = net.addController('c0', controller=RemoteController)
  net.addLink(h1, s1)
  net.addLink(s1, s2)
  net.addLink(s1, s3)
  net.addLink(s2, s4)
  net.addLink(s3, s4)
  net.addLink(s4, h2)
  net.build()
  c0.start()
  s1.start([c0])
  s2.start([c0])
  s3.start([c0])
  s4.start([c0])
  # rules for s1
  # h1 ping h2
  s1.cmd("ovs-ofctl add-flow s1 arp,arp_op=1,arp_spa=192.168.10.1,arp_tpa=192.168.20.1,actions=output:2")
  s1.cmd("ovs-ofctl add-flow s1 arp,arp_op=1,arp_spa=192.168.20.1,arp_tpa=192.168.10.1,actions=output:1")
  s1.cmd("ovs-ofctl add-flow s1 arp,arp_op=2,arp_spa=192.168.10.1,arp_tpa=192.168.20.1,actions=output:2")
  s1.cmd("ovs-ofctl add-flow s1 arp,arp_op=2,arp_spa=192.168.20.1,arp_tpa=192.168.10.1,actions=output:1")
  s1.cmd("ovs-ofctl add-flow s1 icmp,nw_src=192.168.10.1,nw_dst=192.168.20.1,icmp_type=8,icmp_code=0,actions=output:2")
  s1.cmd("ovs-ofctl add-flow s1 icmp,nw_src=192.168.20.1,nw_dst=192.168.10.1,icmp_type=0,icmp_code=0,actions=output:1")
  s1.cmd("ovs-ofctl add-flow s1 icmp,nw_src=192.168.20.1,nw_dst=192.168.10.1,icmp_type=8,icmp_code=0,actions=output:1")
  s1.cmd("ovs-ofctl add-flow s1 icmp,nw_src=192.168.10.1,nw_dst=192.168.20.1,icmp_type=0,icmp_code=0,actions=output:3")

  # rules for s2
  # h1 ping h2
  s2.cmd("ovs-ofctl add-flow s2 arp,arp_op=1,arp_spa=192.168.10.1,arp_tpa=192.168.20.1,actions=output:2")
  s2.cmd("ovs-ofctl add-flow s2 arp,arp_op=1,arp_spa=192.168.20.1,arp_tpa=192.168.10.1,actions=output:1")
  s2.cmd("ovs-ofctl add-flow s2 arp,arp_op=2,arp_spa=192.168.10.1,arp_tpa=192.168.20.1,actions=output:2")
  s2.cmd("ovs-ofctl add-flow s2 arp,arp_op=2,arp_spa=192.168.20.1,arp_tpa=192.168.10.1,actions=output:1")
  s2.cmd("ovs-ofctl add-flow s2 icmp,nw_src=192.168.10.1,nw_dst=192.168.20.1,icmp_type=8,icmp_code=0,actions=output:2")
  s2.cmd("ovs-ofctl add-flow s2 icmp,nw_src=192.168.20.1,nw_dst=192.168.10.1,icmp_type=8,icmp_code=0,actions=output:1")

  # rules for s3
  s3.cmd("ovs-ofctl add-flow s3 icmp,nw_src=192.168.10.1,nw_dst=192.168.20.1,icmp_type=0,icmp_code=0,actions=output:2")
  s3.cmd("ovs-ofctl add-flow s3 icmp,nw_src=192.168.20.1,nw_dst=192.168.10.1,icmp_type=0,icmp_code=0,actions=output:1")

  # rules for s4
  s4.cmd("ovs-ofctl add-flow s4 arp,arp_op=1,arp_spa=192.168.10.1,arp_tpa=192.168.20.1,actions=output:3")
  s4.cmd("ovs-ofctl add-flow s4 arp,arp_op=1,arp_spa=192.168.20.1,arp_tpa=192.168.10.1,actions=output:1")
  s4.cmd("ovs-ofctl add-flow s4 arp,arp_op=2,arp_spa=192.168.10.1,arp_tpa=192.168.20.1,actions=output:3")
  s4.cmd("ovs-ofctl add-flow s4 arp,arp_op=2,arp_spa=192.168.20.1,arp_tpa=192.168.10.1,actions=output:1")
  s4.cmd("ovs-ofctl add-flow s4 icmp,nw_src=192.168.10.1,nw_dst=192.168.20.1,icmp_type=8,icmp_code=0,actions=output:3")
  s4.cmd("ovs-ofctl add-flow s4 icmp,nw_src=192.168.20.1,nw_dst=192.168.10.1,icmp_type=0,icmp_code=0,actions=output:2")
  s4.cmd("ovs-ofctl add-flow s4 icmp,nw_src=192.168.20.1,nw_dst=192.168.10.1,icmp_type=8,icmp_code=0,actions=output:1")
  s4.cmd("ovs-ofctl add-flow s4 icmp,nw_src=192.168.10.1,nw_dst=192.168.20.1,icmp_type=0,icmp_code=0,actions=output:3")


  CLI(net)
  net.stop()
```

{% endhint %}
{% endtab %}
{% endtabs %}
