From babe80f53ec489a607fa8df1b652bac6fe4574fe Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:36:02 +0200 Subject: [PATCH 01/50] add device interface Signed-off-by: Pascal Bauer --- .../include/villas/kernel/devices/device.hpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 common/include/villas/kernel/devices/device.hpp diff --git a/common/include/villas/kernel/devices/device.hpp b/common/include/villas/kernel/devices/device.hpp new file mode 100644 index 000000000..1486638e1 --- /dev/null +++ b/common/include/villas/kernel/devices/device.hpp @@ -0,0 +1,33 @@ +/* Device + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include + +namespace villas { +namespace kernel { +namespace devices { + +class Device { +public: + virtual ~Device(){}; + + virtual std::optional> driver() const = 0; + virtual std::optional iommu_group() const = 0; + virtual std::string name() const = 0; + virtual std::filesystem::path override_path() const = 0; + virtual std::filesystem::path path() const = 0; + virtual void probe() const = 0; +}; + +} // namespace devices +} // namespace kernel +} // namespace villas From d312e377bbbef3abe96dbbf7a7e6eb26a1fc89c6 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:36:12 +0200 Subject: [PATCH 02/50] add driver interface Signed-off-by: Pascal Bauer --- .../include/villas/kernel/devices/driver.hpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 common/include/villas/kernel/devices/driver.hpp diff --git a/common/include/villas/kernel/devices/driver.hpp b/common/include/villas/kernel/devices/driver.hpp new file mode 100644 index 000000000..8c58bb726 --- /dev/null +++ b/common/include/villas/kernel/devices/driver.hpp @@ -0,0 +1,28 @@ +/* Driver + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +namespace villas { +namespace kernel { +namespace devices { + +class Device; + +class Driver { +public: + virtual void attach(const Device &device) const = 0; + virtual void bind(const Device &device) const = 0; + virtual std::string name() const = 0; + virtual void override(const Device &device) const = 0; + virtual void unbind(const Device &device) const = 0; +}; + +} // namespace devices +} // namespace kernel +} // namespace villas \ No newline at end of file From adeaa9dee6c858ea5a28f09a549fa6452119a96c Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:36:25 +0200 Subject: [PATCH 03/50] add generic driver Signed-off-by: Pascal Bauer --- .../villas/kernel/devices/generic_driver.hpp | 52 +++++++++++++++++++ common/lib/kernel/devices/generic_driver.cpp | 40 ++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 common/include/villas/kernel/devices/generic_driver.hpp create mode 100644 common/lib/kernel/devices/generic_driver.cpp diff --git a/common/include/villas/kernel/devices/generic_driver.hpp b/common/include/villas/kernel/devices/generic_driver.hpp new file mode 100644 index 000000000..20d0c613e --- /dev/null +++ b/common/include/villas/kernel/devices/generic_driver.hpp @@ -0,0 +1,52 @@ +/* GenericDriver - Works for standard Linux drivers + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include +#include + +namespace villas { +namespace kernel { +namespace devices { + +class GenericDriver : public Driver { +private: + static constexpr char BIND_DEFAULT[] = "bind"; + static constexpr char UNBIND_DEFAULT[] = "unbind"; + +public: + const std::filesystem::path path; + +private: + const std::filesystem::path bind_path; + const std::filesystem::path unbind_path; + +public: + GenericDriver(const std::filesystem::path path) + : GenericDriver(path, path / std::filesystem::path(BIND_DEFAULT), + path / std::filesystem::path(UNBIND_DEFAULT)){}; + + GenericDriver(const std::filesystem::path path, + const std::filesystem::path bind_path, + const std::filesystem::path unbind_path) + : path(path), bind_path(bind_path), unbind_path(unbind_path){}; + +public: + virtual void attach(const Device &device) const = 0; + virtual void bind(const Device &device) const = 0; + virtual std::string name() const = 0; + virtual void override(const Device &device) const = 0; + virtual void unbind(const Device &device) const = 0; +}; + +} // namespace devices +} // namespace kernel +} // namespace villas \ No newline at end of file diff --git a/common/lib/kernel/devices/generic_driver.cpp b/common/lib/kernel/devices/generic_driver.cpp new file mode 100644 index 000000000..9e0de6d98 --- /dev/null +++ b/common/lib/kernel/devices/generic_driver.cpp @@ -0,0 +1,40 @@ +/* GenericDriver + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include +#include + +using villas::kernel::devices::Device, villas::kernel::devices::GenericDriver; +using villas::kernel::devices::utils::write_to_file; + +void GenericDriver::attach(const Device &device) const { + if (device.driver().has_value()) { + device.driver().value()->unbind(device); + } + this->override(device); + device.probe(); +} + +void GenericDriver::bind(const Device &device) const { + write_to_file(device.name(), this->bind_path); +} + +std::string GenericDriver::name() const { + size_t pos = path.u8string().rfind('/'); + return path.u8string().substr(pos + 1); +} + +void GenericDriver::override(const Device &device) const { + write_to_file(this->name(), device.override_path()); +} + +void GenericDriver::unbind(const Device &device) const { + write_to_file(device.name(), this->unbind_path); +} From dca4d4c66a97262556a86511ca0957707089a3f1 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:36:42 +0200 Subject: [PATCH 04/50] add utils Signed-off-by: Pascal Bauer --- .../include/villas/kernel/devices/utils.hpp | 26 ++++++++++ common/lib/kernel/devices/utils.cpp | 48 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 common/include/villas/kernel/devices/utils.hpp create mode 100644 common/lib/kernel/devices/utils.cpp diff --git a/common/include/villas/kernel/devices/utils.hpp b/common/include/villas/kernel/devices/utils.hpp new file mode 100644 index 000000000..9d4984892 --- /dev/null +++ b/common/include/villas/kernel/devices/utils.hpp @@ -0,0 +1,26 @@ +/* Utils + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include + +namespace villas { +namespace kernel { +namespace devices { +namespace utils { + +void write_to_file(std::string data, const std::filesystem::path file); +std::vector read_names_in_directory(const std::string &name); + +} // namespace utils +} // namespace devices +} // namespace kernel +} // namespace villas diff --git a/common/lib/kernel/devices/utils.cpp b/common/lib/kernel/devices/utils.cpp new file mode 100644 index 000000000..16fb5e387 --- /dev/null +++ b/common/lib/kernel/devices/utils.cpp @@ -0,0 +1,48 @@ +/* Utils + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include + +namespace utils = villas::kernel::devices::utils; + +void utils::write_to_file(std::string data, const std::filesystem::path file) { + villas::Log::get("Filewriter")->debug("{} > {}", data, file.u8string()); + std::ofstream outputFile(file.u8string()); + + if (outputFile.is_open()) { + // Write to file + outputFile << data; + outputFile.close(); + } else { + throw std::filesystem::filesystem_error("Cannot open outputfile", + std::error_code()); + } +} + +std::vector +utils::read_names_in_directory(const std::string &name) { + DIR *directory = opendir(name.c_str()); + + struct dirent *dp; + std::vector names; + dp = readdir(directory); + while (dp != NULL) { + auto name = std::string(dp->d_name); + if (name != "." && name != "..") { + names.push_back(name); + } + dp = readdir(directory); + } + closedir(directory); + return names; +} \ No newline at end of file From 9d6b81b15a563a8434d35e9f2d535941a528561b Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:36:56 +0200 Subject: [PATCH 05/50] add platform device Signed-off-by: Pascal Bauer --- .../villas/kernel/devices/platform_device.hpp | 52 ++++++++++++++++++ common/lib/kernel/devices/platform_device.cpp | 54 +++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 common/include/villas/kernel/devices/platform_device.hpp create mode 100644 common/lib/kernel/devices/platform_device.cpp diff --git a/common/include/villas/kernel/devices/platform_device.hpp b/common/include/villas/kernel/devices/platform_device.hpp new file mode 100644 index 000000000..4cef2ceb4 --- /dev/null +++ b/common/include/villas/kernel/devices/platform_device.hpp @@ -0,0 +1,52 @@ +/* Platform Device + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include +#include + +namespace villas { +namespace kernel { +namespace devices { + +class PlatformDevice : public Device { +private: + static constexpr char PROBE_DEFAULT[] = "/sys/bus/platform/drivers_probe"; + static constexpr char OVERRIDE_DEFAULT[] = "driver_override"; + +private: + const std::filesystem::path m_path; + const std::filesystem::path m_probe_path; + const std::filesystem::path m_override_path; + +public: + PlatformDevice(const std::filesystem::path path) + : PlatformDevice(path, std::filesystem::path(PROBE_DEFAULT), + path / std::filesystem::path(OVERRIDE_DEFAULT)){}; + + PlatformDevice(const std::filesystem::path path, + const std::filesystem::path probe_path, + const std::filesystem::path override_path) + : m_path(path), m_probe_path(probe_path), + m_override_path(override_path){}; + + // Implement device interface + std::optional> driver() const override; + std::optional iommu_group() const override; + std::string name() const override; + std::filesystem::path override_path() const override; + std::filesystem::path path() const override; + void probe() const override; +}; + +} // namespace devices +} // namespace kernel +} // namespace villas \ No newline at end of file diff --git a/common/lib/kernel/devices/platform_device.cpp b/common/lib/kernel/devices/platform_device.cpp new file mode 100644 index 000000000..28b6c4796 --- /dev/null +++ b/common/lib/kernel/devices/platform_device.cpp @@ -0,0 +1,54 @@ +/* Platform Device + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +using villas::kernel::devices::Driver, villas::kernel::devices::GenericDriver; +using villas::kernel::devices::PlatformDevice; +using villas::kernel::devices::utils::write_to_file; + +std::optional> PlatformDevice::driver() const { + std::filesystem::path driver_symlink = + this->m_path / std::filesystem::path("driver"); + + if (!std::filesystem::is_symlink(driver_symlink)) + return std::nullopt; + + std::filesystem::path driver_path = + std::filesystem::canonical(driver_symlink); + return std::make_optional(std::make_unique(driver_path)); +} + +std::optional PlatformDevice::iommu_group() const { + std::filesystem::path symlink = + std::filesystem::path(this->m_path.u8string() + "/iommu_group"); + + std::filesystem::path link; + link = std::filesystem::read_symlink(symlink); + + std::string delimiter = "iommu_groups/"; + int pos = link.u8string().find(delimiter); + int iommu_group = std::stoi(link.u8string().substr(pos + delimiter.length())); + return std::make_optional(iommu_group); +} + +std::filesystem::path PlatformDevice::path() const { return this->m_path; }; + +void PlatformDevice::probe() const { + write_to_file(this->name(), this->m_probe_path); +} + +std::filesystem::path PlatformDevice::override_path() const { + return this->m_override_path; +} + +std::string PlatformDevice::name() const { + size_t pos = this->m_path.u8string().rfind('/'); + return this->m_path.u8string().substr(pos + 1); +} \ No newline at end of file From d65910a14a8b040748bd6bcb744e57e91a0f108f Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:37:06 +0200 Subject: [PATCH 06/50] add IpDevice Signed-off-by: Pascal Bauer --- .../villas/kernel/devices/ip_device.hpp | 34 ++++++++++++ common/lib/kernel/devices/ip_device.cpp | 55 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 common/include/villas/kernel/devices/ip_device.hpp create mode 100644 common/lib/kernel/devices/ip_device.cpp diff --git a/common/include/villas/kernel/devices/ip_device.hpp b/common/include/villas/kernel/devices/ip_device.hpp new file mode 100644 index 000000000..0652176fc --- /dev/null +++ b/common/include/villas/kernel/devices/ip_device.hpp @@ -0,0 +1,34 @@ +/* IpDevice + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include + +namespace villas { +namespace kernel { +namespace devices { + +class IpDevice : public PlatformDevice { +public: +public: + static IpDevice from(const std::filesystem::path unsafe_path); + static bool is_path_valid(const std::filesystem::path unsafe_path); + +private: + IpDevice(const std::filesystem::path valid_path) : PlatformDevice(valid_path){}; + +public: + size_t addr() const; + std::string ip_name() const; +}; + +} // namespace devices +} // namespace kernel +} // namespace villas \ No newline at end of file diff --git a/common/lib/kernel/devices/ip_device.cpp b/common/lib/kernel/devices/ip_device.cpp new file mode 100644 index 000000000..5aafb0de9 --- /dev/null +++ b/common/lib/kernel/devices/ip_device.cpp @@ -0,0 +1,55 @@ +/* IpDevice + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#include + +using villas::kernel::devices::IpDevice; + +IpDevice IpDevice::from(const std::filesystem::path unsafe_path) { + if (!is_path_valid(unsafe_path)) + throw std::runtime_error( + "Path \"" + unsafe_path.u8string() + + "\" failed validation as IpDevicePath \"[adress in hex].[name]\". "); + return IpDevice(unsafe_path); +} + +std::string IpDevice::ip_name() const { + int pos = name().find('.'); + return name().substr(pos + 1); +} + +size_t IpDevice::addr() const { + size_t pos = name().find('.'); + std::string addr_hex = name().substr(0, pos); + + // convert from hex string to number + std::stringstream ss; + ss << std::hex << addr_hex; + size_t addr = 0; + ss >> addr; + + return addr; +} + +bool IpDevice::is_path_valid(const std::filesystem::path unsafe_path) { + // Split the string at last slash + int pos = unsafe_path.u8string().rfind('/'); + std::string assumed_device_name = unsafe_path.u8string().substr(pos + 1); + + // Match format of hexaddr.devicename + if (!std::regex_match(assumed_device_name, + std::regex(R"([0-9A-Fa-f]+\..*)"))) { + return false; + } + + return true; +} From abc56a94051993e92d4324fb2066b352d1de907d Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:37:21 +0200 Subject: [PATCH 07/50] add new classes to cmake Signed-off-by: Pascal Bauer --- common/lib/CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/common/lib/CMakeLists.txt b/common/lib/CMakeLists.txt index 116d786e6..5f16f67f4 100644 --- a/common/lib/CMakeLists.txt +++ b/common/lib/CMakeLists.txt @@ -15,6 +15,12 @@ add_library(villas-common SHARED cpuset.cpp dsp/pid.cpp hist.cpp + + kernel/devices/generic_driver.cpp + kernel/devices/ip_device.cpp + kernel/devices/platform_device.cpp + kernel/devices/utils.cpp + kernel/kernel.cpp kernel/rt.cpp list.cpp From 9b1524fe7bd8665be375c1196a43a7f8773a5640 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:42:35 +0200 Subject: [PATCH 08/50] fix generic driver header Signed-off-by: Pascal Bauer --- .../include/villas/kernel/devices/generic_driver.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/common/include/villas/kernel/devices/generic_driver.hpp b/common/include/villas/kernel/devices/generic_driver.hpp index 20d0c613e..8413b63ad 100644 --- a/common/include/villas/kernel/devices/generic_driver.hpp +++ b/common/include/villas/kernel/devices/generic_driver.hpp @@ -40,11 +40,11 @@ class GenericDriver : public Driver { : path(path), bind_path(bind_path), unbind_path(unbind_path){}; public: - virtual void attach(const Device &device) const = 0; - virtual void bind(const Device &device) const = 0; - virtual std::string name() const = 0; - virtual void override(const Device &device) const = 0; - virtual void unbind(const Device &device) const = 0; + void attach(const Device &device) const override; + void bind(const Device &device) const override; + std::string name() const override; + void override(const Device &device) const override; + void unbind(const Device &device) const override; }; } // namespace devices From 10e6aee8f0feca4290648d6348f7b8051105e90c Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 30 Aug 2024 13:37:53 +0200 Subject: [PATCH 09/50] cmake move drivers to linux section Signed-off-by: Pascal Bauer --- common/lib/CMakeLists.txt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/common/lib/CMakeLists.txt b/common/lib/CMakeLists.txt index 5f16f67f4..3aabe90ae 100644 --- a/common/lib/CMakeLists.txt +++ b/common/lib/CMakeLists.txt @@ -15,12 +15,6 @@ add_library(villas-common SHARED cpuset.cpp dsp/pid.cpp hist.cpp - - kernel/devices/generic_driver.cpp - kernel/devices/ip_device.cpp - kernel/devices/platform_device.cpp - kernel/devices/utils.cpp - kernel/kernel.cpp kernel/rt.cpp list.cpp @@ -45,7 +39,11 @@ endif() if(CMAKE_SYSTEM_NAME STREQUAL Linux) target_sources(villas-common PRIVATE + kernel/devices/generic_driver.cpp + kernel/devices/ip_device.cpp kernel/devices/pci_device.cpp + kernel/devices/platform_device.cpp + kernel/devices/utils.cpp kernel/vfio_device.cpp kernel/vfio_group.cpp kernel/vfio_container.cpp From ce8aa3f07ace5842eb576adfaa06e3547e8b47ec Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 30 Aug 2024 13:02:04 +0000 Subject: [PATCH 10/50] remove duplicate public Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/ip_device.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/common/include/villas/kernel/devices/ip_device.hpp b/common/include/villas/kernel/devices/ip_device.hpp index 0652176fc..36734abaa 100644 --- a/common/include/villas/kernel/devices/ip_device.hpp +++ b/common/include/villas/kernel/devices/ip_device.hpp @@ -16,7 +16,6 @@ namespace kernel { namespace devices { class IpDevice : public PlatformDevice { -public: public: static IpDevice from(const std::filesystem::path unsafe_path); static bool is_path_valid(const std::filesystem::path unsafe_path); @@ -31,4 +30,4 @@ class IpDevice : public PlatformDevice { } // namespace devices } // namespace kernel -} // namespace villas \ No newline at end of file +} // namespace villas From 2357122446451c977906d33f8a5ac275c952e336 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 30 Aug 2024 15:30:31 +0200 Subject: [PATCH 11/50] delete default comnstructor Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/ip_device.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/common/include/villas/kernel/devices/ip_device.hpp b/common/include/villas/kernel/devices/ip_device.hpp index 36734abaa..3dcae0504 100644 --- a/common/include/villas/kernel/devices/ip_device.hpp +++ b/common/include/villas/kernel/devices/ip_device.hpp @@ -21,6 +21,7 @@ class IpDevice : public PlatformDevice { static bool is_path_valid(const std::filesystem::path unsafe_path); private: + IpDevice() = delete; IpDevice(const std::filesystem::path valid_path) : PlatformDevice(valid_path){}; public: From 0c270c54adae8fc2dcfa12754e778d7b4415f30a Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 30 Aug 2024 15:55:07 +0200 Subject: [PATCH 12/50] formatting Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/ip_device.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/include/villas/kernel/devices/ip_device.hpp b/common/include/villas/kernel/devices/ip_device.hpp index 3dcae0504..b992e7fb5 100644 --- a/common/include/villas/kernel/devices/ip_device.hpp +++ b/common/include/villas/kernel/devices/ip_device.hpp @@ -22,7 +22,8 @@ class IpDevice : public PlatformDevice { private: IpDevice() = delete; - IpDevice(const std::filesystem::path valid_path) : PlatformDevice(valid_path){}; + IpDevice(const std::filesystem::path valid_path) + : PlatformDevice(valid_path){}; public: size_t addr() const; From 46065f3ab309a114873d94b314dae75b1d66848c Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 30 Aug 2024 15:59:11 +0200 Subject: [PATCH 13/50] use filename() Signed-off-by: Pascal Bauer --- common/lib/kernel/devices/ip_device.cpp | 4 +--- common/lib/kernel/devices/platform_device.cpp | 5 +---- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/common/lib/kernel/devices/ip_device.cpp b/common/lib/kernel/devices/ip_device.cpp index 5aafb0de9..7a6a4a3ee 100644 --- a/common/lib/kernel/devices/ip_device.cpp +++ b/common/lib/kernel/devices/ip_device.cpp @@ -41,9 +41,7 @@ size_t IpDevice::addr() const { } bool IpDevice::is_path_valid(const std::filesystem::path unsafe_path) { - // Split the string at last slash - int pos = unsafe_path.u8string().rfind('/'); - std::string assumed_device_name = unsafe_path.u8string().substr(pos + 1); + std::string assumed_device_name = unsafe_path.filename(); // Match format of hexaddr.devicename if (!std::regex_match(assumed_device_name, diff --git a/common/lib/kernel/devices/platform_device.cpp b/common/lib/kernel/devices/platform_device.cpp index 28b6c4796..4c3e5258f 100644 --- a/common/lib/kernel/devices/platform_device.cpp +++ b/common/lib/kernel/devices/platform_device.cpp @@ -48,7 +48,4 @@ std::filesystem::path PlatformDevice::override_path() const { return this->m_override_path; } -std::string PlatformDevice::name() const { - size_t pos = this->m_path.u8string().rfind('/'); - return this->m_path.u8string().substr(pos + 1); -} \ No newline at end of file +std::string PlatformDevice::name() const { return this->m_path.filename(); } \ No newline at end of file From 6e7c9c61d9f05c633af9c3a672a6dd9143abc6bf Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Wed, 4 Sep 2024 18:19:12 +0200 Subject: [PATCH 14/50] cleanup code Signed-off-by: Pascal Bauer --- common/lib/kernel/devices/platform_device.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/common/lib/kernel/devices/platform_device.cpp b/common/lib/kernel/devices/platform_device.cpp index 4c3e5258f..d8b128ad3 100644 --- a/common/lib/kernel/devices/platform_device.cpp +++ b/common/lib/kernel/devices/platform_device.cpp @@ -29,9 +29,7 @@ std::optional PlatformDevice::iommu_group() const { std::filesystem::path symlink = std::filesystem::path(this->m_path.u8string() + "/iommu_group"); - std::filesystem::path link; - link = std::filesystem::read_symlink(symlink); - + std::filesystem::path link = std::filesystem::read_symlink(symlink); std::string delimiter = "iommu_groups/"; int pos = link.u8string().find(delimiter); int iommu_group = std::stoi(link.u8string().substr(pos + delimiter.length())); From 80d794223c617cdba8cfabf137b697e3dba75102 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Mon, 23 Sep 2024 17:12:16 +0200 Subject: [PATCH 15/50] Refactor: move device utility functions to villas util Signed-off-by: Pascal Bauer --- common/include/villas/utils.hpp | 4 ++ common/lib/CMakeLists.txt | 1 - common/lib/kernel/devices/generic_driver.cpp | 4 +- common/lib/kernel/devices/platform_device.cpp | 5 +- common/lib/kernel/devices/utils.cpp | 48 ------------------- common/lib/utils.cpp | 35 ++++++++++++++ 6 files changed, 44 insertions(+), 53 deletions(-) delete mode 100644 common/lib/kernel/devices/utils.cpp diff --git a/common/include/villas/utils.hpp b/common/include/villas/utils.hpp index a65fb6cba..6022ebd39 100644 --- a/common/include/villas/utils.hpp +++ b/common/include/villas/utils.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -211,6 +212,9 @@ template struct overloaded : Ts... { // explicit deduction guide (not needed as of C++20) template overloaded(Ts...) -> overloaded; +void write_to_file(std::string data, const std::filesystem::path file); +std::vector read_names_in_directory(const std::string &name); + namespace base64 { using byte = std::uint8_t; diff --git a/common/lib/CMakeLists.txt b/common/lib/CMakeLists.txt index 3aabe90ae..4c43567f0 100644 --- a/common/lib/CMakeLists.txt +++ b/common/lib/CMakeLists.txt @@ -43,7 +43,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux) kernel/devices/ip_device.cpp kernel/devices/pci_device.cpp kernel/devices/platform_device.cpp - kernel/devices/utils.cpp kernel/vfio_device.cpp kernel/vfio_group.cpp kernel/vfio_container.cpp diff --git a/common/lib/kernel/devices/generic_driver.cpp b/common/lib/kernel/devices/generic_driver.cpp index 9e0de6d98..a79ac23b3 100644 --- a/common/lib/kernel/devices/generic_driver.cpp +++ b/common/lib/kernel/devices/generic_driver.cpp @@ -9,10 +9,10 @@ #include #include -#include +#include using villas::kernel::devices::Device, villas::kernel::devices::GenericDriver; -using villas::kernel::devices::utils::write_to_file; +using villas::utils::write_to_file; void GenericDriver::attach(const Device &device) const { if (device.driver().has_value()) { diff --git a/common/lib/kernel/devices/platform_device.cpp b/common/lib/kernel/devices/platform_device.cpp index d8b128ad3..f0834887b 100644 --- a/common/lib/kernel/devices/platform_device.cpp +++ b/common/lib/kernel/devices/platform_device.cpp @@ -6,12 +6,13 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include #include +#include +#include using villas::kernel::devices::Driver, villas::kernel::devices::GenericDriver; using villas::kernel::devices::PlatformDevice; -using villas::kernel::devices::utils::write_to_file; +using villas::utils::write_to_file; std::optional> PlatformDevice::driver() const { std::filesystem::path driver_symlink = diff --git a/common/lib/kernel/devices/utils.cpp b/common/lib/kernel/devices/utils.cpp deleted file mode 100644 index 16fb5e387..000000000 --- a/common/lib/kernel/devices/utils.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* Utils - * - * Author: Pascal Bauer - * - * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include -#include -#include - -namespace utils = villas::kernel::devices::utils; - -void utils::write_to_file(std::string data, const std::filesystem::path file) { - villas::Log::get("Filewriter")->debug("{} > {}", data, file.u8string()); - std::ofstream outputFile(file.u8string()); - - if (outputFile.is_open()) { - // Write to file - outputFile << data; - outputFile.close(); - } else { - throw std::filesystem::filesystem_error("Cannot open outputfile", - std::error_code()); - } -} - -std::vector -utils::read_names_in_directory(const std::string &name) { - DIR *directory = opendir(name.c_str()); - - struct dirent *dp; - std::vector names; - dp = readdir(directory); - while (dp != NULL) { - auto name = std::string(dp->d_name); - if (name != "." && name != "..") { - names.push_back(name); - } - dp = readdir(directory); - } - closedir(directory); - return names; -} \ No newline at end of file diff --git a/common/lib/utils.cpp b/common/lib/utils.cpp index f495d8121..4735435f8 100644 --- a/common/lib/utils.cpp +++ b/common/lib/utils.cpp @@ -17,7 +17,11 @@ #include #include #include +#include #include +#include +#include +#include #include #include @@ -351,5 +355,36 @@ bool isPrivileged() { return true; } +void write_to_file(std::string data, const std::filesystem::path file) { + villas::Log::get("Filewriter")->debug("{} > {}", data, file.u8string()); + std::ofstream outputFile(file.u8string()); + + if (outputFile.is_open()) { + // Write to file + outputFile << data; + outputFile.close(); + } else { + throw std::filesystem::filesystem_error("Cannot open outputfile", + std::error_code()); + } +} + +std::vector read_names_in_directory(const std::string &name) { + DIR *directory = opendir(name.c_str()); + + struct dirent *dp; + std::vector names; + dp = readdir(directory); + while (dp != NULL) { + auto name = std::string(dp->d_name); + if (name != "." && name != "..") { + names.push_back(name); + } + dp = readdir(directory); + } + closedir(directory); + return names; +} + } // namespace utils } // namespace villas From 9ac5cd67455efd7007c9fdc44690a7917dd4c83b Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Mon, 23 Sep 2024 17:23:20 +0200 Subject: [PATCH 16/50] update device description Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/device.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/include/villas/kernel/devices/device.hpp b/common/include/villas/kernel/devices/device.hpp index 1486638e1..18b914585 100644 --- a/common/include/villas/kernel/devices/device.hpp +++ b/common/include/villas/kernel/devices/device.hpp @@ -1,4 +1,4 @@ -/* Device +/* Interface for Linux/Unix device drivers * * Author: Pascal Bauer * From 1f9e5c471313380a8e160a01a5a7b823fcbd56cc Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Tue, 24 Sep 2024 08:58:44 +0200 Subject: [PATCH 17/50] format comment Signed-off-by: Pascal Bauer --- common/lib/kernel/devices/ip_device.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/lib/kernel/devices/ip_device.cpp b/common/lib/kernel/devices/ip_device.cpp index 7a6a4a3ee..9b13692d8 100644 --- a/common/lib/kernel/devices/ip_device.cpp +++ b/common/lib/kernel/devices/ip_device.cpp @@ -31,7 +31,7 @@ size_t IpDevice::addr() const { size_t pos = name().find('.'); std::string addr_hex = name().substr(0, pos); - // convert from hex string to number + // Convert from hex-string to number std::stringstream ss; ss << std::hex << addr_hex; size_t addr = 0; From ffbdcf54a03f9b475cd46892b405d3ea8bb5e567 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Tue, 24 Sep 2024 09:01:25 +0200 Subject: [PATCH 18/50] Update comments Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/device.hpp | 2 +- common/include/villas/kernel/devices/driver.hpp | 2 +- common/include/villas/kernel/devices/ip_device.hpp | 4 ++-- common/include/villas/kernel/devices/platform_device.hpp | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/common/include/villas/kernel/devices/device.hpp b/common/include/villas/kernel/devices/device.hpp index 18b914585..896cab042 100644 --- a/common/include/villas/kernel/devices/device.hpp +++ b/common/include/villas/kernel/devices/device.hpp @@ -1,4 +1,4 @@ -/* Interface for Linux/Unix device drivers +/* Interface for Linux/Unix devices. * * Author: Pascal Bauer * diff --git a/common/include/villas/kernel/devices/driver.hpp b/common/include/villas/kernel/devices/driver.hpp index 8c58bb726..9f0a023a9 100644 --- a/common/include/villas/kernel/devices/driver.hpp +++ b/common/include/villas/kernel/devices/driver.hpp @@ -1,4 +1,4 @@ -/* Driver +/* Interface for Linux/Unix device drivers * * Author: Pascal Bauer * diff --git a/common/include/villas/kernel/devices/ip_device.hpp b/common/include/villas/kernel/devices/ip_device.hpp index b992e7fb5..14aca5314 100644 --- a/common/include/villas/kernel/devices/ip_device.hpp +++ b/common/include/villas/kernel/devices/ip_device.hpp @@ -1,4 +1,4 @@ -/* IpDevice +/* IpDevice: Linux/Unix device which represents an IP component of a fpga * * Author: Pascal Bauer * @@ -22,7 +22,7 @@ class IpDevice : public PlatformDevice { private: IpDevice() = delete; - IpDevice(const std::filesystem::path valid_path) + IpDevice(const std::filesystem::path valid_path) //! Dont allow unvalidated paths : PlatformDevice(valid_path){}; public: diff --git a/common/include/villas/kernel/devices/platform_device.hpp b/common/include/villas/kernel/devices/platform_device.hpp index 4cef2ceb4..a2018b4a4 100644 --- a/common/include/villas/kernel/devices/platform_device.hpp +++ b/common/include/villas/kernel/devices/platform_device.hpp @@ -1,4 +1,4 @@ -/* Platform Device +/* Platform Device: Platform bus based Linux/Unix device. * * Author: Pascal Bauer * From eeb56f47ae0e649aaeab1b82617cbd2b2f26beaf Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Tue, 24 Sep 2024 09:06:17 +0200 Subject: [PATCH 19/50] remove comment Signed-off-by: Pascal Bauer --- common/lib/utils.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/common/lib/utils.cpp b/common/lib/utils.cpp index 4735435f8..2e7356039 100644 --- a/common/lib/utils.cpp +++ b/common/lib/utils.cpp @@ -360,7 +360,6 @@ void write_to_file(std::string data, const std::filesystem::path file) { std::ofstream outputFile(file.u8string()); if (outputFile.is_open()) { - // Write to file outputFile << data; outputFile.close(); } else { From 1f19e42416f08dd9422e80659c72a62a20c737cc Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Wed, 25 Sep 2024 15:40:50 +0200 Subject: [PATCH 20/50] Use villas exception Signed-off-by: Pascal Bauer --- common/lib/kernel/devices/ip_device.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/common/lib/kernel/devices/ip_device.cpp b/common/lib/kernel/devices/ip_device.cpp index 9b13692d8..0b850e654 100644 --- a/common/lib/kernel/devices/ip_device.cpp +++ b/common/lib/kernel/devices/ip_device.cpp @@ -10,15 +10,16 @@ #include #include +#include #include using villas::kernel::devices::IpDevice; IpDevice IpDevice::from(const std::filesystem::path unsafe_path) { if (!is_path_valid(unsafe_path)) - throw std::runtime_error( - "Path \"" + unsafe_path.u8string() + - "\" failed validation as IpDevicePath \"[adress in hex].[name]\". "); + throw RuntimeError( + "Path {} failed validation as IpDevicePath [adress in hex].[name] ", + unsafe_path.u8string()); return IpDevice(unsafe_path); } From 43e267a6d21de4d09791ae6138fce96fe2a5607e Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sun, 6 Oct 2024 21:59:06 +0200 Subject: [PATCH 21/50] remove dead header Signed-off-by: Pascal Bauer --- .../villas/kernel/devices/platform_device.hpp | 1 - .../include/villas/kernel/devices/utils.hpp | 26 ------------------- 2 files changed, 27 deletions(-) delete mode 100644 common/include/villas/kernel/devices/utils.hpp diff --git a/common/include/villas/kernel/devices/platform_device.hpp b/common/include/villas/kernel/devices/platform_device.hpp index a2018b4a4..bbe182548 100644 --- a/common/include/villas/kernel/devices/platform_device.hpp +++ b/common/include/villas/kernel/devices/platform_device.hpp @@ -11,7 +11,6 @@ #include #include #include -#include namespace villas { namespace kernel { diff --git a/common/include/villas/kernel/devices/utils.hpp b/common/include/villas/kernel/devices/utils.hpp deleted file mode 100644 index 9d4984892..000000000 --- a/common/include/villas/kernel/devices/utils.hpp +++ /dev/null @@ -1,26 +0,0 @@ -/* Utils - * - * Author: Pascal Bauer - * - * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#include -#include -#include - -namespace villas { -namespace kernel { -namespace devices { -namespace utils { - -void write_to_file(std::string data, const std::filesystem::path file); -std::vector read_names_in_directory(const std::string &name); - -} // namespace utils -} // namespace devices -} // namespace kernel -} // namespace villas From 270acd05dc40c20f74d45f7ce3f3c4caf1eb2442 Mon Sep 17 00:00:00 2001 From: IgnoreWarnings <119685519+IgnoreWarnings@users.noreply.github.com> Date: Wed, 9 Oct 2024 07:57:52 -0400 Subject: [PATCH 22/50] Update comment Co-authored-by: Steffen Vogel Signed-off-by: IgnoreWarnings <119685519+IgnoreWarnings@users.noreply.github.com> Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/ip_device.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/include/villas/kernel/devices/ip_device.hpp b/common/include/villas/kernel/devices/ip_device.hpp index 14aca5314..351f6cae4 100644 --- a/common/include/villas/kernel/devices/ip_device.hpp +++ b/common/include/villas/kernel/devices/ip_device.hpp @@ -1,4 +1,4 @@ -/* IpDevice: Linux/Unix device which represents an IP component of a fpga +/* IpDevice: Linux/Unix device which represents an IP component of a FPGA. * * Author: Pascal Bauer * From 87a2372204a267879543f4ec1c7b8da92710865d Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Wed, 9 Oct 2024 16:39:49 +0200 Subject: [PATCH 23/50] Use std::filestytem Signed-off-by: Pascal Bauer --- common/lib/utils.cpp | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/common/lib/utils.cpp b/common/lib/utils.cpp index 2e7356039..58f7a4773 100644 --- a/common/lib/utils.cpp +++ b/common/lib/utils.cpp @@ -368,20 +368,12 @@ void write_to_file(std::string data, const std::filesystem::path file) { } } -std::vector read_names_in_directory(const std::string &name) { - DIR *directory = opendir(name.c_str()); - - struct dirent *dp; +std::vector +read_names_in_directory(const std::filesystem::path &dir_path) { std::vector names; - dp = readdir(directory); - while (dp != NULL) { - auto name = std::string(dp->d_name); - if (name != "." && name != "..") { - names.push_back(name); - } - dp = readdir(directory); + for (auto const &dir_entry : std::filesystem::directory_iterator{dir_path}) { + names.push_back(dir_entry.path().filename()); } - closedir(directory); return names; } From ff0074701062e75a7cb67e1cb6e349be14eb6404 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Wed, 9 Oct 2024 17:17:46 +0200 Subject: [PATCH 24/50] fix read names in directory method Signed-off-by: Pascal Bauer --- common/include/villas/utils.hpp | 2 +- common/lib/utils.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/common/include/villas/utils.hpp b/common/include/villas/utils.hpp index 6022ebd39..18445d891 100644 --- a/common/include/villas/utils.hpp +++ b/common/include/villas/utils.hpp @@ -213,7 +213,7 @@ template struct overloaded : Ts... { template overloaded(Ts...) -> overloaded; void write_to_file(std::string data, const std::filesystem::path file); -std::vector read_names_in_directory(const std::string &name); +std::vector read_names_in_directory(const std::filesystem::path &directory); namespace base64 { diff --git a/common/lib/utils.cpp b/common/lib/utils.cpp index 58f7a4773..b137bf25e 100644 --- a/common/lib/utils.cpp +++ b/common/lib/utils.cpp @@ -369,9 +369,9 @@ void write_to_file(std::string data, const std::filesystem::path file) { } std::vector -read_names_in_directory(const std::filesystem::path &dir_path) { +read_names_in_directory(const std::filesystem::path &directory) { std::vector names; - for (auto const &dir_entry : std::filesystem::directory_iterator{dir_path}) { + for (auto const &dir_entry : std::filesystem::directory_iterator{directory}) { names.push_back(dir_entry.path().filename()); } return names; From 486877ea09261adcbbbdc21205a98d432892f4a2 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Thu, 10 Oct 2024 14:28:09 +0200 Subject: [PATCH 25/50] add . at end of comment Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/driver.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/include/villas/kernel/devices/driver.hpp b/common/include/villas/kernel/devices/driver.hpp index 9f0a023a9..c8dca2b8f 100644 --- a/common/include/villas/kernel/devices/driver.hpp +++ b/common/include/villas/kernel/devices/driver.hpp @@ -1,4 +1,4 @@ -/* Interface for Linux/Unix device drivers +/* Interface for Linux/Unix device drivers. * * Author: Pascal Bauer * From 5a48e38aee66e4bcdc4aa368f4ee38e3aaf75e38 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 11 Oct 2024 14:44:24 +0200 Subject: [PATCH 26/50] edit driver comment Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/driver.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/include/villas/kernel/devices/driver.hpp b/common/include/villas/kernel/devices/driver.hpp index c8dca2b8f..97a589d05 100644 --- a/common/include/villas/kernel/devices/driver.hpp +++ b/common/include/villas/kernel/devices/driver.hpp @@ -1,4 +1,4 @@ -/* Interface for Linux/Unix device drivers. +/* Interface for device drivers. OS/platform independend. * * Author: Pascal Bauer * From ca2715ddbdae232ad952f286c701d3c9c7696db0 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 11 Oct 2024 14:49:32 +0200 Subject: [PATCH 27/50] rename GenericDriver to LinuxDriver Signed-off-by: Pascal Bauer --- .../villas/kernel/devices/generic_driver.hpp | 16 ++++++++-------- common/lib/kernel/devices/generic_driver.cpp | 14 +++++++------- common/lib/kernel/devices/platform_device.cpp | 4 ++-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/common/include/villas/kernel/devices/generic_driver.hpp b/common/include/villas/kernel/devices/generic_driver.hpp index 8413b63ad..71dbce8d7 100644 --- a/common/include/villas/kernel/devices/generic_driver.hpp +++ b/common/include/villas/kernel/devices/generic_driver.hpp @@ -1,4 +1,4 @@ -/* GenericDriver - Works for standard Linux drivers +/* Implementation of driver interface for Linux/Unix based operation system drivers. * * Author: Pascal Bauer * @@ -17,7 +17,7 @@ namespace villas { namespace kernel { namespace devices { -class GenericDriver : public Driver { +class LinuxDriver : public Driver { private: static constexpr char BIND_DEFAULT[] = "bind"; static constexpr char UNBIND_DEFAULT[] = "unbind"; @@ -30,13 +30,13 @@ class GenericDriver : public Driver { const std::filesystem::path unbind_path; public: - GenericDriver(const std::filesystem::path path) - : GenericDriver(path, path / std::filesystem::path(BIND_DEFAULT), - path / std::filesystem::path(UNBIND_DEFAULT)){}; + LinuxDriver(const std::filesystem::path path) + : LinuxDriver(path, path / std::filesystem::path(BIND_DEFAULT), + path / std::filesystem::path(UNBIND_DEFAULT)){}; - GenericDriver(const std::filesystem::path path, - const std::filesystem::path bind_path, - const std::filesystem::path unbind_path) + LinuxDriver(const std::filesystem::path path, + const std::filesystem::path bind_path, + const std::filesystem::path unbind_path) : path(path), bind_path(bind_path), unbind_path(unbind_path){}; public: diff --git a/common/lib/kernel/devices/generic_driver.cpp b/common/lib/kernel/devices/generic_driver.cpp index a79ac23b3..68afa4368 100644 --- a/common/lib/kernel/devices/generic_driver.cpp +++ b/common/lib/kernel/devices/generic_driver.cpp @@ -1,4 +1,4 @@ -/* GenericDriver +/* LinuxDriver * * Author: Pascal Bauer * @@ -11,10 +11,10 @@ #include #include -using villas::kernel::devices::Device, villas::kernel::devices::GenericDriver; +using villas::kernel::devices::Device, villas::kernel::devices::LinuxDriver; using villas::utils::write_to_file; -void GenericDriver::attach(const Device &device) const { +void LinuxDriver::attach(const Device &device) const { if (device.driver().has_value()) { device.driver().value()->unbind(device); } @@ -22,19 +22,19 @@ void GenericDriver::attach(const Device &device) const { device.probe(); } -void GenericDriver::bind(const Device &device) const { +void LinuxDriver::bind(const Device &device) const { write_to_file(device.name(), this->bind_path); } -std::string GenericDriver::name() const { +std::string LinuxDriver::name() const { size_t pos = path.u8string().rfind('/'); return path.u8string().substr(pos + 1); } -void GenericDriver::override(const Device &device) const { +void LinuxDriver::override(const Device &device) const { write_to_file(this->name(), device.override_path()); } -void GenericDriver::unbind(const Device &device) const { +void LinuxDriver::unbind(const Device &device) const { write_to_file(device.name(), this->unbind_path); } diff --git a/common/lib/kernel/devices/platform_device.cpp b/common/lib/kernel/devices/platform_device.cpp index f0834887b..9cbbdb929 100644 --- a/common/lib/kernel/devices/platform_device.cpp +++ b/common/lib/kernel/devices/platform_device.cpp @@ -10,7 +10,7 @@ #include #include -using villas::kernel::devices::Driver, villas::kernel::devices::GenericDriver; +using villas::kernel::devices::Driver, villas::kernel::devices::LinuxDriver; using villas::kernel::devices::PlatformDevice; using villas::utils::write_to_file; @@ -23,7 +23,7 @@ std::optional> PlatformDevice::driver() const { std::filesystem::path driver_path = std::filesystem::canonical(driver_symlink); - return std::make_optional(std::make_unique(driver_path)); + return std::make_optional(std::make_unique(driver_path)); } std::optional PlatformDevice::iommu_group() const { From e945a9ee4153de0da42ece5b4c1f76e55714d5a9 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 11 Oct 2024 14:57:47 +0200 Subject: [PATCH 28/50] rename generic_driver to linux_driver Signed-off-by: Pascal Bauer --- .../kernel/devices/{generic_driver.hpp => linux_driver.hpp} | 0 common/lib/CMakeLists.txt | 2 +- .../lib/kernel/devices/{generic_driver.cpp => linux_driver.cpp} | 2 +- common/lib/kernel/devices/platform_device.cpp | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename common/include/villas/kernel/devices/{generic_driver.hpp => linux_driver.hpp} (100%) rename common/lib/kernel/devices/{generic_driver.cpp => linux_driver.cpp} (95%) diff --git a/common/include/villas/kernel/devices/generic_driver.hpp b/common/include/villas/kernel/devices/linux_driver.hpp similarity index 100% rename from common/include/villas/kernel/devices/generic_driver.hpp rename to common/include/villas/kernel/devices/linux_driver.hpp diff --git a/common/lib/CMakeLists.txt b/common/lib/CMakeLists.txt index 4c43567f0..74581897c 100644 --- a/common/lib/CMakeLists.txt +++ b/common/lib/CMakeLists.txt @@ -39,8 +39,8 @@ endif() if(CMAKE_SYSTEM_NAME STREQUAL Linux) target_sources(villas-common PRIVATE - kernel/devices/generic_driver.cpp kernel/devices/ip_device.cpp + kernel/devices/linux_driver.cpp kernel/devices/pci_device.cpp kernel/devices/platform_device.cpp kernel/vfio_device.cpp diff --git a/common/lib/kernel/devices/generic_driver.cpp b/common/lib/kernel/devices/linux_driver.cpp similarity index 95% rename from common/lib/kernel/devices/generic_driver.cpp rename to common/lib/kernel/devices/linux_driver.cpp index 68afa4368..0cbc39b18 100644 --- a/common/lib/kernel/devices/generic_driver.cpp +++ b/common/lib/kernel/devices/linux_driver.cpp @@ -6,7 +6,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include +#include #include #include diff --git a/common/lib/kernel/devices/platform_device.cpp b/common/lib/kernel/devices/platform_device.cpp index 9cbbdb929..e899a2d75 100644 --- a/common/lib/kernel/devices/platform_device.cpp +++ b/common/lib/kernel/devices/platform_device.cpp @@ -6,7 +6,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include +#include #include #include From 51eaea3708d664e1e1b786eca5bd852e4bef8368 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 11 Oct 2024 14:58:12 +0200 Subject: [PATCH 29/50] update driver comment Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/driver.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/include/villas/kernel/devices/driver.hpp b/common/include/villas/kernel/devices/driver.hpp index 97a589d05..7578f1e66 100644 --- a/common/include/villas/kernel/devices/driver.hpp +++ b/common/include/villas/kernel/devices/driver.hpp @@ -1,4 +1,5 @@ -/* Interface for device drivers. OS/platform independend. +/* Interface for device drivers. OS/platform independend. + * Implemented for Linux/Unix drivers in linux_driver.hpp * * Author: Pascal Bauer * From fadb14cd1ee7cd52bc3574216ac0466be0f91749 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 23 Aug 2024 18:16:36 +0200 Subject: [PATCH 30/50] initial commit Signed-off-by: Pascal Bauer --- .../villas/kernel/devices/platform_driver.hpp | 53 +++++++++++++++++++ common/lib/kernel/devices/platform_driver.cpp | 39 ++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 common/include/villas/kernel/devices/platform_driver.hpp create mode 100644 common/lib/kernel/devices/platform_driver.cpp diff --git a/common/include/villas/kernel/devices/platform_driver.hpp b/common/include/villas/kernel/devices/platform_driver.hpp new file mode 100644 index 000000000..0a3f89f57 --- /dev/null +++ b/common/include/villas/kernel/devices/platform_driver.hpp @@ -0,0 +1,53 @@ +/* PlatformDriver + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include + +namespace villas { +namespace kernel { +namespace devices { + +class PlatformDevice; + +class PlatformDriver { +private: + static constexpr char BIND_DEFAULT[] = "bind"; + static constexpr char UNBIND_DEFAULT[] = "unbind"; + +public: + const std::filesystem::path path; + +private: + const std::filesystem::path bind_path; + const std::filesystem::path unbind_path; + +public: + PlatformDriver(const std::filesystem::path path) + : PlatformDriver(path, path / std::filesystem::path(BIND_DEFAULT), + path / std::filesystem::path(UNBIND_DEFAULT)){}; + + PlatformDriver(const std::filesystem::path path, + const std::filesystem::path bind_path, + const std::filesystem::path unbind_path) + : path(path), bind_path(bind_path), unbind_path(unbind_path){}; + + std::string name() const; + void attach(const PlatformDevice &device) const; + +private: + void bind(const PlatformDevice &device) const; + void unbind(const PlatformDevice &device) const; + void override(const PlatformDevice &device) const; +}; + +} // namespace devices +} // namespace kernel +} // namespace villas \ No newline at end of file diff --git a/common/lib/kernel/devices/platform_driver.cpp b/common/lib/kernel/devices/platform_driver.cpp new file mode 100644 index 000000000..28c3ed1f1 --- /dev/null +++ b/common/lib/kernel/devices/platform_driver.cpp @@ -0,0 +1,39 @@ +/* PlatformDriver + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +using villas::kernel::devices::PlatformDevice, + villas::kernel::devices::PlatformDriver; +using villas::kernel::devices::utils::write_to_file; + +std::string PlatformDriver::name() const { + size_t pos = path.u8string().rfind('/'); + return path.u8string().substr(pos + 1); +} + +void PlatformDriver::bind(const PlatformDevice &device) const { + write_to_file(device.name(), this->bind_path); +}; + +void PlatformDriver::unbind(const PlatformDevice &device) const { + write_to_file(device.name(), this->unbind_path); +}; + +void PlatformDriver::override(const PlatformDevice &device) const { + write_to_file(this->name(), device.override_path()); +}; + +void PlatformDriver::attach(const PlatformDevice &device) const { + if (device.driver().has_value()) { + device.driver().value().unbind(device); + } + this->override(device); + device.probe(); +}; \ No newline at end of file From ae3ab18488bdddfbba86ea578dd4dc98de83ec8f Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 23 Aug 2024 19:22:07 +0200 Subject: [PATCH 31/50] Driver Interface Signed-off-by: Pascal Bauer --- .../villas/kernel/devices/platform_driver.hpp | 16 ++++++++-------- common/lib/kernel/devices/platform_driver.cpp | 10 +++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/common/include/villas/kernel/devices/platform_driver.hpp b/common/include/villas/kernel/devices/platform_driver.hpp index 0a3f89f57..8a4979f75 100644 --- a/common/include/villas/kernel/devices/platform_driver.hpp +++ b/common/include/villas/kernel/devices/platform_driver.hpp @@ -10,6 +10,7 @@ #include #include +#include namespace villas { namespace kernel { @@ -17,7 +18,7 @@ namespace devices { class PlatformDevice; -class PlatformDriver { +class PlatformDriver : public Driver { private: static constexpr char BIND_DEFAULT[] = "bind"; static constexpr char UNBIND_DEFAULT[] = "unbind"; @@ -39,13 +40,12 @@ class PlatformDriver { const std::filesystem::path unbind_path) : path(path), bind_path(bind_path), unbind_path(unbind_path){}; - std::string name() const; - void attach(const PlatformDevice &device) const; - -private: - void bind(const PlatformDevice &device) const; - void unbind(const PlatformDevice &device) const; - void override(const PlatformDevice &device) const; +public: + std::string name() const override; + void attach(const Device &device) const override; + void bind(const Device &device) const override; + void unbind(const Device &device) const override; + void override(const Device &device) const override; }; } // namespace devices diff --git a/common/lib/kernel/devices/platform_driver.cpp b/common/lib/kernel/devices/platform_driver.cpp index 28c3ed1f1..467d00bb5 100644 --- a/common/lib/kernel/devices/platform_driver.cpp +++ b/common/lib/kernel/devices/platform_driver.cpp @@ -18,21 +18,21 @@ std::string PlatformDriver::name() const { return path.u8string().substr(pos + 1); } -void PlatformDriver::bind(const PlatformDevice &device) const { +void PlatformDriver::bind(const Device &device) const { write_to_file(device.name(), this->bind_path); }; -void PlatformDriver::unbind(const PlatformDevice &device) const { +void PlatformDriver::unbind(const Device &device) const { write_to_file(device.name(), this->unbind_path); }; -void PlatformDriver::override(const PlatformDevice &device) const { +void PlatformDriver::override(const Device &device) const { write_to_file(this->name(), device.override_path()); }; -void PlatformDriver::attach(const PlatformDevice &device) const { +void PlatformDriver::attach(const Device &device) const { if (device.driver().has_value()) { - device.driver().value().unbind(device); + device.driver().value()->unbind(device); } this->override(device); device.probe(); From 3659225e203ad559a0f8795564aee5c6f6d673cd Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 23 Aug 2024 19:28:38 +0200 Subject: [PATCH 32/50] fix broken includes Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/platform_driver.hpp | 1 + common/lib/kernel/devices/platform_driver.cpp | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/common/include/villas/kernel/devices/platform_driver.hpp b/common/include/villas/kernel/devices/platform_driver.hpp index 8a4979f75..0fa4350ff 100644 --- a/common/include/villas/kernel/devices/platform_driver.hpp +++ b/common/include/villas/kernel/devices/platform_driver.hpp @@ -9,6 +9,7 @@ #pragma once #include +#include #include #include diff --git a/common/lib/kernel/devices/platform_driver.cpp b/common/lib/kernel/devices/platform_driver.cpp index 467d00bb5..2b03025fe 100644 --- a/common/lib/kernel/devices/platform_driver.cpp +++ b/common/lib/kernel/devices/platform_driver.cpp @@ -6,11 +6,12 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include +#include + +#include #include -using villas::kernel::devices::PlatformDevice, - villas::kernel::devices::PlatformDriver; +using villas::kernel::devices::Device, villas::kernel::devices::PlatformDriver; using villas::kernel::devices::utils::write_to_file; std::string PlatformDriver::name() const { From 0400f1980c73e627fbb1b45b4b330af05118009e Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 23 Aug 2024 19:29:30 +0200 Subject: [PATCH 33/50] remove unused code Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/platform_driver.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/common/include/villas/kernel/devices/platform_driver.hpp b/common/include/villas/kernel/devices/platform_driver.hpp index 0fa4350ff..c0f4e4017 100644 --- a/common/include/villas/kernel/devices/platform_driver.hpp +++ b/common/include/villas/kernel/devices/platform_driver.hpp @@ -17,8 +17,6 @@ namespace villas { namespace kernel { namespace devices { -class PlatformDevice; - class PlatformDriver : public Driver { private: static constexpr char BIND_DEFAULT[] = "bind"; From 1f9d7d59fa774e950682e79b99fa007e8072b1f5 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 23 Aug 2024 19:34:14 +0200 Subject: [PATCH 34/50] rename Platform driver to GenericDriver Signed-off-by: Pascal Bauer --- .../villas/kernel/devices/platform_driver.hpp | 52 ------------------- common/lib/kernel/devices/platform_driver.cpp | 40 -------------- 2 files changed, 92 deletions(-) delete mode 100644 common/include/villas/kernel/devices/platform_driver.hpp delete mode 100644 common/lib/kernel/devices/platform_driver.cpp diff --git a/common/include/villas/kernel/devices/platform_driver.hpp b/common/include/villas/kernel/devices/platform_driver.hpp deleted file mode 100644 index c0f4e4017..000000000 --- a/common/include/villas/kernel/devices/platform_driver.hpp +++ /dev/null @@ -1,52 +0,0 @@ -/* PlatformDriver - * - * Author: Pascal Bauer - * - * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#include -#include -#include -#include - -namespace villas { -namespace kernel { -namespace devices { - -class PlatformDriver : public Driver { -private: - static constexpr char BIND_DEFAULT[] = "bind"; - static constexpr char UNBIND_DEFAULT[] = "unbind"; - -public: - const std::filesystem::path path; - -private: - const std::filesystem::path bind_path; - const std::filesystem::path unbind_path; - -public: - PlatformDriver(const std::filesystem::path path) - : PlatformDriver(path, path / std::filesystem::path(BIND_DEFAULT), - path / std::filesystem::path(UNBIND_DEFAULT)){}; - - PlatformDriver(const std::filesystem::path path, - const std::filesystem::path bind_path, - const std::filesystem::path unbind_path) - : path(path), bind_path(bind_path), unbind_path(unbind_path){}; - -public: - std::string name() const override; - void attach(const Device &device) const override; - void bind(const Device &device) const override; - void unbind(const Device &device) const override; - void override(const Device &device) const override; -}; - -} // namespace devices -} // namespace kernel -} // namespace villas \ No newline at end of file diff --git a/common/lib/kernel/devices/platform_driver.cpp b/common/lib/kernel/devices/platform_driver.cpp deleted file mode 100644 index 2b03025fe..000000000 --- a/common/lib/kernel/devices/platform_driver.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* PlatformDriver - * - * Author: Pascal Bauer - * - * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include -#include - -using villas::kernel::devices::Device, villas::kernel::devices::PlatformDriver; -using villas::kernel::devices::utils::write_to_file; - -std::string PlatformDriver::name() const { - size_t pos = path.u8string().rfind('/'); - return path.u8string().substr(pos + 1); -} - -void PlatformDriver::bind(const Device &device) const { - write_to_file(device.name(), this->bind_path); -}; - -void PlatformDriver::unbind(const Device &device) const { - write_to_file(device.name(), this->unbind_path); -}; - -void PlatformDriver::override(const Device &device) const { - write_to_file(this->name(), device.override_path()); -}; - -void PlatformDriver::attach(const Device &device) const { - if (device.driver().has_value()) { - device.driver().value()->unbind(device); - } - this->override(device); - device.probe(); -}; \ No newline at end of file From b229d7c1a655423cd798f16605e05b6f8c63c917 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 23 Aug 2024 20:18:22 +0200 Subject: [PATCH 35/50] inherit from device interface Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/pci_device.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/common/include/villas/kernel/devices/pci_device.hpp b/common/include/villas/kernel/devices/pci_device.hpp index 65794644b..d7f457c04 100644 --- a/common/include/villas/kernel/devices/pci_device.hpp +++ b/common/include/villas/kernel/devices/pci_device.hpp @@ -15,6 +15,8 @@ #include +#include + namespace villas { namespace kernel { namespace devices { @@ -58,7 +60,7 @@ struct Region { unsigned long long flags; }; -class PciDevice { +class PciDevice : public Device { public: PciDevice(Id i, Slot s) : id(i), slot(s), log(Log::get("kernel:pci")) {} From ec3da8d5ae1a136d5fdc0f9773281edf92a74043 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 23 Aug 2024 20:38:25 +0200 Subject: [PATCH 36/50] implement driver() Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/pci_device.hpp | 4 ++-- common/lib/kernel/devices/pci_device.cpp | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/common/include/villas/kernel/devices/pci_device.hpp b/common/include/villas/kernel/devices/pci_device.hpp index d7f457c04..2b6185152 100644 --- a/common/include/villas/kernel/devices/pci_device.hpp +++ b/common/include/villas/kernel/devices/pci_device.hpp @@ -70,8 +70,8 @@ class PciDevice : public Device { bool operator==(const PciDevice &other); - // Get currently loaded driver for device - std::string getDriver() const; + // Implement device interface + std::optional> driver() const override; // Bind a new LKM to the PCI device bool attachDriver(const std::string &driver) const; diff --git a/common/lib/kernel/devices/pci_device.cpp b/common/lib/kernel/devices/pci_device.cpp index ef8a8b5d0..7a218b1a2 100644 --- a/common/lib/kernel/devices/pci_device.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -311,8 +312,7 @@ std::list PciDevice::getRegions() const { return regions; } - -std::string PciDevice::getDriver() const { +std::optional> PciDevice::driver() const { int ret; char sysfs[1024], syml[1024]; memset(syml, 0, sizeof(syml)); @@ -323,13 +323,15 @@ std::string PciDevice::getDriver() const { struct stat st; ret = stat(sysfs, &st); if (ret) - return ""; + return std::nullopt; ret = readlink(sysfs, syml, sizeof(syml)); if (ret < 0) throw SystemError("Failed to follow link: {}", sysfs); - return basename(syml); + auto driver = std::make_optional(std::make_unique( + "/sys/bus/pci/drivers/" + std::string(basename(syml)))); + return driver; } bool PciDevice::attachDriver(const std::string &driver) const { From 4772060f95da2fc04048e8af973cc66a9f90ebb4 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 23 Aug 2024 20:41:31 +0200 Subject: [PATCH 37/50] implement iommu_group Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/pci_device.hpp | 3 +-- common/lib/kernel/devices/pci_device.cpp | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/common/include/villas/kernel/devices/pci_device.hpp b/common/include/villas/kernel/devices/pci_device.hpp index 2b6185152..836670dd6 100644 --- a/common/include/villas/kernel/devices/pci_device.hpp +++ b/common/include/villas/kernel/devices/pci_device.hpp @@ -72,12 +72,11 @@ class PciDevice : public Device { // Implement device interface std::optional> driver() const override; + std::optional iommu_group() const override; // Bind a new LKM to the PCI device bool attachDriver(const std::string &driver) const; - // Return the IOMMU group of this PCI device or -1 if the device is not in a group - int getIommuGroup() const; std::list getRegions() const; diff --git a/common/lib/kernel/devices/pci_device.cpp b/common/lib/kernel/devices/pci_device.cpp index 7a218b1a2..1612e5d99 100644 --- a/common/lib/kernel/devices/pci_device.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -425,7 +425,7 @@ void PciDevice::writeBar(uint32_t addr, unsigned barNum) { file.write(reinterpret_cast(&addr), sizeof(addr)); } -int PciDevice::getIommuGroup() const { +std::optional PciDevice::iommu_group() const { int ret; char *group; @@ -439,11 +439,11 @@ int PciDevice::getIommuGroup() const { ret = readlink(sysfs, link, sizeof(link)); if (ret < 0) - return -1; + return std::nullopt; group = basename(link); - return atoi(group); + return std::make_optional(atoi(group)); } std::fstream PciDevice::openSysFs(const std::string &subPath, From 2bfccaacd1982086f69c5f3af4365603f2a073ab Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 23 Aug 2024 20:48:01 +0200 Subject: [PATCH 38/50] implement name Signed-off-by: Pascal Bauer --- .../include/villas/kernel/devices/pci_device.hpp | 1 + common/lib/kernel/devices/pci_device.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/common/include/villas/kernel/devices/pci_device.hpp b/common/include/villas/kernel/devices/pci_device.hpp index 836670dd6..36f16fba9 100644 --- a/common/include/villas/kernel/devices/pci_device.hpp +++ b/common/include/villas/kernel/devices/pci_device.hpp @@ -73,6 +73,7 @@ class PciDevice : public Device { // Implement device interface std::optional> driver() const override; std::optional iommu_group() const override; + std::string name() const override; // Bind a new LKM to the PCI device bool attachDriver(const std::string &driver) const; diff --git a/common/lib/kernel/devices/pci_device.cpp b/common/lib/kernel/devices/pci_device.cpp index 1612e5d99..2082e0a7c 100644 --- a/common/lib/kernel/devices/pci_device.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -459,3 +459,19 @@ std::fstream PciDevice::openSysFs(const std::string &subPath, return file; } + +// TODO: test +std::string PciDevice::name() const { + int ret; + char *group; + + // readlink() does not add a null terminator! + char link[1024] = {0}; + char sysfs[1024]; + + snprintf(sysfs, sizeof(sysfs), + "%04x:%02x:%02x.%x", + slot.domain, slot.bus, slot.device, slot.function); + + return std::string(sysfs); +} \ No newline at end of file From 56b2d69b510e55d458f38f3042efd6ed3a0e3fc5 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 23 Aug 2024 21:53:29 +0200 Subject: [PATCH 39/50] removed unused code Signed-off-by: Pascal Bauer --- common/lib/kernel/devices/pci_device.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/common/lib/kernel/devices/pci_device.cpp b/common/lib/kernel/devices/pci_device.cpp index 2082e0a7c..4b4223128 100644 --- a/common/lib/kernel/devices/pci_device.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -462,11 +462,6 @@ std::fstream PciDevice::openSysFs(const std::string &subPath, // TODO: test std::string PciDevice::name() const { - int ret; - char *group; - - // readlink() does not add a null terminator! - char link[1024] = {0}; char sysfs[1024]; snprintf(sysfs, sizeof(sysfs), From cac2e8990da07879affc07bc1f00af8297870e50 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:05:36 +0200 Subject: [PATCH 40/50] format Signed-off-by: Pascal Bauer --- common/lib/kernel/devices/pci_device.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/common/lib/kernel/devices/pci_device.cpp b/common/lib/kernel/devices/pci_device.cpp index 4b4223128..277e6b708 100644 --- a/common/lib/kernel/devices/pci_device.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -464,9 +464,8 @@ std::fstream PciDevice::openSysFs(const std::string &subPath, std::string PciDevice::name() const { char sysfs[1024]; - snprintf(sysfs, sizeof(sysfs), - "%04x:%02x:%02x.%x", - slot.domain, slot.bus, slot.device, slot.function); + snprintf(sysfs, sizeof(sysfs), "%04x:%02x:%02x.%x", slot.domain, slot.bus, + slot.device, slot.function); return std::string(sysfs); } \ No newline at end of file From d054b9aaa9ae7044fbc14446a5e1ff690fb7431b Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:14:08 +0200 Subject: [PATCH 41/50] implement override_path Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/pci_device.hpp | 4 ++++ common/lib/kernel/devices/pci_device.cpp | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/common/include/villas/kernel/devices/pci_device.hpp b/common/include/villas/kernel/devices/pci_device.hpp index 36f16fba9..292b0cd31 100644 --- a/common/include/villas/kernel/devices/pci_device.hpp +++ b/common/include/villas/kernel/devices/pci_device.hpp @@ -61,6 +61,9 @@ struct Region { }; class PciDevice : public Device { +private: + static constexpr char OVERRIDE_DEFAULT[] = "driver_override"; + public: PciDevice(Id i, Slot s) : id(i), slot(s), log(Log::get("kernel:pci")) {} @@ -74,6 +77,7 @@ class PciDevice : public Device { std::optional> driver() const override; std::optional iommu_group() const override; std::string name() const override; + std::filesystem::path override_path() const override; // Bind a new LKM to the PCI device bool attachDriver(const std::string &driver) const; diff --git a/common/lib/kernel/devices/pci_device.cpp b/common/lib/kernel/devices/pci_device.cpp index 277e6b708..3f6850621 100644 --- a/common/lib/kernel/devices/pci_device.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -468,4 +468,9 @@ std::string PciDevice::name() const { slot.device, slot.function); return std::string(sysfs); -} \ No newline at end of file +} +// TODO: test +std::filesystem::path PciDevice::override_path() const { + + return this->path() / OVERRIDE_DEFAULT; +} From 8e218113289c560a473de994547acd0b366f8d52 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:14:53 +0200 Subject: [PATCH 42/50] implement path Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/pci_device.hpp | 1 + common/lib/kernel/devices/pci_device.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/common/include/villas/kernel/devices/pci_device.hpp b/common/include/villas/kernel/devices/pci_device.hpp index 292b0cd31..b62595202 100644 --- a/common/include/villas/kernel/devices/pci_device.hpp +++ b/common/include/villas/kernel/devices/pci_device.hpp @@ -78,6 +78,7 @@ class PciDevice : public Device { std::optional iommu_group() const override; std::string name() const override; std::filesystem::path override_path() const override; + std::filesystem::path path() const override; // Bind a new LKM to the PCI device bool attachDriver(const std::string &driver) const; diff --git a/common/lib/kernel/devices/pci_device.cpp b/common/lib/kernel/devices/pci_device.cpp index 3f6850621..358796f42 100644 --- a/common/lib/kernel/devices/pci_device.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -469,6 +469,17 @@ std::string PciDevice::name() const { return std::string(sysfs); } + +// TODO: test +std::filesystem::path PciDevice::path() const { + char sysfs[1024]; + + snprintf(sysfs, sizeof(sysfs), "%04x:%02x:%02x.%x", slot.domain, slot.bus, + slot.device, slot.function); + + return sysfs; +}; + // TODO: test std::filesystem::path PciDevice::override_path() const { From 97d35e4375bd98225eb68789eff6a4710e2140fb Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:15:41 +0200 Subject: [PATCH 43/50] format Signed-off-by: Pascal Bauer --- common/lib/kernel/devices/pci_device.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/lib/kernel/devices/pci_device.cpp b/common/lib/kernel/devices/pci_device.cpp index 358796f42..6f2161f3f 100644 --- a/common/lib/kernel/devices/pci_device.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -466,7 +466,7 @@ std::string PciDevice::name() const { snprintf(sysfs, sizeof(sysfs), "%04x:%02x:%02x.%x", slot.domain, slot.bus, slot.device, slot.function); - + return std::string(sysfs); } From 04f8c9218101962eb1f66bede07b47358abaa310 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:19:44 +0200 Subject: [PATCH 44/50] implement probe Signed-off-by: Pascal Bauer --- common/include/villas/kernel/devices/pci_device.hpp | 3 ++- common/lib/kernel/devices/pci_device.cpp | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/common/include/villas/kernel/devices/pci_device.hpp b/common/include/villas/kernel/devices/pci_device.hpp index b62595202..8f6a10cf5 100644 --- a/common/include/villas/kernel/devices/pci_device.hpp +++ b/common/include/villas/kernel/devices/pci_device.hpp @@ -62,6 +62,7 @@ struct Region { class PciDevice : public Device { private: + static constexpr char PROBE_DEFAULT[] = "/sys/bus/pci/drivers_probe"; static constexpr char OVERRIDE_DEFAULT[] = "driver_override"; public: @@ -79,11 +80,11 @@ class PciDevice : public Device { std::string name() const override; std::filesystem::path override_path() const override; std::filesystem::path path() const override; + void probe() const override; // Bind a new LKM to the PCI device bool attachDriver(const std::string &driver) const; - std::list getRegions() const; // Write 32-bit BAR value from to the PCI configuration space diff --git a/common/lib/kernel/devices/pci_device.cpp b/common/lib/kernel/devices/pci_device.cpp index 6f2161f3f..425c2ee3b 100644 --- a/common/lib/kernel/devices/pci_device.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -485,3 +485,7 @@ std::filesystem::path PciDevice::override_path() const { return this->path() / OVERRIDE_DEFAULT; } + +void PciDevice::probe() const { + write_to_file(this->name(), this->PROBE_DEFAULT); +} From 6f6fe52ce4077861b2f0b822dcac31f2269d7c50 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:20:32 +0200 Subject: [PATCH 45/50] remove semicolon Signed-off-by: Pascal Bauer --- common/lib/kernel/devices/pci_device.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/lib/kernel/devices/pci_device.cpp b/common/lib/kernel/devices/pci_device.cpp index 425c2ee3b..6bc9af0b5 100644 --- a/common/lib/kernel/devices/pci_device.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -478,7 +478,7 @@ std::filesystem::path PciDevice::path() const { slot.device, slot.function); return sysfs; -}; +} // TODO: test std::filesystem::path PciDevice::override_path() const { From 795a565adf41b7df0ed6b6d7025b0d67b6a5165b Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:23:09 +0200 Subject: [PATCH 46/50] add include Signed-off-by: Pascal Bauer --- common/lib/kernel/devices/pci_device.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/common/lib/kernel/devices/pci_device.cpp b/common/lib/kernel/devices/pci_device.cpp index 6bc9af0b5..d0730d494 100644 --- a/common/lib/kernel/devices/pci_device.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -20,9 +20,11 @@ #include #include #include +#include #include using namespace villas::kernel::devices; +using villas::kernel::devices::utils::write_to_file; #define PCI_BASE_ADDRESS_N(n) (PCI_BASE_ADDRESS_0 + sizeof(uint32_t) * (n)) From 09eac47a402fffe62fadc725d63e8e06fec282e8 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sat, 24 Aug 2024 12:24:06 +0200 Subject: [PATCH 47/50] adjust to new interface Signed-off-by: Pascal Bauer --- common/lib/kernel/vfio_container.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/lib/kernel/vfio_container.cpp b/common/lib/kernel/vfio_container.cpp index 76923aa08..595edf9fd 100644 --- a/common/lib/kernel/vfio_container.cpp +++ b/common/lib/kernel/vfio_container.cpp @@ -197,7 +197,7 @@ std::shared_ptr Container::attachDevice(devices::PciDevice &pdev) { throw RuntimeError("Failed to load kernel driver: vfio_pci"); // Bind PCI card to vfio-pci driver if not already bound - if (pdev.getDriver() != kernelDriver) { + if ( !(pdev.driver().has_value()) || pdev.driver().value()->name() != kernelDriver) { log->debug("Bind PCI card to kernel driver '{}'", kernelDriver); pdev.attachDriver(kernelDriver); } @@ -215,7 +215,7 @@ std::shared_ptr Container::attachDevice(devices::PciDevice &pdev) { } // Get IOMMU group of device - int index = isIommuEnabled() ? pdev.getIommuGroup() : 0; + int index = isIommuEnabled() ? pdev.iommu_group().value() : 0; if (index < 0) { ret = kernel::getCmdlineParam("intel_iommu", iommu_state, sizeof(iommu_state)); From 45f35338b504901709834aa9573cc20a1fd4122b Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sun, 6 Oct 2024 21:36:27 +0200 Subject: [PATCH 48/50] update namespace Signed-off-by: Pascal Bauer --- common/lib/kernel/devices/pci_device.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/lib/kernel/devices/pci_device.cpp b/common/lib/kernel/devices/pci_device.cpp index d0730d494..b81b2a26d 100644 --- a/common/lib/kernel/devices/pci_device.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -24,7 +24,7 @@ #include using namespace villas::kernel::devices; -using villas::kernel::devices::utils::write_to_file; +using villas::utils::write_to_file; #define PCI_BASE_ADDRESS_N(n) (PCI_BASE_ADDRESS_0 + sizeof(uint32_t) * (n)) From 2b98171e8a98ff1b5096ff2994f4181913112b31 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Sun, 6 Oct 2024 22:09:15 +0200 Subject: [PATCH 49/50] adjust utils to refactor Signed-off-by: Pascal Bauer --- common/lib/kernel/devices/pci_device.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/lib/kernel/devices/pci_device.cpp b/common/lib/kernel/devices/pci_device.cpp index b81b2a26d..50ebe27fb 100644 --- a/common/lib/kernel/devices/pci_device.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include using namespace villas::kernel::devices; From a41e30ed38345a338de18ef351420089715b62b1 Mon Sep 17 00:00:00 2001 From: Pascal Bauer Date: Fri, 11 Oct 2024 15:18:18 +0200 Subject: [PATCH 50/50] apply driver refactor Signed-off-by: Pascal Bauer --- common/lib/kernel/devices/pci_device.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/common/lib/kernel/devices/pci_device.cpp b/common/lib/kernel/devices/pci_device.cpp index 50ebe27fb..2324c6013 100644 --- a/common/lib/kernel/devices/pci_device.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -18,10 +18,9 @@ #include #include -#include +#include #include #include -#include using namespace villas::kernel::devices; using villas::utils::write_to_file; @@ -331,7 +330,7 @@ std::optional> PciDevice::driver() const { if (ret < 0) throw SystemError("Failed to follow link: {}", sysfs); - auto driver = std::make_optional(std::make_unique( + auto driver = std::make_optional(std::make_unique( "/sys/bus/pci/drivers/" + std::string(basename(syml)))); return driver; }