pub struct TlpPacket { /* private fields */ }Expand description
TLP Packet structure is high level abstraction for entire TLP packet Contains Header and Data
§Examples
use rtlp_lib::TlpPacket;
use rtlp_lib::TlpFmt;
use rtlp_lib::TlpType;
use rtlp_lib::TlpMode;
use rtlp_lib::new_msg_req;
use rtlp_lib::new_conf_req;
use rtlp_lib::new_mem_req;
use rtlp_lib::new_cmpl_req;
// Bytes for full TLP Packet
// <------- DW1 --------> <------- DW2 --------> <------- DW3 --------> <------- DW4 -------->
let bytes = vec![0x00, 0x00, 0x20, 0x01, 0x04, 0x00, 0x00, 0x01, 0x20, 0x01, 0xFF, 0x00, 0xC2, 0x81, 0xFF, 0x10];
let packet = TlpPacket::new(bytes, TlpMode::NonFlit).unwrap();
let header = packet.header();
// TLP Type tells us what is this packet
let tlp_type = header.tlp_type().unwrap();
let tlp_format = packet.tlp_format().unwrap();
let requester_id;
match (tlp_type) {
TlpType::MemReadReq |
TlpType::MemReadLockReq |
TlpType::MemWriteReq |
TlpType::DeferrableMemWriteReq |
TlpType::IOReadReq |
TlpType::IOWriteReq |
TlpType::FetchAddAtomicOpReq |
TlpType::SwapAtomicOpReq |
TlpType::CompareSwapAtomicOpReq => requester_id = new_mem_req(packet.data(), &tlp_format).unwrap().req_id(),
TlpType::ConfType0ReadReq |
TlpType::ConfType0WriteReq |
TlpType::ConfType1ReadReq |
TlpType::ConfType1WriteReq => requester_id = new_conf_req(packet.data().to_vec()).unwrap().req_id(),
TlpType::MsgReq |
TlpType::MsgReqData => requester_id = new_msg_req(packet.data().to_vec()).unwrap().req_id(),
TlpType::Cpl |
TlpType::CplData |
TlpType::CplLocked |
TlpType::CplDataLocked => requester_id = new_cmpl_req(packet.data().to_vec()).unwrap().req_id(),
TlpType::LocalTlpPrefix |
TlpType::EndToEndTlpPrefix => println!("I need to implement TLP Type: {:?}", tlp_type),
}Implementations§
Source§impl TlpPacket
impl TlpPacket
Sourcepub fn new(bytes: Vec<u8>, mode: TlpMode) -> Result<TlpPacket, TlpError>
pub fn new(bytes: Vec<u8>, mode: TlpMode) -> Result<TlpPacket, TlpError>
Create a new TlpPacket from raw bytes and the specified framing mode.
Use TlpMode::NonFlit for standard PCIe 1.0–5.0 TLP framing.
Use TlpMode::Flit for PCIe 6.x flit-mode TLP framing.
§Errors
TlpError::InvalidLengthifbytes.len() < 4or the flit header extends beyondbytes.TlpError::InvalidTypeif flit mode and the DW0 type byte is unknown.TlpError::MissingMandatoryOhcif flit mode and a mandatory OHC word is absent.
Sourcepub fn header(&self) -> &TlpPacketHeader
pub fn header(&self) -> &TlpPacketHeader
Returns a reference to the DW0 packet header.
For flit-mode packets the underlying TlpPacketHeader holds an all-zero
placeholder and its fields are meaningless. Check TlpPacket::mode
first and use TlpPacket::flit_type for flit-mode packets.
Sourcepub fn data(&self) -> &[u8] ⓘ
pub fn data(&self) -> &[u8] ⓘ
Returns the packet payload bytes (everything after the 4-byte DW0 header).
For flit-mode read requests this will be empty even when Length > 0.
Sourcepub fn tlp_type(&self) -> Result<TlpType, TlpError>
pub fn tlp_type(&self) -> Result<TlpType, TlpError>
Decode and return the TLP type from the DW0 header fields.
For flit-mode packets, use TlpPacket::flit_type instead.
§Errors
TlpError::NotImplementedif this packet was created withTlpMode::Flit.TlpError::InvalidFormatif the 3-bit Fmt field is unknown.TlpError::InvalidTypeif the 5-bit Type field is unknown.TlpError::UnsupportedCombinationif the Fmt/Type pair is not legal.
Sourcepub fn tlp_format(&self) -> Result<TlpFmt, TlpError>
pub fn tlp_format(&self) -> Result<TlpFmt, TlpError>
Decode and return the TLP format (3DW/4DW, with/without data) from DW0.
For flit-mode packets, use TlpPacket::flit_type instead.
§Errors
TlpError::NotImplementedif this packet was created withTlpMode::Flit.TlpError::InvalidFormatif the 3-bit Fmt field is not a known value.
Sourcepub fn flit_type(&self) -> Option<FlitTlpType>
pub fn flit_type(&self) -> Option<FlitTlpType>
Returns the flit-mode TLP type when the packet was created from
TlpMode::Flit bytes, or None for non-flit packets.
Sourcepub fn mode(&self) -> TlpMode
pub fn mode(&self) -> TlpMode
Returns the framing mode this packet was created with.
Use this to dispatch cleanly between the flit-mode and non-flit-mode
method sets. Relying on flit_type().is_some() as an indirect proxy
works but obscures intent; mode() makes the check self-documenting:
use rtlp_lib::{TlpPacket, TlpMode};
let pkt = TlpPacket::new(bytes, TlpMode::NonFlit).unwrap();
match pkt.mode() {
TlpMode::Flit => { /* use pkt.flit_type() */ }
TlpMode::NonFlit => { /* use pkt.tlp_type(), pkt.tlp_format() */ }
// TlpMode is #[non_exhaustive] — wildcard required in external code
_ => {}
}Sourcepub fn get_header(&self) -> &TlpPacketHeader
👎Deprecated since 0.5.0: use header() instead
pub fn get_header(&self) -> &TlpPacketHeader
use header() instead
Returns a reference to the DW0 packet header.
§Deprecation
Prefer TlpPacket::header which follows Rust naming conventions.
Sourcepub fn get_data(&self) -> Vec<u8> ⓘ
👎Deprecated since 0.5.0: use data() which returns &u8 instead
pub fn get_data(&self) -> Vec<u8> ⓘ
use data() which returns &u8 instead
Returns the packet payload bytes as an owned Vec<u8>.
§Deprecation
Prefer TlpPacket::data which returns &[u8] without allocating.
Sourcepub fn get_tlp_type(&self) -> Result<TlpType, TlpError>
👎Deprecated since 0.5.0: use tlp_type() instead
pub fn get_tlp_type(&self) -> Result<TlpType, TlpError>
use tlp_type() instead
Decode and return the TLP type from the DW0 header fields.
§Deprecation
Prefer TlpPacket::tlp_type which follows Rust naming conventions.
Sourcepub fn get_tlp_format(&self) -> Result<TlpFmt, TlpError>
👎Deprecated since 0.5.0: use tlp_format() instead
pub fn get_tlp_format(&self) -> Result<TlpFmt, TlpError>
use tlp_format() instead
Decode and return the TLP format from DW0.
§Deprecation
Prefer TlpPacket::tlp_format which follows Rust naming conventions.
Sourcepub fn get_flit_type(&self) -> Option<FlitTlpType>
👎Deprecated since 0.5.0: use flit_type() instead
pub fn get_flit_type(&self) -> Option<FlitTlpType>
use flit_type() instead
Returns the flit-mode TLP type.
§Deprecation
Prefer TlpPacket::flit_type which follows Rust naming conventions.