xref: /illumos-gate/usr/src/uts/sun4v/io/mdeg.c (revision a5efd12c)
11ae08745Sheppo /*
21ae08745Sheppo  * CDDL HEADER START
31ae08745Sheppo  *
41ae08745Sheppo  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
71ae08745Sheppo  *
81ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
101ae08745Sheppo  * See the License for the specific language governing permissions
111ae08745Sheppo  * and limitations under the License.
121ae08745Sheppo  *
131ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181ae08745Sheppo  *
191ae08745Sheppo  * CDDL HEADER END
201ae08745Sheppo  */
211ae08745Sheppo 
221ae08745Sheppo /*
230c4606f0SWENTAO YANG  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
241ae08745Sheppo  * Use is subject to license terms.
251ae08745Sheppo  */
261ae08745Sheppo 
271ae08745Sheppo /*
281ae08745Sheppo  * MD Event Generator (MDEG) Module
291ae08745Sheppo  */
301ae08745Sheppo 
311ae08745Sheppo #include <sys/machsystm.h>
321ae08745Sheppo #include <sys/taskq.h>
331ae08745Sheppo #include <sys/disp.h>
341ae08745Sheppo #include <sys/cmn_err.h>
351ae08745Sheppo #include <sys/note.h>
361ae08745Sheppo 
371ae08745Sheppo #include <sys/mdeg.h>
381ae08745Sheppo #include <sys/mach_descrip.h>
391ae08745Sheppo #include <sys/mdesc.h>
401ae08745Sheppo 
411ae08745Sheppo /*
421ae08745Sheppo  * A single client registration
431ae08745Sheppo  */
441ae08745Sheppo typedef struct mdeg_clnt {
451ae08745Sheppo 	boolean_t		valid;		/* structure is in active use */
461ae08745Sheppo 	mdeg_node_match_t	*nmatch;	/* node match filter */
471ae08745Sheppo 	mdeg_node_spec_t	*pspec;		/* parent match filter */
481ae08745Sheppo 	mdeg_cb_t		cb;		/* the client callback */
491ae08745Sheppo 	caddr_t			cb_arg;		/* argument to the callback */
501ae08745Sheppo 	uint64_t		magic;		/* sanity checking magic */
511ae08745Sheppo 	mdeg_handle_t		hdl;		/* handle assigned by MDEG */
521ae08745Sheppo } mdeg_clnt_t;
531ae08745Sheppo 
541ae08745Sheppo /*
551ae08745Sheppo  * Global MDEG data
561ae08745Sheppo  *
571ae08745Sheppo  * Locking Strategy:
581ae08745Sheppo  *
594e476149Srsmaeda  *   mdeg.lock - lock used to synchronize system-wide MD updates. An
601ae08745Sheppo  *	MD update must be treated as an atomic event. The lock is
611ae08745Sheppo  *	taken when notification that a new MD is available and held
621ae08745Sheppo  *	until all clients have been notified.
631ae08745Sheppo  *
644e476149Srsmaeda  *   mdeg.rwlock - lock used to synchronize access to the table of
651ae08745Sheppo  *	registered clients. The reader lock must be held when looking
661ae08745Sheppo  *	up client information in the table. The writer lock must be
671ae08745Sheppo  *	held when modifying any client information.
681ae08745Sheppo  */
691ae08745Sheppo static struct mdeg {
70*a5efd12cSToomas Soome 	taskq_t		*taskq;		/* for internal processing */
711ae08745Sheppo 	boolean_t	enabled;	/* enable/disable taskq processing */
721ae08745Sheppo 	kmutex_t	lock;		/* synchronize MD updates */
731ae08745Sheppo 	md_t		*md_prev;	/* previous MD */
741ae08745Sheppo 	md_t		*md_curr;	/* current MD */
751ae08745Sheppo 	mdeg_clnt_t	*tbl;		/* table of registered clients */
761ae08745Sheppo 	krwlock_t	rwlock;		/* client table lock */
771ae08745Sheppo 	uint_t		maxclnts;	/* client table size */
781ae08745Sheppo 	uint_t		nclnts;		/* current number of clients */
791ae08745Sheppo } mdeg;
801ae08745Sheppo 
811ae08745Sheppo /*
821ae08745Sheppo  * Debugging routines
831ae08745Sheppo  */
841ae08745Sheppo #ifdef DEBUG
851ae08745Sheppo uint_t mdeg_debug = 0x0;
861ae08745Sheppo 
871ae08745Sheppo static void mdeg_dump_clnt(mdeg_clnt_t *clnt);
881ae08745Sheppo static void mdeg_dump_table(void);
891ae08745Sheppo 
901ae08745Sheppo #define	MDEG_DBG		if (mdeg_debug) printf
911ae08745Sheppo #define	MDEG_DUMP_CLNT		mdeg_dump_clnt
921ae08745Sheppo #define	MDEG_DUMP_TABLE		mdeg_dump_table
931ae08745Sheppo 
941ae08745Sheppo #else /* DEBUG */
951ae08745Sheppo 
961ae08745Sheppo #define	MDEG_DBG		_NOTE(CONSTCOND) if (0) printf
97*a5efd12cSToomas Soome #define	MDEG_DUMP_CLNT(...)
98*a5efd12cSToomas Soome #define	MDEG_DUMP_TABLE(...)
991ae08745Sheppo 
1001ae08745Sheppo #endif /* DEBUG */
1011ae08745Sheppo 
1021ae08745Sheppo /*
1031ae08745Sheppo  * Global constants
1041ae08745Sheppo  */
1051ae08745Sheppo #define	MDEG_MAX_TASKQ_THR	512	/* maximum number of taskq threads */
1061ae08745Sheppo #define	MDEG_MAX_CLNTS_INIT	64	/* initial client table size */
1071ae08745Sheppo 
1081ae08745Sheppo #define	MDEG_MAGIC		0x4D4445475F48444Cull	/* 'MDEG_HDL' */
1091ae08745Sheppo 
1101ae08745Sheppo /*
1111ae08745Sheppo  * A client handle is a 64 bit value with two pieces of
1121ae08745Sheppo  * information encoded in it. The upper 32 bits are the
1131ae08745Sheppo  * index into the table of a particular client structure.
1141ae08745Sheppo  * The lower 32 bits are a counter that is incremented
1151ae08745Sheppo  * each time a client structure is reused.
1161ae08745Sheppo  */
1171ae08745Sheppo #define	MDEG_IDX_SHIFT			32
1181ae08745Sheppo #define	MDEG_COUNT_MASK			0xfffffffful
1191ae08745Sheppo 
1201ae08745Sheppo #define	MDEG_ALLOC_HDL(_idx, _count)	(((uint64_t)_idx << MDEG_IDX_SHIFT) | \
1211ae08745Sheppo 					((uint64_t)(_count + 1) &	      \
1221ae08745Sheppo 					MDEG_COUNT_MASK))
1231ae08745Sheppo #define	MDEG_HDL2IDX(hdl)		(hdl >> MDEG_IDX_SHIFT)
1241ae08745Sheppo #define	MDEG_HDL2COUNT(hdl)		(hdl & MDEG_COUNT_MASK)
1251ae08745Sheppo 
1261ae08745Sheppo static const char trunc_str[] = " ... }";
1271ae08745Sheppo 
1281ae08745Sheppo /*
1291ae08745Sheppo  * Utility routines
1301ae08745Sheppo  */
1311ae08745Sheppo static mdeg_clnt_t *mdeg_alloc_clnt(void);
1321ae08745Sheppo static void mdeg_notify_client(void *);
1331ae08745Sheppo static mde_cookie_t mdeg_find_start_node(md_t *, mdeg_node_spec_t *);
1341ae08745Sheppo static boolean_t mdeg_node_spec_match(md_t *, mde_cookie_t, mdeg_node_spec_t *);
1351ae08745Sheppo static void mdeg_get_diff_results(md_diff_cookie_t, mdeg_result_t *);
1361ae08745Sheppo 
1371ae08745Sheppo int
mdeg_init(void)1381ae08745Sheppo mdeg_init(void)
1391ae08745Sheppo {
1401ae08745Sheppo 	int	tblsz;
1411ae08745Sheppo 
1421ae08745Sheppo 	/*
1431ae08745Sheppo 	 * Grab the current MD
1441ae08745Sheppo 	 */
1451ae08745Sheppo 	if ((mdeg.md_curr = md_get_handle()) == NULL) {
1461ae08745Sheppo 		cmn_err(CE_WARN, "unable to cache snapshot of MD");
1471ae08745Sheppo 		return (-1);
1481ae08745Sheppo 	}
1491ae08745Sheppo 
1501ae08745Sheppo 	/*
1511ae08745Sheppo 	 * Initialize table of registered clients
1521ae08745Sheppo 	 */
1531ae08745Sheppo 	mdeg.maxclnts = MDEG_MAX_CLNTS_INIT;
1541ae08745Sheppo 
1551ae08745Sheppo 	tblsz = mdeg.maxclnts * sizeof (mdeg_clnt_t);
1561ae08745Sheppo 	mdeg.tbl = kmem_zalloc(tblsz, KM_SLEEP);
1571ae08745Sheppo 
1581ae08745Sheppo 	rw_init(&mdeg.rwlock, NULL, RW_DRIVER, NULL);
1591ae08745Sheppo 
1601ae08745Sheppo 	mdeg.nclnts = 0;
1611ae08745Sheppo 
1621ae08745Sheppo 	/*
1631ae08745Sheppo 	 * Initialize global lock
1641ae08745Sheppo 	 */
1651ae08745Sheppo 	mutex_init(&mdeg.lock, NULL, MUTEX_DRIVER, NULL);
1661ae08745Sheppo 
1671ae08745Sheppo 	/*
1681ae08745Sheppo 	 * Initialize the task queue
1691ae08745Sheppo 	 */
1701ae08745Sheppo 	mdeg.taskq = taskq_create("mdeg_taskq", 1, minclsyspri, 1,
1711ae08745Sheppo 	    MDEG_MAX_TASKQ_THR, TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
1721ae08745Sheppo 
1731ae08745Sheppo 	/* ready to begin handling clients */
1741ae08745Sheppo 	mdeg.enabled = B_TRUE;
1751ae08745Sheppo 
1761ae08745Sheppo 	return (0);
1771ae08745Sheppo }
1781ae08745Sheppo 
1791ae08745Sheppo void
mdeg_fini(void)1801ae08745Sheppo mdeg_fini(void)
1811ae08745Sheppo {
1821ae08745Sheppo 	/*
1831ae08745Sheppo 	 * Flip the enabled switch off to make sure that
1841ae08745Sheppo 	 * no events get dispatched while things are being
1851ae08745Sheppo 	 * torn down.
1861ae08745Sheppo 	 */
1871ae08745Sheppo 	mdeg.enabled = B_FALSE;
1881ae08745Sheppo 
1891ae08745Sheppo 	/* destroy the task queue */
1901ae08745Sheppo 	taskq_destroy(mdeg.taskq);
1911ae08745Sheppo 
1921ae08745Sheppo 	/*
1931ae08745Sheppo 	 * Deallocate the table of registered clients
1941ae08745Sheppo 	 */
1951ae08745Sheppo 	kmem_free(mdeg.tbl, mdeg.maxclnts * sizeof (mdeg_clnt_t));
1961ae08745Sheppo 	rw_destroy(&mdeg.rwlock);
1971ae08745Sheppo 
1981ae08745Sheppo 	/*
1991ae08745Sheppo 	 * Free up the cached MDs.
2001ae08745Sheppo 	 */
2011ae08745Sheppo 	if (mdeg.md_curr)
2021ae08745Sheppo 		(void) md_fini_handle(mdeg.md_curr);
2031ae08745Sheppo 
2041ae08745Sheppo 	if (mdeg.md_prev)
2051ae08745Sheppo 		(void) md_fini_handle(mdeg.md_prev);
2061ae08745Sheppo 
2071ae08745Sheppo 	mutex_destroy(&mdeg.lock);
2081ae08745Sheppo }
2091ae08745Sheppo 
2101ae08745Sheppo static mdeg_clnt_t *
mdeg_alloc_clnt(void)2111ae08745Sheppo mdeg_alloc_clnt(void)
2121ae08745Sheppo {
2131ae08745Sheppo 	mdeg_clnt_t	*clnt;
2141ae08745Sheppo 	int		idx;
2151ae08745Sheppo 	mdeg_clnt_t	*newtbl;
2161ae08745Sheppo 	uint_t		newmaxclnts;
2171ae08745Sheppo 	uint_t		newtblsz;
2181ae08745Sheppo 	uint_t		oldtblsz;
2191ae08745Sheppo 
2201ae08745Sheppo 	ASSERT(RW_WRITE_HELD(&mdeg.rwlock));
2211ae08745Sheppo 
2221ae08745Sheppo 	/* search for an unused slot in the table */
2231ae08745Sheppo 	for (idx = 0; idx < mdeg.maxclnts; idx++) {
2241ae08745Sheppo 		clnt = &mdeg.tbl[idx];
2251ae08745Sheppo 		if (!clnt->valid) {
2261ae08745Sheppo 			break;
2271ae08745Sheppo 		}
2281ae08745Sheppo 	}
2291ae08745Sheppo 
2301ae08745Sheppo 	/* found any empty slot */
2311ae08745Sheppo 	if (idx != mdeg.maxclnts) {
2321ae08745Sheppo 		goto found;
2331ae08745Sheppo 	}
2341ae08745Sheppo 
2351ae08745Sheppo 	/*
2361ae08745Sheppo 	 * There was no free space in the table. Grow
2371ae08745Sheppo 	 * the table to double its current size.
2381ae08745Sheppo 	 */
2391ae08745Sheppo 
2401ae08745Sheppo 	MDEG_DBG("client table full:\n");
2411ae08745Sheppo 	MDEG_DUMP_TABLE();
2421ae08745Sheppo 
2431ae08745Sheppo 	newmaxclnts = mdeg.maxclnts * 2;
2441ae08745Sheppo 	newtblsz = newmaxclnts * sizeof (mdeg_clnt_t);
2451ae08745Sheppo 
2461ae08745Sheppo 	newtbl = kmem_zalloc(newtblsz, KM_SLEEP);
2471ae08745Sheppo 
2481ae08745Sheppo 	/* copy old table data to the new table */
2491ae08745Sheppo 	oldtblsz = mdeg.maxclnts * sizeof (mdeg_clnt_t);
2501ae08745Sheppo 	bcopy(mdeg.tbl, newtbl, oldtblsz);
2511ae08745Sheppo 
2521ae08745Sheppo 	/*
2531ae08745Sheppo 	 * Since the old table was full, the first free entry
2544e476149Srsmaeda 	 * will be just past the end of the old table data in
2554e476149Srsmaeda 	 * the new table.
2561ae08745Sheppo 	 */
2574e476149Srsmaeda 	clnt = &newtbl[mdeg.maxclnts];
2581ae08745Sheppo 
2591ae08745Sheppo 	/* clean up the old table */
2601ae08745Sheppo 	kmem_free(mdeg.tbl, oldtblsz);
2611ae08745Sheppo 	mdeg.tbl = newtbl;
2621ae08745Sheppo 	mdeg.maxclnts = newmaxclnts;
2631ae08745Sheppo 
2641ae08745Sheppo found:
2651ae08745Sheppo 	ASSERT(clnt->valid == 0);
2661ae08745Sheppo 
2671ae08745Sheppo 	clnt->hdl = MDEG_ALLOC_HDL(idx, MDEG_HDL2COUNT(clnt->hdl));
2681ae08745Sheppo 
2691ae08745Sheppo 	return (clnt);
2701ae08745Sheppo }
2711ae08745Sheppo 
2721ae08745Sheppo static mdeg_clnt_t *
mdeg_get_client(mdeg_handle_t hdl)2731ae08745Sheppo mdeg_get_client(mdeg_handle_t hdl)
2741ae08745Sheppo {
2751ae08745Sheppo 	int		idx;
2761ae08745Sheppo 	mdeg_clnt_t	*clnt;
2771ae08745Sheppo 
2781ae08745Sheppo 	idx = MDEG_HDL2IDX(hdl);
2791ae08745Sheppo 
2801ae08745Sheppo 	/* check if index is out of bounds */
2811ae08745Sheppo 	if ((idx < 0) || (idx >= mdeg.maxclnts)) {
2821ae08745Sheppo 		MDEG_DBG("mdeg_get_client: index out of bounds\n");
2831ae08745Sheppo 		return (NULL);
2841ae08745Sheppo 	}
2851ae08745Sheppo 
2861ae08745Sheppo 	clnt = &mdeg.tbl[idx];
2871ae08745Sheppo 
2881ae08745Sheppo 	/* check for a valid client */
2891ae08745Sheppo 	if (!clnt->valid) {
2901ae08745Sheppo 		MDEG_DBG("mdeg_get_client: client is not valid\n");
2911ae08745Sheppo 		return (NULL);
2921ae08745Sheppo 	}
2931ae08745Sheppo 
2941ae08745Sheppo 	/* make sure the handle is an exact match */
2951ae08745Sheppo 	if (clnt->hdl != hdl) {
2961ae08745Sheppo 		MDEG_DBG("mdeg_get_client: bad handle\n");
2971ae08745Sheppo 		return (NULL);
2981ae08745Sheppo 	}
2991ae08745Sheppo 
3001ae08745Sheppo 	if (clnt->magic != MDEG_MAGIC) {
3011ae08745Sheppo 		MDEG_DBG("mdeg_get_client: bad magic\n");
3021ae08745Sheppo 		return (NULL);
3031ae08745Sheppo 	}
3041ae08745Sheppo 
3051ae08745Sheppo 	return (clnt);
3061ae08745Sheppo }
3071ae08745Sheppo 
3081ae08745Sheppo /*
3091ae08745Sheppo  * Send a notification to a client immediately after it registers.
3101ae08745Sheppo  * The result_t is a list of all the nodes that match their specified
3111ae08745Sheppo  * nodes of interest, all returned on the added list. This serves
3121ae08745Sheppo  * as a base of reference to the client. All future MD updates are
3131ae08745Sheppo  * relative to this list.
3141ae08745Sheppo  */
3151ae08745Sheppo static int
mdeg_notify_client_reg(mdeg_clnt_t * clnt)3161ae08745Sheppo mdeg_notify_client_reg(mdeg_clnt_t *clnt)
3171ae08745Sheppo {
3181ae08745Sheppo 	md_t			*mdp = NULL;
3191ae08745Sheppo 	mde_str_cookie_t	nname;
3201ae08745Sheppo 	mde_str_cookie_t	aname;
3211ae08745Sheppo 	mde_cookie_t		startnode;
3221ae08745Sheppo 	int			nnodes;
3231ae08745Sheppo 	int			nodechk;
3241ae08745Sheppo 	mde_cookie_t		*listp = NULL;
3251ae08745Sheppo 	mdeg_result_t		*mdeg_res = NULL;
3261ae08745Sheppo 	int			rv = MDEG_SUCCESS;
3271ae08745Sheppo 
3281ae08745Sheppo 	mutex_enter(&mdeg.lock);
3291ae08745Sheppo 
3301ae08745Sheppo 	/*
3311ae08745Sheppo 	 * Handle the special case where the node specification
3321ae08745Sheppo 	 * is NULL. In this case, call the client callback without
3331ae08745Sheppo 	 * any results. All processing is left to the client.
3341ae08745Sheppo 	 */
3351ae08745Sheppo 	if (clnt->pspec == NULL) {
3361ae08745Sheppo 		/* call the client callback */
337*a5efd12cSToomas Soome 		(void) (*clnt->cb)(clnt->cb_arg, NULL);
3381ae08745Sheppo 		goto done;
3391ae08745Sheppo 	}
3401ae08745Sheppo 
3411ae08745Sheppo 	if ((mdp = md_get_handle()) == NULL) {
3421ae08745Sheppo 		cmn_err(CE_WARN, "unable to retrieve current MD");
3431ae08745Sheppo 		rv = MDEG_FAILURE;
3441ae08745Sheppo 		goto done;
3451ae08745Sheppo 	}
3461ae08745Sheppo 
3471ae08745Sheppo 	startnode = mdeg_find_start_node(mdp, clnt->pspec);
3481ae08745Sheppo 	if (startnode == MDE_INVAL_ELEM_COOKIE) {
3491ae08745Sheppo 		/* not much we can do */
3501ae08745Sheppo 		cmn_err(CE_WARN, "unable to match node specifier");
3511ae08745Sheppo 		rv = MDEG_FAILURE;
3521ae08745Sheppo 		goto done;
3531ae08745Sheppo 	}
3541ae08745Sheppo 
3551ae08745Sheppo 	/*
3561ae08745Sheppo 	 * Use zalloc to provide correct default values for the
3571ae08745Sheppo 	 * unused removed, match_prev, and match_curr lists.
3581ae08745Sheppo 	 */
3591ae08745Sheppo 	mdeg_res = kmem_zalloc(sizeof (mdeg_result_t), KM_SLEEP);
3601ae08745Sheppo 
3611ae08745Sheppo 	nname = md_find_name(mdp, clnt->nmatch->namep);
3621ae08745Sheppo 	aname = md_find_name(mdp, "fwd");
3631ae08745Sheppo 
3641ae08745Sheppo 	nnodes = md_scan_dag(mdp, startnode, nname, aname, NULL);
3651ae08745Sheppo 
3661ae08745Sheppo 	if (nnodes == 0) {
3671ae08745Sheppo 		MDEG_DBG("mdeg_notify_client_reg: no nodes of interest\n");
3681ae08745Sheppo 		rv = MDEG_SUCCESS;
3691ae08745Sheppo 		goto done;
3701ae08745Sheppo 	} else if (nnodes == -1) {
3711ae08745Sheppo 		MDEG_DBG("error scanning DAG\n");
3721ae08745Sheppo 		rv = MDEG_FAILURE;
3731ae08745Sheppo 		goto done;
3741ae08745Sheppo 	}
3751ae08745Sheppo 
3761ae08745Sheppo 	MDEG_DBG("mdeg_notify_client_reg: %d node%s of interest\n",
3771ae08745Sheppo 	    nnodes, (nnodes == 1) ? "" : "s");
3781ae08745Sheppo 
3791ae08745Sheppo 	/* get the list of nodes of interest */
3801ae08745Sheppo 	listp = kmem_alloc(sizeof (mde_cookie_t) * nnodes, KM_SLEEP);
3811ae08745Sheppo 	nodechk = md_scan_dag(mdp, startnode, nname, aname, listp);
3821ae08745Sheppo 
3831ae08745Sheppo 	ASSERT(nodechk == nnodes);
3841ae08745Sheppo 
3851ae08745Sheppo 	mdeg_res->added.mdp = mdp;
3861ae08745Sheppo 	mdeg_res->added.mdep = listp;
3871ae08745Sheppo 	mdeg_res->added.nelem = nnodes;
3881ae08745Sheppo 
3891ae08745Sheppo 	/* call the client callback */
390*a5efd12cSToomas Soome 	(void) (*clnt->cb)(clnt->cb_arg, mdeg_res);
3911ae08745Sheppo 
3921ae08745Sheppo done:
3931ae08745Sheppo 	mutex_exit(&mdeg.lock);
3941ae08745Sheppo 
3951ae08745Sheppo 	if (mdp)
3961ae08745Sheppo 		(void) md_fini_handle(mdp);
3971ae08745Sheppo 
3981ae08745Sheppo 	if (listp)
3991ae08745Sheppo 		kmem_free(listp, sizeof (mde_cookie_t) * nnodes);
4001ae08745Sheppo 
4011ae08745Sheppo 	if (mdeg_res)
4021ae08745Sheppo 		kmem_free(mdeg_res, sizeof (mdeg_result_t));
4031ae08745Sheppo 
4041ae08745Sheppo 	return (rv);
4051ae08745Sheppo }
4061ae08745Sheppo 
4071ae08745Sheppo /*
4081ae08745Sheppo  * Register to receive an event notification when the system
4091ae08745Sheppo  * machine description is updated.
4101ae08745Sheppo  *
4111ae08745Sheppo  * Passing NULL for the node specification parameter is valid
4121ae08745Sheppo  * as long as the match specification is also NULL. In this
4131ae08745Sheppo  * case, the client will receive a notification when the MD
4141ae08745Sheppo  * has been updated, but the callback will not include any
4151ae08745Sheppo  * information. The client is then responsible for obtaining
4161ae08745Sheppo  * its own copy of the system MD and performing any processing
4171ae08745Sheppo  * manually.
4181ae08745Sheppo  */
4191ae08745Sheppo int
mdeg_register(mdeg_node_spec_t * pspecp,mdeg_node_match_t * nmatchp,mdeg_cb_t cb,void * cb_arg,mdeg_handle_t * hdlp)4201ae08745Sheppo mdeg_register(mdeg_node_spec_t *pspecp, mdeg_node_match_t *nmatchp,
4211ae08745Sheppo     mdeg_cb_t cb, void *cb_arg, mdeg_handle_t *hdlp)
4221ae08745Sheppo {
4231ae08745Sheppo 	mdeg_clnt_t	*clnt;
4241ae08745Sheppo 
4250c4606f0SWENTAO YANG 	/* should never be called from a callback */
4260c4606f0SWENTAO YANG 	ASSERT(!taskq_member(mdeg.taskq, curthread));
4271ae08745Sheppo 
4281ae08745Sheppo 	/* node spec and node match must both be valid, or both NULL */
4291ae08745Sheppo 	if (((pspecp != NULL) && (nmatchp == NULL)) ||
4301ae08745Sheppo 	    ((pspecp == NULL) && (nmatchp != NULL))) {
4311ae08745Sheppo 		MDEG_DBG("mdeg_register: invalid parameters\n");
4321ae08745Sheppo 		return (MDEG_FAILURE);
4331ae08745Sheppo 	}
4341ae08745Sheppo 
4351ae08745Sheppo 	rw_enter(&mdeg.rwlock, RW_WRITER);
4361ae08745Sheppo 
4371ae08745Sheppo 	clnt = mdeg_alloc_clnt();
4381ae08745Sheppo 
4391ae08745Sheppo 	ASSERT(clnt);
4401ae08745Sheppo 
4411ae08745Sheppo 	/*
4421ae08745Sheppo 	 * Fill in the rest of the data
4431ae08745Sheppo 	 */
4441ae08745Sheppo 	clnt->nmatch = nmatchp;
4451ae08745Sheppo 	clnt->pspec = pspecp;
4461ae08745Sheppo 	clnt->cb = cb;
4471ae08745Sheppo 	clnt->cb_arg = cb_arg;
4481ae08745Sheppo 	clnt->magic = MDEG_MAGIC;
4491ae08745Sheppo 
4501ae08745Sheppo 	/* do this last */
4511ae08745Sheppo 	clnt->valid = B_TRUE;
4521ae08745Sheppo 
4531ae08745Sheppo 	MDEG_DBG("client registered (0x%lx):\n", clnt->hdl);
4541ae08745Sheppo 	MDEG_DUMP_CLNT(clnt);
4551ae08745Sheppo 
4561ae08745Sheppo 	mdeg.nclnts++;
4571ae08745Sheppo 
4581ae08745Sheppo 	if (mdeg_notify_client_reg(clnt) != MDEG_SUCCESS) {
4591ae08745Sheppo 		bzero(clnt, sizeof (mdeg_clnt_t));
4601ae08745Sheppo 		rw_exit(&mdeg.rwlock);
4611ae08745Sheppo 		return (MDEG_FAILURE);
4621ae08745Sheppo 	}
4631ae08745Sheppo 
4641ae08745Sheppo 	rw_exit(&mdeg.rwlock);
4651ae08745Sheppo 
4661ae08745Sheppo 	*hdlp = clnt->hdl;
4671ae08745Sheppo 
4681ae08745Sheppo 	return (MDEG_SUCCESS);
4691ae08745Sheppo }
4701ae08745Sheppo 
4711ae08745Sheppo int
mdeg_unregister(mdeg_handle_t hdl)4721ae08745Sheppo mdeg_unregister(mdeg_handle_t hdl)
4731ae08745Sheppo {
4741ae08745Sheppo 	mdeg_clnt_t	*clnt;
4751ae08745Sheppo 	mdeg_handle_t	mdh;
4761ae08745Sheppo 
4770c4606f0SWENTAO YANG 	/* should never be called from a callback */
4780c4606f0SWENTAO YANG 	ASSERT(!taskq_member(mdeg.taskq, curthread));
4790c4606f0SWENTAO YANG 
4800c4606f0SWENTAO YANG 	rw_enter(&mdeg.rwlock, RW_WRITER);
4811ae08745Sheppo 
4821ae08745Sheppo 	/* lookup the client */
4831ae08745Sheppo 	if ((clnt = mdeg_get_client(hdl)) == NULL) {
4840c4606f0SWENTAO YANG 		rw_exit(&mdeg.rwlock);
4851ae08745Sheppo 		return (MDEG_FAILURE);
4861ae08745Sheppo 	}
4871ae08745Sheppo 
4881ae08745Sheppo 	MDEG_DBG("client unregistered (0x%lx):\n", hdl);
4891ae08745Sheppo 	MDEG_DUMP_CLNT(clnt);
4901ae08745Sheppo 
4911ae08745Sheppo 	/* save the handle to prevent reuse */
4921ae08745Sheppo 	mdh = clnt->hdl;
4931ae08745Sheppo 	bzero(clnt, sizeof (mdeg_clnt_t));
4941ae08745Sheppo 
4951ae08745Sheppo 	clnt->hdl = mdh;
4961ae08745Sheppo 
4971ae08745Sheppo 	mdeg.nclnts--;
4981ae08745Sheppo 
4991ae08745Sheppo 	rw_exit(&mdeg.rwlock);
5001ae08745Sheppo 
5011ae08745Sheppo 	return (MDEG_SUCCESS);
5021ae08745Sheppo }
5031ae08745Sheppo 
5041ae08745Sheppo /*
5051ae08745Sheppo  * Simple algorithm for now, grab the global lock and let all
5061ae08745Sheppo  * the clients update themselves in parallel. There is a lot of
5071ae08745Sheppo  * room for improvement here. We could eliminate some scans of
5084e476149Srsmaeda  * the DAG by incrementally scanning at lower levels of the DAG
5091ae08745Sheppo  * rather than having each client start its own scan from the root.
5101ae08745Sheppo  */
5111ae08745Sheppo void
mdeg_notify_clients(void)5121ae08745Sheppo mdeg_notify_clients(void)
5131ae08745Sheppo {
5141ae08745Sheppo 	md_t		*md_new;
5151ae08745Sheppo 	mdeg_clnt_t	*clnt;
5161ae08745Sheppo 	int		idx;
5171ae08745Sheppo 	int		nclnt;
5181ae08745Sheppo 
5191ae08745Sheppo 	rw_enter(&mdeg.rwlock, RW_READER);
5201ae08745Sheppo 	mutex_enter(&mdeg.lock);
5211ae08745Sheppo 
5221ae08745Sheppo 	/*
5231ae08745Sheppo 	 * Rotate the MDs
5241ae08745Sheppo 	 */
5251ae08745Sheppo 	if ((md_new = md_get_handle()) == NULL) {
5261ae08745Sheppo 		cmn_err(CE_WARN, "unable to retrieve new MD");
5271ae08745Sheppo 		goto done;
5281ae08745Sheppo 	}
5291ae08745Sheppo 
5301ae08745Sheppo 	if (mdeg.md_prev) {
5311ae08745Sheppo 		(void) md_fini_handle(mdeg.md_prev);
5321ae08745Sheppo 	}
5331ae08745Sheppo 
5341ae08745Sheppo 	mdeg.md_prev = mdeg.md_curr;
5351ae08745Sheppo 	mdeg.md_curr = md_new;
5361ae08745Sheppo 
5371ae08745Sheppo 	if (mdeg.nclnts == 0) {
5381ae08745Sheppo 		MDEG_DBG("mdeg_notify_clients: no clients registered\n");
5391ae08745Sheppo 		goto done;
5401ae08745Sheppo 	}
5411ae08745Sheppo 
5421ae08745Sheppo 	/* dispatch the update notification to all clients */
5431ae08745Sheppo 	for (idx = 0, nclnt = 0; idx < mdeg.maxclnts; idx++) {
5441ae08745Sheppo 		clnt = &mdeg.tbl[idx];
5451ae08745Sheppo 
5461ae08745Sheppo 		if (!clnt->valid)
5471ae08745Sheppo 			continue;
5481ae08745Sheppo 
5491ae08745Sheppo 		MDEG_DBG("notifying client 0x%lx (%d/%d)\n", clnt->hdl,
5501ae08745Sheppo 		    ++nclnt, mdeg.nclnts);
5511ae08745Sheppo 
5521ae08745Sheppo 		(void) taskq_dispatch(mdeg.taskq, mdeg_notify_client,
5531ae08745Sheppo 		    (void *)clnt, TQ_SLEEP);
5541ae08745Sheppo 	}
5551ae08745Sheppo 
556273a517fSHaik Aftandilian 	/*
557273a517fSHaik Aftandilian 	 * Wait for all mdeg_notify_client notifications to
558273a517fSHaik Aftandilian 	 * finish while we are still holding mdeg.rwlock.
559273a517fSHaik Aftandilian 	 */
5601ae08745Sheppo 	taskq_wait(mdeg.taskq);
5611ae08745Sheppo 
5621ae08745Sheppo done:
5631ae08745Sheppo 	mutex_exit(&mdeg.lock);
5641ae08745Sheppo 	rw_exit(&mdeg.rwlock);
5651ae08745Sheppo }
5661ae08745Sheppo 
5671ae08745Sheppo static void
mdeg_notify_client(void * arg)5681ae08745Sheppo mdeg_notify_client(void *arg)
5691ae08745Sheppo {
5701ae08745Sheppo 	mdeg_clnt_t		*clnt = (mdeg_clnt_t *)arg;
5711ae08745Sheppo 	md_diff_cookie_t	mdd = MD_INVAL_DIFF_COOKIE;
5721ae08745Sheppo 	mdeg_result_t		mdeg_res;
5731ae08745Sheppo 	mde_cookie_t		md_prev_start;
5741ae08745Sheppo 	mde_cookie_t		md_curr_start;
5751ae08745Sheppo 
576273a517fSHaik Aftandilian 	/*
577273a517fSHaik Aftandilian 	 * mdeg.rwlock must be held as a reader while this function
578273a517fSHaik Aftandilian 	 * executes. However, we do not need to acquire the lock as a
579273a517fSHaik Aftandilian 	 * reader here because it is held as a reader by the thread
580273a517fSHaik Aftandilian 	 * executing mdeg_notify_clients which triggers the execution
581273a517fSHaik Aftandilian 	 * of this function from a taskq. Since mdeg_notify_clients
582273a517fSHaik Aftandilian 	 * holds the lock as a reader until the taskq callbacks have
583273a517fSHaik Aftandilian 	 * completed, it will be held for the life of this function call.
584273a517fSHaik Aftandilian 	 * Furthermore, we must not attempt to acquire the lock as a
585273a517fSHaik Aftandilian 	 * reader with rw_enter because if there is a pending writer,
586273a517fSHaik Aftandilian 	 * we will block, creating a circular deadlock with this function,
587273a517fSHaik Aftandilian 	 * the writer, and mdeg_notify_clients. Since we do not need
588273a517fSHaik Aftandilian 	 * to acquire the lock, just assert that it is held.
589273a517fSHaik Aftandilian 	 */
590273a517fSHaik Aftandilian 	ASSERT(RW_READ_HELD(&mdeg.rwlock));
5911ae08745Sheppo 
5921ae08745Sheppo 	if (!mdeg.enabled) {
5931ae08745Sheppo 		/* trying to shutdown */
5941ae08745Sheppo 		MDEG_DBG("mdeg_notify_client: mdeg disabled, aborting\n");
5951ae08745Sheppo 		goto cleanup;
5961ae08745Sheppo 	}
5971ae08745Sheppo 
5981ae08745Sheppo 	/*
5991ae08745Sheppo 	 * Handle the special case where the node specification
6001ae08745Sheppo 	 * is NULL. In this case, call the client callback without
6011ae08745Sheppo 	 * any results. All processing is left to the client.
6021ae08745Sheppo 	 */
6031ae08745Sheppo 	if (clnt->pspec == NULL) {
6041ae08745Sheppo 		/* call the client callback */
605*a5efd12cSToomas Soome 		(void) (*clnt->cb)(clnt->cb_arg, NULL);
6061ae08745Sheppo 
6071ae08745Sheppo 		MDEG_DBG("MDEG client callback done\n");
6081ae08745Sheppo 		goto cleanup;
6091ae08745Sheppo 	}
6101ae08745Sheppo 
6111ae08745Sheppo 	/* find our start nodes */
6121ae08745Sheppo 	md_prev_start = mdeg_find_start_node(mdeg.md_prev, clnt->pspec);
6131ae08745Sheppo 	if (md_prev_start == MDE_INVAL_ELEM_COOKIE) {
6141ae08745Sheppo 		goto cleanup;
6151ae08745Sheppo 	}
6161ae08745Sheppo 
6171ae08745Sheppo 	md_curr_start = mdeg_find_start_node(mdeg.md_curr, clnt->pspec);
6181ae08745Sheppo 	if (md_curr_start == MDE_INVAL_ELEM_COOKIE) {
6191ae08745Sheppo 		goto cleanup;
6201ae08745Sheppo 	}
6211ae08745Sheppo 
6221ae08745Sheppo 	/* diff the MDs */
6231ae08745Sheppo 	mdd = md_diff_init(mdeg.md_prev, md_prev_start, mdeg.md_curr,
6241ae08745Sheppo 	    md_curr_start, clnt->nmatch->namep, clnt->nmatch->matchp);
6251ae08745Sheppo 
6261ae08745Sheppo 	if (mdd == MD_INVAL_DIFF_COOKIE) {
6271ae08745Sheppo 		MDEG_DBG("unable to diff MDs\n");
6281ae08745Sheppo 		goto cleanup;
6291ae08745Sheppo 	}
6301ae08745Sheppo 
6311ae08745Sheppo 	/*
6321ae08745Sheppo 	 * Cache the results of the diff
6331ae08745Sheppo 	 */
6341ae08745Sheppo 	mdeg_get_diff_results(mdd, &mdeg_res);
6351ae08745Sheppo 
6361ae08745Sheppo 	/* call the client callback */
637*a5efd12cSToomas Soome 	(void) (*clnt->cb)(clnt->cb_arg, &mdeg_res);
6381ae08745Sheppo 
6391ae08745Sheppo 	MDEG_DBG("MDEG client callback done\n");
6401ae08745Sheppo 
6411ae08745Sheppo cleanup:
6421ae08745Sheppo 	if (mdd != MD_INVAL_DIFF_COOKIE)
6431ae08745Sheppo 		(void) md_diff_fini(mdd);
6441ae08745Sheppo }
6451ae08745Sheppo 
6461ae08745Sheppo static mde_cookie_t
mdeg_find_start_node(md_t * md,mdeg_node_spec_t * nspec)6471ae08745Sheppo mdeg_find_start_node(md_t *md, mdeg_node_spec_t *nspec)
6481ae08745Sheppo {
6491ae08745Sheppo 	mde_cookie_t		*nodesp;
6501ae08745Sheppo 	mde_str_cookie_t	nname;
6511ae08745Sheppo 	mde_str_cookie_t	aname;
6521ae08745Sheppo 	int			nnodes;
6531ae08745Sheppo 	int			idx;
6541ae08745Sheppo 
6551ae08745Sheppo 	if ((md == NULL) || (nspec == NULL))
6561ae08745Sheppo 		return (MDE_INVAL_ELEM_COOKIE);
6571ae08745Sheppo 
6581ae08745Sheppo 	nname = md_find_name(md, nspec->namep);
6591ae08745Sheppo 	aname = md_find_name(md, "fwd");
6601ae08745Sheppo 
661*a5efd12cSToomas Soome 	nnodes = md_scan_dag(md, 0, nname, aname, NULL);
6621ae08745Sheppo 	if (nnodes == 0)
6631ae08745Sheppo 		return (MDE_INVAL_ELEM_COOKIE);
6641ae08745Sheppo 
6651ae08745Sheppo 	nodesp = kmem_alloc(sizeof (mde_cookie_t) * nnodes, KM_SLEEP);
6661ae08745Sheppo 
667*a5efd12cSToomas Soome 	(void) md_scan_dag(md, 0, nname, aname, nodesp);
6681ae08745Sheppo 
6691ae08745Sheppo 	for (idx = 0; idx < nnodes; idx++) {
6701ae08745Sheppo 
6711ae08745Sheppo 		if (mdeg_node_spec_match(md, nodesp[idx], nspec)) {
6721ae08745Sheppo 			mde_cookie_t res = nodesp[idx];
6731ae08745Sheppo 
6741ae08745Sheppo 			kmem_free(nodesp, sizeof (mde_cookie_t) * nnodes);
6751ae08745Sheppo 			return (res);
6761ae08745Sheppo 		}
6771ae08745Sheppo 	}
6781ae08745Sheppo 
6791ae08745Sheppo 	kmem_free(nodesp, sizeof (mde_cookie_t) * nnodes);
6801ae08745Sheppo 	return (MDE_INVAL_ELEM_COOKIE);
6811ae08745Sheppo }
6821ae08745Sheppo 
6831ae08745Sheppo static boolean_t
mdeg_node_spec_match(md_t * md,mde_cookie_t node,mdeg_node_spec_t * nspec)6841ae08745Sheppo mdeg_node_spec_match(md_t *md, mde_cookie_t node, mdeg_node_spec_t *nspec)
6851ae08745Sheppo {
6861ae08745Sheppo 	mdeg_prop_spec_t	*prop;
6871ae08745Sheppo 
6881ae08745Sheppo 	ASSERT(md && nspec);
6891ae08745Sheppo 	ASSERT(node != MDE_INVAL_ELEM_COOKIE);
6901ae08745Sheppo 
6911ae08745Sheppo 	prop = nspec->specp;
6921ae08745Sheppo 
6931ae08745Sheppo 	while (prop->type != MDET_LIST_END) {
6941ae08745Sheppo 
6951ae08745Sheppo 		switch (prop->type) {
6961ae08745Sheppo 		case MDET_PROP_VAL: {
6971ae08745Sheppo 			uint64_t val;
6981ae08745Sheppo 
6991ae08745Sheppo 			if (md_get_prop_val(md, node, prop->namep, &val) != 0)
7001ae08745Sheppo 				return (B_FALSE);
7011ae08745Sheppo 
7021ae08745Sheppo 			if (prop->ps_val != val)
7031ae08745Sheppo 				return (B_FALSE);
7041ae08745Sheppo 
7051ae08745Sheppo 			break;
7061ae08745Sheppo 		}
7071ae08745Sheppo 		case MDET_PROP_STR: {
7081ae08745Sheppo 			char	*str;
7091ae08745Sheppo 
7101ae08745Sheppo 			if (md_get_prop_str(md, node, prop->namep, &str) != 0)
7111ae08745Sheppo 				return (B_FALSE);
7121ae08745Sheppo 
7131ae08745Sheppo 			if (strcmp(prop->ps_str, str) != 0)
7141ae08745Sheppo 				return (B_FALSE);
7151ae08745Sheppo 
7161ae08745Sheppo 			break;
7171ae08745Sheppo 		}
7181ae08745Sheppo 
7191ae08745Sheppo 		default:
7201ae08745Sheppo 			return (B_FALSE);
7211ae08745Sheppo 		}
7221ae08745Sheppo 
7231ae08745Sheppo 		prop++;
7241ae08745Sheppo 	}
7251ae08745Sheppo 
7261ae08745Sheppo 	return (B_TRUE);
7271ae08745Sheppo }
7281ae08745Sheppo 
7291ae08745Sheppo static void
mdeg_get_diff_results(md_diff_cookie_t mdd,mdeg_result_t * res)7301ae08745Sheppo mdeg_get_diff_results(md_diff_cookie_t mdd, mdeg_result_t *res)
7311ae08745Sheppo {
7321ae08745Sheppo 	/*
7331ae08745Sheppo 	 * Cache added nodes.
7341ae08745Sheppo 	 */
7351ae08745Sheppo 	res->added.mdp = mdeg.md_curr;
7361ae08745Sheppo 	res->added.nelem = md_diff_added(mdd, &(res->added.mdep));
7371ae08745Sheppo 
7381ae08745Sheppo 	if (res->added.nelem == -1) {
7391ae08745Sheppo 		bzero(&(res->added), sizeof (mdeg_diff_t));
7401ae08745Sheppo 	}
7411ae08745Sheppo 
7421ae08745Sheppo 	/*
7431ae08745Sheppo 	 * Cache removed nodes.
7441ae08745Sheppo 	 */
7451ae08745Sheppo 	res->removed.mdp = mdeg.md_prev;
7461ae08745Sheppo 	res->removed.nelem = md_diff_removed(mdd, &(res->removed.mdep));
7471ae08745Sheppo 
7481ae08745Sheppo 	if (res->removed.nelem == -1) {
7491ae08745Sheppo 		bzero(&(res->removed), sizeof (mdeg_diff_t));
7501ae08745Sheppo 	}
7511ae08745Sheppo 
7521ae08745Sheppo 	/*
7531ae08745Sheppo 	 * Cache matching node pairs.
7541ae08745Sheppo 	 */
7551ae08745Sheppo 	res->match_curr.mdp = mdeg.md_curr;
7561ae08745Sheppo 	res->match_prev.mdp = mdeg.md_prev;
7571ae08745Sheppo 	res->match_curr.nelem = md_diff_matched(mdd, &(res->match_prev.mdep),
7581ae08745Sheppo 	    &(res->match_curr.mdep));
7591ae08745Sheppo 	res->match_prev.nelem = res->match_curr.nelem;
7601ae08745Sheppo 
7611ae08745Sheppo 	if (res->match_prev.nelem == -1) {
7621ae08745Sheppo 		bzero(&(res->match_prev), sizeof (mdeg_diff_t));
7631ae08745Sheppo 		bzero(&(res->match_curr), sizeof (mdeg_diff_t));
7641ae08745Sheppo 	}
7651ae08745Sheppo }
7661ae08745Sheppo 
7671ae08745Sheppo #ifdef DEBUG
7681ae08745Sheppo /*
7691ae08745Sheppo  * Generate a string that represents the node specifier
7701ae08745Sheppo  * structure. Clamp the string length if the specifier
7711ae08745Sheppo  * structure contains too much information.
7721ae08745Sheppo  *
7731ae08745Sheppo  *	General form:
7741ae08745Sheppo  *
7751ae08745Sheppo  *		<nodename>:{<propname>=<propval>,...}
7761ae08745Sheppo  *	e.g.
7771ae08745Sheppo  *		vdevice:{name=vsw,reg=0x0}
7781ae08745Sheppo  */
7791ae08745Sheppo static void
mdeg_spec_str(mdeg_node_spec_t * spec,char * buf,int len)7801ae08745Sheppo mdeg_spec_str(mdeg_node_spec_t *spec, char *buf, int len)
7811ae08745Sheppo {
7821ae08745Sheppo 	mdeg_prop_spec_t	*prop;
7831ae08745Sheppo 	int			offset;
7841ae08745Sheppo 	boolean_t		first = B_TRUE;
7851ae08745Sheppo 	char			*end = buf + len;
7861ae08745Sheppo 
7871ae08745Sheppo 	offset = snprintf(buf, len, "%s:{", spec->namep);
7881ae08745Sheppo 
7891ae08745Sheppo 	buf += offset;
7901ae08745Sheppo 	len -= offset;
7911ae08745Sheppo 	if (len <= 0)
7921ae08745Sheppo 		goto trunc;
7931ae08745Sheppo 
7941ae08745Sheppo 	prop = spec->specp;
7951ae08745Sheppo 
7961ae08745Sheppo 	while (prop->type != MDET_LIST_END) {
7971ae08745Sheppo 
7981ae08745Sheppo 		switch (prop->type) {
7991ae08745Sheppo 		case MDET_PROP_VAL:
8001ae08745Sheppo 			offset = snprintf(buf, len, "%s%s=0x%lx",
8011ae08745Sheppo 			    (first) ? "" : ",", prop->namep, prop->ps_val);
8021ae08745Sheppo 			buf += offset;
8031ae08745Sheppo 			len -= offset;
8041ae08745Sheppo 			if (len <= 0)
8051ae08745Sheppo 				goto trunc;
8061ae08745Sheppo 			break;
8071ae08745Sheppo 
8081ae08745Sheppo 		case MDET_PROP_STR:
8091ae08745Sheppo 			offset = snprintf(buf, len, "%s%s=%s",
8101ae08745Sheppo 			    (first) ? "" : ",", prop->namep, prop->ps_str);
8111ae08745Sheppo 			buf += offset;
8121ae08745Sheppo 			len -= offset;
8131ae08745Sheppo 			if (len <= 0)
8141ae08745Sheppo 				goto trunc;
8151ae08745Sheppo 			break;
8161ae08745Sheppo 
8171ae08745Sheppo 		default:
8181ae08745Sheppo 			(void) snprintf(buf, len, "}");
8191ae08745Sheppo 			return;
8201ae08745Sheppo 		}
8211ae08745Sheppo 
8221ae08745Sheppo 		if (first)
8231ae08745Sheppo 			first = B_FALSE;
8241ae08745Sheppo 		prop++;
8251ae08745Sheppo 	}
8261ae08745Sheppo 
8271ae08745Sheppo 	(void) snprintf(buf, len, "}");
8281ae08745Sheppo 	return;
8291ae08745Sheppo 
8301ae08745Sheppo trunc:
8311ae08745Sheppo 	/* string too long, truncate it */
8321ae08745Sheppo 	buf = end - (strlen(trunc_str) + 1);
8331ae08745Sheppo 	(void) sprintf(buf, trunc_str);
8341ae08745Sheppo }
8351ae08745Sheppo 
8361ae08745Sheppo /*
8371ae08745Sheppo  * Generate a string that represents the match structure.
8381ae08745Sheppo  * Clamp the string length if the match structure contains
8391ae08745Sheppo  * too much information.
8401ae08745Sheppo  *
8411ae08745Sheppo  *	General form:
8421ae08745Sheppo  *
8431ae08745Sheppo  *		<nodename>:{<propname>,...}
8441ae08745Sheppo  *	e.g.
8451ae08745Sheppo  *		nmatch=vport:{reg}
8461ae08745Sheppo  */
8471ae08745Sheppo static void
mdeg_match_str(mdeg_node_match_t * match,char * buf,int len)8481ae08745Sheppo mdeg_match_str(mdeg_node_match_t *match, char *buf, int len)
8491ae08745Sheppo {
8501ae08745Sheppo 	md_prop_match_t	*prop;
8511ae08745Sheppo 	int		offset;
8521ae08745Sheppo 	boolean_t	first = B_TRUE;
8531ae08745Sheppo 	char		*end = buf + len;
8541ae08745Sheppo 
8551ae08745Sheppo 	offset = snprintf(buf, len, "%s:{", match->namep);
8561ae08745Sheppo 
8571ae08745Sheppo 	buf += offset;
8581ae08745Sheppo 	len -= offset;
8591ae08745Sheppo 	if (len <= 0)
8601ae08745Sheppo 		goto trunc;
8611ae08745Sheppo 
8621ae08745Sheppo 	prop = match->matchp;
8631ae08745Sheppo 
8641ae08745Sheppo 	while (prop->type != MDET_LIST_END) {
8651ae08745Sheppo 		offset = snprintf(buf, len, "%s%s", (first) ? "" : ",",
8661ae08745Sheppo 		    prop->namep);
8671ae08745Sheppo 		buf += offset;
8681ae08745Sheppo 		len -= offset;
8691ae08745Sheppo 		if (len <= 0)
8701ae08745Sheppo 			goto trunc;
8711ae08745Sheppo 
8721ae08745Sheppo 		if (first)
8731ae08745Sheppo 			first = B_FALSE;
8741ae08745Sheppo 		prop++;
8751ae08745Sheppo 	}
8761ae08745Sheppo 
8771ae08745Sheppo 	(void) snprintf(buf, len, "}");
8781ae08745Sheppo 	return;
8791ae08745Sheppo 
8801ae08745Sheppo trunc:
8811ae08745Sheppo 	/* string too long, truncate it */
8821ae08745Sheppo 	buf = end - (strlen(trunc_str) + 1);
8831ae08745Sheppo 	(void) sprintf(buf, trunc_str);
8841ae08745Sheppo }
8851ae08745Sheppo 
8861ae08745Sheppo #define	MAX_FIELD_STR	80
8871ae08745Sheppo 
8881ae08745Sheppo static void
mdeg_dump_clnt(mdeg_clnt_t * clnt)8891ae08745Sheppo mdeg_dump_clnt(mdeg_clnt_t *clnt)
8901ae08745Sheppo {
8919853d9e8SJason Beloro 	char	str[MAX_FIELD_STR] = "";
8921ae08745Sheppo 
8931ae08745Sheppo 	if (!clnt->valid) {
8941ae08745Sheppo 		MDEG_DBG("  valid=B_FALSE\n");
8951ae08745Sheppo 		return;
8961ae08745Sheppo 	}
8971ae08745Sheppo 
8989853d9e8SJason Beloro 	if (clnt->pspec) {
8999853d9e8SJason Beloro 		mdeg_spec_str(clnt->pspec, str, MAX_FIELD_STR);
9009853d9e8SJason Beloro 		MDEG_DBG("  pspecp=%s\n", str);
9019853d9e8SJason Beloro 	}
9021ae08745Sheppo 
9039853d9e8SJason Beloro 	if (clnt->nmatch) {
9049853d9e8SJason Beloro 		mdeg_match_str(clnt->nmatch, str, MAX_FIELD_STR);
9059853d9e8SJason Beloro 		MDEG_DBG("  nmatch=%s\n", str);
9069853d9e8SJason Beloro 	}
9071ae08745Sheppo }
9081ae08745Sheppo 
9091ae08745Sheppo static void
mdeg_dump_table(void)9101ae08745Sheppo mdeg_dump_table(void)
9111ae08745Sheppo {
9121ae08745Sheppo 	int		idx;
9131ae08745Sheppo 	mdeg_clnt_t	*clnt;
9141ae08745Sheppo 
9151ae08745Sheppo 	for (idx = 0; idx < mdeg.maxclnts; idx++) {
9161ae08745Sheppo 		clnt = &(mdeg.tbl[idx]);
9171ae08745Sheppo 
9181ae08745Sheppo 		MDEG_DBG("client %d (0x%lx):\n", idx, clnt->hdl);
9191ae08745Sheppo 		mdeg_dump_clnt(clnt);
9201ae08745Sheppo 	}
9211ae08745Sheppo }
9221ae08745Sheppo #endif /* DEBUG */
923