bonding.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 netutils2
  15. import (
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "strings"
  20. "yunion.io/x/log"
  21. "yunion.io/x/onecloud/pkg/util/fileutils2"
  22. )
  23. const (
  24. bondingModePath = "/sys/class/net/%s/bonding/mode"
  25. bondingSlavesPath = "/sys/class/net/%s/bonding/slaves"
  26. )
  27. type SBondingConfig struct {
  28. Mode int
  29. ModeName string
  30. Slaves []string
  31. }
  32. func getBondingConfig(ifname string) *SBondingConfig {
  33. modePath := fmt.Sprintf(bondingModePath, ifname)
  34. slavesPath := fmt.Sprintf(bondingSlavesPath, ifname)
  35. if !fileutils2.Exists(modePath) || !fileutils2.Exists(slavesPath) {
  36. return nil
  37. }
  38. conf := SBondingConfig{}
  39. {
  40. content, err := os.ReadFile(modePath)
  41. if err != nil {
  42. log.Errorf("fail to read %s", modePath)
  43. return nil
  44. }
  45. parts := strings.Split(string(content), " ")
  46. mode, _ := strconv.Atoi(strings.TrimSpace(parts[len(parts)-1]))
  47. modeName := strings.Join(parts[:len(parts)-1], " ")
  48. conf.Mode = int(mode)
  49. conf.ModeName = modeName
  50. }
  51. {
  52. content, err := os.ReadFile(slavesPath)
  53. if err != nil {
  54. log.Errorf("fail to read %s", modePath)
  55. return nil
  56. }
  57. parts := strings.Split(string(content), " ")
  58. for _, part := range parts {
  59. part = strings.TrimSpace(part)
  60. if len(part) > 0 {
  61. conf.Slaves = append(conf.Slaves, part)
  62. }
  63. }
  64. }
  65. return &conf
  66. }