this file adding basic functions for configuration of host network
Signed-off-by: Wayne Xia <xiawenc(a)linux.vnet.ibm.com>
---
suites/libvirt-cim/lib/XenKvmLib/host_network.py | 410 ++++++++++++++++++++++
1 files changed, 410 insertions(+), 0 deletions(-)
create mode 100644 suites/libvirt-cim/lib/XenKvmLib/host_network.py
diff --git a/suites/libvirt-cim/lib/XenKvmLib/host_network.py
b/suites/libvirt-cim/lib/XenKvmLib/host_network.py
new file mode 100644
index 0000000..d74b711
--- /dev/null
+++ b/suites/libvirt-cim/lib/XenKvmLib/host_network.py
@@ -0,0 +1,410 @@
+#!/usr/bin/env python
+
+#
+# Copyright 2011 IBM Corp.
+#
+# Authors:
+# Wenchao Xia (Wayne) <xiawenc(a)cn.ibm.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+# This file have basic functions for testing of host network in libvirt-cim
+
+
+import sys
+import os
+import pywbem
+
+from CimTest.Globals import logger, CIM_USER, CIM_PASS, CIM_NS
+from XenKvmLib.classes import get_typed_class
+from XenKvmLib.vxml import get_class
+
+network_clas_prefix = "Net"
+vesssd_parent = "Virt"
+vs_prefix = "VS_"
+ea_prefix = "EA_"
+ec_prefix = "EC_"
+delimiter_System = """:"""
+delimiter_Device = """/"""
+
+vess_cls_name = "Net_VirtualEthernetSwitchSystem"
+vessms_cls_name = "VirtualEthernetSwitchSystemManagementService"
+vesssd_cls_name = "Net_VirtualEthernetSwitchSystemSettingData"
+easd_cls_name = "Net_EthernetPortAllocationSettingData"
+
+vlan_8021q_type = 1
+
+bridge_pNIC = "test_br1"
+bridge_name = """%s%s""" % (vs_prefix, bridge_pNIC)
+bridge_stp = 0
+bridge_stp_modify = 1
+
+
+def set_vs_define(inst):
+ return """
+instance of %s {
+ VirtualSystemIdentifier="%s";
+ STP=%d;
+};""" % (vesssd_cls_name,
+ inst["VirtualSystemIdentifier"],
+ inst["STP"],)
+
+def set_vs_modify(inst):
+ return """
+instance of %s {
+ VirtualSystemIdentifier="%s";
+ STP=%d;
+};""" % (vesssd_cls_name,
+ inst["VirtualSystemIdentifier"],
+ inst["STP"],)
+
+
+def set_vs_destroy(inst):
+ inst = pywbem.CIMInstanceName(classname=vess_cls_name,
keybindings=pywbem.NocaseDict({'CreationClassName': vess_cls_name, 'Name':
inst["Name"]}))
+ return inst
+
+def get_vessms(cim):
+ virt = network_clas_prefix
+ _class = get_typed_class(virt, vessms_cls_name)
+ sys_mgmt_service = cim.EnumerateInstanceNames(_class)[0]
+ #it is not supposed to fail here, the enum of VESSMS is very simple
+ logger.info("got vessms as: %s.", sys_mgmt_service)
+ return sys_mgmt_service
+
+def check_vesssd(cim, targetInst, compareAll = False):
+ #check the result
+ _class = vesssd_cls_name
+ vesssd = cim.EnumerateInstances(_class)
+ targetID = targetInst["Name"]
+ instanceList = ""
+ found = 0
+ for inst in vesssd:
+ items = inst.items()
+ instanceList += str(items)+ "\n"
+ (prefix, ID) = inst["InstanceID"].split(":",1)
+ cmp_ret = cmp(ID, targetID) or cmp(prefix, vesssd_parent)
+ if cmp_ret == 0:
+ logger.info("found the device., it is :\n %s", str(items))
+ if compareAll == False :
+ found = 1
+ else :
+ if inst["STP"] == targetInst["STP"]:
+ found = 1
+ #logger.info("search for %s, found is %d, list is: \n%s", _class, found,
instanceList)
+ logger.info("target vesssd searching result is %d.", found)
+ return found
+
+def try_delete_vs(cim, sys_mgmt_service, inst):
+ #delete the bridge
+ val = set_vs_destroy(inst)
+ method = "DestroySystem"
+ par_name = "AffectedSystem"
+ param = {par_name: val}
+ logger.info("trying method %s of %s with parameter:\n %s", method,
sys_mgmt_service, param)
+ ret = cim.InvokeMethod(method, sys_mgmt_service, **param)
+ found = check_vesssd(cim, inst)
+ if found == 1 :
+ return 0
+ else :
+ return 1
+
+def try_create_vs(cim, sys_mgmt_service, inst):
+ #Check the Env
+ found = check_vesssd(cim, inst)
+ if found == 1:
+ logger.info("device exist, trying delete it");
+ ret = try_delete_vs(cim, sys_mgmt_service, inst)
+ if ret != 1:
+ return 0
+
+ #add the bridge
+ val = set_vs_define(inst)
+ method = "DefineSystem"
+ logger.info("try method %s of %s with parameter %s", method,
sys_mgmt_service, val)
+ ret = cim.InvokeMethod(method, sys_mgmt_service, **{"SystemSettings":
val})
+ found = check_vesssd(cim, inst)
+ if found == 1 :
+ return 1
+ else :
+ return 0
+
+def try_modify_vs(cim, sys_mgmt_service, inst):
+ #modify the bridge
+ val = set_vs_modify(inst)
+ method = "ModifySystemSettings"
+ par_name = "SystemSettings"
+ param = {par_name: val}
+ logger.info("trying method %s of %s with parameter:\n %s", method,
sys_mgmt_service, param)
+ ret = cim.InvokeMethod(method, sys_mgmt_service, **param)
+ found = check_vesssd(cim, inst, True)
+ if found == 1 :
+ return 1
+ else :
+ return 0
+
+
+#inst should be:
+#{'Parent': "eth0", 'VLanID':10, 'ReorderHdr':1,
'Ingress':"1:2 2:3", 'Egress':'2:4'}
+def set_ea_add_8021q(inst):
+ ParentSystem = """%s%s""" % (vs_prefix,
inst['Parent'])
+ AffectedID = """%s%s%s""" % (vesssd_parent,
delimiter_System, ParentSystem)
+ AffectedConfiguration = pywbem.CIMInstanceName(classname= vesssd_cls_name,
keybindings=pywbem.NocaseDict({'InstanceID': AffectedID}))
+ EA_ID = """%s%s.%d""" % (ea_prefix,
inst['Parent'], inst['VLanID'])
+ InstanceID_EA = """%s%s%s""" % (ParentSystem,
delimiter_Device, EA_ID)
+ ea1 = """
+instance of %s {
+ InstanceID ="%s";
+ VLANType = %d;
+ Connection = {"VLAN%d"};
+ ReorderHdr = %d;
+ VLANQosIngress = "%s";
+ VLANQosEgress = "%s";
+};""" % (easd_cls_name,
+ InstanceID_EA,
+ vlan_8021q_type,
+ inst["VLanID"],
+ inst["ReorderHdr"],
+ inst["Ingress"],
+ inst["Egress"])
+ final_param = {"AffectedConfiguration" : AffectedConfiguration,
"ResourceSettings" : [ea1,]}
+ return final_param
+
+def set_ea_mod_8021q(inst):
+ ParentSystem = """%s%s""" % (vs_prefix,
inst['Parent'])
+ EA_ID = """%s%s.%d""" % (ea_prefix,
inst['Parent'], inst['VLanID'])
+ InstanceID_EA = """%s%s%s""" % (ParentSystem,
delimiter_Device, EA_ID)
+ ea1 = """
+instance of %s {
+ InstanceID ="%s";
+ VLANType = %d;
+ Connection = {"VLAN%d"};
+ ReorderHdr = %d;
+ VLANQosIngress = "%s";
+ VLANQosEgress = "%s";
+};""" % (easd_cls_name,
+ InstanceID_EA,
+ vlan_8021q_type,
+ inst["VLanID"],
+ inst["ReorderHdr"],
+ inst["Ingress"],
+ inst["Egress"])
+ final_param = {"ResourceSettings" : [ea1,]}
+ return final_param
+
+def set_ea_del_8021q(inst):
+ ParentSystem = """%s%s""" % (vs_prefix,
inst['Parent'])
+ EA_ID = """%s%s.%d""" % (ea_prefix,
inst['Parent'], inst['VLanID'])
+ InstanceID_EA = """%s%s%s""" % (ParentSystem,
delimiter_Device, EA_ID)
+ del_ref = pywbem.CIMInstanceName(classname= easd_cls_name,
keybindings=pywbem.NocaseDict({'InstanceID': InstanceID_EA}))
+ final_param = {"ResourceSettings" : [del_ref,],}
+ return final_param
+
+
+def check_easd_ea(cim, targetInst, compareAll = False):
+ #check the result
+ _class = easd_cls_name
+ easds = cim.EnumerateInstances(_class)
+
+ inst = targetInst
+ ParentSystem = """%s%s""" % (vs_prefix,
inst['Parent'])
+ EA_ID = """%s%s.%d""" % (ea_prefix,
inst['Parent'], inst['VLanID'])
+ InstanceID_EA = """%s%s%s""" % (ParentSystem,
delimiter_Device, EA_ID)
+ vlanstr = """VLAN%d""" % (inst['VLanID'])
+
+ instanceList = ""
+ found = 0
+ for inst in easds:
+ items = inst.items()
+ instanceList += str(items)+ "\n"
+ cmp_ret = cmp(inst["InstanceID"], InstanceID_EA)
+ if cmp_ret == 0:
+ logger.info("found the device., it is :\n %s", str(items))
+ if compareAll == False :
+ cmp_ret = cmp(inst["Connection"][0], vlanstr)
+ if cmp_ret == 0:
+ found = 1
+ else :
+ found = 1
+ if inst["ReorderHdr"] != targetInst["ReorderHdr"]:
+ found = 0
+ cmp_ret = cmp(inst["VLANQosIngress"],
targetInst["Ingress"])
+ if cmp_ret != 0 :
+ found = 0
+ cmp_ret = cmp(inst["VLANQosEgress"],
targetInst["Egress"])
+ if cmp_ret != 0 :
+ found = 0
+ #logger.info("search for %s, found is %d, list is: \n%s", _class, found,
instanceList)
+ logger.info("target easd searching result is %d.", found)
+ return found
+
+def try_del_ea(cim, sys_mgmt_service, inst):
+ param = set_ea_del_8021q(inst)
+ method = "RemoveResourceSettings"
+ logger.info("trying method %s of %s with parameter:\n %s", method,
sys_mgmt_service, param)
+ ret = cim.InvokeMethod(method, sys_mgmt_service, **param)
+ found = check_easd_ea(cim, inst)
+ if found == 1 :
+ return 0
+ else :
+ return 1
+
+def try_mod_ea(cim, sys_mgmt_service, inst):
+ param = set_ea_mod_8021q(inst)
+ method = "ModifyResourceSettings"
+ logger.info("trying method %s of %s with parameter:\n %s", method,
sys_mgmt_service, param)
+ ret = cim.InvokeMethod(method, sys_mgmt_service, **param)
+ found = check_easd_ea(cim, inst, True)
+ if found == 1 :
+ return 1
+ else :
+ return 0
+
+
+def try_add_ea(cim, sys_mgmt_service, inst):
+ #Check the Env
+ found = check_easd_ea(cim, inst)
+ if found == 1:
+ logger.info("device exist, trying delete it");
+ ret = try_del_ea(cim, sys_mgmt_service, inst)
+ if ret != 1:
+ return 0
+
+ param = set_ea_add_8021q(inst)
+ method = "AddResourceSettings"
+ logger.info("trying method %s of %s with parameter:\n %s", method,
sys_mgmt_service, param)
+ ret = cim.InvokeMethod(method, sys_mgmt_service, **param)
+ found = check_easd_ea(cim, inst)
+ if found == 1 :
+ return 1
+ else :
+ return 0
+
+
+#inst should be:
+#{'ParentBridge': "eth0", 'EthPort':eth0.10,
'TargetBridge': "test_br"}
+def set_ec_add(inst):
+ ParentSystem = """%s%s""" % (vs_prefix,
inst['ParentBridge'])
+ TargetSystem = """%s%s""" % (vs_prefix,
inst['TargetBridge'])
+ AffectedID = """%s%s%s""" % (vesssd_parent,
delimiter_System, ParentSystem)
+ AffectedConfiguration = pywbem.CIMInstanceName(classname= vesssd_cls_name,
keybindings=pywbem.NocaseDict({'InstanceID': AffectedID}))
+ EA_ID = """%s%s""" % (ea_prefix,
inst['EthPort'])
+ EC_ID = """%s%s""" % (ec_prefix,
inst['EthPort'])
+ InstanceID_EA = """%s%s%s""" % (ParentSystem,
delimiter_Device, EA_ID)
+ InstanceID_EC = """%s%s%s""" % (ParentSystem,
delimiter_Device, EC_ID)
+ ea1 = """
+instance of %s {
+ InstanceID ="%s";
+ HostResource = {"%s"};
+ Parent = "%s";
+};""" % (easd_cls_name,
+ InstanceID_EC,
+ TargetSystem,
+ InstanceID_EA)
+ final_param = {"AffectedConfiguration" : AffectedConfiguration,
"ResourceSettings" : [ea1,]}
+ return final_param
+
+def set_ec_del(inst):
+ ParentSystem = """%s%s""" % (vs_prefix,
inst['ParentBridge'])
+ EC_ID = """%s%s""" % (ec_prefix,
inst['EthPort'])
+ InstanceID_EC = """%s%s%s""" % (ParentSystem,
delimiter_Device, EC_ID)
+ del_ref = pywbem.CIMInstanceName(classname= easd_cls_name,
keybindings=pywbem.NocaseDict({'InstanceID': InstanceID_EC}))
+ final_param = {"ResourceSettings" : [del_ref,],}
+ return final_param
+
+def check_easd_ec(cim, targetInst, compareAll = False):
+ #check the result
+ _class = easd_cls_name
+ easds = cim.EnumerateInstances(_class)
+
+ inst = targetInst
+ ParentSystem = """%s%s""" % (vs_prefix,
inst['ParentBridge'])
+ TargetSystem = """%s%s""" % (vs_prefix,
inst['TargetBridge'])
+ EC_ID = """%s%s""" % (ec_prefix,
inst['EthPort'])
+ InstanceID_EC0 = """%s%s%s""" % (ParentSystem,
delimiter_Device, EC_ID)
+ InstanceID_EC1 = """%s%s%s""" % (TargetSystem,
delimiter_Device, EC_ID)
+
+ instanceList = ""
+ EC0_found = 0
+ EC1_found = 0
+ for inst in easds:
+ items = inst.items()
+ instanceList += str(items)+ "\n"
+ cmp_ret = cmp(inst["InstanceID"], InstanceID_EC0)
+ if cmp_ret == 0:
+ logger.info("found the ec0., it is :\n %s", str(items))
+ EC0_found = 1
+ else :
+ cmp_ret = cmp(inst["InstanceID"], InstanceID_EC1)
+ if cmp_ret == 0:
+ logger.info("found the ec1., it is :\n %s", str(items))
+ EC1_found = 1
+ #logger.info("search for %s, list is: \n%s", _class, instanceList)
+ found = EC0_found and EC1_found
+ logger.info("target easd_ec searching result is %d and %d.", EC0_found,
EC1_found)
+ if EC0_found != EC1_found :
+ logger.info("not all ec found as expected.")
+ found = -1
+ return found
+
+def try_del_ec(cim, sys_mgmt_service, inst):
+ param = set_ec_del(inst)
+ method = "RemoveResourceSettings"
+ logger.info("trying method %s of %s with parameter:\n %s", method,
sys_mgmt_service, param)
+ ret = cim.InvokeMethod(method, sys_mgmt_service, **param)
+ found = check_easd_ec(cim, inst)
+ if found == 0 :
+ return 1
+ else :
+ return 0
+
+def try_add_ec(cim, sys_mgmt_service, inst):
+ #Check the Env
+ found = check_easd_ec(cim, inst)
+ if found != 0:
+ logger.info("device exist, trying delete it");
+ ret = try_del_ec(cim, sys_mgmt_service, inst)
+ if ret != 1:
+ return 0
+
+ param = set_ec_add(inst)
+ method = "AddResourceSettings"
+ logger.info("trying method %s of %s with parameter:\n %s", method,
sys_mgmt_service, param)
+ ret = cim.InvokeMethod(method, sys_mgmt_service, **param)
+ found = check_easd_ec(cim, inst)
+ if found == 1 :
+ return 1
+ else :
+ return 0
+
+def FoundOnePNIC(cim):
+ #check the result
+ _class = vesssd_cls_name
+ vesssd = cim.EnumerateInstances(_class)
+ EthList = []
+ found = 0
+ for inst in vesssd:
+ (prefix, ID) = inst["InstanceID"].split(":",1)
+ if ID.find(vs_prefix) == 0 :
+ realname = ID[len(vs_prefix):]
+ if realname.find("eth") == 0 and realname.find(".") == -1
:
+ found = 1
+ EthList.append(realname)
+ #logger.info("search for %s, found is %d, list is: \n%s", _class, found,
instanceList)
+ ret_name = None
+ if found == 1 :
+ ret_name = EthList[0]
+ logger.info("pNIC searching result is %d, list %s, ret is %s.", found,
EthList, ret_name)
+ return ret_name
--
1.7.6