Skip to content

Commit

Permalink
introduce tcp and udp aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
drunkirishcoder committed Jan 27, 2021
1 parent 911bbce commit cc63a34
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 58 deletions.
30 changes: 15 additions & 15 deletions bench/packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use capsule::packets::ip::v4::Ipv4;
use capsule::packets::ip::v6::{Ipv6, SegmentRouting};
use capsule::packets::{Ethernet, Packet, Udp};
use capsule::packets::{Ethernet, Packet, Udp4};
use capsule::testils::criterion::BencherExt;
use capsule::testils::proptest::*;
use capsule::testils::{PacketExt, Rvg};
Expand All @@ -30,12 +30,12 @@ use std::net::Ipv6Addr;

const BATCH_SIZE: usize = 500;

fn single_parse_udp(ipv4: Ipv4) -> Udp<Ipv4> {
ipv4.parse::<Udp<Ipv4>>().unwrap()
fn single_parse_udp(ipv4: Ipv4) -> Udp4 {
ipv4.parse::<Udp4>().unwrap()
}

fn single_peek_udp(ipv4: Ipv4) -> Ipv4 {
ipv4.peek::<Udp<Ipv4>>().unwrap();
ipv4.peek::<Udp4>().unwrap();
ipv4
}

Expand All @@ -62,16 +62,16 @@ fn single_peek_vs_parse(c: &mut Criterion) {
group.finish()
}

fn multi_parse_udp(mbuf: Mbuf) -> Udp<Ipv4> {
fn multi_parse_udp(mbuf: Mbuf) -> Udp4 {
let ethernet = mbuf.parse::<Ethernet>().unwrap();
let ipv4 = ethernet.parse::<Ipv4>().unwrap();
ipv4.parse::<Udp<Ipv4>>().unwrap()
ipv4.parse::<Udp4>().unwrap()
}

fn multi_peek_udp(mbuf: Mbuf) -> Mbuf {
let ethernet = mbuf.peek::<Ethernet>().unwrap();
let ipv4 = ethernet.peek::<Ipv4>().unwrap();
ipv4.peek::<Udp<Ipv4>>().unwrap();
ipv4.peek::<Udp4>().unwrap();
mbuf
}

Expand Down Expand Up @@ -147,7 +147,7 @@ fn multi_parse_upto_variable_srh(c: &mut Criterion) {
});
}

fn deparse_udp(udp: Udp<Ipv4>) -> Mbuf {
fn deparse_udp(udp: Udp4) -> Mbuf {
let d_ipv4 = udp.deparse();
let d_eth = d_ipv4.deparse();
d_eth.deparse()
Expand All @@ -161,7 +161,7 @@ fn deparse(c: &mut Criterion) {
});
}

fn reset_udp(udp: Udp<Ipv4>) -> Mbuf {
fn reset_udp(udp: Udp4) -> Mbuf {
udp.reset()
}

Expand All @@ -173,10 +173,10 @@ fn reset(c: &mut Criterion) {
});
}

fn multi_push_udp(mbuf: Mbuf) -> Udp<Ipv4> {
fn multi_push_udp(mbuf: Mbuf) -> Udp4 {
let ethernet = mbuf.push::<Ethernet>().unwrap();
let ipv4 = ethernet.push::<Ipv4>().unwrap();
ipv4.push::<Udp<Ipv4>>().unwrap()
ipv4.push::<Udp4>().unwrap()
}

#[capsule::bench(mempool_capacity = 511)]
Expand All @@ -187,8 +187,8 @@ fn multi_push(c: &mut Criterion) {
});
}

fn single_push_udp(ipv4: Ipv4) -> Udp<Ipv4> {
ipv4.push::<Udp<Ipv4>>().unwrap()
fn single_push_udp(ipv4: Ipv4) -> Udp4 {
ipv4.push::<Udp4>().unwrap()
}

#[capsule::bench(mempool_capacity = 511)]
Expand All @@ -202,7 +202,7 @@ fn single_push(c: &mut Criterion) {
});
}

fn single_remove_udp(udp: Udp<Ipv4>) -> Ipv4 {
fn single_remove_udp(udp: Udp4) -> Ipv4 {
udp.remove().unwrap()
}

Expand All @@ -214,7 +214,7 @@ fn single_remove(c: &mut Criterion) {
});
}

