driver.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package remote
  2. import (
  3. "fmt"
  4. "net"
  5. log "github.com/Sirupsen/logrus"
  6. "github.com/docker/docker/pkg/plugins"
  7. "github.com/docker/libnetwork/datastore"
  8. "github.com/docker/libnetwork/driverapi"
  9. "github.com/docker/libnetwork/drivers/remote/api"
  10. "github.com/docker/libnetwork/types"
  11. )
  12. type driver struct {
  13. endpoint *plugins.Client
  14. networkType string
  15. }
  16. type maybeError interface {
  17. GetError() string
  18. }
  19. func newDriver(name string, client *plugins.Client) driverapi.Driver {
  20. return &driver{networkType: name, endpoint: client}
  21. }
  22. // Init makes sure a remote driver is registered when a network driver
  23. // plugin is activated.
  24. func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
  25. plugins.Handle(driverapi.NetworkPluginEndpointType, func(name string, client *plugins.Client) {
  26. // negotiate driver capability with client
  27. d := newDriver(name, client)
  28. c, err := d.(*driver).getCapabilities()
  29. if err != nil {
  30. log.Errorf("error getting capability for %s due to %v", name, err)
  31. return
  32. }
  33. if err = dc.RegisterDriver(name, d, *c); err != nil {
  34. log.Errorf("error registering driver for %s due to %v", name, err)
  35. }
  36. })
  37. return nil
  38. }
  39. // Get capability from client
  40. func (d *driver) getCapabilities() (*driverapi.Capability, error) {
  41. var capResp api.GetCapabilityResponse
  42. if err := d.call("GetCapabilities", nil, &capResp); err != nil {
  43. return nil, err
  44. }
  45. c := &driverapi.Capability{}
  46. switch capResp.Scope {
  47. case "global":
  48. c.DataScope = datastore.GlobalScope
  49. case "local":
  50. c.DataScope = datastore.LocalScope
  51. default:
  52. return nil, fmt.Errorf("invalid capability: expecting 'local' or 'global', got %s", capResp.Scope)
  53. }
  54. return c, nil
  55. }
  56. // Config is not implemented for remote drivers, since it is assumed
  57. // to be supplied to the remote process out-of-band (e.g., as command
  58. // line arguments).
  59. func (d *driver) Config(option map[string]interface{}) error {
  60. return &driverapi.ErrNotImplemented{}
  61. }
  62. func (d *driver) call(methodName string, arg interface{}, retVal maybeError) error {
  63. method := driverapi.NetworkPluginEndpointType + "." + methodName
  64. err := d.endpoint.Call(method, arg, retVal)
  65. if err != nil {
  66. return err
  67. }
  68. if e := retVal.GetError(); e != "" {
  69. return fmt.Errorf("remote: %s", e)
  70. }
  71. return nil
  72. }
  73. func (d *driver) CreateNetwork(id string, options map[string]interface{}) error {
  74. create := &api.CreateNetworkRequest{
  75. NetworkID: id,
  76. Options: options,
  77. }
  78. return d.call("CreateNetwork", create, &api.CreateNetworkResponse{})
  79. }
  80. func (d *driver) DeleteNetwork(nid string) error {
  81. delete := &api.DeleteNetworkRequest{NetworkID: nid}
  82. return d.call("DeleteNetwork", delete, &api.DeleteNetworkResponse{})
  83. }
  84. func (d *driver) CreateEndpoint(nid, eid string, epInfo driverapi.EndpointInfo, epOptions map[string]interface{}) error {
  85. var reqIface *api.EndpointInterface
  86. if epInfo == nil {
  87. return fmt.Errorf("must not be called with nil EndpointInfo")
  88. }
  89. iface := epInfo.Interface()
  90. if iface != nil {
  91. addr4 := iface.Address()
  92. addr6 := iface.AddressIPv6()
  93. reqIface = &api.EndpointInterface{
  94. Address: addr4.String(),
  95. AddressIPv6: addr6.String(),
  96. MacAddress: iface.MacAddress().String(),
  97. }
  98. }
  99. create := &api.CreateEndpointRequest{
  100. NetworkID: nid,
  101. EndpointID: eid,
  102. Interface: reqIface,
  103. Options: epOptions,
  104. }
  105. var res api.CreateEndpointResponse
  106. if err := d.call("CreateEndpoint", create, &res); err != nil {
  107. return err
  108. }
  109. inIface, err := parseInterface(res)
  110. if err != nil {
  111. return err
  112. }
  113. if reqIface != nil && inIface != nil {
  114. // We're not supposed to add interface if there is already
  115. // one. Attempt to roll back
  116. return errorWithRollback("driver attempted to add interface ignoring the one provided", d.DeleteEndpoint(nid, eid))
  117. }
  118. if inIface != nil {
  119. var addr4, addr6 net.IPNet
  120. if inIface.Address != nil {
  121. addr4 = *(inIface.Address)
  122. }
  123. if inIface.AddressIPv6 != nil {
  124. addr6 = *(inIface.AddressIPv6)
  125. }
  126. if err := epInfo.AddInterface(inIface.MacAddress, addr4, addr6); err != nil {
  127. return errorWithRollback(fmt.Sprintf("failed to AddInterface %v: %s", inIface, err), d.DeleteEndpoint(nid, eid))
  128. }
  129. }
  130. return nil
  131. }
  132. func errorWithRollback(msg string, err error) error {
  133. rollback := "rolled back"
  134. if err != nil {
  135. rollback = "failed to roll back: " + err.Error()
  136. }
  137. return fmt.Errorf("%s; %s", msg, rollback)
  138. }
  139. func (d *driver) DeleteEndpoint(nid, eid string) error {
  140. delete := &api.DeleteEndpointRequest{
  141. NetworkID: nid,
  142. EndpointID: eid,
  143. }
  144. return d.call("DeleteEndpoint", delete, &api.DeleteEndpointResponse{})
  145. }
  146. func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
  147. info := &api.EndpointInfoRequest{
  148. NetworkID: nid,
  149. EndpointID: eid,
  150. }
  151. var res api.EndpointInfoResponse
  152. if err := d.call("EndpointOperInfo", info, &res); err != nil {
  153. return nil, err
  154. }
  155. return res.Value, nil
  156. }
  157. // Join method is invoked when a Sandbox is attached to an endpoint.
  158. func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
  159. join := &api.JoinRequest{
  160. NetworkID: nid,
  161. EndpointID: eid,
  162. SandboxKey: sboxKey,
  163. Options: options,
  164. }
  165. var (
  166. res api.JoinResponse
  167. err error
  168. )
  169. if err = d.call("Join", join, &res); err != nil {
  170. return err
  171. }
  172. ifaceName := res.InterfaceName
  173. if jinfo.InterfaceName() != nil && ifaceName == nil {
  174. return fmt.Errorf("no interface name information received while one is expected")
  175. }
  176. if iface := jinfo.InterfaceName(); iface != nil {
  177. if err := iface.SetNames(ifaceName.SrcName, ifaceName.DstPrefix); err != nil {
  178. return errorWithRollback(fmt.Sprintf("failed to set interface name: %s", err), d.Leave(nid, eid))
  179. }
  180. }
  181. var addr net.IP
  182. if res.Gateway != "" {
  183. if addr = net.ParseIP(res.Gateway); addr == nil {
  184. return fmt.Errorf(`unable to parse Gateway "%s"`, res.Gateway)
  185. }
  186. if jinfo.SetGateway(addr) != nil {
  187. return errorWithRollback(fmt.Sprintf("failed to set gateway: %v", addr), d.Leave(nid, eid))
  188. }
  189. }
  190. if res.GatewayIPv6 != "" {
  191. if addr = net.ParseIP(res.GatewayIPv6); addr == nil {
  192. return fmt.Errorf(`unable to parse GatewayIPv6 "%s"`, res.GatewayIPv6)
  193. }
  194. if jinfo.SetGatewayIPv6(addr) != nil {
  195. return errorWithRollback(fmt.Sprintf("failed to set gateway IPv6: %v", addr), d.Leave(nid, eid))
  196. }
  197. }
  198. if len(res.StaticRoutes) > 0 {
  199. routes, err := parseStaticRoutes(res)
  200. if err != nil {
  201. return err
  202. }
  203. for _, route := range routes {
  204. if jinfo.AddStaticRoute(route.Destination, route.RouteType, route.NextHop) != nil {
  205. return errorWithRollback(fmt.Sprintf("failed to set static route: %v", route), d.Leave(nid, eid))
  206. }
  207. }
  208. }
  209. return nil
  210. }
  211. // Leave method is invoked when a Sandbox detaches from an endpoint.
  212. func (d *driver) Leave(nid, eid string) error {
  213. leave := &api.LeaveRequest{
  214. NetworkID: nid,
  215. EndpointID: eid,
  216. }
  217. return d.call("Leave", leave, &api.LeaveResponse{})
  218. }
  219. func (d *driver) Type() string {
  220. return d.networkType
  221. }
  222. // DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
  223. func (d *driver) DiscoverNew(dType driverapi.DiscoveryType, data interface{}) error {
  224. if dType != driverapi.NodeDiscovery {
  225. return fmt.Errorf("Unknown discovery type : %v", dType)
  226. }
  227. notif := &api.DiscoveryNotification{
  228. DiscoveryType: dType,
  229. DiscoveryData: data,
  230. }
  231. return d.call("DiscoverNew", notif, &api.DiscoveryResponse{})
  232. }
  233. // DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
  234. func (d *driver) DiscoverDelete(dType driverapi.DiscoveryType, data interface{}) error {
  235. if dType != driverapi.NodeDiscovery {
  236. return fmt.Errorf("Unknown discovery type : %v", dType)
  237. }
  238. notif := &api.DiscoveryNotification{
  239. DiscoveryType: dType,
  240. DiscoveryData: data,
  241. }
  242. return d.call("DiscoverDelete", notif, &api.DiscoveryResponse{})
  243. }
  244. func parseStaticRoutes(r api.JoinResponse) ([]*types.StaticRoute, error) {
  245. var routes = make([]*types.StaticRoute, len(r.StaticRoutes))
  246. for i, inRoute := range r.StaticRoutes {
  247. var err error
  248. outRoute := &types.StaticRoute{RouteType: inRoute.RouteType}
  249. if inRoute.Destination != "" {
  250. if outRoute.Destination, err = toAddr(inRoute.Destination); err != nil {
  251. return nil, err
  252. }
  253. }
  254. if inRoute.NextHop != "" {
  255. outRoute.NextHop = net.ParseIP(inRoute.NextHop)
  256. if outRoute.NextHop == nil {
  257. return nil, fmt.Errorf("failed to parse nexthop IP %s", inRoute.NextHop)
  258. }
  259. }
  260. routes[i] = outRoute
  261. }
  262. return routes, nil
  263. }
  264. // parseInterfaces validates all the parameters of an Interface and returns them.
  265. func parseInterface(r api.CreateEndpointResponse) (*api.Interface, error) {
  266. var outIf *api.Interface
  267. inIf := r.Interface
  268. if inIf != nil {
  269. var err error
  270. outIf = &api.Interface{}
  271. if inIf.Address != "" {
  272. if outIf.Address, err = toAddr(inIf.Address); err != nil {
  273. return nil, err
  274. }
  275. }
  276. if inIf.AddressIPv6 != "" {
  277. if outIf.AddressIPv6, err = toAddr(inIf.AddressIPv6); err != nil {
  278. return nil, err
  279. }
  280. }
  281. if inIf.MacAddress != "" {
  282. if outIf.MacAddress, err = net.ParseMAC(inIf.MacAddress); err != nil {
  283. return nil, err
  284. }
  285. }
  286. }
  287. return outIf, nil
  288. }
  289. func toAddr(ipAddr string) (*net.IPNet, error) {
  290. ip, ipnet, err := net.ParseCIDR(ipAddr)
  291. if err != nil {
  292. return nil, err
  293. }
  294. ipnet.IP = ip
  295. return ipnet, nil
  296. }