Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions fboss/platform/bsp_tests/utils/KmodUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,106 @@
#include <folly/logging/xlog.h>
#include <gtest/gtest.h>
#include <re2/re2.h>
#include <chrono>
#include <filesystem>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#include <folly/FileUtil.h>
#include "fboss/platform/bsp_tests/BspTestEnvironment.h"
#include "fboss/platform/helpers/PlatformUtils.h"
#include "fboss/platform/platform_manager/PkgManager.h"
#include "fboss/platform/platform_manager/Utils.h"

namespace fs = std::filesystem;

namespace facebook::fboss::platform::bsp_tests {

void KmodUtils::bindDesiredDrivers(
const platform_manager::PlatformConfig& platformConfig) {
// Binds pci devices with a desiredDriver attribute to the driver
for (const auto& [pmUnitName, pmUnitConfig] :
*platformConfig.pmUnitConfigs()) {
for (const auto& pciConf : *pmUnitConfig.pciDeviceConfigs()) {
if (pciConf.desiredDriver()) {
for (const auto& dirEntry :
fs::directory_iterator("/sys/bus/pci/devices/")) {
std::string vendor, device, subSystemVendor, subSystemDevice;
auto deviceFilePath = dirEntry.path() / "device";
auto vendorFilePath = dirEntry.path() / "vendor";
auto subSystemVendorFilePath = dirEntry.path() / "subsystem_vendor";
auto subSystemDeviceFilePath = dirEntry.path() / "subsystem_device";
folly::readFile(vendorFilePath.c_str(), vendor);
folly::readFile(deviceFilePath.c_str(), device);
folly::readFile(subSystemVendorFilePath.c_str(), subSystemVendor);
folly::readFile(subSystemDeviceFilePath.c_str(), subSystemDevice);
if (folly::trimWhitespace(vendor).str() == *pciConf.vendorId() &&
folly::trimWhitespace(device).str() == *pciConf.deviceId() &&
folly::trimWhitespace(subSystemVendor).str() ==
*pciConf.subSystemVendorId() &&
folly::trimWhitespace(subSystemDevice).str() ==
*pciConf.subSystemDeviceId()) {
const auto& sysfsPath = dirEntry.path().string();
auto curDriver = fmt::format("{}/driver", sysfsPath);
if (fs::exists(curDriver)) {
break;
}

fs::path desiredDriverPath =
fs::path("/sys/bus/pci/drivers") / *pciConf.desiredDriver();
auto pciDevId = fmt::format(
"{} {} {} {}",
std::string(vendor, 2, 4),
std::string(device, 2, 4),
std::string(subSystemVendor, 2, 4),
std::string(subSystemDevice, 2, 4));

auto command = fmt::format(
"echo '{}' > /sys/bus/pci/drivers/{}/new_id",
pciDevId,
*pciConf.desiredDriver());
auto result = PlatformUtils().execCommand(command);
EXPECT_EQ(result.first, 0) << "Failed to bind a desiredDriver: "
<< *pciConf.desiredDriver();

auto charDevPath = fmt::format(
"/dev/fbiob_{}.{}.{}.{}",
std::string(vendor, 2, 4),
std::string(device, 2, 4),
std::string(subSystemVendor, 2, 4),
std::string(subSystemDevice, 2, 4));
bool isReady = platform_manager::Utils().checkDeviceReadiness(
[&charDevPath]() { return fs::exists(charDevPath); },
fmt::format("Waiting for device {}", charDevPath),
std::chrono::seconds(10));

EXPECT_TRUE(isReady)
<< "Timed out after 10 seconds waiting for device to appear at: "
<< charDevPath;
EXPECT_EQ(result.first, 0) << "Failed to bind a desiredDriver: "
<< *pciConf.desiredDriver();
break;
}
}
}
}
}
}

void KmodUtils::loadKmods(const BspKmodsFile& kmods) {
// Load shared kmods first
for (const auto& kmod : *kmods.sharedKmods()) {
auto result = PlatformUtils().execCommand(fmt::format("modprobe {}", kmod));
EXPECT_EQ(result.first, 0) << "Failed to load shared kmod: " << kmod;
}

BspTestEnvironment* env = BspTestEnvironment::GetInstance();
const platform_manager::PlatformConfig& platformConfig =
env->getPlatformManagerConfig();
bindDesiredDrivers(platformConfig);

// Then load BSP kmods
for (const auto& kmod : *kmods.bspKmods()) {
auto result = PlatformUtils().execCommand(fmt::format("modprobe {}", kmod));
Expand Down
2 changes: 2 additions & 0 deletions fboss/platform/bsp_tests/utils/KmodUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ using platform_manager::BspKmodsFile;

class KmodUtils {
public:
static void bindDesiredDrivers(
const platform_manager::PlatformConfig& platformConfig);
static void loadKmods(const BspKmodsFile& kmods);
static void unloadKmods(const BspKmodsFile& kmods);
static void fbspRemove(
Expand Down
Loading