Skip to contentSkip to conversation

netconsole: terminate netcat background process on exit

PATCH Applied Ahmad Fatoum 2 participants 4 messages May 3 – May 7 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 netconsole: terminate netcat background process on exit

We ship no ncb utility and Debian doesn't package it, so the fallback path with netcat is what everyone is going to hit.

Drop the ncb branches and make sure to actually kill the netcat process and not only the subshell that spawned it.

---
 scripts/netconsole | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
scripts/netconsole +15 -14
index c8109bb09569..7ab81a3a9191 100755
@@ -34,26 +34,27 @@ for nc in netcat nc ; do
 	type ${nc} >/dev/null 2>&1 && break
 done
 
-trap "stty icanon echo intr ^C" 0 2 3 5 10 13 15
+cleanup() {
+	if [ -n "${listener_pid}" ] ; then
+		# The listener subshell is its own process group leader (see set -m
+		# below); kill the whole group so the netcat child dies with it.
+		kill -- -${listener_pid} 2>/dev/null
+		wait ${listener_pid} 2>/dev/null
+	fi
+	stty icanon echo intr ^C
+}
+trap cleanup EXIT INT QUIT TERM HUP
+
 echo "NOTE: the interrupt signal (normally ^C) has been remapped to ^T"
 
 stty -icanon -echo intr ^T
