host_taps.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 models
  15. import (
  16. "context"
  17. "sort"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. )
  23. func (h *SHost) GetDetailsTapConfig(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (api.SHostTapConfig, error) {
  24. conf := api.SHostTapConfig{}
  25. srvs, err := NetTapServiceManager.getEnabledTapServiceOnHost(h.Id)
  26. if err != nil {
  27. return conf, errors.Wrap(err, "NetTapServiceManager.getEnabledTapServiceOnHost")
  28. }
  29. for _, srv := range srvs {
  30. tapConf, err := srv.getConfig()
  31. if err != nil {
  32. return conf, errors.Wrap(err, "srv.getConfig")
  33. }
  34. conf.Taps = append(conf.Taps, tapConf)
  35. }
  36. flows, err := NetTapFlowManager.getEnabledTapFlowsOnHost(h.Id)
  37. if err != nil {
  38. return conf, errors.Wrap(err, "NetTapFlowManager.getEnabledTapFlowsOnHost")
  39. }
  40. mirrors := make([]api.SMirrorConfig, 0)
  41. for _, flow := range flows {
  42. mirror, err := flow.getMirrorConfig(true)
  43. if err != nil {
  44. if errors.Cause(err) == errors.ErrNotFound {
  45. continue
  46. } else {
  47. return conf, errors.Wrap(err, "flow.getMirrorConfig")
  48. }
  49. }
  50. mirrors = append(mirrors, mirror)
  51. }
  52. sort.Sort(sMirrorConfigs(mirrors))
  53. conf.Mirrors = mirrors // groupMirrorConfig(mirrors)
  54. return conf, nil
  55. }
  56. func (g *SGuest) getTapNicJsonDesc(ctx context.Context, p *api.GuestnetworkJsonDesc) *api.GuestnetworkJsonDesc {
  57. srv := NetTapServiceManager.getEnabledTapServiceByGuestId(g.Id)
  58. if srv == nil {
  59. return nil
  60. }
  61. var driver string
  62. var index int
  63. if p == nil {
  64. driver = "virtio"
  65. index = 0
  66. } else {
  67. driver = p.Driver
  68. index = p.Index + 1
  69. }
  70. desc := &api.GuestnetworkJsonDesc{
  71. GuestnetworkBaseDesc: api.GuestnetworkBaseDesc{
  72. Mac: srv.MacAddr,
  73. Virtual: true,
  74. Index: index,
  75. Bridge: api.HostTapBridge,
  76. Ifname: srv.Ifname,
  77. },
  78. Driver: driver,
  79. NumQueues: 1,
  80. }
  81. return desc
  82. }