#!/bin/bash

# 安装必备软件包
yum -y install vim bash-completion wget lrzsz net-tools irqbalance 


# 获取活动的网络接口
interface1=$(ip a | grep UP | grep -v UNKNOWN | grep -v noqueue | awk '{print $2}' | sed 's/://g' | sed -n '1p')
interface2=$(ip a | grep UP | grep -v UNKNOWN | grep -v noqueue | awk '{print $2}' | sed 's/://g' | sed -n '2p')

# 获取第一个接口的最大接收值
maxvalue=$(ethtool -g $interface1 | grep RX: | awk '{print $2}' | sed -n '1p')

# 设置接收和发送的最大值
ethtool -G $interface1 rx $maxvalue tx $maxvalue
ethtool -G $interface2 rx $maxvalue tx $maxvalue

# 设置内核参数
sysctl -w net.core.rps_sock_flow_entries=65536
sysctl -w net.ipv4.tcp_max_syn_backlog=819200
sysctl -w net.core.netdev_max_backlog=262144

# 获取处理器数量
N=$(grep -c processor /proc/cpuinfo)

# 计算 RPS 流量计数
rfc=$(echo 65536/$N | bc)
echo $rfc | tee /sys/class/net/e*/queues/rx-*/rps_flow_cnt

# 根据处理器数量设置 RPS CPU 亲和性
if [ "$N" -eq 56 ]; then
    echo "ffffff,ffffffff" | tee /sys/class/net/e*/queues/rx-*/rps_cpus
    printf "ffffff,ffffffff" | tee /proc/irq/*/smp_affinity
    echo "0-$((N-1))" | tee /proc/irq/*/smp_affinity_list
elif [ "$N" -lt 56 ]; then
    echo $(printf %x $((2**N-1))) | tee /sys/class/net/e*/queues/rx-*/rps_cpus
    printf %x $((2**N-1)) | tee /proc/irq/*/smp_affinity
    echo "0-$((N-1))" | tee /proc/irq/*/smp_affinity_list
fi






echo "1024000" > /proc/sys/fs/nr_open


cat > '/etc/sysctl.conf' << EOF
kernel.msgmnb=65536
kernel.msgmax=65536
kernel.shmmax=68719476736
kernel.shmall=4294967296
kernel.printk = 0
vm.swappiness=10

net.core.rps_sock_flow_entries=65536 #rfs 设置此文件至同时活跃连接数的最大预期值
#net.ipv4.icmp_echo_ignore_all=1 #禁止ping
#net.ipv4.icmp_echo_ignore_broadcasts=1
fs.file-max=1024000
fs.nr_open=1024000
fs.inotify.max_user_instances=131072
#开启路由转发
net.ipv4.conf.all.route_localnet=1
net.ipv4.ip_forward=1
net.ipv4.conf.all.forwarding=1
net.ipv4.conf.default.forwarding=1

net.ipv4.conf.all.accept_redirects=0
net.ipv4.conf.default.accept_redirects=0
net.ipv4.conf.all.secure_redirects=0
net.ipv4.conf.default.secure_redirects=0
net.ipv4.conf.all.send_redirects=0
net.ipv4.conf.default.send_redirects=0
net.ipv4.conf.default.rp_filter=2
net.ipv4.conf.all.rp_filter=2

