From a35a513802788bdf3f75959eff10921e9abc92e0 Mon Sep 17 00:00:00 2001 From: tyrone-wu Date: Mon, 8 Jul 2024 00:36:34 +0000 Subject: [PATCH] aya-obj: conversion from u32 & impl display for link type Adds conversion from u32 to link type enum. Also implements display for condensed metadata output. So far, this is still WIP since this commit/PR also plans to add better access to `bpf_link_info__bindgen_ty_1` metadata. --- aya-obj/src/lib.rs | 1 + aya-obj/src/links.rs | 53 ++++++++++++++++++++++++++++++++++++ xtask/public-api/aya-obj.txt | 11 ++++++++ 3 files changed, 65 insertions(+) create mode 100644 aya-obj/src/links.rs diff --git a/aya-obj/src/lib.rs b/aya-obj/src/lib.rs index ea0e5670..c6e3a45c 100644 --- a/aya-obj/src/lib.rs +++ b/aya-obj/src/lib.rs @@ -87,6 +87,7 @@ mod std { pub mod btf; pub mod generated; +pub mod links; pub mod maps; pub mod obj; pub mod programs; diff --git a/aya-obj/src/links.rs b/aya-obj/src/links.rs new file mode 100644 index 00000000..e197d8b3 --- /dev/null +++ b/aya-obj/src/links.rs @@ -0,0 +1,53 @@ +//! Link struct and type bindings. + +use core::fmt::Display; + +use crate::generated::bpf_link_type::{self, *}; + +impl From for bpf_link_type { + fn from(link_type: u32) -> Self { + match link_type { + x if x == BPF_LINK_TYPE_UNSPEC as u32 => BPF_LINK_TYPE_UNSPEC, + x if x == BPF_LINK_TYPE_RAW_TRACEPOINT as u32 => BPF_LINK_TYPE_RAW_TRACEPOINT, + x if x == BPF_LINK_TYPE_TRACING as u32 => BPF_LINK_TYPE_TRACING, + x if x == BPF_LINK_TYPE_CGROUP as u32 => BPF_LINK_TYPE_CGROUP, + x if x == BPF_LINK_TYPE_ITER as u32 => BPF_LINK_TYPE_ITER, + x if x == BPF_LINK_TYPE_NETNS as u32 => BPF_LINK_TYPE_NETNS, + x if x == BPF_LINK_TYPE_XDP as u32 => BPF_LINK_TYPE_XDP, + x if x == BPF_LINK_TYPE_PERF_EVENT as u32 => BPF_LINK_TYPE_PERF_EVENT, + x if x == BPF_LINK_TYPE_KPROBE_MULTI as u32 => BPF_LINK_TYPE_KPROBE_MULTI, + x if x == BPF_LINK_TYPE_STRUCT_OPS as u32 => BPF_LINK_TYPE_STRUCT_OPS, + x if x == BPF_LINK_TYPE_NETFILTER as u32 => BPF_LINK_TYPE_NETFILTER, + x if x == BPF_LINK_TYPE_TCX as u32 => BPF_LINK_TYPE_TCX, + x if x == BPF_LINK_TYPE_UPROBE_MULTI as u32 => BPF_LINK_TYPE_UPROBE_MULTI, + x if x == BPF_LINK_TYPE_NETKIT as u32 => BPF_LINK_TYPE_NETKIT, + _ => __MAX_BPF_LINK_TYPE, + } + } +} + +impl Display for bpf_link_type { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!( + f, + "{}", + match self { + BPF_LINK_TYPE_UNSPEC => "Unspec", + BPF_LINK_TYPE_RAW_TRACEPOINT => "RawTracePoint", + BPF_LINK_TYPE_TRACING => "Tracing", + BPF_LINK_TYPE_CGROUP => "Cgroup", + BPF_LINK_TYPE_ITER => "Iter", + BPF_LINK_TYPE_NETNS => "NetNs", + BPF_LINK_TYPE_XDP => "Xdp", + BPF_LINK_TYPE_PERF_EVENT => "PerfEvent", + BPF_LINK_TYPE_KPROBE_MULTI => "KProbeMulti", + BPF_LINK_TYPE_STRUCT_OPS => "StructOps", + BPF_LINK_TYPE_NETFILTER => "Netfilter", + BPF_LINK_TYPE_TCX => "Tcx", + BPF_LINK_TYPE_UPROBE_MULTI => "UProbeMulti", + BPF_LINK_TYPE_NETKIT => "NetKit", + __MAX_BPF_LINK_TYPE => "MaxLinkType", + } + ) + } +} diff --git a/xtask/public-api/aya-obj.txt b/xtask/public-api/aya-obj.txt index 22bfe6e7..c64e907b 100644 --- a/xtask/public-api/aya-obj.txt +++ b/xtask/public-api/aya-obj.txt @@ -1523,8 +1523,12 @@ pub fn aya_obj::generated::bpf_link_type::clone(&self) -> aya_obj::generated::bp impl core::cmp::Eq for aya_obj::generated::bpf_link_type impl core::cmp::PartialEq for aya_obj::generated::bpf_link_type pub fn aya_obj::generated::bpf_link_type::eq(&self, other: &aya_obj::generated::bpf_link_type) -> bool +impl core::convert::From for aya_obj::generated::bpf_link_type +pub fn aya_obj::generated::bpf_link_type::from(link_type: u32) -> Self impl core::fmt::Debug for aya_obj::generated::bpf_link_type pub fn aya_obj::generated::bpf_link_type::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result +impl core::fmt::Display for aya_obj::generated::bpf_link_type +pub fn aya_obj::generated::bpf_link_type::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result impl core::hash::Hash for aya_obj::generated::bpf_link_type pub fn aya_obj::generated::bpf_link_type::hash<__H: core::hash::Hasher>(&self, state: &mut __H) impl core::marker::Copy for aya_obj::generated::bpf_link_type @@ -1547,6 +1551,8 @@ impl alloc::borrow::ToOwned for aya_obj::generated::bpf_link_type where T: co pub type aya_obj::generated::bpf_link_type::Owned = T pub fn aya_obj::generated::bpf_link_type::clone_into(&self, target: &mut T) pub fn aya_obj::generated::bpf_link_type::to_owned(&self) -> T +impl alloc::string::ToString for aya_obj::generated::bpf_link_type where T: core::fmt::Display + core::marker::Sized +pub fn aya_obj::generated::bpf_link_type::to_string(&self) -> alloc::string::String impl core::any::Any for aya_obj::generated::bpf_link_type where T: 'static + core::marker::Sized pub fn aya_obj::generated::bpf_link_type::type_id(&self) -> core::any::TypeId impl core::borrow::Borrow for aya_obj::generated::bpf_link_type where T: core::marker::Sized @@ -1605,6 +1611,8 @@ pub type aya_obj::generated::bpf_map_type::Error = aya_obj::maps::InvalidMapType pub fn aya_obj::generated::bpf_map_type::try_from(map_type: u32) -> core::result::Result impl core::fmt::Debug for aya_obj::generated::bpf_map_type pub fn aya_obj::generated::bpf_map_type::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result +impl core::fmt::Display for aya_obj::generated::bpf_map_type +pub fn aya_obj::generated::bpf_map_type::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result impl core::hash::Hash for aya_obj::generated::bpf_map_type pub fn aya_obj::generated::bpf_map_type::hash<__H: core::hash::Hasher>(&self, state: &mut __H) impl core::marker::Copy for aya_obj::generated::bpf_map_type @@ -1627,6 +1635,8 @@ impl alloc::borrow::ToOwned for aya_obj::generated::bpf_map_type where T: cor pub type aya_obj::generated::bpf_map_type::Owned = T pub fn aya_obj::generated::bpf_map_type::clone_into(&self, target: &mut T) pub fn aya_obj::generated::bpf_map_type::to_owned(&self) -> T +impl alloc::string::ToString for aya_obj::generated::bpf_map_type where T: core::fmt::Display + core::marker::Sized +pub fn aya_obj::generated::bpf_map_type::to_string(&self) -> alloc::string::String impl core::any::Any for aya_obj::generated::bpf_map_type where T: 'static + core::marker::Sized pub fn aya_obj::generated::bpf_map_type::type_id(&self) -> core::any::TypeId impl core::borrow::Borrow for aya_obj::generated::bpf_map_type where T: core::marker::Sized @@ -6270,6 +6280,7 @@ pub type aya_obj::generated::_bindgen_ty_7 = core::ffi::c_uint pub type aya_obj::generated::_bindgen_ty_8 = core::ffi::c_uint pub type aya_obj::generated::_bindgen_ty_9 = core::ffi::c_uint pub type aya_obj::generated::_bindgen_ty_92 = core::ffi::c_uint +pub mod aya_obj::links pub mod aya_obj::maps pub enum aya_obj::maps::Map pub aya_obj::maps::Map::Btf(aya_obj::maps::BtfMap)