-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add 43-tcx-ingress-hello and 44-tcx-egress-hello
- Loading branch information
Showing
23 changed files
with
782 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../common/Makefile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../common/cilium-ebpf.Makefile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Oops, something went wrong.