net.ipv4.tcp_abort_on_overflow = 1
net.ipv4.tcp_syncookies=1
net.ipv4.tcp_retries1=3
net.ipv4.tcp_retries2=3
net.ipv4.tcp_orphan_retries=1
net.ipv4.tcp_max_orphans = 32768
net.ipv4.tcp_syn_retries=1
net.ipv4.tcp_synack_retries=3
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_fin_timeout=10
net.ipv4.tcp_max_tw_buckets = 32768
# net.core.dev_weight=8192
net.core.netdev_budget=65536
net.core.netdev_budget_usecs=4096
net.ipv4.tcp_max_syn_backlog = 819200
net.core.netdev_max_backlog=262144
net.core.somaxconn=100000
net.ipv4.tcp_notsent_lowat=16384
net.ipv4.tcp_timestamps=1
net.ipv4.tcp_keepalive_time=60
net.ipv4.tcp_keepalive_probes=3
net.ipv4.tcp_keepalive_intvl=20
net.ipv4.route.gc_timeout=100
net.ipv4.neigh.default.gc_thresh1=2048
net.ipv4.neigh.default.gc_thresh2=4096 
net.ipv4.neigh.default.gc_thresh3=8192
# net.ipv6.neigh.default.gc_thresh1=1024
# net.ipv6.neigh.default.gc_thresh2=4096
# net.ipv6.neigh.default.gc_thresh3=8192
# TCP窗口
net.ipv4.tcp_fastopen=3
net.ipv4.tcp_autocorking=0
net.ipv4.tcp_slow_start_after_idle=0
net.ipv4.tcp_no_metrics_save=1
net.ipv4.tcp_ecn=0
net.ipv4.tcp_frto=0
net.ipv4.tcp_mtu_probing=0
net.ipv4.tcp_sack=1
net.ipv4.tcp_fack=1
net.ipv4.tcp_window_scaling=1
net.ipv4.tcp_adv_win_scale=1
net.ipv4.tcp_moderate_rcvbuf=1
net.ipv4.tcp_thin_linear_timeouts = 0
net.core.rmem_max = 67108864
net.core.wmem_max = 67108864
net.ipv4.tcp_rmem = 8192 87380 67108864
net.ipv4.tcp_wmem = 8192 65536 67108864
net.ipv4.udp_rmem_min=8192
net.ipv4.udp_wmem_min=8192
net.ipv4.tcp_mem=262144 1048576 4194304
net.ipv4.udp_mem=262144 1048576 4194304
net.ipv4.tcp_congestion_control=bbr
net.core.default_qdisc=fq
net.ipv4.ip_local_port_range= 1025 65535
net.ipv4.ping_group_range=0 2147483647


EOF

echo madvise > /sys/kernel/mm/transparent_hugepage/enabled





sed -i '/DefaultTimeoutStartSec/d' /etc/systemd/system.conf
sed -i '/DefaultTimeoutStopSec/d' /etc/systemd/system.conf
sed -i '/DefaultRestartSec/d' /etc/systemd/system.conf
sed -i '/DefaultLimitCORE/d' /etc/systemd/system.conf
sed -i '/DefaultLimitNOFILE/d' /etc/systemd/system.conf
sed -i '/DefaultLimitNPROC/d' /etc/systemd/system.conf

cat > '/etc/systemd/system.conf' << EOF
[Manager]
#DefaultTimeoutStartSec=90s
DefaultTimeoutStopSec=30s
#DefaultRestartSec=100ms
DefaultLimitCORE=infinity
DefaultLimitNOFILE=102400
DefaultLimitNPROC=65535
EOF

sed -i '/soft nofile/d' /etc/security/limits.conf
sed -i '/hard nofile/d' /etc/security/limits.conf
sed -i '/soft nproc/d' /etc/security/limits.conf
sed -i '/hard nproc/d' /etc/security/limits.conf
cat > '/etc/security/limits.conf' << EOF
* soft nofile 512000
* hard nofile 512000
* soft nproc 512000
* hard nproc 512000
EOF



#立即应用内核参数
sysctl -p 



# 启用 IRQ 平衡
systemctl enable irqbalance
systemctl start irqbalance

# 启用硬件加速 (GRO, LRO, TSO, GSO) 并优化网络接口队列
for iface in $(ls /sys/class/net | grep -v lo); do
    ethtool -K $iface gro on lro on tso on gso on
    ethtool -G $iface rx 4096 tx 4096
done

# 调整 I/O 调度器为 deadline
for dev in $(ls /sys/block/ | grep 'sd'); do
    echo "deadline" > /sys/block/$dev/queue/scheduler
done

# 提示完成
echo "优化配置已应用，并将保持重启后生效。"


#重启生效配置
reboot

