hostnetifs.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 cloudprovider
  15. import (
  16. "crypto/sha256"
  17. "fmt"
  18. "sort"
  19. "strings"
  20. "yunion.io/x/pkg/tristate"
  21. )
  22. type TGlobalIdResource interface {
  23. GetGlobalId() string
  24. }
  25. type TCloudResources[T TGlobalIdResource] []T
  26. func (a TCloudResources[T]) Len() int { return len(a) }
  27. func (a TCloudResources[T]) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  28. func (a TCloudResources[T]) Less(i, j int) bool { return a[i].GetGlobalId() < a[j].GetGlobalId() }
  29. func GetHostNetifs(host ICloudHost, wires []ICloudWire) []ICloudHostNetInterface {
  30. localWires := make(TCloudResources[ICloudWire], len(wires))
  31. copy(localWires, wires)
  32. sort.Sort(localWires)
  33. ret := make([]ICloudHostNetInterface, 0, len(localWires))
  34. for i := range localWires {
  35. hw := sFakeHostNics{
  36. host: host,
  37. wire: localWires[i],
  38. index: i,
  39. }
  40. ret = append(ret, &hw)
  41. }
  42. return ret
  43. }
  44. type sFakeHostNics struct {
  45. host ICloudHost
  46. wire ICloudWire
  47. index int
  48. }
  49. func (hn *sFakeHostNics) GetDevice() string {
  50. return fmt.Sprintf("eth%d", hn.index)
  51. }
  52. func (hn *sFakeHostNics) GetDriver() string {
  53. return "e1000"
  54. }
  55. func (hn *sFakeHostNics) GetMac() string {
  56. return HashIdsMac(hn.host.GetGlobalId(), hn.wire.GetGlobalId())
  57. }
  58. func (hn *sFakeHostNics) GetVlanId() int {
  59. return 1
  60. }
  61. func (hn *sFakeHostNics) GetIndex() int8 {
  62. return int8(hn.index)
  63. }
  64. func (hn *sFakeHostNics) IsLinkUp() tristate.TriState {
  65. return tristate.True
  66. }
  67. func (hn *sFakeHostNics) GetIpAddr() string {
  68. return ""
  69. }
  70. func (hn *sFakeHostNics) GetMtu() int32 {
  71. return 1500
  72. }
  73. func (hn *sFakeHostNics) GetNicType() string {
  74. return ""
  75. }
  76. func (hn *sFakeHostNics) GetBridge() string {
  77. return fmt.Sprintf("br%d", hn.index)
  78. }
  79. func (hn *sFakeHostNics) GetIWire() ICloudWire {
  80. return hn.wire
  81. }
  82. func HashIdsMac(ids ...string) string {
  83. h := sha256.New()
  84. for _, id := range ids {
  85. h.Write([]byte(id))
  86. }
  87. sum := h.Sum(nil)
  88. hexStr := make([]string, 6)
  89. hexStr[0] = "ff"
  90. for i := 1; i < 6; i++ {
  91. hexStr[i] = fmt.Sprintf("%02x", sum[i])
  92. }
  93. return strings.Join(hexStr, ":")
  94. }