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>

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/libdladm/common/libdlstat.c
          +++ new/usr/src/lib/libdladm/common/libdlstat.c
↓ open down ↓ 15 lines elided ↑ open up ↑
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   */
  25   25  
       26 +/*
       27 + * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
       28 + */
       29 +
  26   30  #include <stddef.h>
  27   31  #include <stdio.h>
  28   32  #include <stdlib.h>
  29   33  #include <strings.h>
  30   34  #include <err.h>
  31   35  #include <errno.h>
  32   36  #include <fcntl.h>
  33   37  #include <kstat.h>
  34   38  #include <limits.h>
  35   39  #include <unistd.h>
↓ open down ↓ 126 lines elided ↑ open up ↑
 162  166                  return (-1);
 163  167          }
 164  168  
 165  169          return (0);
 166  170  }
 167  171  
 168  172  dladm_status_t
 169  173  dladm_get_single_mac_stat(dladm_handle_t handle, datalink_id_t linkid,
 170  174      const char *name, uint8_t type, void *val)
 171  175  {
 172      -        kstat_ctl_t     *kcp;
 173  176          char            module[DLPI_LINKNAME_MAX];
 174  177          uint_t          instance;
 175  178          char            link[DLPI_LINKNAME_MAX];
 176  179          dladm_status_t  status;
 177  180          uint32_t        flags, media;
 178  181          kstat_t         *ksp;
 179  182          dladm_phys_attr_t dpap;
 180  183  
 181  184          if ((status = dladm_datalink_id2info(handle, linkid, &flags, NULL,
 182  185              &media, link, DLPI_LINKNAME_MAX)) != DLADM_STATUS_OK)
↓ open down ↓ 5 lines elided ↑ open up ↑
 188  191          status = dladm_phys_info(handle, linkid, &dpap, DLADM_OPT_PERSIST);
 189  192  
 190  193          if (status != DLADM_STATUS_OK)
 191  194                  return (status);
 192  195  
 193  196          status = dladm_parselink(dpap.dp_dev, module, &instance);
 194  197  
 195  198          if (status != DLADM_STATUS_OK)
 196  199                  return (status);
 197  200  
 198      -        if ((kcp = kstat_open()) == NULL) {
 199      -                warn("kstat_open operation failed");
 200      -                return (-1);
 201      -        }
 202      -
 203  201          /*
 204  202           * The kstat query could fail if the underlying MAC
 205  203           * driver was already detached.
 206  204           */
 207      -        if ((ksp = kstat_lookup(kcp, module, instance, "mac")) == NULL &&
 208      -            (ksp = kstat_lookup(kcp, module, instance, NULL)) == NULL)
      205 +        if (dladm_dld_kcp(handle) == NULL) {
      206 +                warn("kstat_open operation failed");
      207 +                return (-1);
      208 +        }
      209 +
      210 +        if ((ksp = kstat_lookup(dladm_dld_kcp(handle), module, instance,
      211 +            "mac")) == NULL &&
      212 +            (ksp = kstat_lookup(dladm_dld_kcp(handle), module, instance,
      213 +            NULL)) == NULL)
 209  214                  goto bail;
 210  215  
 211      -        if (kstat_read(kcp, ksp, NULL) == -1)
      216 +        if (kstat_read(dladm_dld_kcp(handle), ksp, NULL) == -1)
 212  217                  goto bail;
 213  218  
 214  219          if (dladm_kstat_value(ksp, name, type, val) < 0)
 215  220                  goto bail;
 216  221  
 217      -        (void) kstat_close(kcp);
 218  222          return (DLADM_STATUS_OK);
 219  223  
 220  224  bail:
 221      -        (void) kstat_close(kcp);
 222  225          return (dladm_errno2status(errno));
 223  226  }
 224  227  
 225  228  /* Compute sum of 2 pktsums (s1 = s2 + s3) */
 226  229  void
 227  230  dladm_stats_total(pktsum_t *s1, pktsum_t *s2, pktsum_t *s3)
 228  231  {
 229  232          s1->rbytes    = s2->rbytes    + s3->rbytes;
 230  233          s1->ipackets  = s2->ipackets  + s3->ipackets;
 231  234          s1->ierrors   = s2->ierrors   + s3->ierrors;
↓ open down ↓ 479 lines elided ↑ open up ↑
 711  714          for (j = 1; j < size; j++) {
 712  715                  int key = idlist[j];
 713  716                  for (i = j - 1; (i >= 0) && (idlist[i] > key); i--)
 714  717                          idlist[i + 1] = idlist[i];
 715  718                  idlist[i + 1] = key;
 716  719          }
 717  720  }
 718  721  
 719  722  /* Support for legacy drivers */
 720  723  void
 721      -i_query_legacy_stats(const char *linkname, pktsum_t *stats)
      724 +i_query_legacy_stats(dladm_handle_t dh, const char *linkname, pktsum_t *stats)
 722  725  {
 723      -        kstat_ctl_t     *kcp;
 724  726          kstat_t         *ksp;
 725  727  
 726  728          bzero(stats, sizeof (*stats));
 727  729  
 728      -        if ((kcp = kstat_open()) == NULL)
      730 +        if (dladm_dld_kcp(dh) == NULL)
 729  731                  return;
 730  732  
 731      -        ksp = dladm_kstat_lookup(kcp, "link", 0, linkname, NULL);
      733 +        ksp = dladm_kstat_lookup(dladm_dld_kcp(dh), "link", 0, linkname, NULL);
 732  734  
 733  735          if (ksp != NULL)
 734      -                dladm_get_stats(kcp, ksp, stats);
 735      -
 736      -        (void) kstat_close(kcp);
      736 +                dladm_get_stats(dladm_dld_kcp(dh), ksp, stats);
 737  737  }
 738  738  
 739  739  void *
 740      -i_dlstat_legacy_rx_lane_stats(const char *linkname)
      740 +i_dlstat_legacy_rx_lane_stats(dladm_handle_t dh, const char *linkname)
 741  741  {
 742  742          dladm_stat_chain_t      *head = NULL;
 743  743          pktsum_t                stats;
 744  744          rx_lane_stat_entry_t    *rx_lane_stat_entry;
 745  745  
 746  746          bzero(&stats, sizeof (pktsum_t));
 747  747  
 748  748          /* Query for dls stats */
 749      -        i_query_legacy_stats(linkname, &stats);
      749 +        i_query_legacy_stats(dh, linkname, &stats);
 750  750  
 751  751          /* Convert to desired data type */
 752  752          rx_lane_stat_entry = calloc(1, sizeof (rx_lane_stat_entry_t));
 753  753          if (rx_lane_stat_entry == NULL)
 754  754                  goto done;
 755  755  
 756  756          rx_lane_stat_entry->rle_index = DLSTAT_INVALID_ENTRY;
 757  757          rx_lane_stat_entry->rle_id = L_SWLANE;
 758  758  
 759  759          rx_lane_stat_entry->rle_stats.rl_ipackets = stats.ipackets;
↓ open down ↓ 7 lines elided ↑ open up ↑
 767  767                  goto done;
 768  768          }
 769  769  
 770  770          head->dc_statentry = rx_lane_stat_entry;
 771  771          head->dc_next = NULL;
 772  772  done:
 773  773          return (head);
 774  774  }
 775  775  
 776  776  void *
 777      -i_dlstat_legacy_tx_lane_stats(const char *linkname)
      777 +i_dlstat_legacy_tx_lane_stats(dladm_handle_t dh, const char *linkname)
 778  778  {
 779  779          dladm_stat_chain_t      *head = NULL;
 780  780          pktsum_t                stats;
 781  781          tx_lane_stat_entry_t    *tx_lane_stat_entry;
 782  782  
 783  783          bzero(&stats, sizeof (pktsum_t));
 784  784  
 785  785          /* Query for dls stats */
 786      -        i_query_legacy_stats(linkname, &stats);
      786 +        i_query_legacy_stats(dh, linkname, &stats);
 787  787  
 788  788          /* Convert to desired data type */
 789  789          tx_lane_stat_entry = calloc(1, sizeof (tx_lane_stat_entry_t));
 790  790          if (tx_lane_stat_entry == NULL)
 791  791                  goto done;
 792  792  
 793  793          tx_lane_stat_entry->tle_index = DLSTAT_INVALID_ENTRY;
 794  794          tx_lane_stat_entry->tle_id = L_SWLANE;
 795  795  
 796  796          tx_lane_stat_entry->tle_stats.tl_opackets = stats.opackets;
↓ open down ↓ 88 lines elided ↑ open up ↑
 885  885      i_dlstat_tx_ring_search},
 886  886  { DLSTAT_RX_HWLANE_IDLIST,      DLSTAT_MAC_RX_HWLANE,
 887  887      i_dlstat_rx_hwlane_search},
 888  888  { DLSTAT_TX_HWLANE_IDLIST,      DLSTAT_MAC_TX_HWLANE,
 889  889      i_dlstat_tx_hwlane_search},
 890  890  { DLSTAT_FANOUT_IDLIST,         DLSTAT_MAC_FANOUT,
 891  891      i_dlstat_fanout_search}
 892  892  };
 893  893  
 894  894  static void
 895      -i_dlstat_get_idlist(const char *modname, dlstat_idlist_type_t idlist_type,
      895 +i_dlstat_get_idlist(dladm_handle_t handle, const char *modname,
      896 +    dlstat_idlist_type_t idlist_type,
 896  897      uint_t idlist[], uint_t *size)
 897  898  {
 898      -        kstat_ctl_t     *kcp;
      899 +        kstat_ctl_t     *kcp = dladm_dld_kcp(handle);
 899  900          kstat_t         *ksp;
 900  901          char            *prefix;
 901  902          int             prefixlen;
 902  903          boolean_t       (*fptr_searchkstat)(kstat_t *);
 903  904  
 904  905          *size = 0;
 905  906  
 906      -        if ((kcp = kstat_open()) == NULL) {
      907 +        if (kcp == NULL) {
 907  908                  warn("kstat_open operation failed");
 908      -                goto done;
      909 +                return;
 909  910          }
 910  911  
 911  912          prefix = dladm_extract_idlist[idlist_type].di_prefix;
 912  913          fptr_searchkstat = dladm_extract_idlist[idlist_type].di_searchkstat;
 913  914          prefixlen = strlen(prefix);
 914  915          for (ksp = kcp->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
 915  916                  if ((strcmp(ksp->ks_module, modname) == 0) &&
 916  917                      fptr_searchkstat(ksp)) {
 917  918                          idlist[(*size)++] = atoi(&ksp->ks_name[prefixlen]);
 918  919                  }
 919  920          }
 920  921          dladm_sort_index_list(idlist, *size);
 921      -
 922      -done:
 923      -        (void) kstat_close(kcp);
 924  922  }
 925  923  
 926  924  static dladm_stat_chain_t *
 927      -i_dlstat_query_stats(const char *modname, const char *prefix,
 928      -    uint_t idlist[], uint_t idlist_size,
      925 +i_dlstat_query_stats(dladm_handle_t handle, const char *modname,
      926 +    const char *prefix, uint_t idlist[], uint_t idlist_size,
 929  927      void * (*fn)(kstat_ctl_t *, kstat_t *, int))
 930  928  {
 931      -        kstat_ctl_t             *kcp;
 932  929          kstat_t                 *ksp;
 933  930          char                    statname[MAXLINKNAMELEN];
 934  931          int                     i = 0;
 935  932          dladm_stat_chain_t      *head = NULL, *prev = NULL;
 936  933          dladm_stat_chain_t      *curr;
 937  934  
 938      -        if ((kcp = kstat_open()) == NULL) {
      935 +        if (dladm_dld_kcp(handle) == NULL) {
 939  936                  warn("kstat_open operation failed");
 940  937                  return (NULL);
 941  938          }
 942  939  
 943  940          for (i = 0; i < idlist_size; i++) {
 944  941                  uint_t  index = idlist[i];
 945  942  
 946  943                  (void) snprintf(statname, sizeof (statname), "%s%d", prefix,
 947  944                      index);
 948  945  
 949      -                ksp = dladm_kstat_lookup(kcp, modname, 0, statname, NULL);
      946 +                ksp = dladm_kstat_lookup(dladm_dld_kcp(handle), modname, 0,
      947 +                    statname, NULL);
 950  948                  if (ksp == NULL)
 951  949                          continue;
 952  950  
 953  951                  curr = malloc(sizeof (dladm_stat_chain_t));
 954  952                  if (curr == NULL)
 955  953                          break;
 956  954  
 957      -                curr->dc_statentry = fn(kcp, ksp, index);
      955 +                curr->dc_statentry = fn(dladm_dld_kcp(handle), ksp, index);
 958  956                  if (curr->dc_statentry == NULL) {
 959  957                          free(curr);
 960  958                          break;
 961  959                  }
 962  960  
 963  961                  (void) strlcpy(curr->dc_statheader, statname,
 964  962                      sizeof (curr->dc_statheader));
 965  963                  curr->dc_next = NULL;
 966  964  
 967  965                  if (head == NULL)       /* First node */
 968  966                          head = curr;
 969  967                  else
 970  968                          prev->dc_next = curr;
 971  969  
 972  970                  prev = curr;
 973  971          }
 974  972  done:
 975      -        (void) kstat_close(kcp);
 976  973          return (head);
 977  974  }
 978  975  
 979  976  static misc_stat_entry_t *
 980      -i_dlstat_misc_stats(const char *linkname)
      977 +i_dlstat_misc_stats(dladm_handle_t handle, const char *linkname)
 981  978  {
 982      -        kstat_ctl_t             *kcp;
 983  979          kstat_t                 *ksp;
 984  980          misc_stat_entry_t       *misc_stat_entry = NULL;
 985  981  
 986      -        if ((kcp = kstat_open()) == NULL)
      982 +        if (dladm_dld_kcp(handle) == NULL)
 987  983                  return (NULL);
 988  984  
 989      -        ksp = dladm_kstat_lookup(kcp, linkname, 0, DLSTAT_MAC_MISC_STAT, NULL);
      985 +        ksp = dladm_kstat_lookup(dladm_dld_kcp(handle), linkname, 0,
      986 +            DLSTAT_MAC_MISC_STAT, NULL);
 990  987          if (ksp == NULL)
 991  988                  goto done;
 992  989  
 993  990          misc_stat_entry = calloc(1, sizeof (misc_stat_entry_t));
 994  991          if (misc_stat_entry == NULL)
 995  992                  goto done;
 996  993  
 997      -        i_dlstat_get_stats(kcp, ksp, &misc_stat_entry->mse_stats,
      994 +        i_dlstat_get_stats(dladm_dld_kcp(handle), ksp,
      995 +            &misc_stat_entry->mse_stats,
 998  996              misc_stats_list, MISC_STAT_SIZE);
 999  997  done:
1000      -        (void) kstat_close(kcp);
1001  998          return (misc_stat_entry);
1002  999  }
1003 1000  
1004 1001  /* Rx lane statistic specific functions */
1005 1002  static boolean_t
1006 1003  i_dlstat_rx_lane_match(void *arg1, void *arg2)
1007 1004  {
1008 1005          rx_lane_stat_entry_t *s1 = arg1;
1009 1006          rx_lane_stat_entry_t *s2 = arg2;
1010 1007  
↓ open down ↓ 90 lines elided ↑ open up ↑
1101 1098              rx_lane_stat_entry->rle_stats.rl_lclpackets;
1102 1099          local_stat_entry->rle_stats.rl_rbytes =
1103 1100              rx_lane_stat_entry->rle_stats.rl_lclbytes;
1104 1101  
1105 1102  done:
1106 1103          free(rx_lane_stat_entry);
1107 1104          return (local_stat_entry);
1108 1105  }
1109 1106  
1110 1107  static dladm_stat_chain_t *
1111      -i_dlstat_rx_local_stats(const char *linkname)
     1108 +i_dlstat_rx_local_stats(dladm_handle_t handle, const char *linkname)
1112 1109  {
1113 1110          dladm_stat_chain_t      *local_stats = NULL;
1114 1111  
1115      -        local_stats = i_dlstat_query_stats(linkname, DLSTAT_MAC_RX_SWLANE,
     1112 +        local_stats = i_dlstat_query_stats(handle, linkname,
     1113 +            DLSTAT_MAC_RX_SWLANE,
1116 1114              default_idlist, default_idlist_size,
1117 1115              i_dlstat_rx_local_retrieve_stat);
1118 1116  
1119 1117          if (local_stats != NULL) {
1120 1118                  (void) strlcpy(local_stats->dc_statheader, "mac_rx_local",
1121 1119                      sizeof (local_stats->dc_statheader));
1122 1120          }
1123 1121          return (local_stats);
1124 1122  }
1125 1123  
1126 1124  static dladm_stat_chain_t *
1127      -i_dlstat_rx_bcast_stats(const char *linkname)
     1125 +i_dlstat_rx_bcast_stats(dladm_handle_t handle, const char *linkname)
1128 1126  {
1129 1127          misc_stat_entry_t       *misc_stat_entry;
1130 1128          dladm_stat_chain_t      *head = NULL;
1131 1129          rx_lane_stat_entry_t    *rx_lane_stat_entry;
1132 1130  
1133      -        misc_stat_entry = i_dlstat_misc_stats(linkname);
     1131 +        misc_stat_entry = i_dlstat_misc_stats(handle, linkname);
1134 1132          if (misc_stat_entry == NULL)
1135 1133                  goto done;
1136 1134  
1137 1135          rx_lane_stat_entry = calloc(1, sizeof (rx_lane_stat_entry_t));
1138 1136          if (rx_lane_stat_entry == NULL)
1139 1137                  goto done;
1140 1138  
1141 1139          rx_lane_stat_entry->rle_index = DLSTAT_INVALID_ENTRY;
1142 1140          rx_lane_stat_entry->rle_id = L_BCAST;
1143 1141  
↓ open down ↓ 15 lines elided ↑ open up ↑
1159 1157  
1160 1158          head->dc_statentry = rx_lane_stat_entry;
1161 1159          head->dc_next = NULL;
1162 1160  
1163 1161          free(misc_stat_entry);
1164 1162  done:
1165 1163          return (head);
1166 1164  }
1167 1165  
1168 1166  static dladm_stat_chain_t *
1169      -i_dlstat_rx_defunctlane_stats(const char *linkname)
     1167 +i_dlstat_rx_defunctlane_stats(dladm_handle_t handle, const char *linkname)
1170 1168  {
1171 1169          misc_stat_entry_t       *misc_stat_entry;
1172 1170          dladm_stat_chain_t      *head = NULL;
1173 1171          rx_lane_stat_entry_t    *rx_lane_stat_entry;
1174 1172  
1175      -        misc_stat_entry = i_dlstat_misc_stats(linkname);
     1173 +        misc_stat_entry = i_dlstat_misc_stats(handle, linkname);
1176 1174          if (misc_stat_entry == NULL)
1177 1175                  goto done;
1178 1176  
1179 1177          rx_lane_stat_entry = calloc(1, sizeof (rx_lane_stat_entry_t));
1180 1178          if (rx_lane_stat_entry == NULL)
1181 1179                  goto done;
1182 1180  
1183 1181          rx_lane_stat_entry->rle_index = DLSTAT_INVALID_ENTRY;
1184 1182          rx_lane_stat_entry->rle_id = L_DFNCT;
1185 1183  
↓ open down ↓ 21 lines elided ↑ open up ↑
1207 1205          }
1208 1206  
1209 1207          head->dc_statentry = rx_lane_stat_entry;
1210 1208          head->dc_next = NULL;
1211 1209  
1212 1210  done:
1213 1211          return (head);
1214 1212  }
1215 1213  
1216 1214  static dladm_stat_chain_t *
1217      -i_dlstat_rx_hwlane_stats(const char *linkname)
     1215 +i_dlstat_rx_hwlane_stats(dladm_handle_t handle, const char *linkname)
1218 1216  {
1219 1217          uint_t  rx_hwlane_idlist[MAX_RINGS_PER_GROUP];
1220 1218          uint_t  rx_hwlane_idlist_size;
1221 1219  
1222      -        i_dlstat_get_idlist(linkname, DLSTAT_RX_HWLANE_IDLIST,
     1220 +        i_dlstat_get_idlist(handle, linkname, DLSTAT_RX_HWLANE_IDLIST,
1223 1221              rx_hwlane_idlist, &rx_hwlane_idlist_size);
1224 1222  
1225      -        return (i_dlstat_query_stats(linkname, DLSTAT_MAC_RX_HWLANE,
     1223 +        return (i_dlstat_query_stats(handle, linkname, DLSTAT_MAC_RX_HWLANE,
1226 1224              rx_hwlane_idlist, rx_hwlane_idlist_size,
1227 1225              i_dlstat_rx_hwlane_retrieve_stat));
1228 1226  }
1229 1227  
1230 1228  /*ARGSUSED*/
1231 1229  static dladm_stat_chain_t *
1232 1230  i_dlstat_rx_swlane_stats(dladm_handle_t dh, datalink_id_t linkid,
1233 1231      const char *linkname)
1234 1232  {
1235      -        return (i_dlstat_query_stats(linkname, DLSTAT_MAC_RX_SWLANE,
     1233 +        return (i_dlstat_query_stats(dh, linkname, DLSTAT_MAC_RX_SWLANE,
1236 1234              default_idlist, default_idlist_size,
1237 1235              i_dlstat_rx_swlane_retrieve_stat));
1238 1236  }
1239 1237  
1240 1238  void *
1241 1239  dlstat_rx_lane_stats(dladm_handle_t dh, datalink_id_t linkid)
1242 1240  {
1243 1241          dladm_stat_chain_t      *head = NULL;
1244 1242          dladm_stat_chain_t      *local_stats = NULL;
1245 1243          dladm_stat_chain_t      *bcast_stats = NULL;
↓ open down ↓ 7 lines elided ↑ open up ↑
1253 1251                  goto done;
1254 1252          }
1255 1253  
1256 1254          /* Check if it is legacy driver */
1257 1255          if (dladm_linkprop_is_set(dh, linkid, DLADM_PROP_VAL_CURRENT,
1258 1256              "_softmac", &is_legacy_driver) != DLADM_STATUS_OK) {
1259 1257                  goto done;
1260 1258          }
1261 1259  
1262 1260          if (is_legacy_driver) {
1263      -                head = i_dlstat_legacy_rx_lane_stats(linkname);
     1261 +                head = i_dlstat_legacy_rx_lane_stats(dh, linkname);
1264 1262                  goto done;
1265 1263          }
1266 1264  
1267      -        local_stats = i_dlstat_rx_local_stats(linkname);
1268      -        bcast_stats = i_dlstat_rx_bcast_stats(linkname);
1269      -        defunctlane_stats = i_dlstat_rx_defunctlane_stats(linkname);
1270      -        lane_stats = i_dlstat_rx_hwlane_stats(linkname);
     1265 +        local_stats = i_dlstat_rx_local_stats(dh, linkname);
     1266 +        bcast_stats = i_dlstat_rx_bcast_stats(dh, linkname);
     1267 +        defunctlane_stats = i_dlstat_rx_defunctlane_stats(dh, linkname);
     1268 +        lane_stats = i_dlstat_rx_hwlane_stats(dh, linkname);
1271 1269          if (lane_stats == NULL)
1272 1270                  lane_stats = i_dlstat_rx_swlane_stats(dh, linkid, linkname);
1273 1271  
1274 1272          head = i_dlstat_join_lists(local_stats, bcast_stats);
1275 1273          head = i_dlstat_join_lists(head, defunctlane_stats);
1276 1274          head = i_dlstat_join_lists(head, lane_stats);
1277 1275  done:
1278 1276          return (head);
1279 1277  }
1280 1278  
↓ open down ↓ 62 lines elided ↑ open up ↑
1343 1341          tx_lane_stat_entry->tle_id = L_SWLANE;
1344 1342  
1345 1343          i_dlstat_get_stats(kcp, ksp, &tx_lane_stat_entry->tle_stats,
1346 1344              tx_lane_stats_list, TX_LANE_STAT_SIZE);
1347 1345  
1348 1346  done:
1349 1347          return (tx_lane_stat_entry);
1350 1348  }
1351 1349  
1352 1350  static dladm_stat_chain_t *
1353      -i_dlstat_tx_bcast_stats(const char *linkname)
     1351 +i_dlstat_tx_bcast_stats(dladm_handle_t handle, const char *linkname)
1354 1352  {
1355 1353          misc_stat_entry_t       *misc_stat_entry;
1356 1354          dladm_stat_chain_t      *head = NULL;
1357 1355          tx_lane_stat_entry_t    *tx_lane_stat_entry;
1358 1356  
1359      -        misc_stat_entry = i_dlstat_misc_stats(linkname);
     1357 +        misc_stat_entry = i_dlstat_misc_stats(handle, linkname);
1360 1358          if (misc_stat_entry == NULL)
1361 1359                  goto done;
1362 1360  
1363 1361          tx_lane_stat_entry = calloc(1, sizeof (tx_lane_stat_entry_t));
1364 1362          if (tx_lane_stat_entry == NULL)
1365 1363                  goto done;
1366 1364  
1367 1365          tx_lane_stat_entry->tle_index = DLSTAT_INVALID_ENTRY;
1368 1366          tx_lane_stat_entry->tle_id = L_BCAST;
1369 1367  
↓ open down ↓ 13 lines elided ↑ open up ↑
1383 1381  
1384 1382          head->dc_statentry = tx_lane_stat_entry;
1385 1383          head->dc_next = NULL;
1386 1384  
1387 1385          free(misc_stat_entry);
1388 1386  done:
1389 1387          return (head);
1390 1388  }
1391 1389  
1392 1390  static dladm_stat_chain_t *
1393      -i_dlstat_tx_defunctlane_stats(const char *linkname)
     1391 +i_dlstat_tx_defunctlane_stats(dladm_handle_t handle, const char *linkname)
1394 1392  {
1395 1393          misc_stat_entry_t       *misc_stat_entry;
1396 1394          dladm_stat_chain_t      *head = NULL;
1397 1395          tx_lane_stat_entry_t    *tx_lane_stat_entry;
1398 1396  
1399      -        misc_stat_entry = i_dlstat_misc_stats(linkname);
     1397 +        misc_stat_entry = i_dlstat_misc_stats(handle, linkname);
1400 1398          if (misc_stat_entry == NULL)
1401 1399                  goto done;
1402 1400  
1403 1401          tx_lane_stat_entry = calloc(1, sizeof (tx_lane_stat_entry_t));
1404 1402          if (tx_lane_stat_entry == NULL)
1405 1403                  goto done;
1406 1404  
1407 1405          tx_lane_stat_entry->tle_index = DLSTAT_INVALID_ENTRY;
1408 1406          tx_lane_stat_entry->tle_id = L_DFNCT;
1409 1407  
↓ open down ↓ 11 lines elided ↑ open up ↑
1421 1419          }
1422 1420  
1423 1421          head->dc_statentry = tx_lane_stat_entry;
1424 1422          head->dc_next = NULL;
1425 1423  
1426 1424  done:
1427 1425          return (head);
1428 1426  }
1429 1427  
1430 1428  static dladm_stat_chain_t *
1431      -i_dlstat_tx_hwlane_stats(const char *linkname)
     1429 +i_dlstat_tx_hwlane_stats(dladm_handle_t handle, const char *linkname)
1432 1430  {
1433 1431          uint_t  tx_hwlane_idlist[MAX_RINGS_PER_GROUP];
1434 1432          uint_t  tx_hwlane_idlist_size;
1435 1433  
1436      -        i_dlstat_get_idlist(linkname, DLSTAT_TX_HWLANE_IDLIST,
     1434 +        i_dlstat_get_idlist(handle, linkname, DLSTAT_TX_HWLANE_IDLIST,
1437 1435              tx_hwlane_idlist, &tx_hwlane_idlist_size);
1438 1436  
1439      -        return (i_dlstat_query_stats(linkname, DLSTAT_MAC_TX_HWLANE,
     1437 +        return (i_dlstat_query_stats(handle, linkname, DLSTAT_MAC_TX_HWLANE,
1440 1438              tx_hwlane_idlist, tx_hwlane_idlist_size,
1441 1439              i_dlstat_tx_hwlane_retrieve_stat));
1442 1440  }
1443 1441  
1444 1442  /*ARGSUSED*/
1445 1443  static dladm_stat_chain_t *
1446 1444  i_dlstat_tx_swlane_stats(dladm_handle_t dh, datalink_id_t linkid,
1447 1445      const char *linkname)
1448 1446  {
1449      -        return (i_dlstat_query_stats(linkname, DLSTAT_MAC_TX_SWLANE,
     1447 +        return (i_dlstat_query_stats(dh, linkname, DLSTAT_MAC_TX_SWLANE,
1450 1448              default_idlist, default_idlist_size,
1451 1449              i_dlstat_tx_swlane_retrieve_stat));
1452 1450  }
1453 1451  
1454 1452  void *
1455 1453  dlstat_tx_lane_stats(dladm_handle_t dh, datalink_id_t linkid)
1456 1454  {
1457 1455          dladm_stat_chain_t      *head = NULL;
1458 1456          dladm_stat_chain_t      *bcast_stats = NULL;
1459 1457          dladm_stat_chain_t      *defunctlane_stats = NULL;
↓ open down ↓ 6 lines elided ↑ open up ↑
1466 1464                  goto done;
1467 1465          }
1468 1466  
1469 1467          /* Check if it is legacy driver */
1470 1468          if (dladm_linkprop_is_set(dh, linkid, DLADM_PROP_VAL_CURRENT,
1471 1469              "_softmac", &is_legacy_driver) != DLADM_STATUS_OK) {
1472 1470                  goto done;
1473 1471          }
1474 1472  
1475 1473          if (is_legacy_driver) {
1476      -                head = i_dlstat_legacy_tx_lane_stats(linkname);
     1474 +                head = i_dlstat_legacy_tx_lane_stats(dh, linkname);
1477 1475                  goto done;
1478 1476          }
1479 1477  
1480      -        bcast_stats = i_dlstat_tx_bcast_stats(linkname);
1481      -        defunctlane_stats = i_dlstat_tx_defunctlane_stats(linkname);
1482      -        lane_stats = i_dlstat_tx_hwlane_stats(linkname);
     1478 +        bcast_stats = i_dlstat_tx_bcast_stats(dh, linkname);
     1479 +        defunctlane_stats = i_dlstat_tx_defunctlane_stats(dh, linkname);
     1480 +        lane_stats = i_dlstat_tx_hwlane_stats(dh, linkname);
1483 1481          if (lane_stats == NULL)
1484 1482                  lane_stats = i_dlstat_tx_swlane_stats(dh, linkid, linkname);
1485 1483  
1486 1484          head = i_dlstat_join_lists(bcast_stats, defunctlane_stats);
1487 1485          head = i_dlstat_join_lists(head, lane_stats);
1488 1486  
1489 1487  done:
1490 1488          return (head);
1491 1489  }
1492 1490  
↓ open down ↓ 152 lines elided ↑ open up ↑
1645 1643          dladm_stat_chain_t      *curr, *curr_head;
1646 1644          dladm_stat_chain_t      *head = NULL, *prev = NULL;
1647 1645          uint_t                  fanout_idlist[MAX_RINGS_PER_GROUP];
1648 1646          uint_t                  fanout_idlist_size;
1649 1647  
1650 1648          if (dladm_datalink_id2info(dh, linkid, NULL, NULL, NULL, linkname,
1651 1649              DLPI_LINKNAME_MAX) != DLADM_STATUS_OK) {
1652 1650                  return (NULL);
1653 1651          }
1654 1652  
1655      -        i_dlstat_get_idlist(linkname, DLSTAT_FANOUT_IDLIST,
     1653 +        i_dlstat_get_idlist(dh, linkname, DLSTAT_FANOUT_IDLIST,
1656 1654              fanout_idlist, &fanout_idlist_size);
1657 1655  
1658 1656          for (i = 0; i < idlist_size; i++) {
1659 1657                  uint_t  index = idlist[i];
1660 1658  
1661 1659                  (void) snprintf(statprefix, sizeof (statprefix), "%s%d_fanout",
1662 1660                      prefix, index);
1663 1661  
1664      -                curr_head = i_dlstat_query_stats(modname, statprefix,
     1662 +                curr_head = i_dlstat_query_stats(dh, modname, statprefix,
1665 1663                      fanout_idlist, fanout_idlist_size,
1666 1664                      i_dlstat_fanout_retrieve_stat);
1667 1665  
1668 1666                  if (curr_head == NULL)  /* Last lane */
1669 1667                          break;
1670 1668  
1671 1669                  if (head == NULL)       /* First lane */
1672 1670                          head = curr_head;
1673 1671                  else    /* Link new lane list to end of previous lane list */
1674 1672                          prev->dc_next = curr_head;
↓ open down ↓ 25 lines elided ↑ open up ↑
1700 1698              DLSTAT_MAC_RX_SWLANE));
1701 1699  }
1702 1700  
1703 1701  void *
1704 1702  dlstat_fanout_hwlane_stats(dladm_handle_t dh, datalink_id_t linkid,
1705 1703      const char *linkname)
1706 1704  {
1707 1705          uint_t  rx_hwlane_idlist[MAX_RINGS_PER_GROUP];
1708 1706          uint_t  rx_hwlane_idlist_size;
1709 1707  
1710      -        i_dlstat_get_idlist(linkname, DLSTAT_RX_HWLANE_IDLIST,
     1708 +        i_dlstat_get_idlist(dh, linkname, DLSTAT_RX_HWLANE_IDLIST,
1711 1709              rx_hwlane_idlist, &rx_hwlane_idlist_size);
1712 1710  
1713 1711          return (i_dlstat_query_fanout_stats(dh, linkid, rx_hwlane_idlist,
1714 1712              rx_hwlane_idlist_size, linkname, DLSTAT_MAC_RX_HWLANE));
1715 1713  }
1716 1714  
1717 1715  void *
1718 1716  dlstat_fanout_stats(dladm_handle_t dh, datalink_id_t linkid)
1719 1717  {
1720 1718          dladm_stat_chain_t      *head = NULL;
↓ open down ↓ 105 lines elided ↑ open up ↑
1826 1824  
1827 1825          if (class != DATALINK_CLASS_AGGR) {
1828 1826                  if (dladm_phys_info(dh, linkid, &dpa, DLADM_OPT_ACTIVE) !=
1829 1827                      DLADM_STATUS_OK) {
1830 1828                          return (NULL);
1831 1829                  }
1832 1830                  modname = dpa.dp_dev;
1833 1831          } else
1834 1832                  modname = linkname;
1835 1833  
1836      -        i_dlstat_get_idlist(modname, DLSTAT_RX_RING_IDLIST,
     1834 +        i_dlstat_get_idlist(dh, modname, DLSTAT_RX_RING_IDLIST,
1837 1835              rx_ring_idlist, &rx_ring_idlist_size);
1838 1836  
1839      -        return (i_dlstat_query_stats(modname, DLSTAT_MAC_RX_RING,
     1837 +        return (i_dlstat_query_stats(dh, modname, DLSTAT_MAC_RX_RING,
1840 1838              rx_ring_idlist, rx_ring_idlist_size,
1841 1839              i_dlstat_rx_ring_retrieve_stat));
1842 1840  }
1843 1841  
1844 1842  /* Tx ring statistic specific functions */
1845 1843  static boolean_t
1846 1844  i_dlstat_tx_ring_match(void *arg1, void *arg2)
1847 1845  {
1848 1846          tx_lane_stat_entry_t    *s1 = arg1;
1849 1847          tx_lane_stat_entry_t    *s2 = arg2;
↓ open down ↓ 63 lines elided ↑ open up ↑
1913 1911  
1914 1912          if (class != DATALINK_CLASS_AGGR) {
1915 1913                  if (dladm_phys_info(dh, linkid, &dpa, DLADM_OPT_ACTIVE) !=
1916 1914                      DLADM_STATUS_OK) {
1917 1915                          return (NULL);
1918 1916                  }
1919 1917                  modname = dpa.dp_dev;
1920 1918          } else
1921 1919                  modname = linkname;
1922 1920  
1923      -        i_dlstat_get_idlist(modname, DLSTAT_TX_RING_IDLIST,
     1921 +        i_dlstat_get_idlist(dh, modname, DLSTAT_TX_RING_IDLIST,
1924 1922              tx_ring_idlist, &tx_ring_idlist_size);
1925 1923  
1926      -        return (i_dlstat_query_stats(modname, DLSTAT_MAC_TX_RING,
     1924 +        return (i_dlstat_query_stats(dh, modname, DLSTAT_MAC_TX_RING,
1927 1925              tx_ring_idlist, tx_ring_idlist_size,
1928 1926              i_dlstat_tx_ring_retrieve_stat));
1929 1927  }
1930 1928  
1931 1929  /* Rx ring total statistic specific functions */
1932 1930  void *
1933 1931  dlstat_rx_ring_total_stats(dladm_handle_t dh, datalink_id_t linkid)
1934 1932  {
1935 1933          dladm_stat_chain_t      *total_head = NULL;
1936 1934          dladm_stat_chain_t      *rx_ring_head, *curr;
↓ open down ↓ 226 lines elided ↑ open up ↑
2163 2161  
2164 2162  done:
2165 2163          return (diff_entry);
2166 2164  }
2167 2165  
2168 2166  /*
2169 2167   * Query dls stats for the aggr port. This results in query for stats into
2170 2168   * the corresponding device driver.
2171 2169   */
2172 2170  static aggr_port_stat_entry_t *
2173      -i_dlstat_single_port_stats(const char *portname, datalink_id_t linkid)
     2171 +i_dlstat_single_port_stats(dladm_handle_t handle, const char *portname,
     2172 +    datalink_id_t linkid)
2174 2173  {
2175      -        kstat_ctl_t             *kcp;
2176 2174          kstat_t                 *ksp;
2177 2175          char                    module[DLPI_LINKNAME_MAX];
2178 2176          uint_t                  instance;
2179 2177          aggr_port_stat_entry_t  *aggr_port_stat_entry = NULL;
2180 2178  
2181 2179          if (dladm_parselink(portname, module, &instance) != DLADM_STATUS_OK)
2182 2180                  goto done;
2183 2181  
2184      -        if ((kcp = kstat_open()) == NULL) {
     2182 +        if (dladm_dld_kcp(handle) == NULL) {
2185 2183                  warn("kstat open operation failed");
2186 2184                  return (NULL);
2187 2185          }
2188 2186  
2189      -        ksp = dladm_kstat_lookup(kcp, module, instance, "mac", NULL);
     2187 +        ksp = dladm_kstat_lookup(dladm_dld_kcp(handle), module, instance,
     2188 +        "mac", NULL);
2190 2189          if (ksp == NULL)
2191 2190                  goto done;
2192 2191  
2193 2192          aggr_port_stat_entry = calloc(1, sizeof (aggr_port_stat_entry_t));
2194 2193          if (aggr_port_stat_entry == NULL)
2195 2194                  goto done;
2196 2195  
2197 2196          /* Save port's linkid */
2198 2197          aggr_port_stat_entry->ape_portlinkid = linkid;
2199 2198  
2200      -        i_dlstat_get_stats(kcp, ksp, &aggr_port_stat_entry->ape_stats,
     2199 +        i_dlstat_get_stats(dladm_dld_kcp(handle), ksp,
     2200 +            &aggr_port_stat_entry->ape_stats,
2201 2201              aggr_port_stats_list, AGGR_PORT_STAT_SIZE);
2202 2202  done:
2203      -        (void) kstat_close(kcp);
2204 2203          return (aggr_port_stat_entry);
2205 2204  }
2206 2205  
2207 2206  void *
2208 2207  dlstat_aggr_port_stats(dladm_handle_t dh, datalink_id_t linkid)
2209 2208  {
2210 2209          dladm_aggr_grp_attr_t   ginfo;
2211 2210          int                     i;
2212 2211          dladm_aggr_port_attr_t   *portp;
2213 2212          dladm_phys_attr_t       dpa;
↓ open down ↓ 7 lines elided ↑ open up ↑
2221 2220              != DLADM_STATUS_OK)
2222 2221                  goto done;
2223 2222          /* For every port that is member of this aggr do */
2224 2223          for (i = 0; i < ginfo.lg_nports; i++) {
2225 2224                  portp = &(ginfo.lg_ports[i]);
2226 2225                  if (dladm_phys_info(dh, portp->lp_linkid, &dpa,
2227 2226                      DLADM_OPT_ACTIVE) != DLADM_STATUS_OK) {
2228 2227                          goto done;
2229 2228                  }
2230 2229  
2231      -                aggr_port_stat_entry = i_dlstat_single_port_stats(dpa.dp_dev,
2232      -                    portp->lp_linkid);
     2230 +                aggr_port_stat_entry = i_dlstat_single_port_stats(dh,
     2231 +                    dpa.dp_dev, portp->lp_linkid);
2233 2232  
2234 2233                  /* Create dladm_stat_chain_t object for this stat */
2235 2234                  curr = malloc(sizeof (dladm_stat_chain_t));
2236 2235                  if (curr == NULL) {
2237 2236                          free(aggr_port_stat_entry);
2238 2237                          goto done;
2239 2238                  }
2240 2239                  (void) strlcpy(curr->dc_statheader, dpa.dp_dev,
2241 2240                      sizeof (curr->dc_statheader));
2242 2241                  curr->dc_statentry = aggr_port_stat_entry;
↓ open down ↓ 29 lines elided ↑ open up ↑
2272 2271  {
2273 2272          misc_stat_entry_t       *misc_stat_entry;
2274 2273          dladm_stat_chain_t      *head = NULL;
2275 2274          char                    linkname[MAXLINKNAMELEN];
2276 2275  
2277 2276          if (dladm_datalink_id2info(dh, linkid, NULL, NULL, NULL, linkname,
2278 2277              DLPI_LINKNAME_MAX) != DLADM_STATUS_OK) {
2279 2278                  goto done;
2280 2279          }
2281 2280  
2282      -        misc_stat_entry = i_dlstat_misc_stats(linkname);
     2281 +        misc_stat_entry = i_dlstat_misc_stats(dh, linkname);
2283 2282          if (misc_stat_entry == NULL)
2284 2283                  goto done;
2285 2284  
2286 2285          head = malloc(sizeof (dladm_stat_chain_t));
2287 2286          if (head == NULL) {
2288 2287                  free(misc_stat_entry);
2289 2288                  goto done;
2290 2289          }
2291 2290  
2292 2291          head->dc_statentry = misc_stat_entry;
↓ open down ↓ 213 lines elided ↑ open up ↑
2506 2505                  }
2507 2506  
2508 2507                  curr = curr->dc_next;
2509 2508                  free(nv_entry);
2510 2509                  free(tofree);
2511 2510          }
2512 2511  }
2513 2512  
2514 2513  /* flow stats specific routines */
2515 2514  flow_stat_t *
2516      -dladm_flow_stat_query(const char *flowname)
     2515 +dladm_flow_stat_query(dladm_handle_t handle, const char *flowname)
2517 2516  {
2518      -        kstat_ctl_t     *kcp;
2519 2517          kstat_t         *ksp;
2520 2518          flow_stat_t     *flow_stat = NULL;
2521 2519  
2522      -        if ((kcp = kstat_open()) == NULL)
     2520 +        if (dladm_dld_kcp(handle) == NULL)
2523 2521                  return (NULL);
2524 2522  
2525 2523          flow_stat = calloc(1, sizeof (flow_stat_t));
2526 2524          if (flow_stat == NULL)
2527 2525                  goto done;
2528 2526  
2529      -        ksp = dladm_kstat_lookup(kcp, NULL, -1, flowname, "flow");
     2527 +        ksp = dladm_kstat_lookup(dladm_dld_kcp(handle), NULL, -1, flowname,
     2528 +        "flow");
2530 2529  
2531 2530          if (ksp != NULL) {
2532      -                i_dlstat_get_stats(kcp, ksp, flow_stat, flow_stats_list,
2533      -                    FLOW_STAT_SIZE);
     2531 +                i_dlstat_get_stats(dladm_dld_kcp(handle), ksp, flow_stat,
     2532 +                    flow_stats_list, FLOW_STAT_SIZE);
2534 2533          }
2535 2534  
2536 2535  done:
2537      -        (void) kstat_close(kcp);
2538 2536          return (flow_stat);
2539 2537  }
2540 2538  
2541 2539  flow_stat_t *
2542 2540  dladm_flow_stat_diff(flow_stat_t *op1, flow_stat_t *op2)
2543 2541  {
2544 2542          flow_stat_t     *diff_stat;
2545 2543  
2546 2544          diff_stat = calloc(1, sizeof (flow_stat_t));
2547 2545          if (diff_stat == NULL)
↓ open down ↓ 10 lines elided ↑ open up ↑
2558 2556  }
2559 2557  
2560 2558  void
2561 2559  dladm_flow_stat_free(flow_stat_t *curr)
2562 2560  {
2563 2561          free(curr);
2564 2562  }
2565 2563  
2566 2564  /* Query all flow stats */
2567 2565  name_value_stat_entry_t *
2568      -dladm_flow_stat_query_all(const char *flowname)
     2566 +dladm_flow_stat_query_all(dladm_handle_t handle, const char *flowname)
2569 2567  {
2570 2568          flow_stat_t             *flow_stat;
2571 2569          name_value_stat_entry_t *name_value_stat_entry = NULL;
2572 2570  
2573 2571          /* Query flow stats */
2574      -        flow_stat =  dladm_flow_stat_query(flowname);
     2572 +        flow_stat =  dladm_flow_stat_query(handle, flowname);
2575 2573          if (flow_stat == NULL)
2576 2574                  goto done;
2577 2575  
2578 2576          /* Allocate memory for query all stat entry */
2579 2577          name_value_stat_entry = calloc(1, sizeof (name_value_stat_entry_t));
2580 2578          if (name_value_stat_entry == NULL) {
2581 2579                  dladm_flow_stat_free(flow_stat);
2582 2580                  goto done;
2583 2581          }
2584 2582  
↓ open down ↓ 27 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX