mirror of
https://github.com/kata-containers/cgroups-rs.git
synced 2026-08-05 02:13:23 +00:00
+13
-2
@@ -1,17 +1,24 @@
|
||||
/* Network classifier controller */
|
||||
//! This module contains the implementation of the `net_cls` cgroup subsystem.
|
||||
//!
|
||||
//! See the Kernel's documentation for more information about this subsystem, found at:
|
||||
//! [Documentation/cgroup-v1/net_cls.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/net_cls.txt)
|
||||
use std::path::PathBuf;
|
||||
use std::io::{Read, Write};
|
||||
use std::fs::File;
|
||||
|
||||
use {NetworkResources, Controllers, Controller, Resources, ControllIdentifier, Subsystem};
|
||||
|
||||
/// A controller that allows controlling the `net_cls` subsystem of a Cgroup.
|
||||
///
|
||||
/// In esssence, using the `net_cls` controller, one can attach a custom class to the network
|
||||
/// packets emitted by the control group's tasks. This can then later be used in iptables to have
|
||||
/// custom firewall rules, QoS, etc.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct NetClsController {
|
||||
base: PathBuf,
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
|
||||
impl Controller for NetClsController {
|
||||
fn control_type(self: &Self) -> Controllers { Controllers::NetCls }
|
||||
fn get_path<'a>(self: &'a Self) -> &'a PathBuf { &self.path }
|
||||
@@ -55,6 +62,7 @@ fn read_u64_from(mut file: File) -> Option<u64> {
|
||||
}
|
||||
|
||||
impl NetClsController {
|
||||
/// Constructs a new `NetClsController` with `oroot` serving as the root of the control group.
|
||||
pub fn new(oroot: PathBuf) -> Self {
|
||||
let mut root = oroot;
|
||||
root.push(Self::controller_type().to_string());
|
||||
@@ -63,6 +71,8 @@ impl NetClsController {
|
||||
path: root,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the network class id of the outgoing packets of the control group's tasks.
|
||||
pub fn set_class(self: &Self, class: u64) {
|
||||
self.open_path("net_cls.classid", true).and_then(|mut file| {
|
||||
let s = format!("{:#08X}", class);
|
||||
@@ -70,6 +80,7 @@ impl NetClsController {
|
||||
});
|
||||
}
|
||||
|
||||
/// Get the network class id of the outgoing packets of the control group's tasks.
|
||||
pub fn get_class(self: &Self) -> u64 {
|
||||
self.open_path("net_cls.classid", false).and_then(|file| {
|
||||
read_u64_from(file)
|
||||
|
||||
Reference in New Issue
Block a user