1. Introduction

This library provides APIs to the kernel interfaces of the routing family.

Note Work in progress.

2. Addresses

The link configuration interface is part of the NETLINK_ROUTE protocol family and implements the following netlink message types:

  • View and modify the configuration of physical and virtual network devices.

  • Create and delete virtual network devices (e.g. dummy devices, VLAN devices, tun devices, bridging devices, …)

  • View and modify per link network configuration settings (e.g. net.ipv6.conf.eth0.accept_ra, net.ipv4.conf.eth1.forwarding, …)

Naming Convention (network device, link, interface)

In networking several terms are commonly used to refer to network devices. While they have distinct meanings they have been used interchangeably in the past. Within the Linux kernel, the term network device or netdev is commonly used In user space the term network interface is very common. The routing netlink protocol uses the term link and so does the iproute2 utility and most routing daemons.

This section describes the protocol semantics of the netlink based link configuration interface. The following messages are defined:

Message Type User → Kernel Kernel → User


Create or update virtual network device

Reply to RTM_GETLINK request or notification of link added or updated


Delete virtual network device

Notification of link deleted or disappeared


Retrieve link configuration and statistics


Modify link configuration

See Netlink Library - Message Types for more information on common semantics of these message types.

All netlink link messages share a common header (struct ifinfomsg) which is appended after the netlink header (struct nlmsghdr).

Link Message Header

The meaning of each field may differ depending on the message type. A struct ifinfomsg is defined in <linux/rtnetlink.h> to represent the header.

Address Family (8bit)

The address family is usually set to AF_UNSPEC but may be specified in RTM_GETLINK requests to limit the returned links to a specific address family.

Link Layer Type (16bit)

Currently only used in kernel→user messages to report the link layer type of a link. The value corresponds to the ARPHRD_* defines found in <linux/if_arp.h>. Translation from/to strings can be done using the functions nl_llproto2str()/nl_str2llproto().

Link Index (32bit)

Carries the interface index and is used to identify existing links.

Flags (32bit)

In kernel→user messages the value of this field represents the current state of the link flags. In user→kernel messages this field is used to change flags or set the initial flag state of new links. Note that in order to change a flag, the flag must also be set in the Flags Change Mask field.

Flags Change Mask (32bit)

The primary use of this field is to specify a mask of flags that should be changed based on the value of the Flags field. A special meaning is given to this field when present in link notifications, see TODO.

Attributes (variable)

All link message types may carry netlink attributes. They are defined in the header file <linux/if_link.h> and share the prefix IFLA_.

RTM_GETLINK (user→kernel)

Lookup link by 1. interface index or 2. link name (IFLA_IFNAME) and return a single RTM_NEWLINK message containing the link configuration and statistics or a netlink error message if no such link was found.


  • Address family

    • If the address family is set to PF_BRIDGE, only bridging devices will be returned.

    • If the address family is set to PF_INET6, only ipv6 enabled devices will be returned.


  • NLM_F_DUMP If set, all links will be returned in form of a multipart message.


  • EINVAL if neither interface nor link name are set

  • ENODEV if no link was found

  • ENOBUFS if allocation failed

RTM_NEWLINK (user→kernel)

Creates a new or updates an existing link. Only virtual links may be created but all links may be updated.


  • NLM_F_CREATE Create link if it does not exist

  • NLM_F_EXCL Return EEXIST if link already exists


  • EINVAL malformed message or invalid configuration parameters

  • EAFNOSUPPORT if a address family specific configuration (IFLA_AF_SPEC) is not supported.

  • EOPNOTSUPP if the link does not support modification of parameters

  • EEXIST if NLM_F_EXCL was set and the link exists alraedy

  • ENODEV if the link does not exist and NLM_F_CREATE is not set

RTM_NEWLINK (kernel→user)

This message type is used in reply to a RTM_GETLINK request and carries the configuration and statistics of a link. If multiple links need to be sent, the messages will be sent in form of a multipart message.

The message type is also used for notifications sent by the kernel to the multicast group RTNLGRP_LINK to inform about various link events. It is therefore recommended to always use a separate link socket for link notifications in order to separate between the two message types.

TODO: document how to detect different notifications

RTM_DELLINK (user→kernel)

Lookup link by 1. interface index or 2. link name (IFLA_IFNAME) and delete the virtual link.


  • EINVAL if neither interface nor link name are set

  • ENODEV if no link was found

  • ENOTSUPP if the operation is not supported (not a virtual link)

RTM_DELLINK (kernel→user)

Notification sent by the kernel to the multicast group RTNLGRP_LINK when

  1. a network device was unregistered (change == ~0)

  2. a bridging device was deleted (address family will be PF_BRIDGE)

3.2. Get / List

To retrieve the list of links in the kernel, allocate a new link cache using rtnl_link_alloc_cache() to hold the links. It will automatically construct and send a RTM_GETLINK message requesting a dump of all links from the kernel and feed the returned RTM_NEWLINK to the internal link message parser which adds the returned links to the cache.

#include <netlink/route/link.h>

