-
Notifications
You must be signed in to change notification settings - Fork 63
Add qos example #628
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
base: sneaky-potato/gsoc26
Are you sure you want to change the base?
Add qos example #628
Changes from all commits
3e1c688
27952b0
a1d5d08
7b900ca
3b1c4dc
df975c8
60f0f01
c491b7e
9204fb8
9466196
1df3778
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # SPDX-FileCopyrightText: (c) 2026 Ashwani Kumar Kamal <ashwanikamal.im421@gmail.com> | ||
| # SPDX-License-Identifier: MIT OR GPL-2.0-only | ||
|
|
||
| all: vmlinux classify.o | ||
|
|
||
| vmlinux: | ||
| bpftool btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h | ||
|
|
||
| classify.o: classify.c | ||
| clang -target bpf -Wall -O2 -c -g $< | ||
|
|
||
| clean: | ||
| rm -f vmlinux.h classify.o | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: (c) 2026 Ashwani Kumar Kamal <ashwanikamal.im421@gmail.com> | ||
| * SPDX-License-Identifier: MIT OR GPL-2.0-only | ||
| */ | ||
|
|
||
| #include "vmlinux.h" | ||
| #include <bpf/bpf_helpers.h> | ||
| #include <bpf/bpf_endian.h> | ||
|
|
||
| extern int bpf_luatc_run(char *key, size_t key__sz, struct __sk_buff *skb, void *arg, size_t arg__sz) __ksym; | ||
|
|
||
| static char runtime[] = "examples/qos/tc"; | ||
|
|
||
| int const TC_ACT_OK = 0; | ||
|
|
||
| SEC("classifier") | ||
| int classify(struct __sk_buff *skb) | ||
| { | ||
| int action = bpf_luatc_run(runtime, sizeof(runtime), skb, NULL, 0); | ||
| return action < 0 ? TC_ACT_OK : action; | ||
| } | ||
|
|
||
| char _license[] SEC("license") = "Dual MIT/GPL"; | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| -- | ||
| -- SPDX-FileCopyrightText: (c) 2026 Ashwani Kumar Kamal <ashwanikamal.im421@gmail.com> | ||
| -- SPDX-License-Identifier: MIT OR GPL-2.0-only | ||
| -- | ||
|
|
||
| local tc = require("tc") | ||
| local action = require("linux.tc") | ||
| local skbattr = require("skb.attr") | ||
| local map = require("ebpf.map") | ||
|
|
||
| local TC_H_MAKE = function(maj, min) return (maj << 16) | min end | ||
|
|
||
| local stats = map.open("/sys/fs/bpf/flow_stats") | ||
|
|
||
| -- struct flow_stats { | ||
| -- u64 packets; | ||
| -- u32 avg_pkt_size; | ||
| -- }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now we can have named struct fields on autogen; won't it help here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. flow_stats is currently only defined in the example BPF program, not in a kernel header. Are you suggesting to move it to a shared header so autogen can generate a layout for it, or some other mechanism?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't suggesting that, but I think it's a good idea to leverage autogen for this. |
||
|
|
||
| local KEY_FMT = "<I4" | ||
| local VAL_FMT = "<I8I4" | ||
|
|
||
| local function qos(ctx) | ||
| local skb = skbattr(ctx:skb()) | ||
| local hash = skb.hash | ||
| if hash == 0 then | ||
| ctx:action(action.ACT_OK) | ||
| return | ||
| end | ||
| local key = string.pack(KEY_FMT, hash) | ||
| local value = stats:lookup(key) | ||
|
|
||
| local packets, avg | ||
|
|
||
| if value then | ||
| packets, avg = string.unpack(VAL_FMT, value) | ||
| else | ||
| packets = 0 | ||
| avg = 0 | ||
| end | ||
|
|
||
| packets = packets + 1 | ||
| if packets == 1 then | ||
| avg = #skb | ||
| else | ||
| avg = (avg * 7 + #skb) // 8 | ||
| end | ||
|
|
||
| value = string.pack(VAL_FMT, packets, avg) | ||
|
|
||
| stats:update(key, value) | ||
|
|
||
| local mask = (avg < 256 and 0x10) or (avg < 800 and 0x20) or 0x30 | ||
| skb.priority = TC_H_MAKE(1, mask) | ||
|
|
||
| ctx:action(action.ACT_OK) | ||
| end | ||
|
|
||
| tc.attach(qos) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # SPDX-FileCopyrightText: (c) 2026 Ashwani Kumar Kamal <ashwanikamal.im421@gmail.com> | ||
| # SPDX-License-Identifier: MIT OR GPL-2.0-only | ||
|
|
||
| all: vmlinux classify.o | ||
|
|
||
| vmlinux: | ||
| bpftool btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h | ||
|
|
||
| classify.o: classify.c | ||
| clang -target bpf -Wall -O2 -c -g $< | ||
|
|
||
| clean: | ||
| rm -f vmlinux.h classify.o | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the caps?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a defined kernel macro (/include/uapi/linux/pkt_sched.h#L72)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine to leave it as is by now, but I think we should have a better way to export such macros.. perhaps leveraging autogen as well.. it is worth creating an issue at least..