# Setup Your Environment
If you encounter any difficult setup issues, please contact Patty at l224749391@gmail.com.
## Update July 15th
{%youtube waOHTaYj95Q %}
Hope you have an Ubuntu Desktop with a version higher than Ubuntu **20.04**, as well as the necessary packages and tools (such as **build-essential**, your preferred Python management tool, **Wireshark**, **iperf3**, **libssh-dev**, **ninja**, **networkx**, **xterm**, etc.).
:::warning
If I missed anything required for the installation, please let me know in the comments section.
:::
## Download Mininet
```shell=
sudo apt update
sudo apt install -y mininet
```
check installing successfully
```shell=
sudo mn --test pingall
```

## Install Ryu OpenFlow Controller
You need to set up an environment with Python 3.8. Please avoid using a newer version, as it may cause some issues.
You can use [pyenv](https://github.com/pyenv/pyenv) or [Anaconda](https://www.anaconda.com/download)
```shell=
conda create -n ryu-env python=3.8 -y
conda activate ryu-env
```
:::warning
You may need to downgrade certain components before installing the Ryu controller.
If you encounter errors, follow the error messages and downgrade the necessary packages accordingly.
:::
```shell=
pip install "setuptools<59"
pip install "eventlet==0.30.2"
pip install ryu
ryu-manager --version
```

:::danger
You may need to disable the SSLv3 minimum version check function to avoid recursive inclusion. You can refer to the error message or check the file at /${your_python_path}/ssl.py for more details (using `pass` to ignore).
:::
## Test Ryu and Mininet
Open one terminal (openflow 1.3 switch)
```shell=
ryu-manager ryu.app.simple_switch_13
```
Open another terminal (create mininet for single switch and 3 hosts)
```bash=
sudo mn --controller=remote,ip=127.0.0.1 --switch ovs,protocols=OpenFlow13 --topo single,3
```
In mininet CLI:
```bash=
pingall
```

```bash=
exit
```
---
Then move on to ovs, sflow collector (optional) and network digital twin system.
## Open vSwitch
No need to download, it is with mininet.
```bash=
ovs-vsctl --version
```
## SFlow Collector
Any tool (you can search online) is fine, as this is just for testing. We have an sFlow collector in NDT, but we still recommend downloading another sFlow collector to check sFlow when necessary.
We recommend [sflowtool.](https://github.com/sflow/sflowtool)
## Test Mininet's SFlow
We demonstrate using sflowtool, but you can use any tool you prefer, or even just observe in Wireshark.
Open one terminal
```bash
sflowtool -p 6343 -L localtime,srcIP,dstIP
```
Open your mininet on another terminal
```bash=
sudo mn --topo single,2 --controller=remote,ip=127.0.0.1 --switch ovs,protocols=OpenFlow13
```
Don't forget to run openflow simple switch controller (on another terminal)
```bash=
conda activate ryu-env
ryu-manager ryu.app.simple_switch_13
```
Set up sflow on mininet's openflow switch
```bash=
sh ovs-vsctl -- --id=@sflow create sflow agent=s1-eth1 target=\"127.0.0.1:6343\" sampling=64 polling=10 -- set Bridge s1 sflow=@sflow
```
Check sflow status
```bash=
sh ovs-vsctl list sflow
```

Generate traffic using iperf3. You can also use built-in iperf, ping, or other tools. However, we use iperf3 for NDT experiments, so you may still need to install it on your Ubuntu desktop in the future.
In your mininet CLI
Start iperf3 server (once) and then client.
```bash=
h1 iperf3 -s &
h2 iperf3 -c h1
```
Observe
sflowtool has received sFlow samples.

Alternatively, you can view the reports in Wireshark using the loopback interface.

---
Let's move on to executing our programs. To compile NDT, we need CMake (built-in) and Ninja for smarter and faster compilation.
```bash=
sudo apt install ninja-build
```
## Compile
Go to the NDT direcotory and compling
(Remove "build" direcotory if it exists.)
```bash=
rm -rf build # if it exists
mkdir build && cd build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug ..
```
You can refer to CMakeLists.txt for more information. Note that you shouldn't set the build type to "Release" because we have not yet refactored our program to pass the `-O3` optimization flag.
```bash=
ninja clean && ninja
```
:::info
If this modification is small-scale (such as adding a logger), you can use `ninja` without running `ninja clean` to save your time.
:::
## Execute
:::danger
Note that this is just an example.
If you need to execute the whole process, please make sure your topology (Mininet) and SDN controller (Ryu) are running before executing NDT, as NDT retrieves the topology from the Ryu controller at startup.
:::
```bash=
bin/ndt_main # --loglevel debug (If you need some certain logging degree, please refer to Logger.hpp)
```
## How to test NDT (Without Running External Third-party Apps)
### 1. Start Ryu Controller
```bash=
ryu-manager intelligent_router.py ryu.app.rest_topology ryu.app.ofctl_rest --ofp-tcp-listen-port 6633 --observe-link
# intelligent_router.py is our own controller app.
```
### 2. Setup Topology

We recommend using a script to quickly build the topology.
{%gist pyjuan91/712fd980bee363dc9ed72001bb2b7ab7 %}
### How to control multiple hosts in mininet?
```bash=
sudo apt install xterm
```
In you mininet CLI
```bash=
xterm h1 h2 h3 #h4 h5 ...
```
### 3. After connecting to all switches, run NDT with root permission.
```bash=
# in NetworkDigitalTwin-main/build
sudo bin/ndt_main # --loglevel trace
```
## Mininet Demo Exp 1 & 2
{%youtube 1AH65Y8wPkc %}