int rtnl_link_alloc_cache(struct nl_sock *sk, int family, struct nl_cache **result)

The cache will contain link objects (struct rtnl_link, see Link Object) and can be accessed using the standard cache functions. By setting the family parameter to an address familly other than AF_UNSPEC, the resulting cache will only contain links supporting the specified address family.

The following direct search functions are provided to search by interface index and by link name:

#include <netlink/route/link.h>

struct rtnl_link *rtnl_link_get(struct nl_cache *cache, int ifindex);

struct rtnl_link *rtnl_link_get_by_name(struct nl_cache *cache, const char *name);

Example: Link Cache
struct nl_cache *cache;

struct rtnl_link *link;

if (rtnl_link_alloc_cache(sock, AF_UNSPEC, &cache)) < 0)

        /* error */

if (!(link = rtnl_link_get_by_name(cache, "eth1")))

        /* link does not exist */

/* do something with link */



If only a single link is of interest, the link can be looked up directly without the use of a link cache using the function rtnl_link_get_kernel().

#include <netlink/route/link.h>

int rtnl_link_get_kernel(struct nl_sock *sk, int ifindex, const char *name, struct rtnl_link **result);

It will construct and send a RTM_GETLINK request using the parameters provided and wait for a RTM_NEWLINK or netlink error message sent in return. If the link exists, the link is returned as link object (see Link Object).

Example: Direct link lookup
struct rtnl_link *link;

if (rtnl_link_get_kernel(sock, 0, "eth1", &link) < 0)

        /* error */

/* do something with link */


Note While using this function can save a substantial amount of bandwidth on the netlink socket, the result will not be cached, subsequent calls to rtnl_link_get_kernel() will always trigger sending a RTM_GETLINK request.

Applications which require to translate interface index to a link name or vice verase may use the following functions to do so. Both functions require a filled link cache to work with.

char *rtnl_link_i2name (struct nl_cache *cache, int ifindex, char *dst, size_t len);

int rtnl_link_name2i (struct nl_cache *cache, const char *name);

3.3. Add / Modify

Several types of virtual link can be added on the fly using the function rtnl_link_add().

#include <netlink/route/link.h>

int rtnl_link_add(struct nl_sock *sk, struct rtnl_link *link, int flags);

3.4. Delete

The deletion of virtual links such as VLAN devices or dummy devices is done using the function rtnl_link_delete(). The link passed on to the function can be a link from a link cache or it can be construct with the minimal attributes needed to identify the link.

#include <netlink/route/link.h>

int rtnl_link_delete(struct nl_sock *sk, const struct rtnl_link *link);

The function will construct and send a RTM_DELLINK request message and returns any errors returned by the kernel.

Example: Delete link by name
struct rtnl_link *link;

if (!(link = rtnl_link_alloc()))

        /* error */

rtnl_link_set_name(link, "my_vlan");

if (rtnl_link_delete(sock, link) < 0)

        /* error */


A link is represented by the structure struct rtnl_link. Instances may be created with the function rtnl_link_alloc() or via a link cache (see Get list of links) and are freed again using the function rtnl_link_put().

#include <netlink/route/link.h>

struct rtnl_link *rtnl_link_alloc(void);

void rtnl_link_put(struct rtnl_link *link);

The name serves as unique, human readable description of the link. By default, links are named based on their type and then enumerated, e.g. eth0, eth1, ethn but they may be renamed at any time.

Kernels >= 2.6.11 support identification by link name.

#include <netlink/route/link.h>

void rtnl_link_set_name(struct rtnl_link *link, const char *name);

char *rtnl_link_get_name(struct rtnl_link *link);

Accepted link name format: [^ /]* (maximum length: 15 characters)

The interface index is an integer uniquely identifying a link. If present in any link message, it will be used to identify an existing link.

#include <netlink/route/link.h>

void rtnl_link_set_ifindex(struct rtnl_link *link, int ifindex);

int rtnl_link_get_ifindex(struct rtnl_link *link);

Each link can be assigned a numeric group identifier to group a bunch of links together and apply a set of changes to a group instead of just a single link.

#include <netlink/route/link.h>

void rtnl_link_set_group(struct rtnl_link *link, uint32_t group);

uint32_t rtnl_link_get_group(struct rtnl_link *link);

The link layer address (e.g. MAC address).

#include <netlink/route/link.h>

void rtnl_link_set_addr(struct rtnl_link *link, struct nl_addr *addr);

struct nl_addr *rtnl_link_get_addr(struct rtnl_link *link);

The link layer broadcast address

#include <netlink/route/link.h>

void rtnl_link_set_broadcast(struct rtnl_link *link, struct nl_addr *addr);

struct nl_addr *rtnl_link_get_broadcast(struct rtnl_link *link);

The maximum transmission unit specifies the maximum packet size a network device can transmit or receive. This value may be lower than the capability of the physical network device.

#include <netlink/route/link.h>

void rtnl_link_set_mtu(struct rtnl_link *link, unsigned int mtu);

