openwrt ubus (OpenWrt micro bus 架構)

爲了在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:~# 

發佈了58 篇原創文章 · 獲贊 66 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章