qop_hfsc.c revision 1.1 1 1.1 thorpej /* $KAME: qop_hfsc.c,v 1.4 2000/10/18 09:15:19 kjc Exp $ */
2 1.1 thorpej /*
3 1.1 thorpej * Copyright (C) 1999-2000
4 1.1 thorpej * Sony Computer Science Laboratories, Inc. All rights reserved.
5 1.1 thorpej *
6 1.1 thorpej * Redistribution and use in source and binary forms, with or without
7 1.1 thorpej * modification, are permitted provided that the following conditions
8 1.1 thorpej * are met:
9 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
10 1.1 thorpej * notice, this list of conditions and the following disclaimer.
11 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
13 1.1 thorpej * documentation and/or other materials provided with the distribution.
14 1.1 thorpej *
15 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
16 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
19 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 thorpej * SUCH DAMAGE.
26 1.1 thorpej */
27 1.1 thorpej
28 1.1 thorpej #include <sys/param.h>
29 1.1 thorpej #include <sys/socket.h>
30 1.1 thorpej #include <sys/sockio.h>
31 1.1 thorpej #include <sys/ioctl.h>
32 1.1 thorpej #include <sys/fcntl.h>
33 1.1 thorpej #include <net/if.h>
34 1.1 thorpej #include <netinet/in.h>
35 1.1 thorpej #include <arpa/inet.h>
36 1.1 thorpej
37 1.1 thorpej #include <stdio.h>
38 1.1 thorpej #include <stdlib.h>
39 1.1 thorpej #include <unistd.h>
40 1.1 thorpej #include <stddef.h>
41 1.1 thorpej #include <string.h>
42 1.1 thorpej #include <ctype.h>
43 1.1 thorpej #include <errno.h>
44 1.1 thorpej #include <syslog.h>
45 1.1 thorpej #include <netdb.h>
46 1.1 thorpej
47 1.1 thorpej #include <altq/altq.h>
48 1.1 thorpej #include <altq/altq_hfsc.h>
49 1.1 thorpej #include "altq_qop.h"
50 1.1 thorpej #include "qop_hfsc.h"
51 1.1 thorpej
52 1.1 thorpej static int read_sc(int *argcp, char ***argvp,
53 1.1 thorpej int *type, u_int *m1, u_int *d, u_int *m2);
54 1.1 thorpej static int qop_hfsc_enable_hook(struct ifinfo *ifinfo);
55 1.1 thorpej static int qop_hfsc_delete_class_hook(struct classinfo *clinfo);
56 1.1 thorpej static int validate_sc(struct service_curve *sc);
57 1.1 thorpej
58 1.1 thorpej static void gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc);
59 1.1 thorpej static void gsc_sub_sc(struct gen_sc *gsc, struct service_curve *sc);
60 1.1 thorpej static int is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc);
61 1.1 thorpej static void gsc_destroy(struct gen_sc *gsc);
62 1.1 thorpej static struct segment *gsc_getentry(struct gen_sc *gsc, double x);
63 1.1 thorpej static int gsc_add_seg(struct gen_sc *gsc,
64 1.1 thorpej double x, double y, double d, double m);
65 1.1 thorpej static int gsc_sub_seg(struct gen_sc *gsc,
66 1.1 thorpej double x, double y, double d, double m);
67 1.1 thorpej static void gsc_compress(struct gen_sc *gsc);
68 1.1 thorpej static double sc_x2y(struct service_curve *sc, double x);
69 1.1 thorpej
70 1.1 thorpej static int hfsc_attach(struct ifinfo *ifinfo);
71 1.1 thorpej static int hfsc_detach(struct ifinfo *ifinfo);
72 1.1 thorpej static int hfsc_clear(struct ifinfo *ifinfo);
73 1.1 thorpej static int hfsc_enable(struct ifinfo *ifinfo);
74 1.1 thorpej static int hfsc_disable(struct ifinfo *ifinfo);
75 1.1 thorpej static int hfsc_add_class(struct classinfo *clinfo);
76 1.1 thorpej static int hfsc_modify_class(struct classinfo *clinfo, void *arg);
77 1.1 thorpej static int hfsc_delete_class(struct classinfo *clinfo);
78 1.1 thorpej static int hfsc_add_filter(struct fltrinfo *fltrinfo);
79 1.1 thorpej static int hfsc_delete_filter(struct fltrinfo *fltrinfo);
80 1.1 thorpej
81 1.1 thorpej #define HFSC_DEVICE "/dev/altq/hfsc"
82 1.1 thorpej
83 1.1 thorpej static int hfsc_fd = -1;
84 1.1 thorpej static int hfsc_refcount = 0;
85 1.1 thorpej
86 1.1 thorpej static struct qdisc_ops hfsc_qdisc = {
87 1.1 thorpej ALTQT_HFSC,
88 1.1 thorpej "hfsc",
89 1.1 thorpej hfsc_attach,
90 1.1 thorpej hfsc_detach,
91 1.1 thorpej hfsc_clear,
92 1.1 thorpej hfsc_enable,
93 1.1 thorpej hfsc_disable,
94 1.1 thorpej hfsc_add_class,
95 1.1 thorpej hfsc_modify_class,
96 1.1 thorpej hfsc_delete_class,
97 1.1 thorpej hfsc_add_filter,
98 1.1 thorpej hfsc_delete_filter,
99 1.1 thorpej };
100 1.1 thorpej
101 1.1 thorpej #define EQUAL(s1, s2) (strcmp((s1), (s2)) == 0)
102 1.1 thorpej
103 1.1 thorpej /*
104 1.1 thorpej * parser interface
105 1.1 thorpej */
106 1.1 thorpej int
107 1.1 thorpej hfsc_interface_parser(const char *ifname, int argc, char **argv)
108 1.1 thorpej {
109 1.1 thorpej u_int bandwidth = 100000000; /* 100Mbps */
110 1.1 thorpej u_int tbrsize = 0;
111 1.1 thorpej int flags = 0;
112 1.1 thorpej
113 1.1 thorpej /*
114 1.1 thorpej * process options
115 1.1 thorpej */
116 1.1 thorpej while (argc > 0) {
117 1.1 thorpej if (EQUAL(*argv, "bandwidth")) {
118 1.1 thorpej argc--; argv++;
119 1.1 thorpej if (argc > 0)
120 1.1 thorpej bandwidth = atobps(*argv);
121 1.1 thorpej } else if (EQUAL(*argv, "tbrsize")) {
122 1.1 thorpej argc--; argv++;
123 1.1 thorpej if (argc > 0)
124 1.1 thorpej tbrsize = atobytes(*argv);
125 1.1 thorpej } else if (EQUAL(*argv, "hfsc")) {
126 1.1 thorpej /* just skip */
127 1.1 thorpej } else {
128 1.1 thorpej LOG(LOG_ERR, 0, "Unknown keyword '%s'\n", argv);
129 1.1 thorpej return (0);
130 1.1 thorpej }
131 1.1 thorpej argc--; argv++;
132 1.1 thorpej }
133 1.1 thorpej
134 1.1 thorpej if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0)
135 1.1 thorpej return (0);
136 1.1 thorpej
137 1.1 thorpej if (qcmd_hfsc_add_if(ifname, bandwidth, flags) != 0)
138 1.1 thorpej return (0);
139 1.1 thorpej return (1);
140 1.1 thorpej }
141 1.1 thorpej
142 1.1 thorpej int
143 1.1 thorpej hfsc_class_parser(const char *ifname, const char *class_name,
144 1.1 thorpej const char *parent_name, int argc, char **argv)
145 1.1 thorpej {
146 1.1 thorpej u_int m1, d, m2, rm1, rd, rm2, fm1, fd, fm2;
147 1.1 thorpej int qlimit = 50;
148 1.1 thorpej int flags = 0, admission = 0;
149 1.1 thorpej int type = 0, error;
150 1.1 thorpej
151 1.1 thorpej rm1 = rd = rm2 = fm1 = fd = fm2 = 0;
152 1.1 thorpej while (argc > 0) {
153 1.1 thorpej if (*argv[0] == '[') {
154 1.1 thorpej if (read_sc(&argc, &argv, &type, &m1, &d, &m2) != 0) {
155 1.1 thorpej LOG(LOG_ERR, 0,
156 1.1 thorpej "Bad service curve in %s, line %d\n",
157 1.1 thorpej altqconfigfile, line_no);
158 1.1 thorpej return (0);
159 1.1 thorpej }
160 1.1 thorpej if (type & HFSC_REALTIMESC) {
161 1.1 thorpej rm1 = m1; rd = d; rm2 = m2;
162 1.1 thorpej }
163 1.1 thorpej if (type & HFSC_LINKSHARINGSC) {
164 1.1 thorpej fm1 = m1; fd = d; fm2 = m2;
165 1.1 thorpej }
166 1.1 thorpej } else if (EQUAL(*argv, "pshare")) {
167 1.1 thorpej argc--; argv++;
168 1.1 thorpej if (argc > 0) {
169 1.1 thorpej struct ifinfo *ifinfo;
170 1.1 thorpej u_int pshare;
171 1.1 thorpej
172 1.1 thorpej pshare = (u_int)strtoul(*argv, NULL, 0);
173 1.1 thorpej if ((ifinfo = ifname2ifinfo(ifname)) != NULL) {
174 1.1 thorpej fm2 = ifinfo->bandwidth / 100 * pshare;
175 1.1 thorpej type |= HFSC_LINKSHARINGSC;
176 1.1 thorpej }
177 1.1 thorpej }
178 1.1 thorpej } else if (EQUAL(*argv, "grate")) {
179 1.1 thorpej argc--; argv++;
180 1.1 thorpej if (argc > 0) {
181 1.1 thorpej rm2 = atobps(*argv);
182 1.1 thorpej type |= HFSC_REALTIMESC;
183 1.1 thorpej }
184 1.1 thorpej } else if (EQUAL(*argv, "qlimit")) {
185 1.1 thorpej argc--; argv++;
186 1.1 thorpej if (argc > 0)
187 1.1 thorpej qlimit = strtoul(*argv, NULL, 0);
188 1.1 thorpej } else if (EQUAL(*argv, "default")) {
189 1.1 thorpej flags |= HFCF_DEFAULTCLASS;
190 1.1 thorpej } else if (EQUAL(*argv, "admission")) {
191 1.1 thorpej argc--; argv++;
192 1.1 thorpej if (argc > 0) {
193 1.1 thorpej if (EQUAL(*argv, "guaranteed")
194 1.1 thorpej || EQUAL(*argv, "cntlload"))
195 1.1 thorpej admission = 1;
196 1.1 thorpej else if (EQUAL(*argv, "none")) {
197 1.1 thorpej /* nothing */
198 1.1 thorpej } else {
199 1.1 thorpej LOG(LOG_ERR, 0,
200 1.1 thorpej "unknown admission type - %s, line %d\n",
201 1.1 thorpej *argv, line_no);
202 1.1 thorpej return (0);
203 1.1 thorpej }
204 1.1 thorpej }
205 1.1 thorpej } else if (EQUAL(*argv, "red")) {
206 1.1 thorpej flags |= HFCF_RED;
207 1.1 thorpej } else if (EQUAL(*argv, "ecn")) {
208 1.1 thorpej flags |= HFCF_ECN;
209 1.1 thorpej } else if (EQUAL(*argv, "rio")) {
210 1.1 thorpej flags |= HFCF_RIO;
211 1.1 thorpej } else if (EQUAL(*argv, "cleardscp")) {
212 1.1 thorpej flags |= HFCF_CLEARDSCP;
213 1.1 thorpej } else {
214 1.1 thorpej LOG(LOG_ERR, 0,
215 1.1 thorpej "Unknown keyword '%s' in %s, line %d\n",
216 1.1 thorpej *argv, altqconfigfile, line_no);
217 1.1 thorpej return (0);
218 1.1 thorpej }
219 1.1 thorpej
220 1.1 thorpej argc--; argv++;
221 1.1 thorpej }
222 1.1 thorpej
223 1.1 thorpej if (type == 0) {
224 1.1 thorpej LOG(LOG_ERR, 0,
225 1.1 thorpej "hfsc: service curve not specified in %s, line %d\n",
226 1.1 thorpej altqconfigfile, line_no);
227 1.1 thorpej return (0);
228 1.1 thorpej }
229 1.1 thorpej
230 1.1 thorpej if ((flags & HFCF_ECN) && (flags & (HFCF_RED|HFCF_RIO)) == 0)
231 1.1 thorpej flags |= HFCF_RED;
232 1.1 thorpej
233 1.1 thorpej /*
234 1.1 thorpej * if the link-sharing service curve is diffrent from
235 1.1 thorpej * the real-time service curve, we first create a class with the
236 1.1 thorpej * smaller service curve and then modify the other service curve.
237 1.1 thorpej */
238 1.1 thorpej if (rm2 <= fm2) {
239 1.1 thorpej m1 = rm1; d = rd; m2 = rm2;
240 1.1 thorpej } else {
241 1.1 thorpej m1 = fm1; d = fd; m2 = fm2;
242 1.1 thorpej }
243 1.1 thorpej error = qcmd_hfsc_add_class(ifname, class_name, parent_name,
244 1.1 thorpej m1, d, m2, qlimit, flags);
245 1.1 thorpej
246 1.1 thorpej if (error == 0 && (rm1 != fm1 || rd != fd || rm2 != fm2)) {
247 1.1 thorpej if (rm2 <= fm2) {
248 1.1 thorpej m1 = fm1; d = fd; m2 = fm2; type = HFSC_LINKSHARINGSC;
249 1.1 thorpej } else {
250 1.1 thorpej m1 = rm1; d = rd; m2 = rm2; type = HFSC_REALTIMESC;
251 1.1 thorpej }
252 1.1 thorpej error = qcmd_hfsc_modify_class(ifname, class_name,
253 1.1 thorpej m1, d, m2, type);
254 1.1 thorpej }
255 1.1 thorpej
256 1.1 thorpej if (error == 0 && admission) {
257 1.1 thorpej /* this is a special class for rsvp */
258 1.1 thorpej struct ifinfo *ifinfo = ifname2ifinfo(ifname);
259 1.1 thorpej struct classinfo *clinfo = clname2clinfo(ifinfo, class_name);
260 1.1 thorpej
261 1.1 thorpej if (ifinfo->resv_class != NULL) {
262 1.1 thorpej LOG(LOG_ERR, 0,
263 1.1 thorpej "more than one admission class specified: %s\n",
264 1.1 thorpej class_name);
265 1.1 thorpej return (0);
266 1.1 thorpej }
267 1.1 thorpej ifinfo->resv_class = clinfo;
268 1.1 thorpej }
269 1.1 thorpej
270 1.1 thorpej if (error) {
271 1.1 thorpej LOG(LOG_ERR, errno, "hfsc_class_parser: %s\n",
272 1.1 thorpej qoperror(error));
273 1.1 thorpej return (0);
274 1.1 thorpej }
275 1.1 thorpej return (1);
276 1.1 thorpej }
277 1.1 thorpej
278 1.1 thorpej /*
279 1.1 thorpej * read service curve parameters
280 1.1 thorpej * '[' <type> <m1> <d> <m2> ']'
281 1.1 thorpej * type := "sc", "rt", or "ls"
282 1.1 thorpej */
283 1.1 thorpej static int
284 1.1 thorpej read_sc(int *argcp, char ***argvp, int *type, u_int *m1, u_int *d, u_int *m2)
285 1.1 thorpej {
286 1.1 thorpej int argc = *argcp;
287 1.1 thorpej char **argv = *argvp;
288 1.1 thorpej char *cp;
289 1.1 thorpej
290 1.1 thorpej cp = *argv;
291 1.1 thorpej if (*cp++ != '[')
292 1.1 thorpej return (-1);
293 1.1 thorpej if (*cp == '\0') {
294 1.1 thorpej cp = *++argv; --argc;
295 1.1 thorpej }
296 1.1 thorpej if (*cp == 's' || *cp == 'S')
297 1.1 thorpej *type = HFSC_DEFAULTSC;
298 1.1 thorpej else if (*cp == 'r' || *cp == 'R')
299 1.1 thorpej *type = HFSC_REALTIMESC;
300 1.1 thorpej else if (*cp == 'l' || *cp == 'L')
301 1.1 thorpej *type = HFSC_LINKSHARINGSC;
302 1.1 thorpej else
303 1.1 thorpej return (-1);
304 1.1 thorpej cp = *++argv; --argc;
305 1.1 thorpej *m1 = atobps(cp);
306 1.1 thorpej cp = *++argv; --argc;
307 1.1 thorpej *d = (u_int)strtoul(cp, NULL, 0);
308 1.1 thorpej cp = *++argv; --argc;
309 1.1 thorpej *m2 = atobps(cp);
310 1.1 thorpej if (strchr(cp, ']') == NULL) {
311 1.1 thorpej cp = *++argv; --argc;
312 1.1 thorpej if (*cp != ']')
313 1.1 thorpej return (-1);
314 1.1 thorpej }
315 1.1 thorpej *argcp = argc;
316 1.1 thorpej *argvp = argv;
317 1.1 thorpej return (0);
318 1.1 thorpej }
319 1.1 thorpej
320 1.1 thorpej /*
321 1.1 thorpej * qcmd api
322 1.1 thorpej */
323 1.1 thorpej int
324 1.1 thorpej qcmd_hfsc_add_if(const char *ifname, u_int bandwidth, int flags)
325 1.1 thorpej {
326 1.1 thorpej int error;
327 1.1 thorpej
328 1.1 thorpej error = qop_hfsc_add_if(NULL, ifname, bandwidth, flags);
329 1.1 thorpej if (error != 0)
330 1.1 thorpej LOG(LOG_ERR, errno, "%s: can't add hfsc on interface '%s'\n",
331 1.1 thorpej qoperror(error), ifname);
332 1.1 thorpej return (error);
333 1.1 thorpej }
334 1.1 thorpej
335 1.1 thorpej int
336 1.1 thorpej qcmd_hfsc_add_class(const char *ifname, const char *class_name,
337 1.1 thorpej const char *parent_name, u_int m1, u_int d, u_int m2,
338 1.1 thorpej int qlimit, int flags)
339 1.1 thorpej {
340 1.1 thorpej struct ifinfo *ifinfo;
341 1.1 thorpej struct classinfo *parent = NULL;
342 1.1 thorpej struct service_curve sc;
343 1.1 thorpej int error = 0;
344 1.1 thorpej
345 1.1 thorpej if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
346 1.1 thorpej error = QOPERR_BADIF;
347 1.1 thorpej
348 1.1 thorpej if (error == 0 &&
349 1.1 thorpej (parent = clname2clinfo(ifinfo, parent_name)) == NULL)
350 1.1 thorpej error = QOPERR_BADCLASS;
351 1.1 thorpej
352 1.1 thorpej sc.m1 = m1;
353 1.1 thorpej sc.d = d;
354 1.1 thorpej sc.m2 = m2;
355 1.1 thorpej
356 1.1 thorpej if (error == 0)
357 1.1 thorpej error = qop_hfsc_add_class(NULL, class_name, ifinfo, parent,
358 1.1 thorpej &sc, qlimit, flags);
359 1.1 thorpej if (error != 0)
360 1.1 thorpej LOG(LOG_ERR, errno,
361 1.1 thorpej "hfsc: %s: can't add class '%s' on interface '%s'\n",
362 1.1 thorpej qoperror(error), class_name, ifname);
363 1.1 thorpej return (error);
364 1.1 thorpej }
365 1.1 thorpej
366 1.1 thorpej int
367 1.1 thorpej qcmd_hfsc_modify_class(const char *ifname, const char *class_name,
368 1.1 thorpej u_int m1, u_int d, u_int m2, int sctype)
369 1.1 thorpej {
370 1.1 thorpej struct ifinfo *ifinfo;
371 1.1 thorpej struct classinfo *clinfo;
372 1.1 thorpej struct service_curve sc;
373 1.1 thorpej
374 1.1 thorpej if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
375 1.1 thorpej return (QOPERR_BADIF);
376 1.1 thorpej
377 1.1 thorpej if ((clinfo = clname2clinfo(ifinfo, class_name)) == NULL)
378 1.1 thorpej return (QOPERR_BADCLASS);
379 1.1 thorpej
380 1.1 thorpej sc.m1 = m1;
381 1.1 thorpej sc.d = d;
382 1.1 thorpej sc.m2 = m2;
383 1.1 thorpej
384 1.1 thorpej return qop_hfsc_modify_class(clinfo, &sc, sctype);
385 1.1 thorpej }
386 1.1 thorpej
387 1.1 thorpej /*
388 1.1 thorpej * qop api
389 1.1 thorpej */
390 1.1 thorpej int
391 1.1 thorpej qop_hfsc_add_if(struct ifinfo **rp, const char *ifname,
392 1.1 thorpej u_int bandwidth, int flags)
393 1.1 thorpej {
394 1.1 thorpej struct ifinfo *ifinfo = NULL;
395 1.1 thorpej struct hfsc_ifinfo *hfsc_ifinfo = NULL;
396 1.1 thorpej struct service_curve sc;
397 1.1 thorpej int error;
398 1.1 thorpej
399 1.1 thorpej if ((hfsc_ifinfo = calloc(1, sizeof(*hfsc_ifinfo))) == NULL)
400 1.1 thorpej return (QOPERR_NOMEM);
401 1.1 thorpej
402 1.1 thorpej error = qop_add_if(&ifinfo, ifname, bandwidth,
403 1.1 thorpej &hfsc_qdisc, hfsc_ifinfo);
404 1.1 thorpej if (error != 0)
405 1.1 thorpej goto err_ret;
406 1.1 thorpej
407 1.1 thorpej /* set enable hook */
408 1.1 thorpej ifinfo->enable_hook = qop_hfsc_enable_hook;
409 1.1 thorpej
410 1.1 thorpej /* create a dummy root class */
411 1.1 thorpej sc.m1 = bandwidth;
412 1.1 thorpej sc.d = 0;
413 1.1 thorpej sc.m2 = bandwidth;
414 1.1 thorpej if ((error = qop_hfsc_add_class(&hfsc_ifinfo->root_class, "root",
415 1.1 thorpej ifinfo, NULL, &sc, 0, 0)) != 0) {
416 1.1 thorpej LOG(LOG_ERR, errno,
417 1.1 thorpej "hfsc: %s: can't create dummy root class on %s!\n",
418 1.1 thorpej qoperror(error), ifname);
419 1.1 thorpej (void)qop_delete_if(ifinfo);
420 1.1 thorpej return (QOPERR_CLASS);
421 1.1 thorpej }
422 1.1 thorpej
423 1.1 thorpej if (rp != NULL)
424 1.1 thorpej *rp = ifinfo;
425 1.1 thorpej return (0);
426 1.1 thorpej
427 1.1 thorpej err_ret:
428 1.1 thorpej if (hfsc_ifinfo != NULL) {
429 1.1 thorpej free(hfsc_ifinfo);
430 1.1 thorpej if (ifinfo != NULL)
431 1.1 thorpej ifinfo->private = NULL;
432 1.1 thorpej }
433 1.1 thorpej return (error);
434 1.1 thorpej }
435 1.1 thorpej
436 1.1 thorpej #define is_sc_null(sc) (((sc) == NULL) || ((sc)->m1 == 0 && (sc)->m2 == 0))
437 1.1 thorpej
438 1.1 thorpej int
439 1.1 thorpej qop_hfsc_add_class(struct classinfo **rp, const char *class_name,
440 1.1 thorpej struct ifinfo *ifinfo, struct classinfo *parent,
441 1.1 thorpej struct service_curve *sc, int qlimit, int flags)
442 1.1 thorpej {
443 1.1 thorpej struct classinfo *clinfo;
444 1.1 thorpej struct hfsc_ifinfo *hfsc_ifinfo;
445 1.1 thorpej struct hfsc_classinfo *hfsc_clinfo = NULL, *parent_clinfo = NULL;
446 1.1 thorpej int error;
447 1.1 thorpej
448 1.1 thorpej hfsc_ifinfo = ifinfo->private;
449 1.1 thorpej if ((flags & HFCF_DEFAULTCLASS) && hfsc_ifinfo->default_class != NULL)
450 1.1 thorpej return (QOPERR_CLASS_INVAL);
451 1.1 thorpej
452 1.1 thorpej if (validate_sc(sc) != 0)
453 1.1 thorpej return (QOPERR_INVAL);
454 1.1 thorpej
455 1.1 thorpej /* admission control */
456 1.1 thorpej if (parent != NULL && !is_sc_null(sc)) {
457 1.1 thorpej parent_clinfo = parent->private;
458 1.1 thorpej gsc_add_sc(&parent_clinfo->gen_rsc, sc);
459 1.1 thorpej gsc_add_sc(&parent_clinfo->gen_fsc, sc);
460 1.1 thorpej if (!is_gsc_under_sc(&parent_clinfo->gen_rsc,
461 1.1 thorpej &parent_clinfo->rsc) ||
462 1.1 thorpej !is_gsc_under_sc(&parent_clinfo->gen_fsc,
463 1.1 thorpej &parent_clinfo->fsc)) {
464 1.1 thorpej /* admission control failure */
465 1.1 thorpej error = QOPERR_ADMISSION_NOBW;
466 1.1 thorpej goto err_ret;
467 1.1 thorpej }
468 1.1 thorpej }
469 1.1 thorpej
470 1.1 thorpej if ((hfsc_clinfo = calloc(1, sizeof(*hfsc_clinfo))) == NULL) {
471 1.1 thorpej error = QOPERR_NOMEM;
472 1.1 thorpej goto err_ret;
473 1.1 thorpej }
474 1.1 thorpej
475 1.1 thorpej hfsc_clinfo->rsc = *sc;
476 1.1 thorpej hfsc_clinfo->fsc = *sc;
477 1.1 thorpej LIST_INIT(&hfsc_clinfo->gen_rsc);
478 1.1 thorpej LIST_INIT(&hfsc_clinfo->gen_fsc);
479 1.1 thorpej hfsc_clinfo->qlimit = qlimit;
480 1.1 thorpej hfsc_clinfo->flags = flags;
481 1.1 thorpej
482 1.1 thorpej if ((error = qop_add_class(&clinfo, class_name, ifinfo, parent,
483 1.1 thorpej hfsc_clinfo)) != 0)
484 1.1 thorpej goto err_ret;
485 1.1 thorpej
486 1.1 thorpej /* set delete hook */
487 1.1 thorpej clinfo->delete_hook = qop_hfsc_delete_class_hook;
488 1.1 thorpej
489 1.1 thorpej if (flags & HFCF_DEFAULTCLASS)
490 1.1 thorpej hfsc_ifinfo->default_class = clinfo;
491 1.1 thorpej
492 1.1 thorpej if (parent == NULL) {
493 1.1 thorpej /*
494 1.1 thorpej * if this is a root class, reserve 20% of the real-time
495 1.1 thorpej * bandwidth for safety.
496 1.1 thorpej * many network cards are not able to saturate the wire,
497 1.1 thorpej * and if we allocate real-time traffic more than the
498 1.1 thorpej * maximum sending rate of the card, hfsc is no longer
499 1.1 thorpej * able to meet the delay bound requirements.
500 1.1 thorpej */
501 1.1 thorpej hfsc_clinfo->rsc.m1 = hfsc_clinfo->rsc.m1 / 10 * 8;
502 1.1 thorpej hfsc_clinfo->rsc.m2 = hfsc_clinfo->rsc.m2 / 10 * 8;
503 1.1 thorpej }
504 1.1 thorpej
505 1.1 thorpej if (rp != NULL)
506 1.1 thorpej *rp = clinfo;
507 1.1 thorpej return (0);
508 1.1 thorpej
509 1.1 thorpej err_ret:
510 1.1 thorpej /* cancel admission control */
511 1.1 thorpej if (parent != NULL && !is_sc_null(sc)) {
512 1.1 thorpej gsc_sub_sc(&parent_clinfo->gen_rsc, sc);
513 1.1 thorpej gsc_sub_sc(&parent_clinfo->gen_fsc, sc);
514 1.1 thorpej }
515 1.1 thorpej
516 1.1 thorpej if (hfsc_clinfo != NULL) {
517 1.1 thorpej free(hfsc_clinfo);
518 1.1 thorpej clinfo->private = NULL;
519 1.1 thorpej }
520 1.1 thorpej
521 1.1 thorpej return (error);
522 1.1 thorpej }
523 1.1 thorpej
524 1.1 thorpej /*
525 1.1 thorpej * this is called from qop_delete_class() before a class is destroyed
526 1.1 thorpej * for discipline specific cleanup.
527 1.1 thorpej */
528 1.1 thorpej static int
529 1.1 thorpej qop_hfsc_delete_class_hook(struct classinfo *clinfo)
530 1.1 thorpej {
531 1.1 thorpej struct hfsc_classinfo *hfsc_clinfo, *parent_clinfo;
532 1.1 thorpej
533 1.1 thorpej hfsc_clinfo = clinfo->private;
534 1.1 thorpej
535 1.1 thorpej /* cancel admission control */
536 1.1 thorpej if (clinfo->parent != NULL) {
537 1.1 thorpej parent_clinfo = clinfo->parent->private;
538 1.1 thorpej
539 1.1 thorpej gsc_sub_sc(&parent_clinfo->gen_rsc, &hfsc_clinfo->rsc);
540 1.1 thorpej gsc_sub_sc(&parent_clinfo->gen_fsc, &hfsc_clinfo->fsc);
541 1.1 thorpej }
542 1.1 thorpej
543 1.1 thorpej gsc_destroy(&hfsc_clinfo->gen_rsc);
544 1.1 thorpej gsc_destroy(&hfsc_clinfo->gen_fsc);
545 1.1 thorpej return (0);
546 1.1 thorpej }
547 1.1 thorpej
548 1.1 thorpej int
549 1.1 thorpej qop_hfsc_modify_class(struct classinfo *clinfo,
550 1.1 thorpej struct service_curve *sc, int sctype)
551 1.1 thorpej {
552 1.1 thorpej struct hfsc_classinfo *hfsc_clinfo, *parent_clinfo;
553 1.1 thorpej struct service_curve rsc, fsc;
554 1.1 thorpej int error;
555 1.1 thorpej
556 1.1 thorpej if (validate_sc(sc) != 0)
557 1.1 thorpej return (QOPERR_INVAL);
558 1.1 thorpej
559 1.1 thorpej hfsc_clinfo = clinfo->private;
560 1.1 thorpej if (clinfo->parent == NULL)
561 1.1 thorpej return (QOPERR_CLASS_INVAL);
562 1.1 thorpej parent_clinfo = clinfo->parent->private;
563 1.1 thorpej
564 1.1 thorpej /* save old service curves */
565 1.1 thorpej rsc = hfsc_clinfo->rsc;
566 1.1 thorpej fsc = hfsc_clinfo->fsc;
567 1.1 thorpej
568 1.1 thorpej /* admission control */
569 1.1 thorpej if (sctype & HFSC_REALTIMESC) {
570 1.1 thorpej if (!is_gsc_under_sc(&hfsc_clinfo->gen_rsc, sc)) {
571 1.1 thorpej /* admission control failure */
572 1.1 thorpej return (QOPERR_ADMISSION);
573 1.1 thorpej }
574 1.1 thorpej
575 1.1 thorpej gsc_sub_sc(&parent_clinfo->gen_rsc, &hfsc_clinfo->rsc);
576 1.1 thorpej gsc_add_sc(&parent_clinfo->gen_rsc, sc);
577 1.1 thorpej if (!is_gsc_under_sc(&parent_clinfo->gen_rsc,
578 1.1 thorpej &parent_clinfo->rsc)) {
579 1.1 thorpej /* admission control failure */
580 1.1 thorpej gsc_sub_sc(&parent_clinfo->gen_rsc, sc);
581 1.1 thorpej gsc_add_sc(&parent_clinfo->gen_rsc, &hfsc_clinfo->rsc);
582 1.1 thorpej return (QOPERR_ADMISSION_NOBW);
583 1.1 thorpej }
584 1.1 thorpej hfsc_clinfo->rsc = *sc;
585 1.1 thorpej }
586 1.1 thorpej if (sctype & HFSC_LINKSHARINGSC) {
587 1.1 thorpej if (!is_gsc_under_sc(&hfsc_clinfo->gen_fsc, sc)) {
588 1.1 thorpej /* admission control failure */
589 1.1 thorpej return (QOPERR_ADMISSION);
590 1.1 thorpej }
591 1.1 thorpej
592 1.1 thorpej gsc_sub_sc(&parent_clinfo->gen_fsc, &hfsc_clinfo->fsc);
593 1.1 thorpej gsc_add_sc(&parent_clinfo->gen_fsc, sc);
594 1.1 thorpej if (!is_gsc_under_sc(&parent_clinfo->gen_fsc,
595 1.1 thorpej &parent_clinfo->fsc)) {
596 1.1 thorpej /* admission control failure */
597 1.1 thorpej gsc_sub_sc(&parent_clinfo->gen_fsc, sc);
598 1.1 thorpej gsc_add_sc(&parent_clinfo->gen_fsc, &hfsc_clinfo->fsc);
599 1.1 thorpej return (QOPERR_ADMISSION_NOBW);
600 1.1 thorpej }
601 1.1 thorpej hfsc_clinfo->fsc = *sc;
602 1.1 thorpej }
603 1.1 thorpej
604 1.1 thorpej error = qop_modify_class(clinfo, (void *)((long)sctype));
605 1.1 thorpej if (error == 0)
606 1.1 thorpej return (0);
607 1.1 thorpej
608 1.1 thorpej /* modify failed!, restore the old service curves */
609 1.1 thorpej if (sctype & HFSC_REALTIMESC) {
610 1.1 thorpej gsc_sub_sc(&parent_clinfo->gen_rsc, sc);
611 1.1 thorpej gsc_add_sc(&parent_clinfo->gen_rsc, &rsc);
612 1.1 thorpej hfsc_clinfo->rsc = rsc;
613 1.1 thorpej }
614 1.1 thorpej if (sctype & HFSC_LINKSHARINGSC) {
615 1.1 thorpej gsc_sub_sc(&parent_clinfo->gen_fsc, sc);
616 1.1 thorpej gsc_add_sc(&parent_clinfo->gen_fsc, &fsc);
617 1.1 thorpej hfsc_clinfo->fsc = fsc;
618 1.1 thorpej }
619 1.1 thorpej return (error);
620 1.1 thorpej }
621 1.1 thorpej
622 1.1 thorpej /*
623 1.1 thorpej * sanity check at enabling hfsc:
624 1.1 thorpej * 1. there must one default class for an interface
625 1.1 thorpej * 2. the default class must be a leaf class
626 1.1 thorpej * 3. an internal class should not have filters
627 1.1 thorpej * (rule 2 and 3 are due to the fact that the hfsc link-sharing algorithm
628 1.1 thorpej * do not schedule internal classes.)
629 1.1 thorpej */
630 1.1 thorpej static int
631 1.1 thorpej qop_hfsc_enable_hook(struct ifinfo *ifinfo)
632 1.1 thorpej {
633 1.1 thorpej struct hfsc_ifinfo *hfsc_ifinfo;
634 1.1 thorpej struct classinfo *clinfo;
635 1.1 thorpej
636 1.1 thorpej hfsc_ifinfo = ifinfo->private;
637 1.1 thorpej if (hfsc_ifinfo->default_class == NULL) {
638 1.1 thorpej LOG(LOG_ERR, 0, "hfsc: no default class on interface %s!\n",
639 1.1 thorpej ifinfo->ifname);
640 1.1 thorpej return (QOPERR_CLASS);
641 1.1 thorpej } else if (hfsc_ifinfo->default_class->child != NULL) {
642 1.1 thorpej LOG(LOG_ERR, 0, "hfsc: default class on %s must be a leaf!\n",
643 1.1 thorpej ifinfo->ifname);
644 1.1 thorpej return (QOPERR_CLASS);
645 1.1 thorpej }
646 1.1 thorpej
647 1.1 thorpej LIST_FOREACH(clinfo, &ifinfo->cllist, next) {
648 1.1 thorpej if (clinfo->child != NULL && !LIST_EMPTY(&clinfo->fltrlist)) {
649 1.1 thorpej LOG(LOG_ERR, 0, "hfsc: internal class \"%s\" should not have a filter!\n",
650 1.1 thorpej clinfo->clname);
651 1.1 thorpej return (QOPERR_CLASS);
652 1.1 thorpej }
653 1.1 thorpej }
654 1.1 thorpej
655 1.1 thorpej return (0);
656 1.1 thorpej }
657 1.1 thorpej
658 1.1 thorpej static int
659 1.1 thorpej validate_sc(struct service_curve *sc)
660 1.1 thorpej {
661 1.1 thorpej /* the 1st segment of a concave curve must be zero */
662 1.1 thorpej if (sc->m1 < sc->m2 && sc->m1 != 0) {
663 1.1 thorpej LOG(LOG_ERR, 0, "m1 must be 0 for convex!\n");
664 1.1 thorpej return (-1);
665 1.1 thorpej }
666 1.1 thorpej return (0);
667 1.1 thorpej }
668 1.1 thorpej
669 1.1 thorpej /*
670 1.1 thorpej * admission control using generalized service curve
671 1.1 thorpej */
672 1.1 thorpej #define INFINITY 1e500 /* IEEE: positive infinity */
673 1.1 thorpej
674 1.1 thorpej /* add a new service curve to a generilized service curve */
675 1.1 thorpej static void
676 1.1 thorpej gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
677 1.1 thorpej {
678 1.1 thorpej if (is_sc_null(sc))
679 1.1 thorpej return;
680 1.1 thorpej if (sc->d != 0)
681 1.1 thorpej gsc_add_seg(gsc, 0, 0, (double)sc->d, (double)sc->m1);
682 1.1 thorpej gsc_add_seg(gsc, (double)sc->d, 0, INFINITY, (double)sc->m2);
683 1.1 thorpej }
684 1.1 thorpej
685 1.1 thorpej /* subtract a service curve from a generilized service curve */
686 1.1 thorpej static void
687 1.1 thorpej gsc_sub_sc(struct gen_sc *gsc, struct service_curve *sc)
688 1.1 thorpej {
689 1.1 thorpej if (is_sc_null(sc))
690 1.1 thorpej return;
691 1.1 thorpej if (sc->d != 0)
692 1.1 thorpej gsc_sub_seg(gsc, 0, 0, (double)sc->d, (double)sc->m1);
693 1.1 thorpej gsc_sub_seg(gsc, (double)sc->d, 0, INFINITY, (double)sc->m2);
694 1.1 thorpej }
695 1.1 thorpej
696 1.1 thorpej /*
697 1.1 thorpej * check whether all points of a generalized service curve have
698 1.1 thorpej * their y-coordinates no larger than a given two-piece linear
699 1.1 thorpej * service curve.
700 1.1 thorpej */
701 1.1 thorpej static int
702 1.1 thorpej is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
703 1.1 thorpej {
704 1.1 thorpej struct segment *s, *last, *end;
705 1.1 thorpej double y;
706 1.1 thorpej
707 1.1 thorpej if (is_sc_null(sc)) {
708 1.1 thorpej if (LIST_EMPTY(gsc))
709 1.1 thorpej return (1);
710 1.1 thorpej LIST_FOREACH(s, gsc, _next) {
711 1.1 thorpej if (s->m != 0)
712 1.1 thorpej return (0);
713 1.1 thorpej }
714 1.1 thorpej return (1);
715 1.1 thorpej }
716 1.1 thorpej /*
717 1.1 thorpej * gsc has a dummy entry at the end with x = INFINITY.
718 1.1 thorpej * loop through up to this dummy entry.
719 1.1 thorpej */
720 1.1 thorpej end = gsc_getentry(gsc, INFINITY);
721 1.1 thorpej if (end == NULL)
722 1.1 thorpej return (1);
723 1.1 thorpej last = NULL;
724 1.1 thorpej for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
725 1.1 thorpej if (s->y > sc_x2y(sc, s->x))
726 1.1 thorpej return (0);
727 1.1 thorpej last = s;
728 1.1 thorpej }
729 1.1 thorpej /* last now holds the real last segment */
730 1.1 thorpej if (last == NULL)
731 1.1 thorpej return (1);
732 1.1 thorpej if (last->m > sc->m2)
733 1.1 thorpej return (0);
734 1.1 thorpej if (last->x < sc->d && last->m > sc->m1) {
735 1.1 thorpej y = last->y + (sc->d - last->x) * last->m;
736 1.1 thorpej if (y > sc_x2y(sc, sc->d))
737 1.1 thorpej return (0);
738 1.1 thorpej }
739 1.1 thorpej return (1);
740 1.1 thorpej }
741 1.1 thorpej
742 1.1 thorpej static void
743 1.1 thorpej gsc_destroy(struct gen_sc *gsc)
744 1.1 thorpej {
745 1.1 thorpej struct segment *s;
746 1.1 thorpej
747 1.1 thorpej while ((s = LIST_FIRST(gsc)) != NULL) {
748 1.1 thorpej LIST_REMOVE(s, _next);
749 1.1 thorpej free(s);
750 1.1 thorpej }
751 1.1 thorpej }
752 1.1 thorpej
753 1.1 thorpej /*
754 1.1 thorpej * return a segment entry starting at x.
755 1.1 thorpej * if gsc has no entry starting at x, a new entry is created at x.
756 1.1 thorpej */
757 1.1 thorpej static struct segment *
758 1.1 thorpej gsc_getentry(struct gen_sc *gsc, double x)
759 1.1 thorpej {
760 1.1 thorpej struct segment *new, *prev, *s;
761 1.1 thorpej
762 1.1 thorpej prev = NULL;
763 1.1 thorpej LIST_FOREACH(s, gsc, _next) {
764 1.1 thorpej if (s->x == x)
765 1.1 thorpej return (s); /* matching entry found */
766 1.1 thorpej else if (s->x < x)
767 1.1 thorpej prev = s;
768 1.1 thorpej else
769 1.1 thorpej break;
770 1.1 thorpej }
771 1.1 thorpej
772 1.1 thorpej /* we have to create a new entry */
773 1.1 thorpej if ((new = calloc(1, sizeof(struct segment))) == NULL)
774 1.1 thorpej return (NULL);
775 1.1 thorpej
776 1.1 thorpej new->x = x;
777 1.1 thorpej if (x == INFINITY || s == NULL)
778 1.1 thorpej new->d = 0;
779 1.1 thorpej else if (s->x == INFINITY)
780 1.1 thorpej new->d = INFINITY;
781 1.1 thorpej else
782 1.1 thorpej new->d = s->x - x;
783 1.1 thorpej if (prev == NULL) {
784 1.1 thorpej /* insert the new entry at the head of the list */
785 1.1 thorpej new->y = 0;
786 1.1 thorpej new->m = 0;
787 1.1 thorpej LIST_INSERT_HEAD(gsc, new, _next);
788 1.1 thorpej } else {
789 1.1 thorpej /*
790 1.1 thorpej * the start point intersects with the segment pointed by
791 1.1 thorpej * prev. divide prev into 2 segments
792 1.1 thorpej */
793 1.1 thorpej if (x == INFINITY) {
794 1.1 thorpej prev->d = INFINITY;
795 1.1 thorpej if (prev->m == 0)
796 1.1 thorpej new->y = prev->y;
797 1.1 thorpej else
798 1.1 thorpej new->y = INFINITY;
799 1.1 thorpej } else {
800 1.1 thorpej prev->d = x - prev->x;
801 1.1 thorpej new->y = prev->d * prev->m + prev->y;
802 1.1 thorpej }
803 1.1 thorpej new->m = prev->m;
804 1.1 thorpej LIST_INSERT_AFTER(prev, new, _next);
805 1.1 thorpej }
806 1.1 thorpej return (new);
807 1.1 thorpej }
808 1.1 thorpej
809 1.1 thorpej /* add a segment to a generalized service curve */
810 1.1 thorpej static int
811 1.1 thorpej gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
812 1.1 thorpej {
813 1.1 thorpej struct segment *start, *end, *s;
814 1.1 thorpej double x2;
815 1.1 thorpej
816 1.1 thorpej if (d == INFINITY)
817 1.1 thorpej x2 = INFINITY;
818 1.1 thorpej else
819 1.1 thorpej x2 = x + d;
820 1.1 thorpej start = gsc_getentry(gsc, x);
821 1.1 thorpej end = gsc_getentry(gsc, x2);
822 1.1 thorpej if (start == NULL || end == NULL)
823 1.1 thorpej return (-1);
824 1.1 thorpej
825 1.1 thorpej for (s = start; s != end; s = LIST_NEXT(s, _next)) {
826 1.1 thorpej s->m += m;
827 1.1 thorpej s->y += y + (s->x - x) * m;
828 1.1 thorpej }
829 1.1 thorpej
830 1.1 thorpej end = gsc_getentry(gsc, INFINITY);
831 1.1 thorpej for (; s != end; s = LIST_NEXT(s, _next)) {
832 1.1 thorpej s->y += m * d;
833 1.1 thorpej }
834 1.1 thorpej
835 1.1 thorpej return (0);
836 1.1 thorpej }
837 1.1 thorpej
838 1.1 thorpej /* subtract a segment from a generalized service curve */
839 1.1 thorpej static int
840 1.1 thorpej gsc_sub_seg(struct gen_sc *gsc, double x, double y, double d, double m)
841 1.1 thorpej {
842 1.1 thorpej if (gsc_add_seg(gsc, x, y, d, -m) < 0)
843 1.1 thorpej return (-1);
844 1.1 thorpej gsc_compress(gsc);
845 1.1 thorpej return (0);
846 1.1 thorpej }
847 1.1 thorpej
848 1.1 thorpej /*
849 1.1 thorpej * collapse adjacent segments with the same slope
850 1.1 thorpej */
851 1.1 thorpej static void
852 1.1 thorpej gsc_compress(struct gen_sc *gsc)
853 1.1 thorpej {
854 1.1 thorpej struct segment *s, *next;
855 1.1 thorpej
856 1.1 thorpej again:
857 1.1 thorpej LIST_FOREACH(s, gsc, _next) {
858 1.1 thorpej
859 1.1 thorpej if ((next = LIST_NEXT(s, _next)) == NULL) {
860 1.1 thorpej if (LIST_FIRST(gsc) == s && s->m == 0) {
861 1.1 thorpej /*
862 1.1 thorpej * if this is the only entry and its
863 1.1 thorpej * slope is 0, it's a remaining dummy
864 1.1 thorpej * entry. we can discard it.
865 1.1 thorpej */
866 1.1 thorpej LIST_REMOVE(s, _next);
867 1.1 thorpej free(s);
868 1.1 thorpej }
869 1.1 thorpej break;
870 1.1 thorpej }
871 1.1 thorpej
872 1.1 thorpej if (s->x == next->x) {
873 1.1 thorpej /* discard this entry */
874 1.1 thorpej LIST_REMOVE(s, _next);
875 1.1 thorpej free(s);
876 1.1 thorpej goto again;
877 1.1 thorpej } else if (s->m == next->m) {
878 1.1 thorpej /* join the two entries */
879 1.1 thorpej if (s->d != INFINITY && next->d != INFINITY)
880 1.1 thorpej s->d += next->d;
881 1.1 thorpej LIST_REMOVE(next, _next);
882 1.1 thorpej free(next);
883 1.1 thorpej goto again;
884 1.1 thorpej }
885 1.1 thorpej }
886 1.1 thorpej }
887 1.1 thorpej
888 1.1 thorpej /* get y-projection of a service curve */
889 1.1 thorpej static double
890 1.1 thorpej sc_x2y(struct service_curve *sc, double x)
891 1.1 thorpej {
892 1.1 thorpej double y;
893 1.1 thorpej
894 1.1 thorpej if (x <= (double)sc->d)
895 1.1 thorpej /* y belongs to the 1st segment */
896 1.1 thorpej y = x * (double)sc->m1;
897 1.1 thorpej else
898 1.1 thorpej /* y belongs to the 2nd segment */
899 1.1 thorpej y = (double)sc->d * (double)sc->m1
900 1.1 thorpej + (x - (double)sc->d) * (double)sc->m2;
901 1.1 thorpej return (y);
902 1.1 thorpej }
903 1.1 thorpej
904 1.1 thorpej /*
905 1.1 thorpej * system call interfaces for qdisc_ops
906 1.1 thorpej */
907 1.1 thorpej static int
908 1.1 thorpej hfsc_attach(struct ifinfo *ifinfo)
909 1.1 thorpej {
910 1.1 thorpej struct hfsc_attach attach;
911 1.1 thorpej
912 1.1 thorpej if (hfsc_fd < 0 &&
913 1.1 thorpej (hfsc_fd = open(HFSC_DEVICE, O_RDWR)) < 0 &&
914 1.1 thorpej (hfsc_fd = open_module(HFSC_DEVICE, O_RDWR)) < 0) {
915 1.1 thorpej LOG(LOG_ERR, errno, "HFSC open\n");
916 1.1 thorpej return (QOPERR_SYSCALL);
917 1.1 thorpej }
918 1.1 thorpej
919 1.1 thorpej hfsc_refcount++;
920 1.1 thorpej memset(&attach, 0, sizeof(attach));
921 1.1 thorpej strncpy(attach.iface.hfsc_ifname, ifinfo->ifname, IFNAMSIZ);
922 1.1 thorpej attach.bandwidth = ifinfo->bandwidth;
923 1.1 thorpej
924 1.1 thorpej if (ioctl(hfsc_fd, HFSC_IF_ATTACH, &attach) < 0)
925 1.1 thorpej return (QOPERR_SYSCALL);
926 1.1 thorpej return (0);
927 1.1 thorpej }
928 1.1 thorpej
929 1.1 thorpej static int
930 1.1 thorpej hfsc_detach(struct ifinfo *ifinfo)
931 1.1 thorpej {
932 1.1 thorpej struct hfsc_interface iface;
933 1.1 thorpej
934 1.1 thorpej memset(&iface, 0, sizeof(iface));
935 1.1 thorpej strncpy(iface.hfsc_ifname, ifinfo->ifname, IFNAMSIZ);
936 1.1 thorpej
937 1.1 thorpej if (ioctl(hfsc_fd, HFSC_IF_DETACH, &iface) < 0)
938 1.1 thorpej return (QOPERR_SYSCALL);
939 1.1 thorpej
940 1.1 thorpej if (--hfsc_refcount == 0) {
941 1.1 thorpej close(hfsc_fd);
942 1.1 thorpej hfsc_fd = -1;
943 1.1 thorpej }
944 1.1 thorpej return (0);
945 1.1 thorpej }
946 1.1 thorpej
947 1.1 thorpej static int
948 1.1 thorpej hfsc_clear(struct ifinfo *ifinfo)
949 1.1 thorpej {
950 1.1 thorpej struct hfsc_interface iface;
951 1.1 thorpej
952 1.1 thorpej memset(&iface, 0, sizeof(iface));
953 1.1 thorpej strncpy(iface.hfsc_ifname, ifinfo->ifname, IFNAMSIZ);
954 1.1 thorpej
955 1.1 thorpej if (ioctl(hfsc_fd, HFSC_CLEAR_HIERARCHY, &iface) < 0)
956 1.1 thorpej return (QOPERR_SYSCALL);
957 1.1 thorpej return (0);
958 1.1 thorpej }
959 1.1 thorpej
960 1.1 thorpej static int
961 1.1 thorpej hfsc_enable(struct ifinfo *ifinfo)
962 1.1 thorpej {
963 1.1 thorpej struct hfsc_interface iface;
964 1.1 thorpej
965 1.1 thorpej memset(&iface, 0, sizeof(iface));
966 1.1 thorpej strncpy(iface.hfsc_ifname, ifinfo->ifname, IFNAMSIZ);
967 1.1 thorpej
968 1.1 thorpej if (ioctl(hfsc_fd, HFSC_ENABLE, &iface) < 0)
969 1.1 thorpej return (QOPERR_SYSCALL);
970 1.1 thorpej return (0);
971 1.1 thorpej }
972 1.1 thorpej
973 1.1 thorpej static int
974 1.1 thorpej hfsc_disable(struct ifinfo *ifinfo)
975 1.1 thorpej {
976 1.1 thorpej struct hfsc_interface iface;
977 1.1 thorpej
978 1.1 thorpej memset(&iface, 0, sizeof(iface));
979 1.1 thorpej strncpy(iface.hfsc_ifname, ifinfo->ifname, IFNAMSIZ);
980 1.1 thorpej
981 1.1 thorpej if (ioctl(hfsc_fd, HFSC_DISABLE, &iface) < 0)
982 1.1 thorpej return (QOPERR_SYSCALL);
983 1.1 thorpej return (0);
984 1.1 thorpej }
985 1.1 thorpej
986 1.1 thorpej static int
987 1.1 thorpej hfsc_add_class(struct classinfo *clinfo)
988 1.1 thorpej {
989 1.1 thorpej struct hfsc_add_class class_add;
990 1.1 thorpej struct hfsc_classinfo *hfsc_clinfo;
991 1.1 thorpej struct hfsc_ifinfo *hfsc_ifinfo;
992 1.1 thorpej
993 1.1 thorpej /* root class is a dummy class */
994 1.1 thorpej if (clinfo->parent == NULL) {
995 1.1 thorpej clinfo->handle = HFSC_ROOTCLASS_HANDLE;
996 1.1 thorpej return (0);
997 1.1 thorpej }
998 1.1 thorpej
999 1.1 thorpej hfsc_ifinfo = clinfo->ifinfo->private;
1000 1.1 thorpej hfsc_clinfo = clinfo->private;
1001 1.1 thorpej
1002 1.1 thorpej memset(&class_add, 0, sizeof(class_add));
1003 1.1 thorpej strncpy(class_add.iface.hfsc_ifname, clinfo->ifinfo->ifname, IFNAMSIZ);
1004 1.1 thorpej if (clinfo->parent == hfsc_ifinfo->root_class)
1005 1.1 thorpej class_add.parent_handle = HFSC_ROOTCLASS_HANDLE;
1006 1.1 thorpej else
1007 1.1 thorpej class_add.parent_handle = clinfo->parent->handle;
1008 1.1 thorpej class_add.service_curve = hfsc_clinfo->rsc;
1009 1.1 thorpej class_add.qlimit = hfsc_clinfo->qlimit;
1010 1.1 thorpej class_add.flags = hfsc_clinfo->flags;
1011 1.1 thorpej if (ioctl(hfsc_fd, HFSC_ADD_CLASS, &class_add) < 0) {
1012 1.1 thorpej clinfo->handle = HFSC_NULLCLASS_HANDLE;
1013 1.1 thorpej return (QOPERR_SYSCALL);
1014 1.1 thorpej }
1015 1.1 thorpej clinfo->handle = class_add.class_handle;
1016 1.1 thorpej return (0);
1017 1.1 thorpej }
1018 1.1 thorpej
1019 1.1 thorpej static int
1020 1.1 thorpej hfsc_modify_class(struct classinfo *clinfo, void *arg)
1021 1.1 thorpej {
1022 1.1 thorpej struct hfsc_modify_class class_mod;
1023 1.1 thorpej struct hfsc_classinfo *hfsc_clinfo;
1024 1.1 thorpej long sctype;
1025 1.1 thorpej
1026 1.1 thorpej sctype = (long)arg;
1027 1.1 thorpej hfsc_clinfo = clinfo->private;
1028 1.1 thorpej
1029 1.1 thorpej memset(&class_mod, 0, sizeof(class_mod));
1030 1.1 thorpej strncpy(class_mod.iface.hfsc_ifname, clinfo->ifinfo->ifname, IFNAMSIZ);
1031 1.1 thorpej class_mod.class_handle = clinfo->handle;
1032 1.1 thorpej if (sctype & HFSC_REALTIMESC)
1033 1.1 thorpej class_mod.service_curve = hfsc_clinfo->rsc;
1034 1.1 thorpej else if (sctype & HFSC_LINKSHARINGSC)
1035 1.1 thorpej class_mod.service_curve = hfsc_clinfo->fsc;
1036 1.1 thorpej else
1037 1.1 thorpej return (QOPERR_INVAL);
1038 1.1 thorpej class_mod.sctype = sctype;
1039 1.1 thorpej
1040 1.1 thorpej if (ioctl(hfsc_fd, HFSC_MOD_CLASS, &class_mod) < 0)
1041 1.1 thorpej return (QOPERR_SYSCALL);
1042 1.1 thorpej return (0);
1043 1.1 thorpej }
1044 1.1 thorpej
1045 1.1 thorpej static int
1046 1.1 thorpej hfsc_delete_class(struct classinfo *clinfo)
1047 1.1 thorpej {
1048 1.1 thorpej struct hfsc_delete_class class_delete;
1049 1.1 thorpej
1050 1.1 thorpej if (clinfo->handle == HFSC_NULLCLASS_HANDLE ||
1051 1.1 thorpej clinfo->handle == HFSC_ROOTCLASS_HANDLE)
1052 1.1 thorpej return (0);
1053 1.1 thorpej
1054 1.1 thorpej memset(&class_delete, 0, sizeof(class_delete));
1055 1.1 thorpej strncpy(class_delete.iface.hfsc_ifname, clinfo->ifinfo->ifname,
1056 1.1 thorpej IFNAMSIZ);
1057 1.1 thorpej class_delete.class_handle = clinfo->handle;
1058 1.1 thorpej
1059 1.1 thorpej if (ioctl(hfsc_fd, HFSC_DEL_CLASS, &class_delete) < 0)
1060 1.1 thorpej return (QOPERR_SYSCALL);
1061 1.1 thorpej return (0);
1062 1.1 thorpej }
1063 1.1 thorpej
1064 1.1 thorpej static int
1065 1.1 thorpej hfsc_add_filter(struct fltrinfo *fltrinfo)
1066 1.1 thorpej {
1067 1.1 thorpej struct hfsc_add_filter fltr_add;
1068 1.1 thorpej
1069 1.1 thorpej memset(&fltr_add, 0, sizeof(fltr_add));
1070 1.1 thorpej strncpy(fltr_add.iface.hfsc_ifname, fltrinfo->clinfo->ifinfo->ifname,
1071 1.1 thorpej IFNAMSIZ);
1072 1.1 thorpej fltr_add.class_handle = fltrinfo->clinfo->handle;
1073 1.1 thorpej fltr_add.filter = fltrinfo->fltr;
1074 1.1 thorpej
1075 1.1 thorpej if (ioctl(hfsc_fd, HFSC_ADD_FILTER, &fltr_add) < 0)
1076 1.1 thorpej return (QOPERR_SYSCALL);
1077 1.1 thorpej fltrinfo->handle = fltr_add.filter_handle;
1078 1.1 thorpej return (0);
1079 1.1 thorpej }
1080 1.1 thorpej
1081 1.1 thorpej static int
1082 1.1 thorpej hfsc_delete_filter(struct fltrinfo *fltrinfo)
1083 1.1 thorpej {
1084 1.1 thorpej struct hfsc_delete_filter fltr_del;
1085 1.1 thorpej
1086 1.1 thorpej memset(&fltr_del, 0, sizeof(fltr_del));
1087 1.1 thorpej strncpy(fltr_del.iface.hfsc_ifname, fltrinfo->clinfo->ifinfo->ifname,
1088 1.1 thorpej IFNAMSIZ);
1089 1.1 thorpej fltr_del.filter_handle = fltrinfo->handle;
1090 1.1 thorpej
1091 1.1 thorpej if (ioctl(hfsc_fd, HFSC_DEL_FILTER, &fltr_del) < 0)
1092 1.1 thorpej return (QOPERR_SYSCALL);
1093 1.1 thorpej return (0);
1094 1.1 thorpej }
1095 1.1 thorpej
1096 1.1 thorpej
1097