unsigned int rtnl_link_get_mtu(struct rtnl_link *link);

The flags of a link enable or disable various link features or inform about the state of the link.

#include <netlink/route/link.h>

void rtnl_link_set_flags(struct rtnl_link *link, unsigned int flags);

void rtnl_link_unset_flags(struct rtnl_link *link, unsigned int flags);

unsigned int rtnl_link_get_flags(struct rtnl_link *link);


Link is up (administratively)


Link is up and carrier is OK (RFC2863 OPER_UP)


Link layer is operational


Driver signals dormant


Link supports broadcasting


Link supports multicasting


Link supports multicast routing


Tell driver to do debugging (currently unused)


Link loopback network


Point-to-point link


ARP is not supported


Status of promiscious mode


Master of a load balancer (bonding)


Slave to a master link


Driver supports setting media type (only used by ARM ethernet)


Link selects port automatically (only used by ARM ethernet)


Echo sent packets (testing feature, CAN only)


Unused (BSD compatibility)


Unused (BSD compatibility)

To translate a link flag to a link flag name or vice versa:

#include <netlink/route/link.h>

char *rtnl_link_flags2str(int flags, char *buf, size_t size);

int rtnl_link_str2flags(const char *flag_name);

The transmission queue holds packets before packets are delivered to the driver for transmission. It is usually specified in number of packets but the unit may be specific to the link type.

#include <netlink/route/link.h>

void rtnl_link_set_txqlen(struct rtnl_link *link, unsigned int txqlen);

unsigned int rtnl_link_get_txqlen(struct rtnl_link *link);

The operational status has been introduced to provide extended information on the link status. Traditionally the link state has been described using the link flags IFF_UP, IFF_RUNNING, IFF_LOWER_UP, and IFF_DORMANT which was no longer sufficient for some link types.

#include <netlink/route/link.h>

void rtnl_link_set_operstate(struct rtnl_link *link, uint8_t state);

uint8_t rtnl_link_get_operstate(struct rtnl_link *link);


Unknown state


Link not present


Link down


L1 down






Link up

Translation of operational status code to string and vice versa:

#include <netlink/route/link.h>

char *rtnl_link_operstate2str(uint8_t state, char *buf, size_t size);

int rtnl_link_str2operstate(const char *name);

Currently known link modes are:


Default link mode


Limit upward transition to dormant

#include <netlink/route/link.h>

void rtnl_link_set_linkmode(struct rtnl_link *link, uint8_t mode);

uint8_t rtnl_link_get_linkmode(struct rtnl_link *link);

Translation of link mode to string and vice versa:

char *rtnl_link_mode2str(uint8_t mode, char *buf, size_t len);

uint8_t rtnl_link_str2mode(const char *name);

Alternative name for the link, primarly used for SNMP IfAlias.

#include <netlink/route/link.h>

const char *rtnl_link_get_ifalias(struct rtnl_link *link);

void rtnl_link_set_ifalias(struct rtnl_link *link, const char *alias);

Length limit: 256

#include <netlink/route/link.h>

#include <linux/if_arp.h>

void rtnl_link_set_arptype(struct rtnl_link *link, unsigned int arptype);

unsigned int rtnl_link_get_arptype(struct rtnl_link *link);

Translation of hardware type to character string and vice versa:

#include <netlink/utils.h>

char *nl_llproto2str(int arptype, char *buf, size_t len);

int nl_str2llproto(const char *name);

The name of the queueing discipline used by the link is of informational nature only. It is a read-only attribute provided by the kernel and cannot be modified. The set function is provided solely for the purpose of creating link objects to be used for comparison.

For more information on how to modify the qdisc of a link, see section Traffic Control.

#include <netlink/route/link.h>

void rtnl_link_set_qdisc(struct rtnl_link *link, const char *name);

char *rtnl_link_get_qdisc(struct rtnl_link *link);

The number of subsystem currently depending on the link being promiscuous mode. A value of 0 indicates that the link is not in promiscuous mode. It is a read-only attribute provided by the kernel and cannot be modified. The set function is provided solely for the purpose of creating link objects to be used for comparison.

#include <netlink/route/link.h>

void rtnl_link_set_promiscuity(struct rtnl_link *link, uint32_t count);

uint32_t rtnl_link_get_promiscuity(struct rtnl_link *link);

The number of RX/TX queues the link provides. The attribute is writable but will only be considered when creating a new network device via netlink.

#include <netlink/route/link.h>

void rtnl_link_set_num_tx_queues(struct rtnl_link *link, uint32_t nqueues);

uint32_t rtnl_link_get_num_tx_queues(struct rtnl_link *link);

void rtnl_link_set_num_rx_queues(struct rtnl_link *link, uint32_t nqueues);

uint32_t rtnl_link_get_num_rx_queues(struct rtnl_link *link);

This attribute is unused and obsoleted in all recent kernels.

Example: Add bonding link
#include <netlink/route/link.h>

struct rtnl_link *link;

link = rtnl_link_bond_alloc();

rtnl_link_set_name(link, "my_bond");

