sandbox_linux.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package libnetwork
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "time"
  7. "github.com/containerd/log"
  8. "github.com/docker/docker/libnetwork/netutils"
  9. "github.com/docker/docker/libnetwork/osl"
  10. "github.com/docker/docker/libnetwork/types"
  11. )
  12. func releaseOSSboxResources(ns *osl.Namespace, ep *Endpoint) {
  13. for _, i := range ns.Interfaces() {
  14. // Only remove the interfaces owned by this endpoint from the sandbox.
  15. if ep.hasInterface(i.SrcName()) {
  16. if err := i.Remove(); err != nil {
  17. log.G(context.TODO()).Debugf("Remove interface %s failed: %v", i.SrcName(), err)
  18. }
  19. }
  20. }
  21. ep.mu.Lock()
  22. joinInfo := ep.joinInfo
  23. vip := ep.virtualIP
  24. lbModeIsDSR := ep.network.loadBalancerMode == loadBalancerModeDSR
  25. ep.mu.Unlock()
  26. if len(vip) > 0 && lbModeIsDSR {
  27. ipNet := &net.IPNet{IP: vip, Mask: net.CIDRMask(32, 32)}
  28. if err := ns.RemoveAliasIP(ns.GetLoopbackIfaceName(), ipNet); err != nil {
  29. log.G(context.TODO()).WithError(err).Debugf("failed to remove virtual ip %v to loopback", ipNet)
  30. }
  31. }
  32. if joinInfo == nil {
  33. return
  34. }
  35. // Remove non-interface routes.
  36. for _, r := range joinInfo.StaticRoutes {
  37. if err := ns.RemoveStaticRoute(r); err != nil {
  38. log.G(context.TODO()).Debugf("Remove route failed: %v", err)
  39. }
  40. }
  41. }
  42. // Statistics retrieves the interfaces' statistics for the sandbox.
  43. func (sb *Sandbox) Statistics() (map[string]*types.InterfaceStatistics, error) {
  44. m := make(map[string]*types.InterfaceStatistics)
  45. sb.mu.Lock()
  46. osb := sb.osSbox
  47. sb.mu.Unlock()
  48. if osb == nil {
  49. return m, nil
  50. }
  51. var err error
  52. for _, i := range osb.Interfaces() {
  53. if m[i.DstName()], err = i.Statistics(); err != nil {
  54. return m, err
  55. }
  56. }
  57. return m, nil
  58. }
  59. func (sb *Sandbox) updateGateway(ep *Endpoint) error {
  60. sb.mu.Lock()
  61. osSbox := sb.osSbox
  62. sb.mu.Unlock()
  63. if osSbox == nil {
  64. return nil
  65. }
  66. osSbox.UnsetGateway() //nolint:errcheck
  67. osSbox.UnsetGatewayIPv6() //nolint:errcheck
  68. if ep == nil {
  69. return nil
  70. }
  71. ep.mu.Lock()
  72. joinInfo := ep.joinInfo
  73. ep.mu.Unlock()
  74. if err := osSbox.SetGateway(joinInfo.gw); err != nil {
  75. return fmt.Errorf("failed to set gateway while updating gateway: %v", err)
  76. }
  77. if err := osSbox.SetGatewayIPv6(joinInfo.gw6); err != nil {
  78. return fmt.Errorf("failed to set IPv6 gateway while updating gateway: %v", err)
  79. }
  80. return nil
  81. }
  82. func (sb *Sandbox) ExecFunc(f func()) error {
  83. sb.mu.Lock()
  84. osSbox := sb.osSbox
  85. sb.mu.Unlock()
  86. if osSbox != nil {
  87. return osSbox.InvokeFunc(f)
  88. }
  89. return fmt.Errorf("osl sandbox unavailable in ExecFunc for %v", sb.ContainerID())
  90. }
  91. // SetKey updates the Sandbox Key.
  92. func (sb *Sandbox) SetKey(basePath string) error {
  93. start := time.Now()
  94. defer func() {
  95. log.G(context.TODO()).Debugf("sandbox set key processing took %s for container %s", time.Since(start), sb.ContainerID())
  96. }()
  97. if basePath == "" {
  98. return types.InvalidParameterErrorf("invalid sandbox key")
  99. }
  100. sb.mu.Lock()
  101. if sb.inDelete {
  102. sb.mu.Unlock()
  103. return types.ForbiddenErrorf("failed to SetKey: sandbox %q delete in progress", sb.id)
  104. }
  105. oldosSbox := sb.osSbox
  106. sb.mu.Unlock()
  107. if oldosSbox != nil {
  108. // If we already have an OS sandbox, release the network resources from that
  109. // and destroy the OS snab. We are moving into a new home further down. Note that none
  110. // of the network resources gets destroyed during the move.
  111. if err := sb.releaseOSSbox(); err != nil {
  112. log.G(context.TODO()).WithError(err).Error("Error destroying os sandbox")
  113. }
  114. }
  115. osSbox, err := osl.GetSandboxForExternalKey(basePath, sb.Key())
  116. if err != nil {
  117. return err
  118. }
  119. sb.mu.Lock()
  120. sb.osSbox = osSbox
  121. sb.mu.Unlock()
  122. // If the resolver was setup before stop it and set it up in the
  123. // new osl sandbox.
  124. if oldosSbox != nil && sb.resolver != nil {
  125. sb.resolver.Stop()
  126. if err := sb.osSbox.InvokeFunc(sb.resolver.SetupFunc(0)); err == nil {
  127. if err := sb.resolver.Start(); err != nil {
  128. log.G(context.TODO()).Errorf("Resolver Start failed for container %s, %q", sb.ContainerID(), err)
  129. }
  130. } else {
  131. log.G(context.TODO()).Errorf("Resolver Setup Function failed for container %s, %q", sb.ContainerID(), err)
  132. }
  133. }
  134. if err := sb.finishInitDNS(); err != nil {
  135. return err
  136. }
  137. for _, ep := range sb.Endpoints() {
  138. if err = sb.populateNetworkResources(ep); err != nil {
  139. return err
  140. }
  141. }
  142. return nil
  143. }
  144. // IPv6 support can always be determined for host networking. For other network
  145. // types it can only be determined once there's a container namespace to probe,
  146. // return ok=false in that case.
  147. func (sb *Sandbox) ipv6Enabled() (enabled, ok bool) {
  148. // For host networking, IPv6 support depends on the host.
  149. if sb.config.useDefaultSandBox {
  150. return netutils.IsV6Listenable(), true
  151. }
  152. // For other network types, look at whether the container's loopback interface has an IPv6 address.
  153. sb.mu.Lock()
  154. osSbox := sb.osSbox
  155. sb.mu.Unlock()
  156. if osSbox == nil {
  157. return false, false
  158. }
  159. return osSbox.IPv6LoEnabled(), true
  160. }
  161. func (sb *Sandbox) releaseOSSbox() error {
  162. sb.mu.Lock()
  163. osSbox := sb.osSbox
  164. sb.osSbox = nil
  165. sb.mu.Unlock()
  166. if osSbox == nil {
  167. return nil
  168. }
  169. for _, ep := range sb.Endpoints() {
  170. releaseOSSboxResources(osSbox, ep)
  171. }
  172. return osSbox.Destroy()
  173. }
  174. func (sb *Sandbox) restoreOslSandbox() error {
  175. var routes []*types.StaticRoute
  176. // restore osl sandbox
  177. interfaces := make(map[osl.Iface][]osl.IfaceOption)
  178. for _, ep := range sb.endpoints {
  179. ep.mu.Lock()
  180. joinInfo := ep.joinInfo
  181. i := ep.iface
  182. ep.mu.Unlock()
  183. if i == nil {
  184. log.G(context.TODO()).Errorf("error restoring endpoint %s for container %s", ep.Name(), sb.ContainerID())
  185. continue
  186. }
  187. ifaceOptions := []osl.IfaceOption{
  188. osl.WithIPv4Address(i.addr),
  189. osl.WithRoutes(i.routes),
  190. }
  191. if i.addrv6 != nil && i.addrv6.IP.To16() != nil {
  192. ifaceOptions = append(ifaceOptions, osl.WithIPv6Address(i.addrv6))
  193. }
  194. if i.mac != nil {
  195. ifaceOptions = append(ifaceOptions, osl.WithMACAddress(i.mac))
  196. }
  197. if len(i.llAddrs) != 0 {
  198. ifaceOptions = append(ifaceOptions, osl.WithLinkLocalAddresses(i.llAddrs))
  199. }
  200. interfaces[osl.Iface{SrcName: i.srcName, DstPrefix: i.dstPrefix}] = ifaceOptions
  201. if joinInfo != nil {
  202. routes = append(routes, joinInfo.StaticRoutes...)
  203. }
  204. if ep.needResolver() {
  205. sb.startResolver(true)
  206. }
  207. }
  208. gwep := sb.getGatewayEndpoint()
  209. if gwep == nil {
  210. return nil
  211. }
  212. // restore osl sandbox
  213. return sb.osSbox.Restore(interfaces, routes, gwep.joinInfo.gw, gwep.joinInfo.gw6)
  214. }
  215. func (sb *Sandbox) populateNetworkResources(ep *Endpoint) error {
  216. sb.mu.Lock()
  217. if sb.osSbox == nil {
  218. sb.mu.Unlock()
  219. return nil
  220. }
  221. inDelete := sb.inDelete
  222. sb.mu.Unlock()
  223. ep.mu.Lock()
  224. joinInfo := ep.joinInfo
  225. i := ep.iface
  226. lbModeIsDSR := ep.network.loadBalancerMode == loadBalancerModeDSR
  227. ep.mu.Unlock()
  228. if ep.needResolver() {
  229. sb.startResolver(false)
  230. }
  231. if i != nil && i.srcName != "" {
  232. var ifaceOptions []osl.IfaceOption
  233. ifaceOptions = append(ifaceOptions, osl.WithIPv4Address(i.addr), osl.WithRoutes(i.routes))
  234. if i.addrv6 != nil && i.addrv6.IP.To16() != nil {
  235. ifaceOptions = append(ifaceOptions, osl.WithIPv6Address(i.addrv6))
  236. }
  237. if len(i.llAddrs) != 0 {
  238. ifaceOptions = append(ifaceOptions, osl.WithLinkLocalAddresses(i.llAddrs))
  239. }
  240. if i.mac != nil {
  241. ifaceOptions = append(ifaceOptions, osl.WithMACAddress(i.mac))
  242. }
  243. if err := sb.osSbox.AddInterface(i.srcName, i.dstPrefix, ifaceOptions...); err != nil {
  244. return fmt.Errorf("failed to add interface %s to sandbox: %v", i.srcName, err)
  245. }
  246. if len(ep.virtualIP) > 0 && lbModeIsDSR {
  247. if sb.loadBalancerNID == "" {
  248. if err := sb.osSbox.DisableARPForVIP(i.srcName); err != nil {
  249. return fmt.Errorf("failed disable ARP for VIP: %v", err)
  250. }
  251. }
  252. ipNet := &net.IPNet{IP: ep.virtualIP, Mask: net.CIDRMask(32, 32)}
  253. if err := sb.osSbox.AddAliasIP(sb.osSbox.GetLoopbackIfaceName(), ipNet); err != nil {
  254. return fmt.Errorf("failed to add virtual ip %v to loopback: %v", ipNet, err)
  255. }
  256. }
  257. }
  258. if joinInfo != nil {
  259. // Set up non-interface routes.
  260. for _, r := range joinInfo.StaticRoutes {
  261. if err := sb.osSbox.AddStaticRoute(r); err != nil {
  262. return fmt.Errorf("failed to add static route %s: %v", r.Destination.String(), err)
  263. }
  264. }
  265. }
  266. if ep == sb.getGatewayEndpoint() {
  267. if err := sb.updateGateway(ep); err != nil {
  268. return err
  269. }
  270. }
  271. // Make sure to add the endpoint to the populated endpoint set
  272. // before populating loadbalancers.
  273. sb.mu.Lock()
  274. sb.populatedEndpoints[ep.ID()] = struct{}{}
  275. sb.mu.Unlock()
  276. // Populate load balancer only after updating all the other
  277. // information including gateway and other routes so that
  278. // loadbalancers are populated all the network state is in
  279. // place in the sandbox.
  280. sb.populateLoadBalancers(ep)
  281. // Only update the store if we did not come here as part of
  282. // sandbox delete. If we came here as part of delete then do
  283. // not bother updating the store. The sandbox object will be
  284. // deleted anyway
  285. if !inDelete {
  286. return sb.storeUpdate()
  287. }
  288. return nil
  289. }