#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/sbin/mkiba/mkiba 1.10.2.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2005,2014 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#   COMPONENT_NAME: ibaif
#
#   FUNCTIONS: problem
#		usage
#
#   ORIGINS: 27
#
#                    -- (                            when
#   combined with the aggregated modules for this product)
#   OBJECT CODE ONLY SOURCE MATERIALS
#
#   (C) COPYRIGHT International Business Machines Corp. 1995
#   All Rights Reserved
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
# FILE NAME: mkiba
#
# FILE DESCRIPTION: High-level shell command for performing minimal
#   configuration required to get a machine up and running TCP/IP over IB.
#
#   See Usage message for explanation of parms
#
#
# RETURN VALUE DESCRIPTION: 
#                             0         Successful
#                             non-zero  Unsuccessful
# 
#
# EXTERNAL PROCEDURES CALLED: chdev, hostname, hostent, lsdev
#                             mkdev, netstat, namerslv, /etc/rc.tcpip
#
#
# GLOBAL VARIABLES: none
#
################################# usage #######################################
#
# NAME: usage()
#
# DESCRIPTION: Issue "usage" message and exit.
#
# INPUT: 
#        None
#
# OUTPUT:
#        Error messages (Standard Error)
#
# RETURN VALUE DESCRIPTION:
#                           2
#
# NOTE: This function will not return (i.e., it will exit the entire
#       script with exit status of 2).
#
usage(){
   cat <<- HERE >&2
   Usage:  $NAME { <-a address | -v address6> -i interface -A ib_adapter -p ib_port [-P P_KEY ] [-v ipv6prefix] [-m subnet_mask]  [-S state] [ -M mtu ] [ -q queue_pair_size ] [ -Q Q_KEY ]  [-k superpacket]  [-t thread ]}

   -a address             IP address of the interface specified by -i
                          (must be in dotted decimal notation)
   -v address6            IPv6 address
   -V ipv6prefix          IPv6 prefix 
   -i interface           Interface to associate with the -a IP address
   -A ib_adapter          IB adapter associated to the interface
   -p ib_port             IB port associated with the IB adapter.
   -P p_key                Partition key associated with the IB port.
   -m subnet_mask         Subnetwork mask (dotted decimal or 0x notation)
   -S state               down,up,detach : The state of the interface following
                          the command
   -M ib_mtu              HCA MTU required
   -q srq_size            Send and Receive queue sizes  
   -Q Q_KEY               Q_Key associated with the multicast group 
   -k superpacket         Superpacket feature on or off 
   -t thread              Threads enable on or off  
   Example: $NAME -a 192.9.200.9 -i ib0 -A iba0 -p 1 -P -1 -q 4000 -M 2044 -m 255.255.255.0  
            $NAME -v fe80::2:c903:1:1b40 -i ib0 -A iba0 -p 1 -P -1 -q 4000 -M 2044 -t on

   Note: Use ifconfig ibX to check state of the interface
         Use ibstat to troubleshoot InfiniBand.

HERE
   exit 2			# don't return
}

############################# mandatory #######################################
#
# NAME: mandatory()
#
# DESCRIPTION: Issue "mandatory" message and invoke "usage" function.
#
# INPUT: 
#        None
#
# OUTPUT:
#        Error messages (Standard Error)
#
# RETURN VALUE DESCRIPTION:
#                           None
#
mandatory(){
   /usr/bin/echo "$NAME: Mandatory flag(s) missing" >&2
   usage			# issue msg and don't return
}

############################## problem ########################################
#
# NAME: problem()
#
# DESCRIPTION: Issue "problem" message and exit
#
# INPUT: 
#        $RTNCODE
#        $1		Name of command that encountered an error
#
# OUTPUT:
#        Error messages (Standard Error)
#
# RETURN VALUE DESCRIPTION:
#                           $RTNCODE
#
# NOTE: This function will not return (i.e., it will exit the entire
#       script with exit status of $RTNCODE).
#
problem(){
   /usr/bin/echo "$NAME: Problem with $1 command, return code = $RTNCODE" >&2
   exit $RTNCODE			# don't return
}

############################### dotted ########################################
#
# NAME: dotted()
#
# DESCRIPTION: Perform rudimentary check of IP address to see if composed
#              of dots and numbers.  Will issue error message and invoke
#              usage function if error detected.
#
# INPUT: 
#        $1, IP Address
#
# OUTPUT:
#        Error messages (Standard Error).  If error detected.
#
# RETURN VALUE DESCRIPTION:
#                           0     (Successful)
#                           None  (Will invoke usage function if error)
#
dotted(){
# compare number chars to number of "decimal and 0-9" characters
if [ "`expr $1 : '.*'`" -ne "`expr $1 : '[.0-9]*'`" ] ; then
   /usr/bin/echo "$NAME: IP address, $1, not in dotted decimal notation" >&2
   usage			# issue message and don't return
fi
}

############################## main ############################################

PATH=/bin:/usr/bin:/usr/sbin:/etc:/usr/ucb export PATH

NAME=$0

