Home | History | Annotate | Line # | Download | only in libaltq
qop_hfsc.c revision 1.6
      1  1.6   itojun /*	$NetBSD: qop_hfsc.c,v 1.6 2002/09/08 09:28:23 itojun Exp $	*/
      2  1.6   itojun /*	$KAME: qop_hfsc.c,v 1.8 2002/09/08 09:08:13 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.6   itojun #include <math.h>
     48  1.1  thorpej 
     49  1.1  thorpej #include <altq/altq.h>
     50  1.1  thorpej #include <altq/altq_hfsc.h>
     51  1.1  thorpej #include "altq_qop.h"
     52  1.1  thorpej #include "qop_hfsc.h"
     53  1.1  thorpej 
     54  1.2   itojun static int read_sc(int *, char ***, int *, u_int *, u_int *, u_int *);
     55  1.2   itojun static int qop_hfsc_enable_hook(struct ifinfo *);
     56  1.2   itojun static int qop_hfsc_delete_class_hook(struct classinfo *);
     57  1.2   itojun static int validate_sc(struct service_curve *);
     58  1.1  thorpej 
     59  1.2   itojun static void gsc_add_sc(struct gen_sc *, struct service_curve *);
     60  1.2   itojun static void gsc_sub_sc(struct gen_sc *, struct service_curve *);
     61  1.2   itojun static int is_gsc_under_sc(struct gen_sc *, struct service_curve *);
     62  1.2   itojun static void gsc_destroy(struct gen_sc *);
     63  1.2   itojun static struct segment *gsc_getentry(struct gen_sc *, double);
     64  1.2   itojun static int gsc_add_seg(struct gen_sc *, double, double, double, double);
     65  1.2   itojun static int gsc_sub_seg(struct gen_sc *, double, double, double, double);
     66  1.2   itojun static void gsc_compress(struct gen_sc *);
     67  1.2   itojun static double sc_x2y(struct service_curve *, double);
     68  1.1  thorpej 
     69  1.2   itojun static int hfsc_attach(struct ifinfo *);
     70  1.2   itojun static int hfsc_detach(struct ifinfo *);
     71  1.2   itojun static int hfsc_clear(struct ifinfo *);
     72  1.2   itojun static int hfsc_enable(struct ifinfo *);
     73  1.2   itojun static int hfsc_disable(struct ifinfo *);
     74  1.2   itojun static int hfsc_add_class(struct classinfo *);
     75  1.2   itojun static int hfsc_modify_class(struct classinfo *, void *);
     76  1.2   itojun static int hfsc_delete_class(struct classinfo *);
     77  1.2   itojun static int hfsc_add_filter(struct fltrinfo *);
     78  1.2   itojun static int hfsc_delete_filter(struct fltrinfo *);
     79  1.1  thorpej 
     80  1.1  thorpej #define HFSC_DEVICE	"/dev/altq/hfsc"
     81  1.1  thorpej 
     82  1.1  thorpej static int hfsc_fd = -1;
     83  1.1  thorpej static int hfsc_refcount = 0;
     84  1.1  thorpej 
     85  1.1  thorpej static struct qdisc_ops hfsc_qdisc = {
     86  1.1  thorpej 	ALTQT_HFSC,
     87  1.1  thorpej 	"hfsc",
     88  1.1  thorpej 	hfsc_attach,
     89  1.1  thorpej 	hfsc_detach,
     90  1.1  thorpej 	hfsc_clear,
     91  1.1  thorpej 	hfsc_enable,
     92  1.1  thorpej 	hfsc_disable,
     93  1.1  thorpej 	hfsc_add_class,
     94  1.1  thorpej 	hfsc_modify_class,
     95  1.1  thorpej 	hfsc_delete_class,
     96  1.1  thorpej 	hfsc_add_filter,
     97  1.1  thorpej 	hfsc_delete_filter,
     98  1.1  thorpej };
     99  1.1  thorpej 
    100  1.1  thorpej #define EQUAL(s1, s2)	(strcmp((s1), (s2)) == 0)
    101  1.1  thorpej 
    102  1.1  thorpej /*
    103  1.1  thorpej  * parser interface
    104  1.1  thorpej  */
    105  1.1  thorpej int
    106  1.1  thorpej hfsc_interface_parser(const char *ifname, int argc, char **argv)
    107  1.1  thorpej {
    108  1.1  thorpej 	u_int  	bandwidth = 100000000;	/* 100Mbps */
    109  1.1  thorpej 	u_int	tbrsize = 0;
    110  1.1  thorpej 	int	flags = 0;
    111  1.1  thorpej 
    112  1.1  thorpej 	/*
    113  1.1  thorpej 	 * process options
    114  1.1  thorpej 	 */
    115  1.1  thorpej 	while (argc > 0) {
    116  1.1  thorpej 		if (EQUAL(*argv, "bandwidth")) {
    117  1.1  thorpej 			argc--; argv++;
    118  1.1  thorpej 			if (argc > 0)
    119  1.1  thorpej 				bandwidth = atobps(*argv);
    120  1.1  thorpej 		} else if (EQUAL(*argv, "tbrsize")) {
    121  1.1  thorpej 			argc--; argv++;
    122  1.1  thorpej 			if (argc > 0)
    123  1.1  thorpej 				tbrsize = atobytes(*argv);
    124  1.1  thorpej 		} else if (EQUAL(*argv, "hfsc")) {
    125  1.1  thorpej 			/* just skip */
    126  1.1  thorpej 		} else {
    127  1.5   itojun 			LOG(LOG_ERR, 0, "Unknown keyword '%s'", *argv);
    128  1.1  thorpej 			return (0);
    129  1.1  thorpej 		}
    130  1.1  thorpej 		argc--; argv++;
    131  1.1  thorpej 	}
    132  1.1  thorpej 
    133  1.1  thorpej 	if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0)
    134  1.1  thorpej 		return (0);
    135  1.1  thorpej 
    136  1.1  thorpej 	if (qcmd_hfsc_add_if(ifname, bandwidth, flags) != 0)
    137  1.1  thorpej 		return (0);
    138  1.1  thorpej 	return (1);
    139  1.1  thorpej }
    140  1.1  thorpej 
    141  1.1  thorpej int
    142  1.1  thorpej hfsc_class_parser(const char *ifname, const char *class_name,
    143  1.1  thorpej 		  const char *parent_name, int argc, char **argv)
    144  1.1  thorpej {
    145  1.1  thorpej 	u_int	m1, d, m2, rm1, rd, rm2, fm1, fd, fm2;
    146  1.1  thorpej 	int	qlimit = 50;
    147  1.1  thorpej 	int	flags = 0, admission = 0;
    148  1.1  thorpej 	int	type = 0, error;
    149  1.1  thorpej 
    150  1.1  thorpej 	rm1 = rd = rm2 = fm1 = fd = fm2 = 0;
    151  1.1  thorpej 	while (argc > 0) {
    152  1.1  thorpej 		if (*argv[0] == '[') {
    153  1.1  thorpej 			if (read_sc(&argc, &argv, &type, &m1, &d, &m2) != 0) {
    154  1.1  thorpej 				LOG(LOG_ERR, 0,
    155  1.4   itojun 				    "Bad service curve in %s, line %d",
    156  1.1  thorpej 				    altqconfigfile, line_no);
    157  1.1  thorpej 				return (0);
    158  1.1  thorpej 			}
    159  1.1  thorpej 			if (type & HFSC_REALTIMESC) {
    160  1.1  thorpej 				rm1 = m1; rd = d; rm2 = m2;
    161  1.1  thorpej 			}
    162  1.1  thorpej 			if (type & HFSC_LINKSHARINGSC) {
    163  1.1  thorpej 				fm1 = m1; fd = d; fm2 = m2;
    164  1.1  thorpej 			}
    165  1.1  thorpej 		} else if (EQUAL(*argv, "pshare")) {
    166  1.1  thorpej 			argc--; argv++;
    167  1.1  thorpej 			if (argc > 0) {
    168  1.1  thorpej 				struct ifinfo	*ifinfo;
    169  1.1  thorpej 				u_int pshare;
    170  1.1  thorpej 
    171  1.1  thorpej 				pshare = (u_int)strtoul(*argv, NULL, 0);
    172  1.1  thorpej 				if ((ifinfo = ifname2ifinfo(ifname)) != NULL) {
    173  1.1  thorpej 					fm2 = ifinfo->bandwidth / 100 * pshare;
    174  1.1  thorpej 					type |= HFSC_LINKSHARINGSC;
    175  1.1  thorpej 				}
    176  1.1  thorpej 			}
    177  1.1  thorpej 		} else if (EQUAL(*argv, "grate")) {
    178  1.1  thorpej 			argc--; argv++;
    179  1.1  thorpej 			if (argc > 0) {
    180  1.1  thorpej 				rm2 = atobps(*argv);
    181  1.1  thorpej 				type |= HFSC_REALTIMESC;
    182  1.1  thorpej 			}
    183  1.1  thorpej 		} else if (EQUAL(*argv, "qlimit")) {
    184  1.1  thorpej 			argc--; argv++;
    185  1.1  thorpej 			if (argc > 0)
    186  1.1  thorpej 				qlimit = strtoul(*argv, NULL, 0);
    187  1.1  thorpej 		} else if (EQUAL(*argv, "default")) {
    188  1.1  thorpej 			flags |= HFCF_DEFAULTCLASS;
    189  1.1  thorpej 		} else if (EQUAL(*argv, "admission")) {
    190  1.1  thorpej 			argc--; argv++;
    191  1.1  thorpej 			if (argc > 0) {
    192  1.1  thorpej 				if (EQUAL(*argv, "guaranteed")
    193  1.1  thorpej 				    || EQUAL(*argv, "cntlload"))
    194  1.1  thorpej 					admission = 1;
    195  1.1  thorpej 				else if (EQUAL(*argv, "none")) {
    196  1.1  thorpej 					/* nothing */
    197  1.1  thorpej 				} else {
    198  1.1  thorpej 					LOG(LOG_ERR, 0,
    199  1.4   itojun 					    "unknown admission type - %s, line %d",
    200  1.1  thorpej 					    *argv, line_no);
    201  1.1  thorpej 					return (0);
    202  1.1  thorpej 				}
    203  1.1  thorpej 			}
    204  1.1  thorpej 		} else if (EQUAL(*argv, "red")) {
    205  1.1  thorpej 			flags |= HFCF_RED;
    206  1.1  thorpej 		} else if (EQUAL(*argv, "ecn")) {
    207  1.1  thorpej 			flags |= HFCF_ECN;
    208  1.1  thorpej 		} else if (EQUAL(*argv, "rio")) {
    209  1.1  thorpej 			flags |= HFCF_RIO;
    210  1.1  thorpej 		} else if (EQUAL(*argv, "cleardscp")) {
    211  1.1  thorpej 			flags |= HFCF_CLEARDSCP;
    212  1.1  thorpej 		} else {
    213  1.1  thorpej 			LOG(LOG_ERR, 0,
    214  1.4   itojun 			    "Unknown keyword '%s' in %s, line %d",
    215  1.1  thorpej 			    *argv, altqconfigfile, line_no);
    216  1.1  thorpej 			return (0);
    217  1.1  thorpej 		}
    218  1.1  thorpej 
    219  1.1  thorpej 		argc--; argv++;
    220  1.1  thorpej 	}
    221  1.1  thorpej 
    222  1.1  thorpej 	if (type == 0) {
    223  1.1  thorpej 		LOG(LOG_ERR, 0,
    224  1.4   itojun 		    "hfsc: service curve not specified in %s, line %d",
    225  1.1  thorpej 		    altqconfigfile, line_no);
    226  1.1  thorpej 		return (0);
    227  1.1  thorpej 	}
    228  1.1  thorpej 
    229  1.1  thorpej 	if ((flags & HFCF_ECN) && (flags & (HFCF_RED|HFCF_RIO)) == 0)
    230  1.1  thorpej 		flags |= HFCF_RED;
    231  1.1  thorpej 
    232  1.1  thorpej 	/*
    233  1.1  thorpej 	 * if the link-sharing service curve is diffrent from
    234  1.1  thorpej 	 * the real-time service curve, we first create a class with the
    235  1.1  thorpej 	 * smaller service curve and then modify the other service curve.
    236  1.1  thorpej 	 */
    237  1.1  thorpej 	if (rm2 <= fm2) {
    238  1.1  thorpej 		m1 = rm1; d = rd; m2 = rm2;
    239  1.1  thorpej 	} else {
    240  1.1  thorpej 		m1 = fm1; d = fd; m2 = fm2;
    241  1.1  thorpej 	}
    242  1.1  thorpej 	error = qcmd_hfsc_add_class(ifname, class_name, parent_name,
    243  1.1  thorpej 				    m1, d, m2, qlimit, flags);
    244  1.1  thorpej 
    245  1.1  thorpej 	if (error == 0 && (rm1 != fm1 || rd != fd || rm2 != fm2)) {
    246  1.1  thorpej 		if (rm2 <= fm2) {
    247  1.1  thorpej 			m1 = fm1; d = fd; m2 = fm2; type = HFSC_LINKSHARINGSC;
    248  1.1  thorpej 		} else {
    249  1.1  thorpej 			m1 = rm1; d = rd; m2 = rm2; type = HFSC_REALTIMESC;
    250  1.1  thorpej 		}
    251  1.1  thorpej 		error = qcmd_hfsc_modify_class(ifname, class_name,
    252  1.1  thorpej 					       m1, d, m2, type);
    253  1.1  thorpej 	}
    254  1.1  thorpej 
    255  1.1  thorpej 	if (error == 0 && admission) {
    256  1.1  thorpej 		/* this is a special class for rsvp */
    257  1.1  thorpej 		struct ifinfo *ifinfo = ifname2ifinfo(ifname);
    258  1.1  thorpej 		struct classinfo *clinfo = clname2clinfo(ifinfo, class_name);
    259  1.1  thorpej 
    260  1.1  thorpej 		if (ifinfo->resv_class != NULL) {
    261  1.1  thorpej 			LOG(LOG_ERR, 0,
    262  1.4   itojun 			    "more than one admission class specified: %s",
    263  1.1  thorpej 			    class_name);
    264  1.1  thorpej 			return (0);
    265  1.1  thorpej 		}
    266  1.1  thorpej 		ifinfo->resv_class = clinfo;
    267  1.1  thorpej 	}
    268  1.1  thorpej 
    269  1.1  thorpej 	if (error) {
    270  1.4   itojun 		LOG(LOG_ERR, errno, "hfsc_class_parser: %s",
    271  1.1  thorpej 		    qoperror(error));
    272  1.1  thorpej 		return (0);
    273  1.1  thorpej 	}
    274  1.1  thorpej 	return (1);
    275  1.1  thorpej }
    276  1.1  thorpej 
    277  1.1  thorpej /*
    278  1.1  thorpej  * read service curve parameters
    279  1.1  thorpej  * '[' <type> <m1> <d> <m2> ']'
    280  1.1  thorpej  *  type := "sc", "rt", or "ls"
    281  1.1  thorpej  */
    282  1.1  thorpej static int
    283  1.1  thorpej read_sc(int *argcp, char ***argvp, int *type, u_int *m1, u_int *d, u_int *m2)
    284  1.1  thorpej {
    285  1.1  thorpej 	int argc = *argcp;
    286  1.1  thorpej 	char **argv = *argvp;
    287  1.1  thorpej 	char *cp;
    288  1.1  thorpej 
    289  1.1  thorpej 	cp = *argv;
    290  1.1  thorpej 	if (*cp++ != '[')
    291  1.1  thorpej 		return (-1);
    292  1.1  thorpej 	if (*cp == '\0') {
    293  1.1  thorpej 		cp = *++argv; --argc;
    294  1.1  thorpej 	}
    295  1.1  thorpej 	if (*cp == 's' || *cp == 'S')
    296  1.1  thorpej 		*type = HFSC_DEFAULTSC;
    297  1.1  thorpej 	else if (*cp == 'r' || *cp == 'R')
    298  1.1  thorpej 		*type = HFSC_REALTIMESC;
    299  1.1  thorpej 	else if (*cp == 'l' || *cp == 'L')
    300  1.1  thorpej 		*type = HFSC_LINKSHARINGSC;
    301  1.1  thorpej 	else
    302  1.1  thorpej 		return (-1);
    303  1.1  thorpej 	cp = *++argv; --argc;
    304  1.1  thorpej 	*m1 = atobps(cp);
    305  1.1  thorpej 	cp = *++argv; --argc;
    306  1.1  thorpej 	*d = (u_int)strtoul(cp, NULL, 0);
    307  1.1  thorpej 	cp = *++argv; --argc;
    308  1.1  thorpej 	*m2 = atobps(cp);
    309  1.1  thorpej 	if (strchr(cp, ']') == NULL) {
    310  1.1  thorpej 		cp = *++argv; --argc;
    311  1.1  thorpej 		if (*cp != ']')
    312  1.1  thorpej 			return (-1);
    313  1.1  thorpej 	}
    314  1.1  thorpej 	*argcp = argc;
    315  1.1  thorpej 	*argvp = argv;
    316  1.1  thorpej 	return (0);
    317  1.1  thorpej }
    318  1.1  thorpej 
    319  1.1  thorpej /*
    320  1.1  thorpej  * qcmd api
    321  1.1  thorpej  */
    322  1.1  thorpej int
    323  1.1  thorpej qcmd_hfsc_add_if(const char *ifname, u_int bandwidth, int flags)
    324  1.1  thorpej {
    325  1.1  thorpej 	int error;
    326  1.1  thorpej 
    327  1.1  thorpej 	error = qop_hfsc_add_if(NULL, ifname, bandwidth, flags);
    328  1.1  thorpej 	if (error != 0)
    329  1.4   itojun 		LOG(LOG_ERR, errno, "%s: can't add hfsc on interface '%s'",
    330  1.1  thorpej 		    qoperror(error), ifname);
    331  1.1  thorpej 	return (error);
    332  1.1  thorpej }
    333  1.1  thorpej 
    334  1.1  thorpej int
    335  1.1  thorpej qcmd_hfsc_add_class(const char *ifname, const char *class_name,
    336  1.1  thorpej 		    const char *parent_name, u_int m1, u_int d, u_int m2,
    337  1.1  thorpej 		    int qlimit, int flags)
    338  1.1  thorpej {
    339  1.1  thorpej 	struct ifinfo *ifinfo;
    340  1.1  thorpej 	struct classinfo *parent = NULL;
    341  1.1  thorpej 	struct service_curve sc;
    342  1.1  thorpej 	int error = 0;
    343  1.1  thorpej 
    344  1.1  thorpej 	if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
    345  1.1  thorpej 		error = QOPERR_BADIF;
    346  1.1  thorpej 
    347  1.1  thorpej 	if (error == 0 &&
    348  1.1  thorpej 	    (parent = clname2clinfo(ifinfo, parent_name)) == NULL)
    349  1.1  thorpej 		error = QOPERR_BADCLASS;
    350  1.1  thorpej 
    351  1.1  thorpej 	sc.m1 = m1;
    352  1.1  thorpej 	sc.d = d;
    353  1.1  thorpej 	sc.m2 = m2;
    354  1.1  thorpej 
    355  1.1  thorpej 	if (error == 0)
    356  1.1  thorpej 		error = qop_hfsc_add_class(NULL, class_name, ifinfo, parent,
    357  1.1  thorpej 					   &sc, qlimit, flags);
    358  1.1  thorpej 	if (error != 0)
    359  1.1  thorpej 		LOG(LOG_ERR, errno,
    360  1.4   itojun 		    "hfsc: %s: can't add class '%s' on interface '%s'",
    361  1.1  thorpej 		    qoperror(error), class_name, ifname);
    362  1.1  thorpej 	return (error);
    363  1.1  thorpej }
    364  1.1  thorpej 
    365  1.1  thorpej int
    366  1.1  thorpej qcmd_hfsc_modify_class(const char *ifname, const char *class_name,
    367  1.1  thorpej 		       u_int m1, u_int d, u_int m2, int sctype)
    368  1.1  thorpej {
    369  1.1  thorpej 	struct ifinfo *ifinfo;
    370  1.1  thorpej 	struct classinfo *clinfo;
    371  1.1  thorpej 	struct service_curve sc;
    372  1.1  thorpej 
    373  1.1  thorpej 	if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
    374  1.1  thorpej 		return (QOPERR_BADIF);
    375  1.1  thorpej 
    376  1.1  thorpej 	if ((clinfo = clname2clinfo(ifinfo, class_name)) == NULL)
    377  1.1  thorpej 		return (QOPERR_BADCLASS);
    378  1.1  thorpej 
    379  1.1  thorpej 	sc.m1 = m1;
    380  1.1  thorpej 	sc.d = d;
    381  1.1  thorpej 	sc.m2 = m2;
    382  1.1  thorpej 
    383  1.1  thorpej 	return qop_hfsc_modify_class(clinfo, &sc, sctype);
    384  1.1  thorpej }
    385  1.1  thorpej 
    386  1.1  thorpej /*
    387  1.1  thorpej  * qop api
    388  1.1  thorpej  */
    389  1.1  thorpej int
    390  1.1  thorpej qop_hfsc_add_if(struct ifinfo **rp, const char *ifname,
    391  1.1  thorpej 		u_int bandwidth, int flags)
    392  1.1  thorpej {
    393  1.1  thorpej 	struct ifinfo *ifinfo = NULL;
    394  1.1  thorpej 	struct hfsc_ifinfo *hfsc_ifinfo = NULL;
    395  1.1  thorpej 	struct service_curve sc;
    396  1.1  thorpej 	int error;
    397  1.1  thorpej 
    398  1.1  thorpej 	if ((hfsc_ifinfo = calloc(1, sizeof(*hfsc_ifinfo))) == NULL)
    399  1.1  thorpej 		return (QOPERR_NOMEM);
    400  1.1  thorpej 
    401  1.1  thorpej 	error = qop_add_if(&ifinfo, ifname, bandwidth,
    402  1.1  thorpej 			   &hfsc_qdisc, hfsc_ifinfo);
    403  1.1  thorpej 	if (error != 0)
    404  1.1  thorpej 		goto err_ret;
    405  1.1  thorpej 
    406  1.1  thorpej 	/* set enable hook */
    407  1.1  thorpej 	ifinfo->enable_hook = qop_hfsc_enable_hook;
    408  1.1  thorpej 
    409  1.1  thorpej 	/* create a dummy root class */
    410  1.1  thorpej 	sc.m1 = bandwidth;
    411  1.1  thorpej 	sc.d = 0;
    412  1.1  thorpej 	sc.m2 = bandwidth;
    413  1.1  thorpej 	if ((error = qop_hfsc_add_class(&hfsc_ifinfo->root_class, "root",
    414  1.1  thorpej 					ifinfo, NULL, &sc, 0, 0)) != 0) {
    415  1.1  thorpej 		LOG(LOG_ERR, errno,
    416  1.4   itojun 		    "hfsc: %s: can't create dummy root class on %s!",
    417  1.1  thorpej 		    qoperror(error), ifname);
    418  1.1  thorpej 		(void)qop_delete_if(ifinfo);
    419  1.1  thorpej 		return (QOPERR_CLASS);
    420  1.1  thorpej 	}
    421  1.1  thorpej 
    422  1.1  thorpej 	if (rp != NULL)
    423  1.1  thorpej 		*rp = ifinfo;
    424  1.1  thorpej 	return (0);
    425  1.1  thorpej 
    426  1.1  thorpej  err_ret:
    427  1.1  thorpej 	if (hfsc_ifinfo != NULL) {
    428  1.1  thorpej 		free(hfsc_ifinfo);
    429  1.1  thorpej 		if (ifinfo != NULL)
    430  1.1  thorpej 			ifinfo->private = NULL;
    431  1.1  thorpej 	}
    432  1.1  thorpej 	return (error);
    433  1.1  thorpej }
    434  1.1  thorpej 
    435  1.1  thorpej #define is_sc_null(sc)	(((sc) == NULL) || ((sc)->m1 == 0 && (sc)->m2 == 0))
    436  1.1  thorpej 
    437  1.1  thorpej int
    438  1.1  thorpej qop_hfsc_add_class(struct classinfo **rp, const char *class_name,
    439  1.1  thorpej 		   struct ifinfo *ifinfo, struct classinfo *parent,
    440  1.1  thorpej 		   struct service_curve *sc, int qlimit, int flags)
    441  1.1  thorpej {
    442  1.1  thorpej 	struct classinfo *clinfo;
    443  1.1  thorpej 	struct hfsc_ifinfo *hfsc_ifinfo;
    444  1.1  thorpej 	struct hfsc_classinfo *hfsc_clinfo = NULL, *parent_clinfo = NULL;
    445  1.1  thorpej 	int error;
    446  1.1  thorpej 
    447  1.1  thorpej 	hfsc_ifinfo = ifinfo->private;
    448  1.1  thorpej 	if ((flags & HFCF_DEFAULTCLASS) && hfsc_ifinfo->default_class != NULL)
    449  1.1  thorpej 		return (QOPERR_CLASS_INVAL);
    450  1.1  thorpej 
    451  1.1  thorpej 	if (validate_sc(sc) != 0)
    452  1.1  thorpej 		return (QOPERR_INVAL);
    453  1.1  thorpej 
    454  1.1  thorpej 	/* admission control */
    455  1.1  thorpej 	if (parent != NULL && !is_sc_null(sc)) {
    456  1.1  thorpej 		parent_clinfo = parent->private;
    457  1.1  thorpej 		gsc_add_sc(&parent_clinfo->gen_rsc, sc);
    458  1.1  thorpej 		gsc_add_sc(&parent_clinfo->gen_fsc, sc);
    459  1.1  thorpej 		if (!is_gsc_under_sc(&parent_clinfo->gen_rsc,
    460  1.1  thorpej 				     &parent_clinfo->rsc) ||
    461  1.1  thorpej 		    !is_gsc_under_sc(&parent_clinfo->gen_fsc,
    462  1.1  thorpej 				     &parent_clinfo->fsc)) {
    463  1.1  thorpej 			/* admission control failure */
    464  1.1  thorpej 			error = QOPERR_ADMISSION_NOBW;
    465  1.1  thorpej 			goto err_ret;
    466  1.1  thorpej 		}
    467  1.1  thorpej 	}
    468  1.1  thorpej 
    469  1.1  thorpej 	if ((hfsc_clinfo = calloc(1, sizeof(*hfsc_clinfo))) == NULL) {
    470  1.1  thorpej 		error = QOPERR_NOMEM;
    471  1.1  thorpej 		goto err_ret;
    472  1.1  thorpej 	}
    473  1.1  thorpej 
    474  1.1  thorpej 	hfsc_clinfo->rsc = *sc;
    475  1.1  thorpej 	hfsc_clinfo->fsc = *sc;
    476  1.1  thorpej 	LIST_INIT(&hfsc_clinfo->gen_rsc);
    477  1.1  thorpej 	LIST_INIT(&hfsc_clinfo->gen_fsc);
    478  1.1  thorpej 	hfsc_clinfo->qlimit = qlimit;
    479  1.1  thorpej 	hfsc_clinfo->flags = flags;
    480  1.1  thorpej 
    481  1.1  thorpej 	if ((error = qop_add_class(&clinfo, class_name, ifinfo, parent,
    482  1.1  thorpej 				   hfsc_clinfo)) != 0)
    483  1.1  thorpej 		goto err_ret;
    484  1.1  thorpej 
    485  1.1  thorpej 	/* set delete hook */
    486  1.1  thorpej 	clinfo->delete_hook = qop_hfsc_delete_class_hook;
    487  1.1  thorpej 
    488  1.1  thorpej 	if (flags & HFCF_DEFAULTCLASS)
    489  1.1  thorpej 		hfsc_ifinfo->default_class = clinfo;
    490  1.1  thorpej 
    491  1.1  thorpej 	if (parent == NULL) {
    492  1.1  thorpej 		/*
    493  1.1  thorpej 		 * if this is a root class, reserve 20% of the real-time
    494  1.1  thorpej 		 * bandwidth for safety.
    495  1.1  thorpej 		 * many network cards are not able to saturate the wire,
    496  1.1  thorpej 		 * and if we allocate real-time traffic more than the
    497  1.1  thorpej 		 * maximum sending rate of the card, hfsc is no longer
    498  1.1  thorpej 		 * able to meet the delay bound requirements.
    499  1.1  thorpej 		 */
    500  1.1  thorpej 		hfsc_clinfo->rsc.m1 = hfsc_clinfo->rsc.m1 / 10 * 8;
    501  1.1  thorpej 		hfsc_clinfo->rsc.m2 = hfsc_clinfo->rsc.m2 / 10 * 8;
    502  1.1  thorpej 	}
    503  1.1  thorpej 
    504  1.1  thorpej 	if (rp != NULL)
    505  1.1  thorpej 		*rp = clinfo;
    506  1.1  thorpej 	return (0);
    507  1.1  thorpej 
    508  1.1  thorpej  err_ret:
    509  1.1  thorpej 	/* cancel admission control */
    510  1.1  thorpej 	if (parent != NULL && !is_sc_null(sc)) {
    511  1.1  thorpej 		gsc_sub_sc(&parent_clinfo->gen_rsc, sc);
    512  1.1  thorpej 		gsc_sub_sc(&parent_clinfo->gen_fsc, sc);
    513  1.1  thorpej 	}
    514  1.1  thorpej 
    515  1.1  thorpej 	if (hfsc_clinfo != NULL) {
    516  1.1  thorpej 		free(hfsc_clinfo);
    517  1.1  thorpej 		clinfo->private = NULL;
    518  1.1  thorpej 	}
    519  1.1  thorpej 
    520  1.1  thorpej 	return (error);
    521  1.1  thorpej }
    522  1.1  thorpej 
    523  1.1  thorpej /*
    524  1.1  thorpej  * this is called from qop_delete_class() before a class is destroyed
    525  1.1  thorpej  * for discipline specific cleanup.
    526  1.1  thorpej  */
    527  1.1  thorpej static int
    528  1.1  thorpej qop_hfsc_delete_class_hook(struct classinfo *clinfo)
    529  1.1  thorpej {
    530  1.1  thorpej 	struct hfsc_classinfo *hfsc_clinfo, *parent_clinfo;
    531  1.1  thorpej 
    532  1.1  thorpej 	hfsc_clinfo = clinfo->private;
    533  1.1  thorpej 
    534  1.1  thorpej 	/* cancel admission control */
    535  1.1  thorpej 	if (clinfo->parent != NULL) {
    536  1.1  thorpej 		parent_clinfo = clinfo->parent->private;
    537  1.1  thorpej 
    538  1.1  thorpej 		gsc_sub_sc(&parent_clinfo->gen_rsc, &hfsc_clinfo->rsc);
    539  1.1  thorpej 		gsc_sub_sc(&parent_clinfo->gen_fsc, &hfsc_clinfo->fsc);
    540  1.1  thorpej 	}
    541  1.1  thorpej 
    542  1.1  thorpej 	gsc_destroy(&hfsc_clinfo->gen_rsc);
    543  1.1  thorpej 	gsc_destroy(&hfsc_clinfo->gen_fsc);
    544  1.1  thorpej 	return (0);
    545  1.1  thorpej }
    546  1.1  thorpej 
    547  1.1  thorpej int
    548  1.1  thorpej qop_hfsc_modify_class(struct classinfo *clinfo,
    549  1.1  thorpej 		      struct service_curve *sc, int sctype)
    550  1.1  thorpej {
    551  1.1  thorpej 	struct hfsc_classinfo *hfsc_clinfo, *parent_clinfo;
    552  1.1  thorpej 	struct service_curve rsc, fsc;
    553  1.1  thorpej 	int error;
    554  1.1  thorpej 
    555  1.1  thorpej 	if (validate_sc(sc) != 0)
    556  1.1  thorpej 		return (QOPERR_INVAL);
    557  1.1  thorpej 
    558  1.1  thorpej 	hfsc_clinfo = clinfo->private;
    559  1.1  thorpej 	if (clinfo->parent == NULL)
    560  1.1  thorpej 		return (QOPERR_CLASS_INVAL);
    561  1.1  thorpej 	parent_clinfo = clinfo->parent->private;
    562  1.1  thorpej 
    563  1.1  thorpej 	/* save old service curves */
    564  1.1  thorpej 	rsc = hfsc_clinfo->rsc;
    565  1.1  thorpej 	fsc = hfsc_clinfo->fsc;
    566  1.1  thorpej 
    567  1.1  thorpej 	/* admission control */
    568  1.1  thorpej 	if (sctype & HFSC_REALTIMESC) {
    569  1.1  thorpej 		if (!is_gsc_under_sc(&hfsc_clinfo->gen_rsc, sc)) {
    570  1.1  thorpej 			/* admission control failure */
    571  1.1  thorpej 			return (QOPERR_ADMISSION);
    572  1.1  thorpej 		}
    573  1.1  thorpej 
    574  1.1  thorpej 		gsc_sub_sc(&parent_clinfo->gen_rsc, &hfsc_clinfo->rsc);
    575  1.1  thorpej 		gsc_add_sc(&parent_clinfo->gen_rsc, sc);
    576  1.1  thorpej 		if (!is_gsc_under_sc(&parent_clinfo->gen_rsc,
    577  1.1  thorpej 				     &parent_clinfo->rsc)) {
    578  1.1  thorpej 			/* admission control failure */
    579  1.1  thorpej 			gsc_sub_sc(&parent_clinfo->gen_rsc, sc);
    580  1.1  thorpej 			gsc_add_sc(&parent_clinfo->gen_rsc, &hfsc_clinfo->rsc);
    581  1.1  thorpej 			return (QOPERR_ADMISSION_NOBW);
    582  1.1  thorpej 		}
    583  1.1  thorpej 		hfsc_clinfo->rsc = *sc;
    584  1.1  thorpej 	}
    585  1.1  thorpej 	if (sctype & HFSC_LINKSHARINGSC) {
    586  1.1  thorpej 		if (!is_gsc_under_sc(&hfsc_clinfo->gen_fsc, sc)) {
    587  1.1  thorpej 			/* admission control failure */
    588  1.1  thorpej 			return (QOPERR_ADMISSION);
    589  1.1  thorpej 		}
    590  1.1  thorpej 
    591  1.1  thorpej 		gsc_sub_sc(&parent_clinfo->gen_fsc, &hfsc_clinfo->fsc);
    592  1.1  thorpej 		gsc_add_sc(&parent_clinfo->gen_fsc, sc);
    593  1.1  thorpej 		if (!is_gsc_under_sc(&parent_clinfo->gen_fsc,
    594  1.1  thorpej 				     &parent_clinfo->fsc)) {
    595  1.1  thorpej 			/* admission control failure */
    596  1.1  thorpej 			gsc_sub_sc(&parent_clinfo->gen_fsc, sc);
    597  1.1  thorpej 			gsc_add_sc(&parent_clinfo->gen_fsc, &hfsc_clinfo->fsc);
    598  1.1  thorpej 			return (QOPERR_ADMISSION_NOBW);
    599  1.1  thorpej 		}
    600  1.1  thorpej 		hfsc_clinfo->fsc = *sc;
    601  1.1  thorpej 	}
    602  1.1  thorpej 
    603  1.1  thorpej 	error = qop_modify_class(clinfo, (void *)((long)sctype));
    604  1.1  thorpej 	if (error == 0)
    605  1.1  thorpej 		return (0);
    606  1.1  thorpej 
    607  1.1  thorpej 	/* modify failed!, restore the old service curves */
    608  1.1  thorpej 	if (sctype & HFSC_REALTIMESC) {
    609  1.1  thorpej 		gsc_sub_sc(&parent_clinfo->gen_rsc, sc);
    610  1.1  thorpej 		gsc_add_sc(&parent_clinfo->gen_rsc, &rsc);
    611  1.1  thorpej 		hfsc_clinfo->rsc = rsc;
    612  1.1  thorpej 	}
    613  1.1  thorpej 	if (sctype & HFSC_LINKSHARINGSC) {
    614  1.1  thorpej 		gsc_sub_sc(&parent_clinfo->gen_fsc, sc);
    615  1.1  thorpej 		gsc_add_sc(&parent_clinfo->gen_fsc, &fsc);
    616  1.1  thorpej 		hfsc_clinfo->fsc = fsc;
    617  1.1  thorpej 	}
    618  1.1  thorpej 	return (error);
    619  1.1  thorpej }
    620  1.1  thorpej 
    621  1.1  thorpej /*
    622  1.1  thorpej  * sanity check at enabling hfsc:
    623  1.1  thorpej  *  1. there must one default class for an interface
    624  1.1  thorpej  *  2. the default class must be a leaf class
    625  1.1  thorpej  *  3. an internal class should not have filters
    626  1.1  thorpej  * (rule 2 and 3 are due to the fact that the hfsc link-sharing algorithm
    627  1.1  thorpej  *  do not schedule internal classes.)
    628  1.1  thorpej  */
    629  1.1  thorpej static int
    630  1.1  thorpej qop_hfsc_enable_hook(struct ifinfo *ifinfo)
    631  1.1  thorpej {
    632  1.1  thorpej 	struct hfsc_ifinfo *hfsc_ifinfo;
    633  1.1  thorpej 	struct classinfo *clinfo;
    634  1.1  thorpej 
    635  1.1  thorpej 	hfsc_ifinfo = ifinfo->private;
    636  1.1  thorpej 	if (hfsc_ifinfo->default_class == NULL) {
    637  1.4   itojun 		LOG(LOG_ERR, 0, "hfsc: no default class on interface %s!",
    638  1.1  thorpej 		    ifinfo->ifname);
    639  1.1  thorpej 		return (QOPERR_CLASS);
    640  1.1  thorpej 	} else if (hfsc_ifinfo->default_class->child != NULL) {
    641  1.4   itojun 		LOG(LOG_ERR, 0, "hfsc: default class on %s must be a leaf!",
    642  1.1  thorpej 		    ifinfo->ifname);
    643  1.1  thorpej 		return (QOPERR_CLASS);
    644  1.1  thorpej 	}
    645  1.1  thorpej 
    646  1.1  thorpej 	LIST_FOREACH(clinfo, &ifinfo->cllist, next) {
    647  1.1  thorpej 		if (clinfo->child != NULL && !LIST_EMPTY(&clinfo->fltrlist)) {
    648  1.4   itojun 			LOG(LOG_ERR, 0,
    649  1.4   itojun 			    "hfsc: internal class \"%s\" should not have a filter!",
    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.4   itojun 		LOG(LOG_ERR, 0, "m1 must be 0 for convex!");
    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.6   itojun #define	INFINITY	HUGE_VAL  /* positive infinity defined in <math.h> */
    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.4   itojun 		LOG(LOG_ERR, errno, "HFSC open");
    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