Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
babe80f
add device interface
IgnoreWarnings Aug 24, 2024
d312e37
add driver interface
IgnoreWarnings Aug 24, 2024
adeaa9d
add generic driver
IgnoreWarnings Aug 24, 2024
dca4d4c
add utils
IgnoreWarnings Aug 24, 2024
9d6b81b
add platform device
IgnoreWarnings Aug 24, 2024
d65910a
add IpDevice
IgnoreWarnings Aug 24, 2024
abc56a9
add new classes to cmake
IgnoreWarnings Aug 24, 2024
9b1524f
fix generic driver header
IgnoreWarnings Aug 24, 2024
10e6aee
cmake move drivers to linux section
IgnoreWarnings Aug 30, 2024
ce8aa3f
remove duplicate public
IgnoreWarnings Aug 30, 2024
2357122
delete default comnstructor
IgnoreWarnings Aug 30, 2024
0c270c5
formatting
IgnoreWarnings Aug 30, 2024
46065f3
use filename()
IgnoreWarnings Aug 30, 2024
6e7c9c6
cleanup code
IgnoreWarnings Sep 4, 2024
80d7942
Refactor: move device utility functions to villas util
IgnoreWarnings Sep 23, 2024
9ac5cd6
update device description
IgnoreWarnings Sep 23, 2024
1f9e5c4
format comment
IgnoreWarnings Sep 24, 2024
ffbdcf5
Update comments
IgnoreWarnings Sep 24, 2024
eeb56f4
remove comment
IgnoreWarnings Sep 24, 2024
1f19e42
Use villas exception
IgnoreWarnings Sep 25, 2024
43e267a
remove dead header
IgnoreWarnings Oct 6, 2024
270acd0
Update comment
IgnoreWarnings Oct 9, 2024
87a2372
Use std::filestytem
IgnoreWarnings Oct 9, 2024
ff00747
fix read names in directory method
IgnoreWarnings Oct 9, 2024
486877e
add . at end of comment
IgnoreWarnings Oct 10, 2024
5a48e38
edit driver comment
IgnoreWarnings Oct 11, 2024
ca2715d
rename GenericDriver to LinuxDriver
IgnoreWarnings Oct 11, 2024
e945a9e
rename generic_driver to linux_driver
IgnoreWarnings Oct 11, 2024
51eaea3
update driver comment
IgnoreWarnings Oct 11, 2024
fadb14c
initial commit
IgnoreWarnings Aug 23, 2024
ae3ab18
Driver Interface
IgnoreWarnings Aug 23, 2024
3659225
fix broken includes
IgnoreWarnings Aug 23, 2024
0400f19
remove unused code
IgnoreWarnings Aug 23, 2024
1f9d7d5
rename Platform driver to GenericDriver
IgnoreWarnings Aug 23, 2024
b229d7c
inherit from device interface
IgnoreWarnings Aug 23, 2024
ec3da8d
implement driver()
IgnoreWarnings Aug 23, 2024
4772060
implement iommu_group
IgnoreWarnings Aug 23, 2024
2bfccaa
implement name
IgnoreWarnings Aug 23, 2024
56b2d69
removed unused code
IgnoreWarnings Aug 23, 2024
cac2e89
format
IgnoreWarnings Aug 24, 2024
d054b9a
implement override_path
IgnoreWarnings Aug 24, 2024
8e21811
implement path
IgnoreWarnings Aug 24, 2024
97d35e4
format
IgnoreWarnings Aug 24, 2024
04f8c92
implement probe
IgnoreWarnings Aug 24, 2024
6f6fe52
remove semicolon
IgnoreWarnings Aug 24, 2024
795a565
add include
IgnoreWarnings Aug 24, 2024
09eac47
adjust to new interface
IgnoreWarnings Aug 24, 2024
45f3533
update namespace
IgnoreWarnings Oct 6, 2024
2b98171
adjust utils to refactor
IgnoreWarnings Oct 6, 2024
a41e30e
apply driver refactor
IgnoreWarnings Oct 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions common/include/villas/kernel/devices/device.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Interface for Linux/Unix devices.
*
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
*
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <filesystem>
#include <optional>
#include <villas/kernel/devices/driver.hpp>

