Skip to content

feature(net): add namespace-aware netdevice kobject and sysfs projection #2254

Description

@fslongjin

Summary

Add namespace-aware kobject and sysfs support for network devices so that every netdevice, including devices owned by non-initial network namespaces, has a real kobject lifecycle and Linux-compatible uevent behavior.

Motivation

DragonOS currently publishes network devices from the initial network namespace through driver-core/kobject and /sys/class/net. Devices created in another network namespace, such as that namespace's loopback device, are registered in the namespace's network-device table but do not receive an equivalent kobject/sysfs projection.

This produces a split model:

  • rtnetlink and network ioctls can find and mutate the non-initial namespace device;
  • the device has no corresponding namespace-aware kobject/sysfs representation;
  • renaming it cannot perform a real driver-core device_rename() operation;
  • a Linux-compatible KOBJ_MOVE event cannot be constructed from a real DEVPATH/DEVPATH_OLD pair;
  • /sys/class/net may continue to expose devices from the namespace to which the sysfs mount is bound, without an explicit DragonOS visibility model.

Fabricating a uevent path for a device that has no kobject would only hide the missing lifecycle and would conflict with a future proper sysfs implementation. This feature should establish the underlying object model instead.

Linux 6.6 semantics

Linux creates a kobject-backed device for every netdevice. Netdevice kobjects carry a network-namespace tag, and sysfs visibility is filtered using that namespace relationship. Netdevice uevents are routed according to the device's owning network namespace rather than the network namespace of the calling task.

Consequently:

  • different network namespaces can each own a device named lo without an identity collision;
  • a rename in a non-initial namespace still goes through the real device/kobject rename lifecycle;
  • KOBJ_MOVE is delivered to listeners in the device-owning namespace;
  • netdevice uevents consistently include INTERFACE and IFINDEX, while move events additionally include a valid DEVPATH_OLD.

Relevant Linux 6.6 areas include net/core/net-sysfs.c, net/core/dev.c, and lib/kobject_uevent.c.

Proposed architecture

  1. Netdevice kobject ownership

    • Give every registered netdevice a real kobject lifecycle, regardless of whether it belongs to the initial network namespace.
    • Associate the kobject with the authoritative NetNamespace that owns the interface.
    • Keep interface ownership in the network namespace as the source of truth; do not duplicate mutable ownership state in an unrelated registry.
  2. Namespace-aware sysfs visibility

    • Introduce a netdevice-specific namespace visibility policy for /sys/class/net and the backing device nodes.
    • Permit identical interface names in different network namespaces without collisions in the internal kernfs identity model.
    • Ensure lookup and directory iteration expose only devices visible from the namespace associated with the sysfs access context/mount.
  3. Namespace-aware uevent delivery

    • Select the target NETLINK_KOBJECT_UEVENT socket table from the netdevice's owner namespace, not current_netns().
    • Build common netdevice fields in one helper so all actions include SUBSYSTEM=net, INTERFACE, and IFINDEX.
    • Emit rename events from real kobject state, with valid DEVPATH and DEVPATH_OLD; do not synthesize paths for unprojected devices.
  4. Transactional rename integration

    • Integrate the kobject/sysfs rename with the existing RTNL-protected link mutation transaction.
    • Prepare all fallible allocations and conflict checks before publishing logical name, kernfs keys, class links, cached symlink targets, or notifications.
    • Preserve inode/kobject identity across rename and keep rollback/teardown behavior correct.

The implementation should remain netdevice-specific where namespace semantics are required. It should not force a namespace abstraction onto every kobject class.

Required behavior

  • A non-initial network namespace's lo has a real kobject-backed identity.
  • Two namespaces can independently contain same-named netdevices.
  • /sys/class/net lookup and iteration follow the namespace visibility model instead of a single global name map.
  • Renaming a non-initial namespace netdevice updates its logical name and its real kobject/sysfs projection atomically with respect to RTNL writers.
  • Rename emits one owner-namespace KOBJ_MOVE event containing correct INTERFACE, IFINDEX, DEVPATH, and DEVPATH_OLD fields.
  • A task that calls setns() but retains an older rtnetlink socket cannot cause the event to be delivered to the task's current namespace; delivery follows the mutated device.
  • Conflict, allocation failure, or stale-object detection leaves the old name, kernfs identity, and notification state unchanged.
  • Device unregister after rename removes the correct objects and leaves no stale kernfs keys or class links.

Validation

  • Add tests with two network namespaces, each containing lo, and verify independent lookup/iteration.
  • Open rtnetlink and kobject-uevent sockets in namespace A, move the task to namespace B, and rename A's device through the retained rtnetlink socket.
  • Verify that only A receives the move event and that all required environment fields are correct.
  • Verify inode/kobject identity before and after rename.
  • Verify rename conflicts are namespace-local.
  • Verify unregister after rename leaves no stale sysfs entries.
  • Exercise concurrent dump/rename/unregister paths under RTNL and confirm there is no deadlock or partially published state.
  • Run make fmt, make kernel, and the relevant dunitest suites in DragonOS/QEMU.

Non-goals

  • Do not introduce Cube-specific APIs or behavior.
  • Do not fake DEVPATH values or emit synthetic move events for objects without a real kobject.
  • Do not redesign namespace handling for unrelated kobject classes unless a separately demonstrated requirement justifies it.
  • Do not introduce a general-purpose transaction framework; reuse the existing RTNL and prepared link-mutation boundaries.

Relationship to #2233

This is a follow-up capability discovered while completing the network control-plane work in #2233. The standard rtnetlink link state can be correct without this feature, but complete Linux-compatible sysfs/kobject and uevent behavior for non-initial network namespaces requires this underlying namespace-aware object model.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions