Blame bash-completion/tc

Packit Service 3880ab
# tc(8) completion                                         -*- shell-script -*-
Packit Service 3880ab
# Copyright 2016 6WIND S.A.
Packit Service 3880ab
# Copyright 2016 Quentin Monnet <quentin.monnet@6wind.com>
Packit Service 3880ab
Packit Service 3880ab
QDISC_KIND=' choke codel bfifo pfifo pfifo_head_drop fq fq_codel gred hhf \
Packit Service 3880ab
            mqprio multiq netem pfifo_fast pie fq_pie red rr sfb sfq tbf atm \
Packit Service 3880ab
            cbq drr dsmark hfsc htb prio qfq '
Packit Service 3880ab
FILTER_KIND=' basic bpf cgroup flow flower fw route rsvp tcindex u32 matchall '
Packit Service 3880ab
ACTION_KIND=' gact mirred bpf sample '
Packit Service 3880ab
Packit Service 3880ab
# Takes a list of words in argument; each one of them is added to COMPREPLY if
Packit Service 3880ab
# it is not already present on the command line. Returns no value.
Packit Service 3880ab
_tc_once_attr()
Packit Service 3880ab
{
Packit Service 3880ab
    local w subcword found
Packit Service 3880ab
    for w in $*; do
Packit Service 3880ab
        found=0
Packit Service 3880ab
        for (( subcword=3; subcword < ${#words[@]}-1; subcword++ )); do
Packit Service 3880ab
            if [[ $w == ${words[subcword]} ]]; then
Packit Service 3880ab
                found=1
Packit Service 3880ab
                break
Packit Service 3880ab
            fi
Packit Service 3880ab
        done
Packit Service 3880ab
        [[ $found -eq 0 ]] && \
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W "$w" -- "$cur" ) )
Packit Service 3880ab
    done
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
# Takes a list of words in argument; each one of them is added to COMPREPLY if
Packit Service 3880ab
# it is not already present on the command line from the provided index. Returns
Packit Service 3880ab
# no value.
Packit Service 3880ab
_tc_once_attr_from()
Packit Service 3880ab
{
Packit Service 3880ab
    local w subcword found from=$1
Packit Service 3880ab
    shift
Packit Service 3880ab
    for w in $*; do
Packit Service 3880ab
        found=0
Packit Service 3880ab
        for (( subcword=$from; subcword < ${#words[@]}-1; subcword++ )); do
Packit Service 3880ab
            if [[ $w == ${words[subcword]} ]]; then
Packit Service 3880ab
                found=1
Packit Service 3880ab
                break
Packit Service 3880ab
            fi
Packit Service 3880ab
        done
Packit Service 3880ab
        [[ $found -eq 0 ]] && \
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W "$w" -- "$cur" ) )
Packit Service 3880ab
    done
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
# Takes a list of words in argument; adds them all to COMPREPLY if none of them
Packit Service 3880ab
# is already present on the command line. Returns no value.
Packit Service 3880ab
_tc_one_of_list()
Packit Service 3880ab
{
Packit Service 3880ab
    local w subcword
Packit Service 3880ab
    for w in $*; do
Packit Service 3880ab
        for (( subcword=3; subcword < ${#words[@]}-1; subcword++ )); do
Packit Service 3880ab
            [[ $w == ${words[subcword]} ]] && return 1
Packit Service 3880ab
        done
Packit Service 3880ab
    done
Packit Service 3880ab
    COMPREPLY+=( $( compgen -W "$*" -- "$cur" ) )
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
# Takes a list of words in argument; adds them all to COMPREPLY if none of them
Packit Service 3880ab
# is already present on the command line from the provided index. Returns no
Packit Service 3880ab
# value.
Packit Service 3880ab
_tc_one_of_list_from()
Packit Service 3880ab
{
Packit Service 3880ab
    local w subcword from=$1
Packit Service 3880ab
    shift
Packit Service 3880ab
    for w in $*; do
Packit Service 3880ab
        for (( subcword=$from; subcword < ${#words[@]}-1; subcword++ )); do
Packit Service 3880ab
            [[ $w == ${words[subcword]} ]] && return 1
Packit Service 3880ab
        done
Packit Service 3880ab
    done
Packit Service 3880ab
    COMPREPLY+=( $( compgen -W "$*" -- "$cur" ) )
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
# Returns "$cur ${cur}arg1 ${cur}arg2 ..."
Packit Service 3880ab
_tc_expand_units()
Packit Service 3880ab
{
Packit Service 3880ab
    [[ $cur =~ ^[0-9]+ ]] || return 1
Packit Service 3880ab
    local value=${cur%%[^0-9]*}
Packit Service 3880ab
    [[ $cur == $value ]] && echo $cur
Packit Service 3880ab
    echo ${@/#/$value}
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
# Complete based on given word, usually $prev (or possibly the word before),
Packit Service 3880ab
# for when an argument or an option name has but a few possible arguments (so
Packit Service 3880ab
# tc does not take particular commands into account here).
Packit Service 3880ab
# Returns 0 is completion should stop after running this function, 1 otherwise.
Packit Service 3880ab
_tc_direct_complete()
Packit Service 3880ab
{
Packit Service 3880ab
    case $1 in
Packit Service 3880ab
        # Command options
Packit Service 3880ab
        dev)
Packit Service 3880ab
            _available_interfaces
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        classid)
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        estimator)
Packit Service 3880ab
            local list=$( _tc_expand_units 'secs' 'msecs' 'usecs' )
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W "$list" -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        handle)
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        parent|flowid)
Packit Service 3880ab
            local i iface ids cmd
Packit Service 3880ab
            for (( i=3; i < ${#words[@]}-2; i++ )); do
Packit Service 3880ab
                [[ ${words[i]} == dev ]] && iface=${words[i+1]}
Packit Service 3880ab
                break
Packit Service 3880ab
            done
Packit Service 3880ab
            for cmd in qdisc class; do
Packit Service 3880ab
                if [[ -n $iface ]]; then
Packit Service 3880ab
                    ids+=$( tc $cmd show dev $iface 2>/dev/null | \
Packit Service 3880ab
                        cut -d\  -f 3 )" "
Packit Service 3880ab
                else
Packit Service 3880ab
                    ids+=$( tc $cmd show 2>/dev/null | cut -d\  -f 3 )
Packit Service 3880ab
                fi
Packit Service 3880ab
            done
Packit Service 3880ab
            [[ $ids != " " ]] && \
Packit Service 3880ab
                COMPREPLY+=( $( compgen -W "$ids" -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        protocol) # list comes from lib/ll_proto.c
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W ' 802.1Q 802.1ad 802_2 802_3 LLDP aarp \
Packit Service 3880ab
                all aoe arp atalk atmfate atmmpoa ax25 bpq can control cust \
Packit Service 3880ab
                ddcmp dec diag dna_dl dna_rc dna_rt econet ieeepup ieeepupat \
Packit Service 3880ab
                ip ipv4 ipv6 ipx irda lat localtalk loop mobitex ppp_disc \
Packit Service 3880ab
                ppp_mp ppp_ses ppptalk pup pupat rarp sca snap tipc tr_802_2 \
Packit Service 3880ab
                wan_ppp x25' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        prio)
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        stab)
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W 'mtu tsize mpu overhead
Packit Service 3880ab
                linklayer' -- "$cur" ) )
Packit Service 3880ab
            ;;
Packit Service 3880ab
Packit Service 3880ab
        # Qdiscs and classes options
Packit Service 3880ab
        alpha|bands|beta|buckets|corrupt|debug|decrement|default|\
Packit Service 3880ab
        default_index|depth|direct_qlen|divisor|duplicate|ewma|flow_limit|\
Packit Service 3880ab
        flows|hh_limit|increment|indices|linklayer|non_hh_weight|num_tc|\
Packit Service 3880ab
        penalty_burst|penalty_rate|prio|priomap|probability|queues|r2q|\
Packit Service 3880ab
        reorder|vq|vqs)
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        setup)
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W 'vqs' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        hw)
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W '1 0' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        distribution)
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W 'uniform normal pareto
Packit Service 3880ab
                paretonormal' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        loss)
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W 'random state gmodel' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
Packit Service 3880ab
        # Qdiscs and classes options options
Packit Service 3880ab
        gap|gmodel|state)
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
Packit Service 3880ab
        # Filters options
Packit Service 3880ab
        map)
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W 'key' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        hash)
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W 'keys' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        indev)
Packit Service 3880ab
            _available_interfaces
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        eth_type)
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W 'ipv4 ipv6' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        ip_proto)
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W 'tcp udp' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
Packit Service 3880ab
        # Filters options options
Packit Service 3880ab
        key|keys)
Packit Service 3880ab
            [[ ${words[@]} =~ graft ]] && return 1
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W 'src dst proto proto-src proto-dst iif \
Packit Service 3880ab
                priority mark nfct nfct-src nfct-dst nfct-proto-src \
Packit Service 3880ab
                nfct-proto-dst rt-classid sk-uid sk-gid vlan-tag rxhash' -- \
Packit Service 3880ab
                "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
Packit Service 3880ab
        # BPF options - used for filters, actions, and exec
Packit Service 3880ab
        export|bytecode|bytecode-file|object-file)
Packit Service 3880ab
            _filedir
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        object-pinned|graft) # Pinned object is probably under /sys/fs/bpf/
Packit Service 3880ab
            [[ -n "$cur" ]] && _filedir && return 0
Packit Service 3880ab
            COMPREPLY=( $( compgen -G "/sys/fs/bpf/*" -- "$cur" ) ) || _filedir
Packit Service 3880ab
            compopt -o nospace
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        section)
Packit Service 3880ab
            if (type objdump > /dev/null 2>&1) ; then
Packit Service 3880ab
                local fword objfile section_list
Packit Service 3880ab
                for (( fword=3; fword < ${#words[@]}-3; fword++ )); do
Packit Service 3880ab
                    if [[ ${words[fword]} == object-file ]]; then
Packit Service 3880ab
                        objfile=${words[fword+1]}
Packit Service 3880ab
                        break
Packit Service 3880ab
                    fi
Packit Service 3880ab
                done
Packit Service 3880ab
                section_list=$( objdump -h $objfile 2>/dev/null | \
Packit Service 3880ab
                    sed -n 's/^ *[0-9]\+ \([^ ]*\) *.*/\1/p' )
Packit Service 3880ab
                COMPREPLY+=( $( compgen -W "$section_list" -- "$cur" ) )
Packit Service 3880ab
            fi
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        import|run)
Packit Service 3880ab
            _filedir
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        type)
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W 'cls act' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
Packit Service 3880ab
        # Actions options
Packit Service 3880ab
        random)
