macvlan_network.go 7.6 KB

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