ovsutils.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ovsutils
  15. import (
  16. "fmt"
  17. "net"
  18. "regexp"
  19. "strings"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/utils"
  23. "yunion.io/x/onecloud/pkg/util/procutils"
  24. "yunion.io/x/onecloud/pkg/util/regutils2"
  25. )
  26. func GetDbPorts(brname string) []string {
  27. output, err := procutils.NewCommand("ovs-vsctl", "list-ifaces", brname).Output()
  28. if err != nil {
  29. log.Errorln(err)
  30. return nil
  31. }
  32. ifaces := make([]string, 0)
  33. re := regexp.MustCompile(`^[a-zA-Z0-9._@-]+$`)
  34. for _, line := range strings.Split(string(output), "\n") {
  35. ifname := strings.TrimSpace(line)
  36. if len(ifname) > 0 && re.MatchString(ifname) {
  37. ifaces = append(ifaces, ifname)
  38. }
  39. }
  40. return ifaces
  41. }
  42. func GetDpPorts(brname string) []string {
  43. output, err := procutils.NewCommand("ovs-dpctl", "show").Output()
  44. if err != nil {
  45. log.Errorln(err)
  46. return nil
  47. }
  48. ifaces := make([]string, 0)
  49. re := regexp.MustCompile(`port \d+: (?P<name>[a-zA-Z0-9._@-]+)`)
  50. for _, line := range strings.Split(string(output), "\n") {
  51. m := regutils2.GetParams(re, line)
  52. if len(m) > 0 {
  53. ifnmae := m["name"]
  54. if ifnmae != brname {
  55. ifaces = append(ifaces, ifnmae)
  56. }
  57. }
  58. }
  59. return ifaces
  60. }
  61. func GetBridges() []string {
  62. output, err := procutils.NewCommand("ovs-vsctl", "list-br").Output()
  63. if err != nil {
  64. log.Errorln(err)
  65. return nil
  66. }
  67. brs := make([]string, 0)
  68. for _, line := range strings.Split(string(output), "\n") {
  69. brname := strings.TrimSpace(line)
  70. if len(brname) > 0 {
  71. brs = append(brs, brname)
  72. }
  73. }
  74. return brs
  75. }
  76. func RemovePortFromBridge(brname, port string) {
  77. log.Infof("remove_port_from_bridge %s %s", brname, port)
  78. if err := procutils.NewCommand("ovs-vsctl", "del-port", brname, port).Run(); err != nil {
  79. log.Errorln(err)
  80. }
  81. }
  82. func CleanHiddenPorts(brname string) {
  83. // BUG patch ports are not included in dpPorts
  84. dbPorts := GetDbPorts(brname)
  85. dpPorts := GetDpPorts(brname)
  86. for _, p := range dbPorts {
  87. if !utils.IsInStringArray(p, dpPorts) {
  88. RemovePortFromBridge(brname, p)
  89. }
  90. }
  91. }
  92. func CleanAllHiddenPorts() {
  93. brs := GetBridges()
  94. for _, br := range brs {
  95. CleanHiddenPorts(br)
  96. }
  97. }
  98. // WHY normalize ovs-db host?
  99. // if ovn-db url is given by domain name, ovn-nbctl will always try to resolve in IPv6 first
  100. // if host is IPv4 only, ovn-nbctl will report error "connect: Address family not supported by protocol"
  101. func NormalizeDbHost(db string) (string, error) {
  102. if strings.HasPrefix(db, "tcp:") {
  103. host, port, err := net.SplitHostPort(db[4:])
  104. if err != nil {
  105. return "", errors.Wrapf(err, "split host port: %s", db)
  106. }
  107. if ip := net.ParseIP(host); len(ip) == 0 {
  108. addrs, err := net.LookupHost(host)
  109. if err != nil {
  110. return "", errors.Wrapf(err, "dns lookup (%s) failed", host)
  111. }
  112. if len(addrs) == 0 {
  113. return "", fmt.Errorf("dns lookup (%s) returned empty result", host)
  114. }
  115. return "tcp:" + net.JoinHostPort(addrs[0], port), nil
  116. }
  117. }
  118. return db, nil
  119. }