device.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. "github.com/containernetworking/plugins/pkg/utils/sysctl"
  25. )
  26. type vxlanDeviceAttrs struct {
  27. vni uint32
  28. name string
  29. vtepIndex int
  30. vtepAddr net.IP
  31. vtepPort int
  32. gbp bool
  33. learning bool
  34. }
  35. type vxlanDevice struct {
  36. link *netlink.Vxlan
  37. directRouting bool
  38. }
  39. func newVXLANDevice(devAttrs *vxlanDeviceAttrs) (*vxlanDevice, error) {
  40. link := &netlink.Vxlan{
  41. LinkAttrs: netlink.LinkAttrs{
  42. Name: devAttrs.name,
  43. },
  44. VxlanId: int(devAttrs.vni),
  45. VtepDevIndex: devAttrs.vtepIndex,
  46. SrcAddr: devAttrs.vtepAddr,
  47. Port: devAttrs.vtepPort,
  48. Learning: devAttrs.learning,
  49. GBP: devAttrs.gbp,
  50. }
  51. link, err := ensureLink(link)
  52. if err != nil {
  53. return nil, err
  54. }
  55. _, _ = sysctl.Sysctl(fmt.Sprintf("net/ipv6/conf/%s/accept_ra", devAttrs.name), "0")
  56. return &vxlanDevice{
  57. link: link,
  58. }, nil
  59. }
  60. func ensureLink(vxlan *netlink.Vxlan) (*netlink.Vxlan, error) {
  61. err := netlink.LinkAdd(vxlan)
  62. if err == syscall.EEXIST {
  63. // it's ok if the device already exists as long as config is similar
  64. log.V(1).Infof("VXLAN device already exists")
  65. existing, err := netlink.LinkByName(vxlan.Name)
  66. if err != nil {
  67. return nil, err
  68. }
  69. incompat := vxlanLinksIncompat(vxlan, existing)
  70. if incompat == "" {
  71. log.V(1).Infof("Returning existing device")
  72. return existing.(*netlink.Vxlan), nil
  73. }
  74. // delete existing
  75. log.Warningf("%q already exists with incompatable configuration: %v; recreating device", vxlan.Name, incompat)
  76. if err = netlink.LinkDel(existing); err != nil {
  77. return nil, fmt.Errorf("failed to delete interface: %v", err)
  78. }
  79. // create new
  80. if err = netlink.LinkAdd(vxlan); err != nil {
  81. return nil, fmt.Errorf("failed to create vxlan interface: %v", err)
  82. }
  83. } else if err != nil {
  84. return nil, err
  85. }
  86. ifindex := vxlan.Index
  87. link, err := netlink.LinkByIndex(vxlan.Index)
  88. if err != nil {
  89. return nil, fmt.Errorf("can't locate created vxlan device with index %v", ifindex)
  90. }
  91. var ok bool
  92. if vxlan, ok = link.(*netlink.Vxlan); !ok {
  93. return nil, fmt.Errorf("created vxlan device with index %v is not vxlan", ifindex)
  94. }
  95. return vxlan, nil
  96. }
  97. func (dev *vxlanDevice) Configure(ipn ip.IP4Net) error {
  98. if err := ip.EnsureV4AddressOnLink(ipn, dev.link); err != nil {
  99. return fmt.Errorf("failed to ensure address of interface %s: %s", dev.link.Attrs().Name, err)
  100. }
  101. if err := netlink.LinkSetUp(dev.link); err != nil {
  102. return fmt.Errorf("failed to set interface %s to UP state: %s", dev.link.Attrs().Name, err)
  103. }
  104. return nil
  105. }
  106. func (dev *vxlanDevice) MACAddr() net.HardwareAddr {
  107. return dev.link.HardwareAddr
  108. }
  109. type neighbor struct {
  110. MAC net.HardwareAddr
  111. IP ip.IP4
  112. }
  113. func (dev *vxlanDevice) AddFDB(n neighbor) error {
  114. log.V(4).Infof("calling AddFDB: %v, %v", n.IP, n.MAC)
  115. return netlink.NeighSet(&netlink.Neigh{
  116. LinkIndex: dev.link.Index,
  117. State: netlink.NUD_PERMANENT,
  118. Family: syscall.AF_BRIDGE,
  119. Flags: netlink.NTF_SELF,
  120. IP: n.IP.ToIP(),
  121. HardwareAddr: n.MAC,
  122. })
  123. }
  124. func (dev *vxlanDevice) DelFDB(n neighbor) error {
  125. log.V(4).Infof("calling DelFDB: %v, %v", n.IP, n.MAC)
  126. return netlink.NeighDel(&netlink.Neigh{
  127. LinkIndex: dev.link.Index,
  128. Family: syscall.AF_BRIDGE,
  129. Flags: netlink.NTF_SELF,
  130. IP: n.IP.ToIP(),
  131. HardwareAddr: n.MAC,
  132. })
  133. }
  134. func (dev *vxlanDevice) AddARP(n neighbor) error {
  135. log.V(4).Infof("calling AddARP: %v, %v", n.IP, n.MAC)
  136. return netlink.NeighSet(&netlink.Neigh{
  137. LinkIndex: dev.link.Index,
  138. State: netlink.NUD_PERMANENT,
  139. Type: syscall.RTN_UNICAST,
  140. IP: n.IP.ToIP(),
  141. HardwareAddr: n.MAC,
  142. })
  143. }
  144. func (dev *vxlanDevice) DelARP(n neighbor) error {
  145. log.V(4).Infof("calling DelARP: %v, %v", n.IP, n.MAC)
  146. return netlink.NeighDel(&netlink.Neigh{
  147. LinkIndex: dev.link.Index,
  148. State: netlink.NUD_PERMANENT,
  149. Type: syscall.RTN_UNICAST,
  150. IP: n.IP.ToIP(),
  151. HardwareAddr: n.MAC,
  152. })
  153. }
  154. func vxlanLinksIncompat(l1, l2 netlink.Link) string {
  155. if l1.Type() != l2.Type() {
  156. return fmt.Sprintf("link type: %v vs %v", l1.Type(), l2.Type())
  157. }
  158. v1 := l1.(*netlink.Vxlan)
  159. v2 := l2.(*netlink.Vxlan)
  160. if v1.VxlanId != v2.VxlanId {
  161. return fmt.Sprintf("vni: %v vs %v", v1.VxlanId, v2.VxlanId)
  162. }
  163. if v1.VtepDevIndex > 0 && v2.VtepDevIndex > 0 && v1.VtepDevIndex != v2.VtepDevIndex {
  164. return fmt.Sprintf("vtep (external) interface: %v vs %v", v1.VtepDevIndex, v2.VtepDevIndex)
  165. }
  166. if len(v1.SrcAddr) > 0 && len(v2.SrcAddr) > 0 && !v1.SrcAddr.Equal(v2.SrcAddr) {
  167. return fmt.Sprintf("vtep (external) IP: %v vs %v", v1.SrcAddr, v2.SrcAddr)
  168. }
  169. if len(v1.Group) > 0 && len(v2.Group) > 0 && !v1.Group.Equal(v2.Group) {
  170. return fmt.Sprintf("group address: %v vs %v", v1.Group, v2.Group)
  171. }
  172. if v1.L2miss != v2.L2miss {
  173. return fmt.Sprintf("l2miss: %v vs %v", v1.L2miss, v2.L2miss)
  174. }
  175. if v1.Port > 0 && v2.Port > 0 && v1.Port != v2.Port {
  176. return fmt.Sprintf("port: %v vs %v", v1.Port, v2.Port)
  177. }
  178. if v1.GBP != v2.GBP {
  179. return fmt.Sprintf("gbp: %v vs %v", v1.GBP, v2.GBP)
  180. }
  181. return ""
  182. }