ovnutils.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 ovnutils
  15. import (
  16. "fmt"
  17. "os"
  18. "runtime/debug"
  19. "strings"
  20. "time"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/errors"
  23. "yunion.io/x/onecloud/pkg/hostman/system_service"
  24. "yunion.io/x/onecloud/pkg/util/netutils2"
  25. "yunion.io/x/onecloud/pkg/util/ovsutils"
  26. "yunion.io/x/onecloud/pkg/util/procutils"
  27. )
  28. func configBridgeMtu(opts SOvnOptions) {
  29. timer := time.NewTimer(time.Minute)
  30. go func() {
  31. <-timer.C
  32. err := ensureConfigBridgeMtu(opts)
  33. if err != nil {
  34. log.Errorf("configuring mtu fail: %s, retry...", err)
  35. configBridgeMtu(opts)
  36. } else {
  37. log.Infof("set brvpc MTU to %d success!", opts.OvnUnderlayMtu)
  38. }
  39. }()
  40. }
  41. func ensureConfigBridgeMtu(opts SOvnOptions) error {
  42. args := []string{"set", "Interface", opts.OvnIntegrationBridge, fmt.Sprintf("mtu_request=%d", opts.OvnUnderlayMtu)}
  43. output, err := procutils.NewCommand("ovs-vsctl", args...).Output()
  44. if err != nil {
  45. return errors.Wrapf(err, "ovs-vsctl %s", string(output))
  46. }
  47. return nil
  48. }
  49. func mustPrepOvsdbConfig(opts SOvnOptions) {
  50. var (
  51. args = []string{"set", "Open_vSwitch", "."}
  52. )
  53. {
  54. if opts.OvnIntegrationBridge == "" {
  55. panic(errors.Wrap(ErrOvnConfig, "bad config: ovn_integration_bridge"))
  56. }
  57. args = append(args, fmt.Sprintf("external_ids:ovn-bridge=%s",
  58. opts.OvnIntegrationBridge))
  59. }
  60. {
  61. encapIp := opts.OvnEncapIp
  62. if encapIp == "" {
  63. var (
  64. err error
  65. meth = opts.OvnEncapIpDetectionMethod
  66. )
  67. switch {
  68. case strings.HasPrefix(meth, "can-reach:"):
  69. target := meth[10:]
  70. if strings.Contains(target, ":") {
  71. encapIp, err = netutils2.MyIPSmartTo("", target)
  72. } else {
  73. encapIp, err = netutils2.MyIPSmartTo(target, "")
  74. }
  75. default:
  76. encapIp, err = netutils2.MyIPSmart()
  77. if err != nil {
  78. encapIp, err = netutils2.MyIP()
  79. }
  80. }
  81. if err != nil {
  82. panic(errors.Wrapf(ErrOvnConfig, "determine encap ip, method: %q: %v", meth, err))
  83. }
  84. }
  85. args = append(args, "external_ids:ovn-encap-type=geneve")
  86. args = append(args, fmt.Sprintf("external_ids:ovn-encap-ip=%s", encapIp))
  87. }
  88. {
  89. if opts.OvnSouthDatabase == "" {
  90. panic(errors.Wrap(ErrOvnConfig, "bad config: ovn_south_database"))
  91. }
  92. db, err := ovsutils.NormalizeDbHost(opts.OvnSouthDatabase)
  93. if err != nil {
  94. panic(errors.Wrap(err, "normalize db host"))
  95. }
  96. opts.OvnSouthDatabase = db
  97. args = append(args, fmt.Sprintf("external_ids:ovn-remote=%s",
  98. opts.OvnSouthDatabase))
  99. }
  100. output, err := procutils.NewCommand("ovs-vsctl", args...).Output()
  101. if err != nil {
  102. panic(errors.Wrapf(err, "configuring ovn-controller: %s", string(output)))
  103. }
  104. }
  105. func mustPrepService() {
  106. ovn := system_service.GetService("ovn-controller")
  107. if !ovn.IsInstalled() {
  108. panic(errors.Wrap(ErrOvnService, "not installed"))
  109. }
  110. if ovn.IsEnabled() {
  111. // - ovn-controller Requires "openvswitch.service"
  112. // - openvswitch service should be disabled on startup
  113. if err := ovn.Disable(); err != nil {
  114. panic(errors.Wrap(err, "disable ovn-controller on startup"))
  115. }
  116. }
  117. if err := ovn.Start(false); err != nil {
  118. panic(errors.Wrap(err, "start ovn-controller"))
  119. }
  120. }
  121. func InitOvn(opts SOvnOptions) (err error) {
  122. defer func() {
  123. if panicVal := recover(); panicVal != nil {
  124. debug.PrintStack()
  125. err = panicVal.(error)
  126. }
  127. }()
  128. mustPrepOvsdbConfig(opts)
  129. configBridgeMtu(opts)
  130. if _, ok := ovnContainerImageTag(); !ok {
  131. mustPrepService()
  132. }
  133. return nil
  134. }
  135. func MustGetOvnVersion() string {
  136. if tag, _ := ovnContainerImageTag(); tag != "" {
  137. return tag
  138. }
  139. output, err := procutils.NewRemoteCommandAsFarAsPossible("ovn-controller", "--version").Output()
  140. if err != nil {
  141. return ""
  142. }
  143. return ovnExtractVersion(string(output))
  144. }
  145. func HasOvnSupport() bool {
  146. if OvnControllerInsideContainer() {
  147. return true
  148. }
  149. ver := MustGetOvnVersion()
  150. return ver != ""
  151. }
  152. func OvnControllerInsideContainer() bool {
  153. tag, _ := ovnContainerImageTag()
  154. return tag != ""
  155. }
  156. func ovnContainerImageTag() (string, bool) {
  157. return os.LookupEnv("OVN_CONTAINER_IMAGE_TAG")
  158. }
  159. func ovnExtractVersion(in string) string {
  160. r := make([]rune, 0, 8)
  161. var (
  162. dot = false
  163. ndot = 0
  164. digit = 0
  165. )
  166. reset := func() {
  167. dot = false
  168. ndot = 0
  169. digit = 0
  170. }
  171. for _, c := range in {
  172. switch {
  173. case c == '.':
  174. if dot || digit == 0 {
  175. reset()
  176. continue
  177. }
  178. r = append(r, c)
  179. dot = true
  180. ndot += 1
  181. digit = 0
  182. case c >= '0' && c <= '9':
  183. dot = false
  184. if digit < 3 {
  185. r = append(r, c)
  186. digit += 1
  187. continue
  188. }
  189. reset()
  190. default:
  191. if ndot > 0 && ndot < 3 {
  192. return string(r)
  193. }
  194. reset()
  195. }
  196. }
  197. if ndot > 0 && ndot < 3 {
  198. return string(r)
  199. }
  200. return ""
  201. }