Allow offloading of UDP NEW connections via act_ct
Subsystems (6)
v3 cover Allow offloading of UDP NEW connections via act_ct
Currently only bidirectional established connections can be offloaded via act_ct. Such approach allows to hardcode a lot of assumptions into act_ct, flow_table and flow_offload intermediate layer codes. In order to enabled offloading of unidirectional UDP NEW connections start with incrementally changing the following assumptions:
- Drivers assume that only established connections are offloaded and don't support updating existing connections. Extract ctinfo from meta action cookie and refuse offloading of new connections in the drivers.
- Fix flow_table offload fixup algorithm to calculate flow timeout according to current connection state instead of hardcoded "established" value.
- Add new flow_table flow flag that designates bidirectional connections instead of assuming it and hardcoding hardware offload of every flow in both directions.
- Add new flow_table flow flag that marks the flow for asynchronous update. Hardware offload state of such flows is updated by gc task by leveraging existing flow 'refresh' code.
With all the necessary infrastructure in place modify act_ct to offload UDP NEW as unidirectional connection. Pass reply direction traffic to CT and promote connection to bidirectional when UDP connection state changes to "assured". Rely on refresh mechanism to propagate connection state change to supporting drivers.
Note that early drop algorithm that is designed to free up some space in connection tracking table when it becomes full (by randomly deleting up to 5% of non-established connections) currently ignores connections marked as "offloaded". Now, with UDP NEW connections becoming "offloaded" it could allow malicious user to perform DoS attack by filling the table with non-droppable UDP NEW connections by sending just one packet in single direction. To prevent such scenario change early drop algorithm to also consider "offloaded" connections for deletion.
Vlad Buslov (7):
net: flow_offload: provision conntrack info in ct_metadata
netfilter: flowtable: fixup UDP timeout depending on ct state
netfilter: flowtable: allow unidirectional rules
netfilter: flowtable: allow updating offloaded rules asynchronously net/sched: act_ct: set ctinfo in meta action depending on ct state net/sched: act_ct: offload UDP NEW connections
netfilter: nf_conntrack: allow early drop of offloaded UDP conns
.../ethernet/mellanox/mlx5/core/en/tc_ct.c | 4 +- .../ethernet/netronome/nfp/flower/conntrack.c | 24 ++++++++ include/net/netfilter/nf_flow_table.h | 4 +- net/netfilter/nf_conntrack_core.c | 11 ++-- net/netfilter/nf_flow_table_core.c | 25 +++++++-- net/netfilter/nf_flow_table_offload.c | 17 ++++-- net/sched/act_ct.c | 55 ++++++++++++++----- 7 files changed, 107 insertions(+), 33 deletions(-)
Signature · 1 line
2.38.1
PATCH v3 1/7 net: flow_offload: provision conntrack info in ct_metadata
In order to offload connections in other states besides "established" the driver offload callbacks need to have access to connection conntrack info. Flow offload intermediate representation data structure already contains that data encoded in 'cookie' field, so just reuse it in the drivers.
Reject offloading IP_CT_NEW connections for now by returning an error in relevant driver callbacks based on value of ctinfo. Support for offloading such connections will need to be added to the drivers afterwards.
---
Notes:
Changes V2 -> V3:
- Reuse existing meta action 'cookie' field to obtain ctinfo instead of introducing a new field as suggested by Marcelo.
Changes V1 -> V2:
- Add missing include that caused compilation errors on certain configs.
- Change naming in nfp driver as suggested by Simon and Baowen.
.../ethernet/mellanox/mlx5/core/en/tc_ct.c | 4 +++- .../ethernet/netronome/nfp/flower/conntrack.c | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c +3 -1
@@ -1073,11 +1073,13 @@ mlx5_tc_ct_block_flow_offload_add(struct mlx5_ct_ft *ft,struct mlx5_tc_ct_priv *ct_priv = ft->ct_priv;struct flow_action_entry *meta_action;unsigned long cookie = flow->cookie; +enum ip_conntrack_info ctinfo;struct mlx5_ct_entry *entry;int err; meta_action =mlx5_tc_ct_get_ct_metadata_action (flow_rule); -if (!meta_action) + ctinfo = meta_action->ct_metadata.cookie &NFCT_INFOMASK ; +if (!meta_action || ctinfo ==IP_CT_NEW )return -EOPNOTSUPP ;spin_lock_bh (&ct_priv->ht_lock);
drivers/net/ethernet/netronome/nfp/flower/conntrack.c +24 -0
@@ -1964,6 +1964,27 @@ int nfp_fl_ct_stats(struct flow_cls_offload *flow,return 0 ; } +static bool +nfp_fl_ct_offload_nft_supported (struct flow_cls_offload *flow) +{ +struct flow_rule *flow_rule = flow->rule; +struct flow_action *flow_action = + &flow_rule->action; +struct flow_action_entry *act; +int i; + +flow_action_for_each (i, act, flow_action) { +if (act->id ==FLOW_ACTION_CT_METADATA ) { +enum ip_conntrack_info ctinfo = + act->ct_metadata.cookie &NFCT_INFOMASK ; + +return ctinfo !=IP_CT_NEW ; + } + } + +return false; +} +static int nfp_fl_ct_offload_nft_flow (struct nfp_fl_ct_zone_entry *zt,struct flow_cls_offload *flow) {
@@ -1976,6 +1997,9 @@ nfp_fl_ct_offload_nft_flow(struct nfp_fl_ct_zone_entry *zt, struct flow_cls_offl extack = flow->common.extack;switch (flow->command) {case FLOW_CLS_REPLACE : +if (!nfp_fl_ct_offload_nft_supported (flow)) +return -EOPNOTSUPP ; +/* Netfilter can request offload multiple times for the same * flow - protect against adding duplicates. */
Signature · 1 line
2.38.1
PATCH v3 2/7 netfilter: flowtable: fixup UDP timeout depending on ct state
In reply to Vlad BuslovCurrently flow_offload_fixup_ct() function assumes that only replied UDP connections can be offloaded and hardcodes UDP_CT_REPLIED timeout value. To enable UDP NEW connection offload in following patches extract the actual connections state from ct->status and set the timeout according to it.
--- net/netfilter/nf_flow_table_core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/netfilter/nf_flow_table_core.c +4 -1
@@ -193,8 +193,11 @@ static void flow_offload_fixup_ct(struct nf_conn *ct) timeout -= tn->offload_timeout; }else if (l4num ==IPPROTO_UDP ) {struct nf_udp_net *tn =nf_udp_pernet (net); +enum udp_conntrack state = +test_bit (IPS_SEEN_REPLY_BIT , &ct->status) ? +UDP_CT_REPLIED :UDP_CT_UNREPLIED ; - timeout = tn->timeouts[UDP_CT_REPLIED ]; + timeout = tn->timeouts[state]; timeout -= tn->offload_timeout; }else {return ;
Signature · 1 line
2.38.1
Pablo Neira Ayuso
maintainerRe: [PATCH net-next v3 2/7] netfilter: flowtable: fixup UDP timeout depending on ct state
Quoted 15 lines · Vlad Buslov
Currently
flow_offload_fixup_ct()function assumes that only replied UDP connections can be offloaded and hardcodesUDP_CT_REPLIEDtimeout value. To enable UDP NEW connection offload in following patches extract the actual connections state fromct->statusand set the timeout according to it.Signed-off-by Vlad Buslov <[email protected]>--- net/netfilter/nf_flow_table_core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/netfilter/nf_flow_table_core.c +0
-0
net/netfilter/nf_flow_table_core.c static void flow_offload_fixup_ct(struct nf_conn *ct) · line 201
Quoted 10 lines
net/netfilter/nf_flow_table_core.c +4
-1@@ -193,8 +193,11 @@ static void flow_offload_fixup_ct(struct nf_conn *ct) timeout -= tn->offload_timeout; }else if (l4num ==IPPROTO_UDP ) {struct nf_udp_net *tn =nf_udp_pernet (net); +enum udp_conntrack state = +test_bit (IPS_SEEN_REPLY_BIT , &ct->status) ? +UDP_CT_REPLIED :UDP_CT_UNREPLIED ; - timeout = tn->timeouts[UDP_CT_REPLIED ]; + timeout = tn->timeouts[state]; timeout -= tn->offload_timeout;
For netfilter's flowtable (not talking about act_ct), this is a "problem" because the flowtable path update with ct->status flags. In other words, for netfilter's flowtable UDP_CT_UNREPLIED timeout will be always used for UDP traffic if it is offloaded and no traffic from the classic path was seen.
If packets go via hardware offload, the host does not see packets in the reply direction (unless hardware counters are used to set on IPS_SEEN_REPLY_BIT?).
Then, there is also IPS_ASSURED: Netfilter's flowtable assumes that TCP flows are only offloaded to hardware if IPS_ASSURED.
Re: [PATCH net-next v3 2/7] netfilter: flowtable: fixup UDP timeout depending on ct state
Quoted 23 lines · Pablo Neira Ayuso
Quoted 22 lines · Vlad Buslov
Currently
flow_offload_fixup_ct()function assumes that only replied UDP connections can be offloaded and hardcodesUDP_CT_REPLIEDtimeout value. To enable UDP NEW connection offload in following patches extract the actual connections state fromct->statusand set the timeout according to it.Signed-off-by Vlad Buslov <[email protected]>--- net/netfilter/nf_flow_table_core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/netfilter/nf_flow_table_core.c +3
-0@@ -193,8 +193,11 @@ static void flow_offload_fixup_ct(struct nf_conn *ct) timeout -= tn->offload_timeout; }else if (l4num ==IPPROTO_UDP ) {struct nf_udp_net *tn =nf_udp_pernet (net); +enum udp_conntrack state = +test_bit (IPS_SEEN_REPLY_BIT , &ct->status) ? +UDP_CT_REPLIED :UDP_CT_UNREPLIED ;
Quoted 9 lines
net/netfilter/nf_flow_table_core.c static void flow_offload_fixup_ct(struct nf_conn *ct) · line 201
Quoted 3 lines
net/netfilter/nf_flow_table_core.c +1
-1- timeout = tn->timeouts[UDP_CT_REPLIED ]; + timeout = tn->timeouts[state]; timeout -= tn->offload_timeout;For netfilter's flowtable (not talking about
act_ct), this is a "problem" because the flowtable path update withct->statusflags. In other words, for netfilter's flowtableUDP_CT_UNREPLIEDtimeout will be always used for UDP traffic if it is offloaded and no traffic from the classic path was seen.
Hmm, I didn't consider that netfilter might not update the status. Will try to decouple the teardown timeout calculation and allow flow_table users to specify their own.
Quoted 7 lines
If packets go via hardware offload, the host does not see packets in the reply direction (unless hardware counters are used to set on
IPS_SEEN_REPLY_BIT?).Then, there is also
IPS_ASSURED: Netfilter's flowtable assumes that TCP flows are only offloaded to hardware ifIPS_ASSURED.
PATCH v3 3/7 netfilter: flowtable: allow unidirectional rules
In reply to Vlad BuslovModify flow table offload to support unidirectional connections by extending enum nf_flow_flags with new "NF_FLOW_HW_BIDIRECTIONAL" flag. Only offload reply direction when the flag is set. This infrastructure change is necessary to support offloading UDP NEW connections in original direction in following patches in series.
---
Notes:
Changes V2 -> V3:
- Fix error in commit message (spotted by Marcelo).
include/net/netfilter/nf_flow_table.h | 1 + net/netfilter/nf_flow_table_offload.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-)
include/net/netfilter/nf_flow_table.h +1 -0
@@ -164,6 +164,7 @@ enum nf_flow_flags {NF_FLOW_HW_DYING ,NF_FLOW_HW_DEAD ,NF_FLOW_HW_PENDING , + NF_FLOW_HW_BIDIRECTIONAL, }; enum flow_offload_type {
net/netfilter/nf_flow_table_offload.c +8 -4
@@ -895,8 +895,9 @@ static int flow_offload_rule_add(struct flow_offload_work *offload, ok_count +=flow_offload_tuple_add (offload, flow_rule[0 ],FLOW_OFFLOAD_DIR_ORIGINAL ); - ok_count +=flow_offload_tuple_add (offload, flow_rule[1 ], -FLOW_OFFLOAD_DIR_REPLY ); +if (test_bit (NF_FLOW_HW_BIDIRECTIONAL , &offload->flow->flags)) + ok_count +=flow_offload_tuple_add (offload, flow_rule[1 ], +FLOW_OFFLOAD_DIR_REPLY );if (ok_count ==0 )return -ENOENT ;
@@ -926,7 +927,8 @@ static void flow_offload_work_del(struct flow_offload_work *offload) {clear_bit (IPS_HW_OFFLOAD_BIT , &offload->flow->ct->status);flow_offload_tuple_del (offload,FLOW_OFFLOAD_DIR_ORIGINAL ); -flow_offload_tuple_del (offload,FLOW_OFFLOAD_DIR_REPLY ); +if (test_bit (NF_FLOW_HW_BIDIRECTIONAL , &offload->flow->flags)) +flow_offload_tuple_del (offload,FLOW_OFFLOAD_DIR_REPLY );set_bit (NF_FLOW_HW_DEAD , &offload->flow->flags); }
@@ -946,7 +948,9 @@ static void flow_offload_work_stats(struct flow_offload_work *offload)u64 lastused;flow_offload_tuple_stats (offload,FLOW_OFFLOAD_DIR_ORIGINAL , &stats[0 ]); -flow_offload_tuple_stats (offload,FLOW_OFFLOAD_DIR_REPLY , &stats[1 ]); +if (test_bit (NF_FLOW_HW_BIDIRECTIONAL , &offload->flow->flags)) +flow_offload_tuple_stats (offload,FLOW_OFFLOAD_DIR_REPLY , + &stats[1 ]); lastused =max_t (u64, stats[0 ].lastused, stats[1 ].lastused); offload->flow->timeout = max_t(u64, offload->flow->timeout,
Signature · 1 line
2.38.1
PATCH v3 4/7 netfilter: flowtable: allow updating offloaded rules asynchronously
In reply to Vlad BuslovFollowing patches in series need to update flowtable rule several times during its lifetime in order to synchronize hardware offload with actual ct status. However, reusing existing 'refresh' logic in act_ct would cause data path to potentially schedule significant amount of spurious tasks in 'add' workqueue since it is executed per-packet. Instead, introduce a new flow 'update' flag and use it to schedule async flow refresh in flowtable gc which will only be executed once per gc iteration.
--- include/net/netfilter/nf_flow_table.h | 3 ++- net/netfilter/nf_flow_table_core.c | 20 +++++++++++++++----- net/netfilter/nf_flow_table_offload.c | 5 +++-- 3 files changed, 20 insertions(+), 8 deletions(-)
include/net/netfilter/nf_flow_table.h +2 -1
@@ -165,6 +165,7 @@ enum nf_flow_flags {NF_FLOW_HW_DEAD ,NF_FLOW_HW_PENDING ,NF_FLOW_HW_BIDIRECTIONAL , + NF_FLOW_HW_UPDATE, }; enum flow_offload_type {
@@ -300,7 +301,7 @@ unsigned int nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,#define MODULE_ALIAS_NF_FLOWTABLE (family) \ MODULE_ALIAS("nf-flowtable-" __stringify(family)) -void nf_flow_offload_add (struct nf_flowtable *flowtable, +bool nf_flow_offload_add (struct nf_flowtable *flowtable,struct flow_offload *flow);void nf_flow_offload_del (struct nf_flowtable *flowtable,struct flow_offload *flow);
net/netfilter/nf_flow_table_core.c +15 -5
@@ -316,21 +316,28 @@ int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow) }EXPORT_SYMBOL_GPL (flow_offload_add); +static bool __flow_offload_refresh (struct nf_flowtable *flow_table, +struct flow_offload *flow) +{ +if (likely (!nf_flowtable_hw_offload (flow_table))) +return true; + +return nf_flow_offload_add (flow_table, flow); +} +void flow_offload_refresh (struct nf_flowtable *flow_table,struct flow_offload *flow) {u32 timeout; timeout = nf_flowtable_time_stamp +flow_offload_get_timeout (flow); -if (timeout -READ_ONCE (flow->timeout) >HZ ) +if (timeout -READ_ONCE (flow->timeout) >HZ && + !test_bit (NF_FLOW_HW_UPDATE , &flow->flags))WRITE_ONCE (flow->timeout, timeout);else return ; -if (likely (!nf_flowtable_hw_offload (flow_table))) -return ; - -nf_flow_offload_add (flow_table, flow); +__flow_offload_refresh (flow_table, flow); }EXPORT_SYMBOL_GPL (flow_offload_refresh);
@@ -435,6 +442,9 @@ static void nf_flow_offload_gc_step(struct nf_flowtable *flow_table, }else {flow_offload_del (flow_table, flow); } + }else if (test_and_clear_bit (NF_FLOW_HW_UPDATE , &flow->flags)) { +if (!__flow_offload_refresh (flow_table, flow)) +set_bit (NF_FLOW_HW_UPDATE , &flow->flags); }else if (test_bit (NF_FLOW_HW , &flow->flags)) {nf_flow_offload_stats (flow_table, flow); }
net/netfilter/nf_flow_table_offload.c +3 -2
@@ -1036,16 +1036,17 @@ nf_flow_offload_work_alloc(struct nf_flowtable *flowtable, } -void nf_flow_offload_add (struct nf_flowtable *flowtable, +bool nf_flow_offload_add (struct nf_flowtable *flowtable,struct flow_offload *flow) {struct flow_offload_work *offload; offload =nf_flow_offload_work_alloc (flowtable, flow,FLOW_CLS_REPLACE );if (!offload) -return ; +return false;flow_offload_queue_work (offload); +return true; } void nf_flow_offload_del(struct nf_flowtable *flowtable,
Signature · 1 line
2.38.1
Pablo Neira Ayuso
maintainerRe: [PATCH net-next v3 4/7] netfilter: flowtable: allow updating offloaded rules asynchronously
Hi Vlad,
Quoted 7 lines · Vlad Buslov
Following patches in series need to update flowtable rule several times during its lifetime in order to synchronize hardware offload with actual ct status. However, reusing existing 'refresh' logic in
act_ctwould cause data path to potentially schedule significant amount of spurious tasks in 'add' workqueue since it is executed per-packet. Instead, introduce a new flow 'update' flag and use it to schedule async flow refresh in flowtable gc which will only be executed once per gc iteration.
So the idea is to use a NF_FLOW_HW_UPDATE which triggers the update from the garbage collector. I understand the motivation here is to avoid adding more work to the workqueue, by simply letting the gc thread pick up for the update.
I already proposed in the last year alternative approaches to improve the workqueue logic, including cancelation of useless work. For example, cancel a flying "add" work if "delete" just arrive and the work is still sitting in the queue. Same approach could be use for this update logic, ie. cancel an add UDP unidirectional or upgrade it to bidirectional if, by the time we see traffic in both directions, then work is still sitting in the queue.
I am sorry to say but it seems to me this approach based on flags is pushing the existing design to the limit. The flag semantics is already overloaded that this just makes the state machine behind the flag logic more complicated. I really think we should explore for better strategies for the offload work to be processed.
Thanks.
Re: [PATCH net-next v3 4/7] netfilter: flowtable: allow updating offloaded rules asynchronously
Quoted 16 lines · Pablo Neira Ayuso
Hi Vlad,
Quoted 7 lines · Vlad Buslov
Following patches in series need to update flowtable rule several times during its lifetime in order to synchronize hardware offload with actual ct status. However, reusing existing 'refresh' logic in
act_ctwould cause data path to potentially schedule significant amount of spurious tasks in 'add' workqueue since it is executed per-packet. Instead, introduce a new flow 'update' flag and use it to schedule async flow refresh in flowtable gc which will only be executed once per gc iteration.So the idea is to use a
NF_FLOW_HW_UPDATEwhich triggers the update from the garbage collector. I understand the motivation here is to avoid adding more work to the workqueue, by simply letting the gc thread pick up for the update.
Quoted 7 lines
I already proposed in the last year alternative approaches to improve the workqueue logic, including cancelation of useless work. For example, cancel a flying "add" work if "delete" just arrive and the work is still sitting in the queue. Same approach could be use for this update logic, ie. cancel an add UDP unidirectional or upgrade it to bidirectional if, by the time we see traffic in both directions, then work is still sitting in the queue.
Thanks for the suggestion. I'll try to make this work over regular workqueues without further extending the flow flags and/or putting more stuff into gc.
Quoted 6 lines
I am sorry to say but it seems to me this approach based on flags is pushing the existing design to the limit. The flag semantics is already overloaded that this just makes the state machine behind the flag logic more complicated. I really think we should explore for better strategies for the offload work to be processed.
Got it.
Pablo Neira Ayuso
maintainerRe: [PATCH net-next v3 4/7] netfilter: flowtable: allow updating offloaded rules asynchronously
Hi Vlad,
Quoted 26 lines · Vlad Buslov
Quoted 16 lines · Pablo Neira Ayuso
Hi Vlad,
Quoted 7 lines · Vlad Buslov
Following patches in series need to update flowtable rule several times during its lifetime in order to synchronize hardware offload with actual ct status. However, reusing existing 'refresh' logic in
act_ctwould cause data path to potentially schedule significant amount of spurious tasks in 'add' workqueue since it is executed per-packet. Instead, introduce a new flow 'update' flag and use it to schedule async flow refresh in flowtable gc which will only be executed once per gc iteration.So the idea is to use a
NF_FLOW_HW_UPDATEwhich triggers the update from the garbage collector. I understand the motivation here is to avoid adding more work to the workqueue, by simply letting the gc thread pick up for the update.Quoted 7 lines
I already proposed in the last year alternative approaches to improve the workqueue logic, including cancelation of useless work. For example, cancel a flying "add" work if "delete" just arrive and the work is still sitting in the queue. Same approach could be use for this update logic, ie. cancel an add UDP unidirectional or upgrade it to bidirectional if, by the time we see traffic in both directions, then work is still sitting in the queue.
Quoted 3 lines
Thanks for the suggestion. I'll try to make this work over regular workqueues without further extending the flow flags and/or putting more stuff into gc.
Let me make a second pass to sort out thoughts on this.
Either we use regular workqueues (without new flags) or we explore fully consolidating this hardware offload workqueue infrastructure around flags, ie. use flags not only for update events, but also for new and delete.
This would go more in the direction of your _UPDATE flag idea:
- Update the hardware offload workqueue to iterate over the flowtable. The hardware offload workqueue would be "scanning" for entries in the flowtable that require some sort of update in the hardware. The flags would tell what kind of action is needed.
- Add these flags:
NF_FLOW_HW_NEWNF_FLOW_HW_UPDATENF_FLOW_HW_DELETE
and remove the work object (flow_offload_work) and the existing list. If the workqueue finds an entry with:
NEW|DELETE, this means this is short lived flow, not worth to waste cycles to offload it.
NEW|UPDATE, this means this is an UDP flow that is bidirectional.
Then, there will be no more work allocation + "flying" work objects to the hardware offload workqueue. Instead, the hardware offload workqueue will be iterating.
This approach would need _DONE flags to annotate if the offload updates have been applied to hardware already (similar to the conntrack _DONE flags).
(Oh well, this proposal is adding even more flags. But I think flags are not the issue, but the mixture of the existing flow_offload_work approach with this new _UPDATE flag and the gc changes).
If flow_offload_work is removed, we would also need to add a:
struct nf_flowtable *flowtable;
field to the flow_offload entry, which is an entry field that is passed via flow_offload_work. So it is one extra field for the each flow_offload entry.
The other alternative is to use the existing nf_flow_offload_add_wq with UPDATE command, which might result in more flying objects in turn. I think this is what you are trying to avoid with the _UPDATE flag approach.
Thanks.
Re: [PATCH net-next v3 4/7] netfilter: flowtable: allow updating offloaded rules asynchronously
Quoted 76 lines · Pablo Neira Ayuso
Hi Vlad,
Quoted 26 lines · Vlad Buslov
Quoted 16 lines · Pablo Neira Ayuso
Hi Vlad,
Quoted 7 lines · Vlad Buslov
Following patches in series need to update flowtable rule several times during its lifetime in order to synchronize hardware offload with actual ct status. However, reusing existing 'refresh' logic in
act_ctwould cause data path to potentially schedule significant amount of spurious tasks in 'add' workqueue since it is executed per-packet. Instead, introduce a new flow 'update' flag and use it to schedule async flow refresh in flowtable gc which will only be executed once per gc iteration.So the idea is to use a
NF_FLOW_HW_UPDATEwhich triggers the update from the garbage collector. I understand the motivation here is to avoid adding more work to the workqueue, by simply letting the gc thread pick up for the update.Quoted 7 lines
I already proposed in the last year alternative approaches to improve the workqueue logic, including cancelation of useless work. For example, cancel a flying "add" work if "delete" just arrive and the work is still sitting in the queue. Same approach could be use for this update logic, ie. cancel an add UDP unidirectional or upgrade it to bidirectional if, by the time we see traffic in both directions, then work is still sitting in the queue.
Quoted 3 lines
Thanks for the suggestion. I'll try to make this work over regular workqueues without further extending the flow flags and/or putting more stuff into gc.
Let me make a second pass to sort out thoughts on this.
Either we use regular workqueues (without new flags) or we explore fully consolidating this hardware offload workqueue infrastructure around flags, ie. use flags not only for update events, but also for new and delete.
This would go more in the direction of your
_UPDATEflag idea:- Update the hardware offload workqueue to iterate over the flowtable. The hardware offload workqueue would be "scanning" for entries in the flowtable that require some sort of update in the hardware. The flags would tell what kind of action is needed.
- Add these flags:
NF_FLOW_HW_NEWNF_FLOW_HW_UPDATENF_FLOW_HW_DELETEand remove the work object (
flow_offload_work) and the existing list. If the workqueue finds an entry with:NEW|DELETE, this means this is short lived flow, not worth to waste cycles to offload it.
NEW|UPDATE, this means this is an UDP flow that is bidirectional.Then, there will be no more work allocation + "flying" work objects to the hardware offload workqueue. Instead, the hardware offload workqueue will be iterating.
This approach would need
_DONEflags to annotate if the offload updates have been applied to hardware already (similar to the conntrack_DONEflags).(Oh well, this proposal is adding even more flags. But I think flags are not the issue, but the mixture of the existing
flow_offload_workapproach with this new_UPDATEflag and the gc changes).If
flow_offload_workis removed, we would also need to add a:struct
nf_flowtable*flowtable;
Quoted 8 lines
field to the
flow_offloadentry, which is an entry field that is passed viaflow_offload_work. So it is one extra field for the eachflow_offloadentry.The other alternative is to use the existing
nf_flow_offload_add_wqwith UPDATE command, which might result in more flying objects in turn. I think this is what you are trying to avoid with the_UPDATEflag approach.
This looks interesting, but is very ambitious and will probably be a bigger change than this whole series. I have an idea how we can leverage existing 'refresh' mechanism for updating flow state that doesn't involve large-scale refactoring of existing offload infrastructure, which I would prefer to try first. WDYT?
Re: [PATCH net-next v3 4/7] netfilter: flowtable: allow updating offloaded rules asynchronously
Quoted 86 lines · Vlad Buslov
Quoted 76 lines · Pablo Neira Ayuso
Hi Vlad,
Quoted 26 lines · Vlad Buslov
Quoted 16 lines · Pablo Neira Ayuso
Hi Vlad,
Quoted 7 lines · Vlad Buslov
Following patches in series need to update flowtable rule several times during its lifetime in order to synchronize hardware offload with actual ct status. However, reusing existing 'refresh' logic in
act_ctwould cause data path to potentially schedule significant amount of spurious tasks in 'add' workqueue since it is executed per-packet. Instead, introduce a new flow 'update' flag and use it to schedule async flow refresh in flowtable gc which will only be executed once per gc iteration.So the idea is to use a
NF_FLOW_HW_UPDATEwhich triggers the update from the garbage collector. I understand the motivation here is to avoid adding more work to the workqueue, by simply letting the gc thread pick up for the update.Quoted 7 lines
I already proposed in the last year alternative approaches to improve the workqueue logic, including cancelation of useless work. For example, cancel a flying "add" work if "delete" just arrive and the work is still sitting in the queue. Same approach could be use for this update logic, ie. cancel an add UDP unidirectional or upgrade it to bidirectional if, by the time we see traffic in both directions, then work is still sitting in the queue.
Quoted 3 lines
Thanks for the suggestion. I'll try to make this work over regular workqueues without further extending the flow flags and/or putting more stuff into gc.
Let me make a second pass to sort out thoughts on this.
Either we use regular workqueues (without new flags) or we explore fully consolidating this hardware offload workqueue infrastructure around flags, ie. use flags not only for update events, but also for new and delete.
This would go more in the direction of your
_UPDATEflag idea:- Update the hardware offload workqueue to iterate over the flowtable. The hardware offload workqueue would be "scanning" for entries in the flowtable that require some sort of update in the hardware. The flags would tell what kind of action is needed.
- Add these flags:
NF_FLOW_HW_NEWNF_FLOW_HW_UPDATENF_FLOW_HW_DELETEand remove the work object (
flow_offload_work) and the existing list. If the workqueue finds an entry with:NEW|DELETE, this means this is short lived flow, not worth to waste cycles to offload it.
NEW|UPDATE, this means this is an UDP flow that is bidirectional.Then, there will be no more work allocation + "flying" work objects to the hardware offload workqueue. Instead, the hardware offload workqueue will be iterating.
This approach would need
_DONEflags to annotate if the offload updates have been applied to hardware already (similar to the conntrack_DONEflags).(Oh well, this proposal is adding even more flags. But I think flags are not the issue, but the mixture of the existing
flow_offload_workapproach with this new_UPDATEflag and the gc changes).If
flow_offload_workis removed, we would also need to add a:struct
nf_flowtable*flowtable;Quoted 8 lines
field to the
flow_offloadentry, which is an entry field that is passed viaflow_offload_work. So it is one extra field for the eachflow_offloadentry.The other alternative is to use the existing
nf_flow_offload_add_wqwith UPDATE command, which might result in more flying objects in turn. I think this is what you are trying to avoid with the_UPDATEflag approach.
Quoted 5 lines
This looks interesting, but is very ambitious and will probably be a bigger change than this whole series. I have an idea how we can leverage existing 'refresh' mechanism for updating flow state that doesn't involve large-scale refactoring of existing offload infrastructure, which I would prefer to try first. WDYT?
Update: to illustrate this point I prepared V4 that uses regular refresh to update the flow and also tries to prevent excessive wq spam or updating flow offload to a state that is already outdated.
PATCH v3 5/7 net/sched: act_ct: set ctinfo in meta action depending on ct state
In reply to Vlad BuslovCurrently tcf_ct_flow_table_add_action_meta() function assumes that only established connections can be offloaded and always sets ctinfo to either IP_CT_ESTABLISHED or IP_CT_ESTABLISHED_REPLY strictly based on direction without checking actual connection state. To enable UDP NEW connection offload set the ctinfo and metadata cookie based on ct->status value.
--- net/sched/act_ct.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
net/sched/act_ct.c +5 -2
@@ -182,8 +182,11 @@ static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct,#if IS_ENABLED (CONFIG_NF_CONNTRACK_MARK ) entry->ct_metadata.mark =READ_ONCE (ct->mark);#endif - ctinfo = dir ==IP_CT_DIR_ORIGINAL ?IP_CT_ESTABLISHED : -IP_CT_ESTABLISHED_REPLY ; +if (dir ==IP_CT_DIR_ORIGINAL ) + ctinfo =test_bit (IPS_SEEN_REPLY_BIT , &ct->status) ? +IP_CT_ESTABLISHED :IP_CT_NEW ; +else + ctinfo =IP_CT_ESTABLISHED_REPLY ;/* aligns with the CT reference on the SKB nf_ct_set */ entry->ct_metadata.cookie = (unsigned long )ct | ctinfo; entry->ct_metadata.orig_dir = dir ==IP_CT_DIR_ORIGINAL ;
Signature · 1 line
2.38.1
PATCH v3 6/7 net/sched: act_ct: offload UDP NEW connections
In reply to Vlad BuslovWhen processing connections allow offloading of UDP connections that don't have IPS_ASSURED_BIT set as unidirectional. When performing table lookup for reply packets check the current connection status: If UDP unidirectional connection became assured also promote the corresponding flow table entry to bidirectional and set the 'update' bit, else just set the 'update' bit since reply directional traffic will most likely cause connection status to become 'established' which requires updating the offload state.
--- net/sched/act_ct.c | 48 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-)
net/sched/act_ct.c +36 -12
@@ -368,7 +368,7 @@ static void tcf_ct_flow_tc_ifidx(struct flow_offload *entry,static void tcf_ct_flow_table_add (struct tcf_ct_flow_table *ct_ft,struct nf_conn *ct, -bool tcp) +bool tcp,bool bidirectional) {struct nf_conn_act_ct_ext *act_ct_ext;struct flow_offload *entry;
@@ -387,6 +387,8 @@ static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft, ct->proto.tcp.seen[0 ].flags |=IP_CT_TCP_FLAG_BE_LIBERAL ; ct->proto.tcp.seen[1 ].flags |=IP_CT_TCP_FLAG_BE_LIBERAL ; } +if (bidirectional) +__set_bit (NF_FLOW_HW_BIDIRECTIONAL , &entry->flags); act_ct_ext =nf_conn_act_ct_ext_find (ct);if (act_ct_ext) {
@@ -410,26 +412,34 @@ static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft,struct nf_conn *ct, enum ip_conntrack_info ctinfo) { - bool tcp = false; - -if ((ctinfo !=IP_CT_ESTABLISHED && ctinfo !=IP_CT_ESTABLISHED_REPLY ) || - !test_bit (IPS_ASSURED_BIT , &ct->status)) -return ; + bool tcp = false, bidirectional = true;switch (nf_ct_protonum (ct)) {case IPPROTO_TCP : - tcp = true; -if (ct->proto.tcp.state !=TCP_CONNTRACK_ESTABLISHED ) +if ((ctinfo !=IP_CT_ESTABLISHED && + ctinfo !=IP_CT_ESTABLISHED_REPLY ) || + !test_bit (IPS_ASSURED_BIT , &ct->status) || + ct->proto.tcp.state !=TCP_CONNTRACK_ESTABLISHED )return ; + + tcp = true;break ;case IPPROTO_UDP : +if (!nf_ct_is_confirmed (ct)) +return ; +if (!test_bit (IPS_ASSURED_BIT , &ct->status)) + bidirectional = false;break ;#ifdef CONFIG_NF_CT_PROTO_GRE case IPPROTO_GRE : {struct nf_conntrack_tuple *tuple; -if (ct->status &IPS_NAT_MASK ) +if ((ctinfo !=IP_CT_ESTABLISHED && + ctinfo !=IP_CT_ESTABLISHED_REPLY ) || + !test_bit (IPS_ASSURED_BIT , &ct->status) || + ct->status &IPS_NAT_MASK )return ; + tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple;/* No support for GRE v1 */ if (tuple->src.u.gre.key || tuple->dst.u.gre.key)
@@ -445,7 +455,7 @@ static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft, ct->status & IPS_SEQ_ADJUST)return ; -tcf_ct_flow_table_add (ct_ft, ct, tcp); +tcf_ct_flow_table_add (ct_ft, ct, tcp, bidirectional); } static bool
@@ -624,13 +634,27 @@ static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p, flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); ct = flow->ct; +if (dir ==FLOW_OFFLOAD_DIR_REPLY && + !test_bit (NF_FLOW_HW_BIDIRECTIONAL , &flow->flags)) { +/* Only offload reply direction after connection became +* assured. +*/ +if (test_bit (IPS_ASSURED_BIT , &ct->status)) +set_bit (NF_FLOW_HW_BIDIRECTIONAL , &flow->flags); +set_bit (NF_FLOW_HW_UPDATE , &flow->flags); +return false; + } +if (tcph && (unlikely (tcph->fin || tcph->rst))) {flow_offload_teardown (flow);return false; } - ctinfo = dir ==FLOW_OFFLOAD_DIR_ORIGINAL ?IP_CT_ESTABLISHED : -IP_CT_ESTABLISHED_REPLY ; +if (dir ==FLOW_OFFLOAD_DIR_ORIGINAL ) + ctinfo =test_bit (IPS_SEEN_REPLY_BIT , &ct->status) ? +IP_CT_ESTABLISHED :IP_CT_NEW ; +else + ctinfo =IP_CT_ESTABLISHED_REPLY ;flow_offload_refresh (nf_ft, flow);nf_conntrack_get (&ct->ct_general);
Signature · 1 line
2.38.1
PATCH v3 7/7 netfilter: nf_conntrack: allow early drop of offloaded UDP conns
In reply to Vlad BuslovBoth synchronous early drop algorithm and asynchronous gc worker completely ignore connections with IPS_OFFLOAD_BIT status bit set. With new functionality that enabled UDP NEW connection offload in action CT malicious user can flood the conntrack table with offloaded UDP connections by just sending a single packet per 5tuple because such connections can no longer be deleted by early drop algorithm.
To mitigate the issue allow both early drop and gc to consider offloaded UDP connections for deletion.
--- net/netfilter/nf_conntrack_core.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_core.c +6 -5
@@ -1374,9 +1374,6 @@ static unsigned int early_drop_list(struct net *net,hlist_nulls_for_each_entry_rcu (h, n, head, hnnode) { tmp =nf_ct_tuplehash_to_ctrack (h); -if (test_bit (IPS_OFFLOAD_BIT , &tmp->status)) -continue ; -if (nf_ct_is_expired (tmp)) {nf_ct_gc_expired (tmp);continue ;
@@ -1446,11 +1443,14 @@ static bool gc_worker_skip_ct(const struct nf_conn *ct)static bool gc_worker_can_early_drop (const struct nf_conn *ct) {const struct nf_conntrack_l4proto *l4proto; +u8 protonum =nf_ct_protonum (ct); +if (test_bit (IPS_OFFLOAD_BIT , &ct->status) && protonum !=IPPROTO_UDP ) +return false;if (!test_bit (IPS_ASSURED_BIT , &ct->status))return true; - l4proto =nf_ct_l4proto_find (nf_ct_protonum (ct)); + l4proto =nf_ct_l4proto_find (protonum);if (l4proto->can_early_drop && l4proto->can_early_drop (ct))return true;
@@ -1507,7 +1507,8 @@ static void gc_worker(struct work_struct *work)if (test_bit (IPS_OFFLOAD_BIT , &tmp->status)) {nf_ct_offload_timeout (tmp); -continue ; +if (!nf_conntrack_max95) +continue ; }if (expired_count >GC_SCAN_EXPIRED_MAX ) {
Signature · 1 line
2.38.1
Quoted 9 lines · Vlad Buslov
Currently only bidirectional established connections can be offloaded via
act_ct. Such approach allows to hardcode a lot of assumptions intoact_ct,flow_tableandflow_offloadintermediate layer codes. In order to enabled offloading of unidirectional UDP NEW connections start with incrementally changing the following assumptions:- Drivers assume that only established connections are offloaded and don't support updating existing connections. Extract ctinfo from meta action cookie and refuse offloading of new connections in the drivers.
Hi Vlad,
Regarding ct_seq_show(). When dumping the CT entries today, it will do things like:
if (!test_bit(IPS_OFFLOAD_BIT, &ct->status))
seq_printf(s, "%ld ", nf_ct_expires(ct) / HZ);omit the timeout, which is okay with this new patchset, but then:
if (test_bit(IPS_HW_OFFLOAD_BIT, &ct->status))
seq_puts(s, "[HW_OFFLOAD] ");
else if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
seq_puts(s, "[OFFLOAD] ");
else if (test_bit(IPS_ASSURED_BIT, &ct->status))
seq_puts(s, "[ASSURED] ");Previously, in order to be offloaded, it had to be Assured. But not anymore after this patchset. Thoughts?
Marcelo
Quoted 28 lines · Marcelo Ricardo Leitner
Quoted 9 lines · Vlad Buslov
Currently only bidirectional established connections can be offloaded via
act_ct. Such approach allows to hardcode a lot of assumptions intoact_ct,flow_tableandflow_offloadintermediate layer codes. In order to enabled offloading of unidirectional UDP NEW connections start with incrementally changing the following assumptions:- Drivers assume that only established connections are offloaded and don't support updating existing connections. Extract ctinfo from meta action cookie and refuse offloading of new connections in the drivers.
Hi Vlad,
Regarding
ct_seq_show(). When dumping the CT entries today, it will do things like:if (!test_bit(IPS_OFFLOAD_BIT, &ct->status)) seq_printf(s, "%ld ", nf_ct_expires(ct) / HZ);omit the timeout, which is okay with this new patchset, but then:
if (test_bit(IPS_HW_OFFLOAD_BIT, &ct->status)) seq_puts(s, "[HW_OFFLOAD] "); else if (test_bit(IPS_OFFLOAD_BIT, &ct->status)) seq_puts(s, "[OFFLOAD] "); else if (test_bit(IPS_ASSURED_BIT, &ct->status)) seq_puts(s, "[ASSURED] ");
Quoted 2 lines
Previously, in order to be offloaded, it had to be Assured. But not anymore after this patchset. Thoughts?
Hi Marcelo,
I know that for some reason offloaded entries no longer display 'assured' flag in the dump. This could be changed, but I don't have a preference either way and this patch set doesn't modify the behavior. Up to you and maintainers I guess.
Quoted 32 lines · Vlad Buslov
Quoted 28 lines · Marcelo Ricardo Leitner
Quoted 9 lines · Vlad Buslov
Currently only bidirectional established connections can be offloaded via
act_ct. Such approach allows to hardcode a lot of assumptions intoact_ct,flow_tableandflow_offloadintermediate layer codes. In order to enabled offloading of unidirectional UDP NEW connections start with incrementally changing the following assumptions:- Drivers assume that only established connections are offloaded and don't support updating existing connections. Extract ctinfo from meta action cookie and refuse offloading of new connections in the drivers.
Hi Vlad,
Regarding
ct_seq_show(). When dumping the CT entries today, it will do things like:if (!test_bit(IPS_OFFLOAD_BIT, &ct->status)) seq_printf(s, "%ld ", nf_ct_expires(ct) / HZ);omit the timeout, which is okay with this new patchset, but then:
if (test_bit(IPS_HW_OFFLOAD_BIT, &ct->status)) seq_puts(s, "[HW_OFFLOAD] "); else if (test_bit(IPS_OFFLOAD_BIT, &ct->status)) seq_puts(s, "[OFFLOAD] "); else if (test_bit(IPS_ASSURED_BIT, &ct->status)) seq_puts(s, "[ASSURED] ");Quoted 2 lines
Previously, in order to be offloaded, it had to be Assured. But not anymore after this patchset. Thoughts?
Quoted 6 lines
Hi Marcelo,
I know that for some reason offloaded entries no longer display 'assured' flag in the dump. This could be changed, but I don't have a preference either way and this patch set doesn't modify the behavior. Up to you and maintainers I guess.
BTW after checking the log I don't think the assumption that all offloaded connections are always assured is true. As far as I understand act_ct originally offloaded established connections and change to offload assured was made relatively recently in 43332cf97425 ("net/sched: act_ct: Offload only ASSURED connections") without modifying the prints you mentioned.
Marcelo Ricardo Leitner
Reviewed-byQuoted 40 lines · Vlad Buslov
Quoted 32 lines · Vlad Buslov
Quoted 28 lines · Marcelo Ricardo Leitner
Quoted 9 lines · Vlad Buslov
Currently only bidirectional established connections can be offloaded via
act_ct. Such approach allows to hardcode a lot of assumptions intoact_ct,flow_tableandflow_offloadintermediate layer codes. In order to enabled offloading of unidirectional UDP NEW connections start with incrementally changing the following assumptions:- Drivers assume that only established connections are offloaded and don't support updating existing connections. Extract ctinfo from meta action cookie and refuse offloading of new connections in the drivers.
Hi Vlad,
Regarding
ct_seq_show(). When dumping the CT entries today, it will do things like:if (!test_bit(IPS_OFFLOAD_BIT, &ct->status)) seq_printf(s, "%ld ", nf_ct_expires(ct) / HZ);omit the timeout, which is okay with this new patchset, but then:
if (test_bit(IPS_HW_OFFLOAD_BIT, &ct->status)) seq_puts(s, "[HW_OFFLOAD] "); else if (test_bit(IPS_OFFLOAD_BIT, &ct->status)) seq_puts(s, "[OFFLOAD] "); else if (test_bit(IPS_ASSURED_BIT, &ct->status)) seq_puts(s, "[ASSURED] ");Quoted 2 lines
Previously, in order to be offloaded, it had to be Assured. But not anymore after this patchset. Thoughts?
Quoted 6 lines
Hi Marcelo,
I know that for some reason offloaded entries no longer display 'assured' flag in the dump. This could be changed, but I don't have a preference either way and this patch set doesn't modify the behavior. Up to you and maintainers I guess.
Quoted 6 lines
BTW after checking the log I don't think the assumption that all offloaded connections are always assured is true. As far as I understand
act_ctoriginally offloaded established connections and change to offload assured was made relatively recently in43332cf97425("net/sched:act_ct: Offload only ASSURED connections") without modifying the prints you mentioned.
Oh. Somehow this behavior glued to my mind as it was always there. Not sure which glue was used, please don't ask :D
Thanks!