#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_slowpath - Runs show commands for the CLI:
#                           show tech-support slowpath
#
# Copyright (c) 2021 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------

#
# Include the standard show tech-support functions
#
. /pkg/bin/show_tech_main_fragment

__cardtype="unspecified"
arg_string=""


# Set the default values for show tech-support
# Number of iterations the commands needs to be captured
iterations=1
# Time duration in secs between iterations
duration=10

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

if [ "$__filename" = "unspecified" ]; then
    echo "show tech-support slowpath: output file not specified"
    exit
fi

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

#
# Rebuild the arg_string.  Note that iterations and duration aren't
# included as the arg string is passed within the context of an iteration
# rather than to perform the iterations within the command
#
arg_string="$arg_string -t $__cardtype"

#
# Specify the commands to run.
#

# Commands to be run once in total (ie. not per iteration)
sys_once_exec[1]='show version'
#separating NG $ Non-NG XR commands
if [[ "$platform" = "panini" ]]; then
    sys_once__ksh[1]='ng_show_version'
else
    sys_once__ksh[1]='show_version'
fi

sys_once_exec[2]='show logging'
sys_once__ksh[2]='show_logging'

sys_once_exec[3]=''
sys_once__ksh[3]=''

nodes_once_exec[1]=''
nodes_once__ksh[1]=''

# Commands to be run once per iteration
sys_exec[1]='show processes aborts'
sys__ksh[1]='sysmgr_show -l aborts'

sys_exec[2]='show context all'
sys_exec[3]='show processes memory'
sys_exec[4]='show memory summary'

#separating NG $ Non-NG XR commands
if [[ "$platform" = "panini" ]]; then
    sys__ksh[2]='corehelper_context -c 0x1 -n all'
    sys__ksh[3]='sh_proc_memory'
    sys__ksh[4]='show_memory_ng -s'
else
    sys__ksh[2]='dumper_context -c 0xb'
    sys__ksh[3]='show_processes -X -m'
    sys__ksh[4]='show_memory -X -s'
fi

sys_exec[5]='show spp buffer detail location all'
sys__ksh[5]='spp_sysdb_get -M buffer_analysis_detail'

sys_exec[6]=''
sys__ksh[6]=''
# Commands to be run for each node per iteration
nodes_exec[1]='show processes blocked location $location'
if [[ "$platform" = "panini" ]]; then
    nodes__ksh[1]='sh_proc_ng_blocked -l $fq_nodeid'
else
    nodes__ksh[1]='show_processes -b -n $fq_nodeid'
fi

cmd_index=2

nodes_exec[$cmd_index]='show spp buffer location $location'
nodes__ksh[$cmd_index]='spp_sysdb_get -L $fq_nodeid buffer_analysis'
((cmd_index++))

nodes_exec[$cmd_index]='sh fwd statistics all detail location $location'
nodes__ksh[$cmd_index]='fwd_show_stats -i $fq_nodeid  -a -d'
((cmd_index++))

nodes_exec[$cmd_index]='sh netio drops location $location'
nodes__ksh[$cmd_index]='netio_show -i $fq_nodeid  -e'
((cmd_index++))

nodes_exec[$cmd_index]='show netio idb finT location $location'
nodes__ksh[$cmd_index]='netio_show -i $fq_nodeid  -I -F'
((cmd_index++))

nodes_exec[$cmd_index]='show netio clients location $location'
nodes__ksh[$cmd_index]='netio_show -i $fq_nodeid  -L'
((cmd_index++))

nodes_exec[$cmd_index]='show netio trace all location $location'
nodes__ksh[$cmd_index]='netio_show_ltrace -i $fq_nodeid  -A'
((cmd_index++))

nodes_exec[$cmd_index]='sh fwd trace all location $location'
nodes__ksh[$cmd_index]='fwd_show_ltrace -i $fq_nodeid  -A'
((cmd_index++))

nodes_exec[$cmd_index]='sh captured packets ingress detail hexdump location $location'
nodes__ksh[$cmd_index]='pak_capture_show_command -i $fq_nodeid -I -H' 
((cmd_index++))

