Print this page
10052 "dladm show-ether" should pick one kstat snapshot and stick with it
Reviewed by: Rob Johnston <rob.johnston@joyent.com>
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Gergo Doma <domag02@gmail.com>
Reviewed by: Andy Fiddaman <andy@omniosce.org>

@@ -21,10 +21,14 @@
 /*
  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
+/*
+ * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
+ */
+
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <strings.h>
 #include <err.h>

@@ -167,11 +171,10 @@
 
 dladm_status_t
 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];
         dladm_status_t  status;
         uint32_t        flags, media;

@@ -193,34 +196,34 @@
         status = dladm_parselink(dpap.dp_dev, module, &instance);
 
         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));
 }
 
 /* Compute sum of 2 pktsums (s1 = s2 + s3) */
 void

@@ -716,39 +719,36 @@
         }
 }
 
 /* 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;
         rx_lane_stat_entry_t    *rx_lane_stat_entry;
 
         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));
         if (rx_lane_stat_entry == NULL)
                 goto done;

@@ -772,20 +772,20 @@
 done:
         return (head);
 }
 
 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;
         tx_lane_stat_entry_t    *tx_lane_stat_entry;
 
         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));
         if (tx_lane_stat_entry == NULL)
                 goto done;

@@ -890,24 +890,25 @@
 { DLSTAT_FANOUT_IDLIST,         DLSTAT_MAC_FANOUT,
     i_dlstat_fanout_search}
 };
 
 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;
         boolean_t       (*fptr_searchkstat)(kstat_t *);
 
         *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;
         fptr_searchkstat = dladm_extract_idlist[idlist_type].di_searchkstat;
         prefixlen = strlen(prefix);

@@ -916,28 +917,24 @@
                     fptr_searchkstat(ksp)) {
                         idlist[(*size)++] = atoi(&ksp->ks_name[prefixlen]);
                 }
         }
         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;
         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);
         }
 
         for (i = 0; i < idlist_size; i++) {

@@ -944,19 +941,20 @@
                 uint_t  index = idlist[i];
 
                 (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;
 
                 curr = malloc(sizeof (dladm_stat_chain_t));
                 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;
                 }
 

@@ -970,36 +968,35 @@
                         prev->dc_next = curr;
 
                 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;
 
         misc_stat_entry = calloc(1, sizeof (misc_stat_entry_t));
         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);
 }
 
 /* Rx lane statistic specific functions */
 static boolean_t

@@ -1106,15 +1103,16 @@
         free(rx_lane_stat_entry);
         return (local_stat_entry);
 }
 
 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);
 
         if (local_stats != NULL) {
                 (void) strlcpy(local_stats->dc_statheader, "mac_rx_local",

@@ -1122,17 +1120,17 @@
         }
         return (local_stats);
 }
 
 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;
 
         rx_lane_stat_entry = calloc(1, sizeof (rx_lane_stat_entry_t));
         if (rx_lane_stat_entry == NULL)

@@ -1164,17 +1162,17 @@
 done:
         return (head);
 }
 
 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;
 
         rx_lane_stat_entry = calloc(1, sizeof (rx_lane_stat_entry_t));
         if (rx_lane_stat_entry == NULL)

@@ -1212,29 +1210,29 @@
 done:
         return (head);
 }
 
 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));
 }
 
 /*ARGSUSED*/
 static dladm_stat_chain_t *
 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));
 }
 
 void *

@@ -1258,18 +1256,18 @@
             "_softmac", &is_legacy_driver) != DLADM_STATUS_OK) {
                 goto done;
         }
 
         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);
 
         head = i_dlstat_join_lists(local_stats, bcast_stats);
         head = i_dlstat_join_lists(head, defunctlane_stats);

@@ -1348,17 +1346,17 @@
 done:
         return (tx_lane_stat_entry);
 }
 
 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;
 
         tx_lane_stat_entry = calloc(1, sizeof (tx_lane_stat_entry_t));
         if (tx_lane_stat_entry == NULL)

@@ -1388,17 +1386,17 @@
 done:
         return (head);
 }
 
 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;
 
         tx_lane_stat_entry = calloc(1, sizeof (tx_lane_stat_entry_t));
         if (tx_lane_stat_entry == NULL)

@@ -1426,29 +1424,29 @@
 done:
         return (head);
 }
 
 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));
 }
 
 /*ARGSUSED*/
 static dladm_stat_chain_t *
 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));
 }
 
 void *

@@ -1471,17 +1469,17 @@
             "_softmac", &is_legacy_driver) != DLADM_STATUS_OK) {
                 goto done;
         }
 
         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);
 
         head = i_dlstat_join_lists(bcast_stats, defunctlane_stats);
         head = i_dlstat_join_lists(head, lane_stats);

@@ -1650,20 +1648,20 @@
         if (dladm_datalink_id2info(dh, linkid, NULL, NULL, NULL, linkname,
             DLPI_LINKNAME_MAX) != DLADM_STATUS_OK) {
                 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++) {
                 uint_t  index = idlist[i];
 
                 (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);
 
                 if (curr_head == NULL)  /* Last lane */
                         break;

@@ -1705,11 +1703,11 @@
     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(dh, linkname, DLSTAT_RX_HWLANE_IDLIST,
             rx_hwlane_idlist, &rx_hwlane_idlist_size);
 
         return (i_dlstat_query_fanout_stats(dh, linkid, rx_hwlane_idlist,
             rx_hwlane_idlist_size, linkname, DLSTAT_MAC_RX_HWLANE));
 }

@@ -1831,14 +1829,14 @@
                 }
                 modname = dpa.dp_dev;
         } 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));
 }
 
 /* Tx ring statistic specific functions */

@@ -1918,14 +1916,14 @@
                 }
                 modname = dpa.dp_dev;
         } 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));
 }
 
 /* Rx ring total statistic specific functions */

@@ -2168,27 +2166,28 @@
 /*
  * Query dls stats for the aggr port. This results in query for stats into
  * 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;
         aggr_port_stat_entry_t  *aggr_port_stat_entry = NULL;
 
         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;
 
         aggr_port_stat_entry = calloc(1, sizeof (aggr_port_stat_entry_t));
         if (aggr_port_stat_entry == NULL)

@@ -2195,14 +2194,14 @@
                 goto done;
 
         /* 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);
 }
 
 void *
 dlstat_aggr_port_stats(dladm_handle_t dh, datalink_id_t linkid)

@@ -2226,12 +2225,12 @@
                 if (dladm_phys_info(dh, portp->lp_linkid, &dpa,
                     DLADM_OPT_ACTIVE) != DLADM_STATUS_OK) {
                         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));
                 if (curr == NULL) {
                         free(aggr_port_stat_entry);

@@ -2277,11 +2276,11 @@
         if (dladm_datalink_id2info(dh, linkid, NULL, NULL, NULL, linkname,
             DLPI_LINKNAME_MAX) != DLADM_STATUS_OK) {
                 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;
 
         head = malloc(sizeof (dladm_stat_chain_t));
         if (head == NULL) {

@@ -2511,32 +2510,31 @@
         }
 }
 
 /* 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));
         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);
 }
 
 flow_stat_t *
 dladm_flow_stat_diff(flow_stat_t *op1, flow_stat_t *op2)

@@ -2563,17 +2561,17 @@
         free(curr);
 }
 
 /* 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;
 
         /* Allocate memory for query all stat entry */
         name_value_stat_entry = calloc(1, sizeof (name_value_stat_entry_t));