--- old/usr/src/lib/libdladm/common/libdlstat.c	Wed Feb  6 15:51:12 2019
+++ new/usr/src/lib/libdladm/common/libdlstat.c	Wed Feb  6 15:51:12 2019
@@ -23,6 +23,10 @@
  * Use is subject to license terms.
  */
 
+/*
+ * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
+ */
+
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -169,7 +173,6 @@
 dladm_get_single_mac_stat(dladm_handle_t handle, datalink_id_t linkid,
     const char *name, uint8_t type, void *val)
 {
-	kstat_ctl_t	*kcp;
 	char		module[DLPI_LINKNAME_MAX];
 	uint_t		instance;
 	char 		link[DLPI_LINKNAME_MAX];
@@ -195,30 +198,30 @@
 	if (status != DLADM_STATUS_OK)
 		return (status);
 
-	if ((kcp = kstat_open()) == NULL) {
-		warn("kstat_open operation failed");
-		return (-1);
-	}
-
 	/*
 	 * The kstat query could fail if the underlying MAC
 	 * driver was already detached.
 	 */
-	if ((ksp = kstat_lookup(kcp, module, instance, "mac")) == NULL &&
-	    (ksp = kstat_lookup(kcp, module, instance, NULL)) == NULL)
+	if (dladm_dld_kcp(handle) == NULL) {
+		warn("kstat_open operation failed");
+		return (-1);
+	}
+
+	if ((ksp = kstat_lookup(dladm_dld_kcp(handle), module, instance,
+	    "mac")) == NULL &&
+	    (ksp = kstat_lookup(dladm_dld_kcp(handle), module, instance,
+	    NULL)) == NULL)
 		goto bail;
 
-	if (kstat_read(kcp, ksp, NULL) == -1)
+	if (kstat_read(dladm_dld_kcp(handle), ksp, NULL) == -1)
 		goto bail;
 
 	if (dladm_kstat_value(ksp, name, type, val) < 0)
 		goto bail;
 
-	(void) kstat_close(kcp);
 	return (DLADM_STATUS_OK);
 
 bail:
-	(void) kstat_close(kcp);
 	return (dladm_errno2status(errno));
 }
 
@@ -718,26 +721,23 @@
 
 /* Support for legacy drivers */
 void
-i_query_legacy_stats(const char *linkname, pktsum_t *stats)
+i_query_legacy_stats(dladm_handle_t dh, const char *linkname, pktsum_t *stats)
 {
-	kstat_ctl_t	*kcp;
 	kstat_t		*ksp;
 
 	bzero(stats, sizeof (*stats));
 
-	if ((kcp = kstat_open()) == NULL)
+	if (dladm_dld_kcp(dh) == NULL)
 		return;
 
-	ksp = dladm_kstat_lookup(kcp, "link", 0, linkname, NULL);
+	ksp = dladm_kstat_lookup(dladm_dld_kcp(dh), "link", 0, linkname, NULL);
 
 	if (ksp != NULL)
-		dladm_get_stats(kcp, ksp, stats);
-
-	(void) kstat_close(kcp);
+		dladm_get_stats(dladm_dld_kcp(dh), ksp, stats);
 }
 
 void *
-i_dlstat_legacy_rx_lane_stats(const char *linkname)
+i_dlstat_legacy_rx_lane_stats(dladm_handle_t dh, const char *linkname)
 {
 	dladm_stat_chain_t	*head = NULL;
 	pktsum_t		stats;
@@ -746,7 +746,7 @@
 	bzero(&stats, sizeof (pktsum_t));
 
 	/* Query for dls stats */
-	i_query_legacy_stats(linkname, &stats);
+	i_query_legacy_stats(dh, linkname, &stats);
 
 	/* Convert to desired data type */
 	rx_lane_stat_entry = calloc(1, sizeof (rx_lane_stat_entry_t));
@@ -774,7 +774,7 @@
 }
 
 void *
