netns_helper.sh 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #!/bin/sh
  2. #==============================================================================
  3. # Copyright (c) Linux Test Project, 2014
  4. # Copyright (c) 2015 Red Hat, Inc.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #==============================================================================
  19. . test.sh
  20. TST_CLEANUP=netns_ns_exec_cleanup
  21. # Set to 1 only for test cases using ifconfig (ioctl).
  22. USE_IFCONFIG=0
  23. ##
  24. # Variables which can be used in test cases (set by netns_setup() function):
  25. ###############################################################################
  26. # Use in test cases to execute commands inside a namespace. Set to 'ns_exec' or
  27. # 'ip netns exec' command according to NS_EXEC_PROGRAM argument specified in
  28. # netns_setup() function call.
  29. NS_EXEC=""
  30. # Set to "net" for ns_create/ns_exec as their options requires
  31. # to specify a namespace type. Empty for ip command.
  32. NS_TYPE=""
  33. # IP addresses of veth0 (IP0) and veth1 (IP1) devices (ipv4/ipv6 variant
  34. # is determined according to the IP_VERSION argument specified in netns_setup()
  35. # function call.
  36. IP0=""
  37. IP1=""
  38. NETMASK=""
  39. # 'ping' or 'ping6' according to the IP_VERSION argument specified
  40. # in netns_setup() function call.
  41. tping=""
  42. # Network namespaces handles for manipulating and executing commands inside
  43. # namespaces. For 'ns_exec' handles are PIDs of daemonized processes running
  44. # in namespaces.
  45. NS_HANDLE0=""
  46. NS_HANDLE1=""
  47. # Adds "inet6 add" to the 'ifconfig' arguments which is required for the ipv6
  48. # version. Always use with 'ifconfig', even if ipv4 version of a test case is
  49. # used, in which case IFCONF_IN6_ARG will be empty string. Usage:
  50. # ifconfig <device> $IFCONF_IN6_ARG IP/NETMASK
  51. IFCONF_IN6_ARG=""
  52. ###############################################################################
  53. tst_check_iproute()
  54. {
  55. local cur_ipver="$(ip -V)"
  56. local spe_ipver="$1"
  57. cur_ipver=${cur_ipver##*s}
  58. if [ -z $cur_ipver ] || [ -z $spe_ipver ]; then
  59. tst_brkm TBROK "don't obtain valid iproute version"
  60. fi
  61. if [ $cur_ipver -lt $spe_ipver ]; then
  62. tst_brkm TCONF \
  63. "The commands in iproute tools do not support required objects"
  64. fi
  65. }
  66. ##
  67. # Sets up global variables which can be used in test cases (documented above),
  68. # creates two network namespaces and a pair of virtual ethernet devices, each
  69. # device in one namespace. Each device is then enabled and assigned an IP
  70. # address according to the function parameters. IFCONF_IN6_ARG variable is set
  71. # only if ipv6 variant of test case is used (determined by IP_VERSION argument).
  72. #
  73. # SYNOPSIS:
  74. # netns_setup <NS_EXEC_PROGRAM> <IP_VERSION> <COMM_TYPE> <IP4_VETH0>
  75. # <IP4_VETH1> <IP6_VETH0> <IP6_VETH1>
  76. #
  77. # OPTIONS:
  78. # * NS_EXEC_PROGRAM (ns_exec|ip)
  79. # Program which will be used to enter and run other commands
  80. # inside a network namespace.
  81. # * IP_VERSION (ipv4|ipv6)
  82. # Version of IP. (ipv4|ipv6)
  83. # * COMM_TYPE (netlink|ioctl)
  84. # Communication type between kernel and user space
  85. # for enabling and assigning IP addresses to the virtual
  86. # ethernet devices. Uses 'ip' command for netlink and 'ifconfig'
  87. # for ioctl. (If set to ioctl, function also checks the existance
  88. # of the 'ifconfig' command.)
  89. # * IP4_VETH0, IP4_VETH1
  90. # IPv4 addresses for veth0 and veth1 devices.
  91. # * IP6_VETH0, IP6_VETH1
  92. # IPv6 addresses for veth0 and veth1 devices.
  93. #
  94. # On success function returns, on error tst_brkm is called and TC is terminated.
  95. netns_setup()
  96. {
  97. tst_require_root
  98. tst_require_cmds ip modprobe
  99. modprobe veth > /dev/null 2>&1
  100. case "$1" in
  101. ns_exec)
  102. setns_check
  103. if [ $? -eq 32 ]; then
  104. tst_brkm TCONF "setns not supported"
  105. fi
  106. NS_TYPE="net"
  107. netns_ns_exec_setup
  108. TST_CLEANUP=netns_ns_exec_cleanup
  109. ;;
  110. ip)
  111. netns_ip_setup
  112. TST_CLEANUP=netns_ip_cleanup
  113. ;;
  114. *)
  115. tst_brkm TBROK \
  116. "first argument must be a program used to enter a network namespace (ns_exec|ip)"
  117. ;;
  118. esac
  119. case "$3" in
  120. netlink)
  121. ;;
  122. ioctl)
  123. USE_IFCONFIG=1
  124. tst_require_cmds ifconfig
  125. ;;
  126. *)
  127. tst_brkm TBROK \
  128. "third argument must be a comm. type between kernel and user space (netlink|ioctl)"
  129. ;;
  130. esac
  131. if [ -z "$4" ]; then
  132. tst_brkm TBROK "fourth argument must be the IPv4 address for veth0"
  133. fi
  134. if [ -z "$5" ]; then
  135. tst_brkm TBROK "fifth argument must be the IPv4 address for veth1"
  136. fi
  137. if [ -z "$6" ]; then
  138. tst_brkm TBROK "sixth argument must be the IPv6 address for veth0"
  139. fi
  140. if [ -z "$7" ]; then
  141. tst_brkm TBROK "seventh argument must be the IPv6 address for veth1"
  142. fi
  143. case "$2" in
  144. ipv4)
  145. IP0=$4; IP1=$5
  146. tping="ping"; NETMASK=24
  147. ;;
  148. ipv6)
  149. IFCONF_IN6_ARG="inet6 add"
  150. IP0=$6; IP1=$7;
  151. if which ping6 >/dev/null 2>&1; then
  152. tping="ping6"
  153. else
  154. tping="ping -6"
  155. fi
  156. NETMASK=64
  157. ;;
  158. *)
  159. tst_brkm TBROK "second argument must be an ip version (ipv4|ipv6)"
  160. ;;
  161. esac
  162. netns_set_ip
  163. }
  164. ##
  165. # Sets up NS_EXEC to use 'ns_exec', creates two network namespaces and stores
  166. # their handles into NS_HANDLE0 and NS_HANDLE1 variables (in this case handles
  167. # are PIDs of daemonized processes running in these namespaces). Virtual
  168. # ethernet device is then created for each namespace.
  169. netns_ns_exec_setup()
  170. {
  171. NS_EXEC="ns_exec"
  172. NS_HANDLE0=$(ns_create $NS_TYPE)
  173. if [ $? -eq 1 ]; then
  174. tst_resm TINFO "$NS_HANDLE0"
  175. tst_brkm TBROK "unable to create a new network namespace"
  176. fi
  177. NS_HANDLE1=$(ns_create $NS_TYPE)
  178. if [ $? -eq 1 ]; then
  179. tst_resm TINFO "$NS_HANDLE1"
  180. tst_brkm TBROK "unable to create a new network namespace"
  181. fi
  182. $NS_EXEC $NS_HANDLE0 $NS_TYPE ip link add veth0 type veth peer name veth1 || \
  183. tst_brkm TBROK "unable to create veth pair devices"
  184. $NS_EXEC $NS_HANDLE0 $NS_TYPE ns_ifmove veth1 $NS_HANDLE1
  185. ret=$?
  186. if [ $ret -eq 0 ]; then
  187. return;
  188. fi
  189. if [ $ret -eq 32 ]; then
  190. tst_brkm TCONF "IFLA_NET_NS_PID not supported"
  191. fi
  192. tst_brkm TBROK "unable to add device veth1 to the separate network namespace"
  193. }
  194. ##
  195. # Sets up NS_EXEC to use 'ip netns exec', creates two network namespaces
  196. # and stores their handles into NS_HANDLE0 and NS_HANDLE1 variables. Virtual
  197. # ethernet device is then created for each namespace.
  198. netns_ip_setup()
  199. {
  200. tst_check_iproute 111010
  201. NS_EXEC="ip netns exec"
  202. NS_HANDLE0=tst_net_ns0
  203. NS_HANDLE1=tst_net_ns1
  204. ip netns del $NS_HANDLE0 2>/dev/null
  205. ip netns del $NS_HANDLE1 2>/dev/null
  206. ip netns add $NS_HANDLE0 || \
  207. tst_brkm TBROK "unable to create a new network namespace"
  208. ip netns add $NS_HANDLE1 || \
  209. tst_brkm TBROK "unable to create a new network namespace"
  210. $NS_EXEC $NS_HANDLE0 ip link add veth0 type veth peer name veth1 || \
  211. tst_brkm TBROK "unable to create veth pair devices"
  212. $NS_EXEC $NS_HANDLE0 ip link set veth1 netns $NS_HANDLE1 || \
  213. tst_brkm TBROK "unable to add device veth1 to the separate network namespace"
  214. }
  215. ##
  216. # Enables virtual ethernet devices and assigns IP addresses for both
  217. # of them (IPv4/IPv6 variant is decided by netns_setup() function).
  218. netns_set_ip()
  219. {
  220. if [ -z "$NS_EXEC" ]; then
  221. tst_brkm TBROK "netns_setup() function must be called first"
  222. fi
  223. # This applies only for ipv6 variant:
  224. # Do not accept Router Advertisements (accept_ra) and do not use
  225. # Duplicate Address Detection (accept_dad) which uses Neighbor
  226. # Discovery Protocol - the problem is that until DAD can confirm that
  227. # there is no other host with the same address, the address is
  228. # considered to be "tentative" (attempts to bind() to the address fail
  229. # with EADDRNOTAVAIL) which may cause problems for tests using ipv6.
  230. echo 0 | $NS_EXEC $NS_HANDLE0 $NS_TYPE \
  231. tee /proc/sys/net/ipv6/conf/veth0/accept_dad \
  232. /proc/sys/net/ipv6/conf/veth0/accept_ra >/dev/null
  233. echo 0 | $NS_EXEC $NS_HANDLE1 $NS_TYPE \
  234. tee /proc/sys/net/ipv6/conf/veth1/accept_dad \
  235. /proc/sys/net/ipv6/conf/veth1/accept_ra >/dev/null
  236. case $USE_IFCONFIG in
  237. 1)
  238. $NS_EXEC $NS_HANDLE0 $NS_TYPE ifconfig veth0 $IFCONF_IN6_ARG $IP0/$NETMASK ||
  239. tst_brkm TBROK "adding address to veth0 failed"
  240. $NS_EXEC $NS_HANDLE1 $NS_TYPE ifconfig veth1 $IFCONF_IN6_ARG $IP1/$NETMASK ||
  241. tst_brkm TBROK "adding address to veth1 failed"
  242. $NS_EXEC $NS_HANDLE0 $NS_TYPE ifconfig veth0 up ||
  243. tst_brkm TBROK "enabling veth0 device failed"
  244. $NS_EXEC $NS_HANDLE1 $NS_TYPE ifconfig veth1 up ||
  245. tst_brkm TBROK "enabling veth1 device failed"
  246. ;;
  247. *)
  248. $NS_EXEC $NS_HANDLE0 $NS_TYPE ip address add $IP0/$NETMASK dev veth0 ||
  249. tst_brkm TBROK "adding address to veth0 failed"
  250. $NS_EXEC $NS_HANDLE1 $NS_TYPE ip address add $IP1/$NETMASK dev veth1 ||
  251. tst_brkm TBROK "adding address to veth1 failed"
  252. $NS_EXEC $NS_HANDLE0 $NS_TYPE ip link set veth0 up ||
  253. tst_brkm TBROK "enabling veth0 device failed"
  254. $NS_EXEC $NS_HANDLE1 $NS_TYPE ip link set veth1 up ||
  255. tst_brkm TBROK "enabling veth1 device failed"
  256. ;;
  257. esac
  258. }
  259. netns_ns_exec_cleanup()
  260. {
  261. if [ -z "$NS_EXEC" ]; then
  262. return
  263. fi
  264. # removes veth0 device (which also removes the paired veth1 device)
  265. $NS_EXEC $NS_HANDLE0 $NS_TYPE ip link delete veth0
  266. kill -9 $NS_HANDLE0 2>/dev/null
  267. kill -9 $NS_HANDLE1 2>/dev/null
  268. }
  269. netns_ip_cleanup()
  270. {
  271. if [ -z "$NS_EXEC" ]; then
  272. return
  273. fi
  274. # removes veth0 device (which also removes the paired veth1 device)
  275. $NS_EXEC $NS_HANDLE0 ip link delete veth0
  276. ip netns del $NS_HANDLE0 2>/dev/null
  277. ip netns del $NS_HANDLE1 2>/dev/null
  278. }