大网数据广告活动
服务器运维问答

How to drop packets based on a probability with nftables?

· 52CE 我爱测

iptables has the statistic module that allows doing operations on packets based on a probability, how would I go about and do this with nftables?

问题原文

原帖链接 · Avamander · CC BY-SA 4.0

iptables has the statistic module that allows doing operations on packets based on a probability, how would I go about and do this with nftables?

已采纳解答

原帖链接 · A.B · CC BY-SA 4.0

numgen

nftables has all features in place, but for some unknown reason, it's not documented in any version of the nft manpage.

The kernel side handling this, nft_numgen has been available since Linux 4.9 and the userland side since the nftables 0.7 release:

  • Add number generation support. Useful for round-robin packet mark setting, eg.

    # nft add rule filter prerouting meta mark set numgen inc mod 2
    

    You can also specify an offset to indicate from what value you want to start from.

    The modulus provides the scale of the counting sequence. You can
    also use this from maps, eg.

    # nft add rule nat prerouting \
          dnat to numgen inc mod 2 map { 0 : 192.168.10.100, 1 : 192.168.20.200 }
    

    So this is distributing new connections in a round-robin fashion
    between 192.168.10.100 and 192.168.20.200. Don't forget the special NAT chain semantics: Only the first packet evaluates the rule, follow up packets rely on conntrack to apply the NAT information.

    You can also emulate flow distribution with different backend weights using intervals, eg.

    # nft add rule nat prerouting \
          dnat to numgen inc mod 10 map { 0-5 : 192.168.10.100, 6-9 : 192.168.20.200 }
    

There's also support for random numbers. Example: replacing in a previous example inc (used for round-robin) with random:

nft add rule nat prerouting dnat to numgen random mod 2 map { 0 : 192.168.10.100, 1 : 192.168.20.200 }

Further documentation and related stuff:


Back to the question

The random number is a 32 bits integer (big enough to ignore modulus bias) on which is applied a modulus. Its value can then be compared. To drop 5% of all non-local incoming IPv4 packets that would then be:

#!/usr/sbin/nft -f

flush ruleset

table ip filter {
    chain input {
        type filter hook input priority 0; policy accept;
        iif "lo" accept
        numgen random mod 100 < 5 drop
    }
}

Example when pinging from its host a container with this ruleset:

# ping -q -f 10.0.3.66
PING 10.0.3.66 (10.0.3.66) 56(84) bytes of data.
^C
--- 10.0.3.66 ping statistics ---
34351 packets transmitted, 32592 received, 5% packet loss, time 19744ms
rtt min/avg/max/mdev = 0.004/0.017/0.481/0.018 ms, ipg/ewma 0.574/0.017 ms

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-06-18)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。