summaryrefslogtreecommitdiff
path: root/lib/cli/qdisc/hfsc.c
blob: 9ab7f7e458b5e1783bc76274888fa1eebe0dec32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* SPDX-License-Identifier: LGPL-2.1-only */
/*
 * Copyright (c) 2014 Cong Wang <xiyou.wangcong@gmail.com>
 */

#include "nl-default.h"

#include <linux/pkt_sched.h>

#include <netlink/cli/utils.h>
#include <netlink/cli/tc.h>
#include <netlink/route/qdisc/hfsc.h>

static void print_qdisc_usage(void)
{
	printf(
"Usage: nl-qdisc-add [...] hfsc [OPTIONS]...\n"
"\n"
"OPTIONS\n"
"     --help                Show this help text.\n"
"     --default=ID          Default class for unclassified traffic.\n"
"\n"
"EXAMPLE"
"    # Create hfsc root qdisc 1: and direct unclassified traffic to class 1:10\n"
"    nl-qdisc-add --dev=eth1 --parent=root --handle=1: hfsc --default=10\n");
}

static void hfsc_parse_qdisc_argv(struct rtnl_tc *tc, int argc, char **argv)
{
	struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) tc;

	for (;;) {
		int c, optidx = 0;
		enum {
			ARG_DEFAULT = 257,
		};
		static struct option long_opts[] = {
			{ "help", 0, 0, 'h' },
			{ "default", 1, 0, ARG_DEFAULT },
			{ 0, 0, 0, 0 }
		};

		c = getopt_long(argc, argv, "hv", long_opts, &optidx);
		if (c == -1)
			break;

		switch (c) {
		case 'h':
			print_qdisc_usage();
			return;

		case ARG_DEFAULT:
			rtnl_qdisc_hfsc_set_defcls(qdisc, nl_cli_parse_u32(optarg));
			break;
		}
	}
}

static void print_class_usage(void)
{
	printf(
"Usage: nl-class-add [...] hfsc [OPTIONS]...\n"
"\n"
"OPTIONS\n"
"     --help                Show this help text.\n"
"     --ls=SC               Link-sharing service curve\n"
"     --rt=SC               Real-time service curve\n"
"     --sc=SC               Specifiy both of the above\n"
"     --ul=SC               Upper limit\n"
"     where SC := [ [ m1 bits ] d usec ] m2 bits\n"
"\n"
"EXAMPLE"
"    # Attach class 1:1 to hfsc qdisc 1: and use rt and ls curve\n"
"    nl-class-add --dev=eth1 --parent=1: --classid=1:1 hfsc --sc=m1:250,d:8,m2:100\n");
}

static int
hfsc_get_sc(char *optarg, struct tc_service_curve *sc)
{
	unsigned int m1 = 0, d = 0, m2 = 0;
	char *tmp = strdup(optarg);
	char *p, *endptr;
	char *pp = tmp;

	if (!tmp)
		return -ENOMEM;

	p = strstr(pp, "m1:");
	if (p) {
		char *q;
		p += 3;
		if (*p == 0)
			goto err;
		q = strchr(p, ',');
		if (!q)
			goto err;
		*q = 0;
		m1 = strtoul(p, &endptr, 10);
		if (endptr == p)
			goto err;
		pp = q + 1;
	}

	p = strstr(pp, "d:");
	if (p) {
		char *q;
		p += 2;
		if (*p == 0)
			goto err;
		q = strchr(p, ',');
		if (!q)
			goto err;
		*q = 0;
		d = strtoul(p, &endptr, 10);
		if (endptr == p)
			goto err;
		pp = q + 1;
	}

	p = strstr(pp, "m2:");
	if (p) {
		p += 3;
		if (*p == 0)
			goto err;
		m2 = strtoul(p, &endptr, 10);
		if (endptr == p)
			goto err;
	} else
		goto err;

	free(tmp);
	sc->m1 = m1;
	sc->d  = d;
	sc->m2 = m2;
	return 0;

err:
	free(tmp);
	return -EINVAL;
}

static void hfsc_parse_class_argv(struct rtnl_tc *tc, int argc, char **argv)
{
	struct rtnl_class *class = (struct rtnl_class *) tc;
	int arg_ok = 0, ret = -EINVAL;

	for (;;) {
		int c, optidx = 0;
		enum {
			ARG_RT = 257,
			ARG_LS = 258,
			ARG_SC,
			ARG_UL,
		};
		static struct option long_opts[] = {
			{ "help", 0, 0, 'h' },
			{ "rt", 1, 0, ARG_RT },
			{ "ls", 1, 0, ARG_LS },
			{ "sc", 1, 0, ARG_SC },
			{ "ul", 1, 0, ARG_UL },
			{ 0, 0, 0, 0 }
		};
		struct tc_service_curve tsc;

		c = getopt_long(argc, argv, "h", long_opts, &optidx);
		if (c == -1)
			break;

		switch (c) {
		case 'h':
			print_class_usage();
			return;

		case ARG_RT:
			ret = hfsc_get_sc(optarg, &tsc);
			if (ret < 0) {
				nl_cli_fatal(ret, "Unable to parse sc "
					"\"%s\": Invalid format.", optarg);
			}

			rtnl_class_hfsc_set_rsc(class, &tsc);
			arg_ok++;
			break;

		case ARG_LS:
			ret = hfsc_get_sc(optarg, &tsc);
			if (ret < 0) {
				nl_cli_fatal(ret, "Unable to parse sc "
					"\"%s\": Invalid format.", optarg);
			}

			rtnl_class_hfsc_set_fsc(class, &tsc);
			arg_ok++;
			break;

		case ARG_SC:
			ret = hfsc_get_sc(optarg, &tsc);
			if (ret < 0) {
				nl_cli_fatal(ret, "Unable to parse sc "
					"\"%s\": Invalid format.", optarg);
			}

			rtnl_class_hfsc_set_rsc(class, &tsc);
			rtnl_class_hfsc_set_fsc(class, &tsc);
			arg_ok++;
			break;

		case ARG_UL:
			ret = hfsc_get_sc(optarg, &tsc);
			if (ret < 0) {
				nl_cli_fatal(ret, "Unable to parse sc "
					"\"%s\": Invalid format.", optarg);
			}

			rtnl_class_hfsc_set_usc(class, &tsc);
			arg_ok++;
			break;
		}
	}

	if (!arg_ok)
		nl_cli_fatal(ret, "Invalid arguments");
}

static struct nl_cli_tc_module hfsc_qdisc_module =
{
	.tm_name		= "hfsc",
	.tm_type		= RTNL_TC_TYPE_QDISC,
	.tm_parse_argv		= hfsc_parse_qdisc_argv,
};

static struct nl_cli_tc_module hfsc_class_module =
{
	.tm_name		= "hfsc",
	.tm_type		= RTNL_TC_TYPE_CLASS,
	.tm_parse_argv		= hfsc_parse_class_argv,
};

static void _nl_init hfsc_init(void)
{
	nl_cli_tc_register(&hfsc_qdisc_module);
	nl_cli_tc_register(&hfsc_class_module);
}

static void _nl_exit hfsc_exit(void)
{
	nl_cli_tc_unregister(&hfsc_class_module);
	nl_cli_tc_unregister(&hfsc_qdisc_module);
}