+set -m
 (
-if type ncb 2>/dev/null ; then
-	# see if ncb is in $PATH
-	exec ncb ${port}
-
-elif [ -x ${0%/*}/ncb ] ; then
-	# maybe it's in the same dir as the netconsole script
-	exec ${0%/*}/ncb ${port}
-
-else
-	# blah, just use regular netcat
 	while ${nc} -u -l -p ${port} < /dev/null ; do
 		:
 	done
-fi
 ) &
-pid=$!
+listener_pid=$!
+set +m
+
 ${nc} -u ${ip} ${port}
-kill ${pid} 2>/dev/null
Signature · 1 line
2.47.3
#1 lore raw

PATCH 2/3 netconsole: add support for binding to specific IP

If scripts/netconsole is already running when QEMU starts up, the host forwarding rule may break QEMU startup:

  qemu-system-aarch64: -nic user,id=net0,hostfwd=udp:127.0.0.2:6666-:6666,\
  tftp=/src/build/barebox/imx8/testfs:
    Could not set up host forwarding rule 'udp:127.0.0.2:6666-:6666'

This can be worked around by having QEMU bind to a different loopback address than what's netcat is going to use.

Add a -s option to control this.

---
 scripts/netconsole | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)
scripts/netconsole +24 -9
index 7ab81a3a9191..1945de7f9381 100755
@@ -2,9 +2,14 @@
 
 usage() {
 	(
-	echo "Usage: $0 <board IP> [board port]"
+	echo "Usage: $0 [-sh] <board IP> [board port]"
 	echo ""
 	echo "If port is not specified, '6666' will be used"
+	echo ""
+	echo "Options:"
+	echo "  -s SADDR   Bind to SADDR on host side"
+	echo "  -h         Show this help text and exit"
+
 	[ -z "$*" ] && exit 0
 	echo ""
 	echo "ERROR: $*"
@@ -13,16 +18,26 @@ usage() {
 	exit $?
 }
 
-while [ -n "$1" ] ; do
-	case $1 in
-		-h|--help) usage;;
-		--)        break;;
-		-*)        usage "Invalid option $1";;
-		*)         break;;
+if [ "$1" = "--help" ]; then
+	usage
+fi
+
+while getopts ":hs:" opt; do
+	case "$opt" in
+		h) usage ;;
+		s) saddr="$OPTARG" ;;
+		\?) usage "Invalid option -$OPTARG" ;;
+		:)  usage "Option -$OPTARG requires an argument" ;;
 	esac
-	shift
 done
 
+shift $((OPTIND - 1))
+
+# handle end-of-options marker
+if [ "$1" = "--" ]; then
+	shift
+fi
+
 ip=$1
 port=${2:-6666}
 
@@ -50,7 +65,7 @@ echo "NOTE: the interrupt signal (normally ^C) has been remapped to ^T"
 stty -icanon -echo intr ^T
 set -m
 (
-	while ${nc} -u -l -p ${port} < /dev/null ; do
+	while ${nc} ${saddr:+-s "$saddr"} -u -l -p ${port} < /dev/null ; do
 		:
 	done
 ) &
Signature · 1 line
2.47.3
#2 lore raw

PATCH 3/3 test: py: implement --port-forward option

In reply to Ahmad Fatoum

Unsolicited host-to-guest UDP requires an explicit port forward. As we already have a -nic option, the easiest way is to have a barebox-specific option that's expanded to the correct QEMU incantations, so implement it.

---
 Documentation/boards/emulated.rst | 25 +++++++++++++++++++++++++
 Documentation/user/networking.rst |  2 ++
 conftest.py                       | 13 ++++++++++++-
 3 files changed, 39 insertions(+), 1 deletion(-)
Documentation/boards/emulated.rst +25 -0
index e3f7f78ab258..0d6dd85dc759 100644
@@ -34,3 +34,28 @@ Emulated targets can be started interactively with ``pytest --interactive``::
 The test suite can be run by omitting the ``--interactive``.
 For more information, see the :ref:`labgrid` section in the
 :ref:`contributing` guide.
+
+Netconsole over QEMU user networking
+------------------------------------
+
+barebox' UDP-based :ref:`network console <network_console>` can also
+be used in combination with QEMU. With user-mode networking (SLIRP),
+guest-to-host UDP works via NAT out of the box,
+but unsolicited host-to-guest UDP requires an explicit port forward::
+
+  pytest --lg-env test/arm/multi_v8_defconfig.yaml --interactive \
+    --env nv/dev.netconsole.ip=10.0.2.2                          \
+    --env nv/dev.netconsole.port=6666                            \
+    --env init/netconsole="ifup -a1; netconsole.active=ioe"      \
+    --port-forward=6666
+
+This will point netconsole at the SLIRP gateway (``10.0.2.2`` is the host
+as seen from the guest) and bring up the interface::
+
+  netconsole: netconsole initialized with 10.0.2.2:6666
+
+The ``i`` flag in ``netconsole.active`` is required for input; without it
+only output reaches the host. On the host, you can then interact with
+the netconsole via::
+
+  scripts/netconsole -s 127.0.0.1 127.0.0.2 6666
Documentation/user/networking.rst +2 -0
index d1db2768ad20..99ab4d8a3fda 100644
@@ -173,6 +173,8 @@ variables:
   ``-o port=${global.nfs.port},mountport=${global.nfs.port}`` as argument
   to the :ref:`mount command <command_mount>`.
 
+.. _network_console:
+
 Network console
 ---------------
 
conftest.py +12 -1
index 72d2df792e6d..6f76586e010b 100644
@@ -144,6 +144,8 @@ def pytest_addoption(parser):
                      help=('Pass all remaining options to QEMU as is'))
     parser.addoption('--bootarg', action='append', dest='bootarg', default=[],
                      help=('Pass boot arguments to barebox for debugging purposes'))
+    parser.addoption('--port-forward', metavar="PORT", action='append', dest='qemu_port', default=[],
+                     help=('Forward incoming TCP or UDP connections on specified PORT'))
 
 
 @pytest.fixture(scope="session")
@@ -258,12 +260,21 @@ def strategy(request, target, pytestconfig):  # noqa: max-complexity=30
     for arg in pytestconfig.option.qemu_arg:
         strategy.append_qemu_args(arg)
 
+    qemu_nic = "user,id=net0"
+
+    for port in pytestconfig.option.qemu_port:
+        qemu_nic += f",hostfwd=udp:127.0.0.2:{port}-:{port}"
+        qemu_nic += f",hostfwd=tcp:127.0.0.2:{port}-:{port}"
+
     if "testfs" in features:
         if not any(fs and fs[0] == "testfs" for fs in pytestconfig.option.qemu_fs):
             testfs_path = os.path.join(os.environ["LG_BUILDDIR"], "testfs")
             pytestconfig.option.qemu_fs.append(["testfs", testfs_path])
             os.makedirs(testfs_path, exist_ok=True)
-            strategy.append_qemu_args("-nic", f"user,id=net0,tftp={testfs_path}")
+            qemu_nic += f",tftp={testfs_path}"
+
+    if "qemu" in features:
+        strategy.append_qemu_args("-nic", qemu_nic)
 
     for i, fs in enumerate(pytestconfig.option.qemu_fs):
         if virtio:
Signature · 1 line
2.47.3
Applied
#3 lore raw
In reply to Ahmad Fatoum
Quoted 8 lines · Ahmad Fatoum

We ship no ncb utility and Debian doesn't package it, so the fallback path with netcat is what everyone is going to hit.

Drop the ncb branches and make sure to actually kill the netcat process and not only the subshell that spawned it.

[...]

Applied, thanks!

[1/3] netconsole: terminate netcat background process on exit

      https://git.pengutronix.de/cgit/barebox/commit/?id=2a519746b769 (link may not be stable)

[2/3] netconsole: add support for binding to specific IP
https://git.pengutronix.de/cgit/barebox/commit/?id=1af03d3d5044 (link may not be stable)
[3/3] test: py: implement --port-forward option
https://git.pengutronix.de/cgit/barebox/commit/?id=26bf5faf4ed3 (link may not be stable)

Best regards,

Signature · 1 line
Sascha Hauer <[email protected]>