macvlan_network.go 7.0 KB

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