libnl 1.1.4
Modules | Data Structures

Modules

 Queueing Discipline Modules
 
 Queueing Discipline Object
 

Data Structures

struct  rtnl_qdisc_ops
 Qdisc Operations. More...
 

QDisc Addition

struct nl_msg * rtnl_qdisc_build_add_request (struct rtnl_qdisc *qdisc, int flags)
 Build a netlink message to add a new qdisc.
 
int rtnl_qdisc_add (struct nl_handle *handle, struct rtnl_qdisc *qdisc, int flags)
 Add a new qdisc.
 

QDisc Modification

struct nl_msg * rtnl_qdisc_build_change_request (struct rtnl_qdisc *qdisc, struct rtnl_qdisc *new)
 Build a netlink message to change attributes of a existing qdisc.
 
int rtnl_qdisc_change (struct nl_handle *handle, struct rtnl_qdisc *qdisc, struct rtnl_qdisc *new)
 Change attributes of a qdisc.
 

QDisc Deletion

struct nl_msg * rtnl_qdisc_build_delete_request (struct rtnl_qdisc *qdisc)
 Build a netlink request message to delete a qdisc.
 
int rtnl_qdisc_delete (struct nl_handle *handle, struct rtnl_qdisc *qdisc)
 Delete a qdisc.
 

Qdisc Cache Management

struct nl_cache * rtnl_qdisc_alloc_cache (struct nl_handle *handle)
 Build a qdisc cache including all qdiscs currently configured in the kernel.
 
struct rtnl_qdisc * rtnl_qdisc_get_by_parent (struct nl_cache *cache, int ifindex, uint32_t parent)
 Look up qdisc by its parent in the provided cache.
 
struct rtnl_qdisc * rtnl_qdisc_get (struct nl_cache *cache, int ifindex, uint32_t handle)
 Look up qdisc by its handle in the provided cache.
 

Detailed Description

Qdisc Handles
In general, qdiscs are identified by the major part of a traffic control handle (the upper 16 bits). A few special values exist though:
  • TC_H_ROOT: root qdisc (directly attached to the device)
  • TC_H_INGRESS: ingress qdisc (directly attached to the device)
  • TC_H_UNSPEC: unspecified qdisc (no reference)
1) Adding a Qdisc
// Allocate a new empty qdisc to be filled out
struct rtnl_qdisc *qdisc = rtnl_qdisc_alloc();
// ... specify the kind of the Qdisc
rtnl_qdisc_set_kind(qdisc, "pfifo");
// Specify the device the qdisc should be attached to
rtnl_qdisc_set_ifindex(qdisc, ifindex);
// ... specify the parent qdisc
rtnl_qdisc_set_parent(qdisc, TC_H_ROOT);
// Specifying the handle is not required but makes reidentifying easier
// and may help to avoid adding a qdisc twice.
rtnl_qdisc_set_handle(qdisc, 0x000A0000);
// Now on to specify the qdisc specific options, see the relevant qdisc
// modules for documentation, in this example we set the upper limit of
// the packet fifo qdisc to 64
rtnl_qdisc_add(handle, qdisc, NLM_R_REPLACE);
// Free up the memory
rtnl_qdisc_put(qdisc);
int rtnl_qdisc_fifo_set_limit(struct rtnl_qdisc *qdisc, int limit)
Set limit of FIFO qdisc.
Definition: fifo.c:145
int rtnl_qdisc_add(struct nl_handle *handle, struct rtnl_qdisc *qdisc, int flags)
Add a new qdisc.
Definition: qdisc.c:224
2) Deleting a Qdisc
// Allocate a new empty qdisc to be filled out with the parameters
// specifying the qdisc to be deleted. Alternatively a fully equiped
// Qdisc object from a cache can be used.
struct rtnl_qdisc *qdisc = rtnl_qdisc_alloc();
// The interface index of the device the qdisc is on and the parent handle
// are the least required fields to be filled out.
// Note: Specify TC_H_ROOT or TC_H_INGRESS as parent handle to delete the
// root respectively root ingress qdisc.
rtnl_qdisc_set_ifindex(qdisc, ifindex);
rtnl_qdisc_set_parent(qdisc, parent_handle);
// If required for identification, the handle can be specified as well.
rtnl_qdisc_set_handle(qdisc, qdisc_handle);
// Not required but maybe helpful as sanity check, the kind of the qdisc
// can be specified to avoid mistakes.
rtnl_qdisc_set_kind(qdisc, "pfifo");
// Finally delete the qdisc with rtnl_qdisc_delete(), alternatively
// rtnl_qdisc_build_delete_request() can be invoked to generate an
// appropritate netlink message to send out.
rtnl_qdisc_delete(handle, qdisc);
// Free up the memory
rtnl_qdisc_put(qdisc);
int rtnl_qdisc_delete(struct nl_handle *handle, struct rtnl_qdisc *qdisc)
Delete a qdisc.
Definition: qdisc.c:348

Function Documentation

◆ rtnl_qdisc_build_add_request()

