前面几个章节主要分析了内核中的datapath,后续章节重点分析下用户态的Vswitchd
yum install openvswitch
systemctl start openvswitch
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 --monitor
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 --monitor
systemctl status openvswitch
● openvswitch.service - Open vSwitch
Loaded: loaded (/usr/lib/systemd/system/openvswitch.service; disabled; vendor preset: disabled)
Active: active (exited) since Thu 2021-09-09 15:37:28 CST; 1min 54s ago
Process: 23379 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 23379 (code=exited, status=0/SUCCESS)
Sep 09 15:37:28 p56839v.hulk.bjpdc.qihoo.net systemd[1]: Starting Open vSwitch...
Sep 09 15:37:28 p56839v.hulk.bjpdc.qihoo.net systemd[1]: Started Open vSwitch.
vi /usr/lib/systemd/system/openvswitch.service
[Unit]
Description=Open vSwitch
After=syslog.target network.target openvswitch-nonetwork.service
Requires=openvswitch-nonetwork.service[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/bin/true
RemainAfterExit=yes[Install]
WantedBy=multi-user.targetvi /usr/lib/systemd/system/openvswitch-nonetwork.service
[Unit]
Description=Open vSwitch Internal Unit
After=syslog.target
PartOf=openvswitch.service
Wants=openvswitch.service[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=-/etc/sysconfig/openvswitch
ExecStart=/usr/share/openvswitch/scripts/ovs-ctl start \
--system-id=random $OPTIONS
ExecStop=/usr/share/openvswitch/scripts/ovs-ctl stop
[root@slb1 x86_64]# uname -r
3.10.0-693.21.1.el7.x86_64
[root@slb1 x86_64]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@slb1 x86_64]#
安装依赖包
yum install wget openssl-devel python-sphinx gcc make python-devel openssl-devel kernel-devel graphviz kernel-debug-devel autoconf automake rpm-build redhat-rpm-config libtool python-twisted-core python-zope-interface PyQt4 desktop-file-utils libcap-ng-devel groff checkpolicy selinux-policy-devel gcc-c++ python-six unbound unbound-devel -y
编译rpm包
mkdir -p ~/rpmbuild/SOURCES
mv openvswitch-2.11.0.tar.gz ~/rpmbuild/SOURCES/
cd ~/rpmbuild/SOURCES/
tar -xvf openvswitch-2.11.0.tar.gzrpmbuild -bb --nocheck openvswitch-2.11.0/rhel/openvswitch-fedora.spec
启动ovs
/root/rpmbuild/RPMS/x86_64
rpm -ivh openvswitch-2.11.0-1.el7.x86_64.rpm
systemctl start openvswitch
验证版本是否正确
[root@slb1 weiyanhua]# ovs-vsctl --version
ovs-vsctl (Open vSwitch) 2.11.0
DB Schema 7.16.1
[root@slb1 weiyanhua]#
[root@slb1 weiyanhua]# systemctl status openvswitch
鈼[0m openvswitch.service - Open vSwitch
Loaded: loaded (/usr/lib/systemd/system/openvswitch.service; disabled; vendor preset: disabled)
Active: active (exited) since Thu 2021-09-09 17:43:44 CST; 5min ago
Process: 1927269 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 1927269 (code=exited, status=0/SUCCESS)
Sep 09 17:43:44 slb1.tcloud.lycc.qihoo.net systemd[1]: Starting Open vSwitch...
Sep 09 17:43:44 slb1.tcloud.lycc.qihoo.net systemd[1]: Started Open vSwitch.
[root@slb1 weiyanhua]#
[root@slb1 weiyanhua]# vi /usr/lib/systemd/system/openvswitch.service
[Unit]
Description=Open vSwitch
Before=network.target network.service
After=network-pre.target ovsdb-server.service ovs-vswitchd.service
PartOf=network.target
Requires=ovsdb-server.service
Requires=ovs-vswitchd.service[Service]
Type=oneshot
ExecStart=/bin/true
ExecReload=/usr/share/openvswitch/scripts/ovs-systemd-reload
ExecStop=/bin/true
RemainAfterExit=yes[Install]
WantedBy=multi-user.target
再分别看一下 ovsdb-server.service和ovs-vswitchd.service
[root@slb1 weiyanhua]# vi /usr/lib/systemd/system/ovsdb-server.service
[Unit]
Description=Open vSwitch Database Unit
After=syslog.target network-pre.target
Before=network.target network.service
Wants=ovs-delete-transient-ports.service
PartOf=openvswitch.service[Service]
Type=forking
Restart=on-failure
EnvironmentFile=/etc/openvswitch/default.conf
EnvironmentFile=-/etc/sysconfig/openvswitch
ExecStartPre=/usr/bin/chown ${OVS_USER_ID} /var/run/openvswitch /var/log/openvswitch
ExecStartPre=/bin/sh -c 'rm -f /run/openvswitch/useropts; if [ "$${OVS_USER_ID/:*/}" != "root" ]; then /usr/bin/echo "OVSUSER=--ovs-user=${OVS_USER_ID}" > /run/openvswitch/useropts; fi'
EnvironmentFile=-/run/openvswitch/useropts
ExecStart=/usr/share/openvswitch/scripts/ovs-ctl \
--no-ovs-vswitchd --no-monitor --system-id=random \
${OVSUSER} \
start $OPTIONS
ExecStop=/usr/share/openvswitch/scripts/ovs-ctl --no-ovs-vswitchd stop
ExecReload=/usr/share/openvswitch/scripts/ovs-ctl --no-ovs-vswitchd \
${OVSUSER} \
--no-monitor restart $OPTIONS
RuntimeDirectory=openvswitch
RuntimeDirectoryMode=0755
[root@slb1 weiyanhua]# vi /usr/lib/systemd/system/ovs-vswitchd.service
[Unit]
Description=Open vSwitch Forwarding Unit
After=ovsdb-server.service network-pre.target systemd-udev-settle.service
Before=network.target network.service
Requires=ovsdb-server.service
ReloadPropagatedFrom=ovsdb-server.service
AssertPathIsReadWrite=/var/run/openvswitch/db.sock
PartOf=openvswitch.service[Service]
Type=forking
Restart=on-failure
Environment=XDG_RUNTIME_DIR=/var/run/openvswitch
EnvironmentFile=/etc/openvswitch/default.conf
EnvironmentFile=-/etc/sysconfig/openvswitch
EnvironmentFile=-/run/openvswitch/useropts
ExecStart=/usr/share/openvswitch/scripts/ovs-ctl \
--no-ovsdb-server --no-monitor --system-id=random \
${OVSUSER} \
start $OPTIONS
ExecStop=/usr/share/openvswitch/scripts/ovs-ctl --no-ovsdb-server stop
ExecReload=/usr/share/openvswitch/scripts/ovs-ctl --no-ovsdb-server \
--no-monitor --system-id=random \
${OVSUSER} \
restart $OPTIONS
TimeoutSec=300
rpm包安装和yum install安装的生成的service稍有不同,rpm安装是分别启动ovs-vswitchd和ovsdb-server 两个进程。
[root@slb1 bin]# ll |grep ovs
-rwxr-xr-x. 1 root root 15328 Sep 9 17:42 ovs-appctl
-rwxr-xr-x. 1 root root 61264 Sep 9 17:42 ovsdb-client
-rwxr-xr-x. 1 root root 44496 Sep 9 17:42 ovsdb-tool
-rwxr-xr-x. 1 root root 8064 Sep 9 17:41 ovs-docker
-rwxr-xr-x. 1 root root 15352 Sep 9 17:42 ovs-dpctl
-rwxr-xr-x. 1 root root 61384 Sep 9 17:41 ovs-dpctl-top
-rwxr-xr-x. 1 root root 111280 Sep 9 17:42 ovs-ofctl
-rwxr-xr-x. 1 root root 15121 Sep 9 17:41 ovs-pki
-rwxr-xr-x. 1 root root 23720 Sep 9 17:42 ovs-testcontroller
-rwxr-xr-x. 1 root root 78216 Sep 9 17:42 ovs-vsctl
modinfo openvswitch
filename: /lib/modules/3.10.0-693.21.1.el7.x86_64/kernel/net/openvswitch/openvswitch.ko.xz
license: GPL
description: Open vSwitch switching datapath
retpoline: Y
rhelversion: 7.4
srcversion: 6F25EA2AC487B827093E53B
depends: nf_conntrack,nf_nat,libcrc32c,nf_nat_ipv6,nf_nat_ipv4,nf_defrag_ipv6
intree: Y
vermagic: 3.10.0-693.21.1.el7.x86_64 SMP mod_unload modversions
signer: CentOS Linux kernel signing key
sig_key: 03:DA:60:92:F6:71:13:21:B5:AC:E1:2E:84:5D:A9:73:36:F7:67:4D
sig_hashalgo: sha256
查看内核datapath的版本
[root@slb2 weiyanhua]# ovs-dpctl --version
ovs-dpctl (Open vSwitch) 2.11.0
[root@slb2 weiyanhua]#
在通过rpm包安装时会自动替换原有的内核openvswitch.ko
root@slb2 weiyanhua]# lsmod |grep openvswitch
openvswitch 176997 0
nf_nat_ipv6 14131 1 openvswitch
nf_nat_ipv4 14115 1 openvswitch
nf_defrag_ipv6 35104 2 openvswitch,nf_conntrack_ipv6
nf_nat 26787 3 openvswitch,nf_nat_ipv4,nf_nat_ipv6
nf_conntrack 133387 6 openvswitch,nf_nat,nf_nat_ipv4,nf_nat_ipv6,nf_conntrack_ipv4,nf_conntrack_ipv6
udp_tunnel 14137 1 openvswitch
libcrc32c 12644 4 xfs,openvswitch,nf_nat,nf_conntrack
[root@slb2 weiyanhua]#
源码编译安装和启动不再进行分析。