tools/testing/selftests/drivers/net/hw/nic_timestamp.py

Source file repositories/reference/linux-study-clean/tools/testing/selftests/drivers/net/hw/nic_timestamp.py

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/drivers/net/hw/nic_timestamp.py
Extension
.py
Size
7126 bytes
Lines
226
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
# pylint: disable=locally-disabled, invalid-name, attribute-defined-outside-init, too-few-public-methods

"""
Tests related to configuration of HW timestamping
"""

import errno
import ctypes
import fcntl
import socket
from lib.py import ksft_run, ksft_exit, ksft_ge, ksft_eq, KsftSkipEx
from lib.py import NetDrvEnv, EthtoolFamily, NlError


SIOCSHWTSTAMP = 0x89b0
SIOCGHWTSTAMP = 0x89b1
class hwtstamp_config(ctypes.Structure):
    """ Python copy of struct hwtstamp_config """
    _fields_ = [
        ("flags", ctypes.c_int),
        ("tx_type", ctypes.c_int),
        ("rx_filter", ctypes.c_int),
    ]


class ifreq(ctypes.Structure):
    """ Python copy of struct ifreq """
    _fields_ = [
        ("ifr_name", ctypes.c_char * 16),
        ("ifr_data", ctypes.POINTER(hwtstamp_config)),
    ]


def __get_hwtimestamp_support(cfg):
    """ Retrieve supported configuration information """

    try:
        tsinfo = cfg.ethnl.tsinfo_get({'header': {'dev-name': cfg.ifname}})
    except NlError as e:
        if e.error == errno.EOPNOTSUPP:
            raise KsftSkipEx("timestamping configuration is not supported") from e
        raise

    ctx = {}
    tx = tsinfo.get('tx-types', {})
    rx = tsinfo.get('rx-filters', {})

    bits = tx.get('bits', {})
    ctx['tx'] = bits.get('bit', [])
    bits = rx.get('bits', {})
    ctx['rx'] = bits.get('bit', [])
    return ctx


def __get_hwtimestamp_config_ioctl(cfg):
    """ Retrieve current TS configuration information (via ioctl) """

    config = hwtstamp_config()

    req = ifreq()
    req.ifr_name = cfg.ifname.encode()
    req.ifr_data = ctypes.pointer(config)

    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        fcntl.ioctl(sock.fileno(), SIOCGHWTSTAMP, req)
        sock.close()

Annotation

Implementation Notes