device.go 6.5 KB

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