lib.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #![no_std]
  2. use core::{mem, num, ptr};
  3. use num_enum::IntoPrimitive;
  4. pub const LOG_BUF_CAPACITY: usize = 8192;
  5. pub const LOG_FIELDS: usize = 6;
  6. pub type LogValueLength = u16;
  7. #[repr(u8)]
  8. #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, IntoPrimitive)]
  9. pub enum Level {
  10. /// The "error" level.
  11. ///
  12. /// Designates very serious errors.
  13. Error = 1,
  14. /// The "warn" level.
  15. ///
  16. /// Designates hazardous situations.
  17. Warn,
  18. /// The "info" level.
  19. ///
  20. /// Designates useful information.
  21. Info,
  22. /// The "debug" level.
  23. ///
  24. /// Designates lower priority information.
  25. Debug,
  26. /// The "trace" level.
  27. ///
  28. /// Designates very low priority, often extremely verbose, information.
  29. Trace,
  30. }
  31. macro_rules! impl_formatter_for_types {
  32. ($trait:path : { $($type:ty),*}) => {
  33. $(
  34. impl $trait for $type {}
  35. )*
  36. };
  37. }
  38. pub trait DefaultFormatter {}
  39. impl_formatter_for_types!(
  40. DefaultFormatter: {
  41. bool,
  42. i8, i16, i32, i64, isize,
  43. u8, u16, u32, u64, usize,
  44. f32, f64,
  45. char,
  46. str,
  47. &str
  48. }
  49. );
  50. pub trait LowerHexFormatter {}
  51. impl_formatter_for_types!(
  52. LowerHexFormatter: {
  53. i8, i16, i32, i64, isize,
  54. u8, u16, u32, u64, usize,
  55. &[u8]
  56. }
  57. );
  58. pub trait UpperHexFormatter {}
  59. impl_formatter_for_types!(
  60. UpperHexFormatter: {
  61. i8, i16, i32, i64, isize,
  62. u8, u16, u32, u64, usize,
  63. &[u8]
  64. }
  65. );
  66. pub trait IpFormatter {}
  67. impl IpFormatter for u32 {}
  68. impl IpFormatter for [u8; 16] {}
  69. impl IpFormatter for [u16; 8] {}
  70. pub trait LowerMacFormatter {}
  71. impl LowerMacFormatter for [u8; 6] {}
  72. pub trait UpperMacFormatter {}
  73. impl UpperMacFormatter for [u8; 6] {}
  74. #[repr(u8)]
  75. #[derive(Copy, Clone, Debug)]
  76. pub enum RecordField {
  77. Target = 1,
  78. Level,
  79. Module,
  80. File,
  81. Line,
  82. NumArgs,
  83. }
  84. /// Types which are supported by aya-log and can be safely sent from eBPF
  85. /// programs to userspace.
  86. #[repr(u8)]
  87. #[derive(Copy, Clone, Debug)]
  88. pub enum Argument {
  89. DisplayHint,
  90. I8,
  91. I16,
  92. I32,
  93. I64,
  94. Isize,
  95. U8,
  96. U16,
  97. U32,
  98. U64,
  99. Usize,
  100. F32,
  101. F64,
  102. /// `[u8; 6]` array which represents a MAC address.
  103. ArrU8Len6,
  104. /// `[u8; 16]` array which represents an IPv6 address.
  105. ArrU8Len16,
  106. /// `[u16; 8]` array which represents an IPv6 address.
  107. ArrU16Len8,
  108. Bytes,
  109. Str,
  110. }
  111. /// All display hints
  112. #[repr(u8)]
  113. #[derive(Copy, Clone, Debug, PartialEq, Eq, IntoPrimitive)]
  114. pub enum DisplayHint {
  115. /// Default string representation.
  116. Default = 1,
  117. /// `:x`
  118. LowerHex,
  119. /// `:X`
  120. UpperHex,
  121. /// `:i`
  122. Ip,
  123. /// `:mac`
  124. LowerMac,
  125. /// `:MAC`
  126. UpperMac,
  127. }
  128. struct TagLenValue<T, V> {
  129. pub tag: T,
  130. pub value: V,
  131. }
  132. impl<T, V> TagLenValue<T, V>
  133. where
  134. V: IntoIterator<Item = u8>,
  135. <V as IntoIterator>::IntoIter: ExactSizeIterator,
  136. {
  137. pub(crate) fn write(self, mut buf: &mut [u8]) -> Result<usize, ()> {
  138. // Break the abstraction to please the verifier.
  139. if buf.len() > LOG_BUF_CAPACITY {
  140. buf = &mut buf[..LOG_BUF_CAPACITY];
  141. }
  142. let Self { tag, value } = self;
  143. let value = value.into_iter();
  144. let len = value.len();
  145. let wire_len: LogValueLength = value
  146. .len()
  147. .try_into()
  148. .map_err(|num::TryFromIntError { .. }| ())?;
  149. let size = mem::size_of_val(&tag) + mem::size_of_val(&wire_len) + len;
  150. if size > buf.len() {
  151. return Err(());
  152. }
  153. let tag_size = mem::size_of_val(&tag);
  154. unsafe { ptr::write_unaligned(buf.as_mut_ptr() as *mut _, tag) };
  155. buf = &mut buf[tag_size..];
  156. unsafe { ptr::write_unaligned(buf.as_mut_ptr() as *mut _, wire_len) };
  157. buf = &mut buf[mem::size_of_val(&wire_len)..];
  158. buf.iter_mut().zip(value).for_each(|(dst, src)| {
  159. *dst = src;
  160. });
  161. Ok(size)
  162. }
  163. }
  164. impl<T, V> TagLenValue<T, V> {
  165. #[inline(always)]
  166. pub(crate) fn new(tag: T, value: V) -> TagLenValue<T, V> {
  167. TagLenValue { tag, value }
  168. }
  169. }
  170. pub trait WriteToBuf {
  171. #[allow(clippy::result_unit_err)]
  172. fn write(self, buf: &mut [u8]) -> Result<usize, ()>;
  173. }
  174. macro_rules! impl_write_to_buf {
  175. ($type:ident, $arg_type:expr) => {
  176. impl WriteToBuf for $type {
  177. fn write(self, buf: &mut [u8]) -> Result<usize, ()> {
  178. TagLenValue::new($arg_type, self.to_ne_bytes()).write(buf)
  179. }
  180. }
  181. };
  182. }
  183. impl_write_to_buf!(i8, Argument::I8);
  184. impl_write_to_buf!(i16, Argument::I16);
  185. impl_write_to_buf!(i32, Argument::I32);
  186. impl_write_to_buf!(i64, Argument::I64);
  187. impl_write_to_buf!(isize, Argument::Isize);
  188. impl_write_to_buf!(u8, Argument::U8);
  189. impl_write_to_buf!(u16, Argument::U16);
  190. impl_write_to_buf!(u32, Argument::U32);
  191. impl_write_to_buf!(u64, Argument::U64);
  192. impl_write_to_buf!(usize, Argument::Usize);
  193. impl_write_to_buf!(f32, Argument::F32);
  194. impl_write_to_buf!(f64, Argument::F64);
  195. impl WriteToBuf for [u8; 16] {
  196. fn write(self, buf: &mut [u8]) -> Result<usize, ()> {
  197. TagLenValue::new(Argument::ArrU8Len16, self).write(buf)
  198. }
  199. }
  200. impl WriteToBuf for [u16; 8] {
  201. fn write(self, buf: &mut [u8]) -> Result<usize, ()> {
  202. let bytes = unsafe { core::mem::transmute::<_, [u8; 16]>(self) };
  203. TagLenValue::new(Argument::ArrU16Len8, bytes).write(buf)
  204. }
  205. }
  206. impl WriteToBuf for [u8; 6] {
  207. fn write(self, buf: &mut [u8]) -> Result<usize, ()> {
  208. TagLenValue::new(Argument::ArrU8Len6, self).write(buf)
  209. }
  210. }
  211. impl WriteToBuf for &[u8] {
  212. fn write(self, buf: &mut [u8]) -> Result<usize, ()> {
  213. TagLenValue::new(Argument::Bytes, self.iter().copied()).write(buf)
  214. }
  215. }
  216. impl WriteToBuf for &str {
  217. fn write(self, buf: &mut [u8]) -> Result<usize, ()> {
  218. TagLenValue::new(Argument::Str, self.as_bytes().iter().copied()).write(buf)
  219. }
  220. }
  221. impl WriteToBuf for DisplayHint {
  222. fn write(self, buf: &mut [u8]) -> Result<usize, ()> {
  223. let v: u8 = self.into();
  224. TagLenValue::new(Argument::DisplayHint, v.to_ne_bytes()).write(buf)
  225. }
  226. }
  227. #[allow(clippy::result_unit_err)]
  228. #[doc(hidden)]
  229. #[inline(always)]
  230. pub fn write_record_header(
  231. buf: &mut [u8],
  232. target: &str,
  233. level: Level,
  234. module: &str,
  235. file: &str,
  236. line: u32,
  237. num_args: usize,
  238. ) -> Result<usize, ()> {
  239. let level: u8 = level.into();
  240. let mut size = 0;
  241. size += TagLenValue::new(RecordField::Target, target.as_bytes().iter().copied())
  242. .write(&mut buf[size..])?;
  243. size += TagLenValue::new(RecordField::Level, level.to_ne_bytes()).write(&mut buf[size..])?;
  244. size += TagLenValue::new(RecordField::Module, module.as_bytes().iter().copied())
  245. .write(&mut buf[size..])?;
  246. size += TagLenValue::new(RecordField::File, file.as_bytes().iter().copied())
  247. .write(&mut buf[size..])?;
  248. size += TagLenValue::new(RecordField::Line, line.to_ne_bytes()).write(&mut buf[size..])?;
  249. size +=
  250. TagLenValue::new(RecordField::NumArgs, num_args.to_ne_bytes()).write(&mut buf[size..])?;
  251. Ok(size)
  252. }
  253. #[cfg(test)]
  254. mod test {
  255. use super::*;
  256. #[test]
  257. fn log_value_length_sufficient() {
  258. assert!(
  259. LOG_BUF_CAPACITY <= LogValueLength::MAX.into(),
  260. "{} > {}",
  261. LOG_BUF_CAPACITY,
  262. LogValueLength::MAX
  263. );
  264. }
  265. }