3 # SPDX-License-Identifier: BSD-2-Clause
5 # Copyright (c) 2020 Alexander V. Chernikov
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 . $(atf_get_srcdir)/../common/vnet.subr
33 atf_test_case "output_tcp_setup_success" "cleanup"
34 output_tcp_setup_success_head()
37 atf_set descr 'Test valid IPv4 TCP output'
38 atf_set require.user root
41 output_tcp_setup_success_body()
54 script_name=`dirname $0`/../common/net_receiver.py
55 script_name=`realpath ${script_name}`
56 jname="v4t-output_tcp_setup_success"
60 vnet_mkjail ${jname}a ${epair}a
61 jexec ${jname}a ifconfig ${epair}a up
62 jexec ${jname}a ifconfig ${epair}a inet ${ip_src}/${plen}
64 vnet_mkjail ${jname}b ${epair}b
65 jexec ${jname}b ifconfig ${epair}b up
67 jexec ${jname}b ifconfig ${epair}b inet ${ip_dst}/${plen}
70 args="--family inet --ports ${port} --match_str ${text}"
71 echo jexec ${jname}b ${script_name} ${args}
72 jexec ${jname}b ${script_name} --test_name "test_listen_tcp" ${args} &
75 # wait for the script init
77 while [ `jexec ${jname}b sockstat -4qlp ${port} | wc -l` != "1" ]; do
79 counter=$((counter+1))
80 if [ ${counter} -ge 50 ]; then break; fi
82 if [ `jexec ${jname}b sockstat -4qlp ${port} | wc -l` != "1" ]; then
83 echo "App setup failed"
88 echo -n "${text}" | jexec ${jname}a nc -N ${ip_dst} ${port}
90 if [ $exit_code -ne 0 ]; then atf_fail "sender exit code $exit_code" ; fi
94 if [ $exit_code -ne 0 ]; then atf_fail "receiver exit code $exit_code" ; fi
97 output_tcp_setup_success_cleanup()
103 atf_test_case "output_udp_setup_success" "cleanup"
104 output_udp_setup_success_head()
107 atf_set descr 'Test valid IPv4 UDP output'
108 atf_set require.user root
111 output_udp_setup_success_body()
124 script_name=`dirname $0`/../common/net_receiver.py
125 script_name=`realpath ${script_name}`
126 jname="v4t-output_udp_setup_success"
128 epair=$(vnet_mkepair)
130 vnet_mkjail ${jname}a ${epair}a
131 jexec ${jname}a ifconfig ${epair}a up
132 jexec ${jname}a ifconfig ${epair}a inet ${ip_src}/${plen}
134 vnet_mkjail ${jname}b ${epair}b
135 jexec ${jname}b ifconfig ${epair}b up
136 jexec ${jname}b ifconfig ${epair}b inet ${ip_dst}/${plen}
139 args="--family inet --ports ${port} --match_str ${text}"
140 echo jexec ${jname}b ${script_name} ${args}
141 jexec ${jname}b ${script_name} --test_name "test_listen_udp" ${args} &
144 # wait for the script init
146 while [ `jexec ${jname}b sockstat -4qlp ${port} | wc -l` != "1" ]; do
148 counterc=$((counter+1))
149 if [ ${counter} -ge 50 ]; then break; fi
151 if [ `jexec ${jname}b sockstat -4qlp ${port} | wc -l` != "1" ]; then
152 echo "App setup failed"
157 # TODO: switch from nc to some alternative to avoid 1-second delay
158 echo -n "${text}" | jexec ${jname}a nc -uNw1 ${ip_dst} ${port}
160 if [ $exit_code -ne 0 ]; then atf_fail "sender exit code $exit_code" ; fi
164 if [ $exit_code -ne 0 ]; then atf_fail "receiver exit code $exit_code" ; fi
167 output_udp_setup_success_cleanup()
172 atf_test_case "output_raw_success" "cleanup"
173 output_raw_success_head()
176 atf_set descr 'Test valid IPv4 raw output'
177 atf_set require.user root
180 output_raw_success_body()
191 script_name=`dirname $0`/../common/net_receiver.py
192 script_name=`realpath ${script_name}`
193 jname="v4t-output_raw_success"
195 epair=$(vnet_mkepair)
197 vnet_mkjail ${jname}a ${epair}a
198 jexec ${jname}a ifconfig ${epair}a up
199 jexec ${jname}a ifconfig ${epair}a inet ${ip_src}/${plen}
201 vnet_mkjail ${jname}b ${epair}b
202 jexec ${jname}b ifconfig ${epair}b up
204 jexec ${jname}b ifconfig ${epair}b inet ${ip_dst}/${plen}
206 atf_check -o match:'1 packets transmitted, 1 packets received' jexec ${jname}a ping -nc1 ${ip_dst}
209 output_raw_success_cleanup()
214 # Multipath tests are done the following way:
216 # jailA lo < > lo jailB
218 # jailA has 2 routes towards /24 prefix on jailB loopback, via 2 epairs
219 # jailB has 1 route towards /24 prefix on jailA loopback, via epair0
221 # jailA initiates connections/sends packets towards IPs on jailB loopback.
222 # Script then compares amount of packets sent via epair0 and epair1
226 if [ "`sysctl -i -n net.route.multipath`" != 1 ]; then
227 atf_skip "This test requires ROUTE_MPATH enabled"
231 atf_test_case "output_tcp_flowid_mpath_success" "cleanup"
232 output_tcp_flowid_mpath_success_head()
235 atf_set descr 'Test valid IPv4 TCP output flowid generation'
236 atf_set require.user root
239 output_tcp_flowid_mpath_success_body()
245 net_dst="198.51.100."
251 script_name=`dirname $0`/../common/net_receiver.py
252 script_name=`realpath ${script_name}`
253 jname="v4t-output_tcp_flowid_mpath_success"
255 epair0=$(vnet_mkepair)
256 epair1=$(vnet_mkepair)
257 lo_src=$(vnet_mkloopback)
258 lo_dst=$(vnet_mkloopback)
260 vnet_mkjail ${jname}a ${epair0}a ${epair1}a ${lo_src}
261 # Setup transit IPv4 networks
262 jexec ${jname}a ifconfig ${epair0}a up
263 jexec ${jname}a ifconfig ${epair0}a inet 203.0.113.1/30
264 jexec ${jname}a ifconfig ${epair1}a up
265 jexec ${jname}a ifconfig ${epair1}a inet 203.0.113.5/30
266 jexec ${jname}a ifconfig ${lo_src} up
268 vnet_mkjail ${jname}b ${epair0}b ${epair1}b ${lo_dst}
269 jexec ${jname}b ifconfig ${epair0}b up
270 jexec ${jname}b ifconfig ${epair0}b inet 203.0.113.2/30
271 jexec ${jname}b ifconfig ${epair1}b up
272 jexec ${jname}b ifconfig ${epair1}b inet 203.0.113.6/30
273 jexec ${jname}b ifconfig ${lo_dst} up
275 # DST ips/ports to test
276 ips="4 29 48 53 55 61 71 80 84 87 90 91 119 131 137 153 154 158 162 169 169 171 176 187 197 228 233 235 236 237 245 251"
277 ports="53540 49743 43067 9131 16734 5150 14379 40292 20634 51302 3387 24387 9282 14275 42103 26881 42461 29520 45714 11096"
279 jexec ${jname}a ifconfig ${lo_src} inet ${ip_src}/32
281 jexec ${jname}b ifconfig ${lo_dst} inet ${ip_dst}/32
283 jexec ${jname}b ifconfig ${lo_dst} alias ${net_dst}${i}/32
287 # A -> towards B via epair0a
288 jexec ${jname}a route add -4 -net ${net_dst}0/${plen} 203.0.113.2
289 # A -> towards B via epair1a
290 jexec ${jname}a route add -4 -net ${net_dst}0/${plen} 203.0.113.6
292 # B towards A via epair0b
293 jexec ${jname}b route add -4 -net ${net_src}0/${plen} 203.0.113.1
295 # Base setup verification
296 atf_check -o match:'1 packets transmitted, 1 packets received' jexec ${jname}a ping -nc1 ${ip_dst}
299 num_ports=`echo ${ports} | wc -w`
300 num_ips=`echo ${ips} | wc -w`
301 count_examples=$((num_ports*num_ips))
302 listener_ports=`echo ${ports} | tr ' ' '\n' | sort -n | tr '\n' ',' | sed -e 's?,$??'`
303 args="--family inet --ports ${listener_ports} --count ${count_examples} --match_str ${text}"
304 echo jexec ${jname}b ${script_name} ${args}
305 jexec ${jname}b ${script_name} --test_name "test_listen_tcp" ${args} &
308 # wait for the app init
311 while [ ${counter} -le 50 ]; do
312 _ports=`jexec ${jname}b sockstat -4ql | awk "\\\$3 == ${cmd_pid} {print \\\$6}"|awk -F: "{print \\\$2}" | sort -n | tr '\n' ','`
313 if [ "${_ports}" = "${listener_ports}," ]; then
318 if [ ${init} -eq 0 ]; then
319 jexec ${jname}b sockstat -6ql | awk "\$3 == ${cmd_pid}"
320 echo "App setup failed"
323 echo "App setup done"
326 for _ip in ${ips}; do
327 ip="${net_dst}${_ip}"
328 for port in ${ports}; do
329 echo -n "${text}" | jexec ${jname}a nc -nN ${ip} ${port}
331 if [ $exit_code -ne 0 ]; then atf_fail "sender exit code $exit_code" ; fi
337 if [ $exit_code -ne 0 ]; then atf_fail "receiver exit code $exit_code" ; fi
339 pkt_0=`jexec ${jname}a netstat -Wf link -I ${epair0}a | head | awk '$1!~/^Name/{print$8}'`
340 pkt_1=`jexec ${jname}a netstat -Wf link -I ${epair1}a | head | awk '$1!~/^Name/{print$8}'`
341 if [ ${pkt_0} -le 10 ]; then
342 echo "Balancing failure: 1: ${pkt_0} 2: ${pkt_1}"
345 if [ ${pkt_1} -le 10 ]; then
346 echo "Balancing failure: 1: ${pkt_0} 2: ${pkt_1}"
349 echo "TCP Balancing: 1: ${pkt_0} 2: ${pkt_1}"
352 output_tcp_flowid_mpath_success_cleanup()
357 atf_test_case "output_udp_flowid_mpath_success" "cleanup"
358 output_udp_flowid_mpath_success_head()
361 atf_set descr 'Test valid IPv4 UDP output flowid generation'
362 atf_set require.user root
365 output_udp_flowid_mpath_success_body()
371 # Note this test will spawn around ~100 nc processes
374 net_dst="198.51.100."
380 script_name=`dirname $0`/../common/net_receiver.py
381 script_name=`realpath ${script_name}`
382 jname="v4t-output_tcp_flowid_mpath_success"
384 epair0=$(vnet_mkepair)
385 epair1=$(vnet_mkepair)
386 lo_src=$(vnet_mkloopback)
387 lo_dst=$(vnet_mkloopback)
389 vnet_mkjail ${jname}a ${epair0}a ${epair1}a ${lo_src}
390 # Setup transit IPv4 networks
391 jexec ${jname}a ifconfig ${epair0}a up
392 jexec ${jname}a ifconfig ${epair0}a inet 203.0.113.1/30
393 jexec ${jname}a ifconfig ${epair1}a up
394 jexec ${jname}a ifconfig ${epair1}a inet 203.0.113.5/30
395 jexec ${jname}a ifconfig ${lo_src} up
397 vnet_mkjail ${jname}b ${epair0}b ${epair1}b ${lo_dst}
398 jexec ${jname}b ifconfig ${epair0}b up
399 jexec ${jname}b ifconfig ${epair0}b inet 203.0.113.2/30
400 jexec ${jname}b ifconfig ${epair1}b up
401 jexec ${jname}b ifconfig ${epair1}b inet 203.0.113.6/30
402 jexec ${jname}b ifconfig ${lo_dst} up
404 # DST ips/ports to test
405 ips="4 29 48 53 55 61 71 80 84 87 90 91 119 131 137 153 154 158 162 169 169 171 176 187 197 228 233 235 236 237 245 251"
406 ports="53540 49743 43067 9131 16734 5150 14379 40292 20634 51302 3387 24387 9282 14275 42103 26881 42461 29520 45714 11096"
408 jexec ${jname}a ifconfig ${lo_src} inet ${ip_src}/32
410 jexec ${jname}b ifconfig ${lo_dst} inet ${ip_dst}/32
412 jexec ${jname}b ifconfig ${lo_dst} alias ${net_dst}${i}/32
416 # A -> towards B via epair0a
417 jexec ${jname}a route add -4 -net ${net_dst}0/${plen} 203.0.113.2
418 # A -> towards B via epair1a
419 jexec ${jname}a route add -4 -net ${net_dst}0/${plen} 203.0.113.6
421 # B towards A via epair0b
422 jexec ${jname}b route add -4 -net ${net_src}0/${plen} 203.0.113.1
424 # Base setup verification
425 atf_check -o match:'1 packets transmitted, 1 packets received' jexec ${jname}a ping -nc1 ${ip_dst}
428 num_ports=`echo ${ports} | wc -w`
429 num_ips=`echo ${ips} | wc -w`
430 count_examples=$((num_ports*num_ips))
431 listener_ports=`echo ${ports} | tr ' ' '\n' | sort -n | tr '\n' ',' | sed -e 's?,$??'`
432 args="--family inet --ports ${listener_ports} --count ${count_examples} --match_str ${text}"
433 echo jexec ${jname}b ${script_name} ${args}
434 jexec ${jname}b ${script_name} --test_name "test_listen_udp" ${args} &
437 # wait for the app init
440 while [ ${counter} -le 50 ]; do
441 _ports=`jexec ${jname}b sockstat -4ql | awk "\\\$3 == ${cmd_pid} {print \\\$6}"|awk -F: "{print \\\$2}" | sort -n | tr '\n' ','`
442 if [ "${_ports}" = "${listener_ports}," ]; then
447 if [ ${init} -eq 0 ]; then
448 jexec ${jname}b sockstat -4ql | awk "\$3 == ${cmd_pid}"
449 echo "App setup failed"
452 echo "App setup done"
455 for _ip in ${ips}; do
456 ip="${net_dst}${_ip}"
457 for port in ${ports}; do
458 # XXX: switch to something that allows immediate exit
459 echo -n "${text}" | jexec ${jname}a nc -nuNw1 ${ip} ${port} &
466 if [ $exit_code -ne 0 ]; then atf_fail "receiver exit code $exit_code" ; fi
468 pkt_0=`jexec ${jname}a netstat -Wf link -I ${epair0}a | head | awk '$1!~/^Name/{print$8}'`
469 pkt_1=`jexec ${jname}a netstat -Wf link -I ${epair1}a | head | awk '$1!~/^Name/{print$8}'`
470 if [ ${pkt_0} -le 10 ]; then
471 echo "Balancing failure: 1: ${pkt_0} 2: ${pkt_1}"
474 if [ ${pkt_1} -le 10 ]; then
475 echo "Balancing failure: 1: ${pkt_0} 2: ${pkt_1}"
478 echo "UDP BALANCING: 1: ${pkt_0} 2: ${pkt_1}"
481 output_udp_flowid_mpath_success_cleanup()
486 atf_test_case "output_raw_flowid_mpath_success" "cleanup"
487 output_raw_flowid_mpath_success_head()
490 atf_set descr 'Test valid IPv4 raw output flowid generation'
491 atf_set require.user root
494 output_raw_flowid_mpath_success_body()
501 net_dst="198.51.100."
507 jname="v4t-output_tcp_flowid_mpath_success"
509 epair0=$(vnet_mkepair)
510 epair1=$(vnet_mkepair)
511 lo_src=$(vnet_mkloopback)
512 lo_dst=$(vnet_mkloopback)
514 vnet_mkjail ${jname}a ${epair0}a ${epair1}a ${lo_src}
515 # Setup transit IPv4 networks
516 jexec ${jname}a ifconfig ${epair0}a up
517 jexec ${jname}a ifconfig ${epair0}a inet 203.0.113.1/30
518 jexec ${jname}a ifconfig ${epair1}a up
519 jexec ${jname}a ifconfig ${epair1}a inet 203.0.113.5/30
520 jexec ${jname}a ifconfig ${lo_src} up
522 vnet_mkjail ${jname}b ${epair0}b ${epair1}b ${lo_dst}
523 jexec ${jname}b ifconfig ${epair0}b up
524 jexec ${jname}b ifconfig ${epair0}b inet 203.0.113.2/30
525 jexec ${jname}b ifconfig ${epair1}b up
526 jexec ${jname}b ifconfig ${epair1}b inet 203.0.113.6/30
527 jexec ${jname}b ifconfig ${lo_dst} up
529 # DST ips/ports to test
530 ips="4 29 48 53 55 61 71 80 84 87 90 91 119 131 137 153 154 158 162 169 169 171 176 187 197 228 233 235 236 237 245 251"
532 jexec ${jname}a ifconfig ${lo_src} inet ${ip_src}/32
534 jexec ${jname}b ifconfig ${lo_dst} inet ${ip_dst}/32
536 jexec ${jname}b ifconfig ${lo_dst} alias ${net_dst}${i}/32
540 # A -> towards B via epair0a
541 jexec ${jname}a route add -4 -net ${net_dst}0/${plen} 203.0.113.2
542 # A -> towards B via epair1a
543 jexec ${jname}a route add -4 -net ${net_dst}0/${plen} 203.0.113.6
545 # B towards A via epair0b
546 jexec ${jname}b route add -4 -net ${net_src}0/${plen} 203.0.113.1
548 # Base setup verification
549 atf_check -o match:'1 packets transmitted, 1 packets received' jexec ${jname}a ping -nc1 ${ip_dst}
552 valid_message='1 packets transmitted, 1 packets received'
553 for _ip in ${ips}; do
554 ip="${net_dst}${_ip}"
555 atf_check -o match:"${valid_message}" jexec ${jname}a ping -nc1 ${ip}
558 pkt_0=`jexec ${jname}a netstat -Wf link -I ${epair0}a | head | awk '$1!~/^Name/{print$8}'`
559 pkt_1=`jexec ${jname}a netstat -Wf link -I ${epair1}a | head | awk '$1!~/^Name/{print$8}'`
561 jexec ${jname}a netstat -bWf link -I ${epair0}a
562 jexec ${jname}a netstat -bWf link -I ${epair1}a
563 if [ ${pkt_0} -le 10 ]; then
564 echo "Balancing failure: 1: ${pkt_0} 2: ${pkt_1}"
567 if [ ${pkt_1} -le 10 ]; then
568 echo "Balancing failure: 1: ${pkt_0} 2: ${pkt_1}"
571 echo "RAW BALANCING: 1: ${pkt_0} 2: ${pkt_1}"
574 output_raw_flowid_mpath_success_cleanup()
579 atf_init_test_cases()
581 atf_add_test_case "output_tcp_setup_success"
582 atf_add_test_case "output_udp_setup_success"
583 atf_add_test_case "output_raw_success"
584 atf_add_test_case "output_tcp_flowid_mpath_success"
585 atf_add_test_case "output_udp_flowid_mpath_success"
586 atf_add_test_case "output_raw_flowid_mpath_success"