endpoint_info.go 12 KB

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