Skip to content

Commit

Permalink
events: add ConnectionCloseFrameReceived event (#2436)
Browse files Browse the repository at this point in the history
* create new ConnectionCloseFrameReceived event

* fix typo

* add helper method and debug to print reason as UTF-8

* put behind alloc feature

* PR feedback

* add cfg_attr

* trying again

* trying again
  • Loading branch information
WesleyRosenblum authored Jan 6, 2025
1 parent ac52a48 commit fa2e663
Show file tree
Hide file tree
Showing 16 changed files with 1,047 additions and 658 deletions.
45 changes: 45 additions & 0 deletions quic/s2n-quic-core/events/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,51 @@ impl<'a> IntoEvent<builder::Frame> for &crate::frame::DcStatelessResetTokens<'a>
}
}

#[derive(Clone)]
struct ConnectionCloseFrame<'a> {
error_code: u64,
frame_type: Option<u64>,
reason: Option<&'a [u8]>,
}

#[cfg(feature = "alloc")]
impl<'a> ConnectionCloseFrame<'a> {
/// Converts the reason to a UTF-8 `str`, including invalid characters
pub fn reason_lossy_utf8(&self) -> Option<alloc::borrow::Cow<'a, str>> {
self.reason
.map(|reason| alloc::string::String::from_utf8_lossy(reason))
}
}

impl<'a> IntoEvent<builder::ConnectionCloseFrame<'a>> for &crate::frame::ConnectionClose<'a> {
#[inline]
fn into_event(self) -> builder::ConnectionCloseFrame<'a> {
builder::ConnectionCloseFrame {
error_code: self.error_code.as_u64(),
frame_type: self.frame_type.into_event(),
reason: self.reason.into_event(),
}
}
}

#[cfg(feature = "alloc")]
impl<'a> core::fmt::Debug for ConnectionCloseFrame<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.debug_struct("ConnectionCloseFrame")
.field("error_code", &self.error_code)
.field("frame_type", &self.frame_type)
.field("reason", &self.reason_lossy_utf8())
.finish()
}
}

#[cfg(not(feature = "alloc"))]
impl<'a> core::fmt::Debug for ConnectionCloseFrame<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{:?}", self)
}
}

enum StreamType {
Bidirectional,
Unidirectional,
Expand Down
12 changes: 12 additions & 0 deletions quic/s2n-quic-core/events/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ struct FrameReceived<'a> {
frame: Frame,
}

/// A `CONNECTION_CLOSE` frame was received
///
/// This event includes additional details from the frame, particularly the
/// reason (if provided) the peer closed the connection
#[event("transport:connection_close_frame_received")]
struct ConnectionCloseFrameReceived<'a> {
#[nominal_counter("packet")]
packet_header: PacketHeader,
path: Path<'a>,
frame: ConnectionCloseFrame<'a>,
}

