#!/bin/sh
export PATH=/stand:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

OUTPUT=$1/boot/loader.conf

PHYSMEM=$(sysctl hw.physmem | awk '{print $2}')

# One L7 node per 64K.  Use expr, /bin/sh often overflows at 32 bits.
L7NODES=$(expr $PHYSMEM / 65536)

# Three socks per L7 node, plus some slush
SOX=$(($L7NODES * 3 + 2048))

# Two PCB hash table buckets per L7 node.
TCBHASHSZ=$(($L7NODES * 2))
# Fit to next larger power of 2, kernel wants power-of-two hash size
# but $PHYSMEM is usually missing a few bytes, which distorts things.
#
pow=2; while [ $pow -lt $TCBHASHSZ ]
do
	pow=$(( $pow * 2 ))
done
TCBHASHSZ=$pow

# 8 mbuf clusters per L7 node, to a maximum of 128K (256MB).
MBCL=$(($L7NODES * 8))
if [ $MBCL -gt 131072 ]; then
	MBCL=131072
fi

## The fixed part

cat > $OUTPUT << ZOG
userconfig_script_load="YES"
# Xcel II wants this since it polls like a network device
kern.polling.enable=1
#
# ensure we don't hang about in the debugger
#
debug.debugger_on_panic=0
#
net.inet.tcp.syncache.hashsize=8192
hw.em.rx_desc=4096
#
ZOG

## The variable part
echo net.inet.tcp.tcbhashsize=$TCBHASHSZ >> $OUTPUT
echo kern.ipc.maxsockets=$SOX >> $OUTPUT
echo kern.ipc.nmbclusters=$MBCL >> $OUTPUT
echo eq.l7lb.max_nodes=$L7NODES >> $OUTPUT
