目录

nf_conntrack

bridge-nf

反向路径过滤

TCP相关

ARP相关

ARP回收

ARP过滤

参考文档


nf_conntrack

nf_conntrack是Linux内核连接跟踪的模块,常用在iptables中,比如

-A INPUT -m state --state RELATED,ESTABLISHED  -j RETURN
-A INPUT -m state --state INVALID -j DROP

可以通过cat /proc/net/nf_conntrack来查看当前跟踪的连接信息,这些信息以哈希形式(用链地址法处理冲突)存在内存中,并且每条记录大约占300B空间。

nf_conntrack相关的内核参数有三个:

  • nf_conntrack_max:连接跟踪表的大小,建议根据内存计算该值CONNTRACK_MAX = RAMSIZE (in bytes) / 16384 / (x / 32),并满足nf_conntrack_max=4*nf_conntrack_buckets,默认262144
  • nf_conntrack_buckets:哈希表的大小,(nf_conntrack_max/nf_conntrack_buckets就是每条哈希记录链表的长度),默认65536
  • nf_conntrack_tcp_timeout_established:tcp会话的超时时间,默认是432000 (5天)

比如,对64G内存的机器,推荐配置:

net.netfilter.nf_conntrack_max=4194304
net.netfilter.nf_conntrack_tcp_timeout_established=300
net.netfilter.nf_conntrack_buckets=1048576

bridge-nf

bridge-nf使得netfilter可以对Linux网桥上的IPv4/ARP/IPv6包过滤。比如,设置net.bridge.bridge-nf-call-iptables=1后,二层的网桥在转发包时也会被iptables的FORWARD规则所过滤,这样有时会出现L3层的iptables rules去过滤L2的帧的问题(见这里)。

常用的选项包括

  • net.bridge.bridge-nf-call-arptables:是否在arptables的FORWARD中过滤网桥的ARP包
  • net.bridge.bridge-nf-call-ip6tables:是否在ip6tables链中过滤IPv6包
  • net.bridge.bridge-nf-call-iptables:是否在iptables链中过滤IPv4包
  • net.bridge.bridge-nf-filter-vlan-tagged:是否在iptables/arptables中过滤打了vlan标签的包

当然,也可以通过/sys/devices/virtual/net/<bridge-name>/bridge/nf_call_iptables来设置,但要注意内核是取两者中大的生效。

有时,可能只希望部分网桥禁止bridge-nf,而其他网桥都开启(比如CNI网络插件中一般要求bridge-nf-call-iptables选项开启,而有时又希望禁止某个网桥的bridge-nf),这时可以改用iptables的方法:

iptables -t raw -I PREROUTING -i <bridge-name> -j NOTRACK

反向路径过滤

反向路径过滤可用于防止数据包从一接口传入,又从另一不同的接口传出(这有时被称为 “非对称路由” )。除非必要,否则最好将其关闭,因为它可防止来自子网络的用户采用 IP 地址欺骗手段,并减少 DDoS (分布式拒绝服务)攻击的机会。

通过 rp_filter 选项启用反向路径过滤,比如 sysctl -w net.ipv4.conf.default.rp_filter=INTEGER。支持三种选项:

  • 0 ——未进行源验证。
  • 1 ——处于如 RFC3704 所定义的严格模式。
  • 2 ——处于如 RFC3704 所定义的松散模式。

可以通过 net.ipv4.interface.rp_filter可实现对每一网络接口设置的覆盖。

TCP相关

