OpenWrt ubus 官方例子(1):数据模型——blob、blobmsg 与 policy
在练习中成长
不用等读完,先测一下一句话:ubus 的“数据模型”不是一张表,而是 对象模型 + 一次性的 blobmsg 载荷。 对象模型挂在总线上;载荷模型只活在这一次调用里。
OpenWrt ubus 是什么
ubus 是 OpenWrt 的进程间总线:ubusd 维护对象注册表,进程通过 Unix socket 注册对象、调用方法、收发事件。它解决的是“本机进程怎么互相找到并调用”,不是网络 RPC,也不是配置系统。
先分清两层,后面的 CLI、C API、RPC 都在这两层上:
- 对象模型:总线上有哪些对象、方法、参数签名;
- 载荷模型:每次调用/回复里的 blobmsg 数据。
先分清两层模型
ubus 里容易混的两件事:
1)对象模型(总线上的)
ubus_object
├─ name "test"
├─ type ubus_object_type
└─ methods[] ubus_method(名字 + handler + policy)
它回答:总线上有什么对象、能调什么方法、参数长什么样。
2)载荷模型(每次调用的)
blob_buf → blob_attr → blobmsg
它回答:这次调用/回复具体带了什么数据。
server.c 里 UBUS_METHOD("hello", test_hello, hello_policy) 同时出现了两层:
"hello"是对象模型里的方法名;hello_policy是载荷模型的解析规则。
blob 和 blobmsg 差在哪
- blob:
id_len+ payload,紧凑的二进制属性;没有名字/类型语义。 - blobmsg:在 blob 上加 name + type,支持 table / array,接近 JSON。
所以 ubus CLI 可以把 JSON 转成 blobmsg:
ubus call test hello '{"id":1,"msg":"hello"}'
C 侧不直接解析 JSON,而是:
struct blob_buf b;
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "id", 1);
blobmsg_add_string(&b, "msg", "hello");
官方例子里的四个 policy
examples/server.c 定义了三组参数:
/* hello: id(int32), msg(string) */
static const struct blobmsg_policy hello_policy[] = {
[HELLO_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
[HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
};
/* watch: id(int32), counter(int32) */
static const struct blobmsg_policy watch_policy[] = { ... };
/* count: to(int32), string(string) */
static const struct blobmsg_policy count_policy[] = { ... };
解析:
struct blob_attr *tb[__HELLO_MAX];
blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy),
tb, blob_data(msg), blob_len(msg));
if (tb[HELLO_MSG]) {
msgstr = blobmsg_data(tb[HELLO_MSG]);
}
要点:
blobmsg_parse按 name 找属性,按 type 校验;- 缺失字段不会自动填默认值,要自己判断;
- 未知字段通常被忽略,policy 不是完整 schema;
- 取整数用
blobmsg_get_u32,取字符串用blobmsg_get_string。
三个方法分别演示什么
hello:解析 + 延迟回复 + fd
- 解析
id/msg; ubus_defer_request把请求挂起;- 1 秒后
ubus_send_reply回message; - 还通过 pipe 把 fd 传给客户端,客户端用
ustream_fd持续读msg1: ...。
这是 ubus 的“异步 RPC + fd 传递”,不是普通 JSON RPC。
watch:订阅 + 通知
- 客户端先注册自己的对象,拿到
id; - 调
test的watch,参数id; - 服务端
ubus_subscribe(ctx, &test_event, id); - 之后客户端
ubus_notify,服务端test_notify收到通知; - 客户端对象消失时,
test_handle_remove收到 remove 回调。
count:请求/回复校验
- 客户端传
to和string; - 服务端用
count_to_number(to)生成字符串并比较; - 回复
{ "rc": 0/非0 }; - 客户端在
ubus_invoke的 callback 里blobmsg_parse解析rc。
实跑:不是只看代码
在 Ubuntu 上编译官方 examples(BUILD_EXAMPLES=ON),用独立 socket:
git clone https://github.com/openwrt/ubus.git
cmake -S ubus -B ubus/build \
-DBUILD_LUA=OFF -DBUILD_EXAMPLES=ON \
-DCMAKE_PREFIX_PATH=/usr/local
cmake --build ubus/build -j"$(nproc)"
ubusd -s /tmp/ubus-ex.sock &
./ubus/build/examples/server -s /tmp/ubus-ex.sock &
timeout 6 ./ubus/build/examples/client -s /tmp/ubus-ex.sock
实测输出(节选):
Avg time per iteration: 63 usec
Subscribers active: 1
Got fd from the server, watching...
completed request, ret: 0
Got line: msg1: test received a message: blah
Sending count up to '100100'; string has length '592926'
Server validated our count up to '100100'
ubus list 能看到 test;client 因定时器循环不会自己退出,timeout 124 是预期的。
注意:非 root 客户端在默认
ubusdACL 下会ubus_add_object失败(Invalid argument)。 这个 demo 用 root 跑;生产要配 ACL,别直接照搬 root。
边界:它不是 TR-181
- ubus 的 blobmsg 是瞬时 RPC 载荷,不持久化、不提供对象树/事务/事件模型;
- policy 只做“名字 + 类型”解析,不做默认值、范围、跨字段校验;
- 配置/状态模型要看 UCI 或 AMX/TR-181,见
- 嵌套结构用
blobmsg_open_table/blobmsg_open_array,policy 里对应BLOBMSG_TYPE_TABLE/ARRAY;官方 examples 只用了顶层属性; - 消息有大小上限,复杂数据要自己控制。
验证状态(本站宗旨)
- ✅ 源码:
openwrt/ubus414c60a,examples/server.c、client.c、count.c、CMakeLists.txt; - ✅ PC 实跑:Ubuntu 上编译 examples,root + 独立 socket 跑通
test对象、notify、hello 延迟回复、count 校验; - ⚠️ 环境差异:未在 OpenWrt 设备上跑;非 root 需要 ACL;
- ❓ 未覆盖:嵌套 table/array、消息大小上限、并发/错误路径。
一句话
ubus 的对象模型负责“能调什么”,blobmsg 模型负责“这次带了什么”。 policy 是两者之间的桥;它很轻,但不是 schema。
源码:openwrt/ubus examples(
414c60a) 相关:ubus 命令实战 · C API 最小 server/client · 从 UCI 到数据模型 · UCI 指南 · prplOS LED 插件 系列:OpenWrt ubus 官方例子解读。
评论 0
还没有评论,来抢沙发~