namespace villas {
namespace kernel {
namespace devices {

class Device {
public:
virtual ~Device(){};

virtual std::optional<std::unique_ptr<Driver>> driver() const = 0;
virtual std::optional<int> 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
29 changes: 29 additions & 0 deletions common/include/villas/kernel/devices/driver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Interface for device drivers. OS/platform independend.
* Implemented for Linux/Unix drivers in linux_driver.hpp
*
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
*
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
* 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
35 changes: 35 additions & 0 deletions common/include/villas/kernel/devices/ip_device.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* IpDevice: Linux/Unix device which represents an IP component of a FPGA.
*
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
*
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <filesystem>
#include <villas/kernel/devices/platform_device.hpp>

namespace villas {
namespace kernel {
namespace devices {

class IpDevice : public PlatformDevice {
public:
static IpDevice from(const std::filesystem::path unsafe_path);
static bool is_path_valid(const std::filesystem::path unsafe_path);

private:
IpDevice() = delete;
IpDevice(const std::filesystem::path valid_path) //! Dont allow unvalidated paths
: PlatformDevice(valid_path){};

public:
size_t addr() const;
std::string ip_name() const;
};

} // namespace devices
} // namespace kernel
} // namespace villas
52 changes: 52 additions & 0 deletions common/include/villas/kernel/devices/linux_driver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* Implementation of driver interface for Linux/Unix based operation system drivers.
*
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
*
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <filesystem>
#include <fstream>
#include <iostream>
#include <villas/kernel/devices/driver.hpp>

namespace villas {
namespace kernel {
namespace devices {

class LinuxDriver : 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:
LinuxDriver(const std::filesystem::path path)
: LinuxDriver(path, path / std::filesystem::path(BIND_DEFAULT),
path / std::filesystem::path(UNBIND_DEFAULT)){};

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:
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
} // namespace kernel
} // namespace villas
20 changes: 14 additions & 6 deletions common/include/villas/kernel/devices/pci_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include <villas/log.hpp>

#include <villas/kernel/devices/device.hpp>

namespace villas {
namespace kernel {
namespace devices {
Expand Down Expand Up @@ -58,7 +60,11 @@ struct Region {
unsigned long long flags;
};

class PciDevice {
class PciDevice : public Device {
private:
static constexpr char PROBE_DEFAULT[] = "/sys/bus/pci/drivers_probe";
static constexpr char OVERRIDE_DEFAULT[] = "driver_override";

public:
PciDevice(Id i, Slot s) : id(i), slot(s), log(Log::get("kernel:pci")) {}

Expand All @@ -68,15 +74,17 @@ class PciDevice {

bool operator==(const PciDevice &other);

// Get currently loaded driver for device
std::string getDriver() const;
// Implement device interface
std::optional<std::unique_ptr<Driver>> driver() const override;
std::optional<int> 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;

// 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<Region> getRegions() const;

// Write 32-bit BAR value from to the PCI configuration space
Expand Down
51 changes: 51 additions & 0 deletions common/include/villas/kernel/devices/platform_device.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Platform Device: Platform bus based Linux/Unix device.
*
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
*
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <filesystem>
#include <villas/kernel/devices/device.hpp>
#include <villas/kernel/devices/driver.hpp>

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<std::unique_ptr<Driver>> driver() const override;
std::optional<int> 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
4 changes: 4 additions & 0 deletions common/include/villas/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <list>
#include <string>
#include <vector>
#include <filesystem>

#include <cassert>
#include <cstdint>
Expand Down Expand Up @@ -211,6 +212,9 @@ template <class... Ts> struct overloaded : Ts... {
// explicit deduction guide (not needed as of C++20)
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

void write_to_file(std::string data, const std::filesystem::path file);
std::vector<std::string> read_names_in_directory(const std::filesystem::path &directory);

namespace base64 {

using byte = std::uint8_t;
Expand Down
3 changes: 3 additions & 0 deletions common/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ endif()

if(CMAKE_SYSTEM_NAME STREQUAL Linux)
target_sources(villas-common PRIVATE
kernel/devices/ip_device.cpp
kernel/devices/linux_driver.cpp
kernel/devices/pci_device.cpp
kernel/devices/platform_device.cpp
kernel/vfio_device.cpp
kernel/vfio_group.cpp
kernel/vfio_container.cpp
Expand Down
54 changes: 54 additions & 0 deletions common/lib/kernel/devices/ip_device.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* IpDevice
*
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
*
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
* SPDX-License-Identifier: Apache-2.0
*/

#include <filesystem>
#include <regex>
#include <stdexcept>

#include <villas/exceptions.hpp>
#include <villas/kernel/devices/ip_device.hpp>

using villas::kernel::devices::IpDevice;

IpDevice IpDevice::from(const std::filesystem::path unsafe_path) {
if (!is_path_valid(unsafe_path))
throw RuntimeError(
"Path {} failed validation as IpDevicePath [adress in hex].[name] ",
unsafe_path.u8string());
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) {
std::string assumed_device_name = unsafe_path.filename();

// Match format of hexaddr.devicename
if (!std::regex_match(assumed_device_name,
std::regex(R"([0-9A-Fa-f]+\..*)"))) {
return false;
}

return true;
}
40 changes: 40 additions & 0 deletions common/lib/kernel/devices/linux_driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* LinuxDriver
*
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
*
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
* SPDX-License-Identifier: Apache-2.0
*/

#include <villas/kernel/devices/linux_driver.hpp>

#include <villas/kernel/devices/device.hpp>
#include <villas/utils.hpp>

using villas::kernel::devices::Device, villas::kernel::devices::LinuxDriver;
using villas::utils::write_to_file;

void LinuxDriver::attach(const Device &device) const {
if (device.driver().has_value()) {
device.driver().value()->unbind(device);
}
this->override(device);
device.probe();
}

void LinuxDriver::bind(const Device &device) const {
write_to_file(device.name(), this->bind_path);
}

std::string LinuxDriver::name() const {
size_t pos = path.u8string().rfind('/');
return path.u8string().substr(pos + 1);
}

void LinuxDriver::override(const Device &device) const {
write_to_file(this->name(), device.override_path());
}

void LinuxDriver::unbind(const Device &device) const {
write_to_file(device.name(), this->unbind_path);
}
Loading