endpoint_info.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. package libnetwork
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "github.com/docker/libnetwork/driverapi"
  7. "github.com/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. }
  28. // InterfaceInfo provides an interface to retrieve interface addresses bound to the endpoint.
  29. type InterfaceInfo interface {
  30. // MacAddress returns the MAC address assigned to the endpoint.
  31. MacAddress() net.HardwareAddr
  32. // Address returns the IPv4 address assigned to the endpoint.
  33. Address() *net.IPNet
  34. // AddressIPv6 returns the IPv6 address assigned to the endpoint.
  35. AddressIPv6() *net.IPNet
  36. // LinkLocalAddresses returns the list of link-local (IPv4/IPv6) addresses assigned to the endpoint.
  37. LinkLocalAddresses() []*net.IPNet
  38. }
  39. type endpointInterface struct {
  40. mac net.HardwareAddr
  41. addr *net.IPNet
  42. addrv6 *net.IPNet
  43. llAddrs []*net.IPNet
  44. srcName string
  45. dstPrefix string
  46. routes []*net.IPNet
  47. v4PoolID string
  48. v6PoolID string
  49. }
  50. func (epi *endpointInterface) MarshalJSON() ([]byte, error) {
  51. epMap := make(map[string]interface{})
  52. if epi.mac != nil {
  53. epMap["mac"] = epi.mac.String()
  54. }
  55. if epi.addr != nil {
  56. epMap["addr"] = epi.addr.String()
  57. }
  58. if epi.addrv6 != nil {
  59. epMap["addrv6"] = epi.addrv6.String()
  60. }
  61. if len(epi.llAddrs) != 0 {
  62. list := make([]string, 0, len(epi.llAddrs))
  63. for _, ll := range epi.llAddrs {
  64. list = append(list, ll.String())
  65. }
  66. epMap["llAddrs"] = list
  67. }
  68. epMap["srcName"] = epi.srcName
  69. epMap["dstPrefix"] = epi.dstPrefix
  70. var routes []string
  71. for _, route := range epi.routes {
  72. routes = append(routes, route.String())
  73. }
  74. epMap["routes"] = routes
  75. epMap["v4PoolID"] = epi.v4PoolID
  76. epMap["v6PoolID"] = epi.v6PoolID
  77. return json.Marshal(epMap)
  78. }
  79. func (epi *endpointInterface) UnmarshalJSON(b []byte) error {
  80. var (
  81. err error
  82. epMap map[string]interface{}
  83. )
  84. if err = json.Unmarshal(b, &epMap); err != nil {
  85. return err
  86. }
  87. if v, ok := epMap["mac"]; ok {
  88. if epi.mac, err = net.ParseMAC(v.(string)); err != nil {
  89. return types.InternalErrorf("failed to decode endpoint interface mac address after json unmarshal: %s", v.(string))
  90. }
  91. }
  92. if v, ok := epMap["addr"]; ok {
  93. if epi.addr, err = types.ParseCIDR(v.(string)); err != nil {
  94. return types.InternalErrorf("failed to decode endpoint interface ipv4 address after json unmarshal: %v", err)
  95. }
  96. }
  97. if v, ok := epMap["addrv6"]; ok {
  98. if epi.addrv6, err = types.ParseCIDR(v.(string)); err != nil {
  99. return types.InternalErrorf("failed to decode endpoint interface ipv6 address after json unmarshal: %v", err)
  100. }
  101. }
  102. if v, ok := epMap["llAddrs"]; ok {
  103. list := v.([]interface{})
  104. epi.llAddrs = make([]*net.IPNet, 0, len(list))
  105. for _, llS := range list {
  106. ll, err := types.ParseCIDR(llS.(string))
  107. if err != nil {
  108. return types.InternalErrorf("failed to decode endpoint interface link-local address (%v) after json unmarshal: %v", llS, err)
  109. }
  110. epi.llAddrs = append(epi.llAddrs, ll)
  111. }
  112. }
  113. epi.srcName = epMap["srcName"].(string)
  114. epi.dstPrefix = epMap["dstPrefix"].(string)
  115. rb, _ := json.Marshal(epMap["routes"])
  116. var routes []string
  117. json.Unmarshal(rb, &routes)
  118. epi.routes = make([]*net.IPNet, 0)
  119. for _, route := range routes {
  120. ip, ipr, err := net.ParseCIDR(route)
  121. if err == nil {
  122. ipr.IP = ip
  123. epi.routes = append(epi.routes, ipr)
  124. }
  125. }
  126. epi.v4PoolID = epMap["v4PoolID"].(string)
  127. epi.v6PoolID = epMap["v6PoolID"].(string)
  128. return nil
  129. }
  130. func (epi *endpointInterface) CopyTo(dstEpi *endpointInterface) error {
  131. dstEpi.mac = types.GetMacCopy(epi.mac)
  132. dstEpi.addr = types.GetIPNetCopy(epi.addr)
  133. dstEpi.addrv6 = types.GetIPNetCopy(epi.addrv6)
  134. dstEpi.srcName = epi.srcName
  135. dstEpi.dstPrefix = epi.dstPrefix
  136. dstEpi.v4PoolID = epi.v4PoolID
  137. dstEpi.v6PoolID = epi.v6PoolID
  138. if len(epi.llAddrs) != 0 {
  139. dstEpi.llAddrs = make([]*net.IPNet, 0, len(epi.llAddrs))
  140. dstEpi.llAddrs = append(dstEpi.llAddrs, epi.llAddrs...)
  141. }
  142. for _, route := range epi.routes {
  143. dstEpi.routes = append(dstEpi.routes, types.GetIPNetCopy(route))
  144. }
  145. return nil
  146. }
  147. type endpointJoinInfo struct {
  148. gw net.IP
  149. gw6 net.IP
  150. StaticRoutes []*types.StaticRoute
  151. driverTableEntries []*tableEntry
  152. disableGatewayService bool
  153. }
  154. type tableEntry struct {
  155. tableName string
  156. key string
  157. value []byte
  158. }
  159. func (ep *endpoint) Info() EndpointInfo {
  160. if ep.sandboxID != "" {
  161. return ep
  162. }
  163. n, err := ep.getNetworkFromStore()
  164. if err != nil {
  165. return nil
  166. }
  167. ep, err = n.getEndpointFromStore(ep.ID())
  168. if err != nil {
  169. return nil
  170. }
  171. sb, ok := ep.getSandbox()
  172. if !ok {
  173. // endpoint hasn't joined any sandbox.
  174. // Just return the endpoint
  175. return ep
  176. }
  177. if epi := sb.getEndpoint(ep.ID()); epi != nil {
  178. return epi
  179. }
  180. return nil
  181. }
  182. func (ep *endpoint) Iface() InterfaceInfo {
  183. ep.Lock()
  184. defer ep.Unlock()
  185. if ep.iface != nil {
  186. return ep.iface
  187. }
  188. return nil
  189. }
  190. func (ep *endpoint) Interface() driverapi.InterfaceInfo {
  191. ep.Lock()
  192. defer ep.Unlock()
  193. if ep.iface != nil {
  194. return ep.iface
  195. }
  196. return nil
  197. }
  198. func (epi *endpointInterface) SetMacAddress(mac net.HardwareAddr) error {
  199. if epi.mac != nil {
  200. return types.ForbiddenErrorf("endpoint interface MAC address present (%s). Cannot be modified with %s.", epi.mac, mac)
  201. }
  202. if mac == nil {
  203. return types.BadRequestErrorf("tried to set nil MAC address to endpoint interface")
  204. }
  205. epi.mac = types.GetMacCopy(mac)
  206. return nil
  207. }
  208. func (epi *endpointInterface) SetIPAddress(address *net.IPNet) error {
  209. if address.IP == nil {
  210. return types.BadRequestErrorf("tried to set nil IP address to endpoint interface")
  211. }
  212. if address.IP.To4() == nil {
  213. return setAddress(&epi.addrv6, address)
  214. }
  215. return setAddress(&epi.addr, address)
  216. }
  217. func setAddress(ifaceAddr **net.IPNet, address *net.IPNet) error {
  218. if *ifaceAddr != nil {
  219. return types.ForbiddenErrorf("endpoint interface IP present (%s). Cannot be modified with (%s).", *ifaceAddr, address)
  220. }
  221. *ifaceAddr = types.GetIPNetCopy(address)
  222. return nil
  223. }
  224. func (epi *endpointInterface) MacAddress() net.HardwareAddr {
  225. return types.GetMacCopy(epi.mac)
  226. }
  227. func (epi *endpointInterface) Address() *net.IPNet {
  228. return types.GetIPNetCopy(epi.addr)
  229. }
  230. func (epi *endpointInterface) AddressIPv6() *net.IPNet {
  231. return types.GetIPNetCopy(epi.addrv6)
  232. }
  233. func (epi *endpointInterface) LinkLocalAddresses() []*net.IPNet {
  234. return epi.llAddrs
  235. }
  236. func (epi *endpointInterface) SetNames(srcName string, dstPrefix string) error {
  237. epi.srcName = srcName
  238. epi.dstPrefix = dstPrefix
  239. return nil
  240. }
  241. func (ep *endpoint) InterfaceName() driverapi.InterfaceNameInfo {
  242. ep.Lock()
  243. defer ep.Unlock()
  244. if ep.iface != nil {
  245. return ep.iface
  246. }
  247. return nil
  248. }
  249. func (ep *endpoint) AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error {
  250. ep.Lock()
  251. defer ep.Unlock()
  252. r := types.StaticRoute{Destination: destination, RouteType: routeType, NextHop: nextHop}
  253. if routeType == types.NEXTHOP {
  254. // If the route specifies a next-hop, then it's loosely routed (i.e. not bound to a particular interface).
  255. ep.joinInfo.StaticRoutes = append(ep.joinInfo.StaticRoutes, &r)
  256. } else {
  257. // If the route doesn't specify a next-hop, it must be a connected route, bound to an interface.
  258. ep.iface.routes = append(ep.iface.routes, r.Destination)
  259. }
  260. return nil
  261. }
  262. func (ep *endpoint) AddTableEntry(tableName, key string, value []byte) error {
  263. ep.Lock()
  264. defer ep.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. func (ep *endpoint) Sandbox() Sandbox {
  273. cnt, ok := ep.getSandbox()
  274. if !ok {
  275. return nil
  276. }
  277. return cnt
  278. }
  279. func (ep *endpoint) StaticRoutes() []*types.StaticRoute {
  280. ep.Lock()
  281. defer ep.Unlock()
  282. if ep.joinInfo == nil {
  283. return nil
  284. }
  285. return ep.joinInfo.StaticRoutes
  286. }
  287. func (ep *endpoint) Gateway() net.IP {
  288. ep.Lock()
  289. defer ep.Unlock()
  290. if ep.joinInfo == nil {
  291. return net.IP{}
  292. }
  293. return types.GetIPCopy(ep.joinInfo.gw)
  294. }
  295. func (ep *endpoint) GatewayIPv6() net.IP {
  296. ep.Lock()
  297. defer ep.Unlock()
  298. if ep.joinInfo == nil {
  299. return net.IP{}
  300. }
  301. return types.GetIPCopy(ep.joinInfo.gw6)
  302. }
  303. func (ep *endpoint) SetGateway(gw net.IP) error {
  304. ep.Lock()
  305. defer ep.Unlock()
  306. ep.joinInfo.gw = types.GetIPCopy(gw)
  307. return nil
  308. }
  309. func (ep *endpoint) SetGatewayIPv6(gw6 net.IP) error {
  310. ep.Lock()
  311. defer ep.Unlock()
  312. ep.joinInfo.gw6 = types.GetIPCopy(gw6)
  313. return nil
  314. }
  315. func (ep *endpoint) retrieveFromStore() (*endpoint, error) {
  316. n, err := ep.getNetworkFromStore()
  317. if err != nil {
  318. return nil, fmt.Errorf("could not find network in store to get latest endpoint %s: %v", ep.Name(), err)
  319. }
  320. return n.getEndpointFromStore(ep.ID())
  321. }
  322. func (ep *endpoint) DisableGatewayService() {
  323. ep.Lock()
  324. defer ep.Unlock()
  325. ep.joinInfo.disableGatewayService = true
  326. }
  327. func (epj *endpointJoinInfo) MarshalJSON() ([]byte, error) {
  328. epMap := make(map[string]interface{})
  329. if epj.gw != nil {
  330. epMap["gw"] = epj.gw.String()
  331. }
  332. if epj.gw6 != nil {
  333. epMap["gw6"] = epj.gw6.String()
  334. }
  335. epMap["disableGatewayService"] = epj.disableGatewayService
  336. epMap["StaticRoutes"] = epj.StaticRoutes
  337. return json.Marshal(epMap)
  338. }
  339. func (epj *endpointJoinInfo) UnmarshalJSON(b []byte) error {
  340. var (
  341. err error
  342. epMap map[string]interface{}
  343. )
  344. if err = json.Unmarshal(b, &epMap); err != nil {
  345. return err
  346. }
  347. if v, ok := epMap["gw"]; ok {
  348. epj.gw6 = net.ParseIP(v.(string))
  349. }
  350. if v, ok := epMap["gw6"]; ok {
  351. epj.gw6 = net.ParseIP(v.(string))
  352. }
  353. epj.disableGatewayService = epMap["disableGatewayService"].(bool)
  354. var tStaticRoute []types.StaticRoute
  355. if v, ok := epMap["StaticRoutes"]; ok {
  356. tb, _ := json.Marshal(v)
  357. var tStaticRoute []types.StaticRoute
  358. json.Unmarshal(tb, &tStaticRoute)
  359. }
  360. var StaticRoutes []*types.StaticRoute
  361. for _, r := range tStaticRoute {
  362. StaticRoutes = append(StaticRoutes, &r)
  363. }
  364. epj.StaticRoutes = StaticRoutes
  365. return nil
  366. }
  367. func (epj *endpointJoinInfo) CopyTo(dstEpj *endpointJoinInfo) error {
  368. dstEpj.disableGatewayService = epj.disableGatewayService
  369. dstEpj.StaticRoutes = make([]*types.StaticRoute, len(epj.StaticRoutes))
  370. copy(dstEpj.StaticRoutes, epj.StaticRoutes)
  371. dstEpj.driverTableEntries = make([]*tableEntry, len(epj.driverTableEntries))
  372. copy(dstEpj.driverTableEntries, epj.driverTableEntries)
  373. dstEpj.gw = types.GetIPCopy(epj.gw)
  374. dstEpj.gw = types.GetIPCopy(epj.gw6)
  375. return nil
  376. }