ethtool_msglvl.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. // Package ethtool aims to provide a library giving a simple access to the
  22. // Linux SIOCETHTOOL ioctl operations. It can be used to retrieve informations
  23. // from a network device like statistics, driver related informations or
  24. // even the peer of a VETH interface.
  25. package ethtool
  26. import (
  27. "unsafe"
  28. "golang.org/x/sys/unix"
  29. )
  30. type ethtoolValue struct { /* ethtool.c: struct ethtool_value */
  31. cmd uint32
  32. data uint32
  33. }
  34. // MsglvlGet returns the msglvl of the given interface.
  35. func (e *Ethtool) MsglvlGet(intf string) (uint32, error) {
  36. edata := ethtoolValue{
  37. cmd: ETHTOOL_GMSGLVL,
  38. }
  39. var name [IFNAMSIZ]byte
  40. copy(name[:], []byte(intf))
  41. ifr := ifreq{
  42. ifr_name: name,
  43. ifr_data: uintptr(unsafe.Pointer(&edata)),
  44. }
  45. _, _, ep := unix.Syscall(unix.SYS_IOCTL, uintptr(e.fd),
  46. SIOCETHTOOL, uintptr(unsafe.Pointer(&ifr)))
  47. if ep != 0 {
  48. return 0, ep
  49. }
  50. return edata.data, nil
  51. }
  52. // MsglvlSet returns the read-msglvl, post-set-msglvl of the given interface.
  53. func (e *Ethtool) MsglvlSet(intf string, valset uint32) (uint32, uint32, error) {
  54. edata := ethtoolValue{
  55. cmd: ETHTOOL_GMSGLVL,
  56. }
  57. var name [IFNAMSIZ]byte
  58. copy(name[:], []byte(intf))
  59. ifr := ifreq{
  60. ifr_name: name,
  61. ifr_data: uintptr(unsafe.Pointer(&edata)),
  62. }
  63. _, _, ep := unix.Syscall(unix.SYS_IOCTL, uintptr(e.fd),
  64. SIOCETHTOOL, uintptr(unsafe.Pointer(&ifr)))
  65. if ep != 0 {
  66. return 0, 0, ep
  67. }
  68. readval := edata.data
  69. edata.cmd = ETHTOOL_SMSGLVL
  70. edata.data = valset
  71. _, _, ep = unix.Syscall(unix.SYS_IOCTL, uintptr(e.fd),
  72. SIOCETHTOOL, uintptr(unsafe.Pointer(&ifr)))
  73. if ep != 0 {
  74. return 0, 0, ep
  75. }
  76. return readval, edata.data, nil
  77. }
  78. // MsglvlGet returns the msglvl of the given interface.
  79. func MsglvlGet(intf string) (uint32, error) {
  80. e, err := NewEthtool()
  81. if err != nil {
  82. return 0, err
  83. }
  84. defer e.Close()
  85. return e.MsglvlGet(intf)
  86. }
  87. // MsglvlSet returns the read-msglvl, post-set-msglvl of the given interface.
  88. func MsglvlSet(intf string, valset uint32) (uint32, uint32, error) {
  89. e, err := NewEthtool()
  90. if err != nil {
  91. return 0, 0, err
  92. }
  93. defer e.Close()
  94. return e.MsglvlSet(intf, valset)
  95. }