ipvlan_network.go 8.0 KB

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