xref: /onnv-gate/usr/src/uts/common/ipp/meters/tokenmtddi.c (revision 7862:f8b6a07acfd6)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7862SRichard.Bean@Sun.COM  * Common Development and Distribution License (the "License").
6*7862SRichard.Bean@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7862SRichard.Bean@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/atomic.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/socket.h>
300Sstevel@tonic-gate #include <netinet/in.h>
310Sstevel@tonic-gate #include <sys/modctl.h>
320Sstevel@tonic-gate #include <sys/sunddi.h>
330Sstevel@tonic-gate #include <ipp/ipp.h>
340Sstevel@tonic-gate #include <ipp/ipp_config.h>
350Sstevel@tonic-gate #include <inet/common.h>
360Sstevel@tonic-gate #include <ipp/meters/meter_impl.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #define	D_SM_COMMENT	"IPP Single-Two Rate Token Meter"
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /* DDI file for tokenmt ipp module */
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /* Default DSCP to colour mapping for colour-aware meter */
430Sstevel@tonic-gate enum meter_colour default_dscp_to_colour[64] = {
440Sstevel@tonic-gate 	TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN,
450Sstevel@tonic-gate 	TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN,
460Sstevel@tonic-gate 	TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN,
470Sstevel@tonic-gate 	TOKENMT_YELLOW, TOKENMT_GREEN, TOKENMT_RED, TOKENMT_GREEN,
480Sstevel@tonic-gate 	TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN,
490Sstevel@tonic-gate 	TOKENMT_YELLOW, TOKENMT_GREEN, TOKENMT_RED, TOKENMT_GREEN,
500Sstevel@tonic-gate 	TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN,
510Sstevel@tonic-gate 	TOKENMT_YELLOW, TOKENMT_GREEN, TOKENMT_RED, TOKENMT_GREEN,
520Sstevel@tonic-gate 	TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN,
530Sstevel@tonic-gate 	TOKENMT_YELLOW, TOKENMT_GREEN, TOKENMT_RED, TOKENMT_GREEN,
540Sstevel@tonic-gate 	TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN,
550Sstevel@tonic-gate 	TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN,
560Sstevel@tonic-gate 	TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN,
570Sstevel@tonic-gate 	TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN,
580Sstevel@tonic-gate 	TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN,
590Sstevel@tonic-gate 	TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN, TOKENMT_GREEN
600Sstevel@tonic-gate };
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static int tokenmt_create_action(ipp_action_id_t, nvlist_t **, ipp_flags_t);
630Sstevel@tonic-gate static int tokenmt_modify_action(ipp_action_id_t, nvlist_t **, ipp_flags_t);
640Sstevel@tonic-gate static int tokenmt_destroy_action(ipp_action_id_t, ipp_flags_t);
650Sstevel@tonic-gate static int tokenmt_info(ipp_action_id_t, int (*)(nvlist_t *, void *), void *,
660Sstevel@tonic-gate     ipp_flags_t);
670Sstevel@tonic-gate static int tokenmt_invoke_action(ipp_action_id_t, ipp_packet_t *);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /* Initialize stats */
700Sstevel@tonic-gate static int tokenmt_statinit(ipp_action_id_t, tokenmt_data_t *);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /* Stats callback function */
730Sstevel@tonic-gate static int tokenmt_update_stats(ipp_stat_t *, void *, int);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate ipp_ops_t tokenmt_ops = {
760Sstevel@tonic-gate 	IPPO_REV,
770Sstevel@tonic-gate 	tokenmt_create_action,	/* ippo_action_create */
780Sstevel@tonic-gate 	tokenmt_modify_action,	/* ippo_action_modify */
790Sstevel@tonic-gate 	tokenmt_destroy_action,	/* ippo_action_destroy */
800Sstevel@tonic-gate 	tokenmt_info,		/* ippo_action_info */
810Sstevel@tonic-gate 	tokenmt_invoke_action	/* ippo_action_invoke */
820Sstevel@tonic-gate };
830Sstevel@tonic-gate 
840Sstevel@tonic-gate extern struct mod_ops mod_ippops;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * Module linkage information for the kernel.
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate static struct modlipp modlipp = {
900Sstevel@tonic-gate 	&mod_ippops,
91*7862SRichard.Bean@Sun.COM 	D_SM_COMMENT,
920Sstevel@tonic-gate 	&tokenmt_ops
930Sstevel@tonic-gate };
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static struct modlinkage modlinkage = {
960Sstevel@tonic-gate 	MODREV_1,
970Sstevel@tonic-gate 	(void *)&modlipp,
980Sstevel@tonic-gate 	NULL
990Sstevel@tonic-gate };
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate int
_init(void)1030Sstevel@tonic-gate _init(void)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate int
_fini(void)1090Sstevel@tonic-gate _fini(void)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1150Sstevel@tonic-gate _info(struct modinfo *modinfop)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /* ARGSUSED */
1210Sstevel@tonic-gate static int
tokenmt_create_action(ipp_action_id_t aid,nvlist_t ** nvlpp,ipp_flags_t flags)1220Sstevel@tonic-gate tokenmt_create_action(ipp_action_id_t aid, nvlist_t **nvlpp, ipp_flags_t flags)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	nvlist_t *nvlp;
1250Sstevel@tonic-gate 	tokenmt_data_t *tokenmt_data;
1260Sstevel@tonic-gate 	char *next_action;
1270Sstevel@tonic-gate 	tokenmt_cfg_t *cfg_parms;
1280Sstevel@tonic-gate 	uint32_t mode;
1290Sstevel@tonic-gate 	uint32_t bstats;
1300Sstevel@tonic-gate 	int rc, rc2;
1310Sstevel@tonic-gate 	int32_t *colour_tbl;
1320Sstevel@tonic-gate 	uint_t nelem = 64;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	nvlp = *nvlpp;
1350Sstevel@tonic-gate 	*nvlpp = NULL;		/* nvlist should be NULL on return */
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	if ((cfg_parms = kmem_zalloc(TOKENMT_CFG_SZ, KM_NOSLEEP)) == NULL) {
1380Sstevel@tonic-gate 		nvlist_free(nvlp);
1390Sstevel@tonic-gate 		return (ENOMEM);
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/* parse red next action name */
1430Sstevel@tonic-gate 	if ((rc = nvlist_lookup_string(nvlp, TOKENMT_RED_ACTION_NAME,
1440Sstevel@tonic-gate 	    &next_action)) != 0) {
1450Sstevel@tonic-gate 		nvlist_free(nvlp);
1460Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_create_action:invalid config, red "\
1470Sstevel@tonic-gate 		    "action name missing\n"));
1480Sstevel@tonic-gate 		kmem_free(cfg_parms, TOKENMT_CFG_SZ);
1490Sstevel@tonic-gate 		return (rc);
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 	if ((cfg_parms->red_action = ipp_action_lookup(next_action))
1520Sstevel@tonic-gate 	    == IPP_ACTION_INVAL) {
1530Sstevel@tonic-gate 		nvlist_free(nvlp);
1540Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_create_action: red action invalid\n"));
1550Sstevel@tonic-gate 		kmem_free(cfg_parms, TOKENMT_CFG_SZ);
1560Sstevel@tonic-gate 		return (EINVAL);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	/* parse yellow next action name, if present  this is Two Rate meter */
1600Sstevel@tonic-gate 	if ((rc = nvlist_lookup_string(nvlp, TOKENMT_YELLOW_ACTION_NAME,
1610Sstevel@tonic-gate 	    &next_action)) == 0) {
1620Sstevel@tonic-gate 		if ((cfg_parms->yellow_action = ipp_action_lookup(next_action))
1630Sstevel@tonic-gate 		    == IPP_ACTION_INVAL) {
1640Sstevel@tonic-gate 			nvlist_free(nvlp);
1650Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_create_action: yellow action "\
1660Sstevel@tonic-gate 			    "invalid\n"));
1670Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
1680Sstevel@tonic-gate 			return (EINVAL);
1690Sstevel@tonic-gate 		}
1700Sstevel@tonic-gate 	} else {
1710Sstevel@tonic-gate 		cfg_parms->yellow_action = TOKENMT_NO_ACTION;
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	/* parse green next action name */
1750Sstevel@tonic-gate 	if ((rc = nvlist_lookup_string(nvlp, TOKENMT_GREEN_ACTION_NAME,
1760Sstevel@tonic-gate 	    &next_action)) != 0) {
1770Sstevel@tonic-gate 		nvlist_free(nvlp);
1780Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_create_action:invalid config, green " \
1790Sstevel@tonic-gate 		    "action name missing\n"));
1800Sstevel@tonic-gate 		kmem_free(cfg_parms, TOKENMT_CFG_SZ);
1810Sstevel@tonic-gate 		return (rc);
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 	if ((cfg_parms->green_action = ipp_action_lookup(next_action))
1840Sstevel@tonic-gate 	    == IPP_ACTION_INVAL) {
1850Sstevel@tonic-gate 		nvlist_free(nvlp);
1860Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_create_action: green action invalid\n"));
1870Sstevel@tonic-gate 		kmem_free(cfg_parms, TOKENMT_CFG_SZ);
1880Sstevel@tonic-gate 		return (EINVAL);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	/* parse committed rate  - in kilo bits / sec */
1920Sstevel@tonic-gate 	if ((rc = nvlist_lookup_uint32(nvlp, TOKENMT_COMMITTED_RATE,
1930Sstevel@tonic-gate 	    &cfg_parms->committed_rate)) != 0) {
1940Sstevel@tonic-gate 		nvlist_free(nvlp);
1950Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_create_action: invalid config, "\
1960Sstevel@tonic-gate 		    " committed rate missing\n"));
1970Sstevel@tonic-gate 		kmem_free(cfg_parms, TOKENMT_CFG_SZ);
1980Sstevel@tonic-gate 		return (rc);
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 	if (cfg_parms->committed_rate == 0) {
2010Sstevel@tonic-gate 		nvlist_free(nvlp);
2020Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_create_action: invalid committed rate, "\
2030Sstevel@tonic-gate 		    "%u\n", cfg_parms->committed_rate));
2040Sstevel@tonic-gate 		kmem_free(cfg_parms, TOKENMT_CFG_SZ);
2050Sstevel@tonic-gate 		return (EINVAL);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/* parse committed burst in bits */
2090Sstevel@tonic-gate 	if ((rc = nvlist_lookup_uint32(nvlp, TOKENMT_COMMITTED_BURST,
2100Sstevel@tonic-gate 	    &cfg_parms->committed_burst)) != 0) {
2110Sstevel@tonic-gate 		nvlist_free(nvlp);
2120Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_create_action: invalid config, "\
2130Sstevel@tonic-gate 		    " committed burst missing\n"));
2140Sstevel@tonic-gate 		kmem_free(cfg_parms, TOKENMT_CFG_SZ);
2150Sstevel@tonic-gate 		return (rc);
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	/*
2200Sstevel@tonic-gate 	 * If the peak burst size is specified, make sure we have the
2210Sstevel@tonic-gate 	 * yellow action.
2220Sstevel@tonic-gate 	 */
2230Sstevel@tonic-gate 	if ((rc = nvlist_lookup_uint32(nvlp, TOKENMT_PEAK_BURST,
2240Sstevel@tonic-gate 	    &cfg_parms->peak_burst)) == 0) {
2250Sstevel@tonic-gate 		if (cfg_parms->yellow_action == TOKENMT_NO_ACTION) {
2260Sstevel@tonic-gate 			nvlist_free(nvlp);
2270Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_create_action: peak burst "\
2280Sstevel@tonic-gate 			    "specified without yellow action\n"));
2290Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
2300Sstevel@tonic-gate 			return (EINVAL);
2310Sstevel@tonic-gate 		}
2320Sstevel@tonic-gate 	} else if (cfg_parms->yellow_action != TOKENMT_NO_ACTION) {
2330Sstevel@tonic-gate 		nvlist_free(nvlp);
2340Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_create_action: peak burst must be "\
2350Sstevel@tonic-gate 		    "provided with yellow action\n"));
2360Sstevel@tonic-gate 		kmem_free(cfg_parms, TOKENMT_CFG_SZ);
2370Sstevel@tonic-gate 		return (EINVAL);
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/* Check if we have a peak_rate */
2410Sstevel@tonic-gate 	if ((rc = nvlist_lookup_uint32(nvlp, TOKENMT_PEAK_RATE,
2420Sstevel@tonic-gate 	    &cfg_parms->peak_rate)) == 0) {
2430Sstevel@tonic-gate 		if (cfg_parms->yellow_action == TOKENMT_NO_ACTION) {
2440Sstevel@tonic-gate 			nvlist_free(nvlp);
2450Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_create_action: peak rate "\
2460Sstevel@tonic-gate 			    "specified without yellow action\n"));
2470Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
2480Sstevel@tonic-gate 			return (EINVAL);
2490Sstevel@tonic-gate 		} else if ((cfg_parms->peak_rate == 0) ||
2500Sstevel@tonic-gate 		    (cfg_parms->peak_rate < cfg_parms->committed_rate)) {
2510Sstevel@tonic-gate 			nvlist_free(nvlp);
2520Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_create_action: invalid "\
2530Sstevel@tonic-gate 			    "peak rate, %u\n", cfg_parms->peak_rate));
2540Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
2550Sstevel@tonic-gate 			return (EINVAL);
2560Sstevel@tonic-gate 		}
2570Sstevel@tonic-gate 		cfg_parms->tokenmt_type = TRTCL_TOKENMT;
2580Sstevel@tonic-gate 	} else {
2590Sstevel@tonic-gate 		cfg_parms->tokenmt_type = SRTCL_TOKENMT;
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/* Validate the committed and peak burst size */
2630Sstevel@tonic-gate 	if (cfg_parms->tokenmt_type == SRTCL_TOKENMT) {
2640Sstevel@tonic-gate 		if ((cfg_parms->committed_burst == 0) &&
2650Sstevel@tonic-gate 		    (cfg_parms->peak_burst == 0)) {
2660Sstevel@tonic-gate 			nvlist_free(nvlp);
2670Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_create_action: at least one "\
2680Sstevel@tonic-gate 			    "burst size must be non-zero\n"));
2690Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
2700Sstevel@tonic-gate 			return (EINVAL);
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate 	} else {	/* TRTCL_TOKENMT */
2730Sstevel@tonic-gate 		if ((cfg_parms->committed_burst == 0) ||
2740Sstevel@tonic-gate 		    (cfg_parms->peak_burst == 0)) {
2750Sstevel@tonic-gate 			nvlist_free(nvlp);
2760Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_create_action: both the "\
2770Sstevel@tonic-gate 			    "burst sizes must be non-zero\n"));
2780Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
2790Sstevel@tonic-gate 			return (EINVAL);
2800Sstevel@tonic-gate 		}
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	/* just copy default colour mapping */
2840Sstevel@tonic-gate 	bcopy(default_dscp_to_colour, cfg_parms->dscp_to_colour,
2850Sstevel@tonic-gate 	    sizeof (default_dscp_to_colour));
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	/* parse mode, if present */
2880Sstevel@tonic-gate 	if ((rc = nvlist_lookup_uint32(nvlp, TOKENMT_COLOUR_AWARE,
2890Sstevel@tonic-gate 	    &mode)) != 0) {
2900Sstevel@tonic-gate 		cfg_parms->colour_aware = B_FALSE;
2910Sstevel@tonic-gate 	} else {
2920Sstevel@tonic-gate 		cfg_parms->colour_aware = (mode == 0) ? B_FALSE : B_TRUE;
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	/* Get the dscp to colour mapping array */
2960Sstevel@tonic-gate 	if (cfg_parms->colour_aware) {
2970Sstevel@tonic-gate 		if ((rc = nvlist_lookup_int32_array(nvlp,
2980Sstevel@tonic-gate 		    TOKENMT_COLOUR_MAP, &colour_tbl, &nelem)) == 0) {
2990Sstevel@tonic-gate 			int count;
3000Sstevel@tonic-gate 			for (count = 0; count < 64; count++) {
3010Sstevel@tonic-gate 				if (colour_tbl[count] == -1)
3020Sstevel@tonic-gate 					continue;
3030Sstevel@tonic-gate 				cfg_parms->dscp_to_colour[count] =
3040Sstevel@tonic-gate 				    colour_tbl[count];
3050Sstevel@tonic-gate 			}
3060Sstevel@tonic-gate 		}
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	/* parse stats */
3100Sstevel@tonic-gate 	if ((rc = nvlist_lookup_uint32(nvlp, IPP_ACTION_STATS_ENABLE, &bstats))
3110Sstevel@tonic-gate 	    != 0) {
3120Sstevel@tonic-gate 		cfg_parms->stats = B_FALSE;
3130Sstevel@tonic-gate 	} else {
3140Sstevel@tonic-gate 		cfg_parms->stats = (bstats == 0) ? B_FALSE : B_TRUE;
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	nvlist_free(nvlp);
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	/* Initialize other stuff */
3200Sstevel@tonic-gate 	tokenmt_data = kmem_zalloc(TOKENMT_DATA_SZ, KM_NOSLEEP);
3210Sstevel@tonic-gate 	if (tokenmt_data == NULL) {
3220Sstevel@tonic-gate 		kmem_free(cfg_parms, TOKENMT_CFG_SZ);
3230Sstevel@tonic-gate 		return (ENOMEM);
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	/* Initialize stats, if required */
3270Sstevel@tonic-gate 	if (cfg_parms->stats) {
3280Sstevel@tonic-gate 		if ((rc = tokenmt_statinit(aid, tokenmt_data)) != 0) {
3290Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
3300Sstevel@tonic-gate 			kmem_free(tokenmt_data, TOKENMT_DATA_SZ);
3310Sstevel@tonic-gate 			return (rc);
3320Sstevel@tonic-gate 		}
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/* set action chain reference */
3360Sstevel@tonic-gate 	if ((rc = ipp_action_ref(aid, cfg_parms->red_action, flags)) != 0) {
3370Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_create_action: ipp_action_ref " \
3380Sstevel@tonic-gate 		    "returned with error %d", rc));
3390Sstevel@tonic-gate 		goto cleanup;
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 	if ((rc = ipp_action_ref(aid, cfg_parms->green_action, flags)) != 0) {
3420Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_create_action: ipp_action_ref " \
3430Sstevel@tonic-gate 		    "returned with error %d", rc));
3440Sstevel@tonic-gate 		rc2 = ipp_action_unref(aid, cfg_parms->red_action, flags);
3450Sstevel@tonic-gate 		ASSERT(rc2 == 0);
3460Sstevel@tonic-gate 		goto cleanup;
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	if (cfg_parms->yellow_action != TOKENMT_NO_ACTION) {
3500Sstevel@tonic-gate 		if ((rc = ipp_action_ref(aid, cfg_parms->yellow_action,
3510Sstevel@tonic-gate 		    flags)) != 0) {
3520Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_create_action: ipp_action_ref "\
3530Sstevel@tonic-gate 			    "returned with error %d", rc));
3540Sstevel@tonic-gate 			rc2 = ipp_action_unref(aid, cfg_parms->red_action,
3550Sstevel@tonic-gate 			    flags);
3560Sstevel@tonic-gate 			ASSERT(rc2 == 0);
3570Sstevel@tonic-gate 			rc2 = ipp_action_unref(aid, cfg_parms->green_action,
3580Sstevel@tonic-gate 			    flags);
3590Sstevel@tonic-gate 			ASSERT(rc2 == 0);
3600Sstevel@tonic-gate 			goto cleanup;
3610Sstevel@tonic-gate 		}
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	tokenmt_data->cfg_parms = cfg_parms;
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	tokenmt_data->committed_tokens = cfg_parms->committed_burst;
3680Sstevel@tonic-gate 	tokenmt_data->peak_tokens = cfg_parms->peak_burst;
3690Sstevel@tonic-gate 	tokenmt_data->last_seen = gethrtime();
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	mutex_init(&tokenmt_data->tokenmt_lock, NULL, MUTEX_DEFAULT, 0);
3720Sstevel@tonic-gate 	ipp_action_set_ptr(aid, (void *)tokenmt_data);
3730Sstevel@tonic-gate 	return (0);
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate cleanup:
3760Sstevel@tonic-gate 	if (cfg_parms->stats) {
3770Sstevel@tonic-gate 		ipp_stat_destroy(tokenmt_data->stats);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 	kmem_free(cfg_parms, TOKENMT_CFG_SZ);
3800Sstevel@tonic-gate 	kmem_free(tokenmt_data, TOKENMT_DATA_SZ);
3810Sstevel@tonic-gate 	return (rc);
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate static int
tokenmt_modify_action(ipp_action_id_t aid,nvlist_t ** nvlpp,ipp_flags_t flags)3850Sstevel@tonic-gate tokenmt_modify_action(ipp_action_id_t aid, nvlist_t **nvlpp, ipp_flags_t flags)
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate 	nvlist_t *nvlp;
3880Sstevel@tonic-gate 	int err = 0, err2;
3890Sstevel@tonic-gate 	uint8_t config_type;
3900Sstevel@tonic-gate 	char *next_action_name;
3910Sstevel@tonic-gate 	ipp_action_id_t next_action;
3920Sstevel@tonic-gate 	uint32_t rate, cbs, pbs;
3930Sstevel@tonic-gate 	tokenmt_cfg_t *cfg_parms, *old_cfg;
3940Sstevel@tonic-gate 	tokenmt_data_t *tokenmt_data;
3950Sstevel@tonic-gate 	uint32_t bstats, mode;
3960Sstevel@tonic-gate 	int32_t *colour_tbl;
3970Sstevel@tonic-gate 	uint_t nelem = 64;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	nvlp = *nvlpp;
4000Sstevel@tonic-gate 	*nvlpp = NULL;		/* nvlist should be NULL when this returns */
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	if ((err = nvlist_lookup_byte(nvlp, IPP_CONFIG_TYPE, &config_type))
4030Sstevel@tonic-gate 	    != 0) {
4040Sstevel@tonic-gate 		nvlist_free(nvlp);
4050Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_modify_action: invalid configuration "\
4060Sstevel@tonic-gate 		    "type"));
4070Sstevel@tonic-gate 		return (err);
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	if (config_type != IPP_SET) {
4110Sstevel@tonic-gate 		nvlist_free(nvlp);
4120Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_modify_action: invalid configuration "\
4130Sstevel@tonic-gate 		    "type %d", config_type));
4140Sstevel@tonic-gate 		return (EINVAL);
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	tokenmt_data = (tokenmt_data_t *)ipp_action_get_ptr(aid);
4180Sstevel@tonic-gate 	old_cfg = tokenmt_data->cfg_parms;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	cfg_parms = kmem_zalloc(TOKENMT_CFG_SZ, KM_NOSLEEP);
4210Sstevel@tonic-gate 	if (cfg_parms == NULL) {
4220Sstevel@tonic-gate 		nvlist_free(nvlp);
4230Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_modify_action: memory allocation "\
4240Sstevel@tonic-gate 		    "failure\n"));
4250Sstevel@tonic-gate 		return (ENOMEM);
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	/* Just copy all and change as needed */
4290Sstevel@tonic-gate 	bcopy(old_cfg, cfg_parms, TOKENMT_CFG_SZ);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/* parse red action name, if present */
4320Sstevel@tonic-gate 	if ((err = nvlist_lookup_string(nvlp, TOKENMT_RED_ACTION_NAME,
4330Sstevel@tonic-gate 	    &next_action_name)) == 0) {
4340Sstevel@tonic-gate 		/* Get action id */
4350Sstevel@tonic-gate 		if ((next_action = ipp_action_lookup(next_action_name))
4360Sstevel@tonic-gate 		    == IPP_ACTION_INVAL) {
4370Sstevel@tonic-gate 			nvlist_free(nvlp);
4380Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_modify_action: next_action "\
4390Sstevel@tonic-gate 			    "invalid"));
4400Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
4410Sstevel@tonic-gate 			return (EINVAL);
4420Sstevel@tonic-gate 		}
4430Sstevel@tonic-gate 		cfg_parms->red_action = next_action;
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	/* parse yellow action name, if present */
4470Sstevel@tonic-gate 	if ((err = nvlist_lookup_string(nvlp, TOKENMT_YELLOW_ACTION_NAME,
4480Sstevel@tonic-gate 	    &next_action_name)) == 0) {
4490Sstevel@tonic-gate 		/* Get action id */
4500Sstevel@tonic-gate 		if ((next_action = ipp_action_lookup(next_action_name))
4510Sstevel@tonic-gate 		    == IPP_ACTION_INVAL) {
4520Sstevel@tonic-gate 			nvlist_free(nvlp);
4530Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_modify_action: next_action "\
4540Sstevel@tonic-gate 			    "invalid"));
4550Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
4560Sstevel@tonic-gate 			return (EINVAL);
4570Sstevel@tonic-gate 		}
4580Sstevel@tonic-gate 		cfg_parms->yellow_action = next_action;
4590Sstevel@tonic-gate 	} else {
4600Sstevel@tonic-gate 		cfg_parms->yellow_action = TOKENMT_NO_ACTION;
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	/* parse green action name, if present */
4640Sstevel@tonic-gate 	if ((err = nvlist_lookup_string(nvlp, TOKENMT_GREEN_ACTION_NAME,
4650Sstevel@tonic-gate 	    &next_action_name)) == 0) {
4660Sstevel@tonic-gate 		/* Get action id */
4670Sstevel@tonic-gate 		if ((next_action = ipp_action_lookup(next_action_name))
4680Sstevel@tonic-gate 		    == IPP_ACTION_INVAL) {
4690Sstevel@tonic-gate 			nvlist_free(nvlp);
4700Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_modify_action: next_action "\
4710Sstevel@tonic-gate 			    "invalid"));
4720Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
4730Sstevel@tonic-gate 			return (EINVAL);
4740Sstevel@tonic-gate 		}
4750Sstevel@tonic-gate 		cfg_parms->green_action = next_action;
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	/* parse committed rate, if present */
4790Sstevel@tonic-gate 	if ((err = nvlist_lookup_uint32(nvlp, TOKENMT_COMMITTED_RATE, &rate))
4800Sstevel@tonic-gate 	    == 0) {
4810Sstevel@tonic-gate 		if (rate == 0) {
4820Sstevel@tonic-gate 			nvlist_free(nvlp);
4830Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_modify_action: invalid "\
4840Sstevel@tonic-gate 			    "committed rate %u\n", cfg_parms->committed_rate));
4850Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
4860Sstevel@tonic-gate 			return (EINVAL);
4870Sstevel@tonic-gate 		}
4880Sstevel@tonic-gate 		cfg_parms->committed_rate = rate;
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	/* parse committed burst, if present */
4920Sstevel@tonic-gate 	if (nvlist_lookup_uint32(nvlp, TOKENMT_COMMITTED_BURST, &cbs) == 0) {
4930Sstevel@tonic-gate 		cfg_parms->committed_burst = cbs;
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	if (nvlist_lookup_uint32(nvlp, TOKENMT_PEAK_BURST, &pbs) == 0) {
4980Sstevel@tonic-gate 		cfg_parms->peak_burst = pbs;
4990Sstevel@tonic-gate 	} else {
5000Sstevel@tonic-gate 		cfg_parms->peak_burst = 0;
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	/* If the peak rate is not specified, then it means single rate meter */
5040Sstevel@tonic-gate 	if (nvlist_lookup_uint32(nvlp, TOKENMT_PEAK_RATE, &rate) == 0) {
5050Sstevel@tonic-gate 		cfg_parms->peak_rate = rate;
5060Sstevel@tonic-gate 		if ((rate == 0) || (rate < cfg_parms->committed_rate)) {
5070Sstevel@tonic-gate 			nvlist_free(nvlp);
5080Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_modify_action: invalid "\
5090Sstevel@tonic-gate 			    "committed rate %u\n", cfg_parms->committed_rate));
5100Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
5110Sstevel@tonic-gate 			return (EINVAL);
5120Sstevel@tonic-gate 		}
5130Sstevel@tonic-gate 		cfg_parms->tokenmt_type = TRTCL_TOKENMT;
5140Sstevel@tonic-gate 	} else {
5150Sstevel@tonic-gate 		cfg_parms->peak_rate = 0;
5160Sstevel@tonic-gate 		cfg_parms->tokenmt_type = SRTCL_TOKENMT;
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	if (cfg_parms->yellow_action == TOKENMT_NO_ACTION) {
5200Sstevel@tonic-gate 		if ((cfg_parms->peak_burst != 0) ||
5210Sstevel@tonic-gate 		    (cfg_parms->tokenmt_type == TRTCL_TOKENMT)) {
5220Sstevel@tonic-gate 			nvlist_free(nvlp);
5230Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_modify_action: yellow action "\
5240Sstevel@tonic-gate 			    "missing\n"));
5250Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
5260Sstevel@tonic-gate 			return (EINVAL);
5270Sstevel@tonic-gate 		}
5280Sstevel@tonic-gate 	} else {
5290Sstevel@tonic-gate 		if ((cfg_parms->tokenmt_type != TRTCL_TOKENMT) &&
5300Sstevel@tonic-gate 		    (cfg_parms->peak_burst == 0)) {
5310Sstevel@tonic-gate 			nvlist_free(nvlp);
5320Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_modify_action: peak "\
5330Sstevel@tonic-gate 			    "burst/rate missing\n"));
5340Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
5350Sstevel@tonic-gate 			return (EINVAL);
5360Sstevel@tonic-gate 		}
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	/* Validate the committed and peak burst size */
5400Sstevel@tonic-gate 	if (cfg_parms->tokenmt_type == SRTCL_TOKENMT) {
5410Sstevel@tonic-gate 		if ((cfg_parms->committed_burst == 0) &&
5420Sstevel@tonic-gate 		    (cfg_parms->peak_burst == 0)) {
5430Sstevel@tonic-gate 			nvlist_free(nvlp);
5440Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_modify_action: at least one "\
5450Sstevel@tonic-gate 			    "burst size must be non-zero\n"));
5460Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
5470Sstevel@tonic-gate 			return (EINVAL);
5480Sstevel@tonic-gate 		}
5490Sstevel@tonic-gate 	} else {	/* TRTCL_TOKENMT */
5500Sstevel@tonic-gate 		if ((cfg_parms->committed_burst == 0) ||
5510Sstevel@tonic-gate 		    (cfg_parms->peak_burst == 0)) {
5520Sstevel@tonic-gate 			nvlist_free(nvlp);
5530Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_modify_action: both the "\
5540Sstevel@tonic-gate 			    "burst sizes must be non-zero\n"));
5550Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
5560Sstevel@tonic-gate 			return (EINVAL);
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	/* parse mode */
5610Sstevel@tonic-gate 	if (nvlist_lookup_uint32(nvlp, TOKENMT_COLOUR_AWARE, &mode) == 0) {
5620Sstevel@tonic-gate 		cfg_parms->colour_aware = (mode == 0) ? B_FALSE : B_TRUE;
5630Sstevel@tonic-gate 	} else {
5640Sstevel@tonic-gate 		cfg_parms->colour_aware = B_FALSE;
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	if (cfg_parms->colour_aware) {
5680Sstevel@tonic-gate 		if (nvlist_lookup_int32_array(nvlp, TOKENMT_COLOUR_MAP,
5690Sstevel@tonic-gate 		    &colour_tbl, &nelem) == 0) {
5700Sstevel@tonic-gate 			int count;
5710Sstevel@tonic-gate 			for (count = 0; count < 64; count++) {
5720Sstevel@tonic-gate 				if (colour_tbl[count] == -1)
5730Sstevel@tonic-gate 					continue;
5740Sstevel@tonic-gate 				cfg_parms->dscp_to_colour[count] =
5750Sstevel@tonic-gate 				    colour_tbl[count];
5760Sstevel@tonic-gate 			}
5770Sstevel@tonic-gate 		} else {
5780Sstevel@tonic-gate 			bcopy(default_dscp_to_colour, cfg_parms->dscp_to_colour,
5790Sstevel@tonic-gate 			    sizeof (default_dscp_to_colour));
5800Sstevel@tonic-gate 		}
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	/* parse stats, if present */
5840Sstevel@tonic-gate 	if (nvlist_lookup_uint32(nvlp, IPP_ACTION_STATS_ENABLE, &bstats) == 0) {
5850Sstevel@tonic-gate 		cfg_parms->stats = (bstats == 0) ? B_FALSE : B_TRUE;
5860Sstevel@tonic-gate 		if (cfg_parms->stats && !old_cfg->stats) {
5870Sstevel@tonic-gate 			if ((err = tokenmt_statinit(aid, tokenmt_data)) != 0) {
5880Sstevel@tonic-gate 				nvlist_free(nvlp);
5890Sstevel@tonic-gate 				kmem_free(cfg_parms, TOKENMT_CFG_SZ);
5900Sstevel@tonic-gate 				return (err);
5910Sstevel@tonic-gate 			}
5920Sstevel@tonic-gate 		} else if (!cfg_parms->stats && old_cfg->stats) {
5930Sstevel@tonic-gate 			ipp_stat_destroy(tokenmt_data->stats);
5940Sstevel@tonic-gate 		}
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	/* Can we ref all the new actions? */
5980Sstevel@tonic-gate 	if ((err = ipp_action_ref(aid, cfg_parms->red_action, flags)) != 0) {
5990Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_modify_data: can't ref. red action\n"));
6000Sstevel@tonic-gate 		kmem_free(cfg_parms, TOKENMT_CFG_SZ);
6010Sstevel@tonic-gate 		return (err);
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 	if ((err = ipp_action_ref(aid, cfg_parms->green_action, flags)) != 0) {
6040Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_modify_data:can't ref. green action\n"));
6050Sstevel@tonic-gate 		err2 = ipp_action_unref(aid, cfg_parms->red_action, flags);
6060Sstevel@tonic-gate 		ASSERT(err2 == 0);
6070Sstevel@tonic-gate 		kmem_free(cfg_parms, TOKENMT_CFG_SZ);
6080Sstevel@tonic-gate 		return (err);
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	if (cfg_parms->yellow_action != TOKENMT_NO_ACTION) {
6120Sstevel@tonic-gate 		if ((err = ipp_action_ref(aid, cfg_parms->yellow_action,
6130Sstevel@tonic-gate 		    flags)) != 0) {
6140Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_modify_data:can't ref. yellow "\
6150Sstevel@tonic-gate 			    "action\n"));
6160Sstevel@tonic-gate 			err2 = ipp_action_unref(aid, cfg_parms->red_action,
6170Sstevel@tonic-gate 			    flags);
6180Sstevel@tonic-gate 			ASSERT(err2 == 0);
6190Sstevel@tonic-gate 			err2 = ipp_action_unref(aid, cfg_parms->green_action,
6200Sstevel@tonic-gate 			    flags);
6210Sstevel@tonic-gate 			ASSERT(err2 == 0);
6220Sstevel@tonic-gate 			kmem_free(cfg_parms, TOKENMT_CFG_SZ);
6230Sstevel@tonic-gate 			return (err);
6240Sstevel@tonic-gate 		}
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	/* Actually modify the configuration */
6290Sstevel@tonic-gate 	mutex_enter(&tokenmt_data->tokenmt_lock);
6300Sstevel@tonic-gate 	tokenmt_data->cfg_parms = cfg_parms;
6310Sstevel@tonic-gate 	mutex_exit(&tokenmt_data->tokenmt_lock);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	/* Un-ref the old actions */
6340Sstevel@tonic-gate 	err = ipp_action_unref(aid, old_cfg->red_action, flags);
6350Sstevel@tonic-gate 	ASSERT(err == 0);
6360Sstevel@tonic-gate 	if (old_cfg->yellow_action != TOKENMT_NO_ACTION) {
6370Sstevel@tonic-gate 		err = ipp_action_unref(aid, old_cfg->yellow_action, flags);
6380Sstevel@tonic-gate 		ASSERT(err == 0);
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 	err = ipp_action_unref(aid, old_cfg->green_action, flags);
6410Sstevel@tonic-gate 	ASSERT(err == 0);
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	/* Free the old configuration */
6440Sstevel@tonic-gate 	kmem_free(old_cfg, TOKENMT_CFG_SZ);
6450Sstevel@tonic-gate 	return (0);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate static int
tokenmt_destroy_action(ipp_action_id_t aid,ipp_flags_t flags)6490Sstevel@tonic-gate tokenmt_destroy_action(ipp_action_id_t aid, ipp_flags_t flags)
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate 	tokenmt_data_t *tokenmt_data;
6520Sstevel@tonic-gate 	tokenmt_cfg_t *cfg_parms;
6530Sstevel@tonic-gate 	int rc;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	tokenmt_data = (tokenmt_data_t *)ipp_action_get_ptr(aid);
6560Sstevel@tonic-gate 	ASSERT(tokenmt_data != NULL);
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	cfg_parms = tokenmt_data->cfg_parms;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	if (cfg_parms->stats) {
6610Sstevel@tonic-gate 		ipp_stat_destroy(tokenmt_data->stats);
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	/* unreference the action */
6650Sstevel@tonic-gate 	rc = ipp_action_unref(aid, cfg_parms->red_action, flags);
6660Sstevel@tonic-gate 	ASSERT(rc == 0);
6670Sstevel@tonic-gate 	if (cfg_parms->yellow_action != TOKENMT_NO_ACTION) {
6680Sstevel@tonic-gate 		rc = ipp_action_unref(aid, cfg_parms->yellow_action, flags);
6690Sstevel@tonic-gate 		ASSERT(rc == 0);
6700Sstevel@tonic-gate 	}
6710Sstevel@tonic-gate 	rc = ipp_action_unref(aid, cfg_parms->green_action, flags);
6720Sstevel@tonic-gate 	ASSERT(rc == 0);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	mutex_destroy(&tokenmt_data->tokenmt_lock);
6750Sstevel@tonic-gate 	kmem_free(cfg_parms, TOKENMT_CFG_SZ);
6760Sstevel@tonic-gate 	kmem_free(tokenmt_data, TOKENMT_DATA_SZ);
6770Sstevel@tonic-gate 	return (0);
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate static int
tokenmt_invoke_action(ipp_action_id_t aid,ipp_packet_t * packet)6810Sstevel@tonic-gate tokenmt_invoke_action(ipp_action_id_t aid, ipp_packet_t *packet)
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate 	tokenmt_data_t *tokenmt_data;
6840Sstevel@tonic-gate 	ipp_action_id_t next_action;
6850Sstevel@tonic-gate 	mblk_t *mp = NULL;
6860Sstevel@tonic-gate 	int rc;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	/* get mblk from ipp_packet structure */
6890Sstevel@tonic-gate 	mp = ipp_packet_get_data(packet);
6900Sstevel@tonic-gate 	tokenmt_data = (tokenmt_data_t *)ipp_action_get_ptr(aid);
6910Sstevel@tonic-gate 	ASSERT(tokenmt_data != NULL);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	/* meter packet as configured */
6940Sstevel@tonic-gate 	if ((rc = tokenmt_process(&mp, tokenmt_data, &next_action)) != 0) {
6950Sstevel@tonic-gate 		return (rc);
6960Sstevel@tonic-gate 	} else {
6970Sstevel@tonic-gate 		return (ipp_packet_next(packet, next_action));
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate static int
tokenmt_statinit(ipp_action_id_t aid,tokenmt_data_t * tokenmt_data)7020Sstevel@tonic-gate tokenmt_statinit(ipp_action_id_t aid, tokenmt_data_t *tokenmt_data) {
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	int rc = 0;
7050Sstevel@tonic-gate 	meter_stat_t *statsp;
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	/* install stats entry */
7080Sstevel@tonic-gate 	if ((rc = ipp_stat_create(aid, TOKENMT_STATS_STRING, METER_STATS_COUNT,
7090Sstevel@tonic-gate 	    tokenmt_update_stats, tokenmt_data, &tokenmt_data->stats)) != 0) {
7100Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_statinit: ipp_stat_create failed "\
7110Sstevel@tonic-gate 		    " with %d\n", rc));
7120Sstevel@tonic-gate 		return (rc);
7130Sstevel@tonic-gate 	}
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	statsp = (meter_stat_t *)(tokenmt_data->stats)->ipps_data;
7160Sstevel@tonic-gate 	ASSERT(statsp != NULL);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	if ((rc = ipp_stat_named_init(tokenmt_data->stats, "red_packets",
7190Sstevel@tonic-gate 	    IPP_STAT_UINT64, &statsp->red_packets)) != 0) {
7200Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_statinit:ipp_stat_named_init failed "\
7210Sstevel@tonic-gate 		    " with %d\n", rc));
7220Sstevel@tonic-gate 		return (rc);
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 	if ((rc = ipp_stat_named_init(tokenmt_data->stats, "yellow_packets",
7250Sstevel@tonic-gate 	    IPP_STAT_UINT64, &statsp->yellow_packets)) != 0) {
7260Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_statinit:ipp_stat_named_init failed "\
7270Sstevel@tonic-gate 		    " with %d\n", rc));
7280Sstevel@tonic-gate 		return (rc);
7290Sstevel@tonic-gate 	}
7300Sstevel@tonic-gate 	if ((rc = ipp_stat_named_init(tokenmt_data->stats, "green_packets",
7310Sstevel@tonic-gate 	    IPP_STAT_UINT64, &statsp->green_packets)) != 0) {
7320Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_statinit:ipp_stat_named_init failed "\
7330Sstevel@tonic-gate 		    " with %d\n", rc));
7340Sstevel@tonic-gate 		return (rc);
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 	if ((rc = ipp_stat_named_init(tokenmt_data->stats, "red_bits",
7370Sstevel@tonic-gate 	    IPP_STAT_UINT64, &statsp->red_bits)) != 0) {
7380Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_statinit:ipp_stat_named_init failed "\
7390Sstevel@tonic-gate 		    " with %d\n", rc));
7400Sstevel@tonic-gate 		return (rc);
7410Sstevel@tonic-gate 	}
7420Sstevel@tonic-gate 	if ((rc = ipp_stat_named_init(tokenmt_data->stats, "yellow_bits",
7430Sstevel@tonic-gate 	    IPP_STAT_UINT64, &statsp->yellow_bits)) != 0) {
7440Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_statinit:ipp_stat_named_init failed "\
7450Sstevel@tonic-gate 		    " with %d\n", rc));
7460Sstevel@tonic-gate 		return (rc);
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 	if ((rc = ipp_stat_named_init(tokenmt_data->stats, "green_bits",
7490Sstevel@tonic-gate 	    IPP_STAT_UINT64, &statsp->green_bits)) != 0) {
7500Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_statinit:ipp_stat_named_init failed "\
7510Sstevel@tonic-gate 		    " with %d\n", rc));
7520Sstevel@tonic-gate 		return (rc);
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate 	if ((rc = ipp_stat_named_init(tokenmt_data->stats, "epackets",
7550Sstevel@tonic-gate 	    IPP_STAT_UINT64, &statsp->epackets)) != 0) {
7560Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_statinit:ipp_stat_named_init failed "\
7570Sstevel@tonic-gate 		    " with %d\n", rc));
7580Sstevel@tonic-gate 		return (rc);
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	ipp_stat_install(tokenmt_data->stats);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	return (rc);
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate static int
tokenmt_update_stats(ipp_stat_t * sp,void * args,int rw)7670Sstevel@tonic-gate tokenmt_update_stats(ipp_stat_t *sp, void *args, int rw)
7680Sstevel@tonic-gate {
7690Sstevel@tonic-gate 	tokenmt_data_t *tokenmt_data = (tokenmt_data_t *)args;
7700Sstevel@tonic-gate 	meter_stat_t *stats = (meter_stat_t *)sp->ipps_data;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	ASSERT((tokenmt_data != NULL) && (stats != NULL));
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	(void) ipp_stat_named_op(&stats->red_packets,
7750Sstevel@tonic-gate 	    &tokenmt_data->red_packets, rw);
7760Sstevel@tonic-gate 	(void) ipp_stat_named_op(&stats->yellow_packets,
7770Sstevel@tonic-gate 	    &tokenmt_data->yellow_packets, rw);
7780Sstevel@tonic-gate 	(void) ipp_stat_named_op(&stats->green_packets,
7790Sstevel@tonic-gate 	    &tokenmt_data->green_packets, rw);
7800Sstevel@tonic-gate 	(void) ipp_stat_named_op(&stats->red_bits,
7810Sstevel@tonic-gate 	    &tokenmt_data->red_bits, rw);
7820Sstevel@tonic-gate 	(void) ipp_stat_named_op(&stats->yellow_bits,
7830Sstevel@tonic-gate 	    &tokenmt_data->yellow_bits, rw);
7840Sstevel@tonic-gate 	(void) ipp_stat_named_op(&stats->green_bits,
7850Sstevel@tonic-gate 	    &tokenmt_data->green_bits, rw);
7860Sstevel@tonic-gate 	(void) ipp_stat_named_op(&stats->epackets, &tokenmt_data->epackets,
7870Sstevel@tonic-gate 	    rw);
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	return (0);
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate /* ARGSUSED */
7930Sstevel@tonic-gate static int
tokenmt_info(ipp_action_id_t aid,int (* fn)(nvlist_t *,void *),void * arg,ipp_flags_t flags)7940Sstevel@tonic-gate tokenmt_info(ipp_action_id_t aid, int (*fn)(nvlist_t *, void *), void *arg,
7950Sstevel@tonic-gate     ipp_flags_t flags)
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate 	nvlist_t *nvlp;
7980Sstevel@tonic-gate 	tokenmt_data_t *tokenmt_data;
7990Sstevel@tonic-gate 	tokenmt_cfg_t *cfg_parms;
8000Sstevel@tonic-gate 	char *next_action;
8010Sstevel@tonic-gate 	int32_t dscp_to_colour[64];
8020Sstevel@tonic-gate 	int rc;
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	tokenmt_data = (tokenmt_data_t *)ipp_action_get_ptr(aid);
8050Sstevel@tonic-gate 	ASSERT(tokenmt_data != NULL);
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	cfg_parms = tokenmt_data->cfg_parms;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	/* allocate nvlist to be passed back */
8100Sstevel@tonic-gate 	if ((rc = nvlist_alloc(&nvlp, NV_UNIQUE_NAME, KM_NOSLEEP)) != 0) {
8110Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_info: memory allocation failure\n"));
8120Sstevel@tonic-gate 		return (rc);
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	/* look up red next action with the next action id */
8160Sstevel@tonic-gate 	if ((rc = ipp_action_name(cfg_parms->red_action, &next_action)) != 0) {
8170Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_info: red_action not available\n"));
8180Sstevel@tonic-gate 		nvlist_free(nvlp);
8190Sstevel@tonic-gate 		return (rc);
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	/* add next action name */
8230Sstevel@tonic-gate 	if ((rc = nvlist_add_string(nvlp, TOKENMT_RED_ACTION_NAME,
8240Sstevel@tonic-gate 	    next_action)) != 0) {
8250Sstevel@tonic-gate 		nvlist_free(nvlp);
8260Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_info: error adding red_action\n"));
8270Sstevel@tonic-gate 		kmem_free(next_action, (strlen(next_action) + 1));
8280Sstevel@tonic-gate 		return (rc);
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	/* free action name */
8320Sstevel@tonic-gate 	kmem_free(next_action, (strlen(next_action) + 1));
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	/* look up yellow next action with the next action id */
8360Sstevel@tonic-gate 	if (cfg_parms->yellow_action != TOKENMT_NO_ACTION) {
8370Sstevel@tonic-gate 		if ((rc = ipp_action_name(cfg_parms->yellow_action,
8380Sstevel@tonic-gate 		    &next_action)) != 0) {
8390Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_info: yellow_action not "\
8400Sstevel@tonic-gate 			    "available\n"));
8410Sstevel@tonic-gate 			nvlist_free(nvlp);
8420Sstevel@tonic-gate 			return (rc);
8430Sstevel@tonic-gate 		}
8440Sstevel@tonic-gate 		/* add next action name */
8450Sstevel@tonic-gate 		if ((rc = nvlist_add_string(nvlp, TOKENMT_YELLOW_ACTION_NAME,
8460Sstevel@tonic-gate 		    next_action)) != 0) {
8470Sstevel@tonic-gate 			nvlist_free(nvlp);
8480Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_info: error adding "\
8490Sstevel@tonic-gate 			    "yellow_action\n"));
8500Sstevel@tonic-gate 			kmem_free(next_action, (strlen(next_action) + 1));
8510Sstevel@tonic-gate 			return (rc);
8520Sstevel@tonic-gate 		}
8530Sstevel@tonic-gate 		/* free action name */
8540Sstevel@tonic-gate 		kmem_free(next_action, (strlen(next_action) + 1));
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	/* look up green next action with the next action id */
8580Sstevel@tonic-gate 	if ((rc = ipp_action_name(cfg_parms->green_action,
8590Sstevel@tonic-gate 	    &next_action)) != 0) {
8600Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_info: green_action not available\n"));
8610Sstevel@tonic-gate 		nvlist_free(nvlp);
8620Sstevel@tonic-gate 		return (rc);
8630Sstevel@tonic-gate 	}
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	/* add next action name */
8660Sstevel@tonic-gate 	if ((rc = nvlist_add_string(nvlp, TOKENMT_GREEN_ACTION_NAME,
8670Sstevel@tonic-gate 	    next_action)) != 0) {
8680Sstevel@tonic-gate 		nvlist_free(nvlp);
8690Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_info: error adding green_action\n"));
8700Sstevel@tonic-gate 		kmem_free(next_action, (strlen(next_action) + 1));
8710Sstevel@tonic-gate 		return (rc);
8720Sstevel@tonic-gate 	}
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	/* free action name */
8750Sstevel@tonic-gate 	kmem_free(next_action, (strlen(next_action) + 1));
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	/* add config type */
8780Sstevel@tonic-gate 	if ((rc = nvlist_add_byte(nvlp, IPP_CONFIG_TYPE, IPP_SET)) != 0) {
8790Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_info: error adding config_type\n"));
8800Sstevel@tonic-gate 		nvlist_free(nvlp);
8810Sstevel@tonic-gate 		return (rc);
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	/* add committed_rate  */
8850Sstevel@tonic-gate 	if ((rc = nvlist_add_uint32(nvlp, TOKENMT_COMMITTED_RATE,
8860Sstevel@tonic-gate 	    cfg_parms->committed_rate)) != 0) {
8870Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_info: error adding committed_rate\n"));
8880Sstevel@tonic-gate 		nvlist_free(nvlp);
8890Sstevel@tonic-gate 		return (rc);
8900Sstevel@tonic-gate 	}
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	if (cfg_parms->tokenmt_type == TRTCL_TOKENMT) {
8930Sstevel@tonic-gate 		/* add peak  rate */
8940Sstevel@tonic-gate 		if ((rc = nvlist_add_uint32(nvlp, TOKENMT_PEAK_RATE,
8950Sstevel@tonic-gate 		    cfg_parms->peak_rate)) != 0) {
8960Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_info: error adding peak_rate\n"));
8970Sstevel@tonic-gate 			nvlist_free(nvlp);
8980Sstevel@tonic-gate 			return (rc);
8990Sstevel@tonic-gate 		}
9000Sstevel@tonic-gate 	}
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	/* add committed_burst  */
9030Sstevel@tonic-gate 	if ((rc = nvlist_add_uint32(nvlp, TOKENMT_COMMITTED_BURST,
9040Sstevel@tonic-gate 	    cfg_parms->committed_burst)) != 0) {
9050Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_info: error adding committed_burst\n"));
9060Sstevel@tonic-gate 		nvlist_free(nvlp);
9070Sstevel@tonic-gate 		return (rc);
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	/* add peak_burst  */
9110Sstevel@tonic-gate 	if (cfg_parms->peak_burst != 0) {
9120Sstevel@tonic-gate 		if ((rc = nvlist_add_uint32(nvlp, TOKENMT_PEAK_BURST,
9130Sstevel@tonic-gate 		    cfg_parms->peak_burst)) != 0) {
9140Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_info: error adding peak "\
9150Sstevel@tonic-gate 			    "burst\n"));
9160Sstevel@tonic-gate 			nvlist_free(nvlp);
9170Sstevel@tonic-gate 			return (rc);
9180Sstevel@tonic-gate 		}
9190Sstevel@tonic-gate 	}
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	/* add colour aware  */
9220Sstevel@tonic-gate 	if ((rc = nvlist_add_uint32(nvlp, TOKENMT_COLOUR_AWARE,
9230Sstevel@tonic-gate 	    cfg_parms->colour_aware)) != 0) {
9240Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_info: error adding mode\n"));
9250Sstevel@tonic-gate 		nvlist_free(nvlp);
9260Sstevel@tonic-gate 		return (rc);
9270Sstevel@tonic-gate 	}
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	if (cfg_parms->colour_aware) {
9300Sstevel@tonic-gate 		bcopy(cfg_parms->dscp_to_colour, dscp_to_colour,
9310Sstevel@tonic-gate 		    sizeof (cfg_parms->dscp_to_colour));
9320Sstevel@tonic-gate 		if ((rc = nvlist_add_int32_array(nvlp, TOKENMT_COLOUR_MAP,
9330Sstevel@tonic-gate 		    dscp_to_colour, 64)) != 0) {
9340Sstevel@tonic-gate 			tokenmt0dbg(("tokenmt_info: error adding colour "\
9350Sstevel@tonic-gate 			    "array\n"));
9360Sstevel@tonic-gate 			nvlist_free(nvlp);
9370Sstevel@tonic-gate 			return (rc);
9380Sstevel@tonic-gate 		}
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	if ((rc = nvlist_add_uint32(nvlp, IPP_ACTION_STATS_ENABLE,
9420Sstevel@tonic-gate 	    (uint32_t)cfg_parms->stats)) != 0) {
9430Sstevel@tonic-gate 		tokenmt0dbg(("tokenmt_info: error adding stats status\n"));
9440Sstevel@tonic-gate 		nvlist_free(nvlp);
9450Sstevel@tonic-gate 		return (rc);
9460Sstevel@tonic-gate 	}
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	/* call back with nvlist */
9490Sstevel@tonic-gate 	rc = fn(nvlp, arg);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	nvlist_free(nvlp);
9520Sstevel@tonic-gate 	return (rc);
9530Sstevel@tonic-gate }
954