-i_dlstat_legacy_tx_lane_stats(const char *linkname)
+i_dlstat_legacy_tx_lane_stats(dladm_handle_t dh, const char *linkname)
 {
 	dladm_stat_chain_t	*head = NULL;
 	pktsum_t		stats;
@@ -783,7 +783,7 @@
 	bzero(&stats, sizeof (pktsum_t));
 
 	/* Query for dls stats */
-	i_query_legacy_stats(linkname, &stats);
+	i_query_legacy_stats(dh, linkname, &stats);
 
 	/* Convert to desired data type */
 	tx_lane_stat_entry = calloc(1, sizeof (tx_lane_stat_entry_t));
@@ -892,10 +892,11 @@
 };
 
 static void
-i_dlstat_get_idlist(const char *modname, dlstat_idlist_type_t idlist_type,
+i_dlstat_get_idlist(dladm_handle_t handle, const char *modname,
+    dlstat_idlist_type_t idlist_type,
     uint_t idlist[], uint_t *size)
 {
-	kstat_ctl_t	*kcp;
+	kstat_ctl_t	*kcp = dladm_dld_kcp(handle);
 	kstat_t		*ksp;
 	char		*prefix;
 	int		prefixlen;
@@ -903,9 +904,9 @@
 
 	*size = 0;
 
-	if ((kcp = kstat_open()) == NULL) {
+	if (kcp == NULL) {
 		warn("kstat_open operation failed");
-		goto done;
+		return;
 	}
 
 	prefix = dladm_extract_idlist[idlist_type].di_prefix;
@@ -918,17 +919,13 @@
 		}
 	}
 	dladm_sort_index_list(idlist, *size);
-
-done:
-	(void) kstat_close(kcp);
 }
 
 static dladm_stat_chain_t *
