Skip to content

Commit

Permalink
simplify and update tcx example to use Variable API
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
smagnani96 committed Nov 13, 2024
1 parent 5e1f032 commit 96cd686
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 56 deletions.
13 changes: 5 additions & 8 deletions examples/tcx/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified examples/tcx/bpf_bpfeb.o
Binary file not shown.
13 changes: 5 additions & 8 deletions examples/tcx/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified examples/tcx/bpf_bpfel.o
Binary file not shown.
14 changes: 6 additions & 8 deletions examples/tcx/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// This program demonstrates attaching an eBPF program to a network interface
// with Linux TC (Traffic Control). The program counts ingress and egress
// packets using two ARRAY maps.
// The userspace program (Go code in this file) prints the contents
// of the two maps to stdout every second.
// with Linux TCX (Traffic Control with eBPF). The program counts ingress and egress
// packets using two variables. The userspace program (Go code in this file)
// prints the contents of the two variables to stdout every second.
// This example depends on tcx bpf_link, available in Linux kernel version 6.6 or newer.
package main

Expand Down Expand Up @@ -78,20 +77,19 @@ func main() {
}
}

func formatCounters(ingressMap, egressMap *ebpf.Map) (string, error) {
func formatCounters(ingressVar, egressVar *ebpf.Variable) (string, error) {
var (
ingressPacketCount uint64
egressPacketCount uint64
key int32
)

// retrieve value from the ingress map
if err := ingressMap.Lookup(&key, &ingressPacketCount); err != nil {
if err := ingressVar.Get(&ingressPacketCount); err != nil {
return "", err
}

// retrieve value from the egress map
if err := egressMap.Lookup(&key, &egressPacketCount); err != nil {
if err := egressVar.Get(&egressPacketCount); err != nil {
return "", err
}

Expand Down
38 changes: 6 additions & 32 deletions examples/tcx/tcx.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,17 @@

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

/* Define an ARRAY map for storing ingress packet count */
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, __u32);
__type(value, __u64);
__uint(max_entries, 1);
} ingress_pkt_count SEC(".maps");

/* Define an ARRAY map for storing egress packet count */
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, __u32);
__type(value, __u64);
__uint(max_entries, 1);
} egress_pkt_count SEC(".maps");

/*
Upon arrival of each network packet, retrieve and increment
the packet count from the provided map.
Returns TC_ACT_OK, allowing the packet to proceed.
*/
static __always_inline int update_map_pkt_count(void *map) {
__u32 key = 0;
__u64 *count = bpf_map_lookup_elem(map, &key);
if (count) {
__sync_fetch_and_add(count, 1);
}

return TC_ACT_OK;
}
__u64 ingress_pkt_count = 0;
__u64 egress_pkt_count = 0;

SEC("tc")
int ingress_prog_func(struct __sk_buff *skb) {
return update_map_pkt_count(&ingress_pkt_count);
ingress_pkt_count++;
return TC_ACT_OK;
}

SEC("tc")
int egress_prog_func(struct __sk_buff *skb) {
return update_map_pkt_count(&egress_pkt_count);
egress_pkt_count++;
return TC_ACT_OK;
}

0 comments on commit 96cd686

Please sign in to comment.