Skip to content

Commit

Permalink
Refactor | logging for src ip (#74)
Browse files Browse the repository at this point in the history
Fixes : #71 

Replace custom function with formatter from aaya itself.

---------

Signed-off-by: rushi47 <[email protected]>
Co-authored-by: Shane Utt <[email protected]>
  • Loading branch information
rushi47 and shaneutt authored Mar 8, 2023
1 parent 6c192ad commit 822eed6
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 49 deletions.
14 changes: 3 additions & 11 deletions dataplane/ebpf/src/egress/icmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use aya_log_ebpf::info;

use crate::{
bindings::{iphdr, icmphdr},
utils::{csum_fold_helper, ip_from_int, ptr_at, ETH_HDR_LEN, IP_HDR_LEN},
utils::{csum_fold_helper, ptr_at, ETH_HDR_LEN, IP_HDR_LEN},
BLIXT_CONNTRACK,
};

Expand Down Expand Up @@ -38,16 +38,8 @@ pub fn handle_icmp_egress(ctx: TcContext) -> Result<i32, i64> {

let new_src = unsafe { BLIXT_CONNTRACK.get(&dest_addr) }.ok_or(TC_ACT_PIPE)?;

let daddr_dot_dec = ip_from_int(unsafe { (*ip_hdr).daddr });
info!(
&ctx,
"Received a ICMP Unreachable packet destined for svc ip: {}.{}.{}.{}",
daddr_dot_dec[0],
daddr_dot_dec[1],
daddr_dot_dec[2],
daddr_dot_dec[3],
);

info!(&ctx, "Received a ICMP Unreachable packet destined for svc ip: {:ipv4} ", u32::from_be(unsafe { (*ip_hdr).daddr }));

// redirect icmp unreachable message back to client
unsafe {
(*ip_hdr).saddr = *new_src;
Expand Down
13 changes: 2 additions & 11 deletions dataplane/ebpf/src/ingress/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use aya_log_ebpf::info;

use crate::{
bindings::{iphdr, tcphdr},
utils::{csum_fold_helper, ip_from_int, ptr_at, ETH_HDR_LEN, IP_HDR_LEN},
utils::{csum_fold_helper, ptr_at, ETH_HDR_LEN, IP_HDR_LEN},
BACKENDS,
};
use common::BackendKey;
Expand All @@ -28,16 +28,7 @@ pub fn handle_tcp_ingress(ctx: TcContext) -> Result<i32, i64> {

let backend = unsafe { BACKENDS.get(&key) }.ok_or(TC_ACT_OK)?;

let daddr_dot_dec = ip_from_int(unsafe { (*ip_hdr).daddr });
info!(
&ctx,
"Received a TCP packet destined for svc ip: {}.{}.{}.{} at port: {}",
daddr_dot_dec[0],
daddr_dot_dec[1],
daddr_dot_dec[2],
daddr_dot_dec[3],
u16::from_be(unsafe { (*tcp_hdr).dest })
);
info!(&ctx, "Received a TCP packet destined for svc ip: {:ipv4} at Port: {} ", u32::from_be(unsafe { (*ip_hdr).daddr }), u16::from_be(unsafe { (*tcp_hdr).dest} ));

unsafe {
(*ip_hdr).daddr = backend.daddr.to_be();
Expand Down
15 changes: 3 additions & 12 deletions dataplane/ebpf/src/ingress/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use aya_log_ebpf::info;

use crate::{
bindings::{iphdr, udphdr},
utils::{csum_fold_helper, ip_from_int, ptr_at, ETH_HDR_LEN, IP_HDR_LEN},
utils::{csum_fold_helper, ptr_at, ETH_HDR_LEN, IP_HDR_LEN},
BACKENDS,
BLIXT_CONNTRACK,
};
Expand All @@ -30,17 +30,8 @@ pub fn handle_udp_ingress(ctx: TcContext) -> Result<i32, i64> {
};

let backend = unsafe { BACKENDS.get(&key) }.ok_or(TC_ACT_PIPE)?;

let daddr_dot_dec = ip_from_int(unsafe { (*ip_hdr).daddr });
info!(
&ctx,
"Received a UDP packet destined for svc ip: {}.{}.{}.{} at port: {}",
daddr_dot_dec[0],
daddr_dot_dec[1],
daddr_dot_dec[2],
daddr_dot_dec[3],
u16::from_be(unsafe { (*udp_hdr).dest })
);

info!(&ctx, "Received a UDP packet destined for svc ip: {:ipv4} at Port: {} ", u32::from_be(unsafe { (*ip_hdr).daddr }), u16::from_be(unsafe { (*udp_hdr).dest} ));

unsafe {
BLIXT_CONNTRACK.insert(&(*ip_hdr).saddr, &original_daddr, 0 as u64)?;
Expand Down
15 changes: 0 additions & 15 deletions dataplane/ebpf/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,6 @@ pub unsafe fn ptr_at<T>(ctx: &TcContext, offset: usize) -> Result<*mut T, i64> {
Ok((start + offset) as *mut T)
}

// Produces an IPv4 address as a [u8;4] which is easy to print out in
// dot-decimal notation using the info!() macro.
//
// TODO: use a type alias and implement print formatting for this?
#[inline(always)]
pub fn ip_from_int(ip: u32) -> [u8; 4] {
let mut addr: [u8; 4] = [0; 4];

addr[0] = ((ip >> 0) & 0xFF) as u8;
addr[1] = ((ip >> 8) & 0xFF) as u8;
addr[2] = ((ip >> 16) & 0xFF) as u8;
addr[3] = ((ip >> 24) & 0xFF) as u8;

addr
}

// Converts a checksum into u16
#[inline(always)]
Expand Down

0 comments on commit 822eed6

Please sign in to comment.