endpoint_info.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. package libnetwork
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "github.com/docker/docker/libnetwork/driverapi"
  7. "github.com/docker/docker/libnetwork/types"
  8. )
  9. // EndpointInfo provides an interface to retrieve network resources bound to the endpoint.
  10. type EndpointInfo interface {
  11. // Iface returns information about the interface which was assigned to
  12. // the endpoint by the driver. This can be used after the
  13. // endpoint has been created.
  14. Iface() *EndpointInterface
  15. // Gateway returns the IPv4 gateway assigned by the driver.
  16. // This will only return a valid value if a container has joined the endpoint.
  17. Gateway() net.IP
  18. // GatewayIPv6 returns the IPv6 gateway assigned by the driver.
  19. // This will only return a valid value if a container has joined the endpoint.
  20. GatewayIPv6() net.IP
  21. // StaticRoutes returns the list of static routes configured by the network
  22. // driver when the container joins a network
  23. StaticRoutes() []*types.StaticRoute
  24. // Sandbox returns the attached sandbox if there, nil otherwise.
  25. Sandbox() *Sandbox
  26. // LoadBalancer returns whether the endpoint is the load balancer endpoint for the network.
  27. LoadBalancer() bool
  28. }
  29. // EndpointInterface holds interface addresses bound to the endpoint.
  30. type EndpointInterface struct {
  31. mac net.HardwareAddr
  32. addr *net.IPNet
  33. addrv6 *net.IPNet
  34. llAddrs []*net.IPNet
  35. srcName string
  36. dstPrefix string
  37. routes []*net.IPNet
  38. v4PoolID string
  39. v6PoolID string
  40. }
  41. func (epi *EndpointInterface) MarshalJSON() ([]byte, error) {
  42. epMap := make(map[string]interface{})
  43. if epi.mac != nil {
  44. epMap["mac"] = epi.mac.String()
  45. }
  46. if epi.addr != nil {
  47. epMap["addr"] = epi.addr.String()
  48. }
  49. if epi.addrv6 != nil {
  50. epMap["addrv6"] = epi.addrv6.String()
  51. }
  52. if len(epi.llAddrs) != 0 {
  53. list := make([]string, 0, len(epi.llAddrs))
  54. for _, ll := range epi.llAddrs {
  55. list = append(list, ll.String())
  56. }
  57. epMap["llAddrs"] = list
  58. }
  59. epMap["srcName"] = epi.srcName
  60. epMap["dstPrefix"] = epi.dstPrefix
  61. var routes []string
  62. for _, route := range epi.routes {
  63. routes = append(routes, route.String())
  64. }
  65. epMap["routes"] = routes
  66. epMap["v4PoolID"] = epi.v4PoolID
  67. epMap["v6PoolID"] = epi.v6PoolID
  68. return json.Marshal(epMap)
  69. }
  70. func (epi *EndpointInterface) UnmarshalJSON(b []byte) error {
  71. var (
  72. err error
  73. epMap map[string]interface{}
  74. )
  75. if err = json.Unmarshal(b, &epMap); err != nil {
  76. return err
  77. }
  78. if v, ok := epMap["mac"]; ok {
  79. if epi.mac, err = net.ParseMAC(v.(string)); err != nil {
  80. return types.InternalErrorf("failed to decode endpoint interface mac address after json unmarshal: %s", v.(string))
  81. }
  82. }
  83. if v, ok := epMap["addr"]; ok {
  84. if epi.addr, err = types.ParseCIDR(v.(string)); err != nil {
  85. return types.InternalErrorf("failed to decode endpoint interface ipv4 address after json unmarshal: %v", err)
  86. }
  87. }
  88. if v, ok := epMap["addrv6"]; ok {
  89. if epi.addrv6, err = types.ParseCIDR(v.(string)); err != nil {
  90. return types.InternalErrorf("failed to decode endpoint interface ipv6 address after json unmarshal: %v", err)
  91. }
  92. }
  93. if v, ok := epMap["llAddrs"]; ok {
  94. list := v.([]interface{})
  95. epi.llAddrs = make([]*net.IPNet, 0, len(list))
  96. for _, llS := range list {
  97. ll, err := types.ParseCIDR(llS.(string))
  98. if err != nil {
  99. return types.InternalErrorf("failed to decode endpoint interface link-local address (%v) after json unmarshal: %v", llS, err)
  100. }
  101. epi.llAddrs = append(epi.llAddrs, ll)
  102. }
  103. }
  104. epi.srcName = epMap["srcName"].(string)
  105. epi.dstPrefix = epMap["dstPrefix"].(string)
  106. rb, _ := json.Marshal(epMap["routes"])
  107. var routes []string
  108. // TODO(cpuguy83): linter noticed we don't check the error here... no idea why but it seems like it could introduce problems if we start checking
  109. json.Unmarshal(rb, &routes) //nolint:errcheck
  110. epi.routes = make([]*net.IPNet, 0)
  111. for _, route := range routes {
  112. ip, ipr, err := net.ParseCIDR(route)
  113. if err == nil {
  114. ipr.IP = ip
  115. epi.routes = append(epi.routes, ipr)
  116. }
  117. }
  118. epi.v4PoolID = epMap["v4PoolID"].(string)
  119. epi.v6PoolID = epMap["v6PoolID"].(string)
  120. return nil
  121. }
  122. func (epi *EndpointInterface) CopyTo(dstEpi *EndpointInterface) error {
  123. dstEpi.mac = types.GetMacCopy(epi.mac)
  124. dstEpi.addr = types.GetIPNetCopy(epi.addr)
  125. dstEpi.addrv6 = types.GetIPNetCopy(epi.addrv6)
  126. dstEpi.srcName = epi.srcName
  127. dstEpi.dstPrefix = epi.dstPrefix
  128. dstEpi.v4PoolID = epi.v4PoolID
  129. dstEpi.v6PoolID = epi.v6PoolID
  130. if len(epi.llAddrs) != 0 {
  131. dstEpi.llAddrs = make([]*net.IPNet, 0, len(epi.llAddrs))
  132. dstEpi.llAddrs = append(dstEpi.llAddrs, epi.llAddrs...)
  133. }
  134. for _, route := range epi.routes {
  135. dstEpi.routes = append(dstEpi.routes, types.GetIPNetCopy(route))
  136. }
  137. return nil
  138. }
  139. type endpointJoinInfo struct {
  140. gw net.IP
  141. gw6 net.IP
  142. StaticRoutes []*types.StaticRoute
  143. driverTableEntries []*tableEntry
  144. disableGatewayService bool
  145. }
  146. type tableEntry struct {
  147. tableName string
  148. key string
  149. value []byte
  150. }
  151. // Info hydrates the endpoint and returns certain operational data belonging
  152. // to this endpoint.
  153. //
  154. // TODO(thaJeztah): make sure that Endpoint is always fully hydrated, and remove the EndpointInfo interface, and use Endpoint directly.
  155. func (ep *Endpoint) Info() EndpointInfo {
  156. if ep.sandboxID != "" {
  157. return ep
  158. }
  159. n, err := ep.getNetworkFromStore()
  160. if err != nil {
  161. return nil
  162. }
  163. ep, err = n.getEndpointFromStore(ep.ID())
  164. if err != nil {
  165. return nil
  166. }
  167. sb, ok := ep.getSandbox()
  168. if !ok {
  169. // endpoint hasn't joined any sandbox.
  170. // Just return the endpoint
  171. return ep
  172. }
  173. return sb.getEndpoint(ep.ID())
  174. }
  175. // Iface returns information about the interface which was assigned to
  176. // the endpoint by the driver. This can be used after the
  177. // endpoint has been created.
  178. func (ep *Endpoint) Iface() *EndpointInterface {
  179. ep.mu.Lock()
  180. defer ep.mu.Unlock()
  181. return ep.iface
  182. }
  183. // SetMacAddress allows the driver to set the mac address to the endpoint interface
  184. // during the call to CreateEndpoint, if the mac address is not already set.
  185. func (epi *EndpointInterface) SetMacAddress(mac net.HardwareAddr) error {
  186. if epi.mac != nil {
  187. return types.ForbiddenErrorf("endpoint interface MAC address present (%s). Cannot be modified with %s.", epi.mac, mac)
  188. }
  189. if mac == nil {
  190. return types.InvalidParameterErrorf("tried to set nil MAC address to endpoint interface")
  191. }
  192. epi.mac = types.GetMacCopy(mac)
  193. return nil
  194. }
  195. func (epi *EndpointInterface) SetIPAddress(address *net.IPNet) error {
  196. if address.IP == nil {
  197. return types.InvalidParameterErrorf("tried to set nil IP address to endpoint interface")
  198. }
  199. if address.IP.To4() == nil {
  200. return setAddress(&epi.addrv6, address)
  201. }
  202. return setAddress(&epi.addr, address)
  203. }
  204. func setAddress(ifaceAddr **net.IPNet, address *net.IPNet) error {
  205. if *ifaceAddr != nil {
  206. return types.ForbiddenErrorf("endpoint interface IP present (%s). Cannot be modified with (%s).", *ifaceAddr, address)
  207. }
  208. *ifaceAddr = types.GetIPNetCopy(address)
  209. return nil
  210. }
  211. // MacAddress returns the MAC address assigned to the endpoint.
  212. func (epi *EndpointInterface) MacAddress() net.HardwareAddr {
  213. return types.GetMacCopy(epi.mac)
  214. }
  215. // Address returns the IPv4 address assigned to the endpoint.
  216. func (epi *EndpointInterface) Address() *net.IPNet {
  217. return types.GetIPNetCopy(epi.addr)
  218. }
  219. // AddressIPv6 returns the IPv6 address assigned to the endpoint.
  220. func (epi *EndpointInterface) AddressIPv6() *net.IPNet {
  221. return types.GetIPNetCopy(epi.addrv6)
  222. }
  223. // LinkLocalAddresses returns the list of link-local (IPv4/IPv6) addresses assigned to the endpoint.
  224. func (epi *EndpointInterface) LinkLocalAddresses() []*net.IPNet {
  225. return epi.llAddrs
  226. }
  227. // SrcName returns the name of the interface w/in the container
  228. func (epi *EndpointInterface) SrcName() string {
  229. return epi.srcName
  230. }
  231. // SetNames method assigns the srcName and dstPrefix for the interface.
  232. func (epi *EndpointInterface) SetNames(srcName string, dstPrefix string) error {
  233. epi.srcName = srcName
  234. epi.dstPrefix = dstPrefix
  235. return nil
  236. }
  237. func (ep *Endpoint) InterfaceName() driverapi.InterfaceNameInfo {
  238. ep.mu.Lock()
  239. defer ep.mu.Unlock()
  240. return ep.iface
  241. }
  242. // AddStaticRoute adds a route to the sandbox.
  243. // It may be used in addition to or instead of a default gateway (as above).
  244. func (ep *Endpoint) AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error {
  245. ep.mu.Lock()
  246. defer ep.mu.Unlock()
  247. if routeType == types.NEXTHOP {
  248. // If the route specifies a next-hop, then it's loosely routed (i.e. not bound to a particular interface).
  249. ep.joinInfo.StaticRoutes = append(ep.joinInfo.StaticRoutes, &types.StaticRoute{
  250. Destination: destination,
  251. RouteType: routeType,
  252. NextHop: nextHop,
  253. })
  254. } else {
  255. // If the route doesn't specify a next-hop, it must be a connected route, bound to an interface.
  256. ep.iface.routes = append(ep.iface.routes, destination)
  257. }
  258. return nil
  259. }
  260. // AddTableEntry adds a table entry to the gossip layer
  261. // passing the table name, key and an opaque value.
  262. func (ep *Endpoint) AddTableEntry(tableName, key string, value []byte) error {
  263. ep.mu.Lock()
  264. defer ep.mu.Unlock()
  265. ep.joinInfo.driverTableEntries = append(ep.joinInfo.driverTableEntries, &tableEntry{
  266. tableName: tableName,
  267. key: key,
  268. value: value,
  269. })
  270. return nil
  271. }
  272. // Sandbox returns the attached sandbox if there, nil otherwise.
  273. func (ep *Endpoint) Sandbox() *Sandbox {
  274. cnt, ok := ep.getSandbox()
  275. if !ok {
  276. return nil
  277. }
  278. return cnt
  279. }
  280. // LoadBalancer returns whether the endpoint is the load balancer endpoint for the network.
  281. func (ep *Endpoint) LoadBalancer() bool {
  282. ep.mu.Lock()
  283. defer ep.mu.Unlock()
  284. return ep.loadBalancer
  285. }
  286. // StaticRoutes returns the list of static routes configured by the network
  287. // driver when the container joins a network
  288. func (ep *Endpoint) StaticRoutes() []*types.StaticRoute {
  289. ep.mu.Lock()
  290. defer ep.mu.Unlock()
  291. if ep.joinInfo == nil {
  292. return nil
  293. }
  294. return ep.joinInfo.StaticRoutes
  295. }
  296. // Gateway returns the IPv4 gateway assigned by the driver.
  297. // This will only return a valid value if a container has joined the endpoint.
  298. func (ep *Endpoint) Gateway() net.IP {
  299. ep.mu.Lock()
  300. defer ep.mu.Unlock()
  301. if ep.joinInfo == nil {
  302. return net.IP{}
  303. }
  304. return types.GetIPCopy(ep.joinInfo.gw)
  305. }
  306. // GatewayIPv6 returns the IPv6 gateway assigned by the driver.
  307. // This will only return a valid value if a container has joined the endpoint.
  308. func (ep *Endpoint) GatewayIPv6() net.IP {
  309. ep.mu.Lock()
  310. defer ep.mu.Unlock()
  311. if ep.joinInfo == nil {
  312. return net.IP{}
  313. }
  314. return types.GetIPCopy(ep.joinInfo.gw6)
  315. }
  316. // SetGateway sets the default IPv4 gateway when a container joins the endpoint.
  317. func (ep *Endpoint) SetGateway(gw net.IP) error {
  318. ep.mu.Lock()
  319. defer ep.mu.Unlock()
  320. ep.joinInfo.gw = types.GetIPCopy(gw)
  321. return nil
  322. }
  323. // SetGatewayIPv6 sets the default IPv6 gateway when a container joins the endpoint.
  324. func (ep *Endpoint) SetGatewayIPv6(gw6 net.IP) error {
  325. ep.mu.Lock()
  326. defer ep.mu.Unlock()
  327. ep.joinInfo.gw6 = types.GetIPCopy(gw6)
  328. return nil
  329. }
  330. func (ep *Endpoint) retrieveFromStore() (*Endpoint, error) {
  331. n, err := ep.getNetworkFromStore()
  332. if err != nil {
  333. return nil, fmt.Errorf("could not find network in store to get latest endpoint %s: %v", ep.Name(), err)
  334. }
  335. return n.getEndpointFromStore(ep.ID())
  336. }
  337. // DisableGatewayService tells libnetwork not to provide Default GW for the container
  338. func (ep *Endpoint) DisableGatewayService() {
  339. ep.mu.Lock()
  340. defer ep.mu.Unlock()
  341. ep.joinInfo.disableGatewayService = true
  342. }
  343. func (epj *endpointJoinInfo) MarshalJSON() ([]byte, error) {
  344. epMap := make(map[string]interface{})
  345. if epj.gw != nil {
  346. epMap["gw"] = epj.gw.String()
  347. }
  348. if epj.gw6 != nil {
  349. epMap["gw6"] = epj.gw6.String()
  350. }
  351. epMap["disableGatewayService"] = epj.disableGatewayService
  352. epMap["StaticRoutes"] = epj.StaticRoutes
  353. return json.Marshal(epMap)
  354. }
  355. func (epj *endpointJoinInfo) UnmarshalJSON(b []byte) error {
  356. var (
  357. err error
  358. epMap map[string]interface{}
  359. )
  360. if err = json.Unmarshal(b, &epMap); err != nil {
  361. return err
  362. }
  363. if v, ok := epMap["gw"]; ok {
  364. epj.gw = net.ParseIP(v.(string))
  365. }
  366. if v, ok := epMap["gw6"]; ok {
  367. epj.gw6 = net.ParseIP(v.(string))
  368. }
  369. epj.disableGatewayService = epMap["disableGatewayService"].(bool)
  370. var tStaticRoute []types.StaticRoute
  371. if v, ok := epMap["StaticRoutes"]; ok {
  372. tb, _ := json.Marshal(v)
  373. var tStaticRoute []types.StaticRoute
  374. // TODO(cpuguy83): Linter caught that we aren't checking errors here
  375. // I don't know why we aren't other than potentially the data is not always expected to be right?
  376. // This is why I'm not adding the error check.
  377. //
  378. // In any case for posterity please if you figure this out document it or check the error
  379. json.Unmarshal(tb, &tStaticRoute) //nolint:errcheck
  380. }
  381. var StaticRoutes []*types.StaticRoute
  382. for _, r := range tStaticRoute {
  383. r := r
  384. StaticRoutes = append(StaticRoutes, &r)
  385. }
  386. epj.StaticRoutes = StaticRoutes
  387. return nil
  388. }
  389. func (epj *endpointJoinInfo) CopyTo(dstEpj *endpointJoinInfo) error {
  390. dstEpj.disableGatewayService = epj.disableGatewayService
  391. dstEpj.StaticRoutes = make([]*types.StaticRoute, len(epj.StaticRoutes))
  392. copy(dstEpj.StaticRoutes, epj.StaticRoutes)
  393. dstEpj.driverTableEntries = make([]*tableEntry, len(epj.driverTableEntries))
  394. copy(dstEpj.driverTableEntries, epj.driverTableEntries)
  395. dstEpj.gw = types.GetIPCopy(epj.gw)
  396. dstEpj.gw6 = types.GetIPCopy(epj.gw6)
  397. return nil
  398. }