From 2ecb09ba7e22976d9b6c74ef87434e629ee9c8f6 Mon Sep 17 00:00:00 2001
From: Johannes Schlatow <johannes.schlatow@genode-labs.com>
Date: Wed, 3 Nov 2021 15:27:35 +0100
Subject: [PATCH] tool/run: move nic model to qemu_args file

Always instantiating a network device with id `net0`, removes the need to call
append_qemu_nic_args in run scripts unless we want to add forwarding
rules.

genodelabs/genode#4311
---
 repos/base/board/pbxa9/qemu_args     |  1 +
 repos/base/board/pc/qemu_args        |  1 +
 repos/base/board/virt_qemu/qemu_args |  2 ++
 repos/libports/run/netty.inc         | 10 ----------
 repos/libports/run/netty_lwip.inc    |  9 ---------
 tool/run/power_on/qemu               |  6 ++++++
 tool/run/qemu.inc                    | 14 --------------
 7 files changed, 10 insertions(+), 33 deletions(-)

diff --git a/repos/base/board/pbxa9/qemu_args b/repos/base/board/pbxa9/qemu_args
index cbda496704..536c7d8cd2 100644
--- a/repos/base/board/pbxa9/qemu_args
+++ b/repos/base/board/pbxa9/qemu_args
@@ -1 +1,2 @@
 -m 768 -M realview-pbx-a9
+-net nic,model=lan9118,netdev=net0 -netdev user,id=net0
diff --git a/repos/base/board/pc/qemu_args b/repos/base/board/pc/qemu_args
index 2df5a69e38..7232ed8d4b 100644
--- a/repos/base/board/pc/qemu_args
+++ b/repos/base/board/pc/qemu_args
@@ -1 +1,2 @@
 -machine q35
+-net nic,model=e1000,netdev=net0 -netdev user,id=net0
diff --git a/repos/base/board/virt_qemu/qemu_args b/repos/base/board/virt_qemu/qemu_args
index c6fb1fb6f4..3eb893e0c1 100644
--- a/repos/base/board/virt_qemu/qemu_args
+++ b/repos/base/board/virt_qemu/qemu_args
@@ -1,6 +1,8 @@
 -m 2048
 -global virtio-mmio.force-legacy=false
+-device virtio-net-device,bus=virtio-mmio-bus.0,netdev=net0
 -device virtio-mouse-device
 -device virtio-keyboard-device
+-netdev user,id=net0
 arm_v8a: -M virt,virtualization=true,gic-version=3 -cpu cortex-a53 -smp 4
 arm_v7a: -M virt,virtualization=true -cpu cortex-a15 -smp 2
diff --git a/repos/libports/run/netty.inc b/repos/libports/run/netty.inc
index 08aa3cbbb0..9a3857cf47 100644
--- a/repos/libports/run/netty.inc
+++ b/repos/libports/run/netty.inc
@@ -155,14 +155,4 @@ append config {
 append qemu_args " -nographic "
 append_qemu_nic_args
 
-# When using with a tap device manually replace append_qemu_nic_args with:
-#proc qemu_nic_model {} {
-#       if [have_spec x86]        { return e1000 }
-#       if [have_board pbxa9]     { return lan9118 }
-#       if [have_board zynq_qemu] { return cadence_gem }
-#       return nic_model_missing
-#}
-#append qemu_args " -net nic,model=[qemu_nic_model] "
-#append qemu_args " -net tap,ifname=tap0,downscript=no,script=no "
-
 # vi: set ft=tcl :
diff --git a/repos/libports/run/netty_lwip.inc b/repos/libports/run/netty_lwip.inc
index bedd22d431..9962ee073b 100644
--- a/repos/libports/run/netty_lwip.inc
+++ b/repos/libports/run/netty_lwip.inc
@@ -114,13 +114,4 @@ append config {
 append qemu_args " -nographic "
 append_qemu_nic_args
 
-#proc qemu_nic_model {} {
-#       if [have_spec x86]        { return e1000 }
-#       if [have_board pbxa9]     { return lan9118 }
-#       if [have_board zynq_qemu] { return cadence_gem }
-#       return nic_model_missing
-#}
-#append qemu_args " -net nic,model=[qemu_nic_model] "
-#append qemu_args " -net tap,ifname=tap0,downscript=no,script=no "
-
 # vi: set ft=tcl :
diff --git a/tool/run/power_on/qemu b/tool/run/power_on/qemu
index 32b052c44c..06ebc518fd 100644
--- a/tool/run/power_on/qemu
+++ b/tool/run/power_on/qemu
@@ -198,6 +198,12 @@ proc run_power_on { } {
 	if {[regexp -- {-m\s+} $board_qemu_args dummy]} {
 		regsub -all {\-m\s+\S+} $qemu_args "" qemu_args }
 
+	##
+	# let user override any netdev
+	#
+	if {[regexp -- {-netdev\s+user,id=(\w+)} $qemu_args dummy netdev]} {
+		regsub -all "\\-netdev\\s+user,id=$netdev\\S*" $board_qemu_args "" board_qemu_args }
+
 	# append custom board-specific qemu_args
 	append qemu_args " $board_qemu_args"
 
diff --git a/tool/run/qemu.inc b/tool/run/qemu.inc
index c51d172111..5e39425cc1 100644
--- a/tool/run/qemu.inc
+++ b/tool/run/qemu.inc
@@ -14,13 +14,6 @@ proc qemu_args { } {
 	return $qemu_args
 }
 
-proc qemu_nic_model {} {
-	if [have_board pbxa9]     { return lan9118 }
-	if [have_board zynq_qemu] { return cadence_gem }
-	if [have_board pc]        { return e1000 }
-	return nic_model_missing
-}
-
 proc append_qemu_nic_args { { extra_netdev_args "" } } {
 	global qemu_args
 	append qemu_args " -netdev user,id=net0"
@@ -28,13 +21,6 @@ proc append_qemu_nic_args { { extra_netdev_args "" } } {
 	if { $extra_netdev_args ne "" } {
 		append qemu_args ",$extra_netdev_args"
 	}
-
-	if {[have_board virt_qemu]} {
-		append qemu_args " -global virtio-mmio.force-legacy=false "
-		append qemu_args " -device virtio-net-device,bus=virtio-mmio-bus.0,netdev=net0 "
-	} else {
-		append qemu_args " -net nic,model=[qemu_nic_model],netdev=net0 "
-	}
 }