xref: /linux/tools/testing/selftests/net/net_helper.sh (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
13bdd9fd2SLucas Karpinski#!/bin/bash
23bdd9fd2SLucas Karpinski# SPDX-License-Identifier: GPL-2.0
33bdd9fd2SLucas Karpinski#
43bdd9fd2SLucas Karpinski# Helper functions
53bdd9fd2SLucas Karpinski
63bdd9fd2SLucas Karpinskiwait_local_port_listen()
73bdd9fd2SLucas Karpinski{
83bdd9fd2SLucas Karpinski	local listener_ns="${1}"
93bdd9fd2SLucas Karpinski	local port="${2}"
103bdd9fd2SLucas Karpinski	local protocol="${3}"
11*a71d0908SPaolo Abeni	local pattern
123bdd9fd2SLucas Karpinski	local i
133bdd9fd2SLucas Karpinski
14*a71d0908SPaolo Abeni	pattern=":$(printf "%04X" "${port}") "
15*a71d0908SPaolo Abeni
16*a71d0908SPaolo Abeni	# for tcp protocol additionally check the socket state
17*a71d0908SPaolo Abeni	[ ${protocol} = "tcp" ] && pattern="${pattern}0A"
183bdd9fd2SLucas Karpinski	for i in $(seq 10); do
19*a71d0908SPaolo Abeni		if ip netns exec "${listener_ns}" awk '{print $2" "$4}' \
20*a71d0908SPaolo Abeni		   /proc/net/"${protocol}"* | grep -q "${pattern}"; then
213bdd9fd2SLucas Karpinski			break
223bdd9fd2SLucas Karpinski		fi
233bdd9fd2SLucas Karpinski		sleep 0.1
243bdd9fd2SLucas Karpinski	done
253bdd9fd2SLucas Karpinski}
26