libnl 3.5.0

nl-qdisc-add.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * src/nl-qdisc-add.c Add Queueing Discipline
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation version 2.1
8 * of the License.
9 *
10 * Copyright (c) 2010 Thomas Graf <tgraf@suug.ch>
11 */
12
13 #include <netlink/cli/utils.h>
14 #include <netlink/cli/tc.h>
15 #include <netlink/cli/qdisc.h>
16 #include <netlink/cli/link.h>
17
18 #include <netlink-private/route/tc-api.h>
19
20 #include <linux/netlink.h>
21
22 static int quiet = 0;
23
24 static void print_usage(void)
25 {
26 printf(
27 "Usage: nl-qdisc-add [OPTIONS]... QDISC [CONFIGURATION]...\n"
28 "\n"
29 "OPTIONS\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"
40 "\n"
41 "CONFIGURATION\n"
42 " -h, --help Show help text of qdisc specific options.\n"
43 "\n"
44 "EXAMPLE\n"
45 " $ nl-qdisc-add --dev=eth1 --parent=root htb --rate=100mbit\n"
46 "\n"
47 );
48 exit(0);
49 }
50
51 int main(int argc, char *argv[])
52 {
53 struct nl_sock *sock;
54 struct rtnl_qdisc *qdisc;
55 struct rtnl_tc *tc;
56 struct nl_cache *link_cache;
57 struct nl_dump_params dp = {
58 .dp_type = NL_DUMP_DETAILS,
59 .dp_fd = stdout,
60 };
61 struct nl_cli_tc_module *tm;
62 struct rtnl_tc_ops *ops;
63 int err, flags = NLM_F_CREATE | NLM_F_EXCL;
64 char *kind, *id = NULL;
65
66 sock = nl_cli_alloc_socket();
67 nl_cli_connect(sock, NETLINK_ROUTE);
68
69 link_cache = nl_cli_link_alloc_cache(sock);
70
71 qdisc = nl_cli_qdisc_alloc();
72 tc = (struct rtnl_tc *) qdisc;
73
74 for (;;) {
75 int c, optidx = 0;
76 enum {
77 ARG_REPLACE = 257,
78 ARG_UPDATE = 258,
79 ARG_REPLACE_ONLY,
80 ARG_UPDATE_ONLY,
81 };
82 static struct option long_opts[] = {
83 { "quiet", 0, 0, 'q' },
84 { "help", 0, 0, 'h' },
85 { "version", 0, 0, 'v' },
86 { "dev", 1, 0, 'd' },
87 { "parent", 1, 0, 'p' },
88 { "id", 1, 0, 'i' },
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 },
93 { 0, 0, 0, 0 }
94 };
95
96 c = getopt_long(argc, argv, "+qhvd:p:i:",
97 long_opts, &optidx);
98 if (c == -1)
99 break;
100
101 switch (c) {
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;
112 }
113 }
114
115 if (optind >= argc)
116 print_usage();
117
118 if (!rtnl_tc_get_ifindex(tc))
119 nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
120
121 if (!rtnl_tc_get_parent(tc))
122 nl_cli_fatal(EINVAL, "You must specify a parent");
123
124 if (id) {
125 nl_cli_tc_parse_handle(tc, id, 1);
126 free(id);
127 }
128
129 kind = argv[optind++];
130 rtnl_tc_set_kind(tc, kind);
131
132 if (!(ops = rtnl_tc_get_ops(tc)))
133 nl_cli_fatal(ENOENT, "Unknown qdisc \"%s\"", kind);
134
135 if (!(tm = nl_cli_tc_lookup(ops)))
136 nl_cli_fatal(ENOTSUP, "Qdisc type \"%s\" not supported.", kind);
137
138 tm->tm_parse_argv(tc, argc, argv);
139
140 if (!quiet) {
141 printf("Adding ");
142 nl_object_dump(OBJ_CAST(qdisc), &dp);
143 }
144
145 if ((err = rtnl_qdisc_add(sock, qdisc, flags)) < 0)
146 nl_cli_fatal(EINVAL, "Unable to add qdisc: %s", nl_geterror(err));
147
148 return 0;
149 }
rtnl_tc_set_kind
int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
Define the type of traffic control object.
Definition: tc.c:530
nl_dump_params::dp_type
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
rtnl_qdisc_add
int rtnl_qdisc_add(struct nl_sock *sk, struct rtnl_qdisc *qdisc, int flags)
Add qdisc.
Definition: qdisc.c:163
NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:23
nl_object_dump
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:289
nl_cli_tc_module
Definition: tc.h:28
rtnl_tc_get_parent
uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc)
Return parent identifier of a traffic control object.
Definition: tc.c:518
nl_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:78
nl_dump_params
Dumping parameters.
Definition: types.h:33
rtnl_tc_get_ifindex
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
Definition: tc.c:294