Skip to contentSkip to conversation

net: add the PON subsystem

RFC In review John Crispin 3 participants 16 messages Oct 8 – Oct 9

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover net: add the PON subsystem

This series adds net/pon, a subsystem for the ONU side of an ITU-T passive optical network. It follows the thread "[RFC] net: towards a generic PON framework" [1], which converged on a wiphy-like PON object that is not a netdev, PLOAM in the MAC driver, service traffic on ordinary netdevs and OMCI in userspace. This is that design as working code.

The model is cfg80211 with a fullmac driver:

- struct pon_dev is the PON object. It is not a netdev. Userspace addresses it by a device id, as nl80211 addresses a wiphy.
- The MAC driver runs PLOAM activation and the key hierarchy. It reports the activation state through pon_dev_state_report(). The core validates the edge against ITU-T G.9807.1 Table C.12.4, publishes it and sets the carrier from it.
- The core owns the T-CONTs, the GEM ports and the upstream classifier rules (the 802.1p mapper of the thread). It stores them and dumps them. The driver offloads them.
- The core lends the driver a serialized context: an ordered workqueue per instance and the instance lock that the netlink handlers take.
- Service traffic uses a data netdev plus GEM netdevs (rtnetlink link kind "gem") where a GEM port needs a bridge port of its own. Frames ride the rings of an ethernet controller, the conduit, with the per frame information passed as arguments.
- OMCI travels over the pon genl family the way nl80211 carries management frames. One socket registers for the OMCI PDUs of a device, receives them as notifications and sends with omci-tx. No OMCI netdev and no new address family.
- Control is one generic netlink family with a YAML spec. The uapi header and the policy are generated.

This posting is the minimal core. The counters, alarms, events, the ETS scheduler offload, a PON mode for the generic PHY framework, the PON PCS object and the flow offload are further commits on top of it that come in later series.

The scope is XGS-PON. The mode enum keeps gpon and xg-pon so that the uapi order stays clean for later drivers. The core refuses a mode that the device does not list.

The first driver is for the Airoha AN7581 PON MAC, with its PON PHY, an optics driver and the conduit in airoha_eth. It is not part of this posting, because it depends on the Airoha PCS series that is not upstream yet. pon-tool [2] consumes the family through the libpon library. It has a verb for every attribute of the family and runs in OpenWrt today. The stack reaches O5, passes traffic and recovers from fiber pulls against a production OLT with HGU, SFU and IP host services at the same time.

[1] https://lore.kernel.org/netdev/[email protected]/
[2] https://github.com/blogic/pon-tool

John Crispin (12):
net: pon: add the netlink specification of the pon family
net: add a PON device pointer to struct net_device
net: pon: add the PLOAM vocabulary and message codec
net: pon: add the device state and the lent context
net: pon: add the conduit contract
net: pon: add the GEM network devices
net: pon: add the OMCI channel
net: pon: add netlink support
net: pon: add the device registration
net: pon: build the PON subsystem
Documentation: networking: describe the PON subsystem
MAINTAINERS: add the PON subsystem

 Documentation/netlink/specs/pon.yaml     |  604 ++++++++
 Documentation/netlink/specs/rt-link.yaml |   14 +
 Documentation/networking/index.rst       |    1 +
 Documentation/networking/pon.rst         |  302 ++++
 MAINTAINERS                              |   11 +
 include/linux/netdevice.h                |    6 +
 include/net/pon.h                        |   14 +
 include/net/pon/functions.h              |  129 ++
 include/net/pon/ploam.h                  |  266 ++++
 include/net/pon/ploam_msg.h              |  306 ++++
 include/net/pon/types.h                  |  443 ++++++
 include/uapi/linux/if_link.h             |   10 +
 include/uapi/linux/pon.h                 |  166 +++
 net/Kconfig                              |    1 +
 net/Makefile                             |    1 +
 net/pon/Kconfig                          |   17 +
 net/pon/Makefile                         |    6 +
 net/pon/pon-nl-gen.c                     |  286 ++++
 net/pon/pon-nl-gen.h                     |   45 +
 net/pon/pon.h                            |  162 +++
 net/pon/pon_conduit.c                    |  614 ++++++++
 net/pon/pon_gem.c                        |  537 +++++++
 net/pon/pon_main.c                       |  454 ++++++
 net/pon/pon_nl.c                         | 1671 ++++++++++++++++++++++
 net/pon/pon_omci.c                       |  326 +++++
 net/pon/pon_ploam_msg.c                  |  312 ++++
 net/pon/pon_state.c                      |  408 ++++++
 net/pon/pon_work.c                       |  106 ++
 tools/net/ynl/Makefile.deps              |    1 +
 29 files changed, 7219 insertions(+)
 create mode 100644 Documentation/netlink/specs/pon.yaml
 create mode 100644 Documentation/networking/pon.rst
 create mode 100644 include/net/pon.h
 create mode 100644 include/net/pon/functions.h
 create mode 100644 include/net/pon/ploam.h
 create mode 100644 include/net/pon/ploam_msg.h
 create mode 100644 include/net/pon/types.h
 create mode 100644 include/uapi/linux/pon.h
 create mode 100644 net/pon/Kconfig
 create mode 100644 net/pon/Makefile
 create mode 100644 net/pon/pon-nl-gen.c
 create mode 100644 net/pon/pon-nl-gen.h
 create mode 100644 net/pon/pon.h
 create mode 100644 net/pon/pon_conduit.c
 create mode 100644 net/pon/pon_gem.c
 create mode 100644 net/pon/pon_main.c
 create mode 100644 net/pon/pon_nl.c
 create mode 100644 net/pon/pon_omci.c
 create mode 100644 net/pon/pon_ploam_msg.c
 create mode 100644 net/pon/pon_state.c
 create mode 100644 net/pon/pon_work.c

base-commit: 8df0638138d3e0344fd1fb36cf2d1ca1cf5028f0

Signature · 1 line
2.34.1
#1 lore raw

RFC 1/12 net: pon: add the netlink specification of the pon family

The pon generic netlink family configures the ONU side of an ITU-T passive optical network. The kernel owns activation (which the MAC driver runs) and the datapath objects. Userspace owns OMCI (the management plane) and expresses what the OLT asks for through this family.

The family carries the identity and the activation state of a device, the T-CONTs, the GEM ports, the upstream classifier rules and the OMCI channel. Notifications are levels, so a listener that loses one reads the state again. A write operation answers with the netlink ACK only. No key material crosses the family.

The mode enum lists gpon, xg-pon and xgs-pon. No driver runs gpon or xg-pon yet. The enum keeps them so that the uapi order stays clean for the drivers that add them later. The core refuses them through the mode capability of the device: dev-set refuses a mode the device does not list, with an extack message and a line in the kernel log.

pon.yaml is the source. The uapi header is generated from it with ynl-gen. The Makefile.deps of tools/net/ynl gets a line for the family, so that the header of the tree wins over an older copy that a distribution ships. The kernel side follows with the subsystem.

Assisted-by LLM
---
 Documentation/netlink/specs/pon.yaml | 604 +++++++++++++++++++++++++++
 include/uapi/linux/pon.h             | 166 ++++++++
 tools/net/ynl/Makefile.deps          |   1 +
 3 files changed, 771 insertions(+)
 create mode 100644 Documentation/netlink/specs/pon.yaml
 create mode 100644 include/uapi/linux/pon.h
Documentation/netlink/specs/pon.yaml new file +604 -0
new file mode 100644
index 000000000000..45780c179613
@@ -0,0 +1,604 @@
+# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
+---
+name: pon
+
+doc:
+  Passive Optical Network device configuration over generic netlink.
+
+  A PON device represents the ONU-side MAC of a GPON or XG(S)-PON link. The
+  kernel owns activation (the PLOAM state machine, ranging and the key
+  hierarchy run in hardware or in the driver, driven by MAC interrupts) and
+  the datapath objects (T-CONTs and GEM ports). Userspace owns the management
+  plane, OMCI, whose PDUs this family carries between the OMCC and the one
+  socket that registered for them. This family also carries identity,
+  datapath object configuration, state notifications and counters.
+
+  An operation is a set when it can change an object that already exists
+  and a new when it can only create one. A tcont-set rebinds a T-CONT, while
+  a gem-new refuses a GEM port that exists with other attributes.
+
+definitions:
+  -
+    type: enum
+    name: mode
+    doc: PON operating mode.
+    entries: [gpon, xg-pon, xgs-pon]
+  -
+    type: enum
+    name: ploam-state
+    doc: |
+      ITU-T G.9807.1 / G.984.3 ONU activation state. O5 is the only state in
+      which the OMCC carries traffic. G.9807.1 has one Serial Number state,
+      O2-3 (Table C.12.1), reported as o2. Only a G.984.3 device reports o3.
+    entries: [unknown, o1, o2, o3, o4, o5, o6, o7]
+  -
+    type: enum
+    name: gem-dir
+    doc: |
+      GEM port direction. The values are those of the direction attribute of
+      the GEM port network CTP managed entity of ITU-T G.988: UNI-to-ANI (1),
+      ANI-to-UNI (2) or bidirectional (3). An OMCI stack passes the value
+      through.
+    entries:
+      -
+        name: upstream
+        value: 1
+      -
+        name: downstream
+        value: 2
+      -
+        name: bidir
+        value: 3
+  -
+    type: enum
+    name: gem-key-ring
+    doc: |
+      Whether a GEM port is encrypted and with which keys, as the
+      encryption key ring attribute of the GEM port network CTP managed
+      entity of ITU-T G.988 clause 9.2.3 defines it.
+    entries:
+      -
+        name: none
+        doc: No encryption. Upstream is sent with key index 0.
+      -
+        name: unicast
+        doc: |
+          Unicast encryption in both directions, with the keys the ONU
+          generates.
+      -
+        name: broadcast
+        doc: |
+          Broadcast encryption, with the keys the OLT distributes over the
+          OMCI.
+      -
+        name: unicast-downstream
+        doc: |
+          Unicast encryption of the downstream only, with the keys the ONU
+          generates.
+  -
+    type: enum
+    name: gem-map-tag
+    doc: VLAN tag state an upstream classifier rule matches.
+    entries: [untagged, tagged]
+
+attribute-sets:
+  -
+    name: dev
+    attributes:
+      -
+        name: id
+        doc: PON device ID.
+        type: u32
+        checks:
+          min: 1
+      -
+        name: ifindex
+        doc: ifindex of the PON data network device (the PON MAC port).
+        type: u32
+      -
+        name: mode
+        doc: Active PON mode.
+        type: u32
+        enum: mode
+      -
+        name: modes-cap
+        doc: Bitmask of the modes the device supports.
+        type: u32
+        enum: mode
+        enum-as-flags: true
+      -
+        name: serial
+        doc: |
+          ONU serial number, 8 bytes: the 4 ASCII characters of the Vendor_ID
+          of ITU-T G.9807.1 clause C.11.2.6.1, then the 4 byte VSSN of clause
+          C.11.2.6.2. Eight 0x00 bytes address every ONU (Table C.11.23A) and
+          are refused.
+        type: binary
+        checks:
+          exact-len: 8
+      -
+        name: registration-id
+        doc: |
+          Registration_ID of the Registration message of ITU-T G.9807.1
+          (Table C.11.25), 1 to 36 bytes. An empty value is refused. The
+          kernel pads a shorter value with 0x00 bytes to 36, as the note of
+          the table recommends. Write only: it is a credential.
+        type: binary
+        checks:
+          max-len: 36
+      -
+        name: ploam-state
+        doc: Current activation state.
+        type: u32
+        enum: ploam-state
+      -
+        name: max-tconts
+        doc: Number of T-CONTs the device supports.
+        type: u32
+      -
+        name: max-gems
+        doc: |
+          Number of GEM ports gem-new can create, not counting the GEM port
+          of the OMCC. gem-new answers -ENOSPC beyond it.
+        type: u32
+      -
+        name: enable
+        doc: |
+          1 starts the ONU upstream link, 0 stops it. In a reply, whether the
+          link was last started.
+        type: u8
+        checks:
+          max: 1
+  -
+    name: tcont
+    attributes:
+      -
+        name: dev-id
+        doc: PON device ID.
+        type: u32
+        checks:
+          min: 1
+      -
+        name: index
+        doc: |
+          T-CONT index in the device, 0 based and below the max-tconts of
+          the device.
+        type: u32
+        checks:
+          max: 65534
+      -
+        name: alloc-id
+        doc: |
+          Alloc-ID bound to the T-CONT, as assigned by the OLT. ITU-T
+          G.9807.1 clause C.6.1.5.7 bounds it at 16383.
+        type: u32
+        checks:
+          max: 16383
+  -
+    name: gem
+    attributes:
+      -
+        name: dev-id
+        doc: PON device ID.
+        type: u32
+        checks:
+          min: 1
+      -
+        name: id
+        doc: |
+          GEM port ID, as the OLT assigns it over the OMCI. ITU-T G.9807.1
+          Table C.6.6 reserves 0 to 1020 for the default Port-ID (equal
+          to the ONU-ID, OMCC only) and 65535 for the idle Port-ID, so an
+          assigned GEM port is 1021 to 65534.
+        type: u32
+        checks:
+          min: 1021
+          max: 65534
+      -
+        name: dir
+        doc: Direction.
+        type: u32
+        enum: gem-dir
+      -
+        name: tcont-index
+        doc: |
+          T-CONT the upstream half rides. Required for an upstream or
+          bidirectional GEM port. A downstream GEM port rides no T-CONT and
+          is refused with this attribute.
+        type: u32
+        checks:
+          max: 65534
+      -
+        name: key-ring
+        doc: |
+          Encryption key ring of the GEM port. Absent means none. The
+          kernel refuses broadcast, since it takes no broadcast key.
+        type: u32
+        enum: gem-key-ring
+  -
+    name: gem-map
+    attributes:
+      -
+        name: dev-id
+        doc: PON device ID.
+        type: u32
+        checks:
+          min: 1
+      -
+        name: gem-id
+        doc: GEM port the matching frames map to, 1021 to 65534 as for gem id.
+        type: u32
+        checks:
+          min: 1021
+          max: 65534
+      -
+        name: tag
+        doc: Match the frame VLAN tag state. Absent matches either.
+        type: u32
+        enum: gem-map-tag
+      -
+        name: vid
+        doc: Match the outer VLAN ID. Absent matches any.
+        type: u32
+        checks:
+          max: 4094
+      -
+        name: pbit
+        doc: Match the outer VLAN priority. Absent matches any.
+        type: u32
+        checks:
+          max: 7
+      -
+        name: dscp
+        doc: Match the IP DSCP field. Absent matches any.
+        type: u32
+        checks:
+          max: 63
+  -
+    name: omci
+    attributes:
+      -
+        name: dev-id
+        doc: PON device ID.
+        type: u32
+        checks:
+          min: 1
+      -
+        name: pdu
+        doc: |
+          One OMCI PDU, from the transaction correlation id to the end of the
+          message contents: 44 bytes for the baseline format, the 10 byte
+          header and the contents for the extended format of ITU-T G.988
+          annex B. It carries no integrity field in either direction. The
+          MAC adds the MIC upstream and checks it downstream. Upstream the
+          device identifier selects the format and the length must match
+          it: 44 bytes for 0x0a, 10 bytes plus the message contents length
+          of the header for 0x0b. Any other PDU is refused.
+        type: binary
+        checks:
+          max-len: 1976
+
+operations:
+  list:
+    -
+      name: dev-get
+      doc: Get or dump PON devices.
+      attribute-set: dev
+      do:
+        request:
+          attributes:
+            - id
+        reply: &dev-all
+          attributes:
+            - id
+            - ifindex
+            - mode
+            - modes-cap
+            - serial
+            - ploam-state
+            - max-tconts
+            - max-gems
+            - enable
+        pre: pon-device-get-locked
+        post: pon-device-unlock
+      dump:
+        reply: *dev-all
+    -
+      name: dev-set
+      doc: |
+        Set the identity of a PON device, start or stop its upstream link, or
+        any combination. While the link is enabled, a mode or a serial number
+        other than the one the device holds is refused with EBUSY.
+        The settings are applied before the link moves.
+      attribute-set: dev
+      flags: [admin-perm]
+      do:
+        request:
+          attributes:
+            - id
+            - mode
+            - serial
+            - registration-id
+            - enable
+        pre: pon-device-get-locked
+        post: pon-device-unlock
+    -
+      name: dev-add-ntf
+      doc: Notification about a PON device appearing.
+      notify: dev-get
+      mcgrp: mgmt
+    -
+      name: dev-del-ntf
+      doc: |
+        Notification about a PON device disappearing. Every T-CONT, GEM port
+        and classifier rule of the device goes with it, without a
+        notification of its own.
+      notify: dev-get
+      mcgrp: mgmt
+    -
+      name: dev-change-ntf
+      doc: Notification about a PON device configuration change.
+      notify: dev-get
+      mcgrp: mgmt
+    -
+      name: ploam-ntf
+      doc: |
+        The activation state changed. Every transition is reported, so the
+        O5 edge, which is the OMCC becoming usable or unusable, is visible.
+      attribute-set: dev
+      mcgrp: state
+      event:
+        attributes:
+          - id
+          - ploam-state
+
+    -
+      name: tcont-get
+      doc: |
+        Get or dump T-CONTs. A dump with a device ID lists the T-CONTs of
+        that device only.
+      attribute-set: tcont
+      do:
+        request:
+          attributes:
+            - dev-id
+            - index
+        reply: &tcont-all
+          attributes:
+            - dev-id
+            - index
+            - alloc-id
+        pre: pon-device-get-locked
+        post: pon-device-unlock
+      dump:
+        request:
+          attributes:
+            - dev-id
+        reply: *tcont-all
+    -
+      name: tcont-set
+      doc: |
+        Bind an alloc-id to a T-CONT, or rebind it. A rebind moves the GEM
+        ports of the T-CONT too. If the driver refuses one, the old alloc-id
+        stays on the T-CONT and on its GEM ports and the request fails.
+      attribute-set: tcont
+      flags: [admin-perm]
+      do:
+        request:
+          attributes:
+            - dev-id
+            - index
+            - alloc-id
+        pre: pon-device-get-locked
+        post: pon-device-unlock
+    -
+      name: tcont-del
+      doc: |
+        Release a T-CONT. Fails with -EBUSY while a GEM port names the
+        T-CONT. Delete the GEM ports first.
+      attribute-set: tcont
+      flags: [admin-perm]
+      do:
+        request:
+          attributes:
+            - dev-id
+            - index
+        pre: pon-device-get-locked
+        post: pon-device-unlock
+    -
+      name: tcont-add-ntf
+      doc: A T-CONT object was created.
+      notify: tcont-get
+      mcgrp: mgmt
+    -
+      name: tcont-change-ntf
+      doc: A T-CONT object changed.
+      notify: tcont-get
+      mcgrp: mgmt
+    -
+      name: tcont-del-ntf
+      doc: A T-CONT object was removed.
+      notify: tcont-get
+      mcgrp: mgmt
+
+    -
+      name: gem-get
+      doc: |
+        Get or dump GEM ports. A dump with a device ID lists the GEM ports
+        of that device only.
+      attribute-set: gem
+      do:
+        request:
+          attributes:
+            - dev-id
+            - id
+        reply: &gem-all
+          attributes:
+            - dev-id
+            - id
+            - dir
+            - tcont-index
+            - key-ring
+        pre: pon-device-get-locked
+        post: pon-device-unlock
+      dump:
+        request:
+          attributes:
+            - dev-id
+        reply: *gem-all
+    -
+      name: gem-new
+      doc: |
+        Create a GEM port. A GEM port that exists with the same attributes
+        is accepted. One that exists with other attributes is refused.
+      attribute-set: gem
+      flags: [admin-perm]
+      do:
+        request:
+          attributes:
+            - dev-id
+            - id
+            - dir
+            - tcont-index
+            - key-ring
+        pre: pon-device-get-locked
+        post: pon-device-unlock
+    -
+      name: gem-del
+      doc: Destroy a GEM port.
+      attribute-set: gem
+      flags: [admin-perm]
+      do:
+        request:
+          attributes:
+            - dev-id
+            - id
+        pre: pon-device-get-locked
+        post: pon-device-unlock
+    -
+      name: gem-add-ntf
+      doc: A GEM port object was created.
+      notify: gem-get
+      mcgrp: mgmt
+    -
+      name: gem-del-ntf
+      doc: A GEM port object was removed.
+      notify: gem-get
+      mcgrp: mgmt
+
+    -
+      name: gem-map-get
+      doc: |
+        Dump the upstream classifier rules. The core keeps a copy of each
+        rule that the driver took. A dump with a device ID lists the rules of
+        that device only.
+      attribute-set: gem-map
+      dump:
+        request:
+          attributes:
+            - dev-id
+        reply:
+          attributes:
+            - dev-id
+            - gem-id
+            - tag
+            - vid
+            - pbit
+            - dscp
+    -
+      name: gem-map-new
+      doc: |
+        Add one upstream classifier rule mapping frames onto a GEM port.
+        A rule matches on the attributes it carries. A more specific rule
+        wins over a less specific one.
+      attribute-set: gem-map
+      flags: [admin-perm]
+      do:
+        request:
+          attributes:
+            - dev-id
+            - gem-id
+            - tag
+            - vid
+            - pbit
+            - dscp
+        pre: pon-device-get-locked
+        post: pon-device-unlock
+    -
+      name: gem-map-del
+      doc: Remove one upstream classifier rule.
+      attribute-set: gem-map
+      flags: [admin-perm]
+      do:
+        request:
+          attributes:
+            - dev-id
+            - gem-id
+            - tag
+            - vid
+            - pbit
+            - dscp
+        pre: pon-device-get-locked
+        post: pon-device-unlock
+    -
+      name: gem-map-add-ntf
+      doc: An upstream classifier rule was created.
+      notify: gem-map-get
+      mcgrp: mgmt
+    -
+      name: gem-map-del-ntf
+      doc: An upstream classifier rule was removed.
+      notify: gem-map-get
+      mcgrp: mgmt
+
+
+    -
+      name: omci-register
+      doc: |
+        Receive the OMCI PDUs of one device on this socket, as omci-ntf.
+        One socket at a time owns the OMCI channel of a device. Another
+        socket is refused with EBUSY until the owner closes. A registration
+        ends only when its socket closes.
+      attribute-set: omci
+      flags: [admin-perm]
+      do:
+        request:
+          attributes:
+            - dev-id
+        pre: pon-device-get-locked
+        post: pon-device-unlock
+    -
+      name: omci-tx
+      doc: |
+        Send one OMCI PDU to the OLT. Only the socket that owns the OMCI
+        channel of the device may send and the MAC refuses a PDU while the
+        ONU is not in O5.
+      attribute-set: omci
+      flags: [admin-perm]
+      do:
+        request:
+          attributes:
+            - dev-id
+            - pdu
+        pre: pon-device-get-locked
+        post: pon-device-unlock
+    -
+      name: omci-ntf
+      doc: |
+        One OMCI PDU from the OLT, sent only to the socket that owns the
+        OMCI channel of the device. A PDU that finds no room on that socket
+        is lost. The OLT retries.
+      attribute-set: omci
+      event:
+        attributes:
+          - dev-id
+          - pdu
+
+mcast-groups:
+  list:
+    -
+      name: mgmt
+    -
+      name: state
+
+...
include/uapi/linux/pon.h new file +166 -0

