openwrt ubus (OpenWrt micro bus 架构)

来源:互联网 发布:大数据架构基础 编辑:程序博客网 时间:2024/05/29 17:59

为了在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 listnetworknetwork.devicenetwork.interface.lannetwork.interface.loopbacknetwork.interface.wanroot@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:~# 

1 0
原创粉丝点击