endpoint_info.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. func (ep *endpoint) Info() EndpointInfo {
  165. if ep.sandboxID != "" {
  166. return ep
  167. }
  168. n, err := ep.getNetworkFromStore()
  169. if err != nil {
  170. return nil
  171. }
  172. ep, err = n.getEndpointFromStore(ep.ID())
  173. if err != nil {
  174. return nil
  175. }
  176. sb, ok := ep.getSandbox()
  177. if !ok {
  178. // endpoint hasn't joined any sandbox.
  179. // Just return the endpoint
  180. return ep
  181. }
  182. return sb.getEndpoint(ep.ID())
  183. }
  184. func (ep *endpoint) Iface() InterfaceInfo {
  185. ep.Lock()
  186. defer ep.Unlock()
  187. if ep.iface != nil {
  188. return ep.iface
  189. }
  190. return nil
  191. }
  192. func (ep *endpoint) Interface() driverapi.InterfaceInfo {
  193. ep.Lock()
  194. defer ep.Unlock()
  195. if ep.iface != nil {
  196. return ep.iface
  197. }
  198. return nil
  199. }
  200. func (epi *endpointInterface) SetMacAddress(mac net.HardwareAddr) error {
  201. if epi.mac != nil {
  202. return types.ForbiddenErrorf("endpoint interface MAC address present (%s). Cannot be modified with %s.", epi.mac, mac)
  203. }
  204. if mac == nil {
  205. return types.BadRequestErrorf("tried to set nil MAC address to endpoint interface")
  206. }
  207. epi.mac = types.GetMacCopy(mac)
  208. return nil
  209. }
  210. func (epi *endpointInterface) SetIPAddress(address *net.IPNet) error {
  211. if address.IP == nil {
  212. return types.BadRequestErrorf("tried to set nil IP address to endpoint interface")
  213. }
  214. if address.IP.To4() == nil {
  215. return setAddress(&epi.addrv6, address)
  216. }
  217. return setAddress(&epi.addr, address)
  218. }
  219. func setAddress(ifaceAddr **net.IPNet, address *net.IPNet) error {
  220. if *ifaceAddr != nil {
  221. return types.ForbiddenErrorf("endpoint interface IP present (%s). Cannot be modified with (%s).", *ifaceAddr, address)
  222. }
  223. *ifaceAddr = types.GetIPNetCopy(address)
  224. return nil
  225. }
  226. func (epi *endpointInterface) MacAddress() net.HardwareAddr {
  227. return types.GetMacCopy(epi.mac)
  228. }
  229. func (epi *endpointInterface) Address() *net.IPNet {
  230. return types.GetIPNetCopy(epi.addr)
  231. }
  232. func (epi *endpointInterface) AddressIPv6() *net.IPNet {
  233. return types.GetIPNetCopy(epi.addrv6)
  234. }
  235. func (epi *endpointInterface) LinkLocalAddresses() []*net.IPNet {
  236. return epi.llAddrs
  237. }
  238. func (epi *endpointInterface) SrcName() string {
  239. return epi.srcName
  240. }
  241. func (epi *endpointInterface) SetNames(srcName string, dstPrefix string) error {
  242. epi.srcName = srcName
  243. epi.dstPrefix = dstPrefix
  244. return nil
  245. }
  246. func (ep *endpoint) InterfaceName() driverapi.InterfaceNameInfo {
  247. ep.Lock()
  248. defer ep.Unlock()
  249. if ep.iface != nil {
  250. return ep.iface
  251. }
  252. return nil
  253. }
  254. func (ep *endpoint) AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error {
  255. ep.Lock()
  256. defer ep.Unlock()
  257. r := types.StaticRoute{Destination: destination, RouteType: routeType, NextHop: nextHop}
  258. if routeType == types.NEXTHOP {
  259. // If the route specifies a next-hop, then it's loosely routed (i.e. not bound to a particular interface).
  260. ep.joinInfo.StaticRoutes = append(ep.joinInfo.StaticRoutes, &r)
  261. } else {
  262. // If the route doesn't specify a next-hop, it must be a connected route, bound to an interface.
  263. ep.iface.routes = append(ep.iface.routes, r.Destination)
  264. }
  265. return nil
  266. }
  267. func (ep *endpoint) AddTableEntry(tableName, key string, value []byte) error {
  268. ep.Lock()
  269. defer ep.Unlock()
  270. ep.joinInfo.driverTableEntries = append(ep.joinInfo.driverTableEntries, &tableEntry{
  271. tableName: tableName,
  272. key: key,
  273. value: value,
  274. })
  275. return nil
  276. }
  277. func (ep *endpoint) Sandbox() Sandbox {
  278. cnt, ok := ep.getSandbox()
  279. if !ok {
  280. return nil
  281. }
  282. return cnt
  283. }
  284. func (ep *endpoint) LoadBalancer() bool {
  285. ep.Lock()
  286. defer ep.Unlock()
  287. return ep.loadBalancer
  288. }
  289. func (ep *endpoint) StaticRoutes() []*types.StaticRoute {
  290. ep.Lock()
  291. defer ep.Unlock()
  292. if ep.joinInfo == nil {
  293. return nil
  294. }
  295. return ep.joinInfo.StaticRoutes
  296. }
  297. func (ep *endpoint) Gateway() net.IP {
  298. ep.Lock()
  299. defer ep.Unlock()
  300. if ep.joinInfo == nil {
  301. return net.IP{}
  302. }
  303. return types.GetIPCopy(ep.joinInfo.gw)
  304. }
  305. func (ep *endpoint) GatewayIPv6() net.IP {
  306. ep.Lock()
  307. defer ep.Unlock()
  308. if ep.joinInfo == nil {
  309. return net.IP{}
  310. }
  311. return types.GetIPCopy(ep.joinInfo.gw6)
  312. }
  313. func (ep *endpoint) SetGateway(gw net.IP) error {
  314. ep.Lock()
  315. defer ep.Unlock()
  316. ep.joinInfo.gw = types.GetIPCopy(gw)
  317. return nil
  318. }
  319. func (ep *endpoint) SetGatewayIPv6(gw6 net.IP) error {
  320. ep.Lock()
  321. defer ep.Unlock()
  322. ep.joinInfo.gw6 = types.GetIPCopy(gw6)
  323. return nil
  324. }
  325. func (ep *endpoint) retrieveFromStore() (*endpoint, error) {
  326. n, err := ep.getNetworkFromStore()
  327. if err != nil {
  328. return nil, fmt.Errorf("could not find network in store to get latest endpoint %s: %v", ep.Name(), err)
  329. }
  330. return n.getEndpointFromStore(ep.ID())
  331. }
  332. func (ep *endpoint) DisableGatewayService() {
  333. ep.Lock()
  334. defer ep.Unlock()
  335. ep.joinInfo.disableGatewayService = true
  336. }
  337. func (epj *endpointJoinInfo) MarshalJSON() ([]byte, error) {
  338. epMap := make(map[string]interface{})
  339. if epj.gw != nil {
  340. epMap["gw"] = epj.gw.String()
  341. }
  342. if epj.gw6 != nil {
  343. epMap["gw6"] = epj.gw6.String()
  344. }
  345. epMap["disableGatewayService"] = epj.disableGatewayService
  346. epMap["StaticRoutes"] = epj.StaticRoutes
  347. return json.Marshal(epMap)
  348. }
  349. func (epj *endpointJoinInfo) UnmarshalJSON(b []byte) error {
  350. var (
  351. err error
  352. epMap map[string]interface{}
  353. )
  354. if err = json.Unmarshal(b, &epMap); err != nil {
  355. return err
  356. }
  357. if v, ok := epMap["gw"]; ok {
  358. epj.gw = net.ParseIP(v.(string))
  359. }
  360. if v, ok := epMap["gw6"]; ok {
  361. epj.gw6 = net.ParseIP(v.(string))
  362. }
  363. epj.disableGatewayService = epMap["disableGatewayService"].(bool)
  364. var tStaticRoute []types.StaticRoute
  365. if v, ok := epMap["StaticRoutes"]; ok {
  366. tb, _ := json.Marshal(v)
  367. var tStaticRoute []types.StaticRoute
  368. // TODO(cpuguy83): Linter caught that we aren't checking errors here
  369. // I don't know why we aren't other than potentially the data is not always expected to be right?
  370. // This is why I'm not adding the error check.
  371. //
  372. // In any case for posterity please if you figure this out document it or check the error
  373. json.Unmarshal(tb, &tStaticRoute) // nolint:errcheck
  374. }
  375. var StaticRoutes []*types.StaticRoute
  376. for _, r := range tStaticRoute {
  377. r := r
  378. StaticRoutes = append(StaticRoutes, &r)
  379. }
  380. epj.StaticRoutes = StaticRoutes
  381. return nil
  382. }
  383. func (epj *endpointJoinInfo) CopyTo(dstEpj *endpointJoinInfo) error {
  384. dstEpj.disableGatewayService = epj.disableGatewayService
  385. dstEpj.StaticRoutes = make([]*types.StaticRoute, len(epj.StaticRoutes))
  386. copy(dstEpj.StaticRoutes, epj.StaticRoutes)
  387. dstEpj.driverTableEntries = make([]*tableEntry, len(epj.driverTableEntries))
  388. copy(dstEpj.driverTableEntries, epj.driverTableEntries)
  389. dstEpj.gw = types.GetIPCopy(epj.gw)
  390. dstEpj.gw6 = types.GetIPCopy(epj.gw6)
  391. return nil
  392. }