endpoint_info.go 12 KB

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