-
Notifications
You must be signed in to change notification settings - Fork 443
/
ovs.go
253 lines (220 loc) · 7.48 KB
/
ovs.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package daemon
import (
"fmt"
"strings"
"time"
goping "github.com/prometheus-community/pro-bing"
"k8s.io/klog/v2"
"github.com/kubeovn/kube-ovn/pkg/ovs"
"github.com/kubeovn/kube-ovn/pkg/util"
)
const gatewayCheckMaxRetry = 200
func pingGateway(gw, src string, verbose bool, maxRetry int) (count int, err error) {
pinger, err := goping.NewPinger(gw)
if err != nil {
return 0, fmt.Errorf("failed to init pinger: %v", err)
}
pinger.SetPrivileged(true)
// CNITimeoutSec = 220, cannot exceed
pinger.Count = maxRetry
pinger.Timeout = time.Duration(maxRetry) * time.Second
pinger.Interval = time.Second
pinger.OnRecv = func(_ *goping.Packet) {
pinger.Stop()
}
pinger.OnSend = func(_ *goping.Packet) {
if pinger.PacketsRecv == 0 && pinger.PacketsSent != 0 && pinger.PacketsSent%3 == 0 {
klog.Warningf("%s network not ready after %d ping to gateway %s", src, pinger.PacketsSent, gw)
}
}
if err = pinger.Run(); err != nil {
klog.Errorf("failed to run pinger for destination %s: %v", gw, err)
return 0, err
}
if pinger.PacketsRecv == 0 {
klog.Warningf("%s network not ready after %d ping, gw %s", src, pinger.PacketsSent, gw)
return pinger.PacketsSent, fmt.Errorf("no packets received from gateway %s", gw)
}
cniConnectivityResult.WithLabelValues(nodeName).Add(float64(pinger.PacketsSent))
if verbose {
klog.Infof("%s network ready after %d ping, gw %s", src, pinger.PacketsSent, gw)
}
return pinger.PacketsSent, nil
}
func configureGlobalMirror(portName string, mtu int) error {
nicExist, err := linkExists(portName)
if err != nil {
return err
}
if !nicExist {
raw, err := ovs.Exec(ovs.MayExist, "add-port", "br-int", portName, "--",
"set", "interface", portName, "type=internal", "--",
"clear", "bridge", "br-int", "mirrors", "--",
"--id=@mirror0", "get", "port", portName, "--",
"--id=@m", "create", "mirror", fmt.Sprintf("name=%s", util.MirrorDefaultName), "select_all=true", "output_port=@mirror0", "--",
"add", "bridge", "br-int", "mirrors", "@m")
if err != nil {
klog.Errorf("failed to configure mirror nic %s %q", portName, raw)
return fmt.Errorf(raw)
}
} else {
raw, err := ovs.Exec(ovs.MayExist, "add-port", "br-int", portName, "--",
"clear", "bridge", "br-int", "mirrors", "--",
"--id=@mirror0", "get", "port", portName, "--",
"--id=@m", "create", "mirror", fmt.Sprintf("name=%s", util.MirrorDefaultName), "select_all=true", "output_port=@mirror0", "--",
"add", "bridge", "br-int", "mirrors", "@m")
if err != nil {
klog.Errorf("failed to configure mirror nic %s %q", portName, raw)
return fmt.Errorf(raw)
}
}
return configureMirrorLink(portName, mtu)
}
func configureEmptyMirror(portName string, mtu int) error {
nicExist, err := linkExists(portName)
if err != nil {
return err
}
if !nicExist {
raw, err := ovs.Exec(ovs.MayExist, "add-port", "br-int", portName, "--",
"set", "interface", portName, "type=internal", "--",
"clear", "bridge", "br-int", "mirrors", "--",
"--id=@mirror0", "get", "port", portName, "--",
"--id=@m", "create", "mirror", fmt.Sprintf("name=%s", util.MirrorDefaultName), "output_port=@mirror0", "--",
"add", "bridge", "br-int", "mirrors", "@m")
if err != nil {
klog.Errorf("failed to configure mirror nic %s %q", portName, raw)
return fmt.Errorf(raw)
}
} else {
raw, err := ovs.Exec(ovs.MayExist, "add-port", "br-int", portName, "--",
"clear", "bridge", "br-int", "mirrors", "--",
"--id=@mirror0", "get", "port", portName, "--",
"--id=@m", "create", "mirror", fmt.Sprintf("name=%s", util.MirrorDefaultName), "output_port=@mirror0", "--",
"add", "bridge", "br-int", "mirrors", "@m")
if err != nil {
klog.Errorf("failed to configure mirror nic %s %q", portName, raw)
return fmt.Errorf(raw)
}
}
return configureMirrorLink(portName, mtu)
}
func decodeOvnMappings(s string) map[string]string {
if len(s) == 0 {
return map[string]string{}
}
fields := strings.Split(s, ",")
mappings := make(map[string]string, len(fields)+1)
for _, f := range fields {
idx := strings.IndexRune(f, ':')
if idx <= 0 || idx == len(f)-1 {
klog.Warningf("invalid mapping entry: %s", f)
continue
}
mappings[f[:idx]] = f[idx+1:]
}
return mappings
}
func encodeOvnMappings(mappings map[string]string) string {
if len(mappings) == 0 {
return ""
}
fields := make([]string, 0, len(mappings))
for k, v := range mappings {
fields = append(fields, fmt.Sprintf("%s:%v", k, v))
}
return strings.Join(fields, ",")
}
func getOvnMappings(name string) (map[string]string, error) {
output, err := ovs.Exec(ovs.IfExists, "get", "open", ".", "external-ids:"+name)
if err != nil {
return nil, fmt.Errorf("failed to get %s, %v: %q", name, err, output)
}
return decodeOvnMappings(output), nil
}
func setOvnMappings(name string, mappings map[string]string) error {
var err error
var output string
if s := encodeOvnMappings(mappings); len(s) == 0 {
output, err = ovs.Exec(ovs.IfExists, "remove", "open", ".", "external-ids", name)
} else {
output, err = ovs.Exec("set", "open", ".", fmt.Sprintf("external-ids:%s=%s", name, s))
}
if err != nil {
return fmt.Errorf("failed to set %s, %v: %q", name, err, output)
}
return nil
}
func addOvnMapping(name, key, value string, overwrite bool) error {
mappings, err := getOvnMappings(name)
if err != nil {
return err
}
if mappings[key] == value || (mappings[key] != "" && !overwrite) {
return nil
}
mappings[key] = value
return setOvnMappings(name, mappings)
}
func removeOvnMapping(name, key string) error {
mappings, err := getOvnMappings(name)
if err != nil {
return err
}
length := len(mappings)
delete(mappings, key)
if len(mappings) == length {
return nil
}
return setOvnMappings(name, mappings)
}
func (c *Controller) configExternalBridge(provider, bridge, nic string, exchangeLinkName, macLearningFallback bool) error {
brExists, err := ovs.BridgeExists(bridge)
if err != nil {
return fmt.Errorf("failed to check OVS bridge existence: %v", err)
}
cmd := []string{
ovs.MayExist, "add-br", bridge,
"--", "set", "bridge", bridge, fmt.Sprintf("other_config:mac-learning-fallback=%v", macLearningFallback),
"--", "set", "bridge", bridge, "external_ids:vendor=" + util.CniTypeName,
"--", "set", "bridge", bridge, fmt.Sprintf("external_ids:exchange-link-name=%v", exchangeLinkName),
}
if !brExists {
// assign a new generated mac address only when the bridge is newly created
cmd = append(cmd, "--", "set", "bridge", bridge, fmt.Sprintf(`other-config:hwaddr="%s"`, util.GenerateMac()))
}
output, err := ovs.Exec(cmd...)
if err != nil {
return fmt.Errorf("failed to create OVS bridge %s, %v: %q", bridge, err, output)
}
if output, err = ovs.Exec("list-ports", bridge); err != nil {
return fmt.Errorf("failed to list ports of OVS bridge %s, %v: %q", bridge, err, output)
}
if output != "" {
for _, port := range strings.Split(output, "\n") {
if port != nic {
ok, err := ovs.ValidatePortVendor(port)
if err != nil {
return fmt.Errorf("failed to check vendor of port %s: %v", port, err)
}
if ok {
if err = c.removeProviderNic(port, bridge); err != nil {
return fmt.Errorf("failed to remove port %s from OVS bridge %s: %v", port, bridge, err)
}
}
}
}
}
if err = addOvnMapping("ovn-bridge-mappings", provider, bridge, true); err != nil {
klog.Error(err)
return err
}
return nil
}
func initProviderChassisMac(provider string) error {
if err := addOvnMapping("ovn-chassis-mac-mappings", provider, util.GenerateMac(), false); err != nil {
klog.Error(err)
return err
}
return nil
}