Skip to content

pwru: Add --output-skb-metadata and --output-xdp-metadata#509

Merged
brb merged 2 commits intocilium:mainfrom
Asphaltt:feature/output-skb-meta
Apr 11, 2025
Merged

pwru: Add --output-skb-metadata and --output-xdp-metadata#509
brb merged 2 commits intocilium:mainfrom
Asphaltt:feature/output-skb-meta

Conversation

@Asphaltt
Copy link
Contributor

@Asphaltt Asphaltt commented Mar 19, 2025

Fixes #502

I'm porting such feature from bpfsnoop's --output-arg 'skb->hash'.


It's really useful to output specified metadatas of skb instead of outputing the whole skb btf data.

However, it's not easy to --output-skb-metadata 'skb->mark' because the metadata is provided by users.

Here're the steps to achieve it:

  1. Parse the text to C AST.
  2. Convert the C AST to bpf insns with skb btf type info.
  3. Inject the set_skb_metadata stub with the generated insns.
  4. Output the metadata with the provided field's btf type info.

e.g.

$ sudo ./pwru --filter-func 'icmp_rcv' --output-skb-metadata 'skb->hash' --output-skb-metadata 'skb->sw_hash' --output-skb-metadata 'skb->l4_hash' --output-skb-metadata 'skb->dev->ifindex' --filter-trace-xdp --output-xdp-metadata 'xdp->rxq->dev->ifindex' host 1.1.1.1
2025/04/08 13:18:58 Attaching xdp progs...
2025/04/08 13:18:58 Attaching kprobes (via kprobe-multi)...
1 / 1 [----------------------------------------------------------------------------------------------------------------------------------------] 100.00% ? p/s
2025/04/08 13:18:58 Attached (ignored 0)
2025/04/08 13:18:58 Listening for events..
2025/04/08 13:18:58 Failed to retrieve all ifaces from all network namespaces: open /proc/559446/ns/net: no such file or directory. Some iface names might be not shown.
SKB                CPU PROCESS          NETNS      MARK/x        IFACE       PROTO  MTU   LEN   TUPLE FUNC
0xffffa64a80600ae8 6   <empty>:0        4026531840 0            ens33:2      0x0000 1500  98    1.1.1.1:0->192.168.241.133:0(icmp) bpf_prog_3b185187f1855c4c_dummy[bpf](xdp) ifindex=2
0xffff991e4b8d5b00 6   <empty>:0        4026531840 0            ens33:2      0x0800 65536 64    1.1.1.1:0->192.168.241.133:0(icmp) icmp_rcv hash=0 sw_hash=0 l4_hash=0 ifindex=2
^C2025/04/08 13:19:19 Received signal, exiting program..
2025/04/08 13:19:19 Detaching kprobes...
1 / 1 [----------------------------------------------------------------------------------------------------------------------------------------] 100.00% ? p/s

@Asphaltt Asphaltt force-pushed the feature/output-skb-meta branch 2 times, most recently from 565250b to e97a6da Compare March 20, 2025 12:38
@brb
Copy link
Member

brb commented Apr 4, 2025

Yep, this feature makes sense 👍

Mind updating the PR?

@Asphaltt
Copy link
Contributor Author

Asphaltt commented Apr 4, 2025

Mind updating the PR?

I will update the PR later.

@Asphaltt Asphaltt force-pushed the feature/output-skb-meta branch from e97a6da to 68154ce Compare April 7, 2025 16:01
@Asphaltt Asphaltt changed the title [RFC] pwru: Support --output-skb-metadata pwru: Add --output-skb-metadata and --output-xdp-metadata Apr 7, 2025
@Asphaltt Asphaltt force-pushed the feature/output-skb-meta branch 3 times, most recently from a64245e to 9a9c914 Compare April 8, 2025 05:06
Because the `filter_pcap_*` subprogs use its own stack memory, it is
unnecessary to reserved 80B stack memory.

Therefore, `filter_pcap_*` subprogs can use the whole stack memory.

Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
@Asphaltt Asphaltt force-pushed the feature/output-skb-meta branch from 9a9c914 to 1e520b1 Compare April 8, 2025 05:08
@Asphaltt Asphaltt marked this pull request as ready for review April 8, 2025 05:20
@Asphaltt Asphaltt requested a review from a team as a code owner April 8, 2025 05:20
@Asphaltt Asphaltt requested review from gentoo-root and removed request for a team April 8, 2025 05:20
@brb brb self-requested a review April 8, 2025 06:27
@Asphaltt Asphaltt force-pushed the feature/output-skb-meta branch 2 times, most recently from 17e40db to b3c8c05 Compare April 8, 2025 13:19
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.

Nice! Mostly questions / nits.

@brb brb requested review from jschwinger233 and removed request for gentoo-root April 9, 2025 17:21
@Asphaltt Asphaltt force-pushed the feature/output-skb-meta branch from b3c8c05 to 8eec248 Compare April 10, 2025 07:12
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!

It's really useful to output specified metadatas of skb instead of
outputing the whole skb btf data.

However, it's not easy to `--output-skb-metadata 'skb->mark'` because
the metadata is provided by users.

Here're the steps to achieve it:

1. Parse the text to C AST.
2. Convert the C AST to bpf insns with `skb` btf type info.
3. Inject the `set_skb_metadata` stub with the generated insns.
4. Output the metadata with the provided field's btf type info.

e.g.

```bash
$ sudo ./pwru --filter-func 'icmp_rcv' --output-skb-metadata 'skb->hash' --output-skb-metadata 'skb->sw_hash' --output-skb-metadata 'skb->l4_hash' --filter-trace-xdp --output-xdp-metadata 'xdp->rxq->queue_index' host 1.1.1.1
2025/04/07 15:55:11 Attaching xdp progs...
2025/04/07 15:55:11 Attaching kprobes (via kprobe-multi)...
1 / 1 [----------------------------------------------------------------------------------------------------------------------------------------] 100.00% ? p/s
2025/04/07 15:55:11 Attached (ignored 0)
2025/04/07 15:55:11 Listening for events..
2025/04/07 15:55:11 Failed to retrieve all ifaces from all network namespaces: open /proc/559446/ns/net: no such file or directory. Some iface names might be not shown.
SKB                CPU PROCESS          NETNS      MARK/x        IFACE       PROTO  MTU   LEN   TUPLE FUNC
0xffffa64a81e67a38 6   ~wru/pwru:746016 4026531840 0            ens33:2      0x0000 1500  98    1.1.1.1:0->192.168.241.133:0(icmp) bpf_prog_3b185187f1855c4c_dummy[bpf](xdp) queue_index=0
0xffff991e4b8d5d00 6   ~wru/pwru:746016 4026531840 0            ens33:2      0x0800 65536 64    1.1.1.1:0->192.168.241.133:0(icmp) icmp_rcv hash=0 sw_hash=0 l4_hash=0
^C2025/04/07 15:55:46 Received signal, exiting program..
2025/04/07 15:55:46 Detaching kprobes...
1 / 1 [----------------------------------------------------------------------------------------------------------------------------------------] 100.00% ? p/s
```

Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
@Asphaltt Asphaltt force-pushed the feature/output-skb-meta branch from 8eec248 to fcc6d97 Compare April 10, 2025 16:05
@brb brb merged commit 694b750 into cilium:main Apr 11, 2025
8 checks passed
@Asphaltt Asphaltt deleted the feature/output-skb-meta branch April 11, 2025 08:35
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.

Support dumping skb hash

3 participants