-i_dlstat_query_stats(const char *modname, const char *prefix,
-    uint_t idlist[], uint_t idlist_size,
+i_dlstat_query_stats(dladm_handle_t handle, const char *modname,
+    const char *prefix, uint_t idlist[], uint_t idlist_size,
     void * (*fn)(kstat_ctl_t *, kstat_t *, int))
 {
-	kstat_ctl_t		*kcp;
 	kstat_t			*ksp;
 	char			statname[MAXLINKNAMELEN];
 	int 			i = 0;
@@ -935,7 +932,7 @@
 	dladm_stat_chain_t 	*head = NULL, *prev = NULL;
 	dladm_stat_chain_t	*curr;
 
-	if ((kcp = kstat_open()) == NULL) {
+	if (dladm_dld_kcp(handle) == NULL) {
 		warn("kstat_open operation failed");
 		return (NULL);
 	}
@@ -946,7 +943,8 @@
 		(void) snprintf(statname, sizeof (statname), "%s%d", prefix,
 		    index);
 
-		ksp = dladm_kstat_lookup(kcp, modname, 0, statname, NULL);
+		ksp = dladm_kstat_lookup(dladm_dld_kcp(handle), modname, 0,
+		    statname, NULL);
 		if (ksp == NULL)
 			continue;
 
@@ -954,7 +952,7 @@
 		if (curr == NULL)
 			break;
 
-		curr->dc_statentry = fn(kcp, ksp, index);
+		curr->dc_statentry = fn(dladm_dld_kcp(handle), ksp, index);
 		if (curr->dc_statentry == NULL) {
 			free(curr);
 			break;
@@ -972,21 +970,20 @@
 		prev = curr;
 	}
 done:
-	(void) kstat_close(kcp);
 	return (head);
 }
 
 static misc_stat_entry_t *
-i_dlstat_misc_stats(const char *linkname)
+i_dlstat_misc_stats(dladm_handle_t handle, const char *linkname)
 {
-	kstat_ctl_t		*kcp;
 	kstat_t			*ksp;
 	misc_stat_entry_t 	*misc_stat_entry = NULL;
 
-	if ((kcp = kstat_open()) == NULL)
+	if (dladm_dld_kcp(handle) == NULL)
 		return (NULL);
 
-	ksp = dladm_kstat_lookup(kcp, linkname, 0, DLSTAT_MAC_MISC_STAT, NULL);
+	ksp = dladm_kstat_lookup(dladm_dld_kcp(handle), linkname, 0,
+	    DLSTAT_MAC_MISC_STAT, NULL);
 	if (ksp == NULL)
 		goto done;
 
@@ -994,10 +991,10 @@
 	if (misc_stat_entry == NULL)
 		goto done;
 
-	i_dlstat_get_stats(kcp, ksp, &misc_stat_entry->mse_stats,
+	i_dlstat_get_stats(dladm_dld_kcp(handle), ksp,
+	    &misc_stat_entry->mse_stats,
 	    misc_stats_list, MISC_STAT_SIZE);
 done:
-	(void) kstat_close(kcp);
 	return (misc_stat_entry);
 }
 
@@ -1108,11 +1105,12 @@
 }
 
 static dladm_stat_chain_t *
-i_dlstat_rx_local_stats(const char *linkname)
+i_dlstat_rx_local_stats(dladm_handle_t handle, const char *linkname)
 {
 	dladm_stat_chain_t	*local_stats = NULL;
 
-	local_stats = i_dlstat_query_stats(linkname, DLSTAT_MAC_RX_SWLANE,
+	local_stats = i_dlstat_query_stats(handle, linkname,
+	    DLSTAT_MAC_RX_SWLANE,
 	    default_idlist, default_idlist_size,
 	    i_dlstat_rx_local_retrieve_stat);
 
@@ -1124,13 +1122,13 @@
 }
 
 static dladm_stat_chain_t *
-i_dlstat_rx_bcast_stats(const char *linkname)
+i_dlstat_rx_bcast_stats(dladm_handle_t handle, const char *linkname)
 {
 	misc_stat_entry_t	*misc_stat_entry;
 	dladm_stat_chain_t	*head = NULL;
 	rx_lane_stat_entry_t	*rx_lane_stat_entry;
 
-	misc_stat_entry = i_dlstat_misc_stats(linkname);
+	misc_stat_entry = i_dlstat_misc_stats(handle, linkname);
 	if (misc_stat_entry == NULL)
 		goto done;
 
@@ -1166,13 +1164,13 @@
 }
 
 static dladm_stat_chain_t *
-i_dlstat_rx_defunctlane_stats(const char *linkname)
+i_dlstat_rx_defunctlane_stats(dladm_handle_t handle, const char *linkname)
 {
 	misc_stat_entry_t	*misc_stat_entry;
 	dladm_stat_chain_t	*head = NULL;
 	rx_lane_stat_entry_t	*rx_lane_stat_entry;
 
-	misc_stat_entry = i_dlstat_misc_stats(linkname);
+	misc_stat_entry = i_dlstat_misc_stats(handle, linkname);
 	if (misc_stat_entry == NULL)
 		goto done;
 
@@ -1214,15 +1212,15 @@
 }
 
 static dladm_stat_chain_t *
-i_dlstat_rx_hwlane_stats(const char *linkname)
+i_dlstat_rx_hwlane_stats(dladm_handle_t handle, const char *linkname)
 {
 	uint_t	rx_hwlane_idlist[MAX_RINGS_PER_GROUP];
 	uint_t	rx_hwlane_idlist_size;
 
-	i_dlstat_get_idlist(linkname, DLSTAT_RX_HWLANE_IDLIST,
+	i_dlstat_get_idlist(handle, linkname, DLSTAT_RX_HWLANE_IDLIST,
 	    rx_hwlane_idlist, &rx_hwlane_idlist_size);
 
-	return (i_dlstat_query_stats(linkname, DLSTAT_MAC_RX_HWLANE,
+	return (i_dlstat_query_stats(handle, linkname, DLSTAT_MAC_RX_HWLANE,
 	    rx_hwlane_idlist, rx_hwlane_idlist_size,
 	    i_dlstat_rx_hwlane_retrieve_stat));
 }
@@ -1232,7 +1230,7 @@
 i_dlstat_rx_swlane_stats(dladm_handle_t dh, datalink_id_t linkid,
     const char *linkname)
 {
-	return (i_dlstat_query_stats(linkname, DLSTAT_MAC_RX_SWLANE,
+	return (i_dlstat_query_stats(dh, linkname, DLSTAT_MAC_RX_SWLANE,
 	    default_idlist, default_idlist_size,
 	    i_dlstat_rx_swlane_retrieve_stat));
 }
@@ -1260,14 +1258,14 @@
 	}
 
 	if (is_legacy_driver) {
-		head = i_dlstat_legacy_rx_lane_stats(linkname);
+		head = i_dlstat_legacy_rx_lane_stats(dh, linkname);
 		goto done;
 	}
 
-	local_stats = i_dlstat_rx_local_stats(linkname);
-	bcast_stats = i_dlstat_rx_bcast_stats(linkname);
-	defunctlane_stats = i_dlstat_rx_defunctlane_stats(linkname);
-	lane_stats = i_dlstat_rx_hwlane_stats(linkname);
+	local_stats = i_dlstat_rx_local_stats(dh, linkname);
+	bcast_stats = i_dlstat_rx_bcast_stats(dh, linkname);
+	defunctlane_stats = i_dlstat_rx_defunctlane_stats(dh, linkname);
+	lane_stats = i_dlstat_rx_hwlane_stats(dh, linkname);
 	if (lane_stats == NULL)
 		lane_stats = i_dlstat_rx_swlane_stats(dh, linkid, linkname);
 
@@ -1350,13 +1348,13 @@
 }
 
 static dladm_stat_chain_t *
-i_dlstat_tx_bcast_stats(const char *linkname)
+i_dlstat_tx_bcast_stats(dladm_handle_t handle, const char *linkname)
 {
 	misc_stat_entry_t	*misc_stat_entry;
 	dladm_stat_chain_t	*head = NULL;
 	tx_lane_stat_entry_t	*tx_lane_stat_entry;
 
-	misc_stat_entry = i_dlstat_misc_stats(linkname);
+	misc_stat_entry = i_dlstat_misc_stats(handle, linkname);
 	if (misc_stat_entry == NULL)
 		goto done;
 
@@ -1390,13 +1388,13 @@
 }
 
 static dladm_stat_chain_t *
-i_dlstat_tx_defunctlane_stats(const char *linkname)
+i_dlstat_tx_defunctlane_stats(dladm_handle_t handle, const char *linkname)
 {
 	misc_stat_entry_t	*misc_stat_entry;
 	dladm_stat_chain_t	*head = NULL;
 	tx_lane_stat_entry_t	*tx_lane_stat_entry;
 
-	misc_stat_entry = i_dlstat_misc_stats(linkname);
+	misc_stat_entry = i_dlstat_misc_stats(handle, linkname);
 	if (misc_stat_entry == NULL)
 		goto done;
 
@@ -1428,15 +1426,15 @@
 }
 
 static dladm_stat_chain_t *
-i_dlstat_tx_hwlane_stats(const char *linkname)
+i_dlstat_tx_hwlane_stats(dladm_handle_t handle, const char *linkname)
 {
 	uint_t	tx_hwlane_idlist[MAX_RINGS_PER_GROUP];
 	uint_t	tx_hwlane_idlist_size;
 
-	i_dlstat_get_idlist(linkname, DLSTAT_TX_HWLANE_IDLIST,
+	i_dlstat_get_idlist(handle, linkname, DLSTAT_TX_HWLANE_IDLIST,
 	    tx_hwlane_idlist, &tx_hwlane_idlist_size);
 
-	return (i_dlstat_query_stats(linkname, DLSTAT_MAC_TX_HWLANE,
+	return (i_dlstat_query_stats(handle, linkname, DLSTAT_MAC_TX_HWLANE,
 	    tx_hwlane_idlist, tx_hwlane_idlist_size,
 	    i_dlstat_tx_hwlane_retrieve_stat));
 }
@@ -1446,7 +1444,7 @@
 i_dlstat_tx_swlane_stats(dladm_handle_t dh, datalink_id_t linkid,
     const char *linkname)
 {
-	return (i_dlstat_query_stats(linkname, DLSTAT_MAC_TX_SWLANE,
+	return (i_dlstat_query_stats(dh, linkname, DLSTAT_MAC_TX_SWLANE,
 	    default_idlist, default_idlist_size,
 	    i_dlstat_tx_swlane_retrieve_stat));
 }
@@ -1473,13 +1471,13 @@
 	}
 
 	if (is_legacy_driver) {
-		head = i_dlstat_legacy_tx_lane_stats(linkname);
+		head = i_dlstat_legacy_tx_lane_stats(dh, linkname);
 		goto done;
 	}
 
-	bcast_stats = i_dlstat_tx_bcast_stats(linkname);
-	defunctlane_stats = i_dlstat_tx_defunctlane_stats(linkname);
-	lane_stats = i_dlstat_tx_hwlane_stats(linkname);
+	bcast_stats = i_dlstat_tx_bcast_stats(dh, linkname);
+	defunctlane_stats = i_dlstat_tx_defunctlane_stats(dh, linkname);
+	lane_stats = i_dlstat_tx_hwlane_stats(dh, linkname);
 	if (lane_stats == NULL)
 		lane_stats = i_dlstat_tx_swlane_stats(dh, linkid, linkname);
 
@@ -1652,7 +1650,7 @@
 		return (NULL);
 	}
 
-	i_dlstat_get_idlist(linkname, DLSTAT_FANOUT_IDLIST,
+	i_dlstat_get_idlist(dh, linkname, DLSTAT_FANOUT_IDLIST,
 	    fanout_idlist, &fanout_idlist_size);
 
 	for (i = 0; i < idlist_size; i++) {
@@ -1661,7 +1659,7 @@
 		(void) snprintf(statprefix, sizeof (statprefix), "%s%d_fanout",
 		    prefix, index);
 
-		curr_head = i_dlstat_query_stats(modname, statprefix,
+		curr_head = i_dlstat_query_stats(dh, modname, statprefix,
 		    fanout_idlist, fanout_idlist_size,
 		    i_dlstat_fanout_retrieve_stat);
 
@@ -1707,7 +1705,7 @@
 	uint_t	rx_hwlane_idlist[MAX_RINGS_PER_GROUP];
 	uint_t	rx_hwlane_idlist_size;
 
-	i_dlstat_get_idlist(linkname, DLSTAT_RX_HWLANE_IDLIST,
+	i_dlstat_get_idlist(dh, linkname, DLSTAT_RX_HWLANE_IDLIST,
 	    rx_hwlane_idlist, &rx_hwlane_idlist_size);
 
 	return (i_dlstat_query_fanout_stats(dh, linkid, rx_hwlane_idlist,
@@ -1833,10 +1831,10 @@
 	} else
 		modname = linkname;
 
-	i_dlstat_get_idlist(modname, DLSTAT_RX_RING_IDLIST,
+	i_dlstat_get_idlist(dh, modname, DLSTAT_RX_RING_IDLIST,
 	    rx_ring_idlist, &rx_ring_idlist_size);
 
-	return (i_dlstat_query_stats(modname, DLSTAT_MAC_RX_RING,
+	return (i_dlstat_query_stats(dh, modname, DLSTAT_MAC_RX_RING,
 	    rx_ring_idlist, rx_ring_idlist_size,
 	    i_dlstat_rx_ring_retrieve_stat));
 }
@@ -1920,10 +1918,10 @@
 	} else
 		modname = linkname;
 
-	i_dlstat_get_idlist(modname, DLSTAT_TX_RING_IDLIST,
+	i_dlstat_get_idlist(dh, modname, DLSTAT_TX_RING_IDLIST,
 	    tx_ring_idlist, &tx_ring_idlist_size);
 
-	return (i_dlstat_query_stats(modname, DLSTAT_MAC_TX_RING,
+	return (i_dlstat_query_stats(dh, modname, DLSTAT_MAC_TX_RING,
 	    tx_ring_idlist, tx_ring_idlist_size,
 	    i_dlstat_tx_ring_retrieve_stat));
 }
@@ -2170,9 +2168,9 @@
  * the corresponding device driver.
  */
 static aggr_port_stat_entry_t *
-i_dlstat_single_port_stats(const char *portname, datalink_id_t linkid)
+i_dlstat_single_port_stats(dladm_handle_t handle, const char *portname,
+    datalink_id_t linkid)
 {
-	kstat_ctl_t		*kcp;
 	kstat_t			*ksp;
 	char			module[DLPI_LINKNAME_MAX];
 	uint_t			instance;
@@ -2181,12 +2179,13 @@
 	if (dladm_parselink(portname, module, &instance) != DLADM_STATUS_OK)
 		goto done;
 
-	if ((kcp = kstat_open()) == NULL) {
+	if (dladm_dld_kcp(handle) == NULL) {
 		warn("kstat open operation failed");
 		return (NULL);
 	}
 
-	ksp = dladm_kstat_lookup(kcp, module, instance, "mac", NULL);
+	ksp = dladm_kstat_lookup(dladm_dld_kcp(handle), module, instance,
+	"mac", NULL);
 	if (ksp == NULL)
 		goto done;
 
@@ -2197,10 +2196,10 @@
 	/* Save port's linkid */
 	aggr_port_stat_entry->ape_portlinkid = linkid;
 
-	i_dlstat_get_stats(kcp, ksp, &aggr_port_stat_entry->ape_stats,
+	i_dlstat_get_stats(dladm_dld_kcp(handle), ksp,
+	    &aggr_port_stat_entry->ape_stats,
 	    aggr_port_stats_list, AGGR_PORT_STAT_SIZE);
 done:
-	(void) kstat_close(kcp);
 	return (aggr_port_stat_entry);
 }
 
@@ -2228,8 +2227,8 @@
 			goto done;
 		}
 
-		aggr_port_stat_entry = i_dlstat_single_port_stats(dpa.dp_dev,
-		    portp->lp_linkid);
+		aggr_port_stat_entry = i_dlstat_single_port_stats(dh,
+		    dpa.dp_dev, portp->lp_linkid);
 
 		/* Create dladm_stat_chain_t object for this stat */
 		curr = malloc(sizeof (dladm_stat_chain_t));
@@ -2279,7 +2278,7 @@
 		goto done;
 	}
 
-	misc_stat_entry = i_dlstat_misc_stats(linkname);
+	misc_stat_entry = i_dlstat_misc_stats(dh, linkname);
 	if (misc_stat_entry == NULL)
 		goto done;
 
@@ -2513,13 +2512,12 @@
 
 /* flow stats specific routines */
 flow_stat_t *
-dladm_flow_stat_query(const char *flowname)
+dladm_flow_stat_query(dladm_handle_t handle, const char *flowname)
 {
-	kstat_ctl_t	*kcp;
 	kstat_t		*ksp;
 	flow_stat_t	*flow_stat = NULL;
 
-	if ((kcp = kstat_open()) == NULL)
+	if (dladm_dld_kcp(handle) == NULL)
 		return (NULL);
 
 	flow_stat = calloc(1, sizeof (flow_stat_t));
@@ -2526,15 +2524,15 @@
 	if (flow_stat == NULL)
 		goto done;
 
-	ksp = dladm_kstat_lookup(kcp, NULL, -1, flowname, "flow");
+	ksp = dladm_kstat_lookup(dladm_dld_kcp(handle), NULL, -1, flowname,
+	"flow");
 
 	if (ksp != NULL) {
-		i_dlstat_get_stats(kcp, ksp, flow_stat, flow_stats_list,
-		    FLOW_STAT_SIZE);
+		i_dlstat_get_stats(dladm_dld_kcp(handle), ksp, flow_stat,
+		    flow_stats_list, FLOW_STAT_SIZE);
 	}
 
 done:
-	(void) kstat_close(kcp);
 	return (flow_stat);
 }
 
@@ -2565,13 +2563,13 @@
 
 /* Query all flow stats */
 name_value_stat_entry_t *
-dladm_flow_stat_query_all(const char *flowname)
+dladm_flow_stat_query_all(dladm_handle_t handle, const char *flowname)
 {
 	flow_stat_t		*flow_stat;
 	name_value_stat_entry_t	*name_value_stat_entry = NULL;
 
 	/* Query flow stats */
-	flow_stat =  dladm_flow_stat_query(flowname);
+	flow_stat =  dladm_flow_stat_query(handle, flowname);
 	if (flow_stat == NULL)
 		goto done;
 
