From bb402b3c0e9143d21219eac6ffb940b46dda66e6 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Thu, 13 Feb 2025 14:21:27 +0100 Subject: [PATCH] Move "device_connection" implementation to .cpp Signed-off-by: Pascal Bauer --- .../kernel/devices/device_connection.hpp | 50 ++------------ common/lib/CMakeLists.txt | 1 + .../lib/kernel/devices/device_connection.cpp | 69 +++++++++++++++++++ 3 files changed, 75 insertions(+), 45 deletions(-) create mode 100644 common/lib/kernel/devices/device_connection.cpp diff --git a/common/include/villas/kernel/devices/device_connection.hpp b/common/include/villas/kernel/devices/device_connection.hpp index a65f171f4..f38c413fb 100644 --- a/common/include/villas/kernel/devices/device_connection.hpp +++ b/common/include/villas/kernel/devices/device_connection.hpp @@ -10,8 +10,9 @@ #include -#include #include +#include +#include namespace villas { namespace kernel { @@ -23,55 +24,14 @@ public: const std::shared_ptr vfio_device; private: - DeviceConnection(std::shared_ptr vfio_device) - : logger(villas::Log::get("DeviceConnection")), - vfio_device(vfio_device) {}; + DeviceConnection(std::shared_ptr vfio_device); public: static DeviceConnection from(const villas::kernel::devices::Device &device, - std::shared_ptr vfio_container) { - auto logger = villas::Log::get("Builder: DeviceConnection"); + std::shared_ptr vfio_container); - // Bind to driver - LinuxDriver driver( - std::filesystem::path("/sys/bus/platform/drivers/vfio-platform")); - driver.attach(device); - - // Attach group to container - const int iommu_group = device.iommu_group().value(); - auto vfio_group = vfio_container->getOrAttachGroup(iommu_group); - logger->debug("Device: {}, Iommu: {}", device.name(), iommu_group); - - // Open Vfio Device - auto vfio_device = std::make_shared( - device.name(), vfio_group->getFileDescriptor()); - - // Attach device to group - vfio_group->attachDevice(vfio_device); - - return DeviceConnection(vfio_device); - } - - void add_to_memorygraph() const { - // Map vfio device memory to process - const void *mapping = this->vfio_device->regionMap(0); - if (mapping == MAP_FAILED) { - logger->error("Failed to mmap() device"); - } - logger->debug("memory mapped: {}", this->vfio_device->getName()); - - // Create memorygraph edge from process to vfio device - auto &mm = MemoryManager::get(); - size_t srcVertexId = mm.getProcessAddressSpace(); - const size_t mem_size = this->vfio_device->regionGetSize(0); - size_t targetVertexId = - mm.getOrCreateAddressSpace(this->vfio_device->getName()); - mm.createMapping(reinterpret_cast(mapping), 0, mem_size, - "process to vfio", srcVertexId, targetVertexId); - logger->debug("create edge from process to {}", - this->vfio_device->getName()); - } + void add_to_memorygraph() const; }; } // namespace devices diff --git a/common/lib/CMakeLists.txt b/common/lib/CMakeLists.txt index 0f9680bfb..497fe9994 100644 --- a/common/lib/CMakeLists.txt +++ b/common/lib/CMakeLists.txt @@ -35,6 +35,7 @@ add_library(villas-common SHARED if(CMAKE_SYSTEM_NAME STREQUAL Linux) target_sources(villas-common PRIVATE + kernel/devices/device_connection.cpp kernel/devices/ip_device.cpp kernel/devices/linux_driver.cpp kernel/devices/pci_device.cpp diff --git a/common/lib/kernel/devices/device_connection.cpp b/common/lib/kernel/devices/device_connection.cpp new file mode 100644 index 000000000..a2b85d1a3 --- /dev/null +++ b/common/lib/kernel/devices/device_connection.cpp @@ -0,0 +1,69 @@ +/* Vfio connection to a device. + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2024-25 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include + +#include +#include + +namespace villas { +namespace kernel { +namespace devices { + +DeviceConnection::DeviceConnection( + std::shared_ptr vfio_device) + : logger(villas::Log::get("DeviceConnection")), vfio_device(vfio_device) {}; + +DeviceConnection DeviceConnection::from(const villas::kernel::devices::Device &device, + std::shared_ptr vfio_container) { + auto logger = villas::Log::get("Builder: DeviceConnection"); + + // Bind to driver + LinuxDriver driver( + std::filesystem::path("/sys/bus/platform/drivers/vfio-platform")); + driver.attach(device); + + // Attach group to container + const int iommu_group = device.iommu_group().value(); + auto vfio_group = vfio_container->getOrAttachGroup(iommu_group); + logger->debug("Device: {}, Iommu: {}", device.name(), iommu_group); + + // Open Vfio Device + auto vfio_device = std::make_shared( + device.name(), vfio_group->getFileDescriptor()); + + // Attach device to group + vfio_group->attachDevice(vfio_device); + + return DeviceConnection(vfio_device); +} + +void DeviceConnection::add_to_memorygraph() const { + // Map vfio device memory to process + const void *mapping = this->vfio_device->regionMap(0); + if (mapping == MAP_FAILED) { + logger->error("Failed to mmap() device"); + } + logger->debug("memory mapped: {}", this->vfio_device->getName()); + + // Create memorygraph edge from process to vfio device + auto &mm = MemoryManager::get(); + size_t srcVertexId = mm.getProcessAddressSpace(); + const size_t mem_size = this->vfio_device->regionGetSize(0); + size_t targetVertexId = + mm.getOrCreateAddressSpace(this->vfio_device->getName()); + mm.createMapping(reinterpret_cast(mapping), 0, mem_size, + "process to vfio", srcVertexId, targetVertexId); + logger->debug("create edge from process to {}", this->vfio_device->getName()); +} + +} // namespace devices +} // namespace kernel +} // namespace villas \ No newline at end of file