参数 描述 默认值 优化值
net.core.rmem_default 默认的TCP数据接收窗口大小(字节) 212992  
net.core.rmem_max 最大的TCP数据接收窗口(字节)。 212992  
net.core.wmem_default 默认的TCP数据发送窗口大小(字节)。 212992  
net.core.wmem_max 最大的TCP数据发送窗口(字节)。 212992  
net.core.netdev_max_backlog 在每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目。 1000 10000
net.core.somaxconn 定义了系统中每一个端口最大的监听队列的长度,这是个全局的参数。 128 2048
net.core.optmem_max 表示每个套接字所允许的最大缓冲区的大小。 20480 81920
net.ipv4.tcp_mem 确定TCP栈应该如何反映内存使用,每个值的单位都是内存页(通常是4KB)。第一个值是内存使用的下限;第二个值是内存压力模式开始对缓冲区使用应用压力的上限;第三个值是内存使用的上限。在这个层次上可以将报文丢弃,从而减少对内存的使用。对于较大的BDP可以增大这些值(注意,其单位是内存页而不是字节)。 5814 7754 11628  
net.ipv4.tcp_rmem 为自动调优定义socket使用的内存。第一个值是为socket接收缓冲区分配的最少字节数;第二个值是默认值(该值会被rmem_default覆盖),缓冲区在系统负载不重的情况下可以增长到这个值;第三个值是接收缓冲区空间的最大字节数(该值会被rmem_max覆盖)。 4096 87380 3970528  
net.ipv4.tcp_wmem 为自动调优定义socket使用的内存。第一个值是为socket发送缓冲区分配的最少字节数;第二个值是默认值(该值会被wmem_default覆盖),缓冲区在系统负载不重的情况下可以增长到这个值;第三个值是发送缓冲区空间的最大字节数(该值会被wmem_max覆盖)。 4096 16384 3970528  
net.ipv4.tcp_keepalive_time TCP发送keepalive探测消息的间隔时间(秒),用于确认TCP连接是否有效。 7200 1800
net.ipv4.tcp_keepalive_intvl 探测消息未获得响应时,重发该消息的间隔时间(秒) 75 30
net.ipv4.tcp_keepalive_probes 在认定TCP连接失效之前,最多发送多少个keepalive探测消息。 9 3
net.ipv4.tcp_sack 启用有选择的应答(1表示启用),通过有选择地应答乱序接收到的报文来提高性能,让发送者只发送丢失的报文段,(对于广域网通信来说)这个选项应该启用,但是会增加对CPU的占用。 1 1
net.ipv4.tcp_fack 启用转发应答,可以进行有选择应答(SACK)从而减少拥塞情况的发生,这个选项也应该启用。 1 1
net.ipv4.tcp_timestamps TCP时间戳(会在TCP包头增加12个字节),以一种比重发超时更精确的方法(参考RFC 1323)来启用对RTT 的计算,为实现更好的性能应该启用这个选项。 1 1
net.ipv4.tcp_window_scaling 启用RFC 1323定义的window scaling,要支持超过64KB的TCP窗口,必须启用该值(1表示启用),TCP窗口最大至1GB,TCP连接双方都启用时才生效。 1 1
net.ipv4.tcp_syncookies 表示是否打开TCP同步标签(syncookie),内核必须打开了CONFIG_SYN_COOKIES项进行编译,同步标签可以防止一个套接字在有过多试图连接到达时引起过载。 1 1
net.ipv4.tcp_tw_reuse 表示是否允许将处于TIME-WAIT状态的socket(TIME-WAIT的端口)用于新的TCP连接 。 0 1
net.ipv4.tcp_tw_recycle 能够更快地回收TIME-WAIT套接字。 0 1
net.ipv4.tcp_fin_timeout 对于本端断开的socket连接,TCP保持在FIN-WAIT-2状态的时间(秒)。对方可能会断开连接或一直不结束连接或不可预料的进程死亡。 60 30
net.ipv4.ip_local_port_range 表示TCP/UDP协议允许使用的本地端口号 32768 60999 1024 65000
net.ipv4.tcp_max_syn_backlog 对于还未获得对方确认的连接请求,可保存在队列中的最大数目。如果服务器经常出现过载,可以尝试增加这个数字。 128  
net.ipv4.tcp_low_latency 允许TCP/IP栈适应在高吞吐量情况下低延时的情况,这个选项应该禁用。 0 0

ARP相关

ARP回收

  • gc_stale_time 每次检查neighbour记录的有效性的周期。当neighbour记录失效时,将在给它发送数据前再解析一次。缺省值是60秒。
  • gc_thresh1 存在于ARP高速缓存中的最少记录数,如果少于这个数,垃圾收集器将不会运行。缺省值是128。
  • gc_thresh2 存在 ARP 高速缓存中的最多的记录软限制。垃圾收集器在开始收集前,允许记录数超过这个数字 5 秒。缺省值是 512。
  • gc_thresh3 保存在 ARP 高速缓存中的最多记录的硬限制,一旦高速缓存中的数目高于此,垃圾收集器将马上运行。缺省值是1024。

