zoukankan      html  css  js  c++  java
  • Linux网卡调优篇-禁用ipv6与优化socket缓冲区大小

                  Linux网卡调优篇-禁用ipv6与优化socket缓冲区大小

                                               作者:尹正杰

    版权声明:原创作品,谢绝转载!否则将追究法律责任。

       一般在内网环境中,我们几乎是用不到IPV6,因此我们没有必要把多不用的功能开起来从而浪费不必要的资源。默认情况下,系统内核没有针对快速的的大流量网络传输进行优化,所以对于应用程序来说,一般需要对Linux系统的网络栈进行调优,以实现对大流量的支持。

    一.禁用IPv6图解

    1>.使用ifconfig查看网卡信息(如果没有该工具,直接yum按照响应的工具包即可:[root@node118 ~]# yum -y install net-tools )

    2>. 禁用IPV6的步骤

    [root@node118 ~]# echo "net.ipv6.conf.all.disable_ipv6=1" >> /etc/sysctl.conf
    [root@node118 ~]# 
    [root@node118 ~]# echo "NETWORKING_IPV6=no" >> /etc/sysconfig/network
    [root@node118 ~]# 
    [root@node118 ~]# echo NETWORKING_IPV6=no >> /etc/sysconfig/network-scripts/ifcfg-bond0 
    [root@node118 ~]# 
    [root@node118 ~]# sysctl -p
    net.ipv6.conf.all.disable_ipv6 = 1
    [root@node118 ~]# 

    3>.执行上述命令后,再次查看bond0的网卡配置信息 

    二.socket读写缓冲区调优

       实际上,调整Kafka的网络配置与其他的配置是一样的。首先可以对分配给socket读写缓冲区的内存大小作出调整,这样可以显著提升网络的传输性能。

    1>.默认的TCP数据发送窗口大小(字节)。

    [root@yinzhengjie ~]# cat /proc/sys/net/core/wmem_default                                       #对应net.core.wmem_default这个参数 
    212992
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -q net.core.wmem_default
    net.core.wmem_default = 212992
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# echo "net.core.wmem_default=256960" >> /etc/sysctl.conf                #我这里设置发送大小为256KB
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -p
    vm.swappiness = 1
    vm.dirty_background_ratio = 5
    vm.dirty_ratio = 80
    net.ipv6.conf.all.disable_ipv6 = 1
    net.core.wmem_default = 256960
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -q net.core.wmem_default                        
    net.core.wmem_default = 256960
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# 

    2>.默认的TCP数据接收窗口大小(字节)。

    [root@yinzhengjie ~]# cat /proc/sys/net/core/rmem_default                                    #对应的参数是net.core.rmem_default
    212992
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -q net.core.rmem_default
    net.core.rmem_default = 212992
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# echo "net.core.rmem_default=256960" >> /etc/sysctl.conf                     #我这里接受大小设置的是256KB
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -p
    vm.swappiness = 1
    vm.dirty_background_ratio = 5
    vm.dirty_ratio = 80
    net.ipv6.conf.all.disable_ipv6 = 1
    net.core.wmem_default = 256960
    net.core.rmem_default = 256960
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -q net.core.rmem_default
    net.core.rmem_default = 256960
    [root@yinzhengjie ~]# 

    3>.最大的TCP数据发送窗口(字节)。

    [root@yinzhengjie ~]# cat /proc/sys/net/core/wmem_max                                    #对应的参数是:net.core.wmem_max
    212992
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -q net.core.wmem_max
    net.core.wmem_max = 212992
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# echo "net.core.wmem_max=2097152" >> /etc/sysctl.conf                 #我这里将最大的TCP数据发送缓冲区最大值为2M          
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -p
    vm.swappiness = 1
    vm.dirty_background_ratio = 5
    vm.dirty_ratio = 80
    net.ipv6.conf.all.disable_ipv6 = 1
    net.core.wmem_default = 256960
    net.core.rmem_default = 256960
    net.core.wmem_max = 2097152
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -q net.core.wmem_max                         
    net.core.wmem_max = 2097152
    [root@yinzhengjie ~]#  

    4>.最大的TCP数据接收窗口(字节)。

    [root@yinzhengjie ~]# cat /proc/sys/net/core/rmem_max                                #对应参数是:net.core.rmem_max
    212992
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -q net.core.rmem_max
    net.core.rmem_max = 212992
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# echo "net.core.rmem_max=2097152" >> /etc/sysctl.conf                     #我这里将TCP数据接受缓冲区的最大值设置为2M
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -p
    vm.swappiness = 1
    vm.dirty_background_ratio = 5
    vm.dirty_ratio = 80
    net.ipv6.conf.all.disable_ipv6 = 1
    net.core.wmem_default = 256960
    net.core.rmem_default = 256960
    net.core.wmem_max = 2097152
    net.core.rmem_max = 2097152
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -q net.core.rmem_max                         
    net.core.rmem_max = 2097152
    [root@yinzhengjie ~]# 

      温馨提示:最大值并不意味着每个socket一定要有这么大大缓冲空间,只是说在必要大情况下才会达到这个值。

     

    三.TCP socket的读写缓冲区调优

       除了设置socket外,我们还需要设置TCP socket的读写缓冲区。

    1>.为自动调优定义socket使用的发送内存(写)。

    [root@yinzhengjie ~]# cat /proc/sys/net/ipv4/tcp_wmem                #对应参数:net.ipv4.tcp_wmem
    4096    16384   4194304
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -q net.ipv4.tcp_wmem                        
    net.ipv4.tcp_wmem = 4096        16384   4194304                        #第一个值是为socket发送缓冲区分配的最少字节数;第二个值是默认值(该值会被wmem_default覆盖),缓冲区在系统负载不重的情况下可以增长到这个值;第三个值是发送缓冲区空间的最大字节数(该值会被wmem_max覆盖)。
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# echo "net.ipv4.tcp_wmem=8760  256960  4088000" >> /etc/sysctl.conf         #根据服务接受的实际情况,可能需要设置更高的最大值,为网络链接提供更大的缓存空间。
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -p
    vm.swappiness = 1
    vm.dirty_background_ratio = 5
    vm.dirty_ratio = 80
    net.ipv6.conf.all.disable_ipv6 = 1
    net.core.wmem_default = 256960
    net.core.rmem_default = 256960
    net.core.wmem_max = 2097152
    net.core.rmem_max = 2097152
    net.ipv4.tcp_wmem = 8760  256960  4088000
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -q net.ipv4.tcp_wmem                                        
    net.ipv4.tcp_wmem = 8760        256960  4088000
    [root@yinzhengjie ~]# 

    2>. 为自动调优定义socket使用的接受内存(读)。

    [root@yinzhengjie ~]# cat /proc/sys/net/ipv4/tcp_rmem            #对应参数为:net.ipv4.tcp_rmem
    4096    87380   6291456
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -q net.ipv4.tcp_rmem
    net.ipv4.tcp_rmem = 4096        87380   6291456                #第一个值是为socket接收缓冲区分配的最少字节数;第二个值是默认值(该值会被rmem_default覆盖),缓冲区在系统负载不重的情况下可以增长到这个值;第三个值是接收缓冲区空间的最大字节数(该值会被rmem_max覆盖)。
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# echo "net.ipv4.tcp_rmem=8760  256960  4088000" >> /etc/sysctl.conf                  
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -p
    vm.swappiness = 1
    vm.dirty_background_ratio = 5
    vm.dirty_ratio = 80
    net.ipv6.conf.all.disable_ipv6 = 1
    net.core.wmem_default = 256960
    net.core.rmem_default = 256960
    net.core.wmem_max = 2097152
    net.core.rmem_max = 2097152
    net.ipv4.tcp_wmem = 8760  256960  4088000
    net.ipv4.tcp_rmem = 8760  256960  4088000
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# sysctl -q net.ipv4.tcp_rmem                                        
    net.ipv4.tcp_rmem = 8760        256960  4088000
    [root@yinzhengjie ~]#  

      根据kafka服务器接受不了流量的实际情况,可能需要设置更高的最大值,为网络链接提供更大的缓冲空间。

    四.其他网络参数调优

    [root@yinzhengjie ~]# echo "net.ipv4.tcp_window_scaling=1" >> /etc/sysctl.conf                 # 启用RFC 1323定义的window scaling,要支持超过64KB的TCP窗口,必须启用该值(1表示启用),TCP窗口最大至1GB,TCP连接双方都启用时才生效。
    
    
    [root@yinzhengjie ~]# echo "net.ipv4.tcp_max_syn_backlog=2048" >> /etc/sysctl.conf             #每一个连接请求(SYN报文)都需要排队,直至本地服务器接收,该变量就是控制每个端口的 TCP SYN队列长度的。如果连接请求多余该值,则请求会被丢弃。
    
    [root@yinzhengjie ~]# echo "net.core.netdev_max_backlog=2000" >> /etc/sysctl.conf            #该参数定义了当接口收到包的速率大于内核处理包的速率时,设备的输入队列中的最大报文数。
    
    
    
    [root@yinzhengjie ~]# echo "net.ipv4.tcp_sack=1" >> /etc/sysctl.conf                        #管理TCP的选择性应答,允许接收端向发送端传递关于字节流中丢失的序列号,减少了段丢失时需要重传的段数目,当段丢失频繁时,sack是很有益的。
    
    
    [root@yinzhengjie ~]# echo "net.ipv4.tcp_fack=1">> /etc/sysctl.conf                              #启用转发应答,可以进行有选择应答(SACK)从而减少拥塞情况的发生,这个选项也应该启用。
    
    [root@yinzhengjie ~]# echo "net.ipv4.tcp_tw_recycle=1">> /etc/sysctl.conf                 #能够更快地回收TIME-WAIT套接字,默认是0。
    
    [root@yinzhengjie ~]# echo "net.ipv4.tcp_timestamps=0" >> /etc/sysctl.conf             #表示不再检查时间戳,默认值是开启的。发现tcp_timestamps默认是开启,如果再把tcp_tw_recycle设置为1,则60s内同一源ip主机的socket connect请求中的timestamp必须是递增的。也就是说服务器打开了 tcp_tw_reccycle了,就会检查时间戳,如果对方发来的包的时间戳是乱跳的或者说时间戳是滞后的,这样服务器肯定不会回复,所以服务器就把带了“倒退”的时间戳的包当作是“recycle的tw连接的重传数据,不是新的请求”,于是丢掉不回包,就出现了开始说的syn不响应。

     

  • 相关阅读:
    5 Things Every Manager Should Know about Microsoft SharePoint 关于微软SharePoint每个经理应该知道的五件事
    Microsoft SharePoint 2010, is it a true Document Management System? 微软SharePoint 2010,它是真正的文档管理系统吗?
    You think you use SharePoint but you really don't 你认为你使用了SharePoint,但是实际上不是
    Introducing Document Management in SharePoint 2010 介绍SharePoint 2010中的文档管理
    Creating Your Own Document Management System With SharePoint 使用SharePoint创建你自己的文档管理系统
    MVP模式介绍
    权重初始化的选择
    机器学习中线性模型和非线性的区别
    神经网络激励函数的作用是什么
    深度学习中,交叉熵损失函数为什么优于均方差损失函数
  • 原文地址:https://www.cnblogs.com/yinzhengjie/p/9995756.html
Copyright © 2011-2022 走看看