Skip to content

Commit 00fcefc

Browse files
smagnani96ti-mo
authored andcommitted
examples: tcx: use Variable API
This commit updates the `example/tcx` program to use the new Variable API rather than traditional bpf maps. The example already depends on `bpf_link`, which requires kernel >= v6.6, therefore the Variable API is already supported and working (since v5.5). Signed-off-by: Simone Magnani <[email protected]>
1 parent 9a89e65 commit 00fcefc

File tree

6 files changed

+22
-56
lines changed

6 files changed

+22
-56
lines changed

examples/tcx/bpf_bpfeb.go

+5-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/tcx/bpf_bpfeb.o

-816 Bytes
Binary file not shown.

examples/tcx/bpf_bpfel.go

+5-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/tcx/bpf_bpfel.o

-816 Bytes
Binary file not shown.

examples/tcx/main.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// This program demonstrates attaching an eBPF program to a network interface
2-
// with Linux TC (Traffic Control). The program counts ingress and egress
3-
// packets using two ARRAY maps.
4-
// The userspace program (Go code in this file) prints the contents
5-
// of the two maps to stdout every second.
2+
// with Linux TCX (Traffic Control with eBPF). The program counts ingress and egress
3+
// packets using two variables. The userspace program (Go code in this file)
4+
// prints the contents of the two variables to stdout every second.
65
// This example depends on tcx bpf_link, available in Linux kernel version 6.6 or newer.
76
package main
87

@@ -78,20 +77,19 @@ func main() {
7877
}
7978
}
8079

81-
func formatCounters(ingressMap, egressMap *ebpf.Map) (string, error) {
80+
func formatCounters(ingressVar, egressVar *ebpf.Variable) (string, error) {
8281
var (
8382
ingressPacketCount uint64
8483
egressPacketCount uint64
85-
key int32
8684
)
8785

8886
// retrieve value from the ingress map
89-
if err := ingressMap.Lookup(&key, &ingressPacketCount); err != nil {
87+
if err := ingressVar.Get(&ingressPacketCount); err != nil {
9088
return "", err
9189
}
9290

9391
// retrieve value from the egress map
94-
if err := egressMap.Lookup(&key, &egressPacketCount); err != nil {
92+
if err := egressVar.Get(&egressPacketCount); err != nil {
9593
return "", err
9694
}
9795

examples/tcx/tcx.c

+6-32
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,17 @@
44

55
char __license[] SEC("license") = "Dual MIT/GPL";
66

7-
/* Define an ARRAY map for storing ingress packet count */
8-
struct {
9-
__uint(type, BPF_MAP_TYPE_ARRAY);
10-
__type(key, __u32);
11-
__type(value, __u64);
12-
__uint(max_entries, 1);
13-
} ingress_pkt_count SEC(".maps");
14-
15-
/* Define an ARRAY map for storing egress packet count */
16-
struct {
17-
__uint(type, BPF_MAP_TYPE_ARRAY);
18-
__type(key, __u32);
19-
__type(value, __u64);
20-
__uint(max_entries, 1);
21-
} egress_pkt_count SEC(".maps");
22-
23-
/*
24-
Upon arrival of each network packet, retrieve and increment
25-
the packet count from the provided map.
26-
Returns TC_ACT_OK, allowing the packet to proceed.
27-
*/
28-
static __always_inline int update_map_pkt_count(void *map) {
29-
__u32 key = 0;
30-
__u64 *count = bpf_map_lookup_elem(map, &key);
31-
if (count) {
32-
__sync_fetch_and_add(count, 1);
33-
}
34-
35-
return TC_ACT_OK;
36-
}
7+
__u64 ingress_pkt_count = 0;
8+
__u64 egress_pkt_count = 0;
379

3810
SEC("tc")
3911
int ingress_prog_func(struct __sk_buff *skb) {
40-
return update_map_pkt_count(&ingress_pkt_count);
12+
__sync_fetch_and_add(&ingress_pkt_count, 1);
13+
return TC_ACT_OK;
4114
}
4215

4316
SEC("tc")
4417
int egress_prog_func(struct __sk_buff *skb) {
45-
return update_map_pkt_count(&egress_pkt_count);
18+
__sync_fetch_and_add(&egress_pkt_count, 1);
19+
return TC_ACT_OK;
4620
}

0 commit comments

Comments
 (0)