#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_qos_tm - Jericho/JerichoPlus
#
# Jan 2022, Akshay
#
# Copyright (c) 2022 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------

. /pkg/bin/show_tech_main_fragment


__cardtype="unspecified"
__arch=$(echo $ENV_FILE | cut -d'/' -f4)

# Parse the arguments to the script.
while [ "$#" -gt "0" ]; do
    case "$1" in
        -t) __cardtype="$2"; shift 2;;
        *)  default_parser_function "$@"; shift $#;;
    esac
done

if [ "$__filename" = "unspecified" ]; then
    echo "show_tech_qos_tm: output file not specified"
    exit
fi

if [ "$__cardtype" == "unspecified" ]; then
    __cardtype=$(node_type)
fi

PLATFORM_TYPE=$(uname -m)

# ***********************************************************
#  Show commands to be run by the show tech-support commands
# ***********************************************************

#############################################################
# Show commands that run once per system
sys_show_exec[1]='show version'
sys_show__ksh[1]='ng_show_version'
sys_show_exec[2]='show install active'
sys_show__ksh[2]='sh /pkg/bin/instcmd_cli_hook.sh show install active'
sys_show_exec[3]='show run'
sys_show__ksh[3]='nvgen -c -l 1 -t 1 -o 1'
sys_show_exec[4]='show log'
sys_show__ksh[4]='show_logging'
#End
sys_show__ksh[5]=''

# Trace
sys_trace__ksh[1]=''
#End


#############################################################
# Commands that run on RP and LC nodes

# Show commands

# Show procs
rp_show__ksh[1]=''
#End

#############################################################
# Commands that run on all LCs
cmd_index=1

# NIF layer drops, look for NbihPktDrop
xrlc_exec[$cmd_index]='show controllers fia diagshell all diag-counters location $location'
xrlc__ksh[$cmd_index]='fia_driver_show -c diag-counters -u all -n $fq_nodeid'

## taking 2nd iteration of above
((cmd_index++))
xrlc_exec[$cmd_index]='ITER2 - show controllers fia diagshell all diag-counters location $location'
xrlc__ksh[$cmd_index]='fia_driver_show -c diag-counters -u all -n $fq_nodeid'

# Dump the "diag counter g"
((cmd_index++))
xrlc_exec[$cmd_index]='show controllers fia diagshell all diag-counter-g location $location'
xrlc__ksh[$cmd_index]='fia_driver_show -c diag-counter-g -u all -n $fq_nodeid'

## taking 2nd iteration of above
((cmd_index++))
xrlc_exec[$cmd_index]='ITER2 - show controllers fia diagshell all diag-counter-g location $location'
xrlc__ksh[$cmd_index]='fia_driver_show -c diag-counter-g -u all -n $fq_nodeid'

# Voq-Usage
((cmd_index++))
xrlc_exec[$cmd_index]='show controllers npu voq-usage interface all instance all location $location'
xrlc__ksh[$cmd_index]='dpa_qosea_voq_show -v x -i 0x10 -n $fq_nodeid -t n -p 0x0'

# Traps
((cmd_index++))
xrlc_exec[$cmd_index]='show controllers npu stats traps-all instance all location $location'
xrlc__ksh[$cmd_index]='ofa_npu_stats_show -v a -i 0x10 -n $fq_nodeid -t t -p 0xffffffff -s A -d 0x0'

# asic-errors
((cmd_index++))
xrlc_exec[$cmd_index]='show asic-errors all location $location'
xrlc__ksh[$cmd_index]='asic_errors_show -n $fq_nodeid -a 0x0 -s -x 0x0 -t 0x0 -m 0x0'

# Understand which queue is congested
((cmd_index++))
xrlc_exec[$cmd_index]='show controllers fia diagshell all diag-cosq-non location $location'
xrlc__ksh[$cmd_index]='fia_driver_show -c diag-cosq-non -u all -n $fq_nodeid'

# Check the diag egr to understand the PD and DB usage
((cmd_index++))
xrlc_exec[$cmd_index]='show controllers fia diagshell all diag-egr location $location'
xrlc__ksh[$cmd_index]='fia_driver_show -c diag-egr -u all -n $fq_nodeid'

# Ingress buffer utilization
((cmd_index++))
xrlc_exec[$cmd_index]='show controllers fia diagshell all diag-ing location $location'
xrlc__ksh[$cmd_index]='fia_driver_show -c diag-ing -u all -n $fq_nodeid'

# dump register for all instance
((cmd_index++))
xrlc_exec[$cmd_index]='show controllers fia register-dump instance all location $location'
xrlc__ksh[$cmd_index]='fia_driver_show -i 0xff -n $fq_nodeid -r -r'

((cmd_index++))
xrlc_exec[$cmd_index]='copy the /var/log files to showtech dir'
xrlc__ksh[$cmd_index]='cp /var/log/fia_debug_dump_unit_*.txt.gz $__tar_file_directory_on_node 2>> /dev/null'

((cmd_index++))
#End
xrlc__ksh[$cmd_index]=''

#############################################################
# Commands that run on J/J+ LCs

cmd_index=1

# Reject reasonV
jlc_exec[$cmd_index]='show controller fia diagshell all g-chg-IQM_REJECT_STATUS_BMP location $location'
jlc__ksh[$cmd_index]='fia_driver_show -c g-chg-IQM_REJECT_STATUS_BMP -u all -n $fq_nodeid'

# Check the PQP discard reason if the PQP_DSCRD counters are non-zero in the "diag counter g"
((cmd_index++))
jlc_exec[$cmd_index]='show controllers fia diagshell all g-CGM_PQP_DISCARD_REASONS location $location'
jlc__ksh[$cmd_index]='fia_driver_show -c g-CGM_PQP_DISCARD_REASONS -u all -n $fq_nodeid'

# Check the RQP discard reason if the RQP_DSCRD counters are non-zero in the "diag counter g"
((cmd_index++))
jlc_exec[$cmd_index]='show controllers fia diagshell all g-CGM_RQP_DISCARD_REASONS location $location'
jlc__ksh[$cmd_index]='fia_driver_show -c g-CGM_RQP_DISCARD_REASONS -u all -n $fq_nodeid'

# Check for flow control
# For scheduled traffic FDR_FDR_SCH_RCI_SCORE_WM value should be non-zero
((cmd_index++))
jlc_exec[$cmd_index]='show controllers fia diagshell all g-FDR_FDR_SCH_RCI_SCORE_WM location $location'
jlc__ksh[$cmd_index]='fia_driver_show -c g-FDR_FDR_SCH_RCI_SCORE_WM -u all -n $fq_nodeid'

# Centralized Flow Control: This will display the flow control sending from Scheduler to EGQ
((cmd_index++))
jlc_exec[$cmd_index]='show controllers fia diagshell all g-EGQ_CFC_FLOW_CONTROL location $location'
jlc__ksh[$cmd_index]='fia_driver_show -c g-EGQ_CFC_FLOW_CONTROL -u all -n $fq_nodeid'

# This will provide profile index for a given TM_PORT
((cmd_index++))
jlc_exec[$cmd_index]='show controllers fia diagshell all dump-EGQ_PPCT location $location'
jlc__ksh[$cmd_index]='fia_driver_show -c dump-EGQ_PPCT -u all -n $fq_nodeid'

# This will provide the content of the each profile
((cmd_index++))
jlc_exec[$cmd_index]='show controllers fia diagshell all dump-EGQ_TC_DP_MAP location $location'
jlc__ksh[$cmd_index]='fia_driver_show -c dump-EGQ_TC_DP_MAP -u all -n $fq_nodeid'

# VOQ properties
#((cmd_index++))
#jlc_exec[$cmd_index]='show controllers fia diagshell 0 "diag cosq voq id=<voq> det=1" location $location'
#jlc__ksh[$cmd_index]=''

((cmd_index++))
#End
jlc__ksh[$cmd_index]=''

#############################################################
# Commands that run on J2 LCs

cmd_index=1

# NIF layer drops, look for NbihPktDrop
j2lc_exec[$cmd_index]='show controllers fia diagshell all diag-counter location $location'
j2lc__ksh[$cmd_index]='fia_driver_show -c diag-counter -u all -n $fq_nodeid'
## taking 2nd iteration of above
((cmd_index++))
j2lc_exec[$cmd_index]='ITER2 - show controllers fia diagshell all diag-counter location $location'
j2lc__ksh[$cmd_index]='fia_driver_show -c diag-counter -u all -n $fq_nodeid'

# To know the VOQ level drops
((cmd_index++))
j2lc_exec[$cmd_index]='show controllers fia diagshell all tm-ing-congestion location $location'
j2lc__ksh[$cmd_index]='fia_driver_show -c tm-ing-congestion -u all -n $fq_nodeid'

# Check the congested queues
((cmd_index++))
j2lc_exec[$cmd_index]='show controllers fia diagshell all tm-ing-queue-non location $location'
j2lc__ksh[$cmd_index]='fia_driver_show -c tm-ing-queue-non -u all -n $fq_nodeid'

# Check the diag egr to understand the PD and DB usage
((cmd_index++))
j2lc_exec[$cmd_index]='show controllers fia diagshell all tm-egr-congestion location $location'
j2lc__ksh[$cmd_index]='fia_driver_show -c tm-egr-congestion -u all -n $fq_nodeid'

# Check the egress shaper
((cmd_index++))
j2lc_exec[$cmd_index]='show controllers fia diagshell all tm-egr-spr location $location'
j2lc__ksh[$cmd_index]='fia_driver_show -c tm-egr-spr -u all -n $fq_nodeid'

# This will provide profile index for a given TM_PORT
((cmd_index++))
j2lc_exec[$cmd_index]='show controllers fia diagshell all dump-ERPP_PER_PORT_TABLE location $location'
j2lc__ksh[$cmd_index]='fia_driver_show -c dump-ERPP_PER_PORT_TABLE -u all -n $fq_nodeid'

# This will provide the content of the each profile
((cmd_index++))
j2lc_exec[$cmd_index]='show controllers fia diagshell all dump-PQP_TC_DP_MAP location $location'
j2lc__ksh[$cmd_index]='fia_driver_show -c dump-PQP_TC_DP_MAP -u all -n $fq_nodeid'

((cmd_index++))
#End
j2lc__ksh[$cmd_index]=''

#############################################################
# Commands that run on J2C LCs
cmd_index=1

# This will provide profile index for a given TM_PORT
j2clc_exec[$cmd_index]='show controllers fia diagshell all dump-ERPP_PER_PP_PORT_TABLE location $location'
j2clc__ksh[$cmd_index]='fia_driver_show -c dump-ERPP_PER_PP_PORT_TABLE -u all -n $fq_nodeid'

# This will provide the content of the each profile
((cmd_index++))
j2clc_exec[$cmd_index]='show controllers fia diagshell all dump-PQP_TC_DP_MAP location $location'
j2clc__ksh[$cmd_index]='fia_driver_show -c dump-PQP_TC_DP_MAP -u all -n $fq_nodeid'

((cmd_index++))
#End
j2clc__ksh[$cmd_index]=''

#############################################################
# Commands that run on all RPs
# To know the VOQ level drops

cmd_index=1

#FIXME: This command need to be executed from RP for all LC locations which is currently not working
# this command can not be executed on LC directly.
rp_exec[$cmd_index]='show controllers npu stats voq ingress interface all instance all location $location'
rp__ksh[$cmd_index]='ofa_npu_stats_show -v a -i 0x10 -n $fq_nodeid -t n -p A -s 0x0 -d 0x0'

((cmd_index++))
#End
rp__ksh[$cmd_index]=''

#############################################################
# Commands that run on all SCs
sc__ksh[0]=''

#############################################################
# Commands that run on all SPs
sp__ksh[0]=''

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

bcm_trace() {
    ext_node_name=$(node_list_generation -f MY)
    int_node_name=$(node_conversion -N "$ext_node_name")
    file_bcm_tech="/bcm_trace_${int_node_name}.tech"
    echo "+++++ show controllers fia trace brcm-error location $ext_node_name +++++" >> \
         $__tar_file_directory_on_node$file_bcm_tech
    $(eval "echo 'fia_show_ltrace -W -i $fq_nodeid'") >> \
         $__tar_file_directory_on_node$file_bcm_tech
}


display() {
    print_main_heading "show tech-support tm"

    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys_show
        exec_commands sys_trace
    else
        case "$__cardtype" in
        "RP")
            exec_commands rplc_show
            exec_commands rp
            ;;
        "DRP")
            exec_commands rplc_show
            exec_commands rp
            ;;
        "LC")
            exec_commands rplc_show
            exec_commands xrlc
            bcm_trace

            # FIXME: thinxr __cardtpe is getting populated as "RP LC" and LC is being considerd
            if [ "$__arch" == "thinxr" ]; then
                exec_commands rp
            fi

            #Check if NP is J or J+ only
            pid=$(fia_driver_show -c show-unit -u 0 -n "$fq_nodeid" | grep "Unit 0" | awk '{print $4}')
            if [ "$pid" == "BCM88680_A1" ] || [ "$pid" == "BCM88670_A0" ] || [ "$pid" == "BCM88670_B0" ] ||
               [ "$pid" == "BCM88680_A0" ] || [ "$pid" == "BCM88370_A0" ] || [ "$pid" == "BCM88370_B0" ] ||
               [ "$pid" == "BCM88675_B0" ]; then
                exec_commands jlc
            elif [ "$pid" == "BCM88690_A0" ] || [ "$pid" == "BCM88690_B1" ]; then
                exec_commands j2lc
            elif [ "$pid" == "BCM88802_A1" ] || [ "$pid" == "BCM88800_A0" ]; then
                exec_commands j2clc
            fi
            ;;
        esac
    fi

    print_main_heading "show tech-support tm complete"
}

# Run the appropriate function depending on the node specified and if a
# file is specified write the output to that file. We need to redirect
# stderr to stdout when writing to a file because some of the show
# commands output to stderr instead of stdout
. /pkg/bin/show_tech_file_fragment
