#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_ether_platform - Shared show tech-support script for ethernet
# Cisco_user: Ravi Kant(rakant)
# Copyright (c) 2019-2021 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------

# Load the script fragment containing the code for the following functions
# - print_main_heading
# - print_command_heading
# - run_single_command
# - run_commands
# - run_single_command_on_all_nodes
# - run_commands_on_all_nodes
# - default_parser_function
. /pkg/bin/show_tech_main_fragment
. /pkg/bin/show_tech_pmdb_util

# Parse the arguments to the script.
__cardtype="unspecified"

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

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

if [ "$__cardtype" == "unspecified" ]; then
    __cardtype=`node_type`
fi
     
################################################################################
# List each set of show commands to be run. Each set must finish with an empty
# string. Note that it is important to use single quotes rather than double
# quotes for the strings containing your commands.
###############################################################################

#############################################################
# Show commands that run once per system                    #
#############################################################

cmd_index=1
sys_exec[$cmd_index]='show context'
sys__ksh[$cmd_index]='corehelper_context -c 0x1 -n all'
((cmd_index++))

sys_exec[$cmd_index]='show redundancy'
sys__ksh[$cmd_index]='redcon_show'
((cmd_index++))

sys_exec[$cmd_index]='show issu'
sys__ksh[$cmd_index]='issu_show_status -I'
((cmd_index++))

sys_exec[$cmd_index]='show inventory all'
sys__ksh[$cmd_index]='show_inventory -e -t'
((cmd_index++))

sys_exec[$cmd_index]='show platform vm'
sys__ksh[$cmd_index]='show_platform_sysdb -v'
((cmd_index++))

sys_exec[$cmd_index]='show process blocked location all'
sys__ksh[$cmd_index]='sh_proc_ng_blocked -l all'
((cmd_index++))

sys_exec[$cmd_index]='show im status'
sys__ksh[$cmd_index]='im_show status'
((cmd_index++))

sys_exec[$cmd_index]=''
sys__ksh[$cmd_index]=''

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

cmd_index=1
rplc_exec[$cmd_index]='show im trace'
rplc__ksh[$cmd_index]='im_show_trace'
((cmd_index++))

rplc_exec[$cmd_index]='show ethernet v-ether trace location $location'
rplc__ksh[$cmd_index]='eth_intf_show_trace -i $fq_nodeid'
((cmd_index++))

rplc_exec[$cmd_index]='show ethernet infra trace location $location'
rplc__ksh[$cmd_index]='ether_show_trace -i $fq_nodeid'
((cmd_index++))

rplc_exec[$cmd_index]='show spp trace platform common all location $location'
rplc__ksh[$cmd_index]='spp_fretta_show_ltrace -i $fq_nodeid -A'
((cmd_index++))

rplc_exec[$cmd_index]='show spp node-counters location $location'
rplc__ksh[$cmd_index]='spp_sysdb_get -L $fq_nodeid node_counters'
((cmd_index++))

rplc_exec[$cmd_index]='show spp trace platform socket all location $location'
rplc__ksh[$cmd_index]='spp_socket_show_ltrace -i $fq_nodeid -A'
((cmd_index++))

rplc_exec[$cmd_index]='show ethernet driver trace location $location'
rplc__ksh[$cmd_index]='ethernet_trace_show -i $fq_nodeid'
((cmd_index++))

rplc_exec[$cmd_index]='show fwdlib trace all '
rplc__ksh[$cmd_index]='fwdlib_show_ltrace -A'
((cmd_index++))

rplc_exec[$cmd_index]='show optics trace all '
rplc__ksh[$cmd_index]='optics_show_ltrace -A'
((cmd_index++))

rplc_exec[$cmd_index]='show eapd optics trace all '
rplc__ksh[$cmd_index]='optics_eapd_show_ltrace -A '
((cmd_index++))

rplc_exec[$cmd_index]='show dpfpga trace all '
rplc__ksh[$cmd_index]='dpfpga_show_ltrace -E -V '
((cmd_index++))

rplc_exec[$cmd_index]=''
rplc__ksh[$cmd_index]=''

#############################################################
# Commands that run on RP nodes                             #
#############################################################
cmd_index=1
rp_exec[$cmd_index]='show portmode trace all'
rp__ksh[$cmd_index]='portmode_show_ltrace -A'
((cmd_index++))

rp_exec[$cmd_index]=''
rp__ksh[$cmd_index]=''

#############################################################
# Commands that run on LC nodes                             #
#############################################################

cmd_index=1
lc_exec[$cmd_index]='show qos ea trace all location $location'
lc__ksh[$cmd_index]='qos_ea_show_ltrace -i $fq_nodeid -A'
((cmd_index++))

lc_exec[$cmd_index]='show pidb trace location $location'
lc__ksh[$cmd_index]='dpa_pidb_show_ltrace -i $fq_nodeid -T 0x0'
((cmd_index++))

