tools/testing/selftests/net/link_netns.py

Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/link_netns.py

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/net/link_netns.py
Extension
.py
Size
6926 bytes
Lines
199
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: tools
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0

import time

from lib.py import ksft_run, ksft_exit, ksft_eq, ksft_true
from lib.py import ip
from lib.py import NetNS, NetNSEnter
from lib.py import RtnlFamily


LINK_NETNSID = 100
LINK_NETNSID2 = 200


def test_event() -> None:
    with NetNS() as ns1, NetNS() as ns2:
        with NetNSEnter(str(ns2)):
            rtnl = RtnlFamily()

        rtnl.ntf_subscribe("rtnlgrp-link")

        ip(f"netns set {ns2} {LINK_NETNSID}", ns=str(ns1))
        ip(f"link add netns {ns1} link-netnsid {LINK_NETNSID} dummy1 type dummy")
        ip(f"link add netns {ns1} dummy2 type dummy", ns=str(ns2))

        ip("link del dummy1", ns=str(ns1))
        ip("link del dummy2", ns=str(ns1))

        time.sleep(1)
        rtnl.check_ntf()
        ksft_true(rtnl.async_msg_queue.empty(),
                  "Received unexpected link notification")


def test_event_all_nsid() -> None:
    """NETLINK_LISTEN_ALL_NSID notifications: local events must not
    carry nsid even with a self-referential mapping.  Remote events
    must carry the correct nsid."""

    with NetNS() as ns1, NetNS() as ns2:
        net1, net2 = str(ns1), str(ns2)

        with NetNSEnter(net1):
            rtnl = RtnlFamily()
        rtnl.ntf_listen_all_nsid()
        rtnl.ntf_subscribe("rtnlgrp-link")

        # Case 1: no nsid assigned, local event, no nsid expected.
        ip("link add dummy-lo type dummy", ns=net1)

        # Case 2: self-referential nsid, local event, still no nsid.
        ip(f"netns set {net1} {LINK_NETNSID}", ns=net1)
        ip("link add dummy-sr type dummy", ns=net1)

        # Case 3: remote event, nsid present.
        ip(f"netns set {net2} {LINK_NETNSID2}", ns=net1)
        ip("link add dummy-re type dummy", ns=net2)

        # Collect the three newlink events, ignoring unrelated noise.
        events = {}
        for msg in rtnl.poll_ntf(duration=1):
            if msg['name'] == 'getlink':
                ifname = msg['msg'].get('ifname')
                if ifname in ('dummy-lo', 'dummy-sr', 'dummy-re'):
                    events[ifname] = msg
            if len(events) == 3:
                break

        ksft_true('dummy-lo' in events, "missing local event")

Annotation

Implementation Notes