Skip to content

Commit 3d2c7a5

Browse files
authored
fix: optimize DropReason eBPF map lookups (#730)
# Description For all eBPF program in the DropReason plugin (except `inet_csk_accept` which has issue we need to investigate #715): - only make ebpf map calls when necessary - omit setting some packet fields to 0 right after `memset` is called on the entire struct **Details**: Previously, we did a map lookup regardless of whether the input `retVal` indicated a drop. Now, only for drops. We also skip a map delete when there wasn't a earlier kprobe that saved the corresponding PID. ## Checklist - [x] I have read the [contributing documentation](https://retina.sh/docs/contributing). - [x] I signed and signed-off the commits (`git commit -S -s ...`). See [this documentation](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification) on signing commits. - [x] I have correctly attributed the author(s) of the code. - [x] I have tested the changes locally. - [x] I have followed the project's style guidelines. - [x] I have updated the documentation, if necessary. - [x] I have added tests, if applicable. Signed-off-by: Igor Klemenski <[email protected]>
1 parent 19faa61 commit 3d2c7a5

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

pkg/plugin/dropreason/_cprog/drop_reason.c

+23-28
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,6 @@ int BPF_KPROBE(nf_hook_slow, struct sk_buff *skb, struct nf_hook_state *state)
249249

250250
struct packet p;
251251
__builtin_memset(&p, 0, sizeof(p));
252-
253-
p.in_filtermap = false;
254-
p.skb_len = 0;
255252
get_packet_from_skb(&p, skb);
256253

257254
__u64 pid_tgid = bpf_get_current_pid_tgid();
@@ -271,26 +268,25 @@ int BPF_KRETPROBE(nf_hook_slow_ret, int retVal)
271268
{
272269
__u64 pid_tgid = bpf_get_current_pid_tgid();
273270
__u32 pid = pid_tgid >> 32;
274-
struct packet *p = bpf_map_lookup_elem(&drop_pids, &pid);
275-
bpf_map_delete_elem(&drop_pids, &pid);
276271

277-
if (!p)
272+
if (retVal >= 0)
278273
{
274+
bpf_map_delete_elem(&drop_pids, &pid);
279275
return 0;
280276
}
281277

282-
if (retVal >= 0)
278+
struct packet *p = bpf_map_lookup_elem(&drop_pids, &pid);
279+
if (!p)
283280
{
284281
return 0;
285282
}
286283

284+
bpf_map_delete_elem(&drop_pids, &pid);
285+
287286
update_metrics_map(ctx, IPTABLE_RULE_DROP, 0, p);
288287
return 0;
289288
}
290289

291-
// static __always_inline int
292-
// exit_tcp_connect(struct pt_regs *ctx, int ret)
293-
294290
/*
295291
This function checks the return value of tcp_v4_connect and
296292
update the metrics map accordingly.
@@ -309,9 +305,6 @@ int BPF_KRETPROBE(tcp_v4_connect_ret, int retVal)
309305
struct packet p;
310306
__builtin_memset(&p, 0, sizeof(p));
311307

312-
p.in_filtermap = false;
313-
p.skb_len = 0;
314-
315308
update_metrics_map(ctx, TCP_CONNECT_BASIC, retVal, &p);
316309
return 0;
317310
}
@@ -387,9 +380,6 @@ int BPF_KPROBE(nf_nat_inet_fn, void *priv, struct sk_buff *skb, const struct nf_
387380

388381
struct packet p;
389382
__builtin_memset(&p, 0, sizeof(p));
390-
391-
p.in_filtermap = false;
392-
p.skb_len = 0;
393383
get_packet_from_skb(&p, skb);
394384

395385
__u64 pid_tgid = bpf_get_current_pid_tgid();
@@ -403,17 +393,21 @@ int BPF_KRETPROBE(nf_nat_inet_fn_ret, int retVal)
403393
{
404394
__u64 pid_tgid = bpf_get_current_pid_tgid();
405395
__u32 pid = pid_tgid >> 32;
406-
struct packet *p = bpf_map_lookup_elem(&natdrop_pids, &pid);
407-
bpf_map_delete_elem(&natdrop_pids, &pid);
408396

409-
if (!p)
397+
if (retVal != NF_DROP)
398+
{
399+
bpf_map_delete_elem(&natdrop_pids, &pid);
410400
return 0;
401+
}
411402

412-
if (retVal != NF_DROP)
403+
struct packet *p = bpf_map_lookup_elem(&natdrop_pids, &pid);
404+
if (!p)
413405
{
414406
return 0;
415407
}
416408

409+
bpf_map_delete_elem(&natdrop_pids, &pid);
410+
417411
update_metrics_map(ctx, IPTABLE_NAT_DROP, 0, p);
418412
return 0;
419413
}
@@ -432,9 +426,6 @@ int BPF_KPROBE(nf_conntrack_confirm, struct sk_buff *skb)
432426

433427
struct packet p;
434428
__builtin_memset(&p, 0, sizeof(p));
435-
436-
p.in_filtermap = false;
437-
p.skb_len = 0;
438429
get_packet_from_skb(&p, skb);
439430

440431
__u64 pid_tgid = bpf_get_current_pid_tgid();
@@ -448,17 +439,21 @@ int BPF_KRETPROBE(nf_conntrack_confirm_ret, int retVal)
448439
{
449440
__u64 pid_tgid = bpf_get_current_pid_tgid();
450441
__u32 pid = pid_tgid >> 32;
442+
443+
if (retVal != NF_DROP)
444+
{
445+
bpf_map_delete_elem(&natdrop_pids, &pid);
446+
return 0;
447+
}
448+
451449
struct packet *p = bpf_map_lookup_elem(&natdrop_pids, &pid);
452-
bpf_map_delete_elem(&natdrop_pids, &pid);
453-
454450
if (!p)
455-
return 0;
456-
457-
if (retVal != NF_DROP)
458451
{
459452
return 0;
460453
}
461454

455+
bpf_map_delete_elem(&natdrop_pids, &pid);
456+
462457
update_metrics_map(ctx, CONNTRACK_ADD_DROP, retVal, p);
463458
return 0;
464459
}

0 commit comments

Comments
 (0)