ipvlan_network.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package ipvlan
  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", ipvlanType, err)
  20. }
  21. // ensure Kernel version is >= v4.2 for ipvlan support
  22. if kv.Kernel < ipvlanKernelVer || (kv.Kernel == ipvlanKernelVer && kv.Major < ipvlanMajorVer) {
  23. return fmt.Errorf("kernel version failed to meet the minimum ipvlan kernel requirement of %d.%d, found %d.%d.%d",
  24. ipvlanKernelVer, ipvlanMajorVer, 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 ipvlan mode from -o ipvlan_mode option
  41. switch config.IpvlanMode {
  42. case "", modeL2:
  43. // default to ipvlan L2 mode if -o ipvlan_mode is empty
  44. config.IpvlanMode = modeL2
  45. case modeL3:
  46. config.IpvlanMode = modeL3
  47. default:
  48. return fmt.Errorf("requested ipvlan mode '%s' is not valid, 'l2' mode is the ipvlan driver default", config.IpvlanMode)
  49. }
  50. // loopback is not a valid parent link
  51. if config.Parent == "lo" {
  52. return fmt.Errorf("loopback interface is not a valid %s parent link", ipvlanType)
  53. }
  54. // if parent interface not specified, create a dummy type link to use named dummy+net_id
  55. if config.Parent == "" {
  56. config.Parent = getDummyName(stringid.TruncateID(config.ID))
  57. // empty parent and --internal are handled the same. Set here to update k/v
  58. config.Internal = true
  59. }
  60. foundExisting, err := d.createNetwork(config)
  61. if err != nil {
  62. return err
  63. }
  64. if foundExisting {
  65. return types.InternalMaskableErrorf("restoring existing network %s", config.ID)
  66. }
  67. // update persistent db, rollback on fail
  68. err = d.storeUpdate(config)
  69. if err != nil {
  70. d.deleteNetwork(config.ID)
  71. logrus.Debugf("encountered an error rolling back a network create for %s : %v", config.ID, err)
  72. return err
  73. }
  74. return nil
  75. }
  76. // createNetwork is used by new network callbacks and persistent network cache
  77. func (d *driver) createNetwork(config *configuration) (bool, error) {
  78. foundExisting := false
  79. networkList := d.getNetworks()
  80. for _, nw := range networkList {
  81. if config.Parent == nw.config.Parent {
  82. if config.ID != nw.config.ID {
  83. return false, fmt.Errorf("network %s is already using parent interface %s",
  84. getDummyName(stringid.TruncateID(nw.config.ID)), config.Parent)
  85. }
  86. logrus.Debugf("Create Network for the same ID %s\n", config.ID)
  87. foundExisting = true
  88. break
  89. }
  90. }
  91. if !parentExists(config.Parent) {
  92. // if the --internal flag is set, create a dummy link
  93. if config.Internal {
  94. err := createDummyLink(config.Parent, getDummyName(stringid.TruncateID(config.ID)))
  95. if err != nil {
  96. return false, err
  97. }
  98. config.CreatedSlaveLink = true
  99. // notify the user in logs they have limited communications
  100. if config.Parent == getDummyName(stringid.TruncateID(config.ID)) {
  101. logrus.Debugf("Empty -o parent= and --internal flags limit communications to other containers inside of network: %s",
  102. config.Parent)
  103. }
  104. } else {
  105. // if the subinterface parent_iface.vlan_id checks do not pass, return err.
  106. // a valid example is 'eth0.10' for a parent iface 'eth0' with a vlan id '10'
  107. err := createVlanLink(config.Parent)
  108. if err != nil {
  109. return false, err
  110. }
  111. // if driver created the networks slave link, record it for future deletion
  112. config.CreatedSlaveLink = true
  113. }
  114. }
  115. if !foundExisting {
  116. n := &network{
  117. id: config.ID,
  118. driver: d,
  119. endpoints: endpointTable{},
  120. config: config,
  121. }
  122. // add the network
  123. d.addNetwork(n)
  124. }
  125. return foundExisting, nil
  126. }
  127. // DeleteNetwork the network for the specified driver type
  128. func (d *driver) DeleteNetwork(nid string) error {
  129. defer osl.InitOSContext()()
  130. n := d.network(nid)
  131. if n == nil {
  132. return fmt.Errorf("network id %s not found", nid)
  133. }
  134. // if the driver created the slave interface, delete it, otherwise leave it
  135. if ok := n.config.CreatedSlaveLink; ok {
  136. // if the interface exists, only delete if it matches iface.vlan or dummy.net_id naming
  137. if ok := parentExists(n.config.Parent); ok {
  138. // only delete the link if it is named the net_id
  139. if n.config.Parent == getDummyName(stringid.TruncateID(nid)) {
  140. err := delDummyLink(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. } else {
  146. // only delete the link if it matches iface.vlan naming
  147. err := delVlanLink(n.config.Parent)
  148. if err != nil {
  149. logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v",
  150. n.config.Parent, err)
  151. }
  152. }
  153. }
  154. }
  155. for _, ep := range n.endpoints {
  156. if link, err := ns.NlHandle().LinkByName(ep.srcName); err == nil {
  157. if err := ns.NlHandle().LinkDel(link); err != nil {
  158. logrus.WithError(err).Warnf("Failed to delete interface (%s)'s link on endpoint (%s) delete", ep.srcName, ep.id)
  159. }
  160. }
  161. if err := d.storeDelete(ep); err != nil {
  162. logrus.Warnf("Failed to remove ipvlan endpoint %.7s from store: %v", ep.id, err)
  163. }
  164. }
  165. // delete the *network
  166. d.deleteNetwork(nid)
  167. // delete the network record from persistent cache
  168. err := d.storeDelete(n.config)
  169. if err != nil {
  170. return fmt.Errorf("error deleting deleting id %s from datastore: %v", nid, err)
  171. }
  172. return nil
  173. }
  174. // parseNetworkOptions parse docker network options
  175. func parseNetworkOptions(id string, option options.Generic) (*configuration, error) {
  176. var (
  177. err error
  178. config = &configuration{}
  179. )
  180. // parse generic labels first
  181. if genData, ok := option[netlabel.GenericData]; ok && genData != nil {
  182. if config, err = parseNetworkGenericOptions(genData); err != nil {
  183. return nil, err
  184. }
  185. }
  186. // setting the parent to "" will trigger an isolated network dummy parent link
  187. if _, ok := option[netlabel.Internal]; ok {
  188. config.Internal = true
  189. // empty --parent= and --internal are handled the same.
  190. config.Parent = ""
  191. }
  192. return config, nil
  193. }
  194. // parseNetworkGenericOptions parse generic driver docker network options
  195. func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
  196. var (
  197. err error
  198. config *configuration
  199. )
  200. switch opt := data.(type) {
  201. case *configuration:
  202. config = opt
  203. case map[string]string:
  204. config = &configuration{}
  205. err = config.fromOptions(opt)
  206. case options.Generic:
  207. var opaqueConfig interface{}
  208. if opaqueConfig, err = options.GenerateFromModel(opt, config); err == nil {
  209. config = opaqueConfig.(*configuration)
  210. }
  211. default:
  212. err = types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
  213. }
  214. return config, err
  215. }
  216. // fromOptions binds the generic options to networkConfiguration to cache
  217. func (config *configuration) fromOptions(labels map[string]string) error {
  218. for label, value := range labels {
  219. switch label {
  220. case parentOpt:
  221. // parse driver option '-o parent'
  222. config.Parent = value
  223. case driverModeOpt:
  224. // parse driver option '-o ipvlan_mode'
  225. config.IpvlanMode = value
  226. }
  227. }
  228. return nil
  229. }
  230. // processIPAM parses v4 and v6 IP information and binds it to the network configuration
  231. func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
  232. if len(ipamV4Data) > 0 {
  233. for _, ipd := range ipamV4Data {
  234. s := &ipv4Subnet{
  235. SubnetIP: ipd.Pool.String(),
  236. GwIP: ipd.Gateway.String(),
  237. }
  238. config.Ipv4Subnets = append(config.Ipv4Subnets, s)
  239. }
  240. }
  241. if len(ipamV6Data) > 0 {
  242. for _, ipd := range ipamV6Data {
  243. s := &ipv6Subnet{
  244. SubnetIP: ipd.Pool.String(),
  245. GwIP: ipd.Gateway.String(),
  246. }
  247. config.Ipv6Subnets = append(config.Ipv6Subnets, s)
  248. }
  249. }
  250. return nil
  251. }