lc_exec[$cmd_index]='show pidb error location $location'
lc__ksh[$cmd_index]='dpa_pidb_show_ltrace -i $fq_nodeid -T 0x1'
((cmd_index++))

lc_exec[$cmd_index]='show ether-ea stats location $location'
lc__ksh[$cmd_index]='dpa_ether_ea_interface_debug_show -o s -n $fq_nodeid -i'
((cmd_index++))

lc_exec[$cmd_index]='show ether-ea stats location $location'
lc__ksh[$cmd_index]='dnx_ether_ea_interface_debug_show -o s -n $fq_nodeid -i'
((cmd_index++))

lc_exec[$cmd_index]='show bundle load-balancing detail location $location $fq_nodeid'
lc__ksh[$cmd_index]='bundlemgr_adj_show -d -h $fq_nodeid'
((cmd_index++))

lc_exec[$cmd_index]=''
lc__ksh[$cmd_index]=''

###############################################################################
# Run this after all other commands, to leave a time stamp after the last     #
# command we run - makes analysis easier!                                     #
###############################################################################
final_exec[1]='Last show tech ethernet command'
final__ksh[1]='echo Last show tech ethernet command'

final_exec[2]=''
final__ksh[2]=''
##################################################################
# Run show tech commands
##################################################################


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})
}

add_xl_mac() {

    ((cmd_index--))
    lc_exec[$cmd_index]='show controllers fia diagshell all "g XLMAC_CTRL" location $location'
    lc__ksh[$cmd_index]='fia_driver_show -c  g-XLMAC_CTRL -u all -n $fq_nodeid'
    ((cmd_index++))

    lc_exec[$cmd_index]='show controllers fia diagshell all "g CLMAC_CTRL" location $location'
    lc__ksh[$cmd_index]='fia_driver_show -c  g-CLMAC_CTRL -u all -n $fq_nodeid'
    ((cmd_index++))

    lc_exec[$cmd_index]='show controllers fia diagshell all "g CDMAC_CTRL" location $location'
    lc__ksh[$cmd_index]='fia_driver_show -c  g-CDMAC_CTRL -u all -n $fq_nodeid'
    ((cmd_index++))

    lc_exec[$cmd_index]=''
    lc__ksh[$cmd_index]=''
}


show_phy_diag_dsc_config(){
    outfile=$1/phy_diag
    echo "----------------------------------------" >> $outfile
    echo "--- show phy diag xe<port> dsc config: start -----" >> $outfile
    echo "----------------------------------------" >> $outfile
    fia_driver_show -c ps -u all -n $fq_nodeid | while read -r x;
        do
            if [[ $x == *"R/S/I"* ]]; then
                inst=$(echo $x | awk -F'[:]' '{print $2}' | awk -F'[/]' '{print $3}')
                slot=$(echo $x | awk -F'[:]' '{print $2}' | awk -F'[/]' '{print $2}')
            elif [[ $x == *"Node"* || $x == *"speed"* || $x == *"duplex"* || -z "$x" ]]; then
                continue
            else 
                port=$(echo $x | awk -F'[(]' '{print $1}')
                echo "show ps : port: ${port} instance: ${inst} slot: ${slot}" >> $outfile
                echo "---------------------------------" >> $outfile
                fia_driver_show -c "phy diag ${port} dsc config" -u 0x$inst -n $fq_nodeid >> $outfile
                echo "=================================" >> $outfile
            fi
        done
    echo "--------------------------------------" >> $outfile
    echo "--- show phy diag xe<port> dsc config: end -----" >> $outfile
    echo "--------------------------------------" >> $outfile

}

display() {

    print_main_heading "show tech-support ethernet platform"

    # Get the card_type: SYS|RP|DRP|LC.
    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
    else
        case "$__cardtype" in
        "RP")
            exec_commands rplc
            exec_commands rp
            display_pmdb

            ;;
        "LC")
            get_brcm_npu_type
            if [ ${num_j2} -eq 0 ]; then
                add_xl_mac
            fi
            exec_commands rplc
            exec_commands lc
            display_pmdb
            if [ ${num_j2} -eq 0 ]; then
                node_name=`uname -n`
                node_subdir=$__tar_file_directory_on_node/$node_name
                if [ ! -d "$node_subdir" ]; then
                    /bin/mkdir $node_subdir
                fi
                show_phy_diag_dsc_config $node_subdir
            fi
             
            ;;
        esac
    fi

    # Run the common modules
    arg_string="$arg_string -t $__cardtype"
    if [[ -x `which show_tech_fast_ether_pd 2>/dev/null` ]]; then
        run_module show_tech_fast_ether_pd "$arg_string"
    fi

    exec_commands final

    print_main_heading "show tech-support ethernet platform complete"
}

# This function calls the display() function and sends the output to file if
# the file option has been set.
. /pkg/bin/show_tech_file_fragment
