aya-log: Unify IP format hints into one, repsesent it by `:i` token

Having separate format hints and tokens per IP address family is
unnecessary, since they are represented by different types and we handle
format hints for each type separately. So we can just have one format
hint.

Also, we should be consistent with the format strings grammar in
Rust[0]. The `type` token, which is mapped to formatting traits, usually
consists of one letter[1] (and optional `?` for `Debug` trait, but that
doesn't matter for us). It shouldn't consist of multiple letters. Our
`:ipv4` and `:ipv6` tokens were clearly breaking that convention, so we
should rather switch to something with one letter - hence `:i`.

[0] https://doc.rust-lang.org/std/fmt/#syntax
[1] https://doc.rust-lang.org/std/fmt/#formatting-traits
pull/613/head
Michal Rostecki 2 years ago committed by Alessandro Decina
parent 5894c4ce82
commit 84e5e2894f

@ -156,10 +156,8 @@ pub enum DisplayHint {
LowerHex, LowerHex,
/// `:X` /// `:X`
UpperHex, UpperHex,
/// `:ipv4` /// `:i`
Ipv4, Ip,
/// `:ipv6`
Ipv6,
/// `:mac` /// `:mac`
LowerMac, LowerMac,
/// `:MAC` /// `:MAC`

@ -79,8 +79,7 @@ fn hint_to_expr(hint: DisplayHint) -> Result<Expr> {
DisplayHint::Default => parse_str("::aya_log_ebpf::macro_support::DisplayHint::Default"), DisplayHint::Default => parse_str("::aya_log_ebpf::macro_support::DisplayHint::Default"),
DisplayHint::LowerHex => parse_str("::aya_log_ebpf::macro_support::DisplayHint::LowerHex"), DisplayHint::LowerHex => parse_str("::aya_log_ebpf::macro_support::DisplayHint::LowerHex"),
DisplayHint::UpperHex => parse_str("::aya_log_ebpf::macro_support::DisplayHint::UpperHex"), DisplayHint::UpperHex => parse_str("::aya_log_ebpf::macro_support::DisplayHint::UpperHex"),
DisplayHint::Ipv4 => parse_str("::aya_log_ebpf::macro_support::DisplayHint::Ipv4"), DisplayHint::Ip => parse_str("::aya_log_ebpf::macro_support::DisplayHint::Ip"),
DisplayHint::Ipv6 => parse_str("::aya_log_ebpf::macro_support::DisplayHint::Ipv6"),
DisplayHint::LowerMac => parse_str("::aya_log_ebpf::macro_support::DisplayHint::LowerMac"), DisplayHint::LowerMac => parse_str("::aya_log_ebpf::macro_support::DisplayHint::LowerMac"),
DisplayHint::UpperMac => parse_str("::aya_log_ebpf::macro_support::DisplayHint::UpperMac"), DisplayHint::UpperMac => parse_str("::aya_log_ebpf::macro_support::DisplayHint::UpperMac"),
} }

@ -57,8 +57,7 @@ fn parse_display_hint(s: &str) -> Result<DisplayHint, String> {
Ok(match s { Ok(match s {
"x" => DisplayHint::LowerHex, "x" => DisplayHint::LowerHex,
"X" => DisplayHint::UpperHex, "X" => DisplayHint::UpperHex,
"ipv4" => DisplayHint::Ipv4, "i" => DisplayHint::Ip,
"ipv6" => DisplayHint::Ipv6,
"mac" => DisplayHint::LowerMac, "mac" => DisplayHint::LowerMac,
"MAC" => DisplayHint::UpperMac, "MAC" => DisplayHint::UpperMac,
_ => return Err(format!("unknown display hint: {s:?}")), _ => return Err(format!("unknown display hint: {s:?}")),
@ -146,7 +145,7 @@ mod test {
#[test] #[test]
fn test_parse() { fn test_parse() {
assert_eq!( assert_eq!(
parse("foo {} bar {:x} test {:X} ayy {:ipv4} lmao {:ipv6} {{}} {{something}}"), parse("foo {} bar {:x} test {:X} ayy {:i} lmao {{}} {{something}}"),
Ok(vec![ Ok(vec![
Fragment::Literal("foo ".into()), Fragment::Literal("foo ".into()),
Fragment::Parameter(Parameter { Fragment::Parameter(Parameter {
@ -162,12 +161,9 @@ mod test {
}), }),
Fragment::Literal(" ayy ".into()), Fragment::Literal(" ayy ".into()),
Fragment::Parameter(Parameter { Fragment::Parameter(Parameter {
hint: DisplayHint::Ipv4 hint: DisplayHint::Ip
}), }),
Fragment::Literal(" lmao ".into()), Fragment::Literal(" lmao ".into()),
Fragment::Parameter(Parameter {
hint: DisplayHint::Ipv6
}),
Fragment::Literal(" {{}} {{something}}".into()), Fragment::Literal(" {{}} {{something}}".into()),
]) ])
); );

@ -258,8 +258,7 @@ impl Format for u32 {
Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)), Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)),
Some(DisplayHint::LowerHex) => Ok(LowerHexFormatter::format(self)), Some(DisplayHint::LowerHex) => Ok(LowerHexFormatter::format(self)),
Some(DisplayHint::UpperHex) => Ok(UpperHexFormatter::format(self)), Some(DisplayHint::UpperHex) => Ok(UpperHexFormatter::format(self)),
Some(DisplayHint::Ipv4) => Ok(Ipv4Formatter::format(*self)), Some(DisplayHint::Ip) => Ok(Ipv4Formatter::format(*self)),
Some(DisplayHint::Ipv6) => Err(()),
Some(DisplayHint::LowerMac) => Err(()), Some(DisplayHint::LowerMac) => Err(()),
Some(DisplayHint::UpperMac) => Err(()), Some(DisplayHint::UpperMac) => Err(()),
_ => Ok(DefaultFormatter::format(self)), _ => Ok(DefaultFormatter::format(self)),
@ -273,8 +272,7 @@ impl Format for [u8; 6] {
Some(DisplayHint::Default) => Err(()), Some(DisplayHint::Default) => Err(()),
Some(DisplayHint::LowerHex) => Err(()), Some(DisplayHint::LowerHex) => Err(()),
Some(DisplayHint::UpperHex) => Err(()), Some(DisplayHint::UpperHex) => Err(()),
Some(DisplayHint::Ipv4) => Err(()), Some(DisplayHint::Ip) => Err(()),
Some(DisplayHint::Ipv6) => Err(()),
Some(DisplayHint::LowerMac) => Ok(LowerMacFormatter::format(*self)), Some(DisplayHint::LowerMac) => Ok(LowerMacFormatter::format(*self)),
Some(DisplayHint::UpperMac) => Ok(UpperMacFormatter::format(*self)), Some(DisplayHint::UpperMac) => Ok(UpperMacFormatter::format(*self)),
_ => Err(()), _ => Err(()),
@ -288,8 +286,7 @@ impl Format for [u8; 16] {
Some(DisplayHint::Default) => Err(()), Some(DisplayHint::Default) => Err(()),
Some(DisplayHint::LowerHex) => Err(()), Some(DisplayHint::LowerHex) => Err(()),
Some(DisplayHint::UpperHex) => Err(()), Some(DisplayHint::UpperHex) => Err(()),
Some(DisplayHint::Ipv4) => Err(()), Some(DisplayHint::Ip) => Ok(Ipv6Formatter::format(*self)),
Some(DisplayHint::Ipv6) => Ok(Ipv6Formatter::format(*self)),
Some(DisplayHint::LowerMac) => Err(()), Some(DisplayHint::LowerMac) => Err(()),
Some(DisplayHint::UpperMac) => Err(()), Some(DisplayHint::UpperMac) => Err(()),
_ => Err(()), _ => Err(()),
@ -303,8 +300,7 @@ impl Format for [u16; 8] {
Some(DisplayHint::Default) => Err(()), Some(DisplayHint::Default) => Err(()),
Some(DisplayHint::LowerHex) => Err(()), Some(DisplayHint::LowerHex) => Err(()),
Some(DisplayHint::UpperHex) => Err(()), Some(DisplayHint::UpperHex) => Err(()),
Some(DisplayHint::Ipv4) => Err(()), Some(DisplayHint::Ip) => Ok(Ipv6Formatter::format(*self)),
Some(DisplayHint::Ipv6) => Ok(Ipv6Formatter::format(*self)),
Some(DisplayHint::LowerMac) => Err(()), Some(DisplayHint::LowerMac) => Err(()),
Some(DisplayHint::UpperMac) => Err(()), Some(DisplayHint::UpperMac) => Err(()),
_ => Err(()), _ => Err(()),
@ -320,8 +316,7 @@ macro_rules! impl_format {
Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)), Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)),
Some(DisplayHint::LowerHex) => Ok(LowerHexFormatter::format(self)), Some(DisplayHint::LowerHex) => Ok(LowerHexFormatter::format(self)),
Some(DisplayHint::UpperHex) => Ok(UpperHexFormatter::format(self)), Some(DisplayHint::UpperHex) => Ok(UpperHexFormatter::format(self)),
Some(DisplayHint::Ipv4) => Err(()), Some(DisplayHint::Ip) => Err(()),
Some(DisplayHint::Ipv6) => Err(()),
Some(DisplayHint::LowerMac) => Err(()), Some(DisplayHint::LowerMac) => Err(()),
Some(DisplayHint::UpperMac) => Err(()), Some(DisplayHint::UpperMac) => Err(()),
_ => Ok(DefaultFormatter::format(self)), _ => Ok(DefaultFormatter::format(self)),
@ -350,8 +345,7 @@ macro_rules! impl_format_float {
Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)), Some(DisplayHint::Default) => Ok(DefaultFormatter::format(self)),
Some(DisplayHint::LowerHex) => Err(()), Some(DisplayHint::LowerHex) => Err(()),
Some(DisplayHint::UpperHex) => Err(()), Some(DisplayHint::UpperHex) => Err(()),
Some(DisplayHint::Ipv4) => Err(()), Some(DisplayHint::Ip) => Err(()),
Some(DisplayHint::Ipv6) => Err(()),
Some(DisplayHint::LowerMac) => Err(()), Some(DisplayHint::LowerMac) => Err(()),
Some(DisplayHint::UpperMac) => Err(()), Some(DisplayHint::UpperMac) => Err(()),
_ => Ok(DefaultFormatter::format(self)), _ => Ok(DefaultFormatter::format(self)),
@ -752,7 +746,7 @@ mod test {
let (mut len, mut input) = new_log(3).unwrap(); let (mut len, mut input) = new_log(3).unwrap();
len += "ipv4: ".write(&mut input[len..]).unwrap(); len += "ipv4: ".write(&mut input[len..]).unwrap();
len += DisplayHint::Ipv4.write(&mut input[len..]).unwrap(); len += DisplayHint::Ip.write(&mut input[len..]).unwrap();
// 10.0.0.1 as u32 // 10.0.0.1 as u32
len += 167772161u32.write(&mut input[len..]).unwrap(); len += 167772161u32.write(&mut input[len..]).unwrap();
@ -773,7 +767,7 @@ mod test {
let (mut len, mut input) = new_log(3).unwrap(); let (mut len, mut input) = new_log(3).unwrap();
len += "ipv6: ".write(&mut input[len..]).unwrap(); len += "ipv6: ".write(&mut input[len..]).unwrap();
len += DisplayHint::Ipv6.write(&mut input[len..]).unwrap(); len += DisplayHint::Ip.write(&mut input[len..]).unwrap();
// 2001:db8::1:1 as byte array // 2001:db8::1:1 as byte array
let ipv6_arr: [u8; 16] = [ let ipv6_arr: [u8; 16] = [
0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
@ -798,7 +792,7 @@ mod test {
let (mut len, mut input) = new_log(3).unwrap(); let (mut len, mut input) = new_log(3).unwrap();
len += "ipv6: ".write(&mut input[len..]).unwrap(); len += "ipv6: ".write(&mut input[len..]).unwrap();
len += DisplayHint::Ipv6.write(&mut input[len..]).unwrap(); len += DisplayHint::Ip.write(&mut input[len..]).unwrap();
// 2001:db8::1:1 as u16 array // 2001:db8::1:1 as u16 array
let ipv6_arr: [u16; 8] = [ let ipv6_arr: [u16; 8] = [
0x2001, 0x0db8, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0001, 0x2001, 0x0db8, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0001,

@ -12,7 +12,7 @@ pub fn test_log(ctx: ProbeContext) {
let ipv6 = [ let ipv6 = [
32u8, 1u8, 13u8, 184u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8, 32u8, 1u8, 13u8, 184u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8,
]; // 2001:db8::1 ]; // 2001:db8::1
info!(&ctx, "ipv4: {:ipv4}, ipv6: {:ipv6}", ipv4, ipv6); info!(&ctx, "ipv4: {:i}, ipv6: {:i}", ipv4, ipv6);
let mac = [4u8, 32u8, 6u8, 9u8, 0u8, 64u8]; let mac = [4u8, 32u8, 6u8, 9u8, 0u8, 64u8];
trace!(&ctx, "mac lc: {:mac}, mac uc: {:MAC}", mac, mac); trace!(&ctx, "mac lc: {:mac}, mac uc: {:MAC}", mac, mac);
let hex = 0x2f; let hex = 0x2f;

Loading…
Cancel
Save