device.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. if err := ip.EnsureV4AddressOnLink(ipn, dev.link); err != nil {
  95. return fmt.Errorf("failed to ensure address of interface %s: %s", dev.link.Attrs().Name, err)
  96. }
  97. if err := netlink.LinkSetUp(dev.link); err != nil {
  98. return fmt.Errorf("failed to set interface %s to UP state: %s", dev.link.Attrs().Name, err)
  99. }
  100. return nil
  101. }
  102. func (dev *vxlanDevice) MACAddr() net.HardwareAddr {
  103. return dev.link.HardwareAddr
  104. }
  105. func (dev *vxlanDevice) MTU() int {
  106. return dev.link.MTU
  107. }
  108. type neighbor struct {
  109. MAC net.HardwareAddr
  110. IP ip.IP4
  111. }
  112. func (dev *vxlanDevice) AddFDB(n neighbor) error {
  113. log.V(4).Infof("calling AddFDB: %v, %v", n.IP, n.MAC)
  114. return netlink.NeighSet(&netlink.Neigh{
  115. LinkIndex: dev.link.Index,
  116. State: netlink.NUD_PERMANENT,
  117. Family: syscall.AF_BRIDGE,
  118. Flags: netlink.NTF_SELF,
  119. IP: n.IP.ToIP(),
  120. HardwareAddr: n.MAC,
  121. })
  122. }
  123. func (dev *vxlanDevice) DelFDB(n neighbor) error {
  124. log.V(4).Infof("calling DelFDB: %v, %v", n.IP, n.MAC)
  125. return netlink.NeighDel(&netlink.Neigh{
  126. LinkIndex: dev.link.Index,
  127. Family: syscall.AF_BRIDGE,
  128. Flags: netlink.NTF_SELF,
  129. IP: n.IP.ToIP(),
  130. HardwareAddr: n.MAC,
  131. })
  132. }
  133. func (dev *vxlanDevice) AddARP(n neighbor) error {
  134. log.V(4).Infof("calling AddARP: %v, %v", n.IP, n.MAC)
  135. return netlink.NeighSet(&netlink.Neigh{
  136. LinkIndex: dev.link.Index,
  137. State: netlink.NUD_PERMANENT,
  138. Type: syscall.RTN_UNICAST,
  139. IP: n.IP.ToIP(),
  140. HardwareAddr: n.MAC,
  141. })
  142. }
  143. func (dev *vxlanDevice) DelARP(n neighbor) error {
  144. log.V(4).Infof("calling DelARP: %v, %v", n.IP, n.MAC)
  145. return netlink.NeighDel(&netlink.Neigh{
  146. LinkIndex: dev.link.Index,
  147. State: netlink.NUD_PERMANENT,
  148. Type: syscall.RTN_UNICAST,
  149. IP: n.IP.ToIP(),
  150. HardwareAddr: n.MAC,
  151. })
  152. }
  153. func vxlanLinksIncompat(l1, l2 netlink.Link) string {
  154. if l1.Type() != l2.Type() {
  155. return fmt.Sprintf("link type: %v vs %v", l1.Type(), l2.Type())
  156. }
  157. v1 := l1.(*netlink.Vxlan)
  158. v2 := l2.(*netlink.Vxlan)
  159. if v1.VxlanId != v2.VxlanId {
  160. return fmt.Sprintf("vni: %v vs %v", v1.VxlanId, v2.VxlanId)
  161. }
  162. if v1.VtepDevIndex > 0 && v2.VtepDevIndex > 0 && v1.VtepDevIndex != v2.VtepDevIndex {
  163. return fmt.Sprintf("vtep (external) interface: %v vs %v", v1.VtepDevIndex, v2.VtepDevIndex)
  164. }
  165. if len(v1.SrcAddr) > 0 && len(v2.SrcAddr) > 0 && !v1.SrcAddr.Equal(v2.SrcAddr) {
  166. return fmt.Sprintf("vtep (external) IP: %v vs %v", v1.SrcAddr, v2.SrcAddr)
  167. }
  168. if len(v1.Group) > 0 && len(v2.Group) > 0 && !v1.Group.Equal(v2.Group) {
  169. return fmt.Sprintf("group address: %v vs %v", v1.Group, v2.Group)
  170. }
  171. if v1.L2miss != v2.L2miss {
  172. return fmt.Sprintf("l2miss: %v vs %v", v1.L2miss, v2.L2miss)
  173. }
  174. if v1.Port > 0 && v2.Port > 0 && v1.Port != v2.Port {
  175. return fmt.Sprintf("port: %v vs %v", v1.Port, v2.Port)
  176. }
  177. if v1.GBP != v2.GBP {
  178. return fmt.Sprintf("gbp: %v vs %v", v1.GBP, v2.GBP)
  179. }
  180. return ""
  181. }