-
Notifications
You must be signed in to change notification settings - Fork 438
/
ovs-vsctl.go
346 lines (316 loc) · 10.5 KB
/
ovs-vsctl.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package ovs
import (
"fmt"
"os/exec"
"strconv"
"strings"
"time"
"github.com/kubeovn/kube-ovn/pkg/util"
"k8s.io/klog/v2"
)
// Glory belongs to openvswitch/ovn-kubernetes
// https://github.com/openvswitch/ovn-kubernetes/blob/master/go-controller/pkg/util/ovs.go
func Exec(args ...string) (string, error) {
start := time.Now()
args = append([]string{"--timeout=30"}, args...)
output, err := exec.Command(OvsVsCtl, args...).CombinedOutput()
elapsed := float64((time.Since(start)) / time.Millisecond)
klog.V(4).Infof("command %s %s in %vms", OvsVsCtl, strings.Join(args, " "), elapsed)
method := ""
for _, arg := range args {
if !strings.HasPrefix(arg, "--") {
method = arg
break
}
}
code := "0"
defer func() {
ovsClientRequestLatency.WithLabelValues("ovsdb", method, code).Observe(elapsed)
}()
if err != nil {
code = "1"
klog.Warningf("ovs-vsctl command error: %s %s in %vms", OvsVsCtl, strings.Join(args, " "), elapsed)
return "", fmt.Errorf("failed to run '%s %s': %v\n %q", OvsVsCtl, strings.Join(args, " "), err, output)
} else if elapsed > 500 {
klog.Warningf("ovs-vsctl command took too long: %s %s in %vms", OvsVsCtl, strings.Join(args, " "), elapsed)
}
return trimCommandOutput(output), nil
}
func ovsCreate(table string, values ...string) (string, error) {
args := append([]string{"create", table}, values...)
return Exec(args...)
}
func ovsDestroy(table, record string) error {
_, err := Exec("--if-exists", "destroy", table, record)
return err
}
func ovsSet(table, record string, values ...string) error {
args := append([]string{"set", table, record}, values...)
_, err := Exec(args...)
return err
}
func ovsAdd(table, record string, column string, values ...string) error {
args := append([]string{"add", table, record, column}, values...)
_, err := Exec(args...)
return err
}
// Returns the given column of records that match the condition
func ovsFind(table, column, condition string) ([]string, error) {
output, err := Exec("--no-heading", "--columns="+column, "find", table, condition)
if err != nil {
return nil, err
}
values := strings.Split(output, "\n\n")
// We want "bare" values for strings, but we can't pass --bare to ovs-vsctl because
// it breaks more complicated types. So try passing each value through Unquote();
// if it fails, that means the value wasn't a quoted string, so use it as-is.
for i, val := range values {
if unquoted, err := strconv.Unquote(val); err == nil {
values[i] = unquoted
}
}
ret := make([]string, 0, len(values))
for _, val := range values {
if strings.TrimSpace(val) != "" {
ret = append(ret, strings.Trim(strings.TrimSpace(val), "\""))
}
}
return ret, nil
}
func ovsClear(table, record string, columns ...string) error {
args := append([]string{"--if-exists", "clear", table, record}, columns...)
_, err := Exec(args...)
return err
}
// Bridges returns bridges created by Kube-OVN
func Bridges() ([]string, error) {
return ovsFind("bridge", "name", fmt.Sprintf("external-ids:vendor=%s", util.CniTypeName))
}
// BridgeExists checks whether the bridge already exists
func BridgeExists(name string) (bool, error) {
bridges, err := Bridges()
if err != nil {
return false, err
}
return util.ContainsString(bridges, name), nil
}
// ClearPodBandwidth remove qos related to this pod. Only used when remove pod.
func ClearPodBandwidth(podName, podNamespace string) error {
qosList, err := ovsFind("qos", "_uuid", fmt.Sprintf(`external-ids:iface-id="%s.%s"`, podName, podNamespace))
if err != nil {
return err
}
qosListByPod, err := ovsFind("qos", "_uuid", fmt.Sprintf(`external-ids:pod="%s/%s"`, podNamespace, podName))
if err != nil {
return err
}
qosList = append(qosList, qosListByPod...)
qosList = util.UniqString(qosList)
// https://github.com/kubeovn/kube-ovn/issues/1191
usedQosList, err := ovsFind("port", "qos", "qos!=[]")
if err != nil {
return err
}
for _, qosId := range qosList {
found := false
for _, usedQosId := range usedQosList {
if qosId == usedQosId {
found = true
break
}
}
if found {
continue
}
if err := ovsDestroy("qos", qosId); err != nil {
return err
}
}
return nil
}
// SetInterfaceBandwidth set ingress/egress qos for given pod, annotation values are for node/pod
// but ingress/egress parameters here are from the point of ovs port/interface view, so reverse input parameters when call func SetInterfaceBandwidth
func SetInterfaceBandwidth(podName, podNamespace, iface, ingress, egress string) error {
ingressMPS, _ := strconv.Atoi(ingress)
ingressKPS := ingressMPS * 1000
interfaceList, err := ovsFind("interface", "name", fmt.Sprintf("external-ids:iface-id=%s", iface))
if err != nil {
return err
}
for _, ifName := range interfaceList {
// ingress_policing_rate is in Kbps
err := ovsSet("interface", ifName, fmt.Sprintf("ingress_policing_rate=%d", ingressKPS), fmt.Sprintf("ingress_policing_burst=%d", ingressKPS*8/10))
if err != nil {
return err
}
egressMPS, _ := strconv.Atoi(egress)
egressBPS := egressMPS * 1000 * 1000
qosList, err := ovsFind("qos", "_uuid", fmt.Sprintf("external-ids:iface-id=%s", iface))
if err != nil {
return err
}
if egressBPS > 0 {
if len(qosList) == 0 {
qosCommandValues := []string{"type=linux-htb", fmt.Sprintf("other-config:max-rate=%d", egressBPS), fmt.Sprintf("external-ids:iface-id=%s", iface)}
if podNamespace != "" && podName != "" {
qosCommandValues = append(qosCommandValues, fmt.Sprintf("external-ids:pod=%s/%s", podNamespace, podName))
}
qos, err := ovsCreate("qos", qosCommandValues...)
if err != nil {
return err
}
err = ovsSet("port", ifName, fmt.Sprintf("qos=%s", qos))
if err != nil {
return err
}
} else {
for _, qos := range qosList {
klog.Infof("qos %s already exists", qos)
if err := ovsSet("qos", qos, fmt.Sprintf("other-config:max-rate=%d", egressBPS)); err != nil {
return err
}
}
}
} else {
if err = ovsClear("port", ifName, "qos"); err != nil {
return err
}
for _, qos := range qosList {
if err := ovsDestroy("qos", qos); err != nil {
return err
}
}
}
}
return nil
}
// CleanLostInterface will clean up related ovs port, interface and qos
// When reboot node, the ovs internal interface will be deleted.
func CleanLostInterface() {
// when interface error ofport will be -1
interfaceList, err := ovsFind("interface", "name,error", "ofport=-1")
if err != nil {
klog.Errorf("failed to list failed interface %v", err)
return
}
if len(interfaceList) > 0 {
klog.Infof("error interfaces:\n %v", interfaceList)
}
for _, intf := range interfaceList {
name, errText := strings.Trim(strings.Split(intf, "\n")[0], "\""), strings.Split(intf, "\n")[1]
if strings.Contains(errText, "No such device") {
qosList, err := ovsFind("port", "qos", fmt.Sprintf("name=%s", name))
if err != nil {
klog.Errorf("failed to find related port %v", err)
return
}
klog.Infof("delete lost port %s", name)
output, err := Exec("--if-exists", "--with-iface", "del-port", "br-int", name)
if err != nil {
klog.Errorf("failed to delete ovs port %v, %s", err, output)
return
}
for _, qos := range qosList {
qos = strings.TrimSpace(qos)
if qos != "" && qos != "[]" {
klog.Infof("delete lost qos %s", qos)
err = ovsDestroy("qos", qos)
if err != nil {
klog.Errorf("failed to delete qos %s, %v", qos, err)
return
}
}
}
}
}
}
// Find and remove any existing OVS port with this iface-id. Pods can
// have multiple sandboxes if some are waiting for garbage collection,
// but only the latest one should have the iface-id set.
// See: https://github.com/ovn-org/ovn-kubernetes/pull/869
func CleanDuplicatePort(ifaceID string) {
uuids, _ := ovsFind("Interface", "_uuid", "external-ids:iface-id="+ifaceID)
for _, uuid := range uuids {
if out, err := Exec("remove", "Interface", uuid, "external-ids", "iface-id"); err != nil {
klog.Errorf("failed to clear stale OVS port %q iface-id %q: %v\n %q", uuid, ifaceID, err, out)
}
}
}
func SetPortTag(port, tag string) error {
return ovsSet("port", port, fmt.Sprintf("tag=%s", tag))
}
// ValidatePortVendor returns true if the port's external_ids:vendor=kube-ovn
func ValidatePortVendor(port string) (bool, error) {
output, err := ovsFind("Port", "name", "external_ids:vendor="+util.CniTypeName)
return util.ContainsString(output, port), err
}
//config mirror for interface by pod annotations and install param
func ConfigInterfaceMirror(globalMirror bool, open string, iface string) error {
if !globalMirror {
//find interface name for port
interfaceList, err := ovsFind("interface", "name", fmt.Sprintf("external-ids:iface-id=%s", iface))
if err != nil {
return err
}
for _, ifName := range interfaceList {
//ifName example: xxx_h
//find port uuid by interface name
portUUIDs, err := ovsFind("port", "_uuid", fmt.Sprintf("name=%s", ifName))
if err != nil {
return err
}
if len(portUUIDs) != 1 {
return fmt.Errorf(fmt.Sprintf("find port failed, portName=%s", ifName))
}
portId := portUUIDs[0]
if open == "true" {
//add port to mirror
err = ovsAdd("mirror", util.MirrorDefaultName, "select_dst_port", portId)
if err != nil {
return err
}
} else {
mirrorPorts, err := ovsFind("mirror", "select_dst_port", fmt.Sprintf("name=%s", util.MirrorDefaultName))
if err != nil {
return err
}
if len(mirrorPorts) == 0 {
return fmt.Errorf("find mirror failed, mirror name=" + util.MirrorDefaultName)
}
if len(mirrorPorts) > 1 {
return fmt.Errorf("repeated mirror data, mirror name=" + util.MirrorDefaultName)
}
for _, mirrorPortIds := range mirrorPorts {
if strings.Contains(mirrorPortIds, portId) {
//remove port from mirror
_, err := Exec("remove", "mirror", util.MirrorDefaultName, "select_dst_port", portId)
if err != nil {
return err
}
}
}
}
}
}
return nil
}
func GetResidualInternalPorts() []string {
residualPorts := make([]string, 0)
interfaceList, err := ovsFind("interface", "name,external_ids", "type=internal")
if err != nil {
klog.Errorf("failed to list ovs internal interface %v", err)
return residualPorts
}
for _, intf := range interfaceList {
name := strings.Trim(strings.Split(intf, "\n")[0], "\"")
if !strings.Contains(name, "_c") {
continue
}
// iface-id field does not exist in external_ids for residual internal port
externaIds := strings.Split(intf, "\n")[1]
if !strings.Contains(externaIds, "iface-id") {
residualPorts = append(residualPorts, name)
}
}
return residualPorts
}