ipvlan_network.go 7.4 KB

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