macvlan_network.go 8.0 KB

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