8000 add socket-lookup-cgroup bpf demo · lx1036/code@d1ef50e · GitHub
[go: up one dir, main page]

Skip to content

Commit d1ef50e

Browse files
author
shenming
committed
add socket-lookup-cgroup bpf demo
1 parent 62b306a commit d1ef50e

File tree

15 files changed

+1245
-47
lines changed

15 files changed

+1245
-47
lines changed

go/k8s/bpf/xdp-l4lb/xdp-cilium-l4lb/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,6 @@ add_executable(xdp_cilium_l4lb
120120
cilium/test/tproxy/socket-service/tcphdr/rewrite-constant/test_rewrite_const.c
121121
cilium/test/tproxy/socket-service/udp-sk-limit/test_udp_sk_limit.c
122122
cilium/test/tproxy/socket-service/tcphdr/tcp-socket-cookie/test_tcp_socket_cookie.c
123-
cilium/test/tproxy/socket-service/tcphdr/tcpfsm/test_tcp_fsm.c)
123+
cilium/test/tproxy/socket-service/tcphdr/tcpfsm/test_tcp_fsm.c
124+
cilium/test/tproxy/socket-lookup/socket-lookup-cgroup/socket_lookup_cgroup.c
125+
cilium/test/tproxy/socket-lookup/socket-lookup-cgroup/linux_tcp.h)

go/k8s/bpf/xdp-l4lb/xdp-cilium-l4lb/cilium/test/tcp/tcpconnect/tcp_connect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88

9-
#include <vmlinux.h>
9+
//#include <vmlinux.h>
1010

1111
#include <linux/bpf.h>
1212

go/k8s/bpf/xdp-l4lb/xdp-cilium-l4lb/cilium/test/tproxy/socket-assign/test_sk_assign.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ get_tuple(struct __sk_buff *skb, bool *tcp) {
6868
/* Options are not supported */
6969
return NULL;
7070

71-
ihl_len = iph->ihl * 4;
71+
ihl_len = iph->ihl * 4; // iphdr 头字节长度 <<2
7272
proto = iph->protocol;
7373
result = (struct bpf_sock_tuple *) &iph->saddr; // 这个类型转换非常经典,bpf_sock_tuple 就是 saddr/daddr/sport/dport!!!
7474
} else {

go/k8s/bpf/xdp-l4lb/xdp-cilium-l4lb/cilium/test/tproxy/socket-lookup/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
p 10000 ackage main
22

33
import (
4+
"fmt"
45
"github.com/cilium/ebpf"
56
"github.com/cilium/ebpf/link"
7+
"github.com/containernetworking/plugins/pkg/ns"
68
"github.com/sirupsen/logrus"
79
"golang.org/x/sys/unix"
810
"net"
11+
"path/filepath"
912
)
1013

1114
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go bpf test_sk_lookup.c -- -I.
@@ -341,3 +344,24 @@ func makeServer(socketType int, reuseportProg *ebpf.Program) [MAX_SERVERS]int {
341344

342345
return sockfds
343346
}
347+
348+
func openNetNS(nsPath, bpfFsPath string) (ns.NetNS, string, error) {
349+
var fs unix.Statfs_t
350+
err := unix.Statfs(bpfFsPath, &fs)
351+
if err != nil || fs.Type != unix.BPF_FS_MAGIC {
352+
return nil, "", fmt.Errorf("invalid BPF filesystem path: %s", bpfFsPath)
353+
}
354+
355+
netNs, err := ns.GetNS(nsPath)
356+
if err != nil {
357+
return nil, "", err
358+
}
359+
360+
var stat unix.Stat_t
361+
if err := unix.Fstat(int(netNs.Fd()), &stat); err != nil {
362+
return nil, "", fmt.Errorf("stat netns: %s", err)
363+
}
364+
365+
dir := fmt.Sprintf("%d_dispatcher", stat.Ino)
366+
return netNs, filepath.Join(bpfFsPath, dir), nil
367+
}

go/k8s/bpf/xdp-l4lb/xdp-cilium-l4lb/cilium/test/tproxy/socket-lookup/main_test.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ enum server {
1515
int main() {
1616
int server_fds[] = {[0 ... MAX_SERVERS - 1] = -1};
1717
int size = ARRAY_SIZE(server_fds);
18-
printf("%d", size); // 2
18+
printf("%d\n", size); // 2
19+
20+
int cnt = ~0;
21+
printf("%d\n", cnt); // -1
1922
}
2023

go/k8s/bpf/xdp-l4lb/xdp-cilium-l4lb/cilium/test/tproxy/socket-lookup/netns.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

go/k8s/bpf/xdp-l4lb/xdp-cilium-l4lb/cilium/test/tproxy/socket-lookup/socket-lookup-cgroup/bpf_bpfeb.go

Lines changed: 145 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/k8s/bpf/xdp-l4lb/xdp-cilium-l4lb/cilium/test/tproxy/socket-lookup/socket-lookup-cgroup/bpf_bpfel.go

Lines changed: 145 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0