ipvlan_network.go 8.1 KB

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