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