dd32cf
#! /bin/bash
dd32cf
dd32cf
usage()
dd32cf
{
dd32cf
  echo "$(basename $0) <options>"
dd32cf
  echo "-h, --help                print help message"
dd32cf
  echo "-v, --verbose             print more info"
dd32cf
}
dd32cf
dd32cf
case $1 in
dd32cf
  "-h" | "--help")
dd32cf
    usage
dd32cf
    exit 0
dd32cf
    ;;
dd32cf
esac
dd32cf
dd32cf
if (( $# > 1 )); then
dd32cf
  usage
dd32cf
  exit -1
dd32cf
fi
dd32cf
dd32cf
if (( $# == 1 )) && [ "$1" != "-v" ]; then
dd32cf
  usage
dd32cf
  exit -1
dd32cf
fi
dd32cf
dd32cf
ibdevs=$(ls /sys/class/infiniband/)
dd32cf
dd32cf
pushd /sys/class/net > /dev/null
dd32cf
devs=$(ls -d e* i* 2> /dev/null)
dd32cf
popd > /dev/null
dd32cf
if [ "x$devs" == "x" ]; then
dd32cf
  # no relevant devices - quit immediately
dd32cf
  exit
dd32cf
fi
dd32cf
dd32cf
for d in $devs; do
dd32cf
  if [ -f /sys/class/net/$d/dev_id ]; then
dd32cf
    oldstyle=n
dd32cf
    break
dd32cf
  fi
dd32cf
done
dd32cf
dd32cf
function print_line()
dd32cf
{
dd32cf
  echo "$1 port $2 <===> $3"
dd32cf
}
dd32cf
dd32cf
function find_guid()
dd32cf
{
dd32cf
  ibdevs=$(ls /sys/class/infiniband/)
dd32cf
  for ibdev in $ibdevs; do
dd32cf
    ports=$(ls /sys/class/infiniband/$ibdev/ports/)
dd32cf
    for port in $ports; do
dd32cf
      gids=$(ls /sys/class/infiniband/$ibdev/ports/$port/gids)
dd32cf
      for gid in $gids; do
dd32cf
        pguid=$(cat /sys/class/infiniband/$ibdev/ports/$port/gids/$gid | cut -b 21- | sed -e 's/://g')
dd32cf
        if [ x$pguid == x$1 ]; then
dd32cf
          print_line $ibdev $port $2
dd32cf
        fi
dd32cf
      done
dd32cf
    done
dd32cf
  done
dd32cf
}
dd32cf
dd32cf
function find_mac()
dd32cf
{
dd32cf
  ibdevs=$(ls /sys/class/infiniband/)
dd32cf
  for ibdev in $ibdevs; do
dd32cf
    type=$(cat /sys/class/infiniband/$ibdev/node_type|cut -d ' ' -f 2)
dd32cf
    ports=$(ls /sys/class/infiniband/$ibdev/ports/)
dd32cf
    for port in $ports; do
dd32cf
      gids=$(ls /sys/class/infiniband/$ibdev/ports/$port/gids)
dd32cf
      for gid in $gids; do
dd32cf
        if [[ "$type" = "RNIC" ]]; then
dd32cf
          pmac=$(cat /sys/class/infiniband/$ibdev/ports/$port/gids/$gid | cut -b 1-14|sed -e 's/://g')
dd32cf
        else
dd32cf
          first=$(cat /sys/class/infiniband/$ibdev/ports/$port/gids/$gid | cut -b 21-22)
dd32cf
          first=$(( first ^ 2 ))
dd32cf
          first=$(printf "%02x" $first)
dd32cf
          second=$(cat /sys/class/infiniband/$ibdev/ports/$port/gids/$gid | cut -b 21- | sed -e 's/://g' | cut -b 3-6)
dd32cf
          third=$(cat /sys/class/infiniband/$ibdev/ports/$port/gids/$gid | cut -b 21- | sed -e 's/://g' | cut -b 11-)
dd32cf
          pmac=$first$second$third
dd32cf
        fi
dd32cf
        if [ x$pmac == x$1 ]; then
dd32cf
          print_line $ibdev $port $2
dd32cf
        fi
dd32cf
      done
dd32cf
    done
dd32cf
  done
dd32cf
}
dd32cf
dd32cf
if [ "x$oldstyle" == "xn" ]; then
dd32cf
  for d in $ibdevs; do
dd32cf
    ibrsc=$(cat /sys/class/infiniband/$d/device/resource)
dd32cf
    eths=$(ls /sys/class/net/)
dd32cf
    for eth in $eths; do
dd32cf
      filepath_resource=/sys/class/net/$eth/device/resource
dd32cf
dd32cf
      if [ -f $filepath_resource ]; then
dd32cf
        ethrsc=$(cat $filepath_resource)
dd32cf
        if [ "x$ethrsc" == "x$ibrsc" ]; then
dd32cf
          filepath_devid=/sys/class/net/$eth/dev_id
dd32cf
          filepath_devport=/sys/class/net/$eth/dev_port
dd32cf
          if [ -f $filepath_devid ]; then
dd32cf
            port1=0
dd32cf
            if [ -f $filepath_devport ]; then
dd32cf
              port1=$(cat $filepath_devport)
dd32cf
              port1=$(printf "%d" $port1)
dd32cf
            fi
dd32cf
dd32cf
            port=$(cat $filepath_devid)
dd32cf
            port=$(printf "%d" $port)
dd32cf
            if [ $port1 -gt $port ]; then
dd32cf
              port=$port1
dd32cf
            fi
dd32cf
dd32cf
            port=$(( port + 1 ))
dd32cf
dd32cf
            filepath_carrier=/sys/class/net/$eth/carrier
dd32cf
dd32cf
            if [ -f $filepath_carrier ]; then
dd32cf
              link_state=$(cat $filepath_carrier 2> /dev/null)
dd32cf
              if (( link_state == 1 )); then
dd32cf
                link_state="Up"
dd32cf
              else
dd32cf
                link_state="Down"
dd32cf
              fi
dd32cf
            else
dd32cf
              link_state="NA"
dd32cf
            fi
dd32cf
dd32cf
            if [ "$1" == "-v" ]; then
dd32cf
              filepath_portstate=/sys/class/infiniband/$d/ports/$port/state
dd32cf
              filepath_deviceid=/sys/class/infiniband/$d/device/device
dd32cf
              filepath_fwver=/sys/class/infiniband/$d/fw_ver
dd32cf
              filepath_vpd=/sys/class/infiniband/$d/device/vpd
dd32cf
dd32cf
              # read port state
dd32cf
              if [ -f $filepath_portstate ]; then
dd32cf
                ibstate=$(printf "%-6s" "$(cat $filepath_portstate | gawk '{print $2}')")
dd32cf
              else
dd32cf
                ibstate="na"
dd32cf
              fi
dd32cf
dd32cf
              # read device
dd32cf
              if [ -f $filepath_deviceid ]; then
dd32cf
                devid=$(printf "mt%d" "$(cat $filepath_deviceid)")
dd32cf
              else
dd32cf
                devid="na"
dd32cf
              fi
dd32cf
dd32cf
              # read fw version
dd32cf
              if [ -f $filepath_fwver ]; then
dd32cf
                fwver=$(cat $filepath_fwver)
dd32cf
              else
dd32cf
                fwver="na"
dd32cf
              fi
dd32cf
dd32cf
              # read device description and part id from the vpd
dd32cf
              if [ -f $filepath_vpd ]; then
dd32cf
                tmp=$ifs
dd32cf
                ifs=":"
745326
                vpd_content=`cat $filepath_vpd | tr -d '\0'`
dd32cf
                devdesc=$(printf "%-15s" "$(echo $vpd_content | strings | head -1)")
dd32cf
                partid=$(printf "%-11s" "$(echo $vpd_content | strings | head -4 | tail -1 | gawk '{print $1}')")
dd32cf
                ifs=$tmp
dd32cf
              else
dd32cf
                devdesc=""
dd32cf
                partid="na"
dd32cf
              fi
dd32cf
dd32cf
              echo "$d ($devid - $partid) $devdesc fw $fwver port $port ($ibstate) ==> $eth ($link_state)"
dd32cf
            else
dd32cf
              echo "$d port $port ==> $eth ($link_state)"
dd32cf
            fi
dd32cf
          fi
dd32cf
        fi
dd32cf
      fi
dd32cf
    done
dd32cf
  done
dd32cf
else
dd32cf
##########################
dd32cf
### old style
dd32cf
##########################
dd32cf
  ifcs=$(ifconfig -a | egrep '^eth|^ib' | gawk '{print $1}')
dd32cf
dd32cf
  for ifc in $ifcs; do
dd32cf
    len=$(cat /sys/class/net/$ifc/addr_len)
dd32cf
    if (( len == 20 )); then
dd32cf
      guid=$(cat /sys/class/net/$ifc/address | cut -b 37- | sed -e 's/://g')
dd32cf
      find_guid $guid $ifc
dd32cf
    elif (( len == 6)); then
dd32cf
      mac=$(cat /sys/class/net/$ifc/address | sed -e 's/://g')
dd32cf
      find_mac $mac $ifc
dd32cf
    fi
dd32cf
  done
dd32cf
fi
dd32cf