Skip to content

Commit

Permalink
aya: Fix program loading on kernels with a patch > 255
Browse files Browse the repository at this point in the history
  • Loading branch information
nrxus committed Sep 22, 2023
1 parent 42fd82e commit c01bc92
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
10 changes: 2 additions & 8 deletions aya/src/programs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,14 +617,8 @@ fn load_program<T: Link>(
},
) = obj;

let target_kernel_version = kernel_version.unwrap_or_else(|| {
let KernelVersion {
major,
minor,
patch,
} = KernelVersion::current().unwrap();
(u32::from(major) << 16) + (u32::from(minor) << 8) + u32::from(patch)
});
let target_kernel_version =
kernel_version.unwrap_or_else(|| KernelVersion::current().unwrap().code());

let prog_name = if let Some(name) = name {
let mut name = name.clone();
Expand Down
14 changes: 14 additions & 0 deletions aya/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ impl KernelVersion {
kernel_version
}

/// The equivalent of LINUX_VERSION_CODE
pub(crate) fn code(self) -> u32 {
let Self {
major,
minor,
patch,
} = self;
// clamp patch to 255 to avoid it overflowing into the minor version.
//
// See: https://github.com/torvalds/linux/commit/9b82f13e7ef316cdc0a8858f1349f4defce3f9e0
let patch = patch.min(255);
(u32::from(major) << 16) + (u32::from(minor) << 8) + u32::from(patch)
}

// This is ported from https://github.com/torvalds/linux/blob/3f01e9f/tools/lib/bpf/libbpf_probes.c#L21-L101.

fn get_ubuntu_kernel_version() -> Result<Option<Self>, CurrentKernelVersionError> {
Expand Down

0 comments on commit c01bc92

Please sign in to comment.