macvlan_network.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package macvlan
  2. import (
  3. "fmt"
  4. "github.com/Sirupsen/logrus"
  5. "github.com/docker/docker/pkg/parsers/kernel"
  6. "github.com/docker/docker/pkg/stringid"
  7. "github.com/docker/libnetwork/driverapi"
  8. "github.com/docker/libnetwork/netlabel"
  9. "github.com/docker/libnetwork/options"
  10. "github.com/docker/libnetwork/types"
  11. )
  12. // CreateNetwork the network for the specified driver type
  13. func (d *driver) CreateNetwork(nid string, option map[string]interface{}, ipV4Data, ipV6Data []driverapi.IPAMData) error {
  14. kv, err := kernel.GetKernelVersion()
  15. if err != nil {
  16. return fmt.Errorf("failed to check kernel version for %s driver support: %v", macvlanType, err)
  17. }
  18. // ensure Kernel version is greater then v3.9 for macvlan support
  19. if kv.Kernel < macvlanKernelVer || (kv.Kernel == macvlanKernelVer && kv.Major < macvlanMajorVer) {
  20. return fmt.Errorf("kernel version failed to meet the minimum macvlan kernel requirement of %d.%d, found %d.%d.%d",
  21. macvlanKernelVer, macvlanMajorVer, kv.Kernel, kv.Major, kv.Minor)
  22. }
  23. // parse and validate the config and bind to networkConfiguration
  24. config, err := parseNetworkOptions(nid, option)
  25. if err != nil {
  26. return err
  27. }
  28. config.ID = nid
  29. err = config.processIPAM(nid, ipV4Data, ipV6Data)
  30. if err != nil {
  31. return err
  32. }
  33. // verify the macvlan mode from -o macvlan_mode option
  34. switch config.MacvlanMode {
  35. case "", modeBridge:
  36. // default to macvlan bridge mode if -o macvlan_mode is empty
  37. config.MacvlanMode = modeBridge
  38. case modeOpt:
  39. config.MacvlanMode = modeOpt
  40. case modePassthru:
  41. config.MacvlanMode = modePassthru
  42. case modeVepa:
  43. config.MacvlanMode = modeVepa
  44. default:
  45. return fmt.Errorf("requested macvlan mode '%s' is not valid, 'bridge' mode is the macvlan driver default", config.MacvlanMode)
  46. }
  47. // loopback is not a valid parent link
  48. if config.Parent == "lo" {
  49. return fmt.Errorf("loopback interface is not a valid %s parent link", macvlanType)
  50. }
  51. // if parent interface not specified, create a dummy type link to use named dummy+net_id
  52. if config.Parent == "" {
  53. config.Parent = getDummyName(stringid.TruncateID(config.ID))
  54. // empty parent and --internal are handled the same. Set here to update k/v
  55. config.Internal = true
  56. }
  57. err = d.createNetwork(config)
  58. if err != nil {
  59. return err
  60. }
  61. // update persistent db, rollback on fail
  62. err = d.storeUpdate(config)
  63. if err != nil {
  64. d.deleteNetwork(config.ID)
  65. logrus.Debugf("encoutered an error rolling back a network create for %s : %v", config.ID, err)
  66. return err
  67. }
  68. return nil
  69. }
  70. // createNetwork is used by new network callbacks and persistent network cache
  71. func (d *driver) createNetwork(config *configuration) error {
  72. // fail the network create if the macvlan kernel module is unavailable
  73. if err := kernelSupport(macvlanType); err != nil {
  74. return err
  75. }
  76. networkList := d.getNetworks()
  77. for _, nw := range networkList {
  78. if config.Parent == nw.config.Parent {
  79. return fmt.Errorf("network %s is already using parent interface %s",
  80. getDummyName(stringid.TruncateID(nw.config.ID)), config.Parent)
  81. }
  82. }
  83. if !parentExists(config.Parent) {
  84. // if the --internal flag is set, create a dummy link
  85. if config.Internal {
  86. err := createDummyLink(config.Parent, getDummyName(stringid.TruncateID(config.ID)))
  87. if err != nil {
  88. return err
  89. }
  90. config.CreatedSlaveLink = true
  91. // notify the user in logs they have limited comunicatins
  92. if config.Parent == getDummyName(stringid.TruncateID(config.ID)) {
  93. logrus.Debugf("Empty -o parent= and --internal flags limit communications to other containers inside of network: %s",
  94. config.Parent)
  95. }
  96. } else {
  97. // if the subinterface parent_iface.vlan_id checks do not pass, return err.
  98. // a valid example is 'eth0.10' for a parent iface 'eth0' with a vlan id '10'
  99. err := createVlanLink(config.Parent)
  100. if err != nil {
  101. return err
  102. }
  103. // if driver created the networks slave link, record it for future deletion
  104. config.CreatedSlaveLink = true
  105. }
  106. }
  107. n := &network{
  108. id: config.ID,
  109. driver: d,
  110. endpoints: endpointTable{},
  111. config: config,
  112. }
  113. // add the *network
  114. d.addNetwork(n)
  115. return nil
  116. }
  117. // DeleteNetwork the network for the specified driver type
  118. func (d *driver) DeleteNetwork(nid string) error {
  119. n := d.network(nid)
  120. if n == nil {
  121. return fmt.Errorf("network id %s not found", nid)
  122. }
  123. // if the driver created the slave interface, delete it, otherwise leave it
  124. if ok := n.config.CreatedSlaveLink; ok {
  125. // if the interface exists, only delete if it matches iface.vlan or dummy.net_id naming
  126. if ok := parentExists(n.config.Parent); ok {
  127. // only delete the link if it is named the net_id
  128. if n.config.Parent == getDummyName(stringid.TruncateID(nid)) {
  129. err := delDummyLink(n.config.Parent)
  130. if err != nil {
  131. logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v",
  132. n.config.Parent, err)
  133. }
  134. } else {
  135. // only delete the link if it matches iface.vlan naming
  136. err := delVlanLink(n.config.Parent)
  137. if err != nil {
  138. logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v",
  139. n.config.Parent, err)
  140. }
  141. }
  142. }
  143. }
  144. // delete the *network
  145. d.deleteNetwork(nid)
  146. // delete the network record from persistent cache
  147. err := d.storeDelete(n.config)
  148. if err != nil {
  149. return fmt.Errorf("error deleting deleting id %s from datastore: %v", nid, err)
  150. }
  151. return nil
  152. }
  153. // parseNetworkOptions parse docker network options
  154. func parseNetworkOptions(id string, option options.Generic) (*configuration, error) {
  155. var (
  156. err error
  157. config = &configuration{}
  158. )
  159. // parse generic labels first
  160. if genData, ok := option[netlabel.GenericData]; ok && genData != nil {
  161. if config, err = parseNetworkGenericOptions(genData); err != nil {
  162. return nil, err
  163. }
  164. }
  165. // setting the parent to "" will trigger an isolated network dummy parent link
  166. if _, ok := option[netlabel.Internal]; ok {
  167. config.Internal = true
  168. // empty --parent= and --internal are handled the same.
  169. config.Parent = ""
  170. }
  171. return config, nil
  172. }
  173. // parseNetworkGenericOptions parse generic driver docker network options
  174. func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
  175. var (
  176. err error
  177. config *configuration
  178. )
  179. switch opt := data.(type) {
  180. case *configuration:
  181. config = opt
  182. case map[string]string:
  183. config = &configuration{}
  184. err = config.fromOptions(opt)
  185. case options.Generic:
  186. var opaqueConfig interface{}
  187. if opaqueConfig, err = options.GenerateFromModel(opt, config); err == nil {
  188. config = opaqueConfig.(*configuration)
  189. }
  190. default:
  191. err = types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
  192. }
  193. return config, err
  194. }
  195. // fromOptions binds the generic options to networkConfiguration to cache
  196. func (config *configuration) fromOptions(labels map[string]string) error {
  197. for label, value := range labels {
  198. switch label {
  199. case parentOpt:
  200. // parse driver option '-o parent'
  201. config.Parent = value
  202. case driverModeOpt:
  203. // parse driver option '-o macvlan_mode'
  204. config.MacvlanMode = value
  205. }
  206. }
  207. return nil
  208. }
  209. // processIPAM parses v4 and v6 IP information and binds it to the network configuration
  210. func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
  211. if len(ipamV4Data) > 0 {
  212. for _, ipd := range ipamV4Data {
  213. s := &ipv4Subnet{
  214. SubnetIP: ipd.Pool.String(),
  215. GwIP: ipd.Gateway.String(),
  216. }
  217. config.Ipv4Subnets = append(config.Ipv4Subnets, s)
  218. }
  219. }
  220. if len(ipamV6Data) > 0 {
  221. for _, ipd := range ipamV6Data {
  222. s := &ipv6Subnet{
  223. SubnetIP: ipd.Pool.String(),
  224. GwIP: ipd.Gateway.String(),
  225. }
  226. config.Ipv6Subnets = append(config.Ipv6Subnets, s)
  227. }
  228. }
  229. return nil
  230. }