vnic.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 esxi
  15. import (
  16. "github.com/vmware/govmomi/vim25/types"
  17. "yunion.io/x/log"
  18. "yunion.io/x/pkg/util/netutils"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. )
  21. type SVirtualNIC struct {
  22. SVirtualDevice
  23. cloudprovider.DummyICloudNic
  24. }
  25. func NewVirtualNIC(vm *SVirtualMachine, dev types.BaseVirtualDevice, index int) SVirtualNIC {
  26. return SVirtualNIC{
  27. SVirtualDevice: NewVirtualDevice(vm, dev, index),
  28. }
  29. }
  30. func (nic *SVirtualNIC) getVirtualEthernetCard() *types.VirtualEthernetCard {
  31. card := types.VirtualEthernetCard{}
  32. if FetchAnonymousFieldValue(nic.dev, &card) {
  33. return &card
  34. }
  35. return nil
  36. }
  37. func (nic *SVirtualNIC) GetId() string {
  38. return ""
  39. }
  40. func (nic *SVirtualNIC) GetIP() string {
  41. guestIps := nic.vm.getGuestIps()
  42. if nicConf, ok := guestIps[nic.GetMAC()]; ok {
  43. if len(nicConf.IPs) > 0 {
  44. return nicConf.IPs[0]
  45. }
  46. }
  47. log.Warningf("cannot find ip for mac %s", nic.GetMAC())
  48. return ""
  49. }
  50. func (nic *SVirtualNIC) GetDriver() string {
  51. return nic.SVirtualDevice.GetDriver()
  52. }
  53. func (nic *SVirtualNIC) GetMAC() string {
  54. return netutils.FormatMacAddr(nic.getVirtualEthernetCard().MacAddress)
  55. }
  56. func (nic *SVirtualNIC) InClassicNetwork() bool {
  57. return false
  58. }
  59. func (nic *SVirtualNIC) GetINetworkId() string {
  60. return ""
  61. }
  62. func (nic *SVirtualNIC) GetSubAddress() ([]string, error) {
  63. guestIps := nic.vm.getGuestIps()
  64. if nicConf, ok := guestIps[nic.GetMAC()]; ok {
  65. if len(nicConf.IPs) > 1 {
  66. return nicConf.IPs[1:], nil
  67. }
  68. }
  69. return nil, nil
  70. }