macvlan_network.go 7.5 KB

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