JFLib

netpkt/stp.rs#

Source: lib/netpkt/stp.rs

stp -- Bridge Protocol Data Units of the spanning tree protocols: Configuration and Topology
Change Notification BPDUs (IEEE 802.1D-1998 clause 9) and RST BPDUs (IEEE 802.1D-2004 9.3.3,
RSTP of 802.1w) with their flags, bridge and port identifiers, the recommended path costs,
and the 802.3 frames with LLC that carry them to 01:80:c2:00:00:00

  BridgeId(u64)            priority (16 bits: 4 settable, 12 system ID extension) and MAC:
                           new(priority, mac), priority(), ext(), mac(); Display
                           "4096/0/02:4a:00:00:01:00" as capture tools write it
  PortId(u16)              priority (4 bits, in steps of 16) and port number (12 bits):
                           new(priority, number), priority(), number(); Display "128.1"
  path_cost(kbps)          the recommended Port Path Cost of 802.1D-2004 Table 17-3:
                           20 000 000 000 / speed in kbit/s, 1 .. 200 000 000 (100M 200 000,
                           1G 20 000, 2.5G 8 000, 10G 2 000, 25G 800, 100G 200)
  Role                     the port role of an RST BPDU (Unknown, AltBackup, Root, Designated)
  F_TC, F_PROPOSAL, F_ROLE, F_LEARNING, F_FORWARDING, F_AGREEMENT, F_TCA   the flag bits
  Bpdu { kind, version, flags, root, cost, bridge, port, msg_age, max_age, hello, fwd_delay }
                           times in 1/256 s (TIME_UNIT per second); config / tcn / rst build
                           one from whole seconds (Times), with_role sets the role bits,
                           times() reads them back in seconds
  Bpdu::parse(b)           the BPDU after the LLC header, validated as 802.1D-2004 9.3.4 says
                           (protocol identifier 0, type and length); Err with the reason
  Bpdu::to_bytes()         35 (Configuration), 4 (TCN) or 36 bytes (RST)
  Bpdu::frame(src)         the whole frame: 01:80:c2:00:00:00, the 802.3 length, LLC
                           42 42 03, the BPDU, padded to 60 bytes
  of_frame(frame)          the BPDU of a received frame (untagged or tagged), None for any
                           other frame or a BPDU that does not validate
  flags_text(kind, flags)  "Forwarding, Learning, Port Role: Designated" (dissectors)
  summary(&Bpdu)           a one-line summary like a capture tool's ("RST. Root =
                           4096/0/02:4a:00:00:01:00  Cost = 0  Port = 0x8001")

Multi-byte fields are big endian.  The receiver checks what needs its own state (a Message Age
that is not below Max Age, its own BPDU looped back); the codec only reads and writes.
/// BPDU timer values count in 1/256 s
TIME_UNIT: u16 = 256
/// the lengths of the three BPDUs
CONFIG_LEN: usize = 35
TCN_LEN: usize = 4
RST_LEN: usize = 36
TYPE_CONFIG: u8 = 0x00
TYPE_RST: u8 = 0x02
TYPE_TCN: u8 = 0x80
/// Protocol Version Identifiers: 802.1D STP, RSTP (802.1w), MSTP (802.1s)
VERSION_STP: u8 = 0
VERSION_RSTP: u8 = 2
VERSION_MSTP: u8 = 3
/// flag bits: Topology Change (both BPDUs), the RST BPDU's own ones, and the Topology Change
/// Acknowledgment of Configuration BPDUs
F_TC: u8 = 0x01
F_PROPOSAL: u8 = 0x02
F_ROLE: u8 = 0x0C
F_LEARNING: u8 = 0x10
F_FORWARDING: u8 = 0x20
F_AGREEMENT: u8 = 0x40
F_TCA: u8 = 0x80
  • struct BridgeId(pub u64) a bridge identifier: the 16-bit priority (4 settable bits and the 12-bit system ID extension, 0 for one spanning tree) and the bridge's MAC address; lower is better

