From TR-181 routes to UCI: a minimal mapping implementation
Grow through practice
No need to finish reading — test nowThis article gives the “TR-181 route → UCI” mapping a complete, runnable implementation. It is not a line-by-line copy of the official
mod-routing-uci; it is the smallest closed loop for the mapping layer.Full code: zishuowang696/amx-uci-route-demo
1. Define the model
To avoid drowning in the full TR-181 schema, use a simplified object:
%define {
object UciRoute[] {
%key string Alias;
string Target;
string Netmask;
string Gateway;
string Interface;
}
}
In a real integration, replace UciRoute with Device.Routing.Router.{i}.IPv4Forwarding.{i} and map the fields accordingly.
2. Target UCI
config route 'route1'
option interface 'lan'
option target '10.0.0.0'
option netmask '255.0.0.0'
option gateway '192.168.1.1'
3. Runnable mapping: shell
Get the UCI semantics working first:
#!/bin/sh
set -eu
NAME="${1:?route name}"
TARGET="${2:?target}"
NETMASK="${3:?netmask}"
GATEWAY="${4:?gateway}"
IFACE="${5:?interface}"
if ! uci -q get "network.${NAME}" >/dev/null 2>&1; then
uci set "network.${NAME}=route"
fi
uci set "network.${NAME}.interface=${IFACE}"
uci set "network.${NAME}.target=${TARGET}"
uci set "network.${NAME}.netmask=${NETMASK}"
uci set "network.${NAME}.gateway=${GATEWAY}"
uci commit network
Delete:
uci delete "network.${NAME}"
uci commit network
Verify:
uci show network | grep "${NAME}"
ip route
uci commit only writes the file; the route becomes active when netifd reloads or syncs.
4. libuci mapping
Do not put system("uci ...") inside a uloop callback; use libuci:
#include <uci.h>
#include <stdio.h>
static int set_option(struct uci_context *ctx,
struct uci_package *pkg,
struct uci_section *section,
const char *name,
const char *value)
{
struct uci_ptr ptr = {
.p = pkg,
.s = section,
.section = section->e.name,
.option = name,
.value = value,
};
return uci_set(ctx, &ptr);
}
static int apply_route(const char *name,
const char *target,
const char *netmask,
const char *gateway,
const char *iface)
{
struct uci_context *ctx = uci_alloc_context();
struct uci_package *pkg = NULL;
struct uci_section *sec;
int rc = -1;
if (!ctx) return -1;
if (getenv("UCI_CONFIG_DIR"))
uci_set_confdir(ctx, getenv("UCI_CONFIG_DIR"));
if (uci_load(ctx, "network", &pkg) != UCI_OK) goto out;
sec = uci_lookup_section(ctx, pkg, name);
if (!sec && uci_add_section(ctx, pkg, "route", &sec) != UCI_OK)
goto out;
if (set_option(ctx, pkg, sec, "interface", iface) != UCI_OK) goto out;
if (set_option(ctx, pkg, sec, "target", target) != UCI_OK) goto out;
if (set_option(ctx, pkg, sec, "netmask", netmask) != UCI_OK) goto out;
if (set_option(ctx, pkg, sec, "gateway", gateway) != UCI_OK) goto out;
if (uci_commit(ctx, &pkg, false) != UCI_OK) goto out;
rc = 0;
out:
uci_free_context(ctx);
return rc;
}
This implementation was compiled and run locally with ubusd + amxrt + libuci; _add + _set produced a UCI config route section.
5. Connect it to AMX
entry-point uci_route_demo.uci_route_main;
%populate {
on event "dm:object-changed" of "UciRoute." call route_changed;
}
The callback reads path from the event, finds the object in the AMX data model, pulls Alias/Target/Netmask/Gateway/Interface, and calls apply_route(). The of "UciRoute." scope is required for instance changes.
Do not “call uci commands” from ODL:
- ODL is not an execution environment;
- there is no transaction/retry/rollback for UCI;
- external commands block the uloop.
ODL defines the object, a C module translates, UCI persists, netifd applies.
6. Production gaps
- handles delete/disable/field changes;
- retries/rollback on UCI failure;
- concurrent writes and idempotency;
- controlled section names, never shell-concatenate arbitrary input;
- use libuci, not shell strings;
- notify netifd instead of assuming immediate effect.
Verification
- ✅ UCI semantics:
set/commit/showand/etc/config; - ✅ Mapping chain: TR-181 → module → UCI → netifd;
- ✅ Compiled and run locally: AMX headers/libs + libuci;
amxrtregisteredUciRoute, and_add+_setproduced a UCIconfig route; - ⚠️ Not verified on a physical prplOS device;
- ❓ Exact field mapping/transaction details of the official
mod-routing-uciwere not checked line by line.
Series: Inside prplOS: AMX Runtime (Guide) Related: prplOS UCI backend · How AMX calls UCI · Where the AMX data model lives · From UCI to a data model
Comments 0
No comments yet — be the first!