driver.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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{}, ipV4Data, ipV6Data []driverapi.IPAMData) error {
  74. create := &api.CreateNetworkRequest{
  75. NetworkID: id,
  76. Options: options,
  77. IPv4Data: ipV4Data,
  78. IPv6Data: ipV6Data,
  79. }
  80. return d.call("CreateNetwork", create, &api.CreateNetworkResponse{})
  81. }
  82. func (d *driver) DeleteNetwork(nid string) error {
  83. delete := &api.DeleteNetworkRequest{NetworkID: nid}
  84. return d.call("DeleteNetwork", delete, &api.DeleteNetworkResponse{})
  85. }
  86. func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, epOptions map[string]interface{}) error {
  87. if ifInfo == nil {
  88. return fmt.Errorf("must not be called with nil InterfaceInfo")
  89. }
  90. reqIface := &api.EndpointInterface{}
  91. if ifInfo.Address() != nil {
  92. reqIface.Address = ifInfo.Address().String()
  93. }
  94. if ifInfo.AddressIPv6() != nil {
  95. reqIface.AddressIPv6 = ifInfo.AddressIPv6().String()
  96. }
  97. if ifInfo.MacAddress() != nil {
  98. reqIface.MacAddress = ifInfo.MacAddress().String()
  99. }
  100. create := &api.CreateEndpointRequest{
  101. NetworkID: nid,
  102. EndpointID: eid,
  103. Interface: reqIface,
  104. Options: epOptions,
  105. }
  106. var res api.CreateEndpointResponse
  107. if err := d.call("CreateEndpoint", create, &res); err != nil {
  108. return err
  109. }
  110. inIface, err := parseInterface(res)
  111. if err != nil {
  112. return err
  113. }
  114. if inIface == nil {
  115. // Remote driver did not set any field
  116. return nil
  117. }
  118. if inIface.MacAddress != nil {
  119. if err := ifInfo.SetMacAddress(inIface.MacAddress); err != nil {
  120. return errorWithRollback(fmt.Sprintf("driver modified interface MAC address: %v", err), d.DeleteEndpoint(nid, eid))
  121. }
  122. }
  123. if inIface.Address != nil {
  124. if err := ifInfo.SetIPAddress(inIface.Address); err != nil {
  125. return errorWithRollback(fmt.Sprintf("driver modified interface address: %v", err), d.DeleteEndpoint(nid, eid))
  126. }
  127. }
  128. if inIface.AddressIPv6 != nil {
  129. if err := ifInfo.SetIPAddress(inIface.AddressIPv6); err != nil {
  130. return errorWithRollback(fmt.Sprintf("driver modified interface address: %v", err), d.DeleteEndpoint(nid, eid))
  131. }
  132. }
  133. return nil
  134. }
  135. func errorWithRollback(msg string, err error) error {
  136. rollback := "rolled back"
  137. if err != nil {
  138. rollback = "failed to roll back: " + err.Error()
  139. }
  140. return fmt.Errorf("%s; %s", msg, rollback)
  141. }
  142. func (d *driver) DeleteEndpoint(nid, eid string) error {
  143. delete := &api.DeleteEndpointRequest{
  144. NetworkID: nid,
  145. EndpointID: eid,
  146. }
  147. return d.call("DeleteEndpoint", delete, &api.DeleteEndpointResponse{})
  148. }
  149. func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
  150. info := &api.EndpointInfoRequest{
  151. NetworkID: nid,
  152. EndpointID: eid,
  153. }
  154. var res api.EndpointInfoResponse
  155. if err := d.call("EndpointOperInfo", info, &res); err != nil {
  156. return nil, err
  157. }
  158. return res.Value, nil
  159. }
  160. // Join method is invoked when a Sandbox is attached to an endpoint.
  161. func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
  162. join := &api.JoinRequest{
  163. NetworkID: nid,
  164. EndpointID: eid,
  165. SandboxKey: sboxKey,
  166. Options: options,
  167. }
  168. var (
  169. res api.JoinResponse
  170. err error
  171. )
  172. if err = d.call("Join", join, &res); err != nil {
  173. return err
  174. }
  175. ifaceName := res.InterfaceName
  176. if iface := jinfo.InterfaceName(); iface != nil && ifaceName != 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 = types.ParseCIDR(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 = types.ParseCIDR(inIf.Address); err != nil {
  273. return nil, err
  274. }
  275. }
  276. if inIf.AddressIPv6 != "" {
  277. if outIf.AddressIPv6, err = types.ParseCIDR(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. }