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