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/class.h>
10 #include <netlink/cli/link.h>
11
12 #include <netlink-private/route/tc-api.h>
13
14 #include <linux/netlink.h>
15
16 static int quiet = 0;
17
print_usage(void)18 static void print_usage(void)
19 {
20 printf(
21 "Usage: nl-class-add [OPTIONS]... class [CONFIGURATION]...\n"
22 "\n"
23 "OPTIONS\n"
24 " -q, --quiet Do not print informal notifications.\n"
25 " -h, --help Show this help text.\n"
26 " -v, --version Show versioning information.\n"
27 " --update Update class if it exists.\n"
28 " --update-only Only update class, never create it.\n"
29 " -d, --dev=DEV Network device the class should be attached to.\n"
30 " -i, --id=ID ID of new class (default: auto-generated)\n"
31 " -p, --parent=ID ID of parent { root | ingress | class-ID }\n"
32 " --mtu=SIZE Overwrite MTU (default: MTU of network device)\n"
33 " --mpu=SIZE Minimum packet size on the link (default: 0).\n"
34 " --overhead=SIZE Overhead in bytes per packet (default: 0).\n"
35 " --linktype=TYPE Overwrite linktype (default: type of network device)\n"
36 "\n"
37 "CONFIGURATION\n"
38 " -h, --help Show help text of class specific options.\n"
39 "\n"
40 "EXAMPLE\n"
41 " $ nl-class-add --dev=eth1 --parent=root htb --rate=100mbit\n"
42 "\n"
43 );
44 exit(0);
45 }
46
main(int argc,char * argv[])47 int main(int argc, char *argv[])
48 {
49 struct nl_sock *sock;
50 struct rtnl_class *class;
51 struct rtnl_tc *tc;
52 struct nl_cache *link_cache;
53 struct nl_dump_params dp = {
54 .dp_type = NL_DUMP_DETAILS,
55 .dp_fd = stdout,
56 };
57 struct nl_cli_tc_module *tm;
58 struct rtnl_tc_ops *ops;
59 int err, flags = NLM_F_CREATE | NLM_F_EXCL;
60 char *kind, *id = NULL;
61
62 sock = nl_cli_alloc_socket();
63 nl_cli_connect(sock, NETLINK_ROUTE);
64
65 link_cache = nl_cli_link_alloc_cache(sock);
66
67 class = nl_cli_class_alloc();
68 tc = (struct rtnl_tc *) class;
69
70 for (;;) {
71 int c, optidx = 0;
72 enum {
73 ARG_UPDATE = 257,
74 ARG_UPDATE_ONLY = 258,
75 ARG_MTU,
76 ARG_MPU,
77 ARG_OVERHEAD,
78 ARG_LINKTYPE,
79 };
80 static struct option long_opts[] = {
81 { "quiet", 0, 0, 'q' },
82 { "help", 0, 0, 'h' },
83 { "version", 0, 0, 'v' },
84 { "dev", 1, 0, 'd' },
85 { "parent", 1, 0, 'p' },
86 { "id", 1, 0, 'i' },
87 { "update", 0, 0, ARG_UPDATE },
88 { "update-only", 0, 0, ARG_UPDATE_ONLY },
89 { "mtu", 1, 0, ARG_MTU },
90 { "mpu", 1, 0, ARG_MPU },
91 { "overhead", 1, 0, ARG_OVERHEAD },
92 { "linktype", 1, 0, ARG_LINKTYPE },
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_UPDATE_ONLY: flags = 0; break;
110 case ARG_MTU: nl_cli_tc_parse_mtu(tc, optarg); break;
111 case ARG_MPU: nl_cli_tc_parse_mpu(tc, optarg); break;
112 case ARG_OVERHEAD: nl_cli_tc_parse_overhead(tc, optarg); break;
113 case ARG_LINKTYPE: nl_cli_tc_parse_linktype(tc, optarg); break;
114 }
115 }
116
117 if (optind >= argc)
118 print_usage();
119
120 if (!rtnl_tc_get_ifindex(tc))
121 nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
122
123 if (!rtnl_tc_get_parent(tc))
124 nl_cli_fatal(EINVAL, "You must specify a parent (--parent=XXX)");
125
126 if (id) {
127 nl_cli_tc_parse_handle(tc, id, 1);
128 free(id);
129 }
130
131 kind = argv[optind++];
132 rtnl_tc_set_kind(tc, kind);
133
134 if (!(ops = rtnl_tc_get_ops(tc)))
135 nl_cli_fatal(ENOENT, "Unknown class \"%s\"", kind);
136
137 if (!(tm = nl_cli_tc_lookup(ops)))
138 nl_cli_fatal(ENOTSUP, "class type \"%s\" not supported.", kind);
139
140 tm->tm_parse_argv(tc, argc, argv);
141
142 if (!quiet) {
143 printf("Adding ");
144 nl_object_dump(OBJ_CAST(class), &dp);
145 }
146
147 if ((err = rtnl_class_add(sock, class, flags)) < 0)
148 nl_cli_fatal(EINVAL, "Unable to add class: %s", nl_geterror(err));
149
150 return 0;
151 }
152