#!/bin/bash

#### rp_filter
for DEV in /proc/sys/net/ipv4/conf/*/rp_filter
do
    echo 0 > $DEV
done

#### interrupts-tuning

service irqbalance stop
pkill irqbalance
#### cpu-clock
grep -E '^model name|^cpu MHz' /proc/cpuinfo | head -n1
echo "power scheme"
for CPUFREQ in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do [ -f $CPUFREQ ] || continue; cat $CPUFREQ; done  | head -n1
for CPUFREQ in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do [ -f $CPUFREQ ] || continue; echo -n performance > $CPUFREQ; done
echo "changed to"
for CPUFREQ in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do [ -f $CPUFREQ ] || continue; cat $CPUFREQ; done  | head -n1

cpucount=$(nproc)

#### cpu-clock
#
#sysdir=/sys/devices/system/cpu
#for cpu in $(eval echo cpu{0..$((cpucount-1))}); do
#        cat $sysdir/$cpu/cpufreq/scaling_max_freq > $sysdir/$cpu/cpufreq/scaling_min_freq
#done
#
#### sysctl


sysctl -w "net.core.rmem_max=67108864"
sysctl -w "net.core.wmem_max=67108864"
sysctl -w "net.ipv4.udp_mem=8388608 16777216 33554432"
sysctl -w "net.ipv4.tcp_rmem=4096 87380 33554432"
sysctl -w "net.ipv4.tcp_wmem=4096 65536 33554432"
sysctl -w "net.core.wmem_default=33554432"
sysctl -w "net.core.rmem_default=33554432"
sysctl -w "net.ipv4.tcp_tw_reuse=1"
sysctl -w "fs.file-max=818000"
sysctl -w "fs.suid_dumpable=0"
sysctl -w "kernel.core_uses_pid=1"
sysctl -w "kernel.exec-shield=1"
sysctl -w "kernel.kptr_restrict=1"
sysctl -w "kernel.maps_protect=1"
sysctl -w "kernel.msgmax=65535"
sysctl -w "kernel.msgmnb=65535"
sysctl -w "kernel.pid_max=65535"
sysctl -w "kernel.randomize_va_space=2"
sysctl -w "kernel.shmall=268435456"
sysctl -w "kernel.shmmax=268435456"
sysctl -w "kernel.sysrq=0"
sysctl -w "net.core.default_qdisc=fq"
sysctl -w "net.core.dev_weight=64"
sysctl -w "net.core.netdev_budget=50000"
sysctl -w "net.core.netdev_max_backlog=100000"
sysctl -w "net.core.optmem_max=204800"
sysctl -w "net.core.somaxconn=32768"
sysctl -w "net.ipv4.conf.all.accept_redirects=0"
sysctl -w "net.ipv4.conf.all.accept_source_route=0"
sysctl -w "net.ipv4.conf.all.arp_ignore=1"
sysctl -w "net.ipv4.conf.all.bootp_relay=0"
sysctl -w "net.ipv4.conf.all.force_igmp_version=2"
sysctl -w "net.ipv4.conf.all.forwarding=0"
sysctl -w "net.ipv4.conf.all.log_martians=0"
sysctl -w "net.ipv4.conf.all.proxy_arp=0"
sysctl -w "net.ipv4.conf.all.secure_redirects=0"
sysctl -w "net.ipv4.conf.all.send_redirects=0"
sysctl -w "net.ipv4.conf.default.accept_redirects=0"
sysctl -w "net.ipv4.conf.default.accept_source_route=0"
sysctl -w "net.ipv4.conf.default.forwarding=0"
sysctl -w "net.ipv4.conf.default.log_martians=0"
sysctl -w "net.ipv4.conf.default.secure_redirects=0"
sysctl -w "net.ipv4.conf.default.send_redirects=0"
sysctl -w "net.ipv4.conf.lo.log_martians=0"
sysctl -w "net.ipv4.icmp_echo_ignore_all=0"
sysctl -w "net.ipv4.icmp_echo_ignore_broadcasts=1"
sysctl -w "net.ipv4.icmp_ignore_bogus_error_responses=1"
sysctl -w "net.ipv4.ipfrag_high_thresh=512000"
sysctl -w "net.ipv4.ipfrag_low_thresh=446464"
sysctl -w "net.ipv4.ip_local_port_range=16384 65535"
sysctl -w "net.ipv4.neigh.default.gc_interval=30"
sysctl -w "net.ipv4.neigh.default.gc_thresh1=8192"
sysctl -w "net.ipv4.neigh.default.gc_thresh2=16384"
sysctl -w "net.ipv4.neigh.default.gc_thresh3=32768"
sysctl -w "net.ipv4.neigh.default.proxy_qlen=96"
sysctl -w "net.ipv4.neigh.default.unres_qlen=6"
sysctl -w "net.ipv4.netfilter.ip_conntrack_buckets=65536"
sysctl -w "net.ipv4.netfilter.ip_conntrack_generic_timeout=60"
sysctl -w "net.ipv4.netfilter.ip_conntrack_icmp_timeout=10"
sysctl -w "net.ipv4.netfilter.ip_conntrack_tcp_timeout_close=10"
sysctl -w "net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait=20"
sysctl -w "net.ipv4.netfilter.ip_conntrack_tcp_timeout_established=900"
sysctl -w "net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait=30"
sysctl -w "net.ipv4.netfilter.ip_conntrack_tcp_timeout_last_ack=30"
sysctl -w "net.ipv4.netfilter.ip_conntrack_tcp_timeout_syn_recv=30"
sysctl -w "net.ipv4.netfilter.ip_conntrack_tcp_timeout_syn_sent=15"
sysctl -w "net.ipv4.netfilter.ip_conntrack_tcp_timeout_syn_sent2=15"
sysctl -w "net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait=30"
sysctl -w "net.ipv4.netfilter.ip_conntrack_udp_timeout=30"
sysctl -w "net.ipv4.netfilter.ip_conntrack_udp_timeout_stream=45"
sysctl -w "net.ipv4.route.flush=1"
sysctl -w "net.ipv4.tcp_congestion_control=bbr"
sysctl -w "net.ipv4.tcp_ecn=1"
sysctl -w "net.ipv4.tcp_fastopen=1"
sysctl -w "net.ipv4.tcp_fin_timeout=30"
sysctl -w "net.ipv4.tcp_keepalive_intvl=15"
sysctl -w "net.ipv4.tcp_keepalive_probes=5"
sysctl -w "net.ipv4.tcp_keepalive_time=300"
sysctl -w "net.ipv4.tcp_max_orphans=65536"
sysctl -w "net.ipv4.tcp_max_syn_backlog=4096"
sysctl -w "net.ipv4.tcp_max_tw_buckets=1440000"
sysctl -w "net.ipv4.tcp_moderate_rcvbuf=1"
sysctl -w "net.ipv4.tcp_mtu_probing=1"
sysctl -w "net.ipv4.tcp_no_metrics_save=1"
sysctl -w "net.ipv4.tcp_orphan_retries=1"
sysctl -w "net.ipv4.tcp_reordering=3"
sysctl -w "net.ipv4.tcp_retries1=3"
sysctl -w "net.ipv4.tcp_retries2=15"
sysctl -w "net.ipv4.tcp_rfc1337=1"
sysctl -w "net.ipv4.tcp_sack=1"
sysctl -w "net.ipv4.tcp_slow_start_after_idle=0"
sysctl -w "net.ipv4.tcp_synack_retries=2"
sysctl -w "net.ipv4.tcp_syncookies=1"
sysctl -w "net.ipv4.tcp_syn_retries=2"
sysctl -w "net.ipv4.tcp_timestamps=1"
sysctl -w "net.ipv4.tcp_tw_reuse=1"
sysctl -w "net.ipv4.tcp_window_scaling=1"
sysctl -w "net.ipv6.conf.all.accept_ra=0"
sysctl -w "net.ipv6.conf.all.accept_redirects=0"
sysctl -w "net.ipv6.conf.all.accept_source_route=0"
sysctl -w "net.ipv6.conf.all.autoconf=0"
sysctl -w "net.ipv6.conf.all.forwarding=0"
sysctl -w "net.ipv6.conf.default.accept_ra=0"
sysctl -w "net.ipv6.conf.default.accept_redirects=0"
sysctl -w "net.ipv6.conf.default.accept_source_route=0"
sysctl -w "net.ipv6.conf.default.autoconf=0"
sysctl -w "net.ipv6.conf.default.forwarding=0"
sysctl -w "net.ipv6.route.flush=1"
sysctl -w "net.netfilter.nf_conntrack_max=1048576"
sysctl -w "net.nf_conntrack_max=1048576"
sysctl -w "net.unix.max_dgram_qlen=50"
sysctl -w "vm.dirty_background_bytes=67108864"
sysctl -w "vm.dirty_background_ratio=5"
sysctl -w "vm.dirty_bytes=134217728"
sysctl -w "vm.dirty_ratio=30"
sysctl -w "vm.min_free_kbytes=32768"
sysctl -w "vm.min_free_kbytes=65535"
sysctl -w "vm.mmap_min_addr=4096"
sysctl -w "vm.nr_hugepages=1024"
sysctl -w "vm.overcommit_memory=0"
sysctl -w "vm.overcommit_ratio=50"
sysctl -w "vm.swappiness=1"
sysctl -w "vm.vfs_cache_pressure=50"
sysctl -w "net.ipv4.ip_local_port_range=15000 61000"



#### irq affinity

test "$cpucount" -gt 1 || exit 1
n=0

for irq in $(cat /proc/interrupts | grep 'tbsecp3\|eth\|ddbridge\|em\|eno\|ens\|enp\|p6p\|\ens' | awk '{print $1}' | sed 's|:||g') ; do
    f="/proc/irq/$irq/smp_affinity"
    test -r "$f" || continue
    cpu=$[$cpucount - ($n % $cpucount) - 1]
    if [ $cpu -ge 0 ] ; then
        mask=$(printf %x $[2 ** $cpu])
        echo "Assign SMP affinity: dev$n, irq $irq, cpu $cpu, mask 0x$mask"
        echo "$mask" > "$f"
        let n+=1
    fi
done

