Hello world.
This commit is contained in:
commit
5514d65ac5
365 changed files with 192057 additions and 0 deletions
19
.gitignore
vendored
Normal file
19
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# Build output directories
|
||||
dist/*
|
||||
!dist/.keep
|
||||
_build/
|
||||
|
||||
# Object files and libraries
|
||||
*.o
|
||||
*.ko
|
||||
*.kld
|
||||
*.a
|
||||
|
||||
# Dependency tracking
|
||||
.depend*
|
||||
|
||||
# Kernel module build artifacts
|
||||
export_syms
|
||||
*_if.h
|
||||
opt_*.h
|
||||
machine
|
||||
220
ARCHITECTURE.md
Normal file
220
ARCHITECTURE.md
Normal file
|
|
@ -0,0 +1,220 @@
|
|||
# Architecture Overview
|
||||
|
||||
Hardware flow offload stack for the Mono Gateway (NXP LS1046A / DPAA1).
|
||||
Packets matching offloaded flows are forwarded entirely in FMan hardware,
|
||||
bypassing the FreeBSD networking stack.
|
||||
|
||||
## System Diagram
|
||||
|
||||
```
|
||||
┌────────────────────────────────────────────────────────────────┐
|
||||
│ USERSPACE │
|
||||
│ │
|
||||
│ ┌─────────┐ ┌─────────┐ ┌──────────┐ ┌────────────┐ │
|
||||
│ │ cmm │ │ dpa_app │ │ fand │ │ cmmctl │ │
|
||||
│ │ │ │ │ │ │ │ │ │
|
||||
│ │ offload │ │ PCD │ │ fan │ │ query/ctrl │ │
|
||||
│ │ manager │ │ setup │ │ control │ │ tool │ │
|
||||
│ └────┬────┘ └────┬────┘ └──────────┘ └─────┬──────┘ │
|
||||
│ │FCI │chardev │socket │
|
||||
│ ══════╪══════════════╪═══════════════════════════════╪═══════ │
|
||||
│ KERNEL │ │ │
|
||||
│ │ ┌────┴────┐ │ │
|
||||
│ ┌────┴────┐ │ fmc │ ┌────┴────┐ │
|
||||
│ │ fci.ko │ │ chardev │ │ cmm │ │
|
||||
│ │ │ │ (fman) │ │ ctrl │ │
|
||||
│ │ cmd/evt │ └────┬────┘ │ socket │ │
|
||||
│ │ channel │ │FM ioctl └─────────┘ │
|
||||
│ └────┬────┘ ┌────┴────────────────────────┐ │
|
||||
│ │ │ FreeBSD DPAA1 kernel │ │
|
||||
│ ┌────┴────┐ │ (dtsec, QMan, BMan, FMan) │ │
|
||||
│ │ cdx.ko │ └────┬────────────────────────┘ │
|
||||
│ │ │ │ │
|
||||
│ │ flow │ ┌────┴────┐ ┌────────────┐ ┌─────────────┐ │
|
||||
│ │ engine │ │pf_notify│ │auto_bridge │ │ caam.ko │ │
|
||||
│ └────┬────┘ │ .ko │ │ .ko │ │ (crypto) │ │
|
||||
│ │ └────┬────┘ └─────┬──────┘ └─────────────┘ │
|
||||
│ │ │ │ │
|
||||
│ ┌────┴──────────────┴─────────────┴──────────────────────┐ │
|
||||
│ │ FMan Hardware │ │
|
||||
│ │ Parser -> KeyGen -> CC/Hash Tables --> Policer/EN. │ │
|
||||
│ └────────────────────────────────────────────────────────┘ │
|
||||
└────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Boot Sequence
|
||||
|
||||
```
|
||||
kernel boot
|
||||
└─► kldload cdx fci pf_notify auto_bridge
|
||||
└─► dpa_app /etc/cdx_pcd.xml /etc/cdx_cfg.xml /etc/cdx_sp.xml
|
||||
└─► service cmm start
|
||||
```
|
||||
|
||||
## Fastpath Modules
|
||||
|
||||
### cdx.ko — Flow Offload Engine (kernel module)
|
||||
|
||||
The core datapath acceleration module. Receives conntrack and route
|
||||
registrations from CMM via FCI, installs flow entries in FMan's
|
||||
enhanced hash tables, and executes packet transformations (NAT,
|
||||
TTL decrement, MAC rewrite) in hardware.
|
||||
|
||||
- **Timer wheel**: Ages out idle flows (TCP ~12h, UDP unidir ~5min)
|
||||
- **Miss path**: Unmatched packets enqueue to dtsec default RX FQID (software path)
|
||||
- **Notifications**: Sends `CONNTRACK_CHANGE` events back to CMM via FCI when flows expire
|
||||
|
||||
Source: `fastpath/cdx/` + `fastpath/vendor/cdx/`
|
||||
|
||||
### fci.ko — Fast Control Interface (kernel module)
|
||||
|
||||
IPC channel between userspace (CMM) and kernel (CDX). Provides:
|
||||
|
||||
- **Command path** (CMM → CDX): register/deregister routes, conntrack entries, L2 flows
|
||||
- **Event path** (CDX → CMM): flow timeout notifications, TCP FIN events
|
||||
- **Mechanism**: chardev with ioctl for commands, ring buffer + kqueue for async events
|
||||
|
||||
Source: `fastpath/fci/`
|
||||
|
||||
### pf_notify.ko — PF State Event Bridge (kernel module)
|
||||
|
||||
Hooks into the PF firewall to push state change events to userspace:
|
||||
|
||||
- `PFN_EVENT_INSERT` — new state created
|
||||
- `PFN_EVENT_READY` — state became offload-eligible (TCP ESTABLISHED)
|
||||
- `PFN_EVENT_DELETE` — state removed by PF
|
||||
|
||||
Exposes `/dev/pfnotify` (read-only, non-blocking). Without this module,
|
||||
CMM falls back to polling the PF state table every 1s.
|
||||
|
||||
Source: `fastpath/pf_notify/`
|
||||
|
||||
### auto_bridge.ko — L2 Bridge Flow Offload (kernel module)
|
||||
|
||||
Captures L2 forwarding decisions from the FreeBSD bridge and
|
||||
exposes them to CMM for hardware offload. Exposes `/dev/autobridge`.
|
||||
|
||||
Source: `fastpath/auto_bridge/`
|
||||
|
||||
### cmm — Connection Management Module (userspace daemon)
|
||||
|
||||
The orchestrator. Monitors PF state and routing tables, resolves
|
||||
next-hop neighbors, and programs CDX with offload entries.
|
||||
|
||||
```
|
||||
┌──────────────┐
|
||||
/dev/pfnotify │ │ FCI commands
|
||||
PF state poll │ CMM ├──────────────► cdx.ko
|
||||
route socket │ │
|
||||
PF_KEY socket │ event loop │◄────────────── FCI events
|
||||
/dev/autobridge│ (kqueue) │ (flow timeout)
|
||||
ctrl socket │ │
|
||||
└──────────────┘
|
||||
```
|
||||
|
||||
**Flow lifecycle**:
|
||||
1. PF creates state → pfnotify READY → CMM resolves route + neighbor
|
||||
2. CMM sends route + conntrack to CDX via FCI → flow in hardware
|
||||
3. Packets forwarded at line rate by FMan, bypassing stack
|
||||
4. CDX timer expires or TCP FIN → FCI event → CMM clears offload flag
|
||||
5. If PF state still alive → next poll re-offloads automatically
|
||||
|
||||
Source: `fastpath/cmm/`
|
||||
|
||||
### cmmctl — CMM Control Tool (userspace)
|
||||
|
||||
CLI tool to query CMM state (offloaded connections, routes, stats)
|
||||
via a unix domain socket.
|
||||
|
||||
Source: `fastpath/cmmctl/`
|
||||
|
||||
### dpa_app — PCD Configuration Tool (userspace)
|
||||
|
||||
Loads FMan Parser/Classification/Distribution configuration from
|
||||
XML files via the FMan chardev. Sets up the KeyGen hash schemes
|
||||
and CC tree that CDX later patches with per-flow entries.
|
||||
|
||||
Depends on: fmlib (FMan API), fmc (XML parser/compiler)
|
||||
|
||||
Source: `fastpath/dpa_app/`, `fastpath/fmc/`, `fastpath/fmlib/`
|
||||
|
||||
## Hardware Drivers
|
||||
|
||||
### emc2302.ko — Fan Controller
|
||||
|
||||
I2C driver for EMC2302 fan controller. Exposes sysctl interface
|
||||
for fan RPM reading and PWM duty cycle control.
|
||||
|
||||
Source: `drivers/emc2302/`
|
||||
|
||||
### ina2xx.ko — Power Monitor
|
||||
|
||||
I2C driver for INA226/INA219 current/voltage/power monitors.
|
||||
Exposes sysctl interface for voltage, current, and power readings.
|
||||
|
||||
Source: `drivers/ina2xx/`
|
||||
|
||||
### lp5812.ko — RGB LED Controller
|
||||
|
||||
I2C driver for TI LP5812 LED controller. Drives the board's
|
||||
RGB status LEDs.
|
||||
|
||||
Source: `drivers/lp5812/`
|
||||
|
||||
### sfpled.ko — SFP+ Link LEDs
|
||||
|
||||
GPIO-based LED driver that reflects SFP+ link state on
|
||||
front-panel LEDs by polling dtsec link status.
|
||||
|
||||
Source: `drivers/sfp-led/`
|
||||
|
||||
### fand — Fan Control Daemon (userspace)
|
||||
|
||||
Reads temperature sensors and adjusts fan speed via emc2302
|
||||
sysctl interface. Runs as a daemon.
|
||||
|
||||
Source: `drivers/fand/`
|
||||
|
||||
## Data Flow: Packet Through Hardware Offload
|
||||
|
||||
```
|
||||
Wire ──► FMan RX Port
|
||||
│
|
||||
▼
|
||||
Parser (extract L3/L4 headers)
|
||||
│
|
||||
▼
|
||||
KeyGen (hash IP src/dst + L4 ports)
|
||||
│
|
||||
▼
|
||||
CC Hash Table lookup
|
||||
╱ ╲
|
||||
HIT MISS
|
||||
│ │
|
||||
▼ ▼
|
||||
CDX opcodes Default RX FQID
|
||||
(NAT rewrite, (FreeBSD stack)
|
||||
TTL dec, │
|
||||
MAC rewrite) ▼
|
||||
│ PF firewall
|
||||
▼ routing
|
||||
FMan TX Port if_output
|
||||
│ │
|
||||
▼ ▼
|
||||
Wire Wire
|
||||
(line rate) (software path)
|
||||
```
|
||||
|
||||
## IPC Summary
|
||||
|
||||
| Path | Mechanism | Purpose |
|
||||
|------|-----------|---------|
|
||||
| CMM → CDX | FCI ioctl | Register/deregister routes, conntrack, L2 flows |
|
||||
| CDX → CMM | FCI ring + kqueue | Flow timeout, TCP FIN notifications |
|
||||
| PF → CMM | /dev/pfnotify | State insert/ready/delete events |
|
||||
| CMM ← PF | DIOCGETSTATESV2 ioctl | Periodic state table poll (fallback) |
|
||||
| CMM ← routing | PF_ROUTE socket | Route/neighbor/interface changes |
|
||||
| CMM ← IPsec | PF_KEY socket | SA add/delete events |
|
||||
| CMM ← bridge | /dev/autobridge | L2 flow learning events |
|
||||
| cmmctl → CMM | Unix socket | Query/control commands |
|
||||
| dpa_app → FMan | /dev/fman chardev | PCD configuration (XML → hardware) |
|
||||
156
Makefile
Normal file
156
Makefile
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
# Top-level build Makefile for opnsense-deps
|
||||
#
|
||||
# Build everything:
|
||||
# make -C /build/opnsense-deps all
|
||||
#
|
||||
# Clean all build artifacts:
|
||||
# make -C /build/opnsense-deps clean
|
||||
#
|
||||
# Build subsets:
|
||||
# make -C /build/opnsense-deps modules # kernel modules only
|
||||
# make -C /build/opnsense-deps userspace # userspace only
|
||||
#
|
||||
# Parallel build (modules build concurrently):
|
||||
# make -C /build/opnsense-deps -j24 all
|
||||
#
|
||||
# Copyright 2026 Mono Technologies Inc.
|
||||
# SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
OPSDIR= ${.CURDIR}
|
||||
FASTPATH= ${OPSDIR}/fastpath
|
||||
DRIVERS= ${OPSDIR}/drivers
|
||||
|
||||
# Cross-compilation settings (all ?= for easy override)
|
||||
SRCTOP?= /build
|
||||
SYSROOT?= /usr/obj${SRCTOP}/opnsense-src/arm64.aarch64/tmp
|
||||
KERNBUILDDIR?= /usr/obj${SRCTOP}/opnsense-src/arm64.aarch64/sys/GATEWAY
|
||||
|
||||
# Common args for all kernel module builds
|
||||
# Host clang cross-compiles with explicit --target (bsd.kmod.mk does NOT add it)
|
||||
KMOD_ARGS= KERNBUILDDIR=${KERNBUILDDIR} \
|
||||
MACHINE=arm64 MACHINE_ARCH=aarch64 \
|
||||
CC="cc --target=aarch64-unknown-freebsd14.3" LD=ld.lld
|
||||
|
||||
# Local build directory for userspace components
|
||||
BUILDDIR?= ${OPSDIR}/_build
|
||||
|
||||
# Output directory for all final artifacts
|
||||
DISTDIR?= ${OPSDIR}/dist
|
||||
|
||||
# ============================================================
|
||||
# Top-level targets
|
||||
# ============================================================
|
||||
|
||||
all: dist
|
||||
|
||||
# ============================================================
|
||||
# Kernel modules (all independent — safe for -j)
|
||||
# ============================================================
|
||||
|
||||
KMOD_CDX= ${FASTPATH}/cdx
|
||||
KMOD_FCI= ${FASTPATH}/fci
|
||||
KMOD_AB= ${FASTPATH}/auto_bridge
|
||||
KMOD_PN= ${FASTPATH}/pf_notify
|
||||
KMOD_EMC= ${DRIVERS}/emc2302
|
||||
KMOD_INA= ${DRIVERS}/ina2xx
|
||||
KMOD_LP= ${DRIVERS}/lp5812
|
||||
KMOD_SFP= ${DRIVERS}/sfp-led
|
||||
|
||||
build-cdx:
|
||||
${MAKE} -C ${KMOD_CDX} ${KMOD_ARGS}
|
||||
|
||||
build-fci:
|
||||
${MAKE} -C ${KMOD_FCI} ${KMOD_ARGS}
|
||||
|
||||
build-auto_bridge:
|
||||
${MAKE} -C ${KMOD_AB} ${KMOD_ARGS}
|
||||
|
||||
build-pf_notify:
|
||||
${MAKE} -C ${KMOD_PN} ${KMOD_ARGS}
|
||||
|
||||
build-emc2302:
|
||||
${MAKE} -C ${KMOD_EMC} ${KMOD_ARGS}
|
||||
|
||||
build-ina2xx:
|
||||
${MAKE} -C ${KMOD_INA} ${KMOD_ARGS}
|
||||
|
||||
build-lp5812:
|
||||
${MAKE} -C ${KMOD_LP} ${KMOD_ARGS}
|
||||
|
||||
build-sfpled:
|
||||
${MAKE} -C ${KMOD_SFP} ${KMOD_ARGS}
|
||||
|
||||
modules: build-cdx build-fci build-auto_bridge build-pf_notify \
|
||||
build-emc2302 build-ina2xx build-lp5812 build-sfpled
|
||||
|
||||
# ============================================================
|
||||
# Userspace components (dependency order: fmlib -> fmc -> dpa_app)
|
||||
# ============================================================
|
||||
|
||||
fmlib:
|
||||
@mkdir -p ${BUILDDIR}/fmlib
|
||||
cd ${BUILDDIR}/fmlib && ${MAKE} -f ${FASTPATH}/fmlib/Makefile.cross all
|
||||
|
||||
fmc: fmlib
|
||||
@mkdir -p ${BUILDDIR}/fmc
|
||||
cd ${BUILDDIR}/fmc && ${MAKE} -f ${FASTPATH}/fmc/Makefile.cross \
|
||||
FMLIB_LIB=${BUILDDIR}/fmlib all
|
||||
|
||||
dpa_app: fmc
|
||||
@mkdir -p ${BUILDDIR}/dpa_app
|
||||
cd ${BUILDDIR}/dpa_app && ${MAKE} -f ${FASTPATH}/dpa_app/Makefile.cross \
|
||||
FMLIB_LIB=${BUILDDIR}/fmlib FMC_LIB=${BUILDDIR}/fmc all
|
||||
|
||||
cmm:
|
||||
@mkdir -p ${BUILDDIR}/cmm
|
||||
cd ${BUILDDIR}/cmm && ${MAKE} -f ${FASTPATH}/cmm/Makefile.cross all
|
||||
|
||||
cmmctl:
|
||||
@mkdir -p ${BUILDDIR}/cmmctl
|
||||
cd ${BUILDDIR}/cmmctl && ${MAKE} -f ${FASTPATH}/cmmctl/Makefile.cross all
|
||||
|
||||
fand:
|
||||
@mkdir -p ${BUILDDIR}/fand
|
||||
cd ${BUILDDIR}/fand && ${MAKE} -f ${DRIVERS}/fand/Makefile all
|
||||
|
||||
userspace: fmlib fmc dpa_app cmm cmmctl fand
|
||||
|
||||
# ============================================================
|
||||
# Collect outputs into dist/
|
||||
# ============================================================
|
||||
|
||||
dist: modules userspace
|
||||
@mkdir -p ${DISTDIR}
|
||||
cp ${KMOD_CDX}/cdx.ko ${DISTDIR}/
|
||||
cp ${KMOD_FCI}/fci.ko ${DISTDIR}/
|
||||
cp ${KMOD_AB}/auto_bridge.ko ${DISTDIR}/
|
||||
cp ${KMOD_PN}/pf_notify.ko ${DISTDIR}/
|
||||
cp ${KMOD_EMC}/emc2302.ko ${DISTDIR}/
|
||||
cp ${KMOD_INA}/ina2xx.ko ${DISTDIR}/
|
||||
cp ${KMOD_LP}/lp5812.ko ${DISTDIR}/
|
||||
cp ${KMOD_SFP}/sfpled.ko ${DISTDIR}/
|
||||
cp ${BUILDDIR}/fmc/fmc ${DISTDIR}/
|
||||
cp ${BUILDDIR}/dpa_app/dpa_app ${DISTDIR}/
|
||||
cp ${BUILDDIR}/cmm/cmm ${DISTDIR}/
|
||||
cp ${BUILDDIR}/cmmctl/cmmctl ${DISTDIR}/
|
||||
cp ${BUILDDIR}/fand/fand ${DISTDIR}/
|
||||
|
||||
# ============================================================
|
||||
# Clean
|
||||
# ============================================================
|
||||
|
||||
clean:
|
||||
${MAKE} -C ${KMOD_CDX} ${KMOD_ARGS} clean
|
||||
${MAKE} -C ${KMOD_FCI} ${KMOD_ARGS} clean
|
||||
${MAKE} -C ${KMOD_AB} ${KMOD_ARGS} clean
|
||||
${MAKE} -C ${KMOD_PN} ${KMOD_ARGS} clean
|
||||
${MAKE} -C ${KMOD_EMC} ${KMOD_ARGS} clean
|
||||
${MAKE} -C ${KMOD_INA} ${KMOD_ARGS} clean
|
||||
${MAKE} -C ${KMOD_LP} ${KMOD_ARGS} clean
|
||||
${MAKE} -C ${KMOD_SFP} ${KMOD_ARGS} clean
|
||||
rm -rf ${BUILDDIR}
|
||||
|
||||
.PHONY: all modules userspace dist clean \
|
||||
build-cdx build-fci build-auto_bridge build-pf_notify \
|
||||
build-emc2302 build-ina2xx build-lp5812 build-sfpled \
|
||||
fmlib fmc dpa_app cmm cmmctl fand
|
||||
26956
OPNsense-26.1-aarch64-GATEWAY.patch
Normal file
26956
OPNsense-26.1-aarch64-GATEWAY.patch
Normal file
File diff suppressed because it is too large
Load diff
205
README.md
Normal file
205
README.md
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
# Mono Gateway custom components for OPNsense
|
||||
|
||||
Out-of-tree kernel modules, userspace daemons, and build tooling for the Mono
|
||||
Gateway OPNsense image. Contains the DPAA1 fast-path stack (CDX flow offload,
|
||||
FCI, CMM connection manager, PF notifier, auto-bridge), board-support drivers
|
||||
(LP5812 LED, EMC2302 fan, INA2xx power, SFP LED), FMan toolchain (fmlib, fmc,
|
||||
dpa_app), and helper scripts for kernel image creation and deployment.
|
||||
|
||||
## Build instructions
|
||||
|
||||
All commands run on the build server (amd64 FreeBSD).
|
||||
|
||||
The build server should run the same FreeBSD version that OPNsense is based on (e.g., FreeBSD 14.3
|
||||
for OPNsense 26.1). Mismatched versions can cause ABI issues in cross-compiled kernel modules.
|
||||
|
||||
### 1. Prerequisites
|
||||
|
||||
```
|
||||
sudo pkg install -y git aarch64-binutils qemu-user-static
|
||||
```
|
||||
|
||||
### 2. Set up `/build`
|
||||
|
||||
All build paths assume the source tree is at `/build`. This directory must contain
|
||||
`opnsense-build`, `opnsense-src`, and `opnsense-deps`.
|
||||
|
||||
**Local development** (source tree lives on the build server):
|
||||
|
||||
```
|
||||
# Clone into /build directly
|
||||
sudo mkdir /build && sudo chown $(whoami) /build
|
||||
cd /build
|
||||
git clone https://github.com/opnsense/tools.git opnsense-build
|
||||
git clone https://github.com/opnsense/src.git opnsense-src
|
||||
# opnsense-deps is this repository
|
||||
|
||||
# Copy the GATEWAY device config into the build system
|
||||
cp /build/opnsense-deps/config/GATEWAY.conf /build/opnsense-build/device/
|
||||
```
|
||||
|
||||
**Remote source via NFS** (source tree lives on another machine):
|
||||
|
||||
```
|
||||
# Mount the remote share, then expose it at /build via nullfs
|
||||
echo '/path/to/nfs/mount /mnt/remote nfs rw 0 0' | sudo tee -a /etc/fstab
|
||||
sudo mount /mnt/remote
|
||||
|
||||
sudo mkdir /build
|
||||
echo '/mnt/remote /build nullfs rw,late 0 0' | sudo tee -a /etc/fstab
|
||||
sudo mount /build
|
||||
|
||||
# Copy the GATEWAY device config into the build system
|
||||
cp /build/opnsense-deps/config/GATEWAY.conf /build/opnsense-build/device/
|
||||
```
|
||||
|
||||
Using nullfs (rather than a symlink) ensures `pwd` reports `/build/...` in build
|
||||
output, keeping kernel version strings and module paths clean.
|
||||
|
||||
### 3. Prefetch base and packages
|
||||
|
||||
Official OPNsense mirrors only publish amd64 sets. For aarch64, use walker.earth.
|
||||
Check https://opnsense-update.walker.earth/FreeBSD:14:aarch64/26.1/sets/ for available versions.
|
||||
|
||||
```
|
||||
VERSION=26.1.1 # use latest available on the mirror
|
||||
|
||||
sudo make -C /build/opnsense-build prefetch-base DEVICE=GATEWAY SETTINGS=26.1 TOOLSDIR=/build/opnsense-build MIRRORS=https://opnsense-update.walker.earth VERSION=$VERSION
|
||||
|
||||
# The build system expects a -GATEWAY suffix on the base set
|
||||
cd /usr/local/opnsense/build/26.1/aarch64/sets
|
||||
sudo mv base-${VERSION}-aarch64.txz base-${VERSION}-aarch64-GATEWAY.txz
|
||||
|
||||
sudo make -C /build/opnsense-build prefetch-packages DEVICE=GATEWAY SETTINGS=26.1 TOOLSDIR=/build/opnsense-build MIRRORS=https://opnsense-update.walker.earth VERSION=$VERSION
|
||||
```
|
||||
|
||||
This skips `make base`, `make ports`, `make core`, and `make plugins` (no cross-compilation needed).
|
||||
|
||||
**Important:** Do NOT run `make base` or `make xtools` locally. If an `xtools-*-aarch64.txz`
|
||||
set exists in the sets directory, remove it before building the image. The xtools overlay/restore
|
||||
mechanism (`setup_xtools`/`setup_xbase`) is broken for cross-builds and will leave amd64 binaries
|
||||
in the image.
|
||||
|
||||
### 4. Build kernel
|
||||
|
||||
```
|
||||
sudo make -C /build/opnsense-build kernel DEVICE=GATEWAY SETTINGS=26.1 TOOLSDIR=/build/opnsense-build SRCDIR=/build/opnsense-src
|
||||
```
|
||||
|
||||
The OPNsense build system defaults `SRCDIR` to `/usr/src`; since our source tree
|
||||
is named `opnsense-src`, the explicit `SRCDIR=` is required on all make targets
|
||||
that touch the kernel source (kernel, arm).
|
||||
|
||||
The kernel is cross-compiled natively (no QEMU). Package installation during `make arm` uses QEMU user-static emulation.
|
||||
|
||||
**Rebuilding:** The build system caches the kernel set in the sets directory and
|
||||
skips the build if one already exists. When rebuilding after source changes,
|
||||
remove the stale set and obj tree first:
|
||||
|
||||
```
|
||||
sudo rm -f /usr/local/opnsense/build/26.1/aarch64/sets/kernel-*-GATEWAY.txz
|
||||
sudo rm -rf /usr/obj/build/opnsense-src/arm64.aarch64/sys/GATEWAY
|
||||
```
|
||||
### 5. Install libxml2 into cross-compilation sysroot
|
||||
|
||||
Required by opnsense-deps (fmc links against libxml2).
|
||||
|
||||
```
|
||||
fetch -o /tmp/libxml2.pkg 'https://pkg.FreeBSD.org/FreeBSD:14:aarch64/latest/All/Hashed/libxml2-2.15.1_1~82f6f2bc79.pkg'
|
||||
mkdir -p /tmp/libxml2-extract && cd /tmp/libxml2-extract && tar xf /tmp/libxml2.pkg
|
||||
|
||||
SYSROOT=/usr/obj/build/opnsense-src/arm64.aarch64/tmp
|
||||
sudo mkdir -p ${SYSROOT}/usr/local/include ${SYSROOT}/usr/local/lib
|
||||
sudo cp -r /tmp/libxml2-extract/usr/local/include/libxml2 ${SYSROOT}/usr/local/include/
|
||||
sudo cp -a /tmp/libxml2-extract/usr/local/lib/libxml2* ${SYSROOT}/usr/local/lib/
|
||||
```
|
||||
|
||||
### 6. Build custom modules and tools
|
||||
|
||||
```
|
||||
make -C /build/opnsense-deps -j24 all
|
||||
```
|
||||
|
||||
### 7. Assemble eMMC image
|
||||
|
||||
```
|
||||
sudo make -C /build/opnsense-build arm-5G DEVICE=GATEWAY SETTINGS=26.1 TOOLSDIR=/build/opnsense-build SRCDIR=/build/opnsense-src
|
||||
```
|
||||
|
||||
Output: `/usr/local/opnsense/build/26.1/aarch64/images/OPNsense-*-arm-aarch64-GATEWAY.img`
|
||||
|
||||
## Development
|
||||
|
||||
The kernel source tree (`opnsense-src`) is patched from upstream OPNsense via
|
||||
`OPNsense-26.1-aarch64-GATEWAY.patch`. To work on kernel changes:
|
||||
|
||||
1. Clone a fresh `opnsense-src` and apply the patch:
|
||||
```
|
||||
git clone https://github.com/opnsense/src.git opnsense-src
|
||||
cd opnsense-src
|
||||
git checkout stable/26.1
|
||||
git apply ../opnsense-deps/OPNsense-26.1-aarch64-GATEWAY.patch
|
||||
git checkout -b mono-gateway
|
||||
git commit -am "Apply Mono Gateway patch"
|
||||
```
|
||||
|
||||
2. Make your changes, build, test, and commit as needed.
|
||||
|
||||
3. When done, regenerate the patch. Use `git format-patch` to produce a patch
|
||||
with a proper commit header (author, date, subject, diffstat):
|
||||
```
|
||||
git format-patch -1 mono-gateway --stdout > ../opnsense-deps/OPNsense-26.1-aarch64-GATEWAY.patch
|
||||
```
|
||||
This generates a patch from the single squashed commit on `mono-gateway`.
|
||||
If you have multiple commits on top of `stable/26.1`, squash them first:
|
||||
```
|
||||
git reset --soft stable/26.1
|
||||
git commit -m "Port Mono Gateway (NXP LS1046A) to OPNsense's FreeBSD 14.3"
|
||||
git format-patch -1 --stdout > ../opnsense-deps/OPNsense-26.1-aarch64-GATEWAY.patch
|
||||
```
|
||||
|
||||
## Local / Test Deployment
|
||||
|
||||
Boot the gateway into Recovery Linux. Assign an IP since Recovery Linux has no DHCP (adjust IPs to match your network):
|
||||
|
||||
```
|
||||
ip link set eth3 up
|
||||
ip addr add 10.0.0.69/24 dev eth3
|
||||
ip route add default via 10.0.0.1 dev eth3
|
||||
```
|
||||
|
||||
On your build server, serve the image directory over HTTP:
|
||||
|
||||
```
|
||||
python3 -m http.server
|
||||
```
|
||||
|
||||
Then on the gateway, fetch and flash in one go (the image is too large to store locally first):
|
||||
|
||||
```
|
||||
curl http://10.0.0.70:8000/OPNsense-GATEWAY.img | dd of=/dev/mmcblk0 bs=1M
|
||||
```
|
||||
|
||||
**Warning:** This overwrites the entire eMMC, including any firmware stored there.
|
||||
|
||||
## Booting into OPNsense
|
||||
|
||||
The board's default boot command loads Recovery Linux. To boot OPNsense instead,
|
||||
interrupt U-Boot's autoboot (press any key during the countdown) and configure
|
||||
a persistent `opnsense` boot environment:
|
||||
|
||||
```
|
||||
=> setenv opnsense "load mmc 0:1 ${kernel_addr_r} kernel.img; load mmc 0:1 ${fdt_addr_r} dtb/mono-gateway-dk.dtb; booti 0x82000000 - 0x88000000"
|
||||
=> setenv bootcmd_bak ${bootcmd}
|
||||
=> setenv bootcmd "run opnsense || run recovery"
|
||||
=> saveenv
|
||||
=> reset
|
||||
```
|
||||
|
||||
This saves the original boot command as `bootcmd_bak`, then sets `bootcmd` to
|
||||
try OPNsense first and fall back to Recovery Linux if the kernel image is missing.
|
||||
After `saveenv` and `reset`, the board will automatically boot into OPNsense on
|
||||
every subsequent power-on.
|
||||
|
||||
> ## Disclaimer
|
||||
> Many of the files in this repository, including the large kernel patch, were ported from Linux sources provided by NXP (originally targeting Linux kernel 5.4). Due to the sheer scope of the porting effort, Claude Opus 4.6 was used to assist with the adaptation of this code to FreeBSD. As such, this project should be considered **highly experimental** and is **not fit for production use** until thorough reviews and testing have been performed by the wider community.
|
||||
114
config/GATEWAY.conf
Normal file
114
config/GATEWAY.conf
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
# Mono Gateway Development Kit (NXP LS1046A)
|
||||
# https://mono.vision
|
||||
#
|
||||
# Boot: U-Boot booti (NOT EFI -- loader.efi broken on this SoC)
|
||||
# Console: serial 115200 (headless, no video output)
|
||||
# U-Boot lives on QSPI NOR -- not installed from SD card
|
||||
|
||||
export PRODUCT_KERNEL=GATEWAY
|
||||
export PRODUCT_TARGET=arm64
|
||||
export PRODUCT_ARCH=aarch64
|
||||
export PRODUCT_WANTS_CROSS="aarch64-binutils qemu-user-static"
|
||||
export PRODUCT_COMSPEED=115200
|
||||
export HOSTNAME=mono-gateway
|
||||
export ARM_FAT_SIZE="80m -b 32m"
|
||||
# No PRODUCT_UEFI -- we boot via U-Boot booti, not EFI
|
||||
|
||||
# Artifacts directory (custom board modules, tools, configs)
|
||||
MONO_GW_DEPS="${MONO_GW_DEPS:-${TOOLSDIR%/*}/opnsense-deps/dist}"
|
||||
|
||||
arm_install_uboot()
|
||||
{
|
||||
# U-Boot is on QSPI NOR, not SD card. We just need kernel.img
|
||||
# and DTB on the FAT boot partition for U-Boot to load via booti.
|
||||
|
||||
local KERNEL_ELF="${STAGEDIR}/boot/kernel/kernel"
|
||||
|
||||
# Convert ELF kernel to ARM64 Image format for U-Boot booti
|
||||
sh "${TOOLSDIR%/*}/opnsense-deps/mk-kernel-img.sh" \
|
||||
"${KERNEL_ELF}" "${STAGEDIR}/boot/msdos/kernel.img"
|
||||
|
||||
# Copy device tree binary
|
||||
mkdir -p ${STAGEDIR}/boot/msdos/dtb
|
||||
cp ${STAGEDIR}/boot/dtb/freescale/mono-gateway-dk.dtb \
|
||||
${STAGEDIR}/boot/msdos/dtb/
|
||||
|
||||
# rc.conf.local must be written here (not in arm_hook) because
|
||||
# setup_xbase() deletes rc.conf.local after arm_hook runs.
|
||||
local MONO_GW_RCD="${TOOLSDIR%/*}/opnsense-deps/rc.d"
|
||||
if [ -f "${MONO_GW_RCD}/rc.conf.local" ]; then
|
||||
cat "${MONO_GW_RCD}/rc.conf.local" >> \
|
||||
"${STAGEDIR}/etc/rc.conf.local"
|
||||
fi
|
||||
}
|
||||
|
||||
arm_hook()
|
||||
{
|
||||
local STAGEDIR=${1}
|
||||
|
||||
# The generic arm_hook (extras.conf) already ran and set
|
||||
# primaryconsole=video. Fix it: this board has no video.
|
||||
sed -i '' \
|
||||
's|<primaryconsole>video</primaryconsole>|<primaryconsole>serial</primaryconsole>|' \
|
||||
${STAGEDIR}${CONFIG_XML}
|
||||
sed -i '' '/<secondaryconsole>/d' ${STAGEDIR}${CONFIG_XML}
|
||||
|
||||
# Headless serial only (generic set -D for dual console)
|
||||
echo "-S${PRODUCT_COMSPEED} -h" > ${STAGEDIR}/boot.config
|
||||
|
||||
# Serial console: auto-detect terminal size on login
|
||||
echo "resizewin" >> ${STAGEDIR}/root/.profile
|
||||
|
||||
# --- Custom kernel modules ---
|
||||
for mod in cdx auto_bridge pf_notify fci sfpled lp5812 \
|
||||
emc2302 ina2xx; do
|
||||
if [ -f "${MONO_GW_DEPS}/${mod}.ko" ]; then
|
||||
cp "${MONO_GW_DEPS}/${mod}.ko" \
|
||||
"${STAGEDIR}/boot/modules/"
|
||||
fi
|
||||
done
|
||||
|
||||
# --- Userspace tools ---
|
||||
for bin in cmm cmmctl dpa_app fmc fand; do
|
||||
if [ -f "${MONO_GW_DEPS}/${bin}" ]; then
|
||||
cp "${MONO_GW_DEPS}/${bin}" \
|
||||
"${STAGEDIR}/usr/local/sbin/"
|
||||
chmod 755 "${STAGEDIR}/usr/local/sbin/${bin}"
|
||||
fi
|
||||
done
|
||||
|
||||
# --- PCD/CDX configuration ---
|
||||
local MONO_GW_CONFIG="${TOOLSDIR%/*}/opnsense-deps/config"
|
||||
if [ -d "${MONO_GW_CONFIG}" ]; then
|
||||
cp "${MONO_GW_CONFIG}/cdx_cfg.xml" "${STAGEDIR}/etc/"
|
||||
cp "${MONO_GW_CONFIG}/cdx_pcd.xml" "${STAGEDIR}/etc/"
|
||||
cp "${MONO_GW_CONFIG}/cdx_sp.xml" "${STAGEDIR}/etc/"
|
||||
mkdir -p "${STAGEDIR}/etc/fmc/config"
|
||||
cp "${MONO_GW_CONFIG}/hxs_pdl_v3.xml" \
|
||||
"${STAGEDIR}/etc/fmc/config/"
|
||||
fi
|
||||
|
||||
# --- rc.d boot scripts ---
|
||||
local MONO_GW_RCD="${TOOLSDIR%/*}/opnsense-deps/rc.d"
|
||||
mkdir -p "${STAGEDIR}/usr/local/etc/rc.d"
|
||||
for script in cmm fand; do
|
||||
if [ -f "${MONO_GW_RCD}/${script}" ]; then
|
||||
cp "${MONO_GW_RCD}/${script}" \
|
||||
"${STAGEDIR}/usr/local/etc/rc.d/"
|
||||
chmod 755 \
|
||||
"${STAGEDIR}/usr/local/etc/rc.d/${script}"
|
||||
fi
|
||||
done
|
||||
|
||||
# --- rc.syshook early script (kernel module loading) ---
|
||||
# OPNsense ignores FreeBSD's kld rc.d service and rcorder
|
||||
# BEFORE:NETWORKING. Use rc.syshook.d/early/ to load modules
|
||||
# before interfaces come up (cdx calls dpa_app internally).
|
||||
mkdir -p "${STAGEDIR}/usr/local/etc/rc.syshook.d/early"
|
||||
if [ -f "${MONO_GW_RCD}/02-mono-modules" ]; then
|
||||
cp "${MONO_GW_RCD}/02-mono-modules" \
|
||||
"${STAGEDIR}/usr/local/etc/rc.syshook.d/early/"
|
||||
chmod 755 \
|
||||
"${STAGEDIR}/usr/local/etc/rc.syshook.d/early/02-mono-modules"
|
||||
fi
|
||||
}
|
||||
18
config/cdx_cfg.xml
Normal file
18
config/cdx_cfg.xml
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<cfgdata>
|
||||
<config>
|
||||
<!-- Mono Gateway DK port configuration -->
|
||||
<engine name="fm0">
|
||||
<!-- 1G Ethernet ports -->
|
||||
<port type="1G" number="1" policy="cdx_ethport_1_policy" portid="1"/>
|
||||
<port type="1G" number="4" policy="cdx_ethport_4_policy" portid="4"/>
|
||||
<port type="1G" number="5" policy="cdx_ethport_5_policy" portid="5"/>
|
||||
<!-- 10G Ethernet ports -->
|
||||
<port type="10G" number="0" policy="cdx_ethport_6_policy" portid="6"/>
|
||||
<port type="10G" number="1" policy="cdx_ethport_7_policy" portid="7"/>
|
||||
<!-- OFFLINE ports for CDX -->
|
||||
<!-- number="1" -> dpa-fman0-oh@2 (IPsec), number="2" -> dpa-fman0-oh@3 (WiFi) -->
|
||||
<port type="OFFLINE" number="1" policy="cdx_port_of2_policy" portid="8"/>
|
||||
<port type="OFFLINE" number="2" policy="cdx_port_of3_policy" portid="9"/>
|
||||
</engine>
|
||||
</config>
|
||||
</cfgdata>
|
||||
525
config/cdx_pcd.xml
Normal file
525
config/cdx_pcd.xml
Normal file
|
|
@ -0,0 +1,525 @@
|
|||
<!--NOTE: In soft parser code, pppoe table base is calculated and added as ccbase +0x30 -->
|
||||
<!-- this pppoe relay table is allocated dynamically by FMC while pushing tables from cdx_pcd.xml -->
|
||||
<!-- If any new table gets added ccbase value of ppoe relay table should be checked and updated accordingly -->
|
||||
<netpcd>
|
||||
<classification name="cdx_udp4_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0x7fff" hashshift="0" keysize="14" aging="yes"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_tcp4_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0x7fff" hashshift="0" keysize="14" aging="yes"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_udp6_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0x7fff" hashshift="0" keysize="38" aging="yes"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_tcp6_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0x7fff" hashshift="0" keysize="38" aging="yes"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_esp4_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0xff" hashshift="0" keysize="10" aging="yes"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_esp6_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0xff" hashshift="0" keysize="22" aging="yes"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_multicast4_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0xff" hashshift="0" keysize="10" aging="no"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_multicast6_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0xff" hashshift="0" keysize="34" aging="no"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_ethernet_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0xff" hashshift="0" keysize="15" aging="yes"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_pppoe_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0xf" hashshift="0" keysize="11" aging="yes"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_tuple3udp4_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0xff" hashshift="0" keysize="8" aging="yes"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_tuple3tcp4_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0xf" hashshift="0" keysize="8" aging="yes"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_tuple3udp6_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0xff" hashshift="0" keysize="20" aging="yes"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_tuple3tcp6_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0xf" hashshift="0" keysize="20" aging="yes"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_frag4_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0xf" hashshift="0" keysize="12" aging="no"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<classification name="cdx_frag6_cc" max="512" masks="yes" shared="true" statistics="byteframe">
|
||||
<key>
|
||||
<hashtable external="yes" mask="0xf" hashshift="0" keysize="38" aging="no"/>
|
||||
</key>
|
||||
</classification>
|
||||
|
||||
<distribution name="cdx_udp4_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="udp"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ipv4.src" header_index="last"/>
|
||||
<fieldref name="ipv4.dst" header_index="last"/>
|
||||
<fieldref name="ipv4.nextp" header_index="last"/>
|
||||
<fieldref name="udp.sport"/>
|
||||
<fieldref name="udp.dport"/>
|
||||
</key>
|
||||
<queue count="1" base="0x1000"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<action type="classification" name="cdx_udp4_cc"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_tcp4_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="tcp"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ipv4.src" header_index="last"/>
|
||||
<fieldref name="ipv4.dst" header_index="last"/>
|
||||
<fieldref name="ipv4.nextp" header_index="last"/>
|
||||
<fieldref name="tcp.sport"/>
|
||||
<fieldref name="tcp.dport"/>
|
||||
</key>
|
||||
<queue count="1" base="0x1010"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<action type="classification" name="cdx_tcp4_cc"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_udp6_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="udp"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ipv6.src" header_index="last"/>
|
||||
<fieldref name="ipv6.dst" header_index="last"/>
|
||||
<fieldref name="ipv6.nexthdr" header_index="last"/>
|
||||
<fieldref name="udp.sport"/>
|
||||
<fieldref name="udp.dport"/>
|
||||
</key>
|
||||
<queue count="1" base="0x1020"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<action type="classification" name="cdx_udp6_cc"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_tcp6_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="tcp"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ipv6.src" header_index="last"/>
|
||||
<fieldref name="ipv6.dst" header_index="last"/>
|
||||
<fieldref name="ipv6.nexthdr" header_index="last"/>
|
||||
<fieldref name="tcp.sport"/>
|
||||
<fieldref name="tcp.dport"/>
|
||||
</key>
|
||||
<queue count="1" base="0x1030"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<action type="classification" name="cdx_tcp6_cc"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_ipv4multicast_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="ipv4"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ipv4.src"/>
|
||||
<fieldref name="ipv4.dst"/>
|
||||
<fieldref name="ipv4.nextp"/>
|
||||
</key>
|
||||
<queue count="1" base="0x1040"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<action type="classification" name="cdx_multicast4_cc"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_ipv6multicast_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="ipv6"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ipv6.src"/>
|
||||
<fieldref name="ipv6.dst"/>
|
||||
<fieldref name="ipv6.nexthdr"/>
|
||||
</key>
|
||||
<queue count="1" base="0x1050"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<action type="classification" name="cdx_multicast6_cc"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_pppoe_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="pppoe"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ethernet.src"/>
|
||||
<fieldref name="ethernet.type"/>
|
||||
<fieldref name="pppoe.session_ID"/>
|
||||
</key>
|
||||
<action type="classification" name="cdx_pppoe_cc"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<queue count="1" base="0x1080"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_ethernet_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="ethernet"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ethernet.dst"/>
|
||||
<fieldref name="ethernet.src"/>
|
||||
<fieldref name="ethernet.type"/>
|
||||
</key>
|
||||
<action type="classification" name="cdx_ethernet_cc"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<queue count="128" base="0x10000"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_esp4_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="ipv4"/>
|
||||
<protocolref name="ipsec_esp"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ipv4.dst"/>
|
||||
<fieldref name="ipv4.nextp"/>
|
||||
<fieldref name="ipsec_esp.spi"/>
|
||||
</key>
|
||||
<queue count="1" base="0x1060"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<action type="classification" name="cdx_esp4_cc"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_esp6_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="ipv6"/>
|
||||
<protocolref name="ipsec_esp"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ipv6.dst"/>
|
||||
<fieldref name="ipv6.nexthdr"/>
|
||||
<fieldref name="ipsec_esp.spi"/>
|
||||
</key>
|
||||
<queue count="1" base="0x1070"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<action type="classification" name="cdx_esp6_cc"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_tup3udp4_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="udp"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ipv4.dst" header_index="last"/>
|
||||
<fieldref name="ipv4.nextp" header_index="last"/>
|
||||
<fieldref name="udp.dport"/>
|
||||
</key>
|
||||
<queue count="1" base="0x1090"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<action type="classification" name="cdx_tuple3udp4_cc"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_tup3tcp4_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="tcp"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ipv4.dst" header_index="last"/>
|
||||
<fieldref name="ipv4.nextp" header_index="last"/>
|
||||
<fieldref name="tcp.dport"/>
|
||||
</key>
|
||||
<queue count="1" base="0x10a0"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<action type="classification" name="cdx_tuple3tcp4_cc"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_tup3udp6_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="udp"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ipv6.dst" header_index="last"/>
|
||||
<fieldref name="ipv6.nexthdr" header_index="last"/>
|
||||
<fieldref name="udp.dport"/>
|
||||
</key>
|
||||
<queue count="1" base="0x10b0"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<action type="classification" name="cdx_tuple3udp6_cc"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_tup3tcp6_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="tcp"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ipv6.dst" header_index="last"/>
|
||||
<fieldref name="ipv6.nexthdr" header_index="last"/>
|
||||
<fieldref name="tcp.dport"/>
|
||||
</key>
|
||||
<queue count="1" base="0x10c0"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<action type="classification" name="cdx_tuple3tcp6_cc"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_ipv4frag_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="ipv4"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ipv4.src"/>
|
||||
<fieldref name="ipv4.dst"/>
|
||||
<fieldref name="ipv4.nextp"/>
|
||||
</key>
|
||||
<queue count="1" base="0x10d0"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<action type="classification" name="cdx_frag4_cc"/>
|
||||
</distribution>
|
||||
|
||||
<distribution name="cdx_ipv6frag_dist" shared="true">
|
||||
<protocols>
|
||||
<protocolref name="ipv6"/>
|
||||
</protocols>
|
||||
<key>
|
||||
<fieldref name="ipv6.src"/>
|
||||
<fieldref name="ipv6.dst"/>
|
||||
<fieldref name="ipv6.nexthdr"/>
|
||||
</key>
|
||||
<queue count="1" base="0x10e0"/>
|
||||
<combine portid="true" offset="16" mask="0xF"/>
|
||||
<action type="classification" name="cdx_frag6_cc"/>
|
||||
</distribution>
|
||||
|
||||
<policy name="cdx_ethport_0_policy">
|
||||
<dist_order>
|
||||
<distributionref name="cdx_esp4_dist"/>
|
||||
<distributionref name="cdx_esp6_dist"/>
|
||||
<distributionref name="cdx_udp4_dist"/>
|
||||
<distributionref name="cdx_tcp4_dist"/>
|
||||
<distributionref name="cdx_udp6_dist"/>
|
||||
<distributionref name="cdx_tcp6_dist"/>
|
||||
<distributionref name="cdx_ipv4multicast_dist"/>
|
||||
<distributionref name="cdx_ipv6multicast_dist"/>
|
||||
<distributionref name="cdx_tup3udp4_dist"/>
|
||||
<distributionref name="cdx_tup3udp6_dist"/>
|
||||
<distributionref name="cdx_pppoe_dist"/>
|
||||
<distributionref name="cdx_ethernet_dist"/>
|
||||
<!-- distributionref name="cdx_ipv4frag_dist"/>
|
||||
<distributionref name="cdx_ipv6frag_dist"/ -->
|
||||
</dist_order>
|
||||
</policy>
|
||||
|
||||
<policy name="cdx_ethport_1_policy">
|
||||
<dist_order>
|
||||
<distributionref name="cdx_esp4_dist"/>
|
||||
<distributionref name="cdx_esp6_dist"/>
|
||||
<distributionref name="cdx_udp4_dist"/>
|
||||
<distributionref name="cdx_tcp4_dist"/>
|
||||
<distributionref name="cdx_udp6_dist"/>
|
||||
<distributionref name="cdx_tcp6_dist"/>
|
||||
<distributionref name="cdx_ipv4multicast_dist"/>
|
||||
<distributionref name="cdx_ipv6multicast_dist"/>
|
||||
<distributionref name="cdx_tup3udp4_dist"/>
|
||||
<distributionref name="cdx_tup3udp6_dist"/>
|
||||
<distributionref name="cdx_pppoe_dist"/>
|
||||
<distributionref name="cdx_ethernet_dist"/>
|
||||
<!-- distributionref name="cdx_ipv4frag_dist"/>
|
||||
<distributionref name="cdx_ipv6frag_dist"/-->
|
||||
</dist_order>
|
||||
</policy>
|
||||
|
||||
<policy name="cdx_ethport_2_policy">
|
||||
<dist_order>
|
||||
<distributionref name="cdx_esp4_dist"/>
|
||||
<distributionref name="cdx_esp6_dist"/>
|
||||
<distributionref name="cdx_udp4_dist"/>
|
||||
<distributionref name="cdx_tcp4_dist"/>
|
||||
<distributionref name="cdx_udp6_dist"/>
|
||||
<distributionref name="cdx_tcp6_dist"/>
|
||||
<distributionref name="cdx_ipv4multicast_dist"/>
|
||||
<distributionref name="cdx_ipv6multicast_dist"/>
|
||||
<distributionref name="cdx_tup3udp4_dist"/>
|
||||
<distributionref name="cdx_tup3udp6_dist"/>
|
||||
<distributionref name="cdx_pppoe_dist"/>
|
||||
<distributionref name="cdx_ethernet_dist"/>
|
||||
<!-- distributionref name="cdx_ipv4frag_dist"/>
|
||||
<distributionref name="cdx_ipv6frag_dist"/ -->
|
||||
</dist_order>
|
||||
</policy>
|
||||
|
||||
<policy name="cdx_ethport_3_policy">
|
||||
<dist_order>
|
||||
<distributionref name="cdx_esp4_dist"/>
|
||||
<distributionref name="cdx_esp6_dist"/>
|
||||
<distributionref name="cdx_udp4_dist"/>
|
||||
<distributionref name="cdx_tcp4_dist"/>
|
||||
<distributionref name="cdx_udp6_dist"/>
|
||||
<distributionref name="cdx_tcp6_dist"/>
|
||||
<distributionref name="cdx_ipv4multicast_dist"/>
|
||||
<distributionref name="cdx_ipv6multicast_dist"/>
|
||||
<distributionref name="cdx_tup3udp4_dist"/>
|
||||
<distributionref name="cdx_tup3udp6_dist"/>
|
||||
<distributionref name="cdx_pppoe_dist"/>
|
||||
<distributionref name="cdx_ethernet_dist"/>
|
||||
<!-- distributionref name="cdx_ipv4frag_dist"/>
|
||||
<distributionref name="cdx_ipv6frag_dist"/ -->
|
||||
</dist_order>
|
||||
</policy>
|
||||
|
||||
<policy name="cdx_ethport_4_policy">
|
||||
<dist_order>
|
||||
<distributionref name="cdx_esp4_dist"/>
|
||||
<distributionref name="cdx_esp6_dist"/>
|
||||
<distributionref name="cdx_udp4_dist"/>
|
||||
<distributionref name="cdx_tcp4_dist"/>
|
||||
<distributionref name="cdx_udp6_dist"/>
|
||||
<distributionref name="cdx_tcp6_dist"/>
|
||||
<distributionref name="cdx_ipv4multicast_dist"/>
|
||||
<distributionref name="cdx_ipv6multicast_dist"/>
|
||||
<distributionref name="cdx_tup3udp4_dist"/>
|
||||
<distributionref name="cdx_tup3udp6_dist"/>
|
||||
<distributionref name="cdx_pppoe_dist"/>
|
||||
<distributionref name="cdx_ethernet_dist"/>
|
||||
<!-- distributionref name="cdx_ipv4frag_dist"/>
|
||||
<distributionref name="cdx_ipv6frag_dist"/-->
|
||||
</dist_order>
|
||||
</policy>
|
||||
|
||||
<policy name="cdx_ethport_5_policy">
|
||||
<dist_order>
|
||||
<distributionref name="cdx_esp4_dist"/>
|
||||
<distributionref name="cdx_esp6_dist"/>
|
||||
<distributionref name="cdx_udp4_dist"/>
|
||||
<distributionref name="cdx_tcp4_dist"/>
|
||||
<distributionref name="cdx_udp6_dist"/>
|
||||
<distributionref name="cdx_tcp6_dist"/>
|
||||
<distributionref name="cdx_ipv4multicast_dist"/>
|
||||
<distributionref name="cdx_ipv6multicast_dist"/>
|
||||
<distributionref name="cdx_tup3udp4_dist"/>
|
||||
<distributionref name="cdx_tup3udp6_dist"/>
|
||||
<distributionref name="cdx_pppoe_dist"/>
|
||||
<distributionref name="cdx_ethernet_dist"/>
|
||||
<!-- distributionref name="cdx_ipv4frag_dist"/>
|
||||
<distributionref name="cdx_ipv6frag_dist"/ -->
|
||||
</dist_order>
|
||||
</policy>
|
||||
|
||||
<policy name="cdx_ethport_6_policy">
|
||||
<dist_order>
|
||||
<distributionref name="cdx_esp4_dist"/>
|
||||
<distributionref name="cdx_esp6_dist"/>
|
||||
<distributionref name="cdx_udp4_dist"/>
|
||||
<distributionref name="cdx_tcp4_dist"/>
|
||||
<distributionref name="cdx_udp6_dist"/>
|
||||
<distributionref name="cdx_tcp6_dist"/>
|
||||
<distributionref name="cdx_ipv4multicast_dist"/>
|
||||
<distributionref name="cdx_ipv6multicast_dist"/>
|
||||
<distributionref name="cdx_tup3udp4_dist"/>
|
||||
<distributionref name="cdx_tup3udp6_dist"/>
|
||||
<distributionref name="cdx_pppoe_dist"/>
|
||||
<distributionref name="cdx_ethernet_dist"/>
|
||||
<!-- distributionref name="cdx_ipv4frag_dist"/>
|
||||
<distributionref name="cdx_ipv6frag_dist"/ -->
|
||||
</dist_order>
|
||||
</policy>
|
||||
|
||||
<policy name="cdx_ethport_7_policy">
|
||||
<dist_order>
|
||||
<distributionref name="cdx_esp4_dist"/>
|
||||
<distributionref name="cdx_esp6_dist"/>
|
||||
<distributionref name="cdx_udp4_dist"/>
|
||||
<distributionref name="cdx_tcp4_dist"/>
|
||||
<distributionref name="cdx_udp6_dist"/>
|
||||
<distributionref name="cdx_tcp6_dist"/>
|
||||
<distributionref name="cdx_ipv4multicast_dist"/>
|
||||
<distributionref name="cdx_ipv6multicast_dist"/>
|
||||
<distributionref name="cdx_tup3udp4_dist"/>
|
||||
<distributionref name="cdx_tup3udp6_dist"/>
|
||||
<distributionref name="cdx_pppoe_dist"/>
|
||||
<distributionref name="cdx_ethernet_dist"/>
|
||||
<!-- distributionref name="cdx_ipv4frag_dist"/>
|
||||
<distributionref name="cdx_ipv6frag_dist"/ -->
|
||||
</dist_order>
|
||||
</policy>
|
||||
|
||||
|
||||
<policy name="cdx_port_of2_policy">
|
||||
<dist_order>
|
||||
<distributionref name="cdx_esp4_dist"/>
|
||||
<distributionref name="cdx_esp6_dist"/>
|
||||
<distributionref name="cdx_udp4_dist"/>
|
||||
<distributionref name="cdx_tcp4_dist"/>
|
||||
<distributionref name="cdx_udp6_dist"/>
|
||||
<distributionref name="cdx_tcp6_dist"/>
|
||||
<distributionref name="cdx_ipv4multicast_dist"/>
|
||||
<distributionref name="cdx_ipv6multicast_dist"/>
|
||||
<distributionref name="cdx_tup3udp4_dist"/>
|
||||
<distributionref name="cdx_tup3udp6_dist"/>
|
||||
<distributionref name="cdx_pppoe_dist"/>
|
||||
<distributionref name="cdx_ethernet_dist"/>
|
||||
<!-- distributionref name="cdx_ipv4frag_dist"/>
|
||||
<distributionref name="cdx_ipv6frag_dist"/-->
|
||||
</dist_order>
|
||||
</policy>
|
||||
|
||||
<policy name="cdx_port_of3_policy">
|
||||
<dist_order>
|
||||
<distributionref name="cdx_esp4_dist"/>
|
||||
<distributionref name="cdx_esp6_dist"/>
|
||||
<distributionref name="cdx_udp4_dist"/>
|
||||
<distributionref name="cdx_tcp4_dist"/>
|
||||
<distributionref name="cdx_udp6_dist"/>
|
||||
<distributionref name="cdx_tcp6_dist"/>
|
||||
<distributionref name="cdx_ipv4multicast_dist"/>
|
||||
<distributionref name="cdx_ipv6multicast_dist"/>
|
||||
<distributionref name="cdx_tup3udp4_dist"/>
|
||||
<distributionref name="cdx_tup3udp6_dist"/>
|
||||
<distributionref name="cdx_pppoe_dist"/>
|
||||
<distributionref name="cdx_ethernet_dist"/>
|
||||
<!-- distributionref name="cdx_ipv4frag_dist"/>
|
||||
<distributionref name="cdx_ipv6frag_dist"/ -->
|
||||
</dist_order>
|
||||
</policy>
|
||||
</netpcd>
|
||||
194
config/cdx_sp.xml
Normal file
194
config/cdx_sp.xml
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
<netpdl name="NetPDL custom protocol" description="NetPDL custom protocol">
|
||||
<protocol name="pppoeschema" longname="pppoe handler" prevproto="pppoe">
|
||||
<execute-code>
|
||||
<before>
|
||||
<if expr="($FW[48:16]) == 0xc021">
|
||||
<if-false>
|
||||
<!-- PPPoE session packet, police the packet first -->
|
||||
<assign-variable name="$nia" value="0x4C0000"/>
|
||||
<!--NOTE: this ccbase +0x30 is corresponding pppoe relay table -->
|
||||
<!-- this pppoe relay table is allocated dynamically by FMC while pushing tables from cdx_pcd.xml -->
|
||||
<!-- If any new table gets added ccbase value of ppoe relay table should be checked and updated accordingly -->
|
||||
<assign-variable name="$ccbase" value="$ccbase + 0x30"/>
|
||||
<!--indicate we came into cc from pppoe schema-->
|
||||
<assign-variable name="$shimr" value="0x40"/>
|
||||
<action type="exit" confirm="yes" nextproto="end_parse"/>
|
||||
</if-false>
|
||||
<if-true>
|
||||
<!-- PPPoE control packet, enqueue directly -->
|
||||
<assign-variable name="$hpnia" value="0x500002"/>
|
||||
<action type="exit" confirm="yes" nextproto="end_parse"/>
|
||||
</if-true>
|
||||
</if>
|
||||
</before>
|
||||
</execute-code>
|
||||
</protocol>
|
||||
<protocol name="ethernetschema" longname="offline port Ethernet correction" prevproto="ethernet">
|
||||
<execute-code>
|
||||
<before>
|
||||
<if expr="($logicalportid ge 9) and (ethernet.type == 0x0800 or ethernet.type == 0x86dd)">
|
||||
<if-true>
|
||||
<if expr="($FW[112:8] bitwand 0xF0) == 0x40">
|
||||
<if-true>
|
||||
<nextproto proto="#ip4"/>
|
||||
<action type="exit" confirm="no" nextproto="ipv4"/>
|
||||
</if-true>
|
||||
</if>
|
||||
<if expr="($FW[112:8] bitwand 0xF0) == 0x60">
|
||||
<if-true>
|
||||
<nextproto proto="#ip6"/>
|
||||
<action type="exit" confirm="no" nextproto="ipv6"/>
|
||||
</if-true>
|
||||
</if>
|
||||
</if-true>
|
||||
</if>
|
||||
<action type="exit" nextproto="return"/>
|
||||
</before>
|
||||
</execute-code>
|
||||
</protocol>
|
||||
<protocol name="ipv4schema" longname="ipv4schema" prevproto="ipv4">
|
||||
<execute-code>
|
||||
<before>
|
||||
<!-- drop packets with invalid ttl value -->
|
||||
<if expr="ipv4.ttl == 1 or ipv4.ttl == 0">
|
||||
<if-true>
|
||||
<assign-variable name="$nia" value="0x500002"/>
|
||||
<assign-variable name="$hpnia" value="0x500002"/>
|
||||
<action type="exit" confirm="no" nextproto="end_parse"/>
|
||||
</if-true>
|
||||
</if>
|
||||
<!-- If it is a multicast packet stop parsing -->
|
||||
<if expr="$l2r bitwand 0x0060 == 0x0040">
|
||||
<if-true>
|
||||
<action type="exit" confirm="yes" nextproto="end_parse"/>
|
||||
</if-true>
|
||||
</if>
|
||||
<!-- If next protocol is ipv6 change protocol -->
|
||||
<if expr="ipv4.nextp == 0x29">
|
||||
<if-true>
|
||||
<action type="exit" confirm="no" nextproto="ipv6"/>
|
||||
</if-true>
|
||||
</if>
|
||||
</before>
|
||||
</execute-code>
|
||||
</protocol>
|
||||
<protocol name="ipv6schema" longname="ipv6schema" prevproto="ipv6">
|
||||
<execute-code>
|
||||
<before>
|
||||
<if expr="ipv6.hop == 1 or ipv6.hop == 0">
|
||||
<if-true>
|
||||
<assign-variable name="$nia" value="0x500002"/>
|
||||
<assign-variable name="$hpnia" value="0x500002"/>
|
||||
<action type="exit" confirm="no" nextproto="end_parse"/>
|
||||
</if-true>
|
||||
</if>
|
||||
<if expr="$l2r bitwand 0x0060 == 0x0040">
|
||||
<if-true>
|
||||
<action type="exit" confirm="yes" nextproto="end_parse"/>
|
||||
</if-true>
|
||||
</if>
|
||||
</before>
|
||||
</execute-code>
|
||||
</protocol>
|
||||
<protocol name="udpschema" longname="udpschema" prevproto="udp">
|
||||
<execute-code>
|
||||
<before>
|
||||
<if expr="$ipoffset_1 == $ipoffset_n">
|
||||
<if-true>
|
||||
<if expr="$l3r bitwand 0x8000 == 0x8000">
|
||||
<if-true>
|
||||
<assign-variable name="$l3r" value="$l3r bitwor 0x800"/>
|
||||
</if-true>
|
||||
<if-false>
|
||||
<assign-variable name="$l3r" value="$l3r bitwor 0x400"/>
|
||||
</if-false>
|
||||
</if>
|
||||
</if-true>
|
||||
</if>
|
||||
<if expr="(udp.sport == 0x1194 or udp.dport == 0x1194)">
|
||||
<if-true>
|
||||
<!-- esp-in-udp packet, if source or dest port is 4500 -->
|
||||
<!-- ESP Marker after UDP header is (spi is 0) 0 for all ISAKMP NATT packet -->
|
||||
<!-- Send all ISAKMP NATT packets to HOST -->
|
||||
<if expr="($FW[64:32] bitwand 0xFFFFFFFF) == 0x00000000">
|
||||
<if-true>
|
||||
<assign-variable name="$nia" value="0x500002"/>
|
||||
<assign-variable name="$hpnia" value="0x500002"/>
|
||||
<action type="exit" confirm="no" nextproto="end_parse"/>
|
||||
</if-true>
|
||||
</if>
|
||||
</if-true>
|
||||
</if>
|
||||
<if expr="($logicalportid lt 9)">
|
||||
<if-true>
|
||||
<if expr="$l2r bitwand 0x0800 == 0x0800">
|
||||
<if-false>
|
||||
<!-- not PPPoE session packet, police the packet -->
|
||||
<assign-variable name="$nia" value="0x4C0000"/>
|
||||
<action type="exit" confirm="yes" nextproto="end_parse"/>
|
||||
</if-false>
|
||||
<if-true>
|
||||
</if-true>
|
||||
</if>
|
||||
</if-true>
|
||||
</if>
|
||||
</before>
|
||||
</execute-code>
|
||||
</protocol>
|
||||
<protocol name="tcpschema" longname="tcpschema" prevproto="tcp">
|
||||
<execute-code>
|
||||
<before>
|
||||
<if expr="((tcp.flags bitwand 7) != 0)">
|
||||
<if-true>
|
||||
<assign-variable name="$hpnia" value="0x500002"/>
|
||||
<action type="exit" confirm="yes" nextproto="end_parse"/>
|
||||
</if-true>
|
||||
</if>
|
||||
<if expr="$ipoffset_1 == $ipoffset_n">
|
||||
<if-true>
|
||||
<if expr="$l3r bitwand 0x8000 == 0x8000">
|
||||
<if-true>
|
||||
<assign-variable name="$l3r" value="$l3r bitwor 0x800"/>
|
||||
</if-true>
|
||||
<if-false>
|
||||
<assign-variable name="$l3r" value="$l3r bitwor 0x400"/>
|
||||
</if-false>
|
||||
</if>
|
||||
</if-true>
|
||||
</if>
|
||||
<if expr="$logicalportid lt 9">
|
||||
<if-true>
|
||||
<if expr="$l2r bitwand 0x0800 == 0x0800">
|
||||
<if-false>
|
||||
<!-- not PPPoE session packet, police the packet -->
|
||||
<assign-variable name="$nia" value="0x4C0000"/>
|
||||
<action type="exit" confirm="yes" nextproto="end_parse"/>
|
||||
</if-false>
|
||||
<if-true>
|
||||
</if-true>
|
||||
</if>
|
||||
</if-true>
|
||||
</if>
|
||||
</before>
|
||||
</execute-code>
|
||||
</protocol>
|
||||
<protocol name="espschema" longname="espschema" prevproto="ipsec_esp">
|
||||
<execute-code>
|
||||
<before>
|
||||
<if expr="$logicalportid lt 9">
|
||||
<if-true>
|
||||
<if expr="$l2r bitwand 0x0800 == 0x0800">
|
||||
<if-false>
|
||||
<!-- not PPPoE session packet, police the packet -->
|
||||
<assign-variable name="$nia" value="0x4C0000"/>
|
||||
<action type="exit" confirm="yes" nextproto="end_parse"/>
|
||||
</if-false>
|
||||
<if-true>
|
||||
</if-true>
|
||||
</if>
|
||||
</if-true>
|
||||
</if>
|
||||
</before>
|
||||
</execute-code>
|
||||
</protocol>
|
||||
</netpdl>
|
||||
1753
config/hxs_pdl_v3.xml
Normal file
1753
config/hxs_pdl_v3.xml
Normal file
File diff suppressed because it is too large
Load diff
0
dist/.keep
vendored
Normal file
0
dist/.keep
vendored
Normal file
17
drivers/emc2302/Makefile
Normal file
17
drivers/emc2302/Makefile
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# Microchip EMC2301/2302/2303/2305 Fan Controller kernel module for FreeBSD
|
||||
#
|
||||
# Build:
|
||||
# make -C /build/opnsense-deps/drivers/emc2302 \
|
||||
# KERNBUILDDIR=/usr/obj/build/opnsense-src/arm64.aarch64/sys/MONO-GATEWAY \
|
||||
# MACHINE=arm64 MACHINE_ARCH=aarch64 CC=cc LD=ld.lld
|
||||
#
|
||||
# Copyright 2026 Mono Technologies Inc.
|
||||
# SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
SYSDIR?= ${.CURDIR}/../../../opnsense-src/sys
|
||||
|
||||
KMOD= emc2302
|
||||
SRCS= emc2302.c
|
||||
SRCS+= opt_platform.h device_if.h bus_if.h iicbus_if.h ofw_bus_if.h
|
||||
|
||||
.include <bsd.kmod.mk>
|
||||
433
drivers/emc2302/emc2302.c
Normal file
433
drivers/emc2302/emc2302.c
Normal file
|
|
@ -0,0 +1,433 @@
|
|||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2026 Mono Technologies Inc.
|
||||
*
|
||||
* Microchip EMC2301/2302/2303/2305 RPM-based PWM fan controller driver.
|
||||
* Provides per-channel sysctl nodes for PWM duty cycle (RW),
|
||||
* tachometer RPM (RO), and drive fault status (RO).
|
||||
*
|
||||
* Based on Linux hwmon/emc2305.c by Nvidia.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/bus.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/module.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
#include <dev/iicbus/iicbus.h>
|
||||
#include <dev/iicbus/iiconf.h>
|
||||
#include <dev/ofw/ofw_bus.h>
|
||||
#include <dev/ofw/ofw_bus_subr.h>
|
||||
#include <dev/ofw/openfirm.h>
|
||||
|
||||
/* Identification registers */
|
||||
#define EMC2305_REG_VENDOR 0xFE
|
||||
#define EMC2305_REG_PRODUCT_ID 0xFD
|
||||
#define EMC2305_VENDOR_MICROCHIP 0x5D
|
||||
|
||||
/* Product IDs */
|
||||
#define EMC2305_PID_2305 0x34 /* 5 channels */
|
||||
#define EMC2305_PID_2303 0x35 /* 3 channels */
|
||||
#define EMC2305_PID_2302 0x36 /* 2 channels */
|
||||
#define EMC2305_PID_2301 0x37 /* 1 channel */
|
||||
|
||||
/* Global configuration registers */
|
||||
#define EMC2305_REG_DRIVE_FAIL 0x27
|
||||
#define EMC2305_REG_POLARITY 0x2A
|
||||
#define EMC2305_REG_OUTPUT_TYPE 0x2B
|
||||
|
||||
/* Per-channel registers (n = 0..4) */
|
||||
#define EMC2305_REG_FAN_DRIVE(n) (0x30 + 0x10 * (n))
|
||||
#define EMC2305_REG_FAN_MIN_DRIVE(n) (0x38 + 0x10 * (n))
|
||||
#define EMC2305_REG_FAN_TACH(n) (0x3E + 0x10 * (n))
|
||||
|
||||
/* RPM calculation constants (from datasheet) */
|
||||
#define EMC2305_RPM_FACTOR 3932160
|
||||
#define EMC2305_TACH_SHIFT 3 /* lower 3 bits unused */
|
||||
#define EMC2305_TACH_MULTIPLIER 2
|
||||
#define EMC2305_RPM_MIN 480
|
||||
|
||||
#define EMC2305_MAX_FANS 5
|
||||
|
||||
struct emc2302_softc {
|
||||
device_t dev;
|
||||
int nfans;
|
||||
uint8_t product_id;
|
||||
struct sysctl_ctx_list sysctl_ctx;
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* I2C register helpers
|
||||
* ================================================================ */
|
||||
|
||||
static int
|
||||
emc2302_read8(device_t dev, uint8_t reg, uint8_t *val)
|
||||
{
|
||||
|
||||
return (iicdev_readfrom(dev, reg, val, 1, IIC_WAIT));
|
||||
}
|
||||
|
||||
static int
|
||||
emc2302_write8(device_t dev, uint8_t reg, uint8_t val)
|
||||
{
|
||||
|
||||
return (iicdev_writeto(dev, reg, &val, 1, IIC_WAIT));
|
||||
}
|
||||
|
||||
static int
|
||||
emc2302_read16(device_t dev, uint8_t reg, uint16_t *val)
|
||||
{
|
||||
uint8_t buf[2];
|
||||
int error;
|
||||
|
||||
error = iicdev_readfrom(dev, reg, buf, 2, IIC_WAIT);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
*val = (buf[0] << 8) | buf[1];
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Hardware read helpers
|
||||
* ================================================================ */
|
||||
|
||||
static int
|
||||
emc2302_read_rpm(struct emc2302_softc *sc, int ch, int *rpm)
|
||||
{
|
||||
uint16_t raw;
|
||||
uint32_t count;
|
||||
int error;
|
||||
|
||||
error = emc2302_read16(sc->dev, EMC2305_REG_FAN_TACH(ch), &raw);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
|
||||
count = raw >> EMC2305_TACH_SHIFT;
|
||||
if (count == 0) {
|
||||
*rpm = 0;
|
||||
return (0);
|
||||
}
|
||||
|
||||
*rpm = (EMC2305_RPM_FACTOR / count) * EMC2305_TACH_MULTIPLIER;
|
||||
if (*rpm <= EMC2305_RPM_MIN)
|
||||
*rpm = 0;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Sysctl handlers
|
||||
* ================================================================ */
|
||||
|
||||
static int
|
||||
emc2302_sysctl_pwm(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
struct emc2302_softc *sc = arg1;
|
||||
int ch = arg2;
|
||||
uint8_t reg_val;
|
||||
int val, error;
|
||||
|
||||
error = emc2302_read8(sc->dev, EMC2305_REG_FAN_DRIVE(ch), ®_val);
|
||||
if (error != 0)
|
||||
return (EIO);
|
||||
val = reg_val;
|
||||
|
||||
error = sysctl_handle_int(oidp, &val, 0, req);
|
||||
if (error != 0 || req->newptr == NULL)
|
||||
return (error);
|
||||
|
||||
if (val < 0)
|
||||
val = 0;
|
||||
if (val > 255)
|
||||
val = 255;
|
||||
|
||||
error = emc2302_write8(sc->dev, EMC2305_REG_FAN_DRIVE(ch),
|
||||
(uint8_t)val);
|
||||
if (error != 0)
|
||||
return (EIO);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
emc2302_sysctl_rpm(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
struct emc2302_softc *sc = arg1;
|
||||
int ch = arg2;
|
||||
int val, error;
|
||||
|
||||
if (req->newptr != NULL)
|
||||
return (EINVAL);
|
||||
|
||||
error = emc2302_read_rpm(sc, ch, &val);
|
||||
if (error != 0)
|
||||
return (EIO);
|
||||
|
||||
return (sysctl_handle_int(oidp, &val, 0, req));
|
||||
}
|
||||
|
||||
static int
|
||||
emc2302_sysctl_fault(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
struct emc2302_softc *sc = arg1;
|
||||
int ch = arg2;
|
||||
uint8_t status;
|
||||
int val, error;
|
||||
|
||||
if (req->newptr != NULL)
|
||||
return (EINVAL);
|
||||
|
||||
error = emc2302_read8(sc->dev, EMC2305_REG_DRIVE_FAIL, &status);
|
||||
if (error != 0)
|
||||
return (EIO);
|
||||
|
||||
val = (status >> ch) & 1;
|
||||
|
||||
return (sysctl_handle_int(oidp, &val, 0, req));
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* DT child node parsing
|
||||
* ================================================================ */
|
||||
|
||||
static void
|
||||
emc2302_parse_dt(struct emc2302_softc *sc)
|
||||
{
|
||||
phandle_t node, child;
|
||||
uint32_t reg, pwm_args[3];
|
||||
uint8_t polarity, output_type;
|
||||
int i;
|
||||
|
||||
node = ofw_bus_get_node(sc->dev);
|
||||
if (node == 0)
|
||||
return;
|
||||
|
||||
polarity = 0;
|
||||
output_type = 0;
|
||||
|
||||
for (child = OF_child(node); child != 0; child = OF_peer(child)) {
|
||||
if (OF_getencprop(child, "reg", ®, sizeof(reg)) <= 0)
|
||||
continue;
|
||||
if ((int)reg >= sc->nfans)
|
||||
continue;
|
||||
|
||||
/* Parse pwms = <&ref freq polarity output_type> */
|
||||
if (OF_getencprop(child, "pwms", pwm_args,
|
||||
sizeof(pwm_args)) >= (int)(3 * sizeof(uint32_t))) {
|
||||
/* pwm_args[0] = phandle (skip) */
|
||||
/* pwm_args[1] = polarity */
|
||||
if (pwm_args[1])
|
||||
polarity |= (1 << reg);
|
||||
/* pwm_args[2] = output type (push-pull) */
|
||||
if (pwm_args[2])
|
||||
output_type |= (1 << reg);
|
||||
}
|
||||
}
|
||||
|
||||
/* Write polarity and output type registers */
|
||||
emc2302_write8(sc->dev, EMC2305_REG_POLARITY, polarity);
|
||||
emc2302_write8(sc->dev, EMC2305_REG_OUTPUT_TYPE, output_type);
|
||||
|
||||
/* Set minimum PWM per channel from DT if specified */
|
||||
for (child = OF_child(node); child != 0; child = OF_peer(child)) {
|
||||
uint32_t min_pwm;
|
||||
|
||||
if (OF_getencprop(child, "reg", ®, sizeof(reg)) <= 0)
|
||||
continue;
|
||||
if ((int)reg >= sc->nfans)
|
||||
continue;
|
||||
if (OF_getencprop(child, "pwm-min", &min_pwm,
|
||||
sizeof(min_pwm)) > 0) {
|
||||
if (min_pwm > 255)
|
||||
min_pwm = 255;
|
||||
emc2302_write8(sc->dev,
|
||||
EMC2305_REG_FAN_MIN_DRIVE(reg), (uint8_t)min_pwm);
|
||||
}
|
||||
}
|
||||
|
||||
/* Log per-channel labels */
|
||||
for (child = OF_child(node); child != 0; child = OF_peer(child)) {
|
||||
char label[64];
|
||||
|
||||
if (OF_getencprop(child, "reg", ®, sizeof(reg)) <= 0)
|
||||
continue;
|
||||
if ((int)reg >= sc->nfans)
|
||||
continue;
|
||||
if (OF_getprop(child, "label", label, sizeof(label)) > 0) {
|
||||
label[sizeof(label) - 1] = '\0';
|
||||
device_printf(sc->dev, "fan%u: %s\n", reg, label);
|
||||
}
|
||||
}
|
||||
|
||||
i = 0;
|
||||
for (child = OF_child(node); child != 0; child = OF_peer(child))
|
||||
i++;
|
||||
if (i > 0)
|
||||
device_printf(sc->dev, "polarity=0x%02x output=0x%02x\n",
|
||||
polarity, output_type);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Probe / Attach / Detach
|
||||
* ================================================================ */
|
||||
|
||||
static struct ofw_compat_data compat_data[] = {
|
||||
{ "microchip,emc2305", EMC2305_PID_2305 },
|
||||
{ "microchip,emc2303", EMC2305_PID_2303 },
|
||||
{ "microchip,emc2302", EMC2305_PID_2302 },
|
||||
{ "microchip,emc2301", EMC2305_PID_2301 },
|
||||
{ NULL, 0 },
|
||||
};
|
||||
|
||||
static const char *
|
||||
emc2302_chip_name(uint8_t pid)
|
||||
{
|
||||
|
||||
switch (pid) {
|
||||
case EMC2305_PID_2305: return ("EMC2305");
|
||||
case EMC2305_PID_2303: return ("EMC2303");
|
||||
case EMC2305_PID_2302: return ("EMC2302");
|
||||
case EMC2305_PID_2301: return ("EMC2301");
|
||||
default: return ("EMC23xx");
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
emc2302_nfans_from_pid(uint8_t pid)
|
||||
{
|
||||
|
||||
switch (pid) {
|
||||
case EMC2305_PID_2305: return (5);
|
||||
case EMC2305_PID_2303: return (3);
|
||||
case EMC2305_PID_2302: return (2);
|
||||
case EMC2305_PID_2301: return (1);
|
||||
default: return (0);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
emc2302_probe(device_t dev)
|
||||
{
|
||||
const struct ofw_compat_data *cd;
|
||||
char desc[80];
|
||||
|
||||
if (!ofw_bus_status_okay(dev))
|
||||
return (ENXIO);
|
||||
|
||||
cd = ofw_bus_search_compatible(dev, compat_data);
|
||||
if (cd->ocd_data == 0)
|
||||
return (ENXIO);
|
||||
|
||||
snprintf(desc, sizeof(desc), "%s Fan Controller",
|
||||
emc2302_chip_name((uint8_t)cd->ocd_data));
|
||||
device_set_desc_copy(dev, desc);
|
||||
|
||||
return (BUS_PROBE_DEFAULT);
|
||||
}
|
||||
|
||||
static int
|
||||
emc2302_attach(device_t dev)
|
||||
{
|
||||
struct emc2302_softc *sc;
|
||||
struct sysctl_oid *tree, *fan_oid;
|
||||
uint8_t vendor, pid;
|
||||
char fan_name[8];
|
||||
int error, i;
|
||||
|
||||
sc = device_get_softc(dev);
|
||||
sc->dev = dev;
|
||||
|
||||
/* Verify vendor ID */
|
||||
error = emc2302_read8(dev, EMC2305_REG_VENDOR, &vendor);
|
||||
if (error != 0) {
|
||||
device_printf(dev, "failed to read vendor register\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
if (vendor != EMC2305_VENDOR_MICROCHIP) {
|
||||
device_printf(dev, "unexpected vendor ID: 0x%02x\n", vendor);
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
/* Read product ID and determine fan count */
|
||||
error = emc2302_read8(dev, EMC2305_REG_PRODUCT_ID, &pid);
|
||||
if (error != 0) {
|
||||
device_printf(dev, "failed to read product ID\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
sc->product_id = pid;
|
||||
sc->nfans = emc2302_nfans_from_pid(pid);
|
||||
if (sc->nfans == 0) {
|
||||
device_printf(dev, "unknown product ID: 0x%02x\n", pid);
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
device_printf(dev, "%s (product 0x%02x), %d fan channel%s\n",
|
||||
emc2302_chip_name(pid), pid, sc->nfans,
|
||||
sc->nfans > 1 ? "s" : "");
|
||||
|
||||
/* Parse DT child nodes for polarity, output type, min PWM */
|
||||
emc2302_parse_dt(sc);
|
||||
|
||||
/* Create sysctl tree */
|
||||
sysctl_ctx_init(&sc->sysctl_ctx);
|
||||
tree = device_get_sysctl_tree(dev);
|
||||
|
||||
for (i = 0; i < sc->nfans; i++) {
|
||||
snprintf(fan_name, sizeof(fan_name), "fan%d", i);
|
||||
|
||||
fan_oid = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
|
||||
SYSCTL_CHILDREN(tree), OID_AUTO, fan_name,
|
||||
CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Fan channel");
|
||||
|
||||
SYSCTL_ADD_PROC(&sc->sysctl_ctx,
|
||||
SYSCTL_CHILDREN(fan_oid), OID_AUTO, "pwm",
|
||||
CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
|
||||
sc, i, emc2302_sysctl_pwm, "I",
|
||||
"PWM duty cycle (0-255)");
|
||||
|
||||
SYSCTL_ADD_PROC(&sc->sysctl_ctx,
|
||||
SYSCTL_CHILDREN(fan_oid), OID_AUTO, "rpm",
|
||||
CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
|
||||
sc, i, emc2302_sysctl_rpm, "I",
|
||||
"Fan speed (RPM)");
|
||||
|
||||
SYSCTL_ADD_PROC(&sc->sysctl_ctx,
|
||||
SYSCTL_CHILDREN(fan_oid), OID_AUTO, "fault",
|
||||
CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
|
||||
sc, i, emc2302_sysctl_fault, "I",
|
||||
"Drive failure (0=ok, 1=fault)");
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
emc2302_detach(device_t dev)
|
||||
{
|
||||
struct emc2302_softc *sc;
|
||||
|
||||
sc = device_get_softc(dev);
|
||||
sysctl_ctx_free(&sc->sysctl_ctx);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static device_method_t emc2302_methods[] = {
|
||||
DEVMETHOD(device_probe, emc2302_probe),
|
||||
DEVMETHOD(device_attach, emc2302_attach),
|
||||
DEVMETHOD(device_detach, emc2302_detach),
|
||||
DEVMETHOD_END
|
||||
};
|
||||
|
||||
static driver_t emc2302_driver = {
|
||||
"emc2302",
|
||||
emc2302_methods,
|
||||
sizeof(struct emc2302_softc),
|
||||
};
|
||||
|
||||
DRIVER_MODULE(emc2302, iicbus, emc2302_driver, NULL, NULL);
|
||||
MODULE_VERSION(emc2302, 1);
|
||||
MODULE_DEPEND(emc2302, iicbus, IICBUS_MODVER, IICBUS_MODVER, IICBUS_MODVER);
|
||||
IICBUS_FDT_PNP_INFO(compat_data);
|
||||
29
drivers/fand/Makefile
Normal file
29
drivers/fand/Makefile
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# Fan control daemon for Mono Gateway
|
||||
#
|
||||
# Build:
|
||||
# mkdir -p /tmp/fand-build && cd /tmp/fand-build
|
||||
# make -f /build/opnsense-deps/drivers/fand/Makefile
|
||||
#
|
||||
# Copyright 2026 Mono Technologies Inc.
|
||||
# SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
SYSROOT?= /usr/obj/build/opnsense-src/arm64.aarch64/tmp
|
||||
CC?= cc
|
||||
TARGET?= --target=aarch64-unknown-freebsd14.3
|
||||
|
||||
CFLAGS= -O2 -pipe -Wall --sysroot=${SYSROOT} ${TARGET}
|
||||
LDFLAGS= ${TARGET} --sysroot=${SYSROOT}
|
||||
|
||||
SRCDIR?= ${MAKEFILE:H}
|
||||
|
||||
PROG= fand
|
||||
|
||||
all: ${PROG}
|
||||
|
||||
${PROG}: ${SRCDIR}/fand.c
|
||||
${CC} ${CFLAGS} ${LDFLAGS} -o ${PROG} ${SRCDIR}/fand.c
|
||||
|
||||
clean:
|
||||
rm -f ${PROG}
|
||||
|
||||
.PHONY: all clean
|
||||
158
drivers/fand/fand.c
Normal file
158
drivers/fand/fand.c
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2026 Mono Technologies Inc.
|
||||
*
|
||||
* Fan control daemon for the Mono Gateway.
|
||||
* Linearly scales PWM from 20% at 40°C to 100% at 80°C
|
||||
* using the SoC TMU temperature and EMC2302 fan controller.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
#include <err.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* Temperature thresholds (millidegrees C) */
|
||||
#define TEMP_LOW_MC 40000 /* 40°C — minimum fan speed */
|
||||
#define TEMP_HIGH_MC 80000 /* 80°C — maximum fan speed */
|
||||
|
||||
/* PWM range (0-255) */
|
||||
#define PWM_MIN 51 /* 20% of 255 */
|
||||
#define PWM_MAX 255 /* 100% */
|
||||
|
||||
/* Polling interval */
|
||||
#define POLL_INTERVAL 2 /* seconds */
|
||||
|
||||
/* Sysctl names */
|
||||
#define SYSCTL_TEMP "hw.temperature.core-cluster"
|
||||
#define SYSCTL_FAN0_PWM "dev.emc2302.0.fan0.pwm"
|
||||
#define SYSCTL_FAN1_PWM "dev.emc2302.0.fan1.pwm"
|
||||
|
||||
static volatile sig_atomic_t running = 1;
|
||||
|
||||
static void
|
||||
signal_handler(int sig __unused)
|
||||
{
|
||||
|
||||
running = 0;
|
||||
}
|
||||
|
||||
static int
|
||||
read_temp_mc(void)
|
||||
{
|
||||
int val;
|
||||
size_t len;
|
||||
|
||||
len = sizeof(val);
|
||||
if (sysctlbyname(SYSCTL_TEMP, &val, &len, NULL, 0) != 0)
|
||||
return (-1);
|
||||
|
||||
/* Convert decikelvin to millidegrees C */
|
||||
return ((val - 2731) * 100);
|
||||
}
|
||||
|
||||
static int
|
||||
set_pwm(const char *name, int pwm)
|
||||
{
|
||||
|
||||
return (sysctlbyname(name, NULL, NULL, &pwm, sizeof(pwm)));
|
||||
}
|
||||
|
||||
static int
|
||||
compute_pwm(int temp_mc)
|
||||
{
|
||||
|
||||
if (temp_mc <= TEMP_LOW_MC)
|
||||
return (PWM_MIN);
|
||||
if (temp_mc >= TEMP_HIGH_MC)
|
||||
return (PWM_MAX);
|
||||
|
||||
return (PWM_MIN + (temp_mc - TEMP_LOW_MC) *
|
||||
(PWM_MAX - PWM_MIN) / (TEMP_HIGH_MC - TEMP_LOW_MC));
|
||||
}
|
||||
|
||||
static void
|
||||
set_fans(int pwm)
|
||||
{
|
||||
|
||||
set_pwm(SYSCTL_FAN0_PWM, pwm);
|
||||
set_pwm(SYSCTL_FAN1_PWM, pwm);
|
||||
}
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
|
||||
fprintf(stderr, "usage: fand [-d]\n");
|
||||
fprintf(stderr, " -d daemonize\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int dflag, ch, temp_mc, pwm, last_pwm;
|
||||
|
||||
dflag = 0;
|
||||
while ((ch = getopt(argc, argv, "d")) != -1) {
|
||||
switch (ch) {
|
||||
case 'd':
|
||||
dflag = 1;
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
}
|
||||
}
|
||||
|
||||
/* Verify sysctls exist */
|
||||
temp_mc = read_temp_mc();
|
||||
if (temp_mc < 0)
|
||||
errx(1, "cannot read %s", SYSCTL_TEMP);
|
||||
if (set_pwm(SYSCTL_FAN0_PWM, PWM_MIN) != 0)
|
||||
errx(1, "cannot write %s", SYSCTL_FAN0_PWM);
|
||||
if (set_pwm(SYSCTL_FAN1_PWM, PWM_MIN) != 0)
|
||||
errx(1, "cannot write %s", SYSCTL_FAN1_PWM);
|
||||
|
||||
if (dflag && daemon(0, 0) != 0)
|
||||
err(1, "daemon");
|
||||
|
||||
signal(SIGTERM, signal_handler);
|
||||
signal(SIGINT, signal_handler);
|
||||
|
||||
last_pwm = -1;
|
||||
|
||||
while (running) {
|
||||
temp_mc = read_temp_mc();
|
||||
if (temp_mc < 0) {
|
||||
/* Sensor failure — full speed for safety */
|
||||
set_fans(PWM_MAX);
|
||||
last_pwm = PWM_MAX;
|
||||
sleep(POLL_INTERVAL);
|
||||
continue;
|
||||
}
|
||||
|
||||
pwm = compute_pwm(temp_mc);
|
||||
|
||||
if (last_pwm < 0 || abs(pwm - last_pwm) >= 5) {
|
||||
fprintf(stderr, "fand: %d.%d°C -> PWM %d (%d%%)\n",
|
||||
temp_mc / 1000, (temp_mc % 1000) / 100,
|
||||
pwm, pwm * 100 / 255);
|
||||
set_fans(pwm);
|
||||
last_pwm = pwm;
|
||||
}
|
||||
|
||||
sleep(POLL_INTERVAL);
|
||||
}
|
||||
|
||||
/* Safety: full speed on exit */
|
||||
fprintf(stderr, "fand: shutting down, fans to 100%%\n");
|
||||
set_fans(PWM_MAX);
|
||||
|
||||
return (0);
|
||||
}
|
||||
16
drivers/fand/fand.rc
Normal file
16
drivers/fand/fand.rc
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#!/bin/sh
|
||||
|
||||
# PROVIDE: fand
|
||||
# REQUIRE: DAEMON
|
||||
# KEYWORD: shutdown
|
||||
|
||||
. /etc/rc.subr
|
||||
|
||||
name="fand"
|
||||
desc="Fan control daemon"
|
||||
rcvar="fand_enable"
|
||||
command="/usr/local/sbin/${name}"
|
||||
command_args="-d"
|
||||
|
||||
load_rc_config $name
|
||||
run_rc_command "$1"
|
||||
17
drivers/ina2xx/Makefile
Normal file
17
drivers/ina2xx/Makefile
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# TI INA2xx Power Monitor kernel module for FreeBSD
|
||||
#
|
||||
# Build:
|
||||
# make -C /build/opnsense-deps/drivers/ina2xx \
|
||||
# KERNBUILDDIR=/usr/obj/build/opnsense-src/arm64.aarch64/sys/MONO-GATEWAY \
|
||||
# MACHINE=arm64 MACHINE_ARCH=aarch64 CC=cc LD=ld.lld
|
||||
#
|
||||
# Copyright 2026 Mono Technologies Inc.
|
||||
# SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
SYSDIR?= ${.CURDIR}/../../../opnsense-src/sys
|
||||
|
||||
KMOD= ina2xx
|
||||
SRCS= ina2xx.c
|
||||
SRCS+= opt_platform.h device_if.h bus_if.h iicbus_if.h ofw_bus_if.h
|
||||
|
||||
.include <bsd.kmod.mk>
|
||||
395
drivers/ina2xx/ina2xx.c
Normal file
395
drivers/ina2xx/ina2xx.c
Normal file
|
|
@ -0,0 +1,395 @@
|
|||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2026 Mono Technologies Inc.
|
||||
*
|
||||
* Texas Instruments INA219/INA226/INA234 I2C power monitor driver.
|
||||
* Reads bus voltage, shunt voltage, current, and power from
|
||||
* INA2xx-family digital power monitors over I2C.
|
||||
*
|
||||
* Based on Linux hwmon/ina2xx.c by Lothar Felten.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/bus.h>
|
||||
#include <sys/endian.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/module.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
#include <dev/iicbus/iicbus.h>
|
||||
#include <dev/iicbus/iiconf.h>
|
||||
#include <dev/ofw/ofw_bus.h>
|
||||
#include <dev/ofw/ofw_bus_subr.h>
|
||||
|
||||
/* INA2xx register addresses */
|
||||
#define INA2XX_CONFIG 0x00
|
||||
#define INA2XX_SHUNT_VOLTAGE 0x01
|
||||
#define INA2XX_BUS_VOLTAGE 0x02
|
||||
#define INA2XX_POWER 0x03
|
||||
#define INA2XX_CURRENT 0x04
|
||||
#define INA2XX_CALIBRATION 0x05
|
||||
|
||||
/* Default shunt resistance: 10 mOhm = 10000 uOhm */
|
||||
#define INA2XX_RSHUNT_DEFAULT 10000
|
||||
|
||||
/* Chip type enumeration */
|
||||
enum ina2xx_chip { INA219, INA226, INA234 };
|
||||
|
||||
/* Per-chip-type configuration constants */
|
||||
struct ina2xx_config {
|
||||
uint16_t config_default;
|
||||
uint16_t calibration_value;
|
||||
int shunt_div; /* shunt voltage divider (LSB factor) */
|
||||
int bus_voltage_shift;
|
||||
int bus_voltage_lsb; /* uV per LSB */
|
||||
int power_lsb_factor;
|
||||
};
|
||||
|
||||
static const struct ina2xx_config ina2xx_configs[] = {
|
||||
[INA219] = {
|
||||
.config_default = 0x399F, /* PGA /8 */
|
||||
.calibration_value = 4096,
|
||||
.shunt_div = 100,
|
||||
.bus_voltage_shift = 3,
|
||||
.bus_voltage_lsb = 4000, /* 4 mV */
|
||||
.power_lsb_factor = 20,
|
||||
},
|
||||
[INA226] = {
|
||||
.config_default = 0x4527, /* avg=16 */
|
||||
.calibration_value = 2048,
|
||||
.shunt_div = 400,
|
||||
.bus_voltage_shift = 0,
|
||||
.bus_voltage_lsb = 1250, /* 1.25 mV */
|
||||
.power_lsb_factor = 25,
|
||||
},
|
||||
[INA234] = {
|
||||
.config_default = 0x4527, /* same as INA226 */
|
||||
.calibration_value = 2048,
|
||||
.shunt_div = 400,
|
||||
.bus_voltage_shift = 4,
|
||||
.bus_voltage_lsb = 25600, /* 25.6 mV */
|
||||
.power_lsb_factor = 32,
|
||||
},
|
||||
};
|
||||
|
||||
struct ina2xx_softc {
|
||||
device_t dev;
|
||||
enum ina2xx_chip chip;
|
||||
const struct ina2xx_config *config;
|
||||
uint32_t rshunt; /* shunt resistance, uOhm */
|
||||
long current_lsb_uA;
|
||||
long power_lsb_uW;
|
||||
struct sysctl_ctx_list sysctl_ctx;
|
||||
char label[64];
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* I2C register helpers — 16-bit big-endian
|
||||
* ================================================================ */
|
||||
|
||||
static int
|
||||
ina2xx_read_reg(device_t dev, uint8_t reg, uint16_t *val)
|
||||
{
|
||||
uint8_t buf[2];
|
||||
int error;
|
||||
|
||||
error = iicdev_readfrom(dev, reg, buf, 2, IIC_WAIT);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
*val = (buf[0] << 8) | buf[1];
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
ina2xx_write_reg(device_t dev, uint8_t reg, uint16_t val)
|
||||
{
|
||||
uint8_t buf[2];
|
||||
|
||||
buf[0] = (val >> 8) & 0xFF;
|
||||
buf[1] = val & 0xFF;
|
||||
return (iicdev_writeto(dev, reg, buf, 2, IIC_WAIT));
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Measurement value conversion (matches Linux ina2xx_get_value)
|
||||
* ================================================================ */
|
||||
|
||||
static int
|
||||
ina2xx_read_bus_voltage(struct ina2xx_softc *sc, int *millivolts)
|
||||
{
|
||||
uint16_t raw;
|
||||
int error;
|
||||
|
||||
error = ina2xx_read_reg(sc->dev, INA2XX_BUS_VOLTAGE, &raw);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
*millivolts = ((raw >> sc->config->bus_voltage_shift) *
|
||||
sc->config->bus_voltage_lsb) / 1000;
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
ina2xx_read_shunt_voltage(struct ina2xx_softc *sc, int *microvolts)
|
||||
{
|
||||
uint16_t raw;
|
||||
int error;
|
||||
|
||||
error = ina2xx_read_reg(sc->dev, INA2XX_SHUNT_VOLTAGE, &raw);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
/* Shunt voltage register is signed */
|
||||
*microvolts = (int16_t)raw * 1000 / sc->config->shunt_div;
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
ina2xx_read_current(struct ina2xx_softc *sc, int *milliamps)
|
||||
{
|
||||
uint16_t raw;
|
||||
int error;
|
||||
|
||||
error = ina2xx_read_reg(sc->dev, INA2XX_CURRENT, &raw);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
/* Current register is signed */
|
||||
*milliamps = (int16_t)raw * sc->current_lsb_uA / 1000;
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
ina2xx_read_power(struct ina2xx_softc *sc, int *milliwatts)
|
||||
{
|
||||
uint16_t raw;
|
||||
int error;
|
||||
|
||||
error = ina2xx_read_reg(sc->dev, INA2XX_POWER, &raw);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
/* Power register is unsigned */
|
||||
*milliwatts = (int)((uint32_t)raw * sc->power_lsb_uW / 1000);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Sysctl handlers
|
||||
* ================================================================ */
|
||||
|
||||
static int
|
||||
ina2xx_sysctl_bus_voltage(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
struct ina2xx_softc *sc = arg1;
|
||||
int val, error;
|
||||
|
||||
if (req->newptr != NULL)
|
||||
return (EINVAL);
|
||||
error = ina2xx_read_bus_voltage(sc, &val);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
return (sysctl_handle_int(oidp, &val, 0, req));
|
||||
}
|
||||
|
||||
static int
|
||||
ina2xx_sysctl_shunt_voltage(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
struct ina2xx_softc *sc = arg1;
|
||||
int val, error;
|
||||
|
||||
if (req->newptr != NULL)
|
||||
return (EINVAL);
|
||||
error = ina2xx_read_shunt_voltage(sc, &val);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
return (sysctl_handle_int(oidp, &val, 0, req));
|
||||
}
|
||||
|
||||
static int
|
||||
ina2xx_sysctl_current(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
struct ina2xx_softc *sc = arg1;
|
||||
int val, error;
|
||||
|
||||
if (req->newptr != NULL)
|
||||
return (EINVAL);
|
||||
error = ina2xx_read_current(sc, &val);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
return (sysctl_handle_int(oidp, &val, 0, req));
|
||||
}
|
||||
|
||||
static int
|
||||
ina2xx_sysctl_power(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
struct ina2xx_softc *sc = arg1;
|
||||
int val, error;
|
||||
|
||||
if (req->newptr != NULL)
|
||||
return (EINVAL);
|
||||
error = ina2xx_read_power(sc, &val);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
return (sysctl_handle_int(oidp, &val, 0, req));
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Probe / Attach / Detach
|
||||
* ================================================================ */
|
||||
|
||||
static struct ofw_compat_data compat_data[] = {
|
||||
{ "ti,ina219", (uintptr_t)INA219 },
|
||||
{ "ti,ina220", (uintptr_t)INA219 },
|
||||
{ "ti,ina226", (uintptr_t)INA226 },
|
||||
{ "ti,ina230", (uintptr_t)INA226 },
|
||||
{ "ti,ina231", (uintptr_t)INA226 },
|
||||
{ "ti,ina234", (uintptr_t)INA234 },
|
||||
{ NULL, 0 },
|
||||
};
|
||||
|
||||
static int
|
||||
ina2xx_probe(device_t dev)
|
||||
{
|
||||
const struct ofw_compat_data *cd;
|
||||
phandle_t node;
|
||||
char desc[80];
|
||||
char label[64];
|
||||
|
||||
if (!ofw_bus_status_okay(dev))
|
||||
return (ENXIO);
|
||||
|
||||
cd = ofw_bus_search_compatible(dev, compat_data);
|
||||
if (cd->ocd_data == 0)
|
||||
return (ENXIO);
|
||||
|
||||
node = ofw_bus_get_node(dev);
|
||||
if (OF_getprop(node, "label", label, sizeof(label)) > 0)
|
||||
snprintf(desc, sizeof(desc), "INA2xx Power Monitor (%s)",
|
||||
label);
|
||||
else
|
||||
snprintf(desc, sizeof(desc), "INA2xx Power Monitor");
|
||||
device_set_desc_copy(dev, desc);
|
||||
|
||||
return (BUS_PROBE_DEFAULT);
|
||||
}
|
||||
|
||||
static int
|
||||
ina2xx_attach(device_t dev)
|
||||
{
|
||||
struct ina2xx_softc *sc;
|
||||
const struct ofw_compat_data *cd;
|
||||
phandle_t node;
|
||||
struct sysctl_oid *oid;
|
||||
uint32_t shunt;
|
||||
long dividend;
|
||||
int error;
|
||||
|
||||
sc = device_get_softc(dev);
|
||||
sc->dev = dev;
|
||||
|
||||
cd = ofw_bus_search_compatible(dev, compat_data);
|
||||
sc->chip = (enum ina2xx_chip)cd->ocd_data;
|
||||
sc->config = &ina2xx_configs[sc->chip];
|
||||
|
||||
node = ofw_bus_get_node(dev);
|
||||
|
||||
/* Read shunt resistance from DT (micro-ohms), default 10 mOhm */
|
||||
if (OF_getencprop(node, "shunt-resistor", &shunt, sizeof(shunt)) <= 0)
|
||||
shunt = INA2XX_RSHUNT_DEFAULT;
|
||||
sc->rshunt = shunt;
|
||||
|
||||
/* Read label from DT */
|
||||
sc->label[0] = '\0';
|
||||
OF_getprop(node, "label", sc->label, sizeof(sc->label));
|
||||
sc->label[sizeof(sc->label) - 1] = '\0';
|
||||
|
||||
/* Compute current and power LSBs */
|
||||
dividend = 1000000000L / sc->config->shunt_div;
|
||||
sc->current_lsb_uA = dividend / (long)sc->rshunt;
|
||||
sc->power_lsb_uW = sc->config->power_lsb_factor * sc->current_lsb_uA;
|
||||
|
||||
/* Program configuration register */
|
||||
error = ina2xx_write_reg(dev, INA2XX_CONFIG,
|
||||
sc->config->config_default);
|
||||
if (error != 0) {
|
||||
device_printf(dev, "failed to write config register\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
/* Program calibration register */
|
||||
error = ina2xx_write_reg(dev, INA2XX_CALIBRATION,
|
||||
sc->config->calibration_value);
|
||||
if (error != 0) {
|
||||
device_printf(dev, "failed to write calibration register\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
/* Create sysctl tree */
|
||||
sysctl_ctx_init(&sc->sysctl_ctx);
|
||||
oid = device_get_sysctl_tree(dev);
|
||||
|
||||
if (sc->label[0] != '\0') {
|
||||
SYSCTL_ADD_STRING(&sc->sysctl_ctx, SYSCTL_CHILDREN(oid),
|
||||
OID_AUTO, "label", CTLFLAG_RD, sc->label, 0,
|
||||
"Sensor label");
|
||||
}
|
||||
|
||||
SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(oid),
|
||||
OID_AUTO, "bus_voltage",
|
||||
CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
|
||||
sc, 0, ina2xx_sysctl_bus_voltage, "I",
|
||||
"Bus voltage (mV)");
|
||||
|
||||
SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(oid),
|
||||
OID_AUTO, "shunt_voltage",
|
||||
CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
|
||||
sc, 0, ina2xx_sysctl_shunt_voltage, "I",
|
||||
"Shunt voltage (uV)");
|
||||
|
||||
SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(oid),
|
||||
OID_AUTO, "current",
|
||||
CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
|
||||
sc, 0, ina2xx_sysctl_current, "I",
|
||||
"Current (mA)");
|
||||
|
||||
SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(oid),
|
||||
OID_AUTO, "power",
|
||||
CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
|
||||
sc, 0, ina2xx_sysctl_power, "I",
|
||||
"Power (mW)");
|
||||
|
||||
if (sc->label[0] != '\0')
|
||||
device_printf(dev, "%s: shunt=%u uOhm, "
|
||||
"current_lsb=%ld uA, power_lsb=%ld uW\n",
|
||||
sc->label, sc->rshunt,
|
||||
sc->current_lsb_uA, sc->power_lsb_uW);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
ina2xx_detach(device_t dev)
|
||||
{
|
||||
struct ina2xx_softc *sc;
|
||||
|
||||
sc = device_get_softc(dev);
|
||||
sysctl_ctx_free(&sc->sysctl_ctx);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static device_method_t ina2xx_methods[] = {
|
||||
DEVMETHOD(device_probe, ina2xx_probe),
|
||||
DEVMETHOD(device_attach, ina2xx_attach),
|
||||
DEVMETHOD(device_detach, ina2xx_detach),
|
||||
DEVMETHOD_END
|
||||
};
|
||||
|
||||
static driver_t ina2xx_driver = {
|
||||
"ina2xx",
|
||||
ina2xx_methods,
|
||||
sizeof(struct ina2xx_softc),
|
||||
};
|
||||
|
||||
DRIVER_MODULE(ina2xx, iicbus, ina2xx_driver, NULL, NULL);
|
||||
MODULE_VERSION(ina2xx, 1);
|
||||
MODULE_DEPEND(ina2xx, iicbus, IICBUS_MODVER, IICBUS_MODVER, IICBUS_MODVER);
|
||||
IICBUS_FDT_PNP_INFO(compat_data);
|
||||
19
drivers/lp5812/Makefile
Normal file
19
drivers/lp5812/Makefile
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# TI LP5812 LED Controller kernel module for FreeBSD
|
||||
#
|
||||
# Build:
|
||||
# make -C /build/opnsense-deps/drivers/lp5812 \
|
||||
# KERNBUILDDIR=/usr/obj/build/opnsense-src/arm64.aarch64/sys/MONO-GATEWAY \
|
||||
# MACHINE=arm64 MACHINE_ARCH=aarch64 CC=cc LD=ld.lld
|
||||
#
|
||||
# Copyright 2026 Mono Technologies Inc.
|
||||
# SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
SYSDIR?= ${.CURDIR}/../../../opnsense-src/sys
|
||||
|
||||
KMOD= lp5812
|
||||
SRCS= lp5812.c
|
||||
|
||||
# Auto-generated headers required for OFW/FDT + I2C driver
|
||||
SRCS+= opt_platform.h device_if.h bus_if.h iicbus_if.h ofw_bus_if.h
|
||||
|
||||
.include <bsd.kmod.mk>
|
||||
516
drivers/lp5812/lp5812.c
Normal file
516
drivers/lp5812/lp5812.c
Normal file
|
|
@ -0,0 +1,516 @@
|
|||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* Author: Tomaz Zaman <tomaz@mono.si>
|
||||
*
|
||||
* TI LP5812 I2C LED Driver for FreeBSD
|
||||
*
|
||||
* Controls up to 12 LED channels via I2C in direct (non-multiplexed) mode.
|
||||
* Each LED is exposed as /dev/led/<label> for on/off/blink control and as
|
||||
* a sysctl node (dev.lp5812.N.<label>) for PWM brightness (0-255).
|
||||
*
|
||||
* The LP5812 uses a non-standard I2C addressing scheme where register
|
||||
* address bits [9:8] are encoded in the I2C slave address field:
|
||||
* slave = base_addr | (reg[9:8] << 1) (FreeBSD 8-bit addressing)
|
||||
* data = { reg[7:0], value }
|
||||
*
|
||||
* Device tree binding:
|
||||
* lp5812@6c {
|
||||
* compatible = "ti,lp5812";
|
||||
* reg = <0x6c>;
|
||||
* #address-cells = <1>;
|
||||
* #size-cells = <0>;
|
||||
*
|
||||
* led@0 {
|
||||
* reg = <0>;
|
||||
* label = "status:white";
|
||||
* led-max-microamp = <25500>;
|
||||
* };
|
||||
* // ... more LED children
|
||||
* };
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#include "opt_platform.h"
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/bus.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/module.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/systm.h>
|
||||
|
||||
#include <dev/iicbus/iicbus.h>
|
||||
#include <dev/iicbus/iiconf.h>
|
||||
#include <dev/led/led.h>
|
||||
|
||||
#ifdef FDT
|
||||
#include <dev/ofw/openfirm.h>
|
||||
#include <dev/ofw/ofw_bus.h>
|
||||
#include <dev/ofw/ofw_bus_subr.h>
|
||||
#endif
|
||||
|
||||
#include "iicbus_if.h"
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* LP5812 Register definitions (from leds-lp5812.h)
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
#define LP5812_REG_ENABLE 0x0000
|
||||
#define LP5812_DEV_CONFIG1 0x0002
|
||||
#define LP5812_DEV_CONFIG2 0x0003
|
||||
#define LP5812_DEV_CONFIG3 0x0004
|
||||
#define LP5812_DEV_CONFIG4 0x0005
|
||||
#define LP5812_DEV_CONFIG12 0x000D
|
||||
#define LP5812_CMD_UPDATE 0x0010
|
||||
#define LP5812_LED_EN_1 0x0020
|
||||
#define LP5812_LED_EN_2 0x0021
|
||||
#define LP5812_MANUAL_DC_BASE 0x0030
|
||||
#define LP5812_MANUAL_PWM_BASE 0x0040
|
||||
#define LP5812_AUTO_DC_BASE 0x0050
|
||||
#define LP5812_TSD_CONFIG_STATUS 0x0300
|
||||
|
||||
#define LP5812_UPDATE_CMD_VAL 0x55
|
||||
#define LP5812_LSD_LOD_START_UP 0x0B
|
||||
#define LP5812_LEDS_PER_EN_REG 8
|
||||
|
||||
#define LP5812_MAX_LEDS 12
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* Data structures
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
struct lp5812_led {
|
||||
struct lp5812_softc *sc;
|
||||
struct cdev *leddev; /* /dev/led/ handle */
|
||||
char name[32]; /* DT label, e.g. "status:white" */
|
||||
int channel; /* LP5812 channel (0-11) */
|
||||
uint8_t max_current; /* led-max-microamp / 100 */
|
||||
uint8_t brightness; /* current PWM value 0-255 */
|
||||
};
|
||||
|
||||
struct lp5812_softc {
|
||||
device_t sc_dev;
|
||||
uint16_t sc_addr; /* 8-bit base I2C address */
|
||||
int sc_nleds;
|
||||
struct lp5812_led sc_leds[LP5812_MAX_LEDS];
|
||||
};
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* I2C register access
|
||||
*
|
||||
* LP5812 encodes register address bits [9:8] into the I2C slave
|
||||
* address. FreeBSD uses 8-bit addresses (7-bit << 1), so the
|
||||
* page bits go into bits [2:1] of the slave field.
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
static int
|
||||
lp5812_write_reg(struct lp5812_softc *sc, uint16_t reg, uint8_t val)
|
||||
{
|
||||
uint8_t buf[2];
|
||||
struct iic_msg msg;
|
||||
|
||||
buf[0] = reg & 0xFF;
|
||||
buf[1] = val;
|
||||
|
||||
msg.slave = sc->sc_addr | (((reg >> 8) & 0x3) << 1);
|
||||
msg.flags = IIC_M_WR;
|
||||
msg.len = 2;
|
||||
msg.buf = buf;
|
||||
|
||||
return (iicbus_transfer_excl(sc->sc_dev, &msg, 1, IIC_WAIT));
|
||||
}
|
||||
|
||||
static int
|
||||
lp5812_read_reg(struct lp5812_softc *sc, uint16_t reg, uint8_t *val)
|
||||
{
|
||||
uint8_t regbyte;
|
||||
uint16_t slave;
|
||||
struct iic_msg msgs[2];
|
||||
|
||||
slave = sc->sc_addr | (((reg >> 8) & 0x3) << 1);
|
||||
regbyte = reg & 0xFF;
|
||||
|
||||
msgs[0].slave = slave;
|
||||
msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
|
||||
msgs[0].len = 1;
|
||||
msgs[0].buf = ®byte;
|
||||
|
||||
msgs[1].slave = slave;
|
||||
msgs[1].flags = IIC_M_RD;
|
||||
msgs[1].len = 1;
|
||||
msgs[1].buf = val;
|
||||
|
||||
return (iicbus_transfer_excl(sc->sc_dev, msgs, 2, IIC_WAIT));
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* Chip control helpers
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
static int
|
||||
lp5812_update_config(struct lp5812_softc *sc)
|
||||
{
|
||||
uint8_t status;
|
||||
int err;
|
||||
|
||||
err = lp5812_write_reg(sc, LP5812_CMD_UPDATE, LP5812_UPDATE_CMD_VAL);
|
||||
if (err != 0)
|
||||
return (err);
|
||||
|
||||
err = lp5812_read_reg(sc, LP5812_TSD_CONFIG_STATUS, &status);
|
||||
if (err != 0)
|
||||
return (err);
|
||||
|
||||
if (status & 0x01) {
|
||||
device_printf(sc->sc_dev, "config error (status=0x%02x)\n",
|
||||
status);
|
||||
return (EIO);
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
lp5812_init_device(struct lp5812_softc *sc)
|
||||
{
|
||||
int err;
|
||||
|
||||
/* Stabilization delay (1ms, matches Linux driver) */
|
||||
DELAY(1100);
|
||||
|
||||
/* Enable chip */
|
||||
err = lp5812_write_reg(sc, LP5812_REG_ENABLE, 0x01);
|
||||
if (err != 0) {
|
||||
device_printf(sc->sc_dev, "chip enable failed: %d\n", err);
|
||||
return (err);
|
||||
}
|
||||
|
||||
/* Configure LSD/LOD startup detection */
|
||||
err = lp5812_write_reg(sc, LP5812_DEV_CONFIG12, LP5812_LSD_LOD_START_UP);
|
||||
if (err != 0) {
|
||||
device_printf(sc->sc_dev, "DEV_CONFIG12 write failed: %d\n",
|
||||
err);
|
||||
return (err);
|
||||
}
|
||||
|
||||
/* Direct mode: drive_mode = 0, scan_order = 0 */
|
||||
err = lp5812_write_reg(sc, LP5812_DEV_CONFIG1, 0x00);
|
||||
if (err != 0)
|
||||
return (err);
|
||||
|
||||
err = lp5812_write_reg(sc, LP5812_DEV_CONFIG2, 0x00);
|
||||
if (err != 0)
|
||||
return (err);
|
||||
|
||||
/* Apply configuration */
|
||||
err = lp5812_update_config(sc);
|
||||
if (err != 0) {
|
||||
device_printf(sc->sc_dev, "config update failed: %d\n", err);
|
||||
return (err);
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void
|
||||
lp5812_deinit_device(struct lp5812_softc *sc)
|
||||
{
|
||||
|
||||
lp5812_write_reg(sc, LP5812_LED_EN_1, 0x00);
|
||||
lp5812_write_reg(sc, LP5812_LED_EN_2, 0x00);
|
||||
lp5812_write_reg(sc, LP5812_REG_ENABLE, 0x00);
|
||||
}
|
||||
|
||||
static int
|
||||
lp5812_set_brightness(struct lp5812_softc *sc, int channel, uint8_t pwm)
|
||||
{
|
||||
|
||||
return (lp5812_write_reg(sc, LP5812_MANUAL_PWM_BASE + channel, pwm));
|
||||
}
|
||||
|
||||
static int
|
||||
lp5812_setup_led(struct lp5812_softc *sc, struct lp5812_led *led)
|
||||
{
|
||||
uint16_t en_reg;
|
||||
uint16_t mode_reg;
|
||||
uint8_t reg_val;
|
||||
int ch = led->channel;
|
||||
int err;
|
||||
|
||||
/* Set auto-DC (autonomous mode current limit) */
|
||||
err = lp5812_write_reg(sc, LP5812_AUTO_DC_BASE + ch, led->max_current);
|
||||
if (err != 0)
|
||||
return (err);
|
||||
|
||||
/* Set manual-DC (manual mode current) */
|
||||
err = lp5812_write_reg(sc, LP5812_MANUAL_DC_BASE + ch, led->max_current);
|
||||
if (err != 0)
|
||||
return (err);
|
||||
|
||||
/* Set manual mode: clear the bit for this LED in DEV_CONFIG3/4 */
|
||||
mode_reg = (ch < LP5812_LEDS_PER_EN_REG) ?
|
||||
LP5812_DEV_CONFIG3 : LP5812_DEV_CONFIG4;
|
||||
|
||||
err = lp5812_read_reg(sc, mode_reg, ®_val);
|
||||
if (err != 0)
|
||||
return (err);
|
||||
|
||||
reg_val &= ~(1 << (ch % LP5812_LEDS_PER_EN_REG));
|
||||
err = lp5812_write_reg(sc, mode_reg, reg_val);
|
||||
if (err != 0)
|
||||
return (err);
|
||||
|
||||
err = lp5812_update_config(sc);
|
||||
if (err != 0)
|
||||
return (err);
|
||||
|
||||
/* Enable this LED channel */
|
||||
en_reg = (ch < LP5812_LEDS_PER_EN_REG) ?
|
||||
LP5812_LED_EN_1 : LP5812_LED_EN_2;
|
||||
|
||||
err = lp5812_read_reg(sc, en_reg, ®_val);
|
||||
if (err != 0)
|
||||
return (err);
|
||||
|
||||
reg_val |= (1 << (ch % LP5812_LEDS_PER_EN_REG));
|
||||
err = lp5812_write_reg(sc, en_reg, reg_val);
|
||||
if (err != 0)
|
||||
return (err);
|
||||
|
||||
/* Set initial brightness to 0 (off) */
|
||||
err = lp5812_set_brightness(sc, ch, 0);
|
||||
if (err != 0)
|
||||
return (err);
|
||||
|
||||
led->brightness = 0;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* /dev/led/ callback
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
static void
|
||||
lp5812_led_control(void *priv, int onoff)
|
||||
{
|
||||
struct lp5812_led *led = priv;
|
||||
uint8_t pwm;
|
||||
|
||||
pwm = onoff ? 255 : 0;
|
||||
if (lp5812_set_brightness(led->sc, led->channel, pwm) == 0)
|
||||
led->brightness = pwm;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* Sysctl brightness handler
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
static int
|
||||
lp5812_brightness_sysctl(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
struct lp5812_led *led = (struct lp5812_led *)arg1;
|
||||
unsigned int val;
|
||||
int err;
|
||||
|
||||
val = led->brightness;
|
||||
err = sysctl_handle_int(oidp, &val, 0, req);
|
||||
if (err != 0 || req->newptr == NULL)
|
||||
return (err);
|
||||
|
||||
if (val > 255)
|
||||
val = 255;
|
||||
|
||||
err = lp5812_set_brightness(led->sc, led->channel, (uint8_t)val);
|
||||
if (err != 0)
|
||||
return (EIO);
|
||||
|
||||
led->brightness = (uint8_t)val;
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* Deferred init (called after I2C bus is fully ready)
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
static void
|
||||
lp5812_start(void *arg)
|
||||
{
|
||||
device_t dev = arg;
|
||||
struct lp5812_softc *sc = device_get_softc(dev);
|
||||
struct sysctl_ctx_list *ctx;
|
||||
struct sysctl_oid *tree;
|
||||
phandle_t node, child;
|
||||
pcell_t reg;
|
||||
uint32_t max_microamp;
|
||||
char *label;
|
||||
char sysctl_name[32];
|
||||
int nleds, i, err;
|
||||
|
||||
/* Initialize the chip */
|
||||
err = lp5812_init_device(sc);
|
||||
if (err != 0) {
|
||||
device_printf(dev, "device init failed: %d\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Count and parse DT LED children */
|
||||
node = ofw_bus_get_node(dev);
|
||||
nleds = 0;
|
||||
|
||||
for (child = OF_child(node); child != 0; child = OF_peer(child)) {
|
||||
if (nleds >= LP5812_MAX_LEDS)
|
||||
break;
|
||||
|
||||
if (OF_getencprop(child, "reg", ®, sizeof(reg)) <= 0)
|
||||
continue;
|
||||
|
||||
struct lp5812_led *led = &sc->sc_leds[nleds];
|
||||
|
||||
led->sc = sc;
|
||||
led->channel = (int)reg;
|
||||
|
||||
/* Read label */
|
||||
label = NULL;
|
||||
if (OF_getprop_alloc(child, "label", (void **)&label) == -1)
|
||||
OF_getprop_alloc(child, "name", (void **)&label);
|
||||
|
||||
if (label != NULL) {
|
||||
strlcpy(led->name, label, sizeof(led->name));
|
||||
OF_prop_free(label);
|
||||
} else {
|
||||
snprintf(led->name, sizeof(led->name), "lp5812_%d",
|
||||
led->channel);
|
||||
}
|
||||
|
||||
/* Read max current (microamps → value/100 for register) */
|
||||
max_microamp = 25500; /* default */
|
||||
OF_getencprop(child, "led-max-microamp", &max_microamp,
|
||||
sizeof(max_microamp));
|
||||
led->max_current = (uint8_t)(max_microamp / 100);
|
||||
|
||||
/* Configure this LED channel on the chip */
|
||||
err = lp5812_setup_led(sc, led);
|
||||
if (err != 0) {
|
||||
device_printf(dev, "%s: setup failed: %d\n",
|
||||
led->name, err);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Create /dev/led/<label> */
|
||||
led->leddev = led_create_state(lp5812_led_control, led,
|
||||
led->name, 0);
|
||||
if (led->leddev == NULL)
|
||||
device_printf(dev, "%s: led_create failed\n",
|
||||
led->name);
|
||||
|
||||
nleds++;
|
||||
}
|
||||
|
||||
sc->sc_nleds = nleds;
|
||||
|
||||
/* Create sysctl brightness nodes */
|
||||
ctx = device_get_sysctl_ctx(dev);
|
||||
tree = device_get_sysctl_tree(dev);
|
||||
|
||||
for (i = 0; i < sc->sc_nleds; i++) {
|
||||
struct lp5812_led *led = &sc->sc_leds[i];
|
||||
|
||||
/* Convert "status:red" → "status_red" for sysctl name */
|
||||
strlcpy(sysctl_name, led->name, sizeof(sysctl_name));
|
||||
for (char *p = sysctl_name; *p != '\0'; p++) {
|
||||
if (*p == ':' || *p == '-' || *p == '.')
|
||||
*p = '_';
|
||||
}
|
||||
|
||||
SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
|
||||
sysctl_name,
|
||||
CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
|
||||
led, 0, lp5812_brightness_sysctl, "IU",
|
||||
"LED brightness (0-255)");
|
||||
}
|
||||
|
||||
device_printf(dev, "%d LED(s) configured\n", sc->sc_nleds);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* Device methods
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
#ifdef FDT
|
||||
static struct ofw_compat_data compat_data[] = {
|
||||
{ "ti,lp5812", 1 },
|
||||
{ NULL, 0 }
|
||||
};
|
||||
#endif
|
||||
|
||||
static int
|
||||
lp5812_probe(device_t dev)
|
||||
{
|
||||
|
||||
#ifdef FDT
|
||||
if (!ofw_bus_status_okay(dev))
|
||||
return (ENXIO);
|
||||
if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
|
||||
return (ENXIO);
|
||||
#endif
|
||||
|
||||
device_set_desc(dev, "TI LP5812 LED Controller");
|
||||
return (BUS_PROBE_DEFAULT);
|
||||
}
|
||||
|
||||
static int
|
||||
lp5812_attach(device_t dev)
|
||||
{
|
||||
struct lp5812_softc *sc;
|
||||
|
||||
sc = device_get_softc(dev);
|
||||
sc->sc_dev = dev;
|
||||
sc->sc_addr = iicbus_get_addr(dev);
|
||||
|
||||
/* Defer init until I2C bus is fully operational */
|
||||
config_intrhook_oneshot(lp5812_start, dev);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
lp5812_detach(device_t dev)
|
||||
{
|
||||
struct lp5812_softc *sc = device_get_softc(dev);
|
||||
int i;
|
||||
|
||||
/* Destroy /dev/led/ devices */
|
||||
for (i = 0; i < sc->sc_nleds; i++) {
|
||||
if (sc->sc_leds[i].leddev != NULL)
|
||||
led_destroy(sc->sc_leds[i].leddev);
|
||||
}
|
||||
|
||||
/* Turn off all LEDs and disable chip */
|
||||
lp5812_deinit_device(sc);
|
||||
|
||||
device_printf(dev, "detached\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
static device_method_t lp5812_methods[] = {
|
||||
DEVMETHOD(device_probe, lp5812_probe),
|
||||
DEVMETHOD(device_attach, lp5812_attach),
|
||||
DEVMETHOD(device_detach, lp5812_detach),
|
||||
DEVMETHOD_END
|
||||
};
|
||||
|
||||
static driver_t lp5812_driver = {
|
||||
"lp5812",
|
||||
lp5812_methods,
|
||||
sizeof(struct lp5812_softc),
|
||||
};
|
||||
|
||||
DRIVER_MODULE(lp5812, iicbus, lp5812_driver, NULL, NULL);
|
||||
MODULE_VERSION(lp5812, 1);
|
||||
MODULE_DEPEND(lp5812, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
|
||||
#ifdef FDT
|
||||
IICBUS_FDT_PNP_INFO(compat_data);
|
||||
#endif
|
||||
197
drivers/lp5812/lp5812.h
Normal file
197
drivers/lp5812/lp5812.h
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
/*
|
||||
* LP5812 Driver Header
|
||||
*
|
||||
* Copyright (C) 2025 Texas Instruments
|
||||
*
|
||||
* Author: Jared Zhou <jared-zhou@ti.com>
|
||||
*/
|
||||
|
||||
#ifndef _LP5812_H_
|
||||
#define _LP5812_H_
|
||||
|
||||
#include <linux/delay.h>
|
||||
#include <linux/i2c.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/led-class-multicolor.h>
|
||||
#include <linux/leds.h>
|
||||
#include <linux/mutex.h>
|
||||
#include <linux/sysfs.h>
|
||||
#include <linux/types.h>
|
||||
|
||||
#define LP5812_REG_ENABLE 0x0000
|
||||
#define LP5812_REG_RESET 0x0023
|
||||
#define LP5812_DEV_CONFIG0 0x0001
|
||||
#define LP5812_DEV_CONFIG1 0x0002
|
||||
#define LP5812_DEV_CONFIG2 0x0003
|
||||
#define LP5812_DEV_CONFIG3 0x0004
|
||||
#define LP5812_DEV_CONFIG4 0x0005
|
||||
#define LP5812_DEV_CONFIG5 0x0006
|
||||
#define LP5812_DEV_CONFIG6 0x0007
|
||||
#define LP5812_DEV_CONFIG7 0x0008
|
||||
#define LP5812_DEV_CONFIG8 0x0009
|
||||
#define LP5812_DEV_CONFIG9 0x000A
|
||||
#define LP5812_DEV_CONFIG10 0x000B
|
||||
#define LP5812_DEV_CONFIG11 0x000c
|
||||
#define LP5812_DEV_CONFIG12 0x000D
|
||||
#define LP5812_CMD_UPDATE 0x0010
|
||||
#define LP5812_LED_EN_1 0x0020
|
||||
#define LP5812_LED_EN_2 0x0021
|
||||
#define LP5812_FAULT_CLEAR 0x0022
|
||||
#define LP5812_MANUAL_DC_BASE 0x0030
|
||||
#define LP5812_AUTO_DC_BASE 0x0050
|
||||
#define LP5812_MANUAL_PWM_BASE 0x0040
|
||||
|
||||
#define LP5812_TSD_CONFIG_STATUS 0x0300
|
||||
#define LP5812_LOD_STATUS 0x0301
|
||||
#define LP5812_LSD_STATUS 0x0303
|
||||
|
||||
#define LP5812_ENABLE_DEFAULT 0x01
|
||||
#define FAULT_CLEAR_ALL 0x07
|
||||
#define TSD_CLEAR_VAL 0x04
|
||||
#define LSD_CLEAR_VAL 0x02
|
||||
#define LOD_CLEAR_VAL 0x01
|
||||
#define LP5812_RESET 0x66
|
||||
#define LP5812_DEV_CONFIG12_DEFAULT 0x08
|
||||
|
||||
#define LP5812_UPDATE_CMD_VAL 0x55
|
||||
#define LP5812_REG_ADDR_HIGH_SHIFT 8
|
||||
#define LP5812_REG_ADDR_BIT_8_9_MASK 0x03
|
||||
#define LP5812_REG_ADDR_LOW_MASK 0xFF
|
||||
#define LP5812_CHIP_ADDR_SHIFT 0 /* no address shift for LP5810 */
|
||||
#define LP5812_DATA_LENGTH 2
|
||||
#define LP5812_DATA_BYTE_0_IDX 0
|
||||
#define LP5812_DATA_BYTE_1_IDX 1
|
||||
|
||||
#define LP5812_READ_MSG_LENGTH 2
|
||||
#define LP5812_MSG_0_IDX 0
|
||||
#define LP5812_MSG_1_IDX 1
|
||||
#define LP5812_CFG_ERR_STATUS_MASK 0x01
|
||||
#define LP5812_CFG_TSD_STATUS_SHIFT 1
|
||||
#define LP5812_CFG_TSD_STATUS_MASK 0x01
|
||||
|
||||
#define LP5812_FAULT_CLEAR_LOD 0
|
||||
#define LP5812_FAULT_CLEAR_LSD 1
|
||||
#define LP5812_FAULT_CLEAR_TSD 2
|
||||
#define LP5812_FAULT_CLEAR_ALL 3
|
||||
#define LP5812_NUMBER_LED_IN_REG 8
|
||||
|
||||
#define LP5812_WAIT_DEVICE_STABLE_MIN 1000
|
||||
#define LP5812_WAIT_DEVICE_STABLE_MAX 1100
|
||||
|
||||
#define LP5812_LSD_LOD_START_UP 0x0B
|
||||
#define LP5812_MODE_NAME_MAX_LEN 20
|
||||
#define LP5812_MODE_DIRECT_NAME "direct_mode"
|
||||
#define LP5812_MODE_DIRECT_VALUE 0
|
||||
#define LP5812_MODE_MIX_SELECT_LED_0 0
|
||||
#define LP5812_MODE_MIX_SELECT_LED_1 1
|
||||
#define LP5812_MODE_MIX_SELECT_LED_2 2
|
||||
#define LP5812_MODE_MIX_SELECT_LED_3 3
|
||||
|
||||
enum control_mode {
|
||||
LP5812_MODE_MANUAL = 0,
|
||||
LP5812_MODE_AUTONOMOUS
|
||||
};
|
||||
|
||||
enum dimming_type {
|
||||
LP5812_DIMMING_ANALOG,
|
||||
LP5812_DIMMING_PWM
|
||||
};
|
||||
|
||||
union u_scan_order {
|
||||
struct {
|
||||
u8 scan_order_0:2;
|
||||
u8 scan_order_1:2;
|
||||
u8 scan_order_2:2;
|
||||
u8 scan_order_3:2;
|
||||
} s_scan_order;
|
||||
u8 scan_order_val;
|
||||
};
|
||||
|
||||
union u_drive_mode {
|
||||
struct {
|
||||
u8 mix_sel_led_0:1;
|
||||
u8 mix_sel_led_1:1;
|
||||
u8 mix_sel_led_2:1;
|
||||
u8 mix_sel_led_3:1;
|
||||
u8 led_mode:3;
|
||||
u8 pwm_fre:1;
|
||||
} s_drive_mode;
|
||||
u8 drive_mode_val;
|
||||
};
|
||||
|
||||
struct lp5812_reg {
|
||||
u16 addr;
|
||||
union {
|
||||
u8 val;
|
||||
u8 mask;
|
||||
u8 shift;
|
||||
};
|
||||
};
|
||||
|
||||
struct lp5812_mode_mapping {
|
||||
char mode_name[LP5812_MODE_NAME_MAX_LEN];
|
||||
u8 mode;
|
||||
u8 scan_order_0;
|
||||
u8 scan_order_1;
|
||||
u8 scan_order_2;
|
||||
u8 scan_order_3;
|
||||
u8 selection_led;
|
||||
};
|
||||
|
||||
struct lp5812_led_config {
|
||||
bool is_sc_led;
|
||||
const char *name;
|
||||
u8 color_id[LED_COLOR_ID_MAX];
|
||||
u32 max_current[LED_COLOR_ID_MAX];
|
||||
int chan_nr;
|
||||
int num_colors;
|
||||
int led_id[LED_COLOR_ID_MAX];
|
||||
};
|
||||
|
||||
struct lp5812_chip {
|
||||
u8 num_channels;
|
||||
struct i2c_client *client;
|
||||
struct mutex lock; /* Protects register access */
|
||||
struct lp5812_led_config *led_config;
|
||||
const char *label;
|
||||
const char *scan_mode;
|
||||
const struct lp5812_device_config *cfg;
|
||||
union u_scan_order u_scan_order;
|
||||
union u_drive_mode u_drive_mode;
|
||||
};
|
||||
|
||||
struct lp5812_led {
|
||||
u8 brightness;
|
||||
int chan_nr;
|
||||
struct led_classdev cdev;
|
||||
struct led_classdev_mc mc_cdev;
|
||||
struct lp5812_chip *chip;
|
||||
};
|
||||
|
||||
struct lp5812_device_config {
|
||||
const struct lp5812_reg reg_reset;
|
||||
const struct lp5812_reg reg_chip_en;
|
||||
const struct lp5812_reg reg_dev_config_0;
|
||||
const struct lp5812_reg reg_dev_config_1;
|
||||
const struct lp5812_reg reg_dev_config_2;
|
||||
const struct lp5812_reg reg_dev_config_3;
|
||||
const struct lp5812_reg reg_dev_config_4;
|
||||
const struct lp5812_reg reg_dev_config_5;
|
||||
const struct lp5812_reg reg_dev_config_6;
|
||||
const struct lp5812_reg reg_dev_config_7;
|
||||
const struct lp5812_reg reg_dev_config_12;
|
||||
const struct lp5812_reg reg_cmd_update;
|
||||
|
||||
const struct lp5812_reg reg_led_en_1;
|
||||
const struct lp5812_reg reg_led_en_2;
|
||||
const struct lp5812_reg reg_fault_clear;
|
||||
const struct lp5812_reg reg_manual_dc_base;
|
||||
const struct lp5812_reg reg_auto_dc_base;
|
||||
const struct lp5812_reg reg_manual_pwm_base;
|
||||
const struct lp5812_reg reg_tsd_config_status;
|
||||
const struct lp5812_reg reg_lod_status_base;
|
||||
const struct lp5812_reg reg_lsd_status_base;
|
||||
};
|
||||
|
||||
#endif /*_LP5812_H_*/
|
||||
19
drivers/sfp-led/Makefile
Normal file
19
drivers/sfp-led/Makefile
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# SFP LED Control kernel module for FreeBSD
|
||||
#
|
||||
# Build:
|
||||
# make -C /build/opnsense-deps/drivers/sfp-led \
|
||||
# KERNBUILDDIR=/usr/obj/build/opnsense-src/arm64.aarch64/sys/MONO-GATEWAY \
|
||||
# MACHINE=arm64 MACHINE_ARCH=aarch64 CC=cc LD=ld.lld
|
||||
#
|
||||
# Copyright 2026 Mono Technologies Inc.
|
||||
# SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
SYSDIR?= ${.CURDIR}/../../../opnsense-src/sys
|
||||
|
||||
KMOD= sfpled
|
||||
SRCS= sfp_led.c
|
||||
|
||||
# Auto-generated headers required for OFW/FDT + GPIO + I2C driver
|
||||
SRCS+= opt_platform.h device_if.h bus_if.h ofw_bus_if.h gpiobus_if.h iicbus_if.h
|
||||
|
||||
.include <bsd.kmod.mk>
|
||||
629
drivers/sfp-led/sfp_led.c
Normal file
629
drivers/sfp-led/sfp_led.c
Normal file
|
|
@ -0,0 +1,629 @@
|
|||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* Author: Tomaz Zaman <tomaz@mono.si>
|
||||
*
|
||||
* SFP LED Control Driver for FreeBSD (Passive Monitor)
|
||||
*
|
||||
* Controls SFP port LEDs based on module presence and link state.
|
||||
* This driver passively monitors GPIO signals and network interface
|
||||
* state without interfering with the SFP subsystem or MAC driver.
|
||||
*
|
||||
* Detection method:
|
||||
* - Module presence: mod-def0 GPIO (active-low, directly from SFP cage)
|
||||
* - Link state: LOS GPIO (fiber) and/or netdev link state (DAC)
|
||||
* - Activity: Poll netdev tx/rx packet counters
|
||||
* - Cable type: I2C EEPROM bytes 3/8 for DAC vs fiber classification
|
||||
*
|
||||
* LED behavior:
|
||||
* State | Green (Link) | Orange (Activity)
|
||||
* ---------------------------|--------------|-------------------
|
||||
* No module | OFF | OFF
|
||||
* Module present, no link | OFF | ON (solid)
|
||||
* Module present, link up | ON | Blinks on traffic
|
||||
*
|
||||
* Device tree binding:
|
||||
* sfp-led-controller {
|
||||
* compatible = "mono,sfp-led";
|
||||
* sfp-ports = <&sfp_xfi0>, <&sfp_xfi1>;
|
||||
* };
|
||||
*
|
||||
* // SFP nodes must have leds, mod-def0-gpios, los-gpios, i2c-bus
|
||||
* // LED nodes must have gpios property
|
||||
* // MAC nodes must have sfp = <&sfp_xfiN> for netdev association
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/bus.h>
|
||||
#include <sys/module.h>
|
||||
#include <sys/malloc.h>
|
||||
#include <sys/taskqueue.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/gpio.h>
|
||||
|
||||
#include <net/if.h>
|
||||
#include <net/if_var.h>
|
||||
#include <net/vnet.h>
|
||||
|
||||
#include <dev/ofw/ofw_bus.h>
|
||||
#include <dev/ofw/ofw_bus_subr.h>
|
||||
#include <dev/ofw/openfirm.h>
|
||||
|
||||
#include <dev/gpio/gpiobusvar.h>
|
||||
|
||||
#include <dev/iicbus/iicbus.h>
|
||||
#include <dev/iicbus/iiconf.h>
|
||||
|
||||
#define SFPLED_POLL_MS 100
|
||||
#define SFPLED_MAX_PORTS 2
|
||||
#define SFPLED_MAX_IFP_RETRIES 60 /* 6 seconds at 100ms */
|
||||
|
||||
/* SFP EEPROM A0h (address 0x50 = 0xA0 in 8-bit) */
|
||||
#define SFP_I2C_ADDR 0xA0
|
||||
#define SFP_COMPLIANCE_3 3 /* 10G/1G Ethernet compliance */
|
||||
#define SFP_COMPLIANCE_8 8 /* SFP+ cable technology */
|
||||
|
||||
/* Byte 3 bits */
|
||||
#define SFP_IF_1X_COPPER_PASSIVE 0x01
|
||||
#define SFP_IF_1X_COPPER_ACTIVE 0x02
|
||||
|
||||
/* Byte 8 bits */
|
||||
#define SFP_CT_PASSIVE 0x04
|
||||
#define SFP_CT_ACTIVE 0x08
|
||||
|
||||
struct sfp_led_port {
|
||||
struct sfp_led_softc *sc;
|
||||
phandle_t sfp_node; /* SFP DT node handle */
|
||||
pcell_t sfp_xref; /* SFP DT xref (raw phandle value) */
|
||||
|
||||
/* GPIO pins (acquired lazily from SFP + LED DT nodes) */
|
||||
gpio_pin_t mod_def0; /* module present (active-low) */
|
||||
gpio_pin_t los; /* loss of signal (active-high) */
|
||||
gpio_pin_t link_led; /* green LED */
|
||||
gpio_pin_t activity_led; /* orange LED */
|
||||
bool gpio_acquired;
|
||||
bool gpio_failed; /* permanent failure, stop retrying */
|
||||
|
||||
/* LED DT node phandles (read at attach, used for GPIO acquisition) */
|
||||
pcell_t link_led_xref;
|
||||
pcell_t activity_led_xref;
|
||||
|
||||
/* I2C bus (resolved lazily from SFP node's i2c-bus property) */
|
||||
pcell_t i2c_xref; /* i2c-bus phandle from DT */
|
||||
device_t iicbus; /* resolved I2C bus device */
|
||||
bool i2c_resolved;
|
||||
bool i2c_valid; /* true if i2c_xref was found in DT */
|
||||
|
||||
/* Network interface (resolved lazily) */
|
||||
if_t ifp;
|
||||
int ifp_retries;
|
||||
|
||||
/* Cached state */
|
||||
bool last_present;
|
||||
bool last_link;
|
||||
bool is_dac;
|
||||
uint64_t last_tx_pkts;
|
||||
uint64_t last_rx_pkts;
|
||||
bool activity_on;
|
||||
|
||||
int index;
|
||||
};
|
||||
|
||||
struct sfp_led_softc {
|
||||
device_t dev;
|
||||
int nports;
|
||||
struct sfp_led_port ports[SFPLED_MAX_PORTS];
|
||||
struct timeout_task poll_task;
|
||||
};
|
||||
|
||||
static int sfpled_probe(device_t dev);
|
||||
static int sfpled_attach(device_t dev);
|
||||
static int sfpled_detach(device_t dev);
|
||||
static void sfpled_poll(void *arg, int pending);
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* GPIO acquisition (lazy — gpiobus may not be ready at attach time)
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
static bool
|
||||
sfpled_acquire_gpios(struct sfp_led_port *port)
|
||||
{
|
||||
device_t dev = port->sc->dev;
|
||||
phandle_t led_node;
|
||||
int err;
|
||||
|
||||
/* Acquire mod-def0 GPIO from SFP node */
|
||||
err = gpio_pin_get_by_ofw_property(dev, port->sfp_node,
|
||||
"mod-def0-gpios", &port->mod_def0);
|
||||
if (err != 0) {
|
||||
if (err == ENODEV)
|
||||
return (false); /* gpiobus not ready, retry later */
|
||||
device_printf(dev, "sfp%d: mod-def0 GPIO error: %d\n",
|
||||
port->index, err);
|
||||
port->gpio_failed = true;
|
||||
return (false);
|
||||
}
|
||||
|
||||
/* Acquire LOS GPIO from SFP node */
|
||||
err = gpio_pin_get_by_ofw_property(dev, port->sfp_node,
|
||||
"los-gpios", &port->los);
|
||||
if (err != 0) {
|
||||
device_printf(dev, "sfp%d: los GPIO error: %d\n",
|
||||
port->index, err);
|
||||
gpio_pin_release(port->mod_def0);
|
||||
port->mod_def0 = NULL;
|
||||
port->gpio_failed = true;
|
||||
return (false);
|
||||
}
|
||||
|
||||
/* Acquire link LED GPIO from LED DT node */
|
||||
led_node = OF_node_from_xref(port->link_led_xref);
|
||||
err = gpio_pin_get_by_ofw_idx(dev, led_node, 0, &port->link_led);
|
||||
if (err != 0) {
|
||||
device_printf(dev, "sfp%d: link LED GPIO error: %d\n",
|
||||
port->index, err);
|
||||
gpio_pin_release(port->los);
|
||||
port->los = NULL;
|
||||
gpio_pin_release(port->mod_def0);
|
||||
port->mod_def0 = NULL;
|
||||
port->gpio_failed = true;
|
||||
return (false);
|
||||
}
|
||||
gpio_pin_setflags(port->link_led, GPIO_PIN_OUTPUT);
|
||||
|
||||
/* Acquire activity LED GPIO from LED DT node */
|
||||
led_node = OF_node_from_xref(port->activity_led_xref);
|
||||
err = gpio_pin_get_by_ofw_idx(dev, led_node, 0, &port->activity_led);
|
||||
if (err != 0) {
|
||||
device_printf(dev, "sfp%d: activity LED GPIO error: %d\n",
|
||||
port->index, err);
|
||||
gpio_pin_release(port->link_led);
|
||||
port->link_led = NULL;
|
||||
gpio_pin_release(port->los);
|
||||
port->los = NULL;
|
||||
gpio_pin_release(port->mod_def0);
|
||||
port->mod_def0 = NULL;
|
||||
port->gpio_failed = true;
|
||||
return (false);
|
||||
}
|
||||
gpio_pin_setflags(port->activity_led, GPIO_PIN_OUTPUT);
|
||||
|
||||
port->gpio_acquired = true;
|
||||
device_printf(dev, "sfp%d: GPIOs acquired\n", port->index);
|
||||
|
||||
return (true);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* I2C helpers (for DAC cable detection)
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
static void
|
||||
sfpled_resolve_i2c(struct sfp_led_port *port)
|
||||
{
|
||||
port->i2c_resolved = true;
|
||||
|
||||
if (!port->i2c_valid)
|
||||
return;
|
||||
|
||||
port->iicbus = OF_device_from_xref(port->i2c_xref);
|
||||
if (port->iicbus == NULL) {
|
||||
device_printf(port->sc->dev,
|
||||
"sfp%d: i2c-bus not yet available\n", port->index);
|
||||
port->i2c_resolved = false; /* retry next poll */
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
sfpled_i2c_read_byte(struct sfp_led_port *port, uint8_t reg, uint8_t *val)
|
||||
{
|
||||
struct iic_msg msgs[2];
|
||||
int err;
|
||||
|
||||
if (port->iicbus == NULL)
|
||||
return (ENXIO);
|
||||
|
||||
msgs[0].slave = SFP_I2C_ADDR;
|
||||
msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
|
||||
msgs[0].len = 1;
|
||||
msgs[0].buf = ®
|
||||
|
||||
msgs[1].slave = SFP_I2C_ADDR;
|
||||
msgs[1].flags = IIC_M_RD;
|
||||
msgs[1].len = 1;
|
||||
msgs[1].buf = val;
|
||||
|
||||
err = iicbus_request_bus(port->iicbus, port->sc->dev, IIC_WAIT);
|
||||
if (err != 0)
|
||||
return (err);
|
||||
|
||||
err = iicbus_transfer(port->iicbus, msgs, 2);
|
||||
|
||||
iicbus_release_bus(port->iicbus, port->sc->dev);
|
||||
return (err);
|
||||
}
|
||||
|
||||
static bool
|
||||
sfpled_is_dac(struct sfp_led_port *port)
|
||||
{
|
||||
uint8_t byte3, byte8;
|
||||
|
||||
if (sfpled_i2c_read_byte(port, SFP_COMPLIANCE_3, &byte3) != 0)
|
||||
return (false);
|
||||
|
||||
if (sfpled_i2c_read_byte(port, SFP_COMPLIANCE_8, &byte8) != 0)
|
||||
return (false);
|
||||
|
||||
return ((byte3 & (SFP_IF_1X_COPPER_PASSIVE | SFP_IF_1X_COPPER_ACTIVE)) ||
|
||||
(byte8 & (SFP_CT_PASSIVE | SFP_CT_ACTIVE)));
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* Network interface lookup (lazy — dtsec may not be ready yet)
|
||||
*
|
||||
* Must be called outside NET_EPOCH or will wrap internally.
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
static void
|
||||
sfpled_find_ifp(struct sfp_led_port *port)
|
||||
{
|
||||
devclass_t dc;
|
||||
device_t dev;
|
||||
phandle_t node;
|
||||
pcell_t sfp_ref;
|
||||
char ifname[IFNAMSIZ];
|
||||
if_t ifp;
|
||||
int unit, maxunit;
|
||||
|
||||
/*
|
||||
* The DPAA dtsec driver registers as devclass "dtsec" but names
|
||||
* its interfaces "ethN". Walk the dtsec devclass to find the
|
||||
* device whose DT node has an "sfp" property matching our SFP
|
||||
* phandle, then look up the interface by constructed name.
|
||||
*/
|
||||
dc = devclass_find("dtsec");
|
||||
if (dc == NULL)
|
||||
return;
|
||||
|
||||
maxunit = devclass_get_maxunit(dc);
|
||||
for (unit = 0; unit < maxunit; unit++) {
|
||||
dev = devclass_get_device(dc, unit);
|
||||
if (dev == NULL)
|
||||
continue;
|
||||
|
||||
node = ofw_bus_get_node(dev);
|
||||
if (node <= 0)
|
||||
continue;
|
||||
|
||||
if (OF_getencprop(node, "sfp", &sfp_ref,
|
||||
sizeof(sfp_ref)) <= 0)
|
||||
continue;
|
||||
|
||||
if (OF_node_from_xref(sfp_ref) != port->sfp_node)
|
||||
continue;
|
||||
|
||||
/* Found matching device — look up its network interface.
|
||||
* ifunit_ref needs CURVNET context (VIMAGE taskqueue
|
||||
* threads have curvnet=NULL). */
|
||||
snprintf(ifname, sizeof(ifname), "%s%d",
|
||||
device_get_name(dev), device_get_unit(dev));
|
||||
CURVNET_SET(vnet0);
|
||||
ifp = ifunit_ref(ifname);
|
||||
CURVNET_RESTORE();
|
||||
if (ifp != NULL) {
|
||||
port->ifp = ifp;
|
||||
device_printf(port->sc->dev,
|
||||
"sfp%d: associated with %s\n",
|
||||
port->index, ifname);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* LED control helpers
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
static void
|
||||
sfpled_set_link(struct sfp_led_port *port, bool on)
|
||||
{
|
||||
|
||||
if (port->link_led != NULL)
|
||||
gpio_pin_set_active(port->link_led, on);
|
||||
}
|
||||
|
||||
static void
|
||||
sfpled_set_activity(struct sfp_led_port *port, bool on)
|
||||
{
|
||||
|
||||
if (port->activity_led != NULL)
|
||||
gpio_pin_set_active(port->activity_led, on);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* Poll handler — runs every 100ms in taskqueue_thread
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
static void
|
||||
sfpled_poll_port(struct sfp_led_port *port)
|
||||
{
|
||||
bool present, link;
|
||||
|
||||
/* Lazy GPIO acquisition */
|
||||
if (!port->gpio_acquired) {
|
||||
if (port->gpio_failed)
|
||||
return;
|
||||
if (!sfpled_acquire_gpios(port))
|
||||
return;
|
||||
}
|
||||
|
||||
/* Module presence via mod-def0 GPIO (active-low: active=true means
|
||||
* present, because gpio_pin_is_active accounts for GPIO_ACTIVE_LOW) */
|
||||
gpio_pin_is_active(port->mod_def0, &present);
|
||||
|
||||
/* Handle module insertion/removal */
|
||||
if (present != port->last_present) {
|
||||
port->last_present = present;
|
||||
|
||||
if (!present) {
|
||||
/* Module removed */
|
||||
sfpled_set_link(port, false);
|
||||
sfpled_set_activity(port, false);
|
||||
port->last_link = false;
|
||||
port->is_dac = false;
|
||||
port->activity_on = false;
|
||||
port->last_tx_pkts = 0;
|
||||
port->last_rx_pkts = 0;
|
||||
} else {
|
||||
/* Module inserted — detect cable type, show present */
|
||||
sfpled_set_link(port, false);
|
||||
sfpled_set_activity(port, true);
|
||||
|
||||
/* Lazy I2C resolution */
|
||||
if (!port->i2c_resolved)
|
||||
sfpled_resolve_i2c(port);
|
||||
|
||||
if (port->iicbus != NULL) {
|
||||
port->is_dac = sfpled_is_dac(port);
|
||||
device_printf(port->sc->dev,
|
||||
"sfp%d: module inserted (%s)\n",
|
||||
port->index,
|
||||
port->is_dac ? "DAC" : "fiber");
|
||||
} else {
|
||||
device_printf(port->sc->dev,
|
||||
"sfp%d: module inserted (no I2C)\n",
|
||||
port->index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!present)
|
||||
return;
|
||||
|
||||
/* Lazy netdev lookup */
|
||||
if (port->ifp == NULL && port->ifp_retries < SFPLED_MAX_IFP_RETRIES) {
|
||||
sfpled_find_ifp(port);
|
||||
port->ifp_retries++;
|
||||
}
|
||||
|
||||
/*
|
||||
* Link detection:
|
||||
* - Fiber: LOS GPIO (active-high, so active=true means signal LOST)
|
||||
* - DAC: netdev link state (LOS may not be connected on DAC cables)
|
||||
* - Combined: link if LOS clear OR netdev reports LINK_STATE_UP
|
||||
*/
|
||||
link = false;
|
||||
|
||||
/* Check LOS GPIO — works reliably for fiber */
|
||||
{
|
||||
bool los_active;
|
||||
gpio_pin_is_active(port->los, &los_active);
|
||||
if (!los_active)
|
||||
link = true; /* LOS not asserted = signal present */
|
||||
}
|
||||
|
||||
/* Also check netdev link state — covers DAC and all cases */
|
||||
if (!link && port->ifp != NULL) {
|
||||
if (if_getlinkstate(port->ifp) == LINK_STATE_UP)
|
||||
link = true;
|
||||
}
|
||||
|
||||
/* Handle link state changes */
|
||||
if (link != port->last_link) {
|
||||
port->last_link = link;
|
||||
sfpled_set_link(port, link);
|
||||
|
||||
if (link) {
|
||||
/* Link up — start activity monitoring */
|
||||
sfpled_set_activity(port, false);
|
||||
port->last_tx_pkts = 0;
|
||||
port->last_rx_pkts = 0;
|
||||
port->activity_on = false;
|
||||
} else {
|
||||
/* Link down — solid activity to show module present */
|
||||
sfpled_set_activity(port, true);
|
||||
port->activity_on = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!link)
|
||||
return;
|
||||
|
||||
/* Activity monitoring — blink on traffic */
|
||||
if (port->ifp != NULL &&
|
||||
(if_getflags(port->ifp) & IFF_UP) != 0) {
|
||||
uint64_t tx = if_getcounter(port->ifp, IFCOUNTER_OPACKETS);
|
||||
uint64_t rx = if_getcounter(port->ifp, IFCOUNTER_IPACKETS);
|
||||
|
||||
if (tx != port->last_tx_pkts || rx != port->last_rx_pkts) {
|
||||
/* Traffic — toggle LED for visible blink */
|
||||
port->activity_on = !port->activity_on;
|
||||
sfpled_set_activity(port, port->activity_on);
|
||||
port->last_tx_pkts = tx;
|
||||
port->last_rx_pkts = rx;
|
||||
} else if (port->activity_on) {
|
||||
/* No traffic — turn off */
|
||||
port->activity_on = false;
|
||||
sfpled_set_activity(port, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
sfpled_poll(void *arg, int pending)
|
||||
{
|
||||
struct sfp_led_softc *sc = arg;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < sc->nports; i++)
|
||||
sfpled_poll_port(&sc->ports[i]);
|
||||
|
||||
taskqueue_enqueue_timeout(taskqueue_thread, &sc->poll_task,
|
||||
hz / (1000 / SFPLED_POLL_MS));
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* Device methods
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
static int
|
||||
sfpled_probe(device_t dev)
|
||||
{
|
||||
if (!ofw_bus_status_okay(dev))
|
||||
return (ENXIO);
|
||||
if (!ofw_bus_is_compatible(dev, "mono,sfp-led"))
|
||||
return (ENXIO);
|
||||
|
||||
device_set_desc(dev, "SFP LED Controller");
|
||||
return (BUS_PROBE_DEFAULT);
|
||||
}
|
||||
|
||||
static int
|
||||
sfpled_attach(device_t dev)
|
||||
{
|
||||
struct sfp_led_softc *sc;
|
||||
phandle_t node;
|
||||
pcell_t sfp_phandles[SFPLED_MAX_PORTS];
|
||||
pcell_t led_phandles[2];
|
||||
ssize_t len;
|
||||
int nports, i, registered;
|
||||
|
||||
sc = device_get_softc(dev);
|
||||
sc->dev = dev;
|
||||
|
||||
node = ofw_bus_get_node(dev);
|
||||
|
||||
/* Read sfp-ports phandle array */
|
||||
len = OF_getencprop(node, "sfp-ports", sfp_phandles,
|
||||
sizeof(sfp_phandles));
|
||||
if (len <= 0) {
|
||||
device_printf(dev, "no sfp-ports property\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
nports = len / sizeof(pcell_t);
|
||||
if (nports > SFPLED_MAX_PORTS)
|
||||
nports = SFPLED_MAX_PORTS;
|
||||
|
||||
registered = 0;
|
||||
for (i = 0; i < nports; i++) {
|
||||
struct sfp_led_port *port = &sc->ports[i];
|
||||
|
||||
port->sc = sc;
|
||||
port->index = i;
|
||||
port->sfp_xref = sfp_phandles[i];
|
||||
port->sfp_node = OF_node_from_xref(sfp_phandles[i]);
|
||||
|
||||
if (port->sfp_node <= 0) {
|
||||
device_printf(dev, "sfp%d: invalid phandle\n", i);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Read LED phandles from SFP node's "leds" property */
|
||||
len = OF_getencprop(port->sfp_node, "leds", led_phandles,
|
||||
sizeof(led_phandles));
|
||||
if (len < (ssize_t)(2 * sizeof(pcell_t))) {
|
||||
device_printf(dev,
|
||||
"sfp%d: need 2 LED phandles in leds property\n",
|
||||
i);
|
||||
continue;
|
||||
}
|
||||
port->link_led_xref = led_phandles[0];
|
||||
port->activity_led_xref = led_phandles[1];
|
||||
|
||||
/* Read i2c-bus phandle from SFP node */
|
||||
if (OF_getencprop(port->sfp_node, "i2c-bus",
|
||||
&port->i2c_xref, sizeof(port->i2c_xref)) > 0) {
|
||||
port->i2c_valid = true;
|
||||
}
|
||||
|
||||
registered++;
|
||||
}
|
||||
|
||||
if (registered == 0) {
|
||||
device_printf(dev, "no valid SFP ports\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
sc->nports = nports;
|
||||
|
||||
/* Start polling */
|
||||
TIMEOUT_TASK_INIT(taskqueue_thread, &sc->poll_task, 0,
|
||||
sfpled_poll, sc);
|
||||
taskqueue_enqueue_timeout(taskqueue_thread, &sc->poll_task, hz);
|
||||
|
||||
device_printf(dev, "%d SFP port(s) registered\n", registered);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
sfpled_detach(device_t dev)
|
||||
{
|
||||
struct sfp_led_softc *sc = device_get_softc(dev);
|
||||
int i;
|
||||
|
||||
/* Stop polling */
|
||||
taskqueue_drain_timeout(taskqueue_thread, &sc->poll_task);
|
||||
|
||||
for (i = 0; i < sc->nports; i++) {
|
||||
struct sfp_led_port *port = &sc->ports[i];
|
||||
|
||||
/* Turn off LEDs */
|
||||
sfpled_set_link(port, false);
|
||||
sfpled_set_activity(port, false);
|
||||
|
||||
/* Release GPIO pins */
|
||||
if (port->activity_led != NULL)
|
||||
gpio_pin_release(port->activity_led);
|
||||
if (port->link_led != NULL)
|
||||
gpio_pin_release(port->link_led);
|
||||
if (port->los != NULL)
|
||||
gpio_pin_release(port->los);
|
||||
if (port->mod_def0 != NULL)
|
||||
gpio_pin_release(port->mod_def0);
|
||||
|
||||
/* Release network interface reference */
|
||||
if (port->ifp != NULL)
|
||||
if_rele(port->ifp);
|
||||
}
|
||||
|
||||
device_printf(dev, "detached\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
static device_method_t sfpled_methods[] = {
|
||||
DEVMETHOD(device_probe, sfpled_probe),
|
||||
DEVMETHOD(device_attach, sfpled_attach),
|
||||
DEVMETHOD(device_detach, sfpled_detach),
|
||||
DEVMETHOD_END
|
||||
};
|
||||
|
||||
DEFINE_CLASS_0(sfpled, sfpled_driver, sfpled_methods,
|
||||
sizeof(struct sfp_led_softc));
|
||||
|
||||
DRIVER_MODULE(sfpled, simplebus, sfpled_driver, 0, 0);
|
||||
DRIVER_MODULE(sfpled, ofwbus, sfpled_driver, 0, 0);
|
||||
MODULE_DEPEND(sfpled, gpiobus, 1, 1, 1);
|
||||
MODULE_DEPEND(sfpled, iicbus, 1, 1, 1);
|
||||
11
fastpath/Makefile.inc
Normal file
11
fastpath/Makefile.inc
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# Common build settings for fastpath FreeBSD modules
|
||||
# Cross-compile: env MAKEOBJDIRPREFIX=/tmp/obj make TARGET=arm64 TARGET_ARCH=aarch64
|
||||
#
|
||||
# Copyright 2026 Mono Technologies Inc.
|
||||
# SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
FASTPATH_ROOT= ${.CURDIR}/..
|
||||
SYSDIR?= ${FASTPATH_ROOT}/../../opnsense-src/sys
|
||||
|
||||
CFLAGS+= -I${FASTPATH_ROOT}/include
|
||||
CFLAGS+= -DENDIAN_LITTLE -DGCC_TOOLCHAIN
|
||||
18
fastpath/auto_bridge/Makefile
Normal file
18
fastpath/auto_bridge/Makefile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# auto_bridge kernel module
|
||||
#
|
||||
# Build (cross-compile for arm64):
|
||||
# make -C /build/opnsense-deps/fastpath/auto_bridge \
|
||||
# KERNBUILDDIR=/usr/obj/build/opnsense-src/arm64.aarch64/sys/MONO-GATEWAY \
|
||||
# MACHINE=arm64 MACHINE_ARCH=aarch64 CC=cc LD=ld.lld
|
||||
#
|
||||
# Copyright 2026 Mono Technologies Inc.
|
||||
# SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
SYSDIR?= ${.CURDIR}/../../../opnsense-src/sys
|
||||
|
||||
KMOD= auto_bridge
|
||||
SRCS= auto_bridge.c
|
||||
|
||||
CFLAGS+= -I${.CURDIR}
|
||||
|
||||
.include <bsd.kmod.mk>
|
||||
795
fastpath/auto_bridge/auto_bridge.c
Normal file
795
fastpath/auto_bridge/auto_bridge.c
Normal file
|
|
@ -0,0 +1,795 @@
|
|||
/*
|
||||
* auto_bridge.c — L2 flow detection kernel module for bridge offload
|
||||
*
|
||||
* Hooks into FreeBSD's if_bridge via bridge_l2flow_hook to detect
|
||||
* L2 flows crossing bridge ports. Maintains a flow state machine
|
||||
* and communicates with userspace (CMM) via /dev/autobridge.
|
||||
*
|
||||
* FreeBSD port of the Linux auto_bridge kernel module.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/module.h>
|
||||
#include <sys/conf.h>
|
||||
#include <sys/fcntl.h>
|
||||
#include <sys/malloc.h>
|
||||
#include <sys/lock.h>
|
||||
#include <sys/mutex.h>
|
||||
#include <sys/queue.h>
|
||||
#include <sys/callout.h>
|
||||
#include <sys/selinfo.h>
|
||||
#include <sys/poll.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/uio.h>
|
||||
#include <sys/event.h>
|
||||
#include <sys/mbuf.h>
|
||||
|
||||
#include <net/if.h>
|
||||
#include <net/if_var.h>
|
||||
#include <net/if_types.h>
|
||||
#include <net/if_bridgevar.h>
|
||||
#include <net/ethernet.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/ip6.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <netinet/udp.h>
|
||||
|
||||
#include "auto_bridge.h"
|
||||
#include "auto_bridge_private.h"
|
||||
|
||||
MALLOC_DEFINE(M_ABM, "autobridge", "auto_bridge flow entries");
|
||||
|
||||
/* --- Global state ------------------------------------------------- */
|
||||
|
||||
static struct mtx abm_mtx;
|
||||
static struct abm_hash_head abm_table[ABM_HASH_SIZE];
|
||||
static struct abm_hash_head abm_by_src[ABM_MAC_HASH_SIZE];
|
||||
static struct abm_hash_head abm_by_dst[ABM_MAC_HASH_SIZE];
|
||||
static unsigned int abm_count;
|
||||
|
||||
/* Ring buffer + consumer wakeup */
|
||||
static struct abm_ring abm_ring;
|
||||
static struct selinfo abm_rsel;
|
||||
static int abm_open; /* only one client */
|
||||
|
||||
/* Character device */
|
||||
static struct cdev *abm_cdev;
|
||||
|
||||
/* Tunables */
|
||||
static int abm_l3_filtering;
|
||||
static int abm_timeout_confirmed = ABM_TIMEOUT_CONFIRMED;
|
||||
static int abm_timeout_dying = ABM_TIMEOUT_DYING;
|
||||
static int abm_max_entries = ABM_MAX_ENTRIES;
|
||||
|
||||
static struct sysctl_ctx_list abm_sysctl_ctx;
|
||||
static struct sysctl_oid *abm_sysctl_tree;
|
||||
|
||||
/* Flow state timeouts (seconds) */
|
||||
static int abm_timeouts[] = {
|
||||
[ABM_STATE_CONFIRMED] = ABM_TIMEOUT_CONFIRMED,
|
||||
[ABM_STATE_LINUX] = 10,
|
||||
[ABM_STATE_DYING] = ABM_TIMEOUT_DYING,
|
||||
};
|
||||
|
||||
/* --- Ring buffer -------------------------------------------------- */
|
||||
|
||||
static inline int
|
||||
ring_empty(const struct abm_ring *r)
|
||||
{
|
||||
|
||||
return (r->head == r->tail);
|
||||
}
|
||||
|
||||
static inline int
|
||||
ring_full(const struct abm_ring *r)
|
||||
{
|
||||
|
||||
return (((r->head + 1) % ABM_RING_SIZE) == r->tail);
|
||||
}
|
||||
|
||||
static int
|
||||
ring_put(struct abm_ring *r, const struct abm_event *ev)
|
||||
{
|
||||
|
||||
if (ring_full(r))
|
||||
return (ENOSPC);
|
||||
r->events[r->head] = *ev;
|
||||
r->head = (r->head + 1) % ABM_RING_SIZE;
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
ring_get(struct abm_ring *r, struct abm_event *ev)
|
||||
{
|
||||
|
||||
if (ring_empty(r))
|
||||
return (EAGAIN);
|
||||
*ev = r->events[r->tail];
|
||||
r->tail = (r->tail + 1) % ABM_RING_SIZE;
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* --- Entry management --------------------------------------------- */
|
||||
|
||||
static struct abm_entry *
|
||||
abm_find(const struct abm_l2flow *flow)
|
||||
{
|
||||
struct abm_entry *e;
|
||||
uint32_t h;
|
||||
|
||||
h = abm_flow_hash(flow);
|
||||
LIST_FOREACH(e, &abm_table[h], hash_entry) {
|
||||
if (memcmp(&e->flow, flow, sizeof(*flow)) == 0)
|
||||
return (e);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static void abm_entry_timeout(void *arg);
|
||||
|
||||
static struct abm_entry *
|
||||
abm_add(const struct abm_l2flow *flow, uint32_t iif, uint32_t oif,
|
||||
uint16_t mark)
|
||||
{
|
||||
struct abm_entry *e;
|
||||
uint32_t h, sh, dh;
|
||||
|
||||
if (abm_count >= (unsigned int)abm_max_entries)
|
||||
return (NULL);
|
||||
|
||||
e = malloc(sizeof(*e), M_ABM, M_NOWAIT | M_ZERO);
|
||||
if (e == NULL)
|
||||
return (NULL);
|
||||
|
||||
e->flow = *flow;
|
||||
e->iif_index = iif;
|
||||
e->oif_index = oif;
|
||||
e->mark = mark;
|
||||
e->state = ABM_STATE_CONFIRMED;
|
||||
e->flags = 0;
|
||||
|
||||
callout_init_mtx(&e->timer, &abm_mtx, 0);
|
||||
|
||||
h = abm_flow_hash(flow);
|
||||
LIST_INSERT_HEAD(&abm_table[h], e, hash_entry);
|
||||
|
||||
sh = abm_mac_hash(flow->saddr);
|
||||
LIST_INSERT_HEAD(&abm_by_src[sh], e, src_mac_entry);
|
||||
|
||||
dh = abm_mac_hash(flow->daddr);
|
||||
LIST_INSERT_HEAD(&abm_by_dst[dh], e, dst_mac_entry);
|
||||
|
||||
abm_count++;
|
||||
return (e);
|
||||
}
|
||||
|
||||
static void
|
||||
abm_remove(struct abm_entry *e)
|
||||
{
|
||||
|
||||
callout_stop(&e->timer);
|
||||
LIST_REMOVE(e, hash_entry);
|
||||
LIST_REMOVE(e, src_mac_entry);
|
||||
LIST_REMOVE(e, dst_mac_entry);
|
||||
abm_count--;
|
||||
free(e, M_ABM);
|
||||
}
|
||||
|
||||
/* --- Event posting ------------------------------------------------ */
|
||||
|
||||
static void
|
||||
abm_post_event(struct abm_entry *e, uint8_t type)
|
||||
{
|
||||
struct abm_event ev;
|
||||
|
||||
memset(&ev, 0, sizeof(ev));
|
||||
ev.type = type;
|
||||
ev.flow = e->flow;
|
||||
ev.iif_index = e->iif_index;
|
||||
ev.oif_index = e->oif_index;
|
||||
ev.mark = e->mark;
|
||||
|
||||
if (ring_put(&abm_ring, &ev) == 0) {
|
||||
selwakeup(&abm_rsel);
|
||||
KNOTE_LOCKED(&abm_rsel.si_note, 0);
|
||||
wakeup(&abm_ring);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
abm_post_reset(void)
|
||||
{
|
||||
struct abm_event ev;
|
||||
|
||||
memset(&ev, 0, sizeof(ev));
|
||||
ev.type = ABM_EVENT_RESET;
|
||||
|
||||
if (ring_put(&abm_ring, &ev) == 0) {
|
||||
selwakeup(&abm_rsel);
|
||||
KNOTE_LOCKED(&abm_rsel.si_note, 0);
|
||||
wakeup(&abm_ring);
|
||||
}
|
||||
}
|
||||
|
||||
/* --- State machine ------------------------------------------------ */
|
||||
|
||||
/*
|
||||
* Timer callback — move to DYING or delete.
|
||||
* Called with abm_mtx held (callout_init_mtx).
|
||||
*/
|
||||
static void
|
||||
abm_entry_timeout(void *arg)
|
||||
{
|
||||
struct abm_entry *e = arg;
|
||||
|
||||
mtx_assert(&abm_mtx, MA_OWNED);
|
||||
|
||||
if (e->state == ABM_STATE_DYING) {
|
||||
/* Second timeout in DYING — delete for real */
|
||||
abm_post_event(e, ABM_EVENT_FLOW_DEL);
|
||||
abm_remove(e);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Move to DYING, set final timeout */
|
||||
e->state = ABM_STATE_DYING;
|
||||
abm_post_event(e, ABM_EVENT_FLOW_DEL);
|
||||
callout_reset(&e->timer, abm_timeouts[ABM_STATE_DYING] * hz,
|
||||
abm_entry_timeout, e);
|
||||
}
|
||||
|
||||
/*
|
||||
* Process a CMM response (from write() on /dev/autobridge).
|
||||
*/
|
||||
static void
|
||||
abm_handle_response(const struct abm_response *resp)
|
||||
{
|
||||
struct abm_entry *e;
|
||||
|
||||
mtx_lock(&abm_mtx);
|
||||
e = abm_find(&resp->flow);
|
||||
if (e == NULL) {
|
||||
mtx_unlock(&abm_mtx);
|
||||
return;
|
||||
}
|
||||
|
||||
if (resp->flags & ABM_FLAG_OFFLOADED) {
|
||||
e->state = ABM_STATE_FF;
|
||||
callout_stop(&e->timer);
|
||||
} else if (resp->flags & ABM_FLAG_DENIED) {
|
||||
e->state = ABM_STATE_LINUX;
|
||||
callout_reset(&e->timer, abm_timeouts[ABM_STATE_LINUX] * hz,
|
||||
abm_entry_timeout, e);
|
||||
}
|
||||
|
||||
e->flags &= ~ABM_FL_WAIT_ACK;
|
||||
mtx_unlock(&abm_mtx);
|
||||
}
|
||||
|
||||
/* --- Flush all flows ---------------------------------------------- */
|
||||
|
||||
static void
|
||||
abm_flush_all(void)
|
||||
{
|
||||
struct abm_entry *e, *tmp;
|
||||
int i;
|
||||
|
||||
mtx_assert(&abm_mtx, MA_OWNED);
|
||||
|
||||
for (i = 0; i < ABM_HASH_SIZE; i++) {
|
||||
LIST_FOREACH_SAFE(e, &abm_table[i], hash_entry, tmp) {
|
||||
abm_remove(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* --- Bridge hooks ------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Called from bridge_forward() for every unicast frame with a known
|
||||
* destination port. Both src_if and dst_if are bridge member ports.
|
||||
*
|
||||
* Context: network stack (interrupt or thread), must be fast.
|
||||
*/
|
||||
static void
|
||||
abm_l2flow_hook(struct ifnet *bridge_ifp __unused, struct mbuf *m,
|
||||
struct ifnet *src_if, struct ifnet *dst_if)
|
||||
{
|
||||
struct ether_header *eh;
|
||||
struct abm_l2flow flow;
|
||||
struct abm_entry *e;
|
||||
int new_flow = 0;
|
||||
|
||||
if (m->m_len < sizeof(*eh))
|
||||
return;
|
||||
|
||||
eh = mtod(m, struct ether_header *);
|
||||
|
||||
/* Build L2 flow tuple */
|
||||
memset(&flow, 0, sizeof(flow));
|
||||
memcpy(flow.saddr, eh->ether_shost, ETHER_ADDR_LEN);
|
||||
memcpy(flow.daddr, eh->ether_dhost, ETHER_ADDR_LEN);
|
||||
flow.ethertype = eh->ether_type;
|
||||
flow.svlan_tag = 0xFFFF;
|
||||
flow.cvlan_tag = 0xFFFF;
|
||||
|
||||
/*
|
||||
* Extract VLAN tags if present.
|
||||
* FreeBSD strips VLAN tags into m_pkthdr before bridge_forward(),
|
||||
* so check the pkthdr for in-band VLAN info.
|
||||
*/
|
||||
if (m->m_flags & M_VLANTAG) {
|
||||
flow.svlan_tag = m->m_pkthdr.ether_vtag;
|
||||
}
|
||||
|
||||
/*
|
||||
* Optional L3/L4 extraction (when sysctl enabled).
|
||||
*/
|
||||
if (abm_l3_filtering) {
|
||||
int hlen = sizeof(*eh);
|
||||
uint16_t etype = ntohs(eh->ether_type);
|
||||
|
||||
if (etype == ETHERTYPE_IP && m->m_pkthdr.len >= hlen + 20) {
|
||||
struct ip iph;
|
||||
|
||||
m_copydata(m, hlen, sizeof(iph), (caddr_t)&iph);
|
||||
flow.sip[0] = iph.ip_src.s_addr;
|
||||
flow.dip[0] = iph.ip_dst.s_addr;
|
||||
flow.proto = iph.ip_p;
|
||||
|
||||
/* L4 ports if not fragmented */
|
||||
if ((ntohs(iph.ip_off) & (IP_MF | IP_OFFMASK)) == 0 &&
|
||||
(iph.ip_p == IPPROTO_TCP ||
|
||||
iph.ip_p == IPPROTO_UDP)) {
|
||||
int l4off = hlen + (iph.ip_hl << 2);
|
||||
uint16_t ports[2];
|
||||
|
||||
if (m->m_pkthdr.len >= l4off + 4) {
|
||||
m_copydata(m, l4off, 4,
|
||||
(caddr_t)ports);
|
||||
flow.sport = ports[0];
|
||||
flow.dport = ports[1];
|
||||
}
|
||||
}
|
||||
} else if (etype == ETHERTYPE_IPV6 &&
|
||||
m->m_pkthdr.len >= hlen + 40) {
|
||||
struct ip6_hdr ip6h;
|
||||
|
||||
m_copydata(m, hlen, sizeof(ip6h), (caddr_t)&ip6h);
|
||||
memcpy(flow.sip, &ip6h.ip6_src, 16);
|
||||
memcpy(flow.dip, &ip6h.ip6_dst, 16);
|
||||
flow.proto = ip6h.ip6_nxt;
|
||||
|
||||
if (flow.proto == IPPROTO_TCP ||
|
||||
flow.proto == IPPROTO_UDP) {
|
||||
int l4off = hlen + 40;
|
||||
uint16_t ports[2];
|
||||
|
||||
if (m->m_pkthdr.len >= l4off + 4) {
|
||||
m_copydata(m, l4off, 4,
|
||||
(caddr_t)ports);
|
||||
flow.sport = ports[0];
|
||||
flow.dport = ports[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mtx_lock(&abm_mtx);
|
||||
|
||||
e = abm_find(&flow);
|
||||
if (e == NULL) {
|
||||
/* New flow */
|
||||
e = abm_add(&flow, if_getindex(src_if),
|
||||
if_getindex(dst_if), 0);
|
||||
if (e == NULL) {
|
||||
mtx_unlock(&abm_mtx);
|
||||
return;
|
||||
}
|
||||
new_flow = 1;
|
||||
/* Set confirmed timeout */
|
||||
callout_reset(&e->timer,
|
||||
abm_timeouts[ABM_STATE_CONFIRMED] * hz,
|
||||
abm_entry_timeout, e);
|
||||
} else {
|
||||
/* Existing flow — check if interfaces changed */
|
||||
if (e->iif_index != (uint32_t)if_getindex(src_if) ||
|
||||
e->oif_index != (uint32_t)if_getindex(dst_if)) {
|
||||
e->iif_index = if_getindex(src_if);
|
||||
e->oif_index = if_getindex(dst_if);
|
||||
if (e->state != ABM_STATE_DYING) {
|
||||
abm_post_event(e, ABM_EVENT_FLOW_UPDATE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (new_flow && abm_open)
|
||||
abm_post_event(e, ABM_EVENT_FLOW_NEW);
|
||||
|
||||
mtx_unlock(&abm_mtx);
|
||||
}
|
||||
|
||||
/*
|
||||
* Called from bridge_rtage() before destroying a dynamic FDB entry.
|
||||
* Returns 0 to prevent expiry (MAC belongs to an offloaded flow),
|
||||
* or 1 to allow normal expiry.
|
||||
*
|
||||
* NOTE: bridge_ifp is the bridge interface, not the member port.
|
||||
*/
|
||||
static int
|
||||
abm_fdb_can_expire(const uint8_t *mac, struct ifnet *bridge_ifp __unused)
|
||||
{
|
||||
struct abm_entry *e;
|
||||
uint32_t h;
|
||||
|
||||
h = abm_mac_hash(mac);
|
||||
|
||||
mtx_lock(&abm_mtx);
|
||||
LIST_FOREACH(e, &abm_by_src[h], src_mac_entry) {
|
||||
if (memcmp(mac, e->flow.saddr, ETHER_ADDR_LEN) == 0 &&
|
||||
e->state == ABM_STATE_FF) {
|
||||
mtx_unlock(&abm_mtx);
|
||||
return (0); /* don't expire */
|
||||
}
|
||||
}
|
||||
mtx_unlock(&abm_mtx);
|
||||
return (1); /* allow expiry */
|
||||
}
|
||||
|
||||
/* --- Character device --------------------------------------------- */
|
||||
|
||||
static int
|
||||
abm_dev_open(struct cdev *dev __unused, int oflags __unused,
|
||||
int devtype __unused, struct thread *td __unused)
|
||||
{
|
||||
|
||||
mtx_lock(&abm_mtx);
|
||||
if (abm_open) {
|
||||
mtx_unlock(&abm_mtx);
|
||||
return (EBUSY);
|
||||
}
|
||||
abm_open = 1;
|
||||
/* Reset ring on new open */
|
||||
abm_ring.head = 0;
|
||||
abm_ring.tail = 0;
|
||||
mtx_unlock(&abm_mtx);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
abm_dev_close(struct cdev *dev __unused, int fflag __unused,
|
||||
int devtype __unused, struct thread *td __unused)
|
||||
{
|
||||
|
||||
mtx_lock(&abm_mtx);
|
||||
abm_open = 0;
|
||||
|
||||
/*
|
||||
* Move all FF flows to DYING — traffic will revert to
|
||||
* software bridge after CDX flows time out.
|
||||
*/
|
||||
{
|
||||
struct abm_entry *e;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ABM_HASH_SIZE; i++) {
|
||||
LIST_FOREACH(e, &abm_table[i], hash_entry) {
|
||||
if (e->state == ABM_STATE_FF) {
|
||||
e->state = ABM_STATE_DYING;
|
||||
callout_reset(&e->timer,
|
||||
abm_timeouts[ABM_STATE_DYING] * hz,
|
||||
abm_entry_timeout, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mtx_unlock(&abm_mtx);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
abm_dev_read(struct cdev *dev __unused, struct uio *uio,
|
||||
int ioflag)
|
||||
{
|
||||
struct abm_event ev;
|
||||
int error;
|
||||
|
||||
if (uio->uio_resid < (ssize_t)sizeof(ev))
|
||||
return (EINVAL);
|
||||
|
||||
mtx_lock(&abm_mtx);
|
||||
while (ring_empty(&abm_ring)) {
|
||||
if (ioflag & FNONBLOCK) {
|
||||
mtx_unlock(&abm_mtx);
|
||||
return (EAGAIN);
|
||||
}
|
||||
error = msleep(&abm_ring, &abm_mtx, PCATCH, "abmrd", 0);
|
||||
if (error) {
|
||||
mtx_unlock(&abm_mtx);
|
||||
return (error);
|
||||
}
|
||||
}
|
||||
|
||||
/* Drain as many events as will fit */
|
||||
while (uio->uio_resid >= (ssize_t)sizeof(ev) &&
|
||||
ring_get(&abm_ring, &ev) == 0) {
|
||||
mtx_unlock(&abm_mtx);
|
||||
error = uiomove(&ev, sizeof(ev), uio);
|
||||
if (error)
|
||||
return (error);
|
||||
mtx_lock(&abm_mtx);
|
||||
}
|
||||
|
||||
mtx_unlock(&abm_mtx);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
abm_dev_write(struct cdev *dev __unused, struct uio *uio,
|
||||
int ioflag __unused)
|
||||
{
|
||||
struct abm_response resp;
|
||||
int error;
|
||||
|
||||
while (uio->uio_resid >= (ssize_t)sizeof(resp)) {
|
||||
error = uiomove(&resp, sizeof(resp), uio);
|
||||
if (error)
|
||||
return (error);
|
||||
abm_handle_response(&resp);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
abm_dev_poll(struct cdev *dev __unused, int events, struct thread *td)
|
||||
{
|
||||
int revents = 0;
|
||||
|
||||
mtx_lock(&abm_mtx);
|
||||
if (events & (POLLIN | POLLRDNORM)) {
|
||||
if (!ring_empty(&abm_ring))
|
||||
revents |= events & (POLLIN | POLLRDNORM);
|
||||
else
|
||||
selrecord(td, &abm_rsel);
|
||||
}
|
||||
mtx_unlock(&abm_mtx);
|
||||
return (revents);
|
||||
}
|
||||
|
||||
/* --- kqueue support ---------------------------------------------- */
|
||||
|
||||
static int abm_kqread(struct knote *kn, long hint);
|
||||
static void abm_kqdetach(struct knote *kn);
|
||||
|
||||
static const struct filterops abm_read_filterops = {
|
||||
.f_isfd = 1,
|
||||
.f_detach = abm_kqdetach,
|
||||
.f_event = abm_kqread,
|
||||
#if __FreeBSD_version >= 1500000
|
||||
.f_copy = knote_triv_copy,
|
||||
#endif
|
||||
};
|
||||
|
||||
static int
|
||||
abm_kqread(struct knote *kn, long hint)
|
||||
{
|
||||
unsigned int count;
|
||||
|
||||
count = (abm_ring.head - abm_ring.tail + ABM_RING_SIZE) %
|
||||
ABM_RING_SIZE;
|
||||
kn->kn_data = count * sizeof(struct abm_event);
|
||||
return (count > 0);
|
||||
}
|
||||
|
||||
static void
|
||||
abm_kqdetach(struct knote *kn)
|
||||
{
|
||||
|
||||
knlist_remove(&abm_rsel.si_note, kn, 0);
|
||||
}
|
||||
|
||||
static int
|
||||
abm_dev_kqfilter(struct cdev *dev __unused, struct knote *kn)
|
||||
{
|
||||
|
||||
switch (kn->kn_filter) {
|
||||
case EVFILT_READ:
|
||||
kn->kn_fop = &abm_read_filterops;
|
||||
break;
|
||||
default:
|
||||
return (EINVAL);
|
||||
}
|
||||
knlist_add(&abm_rsel.si_note, kn, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static struct cdevsw abm_cdevsw = {
|
||||
.d_version = D_VERSION,
|
||||
.d_open = abm_dev_open,
|
||||
.d_close = abm_dev_close,
|
||||
.d_read = abm_dev_read,
|
||||
.d_write = abm_dev_write,
|
||||
.d_poll = abm_dev_poll,
|
||||
.d_kqfilter = abm_dev_kqfilter,
|
||||
.d_name = ABM_DEV_NAME,
|
||||
};
|
||||
|
||||
/* --- Sysctl ------------------------------------------------------- */
|
||||
|
||||
static int
|
||||
abm_sysctl_l3_filtering(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
int error, old;
|
||||
|
||||
old = abm_l3_filtering;
|
||||
error = sysctl_handle_int(oidp, &abm_l3_filtering, 0, req);
|
||||
if (error || req->newptr == NULL)
|
||||
return (error);
|
||||
|
||||
/* State change → flush all flows and notify CMM */
|
||||
if (old != abm_l3_filtering) {
|
||||
mtx_lock(&abm_mtx);
|
||||
abm_flush_all();
|
||||
if (abm_open)
|
||||
abm_post_reset();
|
||||
mtx_unlock(&abm_mtx);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void
|
||||
abm_sysctl_init(void)
|
||||
{
|
||||
struct sysctl_oid *parent;
|
||||
|
||||
sysctl_ctx_init(&abm_sysctl_ctx);
|
||||
|
||||
/*
|
||||
* Create our own top-level node: net.autobridge
|
||||
* (net.link.bridge is static within if_bridge.c and
|
||||
* not accessible from loadable modules)
|
||||
*/
|
||||
parent = SYSCTL_ADD_NODE(&abm_sysctl_ctx,
|
||||
SYSCTL_STATIC_CHILDREN(_net), OID_AUTO,
|
||||
"autobridge", CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
|
||||
"Auto-bridge L2 flow detection");
|
||||
abm_sysctl_tree = parent;
|
||||
|
||||
SYSCTL_ADD_PROC(&abm_sysctl_ctx, SYSCTL_CHILDREN(parent),
|
||||
OID_AUTO, "l3_filtering",
|
||||
CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
|
||||
NULL, 0, abm_sysctl_l3_filtering, "I",
|
||||
"Enable L3/L4 field extraction (0=off, 1=on)");
|
||||
|
||||
SYSCTL_ADD_INT(&abm_sysctl_ctx, SYSCTL_CHILDREN(parent),
|
||||
OID_AUTO, "timeout_confirmed", CTLFLAG_RW,
|
||||
&abm_timeout_confirmed, 0,
|
||||
"Confirmed state timeout (seconds)");
|
||||
|
||||
SYSCTL_ADD_INT(&abm_sysctl_ctx, SYSCTL_CHILDREN(parent),
|
||||
OID_AUTO, "timeout_dying", CTLFLAG_RW,
|
||||
&abm_timeout_dying, 0,
|
||||
"Dying state timeout (seconds)");
|
||||
|
||||
SYSCTL_ADD_INT(&abm_sysctl_ctx, SYSCTL_CHILDREN(parent),
|
||||
OID_AUTO, "max_entries", CTLFLAG_RW,
|
||||
&abm_max_entries, 0,
|
||||
"Maximum number of tracked flows");
|
||||
|
||||
SYSCTL_ADD_UINT(&abm_sysctl_ctx, SYSCTL_CHILDREN(parent),
|
||||
OID_AUTO, "count", CTLFLAG_RD,
|
||||
&abm_count, 0,
|
||||
"Current number of tracked flows");
|
||||
}
|
||||
|
||||
static void
|
||||
abm_sysctl_fini(void)
|
||||
{
|
||||
|
||||
sysctl_ctx_free(&abm_sysctl_ctx);
|
||||
}
|
||||
|
||||
/* --- Module init/fini --------------------------------------------- */
|
||||
|
||||
static int
|
||||
abm_load(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
mtx_init(&abm_mtx, "autobridge", NULL, MTX_DEF);
|
||||
knlist_init_mtx(&abm_rsel.si_note, &abm_mtx);
|
||||
|
||||
for (i = 0; i < ABM_HASH_SIZE; i++)
|
||||
LIST_INIT(&abm_table[i]);
|
||||
for (i = 0; i < ABM_MAC_HASH_SIZE; i++) {
|
||||
LIST_INIT(&abm_by_src[i]);
|
||||
LIST_INIT(&abm_by_dst[i]);
|
||||
}
|
||||
abm_count = 0;
|
||||
abm_open = 0;
|
||||
abm_ring.head = 0;
|
||||
abm_ring.tail = 0;
|
||||
|
||||
/* Sync sysctl values into timeouts array */
|
||||
abm_timeouts[ABM_STATE_CONFIRMED] = abm_timeout_confirmed;
|
||||
abm_timeouts[ABM_STATE_DYING] = abm_timeout_dying;
|
||||
|
||||
/* Create /dev/autobridge */
|
||||
abm_cdev = make_dev(&abm_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
|
||||
ABM_DEV_NAME);
|
||||
if (abm_cdev == NULL) {
|
||||
printf("auto_bridge: failed to create /dev/%s\n",
|
||||
ABM_DEV_NAME);
|
||||
mtx_destroy(&abm_mtx);
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
abm_sysctl_init();
|
||||
|
||||
/* Register bridge hooks */
|
||||
bridge_l2flow_hook = abm_l2flow_hook;
|
||||
bridge_fdb_can_expire_hook = abm_fdb_can_expire;
|
||||
|
||||
printf("auto_bridge: loaded (max_entries=%d, l3_filtering=%d)\n",
|
||||
abm_max_entries, abm_l3_filtering);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void
|
||||
abm_unload(void)
|
||||
{
|
||||
|
||||
/* Unregister hooks first */
|
||||
bridge_l2flow_hook = NULL;
|
||||
bridge_fdb_can_expire_hook = NULL;
|
||||
|
||||
/* Flush all entries */
|
||||
mtx_lock(&abm_mtx);
|
||||
abm_flush_all();
|
||||
mtx_unlock(&abm_mtx);
|
||||
|
||||
abm_sysctl_fini();
|
||||
|
||||
if (abm_cdev != NULL)
|
||||
destroy_dev(abm_cdev);
|
||||
|
||||
seldrain(&abm_rsel);
|
||||
knlist_destroy(&abm_rsel.si_note);
|
||||
mtx_destroy(&abm_mtx);
|
||||
|
||||
printf("auto_bridge: unloaded\n");
|
||||
}
|
||||
|
||||
static int
|
||||
auto_bridge_modevent(module_t mod __unused, int type, void *unused __unused)
|
||||
{
|
||||
|
||||
switch (type) {
|
||||
case MOD_LOAD:
|
||||
return (abm_load());
|
||||
case MOD_UNLOAD:
|
||||
abm_unload();
|
||||
return (0);
|
||||
default:
|
||||
return (EOPNOTSUPP);
|
||||
}
|
||||
}
|
||||
|
||||
static moduledata_t auto_bridge_mod = {
|
||||
"auto_bridge",
|
||||
auto_bridge_modevent,
|
||||
NULL
|
||||
};
|
||||
|
||||
DECLARE_MODULE(auto_bridge, auto_bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
|
||||
MODULE_VERSION(auto_bridge, 1);
|
||||
MODULE_DEPEND(auto_bridge, if_bridge, 1, 1, 1);
|
||||
82
fastpath/auto_bridge/auto_bridge.h
Normal file
82
fastpath/auto_bridge/auto_bridge.h
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* auto_bridge.h — L2 flow detection for bridge offload
|
||||
*
|
||||
* Public header shared between the auto_bridge kernel module and
|
||||
* userspace consumers (CMM). Defines the message format for the
|
||||
* /dev/autobridge character device.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef AUTO_BRIDGE_H
|
||||
#define AUTO_BRIDGE_H
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <sys/types.h>
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
#include <net/ethernet.h>
|
||||
|
||||
#define ABM_DEV_PATH "/dev/autobridge"
|
||||
#define ABM_DEV_NAME "autobridge"
|
||||
|
||||
/* Event types (kernel -> userspace via read()) */
|
||||
#define ABM_EVENT_FLOW_NEW 1
|
||||
#define ABM_EVENT_FLOW_UPDATE 2
|
||||
#define ABM_EVENT_FLOW_DEL 3
|
||||
#define ABM_EVENT_RESET 4
|
||||
|
||||
/* Response flags (userspace -> kernel via write()) */
|
||||
#define ABM_FLAG_OFFLOADED 0x01
|
||||
#define ABM_FLAG_DENIED 0x02
|
||||
#define ABM_FLAG_ACK 0x04
|
||||
|
||||
/*
|
||||
* L2 flow identification tuple.
|
||||
* Matches on MAC pair + ethertype + optional VLAN/PPPoE/L3/L4.
|
||||
* Used as hash key in both kernel and CMM hash tables.
|
||||
*/
|
||||
struct abm_l2flow {
|
||||
uint8_t saddr[ETHER_ADDR_LEN]; /* source MAC */
|
||||
uint8_t daddr[ETHER_ADDR_LEN]; /* destination MAC */
|
||||
uint16_t ethertype; /* network byte order */
|
||||
uint16_t svlan_tag; /* S-VLAN TCI, 0xFFFF=none */
|
||||
uint16_t cvlan_tag; /* C-VLAN TCI, 0xFFFF=none */
|
||||
uint16_t session_id; /* PPPoE session, 0=none */
|
||||
/* L3/L4 fields — populated only when l3_filtering enabled */
|
||||
uint8_t proto; /* IPPROTO_*, 0=none */
|
||||
uint8_t pad;
|
||||
uint16_t sport; /* source port, net order */
|
||||
uint16_t dport; /* dest port, net order */
|
||||
uint16_t pad2;
|
||||
uint32_t sip[4]; /* source IP (v4 in [0]) */
|
||||
uint32_t dip[4]; /* dest IP (v4 in [0]) */
|
||||
};
|
||||
|
||||
/*
|
||||
* Event message: kernel -> CMM (read from /dev/autobridge).
|
||||
* Fixed-size for simple ring buffer and framing.
|
||||
*/
|
||||
struct abm_event {
|
||||
uint8_t type; /* ABM_EVENT_* */
|
||||
uint8_t pad[3];
|
||||
struct abm_l2flow flow;
|
||||
uint32_t iif_index; /* input member port ifindex */
|
||||
uint32_t oif_index; /* output member port ifindex */
|
||||
uint16_t mark; /* QoS mark */
|
||||
uint16_t pad2;
|
||||
};
|
||||
|
||||
/*
|
||||
* Response message: CMM -> kernel (write to /dev/autobridge).
|
||||
* Identifies the flow by its L2 tuple and sets flags.
|
||||
*/
|
||||
struct abm_response {
|
||||
struct abm_l2flow flow; /* identifies the flow */
|
||||
uint32_t flags; /* ABM_FLAG_* */
|
||||
};
|
||||
|
||||
#endif /* AUTO_BRIDGE_H */
|
||||
115
fastpath/auto_bridge/auto_bridge_private.h
Normal file
115
fastpath/auto_bridge/auto_bridge_private.h
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
* auto_bridge_private.h — Kernel-private structures
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef AUTO_BRIDGE_PRIVATE_H
|
||||
#define AUTO_BRIDGE_PRIVATE_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/queue.h>
|
||||
#include <sys/callout.h>
|
||||
#include <sys/lock.h>
|
||||
#include <sys/mutex.h>
|
||||
|
||||
#include "auto_bridge.h"
|
||||
|
||||
#define ABM_HASH_SIZE 1024
|
||||
#define ABM_MAC_HASH_SIZE 128
|
||||
#define ABM_RING_SIZE 1024 /* event ring buffer entries */
|
||||
#define ABM_MAX_ENTRIES 5000
|
||||
|
||||
/* Timeouts (seconds) */
|
||||
#define ABM_TIMEOUT_CONFIRMED 120
|
||||
#define ABM_TIMEOUT_LINUX 10
|
||||
#define ABM_TIMEOUT_DYING 120
|
||||
|
||||
/* L2 flow states */
|
||||
enum abm_state {
|
||||
ABM_STATE_CONFIRMED, /* detected, event sent to CMM */
|
||||
ABM_STATE_FF, /* CMM offloaded to CDX */
|
||||
ABM_STATE_LINUX, /* CMM denied offload */
|
||||
ABM_STATE_DYING, /* cleanup / port down */
|
||||
};
|
||||
|
||||
/* Internal flags */
|
||||
#define ABM_FL_NEEDS_UPDATE 0x01
|
||||
#define ABM_FL_DEAD 0x02
|
||||
#define ABM_FL_WAIT_ACK 0x04
|
||||
#define ABM_FL_PENDING_MSG 0x08
|
||||
|
||||
/*
|
||||
* L2 flow table entry.
|
||||
*
|
||||
* Three linked lists for different lookup patterns:
|
||||
* - hash_entry: primary hash by full L2 flow tuple
|
||||
* - src_mac_entry: secondary hash by source MAC (FDB expiry checks)
|
||||
* - dst_mac_entry: secondary hash by dest MAC (MAC movement detection)
|
||||
*/
|
||||
struct abm_entry {
|
||||
LIST_ENTRY(abm_entry) hash_entry;
|
||||
LIST_ENTRY(abm_entry) src_mac_entry;
|
||||
LIST_ENTRY(abm_entry) dst_mac_entry;
|
||||
|
||||
struct abm_l2flow flow; /* L2 flow tuple */
|
||||
uint32_t iif_index; /* input interface index */
|
||||
uint32_t oif_index; /* output interface index */
|
||||
uint16_t mark; /* QoS mark */
|
||||
uint8_t state; /* enum abm_state */
|
||||
uint8_t flags; /* ABM_FL_* */
|
||||
struct callout timer; /* timeout callout */
|
||||
time_t time_sent; /* last event send time */
|
||||
};
|
||||
|
||||
LIST_HEAD(abm_hash_head, abm_entry);
|
||||
|
||||
/*
|
||||
* Ring buffer for kernel -> userspace events.
|
||||
* Single producer (hook context), single consumer (read()).
|
||||
*/
|
||||
struct abm_ring {
|
||||
struct abm_event events[ABM_RING_SIZE];
|
||||
volatile uint32_t head; /* next write position */
|
||||
volatile uint32_t tail; /* next read position */
|
||||
};
|
||||
|
||||
/*
|
||||
* Jenkins one-at-a-time hash (portable version).
|
||||
*/
|
||||
static inline uint32_t
|
||||
abm_jenkins_hash(const void *key, size_t len, uint32_t initval)
|
||||
{
|
||||
const uint8_t *p = (const uint8_t *)key;
|
||||
uint32_t hash = initval;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
hash += p[i];
|
||||
hash += (hash << 10);
|
||||
hash ^= (hash >> 6);
|
||||
}
|
||||
hash += (hash << 3);
|
||||
hash ^= (hash >> 11);
|
||||
hash += (hash << 15);
|
||||
return (hash);
|
||||
}
|
||||
|
||||
static inline uint32_t
|
||||
abm_flow_hash(const struct abm_l2flow *flow)
|
||||
{
|
||||
|
||||
return (abm_jenkins_hash(flow, sizeof(*flow), 0x12345678) &
|
||||
(ABM_HASH_SIZE - 1));
|
||||
}
|
||||
|
||||
static inline uint32_t
|
||||
abm_mac_hash(const uint8_t *mac)
|
||||
{
|
||||
|
||||
return (abm_jenkins_hash(mac, ETHER_ADDR_LEN, 0x12345678) &
|
||||
(ABM_MAC_HASH_SIZE - 1));
|
||||
}
|
||||
|
||||
#endif /* AUTO_BRIDGE_PRIVATE_H */
|
||||
101
fastpath/cdx/Makefile
Normal file
101
fastpath/cdx/Makefile
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
# CDX (Conntrack Data-plane eXpress) kernel module for FreeBSD
|
||||
#
|
||||
# Copyright 2026 Mono Technologies Inc.
|
||||
# SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
.include "../Makefile.inc"
|
||||
|
||||
KMOD= cdx
|
||||
|
||||
# FreeBSD-specific files (in this directory)
|
||||
SRCS= cdx_main_freebsd.c cdx_timer_freebsd.c cdx_cmdhandler_freebsd.c \
|
||||
cdx_dev_freebsd.c cdx_sysctl.c cdx_dpa_stub.c cdx_dpa_takeover.c \
|
||||
cdx_dpa_bridge.c cdx_dpa_cfg.c cdx_dpa_init.c \
|
||||
cdx_usermodehelper.c \
|
||||
cdx_devman_freebsd.c cdx_ehash.c \
|
||||
cdx_ifstats_freebsd.c cdx_debug_freebsd.c \
|
||||
cdx_qos_freebsd.c cdx_mc_freebsd.c \
|
||||
cdx_devoh_freebsd.c cdx_reassm_freebsd.c \
|
||||
cdx_ceetm_freebsd.c \
|
||||
dpa_ipsec_freebsd.c cdx_dpa_ipsec_freebsd.c
|
||||
|
||||
# Tier 1 CDX sources (from original CDX tree, mostly unchanged)
|
||||
SRCS+= cdx_hal.c \
|
||||
layer2.c control_ipv4.c control_ipv6.c control_tx.c control_rx.c \
|
||||
control_vlan.c control_pppoe.c control_bridge.c control_tunnel.c \
|
||||
control_socket.c control_stat.c control_natpt.c control_qm.c \
|
||||
control_wifi.c control_ipsec.c control_rtp_relay.c \
|
||||
control_pktcap.c \
|
||||
query_Rx.c query_ipsec.c
|
||||
|
||||
SRCS+= opt_platform.h opt_dpaa.h device_if.h bus_if.h ofw_bus_if.h
|
||||
|
||||
# Search path for Tier 1 sources in vendor CDX tree
|
||||
.PATH: ${FASTPATH_ROOT}/vendor/cdx
|
||||
|
||||
# Force-include our prelude before every source file. This pre-includes
|
||||
# shadow headers (cdx.h, dpa_ipsec.h, misc.h) setting their include guards,
|
||||
# so when Tier 1 files find originals in vendor/cdx/ they're skipped.
|
||||
CFLAGS+= -include ${FASTPATH_ROOT}/include/cdx_prelude.h
|
||||
CFLAGS+= -I${FASTPATH_ROOT}/vendor/cdx
|
||||
CFLAGS+= -DENABLE_EGRESS_QOS
|
||||
CFLAGS+= -DDPAA_FAST_PATH
|
||||
CFLAGS+= -DENABLE_FLOW_TIME_STAMPS
|
||||
CFLAGS+= -DDPA_IPSEC_OFFLOAD
|
||||
|
||||
# NCSW build environment defines (replicate dflags_arm64.h without its
|
||||
# Linux-specific includes like <asm/byteorder.h> and <linux/delay.h>)
|
||||
CFLAGS+= -Wno-typedef-redefinition
|
||||
CFLAGS+= -DNCSW_ARM_CORE -DNCSW_FREEBSD -DCORE_CACHELINE_SIZE=64
|
||||
CFLAGS+= -DDEBUG_ERRORS=1 -DREPORT_EVENTS=1
|
||||
CFLAGS+= -DDEBUG_GLOBAL_LEVEL=REPORT_LEVEL_INFO
|
||||
CFLAGS+= -DEVENT_GLOBAL_LEVEL=REPORT_LEVEL_MINOR
|
||||
|
||||
# NCSW headers — the full set needed for cross-directory quoted includes
|
||||
CFLAGS+= -I${SYSDIR}/contrib/ncsw/inc
|
||||
CFLAGS+= -I${SYSDIR}/contrib/ncsw/inc/etc
|
||||
CFLAGS+= -I${SYSDIR}/contrib/ncsw/inc/flib
|
||||
CFLAGS+= -I${SYSDIR}/contrib/ncsw/inc/cores
|
||||
CFLAGS+= -I${SYSDIR}/contrib/ncsw/inc/integrations
|
||||
CFLAGS+= -I${SYSDIR}/contrib/ncsw/inc/Peripherals
|
||||
CFLAGS+= -I${SYSDIR}/contrib/ncsw/build
|
||||
CFLAGS+= -I${SYSDIR}/contrib/ncsw/Peripherals/FM/Pcd
|
||||
CFLAGS+= -I${SYSDIR}/contrib/ncsw/Peripherals/FM/inc
|
||||
|
||||
# Relax warnings for Tier 1 vendor sources — original NXP code not written
|
||||
# for FreeBSD's strict Clang warnings. Shared relaxation flags:
|
||||
VENDOR_WARN= -Wno-missing-prototypes -Wno-gnu-pointer-arith \
|
||||
-Wno-pointer-arith -Wno-cast-qual
|
||||
|
||||
CFLAGS.cdx_hal.c+= ${VENDOR_WARN}
|
||||
CFLAGS.layer2.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_ipv4.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_ipv6.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_tx.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_rx.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_vlan.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_pppoe.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_bridge.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_tunnel.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_socket.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_stat.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_natpt.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_qm.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_wifi.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_ipsec.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_rtp_relay.c+= ${VENDOR_WARN}
|
||||
CFLAGS.control_pktcap.c+= ${VENDOR_WARN}
|
||||
CFLAGS.query_Rx.c+= ${VENDOR_WARN}
|
||||
CFLAGS.query_ipsec.c+= ${VENDOR_WARN}
|
||||
|
||||
# cdx_ehash.c is ported vendor code (originally cdx-5.03.1/cdx_ehash.c)
|
||||
CFLAGS.cdx_ehash.c+= ${VENDOR_WARN}
|
||||
|
||||
# cdx_mc_freebsd.c is ported vendor code (originally dpa_control_mc.c + cdx_mc_query.c)
|
||||
CFLAGS.cdx_mc_freebsd.c+= ${VENDOR_WARN}
|
||||
|
||||
# IPsec offload — ported from cdx-5.03.1/dpa_ipsec.c + cdx_dpa_ipsec.c
|
||||
CFLAGS.dpa_ipsec_freebsd.c+= ${VENDOR_WARN}
|
||||
CFLAGS.cdx_dpa_ipsec_freebsd.c+= ${VENDOR_WARN}
|
||||
|
||||
.include <bsd.kmod.mk>
|
||||
1607
fastpath/cdx/cdx_ceetm_freebsd.c
Normal file
1607
fastpath/cdx/cdx_ceetm_freebsd.c
Normal file
File diff suppressed because it is too large
Load diff
346
fastpath/cdx/cdx_cmdhandler_freebsd.c
Normal file
346
fastpath/cdx/cdx_cmdhandler_freebsd.c
Normal file
|
|
@ -0,0 +1,346 @@
|
|||
/*
|
||||
* CDX command handler — FreeBSD port
|
||||
*
|
||||
* Minimal changes from cdx_cmdhandler.c. The compat headers handle:
|
||||
* - container_of, list_head, list_entry, list_del, list_empty
|
||||
* - spin_lock_irqsave / spin_unlock_irqrestore
|
||||
* - struct work_struct / schedule_work
|
||||
* - EXPORT_SYMBOL (no-op)
|
||||
* - __init / __exit (no-op)
|
||||
*
|
||||
* The main functional change: comcerto_fpp_send_command is now the
|
||||
* local dispatch function (CDX is not an EXPORT_SYMBOL provider for
|
||||
* FCI; instead CDX registers with FCI via fci_register_send_command).
|
||||
*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017-2018,2021 NXP
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include "cdx.h"
|
||||
|
||||
/* FCI registration API (from fci_freebsd.h) */
|
||||
extern void fci_register_send_command(
|
||||
int (*fn)(uint16_t, uint16_t, uint16_t *, uint16_t *, uint16_t *));
|
||||
|
||||
/* Forward declarations for exported functions */
|
||||
static int cdx_send_command(uint16_t fcode, uint16_t length,
|
||||
uint16_t *payload, uint16_t *rlen, uint16_t *rbuf);
|
||||
int comcerto_fpp_send_command(u16 fcode, u16 length, u16 *payload,
|
||||
u16 *rlen, u16 *rbuf);
|
||||
int comcerto_fpp_send_command_simple(u16 fcode, u16 length, u16 *payload);
|
||||
int comcerto_fpp_send_command_atomic(u16 fcode, u16 length, u16 *payload,
|
||||
void (*callback)(unsigned long, int, u16, u16 *), unsigned long data);
|
||||
int comcerto_fpp_register_event_cb(int (*event_cb)(u16, u16, u16 *));
|
||||
int cdx_ctrl_send_command_simple(u16 fcode, u16 length, u16 *payload);
|
||||
|
||||
CmdProc gCmdProcTable[EVENT_MAX];
|
||||
|
||||
int
|
||||
FCODE_TO_EVENT(U32 fcode)
|
||||
{
|
||||
int eventid;
|
||||
|
||||
switch ((fcode & 0xFF00) >> 8) {
|
||||
case FC_RX:
|
||||
if (fcode >= L2BRIDGE_FIRST_COMMAND &&
|
||||
fcode <= L2BRIDGE_LAST_COMMAND)
|
||||
eventid = EVENT_BRIDGE;
|
||||
else
|
||||
eventid = EVENT_PKT_RX;
|
||||
break;
|
||||
case FC_IPV4: eventid = EVENT_IPV4; break;
|
||||
case FC_IPV6: eventid = EVENT_IPV6; break;
|
||||
case FC_QM: eventid = EVENT_QM; break;
|
||||
case FC_TX: eventid = EVENT_PKT_TX; break;
|
||||
case FC_PPPOE: eventid = EVENT_PPPOE; break;
|
||||
case FC_MC:
|
||||
if (fcode <= CMD_MC4_RESET)
|
||||
eventid = EVENT_MC4;
|
||||
else
|
||||
eventid = EVENT_MC6;
|
||||
break;
|
||||
case FC_RTP: eventid = EVENT_RTP_RELAY; break;
|
||||
case FC_VLAN: eventid = EVENT_VLAN; break;
|
||||
case FC_IPSEC: eventid = EVENT_IPS_IN; break;
|
||||
case FC_TRC: eventid = EVENT_IPS_OUT; break;
|
||||
case FC_TNL: eventid = EVENT_TNL_IN; break;
|
||||
case FC_MACVLAN: eventid = EVENT_MACVLAN; break;
|
||||
case FC_STAT: eventid = EVENT_STAT; break;
|
||||
case FC_ALTCONF: eventid = EVENT_IPV4; break;
|
||||
case FC_WIFI_RX: eventid = EVENT_PKT_WIFIRX; break;
|
||||
case FC_NATPT: eventid = EVENT_NATPT; break;
|
||||
case FC_PKTCAP: eventid = EVENT_PKTCAP; break;
|
||||
case FC_FPPDIAG: eventid = EVENT_IPV4; break;
|
||||
case FC_ICC: eventid = EVENT_ICC; break;
|
||||
case FC_L2TP: eventid = EVENT_L2TP; break;
|
||||
default: eventid = -1; break;
|
||||
}
|
||||
|
||||
return (eventid);
|
||||
}
|
||||
|
||||
void
|
||||
cdx_cmd_handler(U16 fcode, U16 length, U16 *payload, U16 *rlen, U16 *rbuf)
|
||||
{
|
||||
CmdProc cmdproc;
|
||||
int eventid;
|
||||
|
||||
if (length > FPP_MAX_MSG_LENGTH) {
|
||||
rbuf[0] = ERR_WRONG_COMMAND_SIZE;
|
||||
*rlen = 2;
|
||||
return;
|
||||
}
|
||||
|
||||
eventid = FCODE_TO_EVENT(fcode);
|
||||
|
||||
if (fcode == CMD_VOICE_BUFFER_RESET) {
|
||||
rbuf[0] = NO_ERR;
|
||||
*rlen = 2;
|
||||
} else if (eventid >= 0 && eventid < EVENT_MAX &&
|
||||
(cmdproc = gCmdProcTable[eventid]) != NULL) {
|
||||
memcpy(rbuf, payload, length);
|
||||
*rlen = (*cmdproc)(fcode, length, rbuf);
|
||||
if (*rlen > FPP_MAX_MSG_LENGTH) {
|
||||
DPA_ERROR("%s: handler fcode 0x%x returned rlen %u, "
|
||||
"clamped to %u\n", __func__, fcode,
|
||||
*rlen, FPP_MAX_MSG_LENGTH);
|
||||
*rlen = FPP_MAX_MSG_LENGTH;
|
||||
}
|
||||
if (*rlen == 0) {
|
||||
rbuf[0] = NO_ERR;
|
||||
*rlen = 2;
|
||||
}
|
||||
} else {
|
||||
rbuf[0] = ERR_UNKNOWN_COMMAND;
|
||||
*rlen = 2;
|
||||
}
|
||||
|
||||
if (rbuf[0] != NO_ERR)
|
||||
DPRINT("rbuf[0]=0x%04x, *rlen=%d\n", rbuf[0], *rlen);
|
||||
}
|
||||
|
||||
#define CMD_DECLARE(xx) \
|
||||
static int xx##_init_flag = 0; \
|
||||
int xx##_init(void); \
|
||||
void xx##_exit(void);
|
||||
|
||||
#define CMD_INIT(xx) do { \
|
||||
rc = xx##_init(); \
|
||||
if (rc < 0) \
|
||||
goto exit; \
|
||||
xx##_init_flag = 1; \
|
||||
} while (0)
|
||||
|
||||
#define CMD_EXIT(xx) do { \
|
||||
if (xx##_init_flag) \
|
||||
xx##_exit(); \
|
||||
xx##_init_flag = 0; \
|
||||
} while (0)
|
||||
|
||||
CMD_DECLARE(tx)
|
||||
CMD_DECLARE(rx)
|
||||
CMD_DECLARE(pppoe)
|
||||
CMD_DECLARE(vlan)
|
||||
CMD_DECLARE(ipv4)
|
||||
CMD_DECLARE(ipv6)
|
||||
CMD_DECLARE(socket)
|
||||
CMD_DECLARE(tunnel)
|
||||
CMD_DECLARE(natpt)
|
||||
CMD_DECLARE(bridge)
|
||||
CMD_DECLARE(qm)
|
||||
CMD_DECLARE(statistics)
|
||||
#ifdef DPA_IPSEC_OFFLOAD
|
||||
CMD_DECLARE(ipsec)
|
||||
#endif
|
||||
CMD_DECLARE(mc4)
|
||||
CMD_DECLARE(mc6)
|
||||
CMD_DECLARE(rtp_relay)
|
||||
CMD_DECLARE(pktcap)
|
||||
|
||||
int
|
||||
cdx_cmdhandler_init(void)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
CMD_INIT(tx);
|
||||
CMD_INIT(rx);
|
||||
CMD_INIT(pppoe);
|
||||
CMD_INIT(vlan);
|
||||
CMD_INIT(ipv4);
|
||||
CMD_INIT(ipv6);
|
||||
CMD_INIT(socket);
|
||||
CMD_INIT(tunnel);
|
||||
CMD_INIT(natpt);
|
||||
CMD_INIT(bridge);
|
||||
CMD_INIT(qm);
|
||||
CMD_INIT(statistics);
|
||||
#ifdef DPA_IPSEC_OFFLOAD
|
||||
CMD_INIT(ipsec);
|
||||
#endif
|
||||
CMD_INIT(mc4);
|
||||
CMD_INIT(mc6);
|
||||
CMD_INIT(rtp_relay);
|
||||
CMD_INIT(pktcap);
|
||||
|
||||
/* Register CDX command handler with FCI module */
|
||||
fci_register_send_command(cdx_send_command);
|
||||
|
||||
exit:
|
||||
return (rc);
|
||||
}
|
||||
|
||||
void
|
||||
cdx_cmdhandler_exit(void)
|
||||
{
|
||||
CMD_EXIT(pktcap);
|
||||
CMD_EXIT(rtp_relay);
|
||||
CMD_EXIT(mc6);
|
||||
CMD_EXIT(mc4);
|
||||
#ifdef DPA_IPSEC_OFFLOAD
|
||||
CMD_EXIT(ipsec);
|
||||
#endif
|
||||
CMD_EXIT(statistics);
|
||||
CMD_EXIT(qm);
|
||||
CMD_EXIT(bridge);
|
||||
CMD_EXIT(natpt);
|
||||
CMD_EXIT(tunnel);
|
||||
CMD_EXIT(socket);
|
||||
CMD_EXIT(ipv6);
|
||||
CMD_EXIT(ipv4);
|
||||
CMD_EXIT(vlan);
|
||||
CMD_EXIT(pppoe);
|
||||
CMD_EXIT(rx);
|
||||
CMD_EXIT(tx);
|
||||
}
|
||||
|
||||
/*
|
||||
* comcerto_fpp_send_command — synchronous command dispatch.
|
||||
*
|
||||
* On FreeBSD, this is called both internally by CDX and by FCI
|
||||
* (via the registered function pointer). It takes the ctrl mutex,
|
||||
* dispatches the command, and returns.
|
||||
*/
|
||||
static int
|
||||
cdx_send_command(uint16_t fcode, uint16_t length,
|
||||
uint16_t *payload, uint16_t *rlen, uint16_t *rbuf)
|
||||
{
|
||||
struct _cdx_ctrl *ctrl = &cdx_info->ctrl;
|
||||
|
||||
mutex_lock(&ctrl->mutex);
|
||||
cdx_cmd_handler(fcode, length, payload, rlen, rbuf);
|
||||
mutex_unlock(&ctrl->mutex);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Legacy name for internal callers.
|
||||
*/
|
||||
int
|
||||
comcerto_fpp_send_command(u16 fcode, u16 length, u16 *payload,
|
||||
u16 *rlen, u16 *rbuf)
|
||||
{
|
||||
return (cdx_send_command(fcode, length, payload, rlen, rbuf));
|
||||
}
|
||||
|
||||
int
|
||||
comcerto_fpp_send_command_simple(u16 fcode, u16 length, u16 *payload)
|
||||
{
|
||||
u16 rbuf[128];
|
||||
u16 rlen;
|
||||
int rc;
|
||||
|
||||
rc = comcerto_fpp_send_command(fcode, length, payload, &rlen, rbuf);
|
||||
if (rc < 0)
|
||||
return (rc);
|
||||
return (rbuf[0]);
|
||||
}
|
||||
|
||||
void
|
||||
comcerto_fpp_workqueue(struct work_struct *work)
|
||||
{
|
||||
struct _cdx_ctrl *ctrl = container_of(work, struct _cdx_ctrl, work);
|
||||
struct fpp_msg *msg;
|
||||
unsigned long flags;
|
||||
u16 rbuf[128];
|
||||
u16 rlen;
|
||||
int rc;
|
||||
|
||||
spin_lock_irqsave(&ctrl->lock, flags);
|
||||
|
||||
while (!list_empty(&ctrl->msg_list)) {
|
||||
msg = list_entry(ctrl->msg_list.next, struct fpp_msg, list);
|
||||
list_del(&msg->list);
|
||||
spin_unlock_irqrestore(&ctrl->lock, flags);
|
||||
|
||||
rc = comcerto_fpp_send_command(msg->fcode, msg->length,
|
||||
msg->payload, &rlen, rbuf);
|
||||
|
||||
if (msg->callback != NULL)
|
||||
msg->callback(msg->data, rc, rlen, rbuf);
|
||||
|
||||
kfree(msg);
|
||||
|
||||
spin_lock_irqsave(&ctrl->lock, flags);
|
||||
}
|
||||
|
||||
spin_unlock_irqrestore(&ctrl->lock, flags);
|
||||
}
|
||||
|
||||
int
|
||||
comcerto_fpp_send_command_atomic(u16 fcode, u16 length, u16 *payload,
|
||||
void (*callback)(unsigned long, int, u16, u16 *), unsigned long data)
|
||||
{
|
||||
struct _cdx_ctrl *ctrl = &cdx_info->ctrl;
|
||||
struct fpp_msg *msg;
|
||||
unsigned long flags;
|
||||
|
||||
if (length > FPP_MAX_MSG_LENGTH)
|
||||
return (-EINVAL);
|
||||
|
||||
msg = kmalloc(sizeof(struct fpp_msg) + length, GFP_ATOMIC);
|
||||
if (!msg)
|
||||
return (-ENOMEM);
|
||||
|
||||
msg->callback = callback;
|
||||
msg->data = data;
|
||||
msg->payload = (u16 *)(msg + 1);
|
||||
msg->fcode = fcode;
|
||||
msg->length = length;
|
||||
memcpy(msg->payload, payload, length);
|
||||
|
||||
spin_lock_irqsave(&ctrl->lock, flags);
|
||||
list_add(&msg->list, &ctrl->msg_list);
|
||||
spin_unlock_irqrestore(&ctrl->lock, flags);
|
||||
|
||||
schedule_work(&ctrl->work);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Register CDX's command handler with FCI.
|
||||
*
|
||||
* Called from cdx_cmdhandler_init or cdx_main_freebsd.c at module load.
|
||||
*/
|
||||
int
|
||||
comcerto_fpp_register_event_cb(int (*event_cb)(u16, u16, u16 *))
|
||||
{
|
||||
struct _cdx_ctrl *ctrl = &cdx_info->ctrl;
|
||||
|
||||
ctrl->event_cb = event_cb;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cdx_ctrl_send_command_simple(u16 fcode, u16 length, u16 *payload)
|
||||
{
|
||||
u16 rbuf[128];
|
||||
u16 rlen;
|
||||
|
||||
comcerto_fpp_send_command(fcode, length, payload, &rlen, rbuf);
|
||||
return (rbuf[0]);
|
||||
}
|
||||
130
fastpath/cdx/cdx_debug_freebsd.c
Normal file
130
fastpath/cdx/cdx_debug_freebsd.c
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
* cdx_debug_freebsd.c — Debug display routines for FreeBSD CDX port
|
||||
*
|
||||
* Port of cdx-5.03.1/cdx_debug.c. Provides debug display functions
|
||||
* for inspecting CDX data structures (interfaces, routes, conntrack
|
||||
* entries, raw buffers).
|
||||
*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017-2018,2021 NXP
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/systm.h>
|
||||
|
||||
#include "portdefs.h"
|
||||
#include "cdx.h"
|
||||
#include "misc.h"
|
||||
#include "layer2.h"
|
||||
#include "control_ipv4.h"
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
|
||||
|
||||
/*
|
||||
* display_itf — Print CDX itf structure fields.
|
||||
*/
|
||||
void
|
||||
display_itf(struct _itf *itf)
|
||||
{
|
||||
|
||||
printf("\nitf entry\t%p\n", itf);
|
||||
printf("type\t\t%x\n", itf->type);
|
||||
printf("index\t\t%d\n", itf->index);
|
||||
printf("parent\t\t%p\n", itf->phys);
|
||||
}
|
||||
|
||||
/*
|
||||
* display_route_entry — Print CDX route entry fields.
|
||||
*/
|
||||
void
|
||||
display_route_entry(PRouteEntry entry)
|
||||
{
|
||||
|
||||
printf("\nRoute entry\t%p\n", entry);
|
||||
printf("flags\t\t%x\n", entry->flags);
|
||||
printf("mtu\t\t%d\n", entry->mtu);
|
||||
printf("underlying_input_itf\t%p\n", entry->underlying_input_itf);
|
||||
printf("input_itf\t%p\n", entry->input_itf);
|
||||
|
||||
printf("dest_mac\t");
|
||||
display_mac_addr(entry->dstmac);
|
||||
|
||||
if (entry->itf != NULL)
|
||||
display_itf(entry->itf);
|
||||
else
|
||||
printf("itf is NULL\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* display_ctentry — Print CDX conntrack entry fields.
|
||||
*/
|
||||
void
|
||||
display_ctentry(PCtEntry entry)
|
||||
{
|
||||
|
||||
printf("<<<<<\nentry\t%p\n\n", entry);
|
||||
printf("status\t\t%x\n\n", entry->status);
|
||||
|
||||
if (entry->status & CONNTRACK_ORIG)
|
||||
printf("forward dir, ");
|
||||
else
|
||||
printf("rev dir, ");
|
||||
|
||||
if (IS_IPV6_FLOW(entry)) {
|
||||
printf("ipv6 entry\n");
|
||||
printf("source ip\t");
|
||||
display_ipv6_addr((uint8_t *)entry->Saddr_v6);
|
||||
printf("dest ip\t\t");
|
||||
display_ipv6_addr((uint8_t *)entry->Daddr_v6);
|
||||
} else {
|
||||
printf("ipv4 entry\n");
|
||||
printf("source ip\t");
|
||||
display_ipv4_addr(entry->Saddr_v4);
|
||||
printf("dest ip\t\t");
|
||||
display_ipv4_addr(entry->Daddr_v4);
|
||||
}
|
||||
|
||||
if ((entry->proto == IPPROTOCOL_UDP) ||
|
||||
(entry->proto == IPPROTOCOL_TCP)) {
|
||||
printf("protocol\t%d\n", entry->proto);
|
||||
printf("sport\t\t%d\n", ntohs(entry->Sport));
|
||||
printf("dport\t\t%d\n", ntohs(entry->Dport));
|
||||
}
|
||||
|
||||
printf("twin entry\t%p\n", entry->twin);
|
||||
|
||||
if (entry->pRtEntry != NULL)
|
||||
display_route_entry(entry->pRtEntry);
|
||||
else
|
||||
printf("No route entry\n");
|
||||
|
||||
printf(">>>>>\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* display_buf — Hex dump a buffer.
|
||||
*/
|
||||
void
|
||||
display_buf(void *buf, uint32_t size)
|
||||
{
|
||||
uint8_t *ptr;
|
||||
uint32_t ii, jj = 0;
|
||||
char buff[200];
|
||||
|
||||
ptr = (uint8_t *)buf;
|
||||
for (ii = 0; ii < size; ii++) {
|
||||
if (ii && ((ii % 16) == 0)) {
|
||||
buff[jj] = 0;
|
||||
printf("%s\n", buff);
|
||||
jj = 0;
|
||||
}
|
||||
jj += sprintf(buff + jj, "%02x ", *ptr);
|
||||
ptr++;
|
||||
}
|
||||
buff[jj] = 0;
|
||||
printf("%s\n", buff);
|
||||
}
|
||||
127
fastpath/cdx/cdx_dev_freebsd.c
Normal file
127
fastpath/cdx/cdx_dev_freebsd.c
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
/*
|
||||
* CDX char device — FreeBSD port
|
||||
*
|
||||
* Replaces cdx_dev.c. Provides /dev/cdx_ctrl using FreeBSD cdevsw
|
||||
* instead of Linux register_chrdev/class_create/device_create.
|
||||
*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017,2021 NXP
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/conf.h>
|
||||
#include <sys/ioccom.h>
|
||||
#include <sys/malloc.h>
|
||||
#include <sys/module.h>
|
||||
|
||||
#include "cdx.h"
|
||||
#include "cdx_ioctl.h"
|
||||
#include "misc.h"
|
||||
|
||||
static struct cdev *cdx_ctrl_cdev;
|
||||
static volatile int cdx_ctrl_open_count;
|
||||
|
||||
/* Forward declarations for ioctl handlers (stubbed in cdx_dpa_stub.c) */
|
||||
int cdx_ioc_set_dpa_params(unsigned long args);
|
||||
int cdx_ioc_dpa_connadd(unsigned long args);
|
||||
int cdx_ioc_create_mc_group(unsigned long args);
|
||||
int cdx_ioc_add_member_to_group(unsigned long args);
|
||||
int cdx_ioc_add_mcast_table_entry(unsigned long args);
|
||||
|
||||
static int
|
||||
cdx_ctrl_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
|
||||
{
|
||||
/* Allow only one open instance */
|
||||
if (atomic_cmpset_int(&cdx_ctrl_open_count, 0, 1) == 0)
|
||||
return (EBUSY);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
cdx_ctrl_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
|
||||
{
|
||||
atomic_store_int(&cdx_ctrl_open_count, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
cdx_ctrl_ioctl_fbsd(struct cdev *dev, u_long cmd, caddr_t addr,
|
||||
int fflag, struct thread *td)
|
||||
{
|
||||
int retval;
|
||||
|
||||
switch (cmd) {
|
||||
case CDX_CTRL_DPA_SET_PARAMS:
|
||||
retval = cdx_ioc_set_dpa_params((unsigned long)addr);
|
||||
break;
|
||||
|
||||
case CDX_CTRL_DPA_CONNADD:
|
||||
retval = cdx_ioc_dpa_connadd((unsigned long)addr);
|
||||
break;
|
||||
|
||||
case CDX_CTRL_DPA_QOS_CONFIG_ADD:
|
||||
retval = 0;
|
||||
break;
|
||||
|
||||
case CDX_CTRL_DPA_ADD_MCAST_GROUP:
|
||||
retval = cdx_ioc_create_mc_group((unsigned long)addr);
|
||||
break;
|
||||
|
||||
case CDX_CTRL_DPA_ADD_MCAST_MEMBER:
|
||||
retval = cdx_ioc_add_member_to_group((unsigned long)addr);
|
||||
break;
|
||||
|
||||
case CDX_CTRL_DPA_ADD_MCAST_TABLE_ENTRY:
|
||||
retval = cdx_ioc_add_mcast_table_entry((unsigned long)addr);
|
||||
break;
|
||||
|
||||
default:
|
||||
DPA_ERROR("unsupported ioctl cmd %lx\n", cmd);
|
||||
retval = EINVAL;
|
||||
break;
|
||||
}
|
||||
|
||||
return (retval);
|
||||
}
|
||||
|
||||
static struct cdevsw cdx_ctrl_cdevsw = {
|
||||
.d_version = D_VERSION,
|
||||
.d_flags = D_TRACKCLOSE,
|
||||
.d_open = cdx_ctrl_open,
|
||||
.d_close = cdx_ctrl_close,
|
||||
.d_ioctl = cdx_ctrl_ioctl_fbsd,
|
||||
.d_name = CDX_CTRL_CDEVNAME,
|
||||
};
|
||||
|
||||
static void
|
||||
cdx_driver_deinit(void)
|
||||
{
|
||||
if (cdx_ctrl_cdev != NULL) {
|
||||
destroy_dev(cdx_ctrl_cdev);
|
||||
cdx_ctrl_cdev = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
cdx_driver_init(void)
|
||||
{
|
||||
if (cdx_ctrl_cdev != NULL)
|
||||
return (0);
|
||||
|
||||
cdx_ctrl_open_count = 0;
|
||||
|
||||
cdx_ctrl_cdev = make_dev(&cdx_ctrl_cdevsw, 0,
|
||||
UID_ROOT, GID_WHEEL, 0600, CDX_CTRL_CDEVNAME);
|
||||
if (cdx_ctrl_cdev == NULL) {
|
||||
DPA_ERROR("could not create /dev/%s\n", CDX_CTRL_CDEVNAME);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
register_cdx_deinit_func(cdx_driver_deinit);
|
||||
return (0);
|
||||
}
|
||||
1747
fastpath/cdx/cdx_devman_freebsd.c
Normal file
1747
fastpath/cdx/cdx_devman_freebsd.c
Normal file
File diff suppressed because it is too large
Load diff
368
fastpath/cdx/cdx_devoh_freebsd.c
Normal file
368
fastpath/cdx/cdx_devoh_freebsd.c
Normal file
|
|
@ -0,0 +1,368 @@
|
|||
/*
|
||||
* cdx_devoh_freebsd.c — Offline handler (OH) port management for FreeBSD
|
||||
*
|
||||
* Port of cdx-5.03.1/devoh.c. Manages the mapping between OH ports
|
||||
* and their table descriptors, channel IDs, and port type assignments
|
||||
* (WiFi / IPsec). OH ports are used by IPsec and WiFi offload to
|
||||
* re-inject packets into FMan for header manipulation.
|
||||
*
|
||||
* The Linux version creates QMan FQs via cdx_create_fq() and maintains
|
||||
* DQRR callbacks for debug/buffer-release. On FreeBSD, OH port default
|
||||
* and error FQs are managed by the NCSW OH port driver (dpaa_oh.c),
|
||||
* so this file only handles the CDX-level port tracking and table
|
||||
* descriptor management.
|
||||
*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017,2021 NXP
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include "portdefs.h"
|
||||
#include "cdx.h"
|
||||
#include "cdx_ioctl.h"
|
||||
#include "cdx_common.h"
|
||||
#include "misc.h"
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
|
||||
|
||||
/* ================================================================
|
||||
* OH port info tracking
|
||||
* ================================================================ */
|
||||
|
||||
struct oh_port_info {
|
||||
char name[64];
|
||||
uint32_t fm_idx;
|
||||
uint32_t flags;
|
||||
void *td[MAX_MATCH_TABLES];
|
||||
uint32_t channel;
|
||||
struct oh_iface_info *ohinfo;
|
||||
};
|
||||
|
||||
struct oh_port_type {
|
||||
const char *name;
|
||||
uint32_t type;
|
||||
};
|
||||
|
||||
/*
|
||||
* Port type assignments. These match our DTS:
|
||||
* oh@3 → cell-index 3 → port_idx 1 → dpa-fman0-oh@2 → IPsec
|
||||
* oh@4 → cell-index 4 → port_idx 2 → dpa-fman0-oh@3 → WiFi
|
||||
*/
|
||||
static struct oh_port_type ohport_assign[] = {
|
||||
{ "dpa-fman0-oh@3", PORT_TYPE_WIFI },
|
||||
{ "dpa-fman0-oh@2", PORT_TYPE_IPSEC },
|
||||
};
|
||||
#define MAX_OH_PORT_ASSIGN \
|
||||
(sizeof(ohport_assign) / sizeof(struct oh_port_type))
|
||||
|
||||
static struct oh_port_info offline_port_info[MAX_FRAME_MANAGERS][MAX_OF_PORTS];
|
||||
|
||||
extern struct dpa_iface_info *dpa_interface_info;
|
||||
extern spinlock_t dpa_devlist_lock;
|
||||
extern struct cdx_fman_info *fman_info;
|
||||
|
||||
/* ================================================================
|
||||
* get_tableInfo_by_portid — ported from dpa_cfg.c
|
||||
*
|
||||
* Scans fman_info->tbl_info[] for tables attached to the given portid
|
||||
* and fills the td[] array + flags with matching table descriptors.
|
||||
* ================================================================ */
|
||||
|
||||
int
|
||||
get_tableInfo_by_portid(int fm_index, int portid, void **td, int *flags)
|
||||
{
|
||||
uint32_t jj;
|
||||
struct cdx_fman_info *finfo;
|
||||
struct table_info *tinfo;
|
||||
|
||||
if (fm_index < 0 || fm_index >= MAX_FRAME_MANAGERS) {
|
||||
DPA_ERROR("%s: invalid fman index %d\n", __func__, fm_index);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
finfo = &fman_info[fm_index];
|
||||
tinfo = finfo->tbl_info;
|
||||
for (jj = 0; jj < finfo->num_tables; jj++) {
|
||||
if (tinfo->type >= MAX_MATCH_TABLES) {
|
||||
tinfo++;
|
||||
continue;
|
||||
}
|
||||
if (tinfo->port_idx == (uint32_t)(1 << portid)) {
|
||||
td[tinfo->type] = tinfo->id;
|
||||
*flags |= (1 << tinfo->type);
|
||||
}
|
||||
tinfo++;
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* OH port query functions
|
||||
* ================================================================ */
|
||||
|
||||
void *
|
||||
get_oh_port_td(uint32_t fm_index, uint32_t port_idx, uint32_t type)
|
||||
{
|
||||
|
||||
if (fm_index >= MAX_FRAME_MANAGERS || port_idx >= MAX_OF_PORTS ||
|
||||
type >= MAX_MATCH_TABLES)
|
||||
return (NULL);
|
||||
|
||||
return (offline_port_info[fm_index][port_idx].td[type]);
|
||||
}
|
||||
|
||||
int
|
||||
get_ofport_fman_and_portindex(uint32_t fm_index, uint32_t handle,
|
||||
uint32_t *fm_idx, uint32_t *port_idx, uint32_t *portid)
|
||||
{
|
||||
struct oh_port_info *info;
|
||||
|
||||
info = &offline_port_info[fm_index][handle];
|
||||
if (info->ohinfo == NULL)
|
||||
return (-1);
|
||||
|
||||
*fm_idx = info->ohinfo->fman_idx;
|
||||
*port_idx = info->ohinfo->port_idx;
|
||||
*portid = info->ohinfo->portid;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
get_ofport_portid(uint32_t fm_idx, uint32_t handle, uint32_t *portid)
|
||||
{
|
||||
struct oh_port_info *info;
|
||||
|
||||
if (fm_idx >= MAX_FRAME_MANAGERS) {
|
||||
DPA_ERROR("%s: invalid fman index\n", __func__);
|
||||
return (-1);
|
||||
}
|
||||
if (handle >= MAX_OF_PORTS) {
|
||||
DPA_ERROR("%s: invalid ofport handle %u\n", __func__, handle);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
info = &offline_port_info[fm_idx][handle];
|
||||
if (info->ohinfo == NULL)
|
||||
return (-1);
|
||||
|
||||
*portid = info->ohinfo->portid;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
get_ofport_info(uint32_t fm_idx, uint32_t handle, uint32_t *channel,
|
||||
void **td)
|
||||
{
|
||||
struct oh_port_info *info;
|
||||
uint32_t ii;
|
||||
|
||||
if (fm_idx >= MAX_FRAME_MANAGERS) {
|
||||
DPA_ERROR("%s: invalid fman index\n", __func__);
|
||||
return (-1);
|
||||
}
|
||||
if (handle >= MAX_OF_PORTS) {
|
||||
DPA_ERROR("%s: invalid ofport handle %u\n", __func__, handle);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
info = &offline_port_info[fm_idx][handle];
|
||||
if (!(info->flags & IN_USE)) {
|
||||
DPA_ERROR("%s: ofport handle %u not in use\n",
|
||||
__func__, handle);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
*channel = info->channel;
|
||||
get_tableInfo_by_portid(fm_idx, info->ohinfo->portid,
|
||||
info->td, (int *)&info->flags);
|
||||
for (ii = 0; ii < MAX_MATCH_TABLES; ii++) {
|
||||
if (info->flags & (1 << ii))
|
||||
*(td + ii) = info->td[ii];
|
||||
else
|
||||
*(td + ii) = NULL;
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
get_ofport_max_dist(uint32_t fm_idx, uint32_t handle, uint32_t *max_dist)
|
||||
{
|
||||
struct oh_port_info *info;
|
||||
|
||||
if (handle >= MAX_OF_PORTS)
|
||||
return (-1);
|
||||
|
||||
info = &offline_port_info[fm_idx][handle];
|
||||
if (info->ohinfo == NULL)
|
||||
return (-1);
|
||||
|
||||
*max_dist = info->ohinfo->max_dist;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
get_oh_port_pcd_fqinfo(uint32_t fm_idx, uint32_t handle, uint32_t type,
|
||||
uint32_t *pfqid, uint32_t *count)
|
||||
{
|
||||
uint32_t ii;
|
||||
struct oh_iface_info *iface_info;
|
||||
struct cdx_dist_info *dist;
|
||||
struct oh_port_info *info;
|
||||
|
||||
if (fm_idx >= MAX_FRAME_MANAGERS) {
|
||||
DPA_ERROR("%s: invalid fman index\n", __func__);
|
||||
return (-1);
|
||||
}
|
||||
if (handle >= MAX_OF_PORTS) {
|
||||
DPA_ERROR("%s: invalid ofport handle %u\n", __func__, handle);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
info = &offline_port_info[fm_idx][handle];
|
||||
if (!(info->flags & IN_USE)) {
|
||||
DPA_ERROR("%s: ofport handle %u not in use\n",
|
||||
__func__, handle);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
iface_info = info->ohinfo;
|
||||
dist = iface_info->dist_info;
|
||||
for (ii = 0; ii < iface_info->max_dist; ii++) {
|
||||
if (dist->type == type) {
|
||||
*pfqid = dist->base_fqid;
|
||||
*count = dist->count;
|
||||
}
|
||||
dist++;
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Port allocation / release
|
||||
* ================================================================ */
|
||||
|
||||
int
|
||||
alloc_offline_port(uint32_t fm_idx, uint32_t type,
|
||||
qman_cb_dqrr defa_rx __unused, qman_cb_dqrr err_rx __unused)
|
||||
{
|
||||
uint32_t ii;
|
||||
struct oh_port_info *info;
|
||||
|
||||
if (fm_idx >= MAX_FRAME_MANAGERS) {
|
||||
DPA_ERROR("%s: invalid fman index\n", __func__);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
type &= PORT_TYPE_MASK;
|
||||
for (ii = 0; ii < MAX_OF_PORTS; ii++) {
|
||||
info = &offline_port_info[fm_idx][ii];
|
||||
if (info->flags & PORT_VALID) {
|
||||
if ((info->flags & PORT_TYPE_MASK) == type) {
|
||||
info->flags |= IN_USE;
|
||||
DPA_INFO("cdx: devoh: allocated OH port %u "
|
||||
"(type 0x%x)\n", ii, type);
|
||||
return (ii);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DPA_ERROR("%s: no free OH ports for type 0x%x\n", __func__, type);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
int
|
||||
release_offline_port(uint32_t fm_idx, int handle)
|
||||
{
|
||||
struct oh_port_info *info;
|
||||
|
||||
if (fm_idx >= MAX_FRAME_MANAGERS) {
|
||||
DPA_ERROR("%s: invalid fman index\n", __func__);
|
||||
return (-1);
|
||||
}
|
||||
if (handle >= MAX_OF_PORTS) {
|
||||
DPA_ERROR("%s: invalid port index\n", __func__);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
info = &offline_port_info[fm_idx][handle];
|
||||
if (info->flags & IN_USE) {
|
||||
info->flags &= ~IN_USE;
|
||||
DPA_INFO("cdx: devoh: released OH port %d\n", handle);
|
||||
return (0);
|
||||
}
|
||||
|
||||
DPA_ERROR("%s: port %d was not in use\n", __func__, handle);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Table descriptor management
|
||||
* ================================================================ */
|
||||
|
||||
void
|
||||
add_oh_port_tbl_info(uint32_t fm_index, uint32_t port_idx, void *td,
|
||||
uint32_t type)
|
||||
{
|
||||
uint32_t ii;
|
||||
|
||||
for (ii = 0; ii < MAX_OF_PORTS; ii++) {
|
||||
if (port_idx & (1 << ii)) {
|
||||
offline_port_info[fm_index][ii].td[type] = td;
|
||||
offline_port_info[fm_index][ii].flags |= (1 << type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* OH port registration — called during CDX init
|
||||
*
|
||||
* This populates the offline_port_info[][] array from the already-
|
||||
* registered OH interfaces in the devman list. Must be called after
|
||||
* dpa_add_oh_if() has run for all OH ports.
|
||||
* ================================================================ */
|
||||
|
||||
int
|
||||
cdxdrv_create_of_fqs(struct dpa_iface_info *dpa_oh_iface_info)
|
||||
{
|
||||
struct oh_iface_info *iface_info;
|
||||
struct oh_port_info *port_info;
|
||||
uint32_t ii;
|
||||
|
||||
iface_info = &dpa_oh_iface_info->oh_info;
|
||||
port_info =
|
||||
&offline_port_info[iface_info->fman_idx][iface_info->port_idx];
|
||||
|
||||
port_info->fm_idx = iface_info->fman_idx;
|
||||
port_info->ohinfo = iface_info;
|
||||
port_info->channel = iface_info->channel_id;
|
||||
|
||||
snprintf(port_info->name, sizeof(port_info->name),
|
||||
"dpa-fman%u-oh@%u",
|
||||
iface_info->fman_idx, iface_info->port_idx + 1);
|
||||
|
||||
/* Assign port type based on name mapping */
|
||||
for (ii = 0; ii < MAX_OH_PORT_ASSIGN; ii++) {
|
||||
if (strcmp(ohport_assign[ii].name, port_info->name) == 0) {
|
||||
port_info->flags |= ohport_assign[ii].type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
port_info->flags |= (OF_FQID_VALID | PORT_VALID);
|
||||
|
||||
DPA_INFO("cdx: devoh: registered OH port %s — "
|
||||
"fm=%u port=%u channel=0x%x type=0x%x\n",
|
||||
port_info->name, port_info->fm_idx, iface_info->port_idx,
|
||||
port_info->channel, port_info->flags & PORT_TYPE_MASK);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ohport_set_ofne, ohport_set_dma — stubs in cdx_dpa_stub.c until
|
||||
* FM_PORT_SetOhPortOfne/Rda are added to the kernel NCSW driver */
|
||||
670
fastpath/cdx/cdx_dpa_bridge.c
Normal file
670
fastpath/cdx/cdx_dpa_bridge.c
Normal file
|
|
@ -0,0 +1,670 @@
|
|||
/*
|
||||
* cdx_dpa_bridge.c — Discover DPAA1 handles from FreeBSD newbus
|
||||
*
|
||||
* CDX is a loadable kernel module, not a newbus child of fman. This
|
||||
* bridge uses devclass_find/devclass_get_device to locate the fman
|
||||
* driver and extract its PCD, MURAM, and FM handles. It then calls
|
||||
* cdx_dpa_init() to create the classification hash tables and populate
|
||||
* fman_info for use by the CDX runtime.
|
||||
*
|
||||
* Copyright (c) 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/bus.h>
|
||||
#include <sys/malloc.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <net/if.h>
|
||||
#include <net/if_var.h>
|
||||
|
||||
#include "cdx_ioctl.h"
|
||||
#include "cdx_dpa_init.h"
|
||||
#include "cdx_dpa_bridge.h"
|
||||
|
||||
/* FreeBSD DPAA1 driver headers — accessor functions */
|
||||
#include <contrib/ncsw/inc/integrations/dpaa_integration_ext.h>
|
||||
#include <contrib/ncsw/inc/Peripherals/fm_ext.h>
|
||||
#include <contrib/ncsw/inc/Peripherals/fm_port_ext.h>
|
||||
#include <contrib/ncsw/inc/Peripherals/qm_ext.h>
|
||||
#include <dev/dpaa/fman.h>
|
||||
#include <dev/dpaa/if_dtsec.h>
|
||||
#include <dev/dpaa/dpaa_oh.h>
|
||||
|
||||
/* NCSW internal header — need t_FmPcd for physicalMuramBase */
|
||||
#include "fm_pcd.h"
|
||||
|
||||
/*
|
||||
* t_FmMuram is a private struct in fm_muram.c. We duplicate the layout
|
||||
* here to extract baseAddr (the virtual MURAM base address). This is
|
||||
* safe because the struct layout is ABI-stable within the NCSW build.
|
||||
*/
|
||||
struct cdx_fm_muram {
|
||||
t_Handle h_Mem;
|
||||
uintptr_t baseAddr;
|
||||
uint32_t size;
|
||||
};
|
||||
_Static_assert(offsetof(struct cdx_fm_muram, baseAddr) == sizeof(t_Handle),
|
||||
"cdx_fm_muram.baseAddr must follow h_Mem");
|
||||
_Static_assert(offsetof(struct cdx_fm_muram, size) == sizeof(t_Handle) + sizeof(uintptr_t),
|
||||
"cdx_fm_muram.size must follow baseAddr");
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* Module-private state
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
static struct cdx_fman_info cdx_fman_info_data;
|
||||
static device_t cdx_fman_dev;
|
||||
|
||||
/*
|
||||
* OH port state — device_t handles indexed by CDX port number (1-based).
|
||||
* CDX OH port number N maps to cell-index (N + 2), i.e.:
|
||||
* number=1 → cell-index 3 ("dpa-fman0-oh@2", IPsec)
|
||||
* number=2 → cell-index 4 ("dpa-fman0-oh@3", WiFi)
|
||||
* Cell-index 2 (port@82000) is skipped — reserved for Host Command.
|
||||
*/
|
||||
#define CDX_MAX_OH_PORTS 2
|
||||
static device_t cdx_oh_devs[CDX_MAX_OH_PORTS]; /* index 0 = number 1 */
|
||||
|
||||
/*
|
||||
* Global fman_info pointer — declared in cdx_dpa_stub.c (or will be
|
||||
* moved here once stubs are fully replaced). CDX runtime code reads
|
||||
* this via dpa_get_pcdhandle(), dpa_get_tdinfo(), etc.
|
||||
*/
|
||||
extern struct cdx_fman_info *fman_info;
|
||||
|
||||
/* Number of FMan instances (LS1046A has one) */
|
||||
uint32_t cdx_num_fmans;
|
||||
|
||||
/*
|
||||
* FmMurambaseAddr — virtual base address of MURAM.
|
||||
* Used by cdx_ehash.c MURAM_VIRT_TO_PHYS_ADDR macro to convert
|
||||
* virtual MURAM pointers to 24-bit physical offsets for Action
|
||||
* Descriptors. On Linux SDK this is set in fm_muram.c; on FreeBSD
|
||||
* we extract it from the NCSW MURAM handle during bridge init.
|
||||
*/
|
||||
void *FmMurambaseAddr;
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* Initialization
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
int
|
||||
cdx_dpa_bridge_init(void)
|
||||
{
|
||||
devclass_t dc;
|
||||
device_t fmdev;
|
||||
t_Handle fm_handle, pcd_handle, muram_handle, netenv_handle;
|
||||
t_FmPcd *p_FmPcd;
|
||||
int rv;
|
||||
|
||||
/* Find fman device via devclass */
|
||||
dc = devclass_find("fman");
|
||||
if (dc == NULL) {
|
||||
printf("cdx: bridge: fman devclass not found\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
fmdev = devclass_get_device(dc, 0);
|
||||
if (fmdev == NULL) {
|
||||
printf("cdx: bridge: fman0 device not found\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
cdx_fman_dev = fmdev;
|
||||
|
||||
/* Get handles from fman driver */
|
||||
rv = fman_get_handle(fmdev, &fm_handle);
|
||||
if (rv != 0 || fm_handle == NULL) {
|
||||
printf("cdx: bridge: fman_get_handle failed\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
rv = fman_get_pcd_handle(fmdev, &pcd_handle);
|
||||
if (rv != 0 || pcd_handle == NULL) {
|
||||
printf("cdx: bridge: fman_get_pcd_handle failed\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
rv = fman_get_muram_handle(fmdev, &muram_handle);
|
||||
if (rv != 0 || muram_handle == NULL) {
|
||||
printf("cdx: bridge: fman_get_muram_handle failed\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
rv = fman_get_netenv_handle(fmdev, &netenv_handle);
|
||||
if (rv != 0 || netenv_handle == NULL) {
|
||||
printf("cdx: bridge: fman_get_netenv_handle failed\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
printf("cdx: bridge: found fman0 — fm=%p pcd=%p muram=%p netenv=%p\n",
|
||||
fm_handle, pcd_handle, muram_handle, netenv_handle);
|
||||
|
||||
/*
|
||||
* Hot-add Host Command port to existing PCD.
|
||||
* The PCD was created at boot with Parser + KeyGen + CC. This
|
||||
* adds the HC port needed for advanced offload (hash tables,
|
||||
* header manipulation) without rebuilding PCD — existing dtsec
|
||||
* KeyGen schemes and port bindings are preserved.
|
||||
*/
|
||||
rv = fman_reinit_pcd_with_hc(fmdev);
|
||||
if (rv != 0) {
|
||||
printf("cdx: bridge: fman_reinit_pcd_with_hc failed: %d\n",
|
||||
rv);
|
||||
return (rv);
|
||||
}
|
||||
|
||||
printf("cdx: bridge: PCD upgraded — pcd=%p netenv=%p\n",
|
||||
pcd_handle, netenv_handle);
|
||||
|
||||
/*
|
||||
* Initialize fman_info with handles only.
|
||||
*
|
||||
* Hash tables and port/table metadata are NOT created here.
|
||||
* dpa_app (userspace) will program FMan PCD via FMC and pass
|
||||
* all table/port info to CDX via the CDX_CTRL_DPA_SET_PARAMS
|
||||
* ioctl (handled in cdx_dpa_takeover.c).
|
||||
*/
|
||||
memset(&cdx_fman_info_data, 0, sizeof(cdx_fman_info_data));
|
||||
cdx_fman_info_data.index = 0;
|
||||
cdx_fman_info_data.pcd_handle = pcd_handle;
|
||||
cdx_fman_info_data.fm_handle = fm_handle;
|
||||
cdx_fman_info_data.muram_handle = muram_handle;
|
||||
|
||||
/* Get physical MURAM base from PCD internal state */
|
||||
p_FmPcd = (t_FmPcd *)pcd_handle;
|
||||
cdx_fman_info_data.physicalMuramBase = p_FmPcd->physicalMuramBase;
|
||||
|
||||
/*
|
||||
* Set FmMurambaseAddr — the virtual base of the MURAM mapping.
|
||||
* cdx_ehash.c uses MURAM_VIRT_TO_PHYS_ADDR(addr) which computes
|
||||
* (addr - FmMurambaseAddr) to get the 24-bit MURAM offset for ADs.
|
||||
*/
|
||||
{
|
||||
struct cdx_fm_muram *p_muram;
|
||||
p_muram = (struct cdx_fm_muram *)muram_handle;
|
||||
FmMurambaseAddr = (void *)p_muram->baseAddr;
|
||||
}
|
||||
|
||||
printf("cdx: bridge: MURAM virt=%p phys=0x%jx\n",
|
||||
FmMurambaseAddr,
|
||||
(uintmax_t)cdx_fman_info_data.physicalMuramBase);
|
||||
|
||||
/* Set global pointers for CDX runtime */
|
||||
fman_info = &cdx_fman_info_data;
|
||||
cdx_num_fmans = 1;
|
||||
|
||||
/* Initialize per-interface MURAM statistics */
|
||||
rv = cdxdrv_init_stats(muram_handle);
|
||||
if (rv != 0) {
|
||||
printf("cdx: bridge: cdxdrv_init_stats failed: %d\n", rv);
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
/* Print dtsec interface summary for diagnostics */
|
||||
cdx_dpa_bridge_print_dtsec();
|
||||
|
||||
/* Discover OH ports */
|
||||
cdx_dpa_bridge_discover_oh_ports();
|
||||
|
||||
printf("cdx: bridge: initialization complete\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* SDK external hash table API (fm_ehash_freebsd.c) */
|
||||
extern void ExternalHashTableDelete(t_Handle h_HashTbl);
|
||||
|
||||
void
|
||||
cdx_dpa_bridge_destroy(void)
|
||||
{
|
||||
struct cdx_fman_info *fi = &cdx_fman_info_data;
|
||||
uint32_t i;
|
||||
|
||||
/* Clear globals first to prevent concurrent access */
|
||||
fman_info = NULL;
|
||||
cdx_num_fmans = 0;
|
||||
cdx_fman_dev = NULL;
|
||||
|
||||
/*
|
||||
* Delete all hash tables tracked in tbl_info.
|
||||
* Handles both standalone (from cdx_dpa_init) and bound
|
||||
* (from cdx_ioc_set_dpa_params) hash tables.
|
||||
*/
|
||||
if (fi->tbl_info != NULL) {
|
||||
for (i = 0; i < fi->num_tables; i++) {
|
||||
if (fi->tbl_info[i].id != NULL) {
|
||||
ExternalHashTableDelete(fi->tbl_info[i].id);
|
||||
fi->tbl_info[i].id = NULL;
|
||||
}
|
||||
}
|
||||
free(fi->tbl_info, M_DEVBUF);
|
||||
fi->tbl_info = NULL;
|
||||
fi->num_tables = 0;
|
||||
}
|
||||
|
||||
/* Free port info (includes trailing dist_info allocation) */
|
||||
if (fi->portinfo != NULL) {
|
||||
free(fi->portinfo, M_DEVBUF);
|
||||
fi->portinfo = NULL;
|
||||
fi->max_ports = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Clear standalone htable_handles[] tracking array.
|
||||
* In standalone mode these point to the same handles we just
|
||||
* freed above. In FMC mode they were already cleared by the
|
||||
* ioctl handler. Either way, NULL them to prevent stale access.
|
||||
*/
|
||||
cdx_dpa_clear_handles();
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the fman device_t for use by other CDX subsystems.
|
||||
* Needed by cdx_ioc_set_dpa_params() to resolve chardev handle IDs.
|
||||
*/
|
||||
device_t
|
||||
cdx_dpa_bridge_get_fman_dev(void)
|
||||
{
|
||||
|
||||
return (cdx_fman_dev);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* dtsec interface registration
|
||||
*
|
||||
* Enumerates all dtsec devices via newbus and registers each with
|
||||
* CDX's onif table. This is the FreeBSD equivalent of Linux's
|
||||
* dpa_cfg.c loop which calls cdx_add_eth_onif() during module init.
|
||||
*
|
||||
* Must be called after cdx_dpa_bridge_init() (so the bridge can
|
||||
* find dtsec devices) and after devman_init_linux_stats() (so the
|
||||
* devlist lock is initialized).
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/* Declared in cdx_devman_freebsd.c */
|
||||
int cdx_add_eth_onif(char *name);
|
||||
|
||||
int
|
||||
cdx_dpa_bridge_register_dtsec(void)
|
||||
{
|
||||
devclass_t dc;
|
||||
device_t *devlist;
|
||||
int count, i, registered, rv;
|
||||
|
||||
dc = devclass_find("dtsec");
|
||||
if (dc == NULL) {
|
||||
printf("cdx: bridge: dtsec devclass not found\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
if (devclass_get_devices(dc, &devlist, &count) != 0) {
|
||||
printf("cdx: bridge: failed to enumerate dtsec devices\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
registered = 0;
|
||||
for (i = 0; i < count; i++) {
|
||||
struct dtsec_softc *sc;
|
||||
const char *ifname;
|
||||
|
||||
sc = device_get_softc(devlist[i]);
|
||||
if (sc == NULL || sc->sc_ifnet == NULL)
|
||||
continue;
|
||||
|
||||
/* Skip hidden (internally-used) interfaces */
|
||||
if (sc->sc_hidden)
|
||||
continue;
|
||||
|
||||
ifname = if_name(sc->sc_ifnet);
|
||||
rv = cdx_add_eth_onif(__DECONST(char *, ifname));
|
||||
if (rv != 0) {
|
||||
printf("cdx: bridge: failed to register %s: %d\n",
|
||||
ifname, rv);
|
||||
continue;
|
||||
}
|
||||
registered++;
|
||||
}
|
||||
|
||||
free(devlist, M_TEMP);
|
||||
printf("cdx: bridge: registered %d dtsec interface(s) with CDX\n",
|
||||
registered);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* dtsec interface enumeration — diagnostic
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
void
|
||||
cdx_dpa_bridge_print_dtsec(void)
|
||||
{
|
||||
devclass_t dc;
|
||||
device_t *devlist;
|
||||
int count, i;
|
||||
|
||||
dc = devclass_find("dtsec");
|
||||
if (dc == NULL) {
|
||||
printf("cdx: bridge: dtsec devclass not found\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (devclass_get_devices(dc, &devlist, &count) != 0) {
|
||||
printf("cdx: bridge: failed to enumerate dtsec devices\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("cdx: bridge: found %d dtsec interface(s)\n", count);
|
||||
for (i = 0; i < count; i++) {
|
||||
struct dtsec_softc *sc;
|
||||
|
||||
sc = device_get_softc(devlist[i]);
|
||||
if (sc == NULL || sc->sc_ifnet == NULL)
|
||||
continue;
|
||||
|
||||
printf("cdx: bridge: %s — eth_id=%u type=%s "
|
||||
"mac=%02x:%02x:%02x:%02x:%02x:%02x%s\n",
|
||||
if_name(sc->sc_ifnet),
|
||||
sc->sc_eth_id,
|
||||
(sc->sc_eth_dev_type == ETH_10GSEC) ? "10G" : "1G",
|
||||
sc->sc_mac_addr[0], sc->sc_mac_addr[1],
|
||||
sc->sc_mac_addr[2], sc->sc_mac_addr[3],
|
||||
sc->sc_mac_addr[4], sc->sc_mac_addr[5],
|
||||
sc->sc_hidden ? " (hidden)" : "");
|
||||
}
|
||||
|
||||
free(devlist, M_TEMP);
|
||||
}
|
||||
|
||||
/*
|
||||
* Find a dtsec device by interface name. Used by devman (Step 6)
|
||||
* when CMM registers an interface.
|
||||
*
|
||||
* Returns the dtsec_softc pointer, or NULL if not found.
|
||||
*/
|
||||
struct dtsec_softc *
|
||||
cdx_dpa_bridge_find_dtsec(const char *ifname)
|
||||
{
|
||||
devclass_t dc;
|
||||
device_t *devlist;
|
||||
int count, i;
|
||||
struct dtsec_softc *result = NULL;
|
||||
|
||||
dc = devclass_find("dtsec");
|
||||
if (dc == NULL)
|
||||
return (NULL);
|
||||
|
||||
if (devclass_get_devices(dc, &devlist, &count) != 0)
|
||||
return (NULL);
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
struct dtsec_softc *sc;
|
||||
|
||||
sc = device_get_softc(devlist[i]);
|
||||
if (sc == NULL || sc->sc_ifnet == NULL)
|
||||
continue;
|
||||
|
||||
if (strcmp(if_name(sc->sc_ifnet), ifname) == 0) {
|
||||
result = sc;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free(devlist, M_TEMP);
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
* Find a dtsec device by FMan port index (eth_id). Used by devman
|
||||
* to look up dtsec devices by port number rather than name.
|
||||
*
|
||||
* Returns the dtsec_softc pointer, or NULL if not found.
|
||||
*/
|
||||
struct dtsec_softc *
|
||||
cdx_dpa_bridge_find_dtsec_by_ethid(uint8_t eth_id)
|
||||
{
|
||||
devclass_t dc;
|
||||
device_t *devlist;
|
||||
int count, i;
|
||||
struct dtsec_softc *result = NULL;
|
||||
|
||||
dc = devclass_find("dtsec");
|
||||
if (dc == NULL)
|
||||
return (NULL);
|
||||
|
||||
if (devclass_get_devices(dc, &devlist, &count) != 0)
|
||||
return (NULL);
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
struct dtsec_softc *sc;
|
||||
|
||||
sc = device_get_softc(devlist[i]);
|
||||
if (sc == NULL)
|
||||
continue;
|
||||
|
||||
if (sc->sc_eth_id == eth_id) {
|
||||
result = sc;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free(devlist, M_TEMP);
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
* cdx_dpa_bridge_find_ifname_by_fman_params — Find dtsec interface name
|
||||
* by FMan index, port index, and speed type.
|
||||
*
|
||||
* This is the FreeBSD equivalent of Linux's find_osdev_by_fman_params().
|
||||
* Used by cdx_dpa_takeover.c to replace dpa_app-generated portinfo names
|
||||
* (e.g., "dpa-fm0-10G-eth0") with actual FreeBSD kernel interface names
|
||||
* (e.g., "dtsec3").
|
||||
*
|
||||
* type: 1 = 1G (ETH_DTSEC), 10 = 10G (ETH_10GSEC), 0 = OH (skipped)
|
||||
*
|
||||
* Returns a pointer to the interface name string (owned by ifnet, valid
|
||||
* as long as the interface exists), or NULL if not found.
|
||||
*/
|
||||
const char *
|
||||
cdx_dpa_bridge_find_ifname_by_fman_params(uint32_t fm_index, uint32_t port_idx,
|
||||
uint32_t type)
|
||||
{
|
||||
devclass_t dc;
|
||||
device_t *devlist;
|
||||
int count, i;
|
||||
enum eth_dev_type want_type;
|
||||
const char *result = NULL;
|
||||
|
||||
if (type == 0)
|
||||
return (NULL); /* OH ports don't have dtsec interfaces */
|
||||
|
||||
want_type = (type == 10) ? ETH_10GSEC : ETH_DTSEC;
|
||||
|
||||
dc = devclass_find("dtsec");
|
||||
if (dc == NULL)
|
||||
return (NULL);
|
||||
|
||||
if (devclass_get_devices(dc, &devlist, &count) != 0)
|
||||
return (NULL);
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
struct dtsec_softc *sc;
|
||||
|
||||
sc = device_get_softc(devlist[i]);
|
||||
if (sc == NULL || sc->sc_ifnet == NULL)
|
||||
continue;
|
||||
|
||||
if (sc->sc_eth_dev_type == want_type &&
|
||||
sc->sc_eth_id == port_idx) {
|
||||
result = if_name(sc->sc_ifnet);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free(devlist, M_TEMP);
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
* cdx_dpa_bridge_get_rx_pool — Return the BMan pool handle from a dtsec.
|
||||
*
|
||||
* All dtsec interfaces share the same BMan buffer pool (same bpid from FDT).
|
||||
* Returns NULL if no dtsec is found or the pool is not initialized.
|
||||
*/
|
||||
t_Handle
|
||||
cdx_dpa_bridge_get_rx_pool(void)
|
||||
{
|
||||
struct dtsec_softc *sc;
|
||||
|
||||
sc = cdx_dpa_bridge_find_dtsec_by_ethid(0);
|
||||
if (sc != NULL)
|
||||
return (sc->sc_rx_pool);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* QoS helpers — port handle and PCD accessors for cdx_qos_freebsd.c
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Get the NCSW FM_PORT RX handle for a dtsec port by eth_id.
|
||||
* Used by cdx_qos_freebsd.c to call FM_PORT_PcdPlcrAllocProfiles.
|
||||
*/
|
||||
t_Handle
|
||||
cdx_dpa_bridge_get_rx_port_handle(uint8_t eth_id)
|
||||
{
|
||||
struct dtsec_softc *sc;
|
||||
|
||||
sc = cdx_dpa_bridge_find_dtsec_by_ethid(eth_id);
|
||||
if (sc == NULL)
|
||||
return (NULL);
|
||||
return (sc->sc_rxph);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the PCD handle (from fman_info).
|
||||
* All dtsec ports share the same PCD on LS1046A (single FMan).
|
||||
*/
|
||||
t_Handle
|
||||
cdx_dpa_bridge_get_pcd_handle(void)
|
||||
{
|
||||
|
||||
if (fman_info == NULL)
|
||||
return (NULL);
|
||||
return (fman_info->pcd_handle);
|
||||
}
|
||||
|
||||
/*
|
||||
* Check whether a dtsec port is 10G (TGEC) by eth_id.
|
||||
* Used to select appropriate default rate limits.
|
||||
*/
|
||||
bool
|
||||
cdx_dpa_bridge_is_10g_port(uint8_t eth_id)
|
||||
{
|
||||
struct dtsec_softc *sc;
|
||||
|
||||
sc = cdx_dpa_bridge_find_dtsec_by_ethid(eth_id);
|
||||
if (sc == NULL)
|
||||
return (false);
|
||||
return (sc->sc_eth_dev_type == ETH_10GSEC);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* OH port discovery
|
||||
*
|
||||
* Discovers dpaa_oh devices via newbus and stores their device_t
|
||||
* handles for later use by CDX devoh code. OH ports are identified
|
||||
* by cell-index, matching Linux SDK port assignment:
|
||||
* cell-index 3 → CDX number 1 (portid 9, IPsec, "dpa-fman0-oh@2")
|
||||
* cell-index 4 → CDX number 2 (portid 10, WiFi, "dpa-fman0-oh@3")
|
||||
* Cell-index 2 (port@82000) is reserved for Host Command.
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
void
|
||||
cdx_dpa_bridge_discover_oh_ports(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CDX_MAX_OH_PORTS; i++) {
|
||||
int cell_index = i + 3; /* number 1→cell 3, number 2→cell 4 */
|
||||
device_t dev;
|
||||
|
||||
dev = dpaa_oh_find_port(cell_index);
|
||||
if (dev == NULL) {
|
||||
printf("cdx: bridge: OH port cell-index %d not found\n",
|
||||
cell_index);
|
||||
cdx_oh_devs[i] = NULL;
|
||||
continue;
|
||||
}
|
||||
|
||||
cdx_oh_devs[i] = dev;
|
||||
printf("cdx: bridge: OH port %d: cell-index=%d "
|
||||
"QMan chan=0x%x dflt FQID=%u data_off=%u\n",
|
||||
i + 1, cell_index,
|
||||
dpaa_oh_get_qman_channel(dev),
|
||||
dpaa_oh_get_default_fqid(dev),
|
||||
dpaa_oh_get_data_offset(dev));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the device_t for an OH port by CDX port number (1-based).
|
||||
* Returns NULL if the port was not discovered.
|
||||
*/
|
||||
device_t
|
||||
cdx_dpa_bridge_get_oh_dev(int number)
|
||||
{
|
||||
|
||||
if (number < 1 || number > CDX_MAX_OH_PORTS)
|
||||
return (NULL);
|
||||
return (cdx_oh_devs[number - 1]);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the FM_PORT handle for an OH port by CDX port number.
|
||||
* Used by CDX PCD initialization to call FM_PORT_SetPCD().
|
||||
*/
|
||||
t_Handle
|
||||
cdx_dpa_bridge_get_oh_fm_port(int number)
|
||||
{
|
||||
device_t dev;
|
||||
|
||||
dev = cdx_dpa_bridge_get_oh_dev(number);
|
||||
if (dev == NULL)
|
||||
return (NULL);
|
||||
return (dpaa_oh_get_fm_port(dev));
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the QMan channel for an OH port by CDX port number.
|
||||
* Used by devoh to create FQs targeting the OH port.
|
||||
*/
|
||||
uint32_t
|
||||
cdx_dpa_bridge_get_oh_channel(int number)
|
||||
{
|
||||
device_t dev;
|
||||
|
||||
dev = cdx_dpa_bridge_get_oh_dev(number);
|
||||
if (dev == NULL)
|
||||
return (0);
|
||||
return (dpaa_oh_get_qman_channel(dev));
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the default FQID for an OH port by CDX port number.
|
||||
*/
|
||||
uint32_t
|
||||
cdx_dpa_bridge_get_oh_dflt_fqid(int number)
|
||||
{
|
||||
device_t dev;
|
||||
|
||||
dev = cdx_dpa_bridge_get_oh_dev(number);
|
||||
if (dev == NULL)
|
||||
return (0);
|
||||
return (dpaa_oh_get_default_fqid(dev));
|
||||
}
|
||||
44
fastpath/cdx/cdx_dpa_bridge.h
Normal file
44
fastpath/cdx/cdx_dpa_bridge.h
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CDX_DPA_BRIDGE_H
|
||||
#define CDX_DPA_BRIDGE_H
|
||||
|
||||
#include <contrib/ncsw/inc/ncsw_ext.h>
|
||||
|
||||
struct dtsec_softc;
|
||||
|
||||
int cdx_dpa_bridge_init(void);
|
||||
void cdx_dpa_bridge_destroy(void);
|
||||
int cdx_dpa_bridge_register_dtsec(void);
|
||||
void cdx_dpa_bridge_print_dtsec(void);
|
||||
|
||||
/* dtsec lookup helpers for devman */
|
||||
struct dtsec_softc *cdx_dpa_bridge_find_dtsec(const char *ifname);
|
||||
struct dtsec_softc *cdx_dpa_bridge_find_dtsec_by_ethid(uint8_t eth_id);
|
||||
const char *cdx_dpa_bridge_find_ifname_by_fman_params(uint32_t fm_index,
|
||||
uint32_t port_idx, uint32_t type);
|
||||
t_Handle cdx_dpa_bridge_get_rx_pool(void);
|
||||
|
||||
/* OH port discovery and lookup (CDX port number is 1-based) */
|
||||
void cdx_dpa_bridge_discover_oh_ports(void);
|
||||
device_t cdx_dpa_bridge_get_oh_dev(int number);
|
||||
t_Handle cdx_dpa_bridge_get_oh_fm_port(int number);
|
||||
uint32_t cdx_dpa_bridge_get_oh_channel(int number);
|
||||
uint32_t cdx_dpa_bridge_get_oh_dflt_fqid(int number);
|
||||
|
||||
/* Access fman device (for chardev handle resolution) */
|
||||
device_t cdx_dpa_bridge_get_fman_dev(void);
|
||||
|
||||
/* QoS helpers — port handle and PCD accessors */
|
||||
t_Handle cdx_dpa_bridge_get_rx_port_handle(uint8_t eth_id);
|
||||
t_Handle cdx_dpa_bridge_get_pcd_handle(void);
|
||||
bool cdx_dpa_bridge_is_10g_port(uint8_t eth_id);
|
||||
|
||||
/* Number of FMan instances (set by bridge init) */
|
||||
extern uint32_t cdx_num_fmans;
|
||||
|
||||
#endif /* CDX_DPA_BRIDGE_H */
|
||||
374
fastpath/cdx/cdx_dpa_cfg.c
Normal file
374
fastpath/cdx/cdx_dpa_cfg.c
Normal file
|
|
@ -0,0 +1,374 @@
|
|||
/*
|
||||
* cdx_dpa_cfg.c — DPA configuration accessor functions for FreeBSD
|
||||
*
|
||||
* Replaces the dpa_cfg.c stubs in cdx_dpa_stub.c with real implementations
|
||||
* that read from the fman_info global (populated by cdx_dpa_bridge.c +
|
||||
* cdx_dpa_init.c).
|
||||
*
|
||||
* On Linux, these functions are part of cdx-5.03.1/dpa_cfg.c which receives
|
||||
* configuration from userspace via ioctl. On FreeBSD, the configuration
|
||||
* is built in-kernel by cdx_dpa_init.c, so these are pure read accessors.
|
||||
*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017-2018,2021-2022 NXP
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/systm.h>
|
||||
|
||||
#include "portdefs.h"
|
||||
#include "cdx_ioctl.h"
|
||||
#include "cdx_dpa_bridge.h"
|
||||
|
||||
/* NCSW internal header — need t_FmPcd for physicalMuramBase */
|
||||
#include "fm_pcd.h"
|
||||
|
||||
/*
|
||||
* Suppress -Wmissing-prototypes for this file. These accessor functions
|
||||
* have prototypes in the CDX headers (dpa_cfg.h / devman.h) that their
|
||||
* callers include. This file doesn't include all of them to keep
|
||||
* dependencies minimal.
|
||||
*/
|
||||
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
|
||||
|
||||
/* Global fman_info pointer (set by cdx_dpa_bridge_init) */
|
||||
extern struct cdx_fman_info *fman_info;
|
||||
|
||||
/* ================================================================
|
||||
* PCD / FMan handle accessors
|
||||
* ================================================================ */
|
||||
|
||||
/*
|
||||
* dpa_get_pcdhandle — Return PCD handle for a given FMan index.
|
||||
*/
|
||||
void *
|
||||
dpa_get_pcdhandle(uint32_t fm_index)
|
||||
{
|
||||
uint32_t ii;
|
||||
struct cdx_fman_info *finfo;
|
||||
|
||||
finfo = fman_info;
|
||||
for (ii = 0; ii < cdx_num_fmans; ii++) {
|
||||
if (finfo->index == fm_index)
|
||||
return (finfo->pcd_handle);
|
||||
finfo++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* dpa_get_fm_ctx — Return fman_info pointer for a given FMan index.
|
||||
*/
|
||||
void *
|
||||
dpa_get_fm_ctx(uint32_t fm_idx)
|
||||
{
|
||||
|
||||
if (fm_idx < cdx_num_fmans)
|
||||
return (fman_info + fm_idx);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* dpa_get_fm_MURAM_handle — Return MURAM handle and metadata.
|
||||
*/
|
||||
void *
|
||||
dpa_get_fm_MURAM_handle(uint32_t fm_idx, uint64_t *phyBaseAddr,
|
||||
uint32_t *MuramSize)
|
||||
{
|
||||
struct cdx_fman_info *finfo;
|
||||
|
||||
if (fm_idx >= cdx_num_fmans)
|
||||
return (NULL);
|
||||
|
||||
finfo = fman_info + fm_idx;
|
||||
if (phyBaseAddr != NULL)
|
||||
*phyBaseAddr = finfo->physicalMuramBase;
|
||||
if (MuramSize != NULL)
|
||||
*MuramSize = finfo->fmMuramMemSize;
|
||||
return (finfo->muram_handle);
|
||||
}
|
||||
|
||||
/*
|
||||
* dpa_get_fm_timestamp — Return current kernel timestamp.
|
||||
*
|
||||
* On Linux this returns jiffies. On FreeBSD we return ticks,
|
||||
* which serves the same purpose for connection aging.
|
||||
*/
|
||||
uint32_t
|
||||
dpa_get_fm_timestamp(void *fm_ctx)
|
||||
{
|
||||
|
||||
return ((uint32_t)ticks);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Classification table lookup
|
||||
* ================================================================ */
|
||||
|
||||
/*
|
||||
* dpa_get_tdinfo — Find a hash table handle by FMan index, port, and type.
|
||||
*
|
||||
* Scans fman_info[].tbl_info[] for a matching table type whose port_idx
|
||||
* bitmask includes the specified port. Returns the table handle (id).
|
||||
*/
|
||||
void *
|
||||
dpa_get_tdinfo(uint32_t fm_index, uint32_t port_idx, uint32_t type)
|
||||
{
|
||||
struct cdx_fman_info *finfo;
|
||||
struct table_info *tinfo;
|
||||
uint32_t ii, jj;
|
||||
|
||||
finfo = fman_info;
|
||||
for (ii = 0; ii < cdx_num_fmans; ii++) {
|
||||
if (finfo->index == fm_index) {
|
||||
tinfo = finfo->tbl_info;
|
||||
for (jj = 0; jj < finfo->num_tables; jj++) {
|
||||
if ((tinfo->type == type) &&
|
||||
(tinfo->port_idx & (1U << port_idx)))
|
||||
return (tinfo->id);
|
||||
tinfo++;
|
||||
}
|
||||
printf("cdx: dpa_get_tdinfo: no table type %u "
|
||||
"at port %u fm %u\n", type, port_idx, fm_index);
|
||||
return (NULL);
|
||||
}
|
||||
finfo++;
|
||||
}
|
||||
printf("cdx: dpa_get_tdinfo: invalid fm_index %u\n", fm_index);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Port information lookup
|
||||
* ================================================================ */
|
||||
|
||||
/*
|
||||
* get_dpa_port_info — Find port_info by port name.
|
||||
*/
|
||||
struct cdx_port_info *
|
||||
get_dpa_port_info(char *name)
|
||||
{
|
||||
uint32_t ii;
|
||||
struct cdx_fman_info *finfo;
|
||||
|
||||
finfo = fman_info;
|
||||
for (ii = 0; ii < cdx_num_fmans; ii++) {
|
||||
struct cdx_port_info *port_info;
|
||||
uint32_t jj;
|
||||
|
||||
port_info = finfo->portinfo;
|
||||
for (jj = 0; jj < finfo->max_ports; jj++) {
|
||||
if (strcmp(name, port_info->name) == 0)
|
||||
return (port_info);
|
||||
port_info++;
|
||||
}
|
||||
finfo++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* get_dpa_port_name — Find port name by port ID.
|
||||
*/
|
||||
char *
|
||||
get_dpa_port_name(uint32_t portid)
|
||||
{
|
||||
uint32_t ii;
|
||||
struct cdx_fman_info *finfo;
|
||||
|
||||
finfo = fman_info;
|
||||
for (ii = 0; ii < cdx_num_fmans; ii++) {
|
||||
struct cdx_port_info *port_info;
|
||||
uint32_t jj;
|
||||
|
||||
port_info = finfo->portinfo;
|
||||
for (jj = 0; jj < finfo->max_ports; jj++) {
|
||||
if (port_info->portid == portid)
|
||||
return (port_info->name);
|
||||
port_info++;
|
||||
}
|
||||
finfo++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* get_dpa_oh_iface_info — Populate OH interface info from port config.
|
||||
*
|
||||
* Searches fman_info->portinfo[] for a matching port name and copies
|
||||
* portid, max_dist, and dist_info into the caller's oh_iface_info.
|
||||
* Called from dpa_add_oh_if() in cdx_devman_freebsd.c.
|
||||
*/
|
||||
int
|
||||
get_dpa_oh_iface_info(struct oh_iface_info *iface_info, char *name)
|
||||
{
|
||||
uint32_t ii;
|
||||
struct cdx_fman_info *finfo;
|
||||
|
||||
finfo = fman_info;
|
||||
for (ii = 0; ii < cdx_num_fmans; ii++) {
|
||||
struct cdx_port_info *port_info;
|
||||
uint32_t jj;
|
||||
|
||||
port_info = finfo->portinfo;
|
||||
for (jj = 0; jj < finfo->max_ports; jj++) {
|
||||
if (strcmp(name, port_info->name) == 0) {
|
||||
iface_info->portid = port_info->portid;
|
||||
iface_info->max_dist = port_info->max_dist;
|
||||
iface_info->dist_info = port_info->dist_info;
|
||||
return (0);
|
||||
}
|
||||
port_info++;
|
||||
}
|
||||
finfo++;
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* get_dpa_eth_iface_info — Populate Ethernet interface info from port config.
|
||||
*
|
||||
* Same as get_dpa_oh_iface_info but for Ethernet interfaces.
|
||||
* Called from add_incoming_iface_info() in cdx_devman_freebsd.c.
|
||||
*/
|
||||
int
|
||||
get_dpa_eth_iface_info(struct eth_iface_info *iface_info, char *name)
|
||||
{
|
||||
uint32_t ii;
|
||||
struct cdx_fman_info *finfo;
|
||||
|
||||
finfo = fman_info;
|
||||
for (ii = 0; ii < cdx_num_fmans; ii++) {
|
||||
struct cdx_port_info *port_info;
|
||||
uint32_t jj;
|
||||
|
||||
port_info = finfo->portinfo;
|
||||
for (jj = 0; jj < finfo->max_ports; jj++) {
|
||||
if (strcmp(name, port_info->name) == 0) {
|
||||
iface_info->portid = port_info->portid;
|
||||
iface_info->max_dist = port_info->max_dist;
|
||||
iface_info->dist_info = port_info->dist_info;
|
||||
return (0);
|
||||
}
|
||||
port_info++;
|
||||
}
|
||||
finfo++;
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* dpa_get_wan_port — Find the WAN port (10G) for a given FMan.
|
||||
*/
|
||||
int
|
||||
dpa_get_wan_port(uint32_t fm_index, uint32_t *port_idx)
|
||||
{
|
||||
uint32_t ii;
|
||||
struct cdx_fman_info *finfo;
|
||||
|
||||
finfo = fman_info;
|
||||
for (ii = 0; ii < cdx_num_fmans; ii++) {
|
||||
if (finfo->index == fm_index) {
|
||||
struct cdx_port_info *port_info;
|
||||
uint32_t jj;
|
||||
|
||||
port_info = finfo->portinfo;
|
||||
for (jj = 0; jj < finfo->max_ports; jj++) {
|
||||
if (port_info->type == 10) {
|
||||
*port_idx = port_info->portid;
|
||||
return (0);
|
||||
}
|
||||
port_info++;
|
||||
}
|
||||
}
|
||||
finfo++;
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Memory management (DDR/MURAM)
|
||||
*
|
||||
* MURAM is Device memory on ARM64 (mapped via pmap_mapdev →
|
||||
* Device-nGnRnE). The kernel's optimized memcpy uses NEON/STP
|
||||
* instructions that require Normal memory — they trigger alignment
|
||||
* faults on Device memory. Use word-by-word volatile accesses.
|
||||
*
|
||||
* Size must be a multiple of 4 bytes (all MURAM structures are).
|
||||
* ================================================================ */
|
||||
|
||||
void *
|
||||
create_ddr_and_copy_from_muram(void *muramptr, void **ddrptr, U32 size)
|
||||
{
|
||||
volatile uint32_t *s = (volatile uint32_t *)muramptr;
|
||||
uint32_t *d;
|
||||
U32 nwords = size / 4;
|
||||
U32 i;
|
||||
|
||||
*ddrptr = malloc(size, M_ASK, M_NOWAIT);
|
||||
if (*ddrptr == NULL) {
|
||||
DPA_ERROR("%s: malloc(%u) failed\n", __func__, size);
|
||||
return (NULL);
|
||||
}
|
||||
d = (uint32_t *)*ddrptr;
|
||||
for (i = 0; i < nwords; i++)
|
||||
d[i] = s[i];
|
||||
__asm __volatile("dsb sy" ::: "memory");
|
||||
return (*ddrptr);
|
||||
}
|
||||
|
||||
void
|
||||
copy_ddr_to_muram_and_free_ddr(void *muramptr, void **ddrptr, U32 size)
|
||||
{
|
||||
volatile uint32_t *d = (volatile uint32_t *)muramptr;
|
||||
const uint32_t *s = (const uint32_t *)*ddrptr;
|
||||
U32 nwords = size / 4;
|
||||
U32 i;
|
||||
|
||||
for (i = 0; i < nwords; i++)
|
||||
d[i] = s[i];
|
||||
__asm __volatile("dsb sy" ::: "memory");
|
||||
free(*ddrptr, M_ASK);
|
||||
*ddrptr = NULL;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* PCD Frame Queue tracking
|
||||
*
|
||||
* Singly-linked list of PCD FQs created by devoh / cdx_reassm.
|
||||
* Used by find_pcd_fq_info() to avoid creating duplicate FQs for
|
||||
* the same FQID, and add_pcd_fq_info() to register new ones.
|
||||
* ================================================================ */
|
||||
|
||||
static struct dpa_fq *dpa_pcd_fq;
|
||||
|
||||
/*
|
||||
* find_pcd_fq_info — Check if a PCD FQID is already tracked.
|
||||
* Returns 0 if found, -1 if not.
|
||||
*/
|
||||
int
|
||||
find_pcd_fq_info(uint32_t fqid)
|
||||
{
|
||||
struct dpa_fq *fqinfo;
|
||||
|
||||
for (fqinfo = dpa_pcd_fq; fqinfo != NULL; fqinfo = fqinfo->next) {
|
||||
if (fqinfo->fqid == fqid)
|
||||
return (0);
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* add_pcd_fq_info — Prepend a PCD FQ to the tracked list.
|
||||
*/
|
||||
void
|
||||
add_pcd_fq_info(struct dpa_fq *fq_info)
|
||||
{
|
||||
|
||||
fq_info->next = dpa_pcd_fq;
|
||||
dpa_pcd_fq = fq_info;
|
||||
}
|
||||
68
fastpath/cdx/cdx_dpa_init.c
Normal file
68
fastpath/cdx/cdx_dpa_init.c
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* cdx_dpa_init.c — Cleanup helpers for CDX hash table state
|
||||
*
|
||||
* Hash table creation and fman_info population is now handled by
|
||||
* dpa_app (userspace) via FMC, with handles passed to CDX via the
|
||||
* CDX_CTRL_DPA_SET_PARAMS ioctl (cdx_dpa_takeover.c).
|
||||
*
|
||||
* This file retains only the cleanup functions needed by
|
||||
* cdx_dpa_bridge_destroy() to safely tear down state.
|
||||
*
|
||||
* Copyright (c) 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/systm.h>
|
||||
|
||||
#include <contrib/ncsw/inc/ncsw_ext.h>
|
||||
|
||||
/* SDK external hash table API (fm_ehash_freebsd.c) */
|
||||
extern void ExternalHashTableDelete(t_Handle h_HashTbl);
|
||||
|
||||
/* Forward declarations for functions exported from this file */
|
||||
void cdx_dpa_destroy(void);
|
||||
void cdx_dpa_clear_handles(void);
|
||||
|
||||
/*
|
||||
* No standalone hash tables are created in-kernel.
|
||||
* The htable_handles array exists only for cdx_dpa_clear_handles()
|
||||
* compatibility with cdx_dpa_bridge_destroy().
|
||||
*/
|
||||
#define HTABLE_COUNT 16
|
||||
static t_Handle htable_handles[HTABLE_COUNT];
|
||||
|
||||
/*
|
||||
* Cleanup — delete any hash tables tracked in htable_handles[].
|
||||
* In the dpa_app model this array is always empty (tables are
|
||||
* tracked in fman_info->tbl_info[] instead), but we keep this
|
||||
* as a safety net.
|
||||
*/
|
||||
void
|
||||
cdx_dpa_destroy(void)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < HTABLE_COUNT; i++) {
|
||||
if (htable_handles[i] != NULL) {
|
||||
ExternalHashTableDelete(htable_handles[i]);
|
||||
htable_handles[i] = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Clear the htable_handles array without freeing.
|
||||
*
|
||||
* Called by cdx_dpa_bridge_destroy() after hash tables have been
|
||||
* freed through fman_info->tbl_info[]. Prevents double-free if
|
||||
* handles were ever shared between both tracking mechanisms.
|
||||
*/
|
||||
void
|
||||
cdx_dpa_clear_handles(void)
|
||||
{
|
||||
|
||||
memset(htable_handles, 0, sizeof(htable_handles));
|
||||
}
|
||||
13
fastpath/cdx/cdx_dpa_init.h
Normal file
13
fastpath/cdx/cdx_dpa_init.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/*
|
||||
* Copyright (c) 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CDX_DPA_INIT_H
|
||||
#define CDX_DPA_INIT_H
|
||||
|
||||
void cdx_dpa_destroy(void);
|
||||
void cdx_dpa_clear_handles(void);
|
||||
|
||||
#endif /* CDX_DPA_INIT_H */
|
||||
1568
fastpath/cdx/cdx_dpa_ipsec_freebsd.c
Normal file
1568
fastpath/cdx/cdx_dpa_ipsec_freebsd.c
Normal file
File diff suppressed because it is too large
Load diff
191
fastpath/cdx/cdx_dpa_stub.c
Normal file
191
fastpath/cdx/cdx_dpa_stub.c
Normal file
|
|
@ -0,0 +1,191 @@
|
|||
/*
|
||||
* CDX DPAA hardware function stubs — FreeBSD port
|
||||
*
|
||||
* Residual stubs for DPAA functions that are either:
|
||||
* - Dead code on FreeBSD (zero callers, different architecture)
|
||||
* - Stubs even in the Linux reference (multicast ioctls use FCI path)
|
||||
* - Features not in scope (WiFi offload, OH port NIA reconfiguration)
|
||||
*
|
||||
* Most original stubs have been replaced with real implementations in:
|
||||
* - cdx_devman_freebsd.c (interface management)
|
||||
* - cdx_dpa_cfg.c (FMan/PCD configuration)
|
||||
* - cdx_ehash.c (external hash table operations)
|
||||
* - cdx_dpa_takeover.c (DPA interface registration)
|
||||
* - cdx_devoh_freebsd.c (offline handler port management)
|
||||
* - cdx_ifstats_freebsd.c (interface statistics)
|
||||
* - cdx_qos_freebsd.c (QoS / policer)
|
||||
* - cdx_mc_freebsd.c (multicast)
|
||||
* - cdx_debug_freebsd.c (debug display)
|
||||
* - dpa_ipsec_freebsd.c (IPsec SA management)
|
||||
* - cdx_reassm_freebsd.c (IP reassembly)
|
||||
* - cdx_ceetm_freebsd.c (CEETM QoS)
|
||||
*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017-2018,2021-2022 NXP
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include "portdefs.h"
|
||||
#include "cdx.h"
|
||||
#include "cdx_ioctl.h"
|
||||
#include "misc.h"
|
||||
#include "fm_ehash.h"
|
||||
#include "control_tunnel.h"
|
||||
#include "control_socket.h"
|
||||
#include "module_qm.h"
|
||||
#include "cdx_ceetm_app.h"
|
||||
#include "dpa_ipsec.h"
|
||||
|
||||
/*
|
||||
* Suppress -Wmissing-prototypes for this file. All stub functions have
|
||||
* prototypes in the CDX headers that their callers include. This stub
|
||||
* file intentionally doesn't include every CDX header to keep dependencies
|
||||
* minimal.
|
||||
*/
|
||||
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
|
||||
|
||||
/* ================================================================
|
||||
* Globals referenced by Tier 1 code
|
||||
* ================================================================ */
|
||||
|
||||
/* devman.c globals */
|
||||
struct dpa_iface_info *dpa_interface_info;
|
||||
DEFINE_SPINLOCK(dpa_devlist_lock);
|
||||
|
||||
/* dpa_cfg.c globals */
|
||||
struct cdx_fman_info *fman_info;
|
||||
|
||||
/* num_active_connections — defined in control_ipv4.c (Tier 1) */
|
||||
|
||||
/* Linux network namespace stub — used by dev_get_by_name() */
|
||||
struct net init_net;
|
||||
|
||||
/* ================================================================
|
||||
* Dead-code stubs — zero callers on FreeBSD or permanently N/A
|
||||
* ================================================================ */
|
||||
|
||||
/* dpa_get_tx_chnl_info — zero callers. FreeBSD devman gets the
|
||||
* TX QMan channel directly from sc->sc_port_tx_qman_chan. */
|
||||
int
|
||||
dpa_get_tx_chnl_info(uint32_t fqid, uint32_t *ch_id, uint32_t *wq_id)
|
||||
{
|
||||
if (ch_id)
|
||||
*ch_id = 0;
|
||||
if (wq_id)
|
||||
*wq_id = 0;
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* cdx_ioc_dpa_connadd — test-only ioctl (Linux dpa_test.c).
|
||||
* Production flow creation goes through FCI → control_ipv4.c. */
|
||||
int
|
||||
cdx_ioc_dpa_connadd(unsigned long args)
|
||||
{
|
||||
DPA_INFO("stub: cdx_ioc_dpa_connadd\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Multicast ioctls — stubs even in the Linux reference (dpa_test.c).
|
||||
* Real multicast goes through FCI command handlers
|
||||
* (M_mc4_cmdproc/M_mc6_cmdproc) registered in cdx_mc_freebsd.c. */
|
||||
int
|
||||
cdx_ioc_create_mc_group(unsigned long args)
|
||||
{
|
||||
DPA_INFO("stub: cdx_ioc_create_mc_group (use FCI path)\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cdx_ioc_add_member_to_group(unsigned long args)
|
||||
{
|
||||
DPA_INFO("stub: cdx_ioc_add_member_to_group (use FCI path)\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cdx_ioc_add_mcast_table_entry(unsigned long args)
|
||||
{
|
||||
DPA_INFO("stub: cdx_ioc_add_mcast_table_entry (use FCI path)\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* dpa_classif_table_get_entry_stats_by_ref — only called under
|
||||
* #ifdef PRINT_CTENTRY_STATS (never defined). Dead code. */
|
||||
int
|
||||
dpa_classif_table_get_entry_stats_by_ref(void *td, int dpa_handle,
|
||||
void *stats)
|
||||
{
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* get_ipsec_bp — only called from VWD (WiFi) RX path, not compiled. */
|
||||
struct dpa_bp *
|
||||
get_ipsec_bp(void)
|
||||
{
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/* dpa_register_ipsec_fq_handler — only called under
|
||||
* CONFIG_INET_IPSEC_OFFLOAD (Linux-only Kconfig symbol). */
|
||||
int
|
||||
dpa_register_ipsec_fq_handler(void *handler)
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* WiFi / VWD stubs — no WiFi offload on this platform
|
||||
* ================================================================ */
|
||||
int
|
||||
dpaa_vwd_init(void)
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
dpaa_vwd_exit(void)
|
||||
{
|
||||
/* Stub */
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* OH port NIA/DMA reconfiguration — only called from WiFi path
|
||||
* (dpa_wifi.c:vwd_configure_port_pcd). Stubbed because VWD is N/A.
|
||||
* When needed, implement via dpaa_oh.c kernel driver API.
|
||||
* ================================================================ */
|
||||
|
||||
int
|
||||
ohport_set_ofne(uint32_t handle, uint32_t nia_val)
|
||||
{
|
||||
return (-1);
|
||||
}
|
||||
|
||||
int
|
||||
ohport_set_dma(uint32_t handle, uint32_t val)
|
||||
{
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Remaining devman helpers
|
||||
* ================================================================ */
|
||||
|
||||
/* fm_ehash_freebsd.c timestamp accessors */
|
||||
extern void cdx_ehash_update_timestamp(uint32_t id, uint32_t value);
|
||||
|
||||
void
|
||||
dpa_update_timestamp(uint32_t ts)
|
||||
{
|
||||
|
||||
cdx_ehash_update_timestamp(EXTERNAL_TIMESTAMP_TIMERID, ts);
|
||||
}
|
||||
|
||||
/* cdx_check_rx_iface_type_vlan — real implementation in cdx_devman_freebsd.c */
|
||||
|
||||
/* cdx_debug.c stubs removed — real implementations in cdx_debug_freebsd.c */
|
||||
|
||||
/* mc4_init, mc6_init, mc4_exit, mc6_exit moved to cdx_mc_freebsd.c */
|
||||
|
||||
/* M_ipsec_sa_cache_lookup_by_h — real implementation in control_ipsec.c */
|
||||
618
fastpath/cdx/cdx_dpa_takeover.c
Normal file
618
fastpath/cdx/cdx_dpa_takeover.c
Normal file
|
|
@ -0,0 +1,618 @@
|
|||
/*
|
||||
* cdx_dpa_takeover.c — CDX_CTRL_DPA_SET_PARAMS ioctl handler
|
||||
*
|
||||
* Handles the CDX_CTRL_DPA_SET_PARAMS ioctl from dpa_app. dpa_app
|
||||
* programs FMan PCD via FMC, creating NCSW hash tables and CC trees.
|
||||
* The ioctl passes hash table handles to CDX, which binds them to
|
||||
* CDX enhanced external hash format via ExternalHashTableBindNCSW:
|
||||
* 1. Reusing the MURAM Action Descriptor (same address, CC tree valid)
|
||||
* 2. Overwriting the AD with CDX enhanced external hash format
|
||||
* 3. Allocating new DDR buckets and per-bucket spinlocks
|
||||
*
|
||||
* After bind, CDX runtime (ExternalHashTableAddKey/DeleteKey) manages
|
||||
* flow entries in the DDR buckets. FMan CC tree follows the same MURAM
|
||||
* address into CDX-format entries with hardware opcodes.
|
||||
*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017-2018,2021-2022 NXP
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/malloc.h>
|
||||
|
||||
#include "cdx_ioctl.h"
|
||||
#include "cdx_dpa_bridge.h"
|
||||
|
||||
/* FreeBSD DPAA1 driver — handle resolution */
|
||||
#include <dev/dpaa/fman.h>
|
||||
#include <dev/dpaa/fman_chardev.h>
|
||||
#include <dev/dpaa/bman.h>
|
||||
|
||||
/* NCSW types (t_Handle) */
|
||||
#include <contrib/ncsw/inc/ncsw_ext.h>
|
||||
|
||||
/* NCSW PCD types (for t_FmPcdCcNextEngineParams) */
|
||||
#include <contrib/ncsw/inc/Peripherals/fm_pcd_ext.h>
|
||||
|
||||
/* NCSW QMan types (for e_QmFQChannel, t_QmReceivedFrameCallback) */
|
||||
#include <contrib/ncsw/inc/Peripherals/qm_ext.h>
|
||||
|
||||
/* ExternalHashTableModifyMissNextEngine — provided by kernel fm_ehash.c */
|
||||
extern t_Error ExternalHashTableModifyMissNextEngine(t_Handle h_HashTbl,
|
||||
t_FmPcdCcNextEngineParams *p_FmPcdCcNextEngineParams);
|
||||
|
||||
/* FreeBSD QMan API — kernel symbol resolution */
|
||||
extern t_Handle qman_fqr_create(uint32_t fqids_num,
|
||||
e_QmFQChannel channel, uint8_t wq, bool force_fqid,
|
||||
uint32_t fqid_or_align, bool init_parked, bool hold_active,
|
||||
bool prefer_in_cache, bool congst_avoid_ena, t_Handle congst_group,
|
||||
int8_t overhead_accounting_len, uint32_t tail_drop_threshold);
|
||||
extern t_Error qman_fqr_register_cb(t_Handle fqr,
|
||||
t_QmReceivedFrameCallback *callback, t_Handle app);
|
||||
extern t_Error qman_fqr_free(t_Handle fqr);
|
||||
extern void dtsec_release_rss_fqrs(void);
|
||||
|
||||
/* Global fman_info pointer (declared in cdx_dpa_stub.c) */
|
||||
extern struct cdx_fman_info *fman_info;
|
||||
extern uint32_t cdx_num_fmans;
|
||||
|
||||
/* devman list and OH port registration (cdx_devoh_freebsd.c) */
|
||||
#include "portdefs.h" /* struct dpa_iface_info */
|
||||
#define IF_TYPE_OFPORT (1 << 8) /* from layer2.h */
|
||||
extern struct dpa_iface_info *dpa_interface_info;
|
||||
extern int cdxdrv_create_of_fqs(struct dpa_iface_info *dpa_oh_iface_info);
|
||||
|
||||
/* IPR configuration (cdx_reassm_freebsd.c) */
|
||||
extern struct cdx_ipr_info ipr_info;
|
||||
|
||||
/* QoS policer init (cdx_qos_freebsd.c) */
|
||||
extern int cdx_qos_init_expt_profiles(void);
|
||||
#ifdef ENABLE_INGRESS_QOS
|
||||
extern int cdx_qos_init_ingress_profiles(void);
|
||||
#endif
|
||||
|
||||
#define PORTID_SHIFT_VAL 8
|
||||
|
||||
/*
|
||||
* Distribution FQR tracking for cleanup.
|
||||
*/
|
||||
#define CDX_MAX_DIST_FQRS 64
|
||||
static t_Handle cdx_dist_fqrs[CDX_MAX_DIST_FQRS];
|
||||
static int cdx_num_dist_fqrs;
|
||||
|
||||
/* Global BMan pool handle for releasing buffers in CDX dist FQ callback */
|
||||
static t_Handle cdx_rx_pool;
|
||||
|
||||
/*
|
||||
* cdx_dist_fq_rx_callback — Callback for CDX distribution FQs.
|
||||
*
|
||||
* Frames arriving here matched a KG distribution scheme but didn't
|
||||
* match any hash table entry (and weren't caught by the miss action).
|
||||
* This shouldn't normally happen with correct PCD config, but if it
|
||||
* does we must release the BMan buffer to avoid pool exhaustion.
|
||||
*/
|
||||
static volatile uint64_t cdx_dist_fq_rx_count;
|
||||
|
||||
static e_RxStoreResponse
|
||||
cdx_dist_fq_rx_callback(t_Handle app __unused, t_Handle qm_fqr,
|
||||
t_Handle qm_portal __unused, uint32_t fqid_offset,
|
||||
t_DpaaFD *frame)
|
||||
{
|
||||
void *frame_va;
|
||||
|
||||
atomic_fetchadd_64(&cdx_dist_fq_rx_count, 1);
|
||||
if (cdx_dist_fq_rx_count <= 5 ||
|
||||
(cdx_dist_fq_rx_count % 100000) == 0) {
|
||||
uint32_t base_fqid = QM_FQR_GetFqid(qm_fqr);
|
||||
printf("cdx: dist_fq_rx_callback: count=%ju "
|
||||
"fqid=0x%x (base=0x%x+%u)\n",
|
||||
(uintmax_t)cdx_dist_fq_rx_count,
|
||||
base_fqid + fqid_offset, base_fqid, fqid_offset);
|
||||
}
|
||||
|
||||
frame_va = DPAA_FD_GET_ADDR(frame);
|
||||
if (frame_va != NULL && cdx_rx_pool != NULL)
|
||||
bman_put_buffer(cdx_rx_pool, frame_va);
|
||||
|
||||
return (e_RX_STORE_RESPONSE_CONTINUE);
|
||||
}
|
||||
|
||||
/*
|
||||
* cdx_create_port_fqs — Create QMan FQs for CDX distribution FQIDs.
|
||||
*
|
||||
* CDX PCD uses KG distribution schemes that hash frames to FQIDs
|
||||
* computed as base_fqid + (portid << 8). These FQIDs MUST exist as
|
||||
* initialized FQs in QMan — otherwise the CDX microcode's POST_BMI_ENQ
|
||||
* handler fails to release FMan tasks (TNUMs), stalling the pipeline.
|
||||
*
|
||||
* Called after fman_info is populated with port/distribution metadata
|
||||
* from the CDX_CTRL_DPA_SET_PARAMS ioctl.
|
||||
*/
|
||||
int
|
||||
cdx_create_port_fqs(void)
|
||||
{
|
||||
struct cdx_fman_info *fi = fman_info;
|
||||
uint32_t i, j;
|
||||
int nfqr = 0;
|
||||
|
||||
if (fi == NULL || fi->portinfo == NULL) {
|
||||
printf("cdx: create_port_fqs: no port info\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Grab BMan pool handle from first dtsec for buffer release in
|
||||
* dist FQ callback. All dtsec interfaces share the same pool. */
|
||||
if (cdx_rx_pool == NULL)
|
||||
cdx_rx_pool = cdx_dpa_bridge_get_rx_pool();
|
||||
|
||||
for (i = 0; i < fi->max_ports; i++) {
|
||||
struct cdx_port_info *pi = &fi->portinfo[i];
|
||||
struct cdx_dist_info *di = pi->dist_info;
|
||||
uint32_t portid = pi->portid;
|
||||
|
||||
if (di == NULL || pi->max_dist == 0)
|
||||
continue;
|
||||
|
||||
for (j = 0; j < pi->max_dist; j++) {
|
||||
uint32_t fqid;
|
||||
uint32_t count;
|
||||
t_Handle fqr;
|
||||
|
||||
count = di[j].count;
|
||||
if (count == 0)
|
||||
continue;
|
||||
|
||||
fqid = di[j].base_fqid +
|
||||
(portid << PORTID_SHIFT_VAL);
|
||||
|
||||
fqr = qman_fqr_create(count,
|
||||
e_QM_FQ_CHANNEL_POOL1, /* pool channel */
|
||||
1, /* wq */
|
||||
true, /* force_fqid */
|
||||
fqid, /* specific FQID */
|
||||
false, /* init_parked */
|
||||
false, /* hold_active */
|
||||
true, /* prefer_in_cache */
|
||||
false, /* congst_avoid */
|
||||
NULL, /* congst_group */
|
||||
0, /* overhead_len */
|
||||
0); /* tail_drop */
|
||||
if (fqr == NULL) {
|
||||
printf("cdx: create_port_fqs: FAILED "
|
||||
"FQID 0x%x count %u port '%s'\n",
|
||||
fqid, count, pi->name);
|
||||
continue; /* non-fatal */
|
||||
}
|
||||
|
||||
qman_fqr_register_cb(fqr,
|
||||
cdx_dist_fq_rx_callback, NULL);
|
||||
|
||||
if (nfqr < CDX_MAX_DIST_FQRS)
|
||||
cdx_dist_fqrs[nfqr] = fqr;
|
||||
nfqr++;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
cdx_num_dist_fqrs = nfqr;
|
||||
printf("cdx: create_port_fqs: %d FQRs created\n", nfqr);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* cdx_ioc_set_dpa_params — Handle CDX_CTRL_DPA_SET_PARAMS ioctl.
|
||||
*
|
||||
* Called from cdx_dev_freebsd.c. The top-level struct is already
|
||||
* in kernel space (FreeBSD _IOWR copies it), but pointer members
|
||||
* inside still reference userspace and need copyin().
|
||||
*
|
||||
* Flow:
|
||||
* 1. copyin fman_info, table_info, port_info, dist_info from userspace
|
||||
* 2. Resolve chardev handle IDs to NCSW hash table handles
|
||||
* 3. Bind each NCSW hash table to CDX (ExternalHashTableBindNCSW)
|
||||
* 4. Tear down old standalone hash tables
|
||||
* 5. Install new metadata into global fman_info
|
||||
*/
|
||||
int
|
||||
cdx_ioc_set_dpa_params(unsigned long args)
|
||||
{
|
||||
struct cdx_ctrl_set_dpa_params *params;
|
||||
struct cdx_fman_info u_finfo;
|
||||
struct table_info *k_tinfo;
|
||||
struct cdx_port_info *k_pinfo, *tmp_pinfo;
|
||||
struct cdx_dist_info *k_dinfo;
|
||||
struct cdx_ipr_info k_ipr;
|
||||
device_t fmdev;
|
||||
struct fmcd_softc *fmcd;
|
||||
t_Handle pcd_handle, fm_handle, muram_handle;
|
||||
uint32_t i, num_tables, max_ports, total_dists;
|
||||
int error;
|
||||
|
||||
params = (struct cdx_ctrl_set_dpa_params *)args;
|
||||
|
||||
if (params->num_fmans == 0 || params->num_fmans > 4) {
|
||||
printf("cdx: set_dpa_params: invalid num_fmans %u\n",
|
||||
params->num_fmans);
|
||||
return (EINVAL);
|
||||
}
|
||||
|
||||
/* LS1046A has exactly 1 FMan */
|
||||
if (params->num_fmans != 1) {
|
||||
printf("cdx: set_dpa_params: only 1 FMan supported, got %u\n",
|
||||
params->num_fmans);
|
||||
return (EINVAL);
|
||||
}
|
||||
|
||||
if (fman_info == NULL) {
|
||||
printf("cdx: set_dpa_params: bridge not initialized\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
/* Get fman device and chardev for handle resolution */
|
||||
fmdev = cdx_dpa_bridge_get_fman_dev();
|
||||
if (fmdev == NULL) {
|
||||
printf("cdx: set_dpa_params: no fman device\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
fmcd = fman_get_fmcd(fmdev);
|
||||
if (fmcd == NULL) {
|
||||
printf("cdx: set_dpa_params: no fmcd chardev\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
|
||||
fman_get_pcd_handle(fmdev, &pcd_handle);
|
||||
fman_get_handle(fmdev, &fm_handle);
|
||||
fman_get_muram_handle(fmdev, &muram_handle);
|
||||
|
||||
/*
|
||||
* Step 1: copyin fman_info from userspace.
|
||||
* params->fman_info is a userspace pointer.
|
||||
*/
|
||||
error = copyin(params->fman_info, &u_finfo, sizeof(u_finfo));
|
||||
if (error != 0) {
|
||||
printf("cdx: set_dpa_params: copyin fman_info: %d\n", error);
|
||||
return (error);
|
||||
}
|
||||
|
||||
num_tables = u_finfo.num_tables;
|
||||
max_ports = u_finfo.max_ports;
|
||||
|
||||
if (num_tables > 256 || max_ports > 32) {
|
||||
printf("cdx: set_dpa_params: invalid num_tables=%u "
|
||||
"max_ports=%u\n", num_tables, max_ports);
|
||||
return (EINVAL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Step 2: copyin table_info array from userspace.
|
||||
* u_finfo.tbl_info is a userspace pointer.
|
||||
*/
|
||||
k_tinfo = malloc(num_tables * sizeof(*k_tinfo),
|
||||
M_DEVBUF, M_WAITOK | M_ZERO);
|
||||
error = copyin(u_finfo.tbl_info, k_tinfo,
|
||||
num_tables * sizeof(*k_tinfo));
|
||||
if (error != 0) {
|
||||
printf("cdx: set_dpa_params: copyin tbl_info: %d\n", error);
|
||||
goto fail_tinfo;
|
||||
}
|
||||
|
||||
/*
|
||||
* Step 2.5: Resolve chardev handle IDs to kernel pointers.
|
||||
*
|
||||
* dpa_app stores opaque handle IDs (from the chardev handle
|
||||
* table) in tbl_info[].id. Translate them to NCSW kernel
|
||||
* pointers (en_exthash_info*) so the rest of the code can
|
||||
* use them directly with ExternalHashTable* functions.
|
||||
*/
|
||||
for (i = 0; i < num_tables; i++) {
|
||||
if (k_tinfo[i].id != NULL) {
|
||||
t_Handle resolved = fmcd_handle_resolve(fmcd,
|
||||
(uint64_t)(uintptr_t)k_tinfo[i].id,
|
||||
FMCD_HDL_HASH_TABLE);
|
||||
if (resolved == NULL) {
|
||||
printf("cdx: set_dpa_params: table %u: "
|
||||
"bad handle 0x%lx\n",
|
||||
i, (unsigned long)(uintptr_t)
|
||||
k_tinfo[i].id);
|
||||
k_tinfo[i].id = NULL;
|
||||
} else {
|
||||
k_tinfo[i].id = resolved;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Step 3: copyin port_info + dist_info from userspace.
|
||||
*
|
||||
* dpa_app allocates portinfo + trailing dist_info as one block.
|
||||
* Each port->dist_info is a userspace pointer into that block.
|
||||
* We copyin portinfo first to learn dist counts, then allocate
|
||||
* a combined kernel buffer and copyin each port's dist_info.
|
||||
*/
|
||||
k_pinfo = NULL;
|
||||
if (max_ports > 0 && u_finfo.portinfo != NULL) {
|
||||
/* Temporary copyin of portinfo to learn dist counts */
|
||||
tmp_pinfo = malloc(max_ports * sizeof(*tmp_pinfo),
|
||||
M_TEMP, M_WAITOK);
|
||||
error = copyin(u_finfo.portinfo, tmp_pinfo,
|
||||
max_ports * sizeof(*tmp_pinfo));
|
||||
if (error != 0) {
|
||||
printf("cdx: set_dpa_params: copyin portinfo: %d\n",
|
||||
error);
|
||||
free(tmp_pinfo, M_TEMP);
|
||||
goto fail_tinfo;
|
||||
}
|
||||
|
||||
/* Calculate total distribution entries */
|
||||
total_dists = 0;
|
||||
for (i = 0; i < max_ports; i++) {
|
||||
if (tmp_pinfo[i].max_dist > 64) {
|
||||
printf("cdx: set_dpa_params: port %u: "
|
||||
"invalid max_dist %u\n",
|
||||
i, tmp_pinfo[i].max_dist);
|
||||
free(tmp_pinfo, M_TEMP);
|
||||
error = EINVAL;
|
||||
goto fail_tinfo;
|
||||
}
|
||||
total_dists += tmp_pinfo[i].max_dist;
|
||||
}
|
||||
|
||||
/* Allocate combined portinfo + dist_info buffer */
|
||||
k_pinfo = malloc(
|
||||
max_ports * sizeof(*k_pinfo) +
|
||||
total_dists * sizeof(struct cdx_dist_info),
|
||||
M_DEVBUF, M_WAITOK | M_ZERO);
|
||||
|
||||
/* Copy portinfo from temp buffer */
|
||||
memcpy(k_pinfo, tmp_pinfo, max_ports * sizeof(*k_pinfo));
|
||||
free(tmp_pinfo, M_TEMP);
|
||||
|
||||
/* copyin each port's dist_info and fix up pointers */
|
||||
k_dinfo = (struct cdx_dist_info *)(k_pinfo + max_ports);
|
||||
for (i = 0; i < max_ports; i++) {
|
||||
struct cdx_dist_info *u_dinfo;
|
||||
|
||||
u_dinfo = k_pinfo[i].dist_info; /* userspace ptr */
|
||||
if (k_pinfo[i].max_dist > 0 && u_dinfo != NULL) {
|
||||
error = copyin(u_dinfo, k_dinfo,
|
||||
k_pinfo[i].max_dist *
|
||||
sizeof(struct cdx_dist_info));
|
||||
if (error != 0) {
|
||||
printf("cdx: set_dpa_params: "
|
||||
"copyin dist_info port %u: %d\n",
|
||||
i, error);
|
||||
goto fail_pinfo;
|
||||
}
|
||||
}
|
||||
k_pinfo[i].dist_info = k_dinfo;
|
||||
k_dinfo += k_pinfo[i].max_dist;
|
||||
}
|
||||
|
||||
/*
|
||||
* Replace dpa_app-generated port names with actual FreeBSD
|
||||
* kernel interface names. dpa_app creates names like
|
||||
* "dpa-fm0-10G-eth0" but CDX commands use kernel names
|
||||
* like "dtsec3". This is the FreeBSD equivalent of Linux
|
||||
* dpa_cfg.c:311-324 which calls find_osdev_by_fman_params().
|
||||
*/
|
||||
for (i = 0; i < max_ports; i++) {
|
||||
const char *ifname;
|
||||
|
||||
if (k_pinfo[i].type == 0)
|
||||
continue; /* OH port — no dtsec */
|
||||
|
||||
ifname = cdx_dpa_bridge_find_ifname_by_fman_params(
|
||||
k_pinfo[i].fm_index, k_pinfo[i].index,
|
||||
k_pinfo[i].type);
|
||||
if (ifname != NULL) {
|
||||
printf("cdx: set_dpa_params: port %s -> %s\n",
|
||||
k_pinfo[i].name, ifname);
|
||||
strlcpy(k_pinfo[i].name, ifname,
|
||||
sizeof(k_pinfo[i].name));
|
||||
} else {
|
||||
printf("cdx: set_dpa_params: WARNING: "
|
||||
"no dtsec for port %s "
|
||||
"(fm%u idx%u type%u)\n",
|
||||
k_pinfo[i].name, k_pinfo[i].fm_index,
|
||||
k_pinfo[i].index, k_pinfo[i].type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Step 4: copyin ipr_info (optional).
|
||||
*/
|
||||
memset(&k_ipr, 0, sizeof(k_ipr));
|
||||
if (params->ipr_info != NULL) {
|
||||
error = copyin(params->ipr_info, &k_ipr, sizeof(k_ipr));
|
||||
if (error != 0) {
|
||||
printf("cdx: set_dpa_params: copyin ipr_info: %d\n",
|
||||
error);
|
||||
goto fail_pinfo;
|
||||
}
|
||||
}
|
||||
/* Store in global for cdx_init_ip_reassembly() */
|
||||
ipr_info = k_ipr;
|
||||
|
||||
/*
|
||||
* Step 5: Patch miss actions on CDX enhanced hash tables.
|
||||
*
|
||||
* With USE_ENHANCED_EHASH, FM_PCD_HashTableSet creates CDX
|
||||
* enhanced hash tables directly (via ExternalHashTableSet).
|
||||
* k_tinfo[].id was resolved from chardev handle IDs to
|
||||
* en_exthash_info* kernel pointers in step 2.5 above.
|
||||
*
|
||||
* Patch each table's miss action to enqueue unmatched frames
|
||||
* to the dtsec driver's RX default FQID (so misses reach the
|
||||
* FreeBSD network stack).
|
||||
*
|
||||
* port_idx is a bitmask: bit N set = table belongs to portid N.
|
||||
* CDX portids 0-7 map to fmlib RX indexes (fmcd rx_port_fqids[]).
|
||||
*/
|
||||
{
|
||||
uint32_t patched = 0, skipped = 0;
|
||||
|
||||
for (i = 0; i < num_tables; i++) {
|
||||
uint32_t port_bits, bit;
|
||||
uint32_t rx_fqid;
|
||||
|
||||
if (k_tinfo[i].id == NULL ||
|
||||
(uintptr_t)k_tinfo[i].id < VM_MIN_KERNEL_ADDRESS ||
|
||||
k_tinfo[i].dpa_type != DPA_CLS_TBL_EXTERNAL_HASH) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Find the first physical port and its RX FQID */
|
||||
port_bits = k_tinfo[i].port_idx;
|
||||
rx_fqid = 0;
|
||||
for (bit = 0; bit < 8; bit++) {
|
||||
if (port_bits & (1U << bit)) {
|
||||
rx_fqid = fmcd_get_rx_dflt_fqid(
|
||||
fmcd, bit);
|
||||
if (rx_fqid != 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (rx_fqid != 0) {
|
||||
t_FmPcdCcNextEngineParams miss_params;
|
||||
|
||||
memset(&miss_params, 0, sizeof(miss_params));
|
||||
miss_params.nextEngine = e_FM_PCD_DONE;
|
||||
miss_params.params.enqueueParams.action =
|
||||
e_FM_PCD_ENQ_FRAME;
|
||||
miss_params.params.enqueueParams.overrideFqid =
|
||||
TRUE;
|
||||
miss_params.params.enqueueParams.newFqid =
|
||||
rx_fqid;
|
||||
|
||||
ExternalHashTableModifyMissNextEngine(
|
||||
k_tinfo[i].id, &miss_params);
|
||||
}
|
||||
patched++;
|
||||
if (patched <= 3 || (patched % 20) == 0) {
|
||||
printf("cdx: set_dpa_params: table %u '%s' "
|
||||
"miss -> FQID %u\n",
|
||||
i, k_tinfo[i].name, rx_fqid);
|
||||
}
|
||||
}
|
||||
|
||||
printf("cdx: set_dpa_params: miss fixup complete — "
|
||||
"%u patched, %u skipped (of %u tables)\n",
|
||||
patched, skipped, num_tables);
|
||||
}
|
||||
|
||||
/*
|
||||
* Step 7: Install new state into global fman_info.
|
||||
*
|
||||
* fman_info points to the static cdx_fman_info_data in bridge.c.
|
||||
* We update it in-place with the new metadata from dpa_app.
|
||||
*
|
||||
* If dpa_app is restarted, old tbl_info/portinfo allocations
|
||||
* must be freed to prevent memory leaks. The hash table handles
|
||||
* (tbl_info[].id) are NOT freed here — they are NCSW objects
|
||||
* owned by the PCD CC tree and remain valid across dpa_app
|
||||
* restarts (only freed on CDX module unload).
|
||||
*/
|
||||
if (fman_info->tbl_info != NULL && fman_info->tbl_info != k_tinfo) {
|
||||
free(fman_info->tbl_info, M_DEVBUF);
|
||||
printf("cdx: set_dpa_params: freed old tbl_info\n");
|
||||
}
|
||||
if (fman_info->portinfo != NULL && fman_info->portinfo != k_pinfo) {
|
||||
free(fman_info->portinfo, M_DEVBUF);
|
||||
printf("cdx: set_dpa_params: freed old portinfo\n");
|
||||
}
|
||||
fman_info->num_tables = num_tables;
|
||||
fman_info->tbl_info = k_tinfo;
|
||||
fman_info->max_ports = max_ports;
|
||||
fman_info->portinfo = k_pinfo;
|
||||
fman_info->index = u_finfo.index;
|
||||
|
||||
/* Rate limiter config from userspace */
|
||||
memcpy(&fman_info->expt_rate_limit_info,
|
||||
&u_finfo.expt_rate_limit_info,
|
||||
sizeof(fman_info->expt_rate_limit_info));
|
||||
fman_info->expt_ratelim_mode = u_finfo.expt_ratelim_mode;
|
||||
fman_info->expt_ratelim_burst_size = u_finfo.expt_ratelim_burst_size;
|
||||
|
||||
/* Use kernel-known NCSW handles (NOT userspace fmlib handles) */
|
||||
fman_info->fm_handle = fm_handle;
|
||||
fman_info->pcd_handle = pcd_handle;
|
||||
fman_info->muram_handle = muram_handle;
|
||||
/* physicalMuramBase + fmMuramMemSize already set by bridge_init */
|
||||
|
||||
/*
|
||||
* Step 7b: Create exception traffic policer profiles.
|
||||
* These rate-limit miss/exception frames reaching the host CPU.
|
||||
*/
|
||||
if (cdx_qos_init_expt_profiles() != 0)
|
||||
printf("cdx: set_dpa_params: expt policer init failed\n");
|
||||
|
||||
/*
|
||||
* Step 7c: Create ingress QoS policer profiles.
|
||||
* Per-queue rate limiting for offloaded flows.
|
||||
*/
|
||||
#ifdef ENABLE_INGRESS_QOS
|
||||
if (cdx_qos_init_ingress_profiles() != 0)
|
||||
printf("cdx: set_dpa_params: ingress policer init failed\n");
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Step 8: Create QMan FQs for CDX distribution FQIDs.
|
||||
*
|
||||
* The CDX microcode's POST_BMI_ENQ handler references distribution
|
||||
* FQIDs internally. Without initialized FQs at these FQIDs, the
|
||||
* microcode fails to release FMan tasks (TNUMs), stalling the
|
||||
* pipeline after ~6 frames.
|
||||
*/
|
||||
|
||||
/* Free driver RSS FQRs 1..127 — orphaned after CDX replaces PCD.
|
||||
* Recovers ~1270 MallocSmart slices for CDX distribution FQRs. */
|
||||
dtsec_release_rss_fqrs();
|
||||
|
||||
error = cdx_create_port_fqs();
|
||||
if (error != 0)
|
||||
printf("cdx: set_dpa_params: create_port_fqs failed: %d\n",
|
||||
error);
|
||||
|
||||
/*
|
||||
* Step 9: Register OH ports in offline_port_info.
|
||||
*
|
||||
* Walk the devman interface list for OH ports and populate the
|
||||
* offline port tracking array (cdx_devoh_freebsd.c). This must
|
||||
* run after dpa_add_oh_if() has registered all OH interfaces.
|
||||
*/
|
||||
{
|
||||
struct dpa_iface_info *p;
|
||||
|
||||
for (p = dpa_interface_info; p != NULL; p = p->next) {
|
||||
if (p->if_flags & IF_TYPE_OFPORT)
|
||||
cdxdrv_create_of_fqs(p);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Step 10: Mark PCD resources as transferred to CDX.
|
||||
*
|
||||
* All PCD objects (hash tables, CC trees, KG schemes, net envs)
|
||||
* created by dpa_app are now owned by CDX. Tell the chardev
|
||||
* so its close handler won't attempt to delete them.
|
||||
*/
|
||||
fmcd_pcd_mark_transferred(fmcd);
|
||||
|
||||
return (0);
|
||||
|
||||
fail_pinfo:
|
||||
if (k_pinfo != NULL)
|
||||
free(k_pinfo, M_DEVBUF);
|
||||
fail_tinfo:
|
||||
free(k_tinfo, M_DEVBUF);
|
||||
return (error);
|
||||
}
|
||||
4245
fastpath/cdx/cdx_ehash.c
Executable file
4245
fastpath/cdx/cdx_ehash.c
Executable file
File diff suppressed because it is too large
Load diff
256
fastpath/cdx/cdx_ifstats_freebsd.c
Normal file
256
fastpath/cdx/cdx_ifstats_freebsd.c
Normal file
|
|
@ -0,0 +1,256 @@
|
|||
/*
|
||||
* cdx_ifstats_freebsd.c — Interface statistics (MURAM-based) for FreeBSD
|
||||
*
|
||||
* Port of cdx-5.03.1/cdx_ifstats.c. Allocates a contiguous block of
|
||||
* FMan MURAM to hold per-interface packet/byte counters that FMan
|
||||
* hardware updates atomically during flow offload processing.
|
||||
*
|
||||
* Two free-lists are maintained:
|
||||
* - PPPoE interfaces: cdx_pppoe_iface_ifinfo (with timestamp)
|
||||
* - All other interfaces: cdx_iface_ifinfo (without timestamp)
|
||||
*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017,2021 NXP
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/malloc.h>
|
||||
|
||||
#include "portdefs.h"
|
||||
#include "cdx.h"
|
||||
#include "cdx_ioctl.h"
|
||||
#include "fm_ehash.h"
|
||||
|
||||
#include <contrib/ncsw/inc/Peripherals/fm_muram_ext.h>
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
|
||||
|
||||
DEFINE_SPINLOCK(dpa_statslist_lock);
|
||||
|
||||
/* Base of stats area (virtual MURAM address) */
|
||||
static void *stats_mem;
|
||||
|
||||
/* Stats area physical MURAM offset */
|
||||
static uint32_t stats_mem_phys;
|
||||
|
||||
/* Free-list for all interfaces other than PPPoE */
|
||||
static struct cdx_iface_ifinfo *ifstats_freelist;
|
||||
|
||||
/* Free-list for PPPoE */
|
||||
static struct cdx_pppoe_iface_ifinfo *pppoe_ifstats_freelist;
|
||||
|
||||
/* MURAM handle for cleanup */
|
||||
static void *stats_muram_handle;
|
||||
|
||||
extern void *FmMurambaseAddr;
|
||||
|
||||
/*
|
||||
* cdxdrv_init_stats — Allocate MURAM and create free-lists.
|
||||
*
|
||||
* Called during CDX bridge init after the MURAM handle and
|
||||
* FmMurambaseAddr are set up.
|
||||
*/
|
||||
int
|
||||
cdxdrv_init_stats(void *muram_handle)
|
||||
{
|
||||
uint32_t ii;
|
||||
uint32_t num_log_ifaces;
|
||||
uint32_t size;
|
||||
struct cdx_pppoe_iface_ifinfo *pppoe_stats;
|
||||
struct cdx_iface_ifinfo *ifstats;
|
||||
|
||||
size = (MAX_PPPoE_INTERFACES *
|
||||
sizeof(struct cdx_pppoe_iface_ifinfo)) +
|
||||
((MAX_LOGICAL_INTERFACES - MAX_PPPoE_INTERFACES) *
|
||||
sizeof(struct cdx_iface_ifinfo));
|
||||
|
||||
stats_mem = FM_MURAM_AllocMem(muram_handle, size, sizeof(uint64_t));
|
||||
if (stats_mem == NULL) {
|
||||
printf("cdx: cdxdrv_init_stats: unable to allocate MURAM "
|
||||
"for iface stats, size %u\n", size);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
stats_muram_handle = muram_handle;
|
||||
stats_mem_phys = (uint32_t)((uint8_t *)stats_mem -
|
||||
(uint8_t *)FmMurambaseAddr);
|
||||
|
||||
printf("cdx: cdxdrv_init_stats: mem=%p phys=0x%x size=%u\n",
|
||||
stats_mem, stats_mem_phys, size);
|
||||
|
||||
spin_lock(&dpa_statslist_lock);
|
||||
|
||||
/* Build PPPoE stats free-list */
|
||||
pppoe_ifstats_freelist = (struct cdx_pppoe_iface_ifinfo *)stats_mem;
|
||||
pppoe_stats = pppoe_ifstats_freelist;
|
||||
for (ii = 0; ii < MAX_PPPoE_INTERFACES; ii++) {
|
||||
if (ii != (MAX_PPPoE_INTERFACES - 1))
|
||||
pppoe_stats->next = (pppoe_stats + 1);
|
||||
else
|
||||
pppoe_stats->next = NULL;
|
||||
pppoe_stats++;
|
||||
}
|
||||
|
||||
/* Build regular interface stats free-list */
|
||||
ifstats = (struct cdx_iface_ifinfo *)pppoe_stats;
|
||||
num_log_ifaces = MAX_LOGICAL_INTERFACES - MAX_PPPoE_INTERFACES;
|
||||
|
||||
ifstats_freelist = ifstats;
|
||||
for (ii = 0; ii < num_log_ifaces; ii++) {
|
||||
if (ii != (num_log_ifaces - 1))
|
||||
ifstats->next = (ifstats + 1);
|
||||
else
|
||||
ifstats->next = NULL;
|
||||
ifstats++;
|
||||
}
|
||||
|
||||
spin_unlock(&dpa_statslist_lock);
|
||||
|
||||
printf("cdx: cdxdrv_init_stats: %u PPPoE + %u regular slots\n",
|
||||
MAX_PPPoE_INTERFACES, num_log_ifaces);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* alloc_iface_stats — Allocate a MURAM stats slot for an interface.
|
||||
*
|
||||
* Pops from the appropriate free-list and computes the rxstats_index
|
||||
* and txstats_index relative to stats_mem. Also allocates a heap-side
|
||||
* last_stats snapshot for delta calculation.
|
||||
*/
|
||||
int
|
||||
alloc_iface_stats(uint32_t dev_type, struct dpa_iface_info *iface)
|
||||
{
|
||||
|
||||
iface->last_stats = kzalloc(sizeof(struct iface_stats), 0);
|
||||
if (iface->last_stats == NULL) {
|
||||
DPA_ERROR("cdx: alloc_iface_stats: "
|
||||
"memory alloc failed for last_stats\n");
|
||||
return (FAILURE);
|
||||
}
|
||||
|
||||
if (dev_type == IF_TYPE_PPPOE) {
|
||||
struct cdx_pppoe_iface_ifinfo *pppoe_stats;
|
||||
|
||||
spin_lock(&dpa_statslist_lock);
|
||||
pppoe_stats = pppoe_ifstats_freelist;
|
||||
if (pppoe_stats != NULL) {
|
||||
pppoe_ifstats_freelist = pppoe_stats->next;
|
||||
memset(pppoe_stats, 0,
|
||||
sizeof(struct cdx_pppoe_iface_ifinfo));
|
||||
iface->rxstats_index =
|
||||
(((uint32_t)((uint8_t *)&pppoe_stats->stats.rxstats -
|
||||
(uint8_t *)stats_mem) /
|
||||
sizeof(struct en_ehash_stats_with_ts)) |
|
||||
STATS_WITH_TS);
|
||||
iface->txstats_index =
|
||||
(((uint32_t)((uint8_t *)&pppoe_stats->stats.txstats -
|
||||
(uint8_t *)stats_mem) /
|
||||
sizeof(struct en_ehash_stats_with_ts)) |
|
||||
STATS_WITH_TS);
|
||||
}
|
||||
iface->stats = pppoe_stats;
|
||||
spin_unlock(&dpa_statslist_lock);
|
||||
} else {
|
||||
struct cdx_iface_ifinfo *ifstats;
|
||||
|
||||
spin_lock(&dpa_statslist_lock);
|
||||
ifstats = ifstats_freelist;
|
||||
if (ifstats != NULL) {
|
||||
ifstats_freelist = ifstats->next;
|
||||
memset(ifstats, 0, sizeof(struct cdx_iface_ifinfo));
|
||||
iface->rxstats_index =
|
||||
((uint32_t)((uint8_t *)&ifstats->stats.rxstats -
|
||||
(uint8_t *)stats_mem) /
|
||||
sizeof(struct en_ehash_stats));
|
||||
iface->txstats_index =
|
||||
((uint32_t)((uint8_t *)&ifstats->stats.txstats -
|
||||
(uint8_t *)stats_mem) /
|
||||
sizeof(struct en_ehash_stats));
|
||||
}
|
||||
iface->stats = ifstats;
|
||||
spin_unlock(&dpa_statslist_lock);
|
||||
}
|
||||
|
||||
if (iface->stats == NULL) {
|
||||
kfree(iface->last_stats);
|
||||
iface->last_stats = NULL;
|
||||
DPA_ERROR("cdx: alloc_iface_stats: "
|
||||
"no free stats slots for type 0x%x\n", dev_type);
|
||||
return (FAILURE);
|
||||
}
|
||||
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
/*
|
||||
* free_iface_stats — Return a MURAM stats slot to the free-list.
|
||||
*/
|
||||
void
|
||||
free_iface_stats(uint32_t dev_type, struct dpa_iface_info *iface)
|
||||
{
|
||||
|
||||
if (iface->stats == NULL)
|
||||
return;
|
||||
|
||||
if (dev_type == IF_TYPE_PPPOE) {
|
||||
struct cdx_pppoe_iface_ifinfo *pppoe_stats;
|
||||
|
||||
pppoe_stats =
|
||||
(struct cdx_pppoe_iface_ifinfo *)iface->stats;
|
||||
spin_lock(&dpa_statslist_lock);
|
||||
pppoe_stats->next = pppoe_ifstats_freelist;
|
||||
pppoe_ifstats_freelist = pppoe_stats;
|
||||
spin_unlock(&dpa_statslist_lock);
|
||||
} else {
|
||||
struct cdx_iface_ifinfo *ifstats;
|
||||
|
||||
ifstats = (struct cdx_iface_ifinfo *)iface->stats;
|
||||
spin_lock(&dpa_statslist_lock);
|
||||
ifstats->next = ifstats_freelist;
|
||||
ifstats_freelist = ifstats;
|
||||
spin_unlock(&dpa_statslist_lock);
|
||||
}
|
||||
|
||||
iface->stats = NULL;
|
||||
|
||||
if (iface->last_stats != NULL) {
|
||||
kfree(iface->last_stats);
|
||||
iface->last_stats = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* get_logical_ifstats_base — Return the MURAM physical offset of the
|
||||
* stats area. Used by cdx_ehash.c to compute 24-bit Action Descriptor
|
||||
* stats pointers.
|
||||
*/
|
||||
uint32_t
|
||||
get_logical_ifstats_base(void)
|
||||
{
|
||||
|
||||
return (stats_mem_phys);
|
||||
}
|
||||
|
||||
/*
|
||||
* cdxdrv_cleanup_stats — Free MURAM stats allocation.
|
||||
*
|
||||
* Called during CDX module unload.
|
||||
*/
|
||||
void
|
||||
cdxdrv_cleanup_stats(void)
|
||||
{
|
||||
|
||||
if (stats_mem != NULL && stats_muram_handle != NULL) {
|
||||
FM_MURAM_FreeMem(stats_muram_handle, stats_mem);
|
||||
stats_mem = NULL;
|
||||
stats_muram_handle = NULL;
|
||||
printf("cdx: cdxdrv_cleanup_stats: freed MURAM stats\n");
|
||||
}
|
||||
}
|
||||
327
fastpath/cdx/cdx_main_freebsd.c
Normal file
327
fastpath/cdx/cdx_main_freebsd.c
Normal file
|
|
@ -0,0 +1,327 @@
|
|||
/*
|
||||
* CDX module init/exit — FreeBSD port
|
||||
*
|
||||
* Replaces cdx_main.c. Uses DECLARE_MODULE/DEV_MODULE instead of
|
||||
* module_init/module_exit. Removes device_register (unnecessary on
|
||||
* FreeBSD). Launches dpa_app from kernel context via fork1/kern_execve
|
||||
* (FreeBSD equivalent of Linux call_usermodehelper).
|
||||
*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017-2018,2021 NXP
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/module.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/conf.h>
|
||||
#include <sys/malloc.h>
|
||||
|
||||
#include "portdefs.h"
|
||||
#include "cdx.h"
|
||||
#include "cdx_cmdhandler.h"
|
||||
#include "cdx_dpa_bridge.h"
|
||||
#include "dpa_ipsec.h"
|
||||
|
||||
/* IP reassembly init/deinit (cdx_reassm_freebsd.c) */
|
||||
extern void cdx_deinit_ip_reassembly(void);
|
||||
|
||||
/* Forwarding FQ diagnostics (cdx_devman_freebsd.c) */
|
||||
extern void cdx_diag_dump_fwd_fqs(void);
|
||||
|
||||
/*
|
||||
* Define CDX global variables. The prelude force-includes cdx.h
|
||||
* (which includes globals.h) before DEFINE_GLOBALS can be set,
|
||||
* so all globals end up as extern declarations. Re-include
|
||||
* globals.h with the guard cleared and DEFINE_GLOBALS set to
|
||||
* actually allocate storage.
|
||||
*/
|
||||
#undef _GLOBALS_H_
|
||||
#undef GLOBAL_DEFINE
|
||||
#undef GLOBAL_INIT
|
||||
#define DEFINE_GLOBALS
|
||||
#include "globals.h"
|
||||
|
||||
MALLOC_DEFINE(M_ASK, "ask", "ASK CDX module");
|
||||
MALLOC_DEFINE(M_CDX, "cdx", "CDX IPsec allocations");
|
||||
|
||||
/* RCU lock used by linux_mutex_compat.h */
|
||||
struct sx ask_rcu_lock;
|
||||
|
||||
static uint32_t init_level;
|
||||
static cdx_deinit_func deinit_fn[MAX_CDX_INIT_FUNCTIONS];
|
||||
|
||||
void
|
||||
register_cdx_deinit_func(cdx_deinit_func func)
|
||||
{
|
||||
if (init_level == MAX_CDX_INIT_FUNCTIONS) {
|
||||
printf("%s: can't register deinit function, "
|
||||
"increase MAX_CDX_INIT_FUNCTIONS\n", __func__);
|
||||
return;
|
||||
}
|
||||
deinit_fn[init_level] = func;
|
||||
init_level++;
|
||||
}
|
||||
|
||||
static void
|
||||
cdx_ctrl_deinit(void)
|
||||
{
|
||||
struct _cdx_ctrl *ctrl = &cdx_info->ctrl;
|
||||
|
||||
mutex_lock(&ctrl->mutex);
|
||||
cdx_cmdhandler_exit();
|
||||
mutex_unlock(&ctrl->mutex);
|
||||
}
|
||||
|
||||
static int
|
||||
cdx_ctrl_init(struct _cdx_info *info)
|
||||
{
|
||||
struct _cdx_ctrl *ctrl = &info->ctrl;
|
||||
int rc;
|
||||
|
||||
mutex_init(&ctrl->mutex);
|
||||
spin_lock_init(&ctrl->lock);
|
||||
INIT_WORK(&ctrl->work, comcerto_fpp_workqueue);
|
||||
INIT_LIST_HEAD(&ctrl->msg_list);
|
||||
|
||||
ctrl->dev = &info->dev;
|
||||
rc = cdx_ctrl_timer_init(ctrl);
|
||||
if (rc)
|
||||
goto error;
|
||||
|
||||
mutex_lock(&ctrl->mutex);
|
||||
rc = cdx_cmdhandler_init();
|
||||
mutex_unlock(&ctrl->mutex);
|
||||
|
||||
/* Timer thread already started by kthread_add in timer_init */
|
||||
|
||||
register_cdx_deinit_func(cdx_ctrl_deinit);
|
||||
error:
|
||||
return (rc);
|
||||
}
|
||||
|
||||
/* Forward declarations — cdx_sysctl.c */
|
||||
int cdx_init_fqid_procfs(void);
|
||||
void cdx_sysctl_init(void);
|
||||
void cdx_sysctl_fini(void);
|
||||
|
||||
/* Forward declarations — cdx_qos_freebsd.c */
|
||||
int cdx_qos_init_ff_profiles(void);
|
||||
void cdx_qos_cleanup_ff_profiles(void);
|
||||
|
||||
/* cdx_usermodehelper.c */
|
||||
extern int cdx_call_usermodehelper(const char *path, char *const argv[],
|
||||
char *const envp[], int *statusp);
|
||||
|
||||
/*
|
||||
* start_dpa_app — Launch dpa_app from kernel context.
|
||||
*
|
||||
* dpa_app opens /dev/fm0*, programs FMan PCD via FMC (from XML configs
|
||||
* at /etc/cdx_*.xml), then sends CDX_CTRL_DPA_SET_PARAMS ioctl to
|
||||
* /dev/cdx_ctrl. The ioctl handler (cdx_dpa_takeover.c) binds the
|
||||
* NCSW hash tables to CDX enhanced format via ExternalHashTableBindNCSW.
|
||||
*
|
||||
* This is the FreeBSD equivalent of Linux's start_dpa_app() which
|
||||
* uses call_usermodehelper(UMH_WAIT_PROC).
|
||||
*/
|
||||
static int
|
||||
start_dpa_app(void)
|
||||
{
|
||||
static char *dpa_app_path = "/usr/local/sbin/dpa_app";
|
||||
static char *argv[] = { "/usr/local/sbin/dpa_app", NULL };
|
||||
static char *envp[] = {
|
||||
"HOME=/",
|
||||
"TERM=dumb",
|
||||
"PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin",
|
||||
NULL
|
||||
};
|
||||
int error;
|
||||
|
||||
printf("cdx: launching %s\n", dpa_app_path);
|
||||
error = cdx_call_usermodehelper(dpa_app_path, argv, envp, NULL);
|
||||
if (error != 0)
|
||||
printf("cdx: %s failed: %d\n", dpa_app_path, error);
|
||||
else
|
||||
printf("cdx: %s completed successfully\n", dpa_app_path);
|
||||
|
||||
return (error);
|
||||
}
|
||||
|
||||
static void
|
||||
cdx_module_deinit(void)
|
||||
{
|
||||
int ii;
|
||||
|
||||
cdx_sysctl_fini();
|
||||
|
||||
if (init_level > 0) {
|
||||
ii = (int)(init_level - 1);
|
||||
while (1) {
|
||||
if (deinit_fn[ii])
|
||||
deinit_fn[ii]();
|
||||
if (ii == 0)
|
||||
break;
|
||||
ii--;
|
||||
}
|
||||
}
|
||||
kfree(cdx_info);
|
||||
}
|
||||
|
||||
static int
|
||||
cdx_module_init(void)
|
||||
{
|
||||
int rc = 0;
|
||||
int ii;
|
||||
|
||||
printf("cdx: initializing CDX module\n");
|
||||
|
||||
for (ii = 0; ii < MAX_CDX_INIT_FUNCTIONS; ii++)
|
||||
deinit_fn[ii] = NULL;
|
||||
init_level = 0;
|
||||
|
||||
cdx_info = kzalloc(sizeof(struct _cdx_info), GFP_KERNEL);
|
||||
if (!cdx_info) {
|
||||
printf("cdx: error allocating cdx_info\n");
|
||||
return (-ENOMEM);
|
||||
}
|
||||
|
||||
/* Skip device_register — KLD module itself is the presence indicator */
|
||||
|
||||
/*
|
||||
* Initialize sysctl tree early so that cdx_sysctl_fini() in the
|
||||
* cleanup path always has a valid context, even if init fails
|
||||
* before the module is fully operational.
|
||||
*/
|
||||
cdx_sysctl_init();
|
||||
|
||||
rc = cdx_ctrl_init(cdx_info);
|
||||
if (rc != 0) {
|
||||
printf("cdx: cdx_ctrl_init failed\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rc = devman_init_linux_stats();
|
||||
if (rc != 0) {
|
||||
printf("cdx: devman_init_linux_stats failed\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Discover FMan/dtsec handles and populate fman_info globals.
|
||||
* Hash tables are NOT created here — dpa_app (via start_dpa_app)
|
||||
* programs FMan PCD and registers tables via ioctl.
|
||||
*/
|
||||
rc = cdx_dpa_bridge_init();
|
||||
if (rc != 0) {
|
||||
printf("cdx: cdx_dpa_bridge_init failed\n");
|
||||
goto exit;
|
||||
}
|
||||
register_cdx_deinit_func(cdx_dpa_bridge_destroy);
|
||||
|
||||
/*
|
||||
* Create fast-forward policer profiles for all dtsec ports.
|
||||
* Must happen after cdx_dpa_bridge_init() (PCD handle available)
|
||||
* and before start_dpa_app() (which calls FM_PORT_SetPCD).
|
||||
* The kernel already called FM_PORT_PcdPlcrAllocProfiles(rxph, 1)
|
||||
* at boot, so the profile slots are reserved.
|
||||
*/
|
||||
cdx_qos_init_ff_profiles();
|
||||
register_cdx_deinit_func(cdx_qos_cleanup_ff_profiles);
|
||||
|
||||
rc = cdx_driver_init();
|
||||
if (rc != 0) {
|
||||
printf("cdx: cdx_driver_init failed\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
cdx_init_fqid_procfs();
|
||||
|
||||
/*
|
||||
* Launch dpa_app to program FMan PCD and register hash tables.
|
||||
* Must happen after cdx_driver_init() (creates /dev/cdx_ctrl
|
||||
* that dpa_app sends the ioctl to) and after
|
||||
* cdx_dpa_bridge_init() (fman discovery).
|
||||
*/
|
||||
rc = start_dpa_app();
|
||||
if (rc != 0) {
|
||||
printf("cdx: start_dpa_app failed\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Register all dtsec interfaces in CDX onif table.
|
||||
* Must happen after start_dpa_app() (hash tables now bound in
|
||||
* CDX enhanced format). This is the FreeBSD equivalent of
|
||||
* Linux dpa_cfg.c calling cdx_add_eth_onif().
|
||||
*/
|
||||
rc = cdx_dpa_bridge_register_dtsec();
|
||||
if (rc != 0) {
|
||||
printf("cdx: dtsec registration failed: %d\n", rc);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* VWD (Virtual WiFi Driver) not applicable — no WiFi offload */
|
||||
|
||||
/*
|
||||
* Initialize frag/common-db module: allocates MURAM block for
|
||||
* the microcode common database (frag params + DSCP-to-FQID map).
|
||||
* Every hash table entry's ENQUEUE_PKT opcode references this
|
||||
* MURAM block via word2. Without it, word2 contains a garbage
|
||||
* pointer and the microcode reads corrupt data.
|
||||
*
|
||||
* On FreeBSD, CDX_FRAG_USE_BUFF_POOL is not defined, so the
|
||||
* fragment buffer pool creation is skipped — only the MURAM
|
||||
* common database is allocated and zero-initialized.
|
||||
*/
|
||||
if (cdx_init_frag_module()) {
|
||||
printf("cdx: cdx_init_frag_module failed\n");
|
||||
/* Non-fatal for init, but flow offload will not work */
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize IP reassembly: create BMan pools, TX-confirm FQR,
|
||||
* start timer thread, configure EHASH tables.
|
||||
* Must run after start_dpa_app() (needs ipr_info from ioctl).
|
||||
*/
|
||||
if (cdx_init_ip_reassembly()) {
|
||||
printf("cdx: cdx_init_ip_reassembly failed\n");
|
||||
/* Non-fatal — flow offload works without reassembly */
|
||||
}
|
||||
register_cdx_deinit_func(cdx_deinit_ip_reassembly);
|
||||
|
||||
/* IPsec SA management done via FCI/CMM path (dpa_ipsec_freebsd.c) */
|
||||
|
||||
exit:
|
||||
if (rc) {
|
||||
printf("cdx: module initialization FAILED\n");
|
||||
cdx_module_deinit();
|
||||
}
|
||||
return (rc);
|
||||
}
|
||||
|
||||
static int
|
||||
cdx_modevent(module_t mod, int type, void *unused)
|
||||
{
|
||||
switch (type) {
|
||||
case MOD_LOAD:
|
||||
sx_init(&ask_rcu_lock, "ask_rcu");
|
||||
return (cdx_module_init());
|
||||
|
||||
case MOD_UNLOAD:
|
||||
printf("cdx: unloading CDX module\n");
|
||||
cdx_diag_dump_fwd_fqs();
|
||||
cdx_module_deinit();
|
||||
sx_destroy(&ask_rcu_lock);
|
||||
return (0);
|
||||
|
||||
default:
|
||||
return (EOPNOTSUPP);
|
||||
}
|
||||
}
|
||||
|
||||
DEV_MODULE(cdx, cdx_modevent, NULL);
|
||||
MODULE_VERSION(cdx, 1);
|
||||
MODULE_DEPEND(cdx, fci, 1, 1, 1);
|
||||
1421
fastpath/cdx/cdx_mc_freebsd.c
Normal file
1421
fastpath/cdx/cdx_mc_freebsd.c
Normal file
File diff suppressed because it is too large
Load diff
1076
fastpath/cdx/cdx_qos_freebsd.c
Normal file
1076
fastpath/cdx/cdx_qos_freebsd.c
Normal file
File diff suppressed because it is too large
Load diff
450
fastpath/cdx/cdx_reassm_freebsd.c
Normal file
450
fastpath/cdx/cdx_reassm_freebsd.c
Normal file
|
|
@ -0,0 +1,450 @@
|
|||
/*
|
||||
* cdx_reassm_freebsd.c — IP reassembly with EHASH for FreeBSD
|
||||
*
|
||||
* Port of cdx-5.03.1/cdx_reassm.c. Creates BMan buffer pools for
|
||||
* reassembly contexts and fragments, allocates a TX-confirm FQR for
|
||||
* buffer release, runs a timer kthread that ticks the IPR timestamp
|
||||
* in MURAM, and configures the EHASH tables via
|
||||
* ExternalHashSetReasslyPool().
|
||||
*
|
||||
* The Linux version uses dpa_bp_alloc/bman_acquire/bman_release
|
||||
* which are Linux DPAA SDK APIs. On FreeBSD, we use the NCSW
|
||||
* bman_pool_create/bman_pool_fill API and qman_fqr_create for FQs.
|
||||
*
|
||||
* ExternalHashSetReasslyPool and ipr_update_timestamp are implemented
|
||||
* in the kernel fm_ehash_freebsd.c. They are currently stubs until
|
||||
* the kernel EHASH code supports IPR offload (EXCLUDE_FMAN_IPR_OFFLOAD
|
||||
* removed).
|
||||
*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017-2018,2021 NXP
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/malloc.h>
|
||||
#include <sys/kthread.h>
|
||||
#include <sys/proc.h>
|
||||
#include <sys/sched.h>
|
||||
|
||||
#include <vm/uma.h>
|
||||
|
||||
#include "portdefs.h"
|
||||
#include "cdx.h"
|
||||
#include "cdx_ioctl.h"
|
||||
#include "cdx_common.h"
|
||||
#include "misc.h"
|
||||
#include "fm_ehash.h"
|
||||
|
||||
/* FreeBSD DPAA1 APIs */
|
||||
#include <dev/dpaa/bman.h>
|
||||
#include <dev/dpaa/qman.h>
|
||||
|
||||
/* NCSW types for FQR and BM pool */
|
||||
#include <contrib/ncsw/inc/ncsw_ext.h>
|
||||
#include <contrib/ncsw/inc/Peripherals/qm_ext.h>
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
|
||||
|
||||
/* ================================================================
|
||||
* IPR configuration — populated by cdx_dpa_takeover.c
|
||||
* ================================================================ */
|
||||
|
||||
struct cdx_ipr_info ipr_info;
|
||||
|
||||
/* ================================================================
|
||||
* Constants
|
||||
* ================================================================ */
|
||||
|
||||
#define IPR_TIMER_FREQUENCY 10 /* ticks per second */
|
||||
#define IPR_CTX_POOL_BUFS 64 /* reassembly context buffers */
|
||||
#define IPR_FRAG_POOL_BUFS 256 /* fragment buffers */
|
||||
#define IPR_POOL_ALIGN 256 /* DMA alignment for buffers */
|
||||
|
||||
/* ================================================================
|
||||
* BMan pool state
|
||||
* ================================================================ */
|
||||
|
||||
struct ipr_pool {
|
||||
t_Handle pool; /* NCSW BM pool handle */
|
||||
uma_zone_t zone; /* UMA zone for buffer allocation */
|
||||
uint8_t bpid; /* BMan pool ID */
|
||||
uint16_t buf_size; /* buffer size */
|
||||
char zname[32]; /* zone name */
|
||||
};
|
||||
|
||||
static struct ipr_pool ipr_ctx_pool; /* reassembly context pool */
|
||||
static struct ipr_pool ipr_frag_pool; /* fragment pool */
|
||||
|
||||
/* ================================================================
|
||||
* TX-confirm FQR
|
||||
* ================================================================ */
|
||||
|
||||
static t_Handle ipr_txc_fqr; /* TX-confirm FQ range */
|
||||
static uint32_t ipr_txc_fqid; /* base FQID */
|
||||
|
||||
/* ================================================================
|
||||
* Timer kthread
|
||||
* ================================================================ */
|
||||
|
||||
static struct thread *ipr_timer_td;
|
||||
static volatile int ipr_timer_should_stop;
|
||||
|
||||
static uint32_t ipr_txc_count; /* TX-confirm counter */
|
||||
|
||||
/* ================================================================
|
||||
* BMan pool callbacks — allocate/free via UMA zone
|
||||
* ================================================================ */
|
||||
|
||||
static uint8_t *
|
||||
ipr_pool_get_buf(t_Handle h_pool, t_Handle *context)
|
||||
{
|
||||
struct ipr_pool *p;
|
||||
|
||||
p = h_pool;
|
||||
return (uma_zalloc(p->zone, M_NOWAIT));
|
||||
}
|
||||
|
||||
static t_Error
|
||||
ipr_pool_put_buf(t_Handle h_pool, uint8_t *buffer, t_Handle context)
|
||||
{
|
||||
struct ipr_pool *p;
|
||||
|
||||
p = h_pool;
|
||||
uma_zfree(p->zone, buffer);
|
||||
return (E_OK);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Pool creation / destruction
|
||||
* ================================================================ */
|
||||
|
||||
static int
|
||||
ipr_pool_create(struct ipr_pool *p, const char *name, uint16_t buf_size,
|
||||
uint16_t num_bufs)
|
||||
{
|
||||
|
||||
memset(p, 0, sizeof(*p));
|
||||
p->buf_size = buf_size;
|
||||
|
||||
snprintf(p->zname, sizeof(p->zname), "cdx_ipr_%s", name);
|
||||
p->zone = uma_zcreate(p->zname, buf_size, NULL, NULL, NULL, NULL,
|
||||
IPR_POOL_ALIGN - 1, 0);
|
||||
if (p->zone == NULL) {
|
||||
DPA_ERROR("cdx: ipr: failed to create UMA zone '%s'\n",
|
||||
p->zname);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
p->pool = bman_pool_create(&p->bpid, buf_size,
|
||||
0, 0, num_bufs, /* max=0, min=0, alloc=num_bufs */
|
||||
ipr_pool_get_buf, ipr_pool_put_buf,
|
||||
0, 0, 0, 0, /* no depletion thresholds */
|
||||
NULL, /* no depletion callback */
|
||||
p, /* h_BufferPool = our pool struct */
|
||||
NULL, NULL); /* default phys/virt translation */
|
||||
|
||||
if (p->pool == NULL) {
|
||||
DPA_ERROR("cdx: ipr: bman_pool_create failed for '%s'\n",
|
||||
p->zname);
|
||||
uma_zdestroy(p->zone);
|
||||
p->zone = NULL;
|
||||
return (-1);
|
||||
}
|
||||
|
||||
DPA_INFO("cdx: ipr: pool '%s' created — bpid=%u buf_size=%u "
|
||||
"bufs=%u\n", name, p->bpid, buf_size, num_bufs);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void
|
||||
ipr_pool_destroy(struct ipr_pool *p)
|
||||
{
|
||||
|
||||
if (p->pool != NULL) {
|
||||
bman_pool_destroy(p->pool);
|
||||
p->pool = NULL;
|
||||
}
|
||||
/*
|
||||
* Do NOT call uma_zdestroy() here. Buffers allocated from the
|
||||
* UMA zone were released into BMan. After BMan round-trip,
|
||||
* XX_PhysToVirt returns DMAP addresses (0xffffa000...) instead
|
||||
* of the original KVA addresses (0xffff0000...). These DMAP
|
||||
* addresses were never tracked by UMA's slab allocator, so
|
||||
* uma_zdestroy → slab cleanup dereferences corrupted pointers
|
||||
* and panics. Leak the zone; memory is reclaimed on reboot.
|
||||
*/
|
||||
p->zone = NULL;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* TX-confirm FQR — receives frames from FMan after reassembly
|
||||
* buffer release.
|
||||
*
|
||||
* The RX callback logs the frame and releases the buffer back to
|
||||
* the appropriate pool.
|
||||
* ================================================================ */
|
||||
|
||||
static e_RxStoreResponse
|
||||
ipr_txc_rx_callback(t_Handle app __unused, t_Handle fqr __unused,
|
||||
t_Handle portal __unused, uint32_t fqid_off __unused,
|
||||
t_DpaaFD *frame __unused)
|
||||
{
|
||||
|
||||
ipr_txc_count++;
|
||||
/*
|
||||
* Real buffer release will be implemented when
|
||||
* fm_ehash_freebsd.c supports IPR offload.
|
||||
* For now, just count the callback.
|
||||
*/
|
||||
return (e_RX_STORE_RESPONSE_CONTINUE);
|
||||
}
|
||||
|
||||
static int
|
||||
ipr_create_txc_fqr(void)
|
||||
{
|
||||
t_Handle fqr;
|
||||
|
||||
/*
|
||||
* Create a single FQR on pool channel 1 (any portal can dequeue).
|
||||
* Work queue 0, auto-allocated FQID, prefer in cache.
|
||||
*/
|
||||
fqr = qman_fqr_create(1,
|
||||
e_QM_FQ_CHANNEL_POOL1, 0, /* channel, wq */
|
||||
false, 0, /* no forced FQID */
|
||||
false, false, /* not parked, not held active */
|
||||
true, /* prefer in cache */
|
||||
false, NULL, /* no congestion avoidance */
|
||||
0, 0); /* no overhead accounting, no tail drop */
|
||||
|
||||
if (fqr == NULL) {
|
||||
DPA_ERROR("cdx: ipr: failed to create TX-confirm FQR\n");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if (qman_fqr_register_cb(fqr, ipr_txc_rx_callback, NULL) != E_OK) {
|
||||
DPA_ERROR("cdx: ipr: failed to register TX-confirm callback\n");
|
||||
qman_fqr_free(fqr);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
ipr_txc_fqr = fqr;
|
||||
ipr_txc_fqid = qman_fqr_get_base_fqid(fqr);
|
||||
|
||||
DPA_INFO("cdx: ipr: TX-confirm FQR created — fqid=0x%x\n",
|
||||
ipr_txc_fqid);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* IPR timer kthread — ticks the MURAM IPR timestamp at 10 Hz
|
||||
* ================================================================ */
|
||||
|
||||
extern void ipr_update_timestamp(void);
|
||||
|
||||
static void
|
||||
ipr_timer_fn(void *arg __unused)
|
||||
{
|
||||
|
||||
while (!ipr_timer_should_stop) {
|
||||
pause("iprtmr", hz / IPR_TIMER_FREQUENCY);
|
||||
ipr_update_timestamp();
|
||||
}
|
||||
|
||||
printf("cdx: ipr: timer thread exiting\n");
|
||||
kthread_exit();
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Public API — called from cdx_main_freebsd.c init path
|
||||
* ================================================================ */
|
||||
|
||||
int
|
||||
cdx_init_ip_reassembly(void)
|
||||
{
|
||||
uint16_t ctx_bufs, frag_bufs;
|
||||
uint16_t ctx_bsize, frag_bsize;
|
||||
uint32_t txc_fqid_packed;
|
||||
int error;
|
||||
|
||||
/*
|
||||
* Use IPR config from dpa_app if provided, otherwise defaults.
|
||||
*/
|
||||
ctx_bsize = (ipr_info.ipr_ctx_bsize != 0) ?
|
||||
ipr_info.ipr_ctx_bsize : 1600;
|
||||
frag_bsize = (ipr_info.ipr_frag_bsize != 0) ?
|
||||
ipr_info.ipr_frag_bsize : 1600;
|
||||
ctx_bufs = (ipr_info.max_contexts != 0) ?
|
||||
ipr_info.max_contexts : IPR_CTX_POOL_BUFS;
|
||||
frag_bufs = (ipr_info.max_contexts != 0 && ipr_info.max_frags != 0) ?
|
||||
(ipr_info.max_contexts * ipr_info.max_frags) :
|
||||
IPR_FRAG_POOL_BUFS;
|
||||
|
||||
/*
|
||||
* Check if reassembly tables exist in the PCD configuration.
|
||||
* If neither IPv4 nor IPv6 reassembly tables were created by
|
||||
* dpa_app, skip all resource allocation — software reassembly
|
||||
* in the kernel stack handles fragments.
|
||||
*/
|
||||
if (!ExternalHashReasslyTableExists(IPV4_REASSM_TABLE) &&
|
||||
!ExternalHashReasslyTableExists(IPV6_REASSM_TABLE)) {
|
||||
DPA_INFO("cdx: ipr: no reassembly tables configured, "
|
||||
"skipping\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
DPA_INFO("cdx: ipr: init — ctx_bsize=%u frag_bsize=%u "
|
||||
"ctx_bufs=%u frag_bufs=%u\n",
|
||||
ctx_bsize, frag_bsize, ctx_bufs, frag_bufs);
|
||||
|
||||
/*
|
||||
* Step 1: Create BMan pool for reassembly contexts.
|
||||
*/
|
||||
error = ipr_pool_create(&ipr_ctx_pool, "ctx", ctx_bsize, ctx_bufs);
|
||||
if (error != 0)
|
||||
return (-1);
|
||||
|
||||
/*
|
||||
* Step 2: Create BMan pool for fragments.
|
||||
*/
|
||||
error = ipr_pool_create(&ipr_frag_pool, "frag", frag_bsize, frag_bufs);
|
||||
if (error != 0) {
|
||||
ipr_pool_destroy(&ipr_ctx_pool);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Step 3: Create TX-confirm FQR for buffer release.
|
||||
*/
|
||||
error = ipr_create_txc_fqr();
|
||||
if (error != 0) {
|
||||
ipr_pool_destroy(&ipr_frag_pool);
|
||||
ipr_pool_destroy(&ipr_ctx_pool);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Step 4: Start IPR timer kthread.
|
||||
*/
|
||||
ipr_timer_should_stop = 0;
|
||||
error = kthread_add(ipr_timer_fn, NULL, NULL,
|
||||
&ipr_timer_td, 0, 0, "cdx_ipr_timer");
|
||||
if (error != 0) {
|
||||
DPA_ERROR("cdx: ipr: kthread_add failed: %d\n", error);
|
||||
if (ipr_txc_fqr != NULL) {
|
||||
qman_fqr_free(ipr_txc_fqr);
|
||||
ipr_txc_fqr = NULL;
|
||||
}
|
||||
ipr_pool_destroy(&ipr_frag_pool);
|
||||
ipr_pool_destroy(&ipr_ctx_pool);
|
||||
return (-1);
|
||||
}
|
||||
DPA_INFO("cdx: ipr: timer thread started at %d Hz\n",
|
||||
IPR_TIMER_FREQUENCY);
|
||||
|
||||
/*
|
||||
* Step 5: Configure EHASH reassembly tables.
|
||||
*
|
||||
* Pack num_fqs (1) into upper byte of txc_fqid as expected
|
||||
* by ExternalHashSetReasslyPool.
|
||||
*/
|
||||
txc_fqid_packed = ipr_txc_fqid | (1 << 24);
|
||||
|
||||
if (ExternalHashSetReasslyPool(IPV4_REASSM_TABLE,
|
||||
ipr_ctx_pool.bpid, ctx_bsize,
|
||||
ipr_frag_pool.bpid, frag_bsize,
|
||||
txc_fqid_packed, IPR_TIMER_FREQUENCY)) {
|
||||
DPA_INFO("cdx: ipr: IPv4 reassembly table not configured\n");
|
||||
}
|
||||
|
||||
if (ExternalHashSetReasslyPool(IPV6_REASSM_TABLE,
|
||||
ipr_ctx_pool.bpid, ctx_bsize,
|
||||
ipr_frag_pool.bpid, frag_bsize,
|
||||
txc_fqid_packed, IPR_TIMER_FREQUENCY)) {
|
||||
DPA_INFO("cdx: ipr: IPv6 reassembly table not configured\n");
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cdx_deinit_ip_reassembly(void)
|
||||
{
|
||||
|
||||
/* Stop the timer thread */
|
||||
if (ipr_timer_td != NULL) {
|
||||
ipr_timer_should_stop = 1;
|
||||
tsleep(__DEVOLATILE(const void *, &ipr_timer_should_stop),
|
||||
0, "iprstop", hz * 2);
|
||||
ipr_timer_td = NULL;
|
||||
}
|
||||
|
||||
/* Free the TX-confirm FQR */
|
||||
if (ipr_txc_fqr != NULL) {
|
||||
qman_fqr_free(ipr_txc_fqr);
|
||||
ipr_txc_fqr = NULL;
|
||||
}
|
||||
|
||||
/* Destroy buffer pools */
|
||||
ipr_pool_destroy(&ipr_frag_pool);
|
||||
ipr_pool_destroy(&ipr_ctx_pool);
|
||||
|
||||
DPA_INFO("cdx: ipr: deinitialized\n");
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Stats query — called from Tier 1 FCI command handlers
|
||||
* ================================================================ */
|
||||
|
||||
/*
|
||||
* ipr_statistics — response structure for FCI stats query.
|
||||
* Must match what CMM expects (16-bit ack + ip_reassembly_info).
|
||||
*/
|
||||
struct ipr_statistics {
|
||||
uint16_t ackstats;
|
||||
struct ip_reassembly_info info;
|
||||
};
|
||||
|
||||
int
|
||||
cdx_get_ipr_v4_stats(void *resp)
|
||||
{
|
||||
struct ipr_statistics *stats;
|
||||
|
||||
stats = (struct ipr_statistics *)resp;
|
||||
|
||||
/*
|
||||
* get_ip_reassem_info not yet implemented in fm_ehash_freebsd.c.
|
||||
* Return empty stats with pool info we know.
|
||||
*/
|
||||
memset(&stats->info, 0, sizeof(stats->info));
|
||||
stats->info.reassem_bpid = ipr_ctx_pool.bpid;
|
||||
stats->info.reassem_bsize = ipr_ctx_pool.buf_size;
|
||||
stats->info.frag_bpid = ipr_frag_pool.bpid;
|
||||
stats->info.frag_bsize = ipr_frag_pool.buf_size;
|
||||
stats->info.txc_fqid = ipr_txc_fqid;
|
||||
|
||||
return (sizeof(struct ipr_statistics));
|
||||
}
|
||||
|
||||
int
|
||||
cdx_get_ipr_v6_stats(void *resp)
|
||||
{
|
||||
struct ipr_statistics *stats;
|
||||
|
||||
stats = (struct ipr_statistics *)resp;
|
||||
|
||||
memset(&stats->info, 0, sizeof(stats->info));
|
||||
stats->info.reassem_bpid = ipr_ctx_pool.bpid;
|
||||
stats->info.reassem_bsize = ipr_ctx_pool.buf_size;
|
||||
stats->info.frag_bpid = ipr_frag_pool.bpid;
|
||||
stats->info.frag_bsize = ipr_frag_pool.buf_size;
|
||||
stats->info.txc_fqid = ipr_txc_fqid;
|
||||
|
||||
return (sizeof(struct ipr_statistics));
|
||||
}
|
||||
148
fastpath/cdx/cdx_sysctl.c
Normal file
148
fastpath/cdx/cdx_sysctl.c
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
/*
|
||||
* CDX sysctl stats — FreeBSD port
|
||||
*
|
||||
* Replaces procfs.c. Exposes CDX module statistics and FQID information
|
||||
* via sysctl dev.cdx.* instead of /proc/fqid_stats/.
|
||||
*
|
||||
* Per-FQID stats functions (cdx_init_fqid_procfs, cdx_create_dir_in_procfs,
|
||||
* cdx_create_type_fqid_info_in_procfs, cdx_remove_fqid_info_in_procfs)
|
||||
* are no-ops — they would create dynamic sysctl nodes for QMan queue
|
||||
* introspection. This is a monitoring enhancement, not required for
|
||||
* data-plane operation.
|
||||
*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017,2022 NXP
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/module.h>
|
||||
|
||||
#include "portdefs.h"
|
||||
#include "cdx.h"
|
||||
|
||||
/* Forward declarations — exported to other CDX files */
|
||||
void cdx_sysctl_init(void);
|
||||
void cdx_sysctl_fini(void);
|
||||
int cdx_init_fqid_procfs(void);
|
||||
int cdx_create_dir_in_procfs(void **proc_dir_entry, char *name, uint32_t type);
|
||||
void cdx_remove_fqid_info_in_procfs(uint32_t fqid);
|
||||
int cdx_create_type_fqid_info_in_procfs(struct qman_fq *fq, uint32_t type,
|
||||
void *proc_entry, uint8_t *fq_alias_name);
|
||||
|
||||
/* ------------------------------------------------------------------
|
||||
* sysctl tree: dev.cdx.*
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
static struct sysctl_ctx_list cdx_sysctl_ctx;
|
||||
static struct sysctl_oid *cdx_sysctl_tree;
|
||||
|
||||
/* Counters exposed via sysctl */
|
||||
static unsigned long cdx_stat_fci_commands;
|
||||
static unsigned long cdx_stat_timer_ticks;
|
||||
|
||||
/* sysctl handler for active connections — reads the global atomic */
|
||||
static int
|
||||
sysctl_cdx_active_conn(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
unsigned long val;
|
||||
|
||||
val = (unsigned long)atomic_read(&num_active_connections);
|
||||
return (sysctl_handle_long(oidp, &val, 0, req));
|
||||
}
|
||||
|
||||
void
|
||||
cdx_sysctl_init(void)
|
||||
{
|
||||
struct sysctl_oid *stats_node;
|
||||
|
||||
sysctl_ctx_init(&cdx_sysctl_ctx);
|
||||
|
||||
cdx_sysctl_tree = SYSCTL_ADD_NODE(&cdx_sysctl_ctx,
|
||||
SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, "cdx",
|
||||
CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "CDX data-plane module");
|
||||
|
||||
/* dev.cdx.version */
|
||||
SYSCTL_ADD_STRING(&cdx_sysctl_ctx,
|
||||
SYSCTL_CHILDREN(cdx_sysctl_tree), OID_AUTO, "version",
|
||||
CTLFLAG_RD, __DECONST(char *, "5.03.1-fbsd"), 0,
|
||||
"CDX module version");
|
||||
|
||||
/* dev.cdx.stats.* */
|
||||
stats_node = SYSCTL_ADD_NODE(&cdx_sysctl_ctx,
|
||||
SYSCTL_CHILDREN(cdx_sysctl_tree), OID_AUTO, "stats",
|
||||
CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "CDX statistics");
|
||||
|
||||
SYSCTL_ADD_PROC(&cdx_sysctl_ctx,
|
||||
SYSCTL_CHILDREN(stats_node), OID_AUTO, "active_connections",
|
||||
CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE,
|
||||
NULL, 0, sysctl_cdx_active_conn, "LU",
|
||||
"Number of active forwarding connections");
|
||||
|
||||
SYSCTL_ADD_ULONG(&cdx_sysctl_ctx,
|
||||
SYSCTL_CHILDREN(stats_node), OID_AUTO, "fci_commands",
|
||||
CTLFLAG_RD, &cdx_stat_fci_commands,
|
||||
"FCI commands processed");
|
||||
|
||||
SYSCTL_ADD_ULONG(&cdx_sysctl_ctx,
|
||||
SYSCTL_CHILDREN(stats_node), OID_AUTO, "timer_ticks",
|
||||
CTLFLAG_RD, &cdx_stat_timer_ticks,
|
||||
"Timer wheel ticks");
|
||||
}
|
||||
|
||||
void
|
||||
cdx_sysctl_fini(void)
|
||||
{
|
||||
sysctl_ctx_free(&cdx_sysctl_ctx);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------
|
||||
* Per-FQID statistics stubs
|
||||
*
|
||||
* These are called by devman.c / dpa_cfg.c to create per-FQID
|
||||
* /proc entries. On FreeBSD, these would become dynamic sysctl
|
||||
* nodes under dev.cdx.fqid.*. Currently no-ops — per-FQID stats
|
||||
* are a monitoring enhancement that doesn't affect data-plane
|
||||
* operation.
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
int
|
||||
cdx_init_fqid_procfs(void)
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a sub-directory in the FQID stats hierarchy.
|
||||
* type: TX_DIR, RX_DIR, PCD_DIR, SA_DIR
|
||||
*/
|
||||
static int procfs_dummy;
|
||||
|
||||
int
|
||||
cdx_create_dir_in_procfs(void **proc_dir_entry, char *name, uint32_t type)
|
||||
{
|
||||
/* Stub — returns a non-NULL dummy so callers don't error out */
|
||||
*proc_dir_entry = &procfs_dummy;
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Remove per-FQID sysctl node */
|
||||
void
|
||||
cdx_remove_fqid_info_in_procfs(uint32_t fqid)
|
||||
{
|
||||
/* Stub */
|
||||
}
|
||||
|
||||
/* Create per-FQID sysctl node under the appropriate type directory */
|
||||
int
|
||||
cdx_create_type_fqid_info_in_procfs(struct qman_fq *fq, uint32_t type,
|
||||
void *proc_entry, uint8_t *fq_alias_name)
|
||||
{
|
||||
/* Stub */
|
||||
return (0);
|
||||
}
|
||||
218
fastpath/cdx/cdx_timer_freebsd.c
Normal file
218
fastpath/cdx/cdx_timer_freebsd.c
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
/*
|
||||
* CDX timer control code — FreeBSD port
|
||||
*
|
||||
* Replaces cdx_timer.c. Key changes:
|
||||
* - kthread_create → kthread_add (via wrapper)
|
||||
* - kthread_should_stop → volatile flag
|
||||
* - schedule_timeout_uninterruptible → pause(9)
|
||||
* - wake_up_process → thread starts immediately on FreeBSD
|
||||
*
|
||||
* The timer wheel logic itself (inner/outer wheel, __timer_add,
|
||||
* __timer_del) is OS-independent, using hlist_* from compat headers.
|
||||
*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017,2021 NXP
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include "cdx.h"
|
||||
|
||||
u32 x_inner = 0;
|
||||
u32 x_outer = 0;
|
||||
|
||||
/* Thread control */
|
||||
static struct thread *timer_td;
|
||||
static volatile int timer_should_stop;
|
||||
static volatile int timer_exited;
|
||||
|
||||
void
|
||||
cdx_timer_init(TIMER_ENTRY *timer, TIMER_HANDLER handler)
|
||||
{
|
||||
INIT_HLIST_NODE(&timer->node);
|
||||
timer->handler = handler;
|
||||
timer->period = 0;
|
||||
timer->data64 = 0;
|
||||
}
|
||||
|
||||
static inline void
|
||||
__timer_del(TIMER_ENTRY *timer)
|
||||
{
|
||||
hlist_del(&timer->node);
|
||||
timer->period = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
__timer_add(struct _cdx_ctrl *ctrl, TIMER_ENTRY *timer)
|
||||
{
|
||||
u32 x;
|
||||
signed int this_period;
|
||||
|
||||
this_period = (signed int)(timer->timeout - JIFFIES32);
|
||||
if (this_period <= 0)
|
||||
this_period = 1;
|
||||
|
||||
if (this_period <= INNER_WHEEL_MAXTIME) {
|
||||
x = x_inner + (this_period + INNER_WHEEL_PERIOD - 1) /
|
||||
INNER_WHEEL_PERIOD;
|
||||
x &= (INNER_WHEEL_SIZE - 1);
|
||||
hlist_add_head(&timer->node, &ctrl->timer_inner_wheel[x]);
|
||||
} else {
|
||||
if (this_period > OUTER_WHEEL_MAXTIME)
|
||||
this_period = OUTER_WHEEL_MAXTIME;
|
||||
x = x_outer + this_period / OUTER_WHEEL_PERIOD;
|
||||
x &= (OUTER_WHEEL_SIZE - 1);
|
||||
hlist_add_head(&timer->node, &ctrl->timer_outer_wheel[x]);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cdx_timer_del(TIMER_ENTRY *timer)
|
||||
{
|
||||
if (timer->period)
|
||||
__timer_del(timer);
|
||||
}
|
||||
|
||||
void
|
||||
cdx_timer_add(TIMER_ENTRY *timer, cdx_timer_t period)
|
||||
{
|
||||
struct _cdx_ctrl *ctrl = &cdx_info->ctrl;
|
||||
|
||||
if (!timer->handler)
|
||||
return;
|
||||
if (period == 0)
|
||||
cdx_timer_del(timer);
|
||||
else if (timer->period == 0) {
|
||||
timer->period = period;
|
||||
timer->timeout = JIFFIES32 + period;
|
||||
__timer_add(ctrl, timer);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Timer thread function.
|
||||
*
|
||||
* On FreeBSD, kthread_add takes void (*)(void *) so we match that
|
||||
* signature directly (unlike Linux which returns int).
|
||||
*/
|
||||
|
||||
void dpa_update_timestamp(uint32_t ts);
|
||||
|
||||
static void
|
||||
cdx_ctrl_timer_fn(void *data)
|
||||
{
|
||||
struct _cdx_ctrl *ctrl = data;
|
||||
TIMER_ENTRY *timer;
|
||||
struct hlist_node *temp;
|
||||
int outer_wheel_counter = OUTER_WHEEL_COUNTER;
|
||||
|
||||
while (!timer_should_stop) {
|
||||
pause("cdxtmr", INNER_WHEEL_PERIOD);
|
||||
|
||||
mutex_lock(&ctrl->mutex);
|
||||
|
||||
dpa_update_timestamp(JIFFIES32);
|
||||
|
||||
/* Process inner wheel */
|
||||
hlist_for_each_entry_safe(timer, temp,
|
||||
&ctrl->timer_inner_wheel[x_inner], node) {
|
||||
cdx_timer_t old_period = timer->period;
|
||||
__timer_del(timer);
|
||||
|
||||
if (timer->handler(timer)) {
|
||||
if (timer->period == 0)
|
||||
timer->period = old_period;
|
||||
timer->timeout += timer->period;
|
||||
__timer_add(ctrl, timer);
|
||||
}
|
||||
}
|
||||
x_inner = (x_inner + 1) & (INNER_WHEEL_SIZE - 1);
|
||||
|
||||
if (--outer_wheel_counter == 0) {
|
||||
/* Process outer wheel */
|
||||
hlist_for_each_entry_safe(timer, temp,
|
||||
&ctrl->timer_outer_wheel[x_outer], node) {
|
||||
hlist_del(&timer->node);
|
||||
__timer_add(ctrl, timer);
|
||||
}
|
||||
x_outer = (x_outer + 1) & (OUTER_WHEEL_SIZE - 1);
|
||||
outer_wheel_counter = OUTER_WHEEL_COUNTER;
|
||||
}
|
||||
|
||||
mutex_unlock(&ctrl->mutex);
|
||||
}
|
||||
|
||||
printf("cdx: timer thread exiting\n");
|
||||
timer_exited = 1;
|
||||
wakeup(__DEVOLATILE(void *, &timer_exited));
|
||||
kthread_exit();
|
||||
}
|
||||
|
||||
/* Kept for source compat — Linux version returns int */
|
||||
int
|
||||
cdx_ctrl_timer(void *data)
|
||||
{
|
||||
cdx_ctrl_timer_fn(data);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void
|
||||
cdx_ctrl_timer_exit(void)
|
||||
{
|
||||
struct _cdx_ctrl *ctrl;
|
||||
|
||||
ctrl = &cdx_info->ctrl;
|
||||
|
||||
if (timer_td != NULL) {
|
||||
timer_should_stop = 1;
|
||||
while (!timer_exited)
|
||||
tsleep(__DEVOLATILE(void *, &timer_exited),
|
||||
0, "cdxstop", hz);
|
||||
timer_td = NULL;
|
||||
}
|
||||
|
||||
if (ctrl->timer_inner_wheel)
|
||||
kfree(ctrl->timer_inner_wheel);
|
||||
if (ctrl->timer_outer_wheel)
|
||||
kfree(ctrl->timer_outer_wheel);
|
||||
ctrl->timer_inner_wheel = NULL;
|
||||
ctrl->timer_outer_wheel = NULL;
|
||||
}
|
||||
|
||||
int
|
||||
cdx_ctrl_timer_init(struct _cdx_ctrl *ctrl)
|
||||
{
|
||||
int i;
|
||||
int rc = 0;
|
||||
|
||||
timer_should_stop = 0;
|
||||
timer_exited = 0;
|
||||
timer_td = NULL;
|
||||
|
||||
ctrl->timer_inner_wheel = kmalloc(INNER_WHEEL_SIZE *
|
||||
sizeof(struct hlist_head), GFP_KERNEL);
|
||||
ctrl->timer_outer_wheel = kmalloc(OUTER_WHEEL_SIZE *
|
||||
sizeof(struct hlist_head), GFP_KERNEL);
|
||||
if (!ctrl->timer_inner_wheel || !ctrl->timer_outer_wheel) {
|
||||
printf("cdx: timer wheel allocation failed\n");
|
||||
rc = -ENOMEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
for (i = 0; i < INNER_WHEEL_SIZE; i++)
|
||||
INIT_HLIST_HEAD(&ctrl->timer_inner_wheel[i]);
|
||||
for (i = 0; i < OUTER_WHEEL_SIZE; i++)
|
||||
INIT_HLIST_HEAD(&ctrl->timer_outer_wheel[i]);
|
||||
|
||||
rc = kthread_add(cdx_ctrl_timer_fn, ctrl, NULL,
|
||||
&timer_td, 0, 0, "cdx_ctrl_timer");
|
||||
if (rc != 0) {
|
||||
printf("cdx: kthread_add() failed: %d\n", rc);
|
||||
goto error;
|
||||
}
|
||||
|
||||
error:
|
||||
register_cdx_deinit_func(cdx_ctrl_timer_exit);
|
||||
return (rc);
|
||||
}
|
||||
221
fastpath/cdx/cdx_usermodehelper.c
Normal file
221
fastpath/cdx/cdx_usermodehelper.c
Normal file
|
|
@ -0,0 +1,221 @@
|
|||
/*
|
||||
* cdx_usermodehelper.c — Run userspace programs from kernel context
|
||||
*
|
||||
* FreeBSD equivalent of Linux's call_usermodehelper(UMH_WAIT_PROC).
|
||||
* Creates a new process via fork1(), execs the binary via kern_execve(),
|
||||
* and waits synchronously for it to exit via kern_wait().
|
||||
*
|
||||
* Pattern derived from FreeBSD sys/kern/init_main.c (create_init +
|
||||
* start_init), adapted for use from loadable kernel modules.
|
||||
*
|
||||
* Copyright (c) 2026 Mono Technologies Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/proc.h>
|
||||
#include <sys/kthread.h>
|
||||
#include <sys/unistd.h>
|
||||
#include <sys/imgact.h>
|
||||
#include <sys/sched.h>
|
||||
#include <sys/ucred.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/syscallsubr.h>
|
||||
|
||||
/* Prototype (called from cdx_main_freebsd.c) */
|
||||
int cdx_call_usermodehelper(const char *path, char *const argv[],
|
||||
char *const envp[], int *statusp);
|
||||
|
||||
/*
|
||||
* Arguments passed from cdx_call_usermodehelper() to cdx_umh_entry()
|
||||
* via cpu_fork_kthread_handler(). Lives on the caller's stack, which
|
||||
* remains valid because the caller blocks in kern_wait() while the
|
||||
* child reads these fields.
|
||||
*/
|
||||
struct umh_args {
|
||||
const char *path;
|
||||
char *const *argv;
|
||||
char *const *envp;
|
||||
int argc;
|
||||
int envc;
|
||||
};
|
||||
|
||||
/*
|
||||
* Child process entry point. Runs in a new process created by fork1().
|
||||
* Builds exec arguments and calls kern_execve() to transition from
|
||||
* kernel to userspace. On success (EJUSTRETURN), the thread returns
|
||||
* to userspace as the target binary. On failure, the process exits.
|
||||
*/
|
||||
static void
|
||||
cdx_umh_entry(void *arg)
|
||||
{
|
||||
struct umh_args *ua = arg;
|
||||
struct thread *td = curthread;
|
||||
struct proc *p = td->td_proc;
|
||||
struct image_args args;
|
||||
struct vmspace *oldvmspace;
|
||||
int error, i;
|
||||
|
||||
memset(&args, 0, sizeof(args));
|
||||
error = exec_alloc_args(&args);
|
||||
if (error != 0) {
|
||||
printf("cdx: umh: exec_alloc_args: %d\n", error);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
error = exec_args_add_fname(&args, ua->path, UIO_SYSSPACE);
|
||||
if (error != 0) {
|
||||
printf("cdx: umh: exec_args_add_fname: %d\n", error);
|
||||
exec_free_args(&args);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
for (i = 0; i < ua->argc; i++) {
|
||||
error = exec_args_add_arg(&args, ua->argv[i], UIO_SYSSPACE);
|
||||
if (error != 0) {
|
||||
printf("cdx: umh: exec_args_add_arg[%d]: %d\n",
|
||||
i, error);
|
||||
exec_free_args(&args);
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < ua->envc; i++) {
|
||||
error = exec_args_add_env(&args, ua->envp[i], UIO_SYSSPACE);
|
||||
if (error != 0) {
|
||||
printf("cdx: umh: exec_args_add_env[%d]: %d\n",
|
||||
i, error);
|
||||
exec_free_args(&args);
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Transition from kernel to userspace. kern_execve() replaces
|
||||
* this process's address space with the target binary. On
|
||||
* success it returns EJUSTRETURN — the thread resumes in
|
||||
* userspace via fork_trampoline.
|
||||
*/
|
||||
memset(td->td_frame, 0, sizeof(*td->td_frame));
|
||||
oldvmspace = p->p_vmspace;
|
||||
error = kern_execve(td, &args, NULL, oldvmspace);
|
||||
|
||||
if (error == EJUSTRETURN) {
|
||||
exec_cleanup(td, oldvmspace);
|
||||
return; /* returns to userspace */
|
||||
}
|
||||
|
||||
printf("cdx: umh: kern_execve %s: error %d\n", ua->path, error);
|
||||
|
||||
fail:
|
||||
kproc_exit(error != 0 ? error : EIO);
|
||||
/* NOTREACHED */
|
||||
}
|
||||
|
||||
/*
|
||||
* cdx_call_usermodehelper — Run a userspace program synchronously.
|
||||
*
|
||||
* Creates a new process, execs the given binary, and blocks until
|
||||
* it exits. Returns 0 on success (child ran and exited cleanly),
|
||||
* or errno on failure.
|
||||
*
|
||||
* This is the FreeBSD equivalent of Linux's
|
||||
* call_usermodehelper(path, argv, envp, UMH_WAIT_PROC).
|
||||
*
|
||||
* MUST NOT be called from interrupt context or with locks held.
|
||||
*/
|
||||
int
|
||||
cdx_call_usermodehelper(const char *path, char *const argv[],
|
||||
char *const envp[], int *statusp)
|
||||
{
|
||||
struct umh_args ua;
|
||||
struct fork_req fr;
|
||||
struct proc *newproc;
|
||||
struct thread *td2;
|
||||
int error, status, i;
|
||||
pid_t pid;
|
||||
|
||||
/* Count argc and envc */
|
||||
ua.path = path;
|
||||
ua.argv = argv;
|
||||
ua.envp = envp;
|
||||
ua.argc = 0;
|
||||
ua.envc = 0;
|
||||
if (argv != NULL)
|
||||
for (i = 0; argv[i] != NULL; i++)
|
||||
ua.argc++;
|
||||
if (envp != NULL)
|
||||
for (i = 0; envp[i] != NULL; i++)
|
||||
ua.envc++;
|
||||
|
||||
/*
|
||||
* Create a new process via fork1(), matching the create_init()
|
||||
* pattern from init_main.c. RFFDG gives the child its own
|
||||
* file descriptor table. RFSTOPPED creates it stopped so we
|
||||
* can set the entry point before it runs. We omit RFMEM
|
||||
* (unlike kproc_create) because kern_execve will replace the
|
||||
* vmspace entirely.
|
||||
*/
|
||||
newproc = NULL;
|
||||
bzero(&fr, sizeof(fr));
|
||||
fr.fr_flags = RFFDG | RFPROC | RFSTOPPED;
|
||||
fr.fr_procp = &newproc;
|
||||
|
||||
error = fork1(curthread, &fr);
|
||||
if (error != 0) {
|
||||
printf("cdx: umh: fork1 failed: %d\n", error);
|
||||
return (error);
|
||||
}
|
||||
|
||||
pid = newproc->p_pid;
|
||||
|
||||
/*
|
||||
* Set the child's entry point to cdx_umh_entry, which will
|
||||
* call kern_execve() to become the userspace binary.
|
||||
*/
|
||||
td2 = FIRST_THREAD_IN_PROC(newproc);
|
||||
cpu_fork_kthread_handler(td2, cdx_umh_entry, &ua);
|
||||
|
||||
/* Make the child runnable */
|
||||
thread_lock(td2);
|
||||
TD_SET_CAN_RUN(td2);
|
||||
sched_add(td2, SRQ_BORING);
|
||||
|
||||
/*
|
||||
* Wait for the child to exit. This blocks the current thread
|
||||
* (the kldload caller) until dpa_app completes — equivalent
|
||||
* to Linux's UMH_WAIT_PROC.
|
||||
*/
|
||||
error = kern_wait(curthread, pid, &status, 0, NULL);
|
||||
if (error != 0) {
|
||||
printf("cdx: umh: kern_wait (pid %d) failed: %d\n",
|
||||
pid, error);
|
||||
return (error);
|
||||
}
|
||||
|
||||
if (statusp != NULL)
|
||||
*statusp = status;
|
||||
|
||||
if (WIFEXITED(status)) {
|
||||
if (WEXITSTATUS(status) != 0) {
|
||||
printf("cdx: umh: %s exited with status %d\n",
|
||||
path, WEXITSTATUS(status));
|
||||
return (EIO);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (WIFSIGNALED(status)) {
|
||||
printf("cdx: umh: %s killed by signal %d\n",
|
||||
path, WTERMSIG(status));
|
||||
return (EIO);
|
||||
}
|
||||
|
||||
printf("cdx: umh: %s: unexpected wait status 0x%x\n",
|
||||
path, status);
|
||||
return (EIO);
|
||||
}
|
||||
723
fastpath/cdx/dpa_ipsec_freebsd.c
Normal file
723
fastpath/cdx/dpa_ipsec_freebsd.c
Normal file
|
|
@ -0,0 +1,723 @@
|
|||
/*
|
||||
* DPAA IPsec plumbing layer — FreeBSD port.
|
||||
*
|
||||
* Ported from ASK/cdx-5.03.1/dpa_ipsec.c. Provides the DPAA
|
||||
* infrastructure for CDX inline IPsec offload:
|
||||
* - Per-SA frame queue creation/teardown (FQ_TO_SEC, FQ_FROM_SEC, FQ_TO_CP)
|
||||
* - IPsec OH port discovery and channel setup
|
||||
* - Slow-path exception handler for FQ_TO_CP (decrypted miss packets)
|
||||
* - Buffer pool info for CAAM preheader (BPID/BSIZE)
|
||||
* - sec_descriptor allocation (64-byte aligned)
|
||||
*
|
||||
* Uses FreeBSD NCSW QMan API (qman_fqr_create_ctx, qman_fqr_create,
|
||||
* qman_fqr_register_cb, qman_fqr_free) instead of Linux SDK QMan API.
|
||||
*
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017,2021 NXP
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include "portdefs.h"
|
||||
#include "cdx.h"
|
||||
#include "cdx_ioctl.h"
|
||||
#include "misc.h"
|
||||
#include "dpa_ipsec.h"
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/malloc.h>
|
||||
#include <sys/mbuf.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_var.h>
|
||||
#include <net/netisr.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <vm/vm.h>
|
||||
#include <vm/pmap.h>
|
||||
#include <vm/uma.h>
|
||||
|
||||
/* DPAA kernel headers */
|
||||
#include <dev/dpaa/qman.h>
|
||||
#include <dev/dpaa/bman.h>
|
||||
#include <dev/dpaa/dpaa_oh.h>
|
||||
|
||||
/* NCSW types */
|
||||
#include <contrib/ncsw/inc/ncsw_ext.h>
|
||||
#include <contrib/ncsw/inc/integrations/dpaa_integration_ext.h>
|
||||
|
||||
MALLOC_DECLARE(M_CDX);
|
||||
|
||||
#define DPAIPSEC_CELL_INDEX 3 /* OH port cell-index for IPsec */
|
||||
#define IPSEC_WQ_ID 2 /* Work queue for per-SA FQs */
|
||||
|
||||
/* Buffer size for IPsec BMan pool — same as FM_PORT_BUFFER_SIZE (9600+64) */
|
||||
#define IPSEC_BUFFER_SIZE (9600 + 64)
|
||||
|
||||
#define DPAIPSEC_ERROR(fmt, ...) \
|
||||
printf("cdx_ipsec: " fmt, ## __VA_ARGS__)
|
||||
#define DPAIPSEC_INFO(fmt, ...) \
|
||||
printf("cdx_ipsec: " fmt, ## __VA_ARGS__)
|
||||
|
||||
/* ================================================================
|
||||
* Per-SA info — holds sec_descriptor + 3 FQR handles
|
||||
* ================================================================ */
|
||||
|
||||
struct dpa_ipsec_sainfo {
|
||||
void *shdesc_mem; /* raw allocation (for free) */
|
||||
struct sec_descriptor *shared_desc; /* 64-byte aligned */
|
||||
t_Handle fqr[NUM_FQS_PER_SA]; /* NCSW FQR handles */
|
||||
uint32_t fqid[NUM_FQS_PER_SA]; /* resolved FQIDs */
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* IPsec instance singleton — one per CDX module lifetime
|
||||
* ================================================================ */
|
||||
|
||||
struct ipsec_info {
|
||||
uint32_t crypto_channel_id; /* CAAM DCP channel */
|
||||
device_t oh_dev; /* OH port device_t */
|
||||
uint32_t ofport_channel; /* OH port QMan channel */
|
||||
uint32_t ipsec_bpid; /* buffer pool ID for CAAM output */
|
||||
uint32_t ipsec_buf_size; /* buffer size in that pool */
|
||||
t_Handle ipsec_pool; /* BMan pool handle */
|
||||
uma_zone_t ipsec_zone; /* UMA zone for pool buffers */
|
||||
int initialized;
|
||||
};
|
||||
|
||||
static struct ipsec_info ipsecinfo;
|
||||
|
||||
/* Interface list from cdx_devman_freebsd.c / cdx_dpa_stub.c */
|
||||
extern struct dpa_iface_info *dpa_interface_info;
|
||||
|
||||
/* ================================================================
|
||||
* Accessor functions — called by Tier 1 control_ipsec.c
|
||||
* ================================================================ */
|
||||
|
||||
void *
|
||||
dpa_get_ipsec_instance(void)
|
||||
{
|
||||
|
||||
return (&ipsecinfo);
|
||||
}
|
||||
|
||||
struct sec_descriptor *
|
||||
get_shared_desc(void *handle)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sa = (struct dpa_ipsec_sainfo *)handle;
|
||||
|
||||
return (sa->shared_desc);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
get_fqid_to_sec(void *handle)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sa = (struct dpa_ipsec_sainfo *)handle;
|
||||
|
||||
return (sa->fqid[FQ_TO_SEC]);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
get_fqid_from_sec(void *handle)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sa = (struct dpa_ipsec_sainfo *)handle;
|
||||
|
||||
return (sa->fqid[FQ_FROM_SEC]);
|
||||
}
|
||||
|
||||
#ifdef UNIQUE_IPSEC_CP_FQID
|
||||
uint32_t
|
||||
ipsec_get_to_cp_fqid(void *handle)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sa = (struct dpa_ipsec_sainfo *)handle;
|
||||
|
||||
return (sa->fqid[FQ_TO_CP]);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ================================================================
|
||||
* OH port table descriptor — used for classification table lookups
|
||||
* ================================================================ */
|
||||
|
||||
/* From cdx_devoh_freebsd.c */
|
||||
extern void *get_oh_port_td(uint32_t fm_index, uint32_t port_idx,
|
||||
uint32_t type);
|
||||
|
||||
int
|
||||
dpa_ipsec_ofport_td(struct ipsec_info *info __unused, uint32_t table_type,
|
||||
void **td, uint32_t *portid)
|
||||
{
|
||||
struct dpa_iface_info *p;
|
||||
|
||||
if (table_type >= MAX_MATCH_TABLES) {
|
||||
DPAIPSEC_ERROR("invalid table type %u\n", table_type);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Walk interface list to find the IPsec OH port.
|
||||
* Table descriptors are stored in cdx_devoh_freebsd.c's
|
||||
* offline_port_info[][] — use get_oh_port_td() accessor.
|
||||
*/
|
||||
for (p = dpa_interface_info; p != NULL; p = p->next) {
|
||||
if ((p->if_flags & (1 << 8)) == 0) /* IF_TYPE_OFPORT */
|
||||
continue;
|
||||
if (p->oh_info.port_idx != DPAIPSEC_CELL_INDEX)
|
||||
continue;
|
||||
|
||||
*td = get_oh_port_td(p->oh_info.fman_idx,
|
||||
p->oh_info.port_idx, table_type);
|
||||
*portid = p->oh_info.portid;
|
||||
return (0);
|
||||
}
|
||||
|
||||
DPAIPSEC_ERROR("IPsec OH port not found in interface list\n");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Buffer pool info — used by preheader for CAAM output buffers
|
||||
* ================================================================ */
|
||||
|
||||
int
|
||||
cdx_dpa_get_ipsec_pool_info(uint32_t *bpid, uint32_t *buf_size)
|
||||
{
|
||||
|
||||
if (!ipsecinfo.initialized) {
|
||||
DPAIPSEC_ERROR("ipsec not initialized\n");
|
||||
return (-1);
|
||||
}
|
||||
*bpid = ipsecinfo.ipsec_bpid;
|
||||
*buf_size = ipsecinfo.ipsec_buf_size;
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Dedicated IPsec BMan buffer pool.
|
||||
*
|
||||
* CAAM QI acquires output buffers from BMan using the BPID in the
|
||||
* preheader. Using a dedicated pool avoids competing with FMan RX
|
||||
* for buffers from the dtsec pool — the same starvation pattern
|
||||
* that caused the dtsec 256→2048 buffer fix.
|
||||
*/
|
||||
static t_Error
|
||||
ipsec_pool_put_buffer(t_Handle h_BufferPool, uint8_t *buffer,
|
||||
t_Handle context __unused)
|
||||
{
|
||||
|
||||
uma_zfree(ipsecinfo.ipsec_zone, buffer);
|
||||
return (E_OK);
|
||||
}
|
||||
|
||||
static uint8_t *
|
||||
ipsec_pool_get_buffer(t_Handle h_BufferPool, t_Handle *context)
|
||||
{
|
||||
|
||||
return (uma_zalloc(ipsecinfo.ipsec_zone, M_NOWAIT));
|
||||
}
|
||||
|
||||
static void
|
||||
ipsec_pool_depleted(t_Handle h_App, bool in)
|
||||
{
|
||||
|
||||
if (!in)
|
||||
return;
|
||||
|
||||
while (bman_count(ipsecinfo.ipsec_pool) < IPSEC_BUFCOUNT)
|
||||
bman_pool_fill(ipsecinfo.ipsec_pool, IPSEC_BUFCOUNT);
|
||||
}
|
||||
|
||||
static int
|
||||
ipsec_pool_create(struct ipsec_info *info)
|
||||
{
|
||||
uint8_t bpid;
|
||||
|
||||
info->ipsec_buf_size = IPSEC_BUFFER_SIZE;
|
||||
|
||||
info->ipsec_zone = uma_zcreate("cdx_ipsec_bufs", IPSEC_BUFFER_SIZE,
|
||||
NULL, NULL, NULL, NULL, 255 /* 256-byte align for DMA */, 0);
|
||||
if (info->ipsec_zone == NULL) {
|
||||
DPAIPSEC_ERROR("failed to create UMA zone\n");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
info->ipsec_pool = bman_pool_create(&bpid,
|
||||
IPSEC_BUFFER_SIZE,
|
||||
0, 0, /* no software stockpile */
|
||||
IPSEC_BUFCOUNT, /* pre-seed 512 buffers */
|
||||
ipsec_pool_get_buffer,
|
||||
ipsec_pool_put_buffer,
|
||||
IPSEC_BUFCOUNT / 4, /* depletion entry threshold */
|
||||
IPSEC_BUFCOUNT / 2, /* depletion exit threshold */
|
||||
0, 0, /* no HW depletion */
|
||||
ipsec_pool_depleted, info, NULL, NULL);
|
||||
if (info->ipsec_pool == NULL) {
|
||||
DPAIPSEC_ERROR("failed to create BMan pool\n");
|
||||
uma_zdestroy(info->ipsec_zone);
|
||||
info->ipsec_zone = NULL;
|
||||
return (-1);
|
||||
}
|
||||
info->ipsec_bpid = bpid;
|
||||
|
||||
DPAIPSEC_INFO("IPsec pool: bpid %u, buf_size %u, count %u\n",
|
||||
info->ipsec_bpid, info->ipsec_buf_size,
|
||||
bman_count(info->ipsec_pool));
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void
|
||||
ipsec_pool_destroy(struct ipsec_info *info)
|
||||
{
|
||||
|
||||
if (info->ipsec_pool != NULL) {
|
||||
bman_pool_destroy(info->ipsec_pool);
|
||||
info->ipsec_pool = NULL;
|
||||
}
|
||||
if (info->ipsec_zone != NULL) {
|
||||
uma_zdestroy(info->ipsec_zone);
|
||||
info->ipsec_zone = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Slow-path exception handler — FQ_TO_CP callback
|
||||
*
|
||||
* Decrypted packets that don't match any fast-path flow entry
|
||||
* arrive here via the OH port's miss path. Convert the BMan
|
||||
* buffer to an mbuf and deliver to the FreeBSD network stack.
|
||||
* ================================================================ */
|
||||
|
||||
static uint32_t ipsec_exception_pkt_cnt;
|
||||
|
||||
static e_RxStoreResponse
|
||||
ipsec_exception_pkt_handler(t_Handle app __unused, t_Handle qm_fqr __unused,
|
||||
t_Handle qm_portal __unused, uint32_t fqid_offset __unused,
|
||||
t_DpaaFD *fd)
|
||||
{
|
||||
struct mbuf *m;
|
||||
void *frame_va;
|
||||
uint32_t fd_status, fd_length, fd_offset;
|
||||
uint8_t fd_format;
|
||||
|
||||
ipsec_exception_pkt_cnt++;
|
||||
|
||||
/*
|
||||
* Only handle short single-buffer (SBSF) frames for now.
|
||||
* Format is in elion bits [7:5] (NCSW t_DpaaFD).
|
||||
*/
|
||||
fd_format = (fd->elion >> 5) & 0x3;
|
||||
if (fd_format != 0) {
|
||||
DPAIPSEC_ERROR("exception: unsupported FD format %u\n",
|
||||
fd_format);
|
||||
goto release;
|
||||
}
|
||||
|
||||
fd_status = fd->status;
|
||||
/* Short SBSF: length[28:20] = offset (9 bits), length[19:0] = len */
|
||||
fd_length = fd->length & 0xFFFFF;
|
||||
fd_offset = (fd->length >> 20) & 0x1FF;
|
||||
|
||||
/* Check for SEC errors */
|
||||
if (fd_status & 0xFF000000) {
|
||||
DPAIPSEC_ERROR("exception: SEC error status 0x%08x\n",
|
||||
fd_status);
|
||||
goto release;
|
||||
}
|
||||
|
||||
/* Get virtual address from physical */
|
||||
frame_va = (void *)PHYS_TO_DMAP(
|
||||
((uint64_t)fd->addrh << 32) | fd->addrl);
|
||||
if (frame_va == NULL)
|
||||
goto release;
|
||||
|
||||
/*
|
||||
* Build an mbuf wrapping the frame data.
|
||||
* The decrypted frame starts at frame_va + fd_offset.
|
||||
*/
|
||||
m = m_gethdr(M_NOWAIT, MT_DATA);
|
||||
if (m == NULL)
|
||||
goto release;
|
||||
|
||||
/*
|
||||
* Copy frame data into the mbuf. For the exception path (slow
|
||||
* path), a copy is acceptable. The BMan buffer is released
|
||||
* immediately after.
|
||||
*/
|
||||
if (fd_length > MHLEN) {
|
||||
if (m_cljget(m, M_NOWAIT, fd_length) == NULL) {
|
||||
m_freem(m);
|
||||
goto release;
|
||||
}
|
||||
}
|
||||
m_copyback(m, 0, fd_length, (char *)frame_va + fd_offset);
|
||||
m->m_pkthdr.len = fd_length;
|
||||
m->m_len = fd_length;
|
||||
|
||||
/*
|
||||
* Deliver the decrypted packet to the IP input path.
|
||||
* The frame should be a raw IP packet (ESP headers stripped).
|
||||
*/
|
||||
{
|
||||
struct ip *ip;
|
||||
|
||||
if (m->m_len < (int)sizeof(struct ip)) {
|
||||
m_freem(m);
|
||||
goto release;
|
||||
}
|
||||
ip = mtod(m, struct ip *);
|
||||
if (ip->ip_v == IPVERSION) {
|
||||
m->m_pkthdr.rcvif = NULL;
|
||||
netisr_dispatch(NETISR_IP, m);
|
||||
} else if (ip->ip_v == 6) {
|
||||
m->m_pkthdr.rcvif = NULL;
|
||||
netisr_dispatch(NETISR_IPV6, m);
|
||||
} else {
|
||||
DPAIPSEC_ERROR("exception: unknown IP version %u\n",
|
||||
ip->ip_v);
|
||||
m_freem(m);
|
||||
}
|
||||
}
|
||||
|
||||
release:
|
||||
/* Release BMan buffer back to the IPsec pool */
|
||||
if (ipsecinfo.ipsec_pool != NULL) {
|
||||
void *buf_va;
|
||||
|
||||
buf_va = (void *)PHYS_TO_DMAP(
|
||||
((uint64_t)fd->addrh << 32) | fd->addrl);
|
||||
if (buf_va != NULL)
|
||||
bman_put_buffer(ipsecinfo.ipsec_pool, buf_va);
|
||||
}
|
||||
return (e_RX_STORE_RESPONSE_CONTINUE);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Per-SA allocation / teardown
|
||||
* ================================================================ */
|
||||
|
||||
/*
|
||||
* Allocate per-SA resources: sec_descriptor + 3 FQs.
|
||||
*
|
||||
* FQ creation order (to resolve FQID dependencies):
|
||||
* 1. FQ_TO_CP — SW portal channel, DQRR callback
|
||||
* 2. FQ_FROM_SEC — OH port channel, context_b = FQ_TO_CP FQID
|
||||
* 3. FQ_TO_SEC — CAAM DCP channel, context_a = sec_desc phys,
|
||||
* context_b = FQ_FROM_SEC FQID
|
||||
*/
|
||||
void *
|
||||
cdx_dpa_ipsecsa_alloc(struct ipsec_info *info, uint32_t handle __unused)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sainfo;
|
||||
t_QmContextA context_a;
|
||||
t_QmContextB context_b;
|
||||
uint64_t desc_pa;
|
||||
|
||||
if (!info || !ipsecinfo.initialized) {
|
||||
DPAIPSEC_ERROR("ipsec not initialized in sa_alloc\n");
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
sainfo = malloc(sizeof(*sainfo), M_CDX, M_WAITOK | M_ZERO);
|
||||
|
||||
/*
|
||||
* Allocate sec_descriptor with 64-byte alignment.
|
||||
* Raw allocation stored for free; aligned pointer for use.
|
||||
*/
|
||||
sainfo->shdesc_mem = malloc(sizeof(struct sec_descriptor) +
|
||||
PRE_HDR_ALIGN, M_CDX, M_WAITOK | M_ZERO);
|
||||
sainfo->shared_desc = (struct sec_descriptor *)
|
||||
roundup2((uintptr_t)sainfo->shdesc_mem, PRE_HDR_ALIGN);
|
||||
|
||||
/*
|
||||
* Step 1: Create FQ_TO_CP — scheduled to CPU 0's SW portal.
|
||||
* This FQ receives decrypted slow-path packets from the OH port.
|
||||
*/
|
||||
sainfo->fqr[FQ_TO_CP] = qman_fqr_create(1,
|
||||
(e_QmFQChannel)e_QM_FQ_CHANNEL_SWPORTAL0,
|
||||
IPSEC_WQ_ID,
|
||||
FALSE, /* don't force FQID */
|
||||
0, /* alignment */
|
||||
FALSE, /* not parked */
|
||||
TRUE, /* holdActive — keep portal affinity */
|
||||
TRUE, /* preferInCache */
|
||||
FALSE, /* no congestion avoidance */
|
||||
NULL, /* no CG handle */
|
||||
0, /* no overhead accounting */
|
||||
0); /* no tail drop */
|
||||
if (sainfo->fqr[FQ_TO_CP] == NULL) {
|
||||
DPAIPSEC_ERROR("failed to create FQ_TO_CP\n");
|
||||
goto err_cp;
|
||||
}
|
||||
sainfo->fqid[FQ_TO_CP] = qman_fqr_get_base_fqid(sainfo->fqr[FQ_TO_CP]);
|
||||
|
||||
/* Register DQRR callback for exception packets */
|
||||
if (qman_fqr_register_cb(sainfo->fqr[FQ_TO_CP],
|
||||
ipsec_exception_pkt_handler, NULL) != E_OK) {
|
||||
DPAIPSEC_ERROR("failed to register FQ_TO_CP callback\n");
|
||||
goto err_from;
|
||||
}
|
||||
|
||||
/*
|
||||
* Step 2: Create FQ_FROM_SEC — OH port channel.
|
||||
*
|
||||
* Context-A flags:
|
||||
* OVERRIDE_FQ — override destination FQ with context_b FQID
|
||||
* A1_FIELD_VALID — enable A1 field (SEC error check)
|
||||
* A1 value = 2 — check SEC error status
|
||||
*
|
||||
* Context-B = FQ_TO_CP FQID (miss destination)
|
||||
*/
|
||||
memset(&context_a, 0, sizeof(context_a));
|
||||
context_a.res[0] =
|
||||
(((CDX_FQD_CTX_A_OVERRIDE_FQ | CDX_FQD_CTX_A_A1_FIELD_VALID)
|
||||
<< CDX_FQD_CTX_A_SHIFT_BITS) |
|
||||
CDX_FQD_CTX_A_A1_VAL_TO_CHECK_SECERR);
|
||||
context_b = sainfo->fqid[FQ_TO_CP];
|
||||
|
||||
sainfo->fqr[FQ_FROM_SEC] = qman_fqr_create_ctx(1,
|
||||
(e_QmFQChannel)ipsecinfo.ofport_channel,
|
||||
IPSEC_WQ_ID,
|
||||
FALSE, /* don't force FQID */
|
||||
0, /* alignment */
|
||||
TRUE, /* preferInCache */
|
||||
&context_a,
|
||||
&context_b);
|
||||
if (sainfo->fqr[FQ_FROM_SEC] == NULL) {
|
||||
DPAIPSEC_ERROR("failed to create FQ_FROM_SEC\n");
|
||||
goto err_from;
|
||||
}
|
||||
sainfo->fqid[FQ_FROM_SEC] = qman_fqr_get_base_fqid(
|
||||
sainfo->fqr[FQ_FROM_SEC]);
|
||||
|
||||
/*
|
||||
* Step 3: Create FQ_TO_SEC — CAAM DCP channel.
|
||||
*
|
||||
* Context-A = physical address of sec_descriptor (preheader + shdesc).
|
||||
* CAAM QI reads the shared descriptor from this address.
|
||||
*
|
||||
* Context-B = FQ_FROM_SEC FQID (CAAM response destination).
|
||||
*/
|
||||
desc_pa = vtophys(sainfo->shared_desc);
|
||||
memset(&context_a, 0, sizeof(context_a));
|
||||
context_a.res[0] = (uint32_t)(desc_pa >> 32);
|
||||
context_a.res[1] = (uint32_t)(desc_pa);
|
||||
context_b = sainfo->fqid[FQ_FROM_SEC];
|
||||
|
||||
sainfo->fqr[FQ_TO_SEC] = qman_fqr_create_ctx(1,
|
||||
e_QM_FQ_CHANNEL_CAAM,
|
||||
IPSEC_WQ_ID,
|
||||
FALSE, /* don't force FQID */
|
||||
0, /* alignment */
|
||||
TRUE, /* preferInCache */
|
||||
&context_a,
|
||||
&context_b);
|
||||
if (sainfo->fqr[FQ_TO_SEC] == NULL) {
|
||||
DPAIPSEC_ERROR("failed to create FQ_TO_SEC\n");
|
||||
goto err_to;
|
||||
}
|
||||
sainfo->fqid[FQ_TO_SEC] = qman_fqr_get_base_fqid(
|
||||
sainfo->fqr[FQ_TO_SEC]);
|
||||
|
||||
DPAIPSEC_INFO("SA alloc: to_sec=%u from_sec=%u to_cp=%u desc_pa=0x%lx\n",
|
||||
sainfo->fqid[FQ_TO_SEC], sainfo->fqid[FQ_FROM_SEC],
|
||||
sainfo->fqid[FQ_TO_CP], (unsigned long)desc_pa);
|
||||
|
||||
return (sainfo);
|
||||
|
||||
err_to:
|
||||
qman_fqr_free(sainfo->fqr[FQ_FROM_SEC]);
|
||||
err_from:
|
||||
if (sainfo->fqr[FQ_TO_CP] != NULL)
|
||||
qman_fqr_free(sainfo->fqr[FQ_TO_CP]);
|
||||
err_cp:
|
||||
free(sainfo->shdesc_mem, M_CDX);
|
||||
free(sainfo, M_CDX);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Retire a single per-SA FQ.
|
||||
*
|
||||
* On Linux, this calls qman_retire_fq() which transitions the FQ to
|
||||
* "retiring" state asynchronously. On FreeBSD, NCSW qman_fqr_free()
|
||||
* does full retire+OOS synchronously. We just free the FQ here and
|
||||
* NULL it out so cdx_dpa_ipsecsa_release() skips it.
|
||||
*
|
||||
* Returns: 0 = FQ already retired/freed, 1 = still retiring (never on NCSW).
|
||||
*/
|
||||
int
|
||||
cdx_dpa_ipsec_retire_fq(void *handle, int fq_num)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sainfo;
|
||||
|
||||
if (handle == NULL)
|
||||
return (0);
|
||||
|
||||
sainfo = (struct dpa_ipsec_sainfo *)handle;
|
||||
|
||||
if (fq_num < 0 || fq_num >= NUM_FQS_PER_SA)
|
||||
return (0);
|
||||
|
||||
if (sainfo->fqr[fq_num] != NULL) {
|
||||
qman_fqr_free(sainfo->fqr[fq_num]);
|
||||
sainfo->fqr[fq_num] = NULL;
|
||||
}
|
||||
|
||||
return (0); /* synchronous — always immediately retired */
|
||||
}
|
||||
|
||||
/*
|
||||
* Check if a per-SA FQ is in retired state.
|
||||
*
|
||||
* On FreeBSD, qman_fqr_free() is synchronous so the FQ is always
|
||||
* fully retired (and freed) after cdx_dpa_ipsec_retire_fq().
|
||||
*
|
||||
* Returns: 0 = retired (OK to proceed), nonzero = still not retired.
|
||||
*/
|
||||
int
|
||||
cdx_ipsec_sa_fq_check_if_retired_state(void *handle, int fq_num)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sainfo;
|
||||
|
||||
if (handle == NULL)
|
||||
return (0); /* treat NULL as "already retired" */
|
||||
|
||||
sainfo = (struct dpa_ipsec_sainfo *)handle;
|
||||
|
||||
if (fq_num < 0 || fq_num >= NUM_FQS_PER_SA)
|
||||
return (0);
|
||||
|
||||
/* If FQR is NULL, it's already freed (retired). Return 0 = OK. */
|
||||
return (sainfo->fqr[fq_num] != NULL ? 1 : 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Release all per-SA resources: tear down FQs, free descriptor memory.
|
||||
*/
|
||||
int
|
||||
cdx_dpa_ipsecsa_release(void *handle)
|
||||
{
|
||||
struct dpa_ipsec_sainfo *sainfo;
|
||||
int ii;
|
||||
|
||||
if (handle == NULL)
|
||||
return (-1);
|
||||
|
||||
sainfo = (struct dpa_ipsec_sainfo *)handle;
|
||||
|
||||
/* Free FQRs in reverse order */
|
||||
for (ii = NUM_FQS_PER_SA - 1; ii >= 0; ii--) {
|
||||
if (sainfo->fqr[ii] != NULL) {
|
||||
qman_fqr_free(sainfo->fqr[ii]);
|
||||
sainfo->fqr[ii] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
free(sainfo->shdesc_mem, M_CDX);
|
||||
free(sainfo, M_CDX);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* XFRM state stubs
|
||||
*
|
||||
* On Linux, these hold/release a reference to the kernel's xfrm_state
|
||||
* to keep it alive while CDX offloads the SA. On FreeBSD there is no
|
||||
* XFRM framework — CDX manages SA lifecycle independently via FCI
|
||||
* commands from CMM. Return a non-NULL sentinel so the Tier 1 NULL
|
||||
* check in control_ipsec.c passes.
|
||||
* ================================================================ */
|
||||
|
||||
#define CDX_XFRM_SENTINEL ((void *)(uintptr_t)0x1)
|
||||
|
||||
void *
|
||||
cdx_get_xfrm_state_of_sa(void *dev __unused, uint16_t handle __unused)
|
||||
{
|
||||
|
||||
return (CDX_XFRM_SENTINEL);
|
||||
}
|
||||
|
||||
void
|
||||
cdx_dpa_ipsec_xfrm_state_dec_ref_cnt(void *xfrm_state __unused)
|
||||
{
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* SG / skb-free buffer pool stubs
|
||||
*
|
||||
* On Linux, these create dedicated BMan pools for scatter-gather
|
||||
* and skb recycling. On FreeBSD, mbuf allocation handles this
|
||||
* natively. Provide stub implementations that succeed.
|
||||
* ================================================================ */
|
||||
|
||||
int
|
||||
cdx_init_scatter_gather_bpool(void)
|
||||
{
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cdx_init_skb_2bfreed_bpool(void)
|
||||
{
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Module init / exit
|
||||
* ================================================================ */
|
||||
|
||||
int
|
||||
cdx_dpa_ipsec_init(void)
|
||||
{
|
||||
device_t oh_dev;
|
||||
|
||||
DPAIPSEC_INFO("initializing IPsec DPAA plumbing\n");
|
||||
|
||||
memset(&ipsecinfo, 0, sizeof(ipsecinfo));
|
||||
|
||||
/* CAAM DCP channel for QI submissions */
|
||||
ipsecinfo.crypto_channel_id = (uint32_t)e_QM_FQ_CHANNEL_CAAM;
|
||||
|
||||
/* Discover IPsec OH port (cell-index 3) */
|
||||
oh_dev = dpaa_oh_find_port(DPAIPSEC_CELL_INDEX);
|
||||
if (oh_dev == NULL) {
|
||||
DPAIPSEC_ERROR("IPsec OH port (cell-index %d) not found\n",
|
||||
DPAIPSEC_CELL_INDEX);
|
||||
return (-1);
|
||||
}
|
||||
ipsecinfo.oh_dev = oh_dev;
|
||||
ipsecinfo.ofport_channel = dpaa_oh_get_qman_channel(oh_dev);
|
||||
|
||||
DPAIPSEC_INFO("OH port: cell-index %d, channel 0x%x\n",
|
||||
DPAIPSEC_CELL_INDEX, ipsecinfo.ofport_channel);
|
||||
|
||||
/* Create dedicated IPsec BMan buffer pool */
|
||||
if (ipsec_pool_create(&ipsecinfo) != 0) {
|
||||
DPAIPSEC_ERROR("failed to create IPsec buffer pool\n");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
ipsecinfo.initialized = 1;
|
||||
|
||||
DPAIPSEC_INFO("IPsec DPAA plumbing initialized: "
|
||||
"CAAM channel 0x%x, OH channel 0x%x, bpid %u, buf_size %u\n",
|
||||
ipsecinfo.crypto_channel_id, ipsecinfo.ofport_channel,
|
||||
ipsecinfo.ipsec_bpid, ipsecinfo.ipsec_buf_size);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cdx_dpa_ipsec_exit(void)
|
||||
{
|
||||
|
||||
DPAIPSEC_INFO("IPsec DPAA plumbing exiting\n");
|
||||
ipsecinfo.initialized = 0;
|
||||
ipsec_pool_destroy(&ipsecinfo);
|
||||
}
|
||||
41
fastpath/cmm/Makefile
Normal file
41
fastpath/cmm/Makefile
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
# CMM — Connection Management Module for FreeBSD
|
||||
#
|
||||
# Userspace daemon that monitors PF state table and routes,
|
||||
# programs FMan hardware hash tables via FCI/CDX.
|
||||
#
|
||||
# Cross-compile for arm64:
|
||||
# make -f Makefile.cross
|
||||
#
|
||||
# Build natively on the target (simpler):
|
||||
# scp -r opnsense-deps/fastpath/{cmm,fci_lib,fci,include} target:/tmp/
|
||||
# ssh target "cd /tmp/cmm && make && make install"
|
||||
#
|
||||
# Copyright 2026 Mono Technologies Inc.
|
||||
# SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
PROG= cmm
|
||||
NO_MAN= yes
|
||||
|
||||
SRCS= cmm.c \
|
||||
cmm_conn.c \
|
||||
cmm_route.c \
|
||||
cmm_neigh.c \
|
||||
cmm_itf.c \
|
||||
cmm_rtsock.c \
|
||||
cmm_fe.c \
|
||||
cmm_offload.c \
|
||||
cmm_vlan.c \
|
||||
cmm_ipsec.c \
|
||||
cmm_tunnel.c \
|
||||
cmm_ctrl.c
|
||||
|
||||
CFLAGS+= -I${.CURDIR}/../fci_lib
|
||||
CFLAGS+= -I${.CURDIR}/../fci
|
||||
CFLAGS+= -I${.CURDIR}/../include
|
||||
CFLAGS+= -DLS1043
|
||||
CFLAGS+= -Wall -Wextra -Wno-unused-parameter
|
||||
|
||||
# libfci
|
||||
LDADD+= -L${.CURDIR}/../fci_lib -lfci
|
||||
|
||||
.include <bsd.prog.mk>
|
||||
61
fastpath/cmm/Makefile.cross
Normal file
61
fastpath/cmm/Makefile.cross
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
# CMM cross-compilation Makefile for arm64
|
||||
#
|
||||
# Usage:
|
||||
# make -f /build/opnsense-deps/fastpath/cmm/Makefile.cross
|
||||
#
|
||||
# Builds in the current directory (use /tmp/cmm-build or similar).
|
||||
# Requires OPNsense base build first:
|
||||
# sudo make -C /build/opnsense-build base DEVICE=MONO-GATEWAY SETTINGS=26.1 TOOLSDIR=/build/opnsense-build
|
||||
#
|
||||
# Copyright 2026 Mono Technologies Inc.
|
||||
# SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
SYSROOT?= /usr/obj/build/opnsense-src/arm64.aarch64/tmp
|
||||
CC?= cc
|
||||
TARGET?= --target=aarch64-unknown-freebsd14.3
|
||||
|
||||
SRCDIR?= ${MAKEFILE:H}/..
|
||||
CMM_DIR= ${SRCDIR}/cmm
|
||||
FCI_DIR= ${SRCDIR}/fci_lib
|
||||
FCI_HDR= ${SRCDIR}/fci
|
||||
|
||||
CMM_SRCS= cmm.c cmm_conn.c cmm_route.c cmm_neigh.c \
|
||||
cmm_itf.c cmm_rtsock.c cmm_fe.c cmm_offload.c \
|
||||
cmm_vlan.c cmm_ipsec.c cmm_tunnel.c cmm_deny.c \
|
||||
cmm_socket.c cmm_bridge.c cmm_ctrl.c cmm_l2tp.c
|
||||
CMM_OBJS= ${CMM_SRCS:.c=.o}
|
||||
|
||||
PROG= cmm
|
||||
|
||||
CFLAGS= -O2 -pipe -Wall -Wextra -Wno-unused-parameter
|
||||
CFLAGS+= --sysroot=${SYSROOT} ${TARGET}
|
||||
CFLAGS+= -I${FCI_DIR} -I${FCI_HDR} -I${SRCDIR}/include -I${CMM_DIR} \
|
||||
-I${SRCDIR}/auto_bridge -I${SRCDIR}/pf_notify
|
||||
CFLAGS+= -DLS1043
|
||||
|
||||
LDFLAGS= ${TARGET} --sysroot=${SYSROOT}
|
||||
LIBS=
|
||||
|
||||
all: ${PROG}
|
||||
|
||||
# Build libfci from source
|
||||
libfci.o: ${FCI_DIR}/libfci_freebsd.c
|
||||
${CC} ${CFLAGS} -c -o libfci.o ${FCI_DIR}/libfci_freebsd.c
|
||||
|
||||
libfci.a: libfci.o
|
||||
ar rcs libfci.a libfci.o
|
||||
|
||||
# CMM objects — source lives on NFS, objects written locally
|
||||
.for src in ${CMM_SRCS}
|
||||
${src:.c=.o}: ${CMM_DIR}/${src}
|
||||
${CC} ${CFLAGS} -c -o ${.TARGET} ${CMM_DIR}/${src}
|
||||
.endfor
|
||||
|
||||
# Link
|
||||
${PROG}: ${CMM_OBJS} libfci.a
|
||||
${CC} ${LDFLAGS} -o ${.TARGET} ${CMM_OBJS} libfci.a ${LIBS}
|
||||
|
||||
clean:
|
||||
rm -f ${CMM_OBJS} libfci.o libfci.a ${PROG}
|
||||
|
||||
.PHONY: all clean
|
||||
325
fastpath/cmm/PORTING.md
Normal file
325
fastpath/cmm/PORTING.md
Normal file
|
|
@ -0,0 +1,325 @@
|
|||
# CMM FreeBSD Port — Review & Action Items
|
||||
|
||||
Comparative review of the FreeBSD CMM port (`ASK/freebsd/cmm/`, ~4300 LOC)
|
||||
against the original Linux CMM (`ASK/cmm-17.03.1/src/`, ~45000 LOC).
|
||||
|
||||
The port is a clean rewrite (not a line-by-line translation). The
|
||||
architecture — kqueue event loop, push-based PF state events via
|
||||
`pf_notify.ko`, PF_ROUTE/PF_KEY sockets — is sound.
|
||||
|
||||
The FreeBSD port currently implements the **core path** only: conntrack
|
||||
polling, route/neighbor resolution, FPP flow programming, and IPsec SA
|
||||
tracking. The Linux CMM has 20 additional feature modules not yet
|
||||
ported:
|
||||
|
||||
---
|
||||
|
||||
## Missing Feature Modules
|
||||
|
||||
| # | Module | Linux file | LOC | FPP commands | Description | Done |
|
||||
|---|--------|-----------|-----|--------------|-------------|------|
|
||||
| 1 | QoS | `module_qm.c` | 2,788 | 40+ `FPP_CMD_QM_*` | Traffic shaping, scheduling, rate limiting, DSCP mapping | [x] |
|
||||
| 2 | VLAN | `module_vlan.c` | 571 | `FPP_CMD_VLAN_ENTRY`, `_RESET` | 802.1Q VLAN interface add/remove/reset | [x] |
|
||||
| 3 | Tunnels | `module_tunnel.c` | 1,885 | `FPP_CMD_TUNNEL_ADD/DEL/UPDATE` | 6in4, 4in6, GRE tunnel offload | [x] |
|
||||
| 4 | L2 Bridge | `module_rx.c` | 1,746 | `FPP_CMD_RX_L2BRIDGE_*`, `_CNG_*` | MAC learning, ARL, L2 bridge flow offload | [x] |
|
||||
| 5 | Statistics | `module_stat.c` | 1,267 | `FPP_CMD_STAT_*` | Per-connection, bridge, tunnel, VLAN, IPsec counters | [x] |
|
||||
| 6 | Deny rules | `ffcontrol.c` | 2,908 | `FPP_CMD_IPSEC_FRAG_CFG` | Deny-rule filtering, ff_enable/disable, IPsec frag config | [x] |
|
||||
| 7 | CLI daemon | `client_daemon.c` | 1,644 | (dispatcher) | Runtime CLI command interface | [x] |
|
||||
| 8 | Socket accel | `module_socket.c` | 1,886 | `FPP_CMD_IPV4/6_SOCK_*` | TCP socket-level acceleration | [x] |
|
||||
| 9 | RTP/voice | `module_rtp.c`, `voicebuf.c` | 1,810 | `FPP_CMD_RTP_*`, `_VOICE_BUFFER_*` | RTP/RTCP flow monitoring, voice buffers | |
|
||||
| 10 | PPPoE | `pppoe.c`, `module_relay.c` | 960 | `FPP_CMD_PPPOE_ENTRY`, `_RELAY_ENTRY` | PPPoE session management and relay | |
|
||||
| 11 | ICC | `module_icc.c` | 554 | `FPP_CMD_ICC_*` | Ingress congestion control | [x] |
|
||||
| 12 | Profiling | `module_prf.c` | 608 | `FPP_CMD_TRC_*` | FPP debug tracing and CPU profiling | [x] |
|
||||
| 13 | WiFi | `module_wifi.c` | 476 | `FPP_CMD_WIFI_VAP_*` | WiFi VAP management | |
|
||||
| 14 | Pkt capture | `module_pktcap.c` | 420 | `FPP_CMD_PKTCAP_*` | Packet capture / traffic mirroring | [x] |
|
||||
| 15 | NAT-PT | `module_natpt.c` | 364 | `FPP_CMD_NATPT_*` | IPv4-IPv6 translation | [x] |
|
||||
| 16 | L2 bridge ctrl | `ffbridge.c` | 336 | — | Bridge port list / config (stub in Linux) | [x] |
|
||||
| 17 | L2TP | `module_l2tp.c` | 334 | `FPP_CMD_L2TP_ITF_*` | L2TP tunnel interfaces | [x] |
|
||||
| 18 | Exception Q | `module_expt.c` | ~200 | `FPP_CMD_EXPT_QUEUE_*` | DSCP exception queue mapping | |
|
||||
| 19 | TX DSCP map | `module_tx.c` | ~200 | `FPP_CMD_DSCP_VLANPCP_*` | Egress DSCP→VLAN PCP mapping | [x] |
|
||||
| 20 | MACVLAN | `module_macvlan.c` | ~200 | `FPP_CMD_MACVLAN_ENTRY` | Virtual MAC interfaces | [x] |
|
||||
|
||||
### Adding a new command-driven module to cmmctl
|
||||
|
||||
Modules that are pure FCI passthrough (no CMM-side state) follow this
|
||||
pattern. For example, adding Statistics (#5):
|
||||
|
||||
1. **Whitelist the FPP commands** in `cmm/cmm_ctrl.c` — add the new
|
||||
`FPP_CMD_STAT_*` codes to `ctrl_whitelist[]`.
|
||||
|
||||
2. **Create `cmmctl/cmmctl_stat.c`** — implement a
|
||||
`cmmctl_stat_main(int argc, char **argv, int fd)` function that
|
||||
parses arguments, populates the FPP struct, and calls
|
||||
`ctrl_command()`.
|
||||
|
||||
3. **Declare the handler** in `cmmctl/cmmctl.h`:
|
||||
```c
|
||||
int cmmctl_stat_main(int argc, char **argv, int fd);
|
||||
```
|
||||
|
||||
4. **Register the sub-command** in `cmmctl/cmmctl.c`:
|
||||
```c
|
||||
{ "stat", cmmctl_stat_main, "Statistics" },
|
||||
```
|
||||
|
||||
5. **Add the source** to `cmmctl/Makefile.cross` (`CTL_SRCS`).
|
||||
|
||||
6. **Rebuild**: `make -f .../cmmctl/Makefile.cross clean all`. CMM
|
||||
itself only needs rebuilding if the whitelist changed.
|
||||
|
||||
---
|
||||
|
||||
- **Tunnel module (#3)**: CDX has no mode for IP-IP (IPv4-in-IPv4) or
|
||||
GRE-over-IPv4 tunnels. Only gif(4) with IPv4 outer (6in4, mode
|
||||
`TNL_6O4`), gif(4) with IPv6 outer (4in6, mode `TNL_4O6`), and gre(4)
|
||||
with IPv6 outer (`TNL_GRE_IPV6`) are supported. `gre` interfaces with
|
||||
an IPv4 outer endpoint are detected but skipped with a log warning.
|
||||
|
||||
- **Statistics module (#5)**: Pure FCI passthrough — no CMM daemon state.
|
||||
All 17 `FPP_CMD_STAT_*` commands are whitelisted so CDX handles them
|
||||
directly. CLI sub-commands are implemented only for ported features
|
||||
(connection, interface, queue, VLAN, tunnel, IPsec, flow, route).
|
||||
PPPoE and Bridge stats are whitelisted but have no CLI; they become
|
||||
available automatically once those modules are ported — just add the
|
||||
CLI sub-command to `cmmctl_stat.c`.
|
||||
|
||||
- **Extending stats for new modules**: When a new feature module is ported
|
||||
(e.g., L2 Bridge, PPPoE), its stat commands are already whitelisted in
|
||||
`cmm_ctrl.c` and handled by CDX's `control_stat.c`. To expose the new
|
||||
stats in cmmctl: (1) add a `stat_<feature>()` function to
|
||||
`cmmctl_stat.c` following the two-phase STATUS/ENTRY iteration pattern,
|
||||
(2) add the sub-command to the dispatcher in `cmmctl_stat_main()`.
|
||||
No CMM daemon changes needed.
|
||||
|
||||
- **Socket acceleration module (#8)**: Command-driven socket registration
|
||||
for hardware offload of services running on the GDK itself (not
|
||||
forwarded traffic — that's handled by conntrack offload). Supports
|
||||
IPv4/IPv6, TCP/UDP, connected (5-tuple) and unconnected (3-tuple)
|
||||
modes. LANWAN type only; MSP/RTP/L2TP types available once those
|
||||
modules are ported. Route changes trigger automatic socket
|
||||
reprogramming via `cmm_socket_route_update()`. No IPsec integration
|
||||
yet (SA fields zeroed). CLI: `cmmctl socket open|close|update|show`.
|
||||
|
||||
- **L2 Bridge module (#4 + #16)**: Three tightly coupled components.
|
||||
(1) `auto_bridge.ko` — kernel module that hooks `bridge_forward()` via
|
||||
`bridge_l2flow_hook` in `if_bridge.c` to detect L2 flows crossing bridge
|
||||
ports. Maintains a hash table with state machine (CONFIRMED→FF/LINUX/DYING).
|
||||
Communicates with CMM via `/dev/autobridge` character device (kqueue-
|
||||
compatible ring buffer). Also hooks `bridge_rtage()` via
|
||||
`bridge_fdb_can_expire_hook` to prevent FDB expiry for offloaded MACs.
|
||||
Sysctl: `net.autobridge.*` (l3_filtering, timeout_confirmed, timeout_dying,
|
||||
max_entries, count).
|
||||
(2) `cmm_bridge.c` — combines Linux `module_rx.c` (L2 flow offload) and
|
||||
`ffbridge.c` (bridge port resolution). Reads flow events from
|
||||
`/dev/autobridge`, programs CDX via `FPP_CMD_RX_L2FLOW_ENTRY`. Scans
|
||||
bridges at startup via `getifaddrs`/`IFT_BRIDGE`, queries member ports
|
||||
via `BRDGGIFS` ioctl, resolves MAC→physical port via `BRDGRTS` ioctl for
|
||||
L3 routes through bridges. Handles CDX flow timeout (`ACTION_REMOVED`)
|
||||
via FCI event callback.
|
||||
(3) `cmmctl_bridge.c` — CLI: `cmmctl bridge enable|disable|mode|timeout|
|
||||
add|remove|show|reset`. All via FCI passthrough.
|
||||
Simplified from Linux: single hook point in `bridge_forward()` (both
|
||||
src_if and dst_if known), no SEEN state needed, 4-state machine instead
|
||||
of 5.
|
||||
|
||||
- **Deny rules module (#6)**: Three components. (1) FF enable/disable —
|
||||
`FPP_CMD_IPV4_FF_CONTROL` whitelisted, CDX sets global `ff_enable`.
|
||||
(2) IPsec pre-frag — `FPP_CMD_IPSEC_FRAG_CFG` whitelisted, CDX stub
|
||||
(accepted, no HW ops). (3) Deny rules — CMM-side config-file-driven
|
||||
filtering (`/usr/local/etc/cmm_deny.conf`). Rules match on proto, src/dst
|
||||
address with CIDR prefix, src/dst port, and interface name. Checked in
|
||||
`cmm_conn_poll()` before offload. No asymmetric fast-forward (Linux
|
||||
feature, not needed on GDK). To add deny-rule fields: extend
|
||||
`struct cmm_deny_rule` in `cmm_deny.h` and the parser/matcher in
|
||||
`cmm_deny.c`.
|
||||
|
||||
- **Packet capture module (#14)**: Full-stack implementation across CDX,
|
||||
CMM, and cmmctl. (1) `control_pktcap.c` in `cdx-5.03.1/` — CDX
|
||||
command handler that stores per-port capture state (enabled, slice
|
||||
size, BPF filter). Handles ENABLE, IFSTATUS, SLICE, FLF (with
|
||||
fragment reassembly), and QUERY commands. Previously behind
|
||||
`#ifdef CDX_TODO` — moved out and enabled. (2) CMM whitelist — four
|
||||
PKTCAP FPP commands added to `cmm_ctrl.c`. No daemon-side state
|
||||
needed (pure FCI passthrough). (3) `cmmctl_pktcap.c` — CLI with
|
||||
`status`, `slice`, `filter`, and `query` sub-commands. BPF filter
|
||||
compilation via libpcap (`pcap_compile()`), validated with ported
|
||||
`check_bpf_filter()` from Linux `module_pktcap.c`. Supports filter
|
||||
fragmentation (up to 90 BPF instructions across 3 fragments of 30
|
||||
each). Port IDs are numeric CDX port indices (0-7). Note: the CDX
|
||||
handler stores configuration state only — actual data-plane capture
|
||||
(FMan frame replication) is a separate hardware integration task.
|
||||
|
||||
- **Profiling module (#12)**: Pure cmmctl passthrough — no CMM daemon
|
||||
state. All 8 `FPP_CMD_TRC_*` commands (0x0f01–0x0f08) whitelisted in
|
||||
`cmm_ctrl.c`. CLI: `cmmctl prf status|trace|busycpu|dmem`. Trace
|
||||
sub-commands: `start [pmn0 [pmn1]]`, `stop`, `switch`, `show`,
|
||||
`setmask <mask>`. Busy CPU: `start [weight]`, `stop` (displays
|
||||
busy/idle cycle counts and percentage). Memory display: `dmem <addr>
|
||||
[len]` reads CDX memory via `FPP_CMD_TRC_DMEM` in 224-byte chunks.
|
||||
**CDX status**: `FC_TRC` maps to `EVENT_IPS_OUT` in
|
||||
`cdx_cmdhandler.c` but no handler is registered — commands currently
|
||||
return `ERR_UNKNOWN_COMMAND`. The CLI is ready for when CDX trace
|
||||
support is added.
|
||||
|
||||
- **NAT-PT module (#15)**: Pure FCI passthrough — no CMM daemon state.
|
||||
CDX `control_natpt.c` (already compiled and linked) handles OPEN
|
||||
(creates IPv6↔IPv4 CT pair, binds sockets), CLOSE (unbinds, removes
|
||||
CT entries), and QUERY (returns socket IDs, control flags, stats).
|
||||
Three `FPP_CMD_NATPT_*` commands whitelisted in `cmm_ctrl.c`. CLI:
|
||||
`cmmctl natpt open|close|query`. OPEN requires socket_a (IPv6) and
|
||||
socket_b (IPv4) to be pre-registered via `cmmctl socket open`, plus
|
||||
at least one direction flag (6to4, 4to6). QUERY displays control
|
||||
flags and 8 stat counters (v6/v4 received/transmitted/dropped/ACP).
|
||||
Stats are currently zeroed (CDX has `#ifdef CDX_TODO_STATS` stub).
|
||||
The Linux CMM daemon-side route rotation (auto-configuring socket
|
||||
input interfaces) is not implemented — sockets must be configured
|
||||
correctly before opening a NAT-PT pair.
|
||||
|
||||
- **ICC module (#11)**: Pure FCI passthrough — no CMM daemon state.
|
||||
**No CDX handler exists** (`control_icc.c` was never written) — all
|
||||
commands return `ERR_UNKNOWN_COMMAND`, same situation as profiling
|
||||
(#12). Event routing (`FC_ICC` → `EVENT_ICC`) and error codes are
|
||||
defined in CDX headers. Four `FPP_CMD_ICC_*` commands whitelisted in
|
||||
`cmm_ctrl.c`. CLI: `cmmctl icc reset|threshold|add|delete|query`.
|
||||
Supports 9 match types: ethertype, protocol (bitmask), DSCP
|
||||
(bitmask), IPv4/IPv6 source/dest address with CIDR, port ranges
|
||||
(sport/dport/both), and VLAN ID+priority ranges. Query uses
|
||||
iterative QUERY/QUERY_CONT protocol to enumerate all entries per
|
||||
interface (0-2). The CLI is ready for when a CDX ICC handler is
|
||||
implemented.
|
||||
|
||||
- **L2TP module (#17)**: Command-driven L2TP session management — unlike
|
||||
pure passthrough modules, requires CMM daemon state. Each L2TP session
|
||||
ties an interface to a UDP socket with tunnel/session IDs. FreeBSD has
|
||||
no native L2TP kernel driver, so all parameters come from cmmctl.
|
||||
(1) `cmm_l2tp.c` — daemon module. On `CMM_CTRL_CMD_L2TP_ADD`: allocates
|
||||
a UDP socket via `cmm_socket_next_id()`/`cmm_socket_fpp_open()`, resolves
|
||||
route to peer, then registers the L2TP interface in CDX via
|
||||
`FPP_CMD_L2TP_ITF_ADD` (ifname, sock_id, tunnel/session IDs, options).
|
||||
On `CMM_CTRL_CMD_L2TP_DEL`: sends `FPP_CMD_L2TP_ITF_DEL`, tears down
|
||||
the associated socket, clears interface state. Rollback on failure
|
||||
(socket deprogrammed + freed if CDX registration fails).
|
||||
(2) `cmm_socket.c` refactored — internal APIs (`cmm_socket_find`,
|
||||
`cmm_socket_next_id`, `cmm_socket_add`, `cmm_socket_remove`,
|
||||
`cmm_socket_fpp_open`, `cmm_socket_fpp_close`) exposed for cross-module
|
||||
use. Existing ctrl handlers become thin wrappers.
|
||||
(3) Interface state — `ITF_F_L2TP`/`ITF_F_FPP_L2TP` flags and L2TP
|
||||
fields (tunnel/session IDs, sock_id) in `cmm_interface`. Iterator
|
||||
`cmm_itf_foreach_l2tp()` for cleanup.
|
||||
(4) `cmmctl_l2tp.c` — CLI: `cmmctl l2tp add <ifname> <inet|inet6>
|
||||
<local-addr> <peer-addr> <local-port> <peer-port> <local-tun-id>
|
||||
<peer-tun-id> <local-ses-id> <peer-ses-id> [options [dscp [queue]]]`,
|
||||
`cmmctl l2tp del <ifname>`. Positional args, ports converted to network
|
||||
byte order. CDX `FC_L2TP` handler is registered (`CMD_INIT(l2tp)`), so
|
||||
commands should work — unlike TRC which has no handler.
|
||||
|
||||
- **TX DSCP map module (#19)**: Pure FCI passthrough — no CMM daemon state.
|
||||
Configures per-interface egress DSCP-to-VLAN-PCP mapping in CDX hardware.
|
||||
64 DSCP values grouped into 8 by top 3 bits (0-7), each mapped to a VLAN
|
||||
PCP value (0-7). CDX handler EXISTS (`control_tx.c`, `M_tx_cmdproc`) —
|
||||
calls down to `ExternalHashSetDscpVlanpcpMapCfg` in MURAM. Three FPP
|
||||
commands whitelisted in `cmm_ctrl.c` (`#ifdef LS1043`):
|
||||
`FPP_CMD_DSCP_VLANPCP_MAP_STATUS` (0x0506, enable/disable),
|
||||
`FPP_CMD_DSCP_VLANPCP_MAP_CFG` (0x0507, set mapping),
|
||||
`FPP_CMD_QUERY_IFACE_DSCP_VLANPCP_MAP` (0x0508, query). CLI:
|
||||
`cmmctl tx enable|disable <ifname>`, `cmmctl tx map <ifname> <dscp> <pcp>`,
|
||||
`cmmctl tx query <ifname>`. Query displays enabled/disabled status and
|
||||
a table of all 8 DSCP group→VLAN PCP mappings.
|
||||
|
||||
---
|
||||
|
||||
## Architecture: Polling vs Push-Based State Notifications
|
||||
|
||||
The Linux CMM receives per-connection events via netlink conntrack
|
||||
(`NFCT_T_NEW/UPDATE/DESTROY`) — the kernel pushes notifications as
|
||||
connections change.
|
||||
|
||||
### Implementation: `pf_notify.ko`
|
||||
|
||||
The `pf_notify` kernel module (`ASK/freebsd/pf_notify/`) hooks into PF
|
||||
via dedicated `V_pfnotify_*_state_ptr` function pointers (added alongside
|
||||
the existing pfsync hooks in `pfvar.h` / `pf.c`). Events are delivered
|
||||
to userspace via `/dev/pfnotify` — a kqueue-compatible character device
|
||||
with a 4096-entry ring buffer. Follows the same chardev + ring + kqueue
|
||||
pattern as `auto_bridge`.
|
||||
|
||||
**Three event types:**
|
||||
- `PFN_EVENT_INSERT` — new PF state created (from `pf_state_insert`)
|
||||
- `PFN_EVENT_READY` — state became offload-ready: TCP ESTABLISHED or
|
||||
UDP bidirectional (from `pf_test` update hook, one-shot per state)
|
||||
- `PFN_EVENT_DELETE` — PF state removed (from `pf_unlink_state`)
|
||||
|
||||
**One-shot UPDATE tracking:** A module-local `uint64_t[16384]` hash
|
||||
indexed by state ID prevents per-packet READY flooding. The UPDATE hook
|
||||
checks readiness (TCP ESTABLISHED / UDP bidirectional) and the one-shot
|
||||
array before queuing. Collisions cause a harmless re-notification.
|
||||
|
||||
**Hybrid approach:** CMM uses push events as the fast path and keeps a
|
||||
30-second reconciliation poll (unchanged `cmm_conn_poll()`) to catch ring
|
||||
overflow drops or module load races. If `pf_notify.ko` is not loaded,
|
||||
CMM falls back to 100 ms polling automatically.
|
||||
|
||||
**PF kernel changes** (~20 lines across 3 files):
|
||||
- `sys/net/pfvar.h` — 3 new VNET hook typedefs + declarations
|
||||
- `sys/netpfil/pf/pf_ioctl.c` — 3 VNET_DEFINE
|
||||
- `sys/netpfil/pf/pf.c` — hook calls at 5 sites (1 insert, 3 update,
|
||||
1 delete), alongside existing pfsync calls
|
||||
|
||||
### kqueue integration: `d_kqfilter` (critical)
|
||||
|
||||
FreeBSD character devices **must** implement `d_kqfilter` for kqueue to
|
||||
work. `d_poll` alone is not enough — there is no fallback.
|
||||
`prep_cdevsw()` in `kern/kern_conf.c` fills any unset `d_kqfilter` slot
|
||||
with `no_kqfilter` (returns ENODEV). `devfs_kqfilter_f()` calls
|
||||
`dsw->d_kqfilter()` directly with no poll-based fallback path.
|
||||
|
||||
This means if a chardev provides only `d_poll`, `EVFILT_READ`
|
||||
registration will silently fail. When CMM registers all kqueue events
|
||||
in a single `kevent()` call with `nevents=0`, the first registration
|
||||
failure causes `kevent()` to return -1 — all subsequent events in the
|
||||
changelist (including the reconciliation timer) are never registered.
|
||||
CMM then exits immediately with "kevent register: Device not configured".
|
||||
|
||||
**Pattern used** (reference: `sys/dev/hid/hidraw.c`):
|
||||
|
||||
1. `struct filterops` with `f_isfd=1`, `f_detach`, `f_event`,
|
||||
`f_copy=knote_triv_copy`
|
||||
2. `d_kqfilter` function: switch on `kn_filter`, set `kn_fop`, call
|
||||
`knlist_add(&rsel.si_note, kn, 0)`
|
||||
3. `f_event` callback: set `kn_data` to readable bytes, return data
|
||||
available
|
||||
4. `f_detach` callback: `knlist_remove(&rsel.si_note, kn, 0)`
|
||||
5. `KNOTE_LOCKED(&rsel.si_note, 0)` in the event producer (alongside
|
||||
`selwakeup`)
|
||||
6. `knlist_clear` + `knlist_destroy` in module unload
|
||||
|
||||
The `knlist_init_mtx(&rsel.si_note, &mtx)` in module load associates the
|
||||
knlist with the module mutex — all `f_event` callbacks are called with
|
||||
the mutex held.
|
||||
|
||||
**Note:** `auto_bridge.c` has the same missing `d_kqfilter` issue. It is
|
||||
dormant because `auto_bridge.ko` is not loaded during routing tests
|
||||
(`autobridge_fd` stays -1, skipping kqueue registration). Must be fixed
|
||||
before enabling L2 bridge offload.
|
||||
|
||||
### CMM event loop integration
|
||||
|
||||
CMM opens `/dev/pfnotify` at startup (`O_RDONLY | O_NONBLOCK`). The fd
|
||||
is registered in the main kqueue alongside route socket, FCI, PF_KEY,
|
||||
control socket, and auto_bridge fds. A udata tag (`(void *)4`)
|
||||
distinguishes pfnotify events in the dispatch loop.
|
||||
|
||||
The reconciliation timer period is mode-dependent:
|
||||
- **Push mode** (`pfnotify_fd >= 0`): 30 seconds (`CMM_RECONCILE_MS`)
|
||||
- **Poll mode** (fallback): 100 ms (`poll_ms`, configurable via `-p`)
|
||||
|
||||
On `EVFILT_READ` for pfnotify, CMM reads events in a loop and dispatches:
|
||||
- `PFN_EVENT_READY` → attempt offload (same path as poll-discovered
|
||||
ESTABLISHED connections)
|
||||
- `PFN_EVENT_DELETE` → deregister offloaded flow from CDX
|
||||
- `PFN_EVENT_INSERT` → logged at debug level, no immediate action
|
||||
|
||||
**Build:** `make -C .../ASK/freebsd/pf_notify $KBUILD MACHINE=arm64 ...`
|
||||
**Load:** `kldload pf_notify` (after PF, before CMM)
|
||||
**Sysctls:** `net.pfnotify.{ring_size,events_total,events_dropped}`
|
||||
**Verified:** 10 Gb/s sustained throughput with push-mode offload
|
||||
445
fastpath/cmm/cmm.c
Normal file
445
fastpath/cmm/cmm.c
Normal file
|
|
@ -0,0 +1,445 @@
|
|||
/*
|
||||
* cmm.c — Connection Management Module for FreeBSD
|
||||
*
|
||||
* Userspace daemon that monitors PF state table and routes,
|
||||
* then programs FMan hardware hash tables via FCI/CDX for
|
||||
* hardware flow offload on the NXP LS1046A.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/event.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <net/pfvar.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_rtsock.h"
|
||||
#include "cmm_itf.h"
|
||||
#include "cmm_neigh.h"
|
||||
#include "cmm_route.h"
|
||||
#include "cmm_conn.h"
|
||||
#include "cmm_fe.h"
|
||||
#include "cmm_vlan.h"
|
||||
#include "cmm_ipsec.h"
|
||||
#include "cmm_tunnel.h"
|
||||
#include "cmm_l2tp.h"
|
||||
#include "cmm_deny.h"
|
||||
#include "cmm_socket.h"
|
||||
#include "cmm_bridge.h"
|
||||
#include "cmm_ctrl.h"
|
||||
#include "pf_notify.h"
|
||||
#include "libfci.h"
|
||||
|
||||
struct cmm_global cmm_g;
|
||||
|
||||
/*
|
||||
* Top-level FCI async event dispatcher.
|
||||
*
|
||||
* Routes CDX notifications to the appropriate subsystem handler.
|
||||
* Each handler returns FCI_CB_CONTINUE if the event wasn't for it,
|
||||
* allowing the next handler to try.
|
||||
*/
|
||||
static int
|
||||
cmm_fci_catch(unsigned short fcode, unsigned short len,
|
||||
unsigned short *payload)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = cmm_conn_fci_event(fcode, len, payload);
|
||||
if (rc <= FCI_CB_STOP)
|
||||
return (rc);
|
||||
|
||||
rc = cmm_bridge_fci_event(fcode, len, payload);
|
||||
return (rc);
|
||||
}
|
||||
|
||||
static void
|
||||
sigterm_handler(int sig __unused)
|
||||
{
|
||||
cmm_g.running = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
write_pidfile(void)
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
f = fopen(CMM_PID_FILE, "w");
|
||||
if (f != NULL) {
|
||||
fprintf(f, "%d\n", getpid());
|
||||
fclose(f);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"usage: cmm [-f] [-d level] [-p poll_ms] [-D deny_conf]\n"
|
||||
" -f run in foreground\n"
|
||||
" -d level debug level (0=err, 1=warn, 2=info, 3=debug, 4=trace)\n"
|
||||
" -p poll_ms PF state poll interval in ms (default %d)\n"
|
||||
" -D path deny-rule config file (default %s)\n",
|
||||
CMM_DEFAULT_POLL_MS, CMM_DENY_CONF);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
struct cmm_global *g = &cmm_g;
|
||||
struct kevent kev[16];
|
||||
const char *deny_conf = NULL;
|
||||
int ch, nev;
|
||||
|
||||
/* Defaults */
|
||||
g->debug_level = CMM_LOG_INFO;
|
||||
g->poll_ms = CMM_DEFAULT_POLL_MS;
|
||||
g->foreground = 0;
|
||||
g->running = 1;
|
||||
g->pf_fd = -1;
|
||||
g->rtsock_fd = -1;
|
||||
g->rtsock_query_fd = -1;
|
||||
g->kq = -1;
|
||||
g->pfkey_fd = -1;
|
||||
g->ctrl_listen_fd = -1;
|
||||
g->autobridge_fd = -1;
|
||||
g->pfnotify_fd = -1;
|
||||
|
||||
while ((ch = getopt(argc, argv, "fd:p:D:")) != -1) {
|
||||
switch (ch) {
|
||||
case 'f':
|
||||
g->foreground = 1;
|
||||
break;
|
||||
case 'd':
|
||||
g->debug_level = atoi(optarg);
|
||||
if (g->debug_level < 0)
|
||||
g->debug_level = 0;
|
||||
if (g->debug_level > CMM_LOG_TRACE)
|
||||
g->debug_level = CMM_LOG_TRACE;
|
||||
break;
|
||||
case 'p':
|
||||
g->poll_ms = atoi(optarg);
|
||||
if (g->poll_ms < 10)
|
||||
g->poll_ms = 10;
|
||||
break;
|
||||
case 'D':
|
||||
deny_conf = optarg;
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
}
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "CMM starting (debug=%d poll=%dms)",
|
||||
g->debug_level, g->poll_ms);
|
||||
|
||||
/* Daemonize unless -f */
|
||||
if (!g->foreground) {
|
||||
if (daemon(0, 1) < 0) {
|
||||
cmm_print(CMM_LOG_ERR, "daemon: %s", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
write_pidfile();
|
||||
|
||||
/* Install signal handlers */
|
||||
{
|
||||
struct sigaction sa;
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sa_handler = sigterm_handler;
|
||||
sigaction(SIGTERM, &sa, NULL);
|
||||
sigaction(SIGINT, &sa, NULL);
|
||||
|
||||
sa.sa_handler = SIG_IGN;
|
||||
sigaction(SIGPIPE, &sa, NULL);
|
||||
}
|
||||
|
||||
/* Open FCI — retry on boot to handle module/device init race */
|
||||
{
|
||||
int retries;
|
||||
|
||||
for (retries = 0; retries < 10; retries++) {
|
||||
g->fci_handle = fci_open(FCILIB_FF_TYPE, 0);
|
||||
if (g->fci_handle != NULL)
|
||||
break;
|
||||
if (retries == 0)
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"fci_open failed, retrying...");
|
||||
sleep(1);
|
||||
}
|
||||
if (g->fci_handle == NULL) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"fci_open failed after %d attempts "
|
||||
"— is fci.ko loaded?", retries);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
cmm_print(CMM_LOG_INFO, "FCI opened");
|
||||
|
||||
/* Open FCI event handle */
|
||||
g->fci_catch = fci_open(FCILIB_FF_TYPE, NL_FF_GROUP);
|
||||
|
||||
/* Reset CDX tables */
|
||||
cmm_fe_reset(g);
|
||||
|
||||
/* Open /dev/pf */
|
||||
g->pf_fd = open("/dev/pf", O_RDONLY);
|
||||
if (g->pf_fd < 0) {
|
||||
cmm_print(CMM_LOG_ERR, "open /dev/pf: %s — is PF enabled?",
|
||||
strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
cmm_print(CMM_LOG_INFO, "/dev/pf opened");
|
||||
|
||||
/* Warn if IP forwarding is not enabled */
|
||||
{
|
||||
int fwd = 0;
|
||||
size_t fwdlen = sizeof(fwd);
|
||||
if (sysctlbyname("net.inet.ip.forwarding", &fwd, &fwdlen,
|
||||
NULL, 0) == 0 && fwd == 0)
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"net.inet.ip.forwarding=0 — no traffic to offload");
|
||||
}
|
||||
|
||||
/* Open route sockets: one for monitoring, one for RTM_GET queries */
|
||||
g->rtsock_fd = cmm_rtsock_open();
|
||||
if (g->rtsock_fd < 0) {
|
||||
cmm_print(CMM_LOG_ERR, "route socket: %s", strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
g->rtsock_query_fd = cmm_rtsock_open();
|
||||
if (g->rtsock_query_fd < 0) {
|
||||
cmm_print(CMM_LOG_ERR, "route query socket: %s",
|
||||
strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
cmm_print(CMM_LOG_INFO, "route sockets opened");
|
||||
|
||||
/* Initialize subsystems */
|
||||
if (cmm_itf_init() < 0)
|
||||
goto out;
|
||||
cmm_vlan_init(g);
|
||||
if (cmm_neigh_init() < 0)
|
||||
goto out;
|
||||
if (cmm_route_init() < 0)
|
||||
goto out;
|
||||
if (cmm_conn_init() < 0)
|
||||
goto out;
|
||||
|
||||
cmm_tunnel_init(g);
|
||||
cmm_l2tp_init(g);
|
||||
cmm_socket_init();
|
||||
cmm_bridge_init(g);
|
||||
|
||||
if (cmm_ipsec_init() < 0)
|
||||
goto out;
|
||||
|
||||
/* Load deny rules (optional — missing file means offload everything) */
|
||||
if (cmm_deny_init(deny_conf) < 0)
|
||||
goto out;
|
||||
|
||||
/* Open PF_KEY socket for IPsec SA events */
|
||||
g->pfkey_fd = cmm_pfkey_open();
|
||||
if (g->pfkey_fd < 0)
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"PF_KEY unavailable — IPsec offload disabled");
|
||||
|
||||
/* Open /dev/pfnotify for push-based state events */
|
||||
{
|
||||
int fd;
|
||||
|
||||
fd = open(PFN_DEV_PATH, O_RDONLY | O_NONBLOCK);
|
||||
if (fd >= 0) {
|
||||
g->pfnotify_fd = fd;
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"pfnotify: push mode (fd=%d)", fd);
|
||||
} else {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"pfnotify: %s unavailable (%s) — polling mode",
|
||||
PFN_DEV_PATH, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
/* Register FCI event dispatcher for CDX async notifications */
|
||||
if (g->fci_catch != NULL)
|
||||
fci_register_cb(g->fci_catch, cmm_fci_catch);
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "all subsystems initialized");
|
||||
|
||||
/* Create kqueue */
|
||||
g->kq = kqueue();
|
||||
if (g->kq < 0) {
|
||||
cmm_print(CMM_LOG_ERR, "kqueue: %s", strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Register events */
|
||||
nev = 0;
|
||||
|
||||
/* Route socket: interface/route/neighbor changes */
|
||||
EV_SET(&kev[nev], g->rtsock_fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
|
||||
nev++;
|
||||
|
||||
/* FCI event fd (if available) */
|
||||
if (g->fci_catch != NULL) {
|
||||
int fci_event_fd = fci_fd(g->fci_catch);
|
||||
if (fci_event_fd >= 0) {
|
||||
EV_SET(&kev[nev], fci_event_fd, EVFILT_READ,
|
||||
EV_ADD, 0, 0, NULL);
|
||||
nev++;
|
||||
}
|
||||
}
|
||||
|
||||
/* PF_KEY socket: IPsec SA events */
|
||||
if (g->pfkey_fd >= 0) {
|
||||
EV_SET(&kev[nev], g->pfkey_fd, EVFILT_READ,
|
||||
EV_ADD, 0, 0, NULL);
|
||||
nev++;
|
||||
}
|
||||
|
||||
/* Control socket: external tool commands (cmmctl) */
|
||||
if (cmm_ctrl_init(g) == 0) {
|
||||
EV_SET(&kev[nev], g->ctrl_listen_fd, EVFILT_READ,
|
||||
EV_ADD, 0, 0, (void *)(uintptr_t)1);
|
||||
nev++;
|
||||
}
|
||||
|
||||
/* auto_bridge device: L2 flow events */
|
||||
if (g->autobridge_fd >= 0) {
|
||||
EV_SET(&kev[nev], g->autobridge_fd, EVFILT_READ,
|
||||
EV_ADD, 0, 0, (void *)(uintptr_t)3);
|
||||
nev++;
|
||||
}
|
||||
|
||||
/* pf_notify device: PF state change events */
|
||||
if (g->pfnotify_fd >= 0) {
|
||||
EV_SET(&kev[nev], g->pfnotify_fd, EVFILT_READ,
|
||||
EV_ADD, 0, 0, (void *)(uintptr_t)4);
|
||||
nev++;
|
||||
}
|
||||
|
||||
/*
|
||||
* PF state polling timer.
|
||||
* In push mode: 30s reconciliation to catch ring overflow drops.
|
||||
* In poll mode: fast polling (default 100ms) as primary path.
|
||||
*/
|
||||
{
|
||||
int timer_ms;
|
||||
|
||||
timer_ms = (g->pfnotify_fd >= 0) ?
|
||||
CMM_RECONCILE_MS : g->poll_ms;
|
||||
EV_SET(&kev[nev], 1, EVFILT_TIMER, EV_ADD,
|
||||
NOTE_MSECONDS, timer_ms, NULL);
|
||||
nev++;
|
||||
}
|
||||
|
||||
if (kevent(g->kq, kev, nev, NULL, 0, NULL) < 0) {
|
||||
cmm_print(CMM_LOG_ERR, "kevent register: %s",
|
||||
strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "event loop started");
|
||||
|
||||
/* Main event loop */
|
||||
while (g->running) {
|
||||
struct kevent events[16];
|
||||
int n, i;
|
||||
|
||||
n = kevent(g->kq, NULL, 0, events, 16, NULL);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
cmm_print(CMM_LOG_ERR, "kevent: %s", strerror(errno));
|
||||
break;
|
||||
}
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
if (events[i].filter == EVFILT_TIMER) {
|
||||
cmm_conn_poll(g);
|
||||
} else if (events[i].udata ==
|
||||
(void *)(uintptr_t)1) {
|
||||
/* Control socket: new connection */
|
||||
cmm_ctrl_accept(g);
|
||||
} else if (events[i].udata ==
|
||||
(void *)(uintptr_t)2) {
|
||||
/* Control socket: client command */
|
||||
cmm_ctrl_dispatch(g,
|
||||
(int)events[i].ident);
|
||||
} else if ((int)events[i].ident ==
|
||||
g->rtsock_fd) {
|
||||
cmm_rtsock_dispatch(g);
|
||||
} else if (g->pfkey_fd >= 0 &&
|
||||
(int)events[i].ident == g->pfkey_fd) {
|
||||
cmm_pfkey_dispatch(g);
|
||||
} else if (events[i].udata ==
|
||||
(void *)(uintptr_t)3) {
|
||||
/* auto_bridge: L2 flow event */
|
||||
cmm_bridge_event(g);
|
||||
} else if (events[i].udata ==
|
||||
(void *)(uintptr_t)4) {
|
||||
/* pf_notify: state change event */
|
||||
cmm_conn_event(g);
|
||||
} else if (g->fci_catch != NULL &&
|
||||
(int)events[i].ident ==
|
||||
fci_fd(g->fci_catch)) {
|
||||
fci_drain(g->fci_catch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "shutting down");
|
||||
|
||||
out:
|
||||
/* Clean shutdown */
|
||||
cmm_ctrl_fini(g);
|
||||
cmm_sa_flush_all(g);
|
||||
cmm_conn_deregister_all(g);
|
||||
cmm_bridge_fini(g);
|
||||
cmm_socket_fini(g);
|
||||
cmm_l2tp_fini(g);
|
||||
cmm_tunnel_fini(g);
|
||||
cmm_vlan_fini(g);
|
||||
cmm_fe_reset(g);
|
||||
|
||||
cmm_deny_fini();
|
||||
cmm_ipsec_fini(g);
|
||||
cmm_conn_fini(g);
|
||||
cmm_route_fini();
|
||||
cmm_neigh_fini();
|
||||
cmm_itf_fini();
|
||||
|
||||
if (g->kq >= 0)
|
||||
close(g->kq);
|
||||
if (g->pfkey_fd >= 0)
|
||||
close(g->pfkey_fd);
|
||||
if (g->rtsock_fd >= 0)
|
||||
close(g->rtsock_fd);
|
||||
if (g->rtsock_query_fd >= 0)
|
||||
close(g->rtsock_query_fd);
|
||||
if (g->pfnotify_fd >= 0)
|
||||
close(g->pfnotify_fd);
|
||||
if (g->pf_fd >= 0)
|
||||
close(g->pf_fd);
|
||||
if (g->fci_catch != NULL)
|
||||
fci_close(g->fci_catch);
|
||||
if (g->fci_handle != NULL)
|
||||
fci_close(g->fci_handle);
|
||||
|
||||
unlink(CMM_PID_FILE);
|
||||
cmm_print(CMM_LOG_INFO, "exited");
|
||||
|
||||
return (0);
|
||||
}
|
||||
90
fastpath/cmm/cmm.h
Normal file
90
fastpath/cmm/cmm.h
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* cmm.h — CMM global state and shared definitions
|
||||
*
|
||||
* Connection Management Module for FreeBSD.
|
||||
* Monitors PF state table and routes, programs FMan hash tables
|
||||
* via FCI/CDX for hardware flow offload.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_H
|
||||
#define CMM_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
#include <net/ethernet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#include "libfci.h"
|
||||
#include "fpp.h"
|
||||
#include "fpp_private.h"
|
||||
#include "list.h"
|
||||
#include "jhash.h"
|
||||
|
||||
/*
|
||||
* Debug levels (set via -d flag).
|
||||
* 0 = errors only, 1 = warnings, 2 = info, 3 = debug, 4 = trace
|
||||
*/
|
||||
#define CMM_LOG_ERR 0
|
||||
#define CMM_LOG_WARN 1
|
||||
#define CMM_LOG_INFO 2
|
||||
#define CMM_LOG_DEBUG 3
|
||||
#define CMM_LOG_TRACE 4
|
||||
|
||||
#define CMM_DEFAULT_POLL_MS 1000 /* PF state poll interval */
|
||||
#define CMM_RECONCILE_MS 30000 /* reconciliation interval (push mode) */
|
||||
#define CMM_PID_FILE "/var/run/cmm.pid"
|
||||
|
||||
struct cmm_global {
|
||||
/* FCI handles */
|
||||
FCI_CLIENT *fci_handle; /* synchronous command handle */
|
||||
FCI_CLIENT *fci_catch; /* async event handle */
|
||||
|
||||
/* OS file descriptors */
|
||||
int pf_fd; /* /dev/pf for state queries */
|
||||
int rtsock_fd; /* PF_ROUTE socket (monitoring) */
|
||||
int rtsock_query_fd;/* PF_ROUTE socket (RTM_GET queries) */
|
||||
int kq; /* kqueue fd */
|
||||
int pfkey_fd; /* PF_KEY socket */
|
||||
int ctrl_listen_fd; /* control socket (listen) */
|
||||
int ctrl_clients[4];/* control socket clients */
|
||||
int autobridge_fd; /* /dev/autobridge (L2 bridge) */
|
||||
int pfnotify_fd; /* /dev/pfnotify (state events) */
|
||||
|
||||
/* Configuration */
|
||||
int debug_level;
|
||||
int poll_ms; /* PF state poll interval */
|
||||
int foreground; /* don't daemonize */
|
||||
|
||||
/* Runtime */
|
||||
volatile int running;
|
||||
|
||||
/* Route ID counter */
|
||||
uint32_t next_route_id;
|
||||
|
||||
/* PF state epoch (incremented each poll) */
|
||||
uint32_t epoch;
|
||||
|
||||
/* Single-threaded: all processing in kqueue event loop */
|
||||
};
|
||||
|
||||
extern struct cmm_global cmm_g;
|
||||
|
||||
/* Logging */
|
||||
#define cmm_print(level, fmt, ...) do { \
|
||||
if ((level) <= cmm_g.debug_level) { \
|
||||
const char *_pfx[] = { "ERR", "WARN", "INFO", \
|
||||
"DBG", "TRC" }; \
|
||||
fprintf(stderr, "cmm[%s]: " fmt "\n", \
|
||||
_pfx[(level)], ##__VA_ARGS__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#endif /* CMM_H */
|
||||
19
fastpath/cmm/cmm.rc
Normal file
19
fastpath/cmm/cmm.rc
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
# PROVIDE: cmm
|
||||
# REQUIRE: NETWORKING pf
|
||||
# KEYWORD: shutdown
|
||||
#
|
||||
# Add the following to /etc/rc.conf to enable CMM:
|
||||
# cmm_enable="YES"
|
||||
#
|
||||
|
||||
. /etc/rc.subr
|
||||
|
||||
name="cmm"
|
||||
rcvar="cmm_enable"
|
||||
command="/usr/local/sbin/cmm"
|
||||
pidfile="/var/run/${name}.pid"
|
||||
|
||||
load_rc_config $name
|
||||
run_rc_command "$1"
|
||||
995
fastpath/cmm/cmm_bridge.c
Normal file
995
fastpath/cmm/cmm_bridge.c
Normal file
|
|
@ -0,0 +1,995 @@
|
|||
/*
|
||||
* cmm_bridge.c — L2 bridge offload module
|
||||
*
|
||||
* Three logical sections:
|
||||
* 1. Bridge detection and port enumeration (FreeBSD ioctls)
|
||||
* 2. L2 flow offload (auto_bridge consumer → CDX programming)
|
||||
* 3. CDX timeout callback (hardware flow expiry)
|
||||
*
|
||||
* Combines the functionality of Linux module_rx.c (L2 flow offload)
|
||||
* and ffbridge.c (bridge port resolution).
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/event.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <net/if_types.h>
|
||||
#include <net/if_bridgevar.h>
|
||||
#include <net/ethernet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_bridge.h"
|
||||
#include "cmm_itf.h"
|
||||
#include "libfci.h"
|
||||
|
||||
/* L2 flow hash table */
|
||||
static struct list_head l2flow_table[CMM_L2FLOW_HASH_SIZE];
|
||||
|
||||
/* Detected bridges */
|
||||
static struct list_head bridge_list;
|
||||
|
||||
/* auto_bridge device fd */
|
||||
static int autobridge_fd = -1;
|
||||
|
||||
/* Default flow timeout in CDX (seconds) */
|
||||
#define CMM_L2FLOW_TIMEOUT_DEFAULT 30
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Section 1: Bridge detection and port enumeration */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/*
|
||||
* Query bridge member ports via SIOCGDRVSPEC / BRDGGIFS ioctl.
|
||||
* Populates br->ports list.
|
||||
*/
|
||||
static int
|
||||
bridge_get_ports(struct cmm_bridge *br, int sd)
|
||||
{
|
||||
struct ifdrv ifd;
|
||||
struct ifbifconf bif;
|
||||
struct ifbreq *breqs;
|
||||
uint32_t len;
|
||||
int i, count;
|
||||
|
||||
/* First call with len=0 to get required buffer size */
|
||||
memset(&ifd, 0, sizeof(ifd));
|
||||
strlcpy(ifd.ifd_name, br->ifname, sizeof(ifd.ifd_name));
|
||||
ifd.ifd_cmd = BRDGGIFS;
|
||||
ifd.ifd_len = sizeof(bif);
|
||||
ifd.ifd_data = &bif;
|
||||
|
||||
memset(&bif, 0, sizeof(bif));
|
||||
bif.ifbic_len = 0;
|
||||
bif.ifbic_buf = NULL;
|
||||
|
||||
if (ioctl(sd, SIOCGDRVSPEC, &ifd) < 0) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"bridge: BRDGGIFS size query failed for %s: %s",
|
||||
br->ifname, strerror(errno));
|
||||
return (-1);
|
||||
}
|
||||
|
||||
len = bif.ifbic_len;
|
||||
if (len == 0)
|
||||
return (0); /* no member ports */
|
||||
|
||||
breqs = calloc(1, len);
|
||||
if (breqs == NULL)
|
||||
return (-1);
|
||||
|
||||
bif.ifbic_len = len;
|
||||
bif.ifbic_req = breqs;
|
||||
|
||||
if (ioctl(sd, SIOCGDRVSPEC, &ifd) < 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: BRDGGIFS failed for %s: %s",
|
||||
br->ifname, strerror(errno));
|
||||
free(breqs);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
count = bif.ifbic_len / sizeof(struct ifbreq);
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
struct cmm_bridge_port *port;
|
||||
int idx;
|
||||
|
||||
idx = if_nametoindex(breqs[i].ifbr_ifsname);
|
||||
if (idx == 0)
|
||||
continue;
|
||||
|
||||
port = calloc(1, sizeof(*port));
|
||||
if (port == NULL)
|
||||
continue;
|
||||
|
||||
strlcpy(port->ifname, breqs[i].ifbr_ifsname,
|
||||
sizeof(port->ifname));
|
||||
port->ifindex = idx;
|
||||
list_add(&br->ports, &port->entry);
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"bridge: %s member port: %s (ifindex %d)",
|
||||
br->ifname, port->ifname, port->ifindex);
|
||||
}
|
||||
|
||||
free(breqs);
|
||||
return (count);
|
||||
}
|
||||
|
||||
/*
|
||||
* Free all port entries from a bridge.
|
||||
*/
|
||||
static void
|
||||
bridge_free_ports(struct cmm_bridge *br)
|
||||
{
|
||||
struct list_head *pos, *tmp;
|
||||
|
||||
for (pos = list_first(&br->ports); pos != &br->ports;) {
|
||||
tmp = list_next(pos);
|
||||
list_del(pos);
|
||||
free(container_of(pos, struct cmm_bridge_port, entry));
|
||||
pos = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Free all bridges and their ports.
|
||||
*/
|
||||
static void
|
||||
bridge_free_all(void)
|
||||
{
|
||||
struct list_head *pos, *tmp;
|
||||
|
||||
for (pos = list_first(&bridge_list); pos != &bridge_list;) {
|
||||
tmp = list_next(pos);
|
||||
struct cmm_bridge *br = container_of(pos,
|
||||
struct cmm_bridge, entry);
|
||||
bridge_free_ports(br);
|
||||
list_del(pos);
|
||||
free(br);
|
||||
pos = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Find a bridge record by ifindex.
|
||||
*/
|
||||
static struct cmm_bridge *
|
||||
bridge_find(int ifindex)
|
||||
{
|
||||
struct list_head *pos;
|
||||
|
||||
for (pos = list_first(&bridge_list); pos != &bridge_list;
|
||||
pos = list_next(pos)) {
|
||||
struct cmm_bridge *br = container_of(pos,
|
||||
struct cmm_bridge, entry);
|
||||
if (br->ifindex == ifindex)
|
||||
return (br);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Scan system for bridge interfaces using getifaddrs.
|
||||
* Detect bridges by IFT_BRIDGE type in the link-layer address.
|
||||
*/
|
||||
static void
|
||||
bridge_scan(int sd)
|
||||
{
|
||||
struct ifaddrs *ifap, *ifa;
|
||||
|
||||
/* Clear old state */
|
||||
bridge_free_all();
|
||||
|
||||
if (getifaddrs(&ifap) < 0) {
|
||||
cmm_print(CMM_LOG_ERR, "bridge: getifaddrs: %s",
|
||||
strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
|
||||
struct sockaddr_dl *sdl;
|
||||
struct cmm_bridge *br;
|
||||
int idx;
|
||||
|
||||
if (ifa->ifa_addr == NULL ||
|
||||
ifa->ifa_addr->sa_family != AF_LINK)
|
||||
continue;
|
||||
|
||||
sdl = (struct sockaddr_dl *)ifa->ifa_addr;
|
||||
if (sdl->sdl_type != IFT_BRIDGE)
|
||||
continue;
|
||||
|
||||
idx = if_nametoindex(ifa->ifa_name);
|
||||
if (idx == 0)
|
||||
continue;
|
||||
|
||||
/* Already seen (multiple addrs per interface) */
|
||||
if (bridge_find(idx) != NULL)
|
||||
continue;
|
||||
|
||||
br = calloc(1, sizeof(*br));
|
||||
if (br == NULL)
|
||||
continue;
|
||||
|
||||
strlcpy(br->ifname, ifa->ifa_name, sizeof(br->ifname));
|
||||
br->ifindex = idx;
|
||||
if (sdl->sdl_alen == ETHER_ADDR_LEN)
|
||||
memcpy(br->macaddr, LLADDR(sdl), ETHER_ADDR_LEN);
|
||||
list_head_init(&br->ports);
|
||||
list_add(&bridge_list, &br->entry);
|
||||
|
||||
bridge_get_ports(br, sd);
|
||||
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"bridge: detected %s (ifindex %d, "
|
||||
"mac %02x:%02x:%02x:%02x:%02x:%02x)",
|
||||
br->ifname, br->ifindex,
|
||||
br->macaddr[0], br->macaddr[1], br->macaddr[2],
|
||||
br->macaddr[3], br->macaddr[4], br->macaddr[5]);
|
||||
}
|
||||
|
||||
freeifaddrs(ifap);
|
||||
}
|
||||
|
||||
/*
|
||||
* Query bridge FDB to resolve a destination MAC to a physical
|
||||
* member port name. Used by cmm_fe_route_register() when the
|
||||
* output interface is a bridge.
|
||||
*/
|
||||
int
|
||||
cmm_bridge_resolve_port(int bridge_ifindex, const uint8_t *dst_mac,
|
||||
char *out_ifname, size_t namelen)
|
||||
{
|
||||
struct ifdrv ifd;
|
||||
struct ifbaconf bac;
|
||||
struct ifbareq *bareqs;
|
||||
char brname[IFNAMSIZ];
|
||||
uint32_t len;
|
||||
int sd, found, i, count;
|
||||
|
||||
if (if_indextoname(bridge_ifindex, brname) == NULL)
|
||||
return (-1);
|
||||
|
||||
sd = socket(AF_LOCAL, SOCK_DGRAM, 0);
|
||||
if (sd < 0)
|
||||
return (-1);
|
||||
|
||||
/* Query FDB size */
|
||||
memset(&ifd, 0, sizeof(ifd));
|
||||
strlcpy(ifd.ifd_name, brname, sizeof(ifd.ifd_name));
|
||||
ifd.ifd_cmd = BRDGRTS;
|
||||
ifd.ifd_len = sizeof(bac);
|
||||
ifd.ifd_data = &bac;
|
||||
|
||||
memset(&bac, 0, sizeof(bac));
|
||||
bac.ifbac_len = 0;
|
||||
bac.ifbac_buf = NULL;
|
||||
|
||||
if (ioctl(sd, SIOCGDRVSPEC, &ifd) < 0) {
|
||||
close(sd);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
len = bac.ifbac_len;
|
||||
if (len == 0) {
|
||||
close(sd);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
bareqs = calloc(1, len);
|
||||
if (bareqs == NULL) {
|
||||
close(sd);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
bac.ifbac_len = len;
|
||||
bac.ifbac_req = bareqs;
|
||||
|
||||
if (ioctl(sd, SIOCGDRVSPEC, &ifd) < 0) {
|
||||
free(bareqs);
|
||||
close(sd);
|
||||
return (-1);
|
||||
}
|
||||
close(sd);
|
||||
|
||||
count = bac.ifbac_len / sizeof(struct ifbareq);
|
||||
found = 0;
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
if (memcmp(bareqs[i].ifba_dst, dst_mac,
|
||||
ETHER_ADDR_LEN) == 0) {
|
||||
strlcpy(out_ifname, bareqs[i].ifba_ifsname, namelen);
|
||||
found = 1;
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"bridge: resolved MAC "
|
||||
"%02x:%02x:%02x:%02x:%02x:%02x → %s "
|
||||
"on %s",
|
||||
dst_mac[0], dst_mac[1], dst_mac[2],
|
||||
dst_mac[3], dst_mac[4], dst_mac[5],
|
||||
out_ifname, brname);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free(bareqs);
|
||||
return (found ? 0 : -1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Rescan bridges after interface change events.
|
||||
* Re-enumerate bridges and send BRIDGED_ITF_UPDATE for each member port.
|
||||
*/
|
||||
void
|
||||
cmm_bridge_itf_update(struct cmm_global *g)
|
||||
{
|
||||
struct list_head *bpos, *ppos;
|
||||
fpp_bridged_itf_cmd_t cmd;
|
||||
int sd;
|
||||
|
||||
sd = socket(AF_LOCAL, SOCK_DGRAM, 0);
|
||||
if (sd < 0)
|
||||
return;
|
||||
|
||||
bridge_scan(sd);
|
||||
close(sd);
|
||||
|
||||
/* Notify CDX about bridged interfaces */
|
||||
for (bpos = list_first(&bridge_list); bpos != &bridge_list;
|
||||
bpos = list_next(bpos)) {
|
||||
struct cmm_bridge *br = container_of(bpos,
|
||||
struct cmm_bridge, entry);
|
||||
|
||||
for (ppos = list_first(&br->ports); ppos != &br->ports;
|
||||
ppos = list_next(ppos)) {
|
||||
struct cmm_bridge_port *port = container_of(ppos,
|
||||
struct cmm_bridge_port, entry);
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
strlcpy(cmd.ifname, port->ifname,
|
||||
sizeof(cmd.ifname));
|
||||
cmd.is_bridged = 1;
|
||||
memcpy(cmd.br_macaddr, br->macaddr,
|
||||
ETHER_ADDR_LEN);
|
||||
|
||||
if (fci_write(g->fci_handle,
|
||||
FPP_CMD_BRIDGED_ITF_UPDATE,
|
||||
sizeof(cmd), (unsigned short *)&cmd) != 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: BRIDGED_ITF_UPDATE "
|
||||
"failed for %s", port->ifname);
|
||||
} else {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"bridge: BRIDGED_ITF_UPDATE "
|
||||
"sent for %s (bridge %s)",
|
||||
port->ifname, br->ifname);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Section 2: L2 flow offload (auto_bridge consumer) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static inline unsigned int
|
||||
l2flow_hash(const struct abm_l2flow *flow)
|
||||
{
|
||||
return (jhash(flow, sizeof(*flow), 0x12345678) &
|
||||
(CMM_L2FLOW_HASH_SIZE - 1));
|
||||
}
|
||||
|
||||
static struct cmm_l2flow *
|
||||
l2flow_find(const struct abm_l2flow *flow)
|
||||
{
|
||||
struct list_head *bucket, *pos;
|
||||
unsigned int h;
|
||||
|
||||
h = l2flow_hash(flow);
|
||||
bucket = &l2flow_table[h];
|
||||
|
||||
for (pos = list_first(bucket); pos != bucket;
|
||||
pos = list_next(pos)) {
|
||||
struct cmm_l2flow *entry = container_of(pos,
|
||||
struct cmm_l2flow, hash_entry);
|
||||
if (memcmp(&entry->flow, flow, sizeof(*flow)) == 0)
|
||||
return (entry);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static struct cmm_l2flow *
|
||||
l2flow_add(const struct abm_l2flow *flow)
|
||||
{
|
||||
struct cmm_l2flow *entry;
|
||||
unsigned int h;
|
||||
|
||||
entry = calloc(1, sizeof(*entry));
|
||||
if (entry == NULL)
|
||||
return (NULL);
|
||||
|
||||
memcpy(&entry->flow, flow, sizeof(*flow));
|
||||
h = l2flow_hash(flow);
|
||||
list_add(&l2flow_table[h], &entry->hash_entry);
|
||||
|
||||
return (entry);
|
||||
}
|
||||
|
||||
static void
|
||||
l2flow_del(struct cmm_l2flow *entry)
|
||||
{
|
||||
list_del(&entry->hash_entry);
|
||||
free(entry);
|
||||
}
|
||||
|
||||
/*
|
||||
* Deregister a single L2 flow from CDX.
|
||||
*/
|
||||
static int
|
||||
l2flow_fpp_deregister(struct cmm_global *g, struct cmm_l2flow *entry)
|
||||
{
|
||||
fpp_l2_bridge_flow_entry_cmd_t cmd;
|
||||
int rc;
|
||||
|
||||
if (!entry->fpp_programmed)
|
||||
return (0);
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.action = FPP_ACTION_DEREGISTER;
|
||||
memcpy(cmd.srcaddr, entry->flow.saddr, ETHER_ADDR_LEN);
|
||||
memcpy(cmd.destaddr, entry->flow.daddr, ETHER_ADDR_LEN);
|
||||
cmd.ethertype = entry->flow.ethertype;
|
||||
cmd.svlan_tag = entry->flow.svlan_tag;
|
||||
cmd.cvlan_tag = entry->flow.cvlan_tag;
|
||||
cmd.session_id = entry->flow.session_id;
|
||||
strlcpy(cmd.input_name, entry->input_name,
|
||||
sizeof(cmd.input_name));
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_RX_L2FLOW_ENTRY,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: L2FLOW deregister failed: %d", rc);
|
||||
}
|
||||
|
||||
entry->fpp_programmed = 0;
|
||||
return (rc == 0 ? 0 : -1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Register or update a L2 flow in CDX.
|
||||
*/
|
||||
static int
|
||||
l2flow_fpp_register(struct cmm_global *g, struct cmm_l2flow *entry,
|
||||
int update)
|
||||
{
|
||||
fpp_l2_bridge_flow_entry_cmd_t cmd;
|
||||
int rc;
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
|
||||
if (update && entry->fpp_programmed)
|
||||
cmd.action = FPP_ACTION_UPDATE;
|
||||
else
|
||||
cmd.action = FPP_ACTION_REGISTER;
|
||||
|
||||
cmd.ethertype = entry->flow.ethertype;
|
||||
memcpy(cmd.destaddr, entry->flow.daddr, ETHER_ADDR_LEN);
|
||||
memcpy(cmd.srcaddr, entry->flow.saddr, ETHER_ADDR_LEN);
|
||||
cmd.svlan_tag = entry->flow.svlan_tag;
|
||||
cmd.cvlan_tag = entry->flow.cvlan_tag;
|
||||
cmd.session_id = entry->flow.session_id;
|
||||
cmd.mark = entry->mark;
|
||||
|
||||
/* L3/L4 optional fields */
|
||||
cmd.proto = entry->flow.proto;
|
||||
memcpy(cmd.saddr, entry->flow.sip, sizeof(cmd.saddr));
|
||||
memcpy(cmd.daddr, entry->flow.dip, sizeof(cmd.daddr));
|
||||
cmd.sport = entry->flow.sport;
|
||||
cmd.dport = entry->flow.dport;
|
||||
|
||||
strlcpy(cmd.input_name, entry->input_name,
|
||||
sizeof(cmd.input_name));
|
||||
strlcpy(cmd.output_name, entry->output_name,
|
||||
sizeof(cmd.output_name));
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_RX_L2FLOW_ENTRY,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
|
||||
if (rc == 0 || rc == FPP_ERR_BRIDGE_ENTRY_ALREADY_EXISTS) {
|
||||
entry->fpp_programmed = 1;
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"bridge: L2FLOW %s "
|
||||
"%02x:%02x:%02x:%02x:%02x:%02x -> "
|
||||
"%02x:%02x:%02x:%02x:%02x:%02x "
|
||||
"etype=0x%04x in=%s out=%s",
|
||||
cmd.action == FPP_ACTION_REGISTER ?
|
||||
"registered" : "updated",
|
||||
entry->flow.saddr[0], entry->flow.saddr[1],
|
||||
entry->flow.saddr[2], entry->flow.saddr[3],
|
||||
entry->flow.saddr[4], entry->flow.saddr[5],
|
||||
entry->flow.daddr[0], entry->flow.daddr[1],
|
||||
entry->flow.daddr[2], entry->flow.daddr[3],
|
||||
entry->flow.daddr[4], entry->flow.daddr[5],
|
||||
ntohs(entry->flow.ethertype),
|
||||
entry->input_name, entry->output_name);
|
||||
return (0);
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: L2FLOW register failed: %d", rc);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Send a response back to auto_bridge.ko via /dev/autobridge.
|
||||
*/
|
||||
static void
|
||||
autobridge_respond(const struct abm_l2flow *flow, uint32_t flags)
|
||||
{
|
||||
struct abm_response resp;
|
||||
|
||||
if (autobridge_fd < 0)
|
||||
return;
|
||||
|
||||
memset(&resp, 0, sizeof(resp));
|
||||
memcpy(&resp.flow, flow, sizeof(resp.flow));
|
||||
resp.flags = flags;
|
||||
|
||||
if (write(autobridge_fd, &resp, sizeof(resp)) !=
|
||||
(ssize_t)sizeof(resp)) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: write to autobridge failed: %s",
|
||||
strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Flush all L2 flows — deregister from CDX and free entries.
|
||||
*/
|
||||
static void
|
||||
l2flow_flush_all(struct cmm_global *g)
|
||||
{
|
||||
struct list_head *pos, *tmp;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CMM_L2FLOW_HASH_SIZE; i++) {
|
||||
for (pos = list_first(&l2flow_table[i]);
|
||||
pos != &l2flow_table[i];) {
|
||||
tmp = list_next(pos);
|
||||
struct cmm_l2flow *entry = container_of(pos,
|
||||
struct cmm_l2flow, hash_entry);
|
||||
l2flow_fpp_deregister(g, entry);
|
||||
l2flow_del(entry);
|
||||
pos = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle a FLOW_NEW event from auto_bridge.
|
||||
*/
|
||||
static void
|
||||
handle_flow_new(struct cmm_global *g, const struct abm_event *ev)
|
||||
{
|
||||
struct cmm_l2flow *entry;
|
||||
struct cmm_interface *iif, *oif;
|
||||
|
||||
/* Resolve interface indexes to names */
|
||||
iif = cmm_itf_find_by_index(ev->iif_index);
|
||||
oif = cmm_itf_find_by_index(ev->oif_index);
|
||||
|
||||
if (iif == NULL || oif == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: flow_new unknown iif=%u or oif=%u",
|
||||
ev->iif_index, ev->oif_index);
|
||||
autobridge_respond(&ev->flow,
|
||||
ABM_FLAG_DENIED | ABM_FLAG_ACK);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Check for existing entry */
|
||||
entry = l2flow_find(&ev->flow);
|
||||
if (entry != NULL) {
|
||||
/* Update if interfaces changed */
|
||||
if (strcmp(entry->input_name, iif->ifname) != 0 ||
|
||||
strcmp(entry->output_name, oif->ifname) != 0) {
|
||||
strlcpy(entry->input_name, iif->ifname,
|
||||
sizeof(entry->input_name));
|
||||
strlcpy(entry->output_name, oif->ifname,
|
||||
sizeof(entry->output_name));
|
||||
entry->mark = ev->mark;
|
||||
l2flow_fpp_register(g, entry, 1);
|
||||
}
|
||||
autobridge_respond(&ev->flow,
|
||||
ABM_FLAG_OFFLOADED | ABM_FLAG_ACK);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Create new entry */
|
||||
entry = l2flow_add(&ev->flow);
|
||||
if (entry == NULL) {
|
||||
autobridge_respond(&ev->flow,
|
||||
ABM_FLAG_DENIED | ABM_FLAG_ACK);
|
||||
return;
|
||||
}
|
||||
|
||||
strlcpy(entry->input_name, iif->ifname,
|
||||
sizeof(entry->input_name));
|
||||
strlcpy(entry->output_name, oif->ifname,
|
||||
sizeof(entry->output_name));
|
||||
entry->mark = ev->mark;
|
||||
|
||||
/* Program CDX */
|
||||
if (l2flow_fpp_register(g, entry, 0) == 0) {
|
||||
autobridge_respond(&ev->flow,
|
||||
ABM_FLAG_OFFLOADED | ABM_FLAG_ACK);
|
||||
} else {
|
||||
autobridge_respond(&ev->flow,
|
||||
ABM_FLAG_DENIED | ABM_FLAG_ACK);
|
||||
l2flow_del(entry);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle a FLOW_UPDATE event from auto_bridge.
|
||||
*/
|
||||
static void
|
||||
handle_flow_update(struct cmm_global *g, const struct abm_event *ev)
|
||||
{
|
||||
struct cmm_l2flow *entry;
|
||||
struct cmm_interface *iif, *oif;
|
||||
|
||||
entry = l2flow_find(&ev->flow);
|
||||
if (entry == NULL) {
|
||||
/* Treat as new */
|
||||
handle_flow_new(g, ev);
|
||||
return;
|
||||
}
|
||||
|
||||
iif = cmm_itf_find_by_index(ev->iif_index);
|
||||
oif = cmm_itf_find_by_index(ev->oif_index);
|
||||
|
||||
if (iif == NULL || oif == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: flow_update unknown interfaces");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Update interfaces if changed */
|
||||
if (strcmp(entry->input_name, iif->ifname) != 0 ||
|
||||
strcmp(entry->output_name, oif->ifname) != 0 ||
|
||||
entry->mark != ev->mark) {
|
||||
strlcpy(entry->input_name, iif->ifname,
|
||||
sizeof(entry->input_name));
|
||||
strlcpy(entry->output_name, oif->ifname,
|
||||
sizeof(entry->output_name));
|
||||
entry->mark = ev->mark;
|
||||
l2flow_fpp_register(g, entry, 1);
|
||||
}
|
||||
|
||||
autobridge_respond(&ev->flow,
|
||||
ABM_FLAG_OFFLOADED | ABM_FLAG_ACK);
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle a FLOW_DEL event from auto_bridge.
|
||||
*/
|
||||
static void
|
||||
handle_flow_del(struct cmm_global *g, const struct abm_event *ev)
|
||||
{
|
||||
struct cmm_l2flow *entry;
|
||||
|
||||
entry = l2flow_find(&ev->flow);
|
||||
if (entry == NULL) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"bridge: flow_del for unknown flow");
|
||||
autobridge_respond(&ev->flow, ABM_FLAG_ACK);
|
||||
return;
|
||||
}
|
||||
|
||||
l2flow_fpp_deregister(g, entry);
|
||||
l2flow_del(entry);
|
||||
|
||||
autobridge_respond(&ev->flow, ABM_FLAG_ACK);
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"bridge: flow deleted "
|
||||
"%02x:%02x:%02x:%02x:%02x:%02x -> "
|
||||
"%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
ev->flow.saddr[0], ev->flow.saddr[1],
|
||||
ev->flow.saddr[2], ev->flow.saddr[3],
|
||||
ev->flow.saddr[4], ev->flow.saddr[5],
|
||||
ev->flow.daddr[0], ev->flow.daddr[1],
|
||||
ev->flow.daddr[2], ev->flow.daddr[3],
|
||||
ev->flow.daddr[4], ev->flow.daddr[5]);
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle a RESET event from auto_bridge.
|
||||
*/
|
||||
static void
|
||||
handle_reset(struct cmm_global *g)
|
||||
{
|
||||
int rc;
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "bridge: reset — flushing all flows");
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_RX_L2BRIDGE_FLOW_RESET,
|
||||
0, NULL);
|
||||
if (rc != 0)
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: FLOW_RESET failed: %d", rc);
|
||||
|
||||
l2flow_flush_all(g);
|
||||
}
|
||||
|
||||
/*
|
||||
* Read and dispatch events from /dev/autobridge.
|
||||
* Called from the kqueue event loop when the fd is readable.
|
||||
*/
|
||||
void
|
||||
cmm_bridge_event(struct cmm_global *g)
|
||||
{
|
||||
struct abm_event ev;
|
||||
ssize_t n;
|
||||
|
||||
for (;;) {
|
||||
n = read(autobridge_fd, &ev, sizeof(ev));
|
||||
if (n != (ssize_t)sizeof(ev)) {
|
||||
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
|
||||
break;
|
||||
if (n == 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: /dev/autobridge closed");
|
||||
break;
|
||||
}
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: short read from autobridge: %zd", n);
|
||||
break;
|
||||
}
|
||||
|
||||
switch (ev.type) {
|
||||
case ABM_EVENT_FLOW_NEW:
|
||||
handle_flow_new(g, &ev);
|
||||
break;
|
||||
case ABM_EVENT_FLOW_UPDATE:
|
||||
handle_flow_update(g, &ev);
|
||||
break;
|
||||
case ABM_EVENT_FLOW_DEL:
|
||||
handle_flow_del(g, &ev);
|
||||
break;
|
||||
case ABM_EVENT_RESET:
|
||||
handle_reset(g);
|
||||
break;
|
||||
default:
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: unknown event type %u", ev.type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Section 3: CDX timeout callback */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/*
|
||||
* FCI event handler for FPP_CMD_RX_L2FLOW_ENTRY.
|
||||
* When CDX sends ACTION_REMOVED (flow timed out in hardware),
|
||||
* remove from our hash table and notify auto_bridge.
|
||||
*
|
||||
* Called from the top-level FCI event dispatcher in cmm.c.
|
||||
* Returns FCI_CB_CONTINUE for unrecognized events.
|
||||
*/
|
||||
int
|
||||
cmm_bridge_fci_event(unsigned short fcode, unsigned short len,
|
||||
unsigned short *payload)
|
||||
{
|
||||
fpp_l2_bridge_flow_entry_cmd_t *cmd;
|
||||
struct abm_l2flow flow;
|
||||
struct cmm_l2flow *entry;
|
||||
|
||||
if (fcode != FPP_CMD_RX_L2FLOW_ENTRY)
|
||||
return (FCI_CB_CONTINUE);
|
||||
|
||||
if (len < sizeof(*cmd))
|
||||
return (FCI_CB_CONTINUE);
|
||||
|
||||
cmd = (fpp_l2_bridge_flow_entry_cmd_t *)payload;
|
||||
|
||||
if (cmd->action != FPP_ACTION_REMOVED)
|
||||
return (FCI_CB_CONTINUE);
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"bridge: CDX removed flow "
|
||||
"%02x:%02x:%02x:%02x:%02x:%02x -> "
|
||||
"%02x:%02x:%02x:%02x:%02x:%02x (timeout)",
|
||||
cmd->srcaddr[0], cmd->srcaddr[1],
|
||||
cmd->srcaddr[2], cmd->srcaddr[3],
|
||||
cmd->srcaddr[4], cmd->srcaddr[5],
|
||||
cmd->destaddr[0], cmd->destaddr[1],
|
||||
cmd->destaddr[2], cmd->destaddr[3],
|
||||
cmd->destaddr[4], cmd->destaddr[5]);
|
||||
|
||||
/*
|
||||
* Build the flow key to look up in our hash table.
|
||||
* The CDX command contains the same fields we need.
|
||||
*/
|
||||
memset(&flow, 0, sizeof(flow));
|
||||
memcpy(flow.saddr, cmd->srcaddr, ETHER_ADDR_LEN);
|
||||
memcpy(flow.daddr, cmd->destaddr, ETHER_ADDR_LEN);
|
||||
flow.ethertype = cmd->ethertype;
|
||||
flow.svlan_tag = cmd->svlan_tag;
|
||||
flow.cvlan_tag = cmd->cvlan_tag;
|
||||
flow.session_id = cmd->session_id;
|
||||
flow.proto = cmd->proto;
|
||||
flow.sport = cmd->sport;
|
||||
flow.dport = cmd->dport;
|
||||
memcpy(flow.sip, cmd->saddr, sizeof(flow.sip));
|
||||
memcpy(flow.dip, cmd->daddr, sizeof(flow.dip));
|
||||
|
||||
entry = l2flow_find(&flow);
|
||||
if (entry != NULL) {
|
||||
entry->fpp_programmed = 0; /* already removed by CDX */
|
||||
l2flow_del(entry);
|
||||
}
|
||||
|
||||
/* Notify auto_bridge so it can clean up kernel state */
|
||||
autobridge_respond(&flow, ABM_FLAG_ACK);
|
||||
|
||||
return (FCI_CB_CONTINUE);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Module lifecycle */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/*
|
||||
* Enable L2 bridge on each detected bridge member port in CDX.
|
||||
*/
|
||||
static void
|
||||
bridge_enable_ports(struct cmm_global *g)
|
||||
{
|
||||
struct list_head *bpos, *ppos;
|
||||
|
||||
for (bpos = list_first(&bridge_list); bpos != &bridge_list;
|
||||
bpos = list_next(bpos)) {
|
||||
struct cmm_bridge *br = container_of(bpos,
|
||||
struct cmm_bridge, entry);
|
||||
|
||||
for (ppos = list_first(&br->ports); ppos != &br->ports;
|
||||
ppos = list_next(ppos)) {
|
||||
struct cmm_bridge_port *port = container_of(ppos,
|
||||
struct cmm_bridge_port, entry);
|
||||
fpp_l2_bridge_enable_cmd_t ecmd;
|
||||
|
||||
memset(&ecmd, 0, sizeof(ecmd));
|
||||
ecmd.enable_flag = 1;
|
||||
strlcpy(ecmd.input_name, port->ifname,
|
||||
sizeof(ecmd.input_name));
|
||||
|
||||
if (fci_write(g->fci_handle,
|
||||
FPP_CMD_RX_L2BRIDGE_ENABLE,
|
||||
sizeof(ecmd),
|
||||
(unsigned short *)&ecmd) != 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: L2BRIDGE_ENABLE "
|
||||
"failed for %s", port->ifname);
|
||||
} else {
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"bridge: L2BRIDGE enabled "
|
||||
"on %s", port->ifname);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
cmm_bridge_init(struct cmm_global *g)
|
||||
{
|
||||
fpp_l2_bridge_control_cmd_t ctrl;
|
||||
int sd, i;
|
||||
|
||||
/* Initialize hash table */
|
||||
for (i = 0; i < CMM_L2FLOW_HASH_SIZE; i++)
|
||||
list_head_init(&l2flow_table[i]);
|
||||
|
||||
list_head_init(&bridge_list);
|
||||
|
||||
/* Scan for bridges */
|
||||
sd = socket(AF_LOCAL, SOCK_DGRAM, 0);
|
||||
if (sd >= 0) {
|
||||
bridge_scan(sd);
|
||||
close(sd);
|
||||
}
|
||||
|
||||
/* Open /dev/autobridge */
|
||||
autobridge_fd = open(ABM_DEV_PATH, O_RDWR | O_NONBLOCK);
|
||||
if (autobridge_fd < 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: open %s: %s — running in manual mode",
|
||||
ABM_DEV_PATH, strerror(errno));
|
||||
|
||||
/* Manual mode: still enable bridge in CDX without
|
||||
* auto flow detection */
|
||||
ctrl.mode_timeout = FPP_L2_BRIDGE_MODE_MANUAL;
|
||||
fci_write(g->fci_handle, FPP_CMD_RX_L2BRIDGE_MODE,
|
||||
sizeof(ctrl), (unsigned short *)&ctrl);
|
||||
|
||||
bridge_enable_ports(g);
|
||||
cmm_bridge_itf_update(g);
|
||||
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"bridge: initialized (manual mode)");
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Store fd in global state for kqueue registration */
|
||||
g->autobridge_fd = autobridge_fd;
|
||||
|
||||
/* Enable bridging on all detected member ports */
|
||||
bridge_enable_ports(g);
|
||||
|
||||
/* Notify CDX about bridged interfaces */
|
||||
cmm_bridge_itf_update(g);
|
||||
|
||||
/* Set auto mode */
|
||||
ctrl.mode_timeout = FPP_L2_BRIDGE_MODE_AUTO;
|
||||
if (fci_write(g->fci_handle, FPP_CMD_RX_L2BRIDGE_MODE,
|
||||
sizeof(ctrl), (unsigned short *)&ctrl) != 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: failed to set AUTO mode");
|
||||
} else {
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"bridge: set AUTO mode in CDX");
|
||||
}
|
||||
|
||||
/* Set default flow timeout */
|
||||
ctrl.mode_timeout = CMM_L2FLOW_TIMEOUT_DEFAULT;
|
||||
if (fci_write(g->fci_handle, FPP_CMD_RX_L2BRIDGE_FLOW_TIMEOUT,
|
||||
sizeof(ctrl), (unsigned short *)&ctrl) != 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"bridge: failed to set flow timeout");
|
||||
} else {
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"bridge: set flow timeout to %ds",
|
||||
CMM_L2FLOW_TIMEOUT_DEFAULT);
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"bridge: initialized (auto mode, fd=%d)", autobridge_fd);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_bridge_fini(struct cmm_global *g)
|
||||
{
|
||||
/* Deregister all flows from CDX */
|
||||
l2flow_flush_all(g);
|
||||
|
||||
/* Free bridge records */
|
||||
bridge_free_all();
|
||||
|
||||
/* Close autobridge device */
|
||||
if (autobridge_fd >= 0) {
|
||||
close(autobridge_fd);
|
||||
autobridge_fd = -1;
|
||||
g->autobridge_fd = -1;
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "bridge: shut down");
|
||||
}
|
||||
86
fastpath/cmm/cmm_bridge.h
Normal file
86
fastpath/cmm/cmm_bridge.h
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* cmm_bridge.h — L2 bridge offload module
|
||||
*
|
||||
* Consumes L2 flow events from auto_bridge.ko (/dev/autobridge)
|
||||
* and programs CDX via FPP_CMD_RX_L2FLOW_ENTRY. Also handles
|
||||
* bridge port resolution for L3 routes (when the output interface
|
||||
* is a bridge, CDX needs the physical member port name).
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_BRIDGE_H
|
||||
#define CMM_BRIDGE_H
|
||||
|
||||
#include "cmm.h"
|
||||
#include "auto_bridge.h"
|
||||
|
||||
#define CMM_L2FLOW_HASH_SIZE 1024
|
||||
|
||||
/*
|
||||
* L2 flow table entry — tracks flows offloaded to CDX.
|
||||
* Keyed by abm_l2flow tuple (same as kernel hash key).
|
||||
*/
|
||||
struct cmm_l2flow {
|
||||
struct list_head hash_entry;
|
||||
struct abm_l2flow flow; /* L2 flow tuple */
|
||||
char input_name[IFNAMSIZ];
|
||||
char output_name[IFNAMSIZ];
|
||||
uint16_t mark;
|
||||
int fpp_programmed; /* registered in CDX */
|
||||
};
|
||||
|
||||
/*
|
||||
* Bridge member port record — cached from BRDGGIFS ioctl.
|
||||
*/
|
||||
struct cmm_bridge_port {
|
||||
struct list_head entry;
|
||||
char ifname[IFNAMSIZ];
|
||||
int ifindex;
|
||||
};
|
||||
|
||||
/*
|
||||
* Per-bridge record — tracks detected bridges and their ports.
|
||||
*/
|
||||
struct cmm_bridge {
|
||||
struct list_head entry;
|
||||
char ifname[IFNAMSIZ];
|
||||
int ifindex;
|
||||
uint8_t macaddr[ETHER_ADDR_LEN];
|
||||
struct list_head ports; /* cmm_bridge_port list */
|
||||
};
|
||||
|
||||
/* Module lifecycle */
|
||||
int cmm_bridge_init(struct cmm_global *g);
|
||||
void cmm_bridge_fini(struct cmm_global *g);
|
||||
|
||||
/* Called from kqueue event loop when /dev/autobridge is readable */
|
||||
void cmm_bridge_event(struct cmm_global *g);
|
||||
|
||||
/*
|
||||
* Bridge port resolution for L3 routes.
|
||||
* When a route's output interface is a bridge, this queries the
|
||||
* bridge FDB to find which physical member port can reach dst_mac,
|
||||
* writing the port's interface name into out_ifname.
|
||||
*
|
||||
* Returns 0 on success, -1 if MAC not found in FDB.
|
||||
*/
|
||||
int cmm_bridge_resolve_port(int bridge_ifindex,
|
||||
const uint8_t *dst_mac, char *out_ifname, size_t namelen);
|
||||
|
||||
/*
|
||||
* Rescan bridges after interface changes (RTM_IFINFO).
|
||||
* Detects new/removed bridges and updates member port lists.
|
||||
*/
|
||||
void cmm_bridge_itf_update(struct cmm_global *g);
|
||||
|
||||
/*
|
||||
* Handle FCI async events (CDX L2 bridge flow timeout notifications).
|
||||
* Called from the top-level FCI event dispatcher in cmm.c.
|
||||
* Returns FCI_CB_CONTINUE for unrecognized events.
|
||||
*/
|
||||
int cmm_bridge_fci_event(unsigned short fcode, unsigned short len,
|
||||
unsigned short *payload);
|
||||
|
||||
#endif /* CMM_BRIDGE_H */
|
||||
1122
fastpath/cmm/cmm_conn.c
Normal file
1122
fastpath/cmm/cmm_conn.c
Normal file
File diff suppressed because it is too large
Load diff
87
fastpath/cmm/cmm_conn.h
Normal file
87
fastpath/cmm/cmm_conn.h
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* cmm_conn.h — PF state table polling and connection tracking
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_CONN_H
|
||||
#define CMM_CONN_H
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_route.h"
|
||||
|
||||
#define CONN_HASH_SIZE 16384
|
||||
|
||||
/* Connection flags */
|
||||
#define CONN_F_OFFLOADED 0x01 /* programmed into CDX */
|
||||
#define CONN_F_ORIG_DIR 0x02 /* offload original direction */
|
||||
#define CONN_F_REP_DIR 0x04 /* offload reply direction */
|
||||
#define CONN_F_HAS_NAT 0x08 /* reply tuples from PF_OUT (NAT) */
|
||||
|
||||
struct cmm_conn {
|
||||
struct list_head hash_entry; /* hash by original 5-tuple */
|
||||
|
||||
/* Original direction 5-tuple (from PF_SK_STACK) */
|
||||
sa_family_t af;
|
||||
uint8_t proto;
|
||||
uint8_t orig_saddr[16];
|
||||
uint8_t orig_daddr[16];
|
||||
uint16_t orig_sport;
|
||||
uint16_t orig_dport;
|
||||
|
||||
/* Reply direction (from PF_SK_STACK, reversed for NAT) */
|
||||
uint8_t rep_saddr[16];
|
||||
uint8_t rep_daddr[16];
|
||||
uint16_t rep_sport;
|
||||
uint16_t rep_dport;
|
||||
|
||||
/* PF tracking */
|
||||
uint64_t pf_id;
|
||||
uint32_t pf_creatorid;
|
||||
uint8_t pf_direction;
|
||||
char ifname[IFNAMSIZ];
|
||||
|
||||
/* Offload state */
|
||||
int flags;
|
||||
|
||||
/* Routes for each direction */
|
||||
struct cmm_route *orig_route;
|
||||
struct cmm_route *rep_route;
|
||||
|
||||
/* Epoch tracking for garbage collection */
|
||||
uint32_t last_seen_epoch;
|
||||
};
|
||||
|
||||
/* Initialize connection table */
|
||||
int cmm_conn_init(void);
|
||||
void cmm_conn_fini(struct cmm_global *g);
|
||||
|
||||
/*
|
||||
* Poll PF state table. Called periodically from the event loop.
|
||||
* Detects new connections, expired connections, and triggers
|
||||
* offload/deregister as needed.
|
||||
*/
|
||||
void cmm_conn_poll(struct cmm_global *g);
|
||||
|
||||
/*
|
||||
* Handle push-based PF state events from /dev/pfnotify.
|
||||
* Called from the event loop when the pfnotify fd is readable.
|
||||
*/
|
||||
void cmm_conn_event(struct cmm_global *g);
|
||||
|
||||
/* Remove all offloaded connections (e.g. on shutdown) */
|
||||
void cmm_conn_deregister_all(struct cmm_global *g);
|
||||
|
||||
/* Deregister connections using a changed route, clearing their route pointers */
|
||||
void cmm_route_invalidate_conns(struct cmm_global *g, struct cmm_route *rt);
|
||||
|
||||
/*
|
||||
* Handle FCI async events (CDX conntrack timeout/FIN notifications).
|
||||
* Called from the top-level FCI event dispatcher in cmm.c.
|
||||
* Returns FCI_CB_CONTINUE for unrecognized events.
|
||||
*/
|
||||
int cmm_conn_fci_event(unsigned short fcode, unsigned short len,
|
||||
unsigned short *payload);
|
||||
|
||||
#endif /* CMM_CONN_H */
|
||||
445
fastpath/cmm/cmm_ctrl.c
Normal file
445
fastpath/cmm/cmm_ctrl.c
Normal file
|
|
@ -0,0 +1,445 @@
|
|||
/*
|
||||
* cmm_ctrl.c — CMM control socket server
|
||||
*
|
||||
* Accepts connections on a Unix domain socket and forwards
|
||||
* FPP commands to CDX via FCI. Used by cmmctl and other tools.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/event.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/un.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_ctrl.h"
|
||||
#include "cmm_tunnel.h"
|
||||
#include "cmm_l2tp.h"
|
||||
#include "cmm_socket.h"
|
||||
|
||||
/* kqueue udata tags — cast to void * for EV_SET */
|
||||
#define CMM_UDATA_CTRL_LISTEN ((void *)(uintptr_t)1)
|
||||
#define CMM_UDATA_CTRL_CLIENT ((void *)(uintptr_t)2)
|
||||
|
||||
/*
|
||||
* Command whitelist — only these FPP commands may be sent
|
||||
* from external tools through the control socket.
|
||||
*/
|
||||
static const uint16_t ctrl_whitelist[] = {
|
||||
/* QM configuration */
|
||||
FPP_CMD_QM_QOSENABLE,
|
||||
FPP_CMD_QM_RESET,
|
||||
FPP_CMD_QM_SHAPER_CFG,
|
||||
#ifdef LS1043
|
||||
FPP_CMD_QM_WBFQ_CFG,
|
||||
FPP_CMD_QM_CQ_CFG,
|
||||
FPP_CMD_QM_CHNL_ASSIGN,
|
||||
FPP_CMD_QM_DSCP_FQ_MAP_STATUS,
|
||||
FPP_CMD_QM_DSCP_FQ_MAP_CFG,
|
||||
FPP_CMD_QM_DSCP_FQ_MAP_RESET,
|
||||
FPP_CMD_QM_FF_RATE,
|
||||
#endif
|
||||
FPP_CMD_QM_EXPT_RATE,
|
||||
FPP_CMD_QM_INGRESS_POLICER_ENABLE,
|
||||
FPP_CMD_QM_INGRESS_POLICER_CONFIG,
|
||||
FPP_CMD_QM_INGRESS_POLICER_RESET,
|
||||
/* QM queries */
|
||||
FPP_CMD_QM_QUERY,
|
||||
FPP_CMD_QM_QUERY_EXPT_RATE,
|
||||
#ifdef LS1043
|
||||
FPP_CMD_QM_QUERY_FF_RATE,
|
||||
FPP_CMD_QM_QUERY_IFACE_DSCP_FQID_MAP,
|
||||
#endif
|
||||
FPP_CMD_QM_CQ_STATS,
|
||||
FPP_CMD_QM_INGRESS_POLICER_QUERY_STATS,
|
||||
/* Tunnel queries (FCI passthrough to CDX) */
|
||||
FPP_CMD_TUNNEL_QUERY,
|
||||
FPP_CMD_TUNNEL_QUERY_CONT,
|
||||
/* Statistics */
|
||||
FPP_CMD_STAT_ENABLE,
|
||||
FPP_CMD_STAT_QUEUE,
|
||||
FPP_CMD_STAT_INTERFACE_PKT,
|
||||
FPP_CMD_STAT_CONNECTION,
|
||||
FPP_CMD_STAT_PPPOE_STATUS,
|
||||
FPP_CMD_STAT_PPPOE_ENTRY,
|
||||
FPP_CMD_STAT_BRIDGE_STATUS,
|
||||
FPP_CMD_STAT_BRIDGE_ENTRY,
|
||||
FPP_CMD_STAT_IPSEC_STATUS,
|
||||
FPP_CMD_STAT_IPSEC_ENTRY,
|
||||
FPP_CMD_STAT_VLAN_STATUS,
|
||||
FPP_CMD_STAT_VLAN_ENTRY,
|
||||
FPP_CMD_STAT_TUNNEL_STATUS,
|
||||
FPP_CMD_STAT_TUNNEL_ENTRY,
|
||||
FPP_CMD_STAT_FLOW,
|
||||
FPP_CMD_IPR_V4_STATS,
|
||||
FPP_CMD_IPR_V6_STATS,
|
||||
/* L2 Bridge */
|
||||
FPP_CMD_RX_L2BRIDGE_ENABLE,
|
||||
FPP_CMD_RX_L2BRIDGE_ADD,
|
||||
FPP_CMD_RX_L2BRIDGE_REMOVE,
|
||||
FPP_CMD_RX_L2BRIDGE_QUERY_STATUS,
|
||||
FPP_CMD_RX_L2BRIDGE_QUERY_ENTRY,
|
||||
FPP_CMD_RX_L2FLOW_ENTRY,
|
||||
FPP_CMD_RX_L2BRIDGE_MODE,
|
||||
FPP_CMD_RX_L2BRIDGE_FLOW_TIMEOUT,
|
||||
FPP_CMD_RX_L2BRIDGE_FLOW_RESET,
|
||||
/* Fast-forward control */
|
||||
FPP_CMD_IPV4_FF_CONTROL,
|
||||
FPP_CMD_IPSEC_FRAG_CFG,
|
||||
/* Socket statistics */
|
||||
FPP_CMD_SOCKETSTATS_STATUS,
|
||||
FPP_CMD_SOCKETSTATS_ENTRY,
|
||||
/* Packet capture */
|
||||
FPP_CMD_PKTCAP_IFSTATUS,
|
||||
FPP_CMD_PKTCAP_FLF,
|
||||
FPP_CMD_PKTCAP_SLICE,
|
||||
FPP_CMD_PKTCAP_QUERY,
|
||||
/* NAT-PT */
|
||||
FPP_CMD_NATPT_OPEN,
|
||||
FPP_CMD_NATPT_CLOSE,
|
||||
FPP_CMD_NATPT_QUERY,
|
||||
/* ICC (ingress congestion control) */
|
||||
FPP_CMD_ICC_RESET,
|
||||
FPP_CMD_ICC_THRESHOLD,
|
||||
FPP_CMD_ICC_ADD_DELETE,
|
||||
FPP_CMD_ICC_QUERY,
|
||||
/* MACVLAN (virtual MAC interfaces) */
|
||||
FPP_CMD_MACVLAN_ENTRY,
|
||||
FPP_CMD_MACVLAN_RESET,
|
||||
/* L2TP */
|
||||
FPP_CMD_L2TP_ITF_ADD,
|
||||
FPP_CMD_L2TP_ITF_DEL,
|
||||
#ifdef LS1043
|
||||
/* TX DSCP to VLAN PCP mapping */
|
||||
FPP_CMD_DSCP_VLANPCP_MAP_STATUS,
|
||||
FPP_CMD_DSCP_VLANPCP_MAP_CFG,
|
||||
FPP_CMD_QUERY_IFACE_DSCP_VLANPCP_MAP,
|
||||
#endif
|
||||
/* Trace / profiling */
|
||||
FPP_CMD_TRC_ON,
|
||||
FPP_CMD_TRC_OFF,
|
||||
FPP_CMD_TRC_SWITCH,
|
||||
FPP_CMD_TRC_DMEM,
|
||||
FPP_CMD_TRC_SETMASK,
|
||||
FPP_CMD_TRC_SHOW,
|
||||
FPP_CMD_TRC_BSYCPU,
|
||||
FPP_CMD_TRC_STATUS,
|
||||
};
|
||||
|
||||
static int
|
||||
ctrl_cmd_allowed(uint16_t cmd)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < sizeof(ctrl_whitelist) / sizeof(ctrl_whitelist[0]);
|
||||
i++) {
|
||||
if (ctrl_whitelist[i] == cmd)
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Read exactly `len` bytes from a stream socket.
|
||||
* Returns len on success, 0 on clean close, -1 on error.
|
||||
*/
|
||||
static ssize_t
|
||||
read_exact(int fd, void *buf, size_t len)
|
||||
{
|
||||
size_t done = 0;
|
||||
|
||||
while (done < len) {
|
||||
ssize_t n = read(fd, (char *)buf + done, len - done);
|
||||
if (n <= 0)
|
||||
return (n);
|
||||
done += n;
|
||||
}
|
||||
return ((ssize_t)done);
|
||||
}
|
||||
|
||||
/*
|
||||
* Write exactly `len` bytes to a stream socket.
|
||||
* Returns len on success, -1 on error.
|
||||
*/
|
||||
static ssize_t
|
||||
write_exact(int fd, const void *buf, size_t len)
|
||||
{
|
||||
size_t done = 0;
|
||||
|
||||
while (done < len) {
|
||||
ssize_t n = write(fd, (const char *)buf + done, len - done);
|
||||
if (n < 0)
|
||||
return (-1);
|
||||
done += n;
|
||||
}
|
||||
return ((ssize_t)done);
|
||||
}
|
||||
|
||||
static void
|
||||
ctrl_close_client(struct cmm_global *g, int fd)
|
||||
{
|
||||
struct kevent kev;
|
||||
int i;
|
||||
|
||||
EV_SET(&kev, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
|
||||
kevent(g->kq, &kev, 1, NULL, 0, NULL);
|
||||
close(fd);
|
||||
|
||||
for (i = 0; i < CMM_CTRL_MAX_CLIENTS; i++) {
|
||||
if (g->ctrl_clients[i] == fd) {
|
||||
g->ctrl_clients[i] = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG, "ctrl: client fd=%d closed", fd);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_ctrl_init(struct cmm_global *g)
|
||||
{
|
||||
struct sockaddr_un sun;
|
||||
int fd, i;
|
||||
|
||||
for (i = 0; i < CMM_CTRL_MAX_CLIENTS; i++)
|
||||
g->ctrl_clients[i] = -1;
|
||||
g->ctrl_listen_fd = -1;
|
||||
|
||||
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (fd < 0) {
|
||||
cmm_print(CMM_LOG_ERR, "ctrl: socket: %s", strerror(errno));
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Remove stale socket from previous unclean exit */
|
||||
unlink(CMM_CTRL_SOCK);
|
||||
|
||||
memset(&sun, 0, sizeof(sun));
|
||||
sun.sun_family = AF_UNIX;
|
||||
strlcpy(sun.sun_path, CMM_CTRL_SOCK, sizeof(sun.sun_path));
|
||||
|
||||
if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
|
||||
cmm_print(CMM_LOG_ERR, "ctrl: bind %s: %s",
|
||||
CMM_CTRL_SOCK, strerror(errno));
|
||||
close(fd);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Enforce root-only access regardless of umask */
|
||||
if (chmod(CMM_CTRL_SOCK, S_IRUSR | S_IWUSR) < 0)
|
||||
cmm_print(CMM_LOG_WARN, "ctrl: chmod %s: %s",
|
||||
CMM_CTRL_SOCK, strerror(errno));
|
||||
|
||||
if (listen(fd, CMM_CTRL_MAX_CLIENTS) < 0) {
|
||||
cmm_print(CMM_LOG_ERR, "ctrl: listen: %s", strerror(errno));
|
||||
close(fd);
|
||||
unlink(CMM_CTRL_SOCK);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
g->ctrl_listen_fd = fd;
|
||||
cmm_print(CMM_LOG_INFO, "ctrl: listening on %s", CMM_CTRL_SOCK);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_ctrl_fini(struct cmm_global *g)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CMM_CTRL_MAX_CLIENTS; i++) {
|
||||
if (g->ctrl_clients[i] >= 0) {
|
||||
close(g->ctrl_clients[i]);
|
||||
g->ctrl_clients[i] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (g->ctrl_listen_fd >= 0) {
|
||||
close(g->ctrl_listen_fd);
|
||||
g->ctrl_listen_fd = -1;
|
||||
unlink(CMM_CTRL_SOCK);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cmm_ctrl_accept(struct cmm_global *g)
|
||||
{
|
||||
struct kevent kev;
|
||||
int fd, i, slot;
|
||||
|
||||
fd = accept(g->ctrl_listen_fd, NULL, NULL);
|
||||
if (fd < 0) {
|
||||
cmm_print(CMM_LOG_WARN, "ctrl: accept: %s", strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
/* Find a free client slot */
|
||||
slot = -1;
|
||||
for (i = 0; i < CMM_CTRL_MAX_CLIENTS; i++) {
|
||||
if (g->ctrl_clients[i] < 0) {
|
||||
slot = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (slot < 0) {
|
||||
cmm_print(CMM_LOG_WARN, "ctrl: too many clients, rejecting");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
g->ctrl_clients[slot] = fd;
|
||||
|
||||
/* Non-blocking so a partial sender can't stall the event loop */
|
||||
{
|
||||
int flags = fcntl(fd, F_GETFL, 0);
|
||||
if (flags >= 0)
|
||||
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
}
|
||||
|
||||
EV_SET(&kev, fd, EVFILT_READ, EV_ADD, 0, 0, CMM_UDATA_CTRL_CLIENT);
|
||||
if (kevent(g->kq, &kev, 1, NULL, 0, NULL) < 0) {
|
||||
cmm_print(CMM_LOG_ERR, "ctrl: kevent add client: %s",
|
||||
strerror(errno));
|
||||
close(fd);
|
||||
g->ctrl_clients[slot] = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG, "ctrl: client fd=%d connected (slot %d)",
|
||||
fd, slot);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_ctrl_dispatch(struct cmm_global *g, int client_fd)
|
||||
{
|
||||
struct cmm_ctrl_hdr hdr;
|
||||
struct cmm_ctrl_resp resp;
|
||||
/* Aligned buffer for FCI — fci_cmd takes unsigned short * */
|
||||
uint16_t cmd_buf[CMM_CTRL_MAX_PAYLOAD / 2];
|
||||
uint16_t resp_buf[CMM_CTRL_MAX_PAYLOAD / 2];
|
||||
unsigned short resp_len;
|
||||
int rc;
|
||||
|
||||
/* Read request header */
|
||||
if (read_exact(client_fd, &hdr, sizeof(hdr)) <= 0) {
|
||||
ctrl_close_client(g, client_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Validate payload length */
|
||||
if (hdr.len > CMM_CTRL_MAX_PAYLOAD) {
|
||||
cmm_print(CMM_LOG_WARN, "ctrl: bad payload len %u", hdr.len);
|
||||
resp.rc = CMM_CTRL_ERR_BAD_LEN;
|
||||
resp.len = 0;
|
||||
write_exact(client_fd, &resp, sizeof(resp));
|
||||
ctrl_close_client(g, client_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Read payload */
|
||||
if (hdr.len > 0) {
|
||||
if (read_exact(client_fd, cmd_buf, hdr.len) <= 0) {
|
||||
ctrl_close_client(g, client_fd);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* CMM-internal commands (above FPP range).
|
||||
* These are CMM's own control protocol, not FPP passthrough,
|
||||
* so they are intentionally exempt from the FPP whitelist.
|
||||
* The socket is chmod 0600 (root-only); no additional
|
||||
* authentication is needed.
|
||||
*/
|
||||
if (hdr.cmd >= CMM_CTRL_CMD_BASE) {
|
||||
switch (hdr.cmd) {
|
||||
case CMM_CTRL_CMD_TNL_ADD:
|
||||
cmm_tunnel_ctrl_add(g, client_fd, cmd_buf, hdr.len);
|
||||
return;
|
||||
case CMM_CTRL_CMD_TNL_DEL:
|
||||
cmm_tunnel_ctrl_del(g, client_fd, cmd_buf, hdr.len);
|
||||
return;
|
||||
case CMM_CTRL_CMD_SOCKET_OPEN:
|
||||
cmm_socket_ctrl_open(g, client_fd, cmd_buf, hdr.len);
|
||||
return;
|
||||
case CMM_CTRL_CMD_SOCKET_CLOSE:
|
||||
cmm_socket_ctrl_close(g, client_fd, cmd_buf, hdr.len);
|
||||
return;
|
||||
case CMM_CTRL_CMD_SOCKET_UPDATE:
|
||||
cmm_socket_ctrl_update(g, client_fd, cmd_buf, hdr.len);
|
||||
return;
|
||||
case CMM_CTRL_CMD_L2TP_ADD:
|
||||
cmm_l2tp_ctrl_add(g, client_fd, cmd_buf, hdr.len);
|
||||
return;
|
||||
case CMM_CTRL_CMD_L2TP_DEL:
|
||||
cmm_l2tp_ctrl_del(g, client_fd, cmd_buf, hdr.len);
|
||||
return;
|
||||
default:
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"ctrl: unknown internal cmd 0x%04x", hdr.cmd);
|
||||
resp.rc = CMM_CTRL_ERR_UNKNOWN_CMD;
|
||||
resp.len = 0;
|
||||
write_exact(client_fd, &resp, sizeof(resp));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* Whitelist check */
|
||||
if (!ctrl_cmd_allowed(hdr.cmd)) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"ctrl: rejected cmd 0x%04x (not whitelisted)", hdr.cmd);
|
||||
resp.rc = CMM_CTRL_ERR_UNKNOWN_CMD;
|
||||
resp.len = 0;
|
||||
write_exact(client_fd, &resp, sizeof(resp));
|
||||
return;
|
||||
}
|
||||
|
||||
/* Check FCI handle */
|
||||
if (g->fci_handle == NULL) {
|
||||
resp.rc = CMM_CTRL_ERR_NO_FCI;
|
||||
resp.len = 0;
|
||||
write_exact(client_fd, &resp, sizeof(resp));
|
||||
return;
|
||||
}
|
||||
|
||||
/* Forward to CDX via FCI */
|
||||
resp_len = sizeof(resp_buf);
|
||||
rc = fci_cmd(g->fci_handle, hdr.cmd,
|
||||
(unsigned short *)cmd_buf, hdr.len,
|
||||
(unsigned short *)resp_buf, &resp_len);
|
||||
|
||||
if (rc < 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"ctrl: fci_cmd 0x%04x failed: %s",
|
||||
hdr.cmd, strerror(errno));
|
||||
resp.rc = CMM_CTRL_ERR_FCI_FAIL;
|
||||
resp.len = 0;
|
||||
write_exact(client_fd, &resp, sizeof(resp));
|
||||
return;
|
||||
}
|
||||
|
||||
/* Send response back to client — forward FPP error code */
|
||||
resp.rc = (resp_len >= 2) ? (int16_t)resp_buf[0] : 0;
|
||||
resp.len = resp_len;
|
||||
if (write_exact(client_fd, &resp, sizeof(resp)) < 0) {
|
||||
ctrl_close_client(g, client_fd);
|
||||
return;
|
||||
}
|
||||
if (resp_len > 0) {
|
||||
if (write_exact(client_fd, resp_buf, resp_len) < 0) {
|
||||
ctrl_close_client(g, client_fd);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_TRACE, "ctrl: cmd 0x%04x len=%u → rc=%d rsp=%u",
|
||||
hdr.cmd, hdr.len, rc, resp_len);
|
||||
}
|
||||
86
fastpath/cmm/cmm_ctrl.h
Normal file
86
fastpath/cmm/cmm_ctrl.h
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* cmm_ctrl.h — CMM control socket protocol definitions
|
||||
*
|
||||
* Binary protocol over Unix domain socket for external tools
|
||||
* (e.g., cmmctl) to send FPP commands through CMM to CDX.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_CTRL_H
|
||||
#define CMM_CTRL_H
|
||||
|
||||
#include <net/if.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define CMM_CTRL_SOCK "/var/run/cmm.sock"
|
||||
#define CMM_CTRL_MAX_PAYLOAD 512 /* matches FCI_MSG_MAX_PAYLOAD */
|
||||
#define CMM_CTRL_MAX_CLIENTS 4
|
||||
|
||||
/*
|
||||
* Request: client → CMM daemon.
|
||||
* Followed by `len` bytes of FPP command payload.
|
||||
*/
|
||||
struct cmm_ctrl_hdr {
|
||||
uint16_t cmd; /* FPP command code */
|
||||
uint16_t len; /* payload length in bytes */
|
||||
};
|
||||
|
||||
/*
|
||||
* Response: CMM daemon → client.
|
||||
* rc: 0 = success, >0 = FPP error code, <0 = CMM error
|
||||
* Followed by `len` bytes of FPP response data.
|
||||
*/
|
||||
struct cmm_ctrl_resp {
|
||||
int16_t rc; /* result code */
|
||||
uint16_t len; /* response payload length */
|
||||
};
|
||||
|
||||
/* CMM error codes (rc < 0) */
|
||||
#define CMM_CTRL_ERR_UNKNOWN_CMD (-1)
|
||||
#define CMM_CTRL_ERR_FCI_FAIL (-2)
|
||||
#define CMM_CTRL_ERR_NO_FCI (-3)
|
||||
#define CMM_CTRL_ERR_BAD_LEN (-4)
|
||||
|
||||
/* CMM-internal control commands (above FPP range) */
|
||||
#define CMM_CTRL_CMD_BASE 0xF000
|
||||
#define CMM_CTRL_CMD_TNL_ADD 0xF001 /* payload: char name[IFNAMSIZ] */
|
||||
#define CMM_CTRL_CMD_TNL_DEL 0xF002 /* payload: char name[IFNAMSIZ] */
|
||||
#define CMM_CTRL_CMD_SOCKET_OPEN 0xF010 /* payload: cmm_ctrl_socket_open */
|
||||
#define CMM_CTRL_CMD_SOCKET_CLOSE 0xF011 /* payload: cmm_ctrl_socket_close */
|
||||
#define CMM_CTRL_CMD_SOCKET_UPDATE 0xF012 /* payload: cmm_ctrl_socket_update */
|
||||
#define CMM_CTRL_CMD_L2TP_ADD 0xF020 /* payload: cmm_ctrl_l2tp_add */
|
||||
#define CMM_CTRL_CMD_L2TP_DEL 0xF021 /* payload: char name[IFNAMSIZ] */
|
||||
|
||||
/*
|
||||
* L2TP session create payload.
|
||||
* All L2TP parameters must be provided — there is no kernel L2TP
|
||||
* driver to query them from.
|
||||
*/
|
||||
struct cmm_ctrl_l2tp_add {
|
||||
char ifname[IFNAMSIZ]; /* interface name */
|
||||
uint8_t af; /* AF_INET=2, AF_INET6=28 */
|
||||
uint8_t pad1;
|
||||
uint16_t local_port; /* network byte order */
|
||||
uint16_t peer_port; /* network byte order */
|
||||
uint16_t local_tun_id;
|
||||
uint16_t peer_tun_id;
|
||||
uint16_t local_ses_id;
|
||||
uint16_t peer_ses_id;
|
||||
uint16_t options; /* L2TP option flags */
|
||||
uint16_t dscp;
|
||||
uint8_t queue;
|
||||
uint8_t pad2;
|
||||
uint8_t local_addr[16]; /* IPv4 in first 4, IPv6 full */
|
||||
uint8_t peer_addr[16]; /* same */
|
||||
};
|
||||
|
||||
struct cmm_global;
|
||||
|
||||
int cmm_ctrl_init(struct cmm_global *g);
|
||||
void cmm_ctrl_fini(struct cmm_global *g);
|
||||
void cmm_ctrl_accept(struct cmm_global *g);
|
||||
void cmm_ctrl_dispatch(struct cmm_global *g, int client_fd);
|
||||
|
||||
#endif /* CMM_CTRL_H */
|
||||
338
fastpath/cmm/cmm_deny.c
Normal file
338
fastpath/cmm/cmm_deny.c
Normal file
|
|
@ -0,0 +1,338 @@
|
|||
/*
|
||||
* cmm_deny.c — Deny-rule filtering for offload eligibility
|
||||
*
|
||||
* Parses /usr/local/etc/cmm_deny.conf (or a user-specified path)
|
||||
* and maintains a linked list of deny rules. Each rule is a set
|
||||
* of AND'd conditions. If ANY rule matches a PF state, the
|
||||
* connection is denied offload and stays in the software path.
|
||||
*
|
||||
* Config file format:
|
||||
* # comment
|
||||
* field=value [field=value ...]
|
||||
*
|
||||
* Supported fields:
|
||||
* proto=tcp|udp|<number>
|
||||
* src=<addr>[/<prefix>]
|
||||
* dst=<addr>[/<prefix>]
|
||||
* sport=<port>
|
||||
* dport=<port>
|
||||
* iface=<name>
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#define COMPAT_FREEBSD14
|
||||
#include <net/pfvar.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_deny.h"
|
||||
|
||||
static struct cmm_deny_list deny_rules = STAILQ_HEAD_INITIALIZER(deny_rules);
|
||||
static int deny_count;
|
||||
|
||||
/*
|
||||
* Compare the first prefixlen bits of two addresses.
|
||||
* Returns 1 if they match, 0 otherwise.
|
||||
*/
|
||||
static int
|
||||
addr_match(sa_family_t af, const void *addr,
|
||||
const struct in6_addr *rule_addr, uint8_t prefixlen)
|
||||
{
|
||||
const uint8_t *a = (const uint8_t *)addr;
|
||||
const uint8_t *r = (const uint8_t *)rule_addr;
|
||||
int alen, full_bytes, remaining;
|
||||
|
||||
alen = (af == AF_INET) ? 4 : 16;
|
||||
if (prefixlen > (uint8_t)(alen * 8))
|
||||
prefixlen = (uint8_t)(alen * 8);
|
||||
|
||||
full_bytes = prefixlen / 8;
|
||||
remaining = prefixlen % 8;
|
||||
|
||||
if (full_bytes > 0 && memcmp(a, r, full_bytes) != 0)
|
||||
return (0);
|
||||
|
||||
if (remaining > 0) {
|
||||
uint8_t mask = (uint8_t)(0xFF << (8 - remaining));
|
||||
if ((a[full_bytes] & mask) != (r[full_bytes] & mask))
|
||||
return (0);
|
||||
}
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse an address with optional /prefix.
|
||||
* Sets addr (stored in in6_addr, v4-mapped for IPv4) and prefixlen.
|
||||
* Returns the detected address family, or 0 on error.
|
||||
*/
|
||||
static sa_family_t
|
||||
parse_addr(const char *s, struct in6_addr *out, uint8_t *prefixlen)
|
||||
{
|
||||
char buf[INET6_ADDRSTRLEN + 4];
|
||||
char *slash;
|
||||
struct in_addr v4;
|
||||
|
||||
strlcpy(buf, s, sizeof(buf));
|
||||
slash = strchr(buf, '/');
|
||||
if (slash != NULL)
|
||||
*slash = '\0';
|
||||
|
||||
/* Try IPv4 first */
|
||||
if (inet_pton(AF_INET, buf, &v4) == 1) {
|
||||
memset(out, 0, sizeof(*out));
|
||||
memcpy(out, &v4, 4);
|
||||
if (slash != NULL) {
|
||||
int p = atoi(slash + 1);
|
||||
if (p < 0 || p > 32)
|
||||
return (0);
|
||||
*prefixlen = (uint8_t)p;
|
||||
} else {
|
||||
*prefixlen = 32;
|
||||
}
|
||||
return (AF_INET);
|
||||
}
|
||||
|
||||
/* Try IPv6 */
|
||||
if (inet_pton(AF_INET6, buf, out) == 1) {
|
||||
if (slash != NULL) {
|
||||
int p = atoi(slash + 1);
|
||||
if (p < 0 || p > 128)
|
||||
return (0);
|
||||
*prefixlen = (uint8_t)p;
|
||||
} else {
|
||||
*prefixlen = 128;
|
||||
}
|
||||
return (AF_INET6);
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse a single config line into a deny rule.
|
||||
* Returns 0 on success, -1 on parse error.
|
||||
*/
|
||||
static int
|
||||
parse_rule(char *line, struct cmm_deny_rule *r)
|
||||
{
|
||||
char *tok, *saveptr;
|
||||
|
||||
memset(r, 0, sizeof(*r));
|
||||
|
||||
for (tok = strtok_r(line, " \t", &saveptr);
|
||||
tok != NULL;
|
||||
tok = strtok_r(NULL, " \t", &saveptr)) {
|
||||
char *eq;
|
||||
|
||||
eq = strchr(tok, '=');
|
||||
if (eq == NULL)
|
||||
return (-1);
|
||||
*eq = '\0';
|
||||
|
||||
if (strcmp(tok, "proto") == 0) {
|
||||
if (strcmp(eq + 1, "tcp") == 0)
|
||||
r->proto = IPPROTO_TCP;
|
||||
else if (strcmp(eq + 1, "udp") == 0)
|
||||
r->proto = IPPROTO_UDP;
|
||||
else {
|
||||
int p = atoi(eq + 1);
|
||||
if (p <= 0 || p > 255)
|
||||
return (-1);
|
||||
r->proto = (uint8_t)p;
|
||||
}
|
||||
} else if (strcmp(tok, "src") == 0) {
|
||||
sa_family_t af;
|
||||
af = parse_addr(eq + 1, &r->src.addr,
|
||||
&r->src.prefixlen);
|
||||
if (af == 0)
|
||||
return (-1);
|
||||
if (r->af == 0)
|
||||
r->af = af;
|
||||
else if (r->af != af)
|
||||
return (-1);
|
||||
} else if (strcmp(tok, "dst") == 0) {
|
||||
sa_family_t af;
|
||||
af = parse_addr(eq + 1, &r->dst.addr,
|
||||
&r->dst.prefixlen);
|
||||
if (af == 0)
|
||||
return (-1);
|
||||
if (r->af == 0)
|
||||
r->af = af;
|
||||
else if (r->af != af)
|
||||
return (-1);
|
||||
} else if (strcmp(tok, "sport") == 0) {
|
||||
int p = atoi(eq + 1);
|
||||
if (p <= 0 || p > 65535)
|
||||
return (-1);
|
||||
r->sport = htons((uint16_t)p);
|
||||
} else if (strcmp(tok, "dport") == 0) {
|
||||
int p = atoi(eq + 1);
|
||||
if (p <= 0 || p > 65535)
|
||||
return (-1);
|
||||
r->dport = htons((uint16_t)p);
|
||||
} else if (strcmp(tok, "iface") == 0) {
|
||||
strlcpy(r->ifname, eq + 1, sizeof(r->ifname));
|
||||
} else {
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_deny_init(const char *confpath)
|
||||
{
|
||||
FILE *f;
|
||||
char line[256];
|
||||
int lineno;
|
||||
|
||||
if (confpath == NULL)
|
||||
confpath = CMM_DENY_CONF;
|
||||
|
||||
f = fopen(confpath, "r");
|
||||
if (f == NULL) {
|
||||
if (errno == ENOENT) {
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"deny: no config file %s — all flows eligible",
|
||||
confpath);
|
||||
return (0);
|
||||
}
|
||||
cmm_print(CMM_LOG_ERR, "deny: open %s: %s",
|
||||
confpath, strerror(errno));
|
||||
return (-1);
|
||||
}
|
||||
|
||||
lineno = 0;
|
||||
while (fgets(line, sizeof(line), f) != NULL) {
|
||||
struct cmm_deny_rule *r;
|
||||
struct cmm_deny_rule tmp;
|
||||
char *p;
|
||||
|
||||
lineno++;
|
||||
|
||||
/* Strip newline */
|
||||
p = strchr(line, '\n');
|
||||
if (p != NULL)
|
||||
*p = '\0';
|
||||
|
||||
/* Skip leading whitespace */
|
||||
p = line;
|
||||
while (isspace((unsigned char)*p))
|
||||
p++;
|
||||
|
||||
/* Skip empty lines and comments */
|
||||
if (*p == '\0' || *p == '#')
|
||||
continue;
|
||||
|
||||
if (parse_rule(p, &tmp) < 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"deny: %s:%d: parse error, skipping",
|
||||
confpath, lineno);
|
||||
continue;
|
||||
}
|
||||
|
||||
r = malloc(sizeof(*r));
|
||||
if (r == NULL) {
|
||||
cmm_print(CMM_LOG_ERR, "deny: malloc: %s",
|
||||
strerror(errno));
|
||||
fclose(f);
|
||||
return (-1);
|
||||
}
|
||||
*r = tmp;
|
||||
STAILQ_INSERT_TAIL(&deny_rules, r, entry);
|
||||
deny_count++;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
cmm_print(CMM_LOG_INFO, "deny: loaded %d rule%s from %s",
|
||||
deny_count, deny_count == 1 ? "" : "s", confpath);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_deny_fini(void)
|
||||
{
|
||||
struct cmm_deny_rule *r;
|
||||
|
||||
while ((r = STAILQ_FIRST(&deny_rules)) != NULL) {
|
||||
STAILQ_REMOVE_HEAD(&deny_rules, entry);
|
||||
free(r);
|
||||
}
|
||||
deny_count = 0;
|
||||
}
|
||||
|
||||
int
|
||||
cmm_deny_check(const struct pf_state_export *pfs)
|
||||
{
|
||||
struct cmm_deny_rule *r;
|
||||
const struct pf_state_key_export *sk;
|
||||
int sidx, didx;
|
||||
|
||||
if (deny_count == 0)
|
||||
return (0);
|
||||
|
||||
sk = &pfs->key[PF_SK_STACK];
|
||||
sidx = (pfs->direction == PF_IN) ? 0 : 1;
|
||||
didx = 1 - sidx;
|
||||
|
||||
STAILQ_FOREACH(r, &deny_rules, entry) {
|
||||
/* Protocol */
|
||||
if (r->proto != 0 && r->proto != pfs->proto)
|
||||
continue;
|
||||
|
||||
/* Address family */
|
||||
if (r->af != 0 && r->af != pfs->af)
|
||||
continue;
|
||||
|
||||
/* Source port */
|
||||
if (r->sport != 0 && r->sport != sk->port[sidx])
|
||||
continue;
|
||||
|
||||
/* Destination port */
|
||||
if (r->dport != 0 && r->dport != sk->port[didx])
|
||||
continue;
|
||||
|
||||
/* Source address */
|
||||
if (r->src.prefixlen != 0 &&
|
||||
!addr_match(pfs->af, &sk->addr[sidx],
|
||||
&r->src.addr, r->src.prefixlen))
|
||||
continue;
|
||||
|
||||
/* Destination address */
|
||||
if (r->dst.prefixlen != 0 &&
|
||||
!addr_match(pfs->af, &sk->addr[didx],
|
||||
&r->dst.addr, r->dst.prefixlen))
|
||||
continue;
|
||||
|
||||
/* Interface */
|
||||
if (r->ifname[0] != '\0' &&
|
||||
strcmp(r->ifname, pfs->ifname) != 0)
|
||||
continue;
|
||||
|
||||
/* All conditions matched — deny offload */
|
||||
cmm_print(CMM_LOG_DEBUG, "deny: rule matched, "
|
||||
"proto=%u iface=%s", pfs->proto, pfs->ifname);
|
||||
return (1);
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_deny_count(void)
|
||||
{
|
||||
|
||||
return (deny_count);
|
||||
}
|
||||
58
fastpath/cmm/cmm_deny.h
Normal file
58
fastpath/cmm/cmm_deny.h
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* cmm_deny.h — Deny-rule filtering for offload eligibility
|
||||
*
|
||||
* Config-file-driven rules that prevent specific connections
|
||||
* from being offloaded to hardware. Rules match on protocol,
|
||||
* source/destination address (with CIDR prefix), ports, and
|
||||
* interface name.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_DENY_H
|
||||
#define CMM_DENY_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/queue.h>
|
||||
#include <net/if.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#define CMM_DENY_CONF "/usr/local/etc/cmm_deny.conf"
|
||||
|
||||
struct cmm_deny_rule {
|
||||
STAILQ_ENTRY(cmm_deny_rule) entry;
|
||||
uint8_t proto; /* IPPROTO_TCP/UDP, or 0 = any */
|
||||
sa_family_t af; /* AF_INET/AF_INET6, or 0 = any */
|
||||
struct {
|
||||
struct in6_addr addr;
|
||||
uint8_t prefixlen; /* 0 = any */
|
||||
} src, dst;
|
||||
uint16_t sport; /* network byte order, 0 = any */
|
||||
uint16_t dport; /* network byte order, 0 = any */
|
||||
char ifname[IFNAMSIZ]; /* empty = any */
|
||||
};
|
||||
|
||||
STAILQ_HEAD(cmm_deny_list, cmm_deny_rule);
|
||||
|
||||
/*
|
||||
* Load deny rules from config file.
|
||||
* If the file doesn't exist, no rules are loaded (everything offloaded).
|
||||
* Returns 0 on success, -1 on fatal error (malloc failure).
|
||||
*/
|
||||
int cmm_deny_init(const char *confpath);
|
||||
|
||||
/* Free all deny rules. */
|
||||
void cmm_deny_fini(void);
|
||||
|
||||
/*
|
||||
* Check if a PF state should be denied offload.
|
||||
* Returns 1 if denied (do not offload), 0 if allowed.
|
||||
*/
|
||||
struct pf_state_export;
|
||||
int cmm_deny_check(const struct pf_state_export *pfs);
|
||||
|
||||
/* Return the number of loaded deny rules. */
|
||||
int cmm_deny_count(void);
|
||||
|
||||
#endif /* CMM_DENY_H */
|
||||
408
fastpath/cmm/cmm_fe.c
Normal file
408
fastpath/cmm/cmm_fe.c
Normal file
|
|
@ -0,0 +1,408 @@
|
|||
/*
|
||||
* cmm_fe.c — Forward engine: programs CDX hash tables via FCI
|
||||
*
|
||||
* Builds FPP command structures and sends them to CDX via the
|
||||
* already-ported libfci library.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <net/if.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_fe.h"
|
||||
#include "cmm_conn.h"
|
||||
#include "cmm_route.h"
|
||||
#include "cmm_itf.h"
|
||||
#include "cmm_bridge.h"
|
||||
|
||||
/* Conntrack command flags (from Linux forward_engine.c) */
|
||||
#define CTCMD_FLAGS_ORIG_DISABLED (1 << 0)
|
||||
#define CTCMD_FLAGS_REP_DISABLED (1 << 1)
|
||||
|
||||
int
|
||||
cmm_fe_reset(struct cmm_global *g)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (g->fci_handle == NULL)
|
||||
return (-1);
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "fe: resetting CDX tables");
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV4_RESET, 0, NULL);
|
||||
if (rc != 0)
|
||||
cmm_print(CMM_LOG_WARN, "fe: IPv4 reset failed: %d", rc);
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV6_RESET, 0, NULL);
|
||||
if (rc != 0)
|
||||
cmm_print(CMM_LOG_WARN, "fe: IPv6 reset failed: %d", rc);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_fe_route_register(struct cmm_global *g, struct cmm_route *rt)
|
||||
{
|
||||
fpp_rt_cmd_t cmd;
|
||||
struct cmm_interface *itf, *iif;
|
||||
int rc;
|
||||
|
||||
if (rt->neigh == NULL || rt->neigh->state != NEIGH_RESOLVED)
|
||||
return (-1);
|
||||
|
||||
itf = cmm_itf_find_by_index(rt->oif_index);
|
||||
if (itf == NULL)
|
||||
return (-1);
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.action = FPP_ACTION_REGISTER;
|
||||
cmd.mtu = rt->mtu;
|
||||
memcpy(cmd.dst_mac, rt->neigh->macaddr, ETHER_ADDR_LEN);
|
||||
cmd.id = rt->fpp_id;
|
||||
|
||||
/*
|
||||
* Bridge port resolution: when the output interface is a
|
||||
* bridge, CDX needs the physical member port name, not the
|
||||
* bridge device. Query the bridge FDB for the destination
|
||||
* MAC to find the egress port.
|
||||
*/
|
||||
if (itf->itf_flags & ITF_F_BRIDGE) {
|
||||
char port_name[IFNAMSIZ];
|
||||
|
||||
if (cmm_bridge_resolve_port(itf->ifindex,
|
||||
rt->neigh->macaddr, port_name,
|
||||
sizeof(port_name)) == 0) {
|
||||
strlcpy(cmd.output_device, port_name,
|
||||
sizeof(cmd.output_device));
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"fe: bridge %s → port %s for route id=%u",
|
||||
itf->ifname, port_name, rt->fpp_id);
|
||||
} else {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"fe: bridge %s FDB lookup failed for "
|
||||
"mac=%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
itf->ifname,
|
||||
rt->neigh->macaddr[0], rt->neigh->macaddr[1],
|
||||
rt->neigh->macaddr[2], rt->neigh->macaddr[3],
|
||||
rt->neigh->macaddr[4], rt->neigh->macaddr[5]);
|
||||
return (-1);
|
||||
}
|
||||
} else {
|
||||
strlcpy(cmd.output_device, itf->ifname,
|
||||
sizeof(cmd.output_device));
|
||||
}
|
||||
|
||||
/*
|
||||
* Input device: the interface where packets arrive before being
|
||||
* forwarded via this route. Set by conn_try_offload() from the
|
||||
* opposite direction's output interface. CDX uses this to install
|
||||
* flow classification entries on the correct FMan RX port.
|
||||
*/
|
||||
if (rt->iif_index != 0) {
|
||||
iif = cmm_itf_find_by_index(rt->iif_index);
|
||||
|
||||
if (iif == NULL) {
|
||||
/*
|
||||
* Not in CMM table. Try kernel directly to handle
|
||||
* late-discovered interfaces.
|
||||
*/
|
||||
char fallback[IFNAMSIZ];
|
||||
|
||||
if (if_indextoname(rt->iif_index, fallback) != NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"fe: route id=%u iif idx=%d (%s) "
|
||||
"not in CMM table, using kernel name",
|
||||
rt->fpp_id, rt->iif_index, fallback);
|
||||
strlcpy(cmd.input_device, fallback,
|
||||
sizeof(cmd.input_device));
|
||||
strlcpy(cmd.underlying_input_device, fallback,
|
||||
sizeof(cmd.underlying_input_device));
|
||||
} else {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"fe: route id=%u iif idx=%d "
|
||||
"does not exist",
|
||||
rt->fpp_id, rt->iif_index);
|
||||
return (-1);
|
||||
}
|
||||
} else if (iif->itf_flags & ITF_F_BRIDGE) {
|
||||
/*
|
||||
* Input is a bridge — CDX only knows physical ports.
|
||||
* resolve_iif_index() tried FDB first; if we still
|
||||
* have a bridge here, FDB resolution failed.
|
||||
*/
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"fe: route id=%u input %s is a bridge "
|
||||
"(FDB lookup failed), deferring",
|
||||
rt->fpp_id, iif->ifname);
|
||||
return (-1);
|
||||
} else {
|
||||
strlcpy(cmd.input_device, iif->ifname,
|
||||
sizeof(cmd.input_device));
|
||||
strlcpy(cmd.underlying_input_device, iif->ifname,
|
||||
sizeof(cmd.underlying_input_device));
|
||||
}
|
||||
} else {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"fe: route id=%u has iif_index=0", rt->fpp_id);
|
||||
}
|
||||
|
||||
if (rt->family == AF_INET) {
|
||||
memcpy(&cmd.dst_addr[0], rt->dst, 4);
|
||||
} else {
|
||||
memcpy(cmd.dst_addr, rt->dst, 16);
|
||||
}
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IP_ROUTE,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
if (rc == FPP_ERR_RT_ENTRY_ALREADY_REGISTERED) {
|
||||
/* Stale entry from previous session — accept it */
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"fe: route id=%u already in CDX, reusing", rt->fpp_id);
|
||||
} else if (rc != 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"fe: route register failed: %d (id=%u)", rc, rt->fpp_id);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
rt->fpp_programmed = 1;
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"fe: route registered id=%u out=%s in=%s "
|
||||
"mac=%02x:%02x:%02x:%02x:%02x:%02x mtu=%u",
|
||||
rt->fpp_id, itf->ifname,
|
||||
cmd.input_device[0] ? cmd.input_device : "(none)",
|
||||
rt->neigh->macaddr[0], rt->neigh->macaddr[1],
|
||||
rt->neigh->macaddr[2], rt->neigh->macaddr[3],
|
||||
rt->neigh->macaddr[4], rt->neigh->macaddr[5],
|
||||
rt->mtu);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_fe_route_deregister(struct cmm_global *g, struct cmm_route *rt)
|
||||
{
|
||||
fpp_rt_cmd_t cmd;
|
||||
int rc;
|
||||
|
||||
if (!rt->fpp_programmed)
|
||||
return (0);
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.action = FPP_ACTION_DEREGISTER;
|
||||
cmd.id = rt->fpp_id;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IP_ROUTE,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"fe: route deregister failed: %d (id=%u)",
|
||||
rc, rt->fpp_id);
|
||||
}
|
||||
|
||||
rt->fpp_programmed = 0;
|
||||
return (rc == 0 ? 0 : -1);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_fe_ct4_register(struct cmm_global *g, struct cmm_conn *conn)
|
||||
{
|
||||
fpp_ct_cmd_t cmd;
|
||||
int rc;
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.action = FPP_ACTION_REGISTER;
|
||||
cmd.protocol = conn->proto;
|
||||
|
||||
memcpy(&cmd.saddr, conn->orig_saddr, 4);
|
||||
memcpy(&cmd.daddr, conn->orig_daddr, 4);
|
||||
cmd.sport = conn->orig_sport;
|
||||
cmd.dport = conn->orig_dport;
|
||||
|
||||
memcpy(&cmd.saddr_reply, conn->rep_saddr, 4);
|
||||
memcpy(&cmd.daddr_reply, conn->rep_daddr, 4);
|
||||
cmd.sport_reply = conn->rep_sport;
|
||||
cmd.dport_reply = conn->rep_dport;
|
||||
|
||||
if (conn->orig_route != NULL)
|
||||
cmd.route_id = conn->orig_route->fpp_id;
|
||||
if (conn->rep_route != NULL)
|
||||
cmd.route_id_reply = conn->rep_route->fpp_id;
|
||||
|
||||
if (!(conn->flags & CONN_F_ORIG_DIR))
|
||||
cmd.flags |= CTCMD_FLAGS_ORIG_DISABLED;
|
||||
if (!(conn->flags & CONN_F_REP_DIR))
|
||||
cmd.flags |= CTCMD_FLAGS_REP_DISABLED;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV4_CONNTRACK,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
if (rc == FPP_ERR_CT_ENTRY_ALREADY_REGISTERED) {
|
||||
/*
|
||||
* Stale entry from a previous CMM session (CDX module
|
||||
* stayed loaded). Deregister the old entry and retry
|
||||
* so we get fresh route IDs and parameters.
|
||||
*/
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"fe: ct4 already exists, replacing");
|
||||
cmd.action = FPP_ACTION_DEREGISTER;
|
||||
fci_write(g->fci_handle, FPP_CMD_IPV4_CONNTRACK,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
cmd.action = FPP_ACTION_REGISTER;
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV4_CONNTRACK,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
}
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_WARN, "fe: ct4 register failed: %d", rc);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
conn->flags |= CONN_F_OFFLOADED;
|
||||
|
||||
{
|
||||
char os[INET_ADDRSTRLEN], od[INET_ADDRSTRLEN];
|
||||
char rs[INET_ADDRSTRLEN], rd[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, conn->orig_saddr, os, sizeof(os));
|
||||
inet_ntop(AF_INET, conn->orig_daddr, od, sizeof(od));
|
||||
inet_ntop(AF_INET, conn->rep_saddr, rs, sizeof(rs));
|
||||
inet_ntop(AF_INET, conn->rep_daddr, rd, sizeof(rd));
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"fe: ct4 offloaded %s orig=%s:%u->%s:%u "
|
||||
"reply=%s:%u->%s:%u",
|
||||
conn->proto == IPPROTO_TCP ? "TCP" : "UDP",
|
||||
os, ntohs(conn->orig_sport),
|
||||
od, ntohs(conn->orig_dport),
|
||||
rs, ntohs(conn->rep_sport),
|
||||
rd, ntohs(conn->rep_dport));
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_fe_ct4_deregister(struct cmm_global *g, struct cmm_conn *conn)
|
||||
{
|
||||
fpp_ct_cmd_t cmd;
|
||||
int rc;
|
||||
|
||||
if (!(conn->flags & CONN_F_OFFLOADED))
|
||||
return (0);
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.action = FPP_ACTION_DEREGISTER;
|
||||
cmd.protocol = conn->proto;
|
||||
|
||||
memcpy(&cmd.saddr, conn->orig_saddr, 4);
|
||||
memcpy(&cmd.daddr, conn->orig_daddr, 4);
|
||||
cmd.sport = conn->orig_sport;
|
||||
cmd.dport = conn->orig_dport;
|
||||
|
||||
memcpy(&cmd.saddr_reply, conn->rep_saddr, 4);
|
||||
memcpy(&cmd.daddr_reply, conn->rep_daddr, 4);
|
||||
cmd.sport_reply = conn->rep_sport;
|
||||
cmd.dport_reply = conn->rep_dport;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV4_CONNTRACK,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
|
||||
conn->flags &= ~CONN_F_OFFLOADED;
|
||||
|
||||
{
|
||||
char sbuf[INET_ADDRSTRLEN], dbuf[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, conn->orig_saddr, sbuf, sizeof(sbuf));
|
||||
inet_ntop(AF_INET, conn->orig_daddr, dbuf, sizeof(dbuf));
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"fe: ct4 removed %s %s:%u -> %s:%u (rc=%d)",
|
||||
conn->proto == IPPROTO_TCP ? "TCP" : "UDP",
|
||||
sbuf, ntohs(conn->orig_sport),
|
||||
dbuf, ntohs(conn->orig_dport), rc);
|
||||
}
|
||||
|
||||
return (rc == 0 ? 0 : -1);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_fe_ct6_register(struct cmm_global *g, struct cmm_conn *conn)
|
||||
{
|
||||
fpp_ct6_cmd_t cmd;
|
||||
int rc;
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.action = FPP_ACTION_REGISTER;
|
||||
cmd.protocol = conn->proto;
|
||||
|
||||
memcpy(cmd.saddr, conn->orig_saddr, 16);
|
||||
memcpy(cmd.daddr, conn->orig_daddr, 16);
|
||||
cmd.sport = conn->orig_sport;
|
||||
cmd.dport = conn->orig_dport;
|
||||
|
||||
memcpy(cmd.saddr_reply, conn->rep_saddr, 16);
|
||||
memcpy(cmd.daddr_reply, conn->rep_daddr, 16);
|
||||
cmd.sport_reply = conn->rep_sport;
|
||||
cmd.dport_reply = conn->rep_dport;
|
||||
|
||||
if (conn->orig_route != NULL)
|
||||
cmd.route_id = conn->orig_route->fpp_id;
|
||||
if (conn->rep_route != NULL)
|
||||
cmd.route_id_reply = conn->rep_route->fpp_id;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV6_CONNTRACK,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
if (rc == FPP_ERR_CT_ENTRY_ALREADY_REGISTERED) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"fe: ct6 already exists, replacing");
|
||||
cmd.action = FPP_ACTION_DEREGISTER;
|
||||
fci_write(g->fci_handle, FPP_CMD_IPV6_CONNTRACK,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
cmd.action = FPP_ACTION_REGISTER;
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV6_CONNTRACK,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
}
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_WARN, "fe: ct6 register failed: %d", rc);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
conn->flags |= CONN_F_OFFLOADED;
|
||||
cmm_print(CMM_LOG_INFO, "fe: ct6 offloaded %s",
|
||||
conn->proto == IPPROTO_TCP ? "TCP" : "UDP");
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_fe_ct6_deregister(struct cmm_global *g, struct cmm_conn *conn)
|
||||
{
|
||||
fpp_ct6_cmd_t cmd;
|
||||
int rc;
|
||||
|
||||
if (!(conn->flags & CONN_F_OFFLOADED))
|
||||
return (0);
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.action = FPP_ACTION_DEREGISTER;
|
||||
cmd.protocol = conn->proto;
|
||||
|
||||
memcpy(cmd.saddr, conn->orig_saddr, 16);
|
||||
memcpy(cmd.daddr, conn->orig_daddr, 16);
|
||||
cmd.sport = conn->orig_sport;
|
||||
cmd.dport = conn->orig_dport;
|
||||
|
||||
memcpy(cmd.saddr_reply, conn->rep_saddr, 16);
|
||||
memcpy(cmd.daddr_reply, conn->rep_daddr, 16);
|
||||
cmd.sport_reply = conn->rep_sport;
|
||||
cmd.dport_reply = conn->rep_dport;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV6_CONNTRACK,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
|
||||
conn->flags &= ~CONN_F_OFFLOADED;
|
||||
cmm_print(CMM_LOG_INFO, "fe: ct6 removed (rc=%d)", rc);
|
||||
|
||||
return (rc == 0 ? 0 : -1);
|
||||
}
|
||||
31
fastpath/cmm/cmm_fe.h
Normal file
31
fastpath/cmm/cmm_fe.h
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* cmm_fe.h — Forward engine: programs CDX hash tables via FCI
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_FE_H
|
||||
#define CMM_FE_H
|
||||
|
||||
#include "cmm.h"
|
||||
|
||||
struct cmm_conn;
|
||||
struct cmm_route;
|
||||
|
||||
/* Reset all CDX conntrack and route tables */
|
||||
int cmm_fe_reset(struct cmm_global *g);
|
||||
|
||||
/* Register/deregister IPv4 conntrack entry */
|
||||
int cmm_fe_ct4_register(struct cmm_global *g, struct cmm_conn *conn);
|
||||
int cmm_fe_ct4_deregister(struct cmm_global *g, struct cmm_conn *conn);
|
||||
|
||||
/* Register/deregister IPv6 conntrack entry */
|
||||
int cmm_fe_ct6_register(struct cmm_global *g, struct cmm_conn *conn);
|
||||
int cmm_fe_ct6_deregister(struct cmm_global *g, struct cmm_conn *conn);
|
||||
|
||||
/* Register/deregister route in CDX */
|
||||
int cmm_fe_route_register(struct cmm_global *g, struct cmm_route *rt);
|
||||
int cmm_fe_route_deregister(struct cmm_global *g, struct cmm_route *rt);
|
||||
|
||||
#endif /* CMM_FE_H */
|
||||
685
fastpath/cmm/cmm_ipsec.c
Normal file
685
fastpath/cmm/cmm_ipsec.c
Normal file
|
|
@ -0,0 +1,685 @@
|
|||
/*
|
||||
* cmm_ipsec.c -- IPsec SA management via PF_KEY
|
||||
*
|
||||
* Monitors PF_KEY socket for SA events from setkey(8) / strongswan,
|
||||
* translates them into FCI commands for CDX hardware offload.
|
||||
*
|
||||
* Flow: setkey(8) -> PF_KEY socket -> CMM -> fci_write() -> CDX -> CAAM HW
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <net/pfkeyv2.h>
|
||||
#include <netipsec/ipsec.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_ipsec.h"
|
||||
#include "cmm_itf.h"
|
||||
|
||||
/* SA hash table */
|
||||
static struct list_head sa_hash[CMM_SA_HASH_SIZE];
|
||||
static uint16_t next_sagd = 1;
|
||||
|
||||
static uint32_t
|
||||
sa_hash_fn(uint32_t spi, const uint8_t *dst, sa_family_t af)
|
||||
{
|
||||
uint32_t h;
|
||||
|
||||
h = spi;
|
||||
if (af == AF_INET)
|
||||
h ^= *(const uint32_t *)dst;
|
||||
else
|
||||
h ^= *(const uint32_t *)dst ^
|
||||
*(const uint32_t *)(dst + 4) ^
|
||||
*(const uint32_t *)(dst + 8) ^
|
||||
*(const uint32_t *)(dst + 12);
|
||||
return (h & (CMM_SA_HASH_SIZE - 1));
|
||||
}
|
||||
|
||||
static struct cmm_sa_entry *
|
||||
sa_find(uint32_t spi, sa_family_t af, const uint8_t *dst)
|
||||
{
|
||||
uint32_t bucket;
|
||||
struct list_head *pos, *head;
|
||||
|
||||
bucket = sa_hash_fn(spi, dst, af);
|
||||
head = &sa_hash[bucket];
|
||||
for (pos = list_first(head); pos != head; pos = list_next(pos)) {
|
||||
struct cmm_sa_entry *sa;
|
||||
|
||||
sa = container_of(pos, struct cmm_sa_entry, list);
|
||||
if (sa->spi == spi && sa->af == af &&
|
||||
memcmp(sa->dst_addr, dst,
|
||||
af == AF_INET ? 4 : 16) == 0)
|
||||
return (sa);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static int
|
||||
sa_sagd_in_use(uint16_t sagd)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CMM_SA_HASH_SIZE; i++) {
|
||||
struct list_head *pos, *head;
|
||||
|
||||
head = &sa_hash[i];
|
||||
for (pos = list_first(head); pos != head;
|
||||
pos = list_next(pos)) {
|
||||
struct cmm_sa_entry *sa;
|
||||
|
||||
sa = container_of(pos, struct cmm_sa_entry, list);
|
||||
if (sa->sagd == sagd)
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static uint16_t
|
||||
sa_alloc_sagd(void)
|
||||
{
|
||||
uint16_t sagd;
|
||||
int tries;
|
||||
|
||||
for (tries = 0; tries < 65534; tries++) {
|
||||
sagd = next_sagd++;
|
||||
if (next_sagd == 0 || next_sagd == 0xFFFF)
|
||||
next_sagd = 1;
|
||||
if (!sa_sagd_in_use(sagd))
|
||||
return (sagd);
|
||||
}
|
||||
return (0); /* exhausted */
|
||||
}
|
||||
|
||||
static void
|
||||
pfkey_parse_extensions(const struct sadb_msg *msg, struct pfkey_parsed *p)
|
||||
{
|
||||
const struct sadb_ext *ext;
|
||||
const uint8_t *end;
|
||||
size_t msglen;
|
||||
|
||||
memset(p, 0, sizeof(*p));
|
||||
|
||||
msglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
|
||||
end = (const uint8_t *)msg + msglen;
|
||||
ext = (const struct sadb_ext *)(msg + 1);
|
||||
|
||||
while ((const uint8_t *)ext + sizeof(*ext) <= end) {
|
||||
size_t extlen;
|
||||
|
||||
extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
|
||||
if (extlen < sizeof(*ext) ||
|
||||
(const uint8_t *)ext + extlen > end)
|
||||
break;
|
||||
|
||||
switch (ext->sadb_ext_type) {
|
||||
case SADB_EXT_SA:
|
||||
p->sa = (const struct sadb_sa *)ext;
|
||||
break;
|
||||
case SADB_EXT_ADDRESS_SRC:
|
||||
p->addr_src = (const struct sadb_address *)ext;
|
||||
break;
|
||||
case SADB_EXT_ADDRESS_DST:
|
||||
p->addr_dst = (const struct sadb_address *)ext;
|
||||
break;
|
||||
case SADB_EXT_KEY_AUTH:
|
||||
p->key_auth = (const struct sadb_key *)ext;
|
||||
break;
|
||||
case SADB_EXT_KEY_ENCRYPT:
|
||||
p->key_enc = (const struct sadb_key *)ext;
|
||||
break;
|
||||
case SADB_X_EXT_SA2:
|
||||
p->sa2 = (const struct sadb_x_sa2 *)ext;
|
||||
break;
|
||||
}
|
||||
|
||||
ext = (const struct sadb_ext *)
|
||||
((const uint8_t *)ext + extlen);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Extract sockaddr from a PF_KEY address extension.
|
||||
*/
|
||||
static const struct sockaddr *
|
||||
pfkey_addr_sa(const struct sadb_address *addr)
|
||||
{
|
||||
|
||||
return ((const struct sockaddr *)(addr + 1));
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy address bytes (4 for IPv4, 16 for IPv6) from a sockaddr.
|
||||
*/
|
||||
static void
|
||||
pfkey_copy_addr(const struct sockaddr *sa, uint8_t *out)
|
||||
{
|
||||
|
||||
if (sa->sa_family == AF_INET) {
|
||||
const struct sockaddr_in *sin =
|
||||
(const struct sockaddr_in *)sa;
|
||||
memcpy(out, &sin->sin_addr, 4);
|
||||
} else if (sa->sa_family == AF_INET6) {
|
||||
const struct sockaddr_in6 *sin6 =
|
||||
(const struct sockaddr_in6 *)sa;
|
||||
memcpy(out, &sin6->sin6_addr, 16);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy address bytes into a uint32_t[4] array for FCI command structures.
|
||||
* IPv4: first element only, rest zero.
|
||||
*/
|
||||
static void
|
||||
pfkey_copy_addr_u32(const struct sockaddr *sa, unsigned int *out)
|
||||
{
|
||||
|
||||
memset(out, 0, 16);
|
||||
if (sa->sa_family == AF_INET) {
|
||||
const struct sockaddr_in *sin =
|
||||
(const struct sockaddr_in *)sa;
|
||||
memcpy(&out[0], &sin->sin_addr, 4);
|
||||
} else if (sa->sa_family == AF_INET6) {
|
||||
const struct sockaddr_in6 *sin6 =
|
||||
(const struct sockaddr_in6 *)sa;
|
||||
memcpy(out, &sin6->sin6_addr, 16);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
handle_sadb_add(struct cmm_global *g, const struct sadb_msg *msg)
|
||||
{
|
||||
struct pfkey_parsed p;
|
||||
const struct sockaddr *sa_src, *sa_dst;
|
||||
struct cmm_sa_entry *sa;
|
||||
CommandIPSecCreateSA cmd_create;
|
||||
CommandIPSecSetKey cmd_keys;
|
||||
CommandIPSecSetTunnel cmd_tunnel;
|
||||
CommandIPSecSetState cmd_state;
|
||||
uint16_t sagd;
|
||||
uint32_t spi;
|
||||
uint8_t dst_addr[16];
|
||||
sa_family_t af;
|
||||
int is_inbound, is_tunnel;
|
||||
int rc;
|
||||
|
||||
pfkey_parse_extensions(msg, &p);
|
||||
|
||||
/* Validate required extensions */
|
||||
if (p.sa == NULL || p.addr_src == NULL || p.addr_dst == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"ipsec: SADB_ADD missing required extensions");
|
||||
return;
|
||||
}
|
||||
if (p.key_enc == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"ipsec: SADB_ADD missing encryption key");
|
||||
return;
|
||||
}
|
||||
|
||||
sa_src = pfkey_addr_sa(p.addr_src);
|
||||
sa_dst = pfkey_addr_sa(p.addr_dst);
|
||||
af = sa_dst->sa_family;
|
||||
spi = p.sa->sadb_sa_spi; /* network byte order */
|
||||
|
||||
memset(dst_addr, 0, sizeof(dst_addr));
|
||||
pfkey_copy_addr(sa_dst, dst_addr);
|
||||
|
||||
/* Check for existing SA */
|
||||
sa = sa_find(spi, af, dst_addr);
|
||||
if (sa != NULL) {
|
||||
if (msg->sadb_msg_type != SADB_UPDATE) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"ipsec: SA SPI=0x%x already tracked, ignoring",
|
||||
ntohl(spi));
|
||||
return;
|
||||
}
|
||||
/* Rekey: delete old SA from CDX, then recreate with new keys */
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"ipsec: rekey SPI=0x%x, deleting sagd=0x%04x",
|
||||
ntohl(spi), sa->sagd);
|
||||
if (sa->offloaded) {
|
||||
CommandIPSecDeleteSA cmd_del;
|
||||
memset(&cmd_del, 0, sizeof(cmd_del));
|
||||
cmd_del.sagd = sa->sagd;
|
||||
fci_write(g->fci_handle, FPP_CMD_IPSEC_SA_DELETE,
|
||||
sizeof(cmd_del), (unsigned short *)&cmd_del);
|
||||
}
|
||||
list_del(&sa->list);
|
||||
free(sa);
|
||||
}
|
||||
|
||||
/* Allocate SAGD */
|
||||
sagd = sa_alloc_sagd();
|
||||
if (sagd == 0) {
|
||||
cmm_print(CMM_LOG_ERR, "ipsec: SAGD space exhausted");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Direction: if dst is one of our addresses, it's inbound */
|
||||
is_inbound = cmm_itf_is_local_addr(af,
|
||||
af == AF_INET ?
|
||||
(const void *)&((const struct sockaddr_in *)sa_dst)->sin_addr :
|
||||
(const void *)&((const struct sockaddr_in6 *)sa_dst)->sin6_addr);
|
||||
|
||||
/* Tunnel mode? */
|
||||
is_tunnel = (p.sa2 != NULL &&
|
||||
p.sa2->sadb_x_sa2_mode == IPSEC_MODE_TUNNEL);
|
||||
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"ipsec: SADB_ADD ESP SPI=0x%x sagd=0x%04x %s %s",
|
||||
ntohl(spi), sagd,
|
||||
is_inbound ? "inbound" : "outbound",
|
||||
is_tunnel ? "tunnel" : "transport");
|
||||
|
||||
/*
|
||||
* FCI 1: SA_ADD — create the SA in CDX
|
||||
*/
|
||||
memset(&cmd_create, 0, sizeof(cmd_create));
|
||||
cmd_create.sagd = sagd;
|
||||
cmd_create.said.spi = spi;
|
||||
cmd_create.said.sa_type = 0; /* ESP */
|
||||
cmd_create.said.proto_family =
|
||||
(af == AF_INET) ? PROTO_FAMILY_IPV4 : PROTO_FAMILY_IPV6;
|
||||
cmd_create.said.replay_window = p.sa->sadb_sa_replay;
|
||||
cmd_create.said.flags = is_inbound ? NLKEY_SAFLAGS_INBOUND : 0;
|
||||
pfkey_copy_addr_u32(sa_dst, cmd_create.said.dst_ip);
|
||||
pfkey_copy_addr_u32(sa_src, cmd_create.said.src_ip);
|
||||
cmd_create.said.mtu = 1500;
|
||||
cmd_create.said.dev_mtu = 1500;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPSEC_SA_ADD,
|
||||
sizeof(cmd_create), (unsigned short *)&cmd_create);
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"ipsec: SA_ADD failed: %d (SPI=0x%x sagd=0x%04x)",
|
||||
rc, ntohl(spi), sagd);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* FCI 2: SA_SET_KEYS
|
||||
*/
|
||||
memset(&cmd_keys, 0, sizeof(cmd_keys));
|
||||
cmd_keys.sagd = sagd;
|
||||
|
||||
if (p.key_auth != NULL && p.key_auth->sadb_key_bits > 0) {
|
||||
size_t auth_len, enc_len;
|
||||
|
||||
/* CBC + HMAC: 2 keys (auth + cipher) */
|
||||
cmd_keys.num_keys = 2;
|
||||
|
||||
auth_len = (p.key_auth->sadb_key_bits + 7) / 8;
|
||||
if (auth_len > IPSEC_MAX_KEY_SIZE) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"ipsec: auth key too large (%zu > %d)",
|
||||
auth_len, IPSEC_MAX_KEY_SIZE);
|
||||
goto fail_delete;
|
||||
}
|
||||
|
||||
enc_len = (p.key_enc->sadb_key_bits + 7) / 8;
|
||||
if (enc_len > IPSEC_MAX_KEY_SIZE) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"ipsec: cipher key too large (%zu > %d)",
|
||||
enc_len, IPSEC_MAX_KEY_SIZE);
|
||||
goto fail_delete;
|
||||
}
|
||||
|
||||
/* Key 0: auth key */
|
||||
cmd_keys.keys[0].key_bits = p.key_auth->sadb_key_bits;
|
||||
cmd_keys.keys[0].key_alg = p.sa->sadb_sa_auth;
|
||||
cmd_keys.keys[0].key_type = 1; /* auth */
|
||||
memcpy(cmd_keys.keys[0].key,
|
||||
(const uint8_t *)(p.key_auth + 1), auth_len);
|
||||
|
||||
/* Key 1: cipher key */
|
||||
cmd_keys.keys[1].key_bits = p.key_enc->sadb_key_bits;
|
||||
cmd_keys.keys[1].key_alg = p.sa->sadb_sa_encrypt;
|
||||
cmd_keys.keys[1].key_type = 0; /* cipher */
|
||||
memcpy(cmd_keys.keys[1].key,
|
||||
(const uint8_t *)(p.key_enc + 1), enc_len);
|
||||
} else {
|
||||
size_t enc_len;
|
||||
|
||||
/* GCM: 1 key (cipher only, includes 4-byte salt) */
|
||||
enc_len = (p.key_enc->sadb_key_bits + 7) / 8;
|
||||
if (enc_len > IPSEC_MAX_KEY_SIZE) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"ipsec: cipher key too large (%zu > %d)",
|
||||
enc_len, IPSEC_MAX_KEY_SIZE);
|
||||
goto fail_delete;
|
||||
}
|
||||
|
||||
cmd_keys.num_keys = 1;
|
||||
cmd_keys.keys[0].key_bits = p.key_enc->sadb_key_bits;
|
||||
cmd_keys.keys[0].key_alg = p.sa->sadb_sa_encrypt;
|
||||
cmd_keys.keys[0].key_type = 0; /* cipher */
|
||||
memcpy(cmd_keys.keys[0].key,
|
||||
(const uint8_t *)(p.key_enc + 1), enc_len);
|
||||
}
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPSEC_SA_SET_KEYS,
|
||||
sizeof(cmd_keys), (unsigned short *)&cmd_keys);
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"ipsec: SA_SET_KEYS failed: %d (sagd=0x%04x)",
|
||||
rc, sagd);
|
||||
goto fail_delete;
|
||||
}
|
||||
|
||||
/*
|
||||
* FCI 3: SA_SET_TUNNEL (if tunnel mode)
|
||||
*/
|
||||
if (is_tunnel) {
|
||||
memset(&cmd_tunnel, 0, sizeof(cmd_tunnel));
|
||||
cmd_tunnel.sagd = sagd;
|
||||
cmd_tunnel.proto_family =
|
||||
(af == AF_INET) ? PROTO_FAMILY_IPV4 : PROTO_FAMILY_IPV6;
|
||||
|
||||
if (af == AF_INET) {
|
||||
const struct sockaddr_in *sin_src =
|
||||
(const struct sockaddr_in *)sa_src;
|
||||
const struct sockaddr_in *sin_dst =
|
||||
(const struct sockaddr_in *)sa_dst;
|
||||
|
||||
cmd_tunnel.h.ipv4h.Version_IHL = 0x45;
|
||||
cmd_tunnel.h.ipv4h.TTL = 64;
|
||||
cmd_tunnel.h.ipv4h.Protocol = 50; /* ESP */
|
||||
memcpy(&cmd_tunnel.h.ipv4h.SourceAddress,
|
||||
&sin_src->sin_addr, 4);
|
||||
memcpy(&cmd_tunnel.h.ipv4h.DestinationAddress,
|
||||
&sin_dst->sin_addr, 4);
|
||||
} else {
|
||||
const struct sockaddr_in6 *sin6_src =
|
||||
(const struct sockaddr_in6 *)sa_src;
|
||||
const struct sockaddr_in6 *sin6_dst =
|
||||
(const struct sockaddr_in6 *)sa_dst;
|
||||
|
||||
cmd_tunnel.h.ipv6h.Version_TC_FLHi = htons(0x6000);
|
||||
cmd_tunnel.h.ipv6h.NextHeader = 50; /* ESP */
|
||||
cmd_tunnel.h.ipv6h.HopLimit = 64;
|
||||
memcpy(cmd_tunnel.h.ipv6h.SourceAddress,
|
||||
&sin6_src->sin6_addr, 16);
|
||||
memcpy(cmd_tunnel.h.ipv6h.DestinationAddress,
|
||||
&sin6_dst->sin6_addr, 16);
|
||||
}
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPSEC_SA_SET_TUNNEL,
|
||||
sizeof(cmd_tunnel), (unsigned short *)&cmd_tunnel);
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"ipsec: SA_SET_TUNNEL failed: %d (sagd=0x%04x)",
|
||||
rc, sagd);
|
||||
goto fail_delete;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* FCI 4: SA_SET_STATE -> VALID
|
||||
*/
|
||||
memset(&cmd_state, 0, sizeof(cmd_state));
|
||||
cmd_state.sagd = sagd;
|
||||
cmd_state.state = SA_STATE_VALID;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPSEC_SA_SET_STATE,
|
||||
sizeof(cmd_state), (unsigned short *)&cmd_state);
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"ipsec: SA_SET_STATE failed: %d (sagd=0x%04x)",
|
||||
rc, sagd);
|
||||
goto fail_delete;
|
||||
}
|
||||
|
||||
/* Add to local SA table */
|
||||
sa = malloc(sizeof(*sa));
|
||||
if (sa == NULL) {
|
||||
cmm_print(CMM_LOG_ERR, "ipsec: malloc failed");
|
||||
goto fail_delete;
|
||||
}
|
||||
memset(sa, 0, sizeof(*sa));
|
||||
sa->sagd = sagd;
|
||||
sa->spi = spi;
|
||||
sa->af = af;
|
||||
memcpy(sa->dst_addr, dst_addr, sizeof(sa->dst_addr));
|
||||
sa->offloaded = 1;
|
||||
|
||||
{
|
||||
uint32_t bucket = sa_hash_fn(spi, dst_addr, af);
|
||||
list_add(&sa_hash[bucket], &sa->list);
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"ipsec: SA offloaded SPI=0x%x sagd=0x%04x",
|
||||
ntohl(spi), sagd);
|
||||
explicit_bzero(&cmd_keys, sizeof(cmd_keys));
|
||||
return;
|
||||
|
||||
fail_delete:
|
||||
/* Roll back: delete the SA we partially created */
|
||||
{
|
||||
CommandIPSecDeleteSA cmd_del;
|
||||
memset(&cmd_del, 0, sizeof(cmd_del));
|
||||
cmd_del.sagd = sagd;
|
||||
fci_write(g->fci_handle, FPP_CMD_IPSEC_SA_DELETE,
|
||||
sizeof(cmd_del), (unsigned short *)&cmd_del);
|
||||
}
|
||||
explicit_bzero(&cmd_keys, sizeof(cmd_keys));
|
||||
}
|
||||
|
||||
static void
|
||||
handle_sadb_delete(struct cmm_global *g, const struct sadb_msg *msg)
|
||||
{
|
||||
struct pfkey_parsed p;
|
||||
const struct sockaddr *sa_dst;
|
||||
struct cmm_sa_entry *sa;
|
||||
uint8_t dst_addr[16];
|
||||
sa_family_t af;
|
||||
uint32_t spi;
|
||||
int rc;
|
||||
|
||||
pfkey_parse_extensions(msg, &p);
|
||||
|
||||
if (p.sa == NULL || p.addr_dst == NULL) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"ipsec: SADB_DELETE missing SA or DST");
|
||||
return;
|
||||
}
|
||||
|
||||
sa_dst = pfkey_addr_sa(p.addr_dst);
|
||||
af = sa_dst->sa_family;
|
||||
spi = p.sa->sadb_sa_spi;
|
||||
|
||||
memset(dst_addr, 0, sizeof(dst_addr));
|
||||
pfkey_copy_addr(sa_dst, dst_addr);
|
||||
|
||||
sa = sa_find(spi, af, dst_addr);
|
||||
if (sa == NULL) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"ipsec: SADB_DELETE SPI=0x%x not tracked",
|
||||
ntohl(spi));
|
||||
return;
|
||||
}
|
||||
|
||||
if (sa->offloaded) {
|
||||
CommandIPSecDeleteSA cmd_del;
|
||||
|
||||
memset(&cmd_del, 0, sizeof(cmd_del));
|
||||
cmd_del.sagd = sa->sagd;
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPSEC_SA_DELETE,
|
||||
sizeof(cmd_del), (unsigned short *)&cmd_del);
|
||||
if (rc != 0)
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"ipsec: SA_DELETE failed: %d (sagd=0x%04x)",
|
||||
rc, sa->sagd);
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"ipsec: SA removed SPI=0x%x sagd=0x%04x",
|
||||
ntohl(spi), sa->sagd);
|
||||
|
||||
list_del(&sa->list);
|
||||
free(sa);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_pfkey_open(void)
|
||||
{
|
||||
struct sadb_msg msg;
|
||||
int fd, flags;
|
||||
ssize_t n;
|
||||
|
||||
fd = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
|
||||
if (fd < 0) {
|
||||
cmm_print(CMM_LOG_ERR, "ipsec: PF_KEY socket: %s",
|
||||
strerror(errno));
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Set non-blocking */
|
||||
flags = fcntl(fd, F_GETFL, 0);
|
||||
if (flags >= 0)
|
||||
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
|
||||
/* Register for ESP SA events */
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
msg.sadb_msg_version = PF_KEY_V2;
|
||||
msg.sadb_msg_type = SADB_REGISTER;
|
||||
msg.sadb_msg_satype = SADB_SATYPE_ESP;
|
||||
msg.sadb_msg_len = PFKEY_UNIT64(sizeof(msg));
|
||||
msg.sadb_msg_pid = getpid();
|
||||
|
||||
if (write(fd, &msg, sizeof(msg)) < 0) {
|
||||
cmm_print(CMM_LOG_WARN, "ipsec: SADB_REGISTER: %s",
|
||||
strerror(errno));
|
||||
/* Non-fatal — we can still receive SA events */
|
||||
}
|
||||
|
||||
/* Drain the register reply */
|
||||
{
|
||||
uint8_t buf[4096];
|
||||
while ((n = read(fd, buf, sizeof(buf))) > 0)
|
||||
;
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "ipsec: PF_KEY socket opened (fd=%d)", fd);
|
||||
return (fd);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_pfkey_dispatch(struct cmm_global *g)
|
||||
{
|
||||
uint8_t buf[4096];
|
||||
ssize_t n;
|
||||
const struct sadb_msg *msg;
|
||||
|
||||
while ((n = read(g->pfkey_fd, buf, sizeof(buf))) > 0) {
|
||||
if ((size_t)n < sizeof(struct sadb_msg))
|
||||
continue;
|
||||
|
||||
msg = (const struct sadb_msg *)buf;
|
||||
|
||||
/* Ignore our own messages */
|
||||
if (msg->sadb_msg_pid == (uint32_t)getpid())
|
||||
continue;
|
||||
|
||||
/* Only handle ESP */
|
||||
if (msg->sadb_msg_satype != SADB_SATYPE_ESP)
|
||||
continue;
|
||||
|
||||
/* Check message length consistency */
|
||||
if ((size_t)PFKEY_UNUNIT64(msg->sadb_msg_len) > (size_t)n)
|
||||
continue;
|
||||
|
||||
switch (msg->sadb_msg_type) {
|
||||
case SADB_ADD:
|
||||
case SADB_UPDATE:
|
||||
handle_sadb_add(g, msg);
|
||||
break;
|
||||
case SADB_DELETE:
|
||||
handle_sadb_delete(g, msg);
|
||||
break;
|
||||
case SADB_EXPIRE:
|
||||
/* Treat hard expire as delete */
|
||||
if (msg->sadb_msg_errno == 0)
|
||||
handle_sadb_delete(g, msg);
|
||||
break;
|
||||
default:
|
||||
cmm_print(CMM_LOG_TRACE,
|
||||
"ipsec: PF_KEY msg type=%d ignored",
|
||||
msg->sadb_msg_type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
explicit_bzero(buf, sizeof(buf));
|
||||
}
|
||||
|
||||
int
|
||||
cmm_ipsec_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CMM_SA_HASH_SIZE; i++)
|
||||
list_head_init(&sa_hash[i]);
|
||||
|
||||
next_sagd = 1;
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG, "ipsec: SA table initialized (%d buckets)",
|
||||
CMM_SA_HASH_SIZE);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_sa_flush_all(struct cmm_global *g)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CMM_SA_HASH_SIZE; i++) {
|
||||
struct list_head *pos, *tmp, *head;
|
||||
|
||||
head = &sa_hash[i];
|
||||
pos = list_first(head);
|
||||
while (pos != head) {
|
||||
struct cmm_sa_entry *sa;
|
||||
|
||||
tmp = list_next(pos);
|
||||
sa = container_of(pos, struct cmm_sa_entry, list);
|
||||
|
||||
if (sa->offloaded && g->fci_handle != NULL) {
|
||||
CommandIPSecDeleteSA cmd_del;
|
||||
|
||||
memset(&cmd_del, 0, sizeof(cmd_del));
|
||||
cmd_del.sagd = sa->sagd;
|
||||
fci_write(g->fci_handle,
|
||||
FPP_CMD_IPSEC_SA_DELETE,
|
||||
sizeof(cmd_del),
|
||||
(unsigned short *)&cmd_del);
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"ipsec: flushed sagd=0x%04x",
|
||||
sa->sagd);
|
||||
}
|
||||
list_del(&sa->list);
|
||||
free(sa);
|
||||
pos = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cmm_ipsec_fini(struct cmm_global *g __unused)
|
||||
{
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG, "ipsec: subsystem finalized");
|
||||
}
|
||||
153
fastpath/cmm/cmm_ipsec.h
Normal file
153
fastpath/cmm/cmm_ipsec.h
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
* cmm_ipsec.h -- IPsec SA management via PF_KEY
|
||||
*
|
||||
* Monitors PF_KEY socket for SA events (add/delete/expire),
|
||||
* translates them into FCI commands for CDX hardware offload.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_IPSEC_H
|
||||
#define CMM_IPSEC_H
|
||||
|
||||
#include "cmm.h"
|
||||
#include <net/pfkeyv2.h>
|
||||
|
||||
/* SA state constants (must match CDX control_ipsec.c) */
|
||||
#define SA_STATE_INIT 0x1
|
||||
#define SA_STATE_VALID 0x2
|
||||
#define SA_STATE_DEAD 0x3
|
||||
|
||||
/* SA flags */
|
||||
#define NLKEY_SAFLAGS_ESN 0x1
|
||||
#define NLKEY_SAFLAGS_INBOUND 0x2
|
||||
|
||||
/* Protocol family (must match CDX fpp.h / keytrack.h) */
|
||||
#define PROTO_FAMILY_IPV4 2
|
||||
#define PROTO_FAMILY_IPV6 10
|
||||
|
||||
/* Limits */
|
||||
#define IPSEC_MAX_KEY_SIZE 64 /* must match CDX control_ipsec.h (512/8) */
|
||||
#define IPSEC_MAX_NUM_KEYS 2
|
||||
|
||||
/* SA hash table */
|
||||
#define CMM_SA_HASH_BITS 5
|
||||
#define CMM_SA_HASH_SIZE (1 << CMM_SA_HASH_BITS)
|
||||
|
||||
/*
|
||||
* FCI command payload structures.
|
||||
* Binary layout must match CDX control_ipsec.c expectations exactly.
|
||||
* Copied from ASK/cmm-17.03.1/src/module_ipsec.h.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
unsigned char Version_IHL;
|
||||
unsigned char TypeOfService;
|
||||
unsigned short TotalLength;
|
||||
unsigned short Identification;
|
||||
unsigned short Flags_FragmentOffset;
|
||||
unsigned char TTL;
|
||||
unsigned char Protocol;
|
||||
unsigned short HeaderChksum;
|
||||
unsigned int SourceAddress;
|
||||
unsigned int DestinationAddress;
|
||||
} ipv4_hdr_t;
|
||||
|
||||
typedef struct {
|
||||
unsigned short Version_TC_FLHi;
|
||||
unsigned short FlowLabelLo;
|
||||
unsigned short TotalLength;
|
||||
unsigned char NextHeader;
|
||||
unsigned char HopLimit;
|
||||
unsigned int SourceAddress[4];
|
||||
unsigned int DestinationAddress[4];
|
||||
} ipv6_hdr_t;
|
||||
|
||||
typedef struct {
|
||||
unsigned int spi;
|
||||
unsigned char sa_type;
|
||||
unsigned char proto_family;
|
||||
unsigned char replay_window;
|
||||
unsigned char flags;
|
||||
unsigned int dst_ip[4];
|
||||
unsigned int src_ip[4];
|
||||
unsigned short mtu;
|
||||
unsigned short dev_mtu;
|
||||
} IPSec_said;
|
||||
|
||||
typedef struct {
|
||||
unsigned short key_bits;
|
||||
unsigned char key_alg;
|
||||
unsigned char key_type;
|
||||
unsigned char key[IPSEC_MAX_KEY_SIZE];
|
||||
} IPSec_key_desc;
|
||||
|
||||
typedef struct {
|
||||
unsigned short sagd;
|
||||
unsigned short rsvd;
|
||||
IPSec_said said;
|
||||
} CommandIPSecCreateSA;
|
||||
|
||||
typedef struct {
|
||||
unsigned short sagd;
|
||||
unsigned short rsvd;
|
||||
} CommandIPSecDeleteSA;
|
||||
|
||||
typedef struct {
|
||||
unsigned short sagd;
|
||||
unsigned short rsvd;
|
||||
unsigned short num_keys;
|
||||
unsigned short rsvd2;
|
||||
IPSec_key_desc keys[IPSEC_MAX_NUM_KEYS];
|
||||
} CommandIPSecSetKey;
|
||||
|
||||
typedef struct {
|
||||
unsigned short sagd;
|
||||
unsigned char rsvd;
|
||||
unsigned char proto_family;
|
||||
union {
|
||||
ipv4_hdr_t ipv4h;
|
||||
ipv6_hdr_t ipv6h;
|
||||
} h;
|
||||
} CommandIPSecSetTunnel;
|
||||
|
||||
typedef struct {
|
||||
unsigned short sagd;
|
||||
unsigned short parent_sa_sagd;
|
||||
unsigned short state;
|
||||
unsigned short rsvd2;
|
||||
} CommandIPSecSetState;
|
||||
|
||||
/* Local SA entry */
|
||||
struct cmm_sa_entry {
|
||||
struct list_head list;
|
||||
uint16_t sagd;
|
||||
uint32_t spi; /* network byte order */
|
||||
sa_family_t af;
|
||||
uint8_t dst_addr[16]; /* IPv4 in first 4 bytes */
|
||||
int offloaded;
|
||||
};
|
||||
|
||||
/* PF_KEY parsed extensions */
|
||||
struct pfkey_parsed {
|
||||
const struct sadb_sa *sa;
|
||||
const struct sadb_address *addr_src;
|
||||
const struct sadb_address *addr_dst;
|
||||
const struct sadb_key *key_auth;
|
||||
const struct sadb_key *key_enc;
|
||||
const struct sadb_x_sa2 *sa2;
|
||||
};
|
||||
|
||||
/* Subsystem init/fini */
|
||||
int cmm_ipsec_init(void);
|
||||
void cmm_ipsec_fini(struct cmm_global *g);
|
||||
|
||||
/* PF_KEY socket */
|
||||
int cmm_pfkey_open(void);
|
||||
void cmm_pfkey_dispatch(struct cmm_global *g);
|
||||
|
||||
/* Flush all offloaded SAs */
|
||||
void cmm_sa_flush_all(struct cmm_global *g);
|
||||
|
||||
#endif /* CMM_IPSEC_H */
|
||||
595
fastpath/cmm/cmm_itf.c
Normal file
595
fastpath/cmm/cmm_itf.c
Normal file
|
|
@ -0,0 +1,595 @@
|
|||
/*
|
||||
* cmm_itf.c — Interface monitoring
|
||||
*
|
||||
* Discovers and tracks network interfaces using getifaddrs(3) for
|
||||
* initial enumeration and PF_ROUTE socket for change notification.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <net/if_types.h>
|
||||
#include <net/if_vlan_var.h>
|
||||
#include <net/route.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_itf.h"
|
||||
#include "cmm_vlan.h"
|
||||
#include "cmm_tunnel.h"
|
||||
#include "cmm_bridge.h"
|
||||
|
||||
static struct list_head itf_hash[ITF_HASH_SIZE];
|
||||
|
||||
static inline unsigned int
|
||||
itf_hash_index(int ifindex)
|
||||
{
|
||||
return ((unsigned int)ifindex % ITF_HASH_SIZE);
|
||||
}
|
||||
|
||||
struct cmm_interface *
|
||||
cmm_itf_find_by_index(int ifindex)
|
||||
{
|
||||
struct list_head *bucket, *pos;
|
||||
struct cmm_interface *itf;
|
||||
unsigned int h;
|
||||
|
||||
h = itf_hash_index(ifindex);
|
||||
bucket = &itf_hash[h];
|
||||
|
||||
for (pos = list_first(bucket); pos != bucket; pos = list_next(pos)) {
|
||||
itf = container_of(pos, struct cmm_interface, entry);
|
||||
if (itf->ifindex == ifindex)
|
||||
return (itf);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
struct cmm_interface *
|
||||
cmm_itf_find_by_name(const char *name)
|
||||
{
|
||||
struct list_head *bucket, *pos;
|
||||
struct cmm_interface *itf;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ITF_HASH_SIZE; i++) {
|
||||
bucket = &itf_hash[i];
|
||||
for (pos = list_first(bucket); pos != bucket;
|
||||
pos = list_next(pos)) {
|
||||
itf = container_of(pos, struct cmm_interface, entry);
|
||||
if (strcmp(itf->ifname, name) == 0)
|
||||
return (itf);
|
||||
}
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static struct cmm_interface *
|
||||
itf_create(const char *name, int ifindex)
|
||||
{
|
||||
struct cmm_interface *itf;
|
||||
unsigned int h;
|
||||
|
||||
itf = calloc(1, sizeof(*itf));
|
||||
if (itf == NULL)
|
||||
return (NULL);
|
||||
|
||||
strlcpy(itf->ifname, name, sizeof(itf->ifname));
|
||||
itf->ifindex = ifindex;
|
||||
list_head_init(&itf->addrs);
|
||||
itf->entry.next = NULL;
|
||||
itf->entry.prev = NULL;
|
||||
|
||||
h = itf_hash_index(ifindex);
|
||||
list_add(&itf_hash[h], &itf->entry);
|
||||
|
||||
return (itf);
|
||||
}
|
||||
|
||||
static void
|
||||
itf_add_addr(struct cmm_interface *itf, sa_family_t family,
|
||||
const void *addr, uint8_t prefixlen)
|
||||
{
|
||||
struct cmm_ifaddr *ifa;
|
||||
|
||||
ifa = calloc(1, sizeof(*ifa));
|
||||
if (ifa == NULL)
|
||||
return;
|
||||
|
||||
ifa->family = family;
|
||||
if (family == AF_INET)
|
||||
memcpy(&ifa->addr.v4, addr, sizeof(struct in_addr));
|
||||
else
|
||||
memcpy(&ifa->addr.v6, addr, sizeof(struct in6_addr));
|
||||
ifa->prefixlen = prefixlen;
|
||||
ifa->entry.next = NULL;
|
||||
ifa->entry.prev = NULL;
|
||||
|
||||
list_add(&itf->addrs, &ifa->entry);
|
||||
}
|
||||
|
||||
static uint8_t
|
||||
mask_to_prefixlen(sa_family_t af, const void *mask)
|
||||
{
|
||||
const uint8_t *p;
|
||||
int len, bits;
|
||||
|
||||
len = (af == AF_INET) ? 4 : 16;
|
||||
p = mask;
|
||||
bits = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (p[i] == 0xff) {
|
||||
bits += 8;
|
||||
} else {
|
||||
uint8_t b = p[i];
|
||||
while (b & 0x80) {
|
||||
bits++;
|
||||
b <<= 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ((uint8_t)bits);
|
||||
}
|
||||
|
||||
/*
|
||||
* Probe an interface for 802.1Q VLAN membership using SIOCGETVLAN.
|
||||
* If successful, populates vlan_id, parent_ifindex, and sets ITF_F_VLAN.
|
||||
*/
|
||||
static void
|
||||
itf_detect_vlan(struct cmm_interface *itf, int sd)
|
||||
{
|
||||
struct ifreq ifr;
|
||||
struct vlanreq vlr;
|
||||
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
memset(&vlr, 0, sizeof(vlr));
|
||||
strlcpy(ifr.ifr_name, itf->ifname, sizeof(ifr.ifr_name));
|
||||
ifr.ifr_data = (caddr_t)&vlr;
|
||||
|
||||
if (ioctl(sd, SIOCGETVLAN, &ifr) < 0)
|
||||
return; /* Not a VLAN interface */
|
||||
if (vlr.vlr_tag == 0)
|
||||
return;
|
||||
|
||||
itf->vlan_id = vlr.vlr_tag;
|
||||
itf->parent_ifindex = if_nametoindex(vlr.vlr_parent);
|
||||
itf->itf_flags |= ITF_F_VLAN;
|
||||
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"itf: %s is VLAN %u on %s (parent idx=%d)",
|
||||
itf->ifname, itf->vlan_id, vlr.vlr_parent,
|
||||
itf->parent_ifindex);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_itf_init(void)
|
||||
{
|
||||
struct ifaddrs *ifap, *ifa;
|
||||
struct cmm_interface *itf;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ITF_HASH_SIZE; i++)
|
||||
list_head_init(&itf_hash[i]);
|
||||
|
||||
if (getifaddrs(&ifap) < 0) {
|
||||
cmm_print(CMM_LOG_ERR, "itf: getifaddrs: %s",
|
||||
strerror(errno));
|
||||
return (-1);
|
||||
}
|
||||
|
||||
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
|
||||
if (ifa->ifa_addr == NULL)
|
||||
continue;
|
||||
|
||||
/* Create interface entry on AF_LINK (one per interface) */
|
||||
if (ifa->ifa_addr->sa_family == AF_LINK) {
|
||||
struct sockaddr_dl *sdl;
|
||||
int idx;
|
||||
|
||||
sdl = (struct sockaddr_dl *)ifa->ifa_addr;
|
||||
idx = sdl->sdl_index;
|
||||
|
||||
itf = cmm_itf_find_by_index(idx);
|
||||
if (itf != NULL)
|
||||
continue;
|
||||
|
||||
itf = itf_create(ifa->ifa_name, idx);
|
||||
if (itf == NULL)
|
||||
continue;
|
||||
|
||||
itf->flags = ifa->ifa_flags;
|
||||
|
||||
/* Extract MAC address */
|
||||
if (sdl->sdl_alen == ETHER_ADDR_LEN)
|
||||
memcpy(itf->macaddr, LLADDR(sdl),
|
||||
ETHER_ADDR_LEN);
|
||||
|
||||
/* Detect bridge by link-layer type */
|
||||
if (sdl->sdl_type == IFT_BRIDGE)
|
||||
itf->itf_flags |= ITF_F_BRIDGE;
|
||||
|
||||
/* Get MTU and detect VLAN via ioctl */
|
||||
{
|
||||
struct ifreq ifr;
|
||||
int sd;
|
||||
|
||||
sd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sd >= 0) {
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strlcpy(ifr.ifr_name, ifa->ifa_name,
|
||||
sizeof(ifr.ifr_name));
|
||||
if (ioctl(sd, SIOCGIFMTU, &ifr) == 0)
|
||||
itf->mtu = ifr.ifr_mtu;
|
||||
itf_detect_vlan(itf, sd);
|
||||
itf_detect_tunnel(itf, sd);
|
||||
close(sd);
|
||||
}
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"itf: %s idx=%d mac=%02x:%02x:%02x:%02x:%02x:%02x "
|
||||
"mtu=%u flags=0x%x",
|
||||
itf->ifname, itf->ifindex,
|
||||
itf->macaddr[0], itf->macaddr[1],
|
||||
itf->macaddr[2], itf->macaddr[3],
|
||||
itf->macaddr[4], itf->macaddr[5],
|
||||
itf->mtu, itf->flags);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Add IP addresses */
|
||||
if (ifa->ifa_addr->sa_family == AF_INET) {
|
||||
struct sockaddr_in *sin, *mask;
|
||||
uint8_t plen = 32;
|
||||
|
||||
sin = (struct sockaddr_in *)ifa->ifa_addr;
|
||||
itf = cmm_itf_find_by_name(ifa->ifa_name);
|
||||
if (itf == NULL)
|
||||
continue;
|
||||
|
||||
if (ifa->ifa_netmask) {
|
||||
mask = (struct sockaddr_in *)ifa->ifa_netmask;
|
||||
plen = mask_to_prefixlen(AF_INET,
|
||||
&mask->sin_addr);
|
||||
}
|
||||
|
||||
itf_add_addr(itf, AF_INET, &sin->sin_addr, plen);
|
||||
cmm_print(CMM_LOG_DEBUG, "itf: %s addr %s/%u",
|
||||
itf->ifname, inet_ntoa(sin->sin_addr), plen);
|
||||
|
||||
} else if (ifa->ifa_addr->sa_family == AF_INET6) {
|
||||
struct sockaddr_in6 *sin6, *mask6;
|
||||
uint8_t plen = 128;
|
||||
|
||||
sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
|
||||
itf = cmm_itf_find_by_name(ifa->ifa_name);
|
||||
if (itf == NULL)
|
||||
continue;
|
||||
|
||||
if (ifa->ifa_netmask) {
|
||||
mask6 = (struct sockaddr_in6 *)ifa->ifa_netmask;
|
||||
plen = mask_to_prefixlen(AF_INET6,
|
||||
&mask6->sin6_addr);
|
||||
}
|
||||
|
||||
itf_add_addr(itf, AF_INET6, &sin6->sin6_addr, plen);
|
||||
}
|
||||
}
|
||||
|
||||
freeifaddrs(ifap);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_itf_fini(void)
|
||||
{
|
||||
struct cmm_interface *itf;
|
||||
struct cmm_ifaddr *ifa;
|
||||
struct list_head *pos, *tmp, *apos, *atmp;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ITF_HASH_SIZE; i++) {
|
||||
pos = list_first(&itf_hash[i]);
|
||||
while (pos != &itf_hash[i]) {
|
||||
tmp = list_next(pos);
|
||||
itf = container_of(pos, struct cmm_interface, entry);
|
||||
|
||||
/* Free addresses */
|
||||
apos = list_first(&itf->addrs);
|
||||
while (apos != &itf->addrs) {
|
||||
atmp = list_next(apos);
|
||||
ifa = container_of(apos, struct cmm_ifaddr,
|
||||
entry);
|
||||
list_del(&ifa->entry);
|
||||
free(ifa);
|
||||
apos = atmp;
|
||||
}
|
||||
|
||||
list_del(&itf->entry);
|
||||
free(itf);
|
||||
pos = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cmm_itf_handle_ifinfo(struct cmm_global *g, void *msg, int msglen)
|
||||
{
|
||||
struct if_msghdr *ifm = msg;
|
||||
struct cmm_interface *itf;
|
||||
|
||||
(void)msglen;
|
||||
|
||||
itf = cmm_itf_find_by_index(ifm->ifm_index);
|
||||
if (itf == NULL) {
|
||||
char ifname[IFNAMSIZ];
|
||||
|
||||
if (if_indextoname(ifm->ifm_index, ifname) == NULL)
|
||||
return;
|
||||
|
||||
itf = itf_create(ifname, ifm->ifm_index);
|
||||
if (itf == NULL)
|
||||
return;
|
||||
|
||||
/* Detect bridge by interface type */
|
||||
if (ifm->ifm_data.ifi_type == IFT_BRIDGE)
|
||||
itf->itf_flags |= ITF_F_BRIDGE;
|
||||
|
||||
/* Populate MTU, MAC, and detect VLAN via ioctl */
|
||||
{
|
||||
struct ifreq ifr;
|
||||
int sd;
|
||||
|
||||
sd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sd >= 0) {
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strlcpy(ifr.ifr_name, ifname,
|
||||
sizeof(ifr.ifr_name));
|
||||
if (ioctl(sd, SIOCGIFMTU, &ifr) == 0)
|
||||
itf->mtu = ifr.ifr_mtu;
|
||||
itf_detect_vlan(itf, sd);
|
||||
itf_detect_tunnel(itf, sd);
|
||||
close(sd);
|
||||
}
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"itf: new %s idx=%d mtu=%u",
|
||||
itf->ifname, itf->ifindex, itf->mtu);
|
||||
}
|
||||
|
||||
if (itf->flags != (uint32_t)ifm->ifm_flags) {
|
||||
cmm_print(CMM_LOG_INFO, "itf: %s flags 0x%x -> 0x%x%s%s",
|
||||
itf->ifname, itf->flags, ifm->ifm_flags,
|
||||
(ifm->ifm_flags & IFF_UP) ? " UP" : " DOWN",
|
||||
(ifm->ifm_flags & IFF_RUNNING) ? " RUNNING" : "");
|
||||
itf->flags = ifm->ifm_flags;
|
||||
}
|
||||
|
||||
if (ifm->ifm_data.ifi_mtu != 0 &&
|
||||
itf->mtu != (uint32_t)ifm->ifm_data.ifi_mtu) {
|
||||
cmm_print(CMM_LOG_INFO, "itf: %s mtu %u -> %u",
|
||||
itf->ifname, itf->mtu,
|
||||
(unsigned int)ifm->ifm_data.ifi_mtu);
|
||||
itf->mtu = ifm->ifm_data.ifi_mtu;
|
||||
}
|
||||
|
||||
/* Notify VLAN, tunnel, and bridge modules of flag changes */
|
||||
cmm_vlan_notify(g, itf);
|
||||
cmm_tunnel_notify(g, itf);
|
||||
|
||||
/* If this is a bridge or a potential bridge member, rescan */
|
||||
if ((itf->itf_flags & ITF_F_BRIDGE) ||
|
||||
ifm->ifm_data.ifi_type == IFT_BRIDGE)
|
||||
cmm_bridge_itf_update(g);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_itf_handle_newaddr(struct cmm_global *g, void *msg, int msglen)
|
||||
{
|
||||
struct ifa_msghdr *ifam = msg;
|
||||
struct cmm_interface *itf;
|
||||
struct sockaddr *sa;
|
||||
char *cp;
|
||||
int i;
|
||||
|
||||
(void)msglen;
|
||||
|
||||
itf = cmm_itf_find_by_index(ifam->ifam_index);
|
||||
if (itf == NULL)
|
||||
return;
|
||||
|
||||
/* Parse sockaddrs to find the address */
|
||||
cp = (char *)(ifam + 1);
|
||||
for (i = 0; i < RTAX_MAX; i++) {
|
||||
if (!(ifam->ifam_addrs & (1 << i)))
|
||||
continue;
|
||||
sa = (struct sockaddr *)cp;
|
||||
if (i == RTAX_IFA) {
|
||||
if (sa->sa_family == AF_INET) {
|
||||
struct sockaddr_in *sin;
|
||||
sin = (struct sockaddr_in *)sa;
|
||||
itf_add_addr(itf, AF_INET,
|
||||
&sin->sin_addr, 0);
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"itf: %s new addr %s",
|
||||
itf->ifname,
|
||||
inet_ntoa(sin->sin_addr));
|
||||
} else if (sa->sa_family == AF_INET6) {
|
||||
struct sockaddr_in6 *sin6;
|
||||
sin6 = (struct sockaddr_in6 *)sa;
|
||||
itf_add_addr(itf, AF_INET6,
|
||||
&sin6->sin6_addr, 0);
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"itf: %s new IPv6 addr", itf->ifname);
|
||||
}
|
||||
break;
|
||||
}
|
||||
cp += (sa->sa_len ?
|
||||
(1 + ((sa->sa_len - 1) | (sizeof(long) - 1))) :
|
||||
sizeof(long));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cmm_itf_handle_deladdr(struct cmm_global *g, void *msg, int msglen)
|
||||
{
|
||||
struct ifa_msghdr *ifam = msg;
|
||||
struct cmm_interface *itf;
|
||||
struct sockaddr *sa;
|
||||
char *cp;
|
||||
int i;
|
||||
|
||||
(void)g;
|
||||
(void)msglen;
|
||||
|
||||
itf = cmm_itf_find_by_index(ifam->ifam_index);
|
||||
if (itf == NULL)
|
||||
return;
|
||||
|
||||
/* Parse sockaddrs to find the deleted address */
|
||||
cp = (char *)(ifam + 1);
|
||||
for (i = 0; i < RTAX_MAX; i++) {
|
||||
if (!(ifam->ifam_addrs & (1 << i)))
|
||||
continue;
|
||||
sa = (struct sockaddr *)cp;
|
||||
if (i == RTAX_IFA) {
|
||||
struct list_head *pos, *tmp;
|
||||
struct cmm_ifaddr *ifa;
|
||||
|
||||
for (pos = list_first(&itf->addrs);
|
||||
pos != &itf->addrs; ) {
|
||||
tmp = list_next(pos);
|
||||
ifa = container_of(pos, struct cmm_ifaddr,
|
||||
entry);
|
||||
if (ifa->family != sa->sa_family) {
|
||||
pos = tmp;
|
||||
continue;
|
||||
}
|
||||
int match = 0;
|
||||
if (sa->sa_family == AF_INET) {
|
||||
struct sockaddr_in *sin;
|
||||
sin = (struct sockaddr_in *)sa;
|
||||
match = memcmp(&ifa->addr.v4,
|
||||
&sin->sin_addr, 4) == 0;
|
||||
} else if (sa->sa_family == AF_INET6) {
|
||||
struct sockaddr_in6 *sin6;
|
||||
sin6 = (struct sockaddr_in6 *)sa;
|
||||
match = memcmp(&ifa->addr.v6,
|
||||
&sin6->sin6_addr, 16) == 0;
|
||||
}
|
||||
if (match) {
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"itf: %s del addr",
|
||||
itf->ifname);
|
||||
list_del(&ifa->entry);
|
||||
free(ifa);
|
||||
break;
|
||||
}
|
||||
pos = tmp;
|
||||
}
|
||||
break;
|
||||
}
|
||||
cp += (sa->sa_len ?
|
||||
(1 + ((sa->sa_len - 1) | (sizeof(long) - 1))) :
|
||||
sizeof(long));
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
cmm_itf_is_local_addr(sa_family_t af, const void *addr)
|
||||
{
|
||||
struct cmm_interface *itf;
|
||||
struct cmm_ifaddr *ifa;
|
||||
struct list_head *bucket, *pos, *apos;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ITF_HASH_SIZE; i++) {
|
||||
bucket = &itf_hash[i];
|
||||
for (pos = list_first(bucket); pos != bucket;
|
||||
pos = list_next(pos)) {
|
||||
itf = container_of(pos, struct cmm_interface, entry);
|
||||
for (apos = list_first(&itf->addrs);
|
||||
apos != &itf->addrs;
|
||||
apos = list_next(apos)) {
|
||||
ifa = container_of(apos, struct cmm_ifaddr,
|
||||
entry);
|
||||
if (ifa->family != af)
|
||||
continue;
|
||||
if (af == AF_INET) {
|
||||
if (memcmp(&ifa->addr.v4, addr, 4) == 0)
|
||||
return (1);
|
||||
} else {
|
||||
if (memcmp(&ifa->addr.v6, addr, 16) == 0)
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_itf_foreach_vlan(struct cmm_global *g, cmm_itf_vlan_fn fn)
|
||||
{
|
||||
struct list_head *bucket, *pos;
|
||||
struct cmm_interface *itf;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ITF_HASH_SIZE; i++) {
|
||||
bucket = &itf_hash[i];
|
||||
for (pos = list_first(bucket); pos != bucket;
|
||||
pos = list_next(pos)) {
|
||||
itf = container_of(pos, struct cmm_interface, entry);
|
||||
if (itf->itf_flags & ITF_F_VLAN)
|
||||
fn(g, itf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cmm_itf_foreach_tunnel(struct cmm_global *g, cmm_itf_tunnel_fn fn)
|
||||
{
|
||||
struct list_head *bucket, *pos;
|
||||
struct cmm_interface *itf;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ITF_HASH_SIZE; i++) {
|
||||
bucket = &itf_hash[i];
|
||||
for (pos = list_first(bucket); pos != bucket;
|
||||
pos = list_next(pos)) {
|
||||
itf = container_of(pos, struct cmm_interface, entry);
|
||||
if (itf->itf_flags & ITF_F_TUNNEL)
|
||||
fn(g, itf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cmm_itf_foreach_l2tp(struct cmm_global *g, cmm_itf_l2tp_fn fn)
|
||||
{
|
||||
struct list_head *bucket, *pos;
|
||||
struct cmm_interface *itf;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ITF_HASH_SIZE; i++) {
|
||||
bucket = &itf_hash[i];
|
||||
for (pos = list_first(bucket); pos != bucket;
|
||||
pos = list_next(pos)) {
|
||||
itf = container_of(pos, struct cmm_interface, entry);
|
||||
if (itf->itf_flags & ITF_F_L2TP)
|
||||
fn(g, itf);
|
||||
}
|
||||
}
|
||||
}
|
||||
95
fastpath/cmm/cmm_itf.h
Normal file
95
fastpath/cmm/cmm_itf.h
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* cmm_itf.h — Interface monitoring
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_ITF_H
|
||||
#define CMM_ITF_H
|
||||
|
||||
#include "cmm.h"
|
||||
|
||||
#define ITF_HASH_SIZE 64
|
||||
|
||||
struct cmm_ifaddr {
|
||||
struct list_head entry;
|
||||
sa_family_t family;
|
||||
union {
|
||||
struct in_addr v4;
|
||||
struct in6_addr v6;
|
||||
} addr;
|
||||
uint8_t prefixlen;
|
||||
};
|
||||
|
||||
/* CMM-internal interface flags (itf_flags, distinct from IFF_* flags) */
|
||||
#define ITF_F_VLAN (1 << 0) /* 802.1Q VLAN interface */
|
||||
#define ITF_F_FPP_VLAN (1 << 1) /* VLAN registered in CDX */
|
||||
#define ITF_F_TUNNEL (1 << 2) /* Tunnel interface (gif/gre) */
|
||||
#define ITF_F_FPP_TNL (1 << 3) /* Tunnel registered in CDX */
|
||||
#define ITF_F_BRIDGE (1 << 4) /* Bridge interface */
|
||||
#define ITF_F_L2TP (1 << 5) /* L2TP tunnel interface */
|
||||
#define ITF_F_FPP_L2TP (1 << 6) /* L2TP registered in CDX */
|
||||
|
||||
struct cmm_route;
|
||||
|
||||
struct cmm_interface {
|
||||
struct list_head entry; /* hash bucket chain */
|
||||
char ifname[IFNAMSIZ];
|
||||
int ifindex;
|
||||
int parent_ifindex; /* physical parent (VLAN) */
|
||||
uint8_t macaddr[ETHER_ADDR_LEN];
|
||||
uint32_t mtu;
|
||||
uint32_t flags; /* IFF_UP, IFF_RUNNING, ... */
|
||||
uint32_t itf_flags; /* ITF_F_* */
|
||||
uint16_t vlan_id;
|
||||
struct list_head addrs; /* cmm_ifaddr list */
|
||||
/* Tunnel state (valid when ITF_F_TUNNEL set) */
|
||||
uint8_t tnl_mode; /* TNL_6O4, TNL_4O6, ... */
|
||||
sa_family_t tnl_family; /* outer address family */
|
||||
uint8_t tnl_local[16]; /* local endpoint */
|
||||
uint8_t tnl_remote[16]; /* remote endpoint */
|
||||
struct cmm_route *tnl_route; /* route to remote */
|
||||
/* L2TP state (valid when ITF_F_L2TP set) */
|
||||
uint16_t l2tp_local_tun_id;
|
||||
uint16_t l2tp_peer_tun_id;
|
||||
uint16_t l2tp_local_ses_id;
|
||||
uint16_t l2tp_peer_ses_id;
|
||||
uint16_t l2tp_options;
|
||||
uint16_t l2tp_sock_id; /* cmm_socket ID */
|
||||
};
|
||||
|
||||
/* Initialize interface table from getifaddrs */
|
||||
int cmm_itf_init(void);
|
||||
|
||||
/* Clean up interface table */
|
||||
void cmm_itf_fini(void);
|
||||
|
||||
/* Handle routing socket messages */
|
||||
void cmm_itf_handle_ifinfo(struct cmm_global *g, void *msg, int msglen);
|
||||
void cmm_itf_handle_newaddr(struct cmm_global *g, void *msg, int msglen);
|
||||
void cmm_itf_handle_deladdr(struct cmm_global *g, void *msg, int msglen);
|
||||
|
||||
/* Lookups */
|
||||
struct cmm_interface *cmm_itf_find_by_index(int ifindex);
|
||||
struct cmm_interface *cmm_itf_find_by_name(const char *name);
|
||||
|
||||
/* Check if an address is local to this machine */
|
||||
int cmm_itf_is_local_addr(sa_family_t af, const void *addr);
|
||||
|
||||
/* Iterate all VLAN interfaces, calling fn for each with ITF_F_VLAN set */
|
||||
typedef int (*cmm_itf_vlan_fn)(struct cmm_global *, struct cmm_interface *);
|
||||
void cmm_itf_foreach_vlan(struct cmm_global *g, cmm_itf_vlan_fn fn);
|
||||
|
||||
/* Iterate all tunnel interfaces, calling fn for each with ITF_F_TUNNEL set */
|
||||
typedef int (*cmm_itf_tunnel_fn)(struct cmm_global *, struct cmm_interface *);
|
||||
void cmm_itf_foreach_tunnel(struct cmm_global *g, cmm_itf_tunnel_fn fn);
|
||||
|
||||
/* Iterate all L2TP interfaces, calling fn for each with ITF_F_L2TP set */
|
||||
typedef int (*cmm_itf_l2tp_fn)(struct cmm_global *, struct cmm_interface *);
|
||||
void cmm_itf_foreach_l2tp(struct cmm_global *g, cmm_itf_l2tp_fn fn);
|
||||
|
||||
/* Probe interface for tunnel endpoints (gif/gre) */
|
||||
void itf_detect_tunnel(struct cmm_interface *itf, int sd);
|
||||
|
||||
#endif /* CMM_ITF_H */
|
||||
346
fastpath/cmm/cmm_l2tp.c
Normal file
346
fastpath/cmm/cmm_l2tp.c
Normal file
|
|
@ -0,0 +1,346 @@
|
|||
/*
|
||||
* cmm_l2tp.c — L2TP tunnel interface offload
|
||||
*
|
||||
* Command-driven management of L2TP sessions. Each session ties an
|
||||
* interface to a UDP socket with tunnel/session IDs. CMM allocates
|
||||
* the socket, programs CDX via FPP_CMD_L2TP_ITF_ADD/DEL, and tracks
|
||||
* state in the interface table.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_l2tp.h"
|
||||
#include "cmm_itf.h"
|
||||
#include "cmm_socket.h"
|
||||
#include "cmm_route.h"
|
||||
#include "cmm_fe.h"
|
||||
#include "cmm_ctrl.h"
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Helpers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void
|
||||
ctrl_send_resp(int fd, int16_t rc)
|
||||
{
|
||||
struct cmm_ctrl_resp resp;
|
||||
|
||||
resp.rc = rc;
|
||||
resp.len = 0;
|
||||
write(fd, &resp, sizeof(resp));
|
||||
}
|
||||
|
||||
/*
|
||||
* Register an L2TP interface in CDX.
|
||||
*
|
||||
* Allocates a UDP socket for the tunnel transport, programs it in CDX,
|
||||
* then registers the L2TP interface itself via FPP_CMD_L2TP_ITF_ADD.
|
||||
*/
|
||||
static int
|
||||
l2tp_register(struct cmm_global *g, struct cmm_interface *itf,
|
||||
const struct cmm_ctrl_l2tp_add *cmd)
|
||||
{
|
||||
fpp_l2tp_itf_add_cmd_t fcmd;
|
||||
struct cmm_socket *sk;
|
||||
uint16_t sock_id;
|
||||
int rc;
|
||||
|
||||
/* Allocate a socket ID */
|
||||
sock_id = cmm_socket_next_id();
|
||||
if (sock_id == 0) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"l2tp: %s: no free socket IDs", cmd->ifname);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Allocate and populate socket */
|
||||
sk = calloc(1, sizeof(*sk));
|
||||
if (sk == NULL) {
|
||||
cmm_print(CMM_LOG_ERR,
|
||||
"l2tp: %s: malloc failed", cmd->ifname);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
sk->id = sock_id;
|
||||
sk->af = cmd->af;
|
||||
sk->proto = IPPROTO_UDP;
|
||||
sk->type = CMM_SOCK_TYPE_LANWAN;
|
||||
sk->mode = CMM_SOCK_MODE_CONNECTED;
|
||||
memcpy(sk->saddr, cmd->local_addr, 16);
|
||||
memcpy(sk->daddr, cmd->peer_addr, 16);
|
||||
sk->sport = cmd->local_port;
|
||||
sk->dport = cmd->peer_port;
|
||||
sk->queue = cmd->queue;
|
||||
sk->dscp = cmd->dscp;
|
||||
|
||||
/* Resolve route to peer */
|
||||
sk->route = cmm_route_get(g, sk->af, sk->daddr);
|
||||
if (sk->route == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"l2tp: %s: no route to peer", cmd->ifname);
|
||||
free(sk);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Ensure route is registered in CDX */
|
||||
if (!sk->route->fpp_programmed) {
|
||||
if (sk->route->neigh == NULL ||
|
||||
sk->route->neigh->state != NEIGH_RESOLVED) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"l2tp: %s: neighbor not resolved",
|
||||
cmd->ifname);
|
||||
cmm_route_put(sk->route);
|
||||
free(sk);
|
||||
return (-1);
|
||||
}
|
||||
if (cmm_fe_route_register(g, sk->route) < 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"l2tp: %s: route registration failed",
|
||||
cmd->ifname);
|
||||
cmm_route_put(sk->route);
|
||||
free(sk);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Program socket in CDX */
|
||||
if (cmm_socket_fpp_open(g, sk) < 0) {
|
||||
cmm_route_put(sk->route);
|
||||
free(sk);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Add socket to global list */
|
||||
cmm_socket_add(sk);
|
||||
|
||||
/* Register L2TP interface in CDX */
|
||||
memset(&fcmd, 0, sizeof(fcmd));
|
||||
strlcpy(fcmd.ifname, cmd->ifname, sizeof(fcmd.ifname));
|
||||
fcmd.sock_id = sock_id;
|
||||
fcmd.local_tun_id = cmd->local_tun_id;
|
||||
fcmd.peer_tun_id = cmd->peer_tun_id;
|
||||
fcmd.local_ses_id = cmd->local_ses_id;
|
||||
fcmd.peer_ses_id = cmd->peer_ses_id;
|
||||
fcmd.options = cmd->options;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_L2TP_ITF_ADD,
|
||||
sizeof(fcmd), (unsigned short *)&fcmd);
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"l2tp: %s: FPP_CMD_L2TP_ITF_ADD failed: %d",
|
||||
cmd->ifname, rc);
|
||||
/* Rollback socket */
|
||||
cmm_socket_fpp_close(g, sk);
|
||||
cmm_socket_remove(sk);
|
||||
if (sk->route != NULL)
|
||||
cmm_route_put(sk->route);
|
||||
free(sk);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Update interface state */
|
||||
itf->itf_flags |= ITF_F_L2TP | ITF_F_FPP_L2TP;
|
||||
itf->l2tp_local_tun_id = cmd->local_tun_id;
|
||||
itf->l2tp_peer_tun_id = cmd->peer_tun_id;
|
||||
itf->l2tp_local_ses_id = cmd->local_ses_id;
|
||||
itf->l2tp_peer_ses_id = cmd->peer_ses_id;
|
||||
itf->l2tp_options = cmd->options;
|
||||
itf->l2tp_sock_id = sock_id;
|
||||
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"l2tp: %s registered (sock=%u tun=%u/%u ses=%u/%u)",
|
||||
cmd->ifname, sock_id,
|
||||
cmd->local_tun_id, cmd->peer_tun_id,
|
||||
cmd->local_ses_id, cmd->peer_ses_id);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Deregister an L2TP interface from CDX.
|
||||
* Sends FPP_CMD_L2TP_ITF_DEL, then tears down the associated socket.
|
||||
*/
|
||||
static void
|
||||
l2tp_deregister(struct cmm_global *g, struct cmm_interface *itf)
|
||||
{
|
||||
fpp_l2tp_itf_del_cmd_t fcmd;
|
||||
struct cmm_socket *sk;
|
||||
int rc;
|
||||
|
||||
if (!(itf->itf_flags & ITF_F_L2TP))
|
||||
return;
|
||||
|
||||
/* Deregister L2TP interface from CDX */
|
||||
if (itf->itf_flags & ITF_F_FPP_L2TP) {
|
||||
memset(&fcmd, 0, sizeof(fcmd));
|
||||
strlcpy(fcmd.ifname, itf->ifname, sizeof(fcmd.ifname));
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_L2TP_ITF_DEL,
|
||||
sizeof(fcmd), (unsigned short *)&fcmd);
|
||||
if (rc != 0)
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"l2tp: %s: FPP_CMD_L2TP_ITF_DEL failed: %d",
|
||||
itf->ifname, rc);
|
||||
}
|
||||
|
||||
/* Tear down associated socket */
|
||||
sk = cmm_socket_find(itf->l2tp_sock_id);
|
||||
if (sk != NULL) {
|
||||
cmm_socket_fpp_close(g, sk);
|
||||
if (sk->route != NULL) {
|
||||
cmm_route_put(sk->route);
|
||||
sk->route = NULL;
|
||||
}
|
||||
cmm_socket_remove(sk);
|
||||
free(sk);
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "l2tp: %s deregistered", itf->ifname);
|
||||
|
||||
/* Clear interface state */
|
||||
itf->itf_flags &= ~(ITF_F_L2TP | ITF_F_FPP_L2TP);
|
||||
itf->l2tp_local_tun_id = 0;
|
||||
itf->l2tp_peer_tun_id = 0;
|
||||
itf->l2tp_local_ses_id = 0;
|
||||
itf->l2tp_peer_ses_id = 0;
|
||||
itf->l2tp_options = 0;
|
||||
itf->l2tp_sock_id = 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Control socket handlers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
void
|
||||
cmm_l2tp_ctrl_add(struct cmm_global *g, int client_fd,
|
||||
const void *payload, uint16_t len)
|
||||
{
|
||||
const struct cmm_ctrl_l2tp_add *cmd = payload;
|
||||
struct cmm_interface *itf;
|
||||
char name[IFNAMSIZ];
|
||||
|
||||
if (len < sizeof(*cmd)) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"l2tp: ctrl add: bad payload len %u (want %zu)",
|
||||
len, sizeof(*cmd));
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_BAD_LEN);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Sanitize interface name */
|
||||
memset(name, 0, sizeof(name));
|
||||
memcpy(name, cmd->ifname,
|
||||
sizeof(cmd->ifname) < IFNAMSIZ ? sizeof(cmd->ifname) : IFNAMSIZ);
|
||||
name[IFNAMSIZ - 1] = '\0';
|
||||
|
||||
/* Validate address family */
|
||||
if (cmd->af != AF_INET && cmd->af != AF_INET6) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"l2tp: ctrl add: %s: unsupported af %d", name, cmd->af);
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Find interface */
|
||||
itf = cmm_itf_find_by_name(name);
|
||||
if (itf == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"l2tp: ctrl add: interface '%s' not found", name);
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Check not already registered */
|
||||
if (itf->itf_flags & ITF_F_L2TP) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"l2tp: ctrl add: %s already registered", name);
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Check FCI handle */
|
||||
if (g->fci_handle == NULL) {
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_NO_FCI);
|
||||
return;
|
||||
}
|
||||
|
||||
if (l2tp_register(g, itf, cmd) < 0) {
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_FCI_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
ctrl_send_resp(client_fd, 0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_l2tp_ctrl_del(struct cmm_global *g, int client_fd,
|
||||
const void *payload, uint16_t len)
|
||||
{
|
||||
char name[IFNAMSIZ];
|
||||
struct cmm_interface *itf;
|
||||
|
||||
if (len < 1 || len > IFNAMSIZ) {
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_BAD_LEN);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(name, 0, sizeof(name));
|
||||
memcpy(name, payload, len);
|
||||
name[IFNAMSIZ - 1] = '\0';
|
||||
|
||||
itf = cmm_itf_find_by_name(name);
|
||||
if (itf == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"l2tp: ctrl del: interface '%s' not found", name);
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(itf->itf_flags & ITF_F_L2TP)) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"l2tp: ctrl del: %s not an L2TP interface", name);
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD);
|
||||
return;
|
||||
}
|
||||
|
||||
l2tp_deregister(g, itf);
|
||||
ctrl_send_resp(client_fd, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Init / Fini */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int
|
||||
l2tp_deregister_wrap(struct cmm_global *g, struct cmm_interface *itf)
|
||||
{
|
||||
|
||||
l2tp_deregister(g, itf);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_l2tp_init(struct cmm_global *g __unused)
|
||||
{
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "l2tp: initialized");
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_l2tp_fini(struct cmm_global *g)
|
||||
{
|
||||
|
||||
cmm_itf_foreach_l2tp(g, l2tp_deregister_wrap);
|
||||
}
|
||||
26
fastpath/cmm/cmm_l2tp.h
Normal file
26
fastpath/cmm/cmm_l2tp.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* cmm_l2tp.h — L2TP tunnel interface offload
|
||||
*
|
||||
* Command-driven L2TP session management. External tools (cmmctl)
|
||||
* send session descriptors via the CMM control socket; CMM allocates
|
||||
* a UDP socket, programs CDX, and tracks state.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_L2TP_H
|
||||
#define CMM_L2TP_H
|
||||
|
||||
struct cmm_global;
|
||||
|
||||
int cmm_l2tp_init(struct cmm_global *g);
|
||||
void cmm_l2tp_fini(struct cmm_global *g);
|
||||
|
||||
/* Control socket handlers */
|
||||
void cmm_l2tp_ctrl_add(struct cmm_global *g, int client_fd,
|
||||
const void *payload, uint16_t len);
|
||||
void cmm_l2tp_ctrl_del(struct cmm_global *g, int client_fd,
|
||||
const void *payload, uint16_t len);
|
||||
|
||||
#endif /* CMM_L2TP_H */
|
||||
305
fastpath/cmm/cmm_neigh.c
Normal file
305
fastpath/cmm/cmm_neigh.c
Normal file
|
|
@ -0,0 +1,305 @@
|
|||
/*
|
||||
* cmm_neigh.c — ARP/NDP neighbor resolution
|
||||
*
|
||||
* Resolves next-hop IP addresses to MAC addresses by querying
|
||||
* FreeBSD's link-layer table via sysctl(NET_RT_FLAGS).
|
||||
*
|
||||
* On FreeBSD 13+, ARP/NDP entries live in a separate lltable,
|
||||
* not as cloned host routes in the FIB. RTM_GET returns the
|
||||
* network route (no MAC), so we use sysctl to walk the lltable
|
||||
* directly — the same method arp(8) uses.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <net/route.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_neigh.h"
|
||||
#include "cmm_rtsock.h"
|
||||
|
||||
static struct list_head neigh_hash[NEIGH_HASH_SIZE];
|
||||
|
||||
static inline unsigned int
|
||||
neigh_hash_index(sa_family_t af, const void *ip)
|
||||
{
|
||||
if (af == AF_INET)
|
||||
return (jhash(ip, 4, 0) % NEIGH_HASH_SIZE);
|
||||
else
|
||||
return (jhash(ip, 16, 0) % NEIGH_HASH_SIZE);
|
||||
}
|
||||
|
||||
static struct cmm_neigh *
|
||||
neigh_find(sa_family_t af, const void *ip, int ifindex)
|
||||
{
|
||||
struct list_head *bucket, *pos;
|
||||
struct cmm_neigh *neigh;
|
||||
unsigned int h;
|
||||
int alen;
|
||||
|
||||
h = neigh_hash_index(af, ip);
|
||||
bucket = &neigh_hash[h];
|
||||
alen = (af == AF_INET) ? 4 : 16;
|
||||
|
||||
for (pos = list_first(bucket); pos != bucket; pos = list_next(pos)) {
|
||||
neigh = container_of(pos, struct cmm_neigh, entry);
|
||||
if (neigh->family == af &&
|
||||
neigh->ifindex == ifindex &&
|
||||
memcmp(neigh->ipaddr, ip, alen) == 0)
|
||||
return (neigh);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Resolve neighbor by walking the kernel's link-layer table
|
||||
* via sysctl(NET_RT_FLAGS). Each entry is an rt_msghdr followed
|
||||
* by sockaddrs: DST (sockaddr_in/in6) + GATEWAY (sockaddr_dl).
|
||||
*
|
||||
* Returns 0 on success, -1 on failure.
|
||||
*/
|
||||
static int
|
||||
neigh_resolve(struct cmm_global *g __unused, struct cmm_neigh *neigh)
|
||||
{
|
||||
int mib[6];
|
||||
size_t needed;
|
||||
char *buf, *next, *lim;
|
||||
struct rt_msghdr *rtm;
|
||||
struct cmm_rtsock_addrs addrs;
|
||||
|
||||
mib[0] = CTL_NET;
|
||||
mib[1] = PF_ROUTE;
|
||||
mib[2] = 0;
|
||||
mib[3] = neigh->family;
|
||||
mib[4] = NET_RT_FLAGS;
|
||||
mib[5] = 0;
|
||||
|
||||
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
|
||||
neigh->state = NEIGH_FAILED;
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if (needed == 0) {
|
||||
neigh->state = NEIGH_INCOMPLETE;
|
||||
return (-1);
|
||||
}
|
||||
|
||||
buf = malloc(needed);
|
||||
if (buf == NULL) {
|
||||
neigh->state = NEIGH_FAILED;
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
|
||||
free(buf);
|
||||
neigh->state = NEIGH_FAILED;
|
||||
return (-1);
|
||||
}
|
||||
|
||||
lim = buf + needed;
|
||||
for (next = buf; next < lim; ) {
|
||||
rtm = (struct rt_msghdr *)next;
|
||||
if (rtm->rtm_msglen == 0)
|
||||
break;
|
||||
next += rtm->rtm_msglen;
|
||||
|
||||
cmm_rtsock_parse_addrs(rtm, &addrs);
|
||||
|
||||
if (addrs.dst == NULL || addrs.gateway == NULL)
|
||||
continue;
|
||||
if (addrs.gateway->sa_family != AF_LINK)
|
||||
continue;
|
||||
|
||||
/* Match destination IP */
|
||||
if (neigh->family == AF_INET) {
|
||||
struct sockaddr_in *sin;
|
||||
|
||||
if (addrs.dst->sa_family != AF_INET)
|
||||
continue;
|
||||
sin = (struct sockaddr_in *)addrs.dst;
|
||||
if (memcmp(&sin->sin_addr, neigh->ipaddr, 4) != 0)
|
||||
continue;
|
||||
} else {
|
||||
struct sockaddr_in6 *sin6;
|
||||
|
||||
if (addrs.dst->sa_family != AF_INET6)
|
||||
continue;
|
||||
sin6 = (struct sockaddr_in6 *)addrs.dst;
|
||||
if (memcmp(&sin6->sin6_addr, neigh->ipaddr, 16) != 0)
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Found — extract MAC from sockaddr_dl gateway */
|
||||
{
|
||||
struct sockaddr_dl *sdl;
|
||||
|
||||
sdl = (struct sockaddr_dl *)addrs.gateway;
|
||||
if (sdl->sdl_alen != ETHER_ADDR_LEN)
|
||||
continue;
|
||||
|
||||
memcpy(neigh->macaddr, LLADDR(sdl), ETHER_ADDR_LEN);
|
||||
neigh->state = NEIGH_RESOLVED;
|
||||
free(buf);
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
||||
free(buf);
|
||||
neigh->state = NEIGH_INCOMPLETE;
|
||||
return (-1);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_neigh_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NEIGH_HASH_SIZE; i++)
|
||||
list_head_init(&neigh_hash[i]);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_neigh_fini(void)
|
||||
{
|
||||
struct cmm_neigh *neigh;
|
||||
struct list_head *pos, *tmp;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NEIGH_HASH_SIZE; i++) {
|
||||
pos = list_first(&neigh_hash[i]);
|
||||
while (pos != &neigh_hash[i]) {
|
||||
tmp = list_next(pos);
|
||||
neigh = container_of(pos, struct cmm_neigh, entry);
|
||||
list_del(&neigh->entry);
|
||||
free(neigh);
|
||||
pos = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct cmm_neigh *
|
||||
cmm_neigh_get(struct cmm_global *g, sa_family_t af,
|
||||
const void *ip, int ifindex)
|
||||
{
|
||||
struct cmm_neigh *neigh;
|
||||
unsigned int h;
|
||||
int alen;
|
||||
|
||||
neigh = neigh_find(af, ip, ifindex);
|
||||
if (neigh != NULL) {
|
||||
if (neigh->state == NEIGH_RESOLVED) {
|
||||
neigh->refcount++;
|
||||
return (neigh);
|
||||
}
|
||||
/* Try to re-resolve stale/incomplete entry */
|
||||
if (neigh_resolve(g, neigh) == 0) {
|
||||
neigh->refcount++;
|
||||
return (neigh);
|
||||
}
|
||||
/*
|
||||
* Re-resolve failed. If nobody holds a reference,
|
||||
* remove the stale entry so a fresh one can be
|
||||
* created on the next attempt.
|
||||
*/
|
||||
if (neigh->refcount == 0) {
|
||||
list_del(&neigh->entry);
|
||||
free(neigh);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/* Create new entry */
|
||||
alen = (af == AF_INET) ? 4 : 16;
|
||||
neigh = calloc(1, sizeof(*neigh));
|
||||
if (neigh == NULL)
|
||||
return (NULL);
|
||||
|
||||
neigh->family = af;
|
||||
memcpy(neigh->ipaddr, ip, alen);
|
||||
neigh->ifindex = ifindex;
|
||||
neigh->state = NEIGH_INCOMPLETE;
|
||||
neigh->entry.next = NULL;
|
||||
neigh->entry.prev = NULL;
|
||||
|
||||
h = neigh_hash_index(af, ip);
|
||||
list_add(&neigh_hash[h], &neigh->entry);
|
||||
|
||||
if (neigh_resolve(g, neigh) == 0) {
|
||||
if (af == AF_INET) {
|
||||
char abuf[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, ip, abuf, sizeof(abuf));
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"neigh: resolved %s -> "
|
||||
"%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
abuf,
|
||||
neigh->macaddr[0], neigh->macaddr[1],
|
||||
neigh->macaddr[2], neigh->macaddr[3],
|
||||
neigh->macaddr[4], neigh->macaddr[5]);
|
||||
}
|
||||
neigh->refcount++;
|
||||
return (neigh);
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG, "neigh: resolution failed for ifindex=%d",
|
||||
ifindex);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_neigh_put(struct cmm_neigh *neigh)
|
||||
{
|
||||
if (neigh == NULL)
|
||||
return;
|
||||
if (neigh->refcount > 0)
|
||||
neigh->refcount--;
|
||||
}
|
||||
|
||||
void
|
||||
cmm_neigh_flush_ifindex(int ifindex)
|
||||
{
|
||||
struct cmm_neigh *neigh;
|
||||
struct list_head *pos;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NEIGH_HASH_SIZE; i++) {
|
||||
for (pos = list_first(&neigh_hash[i]);
|
||||
pos != &neigh_hash[i]; pos = list_next(pos)) {
|
||||
neigh = container_of(pos, struct cmm_neigh, entry);
|
||||
if (neigh->ifindex == ifindex)
|
||||
neigh->state = NEIGH_STALE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cmm_neigh_invalidate(sa_family_t af, const void *ip)
|
||||
{
|
||||
struct cmm_neigh *neigh;
|
||||
unsigned int h;
|
||||
struct list_head *pos;
|
||||
int alen;
|
||||
|
||||
h = neigh_hash_index(af, ip);
|
||||
alen = (af == AF_INET) ? 4 : 16;
|
||||
|
||||
for (pos = list_first(&neigh_hash[h]); pos != &neigh_hash[h];
|
||||
pos = list_next(pos)) {
|
||||
neigh = container_of(pos, struct cmm_neigh, entry);
|
||||
if (neigh->family == af &&
|
||||
memcmp(neigh->ipaddr, ip, alen) == 0) {
|
||||
neigh->state = NEIGH_STALE;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
52
fastpath/cmm/cmm_neigh.h
Normal file
52
fastpath/cmm/cmm_neigh.h
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* cmm_neigh.h — ARP/NDP neighbor resolution
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_NEIGH_H
|
||||
#define CMM_NEIGH_H
|
||||
|
||||
#include "cmm.h"
|
||||
|
||||
#define NEIGH_HASH_SIZE 256
|
||||
|
||||
/* Neighbor states */
|
||||
#define NEIGH_INCOMPLETE 0
|
||||
#define NEIGH_RESOLVED 1
|
||||
#define NEIGH_STALE 2
|
||||
#define NEIGH_FAILED 3
|
||||
|
||||
struct cmm_neigh {
|
||||
struct list_head entry; /* hash bucket chain */
|
||||
int refcount;
|
||||
sa_family_t family;
|
||||
uint8_t ipaddr[16]; /* network byte order */
|
||||
uint8_t macaddr[ETHER_ADDR_LEN];
|
||||
int ifindex;
|
||||
int state;
|
||||
};
|
||||
|
||||
/* Initialize neighbor table */
|
||||
int cmm_neigh_init(void);
|
||||
void cmm_neigh_fini(void);
|
||||
|
||||
/*
|
||||
* Get or create neighbor entry. Resolves via route socket RTM_GET
|
||||
* with RTF_LLINFO. Returns NULL if resolution fails.
|
||||
* Caller must call cmm_neigh_put() when done.
|
||||
*/
|
||||
struct cmm_neigh *cmm_neigh_get(struct cmm_global *g, sa_family_t af,
|
||||
const void *ip, int ifindex);
|
||||
|
||||
/* Release reference */
|
||||
void cmm_neigh_put(struct cmm_neigh *neigh);
|
||||
|
||||
/* Invalidate all neighbors on a given interface */
|
||||
void cmm_neigh_flush_ifindex(int ifindex);
|
||||
|
||||
/* Invalidate a specific neighbor (e.g. on ARP change) */
|
||||
void cmm_neigh_invalidate(sa_family_t af, const void *ip);
|
||||
|
||||
#endif /* CMM_NEIGH_H */
|
||||
131
fastpath/cmm/cmm_offload.c
Normal file
131
fastpath/cmm/cmm_offload.c
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
/*
|
||||
* cmm_offload.c — Offload eligibility decisions
|
||||
*
|
||||
* Determines whether a PF state is eligible for hardware offload.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#define COMPAT_FREEBSD14 /* pf_state_export, pfioc_states_v2 */
|
||||
#include <net/pfvar.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp_fsm.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_offload.h"
|
||||
#include "cmm_itf.h"
|
||||
|
||||
int
|
||||
cmm_offload_eligible(const struct pf_state_export *pfs)
|
||||
{
|
||||
uint8_t proto;
|
||||
sa_family_t af;
|
||||
const struct pf_state_key_export *stack;
|
||||
|
||||
proto = pfs->proto;
|
||||
af = pfs->af;
|
||||
stack = &pfs->key[PF_SK_STACK];
|
||||
|
||||
/* Only TCP and UDP */
|
||||
if (proto != IPPROTO_TCP && proto != IPPROTO_UDP)
|
||||
return (0);
|
||||
|
||||
/* Only IPv4 and IPv6 */
|
||||
if (af != AF_INET && af != AF_INET6)
|
||||
return (0);
|
||||
|
||||
/* Skip loopback */
|
||||
if (strncmp(pfs->ifname, "lo", 2) == 0)
|
||||
return (0);
|
||||
|
||||
/* Skip multicast destinations */
|
||||
int didx = (pfs->direction == PF_IN) ? 1 : 0;
|
||||
if (af == AF_INET) {
|
||||
uint32_t daddr;
|
||||
memcpy(&daddr, &stack->addr[didx].v4, 4);
|
||||
if (IN_MULTICAST(ntohl(daddr)))
|
||||
return (0);
|
||||
} else {
|
||||
if (IN6_IS_ADDR_MULTICAST(&stack->addr[didx].v6))
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* For TCP: require ESTABLISHED state.
|
||||
* PF state peer values are in network byte order.
|
||||
*/
|
||||
if (proto == IPPROTO_TCP) {
|
||||
uint8_t src_state, dst_state;
|
||||
|
||||
src_state = pfs->src.state;
|
||||
dst_state = pfs->dst.state;
|
||||
|
||||
/*
|
||||
* PF TCP state values:
|
||||
* TCPS_ESTABLISHED = 4 on FreeBSD.
|
||||
* Both sides must be at least ESTABLISHED.
|
||||
*/
|
||||
if (src_state < TCPS_ESTABLISHED ||
|
||||
dst_state < TCPS_ESTABLISHED)
|
||||
return (0);
|
||||
|
||||
/* Don't offload connections in FIN/CLOSE states */
|
||||
if (src_state >= TCPS_FIN_WAIT_1 ||
|
||||
dst_state >= TCPS_FIN_WAIT_1)
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* For UDP: require bidirectional traffic (both PF peers
|
||||
* have seen packets — indicated by non-zero state).
|
||||
*/
|
||||
if (proto == IPPROTO_UDP) {
|
||||
if (pfs->src.state == 0 || pfs->dst.state == 0)
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Skip connections where either endpoint is a local address.
|
||||
*
|
||||
* Use the STACK key (not wire key) because for NAT connections
|
||||
* created PF_OUT, the wire key contains the NAT'd address
|
||||
* (which IS local) but the stack key has the real endpoints.
|
||||
* For non-NAT states, wire == stack so this is equivalent.
|
||||
*/
|
||||
if (af == AF_INET) {
|
||||
if (cmm_itf_is_local_addr(AF_INET, &stack->addr[0].v4) ||
|
||||
cmm_itf_is_local_addr(AF_INET, &stack->addr[1].v4))
|
||||
return (0);
|
||||
} else {
|
||||
if (cmm_itf_is_local_addr(AF_INET6, &stack->addr[0].v6) ||
|
||||
cmm_itf_is_local_addr(AF_INET6, &stack->addr[1].v6))
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* For NAT connections, PF creates two states: one on PF_IN
|
||||
* (wire == stack, no NAT info) and one on PF_OUT (wire != stack,
|
||||
* has NAT info). Only offload the PF_OUT state which has the
|
||||
* correct wire-side addresses for both directions.
|
||||
*/
|
||||
if (memcmp(&pfs->key[PF_SK_WIRE], &pfs->key[PF_SK_STACK],
|
||||
sizeof(struct pf_state_key_export)) != 0) {
|
||||
/* NAT state (wire != stack) — eligible */
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*
|
||||
* wire == stack: either non-NAT (offload) or PF_IN side of a
|
||||
* NAT connection (skip — the PF_OUT state has correct info).
|
||||
* Heuristic: if direction is PF_OUT, it's a non-NAT outbound
|
||||
* state (rare for forwarded traffic). If PF_IN and there's a
|
||||
* nat rule, we can't tell here — accept it and let the hash
|
||||
* entry be harmlessly redundant. The PF_OUT state's correct
|
||||
* entry will be the one that actually matches.
|
||||
*/
|
||||
return (1);
|
||||
}
|
||||
22
fastpath/cmm/cmm_offload.h
Normal file
22
fastpath/cmm/cmm_offload.h
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* cmm_offload.h — Offload eligibility decisions
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_OFFLOAD_H
|
||||
#define CMM_OFFLOAD_H
|
||||
|
||||
#include "cmm.h"
|
||||
|
||||
struct cmm_conn;
|
||||
struct pf_state_export;
|
||||
|
||||
/*
|
||||
* Check if a PF state is eligible for hardware offload.
|
||||
* Returns 1 if eligible, 0 if not.
|
||||
*/
|
||||
int cmm_offload_eligible(const struct pf_state_export *pfs);
|
||||
|
||||
#endif /* CMM_OFFLOAD_H */
|
||||
387
fastpath/cmm/cmm_route.c
Normal file
387
fastpath/cmm/cmm_route.c
Normal file
|
|
@ -0,0 +1,387 @@
|
|||
/*
|
||||
* cmm_route.c — Route lookup and caching
|
||||
*
|
||||
* Looks up routes via the PF_ROUTE socket (RTM_GET) and caches
|
||||
* results. Monitors route changes via the same socket.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <net/route.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_route.h"
|
||||
#include "cmm_conn.h"
|
||||
#include "cmm_rtsock.h"
|
||||
#include "cmm_itf.h"
|
||||
#include "cmm_fe.h"
|
||||
#include "cmm_tunnel.h"
|
||||
#include "cmm_socket.h"
|
||||
|
||||
static struct list_head route_hash[ROUTE_HASH_SIZE];
|
||||
|
||||
static inline unsigned int
|
||||
route_hash_index(sa_family_t af, const void *dst)
|
||||
{
|
||||
if (af == AF_INET)
|
||||
return (jhash(dst, 4, 0) % ROUTE_HASH_SIZE);
|
||||
else
|
||||
return (jhash(dst, 16, 0) % ROUTE_HASH_SIZE);
|
||||
}
|
||||
|
||||
static struct cmm_route *
|
||||
route_find(sa_family_t af, const void *dst)
|
||||
{
|
||||
struct list_head *bucket, *pos;
|
||||
struct cmm_route *rt;
|
||||
unsigned int h;
|
||||
int alen;
|
||||
|
||||
h = route_hash_index(af, dst);
|
||||
bucket = &route_hash[h];
|
||||
alen = (af == AF_INET) ? 4 : 16;
|
||||
|
||||
for (pos = list_first(bucket); pos != bucket; pos = list_next(pos)) {
|
||||
rt = container_of(pos, struct cmm_route, entry);
|
||||
if (rt->family == af && memcmp(rt->dst, dst, alen) == 0)
|
||||
return (rt);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Resolve route via RTM_GET.
|
||||
* Returns 0 on success, -1 on failure.
|
||||
*/
|
||||
static int
|
||||
route_resolve(struct cmm_global *g, struct cmm_route *rt)
|
||||
{
|
||||
char reply[1024];
|
||||
size_t replylen;
|
||||
struct rt_msghdr *rtm;
|
||||
struct cmm_rtsock_addrs addrs;
|
||||
|
||||
if (rt->family == AF_INET) {
|
||||
struct sockaddr_in sin;
|
||||
|
||||
memset(&sin, 0, sizeof(sin));
|
||||
sin.sin_len = sizeof(sin);
|
||||
sin.sin_family = AF_INET;
|
||||
memcpy(&sin.sin_addr, rt->dst, 4);
|
||||
|
||||
replylen = sizeof(reply);
|
||||
if (cmm_rtsock_get(g->rtsock_query_fd, (struct sockaddr *)&sin,
|
||||
0, reply, &replylen) < 0)
|
||||
return (-1);
|
||||
} else {
|
||||
struct sockaddr_in6 sin6;
|
||||
|
||||
memset(&sin6, 0, sizeof(sin6));
|
||||
sin6.sin6_len = sizeof(sin6);
|
||||
sin6.sin6_family = AF_INET6;
|
||||
memcpy(&sin6.sin6_addr, rt->dst, 16);
|
||||
|
||||
replylen = sizeof(reply);
|
||||
if (cmm_rtsock_get(g->rtsock_query_fd, (struct sockaddr *)&sin6,
|
||||
0, reply, &replylen) < 0)
|
||||
return (-1);
|
||||
}
|
||||
|
||||
rtm = (struct rt_msghdr *)reply;
|
||||
cmm_rtsock_parse_addrs(rtm, &addrs);
|
||||
|
||||
rt->rt_flags = rtm->rtm_flags;
|
||||
rt->mtu = rtm->rtm_rmx.rmx_mtu;
|
||||
|
||||
/* Get output interface */
|
||||
if (addrs.ifp != NULL) {
|
||||
rt->oif_index = addrs.ifp->sdl_index;
|
||||
} else {
|
||||
rt->oif_index = rtm->rtm_index;
|
||||
}
|
||||
|
||||
/* Get gateway */
|
||||
if ((rtm->rtm_flags & RTF_GATEWAY) && addrs.gateway != NULL) {
|
||||
if (addrs.gateway->sa_family == AF_INET) {
|
||||
struct sockaddr_in *gw;
|
||||
gw = (struct sockaddr_in *)addrs.gateway;
|
||||
memcpy(rt->gw, &gw->sin_addr, 4);
|
||||
} else if (addrs.gateway->sa_family == AF_INET6) {
|
||||
struct sockaddr_in6 *gw6;
|
||||
gw6 = (struct sockaddr_in6 *)addrs.gateway;
|
||||
memcpy(rt->gw, &gw6->sin6_addr, 16);
|
||||
}
|
||||
} else {
|
||||
/* Directly connected — next-hop is the destination */
|
||||
memcpy(rt->gw, rt->dst,
|
||||
(rt->family == AF_INET) ? 4 : 16);
|
||||
}
|
||||
|
||||
/* Get preferred source */
|
||||
if (addrs.ifa != NULL) {
|
||||
if (addrs.ifa->sa_family == AF_INET) {
|
||||
struct sockaddr_in *src;
|
||||
src = (struct sockaddr_in *)addrs.ifa;
|
||||
memcpy(rt->src, &src->sin_addr, 4);
|
||||
} else if (addrs.ifa->sa_family == AF_INET6) {
|
||||
struct sockaddr_in6 *src6;
|
||||
src6 = (struct sockaddr_in6 *)addrs.ifa;
|
||||
memcpy(rt->src, &src6->sin6_addr, 16);
|
||||
}
|
||||
}
|
||||
|
||||
/* Clamp MTU to interface MTU if not set */
|
||||
if (rt->mtu == 0) {
|
||||
struct cmm_interface *itf;
|
||||
itf = cmm_itf_find_by_index(rt->oif_index);
|
||||
if (itf != NULL)
|
||||
rt->mtu = itf->mtu;
|
||||
if (rt->mtu == 0)
|
||||
rt->mtu = 1500;
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_route_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ROUTE_HASH_SIZE; i++)
|
||||
list_head_init(&route_hash[i]);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_route_fini(void)
|
||||
{
|
||||
struct cmm_route *rt;
|
||||
struct list_head *pos, *tmp;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ROUTE_HASH_SIZE; i++) {
|
||||
pos = list_first(&route_hash[i]);
|
||||
while (pos != &route_hash[i]) {
|
||||
tmp = list_next(pos);
|
||||
rt = container_of(pos, struct cmm_route, entry);
|
||||
if (rt->neigh != NULL)
|
||||
cmm_neigh_put(rt->neigh);
|
||||
list_del(&rt->entry);
|
||||
free(rt);
|
||||
pos = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
route_id_in_use(uint32_t id)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ROUTE_HASH_SIZE; i++) {
|
||||
struct list_head *pos;
|
||||
|
||||
for (pos = list_first(&route_hash[i]);
|
||||
pos != &route_hash[i]; pos = list_next(pos)) {
|
||||
struct cmm_route *rt;
|
||||
|
||||
rt = container_of(pos, struct cmm_route, entry);
|
||||
if (rt->fpp_id == id)
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
cmm_route_alloc_id(struct cmm_global *g)
|
||||
{
|
||||
uint32_t id;
|
||||
int tries;
|
||||
|
||||
for (tries = 0; tries < 65536; tries++) {
|
||||
id = ++g->next_route_id;
|
||||
if (id == 0)
|
||||
id = ++g->next_route_id;
|
||||
if (!route_id_in_use(id))
|
||||
return (id);
|
||||
}
|
||||
return (0); /* exhausted */
|
||||
}
|
||||
|
||||
struct cmm_route *
|
||||
cmm_route_get(struct cmm_global *g, sa_family_t af, const void *dst)
|
||||
{
|
||||
struct cmm_route *rt;
|
||||
unsigned int h;
|
||||
int alen;
|
||||
|
||||
rt = route_find(af, dst);
|
||||
if (rt != NULL) {
|
||||
rt->refcount++;
|
||||
return (rt);
|
||||
}
|
||||
|
||||
/* Create and resolve */
|
||||
alen = (af == AF_INET) ? 4 : 16;
|
||||
rt = calloc(1, sizeof(*rt));
|
||||
if (rt == NULL)
|
||||
return (NULL);
|
||||
|
||||
rt->family = af;
|
||||
memcpy(rt->dst, dst, alen);
|
||||
rt->fpp_id = cmm_route_alloc_id(g);
|
||||
if (rt->fpp_id == 0) {
|
||||
cmm_print(CMM_LOG_ERR, "route: route ID space exhausted");
|
||||
free(rt);
|
||||
return (NULL);
|
||||
}
|
||||
rt->entry.next = NULL;
|
||||
rt->entry.prev = NULL;
|
||||
|
||||
h = route_hash_index(af, dst);
|
||||
list_add(&route_hash[h], &rt->entry);
|
||||
|
||||
if (route_resolve(g, rt) < 0) {
|
||||
list_del(&rt->entry);
|
||||
free(rt);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/* Resolve next-hop neighbor */
|
||||
rt->neigh = cmm_neigh_get(g, af, rt->gw, rt->oif_index);
|
||||
|
||||
if (rt->family == AF_INET) {
|
||||
char dbuf[INET_ADDRSTRLEN], gbuf[INET_ADDRSTRLEN];
|
||||
struct cmm_interface *itf;
|
||||
|
||||
inet_ntop(AF_INET, rt->dst, dbuf, sizeof(dbuf));
|
||||
inet_ntop(AF_INET, rt->gw, gbuf, sizeof(gbuf));
|
||||
itf = cmm_itf_find_by_index(rt->oif_index);
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"route: %s via %s dev %s mtu %u id %u neigh=%s",
|
||||
dbuf, gbuf,
|
||||
itf ? itf->ifname : "?",
|
||||
rt->mtu, rt->fpp_id,
|
||||
rt->neigh ? "resolved" : "pending");
|
||||
}
|
||||
|
||||
rt->refcount = 1;
|
||||
return (rt);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_route_put(struct cmm_route *rt)
|
||||
{
|
||||
if (rt == NULL)
|
||||
return;
|
||||
if (rt->refcount > 0)
|
||||
rt->refcount--;
|
||||
}
|
||||
|
||||
void
|
||||
cmm_route_gc(struct cmm_global *g)
|
||||
{
|
||||
struct cmm_route *rt;
|
||||
struct list_head *pos, *tmp;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ROUTE_HASH_SIZE; i++) {
|
||||
pos = list_first(&route_hash[i]);
|
||||
while (pos != &route_hash[i]) {
|
||||
tmp = list_next(pos);
|
||||
rt = container_of(pos, struct cmm_route, entry);
|
||||
if (rt->refcount == 0) {
|
||||
if (rt->fpp_programmed)
|
||||
cmm_fe_route_deregister(g, rt);
|
||||
if (rt->neigh != NULL)
|
||||
cmm_neigh_put(rt->neigh);
|
||||
list_del(&rt->entry);
|
||||
free(rt);
|
||||
}
|
||||
pos = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cmm_route_handle_change(struct cmm_global *g, struct rt_msghdr *rtm)
|
||||
{
|
||||
struct cmm_rtsock_addrs addrs;
|
||||
struct cmm_route *rt;
|
||||
struct list_head *pos;
|
||||
uint8_t old_gw[16];
|
||||
int old_oif, alen, i;
|
||||
|
||||
cmm_rtsock_parse_addrs(rtm, &addrs);
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG, "route: RTM_%s flags=0x%x",
|
||||
rtm->rtm_type == RTM_ADD ? "ADD" :
|
||||
rtm->rtm_type == RTM_DELETE ? "DELETE" : "CHANGE",
|
||||
rtm->rtm_flags);
|
||||
|
||||
/*
|
||||
* Re-resolve all cached routes. This is conservative but safe.
|
||||
* If the gateway or output interface changed, deregister the
|
||||
* stale CDX route and all connections using it. They will
|
||||
* re-offload on the next poll cycle with the new path.
|
||||
*/
|
||||
for (i = 0; i < ROUTE_HASH_SIZE; i++) {
|
||||
for (pos = list_first(&route_hash[i]);
|
||||
pos != &route_hash[i]; pos = list_next(pos)) {
|
||||
rt = container_of(pos, struct cmm_route, entry);
|
||||
|
||||
/* Save old values before re-resolving */
|
||||
old_oif = rt->oif_index;
|
||||
memcpy(old_gw, rt->gw, sizeof(old_gw));
|
||||
|
||||
/* Drop old neighbor, re-resolve */
|
||||
if (rt->neigh != NULL) {
|
||||
cmm_neigh_put(rt->neigh);
|
||||
rt->neigh = NULL;
|
||||
}
|
||||
route_resolve(g, rt);
|
||||
rt->neigh = cmm_neigh_get(g, rt->family,
|
||||
rt->gw, rt->oif_index);
|
||||
|
||||
/* If nothing changed, skip CDX invalidation */
|
||||
alen = (rt->family == AF_INET) ? 4 : 16;
|
||||
if (rt->oif_index == old_oif &&
|
||||
memcmp(rt->gw, old_gw, alen) == 0)
|
||||
continue;
|
||||
|
||||
/* Route changed — tear down stale CDX state */
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"route: id=%u changed, deregistering",
|
||||
rt->fpp_id);
|
||||
cmm_route_invalidate_conns(g, rt);
|
||||
cmm_fe_route_deregister(g, rt);
|
||||
}
|
||||
}
|
||||
|
||||
/* Re-evaluate tunnel and socket registrations after route changes */
|
||||
cmm_tunnel_route_update(g);
|
||||
cmm_socket_route_update(g);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_route_send_fpp(struct cmm_global *g, struct cmm_route *rt, int action)
|
||||
{
|
||||
if (rt->neigh == NULL || rt->neigh->state != NEIGH_RESOLVED) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"route: can't send FPP — neighbor not resolved");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
return (cmm_fe_route_register(g, rt));
|
||||
}
|
||||
61
fastpath/cmm/cmm_route.h
Normal file
61
fastpath/cmm/cmm_route.h
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* cmm_route.h — Route lookup and caching
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_ROUTE_H
|
||||
#define CMM_ROUTE_H
|
||||
|
||||
#include <net/route.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_neigh.h"
|
||||
|
||||
#define ROUTE_HASH_SIZE 256
|
||||
|
||||
struct cmm_route {
|
||||
struct list_head entry; /* hash bucket chain */
|
||||
int refcount;
|
||||
sa_family_t family;
|
||||
uint8_t dst[16]; /* destination (net order) */
|
||||
uint8_t gw[16]; /* gateway / next-hop */
|
||||
uint8_t src[16]; /* preferred source */
|
||||
int oif_index; /* output interface index */
|
||||
int iif_index; /* input interface index (set by conn) */
|
||||
uint16_t mtu;
|
||||
uint32_t rt_flags; /* RTF_GATEWAY, RTF_HOST, ... */
|
||||
uint32_t fpp_id; /* FPP route ID */
|
||||
int fpp_programmed; /* sent to CDX */
|
||||
struct cmm_neigh *neigh; /* resolved next-hop */
|
||||
};
|
||||
|
||||
/* Initialize route cache */
|
||||
int cmm_route_init(void);
|
||||
void cmm_route_fini(void);
|
||||
|
||||
/*
|
||||
* Get route for destination. Resolves via route socket RTM_GET.
|
||||
* Returns NULL if no route. Caller must call cmm_route_put().
|
||||
*/
|
||||
struct cmm_route *cmm_route_get(struct cmm_global *g, sa_family_t af,
|
||||
const void *dst);
|
||||
|
||||
/* Release reference */
|
||||
void cmm_route_put(struct cmm_route *rt);
|
||||
|
||||
/* Free routes with refcount 0 (call after connection sweep) */
|
||||
void cmm_route_gc(struct cmm_global *g);
|
||||
|
||||
/* Handle routing socket changes — invalidate affected routes */
|
||||
void cmm_route_handle_change(struct cmm_global *g, struct rt_msghdr *rtm);
|
||||
|
||||
/* Send route to CDX via FPP */
|
||||
int cmm_route_send_fpp(struct cmm_global *g, struct cmm_route *rt,
|
||||
int action);
|
||||
|
||||
/* Allocate a new unique route ID */
|
||||
uint32_t cmm_route_alloc_id(struct cmm_global *g);
|
||||
|
||||
#endif /* CMM_ROUTE_H */
|
||||
185
fastpath/cmm/cmm_rtsock.c
Normal file
185
fastpath/cmm/cmm_rtsock.c
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
/*
|
||||
* cmm_rtsock.c — PF_ROUTE socket wrapper
|
||||
*
|
||||
* Provides route socket open/dispatch/query for monitoring
|
||||
* routes, interfaces, and neighbor changes on FreeBSD.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <net/route.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_rtsock.h"
|
||||
#include "cmm_itf.h"
|
||||
#include "cmm_route.h"
|
||||
|
||||
int cmm_rtsock_seq;
|
||||
|
||||
int
|
||||
cmm_rtsock_open(void)
|
||||
{
|
||||
int s, n;
|
||||
|
||||
s = socket(PF_ROUTE, SOCK_RAW, 0);
|
||||
if (s < 0)
|
||||
return (-1);
|
||||
|
||||
/* Enable error reporting on reads */
|
||||
n = 1;
|
||||
setsockopt(s, SOL_SOCKET, SO_RERROR, &n, sizeof(n));
|
||||
|
||||
return (s);
|
||||
}
|
||||
|
||||
/*
|
||||
* Round up sockaddr length to next long boundary.
|
||||
* This is the standard FreeBSD SA_SIZE macro pattern.
|
||||
*/
|
||||
#define SA_RLEN(sa) \
|
||||
((sa)->sa_len ? (1 + (((sa)->sa_len - 1) | (sizeof(long) - 1))) : \
|
||||
sizeof(long))
|
||||
|
||||
void
|
||||
cmm_rtsock_parse_addrs(struct rt_msghdr *rtm,
|
||||
struct cmm_rtsock_addrs *addrs)
|
||||
{
|
||||
struct sockaddr *sa;
|
||||
char *cp;
|
||||
int i;
|
||||
|
||||
memset(addrs, 0, sizeof(*addrs));
|
||||
cp = (char *)(rtm + 1);
|
||||
|
||||
for (i = 0; i < RTAX_MAX; i++) {
|
||||
if (!(rtm->rtm_addrs & (1 << i)))
|
||||
continue;
|
||||
sa = (struct sockaddr *)cp;
|
||||
switch (i) {
|
||||
case RTAX_DST:
|
||||
addrs->dst = sa;
|
||||
break;
|
||||
case RTAX_GATEWAY:
|
||||
addrs->gateway = sa;
|
||||
break;
|
||||
case RTAX_NETMASK:
|
||||
addrs->netmask = sa;
|
||||
break;
|
||||
case RTAX_IFP:
|
||||
if (sa->sa_family == AF_LINK)
|
||||
addrs->ifp = (struct sockaddr_dl *)sa;
|
||||
break;
|
||||
case RTAX_IFA:
|
||||
addrs->ifa = sa;
|
||||
break;
|
||||
}
|
||||
cp += SA_RLEN(sa);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
cmm_rtsock_get(int fd, struct sockaddr *dst, int flags,
|
||||
void *reply, size_t *replylen)
|
||||
{
|
||||
struct {
|
||||
struct rt_msghdr hdr;
|
||||
char data[512];
|
||||
} msg;
|
||||
struct rt_msghdr *rtm;
|
||||
pid_t pid;
|
||||
int seq, n;
|
||||
|
||||
pid = getpid();
|
||||
seq = ++cmm_rtsock_seq;
|
||||
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
rtm = &msg.hdr;
|
||||
rtm->rtm_version = RTM_VERSION;
|
||||
rtm->rtm_type = RTM_GET;
|
||||
rtm->rtm_flags = flags;
|
||||
rtm->rtm_addrs = RTA_DST;
|
||||
rtm->rtm_pid = pid;
|
||||
rtm->rtm_seq = seq;
|
||||
|
||||
/* Copy destination sockaddr after header */
|
||||
memcpy(rtm + 1, dst, dst->sa_len);
|
||||
rtm->rtm_msglen = sizeof(struct rt_msghdr) + SA_RLEN(dst);
|
||||
|
||||
if (write(fd, rtm, rtm->rtm_msglen) < 0) {
|
||||
cmm_print(CMM_LOG_DEBUG, "rtsock: RTM_GET write: %s",
|
||||
strerror(errno));
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Read replies until we find ours */
|
||||
for (;;) {
|
||||
n = read(fd, reply, *replylen);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
cmm_print(CMM_LOG_DEBUG, "rtsock: RTM_GET read: %s",
|
||||
strerror(errno));
|
||||
return (-1);
|
||||
}
|
||||
rtm = (struct rt_msghdr *)reply;
|
||||
if (rtm->rtm_seq == seq && rtm->rtm_pid == pid)
|
||||
break;
|
||||
}
|
||||
|
||||
if (rtm->rtm_errno != 0) {
|
||||
errno = rtm->rtm_errno;
|
||||
return (-1);
|
||||
}
|
||||
|
||||
*replylen = n;
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_rtsock_dispatch(struct cmm_global *g)
|
||||
{
|
||||
char buf[2048];
|
||||
struct rt_msghdr *rtm;
|
||||
int n;
|
||||
|
||||
n = read(g->rtsock_fd, buf, sizeof(buf));
|
||||
if (n < (int)sizeof(struct rt_msghdr))
|
||||
return;
|
||||
|
||||
rtm = (struct rt_msghdr *)buf;
|
||||
|
||||
/* Skip our own messages */
|
||||
if (rtm->rtm_pid == getpid())
|
||||
return;
|
||||
|
||||
switch (rtm->rtm_type) {
|
||||
case RTM_IFINFO:
|
||||
cmm_itf_handle_ifinfo(g, buf, n);
|
||||
break;
|
||||
case RTM_NEWADDR:
|
||||
cmm_itf_handle_newaddr(g, buf, n);
|
||||
break;
|
||||
case RTM_DELADDR:
|
||||
cmm_itf_handle_deladdr(g, buf, n);
|
||||
break;
|
||||
case RTM_ADD:
|
||||
case RTM_DELETE:
|
||||
case RTM_CHANGE:
|
||||
cmm_route_handle_change(g, rtm);
|
||||
break;
|
||||
default:
|
||||
cmm_print(CMM_LOG_TRACE, "rtsock: unhandled msg type %d",
|
||||
rtm->rtm_type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
52
fastpath/cmm/cmm_rtsock.h
Normal file
52
fastpath/cmm/cmm_rtsock.h
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* cmm_rtsock.h — PF_ROUTE socket wrapper
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_RTSOCK_H
|
||||
#define CMM_RTSOCK_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/route.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_dl.h>
|
||||
|
||||
/* Open PF_ROUTE socket, returns fd or -1 */
|
||||
int cmm_rtsock_open(void);
|
||||
|
||||
/* Dispatch pending route socket messages (call when kqueue fires) */
|
||||
void cmm_rtsock_dispatch(struct cmm_global *g);
|
||||
|
||||
/*
|
||||
* Send RTM_GET and read reply.
|
||||
* dst: destination sockaddr (sockaddr_in or sockaddr_in6)
|
||||
* flags: RTF_LLINFO for ARP lookup, 0 for route lookup
|
||||
* reply: buffer for reply (caller provides, min 512 bytes)
|
||||
* replylen: in/out — buffer size / actual reply size
|
||||
* Returns 0 on success, -1 on error.
|
||||
*/
|
||||
int cmm_rtsock_get(int fd, struct sockaddr *dst, int flags,
|
||||
void *reply, size_t *replylen);
|
||||
|
||||
/*
|
||||
* Parse sockaddrs after a route message header.
|
||||
* Fills pointers into the message buffer (no copies).
|
||||
*/
|
||||
struct cmm_rtsock_addrs {
|
||||
struct sockaddr *dst;
|
||||
struct sockaddr *gateway;
|
||||
struct sockaddr *netmask;
|
||||
struct sockaddr_dl *ifp; /* interface name */
|
||||
struct sockaddr *ifa; /* interface address */
|
||||
};
|
||||
|
||||
void cmm_rtsock_parse_addrs(struct rt_msghdr *rtm,
|
||||
struct cmm_rtsock_addrs *addrs);
|
||||
|
||||
/* Sequence number (module-internal, exposed for matching) */
|
||||
extern int cmm_rtsock_seq;
|
||||
|
||||
#endif /* CMM_RTSOCK_H */
|
||||
564
fastpath/cmm/cmm_socket.c
Normal file
564
fastpath/cmm/cmm_socket.c
Normal file
|
|
@ -0,0 +1,564 @@
|
|||
/*
|
||||
* cmm_socket.c — Socket acceleration module
|
||||
*
|
||||
* Command-driven registration of sockets for hardware offload via CDX.
|
||||
* Receives OPEN/CLOSE/UPDATE commands from cmmctl via the CMM control
|
||||
* socket, resolves routes, and programs CDX via FPP_CMD_IPV4/6_SOCK_*.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_socket.h"
|
||||
#include "cmm_ctrl.h"
|
||||
#include "cmm_route.h"
|
||||
#include "cmm_fe.h"
|
||||
|
||||
static struct cmm_socket_list socket_list =
|
||||
STAILQ_HEAD_INITIALIZER(socket_list);
|
||||
static int socket_count;
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Helpers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void
|
||||
ctrl_send_resp(int fd, int16_t rc)
|
||||
{
|
||||
struct cmm_ctrl_resp resp;
|
||||
|
||||
resp.rc = rc;
|
||||
resp.len = 0;
|
||||
write(fd, &resp, sizeof(resp));
|
||||
}
|
||||
|
||||
struct cmm_socket *
|
||||
cmm_socket_find(uint16_t id)
|
||||
{
|
||||
struct cmm_socket *sk;
|
||||
|
||||
STAILQ_FOREACH(sk, &socket_list, entry) {
|
||||
if (sk->id == id)
|
||||
return (sk);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
uint16_t
|
||||
cmm_socket_next_id(void)
|
||||
{
|
||||
uint16_t id;
|
||||
|
||||
for (id = 1; id != 0; id++) {
|
||||
if (cmm_socket_find(id) == NULL)
|
||||
return (id);
|
||||
}
|
||||
return (0); /* exhausted */
|
||||
}
|
||||
|
||||
void
|
||||
cmm_socket_add(struct cmm_socket *sk)
|
||||
{
|
||||
|
||||
STAILQ_INSERT_TAIL(&socket_list, sk, entry);
|
||||
socket_count++;
|
||||
}
|
||||
|
||||
void
|
||||
cmm_socket_remove(struct cmm_socket *sk)
|
||||
{
|
||||
|
||||
STAILQ_REMOVE(&socket_list, sk, cmm_socket, entry);
|
||||
socket_count--;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* FPP command helpers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
int
|
||||
cmm_socket_fpp_open(struct cmm_global *g, struct cmm_socket *sk)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (sk->route == NULL || sk->route->fpp_id == 0)
|
||||
return (-1);
|
||||
|
||||
if (sk->af == AF_INET) {
|
||||
fpp_socket4_open_cmd_t cmd;
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.id = sk->id;
|
||||
cmd.type = sk->type;
|
||||
cmd.mode = sk->mode;
|
||||
memcpy(&cmd.saddr, sk->saddr, 4);
|
||||
memcpy(&cmd.daddr, sk->daddr, 4);
|
||||
cmd.sport = sk->sport;
|
||||
cmd.dport = sk->dport;
|
||||
cmd.proto = sk->proto;
|
||||
cmd.queue = sk->queue;
|
||||
cmd.dscp = sk->dscp;
|
||||
cmd.route_id = sk->route->fpp_id;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV4_SOCK_OPEN,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
|
||||
if (rc == FPP_ERR_SOCK_ALREADY_OPENED_WITH_OTHER_ID ||
|
||||
rc == FPP_ERR_SOCKID_ALREADY_USED) {
|
||||
/* Stale entry — close and retry */
|
||||
fpp_socket4_close_cmd_t ccmd;
|
||||
|
||||
memset(&ccmd, 0, sizeof(ccmd));
|
||||
ccmd.id = sk->id;
|
||||
fci_write(g->fci_handle, FPP_CMD_IPV4_SOCK_CLOSE,
|
||||
sizeof(ccmd), (unsigned short *)&ccmd);
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV4_SOCK_OPEN,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
}
|
||||
} else {
|
||||
fpp_socket6_open_cmd_t cmd;
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.id = sk->id;
|
||||
cmd.type = sk->type;
|
||||
cmd.mode = sk->mode;
|
||||
memcpy(cmd.saddr, sk->saddr, 16);
|
||||
memcpy(cmd.daddr, sk->daddr, 16);
|
||||
cmd.sport = sk->sport;
|
||||
cmd.dport = sk->dport;
|
||||
cmd.proto = sk->proto;
|
||||
cmd.queue = sk->queue;
|
||||
cmd.dscp = sk->dscp;
|
||||
cmd.route_id = sk->route->fpp_id;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV6_SOCK_OPEN,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
|
||||
if (rc == FPP_ERR_SOCK_ALREADY_OPENED_WITH_OTHER_ID ||
|
||||
rc == FPP_ERR_SOCKID_ALREADY_USED) {
|
||||
fpp_socket6_close_cmd_t ccmd;
|
||||
|
||||
memset(&ccmd, 0, sizeof(ccmd));
|
||||
ccmd.id = sk->id;
|
||||
fci_write(g->fci_handle, FPP_CMD_IPV6_SOCK_CLOSE,
|
||||
sizeof(ccmd), (unsigned short *)&ccmd);
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV6_SOCK_OPEN,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
}
|
||||
}
|
||||
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"socket: id=%u open failed: FPP error %d", sk->id, rc);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
sk->fpp_programmed = 1;
|
||||
cmm_print(CMM_LOG_INFO, "socket: id=%u opened (%s %s)",
|
||||
sk->id,
|
||||
sk->af == AF_INET ? "IPv4" : "IPv6",
|
||||
sk->proto == IPPROTO_TCP ? "TCP" : "UDP");
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_socket_fpp_close(struct cmm_global *g, struct cmm_socket *sk)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (!sk->fpp_programmed)
|
||||
return;
|
||||
|
||||
if (sk->af == AF_INET) {
|
||||
fpp_socket4_close_cmd_t cmd;
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.id = sk->id;
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV4_SOCK_CLOSE,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
} else {
|
||||
fpp_socket6_close_cmd_t cmd;
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.id = sk->id;
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV6_SOCK_CLOSE,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
}
|
||||
|
||||
sk->fpp_programmed = 0;
|
||||
cmm_print(CMM_LOG_INFO, "socket: id=%u closed (rc=%d)",
|
||||
sk->id, rc);
|
||||
}
|
||||
|
||||
static int
|
||||
socket_fpp_update(struct cmm_global *g, struct cmm_socket *sk)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (!sk->fpp_programmed || sk->route == NULL)
|
||||
return (-1);
|
||||
|
||||
if (sk->af == AF_INET) {
|
||||
fpp_socket4_update_cmd_t cmd;
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.id = sk->id;
|
||||
memcpy(&cmd.saddr, sk->saddr, 4);
|
||||
cmd.sport = sk->sport;
|
||||
cmd.queue = sk->queue;
|
||||
cmd.dscp = sk->dscp;
|
||||
cmd.route_id = sk->route->fpp_id;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV4_SOCK_UPDATE,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
} else {
|
||||
fpp_socket6_update_cmd_t cmd;
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.id = sk->id;
|
||||
memcpy(cmd.saddr, sk->saddr, 16);
|
||||
cmd.sport = sk->sport;
|
||||
cmd.queue = sk->queue;
|
||||
cmd.dscp = sk->dscp;
|
||||
cmd.route_id = sk->route->fpp_id;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_IPV6_SOCK_UPDATE,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
}
|
||||
|
||||
if (rc != 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"socket: id=%u update failed: FPP error %d",
|
||||
sk->id, rc);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_DEBUG, "socket: id=%u updated", sk->id);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Init / fini */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
int
|
||||
cmm_socket_init(void)
|
||||
{
|
||||
STAILQ_INIT(&socket_list);
|
||||
socket_count = 0;
|
||||
cmm_print(CMM_LOG_INFO, "socket: initialized");
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_socket_fini(struct cmm_global *g)
|
||||
{
|
||||
struct cmm_socket *sk, *tmp;
|
||||
|
||||
STAILQ_FOREACH_SAFE(sk, &socket_list, entry, tmp) {
|
||||
cmm_socket_fpp_close(g, sk);
|
||||
if (sk->route != NULL)
|
||||
cmm_route_put(sk->route);
|
||||
STAILQ_REMOVE(&socket_list, sk, cmm_socket, entry);
|
||||
free(sk);
|
||||
}
|
||||
socket_count = 0;
|
||||
cmm_print(CMM_LOG_INFO, "socket: shutdown");
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Control socket handlers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
void
|
||||
cmm_socket_ctrl_open(struct cmm_global *g, int client_fd,
|
||||
const void *payload, uint16_t len)
|
||||
{
|
||||
const struct cmm_ctrl_socket_open *cmd = payload;
|
||||
struct cmm_socket *sk;
|
||||
int alen;
|
||||
static const uint8_t zeros[16];
|
||||
|
||||
/* Validate payload */
|
||||
if (len < sizeof(*cmd)) {
|
||||
cmm_print(CMM_LOG_WARN, "socket: open: bad payload len %u",
|
||||
len);
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_BAD_LEN);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Validate parameters */
|
||||
if (cmd->id == 0) {
|
||||
cmm_print(CMM_LOG_WARN, "socket: open: invalid id 0");
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD);
|
||||
return;
|
||||
}
|
||||
|
||||
if (cmd->af != AF_INET && cmd->af != AF_INET6) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"socket: open: unsupported af %d", cmd->af);
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD);
|
||||
return;
|
||||
}
|
||||
|
||||
if (cmd->proto != IPPROTO_TCP && cmd->proto != IPPROTO_UDP) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"socket: open: unsupported proto %d", cmd->proto);
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD);
|
||||
return;
|
||||
}
|
||||
|
||||
if (cmd->type != CMM_SOCK_TYPE_LANWAN) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"socket: open: unsupported type %d", cmd->type);
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD);
|
||||
return;
|
||||
}
|
||||
|
||||
alen = (cmd->af == AF_INET) ? 4 : 16;
|
||||
|
||||
/* Destination address is required */
|
||||
if (memcmp(cmd->daddr, zeros, alen) == 0) {
|
||||
cmm_print(CMM_LOG_WARN, "socket: open: daddr required");
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Check for duplicate ID */
|
||||
if (cmm_socket_find(cmd->id) != NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"socket: open: id=%u already exists", cmd->id);
|
||||
ctrl_send_resp(client_fd, FPP_ERR_SOCKID_ALREADY_USED);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Check socket limit */
|
||||
if (socket_count >= CMM_SOCKET_MAX) {
|
||||
cmm_print(CMM_LOG_WARN, "socket: open: limit reached (%d)",
|
||||
CMM_SOCKET_MAX);
|
||||
ctrl_send_resp(client_fd, FPP_ERR_TOO_MANY_SOCKET_OPEN);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Allocate and populate */
|
||||
sk = calloc(1, sizeof(*sk));
|
||||
if (sk == NULL) {
|
||||
cmm_print(CMM_LOG_ERR, "socket: open: malloc failed");
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_FCI_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
sk->id = cmd->id;
|
||||
sk->af = cmd->af;
|
||||
sk->proto = cmd->proto;
|
||||
sk->type = cmd->type;
|
||||
sk->mode = cmd->mode;
|
||||
memcpy(sk->saddr, cmd->saddr, 16);
|
||||
memcpy(sk->daddr, cmd->daddr, 16);
|
||||
sk->sport = cmd->sport;
|
||||
sk->dport = cmd->dport;
|
||||
sk->queue = cmd->queue;
|
||||
sk->dscp = cmd->dscp;
|
||||
|
||||
/* Resolve route to destination */
|
||||
sk->route = cmm_route_get(g, sk->af, sk->daddr);
|
||||
if (sk->route == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"socket: open: id=%u no route to destination", sk->id);
|
||||
free(sk);
|
||||
ctrl_send_resp(client_fd, FPP_ERR_NO_ROUTE_TO_SOCK);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Ensure route is registered in CDX */
|
||||
if (!sk->route->fpp_programmed) {
|
||||
if (sk->route->neigh == NULL ||
|
||||
sk->route->neigh->state != NEIGH_RESOLVED) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"socket: open: id=%u neighbor not resolved",
|
||||
sk->id);
|
||||
cmm_route_put(sk->route);
|
||||
free(sk);
|
||||
ctrl_send_resp(client_fd, FPP_ERR_NO_ROUTE_TO_SOCK);
|
||||
return;
|
||||
}
|
||||
if (cmm_fe_route_register(g, sk->route) < 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"socket: open: id=%u route registration failed",
|
||||
sk->id);
|
||||
cmm_route_put(sk->route);
|
||||
free(sk);
|
||||
ctrl_send_resp(client_fd, FPP_ERR_NO_ROUTE_TO_SOCK);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* Program CDX */
|
||||
if (cmm_socket_fpp_open(g, sk) < 0) {
|
||||
cmm_route_put(sk->route);
|
||||
free(sk);
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_FCI_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Success — add to list */
|
||||
cmm_socket_add(sk);
|
||||
|
||||
{
|
||||
char dbuf[INET6_ADDRSTRLEN];
|
||||
inet_ntop(sk->af, sk->daddr, dbuf, sizeof(dbuf));
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"socket: id=%u registered → %s:%u route_id=%u",
|
||||
sk->id, dbuf, ntohs(sk->dport), sk->route->fpp_id);
|
||||
}
|
||||
|
||||
ctrl_send_resp(client_fd, 0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_socket_ctrl_close(struct cmm_global *g, int client_fd,
|
||||
const void *payload, uint16_t len)
|
||||
{
|
||||
const struct cmm_ctrl_socket_close *cmd = payload;
|
||||
struct cmm_socket *sk;
|
||||
|
||||
if (len < sizeof(*cmd)) {
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_BAD_LEN);
|
||||
return;
|
||||
}
|
||||
|
||||
sk = cmm_socket_find(cmd->id);
|
||||
if (sk == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"socket: close: id=%u not found", cmd->id);
|
||||
ctrl_send_resp(client_fd, FPP_ERR_SOCKID_UNKNOWN);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Deregister from CDX */
|
||||
cmm_socket_fpp_close(g, sk);
|
||||
|
||||
/* Release route reference */
|
||||
if (sk->route != NULL) {
|
||||
cmm_route_put(sk->route);
|
||||
sk->route = NULL;
|
||||
}
|
||||
|
||||
/* Remove from list and free */
|
||||
cmm_socket_remove(sk);
|
||||
free(sk);
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "socket: id=%u removed", cmd->id);
|
||||
ctrl_send_resp(client_fd, 0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_socket_ctrl_update(struct cmm_global *g, int client_fd,
|
||||
const void *payload, uint16_t len)
|
||||
{
|
||||
const struct cmm_ctrl_socket_update *cmd = payload;
|
||||
struct cmm_socket *sk;
|
||||
static const uint8_t zeros[16];
|
||||
|
||||
if (len < sizeof(*cmd)) {
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_BAD_LEN);
|
||||
return;
|
||||
}
|
||||
|
||||
sk = cmm_socket_find(cmd->id);
|
||||
if (sk == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"socket: update: id=%u not found", cmd->id);
|
||||
ctrl_send_resp(client_fd, FPP_ERR_SOCKID_UNKNOWN);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Apply changes (sentinels mean "no change") */
|
||||
if (cmd->sport != 0xFFFF)
|
||||
sk->sport = cmd->sport;
|
||||
if (cmd->queue != 0xFF)
|
||||
sk->queue = cmd->queue;
|
||||
if (cmd->dscp != 0xFFFF)
|
||||
sk->dscp = cmd->dscp;
|
||||
if (memcmp(cmd->saddr, zeros, sizeof(cmd->saddr)) != 0) {
|
||||
memcpy(sk->saddr, cmd->saddr, 16);
|
||||
/* If saddr was set, switch to connected mode */
|
||||
sk->mode = CMM_SOCK_MODE_CONNECTED;
|
||||
}
|
||||
|
||||
/* Send update to CDX */
|
||||
if (socket_fpp_update(g, sk) < 0) {
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_FCI_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "socket: id=%u updated", sk->id);
|
||||
ctrl_send_resp(client_fd, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Route change callback */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
void
|
||||
cmm_socket_route_update(struct cmm_global *g)
|
||||
{
|
||||
struct cmm_socket *sk;
|
||||
int reprogram = 0;
|
||||
|
||||
STAILQ_FOREACH(sk, &socket_list, entry) {
|
||||
if (sk->route == NULL)
|
||||
continue;
|
||||
|
||||
/* Route still valid in CDX — nothing to do */
|
||||
if (sk->route->fpp_programmed && sk->fpp_programmed)
|
||||
continue;
|
||||
|
||||
/* Route was invalidated — deregister socket from CDX */
|
||||
if (sk->fpp_programmed) {
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"socket: id=%u route changed, deregistering",
|
||||
sk->id);
|
||||
cmm_socket_fpp_close(g, sk);
|
||||
}
|
||||
|
||||
/* Re-register route if needed */
|
||||
if (!sk->route->fpp_programmed) {
|
||||
if (sk->route->neigh == NULL ||
|
||||
sk->route->neigh->state != NEIGH_RESOLVED) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"socket: id=%u neighbor not resolved yet",
|
||||
sk->id);
|
||||
continue;
|
||||
}
|
||||
if (cmm_fe_route_register(g, sk->route) < 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"socket: id=%u route re-register failed",
|
||||
sk->id);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/* Re-open socket with updated route */
|
||||
if (cmm_socket_fpp_open(g, sk) == 0) {
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"socket: id=%u reprogrammed with route_id=%u",
|
||||
sk->id, sk->route->fpp_id);
|
||||
reprogram++;
|
||||
}
|
||||
}
|
||||
|
||||
if (reprogram > 0)
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"socket: reprogrammed %d socket(s) after route change",
|
||||
reprogram);
|
||||
}
|
||||
110
fastpath/cmm/cmm_socket.h
Normal file
110
fastpath/cmm/cmm_socket.h
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* cmm_socket.h — Socket acceleration module
|
||||
*
|
||||
* Command-driven registration of sockets for hardware offload.
|
||||
* External tools (cmmctl) register specific sockets via the CMM
|
||||
* control socket; CMM resolves routes and programs CDX.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_SOCKET_H
|
||||
#define CMM_SOCKET_H
|
||||
|
||||
#include <sys/queue.h>
|
||||
#include <sys/socket.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define CMM_SOCKET_MAX 256 /* max concurrent sockets */
|
||||
|
||||
/* Socket types (match CDX control_socket.h) */
|
||||
#define CMM_SOCK_TYPE_LANWAN 0
|
||||
|
||||
/* Socket modes */
|
||||
#define CMM_SOCK_MODE_UNCONNECTED 0 /* 3-tuple: daddr/dport/proto */
|
||||
#define CMM_SOCK_MODE_CONNECTED 1 /* 5-tuple: full */
|
||||
|
||||
struct cmm_global;
|
||||
struct cmm_route;
|
||||
|
||||
struct cmm_socket {
|
||||
STAILQ_ENTRY(cmm_socket) entry;
|
||||
uint16_t id; /* socket ID (1–65535) */
|
||||
sa_family_t af; /* AF_INET / AF_INET6 */
|
||||
uint8_t proto; /* IPPROTO_TCP / IPPROTO_UDP */
|
||||
uint8_t type; /* CMM_SOCK_TYPE_LANWAN */
|
||||
uint8_t mode; /* connected / unconnected */
|
||||
uint8_t saddr[16]; /* source address (net order) */
|
||||
uint8_t daddr[16]; /* destination address (net order) */
|
||||
uint16_t sport; /* source port (net order) */
|
||||
uint16_t dport; /* destination port (net order) */
|
||||
uint8_t queue; /* QoS queue (0–19) */
|
||||
uint16_t dscp; /* DSCP value (0–63) */
|
||||
struct cmm_route *route; /* resolved route for daddr */
|
||||
int fpp_programmed; /* registered in CDX */
|
||||
};
|
||||
|
||||
STAILQ_HEAD(cmm_socket_list, cmm_socket);
|
||||
|
||||
/*
|
||||
* Control protocol payload structures.
|
||||
* Sent from cmmctl → CMM daemon via CMM_CTRL_CMD_SOCKET_* commands.
|
||||
*/
|
||||
struct cmm_ctrl_socket_open {
|
||||
uint16_t id;
|
||||
uint8_t type; /* 0=lanwan */
|
||||
uint8_t mode; /* 0=unconnected, 1=connected */
|
||||
uint8_t af; /* AF_INET=2, AF_INET6=28 */
|
||||
uint8_t proto; /* IPPROTO_TCP=6, IPPROTO_UDP=17 */
|
||||
uint16_t sport; /* network byte order, 0=any */
|
||||
uint16_t dport; /* network byte order */
|
||||
uint8_t queue; /* QoS queue (0=default) */
|
||||
uint8_t pad1;
|
||||
uint16_t dscp; /* DSCP (0=default) */
|
||||
uint16_t pad2;
|
||||
uint8_t saddr[16]; /* source address */
|
||||
uint8_t daddr[16]; /* destination address */
|
||||
};
|
||||
|
||||
struct cmm_ctrl_socket_close {
|
||||
uint16_t id;
|
||||
uint16_t pad;
|
||||
};
|
||||
|
||||
struct cmm_ctrl_socket_update {
|
||||
uint16_t id;
|
||||
uint16_t sport; /* 0xFFFF = no change */
|
||||
uint8_t queue; /* 0xFF = no change */
|
||||
uint8_t pad1;
|
||||
uint16_t dscp; /* 0xFFFF = no change */
|
||||
uint8_t saddr[16]; /* all-zeros = no change */
|
||||
};
|
||||
|
||||
int cmm_socket_init(void);
|
||||
void cmm_socket_fini(struct cmm_global *g);
|
||||
|
||||
/* Control socket handlers */
|
||||
void cmm_socket_ctrl_open(struct cmm_global *g, int client_fd,
|
||||
const void *payload, uint16_t len);
|
||||
void cmm_socket_ctrl_close(struct cmm_global *g, int client_fd,
|
||||
const void *payload, uint16_t len);
|
||||
void cmm_socket_ctrl_update(struct cmm_global *g, int client_fd,
|
||||
const void *payload, uint16_t len);
|
||||
|
||||
/* Called from cmm_route_handle_change() after route invalidation */
|
||||
void cmm_socket_route_update(struct cmm_global *g);
|
||||
|
||||
/*
|
||||
* Internal APIs for use by other CMM modules (L2TP, etc.).
|
||||
* These allow modules to manage sockets programmatically without
|
||||
* going through the external control socket protocol.
|
||||
*/
|
||||
struct cmm_socket *cmm_socket_find(uint16_t id);
|
||||
uint16_t cmm_socket_next_id(void);
|
||||
void cmm_socket_add(struct cmm_socket *sk);
|
||||
void cmm_socket_remove(struct cmm_socket *sk);
|
||||
int cmm_socket_fpp_open(struct cmm_global *g, struct cmm_socket *sk);
|
||||
void cmm_socket_fpp_close(struct cmm_global *g, struct cmm_socket *sk);
|
||||
|
||||
#endif /* CMM_SOCKET_H */
|
||||
432
fastpath/cmm/cmm_tunnel.c
Normal file
432
fastpath/cmm/cmm_tunnel.c
Normal file
|
|
@ -0,0 +1,432 @@
|
|||
/*
|
||||
* cmm_tunnel.c — Tunnel interface offload
|
||||
*
|
||||
* Detects gif(4) and gre(4) tunnel interfaces, resolves routes to
|
||||
* remote endpoints, and registers tunnels with CDX via
|
||||
* FPP_CMD_TUNNEL_ADD/DEL for hardware offload.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet6/in6_var.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_tunnel.h"
|
||||
#include "cmm_itf.h"
|
||||
#include "cmm_route.h"
|
||||
#include "cmm_fe.h"
|
||||
#include "cmm_ctrl.h"
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Tunnel detection (called from cmm_itf.c) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/*
|
||||
* Probe an interface for tunnel membership.
|
||||
* Checks name prefix (gif/gre) and retrieves tunnel endpoints via ioctl.
|
||||
* On success, populates tunnel fields and sets ITF_F_TUNNEL.
|
||||
*/
|
||||
void
|
||||
itf_detect_tunnel(struct cmm_interface *itf, int sd)
|
||||
{
|
||||
struct ifreq ifr;
|
||||
struct sockaddr_in *sin;
|
||||
int is_gif, is_gre;
|
||||
|
||||
is_gif = (strncmp(itf->ifname, "gif", 3) == 0);
|
||||
is_gre = (strncmp(itf->ifname, "gre", 3) == 0);
|
||||
|
||||
if (!is_gif && !is_gre)
|
||||
return;
|
||||
|
||||
/*
|
||||
* Try IPv4 tunnel endpoints first (SIOCGIFPSRCADDR).
|
||||
* If that fails or returns AF_UNSPEC, try IPv6.
|
||||
*/
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strlcpy(ifr.ifr_name, itf->ifname, sizeof(ifr.ifr_name));
|
||||
|
||||
if (ioctl(sd, SIOCGIFPSRCADDR, &ifr) == 0 &&
|
||||
ifr.ifr_addr.sa_family == AF_INET) {
|
||||
/* IPv4 outer tunnel */
|
||||
sin = (struct sockaddr_in *)&ifr.ifr_addr;
|
||||
memcpy(itf->tnl_local, &sin->sin_addr, 4);
|
||||
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strlcpy(ifr.ifr_name, itf->ifname, sizeof(ifr.ifr_name));
|
||||
if (ioctl(sd, SIOCGIFPDSTADDR, &ifr) < 0)
|
||||
return;
|
||||
sin = (struct sockaddr_in *)&ifr.ifr_addr;
|
||||
memcpy(itf->tnl_remote, &sin->sin_addr, 4);
|
||||
|
||||
itf->tnl_family = AF_INET;
|
||||
|
||||
if (is_gif) {
|
||||
itf->tnl_mode = TNL_6O4;
|
||||
} else {
|
||||
/* GRE over IPv4 — no CDX mode available */
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"tunnel: %s gre/IPv4 not supported by CDX",
|
||||
itf->ifname);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
/* Try IPv6 outer tunnel */
|
||||
struct in6_ifreq ifr6;
|
||||
struct sockaddr_in6 *sin6;
|
||||
|
||||
memset(&ifr6, 0, sizeof(ifr6));
|
||||
strlcpy(ifr6.ifr_name, itf->ifname, sizeof(ifr6.ifr_name));
|
||||
|
||||
if (ioctl(sd, SIOCGIFPSRCADDR_IN6, &ifr6) < 0)
|
||||
return;
|
||||
if (ifr6.ifr_addr.sin6_family != AF_INET6)
|
||||
return;
|
||||
|
||||
sin6 = &ifr6.ifr_addr;
|
||||
memcpy(itf->tnl_local, &sin6->sin6_addr, 16);
|
||||
|
||||
memset(&ifr6, 0, sizeof(ifr6));
|
||||
strlcpy(ifr6.ifr_name, itf->ifname, sizeof(ifr6.ifr_name));
|
||||
if (ioctl(sd, SIOCGIFPDSTADDR_IN6, &ifr6) < 0)
|
||||
return;
|
||||
sin6 = &ifr6.ifr_addr;
|
||||
memcpy(itf->tnl_remote, &sin6->sin6_addr, 16);
|
||||
|
||||
itf->tnl_family = AF_INET6;
|
||||
|
||||
if (is_gif)
|
||||
itf->tnl_mode = TNL_4O6;
|
||||
else
|
||||
itf->tnl_mode = TNL_GRE_IPV6;
|
||||
}
|
||||
|
||||
itf->itf_flags |= ITF_F_TUNNEL;
|
||||
|
||||
if (itf->tnl_family == AF_INET) {
|
||||
char lbuf[INET_ADDRSTRLEN], rbuf[INET_ADDRSTRLEN];
|
||||
|
||||
inet_ntop(AF_INET, itf->tnl_local, lbuf, sizeof(lbuf));
|
||||
inet_ntop(AF_INET, itf->tnl_remote, rbuf, sizeof(rbuf));
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"tunnel: %s mode=%u (%s) local=%s remote=%s",
|
||||
itf->ifname, itf->tnl_mode,
|
||||
itf->tnl_mode == TNL_6O4 ? "6o4" : "?",
|
||||
lbuf, rbuf);
|
||||
} else {
|
||||
char lbuf[INET6_ADDRSTRLEN], rbuf[INET6_ADDRSTRLEN];
|
||||
|
||||
inet_ntop(AF_INET6, itf->tnl_local, lbuf, sizeof(lbuf));
|
||||
inet_ntop(AF_INET6, itf->tnl_remote, rbuf, sizeof(rbuf));
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"tunnel: %s mode=%u (%s) local=%s remote=%s",
|
||||
itf->ifname, itf->tnl_mode,
|
||||
itf->tnl_mode == TNL_4O6 ? "4o6" :
|
||||
itf->tnl_mode == TNL_GRE_IPV6 ? "gre6" : "?",
|
||||
lbuf, rbuf);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* CDX registration */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int
|
||||
cmm_tunnel_register(struct cmm_global *g, struct cmm_interface *itf)
|
||||
{
|
||||
fpp_tunnel_create_cmd_t cmd;
|
||||
struct cmm_route *rt;
|
||||
int alen, rc;
|
||||
|
||||
if (!(itf->itf_flags & ITF_F_TUNNEL))
|
||||
return (0);
|
||||
if (itf->itf_flags & ITF_F_FPP_TNL)
|
||||
return (0);
|
||||
|
||||
alen = (itf->tnl_family == AF_INET) ? 4 : 16;
|
||||
|
||||
/* Verify endpoints are configured */
|
||||
{
|
||||
uint8_t zero[16] = {0};
|
||||
|
||||
if (memcmp(itf->tnl_remote, zero, alen) == 0) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"tunnel: %s remote endpoint not configured",
|
||||
itf->ifname);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Resolve route to remote endpoint */
|
||||
rt = cmm_route_get(g, itf->tnl_family, itf->tnl_remote);
|
||||
if (rt == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"tunnel: %s no route to remote endpoint",
|
||||
itf->ifname);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Ensure route is programmed in CDX */
|
||||
if (!rt->fpp_programmed) {
|
||||
if (cmm_route_send_fpp(g, rt, 0) < 0) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"tunnel: %s route not ready (neighbor?)",
|
||||
itf->ifname);
|
||||
cmm_route_put(rt);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Build FPP_CMD_TUNNEL_ADD */
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
strlcpy(cmd.name, itf->ifname, sizeof(cmd.name));
|
||||
memcpy(cmd.local, itf->tnl_local, alen);
|
||||
memcpy(cmd.remote, itf->tnl_remote, alen);
|
||||
cmd.mode = itf->tnl_mode;
|
||||
cmd.secure = 0;
|
||||
cmd.encap_limit = 0;
|
||||
cmd.hop_limit = 64;
|
||||
cmd.flow_info = 0;
|
||||
cmd.frag_off = 0;
|
||||
cmd.enabled = 1;
|
||||
cmd.route_id = rt->fpp_id;
|
||||
cmd.mtu = itf->mtu;
|
||||
cmd.tunnel_flags = 0;
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_TUNNEL_ADD,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
if (rc == FPP_ERR_TNL_ALREADY_CREATED) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"tunnel: %s already in CDX, reusing", itf->ifname);
|
||||
} else if (rc != 0) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"tunnel: register %s failed: %d", itf->ifname, rc);
|
||||
cmm_route_put(rt);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
itf->itf_flags |= ITF_F_FPP_TNL;
|
||||
itf->tnl_route = rt;
|
||||
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"tunnel: registered %s (mode=%u route_id=%u mtu=%u)",
|
||||
itf->ifname, itf->tnl_mode, rt->fpp_id, itf->mtu);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
cmm_tunnel_deregister(struct cmm_global *g, struct cmm_interface *itf)
|
||||
{
|
||||
fpp_tunnel_del_cmd_t cmd;
|
||||
int rc;
|
||||
|
||||
if (!(itf->itf_flags & ITF_F_FPP_TNL))
|
||||
return (0);
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
strlcpy(cmd.name, itf->ifname, sizeof(cmd.name));
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_TUNNEL_DEL,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
if (rc != 0 && rc != FPP_ERR_TNL_ENTRY_NOT_FOUND)
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"tunnel: deregister %s failed: %d", itf->ifname, rc);
|
||||
|
||||
itf->itf_flags &= ~ITF_F_FPP_TNL;
|
||||
|
||||
if (itf->tnl_route != NULL) {
|
||||
cmm_route_put(itf->tnl_route);
|
||||
itf->tnl_route = NULL;
|
||||
}
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "tunnel: deregistered %s", itf->ifname);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* foreach callbacks (must be defined before callers) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int
|
||||
tunnel_register_if_up(struct cmm_global *g, struct cmm_interface *itf)
|
||||
{
|
||||
|
||||
if ((itf->flags & IFF_UP) && !(itf->itf_flags & ITF_F_FPP_TNL))
|
||||
cmm_tunnel_register(g, itf);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
tunnel_deregister_wrap(struct cmm_global *g, struct cmm_interface *itf)
|
||||
{
|
||||
|
||||
cmm_tunnel_deregister(g, itf);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
tunnel_recheck(struct cmm_global *g, struct cmm_interface *itf)
|
||||
{
|
||||
|
||||
if (!(itf->flags & IFF_UP))
|
||||
return (0);
|
||||
|
||||
if (itf->itf_flags & ITF_F_FPP_TNL) {
|
||||
/*
|
||||
* Already registered — route may have changed.
|
||||
* Deregister and re-register to pick up the new route.
|
||||
*/
|
||||
cmm_tunnel_deregister(g, itf);
|
||||
cmm_tunnel_register(g, itf);
|
||||
} else {
|
||||
/* Not yet registered — route may now be available */
|
||||
cmm_tunnel_register(g, itf);
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Event handlers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
void
|
||||
cmm_tunnel_notify(struct cmm_global *g, struct cmm_interface *itf)
|
||||
{
|
||||
|
||||
if (!(itf->itf_flags & ITF_F_TUNNEL))
|
||||
return;
|
||||
|
||||
if ((itf->flags & IFF_UP) && !(itf->itf_flags & ITF_F_FPP_TNL))
|
||||
cmm_tunnel_register(g, itf);
|
||||
else if (!(itf->flags & IFF_UP) && (itf->itf_flags & ITF_F_FPP_TNL))
|
||||
cmm_tunnel_deregister(g, itf);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_tunnel_route_update(struct cmm_global *g)
|
||||
{
|
||||
|
||||
cmm_itf_foreach_tunnel(g, tunnel_recheck);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Init / Fini */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
int
|
||||
cmm_tunnel_init(struct cmm_global *g)
|
||||
{
|
||||
|
||||
/* Register all existing UP tunnel interfaces */
|
||||
cmm_itf_foreach_tunnel(g, tunnel_register_if_up);
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "tunnel: initialized");
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_tunnel_fini(struct cmm_global *g)
|
||||
{
|
||||
|
||||
cmm_itf_foreach_tunnel(g, tunnel_deregister_wrap);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Control socket handlers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void
|
||||
ctrl_send_resp(int fd, int16_t rc)
|
||||
{
|
||||
struct cmm_ctrl_resp resp;
|
||||
|
||||
resp.rc = rc;
|
||||
resp.len = 0;
|
||||
write(fd, &resp, sizeof(resp));
|
||||
}
|
||||
|
||||
void
|
||||
cmm_tunnel_ctrl_add(struct cmm_global *g, int client_fd,
|
||||
const void *payload, uint16_t len)
|
||||
{
|
||||
char name[IFNAMSIZ];
|
||||
struct cmm_interface *itf;
|
||||
int rc;
|
||||
|
||||
if (len < 1 || len > IFNAMSIZ) {
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_BAD_LEN);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(name, 0, sizeof(name));
|
||||
memcpy(name, payload, len);
|
||||
name[IFNAMSIZ - 1] = '\0';
|
||||
|
||||
itf = cmm_itf_find_by_name(name);
|
||||
if (itf == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"tunnel: ctrl add: interface '%s' not found", name);
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD);
|
||||
return;
|
||||
}
|
||||
|
||||
/* If not yet detected as tunnel, try detection */
|
||||
if (!(itf->itf_flags & ITF_F_TUNNEL)) {
|
||||
int sd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sd >= 0) {
|
||||
itf_detect_tunnel(itf, sd);
|
||||
close(sd);
|
||||
}
|
||||
}
|
||||
|
||||
if (!(itf->itf_flags & ITF_F_TUNNEL)) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"tunnel: ctrl add: %s is not a tunnel", name);
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD);
|
||||
return;
|
||||
}
|
||||
|
||||
rc = cmm_tunnel_register(g, itf);
|
||||
ctrl_send_resp(client_fd, (rc == 0) ? 0 : CMM_CTRL_ERR_FCI_FAIL);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_tunnel_ctrl_del(struct cmm_global *g, int client_fd,
|
||||
const void *payload, uint16_t len)
|
||||
{
|
||||
char name[IFNAMSIZ];
|
||||
struct cmm_interface *itf;
|
||||
|
||||
if (len < 1 || len > IFNAMSIZ) {
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_BAD_LEN);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(name, 0, sizeof(name));
|
||||
memcpy(name, payload, len);
|
||||
name[IFNAMSIZ - 1] = '\0';
|
||||
|
||||
itf = cmm_itf_find_by_name(name);
|
||||
if (itf == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"tunnel: ctrl del: interface '%s' not found", name);
|
||||
ctrl_send_resp(client_fd, CMM_CTRL_ERR_UNKNOWN_CMD);
|
||||
return;
|
||||
}
|
||||
|
||||
cmm_tunnel_deregister(g, itf);
|
||||
ctrl_send_resp(client_fd, 0);
|
||||
}
|
||||
33
fastpath/cmm/cmm_tunnel.h
Normal file
33
fastpath/cmm/cmm_tunnel.h
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* cmm_tunnel.h — Tunnel interface offload
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_TUNNEL_H
|
||||
#define CMM_TUNNEL_H
|
||||
|
||||
#include "cmm.h"
|
||||
|
||||
struct cmm_interface;
|
||||
|
||||
/* Initialize tunnel subsystem — registers existing UP tunnels */
|
||||
int cmm_tunnel_init(struct cmm_global *g);
|
||||
|
||||
/* Deregister all tunnels from CDX */
|
||||
void cmm_tunnel_fini(struct cmm_global *g);
|
||||
|
||||
/* Called when interface flags change — register/deregister as needed */
|
||||
void cmm_tunnel_notify(struct cmm_global *g, struct cmm_interface *itf);
|
||||
|
||||
/* Called on route changes — re-evaluate pending/active tunnels */
|
||||
void cmm_tunnel_route_update(struct cmm_global *g);
|
||||
|
||||
/* Control socket handlers for CMM_CTRL_CMD_TNL_ADD/DEL */
|
||||
void cmm_tunnel_ctrl_add(struct cmm_global *g, int client_fd,
|
||||
const void *payload, uint16_t len);
|
||||
void cmm_tunnel_ctrl_del(struct cmm_global *g, int client_fd,
|
||||
const void *payload, uint16_t len);
|
||||
|
||||
#endif /* CMM_TUNNEL_H */
|
||||
135
fastpath/cmm/cmm_vlan.c
Normal file
135
fastpath/cmm/cmm_vlan.c
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
/*
|
||||
* cmm_vlan.c — VLAN interface offload
|
||||
*
|
||||
* Detects 802.1Q VLAN interfaces and registers them with CDX
|
||||
* via FPP_CMD_VLAN_ENTRY so the hardware knows the VLAN-to-port
|
||||
* mapping for flow classification.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <net/if.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cmm.h"
|
||||
#include "cmm_vlan.h"
|
||||
#include "cmm_itf.h"
|
||||
|
||||
static int
|
||||
cmm_vlan_register(struct cmm_global *g, struct cmm_interface *itf)
|
||||
{
|
||||
fpp_vlan_cmd_t cmd;
|
||||
struct cmm_interface *parent;
|
||||
int rc;
|
||||
|
||||
if (!(itf->itf_flags & ITF_F_VLAN))
|
||||
return (0);
|
||||
if (itf->itf_flags & ITF_F_FPP_VLAN)
|
||||
return (0);
|
||||
|
||||
parent = cmm_itf_find_by_index(itf->parent_ifindex);
|
||||
if (parent == NULL) {
|
||||
cmm_print(CMM_LOG_WARN,
|
||||
"vlan: %s parent idx=%d not found",
|
||||
itf->ifname, itf->parent_ifindex);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.action = FPP_ACTION_REGISTER;
|
||||
cmd.vlan_id = itf->vlan_id;
|
||||
strlcpy(cmd.vlan_ifname, itf->ifname, sizeof(cmd.vlan_ifname));
|
||||
strlcpy(cmd.vlan_phy_ifname, parent->ifname,
|
||||
sizeof(cmd.vlan_phy_ifname));
|
||||
#if defined(LS1043)
|
||||
memcpy(cmd.macaddr, itf->macaddr, ETHER_ADDR_LEN);
|
||||
#endif
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_VLAN_ENTRY,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
if (rc == FPP_ERR_VLAN_ENTRY_ALREADY_REGISTERED) {
|
||||
cmm_print(CMM_LOG_DEBUG,
|
||||
"vlan: %s already in CDX, reusing", itf->ifname);
|
||||
} else if (rc != 0) {
|
||||
cmm_print(CMM_LOG_WARN, "vlan: register %s failed: %d",
|
||||
itf->ifname, rc);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
itf->itf_flags |= ITF_F_FPP_VLAN;
|
||||
cmm_print(CMM_LOG_INFO,
|
||||
"vlan: registered %s (vid=%u parent=%s)",
|
||||
itf->ifname, itf->vlan_id, parent->ifname);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
cmm_vlan_deregister(struct cmm_global *g, struct cmm_interface *itf)
|
||||
{
|
||||
fpp_vlan_cmd_t cmd;
|
||||
struct cmm_interface *parent;
|
||||
int rc;
|
||||
|
||||
if (!(itf->itf_flags & ITF_F_FPP_VLAN))
|
||||
return (0);
|
||||
|
||||
parent = cmm_itf_find_by_index(itf->parent_ifindex);
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.action = FPP_ACTION_DEREGISTER;
|
||||
cmd.vlan_id = itf->vlan_id;
|
||||
strlcpy(cmd.vlan_ifname, itf->ifname, sizeof(cmd.vlan_ifname));
|
||||
if (parent != NULL)
|
||||
strlcpy(cmd.vlan_phy_ifname, parent->ifname,
|
||||
sizeof(cmd.vlan_phy_ifname));
|
||||
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_VLAN_ENTRY,
|
||||
sizeof(cmd), (unsigned short *)&cmd);
|
||||
if (rc != 0 && rc != FPP_ERR_VLAN_ENTRY_NOT_FOUND)
|
||||
cmm_print(CMM_LOG_WARN, "vlan: deregister %s failed: %d",
|
||||
itf->ifname, rc);
|
||||
|
||||
itf->itf_flags &= ~ITF_F_FPP_VLAN;
|
||||
cmm_print(CMM_LOG_INFO, "vlan: deregistered %s (vid=%u)",
|
||||
itf->ifname, itf->vlan_id);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cmm_vlan_init(struct cmm_global *g)
|
||||
{
|
||||
int rc;
|
||||
|
||||
/* Reset all VLAN entries in CDX */
|
||||
rc = fci_write(g->fci_handle, FPP_CMD_VLAN_RESET, 0, NULL);
|
||||
if (rc != 0)
|
||||
cmm_print(CMM_LOG_WARN, "vlan: reset failed: %d", rc);
|
||||
|
||||
/* Register all existing UP VLAN interfaces */
|
||||
cmm_itf_foreach_vlan(g, cmm_vlan_register);
|
||||
|
||||
cmm_print(CMM_LOG_INFO, "vlan: initialized");
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_vlan_fini(struct cmm_global *g)
|
||||
{
|
||||
cmm_itf_foreach_vlan(g, cmm_vlan_deregister);
|
||||
}
|
||||
|
||||
void
|
||||
cmm_vlan_notify(struct cmm_global *g, struct cmm_interface *itf)
|
||||
{
|
||||
if (!(itf->itf_flags & ITF_F_VLAN))
|
||||
return;
|
||||
|
||||
if ((itf->flags & IFF_UP) && !(itf->itf_flags & ITF_F_FPP_VLAN))
|
||||
cmm_vlan_register(g, itf);
|
||||
else if (!(itf->flags & IFF_UP) && (itf->itf_flags & ITF_F_FPP_VLAN))
|
||||
cmm_vlan_deregister(g, itf);
|
||||
}
|
||||
24
fastpath/cmm/cmm_vlan.h
Normal file
24
fastpath/cmm/cmm_vlan.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* cmm_vlan.h — VLAN interface offload
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMM_VLAN_H
|
||||
#define CMM_VLAN_H
|
||||
|
||||
#include "cmm.h"
|
||||
|
||||
struct cmm_interface;
|
||||
|
||||
/* Reset CDX VLAN table and register all existing UP VLANs */
|
||||
int cmm_vlan_init(struct cmm_global *g);
|
||||
|
||||
/* Deregister all VLANs from CDX */
|
||||
void cmm_vlan_fini(struct cmm_global *g);
|
||||
|
||||
/* Called when interface flags change — register/deregister as needed */
|
||||
void cmm_vlan_notify(struct cmm_global *g, struct cmm_interface *itf);
|
||||
|
||||
#endif /* CMM_VLAN_H */
|
||||
2280
fastpath/cmm/fpp.h
Normal file
2280
fastpath/cmm/fpp.h
Normal file
File diff suppressed because it is too large
Load diff
80
fastpath/cmm/fpp_private.h
Normal file
80
fastpath/cmm/fpp_private.h
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
*
|
||||
* Copyright (C) 2010 Mindspeed Technologies, Inc.
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017,2021 NXP
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
* The GPL-2.0+ license for this file can be found in the COPYING.GPL file
|
||||
* included with this distribution or at http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
*
|
||||
*/
|
||||
#ifndef __FPP_PRIVATE__
|
||||
#define __FPP_PRIVATE__
|
||||
|
||||
/*----------------------------------------- IPV4 -----------------------------*/
|
||||
#define FPP_CMD_IPV4_CONNTRACK_CHANGE 0x0315
|
||||
#define FPP_CMD_IPV4_SOCK_OPEN 0x0330
|
||||
#define FPP_CMD_IPV4_SOCK_CLOSE 0x0331
|
||||
#define FPP_CMD_IPV4_SOCK_UPDATE 0x0332
|
||||
|
||||
/*----------------------------------------- IPV6 -----------------------------*/
|
||||
#define FPP_CMD_IPV6_CONNTRACK_CHANGE 0x0415
|
||||
#define FPP_CMD_IPV6_SOCK_OPEN 0x0430
|
||||
#define FPP_CMD_IPV6_SOCK_CLOSE 0x0431
|
||||
#define FPP_CMD_IPV6_SOCK_UPDATE 0x0432
|
||||
|
||||
|
||||
/*------------- CtEntry/RtEntry/FLowEntry Flags ------------------------------*/
|
||||
#define FPP_PROGRAMMED (1 << 0)
|
||||
#define FPP_NEEDS_UPDATE (1 << 1)
|
||||
/*------------- FlowEntry Flags ----------------------------------------------*/
|
||||
#ifndef IPSEC_NO_FLOW_CACHE
|
||||
#define FLOW_NO_SA (1 << 2)
|
||||
#endif
|
||||
/*------------- CtEntry/RtEntry Flags ----------------------------------------*/
|
||||
#define INVALID (1 << 3)
|
||||
#define RT_POLICY (1 << 4)
|
||||
#define USER_ADDED (1 << 5)
|
||||
#define CHECK_BRIDGE_PORT (1 << 6)
|
||||
#define NEEDS_SOLICIT (1 << 7)
|
||||
|
||||
#define LOCAL_CONN_ORIG (1 << 8)
|
||||
#define LOCAL_CONN_REPL (1 << 9)
|
||||
#define LOCAL_CONN (LOCAL_CONN_ORIG | LOCAL_CONN_REPL)
|
||||
|
||||
#define IS_LOCAL (1 << 10)
|
||||
#ifdef IPSEC_NO_FLOW_CACHE
|
||||
#define FLOW_NO_ORIG_SA (1 << 11)
|
||||
#define FLOW_NO_REPL_SA (1 << 12)
|
||||
|
||||
#define SA_DELETE (1 << 13)
|
||||
#endif
|
||||
#define SENT_PERMANENT_INFO (1<<14)
|
||||
|
||||
#define ADD (1 << 0)
|
||||
#define UPDATE (1 << 1)
|
||||
#define REMOVE (1 << 2)
|
||||
#define ENABLE (1 << 3)
|
||||
#define DISABLE (1 << 4)
|
||||
|
||||
#define IPADDRLEN(family) ((family) == AF_INET ? 4 : 16)
|
||||
|
||||
/* CMM tunnel entry states */
|
||||
#define TNL_IPSEC (1 << 0)
|
||||
#define TNL_6RD (1 << 1)
|
||||
#define TNL_4RD (1 << 2)
|
||||
|
||||
#ifndef IPPROTO_ETHERIP
|
||||
#define IPPROTO_ETHERIP 97
|
||||
#endif
|
||||
|
||||
#define TNL_ETHIPOIP6 0
|
||||
#define TNL_6O4 1
|
||||
#define TNL_4O6 2
|
||||
#define TNL_ETHIPOIP4 3
|
||||
#define TNL_GRE_IPV6 4
|
||||
|
||||
#endif
|
||||
|
||||
105
fastpath/cmm/jhash.h
Normal file
105
fastpath/cmm/jhash.h
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
/* jhash.h: Jenkins hash support.
|
||||
*
|
||||
* Copyright (C) 1996 Bob Jenkins (bob_jenkins@bur tleburtle.net)
|
||||
*
|
||||
* http://burtleburtle.net/bob/hash/
|
||||
*
|
||||
* These are the credits from Bob's sources:
|
||||
*
|
||||
* lookup2.c, by Bob Jenkins, December 1996, Public Domain.
|
||||
* hash(), hash2(), hash3, and mix() are externally useful functions.
|
||||
* Routines to test the hash are included if SELF_TEST is defined.
|
||||
* You can use this free for any purpose. It has no warranty.
|
||||
*
|
||||
* Copyright (C) 2003 David S. Miller (davem@redhat.com)
|
||||
*
|
||||
* I've modified Bob's hash to be useful in the Linux kernel, and
|
||||
* any bugs present are surely my fault. -DaveM
|
||||
*/
|
||||
|
||||
#ifndef __JHASH_H
|
||||
#define __JHASH_H
|
||||
|
||||
#define __jhash_mix(a, b, c) \
|
||||
{ \
|
||||
a -= b; a -= c; a ^= (c>>13); \
|
||||
b -= c; b -= a; b ^= (a<<8); \
|
||||
c -= a; c -= b; c ^= (b>>13); \
|
||||
a -= b; a -= c; a ^= (c>>12); \
|
||||
b -= c; b -= a; b ^= (a<<16); \
|
||||
c -= a; c -= b; c ^= (b>>5); \
|
||||
a -= b; a -= c; a ^= (c>>3); \
|
||||
b -= c; b -= a; b ^= (a<<10); \
|
||||
c -= a; c -= b; c ^= (b>>15); \
|
||||
}
|
||||
|
||||
|
||||
/* The golden ration: an arbitrary value */
|
||||
#define JHASH_GOLDEN_RATIO 0x9e3779b9
|
||||
|
||||
/* The most generic version, hashes an arbitrary sequence
|
||||
* of bytes. No alignment or length assumptions are made about
|
||||
* the input key.
|
||||
*/
|
||||
static inline u_int32_t jhash(const void *key, u_int32_t length, u_int32_t initval)
|
||||
{
|
||||
u_int32_t a, b, c, len;
|
||||
const u_int8_t *k = key;
|
||||
|
||||
len = length;
|
||||
a = b = JHASH_GOLDEN_RATIO;
|
||||
c = initval;
|
||||
|
||||
while (len >= 12) {
|
||||
a += (k[0] +((u_int32_t)k[1]<<8) +((u_int32_t)k[2]<<16) +((u_int32_t)k[3]<<24));
|
||||
b += (k[4] +((u_int32_t)k[5]<<8) +((u_int32_t)k[6]<<16) +((u_int32_t)k[7]<<24));
|
||||
c += (k[8] +((u_int32_t)k[9]<<8) +((u_int32_t)k[10]<<16)+((u_int32_t)k[11]<<24));
|
||||
|
||||
__jhash_mix(a,b,c);
|
||||
|
||||
k += 12;
|
||||
len -= 12;
|
||||
}
|
||||
|
||||
c += length;
|
||||
switch (len) {
|
||||
case 11: c += ((u_int32_t)k[10]<<24);
|
||||
case 10: c += ((u_int32_t)k[9]<<16);
|
||||
case 9 : c += ((u_int32_t)k[8]<<8);
|
||||
case 8 : b += ((u_int32_t)k[7]<<24);
|
||||
case 7 : b += ((u_int32_t)k[6]<<16);
|
||||
case 6 : b += ((u_int32_t)k[5]<<8);
|
||||
case 5 : b += k[4];
|
||||
case 4 : a += ((u_int32_t)k[3]<<24);
|
||||
case 3 : a += ((u_int32_t)k[2]<<16);
|
||||
case 2 : a += ((u_int32_t)k[1]<<8);
|
||||
case 1 : a += k[0];
|
||||
};
|
||||
|
||||
__jhash_mix(a,b,c);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
static inline u_int32_t jhash_3words(u_int32_t a, u_int32_t b, u_int32_t c, u_int32_t initval)
|
||||
{
|
||||
a += JHASH_GOLDEN_RATIO;
|
||||
b += JHASH_GOLDEN_RATIO;
|
||||
c += initval;
|
||||
|
||||
__jhash_mix(a, b, c);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
static inline u_int32_t jhash_2words(u_int32_t a, u_int32_t b, u_int32_t initval)
|
||||
{
|
||||
return jhash_3words(a, b, 0, initval);
|
||||
}
|
||||
|
||||
static inline u_int32_t jhash_1word(u_int32_t a, u_int32_t initval)
|
||||
{
|
||||
return jhash_3words(a, 0, 0, initval);
|
||||
}
|
||||
|
||||
#endif
|
||||
62
fastpath/cmm/list.h
Normal file
62
fastpath/cmm/list.h
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
*
|
||||
* Copyright (C) 2009 Mindspeed Technologies, Inc.
|
||||
* Copyright 2014-2016 Freescale Semiconductor, Inc.
|
||||
* Copyright 2017,2021 NXP
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
* The GPL-2.0+ license for this file can be found in the COPYING.GPL file
|
||||
* included with this distribution or at http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __LIST_H
|
||||
#define __LIST_H
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
struct list_head {
|
||||
struct list_head *prev;
|
||||
struct list_head *next;
|
||||
};
|
||||
|
||||
static inline void list_head_init(struct list_head *head)
|
||||
{
|
||||
head->prev = head;
|
||||
head->next = head;
|
||||
}
|
||||
|
||||
static inline void list_add(struct list_head *head, struct list_head *entry)
|
||||
{
|
||||
assert((!entry->next) && (!entry->prev));
|
||||
|
||||
entry->next = head->next;
|
||||
entry->next->prev = entry;
|
||||
|
||||
head->next = entry;
|
||||
entry->prev = head;
|
||||
}
|
||||
|
||||
static inline void list_del(struct list_head *entry)
|
||||
{
|
||||
if((!entry->next) && (!entry->prev))
|
||||
return;
|
||||
assert((entry->next) && (entry->prev));
|
||||
|
||||
entry->prev->next = entry->next;
|
||||
entry->next->prev = entry->prev;
|
||||
entry->prev = NULL;
|
||||
entry->next = NULL;
|
||||
}
|
||||
|
||||
#define list_empty(head) ((head)->next == (head))
|
||||
|
||||
#define offset_of(type, member) ((unsigned long)&(((type *)0)->member))
|
||||
#define container_of(entry, type, member) ((type *)((unsigned char *)(entry) - offset_of(type, member)))
|
||||
|
||||
#define list_first(head) ((head)->next)
|
||||
#define list_last(head) ((head)->prev)
|
||||
#define list_next(entry) ((entry)->next)
|
||||
|
||||
#endif
|
||||
50
fastpath/cmmctl/Makefile.cross
Normal file
50
fastpath/cmmctl/Makefile.cross
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
# cmmctl cross-compilation Makefile for arm64
|
||||
#
|
||||
# Usage:
|
||||
# mkdir -p /tmp/cmmctl-build && cd /tmp/cmmctl-build
|
||||
# make -f /build/opnsense-deps/fastpath/cmmctl/Makefile.cross clean all
|
||||
#
|
||||
# Requires OPNsense base build first:
|
||||
# sudo make -C /build/opnsense-build base DEVICE=MONO-GATEWAY SETTINGS=26.1 TOOLSDIR=/build/opnsense-build
|
||||
#
|
||||
# Copyright 2026 Mono Technologies Inc.
|
||||
# SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
SYSROOT?= /usr/obj/build/opnsense-src/arm64.aarch64/tmp
|
||||
CC?= cc
|
||||
TARGET?= --target=aarch64-unknown-freebsd14.3
|
||||
|
||||
SRCDIR?= ${MAKEFILE:H}/..
|
||||
CTL_DIR= ${SRCDIR}/cmmctl
|
||||
CMM_DIR= ${SRCDIR}/cmm
|
||||
|
||||
CTL_SRCS= cmmctl.c cmmctl_qm.c cmmctl_tunnel.c cmmctl_stat.c cmmctl_ff.c \
|
||||
cmmctl_socket.c cmmctl_bridge.c cmmctl_pktcap.c cmmctl_prf.c \
|
||||
cmmctl_natpt.c cmmctl_icc.c cmmctl_l2tp.c cmmctl_macvlan.c \
|
||||
cmmctl_tx.c
|
||||
CTL_OBJS= ${CTL_SRCS:.c=.o}
|
||||
|
||||
PROG= cmmctl
|
||||
|
||||
CFLAGS= -O2 -pipe -Wall -Wextra -Wno-unused-parameter
|
||||
CFLAGS+= --sysroot=${SYSROOT} ${TARGET}
|
||||
CFLAGS+= -I${CMM_DIR} -I${SRCDIR}/include
|
||||
CFLAGS+= -DLS1043
|
||||
|
||||
LDFLAGS= ${TARGET} --sysroot=${SYSROOT}
|
||||
LIBS= -lpcap
|
||||
|
||||
all: ${PROG}
|
||||
|
||||
.for src in ${CTL_SRCS}
|
||||
${src:.c=.o}: ${CTL_DIR}/${src}
|
||||
${CC} ${CFLAGS} -c -o ${.TARGET} ${CTL_DIR}/${src}
|
||||
.endfor
|
||||
|
||||
${PROG}: ${CTL_OBJS}
|
||||
${CC} ${LDFLAGS} -o ${.TARGET} ${CTL_OBJS} ${LIBS}
|
||||
|
||||
clean:
|
||||
rm -f ${CTL_OBJS} ${PROG}
|
||||
|
||||
.PHONY: all clean
|
||||
208
fastpath/cmmctl/cmmctl.c
Normal file
208
fastpath/cmmctl/cmmctl.c
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
/*
|
||||
* cmmctl.c — CMM control tool
|
||||
*
|
||||
* CLI tool that connects to CMM's Unix domain socket and
|
||||
* sends FPP commands for QoS, statistics, and other modules.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "cmmctl.h"
|
||||
|
||||
static ssize_t
|
||||
read_exact(int fd, void *buf, size_t len)
|
||||
{
|
||||
size_t done = 0;
|
||||
|
||||
while (done < len) {
|
||||
ssize_t n = read(fd, (char *)buf + done, len - done);
|
||||
if (n <= 0)
|
||||
return (n);
|
||||
done += n;
|
||||
}
|
||||
return ((ssize_t)done);
|
||||
}
|
||||
|
||||
static ssize_t
|
||||
write_exact(int fd, const void *buf, size_t len)
|
||||
{
|
||||
size_t done = 0;
|
||||
|
||||
while (done < len) {
|
||||
ssize_t n = write(fd, (const char *)buf + done, len - done);
|
||||
if (n < 0)
|
||||
return (-1);
|
||||
done += n;
|
||||
}
|
||||
return ((ssize_t)done);
|
||||
}
|
||||
|
||||
int
|
||||
ctrl_connect(void)
|
||||
{
|
||||
struct sockaddr_un sun;
|
||||
int fd;
|
||||
|
||||
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "cmmctl: socket: %s\n", strerror(errno));
|
||||
return (-1);
|
||||
}
|
||||
|
||||
memset(&sun, 0, sizeof(sun));
|
||||
sun.sun_family = AF_UNIX;
|
||||
strlcpy(sun.sun_path, CMM_CTRL_SOCK, sizeof(sun.sun_path));
|
||||
|
||||
if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
|
||||
fprintf(stderr, "cmmctl: connect %s: %s\n",
|
||||
CMM_CTRL_SOCK, strerror(errno));
|
||||
close(fd);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
return (fd);
|
||||
}
|
||||
|
||||
void
|
||||
ctrl_disconnect(int fd)
|
||||
{
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
int
|
||||
ctrl_command(int fd, uint16_t cmd, const void *payload,
|
||||
uint16_t payload_len, int16_t *rc, void *resp_buf,
|
||||
uint16_t *resp_len)
|
||||
{
|
||||
struct cmm_ctrl_hdr hdr;
|
||||
struct cmm_ctrl_resp resp;
|
||||
|
||||
/* Send request */
|
||||
hdr.cmd = cmd;
|
||||
hdr.len = payload_len;
|
||||
if (write_exact(fd, &hdr, sizeof(hdr)) < 0) {
|
||||
fprintf(stderr, "cmmctl: write header: %s\n",
|
||||
strerror(errno));
|
||||
return (-1);
|
||||
}
|
||||
if (payload_len > 0 && payload != NULL) {
|
||||
if (write_exact(fd, payload, payload_len) < 0) {
|
||||
fprintf(stderr, "cmmctl: write payload: %s\n",
|
||||
strerror(errno));
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Read response */
|
||||
if (read_exact(fd, &resp, sizeof(resp)) <= 0) {
|
||||
fprintf(stderr, "cmmctl: read response: %s\n",
|
||||
strerror(errno));
|
||||
return (-1);
|
||||
}
|
||||
|
||||
*rc = resp.rc;
|
||||
|
||||
if (resp.len > 0) {
|
||||
if (resp_buf != NULL && resp_len != NULL &&
|
||||
*resp_len >= resp.len) {
|
||||
if (read_exact(fd, resp_buf, resp.len) <= 0) {
|
||||
fprintf(stderr, "cmmctl: read data: %s\n",
|
||||
strerror(errno));
|
||||
return (-1);
|
||||
}
|
||||
*resp_len = resp.len;
|
||||
} else {
|
||||
/* Drain response data we don't need */
|
||||
char drain[512];
|
||||
uint16_t left = resp.len;
|
||||
while (left > 0) {
|
||||
uint16_t chunk = left > sizeof(drain) ?
|
||||
sizeof(drain) : left;
|
||||
if (read_exact(fd, drain, chunk) <= 0)
|
||||
return (-1);
|
||||
left -= chunk;
|
||||
}
|
||||
if (resp_len != NULL)
|
||||
*resp_len = 0;
|
||||
}
|
||||
} else {
|
||||
if (resp_len != NULL)
|
||||
*resp_len = 0;
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
struct cmmctl_cmd {
|
||||
const char *name;
|
||||
int (*handler)(int argc, char **argv, int fd);
|
||||
const char *desc;
|
||||
};
|
||||
|
||||
static struct cmmctl_cmd commands[] = {
|
||||
{ "qm", cmmctl_qm_main, "QoS / Queue Manager" },
|
||||
{ "tunnel", cmmctl_tunnel_main, "Tunnel offload" },
|
||||
{ "stat", cmmctl_stat_main, "Statistics" },
|
||||
{ "ff", cmmctl_ff_main, "Fast-forward control" },
|
||||
{ "socket", cmmctl_socket_main, "Socket acceleration" },
|
||||
{ "bridge", cmmctl_bridge_main, "L2 bridge offload" },
|
||||
{ "pktcap", cmmctl_pktcap_main, "Packet capture" },
|
||||
{ "prf", cmmctl_prf_main, "FPP trace / profiling" },
|
||||
{ "natpt", cmmctl_natpt_main, "NAT-PT translation" },
|
||||
{ "icc", cmmctl_icc_main, "Ingress congestion control" },
|
||||
{ "l2tp", cmmctl_l2tp_main, "L2TP tunnel interfaces" },
|
||||
{ "macvlan", cmmctl_macvlan_main, "Virtual MAC interfaces" },
|
||||
{ "tx", cmmctl_tx_main, "TX DSCP to VLAN PCP mapping" },
|
||||
{ NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
struct cmmctl_cmd *c;
|
||||
|
||||
fprintf(stderr, "usage: cmmctl <command> [args...]\n\n");
|
||||
fprintf(stderr, "Commands:\n");
|
||||
for (c = commands; c->name != NULL; c++)
|
||||
fprintf(stderr, " %-10s %s\n", c->name, c->desc);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
struct cmmctl_cmd *c;
|
||||
int fd, rc;
|
||||
|
||||
if (argc < 2)
|
||||
usage();
|
||||
|
||||
for (c = commands; c->name != NULL; c++) {
|
||||
if (strcmp(argv[1], c->name) == 0)
|
||||
break;
|
||||
}
|
||||
if (c->name == NULL) {
|
||||
fprintf(stderr, "cmmctl: unknown command '%s'\n", argv[1]);
|
||||
usage();
|
||||
}
|
||||
|
||||
fd = ctrl_connect();
|
||||
if (fd < 0)
|
||||
return (1);
|
||||
|
||||
rc = c->handler(argc - 2, argv + 2, fd);
|
||||
|
||||
ctrl_disconnect(fd);
|
||||
return (rc);
|
||||
}
|
||||
43
fastpath/cmmctl/cmmctl.h
Normal file
43
fastpath/cmmctl/cmmctl.h
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* cmmctl.h — CMM control tool shared definitions
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#ifndef CMMCTL_H
|
||||
#define CMMCTL_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "cmm_ctrl.h"
|
||||
|
||||
/* Connect to CMM control socket. Returns fd or -1 on error. */
|
||||
int ctrl_connect(void);
|
||||
void ctrl_disconnect(int fd);
|
||||
|
||||
/*
|
||||
* Send an FPP command through the CMM control socket.
|
||||
* Returns 0 on success, fills *rc with FPP/CMM result code.
|
||||
* On success with response data: resp_buf/resp_len filled.
|
||||
* resp_buf may be NULL if no response data is expected.
|
||||
*/
|
||||
int ctrl_command(int fd, uint16_t cmd, const void *payload,
|
||||
uint16_t payload_len, int16_t *rc, void *resp_buf,
|
||||
uint16_t *resp_len);
|
||||
|
||||
/* Sub-command handlers */
|
||||
int cmmctl_qm_main(int argc, char **argv, int fd);
|
||||
int cmmctl_tunnel_main(int argc, char **argv, int fd);
|
||||
int cmmctl_stat_main(int argc, char **argv, int fd);
|
||||
int cmmctl_ff_main(int argc, char **argv, int fd);
|
||||
int cmmctl_socket_main(int argc, char **argv, int fd);
|
||||
int cmmctl_bridge_main(int argc, char **argv, int fd);
|
||||
int cmmctl_pktcap_main(int argc, char **argv, int fd);
|
||||
int cmmctl_prf_main(int argc, char **argv, int fd);
|
||||
int cmmctl_natpt_main(int argc, char **argv, int fd);
|
||||
int cmmctl_icc_main(int argc, char **argv, int fd);
|
||||
int cmmctl_l2tp_main(int argc, char **argv, int fd);
|
||||
int cmmctl_macvlan_main(int argc, char **argv, int fd);
|
||||
int cmmctl_tx_main(int argc, char **argv, int fd);
|
||||
|
||||
#endif /* CMMCTL_H */
|
||||
498
fastpath/cmmctl/cmmctl_bridge.c
Normal file
498
fastpath/cmmctl/cmmctl_bridge.c
Normal file
|
|
@ -0,0 +1,498 @@
|
|||
/*
|
||||
* cmmctl_bridge.c — L2 bridge commands for cmmctl
|
||||
*
|
||||
* FCI passthrough to CDX's L2 bridge module.
|
||||
*
|
||||
* Syntax:
|
||||
* cmmctl bridge enable <interface>
|
||||
* cmmctl bridge disable <interface>
|
||||
* cmmctl bridge mode auto|manual
|
||||
* cmmctl bridge timeout <seconds>
|
||||
* cmmctl bridge add srcmac=<MAC> dstmac=<MAC> input=<if> output=<if>
|
||||
* [ethertype=<N>] [prio=<N>]
|
||||
* cmmctl bridge remove srcmac=<MAC> dstmac=<MAC> input=<if>
|
||||
* cmmctl bridge show flows|status
|
||||
* cmmctl bridge reset
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <net/if.h>
|
||||
#include <net/ethernet.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cmmctl.h"
|
||||
#include "fpp.h"
|
||||
|
||||
static int
|
||||
parse_macaddr(const char *str, uint8_t *mac)
|
||||
{
|
||||
unsigned int m[6];
|
||||
int n;
|
||||
|
||||
n = sscanf(str, "%x:%x:%x:%x:%x:%x",
|
||||
&m[0], &m[1], &m[2], &m[3], &m[4], &m[5]);
|
||||
if (n != 6)
|
||||
return (-1);
|
||||
for (n = 0; n < 6; n++)
|
||||
mac[n] = (uint8_t)m[n];
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* enable / disable */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int
|
||||
cmd_enable(int argc, char **argv, int fd, int enable)
|
||||
{
|
||||
fpp_l2_bridge_enable_cmd_t cmd;
|
||||
int16_t rc;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: cmmctl bridge %s <interface>\n",
|
||||
enable ? "enable" : "disable");
|
||||
return (1);
|
||||
}
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.enable_flag = enable ? 1 : 0;
|
||||
strlcpy(cmd.input_name, argv[0], sizeof(cmd.input_name));
|
||||
|
||||
if (ctrl_command(fd, FPP_CMD_RX_L2BRIDGE_ENABLE,
|
||||
&cmd, sizeof(cmd), &rc, NULL, NULL) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0) {
|
||||
fprintf(stderr, "bridge %s %s: error %d\n",
|
||||
enable ? "enable" : "disable", argv[0], rc);
|
||||
return (1);
|
||||
}
|
||||
|
||||
printf("bridge %s on %s\n",
|
||||
enable ? "enabled" : "disabled", argv[0]);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* mode */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int
|
||||
cmd_mode(int argc, char **argv, int fd)
|
||||
{
|
||||
fpp_l2_bridge_control_cmd_t cmd;
|
||||
int16_t rc;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: cmmctl bridge mode auto|manual\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
if (strcmp(argv[0], "auto") == 0)
|
||||
cmd.mode_timeout = FPP_L2_BRIDGE_MODE_AUTO;
|
||||
else if (strcmp(argv[0], "manual") == 0)
|
||||
cmd.mode_timeout = FPP_L2_BRIDGE_MODE_MANUAL;
|
||||
else {
|
||||
fprintf(stderr, "bridge mode: expected 'auto' or 'manual'\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (ctrl_command(fd, FPP_CMD_RX_L2BRIDGE_MODE,
|
||||
&cmd, sizeof(cmd), &rc, NULL, NULL) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0) {
|
||||
fprintf(stderr, "bridge mode: error %d\n", rc);
|
||||
return (1);
|
||||
}
|
||||
|
||||
printf("bridge mode set to %s\n", argv[0]);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* timeout */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int
|
||||
cmd_timeout(int argc, char **argv, int fd)
|
||||
{
|
||||
fpp_l2_bridge_control_cmd_t cmd;
|
||||
char *endp;
|
||||
unsigned long val;
|
||||
int16_t rc;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl bridge timeout <seconds>\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
val = strtoul(argv[0], &endp, 10);
|
||||
if (argv[0] == endp || *endp != '\0' || val == 0 || val > 65535) {
|
||||
fprintf(stderr, "bridge timeout: invalid value '%s' "
|
||||
"(1-65535)\n", argv[0]);
|
||||
return (1);
|
||||
}
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.mode_timeout = (uint16_t)val;
|
||||
|
||||
if (ctrl_command(fd, FPP_CMD_RX_L2BRIDGE_FLOW_TIMEOUT,
|
||||
&cmd, sizeof(cmd), &rc, NULL, NULL) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0) {
|
||||
fprintf(stderr, "bridge timeout: error %d\n", rc);
|
||||
return (1);
|
||||
}
|
||||
|
||||
printf("bridge flow timeout set to %lus\n", val);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* add / remove static entries */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/*
|
||||
* Parse key=value pairs from argv.
|
||||
* Returns 0 on success, -1 on bad syntax.
|
||||
*/
|
||||
static int
|
||||
cmd_add(int argc, char **argv, int fd)
|
||||
{
|
||||
fpp_l2_bridge_add_entry_cmd_t cmd;
|
||||
int16_t rc;
|
||||
int i;
|
||||
int have_src = 0, have_dst = 0, have_in = 0, have_out = 0;
|
||||
|
||||
if (argc < 4) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl bridge add srcmac=<MAC> dstmac=<MAC> "
|
||||
"input=<if> output=<if> [ethertype=<N>] [prio=<N>]\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
char *eq = strchr(argv[i], '=');
|
||||
if (eq == NULL) {
|
||||
fprintf(stderr, "bridge add: expected key=value, "
|
||||
"got '%s'\n", argv[i]);
|
||||
return (1);
|
||||
}
|
||||
*eq = '\0';
|
||||
char *key = argv[i];
|
||||
char *val = eq + 1;
|
||||
|
||||
if (strcmp(key, "srcmac") == 0) {
|
||||
if (parse_macaddr(val, cmd.srcaddr) < 0) {
|
||||
fprintf(stderr, "bridge add: bad srcmac '%s'\n",
|
||||
val);
|
||||
return (1);
|
||||
}
|
||||
have_src = 1;
|
||||
} else if (strcmp(key, "dstmac") == 0) {
|
||||
if (parse_macaddr(val, cmd.destaddr) < 0) {
|
||||
fprintf(stderr, "bridge add: bad dstmac '%s'\n",
|
||||
val);
|
||||
return (1);
|
||||
}
|
||||
have_dst = 1;
|
||||
} else if (strcmp(key, "input") == 0) {
|
||||
strlcpy(cmd.input_name, val, sizeof(cmd.input_name));
|
||||
have_in = 1;
|
||||
} else if (strcmp(key, "output") == 0) {
|
||||
strlcpy(cmd.output_name, val, sizeof(cmd.output_name));
|
||||
have_out = 1;
|
||||
} else if (strcmp(key, "ethertype") == 0) {
|
||||
cmd.ethertype = htons((uint16_t)strtoul(val, NULL, 0));
|
||||
} else if (strcmp(key, "prio") == 0) {
|
||||
cmd.pkt_priority = (uint16_t)strtoul(val, NULL, 0);
|
||||
} else {
|
||||
fprintf(stderr, "bridge add: unknown key '%s'\n", key);
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!have_src || !have_dst || !have_in || !have_out) {
|
||||
fprintf(stderr, "bridge add: srcmac, dstmac, input, and "
|
||||
"output are required\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (ctrl_command(fd, FPP_CMD_RX_L2BRIDGE_ADD,
|
||||
&cmd, sizeof(cmd), &rc, NULL, NULL) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0) {
|
||||
fprintf(stderr, "bridge add: error %d\n", rc);
|
||||
return (1);
|
||||
}
|
||||
|
||||
printf("bridge entry added\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
cmd_remove(int argc, char **argv, int fd)
|
||||
{
|
||||
fpp_l2_bridge_remove_entry_cmd_t cmd;
|
||||
int16_t rc;
|
||||
int i;
|
||||
int have_src = 0, have_dst = 0, have_in = 0;
|
||||
|
||||
if (argc < 3) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl bridge remove srcmac=<MAC> dstmac=<MAC> "
|
||||
"input=<if>\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
char *eq = strchr(argv[i], '=');
|
||||
if (eq == NULL) {
|
||||
fprintf(stderr, "bridge remove: expected key=value, "
|
||||
"got '%s'\n", argv[i]);
|
||||
return (1);
|
||||
}
|
||||
*eq = '\0';
|
||||
char *key = argv[i];
|
||||
char *val = eq + 1;
|
||||
|
||||
if (strcmp(key, "srcmac") == 0) {
|
||||
if (parse_macaddr(val, cmd.srcaddr) < 0) {
|
||||
fprintf(stderr,
|
||||
"bridge remove: bad srcmac '%s'\n", val);
|
||||
return (1);
|
||||
}
|
||||
have_src = 1;
|
||||
} else if (strcmp(key, "dstmac") == 0) {
|
||||
if (parse_macaddr(val, cmd.destaddr) < 0) {
|
||||
fprintf(stderr,
|
||||
"bridge remove: bad dstmac '%s'\n", val);
|
||||
return (1);
|
||||
}
|
||||
have_dst = 1;
|
||||
} else if (strcmp(key, "input") == 0) {
|
||||
strlcpy(cmd.input_name, val, sizeof(cmd.input_name));
|
||||
have_in = 1;
|
||||
} else {
|
||||
fprintf(stderr, "bridge remove: unknown key '%s'\n",
|
||||
key);
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!have_src || !have_dst || !have_in) {
|
||||
fprintf(stderr, "bridge remove: srcmac, dstmac, and input "
|
||||
"are required\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (ctrl_command(fd, FPP_CMD_RX_L2BRIDGE_REMOVE,
|
||||
&cmd, sizeof(cmd), &rc, NULL, NULL) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0) {
|
||||
fprintf(stderr, "bridge remove: error %d\n", rc);
|
||||
return (1);
|
||||
}
|
||||
|
||||
printf("bridge entry removed\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* show status / show flows */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int
|
||||
cmd_show_status(int fd)
|
||||
{
|
||||
fpp_l2_bridge_query_status_response_t resp;
|
||||
uint16_t rlen;
|
||||
int16_t rc;
|
||||
int first = 1;
|
||||
|
||||
while (1) {
|
||||
rlen = sizeof(resp);
|
||||
if (ctrl_command(fd, FPP_CMD_RX_L2BRIDGE_QUERY_STATUS,
|
||||
NULL, 0, &rc, &resp, &rlen) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0 || rlen < sizeof(resp))
|
||||
break;
|
||||
|
||||
if (resp.eof)
|
||||
break;
|
||||
|
||||
if (first) {
|
||||
printf("%-16s %s\n", "Interface", "Status");
|
||||
printf("%-16s %s\n", "---------", "------");
|
||||
first = 0;
|
||||
}
|
||||
|
||||
printf("%-16s %s\n",
|
||||
resp.ifname,
|
||||
resp.status ? "ON" : "OFF");
|
||||
}
|
||||
|
||||
if (first)
|
||||
printf("No bridge interfaces configured\n");
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
cmd_show_flows(int fd)
|
||||
{
|
||||
fpp_l2_bridge_query_entry_response_t resp;
|
||||
uint16_t rlen;
|
||||
int16_t rc;
|
||||
int count = 0;
|
||||
|
||||
printf("%-17s %-17s %-6s %-16s %-16s %s\n",
|
||||
"Src MAC", "Dst MAC", "Etype", "Input", "Output", "Prio");
|
||||
printf("%-17s %-17s %-6s %-16s %-16s %s\n",
|
||||
"---------", "---------", "-----", "-----", "------", "----");
|
||||
|
||||
while (1) {
|
||||
rlen = sizeof(resp);
|
||||
if (ctrl_command(fd, FPP_CMD_RX_L2BRIDGE_QUERY_ENTRY,
|
||||
NULL, 0, &rc, &resp, &rlen) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0 || rlen < sizeof(resp))
|
||||
break;
|
||||
|
||||
if (resp.eof)
|
||||
break;
|
||||
|
||||
printf("%02x:%02x:%02x:%02x:%02x:%02x "
|
||||
"%02x:%02x:%02x:%02x:%02x:%02x "
|
||||
"0x%04x %-16s %-16s %u\n",
|
||||
resp.srcaddr[0], resp.srcaddr[1],
|
||||
resp.srcaddr[2], resp.srcaddr[3],
|
||||
resp.srcaddr[4], resp.srcaddr[5],
|
||||
resp.destaddr[0], resp.destaddr[1],
|
||||
resp.destaddr[2], resp.destaddr[3],
|
||||
resp.destaddr[4], resp.destaddr[5],
|
||||
ntohs(resp.ethertype),
|
||||
resp.input_name[0] ? resp.input_name : "-",
|
||||
resp.output_name[0] ? resp.output_name : "-",
|
||||
resp.pkt_priority);
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
printf("\n%d flow(s)\n", count);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
cmd_show(int argc, char **argv, int fd)
|
||||
{
|
||||
if (argc < 1) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl bridge show flows|status\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (strcmp(argv[0], "status") == 0)
|
||||
return (cmd_show_status(fd));
|
||||
else if (strcmp(argv[0], "flows") == 0)
|
||||
return (cmd_show_flows(fd));
|
||||
else {
|
||||
fprintf(stderr,
|
||||
"bridge show: expected 'flows' or 'status'\n");
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* reset */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int
|
||||
cmd_reset(int fd)
|
||||
{
|
||||
int16_t rc;
|
||||
|
||||
if (ctrl_command(fd, FPP_CMD_RX_L2BRIDGE_FLOW_RESET,
|
||||
NULL, 0, &rc, NULL, NULL) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0) {
|
||||
fprintf(stderr, "bridge reset: error %d\n", rc);
|
||||
return (1);
|
||||
}
|
||||
|
||||
printf("bridge flows reset\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Main dispatch */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void
|
||||
bridge_usage(void)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl bridge <subcommand> [args...]\n\n"
|
||||
"Subcommands:\n"
|
||||
" enable <interface> Enable L2 bridging on interface\n"
|
||||
" disable <interface> Disable L2 bridging on interface\n"
|
||||
" mode auto|manual Set bridge mode\n"
|
||||
" timeout <seconds> Set flow timeout (1-65535)\n"
|
||||
" add srcmac=... dstmac=... input=... output=...\n"
|
||||
" Add static bridge entry\n"
|
||||
" remove srcmac=... dstmac=... input=...\n"
|
||||
" Remove bridge entry\n"
|
||||
" show flows|status Show bridge flows or status\n"
|
||||
" reset Reset all bridge flows\n");
|
||||
}
|
||||
|
||||
int
|
||||
cmmctl_bridge_main(int argc, char **argv, int fd)
|
||||
{
|
||||
if (argc < 1) {
|
||||
bridge_usage();
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (strcmp(argv[0], "enable") == 0)
|
||||
return (cmd_enable(argc - 1, argv + 1, fd, 1));
|
||||
else if (strcmp(argv[0], "disable") == 0)
|
||||
return (cmd_enable(argc - 1, argv + 1, fd, 0));
|
||||
else if (strcmp(argv[0], "mode") == 0)
|
||||
return (cmd_mode(argc - 1, argv + 1, fd));
|
||||
else if (strcmp(argv[0], "timeout") == 0)
|
||||
return (cmd_timeout(argc - 1, argv + 1, fd));
|
||||
else if (strcmp(argv[0], "add") == 0)
|
||||
return (cmd_add(argc - 1, argv + 1, fd));
|
||||
else if (strcmp(argv[0], "remove") == 0)
|
||||
return (cmd_remove(argc - 1, argv + 1, fd));
|
||||
else if (strcmp(argv[0], "show") == 0)
|
||||
return (cmd_show(argc - 1, argv + 1, fd));
|
||||
else if (strcmp(argv[0], "reset") == 0)
|
||||
return (cmd_reset(fd));
|
||||
else {
|
||||
fprintf(stderr, "bridge: unknown subcommand '%s'\n",
|
||||
argv[0]);
|
||||
bridge_usage();
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
110
fastpath/cmmctl/cmmctl_ff.c
Normal file
110
fastpath/cmmctl/cmmctl_ff.c
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* cmmctl_ff.c — Fast-forward control CLI sub-commands
|
||||
*
|
||||
* cmmctl ff enable Enable fast-forward offload
|
||||
* cmmctl ff disable Disable fast-forward offload
|
||||
* cmmctl ff ipsec-frag enable Enable IPsec pre-fragmentation
|
||||
* cmmctl ff ipsec-frag disable Disable IPsec pre-fragmentation
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cmmctl.h"
|
||||
#include "fpp.h"
|
||||
|
||||
static void
|
||||
ff_usage(void)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl ff <command>\n\n"
|
||||
"Commands:\n"
|
||||
" enable Enable fast-forward offload\n"
|
||||
" disable Disable fast-forward offload\n"
|
||||
" ipsec-frag enable Enable IPsec pre-fragmentation\n"
|
||||
" ipsec-frag disable Disable IPsec pre-fragmentation\n");
|
||||
}
|
||||
|
||||
static int
|
||||
ff_control(int fd, int enable)
|
||||
{
|
||||
fpp_ff_ctrl_cmd_t cmd;
|
||||
int16_t rc;
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.enable = enable ? 1 : 0;
|
||||
|
||||
if (ctrl_command(fd, FPP_CMD_IPV4_FF_CONTROL, &cmd, sizeof(cmd),
|
||||
&rc, NULL, NULL) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0) {
|
||||
fprintf(stderr, "ff %s: error %d\n",
|
||||
enable ? "enable" : "disable", rc);
|
||||
return (1);
|
||||
}
|
||||
|
||||
printf("fast-forward %s\n", enable ? "enabled" : "disabled");
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
ff_ipsec_frag(int fd, int enable)
|
||||
{
|
||||
fpp_ipsec_cmd_t cmd;
|
||||
int16_t rc;
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.pre_frag_en = enable ? 1 : 0;
|
||||
|
||||
if (ctrl_command(fd, FPP_CMD_IPSEC_FRAG_CFG, &cmd, sizeof(cmd),
|
||||
&rc, NULL, NULL) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0) {
|
||||
fprintf(stderr, "ipsec-frag %s: error %d\n",
|
||||
enable ? "enable" : "disable", rc);
|
||||
return (1);
|
||||
}
|
||||
|
||||
printf("IPsec pre-fragmentation %s\n",
|
||||
enable ? "enabled" : "disabled");
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cmmctl_ff_main(int argc, char **argv, int fd)
|
||||
{
|
||||
|
||||
if (argc < 1) {
|
||||
ff_usage();
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (strcmp(argv[0], "enable") == 0)
|
||||
return (ff_control(fd, 1));
|
||||
|
||||
if (strcmp(argv[0], "disable") == 0)
|
||||
return (ff_control(fd, 0));
|
||||
|
||||
if (strcmp(argv[0], "ipsec-frag") == 0) {
|
||||
if (argc < 2) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl ff ipsec-frag enable|disable\n");
|
||||
return (1);
|
||||
}
|
||||
if (strcmp(argv[1], "enable") == 0)
|
||||
return (ff_ipsec_frag(fd, 1));
|
||||
if (strcmp(argv[1], "disable") == 0)
|
||||
return (ff_ipsec_frag(fd, 0));
|
||||
fprintf(stderr, "unknown ipsec-frag action: %s\n", argv[1]);
|
||||
return (1);
|
||||
}
|
||||
|
||||
fprintf(stderr, "unknown ff command: %s\n", argv[0]);
|
||||
ff_usage();
|
||||
return (1);
|
||||
}
|
||||
713
fastpath/cmmctl/cmmctl_icc.c
Normal file
713
fastpath/cmmctl/cmmctl_icc.c
Normal file
|
|
@ -0,0 +1,713 @@
|
|||
/*
|
||||
* cmmctl_icc.c — Ingress Congestion Control CLI sub-commands
|
||||
*
|
||||
* cmmctl icc reset
|
||||
* cmmctl icc threshold <bmu1> <bmu2>
|
||||
* cmmctl icc add <iface> <type> [args...]
|
||||
* cmmctl icc delete <iface> <type> [args...]
|
||||
* cmmctl icc query [<iface>]
|
||||
*
|
||||
* Interface numbers are 0-2 (matching CDX ICC interface IDs).
|
||||
*
|
||||
* CDX status: No control_icc.c handler exists in CDX — all commands
|
||||
* currently return ERR_UNKNOWN_COMMAND. The CLI is ready for when
|
||||
* a CDX ICC handler is implemented.
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
#include "cmmctl.h"
|
||||
#include "fpp.h"
|
||||
|
||||
#define ICC_NUM_INTERFACES 3
|
||||
|
||||
#define ICC_ACTION_ADD 0
|
||||
#define ICC_ACTION_DELETE 1
|
||||
|
||||
#define ICC_ACTION_QUERY 0
|
||||
#define ICC_ACTION_QUERY_CONT 1
|
||||
|
||||
#define ICC_TABLETYPE_ETHERTYPE 0
|
||||
#define ICC_TABLETYPE_PROTOCOL 1
|
||||
#define ICC_TABLETYPE_DSCP 2
|
||||
#define ICC_TABLETYPE_SADDR 3
|
||||
#define ICC_TABLETYPE_DADDR 4
|
||||
#define ICC_TABLETYPE_SADDR6 5
|
||||
#define ICC_TABLETYPE_DADDR6 6
|
||||
#define ICC_TABLETYPE_PORT 7
|
||||
#define ICC_TABLETYPE_VLAN 8
|
||||
|
||||
/* ---- Helpers ---- */
|
||||
|
||||
static int
|
||||
parse_uint(const char *s, uint32_t maxval, uint32_t *out)
|
||||
{
|
||||
char *endp;
|
||||
unsigned long val;
|
||||
|
||||
val = strtoul(s, &endp, 0);
|
||||
if (s == endp || *endp != '\0' || val > maxval)
|
||||
return (-1);
|
||||
*out = (uint32_t)val;
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse "N" or "N-M" range. Both values must be <= maxval
|
||||
* and from <= to.
|
||||
*/
|
||||
static int
|
||||
parse_range(const char *s, uint32_t maxval, uint32_t *from, uint32_t *to)
|
||||
{
|
||||
char *endp;
|
||||
unsigned long fromval, toval;
|
||||
|
||||
fromval = strtoul(s, &endp, 0);
|
||||
if (s == endp)
|
||||
return (-1);
|
||||
if (*endp == '-') {
|
||||
const char *p = endp + 1;
|
||||
toval = strtoul(p, &endp, 0);
|
||||
if (p == endp || *endp != '\0')
|
||||
return (-1);
|
||||
} else if (*endp == '\0') {
|
||||
toval = fromval;
|
||||
} else {
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if (toval < fromval || toval > maxval)
|
||||
return (-1);
|
||||
*from = (uint32_t)fromval;
|
||||
*to = (uint32_t)toval;
|
||||
return (0);
|
||||
}
|
||||
|
||||
static inline void
|
||||
setbit(uint8_t *bits, unsigned int idx)
|
||||
{
|
||||
|
||||
bits[idx >> 3] |= 1 << (idx & 7);
|
||||
}
|
||||
|
||||
static inline int
|
||||
testbit(const uint8_t *bits, unsigned int idx)
|
||||
{
|
||||
|
||||
return ((bits[idx >> 3] >> (idx & 7)) & 1);
|
||||
}
|
||||
|
||||
static const char *
|
||||
icc_strerror(int16_t rc)
|
||||
{
|
||||
|
||||
switch ((uint16_t)rc) {
|
||||
case FPP_ERR_UNKNOWN_COMMAND:
|
||||
return ("command not supported by CDX (no ICC handler)");
|
||||
case FPP_ERR_ICC_TOO_MANY_ENTRIES:
|
||||
return ("too many entries");
|
||||
case FPP_ERR_ICC_ENTRY_ALREADY_EXISTS:
|
||||
return ("entry already exists");
|
||||
case FPP_ERR_ICC_ENTRY_NOT_FOUND:
|
||||
return ("entry not found");
|
||||
case FPP_ERR_ICC_THRESHOLD_OUT_OF_RANGE:
|
||||
return ("threshold value out of range");
|
||||
case FPP_ERR_ICC_INVALID_MASKLEN:
|
||||
return ("invalid mask length");
|
||||
default:
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
icc_errmsg(const char *cmd, int16_t rc)
|
||||
{
|
||||
const char *msg;
|
||||
|
||||
msg = icc_strerror(rc);
|
||||
if (msg != NULL)
|
||||
fprintf(stderr, "icc %s: %s (0x%04x)\n", cmd, msg,
|
||||
(unsigned)(uint16_t)rc);
|
||||
else
|
||||
fprintf(stderr, "icc %s: error 0x%04x\n", cmd,
|
||||
(unsigned)(uint16_t)rc);
|
||||
}
|
||||
|
||||
/* ---- Sub-command handlers ---- */
|
||||
|
||||
static int
|
||||
cmd_reset(int fd)
|
||||
{
|
||||
fpp_icc_reset_cmd_t cmd;
|
||||
uint8_t raw[2];
|
||||
uint16_t resp_len, fpp_rc;
|
||||
int16_t rc;
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
resp_len = sizeof(raw);
|
||||
if (ctrl_command(fd, FPP_CMD_ICC_RESET, &cmd, sizeof(cmd),
|
||||
&rc, raw, &resp_len) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0) {
|
||||
icc_errmsg("reset", rc);
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (resp_len >= 2) {
|
||||
memcpy(&fpp_rc, raw, sizeof(fpp_rc));
|
||||
if (fpp_rc != 0) {
|
||||
icc_errmsg("reset", (int16_t)fpp_rc);
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
|
||||
printf("icc reset\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
cmd_threshold(int argc, char **argv, int fd)
|
||||
{
|
||||
fpp_icc_threshold_cmd_t cmd;
|
||||
uint8_t raw[2];
|
||||
uint16_t resp_len, fpp_rc;
|
||||
uint32_t bmu1, bmu2;
|
||||
int16_t rc;
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl icc threshold <bmu1> <bmu2>\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (parse_uint(argv[0], 1024, &bmu1) < 0) {
|
||||
fprintf(stderr,
|
||||
"icc threshold: invalid bmu1 value '%s'\n", argv[0]);
|
||||
return (1);
|
||||
}
|
||||
if (parse_uint(argv[1], 1024, &bmu2) < 0) {
|
||||
fprintf(stderr,
|
||||
"icc threshold: invalid bmu2 value '%s'\n", argv[1]);
|
||||
return (1);
|
||||
}
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.bmu1_threshold = (uint16_t)bmu1;
|
||||
cmd.bmu2_threshold = (uint16_t)bmu2;
|
||||
|
||||
resp_len = sizeof(raw);
|
||||
if (ctrl_command(fd, FPP_CMD_ICC_THRESHOLD, &cmd, sizeof(cmd),
|
||||
&rc, raw, &resp_len) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0) {
|
||||
icc_errmsg("threshold", rc);
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (resp_len >= 2) {
|
||||
memcpy(&fpp_rc, raw, sizeof(fpp_rc));
|
||||
if (fpp_rc != 0) {
|
||||
icc_errmsg("threshold", (int16_t)fpp_rc);
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
|
||||
printf("icc threshold: bmu1=%u bmu2=%u\n", bmu1, bmu2);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
cmd_add_delete(int argc, char **argv, int fd, uint16_t action)
|
||||
{
|
||||
fpp_icc_add_delete_cmd_t cmd;
|
||||
uint8_t raw[2];
|
||||
uint16_t resp_len, fpp_rc;
|
||||
uint32_t iface, val1, val2;
|
||||
int16_t rc;
|
||||
const char *actstr = (action == ICC_ACTION_ADD) ? "add" : "delete";
|
||||
int arg;
|
||||
|
||||
if (argc < 3) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl icc %s <iface> <type> [args...]\n"
|
||||
"\n"
|
||||
"Types:\n"
|
||||
" ethertype <value>\n"
|
||||
" protocol <proto> [<proto>...]\n"
|
||||
" dscp <value> [<value>...]\n"
|
||||
" saddr <ip> [<masklen>]\n"
|
||||
" daddr <ip> [<masklen>]\n"
|
||||
" saddr6 <ip6> [<prefixlen>]\n"
|
||||
" daddr6 <ip6> [<prefixlen>]\n"
|
||||
" port <sport-range> <dport-range>\n"
|
||||
" sport <port-range>\n"
|
||||
" dport <port-range>\n"
|
||||
" vlan <id-range> [<prio-range>]\n",
|
||||
actstr);
|
||||
return (1);
|
||||
}
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.action = action;
|
||||
|
||||
if (parse_uint(argv[0], ICC_NUM_INTERFACES - 1, &iface) < 0) {
|
||||
fprintf(stderr,
|
||||
"icc %s: invalid interface '%s' (0-%d)\n",
|
||||
actstr, argv[0], ICC_NUM_INTERFACES - 1);
|
||||
return (1);
|
||||
}
|
||||
cmd.interface = (uint8_t)iface;
|
||||
|
||||
if (strcasecmp(argv[1], "ethertype") == 0) {
|
||||
if (argc != 3) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl icc %s <iface>"
|
||||
" ethertype <value>\n", actstr);
|
||||
return (1);
|
||||
}
|
||||
cmd.table_type = ICC_TABLETYPE_ETHERTYPE;
|
||||
if (parse_uint(argv[2], 0xffff, &val1) < 0) {
|
||||
fprintf(stderr,
|
||||
"icc %s: invalid ethertype '%s'\n",
|
||||
actstr, argv[2]);
|
||||
return (1);
|
||||
}
|
||||
cmd.ethertype.type = (uint16_t)val1;
|
||||
|
||||
} else if (strcasecmp(argv[1], "protocol") == 0) {
|
||||
cmd.table_type = ICC_TABLETYPE_PROTOCOL;
|
||||
for (arg = 2; arg < argc; arg++) {
|
||||
if (parse_range(argv[arg], 255, &val1, &val2) < 0) {
|
||||
fprintf(stderr,
|
||||
"icc %s: invalid protocol '%s'\n",
|
||||
actstr, argv[arg]);
|
||||
return (1);
|
||||
}
|
||||
for (; val1 <= val2; val1++)
|
||||
setbit(cmd.protocol.ipproto, val1);
|
||||
}
|
||||
|
||||
} else if (strcasecmp(argv[1], "dscp") == 0) {
|
||||
cmd.table_type = ICC_TABLETYPE_DSCP;
|
||||
for (arg = 2; arg < argc; arg++) {
|
||||
if (parse_range(argv[arg], 63, &val1, &val2) < 0) {
|
||||
fprintf(stderr,
|
||||
"icc %s: invalid dscp value '%s'\n",
|
||||
actstr, argv[arg]);
|
||||
return (1);
|
||||
}
|
||||
for (; val1 <= val2; val1++)
|
||||
setbit(cmd.dscp.dscp_value, val1);
|
||||
}
|
||||
|
||||
} else if (strcasecmp(argv[1], "saddr") == 0 ||
|
||||
strcasecmp(argv[1], "daddr") == 0) {
|
||||
if (argc < 3 || argc > 4) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl icc %s <iface>"
|
||||
" %s <ip> [<masklen>]\n", actstr, argv[1]);
|
||||
return (1);
|
||||
}
|
||||
cmd.table_type = (strcasecmp(argv[1], "saddr") == 0) ?
|
||||
ICC_TABLETYPE_SADDR : ICC_TABLETYPE_DADDR;
|
||||
{
|
||||
uint32_t v4tmp;
|
||||
if (inet_pton(AF_INET, argv[2], &v4tmp) != 1) {
|
||||
fprintf(stderr,
|
||||
"icc %s: invalid IPv4 address '%s'\n",
|
||||
actstr, argv[2]);
|
||||
return (1);
|
||||
}
|
||||
cmd.ipaddr.v4_addr = v4tmp;
|
||||
}
|
||||
if (argc == 4) {
|
||||
if (parse_uint(argv[3], 32, &val1) < 0 ||
|
||||
val1 == 0) {
|
||||
fprintf(stderr,
|
||||
"icc %s: invalid mask length '%s'\n",
|
||||
actstr, argv[3]);
|
||||
return (1);
|
||||
}
|
||||
cmd.ipaddr.v4_masklen = (uint8_t)val1;
|
||||
} else {
|
||||
cmd.ipaddr.v4_masklen = 32;
|
||||
}
|
||||
|
||||
} else if (strcasecmp(argv[1], "saddr6") == 0 ||
|
||||
strcasecmp(argv[1], "daddr6") == 0) {
|
||||
if (argc < 3 || argc > 4) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl icc %s <iface>"
|
||||
" %s <ip6> [<prefixlen>]\n", actstr, argv[1]);
|
||||
return (1);
|
||||
}
|
||||
cmd.table_type = (strcasecmp(argv[1], "saddr6") == 0) ?
|
||||
ICC_TABLETYPE_SADDR6 : ICC_TABLETYPE_DADDR6;
|
||||
{
|
||||
uint32_t v6tmp[4];
|
||||
if (inet_pton(AF_INET6, argv[2], v6tmp) != 1) {
|
||||
fprintf(stderr,
|
||||
"icc %s: invalid IPv6 address '%s'\n",
|
||||
actstr, argv[2]);
|
||||
return (1);
|
||||
}
|
||||
memcpy(cmd.ipv6addr.v6_addr, v6tmp, 16);
|
||||
}
|
||||
if (argc == 4) {
|
||||
if (parse_uint(argv[3], 128, &val1) < 0 ||
|
||||
val1 == 0) {
|
||||
fprintf(stderr,
|
||||
"icc %s: invalid prefix length '%s'\n",
|
||||
actstr, argv[3]);
|
||||
return (1);
|
||||
}
|
||||
cmd.ipv6addr.v6_masklen = (uint8_t)val1;
|
||||
} else {
|
||||
cmd.ipv6addr.v6_masklen = 128;
|
||||
}
|
||||
|
||||
} else if (strcasecmp(argv[1], "port") == 0) {
|
||||
if (argc != 4) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl icc %s <iface>"
|
||||
" port <sport-range> <dport-range>\n", actstr);
|
||||
return (1);
|
||||
}
|
||||
cmd.table_type = ICC_TABLETYPE_PORT;
|
||||
if (parse_range(argv[2], 0xffff, &val1, &val2) < 0) {
|
||||
fprintf(stderr,
|
||||
"icc %s: invalid source port '%s'\n",
|
||||
actstr, argv[2]);
|
||||
return (1);
|
||||
}
|
||||
cmd.port.sport_from = (uint16_t)val1;
|
||||
cmd.port.sport_to = (uint16_t)val2;
|
||||
if (parse_range(argv[3], 0xffff, &val1, &val2) < 0) {
|
||||
fprintf(stderr,
|
||||
"icc %s: invalid dest port '%s'\n",
|
||||
actstr, argv[3]);
|
||||
return (1);
|
||||
}
|
||||
cmd.port.dport_from = (uint16_t)val1;
|
||||
cmd.port.dport_to = (uint16_t)val2;
|
||||
|
||||
} else if (strcasecmp(argv[1], "sport") == 0) {
|
||||
if (argc != 3) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl icc %s <iface>"
|
||||
" sport <port-range>\n", actstr);
|
||||
return (1);
|
||||
}
|
||||
cmd.table_type = ICC_TABLETYPE_PORT;
|
||||
if (parse_range(argv[2], 0xffff, &val1, &val2) < 0) {
|
||||
fprintf(stderr,
|
||||
"icc %s: invalid source port '%s'\n",
|
||||
actstr, argv[2]);
|
||||
return (1);
|
||||
}
|
||||
cmd.port.sport_from = (uint16_t)val1;
|
||||
cmd.port.sport_to = (uint16_t)val2;
|
||||
cmd.port.dport_from = 0;
|
||||
cmd.port.dport_to = 65535;
|
||||
|
||||
} else if (strcasecmp(argv[1], "dport") == 0) {
|
||||
if (argc != 3) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl icc %s <iface>"
|
||||
" dport <port-range>\n", actstr);
|
||||
return (1);
|
||||
}
|
||||
cmd.table_type = ICC_TABLETYPE_PORT;
|
||||
if (parse_range(argv[2], 0xffff, &val1, &val2) < 0) {
|
||||
fprintf(stderr,
|
||||
"icc %s: invalid dest port '%s'\n",
|
||||
actstr, argv[2]);
|
||||
return (1);
|
||||
}
|
||||
cmd.port.dport_from = (uint16_t)val1;
|
||||
cmd.port.dport_to = (uint16_t)val2;
|
||||
cmd.port.sport_from = 0;
|
||||
cmd.port.sport_to = 65535;
|
||||
|
||||
} else if (strcasecmp(argv[1], "vlan") == 0) {
|
||||
if (argc < 3 || argc > 4) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl icc %s <iface>"
|
||||
" vlan <id-range> [<prio-range>]\n", actstr);
|
||||
return (1);
|
||||
}
|
||||
cmd.table_type = ICC_TABLETYPE_VLAN;
|
||||
if (parse_range(argv[2], 8191, &val1, &val2) < 0) {
|
||||
fprintf(stderr,
|
||||
"icc %s: invalid VLAN ID '%s'\n",
|
||||
actstr, argv[2]);
|
||||
return (1);
|
||||
}
|
||||
cmd.vlan.vlan_from = (uint16_t)val1;
|
||||
cmd.vlan.vlan_to = (uint16_t)val2;
|
||||
if (argc == 4) {
|
||||
if (parse_range(argv[3], 7, &val1, &val2) < 0) {
|
||||
fprintf(stderr,
|
||||
"icc %s: invalid VLAN priority '%s'\n",
|
||||
actstr, argv[3]);
|
||||
return (1);
|
||||
}
|
||||
} else {
|
||||
val1 = 0;
|
||||
val2 = 7;
|
||||
}
|
||||
cmd.vlan.prio_from = (uint16_t)val1;
|
||||
cmd.vlan.prio_to = (uint16_t)val2;
|
||||
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"icc %s: unknown table type '%s'\n", actstr, argv[1]);
|
||||
return (1);
|
||||
}
|
||||
|
||||
resp_len = sizeof(raw);
|
||||
if (ctrl_command(fd, FPP_CMD_ICC_ADD_DELETE, &cmd, sizeof(cmd),
|
||||
&rc, raw, &resp_len) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0) {
|
||||
icc_errmsg(actstr, rc);
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (resp_len >= 2) {
|
||||
memcpy(&fpp_rc, raw, sizeof(fpp_rc));
|
||||
if (fpp_rc != 0) {
|
||||
icc_errmsg(actstr, (int16_t)fpp_rc);
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
|
||||
printf("icc %s: interface %u ok\n", actstr, iface);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ---- Query ---- */
|
||||
|
||||
/*
|
||||
* Format a bitmask as coalesced ranges. E.g., bits 1,2,3,6,7
|
||||
* becomes "1-3 6-7".
|
||||
*/
|
||||
static void
|
||||
print_bitmask(const uint8_t *bits, unsigned int maxbit)
|
||||
{
|
||||
unsigned int i, j;
|
||||
int first = 1;
|
||||
|
||||
for (i = 0; i <= maxbit; i++) {
|
||||
if (!testbit(bits, i))
|
||||
continue;
|
||||
j = i + 1;
|
||||
while (j <= maxbit && testbit(bits, j))
|
||||
j++;
|
||||
j--;
|
||||
if (!first)
|
||||
printf(" ");
|
||||
if (j == i)
|
||||
printf("%u", i);
|
||||
else
|
||||
printf("%u-%u", i, j);
|
||||
first = 0;
|
||||
i = j;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
print_entry(const fpp_icc_query_reply_t *r)
|
||||
{
|
||||
char buf[INET6_ADDRSTRLEN];
|
||||
|
||||
switch (r->table_type) {
|
||||
case ICC_TABLETYPE_ETHERTYPE:
|
||||
printf(" Ethertype: 0x%04x\n", r->ethertype.type);
|
||||
break;
|
||||
case ICC_TABLETYPE_PROTOCOL:
|
||||
printf(" Protocols: ");
|
||||
print_bitmask(r->protocol.ipproto, 255);
|
||||
printf("\n");
|
||||
break;
|
||||
case ICC_TABLETYPE_DSCP:
|
||||
printf(" DSCP values: ");
|
||||
print_bitmask(r->dscp.dscp_value, 63);
|
||||
printf("\n");
|
||||
break;
|
||||
case ICC_TABLETYPE_SADDR:
|
||||
case ICC_TABLETYPE_DADDR:
|
||||
{
|
||||
uint32_t v4tmp;
|
||||
memcpy(&v4tmp, &r->ipaddr.v4_addr, sizeof(v4tmp));
|
||||
printf(" IPv4 %s: %s/%u\n",
|
||||
r->table_type == ICC_TABLETYPE_SADDR ?
|
||||
"Source" : "Dest",
|
||||
inet_ntop(AF_INET, &v4tmp,
|
||||
buf, sizeof(buf)),
|
||||
r->ipaddr.v4_masklen);
|
||||
}
|
||||
break;
|
||||
case ICC_TABLETYPE_SADDR6:
|
||||
case ICC_TABLETYPE_DADDR6:
|
||||
{
|
||||
uint32_t v6tmp[4];
|
||||
memcpy(v6tmp, r->ipv6addr.v6_addr, 16);
|
||||
printf(" IPv6 %s: %s/%u\n",
|
||||
r->table_type == ICC_TABLETYPE_SADDR6 ?
|
||||
"Source" : "Dest",
|
||||
inet_ntop(AF_INET6, v6tmp,
|
||||
buf, sizeof(buf)),
|
||||
r->ipv6addr.v6_masklen);
|
||||
}
|
||||
break;
|
||||
case ICC_TABLETYPE_PORT:
|
||||
printf(" Ports: src %u-%u / dst %u-%u\n",
|
||||
r->port.sport_from, r->port.sport_to,
|
||||
r->port.dport_from, r->port.dport_to);
|
||||
break;
|
||||
case ICC_TABLETYPE_VLAN:
|
||||
printf(" VLAN: ID %u-%u / priority %u-%u\n",
|
||||
r->vlan.vlan_from, r->vlan.vlan_to,
|
||||
r->vlan.prio_from, r->vlan.prio_to);
|
||||
break;
|
||||
default:
|
||||
printf(" Unknown table_type %u\n", r->table_type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
cmd_query(int argc, char **argv, int fd)
|
||||
{
|
||||
fpp_icc_query_cmd_t cmd;
|
||||
uint8_t raw[sizeof(fpp_icc_query_reply_t)];
|
||||
fpp_icc_query_reply_t *resp;
|
||||
uint16_t resp_len;
|
||||
uint32_t iface_from, iface_to, iface;
|
||||
int16_t rc;
|
||||
|
||||
if (argc > 1) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl icc query [<iface>]\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (argc == 1) {
|
||||
if (parse_uint(argv[0], ICC_NUM_INTERFACES - 1,
|
||||
&iface_from) < 0) {
|
||||
fprintf(stderr,
|
||||
"icc query: invalid interface '%s' (0-%d)\n",
|
||||
argv[0], ICC_NUM_INTERFACES - 1);
|
||||
return (1);
|
||||
}
|
||||
iface_to = iface_from;
|
||||
} else {
|
||||
iface_from = 0;
|
||||
iface_to = ICC_NUM_INTERFACES - 1;
|
||||
}
|
||||
|
||||
resp = (fpp_icc_query_reply_t *)raw;
|
||||
|
||||
for (iface = iface_from; iface <= iface_to; iface++) {
|
||||
uint16_t action = ICC_ACTION_QUERY;
|
||||
|
||||
printf("ICC interface %u --\n", iface);
|
||||
|
||||
for (;;) {
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
cmd.action = action;
|
||||
cmd.interface = (uint8_t)iface;
|
||||
|
||||
resp_len = sizeof(raw);
|
||||
if (ctrl_command(fd, FPP_CMD_ICC_QUERY,
|
||||
&cmd, sizeof(cmd),
|
||||
&rc, raw, &resp_len) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0) {
|
||||
icc_errmsg("query", rc);
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (resp_len < 6) {
|
||||
/* Need at least rtncode + query_result +
|
||||
* interface + table_type */
|
||||
break;
|
||||
}
|
||||
|
||||
if (resp->query_result != 0 ||
|
||||
resp->interface != iface)
|
||||
break;
|
||||
|
||||
print_entry(resp);
|
||||
action = ICC_ACTION_QUERY_CONT;
|
||||
}
|
||||
|
||||
printf("-------------\n\n");
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ---- Main dispatcher ---- */
|
||||
|
||||
static void
|
||||
icc_usage(void)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl icc <command> [args...]\n\n"
|
||||
"Commands:\n"
|
||||
" reset "
|
||||
"Reset ICC state\n"
|
||||
" threshold <bmu1> <bmu2> "
|
||||
"Set buffer thresholds (0-1024)\n"
|
||||
" add <iface> <type> [args...] "
|
||||
"Add classification rule\n"
|
||||
" delete <iface> <type> [args...] "
|
||||
"Delete classification rule\n"
|
||||
" query [<iface>] "
|
||||
"Query ICC rules\n");
|
||||
}
|
||||
|
||||
int
|
||||
cmmctl_icc_main(int argc, char **argv, int fd)
|
||||
{
|
||||
|
||||
if (argc < 1) {
|
||||
icc_usage();
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (strcmp(argv[0], "reset") == 0)
|
||||
return (cmd_reset(fd));
|
||||
|
||||
if (strcmp(argv[0], "threshold") == 0)
|
||||
return (cmd_threshold(argc - 1, argv + 1, fd));
|
||||
|
||||
if (strcmp(argv[0], "add") == 0)
|
||||
return (cmd_add_delete(argc - 1, argv + 1, fd,
|
||||
ICC_ACTION_ADD));
|
||||
|
||||
if (strcmp(argv[0], "delete") == 0)
|
||||
return (cmd_add_delete(argc - 1, argv + 1, fd,
|
||||
ICC_ACTION_DELETE));
|
||||
|
||||
if (strcmp(argv[0], "query") == 0)
|
||||
return (cmd_query(argc - 1, argv + 1, fd));
|
||||
|
||||
fprintf(stderr, "unknown icc command: %s\n", argv[0]);
|
||||
icc_usage();
|
||||
return (1);
|
||||
}
|
||||
212
fastpath/cmmctl/cmmctl_l2tp.c
Normal file
212
fastpath/cmmctl/cmmctl_l2tp.c
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
/*
|
||||
* cmmctl_l2tp.c — L2TP tunnel interface CLI sub-commands
|
||||
*
|
||||
* cmmctl l2tp add <ifname> <af> <local-addr> <peer-addr>
|
||||
* <local-port> <peer-port> <local-tun-id> <peer-tun-id>
|
||||
* <local-ses-id> <peer-ses-id> [options [dscp [queue]]]
|
||||
* cmmctl l2tp del <ifname>
|
||||
*
|
||||
* Copyright 2026 Mono Technologies Inc.
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cmmctl.h"
|
||||
#include "fpp.h"
|
||||
|
||||
static void
|
||||
l2tp_usage(void)
|
||||
{
|
||||
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl l2tp <command>\n\n"
|
||||
"Commands:\n"
|
||||
" add <ifname> <inet|inet6> <local-addr> <peer-addr>\n"
|
||||
" <local-port> <peer-port> <local-tun-id> <peer-tun-id>\n"
|
||||
" <local-ses-id> <peer-ses-id> [options [dscp [queue]]]\n"
|
||||
" del <ifname>\n\n"
|
||||
"Examples:\n"
|
||||
" cmmctl l2tp add l2tpeth0 inet 10.0.0.1 10.0.0.2 "
|
||||
"1701 1701 100 200 300 400\n"
|
||||
" cmmctl l2tp del l2tpeth0\n");
|
||||
}
|
||||
|
||||
static int
|
||||
l2tp_add(int fd, int argc, char **argv)
|
||||
{
|
||||
struct cmm_ctrl_l2tp_add cmd;
|
||||
int16_t rc;
|
||||
unsigned int tmp;
|
||||
sa_family_t af;
|
||||
|
||||
if (argc < 10) {
|
||||
fprintf(stderr,
|
||||
"usage: cmmctl l2tp add <ifname> <inet|inet6> "
|
||||
"<local-addr> <peer-addr>\n"
|
||||
" <local-port> <peer-port> "
|
||||
"<local-tun-id> <peer-tun-id>\n"
|
||||
" <local-ses-id> <peer-ses-id> "
|
||||
"[options [dscp [queue]]]\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
memset(&cmd, 0, sizeof(cmd));
|
||||
|
||||
/* Interface name */
|
||||
strlcpy(cmd.ifname, argv[0], sizeof(cmd.ifname));
|
||||
|
||||
/* Address family */
|
||||
if (strcmp(argv[1], "inet") == 0)
|
||||
af = AF_INET;
|
||||
else if (strcmp(argv[1], "inet6") == 0)
|
||||
af = AF_INET6;
|
||||
else {
|
||||
fprintf(stderr, "invalid address family: %s "
|
||||
"(use inet or inet6)\n", argv[1]);
|
||||
return (1);
|
||||
}
|
||||
cmd.af = af;
|
||||
|
||||
/* Local address */
|
||||
if (inet_pton(af, argv[2], cmd.local_addr) != 1) {
|
||||
fprintf(stderr, "invalid local address: %s\n", argv[2]);
|
||||
return (1);
|
||||
}
|
||||
|
||||
/* Peer address */
|
||||
if (inet_pton(af, argv[3], cmd.peer_addr) != 1) {
|
||||
fprintf(stderr, "invalid peer address: %s\n", argv[3]);
|
||||
return (1);
|
||||
}
|
||||
|
||||
/* Ports (convert to network byte order) */
|
||||
if (sscanf(argv[4], "%u", &tmp) != 1 || tmp > 65535) {
|
||||
fprintf(stderr, "invalid local port: %s\n", argv[4]);
|
||||
return (1);
|
||||
}
|
||||
cmd.local_port = htons((uint16_t)tmp);
|
||||
|
||||
if (sscanf(argv[5], "%u", &tmp) != 1 || tmp > 65535) {
|
||||
fprintf(stderr, "invalid peer port: %s\n", argv[5]);
|
||||
return (1);
|
||||
}
|
||||
cmd.peer_port = htons((uint16_t)tmp);
|
||||
|
||||
/* Tunnel IDs */
|
||||
if (sscanf(argv[6], "%u", &tmp) != 1 || tmp > 65535) {
|
||||
fprintf(stderr, "invalid local tunnel ID: %s\n", argv[6]);
|
||||
return (1);
|
||||
}
|
||||
cmd.local_tun_id = (uint16_t)tmp;
|
||||
|
||||
if (sscanf(argv[7], "%u", &tmp) != 1 || tmp > 65535) {
|
||||
fprintf(stderr, "invalid peer tunnel ID: %s\n", argv[7]);
|
||||
return (1);
|
||||
}
|
||||
cmd.peer_tun_id = (uint16_t)tmp;
|
||||
|
||||
/* Session IDs */
|
||||
if (sscanf(argv[8], "%u", &tmp) != 1 || tmp > 65535) {
|
||||
fprintf(stderr, "invalid local session ID: %s\n", argv[8]);
|
||||
return (1);
|
||||
}
|
||||
cmd.local_ses_id = (uint16_t)tmp;
|
||||
|
||||
if (sscanf(argv[9], "%u", &tmp) != 1 || tmp > 65535) {
|
||||
fprintf(stderr, "invalid peer session ID: %s\n", argv[9]);
|
||||
return (1);
|
||||
}
|
||||
cmd.peer_ses_id = (uint16_t)tmp;
|
||||
|
||||
/* Optional: options, dscp, queue */
|
||||
if (argc > 10) {
|
||||
if (sscanf(argv[10], "%u", &tmp) != 1) {
|
||||
fprintf(stderr, "invalid options: %s\n", argv[10]);
|
||||
return (1);
|
||||
}
|
||||
cmd.options = (uint16_t)tmp;
|
||||
}
|
||||
if (argc > 11) {
|
||||
if (sscanf(argv[11], "%u", &tmp) != 1 || tmp > 63) {
|
||||
fprintf(stderr, "invalid dscp: %s\n", argv[11]);
|
||||
return (1);
|
||||
}
|
||||
cmd.dscp = (uint16_t)tmp;
|
||||
}
|
||||
if (argc > 12) {
|
||||
if (sscanf(argv[12], "%u", &tmp) != 1 || tmp > 255) {
|
||||
fprintf(stderr, "invalid queue: %s\n", argv[12]);
|
||||
return (1);
|
||||
}
|
||||
cmd.queue = (uint8_t)tmp;
|
||||
}
|
||||
|
||||
if (ctrl_command(fd, CMM_CTRL_CMD_L2TP_ADD, &cmd, sizeof(cmd),
|
||||
&rc, NULL, NULL) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0) {
|
||||
fprintf(stderr, "l2tp add %s: error %d\n", cmd.ifname, rc);
|
||||
return (1);
|
||||
}
|
||||
|
||||
printf("l2tp: %s added (tun=%u/%u ses=%u/%u)\n",
|
||||
cmd.ifname,
|
||||
cmd.local_tun_id, cmd.peer_tun_id,
|
||||
cmd.local_ses_id, cmd.peer_ses_id);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
l2tp_del(int fd, int argc, char **argv)
|
||||
{
|
||||
char name[IFNAMSIZ];
|
||||
int16_t rc;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: cmmctl l2tp del <ifname>\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
memset(name, 0, sizeof(name));
|
||||
strlcpy(name, argv[0], sizeof(name));
|
||||
|
||||
if (ctrl_command(fd, CMM_CTRL_CMD_L2TP_DEL, name, sizeof(name),
|
||||
&rc, NULL, NULL) < 0)
|
||||
return (1);
|
||||
|
||||
if (rc != 0) {
|
||||
fprintf(stderr, "l2tp del %s: error %d\n", name, rc);
|
||||
return (1);
|
||||
}
|
||||
|
||||
printf("l2tp: %s deleted\n", name);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
cmmctl_l2tp_main(int argc, char **argv, int fd)
|
||||
{
|
||||
|
||||
if (argc < 1) {
|
||||
l2tp_usage();
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (strcmp(argv[0], "add") == 0)
|
||||
return (l2tp_add(fd, argc - 1, argv + 1));
|
||||
if (strcmp(argv[0], "del") == 0)
|
||||
return (l2tp_del(fd, argc - 1, argv + 1));
|
||||
|
||||
fprintf(stderr, "unknown l2tp command: %s\n", argv[0]);
|
||||
l2tp_usage();
|
||||
return (1);
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Reference in a new issue