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