-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
169 lines (148 loc) · 5.19 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
/*
MIT License
Copyright (c) 2020 The metal-stack Authors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// go-lldpd acts as a send only lldp daemon which is installed on every bare metal machine
// to send required information to the networking backplane like uuid of the machine
// and installation timestamp
package main
import (
"fmt"
"log/slog"
"os"
"path"
"path/filepath"
"syscall"
"time"
"gopkg.in/yaml.v3"
"github.com/metal-stack/go-lldpd/pkg/lldp"
"github.com/metal-stack/v"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)
type installerConfig struct {
// MachineUUID is the unique UUID for this machine, usually the board serial.
MachineUUID string `yaml:"machineuuid"`
// Timestamp is the the timestamp of installer config creation.
Timestamp string `yaml:"timestamp"`
}
const (
debugFs = "/sys/kernel/debug"
installYaml = "/etc/metal/install.yaml"
)
// Starts lldp on every ethernet nic that is up
func main() {
jsonHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{})
log := slog.New(jsonHandler)
log.Info("lldpd", "version", v.V)
b, err := os.ReadFile(installYaml)
if err != nil {
log.Error("lldpd", "unable to open config", err)
os.Exit(1)
}
i := &installerConfig{}
err = yaml.Unmarshal(b, &i)
if err != nil {
log.Error("lldpd", "unable to parse config", err)
os.Exit(1)
}
stopFirmwareLLDP(log)
var interfaces []string
links, _ := netlink.LinkList()
for _, nic := range links {
if nic.Type() == "device" && nic.Attrs().EncapType == "ether" {
name := nic.Attrs().Name
if nic.Attrs().OperState == netlink.OperUp {
interfaces = append(interfaces, name)
} else {
log.Info("interface is not up, will ignore it", "interface", name)
}
}
}
if len(interfaces) < 2 {
log.Info("exiting, because not enough interfaces are up - we need at least two")
return
}
log.Info("will start lldp on interfaces", "interfaces", interfaces)
desc := fmt.Sprintf("provisioned since %s", i.Timestamp)
for _, iface := range interfaces {
lldpd, err := lldp.NewDaemon(log, i.MachineUUID, desc, iface, 2*time.Second)
if err != nil {
log.Error("could not start lldp for interface", "interface", iface)
os.Exit(-1)
}
lldpd.Start()
}
select {}
}
func unmountDebugFs(log *slog.Logger) {
log.Info("unmounting debugfs")
err := syscall.Unmount(debugFs, syscall.MNT_FORCE)
if err != nil {
log.Error("unable to unmount debugfs", "error", err)
}
}
// stopFirmwareLLDP stop Firmware LLDP not persistent over reboots, only during runtime.
// mount -t debugfs none /sys/kernel/debug
// echo lldp stop > /sys/kernel/debug/i40e/0000:01:00.2/command
// where <0000:01:00.2> is the pci address of the ethernet nic, this can be inspected by lspci,
// or a loop over all directories in /sys/kernel/debug/i40e/*/command
func stopFirmwareLLDP(log *slog.Logger) {
var stat syscall.Statfs_t
err := syscall.Statfs(debugFs, &stat)
if err != nil {
log.Error("could not check whether debugfs is mounted", "error", err)
return
}
if stat.Type != unix.DEBUGFS_MAGIC {
log.Info("mounting debugfs")
err := syscall.Mount("debugfs", debugFs, "debugfs", 0, "")
if err != nil {
log.Error("mounting debugfs failed", "error", err)
return
}
defer unmountDebugFs(log)
}
var buggyIntelNicDriverNames = []string{"i40e"}
for _, driver := range buggyIntelNicDriverNames {
debugFSPath := path.Join(debugFs, driver)
log.Info("check whether lldp needs to be deactivated", "path", debugFSPath)
if _, err := os.Stat(debugFSPath); os.IsNotExist(err) {
log.Info("nothing to do here, because directory for driver does not exist", "path", debugFSPath)
continue
}
err := filepath.Walk(debugFSPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Warn("opening/reading debugfs failed", "path", path, "error", err)
return err
}
if !info.IsDir() && info.Name() == "command" {
log.Info("execute echo lldp stop > ", "path", path)
stopCommand := []byte("lldp stop")
err := os.WriteFile(path, stopCommand, os.ModePerm) // nolint:gosec
if err != nil {
log.Error("stop lldp > command failed", "path", path, "error", err)
}
}
return nil
})
if err != nil {
log.Error("unable to walk through debugfs", "path", debugFSPath, "error", err)
}
}
}