/* requires admin privileges */

if (rtnl_link_add(sk, link, NLM_F_CREATE) < 0)

        /* error */


extern char *           rtnl_link_vlan_flags2str(int, char *, size_t);

extern int              rtnl_link_vlan_str2flags(const char *);

extern int              rtnl_link_vlan_set_id(struct rtnl_link *, int);

extern int              rtnl_link_vlan_get_id(struct rtnl_link *);

extern int              rtnl_link_vlan_set_flags(struct rtnl_link *,

                                                 unsigned int);

extern int              rtnl_link_vlan_unset_flags(struct rtnl_link *,

                                                   unsigned int);

extern unsigned int     rtnl_link_vlan_get_flags(struct rtnl_link *);

extern int              rtnl_link_vlan_set_ingress_map(struct rtnl_link *,

                                                       int, uint32_t);

extern uint32_t *       rtnl_link_vlan_get_ingress_map(struct rtnl_link *);

extern int              rtnl_link_vlan_set_egress_map(struct rtnl_link *,

                                                      uint32_t, int);

extern struct vlan_map *rtnl_link_vlan_get_egress_map(struct rtnl_link *,

                                                      int *);

Example: Add a VLAN device
struct rtnl_link *link;

int master_index;

/* lookup interface index of eth0 */

if (!(master_index = rtnl_link_name2i(link_cache, "eth0")))

        /* error */

/* allocate new link object of type vlan */

link = rtnl_link_vlan_alloc();

/* set eth0 to be our master device */

rtnl_link_set_link(link, master_index);

rtnl_link_vlan_set_id(link, 10);

if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0)

        /* error */


extern struct rtnl_link *rtnl_link_macvlan_alloc(void);

extern int              rtnl_link_is_macvlan(struct rtnl_link *);

extern char *           rtnl_link_macvlan_mode2str(int, char *, size_t);

extern int              rtnl_link_macvlan_str2mode(const char *);

extern char *           rtnl_link_macvlan_flags2str(int, char *, size_t);

extern int              rtnl_link_macvlan_str2flags(const char *);

