forked from l3af-project/l3afd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
217 lines (175 loc) · 5.88 KB
/
main.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright Contributors to the L3AF Project.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
"github.com/l3af-project/l3afd/apis"
"github.com/l3af-project/l3afd/apis/handlers"
"github.com/l3af-project/l3afd/config"
"github.com/l3af-project/l3afd/kf"
"github.com/l3af-project/l3afd/models"
"github.com/l3af-project/l3afd/pidfile"
"github.com/l3af-project/l3afd/stats"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const daemonName = "l3afd"
func setupLogging() {
const logLevelEnvName = "L3AF_LOG_LEVEL"
// If this is removed, zerolog will do structured logging. For now,
// we set zerolog to do human-readable logging just to keep the same
// behavior as the closed-source logging package that we replaced with
// zerolog.
log.Logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stderr, TimeFormat: time.RFC3339Nano})
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
// Set the default
zerolog.SetGlobalLevel(zerolog.InfoLevel)
logLevelStr := os.Getenv(logLevelEnvName)
if logLevelStr == "" {
return
}
logLevel, err := zerolog.ParseLevel(logLevelStr)
if err != nil {
log.Error().Err(err).Msg("Invalid L3AF_LOG_LEVEL")
return
}
zerolog.SetGlobalLevel(logLevel)
log.Debug().Msgf("Log level set to %q", logLevel)
}
func main() {
setupLogging()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log.Info().Msgf("%s started.", daemonName)
var confPath string
flag.StringVar(&confPath, "config", "config/l3afd.cfg", "config path")
flag.Parse()
initVersion()
conf, err := config.ReadConfig(confPath)
if err != nil {
log.Fatal().Err(err).Msgf("Unable to parse config %q", confPath)
}
if err = pidfile.CheckPIDConflict(conf.PIDFilename); err != nil {
log.Fatal().Err(err).Msgf("The PID file: %s, is in an unacceptable state", conf.PIDFilename)
}
if err = pidfile.CreatePID(conf.PIDFilename); err != nil {
log.Fatal().Err(err).Msgf("The PID file: %s, could not be created", conf.PIDFilename)
}
if err = checkKernelVersion(conf); err != nil {
log.Fatal().Err(err).Msg("The unsupported kernel version please upgrade")
}
if err = registerL3afD(conf); err != nil {
log.Error().Err(err).Msg("L3afd registration failed")
}
kfConfigs, err := SetupNFConfigs(ctx, conf)
if err != nil {
log.Fatal().Err(err).Msg("L3afd failed to start")
}
t, err := ReadConfigsFromConfigStore(conf)
if err != nil {
log.Error().Err(err).Msg("L3afd failed to read configs from store")
}
if t != nil {
if err := kfConfigs.DeployeBPFPrograms(t); err != nil {
log.Error().Err(err).Msg("L3afd filed to deploy persistent configs from store")
}
}
if err := handlers.InitConfigs(kfConfigs); err != nil {
log.Fatal().Err(err).Msg("L3afd failed to initialise configs")
}
if conf.EBPFChainDebugEnabled {
kf.SetupKFDebug(conf.EBPFChainDebugAddr, kfConfigs)
}
select {}
}
func SetupNFConfigs(ctx context.Context, conf *config.Config) (*kf.NFConfigs, error) {
// Get Hostname
machineHostname, err := os.Hostname()
if err != nil {
log.Error().Err(err).Msg("Could not get hostname from OS")
}
// setup Metrics endpoint
stats.SetupMetrics(machineHostname, daemonName, conf.MetricsAddr)
pMon := kf.NewpCheck(conf.MaxNFReStartCount, conf.BpfChainingEnabled, conf.KFPollInterval)
kfM := kf.NewpKFMetrics(conf.BpfChainingEnabled, conf.NMetricSamples)
nfConfigs, err := kf.NewNFConfigs(ctx, machineHostname, conf, pMon, kfM)
if err != nil {
return nil, fmt.Errorf("error in NewNFConfigs setup: %v", err)
}
if err := apis.StartConfigWatcher(ctx, machineHostname, daemonName, conf, nfConfigs); err != nil {
return nil, fmt.Errorf("error in version announcer: %v", err)
}
return nfConfigs, nil
}
func checkKernelVersion(conf *config.Config) error {
const minVerLen = 2
kernelVersion, err := getKernelVersion()
if err != nil {
return fmt.Errorf("failed to find kernel version: %v", err)
}
//validate version
ver := strings.Split(kernelVersion, ".")
if len(ver) < minVerLen {
return fmt.Errorf("expected minimum kernel version length %d and got %d, ver %+q", minVerLen, len(ver), ver)
}
major_ver, err := strconv.Atoi(ver[0])
if err != nil {
return fmt.Errorf("failed to find kernel major version: %v", err)
}
minor_ver, err := strconv.Atoi(ver[1])
if err != nil {
return fmt.Errorf("failed to find kernel minor version: %v", err)
}
if major_ver > conf.MinKernelMajorVer {
return nil
}
if major_ver == conf.MinKernelMajorVer && minor_ver >= conf.MinKernelMinorVer {
return nil
}
return fmt.Errorf("expected Kernel version >= %d.%d", conf.MinKernelMajorVer, conf.MinKernelMinorVer)
}
func getKernelVersion() (string, error) {
osVersion, err := ioutil.ReadFile("/proc/version")
if err != nil {
return "", fmt.Errorf("failed to read procfs: %v", err)
}
var u1, u2, kernelVersion string
_, err = fmt.Sscanf(string(osVersion), "%s %s %s", &u1, &u2, &kernelVersion)
if err != nil {
return "", fmt.Errorf("failed to scan procfs version: %v", err)
}
return kernelVersion, nil
}
func ReadConfigsFromConfigStore(conf *config.Config) ([]models.L3afBPFPrograms, error) {
// check for persistent file
if _, err := os.Stat(conf.L3afConfigStoreFileName); errors.Is(err, os.ErrNotExist) {
log.Warn().Msgf("no persistent config exists")
return nil, nil
}
file, err := os.OpenFile(conf.L3afConfigStoreFileName, os.O_RDONLY, os.ModePerm)
defer func() {
_ = file.Close()
}()
if err != nil {
return nil, fmt.Errorf("failed to open persistent file (%s): %v", conf.L3afConfigStoreFileName, err)
}
byteValue, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("failed to read persistent file (%s): %v", conf.L3afConfigStoreFileName, err)
}
var t []models.L3afBPFPrograms
if err = json.Unmarshal(byteValue, &t); err != nil {
return nil, fmt.Errorf("failed to unmarshal persistent config json: %v", err)
}
return t, nil
}