Create a veth pair with VETH_INFO_TX_EXTRA_HEADROOM set to 64 bytes through YNL and move the peer to another netns. Check that both ends report 64, and that tx_extra_headroom of the sending end is 64. With XDP on the peer it is 64 + 256, because the two values are added. Also check that a value above 512 is rejected. The helpers leave the RtnlFamily socket to garbage collection, as link_netns.py does. Signed-off-by: Tianyi Gao --- v2: - Test VETH_INFO_TX_EXTRA_HEADROOM with YNL only (Jakub). The IFLA tests of v1 4/8 are gone, so the patch no longer carries David Keogh's sign-off. - Drop the VLAN case of v1 4/8. In v2 no code copies tx_extra_headroom to a stacked device. --- .../selftests/net/tx_extra_headroom.py | 61 +++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/net/tx_extra_headroom.py b/tools/testing/selftests/net/tx_extra_headroom.py index 122a1a1b5ca9..a4e4195179fa 100755 --- a/tools/testing/selftests/net/tx_extra_headroom.py +++ b/tools/testing/selftests/net/tx_extra_headroom.py @@ -3,15 +3,17 @@ """Tests for tx_extra_headroom.""" +import errno import socket import struct from pathlib import Path -from lib.py import ksft_run, ksft_exit, ksft_eq, ksft_ge, ksft_gt +from lib.py import ksft_run, ksft_exit, ksft_eq, ksft_ge, ksft_gt, ksft_raises from lib.py import KsftSkipEx, KsftFailEx from lib.py import ksft_busy_wait, ksft_variants, KsftNamedVariant from lib.py import cmd, defer, ip, bpftool, bpf_prog_map_ids from lib.py import NetNS, NetNSEnter +from lib.py import Netlink, NlError, RtnlFamily BPF_OBJ = Path(__file__).parent / "tx_extra_headroom.bpf.o" XDP_OBJ = Path(__file__).parent / "lib/xdp_dummy.bpf.o" @@ -37,9 +39,34 @@ def _setup_addrs(peer_ns, dev, peer): ip(f"neigh replace {addr} lladdr {peer_mac} dev {dev} nud permanent") -def _setup_veth(peer_ns): +def _add_veth(tx_extra_headroom): + """Create veth0 and veth1 with VETH_INFO_TX_EXTRA_HEADROOM through YNL.""" + # Without names, the peer is created first and becomes veth0. + rtnl = RtnlFamily() + rtnl.newlink( + { + "linkinfo": { + "kind": "veth", + "data": {"tx-extra-headroom": tx_extra_headroom}, + }, + }, + flags=[Netlink.NLM_F_CREATE, Netlink.NLM_F_EXCL], + ) + + +def _veth_tx_extra_headroom(name): + """Return VETH_INFO_TX_EXTRA_HEADROOM of veth device name.""" + rtnl = RtnlFamily() + link = rtnl.getlink({"ifname": name}) + return link["linkinfo"]["data"]["tx-extra-headroom"] + + +def _setup_veth(peer_ns, tx_extra_headroom=None): """Create veth0 in the current netns and its peer veth1 in peer_ns.""" - ip("link add veth0 type veth peer name veth1") + if tx_extra_headroom is None: + ip("link add veth0 type veth peer name veth1") + else: + _add_veth(tx_extra_headroom) ip(f"link set veth1 netns {peer_ns}") _setup_addrs(peer_ns, "veth0", "veth1") @@ -353,6 +380,30 @@ def test_tag_max() -> None: ksft_eq(bss["tx_extra_headroom"], TX_EXTRA_HEADROOM_MAX) +def test_veth_info() -> None: + """Check that VETH_INFO_TX_EXTRA_HEADROOM adds to tx_extra_headroom.""" + with NetNS() as send_ns, NetNS() as peer_ns, NetNSEnter(str(send_ns)): + _setup_veth(peer_ns, tx_extra_headroom=64) + ksft_eq(_veth_tx_extra_headroom("veth0"), 64) + with NetNSEnter(str(peer_ns)): + ksft_eq(_veth_tx_extra_headroom("veth1"), 64) + map_id = _load_progs() + _attach_tcx("veth0", "egress", "record") + _send_udp4() + ksft_eq(_read_bss(map_id)["tx_extra_headroom"], 64) + _attach_xdp(peer_ns) + _send_udp4() + ksft_eq(_read_bss(map_id)["tx_extra_headroom"], + 64 + XDP_PACKET_HEADROOM) + + +def test_veth_info_above_max() -> None: + """Check that a value above the maximum is rejected.""" + with NetNS() as ns, NetNSEnter(str(ns)), ksft_raises(NlError) as cm: + _add_veth(TX_EXTRA_HEADROOM_MAX + 1) + ksft_eq(cm.exception.error, errno.ERANGE) + + def main() -> None: """Run all test cases.""" ksft_run([test_peer_xdp, @@ -365,7 +416,9 @@ def main() -> None: test_push_detach, test_push_peer_xdp, test_push_two_progs, - test_tag_max]) + test_tag_max, + test_veth_info, + test_veth_info_above_max]) ksft_exit() -- 2.56.0