extern int              rtnl_link_macvlan_set_mode(struct rtnl_link *,


extern uint32_t         rtnl_link_macvlan_get_mode(struct rtnl_link *);

extern int              rtnl_link_macvlan_set_flags(struct rtnl_link *,


extern int              rtnl_link_macvlan_unset_flags(struct rtnl_link *,


extern uint16_t         rtnl_link_macvlan_get_flags(struct rtnl_link *);

Example: Add a MACVLAN device
struct rtnl_link *link;

int master_index;

struct nl_addr* addr;

/* lookup interface index of eth0 */

if (!(master_index = rtnl_link_name2i(link_cache, "eth0")))

        /* error */

/* allocate new link object of type macvlan */

link = rtnl_link_macvlan_alloc();

/* set eth0 to be our master device */

rtnl_link_set_link(link, master_index);

/* set address of virtual interface */

addr = nl_addr_build(AF_LLC, ether_aton("00:11:22:33:44:55"), ETH_ALEN);

rtnl_link_set_addr(link, addr);


/* set mode of virtual interface */

rtnl_link_macvlan_set_mode(link, rtnl_link_macvlan_str2mode("bridge"));

if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0)

        /* error */


extern struct rtnl_link *rtnl_link_vxlan_alloc(void);

extern int      rtnl_link_is_vxlan(struct rtnl_link *);

extern int      rtnl_link_vxlan_set_id(struct rtnl_link *, uint32_t);

extern int      rtnl_link_vxlan_get_id(struct rtnl_link *, uint32_t *);

extern int      rtnl_link_vxlan_set_group(struct rtnl_link *, struct nl_addr *);

extern int      rtnl_link_vxlan_get_group(struct rtnl_link *, struct nl_addr **);

extern int      rtnl_link_vxlan_set_link(struct rtnl_link *, uint32_t);

extern int      rtnl_link_vxlan_get_link(struct rtnl_link *, uint32_t *);

extern int      rtnl_link_vxlan_set_local(struct rtnl_link *, struct nl_addr *);

extern int      rtnl_link_vxlan_get_local(struct rtnl_link *, struct nl_addr **);

extern int      rtnl_link_vxlan_set_ttl(struct rtnl_link *, uint8_t);

extern int      rtnl_link_vxlan_get_ttl(struct rtnl_link *);

extern int      rtnl_link_vxlan_set_tos(struct rtnl_link *, uint8_t);

extern int      rtnl_link_vxlan_get_tos(struct rtnl_link *);

extern int      rtnl_link_vxlan_set_learning(struct rtnl_link *, uint8_t);

extern int      rtnl_link_vxlan_get_learning(struct rtnl_link *);

extern int      rtnl_link_vxlan_enable_learning(struct rtnl_link *);

extern int      rtnl_link_vxlan_disable_learning(struct rtnl_link *);

extern int      rtnl_link_vxlan_set_ageing(struct rtnl_link *, uint32_t);

extern int      rtnl_link_vxlan_get_ageing(struct rtnl_link *, uint32_t *);

extern int      rtnl_link_vxlan_set_limit(struct rtnl_link *, uint32_t);

extern int      rtnl_link_vxlan_get_limit(struct rtnl_link *, uint32_t *);

extern int      rtnl_link_vxlan_set_port_range(struct rtnl_link *,

                                               struct ifla_vxlan_port_range *);

extern int      rtnl_link_vxlan_get_port_range(struct rtnl_link *,

                                               struct ifla_vxlan_port_range *);

extern int      rtnl_link_vxlan_set_proxy(struct rtnl_link *, uint8_t);

extern int      rtnl_link_vxlan_get_proxy(struct rtnl_link *);

extern int      rtnl_link_vxlan_enable_proxy(struct rtnl_link *);

extern int      rtnl_link_vxlan_disable_proxy(struct rtnl_link *);

extern int      rtnl_link_vxlan_set_rsc(struct rtnl_link *, uint8_t);

extern int      rtnl_link_vxlan_get_rsc(struct rtnl_link *);

extern int      rtnl_link_vxlan_enable_rsc(struct rtnl_link *);

extern int      rtnl_link_vxlan_disable_rsc(struct rtnl_link *);

extern int      rtnl_link_vxlan_set_l2miss(struct rtnl_link *, uint8_t);

extern int      rtnl_link_vxlan_get_l2miss(struct rtnl_link *);

extern int      rtnl_link_vxlan_enable_l2miss(struct rtnl_link *);

extern int      rtnl_link_vxlan_disable_l2miss(struct rtnl_link *);

extern int      rtnl_link_vxlan_set_l3miss(struct rtnl_link *, uint8_t);

extern int      rtnl_link_vxlan_get_l3miss(struct rtnl_link *);

extern int      rtnl_link_vxlan_enable_l3miss(struct rtnl_link *);

extern int      rtnl_link_vxlan_disable_l3miss(struct rtnl_link *);

Example: Add a VXLAN device
struct rtnl_link *link;

struct nl_addr* addr;

/* allocate new link object of type vxlan */

link = rtnl_link_vxlan_alloc();

/* set interface name */

rtnl_link_set_name(link, "vxlan128");

/* set VXLAN network identifier */

if ((err = rtnl_link_vxlan_set_id(link, 128)) < 0)

        /* error */

/* set multicast address to join */

if ((err = nl_addr_parse("", AF_INET, &addr)) < 0)

        /* error */

if ((err = rtnl_link_set_group(link, addr)) < 0)

        /* error */


if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0)

        /* error */


4. Neighbouring

5. Routing

6. Traffic Control

The traffic control architecture allows the queueing and prioritization of packets before they are enqueued to the network driver. To a limited degree it is also possible to take control of network traffic as it enters the network stack.

The architecture consists of three different types of modules:

  • Queueing disciplines (qdisc) provide a mechanism to enqueue packets in different forms. They may be used to implement fair queueing, prioritization of differentiated services, enforce bandwidth limitations, or even to simulate network behaviour such as packet loss and packet delay. Qdiscs can be classful in which case they allow traffic classes described in the next paragraph to be attached to them.

  • Traffic classes (class) are supported by several qdiscs to build a tree structure for different types of traffic. Each class may be assigned its own set of attributes such as bandwidth limits or queueing priorities. Some qdiscs even allow borrowing of bandwidth between classes.

  • Classifiers (cls) are used to decide which qdisc/class the packet should be enqueued to. Different types of classifiers exists, ranging from classification based on protocol header values to classification based on packet priority or firewall marks. Additionally most classifiers support extended matches (ematch) which allow extending classifiers by a set of matcher modules, and actions which allow classifiers to take actions such as mangling, mirroring, or even rerouting of packets.

Default Qdisc

The default qdisc used on all network devices is pfifo_fast. Network devices which do not require a transmit queue such as the loopback device do not have a default qdisc attached. The pfifo_fast qdisc provides three bands to prioritize interactive traffic over bulk traffic. Classification is based on the packet priority (diffserv).

Default Qdisc

Multiqueue Default Qdisc

If the network device provides multiple transmit queues the mq qdisc is used by default. It will automatically create a separate class for each transmit queue available and will also replace the single per device tx lock with a per queue lock.

Multiqueue default Qdisc

Example of a customized classful qdisc setup

The following figure illustrates a possible combination of different queueing and classification modules to implement quality of service needs.

Classful Qdisc diagram

6.1. Traffic Control Object

Each type traffic control module (qdisc, class, classifier) is represented by its own structure. All of them are based on the traffic control object represented by struct rtnl_tc which itself is based on the generic object struct nl_object to make it cacheable. The traffic control object contains all attributes, implementation details and statistics that are shared by all of the traffic control object types.

struct rtnl_tc hierarchy

It is not possible to allocate a struct rtnl_tc object, instead the actual tc object types must be allocated directly using rtnl_qdisc_alloc(), rtnl_class_alloc(), rtnl_cls_alloc() and then casted to struct rtnl_tc using the TC_CAST() macro.

Usage Example: Allocation, Casting, Freeing
#include <netlink/route/tc.h>

#include <netlink/route/qdisc.h>

struct rtnl_qdisc *qdisc;

/* Allocation of a qdisc object */

qdisc = rtnl_qdisc_alloc();

/* Cast the qdisc to a tc object using TC_CAST() to use rtnl_tc_ functions. */

rtnl_tc_set_mpu(TC_CAST(qdisc), 64);

/* Free the qdisc object */


6.1.1. Attributes


The handle uniquely identifies a tc object and is used to refer to other tc objects when constructing tc trees.

void rtnl_tc_set_handle(struct rtnl_tc *tc, uint32_t handle);

uint32_t rtnl_tc_get_handle(struct rtnl_tc *tc);

Interface Index

The interface index specifies the network device the traffic object is attached to. The function rtnl_tc_set_link() should be preferred when setting the interface index. It stores the reference to the link object in the tc object and allows retrieving the mtu and linktype automatically.

void rtnl_tc_set_ifindex(struct rtnl_tc *tc, int ifindex);

void rtnl_tc_set_link(struct rtnl_tc *tc, struct rtnl_link *link);

int rtnl_tc_get_ifindex(struct rtnl_tc *tc);

Link Type

The link type specifies the kind of link that is used by the network device (e.g. ethernet, ATM, …). It is derived automatically when the network device is specified with rtnl_tc_set_link(). The default fallback is ARPHRD_ETHER (ethernet).

void rtnl_tc_set_linktype(struct rtnl_tc *tc, uint32_t type);

uint32_t rtnl_tc_get_linktype(struct rtnl_tc *tc);


The kind character string specifies the type of qdisc, class, classifier. Setting the kind results in the module specific structure being allocated. Therefore it is imperative to call rtnl_tc_set_kind() before using any type specific API functions such as rtnl_htb_set_rate().

int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind);

