device.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2015 flannel authors
  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 vxlan
  15. import (
  16. "fmt"
  17. "net"
  18. "syscall"
  19. log "github.com/golang/glog"
  20. "github.com/vishvananda/netlink"
  21. "github.com/coreos/flannel/pkg/ip"
  22. )
  23. type vxlanDeviceAttrs struct {
  24. vni uint32
  25. name string
  26. vtepIndex int
  27. vtepAddr net.IP
  28. vtepPort int
  29. gbp bool
  30. }
  31. type vxlanDevice struct {
  32. link *netlink.Vxlan
  33. }
  34. func newVXLANDevice(devAttrs *vxlanDeviceAttrs) (*vxlanDevice, error) {
  35. link := &netlink.Vxlan{
  36. LinkAttrs: netlink.LinkAttrs{
  37. Name: devAttrs.name,
  38. },
  39. VxlanId: int(devAttrs.vni),
  40. VtepDevIndex: devAttrs.vtepIndex,
  41. SrcAddr: devAttrs.vtepAddr,
  42. Port: devAttrs.vtepPort,
  43. Learning: false,
  44. GBP: devAttrs.gbp,
  45. }
  46. link, err := ensureLink(link)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &vxlanDevice{
  51. link: link,
  52. }, nil
  53. }
  54. func ensureLink(vxlan *netlink.Vxlan) (*netlink.Vxlan, error) {
  55. err := netlink.LinkAdd(vxlan)
  56. if err == syscall.EEXIST {
  57. // it's ok if the device already exists as long as config is similar
  58. log.V(1).Infof("VXLAN device already exists")
  59. existing, err := netlink.LinkByName(vxlan.Name)
  60. if err != nil {
  61. return nil, err
  62. }
  63. incompat := vxlanLinksIncompat(vxlan, existing)
  64. if incompat == "" {
  65. log.V(1).Infof("Returning existing device")
  66. return existing.(*netlink.Vxlan), nil
  67. }
  68. // delete existing
  69. log.Warningf("%q already exists with incompatable configuration: %v; recreating device", vxlan.Name, incompat)
  70. if err = netlink.LinkDel(existing); err != nil {
  71. return nil, fmt.Errorf("failed to delete interface: %v", err)
  72. }
  73. // create new
  74. if err = netlink.LinkAdd(vxlan); err != nil {
  75. return nil, fmt.Errorf("failed to create vxlan interface: %v", err)
  76. }
  77. } else if err != nil {
  78. return nil, err
  79. }
  80. ifindex := vxlan.Index
  81. link, err := netlink.LinkByIndex(vxlan.Index)
  82. if err != nil {
  83. return nil, fmt.Errorf("can't locate created vxlan device with index %v", ifindex)
  84. }
  85. var ok bool
  86. if vxlan, ok = link.(*netlink.Vxlan); !ok {
  87. return nil, fmt.Errorf("created vxlan device with index %v is not vxlan", ifindex)
  88. }
  89. return vxlan, nil
  90. }
  91. func (dev *vxlanDevice) Configure(ipn ip.IP4Net) error {
  92. addr := netlink.Addr{IPNet: ipn.ToIPNet()}
  93. existingAddrs, err := netlink.AddrList(dev.link, netlink.FAMILY_V4)
  94. if err != nil {
  95. return err
  96. }
  97. // flannel will never make this happen. This situation can only be caused by a user, so get them to sort it out.
  98. if len(existingAddrs) > 1 {
  99. return fmt.Errorf("link has incompatible addresses. Remove additional addresses and try again. %s", dev.link)
  100. }
  101. // If the device has an incompatible address then delete it. This can happen if the lease changes for example.
  102. if len(existingAddrs) == 1 && !existingAddrs[0].Equal(addr) {
  103. if err := netlink.AddrDel(dev.link, &existingAddrs[0]); err != nil {
  104. return fmt.Errorf("failed to remove IP address %s from %s: %s", ipn.String(), dev.link.Attrs().Name, err)
  105. }
  106. existingAddrs = []netlink.Addr{}
  107. }
  108. // Actually add the desired address to the interface if needed.
  109. if len(existingAddrs) == 0 {
  110. if err := netlink.AddrAdd(dev.link, &addr); err != nil {
  111. return fmt.Errorf("failed to add IP address %s to %s: %s", ipn.String(), dev.link.Attrs().Name, err)
  112. }
  113. }
  114. if err := netlink.LinkSetUp(dev.link); err != nil {
  115. return fmt.Errorf("failed to set interface %s to UP state: %s", dev.link.Attrs().Name, err)
  116. }
  117. return nil
  118. }
  119. func (dev *vxlanDevice) MACAddr() net.HardwareAddr {
  120. return dev.link.HardwareAddr
  121. }
  122. func (dev *vxlanDevice) MTU() int {
  123. return dev.link.MTU
  124. }
  125. type neighbor struct {
  126. MAC net.HardwareAddr
  127. IP ip.IP4
  128. }
  129. func (dev *vxlanDevice) AddFDB(n neighbor) error {
  130. log.V(4).Infof("calling AddFDB: %v, %v", n.IP, n.MAC)
  131. return netlink.NeighSet(&netlink.Neigh{
  132. LinkIndex: dev.link.Index,
  133. State: netlink.NUD_PERMANENT,
  134. Family: syscall.AF_BRIDGE,
  135. Flags: netlink.NTF_SELF,
  136. IP: n.IP.ToIP(),
  137. HardwareAddr: n.MAC,
  138. })
  139. }
  140. func (dev *vxlanDevice) DelFDB(n neighbor) error {
  141. log.V(4).Infof("calling DelFDB: %v, %v", n.IP, n.MAC)
  142. return netlink.NeighDel(&netlink.Neigh{
  143. LinkIndex: dev.link.Index,
  144. Family: syscall.AF_BRIDGE,
  145. Flags: netlink.NTF_SELF,
  146. IP: n.IP.ToIP(),
  147. HardwareAddr: n.MAC,
  148. })
  149. }
  150. func (dev *vxlanDevice) AddARP(n neighbor) error {
  151. log.V(4).Infof("calling AddARP: %v, %v", n.IP, n.MAC)
  152. return netlink.NeighSet(&netlink.Neigh{
  153. LinkIndex: dev.link.Index,
  154. State: netlink.NUD_PERMANENT,
  155. Type: syscall.RTN_UNICAST,
  156. IP: n.IP.ToIP(),
  157. HardwareAddr: n.MAC,
  158. })
  159. }
  160. func (dev *vxlanDevice) DelARP(n neighbor) error {
  161. log.V(4).Infof("calling DelARP: %v, %v", n.IP, n.MAC)
  162. return netlink.NeighDel(&netlink.Neigh{
  163. LinkIndex: dev.link.Index,
  164. State: netlink.NUD_PERMANENT,
  165. Type: syscall.RTN_UNICAST,
  166. IP: n.IP.ToIP(),
  167. HardwareAddr: n.MAC,
  168. })
  169. }
  170. func vxlanLinksIncompat(l1, l2 netlink.Link) string {
  171. if l1.Type() != l2.Type() {
  172. return fmt.Sprintf("link type: %v vs %v", l1.Type(), l2.Type())
  173. }
  174. v1 := l1.(*netlink.Vxlan)
  175. v2 := l2.(*netlink.Vxlan)
  176. if v1.VxlanId != v2.VxlanId {
  177. return fmt.Sprintf("vni: %v vs %v", v1.VxlanId, v2.VxlanId)
  178. }
  179. if v1.VtepDevIndex > 0 && v2.VtepDevIndex > 0 && v1.VtepDevIndex != v2.VtepDevIndex {
  180. return fmt.Sprintf("vtep (external) interface: %v vs %v", v1.VtepDevIndex, v2.VtepDevIndex)
  181. }
  182. if len(v1.SrcAddr) > 0 && len(v2.SrcAddr) > 0 && !v1.SrcAddr.Equal(v2.SrcAddr) {
  183. return fmt.Sprintf("vtep (external) IP: %v vs %v", v1.SrcAddr, v2.SrcAddr)
  184. }
  185. if len(v1.Group) > 0 && len(v2.Group) > 0 && !v1.Group.Equal(v2.Group) {
  186. return fmt.Sprintf("group address: %v vs %v", v1.Group, v2.Group)
  187. }
  188. if v1.L2miss != v2.L2miss {
  189. return fmt.Sprintf("l2miss: %v vs %v", v1.L2miss, v2.L2miss)
  190. }
  191. if v1.Port > 0 && v2.Port > 0 && v1.Port != v2.Port {
  192. return fmt.Sprintf("port: %v vs %v", v1.Port, v2.Port)
  193. }
  194. if v1.GBP != v2.GBP {
  195. return fmt.Sprintf("gbp: %v vs %v", v1.GBP, v2.GBP)
  196. }
  197. return ""
  198. }