#[event("recovery:packet_lost")]
//= https://tools.ietf.org/id/draft-marx-qlog-event-definitions-quic-h3-02#5.4.5
/// Packet was lost
Expand Down
205 changes: 205 additions & 0 deletions quic/s2n-quic-core/src/event/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,23 @@ pub mod api {
fmt.finish()
}
}
#[non_exhaustive]
#[derive(Clone)]
pub struct ConnectionCloseFrame<'a> {
pub error_code: u64,
pub frame_type: Option<u64>,
pub reason: Option<&'a [u8]>,
}
#[cfg(any(test, feature = "testing"))]
impl<'a> crate::event::snapshot::Fmt for ConnectionCloseFrame<'a> {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
let mut fmt = fmt.debug_struct("ConnectionCloseFrame");
fmt.field("error_code", &self.error_code);
fmt.field("frame_type", &self.frame_type);
fmt.field("reason", &self.reason);
fmt.finish()
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct MtuConfig {
Expand Down Expand Up @@ -1923,6 +1940,30 @@ pub mod api {
}
#[derive(Clone, Debug)]
#[non_exhaustive]
#[doc = " A `CONNECTION_CLOSE` frame was received"]
#[doc = ""]
#[doc = " This event includes additional details from the frame, particularly the"]
#[doc = " reason (if provided) the peer closed the connection"]
pub struct ConnectionCloseFrameReceived<'a> {
pub packet_header: PacketHeader,
pub path: Path<'a>,
pub frame: ConnectionCloseFrame<'a>,
}
#[cfg(any(test, feature = "testing"))]
impl<'a> crate::event::snapshot::Fmt for ConnectionCloseFrameReceived<'a> {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
let mut fmt = fmt.debug_struct("ConnectionCloseFrameReceived");
fmt.field("packet_header", &self.packet_header);
fmt.field("path", &self.path);
fmt.field("frame", &self.frame);
fmt.finish()
}
}
impl<'a> Event for ConnectionCloseFrameReceived<'a> {
const NAME: &'static str = "transport:connection_close_frame_received";
}
#[derive(Clone, Debug)]
#[non_exhaustive]
#[doc = " Packet was lost"]
pub struct PacketLost<'a> {
pub packet_header: PacketHeader,
Expand Down Expand Up @@ -3353,6 +3394,40 @@ pub mod api {
builder::Frame::DcStatelessResetTokens {}
}
}
#[cfg(feature = "alloc")]
impl<'a> ConnectionCloseFrame<'a> {
#[doc = " Converts the reason to a UTF-8 `str`, including invalid characters"]
pub fn reason_lossy_utf8(&self) -> Option<alloc::borrow::Cow<'a, str>> {
self.reason
.map(|reason| alloc::string::String::from_utf8_lossy(reason))
}
}
impl<'a> IntoEvent<builder::ConnectionCloseFrame<'a>> for &crate::frame::ConnectionClose<'a> {
#[inline]
fn into_event(self) -> builder::ConnectionCloseFrame<'a> {
builder::ConnectionCloseFrame {
error_code: self.error_code.as_u64(),
frame_type: self.frame_type.into_event(),
reason: self.reason.into_event(),
}
}
}
#[cfg(feature = "alloc")]
impl<'a> core::fmt::Debug for ConnectionCloseFrame<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.debug_struct("ConnectionCloseFrame")
.field("error_code", &self.error_code)
.field("frame_type", &self.frame_type)
.field("reason", &self.reason_lossy_utf8())
.finish()
}
}
#[cfg(not(feature = "alloc"))]
impl<'a> core::fmt::Debug for ConnectionCloseFrame<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{:?}", self)
}
}
impl IntoEvent<builder::StreamType> for &crate::stream::StreamType {
#[inline]
fn into_event(self) -> builder::StreamType {
Expand Down Expand Up @@ -3601,6 +3676,21 @@ pub mod tracing {
tracing :: event ! (target : "frame_received" , parent : id , tracing :: Level :: DEBUG , { packet_header = tracing :: field :: debug (packet_header) , path = tracing :: field :: debug (path) , frame = tracing :: field :: debug (frame) });
}
#[inline]
fn on_connection_close_frame_received(
&mut self,
context: &mut Self::ConnectionContext,
_meta: &api::ConnectionMeta,
event: &api::ConnectionCloseFrameReceived,
) {
let id = context.id();
let api::ConnectionCloseFrameReceived {
packet_header,
path,
frame,
} = event;
tracing :: event ! (target : "connection_close_frame_received" , parent : id , tracing :: Level :: DEBUG , { packet_header = tracing :: field :: debug (packet_header) , path = tracing :: field :: debug (path) , frame = tracing :: field :: debug (frame) });
}
#[inline]
fn on_packet_lost(
&mut self,
context: &mut Self::ConnectionContext,
Expand Down Expand Up @@ -4411,6 +4501,27 @@ pub mod builder {
}
}
#[derive(Clone, Debug)]
pub struct ConnectionCloseFrame<'a> {
pub error_code: u64,
pub frame_type: Option<u64>,
pub reason: Option<&'a [u8]>,
}
impl<'a> IntoEvent<api::ConnectionCloseFrame<'a>> for ConnectionCloseFrame<'a> {
#[inline]
fn into_event(self) -> api::ConnectionCloseFrame<'a> {
let ConnectionCloseFrame {
error_code,
frame_type,
reason,
} = self;
api::ConnectionCloseFrame {
error_code: error_code.into_event(),
frame_type: frame_type.into_event(),
reason: reason.into_event(),
}
}
}
#[derive(Clone, Debug)]
pub struct MtuConfig {
pub initial_mtu: u16,
pub base_mtu: u16,
Expand Down Expand Up @@ -5480,6 +5591,31 @@ pub mod builder {
}
}
#[derive(Clone, Debug)]
#[doc = " A `CONNECTION_CLOSE` frame was received"]
#[doc = ""]
#[doc = " This event includes additional details from the frame, particularly the"]
#[doc = " reason (if provided) the peer closed the connection"]
pub struct ConnectionCloseFrameReceived<'a> {
pub packet_header: PacketHeader,
pub path: Path<'a>,
pub frame: ConnectionCloseFrame<'a>,
}
impl<'a> IntoEvent<api::ConnectionCloseFrameReceived<'a>> for ConnectionCloseFrameReceived<'a> {
#[inline]
fn into_event(self) -> api::ConnectionCloseFrameReceived<'a> {
let ConnectionCloseFrameReceived {
packet_header,
path,
frame,
} = self;
api::ConnectionCloseFrameReceived {
packet_header: packet_header.into_event(),
path: path.into_event(),
frame: frame.into_event(),
}
}
}
#[derive(Clone, Debug)]
#[doc = " Packet was lost"]
pub struct PacketLost<'a> {
pub packet_header: PacketHeader,
Expand Down Expand Up @@ -6699,6 +6835,18 @@ mod traits {
let _ = meta;
let _ = event;
}
#[doc = "Called when the `ConnectionCloseFrameReceived` event is triggered"]
#[inline]
fn on_connection_close_frame_received(
&mut self,
context: &mut Self::ConnectionContext,
meta: &api::ConnectionMeta,
event: &api::ConnectionCloseFrameReceived,
) {
let _ = context;
let _ = meta;
let _ = event;
}
#[doc = "Called when the `PacketLost` event is triggered"]
#[inline]
fn on_packet_lost(
Expand Down Expand Up @@ -7432,6 +7580,16 @@ mod traits {
(self.1).on_frame_received(&mut context.1, meta, event);
}
#[inline]
fn on_connection_close_frame_received(
&mut self,
context: &mut Self::ConnectionContext,
meta: &api::ConnectionMeta,
event: &api::ConnectionCloseFrameReceived,
) {
(self.0).on_connection_close_frame_received(&mut context.0, meta, event);
(self.1).on_connection_close_frame_received(&mut context.1, meta, event);
}
#[inline]
fn on_packet_lost(
&mut self,
context: &mut Self::ConnectionContext,
Expand Down Expand Up @@ -8121,6 +8279,11 @@ mod traits {
fn on_frame_sent(&mut self, event: builder::FrameSent);
#[doc = "Publishes a `FrameReceived` event to the publisher's subscriber"]
fn on_frame_received(&mut self, event: builder::FrameReceived);
#[doc = "Publishes a `ConnectionCloseFrameReceived` event to the publisher's subscriber"]
fn on_connection_close_frame_received(
&mut self,
event: builder::ConnectionCloseFrameReceived,
);
#[doc = "Publishes a `PacketLost` event to the publisher's subscriber"]
fn on_packet_lost(&mut self, event: builder::PacketLost);
#[doc = "Publishes a `RecoveryMetrics` event to the publisher's subscriber"]
Expand Down Expand Up @@ -8310,6 +8473,18 @@ mod traits {
self.subscriber.on_event(&self.meta, &event);
}
#[inline]
fn on_connection_close_frame_received(
&mut self,
event: builder::ConnectionCloseFrameReceived,
) {
let event = event.into_event();
self.subscriber
.on_connection_close_frame_received(self.context, &self.meta, &event);
self.subscriber
.on_connection_event(self.context, &self.meta, &event);
self.subscriber.on_event(&self.meta, &event);
}
#[inline]
fn on_packet_lost(&mut self, event: builder::PacketLost) {
let event = event.into_event();
self.subscriber
Expand Down Expand Up @@ -8882,6 +9057,7 @@ pub mod testing {
pub path_created: u64,
pub frame_sent: u64,
pub frame_received: u64,
pub connection_close_frame_received: u64,
pub packet_lost: u64,
pub recovery_metrics: u64,
pub congestion: u64,
Expand Down Expand Up @@ -8971,6 +9147,7 @@ pub mod testing {
path_created: 0,
frame_sent: 0,
frame_received: 0,
connection_close_frame_received: 0,
packet_lost: 0,
recovery_metrics: 0,
congestion: 0,
Expand Down Expand Up @@ -9157,6 +9334,20 @@ pub mod testing {
self.output.push(out);
}
}
fn on_connection_close_frame_received(
&mut self,
_context: &mut Self::ConnectionContext,
meta: &api::ConnectionMeta,
event: &api::ConnectionCloseFrameReceived,
) {
self.connection_close_frame_received += 1;
if self.location.is_some() {
let meta = crate::event::snapshot::Fmt::to_snapshot(meta);
let event = crate::event::snapshot::Fmt::to_snapshot(event);
let out = format!("{meta:?} {event:?}");
self.output.push(out);
}
}
fn on_packet_lost(
&mut self,
_context: &mut Self::ConnectionContext,
Expand Down Expand Up @@ -9797,6 +9988,7 @@ pub mod testing {
pub path_created: u64,
pub frame_sent: u64,
pub frame_received: u64,
pub connection_close_frame_received: u64,
pub packet_lost: u64,
pub recovery_metrics: u64,
pub congestion: u64,
Expand Down Expand Up @@ -9876,6 +10068,7 @@ pub mod testing {
path_created: 0,
frame_sent: 0,
frame_received: 0,
connection_close_frame_received: 0,
packet_lost: 0,
recovery_metrics: 0,
congestion: 0,
Expand Down Expand Up @@ -10126,6 +10319,18 @@ pub mod testing {
self.output.push(out);
}
}
fn on_connection_close_frame_received(
&mut self,
event: builder::ConnectionCloseFrameReceived,
) {
self.connection_close_frame_received += 1;
let event = event.into_event();
if self.location.is_some() {
let event = crate::event::snapshot::Fmt::to_snapshot(&event);
let out = format!("{event:?}");
self.output.push(out);
}
}
fn on_packet_lost(&mut self, event: builder::PacketLost) {
self.packet_lost += 1;
let event = event.into_event();
Expand Down
Loading

0 comments on commit fa2e663

Please sign in to comment.