1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2010 Thomas Graf <tgraf@suug.ch>
4 */
5
6 #include <netlink/cli/utils.h>
7 #include <netlink/cli/tc.h>
8 #include <netlink/cli/qdisc.h>
9 #include <netlink/cli/link.h>
10
11 #include <netlink-private/route/tc-api.h>
12
13 #include <linux/netlink.h>
14
15 static int quiet = 0;
16
print_usage(void)17 static void print_usage(void)
18 {
19 printf(
20 "Usage: nl-qdisc-add [OPTIONS]... QDISC [CONFIGURATION]...\n"
21 "\n"
22 "OPTIONS\n"
23 " -q, --quiet Do not print informal notifications.\n"
24 " -h, --help Show this help text.\n"
25 " -v, --version Show versioning information.\n"
26 " --update Update qdisc if it exists.\n"
27 " --replace Replace or update qdisc if it exists.\n"
28 " --update-only Only update qdisc, never create it.\n"
29 " --replace-only Only replace or update qdisc, never create it.\n"
30 " -d, --dev=DEV Network device the qdisc should be attached to.\n"
31 " -i, --id=ID ID of new qdisc (default: auto-generated)r\n"
32 " -p, --parent=ID ID of parent { root | ingress | QDISC-ID }\n"
33 "\n"
34 "CONFIGURATION\n"
35 " -h, --help Show help text of qdisc specific options.\n"
36 "\n"
37 "EXAMPLE\n"
38 " $ nl-qdisc-add --dev=eth1 --parent=root htb --rate=100mbit\n"
39 "\n"
40 );
41 exit(0);
42 }
43
main(int argc,char * argv[])44 int main(int argc, char *argv[])
45 {
46 struct nl_sock *sock;
47 struct rtnl_qdisc *qdisc;
48 struct rtnl_tc *tc;
49 struct nl_cache *link_cache;
50 struct nl_dump_params dp = {
51 .dp_type = NL_DUMP_DETAILS,
52 .dp_fd = stdout,
53 };
54 struct nl_cli_tc_module *tm;
55 struct rtnl_tc_ops *ops;
56 int err, flags = NLM_F_CREATE | NLM_F_EXCL;
57 char *kind, *id = NULL;
58
59 sock = nl_cli_alloc_socket();
60 nl_cli_connect(sock, NETLINK_ROUTE);
61
62 link_cache = nl_cli_link_alloc_cache(sock);
63
64 qdisc = nl_cli_qdisc_alloc();
65 tc = (struct rtnl_tc *) qdisc;
66
67 for (;;) {
68 int c, optidx = 0;
69 enum {
70 ARG_REPLACE = 257,
71 ARG_UPDATE = 258,
72 ARG_REPLACE_ONLY,
73 ARG_UPDATE_ONLY,
74 };
75 static struct option long_opts[] = {
76 { "quiet", 0, 0, 'q' },
77 { "help", 0, 0, 'h' },
78 { "version", 0, 0, 'v' },
79 { "dev", 1, 0, 'd' },
80 { "parent", 1, 0, 'p' },
81 { "id", 1, 0, 'i' },
82 { "replace", 0, 0, ARG_REPLACE },
83 { "update", 0, 0, ARG_UPDATE },
84 { "replace-only", 0, 0, ARG_REPLACE_ONLY },
85 { "update-only", 0, 0, ARG_UPDATE_ONLY },
86 { 0, 0, 0, 0 }
87 };
88
89 c = getopt_long(argc, argv, "+qhvd:p:i:",
90 long_opts, &optidx);
91 if (c == -1)
92 break;
93
94 switch (c) {
95 case 'q': quiet = 1; break;
96 case 'h': print_usage(); break;
97 case 'v': nl_cli_print_version(); break;
98 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
99 case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
100 case 'i': id = strdup(optarg); break;
101 case ARG_UPDATE: flags = NLM_F_CREATE; break;
102 case ARG_REPLACE: flags = NLM_F_CREATE | NLM_F_REPLACE; break;
103 case ARG_UPDATE_ONLY: flags = 0; break;
104 case ARG_REPLACE_ONLY: flags = NLM_F_REPLACE; break;
105 }
106 }
107
108 if (optind >= argc)
109 print_usage();
110
111 if (!rtnl_tc_get_ifindex(tc))
112 nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
113
114 if (!rtnl_tc_get_parent(tc))
115 nl_cli_fatal(EINVAL, "You must specify a parent");
116
117 if (id) {
118 nl_cli_tc_parse_handle(tc, id, 1);
119 free(id);
120 }
121
122 kind = argv[optind++];
123 rtnl_tc_set_kind(tc, kind);
124
125 if (!(ops = rtnl_tc_get_ops(tc)))
126 nl_cli_fatal(ENOENT, "Unknown qdisc \"%s\"", kind);
127
128 if (!(tm = nl_cli_tc_lookup(ops)))
129 nl_cli_fatal(ENOTSUP, "Qdisc type \"%s\" not supported.", kind);
130
131 tm->tm_parse_argv(tc, argc, argv);
132
133 if (!quiet) {
134 printf("Adding ");
135 nl_object_dump(OBJ_CAST(qdisc), &dp);
136 }
137
138 if ((err = rtnl_qdisc_add(sock, qdisc, flags)) < 0)
139 nl_cli_fatal(EINVAL, "Unable to add qdisc: %s", nl_geterror(err));
140
141 return 0;
142 }
143