-
Notifications
You must be signed in to change notification settings - Fork 11
/
tc.c
89 lines (69 loc) · 1.43 KB
/
tc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* Copyright 2023 Leon Hwang.
* SPDX-License-Identifier: Apache-2.0
*/
//go:build ignore
#include "bpf_all.h"
#include "lib_xdp_tc.h"
static __always_inline void
handle_tc(void *ctx, struct sk_buff *skb, enum probing_type type, int verdict)
{
void *head = (void *)(long)BPF_CORE_READ(skb, head);
__u16 l2_off = BPF_CORE_READ(skb, mac_header);
__u16 l3_off = BPF_CORE_READ(skb, network_header);
struct ethhdr *eth = head + l2_off;
struct iphdr *iph = head + l3_off;
if (BPF_CORE_READ(eth, h_proto) != bpf_htons(ETH_P_IP))
return;
if (BPF_CORE_READ(iph, protocol) != IPPROTO_ICMP)
return;
__handle_packet(ctx, iph, type, verdict);
}
SEC("fentry/tc")
int BPF_PROG(fentry_tc, struct sk_buff *skb)
{
handle_tc(ctx, skb, PROBE_TYPE_FENTRY, 0);
return 0;
}
SEC("fexit/tc")
int BPF_PROG(fexit_tc, struct sk_buff *skb, int verdict)
{
handle_tc(ctx, skb, PROBE_TYPE_FEXIT, verdict);
return 0;
}
SEC("tc")
int dummy(struct __sk_buff *skb)
{
return TC_ACT_OK;
}
__noinline int
subprog1(void)
{
bpf_printk("Here's subprog1.\n");
return 0;
}
__noinline int
subprog2(void)
{
bpf_printk("Here's subprog2.\n");
return 0;
}
__noinline int
subprog3(void)
{
bpf_printk("Here's subprog3.\n");
return 0;
}
SEC("tc")
int entry1(struct __sk_buff *skb)
{
subprog1();
subprog2();
return TC_ACT_OK;
}
SEC("tc")
int entry2(struct __sk_buff *skb)
{
subprog3();
return TC_ACT_OK;
}