Skip to content

Commit

Permalink
Wrap printk with MACRO and disable them
Browse files Browse the repository at this point in the history
Wrap printk traces with a MACRO PRINTK to disable/enable in one shot,
and disable them by default.
  • Loading branch information
msune committed Aug 24, 2024
1 parent f3d957a commit 447ac27
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/common.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#ifndef FUNNEL_COMMON_H
#define FUNNEL_COMMON_H

#if 0
#define PRINTK(...) bpf_printk( __VA_ARGS__ )
#else
#define PRINTK(...)
#endif

#define SKB_GET_ETH( SKB ) (struct ethhdr*)(unsigned long long)skb->data

#define CHECK_SKB_PTR( SKB, PTR ) \
if ( (void*)(PTR) > ((void*)(unsigned long long) SKB -> data_end )) { \
bpf_printk("PTR: %p on pkt with length: %d out of bounds!\n", \
PRINTK("PTR: %p on pkt with length: %d out of bounds!\n", \
PTR, SKB ->len);\
return TC_ACT_OK; \
} do{}while(0)
Expand Down
11 changes: 6 additions & 5 deletions src/funnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ static __always_inline int ip4_funnel_udp_thru_tcp(struct __sk_buff* skb,

//Store current UDP offset & content of udp header
__u32 l3_off = (__u8*)ip - (__u8*)SKB_GET_ETH(skb);
__u32 l4_off = (__u8*)udp - (__u8*)SKB_GET_ETH(skb);
__u32 l4_off __attribute__((unused)) =
(__u8*)udp - (__u8*)SKB_GET_ETH(skb);

bpf_printk("UDP packet of length: %u, offset: %u!\n", skb->len, l4_off);
PRINTK("UDP packet of length: %u, offset: %u!\n", skb->len, l4_off);

//Change IP PROTO
union ttl_proto old_ttl = *(union ttl_proto*)&ip->ttl;
Expand All @@ -42,13 +43,13 @@ static __always_inline int ip4_funnel_udp_thru_tcp(struct __sk_buff* skb,
0,
diff, 0);
if(rc < 0){
bpf_printk("ERROR l3_csum_replace: %d", rc);
PRINTK("ERROR l3_csum_replace: %d", rc);
return TC_ACT_SHOT;
}
rc = bpf_skb_adjust_room(skb, sizeof(struct tcphdr), BPF_ADJ_ROOM_NET,
0);
if(rc < 0){
bpf_printk("ERROR adjust room: %d", rc);
PRINTK("ERROR adjust room: %d", rc);
return TC_ACT_SHOT;
}

Expand Down Expand Up @@ -135,7 +136,7 @@ int tc_ingress(struct __sk_buff *skb){
return proc_ip4(skb, ip);
}else if(eth->h_proto == bpf_htons(ETH_P_IPV6)){
//XXX
bpf_printk("IPv6 packet with length: %d NOT SUPPORTED!\n", skb->len);
PRINTK("IPv6 packet with length: %d NOT SUPPORTED!\n", skb->len);
}

return TC_ACT_UNSPEC;
Expand Down
10 changes: 5 additions & 5 deletions src/unfunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static __always_inline int ip4_unfunnel_udp_thru_tcp(struct __sk_buff* skb,
__s64 diff = bpf_csum_diff((__be32*)&old_ttl, 4, (__be32*)&ip->ttl, 4,
0);
if(diff < 0){
bpf_printk("ERROR csum_diff: %d", diff);
PRINTK("ERROR csum_diff: %d", diff);
return TC_ACT_SHOT;
}

Expand All @@ -30,7 +30,7 @@ static __always_inline int ip4_unfunnel_udp_thru_tcp(struct __sk_buff* skb,
ip->tot_len = bpf_htons(bpf_ntohs(ip->tot_len)-sizeof(struct tcphdr));
diff = bpf_csum_diff((__be32*)&old_totlen, 4, (__be32*)ip, 4, diff);
if(diff < 0){
bpf_printk("ERROR csum_diff: %d", diff);
PRINTK("ERROR csum_diff: %d", diff);
return TC_ACT_SHOT;
}

Expand All @@ -40,12 +40,12 @@ static __always_inline int ip4_unfunnel_udp_thru_tcp(struct __sk_buff* skb,
0,
diff, 0);
if(rc < 0){
bpf_printk("ERROR l3_csum_replace: %d", rc);
PRINTK("ERROR l3_csum_replace: %d", rc);
return TC_ACT_SHOT;
}
rc = bpf_skb_adjust_room(skb, -(__s32)sizeof(struct tcphdr), BPF_ADJ_ROOM_NET, 0);
if(rc < 0){
bpf_printk("ERROR adjust room: %d", rc);
PRINTK("ERROR adjust room: %d", rc);
return TC_ACT_SHOT;
}

Expand Down Expand Up @@ -82,7 +82,7 @@ int tc_ingress(struct __sk_buff *skb){
return proc_ip4(skb, ip);
}else if(eth->h_proto == bpf_htons(ETH_P_IPV6)){
//XXX
bpf_printk("IPv6 packet with length: %d NOT SUPPORTED!\n", skb->len);
PRINTK("IPv6 packet with length: %d NOT SUPPORTED!\n", skb->len);
}

return TC_ACT_UNSPEC;
Expand Down

0 comments on commit 447ac27

Please sign in to comment.