nf_conntrack之解决方案

本文介绍了解决Linux系统中nf_conntrack表满导致丢包的问题,详细解释了nf_conntrack的工作原理,并提供了一套完整的排查及优化方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.现象

在/var/log/message中出现以下信息

Dec  8 11:22:29 product08 kernel: nf_conntrack: table full, dropping packet.
Dec  8 11:22:29 product08 kernel: nf_conntrack: table full, dropping packet.

2.nf_conntrack是什么?

nf_conntrack(在老版本的 Linux 内核中叫 ip_conntrack)是一个内核模块,用于跟踪一个连接的状态的。连接状态跟踪可以供其他模块使用,最常见的两个使用场景是 iptables 的 nat 的 state 模块。
iptables 的 nat 通过规则来修改目的/源地址,但光修改地址不行,我们还需要能让回来的包能路由到最初的来源主机。这就需要借助 nf_conntrack 来找到原来那个连接的记录才行。
而 state 模块则是直接使用 nf_conntrack 里记录的连接的状态来匹配用户定义的相关规则。例如下面这条 INPUT 规则用于放行 80 端口上的状态为 NEW 的连接上的包。

iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

3.解决方案

(1)先关掉iptables

/etc/init.d/iptables stop

(2)查看当前的连接数:

# grep nf_conntrack /proc/slabinfo

(3)查出目前 nf_conntrack 的排名:

$ cat /proc/net/nf_conntrack | cut -d ' ' -f 10 | cut -d '=' -f 2 | sort | uniq -c | sort -nr | head -n 10

(4)优化参数

状态跟踪表的最大行数的设定,理论最大值 CONNTRACK_MAX = RAMSIZE (in bytes) / 16384 / (ARCH / 32)
以64G的64位操作系统为例,CONNTRACK_MAX = 64*1024*1024*1024/16384/2 = 2097152

即时生效请执行:
sysctl –w net.netfilter.nf_conntrack_max = 524288 (16G)

其哈希表大小通常为总表的1/8,最大为1/2。CONNTRACK_BUCKETS = CONNTRACK_MAX / 8
同样64G的64位操作系统,哈希最佳范围是 262144 ~ 1048576 。

运行状态中通过 sysctl net.netfilter.nf_conntrack_buckets 进行查看,通过文件 /sys/module/nf_conntrack/parameters/hashsize 进行设置
或者新建 /etc/modprobe.d/iptables.conf ,重新加载模块才生效:
options nf_conntrack hashsize = 262144

还有些相关的系统参数`sysctl -a | grep nf_conntrack`可以调优(/etc/sysctl.conf ):
net.netfilter.nf_conntrack_max = 1048576
net.netfilter.ip_conntrack_tcp_timeout_established = 3600
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120

(5)iptables放行

iptables -t raw -A PREROUTING -p tcp -m multiport --dports 80,15000 -j NOTRACK
iptables -t raw -A PREROUTING -p tcp -m multiport --sports 80,15000 -j NOTRACK
iptables -t raw -A OUTPUT -p tcp -m multiport --dports 80,15000 -j NOTRACK
iptables -t raw -A OUTPUT -p tcp -m multiport --sports 80,15000 -j NOTRACK

 

转载于:https://www.cnblogs.com/tongxiaoda/p/7418699.html

struct nf_conn { /* Usage count in here is 1 for hash table, 1 per skb, * plus 1 for any connection(s) we are `master' for * * Hint, SKB address this struct and refcnt via skb->_nfct and * helpers nf_conntrack_get() and nf_conntrack_put(). * Helper nf_ct_put() equals nf_conntrack_put() by dec refcnt, * beware nf_ct_get() is different and don't inc refcnt. */ struct nf_conntrack ct_general; spinlock_t lock; u16 cpu; #if defined(CONFIG_BCM_KF_NETFILTER) struct bcm_nf_conn_ext bcm_ext; #endif #ifdef CONFIG_NF_CONNTRACK_ZONES struct nf_conntrack_zone zone; #endif /* XXX should I move this to the tail ? - Y.K */ /* These are my tuples; original and reply */ struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX]; /* Have we seen traffic both ways yet? (bitset) */ unsigned long status; /* jiffies32 when this ct is considered dead */ u32 timeout; possible_net_t ct_net; #if IS_ENABLED(CONFIG_NF_NAT) struct hlist_node nat_bysource; #endif /* all members below initialized via memset */ struct { } __nfct_init_offset; /* If we were expected by an expectation, this will be it */ struct nf_conn *master; //#if defined(CONFIG_NF_CONNTRACK_MARK) u_int32_t mark; //#endif #ifdef CONFIG_TP_IMAGE u_int32_t wportal_flag; #if defined(CONFIG_TP_AVIRA_PATCH) || defined(CONFIG_TP_SPT_PATCH) u_int8_t type_check_state; #endif #endif #ifdef CONFIG_NF_CONNTRACK_SECMARK u_int32_t secmark; #endif #if defined(CONFIG_NETFILTER_APP_DPI) struct APP_MARK dpi_check_state; #endif #ifdef CONFIG_NETFILTER_APP_DPI /* whether this packet should be skipped by hnat */ __u8 hnat_skip; #endif /* Extensions */ struct nf_ct_ext *ext; /* Storage reserved for other modules, must be the last member */ union nf_conntrack_proto proto; }; 没有start该怎么办?
最新发布
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值