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. :type pci_id: string :return: True if successfully release the device. else false. :rtype: bool """ base_dir = "/sys/bus/pci" short_id = pci_id[5:] vendor_id = utils_misc.get_vendor_from_pci_id(short_id) drv_path = os.path.join(base_dir, "devices/%s/driver" % pci_id) if self.device_driver 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/%s" % self.device_driver) cmd = "echo '%s' > %s/unbind" % (pci_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" % (pci_id, driver) logging.info("Run command in host: %s" % cmd) if os.system(cmd): return False if self.is_binded_to_stub(pci_id): 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, devices=None): """ Implement setup process: unbind the PCI device and then bind it to the device driver. :param devices: List of device dict :type devices: List of dict :return: List of successfully requested devices' PCI IDs. :rtype: List of string """ if not self.pf_vf_info: self.pf_vf_info = self.get_pf_vf_info() base_dir = "/sys/bus/pci" stub_path = os.path.join(base_dir, "drivers/%s" % self.device_driver) self.pci_ids = self.get_devs(devices) logging.info("The following pci_ids were found: %s", self.pci_ids) requested_pci_ids = [] # Setup all devices specified for assignment to guest for p_id in self.pci_ids: if self.device_driver == "vfio-pci": pci_ids = self.get_same_group_devs(p_id) logging.info("Following devices are in same group: %s", pci_ids) else: pci_ids = [p_id] for pci_id in pci_ids: short_id = pci_id[5:] drv_path = os.path.join(base_dir, "devices/%s/driver" % pci_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(pci_id): error.context("Bind device %s to stub" % pci_id, logging.info) vendor_id = utils_misc.get_vendor_from_pci_id(short_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), (pci_id, unbind_dev), (pci_id, stub_bind)] for content, f_name in info_write_to_files: try: logging.info("Write '%s' to file '%s'", content, f_name) utils.open_write_close(f_name, content) except IOError: logging.debug("Failed to write %s to file %s", content, f_name) continue if not self.is_binded_to_stub(pci_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(p_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 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 _release_dev(self, pci_id): """ Release a single PCI device. :param pci_id: PCI ID of a given PCI device. :type pci_id: string :return: True if successfully release the device. else false. :rtype: bool """ base_dir = "/sys/bus/pci" short_id = pci_id[5:] vendor_id = utils_misc.get_vendor_from_pci_id(short_id) drv_path = os.path.join(base_dir, "devices/%s/driver" % pci_id) if self.device_driver 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) try: output = utils.system_output(cmd, timeout=60) except error.CmdError: msg = "Command %s fail with output %s" % (cmd, output) logging.error(msg) return False stub_path = os.path.join(base_dir, "drivers/%s" % self.device_driver) cmd = "echo '%s' > %s/unbind" % (pci_id, stub_path) logging.info("Run command in host: %s" % cmd) try: output = utils.system_output(cmd, timeout=60) except error.CmdError: msg = "Command %s fail with output %s" % (cmd, output) logging.error(msg) return False driver = self.dev_unbind_drivers[pci_id] cmd = "echo '%s' > %s/bind" % (pci_id, driver) logging.info("Run command in host: %s" % cmd) try: output = utils.system_output(cmd, timeout=60) except error.CmdError: msg = "Command %s fail with output %s" % (cmd, output) logging.error(msg) return False if self.is_binded_to_stub(pci_id): return False return True
def request_devs(self, devices=None): """ Implement setup process: unbind the PCI device and then bind it to the device driver. :param devices: List of device dict :type devices: List of dict :return: List of successfully requested devices' PCI IDs. :rtype: List of string """ if not self.pf_vf_info: self.pf_vf_info = self.get_pf_vf_info() base_dir = "/sys/bus/pci" stub_path = os.path.join(base_dir, "drivers/%s" % self.device_driver) self.pci_ids = self.get_devs(devices) logging.info("The following pci_ids were found: %s", self.pci_ids) requested_pci_ids = [] # Setup all devices specified for assignment to guest for p_id in self.pci_ids: if self.device_driver == "vfio-pci": pci_ids = self.get_same_group_devs(p_id) logging.info("Following devices are in same group: %s", pci_ids) else: pci_ids = [p_id] for pci_id in pci_ids: short_id = pci_id[5:] drv_path = os.path.join(base_dir, "devices/%s/driver" % pci_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(pci_id): error.context("Bind device %s to stub" % pci_id, logging.info) vendor_id = utils_misc.get_vendor_from_pci_id(short_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), (pci_id, unbind_dev), (pci_id, stub_bind)] for content, f_name in info_write_to_files: try: logging.info("Write '%s' to file '%s'", content, f_name) utils.open_write_close(f_name, content) except IOError: logging.debug("Failed to write %s to file %s", content, f_name) continue if not self.is_binded_to_stub(pci_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(p_id) return requested_pci_ids