From f8639db35a96a580bdf3e9268227d3ed425ee144 Mon Sep 17 00:00:00 2001 From: pdliyan Date: Sun, 25 Jun 2023 21:41:38 +0800 Subject: [PATCH 1/9] add memlock remove. --- {{project-name}}/Cargo.toml | 1 + {{project-name}}/src/main.rs | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/{{project-name}}/Cargo.toml b/{{project-name}}/Cargo.toml index d06a00f..9f6cdd2 100644 --- a/{{project-name}}/Cargo.toml +++ b/{{project-name}}/Cargo.toml @@ -18,6 +18,7 @@ libc = "0.2" {%- endif %} log = "0.4" tokio = { version = "1.25", features = ["macros", "rt", "rt-multi-thread", "net", "signal"] } +rlimit = "0.9.1" [[bin]] name = "{{project-name}}" diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index 5ef5328..7f16d7d 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -46,6 +46,7 @@ use aya_log::BpfLogger; use clap::Parser; {% endif -%} use log::{info, warn}; +use rlimit::Resource; use tokio::signal; {% if program_types_with_opts contains program_type -%} @@ -71,6 +72,14 @@ async fn main() -> Result<(), anyhow::Error> { {% endif %} env_logger::init(); + if !Resource::MEMLOCK + .set(rlimit::INFINITY, rlimit::INFINITY) + .is_ok() + { + warn!("cannot remove mem lock"); + } + + // This will include your eBPF object file as raw bytes at compile-time and load it at // runtime. This approach is recommended for most real-world use cases. If you would // like to specify the eBPF program at runtime rather than at compile-time, you can From bc2db6b11279c79bc672bf5ded39d4dca2f69821 Mon Sep 17 00:00:00 2001 From: pdliyan Date: Mon, 26 Jun 2023 14:35:59 +0800 Subject: [PATCH 2/9] use libc instead of rlimit. --- {{project-name}}/Cargo.toml | 5 +---- {{project-name}}/src/main.rs | 11 +++++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/{{project-name}}/Cargo.toml b/{{project-name}}/Cargo.toml index 9f6cdd2..56e094d 100644 --- a/{{project-name}}/Cargo.toml +++ b/{{project-name}}/Cargo.toml @@ -13,12 +13,9 @@ clap = { version = "4.1", features = ["derive"] } {{project-name}}-common = { path = "../{{project-name}}-common", features = ["user"] } anyhow = "1" env_logger = "0.10" -{%- if program_type == "uprobe" %} -libc = "0.2" -{%- endif %} log = "0.4" tokio = { version = "1.25", features = ["macros", "rt", "rt-multi-thread", "net", "signal"] } -rlimit = "0.9.1" +libc = "0.2" [[bin]] name = "{{project-name}}" diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index 7f16d7d..7169a78 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -72,14 +72,17 @@ async fn main() -> Result<(), anyhow::Error> { {% endif %} env_logger::init(); - if !Resource::MEMLOCK - .set(rlimit::INFINITY, rlimit::INFINITY) - .is_ok() - { + let rlim = libc::rlimit{ + rlim_cur: libc::RLIM_INFINITY, + rlim_max: libc::RLIM_INFINITY, + }; + let ret = unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &rlim) }; + if !ret == 0{ warn!("cannot remove mem lock"); } + // This will include your eBPF object file as raw bytes at compile-time and load it at // runtime. This approach is recommended for most real-world use cases. If you would // like to specify the eBPF program at runtime rather than at compile-time, you can From f3eb374c3ebcd359cf37d3496375b6b08794165c Mon Sep 17 00:00:00 2001 From: pdliyan Date: Mon, 26 Jun 2023 14:45:32 +0800 Subject: [PATCH 3/9] remove rlimit use. --- {{project-name}}/src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index 7169a78..87d0cfb 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -46,7 +46,6 @@ use aya_log::BpfLogger; use clap::Parser; {% endif -%} use log::{info, warn}; -use rlimit::Resource; use tokio::signal; {% if program_types_with_opts contains program_type -%} From d22827603dcd31b2adca4c701204cc991ed0cf7f Mon Sep 17 00:00:00 2001 From: pdliyan Date: Mon, 26 Jun 2023 15:07:14 +0800 Subject: [PATCH 4/9] remove white line. --- {{project-name}}/src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index 87d0cfb..192112f 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -81,7 +81,6 @@ async fn main() -> Result<(), anyhow::Error> { } - // This will include your eBPF object file as raw bytes at compile-time and load it at // runtime. This approach is recommended for most real-world use cases. If you would // like to specify the eBPF program at runtime rather than at compile-time, you can From 0f4584ef7a712e56cba416fade37b76b799bf137 Mon Sep 17 00:00:00 2001 From: pdliyan Date: Mon, 26 Jun 2023 15:12:32 +0800 Subject: [PATCH 5/9] remove whitespace. --- {{project-name}}/src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index 192112f..181927b 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -80,7 +80,6 @@ async fn main() -> Result<(), anyhow::Error> { warn!("cannot remove mem lock"); } - // This will include your eBPF object file as raw bytes at compile-time and load it at // runtime. This approach is recommended for most real-world use cases. If you would // like to specify the eBPF program at runtime rather than at compile-time, you can From 506ce7ec3dcb61fff1fa567752648806fc30cbc3 Mon Sep 17 00:00:00 2001 From: pdliyan Date: Tue, 27 Jun 2023 14:47:37 +0800 Subject: [PATCH 6/9] make warn info more informative. --- {{project-name}}/Cargo.toml | 2 +- {{project-name}}/src/main.rs | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/{{project-name}}/Cargo.toml b/{{project-name}}/Cargo.toml index 56e094d..a869e6e 100644 --- a/{{project-name}}/Cargo.toml +++ b/{{project-name}}/Cargo.toml @@ -13,9 +13,9 @@ clap = { version = "4.1", features = ["derive"] } {{project-name}}-common = { path = "../{{project-name}}-common", features = ["user"] } anyhow = "1" env_logger = "0.10" +libc = "0.2" log = "0.4" tokio = { version = "1.25", features = ["macros", "rt", "rt-multi-thread", "net", "signal"] } -libc = "0.2" [[bin]] name = "{{project-name}}" diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index 181927b..103b6eb 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -71,13 +71,14 @@ async fn main() -> Result<(), anyhow::Error> { {% endif %} env_logger::init(); - let rlim = libc::rlimit{ + // Allow current process to lock memory for eBPF resources. + let rlim = libc::rlimit { rlim_cur: libc::RLIM_INFINITY, rlim_max: libc::RLIM_INFINITY, }; let ret = unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &rlim) }; - if !ret == 0{ - warn!("cannot remove mem lock"); + if ret != 0 { + warn!("remove limit on locked memory failed, ret is: {}", ret); } // This will include your eBPF object file as raw bytes at compile-time and load it at From c6ece63d979d21a43f10a5321e71308a65d3c394 Mon Sep 17 00:00:00 2001 From: liyan Date: Tue, 4 Jul 2023 15:28:47 +0800 Subject: [PATCH 7/9] Update main.rs --- {{project-name}}/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index 103b6eb..ee29b64 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -78,7 +78,7 @@ async fn main() -> Result<(), anyhow::Error> { }; let ret = unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &rlim) }; if ret != 0 { - warn!("remove limit on locked memory failed, ret is: {}", ret); + debug!("remove limit on locked memory failed, ret is: {}", ret); } // This will include your eBPF object file as raw bytes at compile-time and load it at From 2aeba1b85d967041672ac9dcfda421e0e6f5dc3f Mon Sep 17 00:00:00 2001 From: liyan Date: Tue, 4 Jul 2023 15:29:22 +0800 Subject: [PATCH 8/9] Update {{project-name}}/src/main.rs Co-authored-by: Alessandro Decina --- {{project-name}}/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index ee29b64..861cf51 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -71,7 +71,8 @@ async fn main() -> Result<(), anyhow::Error> { {% endif %} env_logger::init(); - // Allow current process to lock memory for eBPF resources. + // Bump the memlock rlimit. This is needed for older kernels that don't use the + // new memcg based accounting, see https://lwn.net/Articles/837122/ let rlim = libc::rlimit { rlim_cur: libc::RLIM_INFINITY, rlim_max: libc::RLIM_INFINITY, From 288919127cb300899758557736d70b239ded64f8 Mon Sep 17 00:00:00 2001 From: liyan Date: Tue, 4 Jul 2023 15:37:17 +0800 Subject: [PATCH 9/9] Update main.rs --- {{project-name}}/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index 861cf51..ab87e39 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -45,7 +45,7 @@ use aya_log::BpfLogger; {% if program_types_with_opts contains program_type -%} use clap::Parser; {% endif -%} -use log::{info, warn}; +use log::{info, warn, debug}; use tokio::signal; {% if program_types_with_opts contains program_type -%}