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

bpf: Differentiate skb tracking reasons #384

Merged
merged 1 commit into from
Jun 11, 2024

Conversation

jschwinger233
Copy link
Member

By differentiating skb track reason (filter/skb/stackid), we can avoid a mismatching issue that leads to confusing output.

I saw an issue when --filter-track-skb and --filter-track-skb-by-stackid are used together:

  1. At func1, an skb matches the pcap filter, we then fill the map stackid_skb[stackid] = skb for --filter-track-skb-by-stackid;
  2. At func2, the skb arg doesn't match the pcap filter, but stackid can be found in map stackid_skb. We therefore store the map entry skb_addresses[skb] = true for --filter-track-skb;
  3. Due to some reasons, this skb never gets freed by kfree_skbmem(), so pwru keeps matching "wrong" packets due to permanent entry in map skb_addresses for --filter-track-skb;

This patch mitigates these risks by not storing new entry in skb_addresses if the skb is tracked by stackid. This doesn't harm the original purpose of --filter-track-skb-by-stack, which is designed for non-skb function tracing on the same stack.

On the otherhand, calling bpf_update_map() only for specific track reasons improves performance a little bit. For example, if an skb is already in map skb_addresses, now we don't need to put it in the map again.

By differentiating skb track reason (filter/skb/stackid), we can avoid a
mismatching issue that leads to confusing output.

I saw an issue when --filter-track-skb and --filter-track-skb-by-stackid
are used together:
1. At func1, an skb matches the pcap filter, we then fill the map
   stackid_skb[stackid] = skb for --filter-track-skb-by-stackid;
2. At func2, the skb arg doesn't match the pcap filter, but stackid can
   be found in map stackid_skb. We therefore store the map entry
   skb_addresses[skb] = true for --filter-track-skb;
3. Due to some reasons, this skb never gets freed by kfree_skbmem(), so
   pwru keeps matching "wrong" packets due to permanent entry in map
   skb_addresses for --filter-track-skb;

This patch mitigates these risks by not storing new entry in
skb_addresses if the skb is tracked by stackid. This doesn't harm the
original purpose of --filter-track-skb-by-stack, which is designed for
non-skb function tracing on the same stack.

On the otherhand, calling bpf_update_map() only for specific track
reasons improves performance a little bit. For example, if an skb is
already in map skb_addresses, now we don't need to put it in the map
again.

Signed-off-by: gray <gray.liang@isovalent.com>
@jschwinger233 jschwinger233 marked this pull request as ready for review June 4, 2024 03:49
@jschwinger233 jschwinger233 requested a review from a team as a code owner June 4, 2024 03:49
Copy link
Member

@brb brb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@brb brb merged commit 059997a into cilium:main Jun 11, 2024
6 checks passed
jschwinger233 added a commit to jschwinger233/pwru that referenced this pull request Jul 12, 2024
cilium#391 allows --track-skb to track new
skb on veth, it relies on a map lookup to decide whether to track or
not:

```
SEC("kprobe/veth_convert_skb_to_xdp_buff")
int kprobe_veth_convert_skb_to_xdp_buff(struct pt_regs *ctx) {
[...]
	u64 skb_head = (u64) BPF_CORE_READ(skb, head);
	if (bpf_map_lookup_elem(&skb_heads, &skb_head)) {
[...]
	}
	return BPF_OK;
}
```

However, when --track-skb-by-stackid is used along with --track-skb, the
tracked skbs have risks not being recorded in skb_heads map.

This is because:
1. cilium#384 no more updates skb_heads
map when track reason is "by_stackid".
2. cilium#339 changes --track-skb from
   tracking &skb to tracking skb->head.

So imagine an skb whose original skb->head = 0xa, whose value is
updated to 0xb after a while. The first time pwru sees this skb,
skb_heads map will insert 0xa entry. However, after skb->head is set to
0xb, pwru sees the skb being tracked due to "by_stackid", we won't
insert 0xb entry into skb_heads map.

Then when the skb is on veth, we can't find an entry by looking up 0xb
from skb_heads map, ending up with losing track of veth skb again.

This patch fixes the issue by raising the priority of track_by_filter:
if an skb can be defined as tracked_by_filter and tracked_by_stackid,
use tracked_by_filter over tracked_by_stackid.

Another issue cilium#339 brings about is,
an skb can have multiple skb->head stored in skb_heads map during its
lifetime, but we only clean the latest value at
kprobe_skb_lifetime_termination. This issue is beyond this patch.

Signed-off-by: gray <gray.liang@isovalent.com>
jschwinger233 added a commit to jschwinger233/pwru that referenced this pull request Jul 12, 2024
cilium#391 allows --track-skb to track new
skb on veth, it relies on a map lookup to decide whether to track or
not:

```
SEC("kprobe/veth_convert_skb_to_xdp_buff")
int kprobe_veth_convert_skb_to_xdp_buff(struct pt_regs *ctx) {
[...]
	u64 skb_head = (u64) BPF_CORE_READ(skb, head);
	if (bpf_map_lookup_elem(&skb_heads, &skb_head)) {
[...]
	}
	return BPF_OK;
}
```

However, when --track-skb-by-stackid is used along with --track-skb, the
tracked skbs have risks not being recorded in skb_heads map.

This is because:
1. cilium#384 no more updates skb_heads
map when track reason is "by_stackid".
2. cilium#339 changes --track-skb from
   using &skb to skb->head.

So imagine an skb whose original skb->head = 0xa, the value is updated
to 0xb after a while. The first time pwru sees this skb, skb_heads map
will insert 0xa entry, this is correct. However, after skb->head being
set to 0xb, pwru will verdict the skb of being tracked due to
"by_stackid", we end up not inserting 0xb entry into skb_heads map.

Then the skb reaches veth, pwru can't find an entry by looking up 0xb
from skb_heads map, we are losing track of veth skb again.

This patch fixes the issue by raising the priority of track_by_filter:
if an skb can be defined as both tracked_by_filter and
tracked_by_stackid, use tracked_by_filter over tracked_by_stackid.

Another issue cilium#339 brings about is,
an skb can have multiple skb->head stored in skb_heads map during its
lifetime, but we only clean the latest value at
kprobe_skb_lifetime_termination. This issue is beyond this patch.

Signed-off-by: gray <gray.liang@isovalent.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants