cgroups_rs/fs/
rdma.rs

1// Copyright (c) 2018 Levente Kurusa
2//
3// SPDX-License-Identifier: Apache-2.0 or MIT
4//
5
6//! This module contains the implementation of the `rdma` cgroup subsystem.
7//!
8//! See the Kernel's documentation for more information about this subsystem, found at:
9//!  [Documentation/cgroup-v1/rdma.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/rdma.txt)
10use std::io::Write;
11use std::path::PathBuf;
12
13use crate::fs::error::ErrorKind::*;
14use crate::fs::error::*;
15
16use crate::fs::read_string_from;
17use crate::fs::{ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem};
18
19/// A controller that allows controlling the `rdma` subsystem of a Cgroup.
20///
21/// In essence, using this controller one can limit the RDMA/IB specific resources that the tasks
22/// in the control group can use.
23#[derive(Debug, Clone)]
24pub struct RdmaController {
25    base: PathBuf,
26    path: PathBuf,
27}
28
29impl ControllerInternal for RdmaController {
30    fn control_type(&self) -> Controllers {
31        Controllers::Rdma
32    }
33    fn get_path(&self) -> &PathBuf {
34        &self.path
35    }
36    fn get_path_mut(&mut self) -> &mut PathBuf {
37        &mut self.path
38    }
39    fn get_base(&self) -> &PathBuf {
40        &self.base
41    }
42
43    fn apply(&self, _res: &Resources) -> Result<()> {
44        Ok(())
45    }
46}
47
48impl ControllIdentifier for RdmaController {
49    fn controller_type() -> Controllers {
50        Controllers::Rdma
51    }
52}
53
54impl<'a> From<&'a Subsystem> for &'a RdmaController {
55    fn from(sub: &'a Subsystem) -> &'a RdmaController {
56        unsafe {
57            match sub {
58                Subsystem::Rdma(c) => c,
59                _ => {
60                    assert_eq!(1, 0);
61                    let v = std::mem::MaybeUninit::uninit();
62                    v.assume_init()
63                }
64            }
65        }
66    }
67}
68
69impl RdmaController {
70    /// Constructs a new `RdmaController` with `root` serving as the root of the control group.
71    pub fn new(point: PathBuf, root: PathBuf) -> Self {
72        Self {
73            base: root,
74            path: point,
75        }
76    }
77
78    /// Returns the current usage of RDMA/IB specific resources.
79    pub fn current(&self) -> Result<String> {
80        self.open_path("rdma.current", false)
81            .and_then(read_string_from)
82    }
83
84    /// Returns the max usage of RDMA/IB specific resources.
85    pub fn max(&self) -> Result<String> {
86        self.open_path("rdma.max", false).and_then(read_string_from)
87    }
88
89    /// Set a maximum usage for each RDMA/IB resource.
90    pub fn set_max(&self, max: &str) -> Result<()> {
91        self.open_path("rdma.max", true).and_then(|mut file| {
92            file.write_all(max.as_ref()).map_err(|e| {
93                Error::with_cause(WriteFailed("rdma.max".to_string(), max.to_string()), e)
94            })
95        })
96    }
97}