Skip to content

Commit

Permalink
feat: script for setting kernel tunables and check when running neard (
Browse files Browse the repository at this point in the history
…#12577)

Standalone script for setting the recommended networking settings on
Linux and addition of a check when starting neard. If the settings are
not right, errors will be printed without halting the process.

---------

Co-authored-by: Razvan Barbascu <razvan@nearone.org>
Co-authored-by: Akhilesh Singhania <akhi3030@gmail.com>
  • Loading branch information
3 people authored Dec 12, 2024
1 parent 8a4e813 commit 53503e8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
57 changes: 57 additions & 0 deletions neard/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ impl RunCmd {
.unwrap_or_else(|e| panic!("Error loading config: {:#}", e));

check_release_build(&near_config.client_config.chain_id);
check_kernel_params();

// Set current version in client config.
near_config.client_config.version = crate::neard_version();
Expand Down Expand Up @@ -808,6 +809,62 @@ impl ValidateConfigCommand {
}
}

#[cfg(target_os = "linux")]
fn normalize_whitespace(s: &str) -> String {
s.split_whitespace().collect::<Vec<_>>().join(" ")
}

/// Checks the provided kernel parameter from /proc/sys
/// and prints an error if it is not set to the expected value.
#[cfg(target_os = "linux")]
fn check_kernel_param(param_name: &str, expected_value: &str) {
// Convert the dotted param_name into a path under /proc/sys
// For example, "net.core.rmem_max" -> "/proc/sys/net/core/rmem_max"
let mut path = PathBuf::from("/proc/sys");
for part in param_name.split('.') {
path.push(part);
}

match std::fs::read_to_string(&path) {
Ok(contents) => {
let actual = contents.trim();
let actual_normalized = normalize_whitespace(actual);
let expected_normalized = normalize_whitespace(expected_value);

if actual_normalized != expected_normalized {
error!(
"ERROR: {} is set to {}, expected {}. Please run `scripts/set_kernel_params.sh`.",
param_name, actual_normalized, expected_normalized
);
} else {
info!("OK: {} is set to expected value {}", param_name, expected_normalized);
}
}
Err(e) => {
error!("ERROR: failed to read parameter {} from {}: {}", param_name, path.display(), e);
}
}
}

#[cfg(not(target_os = "linux"))]
fn check_kernel_params() {}

/// Checks if the system has the expected values for the sysctl parameters for optimal networking performance.
#[cfg(target_os = "linux")]
fn check_kernel_params() {
let expected_rmem_max = "8388608";
let expected_wmem_max = "8388608";
let expected_tcp_rmem = "4096 87380 8388608";
let expected_tcp_wmem = "4096 16384 8388608";
let expected_slow_start = "0";

check_kernel_param("net.core.rmem_max", expected_rmem_max);
check_kernel_param("net.core.wmem_max", expected_wmem_max);
check_kernel_param("net.ipv4.tcp_rmem", expected_tcp_rmem);
check_kernel_param("net.ipv4.tcp_wmem", expected_tcp_wmem);
check_kernel_param("net.ipv4.tcp_slow_start_after_idle", expected_slow_start);
}

#[cfg(test)]
mod tests {
use super::{CryptoHash, NeardCmd, NeardSubCommand, VerifyProofError, VerifyProofSubCommand};
Expand Down
17 changes: 17 additions & 0 deletions scripts/set_kernel_params.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# This script sets specific sysctl parameters for running a validator.
# Run it as: sudo ./set_kernel_params.sh

# Increase maximum read and write buffer sizes
sysctl -w net.core.rmem_max=8388608
sysctl -w net.core.wmem_max=8388608

# Configure TCP read and write memory parameters
sysctl -w net.ipv4.tcp_rmem="4096 87380 8388608"
sysctl -w net.ipv4.tcp_wmem="4096 16384 8388608"

# Disable slow start after idle
sysctl -w net.ipv4.tcp_slow_start_after_idle=0

echo "Network settings have been updated."

0 comments on commit 53503e8

Please sign in to comment.