docker_experimental_network_test.go 30 KB

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