fn multi_remove_udp(udp: Udp<Ipv4>) -> Mbuf {
fn multi_remove_udp(udp: Udp4) -> Mbuf {
let ipv4 = udp.remove().unwrap();
let ethernet = ipv4.remove().unwrap();
ethernet.remove().unwrap()
Expand Down
4 changes: 2 additions & 2 deletions core/src/packets/ip/v6/srh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ mod tests {
use super::*;
use crate::packets::ip::v6::Ipv6;
use crate::packets::ip::ProtocolNumbers;
use crate::packets::{Ethernet, Tcp};
use crate::packets::{Ethernet, Tcp, Tcp6};
use crate::testils::byte_arrays::{IPV6_TCP_PACKET, SR_TCP_PACKET};
use crate::Mbuf;

Expand Down Expand Up @@ -681,7 +681,7 @@ mod tests {
assert_eq!(ProtocolNumbers::Tcp, ipv6.next_header());

// make sure rest of the packet still valid
let tcp = ipv6.parse::<Tcp<Ipv6>>().unwrap();
let tcp = ipv6.parse::<Tcp6>().unwrap();
assert_eq!(3464, tcp.src_port());
}
}
13 changes: 6 additions & 7 deletions core/src/packets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub struct Internal(());
/// let ethernet = packet.push::<Ethernet>()?;
/// let ipv4 = ethernet.push::<Ipv4>()?;
///
/// let mut tcp = ipv4.push::<Tcp<Ipv4>>()?;
/// let mut tcp = ipv4.push::<Tcp4>()?;
/// tcp.set_dst_ip(remote_ip);
/// tcp.set_dst_port(22);
/// tcp.reconcile_all();
Expand Down Expand Up @@ -338,7 +338,6 @@ mod tests {
use super::*;
use crate::net::MacAddr;
use crate::packets::ip::v4::Ipv4;
use crate::packets::Udp;
use crate::testils::byte_arrays::IPV4_UDP_PACKET;

#[capsule::test]
Expand All @@ -348,7 +347,7 @@ mod tests {

let ethernet = packet.parse::<Ethernet>().unwrap();
let ipv4 = ethernet.parse::<Ipv4>().unwrap();
let udp = ipv4.parse::<Udp<Ipv4>>().unwrap();
let udp = ipv4.parse::<Udp4>().unwrap();
let reset = udp.reset();

assert_eq!(len, reset.data_len());
Expand All @@ -362,7 +361,7 @@ mod tests {
assert_eq!(MacAddr::new(0, 0, 0, 0, 0, 2), ethernet.src());
let v4 = ethernet.peek::<Ipv4>().unwrap();
assert_eq!(255, v4.ttl());
let udp = v4.peek::<Udp<Ipv4>>().unwrap();
let udp = v4.peek::<Udp4>().unwrap();
assert_eq!(39376, udp.src_port());
}

Expand All @@ -371,10 +370,10 @@ mod tests {
let packet = Mbuf::from_bytes(&IPV4_UDP_PACKET).unwrap();
let ethernet = packet.parse::<Ethernet>().unwrap();
let v4 = ethernet.parse::<Ipv4>().unwrap();
let udp = v4.parse::<Udp<Ipv4>>().unwrap();
let udp = v4.parse::<Udp4>().unwrap();
let mut v4_2 = udp.deparse();
v4_2.set_ttl(25);
let udp_2 = v4_2.parse::<Udp<Ipv4>>().unwrap();
let udp_2 = v4_2.parse::<Udp4>().unwrap();
let v4_4 = udp_2.envelope();
assert_eq!(v4_4.ttl(), 25);
}
Expand All @@ -385,7 +384,7 @@ mod tests {
let ethernet = packet.parse::<Ethernet>().unwrap();
let v4 = ethernet.parse::<Ipv4>().unwrap();

let mut udp = v4.parse::<Udp<Ipv4>>().unwrap();
let mut udp = v4.parse::<Udp4>().unwrap();
assert!(udp.payload_len() > 0);

let _ = udp.remove_payload();
Expand Down
20 changes: 14 additions & 6 deletions core/src/packets/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

use crate::packets::ip::v4::Ipv4;
use crate::packets::ip::v6::Ipv6;
use crate::packets::ip::{Flow, IpPacket, ProtocolNumbers};
use crate::packets::types::{u16be, u32be};
use crate::packets::{checksum, Internal, Packet, ParseError};
Expand Down Expand Up @@ -598,6 +600,12 @@ impl<E: IpPacket> Packet for Tcp<E> {
}
}

/// A type alias for an IPv4 TCP packet.
pub type Tcp4 = Tcp<Ipv4>;

/// A type alias for an IPv6 TCP packet.
pub type Tcp6 = Tcp<Ipv6>;

/// TCP header.
///
/// The header only include the fixed portion of the TCP header. Variable
Expand Down Expand Up @@ -652,7 +660,7 @@ mod tests {
let packet = Mbuf::from_bytes(&IPV4_TCP_PACKET).unwrap();
let ethernet = packet.parse::<Ethernet>().unwrap();
let ipv4 = ethernet.parse::<Ipv4>().unwrap();
let tcp = ipv4.parse::<Tcp<Ipv4>>().unwrap();
let tcp = ipv4.parse::<Tcp4>().unwrap();

assert_eq!(36869, tcp.src_port());
assert_eq!(23, tcp.dst_port());
Expand All @@ -679,15 +687,15 @@ mod tests {
let ethernet = packet.parse::<Ethernet>().unwrap();
let ipv4 = ethernet.parse::<Ipv4>().unwrap();

assert!(ipv4.parse::<Tcp<Ipv4>>().is_err());
assert!(ipv4.parse::<Tcp4>().is_err());
}

#[capsule::test]
fn tcp_flow_v4() {
let packet = Mbuf::from_bytes(&IPV4_TCP_PACKET).unwrap();
let ethernet = packet.parse::<Ethernet>().unwrap();
let ipv4 = ethernet.parse::<Ipv4>().unwrap();
let tcp = ipv4.parse::<Tcp<Ipv4>>().unwrap();
let tcp = ipv4.parse::<Tcp4>().unwrap();
let flow = tcp.flow();

assert_eq!("139.133.217.110", flow.src_ip().to_string());
Expand Down Expand Up @@ -718,7 +726,7 @@ mod tests {
let packet = Mbuf::from_bytes(&IPV4_TCP_PACKET).unwrap();
let ethernet = packet.parse::<Ethernet>().unwrap();
let ipv4 = ethernet.parse::<Ipv4>().unwrap();
let mut tcp = ipv4.parse::<Tcp<Ipv4>>().unwrap();
let mut tcp = ipv4.parse::<Tcp4>().unwrap();

let old_checksum = tcp.checksum();
let new_ip = Ipv4Addr::new(10, 0, 0, 0);
Expand All @@ -741,7 +749,7 @@ mod tests {
let packet = Mbuf::from_bytes(&IPV4_TCP_PACKET).unwrap();
let ethernet = packet.parse::<Ethernet>().unwrap();
let ipv4 = ethernet.parse::<Ipv4>().unwrap();
let mut tcp = ipv4.parse::<Tcp<Ipv4>>().unwrap();
let mut tcp = ipv4.parse::<Tcp4>().unwrap();

let expected = tcp.checksum();
// no payload change but force a checksum recompute anyway
Expand All @@ -754,7 +762,7 @@ mod tests {
let packet = Mbuf::new().unwrap();
let ethernet = packet.push::<Ethernet>().unwrap();
let ipv4 = ethernet.push::<Ipv4>().unwrap();
let tcp = ipv4.push::<Tcp<Ipv4>>().unwrap();
let tcp = ipv4.push::<Tcp4>().unwrap();

assert_eq!(TcpHeader::size_of(), tcp.len());
assert_eq!(5, tcp.data_offset());
Expand Down
20 changes: 14 additions & 6 deletions core/src/packets/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

use crate::packets::ip::v4::Ipv4;
use crate::packets::ip::v6::Ipv6;
use crate::packets::ip::{Flow, IpPacket, ProtocolNumbers};
use crate::packets::types::u16be;
use crate::packets::{checksum, Internal, Packet, ParseError};
Expand Down Expand Up @@ -331,6 +333,12 @@ impl<E: IpPacket> Packet for Udp<E> {
}
}

/// A type alias for an IPv4 UDP packet.
pub type Udp4 = Udp<Ipv4>;

/// A type alias for an IPv6 UDP packet.
pub type Udp6 = Udp<Ipv6>;

/// UDP header.
#[derive(Clone, Copy, Debug, Default, SizeOf)]
#[repr(C)]
Expand Down Expand Up @@ -360,7 +368,7 @@ mod tests {
let packet = Mbuf::from_bytes(&IPV4_UDP_PACKET).unwrap();
let ethernet = packet.parse::<Ethernet>().unwrap();
let ipv4 = ethernet.parse::<Ipv4>().unwrap();
let udp = ipv4.parse::<Udp<Ipv4>>().unwrap();
let udp = ipv4.parse::<Udp4>().unwrap();

assert_eq!(39376, udp.src_port());
assert_eq!(1087, udp.dst_port());
Expand All @@ -374,15 +382,15 @@ mod tests {
let ethernet = packet.parse::<Ethernet>().unwrap();
let ipv4 = ethernet.parse::<Ipv4>().unwrap();

assert!(ipv4.parse::<Udp<Ipv4>>().is_err());
assert!(ipv4.parse::<Udp4>().is_err());
}

#[capsule::test]
fn udp_flow_v4() {
let packet = Mbuf::from_bytes(&IPV4_UDP_PACKET).unwrap();
let ethernet = packet.parse::<Ethernet>().unwrap();
let ipv4 = ethernet.parse::<Ipv4>().unwrap();
let udp = ipv4.parse::<Udp<Ipv4>>().unwrap();
let udp = ipv4.parse::<Udp4>().unwrap();
let flow = udp.flow();

assert_eq!("139.133.217.110", flow.src_ip().to_string());
Expand All @@ -397,7 +405,7 @@ mod tests {
let packet = Mbuf::from_bytes(&IPV4_UDP_PACKET).unwrap();
let ethernet = packet.parse::<Ethernet>().unwrap();
let ipv4 = ethernet.parse::<Ipv4>().unwrap();
let mut udp = ipv4.parse::<Udp<Ipv4>>().unwrap();
let mut udp = ipv4.parse::<Udp4>().unwrap();

let old_checksum = udp.checksum();
let new_ip = Ipv4Addr::new(10, 0, 0, 0);
Expand All @@ -420,7 +428,7 @@ mod tests {
let packet = Mbuf::from_bytes(&IPV4_UDP_PACKET).unwrap();
let ethernet = packet.parse::<Ethernet>().unwrap();
let ipv4 = ethernet.parse::<Ipv4>().unwrap();
let mut udp = ipv4.parse::<Udp<Ipv4>>().unwrap();
let mut udp = ipv4.parse::<Udp4>().unwrap();

let expected = udp.checksum();
// no payload change but force a checksum recompute anyway
Expand All @@ -433,7 +441,7 @@ mod tests {
let packet = Mbuf::new().unwrap();
let ethernet = packet.push::<Ethernet>().unwrap();
let ipv4 = ethernet.push::<Ipv4>().unwrap();
let udp = ipv4.push::<Udp<Ipv4>>().unwrap();
let udp = ipv4.push::<Udp4>().unwrap();

assert_eq!(UdpHeader::size_of(), udp.len());

Expand Down
18 changes: 9 additions & 9 deletions core/src/testils/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use crate::packets::ip::v4::Ipv4;
use crate::packets::ip::v6::{Ipv6, SegmentRouting};
use crate::packets::{Ethernet, Packet, Tcp, Udp};
use crate::packets::{Ethernet, Packet, Tcp, Tcp4, Tcp6, Udp4, Udp6};

/// [`Packet`] extension trait.
///
Expand All @@ -39,13 +39,13 @@ pub trait PacketExt: Packet + Sized {
}

/// Converts the packet into a TCP packet inside IPv4.
fn into_v4_tcp(self) -> Tcp<Ipv4> {
self.into_v4().parse::<Tcp<Ipv4>>().unwrap()
fn into_v4_tcp(self) -> Tcp4 {
self.into_v4().parse::<Tcp4>().unwrap()
}

/// Converts the packet into a UDP packet inside IPv4.
fn into_v4_udp(self) -> Udp<Ipv4> {
self.into_v4().parse::<Udp<Ipv4>>().unwrap()
fn into_v4_udp(self) -> Udp4 {
self.into_v4().parse::<Udp4>().unwrap()
}

/// Converts the packet into an IPv6 packet.
Expand All @@ -54,13 +54,13 @@ pub trait PacketExt: Packet + Sized {
}

/// Converts the packet into a TCP packet inside IPv6.
fn into_v6_tcp(self) -> Tcp<Ipv6> {
self.into_v6().parse::<Tcp<Ipv6>>().unwrap()
fn into_v6_tcp(self) -> Tcp6 {
self.into_v6().parse::<Tcp6>().unwrap()
}

/// Converts the packet into a UDP packet inside IPv6.
fn into_v6_udp(self) -> Udp<Ipv6> {
self.into_v6().parse::<Udp<Ipv6>>().unwrap()
fn into_v6_udp(self) -> Udp6 {
self.into_v6().parse::<Udp6>().unwrap()
}

/// Converts the packet into an IPv6 packet with a SRH extension.
Expand Down
Loading

0 comments on commit cc63a34

Please # to comment.