nics.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 sysutils
  15. import (
  16. "io/ioutil"
  17. "net"
  18. "os"
  19. "path"
  20. "path/filepath"
  21. "strconv"
  22. "strings"
  23. "yunion.io/x/log"
  24. "yunion.io/x/pkg/errors"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  26. "yunion.io/x/onecloud/pkg/httperrors"
  27. "yunion.io/x/onecloud/pkg/util/fileutils2"
  28. )
  29. const (
  30. sysNetPath = "/sys/class/net"
  31. )
  32. func Nics() ([]*types.SNicDevInfo, error) {
  33. if _, err := os.Stat(sysNetPath); !os.IsNotExist(err) {
  34. nicDevs, err := ioutil.ReadDir(sysNetPath)
  35. if err != nil {
  36. log.Errorf("ReadDir %s error: %s", sysNetPath, err)
  37. return nil, errors.Wrapf(err, "ioutil.ReadDir(%s)", sysNetPath)
  38. }
  39. nics := make([]*types.SNicDevInfo, 0)
  40. for _, nic := range nicDevs {
  41. netPath := filepath.Join(sysNetPath, nic.Name())
  42. // make sure this is a real NIC device
  43. if fi, err := os.Stat(filepath.Join(netPath, "device")); err != nil || fi == nil {
  44. continue
  45. } /*else if (fi.Mode() & os.ModeSymlink) == 0 {
  46. continue
  47. }*/
  48. nicType, err := fileutils2.FileGetContents(path.Join(netPath, "type"))
  49. if err != nil {
  50. return nil, errors.Wrap(err, "failed get nic type")
  51. }
  52. if strings.TrimSpace(nicType) == "32" {
  53. // include/uapi/linux/if_arp.h
  54. // #define ARPHRD_INFINIBAND 32 /* InfiniBand */
  55. continue // skip infiniband nic
  56. }
  57. speedStr := GetSysConfigQuiet(filepath.Join(netPath, "speed"))
  58. speed := 0
  59. if len(speedStr) > 0 {
  60. speed, _ = strconv.Atoi(speedStr)
  61. }
  62. carrier := GetSysConfigQuiet(filepath.Join(netPath, "carrier"))
  63. up := false
  64. if carrier == "1" {
  65. up = true
  66. }
  67. macStr := GetSysConfigQuiet(filepath.Join(netPath, "address"))
  68. permMacStr := GetSysConfigQuiet(filepath.Join(netPath, "bonding_slave/perm_hwaddr"))
  69. var mac net.HardwareAddr
  70. if len(permMacStr) > 0 {
  71. mac, _ = net.ParseMAC(permMacStr)
  72. } else if len(macStr) > 0 {
  73. mac, _ = net.ParseMAC(macStr)
  74. } else {
  75. // no valid mac address
  76. continue
  77. }
  78. mtuStr := GetSysConfigQuiet(filepath.Join(netPath, "mtu"))
  79. mtu := 0
  80. if len(mtuStr) > 0 {
  81. mtu, _ = strconv.Atoi(mtuStr)
  82. }
  83. nicInfo := &types.SNicDevInfo{
  84. Dev: nic.Name(),
  85. Mac: mac,
  86. Speed: speed,
  87. Up: &up,
  88. Mtu: mtu,
  89. }
  90. nics = append(nics, nicInfo)
  91. }
  92. return nics, nil
  93. }
  94. return nil, errors.Wrapf(httperrors.ErrNotSupported, "no such dir %s", sysNetPath)
  95. }