openwrt ubus (OpenWrt micro bus 架构)

ubus 是 OpenWrt 中的一种守护进程间通信机制,通过 ubusd 守护进程实现守护进程注册和消息发送功能。ubus 使用 Unix socket 和 TLV 消息格式进行通信,并提供 libubus 库简化开发。ubus 支持通过命令行工具进行交互,包括列出注册的命名空间、调用方法和监听事件等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了在OpenWrt中提供守护进程和应用程序间的通讯,开发了ubus项目工程。它包含了守护进程、库以及一些额外的帮助程序。

核心部分是ubusd守护进程,它提供了其他守护进程将自己注册以及发送消息的接口。因为这个,接口通过使用Unix socket来实现,并使用TLV(type-length-value)消息。

为了简化软件的开发,可以使用已有的libubus库来使用ubus(连接ubus)。

每个守护进程在自己的名称空间中注册自有的路径。每个路径可以提供多个带有不定数量参数的方法,方法可以通过消息回复调用。

代码在LGPL 2.1授权方法下发布,你可以通过git在git://nbd.name/luci2/ubus.git或通过http在http://nbd.name/gitweb.cgi?p=luci2/ubus.git;a=summary获取。 ubus从r28499起被包含在OpenWrt中。

ubus命令行工具

ubus可以和ubusd服务器交互(和当前所有已经注册的服务). 它对研究和调试注册的命名空间以及编写脚本非常有用。对于调用带参数和返回信息的方法,它使用友好的JSON格式。下面是它的命令说明。

list
缺省列出所有通过RPC服务器注册的命名空间:

root@uplink:~# ubus list
network
network.device
network.interface.lan
network.interface.loopback
network.interface.wan
root@uplink:~#

如果调用时包含参数-v,将会显示指定命名空间更多方法参数等信息:

root@uplink:~# ubus -v list network.interface.lan
'network.interface.lan' @099f0c8b
    "up": {  }
    "down": {  }
    "status": {  }
    "prepare": {  }
    "add_device": { "name": "String" }
    "remove_device": { "name": "String" }
    "notify_proto": {  }
    "remove": {  }
    "set_data": {  }
root@uplink:~#

call
调用指定命名空间中指定的方法,并且通过消息传递给它:

root@uplink:~# ubus call network.interface.wan status
{
    "up": true,
    "pending": false,
    "available": true,
    "autostart": true,
    "uptime": 86017,
    "l3_device": "eth1",
    "device": "eth1",
    "address": [
        {
            "address": "178.25.65.236",
            "mask": 21
        }
    ],
    "route": [
        {
            "target": "0.0.0.0",
            "mask": 0,
            "nexthop": "178.25.71.254"
        }
    ],
    "data": {

    }
}
root@uplink:~#

消息参数必须是有效的JSON字符串,并且携带函数所要求的键及值:

root@uplink:~# ubus call network.device status '{ "name": "eth0" }'
{
    "type": "Network device",
    "up": true,
    "link": true,
    "mtu": 1500,
    "macaddr": "c6:3d:c7:90:aa:da",
    "txqueuelen": 1000,
    "statistics": {
        "collisions": 0,
        "rx_frame_errors": 0,
        "tx_compressed": 0,
        "multicast": 0,
        "rx_length_errors": 0,
        "tx_dropped": 0,
        "rx_bytes": 0,
        "rx_missed_errors": 0,
        "tx_errors": 0,
        "rx_compressed": 0,
        "rx_over_errors": 0,
        "tx_fifo_errors": 0,
        "rx_crc_errors": 0,
        "rx_packets": 0,
        "tx_heartbeat_errors": 0,
        "rx_dropped": 0,
        "tx_aborted_errors": 0,
        "tx_packets": 184546,
        "rx_errors": 0,
        "tx_bytes": 17409452,
        "tx_window_errors": 0,
        "rx_fifo_errors": 0,
        "tx_carrier_errors": 0
    }
}
root@uplink:~#

listen
设置一个监听socket并观察进入的事件:

root@uplink:~# ubus listen &
root@uplink:~# ubus call network.interface.wan down
{ "network.interface": { "action": "ifdown", "interface": "wan" } }
root@uplink:~# ubus call network.interface.wan up
{ "network.interface": { "action": "ifup", "interface": "wan" } }
{ "network.interface": { "action": "ifdown", "interface": "he" } }
{ "network.interface": { "action": "ifdown", "interface": "v6" } }
{ "network.interface": { "action": "ifup", "interface": "he" } }
{ "network.interface": { "action": "ifup", "interface": "v6" } }
root@uplink:~# 

send
发送一个事件提醒:

root@uplink:~# ubus listen &
root@uplink:~# ubus send foo '{ "bar": "baz" }'
{ "foo": { "bar": "baz" } }
root@uplink:~# 

### OpenWrtUbus 的使用方法和功能介绍 Ubus 是为 OpenWrt 平台开发设计的一个进程间通信框架,它简化了不同组件之间的交互过程并增强了系统的模块化程度[^1]。 #### 功能特性 - **简易的IPC机制**:通过定义良好的API接口使得开发者能够轻松建立服务端与客户端间的高效通讯。 - `libubus.so` 提供了一系列用于操作Ubus的核心函数集,比如创建套接字、发起请求等基本动作。 - `libubox.so` 则包含了辅助性的工具类函数,像事件循环管理等功能。 - 对JSON格式的支持由专门的 `libblobmsg.so` 库来完成,该库内部实现了对二进制形式的消息编码解码逻辑,从而允许应用程序间接利用 JSON 结构传递复杂的数据结构而无需直接调用底层解析器[^3]。 #### 实现方式概述 当某个程序想要成为Ubus的服务提供商时,它可以注册特定的对象及其关联的方法;而对于消费者来说,则可以通过查询已存在的对象列表找到所需的服务,并按照既定协议发送命令获取响应。例如,有一个名为 "client1" 的应用实例分别暴露出了 “interface” 和 “dotalk” 这样的实体给外界访问,前者拥有设置或读取局域网IP地址的能力(即 getlanip/setlanip 方法),后者则能发出问候语句(sayhi/saybye 方法)。每当有来自远程节点的有效呼叫到达后,相应的回调处理器就会被触发执行具体的业务流程[^5]。 ```bash # 注册一个新的ubus对象和服务方法的例子 ubus add_object interface { methods = [ { name="getlanip", type=string }, { name="setlanip", type=string } ] } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值