blob: bce73722e4583ffe78a693ea1b822314d729e1fe [file]
#!/bin/bash
# This script will bring up all MCTP link (i2c, i3c, USB), and assign local MCTP address (from 254 to lower EID) for them.
# Use single MCTP network ID 1 by default
readonly USE_SINGLE_NETWORK_ID='true'
get_next_net()
{
existing=$(mctp link | awk '{print $8}')
net=1
while true; do
if echo "$existing" | grep -E "^${net}$" >/dev/null; then
net=$((net + 1))
else
echo ${net}
return
fi
done
}
# Search unused EID from 254 to lower addresses
get_next_eid()
{
if [ $# -ne 1 ]; then
echo "Error: missing network ID parameter."
exit 1
fi
net="$1"
existing=$(mctp addr | grep "net $net" | awk '{print $2}')
# Valid EID range: [8-254]
eid=254
while [ "$eid" -ge 8 ]; do
if echo "$existing" | grep -E "^${eid}$" >/dev/null; then
eid=$((eid - 1))
else
echo $eid
return
fi
done
echo "Error: all EID range [8-254] of net ${net} are already assigned"
exit 1
}
# Wait 1 second for udevd to fully release the interface rename lock
sleep 1
mctp_ifname_lines=$(mctp link | grep -v 'dev lo')
while IFS= read -r line; do
if [[ $line =~ ^dev\ (mctp[a-z0-9]+)\ index ]]; then
ifname="${BASH_REMATCH[1]}"
# Bring up MCTP link if it is down
if [[ "$line" == *"down" ]]; then
if ${USE_SINGLE_NETWORK_ID}; then
net=1
else
net=$(get_next_net)
fi
echo "mctp link set ${ifname} up network $net"
mctp link set "${ifname}" up network "${net}"
fi
# Assign local MCTP EID if it has no address yet
mctp_addr_line=$(mctp addr show "${ifname}" )
if [[ ${mctp_addr_line} != *"eid "* ]]; then
eid=$(get_next_eid "${net}")
echo "mctp addr add ${eid} dev ${ifname}"
mctp addr add "${eid}" dev "${ifname}"
fi
fi
done <<< "${mctp_ifname_lines}"