# OpenvSwitch 建置
* OpenvSwitch 可以支持多種網路連接方式,包括實體網絡、虛擬網絡和隧道等。這樣可以為虛擬機提供靈活的網路連接。
## proxmox ovs 設定
## 架構圖

* 透過 ovs 可以讓內網的 vm 直接使用外網的 default gateway 上網。
* 每一台 proxmox 只有一張 ens32 實體網卡並透過他上網,Notwork ID 為 192.168.61.0/24
* vm 使用的 Notwork ID 為 192.168.100.0/24,透過 ovs 功能可以直接設定 192.168.61.2 為 gateway,並且透過他上網。
## 安裝 ovs
```
$ apt update
$ apt install openvswitch-switch
```
## ovs 網路設定
* 使用 Open vSwitch 建立一個虛擬橋接器 vmbr0,用於連接實體網卡 ens32 和虛擬 vlan1 interface。
* vm 使用的 Network ID 為 192.168.100.0/24。
* OVSIntPort 是在 Open vSwitch 中使用的一種虛擬接口類型。
* vmbr0 這個虛擬橋接器就是由 open vswitch 做的,他的 type 是 OVSBridge。
* vlan1 虛擬網卡目的是為了讓 proxmox 主機可以跟 192.168.100.xx/24 網段的 vm 溝通。
```
$ nano /etc/network/interfaces
auto lo
iface lo inet loopback
auto ens32
iface ens32 inet manual
auto vmbr0
iface vmbr0 inet manual
address 192.168.61.3/24
gateway 192.168.61.2
ovs_type OVSBridge # vmbr0 是透過 ovs 做出的 Bridge
ovs_ports ens32
auto vlan1
iface vlan1 inet static
ovs_type OVSIntPort
ovs_bridge vmbr0 # 將 vlan1 連到 ovs 做的 vmbr0
address 192.168.100.3/24
post-up ovs-vsctl add-port vmbr0 ens32 # 將實體 ens32 網卡插入到 vmbr0 橋接網路
source /etc/network/interfaces.d/*
```
```
$ reboot
```
### 驗證
```
$ wget https://cloud-images.ubuntu.com/jammy/20231207/jammy-server-cloudimg-amd64.img
$ qm create 9000 --name us2204 --memory 1024 --net0 virtio,bridge=vmbr0
$ qm importdisk 9000 jammy-server-cloudimg-amd64.img CPA
$ qm set 9000 --scsihw virtio-scsi-pci --scsi0 CPA:vm-9000-disk-0
$ qm set 9000 --ide2 local-lvm:cloudinit
$ qm resize 9000 scsi0 100G
$ qm set 9000 --boot c --bootdisk scsi0
$ qm template 9000
```
* 在不同 proxmox 主機上建立兩台 vm,確認是否可以互相溝通與上網。
```
bigred@ub1:~$ ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether bc:24:11:f5:16:b7 brd ff:ff:ff:ff:ff:ff
altname enp0s18
inet 192.168.100.11/24 brd 192.168.100.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::be24:11ff:fef5:16b7/64 scope link
valid_lft forever preferred_lft forever
bigred@ub1:~$ ip r s
default via 192.168.61.2 dev eth0 proto static onlink
192.168.100.0/24 dev eth0 proto kernel scope link src 192.168.100.11
bigred@ub1:~$ ping www.google.com
PING www.google.com (142.251.43.4) 56(84) bytes of data.
64 bytes from tsa03s08-in-f4.1e100.net (142.251.43.4): icmp_seq=1 ttl=128 time=1.92 ms
64 bytes from tsa03s08-in-f4.1e100.net (142.251.43.4): icmp_seq=2 ttl=128 time=2.26 ms
bigred@ub1:~$ ping 192.168.100.12
PING 192.168.100.12 (192.168.100.12) 56(84) bytes of data.
64 bytes from 192.168.100.12: icmp_seq=1 ttl=64 time=1.43 ms
64 bytes from 192.168.100.12: icmp_seq=2 ttl=64 time=0.731 ms
^C
```
```
bigred@ub2:~$ ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether bc:24:11:2f:8f:95 brd ff:ff:ff:ff:ff:ff
altname enp0s18
inet 192.168.100.12/24 brd 192.168.100.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::be24:11ff:fe2f:8f95/64 scope link
valid_lft forever preferred_lft forever
bigred@ub2:~$ ip r s
default via 192.168.61.2 dev eth0 proto static onlink
192.168.100.0/24 dev eth0 proto kernel scope link src 192.168.100.12
bigred@ub2:~$ ping www.google.com
PING www.google.com (142.251.43.4) 56(84) bytes of data.
64 bytes from tsa03s08-in-f4.1e100.net (142.251.43.4): icmp_seq=1 ttl=128 time=2.14 ms
64 bytes from tsa03s08-in-f4.1e100.net (142.251.43.4): icmp_seq=2 ttl=128 time=2.18 ms
^C
bigred@ub2:~$ ping 192.168.100.11
PING 192.168.100.11 (192.168.100.11) 56(84) bytes of data.
64 bytes from 192.168.100.11: icmp_seq=1 ttl=64 time=0.786 ms
64 bytes from 192.168.100.11: icmp_seq=2 ttl=64 time=0.583 ms
^C
```
* 在 proxmox 上查看 ovs 做出的網路橋接器
```
$ ovs-vsctl show
586667f4-b5e1-4fbc-a7b7-61af8308cadc
Bridge vmbr0
Port ens32
Interface ens32
Port vlan1
Interface vlan1
type: internal
Port tap101i0
Interface tap101i0
Port vmbr0
Interface vmbr0
type: internal
ovs_version: "3.1.0"
```
## 效能測試
* 安裝效能測試套件
```
bigred@ub2:~$ sudo apt install iperf3
bigred@ub1:~$ sudo apt install iperf3
```
```
bigred@ub1:~$ iperf3 -s
```
```
bigred@ub2:~$ iperf3 -c 192.168.100.11
Connecting to host 192.168.100.11, port 5201
[ 5] local 192.168.100.12 port 40978 connected to 192.168.100.11 port 5201
[ ID] Interval Transfer Bitrate Retr Cwnd
[ 5] 0.00-1.00 sec 513 MBytes 4.30 Gbits/sec 108 1.38 MBytes
[ 5] 1.00-2.00 sec 531 MBytes 4.46 Gbits/sec 0 1.63 MBytes
[ 5] 2.00-3.00 sec 485 MBytes 4.07 Gbits/sec 0 1.84 MBytes
[ 5] 3.00-4.00 sec 489 MBytes 4.10 Gbits/sec 8 1.46 MBytes
[ 5] 4.00-5.00 sec 489 MBytes 4.10 Gbits/sec 0 1.69 MBytes
[ 5] 5.00-6.00 sec 495 MBytes 4.15 Gbits/sec 0 1.89 MBytes
[ 5] 6.00-7.00 sec 472 MBytes 3.96 Gbits/sec 6 1.51 MBytes
[ 5] 7.00-8.00 sec 489 MBytes 4.10 Gbits/sec 0 1.73 MBytes
[ 5] 8.00-9.00 sec 462 MBytes 3.88 Gbits/sec 0 1.91 MBytes
[ 5] 9.00-10.00 sec 490 MBytes 4.11 Gbits/sec 10 1.52 MBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bitrate Retr
[ 5] 0.00-10.00 sec 4.80 GBytes 4.12 Gbits/sec 132 sender
[ 5] 0.00-10.04 sec 4.80 GBytes 4.10 Gbits/sec receiver
iperf Done.
```
> 總測試時間: 10 秒
> 總傳輸數據量: 4.80 GB
> 平均吞吐量: 4.12 Gbits/sec
## ovs 運作流程
* ovsdb-server 是一個輕量級的數據庫伺服器,用於存儲 OVSDB 數據庫。OVSDB 數據庫包含 OVS 配置訊息,包括橋接、埠、流表等。
* ovs-vswitchd 負責實現網路轉發,以將流量轉發到目的地。
```
$ ps aux|grep ovs
root 716 0.0 0.0 15428 6788 ? S<s 16:48 0:00 ovsdb-server /etc/openvswitch/conf.db -vconsole:emer -vsyslog:err -vfile:info --remote=punix:/var/run/openvswitch/db.sock --private-key=db:Open_vSwitch,SSL,private_key --certificate=db:Open_vSwitch,SSL,certificate --bootstrap-ca-cert=db:Open_vSwitch,SSL,ca_cert --no-chdir --log-file=/var/log/openvswitch/ovsdb-server.log --pidfile=/var/run/openvswitch/ovsdb-server.pid --detach
root 770 0.1 1.2 753464 99100 ? S<Lsl 16:48 0:00 ovs-vswitchd unix:/var/run/openvswitch/db.sock -vconsole:emer -vsyslog:err -vfile:info --mlockall --no-chdir --log-file=/var/log/openvswitch/ovs-vswitchd.log --pidfile=/var/run/openvswitch/ovs-vswitchd.pid --detach
root 4359 0.0 0.0 6332 2048 pts/0 S+ 16:56 0:00 grep ovs
```
#### 連結
https://docs.openvswitch.org/en/latest/faq/issues/
https://pve.proxmox.com/wiki/Open_vSwitch
https://ctimbai.github.io/2017/12/23/tech/net/ovs/OVS_%E6%80%BB%E4%BD%93%E6%9E%B6%E6%9E%84%E3%80%81%E6%BA%90%E7%A0%81%E7%BB%93%E6%9E%84%E5%8F%8A%E6%95%B0%E6%8D%AE%E6%B5%81%E7%A8%8B%E5%85%A8%E9%9D%A2%E8%A7%A3%E6%9E%90/