From f7cad4436fa94cc24bd75ccce41deee9dd81911b Mon Sep 17 00:00:00 2001 From: Kevin Ji <1146876+kevinji@users.noreply.github.com> Date: Thu, 28 Mar 2024 14:52:11 -0700 Subject: [PATCH 1/4] aya-obj: Include VETH_INFO_PEER and ifaddrmsg --- aya-obj/include/linux_wrapper.h | 1 + .../src/generated/linux_bindings_aarch64.rs | 19 +++++ aya-obj/src/generated/linux_bindings_armv7.rs | 19 +++++ .../src/generated/linux_bindings_riscv64.rs | 19 +++++ .../src/generated/linux_bindings_x86_64.rs | 19 +++++ xtask/public-api/aya-obj.txt | 81 +++++++++++++++++++ xtask/src/codegen/aya.rs | 2 + 7 files changed, 160 insertions(+) diff --git a/aya-obj/include/linux_wrapper.h b/aya-obj/include/linux_wrapper.h index 8be2459ac..d10e325a0 100644 --- a/aya-obj/include/linux_wrapper.h +++ b/aya-obj/include/linux_wrapper.h @@ -6,6 +6,7 @@ #include #include #include +#include /* workaround the fact that bindgen can't parse the IOC macros */ int AYA_PERF_EVENT_IOC_ENABLE = PERF_EVENT_IOC_ENABLE; diff --git a/aya-obj/src/generated/linux_bindings_aarch64.rs b/aya-obj/src/generated/linux_bindings_aarch64.rs index 95b7e0d87..e10bc03ea 100644 --- a/aya-obj/src/generated/linux_bindings_aarch64.rs +++ b/aya-obj/src/generated/linux_bindings_aarch64.rs @@ -2384,6 +2384,15 @@ pub const __TCA_BPF_MAX: _bindgen_ty_152 = 12; pub type _bindgen_ty_152 = ::core::ffi::c_uint; #[repr(C)] #[derive(Debug, Copy, Clone)] +pub struct ifaddrmsg { + pub ifa_family: __u8, + pub ifa_prefixlen: __u8, + pub ifa_flags: __u8, + pub ifa_scope: __u8, + pub ifa_index: __u32, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] pub struct ifinfomsg { pub ifi_family: ::core::ffi::c_uchar, pub __ifi_pad: ::core::ffi::c_uchar, @@ -2421,6 +2430,16 @@ pub const TCA_EGRESS_BLOCK: _bindgen_ty_172 = 14; pub const TCA_DUMP_FLAGS: _bindgen_ty_172 = 15; pub const __TCA_MAX: _bindgen_ty_172 = 16; pub type _bindgen_ty_172 = ::core::ffi::c_uint; +pub const VETH_INFO_UNSPEC: _bindgen_ty_175 = _bindgen_ty_175::VETH_INFO_UNSPEC; +pub const VETH_INFO_PEER: _bindgen_ty_175 = _bindgen_ty_175::VETH_INFO_PEER; +pub const __VETH_INFO_MAX: _bindgen_ty_175 = _bindgen_ty_175::__VETH_INFO_MAX; +#[repr(u32)] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub enum _bindgen_ty_175 { + VETH_INFO_UNSPEC = 0, + VETH_INFO_PEER = 1, + __VETH_INFO_MAX = 2, +} pub const AYA_PERF_EVENT_IOC_ENABLE: ::core::ffi::c_int = 9216; pub const AYA_PERF_EVENT_IOC_DISABLE: ::core::ffi::c_int = 9217; pub const AYA_PERF_EVENT_IOC_SET_BPF: ::core::ffi::c_int = 1074013192; diff --git a/aya-obj/src/generated/linux_bindings_armv7.rs b/aya-obj/src/generated/linux_bindings_armv7.rs index 4dcfeb80a..8090d0b62 100644 --- a/aya-obj/src/generated/linux_bindings_armv7.rs +++ b/aya-obj/src/generated/linux_bindings_armv7.rs @@ -2384,6 +2384,15 @@ pub const __TCA_BPF_MAX: _bindgen_ty_152 = 12; pub type _bindgen_ty_152 = ::core::ffi::c_uint; #[repr(C)] #[derive(Debug, Copy, Clone)] +pub struct ifaddrmsg { + pub ifa_family: __u8, + pub ifa_prefixlen: __u8, + pub ifa_flags: __u8, + pub ifa_scope: __u8, + pub ifa_index: __u32, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] pub struct ifinfomsg { pub ifi_family: ::core::ffi::c_uchar, pub __ifi_pad: ::core::ffi::c_uchar, @@ -2421,6 +2430,16 @@ pub const TCA_EGRESS_BLOCK: _bindgen_ty_172 = 14; pub const TCA_DUMP_FLAGS: _bindgen_ty_172 = 15; pub const __TCA_MAX: _bindgen_ty_172 = 16; pub type _bindgen_ty_172 = ::core::ffi::c_uint; +pub const VETH_INFO_UNSPEC: _bindgen_ty_175 = _bindgen_ty_175::VETH_INFO_UNSPEC; +pub const VETH_INFO_PEER: _bindgen_ty_175 = _bindgen_ty_175::VETH_INFO_PEER; +pub const __VETH_INFO_MAX: _bindgen_ty_175 = _bindgen_ty_175::__VETH_INFO_MAX; +#[repr(u32)] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub enum _bindgen_ty_175 { + VETH_INFO_UNSPEC = 0, + VETH_INFO_PEER = 1, + __VETH_INFO_MAX = 2, +} pub const AYA_PERF_EVENT_IOC_ENABLE: ::core::ffi::c_int = 9216; pub const AYA_PERF_EVENT_IOC_DISABLE: ::core::ffi::c_int = 9217; pub const AYA_PERF_EVENT_IOC_SET_BPF: ::core::ffi::c_int = 1074013192; diff --git a/aya-obj/src/generated/linux_bindings_riscv64.rs b/aya-obj/src/generated/linux_bindings_riscv64.rs index 95b7e0d87..e10bc03ea 100644 --- a/aya-obj/src/generated/linux_bindings_riscv64.rs +++ b/aya-obj/src/generated/linux_bindings_riscv64.rs @@ -2384,6 +2384,15 @@ pub const __TCA_BPF_MAX: _bindgen_ty_152 = 12; pub type _bindgen_ty_152 = ::core::ffi::c_uint; #[repr(C)] #[derive(Debug, Copy, Clone)] +pub struct ifaddrmsg { + pub ifa_family: __u8, + pub ifa_prefixlen: __u8, + pub ifa_flags: __u8, + pub ifa_scope: __u8, + pub ifa_index: __u32, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] pub struct ifinfomsg { pub ifi_family: ::core::ffi::c_uchar, pub __ifi_pad: ::core::ffi::c_uchar, @@ -2421,6 +2430,16 @@ pub const TCA_EGRESS_BLOCK: _bindgen_ty_172 = 14; pub const TCA_DUMP_FLAGS: _bindgen_ty_172 = 15; pub const __TCA_MAX: _bindgen_ty_172 = 16; pub type _bindgen_ty_172 = ::core::ffi::c_uint; +pub const VETH_INFO_UNSPEC: _bindgen_ty_175 = _bindgen_ty_175::VETH_INFO_UNSPEC; +pub const VETH_INFO_PEER: _bindgen_ty_175 = _bindgen_ty_175::VETH_INFO_PEER; +pub const __VETH_INFO_MAX: _bindgen_ty_175 = _bindgen_ty_175::__VETH_INFO_MAX; +#[repr(u32)] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub enum _bindgen_ty_175 { + VETH_INFO_UNSPEC = 0, + VETH_INFO_PEER = 1, + __VETH_INFO_MAX = 2, +} pub const AYA_PERF_EVENT_IOC_ENABLE: ::core::ffi::c_int = 9216; pub const AYA_PERF_EVENT_IOC_DISABLE: ::core::ffi::c_int = 9217; pub const AYA_PERF_EVENT_IOC_SET_BPF: ::core::ffi::c_int = 1074013192; diff --git a/aya-obj/src/generated/linux_bindings_x86_64.rs b/aya-obj/src/generated/linux_bindings_x86_64.rs index 95b7e0d87..e10bc03ea 100644 --- a/aya-obj/src/generated/linux_bindings_x86_64.rs +++ b/aya-obj/src/generated/linux_bindings_x86_64.rs @@ -2384,6 +2384,15 @@ pub const __TCA_BPF_MAX: _bindgen_ty_152 = 12; pub type _bindgen_ty_152 = ::core::ffi::c_uint; #[repr(C)] #[derive(Debug, Copy, Clone)] +pub struct ifaddrmsg { + pub ifa_family: __u8, + pub ifa_prefixlen: __u8, + pub ifa_flags: __u8, + pub ifa_scope: __u8, + pub ifa_index: __u32, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] pub struct ifinfomsg { pub ifi_family: ::core::ffi::c_uchar, pub __ifi_pad: ::core::ffi::c_uchar, @@ -2421,6 +2430,16 @@ pub const TCA_EGRESS_BLOCK: _bindgen_ty_172 = 14; pub const TCA_DUMP_FLAGS: _bindgen_ty_172 = 15; pub const __TCA_MAX: _bindgen_ty_172 = 16; pub type _bindgen_ty_172 = ::core::ffi::c_uint; +pub const VETH_INFO_UNSPEC: _bindgen_ty_175 = _bindgen_ty_175::VETH_INFO_UNSPEC; +pub const VETH_INFO_PEER: _bindgen_ty_175 = _bindgen_ty_175::VETH_INFO_PEER; +pub const __VETH_INFO_MAX: _bindgen_ty_175 = _bindgen_ty_175::__VETH_INFO_MAX; +#[repr(u32)] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub enum _bindgen_ty_175 { + VETH_INFO_UNSPEC = 0, + VETH_INFO_PEER = 1, + __VETH_INFO_MAX = 2, +} pub const AYA_PERF_EVENT_IOC_ENABLE: ::core::ffi::c_int = 9216; pub const AYA_PERF_EVENT_IOC_DISABLE: ::core::ffi::c_int = 9217; pub const AYA_PERF_EVENT_IOC_SET_BPF: ::core::ffi::c_int = 1074013192; diff --git a/xtask/public-api/aya-obj.txt b/xtask/public-api/aya-obj.txt index e09efd3ae..596471236 100644 --- a/xtask/public-api/aya-obj.txt +++ b/xtask/public-api/aya-obj.txt @@ -1321,6 +1321,47 @@ pub const aya_obj::generated::bpf_core_relo_kind::BPF_CORE_TYPE_ID_TARGET: aya_o pub const aya_obj::generated::bpf_core_relo_kind::BPF_CORE_TYPE_MATCHES: aya_obj::generated::bpf_core_relo_kind::Type = 12u32 pub const aya_obj::generated::bpf_core_relo_kind::BPF_CORE_TYPE_SIZE: aya_obj::generated::bpf_core_relo_kind::Type = 9u32 pub type aya_obj::generated::bpf_core_relo_kind::Type = core::ffi::c_uint +#[repr(u32)] pub enum aya_obj::generated::_bindgen_ty_175 +pub aya_obj::generated::_bindgen_ty_175::VETH_INFO_PEER = 1 +pub aya_obj::generated::_bindgen_ty_175::VETH_INFO_UNSPEC = 0 +pub aya_obj::generated::_bindgen_ty_175::__VETH_INFO_MAX = 2 +impl core::clone::Clone for aya_obj::generated::_bindgen_ty_175 +pub fn aya_obj::generated::_bindgen_ty_175::clone(&self) -> aya_obj::generated::_bindgen_ty_175 +impl core::cmp::Eq for aya_obj::generated::_bindgen_ty_175 +impl core::cmp::PartialEq for aya_obj::generated::_bindgen_ty_175 +pub fn aya_obj::generated::_bindgen_ty_175::eq(&self, other: &aya_obj::generated::_bindgen_ty_175) -> bool +impl core::fmt::Debug for aya_obj::generated::_bindgen_ty_175 +pub fn aya_obj::generated::_bindgen_ty_175::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result +impl core::hash::Hash for aya_obj::generated::_bindgen_ty_175 +pub fn aya_obj::generated::_bindgen_ty_175::hash<__H: core::hash::Hasher>(&self, state: &mut __H) +impl core::marker::Copy for aya_obj::generated::_bindgen_ty_175 +impl core::marker::StructuralPartialEq for aya_obj::generated::_bindgen_ty_175 +impl core::marker::Freeze for aya_obj::generated::_bindgen_ty_175 +impl core::marker::Send for aya_obj::generated::_bindgen_ty_175 +impl core::marker::Sync for aya_obj::generated::_bindgen_ty_175 +impl core::marker::Unpin for aya_obj::generated::_bindgen_ty_175 +impl core::panic::unwind_safe::RefUnwindSafe for aya_obj::generated::_bindgen_ty_175 +impl core::panic::unwind_safe::UnwindSafe for aya_obj::generated::_bindgen_ty_175 +impl core::convert::Into for aya_obj::generated::_bindgen_ty_175 where U: core::convert::From +pub fn aya_obj::generated::_bindgen_ty_175::into(self) -> U +impl core::convert::TryFrom for aya_obj::generated::_bindgen_ty_175 where U: core::convert::Into +pub type aya_obj::generated::_bindgen_ty_175::Error = core::convert::Infallible +pub fn aya_obj::generated::_bindgen_ty_175::try_from(value: U) -> core::result::Result>::Error> +impl core::convert::TryInto for aya_obj::generated::_bindgen_ty_175 where U: core::convert::TryFrom +pub type aya_obj::generated::_bindgen_ty_175::Error = >::Error +pub fn aya_obj::generated::_bindgen_ty_175::try_into(self) -> core::result::Result>::Error> +impl alloc::borrow::ToOwned for aya_obj::generated::_bindgen_ty_175 where T: core::clone::Clone +pub type aya_obj::generated::_bindgen_ty_175::Owned = T +pub fn aya_obj::generated::_bindgen_ty_175::clone_into(&self, target: &mut T) +pub fn aya_obj::generated::_bindgen_ty_175::to_owned(&self) -> T +impl core::any::Any for aya_obj::generated::_bindgen_ty_175 where T: 'static + core::marker::Sized +pub fn aya_obj::generated::_bindgen_ty_175::type_id(&self) -> core::any::TypeId +impl core::borrow::Borrow for aya_obj::generated::_bindgen_ty_175 where T: core::marker::Sized +pub fn aya_obj::generated::_bindgen_ty_175::borrow(&self) -> &T +impl core::borrow::BorrowMut for aya_obj::generated::_bindgen_ty_175 where T: core::marker::Sized +pub fn aya_obj::generated::_bindgen_ty_175::borrow_mut(&mut self) -> &mut T +impl core::convert::From for aya_obj::generated::_bindgen_ty_175 +pub fn aya_obj::generated::_bindgen_ty_175::from(t: T) -> T #[repr(u32)] pub enum aya_obj::generated::bpf_attach_type pub aya_obj::generated::bpf_attach_type::BPF_CGROUP_DEVICE = 6 pub aya_obj::generated::bpf_attach_type::BPF_CGROUP_GETSOCKOPT = 21 @@ -5680,6 +5721,43 @@ impl core::borrow::BorrowMut for aya_obj::generated::btf_var_secinfo where pub fn aya_obj::generated::btf_var_secinfo::borrow_mut(&mut self) -> &mut T impl core::convert::From for aya_obj::generated::btf_var_secinfo pub fn aya_obj::generated::btf_var_secinfo::from(t: T) -> T +#[repr(C)] pub struct aya_obj::generated::ifaddrmsg +pub aya_obj::generated::ifaddrmsg::ifa_family: aya_obj::generated::__u8 +pub aya_obj::generated::ifaddrmsg::ifa_flags: aya_obj::generated::__u8 +pub aya_obj::generated::ifaddrmsg::ifa_index: aya_obj::generated::__u32 +pub aya_obj::generated::ifaddrmsg::ifa_prefixlen: aya_obj::generated::__u8 +pub aya_obj::generated::ifaddrmsg::ifa_scope: aya_obj::generated::__u8 +impl core::clone::Clone for aya_obj::generated::ifaddrmsg +pub fn aya_obj::generated::ifaddrmsg::clone(&self) -> aya_obj::generated::ifaddrmsg +impl core::fmt::Debug for aya_obj::generated::ifaddrmsg +pub fn aya_obj::generated::ifaddrmsg::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result +impl core::marker::Copy for aya_obj::generated::ifaddrmsg +impl core::marker::Freeze for aya_obj::generated::ifaddrmsg +impl core::marker::Send for aya_obj::generated::ifaddrmsg +impl core::marker::Sync for aya_obj::generated::ifaddrmsg +impl core::marker::Unpin for aya_obj::generated::ifaddrmsg +impl core::panic::unwind_safe::RefUnwindSafe for aya_obj::generated::ifaddrmsg +impl core::panic::unwind_safe::UnwindSafe for aya_obj::generated::ifaddrmsg +impl core::convert::Into for aya_obj::generated::ifaddrmsg where U: core::convert::From +pub fn aya_obj::generated::ifaddrmsg::into(self) -> U +impl core::convert::TryFrom for aya_obj::generated::ifaddrmsg where U: core::convert::Into +pub type aya_obj::generated::ifaddrmsg::Error = core::convert::Infallible +pub fn aya_obj::generated::ifaddrmsg::try_from(value: U) -> core::result::Result>::Error> +impl core::convert::TryInto for aya_obj::generated::ifaddrmsg where U: core::convert::TryFrom +pub type aya_obj::generated::ifaddrmsg::Error = >::Error +pub fn aya_obj::generated::ifaddrmsg::try_into(self) -> core::result::Result>::Error> +impl alloc::borrow::ToOwned for aya_obj::generated::ifaddrmsg where T: core::clone::Clone +pub type aya_obj::generated::ifaddrmsg::Owned = T +pub fn aya_obj::generated::ifaddrmsg::clone_into(&self, target: &mut T) +pub fn aya_obj::generated::ifaddrmsg::to_owned(&self) -> T +impl core::any::Any for aya_obj::generated::ifaddrmsg where T: 'static + core::marker::Sized +pub fn aya_obj::generated::ifaddrmsg::type_id(&self) -> core::any::TypeId +impl core::borrow::Borrow for aya_obj::generated::ifaddrmsg where T: core::marker::Sized +pub fn aya_obj::generated::ifaddrmsg::borrow(&self) -> &T +impl core::borrow::BorrowMut for aya_obj::generated::ifaddrmsg where T: core::marker::Sized +pub fn aya_obj::generated::ifaddrmsg::borrow_mut(&mut self) -> &mut T +impl core::convert::From for aya_obj::generated::ifaddrmsg +pub fn aya_obj::generated::ifaddrmsg::from(t: T) -> T #[repr(C)] pub struct aya_obj::generated::ifinfomsg pub aya_obj::generated::ifinfomsg::__ifi_pad: core::ffi::c_uchar pub aya_obj::generated::ifinfomsg::ifi_change: core::ffi::c_uint @@ -6222,6 +6300,8 @@ pub const aya_obj::generated::TC_H_MIN_MASK: u32 = 65_535u32 pub const aya_obj::generated::TC_H_MIN_PRIORITY: u32 = 65_504u32 pub const aya_obj::generated::TC_H_ROOT: u32 = 4_294_967_295u32 pub const aya_obj::generated::TC_H_UNSPEC: u32 = 0u32 +pub const aya_obj::generated::VETH_INFO_PEER: _bindgen_ty_175::VETH_INFO_PEER +pub const aya_obj::generated::VETH_INFO_UNSPEC: _bindgen_ty_175::VETH_INFO_UNSPEC pub const aya_obj::generated::XDP_FLAGS_DRV_MODE: u32 = 4u32 pub const aya_obj::generated::XDP_FLAGS_HW_MODE: u32 = 8u32 pub const aya_obj::generated::XDP_FLAGS_MASK: u32 = 31u32 @@ -6232,6 +6312,7 @@ pub const aya_obj::generated::XDP_FLAGS_UPDATE_IF_NOEXIST: u32 = 1u32 pub const aya_obj::generated::__IFLA_XDP_MAX: aya_obj::generated::_bindgen_ty_92 = 9u32 pub const aya_obj::generated::__TCA_BPF_MAX: aya_obj::generated::_bindgen_ty_152 = 12u32 pub const aya_obj::generated::__TCA_MAX: aya_obj::generated::_bindgen_ty_172 = 16u32 +pub const aya_obj::generated::__VETH_INFO_MAX: _bindgen_ty_175::__VETH_INFO_MAX pub type aya_obj::generated::__s16 = core::ffi::c_short pub type aya_obj::generated::__s32 = core::ffi::c_int pub type aya_obj::generated::__s64 = core::ffi::c_longlong diff --git a/xtask/src/codegen/aya.rs b/xtask/src/codegen/aya.rs index be447bd59..14cc9d45f 100644 --- a/xtask/src/codegen/aya.rs +++ b/xtask/src/codegen/aya.rs @@ -103,6 +103,7 @@ fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyh "perf_type_id", "perf_event_type", // NETLINK + "ifaddrmsg", "ifinfomsg", "tcmsg", ]; @@ -155,6 +156,7 @@ fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyh "TC_H_MIN_PRIORITY", "TC_H_MIN_INGRESS", "TC_H_MIN_EGRESS", + "VETH_INFO_PEER", // Ringbuf "BPF_RINGBUF_.*", ]; From 9f28e3edfdc9d21f5367f6d2e6ed9348f417d94c Mon Sep 17 00:00:00 2001 From: Kevin Ji <1146876+kevinji@users.noreply.github.com> Date: Thu, 28 Mar 2024 15:42:15 -0700 Subject: [PATCH 2/4] aya: Add netlink functions `netlink_add_veth_pair`, `netlink_delete_link`, `netlink_set_link_down`, and `netlink_add_ip_addr` are used in setting up and tearing down a veth pair for integration tests. --- aya/src/lib.rs | 5 +- aya/src/sys/mod.rs | 7 +- aya/src/sys/netlink.rs | 190 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 191 insertions(+), 11 deletions(-) diff --git a/aya/src/lib.rs b/aya/src/lib.rs index ece5ebc91..4a066cae5 100644 --- a/aya/src/lib.rs +++ b/aya/src/lib.rs @@ -92,4 +92,7 @@ pub use bpf::*; pub use obj::btf::{Btf, BtfError}; pub use object::Endianness; #[doc(hidden)] -pub use sys::netlink_set_link_up; +pub use sys::{ + netlink_add_ip_addr, netlink_add_veth_pair, netlink_delete_link, netlink_set_link_down, + netlink_set_link_up, +}; diff --git a/aya/src/sys/mod.rs b/aya/src/sys/mod.rs index fddab712f..7d8e4682b 100644 --- a/aya/src/sys/mod.rs +++ b/aya/src/sys/mod.rs @@ -15,9 +15,12 @@ pub(crate) use bpf::*; #[cfg(test)] pub(crate) use fake::*; use libc::{pid_t, SYS_bpf, SYS_perf_event_open}; -#[doc(hidden)] -pub use netlink::netlink_set_link_up; pub(crate) use netlink::*; +#[doc(hidden)] +pub use netlink::{ + netlink_add_ip_addr, netlink_add_veth_pair, netlink_delete_link, netlink_set_link_down, + netlink_set_link_up, +}; pub(crate) use perf_event::*; use thiserror::Error; diff --git a/aya/src/sys/netlink.rs b/aya/src/sys/netlink.rs index f10c2839b..25254dd38 100644 --- a/aya/src/sys/netlink.rs +++ b/aya/src/sys/netlink.rs @@ -2,24 +2,28 @@ use std::{ collections::HashMap, ffi::CStr, io, mem, + net::Ipv4Addr, os::fd::{AsRawFd as _, BorrowedFd, FromRawFd as _, OwnedFd}, ptr, slice, }; use libc::{ - getsockname, nlattr, nlmsgerr, nlmsghdr, recv, send, setsockopt, sockaddr_nl, socket, - AF_NETLINK, AF_UNSPEC, ETH_P_ALL, IFF_UP, IFLA_XDP, NETLINK_EXT_ACK, NETLINK_ROUTE, + getsockname, nlattr, nlmsgerr, nlmsghdr, recv, send, setsockopt, sockaddr_nl, socket, AF_INET, + AF_NETLINK, AF_UNSPEC, ETH_P_ALL, IFA_F_PERMANENT, IFA_LOCAL, IFF_UP, IFLA_IFNAME, + IFLA_INFO_DATA, IFLA_INFO_KIND, IFLA_LINKINFO, IFLA_XDP, NETLINK_EXT_ACK, NETLINK_ROUTE, NLA_ALIGNTO, NLA_F_NESTED, NLA_TYPE_MASK, NLMSG_DONE, NLMSG_ERROR, NLM_F_ACK, NLM_F_CREATE, - NLM_F_DUMP, NLM_F_ECHO, NLM_F_EXCL, NLM_F_MULTI, NLM_F_REQUEST, RTM_DELTFILTER, RTM_GETTFILTER, - RTM_NEWQDISC, RTM_NEWTFILTER, RTM_SETLINK, SOCK_RAW, SOL_NETLINK, + NLM_F_DUMP, NLM_F_ECHO, NLM_F_EXCL, NLM_F_MULTI, NLM_F_REQUEST, RTM_DELLINK, RTM_DELTFILTER, + RTM_GETTFILTER, RTM_NEWADDR, RTM_NEWLINK, RTM_NEWQDISC, RTM_NEWTFILTER, RTM_SETLINK, + RT_SCOPE_UNIVERSE, SOCK_RAW, SOL_NETLINK, }; use thiserror::Error; use crate::{ generated::{ - ifinfomsg, tcmsg, IFLA_XDP_EXPECTED_FD, IFLA_XDP_FD, IFLA_XDP_FLAGS, NLMSG_ALIGNTO, - TCA_BPF_FD, TCA_BPF_FLAGS, TCA_BPF_FLAG_ACT_DIRECT, TCA_BPF_NAME, TCA_KIND, TCA_OPTIONS, - TC_H_CLSACT, TC_H_INGRESS, TC_H_MAJ_MASK, TC_H_UNSPEC, XDP_FLAGS_REPLACE, + ifaddrmsg, ifinfomsg, tcmsg, IFLA_XDP_EXPECTED_FD, IFLA_XDP_FD, IFLA_XDP_FLAGS, + NLMSG_ALIGNTO, TCA_BPF_FD, TCA_BPF_FLAGS, TCA_BPF_FLAG_ACT_DIRECT, TCA_BPF_NAME, TCA_KIND, + TCA_OPTIONS, TC_H_CLSACT, TC_H_INGRESS, TC_H_MAJ_MASK, TC_H_UNSPEC, VETH_INFO_PEER, + XDP_FLAGS_REPLACE, }, programs::TcAttachType, util::tc_handler_make, @@ -266,6 +270,63 @@ pub(crate) unsafe fn netlink_find_filter_with_name( Ok(filter_info) } +#[doc(hidden)] +pub unsafe fn netlink_add_veth_pair(name1: &CStr, name2: &CStr) -> Result<(), io::Error> { + let sock = NetlinkSocket::open()?; + + // Safety: Request is POD so this is safe + let mut req = mem::zeroed::(); + + let nlmsg_len = mem::size_of::() + mem::size_of::(); + req.header = nlmsghdr { + nlmsg_len: nlmsg_len as u32, + nlmsg_flags: (NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE) as u16, + nlmsg_type: RTM_NEWLINK, + nlmsg_pid: 0, + nlmsg_seq: 1, + }; + req.if_info.ifi_family = AF_UNSPEC as u8; + req.if_info.ifi_index = 0; + req.if_info.ifi_flags = 0; + req.if_info.ifi_change = 0; + + let attrs_buf = request_attributes(&mut req, nlmsg_len); + + // add IFLA_IFNAME + let ifname_len = write_attr_bytes(attrs_buf, 0, IFLA_IFNAME, name1.to_bytes_with_nul())?; + + // add IFLA_LINKINFO which includes IFLA_INFO_KIND and IFLA_INFO_DATA + let mut linkinfo = NestedAttrs::new(&mut attrs_buf[ifname_len..], IFLA_LINKINFO); + linkinfo.write_attr_bytes(IFLA_INFO_KIND, b"veth")?; + + linkinfo.write_nested_attrs(IFLA_INFO_DATA, |info_data| { + info_data.write_nested_attrs(VETH_INFO_PEER as u16, |info_peer| { + // Safety: ifinfomsg is POD so this is safe + let mut peer_if_info = mem::zeroed::(); + peer_if_info.ifi_family = AF_UNSPEC as u8; + peer_if_info.ifi_index = 0; + peer_if_info.ifi_flags = 0; + peer_if_info.ifi_change = 0; + + info_peer.write_bytes(bytes_of(&peer_if_info))?; + + // add IFLA_IFNAME + info_peer.write_attr_bytes(IFLA_IFNAME, name2.to_bytes_with_nul())?; + + Ok(()) + })?; + Ok(()) + })?; + + let linkinfo_len = linkinfo.finish()?; + + req.header.nlmsg_len += align_to(ifname_len + linkinfo_len, NLA_ALIGNTO as usize) as u32; + sock.send(&bytes_of(&req)[..req.header.nlmsg_len as usize])?; + sock.recv()?; + + Ok(()) +} + #[doc(hidden)] pub unsafe fn netlink_set_link_up(if_index: i32) -> Result<(), io::Error> { let sock = NetlinkSocket::open()?; @@ -292,11 +353,100 @@ pub unsafe fn netlink_set_link_up(if_index: i32) -> Result<(), io::Error> { Ok(()) } +#[doc(hidden)] +pub unsafe fn netlink_set_link_down(if_index: i32) -> Result<(), io::Error> { + let sock = NetlinkSocket::open()?; + + // Safety: Request is POD so this is safe + let mut req = mem::zeroed::(); + + let nlmsg_len = mem::size_of::() + mem::size_of::(); + req.header = nlmsghdr { + nlmsg_len: nlmsg_len as u32, + nlmsg_flags: (NLM_F_REQUEST | NLM_F_ACK) as u16, + nlmsg_type: RTM_SETLINK, + nlmsg_pid: 0, + nlmsg_seq: 1, + }; + req.if_info.ifi_family = AF_UNSPEC as u8; + req.if_info.ifi_index = if_index; + req.if_info.ifi_flags = 0; + req.if_info.ifi_change = IFF_UP as u32; + + sock.send(&bytes_of(&req)[..req.header.nlmsg_len as usize])?; + sock.recv()?; + + Ok(()) +} + +#[doc(hidden)] +pub unsafe fn netlink_delete_link(if_index: i32) -> Result<(), io::Error> { + let sock = NetlinkSocket::open()?; + + // Safety: Request is POD so this is safe + let mut req = mem::zeroed::(); + + let nlmsg_len = mem::size_of::() + mem::size_of::(); + req.header = nlmsghdr { + nlmsg_len: nlmsg_len as u32, + nlmsg_flags: (NLM_F_REQUEST | NLM_F_ACK) as u16, + nlmsg_type: RTM_DELLINK, + nlmsg_pid: 0, + nlmsg_seq: 1, + }; + req.if_info.ifi_family = AF_UNSPEC as u8; + req.if_info.ifi_index = if_index; + req.if_info.ifi_flags = 0; + req.if_info.ifi_change = 0; + + sock.send(&bytes_of(&req)[..req.header.nlmsg_len as usize])?; + sock.recv()?; + + Ok(()) +} + +#[doc(hidden)] +pub unsafe fn netlink_add_ip_addr( + if_index: u32, + ipv4_addr: Ipv4Addr, + ipv4_prefix: u8, +) -> Result<(), io::Error> { + let sock = NetlinkSocket::open()?; + + // Safety: AddrRequest is POD so this is safe + let mut req = mem::zeroed::(); + + let nlmsg_len = mem::size_of::() + mem::size_of::(); + req.header = nlmsghdr { + nlmsg_len: nlmsg_len as u32, + nlmsg_flags: (NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE) as u16, + nlmsg_type: RTM_NEWADDR, + nlmsg_pid: 0, + nlmsg_seq: 1, + }; + req.if_addr.ifa_family = AF_INET as u8; + req.if_addr.ifa_prefixlen = ipv4_prefix; + req.if_addr.ifa_flags = IFA_F_PERMANENT as u8; + req.if_addr.ifa_scope = RT_SCOPE_UNIVERSE; + req.if_addr.ifa_index = if_index; + + let attrs_buf = request_attributes(&mut req, nlmsg_len); + + // add IFA_LOCAL + let local_len = write_attr_bytes(attrs_buf, 0, IFA_LOCAL, &ipv4_addr.octets())?; + + req.header.nlmsg_len += align_to(local_len, NLA_ALIGNTO as usize) as u32; + sock.send(&bytes_of(&req)[..req.header.nlmsg_len as usize])?; + sock.recv()?; + + Ok(()) +} + #[repr(C)] struct Request { header: nlmsghdr, if_info: ifinfomsg, - attrs: [u8; 64], + attrs: [u8; 128], } #[repr(C)] @@ -306,6 +456,13 @@ struct TcRequest { attrs: [u8; 64], } +#[repr(C)] +struct AddrRequest { + header: nlmsghdr, + if_addr: ifaddrmsg, + attrs: [u8; 64], +} + struct NetlinkSocket { sock: OwnedFd, _nl_pid: u32, @@ -505,6 +662,23 @@ impl<'a> NestedAttrs<'a> { Ok(size) } + fn write_bytes(&mut self, value: &[u8]) -> Result { + let size = write_bytes(self.buf, self.offset, value)?; + self.offset += size; + Ok(size) + } + + fn write_nested_attrs(&mut self, attr_type: u16, f: F) -> Result + where + F: FnOnce(&mut NestedAttrs<'_>) -> Result<(), io::Error>, + { + let mut nested_attrs = NestedAttrs::new(&mut self.buf[self.offset..], attr_type); + f(&mut nested_attrs)?; + let size = nested_attrs.finish()?; + self.offset += size; + Ok(size) + } + fn finish(self) -> Result { let nla_len = self.offset; let attr = nlattr { From 6270e6aa50eb4bccf67a8382764a3a53b232273d Mon Sep 17 00:00:00 2001 From: Kevin Ji <1146876+kevinji@users.noreply.github.com> Date: Thu, 28 Mar 2024 15:42:32 -0700 Subject: [PATCH 3/4] integration-test: Use veth pair instead of lo --- test/integration-test/src/tests/load.rs | 35 ++++-- test/integration-test/src/tests/smoke.rs | 23 +++- test/integration-test/src/tests/xdp.rs | 44 ++++--- test/integration-test/src/utils.rs | 148 +++++++++++++++++++---- 4 files changed, 193 insertions(+), 57 deletions(-) diff --git a/test/integration-test/src/tests/load.rs b/test/integration-test/src/tests/load.rs index ffc8ddf74..e4b490758 100644 --- a/test/integration-test/src/tests/load.rs +++ b/test/integration-test/src/tests/load.rs @@ -18,11 +18,15 @@ use aya::{ use aya_obj::programs::XdpAttachType; use test_log::test; +use crate::utils::NetNsGuard; + const MAX_RETRIES: usize = 100; const RETRY_DURATION: Duration = Duration::from_millis(10); #[test] fn long_name() { + let netns = NetNsGuard::new(); + let mut bpf = Ebpf::load(crate::NAME_TEST).unwrap(); let name_prog: &mut Xdp = bpf .program_mut("ihaveaverylongname") @@ -30,7 +34,9 @@ fn long_name() { .try_into() .unwrap(); name_prog.load().unwrap(); - name_prog.attach("lo", XdpFlags::default()).unwrap(); + name_prog + .attach_to_if_index(netns.if_idx2, XdpFlags::default()) + .unwrap(); // We used to be able to assert with bpftool that the program name was short. // It seem though that it now uses the name from the ELF symbol table instead. @@ -198,11 +204,15 @@ fn assert_unloaded(name: &str) { #[test] fn unload_xdp() { + let netns = NetNsGuard::new(); + let mut bpf = Ebpf::load(crate::TEST).unwrap(); let prog: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap(); prog.load().unwrap(); assert_loaded("pass"); - let link = prog.attach("lo", XdpFlags::default()).unwrap(); + let link = prog + .attach_to_if_index(netns.if_idx2, XdpFlags::default()) + .unwrap(); { let _link_owned = prog.take_link(link).unwrap(); prog.unload().unwrap(); @@ -213,7 +223,8 @@ fn unload_xdp() { prog.load().unwrap(); assert_loaded("pass"); - prog.attach("lo", XdpFlags::default()).unwrap(); + prog.attach_to_if_index(netns.if_idx2, XdpFlags::default()) + .unwrap(); assert_loaded("pass"); prog.unload().unwrap(); @@ -351,15 +362,19 @@ fn pin_link() { return; } + let netns = NetNsGuard::new(); + let mut bpf = Ebpf::load(crate::TEST).unwrap(); let prog: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap(); prog.load().unwrap(); - let link_id = prog.attach("lo", XdpFlags::default()).unwrap(); + let link_id = prog + .attach_to_if_index(netns.if_idx2, XdpFlags::default()) + .unwrap(); let link = prog.take_link(link_id).unwrap(); assert_loaded("pass"); let fd_link: FdLink = link.try_into().unwrap(); - let pinned = fd_link.pin("/sys/fs/bpf/aya-xdp-test-lo").unwrap(); + let pinned = fd_link.pin("/sys/fs/bpf/aya-xdp-test-veth-pair").unwrap(); // because of the pin, the program is still attached prog.unload().unwrap(); @@ -382,6 +397,8 @@ fn pin_lifecycle() { return; } + let netns = NetNsGuard::new(); + // 1. Load Program and Pin { let mut bpf = Ebpf::load(crate::PASS).unwrap(); @@ -405,10 +422,12 @@ fn pin_lifecycle() { { let mut prog = Xdp::from_pin("/sys/fs/bpf/aya-xdp-test-prog", XdpAttachType::Interface).unwrap(); - let link_id = prog.attach("lo", XdpFlags::default()).unwrap(); + let link_id = prog + .attach_to_if_index(netns.if_idx2, XdpFlags::default()) + .unwrap(); let link = prog.take_link(link_id).unwrap(); let fd_link: FdLink = link.try_into().unwrap(); - fd_link.pin("/sys/fs/bpf/aya-xdp-test-lo").unwrap(); + fd_link.pin("/sys/fs/bpf/aya-xdp-test-veth-pair").unwrap(); // Unpin the program. It will stay attached since its links were pinned. prog.unpin().unwrap(); @@ -423,7 +442,7 @@ fn pin_lifecycle() { let prog: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap(); prog.load().unwrap(); - let link = PinnedLink::from_pin("/sys/fs/bpf/aya-xdp-test-lo") + let link = PinnedLink::from_pin("/sys/fs/bpf/aya-xdp-test-veth-pair") .unwrap() .unpin() .unwrap(); diff --git a/test/integration-test/src/tests/smoke.rs b/test/integration-test/src/tests/smoke.rs index 708b889ba..a4dd98926 100644 --- a/test/integration-test/src/tests/smoke.rs +++ b/test/integration-test/src/tests/smoke.rs @@ -16,12 +16,14 @@ fn xdp() { return; } - let _netns = NetNsGuard::new(); + let netns = NetNsGuard::new(); let mut bpf = Ebpf::load(crate::PASS).unwrap(); let dispatcher: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap(); dispatcher.load().unwrap(); - dispatcher.attach("lo", XdpFlags::default()).unwrap(); + dispatcher + .attach_to_if_index(netns.if_idx2, XdpFlags::default()) + .unwrap(); } #[test] @@ -54,12 +56,13 @@ fn extension() { return; } - let _netns = NetNsGuard::new(); + let netns = NetNsGuard::new(); let mut bpf = Ebpf::load(crate::MAIN).unwrap(); let pass: &mut Xdp = bpf.program_mut("xdp_pass").unwrap().try_into().unwrap(); pass.load().unwrap(); - pass.attach("lo", XdpFlags::default()).unwrap(); + pass.attach_to_if_index(netns.if_idx2, XdpFlags::default()) + .unwrap(); let mut bpf = EbpfLoader::new() .extension("xdp_drop") @@ -73,11 +76,15 @@ fn extension() { #[test] fn list_loaded_programs() { + let netns = NetNsGuard::new(); + // Load a program. let mut bpf = Ebpf::load(crate::PASS).unwrap(); let dispatcher: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap(); dispatcher.load().unwrap(); - dispatcher.attach("lo", XdpFlags::default()).unwrap(); + dispatcher + .attach_to_if_index(netns.if_idx2, XdpFlags::default()) + .unwrap(); // Ensure the loaded_programs() api doesn't panic. let prog = loaded_programs() @@ -102,11 +109,15 @@ fn list_loaded_programs() { #[test] fn list_loaded_maps() { + let netns = NetNsGuard::new(); + // Load a program with maps. let mut bpf = Ebpf::load(crate::MAP_TEST).unwrap(); let dispatcher: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap(); dispatcher.load().unwrap(); - dispatcher.attach("lo", XdpFlags::default()).unwrap(); + dispatcher + .attach_to_if_index(netns.if_idx2, XdpFlags::default()) + .unwrap(); // Ensure the loaded_maps() api doesn't panic and retrieve a map. let map = loaded_maps() diff --git a/test/integration-test/src/tests/xdp.rs b/test/integration-test/src/tests/xdp.rs index fb137d898..322a3f52b 100644 --- a/test/integration-test/src/tests/xdp.rs +++ b/test/integration-test/src/tests/xdp.rs @@ -1,4 +1,4 @@ -use std::{ffi::CStr, mem::MaybeUninit, net::UdpSocket, num::NonZeroU32, time::Duration}; +use std::{mem::MaybeUninit, net::UdpSocket, num::NonZeroU32, time::Duration}; use aya::{ maps::{Array, CpuMap, XskMap}, @@ -9,11 +9,11 @@ use object::{Object, ObjectSection, ObjectSymbol, SymbolSection}; use test_log::test; use xdpilone::{BufIdx, IfInfo, Socket, SocketConfig, Umem, UmemConfig}; -use crate::utils::NetNsGuard; +use crate::utils::{NetNsGuard, IP_ADDR_1, IP_ADDR_2}; #[test] fn af_xdp() { - let _netns = NetNsGuard::new(); + let netns = NetNsGuard::new(); let mut bpf = Ebpf::load(crate::REDIRECT).unwrap(); let mut socks: XskMap<_> = bpf.take_map("SOCKS").unwrap().try_into().unwrap(); @@ -24,7 +24,8 @@ fn af_xdp() { .try_into() .unwrap(); xdp.load().unwrap(); - xdp.attach("lo", XdpFlags::default()).unwrap(); + xdp.attach_to_if_index(netns.if_idx2, XdpFlags::default()) + .unwrap(); // So this needs to be page aligned. Pages are 4k on all mainstream architectures except for // Apple Silicon which uses 16k pages. So let's align on that for tests to run natively there. @@ -41,9 +42,7 @@ fn af_xdp() { }; let mut iface = IfInfo::invalid(); - iface - .from_name(CStr::from_bytes_with_nul(b"lo\0").unwrap()) - .unwrap(); + iface.from_ifindex(netns.if_idx1).unwrap(); let sock = Socket::with_shared(&iface, &umem).unwrap(); let mut fq_cq = umem.fq_cq(&sock).unwrap(); // Fill Queue / Completion Queue @@ -66,9 +65,12 @@ fn af_xdp() { writer.insert_once(frame.offset); writer.commit(); - let sock = UdpSocket::bind("127.0.0.1:0").unwrap(); - let port = sock.local_addr().unwrap().port(); - sock.send_to(b"hello AF_XDP", "127.0.0.1:1777").unwrap(); + let sock = UdpSocket::bind((IP_ADDR_1, 0)).unwrap(); + let src_port = sock.local_addr().unwrap().port(); + + const DST_PORT: u16 = 1777; + sock.send_to(b"hello AF_XDP", (IP_ADDR_2, DST_PORT)) + .unwrap(); assert_eq!(rx.available(), 1); let desc = rx.receive(1).read().unwrap(); @@ -81,8 +83,8 @@ fn af_xdp() { let (ip, buf) = buf.split_at(20); assert_eq!(ip[9], 17); // UDP let (udp, payload) = buf.split_at(8); - assert_eq!(&udp[0..2], port.to_be_bytes().as_slice()); // Source - assert_eq!(&udp[2..4], 1777u16.to_be_bytes().as_slice()); // Dest + assert_eq!(&udp[0..2], src_port.to_be_bytes().as_slice()); // Source + assert_eq!(&udp[2..4], DST_PORT.to_be_bytes().as_slice()); // Dest assert_eq!(payload, b"hello AF_XDP"); } @@ -134,7 +136,7 @@ fn map_load() { #[test] fn cpumap_chain() { - let _netns = NetNsGuard::new(); + let netns = NetNsGuard::new(); let mut bpf = Ebpf::load(crate::REDIRECT).unwrap(); @@ -157,20 +159,24 @@ fn cpumap_chain() { // Load the main program let xdp: &mut Xdp = bpf.program_mut("redirect_cpu").unwrap().try_into().unwrap(); xdp.load().unwrap(); - xdp.attach("lo", XdpFlags::default()).unwrap(); + xdp.attach_to_if_index(netns.if_idx2, XdpFlags::default()) + .unwrap(); const PAYLOAD: &str = "hello cpumap"; - let sock = UdpSocket::bind("127.0.0.1:0").unwrap(); - let addr = sock.local_addr().unwrap(); - sock.set_read_timeout(Some(Duration::from_secs(60))) + let sock1 = UdpSocket::bind((IP_ADDR_1, 0)).unwrap(); + let sock2 = UdpSocket::bind((IP_ADDR_2, 0)).unwrap(); + sock2 + .set_read_timeout(Some(Duration::from_secs(60))) + .unwrap(); + sock1 + .send_to(PAYLOAD.as_bytes(), sock2.local_addr().unwrap()) .unwrap(); - sock.send_to(PAYLOAD.as_bytes(), addr).unwrap(); // Read back the packet to ensure it went through the entire network stack, including our two // probes. let mut buf = [0u8; PAYLOAD.len() + 1]; - let n = sock.recv(&mut buf).unwrap(); + let n = sock2.recv(&mut buf).unwrap(); assert_eq!(&buf[..n], PAYLOAD.as_bytes()); assert_eq!(hits.get(&0, 0).unwrap(), 1); diff --git a/test/integration-test/src/utils.rs b/test/integration-test/src/utils.rs index 28b68d4aa..6766d5bcd 100644 --- a/test/integration-test/src/utils.rs +++ b/test/integration-test/src/utils.rs @@ -1,19 +1,42 @@ //! Utilities to run tests use std::{ - ffi::CString, - io, process, + ffi::CStr, + io, + net::Ipv4Addr, + process, sync::atomic::{AtomicU64, Ordering}, }; -use aya::netlink_set_link_up; +use aya::{ + netlink_add_ip_addr, netlink_add_veth_pair, netlink_delete_link, netlink_set_link_down, + netlink_set_link_up, +}; use libc::if_nametoindex; use netns_rs::{get_from_current_thread, NetNs}; +pub const IF_NAME_1: &CStr = c"aya-test-1"; +pub const IF_NAME_2: &CStr = c"aya-test-2"; +pub const IP_ADDR_1: Ipv4Addr = Ipv4Addr::new(10, 0, 0, 1); +pub const IP_ADDR_2: Ipv4Addr = Ipv4Addr::new(10, 0, 0, 2); +pub const IP_PREFIX: u8 = 24; + +pub fn setup_test_veth_pair() { + unsafe { netlink_add_veth_pair(IF_NAME_1, IF_NAME_2) }.unwrap_or_else(|e| { + panic!( + "Failed to set up veth pair ({}, {}): {e}", + IF_NAME_1.to_string_lossy(), + IF_NAME_2.to_string_lossy() + ) + }) +} + pub struct NetNsGuard { name: String, old_ns: NetNs, ns: Option, + pub if_idx1: u32, + pub if_idx2: u32, } impl NetNsGuard { @@ -26,39 +49,116 @@ impl NetNsGuard { // Create and enter netns let ns = NetNs::new(&name).unwrap_or_else(|e| panic!("Failed to create netns {name}: {e}")); - let netns = Self { - old_ns, - ns: Some(ns), - name, - }; - let ns = netns.ns.as_ref().unwrap(); ns.enter() - .unwrap_or_else(|e| panic!("Failed to enter network namespace {}: {e}", netns.name)); - println!("Entered network namespace {}", netns.name); + .unwrap_or_else(|e| panic!("Failed to enter network namespace {}: {e}", name)); + println!("Entered network namespace {}", name); // By default, the loopback in a new netns is down. Set it up. - let lo = CString::new("lo").unwrap(); - unsafe { - let idx = if_nametoindex(lo.as_ptr()); - if idx == 0 { - panic!( - "Interface `lo` not found in netns {}: {}", - netns.name, - io::Error::last_os_error() - ); - } - netlink_set_link_up(idx as i32) - .unwrap_or_else(|e| panic!("Failed to set `lo` up in netns {}: {e}", netns.name)); + let lo_idx = unsafe { if_nametoindex(c"lo".as_ptr()) }; + if lo_idx == 0 { + panic!( + "Interface `lo` not found in netns {}: {}", + name, + io::Error::last_os_error() + ); + } + + unsafe { netlink_set_link_up(lo_idx as i32) } + .unwrap_or_else(|e| panic!("Failed to set `lo` up in netns {}: {e}", name)); + + setup_test_veth_pair(); + + let if_idx1 = unsafe { if_nametoindex(IF_NAME_1.as_ptr()) }; + if if_idx1 == 0 { + panic!( + "Interface `{}` not found in netns {}: {}", + IF_NAME_1.to_string_lossy(), + name, + io::Error::last_os_error() + ); + } + + let if_idx2 = unsafe { if_nametoindex(IF_NAME_2.as_ptr()) }; + if if_idx2 == 0 { + panic!( + "Interface `{}` not found in netns {}: {}", + IF_NAME_2.to_string_lossy(), + name, + io::Error::last_os_error() + ); } - netns + unsafe { netlink_add_ip_addr(if_idx1, IP_ADDR_1, IP_PREFIX) }.unwrap_or_else(|e| { + panic!( + "Failed to add IP `{}` to `{}` in netns {}: {e}", + IP_ADDR_1, + IF_NAME_1.to_string_lossy(), + name + ) + }); + + unsafe { netlink_add_ip_addr(if_idx2, IP_ADDR_2, IP_PREFIX) }.unwrap_or_else(|e| { + panic!( + "Failed to add IP `{}` to `{}` in netns {}: {e}", + IP_ADDR_2, + IF_NAME_2.to_string_lossy(), + name + ) + }); + + unsafe { netlink_set_link_up(if_idx1 as i32) }.unwrap_or_else(|e| { + panic!( + "Failed to set `{}` up in netns {}: {e}", + IF_NAME_1.to_string_lossy(), + name + ) + }); + + unsafe { netlink_set_link_up(if_idx2 as i32) }.unwrap_or_else(|e| { + panic!( + "Failed to set `{}` up in netns {}: {e}", + IF_NAME_2.to_string_lossy(), + name + ) + }); + + Self { + old_ns, + ns: Some(ns), + name, + if_idx1, + if_idx2, + } } } impl Drop for NetNsGuard { fn drop(&mut self) { // Avoid panic in panic + if let Err(e) = unsafe { netlink_set_link_down(self.if_idx1 as i32) } { + eprintln!( + "Failed to set `{}` down in netns {}: {e}", + IF_NAME_1.to_string_lossy(), + self.name + ) + } + if let Err(e) = unsafe { netlink_set_link_down(self.if_idx2 as i32) } { + eprintln!( + "Failed to set `{}` down in netns {}: {e}", + IF_NAME_2.to_string_lossy(), + self.name + ) + } + + if let Err(e) = unsafe { netlink_delete_link(self.if_idx1 as i32) } { + eprintln!( + "Failed to delete `{}` in netns {}: {e}", + IF_NAME_1.to_string_lossy(), + self.name + ) + } + if let Err(e) = self.old_ns.enter() { eprintln!("Failed to return to original netns: {e}"); } From 7150c8518678c4692666d132abb6688ed9d159b4 Mon Sep 17 00:00:00 2001 From: Kevin Ji <1146876+kevinji@users.noreply.github.com> Date: Thu, 11 Apr 2024 22:04:46 -0700 Subject: [PATCH 4/4] integration-test: Try using ip commands --- test/integration-test/src/utils.rs | 28 +- xtask/check-config.sh | 419 +++++++++++++++++++++++++++++ xtask/src/run.rs | 17 +- 3 files changed, 458 insertions(+), 6 deletions(-) create mode 100755 xtask/check-config.sh diff --git a/test/integration-test/src/utils.rs b/test/integration-test/src/utils.rs index 6766d5bcd..386505015 100644 --- a/test/integration-test/src/utils.rs +++ b/test/integration-test/src/utils.rs @@ -2,9 +2,9 @@ use std::{ ffi::CStr, - io, + io::{self, Write}, net::Ipv4Addr, - process, + process::{self, Command}, sync::atomic::{AtomicU64, Ordering}, }; @@ -67,6 +67,30 @@ impl NetNsGuard { unsafe { netlink_set_link_up(lo_idx as i32) } .unwrap_or_else(|e| panic!("Failed to set `lo` up in netns {}: {e}", name)); + let ls_output = Command::new("sh").args(["-c", "ls -la /"]).output(); + match ls_output { + Ok(output) => { + eprintln!("ls status: {}", output.status); + io::stdout().write_all(&output.stdout).unwrap(); + io::stderr().write_all(&output.stderr).unwrap(); + } + Err(e) => { + eprintln!("Failed to run ls -la: {e}"); + } + } + + let check_config_output = Command::new("release-check-config.sh").output(); + match check_config_output { + Ok(output) => { + eprintln!("check_config status: {}", output.status); + io::stdout().write_all(&output.stdout).unwrap(); + io::stderr().write_all(&output.stderr).unwrap(); + } + Err(e) => { + eprintln!("Failed to run check-config.sh: {e}"); + } + } + setup_test_veth_pair(); let if_idx1 = unsafe { if_nametoindex(IF_NAME_1.as_ptr()) }; diff --git a/xtask/check-config.sh b/xtask/check-config.sh new file mode 100755 index 000000000..b9cc6bf87 --- /dev/null +++ b/xtask/check-config.sh @@ -0,0 +1,419 @@ +#!/usr/bin/env sh +set -e + +EXITCODE=0 + +# bits of this were adapted from lxc-checkconfig +# see also https://github.com/lxc/lxc/blob/lxc-1.0.2/src/lxc/lxc-checkconfig.in + +possibleConfigs=" + /proc/config.gz + /boot/config-$(uname -r) + /usr/src/linux-$(uname -r)/.config + /usr/src/linux/.config +" + +if [ $# -gt 0 ]; then + CONFIG="$1" +else + : "${CONFIG:=/proc/config.gz}" +fi + +if ! command -v zgrep > /dev/null 2>&1; then + zgrep() { + zcat "$2" | grep "$1" + } +fi + +useColor=true +if [ "$NO_COLOR" = "1" ] || [ ! -t 1 ]; then + useColor=false +fi +kernelVersion="$(uname -r)" +kernelMajor="${kernelVersion%%.*}" +kernelMinor="${kernelVersion#$kernelMajor.}" +kernelMinor="${kernelMinor%%.*}" + +is_set() { + zgrep "CONFIG_$1=[y|m]" "$CONFIG" > /dev/null +} +is_set_in_kernel() { + zgrep "CONFIG_$1=y" "$CONFIG" > /dev/null +} +is_set_as_module() { + zgrep "CONFIG_$1=m" "$CONFIG" > /dev/null +} + +color() { + # if stdout is not a terminal, then don't do color codes. + if [ "$useColor" = "false" ]; then + return 0 + fi + codes= + if [ "$1" = 'bold' ]; then + codes='1' + shift + fi + if [ "$#" -gt 0 ]; then + code= + case "$1" in + # see https://en.wikipedia.org/wiki/ANSI_escape_code#Colors + black) code=30 ;; + red) code=31 ;; + green) code=32 ;; + yellow) code=33 ;; + blue) code=34 ;; + magenta) code=35 ;; + cyan) code=36 ;; + white) code=37 ;; + esac + if [ "$code" ]; then + codes="${codes:+$codes;}$code" + fi + fi + printf '\033[%sm' "$codes" +} +wrap_color() { + text="$1" + shift + color "$@" + printf '%s' "$text" + color reset + echo +} + +wrap_good() { + echo "$(wrap_color "$1" white): $(wrap_color "$2" green)" +} +wrap_bad() { + echo "$(wrap_color "$1" bold): $(wrap_color "$2" bold red)" +} +wrap_warning() { + wrap_color >&2 "$*" red +} + +check_flag() { + if is_set_in_kernel "$1"; then + wrap_good "CONFIG_$1" 'enabled' + elif is_set_as_module "$1"; then + wrap_good "CONFIG_$1" 'enabled (as module)' + else + wrap_bad "CONFIG_$1" 'missing' + EXITCODE=1 + fi +} + +check_flags() { + for flag in "$@"; do + printf -- '- ' + check_flag "$flag" + done +} + +check_command() { + if command -v "$1" > /dev/null 2>&1; then + wrap_good "$1 command" 'available' + else + wrap_bad "$1 command" 'missing' + EXITCODE=1 + fi +} + +check_device() { + if [ -c "$1" ]; then + wrap_good "$1" 'present' + else + wrap_bad "$1" 'missing' + EXITCODE=1 + fi +} + +check_distro_userns() { + if [ ! -e /etc/os-release ]; then + return + fi + . /etc/os-release 2> /dev/null || /bin/true + case "$ID" in + centos | rhel) + case "$VERSION_ID" in + 7*) + # this is a CentOS7 or RHEL7 system + grep -q 'user_namespace.enable=1' /proc/cmdline || { + # no user namespace support enabled + wrap_bad " (RHEL7/CentOS7" "User namespaces disabled; add 'user_namespace.enable=1' to boot command line)" + EXITCODE=1 + } + ;; + esac + ;; + esac +} + +if [ ! -e "$CONFIG" ]; then + wrap_warning "warning: $CONFIG does not exist, searching other paths for kernel config ..." + for tryConfig in $possibleConfigs; do + if [ -e "$tryConfig" ]; then + CONFIG="$tryConfig" + break + fi + done + if [ ! -e "$CONFIG" ]; then + wrap_warning "error: cannot find kernel config" + wrap_warning " try running this script again, specifying the kernel config:" + wrap_warning " CONFIG=/path/to/kernel/.config $0 or $0 /path/to/kernel/.config" + exit 1 + fi +fi + +wrap_color "info: reading kernel config from $CONFIG ..." white +echo + +echo 'Generally Necessary:' + +printf -- '- ' +if [ "$(stat -f -c %t /sys/fs/cgroup 2> /dev/null)" = '63677270' ]; then + wrap_good 'cgroup hierarchy' 'cgroupv2' + cgroupv2ControllerFile='/sys/fs/cgroup/cgroup.controllers' + if [ -f "$cgroupv2ControllerFile" ]; then + echo ' Controllers:' + for controller in cpu cpuset io memory pids; do + if grep -qE '(^| )'"$controller"'($| )' "$cgroupv2ControllerFile"; then + echo " - $(wrap_good "$controller" 'available')" + else + echo " - $(wrap_bad "$controller" 'missing')" + fi + done + else + wrap_bad "$cgroupv2ControllerFile" 'nonexistent??' + fi + # TODO find an efficient way to check if cgroup.freeze exists in subdir +else + cgroupSubsystemDir="$(sed -rne '/^[^ ]+ ([^ ]+) cgroup ([^ ]*,)?(cpu|cpuacct|cpuset|devices|freezer|memory)[, ].*$/ { s//\1/p; q }' /proc/mounts)" + cgroupDir="$(dirname "$cgroupSubsystemDir")" + if [ -d "$cgroupDir/cpu" ] || [ -d "$cgroupDir/cpuacct" ] || [ -d "$cgroupDir/cpuset" ] || [ -d "$cgroupDir/devices" ] || [ -d "$cgroupDir/freezer" ] || [ -d "$cgroupDir/memory" ]; then + echo "$(wrap_good 'cgroup hierarchy' 'properly mounted') [$cgroupDir]" + else + if [ "$cgroupSubsystemDir" ]; then + echo "$(wrap_bad 'cgroup hierarchy' 'single mountpoint!') [$cgroupSubsystemDir]" + else + wrap_bad 'cgroup hierarchy' 'nonexistent??' + fi + EXITCODE=1 + echo " $(wrap_color '(see https://github.com/tianon/cgroupfs-mount)' yellow)" + fi +fi + +if [ "$(cat /sys/module/apparmor/parameters/enabled 2> /dev/null)" = 'Y' ]; then + printf -- '- ' + if command -v apparmor_parser > /dev/null 2>&1; then + wrap_good 'apparmor' 'enabled and tools installed' + else + wrap_bad 'apparmor' 'enabled, but apparmor_parser missing' + printf ' ' + if command -v apt-get > /dev/null 2>&1; then + wrap_color '(use "apt-get install apparmor" to fix this)' + elif command -v yum > /dev/null 2>&1; then + wrap_color '(your best bet is "yum install apparmor-parser")' + else + wrap_color '(look for an "apparmor" package for your distribution)' + fi + EXITCODE=1 + fi +fi + +check_flags \ + NAMESPACES NET_NS PID_NS IPC_NS UTS_NS \ + CGROUPS CGROUP_CPUACCT CGROUP_DEVICE CGROUP_FREEZER CGROUP_SCHED CPUSETS MEMCG \ + KEYS \ + VETH BRIDGE BRIDGE_NETFILTER \ + IP_NF_FILTER IP_NF_MANGLE IP_NF_TARGET_MASQUERADE \ + NETFILTER_XT_MATCH_ADDRTYPE \ + NETFILTER_XT_MATCH_CONNTRACK \ + NETFILTER_XT_MATCH_IPVS \ + NETFILTER_XT_MARK \ + IP_NF_NAT NF_NAT \ + POSIX_MQUEUE +# (POSIX_MQUEUE is required for bind-mounting /dev/mqueue into containers) + +if [ "$kernelMajor" -lt 4 ] || ([ "$kernelMajor" -eq 4 ] && [ "$kernelMinor" -lt 8 ]); then + check_flags DEVPTS_MULTIPLE_INSTANCES +fi + +if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 1 ]; then + check_flags NF_NAT_IPV4 +fi + +if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 2 ]; then + check_flags NF_NAT_NEEDED +fi +# check availability of BPF_CGROUP_DEVICE support +if [ "$kernelMajor" -ge 5 ] || ([ "$kernelMajor" -eq 4 ] && [ "$kernelMinor" -ge 15 ]); then + check_flags CGROUP_BPF +fi + +echo + +echo 'Optional Features:' +{ + check_flags USER_NS + check_distro_userns +} +{ + check_flags SECCOMP + check_flags SECCOMP_FILTER +} +{ + check_flags CGROUP_PIDS +} +{ + check_flags MEMCG_SWAP + # Kernel v5.8+ removes MEMCG_SWAP_ENABLED. + if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 8 ]; then + CODE=${EXITCODE} + check_flags MEMCG_SWAP_ENABLED + # FIXME this check is cgroupv1-specific + if [ -e /sys/fs/cgroup/memory/memory.memsw.limit_in_bytes ]; then + echo " $(wrap_color '(cgroup swap accounting is currently enabled)' bold black)" + EXITCODE=${CODE} + elif is_set MEMCG_SWAP && ! is_set MEMCG_SWAP_ENABLED; then + echo " $(wrap_color '(cgroup swap accounting is currently not enabled, you can enable it by setting boot option "swapaccount=1")' bold black)" + fi + else + # Kernel v5.8+ enables swap accounting by default. + echo " $(wrap_color '(cgroup swap accounting is currently enabled)' bold black)" + fi +} +{ + if is_set LEGACY_VSYSCALL_NATIVE; then + printf -- '- ' + wrap_bad "CONFIG_LEGACY_VSYSCALL_NATIVE" 'enabled' + echo " $(wrap_color '(dangerous, provides an ASLR-bypassing target with usable ROP gadgets.)' bold black)" + elif is_set LEGACY_VSYSCALL_EMULATE; then + printf -- '- ' + wrap_good "CONFIG_LEGACY_VSYSCALL_EMULATE" 'enabled' + elif is_set LEGACY_VSYSCALL_NONE; then + printf -- '- ' + wrap_bad "CONFIG_LEGACY_VSYSCALL_NONE" 'enabled' + echo " $(wrap_color '(containers using eglibc <= 2.13 will not work. Switch to' bold black)" + echo " $(wrap_color ' "CONFIG_VSYSCALL_[NATIVE|EMULATE]" or use "vsyscall=[native|emulate]"' bold black)" + echo " $(wrap_color ' on kernel command line. Note that this will disable ASLR for the,' bold black)" + echo " $(wrap_color ' VDSO which may assist in exploiting security vulnerabilities.)' bold black)" + # else Older kernels (prior to 3dc33bd30f3e, released in v4.40-rc1) do + # not have these LEGACY_VSYSCALL options and are effectively + # LEGACY_VSYSCALL_EMULATE. Even older kernels are presumably + # effectively LEGACY_VSYSCALL_NATIVE. + fi +} + +if [ "$kernelMajor" -lt 4 ] || ([ "$kernelMajor" -eq 4 ] && [ "$kernelMinor" -le 5 ]); then + check_flags MEMCG_KMEM +fi + +if [ "$kernelMajor" -lt 3 ] || ([ "$kernelMajor" -eq 3 ] && [ "$kernelMinor" -le 18 ]); then + check_flags RESOURCE_COUNTERS +fi + +if [ "$kernelMajor" -lt 3 ] || ([ "$kernelMajor" -eq 3 ] && [ "$kernelMinor" -le 13 ]); then + netprio=NETPRIO_CGROUP +else + netprio=CGROUP_NET_PRIO +fi + +if [ "$kernelMajor" -lt 5 ]; then + check_flags IOSCHED_CFQ CFQ_GROUP_IOSCHED +fi + +check_flags \ + BLK_CGROUP BLK_DEV_THROTTLING \ + CGROUP_PERF \ + CGROUP_HUGETLB \ + NET_CLS_CGROUP $netprio \ + CFS_BANDWIDTH FAIR_GROUP_SCHED \ + IP_NF_TARGET_REDIRECT \ + IP_VS \ + IP_VS_NFCT \ + IP_VS_PROTO_TCP \ + IP_VS_PROTO_UDP \ + IP_VS_RR \ + SECURITY_SELINUX \ + SECURITY_APPARMOR + +if ! is_set EXT4_USE_FOR_EXT2; then + check_flags EXT3_FS EXT3_FS_XATTR EXT3_FS_POSIX_ACL EXT3_FS_SECURITY + if ! is_set EXT3_FS || ! is_set EXT3_FS_XATTR || ! is_set EXT3_FS_POSIX_ACL || ! is_set EXT3_FS_SECURITY; then + echo " $(wrap_color '(enable these ext3 configs if you are using ext3 as backing filesystem)' bold black)" + fi +fi + +check_flags EXT4_FS EXT4_FS_POSIX_ACL EXT4_FS_SECURITY +if ! is_set EXT4_FS || ! is_set EXT4_FS_POSIX_ACL || ! is_set EXT4_FS_SECURITY; then + if is_set EXT4_USE_FOR_EXT2; then + echo " $(wrap_color 'enable these ext4 configs if you are using ext3 or ext4 as backing filesystem' bold black)" + else + echo " $(wrap_color 'enable these ext4 configs if you are using ext4 as backing filesystem' bold black)" + fi +fi + +echo '- Network Drivers:' +echo " - \"$(wrap_color 'overlay' blue)\":" +check_flags VXLAN BRIDGE_VLAN_FILTERING | sed 's/^/ /' +echo ' Optional (for encrypted networks):' +check_flags CRYPTO CRYPTO_AEAD CRYPTO_GCM CRYPTO_SEQIV CRYPTO_GHASH \ + XFRM XFRM_USER XFRM_ALGO INET_ESP NETFILTER_XT_MATCH_BPF | sed 's/^/ /' +if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 3 ]; then + check_flags INET_XFRM_MODE_TRANSPORT | sed 's/^/ /' +fi +echo " - \"$(wrap_color 'ipvlan' blue)\":" +check_flags IPVLAN | sed 's/^/ /' +echo " - \"$(wrap_color 'macvlan' blue)\":" +check_flags MACVLAN DUMMY | sed 's/^/ /' +echo " - \"$(wrap_color 'ftp,tftp client in container' blue)\":" +check_flags NF_NAT_FTP NF_CONNTRACK_FTP NF_NAT_TFTP NF_CONNTRACK_TFTP | sed 's/^/ /' + +# only fail if no storage drivers available +CODE=${EXITCODE} +EXITCODE=0 +STORAGE=1 + +echo '- Storage Drivers:' +echo " - \"$(wrap_color 'btrfs' blue)\":" +check_flags BTRFS_FS | sed 's/^/ /' +check_flags BTRFS_FS_POSIX_ACL | sed 's/^/ /' +[ "$EXITCODE" = 0 ] && STORAGE=0 +EXITCODE=0 + +echo " - \"$(wrap_color 'overlay' blue)\":" +check_flags OVERLAY_FS | sed 's/^/ /' +[ "$EXITCODE" = 0 ] && STORAGE=0 +EXITCODE=0 + +echo " - \"$(wrap_color 'zfs' blue)\":" +printf ' - ' +check_device /dev/zfs +printf ' - ' +check_command zfs +printf ' - ' +check_command zpool +[ "$EXITCODE" = 0 ] && STORAGE=0 +EXITCODE=0 + +EXITCODE=$CODE +[ "$STORAGE" = 1 ] && EXITCODE=1 + +echo + +check_limit_over() { + if [ "$(cat "$1")" -le "$2" ]; then + wrap_bad "- $1" "$(cat "$1")" + wrap_color " This should be set to at least $2, for example set: sysctl -w kernel/keys/root_maxkeys=1000000" bold black + EXITCODE=1 + else + wrap_good "- $1" "$(cat "$1")" + fi +} + +echo 'Limits:' +check_limit_over /proc/sys/kernel/keys/root_maxkeys 10000 +echo + +exit $EXITCODE diff --git a/xtask/src/run.rs b/xtask/src/run.rs index 8bba7a532..2f59988a6 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -114,11 +114,11 @@ pub fn run(opts: Options) -> Result<()> { } = opts; type Binary = (String, PathBuf); - fn binaries(target: Option<&str>) -> Result)>> { + fn binaries(target: Option<&str>, is_vm: bool) -> Result)>> { ["dev", "release"] .into_iter() .map(|profile| { - let binaries = build(target, |cmd| { + let mut binaries = build(target, |cmd| { cmd.env(AYA_BUILD_INTEGRATION_BPF, "true").args([ "--package", "integration-test", @@ -127,6 +127,12 @@ pub fn run(opts: Options) -> Result<()> { profile, ]) })?; + if is_vm { + binaries.push(( + "check-config.sh".to_owned(), + PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/check-config.sh")), + )); + } anyhow::Ok((profile, binaries)) }) .collect() @@ -143,7 +149,7 @@ pub fn run(opts: Options) -> Result<()> { let runner = args.next().ok_or(anyhow!("no first argument"))?; let args = args.collect::>(); - let binaries = binaries(None)?; + let binaries = binaries(None, false)?; let mut failures = String::new(); for (profile, binaries) in binaries { @@ -281,7 +287,7 @@ pub fn run(opts: Options) -> Result<()> { init => bail!("expected exactly one init program, found {init:?}"), }; - let binaries = binaries(Some(&target))?; + let binaries = binaries(Some(&target), true)?; let tmp_dir = tempfile::tempdir().context("tempdir failed")?; @@ -325,10 +331,13 @@ pub fn run(opts: Options) -> Result<()> { for (profile, binaries) in binaries { for (name, binary) in binaries { let name = format!("{}-{}", profile, name); + eprintln!("Name: {name}"); let path = tmp_dir.path().join(&name); copy(&binary, &path).with_context(|| { format!("copy({}, {}) failed", binary.display(), path.display()) })?; + eprintln!("Binary: {}", binary.display()); + eprintln!("Path: {}", path.display()); for bytes in [ "file /bin/".as_bytes(), name.as_bytes(),