ubus (OpenWrt micro bus architecture)

Under Construction!
This page is currently under construction. You can edit the article to help completing it.

The code can be found via git at git://nbd.name/luci2/libubox.git or via http at http://nbd.name/gitweb.cgi .

Command line utility

The ubus command line client allows to interact with the ubusd rpc server, below is an explanation of its commands.

list

By default, list all namespaces registered with the RPC server:

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

If invoked with -v, additionally the procedures and their argument signatures are dumped in addition to the namespace path:

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

Calls a given procedure within a given namespace and passes given message to it:

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:~#

The message argument must be a valid JSON string, with keys and values set according to the function signature:

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

Setup a listening socket and observe incoming events:

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

Send an event notification:

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

Lua binding

The package libubus-lua implements a simple Lua binding to the libubus library to allow for easy client connections.

Load module

require "ubus"

Establish connection

local conn = ubus.connect()
if not conn then
    error("Failed to connect to ubusd")
end

Iterate all namespaces and procedures

local namespaces = conn:objects()
for i, n in ipairs(namespaces) do
    print("namespace=" .. n)
    local signatures = conn:signatures(n)
    for p, s in pairs(signatures) do
        print("\tprocedure=" .. p)
        for k, v in pairs(s) do
            print("\t\tattribute=" .. k .. " type=" .. v)
        end
    end
end

Call a procedure

local status = conn:call("network.device", "status", { name = "eth0" })
for k, v in pairs(status) do
    print("key=" .. k .. " value=" .. tostring(v))
end

Close connection

conn:close()

Namespaces & Procedures

Implemented

netifd

Path Procedure Signature Description
network restart { } Restart the network, reconfigures all interfaces
network reload { } Reload the network, reconfigure as needed
network.device status { "name": "ifname" } Dump status of given network device ifname
network.device set_state { "name": "ifname", "defer": deferred } Defer or ready the given network device ifname, depending on the boolean value deferred
network.interface.name up { } Bring interface name up
network.interface.name down { } Bring interface name down
network.interface.name status { } Dump status of interface name
network.interface.name prepare { } Prepare setup of interface name
network.interface.name add_device { "name": "ifname" } Add network device ifname to interface name (e.g. for bridges: brctl addif br-name ifname)
network.interface.name remove_device { "name": "ifname" } Remove network device ifname from interface name (e.g. for bridges: brctl delif br-name ifname)
network.interface.name remove { } Remove interface name (?)

uhttpd-mod-ubus

Path Procedure Signature Description
session list { "session": "sid" } Dump session specified by sid, if no ID is given, dump all sessions
session create { "timeout": timeout } Create a new session and return its ID, set the session timeout to timeout
session grant { "session": "sid", "objects": [ [ "path", "func" ], … ] } Within the session identified by sid grant access to all specified procedures func in the namespace path listed in the objects array
session revoke { "session": "sid", "objects": [ [ "path", "func" ], … ] } Within the session identified by sid revoke access to all specified procedures func in the namespace path listed in the objects array. If objects is unset, revoke all access
session set { "session": "sid", "values": { "key": value, … } } Within the session identified by sid store the given arbitrary values under their corresponding keys specified in the values object
session get { "session": "sid", "keys": [ "key", … ] } Within the session identified by sid retrieve all values associated with the given keys listed in the keys array. If the key array is unset, dump all key/value pairs
session unset { "session": "sid", "keys": [ "key", … ] } Within the session identified by sid unset all keys listed in the keys array. If the key list is unset, clear all keys
session extend { "session": "sid" } Within the session identified by sid reset inactivity timeout counter
session destroy { "session": "sid" } Terminate the session identified by the given ID sid

Planned

LuCI2

Path Procedure Signature Description
uci get { "package": "package", "section": "sname", "type": "type", "option": "oname" }
Return the requested uci value(s), all arguments are optional.
  1. When called without argument or with empty object: return an array of package names in the packages field
  2. When called with package set: return an object containing all sections containing all options in a field named after the package
  3. When called with package and type set: return an object containing all sections of type type containing all options in a field named after the package
  4. When called with package and sname set: return an object containing all options of the section in a field named after the section
  5. When called with package and type and oname set: return an object containing the value of each option named oname within a section of type type in a field named after the matched section
  6. When called with package and sname and oname set: return the result string in a field named oname in case of options or an array of result strings in a field named oname in case of list options

Return messages:

  1. { "packages": [ "package1", … ] }
  2. { "package": { "sname1": { ".type": "type1", "option1": "value1", "option2": [ "value2.1", … ], … }, … } }
  3. { "package": { "sname1": { ".type": "type", "option1": "value1", "option2": [ "value2.1", … ], … }, … } }
  4. { "sname": { ".type": "type", "option1": "value1", "option2": [ "value2.1", … ], … } }
  5. { "sectionname1": "value1", "sectionname2": [ "value2.1", … ], … }
    1. { "oname": "value1" }
    2. { "oname": [ "value1.1", … ] }
uci set { "package": "package", "section": "sname", "option": "oname", "value": "value" }
Set the given value(s), the option argument is optional.
  1. When called with package and sname and value set: add a new section sname in package and set it to the type given in value
  2. When called with package and sname, oname and value set:
    1. If value is of type array: set strings in the value array as list option oname
    2. If value is of type string: set value as normal option oname

The call does not produce any data, instead it returns with the following status codes:

  1. If there already is a section called sname: UBUS_STATUS_INVALID_ARGUMENT else: UBUS_STATUS_OK
  2. If there is no section sname or if value is neither a string nor an array: UBUS_STATUS_INVALID_ARGUMENT else: UBUS_STATUS_OK
uci add { "package": "package", "type": "type" }
Add new anonymous section of given type.
  1. When called with package and type set: Add a new anonymous section of type type.

Return message:

  1. { "section": "sectionname" }
uci delete { "package": "package", "section": "sname", "type": "type", "option": "oname" }
Delete the given value(s) or section(s), the option and type arguments are optional.
  1. When called with package and type set: delete all sections of type type in package
  2. When called with package and sname set: delete the section named sname in package
  3. When called with package, type and oname set: delete the option named oname within each section of type type in package
  4. When called with package, sname and oname set: delete the option named oname in section sname of package

The call does not result in any data, instead it returns the following status codes:

  1. If no section of type type was found: UBUS_STATUS_NOT_FOUND else: UBUS_STATUS_OK
  2. If no section named sname was found: UBUS_STATUS_NOT_FOUND else: UBUS_STATUS_OK
  3. If no options named oname within sections of type type where found: UBUS_STATUS_NOT_FOUND else: UBUS_STATUS_OK
  4. If the option named oname within named section sname was not found: UBUS_STATUS_NOT_FOUND else: UBUS_STATUS_OK

Back to top

doc/techref/ubus.txt · Last modified: 2013/05/11 20:15 by jaseg