-
Notifications
You must be signed in to change notification settings - Fork 332
/
ovs.go
872 lines (765 loc) · 28.8 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
package util
import (
"bytes"
"encoding/json"
"fmt"
"regexp"
"runtime"
"strings"
"sync/atomic"
"time"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/config"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/types"
"github.com/spf13/afero"
"k8s.io/klog/v2"
kexec "k8s.io/utils/exec"
)
const (
// On Windows we need an increased timeout on OVS commands, because
// adding internal ports on a non Hyper-V enabled host will call
// external Powershell commandlets.
// TODO: Decrease the timeout once port adding is improved on Windows
ovsCommandTimeout = 15
ovsVsctlCommand = "ovs-vsctl"
ovsOfctlCommand = "ovs-ofctl"
ovsAppctlCommand = "ovs-appctl"
ovnNbctlCommand = "ovn-nbctl"
ovnSbctlCommand = "ovn-sbctl"
ovnAppctlCommand = "ovn-appctl"
ovsdbClientCommand = "ovsdb-client"
ovsdbToolCommand = "ovsdb-tool"
ipCommand = "ip"
powershellCommand = "powershell"
netshCommand = "netsh"
routeCommand = "route"
sysctlCommand = "sysctl"
osRelease = "/etc/os-release"
rhel = "RHEL"
ubuntu = "Ubuntu"
windowsOS = "windows"
)
const (
nbdbCtlFileName = "ovnnb_db.ctl"
sbdbCtlFileName = "ovnsb_db.ctl"
OvnNbdbLocation = "/etc/ovn/ovnnb_db.db"
OvnSbdbLocation = "/etc/ovn/ovnsb_db.db"
FloodAction = "FLOOD"
NormalAction = "NORMAL"
)
var (
// These are variables (not constants) so that testcases can modify them
ovsRunDir string = "/var/run/openvswitch/"
ovnRunDir string = "/var/run/ovn/"
savedOVSRunDir = ovsRunDir
savedOVNRunDir = ovnRunDir
)
var ovnCmdRetryCount = 200
var AppFs = afero.NewOsFs()
// PrepareTestConfig restores default config values. Used by testcases to
// provide a pristine environment between tests.
func PrepareTestConfig() {
ovsRunDir = savedOVSRunDir
ovnRunDir = savedOVNRunDir
}
func runningPlatform() (string, error) {
if runtime.GOOS == windowsOS {
return windowsOS, nil
}
fileContents, err := afero.ReadFile(AppFs, osRelease)
if err != nil {
return "", fmt.Errorf("failed to parse file %s (%v)", osRelease, err)
}
var platform string
ss := strings.Split(string(fileContents), "\n")
for _, pair := range ss {
keyValue := strings.Split(pair, "=")
if len(keyValue) == 2 {
if keyValue[0] == "Name" || keyValue[0] == "NAME" {
platform = keyValue[1]
break
}
}
}
if platform == "" {
return "", fmt.Errorf("failed to find the platform name")
}
if strings.Contains(platform, "Fedora") ||
strings.Contains(platform, "Red Hat") || strings.Contains(platform, "CentOS") {
return rhel, nil
} else if strings.Contains(platform, "Debian") ||
strings.Contains(platform, ubuntu) {
return ubuntu, nil
} else if strings.Contains(platform, "VMware") {
return "Photon", nil
}
return "", fmt.Errorf("unknown platform")
}
// Exec runs various OVN and OVS utilities
type execHelper struct {
exec kexec.Interface
ofctlPath string
vsctlPath string
appctlPath string
ovnappctlPath string
nbctlPath string
sbctlPath string
ovnctlPath string
ovsdbClientPath string
ovsdbToolPath string
ovnRunDir string
ipPath string
powershellPath string
netshPath string
routePath string
sysctlPath string
}
var runner *execHelper
type ExecRunner interface {
RunCmd(cmd kexec.Cmd, cmdPath string, envVars []string, args ...string) (*bytes.Buffer, *bytes.Buffer, error)
}
// defaultExecRunner implements the methods defined in the ExecRunner interface
type defaultExecRunner struct {
}
// RunCmd invokes the methods of the Cmd interfaces defined in k8s.io/utils/exec to execute commands
// Note: the cmdPath and args parameter are used only for logging and is not processed
func (runsvc *defaultExecRunner) RunCmd(cmd kexec.Cmd, cmdPath string, envVars []string, args ...string) (*bytes.Buffer, *bytes.Buffer, error) {
if cmd == nil {
return &bytes.Buffer{}, &bytes.Buffer{}, fmt.Errorf("cmd object cannot be nil")
}
if len(envVars) != 0 {
cmd.SetEnv(envVars)
}
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd.SetStdout(stdout)
cmd.SetStderr(stderr)
counter := atomic.AddUint64(&runCounter, 1)
logCmd := fmt.Sprintf("%s %s", cmdPath, strings.Join(args, " "))
klog.V(5).Infof("Exec(%d): %s", counter, logCmd)
err := cmd.Run()
klog.V(5).Infof("Exec(%d): stdout: %q", counter, stdout)
klog.V(5).Infof("Exec(%d): stderr: %q", counter, stderr)
if err != nil {
klog.V(5).Infof("Exec(%d): err: %v", counter, err)
}
return stdout, stderr, err
}
var runCmdExecRunner ExecRunner = &defaultExecRunner{}
// SetExec validates executable paths and saves the given exec interface
// to be used for running various OVS and OVN utilites
func SetExec(exec kexec.Interface) error {
err := SetExecWithoutOVS(exec)
if err != nil {
return err
}
runner.ofctlPath, err = exec.LookPath(ovsOfctlCommand)
if err != nil {
return err
}
runner.vsctlPath, err = exec.LookPath(ovsVsctlCommand)
if err != nil {
return err
}
runner.appctlPath, err = exec.LookPath(ovsAppctlCommand)
if err != nil {
return err
}
runner.ovnappctlPath, err = exec.LookPath(ovnAppctlCommand)
if err != nil {
// If ovn-appctl command is not available then fall back to
// ovs-appctl. It also means OVN is using the rundir of
// openvswitch.
runner.ovnappctlPath = runner.appctlPath
runner.ovnctlPath = "/usr/share/openvswitch/scripts/ovn-ctl"
runner.ovnRunDir = ovsRunDir
} else {
// If ovn-appctl command is available, it means OVN
// has its own separate rundir, logdir, sharedir.
runner.ovnctlPath = "/usr/share/ovn/scripts/ovn-ctl"
runner.ovnRunDir = ovnRunDir
}
runner.nbctlPath, err = exec.LookPath(ovnNbctlCommand)
if err != nil {
return err
}
runner.sbctlPath, err = exec.LookPath(ovnSbctlCommand)
if err != nil {
return err
}
runner.ovsdbClientPath, err = exec.LookPath(ovsdbClientCommand)
if err != nil {
return err
}
runner.ovsdbToolPath, err = exec.LookPath(ovsdbToolCommand)
if err != nil {
return err
}
return nil
}
// SetExecWithoutOVS validates executable paths excluding OVS/OVN binaries and
// saves the given exec interface to be used for running various utilites
func SetExecWithoutOVS(exec kexec.Interface) error {
var err error
runner = &execHelper{exec: exec}
if runtime.GOOS == windowsOS {
runner.powershellPath, err = exec.LookPath(powershellCommand)
if err != nil {
return err
}
runner.netshPath, err = exec.LookPath(netshCommand)
if err != nil {
return err
}
runner.routePath, err = exec.LookPath(routeCommand)
if err != nil {
return err
}
} else {
runner.ipPath, err = exec.LookPath(ipCommand)
if err != nil {
return err
}
runner.sysctlPath, err = exec.LookPath(sysctlCommand)
if err != nil {
return err
}
}
return nil
}
// SetSpecificExec validates executable paths for selected commands. It also saves the given
// exec interface to be used for running selected commands
func SetSpecificExec(exec kexec.Interface, commands ...string) error {
var err error
runner = &execHelper{exec: exec}
for _, command := range commands {
switch command {
case ovsVsctlCommand:
runner.vsctlPath, err = exec.LookPath(ovsVsctlCommand)
if err != nil {
return err
}
default:
return fmt.Errorf("unknown command: %q", command)
}
}
return nil
}
// GetExec returns the exec interface which can be used for running commands directly.
// Only use for passing an exec interface into pkg/config which cannot call this
// function directly because this module imports pkg/config already.
func GetExec() kexec.Interface {
return runner.exec
}
// ResetRunner used by unit-tests to reset runner to its initial (un-initialized) value
func ResetRunner() {
runner = nil
}
var runCounter uint64
func runCmd(cmd kexec.Cmd, cmdPath string, args ...string) (*bytes.Buffer, *bytes.Buffer, error) {
return runCmdExecRunner.RunCmd(cmd, cmdPath, []string{}, args...)
}
func run(cmdPath string, args ...string) (*bytes.Buffer, *bytes.Buffer, error) {
cmd := runner.exec.Command(cmdPath, args...)
return runCmdExecRunner.RunCmd(cmd, cmdPath, []string{}, args...)
}
func runWithEnvVars(cmdPath string, envVars []string, args ...string) (*bytes.Buffer, *bytes.Buffer, error) {
cmd := runner.exec.Command(cmdPath, args...)
return runCmdExecRunner.RunCmd(cmd, cmdPath, envVars, args...)
}
// RunOVSOfctl runs a command via ovs-ofctl.
func RunOVSOfctl(args ...string) (string, string, error) {
stdout, stderr, err := run(runner.ofctlPath, args...)
return strings.Trim(stdout.String(), "\" \n"), stderr.String(), err
}
// RunOVSVsctl runs a command via ovs-vsctl.
func RunOVSVsctl(args ...string) (string, string, error) {
cmdArgs := []string{fmt.Sprintf("--timeout=%d", ovsCommandTimeout)}
cmdArgs = append(cmdArgs, args...)
stdout, stderr, err := run(runner.vsctlPath, cmdArgs...)
return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
}
// GetOVSOfPort runs get ofport via ovs-vsctl and handle special return strings.
func GetOVSOfPort(args ...string) (string, string, error) {
stdout, stderr, err := RunOVSVsctl(args...)
if stdout == "[]" || stdout == "-1" {
err = fmt.Errorf("%s return invalid result %s err %s", args, stdout, err)
}
return stdout, stderr, err
}
// RunOVSAppctlWithTimeout runs a command via ovs-appctl.
func RunOVSAppctlWithTimeout(timeout int, args ...string) (string, string, error) {
cmdArgs := []string{fmt.Sprintf("--timeout=%d", timeout)}
cmdArgs = append(cmdArgs, args...)
stdout, stderr, err := run(runner.appctlPath, cmdArgs...)
return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
}
// RunOVSAppctl runs a command via ovs-appctl.
func RunOVSAppctl(args ...string) (string, string, error) {
return RunOVSAppctlWithTimeout(ovsCommandTimeout, args...)
}
// RunOVNAppctlWithTimeout runs a command via ovn-appctl. If ovn-appctl is not present, then it
// falls back to using ovs-appctl.
func RunOVNAppctlWithTimeout(timeout int, args ...string) (string, string, error) {
cmdArgs := []string{fmt.Sprintf("--timeout=%d", timeout)}
cmdArgs = append(cmdArgs, args...)
stdout, stderr, err := run(runner.ovnappctlPath, cmdArgs...)
return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
}
// Run the ovn-ctl command and retry if "Connection refused"
// poll waitng for service to become available
// FIXME: Remove when https://github.com/ovn-org/libovsdb/issues/235 is fixed
func runOVNretry(cmdPath string, envVars []string, args ...string) (*bytes.Buffer, *bytes.Buffer, error) {
retriesLeft := ovnCmdRetryCount
for {
stdout, stderr, err := runWithEnvVars(cmdPath, envVars, args...)
if err == nil {
return stdout, stderr, err
}
// Connection refused
// Master may not be up so keep trying
if strings.Contains(stderr.String(), "Connection refused") {
if retriesLeft == 0 {
return stdout, stderr, err
}
retriesLeft--
time.Sleep(2 * time.Second)
} else {
// Some other problem for caller to handle
return stdout, stderr, fmt.Errorf("OVN command '%s %s' failed: %s", cmdPath, strings.Join(args, " "), err)
}
}
}
func getNbctlArgsAndEnv(timeout int, args ...string) ([]string, []string) {
var cmdArgs []string
if config.OvnNorth.Scheme == config.OvnDBSchemeSSL {
cmdArgs = append(cmdArgs,
fmt.Sprintf("--private-key=%s", config.OvnNorth.PrivKey),
fmt.Sprintf("--certificate=%s", config.OvnNorth.Cert),
fmt.Sprintf("--bootstrap-ca-cert=%s", config.OvnNorth.CACert),
fmt.Sprintf("--db=%s", config.OvnNorth.GetURL()))
} else if config.OvnNorth.Scheme == config.OvnDBSchemeTCP {
cmdArgs = append(cmdArgs, fmt.Sprintf("--db=%s", config.OvnNorth.GetURL()))
}
cmdArgs = append(cmdArgs, fmt.Sprintf("--timeout=%d", timeout))
cmdArgs = append(cmdArgs, args...)
return cmdArgs, []string{}
}
func getNbOVSDBArgs(command string, args ...string) []string {
var cmdArgs []string
if config.OvnNorth.Scheme == config.OvnDBSchemeSSL {
cmdArgs = append(cmdArgs,
fmt.Sprintf("--private-key=%s", config.OvnNorth.PrivKey),
fmt.Sprintf("--certificate=%s", config.OvnNorth.Cert),
fmt.Sprintf("--bootstrap-ca-cert=%s", config.OvnNorth.CACert))
}
cmdArgs = append(cmdArgs, command)
cmdArgs = append(cmdArgs, config.OvnNorth.GetURL())
cmdArgs = append(cmdArgs, args...)
return cmdArgs
}
// RunOVNNbctlWithTimeout runs command via ovn-nbctl with a specific timeout
// FIXME: Remove when https://github.com/ovn-org/libovsdb/issues/235 is fixed
func RunOVNNbctlWithTimeout(timeout int, args ...string) (string, string, error) {
stdout, stderr, err := RunOVNNbctlRawOutput(timeout, args...)
return strings.Trim(strings.TrimSpace(stdout), "\""), stderr, err
}
// RunOVNNbctlRawOutput returns the output with no trimming or other string manipulation
// FIXME: Remove when https://github.com/ovn-org/libovsdb/issues/235 is fixed
func RunOVNNbctlRawOutput(timeout int, args ...string) (string, string, error) {
cmdArgs, envVars := getNbctlArgsAndEnv(timeout, args...)
stdout, stderr, err := runOVNretry(runner.nbctlPath, envVars, cmdArgs...)
return stdout.String(), stderr.String(), err
}
// RunOVNNbctl runs a command via ovn-nbctl.
// FIXME: Remove when https://github.com/ovn-org/libovsdb/issues/235 is fixed
func RunOVNNbctl(args ...string) (string, string, error) {
return RunOVNNbctlWithTimeout(ovsCommandTimeout, args...)
}
// RunOVNSbctlWithTimeout runs command via ovn-sbctl with a specific timeout
// FIXME: Remove when https://github.com/ovn-org/libovsdb/issues/235 is fixed
func RunOVNSbctlWithTimeout(timeout int, args ...string) (string, string,
error) {
var cmdArgs []string
if config.OvnSouth.Scheme == config.OvnDBSchemeSSL {
cmdArgs = []string{
fmt.Sprintf("--private-key=%s", config.OvnSouth.PrivKey),
fmt.Sprintf("--certificate=%s", config.OvnSouth.Cert),
fmt.Sprintf("--bootstrap-ca-cert=%s", config.OvnSouth.CACert),
fmt.Sprintf("--db=%s", config.OvnSouth.GetURL()),
}
} else if config.OvnSouth.Scheme == config.OvnDBSchemeTCP {
cmdArgs = []string{
fmt.Sprintf("--db=%s", config.OvnSouth.GetURL()),
}
}
cmdArgs = append(cmdArgs, fmt.Sprintf("--timeout=%d", timeout))
cmdArgs = append(cmdArgs, "--no-leader-only")
cmdArgs = append(cmdArgs, args...)
stdout, stderr, err := runOVNretry(runner.sbctlPath, nil, cmdArgs...)
return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
}
// RunOVSDBClient runs an 'ovsdb-client [OPTIONS] COMMAND [ARG...] command'.
func RunOVSDBClient(args ...string) (string, string, error) {
stdout, stderr, err := runOVNretry(runner.ovsdbClientPath, nil, args...)
return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
}
// RunOVSDBTool runs an 'ovsdb-tool [OPTIONS] COMMAND [ARG...] command'.
func RunOVSDBTool(args ...string) (string, string, error) {
stdout, stderr, err := run(runner.ovsdbToolPath, args...)
return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
}
// RunOVSDBClientOVN runs an 'ovsdb-client [OPTIONS] COMMAND [SERVER] [ARG...] command' against OVN NB database.
func RunOVSDBClientOVNNB(command string, args ...string) (string, string, error) {
cmdArgs := getNbOVSDBArgs(command, args...)
stdout, stderr, err := runOVNretry(runner.ovsdbClientPath, nil, cmdArgs...)
return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
}
// RunOVNSbctl runs a command via ovn-sbctl.
// FIXME: Remove when https://github.com/ovn-org/libovsdb/issues/235 is fixed
func RunOVNSbctl(args ...string) (string, string, error) {
return RunOVNSbctlWithTimeout(ovsCommandTimeout, args...)
}
// RunOVNNBAppCtlWithTimeout runs an ovn-appctl command with a timeout to nbdb
func RunOVNNBAppCtlWithTimeout(timeout int, args ...string) (string, string, error) {
cmdArgs := []string{fmt.Sprintf("--timeout=%d", timeout)}
cmdArgs = append(cmdArgs, args...)
return RunOVNNBAppCtl(cmdArgs...)
}
// RunOVNNBAppCtl runs an 'ovn-appctl -t nbdbCtlFileName command'.
func RunOVNNBAppCtl(args ...string) (string, string, error) {
var cmdArgs []string
cmdArgs = []string{
"-t",
runner.ovnRunDir + nbdbCtlFileName,
}
cmdArgs = append(cmdArgs, args...)
stdout, stderr, err := runOVNretry(runner.ovnappctlPath, nil, cmdArgs...)
return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
}
// RunOVNSBAppCtlWithTimeout runs an ovn-appctl command with a timeout to sbdb
func RunOVNSBAppCtlWithTimeout(timeout int, args ...string) (string, string, error) {
cmdArgs := []string{fmt.Sprintf("--timeout=%d", timeout)}
cmdArgs = append(cmdArgs, args...)
return RunOVNSBAppCtl(cmdArgs...)
}
// RunOVNSBAppCtl runs an 'ovn-appctl -t sbdbCtlFileName command'.
func RunOVNSBAppCtl(args ...string) (string, string, error) {
var cmdArgs []string
cmdArgs = []string{
"-t",
runner.ovnRunDir + sbdbCtlFileName,
}
cmdArgs = append(cmdArgs, args...)
stdout, stderr, err := runOVNretry(runner.ovnappctlPath, nil, cmdArgs...)
return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
}
// RunOVNNorthAppCtl runs an 'ovs-appctl -t ovn-northd command'.
// TODO: Currently no module is invoking this function, will need to consider adding an unit test when actively used
func RunOVNNorthAppCtl(args ...string) (string, string, error) {
var cmdArgs []string
pid, err := afero.ReadFile(AppFs, runner.ovnRunDir+"ovn-northd.pid")
if err != nil {
return "", "", fmt.Errorf("failed to run the command since failed to get ovn-northd's pid: %v", err)
}
cmdArgs = []string{
"-t",
runner.ovnRunDir + fmt.Sprintf("ovn-northd.%s.ctl", strings.TrimSpace(string(pid))),
}
cmdArgs = append(cmdArgs, args...)
stdout, stderr, err := runOVNretry(runner.ovnappctlPath, nil, cmdArgs...)
return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
}
// RunOVNControllerAppCtl runs an 'ovs-appctl -t ovn-controller.pid.ctl command'.
func RunOVNControllerAppCtl(args ...string) (string, string, error) {
var cmdArgs []string
pid, err := afero.ReadFile(AppFs, runner.ovnRunDir+"ovn-controller.pid")
if err != nil {
return "", "", fmt.Errorf("failed to get ovn-controller pid : %v", err)
}
cmdArgs = []string{
"-t",
runner.ovnRunDir + fmt.Sprintf("ovn-controller.%s.ctl", strings.TrimSpace(string(pid))),
}
cmdArgs = append(cmdArgs, args...)
stdout, stderr, err := runOVNretry(runner.ovnappctlPath, nil, cmdArgs...)
return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
}
// RunOvsVswitchdAppCtl runs an 'ovs-appctl -t /var/run/openvsiwthc/ovs-vswitchd.pid.ctl command'
func RunOvsVswitchdAppCtl(args ...string) (string, string, error) {
var cmdArgs []string
pid, err := GetOvsVSwitchdPID()
if err != nil {
return "", "", err
}
cmdArgs = []string{
"-t",
savedOVSRunDir + fmt.Sprintf("ovs-vswitchd.%s.ctl", pid),
}
cmdArgs = append(cmdArgs, args...)
stdout, stderr, err := runOVNretry(runner.appctlPath, nil, cmdArgs...)
return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
}
// GetOvsVSwitchdPID retrieves the Process IDentifier for ovs-vswitchd daemon.
func GetOvsVSwitchdPID() (string, error) {
pid, err := afero.ReadFile(AppFs, savedOVSRunDir+"ovs-vswitchd.pid")
if err != nil {
return "", fmt.Errorf("failed to get ovs-vswitch pid : %v", err)
}
return strings.TrimSpace(string(pid)), nil
}
// GetOvsDBServerPID retrieves the Process IDentifier for ovs-vswitchd daemon.
func GetOvsDBServerPID() (string, error) {
pid, err := afero.ReadFile(AppFs, savedOVSRunDir+"ovsdb-server.pid")
if err != nil {
return "", fmt.Errorf("failed to get ovsdb-server pid : %v", err)
}
return strings.TrimSpace(string(pid)), nil
}
// RunIP runs a command via the iproute2 "ip" utility
func RunIP(args ...string) (string, string, error) {
stdout, stderr, err := run(runner.ipPath, args...)
return strings.TrimSpace(stdout.String()), stderr.String(), err
}
// RunSysctl runs a command via the procps "sysctl" utility
func RunSysctl(args ...string) (string, string, error) {
stdout, stderr, err := run(runner.sysctlPath, args...)
return strings.TrimSpace(stdout.String()), stderr.String(), err
}
// RunPowershell runs a command via the Windows powershell utility
func RunPowershell(args ...string) (string, string, error) {
stdout, stderr, err := run(runner.powershellPath, args...)
return strings.TrimSpace(stdout.String()), stderr.String(), err
}
// RunNetsh runs a command via the Windows netsh utility
func RunNetsh(args ...string) (string, string, error) {
stdout, stderr, err := run(runner.netshPath, args...)
return strings.TrimSpace(stdout.String()), stderr.String(), err
}
// RunRoute runs a command via the Windows route utility
func RunRoute(args ...string) (string, string, error) {
stdout, stderr, err := run(runner.routePath, args...)
return strings.TrimSpace(stdout.String()), stderr.String(), err
}
// AddOFFlowWithSpecificAction replaces flows in the bridge by a single flow with a
// specified action
func AddOFFlowWithSpecificAction(bridgeName, action string) (string, string, error) {
args := []string{"-O", "OpenFlow13", "replace-flows", bridgeName, "-"}
stdin := &bytes.Buffer{}
stdin.Write([]byte(fmt.Sprintf("table=0,priority=0,actions=%s\n", action)))
cmd := runner.exec.Command(runner.ofctlPath, args...)
cmd.SetStdin(stdin)
stdout, stderr, err := runCmd(cmd, runner.ofctlPath, args...)
return strings.Trim(stdout.String(), "\" \n"), stderr.String(), err
}
// ReplaceOFFlows replaces flows in the bridge with a slice of flows
func ReplaceOFFlows(bridgeName string, flows []string) (string, string, error) {
args := []string{"-O", "OpenFlow13", "--bundle", "replace-flows", bridgeName, "-"}
stdin := &bytes.Buffer{}
stdin.Write([]byte(strings.Join(flows, "\n")))
cmd := runner.exec.Command(runner.ofctlPath, args...)
cmd.SetStdin(stdin)
stdout, stderr, err := runCmd(cmd, runner.ofctlPath, args...)
return strings.Trim(stdout.String(), "\" \n"), stderr.String(), err
}
// GetOFFlows gets all the flows from a bridge
func GetOFFlows(bridgeName string) ([]string, error) {
stdout, stderr, err := RunOVSOfctl("dump-flows", bridgeName)
if err != nil {
return nil, fmt.Errorf("failed to get flows on bridge %q:, stderr: %q, error: %v",
bridgeName, stderr, err)
}
var flows []string
for _, line := range strings.Split(stdout, "\n") {
if strings.Contains(line, "cookie=") {
flows = append(flows, strings.TrimSpace(line))
}
}
return flows, nil
}
// GetOpenFlowPorts names or numbers for a given bridge
func GetOpenFlowPorts(bridgeName string, namedPorts bool) ([]string, error) {
stdout, stderr, err := RunOVSOfctl("show", bridgeName)
if err != nil {
return nil, fmt.Errorf("failed to get list of ports on bridge %q:, stderr: %q, error: %v",
bridgeName, stderr, err)
}
index := 0
if namedPorts {
index = 1
}
var ports []string
re := regexp.MustCompile("[(|)]")
for _, line := range strings.Split(stdout, "\n") {
if strings.Contains(line, "addr:") {
port := strings.TrimSpace(
re.Split(line, -1)[index],
)
ports = append(ports, port)
}
}
return ports, nil
}
// GetOvnRunDir returns the OVN's rundir.
func GetOvnRunDir() string {
return runner.ovnRunDir
}
// ovsdb-server(5) says a clustered database is connected if the server
// is in contact with a majority of its cluster.
type OVNDBServerStatus struct {
Connected bool
Leader bool
Index int
}
// Internal structure that holds the un-marshaled json output from the
// ovsdb-client query command. The Index can hold ["set": []] when it is
// not populated yet, so we need to use `interface{}` type. However, we
// don't want our callers to worry about all this and we want them to see the
// Index as an integer and hence we use an exported OVNDBServerStatus for that
type dbRow struct {
Connected bool `json:"connected"`
Leader bool `json:"leader"`
Index interface{} `json:"index"`
}
type queryResult struct {
Rows []dbRow `json:"rows"`
}
func GetOVNDBServerInfo(timeout int, direction, database string) (*OVNDBServerStatus, error) {
sockPath := fmt.Sprintf("unix:/var/run/openvswitch/ovn%s_db.sock", direction)
transact := fmt.Sprintf(`["_Server", {"op":"select", "table":"Database", "where":[["name", "==", "%s"]], `+
`"columns": ["connected", "leader", "index"]}]`, database)
stdout, stderr, err := RunOVSDBClient(fmt.Sprintf("--timeout=%d", timeout), "query", sockPath, transact)
if err != nil {
return nil, fmt.Errorf("failed to get %q ovsdb-server status: stderr(%s), err(%v)",
direction, stderr, err)
}
var result []queryResult
err = json.Unmarshal([]byte(stdout), &result)
if err != nil {
return nil, fmt.Errorf("failed to parse the json output(%s) from ovsdb-client command for database %q: %v",
stdout, database, err)
}
if len(result) != 1 || len(result[0].Rows) != 1 {
return nil, fmt.Errorf("parsed json output for %q ovsdb-server has incorrect status information",
direction)
}
serverStatus := &OVNDBServerStatus{}
serverStatus.Connected = result[0].Rows[0].Connected
serverStatus.Leader = result[0].Rows[0].Leader
if index, ok := result[0].Rows[0].Index.(float64); ok {
serverStatus.Index = int(index)
} else {
serverStatus.Index = 0
}
return serverStatus, nil
}
// DetectSCTPSupport checks if OVN supports SCTP for load balancer
func DetectSCTPSupport() (bool, error) {
stdout, stderr, err := RunOVSDBClientOVNNB("list-columns", "--data=bare", "--no-heading",
"--format=json", "OVN_Northbound", "Load_Balancer")
if err != nil {
klog.Errorf("Failed to query OVN NB DB for SCTP support, "+
"stdout: %q, stderr: %q, error: %v", stdout, stderr, err)
return false, err
}
type OvsdbData struct {
Data [][]interface{}
}
var lbData OvsdbData
err = json.Unmarshal([]byte(stdout), &lbData)
if err != nil {
return false, err
}
for _, entry := range lbData.Data {
if entry[0].(string) == "protocol" && strings.Contains(fmt.Sprintf("%v", entry[1]), "sctp") {
return true, nil
}
}
return false, nil
}
// DetectCheckPktLengthSupport checks if OVN supports check packet length action in OVS kernel datapath
func DetectCheckPktLengthSupport(bridge string) (bool, error) {
stdout, stderr, err := RunOVSAppctl("dpif/show-dp-features", bridge)
if err != nil {
klog.Errorf("Failed to query OVS for check packet length support, "+
"stdout: %q, stderr: %q, error: %v", stdout, stderr, err)
return false, err
}
re := regexp.MustCompile(`(?i)yes|(?i)true`)
for _, line := range strings.Split(strings.TrimSuffix(stdout, "\n"), "\n") {
if strings.Contains(line, "Check pkt length action") && re.MatchString(line) {
return true, nil
}
}
return false, nil
}
// IsOvsHwOffloadEnabled checks if OvS Hardware Offload is enabled.
func IsOvsHwOffloadEnabled() (bool, error) {
stdout, stderr, err := RunOVSVsctl("--if-exists", "get",
"Open_vSwitch", ".", "other_config:hw-offload")
if err != nil {
klog.Errorf("Failed to get output from ovs-vsctl --if-exists get Open_vSwitch . "+
"other_config:hw-offload stderr(%s) : %v", stderr, err)
return false, err
}
// For the case if the hw-offload key doesn't exist, we check for empty output.
if len(stdout) == 0 || stdout == "false" {
return false, nil
}
return true, nil
}
type OvsDbProperties struct {
AppCtl func(timeout int, args ...string) (string, string, error)
DbAlias string
DbName string
ElectionTimer int
}
// GetOvsDbProperties inits OvsDbProperties based on db file path given to it.
// Now it only works with ovn dbs (nbdb and sbdb)
func GetOvsDbProperties(db string) (*OvsDbProperties, error) {
if strings.Contains(db, "ovnnb") {
return &OvsDbProperties{
ElectionTimer: int(config.OvnNorth.ElectionTimer) * 1000,
AppCtl: RunOVNNBAppCtlWithTimeout,
DbName: "OVN_Northbound",
DbAlias: db,
}, nil
} else if strings.Contains(db, "ovnsb") {
return &OvsDbProperties{
ElectionTimer: int(config.OvnSouth.ElectionTimer) * 1000,
AppCtl: RunOVNSBAppCtlWithTimeout,
DbName: "OVN_Southbound",
DbAlias: db,
}, nil
} else {
return nil, fmt.Errorf("failed to parse ovn db type Northbound/Southbound from the path %s", db)
}
}
// GetExternalIDValByKey returns the value of the specified key in a space separated string (each in the form of k=v)
func GetExternalIDValByKey(keyValString, key string) string {
keyVals := strings.Fields(keyValString)
for _, keyVal := range keyVals {
if strings.HasPrefix(keyVal, key+"=") {
return strings.TrimPrefix(keyVal, key+"=")
}
}
return ""
}
// GetOVSPortPodInfo gets OVS interface associated pod information (sandbox/NAD),
// returns false if the OVS interface does not exists
func GetOVSPortPodInfo(hostIfName string) (bool, string, string, error) {
stdout, stderr, err := RunOVSVsctl("--no-heading", "--format=csv", "--data=bare",
"--columns=external_ids", "find", "Interface", "name="+hostIfName)
if err != nil {
return false, "", "", fmt.Errorf("failed to get OVS interface %s, stderr %v: %v", hostIfName, stderr, err)
}
if stdout == "" {
return false, "", "", nil
}
sandbox := GetExternalIDValByKey(stdout, "sandbox")
nadName := GetExternalIDValByKey(stdout, types.NADExternalID)
// if network_name does not exists, it is default network
if nadName == "" {
nadName = types.DefaultNetworkName
}
return true, sandbox, nadName, nil
}