macvlan_network.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package macvlan
  2. import (
  3. "fmt"
  4. "github.com/docker/docker/pkg/parsers/kernel"
  5. "github.com/docker/docker/pkg/stringid"
  6. "github.com/docker/libnetwork/driverapi"
  7. "github.com/docker/libnetwork/netlabel"
  8. "github.com/docker/libnetwork/ns"
  9. "github.com/docker/libnetwork/options"
  10. "github.com/docker/libnetwork/osl"
  11. "github.com/docker/libnetwork/types"
  12. "github.com/sirupsen/logrus"
  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. foundExisting, err := d.createNetwork(config)
  65. if err != nil {
  66. return err
  67. }
  68. if foundExisting {
  69. return types.InternalMaskableErrorf("restoring existing network %s", config.ID)
  70. }
  71. // update persistent db, rollback on fail
  72. err = d.storeUpdate(config)
  73. if err != nil {
  74. d.deleteNetwork(config.ID)
  75. logrus.Debugf("encountered an error rolling back a network create for %s : %v", config.ID, err)
  76. return err
  77. }
  78. return nil
  79. }
  80. // createNetwork is used by new network callbacks and persistent network cache
  81. func (d *driver) createNetwork(config *configuration) (bool, error) {
  82. foundExisting := false
  83. networkList := d.getNetworks()
  84. for _, nw := range networkList {
  85. if config.Parent == nw.config.Parent {
  86. if config.ID != nw.config.ID {
  87. return false, fmt.Errorf("network %s is already using parent interface %s",
  88. getDummyName(stringid.TruncateID(nw.config.ID)), config.Parent)
  89. }
  90. logrus.Debugf("Create Network for the same ID %s\n", config.ID)
  91. foundExisting = true
  92. break
  93. }
  94. }
  95. if !parentExists(config.Parent) {
  96. // if the --internal flag is set, create a dummy link
  97. if config.Internal {
  98. err := createDummyLink(config.Parent, getDummyName(stringid.TruncateID(config.ID)))
  99. if err != nil {
  100. return false, err
  101. }
  102. config.CreatedSlaveLink = true
  103. // notify the user in logs they have limited communications
  104. if config.Parent == getDummyName(stringid.TruncateID(config.ID)) {
  105. logrus.Debugf("Empty -o parent= and --internal flags limit communications to other containers inside of network: %s",
  106. config.Parent)
  107. }
  108. } else {
  109. // if the subinterface parent_iface.vlan_id checks do not pass, return err.
  110. // a valid example is 'eth0.10' for a parent iface 'eth0' with a vlan id '10'
  111. err := createVlanLink(config.Parent)
  112. if err != nil {
  113. return false, err
  114. }
  115. // if driver created the networks slave link, record it for future deletion
  116. config.CreatedSlaveLink = true
  117. }
  118. }
  119. if !foundExisting {
  120. n := &network{
  121. id: config.ID,
  122. driver: d,
  123. endpoints: endpointTable{},
  124. config: config,
  125. }
  126. // add the network
  127. d.addNetwork(n)
  128. }
  129. return foundExisting, nil
  130. }
  131. // DeleteNetwork deletes the network for the specified driver type
  132. func (d *driver) DeleteNetwork(nid string) error {
  133. defer osl.InitOSContext()()
  134. n := d.network(nid)
  135. if n == nil {
  136. return fmt.Errorf("network id %s not found", nid)
  137. }
  138. // if the driver created the slave interface, delete it, otherwise leave it
  139. if ok := n.config.CreatedSlaveLink; ok {
  140. // if the interface exists, only delete if it matches iface.vlan or dummy.net_id naming
  141. if ok := parentExists(n.config.Parent); ok {
  142. // only delete the link if it is named the net_id
  143. if n.config.Parent == getDummyName(stringid.TruncateID(nid)) {
  144. err := delDummyLink(n.config.Parent)
  145. if err != nil {
  146. logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v",
  147. n.config.Parent, err)
  148. }
  149. } else {
  150. // only delete the link if it matches iface.vlan naming
  151. err := delVlanLink(n.config.Parent)
  152. if err != nil {
  153. logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v",
  154. n.config.Parent, err)
  155. }
  156. }
  157. }
  158. }
  159. for _, ep := range n.endpoints {
  160. if link, err := ns.NlHandle().LinkByName(ep.srcName); err == nil {
  161. if err := ns.NlHandle().LinkDel(link); err != nil {
  162. logrus.WithError(err).Warnf("Failed to delete interface (%s)'s link on endpoint (%s) delete", ep.srcName, ep.id)
  163. }
  164. }
  165. if err := d.storeDelete(ep); err != nil {
  166. logrus.Warnf("Failed to remove macvlan endpoint %.7s from store: %v", ep.id, err)
  167. }
  168. }
  169. // delete the *network
  170. d.deleteNetwork(nid)
  171. // delete the network record from persistent cache
  172. err := d.storeDelete(n.config)
  173. if err != nil {
  174. return fmt.Errorf("error deleting deleting id %s from datastore: %v", nid, err)
  175. }
  176. return nil
  177. }
  178. // parseNetworkOptions parses docker network options
  179. func parseNetworkOptions(id string, option options.Generic) (*configuration, error) {
  180. var (
  181. err error
  182. config = &configuration{}
  183. )
  184. // parse generic labels first
  185. if genData, ok := option[netlabel.GenericData]; ok && genData != nil {
  186. if config, err = parseNetworkGenericOptions(genData); err != nil {
  187. return nil, err
  188. }
  189. }
  190. // setting the parent to "" will trigger an isolated network dummy parent link
  191. if _, ok := option[netlabel.Internal]; ok {
  192. config.Internal = true
  193. // empty --parent= and --internal are handled the same.
  194. config.Parent = ""
  195. }
  196. return config, nil
  197. }
  198. // parseNetworkGenericOptions parses generic driver docker network options
  199. func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
  200. var (
  201. err error
  202. config *configuration
  203. )
  204. switch opt := data.(type) {
  205. case *configuration:
  206. config = opt
  207. case map[string]string:
  208. config = &configuration{}
  209. err = config.fromOptions(opt)
  210. case options.Generic:
  211. var opaqueConfig interface{}
  212. if opaqueConfig, err = options.GenerateFromModel(opt, config); err == nil {
  213. config = opaqueConfig.(*configuration)
  214. }
  215. default:
  216. err = types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
  217. }
  218. return config, err
  219. }
  220. // fromOptions binds the generic options to networkConfiguration to cache
  221. func (config *configuration) fromOptions(labels map[string]string) error {
  222. for label, value := range labels {
  223. switch label {
  224. case parentOpt:
  225. // parse driver option '-o parent'
  226. config.Parent = value
  227. case driverModeOpt:
  228. // parse driver option '-o macvlan_mode'
  229. config.MacvlanMode = value
  230. }
  231. }
  232. return nil
  233. }
  234. // processIPAM parses v4 and v6 IP information and binds it to the network configuration
  235. func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
  236. if len(ipamV4Data) > 0 {
  237. for _, ipd := range ipamV4Data {
  238. s := &ipv4Subnet{
  239. SubnetIP: ipd.Pool.String(),
  240. GwIP: ipd.Gateway.String(),
  241. }
  242. config.Ipv4Subnets = append(config.Ipv4Subnets, s)
  243. }
  244. }
  245. if len(ipamV6Data) > 0 {
  246. for _, ipd := range ipamV6Data {
  247. s := &ipv6Subnet{
  248. SubnetIP: ipd.Pool.String(),
  249. GwIP: ipd.Gateway.String(),
  250. }
  251. config.Ipv6Subnets = append(config.Ipv6Subnets, s)
  252. }
  253. }
  254. return nil
  255. }