Skip to content

Commit

Permalink
add 43-tcx-ingress-hello and 44-tcx-egress-hello
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Nov 17, 2024
1 parent d19c9b2 commit e8d9b9b
Show file tree
Hide file tree
Showing 23 changed files with 782 additions and 4 deletions.
1 change: 1 addition & 0 deletions 43-tcx-ingress-hello/Makefile
22 changes: 22 additions & 0 deletions 43-tcx-ingress-hello/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@



## Usage

build:

```
$ make
```

run:

```
$ make run
$ printf 'HTTP/1.1 200 OK\nContent-Length: 0\n\n' |nc -l 9090 &
$ curl http://127.0.0.1:9090
$ make cat
```
1 change: 1 addition & 0 deletions 43-tcx-ingress-hello/cilium-ebpf/Makefile
22 changes: 22 additions & 0 deletions 43-tcx-ingress-hello/cilium-ebpf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@



## Usage

build:

```
$ make
```

run:

```
$ make run
$ printf 'HTTP/1.1 200 OK\nContent-Length: 0\n\n' |nc -l 9090 &
$ curl http://127.0.0.1:9090
$ make cat
```
115 changes: 115 additions & 0 deletions 43-tcx-ingress-hello/cilium-ebpf/bpf_bpfeb.go

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

Binary file added 43-tcx-ingress-hello/cilium-ebpf/bpf_bpfeb.o
Binary file not shown.
115 changes: 115 additions & 0 deletions 43-tcx-ingress-hello/cilium-ebpf/bpf_bpfel.go

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

Binary file added 43-tcx-ingress-hello/cilium-ebpf/bpf_bpfel.o
Binary file not shown.
59 changes: 59 additions & 0 deletions 43-tcx-ingress-hello/cilium-ebpf/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"context"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/rlimit"
"log"
"net"
"os"
"os/signal"
"syscall"
)

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf ../main.bpf.c -- -I../ -I../output

func main() {
if err := rlimit.RemoveMemlock(); err != nil {
log.Fatal(err)
}

objs := bpfObjects{}
if err := loadBpfObjects(&objs, nil); err != nil {
log.Fatal(err)
}
defer objs.Close()

tcIface := "lo"
if v := os.Getenv("IFACE"); v != "" {
tcIface = v
}
log.Printf("interface name: %s", tcIface)
devID, err := net.InterfaceByName(tcIface)
if err != nil {
log.Println(err)
return
}

lk, err := link.AttachTCX(link.TCXOptions{
Interface: devID.Index,
Program: objs.HandleIngress,
Attach: ebpf.AttachTCXIngress,
})
if err != nil {
log.Println(err)
return
}
defer lk.Close()

ctx, stop := signal.NotifyContext(
context.Background(), syscall.SIGINT, syscall.SIGTERM,
)
defer stop()

log.Println("...")
<-ctx.Done()
log.Println("bye bye")
}
44 changes: 44 additions & 0 deletions 43-tcx-ingress-hello/main.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "vmlinux.h"

#include <bpf/bpf_core_read.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#define ETH_P_IP 0x0800 /* Internet Protocol packet */ // ipv4
#define ETH_HLEN 14 /* Total octets in header. */

#define TCX_NEXT -1
#define TCX_PASS 0
#define TCX_DROP 2
#define TCX_REDIRECT 7

SEC("tcx/ingress")
int handle_ingress(struct __sk_buff *skb) {
bpf_skb_pull_data(skb, 0);

u16 h_proto;
if (bpf_skb_load_bytes(skb, offsetof(struct ethhdr, h_proto), &h_proto,
sizeof(h_proto)) < 0)
goto out;
if (bpf_ntohs(h_proto) != ETH_P_IP) // not ipv4
goto out;

struct iphdr ip_hdr;
if (bpf_skb_load_bytes(skb, ETH_HLEN, &ip_hdr, sizeof(ip_hdr)) < 0)
return 0;
if (ip_hdr.protocol != IPPROTO_TCP) // not tcp
return 0;

struct tcphdr tcp_hdr;
if (bpf_skb_load_bytes(skb, ETH_HLEN + sizeof(struct iphdr), &tcp_hdr,
sizeof(tcp_hdr)) < 0)
goto out;

bpf_printk("saddr: %pI4:%d, daddr: %pI4:%d", &ip_hdr.saddr, bpf_htons(tcp_hdr.source), &ip_hdr.daddr, bpf_htons(tcp_hdr.dest));

out:
return TCX_NEXT;
}

char _license[] SEC("license") = "GPL";
9 changes: 9 additions & 0 deletions 43-tcx-ingress-hello/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"log"
)

func main() {
log.Fatal("libbpfgo doesn't support TCX yet. go to ./cilium-ebpf/ to run example")
}
Loading

0 comments on commit e8d9b9b

Please sign in to comment.