1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-30 00:00:11 +01:00

vfio: minor refactoring

This commit is contained in:
Daniel Krebs 2018-05-15 11:23:48 +02:00
parent 94ba899b21
commit b6ff452e53
2 changed files with 21 additions and 13 deletions

View file

@ -18,7 +18,8 @@
#include <linux/vfio.h> #include <linux/vfio.h>
#include <sys/mman.h> #include <sys/mman.h>
#define VFIO_DEV(x) "/dev/vfio/" x #define VFIO_PATH "/dev/vfio/"
#define VFIO_DEV VFIO_PATH "vfio"
/* Forward declarations */ /* Forward declarations */
struct pci_device; struct pci_device;
@ -90,7 +91,7 @@ public:
~VfioGroup(); ~VfioGroup();
static std::unique_ptr<VfioGroup> static std::unique_ptr<VfioGroup>
attach(int containerFd, int groupIndex); attach(const VfioContainer& container, int groupIndex);
private: private:
/// VFIO group file descriptor /// VFIO group file descriptor
@ -138,6 +139,9 @@ public:
bool isIommuEnabled() const bool isIommuEnabled() const
{ return this->hasIommu; } { return this->hasIommu; }
const int& getFd() const
{ return fd; }
private: private:
VfioGroup& getOrAttachGroup(int index); VfioGroup& getOrAttachGroup(int index);

View file

@ -87,7 +87,7 @@ VfioContainer::VfioContainer()
} }
/* Open VFIO API */ /* Open VFIO API */
fd = open(VFIO_DEV("vfio"), O_RDWR); fd = open(VFIO_DEV, O_RDWR);
if (fd < 0) { if (fd < 0) {
logger->error("Failed to open VFIO container"); logger->error("Failed to open VFIO container");
throw std::exception(); throw std::exception();
@ -412,7 +412,7 @@ VfioContainer::getOrAttachGroup(int index)
} }
// group not yet part of this container, so acquire ownership // group not yet part of this container, so acquire ownership
auto group = VfioGroup::attach(fd, index); auto group = VfioGroup::attach(*this, index);
if(not group) { if(not group) {
logger->error("Failed to attach to IOMMU group: {}", index); logger->error("Failed to attach to IOMMU group: {}", index);
throw std::exception(); throw std::exception();
@ -538,6 +538,7 @@ VfioDevice::pciHotReset()
const size_t reset_infolen = sizeof(struct vfio_pci_hot_reset_info) + const size_t reset_infolen = sizeof(struct vfio_pci_hot_reset_info) +
sizeof(struct vfio_pci_dependent_device) * 64; sizeof(struct vfio_pci_dependent_device) * 64;
auto reset_info = reinterpret_cast<struct vfio_pci_hot_reset_info*> auto reset_info = reinterpret_cast<struct vfio_pci_hot_reset_info*>
(calloc(1, reset_infolen)); (calloc(1, reset_infolen));
@ -562,6 +563,8 @@ VfioDevice::pciHotReset()
} }
} }
free(reset_info);
const size_t resetlen = sizeof(struct vfio_pci_hot_reset) + const size_t resetlen = sizeof(struct vfio_pci_hot_reset) +
sizeof(int32_t) * 1; sizeof(int32_t) * 1;
auto reset = reinterpret_cast<struct vfio_pci_hot_reset*> auto reset = reinterpret_cast<struct vfio_pci_hot_reset*>
@ -571,10 +574,10 @@ VfioDevice::pciHotReset()
reset->count = 1; reset->count = 1;
reset->group_fds[0] = this->group.fd; reset->group_fds[0] = this->group.fd;
const bool success = ioctl(this->fd, VFIO_DEVICE_PCI_HOT_RESET, reset) == 0; int ret = ioctl(this->fd, VFIO_DEVICE_PCI_HOT_RESET, reset);
const bool success = (ret == 0);
free(reset); free(reset);
free(reset_info);
if(not success and not group.container->isIommuEnabled()) { if(not success and not group.container->isIommuEnabled()) {
logger->info("PCI hot reset failed, but this is expected without IOMMU"); logger->info("PCI hot reset failed, but this is expected without IOMMU");
@ -740,13 +743,16 @@ VfioGroup::~VfioGroup()
std::unique_ptr<VfioGroup> std::unique_ptr<VfioGroup>
VfioGroup::attach(int containerFd, int groupIndex) VfioGroup::attach(const VfioContainer& container, int groupIndex)
{ {
std::unique_ptr<VfioGroup> group { new VfioGroup(groupIndex) }; std::unique_ptr<VfioGroup> group { new VfioGroup(groupIndex) };
/* Open group fd */ /* Open group fd */
std::stringstream groupPath; std::stringstream groupPath;
groupPath << VFIO_DEV("") << groupIndex; groupPath << VFIO_PATH
<< (container.isIommuEnabled() ? "" : "noiommu-")
<< groupIndex;
group->fd = open(groupPath.str().c_str(), O_RDWR); group->fd = open(groupPath.str().c_str(), O_RDWR);
if (group->fd < 0) { if (group->fd < 0) {
logger->error("Failed to open VFIO group {}", group->index); logger->error("Failed to open VFIO group {}", group->index);
@ -756,13 +762,11 @@ VfioGroup::attach(int containerFd, int groupIndex)
logger->debug("VFIO group {} (fd {}) has path {}", logger->debug("VFIO group {} (fd {}) has path {}",
groupIndex, group->fd, groupPath.str()); groupIndex, group->fd, groupPath.str());
int ret;
/* Claim group ownership */ /* Claim group ownership */
ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &containerFd); int ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &container.getFd());
if (ret < 0) { if (ret < 0) {
logger->error("Failed to attach VFIO group {} to container fd {} (error {})", logger->error("Failed to attach VFIO group {} to container fd {} (error {})",
group->index, containerFd, ret); group->index, container.getFd(), ret);
return nullptr; return nullptr;
} }
@ -771,7 +775,7 @@ VfioGroup::attach(int containerFd, int groupIndex)
VFIO_TYPE1_IOMMU : VFIO_TYPE1_IOMMU :
VFIO_NOIOMMU_IOMMU; VFIO_NOIOMMU_IOMMU;
ret = ioctl(containerFd, VFIO_SET_IOMMU, iommu_type); ret = ioctl(container.getFd(), VFIO_SET_IOMMU, iommu_type);
if (ret < 0) { if (ret < 0) {
logger->error("Failed to set IOMMU type of container: {}", ret); logger->error("Failed to set IOMMU type of container: {}", ret);
return nullptr; return nullptr;