比如可以增大为

net.ipv4.neigh.default.gc_thresh1=1024
net.ipv4.neigh.default.gc_thresh2=4096
net.ipv4.neigh.default.gc_thresh3=8192

ARP过滤

arp_filter - BOOLEAN

1 - Allows you to have multiple network interfaces on the same
subnet, and have the ARPs for each interface be answered
based on whether or not the kernel would route a packet from
the ARP'd IP out that interface (therefore you must use source
based routing for this to work). In other words it allows control
of which cards (usually 1) will respond to an arp request.0 - (default) The kernel can respond to arp requests with addresses
from other interfaces. This may seem wrong but it usually makes
sense, because it increases the chance of successful communication.
IP addresses are owned by the complete host on Linux, not by
particular interfaces. Only for more complex setups like load-
balancing, does this behaviour cause problems.arp_filter for the interface will be enabled if at least one of
conf/{all,interface}/arp_filter is set to TRUE,
it will be disabled otherwise

arp_announce - INTEGER

Define different restriction levels for announcing the local
source IP address from IP packets in ARP requests sent on
interface:
0 - (default) Use any local address, configured on any interface
1 - Try to avoid local addresses that are not in the target's
subnet for this interface. This mode is useful when target
hosts reachable via this interface require the source IP
address in ARP requests to be part of their logical network
configured on the receiving interface. When we generate the
request we will check all our subnets that include the
target IP and will preserve the source address if it is from
such subnet. If there is no such subnet we select source
address according to the rules for level 2.
2 - Always use the best local address for this target.
In this mode we ignore the source address in the IP packet
and try to select local address that we prefer for talks with
the target host. Such local address is selected by looking
for primary IP addresses on all our subnets on the outgoing
interface that include the target IP address. If no suitable
local address is found we select the first local address
we have on the outgoing interface or on all other interfaces,
with the hope we will receive reply for our request and
even sometimes no matter the source IP address we announce.The max value from conf/{all,interface}/arp_announce is used.Increasing the restriction level gives more chance for
receiving answer from the resolved target while decreasing
the level announces more valid sender's information.

arp_ignore - INTEGER

Define different modes for sending replies in response to
received ARP requests that resolve local target IP addresses:
0 - (default): reply for any local target IP address, configured
on any interface
1 - reply only if the target IP address is local address
configured on the incoming interface
2 - reply only if the target IP address is local address
configured on the incoming interface and both with the
sender's IP address are part from same subnet on this interface
3 - do not reply for local addresses configured with scope host,
only resolutions for global and link addresses are replied
4-7 - reserved
8 - do not reply for all local addressesThe max value from conf/{all,interface}/arp_ignore is used
when ARP request is received on the {interface}

arp_notify - BOOLEAN

Define mode for notification of address and device changes.
0 - (default): do nothing
1 - Generate gratuitous arp requests when device is brought upor hardware address changes.

arp_accept - BOOLEAN

Define behavior for gratuitous ARP frames who's IP is not
already present in the ARP table:
0 - don't create new entries in the ARP table
1 - create new entries in the ARP tableBoth replies and requests type gratuitous arp will trigger the
ARP table to be updated, if this setting is on.If the ARP table already contains the IP address of the
gratuitous arp frame, the arp table will be updated regardless
if this setting is on or off.

参考文档

  • Linux Kernel ip sysctl documentation

