Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: TOS improvements #1774

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions neqo-common/src/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ impl Datagram {
pub fn ttl(&self) -> Option<u8> {
self.ttl
}

pub fn set_tos(&mut self, tos: IpTos) {
self.tos = tos;
}
}

impl Deref for Datagram {
Expand Down Expand Up @@ -90,8 +94,7 @@ use test_fixture::datagram;
fn fmt_datagram() {
let d = datagram([0; 1].to_vec());
assert_eq!(
format!("{d:?}"),
&format!("{d:?}"),
"Datagram IpTos(Cs0, NotEct) TTL Some(128) [fe80::1]:443->[fe80::1]:443: [1]: 00"
.to_string()
);
}
48 changes: 36 additions & 12 deletions neqo-common/src/tos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl From<IpTosEcn> for u8 {

impl From<u8> for IpTosEcn {
fn from(v: u8) -> Self {
match v & 0b11 {
match v & 0b0000_0011 {
0b00 => IpTosEcn::NotEct,
0b01 => IpTosEcn::Ect1,
0b10 => IpTosEcn::Ect0,
Expand All @@ -47,8 +47,8 @@ impl From<u8> for IpTosEcn {
}

impl From<IpTos> for IpTosEcn {
fn from(value: IpTos) -> Self {
IpTosEcn::from(value.0 & 0x3)
fn from(v: IpTos) -> Self {
IpTosEcn::from(u8::from(v))
}
}

Expand Down Expand Up @@ -166,14 +166,13 @@ impl From<u8> for IpTosDscp {
}

impl From<IpTos> for IpTosDscp {
fn from(value: IpTos) -> Self {
IpTosDscp::from(value.0 & 0xfc)
fn from(v: IpTos) -> Self {
IpTosDscp::from(u8::from(v))
}
}

/// The type-of-service field in an IP packet.
#[allow(clippy::module_name_repetitions)]
#[derive(Copy, Clone, PartialEq, Eq)]
#[derive(Copy, Clone, PartialEq, Eq, Default)]
pub struct IpTos(u8);

impl From<IpTosEcn> for IpTos {
Expand Down Expand Up @@ -215,15 +214,19 @@ impl From<u8> for IpTos {
impl Debug for IpTos {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("IpTos")
.field(&IpTosDscp::from(self.0 & 0xfc))
.field(&IpTosEcn::from(self.0 & 0x3))
.field(&IpTosDscp::from(*self))
.field(&IpTosEcn::from(*self))
.finish()
}
}

impl Default for IpTos {
fn default() -> Self {
(IpTosDscp::default(), IpTosEcn::default()).into()
impl IpTos {
pub fn set_ecn(&mut self, ecn: IpTosEcn) {
self.0 = u8::from(IpTosDscp::from(*self)) | u8::from(ecn);
}

pub fn set_dscp(&mut self, dscp: IpTosDscp) {
self.0 = u8::from(IpTosEcn::from(*self)) | u8::from(dscp);
}
}

Expand Down Expand Up @@ -322,4 +325,25 @@ mod tests {
assert_eq!(tos, u8::from(iptos));
assert_eq!(IpTos::from(tos), iptos);
}

#[test]
fn iptos_to_iptosdscp() {
let tos = IpTos::from((IpTosDscp::Af41, IpTosEcn::NotEct));
let dscp = IpTosDscp::from(tos);
assert_eq!(dscp, IpTosDscp::Af41);
}

#[test]
fn tos_modify_ecn() {
let mut iptos: IpTos = (IpTosDscp::Af41, IpTosEcn::NotEct).into();
iptos.set_ecn(IpTosEcn::Ce);
assert_eq!(u8::from(iptos), 0b1000_1011);
}

#[test]
fn tos_modify_dscp() {
let mut iptos: IpTos = (IpTosDscp::Af41, IpTosEcn::Ect1).into();
iptos.set_dscp(IpTosDscp::Le);
assert_eq!(u8::from(iptos), 0b0000_0101);
}
}
13 changes: 11 additions & 2 deletions neqo-transport/src/connection/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use std::fmt::Write;

use neqo_common::{qdebug, Decoder};
use neqo_common::{qdebug, Decoder, IpTos};

use crate::{
connection::Connection,
Expand All @@ -26,6 +26,7 @@ pub fn dump_packet(
pt: PacketType,
pn: PacketNumber,
payload: &[u8],
tos: IpTos,
) {
if log::STATIC_MAX_LEVEL == log::LevelFilter::Off || !log::log_enabled!(log::Level::Debug) {
return;
Expand All @@ -43,5 +44,13 @@ pub fn dump_packet(
write!(&mut s, "\n {} {}", dir, &x).unwrap();
}
}
qdebug!([conn], "pn={} type={:?} {}{}", pn, pt, path.borrow(), s);
qdebug!(
[conn],
"pn={} type={:?} {} {:?}{}",
pn,
pt,
path.borrow(),
tos,
s
);
}
4 changes: 3 additions & 1 deletion neqo-transport/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::{

use neqo_common::{
event::Provider as EventProvider, hex, hex_snip_middle, hrtime, qdebug, qerror, qinfo,
qlog::NeqoQlog, qtrace, qwarn, Datagram, Decoder, Encoder, Role,
qlog::NeqoQlog, qtrace, qwarn, Datagram, Decoder, Encoder, IpTos, Role,
};
use neqo_crypto::{
agent::CertificateInfo, Agent, AntiReplay, AuthenticationStatus, Cipher, Client, Group,
Expand Down Expand Up @@ -1492,6 +1492,7 @@ impl Connection {
payload.packet_type(),
payload.pn(),
&payload[..],
d.tos(),
);

qlog::packet_received(&mut self.qlog, &packet, &payload);
Expand Down Expand Up @@ -2255,6 +2256,7 @@ impl Connection {
pt,
pn,
&builder.as_ref()[payload_start..],
IpTos::default(), // TODO: set from path
);
qlog::packet_sent(
&mut self.qlog,
Expand Down
Loading