Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix deparse for non-aligned fields > 8 bits #49

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions codegen/rust/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,16 @@ impl<'a> HeaderGenerator<'a> {
// NOTE this barfing and then unbarfing a vec is to handle
// the p4 confused-endian data model.
let mut v = self.#name.clone().into_vec();
if ((#end-#offset) % 8) != 0 {
if let Some(x) = v.iter_mut().last() {
*x >>= ((#end - #offset) % 8);
}
}
v.reverse();
let n = (#end-#offset);
let m = n%8;
if (n > 8) && m != 0 {
let mut b = BitVec::<u8, Msb0>::from_vec(v);
if b.len() > m {
x[#offset..#end] |= &b[m..];
} else {
x[#offset..#end] |= &b;
}
} else {
let mut b = BitVec::<u8, Msb0>::from_vec(v);
b.resize(#end-#offset, false);
x[#offset..#end] |= &b;
}
let mut b = BitVec::<u8, Msb0>::from_vec(v);
x[#offset..#end] |= &b[m..];

});
checksum_statements.push(quote! {
Expand Down
10 changes: 8 additions & 2 deletions test/src/vlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ p4_macro::use_p4!("test/src/p4/vlan_header.p4");

#[test]
fn test_vlan_parse() -> anyhow::Result<()> {
let mut data = [0u8; 256];
let mut data = [0u8; 4];
data[0] = 0x0;
data[1] = 0x47;
let mut pkt = vlan_h::new();
pkt.set(&data).unwrap();
let vid: u16 = pkt.vid.to_owned().load_le();
assert_eq!(vid, 0x47);
let bv = pkt.to_bitvec();
let readback = bv.into_vec();
assert_eq!(data.to_vec(), readback);

let mut data = [0u8; 256];
let mut data = [0u8; 4];
data[0] = 0x77;
data[1] = 0x47;
let mut pkt = vlan_h::new();
pkt.set(&data).unwrap();
let vid: u16 = pkt.vid.to_owned().load_le();
assert_eq!(vid, 0x747);
let bv = pkt.to_bitvec();
let readback = bv.into_vec();
assert_eq!(data.to_vec(), readback);

Ok(())
}
Loading