char *rtnl_tc_get_kind(struct rtnl_tc *tc);


The Minimum Packet Unit specifies the minimum packet size which will be transmitted ever be seen by this traffic control object. This value is used for rate calculations. Not all object implementations will make use of this value. The default value is 0.

void rtnl_tc_set_mpu(struct rtnl_tc *tc, uint32_t mpu);

uint32_t rtnl_tc_get_mpu(struct rtnl_tc *tc);


The Maximum Transmission Unit specifies the maximum packet size which will be transmitted. The value is derived from the link specified with rtnl_tc_set_link() if not overwritten with rtnl_tc_set_mtu(). If no link and MTU is specified, the value defaults to 1500 (ethernet).

void rtnl_tc_set_mtu(struct rtnl_tc *tc, uint32_t mtu);

uint32_t rtnl_tc_get_mtu(struct rtnl_tc *tc);


The overhead specifies the additional overhead per packet caused by the network layer. This value can be used to correct packet size calculations if the packet size on the wire does not match the packet size seen by the kernel. The default value is 0.

void rtnl_tc_set_overhead(struct rtnl_tc *tc, uint32_t overhead);

uint32_t rtnl_tc_get_overhead(struct rtnl_tc *tc);


Specifies the parent traffic control object. The parent is identifier by its handle. Special values are:

  • TC_H_ROOT: attach tc object directly to network device (root qdisc, root classifier)

  • TC_H_INGRESS: same as TC_H_ROOT but on the ingress side of the network stack.

    void rtnl_tc_set_parent(struct rtnl_tc *tc, uint32_t parent);
    uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc);

Generic statistics, see Accessing Statistics for additional information.

uint64_t rtnl_tc_get_stat(struct rtnl_tc *tc, enum rtnl_tc_stat id);

6.1.2. Accessing Statistics

The traffic control object holds a set of generic statistics. Not all traffic control modules will make use of all of these statistics. Some modules may provide additional statistics via their own APIs.

Table 1. Statistic identifiers (enum rtnl_tc_stat)
ID Type Description



Total # of packets transmitted



Total # of bytes transmitted



Current bytes/s rate



Current packets/s rate



Current length of the queue



# of packets currently backloged



# of packets dropped



# of packets requeued



# of packets that exceeded the limit

Note RTNL_TC_RATE_BPS and RTNL_TC_RATE_PPS only return meaningful values if a rate estimator has been configured.
Usage Example: Retrieving tc statistics
#include <netlink/route/tc.h>

uint64_t drops, qlen;

drops = rtnl_tc_get_stat(TC_CAST(qdisc), RTNL_TC_DROPS);

qlen  = rtnl_tc_get_stat(TC_CAST(qdisc), RTNL_TC_QLEN);

6.1.3. Rate Table Calculations

6.2. Queueing Discipline (qdisc)

Classless Qdisc

