joinleave.go 6.2 KB

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