Home | History | Annotate | Line # | Download | only in libaltq
      1  1.4   itojun /*	$NetBSD: qop_cdnr.c,v 1.4 2001/08/22 08:52:37 itojun Exp $	*/
      2  1.4   itojun /*	$KAME: qop_cdnr.c,v 1.9 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_cdnr.h>
     50  1.1  thorpej #include "altq_qop.h"
     51  1.1  thorpej #include "qop_cdnr.h"
     52  1.1  thorpej /*
     53  1.1  thorpej  * diffserve traffic conditioner support
     54  1.1  thorpej  *
     55  1.1  thorpej  * we use the existing qop interface to support conditioner.
     56  1.1  thorpej  */
     57  1.1  thorpej 
     58  1.2   itojun static struct ifinfo *cdnr_ifname2ifinfo(const char *);
     59  1.2   itojun static int cdnr_attach(struct ifinfo *);
     60  1.2   itojun static int cdnr_detach(struct ifinfo *);
     61  1.2   itojun static int cdnr_enable(struct ifinfo *);
     62  1.2   itojun static int cdnr_disable(struct ifinfo *);
     63  1.2   itojun static int cdnr_add_class(struct classinfo *);
     64  1.2   itojun static int cdnr_modify_class(struct classinfo *, void *);
     65  1.2   itojun static int cdnr_delete_class(struct classinfo *);
     66  1.2   itojun static int cdnr_add_filter(struct fltrinfo *);
     67  1.2   itojun static int cdnr_delete_filter(struct fltrinfo *);
     68  1.2   itojun static int verify_tbprofile(struct tb_profile *, const char *);
     69  1.1  thorpej 
     70  1.1  thorpej #define CDNR_DEVICE	"/dev/altq/cdnr"
     71  1.1  thorpej 
     72  1.1  thorpej static int cdnr_fd = -1;
     73  1.1  thorpej static int cdnr_refcount = 0;
     74  1.1  thorpej 
     75  1.1  thorpej static struct qdisc_ops cdnr_qdisc = {
     76  1.1  thorpej 	ALTQT_CDNR,
     77  1.1  thorpej 	"cdnr",
     78  1.1  thorpej 	cdnr_attach,
     79  1.1  thorpej 	cdnr_detach,
     80  1.1  thorpej 	NULL,			/* clear */
     81  1.1  thorpej 	cdnr_enable,
     82  1.1  thorpej 	cdnr_disable,
     83  1.1  thorpej 	cdnr_add_class,
     84  1.1  thorpej 	cdnr_modify_class,
     85  1.1  thorpej 	cdnr_delete_class,
     86  1.1  thorpej 	cdnr_add_filter,
     87  1.1  thorpej 	cdnr_delete_filter,
     88  1.1  thorpej };
     89  1.1  thorpej 
     90  1.1  thorpej u_long
     91  1.1  thorpej cdnr_name2handle(const char *ifname, const char *cdnr_name)
     92  1.1  thorpej {
     93  1.1  thorpej 	struct ifinfo		*ifinfo;
     94  1.1  thorpej 	struct classinfo	*clinfo;
     95  1.1  thorpej 
     96  1.1  thorpej 	if ((ifinfo = cdnr_ifname2ifinfo(ifname)) == NULL)
     97  1.1  thorpej 		return (CDNR_NULL_HANDLE);
     98  1.1  thorpej 
     99  1.1  thorpej 	if ((clinfo = clname2clinfo(ifinfo, cdnr_name)) == NULL)
    100  1.1  thorpej 		return (CDNR_NULL_HANDLE);
    101  1.1  thorpej 
    102  1.1  thorpej 	return (clinfo->handle);
    103  1.1  thorpej }
    104  1.1  thorpej 
    105  1.1  thorpej static struct ifinfo *
    106  1.1  thorpej cdnr_ifname2ifinfo(const char *ifname)
    107  1.1  thorpej {
    108  1.1  thorpej 	struct ifinfo	*ifinfo;
    109  1.1  thorpej 	char input_ifname[64];
    110  1.1  thorpej 
    111  1.1  thorpej 	/*
    112  1.1  thorpej 	 * search for an existing input interface
    113  1.1  thorpej 	 */
    114  1.1  thorpej 	if ((ifinfo = input_ifname2ifinfo(ifname)) != NULL)
    115  1.1  thorpej 		return (ifinfo);
    116  1.1  thorpej 
    117  1.1  thorpej 	/*
    118  1.1  thorpej 	 * if there is a corresponding output interface,
    119  1.1  thorpej 	 * create an input interface by prepending "_" to
    120  1.1  thorpej 	 * its name.
    121  1.1  thorpej 	 */
    122  1.1  thorpej 	if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
    123  1.1  thorpej 		return (NULL);
    124  1.1  thorpej 
    125  1.1  thorpej 	input_ifname[0] = '_';
    126  1.2   itojun 	strlcpy(input_ifname+1, ifname, sizeof(input_ifname)-1);
    127  1.1  thorpej 	if (qop_add_if(&ifinfo, input_ifname, 0, &cdnr_qdisc, NULL) != 0) {
    128  1.1  thorpej 		LOG(LOG_ERR, errno,
    129  1.4   itojun 		    "cdnr_ifname2ifinfo: can't add a input interface %s",
    130  1.1  thorpej 		    ifname);
    131  1.1  thorpej 		return (NULL);
    132  1.1  thorpej 	}
    133  1.1  thorpej 	return (ifinfo);
    134  1.1  thorpej }
    135  1.1  thorpej 
    136  1.1  thorpej int
    137  1.1  thorpej qcmd_cdnr_add_element(struct tc_action *rp, const char *ifname,
    138  1.1  thorpej 		   const char *cdnr_name, struct tc_action *action)
    139  1.1  thorpej {
    140  1.1  thorpej 	struct ifinfo		*ifinfo;
    141  1.1  thorpej 	struct classinfo	*clinfo;
    142  1.1  thorpej 	int error;
    143  1.1  thorpej 
    144  1.1  thorpej 	if ((ifinfo = cdnr_ifname2ifinfo(ifname)) == NULL)
    145  1.1  thorpej 		return (QOPERR_BADIF);
    146  1.1  thorpej 
    147  1.1  thorpej 	if ((error = qop_cdnr_add_element(&clinfo, cdnr_name, ifinfo,
    148  1.1  thorpej 					  action)) != 0) {
    149  1.4   itojun 		LOG(LOG_ERR, errno, "%s: add element failed!",
    150  1.1  thorpej 		    qoperror(error));
    151  1.1  thorpej 		return (error);
    152  1.1  thorpej 	}
    153  1.1  thorpej 
    154  1.1  thorpej 	if (rp != NULL) {
    155  1.1  thorpej 		rp->tca_code = TCACODE_HANDLE;
    156  1.1  thorpej 		rp->tca_handle = clinfo->handle;
    157  1.1  thorpej 	}
    158  1.1  thorpej 	return (0);
    159  1.1  thorpej }
    160  1.1  thorpej 
    161  1.1  thorpej int
    162  1.1  thorpej qcmd_cdnr_add_tbmeter(struct tc_action *rp, const char *ifname,
    163  1.1  thorpej 		      const char *cdnr_name,
    164  1.1  thorpej 		      struct tb_profile *profile,
    165  1.1  thorpej 		      struct tc_action *in_action,
    166  1.1  thorpej 		      struct tc_action *out_action)
    167  1.1  thorpej {
    168  1.1  thorpej 	struct ifinfo		*ifinfo;
    169  1.1  thorpej 	struct classinfo	*clinfo;
    170  1.1  thorpej 	int error;
    171  1.1  thorpej 
    172  1.1  thorpej 	if ((ifinfo = cdnr_ifname2ifinfo(ifname)) == NULL)
    173  1.1  thorpej 		return (QOPERR_BADIF);
    174  1.1  thorpej 
    175  1.1  thorpej 	verify_tbprofile(profile, cdnr_name);
    176  1.1  thorpej 
    177  1.1  thorpej 	if ((error = qop_cdnr_add_tbmeter(&clinfo, cdnr_name, ifinfo,
    178  1.1  thorpej 				  profile, in_action, out_action)) != 0) {
    179  1.4   itojun 		LOG(LOG_ERR, errno, "%s: add tbmeter failed!",
    180  1.1  thorpej 		    qoperror(error));
    181  1.1  thorpej 		return (error);
    182  1.1  thorpej 	}
    183  1.1  thorpej 
    184  1.1  thorpej 	if (rp != NULL) {
    185  1.1  thorpej 		rp->tca_code = TCACODE_HANDLE;
    186  1.1  thorpej 		rp->tca_handle = clinfo->handle;
    187  1.1  thorpej 	}
    188  1.1  thorpej 	return (0);
    189  1.1  thorpej }
    190  1.1  thorpej 
    191  1.1  thorpej int
    192  1.1  thorpej qcmd_cdnr_add_trtcm(struct tc_action *rp, const char *ifname,
    193  1.1  thorpej 		    const char *cdnr_name,
    194  1.1  thorpej 		    struct tb_profile *cmtd_profile,
    195  1.1  thorpej 		    struct tb_profile *peak_profile,
    196  1.1  thorpej 		    struct tc_action *green_action,
    197  1.1  thorpej 		    struct tc_action *yellow_action,
    198  1.1  thorpej 		    struct tc_action *red_action, int coloraware)
    199  1.1  thorpej {
    200  1.1  thorpej 	struct ifinfo		*ifinfo;
    201  1.1  thorpej 	struct classinfo	*clinfo;
    202  1.1  thorpej 	int error;
    203  1.1  thorpej 
    204  1.1  thorpej 	if ((ifinfo = cdnr_ifname2ifinfo(ifname)) == NULL)
    205  1.1  thorpej 		return (QOPERR_BADIF);
    206  1.1  thorpej 
    207  1.1  thorpej 	verify_tbprofile(cmtd_profile, cdnr_name);
    208  1.1  thorpej 	verify_tbprofile(peak_profile, cdnr_name);
    209  1.1  thorpej 
    210  1.1  thorpej 	if ((error = qop_cdnr_add_trtcm(&clinfo, cdnr_name, ifinfo,
    211  1.1  thorpej 			  cmtd_profile, peak_profile,
    212  1.1  thorpej 			  green_action, yellow_action, red_action,
    213  1.1  thorpej 	     		  coloraware)) != 0) {
    214  1.4   itojun 		LOG(LOG_ERR, errno, "%s: add trtcm failed!",
    215  1.1  thorpej 		    qoperror(error));
    216  1.1  thorpej 		return (error);
    217  1.1  thorpej 	}
    218  1.1  thorpej 
    219  1.1  thorpej 	if (rp != NULL) {
    220  1.1  thorpej 		rp->tca_code = TCACODE_HANDLE;
    221  1.1  thorpej 		rp->tca_handle = clinfo->handle;
    222  1.1  thorpej 	}
    223  1.1  thorpej 	return (0);
    224  1.1  thorpej }
    225  1.1  thorpej 
    226  1.1  thorpej int
    227  1.1  thorpej qcmd_cdnr_add_tswtcm(struct tc_action *rp, const char *ifname,
    228  1.1  thorpej 		     const char *cdnr_name, const u_int32_t cmtd_rate,
    229  1.1  thorpej 		     const u_int32_t peak_rate, const u_int32_t avg_interval,
    230  1.1  thorpej 		     struct tc_action *green_action,
    231  1.1  thorpej 		     struct tc_action *yellow_action,
    232  1.1  thorpej 		     struct tc_action *red_action)
    233  1.1  thorpej {
    234  1.1  thorpej 	struct ifinfo		*ifinfo;
    235  1.1  thorpej 	struct classinfo	*clinfo;
    236  1.1  thorpej 	int error;
    237  1.1  thorpej 
    238  1.1  thorpej 	if ((ifinfo = cdnr_ifname2ifinfo(ifname)) == NULL)
    239  1.1  thorpej 		return (QOPERR_BADIF);
    240  1.1  thorpej 
    241  1.1  thorpej 	if (cmtd_rate > peak_rate) {
    242  1.1  thorpej 		LOG(LOG_ERR, 0,
    243  1.4   itojun 		    "add tswtcm: cmtd_rate larger than peak_rate!");
    244  1.1  thorpej 		return (QOPERR_INVAL);
    245  1.1  thorpej 	}
    246  1.1  thorpej 
    247  1.1  thorpej 	if ((error = qop_cdnr_add_tswtcm(&clinfo, cdnr_name, ifinfo,
    248  1.1  thorpej 					cmtd_rate, peak_rate, avg_interval,
    249  1.1  thorpej 					green_action, yellow_action,
    250  1.1  thorpej 					red_action)) != 0) {
    251  1.4   itojun 		LOG(LOG_ERR, errno, "%s: add tswtcm failed!",
    252  1.1  thorpej 		    qoperror(error));
    253  1.1  thorpej 		return (error);
    254  1.1  thorpej 	}
    255  1.1  thorpej 
    256  1.1  thorpej 	if (rp != NULL) {
    257  1.1  thorpej 		rp->tca_code = TCACODE_HANDLE;
    258  1.1  thorpej 		rp->tca_handle = clinfo->handle;
    259  1.1  thorpej 	}
    260  1.1  thorpej 	return (0);
    261  1.1  thorpej }
    262  1.1  thorpej 
    263  1.1  thorpej int
    264  1.1  thorpej qcmd_cdnr_delete(const char *ifname, const char *cdnr_name)
    265  1.1  thorpej {
    266  1.1  thorpej 	struct ifinfo		*ifinfo;
    267  1.1  thorpej 	struct classinfo	*clinfo;
    268  1.1  thorpej 
    269  1.1  thorpej 	if ((ifinfo = cdnr_ifname2ifinfo(ifname)) == NULL)
    270  1.1  thorpej 		return (QOPERR_BADIF);
    271  1.1  thorpej 
    272  1.1  thorpej 	if ((clinfo = clname2clinfo(ifinfo, cdnr_name)) == NULL)
    273  1.1  thorpej 		return (QOPERR_BADCLASS);
    274  1.1  thorpej 
    275  1.1  thorpej 	return qop_delete_cdnr(clinfo);
    276  1.1  thorpej }
    277  1.1  thorpej 
    278  1.1  thorpej /*
    279  1.1  thorpej  * class operations:
    280  1.1  thorpej  *	class structure is used to hold conditioners.
    281  1.1  thorpej  *	XXX
    282  1.1  thorpej  *	conditioners has dependencies in the reverse order; parent nodes
    283  1.1  thorpej  *	refere to child nodes, and thus, a child is created first and
    284  1.1  thorpej  *	parents should be removed first.
    285  1.1  thorpej  *	qop_add_cdnr() and qop_delete_cdnr() are wrapper functions
    286  1.1  thorpej  *	of qop_add_class() and qop_delete_class(), and takes care
    287  1.1  thorpej  *	of dependencies.
    288  1.1  thorpej  *	1. when adding a conditioner, it is created as a child of a
    289  1.1  thorpej  *	   dummy root class.  then, the child conditioners are made
    290  1.1  thorpej  *	   as its children.
    291  1.1  thorpej  *	2. when deleting a conditioner, its child conditioners are made
    292  1.1  thorpej  *	   as children of the dummy root class.  then, the conditioner
    293  1.1  thorpej  *	   is deleted.
    294  1.1  thorpej  */
    295  1.1  thorpej 
    296  1.1  thorpej int
    297  1.1  thorpej qop_add_cdnr(struct classinfo **rp, const char *cdnr_name,
    298  1.1  thorpej 	     struct ifinfo *ifinfo, struct classinfo **childlist,
    299  1.1  thorpej 	     void *cdnr_private)
    300  1.1  thorpej {
    301  1.1  thorpej 	struct classinfo	*clinfo, *root, *cl, *prev;
    302  1.1  thorpej 	int error;
    303  1.1  thorpej 
    304  1.1  thorpej 	/*
    305  1.1  thorpej 	 * if there is no root cdnr, create one.
    306  1.1  thorpej 	 */
    307  1.1  thorpej 	if ((root = get_rootclass(ifinfo)) == NULL) {
    308  1.1  thorpej 		if ((error = qop_add_class(&root, "cdnr_root",
    309  1.1  thorpej 					   ifinfo, NULL, NULL)) != 0) {
    310  1.1  thorpej 			LOG(LOG_ERR, errno,
    311  1.4   itojun 			    "cdnr: %s: can't create dummy root cdnr on %s!",
    312  1.1  thorpej 			    qoperror(error), ifinfo->ifname);
    313  1.1  thorpej 			return (QOPERR_CLASS);
    314  1.1  thorpej 		}
    315  1.1  thorpej 	}
    316  1.1  thorpej 
    317  1.1  thorpej 	/*
    318  1.1  thorpej 	 * create a class as a child of a root class.
    319  1.1  thorpej 	 */
    320  1.1  thorpej 	if ((error = qop_add_class(&clinfo, cdnr_name,
    321  1.1  thorpej 				   ifinfo, root, cdnr_private)) != 0)
    322  1.1  thorpej 		return (error);
    323  1.1  thorpej 	/*
    324  1.1  thorpej 	 * move child nodes
    325  1.1  thorpej 	 */
    326  1.1  thorpej 	for (cl = *childlist; cl != NULL; cl = *++childlist) {
    327  1.1  thorpej 		if (cl->parent != root) {
    328  1.1  thorpej 			/*
    329  1.1  thorpej 			 * this conditioner already has a non-root parent.
    330  1.1  thorpej 			 * we can't track down a multi-parent node by a
    331  1.1  thorpej 			 * tree structure; leave it as it is.
    332  1.1  thorpej 			 * (we need a mechanism similar to a symbolic link
    333  1.1  thorpej 			 * in a file system)
    334  1.1  thorpej 			 */
    335  1.1  thorpej 			continue;
    336  1.1  thorpej 		}
    337  1.1  thorpej 		/* remove this child from the root */
    338  1.1  thorpej 		if (root->child == cl)
    339  1.1  thorpej 			root->child = cl->sibling;
    340  1.1  thorpej 		else for (prev = root->child;
    341  1.1  thorpej 			  prev->sibling != NULL; prev = prev->sibling)
    342  1.1  thorpej 			if (prev->sibling == cl) {
    343  1.1  thorpej 				prev->sibling = cl->sibling;
    344  1.1  thorpej 				break;
    345  1.1  thorpej 			}
    346  1.1  thorpej 
    347  1.1  thorpej 		/* add as a child */
    348  1.1  thorpej 		cl->sibling = clinfo->child;
    349  1.1  thorpej 		clinfo->child = cl;
    350  1.1  thorpej 		cl->parent = clinfo;
    351  1.1  thorpej 	}
    352  1.1  thorpej 
    353  1.1  thorpej 	if (rp != NULL)
    354  1.1  thorpej 		*rp = clinfo;
    355  1.1  thorpej 	return (0);
    356  1.1  thorpej }
    357  1.1  thorpej 
    358  1.1  thorpej int
    359  1.1  thorpej qop_delete_cdnr(struct classinfo *clinfo)
    360  1.1  thorpej {
    361  1.1  thorpej 	struct classinfo *cl, *root;
    362  1.1  thorpej 	int error;
    363  1.1  thorpej 
    364  1.1  thorpej 	if ((root = get_rootclass(clinfo->ifinfo)) == NULL) {
    365  1.4   itojun 		LOG(LOG_ERR, 0, "qop_delete_cdnr: no root cdnr!");
    366  1.1  thorpej 		return (QOPERR_CLASS);
    367  1.1  thorpej 	}
    368  1.1  thorpej 
    369  1.1  thorpej 	if (clinfo->parent != root)
    370  1.1  thorpej 		return (QOPERR_CLASS_PERM);
    371  1.1  thorpej 
    372  1.1  thorpej 	if ((cl = clinfo->child) != NULL) {
    373  1.1  thorpej 		/* change child's parent to root, find the last child */
    374  1.1  thorpej 		while (cl->sibling != NULL) {
    375  1.1  thorpej 			cl->parent = root;
    376  1.1  thorpej 			cl = cl->sibling;
    377  1.1  thorpej 		}
    378  1.1  thorpej 		cl->parent = root;
    379  1.1  thorpej 
    380  1.1  thorpej 		/* move children to siblings */
    381  1.1  thorpej 		cl->sibling = clinfo->sibling;
    382  1.1  thorpej 		clinfo->sibling = cl;
    383  1.1  thorpej 		clinfo->child = NULL;
    384  1.1  thorpej 	}
    385  1.1  thorpej 
    386  1.1  thorpej 	error = qop_delete_class(clinfo);
    387  1.1  thorpej 
    388  1.1  thorpej 	if (error) {
    389  1.1  thorpej 		/* ick! restore the class tree */
    390  1.1  thorpej 		if (cl != NULL) {
    391  1.1  thorpej 			clinfo->child = clinfo->sibling;
    392  1.1  thorpej 			clinfo->sibling = cl->sibling;
    393  1.1  thorpej 			cl->sibling = NULL;
    394  1.1  thorpej 			/* restore parent field */
    395  1.1  thorpej 			for (cl = clinfo->child; cl != NULL; cl = cl->sibling)
    396  1.1  thorpej 				cl->parent = clinfo;
    397  1.1  thorpej 		}
    398  1.1  thorpej 	}
    399  1.1  thorpej 	return (error);
    400  1.1  thorpej }
    401  1.1  thorpej 
    402  1.1  thorpej int
    403  1.1  thorpej qop_cdnr_add_element(struct classinfo **rp, const char *cdnr_name,
    404  1.1  thorpej 		     struct ifinfo *ifinfo, struct tc_action *action)
    405  1.1  thorpej {
    406  1.1  thorpej 	struct classinfo *clinfo, *clist[2];
    407  1.1  thorpej 	struct cdnrinfo *cdnrinfo = NULL;
    408  1.1  thorpej 	int error;
    409  1.1  thorpej 
    410  1.1  thorpej 	if (action->tca_code == TCACODE_HANDLE) {
    411  1.1  thorpej 		clinfo = clhandle2clinfo(ifinfo, action->tca_handle);
    412  1.1  thorpej 		if (clinfo == NULL)
    413  1.1  thorpej 			return (QOPERR_BADCLASS);
    414  1.1  thorpej 		clist[0] = clinfo;
    415  1.1  thorpej 		clist[1] = NULL;
    416  1.1  thorpej #if 1
    417  1.1  thorpej 		/*
    418  1.1  thorpej 		 * if the conditioner referred to doesn't have a name,
    419  1.1  thorpej 		 * this is called just to add a name to it.
    420  1.1  thorpej 		 * we can simply add the name to the existing conditioner
    421  1.1  thorpej 		 * and return it.
    422  1.1  thorpej 		 */
    423  1.1  thorpej 		if (cdnr_name != NULL &&
    424  1.1  thorpej 		    strcmp(clinfo->clname, "(null)") == 0) {
    425  1.1  thorpej 			free(clinfo->clname);
    426  1.1  thorpej 			clinfo->clname = strdup(cdnr_name);
    427  1.1  thorpej 			if (rp != NULL)
    428  1.1  thorpej 				*rp = clinfo;
    429  1.1  thorpej 			return (0);
    430  1.1  thorpej 		}
    431  1.1  thorpej #endif
    432  1.1  thorpej 	} else
    433  1.1  thorpej 		clist[0] = NULL;
    434  1.1  thorpej 
    435  1.1  thorpej 	if ((cdnrinfo = calloc(1, sizeof(*cdnrinfo))) == NULL)
    436  1.1  thorpej 		return (QOPERR_NOMEM);
    437  1.1  thorpej 
    438  1.1  thorpej 	cdnrinfo->tce_type = TCETYPE_ELEMENT;
    439  1.1  thorpej 	cdnrinfo->tce_un.element.action = *action;
    440  1.1  thorpej 
    441  1.1  thorpej 	if ((error = qop_add_cdnr(&clinfo, cdnr_name, ifinfo, clist,
    442  1.1  thorpej 				  cdnrinfo)) != 0)
    443  1.1  thorpej 		goto err_ret;
    444  1.1  thorpej 
    445  1.1  thorpej 	if (rp != NULL)
    446  1.1  thorpej 		*rp = clinfo;
    447  1.1  thorpej 	return (0);
    448  1.1  thorpej 
    449  1.1  thorpej  err_ret:
    450  1.1  thorpej 	if (cdnrinfo != NULL)
    451  1.1  thorpej 		free(cdnrinfo);
    452  1.1  thorpej 	return (error);
    453  1.1  thorpej }
    454  1.1  thorpej 
    455  1.1  thorpej int
    456  1.1  thorpej qop_cdnr_add_tbmeter(struct classinfo **rp, const char *cdnr_name,
    457  1.1  thorpej 		     struct ifinfo *ifinfo,
    458  1.1  thorpej 		     struct tb_profile *profile,
    459  1.1  thorpej 		     struct tc_action *in_action,
    460  1.1  thorpej 		     struct tc_action *out_action)
    461  1.1  thorpej {
    462  1.1  thorpej 	struct classinfo *clinfo, *clist[3];
    463  1.1  thorpej 	struct cdnrinfo *cdnrinfo = NULL;
    464  1.1  thorpej 	int n, error;
    465  1.1  thorpej 
    466  1.1  thorpej 	n = 0;
    467  1.1  thorpej 	if (in_action->tca_code == TCACODE_HANDLE) {
    468  1.1  thorpej 		clist[n] = clhandle2clinfo(ifinfo, in_action->tca_handle);
    469  1.1  thorpej 		if (clist[n] == NULL)
    470  1.1  thorpej 			return (QOPERR_BADCLASS);
    471  1.1  thorpej 		n++;
    472  1.1  thorpej 	}
    473  1.1  thorpej 	if (out_action->tca_code == TCACODE_HANDLE) {
    474  1.1  thorpej 		clist[n] = clhandle2clinfo(ifinfo, out_action->tca_handle);
    475  1.1  thorpej 		if (clist[n] == NULL)
    476  1.1  thorpej 			return (QOPERR_BADCLASS);
    477  1.1  thorpej 		n++;
    478  1.1  thorpej 	}
    479  1.1  thorpej 	clist[n] = NULL;
    480  1.1  thorpej 
    481  1.1  thorpej 	if ((cdnrinfo = calloc(1, sizeof(*cdnrinfo))) == NULL)
    482  1.1  thorpej 		return (QOPERR_NOMEM);
    483  1.1  thorpej 
    484  1.1  thorpej 	cdnrinfo->tce_type = TCETYPE_TBMETER;
    485  1.1  thorpej 	cdnrinfo->tce_un.tbmeter.profile = *profile;
    486  1.1  thorpej 	cdnrinfo->tce_un.tbmeter.in_action = *in_action;
    487  1.1  thorpej 	cdnrinfo->tce_un.tbmeter.out_action = *out_action;
    488  1.1  thorpej 
    489  1.1  thorpej 	if ((error = qop_add_cdnr(&clinfo, cdnr_name, ifinfo, clist,
    490  1.1  thorpej 				   cdnrinfo)) != 0)
    491  1.1  thorpej 		goto err_ret;
    492  1.1  thorpej 
    493  1.1  thorpej 	if (rp != NULL)
    494  1.1  thorpej 		*rp = clinfo;
    495  1.1  thorpej 	return (0);
    496  1.1  thorpej 
    497  1.1  thorpej  err_ret:
    498  1.1  thorpej 	if (cdnrinfo != NULL)
    499  1.1  thorpej 		free(cdnrinfo);
    500  1.1  thorpej 	return (error);
    501  1.1  thorpej }
    502  1.1  thorpej 
    503  1.1  thorpej int
    504  1.1  thorpej qop_cdnr_modify_tbmeter(struct classinfo *clinfo, struct tb_profile *profile)
    505  1.1  thorpej {
    506  1.1  thorpej 	struct cdnrinfo *cdnrinfo = clinfo->private;
    507  1.1  thorpej 
    508  1.1  thorpej 	if (cdnrinfo->tce_type != TCETYPE_TBMETER)
    509  1.1  thorpej 		return (QOPERR_CLASS_INVAL);
    510  1.1  thorpej 	cdnrinfo->tce_un.tbmeter.profile = *profile;
    511  1.1  thorpej 
    512  1.1  thorpej 	return qop_modify_class(clinfo, NULL);
    513  1.1  thorpej }
    514  1.1  thorpej 
    515  1.1  thorpej int
    516  1.1  thorpej qop_cdnr_add_trtcm(struct classinfo **rp, const char *cdnr_name,
    517  1.1  thorpej 		   struct ifinfo *ifinfo,
    518  1.1  thorpej 		   struct tb_profile *cmtd_profile,
    519  1.1  thorpej 		   struct tb_profile *peak_profile,
    520  1.1  thorpej 		   struct tc_action *green_action,
    521  1.1  thorpej 		   struct tc_action *yellow_action,
    522  1.1  thorpej 		   struct tc_action *red_action, int coloraware)
    523  1.1  thorpej {
    524  1.1  thorpej 	struct classinfo *clinfo, *clist[4];
    525  1.1  thorpej 	struct cdnrinfo *cdnrinfo = NULL;
    526  1.1  thorpej 	int n, error;
    527  1.1  thorpej 
    528  1.1  thorpej 	n = 0;
    529  1.1  thorpej 	if (green_action->tca_code == TCACODE_HANDLE) {
    530  1.1  thorpej 		clist[n] = clhandle2clinfo(ifinfo, green_action->tca_handle);
    531  1.1  thorpej 		if (clist[n] == NULL)
    532  1.1  thorpej 			return (QOPERR_BADCLASS);
    533  1.1  thorpej 		n++;
    534  1.1  thorpej 	}
    535  1.1  thorpej 	if (yellow_action->tca_code == TCACODE_HANDLE) {
    536  1.1  thorpej 		clist[n] = clhandle2clinfo(ifinfo, yellow_action->tca_handle);
    537  1.1  thorpej 		if (clist[n] == NULL)
    538  1.1  thorpej 			return (QOPERR_BADCLASS);
    539  1.1  thorpej 		n++;
    540  1.1  thorpej 	}
    541  1.1  thorpej 	if (red_action->tca_code == TCACODE_HANDLE) {
    542  1.1  thorpej 		clist[n] = clhandle2clinfo(ifinfo, yellow_action->tca_handle);
    543  1.1  thorpej 		if (clist[n] == NULL)
    544  1.1  thorpej 			return (QOPERR_BADCLASS);
    545  1.1  thorpej 		n++;
    546  1.1  thorpej 	}
    547  1.1  thorpej 	clist[n] = NULL;
    548  1.1  thorpej 
    549  1.1  thorpej 	if ((cdnrinfo = calloc(1, sizeof(*cdnrinfo))) == NULL)
    550  1.1  thorpej 		return (QOPERR_NOMEM);
    551  1.1  thorpej 
    552  1.1  thorpej 	cdnrinfo->tce_type = TCETYPE_TRTCM;
    553  1.1  thorpej 	cdnrinfo->tce_un.trtcm.cmtd_profile = *cmtd_profile;
    554  1.1  thorpej 	cdnrinfo->tce_un.trtcm.peak_profile = *peak_profile;
    555  1.1  thorpej 	cdnrinfo->tce_un.trtcm.green_action = *green_action;
    556  1.1  thorpej 	cdnrinfo->tce_un.trtcm.yellow_action = *yellow_action;
    557  1.1  thorpej 	cdnrinfo->tce_un.trtcm.red_action = *red_action;
    558  1.1  thorpej 	cdnrinfo->tce_un.trtcm.coloraware = coloraware;
    559  1.1  thorpej 
    560  1.1  thorpej 	if ((error = qop_add_cdnr(&clinfo, cdnr_name, ifinfo, clist,
    561  1.1  thorpej 				  cdnrinfo)) != 0)
    562  1.1  thorpej 		goto err_ret;
    563  1.1  thorpej 
    564  1.1  thorpej 	if (rp != NULL)
    565  1.1  thorpej 		*rp = clinfo;
    566  1.1  thorpej 	return (0);
    567  1.1  thorpej 
    568  1.1  thorpej  err_ret:
    569  1.1  thorpej 	if (cdnrinfo != NULL)
    570  1.1  thorpej 		free(cdnrinfo);
    571  1.1  thorpej 	return (error);
    572  1.1  thorpej }
    573  1.1  thorpej 
    574  1.1  thorpej int
    575  1.1  thorpej qop_cdnr_modify_trtcm(struct classinfo *clinfo,
    576  1.1  thorpej 		      struct tb_profile *cmtd_profile,
    577  1.1  thorpej 		      struct tb_profile *peak_profile, int coloraware)
    578  1.1  thorpej {
    579  1.1  thorpej 	struct cdnrinfo *cdnrinfo = clinfo->private;
    580  1.1  thorpej 
    581  1.1  thorpej 	if (cdnrinfo->tce_type != TCETYPE_TRTCM)
    582  1.1  thorpej 		return (QOPERR_CLASS_INVAL);
    583  1.1  thorpej 	cdnrinfo->tce_un.trtcm.cmtd_profile = *cmtd_profile;
    584  1.1  thorpej 	cdnrinfo->tce_un.trtcm.peak_profile = *peak_profile;
    585  1.1  thorpej 	cdnrinfo->tce_un.trtcm.coloraware = coloraware;
    586  1.1  thorpej 
    587  1.1  thorpej 	return qop_modify_class(clinfo, NULL);
    588  1.1  thorpej }
    589  1.1  thorpej 
    590  1.1  thorpej int
    591  1.1  thorpej qop_cdnr_add_tswtcm(struct classinfo **rp, const char *cdnr_name,
    592  1.1  thorpej 		    struct ifinfo *ifinfo, const u_int32_t cmtd_rate,
    593  1.1  thorpej 		    const u_int32_t peak_rate, const u_int32_t avg_interval,
    594  1.1  thorpej 		    struct tc_action *green_action,
    595  1.1  thorpej 		    struct tc_action *yellow_action,
    596  1.1  thorpej 		    struct tc_action *red_action)
    597  1.1  thorpej {
    598  1.1  thorpej 	struct classinfo *clinfo, *clist[4];
    599  1.1  thorpej 	struct cdnrinfo *cdnrinfo = NULL;
    600  1.1  thorpej 	int n, error;
    601  1.1  thorpej 
    602  1.1  thorpej 	n = 0;
    603  1.1  thorpej 	if (green_action->tca_code == TCACODE_HANDLE) {
    604  1.1  thorpej 		clist[n] = clhandle2clinfo(ifinfo, green_action->tca_handle);
    605  1.1  thorpej 		if (clist[n] == NULL)
    606  1.1  thorpej 			return (QOPERR_BADCLASS);
    607  1.1  thorpej 		n++;
    608  1.1  thorpej 	}
    609  1.1  thorpej 	if (yellow_action->tca_code == TCACODE_HANDLE) {
    610  1.1  thorpej 		clist[n] = clhandle2clinfo(ifinfo, yellow_action->tca_handle);
    611  1.1  thorpej 		if (clist[n] == NULL)
    612  1.1  thorpej 			return (QOPERR_BADCLASS);
    613  1.1  thorpej 		n++;
    614  1.1  thorpej 	}
    615  1.1  thorpej 	if (red_action->tca_code == TCACODE_HANDLE) {
    616  1.1  thorpej 		clist[n] = clhandle2clinfo(ifinfo, yellow_action->tca_handle);
    617  1.1  thorpej 		if (clist[n] == NULL)
    618  1.1  thorpej 			return (QOPERR_BADCLASS);
    619  1.1  thorpej 		n++;
    620  1.1  thorpej 	}
    621  1.1  thorpej 	clist[n] = NULL;
    622  1.1  thorpej 
    623  1.1  thorpej 	if ((cdnrinfo = calloc(1, sizeof(*cdnrinfo))) == NULL)
    624  1.1  thorpej 		return (QOPERR_NOMEM);
    625  1.1  thorpej 
    626  1.1  thorpej 	cdnrinfo->tce_type = TCETYPE_TSWTCM;
    627  1.1  thorpej 	cdnrinfo->tce_un.tswtcm.cmtd_rate = cmtd_rate;
    628  1.1  thorpej 	cdnrinfo->tce_un.tswtcm.peak_rate = peak_rate;
    629  1.1  thorpej 	cdnrinfo->tce_un.tswtcm.avg_interval = avg_interval;
    630  1.1  thorpej 	cdnrinfo->tce_un.tswtcm.green_action = *green_action;
    631  1.1  thorpej 	cdnrinfo->tce_un.tswtcm.yellow_action = *yellow_action;
    632  1.1  thorpej 	cdnrinfo->tce_un.tswtcm.red_action = *red_action;
    633  1.1  thorpej 
    634  1.1  thorpej 	if ((error = qop_add_cdnr(&clinfo, cdnr_name, ifinfo, clist,
    635  1.1  thorpej 				  cdnrinfo)) != 0)
    636  1.1  thorpej 		goto err_ret;
    637  1.1  thorpej 
    638  1.1  thorpej 	if (rp != NULL)
    639  1.1  thorpej 		*rp = clinfo;
    640  1.1  thorpej 	return (0);
    641  1.1  thorpej 
    642  1.1  thorpej  err_ret:
    643  1.1  thorpej 	if (cdnrinfo != NULL)
    644  1.1  thorpej 		free(cdnrinfo);
    645  1.1  thorpej 	return (error);
    646  1.1  thorpej }
    647  1.1  thorpej 
    648  1.1  thorpej int
    649  1.1  thorpej qop_cdnr_modify_tswtcm(struct classinfo *clinfo, const u_int32_t cmtd_rate,
    650  1.1  thorpej 		       const u_int32_t peak_rate, const u_int32_t avg_interval)
    651  1.1  thorpej {
    652  1.1  thorpej 	struct cdnrinfo *cdnrinfo = clinfo->private;
    653  1.1  thorpej 
    654  1.1  thorpej 	if (cdnrinfo->tce_type != TCETYPE_TSWTCM)
    655  1.1  thorpej 		return (QOPERR_CLASS_INVAL);
    656  1.1  thorpej 	cdnrinfo->tce_un.tswtcm.cmtd_rate = cmtd_rate;
    657  1.1  thorpej 	cdnrinfo->tce_un.tswtcm.peak_rate = peak_rate;
    658  1.1  thorpej 	cdnrinfo->tce_un.tswtcm.avg_interval = avg_interval;
    659  1.1  thorpej 
    660  1.1  thorpej 	return qop_modify_class(clinfo, NULL);
    661  1.1  thorpej }
    662  1.1  thorpej 
    663  1.1  thorpej /*
    664  1.1  thorpej  *  system call interfaces for qdisc_ops
    665  1.1  thorpej  */
    666  1.1  thorpej static int
    667  1.1  thorpej cdnr_attach(struct ifinfo *ifinfo)
    668  1.1  thorpej {
    669  1.1  thorpej 	struct cdnr_interface iface;
    670  1.1  thorpej 
    671  1.1  thorpej 	if (cdnr_fd < 0 &&
    672  1.1  thorpej 	    (cdnr_fd = open(CDNR_DEVICE, O_RDWR)) < 0 &&
    673  1.1  thorpej 	    (cdnr_fd = open_module(CDNR_DEVICE, O_RDWR)) < 0) {
    674  1.4   itojun 		LOG(LOG_ERR, errno, "CDNR open");
    675  1.1  thorpej 		return (QOPERR_SYSCALL);
    676  1.1  thorpej 	}
    677  1.1  thorpej 
    678  1.1  thorpej 	cdnr_refcount++;
    679  1.1  thorpej 	memset(&iface, 0, sizeof(iface));
    680  1.1  thorpej 	strncpy(iface.cdnr_ifname, ifinfo->ifname+1, IFNAMSIZ);
    681  1.1  thorpej 
    682  1.1  thorpej 	if (ioctl(cdnr_fd, CDNR_IF_ATTACH, &iface) < 0)
    683  1.1  thorpej 		return (QOPERR_SYSCALL);
    684  1.1  thorpej #if 1
    685  1.4   itojun 	LOG(LOG_INFO, 0, "conditioner attached to %s", iface.cdnr_ifname);
    686  1.1  thorpej #endif
    687  1.1  thorpej 	return (0);
    688  1.1  thorpej }
    689  1.1  thorpej 
    690  1.1  thorpej static int
    691  1.1  thorpej cdnr_detach(struct ifinfo *ifinfo)
    692  1.1  thorpej {
    693  1.1  thorpej 	struct cdnr_interface iface;
    694  1.1  thorpej 
    695  1.1  thorpej 	memset(&iface, 0, sizeof(iface));
    696  1.1  thorpej 	strncpy(iface.cdnr_ifname, ifinfo->ifname+1, IFNAMSIZ);
    697  1.1  thorpej 
    698  1.1  thorpej 	if (ioctl(cdnr_fd, CDNR_IF_DETACH, &iface) < 0)
    699  1.1  thorpej 		return (QOPERR_SYSCALL);
    700  1.1  thorpej 
    701  1.1  thorpej 	if (--cdnr_refcount == 0) {
    702  1.1  thorpej 		close(cdnr_fd);
    703  1.1  thorpej 		cdnr_fd = -1;
    704  1.1  thorpej 	}
    705  1.1  thorpej 	return (0);
    706  1.1  thorpej }
    707  1.1  thorpej 
    708  1.1  thorpej static int
    709  1.1  thorpej cdnr_enable(struct ifinfo *ifinfo)
    710  1.1  thorpej {
    711  1.1  thorpej 	struct cdnr_interface iface;
    712  1.1  thorpej 
    713  1.1  thorpej 	memset(&iface, 0, sizeof(iface));
    714  1.1  thorpej 	strncpy(iface.cdnr_ifname, ifinfo->ifname+1, IFNAMSIZ);
    715  1.1  thorpej 
    716  1.1  thorpej 	if (ioctl(cdnr_fd, CDNR_ENABLE, &iface) < 0)
    717  1.1  thorpej 		return (QOPERR_SYSCALL);
    718  1.1  thorpej 	return (0);
    719  1.1  thorpej }
    720  1.1  thorpej 
    721  1.1  thorpej static int
    722  1.1  thorpej cdnr_disable(struct ifinfo *ifinfo)
    723  1.1  thorpej {
    724  1.1  thorpej 	struct cdnr_interface iface;
    725  1.1  thorpej 
    726  1.1  thorpej 	memset(&iface, 0, sizeof(iface));
    727  1.1  thorpej 	strncpy(iface.cdnr_ifname, ifinfo->ifname+1, IFNAMSIZ);
    728  1.1  thorpej 
    729  1.1  thorpej 	if (ioctl(cdnr_fd, CDNR_DISABLE, &iface) < 0)
    730  1.1  thorpej 		return (QOPERR_SYSCALL);
    731  1.1  thorpej 	return (0);
    732  1.1  thorpej }
    733  1.1  thorpej 
    734  1.1  thorpej static int
    735  1.1  thorpej cdnr_add_class(struct classinfo *clinfo)
    736  1.1  thorpej {
    737  1.1  thorpej 	struct cdnr_add_element element_add;
    738  1.1  thorpej 	struct cdnr_add_tbmeter tbmeter_add;
    739  1.1  thorpej 	struct cdnr_add_trtcm   trtcm_add;
    740  1.1  thorpej 	struct cdnr_add_tswtcm  tswtcm_add;
    741  1.1  thorpej 	struct cdnrinfo *cdnrinfo;
    742  1.1  thorpej 
    743  1.1  thorpej 	cdnrinfo = clinfo->private;
    744  1.1  thorpej 
    745  1.1  thorpej 	/* root class is a dummy class */
    746  1.1  thorpej 	if (clinfo->parent == NULL) {
    747  1.1  thorpej 		clinfo->handle = 0;
    748  1.1  thorpej 		return (0);
    749  1.1  thorpej 	}
    750  1.1  thorpej 
    751  1.1  thorpej 	switch (cdnrinfo->tce_type) {
    752  1.1  thorpej 	case TCETYPE_ELEMENT:
    753  1.1  thorpej 		memset(&element_add, 0, sizeof(element_add));
    754  1.1  thorpej 		strncpy(element_add.iface.cdnr_ifname,
    755  1.1  thorpej 			clinfo->ifinfo->ifname+1, IFNAMSIZ);
    756  1.1  thorpej 		element_add.action = cdnrinfo->tce_un.element.action;
    757  1.1  thorpej 		if (ioctl(cdnr_fd, CDNR_ADD_ELEM, &element_add) < 0) {
    758  1.1  thorpej 			clinfo->handle = CDNR_NULL_HANDLE;
    759  1.1  thorpej 			return (QOPERR_SYSCALL);
    760  1.1  thorpej 		}
    761  1.1  thorpej 		clinfo->handle = element_add.cdnr_handle;
    762  1.1  thorpej 		break;
    763  1.1  thorpej 
    764  1.1  thorpej 	case TCETYPE_TBMETER:
    765  1.1  thorpej 		memset(&tbmeter_add, 0, sizeof(tbmeter_add));
    766  1.1  thorpej 		strncpy(tbmeter_add.iface.cdnr_ifname,
    767  1.1  thorpej 			clinfo->ifinfo->ifname+1, IFNAMSIZ);
    768  1.1  thorpej 		tbmeter_add.profile = cdnrinfo->tce_un.tbmeter.profile;
    769  1.1  thorpej 		tbmeter_add.in_action = cdnrinfo->tce_un.tbmeter.in_action;
    770  1.1  thorpej 		tbmeter_add.out_action = cdnrinfo->tce_un.tbmeter.out_action;
    771  1.1  thorpej 		if (ioctl(cdnr_fd, CDNR_ADD_TBM, &tbmeter_add) < 0) {
    772  1.1  thorpej 			clinfo->handle = CDNR_NULL_HANDLE;
    773  1.1  thorpej 			return (QOPERR_SYSCALL);
    774  1.1  thorpej 		}
    775  1.1  thorpej 		clinfo->handle = tbmeter_add.cdnr_handle;
    776  1.1  thorpej 		break;
    777  1.1  thorpej 
    778  1.1  thorpej 	case TCETYPE_TRTCM:
    779  1.1  thorpej 		memset(&trtcm_add, 0, sizeof(trtcm_add));
    780  1.1  thorpej 		strncpy(trtcm_add.iface.cdnr_ifname,
    781  1.1  thorpej 			clinfo->ifinfo->ifname+1, IFNAMSIZ);
    782  1.1  thorpej 		trtcm_add.cmtd_profile = cdnrinfo->tce_un.trtcm.cmtd_profile;
    783  1.1  thorpej 		trtcm_add.peak_profile = cdnrinfo->tce_un.trtcm.peak_profile;
    784  1.1  thorpej 		trtcm_add.green_action = cdnrinfo->tce_un.trtcm.green_action;
    785  1.1  thorpej 		trtcm_add.yellow_action = cdnrinfo->tce_un.trtcm.yellow_action;
    786  1.1  thorpej 		trtcm_add.red_action = cdnrinfo->tce_un.trtcm.red_action;
    787  1.1  thorpej 		trtcm_add.coloraware = cdnrinfo->tce_un.trtcm.coloraware;
    788  1.1  thorpej 		if (ioctl(cdnr_fd, CDNR_ADD_TCM, &trtcm_add) < 0) {
    789  1.1  thorpej 			clinfo->handle = CDNR_NULL_HANDLE;
    790  1.1  thorpej 			return (QOPERR_SYSCALL);
    791  1.1  thorpej 		}
    792  1.1  thorpej 		clinfo->handle = trtcm_add.cdnr_handle;
    793  1.1  thorpej 		break;
    794  1.1  thorpej 
    795  1.1  thorpej 	case TCETYPE_TSWTCM:
    796  1.1  thorpej 		memset(&tswtcm_add, 0, sizeof(tswtcm_add));
    797  1.1  thorpej 		strncpy(tswtcm_add.iface.cdnr_ifname,
    798  1.1  thorpej 			clinfo->ifinfo->ifname+1, IFNAMSIZ);
    799  1.1  thorpej 		tswtcm_add.cmtd_rate = cdnrinfo->tce_un.tswtcm.cmtd_rate;
    800  1.1  thorpej 		tswtcm_add.peak_rate = cdnrinfo->tce_un.tswtcm.peak_rate;
    801  1.1  thorpej 		tswtcm_add.avg_interval = cdnrinfo->tce_un.tswtcm.avg_interval;
    802  1.1  thorpej 		tswtcm_add.green_action = cdnrinfo->tce_un.tswtcm.green_action;
    803  1.1  thorpej 		tswtcm_add.yellow_action = cdnrinfo->tce_un.tswtcm.yellow_action;
    804  1.1  thorpej 		tswtcm_add.red_action = cdnrinfo->tce_un.tswtcm.red_action;
    805  1.1  thorpej 		if (ioctl(cdnr_fd, CDNR_ADD_TSW, &tswtcm_add) < 0) {
    806  1.1  thorpej 			clinfo->handle = CDNR_NULL_HANDLE;
    807  1.1  thorpej 			return (QOPERR_SYSCALL);
    808  1.1  thorpej 		}
    809  1.1  thorpej 		clinfo->handle = tswtcm_add.cdnr_handle;
    810  1.1  thorpej 		break;
    811  1.1  thorpej 
    812  1.1  thorpej 	default:
    813  1.1  thorpej 		return (QOPERR_CLASS_INVAL);
    814  1.1  thorpej 	}
    815  1.1  thorpej 	return (0);
    816  1.1  thorpej }
    817  1.1  thorpej 
    818  1.1  thorpej static int
    819  1.1  thorpej cdnr_modify_class(struct classinfo *clinfo, void *arg)
    820  1.1  thorpej {
    821  1.1  thorpej 	struct cdnr_modify_tbmeter tbmeter_modify;
    822  1.1  thorpej 	struct cdnr_modify_trtcm   trtcm_modify;
    823  1.1  thorpej 	struct cdnr_modify_tswtcm  tswtcm_modify;
    824  1.1  thorpej 	struct cdnrinfo *cdnrinfo;
    825  1.1  thorpej 
    826  1.1  thorpej 	cdnrinfo = clinfo->private;
    827  1.1  thorpej 
    828  1.1  thorpej 	switch (cdnrinfo->tce_type) {
    829  1.1  thorpej 	case TCETYPE_TBMETER:
    830  1.1  thorpej 		memset(&tbmeter_modify, 0, sizeof(tbmeter_modify));
    831  1.1  thorpej 		strncpy(tbmeter_modify.iface.cdnr_ifname,
    832  1.1  thorpej 			clinfo->ifinfo->ifname+1, IFNAMSIZ);
    833  1.1  thorpej 		tbmeter_modify.cdnr_handle = clinfo->handle;
    834  1.1  thorpej 		tbmeter_modify.profile = cdnrinfo->tce_un.tbmeter.profile;
    835  1.1  thorpej 		if (ioctl(cdnr_fd, CDNR_MOD_TBM, &tbmeter_modify) < 0)
    836  1.1  thorpej 			return (QOPERR_SYSCALL);
    837  1.1  thorpej 		break;
    838  1.1  thorpej 
    839  1.1  thorpej 	case TCETYPE_TRTCM:
    840  1.1  thorpej 		memset(&trtcm_modify, 0, sizeof(trtcm_modify));
    841  1.1  thorpej 		strncpy(trtcm_modify.iface.cdnr_ifname,
    842  1.1  thorpej 			clinfo->ifinfo->ifname+1, IFNAMSIZ);
    843  1.1  thorpej 		trtcm_modify.cdnr_handle = clinfo->handle;
    844  1.1  thorpej 		trtcm_modify.cmtd_profile =
    845  1.1  thorpej 			cdnrinfo->tce_un.trtcm.cmtd_profile;
    846  1.1  thorpej 		trtcm_modify.peak_profile =
    847  1.1  thorpej 			cdnrinfo->tce_un.trtcm.peak_profile;
    848  1.1  thorpej 		trtcm_modify.coloraware = cdnrinfo->tce_un.trtcm.coloraware;
    849  1.1  thorpej 		if (ioctl(cdnr_fd, CDNR_MOD_TCM, &trtcm_modify) < 0)
    850  1.1  thorpej 			return (QOPERR_SYSCALL);
    851  1.1  thorpej 		break;
    852  1.1  thorpej 
    853  1.1  thorpej 	case TCETYPE_TSWTCM:
    854  1.1  thorpej 		memset(&tswtcm_modify, 0, sizeof(tswtcm_modify));
    855  1.1  thorpej 		strncpy(tswtcm_modify.iface.cdnr_ifname,
    856  1.1  thorpej 			clinfo->ifinfo->ifname+1, IFNAMSIZ);
    857  1.1  thorpej 		tswtcm_modify.cdnr_handle = clinfo->handle;
    858  1.1  thorpej 		tswtcm_modify.cmtd_rate = cdnrinfo->tce_un.tswtcm.cmtd_rate;
    859  1.1  thorpej 		tswtcm_modify.peak_rate = cdnrinfo->tce_un.tswtcm.peak_rate;
    860  1.1  thorpej 		tswtcm_modify.avg_interval = cdnrinfo->tce_un.tswtcm.avg_interval;
    861  1.1  thorpej 		if (ioctl(cdnr_fd, CDNR_MOD_TSW, &tswtcm_modify) < 0)
    862  1.1  thorpej 			return (QOPERR_SYSCALL);
    863  1.1  thorpej 		break;
    864  1.1  thorpej 
    865  1.1  thorpej 	default:
    866  1.1  thorpej 		return (QOPERR_CLASS_INVAL);
    867  1.1  thorpej 	}
    868  1.1  thorpej 	return (0);
    869  1.1  thorpej }
    870  1.1  thorpej 
    871  1.1  thorpej static int
    872  1.1  thorpej cdnr_delete_class(struct classinfo *clinfo)
    873  1.1  thorpej {
    874  1.1  thorpej 	struct cdnr_delete_element element_delete;
    875  1.1  thorpej 
    876  1.1  thorpej 	if (clinfo->handle == CDNR_NULL_HANDLE)
    877  1.1  thorpej 		return (0);
    878  1.1  thorpej 
    879  1.1  thorpej 	memset(&element_delete, 0, sizeof(element_delete));
    880  1.1  thorpej 	strncpy(element_delete.iface.cdnr_ifname, clinfo->ifinfo->ifname+1,
    881  1.1  thorpej 		IFNAMSIZ);
    882  1.1  thorpej 	element_delete.cdnr_handle = clinfo->handle;
    883  1.1  thorpej 
    884  1.1  thorpej 	if (ioctl(cdnr_fd, CDNR_DEL_ELEM, &element_delete) < 0)
    885  1.1  thorpej 		return (QOPERR_SYSCALL);
    886  1.1  thorpej 	return (0);
    887  1.1  thorpej }
    888  1.1  thorpej 
    889  1.1  thorpej static int
    890  1.1  thorpej cdnr_add_filter(struct fltrinfo *fltrinfo)
    891  1.1  thorpej {
    892  1.1  thorpej 	struct cdnr_add_filter fltr_add;
    893  1.1  thorpej 
    894  1.1  thorpej 	memset(&fltr_add, 0, sizeof(fltr_add));
    895  1.1  thorpej 	strncpy(fltr_add.iface.cdnr_ifname,
    896  1.1  thorpej 		fltrinfo->clinfo->ifinfo->ifname+1, IFNAMSIZ);
    897  1.1  thorpej 	fltr_add.cdnr_handle = fltrinfo->clinfo->handle;
    898  1.1  thorpej 	fltr_add.filter = fltrinfo->fltr;
    899  1.1  thorpej 
    900  1.1  thorpej 	if (ioctl(cdnr_fd, CDNR_ADD_FILTER, &fltr_add) < 0)
    901  1.1  thorpej 		return (QOPERR_SYSCALL);
    902  1.1  thorpej 	fltrinfo->handle = fltr_add.filter_handle;
    903  1.1  thorpej 	return (0);
    904  1.1  thorpej }
    905  1.1  thorpej 
    906  1.1  thorpej static int
    907  1.1  thorpej cdnr_delete_filter(struct fltrinfo *fltrinfo)
    908  1.1  thorpej {
    909  1.1  thorpej 	struct cdnr_delete_filter fltr_del;
    910  1.1  thorpej 
    911  1.1  thorpej 	memset(&fltr_del, 0, sizeof(fltr_del));
    912  1.1  thorpej 	strncpy(fltr_del.iface.cdnr_ifname,
    913  1.1  thorpej 		fltrinfo->clinfo->ifinfo->ifname+1, IFNAMSIZ);
    914  1.1  thorpej 	fltr_del.filter_handle = fltrinfo->handle;
    915  1.1  thorpej 
    916  1.1  thorpej 	if (ioctl(cdnr_fd, CDNR_DEL_FILTER, &fltr_del) < 0)
    917  1.1  thorpej 		return (QOPERR_SYSCALL);
    918  1.1  thorpej 	return (0);
    919  1.1  thorpej }
    920  1.1  thorpej 
    921  1.1  thorpej 
    922  1.1  thorpej static int
    923  1.1  thorpej verify_tbprofile(struct tb_profile *profile, const char *cdnr_name)
    924  1.1  thorpej {
    925  1.1  thorpej 	if (profile->depth < 1500) {
    926  1.1  thorpej 		LOG(LOG_WARNING, 0,
    927  1.4   itojun 		    "warning: token bucket depth for %s is too small (%d)",
    928  1.1  thorpej 		    cdnr_name, profile->depth);
    929  1.1  thorpej 		return (-1);
    930  1.1  thorpej 	}
    931  1.1  thorpej 	return (0);
    932  1.1  thorpej }
    933  1.1  thorpej 
    934