python, tests: switch usernet queries from HMP to QMP
Replace human-monitor-command "info usernet" calls with the structured x-query-usernet QMP command in get_usernet_hostfwd_port() and the VM test runner boot path. Since x-query-usernet returns a list of UsernetInfo entries, callers now iterate over the result and extract the port from each entry's "info" field. Also switch get_info_usernet_hostfwd_port() from split('\r\n') to splitlines(), since QMP strings use \n rather than the HMP monitor's \r\n line endings. Reviewed-by: Thomas Huth <th.huth+qemu@posteo.eu> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Signed-off-by: Marc-Andre Lureau <marcandre.lureau@redhat.com> Message-ID: <20260828-qemu-no-hmp-v5-11-9227de146347@redhat.com>
Marc-André Lureau committed
Aug 28, 2026 at 16:04 UTC
4d37c61590b280e9795b503bf5c95d9035cbc777
3 files changed
+17
-9
python/qemu/utils/__init__.py
+3
-2
@@ -46,10 +46,11 @@ def get_info_usernet_hostfwd_port(info_usernet_output: str) -> Optional[int]:
46
"""
47
Returns the port given to the hostfwd parameter via info usernet
48
49
- :param info_usernet_output: output generated by hmp command "info usernet"
49
+ :param info_usernet_output: output generated by "info usernet" or
50
+ the "info" field from x-query-usernet
51
:return: the port number allocated by the hostfwd option
52
"""
52
- for line in info_usernet_output.split('\r\n'):
53
+ for line in info_usernet_output.splitlines():
54
regex = r'TCP.HOST_FORWARD.*127\.0\.0\.1\s+(\d+)\s+10\.'
55
match = re.search(regex, line)
56
if match is not None:
tests/functional/qemu_test/utils.py
+6
-2
@@ -14,8 +14,12 @@ from qemu.utils import get_info_usernet_hostfwd_port
14
15
16
def get_usernet_hostfwd_port(vm):
17
- res = vm.cmd('human-monitor-command', command_line='info usernet')
18
- return get_info_usernet_hostfwd_port(res)
17
+ res = vm.cmd('x-query-usernet')
18
+ for entry in res:
19
+ port = get_info_usernet_hostfwd_port(entry['info'])
20
+ if port is not None:
21
+ return port
22
+ return None
23
24
def pow2ceil(x):
25
"""
tests/vm/basevm.py
+8
-5
@@ -312,12 +312,15 @@ class BaseVM(object):
312
self._guest = guest
313
# Init console so we can start consuming the chars.
314
self.console_init()
315
- usernet_info = guest.cmd("human-monitor-command",
316
- command_line="info usernet")
317
- self.ssh_port = get_info_usernet_hostfwd_port(usernet_info)
315
+ res = guest.cmd("x-query-usernet")
316
+ for entry in res:
317
+ port = get_info_usernet_hostfwd_port(entry['info'])
318
+ if port is not None:
319
+ self.ssh_port = port
320
+ break
321
if not self.ssh_port:
319
- raise Exception("Cannot find ssh port from 'info usernet':\n%s" % \
320
- usernet_info)
322
+ raise Exception("Cannot find ssh port from"
323
+ " 'x-query-usernet': %s" % res)
324
325
def console_init(self, timeout = None):
326
if timeout == None: