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94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/bin/bash
#
# Copyright (C) 2020 HabanaLabs, Ltd.
# All Rights Reserved.
#
# Unauthorized copying of this file, via any medium is strictly prohibited.
# Proprietary and confidential.
#

readonly HABANA_DRIVER_NAME="habanalabs"
readonly HABANA_PCI_ID="0x1da3"

usage()
{
    echo -e "\nusage: $(basename $1) [options]\n"

    echo -e "options:\n"
    echo -e "       --up         toggle up all interfaces"
    echo -e "       --down       toggle down all interfaces"
    echo -e "       --status     print status of all interfaces"
    echo -e "       --set-pfc    set PFC (enabled=0,1,2,3)"
    echo -e "       --unset-pfc  unset PFC (enabled=none)"
    echo -e "       --check-pfc  dump PFC configuration"
    echo -e "       --no-progbar do not show progress bar"
    echo -e "       --l3         enable support for L3 configuration"
    echo -e "  -v,  --verbose    print more logs"
    echo -e "  -h,  --help       print this help"
}

habana_net_list=""
device_num=0
verbose=false
print_module_id=false
up=0
down=0
status=0
set_pfc=0
unset_pfc=0
check_pfc=0
l3=0
is_prog_bar=true
op=""
my_sudo=""
if [ $EUID -ne 0 ]; then
    my_sudo="sudo"
fi

show_prog_bar ()
{
    local progress
    local done
    local left

    let progress=(${1}*100/${2}*100)/100
    let done=(${progress}*4)/10
    let left=40-$done

    done=$(printf "%${done}s")
    left=$(printf "%${left}s")

    printf "\r$3: [${done// /\#}${left// /-}] ${progress}%%"
}

toggle_links()
{
    local i
    local sum

    sum=$(wc -w <<< "$habana_net_list")
    if [ $is_prog_bar = true ]; then
        show_prog_bar 0 ${sum} $1
    fi

    i=0

    for net_if in $habana_net_list; do
        $my_sudo ip link set $net_if $1
        if [ $? -ne 0 ]; then
            echo "Failed to toggle I/F '$net_if'"
            exit 1
        fi
        let i++
        if [ $is_prog_bar = true ]; then
            show_prog_bar ${i} ${sum} $1
        fi
    done

    echo ""
}

