Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Commit

Permalink
scripts/xen-guest: MAC address generation
Browse files Browse the repository at this point in the history
Generate MAC addresses based on random numbers. The addresses
are subsequent for the attached netifs.

Signed-off-by: Simon Kuenzer <[email protected]>
  • Loading branch information
skuenzer authored and nderjung committed Mar 21, 2022
1 parent e335601 commit 3f1feb7
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions scripts/xen-guest
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ cpu2pool()
return 0
}

# 24bit base for mac addresses
_NETDEV_HWADDR_BASE=$(( (($RANDOM % 256) * 65536) + (($RANDOM % 256) * 256) + ($RANDOM % 256) ))

_netdev_hwaddr()
{
local NICID=${1:-0}
local HWADDR=$(( ${_NETDEV_HWADDR_BASE} + ${NICID} ))

printf '00:16:3e:%02x:%02x:%02x' \
$(( ($HWADDR / 65536) % 256 )) \
$(( ($HWADDR / 256) % 256 )) \
$(( $HWADDR % 256 ))
}

attach_natvif()
{
cat > "${TEMP}/vif-nat" <<EOF
Expand Down Expand Up @@ -171,7 +185,7 @@ EOF
cp "${XEN_SCRIPT_DIR}/logging.sh" "${TEMP}/" || die "Failed to copy ${XEN_SCRIPT_DIR}/logging.sh"
cp "${XEN_SCRIPT_DIR}/locking.sh" "${TEMP}/" || die "Failed to copy ${XEN_SCRIPT_DIR}/locking.sh"

VIFS_CFG+="'bridge=null,script=${TEMP}/vif-nat', "
VIFS_CFG+="'bridge=null,mac=$( _netdev_hwaddr ${NICID} ),script=${TEMP}/vif-nat', "
HAS_NATVIF=0
(( NICID++ ))
}
Expand Down Expand Up @@ -301,15 +315,15 @@ while getopts :h:nb:o:V:d:k:i:a:c:m:p:xDG:g:P OPT; do
attach_natvif
;;
b)
VIFS_CFG+="'bridge=${OPTARG},script=vif-bridge', "
VIFS_CFG+="'bridge=${OPTARG},mac=$( _netdev_hwaddr ${NICID} ),script=vif-bridge', "
(( NICID++ ))
;;
o)
VIFS_CFG+="'bridge=${OPTARG},script=vif-openvswitch', "
VIFS_CFG+="'bridge=${OPTARG},mac=$( _netdev_hwaddr ${NICID} ),script=vif-openvswitch', "
(( NICID++ ))
;;
V)
VIFS_CFG+="'type=nmif,bridge=${OPTARG},script=nmif-vale', "
VIFS_CFG+="'type=nmif,bridge=${OPTARG},mac=$( _netdev_hwaddr ${NICID} ),script=nmif-vale', "
(( NICID++ ))
;;
d)
Expand Down

0 comments on commit 3f1feb7

Please sign in to comment.