#!/bin/bash
function option_irq() {
  /etc/init.d/irqbalance stop 1>/dev/null 2>&1
  IntFE=$(route  -n|grep UG|awk  '{print $NF}')
for int in `echo $IntFE`
do
  IrqID=$(cat /proc/interrupts |grep ${int}|awk -F ':' '{print $1}'|xargs)
  Nx=0
  for Cid in ${IrqID}
  do
      Mex=$(echo $((2**${Nx})))
      Hex=$(printf "%x" ${Mex})
      echo ${Hex} > /proc/irq/${Cid}/smp_affinity
      Nx=$((${Nx}+1))
  done
done
}

option_irq

VAL_RPS_FLOW_CNT="32768"
VAL_RPS_SOCK_FLOW_ENTRIES="32768"

PROC_NUM=$(cat /proc/cpuinfo | grep -c ^processor)
VAL_RPS_CPUS=0

if [ ${PROC_NUM:?} -eq 1 ]
then
  echo "RPS/RFS disabled because of single cpu."
  exit 0
elif [ ${PROC_NUM:?} -le 3 ]
then
  ## 11(bit) = 3(0x3)
  VAL_RPS_CPUS=3
else
  ## 1111(bit) = f(0xf)
  VAL_RPS_CPUS="f"
fi

## RPS
FILES=$(ls /sys/class/net/*/queues/rx-*/rps_cpus)
for FILE in ${FILES}
do
  echo "${VAL_RPS_CPUS:?}" > "${FILE:?}"
done

## RFS
FILES=$(ls /sys/class/net/*/queues/rx-*/rps_flow_cnt)
for FILE in ${FILES}
do
  echo "${VAL_RPS_FLOW_CNT:?}" > "${FILE:?}"
done
sysctl -q -w net.core.rps_sock_flow_entries=${VAL_RPS_SOCK_FLOW_ENTRIES:?}
