joinleave.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //go:build linux
  2. package overlay
  3. import (
  4. "context"
  5. "fmt"
  6. "net"
  7. "syscall"
  8. "github.com/containerd/log"
  9. "github.com/docker/docker/libnetwork/driverapi"
  10. "github.com/docker/docker/libnetwork/ns"
  11. "github.com/docker/docker/libnetwork/osl"
  12. "github.com/docker/docker/libnetwork/types"
  13. "github.com/gogo/protobuf/proto"
  14. )
  15. // Join method is invoked when a Sandbox is attached to an endpoint.
  16. func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
  17. if err := validateID(nid, eid); err != nil {
  18. return err
  19. }
  20. n := d.network(nid)
  21. if n == nil {
  22. return fmt.Errorf("could not find network with id %s", nid)
  23. }
  24. ep := n.endpoint(eid)
  25. if ep == nil {
  26. return fmt.Errorf("could not find endpoint with id %s", eid)
  27. }
  28. if n.secure && len(d.keys) == 0 {
  29. return fmt.Errorf("cannot join secure network: encryption keys not present")
  30. }
  31. nlh := ns.NlHandle()
  32. if n.secure && !nlh.SupportsNetlinkFamily(syscall.NETLINK_XFRM) {
  33. return fmt.Errorf("cannot join secure network: required modules to install IPSEC rules are missing on host")
  34. }
  35. s := n.getSubnetforIP(ep.addr)
  36. if s == nil {
  37. return fmt.Errorf("could not find subnet for endpoint %s", eid)
  38. }
  39. if err := n.joinSandbox(s, true); err != nil {
  40. return fmt.Errorf("network sandbox join failed: %v", err)
  41. }
  42. sbox := n.sandbox()
  43. overlayIfName, containerIfName, err := createVethPair()
  44. if err != nil {
  45. return err
  46. }
  47. ep.ifName = containerIfName
  48. // Set the container interface and its peer MTU to 1450 to allow
  49. // for 50 bytes vxlan encap (inner eth header(14) + outer IP(20) +
  50. // outer UDP(8) + vxlan header(8))
  51. mtu := n.maxMTU()
  52. veth, err := nlh.LinkByName(overlayIfName)
  53. if err != nil {
  54. return fmt.Errorf("cound not find link by name %s: %v", overlayIfName, err)
  55. }
  56. err = nlh.LinkSetMTU(veth, mtu)
  57. if err != nil {
  58. return err
  59. }
  60. if err = sbox.AddInterface(overlayIfName, "veth", osl.WithMaster(s.brName)); err != nil {
  61. return fmt.Errorf("could not add veth pair inside the network sandbox: %v", err)
  62. }
  63. veth, err = nlh.LinkByName(containerIfName)
  64. if err != nil {
  65. return fmt.Errorf("could not find link by name %s: %v", containerIfName, err)
  66. }
  67. err = nlh.LinkSetMTU(veth, mtu)
  68. if err != nil {
  69. return err
  70. }
  71. if err = nlh.LinkSetHardwareAddr(veth, ep.mac); err != nil {
  72. return fmt.Errorf("could not set mac address (%v) to the container interface: %v", ep.mac, err)
  73. }
  74. for _, sub := range n.subnets {
  75. if sub == s {
  76. continue
  77. }
  78. if err = jinfo.AddStaticRoute(sub.subnetIP, types.NEXTHOP, s.gwIP.IP); err != nil {
  79. log.G(context.TODO()).Errorf("Adding subnet %s static route in network %q failed\n", s.subnetIP, n.id)
  80. }
  81. }
  82. if iNames := jinfo.InterfaceName(); iNames != nil {
  83. err = iNames.SetNames(containerIfName, "eth")
  84. if err != nil {
  85. return err
  86. }
  87. }
  88. d.peerAdd(nid, eid, ep.addr.IP, ep.addr.Mask, ep.mac, d.advertiseAddress, false, false, true)
  89. if err = d.checkEncryption(nid, nil, true, true); err != nil {
  90. log.G(context.TODO()).Warn(err)
  91. }
  92. buf, err := proto.Marshal(&PeerRecord{
  93. EndpointIP: ep.addr.String(),
  94. EndpointMAC: ep.mac.String(),
  95. TunnelEndpointIP: d.advertiseAddress.String(),
  96. })
  97. if err != nil {
  98. return err
  99. }
  100. if err := jinfo.AddTableEntry(ovPeerTable, eid, buf); err != nil {
  101. log.G(context.TODO()).Errorf("overlay: Failed adding table entry to joininfo: %v", err)
  102. }
  103. return nil
  104. }
  105. func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
  106. if tablename != ovPeerTable {
  107. log.G(context.TODO()).Errorf("DecodeTableEntry: unexpected table name %s", tablename)
  108. return "", nil
  109. }
  110. var peer PeerRecord
  111. if err := proto.Unmarshal(value, &peer); err != nil {
  112. log.G(context.TODO()).Errorf("DecodeTableEntry: failed to unmarshal peer record for key %s: %v", key, err)
  113. return "", nil
  114. }
  115. return key, map[string]string{
  116. "Host IP": peer.TunnelEndpointIP,
  117. }
  118. }
  119. func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
  120. if tableName != ovPeerTable {
  121. log.G(context.TODO()).Errorf("Unexpected table notification for table %s received", tableName)
  122. return
  123. }
  124. eid := key
  125. var peer PeerRecord
  126. if err := proto.Unmarshal(value, &peer); err != nil {
  127. log.G(context.TODO()).Errorf("Failed to unmarshal peer record: %v", err)
  128. return
  129. }
  130. // Ignore local peers. We already know about them and they
  131. // should not be added to vxlan fdb.
  132. if net.ParseIP(peer.TunnelEndpointIP).Equal(d.advertiseAddress) {
  133. return
  134. }
  135. addr, err := types.ParseCIDR(peer.EndpointIP)
  136. if err != nil {
  137. log.G(context.TODO()).Errorf("Invalid peer IP %s received in event notify", peer.EndpointIP)
  138. return
  139. }
  140. mac, err := net.ParseMAC(peer.EndpointMAC)
  141. if err != nil {
  142. log.G(context.TODO()).Errorf("Invalid mac %s received in event notify", peer.EndpointMAC)
  143. return
  144. }
  145. vtep := net.ParseIP(peer.TunnelEndpointIP)
  146. if vtep == nil {
  147. log.G(context.TODO()).Errorf("Invalid VTEP %s received in event notify", peer.TunnelEndpointIP)
  148. return
  149. }
  150. if etype == driverapi.Delete {
  151. d.peerDelete(nid, eid, addr.IP, addr.Mask, mac, vtep, false)
  152. return
  153. }
  154. d.peerAdd(nid, eid, addr.IP, addr.Mask, mac, vtep, false, false, false)
  155. }
  156. // Leave method is invoked when a Sandbox detaches from an endpoint.
  157. func (d *driver) Leave(nid, eid string) error {
  158. if err := validateID(nid, eid); err != nil {
  159. return err
  160. }
  161. n := d.network(nid)
  162. if n == nil {
  163. return fmt.Errorf("could not find network with id %s", nid)
  164. }
  165. ep := n.endpoint(eid)
  166. if ep == nil {
  167. return types.InternalMaskableErrorf("could not find endpoint with id %s", eid)
  168. }
  169. d.peerDelete(nid, eid, ep.addr.IP, ep.addr.Mask, ep.mac, d.advertiseAddress, true)
  170. n.leaveSandbox()
  171. return nil
  172. }