joinleave.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package overlay
  2. import (
  3. "fmt"
  4. "net"
  5. "github.com/Sirupsen/logrus"
  6. "github.com/docker/libnetwork/driverapi"
  7. "github.com/docker/libnetwork/types"
  8. "github.com/gogo/protobuf/proto"
  9. )
  10. // Join method is invoked when a Sandbox is attached to an endpoint.
  11. func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
  12. if err := validateID(nid, eid); err != nil {
  13. return err
  14. }
  15. n := d.network(nid)
  16. if n == nil {
  17. return fmt.Errorf("could not find network with id %s", nid)
  18. }
  19. ep := n.endpoint(eid)
  20. if ep == nil {
  21. return fmt.Errorf("could not find endpoint with id %s", eid)
  22. }
  23. if n.secure && len(d.keys) == 0 {
  24. return fmt.Errorf("cannot join secure network: encryption keys not present")
  25. }
  26. s := n.getSubnetforIP(ep.addr)
  27. if s == nil {
  28. return fmt.Errorf("could not find subnet for endpoint %s", eid)
  29. }
  30. if err := n.obtainVxlanID(s); err != nil {
  31. return fmt.Errorf("couldn't get vxlan id for %q: %v", s.subnetIP.String(), err)
  32. }
  33. if err := n.joinSandbox(false); err != nil {
  34. return fmt.Errorf("network sandbox join failed: %v", err)
  35. }
  36. if err := n.joinSubnetSandbox(s, false); err != nil {
  37. return fmt.Errorf("subnet sandbox join failed for %q: %v", s.subnetIP.String(), err)
  38. }
  39. // joinSubnetSandbox gets called when an endpoint comes up on a new subnet in the
  40. // overlay network. Hence the Endpoint count should be updated outside joinSubnetSandbox
  41. n.incEndpointCount()
  42. // Add creating a veth Pair for Solaris
  43. containerIfName := "solaris-if"
  44. ep.ifName = containerIfName
  45. if err := d.writeEndpointToStore(ep); err != nil {
  46. return fmt.Errorf("failed to update overlay endpoint %s to local data store: %v", ep.id[0:7], err)
  47. }
  48. // Add solaris plumbing to add veth (with ep mac addr) to sandbox
  49. for _, sub := range n.subnets {
  50. if sub == s {
  51. continue
  52. }
  53. if err := jinfo.AddStaticRoute(sub.subnetIP, types.NEXTHOP, s.gwIP.IP); err != nil {
  54. logrus.Errorf("Adding subnet %s static route in network %q failed\n", s.subnetIP, n.id)
  55. }
  56. }
  57. if iNames := jinfo.InterfaceName(); iNames != nil {
  58. err := iNames.SetNames(containerIfName, "eth")
  59. if err != nil {
  60. return err
  61. }
  62. }
  63. d.peerDbAdd(nid, eid, ep.addr.IP, ep.addr.Mask, ep.mac,
  64. net.ParseIP(d.advertiseAddress), true)
  65. if err := d.checkEncryption(nid, nil, n.vxlanID(s), true, true); err != nil {
  66. logrus.Warn(err)
  67. }
  68. buf, err := proto.Marshal(&PeerRecord{
  69. EndpointIP: ep.addr.String(),
  70. EndpointMAC: ep.mac.String(),
  71. TunnelEndpointIP: d.advertiseAddress,
  72. })
  73. if err != nil {
  74. return err
  75. }
  76. if err := jinfo.AddTableEntry(ovPeerTable, eid, buf); err != nil {
  77. logrus.Errorf("overlay: Failed adding table entry to joininfo: %v", err)
  78. }
  79. d.pushLocalEndpointEvent("join", nid, eid)
  80. return nil
  81. }
  82. func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
  83. if tableName != ovPeerTable {
  84. logrus.Errorf("Unexpected table notification for table %s received", tableName)
  85. return
  86. }
  87. eid := key
  88. var peer PeerRecord
  89. if err := proto.Unmarshal(value, &peer); err != nil {
  90. logrus.Errorf("Failed to unmarshal peer record: %v", err)
  91. return
  92. }
  93. // Ignore local peers. We already know about them and they
  94. // should not be added to vxlan fdb.
  95. if peer.TunnelEndpointIP == d.advertiseAddress {
  96. return
  97. }
  98. addr, err := types.ParseCIDR(peer.EndpointIP)
  99. if err != nil {
  100. logrus.Errorf("Invalid peer IP %s received in event notify", peer.EndpointIP)
  101. return
  102. }
  103. mac, err := net.ParseMAC(peer.EndpointMAC)
  104. if err != nil {
  105. logrus.Errorf("Invalid mac %s received in event notify", peer.EndpointMAC)
  106. return
  107. }
  108. vtep := net.ParseIP(peer.TunnelEndpointIP)
  109. if vtep == nil {
  110. logrus.Errorf("Invalid VTEP %s received in event notify", peer.TunnelEndpointIP)
  111. return
  112. }
  113. if etype == driverapi.Delete {
  114. d.peerDelete(nid, eid, addr.IP, addr.Mask, mac, vtep, true)
  115. return
  116. }
  117. d.peerAdd(nid, eid, addr.IP, addr.Mask, mac, vtep, true)
  118. }
  119. func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
  120. return "", nil
  121. }
  122. // Leave method is invoked when a Sandbox detaches from an endpoint.
  123. func (d *driver) Leave(nid, eid string) error {
  124. if err := validateID(nid, eid); err != nil {
  125. return err
  126. }
  127. n := d.network(nid)
  128. if n == nil {
  129. return fmt.Errorf("could not find network with id %s", nid)
  130. }
  131. ep := n.endpoint(eid)
  132. if ep == nil {
  133. return types.InternalMaskableErrorf("could not find endpoint with id %s", eid)
  134. }
  135. if d.notifyCh != nil {
  136. d.notifyCh <- ovNotify{
  137. action: "leave",
  138. nw: n,
  139. ep: ep,
  140. }
  141. }
  142. n.leaveSandbox()
  143. if err := d.checkEncryption(nid, nil, 0, true, false); err != nil {
  144. logrus.Warn(err)
  145. }
  146. return nil
  147. }