166 lines not on this page · raw message

tools/net/ynl/Makefile.deps +1 -0
index 1e746e25e2bc..fc84485b2c8b 100644
@@ -38,6 +38,7 @@ CFLAGS_ovs_datapath:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
 CFLAGS_ovs_flow:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
 CFLAGS_ovs_packet:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
 CFLAGS_ovs_vport:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
+CFLAGS_pon:=$(call get_hdr_inc,_LINUX_PON_H,pon.h)
 CFLAGS_psp:=$(call get_hdr_inc,_LINUX_PSP_H,psp.h)
 CFLAGS_rt-addr:=$(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \
 	$(call get_hdr_inc,__LINUX_IF_ADDR_H,if_addr.h)
Signature · 1 line
2.34.1
#2 lore raw

RFC 2/12 net: add a PON device pointer to struct net_device

In reply to John Crispin

A PON device attaches to three kinds of network device: the ethernet device whose rings carry its frames (the conduit), its data network device and the GEM network devices that stack on the data network device. Give struct net_device the pointer, beside the other subsystem pointers, as an RCU pointer under CONFIG_PON.

Nothing sets the pointer yet. The PON core and its accessor, netdev_uses_pon(), follow.

Assisted-by LLM
---
 include/linux/netdevice.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/netdevice.h +6 -0
index 8cae9b00211e..1b4ed6ae7e87 100644
@@ -72,6 +72,7 @@ struct wireless_dev;
 /* 802.15.4 specific */
 struct wpan_dev;
 struct mpls_dev;
+struct pon_dev;
 /* UDP Tunnel offloads */
 struct udp_tunnel_info;
 struct udp_tunnel_nic_info;
@@ -1974,6 +1975,8 @@ enum netdev_reg_state {
  *	@mpls_ptr:	mpls_dev struct pointer
  *	@mctp_ptr:	MCTP specific data
  *	@psp_dev:	PSP crypto device registered for this netdev
+ *	@pon_dev:	PON device that uses this netdev as conduit, data
+ *			interface or GEM network device
  *
  *	@dev_addr:	Hw address (before bcast,
  *			because most packets are unicast)
@@ -2390,6 +2393,9 @@ struct net_device {
 #if IS_ENABLED(CONFIG_INET_PSP)
 	struct psp_dev __rcu	*psp_dev;
 #endif
+#if IS_ENABLED(CONFIG_PON)
+	struct pon_dev __rcu	*pon_dev;
+#endif
 
 /*
  * Cache lines mostly used on receive path (including eth_type_trans())
Signature · 1 line
2.34.1
#3 lore raw

RFC 3/12 net: pon: add the PLOAM vocabulary and message codec

In reply to John Crispin

PLOAM is the activation protocol of ITU-T G.9807.1. ploam.h holds the values that the Recommendation defines rather than what one MAC encodes:
the message identifiers, the completion codes, the key indexes of Table C.11.12, the Alloc-ID and XGEM Port-ID ranges of Tables C.6.5 and C.6.6, the ONU-ID range of Table C.6.4, the field lengths and the timers. Every value is a G.9807.1 value. Every PON driver needs them, so they belong to the subsystem.

Four values of the key exchange that every XG(S)-PON MAC needs are in ploam.h as well:

- PON_PLOAM_KEY_NAME_CONSTANT, the ASCII form of the constant that the Key_Name hashes after the key (Table C.11.26), with its length
- PON_PLOAM_KEY_FRAGMENT_FIRST, the fragment number of a single fragment Key_Report (Table C.11.26)
- PON_PLOAM_DEFAULT_PLOAM_IK_BYTE, the byte of the default PLOAM_IK (clause C.15.3.3)
- PON_PLOAM_DEFAULT_MSK, the MSK of the well-known default Registration_ID (formula C.15-2), as an initializer. A MAC that derives an MSK only from the Registration_ID programmed into it needs the value as a constant.

The core does not use them. A PON MAC driver does.

The codec parses the downstream messages that the ONU handles into structures and builds the upstream ones. It holds no state, no timer and no policy. The builder refuses an ONU-ID above ten bits. It sends Serial_Number_ONU with ONU-ID 0x3FF and sequence number 0, as Table C.11.24 fixes both fields. The control member of the parsed Key_Control carries the Control flag of Table C.11.12, generate or confirm.

The core itself never calls the codec. The PLOAM state machine runs in the MAC driver, because its timing and its registers are the MAC's. The codec is in the subsystem so that every PON MAC driver parses and builds the messages with one implementation instead of its own. The core uses a few of the ploam.h values itself: the netlink handlers validate the Alloc-ID and XGEM Port-ID ranges with them. The other values serve the drivers.

The core does not call two helpers either. A PON MAC driver needs pon_ploam_alloc_is_assignable() to refuse an Assign_Alloc-ID outside the assignable range of Table C.6.5. pon_ploam_profile_is_broadcast() tells a driver that a Burst_Profile addresses every ONU of its upstream rate:
Table C.11.4 uses ONU-ID 0x3FE for the 9.95328 Gbit/s ONUs beside the broadcast ONU-ID 0x3FF.

Nothing builds it yet. The build is wired at the end of the series.

Assisted-by LLM
---
 include/net/pon/ploam.h     | 266 ++++++++++++++++++++++++++++++
 include/net/pon/ploam_msg.h | 306 +++++++++++++++++++++++++++++++++++
 net/pon/pon_ploam_msg.c     | 312 ++++++++++++++++++++++++++++++++++++
 3 files changed, 884 insertions(+)
 create mode 100644 include/net/pon/ploam.h
 create mode 100644 include/net/pon/ploam_msg.h
 create mode 100644 net/pon/pon_ploam_msg.c
include/net/pon/ploam.h new file +266 -0
new file mode 100644
index 000000000000..2fc27be3f1cc
@@ -0,0 +1,266 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (C) 2026 John Crispin <[email protected]> */
+
+#ifndef __NET_PON_PLOAM_H
+#define __NET_PON_PLOAM_H
+
+#include <linux/bits.h>
+#include <linux/types.h>
+
+/**
+ * DOC: The PLOAM vocabulary
+ *
+ * What ITU-T G.9807.1 defines, rather than what any one MAC's registers
+ * encode. Every PON driver needs these, so they belong to the subsystem. The
+ * header holds no value of another ITU-T PON system, although the mode
+ * enumeration of the uapi names them.
+ *
+ * It holds the message identifiers, the completion codes, the Alloc-ID and
+ * XGEM Port-ID ranges of Tables C.6.5 and C.6.6, the ONU-ID range of
+ * Table C.6.4, the key indexes of Table C.11.12, the field lengths and the
+ * timers.
+ *
+ * This header is kernel internal. Only the handful of enumerations userspace
+ * reads are uapi, in <uapi/linux/pon.h>.
+ *
+ * A value that is a vendor's recovery policy or a register layout stays in
+ * that vendor's driver even when it sits next to a PLOAM field.
+ *
+ */
+
+/**
+ * enum pon_ploam_down_id - downstream PLOAM message identifiers
+ * @PON_PLOAM_DOWN_BURST_PROFILE: Burst_Profile
+ * @PON_PLOAM_DOWN_ASSIGN_ONU_ID: Assign_ONU-ID
+ * @PON_PLOAM_DOWN_RANGING_TIME: Ranging_Time
+ * @PON_PLOAM_DOWN_DEACTIVATE: Deactivate_ONU-ID
+ * @PON_PLOAM_DOWN_DISABLE_SN: Disable_Serial_Number
+ * @PON_PLOAM_DOWN_REQUEST_REG: Request_Registration
+ * @PON_PLOAM_DOWN_ASSIGN_ALLOC_ID: Assign_Alloc-ID
+ * @PON_PLOAM_DOWN_KEY_CONTROL: Key_Control
+ * @PON_PLOAM_DOWN_SLEEP_ALLOW: Sleep_Allow
+ * @PON_PLOAM_DOWN_REBOOT_ONU: Reboot_ONU
+ * @PON_PLOAM_DOWN_MAX: one above the highest identifier listed here, the
+ *	size of an array indexed by the identifier, not a wire value
+ *
+ * ITU-T G.9807.1 Table C.11.2.
+ */
+enum pon_ploam_down_id {
+	PON_PLOAM_DOWN_BURST_PROFILE	= 0x01,
+	PON_PLOAM_DOWN_ASSIGN_ONU_ID	= 0x03,
+	PON_PLOAM_DOWN_RANGING_TIME	= 0x04,
+	PON_PLOAM_DOWN_DEACTIVATE	= 0x05,
+	PON_PLOAM_DOWN_DISABLE_SN	= 0x06,
+	PON_PLOAM_DOWN_REQUEST_REG	= 0x09,
+	PON_PLOAM_DOWN_ASSIGN_ALLOC_ID	= 0x0a,
+	PON_PLOAM_DOWN_KEY_CONTROL	= 0x0d,
+	PON_PLOAM_DOWN_SLEEP_ALLOW	= 0x12,
+	PON_PLOAM_DOWN_REBOOT_ONU	= 0x1d,
+	PON_PLOAM_DOWN_MAX		= 0x1e,
+};
+
+/**
+ * enum pon_ploam_up_id - upstream PLOAM message identifiers
+ * @PON_PLOAM_UP_SERIAL_NUMBER: Serial_Number_ONU
+ * @PON_PLOAM_UP_REGISTRATION: Registration
+ * @PON_PLOAM_UP_KEY_REPORT: Key_Report
+ * @PON_PLOAM_UP_ACKNOWLEDGE: Acknowledgment
+ * @PON_PLOAM_UP_SLEEP_REQUEST: Sleep_Request
+ * @PON_PLOAM_UP_MAX: one above the highest identifier listed here, the size
+ *	of an array indexed by the identifier, not a wire value
+ *
+ * ITU-T G.9807.1 Table C.11.3.
+ */
+enum pon_ploam_up_id {
+	PON_PLOAM_UP_SERIAL_NUMBER	= 0x01,
+	PON_PLOAM_UP_REGISTRATION	= 0x02,
+	PON_PLOAM_UP_KEY_REPORT		= 0x05,
+	PON_PLOAM_UP_ACKNOWLEDGE	= 0x09,
+	PON_PLOAM_UP_SLEEP_REQUEST	= 0x10,
+	PON_PLOAM_UP_MAX		= 0x11,
+};
+
+/**
+ * enum pon_ploam_ack - completion codes of the upstream Acknowledgment message
+ * @PON_PLOAM_ACK_OK: OK
+ * @PON_PLOAM_ACK_NO_MESSAGE: no message to send
+ * @PON_PLOAM_ACK_BUSY: busy, preparing a response
+ * @PON_PLOAM_ACK_UNKNOWN_TYPE: unknown message type
+ * @PON_PLOAM_ACK_PARAM_ERR: parameter error
+ * @PON_PLOAM_ACK_PROCESS_ERR: processing error
+ *
+ * ITU-T G.9807.1 Table C.11.27, the Completion_code field.
+ */
+enum pon_ploam_ack {
+	PON_PLOAM_ACK_OK		= 0,
+	PON_PLOAM_ACK_NO_MESSAGE	= 1,
+	PON_PLOAM_ACK_BUSY		= 2,
+	PON_PLOAM_ACK_UNKNOWN_TYPE	= 3,
+	PON_PLOAM_ACK_PARAM_ERR		= 4,
+	PON_PLOAM_ACK_PROCESS_ERR	= 5,
+};
+
+/**
+ * enum pon_ploam_disable_mode - the Mode field of Disable_Serial_Number
+ * @PON_PLOAM_DISABLE_ALLOW_ONE: the ONU with this serial number is allowed
+ *	upstream access
+ * @PON_PLOAM_DISABLE_DENY_ALL: all ONUs are denied upstream access. The
+ *	serial number is ignored
+ * @PON_PLOAM_DISABLE_ALLOW_ALL: all ONUs are allowed upstream access
+ * @PON_PLOAM_DISABLE_DENY_ONE: the ONU with this serial number is denied
+ *	upstream access
+ *
+ * ITU-T G.9807.1 Table C.11.9, the Disable/enable field.
+ */
+enum pon_ploam_disable_mode {
+	PON_PLOAM_DISABLE_ALLOW_ONE	= 0x00,
+	PON_PLOAM_DISABLE_DENY_ALL	= 0x0f,
+	PON_PLOAM_DISABLE_ALLOW_ALL	= 0xf0,
+	PON_PLOAM_DISABLE_DENY_ONE	= 0xff,
+};
+
+/* The equalization delay encoding of Ranging_Time, G.9807.1 Table C.11.7. */
+#define PON_PLOAM_EQD_RELATIVE		0
+#define PON_PLOAM_EQD_ABSOLUTE		1
+#define PON_PLOAM_EQD_POSITIVE		0
+
+/* The Alloc-ID type field of Assign_Alloc-ID, G.9807.1 Table C.11.11. */
+#define PON_PLOAM_ALLOC_ASSIGN		0x01
+#define PON_PLOAM_ALLOC_DEALLOCATE	0xff
+
+/* Alloc-ID values, G.9807.1 Table C.6.5. The default alloc-id equals the
+ * ONU-ID, the three above the default range are serial number grants that
+ * are never assigned to an ONU and the rest are assignable. 1022 is the
+ * serial number grant for the 9.95328 Gbit/s upstream rate, 1023 the one
+ * for 2.48832 Gbit/s.
+ */
+#define PON_PLOAM_ALLOC_ID_DEFAULT_MAX	1020
+#define PON_PLOAM_ALLOC_ID_SN_GRANT_MIN	1021
+#define PON_PLOAM_ALLOC_ID_SN_GRANT_10G	1022
+#define PON_PLOAM_ALLOC_ID_SN_GRANT_2G5	1023
+#define PON_PLOAM_ALLOC_ID_MAX		16383
+
+/* XGEM Port-ID values, G.9807.1 Table C.6.6. 0 to 1020 is the default
+ * Port-ID, which equals the ONU-ID and carries only the OMCC. The OLT
+ * assigns every other GEM port of the ONU over the OMCC from 1021 to 65534
+ * and 65535 is the idle Port-ID.
+ */
+#define PON_GEM_PORT_ID_ASSIGNABLE_MIN	1021
+#define PON_GEM_PORT_ID_ASSIGNABLE_MAX	65534
+
+/* The Reboot_ONU fields, G.9807.1 Table C.11.23A. */
+#define PON_PLOAM_REBOOT_DEPTH_MAX		3
+#define PON_PLOAM_REBOOT_IMAGE_MAX		1
+#define PON_PLOAM_REBOOT_STATE_INACTIVE_ONLY	1
+#define PON_PLOAM_REBOOT_CALLS_MASK		0x3
+
+/* The Key_Length a Key_Control carries for the AES-128 cipher,
+ * G.9807.1 Table C.11.12.
+ */
+#define PON_PLOAM_KEY_LEN_AES128	16
+
+/* The Key index of Key_Control and Key_Report, the two low bits of the
+ * field. Values 00 and 11 are not defined. G.9807.1 Table C.11.12 and
+ * Table C.11.26.
+ */
+#define PON_PLOAM_KEY_INDEX_FIRST	1
+#define PON_PLOAM_KEY_INDEX_SECOND	2
+
+/* The Control flag of Key_Control (G.9807.1 Table C.11.12) and the Report
+ * type of Key_Report (Table C.11.26).
+ */
+#define PON_PLOAM_KEY_CONTROL_GENERATE		0
+#define PON_PLOAM_KEY_CONTROL_CONFIRM		1
+#define PON_PLOAM_KEY_REPORT_TYPE_NEW		0
+#define PON_PLOAM_KEY_REPORT_TYPE_EXISTING	1
+
+/* The Key_Name of a Key_Report on an existing key is AES-CMAC(KEK,
+ * encryption_key | 0x33313431353932363533353839373933, 128), G.9807.1
+ * Table C.11.26. The constant is the ASCII string below, without its NUL.
+ * The key fragment number of a single fragment report is 0.
+ */
+#define PON_PLOAM_KEY_NAME_CONSTANT		"3141592653589793"
+#define PON_PLOAM_KEY_NAME_CONSTANT_LEN		16
+#define PON_PLOAM_KEY_FRAGMENT_FIRST		0
+
+/* The default PLOAM_IK is this byte repeated sixteen times, G.9807.1
+ * clause C.15.3.3. The MSK of the well-known default Registration_ID
+ * (thirty-six zero bytes, Table C.11.25) is formula C.15-2 under that key,
+ * clause C.15.3.2. PON_PLOAM_DEFAULT_MSK holds it as an initializer. A MAC
+ * that derives an MSK only from the Registration_ID programmed into it
+ * needs the value as a constant.
+ */
+#define PON_PLOAM_DEFAULT_PLOAM_IK_BYTE		0x55
+#define PON_PLOAM_DEFAULT_MSK	{			\
+	0x24, 0x37, 0xbe, 0x54, 0xe9, 0x5e, 0x6e, 0xe3,	\
+	0x53, 0x8b, 0xb1, 0xb4, 0xb5, 0xd4, 0x32, 0xeb,	\
+}
+
+/* The delimiter and preamble lengths of the burst profile, in octets,
+ * G.9807.1 Table C.11.4.
+ */
+#define PON_PLOAM_BURST_DELIMITER_LEN_MAX	8
+#define PON_PLOAM_BURST_PREAMBLE_LEN_MIN	1
+#define PON_PLOAM_BURST_PREAMBLE_LEN_MAX	8
+
+/* The upstream line rate bit: R of the burst profile (G.9807.1
+ * Table C.11.4) and U of Assign_ONU-ID (Table C.11.6). Then the widths of the
+ * preamble repeat count of the burst profile, Table C.11.4.
+ */
+#define PON_PLOAM_LINE_RATE_XGPON	0
+#define PON_PLOAM_LINE_RATE_XGSPON	1
+#define PON_PLOAM_PREAMBLE_MASK_XGPON	0x1f
+#define PON_PLOAM_PREAMBLE_MASK_XGSPON	0xff
+
+/* The upstream line rate capability of Serial_Number_ONU, a bitmap of the
+ * form 0000 00HL, G.9807.1 Table C.11.24. H set: the ONU supports the
+ * 9.95328 Gbit/s upstream rate. L set: the ONU does not support the
+ * 2.48832 Gbit/s upstream rate.
+ */
+#define PON_PLOAM_SN_RATE_10G		BIT(1)
+#define PON_PLOAM_SN_RATE_NO_2G5	BIT(0)
+
+/* The ONU-ID is ten bits, G.9807.1 clause C.11.2.1. The OLT assigns 0 to
+ * 1020, Table C.6.4. 0x3ff is both the broadcast destination and the value
+ * an ONU carries before the OLT assigns it one. 0x3fe appears only in a
+ * Burst_Profile, as the broadcast destination of a profile for the
+ * 9.95328 Gbit/s upstream rate.
+ */
+#define PON_PLOAM_ONU_ID_MASK			GENMASK(9, 0)
+#define PON_PLOAM_ONU_ID_MAX			1020
+#define PON_PLOAM_ONU_ID_BROADCAST		0x3ff
+#define PON_PLOAM_ONU_ID_UNASSIGNED		0x3ff
+#define PON_PLOAM_ONU_ID_PROFILE_BCAST_10G	0x3fe
+
+/* Fields of the standard message, in bytes. The serial number is the
+ * Vendor_ID and the VSSN, G.9807.1 clauses C.11.2.6.1 and C.11.2.6.2. The
+ * Registration_ID is Table C.11.25 and the key fragment Table C.11.26.
+ */
+#define PON_PLOAM_SN_LEN		8
+#define PON_PLOAM_REG_ID_LEN		36
+#define PON_PLOAM_KEY_FRAGMENT_LEN	32
+
+/* The recommended initial value of TO1, in milliseconds, Table C.12.2. */
+#define PON_PLOAM_TO1_MS		10000
+
+/* The key exchange timers, in milliseconds, G.9807.1 clause C.15.5.3.3. */
+#define PON_PLOAM_TK4_MS		100
+#define PON_PLOAM_TK5_MS		20
+
+/**
+ * pon_ploam_alloc_is_assignable() - whether the OLT can assign an Alloc-ID
+ * @alloc_id: the fourteen bit Alloc-ID
+ *
+ * ITU-T G.9807.1 Table C.6.5. The core does not call it. A PON MAC driver
+ * needs it to refuse an Assign_Alloc-ID outside that range.
+ *
+ * Return: true for the values above PON_PLOAM_ALLOC_ID_SN_GRANT_2G5 up to
+ * PON_PLOAM_ALLOC_ID_MAX, 1024 to 16383.
+ */
+static inline bool pon_ploam_alloc_is_assignable(u16 alloc_id)
+{
+	return alloc_id > PON_PLOAM_ALLOC_ID_SN_GRANT_2G5 &&
+	       alloc_id <= PON_PLOAM_ALLOC_ID_MAX;
+}
+
+#endif /* __NET_PON_PLOAM_H */
include/net/pon/ploam_msg.h new file +306 -0

306 lines not on this page · raw message

net/pon/pon_ploam_msg.c new file +312 -0

312 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

RFC 4/12 net: pon: add the device state and the lent context

In reply to John Crispin

The core lends a MAC driver its serialized context: one ordered workqueue per instance and the instance lock, which the netlink handlers take in their pre_doit. A driver queues a pon_work from any context. The handler runs with the lock held, so a PLOAM transition never interleaves with a netlink transaction.

The core owns the activation state. A driver reports the state it reached through pon_dev_state_report(). The core validates the edge against Table C.12.4 of ITU-T G.9807.1, publishes it and warns about an illegal edge instead of hiding it. The PON network devices have a carrier in O5 and O6 while a GEM port rides a bound alloc-id and the conduit is paired and up. The driver reports the assignment and the release of an alloc-id through pon_dev_event(), which is where the carrier learns of them.

Every edge that pon_dev_state_report() publishes gets one line in the kernel log at info level, for example "pon0: PLOAM state O5 -> O6". An XGS-PON ONU is often a router whose developers watch the serial console. The state reaches userspace through ploam-ntf only, so without the line the console shows nothing when the ONU loses its link or comes back. A repeated report changes nothing and logs nothing. The line uses the names of the uapi states. O2 is named O2-3 in every mode but G-PON, because ITU-T G.9807.1 Table C.12.1 has one Serial Number state that a driver reports as o2.

A print to a serial console blocks for milliseconds, while the driver's PLOAM work answers the OLT within a deadline. pon_dev_log() therefore records a line in a ring of PON_LOG_LINES lines under the instance lock and queues a work item on system_dfl_wq that prints it. The work holds the lock only to take one line out, never across a print. The lines keep their order. A full ring drops the oldest line and counts it. The count is printed in its place. Nothing is recorded once unregister has begun. Each line keeps its printk level. pon_dev_log() records at info level. The core helper pon_dev_log_level() takes the level from its caller.

pon_dev_log() is exported for a PON MAC driver: only the driver knows why it leaves an activation state. It logs that from the same PLOAM work, so it must not print there either. Through the same ring its line keeps its place next to the edge that the core logs.

types.h and functions.h carry the whole contract between the core and a driver, so the files that follow add code and no declarations.

The lent context exists for the MAC driver more than for the core. A PON MAC signals PLOAM messages and PHY edges in hard interrupt context, while its state machine sleeps and must not run against a netlink transaction. The driver therefore queues a pon_work from its interrupt handler. The handler runs under the instance lock. The core's own user of this context is the OMCI receive path. A plain work item would serve it. The mechanism exists for the driver.

The same holds for the reports. pon_dev_state_report() and pon_dev_event() have no caller in the core: the driver runs the state machine and receives the messages of the OLT. The core validates, publishes and relays what the driver reports.

Assisted-by LLM
---
 include/net/pon.h           |  14 ++
 include/net/pon/functions.h | 129 +++++++++++
 include/net/pon/types.h     | 443 ++++++++++++++++++++++++++++++++++++
 net/pon/pon.h               | 162 +++++++++++++
 net/pon/pon_state.c         | 408 +++++++++++++++++++++++++++++++++
 net/pon/pon_work.c          | 106 +++++++++
 6 files changed, 1262 insertions(+)
 create mode 100644 include/net/pon.h
 create mode 100644 include/net/pon/functions.h
 create mode 100644 include/net/pon/types.h
 create mode 100644 net/pon/pon.h
 create mode 100644 net/pon/pon_state.c
 create mode 100644 net/pon/pon_work.c
include/net/pon.h new file +14 -0
new file mode 100644
index 000000000000..5c85f73f98f9
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (C) 2026 John Crispin <[email protected]> */
+
+#ifndef __NET_PON_ALL_H
+#define __NET_PON_ALL_H
+
+#include <uapi/linux/pon.h>
+#include <net/pon/ploam.h>
+#include <net/pon/types.h>
+#include <net/pon/functions.h>
+
+/* Do not add any code here. Put it in the sub-headers instead. */
+
+#endif /* __NET_PON_ALL_H */
include/net/pon/functions.h new file +129 -0

129 lines not on this page · raw message

include/net/pon/types.h new file +443 -0

443 lines not on this page · raw message

net/pon/pon.h new file +162 -0

162 lines not on this page · raw message

net/pon/pon_state.c new file +408 -0

408 lines not on this page · raw message

net/pon/pon_work.c new file +106 -0

106 lines not on this page · raw message

Signature · 1 line
2.34.1
#5 lore raw

RFC 5/12 net: pon: add the conduit contract

In reply to John Crispin

A PON MAC on a system on chip has no DMA of its own, so its frames ride the rings of an ethernet controller, the conduit. The driver of the conduit registers it with pon_conduit_register() when its firmware node names the MAC through a pon-handle. The core pairs the two in whichever order they probe. Neither driver includes the headers of the other.

Per frame information crosses as explicit arguments: the GEM port, the T-CONT channel and the queue on transmit, the GEM port and the OMCI flags on receive. pon_conduit_rx() delivers a frame to the OMCI channel, to the network device of its GEM port, or to the PON data interface. A frame for a PON network device that is down is dropped and counted.

The conduit keeps the address of the data interface and the largest MTU among the PON network devices, never less than an extended OMCI PDU with its integrity field. A conduit that refuses the address or the MTU when it pairs is warned about and stays paired.

A netdev notifier follows the conduit up and down. The PON network devices keep their carrier only while the conduit is paired and up, so the carrier falls before the conduit closes or goes.

pon_conduit_register(), pon_conduit_unregister() and pon_conduit_rx() have no caller in the core. The driver of the conduit calls them. pon_conduit_register() takes rtnl, so the driver calls it without rtnl. pon_conduit_xmit() has no caller in the core either: the MAC driver calls it from its transmit paths, because only the MAC driver can fill struct pon_tx_info. The core carries that structure from one driver to the other without reading it.

Assisted-by LLM
---
 net/pon/pon_conduit.c | 614 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 614 insertions(+)
 create mode 100644 net/pon/pon_conduit.c
net/pon/pon_conduit.c new file +614 -0
new file mode 100644
index 000000000000..ed0e0dff8cd3
@@ -0,0 +1,614 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (C) 2026 John Crispin <[email protected]> */
+
+#include <linux/etherdevice.h>
+#include <linux/netdevice.h>
+#include <linux/notifier.h>
+#include <linux/property.h>
+#include <linux/rtnetlink.h>
+#include <linux/slab.h>
+#include <net/pon.h>
+
+#include "pon.h"
+
+/* One registered conduit. It stays on the list whether or not a PON MAC has
+ * claimed it, so that a MAC which registers later, or registers again, finds
+ * it.
+ */
+struct pon_conduit {
+	struct list_head list;
+	struct net_device *netdev;
+	struct fwnode_handle *mac;
+	const struct pon_conduit_ops *ops;
+	struct pon_dev *pdev;
+	bool up;
+};
+
+/* guarded by pon_devs_lock */
+static LIST_HEAD(pon_conduits);
+
+/**
+ * pon_conduit_find() - look up the conduit of a network device
+ * @netdev:	the ethernet device a driver registered as a conduit
+ *
+ * Context: Called with pon_devs_lock held.
+ * Return: the conduit, or NULL when @netdev is not a registered conduit.
+ */
+static struct pon_conduit *pon_conduit_find(const struct net_device *netdev)
+{
+	struct pon_conduit *entry;
+
+	lockdep_assert_held(&pon_devs_lock);
+	list_for_each_entry(entry, &pon_conduits, list)
+		if (entry->netdev == netdev)
+			return entry;
+	return NULL;
+}
+
+/**
+ * pon_conduit_find_free() - look up an unclaimed conduit that names a MAC
+ * @pdev:	PON device structure
+ *
+ * A conduit names its MAC through the firmware node of @pdev's parent.
+ *
+ * Context: Called with pon_devs_lock held.
+ * Return: a conduit that names the MAC of @pdev and that no instance has
+ * claimed, or NULL when there is none.
+ */
+static struct pon_conduit *pon_conduit_find_free(const struct pon_dev *pdev)
+{
+	struct fwnode_handle *mac = dev_fwnode(pdev->parent);
+	struct pon_conduit *entry;
+
+	lockdep_assert_held(&pon_devs_lock);
+	list_for_each_entry(entry, &pon_conduits, list)
+		if (entry->mac == mac && !entry->pdev)
+			return entry;
+	return NULL;
+}
+
+/**
+ * pon_conduit_pair() - pair a conduit with an instance
+ * @entry:	the conduit
+ * @pdev:	PON device structure
+ *
+ * Takes a reference on the conduit's network device and publishes the
+ * pointer from each side to the other for the receive and transmit paths.
+ *
+ * Context: Called with pon_devs_lock held.
+ */
+static void pon_conduit_pair(struct pon_conduit *entry, struct pon_dev *pdev)
+{
+	lockdep_assert_held(&pon_devs_lock);
+
+	entry->pdev = pdev;
+	netdev_hold(entry->netdev, &pdev->conduit_tracker, GFP_KERNEL);
+	rcu_assign_pointer(pdev->conduit, entry);
+	rcu_assign_pointer(entry->netdev->pon_dev, pdev);
+}
+
+/**
+ * pon_conduit_unpair() - break the pairing of a conduit and its instance
+ * @entry:	the conduit, paired with an instance
+ *
+ * The receive path finds the instance through the conduit and the transmit
+ * path finds the conduit through the instance, both under RCU, so both
+ * pointers go before the grace period and the reference after it.
+ *
+ * Context: Called with pon_devs_lock held. Sleeps.
+ */
+static void pon_conduit_unpair(struct pon_conduit *entry)
+{
+	struct pon_dev *pdev = entry->pdev;
+
+	lockdep_assert_held(&pon_devs_lock);
+
+	rcu_assign_pointer(entry->netdev->pon_dev, NULL);
+	rcu_assign_pointer(pdev->conduit, NULL);
+	synchronize_net();
+	netdev_put(entry->netdev, &pdev->conduit_tracker);
+	entry->pdev = NULL;
+}
+
+/**
+ * pon_conduit_hold() - hold the conduit across a call that may sleep
+ * @pdev:	PON device structure
+ * @ops:	where to store what the conduit's driver does, or NULL
+ * @tracker:	the tracker of the reference
+ *
+ * Return: the conduit's ethernet device with a reference held, or NULL when
+ * no driver has claimed the instance.
+ */
+struct net_device *pon_conduit_hold(struct pon_dev *pdev,
+				    const struct pon_conduit_ops **ops,
+				    netdevice_tracker *tracker)
+{
+	struct net_device *conduit = NULL;
+	struct pon_conduit *entry;
+
+	rcu_read_lock();
+	entry = rcu_dereference(pdev->conduit);
+	if (entry) {
+		conduit = entry->netdev;
+		if (ops)
+			*ops = entry->ops;
+		netdev_hold(conduit, tracker, GFP_ATOMIC);
+	}
+	rcu_read_unlock();
+
+	return conduit;
+}
+
+/**
+ * pon_conduit_running() - whether the instance has a conduit that is up
+ * @pdev:	PON device structure
+ *
+ * A conduit counts as up from NETDEV_UP until NETDEV_GOING_DOWN, so the PON
+ * network devices lose their carrier before the conduit closes.
+ *
+ * Context: Any context.
+ * Return: true when a conduit is paired with @pdev and is up.
+ */
+bool pon_conduit_running(struct pon_dev *pdev)
+{
+	struct pon_conduit *entry;
+	bool up;
+
+	rcu_read_lock();
+	entry = rcu_dereference(pdev->conduit);
+	up = entry && READ_ONCE(entry->up);
+	rcu_read_unlock();
+
+	return up;
+}
+
+/**
+ * pon_conduit_carrier_update() - set the carrier after a conduit change
+ * @pdev:	PON device structure
+ *
+ * Context: Takes @pdev->lock. Called without it.
+ */
+static void pon_conduit_carrier_update(struct pon_dev *pdev)
+{
+	mutex_lock(&pdev->lock);
+	if (pon_dev_is_registered(pdev))
+		pon_dev_carrier_update(pdev);
+	mutex_unlock(&pdev->lock);
+}
+
+/**
+ * pon_conduit_addr_set() - give the conduit the PON data interface's address
+ * @pdev:	PON device structure
+ * @addr:	the address the PON network devices carry
+ *
+ * The frame engine behind a conduit recognizes the frames addressed to the
+ * ONU by the conduit's own address, so the two are kept the same. Called with
+ * rtnl held.
+ *
+ * Return: 0, or what the conduit's driver answered.
+ */
+int pon_conduit_addr_set(struct pon_dev *pdev, const u8 *addr)
+{
+	struct sockaddr_storage ss = {};
+	struct net_device *conduit;
+	netdevice_tracker tracker;
+	int err = 0;
+
+	ASSERT_RTNL();
+
+	conduit = pon_conduit_hold(pdev, NULL, &tracker);
+	if (!conduit)
+		return 0;
+
+	if (!ether_addr_equal(conduit->dev_addr, addr)) {
+		ss.ss_family = conduit->type;
+		memcpy(ss.__data, addr, ETH_ALEN);
+		err = dev_set_mac_address(conduit, &ss, NULL);
+	}
+
+	netdev_put(conduit, &tracker);
+	return err;
+}
+EXPORT_SYMBOL_GPL(pon_conduit_addr_set);
+
+/**
+ * pon_conduit_sync() - give a newly paired conduit the address and the MTU
+ * @pdev:	PON device structure
+ *
+ * After a pairing, outside every lock: the conduit's driver programs its
+ * address and its MTU under rtnl, which is never taken while an instance lock
+ * is held. The carrier follows the new conduit. A conduit that refuses the
+ * address or the MTU is warned about and stays paired.
+ *
+ * Does nothing once pon_dev_unregister() has begun. It tests that under rtnl,
+ * which pon_dev_unregister() takes before it lets the driver free the data
+ * network device, so the device read here is alive.
+ *
+ * Context: Takes rtnl and @pdev->lock. Called without rtnl, pon_devs_lock or
+ * @pdev->lock held.
+ */
+void pon_conduit_sync(struct pon_dev *pdev)
+{
+	int err;
+
+	rtnl_lock();
+	if (READ_ONCE(pdev->going_away)) {
+		rtnl_unlock();
+		return;
+	}
+	err = pon_conduit_addr_set(pdev, pdev->main_netdev->dev_addr);
+	if (err)
+		netdev_warn(pdev->main_netdev,
+			    "the conduit refused the address: %pe\n",
+			    ERR_PTR(err));
+	err = pon_conduit_mtu_set(pdev, NULL, 0);
+	if (err)
+		netdev_warn(pdev->main_netdev,
+			    "the conduit refused the MTU: %pe\n", ERR_PTR(err));
+	rtnl_unlock();
+
+	pon_conduit_carrier_update(pdev);
+}
+
+/**
+ * pon_conduit_register() - offer a network device's rings to a PON MAC
+ * @netdev:	the registered ethernet device, whose firmware node names the
+ *		MAC through a pon-handle reference
+ * @ops:	what the driver does for the MAC
+ *
+ * Neither side waits for the other. A MAC that registers later, or again,
+ * finds the conduit. Until then its frames are dropped and the conduit's own
+ * receive path keeps what arrives.
+ *
+ * Context: Takes rtnl. Called without it.
+ * Return: 0 once registered, -ENOENT when the firmware node names no PON MAC,
+ * -EBUSY when the network device is a conduit already, or a negative errno.
+ */
+int pon_conduit_register(struct net_device *netdev,
+			 const struct pon_conduit_ops *ops)
+{
+	struct pon_dev *pdev = NULL, *iter;
+	struct fwnode_handle *mac;
+	struct pon_conduit *entry;
+	unsigned long id;
+
+	mac = fwnode_find_reference(dev_fwnode(&netdev->dev), "pon-handle", 0);
+	if (IS_ERR(mac))
+		return PTR_ERR(mac);
+
+	entry = kzalloc_obj(*entry, GFP_KERNEL);
+	if (!entry) {
+		fwnode_handle_put(mac);
+		return -ENOMEM;
+	}
+	entry->netdev = netdev;
+	entry->mac = mac;
+	entry->ops = ops;
+
+	rtnl_lock();
+	mutex_lock(&pon_devs_lock);
+
+	if (pon_conduit_find(netdev)) {
+		mutex_unlock(&pon_devs_lock);
+		rtnl_unlock();
+		fwnode_handle_put(mac);
+		kfree(entry);
+		return -EBUSY;
+	}
+	entry->up = netif_running(netdev);
+	list_add_tail(&entry->list, &pon_conduits);
+
+	xa_for_each(&pon_devs, id, iter) {
+		if (dev_fwnode(iter->parent) != mac ||
+		    rcu_access_pointer(iter->conduit))
+			continue;
+		pon_conduit_pair(entry, iter);
+		pon_dev_get(iter);
+		pdev = iter;
+		break;
+	}
+
+	mutex_unlock(&pon_devs_lock);
+	rtnl_unlock();
+
+	if (pdev) {
+		pon_conduit_sync(pdev);
+		pon_dev_put(pdev);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pon_conduit_register);
+
+/**
+ * pon_conduit_unregister() - take a network device's rings back
+ * @netdev:	the device pon_conduit_register() was given
+ *
+ * Call before unregistering the device. A MAC the device served keeps
+ * running without a conduit and its PON network devices lose their carrier.
+ */
+void pon_conduit_unregister(struct net_device *netdev)
+{
+	struct pon_conduit *entry;
+	struct pon_dev *pdev;
+
+	mutex_lock(&pon_devs_lock);
+	entry = pon_conduit_find(netdev);
+	if (entry) {
+		pdev = entry->pdev;
+		if (pdev) {
+			pon_conduit_unpair(entry);
+			pon_conduit_carrier_update(pdev);
+		}
+		list_del(&entry->list);
+	}
+	mutex_unlock(&pon_devs_lock);
+
+	if (!entry)
+		return;
+
+	fwnode_handle_put(entry->mac);
+	kfree(entry);
+}
+EXPORT_SYMBOL_GPL(pon_conduit_unregister);
+
+/**
+ * pon_conduit_netdev_event() - follow a conduit that goes up or down
+ * @nb:		the notifier block
+ * @event:	what happened to the network device
+ * @ptr:	struct netdev_notifier_info of the network device
+ *
+ * A registered conduit counts as up from NETDEV_UP until NETDEV_GOING_DOWN.
+ * The PON network devices of the instance it serves get their carrier
+ * again, so they never carry traffic into a conduit that is down.
+ *
+ * Context: Called with rtnl held. Takes pon_devs_lock and the instance lock.
+ * Return: NOTIFY_DONE.
+ */
+static int pon_conduit_netdev_event(struct notifier_block *nb,
+				    unsigned long event, void *ptr)
+{
+	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
+	struct pon_conduit *entry;
+
+	if (event != NETDEV_UP && event != NETDEV_GOING_DOWN)
+		return NOTIFY_DONE;
+
+	mutex_lock(&pon_devs_lock);
+	entry = pon_conduit_find(netdev);
+	if (entry) {
+		WRITE_ONCE(entry->up, event == NETDEV_UP);
+		if (entry->pdev)
+			pon_conduit_carrier_update(entry->pdev);
+	}
+	mutex_unlock(&pon_devs_lock);
+
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block pon_conduit_netdev_notifier = {
+	.notifier_call = pon_conduit_netdev_event,
+};
+
+/**
+ * pon_conduit_notifier_register() - watch the conduits go up and down
+ *
+ * Return: 0, or the errno of register_netdevice_notifier().
+ */
+int pon_conduit_notifier_register(void)
+{
+	return register_netdevice_notifier(&pon_conduit_netdev_notifier);
+}
+
+/**
+ * pon_conduit_notifier_unregister() - stop the watch of the conduits
+ */
+void pon_conduit_notifier_unregister(void)
+{
+	unregister_netdevice_notifier(&pon_conduit_netdev_notifier);
+}
+
+/**
+ * pon_conduit_attach() - claim a waiting conduit for a new instance
+ * @pdev:	PON device structure
+ *
+ * A new instance claims the conduit that names its MAC, if one is waiting.
+ *
+ * Context: Called with pon_devs_lock held.
+ * Return: true when a conduit was paired and there is an address to sync
+ * with pon_conduit_sync() once the locks are dropped, false otherwise.
+ */
+bool pon_conduit_attach(struct pon_dev *pdev)
+{
+	struct pon_conduit *entry;
+
+	entry = pon_conduit_find_free(pdev);
+	if (!entry)
+		return false;
+
+	pon_conduit_pair(entry, pdev);
+	return true;
+}
+
+/**
+ * pon_conduit_detach() - release the conduit an instance has claimed
+ * @pdev:	PON device structure
+ *
+ * The conduit stays registered and waits for the next instance of its MAC.
+ * Once this returns, no frame of the conduit reaches @pdev.
+ *
+ * Context: Takes pon_devs_lock. Sleeps.
+ */
+void pon_conduit_detach(struct pon_dev *pdev)
+{
+	struct pon_conduit *entry;
+
+	mutex_lock(&pon_devs_lock);
+	entry = rcu_dereference_protected(pdev->conduit,
+					  lockdep_is_held(&pon_devs_lock));
+	if (entry)
+		pon_conduit_unpair(entry);
+	mutex_unlock(&pon_devs_lock);
+}
+
+/**
+ * pon_conduit_mtu_largest() - the MTU the conduit needs
+ * @pdev:	PON device structure
+ * @dev:	the PON network device whose MTU changes, or NULL when none does
+ * @mtu:	the MTU @dev changes to
+ *
+ * Takes @mtu for @dev and the current MTU of every other PON network device
+ * of @pdev. The floor is the extended OMCI PDU with its MIC, 1980 bytes,
+ * ITU-T G.988 clause 11.2.5.
+ *
+ * Return: the largest of these MTUs.
+ */
+static unsigned int pon_conduit_mtu_largest(struct pon_dev *pdev,
+					    const struct net_device *dev,
+					    unsigned int mtu)
+{
+	struct net_device *gem;
+	unsigned long id;
+
+	mtu = max(mtu, PON_OMCI_MAX_LEN + PON_OMCI_MIC_LEN);
+
+	if (dev != pdev->main_netdev)
+		mtu = max(mtu, READ_ONCE(pdev->main_netdev->mtu));
+
+	xa_for_each(&pdev->gem_netdevs, id, gem)
+		if (dev != gem)
+			mtu = max(mtu, READ_ONCE(gem->mtu));
+
+	return mtu;
+}
+
+/**
+ * pon_conduit_mtu_set() - make room on the conduit for a PON network device
+ * @pdev:	PON device structure
+ * @dev:	the PON network device whose MTU changes, or NULL when none does
+ * @mtu:	the MTU @dev changes to
+ *
+ * The frames of every PON network device and the OMCI PDUs ride the conduit,
+ * so the conduit takes the largest MTU among them and never less than the
+ * longest OMCI PDU with its integrity field. Called with rtnl held.
+ *
+ * Return: 0, or what the conduit's driver answered.
+ */
+int pon_conduit_mtu_set(struct pon_dev *pdev, const struct net_device *dev,
+			unsigned int mtu)
+{
+	const struct pon_conduit_ops *ops;
+	struct net_device *conduit;
+	netdevice_tracker tracker;
+	int err = 0;
+
+	ASSERT_RTNL();
+
+	conduit = pon_conduit_hold(pdev, &ops, &tracker);
+	if (!conduit)
+		return 0;
+
+	mtu = pon_conduit_mtu_largest(pdev, dev, mtu);
+	if (mtu != READ_ONCE(conduit->mtu))
+		err = dev_set_mtu(conduit, mtu);
+
+	netdev_put(conduit, &tracker);
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(pon_conduit_mtu_set);
+
+/**
+ * pon_conduit_xmit() - send one frame through the conduit
+ * @pdev:	PON device structure
+ * @skb:	the frame, consumed whatever this returns
+ * @info:	what the conduit needs to build the descriptor
+ *
+ * Return: 0 once the conduit has the frame, -ENETDOWN without a conduit,
+ * -EBUSY when its ring is full.
+ */
+int pon_conduit_xmit(struct pon_dev *pdev, struct sk_buff *skb,
+		     const struct pon_tx_info *info)
+{
+	struct pon_conduit *entry;
+	netdev_tx_t ret;
+
+	rcu_read_lock();
+	entry = rcu_dereference(pdev->conduit);
+	if (!entry) {
+		rcu_read_unlock();
+		dev_kfree_skb_any(skb);
+		return -ENETDOWN;
+	}
+	ret = entry->ops->xmit(entry->netdev, skb, info);
+	rcu_read_unlock();
+
+	if (ret == NETDEV_TX_BUSY) {
+		dev_kfree_skb_any(skb);
+		return -EBUSY;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pon_conduit_xmit);
+
+/**
+ * pon_conduit_rx() - hand one received frame to the PON MAC that owns it
+ * @conduit:	the ethernet device the frame arrived on
+ * @skb:	the frame, starting at its first byte: the MAC header of an
+ *		Ethernet frame, the transaction id of an OMCI PDU
+ * @info:	what the receive descriptor said about it
+ *
+ * Runs in the conduit's NAPI context. An OMCI PDU goes to the socket that
+ * owns the OMCI channel and is consumed here, a frame on a GEM port that
+ * carries a network device of its own goes there and everything else goes
+ * to the PON data interface. A frame for a network device that is down is
+ * dropped and counted in its rx_dropped.
+ *
+ * Return: 1 when the frame belongs to a PON network device (now skb->dev)
+ * and the conduit delivers it, 0 when it was consumed here, or -ENODEV when
+ * no PON MAC owns the conduit and the frame stays the conduit's.
+ */
+int pon_conduit_rx(struct net_device *conduit, struct sk_buff *skb,
+		   const struct pon_rx_info *info)
+{
+	struct net_device *dev;
+	struct pon_dev *pdev;
+	unsigned int len;
+	int ret;
+
+	rcu_read_lock();
+
+	pdev = rcu_dereference(conduit->pon_dev);
+	if (!pdev) {
+		ret = -ENODEV;
+		goto out;
+	}
+
+	if (info->oam) {
+		ret = pon_omci_conduit_rx(pdev, skb, info->mic_unchecked);
+		goto out;
+	}
+
+	dev = xa_load(&pdev->gem_netdevs, info->gem);
+	if (!dev)
+		dev = pdev->main_netdev;
+
+	if (!(READ_ONCE(dev->flags) & IFF_UP)) {
+		dev_core_stats_rx_dropped_inc(dev);
+		kfree_skb(skb);
+		ret = 0;
+		goto out;
+	}
+
+	len = skb->len;
+	skb->dev = dev;
+	skb->protocol = eth_type_trans(skb, dev);
+	dev_sw_netstats_rx_add(dev, len);
+	ret = 1;
+
+out:
+	rcu_read_unlock();
+	return ret;
+}
+EXPORT_SYMBOL_GPL(pon_conduit_rx);
Signature · 1 line
2.34.1
#6 lore raw

RFC 6/12 net: pon: add the GEM network devices

In reply to John Crispin

A service that wants a network device of its own gets one per GEM port, created with the rtnetlink link kind "gem" on top of the PON data interface. The one attribute of the link kind is the GEM port id (IFLA_GEM_ID in uapi/linux/if_link.h, listed in rt-link.yaml for ynl). It must lie in the assignable range of ITU-T G.9807.1 Table C.6.6.

The device is registered outside the instance lock and attached to its GEM port under it. The receive path finds it under RCU. It transmits through the gem_xmit callback of the driver and takes the MTU range of the data interface. A frame that the callback refuses is counted as dropped, as the data interface counts it.

As a VLAN device does, the device names the data interface as its link and transmits without a lock of its own (lltx). A module alias loads the core for the link kind.

The gem_xmit callback belongs to the MAC driver, because only the MAC driver knows the transmit channel and the queue of a GEM port. It fills the descriptor information and hands the frame to the conduit.

Assisted-by LLM
---
 Documentation/netlink/specs/rt-link.yaml |  14 +
 include/uapi/linux/if_link.h             |  10 +
 net/pon/pon_gem.c                        | 537 +++++++++++++++++++++++
 3 files changed, 561 insertions(+)
 create mode 100644 net/pon/pon_gem.c
Documentation/netlink/specs/rt-link.yaml +14 -0
index 81b58633911d..ee2cb7d12c28 100644
@@ -2368,6 +2368,17 @@ attribute-sets:
         name: mode
         type: u8
         enum: ovpn-mode
+  -
+    name: linkinfo-gem-attrs
+    name-prefix: ifla-gem-
+    attributes:
+      -
+        name: id
+        doc: GEM port ID of the PON device the GEM network device rides.
+        type: u32
+        checks:
+          min: 1021
+          max: 65534
 
 sub-messages:
   -
@@ -2427,6 +2438,9 @@ sub-messages:
       -
         value: ovpn
         attribute-set: linkinfo-ovpn-attrs
+      -
+        value: gem
+        attribute-set: linkinfo-gem-attrs
   -
     name: linkinfo-member-data-msg
     formats:
include/uapi/linux/if_link.h +10 -0
index 245b36204525..6d4d149cf4ab 100644
@@ -2075,4 +2075,14 @@ enum {
 
 #define IFLA_OVPN_MAX	(__IFLA_OVPN_MAX - 1)
 
+/* GEM section */
+
+enum {
+	IFLA_GEM_UNSPEC,
+	IFLA_GEM_ID,
+	__IFLA_GEM_MAX,
+};
+
+#define IFLA_GEM_MAX	(__IFLA_GEM_MAX - 1)
+
 #endif /* _UAPI_LINUX_IF_LINK_H */
net/pon/pon_gem.c new file +537 -0

537 lines not on this page · raw message

Signature · 1 line
2.34.1
#7 lore raw

RFC 7/12 net: pon: add the OMCI channel

In reply to John Crispin

OMCI is the management protocol of ITU-T G.988. It is the job of userspace. The kernel carries it over the pon netlink family, as nl80211 carries management frames. One socket registers for the OMCI PDUs of a device, receives them as notifications and sends with omci-tx. The registration ends when the socket closes.

A PDU crosses the boundary bare, without its integrity field. The core checks the framing of a PDU to send against G.988 clause 11.2.3: 44 bytes in the baseline format, the header and the stated contents length in the extended format. So every driver gets the same PDUs. A PDU that the MAC passed up unchecked is verified by the driver, which holds the OMCI integrity key. Received PDUs wait for the context of the instance in one queue bounded at 512, in the order they arrived, checked or not. A PDU that finds no owner or no room is dropped and counted.

The omci_xmit callback runs with bottom halves disabled, because it ends in the transmit path of the conduit.

A MAC can check the integrity of a downstream OMCI PDU inline. A PDU that it passes up unchecked goes to the omci_verify callback of the driver, which checks it with the OMCI integrity key. The key stays in the driver and the check can sleep, so the core defers the call to the lent context. The core has no key and never checks a MIC itself. Without omci_verify it drops an unchecked PDU.

Assisted-by LLM
---
 net/pon/pon_omci.c | 326 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 326 insertions(+)
 create mode 100644 net/pon/pon_omci.c
net/pon/pon_omci.c new file +326 -0
new file mode 100644
index 000000000000..c8093a9c3749
@@ -0,0 +1,326 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (C) 2026 John Crispin <[email protected]> */
+
+#include <linux/atomic.h>
+#include <linux/bottom_half.h>
+#include <linux/netlink.h>
+#include <linux/notifier.h>
+#include <linux/skbuff.h>
+#include <linux/xarray.h>
+#include <linux/unaligned.h>
+#include <net/net_namespace.h>
+#include <net/pon.h>
+
+#include "pon.h"
+
+#define PON_OMCI_DEVID_OFF		3
+#define PON_OMCI_DEVID_BASELINE		0x0a
+#define PON_OMCI_DEVID_EXTENDED		0x0b
+#define PON_OMCI_CONTENTS_LEN_OFF	8
+#define PON_OMCI_EXTENDED_HDR_LEN	10
+#define PON_OMCI_BASELINE_LEN		44
+#define PON_OMCI_CB(skb)		((struct pon_omci_cb *)(skb)->cb)
+
+/**
+ * struct pon_omci_cb - what the receive queue keeps with a PDU
+ * @unverified: the MAC did not check the integrity of the PDU, which still
+ *		ends with its MIC
+ */
+struct pon_omci_cb {
+	bool unverified;
+};
+
+/**
+ * pon_omci_len_valid() - check the length of an OMCI PDU
+ * @len:	the length of the PDU in bytes
+ * @trailer:	the bytes the PDU carries after the message, PON_OMCI_MIC_LEN
+ *		when it still ends with its MIC, 0 otherwise
+ *
+ * The limits are those of the extended message format, ITU-T G.988 clause
+ * 11.2.5 and Table 11.2-2.
+ *
+ * Return: true when @len lies between PON_OMCI_MIN_LEN and PON_OMCI_MAX_LEN,
+ * both plus @trailer, false otherwise.
+ */
+static bool pon_omci_len_valid(unsigned int len, unsigned int trailer)
+{
+	return len >= PON_OMCI_MIN_LEN + trailer &&
+	       len <= PON_OMCI_MAX_LEN + trailer;
+}
+
+/**
+ * pon_omci_framing_valid() - check the framing of an OMCI PDU to send
+ * @pdu:	the PDU, without its MIC
+ * @len:	the length of @pdu in bytes, at least PON_OMCI_MIN_LEN
+ *
+ * The device identifier of ITU-T G.988 clause 11.2.3 selects the format. A
+ * baseline PDU (Table 11.2-1) is 44 bytes without the MIC. An extended PDU
+ * (Table 11.2-2) is the 10 byte header and as many bytes of contents as its
+ * message contents length states.
+ *
+ * Return: true when @len is the length the header of @pdu calls for, false
+ * otherwise.
+ */
+static bool pon_omci_framing_valid(const u8 *pdu, unsigned int len)
+{
+	unsigned int contents;
+
+	switch (pdu[PON_OMCI_DEVID_OFF]) {
+	case PON_OMCI_DEVID_BASELINE:
+		return len == PON_OMCI_BASELINE_LEN;
+	case PON_OMCI_DEVID_EXTENDED:
+		contents = get_unaligned_be16(pdu + PON_OMCI_CONTENTS_LEN_OFF);
+		return len == PON_OMCI_EXTENDED_HDR_LEN + contents;
+	default:
+		return false;
+	}
+}
+
+/**
+ * pon_omci_deliver() - hand one OMCI PDU to the owner of the OMCI channel
+ * @pdev:	PON device structure
+ * @skb:	the verified PDU, which is consumed
+ *
+ * A PDU that cannot be sent to the owner, or that arrives while no socket
+ * owns the channel, is dropped.
+ *
+ * Context: The instance's context, with @pdev->lock held.
+ */
+static void pon_omci_deliver(struct pon_dev *pdev, struct sk_buff *skb)
+{
+	if (pon_nl_omci_ntf(pdev, skb)) {
+		kfree_skb(skb);
+		return;
+	}
+
+	consume_skb(skb);
+}
+
+/**
+ * pon_omci_rx_verify() - check a received OMCI PDU through the driver
+ * @pdev:	PON device structure
+ * @skb:	the PDU the MAC passed up unchecked, ending with its MIC
+ *
+ * The driver's omci_verify callback checks and strips the MIC (ITU-T
+ * G.9807.1 clause C.15.7.2). A PDU that fails is freed. A PDU that cannot be
+ * made linear is freed too, because its integrity was never checked.
+ *
+ * Context: The instance's context, with @pdev->lock held.
+ * Return: true when the PDU passed and is now bare, false when it is gone.
+ */
+static bool pon_omci_rx_verify(struct pon_dev *pdev, struct sk_buff *skb)
+{
+	if (skb_linearize(skb) || pdev->ops->omci_verify(pdev, skb)) {
+		kfree_skb(skb);
+		return false;
+	}
+
+	return true;
+}
+
+/**
+ * pon_omci_rx_work() - verify and deliver the queued OMCI PDUs
+ * @pdev:	PON device structure
+ * @work:	the instance's @omci_rx_work
+ *
+ * Takes the PDUs in the order they arrived. One the MAC passed up unchecked
+ * goes through pon_omci_rx_verify() first. One the MAC verified goes to the
+ * owner as it is.
+ *
+ * Context: The instance's context, with @pdev->lock held.
+ */
+static void pon_omci_rx_work(struct pon_dev *pdev, struct pon_work *work)
+{
+	struct sk_buff *skb;
+
+	while ((skb = skb_dequeue(&pdev->omci_rxq))) {
+		if (PON_OMCI_CB(skb)->unverified &&
+		    !pon_omci_rx_verify(pdev, skb))
+			continue;
+
+		pon_omci_deliver(pdev, skb);
+	}
+}
+
+/**
+ * pon_omci_conduit_rx() - take one OMCI PDU from the conduit
+ * @pdev:	PON device structure
+ * @skb:	the PDU, starting at the transaction correlation id and ending
+ *		with its 4 byte MIC when @unverified
+ * @unverified:	the MAC did not check the integrity of the PDU
+ *
+ * Runs in the conduit's NAPI context. The PDU waits for the instance's
+ * context, which verifies it through the driver when @unverified and hands
+ * it to the owner of the OMCI channel. Checked and unchecked PDUs share one
+ * queue, so the owner gets them in the order they arrived. A PDU is at most
+ * PON_OMCI_MAX_LEN bytes and PON_OMCI_MIC_LEN more when @unverified, because
+ * the driver strips the MIC only once it has checked it (ITU-T G.988 clause
+ * 11.2.5). A PDU that arrives while no socket owns the channel is dropped
+ * here.
+ *
+ * Return: 0, the skb is consumed.
+ */
+int pon_omci_conduit_rx(struct pon_dev *pdev, struct sk_buff *skb,
+			bool unverified)
+{
+	if (!pon_omci_len_valid(skb->len, unverified ? PON_OMCI_MIC_LEN : 0) ||
+	    !READ_ONCE(pdev->omci_portid) ||
+	    (unverified && !pdev->ops->omci_verify) ||
+	    skb_queue_len_lockless(&pdev->omci_rxq) >= PON_OMCI_QUEUE_MAX) {
+		kfree_skb(skb);
+		return 0;
+	}
+
+	PON_OMCI_CB(skb)->unverified = unverified;
+	skb_queue_tail(&pdev->omci_rxq, skb);
+	pon_work_queue(pdev, &pdev->omci_rx_work);
+
+	return 0;
+}
+
+/**
+ * pon_omci_xmit() - send one OMCI PDU to the OLT
+ * @pdev:	PON device structure
+ * @pdu:	the PDU, without its MIC
+ * @len:	the length of @pdu in bytes
+ * @extack:	netlink extended ack for the reason the core refuses the PDU,
+ *		or NULL
+ *
+ * Copies the PDU into an skb and hands it to the driver's omci_xmit
+ * callback with bottom halves disabled. The length must fit the extended
+ * message format, ITU-T G.988 clause 11.2.5 and Table 11.2-2, less the 4
+ * byte MIC, which @pdu does not carry. It must also be the length that the
+ * header of the PDU calls for, as pon_omci_framing_valid() checks, so the
+ * driver receives a bare PDU of a known format. Only that refusal sets
+ * @extack. An error of the driver leaves it alone.
+ *
+ * Context: Called with @pdev->lock held, from the omci-tx netlink handler.
+ * Return: 0, -EINVAL for a length out of range or a length that does not
+ * match the format, -ENOMEM, or the driver's errno.
+ */
+int pon_omci_xmit(struct pon_dev *pdev, const void *pdu, unsigned int len,
+		  struct netlink_ext_ack *extack)
+{
+	struct sk_buff *skb;
+	int err;
+
+	lockdep_assert_held(&pdev->lock);
+
+	if (!pon_omci_len_valid(len, 0) || !pon_omci_framing_valid(pdu, len)) {
+		NL_SET_ERR_MSG(extack,
+			       "OMCI PDU length does not match its format");
+		return -EINVAL;
+	}
+
+	skb = alloc_skb(len, GFP_KERNEL);
+	if (!skb)
+		return -ENOMEM;
+	skb_put_data(skb, pdu, len);
+
+	local_bh_disable();
+	err = pdev->ops->omci_xmit(pdev, skb);
+	local_bh_enable();
+
+	return err;
+}
+
+/**
+ * pon_omci_register() - claim the OMCI channel of a PON device for a socket
+ * @pdev:	PON device structure
+ * @portid:	the netlink port id of the socket
+ *
+ * The first socket to claim the channel owns it until it closes, when
+ * pon_omci_netlink_notify() releases it. A second claim by the owner
+ * succeeds.
+ *
+ * Context: Called with @pdev->lock held.
+ * Return: 0 when @portid owns the channel, -EBUSY when another socket does.
+ */
+int pon_omci_register(struct pon_dev *pdev, u32 portid)
+{
+	u32 owner;
+
+	lockdep_assert_held(&pdev->lock);
+
+	owner = cmpxchg(&pdev->omci_portid, 0, portid);
+
+	return owner && owner != portid ? -EBUSY : 0;
+}
+
+/**
+ * pon_omci_netlink_notify() - release the OMCI channels of a closed socket
+ * @nb:		the notifier block
+ * @state:	what happened to the socket
+ * @data:	struct netlink_notify of the socket
+ *
+ * Runs when a netlink socket is released. Every device in the netns of the
+ * socket that it owned the OMCI channel of loses its owner.
+ *
+ * Return: NOTIFY_DONE.
+ */
+static int pon_omci_netlink_notify(struct notifier_block *nb,
+				   unsigned long state, void *data)
+{
+	struct netlink_notify *notify = data;
+	struct pon_dev *pdev;
+	unsigned long id;
+
+	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
+		return NOTIFY_DONE;
+
+	rcu_read_lock();
+	xa_for_each(&pon_devs, id, pdev) {
+		if (!net_eq(dev_net(pdev->main_netdev), notify->net))
+			continue;
+		cmpxchg(&pdev->omci_portid, notify->portid, 0);
+	}
+	rcu_read_unlock();
+
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block pon_omci_netlink_notifier = {
+	.notifier_call = pon_omci_netlink_notify,
+};
+
+/**
+ * pon_omci_notifier_register() - watch for closed netlink sockets
+ *
+ * Return: 0, or the errno of netlink_register_notifier().
+ */
+int pon_omci_notifier_register(void)
+{
+	return netlink_register_notifier(&pon_omci_netlink_notifier);
+}
+
+/**
+ * pon_omci_notifier_unregister() - stop the watch for closed netlink sockets
+ */
+void pon_omci_notifier_unregister(void)
+{
+	netlink_unregister_notifier(&pon_omci_netlink_notifier);
+}
+
+/**
+ * pon_omci_init() - prepare the OMCI receive path of a new PON device
+ * @pdev:	PON device structure
+ *
+ * Context: From pon_dev_create(), before the device is published.
+ */
+void pon_omci_init(struct pon_dev *pdev)
+{
+	skb_queue_head_init(&pdev->omci_rxq);
+	pon_work_init(&pdev->omci_rx_work, pon_omci_rx_work);
+}
+
+/**
+ * pon_omci_destroy() - free the OMCI PDUs still queued
+ * @pdev:	PON device structure
+ *
+ * Context: From pon_dev_unregister(), after the conduit is detached and the
+ * instance's work is canceled.
+ */
+void pon_omci_destroy(struct pon_dev *pdev)
+{
+	skb_queue_purge(&pdev->omci_rxq);
+}
Signature · 1 line
2.34.1
#8 lore raw

RFC 8/12 net: pon: add netlink support

In reply to John Crispin

The handlers of the pon family, whose spec came first: the identity and the link, the T-CONTs, the GEM ports and the upstream classifier rules and the OMCI channel. pon-nl-gen.c and pon-nl-gen.h are generated from pon.yaml with ynl-gen.

Every handler that names a device takes the instance lock in its pre_doit. A write handler sends no message of its own. The netlink ACK carries its result. Object notifications share the reply format of the matching get. An object dump takes an optional device id and is marked interrupted when a list changed under it, also when the part that follows the change is empty.

A T-CONT and its alloc-id are one to one, as ITU-T G.988 clause 9.2.2 defines. A rebind moves the GEM ports of the T-CONT and restores them when the driver refuses one. A GEM port that exists with other attributes is refused. An upstream or bidirectional GEM port needs a T-CONT. A downstream one takes none. No key material crosses the
family: a GEM port selects its key ring as clause 9.2.3 defines it. The broadcast ring is refused.

The set_identity callback gets the registration id padded with 0x00 to the 36 octets of ITU-T G.9807.1 Table C.11.25, so every driver pads the same way. An empty registration id is refused. The core keeps the serial number only. The serial number and the mode do not change while the link is enabled, because the OLT addresses the ONU by its serial number. The zero serial number, which addresses every ONU, is refused.

A mode the device does not list in its capabilities is refused with an extack message and one line at error level through pon_dev_log_level() that names the mode, for example "mode gpon not supported by the device". The log work prints it, so no print runs under the instance lock. A refused request is no failure of the system, so it raises no WARN.

Assisted-by LLM
---
 net/pon/pon-nl-gen.c |  286 ++++++++
 net/pon/pon-nl-gen.h |   45 ++
 net/pon/pon_nl.c     | 1671 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 2002 insertions(+)
 create mode 100644 net/pon/pon-nl-gen.c
 create mode 100644 net/pon/pon-nl-gen.h
 create mode 100644 net/pon/pon_nl.c
net/pon/pon-nl-gen.c new file +286 -0
new file mode 100644
index 000000000000..e9898952c9fa
@@ -0,0 +1,286 @@
+// SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
+/* Do not edit directly, auto-generated from: */
+/*	Documentation/netlink/specs/pon.yaml */
+/* YNL-GEN kernel source */
+/* To regenerate run: tools/net/ynl/ynl-regen.sh */
+
+#include <net/netlink.h>
+#include <net/genetlink.h>
+
+#include "pon-nl-gen.h"
+
+#include <uapi/linux/pon.h>
+
+/* Integer value ranges */
+static const struct netlink_range_validation pon_a_tcont_index_range = {
+	.max	= 65534ULL,
+};
+
+static const struct netlink_range_validation pon_a_gem_id_range = {
+	.min	= 1021ULL,
+	.max	= 65534ULL,
+};
+
+static const struct netlink_range_validation pon_a_gem_tcont_index_range = {
+	.max	= 65534ULL,
+};
+
+static const struct netlink_range_validation pon_a_gem_map_gem_id_range = {
+	.min	= 1021ULL,
+	.max	= 65534ULL,
+};
+
+/* PON_CMD_DEV_GET - do */
+static const struct nla_policy pon_dev_get_nl_policy[PON_A_DEV_ID + 1] = {
+	[PON_A_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+};
+
+/* PON_CMD_DEV_SET - do */
+static const struct nla_policy pon_dev_set_nl_policy[PON_A_DEV_ENABLE + 1] = {
+	[PON_A_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+	[PON_A_DEV_MODE] = NLA_POLICY_MAX(NLA_U32, 2),
+	[PON_A_DEV_SERIAL] = NLA_POLICY_EXACT_LEN(8),
+	[PON_A_DEV_REGISTRATION_ID] = NLA_POLICY_MAX_LEN(36),
+	[PON_A_DEV_ENABLE] = NLA_POLICY_MAX(NLA_U8, 1),
+};
+
+/* PON_CMD_TCONT_GET - do */
+static const struct nla_policy pon_tcont_get_do_nl_policy[PON_A_TCONT_INDEX + 1] = {
+	[PON_A_TCONT_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+	[PON_A_TCONT_INDEX] = NLA_POLICY_FULL_RANGE(NLA_U32, &pon_a_tcont_index_range),
+};
+
+/* PON_CMD_TCONT_GET - dump */
+static const struct nla_policy pon_tcont_get_dump_nl_policy[PON_A_TCONT_DEV_ID + 1] = {
+	[PON_A_TCONT_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+};
+
+/* PON_CMD_TCONT_SET - do */
+static const struct nla_policy pon_tcont_set_nl_policy[PON_A_TCONT_ALLOC_ID + 1] = {
+	[PON_A_TCONT_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+	[PON_A_TCONT_INDEX] = NLA_POLICY_FULL_RANGE(NLA_U32, &pon_a_tcont_index_range),
+	[PON_A_TCONT_ALLOC_ID] = NLA_POLICY_MAX(NLA_U32, 16383),
+};
+
+/* PON_CMD_TCONT_DEL - do */
+static const struct nla_policy pon_tcont_del_nl_policy[PON_A_TCONT_INDEX + 1] = {
+	[PON_A_TCONT_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+	[PON_A_TCONT_INDEX] = NLA_POLICY_FULL_RANGE(NLA_U32, &pon_a_tcont_index_range),
+};
+
+/* PON_CMD_GEM_GET - do */
+static const struct nla_policy pon_gem_get_do_nl_policy[PON_A_GEM_ID + 1] = {
+	[PON_A_GEM_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+	[PON_A_GEM_ID] = NLA_POLICY_FULL_RANGE(NLA_U32, &pon_a_gem_id_range),
+};
+
+/* PON_CMD_GEM_GET - dump */
+static const struct nla_policy pon_gem_get_dump_nl_policy[PON_A_GEM_DEV_ID + 1] = {
+	[PON_A_GEM_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+};
+
+/* PON_CMD_GEM_NEW - do */
+static const struct nla_policy pon_gem_new_nl_policy[PON_A_GEM_KEY_RING + 1] = {
+	[PON_A_GEM_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+	[PON_A_GEM_ID] = NLA_POLICY_FULL_RANGE(NLA_U32, &pon_a_gem_id_range),
+	[PON_A_GEM_DIR] = NLA_POLICY_RANGE(NLA_U32, 1, 3),
+	[PON_A_GEM_TCONT_INDEX] = NLA_POLICY_FULL_RANGE(NLA_U32, &pon_a_gem_tcont_index_range),
+	[PON_A_GEM_KEY_RING] = NLA_POLICY_MAX(NLA_U32, 3),
+};
+
+/* PON_CMD_GEM_DEL - do */
+static const struct nla_policy pon_gem_del_nl_policy[PON_A_GEM_ID + 1] = {
+	[PON_A_GEM_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+	[PON_A_GEM_ID] = NLA_POLICY_FULL_RANGE(NLA_U32, &pon_a_gem_id_range),
+};
+
+/* PON_CMD_GEM_MAP_GET - dump */
+static const struct nla_policy pon_gem_map_get_nl_policy[PON_A_GEM_MAP_DEV_ID + 1] = {
+	[PON_A_GEM_MAP_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+};
+
+/* PON_CMD_GEM_MAP_NEW - do */
+static const struct nla_policy pon_gem_map_new_nl_policy[PON_A_GEM_MAP_DSCP + 1] = {
+	[PON_A_GEM_MAP_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+	[PON_A_GEM_MAP_GEM_ID] = NLA_POLICY_FULL_RANGE(NLA_U32, &pon_a_gem_map_gem_id_range),
+	[PON_A_GEM_MAP_TAG] = NLA_POLICY_MAX(NLA_U32, 1),
+	[PON_A_GEM_MAP_VID] = NLA_POLICY_MAX(NLA_U32, 4094),
+	[PON_A_GEM_MAP_PBIT] = NLA_POLICY_MAX(NLA_U32, 7),
+	[PON_A_GEM_MAP_DSCP] = NLA_POLICY_MAX(NLA_U32, 63),
+};
+
+/* PON_CMD_GEM_MAP_DEL - do */
+static const struct nla_policy pon_gem_map_del_nl_policy[PON_A_GEM_MAP_DSCP + 1] = {
+	[PON_A_GEM_MAP_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+	[PON_A_GEM_MAP_GEM_ID] = NLA_POLICY_FULL_RANGE(NLA_U32, &pon_a_gem_map_gem_id_range),
+	[PON_A_GEM_MAP_TAG] = NLA_POLICY_MAX(NLA_U32, 1),
+	[PON_A_GEM_MAP_VID] = NLA_POLICY_MAX(NLA_U32, 4094),
+	[PON_A_GEM_MAP_PBIT] = NLA_POLICY_MAX(NLA_U32, 7),
+	[PON_A_GEM_MAP_DSCP] = NLA_POLICY_MAX(NLA_U32, 63),
+};
+
+/* PON_CMD_OMCI_REGISTER - do */
+static const struct nla_policy pon_omci_register_nl_policy[PON_A_OMCI_DEV_ID + 1] = {
+	[PON_A_OMCI_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+};
+
+/* PON_CMD_OMCI_TX - do */
+static const struct nla_policy pon_omci_tx_nl_policy[PON_A_OMCI_PDU + 1] = {
+	[PON_A_OMCI_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1),
+	[PON_A_OMCI_PDU] = NLA_POLICY_MAX_LEN(1976),
+};
+
+/* Ops table for pon */
+static const struct genl_split_ops pon_nl_ops[] = {
+	{
+		.cmd		= PON_CMD_DEV_GET,
+		.pre_doit	= pon_device_get_locked,
+		.doit		= pon_nl_dev_get_doit,
+		.post_doit	= pon_device_unlock,
+		.policy		= pon_dev_get_nl_policy,
+		.maxattr	= PON_A_DEV_ID,
+		.flags		= GENL_CMD_CAP_DO,
+	},
+	{
+		.cmd	= PON_CMD_DEV_GET,
+		.dumpit	= pon_nl_dev_get_dumpit,
+		.flags	= GENL_CMD_CAP_DUMP,
+	},
+	{
+		.cmd		= PON_CMD_DEV_SET,
+		.pre_doit	= pon_device_get_locked,
+		.doit		= pon_nl_dev_set_doit,
+		.post_doit	= pon_device_unlock,
+		.policy		= pon_dev_set_nl_policy,
+		.maxattr	= PON_A_DEV_ENABLE,
+		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+	},
+	{
+		.cmd		= PON_CMD_TCONT_GET,
+		.pre_doit	= pon_device_get_locked,
+		.doit		= pon_nl_tcont_get_doit,
+		.post_doit	= pon_device_unlock,
+		.policy		= pon_tcont_get_do_nl_policy,
+		.maxattr	= PON_A_TCONT_INDEX,
+		.flags		= GENL_CMD_CAP_DO,
+	},
+	{
+		.cmd		= PON_CMD_TCONT_GET,
+		.dumpit		= pon_nl_tcont_get_dumpit,
+		.policy		= pon_tcont_get_dump_nl_policy,
+		.maxattr	= PON_A_TCONT_DEV_ID,
+		.flags		= GENL_CMD_CAP_DUMP,
+	},
+	{
+		.cmd		= PON_CMD_TCONT_SET,
+		.pre_doit	= pon_device_get_locked,
+		.doit		= pon_nl_tcont_set_doit,
+		.post_doit	= pon_device_unlock,
+		.policy		= pon_tcont_set_nl_policy,
+		.maxattr	= PON_A_TCONT_ALLOC_ID,
+		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+	},
+	{
+		.cmd		= PON_CMD_TCONT_DEL,
+		.pre_doit	= pon_device_get_locked,
+		.doit		= pon_nl_tcont_del_doit,
+		.post_doit	= pon_device_unlock,
+		.policy		= pon_tcont_del_nl_policy,
+		.maxattr	= PON_A_TCONT_INDEX,
+		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+	},
+	{
+		.cmd		= PON_CMD_GEM_GET,
+		.pre_doit	= pon_device_get_locked,
+		.doit		= pon_nl_gem_get_doit,
+		.post_doit	= pon_device_unlock,
+		.policy		= pon_gem_get_do_nl_policy,
+		.maxattr	= PON_A_GEM_ID,
+		.flags		= GENL_CMD_CAP_DO,
+	},
+	{
+		.cmd		= PON_CMD_GEM_GET,
+		.dumpit		= pon_nl_gem_get_dumpit,
+		.policy		= pon_gem_get_dump_nl_policy,
+		.maxattr	= PON_A_GEM_DEV_ID,
+		.flags		= GENL_CMD_CAP_DUMP,
+	},
+	{
+		.cmd		= PON_CMD_GEM_NEW,
+		.pre_doit	= pon_device_get_locked,
+		.doit		= pon_nl_gem_new_doit,
+		.post_doit	= pon_device_unlock,
+		.policy		= pon_gem_new_nl_policy,
+		.maxattr	= PON_A_GEM_KEY_RING,
+		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+	},
+	{
+		.cmd		= PON_CMD_GEM_DEL,
+		.pre_doit	= pon_device_get_locked,
+		.doit		= pon_nl_gem_del_doit,
+		.post_doit	= pon_device_unlock,
+		.policy		= pon_gem_del_nl_policy,
+		.maxattr	= PON_A_GEM_ID,
+		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+	},
+	{
+		.cmd		= PON_CMD_GEM_MAP_GET,
+		.dumpit		= pon_nl_gem_map_get_dumpit,
+		.policy		= pon_gem_map_get_nl_policy,
+		.maxattr	= PON_A_GEM_MAP_DEV_ID,
+		.flags		= GENL_CMD_CAP_DUMP,
+	},
+	{
+		.cmd		= PON_CMD_GEM_MAP_NEW,
+		.pre_doit	= pon_device_get_locked,
+		.doit		= pon_nl_gem_map_new_doit,
+		.post_doit	= pon_device_unlock,
+		.policy		= pon_gem_map_new_nl_policy,
+		.maxattr	= PON_A_GEM_MAP_DSCP,
+		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+	},
+	{
+		.cmd		= PON_CMD_GEM_MAP_DEL,
+		.pre_doit	= pon_device_get_locked,
+		.doit		= pon_nl_gem_map_del_doit,
+		.post_doit	= pon_device_unlock,
+		.policy		= pon_gem_map_del_nl_policy,
+		.maxattr	= PON_A_GEM_MAP_DSCP,
+		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+	},
+	{
+		.cmd		= PON_CMD_OMCI_REGISTER,
+		.pre_doit	= pon_device_get_locked,
+		.doit		= pon_nl_omci_register_doit,
+		.post_doit	= pon_device_unlock,
+		.policy		= pon_omci_register_nl_policy,
+		.maxattr	= PON_A_OMCI_DEV_ID,
+		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+	},
+	{
+		.cmd		= PON_CMD_OMCI_TX,
+		.pre_doit	= pon_device_get_locked,
+		.doit		= pon_nl_omci_tx_doit,
+		.post_doit	= pon_device_unlock,
+		.policy		= pon_omci_tx_nl_policy,
+		.maxattr	= PON_A_OMCI_PDU,
+		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+	},
+};
+
+static const struct genl_multicast_group pon_nl_mcgrps[] = {
+	[PON_NLGRP_MGMT] = { "mgmt", },
+	[PON_NLGRP_STATE] = { "state", },
+};
+
+struct genl_family pon_nl_family __ro_after_init = {
+	.name		= PON_FAMILY_NAME,
+	.version	= PON_FAMILY_VERSION,
+	.netnsok	= true,
+	.parallel_ops	= true,
+	.module		= THIS_MODULE,
+	.split_ops	= pon_nl_ops,
+	.n_split_ops	= ARRAY_SIZE(pon_nl_ops),
+	.mcgrps		= pon_nl_mcgrps,
+	.n_mcgrps	= ARRAY_SIZE(pon_nl_mcgrps),
+};
net/pon/pon-nl-gen.h new file +45 -0

45 lines not on this page · raw message

net/pon/pon_nl.c new file +1671 -0

1,671 lines not on this page · raw message

Signature · 1 line
2.34.1
#9 lore raw

RFC 9/12 net: pon: add the device registration

In reply to John Crispin

pon_dev_create() ties the components to one instance. It takes the data network device, the device and the ops of the MAC, pairs a waiting conduit and publishes the instance. The data network device must be bound to its network namespace, because the core looks a device up by that namespace.

pon_dev_unregister() stops the upstream link and withdraws the instance:
its conduit, its work, its GEM network devices and its objects. It disables the work item, so that a late queue runs nothing. It prints the lines that pon_dev_log() recorded until then, then disables the log work. The structure stays until the driver drops it with pon_dev_put(), so the interrupts and timers of a driver never name a freed device.

The module registers the netlink family, the gem link kind, the notifier that releases the OMCI channels and the notifier that follows the conduits. A module alias loads it for the family. DOC: PON locking documents the lock order.

The workqueue of an instance has high priority because the MAC driver runs the handlers of its activation timers on it. drv_priv is cleared only after synchronize_net(): a GEM transmit that read the ops under RCU can still reach the driver, which finds itself through drv_priv.

Assisted-by LLM
---
 net/pon/pon_main.c | 454 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 454 insertions(+)
 create mode 100644 net/pon/pon_main.c
net/pon/pon_main.c new file +454 -0
new file mode 100644
index 000000000000..977b2635a8ea
@@ -0,0 +1,454 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (C) 2026 John Crispin <[email protected]> */
+
+#include <linux/list.h>
+#include <linux/module.h>
+#include <linux/netdevice.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/string.h>
+#include <linux/workqueue.h>
+#include <linux/xarray.h>
+#include <net/pon.h>
+
+#include "pon.h"
+#include "pon-nl-gen.h"
+
+DEFINE_XARRAY_ALLOC1(pon_devs);
+/* guards the pon_devs xarray, taken before any instance lock */
+DEFINE_MUTEX(pon_devs_lock);
+
+/**
+ * DOC: PON locking
+ *
+ * pon_devs_lock protects the pon_devs xarray, the conduit list and the
+ * pairing of a conduit with an instance.
+ * Ordering is take the pon_devs_lock and then the instance lock.
+ * rtnl is taken before them and never while an instance lock or pon_devs_lock
+ * is held: the GEM netdevs are registered and unregistered outside them and
+ * the conduit's address and MTU are set after a pairing once both are
+ * dropped.
+ * Each instance is protected by RCU and has a refcount.
+ * When the driver unregisters, the instance gets flushed, but the struct
+ * sticks around.
+ *
+ * The instance lock is also the activation lock. Every deferred report
+ * reaches the state machine through pdev->wq, whose handler takes it. The
+ * netlink handlers take the same lock in their pre_doit, so a userspace
+ * transaction cannot interleave with a half run transition. pdev->work_lock
+ * is the one lock below it, held only to add and remove work items from
+ * hard interrupt context.
+ */
+
+/**
+ * pon_tcont_find() - look up a T-CONT by its index
+ * @pdev:	PON device structure
+ * @index:	the T-CONT index
+ *
+ * Context: Called with @pdev->lock held.
+ * Return: the T-CONT, or NULL when no T-CONT has @index.
+ */
+struct pon_tcont *pon_tcont_find(struct pon_dev *pdev, u16 index)
+{
+	struct pon_tcont *tcont;
+
+	lockdep_assert_held(&pdev->lock);
+	list_for_each_entry(tcont, &pdev->tconts, list)
+		if (tcont->cfg.index == index)
+			return tcont;
+	return NULL;
+}
+
+/**
+ * pon_gem_find() - look up a GEM port by its id
+ * @pdev:	PON device structure
+ * @gem_id:	the GEM port id
+ *
+ * Context: Called with @pdev->lock held.
+ * Return: the GEM port, or NULL when no GEM port has @gem_id.
+ */
+struct pon_gem *pon_gem_find(struct pon_dev *pdev, u16 gem_id)
+{
+	struct pon_gem *gem;
+
+	lockdep_assert_held(&pdev->lock);
+	list_for_each_entry(gem, &pdev->gems, list)
+		if (gem->cfg.id == gem_id)
+			return gem;
+	return NULL;
+}
+
+/**
+ * pon_gem_channel() - the conduit transmit channel of a GEM port
+ * @pdev:	PON device structure
+ * @gem:	the GEM port
+ *
+ * Asks the driver's tcont_channel callback for the channel of the T-CONT
+ * that @gem rides.
+ *
+ * Context: Called with @pdev->lock held.
+ * Return: the channel, -ENOLINK when @gem rides no T-CONT, the T-CONT does
+ * not exist, the driver has no tcont_channel callback or the T-CONT has no
+ * channel yet, or another negative errno from the driver.
+ */
+int pon_gem_channel(struct pon_dev *pdev, const struct pon_gem *gem)
+{
+	struct pon_tcont *tcont;
+
+	if (!gem->cfg.tcont_valid || !pdev->ops->tcont_channel)
+		return -ENOLINK;
+
+	tcont = pon_tcont_find(pdev, gem->cfg.tcont_index);
+	if (!tcont)
+		return -ENOLINK;
+
+	return pdev->ops->tcont_channel(pdev, &tcont->cfg);
+}
+
+/**
+ * pon_gem_map_same() - test whether two classifier rules are the same rule
+ * @existing:	the rule the instance holds
+ * @requested:	the rule a request names
+ *
+ * A rule is identified by everything it matches on, because two rules for one
+ * GEM that differ only in their VLAN are different rules.
+ *
+ * Return: true when @existing and @requested match on the same fields for the
+ * same GEM port.
+ */
+static bool pon_gem_map_same(const struct pon_gem_map_cfg *existing,
+			     const struct pon_gem_map_cfg *requested)
+{
+	return existing->gem_id == requested->gem_id &&
+	       existing->tag_valid == requested->tag_valid &&
+	       existing->tagged == requested->tagged &&
+	       existing->vid_valid == requested->vid_valid &&
+	       existing->vid == requested->vid &&
+	       existing->pbit_valid == requested->pbit_valid &&
+	       existing->pbit == requested->pbit &&
+	       existing->dscp_valid == requested->dscp_valid &&
+	       existing->dscp == requested->dscp;
+}
+
+/**
+ * pon_gem_map_find() - look up an upstream classifier rule
+ * @pdev:	PON device structure
+ * @cfg:	the rule to look for, compared with pon_gem_map_same()
+ *
+ * Context: Called with @pdev->lock held.
+ * Return: the stored rule, or NULL when the instance holds no such rule.
+ */
+struct pon_gem_map *pon_gem_map_find(struct pon_dev *pdev,
+				     const struct pon_gem_map_cfg *cfg)
+{
+	struct pon_gem_map *map;
+
+	lockdep_assert_held(&pdev->lock);
+	list_for_each_entry(map, &pdev->gem_maps, list)
+		if (pon_gem_map_same(&map->cfg, cfg))
+			return map;
+	return NULL;
+}
+
+/**
+ * pon_dev_create() - create and register a PON device
+ * @netdev:	the PON data network device, already registered, with
+ *		per-CPU transmit and receive statistics
+ *		(NETDEV_PCPU_STAT_TSTATS) for the conduit's receive path to
+ *		count into and bound to its network namespace
+ *		(netns_immutable) since before it was registered
+ * @parent:	the MAC's device, which the interfaces the core creates
+ *		parent onto and whose firmware node a conduit names to be
+ *		paired with this instance
+ * @ops:	driver callbacks
+ * @caps:	device capabilities
+ * @mode:	the mode the MAC is configured for, enum pon_mode
+ * @priv_ptr:	back-pointer to driver private data
+ *
+ * Syncs the conduit when one is already registered, so it takes rtnl and must
+ * not be called with rtnl held. The activation state starts unknown. The driver
+ * reports it through pon_dev_state_report() from its own work.
+ *
+ * Return: pointer to the allocated PON device, or ERR_PTR.
+ */
+struct pon_dev *pon_dev_create(struct net_device *netdev,
+			       struct device *parent,
+			       const struct pon_dev_ops *ops,
+			       const struct pon_dev_caps *caps,
+			       enum pon_mode mode, void *priv_ptr)
+{
+	static u32 last_id;
+	struct pon_dev *pdev;
+	bool paired;
+	int err;
+
+	if (WARN_ON(!netdev || !parent || !ops || !caps ||
+		    netdev->pcpu_stat_type != NETDEV_PCPU_STAT_TSTATS ||
+		    !netdev->netns_immutable ||
+		    !ops->tcont_set ||
+		    !ops->tcont_clear ||
+		    !ops->gem_add ||
+		    !ops->gem_del ||
+		    !ops->omci_xmit))
+		return ERR_PTR(-EINVAL);
+
+	pdev = kzalloc_obj(*pdev, GFP_KERNEL);
+	if (!pdev)
+		return ERR_PTR(-ENOMEM);
+
+	pdev->main_netdev = netdev;
+	pdev->parent = parent;
+	pdev->ops = ops;
+	pdev->caps = caps;
+	pdev->mode = mode;
+	pdev->drv_priv = priv_ptr;
+	pdev->ploam = PON_PLOAM_STATE_UNKNOWN;
+
+	mutex_init(&pdev->lock);
+	spin_lock_init(&pdev->work_lock);
+	INIT_LIST_HEAD(&pdev->tconts);
+	INIT_LIST_HEAD(&pdev->gems);
+	INIT_LIST_HEAD(&pdev->gem_maps);
+	xa_init(&pdev->gem_netdevs);
+	INIT_LIST_HEAD(&pdev->work_list);
+	INIT_WORK(&pdev->work, pon_work_worker);
+	pon_log_init(pdev);
+	refcount_set(&pdev->refcnt, 1);
+
+	/* Ordered, so that the work items run one at a time and in the order
+	 * they were queued. High priority, because the activation and key
+	 * exchange timers whose handlers a driver runs on it are short.
+	 */
+	pdev->wq = alloc_ordered_workqueue("pon-%s", WQ_HIGHPRI, netdev->name);
+	if (!pdev->wq) {
+		err = -ENOMEM;
+		goto err_free;
+	}
+
+	pon_omci_init(pdev);
+
+	mutex_lock(&pon_devs_lock);
+	err = xa_alloc_cyclic(&pon_devs, &pdev->id, pdev, xa_limit_16b,
+			      &last_id, GFP_KERNEL);
+	if (err) {
+		mutex_unlock(&pon_devs_lock);
+		goto err_wq;
+	}
+	paired = pon_conduit_attach(pdev);
+	mutex_lock(&pdev->lock);
+	mutex_unlock(&pon_devs_lock);
+
+	pon_nl_notify_dev(pdev, PON_CMD_DEV_ADD_NTF);
+
+	rcu_assign_pointer(netdev->pon_dev, pdev);
+
+	mutex_unlock(&pdev->lock);
+
+	if (paired)
+		pon_conduit_sync(pdev);
+
+	return pdev;
+
+err_wq:
+	destroy_workqueue(pdev->wq);
+err_free:
+	mutex_destroy(&pdev->lock);
+	kfree(pdev);
+	return ERR_PTR(err);
+}
+EXPORT_SYMBOL_GPL(pon_dev_create);
+
+/**
+ * pon_dev_free() - free a PON device once its last reference is gone
+ * @pdev:	PON device structure
+ *
+ * Releases the device's id, destroys its workqueue and frees the structure
+ * after an RCU grace period.
+ *
+ * Context: Process context. Takes pon_devs_lock and may sleep.
+ */
+static void pon_dev_free(struct pon_dev *pdev)
+{
+	mutex_lock(&pon_devs_lock);
+	xa_erase(&pon_devs, pdev->id);
+	mutex_unlock(&pon_devs_lock);
+
+	destroy_workqueue(pdev->wq);
+	xa_destroy(&pdev->gem_netdevs);
+	mutex_destroy(&pdev->lock);
+	kfree_rcu(pdev, rcu);
+}
+
+/**
+ * pon_dev_put() - drop a reference to a PON device
+ * @pdev:	PON device structure
+ *
+ * A driver drops the reference pon_dev_create() returned, once, after
+ * pon_dev_unregister() and after it has released everything of its own that
+ * names the device: its interrupts, its timers and the data network device.
+ */
+void pon_dev_put(struct pon_dev *pdev)
+{
+	if (refcount_dec_and_test(&pdev->refcnt))
+		pon_dev_free(pdev);
+}
+EXPORT_SYMBOL_GPL(pon_dev_put);
+
+/**
+ * pon_dev_unregister() - unregister a PON device
+ * @pdev:	PON device structure
+ *
+ * Stops the upstream link through the driver's enable callback when it is
+ * enabled, then withdraws the device. Once this returns no netlink request,
+ * no work item and no network device of the core reaches the driver. The
+ * lines that pon_dev_log() recorded until then are printed before it
+ * returns.
+ *
+ * The device itself stays until the driver calls pon_dev_put(). What a
+ * driver still reports to it in between, from an interrupt or from the data
+ * network device, is refused.
+ *
+ * Unregisters network devices, so it takes rtnl and must not be called with
+ * rtnl held.
+ */
+void pon_dev_unregister(struct pon_dev *pdev)
+{
+	struct pon_gem_map *map, *map_next;
+	struct pon_tcont *tcont, *tcont_next;
+	struct pon_gem *gem, *gem_next;
+
+	mutex_lock(&pon_devs_lock);
+	mutex_lock(&pdev->lock);
+
+	/* Wait until pon_dev_free() to call xa_erase() so this id cannot be
+	 * reused while references are still held. Storing NULL makes every
+	 * netlink lookup fail from here on.
+	 */
+	xa_store(&pon_devs, pdev->id, NULL, GFP_KERNEL);
+	pon_nl_obj_gen_inc();
+	mutex_unlock(&pon_devs_lock);
+
+	if (pdev->enabled) {
+		pdev->ops->enable(pdev, false, NULL);
+		pdev->enabled = false;
+	}
+
+	pon_nl_notify_dev(pdev, PON_CMD_DEV_DEL_NTF);
+
+	WRITE_ONCE(pdev->going_away, true);
+	mutex_unlock(&pdev->lock);
+
+	/* First, so that no frame reaches the instance once its objects go. */
+	pon_conduit_detach(pdev);
+
+	/* Drop what is queued before stopping the worker, or an item requeued
+	 * by the one still running would outlive the cancel.
+	 */
+	pon_work_drain(pdev);
+
+	/* Each takes the instance lock, so none is waited for with it held. */
+	disable_work_sync(&pdev->work);
+	flush_work(&pdev->log_work);
+	disable_work_sync(&pdev->log_work);
+
+	pon_gem_netdevs_unregister(pdev);
+
+	pon_omci_destroy(pdev);
+
+	mutex_lock(&pdev->lock);
+
+	list_for_each_entry_safe(tcont, tcont_next, &pdev->tconts, list) {
+		list_del(&tcont->list);
+		kfree(tcont);
+	}
+	list_for_each_entry_safe(gem, gem_next, &pdev->gems, list) {
+		list_del(&gem->list);
+		kfree(gem);
+	}
+	list_for_each_entry_safe(map, map_next, &pdev->gem_maps, list) {
+		list_del(&map->list);
+		kfree(map);
+	}
+
+	rcu_assign_pointer(pdev->main_netdev->pon_dev, NULL);
+
+	WRITE_ONCE(pdev->ops, NULL);
+	memzero_explicit(&pdev->identity, sizeof(pdev->identity));
+
+	mutex_unlock(&pdev->lock);
+
+	/* The GEM transmit path reads @ops under RCU. */
+	synchronize_net();
+
+	/*
+	 * Only now: a transmit that loaded @ops before the store above is
+	 * still inside its read side section and reaches the driver, which
+	 * reads @drv_priv to find itself.
+	 */
+	pdev->drv_priv = NULL;
+}
+EXPORT_SYMBOL_GPL(pon_dev_unregister);
+
+/**
+ * pon_init() - register the PON core
+ *
+ * Registers the generic netlink family, the GEM link type, the netlink
+ * notifier that releases the OMCI channels and the netdev notifier that
+ * follows the conduits.
+ *
+ * Runs before the device drivers when built in, because an ethernet driver
+ * registers its conduit from its own probe. A module carries the same
+ * ordering through its dependencies.
+ *
+ * Return: 0, or a negative errno when a registration fails.
+ */
+static int __init pon_init(void)
+{
+	int err;
+
+	err = genl_register_family(&pon_nl_family);
+	if (err)
+		return err;
+
+	err = pon_gem_link_register();
+	if (err)
+		goto err_family;
+
+	err = pon_omci_notifier_register();
+	if (err)
+		goto err_link;
+
+	err = pon_conduit_notifier_register();
+	if (err)
+		goto err_omci;
+
+	return 0;
+
+err_omci:
+	pon_omci_notifier_unregister();
+err_link:
+	pon_gem_link_unregister();
+err_family:
+	genl_unregister_family(&pon_nl_family);
+	return err;
+}
+subsys_initcall(pon_init);
+
+/**
+ * pon_exit() - unregister the PON core
+ *
+ * Undoes pon_init() in the reverse order.
+ */
+static void __exit pon_exit(void)
+{
+	pon_conduit_notifier_unregister();
+	pon_omci_notifier_unregister();
+	pon_gem_link_unregister();
+	genl_unregister_family(&pon_nl_family);
+}
+module_exit(pon_exit);
+
+MODULE_ALIAS_GENL_FAMILY(PON_FAMILY_NAME);
+MODULE_AUTHOR("John Crispin <[email protected]>");
+MODULE_DESCRIPTION("Passive optical network ONU support");
+MODULE_LICENSE("GPL");
Signature · 1 line
2.34.1
#10 lore raw

RFC 10/12 net: pon: build the PON subsystem

In reply to John Crispin

Add CONFIG_PON (a tristate) and the Makefile of the core. Hook net/pon into net/Kconfig and net/Makefile.

The build is wired last, so every commit before it builds and the series bisects.

Assisted-by LLM
---
 net/Kconfig      |  1 +
 net/Makefile     |  1 +
 net/pon/Kconfig  | 17 +++++++++++++++++
 net/pon/Makefile |  6 ++++++
 4 files changed, 25 insertions(+)
 create mode 100644 net/pon/Kconfig
 create mode 100644 net/pon/Makefile
net/Kconfig +1 -0
index 76ab44aa439a..117845100a06 100644
@@ -82,6 +82,7 @@ menu "Networking options"
 
 source "net/packet/Kconfig"
 source "net/psp/Kconfig"
+source "net/pon/Kconfig"
 source "net/unix/Kconfig"
 source "net/tls/Kconfig"
 source "net/xfrm/Kconfig"
net/Makefile +1 -0
index 5b2dd7f07a85..757650c5428d 100644
@@ -19,6 +19,7 @@ obj-$(CONFIG_TLS)		+= tls/
 obj-$(CONFIG_XFRM)		+= xfrm/
 obj-$(CONFIG_UNIX)		+= unix/
 obj-$(CONFIG_INET_PSP)		+= psp/
+obj-$(CONFIG_PON)		+= pon/
 obj-y				+= ipv6/
 obj-$(CONFIG_PACKET)		+= packet/
 obj-$(CONFIG_NET_KEY)		+= key/
net/pon/Kconfig new file +17 -0
new file mode 100644
index 000000000000..af40d4ebd938
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# PON subsystem configuration
+#
+config PON
+	tristate "Passive Optical Network (PON) device support"
+	depends on NET
+	help
+	  Enable kernel support for PON (GPON / XG(S)-PON) ONU devices: the
+	  netlink configuration family (which also carries the OMCI
+	  management channel to a userspace OMCI stack) and per GEM port
+	  virtual devices.
+
+	  To compile this as a module, choose M here: the module will be
+	  called pon.
+
+	  If unsure, say N.
net/pon/Makefile new file +6 -0
new file mode 100644
index 000000000000..4b23d2265be9
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-$(CONFIG_PON) += pon.o
+
+pon-y := pon_main.o pon_nl.o pon_omci.o pon_gem.o pon_conduit.o \
+	 pon_work.o pon_state.o pon_ploam_msg.o pon-nl-gen.o
Signature · 1 line
2.34.1
#11 lore raw

RFC 11/12 Documentation: networking: describe the PON subsystem

In reply to John Crispin

pon.rst describes the model, activation and why its state machine runs in the driver, the activation log, the objects across a loss of the link, the lent context, the OMCI channel, the conduit and the netlink interface. It says which modes the uapi reserves and that the core refuses a mode the device does not list.

Assisted-by LLM
---
 Documentation/networking/index.rst |   1 +
 Documentation/networking/pon.rst   | 302 +++++++++++++++++++++++++++++
 2 files changed, 303 insertions(+)
 create mode 100644 Documentation/networking/pon.rst
Documentation/networking/index.rst +1 -0
index 44a422ad3b05..05eaac7212f3 100644
@@ -96,6 +96,7 @@ Contents:
    phy-port
    pktgen
    plip
+   pon
    ppp_generic
    proc_net_tcp
    pse-pd/index
Documentation/networking/pon.rst new file +302 -0

302 lines not on this page · raw message

Signature · 1 line
2.34.1
#12 lore raw

RFC 12/12 MAINTAINERS: add the PON subsystem

In reply to John Crispin

Add an entry for the PON subsystem: the core under net/pon, its headers, the uapi header, the netlink specification and the documentation.

Assisted-by LLM
---
 MAINTAINERS | 11 +++++++++++
 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
index 72ca3aab2106..0513cd25a039 100644
@@ -20774,6 +20774,17 @@ F:	include/linux/parman.h
 F:	lib/parman.c
 F:	lib/test_parman.c
 
+PASSIVE OPTICAL NETWORK (PON) SUBSYSTEM
+M:	John Crispin <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/netlink/specs/pon.yaml
+F:	Documentation/networking/pon.rst
+F:	include/net/pon.h
+F:	include/net/pon/
+F:	include/uapi/linux/pon.h
+F:	net/pon/
+
 PC ENGINES APU BOARD DRIVER
 M:	Enrico Weigelt, metux IT consult <[email protected]>
 S:	Maintained
Signature · 1 line
2.34.1
#13 lore raw
In reply to John Crispin

Hi John,

Your cover letter explicitly presents this series as following the framework discussion I started. Yet neither Ziyou nor I was copied on the cover letter or any of the twelve patches.

Given our participation in the original discussion and the cross-vendor follow-up, both of us should have been copied. We find this omission inappropriate.

I've added both of us to Cc. Please keep us copied on future revisions and related framework discussions.

Gaoyang

#14 lore raw
In reply to John Crispin

Hi John,

Quoted 2 lines · John Crispin

The scope is XGS-PON. The mode enum keeps gpon and xg-pon so that the uapi order stays clean for later drivers.

A G-PON data point, since the framework thread mentioned Realtek only as work with proprietary components. odi-oss [1] is open firmware for a GPON SFP ONU stick on the Realtek RTL9602C. It runs mainline 6.18 with GPL drivers of our own for the GPON MAC, the PLOAM state machine of G.984.3, the CPU NIC and the switch, and with a userspace OMCI daemon. Apart from the stock bootloader, the image has no vendor code or binaries. It runs in production on two ISPs, and a user has it working on a third.

I should be upfront: I am not a PON or kernel expert. The drivers were written with an AI coding assistant, working from the ITU-T recommendations and from register traces of the stock firmware, and checked by trial and error on my own sticks against live OLTs. So take what follows as field data from one G-PON implementation, not as review from someone who knows the standards well. Corrections are very welcome.

Our split is already the one this series takes: PLOAM in the MAC driver, OMCI in userspace over netlink (a private family for now). So our G-PON driver should be able to sit on net/pon, as a second MAC and a first G-PON one, at least out of tree (the RTL9602C platform itself is not upstream). Reading the series with that in mind, four things would get in the way:

1. The activation edges. pon_state_legal[] is one table for every mode, and the comment says a per-mode split can wait for a second MAC. Our state machine follows G.984.3 Table 10-1, and five of its edges are not in the table:

     O3 -> O2  TO1 expires in Serial_Number
     O5 -> O2  Deactivate_ONU-ID in Operation (O6 -> O2 too)
     O6 -> O4  broadcast POPUP
     O6 -> O7  Disable_Serial_Number in POPUP
     O7 -> O2  Disable_Serial_Number "enable"; G.9807.1 goes to O1

Each would be published with a warning on every occurrence. A table per mode, selected by the device mode, would fix it.

2. GEM port ids. The spec takes 1021 to 65534, the XGEM Port-ID range. A G-PON Port-ID is 12 bits. The operator with six GEM ports mentioned above assigns 269 to 909, and ours on the other ISP 1434 to 1946. The range would need to depend on the mode too.

3. The datapath of an SFP ONU. Service traffic never reaches the CPU on this stick. The switch bridges the host SerDes and the PON in hardware, and our OMCI daemon programs it from the MIB: GEM flows, upstream queues and VLAN treatment from the Extended VLAN Tagging ME. Only OMCI goes through the CPU NIC: received frames carry a trap reason in the RX descriptor, and we send with per-frame descriptor words (port and GEM stream). So OMCI fits the conduit contract well, but there is no data netdev to pair and nothing to bridge in Linux. Can a driver own the T-CONT, GEM and gem-map objects and offload them without a data netdev or GEM netdevs? The VLAN rules we need also go beyond tag/vid/pbit/dscp (double-tag rules, TPID, the treatment side), but that may belong to switchdev rather than here.

4. Observing OMCI. omci-ntf goes to the one registered socket. Being able to watch the exchange without taking over the channel, as Benjamin and Ziyou asked in the earlier thread, is what we use most when an OLT behaves oddly. A read-only multicast copy of the PDUs in both directions would cover it.

One small note for the docs: baseline G-PON OMCI ends with a CRC-32 trailer, not a MIC. On the RTL9602C neither direction is checked or added by the MAC, so our driver would compute it in software. That fits the current pdu attribute; the docs could just name the G-PON trailer alongside the MIC.

If a per-mode state table and Port-ID range are acceptable, I can port our driver onto the series and report back. I can also share PLOAM traces from both ISPs.

[1] https://github.com/AndreZiviani/odi-oss

Thanks,
Andre Ziviani

#15 lore raw
Quoted 15 lines · Andre Ziviani

Hi John,

Quoted 2 lines · John Crispin

The scope is XGS-PON. The mode enum keeps gpon and xg-pon so that the uapi order stays clean for later drivers.

A G-PON data point, since the framework thread mentioned Realtek only as work with proprietary components. odi-oss [1] is open firmware for a GPON SFP ONU stick on the Realtek RTL9602C. It runs mainline 6.18 with GPL drivers of our own for the GPON MAC, the PLOAM state machine of G.984.3, the CPU NIC and the switch, and with a userspace OMCI daemon. Apart from the stock bootloader, the image has no vendor code or binaries. It runs in production on two ISPs, and a user has it working on a third.

[...]

Quoted 7 lines

traces from both ISPs.

[1] https://github.com/AndreZiviani/odi-oss

Thanks,
Andre Ziviani

and those dongle can be bought on fs.com for ~12euro

I'll grab a couple. Thanks for the info !