← 全部文章

从 TR-181 路由到 UCI:最小映射源码

在练习中成长

不用等读完,先测一下

这篇给“TR-181 路由 → UCI”一个能跑通的完整实现。 它不是官方 mod-routing-uci 的逐行复刻,而是把映射层的最小闭环写出来。

完整代码:zishuowang696/amx-uci-route-demo

1. 先定模型

为了不被完整 TR-181 的字段淹没,用简化对象说明映射:

%define {
    object UciRoute[] {
        %key string Alias;
        string Target;
        string Netmask;
        string Gateway;
        string Interface;
    }
}

真正接 TR-181 时,把 UciRoute 换成 Device.Routing.Router.{i}.IPv4Forwarding.{i},字段对应即可。

2. 目标 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. 可执行映射:shell 版

先用 shell 跑通,确认 UCI 语义:

#!/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

删除:

uci delete "network.${NAME}"
uci commit network

验证:

uci show network | grep "${NAME}"
ip route

注意:uci commit 只写文件,路由真正生效还要 netifd reload 或等它下一次同步。

4. libuci 实际映射

生产模块里不要把 system("uci ...") 塞进 uloop 回调;用 libuci:

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;
}

UCI_CONFIG_DIR 只是 demo 的测试钩子,生产不要用它绕过 /etc/config。

5. 接 AMX:事件回调

ODL 里把事件订阅限定到对象:

entry-point uci_route_demo.uci_route_main;

%populate {
    on event "dm:object-changed" of "UciRoute." call route_changed;
}

C 侧实际回调:

void route_changed(const char *event_name,
                   const amxc_var_t *event_data,
                   void *priv)
{
    const char *path = GET_CHAR(event_data, "path");
    amxd_object_t *obj;
    const char *alias, *target, *netmask, *gateway, *iface;

    if (!g_dm || !path || strncmp(path, "UciRoute.", 9) != 0)
        return;

    obj = amxd_dm_findf(g_dm, "%s", path);
    if (!obj) return;

    alias = param(obj, "Alias");
    target = param(obj, "Target");
    netmask = param(obj, "Netmask");
    gateway = param(obj, "Gateway");
    iface = param(obj, "Interface");
    if (!alias || !target || !netmask || !gateway || !iface) return;

    if (apply_route(alias, target, netmask, gateway, iface) != 0)
        fprintf(stderr, "failed to apply %s\n", alias);
}

g_dm 在 entry-point 的 AMXO_START 里保存;param() 用 amxd_object_get_param_value 取值。

这个实现已在本机用 ubusd + amxrt + libuci 跑通:_add + _set 后,UCI 文件出现 config route。

不要在 ODL 里“调用 uci 命令”:

  • ODL 不是执行环境;
  • 没有事务/重试/回滚;
  • 外部命令会阻塞 uloop。

ODL 定义对象,C 模块做翻译,UCI 负责持久化,netifd 负责生效。

6. 生产还要补什么

  • 删除、禁用、字段变更的同步;
  • UCI 失败后的回滚/重试;
  • 并发写和幂等;
  • 只允许受控 section,别让任意输入拼命令;
  • 用 libuci 而不是 shell 拼接;
  • 提交后通知 netifd,而不是假设立即生效。

验证状态

  • ✅ UCI 命令语义:set/commit/show 与 /etc/config;
  • ✅ 映射结构:TR-181 → 模块 → UCI → netifd;
  • ✅ 本机编译/运行:AMX 头/库 + libuci,amxrt 注册 UciRoute,_add + _set 后 UCI 文件出现 config route;
  • ⚠️ 未在物理 prplOS 设备验证;
  • ❓ 官方 mod-routing-uci 的字段映射/事务细节未逐行核对。

系列:prplOS 与 AMX 相关:prplOS 的 UCI 后端 · AMX 怎么调用 UCI · AMX 数据模型存在哪 · 从 UCI 到数据模型

本页练习

觉得有用?

我持续记录 AI Agent × 嵌入式 / 边缘 AI 的实践。订阅更新,或来邮件交流一个具体问题。

去留言板 →订阅 RSS赞助

评论 0

还没有评论,来抢沙发~

无需登录即可评论;昵称可选。