"Fossies" - the Fresh Open Source Software Archive

Member "open-fcoe-3.19/fcoe-utils/contrib/fcc.sh" (15 Apr 2015, 9050 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) Bash source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 #! /bin/bash
    2 #
    3 # Copyright 2008-2010 Cisco Systems, Inc.  All rights reserved.
    4 #
    5 # This program is free software; you may redistribute it and/or modify
    6 # it under the terms of the GNU General Public License as published by
    7 # the Free Software Foundation; version 2 of the License.
    8 #
    9 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   10 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   11 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   12 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   13 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   14 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   15 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   16 # SOFTWARE.
   17 #
   18 # Author: Joe Eykholt (jeykholt at cisco dot com)
   19 #
   20 # Please send comments and changes to jeykholt at cisco dot com
   21 
   22 VERSION="fcc v1.0.13 11/10/2010"
   23 
   24 fcoe_dir=/sys/module/fcoe
   25 fcoe_lib_dir=/sys/module/libfcoe
   26 fdir=/sys/class/fc_host
   27 sdir=/sys/class/scsi_host
   28 cmdname=`basename $0`
   29 
   30 usage() {
   31 cat <<USAGE
   32 usage: $cmdname '[<cmd> [<hba> ...]]'
   33 
   34 cmd:
   35     create      Start FCoE on an Ethernet interface.
   36     create-vn   Start FCoE (VN_port-VN_port) on an Ethernet interface.
   37     delete / del    Delete an FCoE instance
   38     destroy     Same as delete
   39     enable / en Same as create
   40     help        Show this usage message
   41     info        Show HBA detailed info
   42     list        List the HBAs with remote port and LUN status
   43     luns        Show LUN list and status
   44     scan        Scan the HBA
   45     stats       Show HBA statistics
   46     reload      Reload fcoe and fnic modules
   47     reset       Reset the HBA
   48     version     Show version
   49 USAGE
   50 }
   51 
   52 verify_hba() {
   53     local x=$1
   54 
   55     if [ ! -d $fdir/$x ]
   56     then
   57         echo "$cmdname: invalid HBA name $x" >&2
   58         exit
   59     fi
   60 }
   61 
   62 hba_stats() {
   63     local x=$1
   64     local y
   65 
   66     verify_hba $x
   67 
   68     printf "\n$x Statistics:\n"
   69     (
   70         cd $fdir/$x/statistics
   71         for y in *
   72         do
   73             #
   74             # avoid the write-only files.
   75             # test -r doesn't help if we're root
   76             #
   77             if [ "$y" == "reset_statistics" ]
   78             then
   79                 continue
   80             fi
   81             val=`cat $y`
   82             if [ "$val" != 0xffffffffffffffff ]
   83             then
   84                 printf "%-30s%8lld\n" $y: $val
   85             fi
   86         done
   87     )
   88 }
   89 
   90 
   91 #
   92 # format a /sys file containing a hex WWN or FC_ID
   93 # from 0x123456\n to 12:34:56 or from 0xffffffff to - (unknown WWN or FC_ID)
   94 #
   95 fmt_hex() {
   96     sed -e 's/^0xff*$/-/' -e 's/0x//' -e 's/../&:/g' -e 's/:$//' < $1
   97 }
   98 
   99 rport_list() {
  100     local x
  101     local hba=$1
  102 
  103     rdir=/sys/class/fc_remote_ports
  104     host=`echo $hba | sed -e 's/host//'`
  105     rports=`ls -d $rdir/rport-$host:* 2>/dev/null`
  106     if [ -z "$rports" ]
  107     then
  108         return
  109     fi
  110 
  111     printf "\n$hba Remote Ports:\n"
  112 
  113 
  114     fmt="%-8s  %-23s  %-8s  %-8s  %-15s\n"
  115 
  116     printf "$fmt" Path "Port Name" "Port ID" State Roles
  117     for x in $rports
  118     do
  119         btl="`echo $x | sed -e 's/.*-\(.*-.*\)/\1/'`"
  120         printf "$fmt" "$btl" \
  121             "`fmt_hex $x/port_name`" \
  122             "`fmt_hex $x/port_id`" \
  123             "`cat $x/port_state`" \
  124             "`cat $x/roles`"
  125     done
  126 }
  127 
  128 #
  129 # Translate capacity to readable units.
  130 # compute sizes in base-10 marketing units
  131 #
  132 lun_cap() {
  133     local sizek=0 sizem=0 sizeg=0 sizet=0
  134     local size
  135     local onek=1000
  136     local cap
  137 
  138     size=$1
  139     let sizek="$size * 512 / $onek"
  140     let sizem="$sizek / $onek"
  141     let sizeg="$sizem / $onek"
  142     let sizet="$sizeg / $onek"
  143     if [ "$sizet" -gt 0 ]
  144     then
  145         cap="$sizet TB"
  146     elif [ "$sizeg" -gt 0 ]
  147     then
  148         cap="$sizeg GB"
  149     elif [ "$sizem" -gt 0 ]
  150     then
  151         cap="$sizem MB"
  152     else
  153         cap="$sizek KB"
  154     fi
  155     echo $cap
  156 }
  157 
  158 lun_list() {
  159     local x
  160     local lun
  161     local hba=$1
  162     local ddir=/sys/class/scsi_device
  163 
  164     host=`echo $hba | sed -e 's/host//'`
  165 
  166     local luns=`(cd $ddir &&
  167          ls -d $host:* | sort -n -t: -k1 -k2 -k3 -k4) 2>/dev/null`
  168 
  169     if [ -z "$luns" ]
  170     then
  171         return
  172     fi
  173 
  174     printf "\n$hba LUNs:\n"
  175 
  176     fmt="%-10s %-8s %6s   %-15s %-20s %-8s\n"
  177 
  178     printf "$fmt" Path Device Size Vendor Model State
  179 
  180     for lun in $luns
  181     do
  182         (
  183             local size=0
  184             cap=-
  185 
  186             cd $ddir/$lun/device
  187             if [ -d block ]
  188             then
  189                 dev=`ls block | tail -1`
  190                 size=`cat block/$dev/size`
  191                 cap=`lun_cap $size`
  192             elif [ -d char ]
  193             then
  194                 dev=`ls char | tail -1`
  195                 cap=-
  196             elif [ -d scsi_tape ]
  197             then
  198                 dev=`ls scsi_tape | egrep '^st[0-9]*$'`
  199                 cap=-
  200             elif [ -d scsi_generic ]
  201             then
  202                 dev=`ls scsi_generic | tail -1`
  203                 cap=-
  204             else
  205                 dev="`ls -d block:* char:* 2>/dev/null |
  206                     sed -e 's/.*\://'`"
  207                 if [ -L block:$dev -o -d block:$dev ]
  208                 then
  209                     size=`cat block:$dev/size`
  210                     cap=`lun_cap $size`
  211                 fi
  212             fi
  213 
  214             printf "$fmt" "$lun" "$dev" "$cap"\
  215                 "`cat vendor`" \
  216                 "`cat model`" \
  217                 "`cat state`"
  218         )
  219      done
  220 }
  221 
  222 sym_name() {
  223     local hba=$1
  224     local file
  225 
  226     file=$fdir/$hba/symbolic_name
  227     if [ -f "$file" ]
  228     then
  229         sed -e 's/.*over //' < $file
  230     else
  231         exit 1
  232     fi
  233 }
  234 
  235 #
  236 # Lookup the host name for a given symbolic name
  237 #
  238 hba_name() {
  239     local sym=$1
  240     local hba
  241 
  242     if [ -d "$fdir/$sym" ]
  243     then
  244         echo $sym
  245         exit
  246     fi
  247 
  248     for hba in $all_hbas
  249     do
  250         if [ "`sym_name $hba`" = "$sym" ]
  251         then
  252             echo $hba
  253             exit
  254         fi
  255     done
  256     exit 1
  257 }
  258 
  259 hba_state() {
  260     local x
  261 
  262     echo "FC HBAs:"
  263     fmt="%-8s  %-23s  %-8s  %-8s  %-15s\n"
  264     printf "$fmt" HBA "Port Name" "Port ID" State Device
  265     for x in $hbas
  266     do
  267         (
  268             cd $fdir/$x
  269             printf "$fmt" "$x" \
  270                 "`fmt_hex $fdir/$x/port_name`" \
  271                 "`fmt_hex $fdir/$x/port_id`" \
  272                 "`cat $fdir/$x/port_state`" \
  273                 "`sym_name $x`"
  274         ) 2>/dev/null
  275     done
  276 }
  277 
  278 hba_info() {
  279     local x=`hba_name $1`
  280     local fmt="\t%-20s %s\n"
  281 
  282     verify_hba $x
  283     printf "\n$x Info:\n"
  284     (
  285         cd $fdir/$x
  286 
  287         printf "$fmt" "Symbolic Name" "`cat symbolic_name`"
  288         printf "$fmt" "Port Name" "`fmt_hex port_name`"
  289         printf "$fmt" "Node Name" "`fmt_hex node_name`"
  290         printf "$fmt" "Port Type" "`cat port_type`"
  291         echo
  292         printf "$fmt" "Port State" "`cat port_state`"
  293         printf "$fmt" "Port ID" "`fmt_hex port_id`"
  294         printf "$fmt" "Fabric Name" "`fmt_hex fabric_name`"
  295         echo
  296         printf "$fmt" "Max Frame Size" "`cat maxframe_size`"
  297         printf "$fmt" "Speed" "`cat speed`"
  298         echo
  299     )
  300 }
  301 
  302 scsi_state() {
  303     local x
  304     local dev
  305 
  306     printf "\nSCSI States:\n"
  307     fmt="%-8s  %-10s  %-15s  %-10s  %8s\n"
  308     printf "$fmt" HBA Device Mode State Busy
  309     for x in $scsi_hbas
  310     do
  311         (
  312             cd $fdir/$x
  313             dev="`cat $sdir/$x/proc_name``cat $sdir/$x/unique_id`"
  314             printf "$fmt" "$x" "$dev" \
  315                 "`cat $sdir/$x/active_mode`" \
  316                 "`cat $sdir/$x/state`" \
  317                 "`cat $sdir/$x/host_busy`"
  318         ) 2>/dev/null
  319     done
  320 }
  321 
  322 hba_list() {
  323     local x
  324 
  325     hba_state
  326     #  scsi_state
  327 
  328     for x in $hbas
  329     do
  330         rport_list $x
  331         lun_list $x
  332     done
  333 }
  334 
  335 #
  336 # Do a command for a list of arguments
  337 #
  338 repeat() {
  339     local cmd=$1
  340     local x
  341     shift
  342 
  343     for x
  344     do
  345         $cmd $x
  346     done
  347 }
  348 
  349 fcoe_ctl() {
  350     local cmd=$1
  351     local hba=$2
  352     local file=$fcoe_lib_dir/parameters/$cmd
  353 
  354     if [ -w "$file" ]
  355     then
  356         echo $hba > $file
  357     elif [ -f "$file" ]
  358     then
  359         echo "$cmdname: no permission to $cmd $hba" >&2
  360     else
  361         echo "$cmdname: file $file doesn't exist.  " \
  362             "Check for fcoe module." >&2
  363     fi
  364 }
  365 
  366 fc_host_ctl() {
  367     local hba=$2
  368     local host=$2
  369     local cmd=$1
  370     local value
  371     local file
  372     local dir
  373 
  374     dir=$fdir/$host
  375     if [ ! -d "$dir" ]
  376     then
  377         host=`hba_name $hba`
  378         dir=$fdir/$host
  379         if [ $? != 0 ]
  380         then
  381             echo "$cmdname: hba $hba not found" >&2
  382             exit 1
  383         fi
  384     fi
  385 
  386     case "$cmd" in
  387         reset)
  388             file=$dir/issue_lip
  389             value=1
  390         ;;
  391         scan)
  392             file=$dir/device/scsi_host/$host/scan
  393             value="- - -"
  394     esac
  395 
  396     if [ -w "$file" ]
  397     then
  398         echo $value > $file
  399     elif [ -f "$file" ]
  400     then
  401         echo "$cmdname: no permission to $cmd $hba" 1>&2
  402         exit 2
  403     else
  404         echo "$cmdname: $cmd not supported for $hba" 1>&2
  405         exit 2
  406     fi
  407 }
  408 
  409 load_mod()
  410 {
  411     if [ ! -d $fcoe_dir ]
  412     then
  413         modprobe fcoe
  414         echo "$cmdname: loading fcoe module" >&2
  415         sleep 1
  416         if [ ! -d $fcoe_dir ]
  417         then
  418             echo "$cmdname: $fcoe_dir not found" >&2
  419             exit 2
  420         fi
  421     fi
  422 }
  423 
  424 #
  425 # Start of main script code.
  426 #
  427 scsi_hbas=
  428 if [ -d "$sdir" ]
  429 then
  430     scsi_hbas=`ls $sdir 2>/dev/null`
  431 fi
  432 if [ -d "$fdir" ]
  433 then
  434     # sort the list of HBAs by the number after the 't' in "host9"
  435     all_hbas=`ls $fdir 2>/dev/null | sort -n -tt -k2`
  436 fi
  437 hbas="$all_hbas"
  438 
  439 if [ $# -lt 1 ]
  440 then
  441     hba_list
  442     exit 0
  443 fi
  444 
  445 if [ $# -lt 2 ]
  446 then
  447     cmd=$1
  448     hba_spec=no
  449 else
  450     cmd=$1
  451     hba=$2
  452     shift
  453     if [ $# -eq 0 ]
  454     then
  455         hba_spec=n
  456     elif [ $# -eq 1 -a "$1" = all ]
  457     then
  458         hba_spec=y
  459     else
  460         hba_spec=y
  461         hbas="$@"
  462         scsi_hbas="$@"
  463     fi
  464 fi
  465 
  466 hba_required()
  467 {
  468     if [ "$hba_spec" != y ]
  469     then
  470         echo "$cmdname: $cmd requires HBA name" >&2
  471         exit 2
  472     fi
  473 }
  474 
  475 case "$cmd" in
  476     create | enable | en)
  477         hba_required
  478         load_mod
  479         repeat "fcoe_ctl create" $hbas
  480         ;;
  481     create-vn)
  482         hba_required
  483         load_mod
  484         repeat "fcoe_ctl create_vn2vn" $hbas
  485         ;;
  486     delete | del | destroy)
  487         if [ ! -d $fcoe_lib_dir ]
  488         then
  489             echo "$cmdname: $fcoe_lib_dir not found" >&2
  490             exit 2
  491         fi
  492         hba_required
  493         repeat "fcoe_ctl destroy" $hbas
  494         ;;
  495     info)
  496         repeat hba_info $hbas
  497         ;;
  498     list)
  499         hba_list
  500         ;;
  501     lun*)
  502         repeat lun_list $hbas
  503         ;;
  504     names)
  505         repeat echo $hbas
  506         ;;
  507     stat*)
  508         repeat hba_stats $hbas
  509         ;;
  510     realname)
  511         hba_name $hba
  512         ;;
  513     reload)
  514         mods=
  515         for mod in fcoe fnic libfcoe libfc
  516         do
  517             if [ -d /sys/module/$mod ]
  518             then
  519                 mods="$mods $mod"
  520             fi
  521         done
  522         rmmod $mods
  523         for mod in $mods
  524         do
  525             modprobe $mod
  526         done
  527         ;;
  528     reset | scan)
  529         hba_required
  530         repeat "fc_host_ctl $cmd" $hbas
  531         ;;
  532     version)
  533         echo $VERSION
  534         ;;
  535     *)
  536         usage
  537         ;;
  538 esac