cgroups/
rdma.rs

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