mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-02 08:17:01 +00:00
libs: Add AddSwapPath to service AgentService
AddSwap send the pci path to guest kernel to let it add swap device. But some mmio device doesn't have pci path. To support it add AddSwapPath send virt_path to guest kernel as swap device. Fixes: #10988 Signed-off-by: Hui Zhu <teawater@antgroup.com>
This commit is contained in:
parent
7787340ab6
commit
93cd30862d
@ -28,9 +28,9 @@ use oci::{Hooks, LinuxNamespace, Spec};
|
||||
use oci_spec::runtime as oci;
|
||||
use protobuf::MessageField;
|
||||
use protocols::agent::{
|
||||
AddSwapRequest, AgentDetails, CopyFileRequest, GetIPTablesRequest, GetIPTablesResponse,
|
||||
GuestDetailsResponse, Interfaces, Metrics, OOMEvent, ReadStreamResponse, Routes,
|
||||
SetIPTablesRequest, SetIPTablesResponse, StatsContainerResponse, VolumeStatsRequest,
|
||||
AddSwapPathRequest, AddSwapRequest, AgentDetails, CopyFileRequest, GetIPTablesRequest,
|
||||
GetIPTablesResponse, GuestDetailsResponse, Interfaces, Metrics, OOMEvent, ReadStreamResponse,
|
||||
Routes, SetIPTablesRequest, SetIPTablesResponse, StatsContainerResponse, VolumeStatsRequest,
|
||||
WaitProcessResponse, WriteStreamResponse,
|
||||
};
|
||||
use protocols::csi::{
|
||||
@ -1565,6 +1565,19 @@ impl agent_ttrpc::AgentService for AgentService {
|
||||
Ok(Empty::new())
|
||||
}
|
||||
|
||||
async fn add_swap_path(
|
||||
&self,
|
||||
ctx: &TtrpcContext,
|
||||
req: protocols::agent::AddSwapPathRequest,
|
||||
) -> ttrpc::Result<Empty> {
|
||||
trace_rpc_call!(ctx, "add_swap_path", req);
|
||||
is_allowed(&req).await?;
|
||||
|
||||
do_add_swap_path(&req).await.map_ttrpc_err(same)?;
|
||||
|
||||
Ok(Empty::new())
|
||||
}
|
||||
|
||||
#[cfg(feature = "agent-policy")]
|
||||
async fn set_policy(
|
||||
&self,
|
||||
@ -2092,6 +2105,19 @@ async fn do_add_swap(sandbox: &Arc<Mutex<Sandbox>>, req: &AddSwapRequest) -> Res
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn do_add_swap_path(req: &AddSwapPathRequest) -> Result<()> {
|
||||
let c_str = CString::new(req.path.clone())?;
|
||||
let ret = unsafe { libc::swapon(c_str.as_ptr() as *const c_char, 0) };
|
||||
if ret != 0 {
|
||||
return Err(anyhow!(
|
||||
"libc::swapon get error {}",
|
||||
io::Error::last_os_error()
|
||||
));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Setup container bundle under CONTAINER_BASE, which is cleaned up
|
||||
// before removing a container.
|
||||
// - bundle path is /<CONTAINER_BASE>/<cid>/
|
||||
|
@ -74,6 +74,7 @@ service AgentService {
|
||||
rpc CopyFile(CopyFileRequest) returns (google.protobuf.Empty);
|
||||
rpc GetOOMEvent(GetOOMEventRequest) returns (OOMEvent);
|
||||
rpc AddSwap(AddSwapRequest) returns (google.protobuf.Empty);
|
||||
rpc AddSwapPath(AddSwapPathRequest) returns (google.protobuf.Empty);
|
||||
rpc GetVolumeStats(VolumeStatsRequest) returns (VolumeStatsResponse);
|
||||
rpc ResizeVolume(ResizeVolumeRequest) returns (google.protobuf.Empty);
|
||||
rpc SetPolicy(SetPolicyRequest) returns (google.protobuf.Empty);
|
||||
@ -595,6 +596,10 @@ message AddSwapRequest {
|
||||
repeated uint32 PCIPath = 1;
|
||||
}
|
||||
|
||||
message AddSwapPathRequest {
|
||||
string path = 1;
|
||||
}
|
||||
|
||||
message GetMetricsRequest {}
|
||||
|
||||
message Metrics {
|
||||
|
@ -124,5 +124,6 @@ impl_agent!(
|
||||
online_cpu_mem | crate::OnlineCPUMemRequest | crate::Empty | None,
|
||||
get_metrics | crate::Empty | crate::MetricsResponse | None,
|
||||
get_guest_details | crate::GetGuestDetailsRequest | crate::GuestDetailsResponse | None,
|
||||
add_swap | crate::AddSwapRequest | crate::Empty | None
|
||||
add_swap | crate::AddSwapRequest | crate::Empty | None,
|
||||
add_swap_path | crate::AddSwapPathRequest | crate::Empty | None
|
||||
);
|
||||
|
@ -13,19 +13,20 @@ use protocols::{
|
||||
|
||||
use crate::{
|
||||
types::{
|
||||
ARPNeighbor, ARPNeighbors, AddArpNeighborRequest, AddSwapRequest, AgentDetails, BlkioStats,
|
||||
BlkioStatsEntry, CgroupStats, CheckRequest, CloseStdinRequest, ContainerID,
|
||||
CopyFileRequest, CpuStats, CpuUsage, CreateContainerRequest, CreateSandboxRequest, Device,
|
||||
Empty, ExecProcessRequest, FSGroup, FSGroupChangePolicy, GetIPTablesRequest,
|
||||
GetIPTablesResponse, GuestDetailsResponse, HealthCheckResponse, HugetlbStats, IPAddress,
|
||||
IPFamily, Interface, Interfaces, KernelModule, MemHotplugByProbeRequest, MemoryData,
|
||||
MemoryStats, MetricsResponse, NetworkStats, OnlineCPUMemRequest, PidsStats,
|
||||
ReadStreamRequest, ReadStreamResponse, RemoveContainerRequest, ReseedRandomDevRequest,
|
||||
ResizeVolumeRequest, Route, Routes, SetGuestDateTimeRequest, SetIPTablesRequest,
|
||||
SetIPTablesResponse, SharedMount, SignalProcessRequest, StatsContainerResponse, Storage,
|
||||
StringUser, ThrottlingData, TtyWinResizeRequest, UpdateContainerRequest,
|
||||
UpdateInterfaceRequest, UpdateRoutesRequest, VersionCheckResponse, VolumeStatsRequest,
|
||||
VolumeStatsResponse, WaitProcessRequest, WriteStreamRequest,
|
||||
ARPNeighbor, ARPNeighbors, AddArpNeighborRequest, AddSwapPathRequest, AddSwapRequest,
|
||||
AgentDetails, BlkioStats, BlkioStatsEntry, CgroupStats, CheckRequest, CloseStdinRequest,
|
||||
ContainerID, CopyFileRequest, CpuStats, CpuUsage, CreateContainerRequest,
|
||||
CreateSandboxRequest, Device, Empty, ExecProcessRequest, FSGroup, FSGroupChangePolicy,
|
||||
GetIPTablesRequest, GetIPTablesResponse, GuestDetailsResponse, HealthCheckResponse,
|
||||
HugetlbStats, IPAddress, IPFamily, Interface, Interfaces, KernelModule,
|
||||
MemHotplugByProbeRequest, MemoryData, MemoryStats, MetricsResponse, NetworkStats,
|
||||
OnlineCPUMemRequest, PidsStats, ReadStreamRequest, ReadStreamResponse,
|
||||
RemoveContainerRequest, ReseedRandomDevRequest, ResizeVolumeRequest, Route, Routes,
|
||||
SetGuestDateTimeRequest, SetIPTablesRequest, SetIPTablesResponse, SharedMount,
|
||||
SignalProcessRequest, StatsContainerResponse, Storage, StringUser, ThrottlingData,
|
||||
TtyWinResizeRequest, UpdateContainerRequest, UpdateInterfaceRequest, UpdateRoutesRequest,
|
||||
VersionCheckResponse, VolumeStatsRequest, VolumeStatsResponse, WaitProcessRequest,
|
||||
WriteStreamRequest,
|
||||
},
|
||||
GetGuestDetailsRequest, OomEventResponse, WaitProcessResponse, WriteStreamResponse,
|
||||
};
|
||||
@ -884,3 +885,12 @@ impl From<AddSwapRequest> for agent::AddSwapRequest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AddSwapPathRequest> for agent::AddSwapPathRequest {
|
||||
fn from(from: AddSwapPathRequest) -> Self {
|
||||
Self {
|
||||
path: from.path,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,9 +14,9 @@ mod log_forwarder;
|
||||
mod sock;
|
||||
pub mod types;
|
||||
pub use types::{
|
||||
ARPNeighbor, ARPNeighbors, AddArpNeighborRequest, AddSwapRequest, BlkioStatsEntry,
|
||||
CheckRequest, CloseStdinRequest, ContainerID, ContainerProcessID, CopyFileRequest,
|
||||
CreateContainerRequest, CreateSandboxRequest, Empty, ExecProcessRequest,
|
||||
ARPNeighbor, ARPNeighbors, AddArpNeighborRequest, AddSwapPathRequest, AddSwapRequest,
|
||||
BlkioStatsEntry, CheckRequest, CloseStdinRequest, ContainerID, ContainerProcessID,
|
||||
CopyFileRequest, CreateContainerRequest, CreateSandboxRequest, Empty, ExecProcessRequest,
|
||||
GetGuestDetailsRequest, GetIPTablesRequest, GetIPTablesResponse, GuestDetailsResponse,
|
||||
HealthCheckResponse, IPAddress, IPFamily, Interface, Interfaces, ListProcessesRequest,
|
||||
MemHotplugByProbeRequest, MetricsResponse, OnlineCPUMemRequest, OomEventResponse,
|
||||
@ -95,4 +95,5 @@ pub trait Agent: AgentManager + HealthService + Send + Sync {
|
||||
async fn resize_volume(&self, req: ResizeVolumeRequest) -> Result<Empty>;
|
||||
async fn get_guest_details(&self, req: GetGuestDetailsRequest) -> Result<GuestDetailsResponse>;
|
||||
async fn add_swap(&self, req: AddSwapRequest) -> Result<Empty>;
|
||||
async fn add_swap_path(&self, req: AddSwapPathRequest) -> Result<Empty>;
|
||||
}
|
||||
|
@ -609,6 +609,11 @@ pub struct AddSwapRequest {
|
||||
pub pci_path: Vec<u32>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Clone, Default, Debug)]
|
||||
pub struct AddSwapPathRequest {
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::convert::TryFrom;
|
||||
|
@ -196,34 +196,36 @@ impl SwapTask {
|
||||
}
|
||||
|
||||
if let DeviceType::Block(device) = device_info {
|
||||
if let Some(pci_path) = device.config.pci_path.clone() {
|
||||
if let Err(e) = self
|
||||
.agent
|
||||
.add_swap(agent::types::AddSwapRequest {
|
||||
pci_path: pci_path.slots.iter().map(|slot| slot.0 as u32).collect(),
|
||||
})
|
||||
.await
|
||||
{
|
||||
if let Err(e1) = self
|
||||
.device_manager
|
||||
.write()
|
||||
.await
|
||||
.try_remove_device(&device_id)
|
||||
.await
|
||||
{
|
||||
error!(
|
||||
sl!(),
|
||||
"swap_task: try_remove_device {} fail: {:?}", swap_path, e1
|
||||
);
|
||||
}
|
||||
|
||||
return Err(anyhow!("swap_task: agent.swap_add failed: {:?}", e));
|
||||
}
|
||||
let ret = if let Some(pci_path) = device.config.pci_path.clone() {
|
||||
self.agent.add_swap(agent::types::AddSwapRequest {
|
||||
pci_path: pci_path.slots.iter().map(|slot| slot.0 as u32).collect(),
|
||||
})
|
||||
} else if !device.config.virt_path.is_empty() {
|
||||
self.agent.add_swap_path(agent::types::AddSwapPathRequest {
|
||||
path: device.config.virt_path.clone(),
|
||||
})
|
||||
} else {
|
||||
return Err(anyhow!(
|
||||
"swap_task: device_info {} pci_path is None",
|
||||
swap_path
|
||||
));
|
||||
};
|
||||
|
||||
if let Err(e) = ret.await {
|
||||
if let Err(e1) = self
|
||||
.device_manager
|
||||
.write()
|
||||
.await
|
||||
.try_remove_device(&device_id)
|
||||
.await
|
||||
{
|
||||
error!(
|
||||
sl!(),
|
||||
"swap_task: try_remove_device {} fail: {:?}", swap_path, e1
|
||||
);
|
||||
}
|
||||
|
||||
return Err(anyhow!("swap_task: agent.swap_add failed: {:?}", e));
|
||||
}
|
||||
} else {
|
||||
return Err(anyhow!("swap_task: device_info {} is not Block", swap_path));
|
||||
|
Loading…
Reference in New Issue
Block a user