ipvlan_network.go 7.5 KB

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