Packit Service 3880ab
            _tc_one_of_list 'netrand determ'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
Packit Service 3880ab
        # Units for option arguments
Packit Service 3880ab
        bandwidth|maxrate|peakrate|rate)
Packit Service 3880ab
            local list=$( _tc_expand_units 'bit' \
Packit Service 3880ab
                'kbit' 'kibit' 'kbps' 'kibps' \
Packit Service 3880ab
                'mbit' 'mibit' 'mbps' 'mibps' \
Packit Service 3880ab
                'gbit' 'gibit' 'gbps' 'gibps' \
Packit Service 3880ab
                'tbit' 'tibit' 'tbps' 'tibps' )
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W "$list" -- "$cur" ) )
Packit Service 3880ab
            ;;
Packit Service 3880ab
        admit_bytes|avpkt|burst|cell|initial_quantum|limit|max|min|mtu|mpu|\
Packit Service 3880ab
        overhead|quantum|redflowlist)
Packit Service 3880ab
            local list=$( _tc_expand_units \
Packit Service 3880ab
                'b' 'kbit' 'k' 'mbit' 'm' 'gbit' 'g' )
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W "$list" -- "$cur" ) )
Packit Service 3880ab
            ;;
Packit Service 3880ab
        db|delay|evict_timeout|interval|latency|perturb|rehash|reset_timeout|\
Packit Service 3880ab
        target|tupdate)
Packit Service 3880ab
            local list=$( _tc_expand_units 'secs' 'msecs' 'usecs' )
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W "$list" -- "$cur" ) )
Packit Service 3880ab
            ;;
Packit Service 3880ab
    esac
Packit Service 3880ab
    return 1
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
# Complete with options names for qdiscs. Each qdisc has its own set of options
Packit Service 3880ab
# and it seems we cannot really parse it from anywhere, so we add it manually
Packit Service 3880ab
# in this function.
Packit Service 3880ab
# Returns 0 is completion should stop after running this function, 1 otherwise.
Packit Service 3880ab
_tc_qdisc_options()
Packit Service 3880ab
{
Packit Service 3880ab
    case $1 in
Packit Service 3880ab
        choke)
Packit Service 3880ab
            _tc_once_attr 'limit bandwidth ecn min max burst'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        codel)
Packit Service 3880ab
            _tc_once_attr 'limit target interval'
Packit Service 3880ab
            _tc_one_of_list 'ecn noecn'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        bfifo|pfifo|pfifo_head_drop)
Packit Service 3880ab
            _tc_once_attr 'limit'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        fq)
Packit Service 3880ab
            _tc_once_attr 'limit flow_limit quantum initial_quantum maxrate \
Packit Service 3880ab
                buckets'
Packit Service 3880ab
            _tc_one_of_list 'pacing nopacing'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        fq_codel)
Packit Service 3880ab
            _tc_once_attr 'limit flows target interval quantum'
Packit Service 3880ab
            _tc_one_of_list 'ecn noecn'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        gred)
Packit Service 3880ab
            _tc_once_attr 'setup vqs default grio vq prio limit min max avpkt \
Packit Service 3880ab
                burst probability bandwidth ecn harddrop'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        hhf)
Packit Service 3880ab
            _tc_once_attr 'limit quantum hh_limit reset_timeout admit_bytes \
Packit Service 3880ab
                evict_timeout non_hh_weight'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        mqprio)
Packit Service 3880ab
            _tc_once_attr 'num_tc map queues hw'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        netem)
Packit Service 3880ab
            _tc_once_attr 'delay distribution corrupt duplicate loss ecn \
Packit Service 3880ab
                reorder rate'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        pie)
Packit Service 3880ab
            _tc_once_attr 'limit target tupdate alpha beta'
Packit Service 3880ab
            _tc_one_of_list 'bytemode nobytemode'
Packit Service 3880ab
            _tc_one_of_list 'ecn noecn'
