Red Hat Training
A Red Hat training course is available for RHEL 8
47.6. 在 nftables 命令中使用 verdict 映射
判决映射(也称为字典),使 nft
能够通过将匹配条件映射到某个操作来根据数据包信息执行操作。
47.6.1. 在 nftables 中使用匿名映射
匿名映射是直接在规则中使用的 { match_criteria : action }
语句。这个语句可以包含多个用逗号分开的映射。
匿名映射的缺点是,如果要修改映射,则必须替换规则。对于动态解决方案,请使用命名映射,如 在 nftables 中使用命名映射 中所述。
例如,您可以使用匿名映射将 IPv4 和 IPv6 协议的 TCP 和 UDP 数据包路由到不同的链,以分别计算传入的 TCP 和 UDP 数据包。
流程
创建新表:
# nft add table inet example_table
在
example_table
中创建tcp_packets
链:# nft add chain inet example_table tcp_packets
向统计此链中流量的
tcp_packets
中添加一条规则:# nft add rule inet example_table tcp_packets counter
在
example_table
中创建udp_packets
链# nft add chain inet example_table udp_packets
向统计此链中流量的
udp_packets
中添加一条规则:# nft add rule inet example_table udp_packets counter
为传入的流量创建一个链。例如,要在过滤传入的流量的
example_table
中创建一个名为incoming_traffic
的链:# nft add chain inet example_table incoming_traffic { type filter hook input priority 0 \; }
添加一条带有到
incoming_traffic
匿名映射的规则 :# nft add rule inet example_table incoming_traffic ip protocol vmap { tcp : jump tcp_packets, udp : jump udp_packets }
匿名映射区分数据包,并根据它们的协议将它们发送到不同的计数链。
要列出流量计数器,请显示
example_table
:# nft list table inet example_table table inet example_table { chain tcp_packets { counter packets 36379 bytes 2103816 } chain udp_packets { counter packets 10 bytes 1559 } chain incoming_traffic { type filter hook input priority filter; policy accept; ip protocol vmap { tcp : jump tcp_packets, udp : jump udp_packets } } }
tcp_packets
和udp_packets
链中的计数器显示两者接收的数据包和字节数。