Skip to content

Commit 990a339

Browse files
committed
Improve virtual_network provider's public function
1.Add netperf compile function in netperf_base to support some function test need to capture output 2.Improve network_base.check_throughput can accept netperf's absolute paths 3.Improve netdst obtain method in network_base 4.Improve ovs bridge's judgement method Assisted-by: Gemini AI Signed-off-by: Lei Yang <leiyang@redhat.com>
1 parent ae9ca9f commit 990a339

2 files changed

Lines changed: 119 additions & 12 deletions

File tree

provider/virtual_network/netperf_base.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import logging
2+
import re
23
import os
34

45
from virttest import data_dir, error_context, remote, utils_misc
6+
from avocado.core import exceptions
7+
from avocado.utils import process
58

69
LOG_JOB = logging.getLogger("avocado.test")
710

@@ -147,3 +150,103 @@ def netperf_record(results, filter_list, header=False, base="17", fbase="2"):
147150
record += "%s|" % format_result(results[key], base=base, fbase=fbase)
148151
record = record.rstrip("|")
149152
return record, key_list
153+
154+
155+
def compile_netperf_pkg(params, env, address):
156+
"""
157+
Compile netperf source package
158+
159+
:param params: Test parameters dictionary
160+
:param env: Test environment object
161+
:param address: localhost, vm name, or ip address
162+
:return: netserver_path, netperf_path
163+
"""
164+
netperf_link = params.get("netperf_link", "netperf-2.7.1.tar.bz2")
165+
netperf_src = os.path.join(data_dir.get_deps_dir("netperf"), netperf_link)
166+
guest_netperf_path = params.get("guest_netperf_path", "/var/tmp/")
167+
168+
if address == "localhost":
169+
session, install_path = None, params.get("server_path", "/var/tmp")
170+
elif address in params.get('vms', '').split():
171+
vm = env.get_vm(address)
172+
session, install_path = vm.wait_for_login(), guest_netperf_path
173+
target_ip, user, pwd = vm.get_address(), params.get("username"), params.get("password")
174+
elif re.match(r"^(?:\d{1,3}\.){3}\d{1,3}$", address):
175+
if params.get_boolean("remote_server", False):
176+
session = remote.remote_login(
177+
"ssh",
178+
address,
179+
"22",
180+
params.get("server_user"),
181+
params.get("server_pwd"),
182+
r'[$#%]'
183+
)
184+
else:
185+
raise exceptions.TestError(
186+
f"IP address '{address}' provided but 'remote_server' param is not set. "
187+
"Set remote_server=yes to compile on remote server, or use 'localhost'."
188+
)
189+
install_path = params.get("server_path", "/var/tmp")
190+
target_ip, user, pwd = address, params.get("server_user"), params.get("server_pwd")
191+
else:
192+
raise exceptions.TestError(f"Unsupported address for compilation: {address}")
193+
194+
if netperf_link.endswith(".tar.bz2"):
195+
pack_suffix, decomp_tool = ".tar.bz2", "tar jxf"
196+
elif netperf_link.endswith(".tar.gz"):
197+
pack_suffix, decomp_tool = ".tar.gz", "tar zxf"
198+
else:
199+
raise exceptions.TestError(f"Unsupported compression format for netperf package: {netperf_link}")
200+
201+
full_netperf_dir = os.path.join(install_path, netperf_link[:-len(pack_suffix)])
202+
nserver_path = os.path.join(full_netperf_dir, "src", "netserver")
203+
nperf_path = os.path.join(full_netperf_dir, "src", "netperf")
204+
205+
try:
206+
if session:
207+
if session.cmd_status(f"test -f {nperf_path}") == 0:
208+
LOG_JOB.info(f"Netperf already compiled on {address}, skipping.")
209+
return nserver_path, nperf_path
210+
elif os.path.exists(nperf_path):
211+
LOG_JOB.info("Netperf already compiled on localhost, skipping.")
212+
return nserver_path, nperf_path
213+
214+
if session:
215+
arch = session.cmd_output("arch", timeout=10).strip()
216+
decomp_cmd = f"cd {install_path} && {decomp_tool} {netperf_link}"
217+
else:
218+
arch = process.run("arch").stdout_text.strip()
219+
decomp_cmd = f"{decomp_tool} {netperf_src} -C {install_path}"
220+
221+
build_type_map = {
222+
"aarch64": "aarch64-unknown-linux-gnu",
223+
"x86_64": "x86_64-unknown-linux-gnu",
224+
}
225+
build_type = build_type_map.get(arch, arch)
226+
compile_cmd = (f"cd {full_netperf_dir} && ./autogen.sh && "
227+
f"CFLAGS='-Wno-implicit-function-declaration' ./configure "
228+
f"--build={build_type} --prefix={install_path} && make")
229+
230+
LOG_JOB.info(f"Compiling netperf from source on {address}...")
231+
if session:
232+
remote.copy_files_to(
233+
target_ip,
234+
params.get("cp_client", "scp"),
235+
user,
236+
pwd,
237+
params.get("cp_port", "22"),
238+
netperf_src,
239+
install_path,
240+
600
241+
)
242+
session.cmd(decomp_cmd, timeout=60)
243+
session.cmd(compile_cmd, timeout=600)
244+
else:
245+
process.run(decomp_cmd, shell=True, timeout=60)
246+
process.run(compile_cmd, shell=True, timeout=600)
247+
248+
LOG_JOB.info(f"Netperf compilation completed on {address}")
249+
finally:
250+
if session:
251+
session.close()
252+
return nserver_path, nperf_path