# Parse command flags and arguments
set -- `getopt h:a:i:A:m:p:P:A:S:q:M:Q:k:v:V:t: $*`
if [ $? != 0 ] ; then		# test for syntax error
   usage			# issue msg and don't return
fi

HOSTNAME= IPADDRESS= IPV6ADDRESS= INTERFACE= SUBNETMASK= DEVNAME= SERVADDR= CONNECTION= 
STATE= IDLE= UBR=

while [ "$1" != "--" ]
do
   case $1 in
      -a)
	 IPADDRESS=$2 shift 2
      ;;
      -v) 
         IPV6ADDRESS=$2 shift 2
      ;;
      -V)
         PREFIXLEN=$2 shift 2
      ;;
      -i)
	 INTERFACE=$2 shift 2
      ;;
      -A)
         IB_ADAPTER=$2 shift 2
      ;;
      -m)
	 SUBNETMASK=" -a netmask=$2" shift 2
      ;;
      -p)
	 IB_PORT=$2 shift 2
      ;;
      -P)
         P_KEY=$2 shift 2
      ;;
      -S)
	 STATE=$2 shift 2
      ;;
      -q)
         SRQ_SIZE=$2 shift 2
      ;;
      -M)
         IB_MTU=$2 shift 2
      ;;
      -Q)
         Q_KEY=$2 shift 2
      ;;
      -k)
         SUPERPKT=$2 shift 2
      ;;
      -t)
         THREAD=$2 shift 2
      ;;
      *)
	 usage				# shouldn't ever hit this
      ;;
   esac
done

if [ $# -gt 1 ] ; then			# see if extra parms specified
   /usr/bin/echo "$NAME: Too many flags" >&2
   usage				# issue msg and don't return
fi

if [ "$STATE" = "" ]
then STATE="up"
fi


SHW=`/usr/bin/echo $INTERFACE | /usr/bin/cut -c1-2`	#to get first two letters, ex. en, et..
INT=`/usr/bin/echo $INTERFACE | /usr/bin/cut -c3-4`	#to get last digits in interface.

# See if mandatory parms were specified
if [  "$IPADDRESS" = "" -a "$IPV6ADDRESS" = ""  -o "$INTERFACE" = ""  -o "$IB_ADAPTER" = "" -o "$IB_PORT" = "" ] ; then
   mandatory
fi

if [ "$IPADDRESS" ]; then
dotted $IPADDRESS		# rudimentary check on address

if [ "$SUBNETMASK" = "" ]; then
   /usr/bin/echo " ERROR: A subnetmask is required for the $IPADDRESS address (e.i -m 255.255.255.0 )"
   exit 1
fi

fi

if /usr/sbin/lsdev -C | /usr/bin/grep icm | /usr/bin/grep Available 2>&1 >/dev/null; then
 :                        #icm exist
else
/usr/bin/echo "mkiba: icm kernel extension  is not available. run smitty icm"
exit 1 
fi

if /usr/sbin/lsdev -C  | /usr/bin/egrep $INTERFACE 2>&1 > /dev/null ; then
   : 			# the interface already exists
else
      /usr/lib/methods/defibaif -c if -s IB -t ib -l $INTERFACE  2>&1 >/dev/null
      RTNCODE=$?
      [ $RTNCODE -ne 0 ] && problem mkdev
fi

# Build the command to configure the interface
COMMAND='/usr/sbin/chdev -l "$INTERFACE"  -a ib_adapter="$IB_ADAPTER" -a ib_port="$IB_PORT"  $SUBNETMASK -a state="$STATE"'

if test "$IPADDRESS" != ""; then
COMMAND=`/usr/bin/echo $COMMAND  '-a netaddr="$IPADDRESS"' `
fi
if test "$IPV6ADDRESS" != ""; then

COMMAND=`/usr/bin/echo $COMMAND  '-a netaddr6="$IPV6ADDRESS"' `
fi
if test "$IB_MTU" != ""; then
COMMAND=`/usr/bin/echo $COMMAND  '-a mtu="$IB_MTU"' `
fi
if test "$P_KEY" != ""; then
COMMAND=`/usr/bin/echo $COMMAND '-a p_key="$P_KEY"' `
fi
if test "$SRQ_SIZE" != ""; then
COMMAND=`/usr/bin/echo $COMMAND '-a srq_size="$SRQ_SIZE"'`
fi
if test "$Q_KEY" != ""; then
COMMAND=`/usr/bin/echo $COMMAND '-a q_key="$Q_KEY"'`
fi
if test "$SUPERPKT" != ""; then
COMMAND=`/usr/bin/echo $COMMAND '-a superpacket="$SUPERPKT"'`
fi
if test "$PREFIXLEN" != ""; then
COMMAND=`/usr/bin/echo $COMMAND '-a prefixlen="$PREFIXLEN"'`
fi
if test "$THREAD" != ""; then
COMMAND=`/usr/bin/echo $COMMAND '-a thread="$THREAD"'`
fi
 
eval  $COMMAND

RTNCODE=$?

[ $RTNCODE -ne 0 ] && problem chdev

#/usr/lib/methods/cfgif -l $INTERFACE
exit 0