Path: blob/master/tools/testing/selftests/drivers/net/hw/xsk_reconfig.py
26295 views
#!/usr/bin/env python31# SPDX-License-Identifier: GPL-2.023# This is intended to be run on a virtio-net guest interface.4# The test binds the XDP socket to the interface without setting5# the fill ring to trigger delayed refill_work. This helps to6# make it easier to reproduce the deadlock when XDP program,7# XDP socket bind/unbind, rx ring resize race with refill_work on8# the buggy kernel.9#10# The Qemu command to setup virtio-net11# -netdev tap,id=hostnet1,vhost=on,script=no,downscript=no12# -device virtio-net-pci,netdev=hostnet1,iommu_platform=on,disable-legacy=on1314from lib.py import ksft_exit, ksft_run15from lib.py import KsftSkipEx, KsftFailEx16from lib.py import NetDrvEnv17from lib.py import bkg, ip, cmd, ethtool18import time1920def _get_rx_ring_entries(cfg):21output = ethtool(f"-g {cfg.ifname}", json=True)22return output[0]["rx"]2324def setup_xsk(cfg, xdp_queue_id = 0) -> bkg:25# Probe for support26xdp = cmd(f'{cfg.net_lib_dir / "xdp_helper"} - -', fail=False)27if xdp.ret == 255:28raise KsftSkipEx('AF_XDP unsupported')29elif xdp.ret > 0:30raise KsftFailEx('unable to create AF_XDP socket')3132try:33return bkg(f'{cfg.net_lib_dir / "xdp_helper"} {cfg.ifindex} ' \34'{xdp_queue_id} -z', ksft_wait=3)35except:36raise KsftSkipEx('Failed to bind XDP socket in zerocopy.\n' \37'Please consider adding iommu_platform=on ' \38'when setting up virtio-net-pci')3940def check_xdp_bind(cfg):41with setup_xsk(cfg):42ip(f"link set dev %s xdp obj %s sec xdp" %43(cfg.ifname, cfg.net_lib_dir / "xdp_dummy.bpf.o"))44ip(f"link set dev %s xdp off" % cfg.ifname)4546def check_rx_resize(cfg):47with setup_xsk(cfg):48rx_ring = _get_rx_ring_entries(cfg)49ethtool(f"-G %s rx %d" % (cfg.ifname, rx_ring // 2))50ethtool(f"-G %s rx %d" % (cfg.ifname, rx_ring))5152def main():53with NetDrvEnv(__file__, nsim_test=False) as cfg:54ksft_run([check_xdp_bind, check_rx_resize],55args=(cfg, ))56ksft_exit()5758if __name__ == "__main__":59main()606162