| 1 | # Porting iptables-based components to nftables |
| 2 | |
| 3 | Although it's possible to port an iptables-based component to nftables |
| 4 | in a mostly "dumb" way that just translates the iptables chains to |
| 5 | nftables, this isn't really encouraged, and knftables isn't designed |
| 6 | to necessarily make that easy. |
| 7 | |
| 8 | (There is no particular organization to this document; it's basically |
| 9 | a braindump.) |
| 10 | |
| 11 | ## NFTables tables are private |
| 12 | |
| 13 | In iptables, everyone has to share the same `filter`, `nat`, `raw`, |
| 14 | and `mangle` tables, and has to figure out how to not interfere with |
| 15 | each other. |
| 16 | |
| 17 | In nftables, everyone is expected to create their own table, and _not |
| 18 | touch_ anyone else's tables. You use the `priority` levels of base |
| 19 | chains to control what happens before and after what. |
| 20 | |
| 21 | ## Sets and Maps |
| 22 | |
| 23 | NFTables sets and maps are awesome, and you should try to use them. |
| 24 | |
| 25 | IPTables style: |
| 26 | |
| 27 | ``` |
| 28 | -A KUBE-SERVICES -m comment --comment "ns1/svc1:p80 cluster IP" -m tcp -p tcp -d 172.30.0.41 --dport 80 -j KUBE-SVC-XPGD46QRK7WJZT7O |
| 29 | -A KUBE-SERVICES -m comment --comment "ns2/svc2:p80 cluster IP" -m tcp -p tcp -d 172.30.0.42 --dport 80 -j KUBE-SVC-GNZBNJ2PO5MGZ6GT |
| 30 | -A KUBE-SERVICES -m comment --comment "ns2/svc2:p80 external IP" -m tcp -p tcp -d 192.168.99.22 --dport 80 -j KUBE-EXT-GNZBNJ2PO5MGZ6GT |
| 31 | -A KUBE-SERVICES -m comment --comment "ns2/svc2:p80 loadbalancer IP" -m tcp -p tcp -d 1.2.3.4 --dport 80 -j KUBE-EXT-GNZBNJ2PO5MGZ6GT |
| 32 | -A KUBE-SERVICES -m comment --comment "ns3/svc3:p80 cluster IP" -m tcp -p tcp -d 172.30.0.43 --dport 80 -j KUBE-SVC-X27LE4BHSL4DOUIK |
| 33 | -A KUBE-SERVICES -m comment --comment "ns4/svc4:p80 cluster IP" -m tcp -p tcp -d 172.30.0.44 --dport 80 -j KUBE-SVC-4SW47YFZTEDKD3PK |
| 34 | -A KUBE-SERVICES -m comment --comment "ns4/svc4:p80 external IP" -m tcp -p tcp -d 192.168.99.33 --dport 80 -j KUBE-EXT-4SW47YFZTEDKD3PK |
| 35 | -A KUBE-SERVICES -m comment --comment "ns5/svc5:p80 cluster IP" -m tcp -p tcp -d 172.30.0.45 --dport 80 -j KUBE-SVC-NUKIZ6OKUXPJNT4C |
| 36 | -A KUBE-SERVICES -m comment --comment "ns5/svc5:p80 loadbalancer IP" -m tcp -p tcp -d 5.6.7.8 --dport 80 -j KUBE-FW-NUKIZ6OKUXPJNT4C |
| 37 | ``` |
| 38 | |
| 39 | NFTables style: |
| 40 | |
| 41 | ``` |
| 42 | add chain ip kube-proxy services |
| 43 | add rule ip kube-proxy services ip daddr . meta l4proto . th dport vmap @service-ips |
| 44 | |
| 45 | add element ip kube-proxy service-ips { 172.30.0.41 . tcp . 80 : goto service-ULMVA6XW-ns1/svc1/tcp/p80 } |
| 46 | add element ip kube-proxy service-ips { 172.30.0.42 . tcp . 80 : goto service-42NFTM6N-ns2/svc2/tcp/p80 } |
| 47 | add element ip kube-proxy service-ips { 192.168.99.22 . tcp . 80 : goto external-42NFTM6N-ns2/svc2/tcp/p80 } |
| 48 | add element ip kube-proxy service-ips { 1.2.3.4 . tcp . 80 : goto external-42NFTM6N-ns2/svc2/tcp/p80 } |
| 49 | add element ip kube-proxy service-ips { 172.30.0.43 . tcp . 80 : goto service-4AT6LBPK-ns3/svc3/tcp/p80 } |
| 50 | add element ip kube-proxy service-ips { 172.30.0.44 . tcp . 80 : goto service-LAUZTJTB-ns4/svc4/tcp/p80 } |
| 51 | add element ip kube-proxy service-ips { 192.168.99.33 . tcp . 80 : goto external-LAUZTJTB-ns4/svc4/tcp/p80 } |
| 52 | add element ip kube-proxy service-ips { 172.30.0.45 . tcp . 80 : goto service-HVFWP5L3-ns5/svc5/tcp/p80 } |
| 53 | add element ip kube-proxy service-ips { 5.6.7.8 . tcp . 80 : goto external-HVFWP5L3-ns5/svc5/tcp/p80 } |
| 54 | ``` |
| 55 | |
| 56 | The nftables ruleset is O(1)-ish; there is a single rule, regardless |
| 57 | of how many services you have. The iptables ruleset is O(n); the more |
| 58 | services you have, the more rules netfilter has to run against each |
| 59 | packet. |
| 60 | |
| 61 | ## Static vs dynamic rule sets |
| 62 | |
| 63 | Related to the above, iptables-based components tend to need to |
| 64 | frequently add and delete individual rules. In nftables, it is more |
| 65 | common to have a static set of rules (e.g., the single map lookup rule |
| 66 | above), and have dynamically modified sets and maps. |
| 67 | |
| 68 | In iptables, you can delete a rule by using the same syntax you used |
| 69 | to create it. In nftables, this isn't possible; if you want to delete |
| 70 | a rule, you need to know the current index or the static handle of the |
| 71 | rule. This makes dynamically altering rules slightly more complicated |
| 72 | (and, at the time of writing, knftables doesn't provide any way to |
| 73 | learn the handle of a rule at the time the rule is created). |
| 74 | |
| 75 | As a result of the above (and the fact that you can safely assume that |
| 76 | other components aren't adding rules to your tables), in idiomatic |
| 77 | knftables usage, when you aren't sure that the current state of the |
| 78 | chain is correct, it tends to be more common to flush-and-recreate the |
| 79 | chain rather than checking whether individual rules exist in it and are |
| 80 | correct. |
| 81 | |
| 82 | ## Use of comments |
| 83 | |
| 84 | In iptables, comments only exist on rules. In nftables, you can add |
| 85 | comments to: |
| 86 | |
| 87 | - rules |
| 88 | - set/map elements |
| 89 | - chains |
| 90 | - sets |
| 91 | - maps |
| 92 | - tables |
| 93 | |
| 94 | When porting kube-proxy, we found that in many places where kube-proxy |
| 95 | had per-rule comments, we really only needed per-chain comments, which |
| 96 | explained all of the rules in the chain. |
| 97 | |
| 98 | (Additionally, we ended up getting rid of a lot of comments just |
| 99 | because we use much longer and more-self-documenting chain names in |
| 100 | the nftables kube-proxy backend, as seen above. (The maximum length of |
| 101 | an nftables chain name is 256, as compared to iptables's 32.)) |
| 102 | |
| 103 | ## The `inet`, `ip`, and `ip6` tables |
| 104 | |
| 105 | For dual-stack rulesets, nftables provides two options: |
| 106 | |
| 107 | - Put IPv4 rules in an `ip`-family table and IPv6 rules in an `ip6`-family table. |
| 108 | - Put all rules in an `inet`-family table. |
| 109 | |
| 110 | For kube-proxy, the architecture of kube-proxy meant that the split |
| 111 | `ip`/`ip6` option made much more sense. But the split option often |
| 112 | ends up making sense for other components too, because you can't have |
| 113 | sets/maps that hold/match both IPv4 and IPv6 addresses. If you need to |
| 114 | have rules that match particular IPs against a set or map, then you |
| 115 | need to have per-IP-family sets/map regardless of whether the rules |
| 116 | are in `ip`/`ip6` or `inet`. |