From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by dpdk.org (Postfix) with ESMTP id A3344C332 for ; Fri, 22 May 2015 11:04:42 +0200 (CEST) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga103.jf.intel.com with ESMTP; 22 May 2015 02:04:41 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.13,474,1427785200"; d="scan'208";a="714136213" Received: from shvmail01.sh.intel.com ([10.239.29.42]) by fmsmga001.fm.intel.com with ESMTP; 22 May 2015 02:04:39 -0700 Received: from shecgisg003.sh.intel.com (shecgisg003.sh.intel.com [10.239.29.90]) by shvmail01.sh.intel.com with ESMTP id t4M94bsf003023; Fri, 22 May 2015 17:04:37 +0800 Received: from shecgisg003.sh.intel.com (localhost [127.0.0.1]) by shecgisg003.sh.intel.com (8.13.6/8.13.6/SuSE Linux 0.8) with ESMTP id t4M94Zno014384; Fri, 22 May 2015 17:04:37 +0800 Received: (from yliu84x@localhost) by shecgisg003.sh.intel.com (8.13.6/8.13.6/Submit) id t4M94ZRr014380; Fri, 22 May 2015 17:04:35 +0800 From: "Jiajia, Sun" To: dts@dpdk.org Date: Fri, 22 May 2015 17:04:02 +0800 Message-Id: <1432285452-14286-10-git-send-email-sunx.jiajia@intel.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1432285452-14286-1-git-send-email-sunx.jiajia@intel.com> References: <1432285452-14286-1-git-send-email-sunx.jiajia@intel.com> Subject: [dts] [PATCH v2 09/19] Change and add some functions to support virtual test X-BeenThere: dts@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: test suite reviews and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 22 May 2015 09:04:43 -0000 From: sjiajiax Signed-off-by: sjiajiax --- framework/config.py | 170 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 135 insertions(+), 35 deletions(-) mode change 100755 => 100644 framework/config.py diff --git a/framework/config.py b/framework/config.py old mode 100755 new mode 100644 index d2548e8..7e2436a --- a/framework/config.py +++ b/framework/config.py @@ -37,45 +37,133 @@ import re import ConfigParser # config parse module import argparse # prase arguments module -portconf = "conf/ports.cfg" -crbconf = "conf/crbs.cfg" +PORTCONF = "conf/ports.cfg" +CRBCONF = "conf/crbs.cfg" +VIRTCONF = "conf/virt_global.cfg" class UserConf(): - def __init__(self, port_conf=portconf, crb_conf=crbconf): - self.port_config = port_conf - self.crb_config = crb_conf + def __init__(self, config): + self.conf = ConfigParser.SafeConfigParser() + load_files = self.conf.read(config) + if load_files == []: + print "FAILED LOADING %s!!!" % config + self.conf = None + raise + + def get_sections(self): + if self.conf is None: + return None + + return self.conf.sections() + + def load_section(self, section): + if self.conf is None: + return None + + items = None + for conf_sect in self.conf.sections(): + if conf_sect == section: + items = self.conf.items(section) + + return items + + def load_config(self, item): + confs = [conf.strip() for conf in item.split(';')] + if '' in confs: + confs.remove('') + return confs + + def load_param(self, conf): + paramDict = dict() + + for param in conf.split(','): + (key, _, value) = param.partition('=') + paramDict[key] = value + return paramDict + + +class VirtConf(UserConf): + + def __init__(self, virt_conf=VIRTCONF): + self.config_file = virt_conf + self.virt_cfg = {} + try: + self.virt_conf = UserConf(self.config_file) + except Exception as e: + print "FAILED LOADING VIRT CONFIG!!!" + self.virt_conf = None + + def load_virt_config(self, name): + self.virt_cfgs = [] + + try: + virt_confs = self.virt_conf.load_section(name) + except: + print "FAILED FIND SECTION %s!!!" % name + return + + for virt_conf in virt_confs: + virt_cfg = {} + virt_params = [] + key, config = virt_conf + confs = self.virt_conf.load_config(config) + for config in confs: + virt_params.append(self.load_virt_param(config)) + virt_cfg[key] = virt_params + self.virt_cfgs.append(virt_cfg) + + def get_virt_config(self): + return self.virt_cfgs + + def load_virt_param(self, config): + cfg_params = self.virt_conf.load_param(config) + return cfg_params + + +class PortConf(UserConf): + + def __init__(self, port_conf=PORTCONF): + self.config_file = port_conf self.ports_cfg = {} self.pci_regex = "([\da-f]{2}:[\da-f]{2}.\d{1})$" try: - self.port_conf = ConfigParser.SafeConfigParser() - self.port_conf.read(self.port_config) + self.port_conf = UserConf(self.config_file) except Exception as e: print "FAILED LOADING PORT CONFIG!!!" + self.port_conf = None def load_ports_config(self, crbIP): - ports = [] - for crb in self.port_conf.sections(): - if crb != crbIP: - continue - ports = [port.strip() - for port in self.port_conf.get(crb, 'ports').split(';')] + self.ports_cfg = {} + if self.port_conf is None: + return + + ports = self.port_conf.load_section(crbIP) + if ports is None: + return + key, config = ports[0] + confs = self.port_conf.load_config(config) + + for config in confs: + port_param = self.port_conf.load_param(config) - for port in ports: - port_cfg = self.__parse_port_param(port) # check pci BDF validity - if 'pci' not in port_cfg: + if 'pci' not in port_param: print "NOT FOUND CONFIG FOR NO PCI ADDRESS!!!" continue - m = re.match(self.pci_regex, port_cfg['pci']) + m = re.match(self.pci_regex, port_param['pci']) if m is None: print "INVALID CONFIG FOR NO PCI ADDRESS!!!" continue - keys = port_cfg.keys() + keys = port_param.keys() keys.remove('pci') - self.ports_cfg[port_cfg['pci']] = {key: port_cfg[key] for key in keys} + self.ports_cfg[port_param['pci']] = { + key: port_param[key] for key in keys} + if 'numa' in self.ports_cfg[port_param['pci']]: + numa_str = self.ports_cfg[port_param['pci']]['numa'] + self.ports_cfg[port_param['pci']]['numa'] = int(numa_str) def get_ports_config(self): return self.ports_cfg @@ -86,23 +174,35 @@ class UserConf(): else: return False - def __parse_port_param(self, port): - portDict = dict() - - for param in port.split(','): - (key, _, value) = param.partition('=') - if key == 'numa': - portDict[key] = int(value) - else: - portDict[key] = value - return portDict if __name__ == '__main__': - parser = argparse.ArgumentParser(description="Load DTS configuration files") - parser.add_argument("-p", "--portconf", default=portconf) - parser.add_argument("-c", "--crbconf", default=crbconf) + parser = argparse.ArgumentParser( + description="Load DTS configuration files") + parser.add_argument("-p", "--portconf", default=PORTCONF) + parser.add_argument("-c", "--crbconf", default=CRBCONF) + parser.add_argument("-v", "--virtconf", default=VIRTCONF) args = parser.parse_args() - conf = UserConf() - conf.load_ports_config('192.168.1.1') - conf.check_port_available('0000:86:00.0') + + # not existed configuration file + VirtConf('/tmp/not-existed.cfg') + + # example for basic use configuration file + conf = UserConf(PORTCONF) + for section in conf.get_sections(): + items = conf.load_section(section) + key, value = items[0] + confs = conf.load_config(value) + for config in confs: + conf.load_param(config) + + # example for port configuration file + portconf = PortConf(PORTCONF) + portconf.load_ports_config('DUT IP') + print portconf.get_ports_config() + portconf.check_port_available('86:00.0') + + # example for global virtualization configuration file + virtconf = VirtConf(VIRTCONF) + virtconf.load_virt_config('LIBVIRT') + print virtconf.get_virt_config() -- 1.9.3