Path: blob/master/tools/testing/selftests/drivers/net/xdp.py
26288 views
#!/usr/bin/env python31# SPDX-License-Identifier: GPL-2.023"""4This file contains tests to verify native XDP support in network drivers.5The tests utilize the BPF program `xdp_native.bpf.o` from the `selftests.net.lib`6directory, with each test focusing on a specific aspect of XDP functionality.7"""8import random9import string10from dataclasses import dataclass11from enum import Enum1213from lib.py import ksft_run, ksft_exit, ksft_eq, ksft_ne, ksft_pr14from lib.py import KsftFailEx, NetDrvEpEnv, EthtoolFamily, NlError15from lib.py import bkg, cmd, rand_port, wait_port_listen16from lib.py import ip, bpftool, defer171819class TestConfig(Enum):20"""Enum for XDP configuration options."""21MODE = 0 # Configures the BPF program for a specific test22PORT = 1 # Port configuration to communicate with the remote host23ADJST_OFFSET = 2 # Tail/Head adjustment offset for extension/shrinking24ADJST_TAG = 3 # Adjustment tag to annotate the start and end of extension252627class XDPAction(Enum):28"""Enum for XDP actions."""29PASS = 0 # Pass the packet up to the stack30DROP = 1 # Drop the packet31TX = 2 # Route the packet to the remote host32TAIL_ADJST = 3 # Adjust the tail of the packet33HEAD_ADJST = 4 # Adjust the head of the packet343536class XDPStats(Enum):37"""Enum for XDP statistics."""38RX = 0 # Count of valid packets received for testing39PASS = 1 # Count of packets passed up to the stack40DROP = 2 # Count of packets dropped41TX = 3 # Count of incoming packets routed to the remote host42ABORT = 4 # Count of packets that were aborted434445@dataclass46class BPFProgInfo:47"""Data class to store information about a BPF program."""48name: str # Name of the BPF program49file: str # BPF program object file50xdp_sec: str = "xdp" # XDP section name (e.g., "xdp" or "xdp.frags")51mtu: int = 1500 # Maximum Transmission Unit, default is 1500525354def _exchg_udp(cfg, port, test_string):55"""56Exchanges UDP packets between a local and remote host using the socat tool.5758Args:59cfg: Configuration object containing network settings.60port: Port number to use for the UDP communication.61test_string: String that the remote host will send.6263Returns:64The string received by the test host.65"""66cfg.require_cmd("socat", remote=True)6768rx_udp_cmd = f"socat -{cfg.addr_ipver} -T 2 -u UDP-RECV:{port},reuseport STDOUT"69tx_udp_cmd = f"echo -n {test_string} | socat -t 2 -u STDIN UDP:{cfg.baddr}:{port}"7071with bkg(rx_udp_cmd, exit_wait=True) as nc:72wait_port_listen(port, proto="udp")73cmd(tx_udp_cmd, host=cfg.remote, shell=True)7475return nc.stdout.strip()767778def _test_udp(cfg, port, size=256):79"""80Tests UDP packet exchange between a local and remote host.8182Args:83cfg: Configuration object containing network settings.84port: Port number to use for the UDP communication.85size: The length of the test string to be exchanged, default is 256 characters.8687Returns:88bool: True if the received string matches the sent string, False otherwise.89"""90test_str = "".join(random.choice(string.ascii_lowercase) for _ in range(size))91recvd_str = _exchg_udp(cfg, port, test_str)9293return recvd_str == test_str949596def _load_xdp_prog(cfg, bpf_info):97"""98Loads an XDP program onto a network interface.99100Args:101cfg: Configuration object containing network settings.102bpf_info: BPFProgInfo object containing information about the BPF program.103104Returns:105dict: A dictionary containing the XDP program ID, name, and associated map IDs.106"""107abs_path = cfg.net_lib_dir / bpf_info.file108prog_info = {}109110cmd(f"ip link set dev {cfg.remote_ifname} mtu {bpf_info.mtu}", shell=True, host=cfg.remote)111defer(ip, f"link set dev {cfg.remote_ifname} mtu 1500", host=cfg.remote)112113cmd(114f"ip link set dev {cfg.ifname} mtu {bpf_info.mtu} xdp obj {abs_path} sec {bpf_info.xdp_sec}",115shell=True116)117defer(ip, f"link set dev {cfg.ifname} mtu 1500 xdp off")118119xdp_info = ip(f"-d link show dev {cfg.ifname}", json=True)[0]120prog_info["id"] = xdp_info["xdp"]["prog"]["id"]121prog_info["name"] = xdp_info["xdp"]["prog"]["name"]122prog_id = prog_info["id"]123124map_ids = bpftool(f"prog show id {prog_id}", json=True)["map_ids"]125prog_info["maps"] = {}126for map_id in map_ids:127name = bpftool(f"map show id {map_id}", json=True)["name"]128prog_info["maps"][name] = map_id129130return prog_info131132133def format_hex_bytes(value):134"""135Helper function that converts an integer into a formatted hexadecimal byte string.136137Args:138value: An integer representing the number to be converted.139140Returns:141A string representing hexadecimal equivalent of value, with bytes separated by spaces.142"""143hex_str = value.to_bytes(4, byteorder='little', signed=True)144return ' '.join(f'{byte:02x}' for byte in hex_str)145146147def _set_xdp_map(map_name, key, value):148"""149Updates an XDP map with a given key-value pair using bpftool.150151Args:152map_name: The name of the XDP map to update.153key: The key to update in the map, formatted as a hexadecimal string.154value: The value to associate with the key, formatted as a hexadecimal string.155"""156key_formatted = format_hex_bytes(key)157value_formatted = format_hex_bytes(value)158bpftool(159f"map update name {map_name} key hex {key_formatted} value hex {value_formatted}"160)161162163def _get_stats(xdp_map_id):164"""165Retrieves and formats statistics from an XDP map.166167Args:168xdp_map_id: The ID of the XDP map from which to retrieve statistics.169170Returns:171A dictionary containing formatted packet statistics for various XDP actions.172The keys are based on the XDPStats Enum values.173174Raises:175KsftFailEx: If the stats retrieval fails.176"""177stats_dump = bpftool(f"map dump id {xdp_map_id}", json=True)178if not stats_dump:179raise KsftFailEx(f"Failed to get stats for map {xdp_map_id}")180181stats_formatted = {}182for key in range(0, 5):183val = stats_dump[key]["formatted"]["value"]184if stats_dump[key]["formatted"]["key"] == XDPStats.RX.value:185stats_formatted[XDPStats.RX.value] = val186elif stats_dump[key]["formatted"]["key"] == XDPStats.PASS.value:187stats_formatted[XDPStats.PASS.value] = val188elif stats_dump[key]["formatted"]["key"] == XDPStats.DROP.value:189stats_formatted[XDPStats.DROP.value] = val190elif stats_dump[key]["formatted"]["key"] == XDPStats.TX.value:191stats_formatted[XDPStats.TX.value] = val192elif stats_dump[key]["formatted"]["key"] == XDPStats.ABORT.value:193stats_formatted[XDPStats.ABORT.value] = val194195return stats_formatted196197198def _test_pass(cfg, bpf_info, msg_sz):199"""200Tests the XDP_PASS action by exchanging UDP packets.201202Args:203cfg: Configuration object containing network settings.204bpf_info: BPFProgInfo object containing information about the BPF program.205msg_sz: Size of the test message to send.206"""207208prog_info = _load_xdp_prog(cfg, bpf_info)209port = rand_port()210211_set_xdp_map("map_xdp_setup", TestConfig.MODE.value, XDPAction.PASS.value)212_set_xdp_map("map_xdp_setup", TestConfig.PORT.value, port)213214ksft_eq(_test_udp(cfg, port, msg_sz), True, "UDP packet exchange failed")215stats = _get_stats(prog_info["maps"]["map_xdp_stats"])216217ksft_ne(stats[XDPStats.RX.value], 0, "RX stats should not be zero")218ksft_eq(stats[XDPStats.RX.value], stats[XDPStats.PASS.value], "RX and PASS stats mismatch")219220221def test_xdp_native_pass_sb(cfg):222"""223Tests the XDP_PASS action for single buffer case.224225Args:226cfg: Configuration object containing network settings.227"""228bpf_info = BPFProgInfo("xdp_prog", "xdp_native.bpf.o", "xdp", 1500)229230_test_pass(cfg, bpf_info, 256)231232233def test_xdp_native_pass_mb(cfg):234"""235Tests the XDP_PASS action for a multi-buff size.236237Args:238cfg: Configuration object containing network settings.239"""240bpf_info = BPFProgInfo("xdp_prog_frags", "xdp_native.bpf.o", "xdp.frags", 9000)241242_test_pass(cfg, bpf_info, 8000)243244245def _test_drop(cfg, bpf_info, msg_sz):246"""247Tests the XDP_DROP action by exchanging UDP packets.248249Args:250cfg: Configuration object containing network settings.251bpf_info: BPFProgInfo object containing information about the BPF program.252msg_sz: Size of the test message to send.253"""254255prog_info = _load_xdp_prog(cfg, bpf_info)256port = rand_port()257258_set_xdp_map("map_xdp_setup", TestConfig.MODE.value, XDPAction.DROP.value)259_set_xdp_map("map_xdp_setup", TestConfig.PORT.value, port)260261ksft_eq(_test_udp(cfg, port, msg_sz), False, "UDP packet exchange should fail")262stats = _get_stats(prog_info["maps"]["map_xdp_stats"])263264ksft_ne(stats[XDPStats.RX.value], 0, "RX stats should be zero")265ksft_eq(stats[XDPStats.RX.value], stats[XDPStats.DROP.value], "RX and DROP stats mismatch")266267268def test_xdp_native_drop_sb(cfg):269"""270Tests the XDP_DROP action for a signle-buff case.271272Args:273cfg: Configuration object containing network settings.274"""275bpf_info = BPFProgInfo("xdp_prog", "xdp_native.bpf.o", "xdp", 1500)276277_test_drop(cfg, bpf_info, 256)278279280def test_xdp_native_drop_mb(cfg):281"""282Tests the XDP_DROP action for a multi-buff case.283284Args:285cfg: Configuration object containing network settings.286"""287bpf_info = BPFProgInfo("xdp_prog_frags", "xdp_native.bpf.o", "xdp.frags", 9000)288289_test_drop(cfg, bpf_info, 8000)290291292def test_xdp_native_tx_mb(cfg):293"""294Tests the XDP_TX action for a multi-buff case.295296Args:297cfg: Configuration object containing network settings.298"""299cfg.require_cmd("socat", remote=True)300301bpf_info = BPFProgInfo("xdp_prog_frags", "xdp_native.bpf.o", "xdp.frags", 9000)302prog_info = _load_xdp_prog(cfg, bpf_info)303port = rand_port()304305_set_xdp_map("map_xdp_setup", TestConfig.MODE.value, XDPAction.TX.value)306_set_xdp_map("map_xdp_setup", TestConfig.PORT.value, port)307308test_string = ''.join(random.choice(string.ascii_lowercase) for _ in range(8000))309rx_udp = f"socat -{cfg.addr_ipver} -T 2 -u UDP-RECV:{port},reuseport STDOUT"310tx_udp = f"echo {test_string} | socat -t 2 -u STDIN UDP:{cfg.baddr}:{port}"311312with bkg(rx_udp, host=cfg.remote, exit_wait=True) as rnc:313wait_port_listen(port, proto="udp", host=cfg.remote)314cmd(tx_udp, host=cfg.remote, shell=True)315316stats = _get_stats(prog_info['maps']['map_xdp_stats'])317318ksft_eq(rnc.stdout.strip(), test_string, "UDP packet exchange failed")319ksft_eq(stats[XDPStats.TX.value], 1, "TX stats mismatch")320321322def _validate_res(res, offset_lst, pkt_sz_lst):323"""324Validates the result of a test.325326Args:327res: The result of the test, which should be a dictionary with a "status" key.328329Raises:330KsftFailEx: If the test fails to pass any combination of offset and packet size.331"""332if "status" not in res:333raise KsftFailEx("Missing 'status' key in result dictionary")334335# Validate that not a single case was successful336if res["status"] == "fail":337if res["offset"] == offset_lst[0] and res["pkt_sz"] == pkt_sz_lst[0]:338raise KsftFailEx(f"{res['reason']}")339340# Get the previous offset and packet size to report the successful run341tmp_idx = offset_lst.index(res["offset"])342prev_offset = offset_lst[tmp_idx - 1]343if tmp_idx == 0:344tmp_idx = pkt_sz_lst.index(res["pkt_sz"])345prev_pkt_sz = pkt_sz_lst[tmp_idx - 1]346else:347prev_pkt_sz = res["pkt_sz"]348349# Use these values for error reporting350ksft_pr(351f"Failed run: pkt_sz {res['pkt_sz']}, offset {res['offset']}. "352f"Last successful run: pkt_sz {prev_pkt_sz}, offset {prev_offset}. "353f"Reason: {res['reason']}"354)355356357def _check_for_failures(recvd_str, stats):358"""359Checks for common failures while adjusting headroom or tailroom.360361Args:362recvd_str: The string received from the remote host after sending a test string.363stats: A dictionary containing formatted packet statistics for various XDP actions.364365Returns:366str: A string describing the failure reason if a failure is detected, otherwise None.367"""368369# Any adjustment failure result in an abort hence, we track this counter370if stats[XDPStats.ABORT.value] != 0:371return "Adjustment failed"372373# Since we are using aggregate stats for a single test across all offsets and packet sizes374# we can't use RX stats only to track data exchange failure without taking a previous375# snapshot. An easier way is to simply check for non-zero length of received string.376if len(recvd_str) == 0:377return "Data exchange failed"378379# Check for RX and PASS stats mismatch. Ideally, they should be equal for a successful run380if stats[XDPStats.RX.value] != stats[XDPStats.PASS.value]:381return "RX stats mismatch"382383return None384385386def _test_xdp_native_tail_adjst(cfg, pkt_sz_lst, offset_lst):387"""388Tests the XDP tail adjustment functionality.389390This function loads the appropriate XDP program based on the provided391program name and configures the XDP map for tail adjustment. It then392validates the tail adjustment by sending and receiving UDP packets393with specified packet sizes and offsets.394395Args:396cfg: Configuration object containing network settings.397prog: Name of the XDP program to load.398pkt_sz_lst: List of packet sizes to test.399offset_lst: List of offsets to validate support for tail adjustment.400401Returns:402dict: A dictionary with test status and failure details if applicable.403"""404port = rand_port()405bpf_info = BPFProgInfo("xdp_prog_frags", "xdp_native.bpf.o", "xdp.frags", 9000)406407prog_info = _load_xdp_prog(cfg, bpf_info)408409# Configure the XDP map for tail adjustment410_set_xdp_map("map_xdp_setup", TestConfig.MODE.value, XDPAction.TAIL_ADJST.value)411_set_xdp_map("map_xdp_setup", TestConfig.PORT.value, port)412413for offset in offset_lst:414tag = format(random.randint(65, 90), "02x")415416_set_xdp_map("map_xdp_setup", TestConfig.ADJST_OFFSET.value, offset)417if offset > 0:418_set_xdp_map("map_xdp_setup", TestConfig.ADJST_TAG.value, int(tag, 16))419420for pkt_sz in pkt_sz_lst:421test_str = "".join(random.choice(string.ascii_lowercase) for _ in range(pkt_sz))422recvd_str = _exchg_udp(cfg, port, test_str)423stats = _get_stats(prog_info["maps"]["map_xdp_stats"])424425failure = _check_for_failures(recvd_str, stats)426if failure is not None:427return {428"status": "fail",429"reason": failure,430"offset": offset,431"pkt_sz": pkt_sz,432}433434# Validate data content based on offset direction435expected_data = None436if offset > 0:437expected_data = test_str + (offset * chr(int(tag, 16)))438else:439expected_data = test_str[0:pkt_sz + offset]440441if recvd_str != expected_data:442return {443"status": "fail",444"reason": "Data mismatch",445"offset": offset,446"pkt_sz": pkt_sz,447}448449return {"status": "pass"}450451452def test_xdp_native_adjst_tail_grow_data(cfg):453"""454Tests the XDP tail adjustment by growing packet data.455456Args:457cfg: Configuration object containing network settings.458"""459pkt_sz_lst = [512, 1024, 2048]460offset_lst = [1, 16, 32, 64, 128, 256]461res = _test_xdp_native_tail_adjst(462cfg,463pkt_sz_lst,464offset_lst,465)466467_validate_res(res, offset_lst, pkt_sz_lst)468469470def test_xdp_native_adjst_tail_shrnk_data(cfg):471"""472Tests the XDP tail adjustment by shrinking packet data.473474Args:475cfg: Configuration object containing network settings.476"""477pkt_sz_lst = [512, 1024, 2048]478offset_lst = [-16, -32, -64, -128, -256]479res = _test_xdp_native_tail_adjst(480cfg,481pkt_sz_lst,482offset_lst,483)484485_validate_res(res, offset_lst, pkt_sz_lst)486487488def get_hds_thresh(cfg):489"""490Retrieves the header data split (HDS) threshold for a network interface.491492Args:493cfg: Configuration object containing network settings.494495Returns:496The HDS threshold value. If the threshold is not supported or an error occurs,497a default value of 1500 is returned.498"""499netnl = cfg.netnl500hds_thresh = 1500501502try:503rings = netnl.rings_get({'header': {'dev-index': cfg.ifindex}})504if 'hds-thresh' not in rings:505ksft_pr(f'hds-thresh not supported. Using default: {hds_thresh}')506return hds_thresh507hds_thresh = rings['hds-thresh']508except NlError as e:509ksft_pr(f"Failed to get rings: {e}. Using default: {hds_thresh}")510511return hds_thresh512513514def _test_xdp_native_head_adjst(cfg, prog, pkt_sz_lst, offset_lst):515"""516Tests the XDP head adjustment action for a multi-buffer case.517518Args:519cfg: Configuration object containing network settings.520netnl: Network namespace or link object (not used in this function).521522This function sets up the packet size and offset lists, then performs523the head adjustment test by sending and receiving UDP packets.524"""525cfg.require_cmd("socat", remote=True)526527prog_info = _load_xdp_prog(cfg, BPFProgInfo(prog, "xdp_native.bpf.o", "xdp.frags", 9000))528port = rand_port()529530_set_xdp_map("map_xdp_setup", TestConfig.MODE.value, XDPAction.HEAD_ADJST.value)531_set_xdp_map("map_xdp_setup", TestConfig.PORT.value, port)532533hds_thresh = get_hds_thresh(cfg)534for offset in offset_lst:535for pkt_sz in pkt_sz_lst:536# The "head" buffer must contain at least the Ethernet header537# after we eat into it. We send large-enough packets, but if HDS538# is enabled head will only contain headers. Don't try to eat539# more than 28 bytes (UDPv4 + eth hdr left: (14 + 20 + 8) - 14)540l2_cut_off = 28 if cfg.addr_ipver == 4 else 48541if pkt_sz > hds_thresh and offset > l2_cut_off:542ksft_pr(543f"Failed run: pkt_sz ({pkt_sz}) > HDS threshold ({hds_thresh}) and "544f"offset {offset} > {l2_cut_off}"545)546return {"status": "pass"}547548test_str = ''.join(random.choice(string.ascii_lowercase) for _ in range(pkt_sz))549tag = format(random.randint(65, 90), '02x')550551_set_xdp_map("map_xdp_setup",552TestConfig.ADJST_OFFSET.value,553offset)554_set_xdp_map("map_xdp_setup", TestConfig.ADJST_TAG.value, int(tag, 16))555_set_xdp_map("map_xdp_setup", TestConfig.ADJST_OFFSET.value, offset)556557recvd_str = _exchg_udp(cfg, port, test_str)558559# Check for failures around adjustment and data exchange560failure = _check_for_failures(recvd_str, _get_stats(prog_info['maps']['map_xdp_stats']))561if failure is not None:562return {563"status": "fail",564"reason": failure,565"offset": offset,566"pkt_sz": pkt_sz567}568569# Validate data content based on offset direction570expected_data = None571if offset < 0:572expected_data = chr(int(tag, 16)) * (0 - offset) + test_str573else:574expected_data = test_str[offset:]575576if recvd_str != expected_data:577return {578"status": "fail",579"reason": "Data mismatch",580"offset": offset,581"pkt_sz": pkt_sz582}583584return {"status": "pass"}585586587def test_xdp_native_adjst_head_grow_data(cfg):588"""589Tests the XDP headroom growth support.590591Args:592cfg: Configuration object containing network settings.593594This function sets up the packet size and offset lists, then calls the595_test_xdp_native_head_adjst_mb function to perform the actual test. The596test is passed if the headroom is successfully extended for given packet597sizes and offsets.598"""599pkt_sz_lst = [512, 1024, 2048]600601# Negative values result in headroom shrinking, resulting in growing of payload602offset_lst = [-16, -32, -64, -128, -256]603res = _test_xdp_native_head_adjst(cfg, "xdp_prog_frags", pkt_sz_lst, offset_lst)604605_validate_res(res, offset_lst, pkt_sz_lst)606607608def test_xdp_native_adjst_head_shrnk_data(cfg):609"""610Tests the XDP headroom shrinking support.611612Args:613cfg: Configuration object containing network settings.614615This function sets up the packet size and offset lists, then calls the616_test_xdp_native_head_adjst_mb function to perform the actual test. The617test is passed if the headroom is successfully shrunk for given packet618sizes and offsets.619"""620pkt_sz_lst = [512, 1024, 2048]621622# Positive values result in headroom growing, resulting in shrinking of payload623offset_lst = [16, 32, 64, 128, 256]624res = _test_xdp_native_head_adjst(cfg, "xdp_prog_frags", pkt_sz_lst, offset_lst)625626_validate_res(res, offset_lst, pkt_sz_lst)627628629def main():630"""631Main function to execute the XDP tests.632633This function runs a series of tests to validate the XDP support for634both the single and multi-buffer. It uses the NetDrvEpEnv context635manager to manage the network driver environment and the ksft_run636function to execute the tests.637"""638with NetDrvEpEnv(__file__) as cfg:639cfg.netnl = EthtoolFamily()640ksft_run(641[642test_xdp_native_pass_sb,643test_xdp_native_pass_mb,644test_xdp_native_drop_sb,645test_xdp_native_drop_mb,646test_xdp_native_tx_mb,647test_xdp_native_adjst_tail_grow_data,648test_xdp_native_adjst_tail_shrnk_data,649test_xdp_native_adjst_head_grow_data,650test_xdp_native_adjst_head_shrnk_data,651],652args=(cfg,))653ksft_exit()654655656if __name__ == "__main__":657main()658659660