bridge_linux.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package netlink
  2. import (
  3. "fmt"
  4. "syscall"
  5. "github.com/vishvananda/netlink/nl"
  6. )
  7. // BridgeVlanList gets a map of device id to bridge vlan infos.
  8. // Equivalent to: `bridge vlan show`
  9. func BridgeVlanList() (map[int32][]*nl.BridgeVlanInfo, error) {
  10. return pkgHandle.BridgeVlanList()
  11. }
  12. // BridgeVlanList gets a map of device id to bridge vlan infos.
  13. // Equivalent to: `bridge vlan show`
  14. func (h *Handle) BridgeVlanList() (map[int32][]*nl.BridgeVlanInfo, error) {
  15. req := h.newNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_DUMP)
  16. msg := nl.NewIfInfomsg(syscall.AF_BRIDGE)
  17. req.AddData(msg)
  18. req.AddData(nl.NewRtAttr(nl.IFLA_EXT_MASK, nl.Uint32Attr(uint32(nl.RTEXT_FILTER_BRVLAN))))
  19. msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWLINK)
  20. if err != nil {
  21. return nil, err
  22. }
  23. ret := make(map[int32][]*nl.BridgeVlanInfo)
  24. for _, m := range msgs {
  25. msg := nl.DeserializeIfInfomsg(m)
  26. attrs, err := nl.ParseRouteAttr(m[msg.Len():])
  27. if err != nil {
  28. return nil, err
  29. }
  30. for _, attr := range attrs {
  31. switch attr.Attr.Type {
  32. case nl.IFLA_AF_SPEC:
  33. //nested attr
  34. nestAttrs, err := nl.ParseRouteAttr(attr.Value)
  35. if err != nil {
  36. return nil, fmt.Errorf("failed to parse nested attr %v", err)
  37. }
  38. for _, nestAttr := range nestAttrs {
  39. switch nestAttr.Attr.Type {
  40. case nl.IFLA_BRIDGE_VLAN_INFO:
  41. vlanInfo := nl.DeserializeBridgeVlanInfo(nestAttr.Value)
  42. ret[msg.Index] = append(ret[msg.Index], vlanInfo)
  43. }
  44. }
  45. }
  46. }
  47. }
  48. return ret, nil
  49. }
  50. // BridgeVlanAdd adds a new vlan filter entry
  51. // Equivalent to: `bridge vlan add dev DEV vid VID [ pvid ] [ untagged ] [ self ] [ master ]`
  52. func BridgeVlanAdd(link Link, vid uint16, pvid, untagged, self, master bool) error {
  53. return pkgHandle.BridgeVlanAdd(link, vid, pvid, untagged, self, master)
  54. }
  55. // BridgeVlanAdd adds a new vlan filter entry
  56. // Equivalent to: `bridge vlan add dev DEV vid VID [ pvid ] [ untagged ] [ self ] [ master ]`
  57. func (h *Handle) BridgeVlanAdd(link Link, vid uint16, pvid, untagged, self, master bool) error {
  58. return h.bridgeVlanModify(syscall.RTM_SETLINK, link, vid, pvid, untagged, self, master)
  59. }
  60. // BridgeVlanDel adds a new vlan filter entry
  61. // Equivalent to: `bridge vlan del dev DEV vid VID [ pvid ] [ untagged ] [ self ] [ master ]`
  62. func BridgeVlanDel(link Link, vid uint16, pvid, untagged, self, master bool) error {
  63. return pkgHandle.BridgeVlanDel(link, vid, pvid, untagged, self, master)
  64. }
  65. // BridgeVlanDel adds a new vlan filter entry
  66. // Equivalent to: `bridge vlan del dev DEV vid VID [ pvid ] [ untagged ] [ self ] [ master ]`
  67. func (h *Handle) BridgeVlanDel(link Link, vid uint16, pvid, untagged, self, master bool) error {
  68. return h.bridgeVlanModify(syscall.RTM_DELLINK, link, vid, pvid, untagged, self, master)
  69. }
  70. func (h *Handle) bridgeVlanModify(cmd int, link Link, vid uint16, pvid, untagged, self, master bool) error {
  71. base := link.Attrs()
  72. h.ensureIndex(base)
  73. req := h.newNetlinkRequest(cmd, syscall.NLM_F_ACK)
  74. msg := nl.NewIfInfomsg(syscall.AF_BRIDGE)
  75. msg.Index = int32(base.Index)
  76. req.AddData(msg)
  77. br := nl.NewRtAttr(nl.IFLA_AF_SPEC, nil)
  78. var flags uint16
  79. if self {
  80. flags |= nl.BRIDGE_FLAGS_SELF
  81. }
  82. if master {
  83. flags |= nl.BRIDGE_FLAGS_MASTER
  84. }
  85. if flags > 0 {
  86. nl.NewRtAttrChild(br, nl.IFLA_BRIDGE_FLAGS, nl.Uint16Attr(flags))
  87. }
  88. vlanInfo := &nl.BridgeVlanInfo{Vid: vid}
  89. if pvid {
  90. vlanInfo.Flags |= nl.BRIDGE_VLAN_INFO_PVID
  91. }
  92. if untagged {
  93. vlanInfo.Flags |= nl.BRIDGE_VLAN_INFO_UNTAGGED
  94. }
  95. nl.NewRtAttrChild(br, nl.IFLA_BRIDGE_VLAN_INFO, vlanInfo.Serialize())
  96. req.AddData(br)
  97. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  98. if err != nil {
  99. return err
  100. }
  101. return nil
  102. }