13 #include <netlink/cli/utils.h> 14 #include <netlink/cli/tc.h> 15 #include <netlink/cli/qdisc.h> 16 #include <netlink/cli/link.h> 18 #include <netlink-private/route/tc-api.h> 20 #include <linux/netlink.h> 24 static void print_usage(
void)
27 "Usage: nl-qdisc-add [OPTIONS]... QDISC [CONFIGURATION]...\n" 30 " -q, --quiet Do not print informal notifications.\n" 31 " -h, --help Show this help text.\n" 32 " -v, --version Show versioning information.\n" 33 " --update Update qdisc if it exists.\n" 34 " --replace Replace or update qdisc if it exists.\n" 35 " --update-only Only update qdisc, never create it.\n" 36 " --replace-only Only replace or update qdisc, never create it.\n" 37 " -d, --dev=DEV Network device the qdisc should be attached to.\n" 38 " -i, --id=ID ID of new qdisc (default: auto-generated)r\n" 39 " -p, --parent=ID ID of parent { root | ingress | QDISC-ID }\n" 42 " -h, --help Show help text of qdisc specific options.\n" 45 " $ nl-qdisc-add --dev=eth1 --parent=root htb --rate=100mbit\n" 51 int main(
int argc,
char *argv[])
54 struct rtnl_qdisc *qdisc;
56 struct nl_cache *link_cache;
62 struct rtnl_tc_ops *ops;
63 int err, flags = NLM_F_CREATE | NLM_F_EXCL;
64 char *kind, *
id = NULL;
66 sock = nl_cli_alloc_socket();
67 nl_cli_connect(sock, NETLINK_ROUTE);
69 link_cache = nl_cli_link_alloc_cache(sock);
71 qdisc = nl_cli_qdisc_alloc();
72 tc = (
struct rtnl_tc *) qdisc;
82 static struct option long_opts[] = {
83 {
"quiet", 0, 0,
'q' },
84 {
"help", 0, 0,
'h' },
85 {
"version", 0, 0,
'v' },
87 {
"parent", 1, 0,
'p' },
89 {
"replace", 0, 0, ARG_REPLACE },
90 {
"update", 0, 0, ARG_UPDATE },
91 {
"replace-only", 0, 0, ARG_REPLACE_ONLY },
92 {
"update-only", 0, 0, ARG_UPDATE_ONLY },
96 c = getopt_long(argc, argv,
"+qhvd:p:i:",
102 case 'q': quiet = 1;
break;
103 case 'h': print_usage();
break;
104 case 'v': nl_cli_print_version();
break;
105 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg);
break;
106 case 'p': nl_cli_tc_parse_parent(tc, optarg);
break;
107 case 'i':
id = strdup(optarg);
break;
108 case ARG_UPDATE: flags = NLM_F_CREATE;
break;
109 case ARG_REPLACE: flags = NLM_F_CREATE | NLM_F_REPLACE;
break;
110 case ARG_UPDATE_ONLY: flags = 0;
break;
111 case ARG_REPLACE_ONLY: flags = NLM_F_REPLACE;
break;
119 nl_cli_fatal(EINVAL,
"You must specify a network device (--dev=XXX)");
125 nl_cli_tc_parse_handle(tc,
id, 1);
129 kind = argv[optind++];
132 if (!(ops = rtnl_tc_get_ops(tc)))
135 if (!(tm = nl_cli_tc_lookup(ops)))
136 nl_cli_fatal(ENOTSUP,
"Qdisc type \"%s\" not supported.", kind);
138 tm->tm_parse_argv(tc, argc, argv);
146 nl_cli_fatal(EINVAL,
"Unable to add qdisc: %s", nl_geterror(err));
int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
Define the type of traffic control object.
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
int rtnl_qdisc_add(struct nl_sock *sk, struct rtnl_qdisc *qdisc, int flags)
Add qdisc.
Dump all attributes but no statistics.
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc)
Return parent identifier of a traffic control object.
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.