libnl: /data/src/libnl3/src/nl-qdisc-add.c Source File

libnl 3.10.0

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