def _release_dev(self, pci_id): """ Release a single PCI device. @param pci_id: PCI ID of a given PCI device. """ base_dir = "/sys/bus/pci" full_id = utils_misc.get_full_pci_id(pci_id) vendor_id = utils_misc.get_vendor_from_pci_id(pci_id) drv_path = os.path.join(base_dir, "devices/%s/driver" % full_id) if 'pci-stub' in os.readlink(drv_path): error.context("Release device %s to host" % pci_id, logging.info) driver = self.dev_unbind_drivers[pci_id] cmd = "echo '%s' > %s/new_id" % (vendor_id, driver) logging.info("Run command in host: %s" % cmd) if os.system(cmd): return False stub_path = os.path.join(base_dir, "drivers/pci-stub") cmd = "echo '%s' > %s/unbind" % (full_id, stub_path) logging.info("Run command in host: %s" % cmd) if os.system(cmd): return False driver = self.dev_unbind_drivers[pci_id] cmd = "echo '%s' > %s/bind" % (full_id, driver) logging.info("Run command in host: %s" % cmd) if os.system(cmd): return False return True
def _release_dev(self, pci_id): """ Release a single PCI device. @param pci_id: PCI ID of a given PCI device. """ base_dir = "/sys/bus/pci" full_id = utils_misc.get_full_pci_id(pci_id) vendor_id = utils_misc.get_vendor_from_pci_id(pci_id) drv_path = os.path.join(base_dir, "devices/%s/driver" % full_id) if 'pci-stub' in os.readlink(drv_path): cmd = "echo '%s' > %s/new_id" % (vendor_id, drv_path) if os.system(cmd): return False stub_path = os.path.join(base_dir, "drivers/pci-stub") cmd = "echo '%s' > %s/unbind" % (full_id, stub_path) if os.system(cmd): return False driver = self.dev_drivers[pci_id] cmd = "echo '%s' > %s/bind" % (full_id, driver) if os.system(cmd): return False return True
def request_devs(self, count=None): """ Implement setup process: unbind the PCI device and then bind it to the pci-stub driver. @param count: count number of PCI devices needed for pass through @return: a list of successfully requested devices' PCI IDs. """ if count is None: count = self.devices_requested base_dir = "/sys/bus/pci" stub_path = os.path.join(base_dir, "drivers/pci-stub") self.pci_ids = self.get_devs(count) logging.info("The following pci_ids were found: %s", self.pci_ids) requested_pci_ids = [] # Setup all devices specified for assignment to guest for pci_id in self.pci_ids: full_id = utils_misc.get_full_pci_id(pci_id) if not full_id: continue drv_path = os.path.join(base_dir, "devices/%s/driver" % full_id) dev_prev_driver = os.path.realpath( os.path.join(drv_path, os.readlink(drv_path))) self.dev_drivers[pci_id] = dev_prev_driver # Judge whether the device driver has been binded to stub if not self.is_binded_to_stub(full_id): error.context("Bind device %s to stub" % full_id, logging.info) vendor_id = utils_misc.get_vendor_from_pci_id(pci_id) stub_new_id = os.path.join(stub_path, 'new_id') unbind_dev = os.path.join(drv_path, 'unbind') stub_bind = os.path.join(stub_path, 'bind') info_write_to_files = [(vendor_id, stub_new_id), (full_id, unbind_dev), (full_id, stub_bind)] for content, file in info_write_to_files: try: utils.open_write_close(file, content) except IOError: logging.debug("Failed to write %s to file %s", content, file) continue if not self.is_binded_to_stub(full_id): logging.error("Binding device %s to stub failed", pci_id) continue else: logging.debug("Device %s already binded to stub", pci_id) requested_pci_ids.append(pci_id) return requested_pci_ids
def request_devs(self, count=None): """ Implement setup process: unbind the PCI device and then bind it to the pci-stub driver. @param count: count number of PCI devices needed for pass through @return: a list of successfully requested devices' PCI IDs. """ if count is None: count = self.devices_requested base_dir = "/sys/bus/pci" stub_path = os.path.join(base_dir, "drivers/pci-stub") self.pci_ids = self.get_devs(count) logging.info("The following pci_ids were found: %s", self.pci_ids) requested_pci_ids = [] # Setup all devices specified for assignment to guest for pci_id in self.pci_ids: full_id = utils_misc.get_full_pci_id(pci_id) if not full_id: continue drv_path = os.path.join(base_dir, "devices/%s/driver" % full_id) dev_prev_driver = os.path.realpath(os.path.join(drv_path, os.readlink(drv_path))) self.dev_drivers[pci_id] = dev_prev_driver # Judge whether the device driver has been binded to stub if not self.is_binded_to_stub(full_id): error.context("Bind device %s to stub" % full_id, logging.info) vendor_id = utils_misc.get_vendor_from_pci_id(pci_id) stub_new_id = os.path.join(stub_path, 'new_id') unbind_dev = os.path.join(drv_path, 'unbind') stub_bind = os.path.join(stub_path, 'bind') info_write_to_files = [(vendor_id, stub_new_id), (full_id, unbind_dev), (full_id, stub_bind)] for content, file in info_write_to_files: try: utils.open_write_close(file, content) except IOError: logging.debug("Failed to write %s to file %s", content, file) continue if not self.is_binded_to_stub(full_id): logging.error("Binding device %s to stub failed", pci_id) continue else: logging.debug("Device %s already binded to stub", pci_id) requested_pci_ids.append(pci_id) return requested_pci_ids
def get_pf_vf_info(self): """ Get pf and vf related information in this host that match ``self.pf_filter_re``. for every pf it will create following information: pf_id: The id of the pf device. occupied: Whether the pf device assigned or not vf_ids: Id list of related vf in this pf. ethname: eth device name in host for this pf. :return: return a list contains pf vf information. :rtype: list of dict """ base_dir = "/sys/bus/pci/devices" cmd = "lspci | awk '/%s/ {print $1}'" % self.pf_filter_re pf_ids = [i for i in utils.system_output(cmd).splitlines()] pf_vf_dict = [] for pf_id in pf_ids: pf_info = {} vf_ids = [] full_id = utils_misc.get_full_pci_id(pf_id) pf_info["pf_id"] = full_id pf_info["occupied"] = False d_link = os.path.join("/sys/bus/pci/devices", full_id) txt = utils.system_output("ls %s" % d_link) re_vfn = "(virtfn[0-9])" paths = re.findall(re_vfn, txt) for path in paths: f_path = os.path.join(d_link, path) vf_id = os.path.basename(os.path.realpath(f_path)) vf_ids.append(vf_id) pf_info["vf_ids"] = vf_ids pf_vf_dict.append(pf_info) if_out = utils.system_output("ifconfig -a") re_ethname = "\w+(?=: flags)|eth[0-9](?=\s*Link)" ethnames = re.findall(re_ethname, if_out) for eth in ethnames: cmd = "ethtool -i %s | awk '/bus-info/ {print $2}'" % eth pci_id = utils.system_output(cmd) if not pci_id: continue for pf in pf_vf_dict: if pci_id in pf["pf_id"]: pf["ethname"] = eth return pf_vf_dict