docker_experimental_network_test.go 30 KB

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