2013年1月31日 星期四

KVM libvirt with openvswitch

一般來說,實體主機或虛擬主機要連接 802.1q VLAN tagging 只要裝上 vconfig 套件,再加上一些設定,應該就沒有太大的問題,網路上有很多文章可以參考,不外乎就是:
# apt-get install vlan
# vconfig add eth0 100
# ifconfig eth0.100 <YOUR_IP> <YOUR_NETMASK> up
關於 Linux VLAN/vconfig 你可以參考 這篇

這次為了要讓 Hypervisor 裏面模擬一台帶有 VLAN tag 的 swtich(虛擬第一層),並幫 Hypervisor 裏面的虛擬機路由 802.1q VLAN tagging 的封包(虛擬第二層),所以用到了 nested KVM 與 openvswitch 的技術。要這麼做的話,你對於 KVM、Libvirt、iptables 要有一些基礎的了解。

nested KVM 是參考 這篇
openvswitch 是參考 這篇 還有 這篇 文章,當然你最好能先看看 Open vSwitch 官網的說明,包含支援的版本以及操作。
http://binarybitme.blogspot.tw/2011/09/using-open-vswitch-with-libvirt-and.html

Libvirt 0.9.11 或更新的版本已經支援使用 Open vSwitch,透過 .deb 或 .rpm 安裝 libvirt 與 openvswitch,我在 debian 裝的版本如下:
libvirt-bin    0.9.12-5    amd64        programs for the libvirt library
libvirt0    0.9.12-5    amd64        library for interfacing with different virtualization systems
openvswitch-brcompat    1.4.2+git20120612-9    amd64        Open vSwitch bridge compatibility support
openvswitch-common    1.4.2+git20120612-9    amd64        Open vSwitch common components
openvswitch-datapath-dkms    1.4.2+git20120612-9    all          Open vSwitch datapath module source - DKMS version
openvswitch-switch    1.4.2+git20120612-9    amd64        Open vSwitch switch implementations

大致上就是參考前面提到的文章裏面的安裝與設定,我摘錄一些我的步驟:
# apt-get install openvswitch-common openvswitch-switch openvswitch-brcompat openvswitch-datapath-dkms

# vi /etc/default/openvswitch-switch
BRCOMPAT=yes

我有重新開機,然後檢查:
# lsmod |grep brcom
brcompat_mod           13031  0 
openvswitch_mod        67948  2 brcompat_mod

# service openvswitch-switch status
ovsdb-server is running with pid 3361
ovs-vswitchd is running with pid 3449
ovs-brcompatd is running with pid 3473

到此,應該不會有太大的問題,不過你原來有安裝設定過 openvswitch,中間設定調整數次後出了問題,例如 brcompat_mod 的 module 原來掛的起來後來又出錯,或是原來會通的設定之後又不運作了,建議你可以把前述 packages 移除再重裝看看。

接著就是要將你規劃的網路架構,用 openvswitch 來實現:
openvswitch 用一個 bridge 來實現 tagging 及其 tag id,但是它並非真的 bridge 而是一個 internal port/interfacce,用來傳送 tagging 封包,稱為 fake bridge。我的案例是做出一個 fake bridge vlan10 其 tag id 是 10,虛擬機藉由 vlan10 帶有 tag id 10 連出:
# ovs-vsctl add-br br0
# ovs-vsctl add-port eth0
# ovs-vsctl add br vlan10 br0 10
# ovs-vsctl show

# ovs-vsctl show
aedc8e9e-1881-4c0c-8846-22a7f6d424a8
    Bridge "br0"
        Port "br0"
            Interface "br0"
                type: internal
        Port "eth0"
            Interface "eth0"
        Port "vlan10"  <===== 就是這個
            tag: 10
            Interface "vlan10"
                type: internal
    ovs_version: "1.4.2"


# ovs-vsctl list port vlan10

_uuid               : be520db1-bc7f-4302-8521-32ded2a02eb9
bond_downdelay      : 0
bond_fake_iface     : false
bond_mode           : []
bond_updelay        : 0
external_ids        : {}
fake_bridge         : true
interfaces          : [76f652cb-e0b5-4be4-9acb-9488ebda72af]
lacp                : []
mac                 : []
name                : "vlan10"
other_config        : {}
qos                 : []
statistics          : {}
status              : {}
tag                 : 10  <===== 帶有 10 的 tag id
trunks              : []
vlan_mode           : []

接著就可以使用這個 fake bridge 作為虛擬機的網路介面,前述文章都說直接使用 vlan10 這個 interface/port 加到 VM 的 libvirt 設定中就可以,但是我怎麼弄,都沒辦法運作!

後來我是使用 br0 才會通,請參考看看:









# virsh edit <YOUR_VM_NAME> 看起來是這樣:






*有位網友有提到,要像下面這樣加入設定,到時候 libvirt 在啟動 VM 時,會自動帶起一個名為 vnet0 的 interface:
<interface type='bridge'>
    <mac address='52:54:00:43:1f:f4'/>
    <source bridge='br0'/>
    <virtualport type='openvswitch'>
    </virtualport>
</interface>

我無論有沒有加入 <virtualport type> 都可以運作,也許這是屬於舊版 libvirt+openvsiwth 的作法吧?

最後,也是我一直搞不定的,使用 tcpdump 檢查封包是否真的帶有 vlan tag id,我是參考 這篇,但是怎麼試都試不出來,也許有好心網友可以教教我,感謝 : )

沒有留言:

張貼留言