The queueing discipline (qdisc) is used to implement fair queueing, priorization or rate control. It provides a enqueue() and dequeue() operation. Whenever a network packet leaves the networking stack over a network device, be it a physical or virtual device, it will be enqueued to a qdisc unless the device is queueless. The enqueue() operation is followed by an immediate call to dequeue() for the same qdisc to eventually retrieve a packet which can be scheduled for transmission by the driver. Additionally, the networking stack runs a watchdog which polls the qdisc regularly to dequeue and send packets even if no new packets are being enqueued.

This additional watchdog is required due to the fact that qdiscs may hold on to packets and not return any packets upon dequeue() in order to enforce bandwidth restrictions.

Multiband Qdisc

The figure illustrates a trivial example of a classless qdisc consisting of three bands (queues). Use of multiple bands is a common technique in qdiscs to implement fair queueing between flows or prioritize differentiated services.

Classless qdiscs can be regarded as a blackbox, their inner workings can only be steered using the configuration parameters provided by the qdisc. There is no way of taking influence on the structure of its internal queues itself.

Classful Qdisc

Classful qdiscs allow for the queueing structure and classification process to be created by the user.

Classful Qdisc

The figure above shows a classful qdisc with a classifier attached to it which will make the decision whether to enqueue a packet to traffic class 1:1 or 1:2. Unlike with classless qdiscs, classful qdiscs allow the classification process and the structure of the queues to be defined by the user. This allows for complex traffic class rules to be applied.

Table 2. List of Qdisc Implementations
Qdisc Classful Description






This qdisc will drop all packets passed to it.



The CBQ (Class Based Queueing) is a classful qdisc which allows creating traffic classes and enforce bandwidth limitations for each class.



The DRR (Deficit Round Robin) scheduler is a classful qdisc impelemting fair queueing. Each class is assigned a quantum specyfing the maximum number of bytes that can be served per round. Unused quantum at the end of the round is carried over to the next round.








































Table 3. QDisc API Overview
Attribute C Interface
Allocation / Freeing
struct rtnl_qdisc *rtnl_qdisc_alloc(void);

void rtnl_qdisc_put(struct rtnl_qdisc *qdisc);

int rtnl_qdisc_build_add_request(struct rtnl_qdisc *qdisc, int flags,

                                 struct nl_msg **result);

int rtnl_qdisc_add(struct nl_sock *sock, struct rtnl_qdisc *qdisc,

                   int flags);

int rtnl_qdisc_build_change_request(struct rtnl_qdisc *old,

                                    struct rtnl_qdisc *new,

                                    struct nl_msg **result);

int rtnl_qdisc_change(struct nl_sock *sock, struct rtnl_qdisc *old,

                      struct rtnl_qdisc *new);

int rtnl_qdisc_build_delete_request(struct rtnl_qdisc *qdisc,

                                    struct nl_msg **result);

int rtnl_qdisc_delete(struct nl_sock *sock, struct rtnl_qdisc *qdisc);

int rtnl_qdisc_alloc_cache(struct nl_sock *sock,

                           struct nl_cache **cache);

struct rtnl_qdisc *rtnl_qdisc_get(struct nl_cache *cache, int, uint32_t);

struct rtnl_qdisc *rtnl_qdisc_get_by_parent(struct nl_cache *, int, uint32_t);

6.2.1. Retrieving Qdisc Configuration

The function rtnl_qdisc_alloc_cache() is used to retrieve the current qdisc configuration in the kernel. It will construct a RTM_GETQDISC netlink message, requesting the complete list of qdiscs configured in the kernel.

#include <netlink/route/qdisc.h>

struct nl_cache *all_qdiscs;

if (rtnl_link_alloc_cache(sock, &all_qdiscs) < 0)

        /* error while retrieving qdisc cfg */

The cache can be accessed using the following functions:

  • Search qdisc with matching ifindex and handle:

    struct rtnl_qdisc *rtnl_qdisc_get(struct nl_cache *cache, int ifindex, uint32_t handle);
  • Search qdisc with matching ifindex and parent:

    struct rtnl_qdisc *rtnl_qdisc_get_by_parent(struct nl_cache *cache, int ifindex , uint32_t parent);
  • Or any of the generic cache functions (e.g. nl_cache_search(), nl_cache_dump(), etc.)

Example: Search and print qdisc
struct rtnl_qdisc *qdisc;

int ifindex;

ifindex = rtnl_link_get_ifindex(eth0_obj);

/* search for qdisc on eth0 with handle 1:0 */

if (!(qdisc = rtnl_qdisc_get(all_qdiscs, ifindex, TC_HANDLE(1, 0))))

        /* no such qdisc found */

nl_object_dump(OBJ_CAST(qdisc), NULL);


6.2.2. Adding a Qdisc

In order to add a new qdisc to the kernel, a qdisc object needs to be allocated. It will hold all attributes of the new qdisc.

#include <netlink/route/qdisc.h>

struct rtnl_qdisc *qdisc;

if (!(qdisc = rtnl_qdisc_alloc()))

        /* OOM error */

The next step is to specify all generic qdisc attributes using the tc object interface described in the section Attributes.

The following attributes must be specified: - IfIndex - Parent - Kind

/* Attach qdisc to device eth0 */