impl BridgeId

  • fn new(priority: u16, mac: Mac) -> BridgeId

  • fn priority(&self) -> u16 the whole priority field (settable part plus system ID extension)

  • fn ext(&self) -> u16 the system ID extension (the low 12 bits of the priority field)

  • fn mac(&self) -> Mac

  • fn bytes(&self) -> [u8; 8]

  • fn from_bytes(b: &[u8]) -> BridgeId

  • struct PortId(pub u16) a port identifier: 4 bits of priority (0 .. 240 in steps of 16) and a 12-bit port number

impl PortId

  • fn new(priority: u8, number: u16) -> PortId

  • fn priority(&self) -> u8

  • fn number(&self) -> u16

  • fn path_cost(kbps: u32) -> u32 the recommended Port Path Cost of a link speed (802.1D-2004 Table 17-3: 20 000 000 000 divided by the speed in kbit/s, within 1 .. 200 000 000)

  • enum Role the port role an RST BPDU conveys (flag bits 3 and 4)

    • Unknown
    • AltBackup
    • Root
    • Designated

impl Role

  • fn bits(self) -> u8

  • fn of_flags(flags: u8) -> Role

  • fn name(self) -> &'static str

  • enum Kind which BPDU

    • Config
    • Tcn
    • Rst
  • struct Times the four timer values of a BPDU in whole seconds

    • msg_age: u16
    • max_age: u16
    • hello: u16
    • fwd_delay: u16
  • struct Bpdu a BPDU; the times count in 1/256 s

    • kind: Kind
    • version: u8 -- the Protocol Version Identifier as sent (0 for Configuration and TCN BPDUs, 2 for RSTP)
    • flags: u8
    • root: BridgeId
    • cost: u32
    • bridge: BridgeId
    • port: PortId
    • msg_age: u16
    • max_age: u16
    • hello: u16
    • fwd_delay: u16
  • enum BpduErr why bytes are no BPDU

    • Short -- shorter than its type needs
    • Protocol -- the Protocol Identifier is not 0
    • Type -- a BPDU type (or type and version) this codec does not know

impl BpduErr

  • fn text(self) -> &'static str

impl Bpdu

  • fn config(flags: u8, root: BridgeId, cost: u32, bridge: BridgeId, port: PortId, t: Times) -> Bpdu a Configuration BPDU (only the TC and TC acknowledgment flags count)

  • fn tcn() -> Bpdu a Topology Change Notification BPDU

  • fn rst(flags: u8, root: BridgeId, cost: u32, bridge: BridgeId, port: PortId, t: Times) -> Bpdu an RST BPDU (the port role is part of the flags: Role::bits, with_role)

  • fn with_role(mut self, r: Role) -> Bpdu the same BPDU conveying another port role

  • fn role(&self) -> Role

  • fn tc(&self) -> bool

  • fn tca(&self) -> bool

  • fn proposal(&self) -> bool

  • fn agreement(&self) -> bool

  • fn learning(&self) -> bool

  • fn forwarding(&self) -> bool

  • fn times(&self) -> Times the timer values in whole seconds (rounded down)

  • fn parse(b: &[u8]) -> Result<Bpdu, BpduErr> the BPDU in b (the bytes after the LLC header), as 802.1D-2004 9.3.4 validates it

  • fn to_bytes(&self) -> Vec<u8> the bytes of the BPDU

  • fn frame(&self, src: Mac) -> Vec<u8> the frame that carries the BPDU from a port with MAC src: IEEE 802.3 to the bridge group address, LLC with SAP 0x42, padded to the 60-byte minimum

  • fn of_frame(frame: &[u8]) -> Option<Bpdu> the BPDU a received frame carries: to 01:80:c2:00:00:00 (tagged or not), an 802.3 length, LLC with SAP 0x42; None for any other frame and for one that does not validate

  • fn flags_text(kind: Kind, flags: u8) -> String the flags in words, as capture tools list them ("Agreement, Forwarding, Learning, Port Role: Root"; "Topology Change" first, the acknowledgment last; "none")

  • fn summary(b: &Bpdu) -> String one line about a BPDU, as capture tools sum one up ("Conf. TC + Root = 32768/0/02:4a:00:00:01:00 Cost = 20000 Port = 0x8002", "RST. Root = ...", "Topology Change Notification")

Source: docs/API.md in the JFLib repository.