-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go.bak
166 lines (136 loc) · 3.33 KB
/
main.go.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//go:build linux
// +build linux
// go get github.com/cilium/ebpf/cmd/bpf2go@master
//go:generate bpf2go -cc clang-11 -cflags "-g -O2 -Wall -Wextra" bpf fentry.c ./linux-5.9.1/tools/lib/bpf/libbpf.a -- -I./linux-5.9.1/usr/include -I./linux-5.9.1/tools/lib -I./headers
package main
import (
"log"
"net"
"os"
"os/signal"
"syscall"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/rlimit"
"golang.org/x/sys/unix"
)
const (
MapsPinpath = "/sys/fs/bpf/echo_dispatch_prog"
)
const ServerAddr = "/tmp/echo_server"
// sudo cat /sys/kernel/debug/tracing/trace_pipe
func main() {
stopper := make(chan os.Signal, 1)
signal.Notify(stopper, os.Interrupt, syscall.SIGTERM)
// Allow the current process to lock memory for eBPF resources.
if err := rlimit.RemoveMemlock(); err != nil {
log.Panicln(err)
}
/*
// Create sockmap
var sockMap *ebpf.Map
sockMap, err := ebpf.NewMap(&ebpf.MapSpec{
Type: ebpf.MapType(15), // BPF_MAP_TYPE_SOCKMAP
KeySize: 4,
ValueSize: 4,
MaxEntries: 2,
})
if err != nil {
log.Panicln(err)
}
defer sockMap.Close()
// */
// var options ebpf.CollectionOptions
// os.Mkdir(MapsPinpath, os.ModePerm)
// options.Maps.PinPath = MapsPinpath
// objs.bpfMaps.EchoSocket.FD()
// Load pre-compiled programs and maps into the kernel.
objs := bpfObjects{}
// err := loadBpfObjects(&objs, &options)
err := loadBpfObjects(&objs, nil)
if err != nil {
log.Panicln(err)
}
defer objs.Close()
// Attach program
netns, err := os.Open("/proc/self/ns/net")
if err != nil {
log.Panicln(err)
}
defer netns.Close()
lnk, err := link.AttachNetNs(int(netns.Fd()), objs.bpfPrograms.EchoDispatch)
defer lnk.Close()
// err = link.RawAttachProgram(link.RawAttachProgramOptions{
// Target: int(netns.Fd()),
// Program: objs.bpfPrograms.EchoDispatch,
// Attach: ebpf.AttachSkLookup,
// Flags: 0,
// })
if err != nil {
log.Panicln(err)
}
// Create server
os.Remove(ServerAddr)
ln, err := net.Listen("tcp", "127.0.0.1:8081")
if err != nil {
log.Panicln(err)
}
go func() {
<-stopper
log.Println("Stoping..")
os.Exit(0)
}()
lnUnix := ln.(*net.TCPListener)
f, err := lnUnix.File()
if err != nil {
log.Panicln(err)
}
log.Println("Fd:", f.Fd())
log.Println(unix.SetsockoptInt(int(f.Fd()), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1))
err = objs.bpfMaps.EchoSocket.Put(uint32(0), uint64(f.Fd()))
if err != nil {
log.Panicln(err)
}
log.Println("Put done")
mapIter := objs.bpfMaps.EchoSocket.Iterate()
var keyOut1 uint32
var valOut1 uint64
log.Println(mapIter.Next(&keyOut1, &valOut1))
log.Println(keyOut1, valOut1)
err = objs.bpfMaps.EchoPorts.Put(uint16(0x0309), uint8(0))
if err != nil {
log.Panicln(err)
}
log.Println("Put done")
mapIter = objs.bpfMaps.EchoPorts.Iterate()
var keyOut uint16
var valOut uint8
log.Println(mapIter.Next(&keyOut, &valOut))
log.Println(keyOut, valOut)
// sysConn, err := lnUnix.SyscallConn()
// if err != nil {
// log.Panicln(err)
// }
// log.Println(sysConn)
log.Println("Accepting..")
for {
conn, err := ln.Accept()
if err != nil {
log.Println(err)
continue
}
log.Println("New conn")
go handleEcho(conn)
}
}
func handleEcho(conn net.Conn) {
buf := make([]byte, 1024)
for {
n, err := conn.Read(buf)
if err != nil {
log.Println(err)
return
}
log.Println(string(buf[:n]))
conn.Write(buf[:n])
}
}