provider/virtual_network/network_base.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ def cleanup_for_iface(iface_type, params):
433433
libvirt_network.create_or_del_network(network_dict, is_del=True)
434434

435435

436-
def check_throughput(serv_runner, cli_runner, ip_addr, bw, th_type):
436+
def check_throughput(serv_runner, cli_runner, ip_addr, bw, th_type,
437+
netserver_cmd='netserver', netperf_cmd='netperf'):
437438
"""
438439
Check actual thoughput of network using netperf
439440
@@ -442,12 +443,15 @@ def check_throughput(serv_runner, cli_runner, ip_addr, bw, th_type):
442443
:param ip_addr: ip address
443444
:param bw: bandwidth setting
444445
:param th_type: inbound or outbound
446+
:param netserver_cmd: netserver command with path (default: 'netserver')
447+
:param netperf_cmd: netperf command with path (default: 'netperf')
445448
"""
446449

447-
serv_runner('netserver')
448-
netperf_out = cli_runner(f'netperf -H {ip_addr}')
450+
serv_runner(netserver_cmd)
451+
netperf_out = cli_runner(f'{netperf_cmd} -H {ip_addr}')
449452
LOG.debug(netperf_out)
450-
serv_runner('pkill netserver')
453+
netserver_bin = os.path.basename(netserver_cmd.split()[0])
454+
serv_runner(f'pkill {netserver_bin}')
451455

452456
actual_throu = float(netperf_out.strip().splitlines()[-1].split()[-1])
453457
expect_throu = int(bw) * 8 / 1024
@@ -757,7 +761,7 @@ def cancel_if_ovs_bridge(params, test):
757761
:param params: Test parameters dict
758762
:param test: Test object
759763
"""
760-
bridge_name = params.get('netdst', '').split(',')[0]
764+
bridge_name = params.get('netdst', '')
761765

762766
try:
763767
br_backend = utils_net.find_bridge_manager(bridge_name)
@@ -775,14 +779,14 @@ def setup_ovs_bridge_attrs(params, iface_attrs):
775779
:param iface_attrs: Interface attributes dictionary to modify
776780
:return: True if OVS configuration applied, False otherwise
777781
"""
778-
bridge_name = params.get('netdst', '').split(',')[0]
779-
is_ovs_bridge = not isinstance(utils_net.find_bridge_manager(bridge_name), utils_net.Bridge)
780-
781-
if is_ovs_bridge:
782-
# Configure for OVS bridge
782+
netdst = params.get('netdst', '')
783+
br_obj = utils_net.find_bridge_manager(netdst)
784+
if br_obj is None:
785+
raise exceptions.TestError(f"Bridge '{netdst}' does not exist")
786+
if "OpenVSwitch" in br_obj.__class__.__name__:
783787
iface_attrs['type_name'] = 'bridge'
784-
iface_attrs['source'] = {'bridge': bridge_name}
788+
iface_attrs['source'] = {'bridge': netdst}
785789
iface_attrs['virtualport'] = {'type': 'openvswitch'}
786-
LOG.debug(f"Configured OVS bridge: {bridge_name}")
790+
LOG.debug(f"Configured OVS bridge: {netdst}")
787791
return True
788792
return False

0 commit comments

Comments
 (0)