Skip to content

Commit e1d6fea

Browse files
committed
move bpf c code to local for nodetrace
Signed-off-by: Menglong Dong <imagedong@tencent.com>
1 parent 9160d43 commit e1d6fea

3 files changed

Lines changed: 113 additions & 4 deletions

File tree

nodetrace/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
watch
22
mark
3-
progs/ntrace.c

nodetrace/Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ prog-watch = watch.c $(COMMON_SHARED)
77

88
include ../common.mk
99

10-
$(foreach i,$(bpf_progs),$(i).c):
11-
$(call cmd_download,$@,nodetrace/$@)
12-
1310
all: $(progs)
1411

1512
install: all

nodetrace/progs/ntrace.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include <vmlinux.h>
2+
#include <bpf_helpers.h>
3+
#include <bpf_endian.h>
4+
5+
#include <macro.h>
6+
#include "shared.h"
7+
#include <skb_utils.h>
8+
9+
#define MARK_TOS_VALUE 0xe0
10+
11+
bpf_args_t _bpf_args = {
12+
.quiet = true
13+
};
14+
15+
static inline void do_mark(struct __sk_buff *skb, struct iphdr *ip)
16+
{
17+
__u8 old_tos = ip->tos;
18+
ip->tos = MARK_TOS_VALUE;
19+
bpf_l3_csum_replace(skb, IP_CSUM_OFFSET, bpf_htons(old_tos),
20+
bpf_htons(MARK_TOS_VALUE),
21+
2);
22+
}
23+
24+
static inline bool is_marked(struct iphdr *ip)
25+
{
26+
return (ip->tos & MARK_TOS_VALUE) == MARK_TOS_VALUE;
27+
}
28+
29+
30+
/* mark the packet which to be traced. For now, the method of
31+
* marking is to set TOS in ip header to a special value.
32+
*
33+
* This eBPF with the type of TC is about to attach to the egress
34+
* of TC filter.
35+
*/
36+
SEC("tc")
37+
int ntrace_mark(struct __sk_buff *skb)
38+
{
39+
event_t event = {.location = LOCALTION_MARK};
40+
struct ethhdr *eth = SKB_DATA(skb);
41+
struct iphdr *ip;
42+
43+
eth = SKB_DATA(skb);
44+
if (SKB_CHECK_IP(skb))
45+
goto out;
46+
47+
if (eth->h_proto != bpf_htons(ETH_P_IP))
48+
goto out;
49+
50+
ip = SKB_HDR_IP(skb);
51+
if (ip->tos & MARK_TOS_VALUE) {
52+
bpf_printk("ctrace: conflict mark(tos) value found: %x\n",
53+
ip->tos);
54+
goto out;
55+
}
56+
57+
if (direct_parse_skb(skb, &event.pkt, &_bpf_args.pkt))
58+
goto out;
59+
60+
do_mark(skb, ip);
61+
if (!_bpf_args.quiet)
62+
EVENT_OUTPUT(skb, event);
63+
64+
return TC_ACT_OK;
65+
out:
66+
return TC_ACT_UNSPEC;
67+
}
68+
69+
70+
/*******************************************************************
71+
*
72+
* Following functions aim to trace the receive and send of marked
73+
* packet.
74+
*
75+
* ctrace_entry() is used to trace the packet entry to the node, and
76+
* ctrace_exit() is used to trace the packet leave.
77+
*
78+
*******************************************************************/
79+
80+
static inline void try_output_skb(struct __sk_buff *skb, __u8 location)
81+
{
82+
event_t event = { .location = location };
83+
struct ethhdr *eth = SKB_DATA(skb);
84+
struct iphdr *ip;
85+
86+
if (SKB_CHECK_IP(skb))
87+
return;
88+
89+
if (eth->h_proto != bpf_htons(ETH_P_IP))
90+
return;
91+
92+
ip = SKB_HDR_IP(skb);
93+
if (!is_marked(ip) || direct_parse_skb(skb, &event.pkt, NULL))
94+
return;
95+
96+
EVENT_OUTPUT(skb, event);
97+
}
98+
99+
SEC("tc")
100+
int ntrace_entry(struct __sk_buff *skb)
101+
{
102+
try_output_skb(skb, LOCALTION_INGRESS);
103+
return TC_ACT_UNSPEC;
104+
}
105+
106+
SEC("tc")
107+
int ntrace_exit(struct __sk_buff *skb)
108+
{
109+
try_output_skb(skb, LOCALTION_EGRESS);
110+
return TC_ACT_UNSPEC;
111+
}
112+
113+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)