ubus (OpenWrt micro bus architecture)

To provide communication between various daemons and applications in OpenWrt an ubus project has been developed. It consists of few parts including daemon, library and some extra helpers.

The heart of this project is ubusd daemon. It provides interface for other daemons to register themselves as well as sending messages. For those curious, this interface is implemented using Unix socket and it uses TLV (type-length-value) messages.

To simplify development of software using ubus (connecting to it) a library called libubus has been created.

Every daemon registers set of own paths under specific namespace. Every path can provide multiple procedures with various amount of arguments. Procedures can reply with a message.

The code is published under LGPL 2.1 and can be found via git at git://nbd.name/luci2/ubus.git or via http at http://nbd.name/gitweb.cgi?p=luci2/ubus.git;a=summary. It's included in OpenWrt since r28499.

Command-line ubus tool

The ubus command line tool allows to interact with the ubusd server (with all currently registered services). It's useful for investigating/debugging registered namespaces as well as writing shell scripts. For calling procedures with parameters and returning responses it uses user-friendly JSON format. 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:~# 

Access to ubus over HTTP

There is an uhttpd plugin called uhttpd-mod-ubus that allows ubus calls using HTTP protocol. Requests have to be send to the /ubus URL (unless changed) using POST method. There are two methods (possible calls) available:

  1. method=list&params=...
  2. method=call&sid=...&object=...&function=...&data=...

Lua module for ubus

This is even possible to use ubus in lua scripts. Of course it's not possible to use native libraries directly in lua, so an extra module has been created. It's simply called ubus and is a simple interface between lua scripts and the ubus (it uses libubus internally).

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

As explained earlier, there can be many different daemons (services) registered in ubus. Below you will find a list of the most common projects with namespaces, paths and procedures they provide.

netifd

Design of 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 (?)

rpcd

Project rpcd is set of small plugins providing sets of ubus procedures in separated namespaces. These plugins are not strictly related to any particular software (like netifd or dhcp) so it wasn't worth it to implement them as separated projects.

Path Procedure Signature Description
file read ? ?
file write ? ?
file list ? ?
file stat ? ?
file exec ? ?


Path Procedure Signature Description
iwinfo info ? ?
iwinfo scan ? ?
iwinfo assoclist ? ?
iwinfo freqlist ? ?
iwinfo txpowerlist ? ?
iwinfo countrylist ? ?
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 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 destroy { "session": "sid" } Terminate the session identified by the given ID sid
session login ? ?
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

Example code snippets

Check if Link is up using devstatus and Json

#!/bin/sh

. /usr/share/libubox/jshn.sh

WANDEV="$(uci get network.wan.ifname)"

json_load "$(devstatus $WANDEV)"

json_get_var var1 speed
json_get_var var2 link

echo "Speed: $var1"
echo "Link: $var2"

Back to top

doc/techref/ubus.txt · Last modified: 2014/04/09 22:15 by zajec