> For the complete documentation index, see [llms.txt](https://anida-huang.gitbook.io/notes-network-simulation-and-analysis/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://anida-huang.gitbook.io/notes-network-simulation-and-analysis/qi-zhong/20210308-mininet-jian-jie-yu-ji-ben-cao-zuo-er.md).

# 20210315  Gnuplot

## 課堂資料

{% file src="/files/-MVoRCv1OHAuQvNwGuOJ" %}

{% embed url="<https://www.sdnlab.com/11495.html>" %}

{% embed url="<https://github.com/intrig-unicamp/mininet-wifi>" %}

{% embed url="<https://matplotlib.org/1.4.2/faq/installing_faq.html>" %}

{% embed url="<https://github.com/gmiotto/dockernet>" %}

{% embed url="<http://csie.nqu.edu.tw/smallko/mininet-wifidockerp4.zip>" %}

## 課堂練習

### 建立網路拓樸

![](/files/-Mcj5iKKfvhP-nAecvkz)

```
cd mininet
```

```
mn --link=tc,bw=10,delay=1ms,loss=5
```

![](/files/-MWwlcWEnAJKqajGBgMh)

```
mn --link=tc,bw=10,delay='1ms',loss=0
```

```
h1 ping h2
```

```
xterm h1 h2
```

![](/files/-MWwmaOh1ldNbJH-TFZU)

{% tabs %}
{% tab title="h2" %}

```
iperf -s -i 1
```

![](/files/-MWwewvyzSo60yuEIUcX)
{% endtab %}

{% tab title="h1" %}

```
iperf -c 10.0.0.2 -t 100
```

![](/files/-MWwg5F1fT5MZH9BBktQ)
{% endtab %}
{% endtabs %}

```
h1 ping -c 1000 -i 0.01 h2
```

![](/files/-MWwn3ZBV-EFwjQVwQbz)

```
exit
```

```
mn --link=tc,bw=10,delay='10ms',loss=2
```

```
h1 ping -c 1000 -i 0.01 h2
```

![](/files/-MWwpTxJ2etNSAyfP13A)

```
xterm h1 h2 h1 h2
```

{% tabs %}
{% tab title="h2 - 1" %}

```
iperf -s -i 1 -p 5555 > tcp
```

![](/files/-MWwtA-QK2mynPUYDIXz)
{% endtab %}

{% tab title="h1 - 1" %}

```
iperf -c 10.0.0.2 -p 5555 -t 100
```

![](/files/-MWwt4x0RGYq9Zbho1an)
{% endtab %}

{% tab title="h2 - 2" %}

```
iperf -s -i 1 -p 6666 -u | tee udp
```

![](/files/-MWwsUHPnShCkiVgQfNs)
{% endtab %}

{% tab title="h1 - 2" %}

```
iperf -c 10.0.0.2 -p 6666 -u -b 3M -t 100
```

![](/files/-MWwt-4s3HXlfW9-VYQV)
{% endtab %}
{% endtabs %}

```
exit
```

```
mn --link=tc,bw=100,delay='10ms',loss=2
```

```
xterm h1 h2
```

![](/files/-MWwtwmRVFSIHDJWYA82)

{% tabs %}
{% tab title="h2" %}

```
iperf -c -i 1 -p 5555 | tee tee
```

![](/files/-MWwwAw0TK44UrzEQxrJ)
{% endtab %}

{% tab title="h1" %}

```
iperf -c 10.0.0.2 -t 100 -p 5555
```

![](/files/-MWwwfWRQRi2oZ1Jzr16)
{% endtab %}
{% endtabs %}

```
exit
```

```
mn --link=tc,bw=10,delay='10ms',loss=0
```

```
xterm h1 h2 h1 h2
```

![](/files/-MXsFINyiW8VJOUgTG1T)

{% tabs %}
{% tab title="h2 - 1" %}

```
iperf -c -i 1 -p 5555 | tee tcp
```

![](/files/-MXsGPz7wFCU1BrmPgw2)
{% endtab %}

{% tab title="h1 - 1" %}

```
iperf -c 10.0.0.2 -t 100 -p 5555
```

![](/files/-MXsGCNMOgWLm5cS5dH9)
{% endtab %}

{% tab title="h2 - 2" %}

```
iperf -c -i 1 -p 6666 -u | tee udp
```

![](/files/-MXsFvETdLe6UFrhRKKZ)
{% endtab %}

{% tab title="h1 - 2" %}

```
iperf -c 10.0.0.2 -p 6666 -u -b 3M -t 100
```

![](/files/-MXsGIG2Ftc925r9b7Yc)
{% endtab %}
{% endtabs %}

```
exit
```

```
cat tcp | grep "sec" | head -n 100 | tr "-" " " | awk '{print $4,$8}' > mytcp
```

```
cat udp | grep "sec" | head -n 100 | tr "-" " " | awk '{print $4,$8}' > myudp
```

### Gnuplot

```
gnuplot
```

```
plot "mytcp" title "tcp_flow" with linespoints, "myudp" title "udp_flow" with linespoints
```

```
set xrange [0:100]
```

```
set xtics 0,10,100
```

```
set yrange [0:10]
```

```
set ytics 0,1,10
```

```
replot
```

```
set xlabel "time(sec)" 
```

```
set ylabel "throughput(Mbps)" 
```

```
replot
```

```
set title "tcp vs. udp"
```

```
replot
```

```
set terminal "gif"
```

```
set output "a.gif"
```

![](/files/-MXsI5f-S0OCS9efFqlK)

```
report
```

![](/files/-MXsIV-H5XA8KI0Dvg_O)

### 使用腳本建立 Mininet

![](/files/-MXsaXSQ3oKOrnMCGtpS)

> #### 建立腳本

```
gedit 1.py
```

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

```
#!/usr/bin/env python
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink
 
if '__main__' == __name__:
  net = Mininet(link=TCLink)
  h1 = net.addHost('h1')
  h2 = net.addHost('h2')
  Link(h1, h2)
  net.build()
  CLI(net)
  net.stop()
```

{% endhint %}

![](/files/-MXsJMFQjyIpBQxf16VS)

```
python 1.py
```

```
net
```

```
h1 ifconfig
```

```
h2 ifconfig
```

![](/files/-MXsXF4xsnHKBNL-lZVB)

```
h1 ping -c 3 h2
```

```
exit
```

```
cp 1.py 2.py
```

```
gedit 2.py
```

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

```
#!/usr/bin/env python
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink
 
if '__main__' == __name__:
  net = Mininet(link=TCLink)
  h1 = net.addHost('h1')
  h2 = net.addHost('h2')
  r = net.addHost('r')
  Link(h1, r)
  Link(h2, r)
  net.build()
  CLI(net)
  net.stop()
```

{% endhint %}

![](/files/-MXsk29uTSYy1VGMuynf)

```
python 2.py
```

```
xterm h1 h2 r
```

![](/files/-MXsZ67Mr__iP7qJ-2qq)

{% tabs %}
{% tab title="h1" %}

```
ifconfig
```

```
ifconfig h1-eth0 0
```

```
ip addr add 192.168.1.1/24 brd + dev h1-eth0
```

```
ip ro sh
```

```
route -n
```

```
ip ro add default via 192.168.1.254
```

```
ip ro sh
```

```
route -n
```

![](/files/-MXslOwo58DvZfZpHx-n)

```
ping 192.168.1.254
```

![](/files/-MXsnun77nwMFlFxmWNz)

```
ping 192.168.1.1
```

![](/files/-MXsomMyH2986UGXjNhv)

```
ping 192.168.2.254
```

![](/files/-MXspOnsrrZsx5Wdd071)

```
arp -n
```

```
ip ro sh
```

![](/files/-MXsreLU3uAFxs0SAsmk)
{% endtab %}

{% tab title="h2" %}

```
ifconfig
```

```
ifconfig h2-eth0 0
```

```
ip addr add 192.168.2.1/24 brd + dev h2-eth0
```

```
ip ro sh
```

```
route -n
```

```
ip ro add default via 192.168.2.254
```

```
ip ro sh
```

```
route -n
```

![](/files/-MXsmDXHp_r8-cdJxMBz)

```
ping 192.168.2.1
```

```
ping 192.168.1.254
```

```
ping 192.168.1.1
```

![](/files/-MXsrss5d0i6uh7x-J7d)

```
ping 192.168.1.1
```

![](/files/-MXsscRUILQrkb4nYZh3)
{% endtab %}

{% tab title="r" %}

```
ifconfig
```

![](/files/-MXsmYznAgFV4BU1saMj)

```
ifconfig r-eth0 0
```

```
ifconfig r-eth1 0
```

```
ip addr add 192.168.1.254 brd + dev r-eth0
```

```
ip addr add 192.168.2.254 brd + dev r-eth1
```

```
ifconfig
```

![](/files/-MXsnY33u1tDs9JBwLEY)

```
ip add sh
```

![](/files/-MXso7mwpSLXfGeynkOY)

```
ifconfig r-eth0 0
```

```
ifconfig r-eth1 0
```

```
ip addr add 192.168.1.254/24 brd + dev r-eth0
```

```
ip addr add 192.168.2.254/24 brd + dev r-eth1
```

```
ifconfig
```

![](/files/-MXspCxamav11C-nsOdK)

```
cat /proc/sys/net/ipv4/ip_forward
```

```
echo 1 > /proc/sys/net/ipv4/ip_forward
```

```
cat /proc/sys/net/ipv4/ip_forward
```

![](/files/-MXssSsydb7XLn6TTw8p)
{% endtab %}
{% endtabs %}

```
net
```

![](/files/-MXsuFlB5gV2KxivPq7_)

```
gedit 3.py
```

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

```
#!/usr/bin/env python
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink
 
if '__main__' == __name__:
  net = Mininet(link=TCLink)
  h1 = net.addHost('h1')
  h2 = net.addHost('h2')
  r1 = net.addHost('r1')
  r2 = net.addHost('r2')
  Link(h1, r1)
  Link(h2, r2)
  Link(r1, r2)
  net.build()
  h1.cmd("ifconfig h1-eth0 0")
  h1.cmd("ip addr add 192.168.1.1/24 brd + dev h1-eth0")
  h1.cmd("ip route add default via 192.168.1.254")
  h2.cmd("ifconfig h2-eth0 0")
  h2.cmd("ip addr add 192.168.2.1/24 brd + dev h2-eth0")
  h2.cmd("ip route add default via 192.168.2.254")
  r1.cmd("ifconfig r1-eth0 0")
  r1.cmd("ifconfig r1-eth1 0")
  r1.cmd("ip addr add 192.168.1.254/24 brd + dev r1-eth0")
  r1.cmd("ip addr add 10.0.0.1/24 brd + dev r1-eth1")
  r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  r1.cmd("ip route add 192.168.2.0/24 via 10.0.0.2")
  r2.cmd("ifconfig r2-eth0 0")
  r2.cmd("ifconfig r2-eth1 0")
  r2.cmd("ip addr add 192.168.2.254/24 brd + dev r2-eth0")
  r2.cmd("ip addr add 10.0.0.2/24 brd + dev r2-eth1")
  r2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  r2.cmd("ip route add 192.168.1.0/24 via 10.0.0.1")
  CLI(net)
  net.stop()
```

{% endhint %}

![](/files/-MYE4Qzewaae3nNnrCZO)

```
python 3.py
```

```
net
```

![](/files/-MYE4rf_zr2lYEyy0VWr)

### 練習：安裝 P4

#### 生成 SSH ( GitHub 新增 )

```
apt-get install ssh
```

```
ls -al ~/.ssh
```

![](/files/-MWNz_ldGIKnzJ9iPpg8)

```
ssh-keygen -t rsa -C "xiaoji850312@gmail.com"
```

```
ssh-add ~/.ssh/id_rsa
```

```
cat /root/.ssh/id_rsa.pub
```

![](/files/-MWNzKSflgsQwQR0TwzK)

```
apt install python3.8
```

![](/files/-MWNptFvLQmMz5TQSc33)

```
apt install python3.8-distutils
```

![](/files/-MWNqcIAXVQ2LiMPohOs)

```
wget https://bootstrap.pypa.io/get-pip.py
```

```
python3.8 get-pip.py.1
```

![](/files/-MWNrIYYahcuk-ZhT31o)

```
git clone git@github.com:p4lang/behavioral-model.git
```

![](/files/-MWNzre8FdUniN3QmES2)

```
cd behavioral-model
```

```
./install_deps.sh
```

![](/files/-MWO-49fcuvaNxDWVcLI)

```
./autogen.sh
```

![](/files/-MWO0ybAG0XtEa0dzyPB)

```
./configure
```

![](/files/-MWO19nVzmMZgmbVctkr)

```
make
```

![](/files/-MWO1Lag9Q-KABakgPRy)

```
make install
```

![](/files/-MWO5x9VL5H37yHaVpUE)

### 練習：安裝 Mininet-WiFi ( 失敗 )

```
cd ..
```

```
apt-get install git
```

![](/files/-MXswPEXRpis819_a8_Q)

```
git clone https://github.com/intrig-unicamp/mininet-wifi
```

![](/files/-MXswab35u1NjsjbNzOq)

```
cd mininet-wifi
```

```
util/install.sh -Wlnfv
```

![](/files/-MXswvGBWtOUCqPk6mdJ)

```
mn --wifi
```
