macvlan_network.go 7.7 KB

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