Path: blob/master/tools/testing/selftests/drivers/net/xdp.py
29270 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} xdpdrv obj {abs_path} sec {bpf_info.xdp_sec}",115shell=True116)117defer(ip, f"link set dev {cfg.ifname} mtu 1500 xdpdrv 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(cfg, bpf_info, payload_lens):293"""294Tests the XDP_TX action.295296Args:297cfg: Configuration object containing network settings.298bpf_info: BPFProgInfo object containing the BPF program metadata.299payload_lens: Array of packet lengths to send.300"""301cfg.require_cmd("socat", remote=True)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)307308expected_pkts = 0309for payload_len in payload_lens:310test_string = "".join(311random.choice(string.ascii_lowercase) for _ in range(payload_len)312)313314rx_udp = f"socat -{cfg.addr_ipver} -T 2 " + \315f"-u UDP-RECV:{port},reuseport STDOUT"316317# Writing zero bytes to stdin gets ignored by socat,318# but with the shut-null flag socat generates a zero sized packet319# when the socket is closed.320tx_cmd_suffix = ",shut-null" if payload_len == 0 else ""321tx_udp = f"echo -n {test_string} | socat -t 2 " + \322f"-u STDIN UDP:{cfg.baddr}:{port}{tx_cmd_suffix}"323324with bkg(rx_udp, host=cfg.remote, exit_wait=True) as rnc:325wait_port_listen(port, proto="udp", host=cfg.remote)326cmd(tx_udp, host=cfg.remote, shell=True)327328ksft_eq(rnc.stdout.strip(), test_string, "UDP packet exchange failed")329330expected_pkts += 1331stats = _get_stats(prog_info["maps"]["map_xdp_stats"])332ksft_eq(stats[XDPStats.RX.value], expected_pkts, "RX stats mismatch")333ksft_eq(stats[XDPStats.TX.value], expected_pkts, "TX stats mismatch")334335336def test_xdp_native_tx_sb(cfg):337"""338Tests the XDP_TX action for a single-buff case.339340Args:341cfg: Configuration object containing network settings.342"""343bpf_info = BPFProgInfo("xdp_prog", "xdp_native.bpf.o", "xdp", 1500)344345# Ensure there's enough room for an ETH / IP / UDP header346pkt_hdr_len = 42 if cfg.addr_ipver == "4" else 62347348_test_xdp_native_tx(cfg, bpf_info, [0, 1500 // 2, 1500 - pkt_hdr_len])349350351def test_xdp_native_tx_mb(cfg):352"""353Tests the XDP_TX action for a multi-buff case.354355Args:356cfg: Configuration object containing network settings.357"""358bpf_info = BPFProgInfo("xdp_prog_frags", "xdp_native.bpf.o",359"xdp.frags", 9000)360# The first packet ensures we exercise the fragmented code path.361# And the subsequent 0-sized packet ensures the driver362# reinitializes xdp_buff correctly.363_test_xdp_native_tx(cfg, bpf_info, [8000, 0])364365366def _validate_res(res, offset_lst, pkt_sz_lst):367"""368Validates the result of a test.369370Args:371res: The result of the test, which should be a dictionary with a "status" key.372373Raises:374KsftFailEx: If the test fails to pass any combination of offset and packet size.375"""376if "status" not in res:377raise KsftFailEx("Missing 'status' key in result dictionary")378379# Validate that not a single case was successful380if res["status"] == "fail":381if res["offset"] == offset_lst[0] and res["pkt_sz"] == pkt_sz_lst[0]:382raise KsftFailEx(f"{res['reason']}")383384# Get the previous offset and packet size to report the successful run385tmp_idx = offset_lst.index(res["offset"])386prev_offset = offset_lst[tmp_idx - 1]387if tmp_idx == 0:388tmp_idx = pkt_sz_lst.index(res["pkt_sz"])389prev_pkt_sz = pkt_sz_lst[tmp_idx - 1]390else:391prev_pkt_sz = res["pkt_sz"]392393# Use these values for error reporting394ksft_pr(395f"Failed run: pkt_sz {res['pkt_sz']}, offset {res['offset']}. "396f"Last successful run: pkt_sz {prev_pkt_sz}, offset {prev_offset}. "397f"Reason: {res['reason']}"398)399400401def _check_for_failures(recvd_str, stats):402"""403Checks for common failures while adjusting headroom or tailroom.404405Args:406recvd_str: The string received from the remote host after sending a test string.407stats: A dictionary containing formatted packet statistics for various XDP actions.408409Returns:410str: A string describing the failure reason if a failure is detected, otherwise None.411"""412413# Any adjustment failure result in an abort hence, we track this counter414if stats[XDPStats.ABORT.value] != 0:415return "Adjustment failed"416417# Since we are using aggregate stats for a single test across all offsets and packet sizes418# we can't use RX stats only to track data exchange failure without taking a previous419# snapshot. An easier way is to simply check for non-zero length of received string.420if len(recvd_str) == 0:421return "Data exchange failed"422423# Check for RX and PASS stats mismatch. Ideally, they should be equal for a successful run424if stats[XDPStats.RX.value] != stats[XDPStats.PASS.value]:425return "RX stats mismatch"426427return None428429430def _test_xdp_native_tail_adjst(cfg, pkt_sz_lst, offset_lst):431"""432Tests the XDP tail adjustment functionality.433434This function loads the appropriate XDP program based on the provided435program name and configures the XDP map for tail adjustment. It then436validates the tail adjustment by sending and receiving UDP packets437with specified packet sizes and offsets.438439Args:440cfg: Configuration object containing network settings.441prog: Name of the XDP program to load.442pkt_sz_lst: List of packet sizes to test.443offset_lst: List of offsets to validate support for tail adjustment.444445Returns:446dict: A dictionary with test status and failure details if applicable.447"""448port = rand_port()449bpf_info = BPFProgInfo("xdp_prog_frags", "xdp_native.bpf.o", "xdp.frags", 9000)450451prog_info = _load_xdp_prog(cfg, bpf_info)452453# Configure the XDP map for tail adjustment454_set_xdp_map("map_xdp_setup", TestConfig.MODE.value, XDPAction.TAIL_ADJST.value)455_set_xdp_map("map_xdp_setup", TestConfig.PORT.value, port)456457for offset in offset_lst:458tag = format(random.randint(65, 90), "02x")459460_set_xdp_map("map_xdp_setup", TestConfig.ADJST_OFFSET.value, offset)461if offset > 0:462_set_xdp_map("map_xdp_setup", TestConfig.ADJST_TAG.value, int(tag, 16))463464for pkt_sz in pkt_sz_lst:465test_str = "".join(random.choice(string.ascii_lowercase) for _ in range(pkt_sz))466recvd_str = _exchg_udp(cfg, port, test_str)467stats = _get_stats(prog_info["maps"]["map_xdp_stats"])468469failure = _check_for_failures(recvd_str, stats)470if failure is not None:471return {472"status": "fail",473"reason": failure,474"offset": offset,475"pkt_sz": pkt_sz,476}477478# Validate data content based on offset direction479expected_data = None480if offset > 0:481expected_data = test_str + (offset * chr(int(tag, 16)))482else:483expected_data = test_str[0:pkt_sz + offset]484485if recvd_str != expected_data:486return {487"status": "fail",488"reason": "Data mismatch",489"offset": offset,490"pkt_sz": pkt_sz,491}492493return {"status": "pass"}494495496def test_xdp_native_adjst_tail_grow_data(cfg):497"""498Tests the XDP tail adjustment by growing packet data.499500Args:501cfg: Configuration object containing network settings.502"""503pkt_sz_lst = [512, 1024, 2048]504offset_lst = [1, 16, 32, 64, 128, 256]505res = _test_xdp_native_tail_adjst(506cfg,507pkt_sz_lst,508offset_lst,509)510511_validate_res(res, offset_lst, pkt_sz_lst)512513514def test_xdp_native_adjst_tail_shrnk_data(cfg):515"""516Tests the XDP tail adjustment by shrinking packet data.517518Args:519cfg: Configuration object containing network settings.520"""521pkt_sz_lst = [512, 1024, 2048]522offset_lst = [-16, -32, -64, -128, -256]523res = _test_xdp_native_tail_adjst(524cfg,525pkt_sz_lst,526offset_lst,527)528529_validate_res(res, offset_lst, pkt_sz_lst)530531532def get_hds_thresh(cfg):533"""534Retrieves the header data split (HDS) threshold for a network interface.535536Args:537cfg: Configuration object containing network settings.538539Returns:540The HDS threshold value. If the threshold is not supported or an error occurs,541a default value of 1500 is returned.542"""543netnl = cfg.netnl544hds_thresh = 1500545546try:547rings = netnl.rings_get({'header': {'dev-index': cfg.ifindex}})548if 'hds-thresh' not in rings:549ksft_pr(f'hds-thresh not supported. Using default: {hds_thresh}')550return hds_thresh551hds_thresh = rings['hds-thresh']552except NlError as e:553ksft_pr(f"Failed to get rings: {e}. Using default: {hds_thresh}")554555return hds_thresh556557558def _test_xdp_native_head_adjst(cfg, prog, pkt_sz_lst, offset_lst):559"""560Tests the XDP head adjustment action for a multi-buffer case.561562Args:563cfg: Configuration object containing network settings.564netnl: Network namespace or link object (not used in this function).565566This function sets up the packet size and offset lists, then performs567the head adjustment test by sending and receiving UDP packets.568"""569cfg.require_cmd("socat", remote=True)570571prog_info = _load_xdp_prog(cfg, BPFProgInfo(prog, "xdp_native.bpf.o", "xdp.frags", 9000))572port = rand_port()573574_set_xdp_map("map_xdp_setup", TestConfig.MODE.value, XDPAction.HEAD_ADJST.value)575_set_xdp_map("map_xdp_setup", TestConfig.PORT.value, port)576577hds_thresh = get_hds_thresh(cfg)578for offset in offset_lst:579for pkt_sz in pkt_sz_lst:580# The "head" buffer must contain at least the Ethernet header581# after we eat into it. We send large-enough packets, but if HDS582# is enabled head will only contain headers. Don't try to eat583# more than 28 bytes (UDPv4 + eth hdr left: (14 + 20 + 8) - 14)584l2_cut_off = 28 if cfg.addr_ipver == 4 else 48585if pkt_sz > hds_thresh and offset > l2_cut_off:586ksft_pr(587f"Failed run: pkt_sz ({pkt_sz}) > HDS threshold ({hds_thresh}) and "588f"offset {offset} > {l2_cut_off}"589)590return {"status": "pass"}591592test_str = ''.join(random.choice(string.ascii_lowercase) for _ in range(pkt_sz))593tag = format(random.randint(65, 90), '02x')594595_set_xdp_map("map_xdp_setup",596TestConfig.ADJST_OFFSET.value,597offset)598_set_xdp_map("map_xdp_setup", TestConfig.ADJST_TAG.value, int(tag, 16))599_set_xdp_map("map_xdp_setup", TestConfig.ADJST_OFFSET.value, offset)600601recvd_str = _exchg_udp(cfg, port, test_str)602603# Check for failures around adjustment and data exchange604failure = _check_for_failures(recvd_str, _get_stats(prog_info['maps']['map_xdp_stats']))605if failure is not None:606return {607"status": "fail",608"reason": failure,609"offset": offset,610"pkt_sz": pkt_sz611}612613# Validate data content based on offset direction614expected_data = None615if offset < 0:616expected_data = chr(int(tag, 16)) * (0 - offset) + test_str617else:618expected_data = test_str[offset:]619620if recvd_str != expected_data:621return {622"status": "fail",623"reason": "Data mismatch",624"offset": offset,625"pkt_sz": pkt_sz626}627628return {"status": "pass"}629630631def test_xdp_native_adjst_head_grow_data(cfg):632"""633Tests the XDP headroom growth support.634635Args:636cfg: Configuration object containing network settings.637638This function sets up the packet size and offset lists, then calls the639_test_xdp_native_head_adjst_mb function to perform the actual test. The640test is passed if the headroom is successfully extended for given packet641sizes and offsets.642"""643pkt_sz_lst = [512, 1024, 2048]644645# Negative values result in headroom shrinking, resulting in growing of payload646offset_lst = [-16, -32, -64, -128, -256]647res = _test_xdp_native_head_adjst(cfg, "xdp_prog_frags", pkt_sz_lst, offset_lst)648649_validate_res(res, offset_lst, pkt_sz_lst)650651652def test_xdp_native_adjst_head_shrnk_data(cfg):653"""654Tests the XDP headroom shrinking support.655656Args:657cfg: Configuration object containing network settings.658659This function sets up the packet size and offset lists, then calls the660_test_xdp_native_head_adjst_mb function to perform the actual test. The661test is passed if the headroom is successfully shrunk for given packet662sizes and offsets.663"""664pkt_sz_lst = [512, 1024, 2048]665666# Positive values result in headroom growing, resulting in shrinking of payload667offset_lst = [16, 32, 64, 128, 256]668res = _test_xdp_native_head_adjst(cfg, "xdp_prog_frags", pkt_sz_lst, offset_lst)669670_validate_res(res, offset_lst, pkt_sz_lst)671672673def main():674"""675Main function to execute the XDP tests.676677This function runs a series of tests to validate the XDP support for678both the single and multi-buffer. It uses the NetDrvEpEnv context679manager to manage the network driver environment and the ksft_run680function to execute the tests.681"""682with NetDrvEpEnv(__file__) as cfg:683cfg.netnl = EthtoolFamily()684ksft_run(685[686test_xdp_native_pass_sb,687test_xdp_native_pass_mb,688test_xdp_native_drop_sb,689test_xdp_native_drop_mb,690test_xdp_native_tx_sb,691test_xdp_native_tx_mb,692test_xdp_native_adjst_tail_grow_data,693test_xdp_native_adjst_tail_shrnk_data,694test_xdp_native_adjst_head_grow_data,695test_xdp_native_adjst_head_shrnk_data,696],697args=(cfg,))698ksft_exit()699700701if __name__ == "__main__":702main()703704705