How to set up netem qdisc and tbf qdisc using libnl-route?

Viewed 2058

I'm trying to do network emulation on ubuntu 12.04 while the shell commands work:

tc qdisc add dev eth1 root handle 1:0 tbf rate 200kbit buffer 1600 limit 3000

or

tc qdisc change dev eth0 root netem loss 0.1%

Now I want to do the same things in c code and i found libnl.

I have successfully added prio & htb qdisc using the doc

http://www.carisma.slowglass.com/~tgr/libnl/doc/route.html#tc_qdisc

but when I did the netem qdisc, it returned "Invalid input data or parameter", and "Missing attribute" in tbf qdisc case. My codes is as follows:

  1. netem

    q = rtnl_qdisc_alloc();
    rtnl_tc_set_ifindex(TC_CAST(q), 2);
    rtnl_tc_set_parent(TC_CAST(q), TC_H_ROOT);
    rtnl_tc_set_handle(TC_CAST(q), TC_HANDLE(1, 0));
    rtnl_tc_set_kind(TC_CAST(q), "netem");
    
    rtnl_netem_set_delay(q, 100);
    rtnl_netem_set_loss(q, 10);
    
    int err = rtnl_qdisc_add(sock, q, NLM_F_CREATE);
    if(err<0){
      printf("netem error: %s\n", nl_geterror(err));
    }
    
  2. tbf

    q = rtnl_qdisc_alloc();
    rtnl_tc_set_ifindex(TC_CAST(q), 2);
    rtnl_tc_set_parent(TC_CAST(q), TC_H_ROOT);
    rtnl_tc_set_handle(TC_CAST(q), TC_HANDLE(1, 0));
    rtnl_tc_set_kind(TC_CAST(q), "tbf");
    
    rtnl_qdisc_tbf_set_limit(q, 1000);
    rtnl_qdisc_tbf_set_rate(q, 1000, 1000, 8);
    
    int err = rtnl_qdisc_add(sock, q, NLM_F_CREATE);
    if(err<0){
      printf("tbf error: %s\n", nl_geterror(err));
    }
    

I have tried all the corresponding functions the api ref refers, but always have the same errors "Invalid input data or parameter" and "Missing attribute", and googled with no help.

I have got stuck with the problem for more than a week, i need your help.

1 Answers
Related