Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ members = [
]

[workspace.package]
version = "0.7.0"
version = "0.7.1"
authors = ["luytan <luytan@khora.me>"]
repository = "https://github.com/OpenGamingCollective/cardwire"
edition = "2024"
Expand Down
65 changes: 30 additions & 35 deletions crates/cardwire-core/src/gpu/ebpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::BTreeMap;

use crate::{errors::Error as CardwireError, gpu::models::GpuDevice, pci::PciDevice};
use cardwire_ebpf::EbpfBlocker;
use log::{info, warn};

pub struct GpuBlocker {
inner: EbpfBlocker,
Expand Down Expand Up @@ -74,82 +75,76 @@ pub fn block_gpu(
if block {
blocker.inner.block_card(card_id)?;
blocker.inner.block_render(render_id)?;
recursive_block_pci(blocker, gpu, pci_list)?;
chain_block_pci(blocker, gpu, pci_list)?;
if gpu.nvidia() {
blocker.inner.block_nvidia(gpu.nvidia_minor().unwrap())?
}
Ok(())
} else {
blocker.inner.unblock_card(card_id)?;
blocker.inner.unblock_render(render_id)?;
recursive_unblock_pci(blocker, gpu, pci_list)?;
chain_unblock_pci(blocker, gpu, pci_list)?;
if gpu.nvidia() {
blocker.inner.unblock_nvidia(gpu.nvidia_minor().unwrap())?
}
Ok(())
}
}

fn recursive_block_pci(
fn chain_block_pci(
blocker: &mut GpuBlocker,
gpu: &GpuDevice,
pci_list: &BTreeMap<String, PciDevice>,
) -> Result<(), CardwireError> {
// Block the gpu pci
blocker.inner.block_pci(gpu.pci.pci_address())?;
info!("blocking pci: {}", gpu.pci.pci_address());
// also block audio card
if gpu.pci.pci_address().ends_with(".0") {
let gpu_audio_adress = gpu.pci.pci_address().to_string().replace(".0", ".1");
let gpu_audio_adress = gpu.pci.pci_address().replace(".0", ".1");
blocker.inner.block_pci(&gpu_audio_adress)?;
}
// Check if gpu has a parent pci
if gpu.pci.parent_pci().is_some() {
// first pci to block
let mut parent_pci: String = gpu.pci.parent_pci().clone().unwrap();
loop {
// Also block the parent pci
if let Some(pci_device) = pci_list.get(&parent_pci) {
blocker.inner.block_pci(pci_device.pci_address())?;
if pci_device.parent_pci().is_some() {
// if the device contain a parent, continue the loop
parent_pci = pci_device.parent_pci().clone().unwrap()
} else {
// if no parent. exit the loop
break;
}
}
// first pci to block
let mut current_parent = gpu.pci.parent_pci().clone();

while let Some(parent_pci) = current_parent {
if let Some(pci_device) = pci_list.get(&parent_pci) {
info!("chain blocking pci: {}", pci_device.pci_address());
blocker.inner.block_pci(pci_device.pci_address())?;
current_parent = pci_device.parent_pci().clone();
} else {
warn!("expected parent pci {} not found in pci_list", parent_pci);
break;
}
}
Ok(())
}
fn recursive_unblock_pci(
fn chain_unblock_pci(
blocker: &mut GpuBlocker,
gpu: &GpuDevice,
pci_list: &BTreeMap<String, PciDevice>,
) -> Result<(), CardwireError> {
// Unblock the gpu pci
info!("unblocking pci: {}", gpu.pci.pci_address());
blocker.inner.unblock_pci(gpu.pci.pci_address())?;
// also unblock audio card
if gpu.pci.pci_address().ends_with(".0") {
let gpu_audio_adress = gpu.pci.pci_address().to_string().replace(".0", ".1");
blocker.inner.block_pci(&gpu_audio_adress)?;
}
// Check if gpu has a parent pci
if gpu.pci.parent_pci().is_some() {
// first pci to block
let mut parent_pci: String = gpu.pci.parent_pci().clone().unwrap();
loop {
// Also block the parent pci
if let Some(pci_device) = pci_list.get(&parent_pci) {
blocker.inner.unblock_pci(pci_device.pci_address())?;
if pci_device.parent_pci().is_some() {
// if the device contain a parent, continue the loop
parent_pci = pci_device.parent_pci().clone().unwrap()
} else {
// if no parent. exit the loop
break;
}
}
// first pci to block
let mut current_parent = gpu.pci.parent_pci().clone();

while let Some(parent_pci) = current_parent {
if let Some(pci_device) = pci_list.get(&parent_pci) {
info!("chain unblocking pci: {}", pci_device.pci_address());
blocker.inner.unblock_pci(pci_device.pci_address())?;
current_parent = pci_device.parent_pci().clone();
} else {
warn!("expected parent pci {} not found in pci_list", parent_pci);
break;
}
}
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions crates/cardwire-daemon/src/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ impl Daemon {
// Valide inputs and turn into a Modes
let mode = Modes::parse(&mode)?;
let mut current_mode = self.state.mode_state.write().await;
if let Err(e) = current_mode.save_state(mode).await {
warn!("mode couldn't be saved to config: {e}");
}
let mut blocker = self.state.ebpf_blocker.write().await;
let pci_list = &self.state.pci_devices;

Expand Down Expand Up @@ -74,6 +71,9 @@ impl Daemon {
}
}
}
if let Err(e) = current_mode.save_state(mode).await {
warn!("mode couldn't be saved to config: {e}");
}
info!("Switched to {}", mode);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion packages/arch-linux/cardwire-PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pkgbase=cardwire
pkgname=cardwire
pkgver=0.7.0
pkgver=0.7.1
pkgrel=1
pkgdesc='GPU manager for Linux using eBPF LSM hooks'
arch=('x86_64')
Expand Down