rtnl_tc_set_link(TC_CAST(qdisc), eth0_obj);

/* Make this the root qdisc */

rtnl_tc_set_parent(TC_CAST(qdisc), TC_H_ROOT);

/* Set qdisc identifier to 1:0, if left unspecified, a handle will be generated by the kernel. */

rtnl_tc_set_handle(TC_CAST(qdisc), TC_HANDLE(1, 0));

/* Make this a HTB qdisc */

rtnl_tc_set_kind(TC_CAST(qdisc), "htb");

After specyfing the qdisc kind (rtnl_tc_set_kind()) the qdisc type specific interface can be used to set attributes which are specific to the respective qdisc implementations:

/* HTB feature: Make unclassified packets go to traffic class 1:5 */

rtnl_htb_set_defcls(qdisc, TC_HANDLE(1, 5));

Finally, the qdisc is ready to be added and can be passed on to the function rntl_qdisc_add() which takes care of constructing a netlink message requesting the addition of the new qdisc, sends the message to the kernel and waits for the response by the kernel. The function returns 0 if the qdisc has been added or updated successfully or a negative error code if an error occured.

Caution The kernel operation for updating and adding a qdisc is the same. Therefore when calling rtnl_qdisc_add() any existing qdisc with matching handle will be updated unless the flag NLM_F_EXCL is specified.

The following flags may be specified:


Create qdisc if it does not exist, otherwise -NLE_OBJ_NOTFOUND is returned.


If another qdisc is already attached to the same parent and their handles mismatch, replace the qdisc instead of returning -EEXIST.


Return -NLE_EXISTS if a qdisc with matching handles exists already.

Warning The function rtnl_qdisc_add() requires administrator privileges.
/* Submit request to kernel and wait for response */

err = rtnl_qdisc_add(sock, qdisc, NLM_F_CREATE);

/* Return the qdisc object to free memory resources */


if (err < 0) {

        fprintf(stderr, "Unable to add qdisc: %s\n", nl_geterror(err));

        return err;


6.2.3. Deleting a qdisc

#include <netlink/route/qdisc.h>

struct rtnl_qdisc *qdisc;

qdisc = rtnl_qdisc_alloc();

rtnl_tc_set_link(TC_CAST(qdisc), eth0_obj);

rtnl_tc_set_parent(TC_CAST(qdisc), TC_H_ROOT);

rtnl_qdisc_delete(sock, qdisc)


Warning The function rtnl_qdisc_delete() requires administrator privileges.

6.2.4. HTB - Hierarchical Token Bucket

HTB Qdisc Attributes
Default Class

The default class is the fallback class to which all traffic which remained unclassified is directed to. If no default class or an invalid default class is specified, packets are transmitted directly to the next layer (direct transmissions).

uint32_t rtnl_htb_get_defcls(struct rtnl_qdisc *qdisc);

int rtnl_htb_set_defcls(struct rtnl_qdisc *qdisc, uint32_t defcls);

Rate to Quantum (r2q)


uint32_t rtnl_htb_get_rate2quantum(struct rtnl_qdisc *qdisc);

int rtnl_htb_set_rate2quantum(struct rtnl_qdisc *qdisc, uint32_t rate2quantum);

HTB Class Attributes
uint32_t rtnl_htb_get_prio(struct rtnl_class *class);

int rtnl_htb_set_prio(struct rtnl_class *class, uint32_t prio);


The rate (bytes/s) specifies the maximum bandwidth an invidivual class can use without borrowing. The rate of a class should always be greater or erqual than the rate of its children.

uint32_t rtnl_htb_get_rate(struct rtnl_class *class);

int rtnl_htb_set_rate(struct rtnl_class *class, uint32_t ceil);

Ceil Rate

The ceil rate specifies the maximum bandwidth an invidivual class can use. This includes bandwidth that is being borrowed from other classes. Ceil defaults to the class rate implying that by default the class will not borrow. The ceil rate of a class should always be greater or erqual than the ceil rate of its children.

uint32_t rtnl_htb_get_ceil(struct rtnl_class *class);

int rtnl_htb_set_ceil(struct rtnl_class *class, uint32_t ceil);



uint32_t rtnl_htb_get_rbuffer(struct rtnl_class *class);

int rtnl_htb_set_rbuffer(struct rtnl_class *class, uint32_t burst);

Ceil Burst


uint32_t rtnl_htb_get_bbuffer(struct rtnl_class *class);

int rtnl_htb_set_bbuffer(struct rtnl_class *class, uint32_t burst);



int rtnl_htb_set_quantum(struct rtnl_class *class, uint32_t quantum);

extern int rtnl_htb_set_cbuffer(struct rtnl_class *, uint32_t);

6.3. Class



qdisc =


class =


qdisc =


class =



qdisc =


class =


qdisc =


class =



qdisc =


class =


if pX != hX return -EINVAL

qdisc =


class =


6.4. Classifier (cls)


6.5. ClassID Management


6.6. Packet Location Aliasing (pktloc)


6.7. Traffic Control Module API