docker_experimental_network_test.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // +build !windows
  2. package main
  3. import (
  4. "strings"
  5. "time"
  6. "github.com/docker/docker/integration-cli/checker"
  7. "github.com/docker/docker/integration-cli/cli"
  8. "github.com/docker/docker/pkg/parsers/kernel"
  9. "github.com/go-check/check"
  10. "github.com/gotestyourself/gotestyourself/icmd"
  11. )
  12. // ensure Kernel version is >= v3.9 for macvlan support
  13. func macvlanKernelSupport() bool {
  14. return checkKernelMajorVersionGreaterOrEqualThen(3, 9)
  15. }
  16. // ensure Kernel version is >= v4.2 for ipvlan support
  17. func ipvlanKernelSupport() bool {
  18. return checkKernelMajorVersionGreaterOrEqualThen(4, 2)
  19. }
  20. func checkKernelMajorVersionGreaterOrEqualThen(kernelVersion int, majorVersion int) bool {
  21. kv, err := kernel.GetKernelVersion()
  22. if err != nil {
  23. return false
  24. }
  25. if kv.Kernel < kernelVersion || (kv.Kernel == kernelVersion && kv.Major < majorVersion) {
  26. return false
  27. }
  28. return true
  29. }
  30. func (s *DockerNetworkSuite) TestDockerNetworkMacvlanPersistance(c *check.C) {
  31. // verify the driver automatically provisions the 802.1q link (dm-dummy0.60)
  32. testRequires(c, DaemonIsLinux, macvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  33. // master dummy interface 'dm' abbreviation represents 'docker macvlan'
  34. master := "dm-dummy0"
  35. // simulate the master link the vlan tagged subinterface parent link will use
  36. createMasterDummy(c, master)
  37. // create a network specifying the desired sub-interface name
  38. dockerCmd(c, "network", "create", "--driver=macvlan", "-o", "parent=dm-dummy0.60", "dm-persist")
  39. assertNwIsAvailable(c, "dm-persist")
  40. // Restart docker daemon to test the config has persisted to disk
  41. s.d.Restart(c)
  42. // verify network is recreated from persistence
  43. assertNwIsAvailable(c, "dm-persist")
  44. // cleanup the master interface that also collects the slave dev
  45. deleteInterface(c, "dm-dummy0")
  46. }
  47. func (s *DockerNetworkSuite) TestDockerNetworkIpvlanPersistance(c *check.C) {
  48. // verify the driver automatically provisions the 802.1q link (di-dummy0.70)
  49. testRequires(c, DaemonIsLinux, ipvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  50. // master dummy interface 'di' notation represent 'docker ipvlan'
  51. master := "di-dummy0"
  52. // simulate the master link the vlan tagged subinterface parent link will use
  53. createMasterDummy(c, master)
  54. // create a network specifying the desired sub-interface name
  55. dockerCmd(c, "network", "create", "--driver=ipvlan", "-o", "parent=di-dummy0.70", "di-persist")
  56. assertNwIsAvailable(c, "di-persist")
  57. // Restart docker daemon to test the config has persisted to disk
  58. s.d.Restart(c)
  59. // verify network is recreated from persistence
  60. assertNwIsAvailable(c, "di-persist")
  61. // cleanup the master interface that also collects the slave dev
  62. deleteInterface(c, "di-dummy0")
  63. }
  64. func (s *DockerNetworkSuite) TestDockerNetworkMacvlanSubIntCreate(c *check.C) {
  65. // verify the driver automatically provisions the 802.1q link (dm-dummy0.50)
  66. testRequires(c, DaemonIsLinux, macvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  67. // master dummy interface 'dm' abbreviation represents 'docker macvlan'
  68. master := "dm-dummy0"
  69. // simulate the master link the vlan tagged subinterface parent link will use
  70. createMasterDummy(c, master)
  71. // create a network specifying the desired sub-interface name
  72. dockerCmd(c, "network", "create", "--driver=macvlan", "-o", "parent=dm-dummy0.50", "dm-subinterface")
  73. assertNwIsAvailable(c, "dm-subinterface")
  74. // cleanup the master interface which also collects the slave dev
  75. deleteInterface(c, "dm-dummy0")
  76. }
  77. func (s *DockerNetworkSuite) TestDockerNetworkIpvlanSubIntCreate(c *check.C) {
  78. // verify the driver automatically provisions the 802.1q link (di-dummy0.50)
  79. testRequires(c, DaemonIsLinux, ipvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  80. // master dummy interface 'dm' abbreviation represents 'docker ipvlan'
  81. master := "di-dummy0"
  82. // simulate the master link the vlan tagged subinterface parent link will use
  83. createMasterDummy(c, master)
  84. // create a network specifying the desired sub-interface name
  85. dockerCmd(c, "network", "create", "--driver=ipvlan", "-o", "parent=di-dummy0.60", "di-subinterface")
  86. assertNwIsAvailable(c, "di-subinterface")
  87. // cleanup the master interface which also collects the slave dev
  88. deleteInterface(c, "di-dummy0")
  89. }
  90. func (s *DockerNetworkSuite) TestDockerNetworkMacvlanOverlapParent(c *check.C) {
  91. // verify the same parent interface cannot be used if already in use by an existing network
  92. testRequires(c, DaemonIsLinux, macvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  93. // master dummy interface 'dm' abbreviation represents 'docker macvlan'
  94. master := "dm-dummy0"
  95. createMasterDummy(c, master)
  96. createVlanInterface(c, master, "dm-dummy0.40", "40")
  97. // create a network using an existing parent interface
  98. dockerCmd(c, "network", "create", "--driver=macvlan", "-o", "parent=dm-dummy0.40", "dm-subinterface")
  99. assertNwIsAvailable(c, "dm-subinterface")
  100. // attempt to create another network using the same parent iface that should fail
  101. out, _, err := dockerCmdWithError("network", "create", "--driver=macvlan", "-o", "parent=dm-dummy0.40", "dm-parent-net-overlap")
  102. // verify that the overlap returns an error
  103. c.Assert(err, check.NotNil, check.Commentf(out))
  104. // cleanup the master interface which also collects the slave dev
  105. deleteInterface(c, "dm-dummy0")
  106. }
  107. func (s *DockerNetworkSuite) TestDockerNetworkIpvlanOverlapParent(c *check.C) {
  108. // verify the same parent interface cannot be used if already in use by an existing network
  109. testRequires(c, DaemonIsLinux, ipvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  110. // master dummy interface 'dm' abbreviation represents 'docker ipvlan'
  111. master := "di-dummy0"
  112. createMasterDummy(c, master)
  113. createVlanInterface(c, master, "di-dummy0.30", "30")
  114. // create a network using an existing parent interface
  115. dockerCmd(c, "network", "create", "--driver=ipvlan", "-o", "parent=di-dummy0.30", "di-subinterface")
  116. assertNwIsAvailable(c, "di-subinterface")
  117. // attempt to create another network using the same parent iface that should fail
  118. out, _, err := dockerCmdWithError("network", "create", "--driver=ipvlan", "-o", "parent=di-dummy0.30", "di-parent-net-overlap")
  119. // verify that the overlap returns an error
  120. c.Assert(err, check.NotNil, check.Commentf(out))
  121. // cleanup the master interface which also collects the slave dev
  122. deleteInterface(c, "di-dummy0")
  123. }
  124. func (s *DockerNetworkSuite) TestDockerNetworkMacvlanMultiSubnet(c *check.C) {
  125. // create a dual stack multi-subnet Macvlan bridge mode network and validate connectivity between four containers, two on each subnet
  126. testRequires(c, DaemonIsLinux, IPv6, macvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  127. dockerCmd(c, "network", "create", "--driver=macvlan", "--ipv6", "--subnet=172.28.100.0/24", "--subnet=172.28.102.0/24", "--gateway=172.28.102.254",
  128. "--subnet=2001:db8:abc2::/64", "--subnet=2001:db8:abc4::/64", "--gateway=2001:db8:abc4::254", "dualstackbridge")
  129. // Ensure the network was created
  130. assertNwIsAvailable(c, "dualstackbridge")
  131. // start dual stack containers and verify the user specified --ip and --ip6 addresses on subnets 172.28.100.0/24 and 2001:db8:abc2::/64
  132. dockerCmd(c, "run", "-d", "--net=dualstackbridge", "--name=first", "--ip", "172.28.100.20", "--ip6", "2001:db8:abc2::20", "busybox", "top")
  133. dockerCmd(c, "run", "-d", "--net=dualstackbridge", "--name=second", "--ip", "172.28.100.21", "--ip6", "2001:db8:abc2::21", "busybox", "top")
  134. // Inspect and store the v4 address from specified container on the network dualstackbridge
  135. ip := inspectField(c, "first", "NetworkSettings.Networks.dualstackbridge.IPAddress")
  136. // Inspect and store the v6 address from specified container on the network dualstackbridge
  137. ip6 := inspectField(c, "first", "NetworkSettings.Networks.dualstackbridge.GlobalIPv6Address")
  138. // verify ipv4 connectivity to the explicit --ipv address second to first
  139. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", strings.TrimSpace(ip))
  140. c.Assert(err, check.IsNil)
  141. // verify ipv6 connectivity to the explicit --ipv6 address second to first
  142. c.Skip("Temporarily skipping while investigating sporadic v6 CI issues")
  143. _, _, err = dockerCmdWithError("exec", "second", "ping6", "-c", "1", strings.TrimSpace(ip6))
  144. c.Assert(err, check.IsNil)
  145. // start dual stack containers and verify the user specified --ip and --ip6 addresses on subnets 172.28.102.0/24 and 2001:db8:abc4::/64
  146. dockerCmd(c, "run", "-d", "--net=dualstackbridge", "--name=third", "--ip", "172.28.102.20", "--ip6", "2001:db8:abc4::20", "busybox", "top")
  147. dockerCmd(c, "run", "-d", "--net=dualstackbridge", "--name=fourth", "--ip", "172.28.102.21", "--ip6", "2001:db8:abc4::21", "busybox", "top")
  148. // Inspect and store the v4 address from specified container on the network dualstackbridge
  149. ip = inspectField(c, "third", "NetworkSettings.Networks.dualstackbridge.IPAddress")
  150. // Inspect and store the v6 address from specified container on the network dualstackbridge
  151. ip6 = inspectField(c, "third", "NetworkSettings.Networks.dualstackbridge.GlobalIPv6Address")
  152. // verify ipv4 connectivity to the explicit --ipv address from third to fourth
  153. _, _, err = dockerCmdWithError("exec", "fourth", "ping", "-c", "1", strings.TrimSpace(ip))
  154. c.Assert(err, check.IsNil)
  155. // verify ipv6 connectivity to the explicit --ipv6 address from third to fourth
  156. _, _, err = dockerCmdWithError("exec", "fourth", "ping6", "-c", "1", strings.TrimSpace(ip6))
  157. c.Assert(err, check.IsNil)
  158. // Inspect the v4 gateway to ensure the proper default GW was assigned
  159. ip4gw := inspectField(c, "first", "NetworkSettings.Networks.dualstackbridge.Gateway")
  160. c.Assert(strings.TrimSpace(ip4gw), check.Equals, "172.28.100.1")
  161. // Inspect the v6 gateway to ensure the proper default GW was assigned
  162. ip6gw := inspectField(c, "first", "NetworkSettings.Networks.dualstackbridge.IPv6Gateway")
  163. c.Assert(strings.TrimSpace(ip6gw), check.Equals, "2001:db8:abc2::1")
  164. // Inspect the v4 gateway to ensure the proper explicitly assigned default GW was assigned
  165. ip4gw = inspectField(c, "third", "NetworkSettings.Networks.dualstackbridge.Gateway")
  166. c.Assert(strings.TrimSpace(ip4gw), check.Equals, "172.28.102.254")
  167. // Inspect the v6 gateway to ensure the proper explicitly assigned default GW was assigned
  168. ip6gw = inspectField(c, "third", "NetworkSettings.Networks.dualstackbridge.IPv6Gateway")
  169. c.Assert(strings.TrimSpace(ip6gw), check.Equals, "2001:db8:abc4::254")
  170. }
  171. func (s *DockerNetworkSuite) TestDockerNetworkIpvlanL2MultiSubnet(c *check.C) {
  172. // create a dual stack multi-subnet Ipvlan L2 network and validate connectivity within the subnets, two on each subnet
  173. testRequires(c, DaemonIsLinux, IPv6, ipvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  174. dockerCmd(c, "network", "create", "--driver=ipvlan", "--ipv6", "--subnet=172.28.200.0/24", "--subnet=172.28.202.0/24", "--gateway=172.28.202.254",
  175. "--subnet=2001:db8:abc8::/64", "--subnet=2001:db8:abc6::/64", "--gateway=2001:db8:abc6::254", "dualstackl2")
  176. // Ensure the network was created
  177. assertNwIsAvailable(c, "dualstackl2")
  178. // start dual stack containers and verify the user specified --ip and --ip6 addresses on subnets 172.28.200.0/24 and 2001:db8:abc8::/64
  179. dockerCmd(c, "run", "-d", "--net=dualstackl2", "--name=first", "--ip", "172.28.200.20", "--ip6", "2001:db8:abc8::20", "busybox", "top")
  180. dockerCmd(c, "run", "-d", "--net=dualstackl2", "--name=second", "--ip", "172.28.200.21", "--ip6", "2001:db8:abc8::21", "busybox", "top")
  181. // Inspect and store the v4 address from specified container on the network dualstackl2
  182. ip := inspectField(c, "first", "NetworkSettings.Networks.dualstackl2.IPAddress")
  183. // Inspect and store the v6 address from specified container on the network dualstackl2
  184. ip6 := inspectField(c, "first", "NetworkSettings.Networks.dualstackl2.GlobalIPv6Address")
  185. // verify ipv4 connectivity to the explicit --ipv address second to first
  186. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", strings.TrimSpace(ip))
  187. c.Assert(err, check.IsNil)
  188. // verify ipv6 connectivity to the explicit --ipv6 address second to first
  189. _, _, err = dockerCmdWithError("exec", "second", "ping6", "-c", "1", strings.TrimSpace(ip6))
  190. c.Assert(err, check.IsNil)
  191. // start dual stack containers and verify the user specified --ip and --ip6 addresses on subnets 172.28.202.0/24 and 2001:db8:abc6::/64
  192. dockerCmd(c, "run", "-d", "--net=dualstackl2", "--name=third", "--ip", "172.28.202.20", "--ip6", "2001:db8:abc6::20", "busybox", "top")
  193. dockerCmd(c, "run", "-d", "--net=dualstackl2", "--name=fourth", "--ip", "172.28.202.21", "--ip6", "2001:db8:abc6::21", "busybox", "top")
  194. // Inspect and store the v4 address from specified container on the network dualstackl2
  195. ip = inspectField(c, "third", "NetworkSettings.Networks.dualstackl2.IPAddress")
  196. // Inspect and store the v6 address from specified container on the network dualstackl2
  197. ip6 = inspectField(c, "third", "NetworkSettings.Networks.dualstackl2.GlobalIPv6Address")
  198. // verify ipv4 connectivity to the explicit --ipv address from third to fourth
  199. _, _, err = dockerCmdWithError("exec", "fourth", "ping", "-c", "1", strings.TrimSpace(ip))
  200. c.Assert(err, check.IsNil)
  201. // verify ipv6 connectivity to the explicit --ipv6 address from third to fourth
  202. _, _, err = dockerCmdWithError("exec", "fourth", "ping6", "-c", "1", strings.TrimSpace(ip6))
  203. c.Assert(err, check.IsNil)
  204. // Inspect the v4 gateway to ensure the proper default GW was assigned
  205. ip4gw := inspectField(c, "first", "NetworkSettings.Networks.dualstackl2.Gateway")
  206. c.Assert(strings.TrimSpace(ip4gw), check.Equals, "172.28.200.1")
  207. // Inspect the v6 gateway to ensure the proper default GW was assigned
  208. ip6gw := inspectField(c, "first", "NetworkSettings.Networks.dualstackl2.IPv6Gateway")
  209. c.Assert(strings.TrimSpace(ip6gw), check.Equals, "2001:db8:abc8::1")
  210. // Inspect the v4 gateway to ensure the proper explicitly assigned default GW was assigned
  211. ip4gw = inspectField(c, "third", "NetworkSettings.Networks.dualstackl2.Gateway")
  212. c.Assert(strings.TrimSpace(ip4gw), check.Equals, "172.28.202.254")
  213. // Inspect the v6 gateway to ensure the proper explicitly assigned default GW was assigned
  214. ip6gw = inspectField(c, "third", "NetworkSettings.Networks.dualstackl2.IPv6Gateway")
  215. c.Assert(strings.TrimSpace(ip6gw), check.Equals, "2001:db8:abc6::254")
  216. }
  217. func (s *DockerNetworkSuite) TestDockerNetworkIpvlanL3MultiSubnet(c *check.C) {
  218. // create a dual stack multi-subnet Ipvlan L3 network and validate connectivity between all four containers per L3 mode
  219. testRequires(c, DaemonIsLinux, IPv6, ipvlanKernelSupport, NotUserNamespace, NotArm, IPv6, ExperimentalDaemon)
  220. dockerCmd(c, "network", "create", "--driver=ipvlan", "--ipv6", "--subnet=172.28.10.0/24", "--subnet=172.28.12.0/24", "--gateway=172.28.12.254",
  221. "--subnet=2001:db8:abc9::/64", "--subnet=2001:db8:abc7::/64", "--gateway=2001:db8:abc7::254", "-o", "ipvlan_mode=l3", "dualstackl3")
  222. // Ensure the network was created
  223. assertNwIsAvailable(c, "dualstackl3")
  224. // start dual stack containers and verify the user specified --ip and --ip6 addresses on subnets 172.28.10.0/24 and 2001:db8:abc9::/64
  225. dockerCmd(c, "run", "-d", "--net=dualstackl3", "--name=first", "--ip", "172.28.10.20", "--ip6", "2001:db8:abc9::20", "busybox", "top")
  226. dockerCmd(c, "run", "-d", "--net=dualstackl3", "--name=second", "--ip", "172.28.10.21", "--ip6", "2001:db8:abc9::21", "busybox", "top")
  227. // Inspect and store the v4 address from specified container on the network dualstackl3
  228. ip := inspectField(c, "first", "NetworkSettings.Networks.dualstackl3.IPAddress")
  229. // Inspect and store the v6 address from specified container on the network dualstackl3
  230. ip6 := inspectField(c, "first", "NetworkSettings.Networks.dualstackl3.GlobalIPv6Address")
  231. // verify ipv4 connectivity to the explicit --ipv address second to first
  232. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", strings.TrimSpace(ip))
  233. c.Assert(err, check.IsNil)
  234. // verify ipv6 connectivity to the explicit --ipv6 address second to first
  235. _, _, err = dockerCmdWithError("exec", "second", "ping6", "-c", "1", strings.TrimSpace(ip6))
  236. c.Assert(err, check.IsNil)
  237. // start dual stack containers and verify the user specified --ip and --ip6 addresses on subnets 172.28.12.0/24 and 2001:db8:abc7::/64
  238. dockerCmd(c, "run", "-d", "--net=dualstackl3", "--name=third", "--ip", "172.28.12.20", "--ip6", "2001:db8:abc7::20", "busybox", "top")
  239. dockerCmd(c, "run", "-d", "--net=dualstackl3", "--name=fourth", "--ip", "172.28.12.21", "--ip6", "2001:db8:abc7::21", "busybox", "top")
  240. // Inspect and store the v4 address from specified container on the network dualstackl3
  241. ip = inspectField(c, "third", "NetworkSettings.Networks.dualstackl3.IPAddress")
  242. // Inspect and store the v6 address from specified container on the network dualstackl3
  243. ip6 = inspectField(c, "third", "NetworkSettings.Networks.dualstackl3.GlobalIPv6Address")
  244. // verify ipv4 connectivity to the explicit --ipv address from third to fourth
  245. _, _, err = dockerCmdWithError("exec", "fourth", "ping", "-c", "1", strings.TrimSpace(ip))
  246. c.Assert(err, check.IsNil)
  247. // verify ipv6 connectivity to the explicit --ipv6 address from third to fourth
  248. _, _, err = dockerCmdWithError("exec", "fourth", "ping6", "-c", "1", strings.TrimSpace(ip6))
  249. c.Assert(err, check.IsNil)
  250. // Inspect and store the v4 address from specified container on the network dualstackl3
  251. ip = inspectField(c, "second", "NetworkSettings.Networks.dualstackl3.IPAddress")
  252. // Inspect and store the v6 address from specified container on the network dualstackl3
  253. ip6 = inspectField(c, "second", "NetworkSettings.Networks.dualstackl3.GlobalIPv6Address")
  254. // Verify connectivity across disparate subnets which is unique to L3 mode only
  255. _, _, err = dockerCmdWithError("exec", "third", "ping", "-c", "1", strings.TrimSpace(ip))
  256. c.Assert(err, check.IsNil)
  257. _, _, err = dockerCmdWithError("exec", "third", "ping6", "-c", "1", strings.TrimSpace(ip6))
  258. c.Assert(err, check.IsNil)
  259. // Inspect the v4 gateway to ensure no next hop is assigned in L3 mode
  260. ip4gw := inspectField(c, "first", "NetworkSettings.Networks.dualstackl3.Gateway")
  261. c.Assert(strings.TrimSpace(ip4gw), check.Equals, "")
  262. // Inspect the v6 gateway to ensure the explicitly specified default GW is ignored per L3 mode enabled
  263. ip6gw := inspectField(c, "third", "NetworkSettings.Networks.dualstackl3.IPv6Gateway")
  264. c.Assert(strings.TrimSpace(ip6gw), check.Equals, "")
  265. }
  266. func (s *DockerNetworkSuite) TestDockerNetworkIpvlanAddressing(c *check.C) {
  267. // Ensure the default gateways, next-hops and default dev devices are properly set
  268. testRequires(c, DaemonIsLinux, IPv6, ipvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  269. dockerCmd(c, "network", "create", "--driver=macvlan", "--ipv6", "--subnet=172.28.130.0/24",
  270. "--subnet=2001:db8:abca::/64", "--gateway=2001:db8:abca::254", "-o", "macvlan_mode=bridge", "dualstackbridge")
  271. assertNwIsAvailable(c, "dualstackbridge")
  272. dockerCmd(c, "run", "-d", "--net=dualstackbridge", "--name=first", "busybox", "top")
  273. // Validate macvlan bridge mode defaults gateway sets the default IPAM next-hop inferred from the subnet
  274. out, _, err := dockerCmdWithError("exec", "first", "ip", "route")
  275. c.Assert(err, check.IsNil)
  276. c.Assert(out, checker.Contains, "default via 172.28.130.1 dev eth0")
  277. // Validate macvlan bridge mode sets the v6 gateway to the user specified default gateway/next-hop
  278. out, _, err = dockerCmdWithError("exec", "first", "ip", "-6", "route")
  279. c.Assert(err, check.IsNil)
  280. c.Assert(out, checker.Contains, "default via 2001:db8:abca::254 dev eth0")
  281. // Verify ipvlan l2 mode sets the proper default gateway routes via netlink
  282. // for either an explicitly set route by the user or inferred via default IPAM
  283. dockerCmd(c, "network", "create", "--driver=ipvlan", "--ipv6", "--subnet=172.28.140.0/24", "--gateway=172.28.140.254",
  284. "--subnet=2001:db8:abcb::/64", "-o", "ipvlan_mode=l2", "dualstackl2")
  285. assertNwIsAvailable(c, "dualstackl2")
  286. dockerCmd(c, "run", "-d", "--net=dualstackl2", "--name=second", "busybox", "top")
  287. // Validate ipvlan l2 mode defaults gateway sets the default IPAM next-hop inferred from the subnet
  288. out, _, err = dockerCmdWithError("exec", "second", "ip", "route")
  289. c.Assert(err, check.IsNil)
  290. c.Assert(out, checker.Contains, "default via 172.28.140.254 dev eth0")
  291. // Validate ipvlan l2 mode sets the v6 gateway to the user specified default gateway/next-hop
  292. out, _, err = dockerCmdWithError("exec", "second", "ip", "-6", "route")
  293. c.Assert(err, check.IsNil)
  294. c.Assert(out, checker.Contains, "default via 2001:db8:abcb::1 dev eth0")
  295. // Validate ipvlan l3 mode sets the v4 gateway to dev eth0 and disregards any explicit or inferred next-hops
  296. dockerCmd(c, "network", "create", "--driver=ipvlan", "--ipv6", "--subnet=172.28.160.0/24", "--gateway=172.28.160.254",
  297. "--subnet=2001:db8:abcd::/64", "--gateway=2001:db8:abcd::254", "-o", "ipvlan_mode=l3", "dualstackl3")
  298. assertNwIsAvailable(c, "dualstackl3")
  299. dockerCmd(c, "run", "-d", "--net=dualstackl3", "--name=third", "busybox", "top")
  300. // Validate ipvlan l3 mode sets the v4 gateway to dev eth0 and disregards any explicit or inferred next-hops
  301. out, _, err = dockerCmdWithError("exec", "third", "ip", "route")
  302. c.Assert(err, check.IsNil)
  303. c.Assert(out, checker.Contains, "default dev eth0")
  304. // Validate ipvlan l3 mode sets the v6 gateway to dev eth0 and disregards any explicit or inferred next-hops
  305. out, _, err = dockerCmdWithError("exec", "third", "ip", "-6", "route")
  306. c.Assert(err, check.IsNil)
  307. c.Assert(out, checker.Contains, "default dev eth0")
  308. }
  309. func (s *DockerSuite) TestDockerNetworkMacVlanBridgeNilParent(c *check.C) {
  310. // macvlan bridge mode - dummy parent interface is provisioned dynamically
  311. testRequires(c, DaemonIsLinux, macvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  312. dockerCmd(c, "network", "create", "--driver=macvlan", "dm-nil-parent")
  313. assertNwIsAvailable(c, "dm-nil-parent")
  314. // start two containers on the same subnet
  315. dockerCmd(c, "run", "-d", "--net=dm-nil-parent", "--name=first", "busybox", "top")
  316. c.Assert(waitRun("first"), check.IsNil)
  317. dockerCmd(c, "run", "-d", "--net=dm-nil-parent", "--name=second", "busybox", "top")
  318. c.Assert(waitRun("second"), check.IsNil)
  319. // intra-network communications should succeed
  320. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  321. c.Assert(err, check.IsNil)
  322. }
  323. func (s *DockerSuite) TestDockerNetworkMacVlanBridgeInternalMode(c *check.C) {
  324. // macvlan bridge mode --internal containers can communicate inside the network but not externally
  325. testRequires(c, DaemonIsLinux, macvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  326. cli.DockerCmd(c, "network", "create", "--driver=macvlan", "--internal", "dm-internal")
  327. assertNwIsAvailable(c, "dm-internal")
  328. nr := getNetworkResource(c, "dm-internal")
  329. c.Assert(nr.Internal, checker.True)
  330. // start two containers on the same subnet
  331. cli.DockerCmd(c, "run", "-d", "--net=dm-internal", "--name=first", "busybox", "top")
  332. c.Assert(waitRun("first"), check.IsNil)
  333. cli.DockerCmd(c, "run", "-d", "--net=dm-internal", "--name=second", "busybox", "top")
  334. c.Assert(waitRun("second"), check.IsNil)
  335. // access outside of the network should fail
  336. result := cli.Docker(cli.Args("exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8"), cli.WithTimeout(time.Second))
  337. result.Assert(c, icmd.Expected{Timeout: true})
  338. // intra-network communications should succeed
  339. cli.DockerCmd(c, "exec", "second", "ping", "-c", "1", "first")
  340. }
  341. func (s *DockerSuite) TestDockerNetworkIpvlanL2NilParent(c *check.C) {
  342. // ipvlan l2 mode - dummy parent interface is provisioned dynamically
  343. testRequires(c, DaemonIsLinux, ipvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  344. dockerCmd(c, "network", "create", "--driver=ipvlan", "di-nil-parent")
  345. assertNwIsAvailable(c, "di-nil-parent")
  346. // start two containers on the same subnet
  347. dockerCmd(c, "run", "-d", "--net=di-nil-parent", "--name=first", "busybox", "top")
  348. c.Assert(waitRun("first"), check.IsNil)
  349. dockerCmd(c, "run", "-d", "--net=di-nil-parent", "--name=second", "busybox", "top")
  350. c.Assert(waitRun("second"), check.IsNil)
  351. // intra-network communications should succeed
  352. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  353. c.Assert(err, check.IsNil)
  354. }
  355. func (s *DockerSuite) TestDockerNetworkIpvlanL2InternalMode(c *check.C) {
  356. // ipvlan l2 mode --internal containers can communicate inside the network but not externally
  357. testRequires(c, DaemonIsLinux, ipvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  358. cli.DockerCmd(c, "network", "create", "--driver=ipvlan", "--internal", "di-internal")
  359. assertNwIsAvailable(c, "di-internal")
  360. nr := getNetworkResource(c, "di-internal")
  361. c.Assert(nr.Internal, checker.True)
  362. // start two containers on the same subnet
  363. cli.DockerCmd(c, "run", "-d", "--net=di-internal", "--name=first", "busybox", "top")
  364. c.Assert(waitRun("first"), check.IsNil)
  365. cli.DockerCmd(c, "run", "-d", "--net=di-internal", "--name=second", "busybox", "top")
  366. c.Assert(waitRun("second"), check.IsNil)
  367. // access outside of the network should fail
  368. result := cli.Docker(cli.Args("exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8"), cli.WithTimeout(time.Second))
  369. result.Assert(c, icmd.Expected{Timeout: true})
  370. // intra-network communications should succeed
  371. cli.DockerCmd(c, "exec", "second", "ping", "-c", "1", "first")
  372. }
  373. func (s *DockerSuite) TestDockerNetworkIpvlanL3NilParent(c *check.C) {
  374. // ipvlan l3 mode - dummy parent interface is provisioned dynamically
  375. testRequires(c, DaemonIsLinux, ipvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  376. dockerCmd(c, "network", "create", "--driver=ipvlan", "--subnet=172.28.230.0/24",
  377. "--subnet=172.28.220.0/24", "-o", "ipvlan_mode=l3", "di-nil-parent-l3")
  378. assertNwIsAvailable(c, "di-nil-parent-l3")
  379. // start two containers on separate subnets
  380. dockerCmd(c, "run", "-d", "--ip=172.28.220.10", "--net=di-nil-parent-l3", "--name=first", "busybox", "top")
  381. c.Assert(waitRun("first"), check.IsNil)
  382. dockerCmd(c, "run", "-d", "--ip=172.28.230.10", "--net=di-nil-parent-l3", "--name=second", "busybox", "top")
  383. c.Assert(waitRun("second"), check.IsNil)
  384. // intra-network communications should succeed
  385. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  386. c.Assert(err, check.IsNil)
  387. }
  388. func (s *DockerSuite) TestDockerNetworkIpvlanL3InternalMode(c *check.C) {
  389. // ipvlan l3 mode --internal containers can communicate inside the network but not externally
  390. testRequires(c, DaemonIsLinux, ipvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  391. cli.DockerCmd(c, "network", "create", "--driver=ipvlan", "--subnet=172.28.230.0/24",
  392. "--subnet=172.28.220.0/24", "-o", "ipvlan_mode=l3", "--internal", "di-internal-l3")
  393. assertNwIsAvailable(c, "di-internal-l3")
  394. nr := getNetworkResource(c, "di-internal-l3")
  395. c.Assert(nr.Internal, checker.True)
  396. // start two containers on separate subnets
  397. cli.DockerCmd(c, "run", "-d", "--ip=172.28.220.10", "--net=di-internal-l3", "--name=first", "busybox", "top")
  398. c.Assert(waitRun("first"), check.IsNil)
  399. cli.DockerCmd(c, "run", "-d", "--ip=172.28.230.10", "--net=di-internal-l3", "--name=second", "busybox", "top")
  400. c.Assert(waitRun("second"), check.IsNil)
  401. // access outside of the network should fail
  402. result := cli.Docker(cli.Args("exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8"), cli.WithTimeout(time.Second))
  403. result.Assert(c, icmd.Expected{Timeout: true})
  404. // intra-network communications should succeed
  405. cli.DockerCmd(c, "exec", "second", "ping", "-c", "1", "first")
  406. }
  407. func (s *DockerSuite) TestDockerNetworkMacVlanExistingParent(c *check.C) {
  408. // macvlan bridge mode - empty parent interface containers can reach each other internally but not externally
  409. testRequires(c, DaemonIsLinux, macvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  410. netName := "dm-parent-exists"
  411. createMasterDummy(c, "dm-dummy0")
  412. //out, err := createVlanInterface(c, "dm-parent", "dm-slave", "macvlan", "bridge")
  413. // create a network using an existing parent interface
  414. dockerCmd(c, "network", "create", "--driver=macvlan", "-o", "parent=dm-dummy0", netName)
  415. assertNwIsAvailable(c, netName)
  416. // delete the network while preserving the parent link
  417. dockerCmd(c, "network", "rm", netName)
  418. assertNwNotAvailable(c, netName)
  419. // verify the network delete did not delete the predefined link
  420. linkExists(c, "dm-dummy0")
  421. deleteInterface(c, "dm-dummy0")
  422. }
  423. func (s *DockerSuite) TestDockerNetworkMacVlanSubinterface(c *check.C) {
  424. // macvlan bridge mode - empty parent interface containers can reach each other internally but not externally
  425. testRequires(c, DaemonIsLinux, macvlanKernelSupport, NotUserNamespace, NotArm, ExperimentalDaemon)
  426. netName := "dm-subinterface"
  427. createMasterDummy(c, "dm-dummy0")
  428. createVlanInterface(c, "dm-dummy0", "dm-dummy0.20", "20")
  429. // create a network using an existing parent interface
  430. dockerCmd(c, "network", "create", "--driver=macvlan", "-o", "parent=dm-dummy0.20", netName)
  431. assertNwIsAvailable(c, netName)
  432. // start containers on 802.1q tagged '-o parent' sub-interface
  433. dockerCmd(c, "run", "-d", "--net=dm-subinterface", "--name=first", "busybox", "top")
  434. c.Assert(waitRun("first"), check.IsNil)
  435. dockerCmd(c, "run", "-d", "--net=dm-subinterface", "--name=second", "busybox", "top")
  436. c.Assert(waitRun("second"), check.IsNil)
  437. // verify containers can communicate
  438. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  439. c.Assert(err, check.IsNil)
  440. // remove the containers
  441. dockerCmd(c, "rm", "-f", "first")
  442. dockerCmd(c, "rm", "-f", "second")
  443. // delete the network while preserving the parent link
  444. dockerCmd(c, "network", "rm", netName)
  445. assertNwNotAvailable(c, netName)
  446. // verify the network delete did not delete the predefined sub-interface
  447. linkExists(c, "dm-dummy0.20")
  448. // delete the parent interface which also collects the slave
  449. deleteInterface(c, "dm-dummy0")
  450. }
  451. func createMasterDummy(c *check.C, master string) {
  452. // ip link add <dummy_name> type dummy
  453. icmd.RunCommand("ip", "link", "add", master, "type", "dummy").Assert(c, icmd.Success)
  454. icmd.RunCommand("ip", "link", "set", master, "up").Assert(c, icmd.Success)
  455. }
  456. func createVlanInterface(c *check.C, master, slave, id string) {
  457. // ip link add link <master> name <master>.<VID> type vlan id <VID>
  458. icmd.RunCommand("ip", "link", "add", "link", master, "name", slave, "type", "vlan", "id", id).Assert(c, icmd.Success)
  459. // ip link set <sub_interface_name> up
  460. icmd.RunCommand("ip", "link", "set", slave, "up").Assert(c, icmd.Success)
  461. }
  462. func linkExists(c *check.C, master string) {
  463. // verify the specified link exists, ip link show <link_name>
  464. icmd.RunCommand("ip", "link", "show", master).Assert(c, icmd.Success)
  465. }