nodes_exec[$cmd_index0]='show ip traffic' 
nodes__ksh[$cmd_index0]='show_ip_traffic'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace cfm rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -C -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace cfm slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -C -S' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace cfm txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -C -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace eapol rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -M -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace eapol  slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -M -S' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace eapol txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -M -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace eapol-dot1x rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -Y -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace eapol-dot1x slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -Y -S' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace eapol-dot1x txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -Y -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace elmi rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -F -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace elmi slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -F -S' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace elmi txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -F -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace elo rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -E -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace elo slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -E -S' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace elo txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -E -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace erp rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -B -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace erp slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -B -S'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace erp txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -B -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace exr rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -X -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace exr slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -X-S?
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace exr txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -X -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace lacp rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -L -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace lacp slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -L-S?
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace lacp txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -L -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace lldp rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -D -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace lldp slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -D -S'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace lldp txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -D -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace macsec rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -A -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace macsec slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -A -S'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace macsec txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -A -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace mvrp rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -V -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace mvrp slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -V -S'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace mvrp txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -V -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace ptp rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -P -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace ptp slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -P -S'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace ptp txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -P -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace pvst rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -J -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace pvst slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -J -S'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace pvst txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -J -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace sdacp rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -I -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace sdacp slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -I -S'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace sdacp txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -I -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace ssfp rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -G -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace ssfp slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -G -S'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace ssfp txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -G -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace stp rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -K -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace stp slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -K -S'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace stp txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -K -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace stppb rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -Q -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace stppb slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -Q -S'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace stppb txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -Q -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace synce rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -N -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace synce slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -N -S'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace synce txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -N -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace udld rxpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -U -R' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace udld slow location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -U -S'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform trace udld txpkt location $location'
nodes__ksh[$cmd_index]='spio_pd_show_ltrace -i $fq_nodeid -U -T' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform ea trace location $location'
nodes__ksh[$cmd_index]='spio_pd_ea_show_ltrace  -i $fq_nodeid' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform hw-offload trace location $location'
nodes__ksh[$cmd_index]='cfm_hwoff_pd_show_ltrace  -i $fq_nodeid' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform offload lookup trace location $location'
nodes__ksh[$cmd_index]='sw_offload_pd_show_ltrace  -i $fq_nodeid -b 1'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform offload client-lib packet trace location $location'
nodes__ksh[$cmd_index]='sw_offload_pd_show_ltrace  -i $fq_nodeid -b 2'
((cmd_index++))

nodes_exec[$cmd_index]='sh spio platform offload client-lib trace location $location'
nodes__ksh[$cmd_index]='sw_offload_pd_show_ltrace  -i $fq_nodeid -b 0' 
((cmd_index++))

nodes_exec[$cmd_index]='sh spp  trace platform common all location $location'
nodes__ksh[$cmd_index]='spp_fretta_show_ltrace  -i $fq_nodeid -A'
((cmd_index++))

nodes_exec[$cmd_index]='sh spp  trace platform socket all location $location'
nodes__ksh[$cmd_index]='spp_fretta_show_ltrace  -i $fq_nodeid -B'
((cmd_index++))

nodes_exec[$cmd_index]='sh lpts trace platf all location $location'
nodes__ksh[$cmd_index]='show_platform_pifib_trace  -i $fq_nodeid -A'
((cmd_index++))

nodes_exec[$cmd_index]='sh lpts punt statistics brief location $location'
nodes__ksh[$cmd_index]='show_lpts_pifib  -i $fq_nodeid -z 0xc -a 0x1'
((cmd_index++))

nodes_exec[$cmd_index]='sh netio trace all location $location'
nodes__ksh[$cmd_index]='netio_show_ltrace  -i $fq_nodeid -A'
((cmd_index++))


nodes__ksh[$cmd_index]=''


#
# Specify the display() function that is run by the tech-support script
#
display() {
    local iter_num=0

    print_main_heading "show tech-support slowpath for location: $location $iterations iterations with $duration secs"

    #
    # Run commands that are run only once for the entire command.
    #
    print_heading "show tech-support slowpath for location: $location once-only commands"
    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys_once
    else
        exec_commands nodes_once
    fi

    #
    # Run through the iterations.
    #
    saved_location=$location;

    while [ $iter_num -lt $iterations ]; do
        # Print iteration heading
        ((++iter_num))
        print_heading "show tech-support slowpath for $nodetype $location (iteration $iter_num)"

        if [ "$__cardtype" == "SYS" ]; then
            exec_commands sys
        else
            exec_commands nodes
        fi

        #
        # Modify the iteration count and sleep if necessary.
        #
        if [ $iter_num -lt $iterations ]; then
            sleep $duration
        fi

        #
        # Reset the location value.
        #
        location=$saved_location;
    done

    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
    else
        exec_commands nodes
    fi

    print_main_heading "show tech-support slowpath complete"
    enable_techs ""  /pkg/bin/show_tech_spio
    enable_techs ""  /pkg/bin/show_tech_lpts
}


#
# Use the standard show tech-support infra to call our display function and
# send the output to console or file
#
. /pkg/bin/show_tech_file_fragment

