endpoint_info.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. 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. if ep.sandboxID != "" {
  163. return ep
  164. }
  165. n, err := ep.getNetworkFromStore()
  166. if err != nil {
  167. return nil
  168. }
  169. ep, err = n.getEndpointFromStore(ep.ID())
  170. if err != nil {
  171. return nil
  172. }
  173. sb, ok := ep.getSandbox()
  174. if !ok {
  175. // endpoint hasn't joined any sandbox.
  176. // Just return the endpoint
  177. return ep
  178. }
  179. if epi := sb.getEndpoint(ep.ID()); epi != nil {
  180. return epi
  181. }
  182. return nil
  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) SetNames(srcName string, dstPrefix string) error {
  239. epi.srcName = srcName
  240. epi.dstPrefix = dstPrefix
  241. return nil
  242. }
  243. func (ep *endpoint) InterfaceName() driverapi.InterfaceNameInfo {
  244. ep.Lock()
  245. defer ep.Unlock()
  246. if ep.iface != nil {
  247. return ep.iface
  248. }
  249. return nil
  250. }
  251. func (ep *endpoint) AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error {
  252. ep.Lock()
  253. defer ep.Unlock()
  254. r := types.StaticRoute{Destination: destination, RouteType: routeType, NextHop: nextHop}
  255. if routeType == types.NEXTHOP {
  256. // If the route specifies a next-hop, then it's loosely routed (i.e. not bound to a particular interface).
  257. ep.joinInfo.StaticRoutes = append(ep.joinInfo.StaticRoutes, &r)
  258. } else {
  259. // If the route doesn't specify a next-hop, it must be a connected route, bound to an interface.
  260. ep.iface.routes = append(ep.iface.routes, r.Destination)
  261. }
  262. return nil
  263. }
  264. func (ep *endpoint) AddTableEntry(tableName, key string, value []byte) error {
  265. ep.Lock()
  266. defer ep.Unlock()
  267. ep.joinInfo.driverTableEntries = append(ep.joinInfo.driverTableEntries, &tableEntry{
  268. tableName: tableName,
  269. key: key,
  270. value: value,
  271. })
  272. return nil
  273. }
  274. func (ep *endpoint) Sandbox() Sandbox {
  275. cnt, ok := ep.getSandbox()
  276. if !ok {
  277. return nil
  278. }
  279. return cnt
  280. }
  281. func (ep *endpoint) StaticRoutes() []*types.StaticRoute {
  282. ep.Lock()
  283. defer ep.Unlock()
  284. if ep.joinInfo == nil {
  285. return nil
  286. }
  287. return ep.joinInfo.StaticRoutes
  288. }
  289. func (ep *endpoint) Gateway() net.IP {
  290. ep.Lock()
  291. defer ep.Unlock()
  292. if ep.joinInfo == nil {
  293. return net.IP{}
  294. }
  295. return types.GetIPCopy(ep.joinInfo.gw)
  296. }
  297. func (ep *endpoint) GatewayIPv6() 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.gw6)
  304. }
  305. func (ep *endpoint) SetGateway(gw net.IP) error {
  306. ep.Lock()
  307. defer ep.Unlock()
  308. ep.joinInfo.gw = types.GetIPCopy(gw)
  309. return nil
  310. }
  311. func (ep *endpoint) SetGatewayIPv6(gw6 net.IP) error {
  312. ep.Lock()
  313. defer ep.Unlock()
  314. ep.joinInfo.gw6 = types.GetIPCopy(gw6)
  315. return nil
  316. }
  317. func (ep *endpoint) retrieveFromStore() (*endpoint, error) {
  318. n, err := ep.getNetworkFromStore()
  319. if err != nil {
  320. return nil, fmt.Errorf("could not find network in store to get latest endpoint %s: %v", ep.Name(), err)
  321. }
  322. return n.getEndpointFromStore(ep.ID())
  323. }
  324. func (ep *endpoint) DisableGatewayService() {
  325. ep.Lock()
  326. defer ep.Unlock()
  327. ep.joinInfo.disableGatewayService = true
  328. }
  329. func (epj *endpointJoinInfo) MarshalJSON() ([]byte, error) {
  330. epMap := make(map[string]interface{})
  331. if epj.gw != nil {
  332. epMap["gw"] = epj.gw.String()
  333. }
  334. if epj.gw6 != nil {
  335. epMap["gw6"] = epj.gw6.String()
  336. }
  337. epMap["disableGatewayService"] = epj.disableGatewayService
  338. epMap["StaticRoutes"] = epj.StaticRoutes
  339. return json.Marshal(epMap)
  340. }
  341. func (epj *endpointJoinInfo) UnmarshalJSON(b []byte) error {
  342. var (
  343. err error
  344. epMap map[string]interface{}
  345. )
  346. if err = json.Unmarshal(b, &epMap); err != nil {
  347. return err
  348. }
  349. if v, ok := epMap["gw"]; ok {
  350. epj.gw6 = net.ParseIP(v.(string))
  351. }
  352. if v, ok := epMap["gw6"]; ok {
  353. epj.gw6 = net.ParseIP(v.(string))
  354. }
  355. epj.disableGatewayService = epMap["disableGatewayService"].(bool)
  356. var tStaticRoute []types.StaticRoute
  357. if v, ok := epMap["StaticRoutes"]; ok {
  358. tb, _ := json.Marshal(v)
  359. var tStaticRoute []types.StaticRoute
  360. json.Unmarshal(tb, &tStaticRoute)
  361. }
  362. var StaticRoutes []*types.StaticRoute
  363. for _, r := range tStaticRoute {
  364. StaticRoutes = append(StaticRoutes, &r)
  365. }
  366. epj.StaticRoutes = StaticRoutes
  367. return nil
  368. }
  369. func (epj *endpointJoinInfo) CopyTo(dstEpj *endpointJoinInfo) error {
  370. dstEpj.disableGatewayService = epj.disableGatewayService
  371. dstEpj.StaticRoutes = make([]*types.StaticRoute, len(epj.StaticRoutes))
  372. copy(dstEpj.StaticRoutes, epj.StaticRoutes)
  373. dstEpj.driverTableEntries = make([]*tableEntry, len(epj.driverTableEntries))
  374. copy(dstEpj.driverTableEntries, epj.driverTableEntries)
  375. dstEpj.gw = types.GetIPCopy(epj.gw)
  376. dstEpj.gw = types.GetIPCopy(epj.gw6)
  377. return nil
  378. }