device.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // +build !windows
  2. // Copyright 2015 flannel authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // +build !windows
  16. package vxlan
  17. import (
  18. "fmt"
  19. "net"
  20. "syscall"
  21. log "github.com/golang/glog"
  22. "github.com/vishvananda/netlink"
  23. "github.com/coreos/flannel/pkg/ip"
  24. )
  25. type vxlanDeviceAttrs struct {
  26. vni uint32
  27. name string
  28. vtepIndex int
  29. vtepAddr net.IP
  30. vtepPort int
  31. gbp bool
  32. }
  33. type vxlanDevice struct {
  34. link *netlink.Vxlan
  35. directRouting bool
  36. }
  37. func newVXLANDevice(devAttrs *vxlanDeviceAttrs) (*vxlanDevice, error) {
  38. link := &netlink.Vxlan{
  39. LinkAttrs: netlink.LinkAttrs{
  40. Name: devAttrs.name,
  41. },
  42. VxlanId: int(devAttrs.vni),
  43. VtepDevIndex: devAttrs.vtepIndex,
  44. SrcAddr: devAttrs.vtepAddr,
  45. Port: devAttrs.vtepPort,
  46. Learning: false,
  47. GBP: devAttrs.gbp,
  48. }
  49. link, err := ensureLink(link)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &vxlanDevice{
  54. link: link,
  55. }, nil
  56. }
  57. func ensureLink(vxlan *netlink.Vxlan) (*netlink.Vxlan, error) {
  58. err := netlink.LinkAdd(vxlan)
  59. if err == syscall.EEXIST {
  60. // it's ok if the device already exists as long as config is similar
  61. log.V(1).Infof("VXLAN device already exists")
  62. existing, err := netlink.LinkByName(vxlan.Name)
  63. if err != nil {
  64. return nil, err
  65. }
  66. incompat := vxlanLinksIncompat(vxlan, existing)
  67. if incompat == "" {
  68. log.V(1).Infof("Returning existing device")
  69. return existing.(*netlink.Vxlan), nil
  70. }
  71. // delete existing
  72. log.Warningf("%q already exists with incompatable configuration: %v; recreating device", vxlan.Name, incompat)
  73. if err = netlink.LinkDel(existing); err != nil {
  74. return nil, fmt.Errorf("failed to delete interface: %v", err)
  75. }
  76. // create new
  77. if err = netlink.LinkAdd(vxlan); err != nil {
  78. return nil, fmt.Errorf("failed to create vxlan interface: %v", err)
  79. }
  80. } else if err != nil {
  81. return nil, err
  82. }
  83. ifindex := vxlan.Index
  84. link, err := netlink.LinkByIndex(vxlan.Index)
  85. if err != nil {
  86. return nil, fmt.Errorf("can't locate created vxlan device with index %v", ifindex)
  87. }
  88. var ok bool
  89. if vxlan, ok = link.(*netlink.Vxlan); !ok {
  90. return nil, fmt.Errorf("created vxlan device with index %v is not vxlan", ifindex)
  91. }
  92. return vxlan, nil
  93. }
  94. func (dev *vxlanDevice) Configure(ipn ip.IP4Net) error {
  95. if err := ip.EnsureV4AddressOnLink(ipn, dev.link); err != nil {
  96. return fmt.Errorf("failed to ensure address of interface %s: %s", dev.link.Attrs().Name, err)
  97. }
  98. if err := netlink.LinkSetUp(dev.link); err != nil {
  99. return fmt.Errorf("failed to set interface %s to UP state: %s", dev.link.Attrs().Name, err)
  100. }
  101. return nil
  102. }
  103. func (dev *vxlanDevice) MACAddr() net.HardwareAddr {
  104. return dev.link.HardwareAddr
  105. }
  106. type neighbor struct {
  107. MAC net.HardwareAddr
  108. IP ip.IP4
  109. }
  110. func (dev *vxlanDevice) AddFDB(n neighbor) error {
  111. log.V(4).Infof("calling AddFDB: %v, %v", n.IP, n.MAC)
  112. return netlink.NeighSet(&netlink.Neigh{
  113. LinkIndex: dev.link.Index,
  114. State: netlink.NUD_PERMANENT,
  115. Family: syscall.AF_BRIDGE,
  116. Flags: netlink.NTF_SELF,
  117. IP: n.IP.ToIP(),
  118. HardwareAddr: n.MAC,
  119. })
  120. }
  121. func (dev *vxlanDevice) DelFDB(n neighbor) error {
  122. log.V(4).Infof("calling DelFDB: %v, %v", n.IP, n.MAC)
  123. return netlink.NeighDel(&netlink.Neigh{
  124. LinkIndex: dev.link.Index,
  125. Family: syscall.AF_BRIDGE,
  126. Flags: netlink.NTF_SELF,
  127. IP: n.IP.ToIP(),
  128. HardwareAddr: n.MAC,
  129. })
  130. }
  131. func (dev *vxlanDevice) AddARP(n neighbor) error {
  132. log.V(4).Infof("calling AddARP: %v, %v", n.IP, n.MAC)
  133. return netlink.NeighSet(&netlink.Neigh{
  134. LinkIndex: dev.link.Index,
  135. State: netlink.NUD_PERMANENT,
  136. Type: syscall.RTN_UNICAST,
  137. IP: n.IP.ToIP(),
  138. HardwareAddr: n.MAC,
  139. })
  140. }
  141. func (dev *vxlanDevice) DelARP(n neighbor) error {
  142. log.V(4).Infof("calling DelARP: %v, %v", n.IP, n.MAC)
  143. return netlink.NeighDel(&netlink.Neigh{
  144. LinkIndex: dev.link.Index,
  145. State: netlink.NUD_PERMANENT,
  146. Type: syscall.RTN_UNICAST,
  147. IP: n.IP.ToIP(),
  148. HardwareAddr: n.MAC,
  149. })
  150. }
  151. func vxlanLinksIncompat(l1, l2 netlink.Link) string {
  152. if l1.Type() != l2.Type() {
  153. return fmt.Sprintf("link type: %v vs %v", l1.Type(), l2.Type())
  154. }
  155. v1 := l1.(*netlink.Vxlan)
  156. v2 := l2.(*netlink.Vxlan)
  157. if v1.VxlanId != v2.VxlanId {
  158. return fmt.Sprintf("vni: %v vs %v", v1.VxlanId, v2.VxlanId)
  159. }
  160. if v1.VtepDevIndex > 0 && v2.VtepDevIndex > 0 && v1.VtepDevIndex != v2.VtepDevIndex {
  161. return fmt.Sprintf("vtep (external) interface: %v vs %v", v1.VtepDevIndex, v2.VtepDevIndex)
  162. }
  163. if len(v1.SrcAddr) > 0 && len(v2.SrcAddr) > 0 && !v1.SrcAddr.Equal(v2.SrcAddr) {
  164. return fmt.Sprintf("vtep (external) IP: %v vs %v", v1.SrcAddr, v2.SrcAddr)
  165. }
  166. if len(v1.Group) > 0 && len(v2.Group) > 0 && !v1.Group.Equal(v2.Group) {
  167. return fmt.Sprintf("group address: %v vs %v", v1.Group, v2.Group)
  168. }
  169. if v1.L2miss != v2.L2miss {
  170. return fmt.Sprintf("l2miss: %v vs %v", v1.L2miss, v2.L2miss)
  171. }
  172. if v1.Port > 0 && v2.Port > 0 && v1.Port != v2.Port {
  173. return fmt.Sprintf("port: %v vs %v", v1.Port, v2.Port)
  174. }
  175. if v1.GBP != v2.GBP {
  176. return fmt.Sprintf("gbp: %v vs %v", v1.GBP, v2.GBP)
  177. }
  178. return ""
  179. }