#! /pkg/bin/ksh
#--------------------------------------------------------------
# show_tech_bfdoff_pd  -- BFD Offload show tech-support utils
#
# Copyright (c) 2015-2019 by cisco Systems, Inc.
# All rights reserved.
#-------------------------------------------------------------

. /pkg/bin/show_tech_main_fragment

num_j2="0"
bfdhwoff_pkt_sock_drops="0"

#     BCM88675 (Jericho),    Also BCM88670
#     BCM88680 (JerichoPlus) 
#     BCM88370 (Qumran-MX),  Also BCM88375
#     BCM88470 (Qumran-AX)
#     BCM88690 (Jericho-2
#     We need to revise this logic in the future:
#     1. loop through all possible Dune chip type, find out how many each type(s)
#        are present on the board
#     2. Instead of blindly repeat each cli MAX_NPU number of times, use result
#        from step 1 to dynamically determine how many npus are present
#     3. Asic specific commands(other than J2)
get_brcm_npu_type() {
    brcm_vendor_id=14e4
    brcm_j2_dev_id=869      # 869X
    num_j2=$(lspci -nd ${brcm_vendor_id}: | grep -oc ${brcm_j2_dev_id})    
}

# *************************************************
# BFD related diagshell commands for Jericho chips 
# *************************************************
get_brcm_npu_type

# *******************************************
# BFD Punt packet losses between J2 and CPU
# *******************************************
show_bfdhwoff_pkt_sock_drops() {
    bfd_pid=`pidof bfd_agent`
    numre='^[0-9]+$'
    if [[ $bfd_pid =~ $numre ]]; then
        sock_inode=`lsof -p ${bfd_pid} | grep PACKET | awk '{print $8}'`
        bfdhwoff_pkt_sock_drops=`cat /proc/${bfd_pid}/net/packet | grep ${sock_inode} | awk '{print $14}'`
        echo "BFD-HWOFF Packet Sock Drops: ${bfdhwoff_pkt_sock_drops}"
        print_heading "BFD-HWOFF Packet Sock Drops: ${bfdhwoff_pkt_sock_drops}"
     fi
}

#
# All diagshell commands follow here
#
cmd_index=1
if [ ${num_j2} -gt 0 ]; then
    #
    # J2 only diagshell commands
    #
    # "diag count g " needs to run twice since the count is a running count
    dshell_cmd[$cmd_index]="getreg oamp_cpuport"
    ((cmd_index++))

    #show-tech framework allows only uniq command. as a work-around, using different
    #formats for same command
    dshell_cmd[$cmd_index]="get oamp_cpuport"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="getreg OAMP_VALIDITY_CHECK_STICKY"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="setreg OAMP_VALIDITY_CHECK_STICKY 0"
    ((cmd_index++))

    #show-tech framework allows only uniq command. as a work-around, using different
    #formats for same command
    dshell_cmd[$cmd_index]="get OAMP_VALIDITY_CHECK_STICKY"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="oam oamp counter"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="pp info fec"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="vis lag"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="oam bfdendpoint"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="oam last_lookup"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="oam oamp status"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="oam oamp MEP_DB partition"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="oam oamp MEP_DB allocation"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="dbal table dump table=OAMP_RMEP_DB"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="dbal table dump table=OAMP_MEP_DB"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="dbal table dump table=OAMP_MEP_TYPE_TRAP_CODE_TCAM"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="dbal table dump table=OAMP_TX_RX_COUNTERS"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="dbal table dump table=OAMP_STATUS"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="g OAMP_PUNT_EVENT_HENDLING[0]"
    ((cmd_index++))

    #show-tech framework allows only uniq command. as a work-around, using different
    #formats for same command
    dshell_cmd[$cmd_index]="diag counter graphical cdsp"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="diag counter g cdsp"
    ((cmd_index++))

    #show-tech framework allows only uniq command. as a work-around, using different
    #formats for same command
    dshell_cmd[$cmd_index]="diag counter graphical"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="diag counter g"
    ((cmd_index++))

    #show-tech framework allows only uniq command. as a work-around, using different
    #formats for same command
    dshell_cmd[$cmd_index]="oam oamp counter"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="oam oamp count"
    ((cmd_index++))

else
    #
    # J only diagshell commands
    #
    # "diag count g " needs to run twice since the count is a running count
    dshell_cmd[$cmd_index]="diag counter graphical"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="diag counter graphical"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="dump chg IHB_CPU_TRAP_CODE_CTR"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="get oamp_cpuport"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="get OAMP_VALIDITY_CHECK_STICKY"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="diag oam endpoint"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="diag oam counters"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="dump disable_cache chg OAMP_MEP_DB"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="dump disable_cache chg OAMP_MEP_DB_BFD_ON_IPV4_MULTI_HOP"
    ((cmd_index++))

    dshell_cmd[$cmd_index]="dump disable_cache chg oamp_rmep_db"
    ((cmd_index++))
fi

cmd_index=1
if [ ${num_j2} -gt 0 ]; then
    i=0
    while [[ $i -lt $num_j2 ]]; do
        for d in "${!dshell_cmd[@]}"; do
            dcmd=$(eval "echo ${dshell_cmd[d]} | sed 's/ /-/g'")
            bfdpd_rplc_show_exec[$cmd_index]="show controllers fia diagshell $i ${dshell_cmd[d]} location \$location"
            bfdpd_rplc_show__ksh[$cmd_index]="fia_driver_show -c $dcmd -u $i "
            ((cmd_index++))
         done
         ((i++))
    done
else
    for d in "${!dshell_cmd[@]}"; do
        dcmd=$(eval "echo ${dshell_cmd[d]} | sed 's/ /-/g'")
        bfdpd_rplc_show_exec[$cmd_index]="show controllers fia diagshell 0 ${dshell_cmd[d]} location \$location"
        bfdpd_rplc_show__ksh[$cmd_index]="fia_driver_show -c $dcmd -u 0 -n A "
        ((cmd_index++))
     done
fi

bfdpd_rplc_show_exec[$cmd_index]=''
bfdpd_rplc_show__ksh[$cmd_index]=''

#############################################################################
# Script

display_bfd_hwoff_pd() {
    print_heading "show tech-support bfd-pd-dnx"
   if [ "$__cardtype" != "SYS" ]; then
       case "$__cardtype" in

       "LC")
           exec_commands bfdpd_rplc_show
           show_bfdhwoff_pkt_sock_drops
           ;;
       "RP")
           exec_commands bfdpd_rplc_show
           show_bfdhwoff_pkt_sock_drops
           ;;
       esac
   fi
}