struct nl_msg * rtnl_qdisc_build_add_request ( struct rtnl_qdisc *  qdisc,
int  flags 
)
Parameters
qdiscqdisc to add
flagsadditional netlink message flags

Builds a new netlink message requesting an addition of a qdisc. The netlink message header isn't fully equipped with all relevant fields and must be sent out via nl_send_auto_complete() or supplemented as needed.

Common message flags used:

  • NLM_F_REPLACE - replace a potential existing qdisc
Returns
New netlink message

Definition at line 197 of file qdisc.c.

199{
200 struct nl_msg *msg;
201
202 msg = qdisc_build(qdisc, RTM_NEWQDISC, NLM_F_CREATE | flags);
203 if (!msg)
204 nl_errno(ENOMEM);
205
206 return msg;
207}
#define NLM_F_CREATE
Create config object if it doesn't already exist.

References NLM_F_CREATE.

Referenced by rtnl_qdisc_add().

◆ rtnl_qdisc_add()

int rtnl_qdisc_add ( struct nl_handle *  handle,
struct rtnl_qdisc *  qdisc,
int  flags 
)
Parameters
handlenetlink handle
qdiscqdisc to delete
flagsadditional netlink message flags

Builds a netlink message by calling rtnl_qdisc_build_add_request(), sends the request to the kernel and waits for the ACK to be received and thus blocks until the request has been processed.

Common message flags used:

  • NLM_F_REPLACE - replace a potential existing qdisc
Returns
0 on success or a negative error code

Definition at line 224 of file qdisc.c.

226{
227 struct nl_msg *msg;
228 int err;
229
230 msg = rtnl_qdisc_build_add_request(qdisc, flags);
231 if (!msg)
232 return nl_errno(ENOMEM);
233
234 err = nl_send_auto_complete(handle, msg);
235 nlmsg_free(msg);
236 if (err < 0)
237 return err;
238
239 return nl_wait_for_ack(handle);
240}
void nlmsg_free(struct nl_msg *n)
Free a netlink message.
Definition: msg.c:656
int nl_send_auto_complete(struct nl_handle *handle, struct nl_msg *msg)
Send netlink message and check & extend header values as needed.
Definition: nl.c:373
int nl_wait_for_ack(struct nl_handle *handle)
Wait for ACK.
Definition: nl.c:801
struct nl_msg * rtnl_qdisc_build_add_request(struct rtnl_qdisc *qdisc, int flags)
Build a netlink message to add a new qdisc.
Definition: qdisc.c:197

References nl_send_auto_complete(), nl_wait_for_ack(), nlmsg_free(), and rtnl_qdisc_build_add_request().

◆ rtnl_qdisc_build_change_request()

struct nl_msg * rtnl_qdisc_build_change_request ( struct rtnl_qdisc *  qdisc,
struct rtnl_qdisc *  new 
)
Parameters
qdiscqdisc to change
newnew qdisc attributes

Builds a new netlink message requesting an change of qdisc attributes. The netlink message header isn't fully equipped with all relevant fields and must be sent out via nl_send_auto_complete() or supplemented as needed.

Returns
New netlink message

Definition at line 261 of file qdisc.c.

263{
264 return qdisc_build(qdisc, RTM_NEWQDISC, NLM_F_REPLACE);
265}
#define NLM_F_REPLACE
Replace existing matching config object with this request.

References NLM_F_REPLACE.

Referenced by rtnl_qdisc_change().

◆ rtnl_qdisc_change()

int rtnl_qdisc_change ( struct nl_handle *  handle,
struct rtnl_qdisc *  qdisc,
struct rtnl_qdisc *  new 
)
Parameters
handlenetlink handle
qdiscqdisc to change
newnew qdisc attributes

Builds a netlink message by calling rtnl_qdisc_build_change_request(), sends the request to the kernel and waits for the ACK to be received and thus blocks until the request has been processed.

Returns
0 on success or a negative error code

Definition at line 279 of file qdisc.c.

281{
282 struct nl_msg *msg;
283 int err;
284
285 msg = rtnl_qdisc_build_change_request(qdisc, new);
286 if (!msg)
287 return nl_errno(ENOMEM);
288
289 err = nl_send_auto_complete(handle, msg);
290 nlmsg_free(msg);
291 if (err < 0)
292 return err;
293
294 return nl_wait_for_ack(handle);
295}
struct nl_msg * rtnl_qdisc_build_change_request(struct rtnl_qdisc *qdisc, struct rtnl_qdisc *new)
Build a netlink message to change attributes of a existing qdisc.
Definition: qdisc.c:261

References nl_send_auto_complete(), nl_wait_for_ack(), nlmsg_free(), and rtnl_qdisc_build_change_request().

◆ rtnl_qdisc_build_delete_request()

struct nl_msg * rtnl_qdisc_build_delete_request ( struct rtnl_qdisc *  qdisc)
Parameters
qdiscqdisc to delete

Builds a new netlink message requesting a deletion of a qdisc. The netlink message header isn't fully equipped with all relevant fields and must thus be sent out via nl_send_auto_complete() or supplemented as needed.

Returns
New netlink message

