1 #
   2 # CDDL HEADER START
   3 #
   4 # The contents of this file are subject to the terms of the
   5 # Common Development and Distribution License (the "License").
   6 # You may not use this file except in compliance with the License.
   7 #
   8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9 # or http://www.opensolaris.org/os/licensing.
  10 # See the License for the specific language governing permissions
  11 # and limitations under the License.
  12 #
  13 # When distributing Covered Code, include this CDDL HEADER in each
  14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15 # If applicable, add the following below this CDDL HEADER, with the
  16 # fields enclosed by brackets "[]" replaced with your own identifying
  17 # information: Portions Copyright [yyyy] [name of copyright owner]
  18 #
  19 # CDDL HEADER END
  20 #
  21 
  22 #
  23 # Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24 # Use is subject to license terms.
  25 #
  26 
  27 #
  28 # NAME
  29 #       discover_portal_info
  30 # DESCRIPTION
  31 #       discovery the portal information on the specified host
  32 #       to be utilized on startup checking and track the changes dynamically
  33 #
  34 # ARGUMENT
  35 #       $1 - host name
  36 #
  37 # RETURN
  38 #       Sets the result code
  39 #       return the available portal number
  40 #
  41 function discover_portal_info 
  42 {
  43         typeset host_name=$1
  44 
  45         typeset iscsi_host=$(format_shellvar $host_name)
  46         eval HOST_${iscsi_host}_PORTAL="\${HOST_${iscsi_host}_PORTAL:=''}"      
  47         typeset CMD="$IFCONFIG -a | egrep -v 'ether|inet|ipib|lo0' | cut -d: -f1 \
  48             | sort -u"
  49         run_rsh_cmd $host_name "$CMD"
  50         if [ `get_cmd_retval` -ne 0 ];then
  51                 report_err "rsh $host_name $CMD"
  52         fi
  53         
  54         nif=`get_cmd_stdout`
  55         for interface in $nif
  56         do
  57                 typeset cmd="$IFCONFIG $interface"
  58                 eval typeset port_list="\${HOST_${iscsi_host}_PORTAL}"
  59                 run_rsh_cmd $host_name "$cmd"
  60                 typeset ip_addr=`get_cmd_stdout | awk '/inet/{print $2}'`
  61                 typeset ip_state
  62                 get_cmd_stdout | grep "UP,BROADCAST" >/dev/null 2>&1 
  63                 if [ $? -eq 0 ];then
  64                         ip_state="up"
  65                 else
  66                         ip_state="down"
  67                 fi
  68 
  69                 #
  70                 # Now get ip_phy
  71                 #
  72                 typeset cmd="dladm show-phys $interface"
  73                 run_rsh_cmd $host_name "$cmd"
  74                 typeset ip_phy=`get_cmd_stdout | sed -n "2p"|awk '{print $2}'`
  75 
  76                 # Host IP address can not be used for iscsi test address
  77                 if [ "$ip_addr" = "$host_name" ];then
  78                         continue
  79                 fi
  80                 echo "$port_list" | grep -w "$ip_addr" >/dev/null 2>&1
  81                 typeset retval=$?
  82                 if [ $retval -ne 0 -a "$ip_addr" != "0.0.0.0" ];then
  83                         typeset new_portal="$interface:$ip_addr:$ip_state:$ip_phy"
  84                         eval HOST_${iscsi_host}_PORTAL=\"$new_portal $port_list\"
  85                 elif [ $retval -eq 0 -a "$ip_addr" != "0.0.0.0" ];then
  86                         port_list=$(echo $port_list | awk '{for(i=1;i<=NF;i++) \
  87                             if (! $i~/'"$ip_addr"'/) printf("%s ",$i)} \
  88                             END{print}')
  89                         typeset new_portal="$interface:$ip_addr:$ip_state:$ip_phy"
  90                         eval HOST_${iscsi_host}_PORTAL=\"$new_portal $port_list\"
  91                 else
  92                         cti_report "WARNING - $interface is configured with $ip_addr"
  93                         cti_report "WARNING - $(get_cmd_stdout)"                        
  94                 fi
  95 
  96         done
  97         eval set -A a_portal \${HOST_${iscsi_host}_PORTAL}
  98         return ${#a_portal[@]}
  99 }
 100 
 101 
 102 #
 103 # NAME
 104 #       get_portal_list
 105 # DESCRIPTION
 106 #       discovery the portal list on the specified host
 107 #
 108 # ARGUMENT
 109 #       $1 - host name
 110 #
 111 # RETURN
 112 #       Sets the result code
 113 #       void
 114 #
 115 function get_portal_list 
 116 {
 117         typeset host_name=$1
 118 
 119         typeset iscsi_host=$(format_shellvar $host_name)
 120         eval HOST_${iscsi_host}_PORTAL="\${HOST_${iscsi_host}_PORTAL:=''}"      
 121         eval typeset port_list="\${HOST_${iscsi_host}_PORTAL}"
 122         typeset ip_list=''
 123         for portal_info in $port_list
 124         do
 125                 typeset phy=`echo "$portal_info" | cut -d: -f4`
 126                 if [ "$TRANSPORT" = "ISER" -a "$phy" != "ipib" ];then
 127                         continue        
 128                 elif [ "$TRANSPORT" = "SOCKETS" -a "$phy" != "Ethernet" ];then
 129                         continue
 130                 else
 131                         :
 132                 fi
 133                 typeset ip=$(echo $portal_info | cut -d: -f2)
 134                 ip_list="$ip $ip_list"
 135         done
 136         typeset -L ip_list=$ip_list
 137         typeset -R ip_list=$ip_list
 138 
 139         echo $ip_list
 140 }
 141 
 142 #
 143 # NAME
 144 #       get_portal_number
 145 # DESCRIPTION
 146 #       discovery the portal number on the specified host
 147 #
 148 # ARGUMENT
 149 #       $1 - host name
 150 #
 151 # RETURN
 152 #       Sets the result code
 153 #       void
 154 #
 155 function get_portal_number 
 156 {
 157         typeset host_name=$1
 158 
 159         set -A a_portal $(get_portal_list $host_name)
 160         echo ${#a_portal[@]}
 161 }
 162 #
 163 # NAME
 164 #       get_portal_state
 165 # DESCRIPTION
 166 #       get the portal state of plumb or un-plumb on the specified ip address 
 167 #       on the specified host
 168 #
 169 # ARGUMENT
 170 #       $1 - host name
 171 #       $2 - the specified ip address
 172 #
 173 # RETURN
 174 #       Sets the result code
 175 #       0 - state is marked
 176 #       1 - ip is not found 
 177 #
 178 function get_portal_state 
 179 {
 180         typeset host_name=$1
 181         typeset ip_addr=$2
 182 
 183         typeset iscsi_host=$(format_shellvar $host_name)
 184         eval typeset port_list="\${HOST_${iscsi_host}_PORTAL}"
 185 
 186         typeset port_state=$(echo $port_list | awk '{for(i=1;i<=NF;i++) \
 187                             if ( $i~/'"$ip_addr"'/) print  $i}'|cut -d: -f3)
 188         if [ -n "$port_state" ];then
 189                 echo $port_state
 190                 return 0
 191         else
 192                 cti_report "WARNING - attempt to get the portal state of"\
 193                     "specified ip $ip_addr failed, reason: NOT FOUND"
 194                 return 1
 195         fi
 196 }
 197 
 198 #
 199 # NAME
 200 #       ifconfig_up_portal
 201 # DESCRIPTION
 202 #       ifconfig plumb the specified ip address on the specified host to ensure 
 203 #       the portal is online
 204 #
 205 # ARGUMENT
 206 #       $1 - host name
 207 #       $2 - the specified ip address
 208 #
 209 # RETURN
 210 #       Sets the result code
 211 #       void
 212 #
 213 function ifconfig_up_portal 
 214 {
 215         typeset host_name=$1
 216         typeset ip_addr=$2
 217 
 218         typeset state=$(get_portal_state $host_name $ip_addr)
 219         if [ $? -eq 1 -o -z "$state" ];then
 220                 return
 221         else
 222                 typeset iscsi_host=$(format_shellvar $host_name)
 223                 eval typeset port_list="\${HOST_${iscsi_host}_PORTAL}"
 224 
 225                 typeset interface=$(echo $port_list | awk '{for(i=1;i<=NF;i++)\
 226                     if ( $i~/'"$ip_addr"'/) print  $i}'|cut -d: -f1)            
 227 
 228                 typeset CMD="$IFCONFIG $interface up"
 229                 cti_report "$CMD [$ip_addr]"
 230                 run_rsh_cmd $host_name "$CMD"
 231                 if [ `get_cmd_retval` -eq 0 ];then
 232                         discover_portal_info $host_name
 233                 else
 234                         report_err "$CMD"
 235                         cti_unresolved "WARNING - on $host_name attempt to "\
 236                             "plumb the interface $interface failed"
 237                 fi
 238         fi
 239 }
 240 #
 241 # NAME
 242 #       ifconfig_down_portal
 243 # DESCRIPTION
 244 #       ifconfig unplumb the specified ip address on the specified host to ensure
 245 #       the portal is offline
 246 #
 247 # ARGUMENT
 248 #       $1 - host name
 249 #       $2 - the specified ip address
 250 #
 251 # RETURN
 252 #       Sets the result code
 253 #       void
 254 #
 255 function ifconfig_down_portal 
 256 {
 257         typeset host_name=$1
 258         typeset ip_addr=$2
 259 
 260         typeset state=$(get_portal_state $host_name $ip_addr)
 261         if [ $? -eq 1 -o -z "$state" ];then
 262                 return
 263         else
 264                 typeset iscsi_host=$(format_shellvar $host_name)
 265                 eval typeset port_list="\${HOST_${iscsi_host}_PORTAL}"
 266 
 267                 typeset interface=$(echo $port_list | awk '{for(i=1;i<=NF;i++)\
 268                     if ( $i~/'"$ip_addr"'/) print  $i}'|cut -d: -f1)
 269                 
 270                 typeset CMD="$IFCONFIG $interface down"
 271                 cti_report "$CMD [$ip_addr]"
 272                 run_rsh_cmd $host_name "$CMD"
 273                 if [ `get_cmd_retval` -eq 0 ];then
 274                         discover_portal_info $host_name
 275                 else
 276                         report_err "$CMD"
 277                         cti_unresolved "WARNING - on $host_name attempt to "\
 278                             "unplumb the interface $interface failed"
 279                 fi
 280         fi
 281 }
 282