Skip to content

Commit

Permalink
Definition for ethernet flow control packets.
Browse files Browse the repository at this point in the history
  • Loading branch information
AJMansfield committed Sep 21, 2023
1 parent 87f362d commit 12ca962
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
67 changes: 67 additions & 0 deletions pnet_packet/src/flowcontrol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) 2023 Anson Mansfield <amansfield@mantaro.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Ethernet Flow Control \[IEEE 802.3x\] abstraction.

use crate::PrimitiveValues;

use alloc::vec::Vec;
use core::fmt;

use pnet_macros::packet;
use pnet_macros_support::types::u16be;

/// Represents the opcode field in an Ethernet Flow Control packet.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FlowControlOpcode(pub u16);

impl FlowControlOpcode {
pub fn new(value: u16) -> Self {
FlowControlOpcode(value)
}
}
impl PrimitiveValues for FlowControlOpcode {
type T = (u16,);
fn to_primitive_values(&self) -> (u16,) {
(self.0,)
}
}
impl fmt::Display for FlowControlOpcode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,
"{}",
match self {
&FlowControlOpcodes::Pause => "pause",
_ => "unknown",
})
}
}

/// Flow control opcodes are defined in IEEE 802.3x
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub mod FlowControlOpcodes {
use super::FlowControlOpcode;

/// Request the other station pause for 512*quanta bit times.
pub const Pause: FlowControlOpcode = FlowControlOpcode(1);
}

/// Represents an Ethernet Flow Control packet defined by IEEE 802.3x.
/// ([wikipedia](https://en.wikipedia.org/wiki/Ethernet_flow_control))
///
/// Use with the [EtherTypes::FlowControl](crate::ethernet::EtherTypes::FlowControl) ethertype (0x8808).
#[packet]
#[allow(non_snake_case)]
pub struct FlowControl {
#[construct_with(u16)]
pub command: FlowControlOpcode,
pub quanta: u16be,
#[payload]
pub payload: Vec<u8>,
}
1 change: 1 addition & 0 deletions pnet_packet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub use pnet_macros_support::packet::*;
pub mod arp;
pub mod dhcp;
pub mod ethernet;
pub mod flowcontrol;
pub mod gre;
pub mod icmp;
pub mod icmpv6;
Expand Down

0 comments on commit 12ca962

Please sign in to comment.