Struct PacketRc

Source
pub struct PacketRc { /* private fields */ }
Expand description

A thread-safe shared reference to a Packet.

Although a PacketRc is thread-safe, a panic may occur if multiple threads concurrently write PacketStatus metadata on the inner Packet. Thus, use new_copy_inner() to copy the inner Packet and send the copy to the other thread rather than sharing it between threads.

A PacketRc is a wrapper around a Packet and allows us to support reference counting on Packets while safely sharing them across threads. A clone of a PacketRcs increments the reference count of the shared Packet while dropping the PacketRc decrements the reference count. The shared inner Packet is only dropped when all PacketRcs referring to it are also dropped (i.e., the reference count reaches zero).

The PartialEq implementation on PacketRc compares the pointer values of the wrapped Packet.

PacketRc implements the Deref trait so that all non-associated functions on Packet can be accessed from instances of PacketRc too.

Implementations§

Source§

impl PacketRc

Source

pub fn new_ipv4_tcp( header: TcpHeader, payload: Payload, priority: FifoPacketPriority, ) -> Self

Creates a thread-safe shared reference to a new Packet using the provided information. Additional references to the Packet can be cheaply obtained by cloning the returned PacketRc. The Packet is dropped when its last PacketRc reference is dropped.

See Packet::new_ipv4_tcp() for more details.

Source

pub fn new_ipv4_udp( src: SocketAddrV4, dst: SocketAddrV4, payload: Bytes, priority: FifoPacketPriority, ) -> Self

Creates a thread-safe shared reference to a new Packet using the provided information. Additional references to the Packet can be cheaply obtained by cloning the returned PacketRc. The Packet is dropped when its last PacketRc reference is dropped.

See Packet::new_ipv4_udp() for more details.

Source

pub fn new_copy_inner(&self) -> Self

Creates a thread-safe shared reference to a new Packet that is created by performing a copy of the provided referenced Packet. This function copies all packet data except the packet payload, which is shared by incrementing its reference count. Additional references to the Packet can be cheaply obtained by cloning the returned PacketRc. The Packet is dropped when its last PacketRc reference is dropped.

Source

pub fn from_raw(packet_ptr: *mut Packet) -> Self

Transfers ownership of the given packet_ptr reference into a new PacketRc object. The provided pointer must have been obtained from a call to the Rust function PacketRc::into_raw() or the C function packet_new_tcp().

Use PacketRc::into_raw() if the returned reference is to be handed back to C code.

§Panics

This function panics if the supplied packet pointer is NULL.

§Deprecation

This function provides compatibility with the legacy C TCP stack and should be considered deprecated and removed when the legacy C TCP stack is removed.

Source

pub fn into_raw(self) -> *mut Packet

Transfers ownership of the inner Arc reference to the caller while dropping the PacketRc.

To avoid a memory leak, the returned pointer must be either reconstituted into a PacketRc using the Rust function PacketRc::from_raw(), or dropped using the C function packet_unref().

Although this returns a *mut Packet pointer, the packet is not actually mutuable. We return a *mut Packet pointer only to avoid having to change the instnaces of the pointers to const instances in the C network code.

§Deprecation

This function provides compatibility with the legacy C TCP stack and should be considered deprecated and removed when the legacy C TCP stack is removed.

Methods from Deref<Target = Packet>§

Source

pub fn ipv4_tcp_header(&self) -> Option<TcpHeader>

If the packet is an IPv4 TCP packet, returns a copy of the TCP header in a format defined by the Rust TCP stack. Otherwise, returns None.

Panics

This function panics if the packet was created with packet_new_tcp() in the legacy C API.

Source

pub fn payload(&self) -> Vec<Bytes>

Returns the packet’s payload that was provided at packet creation time. This function allocates a new Vec, but is zero-copy with respect to the payload Bytes.

This function may return a non-empty vector of zero-length Bytes object(s) if zero-length Bytes object(s) were provided at creation time. Thus, it may be helpful to check if the packet has useful payload using payload_len() before calling this function.

Source

pub fn len(&self) -> usize

Returns the total simulated length of the packet, which is the sum of the emulated IP and transport header lengths and the payload length.

Source

pub fn payload_len(&self) -> usize

Returns the total number of payload bytes stored in the packet, which excludes IP and transport header lengths.

Source

pub fn add_status(&self, status: PacketStatus)

Appends the provided packet status to the list of the packet’s status checkpoints.

This function has no effect unless log::Level::Trace is enabled.

Source

pub fn src_ipv4_address(&self) -> SocketAddrV4

Returns the packet’s IPv4 source address and source port.

Panics

This function panics if the source address is not an IPv4 address.

Source

pub fn dst_ipv4_address(&self) -> SocketAddrV4

Returns the packet’s IPv4 destination address and destination port.

Panics

This function panics if the destination address is not an IPv4 address.

Source

pub fn priority(&self) -> FifoPacketPriority

Returns the priority set at packet creation time.

Source

pub fn iana_protocol(&self) -> IanaProtocol

Returns the packet’s iana-assigned protocol type.

Trait Implementations§

Source§

impl Clone for PacketRc

Source§

fn clone(&self) -> PacketRc

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PacketRc

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for PacketRc

Source§

type Target = Packet

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl From<Packet> for PacketRc

Source§

fn from(packet: Packet) -> Self

Converts to this type from the input type.
Source§

impl From<PacketEventData> for PacketRc

Source§

fn from(data: PacketEventData) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for PacketRc

Source§

fn eq(&self, other: &Self) -> bool

Compares the pointer rather than the value of the inner Arc<Packet> object.

1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for PacketRc

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> NoTypeInference for T

Source§

type This = T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Host for T
where T: Debug + Send,

Source§

impl<T> Host for T
where T: Debug + Send + 'static,

Source§

impl<T> Host for T
where T: Host + Host,