-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.go
173 lines (158 loc) · 4.81 KB
/
server.go
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
167
168
169
170
171
172
173
package main
import (
"context"
"fmt"
"github.com/cilium/ebpf"
"github.com/hemanthmalla/reuseport_ebpf/compile"
"github.com/prometheus/procfs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/sys/unix"
"io"
"net"
"net/http"
"os"
"path/filepath"
"reflect"
"syscall"
)
func loadReuseportSelect(s *zap.SugaredLogger) *ebpf.Program {
stateDir := "/sys/fs/bpf/tc/globals"
collectionLocation := "./bpf/"
if _, err := os.Stat(filepath.Join("./bpf/", "reuseport_select.o")); err != nil {
if err := compile.CompileWithOptions(context.TODO(), "./bpf/reuseport_select.c", "./bpf/reuseport_select.o", []string{"-v", "-I", "./bpf/", "-I", "./bpf/include/"}); err != nil {
s.Errorf("failed to compile bpf_reuseport_select.c %w", err)
} else {
s.Info("Compiled bpf_reuseport_select.o successfully !")
}
}
var prog *ebpf.Program
var coll *ebpf.Collection
opts := ebpf.CollectionOptions{
Maps: ebpf.MapOptions{
PinPath: stateDir,
},
Programs: ebpf.ProgramOptions{
LogDisabled: false,
LogSize: ebpf.DefaultVerifierLogSize,
LogLevel: ebpf.LogLevelInstruction,
},
}
spec, err := ebpf.LoadCollectionSpec(filepath.Join(collectionLocation, "reuseport_select.o"))
if err != nil {
s.Errorf("Unable to load at %s: %w", filepath.Join(collectionLocation, "reuseport_select.o"), err)
return nil
} else {
coll, err = ebpf.NewCollectionWithOptions(spec, opts)
if err != nil {
s.Errorf("failed to create eBPF collection: %w", err)
return nil
}
prog = coll.Programs["hot_standby_selector"]
if prog == nil {
s.Error("program not found in collection\n")
return nil
}
}
return prog
}
func getListenConfig(prog *ebpf.Program, mode string, otherInstancesRunning bool) net.ListenConfig {
lc := net.ListenConfig{Control: func(network, address string, c syscall.RawConn) error {
var opErr error
err := c.Control(func(fd uintptr) {
// Set SO_REUSEPORT on the socket
opErr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
// Set eBPF program to be invoked for socket selection
if prog != nil && mode == "primary" && !otherInstancesRunning {
err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_ATTACH_REUSEPORT_EBPF, prog.FD())
if err != nil {
opErr = fmt.Errorf("setsockopt(SO_ATTACH_REUSEPORT_EBPF) failed: %w", err)
} else {
zap.S().Info("SO_REUSEPORT bpf prog attach completed successfully")
}
}
})
if err != nil {
return err
}
return opErr
}}
return lc
}
func handleHello(w http.ResponseWriter, r *http.Request) {
zap.S().Info("got /hello request\n")
io.WriteString(w, fmt.Sprintf("Hello eBPF Summit 2023 - %s!\n", os.Args[1]))
}
// GetFdFromListener get net.Listener's file descriptor.
func GetFdFromListener(l net.Listener) int {
v := reflect.Indirect(reflect.ValueOf(l))
netFD := reflect.Indirect(v.FieldByName("fd"))
pfd := netFD.FieldByName("pfd")
fd := int(pfd.FieldByName("Sysfd").Int())
return fd
}
func main() {
cfg := zap.NewDevelopmentConfig()
cfg.EncoderConfig = zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
TimeKey: "time",
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: func(caller zapcore.EntryCaller, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString("")
}}
logger, _ := cfg.Build()
s := logger.Sugar()
mode := os.Args[1]
if mode != "primary" && mode != "standby" {
s.Infof("Server mode should either be primary or standy")
return
}
s.Infof("Starting server in %s mode", mode)
prog := loadReuseportSelect(s)
http.HandleFunc("/hello", handleHello)
server := http.Server{Addr: "127.0.0.1:8080", Handler: nil}
fs, _ := procfs.NewDefaultFS()
netTCP, _ := fs.NetTCP()
otherInstancesRunning := false
for _, i := range netTCP {
if i.LocalPort == 8080 {
otherInstancesRunning = true
break
}
}
lc := getListenConfig(prog, mode, otherInstancesRunning)
ln, err := lc.Listen(context.Background(), "tcp", server.Addr)
if err != nil {
s.Fatalf("Unable to listen of specified addr %w", err)
} else {
s.Infof("Started listening in 127.0.0.1:8080 successfully !")
}
stateDir := "/sys/fs/bpf/tc/globals"
mapName := "tcp_balancing_targets"
var k uint32
if mode == "primary" {
k = uint32(0)
} else {
k = uint32(1)
}
v := uint64(GetFdFromListener(ln))
s.Infof("Updating with k=%d v=%d", k, v)
m, err := ebpf.LoadPinnedMap(filepath.Join(stateDir, mapName), nil)
if err != nil {
s.Errorf("Unable to load map at %s : %w", filepath.Join(stateDir, mapName), err)
} else {
err = m.Put(k, v)
if err != nil {
s.Errorf("Map update for %s failed : %w", mapName, err)
} else {
s.Infof("Map update for %s succeeded", mapName)
}
}
err = server.Serve(ln)
if err != nil {
s.Fatalf("Unable to start HTTP server %w", err)
}
}