ale-leehrtz has uploaded this change for review.
dahdi_trunkdev: add SD-RI-PLL software clock recovery
Without a hardware clock source, the DAHDI coretimer that paces a
trunkdev runs from the host clock, which is not the clock of the remote
E1 that paces the incoming E1-over-IP stream, so the IP->E1 FIFO slowly
drains or overflows.
Close that loop in software: once per second, read the IP->E1 FIFO
level of line 0, run the PID controller, and steer the coretimer via the
new DAHDI_SW_CLOCK_SET ioctl of the osmocom DAHDI fork. The ioctl is
defined locally if the DAHDI headers lack it; on a kernel without
support the PLL disables itself with an error message. As the trunkdev
span is the timing master in this setup, the dahdi_trunkdev module must
be loaded with permit_clock_master=1.
The PLL is off by default. New VTY commands on 'interface N
dahdi-trunkdev':
pll mode (off|fixed|self-tuning)
pll pid-gains KP KI KD
pll target-fifo <1-1792>
pll self-tuning hold-time <0-31536000>
pll self-tuning window <600-86400>
pll log-file FILENAME / no pll log-file
In self-tuning mode, the configured gains are used for at least the
hold-time while the link is characterized; once the tuner has its
first estimate (about 10 valid estimation windows, 1 h each by
default), the self-tuned gains are applied bumplessly and updated
after every window. A watchdog reverts to the configured gains if the
loop runs away. The optional log file gets one CSV line per tick for
offline analysis. 'show interface' shows the FIFO levels and the PLL
state.
Related: OS#7116
Change-Id: I20e0d62275c8422e3b272630dc52cebd80b66a15
---
M src/Makefile.am
A src/dahdi_plltrunkdev.h
M src/dahdi_trunkdev.c
M src/e1d.h
M src/vty.c
5 files changed, 620 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-e1d refs/changes/32/44232/1
diff --git a/src/Makefile.am b/src/Makefile.am
index 2670b9f..2f9fe73 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -22,6 +22,7 @@
noinst_HEADERS = \
+ dahdi_plltrunkdev.h \
e1d.h \
ice1usb_proto.h \
log.h \
diff --git a/src/dahdi_plltrunkdev.h b/src/dahdi_plltrunkdev.h
new file mode 100644
index 0000000..fa0f943
--- /dev/null
+++ b/src/dahdi_plltrunkdev.h
@@ -0,0 +1,78 @@
+/*
+ * dahdi_plltrunkdev.h
+ *
+ * Per-interface state for the SD-RI-PLL (software-defined, reference-
+ * independent PLL) running on a DAHDI trunkdev line: the PID controller,
+ * the optional self-tuning layer and the periodic timer.
+ *
+ * (C) 2025-2026 by C. Alejandra Carreon-Jimenez <alejandra.carreon@ale-leehrtz.org>
+ *
+ * All Rights Reserved
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <time.h>
+
+#include <sys/ioctl.h>
+#include <dahdi/user.h>
+
+#include <osmocom/core/timer.h>
+
+#include "pll_pid.h"
+#include "pll_tune.h"
+
+struct e1_line;
+
+/* The software clock ioctls are not part of every DAHDI version. Define them
+ * here so osmo-e1d builds against stock DAHDI headers; on a kernel without
+ * support, DAHDI_SW_CLOCK_SET fails at runtime and the PLL disables itself. */
+#ifndef DAHDI_SW_CLOCK_SET
+struct dahdi_sw_clock {
+ int32_t interval_sec; /* coretimer start interval, seconds */
+ int32_t interval_nsec; /* coretimer start interval, nanoseconds */
+ int32_t target_fudge; /* last fudge set via DAHDI_SW_CLOCK_SET */
+ int32_t current_fudge; /* fudge currently applied by the coretimer */
+};
+#define DAHDI_SW_CLOCK_GET _IOR(DAHDI_CODE, 252, struct dahdi_sw_clock)
+#define DAHDI_SW_CLOCK_SET _IOW(DAHDI_CODE, 251, int32_t) /* new target fudge */
+#endif
+
+enum sdripll_phase {
+ SDRIPLL_PHASE_BASELINE = 0, /* configured gains; tuner only estimates */
+ SDRIPLL_PHASE_TUNED = 1, /* tuner-proposed gains applied */
+ SDRIPLL_PHASE_REVERTED = 2, /* watchdog tripped, back to configured gains */
+};
+
+struct dahdi_trunkdev_pll {
+ struct e1_line *line; /* line whose IP->E1 FIFO is regulated */
+ int chan_fd; /* /dev/dahdi/channel, for DAHDI_SW_CLOCK_SET */
+ struct osmo_timer_list timer;
+ struct pll_pid_controller pid;
+ float kp0, ki0; /* configured gains, for the watchdog revert */
+ bool unsupported; /* kernel lacks DAHDI_SW_CLOCK_SET; stopped */
+
+ /* online gain adaptation (pll mode self-tuning) */
+ bool tune_enabled;
+ struct pll_tune tune;
+ enum sdripll_phase phase;
+ time_t t_start;
+ unsigned int hold_s;
+ double wd_ewma; /* watchdog: fraction of bad ticks, ~30 min */
+ FILE *csv;
+};
diff --git a/src/dahdi_trunkdev.c b/src/dahdi_trunkdev.c
index 11c5b9f..660374f 100644
--- a/src/dahdi_trunkdev.c
+++ b/src/dahdi_trunkdev.c
@@ -2,6 +2,7 @@
* trunkdev.c
*
* (C) 2022 by Harald Welte <laforge@osmocom.org>
+ * (C) 2025-2026 by C. Alejandra Carreon-Jimenez <alejandra.carreon@ale-leehrtz.org> (SD-RI-PLL)
*
* All Rights Reserved
*
@@ -24,18 +25,27 @@
#include <string.h>
#include <talloc.h>
#include <fcntl.h>
+#include <math.h>
+#include <stdlib.h>
+#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <osmocom/core/utils.h>
+#include <osmocom/core/stat_item.h>
#include <dahdi/user.h>
#include "e1d.h"
#include "log.h"
+/* SD-RI-PLL: software-defined reference-independent PLL for E1 timing */
+#include "dahdi_plltrunkdev.h"
+#include "octoi/octoi_sock.h" /* struct octoi_peer */
+#include "octoi/e1oip.h" /* struct e1oip_line, FIFO level stat */
+
/***********************************************************************
* low-level trunkdev routines
***********************************************************************/
@@ -67,6 +77,8 @@
struct e1_trunkdev_intf_data {
/* file descriptor to the character device /dev/dahdi/trunkdev */
struct osmo_fd ofd;
+ /* SD-RI-PLL, NULL unless running */
+ struct dahdi_trunkdev_pll *pll;
};
/* file-descriptor call-back. Triggered by DAHDI via poll(), whenever
@@ -132,6 +144,307 @@
}
+/***********************************************************************
+ * SD-RI-PLL: software clock recovery
+ ***********************************************************************/
+
+/* Without a hardware clock source, the DAHDI coretimer that paces the trunkdev
+ * runs from the host clock, which is not the clock of the remote E1 that paces
+ * the incoming E1-over-IP stream: the IP->E1 FIFO slowly drains or overflows.
+ * The SD-RI-PLL closes that loop in software. Once per second a PID controller
+ * compares the FIFO level against a setpoint and steers the coretimer rate via
+ * DAHDI_SW_CLOCK_SET. In 'pll mode self-tuning' the PID gains are additionally
+ * adapted online from the measured link noise and oscillator wander (see
+ * pll_tune.h). The trunkdev span then is the timing master, so the
+ * dahdi_trunkdev kernel module must be loaded with permit_clock_master=1. */
+
+/* Watchdog: a tick is "bad" if the FIFO is empty or far from N*. A loop that
+ * runs away shows up as excursions AND as underruns (FIFO == 0), which the
+ * tuner treats as link-down, so the watchdog must look at every tick.
+ * Trip when the bad fraction over ~30 min exceeds one half. */
+#define SDRIPLL_WD_BAND 300.0
+#define SDRIPLL_WD_TAU 1800.0
+#define SDRIPLL_WD_TRIP 0.5
+
+static const struct value_string sdripll_phase_names[] = {
+ { SDRIPLL_PHASE_BASELINE, "baseline" },
+ { SDRIPLL_PHASE_TUNED, "tuned" },
+ { SDRIPLL_PHASE_REVERTED, "reverted" },
+ { 0, NULL }
+};
+
+static time_t mono_s(void)
+{
+ struct timespec ts;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return ts.tv_sec;
+}
+
+static void sdripll_log(struct dahdi_trunkdev_pll *pll, double fifo_raw, bool up)
+{
+ struct timespec ts;
+
+ if (!pll->csv)
+ return;
+ clock_gettime(CLOCK_REALTIME, &ts);
+ fprintf(pll->csv, "%.3f,%.0f,%d,%.6g,%.6g,%.6g,%.4g,%.4g,%d,%d\n",
+ ts.tv_sec + ts.tv_nsec * 1e-9, fifo_raw, pll->pid.fudge,
+ (double)pll->pid.kp, (double)pll->pid.ki, (double)pll->pid.tau_f,
+ pll->tune_enabled ? pll_tune_sigma_v(&pll->tune) : -1.0,
+ pll->tune_enabled ? pll->tune.q_phi : -1.0, up ? 1 : 0, (int)pll->phase);
+}
+
+static void sdripll_apply_tuned(struct dahdi_trunkdev_pll *pll)
+{
+ pll_pid_set_gains(&pll->pid, (float)pll->tune.kp, (float)pll->tune.ki,
+ (float)pll->tune.tau_f);
+ LOGPLI(pll->line, DTRUNKDEV, LOGL_NOTICE,
+ "SD-RI-PLL: gains Kp=%.1f Ki=%.4f tau_f=%.0fs (sigma_v=%.1f, loop period %.2f h)\n",
+ pll->tune.kp, pll->tune.ki, pll->tune.tau_f, pll_tune_sigma_v(&pll->tune),
+ 2.0 * M_PI / pll->tune.wn / 3600.0);
+}
+
+/* Push a new fudge to the coretimer. Returns false if the kernel lacks the
+ * ioctl (DAHDI answers unknown ioctls with ENOSYS), in which case there is
+ * nothing to regulate and the caller stops the PLL. */
+static bool sdripll_set_fudge(struct dahdi_trunkdev_pll *pll, int32_t fudge)
+{
+ if (ioctl(pll->chan_fd, DAHDI_SW_CLOCK_SET, &fudge) == 0)
+ return true;
+
+ if (errno == ENOSYS || errno == ENOTTY || errno == EINVAL) {
+ LOGPLI(pll->line, DTRUNKDEV, LOGL_ERROR, "SD-RI-PLL: DAHDI kernel module has no "
+ "DAHDI_SW_CLOCK_SET support (%s), PLL disabled\n", strerror(errno));
+ pll->unsupported = true;
+ return false;
+ }
+
+ LOGPLI(pll->line, DTRUNKDEV, LOGL_ERROR, "SD-RI-PLL: DAHDI_SW_CLOCK_SET failed: %s\n",
+ strerror(errno));
+ return true;
+}
+
+/* Periodic (1 Hz) tick: read the IP->E1 FIFO occupancy, run one control
+ * step, and push the resulting fudge to the DAHDI coretimer. */
+static void sdripll_timer_cb(void *data)
+{
+ struct dahdi_trunkdev_pll *pll = data;
+ struct e1_line *line = pll->line;
+ struct octoi_peer *peer = line->octoi_peer;
+ int32_t current_fifo = 0, new_fudge;
+ bool have_fifo = false, up = true, tdm_alive;
+ double z, e_f;
+
+ /* The FIFO stats only exist once the OCTOI peer is up */
+ if (peer && peer->iline && peer->iline->stats) {
+ current_fifo = osmo_stat_item_get_last(
+ osmo_stat_item_group_get_item(peer->iline->stats, LINE_STAT_E1oIP_E1T_FIFO));
+ have_fifo = true;
+ }
+ /* TDM received within the last 3 s: the link is really up (same rule
+ * as the OCTOI FSMs; last_rx_tdm is CLOCK_MONOTONIC) */
+ tdm_alive = peer && mono_s() - peer->last_rx_tdm <= 3;
+
+ if (!pll->tune_enabled) {
+ /* plain PID, exactly as published */
+ if (!have_fifo)
+ goto rearm;
+ z = current_fifo;
+ goto update;
+ }
+
+ /* Tuner input conditioning. Model sign: e = FIFO - N* = -err_f, and a
+ * positive fudge drains the FIFO, so u_applied = +fudge. A FIFO <= 0 or
+ * no TDM arriving (link down / underrun) returns false: the tuner skips
+ * the tick. */
+ z = (have_fifo && tdm_alive) ? (double)current_fifo : 0.0;
+ e_f = pll->pid.have_err_f ? -(double)pll->pid.err_f : NAN;
+ up = pll_tune_input(&pll->tune, &z, e_f, (double)pll->pid.fudge);
+
+ if (pll->phase == SDRIPLL_PHASE_BASELINE) {
+ /* baseline: the tuner only observes; the PID gets the raw FIFO,
+ * exactly as the published controller (no gate, no holdover) */
+ if (!have_fifo)
+ goto log;
+ z = current_fifo;
+ } else if (!up) {
+ /* tuned: holdover, the kernel keeps applying the last fudge and
+ * the integral does not wind up on a stale or empty FIFO */
+ goto log;
+ }
+
+update:
+ new_fudge = pll_pid_update(&pll->pid, (int32_t)lrint(z));
+
+ LOGPLI(line, DTRUNKDEV, LOGL_DEBUG, "SD-RI-PLL: FIFO=%d target=%.0f -> fudge=%d\n",
+ current_fifo, (double)pll->pid.target, new_fudge);
+
+ if (!sdripll_set_fudge(pll, new_fudge))
+ return; /* no kernel support: do not re-arm */
+
+ if (!pll->tune_enabled)
+ goto log;
+
+ if (up && pll_tune_end_tick(&pll->tune) && pll->phase == SDRIPLL_PHASE_TUNED)
+ sdripll_apply_tuned(pll);
+
+ /* end of the baseline phase: switch to the proposed gains */
+ if (pll->phase == SDRIPLL_PHASE_BASELINE && pll->tune.wn > 0.0 &&
+ mono_s() - pll->t_start >= (time_t)pll->hold_s) {
+ sdripll_apply_tuned(pll);
+ pll->phase = SDRIPLL_PHASE_TUNED;
+ }
+
+log:
+ /* Watchdog: persistent runaway after the switch -> back to the configured
+ * gains. Only judge ticks while TDM is actually arriving: an empty FIFO
+ * with the link up is an underrun (bad); with no TDM for >3 s it is a link
+ * outage and says nothing about the loop. */
+ if (pll->tune_enabled && pll->phase == SDRIPLL_PHASE_TUNED && have_fifo && tdm_alive) {
+ double bad = (current_fifo <= 0 ||
+ fabs((double)current_fifo - (double)pll->pid.target) > SDRIPLL_WD_BAND);
+ pll->wd_ewma += (bad - pll->wd_ewma) / SDRIPLL_WD_TAU;
+ if (pll->wd_ewma > SDRIPLL_WD_TRIP) {
+ pll_pid_set_gains(&pll->pid, pll->kp0, pll->ki0, 0.0f);
+ pll->phase = SDRIPLL_PHASE_REVERTED;
+ LOGPLI(line, DTRUNKDEV, LOGL_ERROR,
+ "SD-RI-PLL: watchdog tripped, reverted to configured gains\n");
+ }
+ }
+ sdripll_log(pll, have_fifo ? (double)current_fifo : 0.0, up && have_fifo);
+rearm:
+ osmo_timer_schedule(&pll->timer, 1, 0);
+}
+
+static void trunkdev_pll_start(struct e1_intf *e1i)
+{
+ struct e1_trunkdev_intf_data *tid = e1i->drv_data;
+ struct dahdi_trunkdev_pll *pll;
+ struct e1_line *e1l;
+ int cfd;
+
+ if (!tid || tid->pll || e1i->dahdi_trunkdev.pll.mode == E1_TRUNKDEV_PLL_OFF)
+ return;
+
+ e1l = e1_intf_find_line(e1i, 0);
+ if (!e1l)
+ return;
+
+ cfd = open("/dev/dahdi/channel", O_RDWR);
+ if (cfd < 0) {
+ LOGPLI(e1l, DTRUNKDEV, LOGL_ERROR,
+ "SD-RI-PLL: cannot open /dev/dahdi/channel, PLL disabled: %s\n", strerror(errno));
+ return;
+ }
+
+ pll = talloc_zero(tid, struct dahdi_trunkdev_pll);
+ OSMO_ASSERT(pll);
+ pll->line = e1l;
+ pll->chan_fd = cfd;
+ pll->kp0 = e1i->dahdi_trunkdev.pll.kp;
+ pll->ki0 = e1i->dahdi_trunkdev.pll.ki;
+ pll_pid_init(&pll->pid, pll->kp0, pll->ki0, e1i->dahdi_trunkdev.pll.kd,
+ (float)e1i->dahdi_trunkdev.pll.target_fifo);
+
+ pll->tune_enabled = e1i->dahdi_trunkdev.pll.mode == E1_TRUNKDEV_PLL_SELF_TUNING;
+ pll->phase = SDRIPLL_PHASE_BASELINE;
+ if (pll->tune_enabled) {
+ struct pll_tune_params tp;
+
+ pll->hold_s = e1i->dahdi_trunkdev.pll.hold_s;
+ pll->t_start = mono_s();
+ pll_tune_default_params(&tp);
+ tp.n_star = e1i->dahdi_trunkdev.pll.target_fifo;
+ tp.window = e1i->dahdi_trunkdev.pll.window_s;
+ pll_tune_init(&pll->tune, &tp);
+ }
+
+ if (e1i->dahdi_trunkdev.pll.log_file) {
+ const char *path = e1i->dahdi_trunkdev.pll.log_file;
+
+ pll->csv = fopen(path, "a");
+ if (pll->csv) {
+ setvbuf(pll->csv, NULL, _IOLBF, 0);
+ if (ftell(pll->csv) == 0)
+ fprintf(pll->csv, "t,fifo,fudge,kp,ki,tau_f,sigma_v,q_phi,link_up,phase\n");
+ } else {
+ LOGPLI(e1l, DTRUNKDEV, LOGL_ERROR, "SD-RI-PLL: cannot open log file %s: %s\n",
+ path, strerror(errno));
+ }
+ }
+
+ osmo_timer_setup(&pll->timer, sdripll_timer_cb, pll);
+ osmo_timer_schedule(&pll->timer, 1, 0);
+ tid->pll = pll;
+
+ LOGPLI(e1l, DTRUNKDEV, LOGL_NOTICE, "SD-RI-PLL: started, mode %s, target FIFO %u\n",
+ pll->tune_enabled ? "self-tuning" : "fixed", e1i->dahdi_trunkdev.pll.target_fifo);
+ if (pll->tune_enabled)
+ LOGPLI(e1l, DTRUNKDEV, LOGL_NOTICE, "SD-RI-PLL: configured gains for at least %u s, then "
+ "self-tuned gains (estimation window %u s)\n", pll->hold_s, pll->tune.p.window);
+}
+
+static void trunkdev_pll_stop(struct e1_intf *e1i)
+{
+ struct e1_trunkdev_intf_data *tid = e1i->drv_data;
+ struct dahdi_trunkdev_pll *pll;
+
+ if (!tid || !tid->pll)
+ return;
+ pll = tid->pll;
+
+ osmo_timer_del(&pll->timer);
+ /* do not leave the coretimer running at a stale rate */
+ if (!pll->unsupported)
+ sdripll_set_fudge(pll, 0);
+ close(pll->chan_fd);
+ if (pll->csv)
+ fclose(pll->csv);
+ talloc_free(pll);
+ tid->pll = NULL;
+}
+
+void
+e1_dahdi_trunkdev_pll_defaults(struct e1_intf *e1i)
+{
+ e1i->dahdi_trunkdev.pll.mode = E1_TRUNKDEV_PLL_OFF;
+ e1i->dahdi_trunkdev.pll.kp = PLL_PID_DEFAULT_KP;
+ e1i->dahdi_trunkdev.pll.ki = PLL_PID_DEFAULT_KI;
+ e1i->dahdi_trunkdev.pll.kd = PLL_PID_DEFAULT_KD;
+ e1i->dahdi_trunkdev.pll.target_fifo = PLL_PID_DEFAULT_TARGET_FIFO;
+ e1i->dahdi_trunkdev.pll.hold_s = 86400;
+ e1i->dahdi_trunkdev.pll.window_s = 3600;
+ e1i->dahdi_trunkdev.pll.log_file = NULL;
+}
+
+/* Apply a changed PLL configuration to an open trunkdev. Note that this
+ * restarts the controller from zero (integral, tuner estimates). */
+void
+e1_dahdi_trunkdev_pll_restart(struct e1_intf *e1i)
+{
+ trunkdev_pll_stop(e1i);
+ trunkdev_pll_start(e1i);
+}
+
+const char *
+e1_dahdi_trunkdev_pll_state_str(const struct e1_intf *e1i)
+{
+ static char buf[128];
+ const struct e1_trunkdev_intf_data *tid = e1i->drv_data;
+ const struct dahdi_trunkdev_pll *pll = tid ? tid->pll : NULL;
+
+ if (!pll)
+ return "inactive";
+ if (pll->unsupported)
+ return "disabled (no DAHDI_SW_CLOCK_SET in kernel)";
+
+ snprintf(buf, sizeof(buf), "%s, fudge %d, Kp %.4g, Ki %.4g",
+ pll->tune_enabled ? get_value_string(sdripll_phase_names, pll->phase) : "fixed",
+ pll->pid.fudge, (double)pll->pid.kp, (double)pll->pid.ki);
+ return buf;
+}
+
int
e1_dahdi_trunkdev_open(struct e1_intf *e1i)
{
@@ -197,6 +510,9 @@
llist_for_each_entry(e1l, &e1i->lines, list)
e1_line_active(e1l);
+ /* SD-RI-PLL software clock recovery, if configured */
+ trunkdev_pll_start(e1i);
+
return 0;
}
@@ -216,6 +532,8 @@
return 0;
}
+ trunkdev_pll_stop(e1i);
+
osmo_fd_unregister(&tid->ofd);
/* we're not deleting the dahdi trunkdev as that might upset the applications using
diff --git a/src/e1d.h b/src/e1d.h
index 7d2d4c6..a0eb484 100644
--- a/src/e1d.h
+++ b/src/e1d.h
@@ -2,6 +2,7 @@
* e1d.h
*
* (C) 2019 by Sylvain Munaut <tnt@246tNt.com>
+ * (C) 2026 by C. Alejandra Carreon-Jimenez <alejandra.carreon@ale-leehrtz.org>
*
* All Rights Reserved
*
@@ -213,6 +214,12 @@
extern const struct value_string e1_driver_names[];
extern const struct value_string e1_line_mode_names[];
+enum e1_trunkdev_pll_mode {
+ E1_TRUNKDEV_PLL_OFF, /* coretimer left alone (default) */
+ E1_TRUNKDEV_PLL_FIXED, /* PID with the configured gains */
+ E1_TRUNKDEV_PLL_SELF_TUNING, /* PID gains adapted online */
+};
+
struct e1_intf {
struct llist_head list;
@@ -229,6 +236,15 @@
} usb;
struct {
char *name;
+ /* SD-RI-PLL: software clock recovery, see dahdi_trunkdev.c */
+ struct {
+ enum e1_trunkdev_pll_mode mode;
+ float kp, ki, kd;
+ unsigned int target_fifo;
+ unsigned int hold_s; /* self-tuning: baseline time before adapting */
+ unsigned int window_s; /* self-tuning: frequency estimation window */
+ char *log_file; /* optional CSV, one line per tick */
+ } pll;
} dahdi_trunkdev;
bool vty_created;
@@ -306,6 +322,15 @@
int
e1_dahdi_trunkdev_close(struct e1_intf *e1i);
+
+void
+e1_dahdi_trunkdev_pll_defaults(struct e1_intf *e1i);
+
+void
+e1_dahdi_trunkdev_pll_restart(struct e1_intf *e1i);
+
+const char *
+e1_dahdi_trunkdev_pll_state_str(const struct e1_intf *e1i);
#endif
int
diff --git a/src/vty.c b/src/vty.c
index c955a3f..9dd5266 100644
--- a/src/vty.c
+++ b/src/vty.c
@@ -1,5 +1,6 @@
/* osmo-e1d VTY interface */
/* (C) 2020 by Harald Welte <laforge@osmocom.org>
+ * (C) 2026 by C. Alejandra Carreon-Jimenez <alejandra.carreon@ale-leehrtz.org>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
@@ -42,6 +43,11 @@
#include "e1d.h"
#include "usb.h"
+#include "octoi/octoi_sock.h"
+#include "octoi/e1oip.h"
+#ifdef HAVE_DAHDI_TRUNKDEV
+#include "pll_pid.h"
+#endif
struct e1_daemon *vty_e1d;
@@ -116,6 +122,25 @@
e1_usb_intf_gpsdo_state_string(buf, sizeof(buf), intf);
vty_out(vty, " GPS-DO: %s%s", buf, VTY_NEWLINE);
break;
+#ifdef HAVE_DAHDI_TRUNKDEV
+ case E1_DRIVER_DAHDI_TRUNKDEV:
+ {
+ /* FIFO levels of the E1-over-IP line; IP->E1 is what the PLL regulates */
+ const struct e1_line *line = e1_intf_find_line((struct e1_intf *)intf, 0);
+
+ if (line && line->octoi_peer && line->octoi_peer->iline &&
+ line->octoi_peer->iline->stats) {
+ struct osmo_stat_item_group *st = line->octoi_peer->iline->stats;
+
+ vty_out(vty, " FIFO E1->IP: %d frames, IP->E1: %d frames%s",
+ osmo_stat_item_get_last(osmo_stat_item_group_get_item(st, LINE_STAT_E1oIP_E1O_FIFO)),
+ osmo_stat_item_get_last(osmo_stat_item_group_get_item(st, LINE_STAT_E1oIP_E1T_FIFO)),
+ VTY_NEWLINE);
+ }
+ vty_out(vty, " Software PLL: %s%s", e1_dahdi_trunkdev_pll_state_str(intf), VTY_NEWLINE);
+ break;
+ }
+#endif
default:
break;
}
@@ -320,6 +345,8 @@
vty_out(vty, "%% Could not create interface%s", VTY_NEWLINE);
return CMD_WARNING;
}
+ if (intf->drv != E1_DRIVER_DAHDI_TRUNKDEV)
+ e1_dahdi_trunkdev_pll_defaults(intf);
intf->drv = E1_DRIVER_DAHDI_TRUNKDEV;
intf->vty_created = true;
@@ -393,6 +420,167 @@
return CMD_SUCCESS;
}
+
+#define PLL_STR "Software clock recovery (SD-RI-PLL) of the DAHDI coretimer\n"
+
+static const struct value_string pll_mode_names[] = {
+ { E1_TRUNKDEV_PLL_OFF, "off" },
+ { E1_TRUNKDEV_PLL_FIXED, "fixed" },
+ { E1_TRUNKDEV_PLL_SELF_TUNING, "self-tuning" },
+ { 0, NULL }
+};
+
+static bool parse_gain(struct vty *vty, const char *str, float *out)
+{
+ char *end;
+ float f = strtof(str, &end);
+
+ if (end == str || *end != '\0' || !(f >= 0.0f) || f > 1e9f) {
+ vty_out(vty, "%% Invalid gain '%s', expecting a non-negative number%s", str, VTY_NEWLINE);
+ return false;
+ }
+ *out = f;
+ return true;
+}
+
+DEFUN(cfg_e1d_if_pll_mode, cfg_e1d_if_pll_mode_cmd,
+ "pll mode (off|fixed|self-tuning)",
+ PLL_STR "Operating mode of the software clock recovery\n"
+ "Do not touch the DAHDI coretimer (default)\n"
+ "PID controller with the configured gains\n"
+ "PID controller; after 'pll self-tuning hold-time', gains are adapted online\n")
+{
+ struct e1_intf *intf = vty->index;
+
+ if (intf->drv != E1_DRIVER_DAHDI_TRUNKDEV)
+ return CMD_WARNING;
+
+ intf->dahdi_trunkdev.pll.mode = get_string_value(pll_mode_names, argv[0]);
+ e1_dahdi_trunkdev_pll_restart(intf);
+ return CMD_SUCCESS;
+}
+
+DEFUN(cfg_e1d_if_pll_gains, cfg_e1d_if_pll_gains_cmd,
+ "pll pid-gains KP KI KD",
+ PLL_STR "Configure the PID gains (in self-tuning mode: the initial gains)\n"
+ "Proportional gain\n" "Integral gain\n" "Derivative gain\n")
+{
+ struct e1_intf *intf = vty->index;
+ float kp, ki, kd;
+
+ if (intf->drv != E1_DRIVER_DAHDI_TRUNKDEV)
+ return CMD_WARNING;
+ if (!parse_gain(vty, argv[0], &kp) || !parse_gain(vty, argv[1], &ki) ||
+ !parse_gain(vty, argv[2], &kd))
+ return CMD_WARNING;
+
+ intf->dahdi_trunkdev.pll.kp = kp;
+ intf->dahdi_trunkdev.pll.ki = ki;
+ intf->dahdi_trunkdev.pll.kd = kd;
+ e1_dahdi_trunkdev_pll_restart(intf);
+ return CMD_SUCCESS;
+}
+
+DEFUN(cfg_e1d_if_pll_target, cfg_e1d_if_pll_target_cmd,
+ "pll target-fifo <1-1792>",
+ PLL_STR "Configure the IP->E1 FIFO setpoint\n"
+ "FIFO level in E1 frames (default 248)\n")
+{
+ struct e1_intf *intf = vty->index;
+
+ if (intf->drv != E1_DRIVER_DAHDI_TRUNKDEV)
+ return CMD_WARNING;
+
+ intf->dahdi_trunkdev.pll.target_fifo = atoi(argv[0]);
+ e1_dahdi_trunkdev_pll_restart(intf);
+ return CMD_SUCCESS;
+}
+
+DEFUN(cfg_e1d_if_pll_hold, cfg_e1d_if_pll_hold_cmd,
+ "pll self-tuning hold-time <0-31536000>",
+ PLL_STR "Self-tuning mode parameters\n"
+ "Minimum time to run with the configured gains while the link is characterized "
+ "(the switch also waits for the first self-tuned estimate)\n"
+ "Seconds (default 86400)\n")
+{
+ struct e1_intf *intf = vty->index;
+
+ if (intf->drv != E1_DRIVER_DAHDI_TRUNKDEV)
+ return CMD_WARNING;
+
+ intf->dahdi_trunkdev.pll.hold_s = atoi(argv[0]);
+ e1_dahdi_trunkdev_pll_restart(intf);
+ return CMD_SUCCESS;
+}
+
+DEFUN(cfg_e1d_if_pll_window, cfg_e1d_if_pll_window_cmd,
+ "pll self-tuning window <600-86400>",
+ PLL_STR "Self-tuning mode parameters\n"
+ "Length of the windows over which the oscillator frequency is estimated. "
+ "The first self-tuned gains need about 10 valid windows; shorter windows "
+ "adapt sooner but give noisier estimates\n"
+ "Seconds (default 3600)\n")
+{
+ struct e1_intf *intf = vty->index;
+
+ if (intf->drv != E1_DRIVER_DAHDI_TRUNKDEV)
+ return CMD_WARNING;
+
+ intf->dahdi_trunkdev.pll.window_s = atoi(argv[0]);
+ e1_dahdi_trunkdev_pll_restart(intf);
+ return CMD_SUCCESS;
+}
+
+DEFUN(cfg_e1d_if_pll_log, cfg_e1d_if_pll_log_cmd,
+ "pll log-file FILENAME",
+ PLL_STR "Log the controller state to a CSV file, one line per second\n"
+ "Path of the CSV file (appended to)\n")
+{
+ struct e1_intf *intf = vty->index;
+
+ if (intf->drv != E1_DRIVER_DAHDI_TRUNKDEV)
+ return CMD_WARNING;
+
+ osmo_talloc_replace_string(intf, &intf->dahdi_trunkdev.pll.log_file, argv[0]);
+ e1_dahdi_trunkdev_pll_restart(intf);
+ return CMD_SUCCESS;
+}
+
+DEFUN(cfg_e1d_if_no_pll_log, cfg_e1d_if_no_pll_log_cmd,
+ "no pll log-file",
+ NO_STR PLL_STR "Stop logging the controller state to a CSV file\n")
+{
+ struct e1_intf *intf = vty->index;
+
+ if (intf->drv != E1_DRIVER_DAHDI_TRUNKDEV)
+ return CMD_WARNING;
+
+ TALLOC_FREE(intf->dahdi_trunkdev.pll.log_file);
+ e1_dahdi_trunkdev_pll_restart(intf);
+ return CMD_SUCCESS;
+}
+
+static void config_write_trunkdev_pll(struct vty *vty, const struct e1_intf *intf)
+{
+ if (intf->dahdi_trunkdev.pll.kp != PLL_PID_DEFAULT_KP ||
+ intf->dahdi_trunkdev.pll.ki != PLL_PID_DEFAULT_KI ||
+ intf->dahdi_trunkdev.pll.kd != PLL_PID_DEFAULT_KD)
+ vty_out(vty, " pll pid-gains %g %g %g%s", (double)intf->dahdi_trunkdev.pll.kp,
+ (double)intf->dahdi_trunkdev.pll.ki, (double)intf->dahdi_trunkdev.pll.kd,
+ VTY_NEWLINE);
+ if (intf->dahdi_trunkdev.pll.target_fifo != (unsigned int)PLL_PID_DEFAULT_TARGET_FIFO)
+ vty_out(vty, " pll target-fifo %u%s", intf->dahdi_trunkdev.pll.target_fifo, VTY_NEWLINE);
+ if (intf->dahdi_trunkdev.pll.hold_s != 86400)
+ vty_out(vty, " pll self-tuning hold-time %u%s", intf->dahdi_trunkdev.pll.hold_s, VTY_NEWLINE);
+ if (intf->dahdi_trunkdev.pll.window_s != 3600)
+ vty_out(vty, " pll self-tuning window %u%s", intf->dahdi_trunkdev.pll.window_s, VTY_NEWLINE);
+ if (intf->dahdi_trunkdev.pll.log_file)
+ vty_out(vty, " pll log-file %s%s", intf->dahdi_trunkdev.pll.log_file, VTY_NEWLINE);
+ /* mode last, so that the controller starts once with all parameters set */
+ if (intf->dahdi_trunkdev.pll.mode != E1_TRUNKDEV_PLL_OFF)
+ vty_out(vty, " pll mode %s%s", get_value_string(pll_mode_names, intf->dahdi_trunkdev.pll.mode),
+ VTY_NEWLINE);
+}
#endif /* HAVE_DAHDI_TRUNKDEV */
DEFUN(cfg_e1d_if_line, cfg_e1d_if_line_cmd, "line <0-255>",
@@ -550,6 +738,9 @@
vty_out(vty, " interface %u dahdi-trunkdev%s", intf->id, VTY_NEWLINE);
if (intf->dahdi_trunkdev.name && strlen(intf->dahdi_trunkdev.name))
vty_out(vty, " trunkdev-name %s%s", intf->dahdi_trunkdev.name, VTY_NEWLINE);
+#ifdef HAVE_DAHDI_TRUNKDEV
+ config_write_trunkdev_pll(vty, intf);
+#endif
break;
default:
break;
@@ -584,6 +775,13 @@
install_element(INTF_NODE, &cfg_e1d_if_no_gpsdo_manual_cmd);
#ifdef HAVE_DAHDI_TRUNKDEV
install_element(INTF_NODE, &cfg_e1d_if_trunkdev_name_cmd);
+ install_element(INTF_NODE, &cfg_e1d_if_pll_mode_cmd);
+ install_element(INTF_NODE, &cfg_e1d_if_pll_gains_cmd);
+ install_element(INTF_NODE, &cfg_e1d_if_pll_target_cmd);
+ install_element(INTF_NODE, &cfg_e1d_if_pll_hold_cmd);
+ install_element(INTF_NODE, &cfg_e1d_if_pll_window_cmd);
+ install_element(INTF_NODE, &cfg_e1d_if_pll_log_cmd);
+ install_element(INTF_NODE, &cfg_e1d_if_no_pll_log_cmd);
#endif /* HAVE_DAHDI_TRUNKDEV */
install_node(&line_node, NULL);
To view, visit change 44232. To unsubscribe, or for help writing mail filters, visit settings.