Packit Service 3880ab
            _tc_one_of_list 'dq_rate_estimator no_dq_rate_estimator'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        fq_pie)
Packit Service 3880ab
            _tc_once_attr 'limit flows target tupdate \
Packit Service 3880ab
                alpha beta quantum memory_limit ecn_prob'
Packit Service 3880ab
            _tc_one_of_list 'ecn noecn'
Packit Service 3880ab
            _tc_one_of_list 'bytemode nobytemode'
Packit Service 3880ab
            _tc_one_of_list 'dq_rate_estimator no_dq_rate_estimator'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        red)
Packit Service 3880ab
            _tc_once_attr 'limit min max avpkt burst adaptive probability \
Packit Service 3880ab
                bandwidth ecn harddrop'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        rr|prio)
Packit Service 3880ab
            _tc_once_attr 'bands priomap multiqueue'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        sfb)
Packit Service 3880ab
            _tc_once_attr 'rehash db limit max target increment decrement \
Packit Service 3880ab
                penalty_rate penalty_burst'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        sfq)
Packit Service 3880ab
            _tc_once_attr 'limit perturb quantum divisor flows depth headdrop \
Packit Service 3880ab
                redflowlimit min max avpkt burst probability ecn harddrop'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        tbf)
Packit Service 3880ab
            _tc_once_attr 'limit burst rate mtu peakrate latency overhead \
Packit Service 3880ab
                linklayer'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        cbq)
Packit Service 3880ab
            _tc_once_attr 'bandwidth avpkt mpu cell ewma'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        dsmark)
Packit Service 3880ab
            _tc_once_attr 'indices default_index set_tc_index'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        hfsc)
Packit Service 3880ab
            _tc_once_attr 'default'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        htb)
Packit Service 3880ab
            _tc_once_attr 'default r2q direct_qlen debug'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        multiq|pfifo_fast|atm|drr|qfq)
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
    esac
