protinfo_linux.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package netlink
  2. import (
  3. "fmt"
  4. "syscall"
  5. "github.com/vishvananda/netlink/nl"
  6. )
  7. func LinkGetProtinfo(link Link) (Protinfo, error) {
  8. return pkgHandle.LinkGetProtinfo(link)
  9. }
  10. func (h *Handle) LinkGetProtinfo(link Link) (Protinfo, error) {
  11. base := link.Attrs()
  12. h.ensureIndex(base)
  13. var pi Protinfo
  14. req := h.newNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_DUMP)
  15. msg := nl.NewIfInfomsg(syscall.AF_BRIDGE)
  16. req.AddData(msg)
  17. msgs, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  18. if err != nil {
  19. return pi, err
  20. }
  21. for _, m := range msgs {
  22. ans := nl.DeserializeIfInfomsg(m)
  23. if int(ans.Index) != base.Index {
  24. continue
  25. }
  26. attrs, err := nl.ParseRouteAttr(m[ans.Len():])
  27. if err != nil {
  28. return pi, err
  29. }
  30. for _, attr := range attrs {
  31. if attr.Attr.Type != syscall.IFLA_PROTINFO|syscall.NLA_F_NESTED {
  32. continue
  33. }
  34. infos, err := nl.ParseRouteAttr(attr.Value)
  35. if err != nil {
  36. return pi, err
  37. }
  38. pi = *parseProtinfo(infos)
  39. return pi, nil
  40. }
  41. }
  42. return pi, fmt.Errorf("Device with index %d not found", base.Index)
  43. }
  44. func parseProtinfo(infos []syscall.NetlinkRouteAttr) *Protinfo {
  45. var pi Protinfo
  46. for _, info := range infos {
  47. switch info.Attr.Type {
  48. case nl.IFLA_BRPORT_MODE:
  49. pi.Hairpin = byteToBool(info.Value[0])
  50. case nl.IFLA_BRPORT_GUARD:
  51. pi.Guard = byteToBool(info.Value[0])
  52. case nl.IFLA_BRPORT_FAST_LEAVE:
  53. pi.FastLeave = byteToBool(info.Value[0])
  54. case nl.IFLA_BRPORT_PROTECT:
  55. pi.RootBlock = byteToBool(info.Value[0])
  56. case nl.IFLA_BRPORT_LEARNING:
  57. pi.Learning = byteToBool(info.Value[0])
  58. case nl.IFLA_BRPORT_UNICAST_FLOOD:
  59. pi.Flood = byteToBool(info.Value[0])
  60. }
  61. }
  62. return &pi
  63. }