"Fossies" - the Fresh Open Source Software Archive

Member "open-fcoe-3.19/fcoe-utils/libopenfcoe.c" (15 Apr 2015, 6364 Bytes) of package /linux/misc/open-fcoe-3.19.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "libopenfcoe.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright(c) 2012-2013 Intel Corporation. All rights reserved.
    3  *
    4  * This program is free software; you can redistribute it and/or modify it
    5  * under the terms and conditions of the GNU General Public License,
    6  * version 2, as published by the Free Software Foundation.
    7  *
    8  * This program is distributed in the hope it will be useful, but WITHOUT
    9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   11  * more details.
   12  *
   13  * You should have received a copy of the GNU General Public License along with
   14  * this program; if not, write to the Free Software Foundation, Inc.,
   15  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
   16  *
   17  * Maintained at www.Open-FCoE.org
   18  */
   19 
   20 #include "libopenfcoe.h"
   21 
   22 #define SYSFS_HOST_DIR     "/sys/class/fc_host"
   23 #define SYSFS_HBA_DIR      "/sys/class/net"
   24 
   25 int mac2str(const u_int8_t *mac, char *dst, size_t size)
   26 {
   27     if (dst && size > MAC_ADDR_STRLEN) {
   28         snprintf(dst, size, "%02X:%02X:%02X:%02X:%02X:%02X",
   29              mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
   30         return 0;
   31     }
   32     return -1;
   33 }
   34 
   35 int str2mac(const char *src, u_int8_t *mac, size_t size)
   36 {
   37     int i = 0;
   38     int rc = -1;
   39 
   40     if (size < 6)
   41         goto out_err;
   42 
   43     if (!src)
   44         goto out_err;
   45 
   46     if (strlen(src) != MAC_ADDR_STRLEN)
   47         goto out_err;
   48 
   49     memset(mac, 0, size);
   50     for (i = 0; i < 6; i++, mac++)
   51         if (1 != sscanf(&src[i * 3], "%02hhX", mac))
   52             goto out_err;
   53     rc = 0;
   54 out_err:
   55     return rc;
   56 }
   57 
   58 static int add_fcoe_fcf_device(struct dirent *dp, void *arg)
   59 {
   60     struct fcoe_ctlr_device *ctlr = (struct fcoe_ctlr_device *)arg;
   61     struct fcoe_fcf_device *fcf;
   62 
   63     if (!strstr(dp->d_name, "fcf") ||
   64         (!strcmp(dp->d_name, "fcf_dev_loss_tmo")))
   65         return 0;
   66 
   67     fcf = malloc(sizeof(struct fcoe_fcf_device));
   68     if (!fcf)
   69         return -ENOMEM;
   70 
   71     memset(fcf, 0, sizeof(struct fcoe_fcf_device));
   72 
   73     /* Save the path */
   74     snprintf(fcf->path, sizeof(fcf->path),
   75          "%s/%s", ctlr->path, dp->d_name);
   76 
   77     /* Use the index from the logical enumeration */
   78     fcf->index = atoi(dp->d_name + sizeof("fcf_") - 1);
   79 
   80     /* Save the fcf in the fcport's table */
   81     if (sa_table_insert(&ctlr->fcfs, fcf->index,
   82                 fcf) < 0) {
   83         fprintf(stderr, "%s: insert of fcf %d failed\n",
   84             __func__, fcf->index);
   85         goto fail;
   86     }
   87 
   88     return 0;
   89 
   90 fail:
   91     free(fcf);
   92     return -ENOENT;
   93 }
   94 
   95 static void read_fcoe_fcf_device(void *ep, UNUSED void *arg)
   96 {
   97     struct fcoe_fcf_device *fcf = (struct fcoe_fcf_device *)ep;
   98     char buf[MAX_STR_LEN];
   99 
  100     sa_sys_read_line(fcf->path, "state", buf, sizeof(buf));
  101     sa_enum_encode(fcf_state_table, buf, &fcf->state);
  102     sa_sys_read_u32(fcf->path, "dev_loss_tmo", &fcf->dev_loss_tmo);
  103     sa_sys_read_u64(fcf->path, "fabric_name", &fcf->fabric_name);
  104     sa_sys_read_u64(fcf->path, "switch_name", &fcf->switch_name);
  105     sa_sys_read_u32(fcf->path, "fc_map", &fcf->fc_map);
  106     sa_sys_read_u32(fcf->path, "vfid", &fcf->vfid);
  107 
  108     sa_sys_read_line(fcf->path, "mac", buf, MAX_STR_LEN);
  109     str2mac(buf, &fcf->mac[0], MAC_ADDR_LEN);
  110 
  111     sa_sys_read_u32(fcf->path, "priority", &fcf->priority);
  112     sa_sys_read_u32(fcf->path, "fka_period", &fcf->fka_period);
  113     sa_sys_read_u32(fcf->path, "selected", &fcf->selected);
  114     sa_sys_read_u32(fcf->path, "vlan_id", &fcf->vlan_id);
  115 }
  116 
  117 static void read_fcoe_fcf(void *ep, UNUSED void *arg)
  118 {
  119     struct fcoe_ctlr_device *ctlr = (struct fcoe_ctlr_device *)ep;
  120 
  121     /* Iterate through the ctlr and add any fcfs */
  122     sa_dir_read(ctlr->path, add_fcoe_fcf_device, ctlr);
  123 
  124     /* Populate each fabric */
  125     sa_table_iterate(&ctlr->fcfs, read_fcoe_fcf_device, NULL);
  126 }
  127 
  128 static void free_fcoe_fcf_device(void *ep, UNUSED void *arg)
  129 {
  130     struct fcoe_fcf_device *fcf = (struct fcoe_fcf_device *)ep;
  131 
  132     free(fcf);
  133 }
  134 
  135 #define SYSFS_MOUNT "/sys"
  136 #define FCOE_CTLR_DEVICE_DIR SYSFS_MOUNT "/bus/fcoe/devices/"
  137 
  138 static int find_fchost(struct dirent *dp, void *arg)
  139 {
  140     char *fchost = arg;
  141 
  142     if (strstr(dp->d_name, "host")) {
  143         strncpy(fchost, dp->d_name, MAX_STR_LEN);
  144         return 1;
  145     }
  146 
  147     return 0;
  148 }
  149 
  150 static int read_fcoe_ctlr_device(struct dirent *dp, void *arg)
  151 {
  152     struct sa_table *ctlrs = arg;
  153     struct fcoe_ctlr_device *ctlr;
  154     char buf[MAX_STR_LEN];
  155     char lesb_path[MAX_STR_LEN];
  156     char hpath[MAX_STR_LEN];
  157     char fchost[MAX_STR_LEN];
  158     char *cp, *ifname;
  159     int rc;
  160 
  161     if (strncmp(dp->d_name, "ctlr_", 5))
  162         return 0;
  163 
  164     ctlr = malloc(sizeof(struct fcoe_ctlr_device));
  165     if (!ctlr)
  166         return 0; /* Must return 0 or loop will break */
  167 
  168     memset(ctlr, 0, sizeof(struct fcoe_ctlr_device));
  169     sa_table_init(&ctlr->fcfs);
  170 
  171     /* Save the path */
  172     snprintf(ctlr->path, sizeof(ctlr->path),
  173          FCOE_CTLR_DEVICE_DIR "%s", dp->d_name);
  174 
  175     /* Use the index from the logical enumeration */
  176     ctlr->index = atoi(dp->d_name + sizeof("ctlr_") - 1);
  177 
  178     rc = sa_dir_read(ctlr->path, find_fchost, fchost);
  179     if (!rc)
  180         goto fail;
  181 
  182     sprintf(hpath, "%s/%s/", SYSFS_FCHOST, fchost);
  183 
  184     rc = sa_sys_read_line(hpath, "symbolic_name", buf, sizeof(buf));
  185 
  186     /* Skip the HBA if it isn't Open-FCoE */
  187     cp = strstr(buf, " over ");
  188     if (!cp)
  189         goto fail;
  190 
  191     ifname = get_ifname_from_symbolic_name(buf);
  192     strncpy(ctlr->ifname, ifname, IFNAMSIZ-1);
  193 
  194     /* Get fcf device loss timeout */
  195     sa_sys_read_u32(ctlr->path, "fcf_dev_loss_tmo",
  196             &ctlr->fcf_dev_loss_tmo);
  197 
  198     sa_sys_read_line(ctlr->path, "mode", buf, sizeof(buf));
  199     sa_enum_encode(fip_conn_type_table, buf, &ctlr->mode);
  200 
  201     snprintf(lesb_path, sizeof(lesb_path), "%s/lesb/", ctlr->path);
  202 
  203     /* Get LESB statistics */
  204     sa_sys_read_u32(lesb_path, "link_fail",
  205             &ctlr->lesb_link_fail);
  206     sa_sys_read_u32(lesb_path, "vlink_fail",
  207             &ctlr->lesb_vlink_fail);
  208     sa_sys_read_u32(lesb_path, "miss_fka",
  209             &ctlr->lesb_miss_fka);
  210     sa_sys_read_u32(lesb_path, "symb_err",
  211             &ctlr->lesb_symb_err);
  212     sa_sys_read_u32(lesb_path, "err_block",
  213             &ctlr->lesb_err_block);
  214     sa_sys_read_u32(lesb_path, "fcs_error",
  215             &ctlr->lesb_fcs_error);
  216 
  217     /* Save the ctlr in the supplied table */
  218     if (sa_table_insert(ctlrs, ctlr->index, ctlr) < 0) {
  219         fprintf(stderr, "%s: insert of ctlr %d failed\n",
  220             __func__, ctlr->index);
  221         goto fail;
  222     }
  223 
  224     return 0;
  225 
  226 fail:
  227     free(ctlr);
  228     return -ENOENT;
  229 }
  230 
  231 void read_fcoe_ctlr(struct sa_table *ctlrs)
  232 {
  233     sa_dir_read(FCOE_CTLR_DEVICE_DIR, read_fcoe_ctlr_device, ctlrs);
  234     sa_table_iterate(ctlrs, read_fcoe_fcf, NULL);
  235 }
  236 
  237 void free_fcoe_ctlr_device(void *ep, UNUSED void *arg)
  238 {
  239     struct fcoe_ctlr_device *ctlr = (struct fcoe_ctlr_device *)ep;
  240 
  241     sa_table_iterate(&ctlr->fcfs, free_fcoe_fcf_device, NULL);
  242 
  243     free(ctlr);
  244 }