Packit Service 3880ab
    return 1
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
# Complete with options names for BPF filters or actions.
Packit Service 3880ab
# Returns 0 is completion should stop after running this function, 1 otherwise.
Packit Service 3880ab
_tc_bpf_options()
Packit Service 3880ab
{
Packit Service 3880ab
    [[ ${words[${#words[@]}-3]} == object-file ]] && \
Packit Service 3880ab
        _tc_once_attr 'section export'
Packit Service 3880ab
    [[ ${words[${#words[@]}-5]} == object-file ]] && \
Packit Service 3880ab
        [[ ${words[${#words[@]}-3]} =~ (section|export) ]] && \
Packit Service 3880ab
        _tc_once_attr 'section export'
Packit Service 3880ab
    _tc_one_of_list 'bytecode bytecode-file object-file object-pinned'
Packit Service 3880ab
    _tc_once_attr 'verbose index direct-action action classid'
Packit Service 3880ab
    return 0
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
# Complete with options names for filter actions.
Packit Service 3880ab
# This function is recursive, thus allowing multiple actions statement to be
Packit Service 3880ab
# parsed.
Packit Service 3880ab
# Returns 0 is completion should stop after running this function, 1 otherwise.
Packit Service 3880ab
_tc_filter_action_options()
Packit Service 3880ab
{
Packit Service 3880ab
    for ((acwd=$1; acwd < ${#words[@]}-1; acwd++));
Packit Service 3880ab
    do
Packit Service 3880ab
        if [[ action == ${words[acwd]} ]]; then
Packit Service 3880ab
            _tc_filter_action_options $((acwd+1)) && return 0
Packit Service 3880ab
        fi
Packit Service 3880ab
    done
Packit Service 3880ab
Packit Service 3880ab
    local action acwd
Packit Service 3880ab
    for ((acwd=$1; acwd < ${#words[@]}-1; acwd++)); do
Packit Service 3880ab
        if [[ $ACTION_KIND =~ ' '${words[acwd]}' ' ]]; then
Packit Service 3880ab
            _tc_one_of_list_from $acwd action
Packit Service 3880ab
            _tc_action_options $acwd && return 0
Packit Service 3880ab
        fi
Packit Service 3880ab
    done
Packit Service 3880ab
    _tc_one_of_list_from $acwd $ACTION_KIND
Packit Service 3880ab
    return 0
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
# Complete with options names for filters.
Packit Service 3880ab
# Returns 0 is completion should stop after running this function, 1 otherwise.
Packit Service 3880ab
_tc_filter_options()
Packit Service 3880ab
{
Packit Service 3880ab
Packit Service 3880ab
    for ((acwd=$1; acwd < ${#words[@]}-1; acwd++));
Packit Service 3880ab
    do
Packit Service 3880ab
        if [[ action == ${words[acwd]} ]]; then
Packit Service 3880ab
            _tc_filter_action_options $((acwd+1)) && return 0
Packit Service 3880ab
        fi
Packit Service 3880ab
    done
Packit Service 3880ab
Packit Service 3880ab
    filter=${words[$1]}
Packit Service 3880ab
    case $filter in
Packit Service 3880ab
        basic)
Packit Service 3880ab
            _tc_once_attr 'match action classid'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        bpf)
Packit Service 3880ab
            _tc_bpf_options
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        cgroup)
Packit Service 3880ab
            _tc_once_attr 'match action'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        flow)
Packit Service 3880ab
            local i
Packit Service 3880ab
            for (( i=5; i < ${#words[@]}-1; i++ )); do
Packit Service 3880ab
                if [[ ${words[i]} =~ ^keys?$ ]]; then
Packit Service 3880ab
                    _tc_direct_complete 'key'
Packit Service 3880ab
                    COMPREPLY+=( $( compgen -W 'or and xor rshift addend' -- \
Packit Service 3880ab
                        "$cur" ) )
Packit Service 3880ab
                    break
Packit Service 3880ab
                fi
Packit Service 3880ab
            done
Packit Service 3880ab
            _tc_once_attr 'map hash divisor baseclass match action'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        matchall)
Packit Service 3880ab
            _tc_once_attr 'action classid skip_sw skip_hw'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        flower)
Packit Service 3880ab
            _tc_once_attr 'action classid indev dst_mac src_mac eth_type \
Packit Service 3880ab
                ip_proto dst_ip src_ip dst_port src_port'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        fw)
Packit Service 3880ab
            _tc_once_attr 'action classid'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        route)
Packit Service 3880ab
            _tc_one_of_list 'from fromif'
Packit Service 3880ab
            _tc_once_attr 'to classid action'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        rsvp)
Packit Service 3880ab
            _tc_once_attr 'ipproto session sender classid action tunnelid \
Packit Service 3880ab
                tunnel flowlabel spi/ah spi/esp u8 u16 u32'
Packit Service 3880ab
            [[ ${words[${#words[@]}-3]} == tunnel ]] && \
Packit Service 3880ab
                    COMPREPLY+=( $( compgen -W 'skip' -- "$cur" ) )
Packit Service 3880ab
            [[ ${words[${#words[@]}-3]} =~ u(8|16|32) ]] && \
Packit Service 3880ab
                    COMPREPLY+=( $( compgen -W 'mask' -- "$cur" ) )
Packit Service 3880ab
            [[ ${words[${#words[@]}-3]} == mask ]] && \
Packit Service 3880ab
                    COMPREPLY+=( $( compgen -W 'at' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        tcindex)
Packit Service 3880ab
            _tc_once_attr 'hash mask shift classid action'
Packit Service 3880ab
            _tc_one_of_list 'pass_on fall_through'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        u32)
Packit Service 3880ab
            _tc_once_attr 'match link classid action offset ht hashkey sample'
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W 'ip ip6 udp tcp icmp u8 u16 u32 mark \
Packit Service 3880ab
                divisor' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
    esac
Packit Service 3880ab
    return 1
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
# Complete with options names for actions.
Packit Service 3880ab
# Returns 0 is completion should stop after running this function, 1 otherwise.
Packit Service 3880ab
_tc_action_options()
Packit Service 3880ab
{
Packit Service 3880ab
    local from=$1
Packit Service 3880ab
    local action=${words[from]}
Packit Service 3880ab
    case $action in
Packit Service 3880ab
        bpf)
Packit Service 3880ab
            _tc_bpf_options
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        mirred)
Packit Service 3880ab
            _tc_one_of_list_from $from 'ingress egress'
Packit Service 3880ab
            _tc_one_of_list_from $from 'mirror redirect'
Packit Service 3880ab
            _tc_once_attr_from $from 'index dev'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        sample)
Packit Service 3880ab
            _tc_once_attr_from $from 'rate'
Packit Service 3880ab
            _tc_once_attr_from $from 'trunc'
Packit Service 3880ab
            _tc_once_attr_from $from 'group'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        gact)
Packit Service 3880ab
            _tc_one_of_list_from $from 'reclassify drop continue pass'
Packit Service 3880ab
            _tc_once_attr_from $from 'random'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
    esac
Packit Service 3880ab
    return 1
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
# Complete with options names for exec.
Packit Service 3880ab
# Returns 0 is completion should stop after running this function, 1 otherwise.
Packit Service 3880ab
_tc_exec_options()
Packit Service 3880ab
{
Packit Service 3880ab
    case $1 in
Packit Service 3880ab
        import)
Packit Service 3880ab
            [[ ${words[${#words[@]}-3]} == import ]] && \
Packit Service 3880ab
                _tc_once_attr 'run'
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        graft)
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W 'key type' -- "$cur" ) )
Packit Service 3880ab
            [[ ${words[${#words[@]}-3]} == object-file ]] && \
Packit Service 3880ab
                _tc_once_attr 'type'
Packit Service 3880ab
            _tc_bpf_options
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
    esac
Packit Service 3880ab
    return 1
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
# Main completion function
Packit Service 3880ab
# Logic is as follows:
Packit Service 3880ab
#   1. Check if previous word is a global option; if so, propose arguments.
Packit Service 3880ab
#   2. Check if current word is a global option; if so, propose completion.
Packit Service 3880ab
#   3. Check for the presence of a main command (qdisc|class|filter|...). If
Packit Service 3880ab
#      there is one, first call _tc_direct_complete to see if previous word is
Packit Service 3880ab
#      waiting for a particular completion. If so, propose completion and exit.
Packit Service 3880ab
#   4. Extract main command and -- if available -- its subcommand
Packit Service 3880ab
#      (add|delete|show|...).
Packit Service 3880ab
#   5. Propose completion based on main and sub- command in use. Additional
Packit Service 3880ab
#      functions may be called for qdiscs, classes or filter options.
Packit Service 3880ab
_tc()
Packit Service 3880ab
{
Packit Service 3880ab
    local cur prev words cword
Packit Service 3880ab
    _init_completion || return
Packit Service 3880ab
Packit Service 3880ab
    case $prev in
Packit Service 3880ab
        -V|-Version)
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        -b|-batch|-cf|-conf)
Packit Service 3880ab
            _filedir
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        -force)
Packit Service 3880ab
            COMPREPLY=( $( compgen -W '-batch' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        -nm|name)
Packit Service 3880ab
            [[ -r /etc/iproute2/tc_cls ]] || \
Packit Service 3880ab
                COMPREPLY=( $( compgen -W '-conf' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        -n|-net|-netns)
Packit Service 3880ab
            local nslist=$( ip netns list 2>/dev/null )
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W "$nslist" -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        -tshort)
Packit Service 3880ab
            _tc_once_attr '-statistics'
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W 'monitor' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
        -timestamp)
Packit Service 3880ab
            _tc_once_attr '-statistics -tshort'
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W 'monitor' -- "$cur" ) )
Packit Service 3880ab
            return 0
Packit Service 3880ab
            ;;
Packit Service 3880ab
    esac
Packit Service 3880ab
Packit Service 3880ab
    # Search for main commands
Packit Service 3880ab
    local subcword cmd subcmd
Packit Service 3880ab
    for (( subcword=1; subcword < ${#words[@]}-1; subcword++ )); do
Packit Service 3880ab
        [[ ${words[subcword]} == -b?(atch) ]] && return 0
Packit Service 3880ab
        [[ -n $cmd ]] && subcmd=${words[subcword]} && break
Packit Service 3880ab
        [[ ${words[subcword]} != -* && \
Packit Service 3880ab
            ${words[subcword-1]} != -@(n?(et?(ns))|c?(on)f) ]] && \
Packit Service 3880ab
            cmd=${words[subcword]}
Packit Service 3880ab
    done
Packit Service 3880ab
Packit Service 3880ab
    if [[ -z $cmd ]]; then
Packit Service 3880ab
        case $cur in
Packit Service 3880ab
            -*)
Packit Service 3880ab
                local c='-Version -statistics -details -raw -pretty \
Packit Service 3880ab
                    -iec -graphe -batch -name -netns -timestamp'
Packit Service 3880ab
                [[ $cword -eq 1 ]] && c+=' -force'
Packit Service 3880ab
                COMPREPLY=( $( compgen -W "$c" -- "$cur" ) )
Packit Service 3880ab
                return 0
Packit Service 3880ab
                ;;
Packit Service 3880ab
            *)
Packit Service 3880ab
                COMPREPLY=( $( compgen -W "help $( tc help 2>&1 | \
Packit Service 3880ab
                    command sed \
Packit Service 3880ab
                    -e '/OBJECT := /!d' \
Packit Service 3880ab
                    -e 's/.*{//' \
Packit Service 3880ab
                    -e 's/}.*//' \
Packit Service 3880ab
                    -e \ 's/|//g' )" -- "$cur" ) )
Packit Service 3880ab
                return 0
Packit Service 3880ab
                ;;
Packit Service 3880ab
        esac
Packit Service 3880ab
    fi
Packit Service 3880ab
Packit Service 3880ab
    [[ $subcmd == help ]] && return 0
Packit Service 3880ab
Packit Service 3880ab
    # For this set of commands we may create COMPREPLY just by analysing the
Packit Service 3880ab
    # previous word, if it expects for a specific list of options or values.
Packit Service 3880ab
    if [[ $cmd =~ (qdisc|class|filter|action|exec) ]]; then
Packit Service 3880ab
        _tc_direct_complete $prev && return 0
Packit Service 3880ab
        if [[ ${words[${#words[@]}-3]} == estimator ]]; then
Packit Service 3880ab
            local list=$( _tc_expand_units 'secs' 'msecs' 'usecs' )
Packit Service 3880ab
            COMPREPLY+=( $( compgen -W "$list" -- "$cur" ) ) && return 0
Packit Service 3880ab
        fi
Packit Service 3880ab
    fi
Packit Service 3880ab
Packit Service 3880ab
    # Completion depends on main command and subcommand in use.
Packit Service 3880ab
    case $cmd in
Packit Service 3880ab
        qdisc)
Packit Service 3880ab
            case $subcmd in
Packit Service 3880ab
                add|change|replace|link|del|delete)
Packit Service 3880ab
                    if [[ $(($cword-$subcword)) -eq 1 ]]; then
Packit Service 3880ab
                        COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
Packit Service 3880ab
                        return 0
Packit Service 3880ab
                    fi
Packit Service 3880ab
                    local qdisc qdwd
Packit Service 3880ab
                    for ((qdwd=$subcword; qdwd < ${#words[@]}-1; qdwd++)); do
Packit Service 3880ab
                        if [[ $QDISC_KIND =~ ' '${words[qdwd]}' ' ]]; then
Packit Service 3880ab
                            qdisc=${words[qdwd]}
Packit Service 3880ab
                            _tc_qdisc_options $qdisc && return 0
Packit Service 3880ab
                        fi
Packit Service 3880ab
                    done
Packit Service 3880ab
                    _tc_one_of_list $QDISC_KIND
Packit Service 3880ab
                    _tc_one_of_list 'root ingress parent clsact'
Packit Service 3880ab
                    _tc_once_attr 'handle estimator stab'
Packit Service 3880ab
                    ;;
Packit Service 3880ab
                show)
Packit Service 3880ab
                    _tc_once_attr 'dev'
Packit Service 3880ab
                    _tc_one_of_list 'ingress clsact'
Packit Service 3880ab
                    _tc_once_attr '-statistics -details -raw -pretty -iec \
Packit Service 3880ab
                        -graph -name'
Packit Service 3880ab
                    ;;
Packit Service 3880ab
                help)
Packit Service 3880ab
                    return 0
Packit Service 3880ab
                    ;;
Packit Service 3880ab
                *)
Packit Service 3880ab
                    [[ $cword -eq $subcword ]] && \
Packit Service 3880ab
                        COMPREPLY=( $( compgen -W 'help add delete change \
Packit Service 3880ab
                            replace link show' -- "$cur" ) )
Packit Service 3880ab
                    ;;
Packit Service 3880ab
            esac
Packit Service 3880ab
            ;;
Packit Service 3880ab
Packit Service 3880ab
        class)
Packit Service 3880ab
            case $subcmd in
Packit Service 3880ab
                add|change|replace|del|delete)
Packit Service 3880ab
                    if [[ $(($cword-$subcword)) -eq 1 ]]; then
Packit Service 3880ab
                        COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
Packit Service 3880ab
                        return 0
Packit Service 3880ab
                    fi
Packit Service 3880ab
                    local qdisc qdwd
Packit Service 3880ab
                    for ((qdwd=$subcword; qdwd < ${#words[@]}-1; qdwd++)); do
Packit Service 3880ab
                        if [[ $QDISC_KIND =~ ' '${words[qdwd]}' ' ]]; then
Packit Service 3880ab
                            qdisc=${words[qdwd]}
Packit Service 3880ab
                            _tc_qdisc_options $qdisc && return 0
Packit Service 3880ab
                        fi
Packit Service 3880ab
                    done
Packit Service 3880ab
                    _tc_one_of_list $QDISC_KIND
Packit Service 3880ab
                    _tc_one_of_list 'root parent'
Packit Service 3880ab
                    _tc_once_attr 'classid'
Packit Service 3880ab
                    ;;
Packit Service 3880ab
                show)
Packit Service 3880ab
                    _tc_once_attr 'dev'
Packit Service 3880ab
                    _tc_one_of_list 'root parent'
Packit Service 3880ab
                    _tc_once_attr '-statistics -details -raw -pretty -iec \
Packit Service 3880ab
                        -graph -name'
Packit Service 3880ab
                    ;;
Packit Service 3880ab
                help)
Packit Service 3880ab
                    return 0
Packit Service 3880ab
                    ;;
Packit Service 3880ab
                *)
Packit Service 3880ab
                    [[ $cword -eq $subcword ]] && \
Packit Service 3880ab
                        COMPREPLY=( $( compgen -W 'help add delete change \
Packit Service 3880ab
                            replace show' -- "$cur" ) )
Packit Service 3880ab
                    ;;
Packit Service 3880ab
            esac
Packit Service 3880ab
            ;;
Packit Service 3880ab
Packit Service 3880ab
        filter)
Packit Service 3880ab
            case $subcmd in
Packit Service 3880ab
                add|change|replace|del|delete)
Packit Service 3880ab
                    if [[ $(($cword-$subcword)) -eq 1 ]]; then
Packit Service 3880ab
                        COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
Packit Service 3880ab
                        return 0
Packit Service 3880ab
                    fi
Packit Service 3880ab
                    local filter fltwd
Packit Service 3880ab
                    for ((fltwd=$subcword; fltwd < ${#words[@]}-1; fltwd++));
Packit Service 3880ab
                    do
Packit Service 3880ab
                        if [[ $FILTER_KIND =~ ' '${words[fltwd]}' ' ]]; then
Packit Service 3880ab
                            _tc_filter_options $fltwd && return 0
Packit Service 3880ab
                        fi
Packit Service 3880ab
                    done
Packit Service 3880ab
                    _tc_one_of_list $FILTER_KIND
Packit Service 3880ab
                    _tc_one_of_list 'root ingress egress parent'
Packit Service 3880ab
                    _tc_once_attr 'handle estimator pref protocol'
Packit Service 3880ab
                    ;;
Packit Service 3880ab
                show)
Packit Service 3880ab
                    _tc_once_attr 'dev'
Packit Service 3880ab
                    _tc_one_of_list 'root ingress egress parent'
Packit Service 3880ab
                    _tc_once_attr '-statistics -details -raw -pretty -iec \
Packit Service 3880ab
                        -graph -name'
Packit Service 3880ab
                    ;;
Packit Service 3880ab
                help)
Packit Service 3880ab
                    return 0
Packit Service 3880ab
                    ;;
Packit Service 3880ab
                *)
Packit Service 3880ab
                    [[ $cword -eq $subcword ]] && \
Packit Service 3880ab
                        COMPREPLY=( $( compgen -W 'help add delete change \
Packit Service 3880ab
                            replace show' -- "$cur" ) )
Packit Service 3880ab
                    ;;
Packit Service 3880ab
            esac
Packit Service 3880ab
            ;;
Packit Service 3880ab
Packit Service 3880ab
        action)
Packit Service 3880ab
            case $subcmd in
Packit Service 3880ab
                add|change|replace)
Packit Service 3880ab
                    local action acwd
Packit Service 3880ab
                    for ((acwd=$subcword; acwd < ${#words[@]}-1; acwd++)); do
Packit Service 3880ab
                        if [[ $ACTION_KIND =~ ' '${words[acwd]}' ' ]]; then
Packit Service 3880ab
                            _tc_action_options $acwd && return 0
Packit Service 3880ab
                        fi
Packit Service 3880ab
                    done
Packit Service 3880ab
                    _tc_one_of_list $ACTION_KIND
Packit Service 3880ab
                    ;;
Packit Service 3880ab
                get|del|delete)
Packit Service 3880ab
                    _tc_once_attr 'index'
Packit Service 3880ab
                    ;;
Packit Service 3880ab
                lst|list|flush|show)
Packit Service 3880ab
                    _tc_one_of_list $ACTION_KIND
Packit Service 3880ab
                    ;;
Packit Service 3880ab
                *)
Packit Service 3880ab
                    [[ $cword -eq $subcword ]] && \
Packit Service 3880ab
                        COMPREPLY=( $( compgen -W 'help add delete change \
Packit Service 3880ab
                            replace show list flush action' -- "$cur" ) )
Packit Service 3880ab
                    ;;
Packit Service 3880ab
            esac
Packit Service 3880ab
            ;;
Packit Service 3880ab
Packit Service 3880ab
        monitor)
Packit Service 3880ab
            COMPREPLY=( $( compgen -W 'help' -- "$cur" ) )
Packit Service 3880ab
            ;;
Packit Service 3880ab
Packit Service 3880ab
        exec)
Packit Service 3880ab
            case $subcmd in
Packit Service 3880ab
                bpf)
Packit Service 3880ab
                    local excmd exwd EXEC_KIND=' import debug graft '
Packit Service 3880ab
                    for ((exwd=$subcword; exwd < ${#words[@]}-1; exwd++)); do
Packit Service 3880ab
                        if [[ $EXEC_KIND =~ ' '${words[exwd]}' ' ]]; then
Packit Service 3880ab
                            excmd=${words[exwd]}
Packit Service 3880ab
                            _tc_exec_options $excmd && return 0
Packit Service 3880ab
                        fi
Packit Service 3880ab
                    done
Packit Service 3880ab
                    _tc_one_of_list $EXEC_KIND
Packit Service 3880ab
                    ;;
Packit Service 3880ab
                *)
Packit Service 3880ab
                    [[ $cword -eq $subcword ]] && \
Packit Service 3880ab
                        COMPREPLY=( $( compgen -W 'bpf' -- "$cur" ) )
Packit Service 3880ab
                    ;;
Packit Service 3880ab
            esac
Packit Service 3880ab
            ;;
Packit Service 3880ab
    esac
Packit Service 3880ab
} &&
Packit Service 3880ab
complete -F _tc tc
Packit Service 3880ab
Packit Service 3880ab
# ex: ts=4 sw=4 et filetype=sh