Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track skb by address #194

Merged
merged 2 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion bpf/kprobe_pwru.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#define ETH_P_IP 0x800
#define ETH_P_IPV6 0x86dd

const static bool TRUE = true;

union addr {
u32 v4addr;
struct {
Expand Down Expand Up @@ -70,6 +72,14 @@ struct {
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
} events SEC(".maps");

#define MAX_TRACK_SIZE 1024
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, __u64);
__type(value, bool);
__uint(max_entries, MAX_TRACK_SIZE);
} skb_addresses SEC(".maps");

struct config {
u32 netns;
u32 mark;
Expand All @@ -86,6 +96,7 @@ struct config {
u8 output_skb;
u8 output_stack;
u8 is_set;
u8 track_skb;
} __attribute__((packed));

static volatile const struct config CFG;
Expand Down Expand Up @@ -336,19 +347,30 @@ set_output(struct pt_regs *ctx, struct sk_buff *skb, struct event_t *event) {

static __noinline int
handle_everything(struct sk_buff *skb, struct pt_regs *ctx, bool has_get_func_ip) {
bool tracked = false;
struct event_t event = {};
event.skb_addr = (u64) skb;

if (cfg->is_set) {
if (cfg->track_skb && bpf_map_lookup_elem(&skb_addresses, &event.skb_addr)) {
tracked = true;
goto cont;
}

if (!filter(skb)) {
return 0;
}

cont:
set_output(ctx, skb, &event);
}

if (cfg->track_skb && !tracked) {
bpf_map_update_elem(&skb_addresses, &event.skb_addr, &TRUE, BPF_ANY);
}

event.pid = bpf_get_current_pid_tgid();
event.addr = has_get_func_ip ? bpf_get_func_ip(ctx) : PT_REGS_IP(ctx);
event.skb_addr = (u64) skb;
event.ts = bpf_ktime_get_ns();
event.cpu_id = bpf_get_smp_processor_id();
event.param_second = PT_REGS_PARM2(ctx);
Expand Down
7 changes: 6 additions & 1 deletion internal/pwru/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ type FilterCfg struct {
OutputSkb uint8
OutputStack uint8

IsSet byte
IsSet byte
TrackSkb byte
}

func GetConfig(flags *Flags) FilterCfg {
Expand Down Expand Up @@ -126,5 +127,9 @@ func GetConfig(flags *Flags) FilterCfg {
}
}

if flags.FilterTrackSkb {
cfg.TrackSkb = 1
}

return cfg
}
20 changes: 11 additions & 9 deletions internal/pwru/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ type Flags struct {

KernelBTF string

FilterNetns uint32
FilterMark uint32
FilterFunc string
FilterProto string
FilterSrcIP string
FilterDstIP string
FilterSrcPort uint16
FilterDstPort uint16
FilterPort uint16
FilterNetns uint32
FilterMark uint32
FilterFunc string
FilterProto string
FilterSrcIP string
FilterDstIP string
FilterSrcPort uint16
FilterDstPort uint16
FilterPort uint16
FilterTrackSkb bool

OutputTS string
OutputMeta bool
Expand Down Expand Up @@ -72,6 +73,7 @@ func (f *Flags) SetFlags() {
flag.BoolVar(&f.OutputStack, "output-stack", false, "print stack")
flag.Uint64Var(&f.OutputLimitLines, "output-limit-lines", 0, "exit the program after the number of events has been received/printed")
flag.IntVar(&f.PerCPUBuffer, "per-cpu-buffer", os.Getpagesize(), "per CPU buffer in bytes")
flag.BoolVar(&f.FilterTrackSkb, "filter-track-skb", false, "trace a packet even if it does not match given filters (e.g., after NAT or tunnel decapsulation)")

flag.StringVar(&f.OutputFile, "output-file", "", "write traces to file")

Expand Down
Loading