Linux网络:内核中的网络参数(net.xxx.xxx)相关推荐

  1. linux netstat Netstat是在内核中访问网络连接状态及其相关信息的程序,它能提供TCP连接,TCP和UDP监听,进程内存管理的相关报告。

    在Internet RFC标准中,Netstat的定义是: Netstat是在内核中访问网络连接状态及其相关信息的程序,它能提供TCP连接,TCP和UDP监听,进程内存管理的相关报告. Netstat ...

  2. linux系统内核文百科,Linux之内核中的文件系统 -电脑资料

    文件描述符 一般说起文件和文件系统的时候,都会下意识的想到它们存在于磁盘上,管理各种文件呢?即运行时文件系统在内核中的表示. 我们知道,进程是操作系统分配资源的基本单位,文件也是在进程中被处理的.比如 ...

  3. 非常易用的网络路径中的网络延迟测试和监控以及自动发送报告工具

    非常易用的网络路径中的网络延迟测试和监控以及自动发送报告工具. 作为网络管理员或网络工程师,时刻关注网络的交付速度至关重要.不仅需要确保自己有良好的响应时间,还需要确保网络的速度足以满足用户通信所需的 ...

  4. python网络通信传输的数据类型_Python网络编程中的网络数据和网络错误。

    上一个章节我们说的是套接字名和DNS.这篇文章我们主要解决下面问题. 我们在两台主机之间建立与关闭TCP流连接以及UDP数据报连接后.我们应该怎么准备我们需要传输的数据,该怎么对数据进行编码与格式化. ...

  5. Neutron VxLAN + Linux Bridge 环境中的网络 MTU

    1. 基础知识 1.1 MTU 一个网络接口的 MTU 是它一次所能传输的最大数据块的大小.任何超过MTU的数据块都会在传输前分成小的传输单元.MTU 有两个测量层次:网络层和链路层.比如,网络层上标 ...

  6. linux 保留内核中sas驱动的加载导致crash问题

    [root@localhost ~]# uname -a Linux localhost.localdomain 3.10.0-693.5.2.el7.x86_64 问题描述,在crash的时候,小内 ...

  7. 如何建立网络监控中的网络性能基线?

    网络性能基线是网络性能监控中用来定义企业网络基础架构正常工作条件的一个衡量标准.工程师可以使用网络性能基线和网络监控进行比较,从而发现可能导致问题的流量变化.一个网络性能的好坏,直接影响到用户的体验和 ...

  8. linux在内核中设置开机logo

    1.获取一张16色的bmp格式的logo图片,其他格式可用:可牛影像,ps等工具转换成bmp格式 2.制作ppm格式 ubuntu安装:netpbm工具 sudo apt-get install ne ...

  9. 内核中接收网络帧的处理

    我这里描述的只是2层的处理. 首先,我们来看softnet_data这个结构,每个cpu都有这样的一个队列,它主要是用来存储incoming frame.由于他是每个cpu都有一个队列,因此在不同的c ...

最新文章

  1. Java Day01-2
  2. Python 函数参数的分类及使用方法
  3. 【原创】MySQL 返回更新值(RETURNING)
  4. 为什么我十分喜欢C,却很不喜欢C++
  5. oracle客户端更改字符集,oracle 服务器跟客户端字符集的查看和修改
  6. 心语收集12:我以为要是唱的用心良苦,你就会对我多点在乎
  7. BZOJ5329:[SDOI2018]战略游戏(圆方树,虚树)
  8. 388.文件的最长绝对路径
  9. Linux配置开机自动挂载WindowsNTFS硬盘分区
  10. IPTV的开展在广电企业、电信运营商角度上的异同
  11. 周爱民-javascript
  12. 购房选址分析之上海市
  13. 在计算机中正确的邮件写信格式,电子邮件格式怎么写
  14. 回收戴尔R740 R740XD - CSDN
  15. 宏碁传奇Go评测 怎么样
  16. 利用线性回归实现股票预测分析
  17. 虚拟机防火墙以及端口操作
  18. 多音字重组成新的词语句子
  19. 【python小课堂专栏】python小课堂11 - 变量篇
  20. Android rom开发:自定义序列号ro.serialno

热门文章

  1. 错误记录( 六)tomcat 配置图片虚拟路径不起作用
  2. Java多线程学习四十二:有哪些解决死锁问题的策略和哲学家就餐问题
  3. 关于MongoDB内存占用不断上升,导致OOM问题
  4. Kafka Manager界面添加Partition
  5. VBA精彩代码分享-4
  6. 浅谈FFT(快速博立叶变换)学习笔记
  7. homebrew 安装 formula 的不同历史版本——以安装 node 为例
  8. 单列布局、两列布局、三列布局
  9. 矢量图和Word:EPS,PDF,EMF和SVG
  10. 基于MSP430F413水果电池供电的低功耗时钟