macvlan_network.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. 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 deletes 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. if err := ns.NlHandle().LinkDel(link); err != nil {
  151. logrus.WithError(err).Warnf("Failed to delete interface (%s)'s link on endpoint (%s) delete", ep.srcName, ep.id)
  152. }
  153. }
  154. if err := d.storeDelete(ep); err != nil {
  155. logrus.Warnf("Failed to remove macvlan endpoint %s from store: %v", ep.id[0:7], err)
  156. }
  157. }
  158. // delete the *network
  159. d.deleteNetwork(nid)
  160. // delete the network record from persistent cache
  161. err := d.storeDelete(n.config)
  162. if err != nil {
  163. return fmt.Errorf("error deleting deleting id %s from datastore: %v", nid, err)
  164. }
  165. return nil
  166. }
  167. // parseNetworkOptions parses docker network options
  168. func parseNetworkOptions(id string, option options.Generic) (*configuration, error) {
  169. var (
  170. err error
  171. config = &configuration{}
  172. )
  173. // parse generic labels first
  174. if genData, ok := option[netlabel.GenericData]; ok && genData != nil {
  175. if config, err = parseNetworkGenericOptions(genData); err != nil {
  176. return nil, err
  177. }
  178. }
  179. // setting the parent to "" will trigger an isolated network dummy parent link
  180. if _, ok := option[netlabel.Internal]; ok {
  181. config.Internal = true
  182. // empty --parent= and --internal are handled the same.
  183. config.Parent = ""
  184. }
  185. return config, nil
  186. }
  187. // parseNetworkGenericOptions parses generic driver docker network options
  188. func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
  189. var (
  190. err error
  191. config *configuration
  192. )
  193. switch opt := data.(type) {
  194. case *configuration:
  195. config = opt
  196. case map[string]string:
  197. config = &configuration{}
  198. err = config.fromOptions(opt)
  199. case options.Generic:
  200. var opaqueConfig interface{}
  201. if opaqueConfig, err = options.GenerateFromModel(opt, config); err == nil {
  202. config = opaqueConfig.(*configuration)
  203. }
  204. default:
  205. err = types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
  206. }
  207. return config, err
  208. }
  209. // fromOptions binds the generic options to networkConfiguration to cache
  210. func (config *configuration) fromOptions(labels map[string]string) error {
  211. for label, value := range labels {
  212. switch label {
  213. case parentOpt:
  214. // parse driver option '-o parent'
  215. config.Parent = value
  216. case driverModeOpt:
  217. // parse driver option '-o macvlan_mode'
  218. config.MacvlanMode = value
  219. }
  220. }
  221. return nil
  222. }
  223. // processIPAM parses v4 and v6 IP information and binds it to the network configuration
  224. func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
  225. if len(ipamV4Data) > 0 {
  226. for _, ipd := range ipamV4Data {
  227. s := &ipv4Subnet{
  228. SubnetIP: ipd.Pool.String(),
  229. GwIP: ipd.Gateway.String(),
  230. }
  231. config.Ipv4Subnets = append(config.Ipv4Subnets, s)
  232. }
  233. }
  234. if len(ipamV6Data) > 0 {
  235. for _, ipd := range ipamV6Data {
  236. s := &ipv6Subnet{
  237. SubnetIP: ipd.Pool.String(),
  238. GwIP: ipd.Gateway.String(),
  239. }
  240. config.Ipv6Subnets = append(config.Ipv6Subnets, s)
  241. }
  242. }
  243. return nil
  244. }