Definition at line 315 of file qdisc.c.

316{
317 struct nl_msg *msg;
318 struct tcmsg tchdr;
319 int required = TCA_ATTR_IFINDEX | TCA_ATTR_PARENT;
320
321 if ((qdisc->ce_mask & required) != required)
322 BUG();
323
324 msg = nlmsg_alloc_simple(RTM_DELQDISC, 0);
325 if (!msg)
326 return NULL;
327
328 tchdr.tcm_family = AF_UNSPEC,
329 tchdr.tcm_handle = qdisc->q_handle,
330 tchdr.tcm_parent = qdisc->q_parent,
331 tchdr.tcm_ifindex = qdisc->q_ifindex,
332 nlmsg_append(msg, &tchdr, sizeof(tchdr), NLMSG_ALIGNTO);
333
334 return msg;
335}
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition: msg.c:448
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition: msg.c:549

References nlmsg_alloc_simple(), and nlmsg_append().

Referenced by rtnl_qdisc_delete().

◆ rtnl_qdisc_delete()

int rtnl_qdisc_delete ( struct nl_handle *  handle,
struct rtnl_qdisc *  qdisc 
)
Parameters
handlenetlink handle
qdiscqdisc to delete

Builds a netlink message by calling rtnl_qdisc_build_delete_request(), sends the request to the kernel and waits for the ACK to be received and thus blocks until the request has been processed.

Returns
0 on success or a negative error code

Definition at line 348 of file qdisc.c.

349{
350 struct nl_msg *msg;
351 int err;
352
354 if (!msg)
355 return nl_errno(ENOMEM);
356
357 err = nl_send_auto_complete(handle, msg);
358 nlmsg_free(msg);
359 if (err < 0)
360 return err;
361
362 return nl_wait_for_ack(handle);
363}
struct nl_msg * rtnl_qdisc_build_delete_request(struct rtnl_qdisc *qdisc)
Build a netlink request message to delete a qdisc.
Definition: qdisc.c:315

References nl_send_auto_complete(), nl_wait_for_ack(), nlmsg_free(), and rtnl_qdisc_build_delete_request().

◆ rtnl_qdisc_alloc_cache()

struct nl_cache * rtnl_qdisc_alloc_cache ( struct nl_handle *  handle)
Parameters
handlenetlink handle

Allocates a new cache, initializes it properly and updates it to include all qdiscs currently configured in the kernel.

Note
The caller is responsible for destroying and freeing the cache after using it.
Returns
The cache or NULL if an error has occured.

Definition at line 384 of file qdisc.c.

385{
386 struct nl_cache * cache;
387
389 if (cache == NULL)
390 return NULL;
391
392 if (handle && nl_cache_refill(handle, cache) < 0) {
393 nl_cache_free(cache);
394 return NULL;
395 }
396
397 return cache;
398}
void nl_cache_free(struct nl_cache *cache)
Free a cache.
Definition: cache.c:277
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate an empty cache.
Definition: cache.c:170
int nl_cache_refill(struct nl_handle *handle, struct nl_cache *cache)
(Re)fill a cache with the contents in the kernel.
Definition: cache.c:680
Qdisc Operations.
Definition: qdisc-modules.h:26

References nl_cache_alloc(), nl_cache_free(), and nl_cache_refill().

◆ rtnl_qdisc_get_by_parent()

struct rtnl_qdisc * rtnl_qdisc_get_by_parent ( struct nl_cache *  cache,
int  ifindex,
uint32_t  parent 
)
Parameters
cacheqdisc cache
ifindexinterface the qdisc is attached to
parentparent handle
Returns
pointer to qdisc inside the cache or NULL if no match was found.

Definition at line 407 of file qdisc.c.

409{
410 struct rtnl_qdisc *q;
411
412 if (cache->c_ops != &rtnl_qdisc_ops)
413 return NULL;
414
415 nl_list_for_each_entry(q, &cache->c_items, ce_list) {
416 if (q->q_parent == parent && q->q_ifindex == ifindex) {
417 nl_object_get((struct nl_object *) q);
418 return q;
419 }
420 }
421
422 return NULL;
423}
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition: object.c:167

References nl_object_get().

Referenced by rtnl_class_leaf_qdisc().

◆ rtnl_qdisc_get()

struct rtnl_qdisc * rtnl_qdisc_get ( struct nl_cache *  cache,
int  ifindex,
uint32_t  handle 
)
Parameters
cacheqdisc cache
ifindexinterface the qdisc is attached to
handleqdisc handle
Returns
pointer to qdisc inside the cache or NULL if no match was found.

Definition at line 432 of file qdisc.c.

434{
435 struct rtnl_qdisc *q;
436
437 if (cache->c_ops != &rtnl_qdisc_ops)
438 return NULL;
439
440 nl_list_for_each_entry(q, &cache->c_items, ce_list) {
441 if (q->q_handle == handle && q->q_ifindex == ifindex) {
442 nl_object_get((struct nl_object *) q);
443 return q;
444 }
445 }
446
447 return NULL;
448}

References nl_object_get().