build_net_ifs_global_list()
{
    local net_if
    local if_info
    local driver_name

    for net_if in /sys/class/net/*/ ; do
        net_if=$(basename $net_if)

        # ignore loopback and virtual ethernet devices
        if [ $net_if == "lo" ] || [ `echo $net_if | cut -c1-4` == "veth" ]; then
            continue
        fi

        # consider habanalabs NICs only
        if [ -d /sys/class/net/$net_if/device/ ] && [ -f /sys/class/net/$net_if/device/vendor ]; then
            if [ $(cat /sys/class/net/$net_if/device/vendor) != $HABANA_PCI_ID ]; then
                continue
            fi
        else
            # ignore characters including and after '@' in interface name
            net_if=`echo "$net_if" | cut -d'@' -f1`

            # ignore NICs which aren't managed by KMD
            if_info=`$my_sudo ethtool -i $net_if`
            if [ $? -ne 0 ]; then
                echo "Failed to acquire information for the network interface '$net_if'"
                continue
            fi

            driver_name=`echo "$if_info" | grep 'driver' | awk '{print $2}'`
            if [[ $driver_name != $HABANA_DRIVER_NAME* ]]; then
                continue
            fi
        fi

        habana_net_list="$habana_net_list $net_if"
    done

    if [ -z "$habana_net_list" ]; then
        echo "Warning: no $HABANA_DRIVER_NAME network interfaces were detected"
        exit 1
    fi
}

check_flags ()
{
    local sum

    if [ $status -gt 1 ] || [ $up -gt 1 ] || [ $down -gt 1 ] || [ $set_pfc -gt 1 ] || [ $unset_pfc -gt 1 ] || [ $check_pfc -gt 1 ]; then
        echo "each flag should be used once"
        usage $0
        exit 1
    fi

    let "sum=$up + $down + $set_pfc + $unset_pfc + $check_pfc"
    if [ $status -ne 0 ] && [ $sum -ne 0 ]; then
        echo "status flag can't be combined with other flags"
        usage $0
        exit 1
    fi

    let "sum=$up + $down"
    if [ $sum -gt 1 ]; then
        echo "up and down flags can't be combined together"
        usage $0
        exit 1
    fi

    let "sum=$set_pfc + $unset_pfc + $check_pfc"
    if [ $down -ne 0 ] && [ $sum -ne 0 ]; then
        echo "down flag can't be combined with other flags"
        usage $0
        exit 1
    fi

    if [ $up -ne 0 ] && [ $sum -ne 0 ]; then
        echo "up flag can't be combined with other flags"
        usage $0
        exit 1
    fi

    let "sum=$set_pfc + $unset_pfc + $check_pfc"
    if [ $sum -gt 1 ]; then
        echo "PFC flags can't be combined together"
        usage $0
        exit 1
    fi
}

while [ -n "$1" ];
do
    case $1 in
    -h  | --help )
        usage $0
        exit 0
        ;;
    --up )
        let up++
        ;;
    --down )
        let down++
        ;;
    --status )
        let status++
        ;;
    --set-pfc )
        let set_pfc++
        ;;
    --unset-pfc )
        let unset_pfc++
        ;;
    --check-pfc )
        let check_pfc++
        ;;
    --no-progbar )
        is_prog_bar=false
        ;;
    --l3 )
        let l3++
        ;;
    -v  | --verbose )
        verbose=true
        ;;
    *)
        echo "bad argument '$1'"
        usage $0
        exit 1
        ;;
    esac
    shift
done

check_flags

if [ $status -eq 1 ]; then
    if [ ! -d /sys/class/habanalabs/ ]; then
        echo "no available habanalabs directory"
        exit 1
    fi

    device_num=$(ls /sys/class/habanalabs/ | grep -E "hl[0-9]+" | wc -l)
    if [ $device_num -eq 0 ]; then
        echo "no available habanalabs devices were found"
        exit 1
    fi

    if [ -v HLTHUNK_RELEASE_BUILD ] && [ -f $HLTHUNK_RELEASE_BUILD/bin/control_device ]; then
        print_module_id=true
    fi

    for (( i=0; i<$device_num; i++ )); do
        dev_name="hl$i"
        if [ ! -d /sys/class/habanalabs/$dev_name/ ]; then
            echo "$dev_name doesn't exist"
            continue
        fi

        if [ "$(cat /sys/class/habanalabs/$dev_name/status)" != "Operational" ]; then
            echo "$dev_name is not operational"
            continue
        fi

        pci_addr=$(cat /sys/class/habanalabs/$dev_name/pci_addr)
        if [ $print_module_id = true ]; then
            module_id=$($HLTHUNK_RELEASE_BUILD/bin/control_device -s test_print_hw_ip_info \
                -p $pci_addr -d 2> /dev/null | grep "Module ID" | cut -d ":" -f 2 | cut -c 2)
        fi
        dev_ifs=""
        dev_ifs_up=""
        dev_ifs_down=""
        dev_num_up=0
        dev_num_down=0

        if [ -d /sys/bus/pci/devices/$pci_addr/net/ ]; then
            for dev_if in /sys/bus/pci/devices/$pci_addr/net/*/; do
                dev_ifs="$dev_ifs $dev_if"
            done
        else
            for net_if in /sys/class/net/*/; do
                net_if=$(basename $net_if)

                # ignore loopback and virtual ethernet devices
                if [ $net_if == "lo" ] || [ `echo $net_if | cut -c1-4` == "veth" ]; then
                    continue
                fi

                # ignore characters including and after '@' in interface name
                net_if=`echo "$net_if" | cut -d'@' -f1`

                if_info=`$my_sudo ethtool -i $net_if`
                if [ $? -ne 0 ]; then
                    echo "Failed to acquire information for the network interface '$net_if'"
                    exit 1
                fi

                # ignore interfaces of other devices
                bus_info=`echo "$if_info" | grep 'bus-info' | awk '{print $2}'`
                if [ $bus_info != $pci_addr ]; then
                    continue
                fi

                dev_ifs="$dev_ifs $net_if"
            done
        fi

        for dev_if in $dev_ifs; do
            dev_if=$(basename $dev_if)

            if [ ! -f /sys/class/net/$dev_if/dev_port ] ||
                   [ ! -f /sys/class/net/$dev_if/operstate ]; then
                echo "can't get dev_port/opersate of $dev_if"
                exit 1
            fi

            dev_port=$(cat /sys/class/net/$dev_if/dev_port)

            if [ $(cat /sys/class/net/$dev_if/operstate) == "up" ]; then
                let dev_num_up++
                if [ -z "$dev_ifs_up" ]; then
                    dev_ifs_up="$dev_port"
                else
                    dev_ifs_up="$dev_ifs_up $dev_port"
                fi
            else
                let dev_num_down++
                if [ -z "$dev_ifs_down" ]; then
                    dev_ifs_down="$dev_port"
                else
                    dev_ifs_down="$dev_ifs_down $dev_port"
                fi
            fi
        done

        if [ $print_module_id = true ]; then
            echo "$dev_name (module ID $module_id)"
        else
            echo "$dev_name"
        fi

        if [ -z "$dev_ifs_up" ] && [ -z "$dev_ifs_down" ]; then
            echo "no interfaces were detected"
            continue
        fi

        # sort lists in ascending order
        dev_ifs_up=$(echo $dev_ifs_up | xargs -n1 | sort -n | xargs)
        dev_ifs_down=$(echo $dev_ifs_down | xargs -n1 | sort -n | xargs)
        # add commas
        dev_ifs_up=${dev_ifs_up//" "/", "}
        dev_ifs_down=${dev_ifs_down//" "/", "}

        if [ $dev_num_up -gt 0 ]; then
            echo "$dev_num_up ports up ($dev_ifs_up)"
        fi
        if [ $dev_num_down -gt 0 ]; then
            echo "$dev_num_down ports down ($dev_ifs_down)"
        fi
    done
else
    build_net_ifs_global_list
fi

if [ $up -eq 1 ] || [ $down -eq 1 ]; then
    if [ $up -eq 1 ]; then
        op="up"
    else
        op="down"
    fi

    toggle_links $op

    echo -e "$(wc -w <<< "$habana_net_list") $HABANA_DRIVER_NAME network interfaces were toggled $op"
fi

if [ $set_pfc -eq 1 ] || [ $unset_pfc -eq 1 ] || [ $check_pfc -eq 1 ]; then
    type lldptool &> /dev/null
    if [ $? -ne 0 ]; then
        echo "lldptool is not installed"
        exit 1
    fi

    if [ $set_pfc -eq 1 ]; then
        op="set_pfc"
    elif [ $unset_pfc -eq 1 ]; then
        op="unset_pfc"
    else
        op="check_pfc"
    fi

    for net_if in $habana_net_list; do
        if [ $check_pfc -eq 1 ] || [ $verbose = true ]; then
            echo -e "$op '$net_if'"
        fi
        if [ $set_pfc -eq 1 ]; then
            $my_sudo lldptool -T -i $net_if -V PFC enabled=0,1,2,3 > /dev/null
        elif [ $unset_pfc -eq 1 ]; then
            $my_sudo lldptool -T -i $net_if -V PFC enabled=none > /dev/null
        else
            $my_sudo lldptool -t -i $net_if -V PFC -c enabled
        fi

        if [ $? -ne 0 ]; then
            echo "Error, $op '$net_if'"
            exit 1
        fi
    done

    if [ $verbose = true ]; then
        echo -e ""
    fi
fi

if [ $l3 -eq 1 ]; then
    $my_sudo /software/devops/l3/l3-routes
fi



exit 0