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