RTNL objects

General structure

The NDB objects are dictionary-like structures that represent network objects -- interfaces, routes, addresses etc. They support the common dict API, like item getting, setting, iteration through key and values. In addition to that, NDB object add specific calls, see the API section below.

Most of the NDB object types store all attributes in a flat one level dictionary. Some, like multihop routes, implement nested structures. In addition to that, some objects like Interface provide views on the DB that list only related objects -- addresses, routes and neighbours. More on these topic see in the corresponding sections.

NDB objects and RTNL API

The dictionary fields represent RTNL messages fields and NLA names, and the objects are used as argument dictionaries to normal IPRoute methods like link() or route(). Thus everything described for the IPRoute methods is valid here as well.

See also: IPRoute and related modules

# create a vlan interface with IPRoute
eth0 = 2
with IPRoute() as ipr:
    ipr.link("add",
             ifname="vlan1108",
             kind="vlan",
             link=eth0,
             vlan_id=1108)

# same with NDB:
with NDB(log="stderr") as ndb:
    vlan = ndb.interfaces.create(
        ifname="vlan1108",
        kind="vlan",
        link="eth0",
        vlan_id=1108,
    )
    vlan.commit()

Slightly simplifying, if a network object doesn't exist, NDB will run an RTNL method with "add" argument, if exists -- "set", and to remove an object NDB will call the method with "del" argument.

API

class pyroute2.ndb.objects.RTNL_Object
property table
property etable
property key
apply(rollback: bool = False, req_filter: None | Callable[[dict[str, Union[str, int]]], dict[str, Union[str, int]]] = None, mode: str = 'apply') SyncBase
commit() SyncBase
complete_key(key)
create(**spec)
exists(key)
load_sql(table=None, ctxid=None, set_state=True)
load_value(key, value)
set(key, value)
show(fmt)
snapshot(ctxid=None)
rollback(snapshot=None)