Openvswitch 編譯及升級


https://chengqian90.com/Openvswitch/Openvswitch-%E7%BC%96%E8%AF%91%E5%8F%8A%E5%8D%87%E7%BA%A7.html

 

編譯安裝

參照官網,也查了些資料,編譯期間出現些問題,先將正確的流程及問題解決方法記錄如下。

  • Kernel 版本為 3.10.0-693.11.1.el7.x86_64 。內核與ovs關系可參考 release
  • 有序列表(1、2),順序執行;無序列表(·),多選一。

正式開始。

  1. 獲取代碼(2.9為例,其他版本可參考官網)

    • wget 下載

      1
      2
      3
      # wget http://openvswitch.org/releases/openvswitch-2.9.0.tar.gz
      # tar zxvf openvswitch-2.9.0.tar.gz
      # cd openvswitch-2.9.0
    • Git 下載

      1
      2
      3
      4
      # git clone https://github.com/openvswitch/ovs.git
      # cd ovs
      // git tag 或 git branch -a 查看所有分支,切換為穩定分支
      # git checkout origin/branch-2.9
  2. 編譯環境配置

    1. GUN make
    2. C 編譯器
      • GCC 4.6 or later.
      • Clang 3.4 or later.
      • MSVC 2013. (windows平台)
    3. libssl,openssl相關,連接的安全機密性,可選。
    4. libcap-ng,可選。
    5. Python 2.7. 用到 six version 1.4.0或之后的庫。

    除此之外,如果需要Linux kernel支持的話,還需要打開以下開關(無需內核支持可參考Open vSwitch without Kernel Support )。

    1. 受支持的內核版本。

      Ingress policing的支持,需要以嵌入或模塊形式打開 NET_CLS_BASIC、 NET_SCH_INGRESS和 NET_ACT_POLICE

      3.11之前的內核,不得加載ip_gre模塊(NET_IPGRE)。

      開啟CONFIG_TUN以使能ovs對TAP設備的支持。

    2. 與內核相對應的GCC版本。

    3. 一個內核構建目錄對應於模塊要運行的linux內核鏡像(linux-header相關文件)。

    如果使用git樹或者修改了打開的vswitch構建系統或數據庫架構,則還需要以下軟件:

    1. Autoconf version 2.63 or later.
    2. Automake version 1.10 or later.
    3. libtool version 2.4 or later. (Older versions might work too.)

    還有一些 datapath test 相關包,此處不在贅述。對於一個系統,基本下面的命令搞定

    1
    # yum install -y autoconf automake libtool kernel-devel-$(uname -r)

    為了支持TAP 和 隨機,需要/dev/urandom/dev/net/tun這兩個文件存在。

  3. 構建”configure”腳本

    1
    # ./boot.sh
  4. 配置

    默認情況下,所有的文件都安裝在/usr/local目錄下,即下面的命令

    1
    # ./configure

    此時,openvswitch會在/usr/local/etc/openvswitch下查找數據庫。

    但是如果想按照傳統安裝方式(/usr/local -> /usr, /usr/local/var -> /var, /etc/openvswitch作為默認數據庫目錄),則

    1
    # ./configure --prefix=/usr --localstatedir=/var --sysconfdir=/etc

    構建Linux內核模塊,以便您可以運行基於內核的交換機,在–with-linux上傳遞內核構建目錄的位置。

    1
    # ./configure --with-linux=/lib/modules/$(uname -r)/build

    以上總結,直接運行

    1
    # ./configure --prefix=/usr --localstatedir=/var --sysconfdir=/etc --with-linux=/lib/modules/$(uname -r)/build

    除此之前,還可以編譯不同架構的ovs,例如

    1
    # ./configure --with-linux=/path/to/linux KARCH=mips

    還有其他的選項,可參考官網。

  5. Building

    1. make install 默認將程序及幫助文件裝入/usr/local

      1
      # make ; make install
    2. 安裝內核模塊

      1
      # make modules_install

      如果運行時遇到類似如下錯誤

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      # make modules_install
      cd datapath/linux && make modules_install
      make[1]: Entering directory `/root/openvswitch-2.9.0/datapath/linux'
      make -C /lib/modules/3.10.0-693.11.1.el7.x86_64/build M=/root/openvswitch-2.9.0/datapath/linux modules_install
      make[2]: Entering directory `/usr/src/kernels/3.10.0-693.11.1.el7.x86_64'
      INSTALL /root/openvswitch-2.9.0/datapath/linux/openvswitch.ko
      Can't read private key
      ………………
      INSTALL /root/openvswitch-2.9.0/datapath/linux/vport-vxlan.ko
      Can't read private key
      DEPMOD 3.10.0-693.11.1.el7.x86_64
      make[2]: Leaving directory `/usr/src/kernels/3.10.0-693.11.1.el7.x86_64'
      depmod `sed -n 's/#define UTS_RELEASE "\([^"]*\)"/\1/p' /lib/modules/3.10.0-693.11.1.el7.x86_64/build/include/generated/utsrelease.h`
      make[1]: Leaving directory `/root/openvswitch-2.9.0/datapath/linux'

      恭喜你,和我一樣,中招了。

      此問題是安裝內核模塊是遇錯,那么手動安裝即可,並保持重啟已經安裝,所以需要以下操作

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      // 查看這幾個模塊的依賴關系是否建立,一般在最后幾行
      # vim /lib/modules/$(uname -r)/modules.dep
      // 查看相對目錄的ko文件是否是剛編譯過的
      # cd /lib/modules/$(uname -r)/extra
      # ls -al
      # date
      // 如果不是最新,從源碼目錄`datapath/linux`中把幾個ko文件重新移入過去。
      // 之后改寫文件,讓其可以開機加載就可以了(不想重啟就手動加載)
      # cat /etc/sysconfig/modules/ovs.modules
      #!/bin/sh

      modprobe gre
      modprobe libcrc32c
      modprobe openvswitch

      modprobe vport-geneve
      modprobe vport-gre
      modprobe vport-lisp
      modprobe vport-stt
      modprobe vport-vxlan
      # chmod +x /etc/sysconfig/modules/ovs.modules (文件加入可執行權限)
      # reboot
  6. 開始

    • 全自動執行

      1
      2
      3
      4
      5
      // 加入環境變量,或者直接絕對路徑執行
      // 會啟動 ovs-vswitchd 和 ovsdb-server
      //不在此目錄,則'find /usr/ -name 'ovs-ctl''找一下
      # export PATH=$PATH:/usr/local/share/openvswitch/scripts
      # ovs-ctl start
    • 半自動(指定啟動項)

      1
      2
      3
      4
      //只啟動 ovsdb-server
      # ovs-ctl --no-ovs-vswitchd start
      //只啟動 ovs-vswitchd
      # ovs-ctl --no-ovsdb-server start
    • 手動執行

      1
      2
      3
      4
      //不用 ovs-ctl工具
      //創建數據庫目錄(根據configure),此處為默認
      # mkdir -p /usr/local/etc/openvswitch
      # ovsdb-tool create /usr/local/etc/openvswitch/conf.db vswitchd/vswitch.ovsschema

      配置ovsdb-serve以使用數據庫

      1
      2
      3
      4
      5
      6
      7
      # mkdir -p /usr/local/var/run/openvswitch
      # ovsdb-server --remote=punix:/usr/local/var/run/openvswitch/db.sock \
      --remote=db:Open_vSwitch,Open_vSwitch,manager_options \
      --private-key=db:Open_vSwitch,SSL,private_key \
      --certificate=db:Open_vSwitch,SSL,certificate \
      --bootstrap-ca-cert=db:Open_vSwitch,SSL,ca_cert \
      --pidfile --detach --log-file

      Note:如果openvswitch 不支持ssl,則忽``–private-key,–certificate,–bootstrap-ca-cert`。

      初始化數據庫

      1
      # ovs-vsctl --no-wait init

      開啟openvswitch主要進程

      1
      # ovs-vswitchd --pidfile --detach --log-file
  7. 驗證

    1
    2
    # ovs-vsctl add-br br0
    # ovs-vsctl add-port br0 eth0

升級

  1. 終止進程

    本例為默認目錄

    1
    # kill `cd /usr/local/var/run/openvswitch && cat ovsdb-server.pid ovs-vswitchd.pid`
  2. 安裝新的openvswitch版本

    • 不改變配置直接編譯即可;
    • 更改配置(目錄及編譯選項)。
  3. 升級數據庫

    • 庫內無重要數據則直接刪除重建;

    • 有重要數據則先備份數據庫,然后利用ovsdb-toolconvert來進行升級

      1
      # ovsdb-tool convert /usr/local/etc/openvswitch/conf.db vswitchd/vswitch.ovsschema
  4. 啟動進程。

    1
    # ovs-ctl start

熱升級

一句話概括:可利用 ovs-ctl restart一次性搞定,如果有內核修改則利用ovs-ctl force-reload-kmod

  1. 升級僅涉及用戶空間程序(實用程序、守護進程),確保新的版本與之前加載的內核模塊兼容。

  2. 用戶空間守護進程的升級意味着它們必須重新啟動。重新啟動守護進程意味着ovs-vswitchd守護進程中的openflow流將丟失。

    恢復流量的一種方法是讓控制器重新填充它。

    另一種方法是使用像ovs-ofctl這樣的工具保存以前的流程,然后在重新啟動后重新添加它們。只有當新的開放vswitch接口保留舊的“端口”值時,恢復舊流才是准確的。

    1
    2
    3
    4
    5
    6
    // ovs-save 可以保存每個橋的流表。ovs-save COMMAND {bridge1|bridge2}
    // ovs-ofctl 封裝而成。具體路徑查找方式前面已寫。
    # /usr/share/openvswitch/scripts/ovs-save save-flows br-int

    //進程重啟之后可通過保存文件進行恢復
    # ovs-ofctl replace-flows br-int /tmp/ovs-save.WvZfM1zEhH/br-int.flows.dump -O OpenFlow14
  3. 當新用戶空間守護進程重新啟動時,它們會自動刷新內核中的舊流設置。如果有數百個進入內核的新流程,但用戶空間守護進程正在忙於從控制器或ovs-ofctl等實用程序中設置新的用戶空間流量,則這可能會很耗時(沖突)。

    打開vswitch數據庫提供了一個通過open_vswitch表的other_config:flow-restore-wait列解決此問題的選項。有關詳細信息,請參閱ovs-vswitchd.conf.db(5)手冊頁

    1
    2
    3
    4
    5
    6
    7
    8
    此選項熱升級的過程如下:
    1、終止ovs-vswitchd
    2、設置`other_config:flow-restore-wait`為true
    3、開啟ovs-vswitchd
    4、利用ovs-ofctl恢復流表
    5、設置`other_config:flow-restore-wait`為false

    ovs-ctl 選項 `restart` 和 `force-reload-kmod`利用了此過程。
  4. 如果升級還涉及升級內核模塊,則需要卸載舊的內核模塊,並且應該加載新的內核模塊。

    這意味着屬於開放vswitch的內核網絡設備被重新創建,並且內核流程丟失。如果用戶空間守護程序立即重新啟動並且用戶空間流盡快恢復,則可以減少流量的停機時間。

    1
    `force-reload-kmod`卸載 vport* 和 openvswitch模塊,重裝 openvswitch 模塊。

ovs-ctl實用程序的重新啟動功能僅重新啟動用戶空間守護程序,確保’ofport’值在重新啟動時保持一致,使用ovs-ofctl實用程序還原用戶空間流,並使用other_config:flow-restore-wait列保留交通宕機時間降至最低。

ovs-ctl實用程序的force-reload-kmod函數完成了上述所有操作,但也用新的內核模塊替換了舊的內核模塊。

打開debian,xenserver和rhel的vswitch啟動腳本使用ovs-ctl的功能,並且建議這些功能也可用於其他軟件平台。

升級實例

驗證升級構建的拓撲如下

升級拓撲

過程如下

  1. br-tun、br-int、 qbr、netns創建

    1
    2
    3
    4
    5
    # ovs-vsctl add-br br-tun 
    # ovs-vsctl add-br br-int
    # brctl addbr qbr0
    # ip netns add ns0
    # ip netns exec ns0 ip link set dev lo up
  2. br-tun、br-int 連接

    1
    2
    # ovs-vsctl add-port br-tun patch-int -- set Interface patch-int type=patch options:peer=patch-tun
    # ovs-vsctl add-port br-int patch-tun -- set Interface patch-tun type=patch options:peer=patch-int
  3. br-int、qbr連接

    1
    2
    3
    4
    # ip link add qvo type veth peer name qvb
    # brctl addif qbr0 qvb
    # ovs-vsctl add-port br-int qvo
    # ifconfig qvb up;ifconfig qvo up; ifconfig qbr0 up;
  4. qbr、ns連接

    1
    2
    3
    4
    5
    # ip link add qn0 type veth peer name qn1
    # brctl addif qbr0 qn0
    # ip link set qn1 netns ns0
    # ip netns exec ns0 ifconfig qn1 10.1.1.1/24 up
    # ifconfig qn0 up
  5. br-tun對端連接

    1
    2
    # ovs-vsctl add-port br-tun vxlan1 -- set interface vxlan1 type=vxlan options:remote_ip=192.168.32.5 options:local_ip=192.168.32.4 
    //對端將remote_ip local_ip換一下位置
  6. 進入對端netns ,ping本端netns內部ip地址,且在本端eth0接口抓包驗證。

    抓包

  7. br-int 和 br-tun流表默認都是全通,所以修改一下,為了升級之后進行確認。

    修改流表

  8. 升級!(升級之前的准備見上文,升級過程中一直ping)。

    • 不升級內核

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      # /usr/share/openvswitch/scripts/ovs-ctl restart 
      Saving flows [ OK ]
      Exiting ovsdb-server (119795) [ OK ]
      Starting ovsdb-server [ OK ]
      system ID not configured, please use --system-id ... failed!
      Configuring Open vSwitch system IDs [ OK ]
      Exiting ovs-vswitchd (119865) [ OK ]
      Starting ovs-vswitchd [ OK ]
      Restoring saved flows [ OK ]
      Enabling remote OVSDB managers [ OK ]

      流量不中斷。

    • 升級內核

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      # /usr/share/openvswitch/scripts/ovs-ctl force-reload-kmod
      Detected internal interfaces: br-int br-tun [ OK ]
      Saving flows [ OK ]
      Exiting ovsdb-server (1278) [ OK ]
      Starting ovsdb-server [ OK ]
      system ID not configured, please use --system-id ... failed!
      Configuring Open vSwitch system IDs [ OK ]
      Exiting ovs-vswitchd (1304) [ OK ]
      Saving interface configuration [ OK ]
      Removing datapath: system@ovs-system [ OK ]
      Removing vport_vxlan module [ OK ]
      Removing openvswitch module [ OK ]
      Inserting openvswitch module [ OK ]
      Starting ovs-vswitchd [ OK ]
      Restoring saved flows [ OK ]
      Enabling remote OVSDB managers [ OK ]
      Restoring interface configuration [ OK ]

      流量不中斷。

      Notes:升級的version >= 2.8.2 時,不會重新加載vport*內核模塊,這是因為 在RHEL 7.x上,遇到了一個由iptables啟動腳本引起的錯誤,該腳本嘗試刪除與linux conntrack相關的所有內核模塊。它無法卸載openvswitch內核模塊,因為它有一個引用計數。但它成功地卸載了vport-geneve,並轉而用上游的“geneve”內核模塊。這會導致隧道斷開。通過不加載基於vport的內核模塊來避免上述情況。 ovs-vswitchd啟動時將加載上游模塊。(參考此處)。

  9. 驗證接口及流表(流量不斷基本就沒什么問題)。

Notes:ovs在openstack環境中升級時,需要將openvswitch-agent先停止,以防止ovs相關進程終止時其自動拉起。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM